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>
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 */
73 int ui_initialized
= 0;
74 enum ui_input_mode input_mode
= NORMAL_MODE
;
75 int cur_view
= TREE_VIEW
;
76 struct searchable
*searchable
;
78 /* display parse errors? (command line) */
79 int display_errors
= 0;
81 char *lib_filename
= NULL
;
82 char *pl_filename
= NULL
;
84 /* ------------------------------------------------------------------------- */
86 /* currently playing file */
87 static struct track_info
*cur_track_info
= NULL
;
89 static int running
= 1;
90 static char *lib_autosave_filename
;
91 static char *pl_autosave_filename
;
93 /* shown error message and time stamp
94 * error is cleared if it is older than 3s and key was pressed
96 static char error_buf
[512];
97 static time_t error_time
= 0;
98 /* info messages are displayed in different color */
99 static int msg_is_error
;
100 static int error_count
= 0;
102 static char *server_address
= NULL
;
104 static char *charset
= NULL
;
105 static char print_buffer
[512];
107 /* destination buffer for utf8_encode and utf8_decode */
108 static char conv_buffer
[512];
110 #define print_buffer_size (sizeof(print_buffer) - 1)
111 static int using_utf8
;
113 static const char *t_ts
;
114 static const char *t_fs
;
116 static int tree_win_x
= 0;
117 static int tree_win_y
= 0;
118 static int tree_win_w
= 0;
120 static int track_win_x
= 0;
121 static int track_win_y
= 0;
122 static int track_win_w
= 0;
134 CURSED_WIN_ACTIVE_CUR
,
135 CURSED_WIN_ACTIVE_SEL
,
136 CURSED_WIN_ACTIVE_SEL_CUR
,
151 static unsigned char cursed_to_bg_idx
[NR_CURSED
] = {
154 COLOR_WIN_INACTIVE_SEL_BG
,
155 COLOR_WIN_INACTIVE_CUR_SEL_BG
,
160 COLOR_WIN_CUR_SEL_BG
,
173 static unsigned char cursed_to_fg_idx
[NR_CURSED
] = {
176 COLOR_WIN_INACTIVE_SEL_FG
,
177 COLOR_WIN_INACTIVE_CUR_SEL_FG
,
182 COLOR_WIN_CUR_SEL_FG
,
195 /* index is CURSED_*, value is fucking color pair */
196 static int pairs
[NR_CURSED
];
212 static struct format_option track_fopts
[NR_TFS
+ 1] = {
242 static struct format_option status_fopts
[NR_SFS
+ 1] = {
258 static void utf8_encode(const char *buffer
)
260 static iconv_t cd
= (iconv_t
)-1;
266 if (cd
== (iconv_t
)-1) {
267 d_print("iconv_open(UTF-8, %s)\n", charset
);
268 cd
= iconv_open("UTF-8", charset
);
269 if (cd
== (iconv_t
)-1) {
270 d_print("iconv_open failed: %s\n", strerror(errno
));
277 os
= sizeof(conv_buffer
) - 1;
278 rc
= iconv(cd
, (void *)&i
, &is
, &o
, &os
);
281 d_print("iconv failed: %s\n", strerror(errno
));
286 static void utf8_decode(const char *buffer
)
288 static iconv_t cd
= (iconv_t
)-1;
294 if (cd
== (iconv_t
)-1) {
295 d_print("iconv_open(%s, UTF-8)\n", charset
);
296 cd
= iconv_open(charset
, "UTF-8");
297 if (cd
== (iconv_t
)-1) {
298 d_print("iconv_open failed: %s\n", strerror(errno
));
305 os
= sizeof(conv_buffer
) - 1;
306 rc
= iconv(cd
, (void *)&i
, &is
, &o
, &os
);
309 d_print("iconv failed: %s\n", strerror(errno
));
314 /* screen updates {{{ */
316 static void dump_print_buffer(int row
, int col
)
319 mvaddstr(row
, col
, print_buffer
);
321 utf8_decode(print_buffer
);
322 mvaddstr(row
, col
, conv_buffer
);
326 /* print @str into @buf
328 * if @str is shorter than @width pad with spaces
329 * if @str is wider than @width truncate and add "..."
331 static int format_str(char *buf
, const char *str
, int width
)
333 int s
= 0, d
= 0, ellipsis_pos
= 0, cut_double_width
= 0;
339 u_get_char(str
, &s
, &u
);
341 memset(buf
+ d
, ' ', width
);
349 if (width
== 4 && w
== 2) {
350 /* can't cut double-width char */
351 ellipsis_pos
= d
+ 1;
352 cut_double_width
= 1;
359 if (cut_double_width
) {
360 /* first half of the double-width char */
368 u_set_char(buf
, &d
, u
);
373 static void sprint(int row
, int col
, const char *str
, int width
)
377 print_buffer
[pos
++] = ' ';
378 pos
+= format_str(print_buffer
+ pos
, str
, width
- 2);
379 print_buffer
[pos
++] = ' ';
380 print_buffer
[pos
] = 0;
381 dump_print_buffer(row
, col
);
384 static void sprint_ascii(int row
, int col
, const char *str
, int len
)
391 print_buffer
[0] = ' ';
393 memcpy(print_buffer
+ 1, str
, len
- 3);
394 print_buffer
[len
- 2] = '.';
395 print_buffer
[len
- 1] = '.';
396 print_buffer
[len
- 0] = '.';
398 memcpy(print_buffer
+ 1, str
, l
);
399 memset(print_buffer
+ 1 + l
, ' ', len
- l
);
401 print_buffer
[len
+ 1] = ' ';
402 print_buffer
[len
+ 2] = 0;
403 mvaddstr(row
, col
, print_buffer
);
406 static void print_tree(struct window
*win
, int row
, struct iter
*iter
)
409 struct artist
*artist
;
412 int current
, selected
, active
, pos
;
414 artist
= iter_to_artist(iter
);
415 album
= iter_to_album(iter
);
419 current
= CUR_ALBUM
== album
;
421 current
= CUR_ARTIST
== artist
;
424 window_get_sel(win
, &sel
);
425 selected
= iters_equal(iter
, &sel
);
426 active
= lib_cur_win
== lib_tree_win
;
427 bkgdset(pairs
[(active
<< 2) | (selected
<< 1) | current
]);
429 if (active
&& selected
) {
435 print_buffer
[pos
++] = ' ';
438 print_buffer
[pos
++] = ' ';
439 print_buffer
[pos
++] = ' ';
442 pos
+= format_str(print_buffer
+ pos
, str
, tree_win_w
- pos
- 1);
443 print_buffer
[pos
++] = ' ';
444 print_buffer
[pos
++] = 0;
445 dump_print_buffer(tree_win_y
+ row
+ 1, tree_win_x
);
448 static inline void fopt_set_str(struct format_option
*fopt
, const char *str
)
450 BUG_ON(fopt
->type
!= FO_STR
);
459 static inline void fopt_set_int(struct format_option
*fopt
, int value
, int empty
)
461 BUG_ON(fopt
->type
!= FO_INT
);
462 fopt
->fo_int
= value
;
466 static inline void fopt_set_time(struct format_option
*fopt
, int value
, int empty
)
468 BUG_ON(fopt
->type
!= FO_TIME
);
469 fopt
->fo_time
= value
;
473 static void fill_track_fopts(struct tree_track
*track
)
475 const char *filename
;
476 const struct track_info
*ti
= tree_track_info(track
);
480 filename
= ti
->filename
;
482 utf8_encode(ti
->filename
);
483 filename
= conv_buffer
;
485 disc
= track
->shuffle_track
.simple_track
.disc
;
486 num
= track
->shuffle_track
.simple_track
.num
;
488 fopt_set_str(&track_fopts
[TF_ARTIST
], track
->album
->artist
->name
);
489 fopt_set_str(&track_fopts
[TF_ALBUM
], track
->album
->name
);
490 fopt_set_int(&track_fopts
[TF_DISC
], disc
, disc
== -1);
491 fopt_set_int(&track_fopts
[TF_TRACK
], num
, num
== -1);
492 fopt_set_str(&track_fopts
[TF_TITLE
], comments_get_val(ti
->comments
, "title"));
493 fopt_set_str(&track_fopts
[TF_YEAR
], comments_get_val(ti
->comments
, "date"));
494 fopt_set_str(&track_fopts
[TF_GENRE
], comments_get_val(ti
->comments
, "genre"));
495 fopt_set_time(&track_fopts
[TF_DURATION
], ti
->duration
, ti
->duration
== -1);
496 fopt_set_str(&track_fopts
[TF_PATHFILE
], filename
);
497 if (is_url(ti
->filename
)) {
498 fopt_set_str(&track_fopts
[TF_FILE
], filename
);
502 f
= strrchr(filename
, '/');
504 fopt_set_str(&track_fopts
[TF_FILE
], f
+ 1);
506 fopt_set_str(&track_fopts
[TF_FILE
], filename
);
511 static void fill_track_fopts_track_info(struct track_info
*info
)
517 filename
= info
->filename
;
519 utf8_encode(info
->filename
);
520 filename
= conv_buffer
;
522 disc
= comments_get_int(info
->comments
, "discnumber");
523 num
= comments_get_int(info
->comments
, "tracknumber");
525 fopt_set_str(&track_fopts
[TF_ARTIST
], comments_get_val(info
->comments
, "artist"));
526 fopt_set_str(&track_fopts
[TF_ALBUM
], comments_get_val(info
->comments
, "album"));
527 fopt_set_int(&track_fopts
[TF_DISC
], disc
, disc
== -1);
528 fopt_set_int(&track_fopts
[TF_TRACK
], num
, num
== -1);
529 fopt_set_str(&track_fopts
[TF_TITLE
], comments_get_val(info
->comments
, "title"));
530 fopt_set_str(&track_fopts
[TF_YEAR
], comments_get_val(info
->comments
, "date"));
531 fopt_set_str(&track_fopts
[TF_GENRE
], comments_get_val(info
->comments
, "genre"));
532 fopt_set_time(&track_fopts
[TF_DURATION
], info
->duration
, info
->duration
== -1);
533 fopt_set_str(&track_fopts
[TF_PATHFILE
], filename
);
534 if (is_url(info
->filename
)) {
535 fopt_set_str(&track_fopts
[TF_FILE
], filename
);
539 f
= strrchr(filename
, '/');
541 fopt_set_str(&track_fopts
[TF_FILE
], f
+ 1);
543 fopt_set_str(&track_fopts
[TF_FILE
], filename
);
548 static void print_track(struct window
*win
, int row
, struct iter
*iter
)
550 struct tree_track
*track
;
552 int current
, selected
, active
;
554 track
= iter_to_tree_track(iter
);
555 current
= lib_cur_track
== track
;
556 window_get_sel(win
, &sel
);
557 selected
= iters_equal(iter
, &sel
);
558 active
= lib_cur_win
== lib_track_win
;
559 bkgdset(pairs
[(active
<< 2) | (selected
<< 1) | current
]);
561 if (active
&& selected
) {
562 cursor_x
= track_win_x
;
566 fill_track_fopts(track
);
568 if (track_info_has_tag(tree_track_info(track
))) {
569 format_print(print_buffer
, track_win_w
, track_win_format
, track_fopts
);
571 format_print(print_buffer
, track_win_w
, track_win_alt_format
, track_fopts
);
573 dump_print_buffer(track_win_y
+ row
+ 1, track_win_x
);
576 /* used by print_editable only */
577 static struct simple_track
*current_track
;
579 static void print_editable(struct window
*win
, int row
, struct iter
*iter
)
581 struct simple_track
*track
;
583 int current
, selected
, active
;
585 track
= iter_to_simple_track(iter
);
586 current
= current_track
== track
;
587 window_get_sel(win
, &sel
);
588 selected
= iters_equal(iter
, &sel
);
596 if (!selected
&& track
->marked
) {
601 bkgdset(pairs
[(active
<< 2) | (selected
<< 1) | current
]);
603 fill_track_fopts_track_info(track
->info
);
605 if (track_info_has_tag(track
->info
)) {
606 format_print(print_buffer
, COLS
, list_win_format
, track_fopts
);
608 format_print(print_buffer
, COLS
, list_win_alt_format
, track_fopts
);
610 dump_print_buffer(row
+ 1, 0);
613 static void print_browser(struct window
*win
, int row
, struct iter
*iter
)
615 struct browser_entry
*e
;
619 e
= iter_to_browser_entry(iter
);
620 window_get_sel(win
, &sel
);
621 selected
= iters_equal(iter
, &sel
);
626 bkgdset(pairs
[(active
<< 2) | (selected
<< 1) | current
]);
628 if (e
->type
== BROWSER_ENTRY_DIR
) {
629 bkgdset(pairs
[CURSED_DIR
]);
631 bkgdset(pairs
[CURSED_WIN
]);
640 /* file name encoding == terminal encoding. no need to convert */
642 sprint(row
+ 1, 0, e
->name
, COLS
);
644 sprint_ascii(row
+ 1, 0, e
->name
, COLS
);
648 static void print_filter(struct window
*win
, int row
, struct iter
*iter
)
651 struct filter_entry
*e
= iter_to_filter_entry(iter
);
657 /* is the filter currently active? */
658 int current
= !!e
->act_stat
;
659 const char stat_chars
[3] = " *!";
660 int ch1
, ch2
, ch3
, pos
;
662 window_get_sel(win
, &sel
);
663 selected
= iters_equal(iter
, &sel
);
664 bkgdset(pairs
[(active
<< 2) | (selected
<< 1) | current
]);
673 if (e
->sel_stat
!= e
->act_stat
) {
677 ch2
= stat_chars
[e
->sel_stat
];
678 snprintf(buf
, sizeof(buf
), "%c%c%c%-15s %s", ch1
, ch2
, ch3
, e
->name
, e
->filter
);
679 pos
= format_str(print_buffer
, buf
, COLS
- 1);
680 print_buffer
[pos
++] = ' ';
681 print_buffer
[pos
] = 0;
682 dump_print_buffer(row
+ 1, 0);
685 static void print_help(struct window
*win
, int row
, struct iter
*iter
)
691 const struct help_entry
*e
= iter_to_help_entry(iter
);
692 const struct cmus_opt
*opt
;
694 window_get_sel(win
, &sel
);
695 selected
= iters_equal(iter
, &sel
);
696 bkgdset(pairs
[selected
<< 1]);
705 snprintf(buf
, sizeof(buf
), " %s", e
->text
);
708 snprintf(buf
, sizeof(buf
), " %-8s %-14s %s",
709 key_context_names
[e
->binding
->ctx
],
710 e
->binding
->key
->name
,
714 snprintf(buf
, sizeof(buf
), " %s", e
->command
->name
);
718 snprintf(buf
, sizeof(buf
), " %-29s ", opt
->name
);
719 opt
->get(opt
->id
, buf
+ strlen(buf
));
722 pos
= format_str(print_buffer
, buf
, COLS
- 1);
723 print_buffer
[pos
++] = ' ';
724 print_buffer
[pos
] = 0;
725 dump_print_buffer(row
+ 1, 0);
728 static void update_window(struct window
*win
, int x
, int y
, int w
, const char *title
,
729 void (*print
)(struct window
*, int, struct iter
*))
737 bkgdset(pairs
[CURSED_WIN_TITLE
]);
738 c
= snprintf(print_buffer
, w
+ 1, " %s", title
);
741 memset(print_buffer
+ c
, ' ', w
- c
+ 1);
743 dump_print_buffer(y
, x
);
744 nr_rows
= window_get_nr_rows(win
);
746 if (window_get_top(win
, &iter
)) {
747 while (i
< nr_rows
) {
748 print(win
, i
, &iter
);
750 if (!window_get_next(win
, &iter
))
756 memset(print_buffer
, ' ', w
);
758 while (i
< nr_rows
) {
759 dump_print_buffer(y
+ i
+ 1, x
);
764 static void update_tree_window(void)
766 update_window(lib_tree_win
, tree_win_x
, tree_win_y
,
767 tree_win_w
, "Artist / Album", print_tree
);
770 static void update_track_window(void)
774 /* it doesn't matter what format options we use because the format
775 * string does not contain any format charaters */
776 format_print(title
, track_win_w
- 2, "Track%=Library", track_fopts
);
777 update_window(lib_track_win
, track_win_x
, track_win_y
,
778 track_win_w
, title
, print_track
);
781 static const char *pretty(const char *path
)
783 static int home_len
= -1;
784 static char buf
[256];
787 home_len
= strlen(home_dir
);
789 if (strncmp(path
, home_dir
, home_len
) || path
[home_len
] != '/')
793 strcpy(buf
+ 1, path
+ home_len
);
797 static const char * const sorted_names
[2] = { "", "sorted by " };
799 static void update_editable_window(struct editable
*e
, const char *title
, const char *filename
)
808 utf8_encode(filename
);
809 filename
= conv_buffer
;
811 snprintf(buf
, sizeof(buf
), "%s %s - %d tracks", title
,
812 pretty(filename
), e
->nr_tracks
);
814 snprintf(buf
, sizeof(buf
), "%s - %d tracks", title
, e
->nr_tracks
);
819 snprintf(buf
+ pos
, sizeof(buf
) - pos
, " (%d marked)", e
->nr_marked
);
822 snprintf(buf
+ pos
, sizeof(buf
) - pos
, " %s%s",
823 sorted_names
[e
->sort_str
[0] != 0], e
->sort_str
);
825 update_window(e
->win
, 0, 0, COLS
, buf
, &print_editable
);
828 static void update_sorted_window(void)
830 current_track
= (struct simple_track
*)lib_cur_track
;
831 update_editable_window(&lib_editable
, "Library", lib_filename
);
834 static void update_pl_window(void)
836 current_track
= pl_cur_track
;
837 update_editable_window(&pl_editable
, "Playlist", pl_filename
);
840 static void update_play_queue_window(void)
842 current_track
= NULL
;
843 update_editable_window(&pq_editable
, "Play Queue", NULL
);
846 static void update_browser_window(void)
853 dirname
= browser_dir
;
855 utf8_encode(browser_dir
);
856 dirname
= conv_buffer
;
858 snprintf(title
, sizeof(title
), "Browser - %s", dirname
);
859 update_window(browser_win
, 0, 0, COLS
, title
, print_browser
);
862 static void update_filters_window(void)
864 update_window(filters_win
, 0, 0, COLS
, "Library Filters", print_filter
);
867 static void update_help_window(void)
869 update_window(help_win
, 0, 0, COLS
, "Settings", print_help
);
872 static void draw_separator(void)
876 bkgdset(pairs
[CURSED_WIN_TITLE
]);
877 mvaddch(0, tree_win_w
, ' ');
878 bkgdset(pairs
[CURSED_SEPARATOR
]);
879 for (row
= 1; row
< LINES
- 3; row
++)
880 mvaddch(row
, tree_win_w
, ACS_VLINE
);
883 static void do_update_view(int full
)
891 if (full
|| lib_tree_win
->changed
)
892 update_tree_window();
893 if (full
|| lib_track_win
->changed
)
894 update_track_window();
900 update_sorted_window();
910 update_play_queue_window();
914 update_browser_window();
917 update_filters_window();
920 update_help_window();
925 static void do_update_statusline(void)
927 static const char *status_strs
[] = { ".", ">", "|" };
928 static const char *cont_strs
[] = { " ", "C" };
929 static const char *repeat_strs
[] = { " ", "R" };
930 static const char *shuffle_strs
[] = { " ", "S" };
931 int buffer_fill
, vol
, vol_left
, vol_right
;
937 fopt_set_time(&status_fopts
[SF_TOTAL
], play_library
? lib_editable
.total_time
:
938 pl_editable
.total_time
, 0);
941 fopt_set_str(&status_fopts
[SF_REPEAT
], repeat_strs
[repeat
]);
942 fopt_set_str(&status_fopts
[SF_SHUFFLE
], shuffle_strs
[shuffle
]);
943 fopt_set_str(&status_fopts
[SF_PLAYLISTMODE
], aaa_mode_names
[aaa_mode
]);
946 duration
= cur_track_info
->duration
;
950 if (volume_max
== 0) {
951 vol_left
= vol_right
= vol
= -1;
953 vol_left
= scale_to_percentage(player_info
.vol_left
, volume_max
);
954 vol_right
= scale_to_percentage(player_info
.vol_right
, volume_max
);
955 vol
= (vol_left
+ vol_right
+ 1) / 2;
957 buffer_fill
= scale_to_percentage(player_info
.buffer_fill
, player_info
.buffer_size
);
959 fopt_set_str(&status_fopts
[SF_STATUS
], status_strs
[player_info
.status
]);
961 if (show_remaining_time
&& duration
!= -1) {
962 fopt_set_time(&status_fopts
[SF_POSITION
], player_info
.pos
- duration
, 0);
964 fopt_set_time(&status_fopts
[SF_POSITION
], player_info
.pos
, 0);
967 fopt_set_time(&status_fopts
[SF_DURATION
], duration
, 0);
968 fopt_set_int(&status_fopts
[SF_VOLUME
], vol
, 0);
969 fopt_set_int(&status_fopts
[SF_LVOLUME
], vol_left
, 0);
970 fopt_set_int(&status_fopts
[SF_RVOLUME
], vol_right
, 0);
971 fopt_set_int(&status_fopts
[SF_BUFFER
], buffer_fill
, 0);
972 fopt_set_str(&status_fopts
[SF_CONTINUE
], cont_strs
[player_cont
]);
974 strcpy(format
, " %s %p ");
976 strcat(format
, "/ %d ");
977 strcat(format
, "- %t ");
978 if (volume_max
!= 0) {
979 if (player_info
.vol_left
!= player_info
.vol_right
) {
980 strcat(format
, "vol: %l,%r ");
982 strcat(format
, "vol: %v ");
985 if (cur_track_info
&& is_url(cur_track_info
->filename
))
986 strcat(format
, "buf: %b ");
987 strcat(format
, "%=");
989 /* artist/album modes work only in lib */
991 /* shuffle overrides sorted mode */
992 strcat(format
, "%L from library");
993 } else if (play_sorted
) {
994 strcat(format
, "%L from sorted library");
996 strcat(format
, "%L from library");
999 strcat(format
, "playlist");
1001 strcat(format
, " | %1C%1R%1S ");
1002 format_print(print_buffer
, COLS
, format
, status_fopts
);
1004 msg
= player_info
.error_msg
;
1005 player_info
.error_msg
= NULL
;
1007 player_info_unlock();
1009 bkgdset(pairs
[CURSED_STATUSLINE
]);
1010 dump_print_buffer(LINES
- 2, 0);
1013 error_msg("%s", msg
);
1018 static void dump_buffer(const char *buffer
)
1023 utf8_decode(buffer
);
1024 addstr(conv_buffer
);
1028 static void do_update_commandline(void)
1037 bkgdset(pairs
[CURSED_ERROR
]);
1039 bkgdset(pairs
[CURSED_INFO
]);
1045 bkgdset(pairs
[CURSED_COMMANDLINE
]);
1046 if (input_mode
== NORMAL_MODE
) {
1053 /* cmdline.line actually pretends to be UTF-8 but all non-ASCII
1054 * characters are invalid UTF-8 so it really is in locale's
1057 * This code should be safe because cmdline.bpos ==
1058 * cmdline.cpos as every non-ASCII character is counted as one
1059 * invalid UTF-8 byte.
1061 * NOTE: This has nothing to do with widths of printed
1062 * characters. I.e. even if there were control characters
1063 * (displayed as <xx>) there would be no problem because bpos
1064 * still equals to cpos, I think.
1066 utf8_encode(cmdline
.line
);
1070 /* COMMAND_MODE or SEARCH_MODE */
1071 w
= u_str_width(str
);
1073 if (input_mode
== SEARCH_MODE
)
1074 ch
= search_direction
== SEARCH_FORWARD
? '/' : '?';
1076 if (w
<= COLS
- 2) {
1078 idx
= u_copy_chars(print_buffer
, str
, &w
);
1079 print_buffer
[idx
] = 0;
1080 dump_buffer(print_buffer
);
1083 /* keep cursor as far right as possible */
1084 int skip
, width
, cw
;
1086 /* cursor pos (width, not chars. doesn't count the ':') */
1087 cw
= u_str_nwidth(str
, cmdline
.cpos
);
1089 skip
= cw
+ 2 - COLS
;
1094 /* skip rest (if any) */
1095 idx
= u_skip_chars(str
, &skip
);
1098 idx
= u_copy_chars(print_buffer
, str
+ idx
, &width
);
1099 while (width
< COLS
) {
1100 /* cursor is at end of the buffer
1101 * print 1, 2 or 3 spaces
1105 * If the last _skipped_ character was double-width we may need
1106 * to print 2 spaces.
1108 * If the last _skipped_ character was invalid UTF-8 we may need
1109 * to print 3 spaces.
1111 print_buffer
[idx
++] = ' ';
1114 print_buffer
[idx
] = 0;
1115 dump_buffer(print_buffer
);
1117 /* print ':' + COLS - 1 chars */
1120 idx
= u_copy_chars(print_buffer
, str
, &width
);
1121 print_buffer
[idx
] = 0;
1122 dump_buffer(print_buffer
);
1127 /* lock player_info! */
1128 static const char *get_stream_title(void)
1130 static char stream_title
[255 * 16 + 1];
1133 ptr
= strstr(player_info
.metadata
, "StreamTitle='");
1139 if (*ptr
== '\'' && *(ptr
+ 1) == ';') {
1140 memcpy(stream_title
, title
, ptr
- title
);
1141 stream_title
[ptr
- title
] = 0;
1142 return stream_title
;
1149 static void set_title(const char *title
)
1151 if (!set_term_title
)
1155 printf("%s%s%s", tgoto(t_ts
, 0, 0), title
, t_fs
);
1160 static void do_update_titleline(void)
1162 bkgdset(pairs
[CURSED_TITLELINE
]);
1164 if (cur_track_info
) {
1165 int i
, use_alt_format
= 0;
1168 fill_track_fopts_track_info(cur_track_info
);
1169 if (is_url(cur_track_info
->filename
)) {
1170 const char *title
= get_stream_title();
1174 fopt_set_str(&track_fopts
[TF_TITLE
], title
);
1176 use_alt_format
= !track_info_has_tag(cur_track_info
);
1179 if (use_alt_format
) {
1180 format_print(print_buffer
, COLS
, current_alt_format
, track_fopts
);
1182 format_print(print_buffer
, COLS
, current_format
, track_fopts
);
1184 dump_print_buffer(LINES
- 3, 0);
1186 /* set window title */
1187 if (use_alt_format
) {
1188 format_print(print_buffer
, sizeof(print_buffer
) - 1,
1189 window_title_alt_format
, track_fopts
);
1191 format_print(print_buffer
, sizeof(print_buffer
) - 1,
1192 window_title_format
, track_fopts
);
1195 /* remove whitespace */
1196 i
= strlen(print_buffer
) - 1;
1197 while (i
> 0 && print_buffer
[i
] == ' ')
1199 print_buffer
[i
+ 1] = 0;
1202 wtitle
= print_buffer
;
1204 utf8_decode(print_buffer
);
1205 wtitle
= conv_buffer
;
1213 set_title("cmus " VERSION
);
1215 player_info_unlock();
1218 static int cmdline_cursor_column(void)
1225 /* see do_update_commandline */
1226 utf8_encode(cmdline
.line
);
1230 /* width of the text in the buffer before cursor */
1231 cw
= u_str_nwidth(str
, cmdline
.cpos
);
1233 if (1 + cw
< COLS
) {
1234 /* whole line is visible */
1238 /* beginning of cmdline is not visible */
1240 /* check if the first visible char in cmdline would be halved
1241 * double-width character (or invalid byte <xx>) which is not possible.
1242 * we need to skip the whole character and move cursor to COLS - 2
1244 skip
= cw
+ 2 - COLS
;
1251 u_skip_chars(str
, &s
);
1253 /* the last skipped char was double-width or <xx> */
1254 return COLS
- 1 - (s
- skip
);
1259 static void post_update(void)
1261 /* refresh makes cursor visible at least for urxvt */
1262 if (input_mode
== COMMAND_MODE
|| input_mode
== SEARCH_MODE
) {
1263 move(LINES
- 1, cmdline_cursor_column());
1267 if (cursor_x
>= 0) {
1268 move(cursor_y
, cursor_x
);
1277 void update_titleline(void)
1280 do_update_titleline();
1284 void update_full(void)
1286 if (!ui_initialized
)
1292 do_update_titleline();
1293 do_update_statusline();
1294 do_update_commandline();
1299 static void update_commandline(void)
1302 do_update_commandline();
1306 void update_statusline(void)
1308 if (!ui_initialized
)
1312 do_update_statusline();
1316 void info_msg(const char *format
, ...)
1320 va_start(ap
, format
);
1321 vsnprintf(error_buf
, sizeof(error_buf
), format
, ap
);
1326 update_commandline();
1329 void error_msg(const char *format
, ...)
1333 if (!display_errors
)
1336 strcpy(error_buf
, "Error: ");
1337 va_start(ap
, format
);
1338 vsnprintf(error_buf
+ 7, sizeof(error_buf
) - 7, format
, ap
);
1344 if (ui_initialized
) {
1345 error_time
= time(NULL
);
1346 update_commandline();
1348 warn("%s\n", error_buf
);
1353 int yes_no_query(const char *format
, ...)
1359 va_start(ap
, format
);
1360 vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
1364 bkgdset(pairs
[CURSED_INFO
]);
1366 /* no need to convert buffer.
1367 * it is always encoded in the right charset (assuming filenames are
1368 * encoded in same charset as LC_CTYPE).
1378 if (ch
== ERR
|| ch
== 0)
1384 update_commandline();
1388 void search_not_found(void)
1390 const char *what
= "Track";
1392 if (search_restricted
) {
1395 what
= "Artist/album";
1403 what
= "File/Directory";
1409 what
= "Binding/command/option";
1421 what
= "File/Directory";
1427 what
= "Binding/command/option";
1431 info_msg("%s not found: %s", what
, search_str
? : "");
1434 void set_view(int view
)
1436 if (view
== cur_view
)
1442 searchable
= tree_searchable
;
1445 searchable
= lib_editable
.searchable
;
1448 searchable
= pl_editable
.searchable
;
1451 searchable
= pq_editable
.searchable
;
1454 searchable
= browser_searchable
;
1457 searchable
= filters_searchable
;
1460 searchable
= help_searchable
;
1461 update_help_window();
1470 void enter_command_mode(void)
1474 input_mode
= COMMAND_MODE
;
1475 update_commandline();
1478 void enter_search_mode(void)
1482 input_mode
= SEARCH_MODE
;
1483 search_direction
= SEARCH_FORWARD
;
1484 update_commandline();
1487 void enter_search_backward_mode(void)
1491 input_mode
= SEARCH_MODE
;
1492 search_direction
= SEARCH_BACKWARD
;
1493 update_commandline();
1501 void update_colors(void)
1505 if (!ui_initialized
)
1508 for (i
= 0; i
< NR_CURSED
; i
++) {
1509 int bg
= colors
[cursed_to_bg_idx
[i
]];
1510 int fg
= colors
[cursed_to_fg_idx
[i
]];
1513 if (fg
>= 8 && fg
<= 15) {
1514 /* fg colors 8..15 are special (0..7 + bold) */
1515 init_pair(pair
, fg
& 7, bg
);
1516 pairs
[i
] = COLOR_PAIR(pair
) | (fg
& BRIGHT
? A_BOLD
: 0);
1518 init_pair(pair
, fg
, bg
);
1519 pairs
[i
] = COLOR_PAIR(pair
);
1524 static void clear_error(void)
1526 time_t t
= time(NULL
);
1528 /* error msg is visible at least 3s */
1529 if (t
- error_time
< 3)
1535 update_commandline();
1539 /* screen updates }}} */
1541 static void spawn_status_program(void)
1543 static const char *status_strs
[] = { "stopped", "playing", "paused" };
1544 const char *stream_title
= NULL
;
1548 if (status_display_program
== NULL
|| status_display_program
[0] == 0)
1552 status
= player_info
.status
;
1554 stream_title
= get_stream_title();
1555 player_info_unlock();
1558 argv
[i
++] = xstrdup(status_display_program
);
1560 argv
[i
++] = xstrdup("status");
1561 argv
[i
++] = xstrdup(status_strs
[status
]);
1562 if (cur_track_info
) {
1563 static const char *keys
[] = {
1564 "artist", "album", "discnumber", "tracknumber", "title", "date", NULL
1568 if (is_url(cur_track_info
->filename
)) {
1569 argv
[i
++] = xstrdup("url");
1570 argv
[i
++] = xstrdup(cur_track_info
->filename
);
1572 argv
[i
++] = xstrdup("title");
1573 argv
[i
++] = xstrdup(stream_title
);
1578 argv
[i
++] = xstrdup("file");
1579 argv
[i
++] = xstrdup(cur_track_info
->filename
);
1580 for (j
= 0; keys
[j
]; j
++) {
1581 const char *key
= keys
[j
];
1584 val
= comments_get_val(cur_track_info
->comments
, key
);
1586 argv
[i
++] = xstrdup(key
);
1587 argv
[i
++] = xstrdup(val
);
1590 snprintf(buf
, sizeof(buf
), "%d", cur_track_info
->duration
);
1591 argv
[i
++] = xstrdup("duration");
1592 argv
[i
++] = xstrdup(buf
);
1596 if (spawn(argv
, &status
) == -1)
1597 error_msg("couldn't run `%s': %s", status_display_program
, strerror(errno
));
1598 for (i
= 0; argv
[i
]; i
++)
1602 static int ctrl_c_pressed
= 0;
1604 static void sig_int(int sig
)
1609 static int needs_to_resize
= 1;
1611 static void sig_winch(int sig
)
1613 needs_to_resize
= 1;
1616 static int get_window_size(int *lines
, int *columns
)
1620 if (ioctl(0, TIOCGWINSZ
, &ws
) == -1)
1622 *columns
= ws
.ws_col
;
1627 static void resize_tree_view(int w
, int h
)
1630 track_win_w
= w
- tree_win_w
- 1;
1633 if (track_win_w
< 8)
1637 track_win_x
= tree_win_w
+ 1;
1641 window_set_nr_rows(lib_tree_win
, h
);
1642 window_set_nr_rows(lib_track_win
, h
);
1645 static void update(void)
1647 int needs_view_update
= 0;
1648 int needs_title_update
= 0;
1649 int needs_status_update
= 0;
1650 int needs_command_update
= 0;
1651 int needs_spawn
= 0;
1653 if (needs_to_resize
) {
1657 if (get_window_size(&lines
, &columns
) == 0) {
1658 needs_to_resize
= 0;
1659 resizeterm(lines
, columns
);
1667 resize_tree_view(w
, h
);
1668 window_set_nr_rows(lib_editable
.win
, h
- 1);
1669 window_set_nr_rows(pl_editable
.win
, h
- 1);
1670 window_set_nr_rows(pq_editable
.win
, h
- 1);
1671 window_set_nr_rows(filters_win
, h
- 1);
1672 window_set_nr_rows(help_win
, h
- 1);
1673 window_set_nr_rows(browser_win
, h
- 1);
1675 needs_title_update
= 1;
1676 needs_status_update
= 1;
1677 needs_command_update
= 1;
1684 needs_spawn
= player_info
.status_changed
|| player_info
.file_changed
||
1685 player_info
.metadata_changed
;
1687 if (player_info
.file_changed
) {
1689 track_info_unref(cur_track_info
);
1690 if (player_info
.filename
[0] == 0) {
1691 cur_track_info
= NULL
;
1693 cur_track_info
= cmus_get_track_info(player_info
.filename
);
1695 player_info
.file_changed
= 0;
1696 needs_title_update
= 1;
1697 needs_status_update
= 1;
1699 if (player_info
.metadata_changed
) {
1700 player_info
.metadata_changed
= 0;
1701 needs_title_update
= 1;
1703 if (player_info
.position_changed
|| player_info
.status_changed
|| player_info
.vol_changed
) {
1704 player_info
.position_changed
= 0;
1705 player_info
.status_changed
= 0;
1706 player_info
.vol_changed
= 0;
1708 needs_status_update
= 1;
1712 needs_view_update
+= lib_tree_win
->changed
|| lib_track_win
->changed
;
1715 needs_view_update
+= lib_editable
.win
->changed
;
1718 needs_view_update
+= pl_editable
.win
->changed
;
1721 needs_view_update
+= pq_editable
.win
->changed
;
1724 needs_view_update
+= browser_win
->changed
;
1727 needs_view_update
+= filters_win
->changed
;
1730 needs_view_update
+= help_win
->changed
;
1734 /* total time changed? */
1736 needs_status_update
+= lib_editable
.win
->changed
;
1737 lib_editable
.win
->changed
= 0;
1739 needs_status_update
+= pl_editable
.win
->changed
;
1740 lib_editable
.win
->changed
= 0;
1744 player_info_unlock();
1747 spawn_status_program();
1749 if (needs_view_update
|| needs_title_update
|| needs_status_update
|| needs_command_update
) {
1752 if (needs_view_update
)
1754 if (needs_title_update
)
1755 do_update_titleline();
1756 if (needs_status_update
)
1757 do_update_statusline();
1758 if (needs_command_update
)
1759 do_update_commandline();
1764 static void handle_ch(uchar ch
)
1767 if (input_mode
== NORMAL_MODE
) {
1769 } else if (input_mode
== COMMAND_MODE
) {
1770 command_mode_ch(ch
);
1771 update_commandline();
1772 } else if (input_mode
== SEARCH_MODE
) {
1774 update_commandline();
1778 static void handle_key(int key
)
1781 if (input_mode
== NORMAL_MODE
) {
1782 normal_mode_key(key
);
1783 } else if (input_mode
== COMMAND_MODE
) {
1784 command_mode_key(key
);
1785 update_commandline();
1786 } else if (input_mode
== SEARCH_MODE
) {
1787 search_mode_key(key
);
1788 update_commandline();
1792 static void u_getch(void)
1796 int mask
= (1 << 7);
1800 if (key
== ERR
|| key
== 0)
1808 ch
= (unsigned char)key
;
1809 while (bit
> 0 && ch
& mask
) {
1819 u
= ch
& ((1 << bit
) - 1);
1823 if (key
== ERR
|| key
== 0)
1826 ch
= (unsigned char)key
;
1827 u
= (u
<< 6) | (ch
& 63);
1834 static void main_loop(void)
1838 fd_high
= server_socket
;
1847 FD_SET(server_socket
, &set
);
1849 /* Timeout must be so small that screen updates seem instant.
1850 * Only affects changes done in other threads (worker, player).
1852 * Too small timeout makes window updates too fast (wastes CPU).
1854 * Too large timeout makes status line (position) updates too slow.
1855 * The timeout is accuracy of player position.
1859 rc
= select(fd_high
+ 1, &set
, NULL
, NULL
, &tv
);
1861 if (ctrl_c_pressed
) {
1868 if (FD_ISSET(server_socket
, &set
)) {
1869 /* no error msgs for cmus-remote */
1874 if (FD_ISSET(0, &set
)) {
1875 /* diplay errors for interactive commands */
1883 if (key
!= ERR
&& key
!= 0) {
1890 ch
|= U_INVALID_MASK
;
1899 static int get_next(char **filename
)
1901 struct track_info
*info
;
1904 info
= play_queue_remove();
1907 info
= lib_set_next();
1909 info
= pl_set_next();
1917 *filename
= xstrdup(info
->filename
);
1918 track_info_unref(info
);
1922 static const struct player_callbacks player_callbacks
= {
1923 .get_next
= get_next
1926 static void init_curses(void)
1928 struct sigaction act
;
1931 sigemptyset(&act
.sa_mask
);
1933 act
.sa_handler
= sig_int
;
1934 sigaction(SIGINT
, &act
, NULL
);
1936 sigemptyset(&act
.sa_mask
);
1938 act
.sa_handler
= sig_winch
;
1939 sigaction(SIGWINCH
, &act
, NULL
);
1943 /* turn off kb buffering */
1946 keypad(stdscr
, TRUE
);
1948 /* wait max 5 * 0.1 s if there are no keys available
1949 * doesn't really matter because we use select()
1956 use_default_colors();
1958 d_print("Number of supported colors: %d\n", COLORS
);
1961 /* this was disabled while initializing because it needs to be
1962 * called only once after all colors have been set
1967 t_ts
= tgetstr("ts", &ptr
);
1968 t_fs
= tgetstr("fs", &ptr
);
1969 d_print("ts: %d fs: %d\n", !!t_ts
, !!t_fs
);
1974 if (!t_ts
&& (term
= getenv("TERM"))) {
1979 * terminal (xfce): xterm
1980 * urxvt: rxvt-unicode
1981 * xterm: xterm, xterm-{,16,88,256}color
1983 if (!strcmp(term
, "screen")) {
1986 } else if (!strncmp(term
, "xterm", 5) ||
1987 !strncmp(term
, "rxvt", 4) ||
1988 !strcmp(term
, "Eterm")) {
1989 /* \033]1; change icon
1990 * \033]2; change title
1991 * \033]0; change both
1999 static void init_all(void)
2001 server_init(server_address
);
2003 /* does not select output plugin */
2004 player_init(&player_callbacks
);
2006 /* plugins have been loaded so we know what plugin options are available */
2010 searchable
= tree_searchable
;
2020 /* almost everything must be initialized now */
2023 /* finally we can set the output plugin */
2024 player_set_op(output_plugin
);
2026 player_get_volume(&player_info
.vol_left
, &player_info
.vol_right
);
2028 lib_autosave_filename
= xstrjoin(cmus_config_dir
, "/lib.pl");
2029 pl_autosave_filename
= xstrjoin(cmus_config_dir
, "/playlist.pl");
2030 pl_filename
= xstrdup(pl_autosave_filename
);
2031 lib_filename
= xstrdup(lib_autosave_filename
);
2033 cmus_add(lib_add_track
, lib_autosave_filename
, FILE_TYPE_PL
, JOB_TYPE_LIB
);
2034 cmus_add(pl_add_track
, pl_autosave_filename
, FILE_TYPE_PL
, JOB_TYPE_PL
);
2039 warn("Press <enter> to continue.");
2040 fgets(buf
, sizeof(buf
), stdin
);
2042 help_add_all_unbound();
2047 static void exit_all(void)
2055 cmus_save(lib_for_each
, lib_autosave_filename
);
2056 cmus_save(pl_for_each
, pl_autosave_filename
);
2074 static struct option options
[NR_FLAGS
+ 1] = {
2076 { 0, "plugins", 0 },
2078 { 0, "version", 0 },
2082 static const char *usage
=
2083 "Usage: %s [OPTION]...\n"
2084 "Curses based music player.\n"
2086 " --listen ADDR listen on ADDR instead of ~/.cmus/socket\n"
2087 " ADDR is either a UNIX socket or host[:port]\n"
2088 " --plugins list available plugins and exit\n"
2089 " --help display this help and exit\n"
2090 " --version " VERSION
"\n"
2092 "Use cmus-remote to control cmus from command line.\n"
2093 "Report bugs to <cmus-devel@lists.sourceforge.net>.\n";
2095 int main(int argc
, char *argv
[])
2097 int list_plugins
= 0;
2099 program_name
= argv
[0];
2105 idx
= get_option(&argv
, options
, &arg
);
2111 printf(usage
, program_name
);
2114 printf("cmus " VERSION
"\nCopyright 2004-2006 Timo Hirvonen\n");
2120 server_address
= xstrdup(arg
);
2125 setlocale(LC_CTYPE
, "");
2127 charset
= nl_langinfo(CODESET
);
2129 charset
= "ISO-8859-1";
2131 if (strcmp(charset
, "UTF-8") == 0) {
2137 if (server_address
== NULL
)
2138 server_address
= xstrjoin(cmus_config_dir
, "/socket");
2140 d_print("charset = '%s'\n", charset
);
2142 player_load_plugins();
2144 player_dump_plugins();