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
)
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
);
238 void findCopyFile(char *dir
, char *file
)
242 fullPath
= wfindfileinarray(PixmapPath
, file
);
246 sprintf(buffer
, "could not find file %s", file
);
249 copyFile(dir
, fullPath
);
253 void makeThemePack(WMPropList
* style
, char *themeName
)
262 if ((t
= wusergnusteppath()) == NULL
)
264 themeNameLen
= strlen(t
) + 1 /* / */ + strlen(themeName
) + 8 /* ".themed/" */ + 1 /* '\0' */;
265 themeDir
= wmalloc(themeNameLen
);
266 snprintf(themeDir
, themeNameLen
, "%s/%s.themed/", t
, themeName
);
267 ThemePath
= themeDir
;
268 if (!wmkdirhier(themeDir
))
271 keys
= WMGetPLDictionaryKeys(style
);
273 for (i
= 0; i
< WMGetPropListItemCount(keys
); i
++) {
274 key
= WMGetFromPLArray(keys
, i
);
276 value
= WMGetFromPLDictionary(style
, key
);
277 if (value
&& WMIsPLArray(value
) && WMGetPropListItemCount(value
) > 2) {
281 type
= WMGetFromPLArray(value
, 0);
282 t
= WMGetFromPLString(type
);
286 if (strcasecmp(t
, "tpixmap") == 0 ||
287 strcasecmp(t
, "spixmap") == 0 ||
288 strcasecmp(t
, "cpixmap") == 0 ||
289 strcasecmp(t
, "mpixmap") == 0 ||
290 strcasecmp(t
, "tdgradient") == 0 ||
291 strcasecmp(t
, "tvgradient") == 0 ||
292 strcasecmp(t
, "thgradient") == 0) {
298 file
= WMGetFromPLArray(value
, 1);
300 p
= strrchr(WMGetFromPLString(file
), '/');
302 copyFile(themeDir
, WMGetFromPLString(file
));
304 newPath
= wstrdup(p
+ 1);
305 WMDeleteFromPLArray(value
, 1);
306 WMInsertInPLArray(value
, 1, WMCreatePLString(newPath
));
309 findCopyFile(themeDir
, WMGetFromPLString(file
));
311 } else if (strcasecmp(t
, "bitmap") == 0) {
317 file
= WMGetFromPLArray(value
, 1);
319 p
= strrchr(WMGetFromPLString(file
), '/');
321 copyFile(themeDir
, WMGetFromPLString(file
));
323 newPath
= wstrdup(p
+ 1);
324 WMDeleteFromPLArray(value
, 1);
325 WMInsertInPLArray(value
, 1, WMCreatePLString(newPath
));
328 findCopyFile(themeDir
, WMGetFromPLString(file
));
331 file
= WMGetFromPLArray(value
, 2);
333 p
= strrchr(WMGetFromPLString(file
), '/');
335 copyFile(themeDir
, WMGetFromPLString(file
));
337 newPath
= wstrdup(p
+ 1);
338 WMDeleteFromPLArray(value
, 2);
339 WMInsertInPLArray(value
, 2, WMCreatePLString(newPath
));
342 findCopyFile(themeDir
, WMGetFromPLString(file
));
349 int main(int argc
, char **argv
)
351 WMPropList
*prop
, *style
, *key
, *val
;
353 int i
, ch
, theme_too
= 0, make_pack
= 0;
354 char *style_file
= NULL
;
356 struct option longopts
[] = {
357 { "pack", no_argument
, NULL
, 'p' },
358 { "theme-options", no_argument
, NULL
, 't' },
359 { "version", no_argument
, NULL
, 'v' },
360 { "help", no_argument
, NULL
, 'h' },
364 while ((ch
= getopt_long(argc
, argv
, "ptvh", longopts
, NULL
)) != -1)
367 printf("%s (Window Maker %s)\n", __progname
, VERSION
);
392 style_file
= argv
[0];
393 while ((p
= strchr(style_file
, '/')) != NULL
)
396 if (style_file
&& !make_pack
) /* what's this? */
399 if (make_pack
&& !style_file
) {
400 printf("%s: you must supply a name for the theme pack\n", __progname
);
404 WMPLSetCaseSensitive(False
);
406 path
= wdefaultspathfordomain("WindowMaker");
408 prop
= WMReadPropListFromFile(path
);
410 printf("%s: could not load WindowMaker configuration file \"%s\".\n", __progname
, path
);
414 /* get global value */
415 path
= wglobaldefaultspathfordomain("WindowMaker");
417 val
= WMReadPropListFromFile(path
);
419 WMMergePLDictionaries(val
, prop
, True
);
420 WMReleasePropList(prop
);
424 style
= WMCreatePLDictionary(NULL
, NULL
);
426 for (i
= 0; options
[i
] != NULL
; i
++) {
427 key
= WMCreatePLString(options
[i
]);
429 val
= WMGetFromPLDictionary(prop
, key
);
431 WMRetainPropList(val
);
432 if (isFontOption(options
[i
])) {
433 char *newfont
, *oldfont
;
435 oldfont
= WMGetFromPLString(val
);
436 newfont
= convertFont(oldfont
, False
);
437 /* newfont is a reference to old if conversion is not needed */
438 if (newfont
!= oldfont
) {
439 WMReleasePropList(val
);
440 val
= WMCreatePLString(newfont
);
444 WMPutInPLDictionary(style
, key
, val
);
445 WMReleasePropList(val
);
447 WMReleasePropList(key
);
450 val
= WMGetFromPLDictionary(prop
, WMCreatePLString("PixmapPath"));
455 for (i
= 0; theme_options
[i
] != NULL
; i
++) {
456 key
= WMCreatePLString(theme_options
[i
]);
458 val
= WMGetFromPLDictionary(prop
, key
);
460 WMPutInPLDictionary(style
, key
, val
);
467 makeThemePack(style
, style_file
);
469 path
= wmalloc(strlen(ThemePath
) + 32);
470 strcpy(path
, ThemePath
);
471 strcat(path
, "/style");
472 WMWritePropListToFile(style
, path
);
476 WMWritePropListToFile(style
, style_file
);
478 puts(WMGetPropListDescription(style
, True
));