-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresource.c
1019 lines (894 loc) · 23.9 KB
/
resource.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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- tab-width: 4; c-basic-offset: 4; -*- */
/*
* resource.c
*
* This module deals with the xxkb configuration.
*
* Copyright (c) 1999-2003, by Ivan Pascal <pascal@tsu.ru>
*
* 2003-07-27:
* The X Resources subsystem is used to store the configuration.
* Settings from $XHOME/app-defaults/XXkb may be overwritten by
* the user in $HOME/.xxkbrc file.
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <err.h>
#include <X11/Xlib.h>
#include <X11/Xresource.h>
#include <X11/Xutil.h>
#include <X11/XKBlib.h>
#include "xxkb.h"
#ifdef XT_RESOURCE_SEARCH
#include <X11/IntrinsicP.h>
#endif
#ifdef SVG_GRAPHICS
#include <gdk-pixbuf-xlib/gdk-pixbuf-xlib.h>
#include <librsvg/rsvg.h>
#endif
#include <X11/xpm.h>
#define USERDEFFILE ".xxkbrc"
/* Forward function declarations */
static SearchList* MakeSearchList(char *string);
static char* GetAppListName(char *match, char *action);
static void FreeSearchList(SearchList *list);
static void CreateLabel(Display *dpy, XXkbElement *elem, Pixmap *pixmap, Pixmap *mask, char *label, char *font, unsigned int foreground, unsigned int background);
static void LoadImage(Display *dpy, XXkbElement *elem, Pixmap *pixmap, Pixmap *mask, char *filename);
#ifdef SHAPE_EXT
static void CreateShade(Display *dpy, XXkbElement *elem, Pixmap *shape, Pixmap *shade);
#endif
static char *processTextName[] = {"run.text.1", "run.text.2", "run.text.3", "run.text.4"};
#define countof(a) (sizeof(a) / sizeof(a[0]))
/* Deprecated. Keep temporary for compatibility. */
static
struct {
char *name;
} IgnoreTable[] = {
{ "ignore.wm_class.class" },
{ "ignore.wm_class.name" },
{ "ignore.wm_name" }
};
/* Deprecated. Maintained only for compatibility reasons. */
static
struct {
char *name;
int res;
} GravityTable[] = {
{ "NorthEast", NorthEastGravity },
{ "NorthWest", NorthWestGravity },
{ "North", NorthGravity },
{ "SouthEast", SouthEastGravity },
{ "SouthWest", SouthWestGravity },
{ "South", SouthGravity },
{ "East", EastGravity },
{ "West", WestGravity },
{ "Center", CenterGravity }
};
/* Keep in sync with MatchType and IgnoreTable. */
static
struct {
char *name;
MatchType type;
} MatchTable[] = {
{ "wm_class_class", WMClassClass },
{ "wm_class_name", WMClassName },
{ "wm_name", WMName },
{ "property", Prop }
};
static
struct {
char *name;
ListAction action;
int group;
} ActionTable[] = {
{ "start_alt", InitAltGrp, 0 },
{ "alt_group1", AltGrp, 0 },
{ "alt_group2", AltGrp, 1 },
{ "alt_group3", AltGrp, 2 },
{ "alt_group4", AltGrp, 3 },
{ "ignore", Ignore, 0 }
};
static
struct {
char *name;
int flag;
} ControlsTable[] = {
{ "add_when_start", When_start },
{ "add_when_create", When_create },
{ "add_when_change", When_change },
{ "focusout", Focus_out },
{ "two_state", Two_state },
{ "button_delete", Button_delete },
{ "button_delete_and_forget", Forget_window },
{ "mainwindow_delete", Main_delete }
};
static
struct {
char *name;
int flag;
} KeyMasks[] = {
{ "none", 0 },
{ "shift", ShiftMask },
{ "lock", LockMask },
{ "control", ControlMask },
{ "ctrl", ControlMask },
{ "alt", Mod1Mask },
{ "mod1", Mod1Mask },
{ "mod2", Mod2Mask },
{ "mod3", Mod3Mask },
{ "mod4", Mod4Mask },
{ "mod5", Mod5Mask },
};
/*
* GetRes
* Reads the program resource specified by the name. Some resources
* may be not required.
*
* Returns
* 0 if a resource could be read and non-null otherwise.
* Exits the process if the required resource was not found.
*/
static int
GetRes(XrmDatabase db, char *name, ResType type, Bool required, void *value)
{
XrmValue val;
Bool ok = False;
char *type_ret, *full_res_name;
full_res_name = PrependProgramName(name);
ok = XrmGetResource(db, full_res_name, "", &type_ret, &val);
if (!ok) {
if (required) {
warnx("Unable to get a default value for the required resource `%s'", full_res_name);
free(full_res_name);
/* Be sure to exit if the required resource could not be read. */
exit(2);
}
else {
free(full_res_name);
return 1;
}
}
free(full_res_name);
switch (type) {
case T_string:
*((char**) value) = malloc(strlen(val.addr) + 1);
if (*((char**) value) == NULL) {
err(1, "Failed to allocate memory for the string");
}
strcpy(*((char**) value), val.addr);
break;
case T_bool:
*((Bool*) value) =
(strncasecmp(val.addr, "true", 4) == 0 ||
strncasecmp(val.addr, "yes", 3) == 0 ||
strncasecmp(val.addr, "on", 2) == 0);
break;
case T_int:
*((int*) value) = strtol(val.addr, (char**) NULL, 10);
break;
case T_ulong:
*((unsigned long*) value) = strtoul(val.addr, (char**) NULL, 16);
break;
}
return 0;
}
static void
SetRes(XrmDatabase db, char *name, ResType type, void *val)
{
char *full_res_name;
full_res_name = PrependProgramName(name);
switch (type) {
case T_string:
XrmPutStringResource(&db, full_res_name, (char*) val);
break;
default:
warnx("Setting a resource of an unsupported type");
break;
}
free(full_res_name);
}
static void
GetColorRes(Display *dpy, XrmDatabase db, char *name, unsigned int *color)
{
XColor scr_def, exact_def;
Status stat;
char *color_name;
/* First, try if the color is specified by the name */
GetRes(db, name, T_string, True, &color_name);
stat = XAllocNamedColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
color_name, &scr_def, &exact_def);
if (stat != 0) {
/* success */
*color = scr_def.pixel;
return;
}
/* No luck, so it must be hex */
GetRes(db, name, T_ulong, True, color);
}
static void
GetControlRes(XrmDatabase db, char *name, unsigned long *controls, unsigned long flag)
{
Bool set;
GetRes(db, name, T_bool, True, &set);
if (set)
*controls |= flag;
else
*controls &= ~flag;
}
static void
GetElementRes(Display *dpy, XrmDatabase db, char *window_name, XXkbElement *element)
{
int i;
Bool labels_enabled;
char res_name[64], *str_geom, *str_gravity;
Pixmap *pixmap = element->pictures;
Pixmap *shape = element->shapemask;
#ifdef SHAPE_EXT
Pixmap *bound = element->boundmask;
#endif
Geometry *geom = &element->geometry;
sprintf(res_name, "%s.geometry", window_name);
GetRes(db, res_name, T_string, True, &str_geom);
geom->mask = XParseGeometry(str_geom, &geom->x, &geom->y, &geom->width, &geom->height);
if (~geom->mask & AllValues) {
warnx("Incomplete geometry for %s", window_name);
}
str_gravity = NULL;
sprintf(res_name, "%s.gravity", window_name);
GetRes(db, res_name, T_string, False, &str_gravity);
if (str_gravity == NULL) {
/* Get the gravity from the geometry */
XSizeHints *size_hts = XAllocSizeHints();
Geometry tmp_geom;
if (size_hts == NULL) {
warnx("Unable to allocate size hints");
free(str_geom);
return;
}
XWMGeometry(dpy, DefaultScreen(dpy), str_geom, NULL, 0, size_hts,
&tmp_geom.x, &tmp_geom.y,
(int *)&tmp_geom.width, (int *)&tmp_geom.height,
&geom->gravity);
XFree(size_hts);
}
else {
/* Legacy code */
for (i = 0; i < countof(GravityTable); i++) {
if (!strncmp(str_gravity, GravityTable[i].name, strlen(GravityTable[i].name))) {
geom->gravity = GravityTable[i].res;
break;
}
}
free(str_gravity);
}
free(str_geom);
/* images or labels? */
sprintf(res_name, "%s.label.enable", window_name);
GetRes(db, res_name, T_bool, True, &labels_enabled);
if (labels_enabled) {
/* labels */
unsigned int background, foreground;
char *font, *label;
sprintf(res_name, "%s.label.font", window_name);
GetRes(db, res_name, T_string, True, &font);
sprintf(res_name, "%s.label.background", window_name);
GetColorRes(dpy, db, res_name, &background);
sprintf(res_name, "%s.label.foreground", window_name);
GetColorRes(dpy, db, res_name, &foreground);
for (i = 0; i < MAX_GROUP; i++) {
sprintf(res_name, "%s.label.text.%d", window_name, i + 1);
label = NULL;
GetRes(db, res_name, T_string, False, &label);
if (label == NULL) {
XkbDescPtr desc;
desc = XkbAllocKeyboard();
if (desc == NULL) {
warnx("Unable to allocate a keyboard description");
continue;
}
XkbGetNames(dpy, XkbGroupNamesMask, desc);
if (desc->names == NULL || desc->names->groups[i] == 0) {
/*warnx("Unable to get keyboard names");*/
continue;
}
label = XGetAtomName(dpy, desc->names->groups[i]);
if (label == NULL) {
warnx("Unable to get a name of the group %d", i);
continue;
}
}
CreateLabel(dpy, element, &pixmap[i], &shape[i], label, font, foreground, background);
#ifdef SHAPE_EXT
CreateShade(dpy, element, &shape[i], &bound[i]);
#endif
free(label);
}
free(font);
}
else {
/* images */
char res_name[64], *filename, *fullname, *imgpath;
size_t len;
/* backward compatibility */
if (GetRes(db, "xpm.path", T_string, False, &imgpath) != 0)
GetRes(db, "image.path", T_string, True, &imgpath);
for (i = 0; i < MAX_GROUP; i++) {
/* backward compatibility */
sprintf(res_name, "%s.xpm.%d", window_name, i + 1);
if (GetRes(db, res_name, T_string, False, &filename) != 0) {
sprintf(res_name, "%s.image.%d", window_name, i + 1);
GetRes(db, res_name, T_string, True, &filename);
}
if (filename != NULL && *filename != '\0') {
if (*filename == '/') {
LoadImage(dpy, element, &pixmap[i], &shape[i], filename);
#ifdef SHAPE_EXT
CreateShade(dpy, element, &shape[i], &bound[i]);
#endif
}
else {
len = strlen(imgpath) + 1 + strlen(filename);
fullname = malloc(len + 1);
if (fullname == NULL) {
warn("Failed to allocate memory for full filename");
free(filename);
pixmap[i] = None;
shape[i] = None;
#ifdef SHAPE_EXT
bound[i] = None;
#endif
continue;
}
sprintf(fullname, "%s/%s", imgpath, filename);
LoadImage(dpy, element, &pixmap[i], &shape[i], fullname);
#ifdef SHAPE_EXT
CreateShade(dpy, element, &shape[i], &bound[i]);
#endif
free(fullname);
}
free(filename);
}
else {
pixmap[i] = None;
shape[i] = None;
#ifdef SHAPE_EXT
bound[i] = None;
#endif
}
}
free(imgpath);
}
}
static void
GetKeyMasksRes(XrmDatabase db, char *name, unsigned long *keymask)
{
int i;
char *keymask_name;
GetRes(db, name, T_string, True, &keymask_name);
if (keymask_name == NULL)
return;
*keymask = 0;
for (i = 0; i < countof(KeyMasks); i++) {
if (!strncmp(keymask_name, KeyMasks[i].name, strlen(KeyMasks[i].name))) {
*keymask |= KeyMasks[i].flag;
break;
}
}
free(keymask_name);
}
static void
CreateLabel(Display *dpy, XXkbElement *elem, Pixmap *pixmap, Pixmap *mask, char *label, char *font, unsigned int foreground, unsigned int background)
{
GC gc;
Pixmap pixId;
XGCValues values;
unsigned long valuemask;
XFontStruct* font_struct = NULL;
int w = 3, h = 2, depth, scr;
*pixmap = None;
*mask = None;
scr = DefaultScreen(dpy);
depth = DefaultDepth(dpy, scr);
pixId = XCreatePixmap(dpy, RootWindow(dpy, scr),
elem->geometry.width, elem->geometry.height, depth);
/* Failed to create pixmap */
if (pixId == None) {
return;
}
valuemask = 0;
memset(&values, 0, sizeof(XGCValues));
gc = XCreateGC(dpy, pixId, valuemask, &values);
/* Clear the box. Fill it with a background color */
XSetForeground(dpy, gc, background);
XFillRectangle(dpy, pixId, gc, 0, 0, elem->geometry.width, elem->geometry.height);
XSetForeground(dpy, gc, foreground);
XSetBackground(dpy, gc, background);
/* Load and set the font */
font_struct = XLoadQueryFont(dpy, font);
if (font_struct != NULL) {
XSetFont(dpy, gc, font_struct->fid);
h = (elem->geometry.width -
(font_struct->max_bounds.rbearing
- font_struct->min_bounds.lbearing) * strlen(label)) / 2;
w = (elem->geometry.height -
(font_struct->ascent + font_struct->descent)) / 2
+ font_struct->descent;
XFreeFont(dpy, font_struct);
}
XDrawString(dpy, pixId, gc,
h + 1, elem->geometry.height - w,
label, strlen(label));
XFreeGC(dpy, gc);
*pixmap = pixId;
}
static void
LoadImage(Display *dpy, XXkbElement *elem, Pixmap *pixmap, Pixmap *mask, char *filename)
{
#ifdef SVG_GRAPHICS
GdkPixbuf *pixbuf;
GError *error = NULL;
*pixmap = None;
*mask = None;
pixbuf = rsvg_pixbuf_from_file_at_size(filename, elem->geometry.width, elem->geometry.height, &error);
if (pixbuf == NULL) {
if (error != NULL) {
warnx("SVG file `%s': %s", filename, error->message);
}
else {
warnx("SVG file `%s' is invalid", filename);
}
return;
}
gdk_pixbuf_xlib_render_pixmap_and_mask(pixbuf, pixmap, mask, 127);
g_object_unref(G_OBJECT(pixbuf));
#else
int res;
Pixmap picture, shape;
*pixmap = None;
*mask = None;
res = XpmReadFileToPixmap(dpy, RootWindow(dpy, DefaultScreen(dpy)), filename, &picture, &shape, NULL);
switch (res) {
case XpmOpenFailed:
warnx("Unable to open xpm file `%s'", filename);
break;
case XpmFileInvalid:
warnx("Xpm file `%s' is invalid", filename);
break;
case XpmNoMemory:
warnx("No memory for open xpm file `%s'", filename);
break;
default:
*pixmap = picture;
*mask = shape;
break;
}
#endif
}
#ifdef SHAPE_EXT
static void
CreateShade(Display *dpy, XXkbElement *elem, Pixmap *shape, Pixmap *shade)
{
GC gc;
Pixmap shadow;
XGCValues values;
unsigned long valuemask;
int scr;
*shade = None;
scr = DefaultScreen(dpy);
shadow = XCreatePixmap(dpy, RootWindow(dpy, scr),
elem->geometry.width + 2*elem->border_width,
elem->geometry.height + 2*elem->border_width,
1);
/* Failed to create pixmap */
if (shadow == None) {
return;
}
valuemask = 0;
memset(&values, 0, sizeof(XGCValues));
gc = XCreateGC(dpy, shadow, valuemask, &values);
/* Clear the box. Fill it with a background color */
XSetForeground(dpy, gc, 1);
XFillRectangle(dpy, shadow, gc,
0, 0,
elem->geometry.width + 2*elem->border_width,
elem->geometry.height + 2*elem->border_width);
valuemask = GCFunction;
memset(&values, 0, sizeof(XGCValues));
values.function = GXorReverse;
XChangeGC(dpy, gc, valuemask, &values);
if (shape != NULL && *shape != None) {
XCopyArea(dpy, *shape, shadow, gc,
0, 0,
elem->geometry.width,
elem->geometry.height,
elem->border_width,
elem->border_width);
}
XFreeGC(dpy, gc);
*shade = shadow;
}
#endif
/*
* GetConfig
* Main routine of this module. Fills in the config object.
*
* Returns
* 0 on success.
*/
int
GetConfig(Display *dpy, XXkbConfig *conf)
{
XrmDatabase db;
SearchList *list;
Status stat;
char *homedir, *filename;
char *str_list, *res_app_list, res_ctrls[256];
int i, j;
size_t len;
/*
* Init pixbuf engine
*/
#ifdef SVG_GRAPHICS
g_type_init();
gdk_pixbuf_xlib_init(dpy, DefaultScreen(dpy));
#endif
homedir = getenv("HOME");
XrmInitialize();
/*
* read global settings
*/
#ifdef XT_RESOURCE_SEARCH
filename = XtResolvePathname(dpy, "app-defaults", NULL, NULL, NULL, NULL, 0, NULL);
#else
len = strlen(APPDEFDIR) + 1 + strlen(APPDEFFILE);
filename = malloc(len + 1);
if (filename == NULL) {
warn(NULL);
return 1;
}
sprintf(filename, "%s/%s", APPDEFDIR, APPDEFFILE);
#endif
if (filename)
db = XrmGetFileDatabase(filename);
else
db = NULL;
if (db == NULL) {
/*
* this situation is not fatal if the user has all
* configuration in his $HOME/.xxkbrc file.
*/
warnx("Unable to open default resource file `%s'", filename);
}
#ifdef XT_RESOURCE_SEARCH
XtFree(filename);
#else
free(filename);
#endif
/*
* read user-specific settings
*/
#ifdef XT_RESOURCE_SEARCH
filename = XtResolvePathname(dpy, homedir, USERDEFFILE, NULL, "%T/%L/%N%C:%T/%l/%N%C:%T/%N%C:%T/%L/%N:%T/%l/%N:%T/%N", NULL, 0, NULL);
#else
len = strlen(homedir) + 1 + strlen(USERDEFFILE);
filename = malloc(len + 1);
if (filename == NULL) {
warn(NULL);
XrmDestroyDatabase(db);
return 1;
}
sprintf(filename, "%s/%s", homedir, USERDEFFILE);
#endif
/*
* merge settings
*/
if (filename)
stat = XrmCombineFileDatabase(filename, &db, True);
else
stat = 0;
if (stat == 0 && db == NULL) {
/* failed */
warnx("Unable to find configuration data");
return 5;
}
if (!filename) {
/*
* In case the user did not have a config file we have to come
* up with one
*/
len = strlen(homedir) + 1 + strlen(USERDEFFILE);
filename = malloc(len + 1);
if (filename == NULL) {
warn(NULL);
XrmDestroyDatabase(db);
return 1;
}
sprintf(filename, "%s/%s", homedir, USERDEFFILE);
}
/*
* start with the conf object
*/
conf->user_config = filename;
for (i = 0; i < countof(ControlsTable); i++) {
sprintf(res_ctrls, "controls.%s", ControlsTable[i].name);
GetControlRes(db, res_ctrls, &conf->controls, ControlsTable[i].flag);
}
GetRes(db, "group.base", T_int, True, &conf->Base_group);
GetRes(db, "group.alt", T_int, True, &conf->Alt_group);
conf->Base_group--;
conf->Alt_group--;
GetControlRes(db, "bell.enable", &conf->controls, Bell_enable);
if (conf->controls & Bell_enable) {
GetRes(db, "bell.percent", T_int, True, &conf->Bell_percent);
}
GetControlRes(db, "mainwindow.enable", &conf->controls, Main_enable);
if (conf->controls & Main_enable) {
Bool app_icon;
char *type;
/* backward compatibility */
if (GetRes(db, "mainwindow.appicon", T_bool, False,
&app_icon) == 0 && app_icon == True)
conf->controls |= WMaker;
else {
GetRes(db, "mainwindow.type", T_string, True, &type);
if (strncasecmp(type, "wmaker", 6) == 0) {
conf->controls |= WMaker;
}
else if (strncasecmp(type, "tray", 4) == 0) {
conf->controls |= Main_tray;
}
else if (strncasecmp(type, "top", 3) == 0) {
conf->controls |= Main_ontop;
}
else if (strncasecmp(type, "normal", 6) == 0) {
conf->controls &= ~(Main_tray | Main_ontop | WMaker);
}
else {
errx(2, "Unknown window type '%s'", type);
}
}
if (!(conf->controls & Main_tray)) {
GetRes(db, "mainwindow.border.width", T_int, True, &conf->mainwindow.border_width);
GetColorRes(dpy, db, "mainwindow.border.color", &conf->mainwindow.border_color);
}
GetElementRes(dpy, db, "mainwindow", &conf->mainwindow);
}
GetControlRes(db, "button.enable", &conf->controls, Button_enable);
if (conf->controls & Button_enable) {
GetRes(db, "button.border.width", T_int, True, &conf->button.border_width);
GetColorRes(dpy, db, "button.border.color", &conf->button.border_color);
GetElementRes(dpy, db, "button", &conf->button);
}
for (i = 0; i < countof(MatchTable); i++) {
for (j = 0; j < countof(ActionTable); j++) {
res_app_list = GetAppListName(MatchTable[i].name, ActionTable[j].name);
if (res_app_list == NULL)
continue;
str_list = NULL;
GetRes(db, res_app_list, T_string, False, &str_list);
free(res_app_list);
if (str_list == NULL)
continue;
list = MakeSearchList(str_list);
free(str_list);
if (list == NULL)
continue;
list->action = ActionTable[j].action
| (ActionTable[j].group & GrpMask);
list->type = MatchTable[i].type;
list->next = conf->app_lists[i];
conf->app_lists[i] = list;
}
}
/* XXX: keep temporary for compatibility */
for (i = 0; i < countof(IgnoreTable); i++) {
str_list = NULL;
GetRes(db, IgnoreTable[i].name, T_string, False, &str_list);
if (str_list == NULL)
continue;
list = MakeSearchList(str_list);
free(str_list);
if (list == NULL)
continue;
list->action = Ignore;
list->type = MatchTable[i].type;
list->next = conf->app_lists[i];
conf->app_lists[i] = list;
}
GetControlRes(db, "ignore.reverse", &conf->controls, Ignore_reverse);
GetControlRes(db, "mousebutton.1.reverse", &conf->controls, But1_reverse);
GetControlRes(db, "mousebutton.3.reverse", &conf->controls, But3_reverse);
GetKeyMasksRes(db, "keymask.cycle", &conf->keymask_cycle);
GetRes(db, "run.process", T_string, False, &conf->run_process);
conf->run_enabled = 0;
for (i = 0; i < 4; i++) {
GetRes(db, processTextName[i], T_string, False, &conf->run_text[i]);
if (conf->run_text[i] && strlen(conf->run_text[i]) > 0) {
conf->run_enabled = 1;
}
}
if (conf->run_process && strlen(conf->run_process) == 0) {
conf->run_enabled = 0;
}
XrmDestroyDatabase(db);
return 0;
}
void
AddAppToIgnoreList(XXkbConfig *conf, char *app_ident, MatchType ident_type)
{
XrmDatabase db;
SearchList *cur, *prev, *list;
char *res_name, *new_list, *orig_list;
size_t len;
/* read the current list once again before updating it */
XrmInitialize();
db = XrmGetFileDatabase(conf->user_config);
if (db == NULL) {
warnx("Unable to open resource file `%s'", conf->user_config);
return;
}
res_name = GetAppListName(MatchTable[ident_type].name, "ignore");
if (res_name == NULL) {
return;
}
len = strlen(app_ident);
orig_list = NULL;
GetRes(db, res_name, T_string, False, &orig_list);
if (orig_list != NULL) {
len += strlen(orig_list);
len += 1; /* 1 for the space-separator */
}
/* create a new list */
new_list = malloc(len + 1);
if (new_list == NULL) {
warn("Failed to allocate memory for ignore list");
free(orig_list);
free(res_name);
XrmDestroyDatabase(db);
return;
}
/* fill in the new list */
*new_list = '\0';
if (orig_list != NULL) {
strcat(new_list, orig_list);
strcat(new_list, " ");
}
strcat(new_list, app_ident);
/* parse the new list */
list = MakeSearchList(new_list);
if (list == NULL) {
free(orig_list);
free(res_name);
free(new_list);
XrmDestroyDatabase(db);
return;
}
list->action = Ignore;
list->type = ident_type;
list->next = conf->app_lists[ident_type];
conf->app_lists[ident_type] = list;
for (prev = list, cur = list->next;
cur != NULL && cur->action != Ignore;
prev = cur, cur = cur->next) /* empty body */;
if (cur != NULL) {
prev->next = cur->next;
FreeSearchList(cur);
}
/* we have an updated list now,
* let's update the resource database
*/
SetRes(db, res_name, T_string, new_list);
free(new_list);
free(res_name);
free(orig_list);
/* save the database */
XrmPutFileDatabase(db, conf->user_config);
XrmDestroyDatabase(db);
}
/*
* MakeSearchList
* Converts a space- (and tab-) separated list into a list
* of NUL-separated chunks.
*
* Returns
* A pointer to the list on success, and
* a NULL pointer on failure.
*/
#define IS_SEPARATOR(a) (((a) == ' ') || ((a) == '\t'))
#define IS_NOT_SEPARATOR(a) (!IS_SEPARATOR(a))
static SearchList*
MakeSearchList(char *str)
{
size_t len;
int count;
char *i, *j;
SearchList *ret;
/* allocate the memory for the list */
ret = malloc(sizeof(SearchList));
if (ret == NULL) {
warn("Failed to allocate memory for search list");
return NULL;
}
/* initialize the list structure */
memset(ret, 0, sizeof(SearchList));
len = strlen(str);
if (len == 0) {
return ret;
}
ret->list = malloc(len + 1);
if (ret->list == NULL) {
warn("Failed to allocate memory for search list");
free(ret);
return NULL;
}
/* tokenize the string */
i = str; j = ret->list; count = 0;
while (len) {
count++;
while (IS_NOT_SEPARATOR(*i)) {
*j++ = *i++;
if (!(--len)) {
*j = '\0';
break;
}
}
*j++ = '\0';
while (IS_SEPARATOR(*i)) {
i++;
if (!(--len))
break;
}
}
ret->num = count;
/* allocate the memory for the index list */
ret->idx = malloc(count * sizeof(char*));
if (ret->idx == NULL) {
warn("Failed to allocate memory");
free(ret->list);
free(ret);
return NULL;
}
/* store the chunk pointers */
for (count = 0, i = ret->list; count < ret->num; count++) {
ret->idx[count] = i;
while (*i++) /* empty body */;
}
return ret;
}
/*
* FreeSearchList
* cleans the search list structure.
*/
static void
FreeSearchList(SearchList *list)
{
if (list == NULL)
return;
free(list->list);
free(list->idx);
free(list);
}
/*
* GetAppListName
* Returns
* a resource name for a app_list resource.
*
* Note
* Caller must free the returned pointer.