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
[] = {
120 WMPropList
*PixmapPath
= NULL
;
122 char *ThemePath
= NULL
;
124 extern char* convertFont(char *font
, Bool keepXLFD
);
130 printf("Usage: %s [OPTIONS] [FILE]\n", ProgName
);
131 puts("Retrieves style/theme configuration and output to FILE or to stdout");
133 puts(" -t, --theme-options output theme related options when producing a style file");
134 puts(" -p, --pack produce output as a theme pack");
135 puts(" --help display this help and exit");
136 puts(" --version output version information and exit");
141 globalDefaultsPathForDomain(char *domain
)
143 static char path
[1024];
145 sprintf(path
, "%s/WindowMaker/%s", SYSCONFDIR
, domain
);
152 defaultsPathForDomain(char *domain
)
154 static char path
[1024];
157 gspath
= getenv("GNUSTEP_USER_ROOT");
159 strcpy(path
, gspath
);
164 home
= getenv("HOME");
166 printf("%s:could not get HOME environment variable!\n", ProgName
);
170 strcat(path
, "/GNUstep/");
172 strcat(path
, DEFAULTS_DIR
);
174 strcat(path
, domain
);
181 abortar(char *reason
)
185 printf("%s: %s\n", ProgName
, reason
);
188 printf("Removing unfinished theme pack\n");
189 sprintf(buffer
, "/bin/rm -fr \"%s\"", ThemePath
);
191 if (system(buffer
)!=0) {
192 printf("%s: could not execute command %s\n", ProgName
, buffer
);
204 char *home
= getenv("HOME");
210 user
= getpwuid(getuid());
214 sprintf(buffer
, "could not get password entry for UID %i", getuid());
227 getuserhomedir(char *username
)
231 user
= getpwnam(username
);
235 sprintf(buffer
,"could not get password entry for user %s", username
);
248 wexpandpath(char *path
)
250 char buffer2
[PATH_MAX
+2];
251 char buffer
[PATH_MAX
+2];
254 memset(buffer
, 0, PATH_MAX
+2);
260 if (*path
=='/' || *path
==0) {
261 home
= wgethomedir();
262 strcat(buffer
, home
);
266 while (*path
!=0 && *path
!='/') {
267 buffer2
[j
++] = *path
;
271 home
= getuserhomedir(buffer2
);
274 strcat(buffer
, home
);
286 /* expand $(HOME) or $HOME style environment variables */
289 while (*path
!=0 && *path
!=')') {
290 buffer2
[j
++] = *(path
++);
295 tmp
= getenv(buffer2
);
298 strcat(buffer
, "$(");
299 strcat(buffer
, buffer2
);
301 i
+= strlen(buffer2
)+3;
307 while (*path
!=0 && *path
!='/') {
308 buffer2
[j
++] = *(path
++);
311 tmp
= getenv(buffer2
);
314 strcat(buffer
, buffer2
);
315 i
+= strlen(buffer2
)+1;
327 return wstrdup(buffer
);
333 wfindfileinarray(WMPropList
*paths
, char *file
)
343 if (*file
=='/' || *file
=='~' || !paths
|| !WMIsPLArray(paths
)
344 || WMGetPropListItemCount(paths
)==0) {
345 if (access(file
, R_OK
)<0) {
346 fullpath
= wexpandpath(file
);
350 if (access(fullpath
, R_OK
)<0) {
357 return wstrdup(file
);
362 for (i
=0; i
< WMGetPropListItemCount(paths
); i
++) {
366 tmp
= WMGetFromPLArray(paths
, i
);
367 if (!WMIsPLString(tmp
) || !(dir
= WMGetFromPLString(tmp
)))
371 path
= wmalloc(len
+flen
+2);
372 path
= memcpy(path
, dir
, len
);
377 fullpath
= wexpandpath(path
);
380 /* check if file is readable */
381 if (access(fullpath
, R_OK
)==0) {
392 isFontOption(char *option
)
396 for (i
=0; font_options
[i
]!=NULL
; i
++) {
397 if (strcasecmp(option
, font_options
[i
])==0) {
407 copyFile(char *dir
, char *file
)
411 sprintf(buffer
, "/bin/cp \"%s\" \"%s\"", file
, dir
);
412 if (system(buffer
)!=0) {
413 printf("%s: could not copy file %s\n", ProgName
, file
);
419 findCopyFile(char *dir
, char *file
)
423 fullPath
= wfindfileinarray(PixmapPath
, file
);
427 sprintf(buffer
, "could not find file %s", file
);
430 copyFile(dir
, fullPath
);
436 makeThemePack(WMPropList
*style
, char *themeName
)
444 themeDir
= wmalloc(strlen(themeName
)+50);
445 sprintf(themeDir
, "%s.themed", themeName
);
446 ThemePath
= themeDir
;
450 tmp
= wmalloc(strlen(themeDir
)+20);
451 sprintf(tmp
, "/bin/mkdir \"%s\"", themeDir
);
452 if (system(tmp
)!=0) {
453 printf("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n", ProgName
, themeDir
);
458 keys
= WMGetPLDictionaryKeys(style
);
460 for (i
= 0; i
< WMGetPropListItemCount(keys
); i
++) {
461 key
= WMGetFromPLArray(keys
, i
);
463 value
= WMGetFromPLDictionary(style
, key
);
464 if (value
&& WMIsPLArray(value
) && WMGetPropListItemCount(value
) > 2) {
468 type
= WMGetFromPLArray(value
, 0);
469 t
= WMGetFromPLString(type
);
473 if (strcasecmp(t
, "tpixmap")==0
474 || strcasecmp(t
, "spixmap")==0
475 || strcasecmp(t
, "cpixmap")==0
476 || strcasecmp(t
, "mpixmap")==0
477 || strcasecmp(t
, "tdgradient")==0
478 || strcasecmp(t
, "tvgradient")==0
479 || strcasecmp(t
, "thgradient")==0) {
484 file
= WMGetFromPLArray(value
, 1);
486 p
= strrchr(WMGetFromPLString(file
), '/');
488 copyFile(themeDir
, WMGetFromPLString(file
));
490 newPath
= wstrdup(p
+1);
491 WMDeleteFromPLArray(value
, 1);
492 WMInsertInPLArray(value
, 1, WMCreatePLString(newPath
));
495 findCopyFile(themeDir
, WMGetFromPLString(file
));
497 } else if (strcasecmp(t
, "bitmap")==0) {
502 file
= WMGetFromPLArray(value
, 1);
504 p
= strrchr(WMGetFromPLString(file
), '/');
506 copyFile(themeDir
, WMGetFromPLString(file
));
508 newPath
= wstrdup(p
+1);
509 WMDeleteFromPLArray(value
, 1);
510 WMInsertInPLArray(value
, 1, WMCreatePLString(newPath
));
513 findCopyFile(themeDir
, WMGetFromPLString(file
));
517 file
= WMGetFromPLArray(value
, 2);
519 p
= strrchr(WMGetFromPLString(file
), '/');
521 copyFile(themeDir
, WMGetFromPLString(file
));
523 newPath
= wstrdup(p
+1);
524 WMDeleteFromPLArray(value
, 2);
525 WMInsertInPLArray(value
, 2, WMCreatePLString(newPath
));
528 findCopyFile(themeDir
, WMGetFromPLString(file
));
539 main(int argc
, char **argv
)
541 WMPropList
*prop
, *style
, *key
, *val
;
543 int i
, theme_too
=0, make_pack
=0;
544 char *style_file
= NULL
;
550 for (i
=1; i
<argc
; i
++) {
551 if (strcmp(argv
[i
], "-p")==0
552 || strcmp(argv
[i
], "--pack")==0) {
555 } else if (strcmp(argv
[i
], "-t")==0
556 || strcmp(argv
[i
], "--theme-options")==0) {
558 } else if (strcmp(argv
[i
], "--help")==0) {
561 } else if (strcmp(argv
[i
], "--version")==0) {
565 if (style_file
!=NULL
) {
566 printf("%s: invalid argument '%s'\n", argv
[0],
567 style_file
[0]=='-' ? style_file
: argv
[i
]);
568 printf("Try '%s --help' for more information\n", argv
[0]);
571 style_file
= argv
[i
];
576 if (make_pack
&& !style_file
) {
577 printf("%s: you must supply a name for the theme pack\n", ProgName
);
581 WMPLSetCaseSensitive(False
);
583 path
= defaultsPathForDomain("WindowMaker");
585 prop
= WMReadPropListFromFile(path
);
587 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
592 /* get global value */
593 path
= globalDefaultsPathForDomain("WindowMaker");
594 val
= WMReadPropListFromFile(path
);
596 WMMergePLDictionaries(val
, prop
, True
);
597 WMReleasePropList(prop
);
601 style
= WMCreatePLDictionary(NULL
, NULL
);
603 for (i
=0; options
[i
]!=NULL
; i
++) {
604 key
= WMCreatePLString(options
[i
]);
606 val
= WMGetFromPLDictionary(prop
, key
);
608 WMRetainPropList(val
);
609 if (isFontOption(options
[i
])) {
610 char *newfont
, *oldfont
;
612 oldfont
= WMGetFromPLString(val
);
613 newfont
= convertFont(oldfont
, False
);
614 /* newfont is a reference to old if conversion is not needed */
615 if (newfont
!= oldfont
) {
616 WMReleasePropList(val
);
617 val
= WMCreatePLString(newfont
);
621 WMPutInPLDictionary(style
, key
, val
);
622 WMReleasePropList(val
);
624 WMReleasePropList(key
);
627 val
= WMGetFromPLDictionary(prop
, WMCreatePLString("PixmapPath"));
632 for (i
=0; theme_options
[i
]!=NULL
; i
++) {
633 key
= WMCreatePLString(theme_options
[i
]);
635 val
= WMGetFromPLDictionary(prop
, key
);
637 WMPutInPLDictionary(style
, key
, val
);
644 makeThemePack(style
, style_file
);
646 path
= wmalloc(strlen(ThemePath
)+32);
647 strcpy(path
, ThemePath
);
648 strcat(path
, "/style");
649 WMWritePropListToFile(style
, path
, False
);
653 WMWritePropListToFile(style
, style_file
, False
);
655 puts(WMGetPropListDescription(style
, True
));