2 * Copyright 2004-2005 Timo Hirvonen
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 #include "ui_curses.h"
22 #include "search_mode.h"
23 #include "command_mode.h"
25 #include "play_queue.h"
36 #include "format_print.h"
50 #include <sys/ioctl.h>
59 #if defined(__sun__) || defined(__CYGWIN__)
67 /* defined in <term.h> but without const */
68 char *tgetstr(const char *id
, char **area
);
69 char *tgoto(const char *cap
, int col
, int row
);
71 /* globals. documented in ui_curses.h */
74 int ui_initialized
= 0;
75 enum ui_input_mode input_mode
= NORMAL_MODE
;
76 int cur_view
= TREE_VIEW
;
77 struct searchable
*searchable
;
78 char *lib_filename
= NULL
;
79 char *pl_filename
= NULL
;
81 /* ------------------------------------------------------------------------- */
83 static char *lib_autosave_filename
;
84 static char *pl_autosave_filename
;
86 /* shown error message and time stamp
87 * error is cleared if it is older than 3s and key was pressed
89 static char error_buf
[512];
90 static time_t error_time
= 0;
91 /* info messages are displayed in different color */
92 static int msg_is_error
;
93 static int error_count
= 0;
95 static char *server_address
= NULL
;
97 static char *charset
= NULL
;
98 static char print_buffer
[512];
100 /* destination buffer for utf8_encode and utf8_decode */
101 static char conv_buffer
[512];
103 #define print_buffer_size (sizeof(print_buffer) - 1)
104 static int using_utf8
;
106 static const char *t_ts
;
107 static const char *t_fs
;
109 static int tree_win_x
= 0;
110 static int tree_win_y
= 0;
111 static int tree_win_w
= 0;
113 static int track_win_x
= 0;
114 static int track_win_y
= 0;
115 static int track_win_w
= 0;
127 CURSED_WIN_ACTIVE_CUR
,
128 CURSED_WIN_ACTIVE_SEL
,
129 CURSED_WIN_ACTIVE_SEL_CUR
,
144 static unsigned char cursed_to_bg_idx
[NR_CURSED
] = {
147 COLOR_WIN_INACTIVE_SEL_BG
,
148 COLOR_WIN_INACTIVE_CUR_SEL_BG
,
153 COLOR_WIN_CUR_SEL_BG
,
166 static unsigned char cursed_to_fg_idx
[NR_CURSED
] = {
169 COLOR_WIN_INACTIVE_SEL_FG
,
170 COLOR_WIN_INACTIVE_CUR_SEL_FG
,
175 COLOR_WIN_CUR_SEL_FG
,
188 /* index is CURSED_*, value is fucking color pair */
189 static int pairs
[NR_CURSED
];
205 static struct format_option track_fopts
[NR_TFS
+ 1] = {
235 static struct format_option status_fopts
[NR_SFS
+ 1] = {
251 static void utf8_encode(const char *buffer
)
253 static iconv_t cd
= (iconv_t
)-1;
259 if (cd
== (iconv_t
)-1) {
260 d_print("iconv_open(UTF-8, %s)\n", charset
);
261 cd
= iconv_open("UTF-8", charset
);
262 if (cd
== (iconv_t
)-1) {
263 d_print("iconv_open failed: %s\n", strerror(errno
));
270 os
= sizeof(conv_buffer
) - 1;
271 rc
= iconv(cd
, (void *)&i
, &is
, &o
, &os
);
274 d_print("iconv failed: %s\n", strerror(errno
));
279 static void utf8_decode(const char *buffer
)
281 static iconv_t cd
= (iconv_t
)-1;
287 if (cd
== (iconv_t
)-1) {
288 d_print("iconv_open(%s, UTF-8)\n", charset
);
289 cd
= iconv_open(charset
, "UTF-8");
290 if (cd
== (iconv_t
)-1) {
291 d_print("iconv_open failed: %s\n", strerror(errno
));
298 os
= sizeof(conv_buffer
) - 1;
299 rc
= iconv(cd
, (void *)&i
, &is
, &o
, &os
);
302 d_print("iconv failed: %s\n", strerror(errno
));
307 /* screen updates {{{ */
309 static void dump_print_buffer(int row
, int col
)
312 mvaddstr(row
, col
, print_buffer
);
314 utf8_decode(print_buffer
);
315 mvaddstr(row
, col
, conv_buffer
);
319 /* print @str into @buf
321 * if @str is shorter than @width pad with spaces
322 * if @str is wider than @width truncate and add "..."
324 static int format_str(char *buf
, const char *str
, int width
)
326 int s
= 0, d
= 0, ellipsis_pos
= 0, cut_double_width
= 0;
332 u_get_char(str
, &s
, &u
);
334 memset(buf
+ d
, ' ', width
);
342 if (width
== 4 && w
== 2) {
343 /* can't cut double-width char */
344 ellipsis_pos
= d
+ 1;
345 cut_double_width
= 1;
352 if (cut_double_width
) {
353 /* first half of the double-width char */
361 u_set_char(buf
, &d
, u
);
366 static void sprint(int row
, int col
, const char *str
, int width
)
370 print_buffer
[pos
++] = ' ';
371 pos
+= format_str(print_buffer
+ pos
, str
, width
- 2);
372 print_buffer
[pos
++] = ' ';
373 print_buffer
[pos
] = 0;
374 dump_print_buffer(row
, col
);
377 static void sprint_ascii(int row
, int col
, const char *str
, int len
)
384 print_buffer
[0] = ' ';
386 memcpy(print_buffer
+ 1, str
, len
- 3);
387 print_buffer
[len
- 2] = '.';
388 print_buffer
[len
- 1] = '.';
389 print_buffer
[len
- 0] = '.';
391 memcpy(print_buffer
+ 1, str
, l
);
392 memset(print_buffer
+ 1 + l
, ' ', len
- l
);
394 print_buffer
[len
+ 1] = ' ';
395 print_buffer
[len
+ 2] = 0;
396 mvaddstr(row
, col
, print_buffer
);
399 static void print_tree(struct window
*win
, int row
, struct iter
*iter
)
402 struct artist
*artist
;
405 int current
, selected
, active
, pos
;
407 artist
= iter_to_artist(iter
);
408 album
= iter_to_album(iter
);
412 current
= CUR_ALBUM
== album
;
414 current
= CUR_ARTIST
== artist
;
417 window_get_sel(win
, &sel
);
418 selected
= iters_equal(iter
, &sel
);
419 active
= lib_cur_win
== lib_tree_win
;
420 bkgdset(pairs
[(active
<< 2) | (selected
<< 1) | current
]);
422 if (active
&& selected
) {
428 print_buffer
[pos
++] = ' ';
431 print_buffer
[pos
++] = ' ';
432 print_buffer
[pos
++] = ' ';
435 pos
+= format_str(print_buffer
+ pos
, str
, tree_win_w
- pos
- 1);
436 print_buffer
[pos
++] = ' ';
437 print_buffer
[pos
++] = 0;
438 dump_print_buffer(tree_win_y
+ row
+ 1, tree_win_x
);
441 static inline void fopt_set_str(struct format_option
*fopt
, const char *str
)
443 BUG_ON(fopt
->type
!= FO_STR
);
452 static inline void fopt_set_int(struct format_option
*fopt
, int value
, int empty
)
454 BUG_ON(fopt
->type
!= FO_INT
);
455 fopt
->fo_int
= value
;
459 static inline void fopt_set_time(struct format_option
*fopt
, int value
, int empty
)
461 BUG_ON(fopt
->type
!= FO_TIME
);
462 fopt
->fo_time
= value
;
466 static void fill_track_fopts_track_info(struct track_info
*info
)
472 filename
= info
->filename
;
474 utf8_encode(info
->filename
);
475 filename
= conv_buffer
;
477 disc
= comments_get_int(info
->comments
, "discnumber");
478 num
= comments_get_int(info
->comments
, "tracknumber");
480 fopt_set_str(&track_fopts
[TF_ARTIST
], comments_get_val(info
->comments
, "artist"));
481 fopt_set_str(&track_fopts
[TF_ALBUM
], comments_get_val(info
->comments
, "album"));
482 fopt_set_int(&track_fopts
[TF_DISC
], disc
, disc
== -1);
483 fopt_set_int(&track_fopts
[TF_TRACK
], num
, num
== -1);
484 fopt_set_str(&track_fopts
[TF_TITLE
], comments_get_val(info
->comments
, "title"));
485 fopt_set_str(&track_fopts
[TF_YEAR
], comments_get_val(info
->comments
, "date"));
486 fopt_set_str(&track_fopts
[TF_GENRE
], comments_get_val(info
->comments
, "genre"));
487 fopt_set_time(&track_fopts
[TF_DURATION
], info
->duration
, info
->duration
== -1);
488 fopt_set_str(&track_fopts
[TF_PATHFILE
], filename
);
489 if (is_url(info
->filename
)) {
490 fopt_set_str(&track_fopts
[TF_FILE
], filename
);
494 f
= strrchr(filename
, '/');
496 fopt_set_str(&track_fopts
[TF_FILE
], f
+ 1);
498 fopt_set_str(&track_fopts
[TF_FILE
], filename
);
503 static void print_track(struct window
*win
, int row
, struct iter
*iter
)
505 struct tree_track
*track
;
507 int current
, selected
, active
;
509 track
= iter_to_tree_track(iter
);
510 current
= lib_cur_track
== track
;
511 window_get_sel(win
, &sel
);
512 selected
= iters_equal(iter
, &sel
);
513 active
= lib_cur_win
== lib_track_win
;
514 bkgdset(pairs
[(active
<< 2) | (selected
<< 1) | current
]);
516 if (active
&& selected
) {
517 cursor_x
= track_win_x
;
521 fill_track_fopts_track_info(tree_track_info(track
));
523 if (track_info_has_tag(tree_track_info(track
))) {
524 format_print(print_buffer
, track_win_w
, track_win_format
, track_fopts
);
526 format_print(print_buffer
, track_win_w
, track_win_alt_format
, track_fopts
);
528 dump_print_buffer(track_win_y
+ row
+ 1, track_win_x
);
531 /* used by print_editable only */
532 static struct simple_track
*current_track
;
534 static void print_editable(struct window
*win
, int row
, struct iter
*iter
)
536 struct simple_track
*track
;
538 int current
, selected
, active
;
540 track
= iter_to_simple_track(iter
);
541 current
= current_track
== track
;
542 window_get_sel(win
, &sel
);
543 selected
= iters_equal(iter
, &sel
);
551 if (!selected
&& track
->marked
) {
556 bkgdset(pairs
[(active
<< 2) | (selected
<< 1) | current
]);
558 fill_track_fopts_track_info(track
->info
);
560 if (track_info_has_tag(track
->info
)) {
561 format_print(print_buffer
, COLS
, list_win_format
, track_fopts
);
563 format_print(print_buffer
, COLS
, list_win_alt_format
, track_fopts
);
565 dump_print_buffer(row
+ 1, 0);
568 static void print_browser(struct window
*win
, int row
, struct iter
*iter
)
570 struct browser_entry
*e
;
574 e
= iter_to_browser_entry(iter
);
575 window_get_sel(win
, &sel
);
576 selected
= iters_equal(iter
, &sel
);
581 bkgdset(pairs
[(active
<< 2) | (selected
<< 1) | current
]);
583 if (e
->type
== BROWSER_ENTRY_DIR
) {
584 bkgdset(pairs
[CURSED_DIR
]);
586 bkgdset(pairs
[CURSED_WIN
]);
595 /* file name encoding == terminal encoding. no need to convert */
597 sprint(row
+ 1, 0, e
->name
, COLS
);
599 sprint_ascii(row
+ 1, 0, e
->name
, COLS
);
603 static void print_filter(struct window
*win
, int row
, struct iter
*iter
)
606 struct filter_entry
*e
= iter_to_filter_entry(iter
);
612 /* is the filter currently active? */
613 int current
= !!e
->act_stat
;
614 const char stat_chars
[3] = " *!";
615 int ch1
, ch2
, ch3
, pos
;
617 window_get_sel(win
, &sel
);
618 selected
= iters_equal(iter
, &sel
);
619 bkgdset(pairs
[(active
<< 2) | (selected
<< 1) | current
]);
628 if (e
->sel_stat
!= e
->act_stat
) {
632 ch2
= stat_chars
[e
->sel_stat
];
633 snprintf(buf
, sizeof(buf
), "%c%c%c%-15s %s", ch1
, ch2
, ch3
, e
->name
, e
->filter
);
634 pos
= format_str(print_buffer
, buf
, COLS
- 1);
635 print_buffer
[pos
++] = ' ';
636 print_buffer
[pos
] = 0;
637 dump_print_buffer(row
+ 1, 0);
640 static void print_help(struct window
*win
, int row
, struct iter
*iter
)
647 const struct help_entry
*e
= iter_to_help_entry(iter
);
648 const struct cmus_opt
*opt
;
650 window_get_sel(win
, &sel
);
651 selected
= iters_equal(iter
, &sel
);
652 bkgdset(pairs
[(active
<< 2) | (selected
<< 1)]);
661 snprintf(buf
, sizeof(buf
), " %s", e
->text
);
664 snprintf(buf
, sizeof(buf
), " %-8s %-14s %s",
665 key_context_names
[e
->binding
->ctx
],
666 e
->binding
->key
->name
,
670 snprintf(buf
, sizeof(buf
), " %s", e
->command
->name
);
674 snprintf(buf
, sizeof(buf
), " %-29s ", opt
->name
);
675 opt
->get(opt
->id
, buf
+ strlen(buf
));
678 pos
= format_str(print_buffer
, buf
, COLS
- 1);
679 print_buffer
[pos
++] = ' ';
680 print_buffer
[pos
] = 0;
681 dump_print_buffer(row
+ 1, 0);
684 static void update_window(struct window
*win
, int x
, int y
, int w
, const char *title
,
685 void (*print
)(struct window
*, int, struct iter
*))
693 bkgdset(pairs
[CURSED_WIN_TITLE
]);
694 c
= snprintf(print_buffer
, w
+ 1, " %s", title
);
697 memset(print_buffer
+ c
, ' ', w
- c
+ 1);
699 dump_print_buffer(y
, x
);
700 nr_rows
= window_get_nr_rows(win
);
702 if (window_get_top(win
, &iter
)) {
703 while (i
< nr_rows
) {
704 print(win
, i
, &iter
);
706 if (!window_get_next(win
, &iter
))
712 memset(print_buffer
, ' ', w
);
714 while (i
< nr_rows
) {
715 dump_print_buffer(y
+ i
+ 1, x
);
720 static void update_tree_window(void)
722 update_window(lib_tree_win
, tree_win_x
, tree_win_y
,
723 tree_win_w
, "Artist / Album", print_tree
);
726 static void update_track_window(void)
730 /* it doesn't matter what format options we use because the format
731 * string does not contain any format charaters */
732 format_print(title
, track_win_w
- 2, "Track%=Library", track_fopts
);
733 update_window(lib_track_win
, track_win_x
, track_win_y
,
734 track_win_w
, title
, print_track
);
737 static const char *pretty(const char *path
)
739 static int home_len
= -1;
740 static char buf
[256];
743 home_len
= strlen(home_dir
);
745 if (strncmp(path
, home_dir
, home_len
) || path
[home_len
] != '/')
749 strcpy(buf
+ 1, path
+ home_len
);
753 static const char * const sorted_names
[2] = { "", "sorted by " };
755 static void update_editable_window(struct editable
*e
, const char *title
, const char *filename
)
764 utf8_encode(filename
);
765 filename
= conv_buffer
;
767 snprintf(buf
, sizeof(buf
), "%s %s - %d tracks", title
,
768 pretty(filename
), e
->nr_tracks
);
770 snprintf(buf
, sizeof(buf
), "%s - %d tracks", title
, e
->nr_tracks
);
775 snprintf(buf
+ pos
, sizeof(buf
) - pos
, " (%d marked)", e
->nr_marked
);
778 snprintf(buf
+ pos
, sizeof(buf
) - pos
, " %s%s",
779 sorted_names
[e
->sort_str
[0] != 0], e
->sort_str
);
781 update_window(e
->win
, 0, 0, COLS
, buf
, &print_editable
);
784 static void update_sorted_window(void)
786 current_track
= (struct simple_track
*)lib_cur_track
;
787 update_editable_window(&lib_editable
, "Library", lib_filename
);
790 static void update_pl_window(void)
792 current_track
= pl_cur_track
;
793 update_editable_window(&pl_editable
, "Playlist", pl_filename
);
796 static void update_play_queue_window(void)
798 current_track
= NULL
;
799 update_editable_window(&pq_editable
, "Play Queue", NULL
);
802 static void update_browser_window(void)
809 dirname
= browser_dir
;
811 utf8_encode(browser_dir
);
812 dirname
= conv_buffer
;
814 snprintf(title
, sizeof(title
), "Browser - %s", dirname
);
815 update_window(browser_win
, 0, 0, COLS
, title
, print_browser
);
818 static void update_filters_window(void)
820 update_window(filters_win
, 0, 0, COLS
, "Library Filters", print_filter
);
823 static void update_help_window(void)
825 update_window(help_win
, 0, 0, COLS
, "Settings", print_help
);
828 static void draw_separator(void)
832 bkgdset(pairs
[CURSED_WIN_TITLE
]);
833 mvaddch(0, tree_win_w
, ' ');
834 bkgdset(pairs
[CURSED_SEPARATOR
]);
835 for (row
= 1; row
< LINES
- 3; row
++)
836 mvaddch(row
, tree_win_w
, ACS_VLINE
);
839 static void do_update_view(int full
)
847 if (full
|| lib_tree_win
->changed
)
848 update_tree_window();
849 if (full
|| lib_track_win
->changed
)
850 update_track_window();
856 update_sorted_window();
866 update_play_queue_window();
870 update_browser_window();
873 update_filters_window();
876 update_help_window();
881 static void do_update_statusline(void)
883 static const char *status_strs
[] = { ".", ">", "|" };
884 static const char *cont_strs
[] = { " ", "C" };
885 static const char *repeat_strs
[] = { " ", "R" };
886 static const char *shuffle_strs
[] = { " ", "S" };
887 int buffer_fill
, vol
, vol_left
, vol_right
;
893 fopt_set_time(&status_fopts
[SF_TOTAL
], play_library
? lib_editable
.total_time
:
894 pl_editable
.total_time
, 0);
897 fopt_set_str(&status_fopts
[SF_REPEAT
], repeat_strs
[repeat
]);
898 fopt_set_str(&status_fopts
[SF_SHUFFLE
], shuffle_strs
[shuffle
]);
899 fopt_set_str(&status_fopts
[SF_PLAYLISTMODE
], aaa_mode_names
[aaa_mode
]);
904 duration
= player_info
.ti
->duration
;
906 if (volume_max
== 0) {
907 vol_left
= vol_right
= vol
= -1;
909 vol_left
= scale_to_percentage(player_info
.vol_left
, volume_max
);
910 vol_right
= scale_to_percentage(player_info
.vol_right
, volume_max
);
911 vol
= (vol_left
+ vol_right
+ 1) / 2;
913 buffer_fill
= scale_to_percentage(player_info
.buffer_fill
, player_info
.buffer_size
);
915 fopt_set_str(&status_fopts
[SF_STATUS
], status_strs
[player_info
.status
]);
917 if (show_remaining_time
&& duration
!= -1) {
918 fopt_set_time(&status_fopts
[SF_POSITION
], player_info
.pos
- duration
, 0);
920 fopt_set_time(&status_fopts
[SF_POSITION
], player_info
.pos
, 0);
923 fopt_set_time(&status_fopts
[SF_DURATION
], duration
, 0);
924 fopt_set_int(&status_fopts
[SF_VOLUME
], vol
, 0);
925 fopt_set_int(&status_fopts
[SF_LVOLUME
], vol_left
, 0);
926 fopt_set_int(&status_fopts
[SF_RVOLUME
], vol_right
, 0);
927 fopt_set_int(&status_fopts
[SF_BUFFER
], buffer_fill
, 0);
928 fopt_set_str(&status_fopts
[SF_CONTINUE
], cont_strs
[player_cont
]);
930 strcpy(format
, " %s %p ");
932 strcat(format
, "/ %d ");
933 strcat(format
, "- %t ");
934 if (volume_max
!= 0) {
935 if (player_info
.vol_left
!= player_info
.vol_right
) {
936 strcat(format
, "vol: %l,%r ");
938 strcat(format
, "vol: %v ");
941 if (player_info
.ti
&& is_url(player_info
.ti
->filename
))
942 strcat(format
, "buf: %b ");
943 strcat(format
, "%=");
945 /* artist/album modes work only in lib */
947 /* shuffle overrides sorted mode */
948 strcat(format
, "%L from library");
949 } else if (play_sorted
) {
950 strcat(format
, "%L from sorted library");
952 strcat(format
, "%L from library");
955 strcat(format
, "playlist");
957 strcat(format
, " | %1C%1R%1S ");
958 format_print(print_buffer
, COLS
, format
, status_fopts
);
960 msg
= player_info
.error_msg
;
961 player_info
.error_msg
= NULL
;
963 player_info_unlock();
965 bkgdset(pairs
[CURSED_STATUSLINE
]);
966 dump_print_buffer(LINES
- 2, 0);
969 error_msg("%s", msg
);
974 static void dump_buffer(const char *buffer
)
984 static void do_update_commandline(void)
993 bkgdset(pairs
[CURSED_ERROR
]);
995 bkgdset(pairs
[CURSED_INFO
]);
1001 bkgdset(pairs
[CURSED_COMMANDLINE
]);
1002 if (input_mode
== NORMAL_MODE
) {
1009 /* cmdline.line actually pretends to be UTF-8 but all non-ASCII
1010 * characters are invalid UTF-8 so it really is in locale's
1013 * This code should be safe because cmdline.bpos ==
1014 * cmdline.cpos as every non-ASCII character is counted as one
1015 * invalid UTF-8 byte.
1017 * NOTE: This has nothing to do with widths of printed
1018 * characters. I.e. even if there were control characters
1019 * (displayed as <xx>) there would be no problem because bpos
1020 * still equals to cpos, I think.
1022 utf8_encode(cmdline
.line
);
1026 /* COMMAND_MODE or SEARCH_MODE */
1027 w
= u_str_width(str
);
1029 if (input_mode
== SEARCH_MODE
)
1030 ch
= search_direction
== SEARCH_FORWARD
? '/' : '?';
1032 if (w
<= COLS
- 2) {
1034 idx
= u_copy_chars(print_buffer
, str
, &w
);
1035 print_buffer
[idx
] = 0;
1036 dump_buffer(print_buffer
);
1039 /* keep cursor as far right as possible */
1040 int skip
, width
, cw
;
1042 /* cursor pos (width, not chars. doesn't count the ':') */
1043 cw
= u_str_nwidth(str
, cmdline
.cpos
);
1045 skip
= cw
+ 2 - COLS
;
1050 /* skip rest (if any) */
1051 idx
= u_skip_chars(str
, &skip
);
1054 idx
= u_copy_chars(print_buffer
, str
+ idx
, &width
);
1055 while (width
< COLS
) {
1056 /* cursor is at end of the buffer
1057 * print 1, 2 or 3 spaces
1061 * If the last _skipped_ character was double-width we may need
1062 * to print 2 spaces.
1064 * If the last _skipped_ character was invalid UTF-8 we may need
1065 * to print 3 spaces.
1067 print_buffer
[idx
++] = ' ';
1070 print_buffer
[idx
] = 0;
1071 dump_buffer(print_buffer
);
1073 /* print ':' + COLS - 1 chars */
1076 idx
= u_copy_chars(print_buffer
, str
, &width
);
1077 print_buffer
[idx
] = 0;
1078 dump_buffer(print_buffer
);
1083 /* lock player_info! */
1084 static const char *get_stream_title(void)
1086 static char stream_title
[255 * 16 + 1];
1089 ptr
= strstr(player_info
.metadata
, "StreamTitle='");
1095 if (*ptr
== '\'' && *(ptr
+ 1) == ';') {
1096 memcpy(stream_title
, title
, ptr
- title
);
1097 stream_title
[ptr
- title
] = 0;
1098 return stream_title
;
1105 static void set_title(const char *title
)
1107 if (!set_term_title
)
1111 printf("%s%s%s", tgoto(t_ts
, 0, 0), title
, t_fs
);
1116 static void do_update_titleline(void)
1118 bkgdset(pairs
[CURSED_TITLELINE
]);
1120 if (player_info
.ti
) {
1121 int i
, use_alt_format
= 0;
1124 fill_track_fopts_track_info(player_info
.ti
);
1125 if (is_url(player_info
.ti
->filename
)) {
1126 const char *title
= get_stream_title();
1130 fopt_set_str(&track_fopts
[TF_TITLE
], title
);
1132 use_alt_format
= !track_info_has_tag(player_info
.ti
);
1135 if (use_alt_format
) {
1136 format_print(print_buffer
, COLS
, current_alt_format
, track_fopts
);
1138 format_print(print_buffer
, COLS
, current_format
, track_fopts
);
1140 dump_print_buffer(LINES
- 3, 0);
1142 /* set window title */
1143 if (use_alt_format
) {
1144 format_print(print_buffer
, sizeof(print_buffer
) - 1,
1145 window_title_alt_format
, track_fopts
);
1147 format_print(print_buffer
, sizeof(print_buffer
) - 1,
1148 window_title_format
, track_fopts
);
1151 /* remove whitespace */
1152 i
= strlen(print_buffer
) - 1;
1153 while (i
> 0 && print_buffer
[i
] == ' ')
1155 print_buffer
[i
+ 1] = 0;
1158 wtitle
= print_buffer
;
1160 utf8_decode(print_buffer
);
1161 wtitle
= conv_buffer
;
1169 set_title("cmus " VERSION
);
1171 player_info_unlock();
1174 static int cmdline_cursor_column(void)
1181 /* see do_update_commandline */
1182 utf8_encode(cmdline
.line
);
1186 /* width of the text in the buffer before cursor */
1187 cw
= u_str_nwidth(str
, cmdline
.cpos
);
1189 if (1 + cw
< COLS
) {
1190 /* whole line is visible */
1194 /* beginning of cmdline is not visible */
1196 /* check if the first visible char in cmdline would be halved
1197 * double-width character (or invalid byte <xx>) which is not possible.
1198 * we need to skip the whole character and move cursor to COLS - 2
1200 skip
= cw
+ 2 - COLS
;
1207 u_skip_chars(str
, &s
);
1209 /* the last skipped char was double-width or <xx> */
1210 return COLS
- 1 - (s
- skip
);
1215 static void post_update(void)
1217 /* refresh makes cursor visible at least for urxvt */
1218 if (input_mode
== COMMAND_MODE
|| input_mode
== SEARCH_MODE
) {
1219 move(LINES
- 1, cmdline_cursor_column());
1223 if (cursor_x
>= 0) {
1224 move(cursor_y
, cursor_x
);
1233 void update_titleline(void)
1236 do_update_titleline();
1240 void update_full(void)
1242 if (!ui_initialized
)
1248 do_update_titleline();
1249 do_update_statusline();
1250 do_update_commandline();
1255 static void update_commandline(void)
1258 do_update_commandline();
1262 void update_statusline(void)
1264 if (!ui_initialized
)
1268 do_update_statusline();
1272 void info_msg(const char *format
, ...)
1276 va_start(ap
, format
);
1277 vsnprintf(error_buf
, sizeof(error_buf
), format
, ap
);
1282 update_commandline();
1285 void error_msg(const char *format
, ...)
1289 strcpy(error_buf
, "Error: ");
1290 va_start(ap
, format
);
1291 vsnprintf(error_buf
+ 7, sizeof(error_buf
) - 7, format
, ap
);
1294 d_print("%s\n", error_buf
);
1299 if (ui_initialized
) {
1300 error_time
= time(NULL
);
1301 update_commandline();
1303 warn("%s\n", error_buf
);
1308 int yes_no_query(const char *format
, ...)
1314 va_start(ap
, format
);
1315 vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
1319 bkgdset(pairs
[CURSED_INFO
]);
1321 /* no need to convert buffer.
1322 * it is always encoded in the right charset (assuming filenames are
1323 * encoded in same charset as LC_CTYPE).
1333 if (ch
== ERR
|| ch
== 0)
1339 update_commandline();
1343 void search_not_found(void)
1345 const char *what
= "Track";
1347 if (search_restricted
) {
1350 what
= "Artist/album";
1358 what
= "File/Directory";
1364 what
= "Binding/command/option";
1376 what
= "File/Directory";
1382 what
= "Binding/command/option";
1386 info_msg("%s not found: %s", what
, search_str
? : "");
1389 void set_view(int view
)
1391 if (view
== cur_view
)
1397 searchable
= tree_searchable
;
1400 searchable
= lib_editable
.searchable
;
1403 searchable
= pl_editable
.searchable
;
1406 searchable
= pq_editable
.searchable
;
1409 searchable
= browser_searchable
;
1412 searchable
= filters_searchable
;
1415 searchable
= help_searchable
;
1416 update_help_window();
1425 void enter_command_mode(void)
1429 input_mode
= COMMAND_MODE
;
1430 update_commandline();
1433 void enter_search_mode(void)
1437 input_mode
= SEARCH_MODE
;
1438 search_direction
= SEARCH_FORWARD
;
1439 update_commandline();
1442 void enter_search_backward_mode(void)
1446 input_mode
= SEARCH_MODE
;
1447 search_direction
= SEARCH_BACKWARD
;
1448 update_commandline();
1451 void update_colors(void)
1455 if (!ui_initialized
)
1458 for (i
= 0; i
< NR_CURSED
; i
++) {
1459 int bg
= colors
[cursed_to_bg_idx
[i
]];
1460 int fg
= colors
[cursed_to_fg_idx
[i
]];
1463 if (fg
>= 8 && fg
<= 15) {
1464 /* fg colors 8..15 are special (0..7 + bold) */
1465 init_pair(pair
, fg
& 7, bg
);
1466 pairs
[i
] = COLOR_PAIR(pair
) | (fg
& BRIGHT
? A_BOLD
: 0);
1468 init_pair(pair
, fg
, bg
);
1469 pairs
[i
] = COLOR_PAIR(pair
);
1474 static void clear_error(void)
1476 time_t t
= time(NULL
);
1478 /* prevent accidental clearing of error messages */
1479 if (t
- error_time
< 2)
1485 update_commandline();
1489 /* screen updates }}} */
1491 static void spawn_status_program(void)
1493 static const char *status_strs
[] = { "stopped", "playing", "paused" };
1494 const char *stream_title
= NULL
;
1498 if (status_display_program
== NULL
|| status_display_program
[0] == 0)
1502 status
= player_info
.status
;
1504 stream_title
= get_stream_title();
1507 argv
[i
++] = xstrdup(status_display_program
);
1509 argv
[i
++] = xstrdup("status");
1510 argv
[i
++] = xstrdup(status_strs
[status
]);
1511 if (player_info
.ti
) {
1512 static const char *keys
[] = {
1513 "artist", "album", "discnumber", "tracknumber", "title", "date", NULL
1517 if (is_url(player_info
.ti
->filename
)) {
1518 argv
[i
++] = xstrdup("url");
1519 argv
[i
++] = xstrdup(player_info
.ti
->filename
);
1521 argv
[i
++] = xstrdup("title");
1522 argv
[i
++] = xstrdup(stream_title
);
1527 argv
[i
++] = xstrdup("file");
1528 argv
[i
++] = xstrdup(player_info
.ti
->filename
);
1529 for (j
= 0; keys
[j
]; j
++) {
1530 const char *key
= keys
[j
];
1533 val
= comments_get_val(player_info
.ti
->comments
, key
);
1535 argv
[i
++] = xstrdup(key
);
1536 argv
[i
++] = xstrdup(val
);
1539 snprintf(buf
, sizeof(buf
), "%d", player_info
.ti
->duration
);
1540 argv
[i
++] = xstrdup("duration");
1541 argv
[i
++] = xstrdup(buf
);
1545 player_info_unlock();
1547 if (spawn(argv
, &status
) == -1)
1548 error_msg("couldn't run `%s': %s", status_display_program
, strerror(errno
));
1549 for (i
= 0; argv
[i
]; i
++)
1553 static int ctrl_c_pressed
= 0;
1555 static void sig_int(int sig
)
1560 static int needs_to_resize
= 1;
1562 static void sig_winch(int sig
)
1564 needs_to_resize
= 1;
1567 static int get_window_size(int *lines
, int *columns
)
1571 if (ioctl(0, TIOCGWINSZ
, &ws
) == -1)
1573 *columns
= ws
.ws_col
;
1578 static void resize_tree_view(int w
, int h
)
1581 track_win_w
= w
- tree_win_w
- 1;
1584 if (track_win_w
< 8)
1588 track_win_x
= tree_win_w
+ 1;
1592 window_set_nr_rows(lib_tree_win
, h
);
1593 window_set_nr_rows(lib_track_win
, h
);
1596 static void update(void)
1598 int needs_view_update
= 0;
1599 int needs_title_update
= 0;
1600 int needs_status_update
= 0;
1601 int needs_command_update
= 0;
1602 int needs_spawn
= 0;
1604 if (needs_to_resize
) {
1608 if (get_window_size(&lines
, &columns
) == 0) {
1609 needs_to_resize
= 0;
1610 resizeterm(lines
, columns
);
1618 resize_tree_view(w
, h
);
1619 window_set_nr_rows(lib_editable
.win
, h
- 1);
1620 window_set_nr_rows(pl_editable
.win
, h
- 1);
1621 window_set_nr_rows(pq_editable
.win
, h
- 1);
1622 window_set_nr_rows(filters_win
, h
- 1);
1623 window_set_nr_rows(help_win
, h
- 1);
1624 window_set_nr_rows(browser_win
, h
- 1);
1626 needs_title_update
= 1;
1627 needs_status_update
= 1;
1628 needs_command_update
= 1;
1635 needs_spawn
= player_info
.status_changed
|| player_info
.file_changed
||
1636 player_info
.metadata_changed
;
1638 if (player_info
.file_changed
) {
1639 player_info
.file_changed
= 0;
1640 needs_title_update
= 1;
1641 needs_status_update
= 1;
1643 if (player_info
.metadata_changed
) {
1644 player_info
.metadata_changed
= 0;
1645 needs_title_update
= 1;
1647 if (player_info
.position_changed
|| player_info
.status_changed
|| player_info
.vol_changed
) {
1648 player_info
.position_changed
= 0;
1649 player_info
.status_changed
= 0;
1650 player_info
.vol_changed
= 0;
1652 needs_status_update
= 1;
1656 needs_view_update
+= lib_tree_win
->changed
|| lib_track_win
->changed
;
1659 needs_view_update
+= lib_editable
.win
->changed
;
1662 needs_view_update
+= pl_editable
.win
->changed
;
1665 needs_view_update
+= pq_editable
.win
->changed
;
1668 needs_view_update
+= browser_win
->changed
;
1671 needs_view_update
+= filters_win
->changed
;
1674 needs_view_update
+= help_win
->changed
;
1678 /* total time changed? */
1680 needs_status_update
+= lib_editable
.win
->changed
;
1681 lib_editable
.win
->changed
= 0;
1683 needs_status_update
+= pl_editable
.win
->changed
;
1684 lib_editable
.win
->changed
= 0;
1688 player_info_unlock();
1691 spawn_status_program();
1693 if (needs_view_update
|| needs_title_update
|| needs_status_update
|| needs_command_update
) {
1696 if (needs_view_update
)
1698 if (needs_title_update
)
1699 do_update_titleline();
1700 if (needs_status_update
)
1701 do_update_statusline();
1702 if (needs_command_update
)
1703 do_update_commandline();
1708 static void handle_ch(uchar ch
)
1711 if (input_mode
== NORMAL_MODE
) {
1713 } else if (input_mode
== COMMAND_MODE
) {
1714 command_mode_ch(ch
);
1715 update_commandline();
1716 } else if (input_mode
== SEARCH_MODE
) {
1718 update_commandline();
1722 static void handle_key(int key
)
1725 if (input_mode
== NORMAL_MODE
) {
1726 normal_mode_key(key
);
1727 } else if (input_mode
== COMMAND_MODE
) {
1728 command_mode_key(key
);
1729 update_commandline();
1730 } else if (input_mode
== SEARCH_MODE
) {
1731 search_mode_key(key
);
1732 update_commandline();
1736 static void u_getch(void)
1740 int mask
= (1 << 7);
1744 if (key
== ERR
|| key
== 0)
1752 ch
= (unsigned char)key
;
1753 while (bit
> 0 && ch
& mask
) {
1763 u
= ch
& ((1 << bit
) - 1);
1767 if (key
== ERR
|| key
== 0)
1770 ch
= (unsigned char)key
;
1771 u
= (u
<< 6) | (ch
& 63);
1778 static void main_loop(void)
1782 fd_high
= server_socket
;
1783 while (cmus_running
) {
1791 FD_SET(server_socket
, &set
);
1793 /* Timeout must be so small that screen updates seem instant.
1794 * Only affects changes done in other threads (worker, player).
1796 * Too small timeout makes window updates too fast (wastes CPU).
1798 * Too large timeout makes status line (position) updates too slow.
1799 * The timeout is accuracy of player position.
1803 rc
= select(fd_high
+ 1, &set
, NULL
, NULL
, &tv
);
1805 if (ctrl_c_pressed
) {
1812 if (FD_ISSET(server_socket
, &set
))
1814 if (FD_ISSET(0, &set
)) {
1820 if (key
!= ERR
&& key
!= 0) {
1827 ch
|= U_INVALID_MASK
;
1836 static int get_next(struct track_info
**ti
)
1838 struct track_info
*info
;
1841 info
= play_queue_remove();
1844 info
= lib_set_next();
1846 info
= pl_set_next();
1858 static const struct player_callbacks player_callbacks
= {
1859 .get_next
= get_next
1862 static void init_curses(void)
1864 struct sigaction act
;
1867 sigemptyset(&act
.sa_mask
);
1869 act
.sa_handler
= sig_int
;
1870 sigaction(SIGINT
, &act
, NULL
);
1872 sigemptyset(&act
.sa_mask
);
1874 act
.sa_handler
= sig_winch
;
1875 sigaction(SIGWINCH
, &act
, NULL
);
1879 /* turn off kb buffering */
1882 keypad(stdscr
, TRUE
);
1884 /* wait max 5 * 0.1 s if there are no keys available
1885 * doesn't really matter because we use select()
1892 use_default_colors();
1894 d_print("Number of supported colors: %d\n", COLORS
);
1897 /* this was disabled while initializing because it needs to be
1898 * called only once after all colors have been set
1903 t_ts
= tgetstr("ts", &ptr
);
1904 t_fs
= tgetstr("fs", &ptr
);
1905 d_print("ts: %d fs: %d\n", !!t_ts
, !!t_fs
);
1910 if (!t_ts
&& (term
= getenv("TERM"))) {
1915 * terminal (xfce): xterm
1916 * urxvt: rxvt-unicode
1917 * xterm: xterm, xterm-{,16,88,256}color
1919 if (!strcmp(term
, "screen")) {
1922 } else if (!strncmp(term
, "xterm", 5) ||
1923 !strncmp(term
, "rxvt", 4) ||
1924 !strcmp(term
, "Eterm")) {
1925 /* \033]1; change icon
1926 * \033]2; change title
1927 * \033]0; change both
1935 static void init_all(void)
1937 server_init(server_address
);
1939 /* does not select output plugin */
1940 player_init(&player_callbacks
);
1942 /* plugins have been loaded so we know what plugin options are available */
1946 searchable
= tree_searchable
;
1956 /* almost everything must be initialized now */
1959 /* finally we can set the output plugin */
1960 player_set_op(output_plugin
);
1962 player_get_volume(&player_info
.vol_left
, &player_info
.vol_right
);
1964 lib_autosave_filename
= xstrjoin(cmus_config_dir
, "/lib.pl");
1965 pl_autosave_filename
= xstrjoin(cmus_config_dir
, "/playlist.pl");
1966 pl_filename
= xstrdup(pl_autosave_filename
);
1967 lib_filename
= xstrdup(lib_autosave_filename
);
1969 cmus_add(lib_add_track
, lib_autosave_filename
, FILE_TYPE_PL
, JOB_TYPE_LIB
);
1970 cmus_add(pl_add_track
, pl_autosave_filename
, FILE_TYPE_PL
, JOB_TYPE_PL
);
1975 warn("Press <enter> to continue.");
1976 fgets(buf
, sizeof(buf
), stdin
);
1978 help_add_all_unbound();
1983 static void exit_all(void)
1991 cmus_save(lib_for_each
, lib_autosave_filename
);
1992 cmus_save(pl_for_each
, pl_autosave_filename
);
2010 static struct option options
[NR_FLAGS
+ 1] = {
2012 { 0, "plugins", 0 },
2014 { 0, "version", 0 },
2018 static const char *usage
=
2019 "Usage: %s [OPTION]...\n"
2020 "Curses based music player.\n"
2022 " --listen ADDR listen on ADDR instead of ~/.cmus/socket\n"
2023 " ADDR is either a UNIX socket or host[:port]\n"
2024 " --plugins list available plugins and exit\n"
2025 " --help display this help and exit\n"
2026 " --version " VERSION
"\n"
2028 "Use cmus-remote to control cmus from command line.\n"
2029 "Report bugs to <cmus-devel@lists.sourceforge.net>.\n";
2031 int main(int argc
, char *argv
[])
2033 int list_plugins
= 0;
2035 program_name
= argv
[0];
2041 idx
= get_option(&argv
, options
, &arg
);
2047 printf(usage
, program_name
);
2050 printf("cmus " VERSION
"\nCopyright 2004-2006 Timo Hirvonen\n");
2056 server_address
= xstrdup(arg
);
2061 setlocale(LC_CTYPE
, "");
2063 charset
= nl_langinfo(CODESET
);
2065 charset
= "ISO-8859-1";
2067 if (strcmp(charset
, "UTF-8") == 0) {
2073 if (server_address
== NULL
)
2074 server_address
= xstrjoin(cmus_config_dir
, "/socket");
2076 d_print("charset = '%s'\n", charset
);
2078 player_load_plugins();
2080 player_dump_plugins();