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_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_LYRICS_TIMEOUT "lyrics-timeout"
71 #define CONF_SHOW_SPLASH "show-splash"
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"
79 static bool
80 str2bool(char *str)
81 {
82 return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
83 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
84 }
86 static void
87 print_error(const char *msg, const char *input)
88 {
89 fprintf(stderr, "%s: %s ('%s')\n",
90 /* To translators: prefix for error messages */
91 _("Error"), msg, input);
92 }
94 static int
95 parse_key_value(char *str, char **end)
96 {
97 if (*str == '\'') {
98 if (str[1] == '\'' || str[2] != '\'') {
99 print_error(_("Malformed hotkey definition"), str);
100 return -1;
101 }
103 *end = str + 3;
104 return str[1];
105 } else {
106 long value = strtol(str, end, 0);
107 if (*end == str) {
108 print_error(_("Malformed hotkey definition"), str);
109 return -1;
110 }
112 return (int)value;
113 }
114 }
116 static int
117 parse_key_definition(char *str)
118 {
119 char buf[MAX_LINE_LENGTH];
120 char *p;
121 size_t len = strlen(str), i;
122 int j,key;
123 int keys[MAX_COMMAND_KEYS];
124 command_t cmd;
126 /* get the command name */
127 i=0;
128 j=0;
129 memset(buf, 0, MAX_LINE_LENGTH);
130 while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
131 buf[j++] = str[i++];
132 if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
133 /* the hotkey configuration contains an unknown
134 command */
135 print_error(_("Unknown command"), buf);
136 return -1;
137 }
139 /* skip whitespace */
140 while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
141 i++;
143 /* get the value part */
144 memset(buf, 0, MAX_LINE_LENGTH);
145 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
146 if (*buf == 0) {
147 /* the hotkey configuration line is incomplete */
148 print_error(_("Incomplete hotkey configuration"), str);
149 return -1;
150 }
152 /* parse key values */
153 i = 0;
154 key = 0;
155 p = buf;
156 memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
157 while (i < MAX_COMMAND_KEYS && *p != 0 &&
158 (key = parse_key_value(p, &p)) >= 0) {
159 keys[i++] = key;
160 while (*p==',' || *p==' ' || *p=='\t')
161 p++;
162 }
164 if (key < 0)
165 return -1;
167 return assign_keys(cmd, keys);
168 }
170 static const char *
171 parse_timedisplay_type(const char *str)
172 {
173 if (!strcmp(str,"elapsed") || !strcmp(str,"remaining"))
174 return str;
175 else {
176 /* translators: ncmpc supports displaying the
177 "elapsed" or "remaining" time of a song being
178 played; in this case, the configuration file
179 contained an invalid setting */
180 print_error(_("Bad time display type"), str);
181 return DEFAULT_TIMEDISPLAY_TYPE;
182 }
183 }
185 #ifdef ENABLE_COLORS
186 static char *
187 separate_value(char *p)
188 {
189 char *value;
191 value = strchr(p, '=');
192 if (value == NULL) {
193 /* an equals sign '=' was expected while parsing a
194 configuration file line */
195 fprintf(stderr, "%s\n", _("Missing '='"));
196 return NULL;
197 }
199 *value++ = 0;
201 g_strchomp(p);
202 return g_strchug(value);
203 }
205 static int
206 parse_color(char *str)
207 {
208 char *value;
210 value = separate_value(str);
211 if (value == NULL)
212 return -1;
214 return colors_assign(str, value);
215 }
217 /**
218 * Returns the first non-whitespace character after the next comma
219 * character, or the end of the string. This is used to parse comma
220 * separated values.
221 */
222 static char *
223 after_comma(char *p)
224 {
225 char *comma = strchr(p, ',');
227 if (comma != NULL) {
228 *comma++ = 0;
229 comma = g_strchug(comma);
230 } else
231 comma = p + strlen(p);
233 g_strchomp(p);
234 return comma;
235 }
237 static int
238 parse_color_definition(char *str)
239 {
240 char buf[MAX_LINE_LENGTH];
241 char *value;
242 short color, rgb[3];
244 value = separate_value(str);
245 if (value == NULL)
246 return -1;
248 /* get the command name */
249 color = colors_str2color(str);
250 if (color < 0) {
251 print_error(_("Bad color name"), buf);
252 return -1;
253 }
255 /* parse r,g,b values */
257 for (unsigned i = 0; i < 3; ++i) {
258 char *next = after_comma(value), *endptr;
259 if (*value == 0) {
260 print_error(_("Incomplete color definition"), str);
261 return -1;
262 }
264 rgb[i] = strtol(value, &endptr, 0);
265 if (endptr == value || *endptr != 0) {
266 print_error(_("Invalid number"), value);
267 return -1;
268 }
270 value = next;
271 }
273 if (*value != 0) {
274 print_error(_("Malformed color definition"), str);
275 return -1;
276 }
278 return colors_define(str, rgb[0], rgb[1], rgb[2]);
279 }
280 #endif
282 static char *
283 get_format(char *str)
284 {
285 gsize len = strlen(str);
287 if (str && str[0]=='\"' && str[len-1] == '\"') {
288 str[len - 1] = '\0';
289 str++;
290 }
292 return g_strdup(str);
293 }
295 static char **
296 check_screen_list(char *value)
297 {
298 char **tmp = g_strsplit_set(value, " \t,", 100);
299 char **screen = NULL;
300 int i,j;
302 i=0;
303 j=0;
304 while( tmp && tmp[i] ) {
305 char *name = g_ascii_strdown(tmp[i], -1);
306 if (*name != '\0') {
307 if (screen_lookup_name(name) == NULL) {
308 /* an unknown screen name was specified in the
309 configuration file */
310 print_error(_("Unknown screen name"), name);
311 free(name);
312 } else {
313 screen = g_realloc(screen, (j+2)*sizeof(char *));
314 screen[j++] = name;
315 screen[j] = NULL;
316 }
317 }
318 i++;
319 }
320 g_strfreev(tmp);
321 if( screen == NULL )
322 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
324 return screen;
325 }
327 static int
328 get_search_mode(char *value)
329 {
330 char * test;
331 int mode;
332 mode= strtol(value, &test, 10);
333 if (*test == '\0')
334 {
335 if (0 <= mode && mode <= 4)
336 return mode;
337 else
338 {
339 print_error(_("Invalid search mode"),value);
340 return 0;
341 }
342 }
343 else
344 {
345 for (int i = 0; value[i] != '\0'; i++)
346 value[i] = tolower(value[i]);
348 // TODO: modify screen_search so that its own list of modes can be used
349 // for comparison instead of specifying them here
350 if (strcmp(value, "title") == 0)
351 return 0;
352 else if (strcmp(value, "artist") == 0)
353 return 1;
354 else if (strcmp(value, "album") == 0)
355 return 2;
356 else if (strcmp(value, "filename") == 0)
357 return 3;
358 else if (strcmp(value, "artist+album") == 0)
359 return 4;
360 else
361 {
362 print_error(_("Unknown search mode"),value);
363 return 0;
364 }
365 }
366 }
368 static bool
369 parse_line(char *line)
370 {
371 size_t len = strlen(line), i = 0, j;
372 char name[MAX_LINE_LENGTH];
373 char value[MAX_LINE_LENGTH];
374 bool match_found;
376 /* get the name part */
377 j = 0;
378 while (i < len && line[i] != '=' &&
379 !g_ascii_isspace(line[i])) {
380 name[j++] = line[i++];
381 }
383 name[j] = '\0';
385 /* skip '=' and whitespace */
386 while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
387 i++;
389 /* get the value part */
390 j = 0;
391 while (i < len)
392 value[j++] = line[i++];
393 value[j] = '\0';
395 match_found = true;
397 /* key definition */
398 if (!strcasecmp(CONF_KEY_DEFINITION, name))
399 parse_key_definition(value);
400 /* enable colors */
401 else if(!strcasecmp(CONF_ENABLE_COLORS, name))
402 #ifdef ENABLE_COLORS
403 options.enable_colors = str2bool(value);
404 #else
405 {}
406 #endif
407 /* auto center */
408 else if (!strcasecmp(CONF_AUTO_CENTER, name))
409 options.auto_center = str2bool(value);
410 /* color assignment */
411 else if (!strcasecmp(CONF_COLOR, name))
412 #ifdef ENABLE_COLORS
413 parse_color(value);
414 #else
415 {}
416 #endif
417 /* wide cursor */
418 else if (!strcasecmp(CONF_WIDE_CURSOR, name))
419 options.wide_cursor = str2bool(value);
420 /* welcome screen list */
421 else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
422 options.welcome_screen_list = str2bool(value);
423 /* visible bitrate */
424 else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
425 options.visible_bitrate = str2bool(value);
426 /* timer display type */
427 else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name)) {
428 g_free(options.timedisplay_type);
429 options.timedisplay_type=g_strdup(parse_timedisplay_type(value));
430 /* color definition */
431 } else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
432 #ifdef ENABLE_COLORS
433 parse_color_definition(value);
434 #else
435 {}
436 #endif
437 /* list format string */
438 else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
439 g_free(options.list_format);
440 options.list_format = get_format(value);
441 /* status format string */
442 } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
443 g_free(options.status_format);
444 options.status_format = get_format(value);
445 /* xterm title format string */
446 } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
447 g_free(options.xterm_title_format);
448 options.xterm_title_format = get_format(value);
449 } else if (!strcasecmp(CONF_LIST_WRAP, name))
450 options.list_wrap = str2bool(value);
451 else if (!strcasecmp(CONF_FIND_WRAP, name))
452 options.find_wrap = str2bool(value);
453 else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
454 options.find_show_last_pattern = str2bool(value);
455 else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
456 options.audible_bell = str2bool(value);
457 else if (!strcasecmp(CONF_VISIBLE_BELL, name))
458 options.visible_bell = str2bool(value);
459 else if (!strcasecmp(CONF_BELL_ON_WRAP, name))
460 options.bell_on_wrap = str2bool(value);
461 else if (!strcasecmp(CONF_STATUS_MESSAGE_TIME, name))
462 options.status_message_time = atoi(value);
463 else if (!strcasecmp(CONF_XTERM_TITLE, name))
464 options.enable_xterm_title = str2bool(value);
465 else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
466 #ifdef HAVE_GETMOUSE
467 options.enable_mouse = str2bool(value);
468 #else
469 {}
470 #endif
471 else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
472 options.crossfade_time = atoi(value);
473 else if (!strcasecmp(CONF_SEARCH_MODE, name))
474 options.search_mode = get_search_mode(value);
475 else if (!strcasecmp(CONF_HIDE_CURSOR, name))
476 options.hide_cursor = atoi(value);
477 else if (!strcasecmp(CONF_SEEK_TIME, name))
478 options.seek_time = atoi(value);
479 else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
480 g_strfreev(options.screen_list);
481 options.screen_list = check_screen_list(value);
482 } else if (!strcasecmp(CONF_SHOW_SPLASH, name)) {
483 /* the splash screen was removed */
484 } else if (!strcasecmp(CONF_HOST, name))
485 options.host = get_format(value);
486 else if (!strcasecmp(CONF_PORT, name))
487 options.port = atoi(get_format(value));
488 else if (!strcasecmp(CONF_PASSWORD, name))
489 options.password = get_format(value);
490 else if (!strcasecmp(CONF_LYRICS_TIMEOUT, name))
491 #ifdef ENABLE_LYRICS_SCREEN
492 options.lyrics_timeout = atoi(get_format(value));
493 #else
494 {}
495 #endif
496 else if (!strcasecmp(CONF_SCROLL, name))
497 options.scroll = str2bool(value);
498 else if (!strcasecmp(CONF_SCROLL_SEP, name)) {
499 g_free(options.scroll_sep);
500 options.scroll_sep = get_format(value);
501 } else if (!strcasecmp(CONF_DISPLAY_TIME, name))
502 #ifdef NCMPC_MINI
503 {}
504 #else
505 options.display_time = str2bool(value);
506 #endif
507 else if (!strcasecmp(CONF_JUMP_PREFIX_ONLY, name))
508 #ifdef NCMPC_MINI
509 {}
510 #else
511 options.jump_prefix_only = str2bool(value);
512 #endif
513 else
514 match_found = false;
516 if (!match_found)
517 print_error(_("Unknown configuration parameter"),
518 name);
520 return match_found;
521 }
523 static int
524 read_rc_file(char *filename)
525 {
526 FILE *file;
527 char line[MAX_LINE_LENGTH];
529 if (filename == NULL)
530 return -1;
532 file = fopen(filename, "r");
533 if (file == NULL) {
534 perror(filename);
535 return -1;
536 }
538 while (fgets(line, sizeof(line), file) != NULL) {
539 char *p = g_strchug(line);
541 if (*p != 0 && *p != COMMENT_TOKEN)
542 parse_line(g_strchomp(p));
543 }
545 fclose(file);
546 return 0;
547 }
549 int
550 check_user_conf_dir(void)
551 {
552 int retval;
553 char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
555 if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
556 g_free(directory);
557 return 0;
558 }
560 retval = mkdir(directory, 0755);
561 g_free(directory);
562 return retval;
563 }
565 char *
566 get_user_key_binding_filename(void)
567 {
568 return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
569 }
571 int
572 read_configuration(void)
573 {
574 char *filename = NULL;
576 /* check for command line configuration file */
577 if (options.config_file)
578 filename = g_strdup(options.config_file);
580 /* check for user configuration ~/.ncmpc/config */
581 if (filename == NULL) {
582 filename = g_build_filename(g_get_home_dir(),
583 "." PACKAGE, "config", NULL);
584 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
585 g_free(filename);
586 filename = NULL;
587 }
588 }
590 /* check for global configuration SYSCONFDIR/ncmpc/config */
591 if (filename == NULL) {
592 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
593 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
594 g_free(filename);
595 filename = NULL;
596 }
597 }
599 /* load configuration */
600 if (filename) {
601 read_rc_file(filename);
602 g_free(filename);
603 filename = NULL;
604 }
606 /* check for command line key binding file */
607 if (options.key_file)
608 filename = g_strdup(options.key_file);
610 /* check for user key bindings ~/.ncmpc/keys */
611 if (filename == NULL) {
612 filename = get_user_key_binding_filename();
613 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
614 g_free(filename);
615 filename = NULL;
616 }
617 }
619 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
620 if (filename == NULL) {
621 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
622 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
623 g_free(filename);
624 filename = NULL;
625 }
626 }
628 /* load key bindings */
629 if (filename) {
630 read_rc_file(filename);
631 g_free(filename);
632 filename = NULL;
633 }
635 return 0;
636 }