To ease transition from XLFD fontnames to fontconfig fontnames setstyle
[wmaker-crm.git] / util / setstyle.c
blob41d734de6637128fcc3b6f995fd3e5acf7d40d1f
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 *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);
83 char*
84 capitalize(char *element)
86 unsigned int first = 1;
87 char *p;
88 char *b;
89 b = element;
90 for (p = b; *p != 0; p++) {
91 if (isalpha(*p) && first) {
92 first = 0;
93 *p = toupper(*p);
94 } else if (*p == '-' || *p == ' ') {
95 first = 1;
98 return b;
101 char*
102 getElementFromXLFD(const char *xlfd, int index)
104 const char *p = xlfd;
105 while (*p != 0) {
106 if (*p == '-' && --index == 0) {
107 const char *end = strchr(p + 1, '-');
108 char *buf;
109 size_t len;
110 if (end == 0) end = p + strlen(p);
111 len = end - (p + 1);
112 buf = wmalloc(len);
113 memcpy(buf, p + 1, len);
114 buf[len] = 0;
115 return buf;
117 p++;
119 return "*";
122 char*
123 xlfdToFc(char *xlfd)
125 char *Fcname = NULL;
127 char *family = getElementFromXLFD(xlfd, 2);
128 char *size = getElementFromXLFD(xlfd, 7);
129 char *weight = getElementFromXLFD(xlfd, 3);
130 char *slant = getElementFromXLFD(xlfd, 4);
132 if (strcmp(family, "*") != 0) {
133 Fcname = wstrconcat(Fcname, capitalize(family));
135 if (strcmp(size, "*") != 0) {
136 Fcname = wstrconcat(Fcname, ":pixelsize=");
137 Fcname = wstrconcat(Fcname, size);
139 if (strcmp(weight, "*") != 0) {
140 Fcname = wstrconcat(Fcname, ":style=");
141 Fcname = wstrconcat(Fcname, capitalize(weight));
143 if (strcmp(slant, "*") != 0) {
144 if (strcmp(slant, "i") == 0) {
145 Fcname = wstrconcat(Fcname, ":slant=");
146 Fcname = wstrconcat(Fcname, "Italic");
147 } else if (strcmp(slant, "o") == 0) {
148 Fcname = wstrconcat(Fcname, ":slant=");
149 Fcname = wstrconcat(Fcname, "Oblique");
150 } else if (strcmp(slant, "ri") == 0) {
151 Fcname = wstrconcat(Fcname, ":slant=");
152 Fcname = wstrconcat(Fcname, "Rev Italic");
153 } else if (strcmp(slant, "ro") == 0) {
154 Fcname = wstrconcat(Fcname, ":slant=");
155 Fcname = wstrconcat(Fcname, "Rev Oblique");
158 if (!Fcname)
159 Fcname = wstrdup(default_font);
161 wfree(family);
162 wfree(size);
163 wfree(weight);
164 wfree(slant);
166 return Fcname;
169 char*
170 defaultsPathForDomain(char *domain)
172 static char path[1024];
173 char *gspath;
175 gspath = getenv("GNUSTEP_USER_ROOT");
176 if (gspath) {
177 strcpy(path, gspath);
178 strcat(path, "/");
179 } else {
180 char *home;
182 home = getenv("HOME");
183 if (!home) {
184 printf("%s:could not get HOME environment variable!\n", ProgName);
185 exit(0);
188 strcpy(path, home);
189 strcat(path, "/GNUstep/");
191 strcat(path, DEFAULTS_DIR);
192 strcat(path, "/");
193 strcat(path, domain);
195 return path;
200 void
201 hackPathInTexture(WMPropList *texture, char *prefix)
203 WMPropList *type;
204 char *t;
206 /* get texture type */
207 type = WMGetFromPLArray(texture, 0);
208 t = WMGetFromPLString(type);
209 if (t == NULL)
210 return;
211 if (strcasecmp(t, "tpixmap")==0
212 || strcasecmp(t, "spixmap")==0
213 || strcasecmp(t, "mpixmap")==0
214 || strcasecmp(t, "cpixmap")==0
215 || strcasecmp(t, "tvgradient")==0
216 || strcasecmp(t, "thgradient")==0
217 || strcasecmp(t, "tdgradient")==0) {
218 WMPropList *file;
219 char buffer[4018];
221 /* get pixmap file path */
222 file = WMGetFromPLArray(texture, 1);
223 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
224 /* replace path with full path */
225 WMDeleteFromPLArray(texture, 1);
226 WMInsertInPLArray(texture, 1, WMCreatePLString(buffer));
227 } else if (strcasecmp(t, "bitmap") == 0) {
228 WMPropList *file;
229 char buffer[4018];
231 /* get bitmap file path */
232 file = WMGetFromPLArray(texture, 1);
233 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
234 /* replace path with full path */
235 WMDeleteFromPLArray(texture, 1);
236 WMInsertInPLArray(texture, 1, WMCreatePLString(buffer));
238 /* get mask file path */
239 file = WMGetFromPLArray(texture, 2);
240 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
241 /* replace path with full path */
242 WMDeleteFromPLArray(texture, 2);
243 WMInsertInPLArray(texture, 2, WMCreatePLString(buffer));
248 void
249 hackPaths(WMPropList *style, char *prefix)
251 WMPropList *keys;
252 WMPropList *key;
253 WMPropList *value;
254 int i;
257 keys = WMGetPLDictionaryKeys(style);
259 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
260 key = WMGetFromPLArray(keys, i);
262 value = WMGetFromPLDictionary(style, key);
263 if (!value)
264 continue;
266 if (strcasecmp(WMGetFromPLString(key), "WorkspaceSpecificBack")==0) {
267 if (WMIsPLArray(value)) {
268 int j;
269 WMPropList *texture;
271 for (j = 0; j < WMGetPropListItemCount(value); j++) {
272 texture = WMGetFromPLArray(value, j);
274 if (texture && WMIsPLArray(texture)
275 && WMGetPropListItemCount(texture) > 2) {
277 hackPathInTexture(texture, prefix);
281 } else {
283 if (WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
285 hackPathInTexture(value, prefix);
293 static WMPropList*
294 getColor(WMPropList *texture)
296 WMPropList *value, *type;
297 char *str;
299 type = WMGetFromPLArray(texture, 0);
300 if (!type)
301 return NULL;
303 value = NULL;
305 str = WMGetFromPLString(type);
306 if (strcasecmp(str, "solid")==0) {
307 value = WMGetFromPLArray(texture, 1);
308 } else if (strcasecmp(str, "dgradient")==0
309 || strcasecmp(str, "hgradient")==0
310 || strcasecmp(str, "vgradient")==0) {
311 WMPropList *c1, *c2;
312 int r1, g1, b1, r2, g2, b2;
313 char buffer[32];
315 c1 = WMGetFromPLArray(texture, 1);
316 c2 = WMGetFromPLArray(texture, 2);
317 if (!dpy) {
318 if (sscanf(WMGetFromPLString(c1), "#%2x%2x%2x", &r1, &g1, &b1)==3
319 && sscanf(WMGetFromPLString(c2), "#%2x%2x%2x", &r2, &g2, &b2)==3) {
320 sprintf(buffer, "#%02x%02x%02x", (r1+r2)/2, (g1+g2)/2,
321 (b1+b2)/2);
322 value = WMCreatePLString(buffer);
323 } else {
324 value = c1;
326 } else {
327 XColor color1;
328 XColor color2;
330 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
331 WMGetFromPLString(c1), &color1);
332 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
333 WMGetFromPLString(c2), &color2);
335 sprintf(buffer, "#%02x%02x%02x",
336 (color1.red+color2.red)>>9,
337 (color1.green+color2.green)>>9,
338 (color1.blue+color2.blue)>>9);
339 value = WMCreatePLString(buffer);
341 } else if (strcasecmp(str, "mdgradient")==0
342 || strcasecmp(str, "mhgradient")==0
343 || strcasecmp(str, "mvgradient")==0) {
345 value = WMGetFromPLArray(texture, 1);
347 } else if (strcasecmp(str, "tpixmap")==0
348 || strcasecmp(str, "cpixmap")==0
349 || strcasecmp(str, "spixmap")==0) {
351 value = WMGetFromPLArray(texture, 2);
354 return value;
359 * since some of the options introduce incompatibilities, we will need
360 * to do a kluge here or the themes ppl will get real annoying.
361 * So, treat for the absence of the following options:
362 * IconTitleColor
363 * IconTitleBack
365 void
366 hackStyle(WMPropList *style)
368 WMPropList *keys, *tmp;
369 int foundIconTitle = 0, foundResizebarBack = 0;
370 int i;
372 keys = WMGetPLDictionaryKeys(style);
374 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
375 char *str;
377 tmp = WMGetFromPLArray(keys, i);
378 str = WMGetFromPLString(tmp);
379 if (str) {
380 int j, found;
382 if (ignoreFonts) {
383 for (j = 0, found = 0; FontOptions[j]!=NULL; j++) {
384 if (strcasecmp(str, FontOptions[j])==0) {
385 WMRemoveFromPLDictionary(style, tmp);
386 found = 1;
387 break;
390 if (found)
391 continue;
393 if (ignoreCursors) {
394 for (j = 0, found = 0; CursorOptions[j] != NULL; j++) {
395 if (strcasecmp(str, CursorOptions[j]) == 0) {
396 WMRemoveFromPLDictionary(style, tmp);
397 found = 1;
398 break;
401 if (found)
402 continue;
404 for (j = 0; FontOptions[j]!=NULL; j++) {
405 if (strcasecmp(str, FontOptions[j]) == 0) {
406 char *oldfont;
407 WMPropList *value;
408 value = WMGetFromPLDictionary(style, tmp);
409 oldfont = WMGetFromPLString(value);
410 if (oldfont[0] == '-') {
411 if (!strchr(oldfont, ',')) {
412 char *newfont;
413 newfont = xlfdToFc(oldfont);
414 WMPutInPLDictionary(style, tmp, WMCreatePLString(newfont));
415 break;
416 } else {
417 wwarning("fontsets are not supported. replaced with default: %s", default_font);
418 WMPutInPLDictionary(style, tmp,
419 WMCreatePLString(default_font));
420 break;
422 } else {
423 break;
428 if (strcasecmp(str, "IconTitleColor")==0
429 || strcasecmp(str, "IconTitleBack")==0) {
430 foundIconTitle = 1;
431 } else if (strcasecmp(str, "ResizebarBack")==0) {
432 foundResizebarBack = 1;
437 if (!foundIconTitle) {
438 /* set the default values */
439 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleColor"));
440 if (tmp) {
441 WMPutInPLDictionary(style, WMCreatePLString("IconTitleColor"),
442 tmp);
445 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleBack"));
446 if (tmp) {
447 WMPropList *value;
449 value = getColor(tmp);
451 if (value) {
452 WMPutInPLDictionary(style, WMCreatePLString("IconTitleBack"),
453 value);
458 if (!foundResizebarBack) {
459 /* set the default values */
460 tmp = WMGetFromPLDictionary(style, WMCreatePLString("UTitleBack"));
461 if (tmp) {
462 WMPropList *value;
464 value = getColor(tmp);
466 if (value) {
467 WMPropList *t;
469 t = WMCreatePLArray(WMCreatePLString("solid"), value,
470 NULL);
471 WMPutInPLDictionary(style, WMCreatePLString("ResizebarBack"),
478 if (!WMGetFromPLDictionary(style, WMCreatePLString("MenuStyle"))) {
479 WMPutInPLDictionary(style, WMCreatePLString("MenuStyle"),
480 WMCreatePLString("normal"));
485 void
486 print_help()
488 printf("Usage: %s [OPTIONS] FILE\n", ProgName);
489 puts("Reads style/theme configuration from FILE and updates Window Maker.");
490 puts("");
491 puts(" --no-fonts ignore font related options");
492 puts(" --no-cursors ignore cursor related options");
493 puts(" --ignore <option> ignore changes in the specified option");
494 puts(" --help display this help and exit");
496 puts(" --format <format> specifies the format of the theme to be converted");
498 puts(" --version output version information and exit");
499 /*puts("");
500 puts("Supported formats: blackbox");*/
504 #define F_BLACKBOX 1
506 int
507 main(int argc, char **argv)
509 WMPropList *prop, *style;
510 char *path;
511 char *file = NULL;
512 struct stat statbuf;
513 int i;
514 int ignoreCount = 0;
515 char *ignoreList[MAX_OPTIONS];
517 dpy = XOpenDisplay("");
519 ProgName = argv[0];
521 if (argc<2) {
522 printf("%s: missing argument\n", ProgName);
523 printf("Try '%s --help' for more information\n", ProgName);
524 exit(1);
527 for (i = 1; i < argc; i++) {
528 if (strcmp("--ignore", argv[i])==0) {
529 i++;
530 if (i == argc) {
531 printf("%s: missing argument for option --ignore\n", ProgName);
532 exit(1);
534 ignoreList[ignoreCount++] = argv[i];
536 } else if (strcmp("--no-fonts", argv[i])==0) {
537 ignoreFonts = 1;
538 } else if (strcmp("--no-cursors", argv[i])==0) {
539 ignoreCursors = 1;
540 } else if (strcmp("--version", argv[i])==0) {
541 puts(PROG_VERSION);
542 exit(0);
543 } else if (strcmp("--help", argv[i])==0) {
544 print_help();
545 exit(0);
546 #if 0
547 } else if (strcmp("--format", argv[i])==0) {
548 i++;
549 if (i == argc) {
550 printf("%s: missing argument for option --format\n", ProgName);
551 exit(1);
553 if (strcasecmp(argv[i], "blackbox")==0) {
554 format = F_BLACKBOX;
555 } else {
556 printf("%s: unknown theme format '%s'\n", ProgName, argv[i]);
557 exit(1);
559 #endif
560 } else {
561 if (file) {
562 printf("%s: invalid argument '%s'\n", ProgName, argv[i]);
563 printf("Try '%s --help' for more information\n", ProgName);
564 exit(1);
566 file = argv[i];
570 WMPLSetCaseSensitive(False);
572 path = defaultsPathForDomain("WindowMaker");
574 prop = WMReadPropListFromFile(path);
575 if (!prop) {
576 perror(path);
577 printf("%s:could not load WindowMaker configuration file.\n",
578 ProgName);
579 exit(1);
582 if (stat(file, &statbuf) < 0) {
583 perror(file);
584 exit(1);
586 #if 0
587 if (format == F_BLACKBOX) {
588 style = readBlackBoxStyle(file);
589 if (!style) {
590 printf("%s: could not open style file\n", ProgName);
591 exit(1);
593 } else
594 #endif
596 if (S_ISDIR(statbuf.st_mode)) {
597 char buffer[4018];
598 char *prefix;
599 /* theme pack */
601 if (*argv[argc-1] != '/') {
602 if (!getcwd(buffer, 4000)) {
603 printf("%s: complete path for %s is too long\n", ProgName,
604 file);
605 exit(1);
607 if (strlen(buffer) + strlen(file) > 4000) {
608 printf("%s: complete path for %s is too long\n", ProgName,
609 file);
610 exit(1);
612 strcat(buffer, "/");
613 } else {
614 buffer[0] = 0;
616 strcat(buffer, file);
618 prefix = malloc(strlen(buffer)+10);
619 if (!prefix) {
620 printf("%s: out of memory\n", ProgName);
621 exit(1);
623 strcpy(prefix, buffer);
625 strcat(buffer, "/style");
627 style = WMReadPropListFromFile(buffer);
628 if (!style) {
629 perror(buffer);
630 printf("%s:could not load style file.\n", ProgName);
631 exit(1);
634 hackPaths(style, prefix);
635 free(prefix);
636 } else {
637 /* normal style file */
639 style = WMReadPropListFromFile(file);
640 if (!style) {
641 perror(file);
642 printf("%s:could not load style file.\n", ProgName);
643 exit(1);
648 if (!WMIsPLDictionary(style)) {
649 printf("%s: '%s' is not a style file/theme\n", ProgName, file);
650 exit(1);
653 hackStyle(style);
655 if (ignoreCount > 0) {
656 for (i = 0; i < ignoreCount; i++) {
657 WMRemoveFromPLDictionary(style, WMCreatePLString(ignoreList[i]));
661 WMMergePLDictionaries(prop, style, True);
663 WMWritePropListToFile(prop, path, True);
665 XEvent ev;
667 if (dpy) {
668 int i;
669 char *msg = "Reconfigure";
671 memset(&ev, 0, sizeof(XEvent));
673 ev.xclient.type = ClientMessage;
674 ev.xclient.message_type = XInternAtom(dpy, "_WINDOWMAKER_COMMAND",
675 False);
676 ev.xclient.window = DefaultRootWindow(dpy);
677 ev.xclient.format = 8;
679 for (i = 0; i <= strlen(msg); i++) {
680 ev.xclient.data.b[i] = msg[i];
682 XSendEvent(dpy, DefaultRootWindow(dpy), False,
683 SubstructureRedirectMask, &ev);
684 XFlush(dpy);
688 exit(0);
692 #if 0
693 char*
694 getToken(char *str, int i, char *buf)
700 static WMPropList*
701 readBlackBoxStyle(char *path)
703 FILE *f;
704 char buffer[128], char token[128];
705 WMPropList *style, *p;
707 f = fopen(path, "rb");
708 if (!f) {
709 perror(path);
710 return NULL;
713 while (1) {
714 if (!fgets(buffer, 127, f))
715 break;
717 if (strncasecmp(buffer, "menu.title:", 11)==0) {
722 #endif