1 /* getstyle.c - outputs style related options from WindowMaker to stdout
3 * WindowMaker window manager
5 * Copyright (c) 1997-2002 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,
24 #define PROG_VERSION "getstyle (Window Maker) 0.6"
36 #include <WINGs/WUtil.h>
42 #include "../src/wconfig.h"
44 /* table of style related options */
45 static char *options
[] = {
54 "WindowTitleExtendSpace",
55 "MenuTitleExtendSpace",
56 "MenuTextExtendSpace",
77 "WindowTitleExtendSpace",
78 "MenuTitleExtendSpace",
79 "MenuTextExtendSpace",
84 /* table of theme related options */
85 static char *theme_options
[] = {
91 "TopLeftResizeCursor",
92 "TopRightResizeCursor",
93 "BottomLeftResizeCursor",
94 "BottomRightResizeCursor",
95 "VerticalResizeCursor",
96 "HorizontalResizeCursor",
109 WMPropList
*PixmapPath
= NULL
;
111 char *ThemePath
= NULL
;
117 printf("Usage: %s [OPTIONS] [FILE]\n", ProgName
);
118 puts("Retrieves style/theme configuration and output to FILE or to stdout");
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");
128 globalDefaultsPathForDomain(char *domain
)
130 static char path
[1024];
132 sprintf(path
, "%s/WindowMaker/%s", SYSCONFDIR
, domain
);
139 defaultsPathForDomain(char *domain
)
141 static char path
[1024];
144 gspath
= getenv("GNUSTEP_USER_ROOT");
146 strcpy(path
, gspath
);
151 home
= getenv("HOME");
153 printf("%s:could not get HOME environment variable!\n", ProgName
);
157 strcat(path
, "/GNUstep/");
159 strcat(path
, DEFAULTS_DIR
);
161 strcat(path
, domain
);
168 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
);
191 char *home
= getenv("HOME");
197 user
= getpwuid(getuid());
201 sprintf(buffer
, "could not get password entry for UID %i", getuid());
220 abortar("out of memory");
232 tmp
= mymalloc(strlen(str
)+1);
241 getuserhomedir(char *username
)
245 user
= getpwnam(username
);
249 sprintf(buffer
,"could not get password entry for user %s", username
);
264 wexpandpath(char *path
)
266 char buffer2
[PATH_MAX
+2];
267 char buffer
[PATH_MAX
+2];
270 memset(buffer
, 0, PATH_MAX
+2);
276 if (*path
=='/' || *path
==0) {
277 home
= wgethomedir();
278 strcat(buffer
, home
);
282 while (*path
!=0 && *path
!='/') {
283 buffer2
[j
++] = *path
;
287 home
= getuserhomedir(buffer2
);
290 strcat(buffer
, home
);
302 /* expand $(HOME) or $HOME style environment variables */
305 while (*path
!=0 && *path
!=')') {
306 buffer2
[j
++] = *(path
++);
311 tmp
= getenv(buffer2
);
314 strcat(buffer
, "$(");
315 strcat(buffer
, buffer2
);
317 i
+= strlen(buffer2
)+3;
323 while (*path
!=0 && *path
!='/') {
324 buffer2
[j
++] = *(path
++);
327 tmp
= getenv(buffer2
);
330 strcat(buffer
, buffer2
);
331 i
+= strlen(buffer2
)+1;
343 return mystrdup(buffer
);
349 wfindfileinarray(WMPropList
*paths
, char *file
)
359 if (*file
=='/' || *file
=='~' || !paths
|| !WMIsPLArray(paths
)
360 || WMGetPropListItemCount(paths
)==0) {
361 if (access(file
, R_OK
)<0) {
362 fullpath
= wexpandpath(file
);
366 if (access(fullpath
, R_OK
)<0) {
373 return mystrdup(file
);
378 for (i
=0; i
< WMGetPropListItemCount(paths
); i
++) {
382 tmp
= WMGetFromPLArray(paths
, i
);
383 if (!WMIsPLString(tmp
) || !(dir
= WMGetFromPLString(tmp
)))
387 path
= mymalloc(len
+flen
+2);
388 path
= memcpy(path
, dir
, len
);
393 fullpath
= wexpandpath(path
);
396 /* check if file is readable */
397 if (access(fullpath
, R_OK
)==0) {
410 copyFile(char *dir
, char *file
)
414 sprintf(buffer
, "/bin/cp \"%s\" \"%s\"", file
, dir
);
415 if (system(buffer
)!=0) {
416 printf("%s: could not copy file %s\n", ProgName
, file
);
422 findCopyFile(char *dir
, char *file
)
426 fullPath
= wfindfileinarray(PixmapPath
, file
);
430 sprintf(buffer
, "coould not find file %s", file
);
433 copyFile(dir
, fullPath
);
439 makeThemePack(WMPropList
*style
, char *themeName
)
447 themeDir
= mymalloc(strlen(themeName
)+50);
448 sprintf(themeDir
, "%s.themed", themeName
);
449 ThemePath
= themeDir
;
453 tmp
= mymalloc(strlen(themeDir
)+20);
454 sprintf(tmp
, "/bin/mkdir \"%s\"", themeDir
);
455 if (system(tmp
)!=0) {
456 printf("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n", ProgName
, themeDir
);
461 keys
= WMGetPLDictionaryKeys(style
);
463 for (i
= 0; i
< WMGetPropListItemCount(keys
); i
++) {
464 key
= WMGetFromPLArray(keys
, i
);
466 value
= WMGetFromPLDictionary(style
, key
);
467 if (value
&& WMIsPLArray(value
) && WMGetPropListItemCount(value
) > 2) {
471 type
= WMGetFromPLArray(value
, 0);
472 t
= WMGetFromPLString(type
);
476 if (strcasecmp(t
, "tpixmap")==0
477 || strcasecmp(t
, "spixmap")==0
478 || strcasecmp(t
, "cpixmap")==0
479 || strcasecmp(t
, "mpixmap")==0
480 || strcasecmp(t
, "tdgradient")==0
481 || strcasecmp(t
, "tvgradient")==0
482 || strcasecmp(t
, "thgradient")==0) {
487 file
= WMGetFromPLArray(value
, 1);
489 p
= strrchr(WMGetFromPLString(file
), '/');
491 copyFile(themeDir
, WMGetFromPLString(file
));
493 newPath
= mystrdup(p
+1);
494 WMDeleteFromPLArray(value
, 1);
495 WMInsertInPLArray(value
, 1, WMCreatePLString(newPath
));
498 findCopyFile(themeDir
, WMGetFromPLString(file
));
500 } else if (strcasecmp(t
, "bitmap")==0) {
505 file
= WMGetFromPLArray(value
, 1);
507 p
= strrchr(WMGetFromPLString(file
), '/');
509 copyFile(themeDir
, WMGetFromPLString(file
));
511 newPath
= mystrdup(p
+1);
512 WMDeleteFromPLArray(value
, 1);
513 WMInsertInPLArray(value
, 1, WMCreatePLString(newPath
));
516 findCopyFile(themeDir
, WMGetFromPLString(file
));
520 file
= WMGetFromPLArray(value
, 2);
522 p
= strrchr(WMGetFromPLString(file
), '/');
524 copyFile(themeDir
, WMGetFromPLString(file
));
526 newPath
= mystrdup(p
+1);
527 WMDeleteFromPLArray(value
, 2);
528 WMInsertInPLArray(value
, 2, WMCreatePLString(newPath
));
531 findCopyFile(themeDir
, WMGetFromPLString(file
));
542 main(int argc
, char **argv
)
544 WMPropList
*prop
, *style
, *key
, *val
;
548 char *style_file
= NULL
;
554 for (i
=1; i
<argc
; i
++) {
555 if (strcmp(argv
[i
], "-p")==0
556 || strcmp(argv
[i
], "--pack")==0) {
559 } else if (strcmp(argv
[i
], "-t")==0
560 || strcmp(argv
[i
], "--theme-options")==0) {
562 } else if (strcmp(argv
[i
], "--help")==0) {
565 } else if (strcmp(argv
[i
], "--version")==0) {
569 if (style_file
!=NULL
) {
570 printf("%s: invalid argument '%s'\n", argv
[0],
571 style_file
[0]=='-' ? style_file
: argv
[i
]);
572 printf("Try '%s --help' for more information\n", argv
[0]);
575 style_file
= argv
[i
];
580 if (make_pack
&& !style_file
) {
581 printf("%s: you must supply a name for the theme pack\n", ProgName
);
585 WMPLSetCaseSensitive(False
);
587 path
= defaultsPathForDomain("WindowMaker");
589 prop
= WMReadPropListFromFile(path
);
591 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
596 /* get global value */
597 path
= globalDefaultsPathForDomain("WindowMaker");
598 val
= WMReadPropListFromFile(path
);
600 WMMergePLDictionaries(val
, prop
, True
);
601 WMReleasePropList(prop
);
605 style
= WMCreatePLDictionary(NULL
, NULL
, NULL
);
608 for (i
=0; options
[i
]!=NULL
; i
++) {
609 key
= WMCreatePLString(options
[i
]);
611 val
= WMGetFromPLDictionary(prop
, key
);
613 WMPutInPLDictionary(style
, key
, val
);
616 val
= WMGetFromPLDictionary(prop
, WMCreatePLString("PixmapPath"));
621 for (i
=0; theme_options
[i
]!=NULL
; i
++) {
622 key
= WMCreatePLString(theme_options
[i
]);
624 val
= WMGetFromPLDictionary(prop
, key
);
626 WMPutInPLDictionary(style
, key
, val
);
633 makeThemePack(style
, style_file
);
635 path
= mymalloc(strlen(ThemePath
)+32);
636 strcpy(path
, ThemePath
);
637 strcat(path
, "/style");
638 WMWritePropListToFile(style
, path
, False
);
642 WMWritePropListToFile(style
, style_file
, False
);
644 puts(WMGetPropListDescription(style
, True
));