fixed issue with path assignment
[wmaker-crm.git] / util / setstyle.c
blob515934d42b3223d2238a9ca4c05e9009a7ff705c
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.
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;
77 extern char* convertFont(char *font, Bool keepXLFD);
79 WMPropList *readBlackBoxStyle(char *path);
82 static Bool
83 isCursorOption(char *option)
85 int i;
87 for (i=0; CursorOptions[i]!=NULL; i++) {
88 if (strcasecmp(option, CursorOptions[i])==0) {
89 return True;
93 return False;
97 static Bool
98 isFontOption(char *option)
100 int i;
102 for (i=0; FontOptions[i]!=NULL; i++) {
103 if (strcasecmp(option, FontOptions[i])==0) {
104 return True;
108 return False;
112 char*
113 defaultsPathForDomain(char *domain)
115 static char path[1024];
116 char *gspath;
118 gspath = getenv("GNUSTEP_USER_ROOT");
119 if (gspath) {
120 strcpy(path, gspath);
121 strcat(path, "/");
122 } else {
123 char *home;
125 home = getenv("HOME");
126 if (!home) {
127 printf("%s:could not get HOME environment variable!\n", ProgName);
128 exit(0);
131 strcpy(path, home);
132 strcat(path, "/GNUstep/");
134 strcat(path, DEFAULTS_DIR);
135 strcat(path, "/");
136 strcat(path, domain);
138 return path;
143 void
144 hackPathInTexture(WMPropList *texture, char *prefix)
146 WMPropList *type;
147 char *t;
149 /* get texture type */
150 type = WMGetFromPLArray(texture, 0);
151 t = WMGetFromPLString(type);
152 if (t == NULL)
153 return;
154 if (strcasecmp(t, "tpixmap")==0
155 || strcasecmp(t, "spixmap")==0
156 || strcasecmp(t, "mpixmap")==0
157 || strcasecmp(t, "cpixmap")==0
158 || strcasecmp(t, "tvgradient")==0
159 || strcasecmp(t, "thgradient")==0
160 || strcasecmp(t, "tdgradient")==0) {
161 WMPropList *file;
162 char buffer[4018];
164 /* get pixmap file path */
165 file = WMGetFromPLArray(texture, 1);
166 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
167 /* replace path with full path */
168 WMDeleteFromPLArray(texture, 1);
169 WMInsertInPLArray(texture, 1, WMCreatePLString(buffer));
170 } else if (strcasecmp(t, "bitmap") == 0) {
171 WMPropList *file;
172 char buffer[4018];
174 /* get bitmap file path */
175 file = WMGetFromPLArray(texture, 1);
176 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
177 /* replace path with full path */
178 WMDeleteFromPLArray(texture, 1);
179 WMInsertInPLArray(texture, 1, WMCreatePLString(buffer));
181 /* get mask file path */
182 file = WMGetFromPLArray(texture, 2);
183 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
184 /* replace path with full path */
185 WMDeleteFromPLArray(texture, 2);
186 WMInsertInPLArray(texture, 2, WMCreatePLString(buffer));
191 void
192 hackPaths(WMPropList *style, char *prefix)
194 WMPropList *keys;
195 WMPropList *key;
196 WMPropList *value;
197 int i;
200 keys = WMGetPLDictionaryKeys(style);
202 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
203 key = WMGetFromPLArray(keys, i);
205 value = WMGetFromPLDictionary(style, key);
206 if (!value)
207 continue;
209 if (strcasecmp(WMGetFromPLString(key), "WorkspaceSpecificBack")==0) {
210 if (WMIsPLArray(value)) {
211 int j;
212 WMPropList *texture;
214 for (j = 0; j < WMGetPropListItemCount(value); j++) {
215 texture = WMGetFromPLArray(value, j);
217 if (texture && WMIsPLArray(texture)
218 && WMGetPropListItemCount(texture) > 2) {
220 hackPathInTexture(texture, prefix);
224 } else {
226 if (WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
228 hackPathInTexture(value, prefix);
236 static WMPropList*
237 getColor(WMPropList *texture)
239 WMPropList *value, *type;
240 char *str;
242 type = WMGetFromPLArray(texture, 0);
243 if (!type)
244 return NULL;
246 value = NULL;
248 str = WMGetFromPLString(type);
249 if (strcasecmp(str, "solid")==0) {
250 value = WMGetFromPLArray(texture, 1);
251 } else if (strcasecmp(str, "dgradient")==0
252 || strcasecmp(str, "hgradient")==0
253 || strcasecmp(str, "vgradient")==0) {
254 WMPropList *c1, *c2;
255 int r1, g1, b1, r2, g2, b2;
256 char buffer[32];
258 c1 = WMGetFromPLArray(texture, 1);
259 c2 = WMGetFromPLArray(texture, 2);
260 if (!dpy) {
261 if (sscanf(WMGetFromPLString(c1), "#%2x%2x%2x", &r1, &g1, &b1)==3
262 && sscanf(WMGetFromPLString(c2), "#%2x%2x%2x", &r2, &g2, &b2)==3) {
263 sprintf(buffer, "#%02x%02x%02x", (r1+r2)/2, (g1+g2)/2,
264 (b1+b2)/2);
265 value = WMCreatePLString(buffer);
266 } else {
267 value = c1;
269 } else {
270 XColor color1;
271 XColor color2;
273 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
274 WMGetFromPLString(c1), &color1);
275 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
276 WMGetFromPLString(c2), &color2);
278 sprintf(buffer, "#%02x%02x%02x",
279 (color1.red+color2.red)>>9,
280 (color1.green+color2.green)>>9,
281 (color1.blue+color2.blue)>>9);
282 value = WMCreatePLString(buffer);
284 } else if (strcasecmp(str, "mdgradient")==0
285 || strcasecmp(str, "mhgradient")==0
286 || strcasecmp(str, "mvgradient")==0) {
288 value = WMGetFromPLArray(texture, 1);
290 } else if (strcasecmp(str, "tpixmap")==0
291 || strcasecmp(str, "cpixmap")==0
292 || strcasecmp(str, "spixmap")==0) {
294 value = WMGetFromPLArray(texture, 2);
297 return value;
302 * since some of the options introduce incompatibilities, we will need
303 * to do a kluge here or the themes ppl will get real annoying.
304 * So, treat for the absence of the following options:
305 * IconTitleColor
306 * IconTitleBack
308 void
309 hackStyle(WMPropList *style)
311 WMPropList *keys, *tmp;
312 int foundIconTitle = 0, foundResizebarBack = 0;
313 int i;
315 keys = WMGetPLDictionaryKeys(style);
317 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
318 char *str;
320 tmp = WMGetFromPLArray(keys, i);
321 str = WMGetFromPLString(tmp);
322 if (str) {
323 if (ignoreFonts && isFontOption(str)) {
324 WMRemoveFromPLDictionary(style, tmp);
325 continue;
327 if (ignoreCursors && isCursorOption(str)) {
328 WMRemoveFromPLDictionary(style, tmp);
329 continue;
331 if (isFontOption(str)) {
332 WMPropList *value;
333 char *newfont, *oldfont;
335 value = WMGetFromPLDictionary(style, tmp);
336 if (value) {
337 oldfont = WMGetFromPLString(value);
338 newfont = convertFont(oldfont, False);
339 if (newfont != oldfont) {
340 value = WMCreatePLString(newfont);
341 WMPutInPLDictionary(style, tmp, value);
342 WMReleasePropList(value);
343 wfree(newfont);
347 if (strcasecmp(str, "IconTitleColor")==0
348 || strcasecmp(str, "IconTitleBack")==0) {
349 foundIconTitle = 1;
350 } else if (strcasecmp(str, "ResizebarBack")==0) {
351 foundResizebarBack = 1;
356 if (!foundIconTitle) {
357 /* set the default values */
358 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleColor"));
359 if (tmp) {
360 WMPutInPLDictionary(style, WMCreatePLString("IconTitleColor"),
361 tmp);
364 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleBack"));
365 if (tmp) {
366 WMPropList *value;
368 value = getColor(tmp);
370 if (value) {
371 WMPutInPLDictionary(style, WMCreatePLString("IconTitleBack"),
372 value);
377 if (!foundResizebarBack) {
378 /* set the default values */
379 tmp = WMGetFromPLDictionary(style, WMCreatePLString("UTitleBack"));
380 if (tmp) {
381 WMPropList *value;
383 value = getColor(tmp);
385 if (value) {
386 WMPropList *t;
388 t = WMCreatePLArray(WMCreatePLString("solid"), value,
389 NULL);
390 WMPutInPLDictionary(style, WMCreatePLString("ResizebarBack"),
397 if (!WMGetFromPLDictionary(style, WMCreatePLString("MenuStyle"))) {
398 WMPutInPLDictionary(style, WMCreatePLString("MenuStyle"),
399 WMCreatePLString("normal"));
404 void
405 print_help()
407 printf("Usage: %s [OPTIONS] FILE\n", ProgName);
408 puts("Reads style/theme configuration from FILE and updates Window Maker.");
409 puts("");
410 puts(" --no-fonts ignore font related options");
411 puts(" --no-cursors ignore cursor related options");
412 puts(" --ignore <option> ignore changes in the specified option");
413 puts(" --help display this help and exit");
415 puts(" --format <format> specifies the format of the theme to be converted");
417 puts(" --version output version information and exit");
418 /*puts("");
419 puts("Supported formats: blackbox");*/
423 #define F_BLACKBOX 1
426 main(int argc, char **argv)
428 WMPropList *prop, *style;
429 char *path;
430 char *file = NULL;
431 struct stat statbuf;
432 int i;
433 int ignoreCount = 0;
434 char *ignoreList[MAX_OPTIONS];
436 dpy = XOpenDisplay("");
438 ProgName = argv[0];
440 if (argc<2) {
441 printf("%s: missing argument\n", ProgName);
442 printf("Try '%s --help' for more information\n", ProgName);
443 exit(1);
446 for (i = 1; i < argc; i++) {
447 if (strcmp("--ignore", argv[i])==0) {
448 i++;
449 if (i == argc) {
450 printf("%s: missing argument for option --ignore\n", ProgName);
451 exit(1);
453 ignoreList[ignoreCount++] = argv[i];
455 } else if (strcmp("--no-fonts", argv[i])==0) {
456 ignoreFonts = 1;
457 } else if (strcmp("--no-cursors", argv[i])==0) {
458 ignoreCursors = 1;
459 } else if (strcmp("--version", argv[i])==0) {
460 puts(PROG_VERSION);
461 exit(0);
462 } else if (strcmp("--help", argv[i])==0) {
463 print_help();
464 exit(0);
465 #if 0
466 } else if (strcmp("--format", argv[i])==0) {
467 i++;
468 if (i == argc) {
469 printf("%s: missing argument for option --format\n", ProgName);
470 exit(1);
472 if (strcasecmp(argv[i], "blackbox")==0) {
473 format = F_BLACKBOX;
474 } else {
475 printf("%s: unknown theme format '%s'\n", ProgName, argv[i]);
476 exit(1);
478 #endif
479 } else {
480 if (file) {
481 printf("%s: invalid argument '%s'\n", ProgName, argv[i]);
482 printf("Try '%s --help' for more information\n", ProgName);
483 exit(1);
485 file = argv[i];
489 WMPLSetCaseSensitive(False);
491 path = defaultsPathForDomain("WindowMaker");
493 prop = WMReadPropListFromFile(path);
494 if (!prop) {
495 perror(path);
496 printf("%s:could not load WindowMaker configuration file.\n",
497 ProgName);
498 exit(1);
501 if (stat(file, &statbuf) < 0) {
502 perror(file);
503 exit(1);
505 #if 0
506 if (format == F_BLACKBOX) {
507 style = readBlackBoxStyle(file);
508 if (!style) {
509 printf("%s: could not open style file\n", ProgName);
510 exit(1);
512 } else
513 #endif
515 if (S_ISDIR(statbuf.st_mode)) {
516 char buffer[4018];
517 char *prefix;
518 /* theme pack */
520 if (*argv[argc-1] != '/') {
521 if (!getcwd(buffer, 4000)) {
522 printf("%s: complete path for %s is too long\n", ProgName,
523 file);
524 exit(1);
526 if (strlen(buffer) + strlen(file) > 4000) {
527 printf("%s: complete path for %s is too long\n", ProgName,
528 file);
529 exit(1);
531 strcat(buffer, "/");
532 } else {
533 buffer[0] = 0;
535 strcat(buffer, file);
537 prefix = malloc(strlen(buffer)+10);
538 if (!prefix) {
539 printf("%s: out of memory\n", ProgName);
540 exit(1);
542 strcpy(prefix, buffer);
544 strcat(buffer, "/style");
546 style = WMReadPropListFromFile(buffer);
547 if (!style) {
548 perror(buffer);
549 printf("%s:could not load style file.\n", ProgName);
550 exit(1);
553 hackPaths(style, prefix);
554 free(prefix);
555 } else {
556 /* normal style file */
558 style = WMReadPropListFromFile(file);
559 if (!style) {
560 perror(file);
561 printf("%s:could not load style file.\n", ProgName);
562 exit(1);
567 if (!WMIsPLDictionary(style)) {
568 printf("%s: '%s' is not a style file/theme\n", ProgName, file);
569 exit(1);
572 hackStyle(style);
574 if (ignoreCount > 0) {
575 for (i = 0; i < ignoreCount; i++) {
576 WMRemoveFromPLDictionary(style, WMCreatePLString(ignoreList[i]));
580 WMMergePLDictionaries(prop, style, True);
582 WMWritePropListToFile(prop, path, True);
584 XEvent ev;
586 if (dpy) {
587 int i;
588 char *msg = "Reconfigure";
590 memset(&ev, 0, sizeof(XEvent));
592 ev.xclient.type = ClientMessage;
593 ev.xclient.message_type = XInternAtom(dpy, "_WINDOWMAKER_COMMAND",
594 False);
595 ev.xclient.window = DefaultRootWindow(dpy);
596 ev.xclient.format = 8;
598 for (i = 0; i <= strlen(msg); i++) {
599 ev.xclient.data.b[i] = msg[i];
601 XSendEvent(dpy, DefaultRootWindow(dpy), False,
602 SubstructureRedirectMask, &ev);
603 XFlush(dpy);
607 exit(0);
611 #if 0
612 char*
613 getToken(char *str, int i, char *buf)
619 static WMPropList*
620 readBlackBoxStyle(char *path)
622 FILE *f;
623 char buffer[128], char token[128];
624 WMPropList *style, *p;
626 f = fopen(path, "rb");
627 if (!f) {
628 perror(path);
629 return NULL;
632 while (1) {
633 if (!fgets(buffer, 127, f))
634 break;
636 if (strncasecmp(buffer, "menu.title:", 11)==0) {
641 #endif