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