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