Code

Added support for (auto) center/focus playlists.
[ncmpc.git] / conf.c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
10 #include <glib.h>
11 #include <ncurses.h>
13 #include "config.h"
14 #include "options.h"
15 #include "support.h"
16 #include "conf.h"
18 #ifdef DEBUG
19 #define D(x) x
20 #else
21 #define D(x)
22 #endif
24 #define RCFILE "." PACKAGE "rc"
26 #define MAX_LINE_LENGTH 1024
27 #define COMMENT_TOKEN   '#'
29 /* configuration field names */
30 #define CONF_ENABLE_COLORS           "enable_colors"
31 #define CONF_AUTO_CENTER             "auto_center"
33 /* configuration field names - colors */
34 #define CONF_COLOR_BACKGROUND        "background_color"
35 #define CONF_COLOR_TITLE             "title_color"
36 #define CONF_COLOR_LINE              "line_color"
37 #define CONF_COLOR_LIST              "list_color"
38 #define CONF_COLOR_PROGRESS          "progress_color"
39 #define CONF_COLOR_STATUS            "status_color"
40 #define CONF_COLOR_ALERT             "alert_color"
43 static int
44 str2bool(char *str)
45 {
46   if( !strcasecmp(str,"no")  || !strcasecmp(str,"false") || 
47       !strcasecmp(str,"off") || !strcasecmp(str,"0") )
48     return 0;
49   return 1;
50 }
52 static int
53 str2color(char *str)
54 {
55   if( !strcasecmp(str,"black") )
56     return COLOR_BLACK;
57   else if( !strcasecmp(str,"red") )
58     return COLOR_RED;
59   else if( !strcasecmp(str,"green") )
60     return COLOR_GREEN;
61   else if( !strcasecmp(str,"yellow") )
62     return COLOR_YELLOW;
63   else if( !strcasecmp(str,"blue") )
64     return COLOR_BLUE;
65   else if( !strcasecmp(str,"magenta") )
66     return COLOR_MAGENTA;
67   else if( !strcasecmp(str,"cyan") )
68     return COLOR_CYAN;
69   else if( !strcasecmp(str,"white") )
70     return COLOR_WHITE;
71   fprintf(stderr,"Warning: unknown color %s\n", str);
72   return -1;
73 }
75 int
76 read_rc_file(char *filename, options_t *options)
77 {
78   int fd;
79   int quit  = 0;
80   int color = -1;
81   int free_filename = 0;
83   if( filename==NULL )
84     {
85       filename = g_build_filename(g_get_home_dir(), RCFILE, NULL);
86       free_filename = 1;
87     }
89   D(printf("\n--Reading configuration file %s\n", filename));
90   if( (fd=open(filename,O_RDONLY)) <0 )
91     {
92       D(perror(filename));
93       if( free_filename )
94         g_free(filename);
95       return -1;
96     }
98   while( !quit )
99     {
100       int i,j;
101       int len;
102       int match_found;
103       char line[MAX_LINE_LENGTH];
104       char name[MAX_LINE_LENGTH];
105       char value[MAX_LINE_LENGTH];
107       line[0]  = '\0';
108       value[0] = '\0';
110       i = 0;
111       /* read a line ending with '\n' */
112       do {
113         len = read( fd, &line[i], 1 );
114         if( len == 1 )
115           i++;
116         else
117           quit = 1;
118       } while( !quit && i<MAX_LINE_LENGTH && line[i-1]!='\n' );
119       
120      
121       /* remove trailing whitespace */
122       line[i] = '\0';
123       i--;
124       while( i>=0 && IS_WHITESPACE(line[i]) )
125         {
126           line[i] = '\0';
127           i--;
128         }     
129       len = i+1;
131       if( len>0 )
132         {
133           i = 0;
134           /* skip whitespace */
135           while( i<len && IS_WHITESPACE(line[i]) )
136             i++;
137           
138           /* continue if this line is not a comment */
139           if( line[i] != COMMENT_TOKEN )
140             {
141               /* get the name part */
142               j=0;
143               while( i<len && line[i]!='=' && !IS_WHITESPACE(line[i]) )
144                 {
145                   name[j++] = line[i++];
146                 }
147               name[j] = '\0';
148               
149               /* skip '=' and whitespace */
150               while( i<len && (line[i]=='=' || IS_WHITESPACE(line[i])) )
151                 i++;
152               
153               /* get the value part */
154               j=0;
155               while( i<len )
156                 {
157                   value[j++] = line[i++];
158                 }
159               value[j] = '\0';
160               
161               match_found = 0;
162               
163               /* enable colors */
164               if( !strcasecmp(CONF_ENABLE_COLORS, name) )
165                 {
166                   options->enable_colors = str2bool(value);
167                   match_found = 1;
168                 }
169               /* auto center */
170               else if( !strcasecmp(CONF_AUTO_CENTER, name) )
171                 {
172                   options->auto_center = str2bool(value);
173                   match_found = 1;
174                 }
175               /* background color */
176               else if( !strcasecmp(CONF_COLOR_BACKGROUND, name) )
177                 {
178                   if( (color=str2color(value)) >= 0 )
179                     options->bg_color = color;
180                   match_found = 1;
181                 }
182               /* color - top (title) window */
183               else if( !strcasecmp(CONF_COLOR_TITLE, name) )
184                 {
185                   if( (color=str2color(value)) >= 0 )
186                     options->title_color = color;
187                   match_found = 1;
188                 }
189               /* color - line (title) window */
190               else if( !strcasecmp(CONF_COLOR_LINE, name) )
191                 {
192                   if( (color=str2color(value)) >= 0 )
193                     options->line_color = color;
194                   match_found = 1;
195                 }
196               /* color - list window */
197               else if( !strcasecmp(CONF_COLOR_LIST, name) )
198                 {
199                   if( (color=str2color(value)) >= 0 )
200                     options->list_color = color;
201                   match_found = 1;
202                 }
203               /* color - progress bar */
204               else if( !strcasecmp(CONF_COLOR_PROGRESS, name) )
205                 {
206                   if( (color=str2color(value)) >= 0 )
207                     options->progress_color = color;
208                   match_found = 1;
209                 }
210               /* color - status window */
211               else if( !strcasecmp(CONF_COLOR_STATUS, name) )
212                 {
213                   if( (color=str2color(value)) >= 0 )
214                     options->status_color = color;
215                   match_found = 1;
216                 }
217               /* color - alerts */
218               else if( !strcasecmp(CONF_COLOR_ALERT, name) )
219                 {
220                   if( (color=str2color(value)) >= 0 )
221                     options->alert_color = color;
222                   match_found = 1;
223                 }
224               
226               if( !match_found )
227                 fprintf(stderr, 
228                         "Unknown configuration parameter: %s\n", 
229                         name);
230 #ifdef DEBUG
231               printf( "  %s = %s %s\n", 
232                       name, 
233                       value,
234                       match_found ? "" : "- UNKNOWN SETTING!" );
235 #endif
237             }
238         }         
239     }
241   D(printf( "--\n\n" ));
243   if( free_filename )
244     g_free(filename);
245  
246   return 0;