- Removed obsoleted options from WMGLOBAL and from the WINGs internal
[wmaker-crm.git] / util / getstyle.c
blobe1fd3beae102715c24cb903a06e5cfc9c71e3563
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 char *ProgName;
120 WMPropList *PixmapPath = NULL;
122 char *ThemePath = NULL;
124 extern char* convertFont(char *font, Bool keepXLFD);
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 void
407 copyFile(char *dir, char *file)
409 char buffer[4000];
411 sprintf(buffer, "/bin/cp \"%s\" \"%s\"", file, dir);
412 if (system(buffer)!=0) {
413 printf("%s: could not copy file %s\n", ProgName, file);
418 void
419 findCopyFile(char *dir, char *file)
421 char *fullPath;
423 fullPath = wfindfileinarray(PixmapPath, file);
424 if (!fullPath) {
425 char buffer[4000];
427 sprintf(buffer, "could not find file %s", file);
428 abortar(buffer);
430 copyFile(dir, fullPath);
431 free(fullPath);
435 char*
436 makeThemePack(WMPropList *style, char *themeName)
438 WMPropList *keys;
439 WMPropList *key;
440 WMPropList *value;
441 int i;
442 char *themeDir;
444 themeDir = wmalloc(strlen(themeName)+50);
445 sprintf(themeDir, "%s.themed", themeName);
446 ThemePath = themeDir;
448 char *tmp;
450 tmp = wmalloc(strlen(themeDir)+20);
451 sprintf(tmp, "/bin/mkdir \"%s\"", themeDir);
452 if (system(tmp)!=0) {
453 printf("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n", ProgName, themeDir);
454 exit(1);
456 free(tmp);
458 keys = WMGetPLDictionaryKeys(style);
460 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
461 key = WMGetFromPLArray(keys, i);
463 value = WMGetFromPLDictionary(style, key);
464 if (value && WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
465 WMPropList *type;
466 char *t;
468 type = WMGetFromPLArray(value, 0);
469 t = WMGetFromPLString(type);
470 if (t == NULL)
471 continue;
473 if (strcasecmp(t, "tpixmap")==0
474 || strcasecmp(t, "spixmap")==0
475 || strcasecmp(t, "cpixmap")==0
476 || strcasecmp(t, "mpixmap")==0
477 || strcasecmp(t, "tdgradient")==0
478 || strcasecmp(t, "tvgradient")==0
479 || strcasecmp(t, "thgradient")==0) {
480 WMPropList *file;
481 char *p;
482 char *newPath;
484 file = WMGetFromPLArray(value, 1);
486 p = strrchr(WMGetFromPLString(file), '/');
487 if (p) {
488 copyFile(themeDir, WMGetFromPLString(file));
490 newPath = wstrdup(p+1);
491 WMDeleteFromPLArray(value, 1);
492 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
493 free(newPath);
494 } else {
495 findCopyFile(themeDir, WMGetFromPLString(file));
497 } else if (strcasecmp(t, "bitmap")==0) {
498 WMPropList *file;
499 char *p;
500 char *newPath;
502 file = WMGetFromPLArray(value, 1);
504 p = strrchr(WMGetFromPLString(file), '/');
505 if (p) {
506 copyFile(themeDir, WMGetFromPLString(file));
508 newPath = wstrdup(p+1);
509 WMDeleteFromPLArray(value, 1);
510 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
511 free(newPath);
512 } else {
513 findCopyFile(themeDir, WMGetFromPLString(file));
517 file = WMGetFromPLArray(value, 2);
519 p = strrchr(WMGetFromPLString(file), '/');
520 if (p) {
521 copyFile(themeDir, WMGetFromPLString(file));
523 newPath = wstrdup(p+1);
524 WMDeleteFromPLArray(value, 2);
525 WMInsertInPLArray(value, 2, WMCreatePLString(newPath));
526 free(newPath);
527 } else {
528 findCopyFile(themeDir, WMGetFromPLString(file));
534 return themeDir;
539 main(int argc, char **argv)
541 WMPropList *prop, *style, *key, *val;
542 char *path;
543 int i, theme_too=0, make_pack=0;
544 char *style_file = NULL;
547 ProgName = argv[0];
549 if (argc>1) {
550 for (i=1; i<argc; i++) {
551 if (strcmp(argv[i], "-p")==0
552 || strcmp(argv[i], "--pack")==0) {
553 make_pack = 1;
554 theme_too = 1;
555 } else if (strcmp(argv[i], "-t")==0
556 || strcmp(argv[i], "--theme-options")==0) {
557 theme_too++;
558 } else if (strcmp(argv[i], "--help")==0) {
559 print_help();
560 exit(0);
561 } else if (strcmp(argv[i], "--version")==0) {
562 puts(PROG_VERSION);
563 exit(0);
564 } else {
565 if (style_file!=NULL) {
566 printf("%s: invalid argument '%s'\n", argv[0],
567 style_file[0]=='-' ? style_file : argv[i]);
568 printf("Try '%s --help' for more information\n", argv[0]);
569 exit(1);
571 style_file = argv[i];
576 if (make_pack && !style_file) {
577 printf("%s: you must supply a name for the theme pack\n", ProgName);
578 exit(1);
581 WMPLSetCaseSensitive(False);
583 path = defaultsPathForDomain("WindowMaker");
585 prop = WMReadPropListFromFile(path);
586 if (!prop) {
587 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
588 ProgName, path);
589 exit(1);
592 /* get global value */
593 path = globalDefaultsPathForDomain("WindowMaker");
594 val = WMReadPropListFromFile(path);
595 if (val) {
596 WMMergePLDictionaries(val, prop, True);
597 WMReleasePropList(prop);
598 prop = val;
601 style = WMCreatePLDictionary(NULL, NULL);
603 for (i=0; options[i]!=NULL; i++) {
604 key = WMCreatePLString(options[i]);
606 val = WMGetFromPLDictionary(prop, key);
607 if (val) {
608 WMRetainPropList(val);
609 if (isFontOption(options[i])) {
610 char *newfont, *oldfont;
612 oldfont = WMGetFromPLString(val);
613 newfont = convertFont(oldfont, False);
614 /* newfont is a reference to old if conversion is not needed */
615 if (newfont != oldfont) {
616 WMReleasePropList(val);
617 val = WMCreatePLString(newfont);
618 wfree(newfont);
621 WMPutInPLDictionary(style, key, val);
622 WMReleasePropList(val);
624 WMReleasePropList(key);
627 val = WMGetFromPLDictionary(prop, WMCreatePLString("PixmapPath"));
628 if (val)
629 PixmapPath = val;
631 if (theme_too) {
632 for (i=0; theme_options[i]!=NULL; i++) {
633 key = WMCreatePLString(theme_options[i]);
635 val = WMGetFromPLDictionary(prop, key);
636 if (val)
637 WMPutInPLDictionary(style, key, val);
641 if (make_pack) {
642 char *path;
644 makeThemePack(style, style_file);
646 path = wmalloc(strlen(ThemePath)+32);
647 strcpy(path, ThemePath);
648 strcat(path, "/style");
649 WMWritePropListToFile(style, path, False);
650 wfree(path);
651 } else {
652 if (style_file) {
653 WMWritePropListToFile(style, style_file, False);
654 } else {
655 puts(WMGetPropListDescription(style, True));
658 exit(0);