changed indentation to use spaces only
[wmaker-crm.git] / util / setstyle.c
blob869f7d99e8ac9701ce7d552584a830e4ea3dcff2
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 #define DEFAULT_FONT "sans:pixelsize=12"
42 char *FontOptions[] = {
43 "IconTitleFont",
44 "ClipTitleFont",
45 "DisplayFont",
46 "LargeDisplayFont",
47 "MenuTextFont",
48 "MenuTitleFont",
49 "WindowTitleFont",
50 NULL
53 char *CursorOptions[] = {
54 "NormalCursor",
55 "ArrowCursor",
56 "MoveCursor",
57 "ResizeCursor",
58 "TopLeftResizeCursor",
59 "TopRightResizeCursor",
60 "BottomLeftResizeCursor",
61 "BottomRightResizeCursor",
62 "VerticalResizeCursor",
63 "HorizontalResizeCursor",
64 "WaitCursor",
65 "QuestionCursor",
66 "TextCursor",
67 "SelectCursor",
68 NULL
73 char *ProgName;
74 int ignoreFonts = 0;
75 int ignoreCursors = 0;
77 Display *dpy;
81 WMPropList *readBlackBoxStyle(char *path);
84 static Bool
85 isCursorOption(char *option)
87 int i;
89 for (i=0; CursorOptions[i]!=NULL; i++) {
90 if (strcasecmp(option, CursorOptions[i])==0) {
91 return True;
95 return False;
99 static Bool
100 isFontOption(char *option)
102 int i;
104 for (i=0; FontOptions[i]!=NULL; i++) {
105 if (strcasecmp(option, FontOptions[i])==0) {
106 return True;
110 return False;
114 static int
115 countChar(char *str, char c)
117 int count = 0;
119 if (!str)
120 return 0;
122 for (; *str!=0; str++) {
123 if (*str == c) {
124 count++;
128 return count;
132 typedef struct str {
133 char *str;
134 int len;
135 } str;
137 #define XLFD_TOKENS 14
139 static str*
140 getXLFDTokens(char *xlfd)
142 static str tokens[XLFD_TOKENS];
143 int i, len, size;
144 char *ptr;
146 if (!xlfd || countChar(xlfd, '-')<XLFD_TOKENS)
147 return NULL;
149 memset(tokens, 0, sizeof(str)*XLFD_TOKENS);
151 len = strlen(xlfd);
153 for (ptr=xlfd, i=0; i<XLFD_TOKENS && len>0; i++) {
154 size = strspn(ptr, "-");
155 ptr += size;
156 len -= size;
157 if (len <= 0)
158 break;
159 size = strcspn(ptr, "-");
160 if (size==0)
161 break;
162 tokens[i].str = ptr;
163 tokens[i].len = size;
164 ptr += size;
165 len -= size;
168 return tokens;
172 static int
173 strToInt(str *token)
175 int res=0, pos, c;
177 if (token->len==0 || token->str[0]=='*') {
178 return -1;
179 } else {
180 for (res=0, pos=0; pos<token->len; pos++) {
181 c = token->str[pos] - '0';
182 if (c<0 || c>9)
183 break;
184 res = res*10 + c;
187 return res;
191 static char*
192 mapSlantToName(str *slant)
194 if (slant->len==0 || slant->str[0]=='*')
195 return "roman";
197 switch(slant->str[0]) {
198 case 'i':
199 return "italic";
200 case 'o':
201 return "oblique";
202 case 'r':
203 default:
204 return "roman";
209 char*
210 xlfdToFc(char *xlfd)
212 str *tokens, *family, *weight, *slant;
213 char *name, buf[512];
214 int size, pixelsize;
216 tokens = getXLFDTokens(xlfd);
217 if (!tokens)
218 return wstrdup(DEFAULT_FONT);
220 family = &(tokens[1]);
221 weight = &(tokens[2]);
222 slant = &(tokens[3]);
224 if (family->len==0 || family->str[0]=='*')
225 return wstrdup(DEFAULT_FONT);
227 sprintf(buf, "%.*s", family->len, family->str);
228 name = wstrdup(buf);
230 pixelsize = strToInt(&tokens[6]);
231 size = strToInt(&tokens[7]);
233 if (size<=0 && pixelsize<=0) {
234 name = wstrappend(name, ":pixelsize=12");
235 } else if (pixelsize>0) {
236 /* if pixelsize is present size will be ignored so we skip it */
237 sprintf(buf, ":pixelsize=%d", pixelsize);
238 name = wstrappend(name, buf);
239 } else {
240 sprintf(buf, "-%d", size/10);
241 name = wstrappend(name, buf);
244 if (weight->len>0 && weight->str[0]!='*') {
245 sprintf(buf, ":weight=%.*s", weight->len, weight->str);
246 name = wstrappend(name, buf);
249 if (slant->len>0 && slant->str[0]!='*') {
250 sprintf(buf, ":slant=%s", mapSlantToName(slant));
251 name = wstrappend(name, buf);
254 name = wstrappend(name, ":xlfd=");
255 name = wstrappend(name, xlfd);
257 return name;
261 /* return converted font (if conversion is needed) else the original font */
262 static char*
263 convertFont(char *font)
265 if (font[0]=='-') {
266 if (!strchr(font, ',')) {
267 return xlfdToFc(font);
268 } else {
269 wwarning("fontsets are not supported. replaced "
270 "with default %s", DEFAULT_FONT);
271 return wstrdup(DEFAULT_FONT);
273 } else {
274 return font;
279 char*
280 defaultsPathForDomain(char *domain)
282 static char path[1024];
283 char *gspath;
285 gspath = getenv("GNUSTEP_USER_ROOT");
286 if (gspath) {
287 strcpy(path, gspath);
288 strcat(path, "/");
289 } else {
290 char *home;
292 home = getenv("HOME");
293 if (!home) {
294 printf("%s:could not get HOME environment variable!\n", ProgName);
295 exit(0);
298 strcpy(path, home);
299 strcat(path, "/GNUstep/");
301 strcat(path, DEFAULTS_DIR);
302 strcat(path, "/");
303 strcat(path, domain);
305 return path;
310 void
311 hackPathInTexture(WMPropList *texture, char *prefix)
313 WMPropList *type;
314 char *t;
316 /* get texture type */
317 type = WMGetFromPLArray(texture, 0);
318 t = WMGetFromPLString(type);
319 if (t == NULL)
320 return;
321 if (strcasecmp(t, "tpixmap")==0
322 || strcasecmp(t, "spixmap")==0
323 || strcasecmp(t, "mpixmap")==0
324 || strcasecmp(t, "cpixmap")==0
325 || strcasecmp(t, "tvgradient")==0
326 || strcasecmp(t, "thgradient")==0
327 || strcasecmp(t, "tdgradient")==0) {
328 WMPropList *file;
329 char buffer[4018];
331 /* get pixmap file path */
332 file = WMGetFromPLArray(texture, 1);
333 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
334 /* replace path with full path */
335 WMDeleteFromPLArray(texture, 1);
336 WMInsertInPLArray(texture, 1, WMCreatePLString(buffer));
337 } else if (strcasecmp(t, "bitmap") == 0) {
338 WMPropList *file;
339 char buffer[4018];
341 /* get bitmap file path */
342 file = WMGetFromPLArray(texture, 1);
343 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
344 /* replace path with full path */
345 WMDeleteFromPLArray(texture, 1);
346 WMInsertInPLArray(texture, 1, WMCreatePLString(buffer));
348 /* get mask file path */
349 file = WMGetFromPLArray(texture, 2);
350 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
351 /* replace path with full path */
352 WMDeleteFromPLArray(texture, 2);
353 WMInsertInPLArray(texture, 2, WMCreatePLString(buffer));
358 void
359 hackPaths(WMPropList *style, char *prefix)
361 WMPropList *keys;
362 WMPropList *key;
363 WMPropList *value;
364 int i;
367 keys = WMGetPLDictionaryKeys(style);
369 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
370 key = WMGetFromPLArray(keys, i);
372 value = WMGetFromPLDictionary(style, key);
373 if (!value)
374 continue;
376 if (strcasecmp(WMGetFromPLString(key), "WorkspaceSpecificBack")==0) {
377 if (WMIsPLArray(value)) {
378 int j;
379 WMPropList *texture;
381 for (j = 0; j < WMGetPropListItemCount(value); j++) {
382 texture = WMGetFromPLArray(value, j);
384 if (texture && WMIsPLArray(texture)
385 && WMGetPropListItemCount(texture) > 2) {
387 hackPathInTexture(texture, prefix);
391 } else {
393 if (WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
395 hackPathInTexture(value, prefix);
403 static WMPropList*
404 getColor(WMPropList *texture)
406 WMPropList *value, *type;
407 char *str;
409 type = WMGetFromPLArray(texture, 0);
410 if (!type)
411 return NULL;
413 value = NULL;
415 str = WMGetFromPLString(type);
416 if (strcasecmp(str, "solid")==0) {
417 value = WMGetFromPLArray(texture, 1);
418 } else if (strcasecmp(str, "dgradient")==0
419 || strcasecmp(str, "hgradient")==0
420 || strcasecmp(str, "vgradient")==0) {
421 WMPropList *c1, *c2;
422 int r1, g1, b1, r2, g2, b2;
423 char buffer[32];
425 c1 = WMGetFromPLArray(texture, 1);
426 c2 = WMGetFromPLArray(texture, 2);
427 if (!dpy) {
428 if (sscanf(WMGetFromPLString(c1), "#%2x%2x%2x", &r1, &g1, &b1)==3
429 && sscanf(WMGetFromPLString(c2), "#%2x%2x%2x", &r2, &g2, &b2)==3) {
430 sprintf(buffer, "#%02x%02x%02x", (r1+r2)/2, (g1+g2)/2,
431 (b1+b2)/2);
432 value = WMCreatePLString(buffer);
433 } else {
434 value = c1;
436 } else {
437 XColor color1;
438 XColor color2;
440 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
441 WMGetFromPLString(c1), &color1);
442 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
443 WMGetFromPLString(c2), &color2);
445 sprintf(buffer, "#%02x%02x%02x",
446 (color1.red+color2.red)>>9,
447 (color1.green+color2.green)>>9,
448 (color1.blue+color2.blue)>>9);
449 value = WMCreatePLString(buffer);
451 } else if (strcasecmp(str, "mdgradient")==0
452 || strcasecmp(str, "mhgradient")==0
453 || strcasecmp(str, "mvgradient")==0) {
455 value = WMGetFromPLArray(texture, 1);
457 } else if (strcasecmp(str, "tpixmap")==0
458 || strcasecmp(str, "cpixmap")==0
459 || strcasecmp(str, "spixmap")==0) {
461 value = WMGetFromPLArray(texture, 2);
464 return value;
469 * since some of the options introduce incompatibilities, we will need
470 * to do a kluge here or the themes ppl will get real annoying.
471 * So, treat for the absence of the following options:
472 * IconTitleColor
473 * IconTitleBack
475 void
476 hackStyle(WMPropList *style)
478 WMPropList *keys, *tmp;
479 int foundIconTitle = 0, foundResizebarBack = 0;
480 int i;
482 keys = WMGetPLDictionaryKeys(style);
484 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
485 char *str;
487 tmp = WMGetFromPLArray(keys, i);
488 str = WMGetFromPLString(tmp);
489 if (str) {
490 if (ignoreFonts && isFontOption(str)) {
491 WMRemoveFromPLDictionary(style, tmp);
492 continue;
494 if (ignoreCursors && isCursorOption(str)) {
495 WMRemoveFromPLDictionary(style, tmp);
496 continue;
498 if (isFontOption(str)) {
499 WMPropList *value;
500 char *newfont, *oldfont;
502 value = WMGetFromPLDictionary(style, tmp);
503 if (value) {
504 oldfont = WMGetFromPLString(value);
505 newfont = convertFont(oldfont);
506 if (newfont != oldfont) {
507 value = WMCreatePLString(newfont);
508 WMPutInPLDictionary(style, tmp, value);
509 WMReleasePropList(value);
510 wfree(newfont);
514 if (strcasecmp(str, "IconTitleColor")==0
515 || strcasecmp(str, "IconTitleBack")==0) {
516 foundIconTitle = 1;
517 } else if (strcasecmp(str, "ResizebarBack")==0) {
518 foundResizebarBack = 1;
523 if (!foundIconTitle) {
524 /* set the default values */
525 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleColor"));
526 if (tmp) {
527 WMPutInPLDictionary(style, WMCreatePLString("IconTitleColor"),
528 tmp);
531 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleBack"));
532 if (tmp) {
533 WMPropList *value;
535 value = getColor(tmp);
537 if (value) {
538 WMPutInPLDictionary(style, WMCreatePLString("IconTitleBack"),
539 value);
544 if (!foundResizebarBack) {
545 /* set the default values */
546 tmp = WMGetFromPLDictionary(style, WMCreatePLString("UTitleBack"));
547 if (tmp) {
548 WMPropList *value;
550 value = getColor(tmp);
552 if (value) {
553 WMPropList *t;
555 t = WMCreatePLArray(WMCreatePLString("solid"), value,
556 NULL);
557 WMPutInPLDictionary(style, WMCreatePLString("ResizebarBack"),
564 if (!WMGetFromPLDictionary(style, WMCreatePLString("MenuStyle"))) {
565 WMPutInPLDictionary(style, WMCreatePLString("MenuStyle"),
566 WMCreatePLString("normal"));
571 void
572 print_help()
574 printf("Usage: %s [OPTIONS] FILE\n", ProgName);
575 puts("Reads style/theme configuration from FILE and updates Window Maker.");
576 puts("");
577 puts(" --no-fonts ignore font related options");
578 puts(" --no-cursors ignore cursor related options");
579 puts(" --ignore <option> ignore changes in the specified option");
580 puts(" --help display this help and exit");
582 puts(" --format <format> specifies the format of the theme to be converted");
584 puts(" --version output version information and exit");
585 /*puts("");
586 puts("Supported formats: blackbox");*/
590 #define F_BLACKBOX 1
593 main(int argc, char **argv)
595 WMPropList *prop, *style;
596 char *path;
597 char *file = NULL;
598 struct stat statbuf;
599 int i;
600 int ignoreCount = 0;
601 char *ignoreList[MAX_OPTIONS];
603 dpy = XOpenDisplay("");
605 ProgName = argv[0];
607 if (argc<2) {
608 printf("%s: missing argument\n", ProgName);
609 printf("Try '%s --help' for more information\n", ProgName);
610 exit(1);
613 for (i = 1; i < argc; i++) {
614 if (strcmp("--ignore", argv[i])==0) {
615 i++;
616 if (i == argc) {
617 printf("%s: missing argument for option --ignore\n", ProgName);
618 exit(1);
620 ignoreList[ignoreCount++] = argv[i];
622 } else if (strcmp("--no-fonts", argv[i])==0) {
623 ignoreFonts = 1;
624 } else if (strcmp("--no-cursors", argv[i])==0) {
625 ignoreCursors = 1;
626 } else if (strcmp("--version", argv[i])==0) {
627 puts(PROG_VERSION);
628 exit(0);
629 } else if (strcmp("--help", argv[i])==0) {
630 print_help();
631 exit(0);
632 #if 0
633 } else if (strcmp("--format", argv[i])==0) {
634 i++;
635 if (i == argc) {
636 printf("%s: missing argument for option --format\n", ProgName);
637 exit(1);
639 if (strcasecmp(argv[i], "blackbox")==0) {
640 format = F_BLACKBOX;
641 } else {
642 printf("%s: unknown theme format '%s'\n", ProgName, argv[i]);
643 exit(1);
645 #endif
646 } else {
647 if (file) {
648 printf("%s: invalid argument '%s'\n", ProgName, argv[i]);
649 printf("Try '%s --help' for more information\n", ProgName);
650 exit(1);
652 file = argv[i];
656 WMPLSetCaseSensitive(False);
658 path = defaultsPathForDomain("WindowMaker");
660 prop = WMReadPropListFromFile(path);
661 if (!prop) {
662 perror(path);
663 printf("%s:could not load WindowMaker configuration file.\n",
664 ProgName);
665 exit(1);
668 if (stat(file, &statbuf) < 0) {
669 perror(file);
670 exit(1);
672 #if 0
673 if (format == F_BLACKBOX) {
674 style = readBlackBoxStyle(file);
675 if (!style) {
676 printf("%s: could not open style file\n", ProgName);
677 exit(1);
679 } else
680 #endif
682 if (S_ISDIR(statbuf.st_mode)) {
683 char buffer[4018];
684 char *prefix;
685 /* theme pack */
687 if (*argv[argc-1] != '/') {
688 if (!getcwd(buffer, 4000)) {
689 printf("%s: complete path for %s is too long\n", ProgName,
690 file);
691 exit(1);
693 if (strlen(buffer) + strlen(file) > 4000) {
694 printf("%s: complete path for %s is too long\n", ProgName,
695 file);
696 exit(1);
698 strcat(buffer, "/");
699 } else {
700 buffer[0] = 0;
702 strcat(buffer, file);
704 prefix = malloc(strlen(buffer)+10);
705 if (!prefix) {
706 printf("%s: out of memory\n", ProgName);
707 exit(1);
709 strcpy(prefix, buffer);
711 strcat(buffer, "/style");
713 style = WMReadPropListFromFile(buffer);
714 if (!style) {
715 perror(buffer);
716 printf("%s:could not load style file.\n", ProgName);
717 exit(1);
720 hackPaths(style, prefix);
721 free(prefix);
722 } else {
723 /* normal style file */
725 style = WMReadPropListFromFile(file);
726 if (!style) {
727 perror(file);
728 printf("%s:could not load style file.\n", ProgName);
729 exit(1);
734 if (!WMIsPLDictionary(style)) {
735 printf("%s: '%s' is not a style file/theme\n", ProgName, file);
736 exit(1);
739 hackStyle(style);
741 if (ignoreCount > 0) {
742 for (i = 0; i < ignoreCount; i++) {
743 WMRemoveFromPLDictionary(style, WMCreatePLString(ignoreList[i]));
747 WMMergePLDictionaries(prop, style, True);
749 WMWritePropListToFile(prop, path, True);
751 XEvent ev;
753 if (dpy) {
754 int i;
755 char *msg = "Reconfigure";
757 memset(&ev, 0, sizeof(XEvent));
759 ev.xclient.type = ClientMessage;
760 ev.xclient.message_type = XInternAtom(dpy, "_WINDOWMAKER_COMMAND",
761 False);
762 ev.xclient.window = DefaultRootWindow(dpy);
763 ev.xclient.format = 8;
765 for (i = 0; i <= strlen(msg); i++) {
766 ev.xclient.data.b[i] = msg[i];
768 XSendEvent(dpy, DefaultRootWindow(dpy), False,
769 SubstructureRedirectMask, &ev);
770 XFlush(dpy);
774 exit(0);
778 #if 0
779 char*
780 getToken(char *str, int i, char *buf)
786 static WMPropList*
787 readBlackBoxStyle(char *path)
789 FILE *f;
790 char buffer[128], char token[128];
791 WMPropList *style, *p;
793 f = fopen(path, "rb");
794 if (!f) {
795 perror(path);
796 return NULL;
799 while (1) {
800 if (!fgets(buffer, 127, f))
801 break;
803 if (strncasecmp(buffer, "menu.title:", 11)==0) {
808 #endif