wmaker: Added 'const' attribute to function 'wDefaultsInitDomain'
[wmaker-crm.git] / util / setstyle.c
blob7f99b6ff7893c3cb009af2d3355bf9ed95922e19
1 /* setstyle.c - loads style related options to wmaker
3 * WindowMaker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
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.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #ifdef __GLIBC__
23 #define _GNU_SOURCE /* getopt_long */
24 #endif
26 #include <sys/stat.h>
28 #include <getopt.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <unistd.h>
35 #include <X11/Xlib.h>
37 #include <WINGs/WUtil.h>
39 #include "../src/wconfig.h"
41 #include "common.h"
43 #define MAX_OPTIONS 128
45 char *FontOptions[] = {
46 "IconTitleFont",
47 "ClipTitleFont",
48 "DisplayFont",
49 "LargeDisplayFont",
50 "MenuTextFont",
51 "MenuTitleFont",
52 "WindowTitleFont",
53 NULL
56 char *CursorOptions[] = {
57 "NormalCursor",
58 "ArrowCursor",
59 "MoveCursor",
60 "ResizeCursor",
61 "TopLeftResizeCursor",
62 "TopRightResizeCursor",
63 "BottomLeftResizeCursor",
64 "BottomRightResizeCursor",
65 "VerticalResizeCursor",
66 "HorizontalResizeCursor",
67 "WaitCursor",
68 "QuestionCursor",
69 "TextCursor",
70 "SelectCursor",
71 NULL
74 extern char *__progname;
75 int ignoreFonts = 0;
76 int ignoreCursors = 0;
78 Display *dpy;
81 static Bool isCursorOption(const char *option)
83 int i;
85 for (i = 0; CursorOptions[i] != NULL; i++) {
86 if (strcasecmp(option, CursorOptions[i]) == 0) {
87 return True;
91 return False;
94 static Bool isFontOption(const char *option)
96 int i;
98 for (i = 0; FontOptions[i] != NULL; i++) {
99 if (strcasecmp(option, FontOptions[i]) == 0) {
100 return True;
104 return False;
108 * finds elements in `texture' that reference external files,
109 * prepends `prefix' to these files. `prefix' is a path component
110 * that qualifies the external references to be absolute, possibly
111 * pending further expansion
113 static void hackPathInTexture(WMPropList * texture, const char *prefix)
115 WMPropList *type;
116 char *t;
118 /* get texture type */
119 type = WMGetFromPLArray(texture, 0);
120 t = WMGetFromPLString(type);
121 if (t == NULL)
122 return;
124 if (strcasecmp(t, "tpixmap") == 0 ||
125 strcasecmp(t, "spixmap") == 0 ||
126 strcasecmp(t, "mpixmap") == 0 ||
127 strcasecmp(t, "cpixmap") == 0 ||
128 strcasecmp(t, "tvgradient") == 0 ||
129 strcasecmp(t, "thgradient") == 0 ||
130 strcasecmp(t, "tdgradient") == 0) {
131 WMPropList *file;
132 char buffer[4018];
134 /* get pixmap file path */
135 file = WMGetFromPLArray(texture, 1);
136 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
137 /* replace path with full path */
138 WMDeleteFromPLArray(texture, 1);
139 WMInsertInPLArray(texture, 1, WMCreatePLString(buffer));
141 } else if (strcasecmp(t, "bitmap") == 0) {
142 WMPropList *file;
143 char buffer[4018];
145 /* get bitmap file path */
146 file = WMGetFromPLArray(texture, 1);
147 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
148 /* replace path with full path */
149 WMDeleteFromPLArray(texture, 1);
150 WMInsertInPLArray(texture, 1, WMCreatePLString(buffer));
152 /* get mask file path */
153 file = WMGetFromPLArray(texture, 2);
154 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
155 /* replace path with full path */
156 WMDeleteFromPLArray(texture, 2);
157 WMInsertInPLArray(texture, 2, WMCreatePLString(buffer));
161 static void hackPaths(WMPropList * style, const char *prefix)
163 WMPropList *keys;
164 WMPropList *key;
165 WMPropList *value;
166 int i;
168 keys = WMGetPLDictionaryKeys(style);
170 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
171 key = WMGetFromPLArray(keys, i);
173 value = WMGetFromPLDictionary(style, key);
174 if (!value)
175 continue;
177 if (strcasecmp(WMGetFromPLString(key), "WorkspaceSpecificBack") == 0) {
178 if (WMIsPLArray(value)) {
179 int j;
180 WMPropList *texture;
182 for (j = 0; j < WMGetPropListItemCount(value); j++) {
183 texture = WMGetFromPLArray(value, j);
185 if (texture && WMIsPLArray(texture)
186 && WMGetPropListItemCount(texture) > 2) {
188 hackPathInTexture(texture, prefix);
192 } else {
194 if (WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
196 hackPathInTexture(value, prefix);
203 static WMPropList *getColor(WMPropList * texture)
205 WMPropList *value, *type;
206 char *str;
208 type = WMGetFromPLArray(texture, 0);
209 if (!type)
210 return NULL;
212 value = NULL;
214 str = WMGetFromPLString(type);
215 if (strcasecmp(str, "solid") == 0) {
216 value = WMGetFromPLArray(texture, 1);
217 } else if (strcasecmp(str, "dgradient") == 0
218 || strcasecmp(str, "hgradient") == 0 || strcasecmp(str, "vgradient") == 0) {
219 WMPropList *c1, *c2;
220 int r1, g1, b1, r2, g2, b2;
221 char buffer[32];
223 c1 = WMGetFromPLArray(texture, 1);
224 c2 = WMGetFromPLArray(texture, 2);
225 if (!dpy) {
226 if (sscanf(WMGetFromPLString(c1), "#%2x%2x%2x", &r1, &g1, &b1) == 3
227 && sscanf(WMGetFromPLString(c2), "#%2x%2x%2x", &r2, &g2, &b2) == 3) {
228 sprintf(buffer, "#%02x%02x%02x", (r1 + r2) / 2, (g1 + g2) / 2, (b1 + b2) / 2);
229 value = WMCreatePLString(buffer);
230 } else {
231 value = c1;
233 } else {
234 XColor color1;
235 XColor color2;
237 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)), WMGetFromPLString(c1), &color1);
238 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)), WMGetFromPLString(c2), &color2);
240 sprintf(buffer, "#%02x%02x%02x",
241 (color1.red + color2.red) >> 9,
242 (color1.green + color2.green) >> 9, (color1.blue + color2.blue) >> 9);
243 value = WMCreatePLString(buffer);
245 } else if (strcasecmp(str, "mdgradient") == 0
246 || strcasecmp(str, "mhgradient") == 0 || strcasecmp(str, "mvgradient") == 0) {
248 value = WMGetFromPLArray(texture, 1);
250 } else if (strcasecmp(str, "tpixmap") == 0
251 || strcasecmp(str, "cpixmap") == 0 || strcasecmp(str, "spixmap") == 0) {
253 value = WMGetFromPLArray(texture, 2);
256 return value;
260 * since some of the options introduce incompatibilities, we will need
261 * to do a kluge here or the themes ppl will get real annoying.
262 * So, treat for the absence of the following options:
263 * IconTitleColor
264 * IconTitleBack
266 static void hackStyle(WMPropList * style)
268 WMPropList *keys, *tmp;
269 int foundIconTitle = 0, foundResizebarBack = 0;
270 int i;
272 keys = WMGetPLDictionaryKeys(style);
274 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
275 char *str;
277 tmp = WMGetFromPLArray(keys, i);
278 str = WMGetFromPLString(tmp);
279 if (str) {
280 if (ignoreFonts && isFontOption(str)) {
281 WMRemoveFromPLDictionary(style, tmp);
282 continue;
284 if (ignoreCursors && isCursorOption(str)) {
285 WMRemoveFromPLDictionary(style, tmp);
286 continue;
288 if (isFontOption(str)) {
289 WMPropList *value;
290 char *newfont, *oldfont;
292 value = WMGetFromPLDictionary(style, tmp);
293 if (value) {
294 oldfont = WMGetFromPLString(value);
295 newfont = convertFont(oldfont, False);
296 if (newfont != oldfont) {
297 value = WMCreatePLString(newfont);
298 WMPutInPLDictionary(style, tmp, value);
299 WMReleasePropList(value);
300 wfree(newfont);
304 if (strcasecmp(str, "IconTitleColor") == 0 || strcasecmp(str, "IconTitleBack") == 0) {
305 foundIconTitle = 1;
306 } else if (strcasecmp(str, "ResizebarBack") == 0) {
307 foundResizebarBack = 1;
312 if (!foundIconTitle) {
313 /* set the default values */
314 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleColor"));
315 if (tmp) {
316 WMPutInPLDictionary(style, WMCreatePLString("IconTitleColor"), tmp);
319 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleBack"));
320 if (tmp) {
321 WMPropList *value;
323 value = getColor(tmp);
325 if (value) {
326 WMPutInPLDictionary(style, WMCreatePLString("IconTitleBack"), value);
331 if (!foundResizebarBack) {
332 /* set the default values */
333 tmp = WMGetFromPLDictionary(style, WMCreatePLString("UTitleBack"));
334 if (tmp) {
335 WMPropList *value;
337 value = getColor(tmp);
339 if (value) {
340 WMPropList *t;
342 t = WMCreatePLArray(WMCreatePLString("solid"), value, NULL);
343 WMPutInPLDictionary(style, WMCreatePLString("ResizebarBack"), t);
348 if (!WMGetFromPLDictionary(style, WMCreatePLString("MenuStyle"))) {
349 WMPutInPLDictionary(style, WMCreatePLString("MenuStyle"), WMCreatePLString("normal"));
353 static void print_help(int print_usage, int exitval)
355 printf("Usage: %s [OPTIONS] FILE\n", __progname);
356 if (print_usage) {
357 puts("Reads style/theme configuration from FILE and updates Window Maker.");
358 puts("");
359 puts(" --no-fonts ignore font related options");
360 puts(" --no-cursors ignore cursor related options");
361 puts(" --ignore <option> ignore changes in the specified option");
362 puts(" -h, --help display this help and exit");
363 puts(" -v, --version output version information and exit");
365 exit(exitval);
368 int main(int argc, char **argv)
370 WMPropList *prop, *style;
371 char *path;
372 char *file = NULL;
373 struct stat st;
374 int i, ch, ignflag = 0;
375 int ignoreCount = 0;
376 char *ignoreList[MAX_OPTIONS];
377 XEvent ev;
379 struct option longopts[] = {
380 { "version", no_argument, NULL, 'v' },
381 { "help", no_argument, NULL, 'h' },
382 { "no-fonts", no_argument, &ignoreFonts, 1 },
383 { "no-cursors", no_argument, &ignoreCursors, 1 },
384 { "ignore", required_argument, &ignflag, 1 },
385 { NULL, 0, NULL, 0 }
388 while ((ch = getopt_long(argc, argv, "hv", longopts, NULL)) != -1)
389 switch(ch) {
390 case 'v':
391 printf("%s (Window Maker %s)\n", __progname, VERSION);
392 return 0;
393 /* NOTREACHED */
394 case 'h':
395 print_help(1, 0);
396 /* NOTREACHED */
397 case 0:
398 if (ignflag) {
399 if (ignoreCount >= MAX_OPTIONS) {
400 printf("Maximum %d `ignore' arguments\n", MAX_OPTIONS);
401 return 1;
403 ignoreList[ignoreCount++] = optarg;
404 ignflag = 0;
406 break;
407 default:
408 print_help(0, 1);
409 /* NOTREACHED */
412 argc -= optind;
413 argv += optind;
415 if (argc != 1)
416 print_help(0, 1);
418 file = argv[0];
420 WMPLSetCaseSensitive(False);
422 path = wdefaultspathfordomain("WindowMaker");
424 prop = WMReadPropListFromFile(path);
425 if (!prop) {
426 perror(path);
427 printf("%s: could not load WindowMaker configuration file.\n", __progname);
428 return 1;
431 if (stat(file, &st) < 0) {
432 perror(file);
433 return 1;
435 if (S_ISDIR(st.st_mode)) { /* theme pack */
436 char buf[PATH_MAX];
437 char *homedir;
439 if (realpath(file, buf) == NULL) {
440 perror(file);
441 return 1;
443 strncat(buf, "/style", sizeof(buf) - strlen(buf));
445 if (stat(buf, &st) != 0 || !S_ISREG(st.st_mode)) { /* maybe symlink too? */
446 printf("%s: %s: style file not found or not a file\n", __progname, buf);
447 return 1;
450 style = WMReadPropListFromFile(buf);
451 if (!style) {
452 perror(buf);
453 printf("%s: could not load style file.\n", __progname);
454 return 1;
457 buf[strlen(buf) - 6 /* strlen("/style") */] = '\0';
458 homedir = wstrdup(wgethomedir());
459 if (strlen(homedir) > 1 && /* this is insane, wgethomedir() returns `/' on error */
460 strncmp(homedir, buf, strlen(homedir)) == 0) {
461 /* theme pack is under ${HOME}; exchange ${HOME} part
462 * for `~' so it gets portable references to the user home dir */
463 *buf = '~';
464 memmove(buf + 1, buf + strlen(homedir), strlen(buf) - strlen(homedir) + 1);
466 wfree(homedir);
468 hackPaths(style, buf); /* this will prefix pixmaps in the style
469 * with absolute(ish) references */
471 } else { /* normal style file */
473 style = WMReadPropListFromFile(file);
474 if (!style) {
475 perror(file);
476 printf("%s:could not load style file.\n", __progname);
477 return 1;
481 if (!WMIsPLDictionary(style)) {
482 printf("%s: '%s' is not a style file/theme\n", __progname, file);
483 return 1;
486 hackStyle(style);
488 if (ignoreCount > 0) {
489 for (i = 0; i < ignoreCount; i++) {
490 WMRemoveFromPLDictionary(style, WMCreatePLString(ignoreList[i]));
494 WMMergePLDictionaries(prop, style, True);
496 WMWritePropListToFile(prop, path);
498 dpy = XOpenDisplay("");
499 if (dpy) {
500 memset(&ev, 0, sizeof(XEvent));
502 ev.xclient.type = ClientMessage;
503 ev.xclient.message_type = XInternAtom(dpy, "_WINDOWMAKER_COMMAND", False);
504 ev.xclient.window = DefaultRootWindow(dpy);
505 ev.xclient.format = 8;
506 strncpy(ev.xclient.data.b, "Reconfigure", sizeof(ev.xclient.data.b));
508 XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureRedirectMask, &ev);
509 XFlush(dpy);
512 return 0;