1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "conf.h"
21 #include "config.h"
22 #include "defaults.h"
23 #include "i18n.h"
24 #include "command.h"
25 #include "colors.h"
26 #include "screen_list.h"
28 #include <assert.h>
29 #include <ctype.h>
30 #include <stdio.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <glib.h>
37 #include <glib/gstdio.h>
39 #define MAX_LINE_LENGTH 1024
40 #define COMMENT_TOKEN '#'
42 /* configuration field names */
43 #define CONF_ENABLE_COLORS "enable-colors"
44 #define CONF_SCROLL_OFFSET "scroll-offset"
45 #define CONF_AUTO_CENTER "auto-center"
46 #define CONF_WIDE_CURSOR "wide-cursor"
47 #define CONF_KEY_DEFINITION "key"
48 #define CONF_COLOR "color"
49 #define CONF_COLOR_DEFINITION "colordef"
50 #define CONF_LIST_FORMAT "list-format"
51 #define CONF_STATUS_FORMAT "status-format"
52 #define CONF_XTERM_TITLE_FORMAT "xterm-title-format"
53 #define CONF_LIST_WRAP "wrap-around"
54 #define CONF_FIND_WRAP "find-wrap"
55 #define CONF_FIND_SHOW_LAST "find-show-last"
56 #define CONF_AUDIBLE_BELL "audible-bell"
57 #define CONF_VISIBLE_BELL "visible-bell"
58 #define CONF_BELL_ON_WRAP "bell-on-wrap"
59 #define CONF_STATUS_MESSAGE_TIME "status-message-time"
60 #define CONF_XTERM_TITLE "set-xterm-title"
61 #define CONF_ENABLE_MOUSE "enable-mouse"
62 #define CONF_CROSSFADE_TIME "crossfade-time"
63 #define CONF_SEARCH_MODE "search-mode"
64 #define CONF_HIDE_CURSOR "hide-cursor"
65 #define CONF_SEEK_TIME "seek-time"
66 #define CONF_SCREEN_LIST "screen-list"
67 #define CONF_TIMEDISPLAY_TYPE "timedisplay-type"
68 #define CONF_HOST "host"
69 #define CONF_PORT "port"
70 #define CONF_PASSWORD "password"
71 #define CONF_TIMEOUT "timeout"
72 #define CONF_LYRICS_TIMEOUT "lyrics-timeout"
73 #define CONF_SCROLL "scroll"
74 #define CONF_SCROLL_SEP "scroll-sep"
75 #define CONF_VISIBLE_BITRATE "visible-bitrate"
76 #define CONF_HARDWARE_CURSOR "hardware-cursor"
77 #define CONF_WELCOME_SCREEN_LIST "welcome-screen-list"
78 #define CONF_DISPLAY_TIME "display-time"
79 #define CONF_JUMP_PREFIX_ONLY "jump-prefix-only"
80 #define CONF_LYRICS_AUTOSAVE "lyrics-autosave"
81 #define CONF_LYRICS_SHOW_PLUGIN "lyrics-show-plugin"
82 #define CONF_TEXT_EDITOR "text-editor"
83 #define CONF_TEXT_EDITOR_ASK "text-editor-ask"
84 #define CONF_CHAT_PREFIX "chat-prefix"
85 #define CONF_SECOND_COLUMN "second-column"
87 static bool
88 str2bool(char *str)
89 {
90 return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
91 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
92 }
94 static void
95 print_error(const char *msg, const char *input)
96 {
97 fprintf(stderr, "%s: %s ('%s')\n",
98 /* To translators: prefix for error messages */
99 _("Error"), msg, input);
100 }
102 static int
103 parse_key_value(char *str, char **end)
104 {
105 if (*str == '\'') {
106 if (str[1] == '\'' || str[2] != '\'') {
107 print_error(_("Malformed hotkey definition"), str);
108 return -1;
109 }
111 *end = str + 3;
112 return str[1];
113 } else {
114 long value = strtol(str, end, 0);
115 if (*end == str) {
116 print_error(_("Malformed hotkey definition"), str);
117 return -1;
118 }
120 return (int)value;
121 }
122 }
124 static int
125 parse_key_definition(char *str)
126 {
127 char buf[MAX_LINE_LENGTH];
128 char *p;
129 size_t len = strlen(str), i;
130 int j,key;
131 int keys[MAX_COMMAND_KEYS];
132 command_t cmd;
134 /* get the command name */
135 i=0;
136 j=0;
137 memset(buf, 0, MAX_LINE_LENGTH);
138 while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
139 buf[j++] = str[i++];
140 if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
141 /* the hotkey configuration contains an unknown
142 command */
143 print_error(_("Unknown command"), buf);
144 return -1;
145 }
147 /* skip whitespace */
148 while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
149 i++;
151 /* get the value part */
152 memset(buf, 0, MAX_LINE_LENGTH);
153 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
154 if (*buf == 0) {
155 /* the hotkey configuration line is incomplete */
156 print_error(_("Incomplete hotkey configuration"), str);
157 return -1;
158 }
160 /* parse key values */
161 i = 0;
162 key = 0;
163 p = buf;
164 memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
165 while (i < MAX_COMMAND_KEYS && *p != 0 &&
166 (key = parse_key_value(p, &p)) >= 0) {
167 keys[i++] = key;
168 while (*p==',' || *p==' ' || *p=='\t')
169 p++;
170 }
172 if (key < 0)
173 return -1;
175 return assign_keys(cmd, keys);
176 }
178 static bool
179 parse_timedisplay_type(const char *str)
180 {
181 if (strcmp(str, "elapsed") == 0)
182 return false;
183 else if (strcmp(str, "remaining") == 0)
184 return true;
185 else {
186 /* translators: ncmpc supports displaying the
187 "elapsed" or "remaining" time of a song being
188 played; in this case, the configuration file
189 contained an invalid setting */
190 print_error(_("Bad time display type"), str);
191 return false;
192 }
193 }
195 #ifdef ENABLE_COLORS
196 static char *
197 separate_value(char *p)
198 {
199 char *value;
201 value = strchr(p, '=');
202 if (value == NULL) {
203 /* an equals sign '=' was expected while parsing a
204 configuration file line */
205 fprintf(stderr, "%s\n", _("Missing '='"));
206 return NULL;
207 }
209 *value++ = 0;
211 g_strchomp(p);
212 return g_strchug(value);
213 }
215 static int
216 parse_color(char *str)
217 {
218 char *value;
220 value = separate_value(str);
221 if (value == NULL)
222 return -1;
224 return colors_assign(str, value);
225 }
227 /**
228 * Returns the first non-whitespace character after the next comma
229 * character, or the end of the string. This is used to parse comma
230 * separated values.
231 */
232 static char *
233 after_comma(char *p)
234 {
235 char *comma = strchr(p, ',');
237 if (comma != NULL) {
238 *comma++ = 0;
239 comma = g_strchug(comma);
240 } else
241 comma = p + strlen(p);
243 g_strchomp(p);
244 return comma;
245 }
247 static int
248 parse_color_definition(char *str)
249 {
250 char buf[MAX_LINE_LENGTH];
251 char *value;
252 short color, rgb[3];
254 value = separate_value(str);
255 if (value == NULL)
256 return -1;
258 /* get the command name */
259 color = colors_str2color(str);
260 if (color < 0) {
261 print_error(_("Bad color name"), buf);
262 return -1;
263 }
265 /* parse r,g,b values */
267 for (unsigned i = 0; i < 3; ++i) {
268 char *next = after_comma(value), *endptr;
269 if (*value == 0) {
270 print_error(_("Incomplete color definition"), str);
271 return -1;
272 }
274 rgb[i] = strtol(value, &endptr, 0);
275 if (endptr == value || *endptr != 0) {
276 print_error(_("Invalid number"), value);
277 return -1;
278 }
280 value = next;
281 }
283 if (*value != 0) {
284 print_error(_("Malformed color definition"), str);
285 return -1;
286 }
288 return colors_define(str, rgb[0], rgb[1], rgb[2]);
289 }
290 #endif
292 static char *
293 get_format(char *str)
294 {
295 gsize len = strlen(str);
297 if (str && str[0]=='\"' && str[len-1] == '\"') {
298 str[len - 1] = '\0';
299 str++;
300 }
302 return g_strdup(str);
303 }
305 static char **
306 check_screen_list(char *value)
307 {
308 char **tmp = g_strsplit_set(value, " \t,", 100);
309 char **screen = NULL;
310 int i = 0, j = 0;
312 while( tmp && tmp[i] ) {
313 char *name = g_ascii_strdown(tmp[i], -1);
314 if (*name != '\0') {
315 if (screen_lookup_name(name) == NULL) {
316 /* an unknown screen name was specified in the
317 configuration file */
318 print_error(_("Unknown screen name"), name);
319 free(name);
320 } else {
321 screen = g_realloc(screen, (j+2)*sizeof(char *));
322 screen[j++] = name;
323 screen[j] = NULL;
324 }
325 }
326 i++;
327 }
328 g_strfreev(tmp);
329 if( screen == NULL )
330 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
332 return screen;
333 }
335 static int
336 get_search_mode(char *value)
337 {
338 char * test;
339 int mode;
340 mode= strtol(value, &test, 10);
341 if (*test == '\0')
342 {
343 if (0 <= mode && mode <= 4)
344 return mode;
345 else
346 {
347 print_error(_("Invalid search mode"),value);
348 return 0;
349 }
350 }
351 else
352 {
353 for (int i = 0; value[i] != '\0'; i++)
354 value[i] = tolower(value[i]);
356 // TODO: modify screen_search so that its own list of modes can be used
357 // for comparison instead of specifying them here
358 if (strcmp(value, "title") == 0)
359 return 0;
360 else if (strcmp(value, "artist") == 0)
361 return 1;
362 else if (strcmp(value, "album") == 0)
363 return 2;
364 else if (strcmp(value, "filename") == 0)
365 return 3;
366 else if (strcmp(value, "artist+album") == 0)
367 return 4;
368 else
369 {
370 print_error(_("Unknown search mode"),value);
371 return 0;
372 }
373 }
374 }
376 static bool
377 parse_line(char *line)
378 {
379 size_t len = strlen(line), i = 0, j = 0;
380 char name[MAX_LINE_LENGTH];
381 char value[MAX_LINE_LENGTH];
383 /* get the name part */
384 while (i < len && line[i] != '=' && !g_ascii_isspace(line[i]))
385 name[j++] = line[i++];
387 name[j] = '\0';
389 /* skip '=' and whitespace */
390 while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
391 i++;
393 /* get the value part */
394 j = 0;
395 while (i < len)
396 value[j++] = line[i++];
397 value[j] = '\0';
399 /* key definition */
400 if (!strcasecmp(CONF_KEY_DEFINITION, name))
401 parse_key_definition(value);
402 /* enable colors */
403 else if(!strcasecmp(CONF_ENABLE_COLORS, name))
404 #ifdef ENABLE_COLORS
405 options.enable_colors = str2bool(value);
406 #else
407 {}
408 #endif
409 else if (!strcasecmp(CONF_SCROLL_OFFSET, name))
410 options.scroll_offset = atoi(value);
411 /* auto center */
412 else if (!strcasecmp(CONF_AUTO_CENTER, name))
413 options.auto_center = str2bool(value);
414 /* color assignment */
415 else if (!strcasecmp(CONF_COLOR, name))
416 #ifdef ENABLE_COLORS
417 parse_color(value);
418 #else
419 {}
420 #endif
421 /* wide cursor */
422 else if (!strcasecmp(CONF_WIDE_CURSOR, name))
423 options.wide_cursor = str2bool(value);
424 else if (strcasecmp(name, CONF_HARDWARE_CURSOR) == 0)
425 options.hardware_cursor = str2bool(value);
426 /* welcome screen list */
427 else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
428 options.welcome_screen_list = str2bool(value);
429 /* visible bitrate */
430 else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
431 options.visible_bitrate = str2bool(value);
432 /* timer display type */
433 else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name))
434 options.display_remaining_time = parse_timedisplay_type(value);
435 /* color definition */
436 else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
437 #ifdef ENABLE_COLORS
438 parse_color_definition(value);
439 #else
440 {}
441 #endif
442 /* list format string */
443 else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
444 g_free(options.list_format);
445 options.list_format = get_format(value);
446 /* status format string */
447 } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
448 g_free(options.status_format);
449 options.status_format = get_format(value);
450 /* xterm title format string */
451 } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
452 g_free(options.xterm_title_format);
453 options.xterm_title_format = get_format(value);
454 } else if (!strcasecmp(CONF_LIST_WRAP, name))
455 options.list_wrap = str2bool(value);
456 else if (!strcasecmp(CONF_FIND_WRAP, name))
457 options.find_wrap = str2bool(value);
458 else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
459 options.find_show_last_pattern = str2bool(value);
460 else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
461 options.audible_bell = str2bool(value);
462 else if (!strcasecmp(CONF_VISIBLE_BELL, name))
463 options.visible_bell = str2bool(value);
464 else if (!strcasecmp(CONF_BELL_ON_WRAP, name))
465 options.bell_on_wrap = str2bool(value);
466 else if (!strcasecmp(CONF_STATUS_MESSAGE_TIME, name))
467 options.status_message_time = atoi(value);
468 else if (!strcasecmp(CONF_XTERM_TITLE, name))
469 options.enable_xterm_title = str2bool(value);
470 else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
471 #ifdef HAVE_GETMOUSE
472 options.enable_mouse = str2bool(value);
473 #else
474 {}
475 #endif
476 else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
477 options.crossfade_time = atoi(value);
478 else if (!strcasecmp(CONF_SEARCH_MODE, name))
479 options.search_mode = get_search_mode(value);
480 else if (!strcasecmp(CONF_HIDE_CURSOR, name))
481 options.hide_cursor = atoi(value);
482 else if (!strcasecmp(CONF_SEEK_TIME, name))
483 options.seek_time = atoi(value);
484 else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
485 g_strfreev(options.screen_list);
486 options.screen_list = check_screen_list(value);
487 } else if (!strcasecmp(CONF_HOST, name))
488 options.host = get_format(value);
489 else if (!strcasecmp(CONF_PORT, name))
490 options.port = atoi(get_format(value));
491 else if (!strcasecmp(CONF_PASSWORD, name))
492 options.password = get_format(value);
493 else if (!strcasecmp(CONF_TIMEOUT, name))
494 options.timeout_ms = atoi(get_format(value))
495 * 1000 /* seconds -> milliseconds */;
496 else if (!strcasecmp(CONF_LYRICS_TIMEOUT, name))
497 #ifdef ENABLE_LYRICS_SCREEN
498 options.lyrics_timeout = atoi(get_format(value));
499 #else
500 {}
501 #endif
502 else if (!strcasecmp(CONF_SCROLL, name))
503 options.scroll = str2bool(value);
504 else if (!strcasecmp(CONF_SCROLL_SEP, name)) {
505 g_free(options.scroll_sep);
506 options.scroll_sep = get_format(value);
507 } else if (!strcasecmp(CONF_DISPLAY_TIME, name))
508 #ifdef NCMPC_MINI
509 {}
510 #else
511 options.display_time = str2bool(value);
512 #endif
513 else if (!strcasecmp(CONF_JUMP_PREFIX_ONLY, name))
514 #ifdef NCMPC_MINI
515 {}
516 #else
517 options.jump_prefix_only = str2bool(value);
518 #endif
519 else if (!strcasecmp(CONF_LYRICS_AUTOSAVE, name))
520 #ifdef ENABLE_LYRICS_SCREEN
521 options.lyrics_autosave = str2bool(value);
522 #else
523 {}
524 #endif
525 else if (!strcasecmp(CONF_LYRICS_SHOW_PLUGIN, name))
526 #ifdef ENABLE_LYRICS_SCREEN
527 options.lyrics_show_plugin = str2bool(value);
528 #else
529 {}
530 #endif
531 else if (!strcasecmp(name, CONF_TEXT_EDITOR))
532 #ifdef ENABLE_LYRICS_SCREEN
533 {
534 g_free(options.text_editor);
535 options.text_editor = get_format(value);
536 }
537 #else
538 {}
539 #endif
540 else if (!strcasecmp(name, CONF_TEXT_EDITOR_ASK))
541 #ifdef ENABLE_LYRICS_SCREEN
542 options.text_editor_ask = str2bool(value);
543 #else
544 {}
545 #endif
546 else if (!strcasecmp(name, CONF_CHAT_PREFIX))
547 #ifdef ENABLE_CHAT_SCREEN
548 options.chat_prefix = get_format(value);
549 #else
550 {}
551 #endif
552 else if (!strcasecmp(CONF_SECOND_COLUMN, name))
553 #ifdef NCMPC_MINI
554 {}
555 #else
556 options.second_column = str2bool(value);
557 #endif
558 else {
559 print_error(_("Unknown configuration parameter"), name);
560 return false;
561 }
563 return true;
564 }
566 static int
567 read_rc_file(char *filename)
568 {
569 assert(filename != NULL);
571 FILE *file;
572 char line[MAX_LINE_LENGTH];
574 file = fopen(filename, "r");
575 if (file == NULL) {
576 perror(filename);
577 return -1;
578 }
580 while (fgets(line, sizeof(line), file) != NULL) {
581 char *p = g_strchug(line);
583 if (*p != 0 && *p != COMMENT_TOKEN)
584 parse_line(g_strchomp(p));
585 }
587 fclose(file);
588 return 0;
589 }
591 int
592 check_user_conf_dir(void)
593 {
594 int retval;
595 char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
597 if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
598 g_free(directory);
599 return 0;
600 }
602 retval = g_mkdir(directory, 0755);
603 g_free(directory);
604 return retval;
605 }
607 char *
608 build_user_conf_filename(void)
609 {
610 #ifdef WIN32
611 return g_build_filename(g_get_user_config_dir(), PACKAGE, "ncmpc.conf", NULL);
612 #else
613 return g_build_filename(g_get_home_dir(), "." PACKAGE, "config", NULL);
614 #endif
615 }
617 char *
618 build_system_conf_filename(void)
619 {
620 #ifdef WIN32
621 const gchar* const *system_data_dirs;
622 gchar *pathname = NULL;
624 for (system_data_dirs = g_get_system_config_dirs (); *system_data_dirs != NULL; system_data_dirs++)
625 {
626 g_message (*system_data_dirs);
627 pathname = g_build_filename(*system_data_dirs, PACKAGE, "ncmpc.conf", NULL);
628 if (g_file_test(pathname, G_FILE_TEST_EXISTS))
629 {
630 break;
631 }
632 else
633 {
634 g_free (pathname);
635 pathname = NULL;
636 }
637 }
638 return pathname;
639 #else
640 return g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
641 #endif
642 }
644 char *
645 build_user_key_binding_filename(void)
646 {
647 #ifdef WIN32
648 return g_build_filename(g_get_user_config_dir(), PACKAGE, "keys.conf", NULL);
649 #else
650 return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
651 #endif
652 }
654 static char *
655 g_build_system_key_binding_filename(void)
656 {
657 #ifdef WIN32
658 const gchar* const *system_data_dirs;
659 gchar *pathname = NULL;
661 for (system_data_dirs = g_get_system_config_dirs (); *system_data_dirs != NULL; system_data_dirs++)
662 {
663 g_message (*system_data_dirs);
664 pathname = g_build_filename(*system_data_dirs, PACKAGE, "keys.conf", NULL);
665 if (g_file_test(pathname, G_FILE_TEST_EXISTS))
666 {
667 break;
668 }
669 else
670 {
671 g_free (pathname);
672 pathname = NULL;
673 }
674 }
675 return pathname;
676 #else
677 return g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
678 #endif
679 }
681 static char *
682 find_config_file(void)
683 {
684 /* check for command line configuration file */
685 if (options.config_file != NULL)
686 return g_strdup(options.config_file);
688 /* check for user configuration ~/.ncmpc/config */
689 char *filename = build_user_conf_filename();
690 if (g_file_test(filename, G_FILE_TEST_IS_REGULAR))
691 return filename;
693 g_free(filename);
695 /* check for global configuration SYSCONFDIR/ncmpc/config */
696 filename = build_system_conf_filename();
697 if (g_file_test(filename, G_FILE_TEST_IS_REGULAR))
698 return filename;
700 g_free(filename);
701 return NULL;
702 }
704 static char *
705 find_keys_file(void)
706 {
707 /* check for command line key binding file */
708 if (options.key_file != NULL)
709 return g_strdup(options.key_file);
711 /* check for user key bindings ~/.ncmpc/keys */
712 char *filename = build_user_key_binding_filename();
713 if (g_file_test(filename, G_FILE_TEST_IS_REGULAR))
714 return filename;
716 g_free(filename);
718 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
719 filename = g_build_system_key_binding_filename();
720 if (g_file_test(filename, G_FILE_TEST_IS_REGULAR))
721 return filename;
723 g_free(filename);
724 return NULL;
725 }
727 void
728 read_configuration(void)
729 {
730 /* load configuration */
731 char *filename = find_config_file();
732 if (filename != NULL) {
733 read_rc_file(filename);
734 g_free(filename);
735 }
737 /* load key bindings */
738 filename = find_keys_file();
739 if (filename != NULL) {
740 read_rc_file(filename);
741 g_free(filename);
742 }
743 }