- Added support for NET_WM_NAME/NET_WM_ICON_NAME
[wmaker-crm.git] / util / getstyle.c
blob32e70998c47843b296952ce0593c50dc9c478f2f
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;
126 extern char* convertFont(char *font, Bool keepXLFD);
129 void
130 print_help()
132 printf("Usage: %s [OPTIONS] [FILE]\n", ProgName);
133 puts("Retrieves style/theme configuration and output to FILE or to stdout");
134 puts("");
135 puts(" -t, --theme-options output theme related options when producing a style file");
136 puts(" -p, --pack produce output as a theme pack");
137 puts(" --help display this help and exit");
138 puts(" --version output version information and exit");
142 char*
143 globalDefaultsPathForDomain(char *domain)
145 static char path[1024];
147 sprintf(path, "%s/WindowMaker/%s", SYSCONFDIR, domain);
149 return path;
153 char*
154 defaultsPathForDomain(char *domain)
156 static char path[1024];
157 char *gspath;
159 gspath = getenv("GNUSTEP_USER_ROOT");
160 if (gspath) {
161 strcpy(path, gspath);
162 strcat(path, "/");
163 } else {
164 char *home;
166 home = getenv("HOME");
167 if (!home) {
168 printf("%s:could not get HOME environment variable!\n", ProgName);
169 exit(0);
171 strcpy(path, home);
172 strcat(path, "/GNUstep/");
174 strcat(path, DEFAULTS_DIR);
175 strcat(path, "/");
176 strcat(path, domain);
178 return path;
182 void
183 abortar(char *reason)
185 char buffer[4000];
187 printf("%s: %s\n", ProgName, reason);
189 if (ThemePath) {
190 printf("Removing unfinished theme pack\n");
191 sprintf(buffer, "/bin/rm -fr \"%s\"", ThemePath);
193 if (system(buffer)!=0) {
194 printf("%s: could not execute command %s\n", ProgName, buffer);
197 exit(1);
203 char*
204 wgethomedir()
206 char *home = getenv("HOME");
207 struct passwd *user;
209 if (home)
210 return home;
212 user = getpwuid(getuid());
213 if (!user) {
214 char buffer[80];
216 sprintf(buffer, "could not get password entry for UID %i", getuid());
217 perror(buffer);
218 return "/";
220 if (!user->pw_dir) {
221 return "/";
222 } else {
223 return user->pw_dir;
228 static char*
229 getuserhomedir(char *username)
231 struct passwd *user;
233 user = getpwnam(username);
234 if (!user) {
235 char buffer[100];
237 sprintf(buffer,"could not get password entry for user %s", username);
238 perror(buffer);
239 return NULL;
241 if (!user->pw_dir) {
242 return "/";
243 } else {
244 return user->pw_dir;
249 char*
250 wexpandpath(char *path)
252 char buffer2[PATH_MAX+2];
253 char buffer[PATH_MAX+2];
254 int i;
256 memset(buffer, 0, PATH_MAX+2);
258 if (*path=='~') {
259 char *home;
261 path++;
262 if (*path=='/' || *path==0) {
263 home = wgethomedir();
264 strcat(buffer, home);
265 } else {
266 int j;
267 j = 0;
268 while (*path!=0 && *path!='/') {
269 buffer2[j++] = *path;
270 buffer2[j] = 0;
271 path++;
273 home = getuserhomedir(buffer2);
274 if (!home)
275 return NULL;
276 strcat(buffer, home);
280 i = strlen(buffer);
282 while (*path!=0) {
283 char *tmp;
285 if (*path=='$') {
286 int j = 0;
287 path++;
288 /* expand $(HOME) or $HOME style environment variables */
289 if (*path=='(') {
290 path++;
291 while (*path!=0 && *path!=')') {
292 buffer2[j++] = *(path++);
293 buffer2[j] = 0;
295 if (*path==')')
296 path++;
297 tmp = getenv(buffer2);
298 if (!tmp) {
299 buffer[i] = 0;
300 strcat(buffer, "$(");
301 strcat(buffer, buffer2);
302 strcat(buffer, ")");
303 i += strlen(buffer2)+3;
304 } else {
305 strcat(buffer, tmp);
306 i += strlen(tmp);
308 } else {
309 while (*path!=0 && *path!='/') {
310 buffer2[j++] = *(path++);
311 buffer2[j] = 0;
313 tmp = getenv(buffer2);
314 if (!tmp) {
315 strcat(buffer, "$");
316 strcat(buffer, buffer2);
317 i += strlen(buffer2)+1;
318 } else {
319 strcat(buffer, tmp);
320 i += strlen(tmp);
323 } else {
324 buffer[i++] = *path;
325 path++;
329 return wstrdup(buffer);
334 char*
335 wfindfileinarray(WMPropList *paths, char *file)
337 int i;
338 char *path;
339 int len, flen;
340 char *fullpath;
342 if (!file)
343 return NULL;
345 if (*file=='/' || *file=='~' || !paths || !WMIsPLArray(paths)
346 || WMGetPropListItemCount(paths)==0) {
347 if (access(file, R_OK)<0) {
348 fullpath = wexpandpath(file);
349 if (!fullpath)
350 return NULL;
352 if (access(fullpath, R_OK)<0) {
353 free(fullpath);
354 return NULL;
355 } else {
356 return fullpath;
358 } else {
359 return wstrdup(file);
363 flen = strlen(file);
364 for (i=0; i < WMGetPropListItemCount(paths); i++) {
365 WMPropList *tmp;
366 char *dir;
368 tmp = WMGetFromPLArray(paths, i);
369 if (!WMIsPLString(tmp) || !(dir = WMGetFromPLString(tmp)))
370 continue;
372 len = strlen(dir);
373 path = wmalloc(len+flen+2);
374 path = memcpy(path, dir, len);
375 path[len]=0;
376 strcat(path, "/");
377 strcat(path, file);
378 /* expand tilde */
379 fullpath = wexpandpath(path);
380 free(path);
381 if (fullpath) {
382 /* check if file is readable */
383 if (access(fullpath, R_OK)==0) {
384 return fullpath;
386 free(fullpath);
389 return NULL;
393 static Bool
394 isFontOption(char *option)
396 int i;
398 for (i=0; font_options[i]!=NULL; i++) {
399 if (strcasecmp(option, font_options[i])==0) {
400 return True;
404 return False;
408 void
409 copyFile(char *dir, char *file)
411 char buffer[4000];
413 sprintf(buffer, "/bin/cp \"%s\" \"%s\"", file, dir);
414 if (system(buffer)!=0) {
415 printf("%s: could not copy file %s\n", ProgName, file);
420 void
421 findCopyFile(char *dir, char *file)
423 char *fullPath;
425 fullPath = wfindfileinarray(PixmapPath, file);
426 if (!fullPath) {
427 char buffer[4000];
429 sprintf(buffer, "could not find file %s", file);
430 abortar(buffer);
432 copyFile(dir, fullPath);
433 free(fullPath);
437 char*
438 makeThemePack(WMPropList *style, char *themeName)
440 WMPropList *keys;
441 WMPropList *key;
442 WMPropList *value;
443 int i;
444 char *themeDir;
446 themeDir = wmalloc(strlen(themeName)+50);
447 sprintf(themeDir, "%s.themed", themeName);
448 ThemePath = themeDir;
450 char *tmp;
452 tmp = wmalloc(strlen(themeDir)+20);
453 sprintf(tmp, "/bin/mkdir \"%s\"", themeDir);
454 if (system(tmp)!=0) {
455 printf("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n", ProgName, themeDir);
456 exit(1);
458 free(tmp);
460 keys = WMGetPLDictionaryKeys(style);
462 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
463 key = WMGetFromPLArray(keys, i);
465 value = WMGetFromPLDictionary(style, key);
466 if (value && WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
467 WMPropList *type;
468 char *t;
470 type = WMGetFromPLArray(value, 0);
471 t = WMGetFromPLString(type);
472 if (t == NULL)
473 continue;
475 if (strcasecmp(t, "tpixmap")==0
476 || strcasecmp(t, "spixmap")==0
477 || strcasecmp(t, "cpixmap")==0
478 || strcasecmp(t, "mpixmap")==0
479 || strcasecmp(t, "tdgradient")==0
480 || strcasecmp(t, "tvgradient")==0
481 || strcasecmp(t, "thgradient")==0) {
482 WMPropList *file;
483 char *p;
484 char *newPath;
486 file = WMGetFromPLArray(value, 1);
488 p = strrchr(WMGetFromPLString(file), '/');
489 if (p) {
490 copyFile(themeDir, WMGetFromPLString(file));
492 newPath = wstrdup(p+1);
493 WMDeleteFromPLArray(value, 1);
494 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
495 free(newPath);
496 } else {
497 findCopyFile(themeDir, WMGetFromPLString(file));
499 } else if (strcasecmp(t, "bitmap")==0) {
500 WMPropList *file;
501 char *p;
502 char *newPath;
504 file = WMGetFromPLArray(value, 1);
506 p = strrchr(WMGetFromPLString(file), '/');
507 if (p) {
508 copyFile(themeDir, WMGetFromPLString(file));
510 newPath = wstrdup(p+1);
511 WMDeleteFromPLArray(value, 1);
512 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
513 free(newPath);
514 } else {
515 findCopyFile(themeDir, WMGetFromPLString(file));
519 file = WMGetFromPLArray(value, 2);
521 p = strrchr(WMGetFromPLString(file), '/');
522 if (p) {
523 copyFile(themeDir, WMGetFromPLString(file));
525 newPath = wstrdup(p+1);
526 WMDeleteFromPLArray(value, 2);
527 WMInsertInPLArray(value, 2, WMCreatePLString(newPath));
528 free(newPath);
529 } else {
530 findCopyFile(themeDir, WMGetFromPLString(file));
536 return themeDir;
541 main(int argc, char **argv)
543 WMPropList *prop, *style, *key, *val;
544 char *path;
545 int i, theme_too=0, make_pack=0;
546 char *style_file = NULL;
549 ProgName = argv[0];
551 if (argc>1) {
552 for (i=1; i<argc; i++) {
553 if (strcmp(argv[i], "-p")==0
554 || strcmp(argv[i], "--pack")==0) {
555 make_pack = 1;
556 theme_too = 1;
557 } else if (strcmp(argv[i], "-t")==0
558 || strcmp(argv[i], "--theme-options")==0) {
559 theme_too++;
560 } else if (strcmp(argv[i], "--help")==0) {
561 print_help();
562 exit(0);
563 } else if (strcmp(argv[i], "--version")==0) {
564 puts(PROG_VERSION);
565 exit(0);
566 } else {
567 if (style_file!=NULL) {
568 printf("%s: invalid argument '%s'\n", argv[0],
569 style_file[0]=='-' ? style_file : argv[i]);
570 printf("Try '%s --help' for more information\n", argv[0]);
571 exit(1);
573 style_file = argv[i];
578 if (make_pack && !style_file) {
579 printf("%s: you must supply a name for the theme pack\n", ProgName);
580 exit(1);
583 WMPLSetCaseSensitive(False);
585 path = defaultsPathForDomain("WindowMaker");
587 prop = WMReadPropListFromFile(path);
588 if (!prop) {
589 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
590 ProgName, path);
591 exit(1);
594 /* get global value */
595 path = globalDefaultsPathForDomain("WindowMaker");
596 val = WMReadPropListFromFile(path);
597 if (val) {
598 WMMergePLDictionaries(val, prop, True);
599 WMReleasePropList(prop);
600 prop = val;
603 style = WMCreatePLDictionary(NULL, NULL);
605 for (i=0; options[i]!=NULL; i++) {
606 key = WMCreatePLString(options[i]);
608 val = WMGetFromPLDictionary(prop, key);
609 if (val) {
610 WMRetainPropList(val);
611 if (isFontOption(options[i])) {
612 char *newfont, *oldfont;
614 oldfont = WMGetFromPLString(val);
615 newfont = convertFont(oldfont, False);
616 /* newfont is a reference to old if conversion is not needed */
617 if (newfont != oldfont) {
618 WMReleasePropList(val);
619 val = WMCreatePLString(newfont);
620 wfree(newfont);
623 WMPutInPLDictionary(style, key, val);
624 WMReleasePropList(val);
626 WMReleasePropList(key);
629 val = WMGetFromPLDictionary(prop, WMCreatePLString("PixmapPath"));
630 if (val)
631 PixmapPath = val;
633 if (theme_too) {
634 for (i=0; theme_options[i]!=NULL; i++) {
635 key = WMCreatePLString(theme_options[i]);
637 val = WMGetFromPLDictionary(prop, key);
638 if (val)
639 WMPutInPLDictionary(style, key, val);
643 if (make_pack) {
644 char *path;
646 makeThemePack(style, style_file);
648 path = wmalloc(strlen(ThemePath)+32);
649 strcpy(path, ThemePath);
650 strcat(path, "/style");
651 WMWritePropListToFile(style, path, False);
652 wfree(path);
653 } else {
654 if (style_file) {
655 WMWritePropListToFile(style, style_file, False);
656 } else {
657 puts(WMGetPropListDescription(style, True));
660 exit(0);