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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #define _GNU_SOURCE /* getopt_long */
26 #include <sys/types.h>
40 #include <WINGs/WUtil.h>
42 #define RETRY( x ) do { \
44 } while (errno == EINTR);
50 #include "../src/wconfig.h"
52 #ifndef GLOBAL_DEFAULTS_SUBDIR
53 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
56 /* table of style related options */
57 static char *options
[] = {
66 "WindowTitleExtendSpace",
67 "MenuTitleExtendSpace",
68 "MenuTextExtendSpace",
89 "WindowTitleExtendSpace",
90 "MenuTitleExtendSpace",
91 "MenuTextExtendSpace",
95 /* table of theme related options */
96 static char *theme_options
[] = {
102 "TopLeftResizeCursor",
103 "TopRightResizeCursor",
104 "BottomLeftResizeCursor",
105 "BottomRightResizeCursor",
106 "VerticalResizeCursor",
107 "HorizontalResizeCursor",
115 /* table of style related fonts */
117 static char *font_options
[] = {
128 extern char *__progname
;
130 WMPropList
*PixmapPath
= NULL
;
132 char *ThemePath
= NULL
;
134 extern char *convertFont(char *font
, Bool keepXLFD
);
136 void print_help(int print_usage
, int exitval
)
138 printf("Usage: %s [-t] [-p] [-h] [-v] [file]\n", __progname
);
140 puts("Retrieves style/theme configuration and output to FILE or to stdout");
142 puts(" -h, --help display this help and exit");
143 puts(" -v, --version output version information and exit");
144 puts(" -t, --theme-options output theme related options when producing a style file");
145 puts(" -p, --pack produce output as a theme pack");
150 void abortar(char *reason
)
152 printf("%s: %s\n", __progname
, reason
);
154 printf("Removing unfinished theme pack\n");
155 (void)wrmdirhier(ThemePath
);
160 static Bool
isFontOption(char *option
)
164 for (i
= 0; font_options
[i
] != NULL
; i
++) {
165 if (strcasecmp(option
, font_options
[i
]) == 0) {
174 * copy a file specified by `file' into `directory'. name stays.
177 * it is more or less assumed that this function will only
178 * copy reasonably-sized files
180 /* XXX: is almost like WINGs/wcolodpanel.c:fetchFile() */
181 void copyFile(char *dir
, char *file
)
183 FILE *src
= NULL
, *dst
= NULL
;
184 size_t nread
, nwritten
, len
;
189 /* only to a directory */
190 if (stat(dir
, &st
) != 0 || !S_ISDIR(st
.st_mode
))
192 /* only copy files */
193 if (stat(file
, &st
) != 0 || !S_ISREG(st
.st_mode
))
196 len
= strlen(dir
) + 1 /* / */ + strlen(file
) + 1 /* '\0' */;
197 dstpath
= wmalloc(len
);
198 snprintf(dstpath
, len
, "%s/%s", dir
, basename(file
));
201 RETRY( dst
= fopen(dstpath
, "wb") )
203 werror(_("Could not create %s"), dstpath
);
207 RETRY( src
= fopen(file
, "rb") )
209 werror(_("Could not open %s"), file
);
214 RETRY( nread
= fread(buf
, 1, sizeof(buf
), src
) )
218 RETRY( nwritten
= fwrite(buf
, 1, nread
, dst
) )
219 if (ferror(dst
) || feof(src
))
224 if (ferror(src
) || ferror(dst
))
227 fchmod(fileno(dst
), st
.st_mode
);
239 void findCopyFile(char *dir
, char *file
)
243 fullPath
= wfindfileinarray(PixmapPath
, file
);
247 sprintf(buffer
, "could not find file %s", file
);
250 copyFile(dir
, fullPath
);
254 void makeThemePack(WMPropList
* style
, char *themeName
)
263 if ((t
= wusergnusteppath()) == NULL
)
265 themeNameLen
= strlen(t
) + 1 /* / */ + strlen(themeName
) + 8 /* ".themed/" */ + 1 /* '\0' */;
266 themeDir
= wmalloc(themeNameLen
);
267 snprintf(themeDir
, themeNameLen
, "%s/%s.themed/", t
, themeName
);
268 ThemePath
= themeDir
;
269 if (!wmkdirhier(themeDir
))
272 keys
= WMGetPLDictionaryKeys(style
);
274 for (i
= 0; i
< WMGetPropListItemCount(keys
); i
++) {
275 key
= WMGetFromPLArray(keys
, i
);
277 value
= WMGetFromPLDictionary(style
, key
);
278 if (value
&& WMIsPLArray(value
) && WMGetPropListItemCount(value
) > 2) {
282 type
= WMGetFromPLArray(value
, 0);
283 t
= WMGetFromPLString(type
);
287 if (strcasecmp(t
, "tpixmap") == 0 ||
288 strcasecmp(t
, "spixmap") == 0 ||
289 strcasecmp(t
, "cpixmap") == 0 ||
290 strcasecmp(t
, "mpixmap") == 0 ||
291 strcasecmp(t
, "tdgradient") == 0 ||
292 strcasecmp(t
, "tvgradient") == 0 ||
293 strcasecmp(t
, "thgradient") == 0) {
299 file
= WMGetFromPLArray(value
, 1);
301 p
= strrchr(WMGetFromPLString(file
), '/');
303 copyFile(themeDir
, WMGetFromPLString(file
));
305 newPath
= wstrdup(p
+ 1);
306 WMDeleteFromPLArray(value
, 1);
307 WMInsertInPLArray(value
, 1, WMCreatePLString(newPath
));
310 findCopyFile(themeDir
, WMGetFromPLString(file
));
312 } else if (strcasecmp(t
, "bitmap") == 0) {
318 file
= WMGetFromPLArray(value
, 1);
320 p
= strrchr(WMGetFromPLString(file
), '/');
322 copyFile(themeDir
, WMGetFromPLString(file
));
324 newPath
= wstrdup(p
+ 1);
325 WMDeleteFromPLArray(value
, 1);
326 WMInsertInPLArray(value
, 1, WMCreatePLString(newPath
));
329 findCopyFile(themeDir
, WMGetFromPLString(file
));
332 file
= WMGetFromPLArray(value
, 2);
334 p
= strrchr(WMGetFromPLString(file
), '/');
336 copyFile(themeDir
, WMGetFromPLString(file
));
338 newPath
= wstrdup(p
+ 1);
339 WMDeleteFromPLArray(value
, 2);
340 WMInsertInPLArray(value
, 2, WMCreatePLString(newPath
));
343 findCopyFile(themeDir
, WMGetFromPLString(file
));
350 int main(int argc
, char **argv
)
352 WMPropList
*prop
, *style
, *key
, *val
;
354 int i
, ch
, theme_too
= 0, make_pack
= 0;
355 char *style_file
= NULL
;
357 struct option longopts
[] = {
358 { "pack", no_argument
, NULL
, 'p' },
359 { "theme-options", no_argument
, NULL
, 't' },
360 { "version", no_argument
, NULL
, 'v' },
361 { "help", no_argument
, NULL
, 'h' },
365 while ((ch
= getopt_long(argc
, argv
, "ptvh", longopts
, NULL
)) != -1)
368 printf("%s (Window Maker %s)\n", __progname
, VERSION
);
393 style_file
= argv
[0];
394 while ((p
= strchr(style_file
, '/')) != NULL
)
397 if (style_file
&& !make_pack
) /* what's this? */
400 if (make_pack
&& !style_file
) {
401 printf("%s: you must supply a name for the theme pack\n", __progname
);
405 WMPLSetCaseSensitive(False
);
407 path
= wdefaultspathfordomain("WindowMaker");
409 prop
= WMReadPropListFromFile(path
);
411 printf("%s: could not load WindowMaker configuration file \"%s\".\n", __progname
, path
);
415 /* get global value */
416 path
= wglobaldefaultspathfordomain("WindowMaker");
418 val
= WMReadPropListFromFile(path
);
420 WMMergePLDictionaries(val
, prop
, True
);
421 WMReleasePropList(prop
);
425 style
= WMCreatePLDictionary(NULL
, NULL
);
427 for (i
= 0; options
[i
] != NULL
; i
++) {
428 key
= WMCreatePLString(options
[i
]);
430 val
= WMGetFromPLDictionary(prop
, key
);
432 WMRetainPropList(val
);
433 if (isFontOption(options
[i
])) {
434 char *newfont
, *oldfont
;
436 oldfont
= WMGetFromPLString(val
);
437 newfont
= convertFont(oldfont
, False
);
438 /* newfont is a reference to old if conversion is not needed */
439 if (newfont
!= oldfont
) {
440 WMReleasePropList(val
);
441 val
= WMCreatePLString(newfont
);
445 WMPutInPLDictionary(style
, key
, val
);
446 WMReleasePropList(val
);
448 WMReleasePropList(key
);
451 val
= WMGetFromPLDictionary(prop
, WMCreatePLString("PixmapPath"));
456 for (i
= 0; theme_options
[i
] != NULL
; i
++) {
457 key
= WMCreatePLString(theme_options
[i
]);
459 val
= WMGetFromPLDictionary(prop
, key
);
461 WMPutInPLDictionary(style
, key
, val
);
468 makeThemePack(style
, style_file
);
470 path
= wmalloc(strlen(ThemePath
) + 32);
471 strcpy(path
, ThemePath
);
472 strcat(path
, "/style");
473 WMWritePropListToFile(style
, path
);
477 WMWritePropListToFile(style
, style_file
);
479 puts(WMGetPropListDescription(style
, True
));