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