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