wrlib: return NULL if XImage could not be taken, for consistency
[wmaker-crm.git] / util / setstyle.c
blob095c060482394da4e251b2eb1d3f0ca54c03f98c
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 "config.h"
28 #include <sys/stat.h>
30 #include <getopt.h>
31 #include <limits.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <strings.h>
36 #include <unistd.h>
37 #include <X11/Xlib.h>
39 #ifdef HAVE_STDNORETURN
40 #include <stdnoreturn.h>
41 #endif
43 #include <WINGs/WUtil.h>
45 #include "../src/wconfig.h"
47 #include "common.h"
49 #define MAX_OPTIONS 128
51 char *FontOptions[] = {
52 "IconTitleFont",
53 "ClipTitleFont",
54 "LargeDisplayFont",
55 "MenuTextFont",
56 "MenuTitleFont",
57 "WindowTitleFont",
58 NULL
61 char *CursorOptions[] = {
62 "NormalCursor",
63 "ArrowCursor",
64 "MoveCursor",
65 "ResizeCursor",
66 "TopLeftResizeCursor",
67 "TopRightResizeCursor",
68 "BottomLeftResizeCursor",
69 "BottomRightResizeCursor",
70 "VerticalResizeCursor",
71 "HorizontalResizeCursor",
72 "WaitCursor",
73 "QuestionCursor",
74 "TextCursor",
75 "SelectCursor",
76 NULL
79 extern char *__progname;
80 int ignoreFonts = 0;
81 int ignoreCursors = 0;
83 Display *dpy;
86 static Bool isCursorOption(const char *option)
88 int i;
90 for (i = 0; CursorOptions[i] != NULL; i++) {
91 if (strcasecmp(option, CursorOptions[i]) == 0) {
92 return True;
96 return False;
99 static Bool isFontOption(const char *option)
101 int i;
103 for (i = 0; FontOptions[i] != NULL; i++) {
104 if (strcasecmp(option, FontOptions[i]) == 0) {
105 return True;
109 return False;
113 * finds elements in `texture' that reference external files,
114 * prepends `prefix' to these files. `prefix' is a path component
115 * that qualifies the external references to be absolute, possibly
116 * pending further expansion
118 static void hackPathInTexture(WMPropList * texture, const char *prefix)
120 WMPropList *type;
121 char *t;
123 /* get texture type */
124 type = WMGetFromPLArray(texture, 0);
125 t = WMGetFromPLString(type);
126 if (t == NULL)
127 return;
129 if (strcasecmp(t, "tpixmap") == 0 ||
130 strcasecmp(t, "spixmap") == 0 ||
131 strcasecmp(t, "mpixmap") == 0 ||
132 strcasecmp(t, "cpixmap") == 0 ||
133 strcasecmp(t, "tvgradient") == 0 ||
134 strcasecmp(t, "thgradient") == 0 ||
135 strcasecmp(t, "tdgradient") == 0) {
136 WMPropList *file;
137 char buffer[4018];
139 /* get pixmap file path */
140 file = WMGetFromPLArray(texture, 1);
141 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
142 /* replace path with full path */
143 WMDeleteFromPLArray(texture, 1);
144 WMInsertInPLArray(texture, 1, WMCreatePLString(buffer));
146 } else if (strcasecmp(t, "bitmap") == 0) {
147 WMPropList *file;
148 char buffer[4018];
150 /* get bitmap file path */
151 file = WMGetFromPLArray(texture, 1);
152 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
153 /* replace path with full path */
154 WMDeleteFromPLArray(texture, 1);
155 WMInsertInPLArray(texture, 1, WMCreatePLString(buffer));
157 /* get mask file path */
158 file = WMGetFromPLArray(texture, 2);
159 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
160 /* replace path with full path */
161 WMDeleteFromPLArray(texture, 2);
162 WMInsertInPLArray(texture, 2, WMCreatePLString(buffer));
166 static void hackPaths(WMPropList * style, const char *prefix)
168 WMPropList *keys;
169 WMPropList *key;
170 WMPropList *value;
171 int i;
173 keys = WMGetPLDictionaryKeys(style);
175 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
176 key = WMGetFromPLArray(keys, i);
178 value = WMGetFromPLDictionary(style, key);
179 if (!value)
180 continue;
182 if (strcasecmp(WMGetFromPLString(key), "WorkspaceSpecificBack") == 0) {
183 if (WMIsPLArray(value)) {
184 int j;
185 WMPropList *texture;
187 for (j = 0; j < WMGetPropListItemCount(value); j++) {
188 texture = WMGetFromPLArray(value, j);
190 if (texture && WMIsPLArray(texture)
191 && WMGetPropListItemCount(texture) > 2) {
193 hackPathInTexture(texture, prefix);
197 } else {
199 if (WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
201 hackPathInTexture(value, prefix);
208 static WMPropList *getColor(WMPropList * texture)
210 WMPropList *value, *type;
211 char *str;
213 type = WMGetFromPLArray(texture, 0);
214 if (!type)
215 return NULL;
217 value = NULL;
219 str = WMGetFromPLString(type);
220 if (strcasecmp(str, "solid") == 0) {
221 value = WMGetFromPLArray(texture, 1);
222 } else if (strcasecmp(str, "dgradient") == 0
223 || strcasecmp(str, "hgradient") == 0 || strcasecmp(str, "vgradient") == 0) {
224 WMPropList *c1, *c2;
225 int r1, g1, b1, r2, g2, b2;
226 char buffer[32];
228 c1 = WMGetFromPLArray(texture, 1);
229 c2 = WMGetFromPLArray(texture, 2);
230 if (!dpy) {
231 if (sscanf(WMGetFromPLString(c1), "#%2x%2x%2x", &r1, &g1, &b1) == 3
232 && sscanf(WMGetFromPLString(c2), "#%2x%2x%2x", &r2, &g2, &b2) == 3) {
233 sprintf(buffer, "#%02x%02x%02x", (r1 + r2) / 2, (g1 + g2) / 2, (b1 + b2) / 2);
234 value = WMCreatePLString(buffer);
235 } else {
236 value = c1;
238 } else {
239 XColor color1;
240 XColor color2;
242 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)), WMGetFromPLString(c1), &color1);
243 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)), WMGetFromPLString(c2), &color2);
245 sprintf(buffer, "#%02x%02x%02x",
246 (color1.red + color2.red) >> 9,
247 (color1.green + color2.green) >> 9, (color1.blue + color2.blue) >> 9);
248 value = WMCreatePLString(buffer);
250 } else if (strcasecmp(str, "mdgradient") == 0
251 || strcasecmp(str, "mhgradient") == 0 || strcasecmp(str, "mvgradient") == 0) {
253 value = WMGetFromPLArray(texture, 1);
255 } else if (strcasecmp(str, "tpixmap") == 0
256 || strcasecmp(str, "cpixmap") == 0 || strcasecmp(str, "spixmap") == 0) {
258 value = WMGetFromPLArray(texture, 2);
261 return value;
265 * since some of the options introduce incompatibilities, we will need
266 * to do a kluge here or the themes ppl will get real annoying.
267 * So, treat for the absence of the following options:
268 * IconTitleColor
269 * IconTitleBack
271 static void hackStyle(WMPropList * style)
273 WMPropList *keys, *tmp;
274 int foundIconTitle = 0, foundResizebarBack = 0;
275 int i;
277 keys = WMGetPLDictionaryKeys(style);
279 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
280 char *str;
282 tmp = WMGetFromPLArray(keys, i);
283 str = WMGetFromPLString(tmp);
284 if (str) {
285 if (ignoreFonts && isFontOption(str)) {
286 WMRemoveFromPLDictionary(style, tmp);
287 continue;
289 if (ignoreCursors && isCursorOption(str)) {
290 WMRemoveFromPLDictionary(style, tmp);
291 continue;
293 if (isFontOption(str)) {
294 WMPropList *value;
295 char *newfont, *oldfont;
297 value = WMGetFromPLDictionary(style, tmp);
298 if (value) {
299 oldfont = WMGetFromPLString(value);
300 newfont = convertFont(oldfont, False);
301 if (newfont != oldfont) {
302 value = WMCreatePLString(newfont);
303 WMPutInPLDictionary(style, tmp, value);
304 WMReleasePropList(value);
305 wfree(newfont);
309 if (strcasecmp(str, "IconTitleColor") == 0 || strcasecmp(str, "IconTitleBack") == 0) {
310 foundIconTitle = 1;
311 } else if (strcasecmp(str, "ResizebarBack") == 0) {
312 foundResizebarBack = 1;
317 if (!foundIconTitle) {
318 /* set the default values */
319 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleColor"));
320 if (tmp) {
321 WMPutInPLDictionary(style, WMCreatePLString("IconTitleColor"), tmp);
324 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleBack"));
325 if (tmp) {
326 WMPropList *value;
328 value = getColor(tmp);
330 if (value) {
331 WMPutInPLDictionary(style, WMCreatePLString("IconTitleBack"), value);
336 if (!foundResizebarBack) {
337 /* set the default values */
338 tmp = WMGetFromPLDictionary(style, WMCreatePLString("UTitleBack"));
339 if (tmp) {
340 WMPropList *value;
342 value = getColor(tmp);
344 if (value) {
345 WMPropList *t;
347 t = WMCreatePLArray(WMCreatePLString("solid"), value, NULL);
348 WMPutInPLDictionary(style, WMCreatePLString("ResizebarBack"), t);
353 if (!WMGetFromPLDictionary(style, WMCreatePLString("MenuStyle"))) {
354 WMPutInPLDictionary(style, WMCreatePLString("MenuStyle"), WMCreatePLString("normal"));
358 static noreturn void print_help(int print_usage, int exitval)
360 printf("Usage: %s [OPTIONS] FILE\n", __progname);
361 if (print_usage) {
362 puts("Reads style/theme configuration from FILE and updates Window Maker.");
363 puts("");
364 puts(" --no-fonts ignore font related options");
365 puts(" --no-cursors ignore cursor related options");
366 puts(" --ignore <option> ignore changes in the specified option");
367 puts(" -h, --help display this help and exit");
368 puts(" -v, --version output version information and exit");
370 exit(exitval);
373 int main(int argc, char **argv)
375 WMPropList *prop, *style;
376 char *path;
377 char *file = NULL;
378 struct stat st;
379 int i, ch, ignflag = 0;
380 int ignoreCount = 0;
381 char *ignoreList[MAX_OPTIONS];
382 XEvent ev;
384 struct option longopts[] = {
385 { "version", no_argument, NULL, 'v' },
386 { "help", no_argument, NULL, 'h' },
387 { "no-fonts", no_argument, &ignoreFonts, 1 },
388 { "no-cursors", no_argument, &ignoreCursors, 1 },
389 { "ignore", required_argument, &ignflag, 1 },
390 { NULL, 0, NULL, 0 }
393 while ((ch = getopt_long(argc, argv, "hv", longopts, NULL)) != -1)
394 switch(ch) {
395 case 'v':
396 printf("%s (Window Maker %s)\n", __progname, VERSION);
397 return 0;
398 /* NOTREACHED */
399 case 'h':
400 print_help(1, 0);
401 /* NOTREACHED */
402 case 0:
403 if (ignflag) {
404 if (ignoreCount >= MAX_OPTIONS) {
405 printf("Maximum %d `ignore' arguments\n", MAX_OPTIONS);
406 return 1;
408 ignoreList[ignoreCount++] = optarg;
409 ignflag = 0;
411 break;
412 default:
413 print_help(0, 1);
414 /* NOTREACHED */
417 argc -= optind;
418 argv += optind;
420 if (argc != 1)
421 print_help(0, 1);
423 file = argv[0];
425 WMPLSetCaseSensitive(False);
427 path = wdefaultspathfordomain("WindowMaker");
429 prop = WMReadPropListFromFile(path);
430 if (!prop) {
431 perror(path);
432 printf("%s: could not load WindowMaker configuration file.\n", __progname);
433 return 1;
436 if (stat(file, &st) < 0) {
437 perror(file);
438 return 1;
440 if (S_ISDIR(st.st_mode)) { /* theme pack */
441 char buf[PATH_MAX];
442 char *homedir;
444 if (realpath(file, buf) == NULL) {
445 perror(file);
446 return 1;
448 strncat(buf, "/style", sizeof(buf) - strlen(buf) - 1);
450 if (stat(buf, &st) != 0 || !S_ISREG(st.st_mode)) { /* maybe symlink too? */
451 printf("%s: %s: style file not found or not a file\n", __progname, buf);
452 return 1;
455 style = WMReadPropListFromFile(buf);
456 if (!style) {
457 perror(buf);
458 printf("%s: could not load style file.\n", __progname);
459 return 1;
462 buf[strlen(buf) - 6 /* strlen("/style") */] = '\0';
463 homedir = wstrdup(wgethomedir());
464 if (strlen(homedir) > 1 && /* this is insane, wgethomedir() returns `/' on error */
465 strncmp(homedir, buf, strlen(homedir)) == 0) {
466 /* theme pack is under ${HOME}; exchange ${HOME} part
467 * for `~' so it gets portable references to the user home dir */
468 *buf = '~';
469 memmove(buf + 1, buf + strlen(homedir), strlen(buf) - strlen(homedir) + 1);
471 wfree(homedir);
473 hackPaths(style, buf); /* this will prefix pixmaps in the style
474 * with absolute(ish) references */
476 } else { /* normal style file */
478 style = WMReadPropListFromFile(file);
479 if (!style) {
480 perror(file);
481 printf("%s:could not load style file.\n", __progname);
482 return 1;
486 if (!WMIsPLDictionary(style)) {
487 printf("%s: '%s' is not a style file/theme\n", __progname, file);
488 return 1;
491 hackStyle(style);
493 if (ignoreCount > 0) {
494 for (i = 0; i < ignoreCount; i++) {
495 WMRemoveFromPLDictionary(style, WMCreatePLString(ignoreList[i]));
499 WMMergePLDictionaries(prop, style, True);
501 WMWritePropListToFile(prop, path);
503 dpy = XOpenDisplay("");
504 if (dpy) {
505 memset(&ev, 0, sizeof(XEvent));
507 ev.xclient.type = ClientMessage;
508 ev.xclient.message_type = XInternAtom(dpy, "_WINDOWMAKER_COMMAND", False);
509 ev.xclient.window = DefaultRootWindow(dpy);
510 ev.xclient.format = 8;
511 strncpy(ev.xclient.data.b, "Reconfigure", sizeof(ev.xclient.data.b));
513 XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureRedirectMask, &ev);
514 XFlush(dpy);
517 return 0;