- Fixed crashing bug in menu.c
[wmaker-crm.git] / util / setstyle.c
blob77228a3d659b8212268646086c2991eb166210cb
1 /* setstyle.c - loads style related options to wmaker
3 * WindowMaker window manager
4 *
5 * Copyright (c) 1997-2003 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 puts(" --no-cursors ignore cursor related options");
384 puts(" --ignore <option> ignore changes in the specified option");
385 puts(" --help display this help and exit");
387 puts(" --format <format> specifies the format of the theme to be converted");
389 puts(" --version output version information and exit");
390 /*puts("");
391 puts("Supported formats: blackbox");*/
395 #define F_BLACKBOX 1
397 int
398 main(int argc, char **argv)
400 WMPropList *prop, *style;
401 char *path;
402 char *file = NULL;
403 struct stat statbuf;
404 int i;
405 int ignoreCount = 0;
406 char *ignoreList[MAX_OPTIONS];
408 dpy = XOpenDisplay("");
410 ProgName = argv[0];
412 if (argc<2) {
413 printf("%s: missing argument\n", ProgName);
414 printf("Try '%s --help' for more information\n", ProgName);
415 exit(1);
418 for (i = 1; i < argc; i++) {
419 if (strcmp("--ignore", argv[i])==0) {
420 i++;
421 if (i == argc) {
422 printf("%s: missing argument for option --ignore\n", ProgName);
423 exit(1);
425 ignoreList[ignoreCount++] = argv[i];
427 } else if (strcmp("--no-fonts", argv[i])==0) {
428 ignoreFonts = 1;
429 } else if (strcmp("--no-cursors", argv[i])==0) {
430 ignoreCursors = 1;
431 } else if (strcmp("--version", argv[i])==0) {
432 puts(PROG_VERSION);
433 exit(0);
434 } else if (strcmp("--help", argv[i])==0) {
435 print_help();
436 exit(0);
437 #if 0
438 } else if (strcmp("--format", argv[i])==0) {
439 i++;
440 if (i == argc) {
441 printf("%s: missing argument for option --format\n", ProgName);
442 exit(1);
444 if (strcasecmp(argv[i], "blackbox")==0) {
445 format = F_BLACKBOX;
446 } else {
447 printf("%s: unknown theme format '%s'\n", ProgName, argv[i]);
448 exit(1);
450 #endif
451 } else {
452 if (file) {
453 printf("%s: invalid argument '%s'\n", ProgName, argv[i]);
454 printf("Try '%s --help' for more information\n", ProgName);
455 exit(1);
457 file = argv[i];
461 WMPLSetCaseSensitive(False);
463 path = defaultsPathForDomain("WindowMaker");
465 prop = WMReadPropListFromFile(path);
466 if (!prop) {
467 perror(path);
468 printf("%s:could not load WindowMaker configuration file.\n",
469 ProgName);
470 exit(1);
473 if (stat(file, &statbuf) < 0) {
474 perror(file);
475 exit(1);
477 #if 0
478 if (format == F_BLACKBOX) {
479 style = readBlackBoxStyle(file);
480 if (!style) {
481 printf("%s: could not open style file\n", ProgName);
482 exit(1);
484 } else
485 #endif
487 if (S_ISDIR(statbuf.st_mode)) {
488 char buffer[4018];
489 char *prefix;
490 /* theme pack */
492 if (*argv[argc-1] != '/') {
493 if (!getcwd(buffer, 4000)) {
494 printf("%s: complete path for %s is too long\n", ProgName,
495 file);
496 exit(1);
498 if (strlen(buffer) + strlen(file) > 4000) {
499 printf("%s: complete path for %s is too long\n", ProgName,
500 file);
501 exit(1);
503 strcat(buffer, "/");
504 } else {
505 buffer[0] = 0;
507 strcat(buffer, file);
509 prefix = malloc(strlen(buffer)+10);
510 if (!prefix) {
511 printf("%s: out of memory\n", ProgName);
512 exit(1);
514 strcpy(prefix, buffer);
516 strcat(buffer, "/style");
518 style = WMReadPropListFromFile(buffer);
519 if (!style) {
520 perror(buffer);
521 printf("%s:could not load style file.\n", ProgName);
522 exit(1);
525 hackPaths(style, prefix);
526 free(prefix);
527 } else {
528 /* normal style file */
530 style = WMReadPropListFromFile(file);
531 if (!style) {
532 perror(file);
533 printf("%s:could not load style file.\n", ProgName);
534 exit(1);
539 if (!WMIsPLDictionary(style)) {
540 printf("%s: '%s' is not a style file/theme\n", ProgName, file);
541 exit(1);
544 hackStyle(style);
546 if (ignoreCount > 0) {
547 for (i = 0; i < ignoreCount; i++) {
548 WMRemoveFromPLDictionary(style, WMCreatePLString(ignoreList[i]));
552 WMMergePLDictionaries(prop, style, True);
554 WMWritePropListToFile(prop, path, True);
556 XEvent ev;
558 if (dpy) {
559 int i;
560 char *msg = "Reconfigure";
562 memset(&ev, 0, sizeof(XEvent));
564 ev.xclient.type = ClientMessage;
565 ev.xclient.message_type = XInternAtom(dpy, "_WINDOWMAKER_COMMAND",
566 False);
567 ev.xclient.window = DefaultRootWindow(dpy);
568 ev.xclient.format = 8;
570 for (i = 0; i <= strlen(msg); i++) {
571 ev.xclient.data.b[i] = msg[i];
573 XSendEvent(dpy, DefaultRootWindow(dpy), False,
574 SubstructureRedirectMask, &ev);
575 XFlush(dpy);
579 exit(0);
583 #if 0
584 char*
585 getToken(char *str, int i, char *buf)
591 static WMPropList*
592 readBlackBoxStyle(char *path)
594 FILE *f;
595 char buffer[128], char token[128];
596 WMPropList *style, *p;
598 f = fopen(path, "rb");
599 if (!f) {
600 perror(path);
601 return NULL;
604 while (1) {
605 if (!fgets(buffer, 127, f))
606 break;
608 if (strncasecmp(buffer, "menu.title:", 11)==0) {
613 #endif