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];
381 bool match_found;
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 match_found = true;
401 /* key definition */
402 if (!strcasecmp(CONF_KEY_DEFINITION, name))
403 parse_key_definition(value);
404 /* enable colors */
405 else if(!strcasecmp(CONF_ENABLE_COLORS, name))
406 #ifdef ENABLE_COLORS
407 options.enable_colors = str2bool(value);
408 #else
409 {}
410 #endif
411 else if (!strcasecmp(CONF_SCROLL_OFFSET, name))
412 options.scroll_offset = atoi(value);
413 /* auto center */
414 else if (!strcasecmp(CONF_AUTO_CENTER, name))
415 options.auto_center = str2bool(value);
416 /* color assignment */
417 else if (!strcasecmp(CONF_COLOR, name))
418 #ifdef ENABLE_COLORS
419 parse_color(value);
420 #else
421 {}
422 #endif
423 /* wide cursor */
424 else if (!strcasecmp(CONF_WIDE_CURSOR, name))
425 options.wide_cursor = str2bool(value);
426 else if (strcasecmp(name, CONF_HARDWARE_CURSOR) == 0)
427 options.hardware_cursor = str2bool(value);
428 /* welcome screen list */
429 else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
430 options.welcome_screen_list = str2bool(value);
431 /* visible bitrate */
432 else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
433 options.visible_bitrate = str2bool(value);
434 /* timer display type */
435 else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name))
436 options.display_remaining_time = parse_timedisplay_type(value);
437 /* color definition */
438 else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
439 #ifdef ENABLE_COLORS
440 parse_color_definition(value);
441 #else
442 {}
443 #endif
444 /* list format string */
445 else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
446 g_free(options.list_format);
447 options.list_format = get_format(value);
448 /* status format string */
449 } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
450 g_free(options.status_format);
451 options.status_format = get_format(value);
452 /* xterm title format string */
453 } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
454 g_free(options.xterm_title_format);
455 options.xterm_title_format = get_format(value);
456 } else if (!strcasecmp(CONF_LIST_WRAP, name))
457 options.list_wrap = str2bool(value);
458 else if (!strcasecmp(CONF_FIND_WRAP, name))
459 options.find_wrap = str2bool(value);
460 else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
461 options.find_show_last_pattern = str2bool(value);
462 else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
463 options.audible_bell = str2bool(value);
464 else if (!strcasecmp(CONF_VISIBLE_BELL, name))
465 options.visible_bell = str2bool(value);
466 else if (!strcasecmp(CONF_BELL_ON_WRAP, name))
467 options.bell_on_wrap = str2bool(value);
468 else if (!strcasecmp(CONF_STATUS_MESSAGE_TIME, name))
469 options.status_message_time = atoi(value);
470 else if (!strcasecmp(CONF_XTERM_TITLE, name))
471 options.enable_xterm_title = str2bool(value);
472 else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
473 #ifdef HAVE_GETMOUSE
474 options.enable_mouse = str2bool(value);
475 #else
476 {}
477 #endif
478 else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
479 options.crossfade_time = atoi(value);
480 else if (!strcasecmp(CONF_SEARCH_MODE, name))
481 options.search_mode = get_search_mode(value);
482 else if (!strcasecmp(CONF_HIDE_CURSOR, name))
483 options.hide_cursor = atoi(value);
484 else if (!strcasecmp(CONF_SEEK_TIME, name))
485 options.seek_time = atoi(value);
486 else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
487 g_strfreev(options.screen_list);
488 options.screen_list = check_screen_list(value);
489 } else if (!strcasecmp(CONF_HOST, name))
490 options.host = get_format(value);
491 else if (!strcasecmp(CONF_PORT, name))
492 options.port = atoi(get_format(value));
493 else if (!strcasecmp(CONF_PASSWORD, name))
494 options.password = get_format(value);
495 else if (!strcasecmp(CONF_TIMEOUT, name))
496 options.timeout_ms = atoi(get_format(value))
497 * 1000 /* seconds -> milliseconds */;
498 else if (!strcasecmp(CONF_LYRICS_TIMEOUT, name))
499 #ifdef ENABLE_LYRICS_SCREEN
500 options.lyrics_timeout = atoi(get_format(value));
501 #else
502 {}
503 #endif
504 else if (!strcasecmp(CONF_SCROLL, name))
505 options.scroll = str2bool(value);
506 else if (!strcasecmp(CONF_SCROLL_SEP, name)) {
507 g_free(options.scroll_sep);
508 options.scroll_sep = get_format(value);
509 } else if (!strcasecmp(CONF_DISPLAY_TIME, name))
510 #ifdef NCMPC_MINI
511 {}
512 #else
513 options.display_time = str2bool(value);
514 #endif
515 else if (!strcasecmp(CONF_JUMP_PREFIX_ONLY, name))
516 #ifdef NCMPC_MINI
517 {}
518 #else
519 options.jump_prefix_only = str2bool(value);
520 #endif
521 else if (!strcasecmp(CONF_LYRICS_AUTOSAVE, name))
522 #ifdef ENABLE_LYRICS_SCREEN
523 options.lyrics_autosave = str2bool(value);
524 #else
525 {}
526 #endif
527 else if (!strcasecmp(CONF_LYRICS_SHOW_PLUGIN, name))
528 #ifdef ENABLE_LYRICS_SCREEN
529 options.lyrics_show_plugin = str2bool(value);
530 #else
531 {}
532 #endif
533 else if (!strcasecmp(name, CONF_TEXT_EDITOR))
534 #ifdef ENABLE_LYRICS_SCREEN
535 {
536 g_free(options.text_editor);
537 options.text_editor = get_format(value);
538 }
539 #else
540 {}
541 #endif
542 else if (!strcasecmp(name, CONF_TEXT_EDITOR_ASK))
543 #ifdef ENABLE_LYRICS_SCREEN
544 options.text_editor_ask = str2bool(value);
545 #else
546 {}
547 #endif
548 else if (!strcasecmp(name, CONF_CHAT_PREFIX))
549 #ifdef ENABLE_CHAT_SCREEN
550 options.chat_prefix = get_format(value);
551 #else
552 {}
553 #endif
554 else if (!strcasecmp(CONF_SECOND_COLUMN, name))
555 #ifdef NCMPC_MINI
556 {}
557 #else
558 options.second_column = str2bool(value);
559 #endif
560 else
561 match_found = false;
563 if (!match_found)
564 print_error(_("Unknown configuration parameter"), name);
566 return match_found;
567 }
569 static int
570 read_rc_file(char *filename)
571 {
572 FILE *file;
573 char line[MAX_LINE_LENGTH];
575 if (filename == NULL)
576 return -1;
578 file = fopen(filename, "r");
579 if (file == NULL) {
580 perror(filename);
581 return -1;
582 }
584 while (fgets(line, sizeof(line), file) != NULL) {
585 char *p = g_strchug(line);
587 if (*p != 0 && *p != COMMENT_TOKEN)
588 parse_line(g_strchomp(p));
589 }
591 fclose(file);
592 return 0;
593 }
595 int
596 check_user_conf_dir(void)
597 {
598 int retval;
599 char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
601 if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
602 g_free(directory);
603 return 0;
604 }
606 retval = g_mkdir(directory, 0755);
607 g_free(directory);
608 return retval;
609 }
611 char *
612 build_user_conf_filename(void)
613 {
614 #ifdef WIN32
615 return g_build_filename(g_get_user_config_dir(), PACKAGE, "ncmpc.conf", NULL);
616 #else
617 return g_build_filename(g_get_home_dir(), "." PACKAGE, "config", NULL);
618 #endif
619 }
621 char *
622 build_system_conf_filename(void)
623 {
624 #ifdef WIN32
625 const gchar* const *system_data_dirs;
626 gchar *pathname = NULL;
628 for (system_data_dirs = g_get_system_config_dirs (); *system_data_dirs != NULL; system_data_dirs++)
629 {
630 g_message (*system_data_dirs);
631 pathname = g_build_filename(*system_data_dirs, PACKAGE, "ncmpc.conf", NULL);
632 if (g_file_test(pathname, G_FILE_TEST_EXISTS))
633 {
634 break;
635 }
636 else
637 {
638 g_free (pathname);
639 pathname = NULL;
640 }
641 }
642 return pathname;
643 #else
644 return g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
645 #endif
646 }
648 char *
649 build_user_key_binding_filename(void)
650 {
651 #ifdef WIN32
652 return g_build_filename(g_get_user_config_dir(), PACKAGE, "keys.conf", NULL);
653 #else
654 return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
655 #endif
656 }
658 static char *
659 g_build_system_key_binding_filename(void)
660 {
661 #ifdef WIN32
662 const gchar* const *system_data_dirs;
663 gchar *pathname = NULL;
665 for (system_data_dirs = g_get_system_config_dirs (); *system_data_dirs != NULL; system_data_dirs++)
666 {
667 g_message (*system_data_dirs);
668 pathname = g_build_filename(*system_data_dirs, PACKAGE, "keys.conf", NULL);
669 if (g_file_test(pathname, G_FILE_TEST_EXISTS))
670 {
671 break;
672 }
673 else
674 {
675 g_free (pathname);
676 pathname = NULL;
677 }
678 }
679 return pathname;
680 #else
681 return g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
682 #endif
683 }
685 void
686 read_configuration(void)
687 {
688 char *filename = NULL;
690 /* check for command line configuration file */
691 if (options.config_file)
692 filename = g_strdup(options.config_file);
694 /* check for user configuration ~/.ncmpc/config */
695 if (filename == NULL) {
696 filename = build_user_conf_filename();
697 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
698 g_free(filename);
699 filename = NULL;
700 }
701 }
703 /* check for global configuration SYSCONFDIR/ncmpc/config */
704 if (filename == NULL) {
705 filename = build_system_conf_filename();
706 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
707 g_free(filename);
708 filename = NULL;
709 }
710 }
712 /* load configuration */
713 if (filename) {
714 read_rc_file(filename);
715 g_free(filename);
716 filename = NULL;
717 }
719 /* check for command line key binding file */
720 if (options.key_file)
721 filename = g_strdup(options.key_file);
723 /* check for user key bindings ~/.ncmpc/keys */
724 if (filename == NULL) {
725 filename = build_user_key_binding_filename();
726 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
727 g_free(filename);
728 filename = NULL;
729 }
730 }
732 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
733 if (filename == NULL) {
734 filename = g_build_system_key_binding_filename();
735 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
736 g_free(filename);
737 filename = NULL;
738 }
739 }
741 /* load key bindings */
742 if (filename) {
743 read_rc_file(filename);
744 g_free(filename);
745 filename = NULL;
746 }
747 }