2 * Copyright 2006 Timo Hirvonen
11 #include "ui_curses.h"
12 #include "format_print.h"
20 #include "command_mode.h"
24 #include "config/datadir.h"
35 /* initialized option variables */
37 char *output_plugin
= NULL
;
38 char *status_display_program
= NULL
;
39 char *server_password
;
40 int auto_reshuffle
= 0;
43 int show_remaining_time
= 0;
44 int set_term_title
= 1;
49 int colors
[NR_COLORS
] = {
53 COLOR_YELLOW
| BRIGHT
,
62 COLOR_YELLOW
| BRIGHT
,
65 COLOR_YELLOW
| BRIGHT
,
70 COLOR_YELLOW
| BRIGHT
,
80 /* uninitialized option variables */
81 char *track_win_format
= NULL
;
82 char *track_win_alt_format
= NULL
;
83 char *list_win_format
= NULL
;
84 char *list_win_alt_format
= NULL
;
85 char *current_format
= NULL
;
86 char *current_alt_format
= NULL
;
87 char *window_title_format
= NULL
;
88 char *window_title_alt_format
= NULL
;
89 char *id3_default_charset
= NULL
;
91 static void buf_int(char *buf
, int val
)
93 snprintf(buf
, OPTION_MAX_SIZE
, "%d", val
);
96 static int parse_int(const char *buf
, int minval
, int maxval
, int *val
)
100 if (str_to_int(buf
, &tmp
) == -1 || tmp
< minval
|| tmp
> maxval
) {
101 error_msg("integer in range %d..%d expected", minval
, maxval
);
108 int parse_enum(const char *buf
, int minval
, int maxval
, const char * const names
[], int *val
)
113 if (str_to_int(buf
, &tmp
) == 0) {
114 if (tmp
< minval
|| tmp
> maxval
)
120 for (i
= 0; names
[i
]; i
++) {
121 if (strcasecmp(buf
, names
[i
]) == 0) {
127 error_msg("name or integer in range %d..%d expected", minval
, maxval
);
131 static const char * const bool_names
[] = {
132 "false", "true", NULL
135 static int parse_bool(const char *buf
, int *val
)
137 return parse_enum(buf
, 0, 1, bool_names
, val
);
140 /* this is used as id in struct cmus_opt */
153 /* callbacks for normal options {{{ */
155 #define SECOND_SIZE (44100 * 16 / 8 * 2)
156 static void get_buffer_seconds(unsigned int id
, char *buf
)
158 buf_int(buf
, (player_get_buffer_chunks() * CHUNK_SIZE
+ SECOND_SIZE
/ 2) / SECOND_SIZE
);
161 static void set_buffer_seconds(unsigned int id
, const char *buf
)
165 if (parse_int(buf
, 1, 20, &sec
))
166 player_set_buffer_chunks((sec
* SECOND_SIZE
+ CHUNK_SIZE
/ 2) / CHUNK_SIZE
);
169 static void get_id3_default_charset(unsigned int id
, char *buf
)
171 strcpy(buf
, id3_default_charset
);
174 static void set_id3_default_charset(unsigned int id
, const char *buf
)
176 free(id3_default_charset
);
177 id3_default_charset
= xstrdup(buf
);
180 static const char * const valid_sort_keys
[] = {
192 static const char **parse_sort_keys(const char *value
)
200 keys
= xnew(const char *, size
);
211 while (*e
&& *e
!= ' ')
225 if (valid_sort_keys
[i
] == NULL
) {
226 error_msg("invalid sort key '%s'", buf
);
231 if (strcmp(buf
, valid_sort_keys
[i
]) == 0)
235 if (pos
== size
- 1) {
237 keys
= xrenew(const char *, keys
, size
);
239 keys
[pos
++] = valid_sort_keys
[i
];
245 static void get_lib_sort(unsigned int id
, char *buf
)
247 strcpy(buf
, lib_editable
.sort_str
);
250 static void set_lib_sort(unsigned int id
, const char *buf
)
252 const char **keys
= parse_sort_keys(buf
);
255 editable_set_sort_keys(&lib_editable
, keys
);
258 static void get_pl_sort(unsigned int id
, char *buf
)
260 strcpy(buf
, pl_editable
.sort_str
);
263 static void set_pl_sort(unsigned int id
, const char *buf
)
265 const char **keys
= parse_sort_keys(buf
);
268 editable_set_sort_keys(&pl_editable
, keys
);
271 static void get_output_plugin(unsigned int id
, char *buf
)
273 char *value
= player_get_op();
280 static void set_output_plugin(unsigned int id
, const char *buf
)
282 if (ui_initialized
) {
285 /* must set it later manually */
286 output_plugin
= xstrdup(buf
);
290 static void get_passwd(unsigned int id
, char *buf
)
293 strcpy(buf
, server_password
);
296 static void set_passwd(unsigned int id
, const char *buf
)
298 int len
= strlen(buf
);
301 free(server_password
);
302 server_password
= NULL
;
303 } else if (len
< 6) {
304 error_msg("unsafe password");
306 free(server_password
);
307 server_password
= xstrdup(buf
);
311 static void get_softvol_state(unsigned int id
, char *buf
)
313 sprintf(buf
, "%d %d", soft_vol_l
, soft_vol_r
);
316 static void set_softvol_state(unsigned int id
, const char *buf
)
318 char buffer
[OPTION_MAX_SIZE
];
323 ptr
= strchr(buffer
, ' ');
329 if (str_to_int(buffer
, &l
) == -1 || l
< 0 || l
> 100)
331 if (str_to_int(ptr
, &r
) == -1 || r
< 0 || r
> 100)
335 /* avoid race condition */
336 player_set_volume(l
, r
);
338 /* can't use player_set_volume(), no race condition */
344 error_msg("two integers in range 0..100 expected");
347 static void get_status_display_program(unsigned int id
, char *buf
)
349 if (status_display_program
)
350 strcpy(buf
, status_display_program
);
353 static void set_status_display_program(unsigned int id
, const char *buf
)
355 free(status_display_program
);
356 status_display_program
= NULL
;
358 status_display_program
= xstrdup(buf
);
363 /* callbacks for toggle options {{{ */
365 static void get_auto_reshuffle(unsigned int id
, char *buf
)
367 strcpy(buf
, bool_names
[auto_reshuffle
]);
370 static void set_auto_reshuffle(unsigned int id
, const char *buf
)
372 parse_bool(buf
, &auto_reshuffle
);
375 static void toggle_auto_reshuffle(unsigned int id
)
380 static void get_continue(unsigned int id
, char *buf
)
382 strcpy(buf
, bool_names
[player_cont
]);
385 static void set_continue(unsigned int id
, const char *buf
)
387 if (!parse_bool(buf
, &player_cont
))
392 static void toggle_continue(unsigned int id
)
398 static void get_confirm_run(unsigned int id
, char *buf
)
400 strcpy(buf
, bool_names
[confirm_run
]);
403 static void set_confirm_run(unsigned int id
, const char *buf
)
405 parse_bool(buf
, &confirm_run
);
408 static void toggle_confirm_run(unsigned int id
)
413 const char * const view_names
[NR_VIEWS
+ 1] = {
414 "tree", "sorted", "playlist", "queue", "browser", "filters", "settings", NULL
417 static void get_play_library(unsigned int id
, char *buf
)
419 strcpy(buf
, bool_names
[play_library
]);
422 static void set_play_library(unsigned int id
, const char *buf
)
424 if (!parse_bool(buf
, &play_library
))
429 static void toggle_play_library(unsigned int id
)
435 static void get_play_sorted(unsigned int id
, char *buf
)
437 strcpy(buf
, bool_names
[play_sorted
]);
440 static void set_play_sorted(unsigned int id
, const char *buf
)
444 if (!parse_bool(buf
, &tmp
))
451 static void toggle_play_sorted(unsigned int id
)
454 play_sorted
= play_sorted
^ 1;
456 /* shuffle would override play_sorted... */
458 /* play_sorted makes no sense in playlist */
467 const char * const aaa_mode_names
[] = {
468 "all", "artist", "album", NULL
471 static void get_aaa_mode(unsigned int id
, char *buf
)
473 strcpy(buf
, aaa_mode_names
[aaa_mode
]);
476 static void set_aaa_mode(unsigned int id
, const char *buf
)
480 if (!parse_enum(buf
, 0, 2, aaa_mode_names
, &tmp
))
487 static void toggle_aaa_mode(unsigned int id
)
491 /* aaa mode makes no sense in playlist */
500 static void get_repeat(unsigned int id
, char *buf
)
502 strcpy(buf
, bool_names
[repeat
]);
505 static void set_repeat(unsigned int id
, const char *buf
)
507 if (!parse_bool(buf
, &repeat
))
512 static void toggle_repeat(unsigned int id
)
518 static void get_show_hidden(unsigned int id
, char *buf
)
520 strcpy(buf
, bool_names
[show_hidden
]);
523 static void set_show_hidden(unsigned int id
, const char *buf
)
525 if (!parse_bool(buf
, &show_hidden
))
530 static void toggle_show_hidden(unsigned int id
)
536 static void get_show_remaining_time(unsigned int id
, char *buf
)
538 strcpy(buf
, bool_names
[show_remaining_time
]);
541 static void set_show_remaining_time(unsigned int id
, const char *buf
)
543 if (!parse_bool(buf
, &show_remaining_time
))
548 static void toggle_show_remaining_time(unsigned int id
)
550 show_remaining_time
^= 1;
554 static void get_set_term_title(unsigned int id
, char *buf
)
556 strcpy(buf
, bool_names
[set_term_title
]);
559 static void set_set_term_title(unsigned int id
, const char *buf
)
561 parse_bool(buf
, &set_term_title
);
564 static void toggle_set_term_title(unsigned int id
)
569 static void get_shuffle(unsigned int id
, char *buf
)
571 strcpy(buf
, bool_names
[shuffle
]);
574 static void set_shuffle(unsigned int id
, const char *buf
)
576 if (!parse_bool(buf
, &shuffle
))
581 static void toggle_shuffle(unsigned int id
)
587 static void get_softvol(unsigned int id
, char *buf
)
589 strcpy(buf
, bool_names
[soft_vol
]);
592 static void set_softvol(unsigned int id
, const char *buf
)
596 if (!parse_bool(buf
, &soft
))
598 player_set_soft_vol(soft
);
601 static void toggle_softvol(unsigned int id
)
603 player_set_soft_vol(soft_vol
^ 1);
608 /* special callbacks (id set) {{{ */
610 static const char * const color_enum_names
[1 + 8 * 2 + 1] = {
612 "black", "red", "green", "yellow", "blue", "magenta", "cyan", "gray",
613 "darkgray", "lightred", "lightgreen", "lightyellow", "lightblue", "lightmagenta", "lightcyan", "white",
617 static void get_color(unsigned int id
, char *buf
)
623 strcpy(buf
, color_enum_names
[val
+ 1]);
629 static void set_color(unsigned int id
, const char *buf
)
633 if (!parse_enum(buf
, -1, 255, color_enum_names
, &color
))
641 static char **id_to_fmt(enum format_id id
)
644 case FMT_CURRENT_ALT
:
645 return ¤t_alt_format
;
646 case FMT_PLAYLIST_ALT
:
647 return &list_win_alt_format
;
649 return &window_title_alt_format
;
650 case FMT_TRACKWIN_ALT
:
651 return &track_win_alt_format
;
653 return ¤t_format
;
655 return &list_win_format
;
657 return &window_title_format
;
659 return &track_win_format
;
664 static void get_format(unsigned int id
, char *buf
)
666 char **fmtp
= id_to_fmt(id
);
671 static void set_format(unsigned int id
, const char *buf
)
673 char **fmtp
= id_to_fmt(id
);
675 if (!format_valid(buf
)) {
676 error_msg("invalid format string");
680 *fmtp
= xstrdup(buf
);
687 #define DN(name) { #name, get_ ## name, set_ ## name, NULL },
688 #define DT(name) { #name, get_ ## name, set_ ## name, toggle_ ## name },
690 static const struct {
694 opt_toggle_cb toggle
;
695 } simple_options
[] = {
701 DN(id3_default_charset
)
710 DT(show_remaining_time
)
715 DN(status_display_program
)
716 { NULL
, NULL
, NULL
, NULL
}
719 static const char * const color_names
[NR_COLORS
] = {
725 "color_statusline_bg",
726 "color_statusline_fg",
727 "color_titleline_bg",
728 "color_titleline_fg",
731 "color_win_cur_sel_bg",
732 "color_win_cur_sel_fg",
735 "color_win_inactive_cur_sel_bg",
736 "color_win_inactive_cur_sel_fg",
737 "color_win_inactive_sel_bg",
738 "color_win_inactive_sel_fg",
741 "color_win_title_bg",
745 /* default values for the variables which we must initialize but
746 * can't do it statically */
747 static const struct {
751 { "altformat_current", " %F " },
752 { "altformat_playlist", " %f%= %d " },
753 { "altformat_title", "%f" },
754 { "altformat_trackwin", " %f%= %d " },
755 { "format_current", " %a - %l - %02n. %t%= %y " },
756 { "format_playlist", " %-20a %02n. %t%= %y %d " },
757 { "format_title", "%a - %l - %t (%y)" },
758 { "format_trackwin", " %02n. %t%= %y %d " },
760 { "lib_sort" , "artist album discnumber tracknumber title filename" },
762 { "id3_default_charset","ISO-8859-1" },
766 LIST_HEAD(option_head
);
769 void option_add(const char *name
, unsigned int id
, opt_get_cb get
,
770 opt_set_cb set
, opt_toggle_cb toggle
)
772 struct cmus_opt
*opt
= xnew(struct cmus_opt
, 1);
773 struct list_head
*item
;
779 opt
->toggle
= toggle
;
781 item
= option_head
.next
;
782 while (item
!= &option_head
) {
783 struct cmus_opt
*o
= container_of(item
, struct cmus_opt
, node
);
785 if (strcmp(name
, o
->name
) < 0)
789 /* add before item */
790 list_add_tail(&opt
->node
, item
);
794 struct cmus_opt
*option_find(const char *name
)
796 struct cmus_opt
*opt
;
798 list_for_each_entry(opt
, &option_head
, node
) {
799 if (strcmp(name
, opt
->name
) == 0)
802 error_msg("no such option %s", name
);
806 void option_set(const char *name
, const char *value
)
808 struct cmus_opt
*opt
= option_find(name
);
811 opt
->set(opt
->id
, value
);
814 static void get_op_option(unsigned int id
, char *buf
)
818 player_get_op_option(id
, &val
);
825 static void set_op_option(unsigned int id
, const char *buf
)
827 int rc
= player_set_op_option(id
, buf
);
830 char *msg
= op_get_error_msg(rc
, "setting option");
831 error_msg("%s", msg
);
836 /* id is ((plugin_index << 16) | option_index) */
837 static void add_op_option(unsigned int id
, const char *name
)
839 option_add(xstrdup(name
), id
, get_op_option
, set_op_option
, NULL
);
842 void options_add(void)
848 for (i
= 0; simple_options
[i
].name
; i
++)
849 option_add(simple_options
[i
].name
, 0, simple_options
[i
].get
,
850 simple_options
[i
].set
, simple_options
[i
].toggle
);
852 for (i
= 0; i
< NR_FMTS
; i
++)
853 option_add(str_defaults
[i
].name
, i
, get_format
, set_format
, NULL
);
855 for (i
= 0; i
< NR_COLORS
; i
++)
856 option_add(color_names
[i
], i
, get_color
, set_color
, NULL
);
858 player_for_each_op_option(add_op_option
);
861 static int handle_line(void *data
, const char *line
)
867 int source_file(const char *filename
)
869 return file_for_each_line(filename
, handle_line
, NULL
);
872 void options_load(void)
879 /* initialize those that can't be statically initialized */
880 for (i
= 0; str_defaults
[i
].name
; i
++)
881 option_set(str_defaults
[i
].name
, str_defaults
[i
].value
);
883 /* load autosave config */
884 snprintf(filename
, sizeof(filename
), "%s/autosave", cmus_config_dir
);
885 if (source_file(filename
) == -1) {
886 const char *def
= DATADIR
"/cmus/rc";
889 warn_errno("loading %s", filename
);
892 if (source_file(def
) == -1)
893 die_errno("loading %s", def
);
896 /* load optional static config */
897 snprintf(filename
, sizeof(filename
), "%s/rc", cmus_config_dir
);
898 if (source_file(filename
) == -1) {
900 warn_errno("loading %s", filename
);
904 void options_exit(void)
906 struct cmus_opt
*opt
;
907 struct filter_entry
*filt
;
912 snprintf(filename
, sizeof(filename
), "%s/autosave", cmus_config_dir
);
913 f
= fopen(filename
, "w");
915 warn_errno("creating %s", filename
);
920 list_for_each_entry(opt
, &option_head
, node
) {
921 char buf
[OPTION_MAX_SIZE
];
924 opt
->get(opt
->id
, buf
);
925 fprintf(f
, "set %s=%s\n", opt
->name
, buf
);
928 /* save key bindings */
929 for (i
= 0; i
< NR_CTXS
; i
++) {
930 struct binding
*b
= key_bindings
[i
];
933 fprintf(f
, "bind %s %s %s\n", key_context_names
[i
], b
->key
->name
, b
->cmd
);
939 list_for_each_entry(filt
, &filters_head
, node
)
940 fprintf(f
, "fset %s=%s\n", filt
->name
, filt
->filter
);
941 fprintf(f
, "factivate");
942 list_for_each_entry(filt
, &filters_head
, node
) {
943 switch (filt
->act_stat
) {
945 fprintf(f
, " %s", filt
->name
);
948 fprintf(f
, " !%s", filt
->name
);