]> Dogcows Code - chaz/tint2/blob - src/config.c
f70b78a9a8db0cf055938bb4d05031f7f5aebb78
[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 <Imlib2.h>
36
37 #include "common.h"
38 #include "server.h"
39 #include "task.h"
40 #include "taskbar.h"
41 #include "systraybar.h"
42 #include "clock.h"
43 #include "panel.h"
44 #include "config.h"
45 #include "window.h"
46
47 #ifdef ENABLE_BATTERY
48 #include "battery.h"
49 #endif
50
51
52 // --------------------------------------------------
53 // backward compatibility
54 static int save_file_config;
55 static int old_task_icon_size;
56 static char *old_task_font;
57 static char *old_time1_font;
58 static char *old_time2_font;
59 static Area *area_task, *area_task_active;
60
61 #ifdef ENABLE_BATTERY
62 static char *old_bat1_font;
63 static char *old_bat2_font;
64 #endif
65
66 // temporary panel
67 static Panel *panel_config = 0;
68
69 // temporary list of background
70 static GSList *list_back;
71
72
73 void init_config()
74 {
75 cleanup_panel();
76
77 // get monitor and desktop config
78 get_monitors_and_desktops();
79
80 // append full transparency background
81 list_back = g_slist_append(0, calloc(1, sizeof(Area)));
82
83 panel_config = calloc(1, sizeof(Panel));
84 // window manager's menu default value == false
85 wm_menu = 0;
86 }
87
88
89 void cleanup_config()
90 {
91 free(panel_config);
92 panel_config = 0;
93
94 // cleanup background list
95 GSList *l0;
96 for (l0 = list_back; l0 ; l0 = l0->next) {
97 free(l0->data);
98 }
99 g_slist_free(list_back);
100 list_back = NULL;
101 }
102
103
104 void copy_file(const char *pathSrc, const char *pathDest)
105 {
106 FILE *fileSrc, *fileDest;
107 char line[100];
108 int nb;
109
110 fileSrc = fopen(pathSrc, "rb");
111 if (fileSrc == NULL) return;
112
113 fileDest = fopen(pathDest, "wb");
114 if (fileDest == NULL) return;
115
116 while ((nb = fread(line, 1, 100, fileSrc)) > 0) fwrite(line, 1, nb, fileDest);
117
118 fclose (fileDest);
119 fclose (fileSrc);
120 }
121
122
123 void extract_values (const char *value, char **value1, char **value2, char **value3)
124 {
125 char *b=0, *c=0;
126
127 if (*value1) free (*value1);
128 if (*value2) free (*value2);
129 if (*value3) free (*value3);
130
131 if ((b = strchr (value, ' '))) {
132 b[0] = '\0';
133 b++;
134 }
135 else {
136 *value2 = 0;
137 *value3 = 0;
138 }
139 *value1 = strdup (value);
140 g_strstrip(*value1);
141
142 if (b) {
143 if ((c = strchr (b, ' '))) {
144 c[0] = '\0';
145 c++;
146 }
147 else {
148 c = 0;
149 *value3 = 0;
150 }
151 *value2 = strdup (b);
152 g_strstrip(*value2);
153 }
154
155 if (c) {
156 *value3 = strdup (c);
157 g_strstrip(*value3);
158 }
159 }
160
161
162 int hex_char_to_int (char c)
163 {
164 int r;
165
166 if (c >= '0' && c <= '9') r = c - '0';
167 else if (c >= 'a' && c <= 'f') r = c - 'a' + 10;
168 else if (c >= 'A' && c <= 'F') r = c - 'A' + 10;
169 else r = 0;
170
171 return r;
172 }
173
174
175 int hex_to_rgb (char *hex, int *r, int *g, int *b)
176 {
177 int len;
178
179 if (hex == NULL || hex[0] != '#') return (0);
180
181 len = strlen (hex);
182 if (len == 3 + 1) {
183 *r = hex_char_to_int (hex[1]);
184 *g = hex_char_to_int (hex[2]);
185 *b = hex_char_to_int (hex[3]);
186 }
187 else if (len == 6 + 1) {
188 *r = hex_char_to_int (hex[1]) * 16 + hex_char_to_int (hex[2]);
189 *g = hex_char_to_int (hex[3]) * 16 + hex_char_to_int (hex[4]);
190 *b = hex_char_to_int (hex[5]) * 16 + hex_char_to_int (hex[6]);
191 }
192 else if (len == 12 + 1) {
193 *r = hex_char_to_int (hex[1]) * 16 + hex_char_to_int (hex[2]);
194 *g = hex_char_to_int (hex[5]) * 16 + hex_char_to_int (hex[6]);
195 *b = hex_char_to_int (hex[9]) * 16 + hex_char_to_int (hex[10]);
196 }
197 else return 0;
198
199 return 1;
200 }
201
202
203 void get_color (char *hex, double *rgb)
204 {
205 int r, g, b;
206 hex_to_rgb (hex, &r, &g, &b);
207
208 rgb[0] = (r / 255.0);
209 rgb[1] = (g / 255.0);
210 rgb[2] = (b / 255.0);
211 }
212
213
214 void get_action (char *event, int *action)
215 {
216 if (strcmp (event, "none") == 0)
217 *action = NONE;
218 else if (strcmp (event, "close") == 0)
219 *action = CLOSE;
220 else if (strcmp (event, "toggle") == 0)
221 *action = TOGGLE;
222 else if (strcmp (event, "iconify") == 0)
223 *action = ICONIFY;
224 else if (strcmp (event, "shade") == 0)
225 *action = SHADE;
226 else if (strcmp (event, "toggle_iconify") == 0)
227 *action = TOGGLE_ICONIFY;
228 }
229
230
231 void add_entry (char *key, char *value)
232 {
233 char *value1=0, *value2=0, *value3=0;
234
235 /* Background and border */
236 if (strcmp (key, "rounded") == 0) {
237 // 'rounded' is the first parameter => alloc a new background
238 Area *a = calloc(1, sizeof(Area));
239 a->pix.border.rounded = atoi (value);
240 list_back = g_slist_append(list_back, a);
241 }
242 else if (strcmp (key, "border_width") == 0) {
243 Area *a = g_slist_last(list_back)->data;
244 a->pix.border.width = atoi (value);
245 }
246 else if (strcmp (key, "background_color") == 0) {
247 Area *a = g_slist_last(list_back)->data;
248 extract_values(value, &value1, &value2, &value3);
249 get_color (value1, a->pix.back.color);
250 if (value2) a->pix.back.alpha = (atoi (value2) / 100.0);
251 else a->pix.back.alpha = 0.5;
252 }
253 else if (strcmp (key, "border_color") == 0) {
254 Area *a = g_slist_last(list_back)->data;
255 extract_values(value, &value1, &value2, &value3);
256 get_color (value1, a->pix.border.color);
257 if (value2) a->pix.border.alpha = (atoi (value2) / 100.0);
258 else a->pix.border.alpha = 0.5;
259 }
260
261 /* Panel */
262 else if (strcmp (key, "panel_monitor") == 0) {
263 if (strcmp (value, "all") == 0) panel_config->monitor = -1;
264 else {
265 panel_config->monitor = atoi (value);
266 if (panel_config->monitor > 0) panel_config->monitor -= 1;
267 }
268 }
269 else if (strcmp (key, "panel_size") == 0) {
270 extract_values(value, &value1, &value2, &value3);
271
272 char *b;
273 if ((b = strchr (value1, '%'))) {
274 b[0] = '\0';
275 panel_config->pourcentx = 1;
276 }
277 panel_config->initial_width = atof(value1);
278 if (value2) {
279 if ((b = strchr (value2, '%'))) {
280 b[0] = '\0';
281 panel_config->pourcenty = 1;
282 }
283 panel_config->initial_height = atof(value2);
284 }
285 }
286 else if (strcmp (key, "panel_margin") == 0) {
287 extract_values(value, &value1, &value2, &value3);
288 panel_config->marginx = atoi (value1);
289 if (value2) panel_config->marginy = atoi (value2);
290 }
291 else if (strcmp (key, "panel_padding") == 0) {
292 extract_values(value, &value1, &value2, &value3);
293 panel_config->area.paddingxlr = panel_config->area.paddingx = atoi (value1);
294 if (value2) panel_config->area.paddingy = atoi (value2);
295 if (value3) panel_config->area.paddingx = atoi (value3);
296 }
297 else if (strcmp (key, "panel_position") == 0) {
298 extract_values(value, &value1, &value2, &value3);
299 if (strcmp (value1, "top") == 0) panel_position = TOP;
300 else panel_position = BOTTOM;
301
302 if (!value2) panel_position = CENTER;
303 else {
304 if (strcmp (value2, "left") == 0) panel_position |= LEFT;
305 else {
306 if (strcmp (value2, "right") == 0) panel_position |= RIGHT;
307 else panel_position |= CENTER;
308 }
309 }
310 }
311 else if (strcmp (key, "font_shadow") == 0)
312 panel_config->g_task.font_shadow = atoi (value);
313 else if (strcmp (key, "panel_background_id") == 0) {
314 int id = atoi (value);
315 Area *a = g_slist_nth_data(list_back, id);
316 memcpy(&panel_config->area.pix.back, &a->pix.back, sizeof(Color));
317 memcpy(&panel_config->area.pix.border, &a->pix.border, sizeof(Border));
318 }
319 else if (strcmp (key, "wm_menu") == 0)
320 wm_menu = atoi (value);
321
322 /* Battery */
323 #ifdef ENABLE_BATTERY
324 else if (strcmp (key, "battery") == 0) {
325 if(atoi(value) == 1)
326 panel_config->battery.area.on_screen = 1;
327 }
328 else if (strcmp (key, "battery_low_status") == 0) {
329 battery_low_status = atoi(value);
330 if(battery_low_status < 0 || battery_low_status > 100)
331 battery_low_status = 0;
332 }
333 else if (strcmp (key, "battery_low_cmd") == 0) {
334 if (battery_low_cmd) g_free(battery_low_cmd);
335 if (strlen(value) > 0) battery_low_cmd = strdup (value);
336 else battery_low_cmd = 0;
337 }
338 else if (strcmp (key, "bat1_font") == 0) {
339 if (save_file_config) old_bat1_font = strdup (value);
340 if (bat1_font_desc) pango_font_description_free(bat1_font_desc);
341 bat1_font_desc = pango_font_description_from_string (value);
342 }
343 else if (strcmp (key, "bat2_font") == 0) {
344 if (save_file_config) old_bat2_font = strdup (value);
345 if (bat2_font_desc) pango_font_description_free(bat2_font_desc);
346 bat2_font_desc = pango_font_description_from_string (value);
347 }
348 else if (strcmp (key, "battery_font_color") == 0) {
349 extract_values(value, &value1, &value2, &value3);
350 get_color (value1, panel_config->battery.font.color);
351 if (value2) panel_config->battery.font.alpha = (atoi (value2) / 100.0);
352 else panel_config->battery.font.alpha = 0.5;
353 }
354 else if (strcmp (key, "battery_padding") == 0) {
355 extract_values(value, &value1, &value2, &value3);
356 panel_config->battery.area.paddingxlr = panel_config->battery.area.paddingx = atoi (value1);
357 if (value2) panel_config->battery.area.paddingy = atoi (value2);
358 if (value3) panel_config->battery.area.paddingx = atoi (value3);
359 }
360 else if (strcmp (key, "battery_background_id") == 0) {
361 int id = atoi (value);
362 Area *a = g_slist_nth_data(list_back, id);
363 memcpy(&panel_config->battery.area.pix.back, &a->pix.back, sizeof(Color));
364 memcpy(&panel_config->battery.area.pix.border, &a->pix.border, sizeof(Border));
365 }
366 #else
367 else if ((strcmp (key, "battery") == 0) || (strcmp (key, "battery_low_status") == 0) || (strcmp (key, "battery_low_cmd") == 0) || (strcmp (key, "bat1_font") == 0) || (strcmp (key, "bat2_font") == 0) || (strcmp (key, "battery_font_color") == 0) || (strcmp (key, "battery_padding") == 0) || (strcmp (key, "battery_background_id") == 0)) {
368 printf("tint2 is build without battery support\n");
369 }
370 #endif
371
372 /* Clock */
373 else if (strcmp (key, "time1_format") == 0) {
374 if (time1_format) g_free(time1_format);
375 if (strlen(value) > 0) {
376 time1_format = strdup (value);
377 panel_config->clock.area.on_screen = 1;
378 }
379 else {
380 time1_format = 0;
381 panel_config->clock.area.on_screen = 0;
382 }
383 }
384 else if (strcmp (key, "time2_format") == 0) {
385 if (time2_format) g_free(time2_format);
386 if (strlen(value) > 0) time2_format = strdup (value);
387 else time2_format = 0;
388 }
389 else if (strcmp (key, "time1_font") == 0) {
390 if (save_file_config) old_time1_font = strdup (value);
391 if (time1_font_desc) pango_font_description_free(time1_font_desc);
392 time1_font_desc = pango_font_description_from_string (value);
393 }
394 else if (strcmp (key, "time2_font") == 0) {
395 if (save_file_config) old_time2_font = strdup (value);
396 if (time2_font_desc) pango_font_description_free(time2_font_desc);
397 time2_font_desc = pango_font_description_from_string (value);
398 }
399 else if (strcmp (key, "clock_font_color") == 0) {
400 extract_values(value, &value1, &value2, &value3);
401 get_color (value1, panel_config->clock.font.color);
402 if (value2) panel_config->clock.font.alpha = (atoi (value2) / 100.0);
403 else panel_config->clock.font.alpha = 0.5;
404 }
405 else if (strcmp (key, "clock_padding") == 0) {
406 extract_values(value, &value1, &value2, &value3);
407 panel_config->clock.area.paddingxlr = panel_config->clock.area.paddingx = atoi (value1);
408 if (value2) panel_config->clock.area.paddingy = atoi (value2);
409 if (value3) panel_config->clock.area.paddingx = atoi (value3);
410 }
411 else if (strcmp (key, "clock_background_id") == 0) {
412 int id = atoi (value);
413 Area *a = g_slist_nth_data(list_back, id);
414 memcpy(&panel_config->clock.area.pix.back, &a->pix.back, sizeof(Color));
415 memcpy(&panel_config->clock.area.pix.border, &a->pix.border, sizeof(Border));
416 }
417 else if (strcmp(key, "clock_lclick_command") == 0) {
418 if (clock_lclick_command) g_free(clock_lclick_command);
419 if (strlen(value) > 0) clock_lclick_command = strdup(value);
420 else clock_lclick_command = 0;
421 }
422 else if (strcmp(key, "clock_rclick_command") == 0) {
423 if (clock_rclick_command) g_free(clock_rclick_command);
424 if (strlen(value) > 0) clock_rclick_command = strdup(value);
425 else clock_rclick_command = 0;
426 }
427
428 /* Taskbar */
429 else if (strcmp (key, "taskbar_mode") == 0) {
430 if (strcmp (value, "multi_desktop") == 0) panel_mode = MULTI_DESKTOP;
431 else panel_mode = SINGLE_DESKTOP;
432 }
433 else if (strcmp (key, "taskbar_padding") == 0) {
434 extract_values(value, &value1, &value2, &value3);
435 panel_config->g_taskbar.paddingxlr = panel_config->g_taskbar.paddingx = atoi (value1);
436 if (value2) panel_config->g_taskbar.paddingy = atoi (value2);
437 if (value3) panel_config->g_taskbar.paddingx = atoi (value3);
438 }
439 else if (strcmp (key, "taskbar_background_id") == 0) {
440 int id = atoi (value);
441 Area *a = g_slist_nth_data(list_back, id);
442 memcpy(&panel_config->g_taskbar.pix.back, &a->pix.back, sizeof(Color));
443 memcpy(&panel_config->g_taskbar.pix.border, &a->pix.border, sizeof(Border));
444 }
445
446 /* Task */
447 else if (strcmp (key, "task_text") == 0)
448 panel_config->g_task.text = atoi (value);
449 else if (strcmp (key, "task_icon") == 0)
450 panel_config->g_task.icon = atoi (value);
451 else if (strcmp (key, "task_centered") == 0)
452 panel_config->g_task.centered = atoi (value);
453 else if (strcmp (key, "task_width") == 0)
454 panel_config->g_task.maximum_width = atoi (value);
455 else if (strcmp (key, "task_padding") == 0) {
456 extract_values(value, &value1, &value2, &value3);
457 panel_config->g_task.area.paddingxlr = panel_config->g_task.area.paddingx = atoi (value1);
458 if (value2) panel_config->g_task.area.paddingy = atoi (value2);
459 if (value3) panel_config->g_task.area.paddingx = atoi (value3);
460 }
461 else if (strcmp (key, "task_font") == 0) {
462 if (save_file_config) old_task_font = strdup (value);
463 if (panel_config->g_task.font_desc) pango_font_description_free(panel_config->g_task.font_desc);
464 panel_config->g_task.font_desc = pango_font_description_from_string (value);
465 }
466 else if (strcmp (key, "task_font_color") == 0) {
467 extract_values(value, &value1, &value2, &value3);
468 get_color (value1, panel_config->g_task.font.color);
469 if (value2) panel_config->g_task.font.alpha = (atoi (value2) / 100.0);
470 else panel_config->g_task.font.alpha = 0.1;
471 }
472 else if (strcmp (key, "task_active_font_color") == 0) {
473 extract_values(value, &value1, &value2, &value3);
474 get_color (value1, panel_config->g_task.font_active.color);
475 if (value2) panel_config->g_task.font_active.alpha = (atoi (value2) / 100.0);
476 else panel_config->g_task.font_active.alpha = 0.1;
477 }
478 else if (strcmp (key, "task_background_id") == 0) {
479 int id = atoi (value);
480 Area *a = g_slist_nth_data(list_back, id);
481 memcpy(&panel_config->g_task.area.pix.back, &a->pix.back, sizeof(Color));
482 memcpy(&panel_config->g_task.area.pix.border, &a->pix.border, sizeof(Border));
483 }
484 else if (strcmp (key, "task_active_background_id") == 0) {
485 int id = atoi (value);
486 Area *a = g_slist_nth_data(list_back, id);
487 memcpy(&panel_config->g_task.area.pix_active.back, &a->pix.back, sizeof(Color));
488 memcpy(&panel_config->g_task.area.pix_active.border, &a->pix.border, sizeof(Border));
489 }
490
491 /* Systray */
492 else if (strcmp (key, "systray_padding") == 0) {
493 extract_values(value, &value1, &value2, &value3);
494 systray.area.paddingxlr = systray.area.paddingx = atoi (value1);
495 if (value2) systray.area.paddingy = atoi (value2);
496 if (value3) systray.area.paddingx = atoi (value3);
497 systray.area.on_screen = 1;
498 }
499 else if (strcmp (key, "systray_background_id") == 0) {
500 int id = atoi (value);
501 Area *a = g_slist_nth_data(list_back, id);
502 memcpy(&systray.area.pix.back, &a->pix.back, sizeof(Color));
503 memcpy(&systray.area.pix.border, &a->pix.border, sizeof(Border));
504 }
505
506 /* Mouse actions */
507 else if (strcmp (key, "mouse_middle") == 0)
508 get_action (value, &mouse_middle);
509 else if (strcmp (key, "mouse_right") == 0)
510 get_action (value, &mouse_right);
511 else if (strcmp (key, "mouse_scroll_up") == 0)
512 get_action (value, &mouse_scroll_up);
513 else if (strcmp (key, "mouse_scroll_down") == 0)
514 get_action (value, &mouse_scroll_down);
515
516
517 /* Read tint-0.6 config for backward compatibility */
518 else if (strcmp (key, "panel_mode") == 0) {
519 save_file_config = 1;
520 if (strcmp (value, "single_desktop") == 0) panel_mode = SINGLE_DESKTOP;
521 else panel_mode = MULTI_DESKTOP;
522 }
523 else if (strcmp (key, "panel_rounded") == 0) {
524 Area *a = calloc(1, sizeof(Area));
525 a->pix.border.rounded = atoi (value);
526 list_back = g_slist_append(list_back, a);
527 }
528 else if (strcmp (key, "panel_border_width") == 0) {
529 Area *a = g_slist_last(list_back)->data;
530 a->pix.border.width = atoi (value);
531 }
532 else if (strcmp (key, "panel_background_color") == 0) {
533 Area *a = g_slist_last(list_back)->data;
534 extract_values(value, &value1, &value2, &value3);
535 get_color (value1, a->pix.back.color);
536 if (value2) a->pix.back.alpha = (atoi (value2) / 100.0);
537 else a->pix.back.alpha = 0.5;
538 }
539 else if (strcmp (key, "panel_border_color") == 0) {
540 Area *a = g_slist_last(list_back)->data;
541 extract_values(value, &value1, &value2, &value3);
542 get_color (value1, a->pix.border.color);
543 if (value2) a->pix.border.alpha = (atoi (value2) / 100.0);
544 else a->pix.border.alpha = 0.5;
545 }
546 else if (strcmp (key, "task_text_centered") == 0)
547 panel_config->g_task.centered = atoi (value);
548 else if (strcmp (key, "task_margin") == 0) {
549 panel_config->g_taskbar.paddingxlr = 0;
550 panel_config->g_taskbar.paddingx = atoi (value);
551 }
552 else if (strcmp (key, "task_icon_size") == 0)
553 old_task_icon_size = atoi (value);
554 else if (strcmp (key, "task_rounded") == 0) {
555 area_task = calloc(1, sizeof(Area));
556 area_task->pix.border.rounded = atoi (value);
557 list_back = g_slist_append(list_back, area_task);
558
559 area_task_active = calloc(1, sizeof(Area));
560 area_task_active->pix.border.rounded = atoi (value);
561 list_back = g_slist_append(list_back, area_task_active);
562 }
563 else if (strcmp (key, "task_background_color") == 0) {
564 extract_values(value, &value1, &value2, &value3);
565 get_color (value1, area_task->pix.back.color);
566 if (value2) area_task->pix.back.alpha = (atoi (value2) / 100.0);
567 else area_task->pix.back.alpha = 0.5;
568 }
569 else if (strcmp (key, "task_active_background_color") == 0) {
570 extract_values(value, &value1, &value2, &value3);
571 get_color (value1, area_task_active->pix.back.color);
572 if (value2) area_task_active->pix.back.alpha = (atoi (value2) / 100.0);
573 else area_task_active->pix.back.alpha = 0.5;
574 }
575 else if (strcmp (key, "task_border_width") == 0) {
576 area_task->pix.border.width = atoi (value);
577 area_task_active->pix.border.width = atoi (value);
578 }
579 else if (strcmp (key, "task_border_color") == 0) {
580 extract_values(value, &value1, &value2, &value3);
581 get_color (value1, area_task->pix.border.color);
582 if (value2) area_task->pix.border.alpha = (atoi (value2) / 100.0);
583 else area_task->pix.border.alpha = 0.5;
584 }
585 else if (strcmp (key, "task_active_border_color") == 0) {
586 extract_values(value, &value1, &value2, &value3);
587 get_color (value1, area_task_active->pix.border.color);
588 if (value2) area_task_active->pix.border.alpha = (atoi (value2) / 100.0);
589 else area_task_active->pix.border.alpha = 0.5;
590 }
591
592 else
593 fprintf(stderr, "tint2 : invalid option \"%s\", correct your config file\n", key);
594
595 if (value1) free (value1);
596 if (value2) free (value2);
597 if (value3) free (value3);
598 }
599
600
601 int parse_line (const char *line)
602 {
603 char *a, *b, *key, *value;
604
605 /* Skip useless lines */
606 if ((line[0] == '#') || (line[0] == '\n')) return 0;
607 if (!(a = strchr (line, '='))) return 0;
608
609 /* overwrite '=' with '\0' */
610 a[0] = '\0';
611 key = strdup (line);
612 a++;
613
614 /* overwrite '\n' with '\0' if '\n' present */
615 if ((b = strchr (a, '\n'))) b[0] = '\0';
616
617 value = strdup (a);
618
619 g_strstrip(key);
620 g_strstrip(value);
621
622 add_entry (key, value);
623
624 free (key);
625 free (value);
626 return 1;
627 }
628
629
630 void config_finish ()
631 {
632 if (panel_config->monitor > (server.nb_monitor-1)) {
633 fprintf(stderr, "tint2 exit : monitor %d not found.\n", panel_config->monitor+1);
634 exit(0);
635 }
636
637 // alloc panels
638 int i;
639 if (panel_config->monitor >= 0) {
640 // one monitor
641 nb_panel = 1;
642 panel1 = calloc(nb_panel, sizeof(Panel));
643 memcpy(panel1, panel_config, sizeof(Panel));
644 panel1->monitor = panel_config->monitor;
645 }
646 else {
647 // all monitors
648 nb_panel = server.nb_monitor;
649 panel1 = calloc(nb_panel, sizeof(Panel));
650
651 for (i=0 ; i < nb_panel ; i++) {
652 memcpy(&panel1[i], panel_config, sizeof(Panel));
653 panel1[i].monitor = i;
654 }
655 }
656
657 // TODO: user can configure layout => ordered objects in panel.area.list
658 // clock and systray before taskbar because resize(clock) can resize others object ??
659 init_panel();
660 init_clock();
661 #ifdef ENABLE_BATTERY
662 init_battery();
663 #endif
664 init_systray();
665 init_taskbar();
666 visible_object();
667
668 cleanup_config();
669
670 task_refresh_tasklist();
671 }
672
673
674 int config_read ()
675 {
676 const gchar * const * system_dirs;
677 char *path1;
678 gint i;
679
680 save_file_config = 0;
681
682 // follow XDG specification
683 deb:
684 // check tint2rc in user directory
685 path1 = g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL);
686 if (g_file_test (path1, G_FILE_TEST_EXISTS)) {
687 i = config_read_file (path1);
688 g_free(path1);
689 return i;
690 }
691
692 g_free(path1);
693 if (save_file_config) {
694 fprintf(stderr, "tint2 exit : enable to write $HOME/.config/tint2/tint2rc\n");
695 exit(0);
696 }
697
698 // check old tintrc config file
699 path1 = g_build_filename (g_get_user_config_dir(), "tint", "tintrc", NULL);
700 if (g_file_test (path1, G_FILE_TEST_EXISTS)) {
701 save_file_config = 1;
702 config_read_file (path1);
703 g_free(path1);
704 goto deb;
705 }
706
707 // copy tint2rc from system directory to user directory
708 g_free(path1);
709 char *path2 = 0;
710 system_dirs = g_get_system_config_dirs();
711 for (i = 0; system_dirs[i]; i++) {
712 path2 = g_build_filename(system_dirs[i], "tint2", "tint2rc", NULL);
713
714 if (g_file_test(path2, G_FILE_TEST_EXISTS)) break;
715 g_free (path2);
716 path2 = 0;
717 }
718
719 if (path2) {
720 // copy file in user directory (path1)
721 char *dir = g_build_filename (g_get_user_config_dir(), "tint2", NULL);
722 if (!g_file_test (dir, G_FILE_TEST_IS_DIR)) g_mkdir(dir, 0777);
723 g_free(dir);
724
725 path1 = g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL);
726 copy_file(path2, path1);
727 g_free(path2);
728
729 i = config_read_file (path1);
730 g_free(path1);
731 return i;
732 }
733 return 0;
734 }
735
736
737 int config_read_file (const char *path)
738 {
739 FILE *fp;
740 char line[80];
741
742 if ((fp = fopen(path, "r")) == NULL) return 0;
743 old_task_font = 0;
744 old_time1_font = 0;
745 old_time2_font = 0;
746
747 while (fgets(line, sizeof(line), fp) != NULL)
748 parse_line (line);
749
750 fclose (fp);
751
752 if (save_file_config)
753 save_config();
754
755 if (old_task_font) {
756 g_free(old_task_font);
757 old_task_font = 0;
758 }
759 if (old_time1_font) {
760 g_free(old_time1_font);
761 old_time1_font = 0;
762 }
763 if (old_time2_font) {
764 g_free(old_time2_font);
765 old_time2_font = 0;
766 }
767 return 1;
768 }
769
770
771 void save_config ()
772 {
773 fprintf(stderr, "tint2 : convert user's config file\n");
774
775 char *path, *dir;
776 FILE *fp;
777
778 if (old_task_icon_size) {
779 panel_config->g_task.area.paddingy = ((int)panel_config->initial_height - (2 * panel_config->area.paddingy) - old_task_icon_size) / 2;
780 }
781
782 dir = g_build_filename (g_get_user_config_dir(), "tint2", NULL);
783 if (!g_file_test (dir, G_FILE_TEST_IS_DIR)) g_mkdir(dir, 0777);
784 g_free(dir);
785
786 path = g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL);
787 fp = fopen(path, "w");
788 g_free(path);
789 if (fp == NULL) return;
790
791 fputs("#---------------------------------------------\n", fp);
792 fputs("# TINT2 CONFIG FILE\n", fp);
793 fputs("#---------------------------------------------\n\n", fp);
794 fputs("#---------------------------------------------\n", fp);
795 fputs("# BACKGROUND AND BORDER\n", fp);
796 fputs("#---------------------------------------------\n", fp);
797 GSList *l0;
798 Area *a;
799 l0 = list_back->next;
800 while (l0) {
801 a = l0->data;
802 fprintf(fp, "rounded = %d\n", a->pix.border.rounded);
803 fprintf(fp, "border_width = %d\n", a->pix.border.width);
804 fprintf(fp, "background_color = #%02x%02x%02x %d\n", (int)(a->pix.back.color[0]*255), (int)(a->pix.back.color[1]*255), (int)(a->pix.back.color[2]*255), (int)(a->pix.back.alpha*100));
805 fprintf(fp, "border_color = #%02x%02x%02x %d\n\n", (int)(a->pix.border.color[0]*255), (int)(a->pix.border.color[1]*255), (int)(a->pix.border.color[2]*255), (int)(a->pix.border.alpha*100));
806
807 l0 = l0->next;
808 }
809
810 fputs("#---------------------------------------------\n", fp);
811 fputs("# PANEL\n", fp);
812 fputs("#---------------------------------------------\n", fp);
813 fputs("panel_monitor = all\n", fp);
814 if (panel_position & BOTTOM) fputs("panel_position = bottom", fp);
815 else fputs("panel_position = top", fp);
816 if (panel_position & LEFT) fputs(" left\n", fp);
817 else if (panel_position & RIGHT) fputs(" right\n", fp);
818 else fputs(" center\n", fp);
819 fprintf(fp, "panel_size = %d %d\n", (int)panel_config->initial_width, (int)panel_config->initial_height);
820 fprintf(fp, "panel_margin = %d %d\n", panel_config->marginx, panel_config->marginy);
821 fprintf(fp, "panel_padding = %d %d %d\n", panel_config->area.paddingxlr, panel_config->area.paddingy, panel_config->area.paddingx);
822 fprintf(fp, "font_shadow = %d\n", panel_config->g_task.font_shadow);
823 fputs("panel_background_id = 1\n", fp);
824
825 fputs("\n#---------------------------------------------\n", fp);
826 fputs("# TASKBAR\n", fp);
827 fputs("#---------------------------------------------\n", fp);
828 if (panel_mode == MULTI_DESKTOP) fputs("taskbar_mode = multi_desktop\n", fp);
829 else fputs("taskbar_mode = single_desktop\n", fp);
830 fprintf(fp, "taskbar_padding = 0 0 %d\n", panel_config->g_taskbar.paddingx);
831 fputs("taskbar_background_id = 0\n", fp);
832
833 fputs("\n#---------------------------------------------\n", fp);
834 fputs("# TASK\n", fp);
835 fputs("#---------------------------------------------\n", fp);
836 if (old_task_icon_size) fputs("task_icon = 1\n", fp);
837 else fputs("task_icon = 0\n", fp);
838 fputs("task_text = 1\n", fp);
839 fprintf(fp, "task_width = %d\n", panel_config->g_task.maximum_width);
840 fprintf(fp, "task_centered = %d\n", panel_config->g_task.centered);
841 fprintf(fp, "task_padding = %d %d\n", panel_config->g_task.area.paddingx, panel_config->g_task.area.paddingy);
842 fprintf(fp, "task_font = %s\n", old_task_font);
843 fprintf(fp, "task_font_color = #%02x%02x%02x %d\n", (int)(panel_config->g_task.font.color[0]*255), (int)(panel_config->g_task.font.color[1]*255), (int)(panel_config->g_task.font.color[2]*255), (int)(panel_config->g_task.font.alpha*100));
844 fprintf(fp, "task_active_font_color = #%02x%02x%02x %d\n", (int)(panel_config->g_task.font_active.color[0]*255), (int)(panel_config->g_task.font_active.color[1]*255), (int)(panel_config->g_task.font_active.color[2]*255), (int)(panel_config->g_task.font_active.alpha*100));
845 fputs("task_background_id = 2\n", fp);
846 fputs("task_active_background_id = 3\n", fp);
847
848 fputs("\n#---------------------------------------------\n", fp);
849 fputs("# SYSTRAYBAR\n", fp);
850 fputs("#---------------------------------------------\n", fp);
851 fputs("systray_padding = 4 3 4\n", fp);
852 fputs("systray_background_id = 0\n", fp);
853
854 fputs("\n#---------------------------------------------\n", fp);
855 fputs("# CLOCK\n", fp);
856 fputs("#---------------------------------------------\n", fp);
857 if (time1_format) fprintf(fp, "time1_format = %s\n", time1_format);
858 else fputs("#time1_format = %H:%M\n", fp);
859 fprintf(fp, "time1_font = %s\n", old_time1_font);
860 if (time2_format) fprintf(fp, "time2_format = %s\n", time2_format);
861 else fputs("#time2_format = %A %d %B\n", fp);
862 fprintf(fp, "time2_font = %s\n", old_time2_font);
863 fprintf(fp, "clock_font_color = #%02x%02x%02x %d\n", (int)(panel_config->clock.font.color[0]*255), (int)(panel_config->clock.font.color[1]*255), (int)(panel_config->clock.font.color[2]*255), (int)(panel_config->clock.font.alpha*100));
864 fputs("clock_padding = 2 2\n", fp);
865 fputs("clock_background_id = 0\n", fp);
866
867 #ifdef ENABLE_BATTERY
868 fputs("\n#---------------------------------------------\n", fp);
869 fputs("# BATTERY\n", fp);
870 fputs("#---------------------------------------------\n", fp);
871 fprintf(fp, "battery = %d\n", panel_config->battery.area.on_screen);
872 fprintf(fp, "battery_low_status = %d\n", battery_low_status);
873 fprintf(fp, "battery_low_cmd = %s\n", battery_low_cmd);
874 fprintf(fp, "bat1_font = %s\n", old_bat1_font);
875 fprintf(fp, "bat2_font = %s\n", old_bat2_font);
876 fprintf(fp, "battery_font_color = #%02x%02x%02x %d\n", (int)(panel_config->battery.font.color[0]*255), (int)(panel_config->battery.font.color[1]*255), (int)(panel_config->battery.font.color[2]*255), (int)(panel_config->battery.font.alpha*100));
877 fputs("battery_padding = 2 2\n", fp);
878 fputs("battery_background_id = 0\n", fp);
879 #endif
880
881 fputs("\n#---------------------------------------------\n", fp);
882 fputs("# MOUSE ACTION ON TASK\n", fp);
883 fputs("#---------------------------------------------\n", fp);
884 if (mouse_middle == NONE) fputs("mouse_middle = none\n", fp);
885 else if (mouse_middle == CLOSE) fputs("mouse_middle = close\n", fp);
886 else if (mouse_middle == TOGGLE) fputs("mouse_middle = toggle\n", fp);
887 else if (mouse_middle == ICONIFY) fputs("mouse_middle = iconify\n", fp);
888 else if (mouse_middle == SHADE) fputs("mouse_middle = shade\n", fp);
889 else fputs("mouse_middle = toggle_iconify\n", fp);
890
891 if (mouse_right == NONE) fputs("mouse_right = none\n", fp);
892 else if (mouse_right == CLOSE) fputs("mouse_right = close\n", fp);
893 else if (mouse_right == TOGGLE) fputs("mouse_right = toggle\n", fp);
894 else if (mouse_right == ICONIFY) fputs("mouse_right = iconify\n", fp);
895 else if (mouse_right == SHADE) fputs("mouse_right = shade\n", fp);
896 else fputs("mouse_right = toggle_iconify\n", fp);
897
898 if (mouse_scroll_up == NONE) fputs("mouse_scroll_up = none\n", fp);
899 else if (mouse_scroll_up == CLOSE) fputs("mouse_scroll_up = close\n", fp);
900 else if (mouse_scroll_up == TOGGLE) fputs("mouse_scroll_up = toggle\n", fp);
901 else if (mouse_scroll_up == ICONIFY) fputs("mouse_scroll_up = iconify\n", fp);
902 else if (mouse_scroll_up == SHADE) fputs("mouse_scroll_up = shade\n", fp);
903 else fputs("mouse_scroll_up = toggle_iconify\n", fp);
904
905 if (mouse_scroll_down == NONE) fputs("mouse_scroll_down = none\n", fp);
906 else if (mouse_scroll_down == CLOSE) fputs("mouse_scroll_down = close\n", fp);
907 else if (mouse_scroll_down == TOGGLE) fputs("mouse_scroll_down = toggle\n", fp);
908 else if (mouse_scroll_down == ICONIFY) fputs("mouse_scroll_down = iconify\n", fp);
909 else if (mouse_scroll_down == SHADE) fputs("mouse_scroll_down = shade\n", fp);
910 else fputs("mouse_scroll_down = toggle_iconify\n", fp);
911
912 fputs("\n\n", fp);
913 fclose (fp);
914 }
915
This page took 0.081514 seconds and 3 git commands to generate.