Code

03ba80dd7407baa7ef78bc898e57afd785974b89
[ncmpc.git] / src / conf.c
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 <assert.h>
29 #include <ctype.h>
30 #include <stdio.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <glib.h>
37 #include <glib/gstdio.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_SEARCH_FORMAT "search-format"
52 #define CONF_STATUS_FORMAT "status-format"
53 #define CONF_XTERM_TITLE_FORMAT "xterm-title-format"
54 #define CONF_LIST_WRAP "wrap-around"
55 #define CONF_FIND_WRAP "find-wrap"
56 #define CONF_FIND_SHOW_LAST "find-show-last"
57 #define CONF_AUDIBLE_BELL "audible-bell"
58 #define CONF_VISIBLE_BELL "visible-bell"
59 #define CONF_BELL_ON_WRAP "bell-on-wrap"
60 #define CONF_STATUS_MESSAGE_TIME "status-message-time"
61 #define CONF_XTERM_TITLE "set-xterm-title"
62 #define CONF_ENABLE_MOUSE "enable-mouse"
63 #define CONF_CROSSFADE_TIME "crossfade-time"
64 #define CONF_SEARCH_MODE "search-mode"
65 #define CONF_HIDE_CURSOR "hide-cursor"
66 #define CONF_SEEK_TIME "seek-time"
67 #define CONF_SCREEN_LIST "screen-list"
68 #define CONF_TIMEDISPLAY_TYPE "timedisplay-type"
69 #define CONF_HOST "host"
70 #define CONF_PORT "port"
71 #define CONF_PASSWORD "password"
72 #define CONF_TIMEOUT "timeout"
73 #define CONF_LYRICS_TIMEOUT "lyrics-timeout"
74 #define CONF_SCROLL "scroll"
75 #define CONF_SCROLL_SEP "scroll-sep"
76 #define CONF_VISIBLE_BITRATE "visible-bitrate"
77 #define CONF_HARDWARE_CURSOR "hardware-cursor"
78 #define CONF_WELCOME_SCREEN_LIST "welcome-screen-list"
79 #define CONF_DISPLAY_TIME "display-time"
80 #define CONF_JUMP_PREFIX_ONLY "jump-prefix-only"
81 #define CONF_LYRICS_AUTOSAVE "lyrics-autosave"
82 #define CONF_LYRICS_SHOW_PLUGIN "lyrics-show-plugin"
83 #define CONF_TEXT_EDITOR "text-editor"
84 #define CONF_TEXT_EDITOR_ASK "text-editor-ask"
85 #define CONF_CHAT_PREFIX "chat-prefix"
86 #define CONF_SECOND_COLUMN "second-column"
88 static bool
89 str2bool(char *str)
90 {
91         return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
92                 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
93 }
95 static void
96 print_error(const char *msg, const char *input)
97 {
98         fprintf(stderr, "%s: %s ('%s')\n",
99                 /* To translators: prefix for error messages */
100                 _("Error"), msg, input);
103 static int
104 parse_key_value(char *str, char **end)
106         if (*str == '\'') {
107                 if (str[1] == '\'' || str[2] != '\'') {
108                         print_error(_("Malformed hotkey definition"), str);
109                         return -1;
110                 }
112                 *end = str + 3;
113                 return str[1];
114         } else {
115                 long value = strtol(str, end, 0);
116                 if (*end == str) {
117                         print_error(_("Malformed hotkey definition"), str);
118                         return -1;
119                 }
121                 return (int)value;
122         }
125 static bool
126 parse_key_definition(char *str)
128         /* get the command name */
129         const size_t len = strlen(str);
130         size_t i = 0;
131         int j = 0;
132         char buf[MAX_LINE_LENGTH];
133         memset(buf, 0, MAX_LINE_LENGTH);
134         while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
135                 buf[j++] = str[i++];
137         command_t cmd = get_key_command_from_name(buf);
138         if(cmd == CMD_NONE) {
139                 /* the hotkey configuration contains an unknown
140                    command */
141                 print_error(_("Unknown command"), buf);
142                 return false;
143         }
145         /* skip whitespace */
146         while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
147                 i++;
149         /* get the value part */
150         memset(buf, 0, MAX_LINE_LENGTH);
151         g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
152         if (*buf == 0) {
153                 /* the hotkey configuration line is incomplete */
154                 print_error(_("Incomplete hotkey configuration"), str);
155                 return false;
156         }
158         /* parse key values */
159         i = 0;
160         int key = 0;
161         char *p = buf;
163         int keys[MAX_COMMAND_KEYS];
164         memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
165         while (i < MAX_COMMAND_KEYS && *p != 0 &&
166                (key = parse_key_value(p, &p)) >= 0) {
167                 keys[i++] = key;
168                 while (*p==',' || *p==' ' || *p=='\t')
169                         p++;
170         }
172         if (key < 0)
173                 return false;
175         return assign_keys(cmd, keys);
178 static bool
179 parse_timedisplay_type(const char *str)
181         if (strcmp(str, "elapsed") == 0)
182                 return false;
183         else if (strcmp(str, "remaining") == 0)
184                 return true;
185         else {
186                 /* translators: ncmpc supports displaying the
187                    "elapsed" or "remaining" time of a song being
188                    played; in this case, the configuration file
189                    contained an invalid setting */
190                 print_error(_("Bad time display type"), str);
191                 return false;
192         }
195 #ifdef ENABLE_COLORS
196 static char *
197 separate_value(char *p)
199         char *value = strchr(p, '=');
200         if (value == NULL) {
201                 /* an equals sign '=' was expected while parsing a
202                    configuration file line */
203                 fprintf(stderr, "%s\n", _("Missing '='"));
204                 return NULL;
205         }
207         *value++ = 0;
209         g_strchomp(p);
210         return g_strchug(value);
213 static int
214 parse_color(char *str)
216         char *value = separate_value(str);
217         if (value == NULL)
218                 return -1;
220         return colors_assign(str, value);
223 /**
224  * Returns the first non-whitespace character after the next comma
225  * character, or the end of the string.  This is used to parse comma
226  * separated values.
227  */
228 static char *
229 after_comma(char *p)
231         char *comma = strchr(p, ',');
233         if (comma != NULL) {
234                 *comma++ = 0;
235                 comma = g_strchug(comma);
236         } else
237                 comma = p + strlen(p);
239         g_strchomp(p);
240         return comma;
243 static int
244 parse_color_definition(char *str)
246         char *value = separate_value(str);
247         if (value == NULL)
248                 return -1;
250         /* get the command name */
251         short color = colors_str2color(str);
252         if (color < 0) {
253                 char buf[MAX_LINE_LENGTH];
254                 print_error(_("Bad color name"), buf);
255                 return -1;
256         }
258         /* parse r,g,b values */
260         short rgb[3];
261         for (unsigned i = 0; i < 3; ++i) {
262                 char *next = after_comma(value), *endptr;
263                 if (*value == 0) {
264                         print_error(_("Incomplete color definition"), str);
265                         return -1;
266                 }
268                 rgb[i] = strtol(value, &endptr, 0);
269                 if (endptr == value || *endptr != 0) {
270                         print_error(_("Invalid number"), value);
271                         return -1;
272                 }
274                 value = next;
275         }
277         if (*value != 0) {
278                 print_error(_("Malformed color definition"), str);
279                 return -1;
280         }
282         return colors_define(str, rgb[0], rgb[1], rgb[2]);
284 #endif
286 static char *
287 get_format(char *str)
289         gsize len = strlen(str);
291         if (str && str[0]=='\"' && str[len-1] == '\"') {
292                 str[len - 1] = '\0';
293                 str++;
294         }
296         return g_strdup(str);
299 static char **
300 check_screen_list(char *value)
302         char **tmp = g_strsplit_set(value, " \t,", 100);
303         char **screen = NULL;
304         int i = 0, 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;
329 static int
330 get_search_mode(char *value)
332         char * test;
333         const int mode = strtol(value, &test, 10);
334         if (*test == '\0')
335         {
336                 if (0 <= mode && mode <= 4)
337                         return mode;
338                 else
339                 {
340                         print_error(_("Invalid search mode"),value);
341                         return 0;
342                 }
343         }
344         else
345         {
346                 for (int i = 0; value[i] != '\0'; i++)
347                         value[i] = tolower(value[i]);
349                 // TODO: modify screen_search so that its own list of modes can be used
350                 // for comparison instead of specifying them here
351                 if (strcmp(value, "title") == 0)
352                         return 0;
353                 else if (strcmp(value, "artist") == 0)
354                         return 1;
355                 else if (strcmp(value, "album") == 0)
356                         return 2;
357                 else if (strcmp(value, "filename") == 0)
358                         return 3;
359                 else if (strcmp(value, "artist+album") == 0)
360                         return 4;
361                 else
362                 {
363                         print_error(_("Unknown search mode"),value);
364                         return 0;
365                 }
366         }
369 static bool
370 parse_line(char *line)
372         size_t len = strlen(line), i = 0, j = 0;
374         /* get the name part */
375         char name[MAX_LINE_LENGTH];
376         while (i < len && line[i] != '=' && !g_ascii_isspace(line[i]))
377                 name[j++] = line[i++];
379         name[j] = '\0';
381         /* skip '=' and whitespace */
382         while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
383                 i++;
385         /* get the value part */
386         char value[MAX_LINE_LENGTH];
387         j = 0;
388         while (i < len)
389                 value[j++] = line[i++];
390         value[j] = '\0';
392         /* key definition */
393         if (!strcasecmp(CONF_KEY_DEFINITION, name))
394                 parse_key_definition(value);
395         /* enable colors */
396         else if(!strcasecmp(CONF_ENABLE_COLORS, name))
397 #ifdef ENABLE_COLORS
398                 options.enable_colors = str2bool(value);
399 #else
400         {}
401 #endif
402         else if (!strcasecmp(CONF_SCROLL_OFFSET, name))
403                 options.scroll_offset = atoi(value);
404         /* auto center */
405         else if (!strcasecmp(CONF_AUTO_CENTER, name))
406                 options.auto_center = str2bool(value);
407         /* color assignment */
408         else if (!strcasecmp(CONF_COLOR, name))
409 #ifdef ENABLE_COLORS
410                 parse_color(value);
411 #else
412         {}
413 #endif
414         /* wide cursor */
415         else if (!strcasecmp(CONF_WIDE_CURSOR, name))
416                 options.wide_cursor = str2bool(value);
417         else if (strcasecmp(name, CONF_HARDWARE_CURSOR) == 0)
418                 options.hardware_cursor = str2bool(value);
419         /* welcome screen list */
420         else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
421                 options.welcome_screen_list = str2bool(value);
422         /* visible bitrate */
423         else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
424                 options.visible_bitrate = str2bool(value);
425         /* timer display type */
426         else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name))
427                 options.display_remaining_time = parse_timedisplay_type(value);
428                 /* color definition */
429         else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
430 #ifdef ENABLE_COLORS
431                 parse_color_definition(value);
432 #else
433         {}
434 #endif
435         /* list format string */
436         else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
437                 g_free(options.list_format);
438                 options.list_format = get_format(value);
439                 /* search format string */
440         } else if (!strcasecmp(CONF_SEARCH_FORMAT, name)) {
441                 g_free(options.search_format);
442                 options.search_format = get_format(value);
443                 /* status format string */
444         } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
445                 g_free(options.status_format);
446                 options.status_format = get_format(value);
447                 /* xterm title format string */
448         } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
449                 g_free(options.xterm_title_format);
450                 options.xterm_title_format = get_format(value);
451         } else if (!strcasecmp(CONF_LIST_WRAP, name))
452                 options.list_wrap = str2bool(value);
453         else if (!strcasecmp(CONF_FIND_WRAP, name))
454                 options.find_wrap = str2bool(value);
455         else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
456                 options.find_show_last_pattern = str2bool(value);
457         else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
458                 options.audible_bell = str2bool(value);
459         else if (!strcasecmp(CONF_VISIBLE_BELL, name))
460                 options.visible_bell = str2bool(value);
461         else if (!strcasecmp(CONF_BELL_ON_WRAP, name))
462                 options.bell_on_wrap = str2bool(value);
463         else if (!strcasecmp(CONF_STATUS_MESSAGE_TIME, name))
464                 options.status_message_time = atoi(value);
465         else if (!strcasecmp(CONF_XTERM_TITLE, name))
466                 options.enable_xterm_title = str2bool(value);
467         else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
468 #ifdef HAVE_GETMOUSE
469                 options.enable_mouse = str2bool(value);
470 #else
471         {}
472 #endif
473         else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
474                 options.crossfade_time = atoi(value);
475         else if (!strcasecmp(CONF_SEARCH_MODE, name))
476                 options.search_mode = get_search_mode(value);
477         else if (!strcasecmp(CONF_HIDE_CURSOR, name))
478                 options.hide_cursor = atoi(value);
479         else if (!strcasecmp(CONF_SEEK_TIME, name))
480                 options.seek_time = atoi(value);
481         else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
482                 g_strfreev(options.screen_list);
483                 options.screen_list = check_screen_list(value);
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_TIMEOUT, name))
491                 options.timeout_ms = atoi(get_format(value))
492                                      * 1000 /* seconds -> milliseconds */;
493         else if (!strcasecmp(CONF_LYRICS_TIMEOUT, name))
494 #ifdef ENABLE_LYRICS_SCREEN
495                 options.lyrics_timeout = atoi(get_format(value));
496 #else
497         {}
498 #endif
499         else if (!strcasecmp(CONF_SCROLL, name))
500                 options.scroll = str2bool(value);
501         else if (!strcasecmp(CONF_SCROLL_SEP, name)) {
502                 g_free(options.scroll_sep);
503                 options.scroll_sep = get_format(value);
504         } else if (!strcasecmp(CONF_DISPLAY_TIME, name))
505 #ifdef NCMPC_MINI
506                 {}
507 #else
508                 options.display_time = str2bool(value);
509 #endif
510         else if (!strcasecmp(CONF_JUMP_PREFIX_ONLY, name))
511 #ifdef NCMPC_MINI
512                 {}
513 #else
514                 options.jump_prefix_only = str2bool(value);
515 #endif
516         else if (!strcasecmp(CONF_LYRICS_AUTOSAVE, name))
517 #ifdef ENABLE_LYRICS_SCREEN
518                 options.lyrics_autosave = str2bool(value);
519 #else
520         {}
521 #endif
522         else if (!strcasecmp(CONF_LYRICS_SHOW_PLUGIN, name))
523 #ifdef ENABLE_LYRICS_SCREEN
524                 options.lyrics_show_plugin = str2bool(value);
525 #else
526                 {}
527 #endif
528         else if (!strcasecmp(name, CONF_TEXT_EDITOR))
529 #ifdef ENABLE_LYRICS_SCREEN
530                 {
531                         g_free(options.text_editor);
532                         options.text_editor = get_format(value);
533                 }
534 #else
535                 {}
536 #endif
537         else if (!strcasecmp(name, CONF_TEXT_EDITOR_ASK))
538 #ifdef ENABLE_LYRICS_SCREEN
539                 options.text_editor_ask = str2bool(value);
540 #else
541                 {}
542 #endif
543         else if (!strcasecmp(name, CONF_CHAT_PREFIX))
544 #ifdef ENABLE_CHAT_SCREEN
545                 options.chat_prefix = get_format(value);
546 #else
547                 {}
548 #endif
549         else if (!strcasecmp(CONF_SECOND_COLUMN, name))
550 #ifdef NCMPC_MINI
551                 {}
552 #else
553                 options.second_column = str2bool(value);
554 #endif
555         else {
556                 print_error(_("Unknown configuration parameter"), name);
557                 return false;
558         }
560         return true;
563 static int
564 read_rc_file(char *filename)
566         assert(filename != NULL);
568         FILE *file = fopen(filename, "r");
569         if (file == NULL) {
570                 perror(filename);
571                 return -1;
572         }
574         char line[MAX_LINE_LENGTH];
575         while (fgets(line, sizeof(line), file) != NULL) {
576                 char *p = g_strchug(line);
578                 if (*p != 0 && *p != COMMENT_TOKEN)
579                         parse_line(g_strchomp(p));
580         }
582         fclose(file);
583         return 0;
586 int
587 check_user_conf_dir(void)
589         char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
591         if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
592                 g_free(directory);
593                 return 0;
594         }
596         int retval = g_mkdir(directory, 0755);
597         g_free(directory);
598         return retval;
601 char *
602 build_user_conf_filename(void)
604 #ifdef WIN32
605         return g_build_filename(g_get_user_config_dir(), PACKAGE, "ncmpc.conf", NULL);
606 #else
607         return g_build_filename(g_get_home_dir(), "." PACKAGE, "config", NULL);
608 #endif
611 char *
612 build_system_conf_filename(void)
614 #ifdef WIN32
615         const gchar* const *system_data_dirs;
616         gchar *pathname = NULL;
618         for (system_data_dirs = g_get_system_config_dirs (); *system_data_dirs != NULL; system_data_dirs++)
619         {
620                 g_message (*system_data_dirs);
621                 pathname = g_build_filename(*system_data_dirs, PACKAGE, "ncmpc.conf", NULL);
622                 if (g_file_test(pathname, G_FILE_TEST_EXISTS))
623                 {
624                         break;
625                 }
626                 else
627                 {
628                         g_free (pathname);
629                         pathname = NULL;
630                 }
631         }
632         return pathname;
633 #else
634         return g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
635 #endif
638 char *
639 build_user_key_binding_filename(void)
641 #ifdef WIN32
642         return g_build_filename(g_get_user_config_dir(), PACKAGE, "keys.conf", NULL);
643 #else
644         return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
645 #endif
648 static char *
649 g_build_system_key_binding_filename(void)
651 #ifdef WIN32
652         const gchar* const *system_data_dirs;
653         gchar *pathname = NULL;
655         for (system_data_dirs = g_get_system_config_dirs (); *system_data_dirs != NULL; system_data_dirs++)
656         {
657                 g_message (*system_data_dirs);
658                 pathname = g_build_filename(*system_data_dirs, PACKAGE, "keys.conf", NULL);
659                 if (g_file_test(pathname, G_FILE_TEST_EXISTS))
660                 {
661                         break;
662                 }
663                 else
664                 {
665                         g_free (pathname);
666                         pathname = NULL;
667                 }
668         }
669         return pathname;
670 #else
671         return g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
672 #endif
675 static char *
676 find_config_file(void)
678         /* check for command line configuration file */
679         if (options.config_file != NULL)
680                 return g_strdup(options.config_file);
682         /* check for user configuration ~/.ncmpc/config */
683         char *filename = build_user_conf_filename();
684         if (g_file_test(filename, G_FILE_TEST_IS_REGULAR))
685                 return filename;
687         g_free(filename);
689         /* check for  global configuration SYSCONFDIR/ncmpc/config */
690         filename = build_system_conf_filename();
691         if (g_file_test(filename, G_FILE_TEST_IS_REGULAR))
692                 return filename;
694         g_free(filename);
695         return NULL;
698 static char *
699 find_keys_file(void)
701         /* check for command line key binding file */
702         if (options.key_file != NULL)
703                 return g_strdup(options.key_file);
705         /* check for  user key bindings ~/.ncmpc/keys */
706         char *filename = build_user_key_binding_filename();
707         if (g_file_test(filename, G_FILE_TEST_IS_REGULAR))
708                 return filename;
710         g_free(filename);
712         /* check for  global key bindings SYSCONFDIR/ncmpc/keys */
713         filename = g_build_system_key_binding_filename();
714         if (g_file_test(filename, G_FILE_TEST_IS_REGULAR))
715                 return filename;
717         g_free(filename);
718         return NULL;
721 void
722 read_configuration(void)
724         /* load configuration */
725         char *filename = find_config_file();
726         if (filename != NULL) {
727                 read_rc_file(filename);
728                 g_free(filename);
729         }
731         /* load key bindings */
732         filename = find_keys_file();
733         if (filename != NULL) {
734                 read_rc_file(filename);
735                 g_free(filename);
736         }