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