definable cursor code updates from Jim Knoble <jmknoble@pobox.com>
[wmaker-crm.git] / util / getstyle.c
blob7a6869ac4e95b04152cb5f9d319b2c4beae39c8f
1 /* getstyle.c - outputs style related options from WindowMaker to stdout
3 * WindowMaker window manager
4 *
5 * Copyright (c) 1997~2000 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 <proplist.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <pwd.h>
35 #include <limits.h>
36 #include <assert.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
107 char *ProgName;
109 proplist_t PixmapPath = NULL;
111 char *ThemePath = NULL;
114 void
115 print_help()
117 printf("Usage: %s [OPTIONS] [FILE]\n", ProgName);
118 puts("Retrieves style/theme configuration and output to FILE or to stdout");
119 puts("");
120 puts(" -t, --theme-options output theme related options when producing a style file");
121 puts(" -p, --pack produce output as a theme pack");
122 puts(" --help display this help and exit");
123 puts(" --version output version information and exit");
127 char*
128 globalDefaultsPathForDomain(char *domain)
130 static char path[1024];
132 sprintf(path, "%s/WindowMaker/%s", SYSCONFDIR, domain);
134 return path;
138 char*
139 defaultsPathForDomain(char *domain)
141 static char path[1024];
142 char *gspath;
144 gspath = getenv("GNUSTEP_USER_ROOT");
145 if (gspath) {
146 strcpy(path, gspath);
147 strcat(path, "/");
148 } else {
149 char *home;
151 home = getenv("HOME");
152 if (!home) {
153 printf("%s:could not get HOME environment variable!\n", ProgName);
154 exit(0);
156 strcpy(path, home);
157 strcat(path, "/GNUstep/");
159 strcat(path, DEFAULTS_DIR);
160 strcat(path, "/");
161 strcat(path, domain);
163 return path;
167 BOOL
168 StringCompareHook(proplist_t pl1, proplist_t pl2)
170 char *str1, *str2;
172 str1 = PLGetString(pl1);
173 str2 = PLGetString(pl2);
175 if (strcasecmp(str1, str2)==0)
176 return YES;
177 else
178 return NO;
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 void*
229 wmalloc(int size)
231 void *tmp;
233 tmp = malloc(size);
234 if (!tmp) {
235 abortar("out of memory");
238 return tmp;
242 char*
243 wstrdup(char *str)
245 char *tmp;
247 tmp = wmalloc(strlen(str)+1);
249 strcpy(tmp, str);
251 return tmp;
255 static char*
256 getuserhomedir(char *username)
258 struct passwd *user;
260 user = getpwnam(username);
261 if (!user) {
262 char buffer[100];
264 sprintf(buffer,"could not get password entry for user %s", username);
265 perror(buffer);
266 return NULL;
268 if (!user->pw_dir) {
269 return "/";
270 } else {
271 return user->pw_dir;
278 char*
279 wexpandpath(char *path)
281 char buffer2[PATH_MAX+2];
282 char buffer[PATH_MAX+2];
283 int i;
285 memset(buffer, 0, PATH_MAX+2);
287 if (*path=='~') {
288 char *home;
290 path++;
291 if (*path=='/' || *path==0) {
292 home = wgethomedir();
293 strcat(buffer, home);
294 } else {
295 int j;
296 j = 0;
297 while (*path!=0 && *path!='/') {
298 buffer2[j++] = *path;
299 buffer2[j] = 0;
300 path++;
302 home = getuserhomedir(buffer2);
303 if (!home)
304 return NULL;
305 strcat(buffer, home);
309 i = strlen(buffer);
311 while (*path!=0) {
312 char *tmp;
314 if (*path=='$') {
315 int j = 0;
316 path++;
317 /* expand $(HOME) or $HOME style environment variables */
318 if (*path=='(') {
319 path++;
320 while (*path!=0 && *path!=')') {
321 buffer2[j++] = *(path++);
322 buffer2[j] = 0;
324 if (*path==')')
325 path++;
326 tmp = getenv(buffer2);
327 if (!tmp) {
328 buffer[i] = 0;
329 strcat(buffer, "$(");
330 strcat(buffer, buffer2);
331 strcat(buffer, ")");
332 i += strlen(buffer2)+3;
333 } else {
334 strcat(buffer, tmp);
335 i += strlen(tmp);
337 } else {
338 while (*path!=0 && *path!='/') {
339 buffer2[j++] = *(path++);
340 buffer2[j] = 0;
342 tmp = getenv(buffer2);
343 if (!tmp) {
344 strcat(buffer, "$");
345 strcat(buffer, buffer2);
346 i += strlen(buffer2)+1;
347 } else {
348 strcat(buffer, tmp);
349 i += strlen(tmp);
352 } else {
353 buffer[i++] = *path;
354 path++;
358 return wstrdup(buffer);
363 char*
364 wfindfileinarray(proplist_t paths, char *file)
366 int i;
367 char *path;
368 int len, flen;
369 char *fullpath;
371 if (!file)
372 return NULL;
374 if (*file=='/' || *file=='~' || !paths || !PLIsArray(paths)
375 || PLGetNumberOfElements(paths)==0) {
376 if (access(file, R_OK)<0) {
377 fullpath = wexpandpath(file);
378 if (!fullpath)
379 return NULL;
381 if (access(fullpath, R_OK)<0) {
382 free(fullpath);
383 return NULL;
384 } else {
385 return fullpath;
387 } else {
388 return wstrdup(file);
392 flen = strlen(file);
393 for (i=0; i < PLGetNumberOfElements(paths); i++) {
394 proplist_t tmp;
395 char *dir;
397 tmp = PLGetArrayElement(paths, i);
398 if (!PLIsString(tmp) || !(dir = PLGetString(tmp)))
399 continue;
401 len = strlen(dir);
402 path = wmalloc(len+flen+2);
403 path = memcpy(path, dir, len);
404 path[len]=0;
405 strcat(path, "/");
406 strcat(path, file);
407 /* expand tilde */
408 fullpath = wexpandpath(path);
409 free(path);
410 if (fullpath) {
411 /* check if file is readable */
412 if (access(fullpath, R_OK)==0) {
413 return fullpath;
415 free(fullpath);
418 return NULL;
424 void
425 copyFile(char *dir, char *file)
427 char buffer[4000];
429 sprintf(buffer, "/bin/cp \"%s\" \"%s\"", file, dir);
430 if (system(buffer)!=0) {
431 printf("%s: could not copy file %s\n", ProgName, file);
436 void
437 findCopyFile(char *dir, char *file)
439 char *fullPath;
441 fullPath = wfindfileinarray(PixmapPath, file);
442 if (!fullPath) {
443 char buffer[4000];
445 sprintf(buffer, "coould not find file %s", file);
446 abortar(buffer);
448 copyFile(dir, fullPath);
449 free(fullPath);
453 char*
454 makeThemePack(proplist_t style, char *themeName)
456 proplist_t keys;
457 proplist_t key;
458 proplist_t value;
459 int i;
460 char *themeDir;
462 themeDir = wmalloc(strlen(themeName)+50);
463 sprintf(themeDir, "%s.themed", themeName);
464 ThemePath = themeDir;
466 char *tmp;
468 tmp = wmalloc(strlen(themeDir)+20);
469 sprintf(tmp, "/bin/mkdir \"%s\"", themeDir);
470 if (system(tmp)!=0) {
471 printf("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n", ProgName, themeDir);
472 exit(1);
474 free(tmp);
476 keys = PLGetAllDictionaryKeys(style);
478 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
479 key = PLGetArrayElement(keys, i);
481 value = PLGetDictionaryEntry(style, key);
482 if (value && PLIsArray(value) && PLGetNumberOfElements(value) > 2) {
483 proplist_t type;
484 char *t;
486 type = PLGetArrayElement(value, 0);
487 t = PLGetString(type);
488 if (t == NULL)
489 continue;
491 if (strcasecmp(t, "tpixmap")==0
492 || strcasecmp(t, "spixmap")==0
493 || strcasecmp(t, "cpixmap")==0
494 || strcasecmp(t, "mpixmap")==0
495 || strcasecmp(t, "tdgradient")==0
496 || strcasecmp(t, "tvgradient")==0
497 || strcasecmp(t, "thgradient")==0) {
498 proplist_t file;
499 char *p;
500 char *newPath;
502 file = PLGetArrayElement(value, 1);
504 p = strrchr(PLGetString(file), '/');
505 if (p) {
506 copyFile(themeDir, PLGetString(file));
508 newPath = wstrdup(p+1);
509 PLRemoveArrayElement(value, 1);
510 PLInsertArrayElement(value, PLMakeString(newPath), 1);
511 free(newPath);
512 } else {
513 findCopyFile(themeDir, PLGetString(file));
515 } else if (strcasecmp(t, "bitmap")==0) {
516 proplist_t file;
517 char *p;
518 char *newPath;
520 file = PLGetArrayElement(value, 1);
522 p = strrchr(PLGetString(file), '/');
523 if (p) {
524 copyFile(themeDir, PLGetString(file));
526 newPath = wstrdup(p+1);
527 PLRemoveArrayElement(value, 1);
528 PLInsertArrayElement(value, PLMakeString(newPath), 1);
529 free(newPath);
530 } else {
531 findCopyFile(themeDir, PLGetString(file));
535 file = PLGetArrayElement(value, 2);
537 p = strrchr(PLGetString(file), '/');
538 if (p) {
539 copyFile(themeDir, PLGetString(file));
541 newPath = wstrdup(p+1);
542 PLRemoveArrayElement(value, 2);
543 PLInsertArrayElement(value, PLMakeString(newPath), 2);
544 free(newPath);
545 } else {
546 findCopyFile(themeDir, PLGetString(file));
552 return themeDir;
556 int
557 main(int argc, char **argv)
559 proplist_t prop, style, key, val;
560 char *path;
561 int i, theme_too=0;
562 int make_pack = 0;
563 char *style_file = NULL;
566 ProgName = argv[0];
568 if (argc>1) {
569 for (i=1; i<argc; i++) {
570 if (strcmp(argv[i], "-p")==0
571 || strcmp(argv[i], "--pack")==0) {
572 make_pack = 1;
573 theme_too = 1;
574 } else if (strcmp(argv[i], "-t")==0
575 || strcmp(argv[i], "--theme-options")==0) {
576 theme_too++;
577 } else if (strcmp(argv[i], "--help")==0) {
578 print_help();
579 exit(0);
580 } else if (strcmp(argv[i], "--version")==0) {
581 puts(PROG_VERSION);
582 exit(0);
583 } else {
584 if (style_file!=NULL) {
585 printf("%s: invalid argument '%s'\n", argv[0],
586 style_file[0]=='-' ? style_file : argv[i]);
587 printf("Try '%s --help' for more information\n", argv[0]);
588 exit(1);
590 style_file = argv[i];
595 if (make_pack && !style_file) {
596 printf("%s: you must supply a name for the theme pack\n", ProgName);
597 exit(1);
600 PLSetStringCmpHook(StringCompareHook);
602 path = defaultsPathForDomain("WindowMaker");
604 prop = PLGetProplistWithPath(path);
605 if (!prop) {
606 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
607 ProgName, path);
608 exit(1);
611 /* get global value */
612 path = globalDefaultsPathForDomain("WindowMaker");
613 val = PLGetProplistWithPath(path);
614 if (val) {
615 PLMergeDictionaries(val, prop);
616 PLRelease(prop);
617 prop = val;
620 style = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
623 for (i=0; options[i]!=NULL; i++) {
624 key = PLMakeString(options[i]);
626 val = PLGetDictionaryEntry(prop, key);
627 if (val)
628 PLInsertDictionaryEntry(style, key, val);
631 val = PLGetDictionaryEntry(prop, PLMakeString("PixmapPath"));
632 if (val)
633 PixmapPath = val;
635 if (theme_too) {
636 for (i=0; theme_options[i]!=NULL; i++) {
637 key = PLMakeString(theme_options[i]);
639 val = PLGetDictionaryEntry(prop, key);
640 if (val)
641 PLInsertDictionaryEntry(style, key, val);
645 if (make_pack) {
646 char *path;
648 makeThemePack(style, style_file);
650 path = wmalloc(strlen(ThemePath)+32);
651 strcpy(path, ThemePath);
652 strcat(path, "/style");
653 PLSetFilename(style, PLMakeString(path));
654 PLSave(style, NO);
655 } else {
656 if (style_file) {
657 val = PLMakeString(style_file);
658 PLSetFilename(style, val);
659 PLSave(style, NO);
660 } else {
661 puts(PLGetDescriptionIndent(style, 0));
664 exit(0);