style property is supposed to override weight and slant, unfortunately it doesn't...
[wmaker-crm.git] / util / getstyle.c
blob061a20655ec27ab72800aa1d01bbc35a3fe78300
1 /* getstyle.c - outputs style related options from WindowMaker to stdout
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 "getstyle (Window Maker) 0.6"
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <pwd.h>
34 #include <limits.h>
35 #include <assert.h>
36 #include <WINGs/WUtil.h>
38 #ifndef PATH_MAX
39 #define PATH_MAX 1024
40 #endif
42 #include "../src/wconfig.h"
44 /* table of style related options */
45 static char *options[] = {
46 "TitleJustify",
47 "ClipTitleFont",
48 "WindowTitleFont",
49 "MenuTitleFont",
50 "MenuTextFont",
51 "IconTitleFont",
52 "DisplayFont",
53 "LargeDisplayFont",
54 "WindowTitleExtendSpace",
55 "MenuTitleExtendSpace",
56 "MenuTextExtendSpace",
57 "HighlightColor",
58 "HighlightTextColor",
59 "ClipTitleColor",
60 "CClipTitleColor",
61 "FTitleColor",
62 "PTitleColor",
63 "UTitleColor",
64 "FTitleBack",
65 "PTitleBack",
66 "UTitleBack",
67 "ResizebarBack",
68 "MenuTitleColor",
69 "MenuTextColor",
70 "MenuDisabledColor",
71 "MenuTitleBack",
72 "MenuTextBack",
73 "IconBack",
74 "IconTitleColor",
75 "IconTitleBack",
76 "MenuStyle",
77 "WindowTitleExtendSpace",
78 "MenuTitleExtendSpace",
79 "MenuTextExtendSpace",
80 NULL
84 /* table of theme related options */
85 static char *theme_options[] = {
86 "WorkspaceBack",
87 "NormalCursor",
88 "ArrowCursor",
89 "MoveCursor",
90 "ResizeCursor",
91 "TopLeftResizeCursor",
92 "TopRightResizeCursor",
93 "BottomLeftResizeCursor",
94 "BottomRightResizeCursor",
95 "VerticalResizeCursor",
96 "HorizontalResizeCursor",
97 "WaitCursor",
98 "QuestionCursor",
99 "TextCursor",
100 "SelectCursor",
101 NULL
105 /* table of style related fonts */
107 static char *font_styles[] = {
108 "ClipTitleFont",
109 "WindowTitleFont",
110 "MenuTitleFont",
111 "MenuTextFont",
112 "IconTitleFont",
113 "DisplayFont",
114 "LargeDisplayFont",
115 NULL
118 char *default_font = "sans:pixelsize=12";
120 char *ProgName;
122 WMPropList *PixmapPath = NULL;
124 char *ThemePath = NULL;
127 void
128 print_help()
130 printf("Usage: %s [OPTIONS] [FILE]\n", ProgName);
131 puts("Retrieves style/theme configuration and output to FILE or to stdout");
132 puts("");
133 puts(" -t, --theme-options output theme related options when producing a style file");
134 puts(" -p, --pack produce output as a theme pack");
135 puts(" --help display this help and exit");
136 puts(" --version output version information and exit");
140 char*
141 globalDefaultsPathForDomain(char *domain)
143 static char path[1024];
145 sprintf(path, "%s/WindowMaker/%s", SYSCONFDIR, domain);
147 return path;
151 char*
152 defaultsPathForDomain(char *domain)
154 static char path[1024];
155 char *gspath;
157 gspath = getenv("GNUSTEP_USER_ROOT");
158 if (gspath) {
159 strcpy(path, gspath);
160 strcat(path, "/");
161 } else {
162 char *home;
164 home = getenv("HOME");
165 if (!home) {
166 printf("%s:could not get HOME environment variable!\n", ProgName);
167 exit(0);
169 strcpy(path, home);
170 strcat(path, "/GNUstep/");
172 strcat(path, DEFAULTS_DIR);
173 strcat(path, "/");
174 strcat(path, domain);
176 return path;
180 void
181 abortar(char *reason)
183 char buffer[4000];
185 printf("%s: %s\n", ProgName, reason);
187 if (ThemePath) {
188 printf("Removing unfinished theme pack\n");
189 sprintf(buffer, "/bin/rm -fr \"%s\"", ThemePath);
191 if (system(buffer)!=0) {
192 printf("%s: could not execute command %s\n", ProgName, buffer);
195 exit(1);
201 char*
202 wgethomedir()
204 char *home = getenv("HOME");
205 struct passwd *user;
207 if (home)
208 return home;
210 user = getpwuid(getuid());
211 if (!user) {
212 char buffer[80];
214 sprintf(buffer, "could not get password entry for UID %i", getuid());
215 perror(buffer);
216 return "/";
218 if (!user->pw_dir) {
219 return "/";
220 } else {
221 return user->pw_dir;
226 void*
227 mymalloc(int size)
229 void *tmp;
231 tmp = malloc(size);
232 if (!tmp) {
233 abortar("out of memory");
236 return tmp;
240 char*
241 mystrdup(char *str)
243 char *tmp;
245 tmp = mymalloc(strlen(str)+1);
247 strcpy(tmp, str);
249 return tmp;
253 static char*
254 getuserhomedir(char *username)
256 struct passwd *user;
258 user = getpwnam(username);
259 if (!user) {
260 char buffer[100];
262 sprintf(buffer,"could not get password entry for user %s", username);
263 perror(buffer);
264 return NULL;
266 if (!user->pw_dir) {
267 return "/";
268 } else {
269 return user->pw_dir;
276 char*
277 wexpandpath(char *path)
279 char buffer2[PATH_MAX+2];
280 char buffer[PATH_MAX+2];
281 int i;
283 memset(buffer, 0, PATH_MAX+2);
285 if (*path=='~') {
286 char *home;
288 path++;
289 if (*path=='/' || *path==0) {
290 home = wgethomedir();
291 strcat(buffer, home);
292 } else {
293 int j;
294 j = 0;
295 while (*path!=0 && *path!='/') {
296 buffer2[j++] = *path;
297 buffer2[j] = 0;
298 path++;
300 home = getuserhomedir(buffer2);
301 if (!home)
302 return NULL;
303 strcat(buffer, home);
307 i = strlen(buffer);
309 while (*path!=0) {
310 char *tmp;
312 if (*path=='$') {
313 int j = 0;
314 path++;
315 /* expand $(HOME) or $HOME style environment variables */
316 if (*path=='(') {
317 path++;
318 while (*path!=0 && *path!=')') {
319 buffer2[j++] = *(path++);
320 buffer2[j] = 0;
322 if (*path==')')
323 path++;
324 tmp = getenv(buffer2);
325 if (!tmp) {
326 buffer[i] = 0;
327 strcat(buffer, "$(");
328 strcat(buffer, buffer2);
329 strcat(buffer, ")");
330 i += strlen(buffer2)+3;
331 } else {
332 strcat(buffer, tmp);
333 i += strlen(tmp);
335 } else {
336 while (*path!=0 && *path!='/') {
337 buffer2[j++] = *(path++);
338 buffer2[j] = 0;
340 tmp = getenv(buffer2);
341 if (!tmp) {
342 strcat(buffer, "$");
343 strcat(buffer, buffer2);
344 i += strlen(buffer2)+1;
345 } else {
346 strcat(buffer, tmp);
347 i += strlen(tmp);
350 } else {
351 buffer[i++] = *path;
352 path++;
356 return mystrdup(buffer);
361 char*
362 wfindfileinarray(WMPropList *paths, char *file)
364 int i;
365 char *path;
366 int len, flen;
367 char *fullpath;
369 if (!file)
370 return NULL;
372 if (*file=='/' || *file=='~' || !paths || !WMIsPLArray(paths)
373 || WMGetPropListItemCount(paths)==0) {
374 if (access(file, R_OK)<0) {
375 fullpath = wexpandpath(file);
376 if (!fullpath)
377 return NULL;
379 if (access(fullpath, R_OK)<0) {
380 free(fullpath);
381 return NULL;
382 } else {
383 return fullpath;
385 } else {
386 return mystrdup(file);
390 flen = strlen(file);
391 for (i=0; i < WMGetPropListItemCount(paths); i++) {
392 WMPropList *tmp;
393 char *dir;
395 tmp = WMGetFromPLArray(paths, i);
396 if (!WMIsPLString(tmp) || !(dir = WMGetFromPLString(tmp)))
397 continue;
399 len = strlen(dir);
400 path = mymalloc(len+flen+2);
401 path = memcpy(path, dir, len);
402 path[len]=0;
403 strcat(path, "/");
404 strcat(path, file);
405 /* expand tilde */
406 fullpath = wexpandpath(path);
407 free(path);
408 if (fullpath) {
409 /* check if file is readable */
410 if (access(fullpath, R_OK)==0) {
411 return fullpath;
413 free(fullpath);
416 return NULL;
419 char*
420 capitalize(char *element)
422 unsigned int first = 1;
423 char *p;
424 char *b;
425 b = element;
426 for (p = b; *p != 0; p++)
428 if (isalpha(*p) && first) {
429 first = 0;
430 *p = toupper(*p);
432 else if (*p == '-' || *p == ' ') {
433 first = 1;
436 return b;
439 char*
440 getElementFromXLFD(const char *xlfd, int index)
442 const char *p = xlfd;
443 while (*p != 0) {
444 if (*p == '-' && --index == 0) {
445 const char *end = strchr(p + 1, '-');
446 char *buf;
447 size_t len;
448 if (end == 0) end = p + strlen(p);
449 len = end - (p + 1);
450 buf = wmalloc(len);
451 memcpy(buf, p + 1, len);
452 buf[len] = 0;
453 return buf;
455 p++;
457 return "*";
460 char*
461 xlfdToFc(char *xlfd)
463 char *Fcname = NULL;
465 char *family = getElementFromXLFD(xlfd, 2);
466 char *size = getElementFromXLFD(xlfd, 7);
467 char *weight = getElementFromXLFD(xlfd, 3);
468 char *slant = getElementFromXLFD(xlfd, 4);
470 if (strcmp(family, "*") != 0) {
471 Fcname = wstrconcat(Fcname, capitalize(family));
473 if (strcmp(size, "*") != 0) {
474 Fcname = wstrconcat(Fcname, ":pixelsize=");
475 Fcname = wstrconcat(Fcname, size);
477 if (strcmp(weight, "*") != 0) {
478 Fcname = wstrconcat(Fcname, ":weight=");
479 Fcname = wstrconcat(Fcname, capitalize(weight));
481 if (strcmp(slant, "*") != 0) {
482 if (strcmp(slant, "i") == 0) {
483 Fcname = wstrconcat(Fcname, ":slant=");
484 Fcname = wstrconcat(Fcname, "Italic");
485 } else if (strcmp(slant, "o") == 0) {
486 Fcname = wstrconcat(Fcname, ":slant=");
487 Fcname = wstrconcat(Fcname, "Oblique");
488 } else if (strcmp(slant, "ri") == 0) {
489 Fcname = wstrconcat(Fcname, ":slant=");
490 Fcname = wstrconcat(Fcname, "Rev Italic");
491 } else if (strcmp(slant, "ro") == 0) {
492 Fcname = wstrconcat(Fcname, ":slant=");
493 Fcname = wstrconcat(Fcname, "Rev Oblique");
496 if (!Fcname)
497 Fcname = wstrdup(default_font);
499 wfree(family);
500 wfree(size);
501 wfree(weight);
502 wfree(slant);
504 return Fcname;
507 void
508 copyFile(char *dir, char *file)
510 char buffer[4000];
512 sprintf(buffer, "/bin/cp \"%s\" \"%s\"", file, dir);
513 if (system(buffer)!=0) {
514 printf("%s: could not copy file %s\n", ProgName, file);
519 void
520 findCopyFile(char *dir, char *file)
522 char *fullPath;
524 fullPath = wfindfileinarray(PixmapPath, file);
525 if (!fullPath) {
526 char buffer[4000];
528 sprintf(buffer, "coould not find file %s", file);
529 abortar(buffer);
531 copyFile(dir, fullPath);
532 free(fullPath);
536 char*
537 makeThemePack(WMPropList *style, char *themeName)
539 WMPropList *keys;
540 WMPropList *key;
541 WMPropList *value;
542 int i;
543 char *themeDir;
545 themeDir = mymalloc(strlen(themeName)+50);
546 sprintf(themeDir, "%s.themed", themeName);
547 ThemePath = themeDir;
549 char *tmp;
551 tmp = mymalloc(strlen(themeDir)+20);
552 sprintf(tmp, "/bin/mkdir \"%s\"", themeDir);
553 if (system(tmp)!=0) {
554 printf("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n", ProgName, themeDir);
555 exit(1);
557 free(tmp);
559 keys = WMGetPLDictionaryKeys(style);
561 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
562 key = WMGetFromPLArray(keys, i);
564 value = WMGetFromPLDictionary(style, key);
565 if (value && WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
566 WMPropList *type;
567 char *t;
569 type = WMGetFromPLArray(value, 0);
570 t = WMGetFromPLString(type);
571 if (t == NULL)
572 continue;
574 if (strcasecmp(t, "tpixmap")==0
575 || strcasecmp(t, "spixmap")==0
576 || strcasecmp(t, "cpixmap")==0
577 || strcasecmp(t, "mpixmap")==0
578 || strcasecmp(t, "tdgradient")==0
579 || strcasecmp(t, "tvgradient")==0
580 || strcasecmp(t, "thgradient")==0) {
581 WMPropList *file;
582 char *p;
583 char *newPath;
585 file = WMGetFromPLArray(value, 1);
587 p = strrchr(WMGetFromPLString(file), '/');
588 if (p) {
589 copyFile(themeDir, WMGetFromPLString(file));
591 newPath = mystrdup(p+1);
592 WMDeleteFromPLArray(value, 1);
593 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
594 free(newPath);
595 } else {
596 findCopyFile(themeDir, WMGetFromPLString(file));
598 } else if (strcasecmp(t, "bitmap")==0) {
599 WMPropList *file;
600 char *p;
601 char *newPath;
603 file = WMGetFromPLArray(value, 1);
605 p = strrchr(WMGetFromPLString(file), '/');
606 if (p) {
607 copyFile(themeDir, WMGetFromPLString(file));
609 newPath = mystrdup(p+1);
610 WMDeleteFromPLArray(value, 1);
611 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
612 free(newPath);
613 } else {
614 findCopyFile(themeDir, WMGetFromPLString(file));
618 file = WMGetFromPLArray(value, 2);
620 p = strrchr(WMGetFromPLString(file), '/');
621 if (p) {
622 copyFile(themeDir, WMGetFromPLString(file));
624 newPath = mystrdup(p+1);
625 WMDeleteFromPLArray(value, 2);
626 WMInsertInPLArray(value, 2, WMCreatePLString(newPath));
627 free(newPath);
628 } else {
629 findCopyFile(themeDir, WMGetFromPLString(file));
635 return themeDir;
639 int
640 main(int argc, char **argv)
642 WMPropList *prop, *style, *key, *val;
643 char *path;
644 int i, theme_too=0;
645 int make_pack = 0;
646 char *style_file = NULL;
649 ProgName = argv[0];
651 if (argc>1) {
652 for (i=1; i<argc; i++) {
653 if (strcmp(argv[i], "-p")==0
654 || strcmp(argv[i], "--pack")==0) {
655 make_pack = 1;
656 theme_too = 1;
657 } else if (strcmp(argv[i], "-t")==0
658 || strcmp(argv[i], "--theme-options")==0) {
659 theme_too++;
660 } else if (strcmp(argv[i], "--help")==0) {
661 print_help();
662 exit(0);
663 } else if (strcmp(argv[i], "--version")==0) {
664 puts(PROG_VERSION);
665 exit(0);
666 } else {
667 if (style_file!=NULL) {
668 printf("%s: invalid argument '%s'\n", argv[0],
669 style_file[0]=='-' ? style_file : argv[i]);
670 printf("Try '%s --help' for more information\n", argv[0]);
671 exit(1);
673 style_file = argv[i];
678 if (make_pack && !style_file) {
679 printf("%s: you must supply a name for the theme pack\n", ProgName);
680 exit(1);
683 WMPLSetCaseSensitive(False);
685 path = defaultsPathForDomain("WindowMaker");
687 prop = WMReadPropListFromFile(path);
688 if (!prop) {
689 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
690 ProgName, path);
691 exit(1);
694 /* get global value */
695 path = globalDefaultsPathForDomain("WindowMaker");
696 val = WMReadPropListFromFile(path);
697 if (val) {
698 WMMergePLDictionaries(val, prop, True);
699 WMReleasePropList(prop);
700 prop = val;
703 style = WMCreatePLDictionary(NULL, NULL, NULL);
706 for (i=0; options[i]!=NULL; i++) {
707 key = WMCreatePLString(options[i]);
709 val = WMGetFromPLDictionary(prop, key);
710 if (val) {
711 int j;
712 char *str = WMGetFromPLString(key);
713 for (j = 0; font_styles[j]!=NULL; j++) {
714 if (strcasecmp(str, font_styles[j]) == 0) {
715 char *oldfont;
716 oldfont = WMGetFromPLString(val);
717 if (oldfont[0] == '-') {
718 if (!strchr(oldfont, ','))
720 char *newfont;
721 newfont = xlfdToFc(oldfont);
722 val = WMCreatePLString(newfont);
723 break;
724 } else {
725 wwarning("fontsets are not supported. replaced with default %s", default_font);
726 val = WMCreatePLString(default_font);
727 break;
729 } else {
730 break;
734 WMPutInPLDictionary(style, key, val);
738 val = WMGetFromPLDictionary(prop, WMCreatePLString("PixmapPath"));
739 if (val)
740 PixmapPath = val;
742 if (theme_too) {
743 for (i=0; theme_options[i]!=NULL; i++) {
744 key = WMCreatePLString(theme_options[i]);
746 val = WMGetFromPLDictionary(prop, key);
747 if (val)
748 WMPutInPLDictionary(style, key, val);
752 if (make_pack) {
753 char *path;
755 makeThemePack(style, style_file);
757 path = mymalloc(strlen(ThemePath)+32);
758 strcpy(path, ThemePath);
759 strcat(path, "/style");
760 WMWritePropListToFile(style, path, False);
761 wfree(path);
762 } else {
763 if (style_file) {
764 WMWritePropListToFile(style, style_file, False);
765 } else {
766 puts(WMGetPropListDescription(style, True));
769 exit(0);