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 _GNU_SOURCE /* getopt_long */
27 #include <sys/types.h>
41 #include <WINGs/WUtil.h>
43 #define RETRY( x ) do { \
45 } while (errno == EINTR);
51 #include "../src/wconfig.h"
53 #ifndef GLOBAL_DEFAULTS_SUBDIR
54 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
57 /* table of style related options */
58 static char *options
[] = {
67 "WindowTitleExtendSpace",
68 "MenuTitleExtendSpace",
69 "MenuTextExtendSpace",
90 "WindowTitleExtendSpace",
91 "MenuTitleExtendSpace",
92 "MenuTextExtendSpace",
96 /* table of theme related options */
97 static char *theme_options
[] = {
103 "TopLeftResizeCursor",
104 "TopRightResizeCursor",
105 "BottomLeftResizeCursor",
106 "BottomRightResizeCursor",
107 "VerticalResizeCursor",
108 "HorizontalResizeCursor",
116 /* table of style related fonts */
118 static char *font_options
[] = {
129 extern char *__progname
;
131 WMPropList
*PixmapPath
= NULL
;
133 char *ThemePath
= NULL
;
135 extern char *convertFont(char *font
, Bool keepXLFD
);
137 void print_help(int print_usage
, int exitval
)
139 printf("Usage: %s [-t] [-p] [-h] [-v] [file]\n", __progname
);
141 puts("Retrieves style/theme configuration and output to FILE or to stdout");
143 puts(" -h, --help display this help and exit");
144 puts(" -v, --version output version information and exit");
145 puts(" -t, --theme-options output theme related options when producing a style file");
146 puts(" -p, --pack produce output as a theme pack");
151 void abortar(char *reason
)
153 printf("%s: %s\n", __progname
, reason
);
155 printf("Removing unfinished theme pack\n");
156 (void)wrmdirhier(ThemePath
);
161 static Bool
isFontOption(char *option
)
165 for (i
= 0; font_options
[i
] != NULL
; i
++) {
166 if (strcasecmp(option
, font_options
[i
]) == 0) {
175 * copy a file specified by `file' into `directory'. name stays.
178 * it is more or less assumed that this function will only
179 * copy reasonably-sized files
181 /* XXX: is almost like WINGs/wcolodpanel.c:fetchFile() */
182 void copyFile(char *dir
, char *file
)
184 FILE *src
= NULL
, *dst
= NULL
;
185 size_t nread
, nwritten
, len
;
190 /* only to a directory */
191 if (stat(dir
, &st
) != 0 || !S_ISDIR(st
.st_mode
))
193 /* only copy files */
194 if (stat(file
, &st
) != 0 || !S_ISREG(st
.st_mode
))
197 len
= strlen(dir
) + 1 /* / */ + strlen(file
) + 1 /* '\0' */;
198 dstpath
= wmalloc(len
);
199 snprintf(dstpath
, len
, "%s/%s", dir
, basename(file
));
202 RETRY( dst
= fopen(dstpath
, "wb") )
204 wsyserror(_("Could not create %s"), dstpath
);
208 RETRY( src
= fopen(file
, "rb") )
210 wsyserror(_("Could not open %s"), file
);
215 RETRY( nread
= fread(buf
, 1, sizeof(buf
), src
) )
219 RETRY( nwritten
= fwrite(buf
, 1, nread
, dst
) )
220 if (ferror(dst
) || feof(src
))
225 if (ferror(src
) || ferror(dst
))
228 fchmod(fileno(dst
), st
.st_mode
);
240 void findCopyFile(char *dir
, char *file
)
244 fullPath
= wfindfileinarray(PixmapPath
, file
);
248 sprintf(buffer
, "could not find file %s", file
);
251 copyFile(dir
, fullPath
);
255 void makeThemePack(WMPropList
* style
, char *themeName
)
264 if ((t
= wusergnusteppath()) == NULL
)
266 themeNameLen
= strlen(t
) + 1 /* / */ + strlen(themeName
) + 8 /* ".themed/" */ + 1 /* '\0' */;
267 themeDir
= wmalloc(themeNameLen
);
268 snprintf(themeDir
, themeNameLen
, "%s/%s.themed/", t
, themeName
);
269 ThemePath
= themeDir
;
270 if (!wmkdirhier(themeDir
))
273 keys
= WMGetPLDictionaryKeys(style
);
275 for (i
= 0; i
< WMGetPropListItemCount(keys
); i
++) {
276 key
= WMGetFromPLArray(keys
, i
);
278 value
= WMGetFromPLDictionary(style
, key
);
279 if (value
&& WMIsPLArray(value
) && WMGetPropListItemCount(value
) > 2) {
283 type
= WMGetFromPLArray(value
, 0);
284 t
= WMGetFromPLString(type
);
288 if (strcasecmp(t
, "tpixmap") == 0 ||
289 strcasecmp(t
, "spixmap") == 0 ||
290 strcasecmp(t
, "cpixmap") == 0 ||
291 strcasecmp(t
, "mpixmap") == 0 ||
292 strcasecmp(t
, "tdgradient") == 0 ||
293 strcasecmp(t
, "tvgradient") == 0 ||
294 strcasecmp(t
, "thgradient") == 0) {
300 file
= WMGetFromPLArray(value
, 1);
302 p
= strrchr(WMGetFromPLString(file
), '/');
304 copyFile(themeDir
, WMGetFromPLString(file
));
306 newPath
= wstrdup(p
+ 1);
307 WMDeleteFromPLArray(value
, 1);
308 WMInsertInPLArray(value
, 1, WMCreatePLString(newPath
));
311 findCopyFile(themeDir
, WMGetFromPLString(file
));
313 } else if (strcasecmp(t
, "bitmap") == 0) {
319 file
= WMGetFromPLArray(value
, 1);
321 p
= strrchr(WMGetFromPLString(file
), '/');
323 copyFile(themeDir
, WMGetFromPLString(file
));
325 newPath
= wstrdup(p
+ 1);
326 WMDeleteFromPLArray(value
, 1);
327 WMInsertInPLArray(value
, 1, WMCreatePLString(newPath
));
330 findCopyFile(themeDir
, WMGetFromPLString(file
));
333 file
= WMGetFromPLArray(value
, 2);
335 p
= strrchr(WMGetFromPLString(file
), '/');
337 copyFile(themeDir
, WMGetFromPLString(file
));
339 newPath
= wstrdup(p
+ 1);
340 WMDeleteFromPLArray(value
, 2);
341 WMInsertInPLArray(value
, 2, WMCreatePLString(newPath
));
344 findCopyFile(themeDir
, WMGetFromPLString(file
));
351 int main(int argc
, char **argv
)
353 WMPropList
*prop
, *style
, *key
, *val
;
355 int i
, ch
, theme_too
= 0, make_pack
= 0;
356 char *style_file
= NULL
;
358 struct option longopts
[] = {
359 { "pack", no_argument
, NULL
, 'p' },
360 { "theme-options", no_argument
, NULL
, 't' },
361 { "version", no_argument
, NULL
, 'v' },
362 { "help", no_argument
, NULL
, 'h' },
366 while ((ch
= getopt_long(argc
, argv
, "ptvh", longopts
, NULL
)) != -1)
369 printf("%s (Window Maker %s)\n", __progname
, VERSION
);
394 style_file
= argv
[0];
395 while ((p
= strchr(style_file
, '/')) != NULL
)
398 if (style_file
&& !make_pack
) /* what's this? */
401 if (make_pack
&& !style_file
) {
402 printf("%s: you must supply a name for the theme pack\n", __progname
);
406 WMPLSetCaseSensitive(False
);
408 path
= wdefaultspathfordomain("WindowMaker");
410 prop
= WMReadPropListFromFile(path
);
412 printf("%s: could not load WindowMaker configuration file \"%s\".\n", __progname
, path
);
416 /* get global value */
417 path
= wglobaldefaultspathfordomain("WindowMaker");
419 val
= WMReadPropListFromFile(path
);
421 WMMergePLDictionaries(val
, prop
, True
);
422 WMReleasePropList(prop
);
426 style
= WMCreatePLDictionary(NULL
, NULL
);
428 for (i
= 0; options
[i
] != NULL
; i
++) {
429 key
= WMCreatePLString(options
[i
]);
431 val
= WMGetFromPLDictionary(prop
, key
);
433 WMRetainPropList(val
);
434 if (isFontOption(options
[i
])) {
435 char *newfont
, *oldfont
;
437 oldfont
= WMGetFromPLString(val
);
438 newfont
= convertFont(oldfont
, False
);
439 /* newfont is a reference to old if conversion is not needed */
440 if (newfont
!= oldfont
) {
441 WMReleasePropList(val
);
442 val
= WMCreatePLString(newfont
);
446 WMPutInPLDictionary(style
, key
, val
);
447 WMReleasePropList(val
);
449 WMReleasePropList(key
);
452 val
= WMGetFromPLDictionary(prop
, WMCreatePLString("PixmapPath"));
457 for (i
= 0; theme_options
[i
] != NULL
; i
++) {
458 key
= WMCreatePLString(theme_options
[i
]);
460 val
= WMGetFromPLDictionary(prop
, key
);
462 WMPutInPLDictionary(style
, key
, val
);
469 makeThemePack(style
, style_file
);
471 path
= wmalloc(strlen(ThemePath
) + 32);
472 strcpy(path
, ThemePath
);
473 strcat(path
, "/style");
474 WMWritePropListToFile(style
, path
);
478 WMWritePropListToFile(style
, style_file
);
480 puts(WMGetPropListDescription(style
, True
));