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"
58 // --------------------------------------------------
59 // backward compatibility
60 // detect if it's an old config file (==1)
61 static int old_config_file
;
73 if (config_path
) g_free(config_path
);
74 if (snapshot_path
) g_free(snapshot_path
);
78 void extract_values (const char *value
, char **value1
, char **value2
, char **value3
)
82 if (*value1
) free (*value1
);
83 if (*value2
) free (*value2
);
84 if (*value3
) free (*value3
);
86 if ((b
= strchr (value
, ' '))) {
94 *value1
= strdup (value
);
98 if ((c
= strchr (b
, ' '))) {
106 *value2
= strdup (b
);
111 *value3
= strdup (c
);
117 void get_action (char *event
, int *action
)
119 if (strcmp (event
, "none") == 0)
121 else if (strcmp (event
, "close") == 0)
123 else if (strcmp (event
, "toggle") == 0)
125 else if (strcmp (event
, "iconify") == 0)
127 else if (strcmp (event
, "shade") == 0)
129 else if (strcmp (event
, "toggle_iconify") == 0)
130 *action
= TOGGLE_ICONIFY
;
131 else if (strcmp (event
, "maximize_restore") == 0)
132 *action
= MAXIMIZE_RESTORE
;
133 else if (strcmp (event
, "desktop_left") == 0)
134 *action
= DESKTOP_LEFT
;
135 else if (strcmp (event
, "desktop_right") == 0)
136 *action
= DESKTOP_RIGHT
;
137 else if (strcmp (event
, "next_task") == 0)
139 else if (strcmp (event
, "prev_task") == 0)
144 int get_task_status(char* status
)
146 if (strcmp(status
, "active") == 0)
148 if (strcmp(status
, "iconified") == 0)
149 return TASK_ICONIFIED
;
150 if (strcmp(status
, "urgent") == 0)
156 int config_get_monitor(char* monitor
)
158 if (strcmp(monitor
, "all") == 0)
162 int ret_int
= strtol(monitor
, &endptr
, 10);
166 // monitor specified by name, not by index
168 for (i
=0; i
<server
.nb_monitor
; ++i
) {
170 while (server
.monitor
[i
].names
[j
] != 0) {
171 if (strcmp(monitor
, server
.monitor
[i
].names
[j
++]) == 0)
180 void add_entry (char *key
, char *value
)
182 char *value1
=0, *value2
=0, *value3
=0;
184 /* Background and border */
185 if (strcmp (key
, "rounded") == 0) {
186 // 'rounded' is the first parameter => alloc a new background
188 bg
.border
.rounded
= atoi(value
);
189 g_array_append_val(backgrounds
, bg
);
191 else if (strcmp (key
, "border_width") == 0) {
192 g_array_index(backgrounds
, Background
, backgrounds
->len
-1).border
.width
= atoi(value
);
194 else if (strcmp (key
, "background_color") == 0) {
195 Background
* bg
= &g_array_index(backgrounds
, Background
, backgrounds
->len
-1);
196 extract_values(value
, &value1
, &value2
, &value3
);
197 get_color (value1
, bg
->back
.color
);
198 if (value2
) bg
->back
.alpha
= (atoi (value2
) / 100.0);
199 else bg
->back
.alpha
= 0.5;
201 else if (strcmp (key
, "border_color") == 0) {
202 Background
* bg
= &g_array_index(backgrounds
, Background
, backgrounds
->len
-1);
203 extract_values(value
, &value1
, &value2
, &value3
);
204 get_color (value1
, bg
->border
.color
);
205 if (value2
) bg
->border
.alpha
= (atoi (value2
) / 100.0);
206 else bg
->border
.alpha
= 0.5;
210 else if (strcmp (key
, "panel_monitor") == 0) {
211 panel_config
.monitor
= config_get_monitor(value
);
213 else if (strcmp (key
, "panel_size") == 0) {
214 extract_values(value
, &value1
, &value2
, &value3
);
217 if ((b
= strchr (value1
, '%'))) {
219 panel_config
.pourcentx
= 1;
221 panel_config
.area
.width
= atoi(value1
);
222 if (panel_config
.area
.width
== 0) {
224 panel_config
.area
.width
= 100;
225 panel_config
.pourcentx
= 1;
228 if ((b
= strchr (value2
, '%'))) {
230 panel_config
.pourcenty
= 1;
232 panel_config
.area
.height
= atoi(value2
);
235 else if (strcmp (key
, "panel_margin") == 0) {
236 extract_values(value
, &value1
, &value2
, &value3
);
237 panel_config
.marginx
= atoi (value1
);
238 if (value2
) panel_config
.marginy
= atoi (value2
);
240 else if (strcmp (key
, "panel_padding") == 0) {
241 extract_values(value
, &value1
, &value2
, &value3
);
242 panel_config
.area
.paddingxlr
= panel_config
.area
.paddingx
= atoi (value1
);
243 if (value2
) panel_config
.area
.paddingy
= atoi (value2
);
244 if (value3
) panel_config
.area
.paddingx
= atoi (value3
);
246 else if (strcmp (key
, "panel_position") == 0) {
247 extract_values(value
, &value1
, &value2
, &value3
);
248 if (strcmp (value1
, "top") == 0) panel_position
= TOP
;
250 if (strcmp (value1
, "bottom") == 0) panel_position
= BOTTOM
;
251 else panel_position
= CENTER
;
254 if (!value2
) panel_position
|= CENTER
;
256 if (strcmp (value2
, "left") == 0) panel_position
|= LEFT
;
258 if (strcmp (value2
, "right") == 0) panel_position
|= RIGHT
;
259 else panel_position
|= CENTER
;
262 if (!value3
) panel_horizontal
= 1;
264 if (strcmp (value3
, "vertical") == 0) panel_horizontal
= 0;
265 else panel_horizontal
= 1;
268 else if (strcmp (key
, "font_shadow") == 0)
269 panel_config
.g_task
.font_shadow
= atoi (value
);
270 else if (strcmp (key
, "panel_background_id") == 0) {
271 int id
= atoi (value
);
272 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
273 panel_config
.area
.bg
= &g_array_index(backgrounds
, Background
, id
);
275 else if (strcmp (key
, "wm_menu") == 0)
276 wm_menu
= atoi (value
);
277 else if (strcmp (key
, "panel_dock") == 0)
278 panel_dock
= atoi (value
);
279 else if (strcmp (key
, "urgent_nb_of_blink") == 0)
280 max_tick_urgent
= (atoi (value
) * 2) + 1;
281 else if (strcmp (key
, "panel_layer") == 0) {
282 if (strcmp(value
, "bottom") == 0)
283 panel_layer
= BOTTOM_LAYER
;
284 else if (strcmp(value
, "normal") == 0)
285 panel_layer
= NORMAL_LAYER
;
286 else if (strcmp(value
, "top") == 0)
287 panel_layer
= TOP_LAYER
;
291 else if (strcmp (key
, "battery") == 0) {
292 #ifdef ENABLE_BATTERY
297 fprintf(stderr
, "tint2 is build without battery support\n");
300 else if (strcmp (key
, "battery_low_status") == 0) {
301 #ifdef ENABLE_BATTERY
302 battery_low_status
= atoi(value
);
303 if(battery_low_status
< 0 || battery_low_status
> 100)
304 battery_low_status
= 0;
307 else if (strcmp (key
, "battery_low_cmd") == 0) {
308 #ifdef ENABLE_BATTERY
309 if (strlen(value
) > 0)
310 battery_low_cmd
= strdup (value
);
313 else if (strcmp (key
, "bat1_font") == 0) {
314 #ifdef ENABLE_BATTERY
315 bat1_font_desc
= pango_font_description_from_string (value
);
318 else if (strcmp (key
, "bat2_font") == 0) {
319 #ifdef ENABLE_BATTERY
320 bat2_font_desc
= pango_font_description_from_string (value
);
323 else if (strcmp (key
, "battery_font_color") == 0) {
324 #ifdef ENABLE_BATTERY
325 extract_values(value
, &value1
, &value2
, &value3
);
326 get_color (value1
, panel_config
.battery
.font
.color
);
327 if (value2
) panel_config
.battery
.font
.alpha
= (atoi (value2
) / 100.0);
328 else panel_config
.battery
.font
.alpha
= 0.5;
331 else if (strcmp (key
, "battery_padding") == 0) {
332 #ifdef ENABLE_BATTERY
333 extract_values(value
, &value1
, &value2
, &value3
);
334 panel_config
.battery
.area
.paddingxlr
= panel_config
.battery
.area
.paddingx
= atoi (value1
);
335 if (value2
) panel_config
.battery
.area
.paddingy
= atoi (value2
);
336 if (value3
) panel_config
.battery
.area
.paddingx
= atoi (value3
);
339 else if (strcmp (key
, "battery_background_id") == 0) {
340 #ifdef ENABLE_BATTERY
341 int id
= atoi (value
);
342 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
343 panel_config
.battery
.area
.bg
= &g_array_index(backgrounds
, Background
, id
);
346 else if (strcmp (key
, "battery_hide") == 0) {
347 #ifdef ENABLE_BATTERY
348 percentage_hide
= atoi (value
);
349 if (percentage_hide
== 0)
350 percentage_hide
= 101;
355 else if (strcmp (key
, "time1_format") == 0) {
356 if (strlen(value
) > 0) {
357 time1_format
= strdup (value
);
361 else if (strcmp (key
, "time2_format") == 0) {
362 if (strlen(value
) > 0)
363 time2_format
= strdup (value
);
365 else if (strcmp (key
, "time1_font") == 0) {
366 time1_font_desc
= pango_font_description_from_string (value
);
368 else if (strcmp(key
, "time1_timezone") == 0) {
369 if (strlen(value
) > 0)
370 time1_timezone
= strdup(value
);
372 else if (strcmp(key
, "time2_timezone") == 0) {
373 if (strlen(value
) > 0)
374 time2_timezone
= strdup(value
);
376 else if (strcmp (key
, "time2_font") == 0) {
377 time2_font_desc
= pango_font_description_from_string (value
);
379 else if (strcmp (key
, "clock_font_color") == 0) {
380 extract_values(value
, &value1
, &value2
, &value3
);
381 get_color (value1
, panel_config
.clock
.font
.color
);
382 if (value2
) panel_config
.clock
.font
.alpha
= (atoi (value2
) / 100.0);
383 else panel_config
.clock
.font
.alpha
= 0.5;
385 else if (strcmp (key
, "clock_padding") == 0) {
386 extract_values(value
, &value1
, &value2
, &value3
);
387 panel_config
.clock
.area
.paddingxlr
= panel_config
.clock
.area
.paddingx
= atoi (value1
);
388 if (value2
) panel_config
.clock
.area
.paddingy
= atoi (value2
);
389 if (value3
) panel_config
.clock
.area
.paddingx
= atoi (value3
);
391 else if (strcmp (key
, "clock_background_id") == 0) {
392 int id
= atoi (value
);
393 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
394 panel_config
.clock
.area
.bg
= &g_array_index(backgrounds
, Background
, id
);
396 else if (strcmp(key
, "clock_tooltip") == 0) {
397 if (strlen(value
) > 0)
398 time_tooltip_format
= strdup (value
);
400 else if (strcmp(key
, "clock_tooltip_timezone") == 0) {
401 if (strlen(value
) > 0)
402 time_tooltip_timezone
= strdup(value
);
404 else if (strcmp(key
, "clock_lclick_command") == 0) {
405 if (strlen(value
) > 0)
406 clock_lclick_command
= strdup(value
);
408 else if (strcmp(key
, "clock_rclick_command") == 0) {
409 if (strlen(value
) > 0)
410 clock_rclick_command
= strdup(value
);
414 else if (strcmp (key
, "taskbar_mode") == 0) {
415 if (strcmp (value
, "multi_desktop") == 0) panel_mode
= MULTI_DESKTOP
;
416 else panel_mode
= SINGLE_DESKTOP
;
418 else if (strcmp (key
, "taskbar_padding") == 0) {
419 extract_values(value
, &value1
, &value2
, &value3
);
420 panel_config
.g_taskbar
.area
.paddingxlr
= panel_config
.g_taskbar
.area
.paddingx
= atoi (value1
);
421 if (value2
) panel_config
.g_taskbar
.area
.paddingy
= atoi (value2
);
422 if (value3
) panel_config
.g_taskbar
.area
.paddingx
= atoi (value3
);
424 else if (strcmp (key
, "taskbar_background_id") == 0) {
425 int id
= atoi (value
);
426 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
427 panel_config
.g_taskbar
.bg
= &g_array_index(backgrounds
, Background
, id
);
428 panel_config
.g_taskbar
.area
.bg
= panel_config
.g_taskbar
.bg
;
430 else if (strcmp (key
, "taskbar_active_background_id") == 0) {
431 int id
= atoi (value
);
432 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
433 panel_config
.g_taskbar
.bg_active
= &g_array_index(backgrounds
, Background
, id
);
434 panel_config
.g_taskbar
.use_active
= 1;
438 else if (strcmp (key
, "task_text") == 0)
439 panel_config
.g_task
.text
= atoi (value
);
440 else if (strcmp (key
, "task_icon") == 0)
441 panel_config
.g_task
.icon
= atoi (value
);
442 else if (strcmp (key
, "task_centered") == 0)
443 panel_config
.g_task
.centered
= atoi (value
);
444 else if (strcmp (key
, "task_width") == 0) {
445 // old parameter : just for backward compatibility
446 panel_config
.g_task
.maximum_width
= atoi (value
);
447 panel_config
.g_task
.maximum_height
= 30;
449 else if (strcmp (key
, "task_maximum_size") == 0) {
450 extract_values(value
, &value1
, &value2
, &value3
);
451 panel_config
.g_task
.maximum_width
= atoi (value1
);
452 panel_config
.g_task
.maximum_height
= 30;
454 panel_config
.g_task
.maximum_height
= atoi (value2
);
456 else if (strcmp (key
, "task_padding") == 0) {
457 extract_values(value
, &value1
, &value2
, &value3
);
458 panel_config
.g_task
.area
.paddingxlr
= panel_config
.g_task
.area
.paddingx
= atoi (value1
);
459 if (value2
) panel_config
.g_task
.area
.paddingy
= atoi (value2
);
460 if (value3
) panel_config
.g_task
.area
.paddingx
= atoi (value3
);
462 else if (strcmp (key
, "task_font") == 0) {
463 panel_config
.g_task
.font_desc
= pango_font_description_from_string (value
);
465 else if (g_regex_match_simple("task.*_font_color", key
, 0, 0)) {
466 gchar
** split
= g_regex_split_simple("_", key
, 0, 0);
467 int status
= get_task_status(split
[1]);
469 extract_values(value
, &value1
, &value2
, &value3
);
471 if (value2
) alpha
= (atoi (value2
) / 100.0);
472 get_color (value1
, panel_config
.g_task
.font
[status
].color
);
473 panel_config
.g_task
.font
[status
].alpha
= alpha
;
474 panel_config
.g_task
.config_font_mask
|= (1<<status
);
476 else if (g_regex_match_simple("task.*_icon_asb", key
, 0, 0)) {
477 gchar
** split
= g_regex_split_simple("_", key
, 0, 0);
478 int status
= get_task_status(split
[1]);
480 extract_values(value
, &value1
, &value2
, &value3
);
481 panel_config
.g_task
.alpha
[status
] = atoi(value1
);
482 panel_config
.g_task
.saturation
[status
] = atoi(value2
);
483 panel_config
.g_task
.brightness
[status
] = atoi(value3
);
484 panel_config
.g_task
.config_asb_mask
|= (1<<status
);
486 else if (g_regex_match_simple("task.*_background_id", key
, 0, 0)) {
487 gchar
** split
= g_regex_split_simple("_", key
, 0, 0);
488 int status
= get_task_status(split
[1]);
490 int id
= atoi (value
);
491 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
492 panel_config
.g_task
.background
[status
] = &g_array_index(backgrounds
, Background
, id
);
493 panel_config
.g_task
.config_background_mask
|= (1<<status
);
494 if (status
== TASK_NORMAL
) panel_config
.g_task
.area
.bg
= panel_config
.g_task
.background
[TASK_NORMAL
];
498 // systray disabled in snapshot mode
499 else if (strcmp (key
, "systray") == 0 && snapshot_path
== 0) {
500 systray_enabled
= atoi(value
);
501 // systray is latest option added. files without 'systray' are old.
504 else if (strcmp (key
, "systray_padding") == 0 && snapshot_path
== 0) {
507 extract_values(value
, &value1
, &value2
, &value3
);
508 systray
.area
.paddingxlr
= systray
.area
.paddingx
= atoi (value1
);
509 if (value2
) systray
.area
.paddingy
= atoi (value2
);
510 if (value3
) systray
.area
.paddingx
= atoi (value3
);
512 else if (strcmp (key
, "systray_background_id") == 0) {
513 int id
= atoi (value
);
514 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
515 systray
.area
.bg
= &g_array_index(backgrounds
, Background
, id
);
517 else if (strcmp(key
, "systray_sort") == 0) {
518 if (strcmp(value
, "descending") == 0)
520 else if (strcmp(value
, "ascending") == 0)
522 else if (strcmp(value
, "left2right") == 0)
524 else if (strcmp(value
, "right2left") == 0)
527 else if (strcmp(key
, "systray_icon_size") == 0) {
528 systray_max_icon_size
= atoi(value
);
530 else if (strcmp(key
, "systray_icon_asb") == 0) {
531 extract_values(value
, &value1
, &value2
, &value3
);
532 systray
.alpha
= atoi(value1
);
533 systray
.saturation
= atoi(value2
);
534 systray
.brightness
= atoi(value3
);
538 else if (strcmp (key
, "tooltip") == 0)
539 g_tooltip
.enabled
= atoi(value
);
540 else if (strcmp (key
, "tooltip_show_timeout") == 0) {
541 int timeout_msec
= 1000*atof(value
);
542 g_tooltip
.show_timeout_msec
= timeout_msec
;
544 else if (strcmp (key
, "tooltip_hide_timeout") == 0) {
545 int timeout_msec
= 1000*atof(value
);
546 g_tooltip
.hide_timeout_msec
= timeout_msec
;
548 else if (strcmp (key
, "tooltip_padding") == 0) {
549 extract_values(value
, &value1
, &value2
, &value3
);
550 if (value1
) g_tooltip
.paddingx
= atoi(value1
);
551 if (value2
) g_tooltip
.paddingy
= atoi(value2
);
553 else if (strcmp (key
, "tooltip_background_id") == 0) {
554 int id
= atoi (value
);
555 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
556 g_tooltip
.bg
= &g_array_index(backgrounds
, Background
, id
);
558 else if (strcmp (key
, "tooltip_font_color") == 0) {
559 extract_values(value
, &value1
, &value2
, &value3
);
560 get_color(value1
, g_tooltip
.font_color
.color
);
561 if (value2
) g_tooltip
.font_color
.alpha
= (atoi (value2
) / 100.0);
562 else g_tooltip
.font_color
.alpha
= 0.1;
564 else if (strcmp (key
, "tooltip_font") == 0) {
565 g_tooltip
.font_desc
= pango_font_description_from_string(value
);
569 else if (strcmp (key
, "mouse_middle") == 0)
570 get_action (value
, &mouse_middle
);
571 else if (strcmp (key
, "mouse_right") == 0)
572 get_action (value
, &mouse_right
);
573 else if (strcmp (key
, "mouse_scroll_up") == 0)
574 get_action (value
, &mouse_scroll_up
);
575 else if (strcmp (key
, "mouse_scroll_down") == 0)
576 get_action (value
, &mouse_scroll_down
);
578 /* autohide options */
579 else if (strcmp(key
, "autohide") == 0)
580 panel_autohide
= atoi(value
);
581 else if (strcmp(key
, "autohide_show_timeout") == 0)
582 panel_autohide_show_timeout
= 1000*atof(value
);
583 else if (strcmp(key
, "autohide_hide_timeout") == 0)
584 panel_autohide_hide_timeout
= 1000*atof(value
);
585 else if (strcmp(key
, "strut_policy") == 0) {
586 if (strcmp(value
, "follow_size") == 0)
587 panel_strut_policy
= STRUT_FOLLOW_SIZE
;
588 else if (strcmp(value
, "none") == 0)
589 panel_strut_policy
= STRUT_NONE
;
591 panel_strut_policy
= STRUT_MINIMUM
;
593 else if (strcmp(key
, "autohide_height") == 0) {
594 panel_autohide_height
= atoi(value
);
595 if (panel_autohide_height
== 0) {
596 // autohide need height > 0
597 panel_autohide_height
= 1;
602 fprintf(stderr
, "tint2 : invalid option \"%s\",\n upgrade tint2 or 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
);