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,
23 #define PROG_VERSION "getstyle (Window Maker) 0.6"
33 #include <WINGs/WUtil.h>
39 #include "../src/wconfig.h"
41 /* table of style related options */
42 static char *options[] = {
51 "WindowTitleExtendSpace",
52 "MenuTitleExtendSpace",
53 "MenuTextExtendSpace",
74 "WindowTitleExtendSpace",
75 "MenuTitleExtendSpace",
76 "MenuTextExtendSpace",
80 /* table of theme related options */
81 static char *theme_options[] = {
87 "TopLeftResizeCursor",
88 "TopRightResizeCursor",
89 "BottomLeftResizeCursor",
90 "BottomRightResizeCursor",
91 "VerticalResizeCursor",
92 "HorizontalResizeCursor",
100 /* table of style related fonts */
102 static char *font_options[] = {
115 WMPropList *PixmapPath = NULL;
117 char *ThemePath = NULL;
119 extern char *convertFont(char *font, Bool keepXLFD);
123 printf("Usage: %s [OPTIONS] [FILE]\n", ProgName);
124 puts("Retrieves style/theme configuration and output to FILE or to stdout");
126 puts(" -t, --theme-options output theme related options when producing a style file");
127 puts(" -p, --pack produce output as a theme pack");
128 puts(" --help display this help and exit");
129 puts(" --version output version information and exit");
132 char *globalDefaultsPathForDomain(char *domain)
134 static char path[1024];
136 sprintf(path, "%s/WindowMaker/%s", SYSCONFDIR, domain);
141 char *defaultsPathForDomain(char *domain)
143 static char path[1024];
146 gspath = getenv("GNUSTEP_USER_ROOT");
148 strcpy(path, gspath);
153 home = getenv("HOME");
155 printf("%s:could not get HOME environment variable!\n", ProgName);
159 strcat(path, "/GNUstep/");
161 strcat(path, DEFAULTS_DIR);
163 strcat(path, domain);
168 void abortar(char *reason)
172 printf("%s: %s\n", ProgName, reason);
175 printf("Removing unfinished theme pack\n");
176 sprintf(buffer, "/bin/rm -fr \"%s\"", ThemePath);
178 if (system(buffer) != 0) {
179 printf("%s: could not execute command %s\n", ProgName, buffer);
187 char *home = getenv("HOME");
193 user = getpwuid(getuid());
197 sprintf(buffer, "could not get password entry for UID %i", getuid());
208 static char *getuserhomedir(char *username)
212 user = getpwnam(username);
216 sprintf(buffer, "could not get password entry for user %s", username);
227 char *wexpandpath(char *path)
229 char buffer2[PATH_MAX + 2];
230 char buffer[PATH_MAX + 2];
233 memset(buffer, 0, PATH_MAX + 2);
239 if (*path == '/' || *path == 0) {
240 home = wgethomedir();
241 strcat(buffer, home);
245 while (*path != 0 && *path != '/') {
246 buffer2[j++] = *path;
250 home = getuserhomedir(buffer2);
253 strcat(buffer, home);
265 /* expand $(HOME) or $HOME style environment variables */
268 while (*path != 0 && *path != ')') {
269 buffer2[j++] = *(path++);
274 tmp = getenv(buffer2);
277 strcat(buffer, "$(");
278 strcat(buffer, buffer2);
280 i += strlen(buffer2) + 3;
286 while (*path != 0 && *path != '/') {
287 buffer2[j++] = *(path++);
290 tmp = getenv(buffer2);
293 strcat(buffer, buffer2);
294 i += strlen(buffer2) + 1;
306 return wstrdup(buffer);
309 char *wfindfileinarray(WMPropList * paths, char *file)
319 if (*file == '/' || *file == '~' || !paths || !WMIsPLArray(paths)
320 || WMGetPropListItemCount(paths) == 0) {
321 if (access(file, R_OK) < 0) {
322 fullpath = wexpandpath(file);
326 if (access(fullpath, R_OK) < 0) {
333 return wstrdup(file);
338 for (i = 0; i < WMGetPropListItemCount(paths); i++) {
342 tmp = WMGetFromPLArray(paths, i);
343 if (!WMIsPLString(tmp) || !(dir = WMGetFromPLString(tmp)))
347 path = wmalloc(len + flen + 2);
348 path = memcpy(path, dir, len);
353 fullpath = wexpandpath(path);
356 /* check if file is readable */
357 if (access(fullpath, R_OK) == 0) {
366 static Bool isFontOption(char *option)
370 for (i = 0; font_options[i] != NULL; i++) {
371 if (strcasecmp(option, font_options[i]) == 0) {
379 void copyFile(char *dir, char *file)
383 sprintf(buffer, "/bin/cp \"%s\" \"%s\"", file, dir);
384 if (system(buffer) != 0) {
385 printf("%s: could not copy file %s\n", ProgName, file);
389 void findCopyFile(char *dir, char *file)
393 fullPath = wfindfileinarray(PixmapPath, file);
397 sprintf(buffer, "could not find file %s", file);
400 copyFile(dir, fullPath);
404 char *makeThemePack(WMPropList * style, char *themeName)
412 themeDir = wmalloc(strlen(themeName) + 50);
413 sprintf(themeDir, "%s.themed", themeName);
414 ThemePath = themeDir;
418 tmp = wmalloc(strlen(themeDir) + 20);
419 sprintf(tmp, "/bin/mkdir \"%s\"", themeDir);
420 if (system(tmp) != 0) {
422 ("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n",
428 keys = WMGetPLDictionaryKeys(style);
430 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
431 key = WMGetFromPLArray(keys, i);
433 value = WMGetFromPLDictionary(style, key);
434 if (value && WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
438 type = WMGetFromPLArray(value, 0);
439 t = WMGetFromPLString(type);
443 if (strcasecmp(t, "tpixmap") == 0
444 || strcasecmp(t, "spixmap") == 0
445 || strcasecmp(t, "cpixmap") == 0
446 || strcasecmp(t, "mpixmap") == 0
447 || strcasecmp(t, "tdgradient") == 0
448 || strcasecmp(t, "tvgradient") == 0 || strcasecmp(t, "thgradient") == 0) {
453 file = WMGetFromPLArray(value, 1);
455 p = strrchr(WMGetFromPLString(file), '/');
457 copyFile(themeDir, WMGetFromPLString(file));
459 newPath = wstrdup(p + 1);
460 WMDeleteFromPLArray(value, 1);
461 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
464 findCopyFile(themeDir, WMGetFromPLString(file));
466 } else if (strcasecmp(t, "bitmap") == 0) {
471 file = WMGetFromPLArray(value, 1);
473 p = strrchr(WMGetFromPLString(file), '/');
475 copyFile(themeDir, WMGetFromPLString(file));
477 newPath = wstrdup(p + 1);
478 WMDeleteFromPLArray(value, 1);
479 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
482 findCopyFile(themeDir, WMGetFromPLString(file));
485 file = WMGetFromPLArray(value, 2);
487 p = strrchr(WMGetFromPLString(file), '/');
489 copyFile(themeDir, WMGetFromPLString(file));
491 newPath = wstrdup(p + 1);
492 WMDeleteFromPLArray(value, 2);
493 WMInsertInPLArray(value, 2, WMCreatePLString(newPath));
496 findCopyFile(themeDir, WMGetFromPLString(file));
505 int main(int argc, char **argv)
507 WMPropList *prop, *style, *key, *val;
509 int i, theme_too = 0, make_pack = 0;
510 char *style_file = NULL;
515 for (i = 1; i < argc; i++) {
516 if (strcmp(argv[i], "-p") == 0 || strcmp(argv[i], "--pack") == 0) {
519 } else if (strcmp(argv[i], "-t") == 0 || strcmp(argv[i], "--theme-options") == 0) {
521 } else if (strcmp(argv[i], "--help") == 0) {
524 } else if (strcmp(argv[i], "--version") == 0) {
528 if (style_file != NULL) {
529 printf("%s: invalid argument '%s'\n", argv[0],
530 style_file[0] == '-' ? style_file : argv[i]);
531 printf("Try '%s --help' for more information\n", argv[0]);
534 style_file = argv[i];
539 if (make_pack && !style_file) {
540 printf("%s: you must supply a name for the theme pack\n", ProgName);
544 WMPLSetCaseSensitive(False);
546 path = defaultsPathForDomain("WindowMaker");
548 prop = WMReadPropListFromFile(path);
550 printf("%s:could not load WindowMaker configuration file \"%s\".\n", ProgName, path);
554 /* get global value */
555 path = globalDefaultsPathForDomain("WindowMaker");
556 val = WMReadPropListFromFile(path);
558 WMMergePLDictionaries(val, prop, True);
559 WMReleasePropList(prop);
563 style = WMCreatePLDictionary(NULL, NULL);
565 for (i = 0; options[i] != NULL; i++) {
566 key = WMCreatePLString(options[i]);
568 val = WMGetFromPLDictionary(prop, key);
570 WMRetainPropList(val);
571 if (isFontOption(options[i])) {
572 char *newfont, *oldfont;
574 oldfont = WMGetFromPLString(val);
575 newfont = convertFont(oldfont, False);
576 /* newfont is a reference to old if conversion is not needed */
577 if (newfont != oldfont) {
578 WMReleasePropList(val);
579 val = WMCreatePLString(newfont);
583 WMPutInPLDictionary(style, key, val);
584 WMReleasePropList(val);
586 WMReleasePropList(key);
589 val = WMGetFromPLDictionary(prop, WMCreatePLString("PixmapPath"));
594 for (i = 0; theme_options[i] != NULL; i++) {
595 key = WMCreatePLString(theme_options[i]);
597 val = WMGetFromPLDictionary(prop, key);
599 WMPutInPLDictionary(style, key, val);
606 makeThemePack(style, style_file);
608 path = wmalloc(strlen(ThemePath) + 32);
609 strcpy(path, ThemePath);
610 strcat(path, "/style");
611 WMWritePropListToFile(style, path, False);
615 WMWritePropListToFile(style, style_file, False);
617 puts(WMGetPropListDescription(style, True));