]> Dogcows Code - chaz/tint2/blob - src/config.c
a4642031c9d06f22d2074469867d11130a1071da
[chaz/tint2] / src / config.c
1 /**************************************************************************
2 *
3 * Tint2 : read/write config file
4 *
5 * Copyright (C) 2007 Pål Staurland (staura@gmail.com)
6 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 **************************************************************************/
20
21 #include <sys/stat.h>
22 #include <sys/time.h>
23 #include <unistd.h>
24 #include <cairo.h>
25 #include <cairo-xlib.h>
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <X11/Xatom.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <glib/gstdio.h>
34 #include <pango/pangocairo.h>
35 #include <pango/pangoxft.h>
36 #include <Imlib2.h>
37
38 #include "common.h"
39 #include "server.h"
40 #include "panel.h"
41 #include "task.h"
42 #include "taskbar.h"
43 #include "systraybar.h"
44 #include "launcher.h"
45 #include "clock.h"
46 #include "config.h"
47 #include "window.h"
48 #include "tooltip.h"
49 #include "timer.h"
50
51 #ifdef ENABLE_BATTERY
52 #include "battery.h"
53 #endif
54
55 // global path
56 char *config_path;
57 char *snapshot_path;
58
59 // --------------------------------------------------
60 // backward compatibility
61 // detect if it's an old config file (==1)
62 static int old_config_file;
63
64
65 void default_config()
66 {
67 config_path = 0;
68 snapshot_path = 0;
69 old_config_file = 1;
70 }
71
72 void cleanup_config()
73 {
74 if (config_path) g_free(config_path);
75 if (snapshot_path) g_free(snapshot_path);
76 }
77
78
79 void extract_values (const char *value, char **value1, char **value2, char **value3)
80 {
81 char *b=0, *c=0;
82
83 if (*value1) free (*value1);
84 if (*value2) free (*value2);
85 if (*value3) free (*value3);
86
87 if ((b = strchr (value, ' '))) {
88 b[0] = '\0';
89 b++;
90 }
91 else {
92 *value2 = 0;
93 *value3 = 0;
94 }
95 *value1 = strdup (value);
96 g_strstrip(*value1);
97
98 if (b) {
99 if ((c = strchr (b, ' '))) {
100 c[0] = '\0';
101 c++;
102 }
103 else {
104 c = 0;
105 *value3 = 0;
106 }
107 *value2 = strdup (b);
108 g_strstrip(*value2);
109 }
110
111 if (c) {
112 *value3 = strdup (c);
113 g_strstrip(*value3);
114 }
115 }
116
117
118 void get_action (char *event, int *action)
119 {
120 if (strcmp (event, "none") == 0)
121 *action = NONE;
122 else if (strcmp (event, "close") == 0)
123 *action = CLOSE;
124 else if (strcmp (event, "toggle") == 0)
125 *action = TOGGLE;
126 else if (strcmp (event, "iconify") == 0)
127 *action = ICONIFY;
128 else if (strcmp (event, "shade") == 0)
129 *action = SHADE;
130 else if (strcmp (event, "toggle_iconify") == 0)
131 *action = TOGGLE_ICONIFY;
132 else if (strcmp (event, "maximize_restore") == 0)
133 *action = MAXIMIZE_RESTORE;
134 else if (strcmp (event, "desktop_left") == 0)
135 *action = DESKTOP_LEFT;
136 else if (strcmp (event, "desktop_right") == 0)
137 *action = DESKTOP_RIGHT;
138 else if (strcmp (event, "next_task") == 0)
139 *action = NEXT_TASK;
140 else if (strcmp (event, "prev_task") == 0)
141 *action = PREV_TASK;
142 }
143
144
145 int get_task_status(char* status)
146 {
147 if (strcmp(status, "active") == 0)
148 return TASK_ACTIVE;
149 if (strcmp(status, "iconified") == 0)
150 return TASK_ICONIFIED;
151 if (strcmp(status, "urgent") == 0)
152 return TASK_URGENT;
153 return TASK_NORMAL;
154 }
155
156
157 int config_get_monitor(char* monitor)
158 {
159 if (strcmp(monitor, "all") != 0) {
160 char* endptr;
161 int ret_int = strtol(monitor, &endptr, 10);
162 if (*endptr == 0)
163 return ret_int-1;
164 else {
165 // monitor specified by name, not by index
166 int i, j;
167 for (i=0; i<server.nb_monitor; ++i) {
168 if (server.monitor[i].names == 0)
169 // xrandr can't identify monitors
170 continue;
171 j = 0;
172 while (server.monitor[i].names[j] != 0) {
173 if (strcmp(monitor, server.monitor[i].names[j++]) == 0)
174 return i;
175 }
176 }
177 }
178 }
179 // monitor == "all" or monitor not found or xrandr can't identify monitors
180 return -1;
181 }
182
183 void add_entry (char *key, char *value)
184 {
185 char *value1=0, *value2=0, *value3=0;
186
187 /* Background and border */
188 if (strcmp (key, "rounded") == 0) {
189 // 'rounded' is the first parameter => alloc a new background
190 Background bg;
191 bg.border.rounded = atoi(value);
192 g_array_append_val(backgrounds, bg);
193 }
194 else if (strcmp (key, "border_width") == 0) {
195 g_array_index(backgrounds, Background, backgrounds->len-1).border.width = atoi(value);
196 }
197 else if (strcmp (key, "background_color") == 0) {
198 Background* bg = &g_array_index(backgrounds, Background, backgrounds->len-1);
199 extract_values(value, &value1, &value2, &value3);
200 get_color (value1, bg->back.color);
201 if (value2) bg->back.alpha = (atoi (value2) / 100.0);
202 else bg->back.alpha = 0.5;
203 }
204 else if (strcmp (key, "border_color") == 0) {
205 Background* bg = &g_array_index(backgrounds, Background, backgrounds->len-1);
206 extract_values(value, &value1, &value2, &value3);
207 get_color (value1, bg->border.color);
208 if (value2) bg->border.alpha = (atoi (value2) / 100.0);
209 else bg->border.alpha = 0.5;
210 }
211
212 /* Panel */
213 else if (strcmp (key, "panel_monitor") == 0) {
214 panel_config.monitor = config_get_monitor(value);
215 }
216 else if (strcmp (key, "panel_size") == 0) {
217 extract_values(value, &value1, &value2, &value3);
218
219 char *b;
220 if ((b = strchr (value1, '%'))) {
221 b[0] = '\0';
222 panel_config.pourcentx = 1;
223 }
224 panel_config.area.width = atoi(value1);
225 if (panel_config.area.width == 0) {
226 // full width mode
227 panel_config.area.width = 100;
228 panel_config.pourcentx = 1;
229 }
230 if (value2) {
231 if ((b = strchr (value2, '%'))) {
232 b[0] = '\0';
233 panel_config.pourcenty = 1;
234 }
235 panel_config.area.height = atoi(value2);
236 }
237 }
238 else if (strcmp (key, "panel_items") == 0) {
239 if (panel_items_order) g_free(panel_items_order);
240 panel_items_order = strdup(value);
241 int j;
242 for (j=0 ; j < strlen(panel_items_order) ; j++) {
243 if (panel_items_order[j] == 'L')
244 launcher_enabled = 1;
245 if (panel_items_order[j] == 'T')
246 taskbar_enabled = 1;
247 if (panel_items_order[j] == 'B') {
248 #ifdef ENABLE_BATTERY
249 battery_enabled = 1;
250 #else
251 fprintf(stderr, "tint2 is build without battery support\n");
252 #endif
253 }
254 if (panel_items_order[j] == 'S') {
255 // systray disabled in snapshot mode
256 if (snapshot_path == 0)
257 systray_enabled = 1;
258 }
259 if (panel_items_order[j] == 'C')
260 clock_enabled = 1;
261 }
262 }
263 else if (strcmp (key, "panel_margin") == 0) {
264 extract_values(value, &value1, &value2, &value3);
265 panel_config.marginx = atoi (value1);
266 if (value2) panel_config.marginy = atoi (value2);
267 }
268 else if (strcmp (key, "panel_padding") == 0) {
269 extract_values(value, &value1, &value2, &value3);
270 panel_config.area.paddingxlr = panel_config.area.paddingx = atoi (value1);
271 if (value2) panel_config.area.paddingy = atoi (value2);
272 if (value3) panel_config.area.paddingx = atoi (value3);
273 }
274 else if (strcmp (key, "panel_position") == 0) {
275 extract_values(value, &value1, &value2, &value3);
276 if (strcmp (value1, "top") == 0) panel_position = TOP;
277 else {
278 if (strcmp (value1, "bottom") == 0) panel_position = BOTTOM;
279 else panel_position = CENTER;
280 }
281
282 if (!value2) panel_position |= CENTER;
283 else {
284 if (strcmp (value2, "left") == 0) panel_position |= LEFT;
285 else {
286 if (strcmp (value2, "right") == 0) panel_position |= RIGHT;
287 else panel_position |= CENTER;
288 }
289 }
290 if (!value3) panel_horizontal = 1;
291 else {
292 if (strcmp (value3, "vertical") == 0) panel_horizontal = 0;
293 else panel_horizontal = 1;
294 }
295 }
296 else if (strcmp (key, "font_shadow") == 0)
297 panel_config.g_task.font_shadow = atoi (value);
298 else if (strcmp (key, "panel_background_id") == 0) {
299 int id = atoi (value);
300 id = (id < backgrounds->len && id >= 0) ? id : 0;
301 panel_config.area.bg = &g_array_index(backgrounds, Background, id);
302 }
303 else if (strcmp (key, "wm_menu") == 0)
304 wm_menu = atoi (value);
305 else if (strcmp (key, "panel_dock") == 0)
306 panel_dock = atoi (value);
307 else if (strcmp (key, "urgent_nb_of_blink") == 0)
308 max_tick_urgent = atoi (value);
309 else if (strcmp (key, "panel_layer") == 0) {
310 if (strcmp(value, "bottom") == 0)
311 panel_layer = BOTTOM_LAYER;
312 else if (strcmp(value, "top") == 0)
313 panel_layer = TOP_LAYER;
314 else
315 panel_layer = NORMAL_LAYER;
316 }
317
318 /* Battery */
319 else if (strcmp (key, "battery_low_status") == 0) {
320 #ifdef ENABLE_BATTERY
321 battery_low_status = atoi(value);
322 if(battery_low_status < 0 || battery_low_status > 100)
323 battery_low_status = 0;
324 #endif
325 }
326 else if (strcmp (key, "battery_low_cmd") == 0) {
327 #ifdef ENABLE_BATTERY
328 if (strlen(value) > 0)
329 battery_low_cmd = strdup (value);
330 #endif
331 }
332 else if (strcmp (key, "bat1_font") == 0) {
333 #ifdef ENABLE_BATTERY
334 bat1_font_desc = pango_font_description_from_string (value);
335 #endif
336 }
337 else if (strcmp (key, "bat2_font") == 0) {
338 #ifdef ENABLE_BATTERY
339 bat2_font_desc = pango_font_description_from_string (value);
340 #endif
341 }
342 else if (strcmp (key, "battery_font_color") == 0) {
343 #ifdef ENABLE_BATTERY
344 extract_values(value, &value1, &value2, &value3);
345 get_color (value1, panel_config.battery.font.color);
346 if (value2) panel_config.battery.font.alpha = (atoi (value2) / 100.0);
347 else panel_config.battery.font.alpha = 0.5;
348 #endif
349 }
350 else if (strcmp (key, "battery_padding") == 0) {
351 #ifdef ENABLE_BATTERY
352 extract_values(value, &value1, &value2, &value3);
353 panel_config.battery.area.paddingxlr = panel_config.battery.area.paddingx = atoi (value1);
354 if (value2) panel_config.battery.area.paddingy = atoi (value2);
355 if (value3) panel_config.battery.area.paddingx = atoi (value3);
356 #endif
357 }
358 else if (strcmp (key, "battery_background_id") == 0) {
359 #ifdef ENABLE_BATTERY
360 int id = atoi (value);
361 id = (id < backgrounds->len && id >= 0) ? id : 0;
362 panel_config.battery.area.bg = &g_array_index(backgrounds, Background, id);
363 #endif
364 }
365 else if (strcmp (key, "battery_hide") == 0) {
366 #ifdef ENABLE_BATTERY
367 percentage_hide = atoi (value);
368 if (percentage_hide == 0)
369 percentage_hide = 101;
370 #endif
371 }
372
373 /* Clock */
374 else if (strcmp (key, "time1_format") == 0) {
375 if (strlen(value) > 0) {
376 time1_format = strdup (value);
377 clock_enabled = 1;
378 }
379 }
380 else if (strcmp (key, "time2_format") == 0) {
381 if (strlen(value) > 0)
382 time2_format = strdup (value);
383 }
384 else if (strcmp (key, "time1_font") == 0) {
385 time1_font_desc = pango_font_description_from_string (value);
386 }
387 else if (strcmp(key, "time1_timezone") == 0) {
388 if (strlen(value) > 0)
389 time1_timezone = strdup(value);
390 }
391 else if (strcmp(key, "time2_timezone") == 0) {
392 if (strlen(value) > 0)
393 time2_timezone = strdup(value);
394 }
395 else if (strcmp (key, "time2_font") == 0) {
396 time2_font_desc = pango_font_description_from_string (value);
397 }
398 else if (strcmp (key, "clock_font_color") == 0) {
399 extract_values(value, &value1, &value2, &value3);
400 get_color (value1, panel_config.clock.font.color);
401 if (value2) panel_config.clock.font.alpha = (atoi (value2) / 100.0);
402 else panel_config.clock.font.alpha = 0.5;
403 }
404 else if (strcmp (key, "clock_padding") == 0) {
405 extract_values(value, &value1, &value2, &value3);
406 panel_config.clock.area.paddingxlr = panel_config.clock.area.paddingx = atoi (value1);
407 if (value2) panel_config.clock.area.paddingy = atoi (value2);
408 if (value3) panel_config.clock.area.paddingx = atoi (value3);
409 }
410 else if (strcmp (key, "clock_background_id") == 0) {
411 int id = atoi (value);
412 id = (id < backgrounds->len && id >= 0) ? id : 0;
413 panel_config.clock.area.bg = &g_array_index(backgrounds, Background, id);
414 }
415 else if (strcmp(key, "clock_tooltip") == 0) {
416 if (strlen(value) > 0)
417 time_tooltip_format = strdup (value);
418 }
419 else if (strcmp(key, "clock_tooltip_timezone") == 0) {
420 if (strlen(value) > 0)
421 time_tooltip_timezone = strdup(value);
422 }
423 else if (strcmp(key, "clock_lclick_command") == 0) {
424 if (strlen(value) > 0)
425 clock_lclick_command = strdup(value);
426 }
427 else if (strcmp(key, "clock_rclick_command") == 0) {
428 if (strlen(value) > 0)
429 clock_rclick_command = strdup(value);
430 }
431
432 /* Taskbar */
433 else if (strcmp (key, "taskbar_mode") == 0) {
434 if (strcmp (value, "multi_desktop") == 0) panel_mode = MULTI_DESKTOP;
435 else panel_mode = SINGLE_DESKTOP;
436 }
437 else if (strcmp (key, "taskbar_padding") == 0) {
438 extract_values(value, &value1, &value2, &value3);
439 panel_config.g_taskbar.area.paddingxlr = panel_config.g_taskbar.area.paddingx = atoi (value1);
440 if (value2) panel_config.g_taskbar.area.paddingy = atoi (value2);
441 if (value3) panel_config.g_taskbar.area.paddingx = atoi (value3);
442 }
443 else if (strcmp (key, "taskbar_background_id") == 0) {
444 int id = atoi (value);
445 id = (id < backgrounds->len && id >= 0) ? id : 0;
446 panel_config.g_taskbar.bg = &g_array_index(backgrounds, Background, id);
447 panel_config.g_taskbar.area.bg = panel_config.g_taskbar.bg;
448 }
449 else if (strcmp (key, "taskbar_active_background_id") == 0) {
450 int id = atoi (value);
451 id = (id < backgrounds->len && id >= 0) ? id : 0;
452 panel_config.g_taskbar.bg_active = &g_array_index(backgrounds, Background, id);
453 panel_config.g_taskbar.use_active = 1;
454 }
455
456 /* Task */
457 else if (strcmp (key, "task_text") == 0)
458 panel_config.g_task.text = atoi (value);
459 else if (strcmp (key, "task_icon") == 0)
460 panel_config.g_task.icon = atoi (value);
461 else if (strcmp (key, "task_centered") == 0)
462 panel_config.g_task.centered = atoi (value);
463 else if (strcmp (key, "task_width") == 0) {
464 // old parameter : just for backward compatibility
465 panel_config.g_task.maximum_width = atoi (value);
466 panel_config.g_task.maximum_height = 30;
467 }
468 else if (strcmp (key, "task_maximum_size") == 0) {
469 extract_values(value, &value1, &value2, &value3);
470 panel_config.g_task.maximum_width = atoi (value1);
471 panel_config.g_task.maximum_height = 30;
472 if (value2)
473 panel_config.g_task.maximum_height = atoi (value2);
474 }
475 else if (strcmp (key, "task_padding") == 0) {
476 extract_values(value, &value1, &value2, &value3);
477 panel_config.g_task.area.paddingxlr = panel_config.g_task.area.paddingx = atoi (value1);
478 if (value2) panel_config.g_task.area.paddingy = atoi (value2);
479 if (value3) panel_config.g_task.area.paddingx = atoi (value3);
480 }
481 else if (strcmp (key, "task_font") == 0) {
482 panel_config.g_task.font_desc = pango_font_description_from_string (value);
483 }
484 else if (g_regex_match_simple("task.*_font_color", key, 0, 0)) {
485 gchar** split = g_regex_split_simple("_", key, 0, 0);
486 int status = get_task_status(split[1]);
487 g_strfreev(split);
488 extract_values(value, &value1, &value2, &value3);
489 float alpha = 1;
490 if (value2) alpha = (atoi (value2) / 100.0);
491 get_color (value1, panel_config.g_task.font[status].color);
492 panel_config.g_task.font[status].alpha = alpha;
493 panel_config.g_task.config_font_mask |= (1<<status);
494 }
495 else if (g_regex_match_simple("task.*_icon_asb", key, 0, 0)) {
496 gchar** split = g_regex_split_simple("_", key, 0, 0);
497 int status = get_task_status(split[1]);
498 g_strfreev(split);
499 extract_values(value, &value1, &value2, &value3);
500 panel_config.g_task.alpha[status] = atoi(value1);
501 panel_config.g_task.saturation[status] = atoi(value2);
502 panel_config.g_task.brightness[status] = atoi(value3);
503 panel_config.g_task.config_asb_mask |= (1<<status);
504 }
505 else if (g_regex_match_simple("task.*_background_id", key, 0, 0)) {
506 gchar** split = g_regex_split_simple("_", key, 0, 0);
507 int status = get_task_status(split[1]);
508 g_strfreev(split);
509 int id = atoi (value);
510 id = (id < backgrounds->len && id >= 0) ? id : 0;
511 panel_config.g_task.background[status] = &g_array_index(backgrounds, Background, id);
512 panel_config.g_task.config_background_mask |= (1<<status);
513 if (status == TASK_NORMAL) panel_config.g_task.area.bg = panel_config.g_task.background[TASK_NORMAL];
514 }
515
516 /* Systray */
517 else if (strcmp (key, "systray_padding") == 0) {
518 extract_values(value, &value1, &value2, &value3);
519 systray.area.paddingxlr = systray.area.paddingx = atoi (value1);
520 if (value2) systray.area.paddingy = atoi (value2);
521 if (value3) systray.area.paddingx = atoi (value3);
522 }
523 else if (strcmp (key, "systray_background_id") == 0) {
524 int id = atoi (value);
525 id = (id < backgrounds->len && id >= 0) ? id : 0;
526 systray.area.bg = &g_array_index(backgrounds, Background, id);
527 }
528 else if (strcmp(key, "systray_sort") == 0) {
529 if (strcmp(value, "descending") == 0)
530 systray.sort = -1;
531 else if (strcmp(value, "ascending") == 0)
532 systray.sort = 1;
533 else if (strcmp(value, "left2right") == 0)
534 systray.sort = 2;
535 else if (strcmp(value, "right2left") == 0)
536 systray.sort = 3;
537 }
538 else if (strcmp(key, "systray_icon_size") == 0) {
539 systray_max_icon_size = atoi(value);
540 }
541 else if (strcmp(key, "systray_icon_asb") == 0) {
542 extract_values(value, &value1, &value2, &value3);
543 systray.alpha = atoi(value1);
544 systray.saturation = atoi(value2);
545 systray.brightness = atoi(value3);
546 }
547
548 /* Launcher */
549 else if (strcmp (key, "launcher_padding") == 0) {
550 extract_values(value, &value1, &value2, &value3);
551 panel_config.launcher.area.paddingxlr = panel_config.launcher.area.paddingx = atoi (value1);
552 if (value2) panel_config.launcher.area.paddingy = atoi (value2);
553 if (value3) panel_config.launcher.area.paddingx = atoi (value3);
554 }
555 else if (strcmp (key, "launcher_background_id") == 0) {
556 int id = atoi (value);
557 id = (id < backgrounds->len && id >= 0) ? id : 0;
558 panel_config.launcher.area.bg = &g_array_index(backgrounds, Background, id);
559 }
560 else if (strcmp(key, "launcher_icon_size") == 0) {
561 launcher_max_icon_size = atoi(value);
562 }
563 else if (strcmp(key, "launcher_item_app") == 0) {
564 char *app = strdup(value);
565 panel_config.launcher.list_apps = g_slist_append(panel_config.launcher.list_apps, app);
566 }
567 else if (strcmp(key, "launcher_icon_theme") == 0) {
568 char *app = strdup(value);
569 panel_config.launcher.icon_theme_names = g_slist_append(panel_config.launcher.icon_theme_names, app);
570 }
571
572 /* Tooltip */
573 else if (strcmp (key, "tooltip") == 0)
574 g_tooltip.enabled = atoi(value);
575 else if (strcmp (key, "tooltip_show_timeout") == 0) {
576 int timeout_msec = 1000*atof(value);
577 g_tooltip.show_timeout_msec = timeout_msec;
578 }
579 else if (strcmp (key, "tooltip_hide_timeout") == 0) {
580 int timeout_msec = 1000*atof(value);
581 g_tooltip.hide_timeout_msec = timeout_msec;
582 }
583 else if (strcmp (key, "tooltip_padding") == 0) {
584 extract_values(value, &value1, &value2, &value3);
585 if (value1) g_tooltip.paddingx = atoi(value1);
586 if (value2) g_tooltip.paddingy = atoi(value2);
587 }
588 else if (strcmp (key, "tooltip_background_id") == 0) {
589 int id = atoi (value);
590 id = (id < backgrounds->len && id >= 0) ? id : 0;
591 g_tooltip.bg = &g_array_index(backgrounds, Background, id);
592 }
593 else if (strcmp (key, "tooltip_font_color") == 0) {
594 extract_values(value, &value1, &value2, &value3);
595 get_color(value1, g_tooltip.font_color.color);
596 if (value2) g_tooltip.font_color.alpha = (atoi (value2) / 100.0);
597 else g_tooltip.font_color.alpha = 0.1;
598 }
599 else if (strcmp (key, "tooltip_font") == 0) {
600 g_tooltip.font_desc = pango_font_description_from_string(value);
601 }
602
603 /* Mouse actions */
604 else if (strcmp (key, "mouse_middle") == 0)
605 get_action (value, &mouse_middle);
606 else if (strcmp (key, "mouse_right") == 0)
607 get_action (value, &mouse_right);
608 else if (strcmp (key, "mouse_scroll_up") == 0)
609 get_action (value, &mouse_scroll_up);
610 else if (strcmp (key, "mouse_scroll_down") == 0)
611 get_action (value, &mouse_scroll_down);
612
613 /* autohide options */
614 else if (strcmp(key, "autohide") == 0)
615 panel_autohide = atoi(value);
616 else if (strcmp(key, "autohide_show_timeout") == 0)
617 panel_autohide_show_timeout = 1000*atof(value);
618 else if (strcmp(key, "autohide_hide_timeout") == 0)
619 panel_autohide_hide_timeout = 1000*atof(value);
620 else if (strcmp(key, "strut_policy") == 0) {
621 if (strcmp(value, "follow_size") == 0)
622 panel_strut_policy = STRUT_FOLLOW_SIZE;
623 else if (strcmp(value, "none") == 0)
624 panel_strut_policy = STRUT_NONE;
625 else
626 panel_strut_policy = STRUT_MINIMUM;
627 }
628 else if (strcmp(key, "autohide_height") == 0) {
629 panel_autohide_height = atoi(value);
630 if (panel_autohide_height == 0) {
631 // autohide need height > 0
632 panel_autohide_height = 1;
633 }
634 }
635
636 else
637 fprintf(stderr, "tint2 : invalid option \"%s\",\n upgrade tint2 or correct your config file\n", key);
638
639 if (value1) free (value1);
640 if (value2) free (value2);
641 if (value3) free (value3);
642 }
643
644
645 int config_read ()
646 {
647 const gchar * const * system_dirs;
648 char *path1;
649 gint i;
650
651 // follow XDG specification
652 // check tint2rc in user directory
653 path1 = g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL);
654 if (g_file_test (path1, G_FILE_TEST_EXISTS)) {
655 i = config_read_file (path1);
656 config_path = strdup(path1);
657 g_free(path1);
658 return i;
659 }
660 g_free(path1);
661
662 // copy tint2rc from system directory to user directory
663 char *path2 = 0;
664 system_dirs = g_get_system_config_dirs();
665 for (i = 0; system_dirs[i]; i++) {
666 path2 = g_build_filename(system_dirs[i], "tint2", "tint2rc", NULL);
667
668 if (g_file_test(path2, G_FILE_TEST_EXISTS)) break;
669 g_free (path2);
670 path2 = 0;
671 }
672
673 if (path2) {
674 // copy file in user directory (path1)
675 char *dir = g_build_filename (g_get_user_config_dir(), "tint2", NULL);
676 if (!g_file_test (dir, G_FILE_TEST_IS_DIR)) g_mkdir(dir, 0777);
677 g_free(dir);
678
679 path1 = g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL);
680 copy_file(path2, path1);
681 g_free(path2);
682
683 i = config_read_file (path1);
684 config_path = strdup(path1);
685 g_free(path1);
686 return i;
687 }
688 return 0;
689 }
690
691
692 int config_read_file (const char *path)
693 {
694 FILE *fp;
695 char line[512];
696 char *key, *value;
697
698 if ((fp = fopen(path, "r")) == NULL) return 0;
699
700 while (fgets(line, sizeof(line), fp) != NULL) {
701 if (parse_line(line, &key, &value)) {
702 add_entry (key, value);
703 free (key);
704 free (value);
705 }
706 }
707 fclose (fp);
708
709 return 1;
710 }
711
712
713
This page took 0.066945 seconds and 3 git commands to generate.