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