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