-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
config.c
749 lines (670 loc) · 24.9 KB
/
config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
/*
* Gromit-MPX -- a program for painting on the screen
*
* Gromit Copyright (C) 2000 Simon Budig <Simon.Budig@unix-ag.org>
*
* Gromit-MPX Copyright (C) 2009,2010 Christian Beier <dontmind@freeshell.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include "config.h"
#include "main.h"
#include "math.h"
#include "build-config.h"
#define KEY_DFLT_SHOW_INTRO_ON_STARTUP TRUE
#define KEYFILE_FLAGS G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS
static gpointer HOTKEY_SYMBOL_VALUE = (gpointer) 3;
static gpointer UNDOKEY_SYMBOL_VALUE = (gpointer) 4;
/*
* Functions for parsing the Configuration-file
*/
static gchar* parse_name (GScanner *scanner)
{
GTokenType token;
guint buttons = 0;
guint modifier = 0;
guint len = 0;
gchar *name;
token = g_scanner_cur_token(scanner);
if (token != G_TOKEN_STRING)
{
g_scanner_unexp_token (scanner, G_TOKEN_STRING, NULL,
NULL, NULL, "aborting", TRUE);
return NULL;
}
len = strlen (scanner->value.v_string);
name = g_strndup (scanner->value.v_string, len + 3);
token = g_scanner_get_next_token (scanner);
/*
* Are there any options to limit the scope of the definition?
*/
if (token == G_TOKEN_LEFT_BRACE)
{
g_scanner_set_scope (scanner, 1);
scanner->config->int_2_float = 0;
modifier = buttons = 0;
while ((token = g_scanner_get_next_token (scanner))
!= G_TOKEN_RIGHT_BRACE)
{
if (token == G_TOKEN_SYMBOL)
{
if ((intptr_t) scanner->value.v_symbol < 11)
buttons |= 1 << ((intptr_t) scanner->value.v_symbol - 1);
else
modifier |= 1 << ((intptr_t) scanner->value.v_symbol - 11);
}
else if (token == G_TOKEN_INT)
{
if (scanner->value.v_int <= 5 && scanner->value.v_int > 0)
buttons |= 1 << (scanner->value.v_int - 1);
else
g_printerr ("Only Buttons 1-5 are supported!\n");
}
else
{
g_printerr ("skipped token\n");
}
}
g_scanner_set_scope (scanner, 0);
scanner->config->int_2_float = 1;
token = g_scanner_get_next_token (scanner);
}
name [len] = 124;
name [len+1] = buttons + 64;
name [len+2] = modifier + 48;
name [len+3] = 0;
return name;
}
enum tool_arguments {
SYM_SIZE = 1,
SYM_COLOR,
SYM_ARROWSIZE,
SYM_ARROWTYPE,
SYM_MINSIZE,
SYM_MAXSIZE,
SYM_MINLEN,
SYM_MAXANGLE,
SYM_RADIUS,
SYM_SIMPLIFY,
SYM_SNAP,
};
/*
* get "=VALUE", where VALUE is a float
* returns NAN is an error occurs
*/
gfloat parse_get_float(GScanner *scanner, const gchar *msg)
{
GTokenType token = g_scanner_get_next_token (scanner);
if (token != G_TOKEN_EQUAL_SIGN)
{
g_printerr ("Missing \"=\"... aborting\n");
return NAN;
}
token = g_scanner_get_next_token (scanner);
if (token != G_TOKEN_FLOAT)
{
g_printerr ("%s", msg);
g_printerr ("... aborting\n");
return NAN;
}
return scanner->value.v_float;
}
gboolean parse_config (GromitData *data)
{
gboolean status = FALSE;
GromitPaintContext *context=NULL;
GromitPaintContext *context_template=NULL;
GScanner *scanner;
GTokenType token;
gchar *filename;
int file;
gchar *name, *copy;
GromitPaintType type;
GdkRGBA *fg_color=NULL;
guint width, arrowsize, minwidth, maxwidth;
guint minlen, maxangle, radius, simplify, snapdist;
GromitArrowType arrowtype;
/* try user config location */
filename = g_strjoin (G_DIR_SEPARATOR_S,
g_get_user_config_dir(), "gromit-mpx.cfg", NULL);
if ((file = open(filename, O_RDONLY)) < 0)
g_print("Could not open user config %s: %s\n", filename, g_strerror (errno));
else
g_print("Using user config %s\n", filename);
/* try global config file */
if (file < 0) {
g_free(filename);
filename = g_strdup (SYSCONFDIR "/gromit-mpx/gromit-mpx.cfg");
if ((file = open(filename, O_RDONLY)) < 0)
g_print("Could not open system config %s: %s\n", filename, g_strerror (errno));
else
g_print("Using system config %s\n", filename);
}
/* was the last possibility, no use to go on */
if (file < 0) {
g_free(filename);
GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(data->win),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_CLOSE,
_("No usable config file found, falling back to default tools."));
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
return FALSE;
}
scanner = g_scanner_new (NULL);
scanner->input_name = filename;
scanner->config->case_sensitive = 0;
scanner->config->scan_octal = 0;
scanner->config->identifier_2_string = 0;
scanner->config->char_2_token = 1;
scanner->config->numbers_2_int = 1;
scanner->config->int_2_float = 1;
g_scanner_scope_add_symbol (scanner, 0, "PEN", (gpointer) GROMIT_PEN);
g_scanner_scope_add_symbol (scanner, 0, "LINE", (gpointer) GROMIT_LINE);
g_scanner_scope_add_symbol (scanner, 0, "RECT", (gpointer) GROMIT_RECT);
g_scanner_scope_add_symbol (scanner, 0, "SMOOTH", (gpointer) GROMIT_SMOOTH);
g_scanner_scope_add_symbol (scanner, 0, "ORTHOGONAL",(gpointer) GROMIT_ORTHOGONAL);
g_scanner_scope_add_symbol (scanner, 0, "ERASER", (gpointer) GROMIT_ERASER);
g_scanner_scope_add_symbol (scanner, 0, "RECOLOR", (gpointer) GROMIT_RECOLOR);
g_scanner_scope_add_symbol (scanner, 0, "HOTKEY", HOTKEY_SYMBOL_VALUE);
g_scanner_scope_add_symbol (scanner, 0, "UNDOKEY", UNDOKEY_SYMBOL_VALUE);
g_scanner_scope_add_symbol (scanner, 1, "BUTTON1", (gpointer) 1);
g_scanner_scope_add_symbol (scanner, 1, "BUTTON2", (gpointer) 2);
g_scanner_scope_add_symbol (scanner, 1, "BUTTON3", (gpointer) 3);
g_scanner_scope_add_symbol (scanner, 1, "BUTTON4", (gpointer) 4);
g_scanner_scope_add_symbol (scanner, 1, "BUTTON5", (gpointer) 5);
g_scanner_scope_add_symbol (scanner, 1, "SHIFT", (gpointer) 11);
g_scanner_scope_add_symbol (scanner, 1, "CONTROL", (gpointer) 12);
g_scanner_scope_add_symbol (scanner, 1, "META", (gpointer) 13);
g_scanner_scope_add_symbol (scanner, 1, "ALT", (gpointer) 13);
g_scanner_scope_add_symbol (scanner, 2, "size", (gpointer) SYM_SIZE);
g_scanner_scope_add_symbol (scanner, 2, "color", (gpointer) SYM_COLOR);
g_scanner_scope_add_symbol (scanner, 2, "arrowsize", (gpointer) SYM_ARROWSIZE);
g_scanner_scope_add_symbol (scanner, 2, "arrowtype", (gpointer) SYM_ARROWTYPE);
g_scanner_scope_add_symbol (scanner, 2, "minsize", (gpointer) SYM_MINSIZE);
g_scanner_scope_add_symbol (scanner, 2, "maxsize", (gpointer) SYM_MAXSIZE);
g_scanner_scope_add_symbol (scanner, 2, "radius", (gpointer) SYM_RADIUS);
g_scanner_scope_add_symbol (scanner, 2, "maxangle", (gpointer) SYM_MAXANGLE);
g_scanner_scope_add_symbol (scanner, 2, "minlen", (gpointer) SYM_MINLEN);
g_scanner_scope_add_symbol (scanner, 2, "simplify", (gpointer) SYM_SIMPLIFY);
g_scanner_scope_add_symbol (scanner, 2, "snap", (gpointer) SYM_SNAP);
g_scanner_set_scope (scanner, 0);
scanner->config->scope_0_fallback = 0;
g_scanner_input_file (scanner, file);
token = g_scanner_get_next_token (scanner);
while (token != G_TOKEN_EOF)
{
if (token == G_TOKEN_STRING)
{
/*
* New tool definition
*/
name = parse_name (scanner);
if(!name)
goto cleanup;
token = g_scanner_cur_token(scanner);
if (token != G_TOKEN_EQUAL_SIGN)
{
g_scanner_unexp_token (scanner, G_TOKEN_EQUAL_SIGN, NULL,
NULL, NULL, "aborting", TRUE);
goto cleanup;
}
token = g_scanner_get_next_token (scanner);
/* defaults */
type = GROMIT_PEN;
width = 7;
arrowsize = 0;
arrowtype = GROMIT_ARROW_END;
minwidth = 1;
maxwidth = G_MAXUINT;
radius = 10;
minlen = 2 * radius + radius / 2;
maxangle = 15;
simplify = 10;
snapdist = 0;
fg_color = data->red;
if (token == G_TOKEN_SYMBOL)
{
type = (GromitPaintType) scanner->value.v_symbol;
token = g_scanner_get_next_token (scanner);
}
else if (token == G_TOKEN_STRING)
{
copy = parse_name (scanner);
if(!copy)
goto cleanup;
token = g_scanner_cur_token(scanner);
context_template = g_hash_table_lookup (data->tool_config, copy);
if (context_template)
{
type = context_template->type;
width = context_template->width;
arrowsize = context_template->arrowsize;
arrowtype = context_template->arrow_type;
radius = context_template->radius;
simplify = context_template->simplify;
minlen = context_template->minlen;
maxangle = context_template->maxangle;
snapdist = context_template->snapdist;
minwidth = context_template->minwidth;
maxwidth = context_template->maxwidth;
fg_color = context_template->paint_color;
}
else
{
g_printerr ("WARNING: Unable to copy \"%s\": "
"not yet defined!\n", copy);
}
}
else
{
g_printerr ("Expected Tool-definition "
"or name of template tool\n");
goto cleanup;
}
/* Are there any tool-options?
*/
if (token == G_TOKEN_LEFT_PAREN)
{
GdkRGBA *color = NULL;
g_scanner_set_scope (scanner, 2);
scanner->config->int_2_float = 1;
token = g_scanner_get_next_token (scanner);
while (token != G_TOKEN_RIGHT_PAREN)
{
if (token == G_TOKEN_SYMBOL)
{
if ((intptr_t) scanner->value.v_symbol == SYM_SIZE)
{
gfloat v = parse_get_float(scanner, "Missing Size (float)");
if (isnan(v)) goto cleanup;
width = (guint) (v + 0.5);
}
else if ((intptr_t) scanner->value.v_symbol == SYM_COLOR)
{
token = g_scanner_get_next_token (scanner);
if (token != G_TOKEN_EQUAL_SIGN)
{
g_printerr ("Missing \"=\"... aborting\n");
goto cleanup;
}
token = g_scanner_get_next_token (scanner);
if (token != G_TOKEN_STRING)
{
g_printerr ("Missing Color (string)... "
"aborting\n");
goto cleanup;
}
color = g_malloc (sizeof (GdkRGBA));
if (gdk_rgba_parse (color, scanner->value.v_string))
{
fg_color = color;
}
else
{
g_printerr ("Unable to parse color. "
"Keeping default.\n");
g_free (color);
}
color = NULL;
}
else if ((intptr_t) scanner->value.v_symbol == SYM_ARROWSIZE)
{
gfloat v = parse_get_float(scanner, "Missing arrowsize (float)");
if (isnan(v)) goto cleanup;
arrowsize = (guint)(v + 0.5);
arrowtype = GROMIT_ARROW_END;
}
else if ((intptr_t) scanner->value.v_symbol == SYM_ARROWTYPE)
{
token = g_scanner_get_next_token (scanner);
if (token != G_TOKEN_EQUAL_SIGN)
{
g_printerr ("Missing \"=\"... aborting\n");
goto cleanup;
}
token = g_scanner_get_next_token (scanner);
if (token != G_TOKEN_STRING)
{
g_printerr ("Missing Arrowsize (string)... "
"aborting\n");
goto cleanup;
}
if (! strcasecmp(scanner->value.v_string, "end"))
arrowtype = GROMIT_ARROW_END;
else if (! strcasecmp(scanner->value.v_string, "start"))
arrowtype = GROMIT_ARROW_START;
else if (! strcasecmp(scanner->value.v_string, "double"))
arrowtype = GROMIT_ARROW_DOUBLE;
else
{
g_printerr ("Arrow type must be \"start\", \"end\", or \"double\"... "
"aborting\n");
goto cleanup;
}
}
else if ((intptr_t) scanner->value.v_symbol == SYM_MINSIZE)
{
gfloat v = parse_get_float(scanner, "Missing minsize (float)");
if (isnan(v)) goto cleanup;
minwidth = v;
}
else if ((intptr_t) scanner->value.v_symbol == SYM_MAXSIZE)
{
gfloat v = parse_get_float(scanner, "Missing maxsize (float)");
if (isnan(v)) goto cleanup;
maxwidth = v;
}
else if ((intptr_t) scanner->value.v_symbol == SYM_RADIUS)
{
gfloat v = parse_get_float(scanner, "Missing radius (float)");
if (isnan(v)) goto cleanup;
radius = v;
}
else if ((intptr_t) scanner->value.v_symbol == SYM_MAXANGLE)
{
gfloat v = parse_get_float(scanner, "Missing angle (float)");
if (isnan(v)) goto cleanup;
maxangle = v;
}
else if ((intptr_t) scanner->value.v_symbol == SYM_SIMPLIFY)
{
gfloat v = parse_get_float(scanner, "Missing simplify value (float)");
if (isnan(v)) goto cleanup;
simplify = v;
}
else if ((intptr_t) scanner->value.v_symbol == SYM_MINLEN)
{
gfloat v = parse_get_float(scanner, "Missing minlen value (float)");
if (isnan(v)) goto cleanup;
minlen = v;
}
else if ((intptr_t) scanner->value.v_symbol == SYM_SNAP)
{
gfloat v = parse_get_float(scanner, "Missing snap distance (float)");
if (isnan(v)) goto cleanup;
snapdist = v;
}
else
{
g_printerr ("Unknown tool type?????\n");
}
}
else
{
g_printerr ("skipped token!!!\n");
}
token = g_scanner_get_next_token (scanner);
}
g_scanner_set_scope (scanner, 0);
token = g_scanner_get_next_token (scanner);
}
/*
* Finally we expect a semicolon
*/
if (token != ';')
{
g_printerr ("Expected \";\"\n");
goto cleanup;
}
context = paint_context_new (data, type, fg_color, width,
arrowsize, arrowtype,
simplify, radius, maxangle, minlen, snapdist,
minwidth, maxwidth);
g_hash_table_insert (data->tool_config, name, context);
}
else if (token == G_TOKEN_SYMBOL &&
(scanner->value.v_symbol == HOTKEY_SYMBOL_VALUE ||
scanner->value.v_symbol == UNDOKEY_SYMBOL_VALUE))
{
/*
* Hot key definition
*/
gpointer key_type = scanner->value.v_symbol;
token = g_scanner_get_next_token(scanner);
if (token != G_TOKEN_EQUAL_SIGN)
{
g_scanner_unexp_token (scanner, G_TOKEN_EQUAL_SIGN, NULL,
NULL, NULL, "aborting", TRUE);
goto cleanup;
}
token = g_scanner_get_next_token (scanner);
if (token != G_TOKEN_STRING)
{
g_scanner_unexp_token (scanner, G_TOKEN_STRING, NULL,
NULL, NULL, "aborting", TRUE);
goto cleanup;
}
if (key_type == HOTKEY_SYMBOL_VALUE)
{
data->hot_keyval = g_strdup(scanner->value.v_string);
}
else if (key_type == UNDOKEY_SYMBOL_VALUE)
{
data->undo_keyval = g_strdup(scanner->value.v_string);
}
token = g_scanner_get_next_token(scanner);
if (token != ';')
{
g_printerr ("Expected \";\"\n");
goto cleanup;
}
}
else
{
g_printerr ("Expected name of Tool to define or Hot key definition\n");
goto cleanup;
}
token = g_scanner_get_next_token (scanner);
}
status = TRUE;
cleanup:
if (!status) {
/* purge incomplete tool config */
GHashTableIter it;
gpointer value;
g_hash_table_iter_init (&it, data->tool_config);
while (g_hash_table_iter_next (&it, NULL, &value))
paint_context_free(value);
g_hash_table_remove_all(data->tool_config);
/* alert user */
GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(data->win),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_CLOSE,
_("Failed parsing config file %s, falling back to default tools."),
filename);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
g_scanner_destroy (scanner);
close (file);
g_free (filename);
return status;
}
int parse_args (int argc, char **argv, GromitData *data)
{
gint i;
gchar *arg;
gboolean wrong_arg = FALSE;
gboolean activate = FALSE;
for (i=1; i < argc ; i++)
{
arg = argv[i];
if (strcmp (arg, "-a") == 0 ||
strcmp (arg, "--active") == 0)
{
activate = TRUE;
}
else if (strcmp (arg, "-d") == 0 ||
strcmp (arg, "--debug") == 0)
{
data->debug = 1;
}
else if (strcmp (arg, "-k") == 0 ||
strcmp (arg, "--key") == 0)
{
if (i+1 < argc)
{
data->hot_keyval = argv[i+1];
data->hot_keycode = 0;
i++;
}
else
{
g_printerr ("-k requires an Key-Name as argument\n");
wrong_arg = TRUE;
}
}
else if (strcmp (arg, "-K") == 0 ||
strcmp (arg, "--keycode") == 0)
{
if (i+1 < argc && atoi (argv[i+1]) > 0)
{
data->hot_keyval = NULL;
data->hot_keycode = atoi (argv[i+1]);
i++;
}
else
{
g_printerr ("-K requires an keycode > 0 as argument\n");
wrong_arg = TRUE;
}
}
else if (strcmp (arg, "-o") == 0 ||
strcmp (arg, "--opacity") == 0)
{
if (i+1 < argc && strtod (argv[i+1], NULL) >= 0.0 && strtod (argv[i+1], NULL) <= 1.0)
{
data->opacity = strtod (argv[i+1], NULL);
g_printerr ("Opacity set to: %.2f\n", data->opacity);
gtk_widget_set_opacity(data->win, data->opacity);
i++;
}
else
{
g_printerr ("-o requires an opacity >=0 and <=1 as argument\n");
wrong_arg = TRUE;
}
}
else if (strcmp (arg, "-u") == 0 ||
strcmp (arg, "--undo-key") == 0)
{
if (i+1 < argc)
{
data->undo_keyval = argv[i+1];
data->undo_keycode = 0;
i++;
}
else
{
g_printerr ("-u requires an Key-Name as argument\n");
wrong_arg = TRUE;
}
}
else if (strcmp (arg, "-U") == 0 ||
strcmp (arg, "--undo-keycode") == 0)
{
if (i+1 < argc && atoi (argv[i+1]) > 0)
{
data->undo_keyval = NULL;
data->undo_keycode = atoi (argv[i+1]);
i++;
}
else
{
g_printerr ("-U requires an keycode > 0 as argument\n");
wrong_arg = TRUE;
}
}
else if (strcmp (arg, "-V") == 0 ||
strcmp (arg, "--version") == 0)
{
g_print("Gromit-MPX " PACKAGE_VERSION "\n");
exit(0);
}
else
{
g_printerr ("Unknown Option for Gromit-MPX startup: \"%s\"\n", arg);
wrong_arg = TRUE;
}
if (wrong_arg)
{
g_printerr ("Please see the Gromit-MPX manpage for the correct usage\n");
exit (1);
}
}
return activate;
}
void read_keyfile(GromitData *data)
{
gchar *filename = g_strjoin (G_DIR_SEPARATOR_S,
g_get_user_config_dir(), "gromit-mpx.ini", NULL);
/*
set defaults
*/
data->show_intro_on_startup = KEY_DFLT_SHOW_INTRO_ON_STARTUP;
/*
read actual settings
*/
GError *error = NULL;
GKeyFile *key_file = g_key_file_new ();
if (!g_key_file_load_from_file (key_file, filename, KEYFILE_FLAGS, &error)) {
// treat file-not-found not as an error
if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
g_warning ("Error loading key file: %s", error->message);
g_error_free(error);
goto cleanup;
}
data->show_intro_on_startup = g_key_file_get_boolean (key_file, "General", "ShowIntroOnStartup", &error);
data->opacity = g_key_file_get_double (key_file, "Drawing", "Opacity", &error);
// 0.0 on not-found, but anyway, also don't use 0.0 when user-set
if(data->opacity == 0)
data->opacity = DEFAULT_OPACITY;
cleanup:
g_free(filename);
g_key_file_free(key_file);
}
void write_keyfile(GromitData *data)
{
gchar *filename = g_strjoin (G_DIR_SEPARATOR_S,
g_get_user_config_dir(), "gromit-mpx.ini", NULL);
GError *error = NULL;
GKeyFile *key_file = g_key_file_new ();
g_key_file_set_boolean (key_file, "General", "ShowIntroOnStartup", data->show_intro_on_startup);
g_key_file_set_double (key_file, "Drawing", "Opacity", data->opacity);
// if file exists but is read-only, bail out
if (access(filename, F_OK) == 0 && access(filename, W_OK) != 0) {
g_warning ("Not overwriting read-only key file");
goto cleanup;
}
// Save as a file.
if (!g_key_file_save_to_file (key_file, filename, &error)) {
g_warning ("Error saving key file: %s", error->message);
g_error_free(error);
goto cleanup;
}
cleanup:
g_free(filename);
g_key_file_free(key_file);
}