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) {
160 int ret_int
= strtol(monitor
, &endptr
, 10);
164 // monitor specified by name, not by index
166 for (i
=0; i
<server
.nb_monitor
; ++i
) {
167 if (server
.monitor
[i
].names
== 0)
168 // xrandr can't identify monitors
171 while (server
.monitor
[i
].names
[j
] != 0) {
172 if (strcmp(monitor
, server
.monitor
[i
].names
[j
++]) == 0)
178 // monitor == "all" or monitor not found or xrandr can't identify monitors
182 void add_entry (char *key
, char *value
)
184 char *value1
=0, *value2
=0, *value3
=0;
186 /* Background and border */
187 if (strcmp (key
, "rounded") == 0) {
188 // 'rounded' is the first parameter => alloc a new background
190 bg
.border
.rounded
= atoi(value
);
191 g_array_append_val(backgrounds
, bg
);
193 else if (strcmp (key
, "border_width") == 0) {
194 g_array_index(backgrounds
, Background
, backgrounds
->len
-1).border
.width
= atoi(value
);
196 else if (strcmp (key
, "background_color") == 0) {
197 Background
* bg
= &g_array_index(backgrounds
, Background
, backgrounds
->len
-1);
198 extract_values(value
, &value1
, &value2
, &value3
);
199 get_color (value1
, bg
->back
.color
);
200 if (value2
) bg
->back
.alpha
= (atoi (value2
) / 100.0);
201 else bg
->back
.alpha
= 0.5;
203 else if (strcmp (key
, "border_color") == 0) {
204 Background
* bg
= &g_array_index(backgrounds
, Background
, backgrounds
->len
-1);
205 extract_values(value
, &value1
, &value2
, &value3
);
206 get_color (value1
, bg
->border
.color
);
207 if (value2
) bg
->border
.alpha
= (atoi (value2
) / 100.0);
208 else bg
->border
.alpha
= 0.5;
212 else if (strcmp (key
, "panel_monitor") == 0) {
213 panel_config
.monitor
= config_get_monitor(value
);
215 else if (strcmp (key
, "panel_size") == 0) {
216 extract_values(value
, &value1
, &value2
, &value3
);
219 if ((b
= strchr (value1
, '%'))) {
221 panel_config
.pourcentx
= 1;
223 panel_config
.area
.width
= atoi(value1
);
224 if (panel_config
.area
.width
== 0) {
226 panel_config
.area
.width
= 100;
227 panel_config
.pourcentx
= 1;
230 if ((b
= strchr (value2
, '%'))) {
232 panel_config
.pourcenty
= 1;
234 panel_config
.area
.height
= atoi(value2
);
237 else if (strcmp (key
, "panel_margin") == 0) {
238 extract_values(value
, &value1
, &value2
, &value3
);
239 panel_config
.marginx
= atoi (value1
);
240 if (value2
) panel_config
.marginy
= atoi (value2
);
242 else if (strcmp (key
, "panel_padding") == 0) {
243 extract_values(value
, &value1
, &value2
, &value3
);
244 panel_config
.area
.paddingxlr
= panel_config
.area
.paddingx
= atoi (value1
);
245 if (value2
) panel_config
.area
.paddingy
= atoi (value2
);
246 if (value3
) panel_config
.area
.paddingx
= atoi (value3
);
248 else if (strcmp (key
, "panel_position") == 0) {
249 extract_values(value
, &value1
, &value2
, &value3
);
250 if (strcmp (value1
, "top") == 0) panel_position
= TOP
;
252 if (strcmp (value1
, "bottom") == 0) panel_position
= BOTTOM
;
253 else panel_position
= CENTER
;
256 if (!value2
) panel_position
|= CENTER
;
258 if (strcmp (value2
, "left") == 0) panel_position
|= LEFT
;
260 if (strcmp (value2
, "right") == 0) panel_position
|= RIGHT
;
261 else panel_position
|= CENTER
;
264 if (!value3
) panel_horizontal
= 1;
266 if (strcmp (value3
, "vertical") == 0) panel_horizontal
= 0;
267 else panel_horizontal
= 1;
270 else if (strcmp (key
, "font_shadow") == 0)
271 panel_config
.g_task
.font_shadow
= atoi (value
);
272 else if (strcmp (key
, "panel_background_id") == 0) {
273 int id
= atoi (value
);
274 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
275 panel_config
.area
.bg
= &g_array_index(backgrounds
, Background
, id
);
277 else if (strcmp (key
, "wm_menu") == 0)
278 wm_menu
= atoi (value
);
279 else if (strcmp (key
, "panel_dock") == 0)
280 panel_dock
= atoi (value
);
281 else if (strcmp (key
, "urgent_nb_of_blink") == 0)
282 max_tick_urgent
= (atoi (value
) * 2) + 1;
283 else if (strcmp (key
, "panel_layer") == 0) {
284 if (strcmp(value
, "bottom") == 0)
285 panel_layer
= BOTTOM_LAYER
;
286 else if (strcmp(value
, "top") == 0)
287 panel_layer
= TOP_LAYER
;
289 panel_layer
= NORMAL_LAYER
;
293 else if (strcmp (key
, "battery") == 0) {
294 #ifdef ENABLE_BATTERY
299 fprintf(stderr
, "tint2 is build without battery support\n");
302 else if (strcmp (key
, "battery_low_status") == 0) {
303 #ifdef ENABLE_BATTERY
304 battery_low_status
= atoi(value
);
305 if(battery_low_status
< 0 || battery_low_status
> 100)
306 battery_low_status
= 0;
309 else if (strcmp (key
, "battery_low_cmd") == 0) {
310 #ifdef ENABLE_BATTERY
311 if (strlen(value
) > 0)
312 battery_low_cmd
= strdup (value
);
315 else if (strcmp (key
, "bat1_font") == 0) {
316 #ifdef ENABLE_BATTERY
317 bat1_font_desc
= pango_font_description_from_string (value
);
320 else if (strcmp (key
, "bat2_font") == 0) {
321 #ifdef ENABLE_BATTERY
322 bat2_font_desc
= pango_font_description_from_string (value
);
325 else if (strcmp (key
, "battery_font_color") == 0) {
326 #ifdef ENABLE_BATTERY
327 extract_values(value
, &value1
, &value2
, &value3
);
328 get_color (value1
, panel_config
.battery
.font
.color
);
329 if (value2
) panel_config
.battery
.font
.alpha
= (atoi (value2
) / 100.0);
330 else panel_config
.battery
.font
.alpha
= 0.5;
333 else if (strcmp (key
, "battery_padding") == 0) {
334 #ifdef ENABLE_BATTERY
335 extract_values(value
, &value1
, &value2
, &value3
);
336 panel_config
.battery
.area
.paddingxlr
= panel_config
.battery
.area
.paddingx
= atoi (value1
);
337 if (value2
) panel_config
.battery
.area
.paddingy
= atoi (value2
);
338 if (value3
) panel_config
.battery
.area
.paddingx
= atoi (value3
);
341 else if (strcmp (key
, "battery_background_id") == 0) {
342 #ifdef ENABLE_BATTERY
343 int id
= atoi (value
);
344 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
345 panel_config
.battery
.area
.bg
= &g_array_index(backgrounds
, Background
, id
);
348 else if (strcmp (key
, "battery_hide") == 0) {
349 #ifdef ENABLE_BATTERY
350 percentage_hide
= atoi (value
);
351 if (percentage_hide
== 0)
352 percentage_hide
= 101;
357 else if (strcmp (key
, "time1_format") == 0) {
358 if (strlen(value
) > 0) {
359 time1_format
= strdup (value
);
363 else if (strcmp (key
, "time2_format") == 0) {
364 if (strlen(value
) > 0)
365 time2_format
= strdup (value
);
367 else if (strcmp (key
, "time1_font") == 0) {
368 time1_font_desc
= pango_font_description_from_string (value
);
370 else if (strcmp(key
, "time1_timezone") == 0) {
371 if (strlen(value
) > 0)
372 time1_timezone
= strdup(value
);
374 else if (strcmp(key
, "time2_timezone") == 0) {
375 if (strlen(value
) > 0)
376 time2_timezone
= strdup(value
);
378 else if (strcmp (key
, "time2_font") == 0) {
379 time2_font_desc
= pango_font_description_from_string (value
);
381 else if (strcmp (key
, "clock_font_color") == 0) {
382 extract_values(value
, &value1
, &value2
, &value3
);
383 get_color (value1
, panel_config
.clock
.font
.color
);
384 if (value2
) panel_config
.clock
.font
.alpha
= (atoi (value2
) / 100.0);
385 else panel_config
.clock
.font
.alpha
= 0.5;
387 else if (strcmp (key
, "clock_padding") == 0) {
388 extract_values(value
, &value1
, &value2
, &value3
);
389 panel_config
.clock
.area
.paddingxlr
= panel_config
.clock
.area
.paddingx
= atoi (value1
);
390 if (value2
) panel_config
.clock
.area
.paddingy
= atoi (value2
);
391 if (value3
) panel_config
.clock
.area
.paddingx
= atoi (value3
);
393 else if (strcmp (key
, "clock_background_id") == 0) {
394 int id
= atoi (value
);
395 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
396 panel_config
.clock
.area
.bg
= &g_array_index(backgrounds
, Background
, id
);
398 else if (strcmp(key
, "clock_tooltip") == 0) {
399 if (strlen(value
) > 0)
400 time_tooltip_format
= strdup (value
);
402 else if (strcmp(key
, "clock_tooltip_timezone") == 0) {
403 if (strlen(value
) > 0)
404 time_tooltip_timezone
= strdup(value
);
406 else if (strcmp(key
, "clock_lclick_command") == 0) {
407 if (strlen(value
) > 0)
408 clock_lclick_command
= strdup(value
);
410 else if (strcmp(key
, "clock_rclick_command") == 0) {
411 if (strlen(value
) > 0)
412 clock_rclick_command
= strdup(value
);
416 else if (strcmp (key
, "taskbar_mode") == 0) {
417 if (strcmp (value
, "multi_desktop") == 0) panel_mode
= MULTI_DESKTOP
;
418 else panel_mode
= SINGLE_DESKTOP
;
420 else if (strcmp (key
, "taskbar_padding") == 0) {
421 extract_values(value
, &value1
, &value2
, &value3
);
422 panel_config
.g_taskbar
.area
.paddingxlr
= panel_config
.g_taskbar
.area
.paddingx
= atoi (value1
);
423 if (value2
) panel_config
.g_taskbar
.area
.paddingy
= atoi (value2
);
424 if (value3
) panel_config
.g_taskbar
.area
.paddingx
= atoi (value3
);
426 else if (strcmp (key
, "taskbar_background_id") == 0) {
427 int id
= atoi (value
);
428 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
429 panel_config
.g_taskbar
.bg
= &g_array_index(backgrounds
, Background
, id
);
430 panel_config
.g_taskbar
.area
.bg
= panel_config
.g_taskbar
.bg
;
432 else if (strcmp (key
, "taskbar_active_background_id") == 0) {
433 int id
= atoi (value
);
434 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
435 panel_config
.g_taskbar
.bg_active
= &g_array_index(backgrounds
, Background
, id
);
436 panel_config
.g_taskbar
.use_active
= 1;
440 else if (strcmp (key
, "task_text") == 0)
441 panel_config
.g_task
.text
= atoi (value
);
442 else if (strcmp (key
, "task_icon") == 0)
443 panel_config
.g_task
.icon
= atoi (value
);
444 else if (strcmp (key
, "task_centered") == 0)
445 panel_config
.g_task
.centered
= atoi (value
);
446 else if (strcmp (key
, "task_width") == 0) {
447 // old parameter : just for backward compatibility
448 panel_config
.g_task
.maximum_width
= atoi (value
);
449 panel_config
.g_task
.maximum_height
= 30;
451 else if (strcmp (key
, "task_maximum_size") == 0) {
452 extract_values(value
, &value1
, &value2
, &value3
);
453 panel_config
.g_task
.maximum_width
= atoi (value1
);
454 panel_config
.g_task
.maximum_height
= 30;
456 panel_config
.g_task
.maximum_height
= atoi (value2
);
458 else if (strcmp (key
, "task_padding") == 0) {
459 extract_values(value
, &value1
, &value2
, &value3
);
460 panel_config
.g_task
.area
.paddingxlr
= panel_config
.g_task
.area
.paddingx
= atoi (value1
);
461 if (value2
) panel_config
.g_task
.area
.paddingy
= atoi (value2
);
462 if (value3
) panel_config
.g_task
.area
.paddingx
= atoi (value3
);
464 else if (strcmp (key
, "task_font") == 0) {
465 panel_config
.g_task
.font_desc
= pango_font_description_from_string (value
);
467 else if (g_regex_match_simple("task.*_font_color", key
, 0, 0)) {
468 gchar
** split
= g_regex_split_simple("_", key
, 0, 0);
469 int status
= get_task_status(split
[1]);
471 extract_values(value
, &value1
, &value2
, &value3
);
473 if (value2
) alpha
= (atoi (value2
) / 100.0);
474 get_color (value1
, panel_config
.g_task
.font
[status
].color
);
475 panel_config
.g_task
.font
[status
].alpha
= alpha
;
476 panel_config
.g_task
.config_font_mask
|= (1<<status
);
478 else if (g_regex_match_simple("task.*_icon_asb", key
, 0, 0)) {
479 gchar
** split
= g_regex_split_simple("_", key
, 0, 0);
480 int status
= get_task_status(split
[1]);
482 extract_values(value
, &value1
, &value2
, &value3
);
483 panel_config
.g_task
.alpha
[status
] = atoi(value1
);
484 panel_config
.g_task
.saturation
[status
] = atoi(value2
);
485 panel_config
.g_task
.brightness
[status
] = atoi(value3
);
486 panel_config
.g_task
.config_asb_mask
|= (1<<status
);
488 else if (g_regex_match_simple("task.*_background_id", key
, 0, 0)) {
489 gchar
** split
= g_regex_split_simple("_", key
, 0, 0);
490 int status
= get_task_status(split
[1]);
492 int id
= atoi (value
);
493 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
494 panel_config
.g_task
.background
[status
] = &g_array_index(backgrounds
, Background
, id
);
495 panel_config
.g_task
.config_background_mask
|= (1<<status
);
496 if (status
== TASK_NORMAL
) panel_config
.g_task
.area
.bg
= panel_config
.g_task
.background
[TASK_NORMAL
];
500 // systray disabled in snapshot mode
501 else if (strcmp (key
, "systray") == 0 && snapshot_path
== 0) {
502 systray_enabled
= atoi(value
);
503 // systray is latest option added. files without 'systray' are old.
506 else if (strcmp (key
, "systray_padding") == 0 && snapshot_path
== 0) {
509 extract_values(value
, &value1
, &value2
, &value3
);
510 systray
.area
.paddingxlr
= systray
.area
.paddingx
= atoi (value1
);
511 if (value2
) systray
.area
.paddingy
= atoi (value2
);
512 if (value3
) systray
.area
.paddingx
= atoi (value3
);
514 else if (strcmp (key
, "systray_background_id") == 0) {
515 int id
= atoi (value
);
516 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
517 systray
.area
.bg
= &g_array_index(backgrounds
, Background
, id
);
519 else if (strcmp(key
, "systray_sort") == 0) {
520 if (strcmp(value
, "descending") == 0)
522 else if (strcmp(value
, "ascending") == 0)
524 else if (strcmp(value
, "left2right") == 0)
526 else if (strcmp(value
, "right2left") == 0)
529 else if (strcmp(key
, "systray_icon_size") == 0) {
530 systray_max_icon_size
= atoi(value
);
532 else if (strcmp(key
, "systray_icon_asb") == 0) {
533 extract_values(value
, &value1
, &value2
, &value3
);
534 systray
.alpha
= atoi(value1
);
535 systray
.saturation
= atoi(value2
);
536 systray
.brightness
= atoi(value3
);
540 else if (strcmp (key
, "tooltip") == 0)
541 g_tooltip
.enabled
= atoi(value
);
542 else if (strcmp (key
, "tooltip_show_timeout") == 0) {
543 int timeout_msec
= 1000*atof(value
);
544 g_tooltip
.show_timeout_msec
= timeout_msec
;
546 else if (strcmp (key
, "tooltip_hide_timeout") == 0) {
547 int timeout_msec
= 1000*atof(value
);
548 g_tooltip
.hide_timeout_msec
= timeout_msec
;
550 else if (strcmp (key
, "tooltip_padding") == 0) {
551 extract_values(value
, &value1
, &value2
, &value3
);
552 if (value1
) g_tooltip
.paddingx
= atoi(value1
);
553 if (value2
) g_tooltip
.paddingy
= atoi(value2
);
555 else if (strcmp (key
, "tooltip_background_id") == 0) {
556 int id
= atoi (value
);
557 id
= (id
< backgrounds
->len
&& id
>= 0) ? id
: 0;
558 g_tooltip
.bg
= &g_array_index(backgrounds
, Background
, id
);
560 else if (strcmp (key
, "tooltip_font_color") == 0) {
561 extract_values(value
, &value1
, &value2
, &value3
);
562 get_color(value1
, g_tooltip
.font_color
.color
);
563 if (value2
) g_tooltip
.font_color
.alpha
= (atoi (value2
) / 100.0);
564 else g_tooltip
.font_color
.alpha
= 0.1;
566 else if (strcmp (key
, "tooltip_font") == 0) {
567 g_tooltip
.font_desc
= pango_font_description_from_string(value
);
571 else if (strcmp (key
, "mouse_middle") == 0)
572 get_action (value
, &mouse_middle
);
573 else if (strcmp (key
, "mouse_right") == 0)
574 get_action (value
, &mouse_right
);
575 else if (strcmp (key
, "mouse_scroll_up") == 0)
576 get_action (value
, &mouse_scroll_up
);
577 else if (strcmp (key
, "mouse_scroll_down") == 0)
578 get_action (value
, &mouse_scroll_down
);
580 /* autohide options */
581 else if (strcmp(key
, "autohide") == 0)
582 panel_autohide
= atoi(value
);
583 else if (strcmp(key
, "autohide_show_timeout") == 0)
584 panel_autohide_show_timeout
= 1000*atof(value
);
585 else if (strcmp(key
, "autohide_hide_timeout") == 0)
586 panel_autohide_hide_timeout
= 1000*atof(value
);
587 else if (strcmp(key
, "strut_policy") == 0) {
588 if (strcmp(value
, "follow_size") == 0)
589 panel_strut_policy
= STRUT_FOLLOW_SIZE
;
590 else if (strcmp(value
, "none") == 0)
591 panel_strut_policy
= STRUT_NONE
;
593 panel_strut_policy
= STRUT_MINIMUM
;
595 else if (strcmp(key
, "autohide_height") == 0) {
596 panel_autohide_height
= atoi(value
);
597 if (panel_autohide_height
== 0) {
598 // autohide need height > 0
599 panel_autohide_height
= 1;
604 fprintf(stderr
, "tint2 : invalid option \"%s\",\n upgrade tint2 or correct your config file\n", key
);
606 if (value1
) free (value1
);
607 if (value2
) free (value2
);
608 if (value3
) free (value3
);
614 const gchar
* const * system_dirs
;
618 // follow XDG specification
619 // check tint2rc in user directory
620 path1
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL
);
621 if (g_file_test (path1
, G_FILE_TEST_EXISTS
)) {
622 i
= config_read_file (path1
);
623 config_path
= strdup(path1
);
629 // copy tint2rc from system directory to user directory
631 system_dirs
= g_get_system_config_dirs();
632 for (i
= 0; system_dirs
[i
]; i
++) {
633 path2
= g_build_filename(system_dirs
[i
], "tint2", "tint2rc", NULL
);
635 if (g_file_test(path2
, G_FILE_TEST_EXISTS
)) break;
641 // copy file in user directory (path1)
642 char *dir
= g_build_filename (g_get_user_config_dir(), "tint2", NULL
);
643 if (!g_file_test (dir
, G_FILE_TEST_IS_DIR
)) g_mkdir(dir
, 0777);
646 path1
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL
);
647 copy_file(path2
, path1
);
650 i
= config_read_file (path1
);
651 config_path
= strdup(path1
);
659 int config_read_file (const char *path
)
665 if ((fp
= fopen(path
, "r")) == NULL
) return 0;
667 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
668 if (parse_line(line
, &key
, &value
)) {
669 add_entry (key
, value
);