1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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.
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.
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 <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <glib.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_LYRICS_TIMEOUT "lyrics-timeout"
72 #define CONF_SHOW_SPLASH "show-splash"
73 #define CONF_SCROLL "scroll"
74 #define CONF_SCROLL_SEP "scroll-sep"
75 #define CONF_VISIBLE_BITRATE "visible-bitrate"
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"
81 static bool
82 str2bool(char *str)
83 {
84 return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
85 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
86 }
88 static void
89 print_error(const char *msg, const char *input)
90 {
91 fprintf(stderr, "%s: %s ('%s')\n",
92 /* To translators: prefix for error messages */
93 _("Error"), msg, input);
94 }
96 static int
97 parse_key_value(char *str, char **end)
98 {
99 if (*str == '\'') {
100 if (str[1] == '\'' || str[2] != '\'') {
101 print_error(_("Malformed hotkey definition"), str);
102 return -1;
103 }
105 *end = str + 3;
106 return str[1];
107 } else {
108 long value = strtol(str, end, 0);
109 if (*end == str) {
110 print_error(_("Malformed hotkey definition"), str);
111 return -1;
112 }
114 return (int)value;
115 }
116 }
118 static int
119 parse_key_definition(char *str)
120 {
121 char buf[MAX_LINE_LENGTH];
122 char *p;
123 size_t len = strlen(str), i;
124 int j,key;
125 int keys[MAX_COMMAND_KEYS];
126 command_t cmd;
128 /* get the command name */
129 i=0;
130 j=0;
131 memset(buf, 0, MAX_LINE_LENGTH);
132 while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
133 buf[j++] = str[i++];
134 if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
135 /* the hotkey configuration contains an unknown
136 command */
137 print_error(_("Unknown command"), buf);
138 return -1;
139 }
141 /* skip whitespace */
142 while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
143 i++;
145 /* get the value part */
146 memset(buf, 0, MAX_LINE_LENGTH);
147 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
148 if (*buf == 0) {
149 /* the hotkey configuration line is incomplete */
150 print_error(_("Incomplete hotkey configuration"), str);
151 return -1;
152 }
154 /* parse key values */
155 i = 0;
156 key = 0;
157 p = buf;
158 memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
159 while (i < MAX_COMMAND_KEYS && *p != 0 &&
160 (key = parse_key_value(p, &p)) >= 0) {
161 keys[i++] = key;
162 while (*p==',' || *p==' ' || *p=='\t')
163 p++;
164 }
166 if (key < 0)
167 return -1;
169 return assign_keys(cmd, keys);
170 }
172 static const char *
173 parse_timedisplay_type(const char *str)
174 {
175 if (!strcmp(str,"elapsed") || !strcmp(str,"remaining"))
176 return str;
177 else {
178 /* translators: ncmpc supports displaying the
179 "elapsed" or "remaining" time of a song being
180 played; in this case, the configuration file
181 contained an invalid setting */
182 print_error(_("Bad time display type"), str);
183 return DEFAULT_TIMEDISPLAY_TYPE;
184 }
185 }
187 #ifdef ENABLE_COLORS
188 static char *
189 separate_value(char *p)
190 {
191 char *value;
193 value = strchr(p, '=');
194 if (value == NULL) {
195 /* an equals sign '=' was expected while parsing a
196 configuration file line */
197 fprintf(stderr, "%s\n", _("Missing '='"));
198 return NULL;
199 }
201 *value++ = 0;
203 g_strchomp(p);
204 return g_strchug(value);
205 }
207 static int
208 parse_color(char *str)
209 {
210 char *value;
212 value = separate_value(str);
213 if (value == NULL)
214 return -1;
216 return colors_assign(str, value);
217 }
219 /**
220 * Returns the first non-whitespace character after the next comma
221 * character, or the end of the string. This is used to parse comma
222 * separated values.
223 */
224 static char *
225 after_comma(char *p)
226 {
227 char *comma = strchr(p, ',');
229 if (comma != NULL) {
230 *comma++ = 0;
231 comma = g_strchug(comma);
232 } else
233 comma = p + strlen(p);
235 g_strchomp(p);
236 return comma;
237 }
239 static int
240 parse_color_definition(char *str)
241 {
242 char buf[MAX_LINE_LENGTH];
243 char *value;
244 short color, rgb[3];
246 value = separate_value(str);
247 if (value == NULL)
248 return -1;
250 /* get the command name */
251 color = colors_str2color(str);
252 if (color < 0) {
253 print_error(_("Bad color name"), buf);
254 return -1;
255 }
257 /* parse r,g,b values */
259 for (unsigned i = 0; i < 3; ++i) {
260 char *next = after_comma(value), *endptr;
261 if (*value == 0) {
262 print_error(_("Incomplete color definition"), str);
263 return -1;
264 }
266 rgb[i] = strtol(value, &endptr, 0);
267 if (endptr == value || *endptr != 0) {
268 print_error(_("Invalid number"), value);
269 return -1;
270 }
272 value = next;
273 }
275 if (*value != 0) {
276 print_error(_("Malformed color definition"), str);
277 return -1;
278 }
280 return colors_define(str, rgb[0], rgb[1], rgb[2]);
281 }
282 #endif
284 static char *
285 get_format(char *str)
286 {
287 gsize len = strlen(str);
289 if (str && str[0]=='\"' && str[len-1] == '\"') {
290 str[len - 1] = '\0';
291 str++;
292 }
294 return g_strdup(str);
295 }
297 static char **
298 check_screen_list(char *value)
299 {
300 char **tmp = g_strsplit_set(value, " \t,", 100);
301 char **screen = NULL;
302 int i,j;
304 i=0;
305 j=0;
306 while( tmp && tmp[i] ) {
307 char *name = g_ascii_strdown(tmp[i], -1);
308 if (*name != '\0') {
309 if (screen_lookup_name(name) == NULL) {
310 /* an unknown screen name was specified in the
311 configuration file */
312 print_error(_("Unknown screen name"), name);
313 free(name);
314 } else {
315 screen = g_realloc(screen, (j+2)*sizeof(char *));
316 screen[j++] = name;
317 screen[j] = NULL;
318 }
319 }
320 i++;
321 }
322 g_strfreev(tmp);
323 if( screen == NULL )
324 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
326 return screen;
327 }
329 static int
330 get_search_mode(char *value)
331 {
332 char * test;
333 int mode;
334 mode= strtol(value, &test, 10);
335 if (*test == '\0')
336 {
337 if (0 <= mode && mode <= 4)
338 return mode;
339 else
340 {
341 print_error(_("Invalid search mode"),value);
342 return 0;
343 }
344 }
345 else
346 {
347 for (int i = 0; value[i] != '\0'; i++)
348 value[i] = tolower(value[i]);
350 // TODO: modify screen_search so that its own list of modes can be used
351 // for comparison instead of specifying them here
352 if (strcmp(value, "title") == 0)
353 return 0;
354 else if (strcmp(value, "artist") == 0)
355 return 1;
356 else if (strcmp(value, "album") == 0)
357 return 2;
358 else if (strcmp(value, "filename") == 0)
359 return 3;
360 else if (strcmp(value, "artist+album") == 0)
361 return 4;
362 else
363 {
364 print_error(_("Unknown search mode"),value);
365 return 0;
366 }
367 }
368 }
370 static bool
371 parse_line(char *line)
372 {
373 size_t len = strlen(line), i = 0, j;
374 char name[MAX_LINE_LENGTH];
375 char value[MAX_LINE_LENGTH];
376 bool match_found;
378 /* get the name part */
379 j = 0;
380 while (i < len && line[i] != '=' &&
381 !g_ascii_isspace(line[i])) {
382 name[j++] = line[i++];
383 }
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 g_free(options.timedisplay_type);
435 options.timedisplay_type=g_strdup(parse_timedisplay_type(value));
436 /* color definition */
437 } else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
438 #ifdef ENABLE_COLORS
439 parse_color_definition(value);
440 #else
441 {}
442 #endif
443 /* list format string */
444 else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
445 g_free(options.list_format);
446 options.list_format = get_format(value);
447 /* status format string */
448 } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
449 g_free(options.status_format);
450 options.status_format = get_format(value);
451 /* xterm title format string */
452 } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
453 g_free(options.xterm_title_format);
454 options.xterm_title_format = get_format(value);
455 } else if (!strcasecmp(CONF_LIST_WRAP, name))
456 options.list_wrap = str2bool(value);
457 else if (!strcasecmp(CONF_FIND_WRAP, name))
458 options.find_wrap = str2bool(value);
459 else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
460 options.find_show_last_pattern = str2bool(value);
461 else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
462 options.audible_bell = str2bool(value);
463 else if (!strcasecmp(CONF_VISIBLE_BELL, name))
464 options.visible_bell = str2bool(value);
465 else if (!strcasecmp(CONF_BELL_ON_WRAP, name))
466 options.bell_on_wrap = str2bool(value);
467 else if (!strcasecmp(CONF_STATUS_MESSAGE_TIME, name))
468 options.status_message_time = atoi(value);
469 else if (!strcasecmp(CONF_XTERM_TITLE, name))
470 options.enable_xterm_title = str2bool(value);
471 else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
472 #ifdef HAVE_GETMOUSE
473 options.enable_mouse = str2bool(value);
474 #else
475 {}
476 #endif
477 else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
478 options.crossfade_time = atoi(value);
479 else if (!strcasecmp(CONF_SEARCH_MODE, name))
480 options.search_mode = get_search_mode(value);
481 else if (!strcasecmp(CONF_HIDE_CURSOR, name))
482 options.hide_cursor = atoi(value);
483 else if (!strcasecmp(CONF_SEEK_TIME, name))
484 options.seek_time = atoi(value);
485 else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
486 g_strfreev(options.screen_list);
487 options.screen_list = check_screen_list(value);
488 } else if (!strcasecmp(CONF_SHOW_SPLASH, name)) {
489 /* the splash screen was removed */
490 } else if (!strcasecmp(CONF_HOST, name))
491 options.host = get_format(value);
492 else if (!strcasecmp(CONF_PORT, name))
493 options.port = atoi(get_format(value));
494 else if (!strcasecmp(CONF_PASSWORD, name))
495 options.password = get_format(value);
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
526 match_found = false;
528 if (!match_found)
529 print_error(_("Unknown configuration parameter"),
530 name);
532 return match_found;
533 }
535 static int
536 read_rc_file(char *filename)
537 {
538 FILE *file;
539 char line[MAX_LINE_LENGTH];
541 if (filename == NULL)
542 return -1;
544 file = fopen(filename, "r");
545 if (file == NULL) {
546 perror(filename);
547 return -1;
548 }
550 while (fgets(line, sizeof(line), file) != NULL) {
551 char *p = g_strchug(line);
553 if (*p != 0 && *p != COMMENT_TOKEN)
554 parse_line(g_strchomp(p));
555 }
557 fclose(file);
558 return 0;
559 }
561 int
562 check_user_conf_dir(void)
563 {
564 int retval;
565 char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
567 if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
568 g_free(directory);
569 return 0;
570 }
572 retval = mkdir(directory, 0755);
573 g_free(directory);
574 return retval;
575 }
577 char *
578 get_user_key_binding_filename(void)
579 {
580 return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
581 }
583 int
584 read_configuration(void)
585 {
586 char *filename = NULL;
588 /* check for command line configuration file */
589 if (options.config_file)
590 filename = g_strdup(options.config_file);
592 /* check for user configuration ~/.ncmpc/config */
593 if (filename == NULL) {
594 filename = g_build_filename(g_get_home_dir(),
595 "." PACKAGE, "config", NULL);
596 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
597 g_free(filename);
598 filename = NULL;
599 }
600 }
602 /* check for global configuration SYSCONFDIR/ncmpc/config */
603 if (filename == NULL) {
604 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
605 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
606 g_free(filename);
607 filename = NULL;
608 }
609 }
611 /* load configuration */
612 if (filename) {
613 read_rc_file(filename);
614 g_free(filename);
615 filename = NULL;
616 }
618 /* check for command line key binding file */
619 if (options.key_file)
620 filename = g_strdup(options.key_file);
622 /* check for user key bindings ~/.ncmpc/keys */
623 if (filename == NULL) {
624 filename = get_user_key_binding_filename();
625 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
626 g_free(filename);
627 filename = NULL;
628 }
629 }
631 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
632 if (filename == NULL) {
633 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
634 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
635 g_free(filename);
636 filename = NULL;
637 }
638 }
640 /* load key bindings */
641 if (filename) {
642 read_rc_file(filename);
643 g_free(filename);
644 filename = NULL;
645 }
647 return 0;
648 }