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