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 <ctype.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <glib.h>
36 #include <glib/gstdio.h>
38 #define MAX_LINE_LENGTH 1024
39 #define COMMENT_TOKEN '#'
41 /* configuration field names */
42 #define CONF_ENABLE_COLORS "enable-colors"
43 #define CONF_SCROLL_OFFSET "scroll-offset"
44 #define CONF_AUTO_CENTER "auto-center"
45 #define CONF_WIDE_CURSOR "wide-cursor"
46 #define CONF_KEY_DEFINITION "key"
47 #define CONF_COLOR "color"
48 #define CONF_COLOR_DEFINITION "colordef"
49 #define CONF_LIST_FORMAT "list-format"
50 #define CONF_STATUS_FORMAT "status-format"
51 #define CONF_XTERM_TITLE_FORMAT "xterm-title-format"
52 #define CONF_LIST_WRAP "wrap-around"
53 #define CONF_FIND_WRAP "find-wrap"
54 #define CONF_FIND_SHOW_LAST "find-show-last"
55 #define CONF_AUDIBLE_BELL "audible-bell"
56 #define CONF_VISIBLE_BELL "visible-bell"
57 #define CONF_BELL_ON_WRAP "bell-on-wrap"
58 #define CONF_STATUS_MESSAGE_TIME "status-message-time"
59 #define CONF_XTERM_TITLE "set-xterm-title"
60 #define CONF_ENABLE_MOUSE "enable-mouse"
61 #define CONF_CROSSFADE_TIME "crossfade-time"
62 #define CONF_SEARCH_MODE "search-mode"
63 #define CONF_HIDE_CURSOR "hide-cursor"
64 #define CONF_SEEK_TIME "seek-time"
65 #define CONF_SCREEN_LIST "screen-list"
66 #define CONF_TIMEDISPLAY_TYPE "timedisplay-type"
67 #define CONF_HOST "host"
68 #define CONF_PORT "port"
69 #define CONF_PASSWORD "password"
70 #define CONF_TIMEOUT "timeout"
71 #define CONF_LYRICS_TIMEOUT "lyrics-timeout"
72 #define CONF_SCROLL "scroll"
73 #define CONF_SCROLL_SEP "scroll-sep"
74 #define CONF_VISIBLE_BITRATE "visible-bitrate"
75 #define CONF_HARDWARE_CURSOR "hardware-cursor"
76 #define CONF_WELCOME_SCREEN_LIST "welcome-screen-list"
77 #define CONF_DISPLAY_TIME "display-time"
78 #define CONF_JUMP_PREFIX_ONLY "jump-prefix-only"
79 #define CONF_LYRICS_AUTOSAVE "lyrics-autosave"
80 #define CONF_LYRICS_SHOW_PLUGIN "lyrics-show-plugin"
81 #define CONF_TEXT_EDITOR "text-editor"
82 #define CONF_TEXT_EDITOR_ASK "text-editor-ask"
83 #define CONF_CHAT_PREFIX "chat-prefix"
84 #define CONF_SECOND_COLUMN "second-column"
86 static bool
87 str2bool(char *str)
88 {
89 return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
90 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
91 }
93 static void
94 print_error(const char *msg, const char *input)
95 {
96 fprintf(stderr, "%s: %s ('%s')\n",
97 /* To translators: prefix for error messages */
98 _("Error"), msg, input);
99 }
101 static int
102 parse_key_value(char *str, char **end)
103 {
104 if (*str == '\'') {
105 if (str[1] == '\'' || str[2] != '\'') {
106 print_error(_("Malformed hotkey definition"), str);
107 return -1;
108 }
110 *end = str + 3;
111 return str[1];
112 } else {
113 long value = strtol(str, end, 0);
114 if (*end == str) {
115 print_error(_("Malformed hotkey definition"), str);
116 return -1;
117 }
119 return (int)value;
120 }
121 }
123 static int
124 parse_key_definition(char *str)
125 {
126 char buf[MAX_LINE_LENGTH];
127 char *p;
128 size_t len = strlen(str), i;
129 int j,key;
130 int keys[MAX_COMMAND_KEYS];
131 command_t cmd;
133 /* get the command name */
134 i=0;
135 j=0;
136 memset(buf, 0, MAX_LINE_LENGTH);
137 while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
138 buf[j++] = str[i++];
139 if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
140 /* the hotkey configuration contains an unknown
141 command */
142 print_error(_("Unknown command"), buf);
143 return -1;
144 }
146 /* skip whitespace */
147 while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
148 i++;
150 /* get the value part */
151 memset(buf, 0, MAX_LINE_LENGTH);
152 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
153 if (*buf == 0) {
154 /* the hotkey configuration line is incomplete */
155 print_error(_("Incomplete hotkey configuration"), str);
156 return -1;
157 }
159 /* parse key values */
160 i = 0;
161 key = 0;
162 p = buf;
163 memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
164 while (i < MAX_COMMAND_KEYS && *p != 0 &&
165 (key = parse_key_value(p, &p)) >= 0) {
166 keys[i++] = key;
167 while (*p==',' || *p==' ' || *p=='\t')
168 p++;
169 }
171 if (key < 0)
172 return -1;
174 return assign_keys(cmd, keys);
175 }
177 static bool
178 parse_timedisplay_type(const char *str)
179 {
180 if (strcmp(str, "elapsed") == 0)
181 return false;
182 else if (strcmp(str, "remaining") == 0)
183 return true;
184 else {
185 /* translators: ncmpc supports displaying the
186 "elapsed" or "remaining" time of a song being
187 played; in this case, the configuration file
188 contained an invalid setting */
189 print_error(_("Bad time display type"), str);
190 return false;
191 }
192 }
194 #ifdef ENABLE_COLORS
195 static char *
196 separate_value(char *p)
197 {
198 char *value;
200 value = strchr(p, '=');
201 if (value == NULL) {
202 /* an equals sign '=' was expected while parsing a
203 configuration file line */
204 fprintf(stderr, "%s\n", _("Missing '='"));
205 return NULL;
206 }
208 *value++ = 0;
210 g_strchomp(p);
211 return g_strchug(value);
212 }
214 static int
215 parse_color(char *str)
216 {
217 char *value;
219 value = separate_value(str);
220 if (value == NULL)
221 return -1;
223 return colors_assign(str, value);
224 }
226 /**
227 * Returns the first non-whitespace character after the next comma
228 * character, or the end of the string. This is used to parse comma
229 * separated values.
230 */
231 static char *
232 after_comma(char *p)
233 {
234 char *comma = strchr(p, ',');
236 if (comma != NULL) {
237 *comma++ = 0;
238 comma = g_strchug(comma);
239 } else
240 comma = p + strlen(p);
242 g_strchomp(p);
243 return comma;
244 }
246 static int
247 parse_color_definition(char *str)
248 {
249 char buf[MAX_LINE_LENGTH];
250 char *value;
251 short color, rgb[3];
253 value = separate_value(str);
254 if (value == NULL)
255 return -1;
257 /* get the command name */
258 color = colors_str2color(str);
259 if (color < 0) {
260 print_error(_("Bad color name"), buf);
261 return -1;
262 }
264 /* parse r,g,b values */
266 for (unsigned i = 0; i < 3; ++i) {
267 char *next = after_comma(value), *endptr;
268 if (*value == 0) {
269 print_error(_("Incomplete color definition"), str);
270 return -1;
271 }
273 rgb[i] = strtol(value, &endptr, 0);
274 if (endptr == value || *endptr != 0) {
275 print_error(_("Invalid number"), value);
276 return -1;
277 }
279 value = next;
280 }
282 if (*value != 0) {
283 print_error(_("Malformed color definition"), str);
284 return -1;
285 }
287 return colors_define(str, rgb[0], rgb[1], rgb[2]);
288 }
289 #endif
291 static char *
292 get_format(char *str)
293 {
294 gsize len = strlen(str);
296 if (str && str[0]=='\"' && str[len-1] == '\"') {
297 str[len - 1] = '\0';
298 str++;
299 }
301 return g_strdup(str);
302 }
304 static char **
305 check_screen_list(char *value)
306 {
307 char **tmp = g_strsplit_set(value, " \t,", 100);
308 char **screen = NULL;
309 int i = 0, j = 0;
311 while( tmp && tmp[i] ) {
312 char *name = g_ascii_strdown(tmp[i], -1);
313 if (*name != '\0') {
314 if (screen_lookup_name(name) == NULL) {
315 /* an unknown screen name was specified in the
316 configuration file */
317 print_error(_("Unknown screen name"), name);
318 free(name);
319 } else {
320 screen = g_realloc(screen, (j+2)*sizeof(char *));
321 screen[j++] = name;
322 screen[j] = NULL;
323 }
324 }
325 i++;
326 }
327 g_strfreev(tmp);
328 if( screen == NULL )
329 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
331 return screen;
332 }
334 static int
335 get_search_mode(char *value)
336 {
337 char * test;
338 int mode;
339 mode= strtol(value, &test, 10);
340 if (*test == '\0')
341 {
342 if (0 <= mode && mode <= 4)
343 return mode;
344 else
345 {
346 print_error(_("Invalid search mode"),value);
347 return 0;
348 }
349 }
350 else
351 {
352 for (int i = 0; value[i] != '\0'; i++)
353 value[i] = tolower(value[i]);
355 // TODO: modify screen_search so that its own list of modes can be used
356 // for comparison instead of specifying them here
357 if (strcmp(value, "title") == 0)
358 return 0;
359 else if (strcmp(value, "artist") == 0)
360 return 1;
361 else if (strcmp(value, "album") == 0)
362 return 2;
363 else if (strcmp(value, "filename") == 0)
364 return 3;
365 else if (strcmp(value, "artist+album") == 0)
366 return 4;
367 else
368 {
369 print_error(_("Unknown search mode"),value);
370 return 0;
371 }
372 }
373 }
375 static bool
376 parse_line(char *line)
377 {
378 size_t len = strlen(line), i = 0, j = 0;
379 char name[MAX_LINE_LENGTH];
380 char value[MAX_LINE_LENGTH];
382 /* get the name part */
383 while (i < len && line[i] != '=' && !g_ascii_isspace(line[i]))
384 name[j++] = line[i++];
386 name[j] = '\0';
388 /* skip '=' and whitespace */
389 while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
390 i++;
392 /* get the value part */
393 j = 0;
394 while (i < len)
395 value[j++] = line[i++];
396 value[j] = '\0';
398 /* key definition */
399 if (!strcasecmp(CONF_KEY_DEFINITION, name))
400 parse_key_definition(value);
401 /* enable colors */
402 else if(!strcasecmp(CONF_ENABLE_COLORS, name))
403 #ifdef ENABLE_COLORS
404 options.enable_colors = str2bool(value);
405 #else
406 {}
407 #endif
408 else if (!strcasecmp(CONF_SCROLL_OFFSET, name))
409 options.scroll_offset = atoi(value);
410 /* auto center */
411 else if (!strcasecmp(CONF_AUTO_CENTER, name))
412 options.auto_center = str2bool(value);
413 /* color assignment */
414 else if (!strcasecmp(CONF_COLOR, name))
415 #ifdef ENABLE_COLORS
416 parse_color(value);
417 #else
418 {}
419 #endif
420 /* wide cursor */
421 else if (!strcasecmp(CONF_WIDE_CURSOR, name))
422 options.wide_cursor = str2bool(value);
423 else if (strcasecmp(name, CONF_HARDWARE_CURSOR) == 0)
424 options.hardware_cursor = str2bool(value);
425 /* welcome screen list */
426 else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
427 options.welcome_screen_list = str2bool(value);
428 /* visible bitrate */
429 else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
430 options.visible_bitrate = str2bool(value);
431 /* timer display type */
432 else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name))
433 options.display_remaining_time = parse_timedisplay_type(value);
434 /* color definition */
435 else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
436 #ifdef ENABLE_COLORS
437 parse_color_definition(value);
438 #else
439 {}
440 #endif
441 /* list format string */
442 else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
443 g_free(options.list_format);
444 options.list_format = get_format(value);
445 /* status format string */
446 } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
447 g_free(options.status_format);
448 options.status_format = get_format(value);
449 /* xterm title format string */
450 } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
451 g_free(options.xterm_title_format);
452 options.xterm_title_format = get_format(value);
453 } else if (!strcasecmp(CONF_LIST_WRAP, name))
454 options.list_wrap = str2bool(value);
455 else if (!strcasecmp(CONF_FIND_WRAP, name))
456 options.find_wrap = str2bool(value);
457 else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
458 options.find_show_last_pattern = str2bool(value);
459 else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
460 options.audible_bell = str2bool(value);
461 else if (!strcasecmp(CONF_VISIBLE_BELL, name))
462 options.visible_bell = str2bool(value);
463 else if (!strcasecmp(CONF_BELL_ON_WRAP, name))
464 options.bell_on_wrap = str2bool(value);
465 else if (!strcasecmp(CONF_STATUS_MESSAGE_TIME, name))
466 options.status_message_time = atoi(value);
467 else if (!strcasecmp(CONF_XTERM_TITLE, name))
468 options.enable_xterm_title = str2bool(value);
469 else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
470 #ifdef HAVE_GETMOUSE
471 options.enable_mouse = str2bool(value);
472 #else
473 {}
474 #endif
475 else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
476 options.crossfade_time = atoi(value);
477 else if (!strcasecmp(CONF_SEARCH_MODE, name))
478 options.search_mode = get_search_mode(value);
479 else if (!strcasecmp(CONF_HIDE_CURSOR, name))
480 options.hide_cursor = atoi(value);
481 else if (!strcasecmp(CONF_SEEK_TIME, name))
482 options.seek_time = atoi(value);
483 else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
484 g_strfreev(options.screen_list);
485 options.screen_list = check_screen_list(value);
486 } else if (!strcasecmp(CONF_HOST, name))
487 options.host = get_format(value);
488 else if (!strcasecmp(CONF_PORT, name))
489 options.port = atoi(get_format(value));
490 else if (!strcasecmp(CONF_PASSWORD, name))
491 options.password = get_format(value);
492 else if (!strcasecmp(CONF_TIMEOUT, name))
493 options.timeout_ms = atoi(get_format(value))
494 * 1000 /* seconds -> milliseconds */;
495 else if (!strcasecmp(CONF_LYRICS_TIMEOUT, name))
496 #ifdef ENABLE_LYRICS_SCREEN
497 options.lyrics_timeout = atoi(get_format(value));
498 #else
499 {}
500 #endif
501 else if (!strcasecmp(CONF_SCROLL, name))
502 options.scroll = str2bool(value);
503 else if (!strcasecmp(CONF_SCROLL_SEP, name)) {
504 g_free(options.scroll_sep);
505 options.scroll_sep = get_format(value);
506 } else if (!strcasecmp(CONF_DISPLAY_TIME, name))
507 #ifdef NCMPC_MINI
508 {}
509 #else
510 options.display_time = str2bool(value);
511 #endif
512 else if (!strcasecmp(CONF_JUMP_PREFIX_ONLY, name))
513 #ifdef NCMPC_MINI
514 {}
515 #else
516 options.jump_prefix_only = str2bool(value);
517 #endif
518 else if (!strcasecmp(CONF_LYRICS_AUTOSAVE, name))
519 #ifdef ENABLE_LYRICS_SCREEN
520 options.lyrics_autosave = str2bool(value);
521 #else
522 {}
523 #endif
524 else if (!strcasecmp(CONF_LYRICS_SHOW_PLUGIN, name))
525 #ifdef ENABLE_LYRICS_SCREEN
526 options.lyrics_show_plugin = str2bool(value);
527 #else
528 {}
529 #endif
530 else if (!strcasecmp(name, CONF_TEXT_EDITOR))
531 #ifdef ENABLE_LYRICS_SCREEN
532 {
533 g_free(options.text_editor);
534 options.text_editor = get_format(value);
535 }
536 #else
537 {}
538 #endif
539 else if (!strcasecmp(name, CONF_TEXT_EDITOR_ASK))
540 #ifdef ENABLE_LYRICS_SCREEN
541 options.text_editor_ask = str2bool(value);
542 #else
543 {}
544 #endif
545 else if (!strcasecmp(name, CONF_CHAT_PREFIX))
546 #ifdef ENABLE_CHAT_SCREEN
547 options.chat_prefix = get_format(value);
548 #else
549 {}
550 #endif
551 else if (!strcasecmp(CONF_SECOND_COLUMN, name))
552 #ifdef NCMPC_MINI
553 {}
554 #else
555 options.second_column = str2bool(value);
556 #endif
557 else {
558 print_error(_("Unknown configuration parameter"), name);
559 return false;
560 }
562 return true;
563 }
565 static int
566 read_rc_file(char *filename)
567 {
568 FILE *file;
569 char line[MAX_LINE_LENGTH];
571 if (filename == NULL)
572 return -1;
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 void
682 read_configuration(void)
683 {
684 char *filename = NULL;
686 /* check for command line configuration file */
687 if (options.config_file)
688 filename = g_strdup(options.config_file);
690 /* check for user configuration ~/.ncmpc/config */
691 if (filename == NULL) {
692 filename = build_user_conf_filename();
693 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
694 g_free(filename);
695 filename = NULL;
696 }
697 }
699 /* check for global configuration SYSCONFDIR/ncmpc/config */
700 if (filename == NULL) {
701 filename = build_system_conf_filename();
702 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
703 g_free(filename);
704 filename = NULL;
705 }
706 }
708 /* load configuration */
709 if (filename) {
710 read_rc_file(filename);
711 g_free(filename);
712 filename = NULL;
713 }
715 /* check for command line key binding file */
716 if (options.key_file)
717 filename = g_strdup(options.key_file);
719 /* check for user key bindings ~/.ncmpc/keys */
720 if (filename == NULL) {
721 filename = build_user_key_binding_filename();
722 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
723 g_free(filename);
724 filename = NULL;
725 }
726 }
728 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
729 if (filename == NULL) {
730 filename = g_build_system_key_binding_filename();
731 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
732 g_free(filename);
733 filename = NULL;
734 }
735 }
737 /* load key bindings */
738 if (filename) {
739 read_rc_file(filename);
740 g_free(filename);
741 filename = NULL;
742 }
743 }