another test for cvs notifications
[wmaker-crm.git] / util / getstyle.c
blob9585cfe958a199e86ef6cdaa78987591ee0bdc51
1 /* getstyle.c - outputs style related options from WindowMaker to stdout
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 "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_options[] = {
108 "ClipTitleFont",
109 "WindowTitleFont",
110 "MenuTitleFont",
111 "MenuTextFont",
112 "IconTitleFont",
113 "DisplayFont",
114 "LargeDisplayFont",
115 NULL
118 #define DEFAULT_FONT "sans-serif: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 static char*
227 getuserhomedir(char *username)
229 struct passwd *user;
231 user = getpwnam(username);
232 if (!user) {
233 char buffer[100];
235 sprintf(buffer,"could not get password entry for user %s", username);
236 perror(buffer);
237 return NULL;
239 if (!user->pw_dir) {
240 return "/";
241 } else {
242 return user->pw_dir;
247 char*
248 wexpandpath(char *path)
250 char buffer2[PATH_MAX+2];
251 char buffer[PATH_MAX+2];
252 int i;
254 memset(buffer, 0, PATH_MAX+2);
256 if (*path=='~') {
257 char *home;
259 path++;
260 if (*path=='/' || *path==0) {
261 home = wgethomedir();
262 strcat(buffer, home);
263 } else {
264 int j;
265 j = 0;
266 while (*path!=0 && *path!='/') {
267 buffer2[j++] = *path;
268 buffer2[j] = 0;
269 path++;
271 home = getuserhomedir(buffer2);
272 if (!home)
273 return NULL;
274 strcat(buffer, home);
278 i = strlen(buffer);
280 while (*path!=0) {
281 char *tmp;
283 if (*path=='$') {
284 int j = 0;
285 path++;
286 /* expand $(HOME) or $HOME style environment variables */
287 if (*path=='(') {
288 path++;
289 while (*path!=0 && *path!=')') {
290 buffer2[j++] = *(path++);
291 buffer2[j] = 0;
293 if (*path==')')
294 path++;
295 tmp = getenv(buffer2);
296 if (!tmp) {
297 buffer[i] = 0;
298 strcat(buffer, "$(");
299 strcat(buffer, buffer2);
300 strcat(buffer, ")");
301 i += strlen(buffer2)+3;
302 } else {
303 strcat(buffer, tmp);
304 i += strlen(tmp);
306 } else {
307 while (*path!=0 && *path!='/') {
308 buffer2[j++] = *(path++);
309 buffer2[j] = 0;
311 tmp = getenv(buffer2);
312 if (!tmp) {
313 strcat(buffer, "$");
314 strcat(buffer, buffer2);
315 i += strlen(buffer2)+1;
316 } else {
317 strcat(buffer, tmp);
318 i += strlen(tmp);
321 } else {
322 buffer[i++] = *path;
323 path++;
327 return wstrdup(buffer);
332 char*
333 wfindfileinarray(WMPropList *paths, char *file)
335 int i;
336 char *path;
337 int len, flen;
338 char *fullpath;
340 if (!file)
341 return NULL;
343 if (*file=='/' || *file=='~' || !paths || !WMIsPLArray(paths)
344 || WMGetPropListItemCount(paths)==0) {
345 if (access(file, R_OK)<0) {
346 fullpath = wexpandpath(file);
347 if (!fullpath)
348 return NULL;
350 if (access(fullpath, R_OK)<0) {
351 free(fullpath);
352 return NULL;
353 } else {
354 return fullpath;
356 } else {
357 return wstrdup(file);
361 flen = strlen(file);
362 for (i=0; i < WMGetPropListItemCount(paths); i++) {
363 WMPropList *tmp;
364 char *dir;
366 tmp = WMGetFromPLArray(paths, i);
367 if (!WMIsPLString(tmp) || !(dir = WMGetFromPLString(tmp)))
368 continue;
370 len = strlen(dir);
371 path = wmalloc(len+flen+2);
372 path = memcpy(path, dir, len);
373 path[len]=0;
374 strcat(path, "/");
375 strcat(path, file);
376 /* expand tilde */
377 fullpath = wexpandpath(path);
378 free(path);
379 if (fullpath) {
380 /* check if file is readable */
381 if (access(fullpath, R_OK)==0) {
382 return fullpath;
384 free(fullpath);
387 return NULL;
391 static Bool
392 isFontOption(char *option)
394 int i;
396 for (i=0; font_options[i]!=NULL; i++) {
397 if (strcasecmp(option, font_options[i])==0) {
398 return True;
402 return False;
406 static int
407 countChar(char *str, char c)
409 int count = 0;
411 if (!str)
412 return 0;
414 for (; *str!=0; str++) {
415 if (*str == c) {
416 count++;
420 return count;
424 typedef struct str {
425 char *str;
426 int len;
427 } str;
429 #define XLFD_TOKENS 14
431 static str*
432 getXLFDTokens(char *xlfd)
434 static str tokens[XLFD_TOKENS];
435 int i, len, size;
436 char *ptr;
438 if (!xlfd || countChar(xlfd, '-')<XLFD_TOKENS)
439 return NULL;
441 memset(tokens, 0, sizeof(str)*XLFD_TOKENS);
443 len = strlen(xlfd);
445 for (ptr=xlfd, i=0; i<XLFD_TOKENS && len>0; i++) {
446 size = strspn(ptr, "-");
447 ptr += size;
448 len -= size;
449 if (len <= 0)
450 break;
451 size = strcspn(ptr, "-");
452 if (size==0)
453 break;
454 tokens[i].str = ptr;
455 tokens[i].len = size;
456 ptr += size;
457 len -= size;
460 return tokens;
464 static int
465 strToInt(str *token)
467 int res=0, pos, c;
469 if (token->len==0 || token->str[0]=='*') {
470 return -1;
471 } else {
472 for (res=0, pos=0; pos<token->len; pos++) {
473 c = token->str[pos] - '0';
474 if (c<0 || c>9)
475 break;
476 res = res*10 + c;
479 return res;
483 static char*
484 mapSlantToName(str *slant)
486 if (slant->len==0 || slant->str[0]=='*')
487 return "roman";
489 switch(slant->str[0]) {
490 case 'i':
491 return "italic";
492 case 'o':
493 return "oblique";
494 case 'r':
495 default:
496 return "roman";
501 char*
502 xlfdToFc(char *xlfd)
504 str *tokens, *family, *weight, *slant;
505 char *name, buf[512];
506 int size, pixelsize;
508 tokens = getXLFDTokens(xlfd);
509 if (!tokens)
510 return wstrdup(DEFAULT_FONT);
512 family = &(tokens[1]);
513 weight = &(tokens[2]);
514 slant = &(tokens[3]);
516 if (family->len==0 || family->str[0]=='*')
517 return wstrdup(DEFAULT_FONT);
519 sprintf(buf, "%.*s", family->len, family->str);
520 name = wstrdup(buf);
522 pixelsize = strToInt(&tokens[6]);
523 size = strToInt(&tokens[7]);
525 if (size<=0 && pixelsize<=0) {
526 name = wstrappend(name, ":pixelsize=12");
527 } else if (pixelsize>0) {
528 /* if pixelsize is present size will be ignored so we skip it */
529 sprintf(buf, ":pixelsize=%d", pixelsize);
530 name = wstrappend(name, buf);
531 } else {
532 sprintf(buf, "-%d", size/10);
533 name = wstrappend(name, buf);
536 if (weight->len>0 && weight->str[0]!='*') {
537 sprintf(buf, ":weight=%.*s", weight->len, weight->str);
538 name = wstrappend(name, buf);
541 if (slant->len>0 && slant->str[0]!='*') {
542 sprintf(buf, ":slant=%s", mapSlantToName(slant));
543 name = wstrappend(name, buf);
546 name = wstrappend(name, ":xlfd=");
547 name = wstrappend(name, xlfd);
549 return name;
553 /* return converted font (if conversion is needed) else the original font */
554 static char*
555 convertFont(char *font)
557 if (font[0]=='-') {
558 if (!strchr(font, ',')) {
559 return xlfdToFc(font);
560 } else {
561 wwarning("fontsets are not supported. replaced "
562 "with default %s", DEFAULT_FONT);
563 return wstrdup(DEFAULT_FONT);
565 } else {
566 return font;
571 void
572 copyFile(char *dir, char *file)
574 char buffer[4000];
576 sprintf(buffer, "/bin/cp \"%s\" \"%s\"", file, dir);
577 if (system(buffer)!=0) {
578 printf("%s: could not copy file %s\n", ProgName, file);
583 void
584 findCopyFile(char *dir, char *file)
586 char *fullPath;
588 fullPath = wfindfileinarray(PixmapPath, file);
589 if (!fullPath) {
590 char buffer[4000];
592 sprintf(buffer, "could not find file %s", file);
593 abortar(buffer);
595 copyFile(dir, fullPath);
596 free(fullPath);
600 char*
601 makeThemePack(WMPropList *style, char *themeName)
603 WMPropList *keys;
604 WMPropList *key;
605 WMPropList *value;
606 int i;
607 char *themeDir;
609 themeDir = wmalloc(strlen(themeName)+50);
610 sprintf(themeDir, "%s.themed", themeName);
611 ThemePath = themeDir;
613 char *tmp;
615 tmp = wmalloc(strlen(themeDir)+20);
616 sprintf(tmp, "/bin/mkdir \"%s\"", themeDir);
617 if (system(tmp)!=0) {
618 printf("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n", ProgName, themeDir);
619 exit(1);
621 free(tmp);
623 keys = WMGetPLDictionaryKeys(style);
625 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
626 key = WMGetFromPLArray(keys, i);
628 value = WMGetFromPLDictionary(style, key);
629 if (value && WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
630 WMPropList *type;
631 char *t;
633 type = WMGetFromPLArray(value, 0);
634 t = WMGetFromPLString(type);
635 if (t == NULL)
636 continue;
638 if (strcasecmp(t, "tpixmap")==0
639 || strcasecmp(t, "spixmap")==0
640 || strcasecmp(t, "cpixmap")==0
641 || strcasecmp(t, "mpixmap")==0
642 || strcasecmp(t, "tdgradient")==0
643 || strcasecmp(t, "tvgradient")==0
644 || strcasecmp(t, "thgradient")==0) {
645 WMPropList *file;
646 char *p;
647 char *newPath;
649 file = WMGetFromPLArray(value, 1);
651 p = strrchr(WMGetFromPLString(file), '/');
652 if (p) {
653 copyFile(themeDir, WMGetFromPLString(file));
655 newPath = wstrdup(p+1);
656 WMDeleteFromPLArray(value, 1);
657 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
658 free(newPath);
659 } else {
660 findCopyFile(themeDir, WMGetFromPLString(file));
662 } else if (strcasecmp(t, "bitmap")==0) {
663 WMPropList *file;
664 char *p;
665 char *newPath;
667 file = WMGetFromPLArray(value, 1);
669 p = strrchr(WMGetFromPLString(file), '/');
670 if (p) {
671 copyFile(themeDir, WMGetFromPLString(file));
673 newPath = wstrdup(p+1);
674 WMDeleteFromPLArray(value, 1);
675 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
676 free(newPath);
677 } else {
678 findCopyFile(themeDir, WMGetFromPLString(file));
682 file = WMGetFromPLArray(value, 2);
684 p = strrchr(WMGetFromPLString(file), '/');
685 if (p) {
686 copyFile(themeDir, WMGetFromPLString(file));
688 newPath = wstrdup(p+1);
689 WMDeleteFromPLArray(value, 2);
690 WMInsertInPLArray(value, 2, WMCreatePLString(newPath));
691 free(newPath);
692 } else {
693 findCopyFile(themeDir, WMGetFromPLString(file));
699 return themeDir;
704 main(int argc, char **argv)
706 WMPropList *prop, *style, *key, *val;
707 char *path;
708 int i, theme_too=0, make_pack=0;
709 char *style_file = NULL;
712 ProgName = argv[0];
714 if (argc>1) {
715 for (i=1; i<argc; i++) {
716 if (strcmp(argv[i], "-p")==0
717 || strcmp(argv[i], "--pack")==0) {
718 make_pack = 1;
719 theme_too = 1;
720 } else if (strcmp(argv[i], "-t")==0
721 || strcmp(argv[i], "--theme-options")==0) {
722 theme_too++;
723 } else if (strcmp(argv[i], "--help")==0) {
724 print_help();
725 exit(0);
726 } else if (strcmp(argv[i], "--version")==0) {
727 puts(PROG_VERSION);
728 exit(0);
729 } else {
730 if (style_file!=NULL) {
731 printf("%s: invalid argument '%s'\n", argv[0],
732 style_file[0]=='-' ? style_file : argv[i]);
733 printf("Try '%s --help' for more information\n", argv[0]);
734 exit(1);
736 style_file = argv[i];
741 if (make_pack && !style_file) {
742 printf("%s: you must supply a name for the theme pack\n", ProgName);
743 exit(1);
746 WMPLSetCaseSensitive(False);
748 path = defaultsPathForDomain("WindowMaker");
750 prop = WMReadPropListFromFile(path);
751 if (!prop) {
752 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
753 ProgName, path);
754 exit(1);
757 /* get global value */
758 path = globalDefaultsPathForDomain("WindowMaker");
759 val = WMReadPropListFromFile(path);
760 if (val) {
761 WMMergePLDictionaries(val, prop, True);
762 WMReleasePropList(prop);
763 prop = val;
766 style = WMCreatePLDictionary(NULL, NULL, NULL);
768 for (i=0; options[i]!=NULL; i++) {
769 key = WMCreatePLString(options[i]);
771 val = WMGetFromPLDictionary(prop, key);
772 if (val) {
773 WMRetainPropList(val);
774 if (isFontOption(options[i])) {
775 char *newfont, *oldfont;
777 oldfont = WMGetFromPLString(val);
778 newfont = convertFont(oldfont);
779 /* newfont is a reference to old if conversion is not needed */
780 if (newfont != oldfont) {
781 WMReleasePropList(val);
782 val = WMCreatePLString(newfont);
783 wfree(newfont);
786 WMPutInPLDictionary(style, key, val);
787 WMReleasePropList(val);
789 WMReleasePropList(key);
792 val = WMGetFromPLDictionary(prop, WMCreatePLString("PixmapPath"));
793 if (val)
794 PixmapPath = val;
796 if (theme_too) {
797 for (i=0; theme_options[i]!=NULL; i++) {
798 key = WMCreatePLString(theme_options[i]);
800 val = WMGetFromPLDictionary(prop, key);
801 if (val)
802 WMPutInPLDictionary(style, key, val);
806 if (make_pack) {
807 char *path;
809 makeThemePack(style, style_file);
811 path = wmalloc(strlen(ThemePath)+32);
812 strcpy(path, ThemePath);
813 strcat(path, "/style");
814 WMWritePropListToFile(style, path, False);
815 wfree(path);
816 } else {
817 if (style_file) {
818 WMWritePropListToFile(style, style_file, False);
819 } else {
820 puts(WMGetPropListDescription(style, True));
823 exit(0);