1 /**************************************************************************
3 * Tint2 : read/write config file
5 * Copyright (C) 2007 Pål Staurland (staura@gmail.com)
6 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
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.
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 **************************************************************************/
25 #include <cairo-xlib.h>
27 #include <X11/Xutil.h>
28 #include <X11/Xatom.h>
33 #include <glib/gstdio.h>
34 #include <pango/pangocairo.h>
35 #include <pango/pangoxft.h>
43 #include "systraybar.h"
55 char *config_path
= 0;
56 char *snapshot_path
= 0;
58 // --------------------------------------------------
59 // backward compatibility
60 static int old_task_icon_size
;
61 // detect if it's an old config file
63 static int old_config_file
;
69 g_array_free(backgrounds
, 1);
70 backgrounds
= g_array_new(0, 0, sizeof(Background
));
72 // append full transparency background
73 Background transparent_bg
;
74 memset(&transparent_bg
, 0, sizeof(Background
));
75 g_array_append_val(backgrounds
, transparent_bg
);
77 // tint2 could reload config, so we cleanup objects
85 // panel's default value
86 if (panel_config
.g_task
.font_desc
) {
87 pango_font_description_free(panel_config
.g_task
.font_desc
);
89 memset(&panel_config
, 0, sizeof(Panel
));
91 for (i
=0; i
<TASK_STATE_COUNT
; ++i
)
92 panel_config
.g_task
.alpha
[i
] = 100;
97 // window manager's menu default value == false
101 // flush pango cache if possible
102 //pango_xft_shutdown_display(server.dsp, server.screen);
103 //PangoFontMap *font_map = pango_xft_get_font_map(server.dsp, server.screen);
104 //pango_fc_font_map_shutdown(font_map);
108 void cleanup_config()
113 void extract_values (const char *value
, char **value1
, char **value2
, char **value3
)
117 if (*value1
) free (*value1
);
118 if (*value2
) free (*value2
);
119 if (*value3
) free (*value3
);
121 if ((b
= strchr (value
, ' '))) {
129 *value1
= strdup (value
);
133 if ((c
= strchr (b
, ' '))) {
141 *value2
= strdup (b
);
146 *value3
= strdup (c
);
152 void get_action (char *event
, int *action
)
154 if (strcmp (event
, "none") == 0)
156 else if (strcmp (event
, "close") == 0)
158 else if (strcmp (event
, "toggle") == 0)
160 else if (strcmp (event
, "iconify") == 0)
162 else if (strcmp (event
, "shade") == 0)
164 else if (strcmp (event
, "toggle_iconify") == 0)
165 *action
= TOGGLE_ICONIFY
;
166 else if (strcmp (event
, "maximize_restore") == 0)
167 *action
= MAXIMIZE_RESTORE
;
168 else if (strcmp (event
, "desktop_left") == 0)
169 *action
= DESKTOP_LEFT
;
170 else if (strcmp (event
, "desktop_right") == 0)
171 *action
= DESKTOP_RIGHT
;
172 else if (strcmp (event
, "next_task") == 0)
174 else if (strcmp (event
, "prev_task") == 0)
179 int get_task_status(char* status
)
181 if (strcmp(status
, "active") == 0)
183 if (strcmp(status
, "iconified") == 0)
184 return TASK_ICONIFIED
;
185 if (strcmp(status
, "urgent") == 0)
191 void add_entry (char *key
, char *value
)
193 char *value1
=0, *value2
=0, *value3
=0;
195 /* Background and border */
196 if (strcmp (key
, "rounded") == 0) {
197 // 'rounded' is the first parameter => alloc a new background
199 bg
.border
.rounded
= atoi(value
);
200 g_array_append_val(backgrounds
, bg
);
202 else if (strcmp (key
, "border_width") == 0) {
203 g_array_index(backgrounds
, Background
, backgrounds
->len
-1).border
.width
= atoi(value
);
205 else if (strcmp (key
, "background_color") == 0) {
206 Background
* bg
= &g_array_index(backgrounds
, Background
, backgrounds
->len
-1);
207 extract_values(value
, &value1
, &value2
, &value3
);
208 get_color (value1
, bg
->back
.color
);
209 if (value2
) bg
->back
.alpha
= (atoi (value2
) / 100.0);
210 else bg
->back
.alpha
= 0.5;
212 else if (strcmp (key
, "border_color") == 0) {
213 Background
* bg
= &g_array_index(backgrounds
, Background
, backgrounds
->len
-1);
214 extract_values(value
, &value1
, &value2
, &value3
);
215 get_color (value1
, bg
->border
.color
);
216 if (value2
) bg
->border
.alpha
= (atoi (value2
) / 100.0);
217 else bg
->border
.alpha
= 0.5;
221 else if (strcmp (key
, "panel_monitor") == 0) {
222 if (strcmp (value
, "all") == 0) panel_config
.monitor
= -1;
224 panel_config
.monitor
= atoi (value
);
225 if (panel_config
.monitor
> 0) panel_config
.monitor
-= 1;
228 else if (strcmp (key
, "panel_size") == 0) {
229 extract_values(value
, &value1
, &value2
, &value3
);
232 if ((b
= strchr (value1
, '%'))) {
234 panel_config
.pourcentx
= 1;
236 panel_config
.area
.width
= atoi(value1
);
237 if (panel_config
.area
.width
== 0) {
239 panel_config
.area
.width
= 100;
240 panel_config
.pourcentx
= 1;
243 if ((b
= strchr (value2
, '%'))) {
245 panel_config
.pourcenty
= 1;
247 panel_config
.area
.height
= atoi(value2
);
250 else if (strcmp (key
, "panel_margin") == 0) {
251 extract_values(value
, &value1
, &value2
, &value3
);
252 panel_config
.marginx
= atoi (value1
);
253 if (value2
) panel_config
.marginy
= atoi (value2
);
255 else if (strcmp (key
, "panel_padding") == 0) {
256 extract_values(value
, &value1
, &value2
, &value3
);
257 panel_config
.area
.paddingxlr
= panel_config
.area
.paddingx
= atoi (value1
);
258 if (value2
) panel_config
.area
.paddingy
= atoi (value2
);
259 if (value3
) panel_config
.area
.paddingx
= atoi (value3
);
261 else if (strcmp (key
, "panel_position") == 0) {
262 extract_values(value
, &value1
, &value2
, &value3
);
263 if (strcmp (value1
, "top") == 0) panel_position
= TOP
;
265 if (strcmp (value1
, "bottom") == 0) panel_position
= BOTTOM
;
266 else panel_position
= CENTER
;
269 if (!value2
) panel_position
|= CENTER
;
271 if (strcmp (value2
, "left") == 0) panel_position
|= LEFT
;
273 if (strcmp (value2
, "right") == 0) panel_position
|= RIGHT
;
274 else panel_position
|= CENTER
;
277 if (!value3
) panel_horizontal
= 1;
279 if (strcmp (value3
, "vertical") == 0) panel_horizontal
= 0;
280 else panel_horizontal
= 1;
283 else if (strcmp (key
, "font_shadow") == 0)
284 panel_config
.g_task
.font_shadow
= atoi (value
);
285 else if (strcmp (key
, "panel_background_id") == 0) {
286 int id
= atoi (value
);
287 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
288 panel_config
.area
.bg
= &g_array_index(backgrounds
, Background
, id
);
290 else if (strcmp (key
, "wm_menu") == 0)
291 wm_menu
= atoi (value
);
292 else if (strcmp (key
, "panel_dock") == 0)
293 panel_dock
= atoi (value
);
294 else if (strcmp (key
, "urgent_nb_of_blink") == 0)
295 max_tick_urgent
= (atoi (value
) * 2) + 1;
296 else if (strcmp (key
, "panel_layer") == 0) {
297 if (strcmp(value
, "bottom") == 0)
298 panel_layer
= BOTTOM_LAYER
;
299 else if (strcmp(value
, "normal") == 0)
300 panel_layer
= NORMAL_LAYER
;
301 else if (strcmp(value
, "top") == 0)
302 panel_layer
= TOP_LAYER
;
306 else if (strcmp (key
, "battery") == 0) {
307 #ifdef ENABLE_BATTERY
312 fprintf(stderr
, "tint2 is build without battery support\n");
315 else if (strcmp (key
, "battery_low_status") == 0) {
316 #ifdef ENABLE_BATTERY
317 battery_low_status
= atoi(value
);
318 if(battery_low_status
< 0 || battery_low_status
> 100)
319 battery_low_status
= 0;
322 else if (strcmp (key
, "battery_low_cmd") == 0) {
323 #ifdef ENABLE_BATTERY
324 if (strlen(value
) > 0)
325 battery_low_cmd
= strdup (value
);
328 else if (strcmp (key
, "bat1_font") == 0) {
329 #ifdef ENABLE_BATTERY
330 bat1_font_desc
= pango_font_description_from_string (value
);
333 else if (strcmp (key
, "bat2_font") == 0) {
334 #ifdef ENABLE_BATTERY
335 bat2_font_desc
= pango_font_description_from_string (value
);
338 else if (strcmp (key
, "battery_font_color") == 0) {
339 #ifdef ENABLE_BATTERY
340 extract_values(value
, &value1
, &value2
, &value3
);
341 get_color (value1
, panel_config
.battery
.font
.color
);
342 if (value2
) panel_config
.battery
.font
.alpha
= (atoi (value2
) / 100.0);
343 else panel_config
.battery
.font
.alpha
= 0.5;
346 else if (strcmp (key
, "battery_padding") == 0) {
347 #ifdef ENABLE_BATTERY
348 extract_values(value
, &value1
, &value2
, &value3
);
349 panel_config
.battery
.area
.paddingxlr
= panel_config
.battery
.area
.paddingx
= atoi (value1
);
350 if (value2
) panel_config
.battery
.area
.paddingy
= atoi (value2
);
351 if (value3
) panel_config
.battery
.area
.paddingx
= atoi (value3
);
354 else if (strcmp (key
, "battery_background_id") == 0) {
355 #ifdef ENABLE_BATTERY
356 int id
= atoi (value
);
357 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
358 panel_config
.battery
.area
.bg
= &g_array_index(backgrounds
, Background
, id
);
363 else if (strcmp (key
, "time1_format") == 0) {
364 if (strlen(value
) > 0) {
365 time1_format
= strdup (value
);
369 else if (strcmp (key
, "time2_format") == 0) {
370 if (strlen(value
) > 0)
371 time2_format
= strdup (value
);
373 else if (strcmp (key
, "time1_font") == 0) {
374 time1_font_desc
= pango_font_description_from_string (value
);
376 else if (strcmp(key
, "time1_timezone") == 0) {
377 if (strlen(value
) > 0)
378 time1_timezone
= strdup(value
);
380 else if (strcmp(key
, "time2_timezone") == 0) {
381 if (strlen(value
) > 0)
382 time2_timezone
= strdup(value
);
384 else if (strcmp (key
, "time2_font") == 0) {
385 time2_font_desc
= pango_font_description_from_string (value
);
387 else if (strcmp (key
, "clock_font_color") == 0) {
388 extract_values(value
, &value1
, &value2
, &value3
);
389 get_color (value1
, panel_config
.clock
.font
.color
);
390 if (value2
) panel_config
.clock
.font
.alpha
= (atoi (value2
) / 100.0);
391 else panel_config
.clock
.font
.alpha
= 0.5;
393 else if (strcmp (key
, "clock_padding") == 0) {
394 extract_values(value
, &value1
, &value2
, &value3
);
395 panel_config
.clock
.area
.paddingxlr
= panel_config
.clock
.area
.paddingx
= atoi (value1
);
396 if (value2
) panel_config
.clock
.area
.paddingy
= atoi (value2
);
397 if (value3
) panel_config
.clock
.area
.paddingx
= atoi (value3
);
399 else if (strcmp (key
, "clock_background_id") == 0) {
400 int id
= atoi (value
);
401 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
402 panel_config
.clock
.area
.bg
= &g_array_index(backgrounds
, Background
, id
);
404 else if (strcmp(key
, "clock_tooltip") == 0) {
405 if (strlen(value
) > 0)
406 time_tooltip_format
= strdup (value
);
408 else if (strcmp(key
, "clock_tooltip_timezone") == 0) {
409 if (strlen(value
) > 0)
410 time_tooltip_timezone
= strdup(value
);
412 else if (strcmp(key
, "clock_lclick_command") == 0) {
413 if (strlen(value
) > 0)
414 clock_lclick_command
= strdup(value
);
416 else if (strcmp(key
, "clock_rclick_command") == 0) {
417 if (strlen(value
) > 0)
418 clock_rclick_command
= strdup(value
);
422 else if (strcmp (key
, "taskbar_mode") == 0) {
423 if (strcmp (value
, "multi_desktop") == 0) panel_mode
= MULTI_DESKTOP
;
424 else panel_mode
= SINGLE_DESKTOP
;
426 else if (strcmp (key
, "taskbar_padding") == 0) {
427 extract_values(value
, &value1
, &value2
, &value3
);
428 panel_config
.g_taskbar
.area
.paddingxlr
= panel_config
.g_taskbar
.area
.paddingx
= atoi (value1
);
429 if (value2
) panel_config
.g_taskbar
.area
.paddingy
= atoi (value2
);
430 if (value3
) panel_config
.g_taskbar
.area
.paddingx
= atoi (value3
);
432 else if (strcmp (key
, "taskbar_background_id") == 0) {
433 int id
= atoi (value
);
434 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
435 panel_config
.g_taskbar
.bg
= &g_array_index(backgrounds
, Background
, id
);
436 panel_config
.g_taskbar
.area
.bg
= panel_config
.g_taskbar
.bg
;
438 else if (strcmp (key
, "taskbar_active_background_id") == 0) {
439 int id
= atoi (value
);
440 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
441 panel_config
.g_taskbar
.bg_active
= &g_array_index(backgrounds
, Background
, id
);
442 panel_config
.g_taskbar
.use_active
= 1;
446 else if (strcmp (key
, "task_text") == 0)
447 panel_config
.g_task
.text
= atoi (value
);
448 else if (strcmp (key
, "task_icon") == 0)
449 panel_config
.g_task
.icon
= atoi (value
);
450 else if (strcmp (key
, "task_centered") == 0)
451 panel_config
.g_task
.centered
= atoi (value
);
452 else if (strcmp (key
, "task_width") == 0) {
453 // old parameter : just for backward compatibility
454 panel_config
.g_task
.maximum_width
= atoi (value
);
455 panel_config
.g_task
.maximum_height
= 30;
457 else if (strcmp (key
, "task_maximum_size") == 0) {
458 extract_values(value
, &value1
, &value2
, &value3
);
459 panel_config
.g_task
.maximum_width
= atoi (value1
);
460 panel_config
.g_task
.maximum_height
= 30;
462 panel_config
.g_task
.maximum_height
= atoi (value2
);
464 else if (strcmp (key
, "task_padding") == 0) {
465 extract_values(value
, &value1
, &value2
, &value3
);
466 panel_config
.g_task
.area
.paddingxlr
= panel_config
.g_task
.area
.paddingx
= atoi (value1
);
467 if (value2
) panel_config
.g_task
.area
.paddingy
= atoi (value2
);
468 if (value3
) panel_config
.g_task
.area
.paddingx
= atoi (value3
);
470 else if (strcmp (key
, "task_font") == 0) {
471 panel_config
.g_task
.font_desc
= pango_font_description_from_string (value
);
473 else if (g_regex_match_simple("task.*_font_color", key
, 0, 0)) {
474 gchar
** split
= g_regex_split_simple("_", key
, 0, 0);
475 int status
= get_task_status(split
[1]);
477 extract_values(value
, &value1
, &value2
, &value3
);
479 if (value2
) alpha
= (atoi (value2
) / 100.0);
480 get_color (value1
, panel_config
.g_task
.font
[status
].color
);
481 panel_config
.g_task
.font
[status
].alpha
= alpha
;
482 panel_config
.g_task
.config_font_mask
|= (1<<status
);
484 else if (g_regex_match_simple("task.*_icon_asb", key
, 0, 0)) {
485 gchar
** split
= g_regex_split_simple("_", key
, 0, 0);
486 int status
= get_task_status(split
[1]);
488 extract_values(value
, &value1
, &value2
, &value3
);
489 panel_config
.g_task
.alpha
[status
] = atoi(value1
);
490 panel_config
.g_task
.saturation
[status
] = atoi(value2
);
491 panel_config
.g_task
.brightness
[status
] = atoi(value3
);
492 panel_config
.g_task
.config_asb_mask
|= (1<<status
);
494 else if (g_regex_match_simple("task.*_background_id", key
, 0, 0)) {
495 gchar
** split
= g_regex_split_simple("_", key
, 0, 0);
496 int status
= get_task_status(split
[1]);
498 int id
= atoi (value
);
499 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
500 panel_config
.g_task
.background
[status
] = &g_array_index(backgrounds
, Background
, id
);
501 panel_config
.g_task
.config_background_mask
|= (1<<status
);
502 if (status
== TASK_NORMAL
) panel_config
.g_task
.area
.bg
= panel_config
.g_task
.background
[TASK_NORMAL
];
506 else if (strcmp (key
, "systray") == 0) {
507 systray_enabled
= atoi(value
);
508 // systray is latest option added. files without 'systray' are old.
511 else if (strcmp (key
, "systray_padding") == 0) {
514 extract_values(value
, &value1
, &value2
, &value3
);
515 systray
.area
.paddingxlr
= systray
.area
.paddingx
= atoi (value1
);
516 if (value2
) systray
.area
.paddingy
= atoi (value2
);
517 if (value3
) systray
.area
.paddingx
= atoi (value3
);
519 else if (strcmp (key
, "systray_background_id") == 0) {
520 int id
= atoi (value
);
521 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
522 systray
.area
.bg
= &g_array_index(backgrounds
, Background
, id
);
524 else if (strcmp(key
, "systray_sort") == 0) {
525 if (strcmp(value
, "descending") == 0)
527 else if (strcmp(value
, "ascending") == 0)
529 else if (strcmp(value
, "left2right") == 0)
531 else if (strcmp(value
, "right2left") == 0)
534 else if (strcmp(key
, "systray_icon_size") == 0) {
535 systray_max_icon_size
= atoi(value
);
537 else if (strcmp(key
, "systray_icon_asb") == 0) {
538 extract_values(value
, &value1
, &value2
, &value3
);
539 systray
.alpha
= atoi(value1
);
540 systray
.saturation
= atoi(value2
);
541 systray
.brightness
= atoi(value3
);
545 else if (strcmp (key
, "tooltip") == 0)
546 g_tooltip
.enabled
= atoi(value
);
547 else if (strcmp (key
, "tooltip_show_timeout") == 0) {
548 int timeout_msec
= 1000*atof(value
);
549 g_tooltip
.show_timeout_msec
= timeout_msec
;
551 else if (strcmp (key
, "tooltip_hide_timeout") == 0) {
552 int timeout_msec
= 1000*atof(value
);
553 g_tooltip
.hide_timeout_msec
= timeout_msec
;
555 else if (strcmp (key
, "tooltip_padding") == 0) {
556 extract_values(value
, &value1
, &value2
, &value3
);
557 if (value1
) g_tooltip
.paddingx
= atoi(value1
);
558 if (value2
) g_tooltip
.paddingy
= atoi(value2
);
560 else if (strcmp (key
, "tooltip_background_id") == 0) {
561 int id
= atoi (value
);
562 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
563 g_tooltip
.bg
= &g_array_index(backgrounds
, Background
, id
);
565 else if (strcmp (key
, "tooltip_font_color") == 0) {
566 extract_values(value
, &value1
, &value2
, &value3
);
567 get_color(value1
, g_tooltip
.font_color
.color
);
568 if (value2
) g_tooltip
.font_color
.alpha
= (atoi (value2
) / 100.0);
569 else g_tooltip
.font_color
.alpha
= 0.1;
571 else if (strcmp (key
, "tooltip_font") == 0) {
572 g_tooltip
.font_desc
= pango_font_description_from_string(value
);
576 else if (strcmp (key
, "mouse_middle") == 0)
577 get_action (value
, &mouse_middle
);
578 else if (strcmp (key
, "mouse_right") == 0)
579 get_action (value
, &mouse_right
);
580 else if (strcmp (key
, "mouse_scroll_up") == 0)
581 get_action (value
, &mouse_scroll_up
);
582 else if (strcmp (key
, "mouse_scroll_down") == 0)
583 get_action (value
, &mouse_scroll_down
);
585 /* autohide options */
586 else if (strcmp(key
, "autohide") == 0)
587 panel_autohide
= atoi(value
);
588 else if (strcmp(key
, "autohide_show_timeout") == 0)
589 panel_autohide_show_timeout
= 1000*atof(value
);
590 else if (strcmp(key
, "autohide_hide_timeout") == 0)
591 panel_autohide_hide_timeout
= 1000*atof(value
);
592 else if (strcmp(key
, "strut_policy") == 0) {
593 if (strcmp(value
, "follow_size") == 0)
594 panel_strut_policy
= STRUT_FOLLOW_SIZE
;
596 panel_strut_policy
= STRUT_MINIMUM
;
598 else if (strcmp(key
, "autohide_height") == 0)
599 panel_autohide_height
= atoi(value
);
602 fprintf(stderr
, "tint2 : invalid option \"%s\", correct your config file\n", key
);
604 if (value1
) free (value1
);
605 if (value2
) free (value2
);
606 if (value3
) free (value3
);
612 const gchar
* const * system_dirs
;
616 // follow XDG specification
617 // check tint2rc in user directory
618 path1
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL
);
619 if (g_file_test (path1
, G_FILE_TEST_EXISTS
)) {
620 i
= config_read_file (path1
);
621 config_path
= strdup(path1
);
627 // copy tint2rc from system directory to user directory
629 system_dirs
= g_get_system_config_dirs();
630 for (i
= 0; system_dirs
[i
]; i
++) {
631 path2
= g_build_filename(system_dirs
[i
], "tint2", "tint2rc", NULL
);
633 if (g_file_test(path2
, G_FILE_TEST_EXISTS
)) break;
639 // copy file in user directory (path1)
640 char *dir
= g_build_filename (g_get_user_config_dir(), "tint2", NULL
);
641 if (!g_file_test (dir
, G_FILE_TEST_IS_DIR
)) g_mkdir(dir
, 0777);
644 path1
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL
);
645 copy_file(path2
, path1
);
648 i
= config_read_file (path1
);
649 config_path
= strdup(path1
);
657 int config_read_file (const char *path
)
663 if ((fp
= fopen(path
, "r")) == NULL
) return 0;
665 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
666 if (parse_line(line
, &key
, &value
)) {
667 add_entry (key
, value
);
674 if (old_task_icon_size
) {
675 panel_config
.g_task
.area
.paddingy
= ((int)panel_config
.area
.height
- (2 * panel_config
.area
.paddingy
) - old_task_icon_size
) / 2;