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 #ifndef GLOBAL_DEFAULTS_SUBDIR
42 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
45 /* table of style related options */
46 static char *options
[] = {
55 "WindowTitleExtendSpace",
56 "MenuTitleExtendSpace",
57 "MenuTextExtendSpace",
78 "WindowTitleExtendSpace",
79 "MenuTitleExtendSpace",
80 "MenuTextExtendSpace",
84 /* table of theme related options */
85 static char *theme_options
[] = {
91 "TopLeftResizeCursor",
92 "TopRightResizeCursor",
93 "BottomLeftResizeCursor",
94 "BottomRightResizeCursor",
95 "VerticalResizeCursor",
96 "HorizontalResizeCursor",
104 /* table of style related fonts */
106 static char *font_options
[] = {
119 WMPropList
*PixmapPath
= NULL
;
121 char *ThemePath
= NULL
;
123 extern char *convertFont(char *font
, Bool keepXLFD
);
127 printf("Usage: %s [OPTIONS] [FILE]\n", ProgName
);
128 puts("Retrieves style/theme configuration and output to FILE or to stdout");
130 puts(" -t, --theme-options output theme related options when producing a style file");
131 puts(" -p, --pack produce output as a theme pack");
132 puts(" --help display this help and exit");
133 puts(" --version output version information and exit");
136 char *globalDefaultsPathForDomain(char *domain
)
138 static char path
[1024];
140 sprintf(path
, "%s/%s/%s", SYSCONFDIR
, GLOBAL_DEFAULTS_SUBDIR
, domain
);
145 char *defaultsPathForDomain(char *domain
)
147 static char path
[1024];
150 gspath
= getenv("GNUSTEP_USER_ROOT");
152 strcpy(path
, gspath
);
157 home
= getenv("HOME");
159 printf("%s:could not get HOME environment variable!\n", ProgName
);
163 strcat(path
, "/GNUstep/");
165 strcat(path
, DEFAULTS_DIR
);
167 strcat(path
, domain
);
172 void abortar(char *reason
)
176 printf("%s: %s\n", ProgName
, reason
);
179 printf("Removing unfinished theme pack\n");
180 sprintf(buffer
, "/bin/rm -fr \"%s\"", ThemePath
);
182 if (system(buffer
) != 0) {
183 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());
212 static char *getuserhomedir(char *username
)
216 user
= getpwnam(username
);
220 sprintf(buffer
, "could not get password entry for user %s", username
);
231 char *wexpandpath(char *path
)
233 char buffer2
[PATH_MAX
+ 2];
234 char buffer
[PATH_MAX
+ 2];
237 memset(buffer
, 0, PATH_MAX
+ 2);
243 if (*path
== '/' || *path
== 0) {
244 home
= wgethomedir();
245 strcat(buffer
, home
);
249 while (*path
!= 0 && *path
!= '/') {
250 buffer2
[j
++] = *path
;
254 home
= getuserhomedir(buffer2
);
257 strcat(buffer
, home
);
269 /* expand $(HOME) or $HOME style environment variables */
272 while (*path
!= 0 && *path
!= ')') {
273 buffer2
[j
++] = *(path
++);
278 tmp
= getenv(buffer2
);
281 strcat(buffer
, "$(");
282 strcat(buffer
, buffer2
);
284 i
+= strlen(buffer2
) + 3;
290 while (*path
!= 0 && *path
!= '/') {
291 buffer2
[j
++] = *(path
++);
294 tmp
= getenv(buffer2
);
297 strcat(buffer
, buffer2
);
298 i
+= strlen(buffer2
) + 1;
310 return wstrdup(buffer
);
313 char *wfindfileinarray(WMPropList
* paths
, char *file
)
323 if (*file
== '/' || *file
== '~' || !paths
|| !WMIsPLArray(paths
)
324 || WMGetPropListItemCount(paths
) == 0) {
325 if (access(file
, R_OK
) < 0) {
326 fullpath
= wexpandpath(file
);
330 if (access(fullpath
, R_OK
) < 0) {
337 return wstrdup(file
);
342 for (i
= 0; i
< WMGetPropListItemCount(paths
); i
++) {
346 tmp
= WMGetFromPLArray(paths
, i
);
347 if (!WMIsPLString(tmp
) || !(dir
= WMGetFromPLString(tmp
)))
351 path
= wmalloc(len
+ flen
+ 2);
352 path
= memcpy(path
, dir
, len
);
357 fullpath
= wexpandpath(path
);
360 /* check if file is readable */
361 if (access(fullpath
, R_OK
) == 0) {
370 static Bool
isFontOption(char *option
)
374 for (i
= 0; font_options
[i
] != NULL
; i
++) {
375 if (strcasecmp(option
, font_options
[i
]) == 0) {
383 void copyFile(char *dir
, char *file
)
387 sprintf(buffer
, "/bin/cp \"%s\" \"%s\"", file
, dir
);
388 if (system(buffer
) != 0) {
389 printf("%s: could not copy file %s\n", ProgName
, file
);
393 void findCopyFile(char *dir
, char *file
)
397 fullPath
= wfindfileinarray(PixmapPath
, file
);
401 sprintf(buffer
, "could not find file %s", file
);
404 copyFile(dir
, fullPath
);
408 char *makeThemePack(WMPropList
* style
, char *themeName
)
416 themeDir
= wmalloc(strlen(themeName
) + 50);
417 sprintf(themeDir
, "%s.themed", themeName
);
418 ThemePath
= themeDir
;
422 tmp
= wmalloc(strlen(themeDir
) + 20);
423 sprintf(tmp
, "/bin/mkdir \"%s\"", themeDir
);
424 if (system(tmp
) != 0) {
426 ("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n",
432 keys
= WMGetPLDictionaryKeys(style
);
434 for (i
= 0; i
< WMGetPropListItemCount(keys
); i
++) {
435 key
= WMGetFromPLArray(keys
, i
);
437 value
= WMGetFromPLDictionary(style
, key
);
438 if (value
&& WMIsPLArray(value
) && WMGetPropListItemCount(value
) > 2) {
442 type
= WMGetFromPLArray(value
, 0);
443 t
= WMGetFromPLString(type
);
447 if (strcasecmp(t
, "tpixmap") == 0
448 || strcasecmp(t
, "spixmap") == 0
449 || strcasecmp(t
, "cpixmap") == 0
450 || strcasecmp(t
, "mpixmap") == 0
451 || strcasecmp(t
, "tdgradient") == 0
452 || strcasecmp(t
, "tvgradient") == 0 || strcasecmp(t
, "thgradient") == 0) {
457 file
= WMGetFromPLArray(value
, 1);
459 p
= strrchr(WMGetFromPLString(file
), '/');
461 copyFile(themeDir
, WMGetFromPLString(file
));
463 newPath
= wstrdup(p
+ 1);
464 WMDeleteFromPLArray(value
, 1);
465 WMInsertInPLArray(value
, 1, WMCreatePLString(newPath
));
468 findCopyFile(themeDir
, WMGetFromPLString(file
));
470 } else if (strcasecmp(t
, "bitmap") == 0) {
475 file
= WMGetFromPLArray(value
, 1);
477 p
= strrchr(WMGetFromPLString(file
), '/');
479 copyFile(themeDir
, WMGetFromPLString(file
));
481 newPath
= wstrdup(p
+ 1);
482 WMDeleteFromPLArray(value
, 1);
483 WMInsertInPLArray(value
, 1, WMCreatePLString(newPath
));
486 findCopyFile(themeDir
, WMGetFromPLString(file
));
489 file
= WMGetFromPLArray(value
, 2);
491 p
= strrchr(WMGetFromPLString(file
), '/');
493 copyFile(themeDir
, WMGetFromPLString(file
));
495 newPath
= wstrdup(p
+ 1);
496 WMDeleteFromPLArray(value
, 2);
497 WMInsertInPLArray(value
, 2, WMCreatePLString(newPath
));
500 findCopyFile(themeDir
, WMGetFromPLString(file
));
509 int main(int argc
, char **argv
)
511 WMPropList
*prop
, *style
, *key
, *val
;
513 int i
, theme_too
= 0, make_pack
= 0;
514 char *style_file
= NULL
;
519 for (i
= 1; i
< argc
; i
++) {
520 if (strcmp(argv
[i
], "-p") == 0 || strcmp(argv
[i
], "--pack") == 0) {
523 } else if (strcmp(argv
[i
], "-t") == 0 || strcmp(argv
[i
], "--theme-options") == 0) {
525 } else if (strcmp(argv
[i
], "--help") == 0) {
528 } else if (strcmp(argv
[i
], "--version") == 0) {
532 if (style_file
!= NULL
) {
533 printf("%s: invalid argument '%s'\n", argv
[0],
534 style_file
[0] == '-' ? style_file
: argv
[i
]);
535 printf("Try '%s --help' for more information\n", argv
[0]);
538 style_file
= argv
[i
];
543 if (make_pack
&& !style_file
) {
544 printf("%s: you must supply a name for the theme pack\n", ProgName
);
548 WMPLSetCaseSensitive(False
);
550 path
= defaultsPathForDomain("WindowMaker");
552 prop
= WMReadPropListFromFile(path
);
554 printf("%s:could not load WindowMaker configuration file \"%s\".\n", ProgName
, path
);
558 /* get global value */
559 path
= globalDefaultsPathForDomain("WindowMaker");
560 val
= WMReadPropListFromFile(path
);
562 WMMergePLDictionaries(val
, prop
, True
);
563 WMReleasePropList(prop
);
567 style
= WMCreatePLDictionary(NULL
, NULL
);
569 for (i
= 0; options
[i
] != NULL
; i
++) {
570 key
= WMCreatePLString(options
[i
]);
572 val
= WMGetFromPLDictionary(prop
, key
);
574 WMRetainPropList(val
);
575 if (isFontOption(options
[i
])) {
576 char *newfont
, *oldfont
;
578 oldfont
= WMGetFromPLString(val
);
579 newfont
= convertFont(oldfont
, False
);
580 /* newfont is a reference to old if conversion is not needed */
581 if (newfont
!= oldfont
) {
582 WMReleasePropList(val
);
583 val
= WMCreatePLString(newfont
);
587 WMPutInPLDictionary(style
, key
, val
);
588 WMReleasePropList(val
);
590 WMReleasePropList(key
);
593 val
= WMGetFromPLDictionary(prop
, WMCreatePLString("PixmapPath"));
598 for (i
= 0; theme_options
[i
] != NULL
; i
++) {
599 key
= WMCreatePLString(theme_options
[i
]);
601 val
= WMGetFromPLDictionary(prop
, key
);
603 WMPutInPLDictionary(style
, key
, val
);
610 makeThemePack(style
, style_file
);
612 path
= wmalloc(strlen(ThemePath
) + 32);
613 strcpy(path
, ThemePath
);
614 strcat(path
, "/style");
615 WMWritePropListToFile(style
, path
);
619 WMWritePropListToFile(style
, style_file
);
621 puts(WMGetPropListDescription(style
, True
));