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_WELCOME_SCREEN_LIST "welcome-screen-list"
76 #define CONF_DISPLAY_TIME "display-time"
77 #define CONF_JUMP_PREFIX_ONLY "jump-prefix-only"
78 #define CONF_LYRICS_AUTOSAVE "lyrics-autosave"
79 #define CONF_LYRICS_SHOW_PLUGIN "lyrics-show-plugin"
80 #define CONF_TEXT_EDITOR "text-editor"
81 #define CONF_TEXT_EDITOR_ASK "text-editor-ask"
82 #define CONF_SECOND_COLUMN "second-column"
84 static bool
85 str2bool(char *str)
86 {
87 return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
88 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
89 }
91 static void
92 print_error(const char *msg, const char *input)
93 {
94 fprintf(stderr, "%s: %s ('%s')\n",
95 /* To translators: prefix for error messages */
96 _("Error"), msg, input);
97 }
99 static int
100 parse_key_value(char *str, char **end)
101 {
102 if (*str == '\'') {
103 if (str[1] == '\'' || str[2] != '\'') {
104 print_error(_("Malformed hotkey definition"), str);
105 return -1;
106 }
108 *end = str + 3;
109 return str[1];
110 } else {
111 long value = strtol(str, end, 0);
112 if (*end == str) {
113 print_error(_("Malformed hotkey definition"), str);
114 return -1;
115 }
117 return (int)value;
118 }
119 }
121 static int
122 parse_key_definition(char *str)
123 {
124 char buf[MAX_LINE_LENGTH];
125 char *p;
126 size_t len = strlen(str), i;
127 int j,key;
128 int keys[MAX_COMMAND_KEYS];
129 command_t cmd;
131 /* get the command name */
132 i=0;
133 j=0;
134 memset(buf, 0, MAX_LINE_LENGTH);
135 while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
136 buf[j++] = str[i++];
137 if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
138 /* the hotkey configuration contains an unknown
139 command */
140 print_error(_("Unknown command"), buf);
141 return -1;
142 }
144 /* skip whitespace */
145 while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
146 i++;
148 /* get the value part */
149 memset(buf, 0, MAX_LINE_LENGTH);
150 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
151 if (*buf == 0) {
152 /* the hotkey configuration line is incomplete */
153 print_error(_("Incomplete hotkey configuration"), str);
154 return -1;
155 }
157 /* parse key values */
158 i = 0;
159 key = 0;
160 p = buf;
161 memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
162 while (i < MAX_COMMAND_KEYS && *p != 0 &&
163 (key = parse_key_value(p, &p)) >= 0) {
164 keys[i++] = key;
165 while (*p==',' || *p==' ' || *p=='\t')
166 p++;
167 }
169 if (key < 0)
170 return -1;
172 return assign_keys(cmd, keys);
173 }
175 static bool
176 parse_timedisplay_type(const char *str)
177 {
178 if (strcmp(str, "elapsed") == 0)
179 return false;
180 else if (strcmp(str, "remaining") == 0)
181 return true;
182 else {
183 /* translators: ncmpc supports displaying the
184 "elapsed" or "remaining" time of a song being
185 played; in this case, the configuration file
186 contained an invalid setting */
187 print_error(_("Bad time display type"), str);
188 return false;
189 }
190 }
192 #ifdef ENABLE_COLORS
193 static char *
194 separate_value(char *p)
195 {
196 char *value;
198 value = strchr(p, '=');
199 if (value == NULL) {
200 /* an equals sign '=' was expected while parsing a
201 configuration file line */
202 fprintf(stderr, "%s\n", _("Missing '='"));
203 return NULL;
204 }
206 *value++ = 0;
208 g_strchomp(p);
209 return g_strchug(value);
210 }
212 static int
213 parse_color(char *str)
214 {
215 char *value;
217 value = separate_value(str);
218 if (value == NULL)
219 return -1;
221 return colors_assign(str, value);
222 }
224 /**
225 * Returns the first non-whitespace character after the next comma
226 * character, or the end of the string. This is used to parse comma
227 * separated values.
228 */
229 static char *
230 after_comma(char *p)
231 {
232 char *comma = strchr(p, ',');
234 if (comma != NULL) {
235 *comma++ = 0;
236 comma = g_strchug(comma);
237 } else
238 comma = p + strlen(p);
240 g_strchomp(p);
241 return comma;
242 }
244 static int
245 parse_color_definition(char *str)
246 {
247 char buf[MAX_LINE_LENGTH];
248 char *value;
249 short color, rgb[3];
251 value = separate_value(str);
252 if (value == NULL)
253 return -1;
255 /* get the command name */
256 color = colors_str2color(str);
257 if (color < 0) {
258 print_error(_("Bad color name"), buf);
259 return -1;
260 }
262 /* parse r,g,b values */
264 for (unsigned i = 0; i < 3; ++i) {
265 char *next = after_comma(value), *endptr;
266 if (*value == 0) {
267 print_error(_("Incomplete color definition"), str);
268 return -1;
269 }
271 rgb[i] = strtol(value, &endptr, 0);
272 if (endptr == value || *endptr != 0) {
273 print_error(_("Invalid number"), value);
274 return -1;
275 }
277 value = next;
278 }
280 if (*value != 0) {
281 print_error(_("Malformed color definition"), str);
282 return -1;
283 }
285 return colors_define(str, rgb[0], rgb[1], rgb[2]);
286 }
287 #endif
289 static char *
290 get_format(char *str)
291 {
292 gsize len = strlen(str);
294 if (str && str[0]=='\"' && str[len-1] == '\"') {
295 str[len - 1] = '\0';
296 str++;
297 }
299 return g_strdup(str);
300 }
302 static char **
303 check_screen_list(char *value)
304 {
305 char **tmp = g_strsplit_set(value, " \t,", 100);
306 char **screen = NULL;
307 int i = 0, j = 0;
309 while( tmp && tmp[i] ) {
310 char *name = g_ascii_strdown(tmp[i], -1);
311 if (*name != '\0') {
312 if (screen_lookup_name(name) == NULL) {
313 /* an unknown screen name was specified in the
314 configuration file */
315 print_error(_("Unknown screen name"), name);
316 free(name);
317 } else {
318 screen = g_realloc(screen, (j+2)*sizeof(char *));
319 screen[j++] = name;
320 screen[j] = NULL;
321 }
322 }
323 i++;
324 }
325 g_strfreev(tmp);
326 if( screen == NULL )
327 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
329 return screen;
330 }
332 static int
333 get_search_mode(char *value)
334 {
335 char * test;
336 int mode;
337 mode= strtol(value, &test, 10);
338 if (*test == '\0')
339 {
340 if (0 <= mode && mode <= 4)
341 return mode;
342 else
343 {
344 print_error(_("Invalid search mode"),value);
345 return 0;
346 }
347 }
348 else
349 {
350 for (int i = 0; value[i] != '\0'; i++)
351 value[i] = tolower(value[i]);
353 // TODO: modify screen_search so that its own list of modes can be used
354 // for comparison instead of specifying them here
355 if (strcmp(value, "title") == 0)
356 return 0;
357 else if (strcmp(value, "artist") == 0)
358 return 1;
359 else if (strcmp(value, "album") == 0)
360 return 2;
361 else if (strcmp(value, "filename") == 0)
362 return 3;
363 else if (strcmp(value, "artist+album") == 0)
364 return 4;
365 else
366 {
367 print_error(_("Unknown search mode"),value);
368 return 0;
369 }
370 }
371 }
373 static bool
374 parse_line(char *line)
375 {
376 size_t len = strlen(line), i = 0, j = 0;
377 char name[MAX_LINE_LENGTH];
378 char value[MAX_LINE_LENGTH];
379 bool match_found;
381 /* get the name part */
382 while (i < len && line[i] != '=' && !g_ascii_isspace(line[i]))
383 name[j++] = line[i++];
385 name[j] = '\0';
387 /* skip '=' and whitespace */
388 while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
389 i++;
391 /* get the value part */
392 j = 0;
393 while (i < len)
394 value[j++] = line[i++];
395 value[j] = '\0';
397 match_found = true;
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, "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(CONF_SECOND_COLUMN, name))
547 #ifdef NCMPC_MINI
548 {}
549 #else
550 options.second_column = str2bool(value);
551 #endif
552 else
553 match_found = false;
555 if (!match_found)
556 print_error(_("Unknown configuration parameter"), name);
558 return match_found;
559 }
561 static int
562 read_rc_file(char *filename)
563 {
564 FILE *file;
565 char line[MAX_LINE_LENGTH];
567 if (filename == NULL)
568 return -1;
570 file = fopen(filename, "r");
571 if (file == NULL) {
572 perror(filename);
573 return -1;
574 }
576 while (fgets(line, sizeof(line), file) != NULL) {
577 char *p = g_strchug(line);
579 if (*p != 0 && *p != COMMENT_TOKEN)
580 parse_line(g_strchomp(p));
581 }
583 fclose(file);
584 return 0;
585 }
587 int
588 check_user_conf_dir(void)
589 {
590 int retval;
591 char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
593 if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
594 g_free(directory);
595 return 0;
596 }
598 retval = g_mkdir(directory, 0755);
599 g_free(directory);
600 return retval;
601 }
603 char *
604 get_user_key_binding_filename(void)
605 {
606 return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
607 }
609 int
610 read_configuration(void)
611 {
612 char *filename = NULL;
614 /* check for command line configuration file */
615 if (options.config_file)
616 filename = g_strdup(options.config_file);
618 /* check for user configuration ~/.ncmpc/config */
619 if (filename == NULL) {
620 filename = g_build_filename(g_get_home_dir(),
621 "." PACKAGE, "config", NULL);
622 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
623 g_free(filename);
624 filename = NULL;
625 }
626 }
628 /* check for global configuration SYSCONFDIR/ncmpc/config */
629 if (filename == NULL) {
630 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
631 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
632 g_free(filename);
633 filename = NULL;
634 }
635 }
637 /* load configuration */
638 if (filename) {
639 read_rc_file(filename);
640 g_free(filename);
641 filename = NULL;
642 }
644 /* check for command line key binding file */
645 if (options.key_file)
646 filename = g_strdup(options.key_file);
648 /* check for user key bindings ~/.ncmpc/keys */
649 if (filename == NULL) {
650 filename = get_user_key_binding_filename();
651 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
652 g_free(filename);
653 filename = NULL;
654 }
655 }
657 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
658 if (filename == NULL) {
659 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
660 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
661 g_free(filename);
662 filename = NULL;
663 }
664 }
666 /* load key bindings */
667 if (filename) {
668 read_rc_file(filename);
669 g_free(filename);
670 filename = NULL;
671 }
673 return 0;
674 }