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