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