Idle fixes
[wmaker-crm.git] / util / setstyle.c
blob36224f58b47f8f2cafd228b274589c2b4b7aeb29
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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #define PROG_VERSION "setstyle (Window Maker) 0.6"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <X11/Xlib.h>
31 #include <WINGs/WUtil.h>
33 #include "../src/wconfig.h"
35 #define MAX_OPTIONS 128
37 char *FontOptions[] = {
38 "IconTitleFont",
39 "ClipTitleFont",
40 "DisplayFont",
41 "LargeDisplayFont",
42 "MenuTextFont",
43 "MenuTitleFont",
44 "WindowTitleFont",
45 NULL
48 char *CursorOptions[] = {
49 "NormalCursor",
50 "ArrowCursor",
51 "MoveCursor",
52 "ResizeCursor",
53 "TopLeftResizeCursor",
54 "TopRightResizeCursor",
55 "BottomLeftResizeCursor",
56 "BottomRightResizeCursor",
57 "VerticalResizeCursor",
58 "HorizontalResizeCursor",
59 "WaitCursor",
60 "QuestionCursor",
61 "TextCursor",
62 "SelectCursor",
63 NULL
66 extern char *__progname;
67 int ignoreFonts = 0;
68 int ignoreCursors = 0;
70 Display *dpy;
72 extern char *convertFont(char *font, Bool keepXLFD);
74 static Bool isCursorOption(char *option)
76 int i;
78 for (i = 0; CursorOptions[i] != NULL; i++) {
79 if (strcasecmp(option, CursorOptions[i]) == 0) {
80 return True;
84 return False;
87 static Bool isFontOption(char *option)
89 int i;
91 for (i = 0; FontOptions[i] != NULL; i++) {
92 if (strcasecmp(option, FontOptions[i]) == 0) {
93 return True;
97 return False;
100 void hackPathInTexture(WMPropList * texture, char *prefix)
102 WMPropList *type;
103 char *t;
105 /* get texture type */
106 type = WMGetFromPLArray(texture, 0);
107 t = WMGetFromPLString(type);
108 if (t == NULL)
109 return;
110 if (strcasecmp(t, "tpixmap") == 0
111 || strcasecmp(t, "spixmap") == 0
112 || strcasecmp(t, "mpixmap") == 0
113 || strcasecmp(t, "cpixmap") == 0
114 || strcasecmp(t, "tvgradient") == 0
115 || strcasecmp(t, "thgradient") == 0 || strcasecmp(t, "tdgradient") == 0) {
116 WMPropList *file;
117 char buffer[4018];
119 /* get pixmap file path */
120 file = WMGetFromPLArray(texture, 1);
121 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
122 /* replace path with full path */
123 WMDeleteFromPLArray(texture, 1);
124 WMInsertInPLArray(texture, 1, WMCreatePLString(buffer));
125 } else if (strcasecmp(t, "bitmap") == 0) {
126 WMPropList *file;
127 char buffer[4018];
129 /* get bitmap file path */
130 file = WMGetFromPLArray(texture, 1);
131 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
132 /* replace path with full path */
133 WMDeleteFromPLArray(texture, 1);
134 WMInsertInPLArray(texture, 1, WMCreatePLString(buffer));
136 /* get mask file path */
137 file = WMGetFromPLArray(texture, 2);
138 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
139 /* replace path with full path */
140 WMDeleteFromPLArray(texture, 2);
141 WMInsertInPLArray(texture, 2, WMCreatePLString(buffer));
145 void hackPaths(WMPropList * style, char *prefix)
147 WMPropList *keys;
148 WMPropList *key;
149 WMPropList *value;
150 int i;
152 keys = WMGetPLDictionaryKeys(style);
154 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
155 key = WMGetFromPLArray(keys, i);
157 value = WMGetFromPLDictionary(style, key);
158 if (!value)
159 continue;
161 if (strcasecmp(WMGetFromPLString(key), "WorkspaceSpecificBack") == 0) {
162 if (WMIsPLArray(value)) {
163 int j;
164 WMPropList *texture;
166 for (j = 0; j < WMGetPropListItemCount(value); j++) {
167 texture = WMGetFromPLArray(value, j);
169 if (texture && WMIsPLArray(texture)
170 && WMGetPropListItemCount(texture) > 2) {
172 hackPathInTexture(texture, prefix);
176 } else {
178 if (WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
180 hackPathInTexture(value, prefix);
187 static WMPropList *getColor(WMPropList * texture)
189 WMPropList *value, *type;
190 char *str;
192 type = WMGetFromPLArray(texture, 0);
193 if (!type)
194 return NULL;
196 value = NULL;
198 str = WMGetFromPLString(type);
199 if (strcasecmp(str, "solid") == 0) {
200 value = WMGetFromPLArray(texture, 1);
201 } else if (strcasecmp(str, "dgradient") == 0
202 || strcasecmp(str, "hgradient") == 0 || strcasecmp(str, "vgradient") == 0) {
203 WMPropList *c1, *c2;
204 int r1, g1, b1, r2, g2, b2;
205 char buffer[32];
207 c1 = WMGetFromPLArray(texture, 1);
208 c2 = WMGetFromPLArray(texture, 2);
209 if (!dpy) {
210 if (sscanf(WMGetFromPLString(c1), "#%2x%2x%2x", &r1, &g1, &b1) == 3
211 && sscanf(WMGetFromPLString(c2), "#%2x%2x%2x", &r2, &g2, &b2) == 3) {
212 sprintf(buffer, "#%02x%02x%02x", (r1 + r2) / 2, (g1 + g2) / 2, (b1 + b2) / 2);
213 value = WMCreatePLString(buffer);
214 } else {
215 value = c1;
217 } else {
218 XColor color1;
219 XColor color2;
221 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)), WMGetFromPLString(c1), &color1);
222 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)), WMGetFromPLString(c2), &color2);
224 sprintf(buffer, "#%02x%02x%02x",
225 (color1.red + color2.red) >> 9,
226 (color1.green + color2.green) >> 9, (color1.blue + color2.blue) >> 9);
227 value = WMCreatePLString(buffer);
229 } else if (strcasecmp(str, "mdgradient") == 0
230 || strcasecmp(str, "mhgradient") == 0 || strcasecmp(str, "mvgradient") == 0) {
232 value = WMGetFromPLArray(texture, 1);
234 } else if (strcasecmp(str, "tpixmap") == 0
235 || strcasecmp(str, "cpixmap") == 0 || strcasecmp(str, "spixmap") == 0) {
237 value = WMGetFromPLArray(texture, 2);
240 return value;
244 * since some of the options introduce incompatibilities, we will need
245 * to do a kluge here or the themes ppl will get real annoying.
246 * So, treat for the absence of the following options:
247 * IconTitleColor
248 * IconTitleBack
250 void hackStyle(WMPropList * style)
252 WMPropList *keys, *tmp;
253 int foundIconTitle = 0, foundResizebarBack = 0;
254 int i;
256 keys = WMGetPLDictionaryKeys(style);
258 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
259 char *str;
261 tmp = WMGetFromPLArray(keys, i);
262 str = WMGetFromPLString(tmp);
263 if (str) {
264 if (ignoreFonts && isFontOption(str)) {
265 WMRemoveFromPLDictionary(style, tmp);
266 continue;
268 if (ignoreCursors && isCursorOption(str)) {
269 WMRemoveFromPLDictionary(style, tmp);
270 continue;
272 if (isFontOption(str)) {
273 WMPropList *value;
274 char *newfont, *oldfont;
276 value = WMGetFromPLDictionary(style, tmp);
277 if (value) {
278 oldfont = WMGetFromPLString(value);
279 newfont = convertFont(oldfont, False);
280 if (newfont != oldfont) {
281 value = WMCreatePLString(newfont);
282 WMPutInPLDictionary(style, tmp, value);
283 WMReleasePropList(value);
284 wfree(newfont);
288 if (strcasecmp(str, "IconTitleColor") == 0 || strcasecmp(str, "IconTitleBack") == 0) {
289 foundIconTitle = 1;
290 } else if (strcasecmp(str, "ResizebarBack") == 0) {
291 foundResizebarBack = 1;
296 if (!foundIconTitle) {
297 /* set the default values */
298 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleColor"));
299 if (tmp) {
300 WMPutInPLDictionary(style, WMCreatePLString("IconTitleColor"), tmp);
303 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleBack"));
304 if (tmp) {
305 WMPropList *value;
307 value = getColor(tmp);
309 if (value) {
310 WMPutInPLDictionary(style, WMCreatePLString("IconTitleBack"), value);
315 if (!foundResizebarBack) {
316 /* set the default values */
317 tmp = WMGetFromPLDictionary(style, WMCreatePLString("UTitleBack"));
318 if (tmp) {
319 WMPropList *value;
321 value = getColor(tmp);
323 if (value) {
324 WMPropList *t;
326 t = WMCreatePLArray(WMCreatePLString("solid"), value, NULL);
327 WMPutInPLDictionary(style, WMCreatePLString("ResizebarBack"), t);
332 if (!WMGetFromPLDictionary(style, WMCreatePLString("MenuStyle"))) {
333 WMPutInPLDictionary(style, WMCreatePLString("MenuStyle"), WMCreatePLString("normal"));
337 void print_help()
339 printf("Usage: %s [OPTIONS] FILE\n", __progname);
340 puts("Reads style/theme configuration from FILE and updates Window Maker.");
341 puts("");
342 puts(" --no-fonts ignore font related options");
343 puts(" --no-cursors ignore cursor related options");
344 puts(" --ignore <option> ignore changes in the specified option");
345 puts(" -h, --help display this help and exit");
346 puts(" -v, --version output version information and exit");
349 int main(int argc, char **argv)
351 WMPropList *prop, *style;
352 char *path;
353 char *file = NULL;
354 struct stat statbuf;
355 int i;
356 int ignoreCount = 0;
357 char *ignoreList[MAX_OPTIONS];
359 dpy = XOpenDisplay("");
361 if (argc < 2) {
362 printf("%s: missing argument\n", __progname);
363 printf("Try '%s --help' for more information\n", __progname);
364 exit(1);
367 for (i = 1; i < argc; i++) {
368 if (strcmp("--ignore", argv[i]) == 0) {
369 i++;
370 if (i == argc) {
371 printf("%s: missing argument for option --ignore\n", __progname);
372 exit(1);
374 ignoreList[ignoreCount++] = argv[i];
376 } else if (strcmp("--no-fonts", argv[i]) == 0) {
377 ignoreFonts = 1;
378 } else if (strcmp("--no-cursors", argv[i]) == 0) {
379 ignoreCursors = 1;
380 } else if (strcmp("-v", argv[i]) == 0 || strcmp("--version", argv[i]) == 0) {
381 puts(PROG_VERSION);
382 exit(0);
383 } else if (strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) {
384 print_help();
385 exit(0);
386 } else {
387 if (file) {
388 printf("%s: invalid argument '%s'\n", __progname, argv[i]);
389 printf("Try '%s --help' for more information\n", __progname);
390 exit(1);
392 file = argv[i];
396 WMPLSetCaseSensitive(False);
398 path = wdefaultspathfordomain("WindowMaker");
400 prop = WMReadPropListFromFile(path);
401 if (!prop) {
402 perror(path);
403 printf("%s: could not load WindowMaker configuration file.\n", __progname);
404 exit(1);
407 if (stat(file, &statbuf) < 0) {
408 perror(file);
409 exit(1);
411 if (S_ISDIR(statbuf.st_mode)) {
412 char buffer[4018];
413 char *prefix;
414 /* theme pack */
416 if (*argv[argc - 1] != '/') {
417 if (!getcwd(buffer, 4000)) {
418 printf("%s: complete path for %s is too long\n", __progname, file);
419 exit(1);
421 if (strlen(buffer) + strlen(file) > 4000) {
422 printf("%s: complete path for %s is too long\n", __progname, file);
423 exit(1);
425 strcat(buffer, "/");
426 } else {
427 buffer[0] = 0;
429 strcat(buffer, file);
431 prefix = malloc(strlen(buffer) + 10);
432 if (!prefix) {
433 printf("%s: out of memory\n", __progname);
434 exit(1);
436 strcpy(prefix, buffer);
438 strcat(buffer, "/style");
440 style = WMReadPropListFromFile(buffer);
441 if (!style) {
442 perror(buffer);
443 printf("%s: could not load style file.\n", __progname);
444 exit(1);
447 hackPaths(style, prefix);
448 free(prefix);
449 } else {
450 /* normal style file */
452 style = WMReadPropListFromFile(file);
453 if (!style) {
454 perror(file);
455 printf("%s:could not load style file.\n", __progname);
456 exit(1);
460 if (!WMIsPLDictionary(style)) {
461 printf("%s: '%s' is not a style file/theme\n", __progname, file);
462 exit(1);
465 hackStyle(style);
467 if (ignoreCount > 0) {
468 for (i = 0; i < ignoreCount; i++) {
469 WMRemoveFromPLDictionary(style, WMCreatePLString(ignoreList[i]));
473 WMMergePLDictionaries(prop, style, True);
475 WMWritePropListToFile(prop, path);
477 XEvent ev;
479 if (dpy) {
480 int i;
481 char *msg = "Reconfigure";
483 memset(&ev, 0, sizeof(XEvent));
485 ev.xclient.type = ClientMessage;
486 ev.xclient.message_type = XInternAtom(dpy, "_WINDOWMAKER_COMMAND", False);
487 ev.xclient.window = DefaultRootWindow(dpy);
488 ev.xclient.format = 8;
490 for (i = 0; i <= strlen(msg); i++) {
491 ev.xclient.data.b[i] = msg[i];
493 XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureRedirectMask, &ev);
494 XFlush(dpy);
498 exit(0);