more updates to the proplist code
[wmaker-crm.git] / util / setstyle.c
blob00e7cdfaa7ce57e6081ca3978c63633b32b9856c
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 <proplist.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
32 #include <X11/Xlib.h>
34 #include <string.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 proplist_t 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(proplist_t texture, char *prefix)
117 proplist_t type;
118 char *t;
120 /* get texture type */
121 type = PLGetArrayElement(texture, 0);
122 t = PLGetString(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 proplist_t file;
133 char buffer[4018];
135 /* get pixmap file path */
136 file = PLGetArrayElement(texture, 1);
137 sprintf(buffer, "%s/%s", prefix, PLGetString(file));
138 /* replace path with full path */
139 PLRemoveArrayElement(texture, 1);
140 PLInsertArrayElement(texture, PLMakeString(buffer), 1);
141 } else if (strcasecmp(t, "bitmap") == 0) {
142 proplist_t file;
143 char buffer[4018];
145 /* get bitmap file path */
146 file = PLGetArrayElement(texture, 1);
147 sprintf(buffer, "%s/%s", prefix, PLGetString(file));
148 /* replace path with full path */
149 PLRemoveArrayElement(texture, 1);
150 PLInsertArrayElement(texture, PLMakeString(buffer), 1);
152 /* get mask file path */
153 file = PLGetArrayElement(texture, 2);
154 sprintf(buffer, "%s/%s", prefix, PLGetString(file));
155 /* replace path with full path */
156 PLRemoveArrayElement(texture, 2);
157 PLInsertArrayElement(texture, PLMakeString(buffer), 2);
162 void
163 hackPaths(proplist_t style, char *prefix)
165 proplist_t keys;
166 proplist_t key;
167 proplist_t value;
168 int i;
171 keys = PLGetAllDictionaryKeys(style);
173 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
174 key = PLGetArrayElement(keys, i);
176 value = PLGetDictionaryEntry(style, key);
177 if (!value)
178 continue;
180 if (strcasecmp(PLGetString(key), "WorkspaceSpecificBack")==0) {
181 if (PLIsArray(value)) {
182 int j;
183 proplist_t texture;
185 for (j = 0; j < PLGetNumberOfElements(value); j++) {
186 texture = PLGetArrayElement(value, j);
188 if (texture && PLIsArray(texture)
189 && PLGetNumberOfElements(texture) > 2) {
191 hackPathInTexture(texture, prefix);
195 } else {
197 if (PLIsArray(value) && PLGetNumberOfElements(value) > 2) {
199 hackPathInTexture(value, prefix);
207 static proplist_t
208 getColor(proplist_t texture)
210 proplist_t value, type;
211 char *str;
213 type = PLGetArrayElement(texture, 0);
214 if (!type)
215 return NULL;
217 value = NULL;
219 str = PLGetString(type);
220 if (strcasecmp(str, "solid")==0) {
221 value = PLGetArrayElement(texture, 1);
222 } else if (strcasecmp(str, "dgradient")==0
223 || strcasecmp(str, "hgradient")==0
224 || strcasecmp(str, "vgradient")==0) {
225 proplist_t c1, c2;
226 int r1, g1, b1, r2, g2, b2;
227 char buffer[32];
229 c1 = PLGetArrayElement(texture, 1);
230 c2 = PLGetArrayElement(texture, 2);
231 if (!dpy) {
232 if (sscanf(PLGetString(c1), "#%2x%2x%2x", &r1, &g1, &b1)==3
233 && sscanf(PLGetString(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 = PLMakeString(buffer);
237 } else {
238 value = c1;
240 } else {
241 XColor color1;
242 XColor color2;
244 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
245 PLGetString(c1), &color1);
246 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
247 PLGetString(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 = PLMakeString(buffer);
255 } else if (strcasecmp(str, "mdgradient")==0
256 || strcasecmp(str, "mhgradient")==0
257 || strcasecmp(str, "mvgradient")==0) {
259 value = PLGetArrayElement(texture, 1);
261 } else if (strcasecmp(str, "tpixmap")==0
262 || strcasecmp(str, "cpixmap")==0
263 || strcasecmp(str, "spixmap")==0) {
265 value = PLGetArrayElement(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(proplist_t style)
282 proplist_t keys;
283 proplist_t tmp;
284 int i;
285 int foundIconTitle = 0;
286 int foundResizebarBack = 0;
288 keys = PLGetAllDictionaryKeys(style);
290 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
291 char *str;
293 tmp = PLGetArrayElement(keys, i);
294 str = PLGetString(tmp);
295 if (str) {
296 int j, found;
298 if (ignoreFonts) {
299 for (j = 0, found = 0; FontOptions[j]!=NULL; j++) {
300 if (strcasecmp(str, FontOptions[j])==0) {
301 PLRemoveDictionaryEntry(style, tmp);
302 found = 1;
303 break;
306 if (found)
307 continue;
309 if (ignoreCursors) {
310 for (j = 0, found = 0; CursorOptions[j] != NULL; j++) {
311 if (strcasecmp(str, CursorOptions[j]) == 0) {
312 PLRemoveDictionaryEntry(style, tmp);
313 found = 1;
314 break;
317 if (found)
318 continue;
321 if (strcasecmp(str, "IconTitleColor")==0
322 || strcasecmp(str, "IconTitleBack")==0) {
323 foundIconTitle = 1;
324 } else if (strcasecmp(str, "ResizebarBack")==0) {
325 foundResizebarBack = 1;
330 if (!foundIconTitle) {
331 /* set the default values */
332 tmp = PLGetDictionaryEntry(style, PLMakeString("FTitleColor"));
333 if (tmp) {
334 PLInsertDictionaryEntry(style, PLMakeString("IconTitleColor"),
335 tmp);
338 tmp = PLGetDictionaryEntry(style, PLMakeString("FTitleBack"));
339 if (tmp) {
340 proplist_t value;
342 value = getColor(tmp);
344 if (value) {
345 PLInsertDictionaryEntry(style, PLMakeString("IconTitleBack"),
346 value);
351 if (!foundResizebarBack) {
352 /* set the default values */
353 tmp = PLGetDictionaryEntry(style, PLMakeString("UTitleBack"));
354 if (tmp) {
355 proplist_t value;
357 value = getColor(tmp);
359 if (value) {
360 proplist_t t;
362 t = PLMakeArrayFromElements(PLMakeString("solid"), value,
363 NULL);
364 PLInsertDictionaryEntry(style, PLMakeString("ResizebarBack"),
371 if (!PLGetDictionaryEntry(style, PLMakeString("MenuStyle"))) {
372 PLInsertDictionaryEntry(style, PLMakeString("MenuStyle"),
373 PLMakeString("normal"));
378 BOOL
379 StringCompareHook(proplist_t pl1, proplist_t pl2)
381 char *str1, *str2;
383 str1 = PLGetString(pl1);
384 str2 = PLGetString(pl2);
386 if (strcasecmp(str1, str2)==0)
387 return YES;
388 else
389 return NO;
393 void
394 print_help()
396 printf("Usage: %s [OPTIONS] FILE\n", ProgName);
397 puts("Reads style/theme configuration from FILE and updates Window Maker.");
398 puts("");
399 puts(" --no-fonts ignore font related options");
400 /* Why these stupid tabs? They're misleading to the programmer,
401 * and they don't do any better than aligning via spaces: If you
402 * have a proportional font, all bets are off anyway. Sheesh.
404 puts(" --no-cursors ignore cursor related options");
405 puts(" --ignore <option> ignore changes in the specified option");
406 puts(" --help display this help and exit");
408 puts(" --format <format> specifies the format of the theme to be converted");
410 puts(" --version output version information and exit");
411 puts("");
412 puts("Supported formats: blackbox");
416 #define F_BLACKBOX 1
418 int
419 main(int argc, char **argv)
421 proplist_t prop, style;
422 char *path;
423 char *file = NULL;
424 struct stat statbuf;
425 int i;
426 int ignoreCount = 0;
427 char *ignoreList[MAX_OPTIONS];
429 dpy = XOpenDisplay("");
431 ProgName = argv[0];
433 if (argc<2) {
434 printf("%s: missing argument\n", ProgName);
435 printf("Try '%s --help' for more information\n", ProgName);
436 exit(1);
439 for (i = 1; i < argc; i++) {
440 if (strcmp("--ignore", argv[i])==0) {
441 i++;
442 if (i == argc) {
443 printf("%s: missing argument for option --ignore\n", ProgName);
444 exit(1);
446 ignoreList[ignoreCount++] = argv[i];
448 } else if (strcmp("--no-fonts", argv[i])==0) {
449 ignoreFonts = 1;
450 } else if (strcmp("--no-cursors", argv[i])==0) {
451 ignoreCursors = 1;
452 } else if (strcmp("--version", argv[i])==0) {
453 puts(PROG_VERSION);
454 exit(0);
455 } else if (strcmp("--help", argv[i])==0) {
456 print_help();
457 exit(0);
458 #if 0
459 } else if (strcmp("--format", argv[i])==0) {
460 i++;
461 if (i == argc) {
462 printf("%s: missing argument for option --format\n", ProgName);
463 exit(1);
465 if (strcasecmp(argv[i], "blackbox")==0) {
466 format = F_BLACKBOX;
467 } else {
468 printf("%s: unknown theme format '%s'\n", ProgName, argv[i]);
469 exit(1);
471 #endif
472 } else {
473 if (file) {
474 printf("%s: invalid argument '%s'\n", ProgName, argv[i]);
475 printf("Try '%s --help' for more information\n", ProgName);
476 exit(1);
478 file = argv[i];
482 PLSetStringCmpHook(StringCompareHook);
484 path = defaultsPathForDomain("WindowMaker");
486 prop = PLGetProplistWithPath(path);
487 if (!prop) {
488 perror(path);
489 printf("%s:could not load WindowMaker configuration file.\n",
490 ProgName);
491 exit(1);
494 if (stat(file, &statbuf) < 0) {
495 perror(file);
496 exit(1);
498 #if 0
499 if (format == F_BLACKBOX) {
500 style = readBlackBoxStyle(file);
501 if (!style) {
502 printf("%s: could not open style file\n", ProgName);
503 exit(1);
505 } else
506 #endif
508 if (S_ISDIR(statbuf.st_mode)) {
509 char buffer[4018];
510 char *prefix;
511 /* theme pack */
513 if (*argv[argc-1] != '/') {
514 if (!getcwd(buffer, 4000)) {
515 printf("%s: complete path for %s is too long\n", ProgName,
516 file);
517 exit(1);
519 if (strlen(buffer) + strlen(file) > 4000) {
520 printf("%s: complete path for %s is too long\n", ProgName,
521 file);
522 exit(1);
524 strcat(buffer, "/");
525 } else {
526 buffer[0] = 0;
528 strcat(buffer, file);
530 prefix = malloc(strlen(buffer)+10);
531 if (!prefix) {
532 printf("%s: out of memory\n", ProgName);
533 exit(1);
535 strcpy(prefix, buffer);
537 strcat(buffer, "/style");
539 style = PLGetProplistWithPath(buffer);
540 if (!style) {
541 perror(buffer);
542 printf("%s:could not load style file.\n", ProgName);
543 exit(1);
546 hackPaths(style, prefix);
547 free(prefix);
548 } else {
549 /* normal style file */
551 style = PLGetProplistWithPath(file);
552 if (!style) {
553 perror(file);
554 printf("%s:could not load style file.\n", ProgName);
555 exit(1);
560 if (!PLIsDictionary(style)) {
561 printf("%s: '%s' is not a style file/theme\n", ProgName, file);
562 exit(1);
565 hackStyle(style);
567 if (ignoreCount > 0) {
568 for (i = 0; i < ignoreCount; i++) {
569 PLRemoveDictionaryEntry(style, PLMakeString(ignoreList[i]));
573 PLMergeDictionaries(prop, style);
575 PLSave(prop, YES);
577 XEvent ev;
579 if (dpy) {
580 int i;
581 char *msg = "Reconfigure";
583 memset(&ev, 0, sizeof(XEvent));
585 ev.xclient.type = ClientMessage;
586 ev.xclient.message_type = XInternAtom(dpy, "_WINDOWMAKER_COMMAND",
587 False);
588 ev.xclient.window = DefaultRootWindow(dpy);
589 ev.xclient.format = 8;
591 for (i = 0; i <= strlen(msg); i++) {
592 ev.xclient.data.b[i] = msg[i];
594 XSendEvent(dpy, DefaultRootWindow(dpy), False,
595 SubstructureRedirectMask, &ev);
596 XFlush(dpy);
600 exit(0);
604 #if 0
605 char*
606 getToken(char *str, int i, char *buf)
612 proplist_t
613 readBlackBoxStyle(char *path)
615 FILE *f;
616 char buffer[128], char token[128];
617 proplist_t style;
618 proplist_t p;
620 f = fopen(path, "r");
621 if (!f) {
622 perror(path);
623 return NULL;
626 while (1) {
627 if (!fgets(buffer, 127, f))
628 break;
630 if (strncasecmp(buffer, "menu.title:", 11)==0) {
635 #endif