- Finished moving to the new proplist handling code in WINGs.
[wmaker-crm.git] / util / setstyle.c
blobc5dd14d6baa0d517ecdbb87c060e59c50cb46d75
1 /* setstyle.c - loads style related options to wmaker
3 * WindowMaker window manager
4 *
5 * Copyright (c) 1997~2000 Alfredo K. Kojima
6 *
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.
24 #define PROG_VERSION "setstyle (Window Maker) 0.6"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <X11/Xlib.h>
32 #include <WINGs/WUtil.h>
36 #include "../src/wconfig.h"
38 #define MAX_OPTIONS 128
40 char *FontOptions[] = {
41 "IconTitleFont",
42 "ClipTitleFont",
43 "DisplayFont",
44 "LargeDisplayFont",
45 "MenuTextFont",
46 "MenuTitleFont",
47 "WindowTitleFont",
48 NULL
51 char *CursorOptions[] = {
52 "NormalCursor"
53 ,"ArrowCursor"
54 ,"MoveCursor"
55 ,"ResizeCursor"
56 ,"TopLeftResizeCursor"
57 ,"TopRightResizeCursor"
58 ,"BottomLeftResizeCursor"
59 ,"BottomRightResizeCursor"
60 ,"VerticalResizeCursor"
61 ,"HorizontalResizeCursor"
62 ,"WaitCursor"
63 ,"QuestionCursor"
64 ,"TextCursor"
65 ,"SelectCursor"
66 ,NULL
71 char *ProgName;
72 int ignoreFonts = 0;
73 int ignoreCursors = 0;
75 Display *dpy;
79 WMPropList *readBlackBoxStyle(char *path);
83 char*
84 defaultsPathForDomain(char *domain)
86 static char path[1024];
87 char *gspath;
89 gspath = getenv("GNUSTEP_USER_ROOT");
90 if (gspath) {
91 strcpy(path, gspath);
92 strcat(path, "/");
93 } else {
94 char *home;
96 home = getenv("HOME");
97 if (!home) {
98 printf("%s:could not get HOME environment variable!\n", ProgName);
99 exit(0);
102 strcpy(path, home);
103 strcat(path, "/GNUstep/");
105 strcat(path, DEFAULTS_DIR);
106 strcat(path, "/");
107 strcat(path, domain);
109 return path;
114 void
115 hackPathInTexture(WMPropList *texture, char *prefix)
117 WMPropList *type;
118 char *t;
120 /* get texture type */
121 type = WMGetFromPLArray(texture, 0);
122 t = WMGetFromPLString(type);
123 if (t == NULL)
124 return;
125 if (strcasecmp(t, "tpixmap")==0
126 || strcasecmp(t, "spixmap")==0
127 || strcasecmp(t, "mpixmap")==0
128 || strcasecmp(t, "cpixmap")==0
129 || strcasecmp(t, "tvgradient")==0
130 || strcasecmp(t, "thgradient")==0
131 || strcasecmp(t, "tdgradient")==0) {
132 WMPropList *file;
133 char buffer[4018];
135 /* get pixmap file path */
136 file = WMGetFromPLArray(texture, 1);
137 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
138 /* replace path with full path */
139 WMDeleteFromPLArray(texture, 1);
140 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));
162 void
163 hackPaths(WMPropList *style, char *prefix)
165 WMPropList *keys;
166 WMPropList *key;
167 WMPropList *value;
168 int i;
171 keys = WMGetPLDictionaryKeys(style);
173 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
174 key = WMGetFromPLArray(keys, i);
176 value = WMGetFromPLDictionary(style, key);
177 if (!value)
178 continue;
180 if (strcasecmp(WMGetFromPLString(key), "WorkspaceSpecificBack")==0) {
181 if (WMIsPLArray(value)) {
182 int j;
183 WMPropList *texture;
185 for (j = 0; j < WMGetPropListItemCount(value); j++) {
186 texture = WMGetFromPLArray(value, j);
188 if (texture && WMIsPLArray(texture)
189 && WMGetPropListItemCount(texture) > 2) {
191 hackPathInTexture(texture, prefix);
195 } else {
197 if (WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
199 hackPathInTexture(value, prefix);
207 static WMPropList*
208 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
224 || strcasecmp(str, "vgradient")==0) {
225 WMPropList *c1, *c2;
226 int r1, g1, b1, r2, g2, b2;
227 char buffer[32];
229 c1 = WMGetFromPLArray(texture, 1);
230 c2 = WMGetFromPLArray(texture, 2);
231 if (!dpy) {
232 if (sscanf(WMGetFromPLString(c1), "#%2x%2x%2x", &r1, &g1, &b1)==3
233 && sscanf(WMGetFromPLString(c2), "#%2x%2x%2x", &r2, &g2, &b2)==3) {
234 sprintf(buffer, "#%02x%02x%02x", (r1+r2)/2, (g1+g2)/2,
235 (b1+b2)/2);
236 value = WMCreatePLString(buffer);
237 } else {
238 value = c1;
240 } else {
241 XColor color1;
242 XColor color2;
244 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
245 WMGetFromPLString(c1), &color1);
246 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
247 WMGetFromPLString(c2), &color2);
249 sprintf(buffer, "#%02x%02x%02x",
250 (color1.red+color2.red)>>9,
251 (color1.green+color2.green)>>9,
252 (color1.blue+color2.blue)>>9);
253 value = WMCreatePLString(buffer);
255 } else if (strcasecmp(str, "mdgradient")==0
256 || strcasecmp(str, "mhgradient")==0
257 || strcasecmp(str, "mvgradient")==0) {
259 value = WMGetFromPLArray(texture, 1);
261 } else if (strcasecmp(str, "tpixmap")==0
262 || strcasecmp(str, "cpixmap")==0
263 || strcasecmp(str, "spixmap")==0) {
265 value = WMGetFromPLArray(texture, 2);
268 return value;
273 * since some of the options introduce incompatibilities, we will need
274 * to do a kluge here or the themes ppl will get real annoying.
275 * So, treat for the absence of the following options:
276 * IconTitleColor
277 * IconTitleBack
279 void
280 hackStyle(WMPropList *style)
282 WMPropList *keys, *tmp;
283 int foundIconTitle = 0, foundResizebarBack = 0;
284 int i;
286 keys = WMGetPLDictionaryKeys(style);
288 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
289 char *str;
291 tmp = WMGetFromPLArray(keys, i);
292 str = WMGetFromPLString(tmp);
293 if (str) {
294 int j, found;
296 if (ignoreFonts) {
297 for (j = 0, found = 0; FontOptions[j]!=NULL; j++) {
298 if (strcasecmp(str, FontOptions[j])==0) {
299 WMRemoveFromPLDictionary(style, tmp);
300 found = 1;
301 break;
304 if (found)
305 continue;
307 if (ignoreCursors) {
308 for (j = 0, found = 0; CursorOptions[j] != NULL; j++) {
309 if (strcasecmp(str, CursorOptions[j]) == 0) {
310 WMRemoveFromPLDictionary(style, tmp);
311 found = 1;
312 break;
315 if (found)
316 continue;
319 if (strcasecmp(str, "IconTitleColor")==0
320 || strcasecmp(str, "IconTitleBack")==0) {
321 foundIconTitle = 1;
322 } else if (strcasecmp(str, "ResizebarBack")==0) {
323 foundResizebarBack = 1;
328 if (!foundIconTitle) {
329 /* set the default values */
330 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleColor"));
331 if (tmp) {
332 WMPutInPLDictionary(style, WMCreatePLString("IconTitleColor"),
333 tmp);
336 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleBack"));
337 if (tmp) {
338 WMPropList *value;
340 value = getColor(tmp);
342 if (value) {
343 WMPutInPLDictionary(style, WMCreatePLString("IconTitleBack"),
344 value);
349 if (!foundResizebarBack) {
350 /* set the default values */
351 tmp = WMGetFromPLDictionary(style, WMCreatePLString("UTitleBack"));
352 if (tmp) {
353 WMPropList *value;
355 value = getColor(tmp);
357 if (value) {
358 WMPropList *t;
360 t = WMCreatePLArray(WMCreatePLString("solid"), value,
361 NULL);
362 WMPutInPLDictionary(style, WMCreatePLString("ResizebarBack"),
369 if (!WMGetFromPLDictionary(style, WMCreatePLString("MenuStyle"))) {
370 WMPutInPLDictionary(style, WMCreatePLString("MenuStyle"),
371 WMCreatePLString("normal"));
376 void
377 print_help()
379 printf("Usage: %s [OPTIONS] FILE\n", ProgName);
380 puts("Reads style/theme configuration from FILE and updates Window Maker.");
381 puts("");
382 puts(" --no-fonts ignore font related options");
383 /* Why these stupid tabs? They're misleading to the programmer,
384 * and they don't do any better than aligning via spaces: If you
385 * have a proportional font, all bets are off anyway. Sheesh.
387 puts(" --no-cursors ignore cursor related options");
388 puts(" --ignore <option> ignore changes in the specified option");
389 puts(" --help display this help and exit");
391 puts(" --format <format> specifies the format of the theme to be converted");
393 puts(" --version output version information and exit");
394 puts("");
395 puts("Supported formats: blackbox");
399 #define F_BLACKBOX 1
401 int
402 main(int argc, char **argv)
404 WMPropList *prop, *style;
405 char *path;
406 char *file = NULL;
407 struct stat statbuf;
408 int i;
409 int ignoreCount = 0;
410 char *ignoreList[MAX_OPTIONS];
412 dpy = XOpenDisplay("");
414 ProgName = argv[0];
416 if (argc<2) {
417 printf("%s: missing argument\n", ProgName);
418 printf("Try '%s --help' for more information\n", ProgName);
419 exit(1);
422 for (i = 1; i < argc; i++) {
423 if (strcmp("--ignore", argv[i])==0) {
424 i++;
425 if (i == argc) {
426 printf("%s: missing argument for option --ignore\n", ProgName);
427 exit(1);
429 ignoreList[ignoreCount++] = argv[i];
431 } else if (strcmp("--no-fonts", argv[i])==0) {
432 ignoreFonts = 1;
433 } else if (strcmp("--no-cursors", argv[i])==0) {
434 ignoreCursors = 1;
435 } else if (strcmp("--version", argv[i])==0) {
436 puts(PROG_VERSION);
437 exit(0);
438 } else if (strcmp("--help", argv[i])==0) {
439 print_help();
440 exit(0);
441 #if 0
442 } else if (strcmp("--format", argv[i])==0) {
443 i++;
444 if (i == argc) {
445 printf("%s: missing argument for option --format\n", ProgName);
446 exit(1);
448 if (strcasecmp(argv[i], "blackbox")==0) {
449 format = F_BLACKBOX;
450 } else {
451 printf("%s: unknown theme format '%s'\n", ProgName, argv[i]);
452 exit(1);
454 #endif
455 } else {
456 if (file) {
457 printf("%s: invalid argument '%s'\n", ProgName, argv[i]);
458 printf("Try '%s --help' for more information\n", ProgName);
459 exit(1);
461 file = argv[i];
465 WMPLSetCaseSensitive(False);
467 path = defaultsPathForDomain("WindowMaker");
469 prop = WMReadPropListFromFile(path);
470 if (!prop) {
471 perror(path);
472 printf("%s:could not load WindowMaker configuration file.\n",
473 ProgName);
474 exit(1);
477 if (stat(file, &statbuf) < 0) {
478 perror(file);
479 exit(1);
481 #if 0
482 if (format == F_BLACKBOX) {
483 style = readBlackBoxStyle(file);
484 if (!style) {
485 printf("%s: could not open style file\n", ProgName);
486 exit(1);
488 } else
489 #endif
491 if (S_ISDIR(statbuf.st_mode)) {
492 char buffer[4018];
493 char *prefix;
494 /* theme pack */
496 if (*argv[argc-1] != '/') {
497 if (!getcwd(buffer, 4000)) {
498 printf("%s: complete path for %s is too long\n", ProgName,
499 file);
500 exit(1);
502 if (strlen(buffer) + strlen(file) > 4000) {
503 printf("%s: complete path for %s is too long\n", ProgName,
504 file);
505 exit(1);
507 strcat(buffer, "/");
508 } else {
509 buffer[0] = 0;
511 strcat(buffer, file);
513 prefix = malloc(strlen(buffer)+10);
514 if (!prefix) {
515 printf("%s: out of memory\n", ProgName);
516 exit(1);
518 strcpy(prefix, buffer);
520 strcat(buffer, "/style");
522 style = WMReadPropListFromFile(buffer);
523 if (!style) {
524 perror(buffer);
525 printf("%s:could not load style file.\n", ProgName);
526 exit(1);
529 hackPaths(style, prefix);
530 free(prefix);
531 } else {
532 /* normal style file */
534 style = WMReadPropListFromFile(file);
535 if (!style) {
536 perror(file);
537 printf("%s:could not load style file.\n", ProgName);
538 exit(1);
543 if (!WMIsPLDictionary(style)) {
544 printf("%s: '%s' is not a style file/theme\n", ProgName, file);
545 exit(1);
548 hackStyle(style);
550 if (ignoreCount > 0) {
551 for (i = 0; i < ignoreCount; i++) {
552 WMRemoveFromPLDictionary(style, WMCreatePLString(ignoreList[i]));
556 WMMergePLDictionaries(prop, style);
558 WMWritePropListToFile(prop, path, True);
560 XEvent ev;
562 if (dpy) {
563 int i;
564 char *msg = "Reconfigure";
566 memset(&ev, 0, sizeof(XEvent));
568 ev.xclient.type = ClientMessage;
569 ev.xclient.message_type = XInternAtom(dpy, "_WINDOWMAKER_COMMAND",
570 False);
571 ev.xclient.window = DefaultRootWindow(dpy);
572 ev.xclient.format = 8;
574 for (i = 0; i <= strlen(msg); i++) {
575 ev.xclient.data.b[i] = msg[i];
577 XSendEvent(dpy, DefaultRootWindow(dpy), False,
578 SubstructureRedirectMask, &ev);
579 XFlush(dpy);
583 exit(0);
587 #if 0
588 char*
589 getToken(char *str, int i, char *buf)
595 static WMPropList*
596 readBlackBoxStyle(char *path)
598 FILE *f;
599 char buffer[128], char token[128];
600 WMPropList *style, *p;
602 f = fopen(path, "r");
603 if (!f) {
604 perror(path);
605 return NULL;
608 while (1) {
609 if (!fgets(buffer, 127, f))
610 break;
612 if (strncasecmp(buffer, "menu.title:", 11)==0) {
617 #endif