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