Code

added scroll-offset option; fixed scrolling bug
[ncmpc.git] / src / conf.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 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.
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"
80 static bool
81 str2bool(char *str)
82 {
83         return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
84                 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
85 }
87 static void
88 print_error(const char *msg, const char *input)
89 {
90         fprintf(stderr, "%s: %s ('%s')\n",
91                 /* To translators: prefix for error messages */
92                 _("Error"), msg, input);
93 }
95 static int
96 parse_key_value(char *str, char **end)
97 {
98         if (*str == '\'') {
99                 if (str[1] == '\'' || str[2] != '\'') {
100                         print_error(_("Malformed hotkey definition"), str);
101                         return -1;
102                 }
104                 *end = str + 3;
105                 return str[1];
106         } else {
107                 long value = strtol(str, end, 0);
108                 if (*end == str) {
109                         print_error(_("Malformed hotkey definition"), str);
110                         return -1;
111                 }
113                 return (int)value;
114         }
117 static int
118 parse_key_definition(char *str)
120         char buf[MAX_LINE_LENGTH];
121         char *p;
122         size_t len = strlen(str), i;
123         int j,key;
124         int keys[MAX_COMMAND_KEYS];
125         command_t cmd;
127         /* get the command name */
128         i=0;
129         j=0;
130         memset(buf, 0, MAX_LINE_LENGTH);
131         while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
132                 buf[j++] = str[i++];
133         if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
134                 /* the hotkey configuration contains an unknown
135                    command */
136                 print_error(_("Unknown command"), buf);
137                 return -1;
138         }
140         /* skip whitespace */
141         while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
142                 i++;
144         /* get the value part */
145         memset(buf, 0, MAX_LINE_LENGTH);
146         g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
147         if (*buf == 0) {
148                 /* the hotkey configuration line is incomplete */
149                 print_error(_("Incomplete hotkey configuration"), str);
150                 return -1;
151         }
153         /* parse key values */
154         i = 0;
155         key = 0;
156         p = buf;
157         memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
158         while (i < MAX_COMMAND_KEYS && *p != 0 &&
159                (key = parse_key_value(p, &p)) >= 0) {
160                 keys[i++] = key;
161                 while (*p==',' || *p==' ' || *p=='\t')
162                         p++;
163         }
165         if (key < 0)
166                 return -1;
168         return assign_keys(cmd, keys);
171 static const char *
172 parse_timedisplay_type(const char *str)
174         if (!strcmp(str,"elapsed") || !strcmp(str,"remaining"))
175                 return str;
176         else {
177                 /* translators: ncmpc supports displaying the
178                    "elapsed" or "remaining" time of a song being
179                    played; in this case, the configuration file
180                    contained an invalid setting */
181                 print_error(_("Bad time display type"), str);
182                 return DEFAULT_TIMEDISPLAY_TYPE;
183         }
186 #ifdef ENABLE_COLORS
187 static char *
188 separate_value(char *p)
190         char *value;
192         value = strchr(p, '=');
193         if (value == NULL) {
194                 /* an equals sign '=' was expected while parsing a
195                    configuration file line */
196                 fprintf(stderr, "%s\n", _("Missing '='"));
197                 return NULL;
198         }
200         *value++ = 0;
202         g_strchomp(p);
203         return g_strchug(value);
206 static int
207 parse_color(char *str)
209         char *value;
211         value = separate_value(str);
212         if (value == NULL)
213                 return -1;
215         return colors_assign(str, value);
218 /**
219  * Returns the first non-whitespace character after the next comma
220  * character, or the end of the string.  This is used to parse comma
221  * separated values.
222  */
223 static char *
224 after_comma(char *p)
226         char *comma = strchr(p, ',');
228         if (comma != NULL) {
229                 *comma++ = 0;
230                 comma = g_strchug(comma);
231         } else
232                 comma = p + strlen(p);
234         g_strchomp(p);
235         return comma;
238 static int
239 parse_color_definition(char *str)
241         char buf[MAX_LINE_LENGTH];
242         char *value;
243         short color, rgb[3];
245         value = separate_value(str);
246         if (value == NULL)
247                 return -1;
249         /* get the command name */
250         color = colors_str2color(str);
251         if (color < 0) {
252                 print_error(_("Bad color name"), buf);
253                 return -1;
254         }
256         /* parse r,g,b values */
258         for (unsigned i = 0; i < 3; ++i) {
259                 char *next = after_comma(value), *endptr;
260                 if (*value == 0) {
261                         print_error(_("Incomplete color definition"), str);
262                         return -1;
263                 }
265                 rgb[i] = strtol(value, &endptr, 0);
266                 if (endptr == value || *endptr != 0) {
267                         print_error(_("Invalid number"), value);
268                         return -1;
269                 }
271                 value = next;
272         }
274         if (*value != 0) {
275                 print_error(_("Malformed color definition"), str);
276                 return -1;
277         }
279         return colors_define(str, rgb[0], rgb[1], rgb[2]);
281 #endif
283 static char *
284 get_format(char *str)
286         gsize len = strlen(str);
288         if (str && str[0]=='\"' && str[len-1] == '\"') {
289                 str[len - 1] = '\0';
290                 str++;
291         }
293         return g_strdup(str);
296 static char **
297 check_screen_list(char *value)
299         char **tmp = g_strsplit_set(value, " \t,", 100);
300         char **screen = NULL;
301         int i,j;
303         i=0;
304         j=0;
305         while( tmp && tmp[i] ) {
306                 char *name = g_ascii_strdown(tmp[i], -1);
307                 if (*name != '\0') {
308                         if (screen_lookup_name(name) == NULL) {
309                                 /* an unknown screen name was specified in the
310                                    configuration file */
311                                 print_error(_("Unknown screen name"), name);
312                                 free(name);
313                         } else {
314                                 screen = g_realloc(screen, (j+2)*sizeof(char *));
315                                 screen[j++] = name;
316                                 screen[j] = NULL;
317                         }
318                 }
319                 i++;
320         }
321         g_strfreev(tmp);
322         if( screen == NULL )
323                 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
325         return screen;
328 static int
329 get_search_mode(char *value)
331         char * test;
332         int mode;
333         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;
373         char name[MAX_LINE_LENGTH];
374         char value[MAX_LINE_LENGTH];
375         bool match_found;
377         /* get the name part */
378         j = 0;
379         while (i < len && line[i] != '=' &&
380                !g_ascii_isspace(line[i])) {
381                 name[j++] = line[i++];
382         }
384         name[j] = '\0';
386         /* skip '=' and whitespace */
387         while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
388                 i++;
390         /* get the value part */
391         j = 0;
392         while (i < len)
393                 value[j++] = line[i++];
394         value[j] = '\0';
396         match_found = true;
398         /* key definition */
399         if (!strcasecmp(CONF_KEY_DEFINITION, name))
400                 parse_key_definition(value);
401         /* enable colors */
402         else if(!strcasecmp(CONF_ENABLE_COLORS, name))
403 #ifdef ENABLE_COLORS
404                 options.enable_colors = str2bool(value);
405 #else
406         {}
407 #endif
408         else if (!strcasecmp(CONF_SCROLL_OFFSET, name))
409                 options.scroll_offset = atoi(value);
410         /* auto center */
411         else if (!strcasecmp(CONF_AUTO_CENTER, name))
412                 options.auto_center = str2bool(value);
413         /* color assignment */
414         else if (!strcasecmp(CONF_COLOR, name))
415 #ifdef ENABLE_COLORS
416                 parse_color(value);
417 #else
418         {}
419 #endif
420         /* wide cursor */
421         else if (!strcasecmp(CONF_WIDE_CURSOR, name))
422                 options.wide_cursor = str2bool(value);
423         /* welcome screen list */
424         else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
425                 options.welcome_screen_list = str2bool(value);
426         /* visible bitrate */
427         else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
428                 options.visible_bitrate = str2bool(value);
429         /* timer display type */
430         else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name)) {
431                 g_free(options.timedisplay_type);
432                 options.timedisplay_type=g_strdup(parse_timedisplay_type(value));
433                 /* color definition */
434         } else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
435 #ifdef ENABLE_COLORS
436                 parse_color_definition(value);
437 #else
438         {}
439 #endif
440         /* list format string */
441         else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
442                 g_free(options.list_format);
443                 options.list_format = get_format(value);
444                 /* status format string */
445         } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
446                 g_free(options.status_format);
447                 options.status_format = get_format(value);
448                 /* xterm title format string */
449         } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
450                 g_free(options.xterm_title_format);
451                 options.xterm_title_format = get_format(value);
452         } else if (!strcasecmp(CONF_LIST_WRAP, name))
453                 options.list_wrap = str2bool(value);
454         else if (!strcasecmp(CONF_FIND_WRAP, name))
455                 options.find_wrap = str2bool(value);
456         else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
457                 options.find_show_last_pattern = str2bool(value);
458         else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
459                 options.audible_bell = str2bool(value);
460         else if (!strcasecmp(CONF_VISIBLE_BELL, name))
461                 options.visible_bell = str2bool(value);
462         else if (!strcasecmp(CONF_BELL_ON_WRAP, name))
463                 options.bell_on_wrap = str2bool(value);
464         else if (!strcasecmp(CONF_STATUS_MESSAGE_TIME, name))
465                 options.status_message_time = atoi(value);
466         else if (!strcasecmp(CONF_XTERM_TITLE, name))
467                 options.enable_xterm_title = str2bool(value);
468         else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
469 #ifdef HAVE_GETMOUSE
470                 options.enable_mouse = str2bool(value);
471 #else
472         {}
473 #endif
474         else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
475                 options.crossfade_time = atoi(value);
476         else if (!strcasecmp(CONF_SEARCH_MODE, name))
477                 options.search_mode = get_search_mode(value);
478         else if (!strcasecmp(CONF_HIDE_CURSOR, name))
479                 options.hide_cursor = atoi(value);
480         else if (!strcasecmp(CONF_SEEK_TIME, name))
481                 options.seek_time = atoi(value);
482         else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
483                 g_strfreev(options.screen_list);
484                 options.screen_list = check_screen_list(value);
485         } else if (!strcasecmp(CONF_SHOW_SPLASH, name)) {
486                 /* the splash screen was removed */
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_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
517                 match_found = false;
519         if (!match_found)
520                 print_error(_("Unknown configuration parameter"),
521                             name);
523         return match_found;
526 static int
527 read_rc_file(char *filename)
529         FILE *file;
530         char line[MAX_LINE_LENGTH];
532         if (filename == NULL)
533                 return -1;
535         file = fopen(filename, "r");
536         if (file == NULL) {
537                         perror(filename);
538                         return -1;
539                 }
541         while (fgets(line, sizeof(line), file) != NULL) {
542                 char *p = g_strchug(line);
544                 if (*p != 0 && *p != COMMENT_TOKEN)
545                         parse_line(g_strchomp(p));
546         }
548         fclose(file);
549         return 0;
552 int
553 check_user_conf_dir(void)
555         int retval;
556         char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
558         if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
559                 g_free(directory);
560                 return 0;
561         }
563         retval = mkdir(directory, 0755);
564         g_free(directory);
565         return retval;
568 char *
569 get_user_key_binding_filename(void)
571         return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
574 int
575 read_configuration(void)
577         char *filename = NULL;
579         /* check for command line configuration file */
580         if (options.config_file)
581                 filename = g_strdup(options.config_file);
583         /* check for user configuration ~/.ncmpc/config */
584         if (filename == NULL) {
585                 filename = g_build_filename(g_get_home_dir(),
586                                             "." PACKAGE, "config", NULL);
587                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
588                         g_free(filename);
589                         filename = NULL;
590                 }
591         }
593         /* check for  global configuration SYSCONFDIR/ncmpc/config */
594         if (filename == NULL) {
595                 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
596                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
597                         g_free(filename);
598                         filename = NULL;
599                 }
600         }
602         /* load configuration */
603         if (filename) {
604                 read_rc_file(filename);
605                 g_free(filename);
606                 filename = NULL;
607         }
609         /* check for command line key binding file */
610         if (options.key_file)
611                 filename = g_strdup(options.key_file);
613         /* check for  user key bindings ~/.ncmpc/keys */
614         if (filename == NULL) {
615                 filename = get_user_key_binding_filename();
616                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
617                         g_free(filename);
618                         filename = NULL;
619                 }
620         }
622         /* check for  global key bindings SYSCONFDIR/ncmpc/keys */
623         if (filename == NULL) {
624                 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
625                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
626                         g_free(filename);
627                         filename = NULL;
628                 }
629         }
631         /* load key bindings */
632         if (filename) {
633                 read_rc_file(filename);
634                 g_free(filename);
635                 filename = NULL;
636         }
638         return 0;