]> Dogcows Code - chaz/tint2/blob - src/config.c
fixed issue 130 by maato
[chaz/tint2] / src / config.c
1 /**************************************************************************
2 *
3 * Tint2 : read/write config file
4 *
5 * Copyright (C) 2007 Pål Staurland (staura@gmail.com)
6 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 **************************************************************************/
20
21 #include <sys/stat.h>
22 #include <sys/time.h>
23 #include <unistd.h>
24 #include <cairo.h>
25 #include <cairo-xlib.h>
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <X11/Xatom.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <glib/gstdio.h>
34 #include <pango/pangocairo.h>
35 #include <Imlib2.h>
36
37 #include "common.h"
38 #include "server.h"
39 #include "task.h"
40 #include "taskbar.h"
41 #include "systraybar.h"
42 #include "clock.h"
43 #include "panel.h"
44 #include "config.h"
45 #include "window.h"
46
47 #ifdef ENABLE_BATTERY
48 #include "battery.h"
49 #endif
50
51 // global path
52 char *config_path = 0;
53 char *thumbnail_path = 0;
54
55 // --------------------------------------------------
56 // backward compatibility
57 static int save_file_config;
58 static int old_task_icon_size;
59 static char *old_task_font;
60 static char *old_time1_font;
61 static char *old_time2_font;
62 static Area *area_task, *area_task_active;
63
64 #ifdef ENABLE_BATTERY
65 static char *old_bat1_font;
66 static char *old_bat2_font;
67 #endif
68
69 // temporary panel
70 static Panel *panel_config = 0;
71
72 // temporary list of background
73 static GSList *list_back;
74
75
76 void init_config()
77 {
78 cleanup_panel();
79
80 // get monitor and desktop config
81 get_monitors_and_desktops();
82
83 // append full transparency background
84 list_back = g_slist_append(0, calloc(1, sizeof(Area)));
85
86 panel_config = calloc(1, sizeof(Panel));
87 systray.sort = 1;
88 // window manager's menu default value == false
89 wm_menu = 0;
90 max_tick_urgent = 7;
91 }
92
93
94 void cleanup_config()
95 {
96 free(panel_config);
97 panel_config = 0;
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, "urgent_nb_of_blink") == 0)
341 max_tick_urgent = (atoi (value) * 2) + 1;
342
343 /* Battery */
344 else if (strcmp (key, "battery") == 0) {
345 #ifdef ENABLE_BATTERY
346 if(atoi(value) == 1)
347 panel_config->battery.area.on_screen = 1;
348 #else
349 if(atoi(value) == 1)
350 printf("tint2 is build without battery support\n");
351 #endif
352 }
353 else if (strcmp (key, "battery_low_status") == 0) {
354 #ifdef ENABLE_BATTERY
355 battery_low_status = atoi(value);
356 if(battery_low_status < 0 || battery_low_status > 100)
357 battery_low_status = 0;
358 #endif
359 }
360 else if (strcmp (key, "battery_low_cmd") == 0) {
361 #ifdef ENABLE_BATTERY
362 if (battery_low_cmd) g_free(battery_low_cmd);
363 if (strlen(value) > 0) battery_low_cmd = strdup (value);
364 else battery_low_cmd = 0;
365 #endif
366 }
367 else if (strcmp (key, "bat1_font") == 0) {
368 #ifdef ENABLE_BATTERY
369 if (save_file_config) old_bat1_font = strdup (value);
370 if (bat1_font_desc) pango_font_description_free(bat1_font_desc);
371 bat1_font_desc = pango_font_description_from_string (value);
372 #endif
373 }
374 else if (strcmp (key, "bat2_font") == 0) {
375 #ifdef ENABLE_BATTERY
376 if (save_file_config) old_bat2_font = strdup (value);
377 if (bat2_font_desc) pango_font_description_free(bat2_font_desc);
378 bat2_font_desc = pango_font_description_from_string (value);
379 #endif
380 }
381 else if (strcmp (key, "battery_font_color") == 0) {
382 #ifdef ENABLE_BATTERY
383 extract_values(value, &value1, &value2, &value3);
384 get_color (value1, panel_config->battery.font.color);
385 if (value2) panel_config->battery.font.alpha = (atoi (value2) / 100.0);
386 else panel_config->battery.font.alpha = 0.5;
387 #endif
388 }
389 else if (strcmp (key, "battery_padding") == 0) {
390 #ifdef ENABLE_BATTERY
391 extract_values(value, &value1, &value2, &value3);
392 panel_config->battery.area.paddingxlr = panel_config->battery.area.paddingx = atoi (value1);
393 if (value2) panel_config->battery.area.paddingy = atoi (value2);
394 if (value3) panel_config->battery.area.paddingx = atoi (value3);
395 #endif
396 }
397 else if (strcmp (key, "battery_background_id") == 0) {
398 #ifdef ENABLE_BATTERY
399 int id = atoi (value);
400 Area *a = g_slist_nth_data(list_back, id);
401 memcpy(&panel_config->battery.area.pix.back, &a->pix.back, sizeof(Color));
402 memcpy(&panel_config->battery.area.pix.border, &a->pix.border, sizeof(Border));
403 #endif
404 }
405
406 /* Clock */
407 else if (strcmp (key, "time1_format") == 0) {
408 if (time1_format) g_free(time1_format);
409 if (strlen(value) > 0) {
410 time1_format = strdup (value);
411 panel_config->clock.area.on_screen = 1;
412 }
413 else {
414 time1_format = 0;
415 panel_config->clock.area.on_screen = 0;
416 }
417 }
418 else if (strcmp (key, "time2_format") == 0) {
419 if (time2_format) g_free(time2_format);
420 if (strlen(value) > 0) time2_format = strdup (value);
421 else time2_format = 0;
422 }
423 else if (strcmp (key, "time1_font") == 0) {
424 if (save_file_config) old_time1_font = strdup (value);
425 if (time1_font_desc) pango_font_description_free(time1_font_desc);
426 time1_font_desc = pango_font_description_from_string (value);
427 }
428 else if (strcmp (key, "time2_font") == 0) {
429 if (save_file_config) old_time2_font = strdup (value);
430 if (time2_font_desc) pango_font_description_free(time2_font_desc);
431 time2_font_desc = pango_font_description_from_string (value);
432 }
433 else if (strcmp (key, "clock_font_color") == 0) {
434 extract_values(value, &value1, &value2, &value3);
435 get_color (value1, panel_config->clock.font.color);
436 if (value2) panel_config->clock.font.alpha = (atoi (value2) / 100.0);
437 else panel_config->clock.font.alpha = 0.5;
438 }
439 else if (strcmp (key, "clock_padding") == 0) {
440 extract_values(value, &value1, &value2, &value3);
441 panel_config->clock.area.paddingxlr = panel_config->clock.area.paddingx = atoi (value1);
442 if (value2) panel_config->clock.area.paddingy = atoi (value2);
443 if (value3) panel_config->clock.area.paddingx = atoi (value3);
444 }
445 else if (strcmp (key, "clock_background_id") == 0) {
446 int id = atoi (value);
447 Area *a = g_slist_nth_data(list_back, id);
448 memcpy(&panel_config->clock.area.pix.back, &a->pix.back, sizeof(Color));
449 memcpy(&panel_config->clock.area.pix.border, &a->pix.border, sizeof(Border));
450 }
451 else if (strcmp(key, "clock_lclick_command") == 0) {
452 if (clock_lclick_command) g_free(clock_lclick_command);
453 if (strlen(value) > 0) clock_lclick_command = strdup(value);
454 else clock_lclick_command = 0;
455 }
456 else if (strcmp(key, "clock_rclick_command") == 0) {
457 if (clock_rclick_command) g_free(clock_rclick_command);
458 if (strlen(value) > 0) clock_rclick_command = strdup(value);
459 else clock_rclick_command = 0;
460 }
461
462 /* Taskbar */
463 else if (strcmp (key, "taskbar_mode") == 0) {
464 if (strcmp (value, "multi_desktop") == 0) panel_mode = MULTI_DESKTOP;
465 else panel_mode = SINGLE_DESKTOP;
466 }
467 else if (strcmp (key, "taskbar_padding") == 0) {
468 extract_values(value, &value1, &value2, &value3);
469 panel_config->g_taskbar.paddingxlr = panel_config->g_taskbar.paddingx = atoi (value1);
470 if (value2) panel_config->g_taskbar.paddingy = atoi (value2);
471 if (value3) panel_config->g_taskbar.paddingx = atoi (value3);
472 }
473 else if (strcmp (key, "taskbar_background_id") == 0) {
474 int id = atoi (value);
475 Area *a = g_slist_nth_data(list_back, id);
476 memcpy(&panel_config->g_taskbar.pix.back, &a->pix.back, sizeof(Color));
477 memcpy(&panel_config->g_taskbar.pix.border, &a->pix.border, sizeof(Border));
478 }
479
480 /* Task */
481 else if (strcmp (key, "task_text") == 0)
482 panel_config->g_task.text = atoi (value);
483 else if (strcmp (key, "task_icon") == 0)
484 panel_config->g_task.icon = atoi (value);
485 else if (strcmp (key, "task_centered") == 0)
486 panel_config->g_task.centered = atoi (value);
487 else if (strcmp (key, "task_width") == 0) {
488 // old parameter : just for backward compatibility
489 panel_config->g_task.maximum_width = atoi (value);
490 panel_config->g_task.maximum_height = 30;
491 }
492 else if (strcmp (key, "task_maximum_size") == 0) {
493 extract_values(value, &value1, &value2, &value3);
494 panel_config->g_task.maximum_width = atoi (value1);
495 panel_config->g_task.maximum_height = 30;
496 if (value2)
497 panel_config->g_task.maximum_height = atoi (value2);
498 }
499 else if (strcmp (key, "task_padding") == 0) {
500 extract_values(value, &value1, &value2, &value3);
501 panel_config->g_task.area.paddingxlr = panel_config->g_task.area.paddingx = atoi (value1);
502 if (value2) panel_config->g_task.area.paddingy = atoi (value2);
503 if (value3) panel_config->g_task.area.paddingx = atoi (value3);
504 }
505 else if (strcmp (key, "task_font") == 0) {
506 if (save_file_config) old_task_font = strdup (value);
507 if (panel_config->g_task.font_desc) pango_font_description_free(panel_config->g_task.font_desc);
508 panel_config->g_task.font_desc = pango_font_description_from_string (value);
509 }
510 else if (strcmp (key, "task_font_color") == 0) {
511 extract_values(value, &value1, &value2, &value3);
512 get_color (value1, panel_config->g_task.font.color);
513 if (value2) panel_config->g_task.font.alpha = (atoi (value2) / 100.0);
514 else panel_config->g_task.font.alpha = 0.1;
515 }
516 else if (strcmp (key, "task_active_font_color") == 0) {
517 extract_values(value, &value1, &value2, &value3);
518 get_color (value1, panel_config->g_task.font_active.color);
519 if (value2) panel_config->g_task.font_active.alpha = (atoi (value2) / 100.0);
520 else panel_config->g_task.font_active.alpha = 0.1;
521 }
522 else if (strcmp (key, "task_icon_hsb") == 0) {
523 extract_values(value, &value1, &value2, &value3);
524 panel_config->g_task.hue = atoi(value1);
525 panel_config->g_task.saturation = atoi(value2);
526 panel_config->g_task.brightness = atoi(value3);
527 }
528 else if (strcmp (key, "task_active_icon_hsb") == 0) {
529 extract_values(value, &value1, &value2, &value3);
530 panel_config->g_task.hue_active = atoi(value1);
531 panel_config->g_task.saturation_active = atoi(value2);
532 panel_config->g_task.brightness_active = atoi(value3);
533 }
534 else if (strcmp (key, "task_background_id") == 0) {
535 int id = atoi (value);
536 Area *a = g_slist_nth_data(list_back, id);
537 memcpy(&panel_config->g_task.area.pix.back, &a->pix.back, sizeof(Color));
538 memcpy(&panel_config->g_task.area.pix.border, &a->pix.border, sizeof(Border));
539 }
540 else if (strcmp (key, "task_active_background_id") == 0) {
541 int id = atoi (value);
542 Area *a = g_slist_nth_data(list_back, id);
543 memcpy(&panel_config->g_task.area.pix_active.back, &a->pix.back, sizeof(Color));
544 memcpy(&panel_config->g_task.area.pix_active.border, &a->pix.border, sizeof(Border));
545 }
546
547 /* Systray */
548 else if (strcmp (key, "systray_padding") == 0) {
549 extract_values(value, &value1, &value2, &value3);
550 systray.area.paddingxlr = systray.area.paddingx = atoi (value1);
551 if (value2) systray.area.paddingy = atoi (value2);
552 if (value3) systray.area.paddingx = atoi (value3);
553 systray.area.on_screen = 1;
554 }
555 else if (strcmp (key, "systray_background_id") == 0) {
556 int id = atoi (value);
557 Area *a = g_slist_nth_data(list_back, id);
558 memcpy(&systray.area.pix.back, &a->pix.back, sizeof(Color));
559 memcpy(&systray.area.pix.border, &a->pix.border, sizeof(Border));
560 }
561 else if (strcmp(key, "systray_sort") == 0) {
562 if (strcmp(value, "desc") == 0)
563 systray.sort = -1;
564 else
565 systray.sort = 1;
566 }
567
568 /* Mouse actions */
569 else if (strcmp (key, "mouse_middle") == 0)
570 get_action (value, &mouse_middle);
571 else if (strcmp (key, "mouse_right") == 0)
572 get_action (value, &mouse_right);
573 else if (strcmp (key, "mouse_scroll_up") == 0)
574 get_action (value, &mouse_scroll_up);
575 else if (strcmp (key, "mouse_scroll_down") == 0)
576 get_action (value, &mouse_scroll_down);
577
578
579 /* Read tint-0.6 config for backward compatibility */
580 else if (strcmp (key, "panel_mode") == 0) {
581 save_file_config = 1;
582 if (strcmp (value, "single_desktop") == 0) panel_mode = SINGLE_DESKTOP;
583 else panel_mode = MULTI_DESKTOP;
584 }
585 else if (strcmp (key, "panel_rounded") == 0) {
586 Area *a = calloc(1, sizeof(Area));
587 a->pix.border.rounded = atoi (value);
588 list_back = g_slist_append(list_back, a);
589 }
590 else if (strcmp (key, "panel_border_width") == 0) {
591 Area *a = g_slist_last(list_back)->data;
592 a->pix.border.width = atoi (value);
593 }
594 else if (strcmp (key, "panel_background_color") == 0) {
595 Area *a = g_slist_last(list_back)->data;
596 extract_values(value, &value1, &value2, &value3);
597 get_color (value1, a->pix.back.color);
598 if (value2) a->pix.back.alpha = (atoi (value2) / 100.0);
599 else a->pix.back.alpha = 0.5;
600 }
601 else if (strcmp (key, "panel_border_color") == 0) {
602 Area *a = g_slist_last(list_back)->data;
603 extract_values(value, &value1, &value2, &value3);
604 get_color (value1, a->pix.border.color);
605 if (value2) a->pix.border.alpha = (atoi (value2) / 100.0);
606 else a->pix.border.alpha = 0.5;
607 }
608 else if (strcmp (key, "task_text_centered") == 0)
609 panel_config->g_task.centered = atoi (value);
610 else if (strcmp (key, "task_margin") == 0) {
611 panel_config->g_taskbar.paddingxlr = 0;
612 panel_config->g_taskbar.paddingx = atoi (value);
613 }
614 else if (strcmp (key, "task_icon_size") == 0)
615 old_task_icon_size = atoi (value);
616 else if (strcmp (key, "task_rounded") == 0) {
617 area_task = calloc(1, sizeof(Area));
618 area_task->pix.border.rounded = atoi (value);
619 list_back = g_slist_append(list_back, area_task);
620
621 area_task_active = calloc(1, sizeof(Area));
622 area_task_active->pix.border.rounded = atoi (value);
623 list_back = g_slist_append(list_back, area_task_active);
624 }
625 else if (strcmp (key, "task_background_color") == 0) {
626 extract_values(value, &value1, &value2, &value3);
627 get_color (value1, area_task->pix.back.color);
628 if (value2) area_task->pix.back.alpha = (atoi (value2) / 100.0);
629 else area_task->pix.back.alpha = 0.5;
630 }
631 else if (strcmp (key, "task_active_background_color") == 0) {
632 extract_values(value, &value1, &value2, &value3);
633 get_color (value1, area_task_active->pix.back.color);
634 if (value2) area_task_active->pix.back.alpha = (atoi (value2) / 100.0);
635 else area_task_active->pix.back.alpha = 0.5;
636 }
637 else if (strcmp (key, "task_border_width") == 0) {
638 area_task->pix.border.width = atoi (value);
639 area_task_active->pix.border.width = atoi (value);
640 }
641 else if (strcmp (key, "task_border_color") == 0) {
642 extract_values(value, &value1, &value2, &value3);
643 get_color (value1, area_task->pix.border.color);
644 if (value2) area_task->pix.border.alpha = (atoi (value2) / 100.0);
645 else area_task->pix.border.alpha = 0.5;
646 }
647 else if (strcmp (key, "task_active_border_color") == 0) {
648 extract_values(value, &value1, &value2, &value3);
649 get_color (value1, area_task_active->pix.border.color);
650 if (value2) area_task_active->pix.border.alpha = (atoi (value2) / 100.0);
651 else area_task_active->pix.border.alpha = 0.5;
652 }
653
654 else
655 fprintf(stderr, "tint2 : invalid option \"%s\", correct your config file\n", key);
656
657 if (value1) free (value1);
658 if (value2) free (value2);
659 if (value3) free (value3);
660 }
661
662
663 int parse_line (const char *line)
664 {
665 char *a, *b, *key, *value;
666
667 /* Skip useless lines */
668 if ((line[0] == '#') || (line[0] == '\n')) return 0;
669 if (!(a = strchr (line, '='))) return 0;
670
671 /* overwrite '=' with '\0' */
672 a[0] = '\0';
673 key = strdup (line);
674 a++;
675
676 /* overwrite '\n' with '\0' if '\n' present */
677 if ((b = strchr (a, '\n'))) b[0] = '\0';
678
679 value = strdup (a);
680
681 g_strstrip(key);
682 g_strstrip(value);
683
684 add_entry (key, value);
685
686 free (key);
687 free (value);
688 return 1;
689 }
690
691
692 void config_finish ()
693 {
694 if (panel_config->monitor > (server.nb_monitor-1)) {
695 // server.nb_monitor minimum value is 1 (see get_monitors_and_desktops())
696 // and panel_config->monitor is higher
697 fprintf(stderr, "warning : monitor not found. tint2 default to monitor 1.\n");
698 panel_config->monitor = 0;
699 }
700
701 // alloc panels
702 int i;
703 if (panel_config->monitor >= 0) {
704 // one monitor
705 nb_panel = 1;
706 panel1 = calloc(nb_panel, sizeof(Panel));
707 memcpy(panel1, panel_config, sizeof(Panel));
708 panel1->monitor = panel_config->monitor;
709 }
710 else {
711 // all monitors
712 nb_panel = server.nb_monitor;
713 panel1 = calloc(nb_panel, sizeof(Panel));
714
715 for (i=0 ; i < nb_panel ; i++) {
716 memcpy(&panel1[i], panel_config, sizeof(Panel));
717 panel1[i].monitor = i;
718 }
719 }
720
721 // TODO: user can configure layout => ordered objects in panel.area.list
722 // clock and systray before taskbar because resize(clock) can resize others object ??
723 init_panel();
724 init_clock();
725 #ifdef ENABLE_BATTERY
726 init_battery();
727 #endif
728 init_systray();
729 init_taskbar();
730 visible_object();
731
732 cleanup_config();
733
734 task_refresh_tasklist();
735 }
736
737
738 int config_read ()
739 {
740 const gchar * const * system_dirs;
741 char *path1;
742 gint i;
743
744 save_file_config = 0;
745
746 // follow XDG specification
747 deb:
748 // check tint2rc in user directory
749 path1 = g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL);
750 if (g_file_test (path1, G_FILE_TEST_EXISTS)) {
751 i = config_read_file (path1);
752 config_path = strdup(path1);
753 g_free(path1);
754 return i;
755 }
756
757 g_free(path1);
758 if (save_file_config) {
759 fprintf(stderr, "tint2 exit : enable to write $HOME/.config/tint2/tint2rc\n");
760 exit(0);
761 }
762
763 // check old tintrc config file
764 path1 = g_build_filename (g_get_user_config_dir(), "tint", "tintrc", NULL);
765 if (g_file_test (path1, G_FILE_TEST_EXISTS)) {
766 save_file_config = 1;
767 config_read_file (path1);
768 g_free(path1);
769 goto deb;
770 }
771
772 // copy tint2rc from system directory to user directory
773 g_free(path1);
774 char *path2 = 0;
775 system_dirs = g_get_system_config_dirs();
776 for (i = 0; system_dirs[i]; i++) {
777 path2 = g_build_filename(system_dirs[i], "tint2", "tint2rc", NULL);
778
779 if (g_file_test(path2, G_FILE_TEST_EXISTS)) break;
780 g_free (path2);
781 path2 = 0;
782 }
783
784 if (path2) {
785 // copy file in user directory (path1)
786 char *dir = g_build_filename (g_get_user_config_dir(), "tint2", NULL);
787 if (!g_file_test (dir, G_FILE_TEST_IS_DIR)) g_mkdir(dir, 0777);
788 g_free(dir);
789
790 path1 = g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL);
791 copy_file(path2, path1);
792 g_free(path2);
793
794 i = config_read_file (path1);
795 config_path = strdup(path1);
796 g_free(path1);
797 return i;
798 }
799 return 0;
800 }
801
802
803 int config_read_file (const char *path)
804 {
805 FILE *fp;
806 char line[80];
807
808 if ((fp = fopen(path, "r")) == NULL) return 0;
809 old_task_font = 0;
810 old_time1_font = 0;
811 old_time2_font = 0;
812
813 while (fgets(line, sizeof(line), fp) != NULL)
814 parse_line (line);
815
816 fclose (fp);
817
818 if (save_file_config)
819 save_config();
820
821 if (old_task_font) {
822 g_free(old_task_font);
823 old_task_font = 0;
824 }
825 if (old_time1_font) {
826 g_free(old_time1_font);
827 old_time1_font = 0;
828 }
829 if (old_time2_font) {
830 g_free(old_time2_font);
831 old_time2_font = 0;
832 }
833 return 1;
834 }
835
836
837 void save_config ()
838 {
839 fprintf(stderr, "tint2 : convert user's config file\n");
840
841 char *path, *dir;
842 FILE *fp;
843
844 if (old_task_icon_size) {
845 panel_config->g_task.area.paddingy = ((int)panel_config->initial_height - (2 * panel_config->area.paddingy) - old_task_icon_size) / 2;
846 }
847
848 dir = g_build_filename (g_get_user_config_dir(), "tint2", NULL);
849 if (!g_file_test (dir, G_FILE_TEST_IS_DIR)) g_mkdir(dir, 0777);
850 g_free(dir);
851
852 path = g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL);
853 fp = fopen(path, "w");
854 g_free(path);
855 if (fp == NULL) return;
856
857 fputs("#---------------------------------------------\n", fp);
858 fputs("# TINT2 CONFIG FILE\n", fp);
859 fputs("#---------------------------------------------\n\n", fp);
860 fputs("#---------------------------------------------\n", fp);
861 fputs("# BACKGROUND AND BORDER\n", fp);
862 fputs("#---------------------------------------------\n", fp);
863 GSList *l0;
864 Area *a;
865 l0 = list_back->next;
866 while (l0) {
867 a = l0->data;
868 fprintf(fp, "rounded = %d\n", a->pix.border.rounded);
869 fprintf(fp, "border_width = %d\n", a->pix.border.width);
870 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));
871 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));
872
873 l0 = l0->next;
874 }
875
876 fputs("#---------------------------------------------\n", fp);
877 fputs("# PANEL\n", fp);
878 fputs("#---------------------------------------------\n", fp);
879 fputs("panel_monitor = all\n", fp);
880 if (panel_position & BOTTOM) fputs("panel_position = bottom", fp);
881 else fputs("panel_position = top", fp);
882 if (panel_position & LEFT) fputs(" left horizontal\n", fp);
883 else if (panel_position & RIGHT) fputs(" right horizontal\n", fp);
884 else fputs(" center horizontal\n", fp);
885 fprintf(fp, "panel_size = %d %d\n", (int)panel_config->initial_width, (int)panel_config->initial_height);
886 fprintf(fp, "panel_margin = %d %d\n", panel_config->marginx, panel_config->marginy);
887 fprintf(fp, "panel_padding = %d %d %d\n", panel_config->area.paddingxlr, panel_config->area.paddingy, panel_config->area.paddingx);
888 fprintf(fp, "font_shadow = %d\n", panel_config->g_task.font_shadow);
889 fputs("panel_background_id = 1\n", fp);
890 fputs("wm_menu = 0\n", fp);
891
892 fputs("\n#---------------------------------------------\n", fp);
893 fputs("# TASKBAR\n", fp);
894 fputs("#---------------------------------------------\n", fp);
895 if (panel_mode == MULTI_DESKTOP) fputs("taskbar_mode = multi_desktop\n", fp);
896 else fputs("taskbar_mode = single_desktop\n", fp);
897 fprintf(fp, "taskbar_padding = 0 0 %d\n", panel_config->g_taskbar.paddingx);
898 fputs("taskbar_background_id = 0\n", fp);
899
900 fputs("\n#---------------------------------------------\n", fp);
901 fputs("# TASK\n", fp);
902 fputs("#---------------------------------------------\n", fp);
903 if (old_task_icon_size) fputs("task_icon = 1\n", fp);
904 else fputs("task_icon = 0\n", fp);
905 fputs("task_text = 1\n", fp);
906 fprintf(fp, "task_maximum_size = %d %d\n", panel_config->g_task.maximum_width, panel_config->g_task.maximum_height);
907 fprintf(fp, "task_centered = %d\n", panel_config->g_task.centered);
908 fprintf(fp, "task_padding = %d %d\n", panel_config->g_task.area.paddingx, panel_config->g_task.area.paddingy);
909 fprintf(fp, "task_font = %s\n", old_task_font);
910 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));
911 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));
912 fputs("task_background_id = 2\n", fp);
913 fputs("task_active_background_id = 3\n", fp);
914
915 fputs("\n#---------------------------------------------\n", fp);
916 fputs("# SYSTRAYBAR\n", fp);
917 fputs("#---------------------------------------------\n", fp);
918 fputs("systray_padding = 4 3 4\n", fp);
919 fputs("systray_background_id = 0\n", fp);
920
921 fputs("\n#---------------------------------------------\n", fp);
922 fputs("# CLOCK\n", fp);
923 fputs("#---------------------------------------------\n", fp);
924 if (time1_format) fprintf(fp, "time1_format = %s\n", time1_format);
925 else fputs("#time1_format = %H:%M\n", fp);
926 fprintf(fp, "time1_font = %s\n", old_time1_font);
927 if (time2_format) fprintf(fp, "time2_format = %s\n", time2_format);
928 else fputs("#time2_format = %A %d %B\n", fp);
929 fprintf(fp, "time2_font = %s\n", old_time2_font);
930 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));
931 fputs("clock_padding = 2 2\n", fp);
932 fputs("clock_background_id = 0\n", fp);
933 fputs("#clock_lclick_command = xclock\n", fp);
934 fputs("clock_rclick_command = orage\n", fp);
935
936 #ifdef ENABLE_BATTERY
937 fputs("\n#---------------------------------------------\n", fp);
938 fputs("# BATTERY\n", fp);
939 fputs("#---------------------------------------------\n", fp);
940 fprintf(fp, "battery = %d\n", panel_config->battery.area.on_screen);
941 fprintf(fp, "battery_low_status = %d\n", battery_low_status);
942 fprintf(fp, "battery_low_cmd = %s\n", battery_low_cmd);
943 fprintf(fp, "bat1_font = %s\n", old_bat1_font);
944 fprintf(fp, "bat2_font = %s\n", old_bat2_font);
945 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));
946 fputs("battery_padding = 2 2\n", fp);
947 fputs("battery_background_id = 0\n", fp);
948 #endif
949
950 fputs("\n#---------------------------------------------\n", fp);
951 fputs("# MOUSE ACTION ON TASK\n", fp);
952 fputs("#---------------------------------------------\n", fp);
953 if (mouse_middle == NONE) fputs("mouse_middle = none\n", fp);
954 else if (mouse_middle == CLOSE) fputs("mouse_middle = close\n", fp);
955 else if (mouse_middle == TOGGLE) fputs("mouse_middle = toggle\n", fp);
956 else if (mouse_middle == ICONIFY) fputs("mouse_middle = iconify\n", fp);
957 else if (mouse_middle == SHADE) fputs("mouse_middle = shade\n", fp);
958 else fputs("mouse_middle = toggle_iconify\n", fp);
959
960 if (mouse_right == NONE) fputs("mouse_right = none\n", fp);
961 else if (mouse_right == CLOSE) fputs("mouse_right = close\n", fp);
962 else if (mouse_right == TOGGLE) fputs("mouse_right = toggle\n", fp);
963 else if (mouse_right == ICONIFY) fputs("mouse_right = iconify\n", fp);
964 else if (mouse_right == SHADE) fputs("mouse_right = shade\n", fp);
965 else fputs("mouse_right = toggle_iconify\n", fp);
966
967 if (mouse_scroll_up == NONE) fputs("mouse_scroll_up = none\n", fp);
968 else if (mouse_scroll_up == CLOSE) fputs("mouse_scroll_up = close\n", fp);
969 else if (mouse_scroll_up == TOGGLE) fputs("mouse_scroll_up = toggle\n", fp);
970 else if (mouse_scroll_up == ICONIFY) fputs("mouse_scroll_up = iconify\n", fp);
971 else if (mouse_scroll_up == SHADE) fputs("mouse_scroll_up = shade\n", fp);
972 else fputs("mouse_scroll_up = toggle_iconify\n", fp);
973
974 if (mouse_scroll_down == NONE) fputs("mouse_scroll_down = none\n", fp);
975 else if (mouse_scroll_down == CLOSE) fputs("mouse_scroll_down = close\n", fp);
976 else if (mouse_scroll_down == TOGGLE) fputs("mouse_scroll_down = toggle\n", fp);
977 else if (mouse_scroll_down == ICONIFY) fputs("mouse_scroll_down = iconify\n", fp);
978 else if (mouse_scroll_down == SHADE) fputs("mouse_scroll_down = shade\n", fp);
979 else fputs("mouse_scroll_down = toggle_iconify\n", fp);
980
981 fputs("\n\n", fp);
982 fclose (fp);
983 }
984
This page took 0.103627 seconds and 5 git commands to generate.