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