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,
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",
105 /* table of style related fonts */
107 static char *font_options
[] = {
118 #define DEFAULT_FONT "sans-serif:pixelsize=12"
122 WMPropList
*PixmapPath
= NULL
;
124 char *ThemePath
= NULL
;
126 extern char* convertFont(char *font
, Bool keepXLFD
);
132 printf("Usage: %s [OPTIONS] [FILE]\n", ProgName
);
133 puts("Retrieves style/theme configuration and output to FILE or to stdout");
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");
143 globalDefaultsPathForDomain(char *domain
)
145 static char path
[1024];
147 sprintf(path
, "%s/WindowMaker/%s", SYSCONFDIR
, domain
);
154 defaultsPathForDomain(char *domain
)
156 static char path
[1024];
159 gspath
= getenv("GNUSTEP_USER_ROOT");
161 strcpy(path
, gspath
);
166 home
= getenv("HOME");
168 printf("%s:could not get HOME environment variable!\n", ProgName
);
172 strcat(path
, "/GNUstep/");
174 strcat(path
, DEFAULTS_DIR
);
176 strcat(path
, domain
);
183 abortar(char *reason
)
187 printf("%s: %s\n", ProgName
, reason
);
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
);
206 char *home
= getenv("HOME");
212 user
= getpwuid(getuid());
216 sprintf(buffer
, "could not get password entry for UID %i", getuid());
229 getuserhomedir(char *username
)
233 user
= getpwnam(username
);
237 sprintf(buffer
,"could not get password entry for user %s", username
);
250 wexpandpath(char *path
)
252 char buffer2
[PATH_MAX
+2];
253 char buffer
[PATH_MAX
+2];
256 memset(buffer
, 0, PATH_MAX
+2);
262 if (*path
=='/' || *path
==0) {
263 home
= wgethomedir();
264 strcat(buffer
, home
);
268 while (*path
!=0 && *path
!='/') {
269 buffer2
[j
++] = *path
;
273 home
= getuserhomedir(buffer2
);
276 strcat(buffer
, home
);
288 /* expand $(HOME) or $HOME style environment variables */
291 while (*path
!=0 && *path
!=')') {
292 buffer2
[j
++] = *(path
++);
297 tmp
= getenv(buffer2
);
300 strcat(buffer
, "$(");
301 strcat(buffer
, buffer2
);
303 i
+= strlen(buffer2
)+3;
309 while (*path
!=0 && *path
!='/') {
310 buffer2
[j
++] = *(path
++);
313 tmp
= getenv(buffer2
);
316 strcat(buffer
, buffer2
);
317 i
+= strlen(buffer2
)+1;
329 return wstrdup(buffer
);
335 wfindfileinarray(WMPropList
*paths
, char *file
)
345 if (*file
=='/' || *file
=='~' || !paths
|| !WMIsPLArray(paths
)
346 || WMGetPropListItemCount(paths
)==0) {
347 if (access(file
, R_OK
)<0) {
348 fullpath
= wexpandpath(file
);
352 if (access(fullpath
, R_OK
)<0) {
359 return wstrdup(file
);
364 for (i
=0; i
< WMGetPropListItemCount(paths
); i
++) {
368 tmp
= WMGetFromPLArray(paths
, i
);
369 if (!WMIsPLString(tmp
) || !(dir
= WMGetFromPLString(tmp
)))
373 path
= wmalloc(len
+flen
+2);
374 path
= memcpy(path
, dir
, len
);
379 fullpath
= wexpandpath(path
);
382 /* check if file is readable */
383 if (access(fullpath
, R_OK
)==0) {
394 isFontOption(char *option
)
398 for (i
=0; font_options
[i
]!=NULL
; i
++) {
399 if (strcasecmp(option
, font_options
[i
])==0) {
409 copyFile(char *dir
, char *file
)
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
);
421 findCopyFile(char *dir
, char *file
)
425 fullPath
= wfindfileinarray(PixmapPath
, file
);
429 sprintf(buffer
, "could not find file %s", file
);
432 copyFile(dir
, fullPath
);
438 makeThemePack(WMPropList
*style
, char *themeName
)
446 themeDir
= wmalloc(strlen(themeName
)+50);
447 sprintf(themeDir
, "%s.themed", themeName
);
448 ThemePath
= themeDir
;
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
);
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) {
470 type
= WMGetFromPLArray(value
, 0);
471 t
= WMGetFromPLString(type
);
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) {
486 file
= WMGetFromPLArray(value
, 1);
488 p
= strrchr(WMGetFromPLString(file
), '/');
490 copyFile(themeDir
, WMGetFromPLString(file
));
492 newPath
= wstrdup(p
+1);
493 WMDeleteFromPLArray(value
, 1);
494 WMInsertInPLArray(value
, 1, WMCreatePLString(newPath
));
497 findCopyFile(themeDir
, WMGetFromPLString(file
));
499 } else if (strcasecmp(t
, "bitmap")==0) {
504 file
= WMGetFromPLArray(value
, 1);
506 p
= strrchr(WMGetFromPLString(file
), '/');
508 copyFile(themeDir
, WMGetFromPLString(file
));
510 newPath
= wstrdup(p
+1);
511 WMDeleteFromPLArray(value
, 1);
512 WMInsertInPLArray(value
, 1, WMCreatePLString(newPath
));
515 findCopyFile(themeDir
, WMGetFromPLString(file
));
519 file
= WMGetFromPLArray(value
, 2);
521 p
= strrchr(WMGetFromPLString(file
), '/');
523 copyFile(themeDir
, WMGetFromPLString(file
));
525 newPath
= wstrdup(p
+1);
526 WMDeleteFromPLArray(value
, 2);
527 WMInsertInPLArray(value
, 2, WMCreatePLString(newPath
));
530 findCopyFile(themeDir
, WMGetFromPLString(file
));
541 main(int argc
, char **argv
)
543 WMPropList
*prop
, *style
, *key
, *val
;
545 int i
, theme_too
=0, make_pack
=0;
546 char *style_file
= NULL
;
552 for (i
=1; i
<argc
; i
++) {
553 if (strcmp(argv
[i
], "-p")==0
554 || strcmp(argv
[i
], "--pack")==0) {
557 } else if (strcmp(argv
[i
], "-t")==0
558 || strcmp(argv
[i
], "--theme-options")==0) {
560 } else if (strcmp(argv
[i
], "--help")==0) {
563 } else if (strcmp(argv
[i
], "--version")==0) {
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]);
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
);
583 WMPLSetCaseSensitive(False
);
585 path
= defaultsPathForDomain("WindowMaker");
587 prop
= WMReadPropListFromFile(path
);
589 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
594 /* get global value */
595 path
= globalDefaultsPathForDomain("WindowMaker");
596 val
= WMReadPropListFromFile(path
);
598 WMMergePLDictionaries(val
, prop
, True
);
599 WMReleasePropList(prop
);
603 style
= WMCreatePLDictionary(NULL
, NULL
);
605 for (i
=0; options
[i
]!=NULL
; i
++) {
606 key
= WMCreatePLString(options
[i
]);
608 val
= WMGetFromPLDictionary(prop
, key
);
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
);
623 WMPutInPLDictionary(style
, key
, val
);
624 WMReleasePropList(val
);
626 WMReleasePropList(key
);
629 val
= WMGetFromPLDictionary(prop
, WMCreatePLString("PixmapPath"));
634 for (i
=0; theme_options
[i
]!=NULL
; i
++) {
635 key
= WMCreatePLString(theme_options
[i
]);
637 val
= WMGetFromPLDictionary(prop
, key
);
639 WMPutInPLDictionary(style
, key
, val
);
646 makeThemePack(style
, style_file
);
648 path
= wmalloc(strlen(ThemePath
)+32);
649 strcpy(path
, ThemePath
);
650 strcat(path
, "/style");
651 WMWritePropListToFile(style
, path
, False
);
655 WMWritePropListToFile(style
, style_file
, False
);
657 puts(WMGetPropListDescription(style
, True
));