]> Dogcows Code - chaz/tint2/blob - src/config.c
play with vertical panel
[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 }
230
231
232 void add_entry (char *key, char *value)
233 {
234 char *value1=0, *value2=0, *value3=0;
235
236 /* Background and border */
237 if (strcmp (key, "rounded") == 0) {
238 // 'rounded' is the first parameter => alloc a new background
239 Area *a = calloc(1, sizeof(Area));
240 a->pix.border.rounded = atoi (value);
241 list_back = g_slist_append(list_back, a);
242 }
243 else if (strcmp (key, "border_width") == 0) {
244 Area *a = g_slist_last(list_back)->data;
245 a->pix.border.width = atoi (value);
246 }
247 else if (strcmp (key, "background_color") == 0) {
248 Area *a = g_slist_last(list_back)->data;
249 extract_values(value, &value1, &value2, &value3);
250 get_color (value1, a->pix.back.color);
251 if (value2) a->pix.back.alpha = (atoi (value2) / 100.0);
252 else a->pix.back.alpha = 0.5;
253 }
254 else if (strcmp (key, "border_color") == 0) {
255 Area *a = g_slist_last(list_back)->data;
256 extract_values(value, &value1, &value2, &value3);
257 get_color (value1, a->pix.border.color);
258 if (value2) a->pix.border.alpha = (atoi (value2) / 100.0);
259 else a->pix.border.alpha = 0.5;
260 }
261
262 /* Panel */
263 else if (strcmp (key, "panel_monitor") == 0) {
264 if (strcmp (value, "all") == 0) panel_config->monitor = -1;
265 else {
266 panel_config->monitor = atoi (value);
267 if (panel_config->monitor > 0) panel_config->monitor -= 1;
268 }
269 }
270 else if (strcmp (key, "panel_size") == 0) {
271 extract_values(value, &value1, &value2, &value3);
272
273 char *b;
274 if ((b = strchr (value1, '%'))) {
275 b[0] = '\0';
276 panel_config->pourcentx = 1;
277 }
278 panel_config->initial_width = atof(value1);
279 if (value2) {
280 if ((b = strchr (value2, '%'))) {
281 b[0] = '\0';
282 panel_config->pourcenty = 1;
283 }
284 panel_config->initial_height = atof(value2);
285 }
286 }
287 else if (strcmp (key, "panel_margin") == 0) {
288 extract_values(value, &value1, &value2, &value3);
289 panel_config->marginx = atoi (value1);
290 if (value2) panel_config->marginy = atoi (value2);
291 }
292 else if (strcmp (key, "panel_padding") == 0) {
293 extract_values(value, &value1, &value2, &value3);
294 panel_config->area.paddingxlr = panel_config->area.paddingx = atoi (value1);
295 if (value2) panel_config->area.paddingy = atoi (value2);
296 if (value3) panel_config->area.paddingx = atoi (value3);
297 }
298 else if (strcmp (key, "panel_position") == 0) {
299 extract_values(value, &value1, &value2, &value3);
300 if (strcmp (value1, "top") == 0) panel_position = TOP;
301 else {
302 if (strcmp (value1, "bottom") == 0) panel_position = BOTTOM;
303 else panel_position = CENTER;
304 }
305
306 if (!value2) panel_position |= CENTER;
307 else {
308 if (strcmp (value2, "left") == 0) panel_position |= LEFT;
309 else {
310 if (strcmp (value2, "right") == 0) panel_position |= RIGHT;
311 else panel_position |= CENTER;
312 }
313 }
314 if (!value3) panel_horizontal = 1;
315 else {
316 if (strcmp (value3, "vertical") == 0) panel_horizontal = 0;
317 else panel_horizontal = 1;
318 }
319 }
320 else if (strcmp (key, "font_shadow") == 0)
321 panel_config->g_task.font_shadow = atoi (value);
322 else if (strcmp (key, "panel_background_id") == 0) {
323 int id = atoi (value);
324 Area *a = g_slist_nth_data(list_back, id);
325 memcpy(&panel_config->area.pix.back, &a->pix.back, sizeof(Color));
326 memcpy(&panel_config->area.pix.border, &a->pix.border, sizeof(Border));
327 }
328 else if (strcmp (key, "wm_menu") == 0)
329 wm_menu = atoi (value);
330 else if (strcmp (key, "urgent_nb_of_blink") == 0)
331 max_tick_urgent = (atoi (value) * 2) + 1;
332
333 /* Battery */
334 #ifdef ENABLE_BATTERY
335 else if (strcmp (key, "battery") == 0) {
336 if(atoi(value) == 1)
337 panel_config->battery.area.on_screen = 1;
338 }
339 else if (strcmp (key, "battery_low_status") == 0) {
340 battery_low_status = atoi(value);
341 if(battery_low_status < 0 || battery_low_status > 100)
342 battery_low_status = 0;
343 }
344 else if (strcmp (key, "battery_low_cmd") == 0) {
345 if (battery_low_cmd) g_free(battery_low_cmd);
346 if (strlen(value) > 0) battery_low_cmd = strdup (value);
347 else battery_low_cmd = 0;
348 }
349 else if (strcmp (key, "bat1_font") == 0) {
350 if (save_file_config) old_bat1_font = strdup (value);
351 if (bat1_font_desc) pango_font_description_free(bat1_font_desc);
352 bat1_font_desc = pango_font_description_from_string (value);
353 }
354 else if (strcmp (key, "bat2_font") == 0) {
355 if (save_file_config) old_bat2_font = strdup (value);
356 if (bat2_font_desc) pango_font_description_free(bat2_font_desc);
357 bat2_font_desc = pango_font_description_from_string (value);
358 }
359 else if (strcmp (key, "battery_font_color") == 0) {
360 extract_values(value, &value1, &value2, &value3);
361 get_color (value1, panel_config->battery.font.color);
362 if (value2) panel_config->battery.font.alpha = (atoi (value2) / 100.0);
363 else panel_config->battery.font.alpha = 0.5;
364 }
365 else if (strcmp (key, "battery_padding") == 0) {
366 extract_values(value, &value1, &value2, &value3);
367 panel_config->battery.area.paddingxlr = panel_config->battery.area.paddingx = atoi (value1);
368 if (value2) panel_config->battery.area.paddingy = atoi (value2);
369 if (value3) panel_config->battery.area.paddingx = atoi (value3);
370 }
371 else if (strcmp (key, "battery_background_id") == 0) {
372 int id = atoi (value);
373 Area *a = g_slist_nth_data(list_back, id);
374 memcpy(&panel_config->battery.area.pix.back, &a->pix.back, sizeof(Color));
375 memcpy(&panel_config->battery.area.pix.border, &a->pix.border, sizeof(Border));
376 }
377 #else
378 else if ((strcmp (key, "battery") == 0) || (strcmp (key, "battery_low_status") == 0) || (strcmp (key, "battery_low_cmd") == 0) || (strcmp (key, "bat1_font") == 0) || (strcmp (key, "bat2_font") == 0) || (strcmp (key, "battery_font_color") == 0) || (strcmp (key, "battery_padding") == 0) || (strcmp (key, "battery_background_id") == 0)) {
379 printf("tint2 is build without battery support\n");
380 }
381 #endif
382
383 /* Clock */
384 else if (strcmp (key, "time1_format") == 0) {
385 if (time1_format) g_free(time1_format);
386 if (strlen(value) > 0) {
387 time1_format = strdup (value);
388 panel_config->clock.area.on_screen = 1;
389 }
390 else {
391 time1_format = 0;
392 panel_config->clock.area.on_screen = 0;
393 }
394 }
395 else if (strcmp (key, "time2_format") == 0) {
396 if (time2_format) g_free(time2_format);
397 if (strlen(value) > 0) time2_format = strdup (value);
398 else time2_format = 0;
399 }
400 else if (strcmp (key, "time1_font") == 0) {
401 if (save_file_config) old_time1_font = strdup (value);
402 if (time1_font_desc) pango_font_description_free(time1_font_desc);
403 time1_font_desc = pango_font_description_from_string (value);
404 }
405 else if (strcmp (key, "time2_font") == 0) {
406 if (save_file_config) old_time2_font = strdup (value);
407 if (time2_font_desc) pango_font_description_free(time2_font_desc);
408 time2_font_desc = pango_font_description_from_string (value);
409 }
410 else if (strcmp (key, "clock_font_color") == 0) {
411 extract_values(value, &value1, &value2, &value3);
412 get_color (value1, panel_config->clock.font.color);
413 if (value2) panel_config->clock.font.alpha = (atoi (value2) / 100.0);
414 else panel_config->clock.font.alpha = 0.5;
415 }
416 else if (strcmp (key, "clock_padding") == 0) {
417 extract_values(value, &value1, &value2, &value3);
418 panel_config->clock.area.paddingxlr = panel_config->clock.area.paddingx = atoi (value1);
419 if (value2) panel_config->clock.area.paddingy = atoi (value2);
420 if (value3) panel_config->clock.area.paddingx = atoi (value3);
421 }
422 else if (strcmp (key, "clock_background_id") == 0) {
423 int id = atoi (value);
424 Area *a = g_slist_nth_data(list_back, id);
425 memcpy(&panel_config->clock.area.pix.back, &a->pix.back, sizeof(Color));
426 memcpy(&panel_config->clock.area.pix.border, &a->pix.border, sizeof(Border));
427 }
428 else if (strcmp(key, "clock_lclick_command") == 0) {
429 if (clock_lclick_command) g_free(clock_lclick_command);
430 if (strlen(value) > 0) clock_lclick_command = strdup(value);
431 else clock_lclick_command = 0;
432 }
433 else if (strcmp(key, "clock_rclick_command") == 0) {
434 if (clock_rclick_command) g_free(clock_rclick_command);
435 if (strlen(value) > 0) clock_rclick_command = strdup(value);
436 else clock_rclick_command = 0;
437 }
438
439 /* Taskbar */
440 else if (strcmp (key, "taskbar_mode") == 0) {
441 if (strcmp (value, "multi_desktop") == 0) panel_mode = MULTI_DESKTOP;
442 else panel_mode = SINGLE_DESKTOP;
443 }
444 else if (strcmp (key, "taskbar_padding") == 0) {
445 extract_values(value, &value1, &value2, &value3);
446 panel_config->g_taskbar.paddingxlr = panel_config->g_taskbar.paddingx = atoi (value1);
447 if (value2) panel_config->g_taskbar.paddingy = atoi (value2);
448 if (value3) panel_config->g_taskbar.paddingx = atoi (value3);
449 }
450 else if (strcmp (key, "taskbar_background_id") == 0) {
451 int id = atoi (value);
452 Area *a = g_slist_nth_data(list_back, id);
453 memcpy(&panel_config->g_taskbar.pix.back, &a->pix.back, sizeof(Color));
454 memcpy(&panel_config->g_taskbar.pix.border, &a->pix.border, sizeof(Border));
455 }
456
457 /* Task */
458 else if (strcmp (key, "task_text") == 0)
459 panel_config->g_task.text = atoi (value);
460 else if (strcmp (key, "task_icon") == 0)
461 panel_config->g_task.icon = atoi (value);
462 else if (strcmp (key, "task_centered") == 0)
463 panel_config->g_task.centered = atoi (value);
464 else if (strcmp (key, "task_width") == 0)
465 panel_config->g_task.maximum_width = atoi (value);
466 else if (strcmp (key, "task_padding") == 0) {
467 extract_values(value, &value1, &value2, &value3);
468 panel_config->g_task.area.paddingxlr = panel_config->g_task.area.paddingx = atoi (value1);
469 if (value2) panel_config->g_task.area.paddingy = atoi (value2);
470 if (value3) panel_config->g_task.area.paddingx = atoi (value3);
471 }
472 else if (strcmp (key, "task_font") == 0) {
473 if (save_file_config) old_task_font = strdup (value);
474 if (panel_config->g_task.font_desc) pango_font_description_free(panel_config->g_task.font_desc);
475 panel_config->g_task.font_desc = pango_font_description_from_string (value);
476 }
477 else if (strcmp (key, "task_font_color") == 0) {
478 extract_values(value, &value1, &value2, &value3);
479 get_color (value1, panel_config->g_task.font.color);
480 if (value2) panel_config->g_task.font.alpha = (atoi (value2) / 100.0);
481 else panel_config->g_task.font.alpha = 0.1;
482 }
483 else if (strcmp (key, "task_active_font_color") == 0) {
484 extract_values(value, &value1, &value2, &value3);
485 get_color (value1, panel_config->g_task.font_active.color);
486 if (value2) panel_config->g_task.font_active.alpha = (atoi (value2) / 100.0);
487 else panel_config->g_task.font_active.alpha = 0.1;
488 }
489 else if (strcmp (key, "task_background_id") == 0) {
490 int id = atoi (value);
491 Area *a = g_slist_nth_data(list_back, id);
492 memcpy(&panel_config->g_task.area.pix.back, &a->pix.back, sizeof(Color));
493 memcpy(&panel_config->g_task.area.pix.border, &a->pix.border, sizeof(Border));
494 }
495 else if (strcmp (key, "task_active_background_id") == 0) {
496 int id = atoi (value);
497 Area *a = g_slist_nth_data(list_back, id);
498 memcpy(&panel_config->g_task.area.pix_active.back, &a->pix.back, sizeof(Color));
499 memcpy(&panel_config->g_task.area.pix_active.border, &a->pix.border, sizeof(Border));
500 }
501
502 /* Systray */
503 else if (strcmp (key, "systray_padding") == 0) {
504 extract_values(value, &value1, &value2, &value3);
505 systray.area.paddingxlr = systray.area.paddingx = atoi (value1);
506 if (value2) systray.area.paddingy = atoi (value2);
507 if (value3) systray.area.paddingx = atoi (value3);
508 systray.area.on_screen = 1;
509 }
510 else if (strcmp (key, "systray_background_id") == 0) {
511 int id = atoi (value);
512 Area *a = g_slist_nth_data(list_back, id);
513 memcpy(&systray.area.pix.back, &a->pix.back, sizeof(Color));
514 memcpy(&systray.area.pix.border, &a->pix.border, sizeof(Border));
515 }
516
517 /* Mouse actions */
518 else if (strcmp (key, "mouse_middle") == 0)
519 get_action (value, &mouse_middle);
520 else if (strcmp (key, "mouse_right") == 0)
521 get_action (value, &mouse_right);
522 else if (strcmp (key, "mouse_scroll_up") == 0)
523 get_action (value, &mouse_scroll_up);
524 else if (strcmp (key, "mouse_scroll_down") == 0)
525 get_action (value, &mouse_scroll_down);
526
527
528 /* Read tint-0.6 config for backward compatibility */
529 else if (strcmp (key, "panel_mode") == 0) {
530 save_file_config = 1;
531 if (strcmp (value, "single_desktop") == 0) panel_mode = SINGLE_DESKTOP;
532 else panel_mode = MULTI_DESKTOP;
533 }
534 else if (strcmp (key, "panel_rounded") == 0) {
535 Area *a = calloc(1, sizeof(Area));
536 a->pix.border.rounded = atoi (value);
537 list_back = g_slist_append(list_back, a);
538 }
539 else if (strcmp (key, "panel_border_width") == 0) {
540 Area *a = g_slist_last(list_back)->data;
541 a->pix.border.width = atoi (value);
542 }
543 else if (strcmp (key, "panel_background_color") == 0) {
544 Area *a = g_slist_last(list_back)->data;
545 extract_values(value, &value1, &value2, &value3);
546 get_color (value1, a->pix.back.color);
547 if (value2) a->pix.back.alpha = (atoi (value2) / 100.0);
548 else a->pix.back.alpha = 0.5;
549 }
550 else if (strcmp (key, "panel_border_color") == 0) {
551 Area *a = g_slist_last(list_back)->data;
552 extract_values(value, &value1, &value2, &value3);
553 get_color (value1, a->pix.border.color);
554 if (value2) a->pix.border.alpha = (atoi (value2) / 100.0);
555 else a->pix.border.alpha = 0.5;
556 }
557 else if (strcmp (key, "task_text_centered") == 0)
558 panel_config->g_task.centered = atoi (value);
559 else if (strcmp (key, "task_margin") == 0) {
560 panel_config->g_taskbar.paddingxlr = 0;
561 panel_config->g_taskbar.paddingx = atoi (value);
562 }
563 else if (strcmp (key, "task_icon_size") == 0)
564 old_task_icon_size = atoi (value);
565 else if (strcmp (key, "task_rounded") == 0) {
566 area_task = calloc(1, sizeof(Area));
567 area_task->pix.border.rounded = atoi (value);
568 list_back = g_slist_append(list_back, area_task);
569
570 area_task_active = calloc(1, sizeof(Area));
571 area_task_active->pix.border.rounded = atoi (value);
572 list_back = g_slist_append(list_back, area_task_active);
573 }
574 else if (strcmp (key, "task_background_color") == 0) {
575 extract_values(value, &value1, &value2, &value3);
576 get_color (value1, area_task->pix.back.color);
577 if (value2) area_task->pix.back.alpha = (atoi (value2) / 100.0);
578 else area_task->pix.back.alpha = 0.5;
579 }
580 else if (strcmp (key, "task_active_background_color") == 0) {
581 extract_values(value, &value1, &value2, &value3);
582 get_color (value1, area_task_active->pix.back.color);
583 if (value2) area_task_active->pix.back.alpha = (atoi (value2) / 100.0);
584 else area_task_active->pix.back.alpha = 0.5;
585 }
586 else if (strcmp (key, "task_border_width") == 0) {
587 area_task->pix.border.width = atoi (value);
588 area_task_active->pix.border.width = atoi (value);
589 }
590 else if (strcmp (key, "task_border_color") == 0) {
591 extract_values(value, &value1, &value2, &value3);
592 get_color (value1, area_task->pix.border.color);
593 if (value2) area_task->pix.border.alpha = (atoi (value2) / 100.0);
594 else area_task->pix.border.alpha = 0.5;
595 }
596 else if (strcmp (key, "task_active_border_color") == 0) {
597 extract_values(value, &value1, &value2, &value3);
598 get_color (value1, area_task_active->pix.border.color);
599 if (value2) area_task_active->pix.border.alpha = (atoi (value2) / 100.0);
600 else area_task_active->pix.border.alpha = 0.5;
601 }
602
603 else
604 fprintf(stderr, "tint2 : invalid option \"%s\", correct your config file\n", key);
605
606 if (value1) free (value1);
607 if (value2) free (value2);
608 if (value3) free (value3);
609 }
610
611
612 int parse_line (const char *line)
613 {
614 char *a, *b, *key, *value;
615
616 /* Skip useless lines */
617 if ((line[0] == '#') || (line[0] == '\n')) return 0;
618 if (!(a = strchr (line, '='))) return 0;
619
620 /* overwrite '=' with '\0' */
621 a[0] = '\0';
622 key = strdup (line);
623 a++;
624
625 /* overwrite '\n' with '\0' if '\n' present */
626 if ((b = strchr (a, '\n'))) b[0] = '\0';
627
628 value = strdup (a);
629
630 g_strstrip(key);
631 g_strstrip(value);
632
633 add_entry (key, value);
634
635 free (key);
636 free (value);
637 return 1;
638 }
639
640
641 void config_finish ()
642 {
643 if (panel_config->monitor > (server.nb_monitor-1)) {
644 fprintf(stderr, "tint2 exit : monitor %d not found.\n", panel_config->monitor+1);
645 exit(0);
646 }
647
648 // alloc panels
649 int i;
650 if (panel_config->monitor >= 0) {
651 // one monitor
652 nb_panel = 1;
653 panel1 = calloc(nb_panel, sizeof(Panel));
654 memcpy(panel1, panel_config, sizeof(Panel));
655 panel1->monitor = panel_config->monitor;
656 }
657 else {
658 // all monitors
659 nb_panel = server.nb_monitor;
660 panel1 = calloc(nb_panel, sizeof(Panel));
661
662 for (i=0 ; i < nb_panel ; i++) {
663 memcpy(&panel1[i], panel_config, sizeof(Panel));
664 panel1[i].monitor = i;
665 }
666 }
667
668 // TODO: user can configure layout => ordered objects in panel.area.list
669 // clock and systray before taskbar because resize(clock) can resize others object ??
670 init_panel();
671 init_clock();
672 #ifdef ENABLE_BATTERY
673 init_battery();
674 #endif
675 init_systray();
676 init_taskbar();
677 visible_object();
678
679 cleanup_config();
680
681 task_refresh_tasklist();
682 }
683
684
685 int config_read ()
686 {
687 const gchar * const * system_dirs;
688 char *path1;
689 gint i;
690
691 save_file_config = 0;
692
693 // follow XDG specification
694 deb:
695 // check tint2rc in user directory
696 path1 = g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL);
697 if (g_file_test (path1, G_FILE_TEST_EXISTS)) {
698 i = config_read_file (path1);
699 g_free(path1);
700 return i;
701 }
702
703 g_free(path1);
704 if (save_file_config) {
705 fprintf(stderr, "tint2 exit : enable to write $HOME/.config/tint2/tint2rc\n");
706 exit(0);
707 }
708
709 // check old tintrc config file
710 path1 = g_build_filename (g_get_user_config_dir(), "tint", "tintrc", NULL);
711 if (g_file_test (path1, G_FILE_TEST_EXISTS)) {
712 save_file_config = 1;
713 config_read_file (path1);
714 g_free(path1);
715 goto deb;
716 }
717
718 // copy tint2rc from system directory to user directory
719 g_free(path1);
720 char *path2 = 0;
721 system_dirs = g_get_system_config_dirs();
722 for (i = 0; system_dirs[i]; i++) {
723 path2 = g_build_filename(system_dirs[i], "tint2", "tint2rc", NULL);
724
725 if (g_file_test(path2, G_FILE_TEST_EXISTS)) break;
726 g_free (path2);
727 path2 = 0;
728 }
729
730 if (path2) {
731 // copy file in user directory (path1)
732 char *dir = g_build_filename (g_get_user_config_dir(), "tint2", NULL);
733 if (!g_file_test (dir, G_FILE_TEST_IS_DIR)) g_mkdir(dir, 0777);
734 g_free(dir);
735
736 path1 = g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL);
737 copy_file(path2, path1);
738 g_free(path2);
739
740 i = config_read_file (path1);
741 g_free(path1);
742 return i;
743 }
744 return 0;
745 }
746
747
748 int config_read_file (const char *path)
749 {
750 FILE *fp;
751 char line[80];
752
753 if ((fp = fopen(path, "r")) == NULL) return 0;
754 old_task_font = 0;
755 old_time1_font = 0;
756 old_time2_font = 0;
757
758 while (fgets(line, sizeof(line), fp) != NULL)
759 parse_line (line);
760
761 fclose (fp);
762
763 if (save_file_config)
764 save_config();
765
766 if (old_task_font) {
767 g_free(old_task_font);
768 old_task_font = 0;
769 }
770 if (old_time1_font) {
771 g_free(old_time1_font);
772 old_time1_font = 0;
773 }
774 if (old_time2_font) {
775 g_free(old_time2_font);
776 old_time2_font = 0;
777 }
778 return 1;
779 }
780
781
782 void save_config ()
783 {
784 fprintf(stderr, "tint2 : convert user's config file\n");
785
786 char *path, *dir;
787 FILE *fp;
788
789 if (old_task_icon_size) {
790 panel_config->g_task.area.paddingy = ((int)panel_config->initial_height - (2 * panel_config->area.paddingy) - old_task_icon_size) / 2;
791 }
792
793 dir = g_build_filename (g_get_user_config_dir(), "tint2", NULL);
794 if (!g_file_test (dir, G_FILE_TEST_IS_DIR)) g_mkdir(dir, 0777);
795 g_free(dir);
796
797 path = g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL);
798 fp = fopen(path, "w");
799 g_free(path);
800 if (fp == NULL) return;
801
802 fputs("#---------------------------------------------\n", fp);
803 fputs("# TINT2 CONFIG FILE\n", fp);
804 fputs("#---------------------------------------------\n\n", fp);
805 fputs("#---------------------------------------------\n", fp);
806 fputs("# BACKGROUND AND BORDER\n", fp);
807 fputs("#---------------------------------------------\n", fp);
808 GSList *l0;
809 Area *a;
810 l0 = list_back->next;
811 while (l0) {
812 a = l0->data;
813 fprintf(fp, "rounded = %d\n", a->pix.border.rounded);
814 fprintf(fp, "border_width = %d\n", a->pix.border.width);
815 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));
816 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));
817
818 l0 = l0->next;
819 }
820
821 fputs("#---------------------------------------------\n", fp);
822 fputs("# PANEL\n", fp);
823 fputs("#---------------------------------------------\n", fp);
824 fputs("panel_monitor = all\n", fp);
825 if (panel_position & BOTTOM) fputs("panel_position = bottom", fp);
826 else fputs("panel_position = top", fp);
827 if (panel_position & LEFT) fputs(" left\n", fp);
828 else if (panel_position & RIGHT) fputs(" right\n", fp);
829 else fputs(" center\n", fp);
830 fprintf(fp, "panel_size = %d %d\n", (int)panel_config->initial_width, (int)panel_config->initial_height);
831 fprintf(fp, "panel_margin = %d %d\n", panel_config->marginx, panel_config->marginy);
832 fprintf(fp, "panel_padding = %d %d %d\n", panel_config->area.paddingxlr, panel_config->area.paddingy, panel_config->area.paddingx);
833 fprintf(fp, "font_shadow = %d\n", panel_config->g_task.font_shadow);
834 fputs("panel_background_id = 1\n", fp);
835
836 fputs("\n#---------------------------------------------\n", fp);
837 fputs("# TASKBAR\n", fp);
838 fputs("#---------------------------------------------\n", fp);
839 if (panel_mode == MULTI_DESKTOP) fputs("taskbar_mode = multi_desktop\n", fp);
840 else fputs("taskbar_mode = single_desktop\n", fp);
841 fprintf(fp, "taskbar_padding = 0 0 %d\n", panel_config->g_taskbar.paddingx);
842 fputs("taskbar_background_id = 0\n", fp);
843
844 fputs("\n#---------------------------------------------\n", fp);
845 fputs("# TASK\n", fp);
846 fputs("#---------------------------------------------\n", fp);
847 if (old_task_icon_size) fputs("task_icon = 1\n", fp);
848 else fputs("task_icon = 0\n", fp);
849 fputs("task_text = 1\n", fp);
850 fprintf(fp, "task_width = %d\n", panel_config->g_task.maximum_width);
851 fprintf(fp, "task_centered = %d\n", panel_config->g_task.centered);
852 fprintf(fp, "task_padding = %d %d\n", panel_config->g_task.area.paddingx, panel_config->g_task.area.paddingy);
853 fprintf(fp, "task_font = %s\n", old_task_font);
854 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));
855 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));
856 fputs("task_background_id = 2\n", fp);
857 fputs("task_active_background_id = 3\n", fp);
858
859 fputs("\n#---------------------------------------------\n", fp);
860 fputs("# SYSTRAYBAR\n", fp);
861 fputs("#---------------------------------------------\n", fp);
862 fputs("systray_padding = 4 3 4\n", fp);
863 fputs("systray_background_id = 0\n", fp);
864
865 fputs("\n#---------------------------------------------\n", fp);
866 fputs("# CLOCK\n", fp);
867 fputs("#---------------------------------------------\n", fp);
868 if (time1_format) fprintf(fp, "time1_format = %s\n", time1_format);
869 else fputs("#time1_format = %H:%M\n", fp);
870 fprintf(fp, "time1_font = %s\n", old_time1_font);
871 if (time2_format) fprintf(fp, "time2_format = %s\n", time2_format);
872 else fputs("#time2_format = %A %d %B\n", fp);
873 fprintf(fp, "time2_font = %s\n", old_time2_font);
874 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));
875 fputs("clock_padding = 2 2\n", fp);
876 fputs("clock_background_id = 0\n", fp);
877
878 #ifdef ENABLE_BATTERY
879 fputs("\n#---------------------------------------------\n", fp);
880 fputs("# BATTERY\n", fp);
881 fputs("#---------------------------------------------\n", fp);
882 fprintf(fp, "battery = %d\n", panel_config->battery.area.on_screen);
883 fprintf(fp, "battery_low_status = %d\n", battery_low_status);
884 fprintf(fp, "battery_low_cmd = %s\n", battery_low_cmd);
885 fprintf(fp, "bat1_font = %s\n", old_bat1_font);
886 fprintf(fp, "bat2_font = %s\n", old_bat2_font);
887 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));
888 fputs("battery_padding = 2 2\n", fp);
889 fputs("battery_background_id = 0\n", fp);
890 #endif
891
892 fputs("\n#---------------------------------------------\n", fp);
893 fputs("# MOUSE ACTION ON TASK\n", fp);
894 fputs("#---------------------------------------------\n", fp);
895 if (mouse_middle == NONE) fputs("mouse_middle = none\n", fp);
896 else if (mouse_middle == CLOSE) fputs("mouse_middle = close\n", fp);
897 else if (mouse_middle == TOGGLE) fputs("mouse_middle = toggle\n", fp);
898 else if (mouse_middle == ICONIFY) fputs("mouse_middle = iconify\n", fp);
899 else if (mouse_middle == SHADE) fputs("mouse_middle = shade\n", fp);
900 else fputs("mouse_middle = toggle_iconify\n", fp);
901
902 if (mouse_right == NONE) fputs("mouse_right = none\n", fp);
903 else if (mouse_right == CLOSE) fputs("mouse_right = close\n", fp);
904 else if (mouse_right == TOGGLE) fputs("mouse_right = toggle\n", fp);
905 else if (mouse_right == ICONIFY) fputs("mouse_right = iconify\n", fp);
906 else if (mouse_right == SHADE) fputs("mouse_right = shade\n", fp);
907 else fputs("mouse_right = toggle_iconify\n", fp);
908
909 if (mouse_scroll_up == NONE) fputs("mouse_scroll_up = none\n", fp);
910 else if (mouse_scroll_up == CLOSE) fputs("mouse_scroll_up = close\n", fp);
911 else if (mouse_scroll_up == TOGGLE) fputs("mouse_scroll_up = toggle\n", fp);
912 else if (mouse_scroll_up == ICONIFY) fputs("mouse_scroll_up = iconify\n", fp);
913 else if (mouse_scroll_up == SHADE) fputs("mouse_scroll_up = shade\n", fp);
914 else fputs("mouse_scroll_up = toggle_iconify\n", fp);
915
916 if (mouse_scroll_down == NONE) fputs("mouse_scroll_down = none\n", fp);
917 else if (mouse_scroll_down == CLOSE) fputs("mouse_scroll_down = close\n", fp);
918 else if (mouse_scroll_down == TOGGLE) fputs("mouse_scroll_down = toggle\n", fp);
919 else if (mouse_scroll_down == ICONIFY) fputs("mouse_scroll_down = iconify\n", fp);
920 else if (mouse_scroll_down == SHADE) fputs("mouse_scroll_down = shade\n", fp);
921 else fputs("mouse_scroll_down = toggle_iconify\n", fp);
922
923 fputs("\n\n", fp);
924 fclose (fp);
925 }
926
This page took 0.103292 seconds and 5 git commands to generate.