Radio and Check buttons now work
[wmaker-crm.git] / util / getstyle.c
blob577554b1c9bd7e36ab7684331bab46acd90019b1
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,
20 * USA.
23 #ifdef __GLIBC__
24 #define _GNU_SOURCE /* getopt_long */
25 #endif
27 #include <sys/types.h>
28 #include <sys/stat.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <getopt.h>
33 #include <libgen.h>
34 #include <limits.h>
35 #include <pwd.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
41 #include <WINGs/WUtil.h>
43 #define RETRY( x ) do { \
44 x; \
45 } while (errno == EINTR);
47 #ifndef PATH_MAX
48 #define PATH_MAX 1024
49 #endif
51 #include "../src/wconfig.h"
53 #ifndef GLOBAL_DEFAULTS_SUBDIR
54 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
55 #endif
57 /* table of style related options */
58 static char *options[] = {
59 "TitleJustify",
60 "ClipTitleFont",
61 "WindowTitleFont",
62 "MenuTitleFont",
63 "MenuTextFont",
64 "IconTitleFont",
65 "DisplayFont",
66 "LargeDisplayFont",
67 "WindowTitleExtendSpace",
68 "MenuTitleExtendSpace",
69 "MenuTextExtendSpace",
70 "HighlightColor",
71 "HighlightTextColor",
72 "ClipTitleColor",
73 "CClipTitleColor",
74 "FTitleColor",
75 "PTitleColor",
76 "UTitleColor",
77 "FTitleBack",
78 "PTitleBack",
79 "UTitleBack",
80 "ResizebarBack",
81 "MenuTitleColor",
82 "MenuTextColor",
83 "MenuDisabledColor",
84 "MenuTitleBack",
85 "MenuTextBack",
86 "IconBack",
87 "IconTitleColor",
88 "IconTitleBack",
89 "MenuStyle",
90 "WindowTitleExtendSpace",
91 "MenuTitleExtendSpace",
92 "MenuTextExtendSpace",
93 NULL
96 /* table of theme related options */
97 static char *theme_options[] = {
98 "WorkspaceBack",
99 "NormalCursor",
100 "ArrowCursor",
101 "MoveCursor",
102 "ResizeCursor",
103 "TopLeftResizeCursor",
104 "TopRightResizeCursor",
105 "BottomLeftResizeCursor",
106 "BottomRightResizeCursor",
107 "VerticalResizeCursor",
108 "HorizontalResizeCursor",
109 "WaitCursor",
110 "QuestionCursor",
111 "TextCursor",
112 "SelectCursor",
113 NULL
116 /* table of style related fonts */
118 static char *font_options[] = {
119 "ClipTitleFont",
120 "WindowTitleFont",
121 "MenuTitleFont",
122 "MenuTextFont",
123 "IconTitleFont",
124 "DisplayFont",
125 "LargeDisplayFont",
126 NULL
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);
140 if (print_usage) {
141 puts("Retrieves style/theme configuration and output to FILE or to stdout");
142 puts("");
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");
148 exit(exitval);
151 void abortar(char *reason)
153 printf("%s: %s\n", __progname, reason);
154 if (ThemePath) {
155 printf("Removing unfinished theme pack\n");
156 (void)wrmdirhier(ThemePath);
158 exit(1);
161 static Bool isFontOption(char *option)
163 int i;
165 for (i = 0; font_options[i] != NULL; i++) {
166 if (strcasecmp(option, font_options[i]) == 0) {
167 return True;
171 return False;
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;
186 char buf[4096];
187 struct stat st;
188 char *dstpath;
190 /* only to a directory */
191 if (stat(dir, &st) != 0 || !S_ISDIR(st.st_mode))
192 return;
193 /* only copy files */
194 if (stat(file, &st) != 0 || !S_ISREG(st.st_mode))
195 return;
197 len = strlen(dir) + 1 /* / */ + strlen(file) + 1 /* '\0' */;
198 dstpath = wmalloc(len);
199 snprintf(dstpath, len, "%s/%s", dir, basename(file));
200 buf[len] = '\0';
202 RETRY( dst = fopen(dstpath, "wb") )
203 if (dst == NULL) {
204 wsyserror(_("Could not create %s"), dstpath);
205 goto err;
208 RETRY( src = fopen(file, "rb") )
209 if (src == NULL) {
210 wsyserror(_("Could not open %s"), file);
211 goto err;
214 do {
215 RETRY( nread = fread(buf, 1, sizeof(buf), src) )
216 if (ferror(src))
217 break;
219 RETRY( nwritten = fwrite(buf, 1, nread, dst) )
220 if (ferror(dst) || feof(src))
221 break;
223 } while (1);
225 if (ferror(src) || ferror(dst))
226 unlink(dstpath);
228 fchmod(fileno(dst), st.st_mode);
229 fsync(fileno(dst));
230 RETRY( fclose(dst) )
232 err:
233 if (src) {
234 RETRY( fclose(src) )
236 wfree(dstpath);
237 return;
240 void findCopyFile(char *dir, char *file)
242 char *fullPath;
244 fullPath = wfindfileinarray(PixmapPath, file);
245 if (!fullPath) {
246 char buffer[4000];
248 sprintf(buffer, "could not find file %s", file);
249 abortar(buffer);
251 copyFile(dir, fullPath);
252 free(fullPath);
255 void makeThemePack(WMPropList * style, char *themeName)
257 WMPropList *keys;
258 WMPropList *key;
259 WMPropList *value;
260 int i;
261 size_t themeNameLen;
262 char *themeDir, *t;
264 if ((t = wusergnusteppath()) == NULL)
265 return;
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))
271 return;
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) {
280 WMPropList *type;
281 char *t;
283 type = WMGetFromPLArray(value, 0);
284 t = WMGetFromPLString(type);
285 if (t == NULL)
286 continue;
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) {
296 WMPropList *file;
297 char *p;
298 char *newPath;
300 file = WMGetFromPLArray(value, 1);
302 p = strrchr(WMGetFromPLString(file), '/');
303 if (p) {
304 copyFile(themeDir, WMGetFromPLString(file));
306 newPath = wstrdup(p + 1);
307 WMDeleteFromPLArray(value, 1);
308 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
309 free(newPath);
310 } else {
311 findCopyFile(themeDir, WMGetFromPLString(file));
313 } else if (strcasecmp(t, "bitmap") == 0) {
315 WMPropList *file;
316 char *p;
317 char *newPath;
319 file = WMGetFromPLArray(value, 1);
321 p = strrchr(WMGetFromPLString(file), '/');
322 if (p) {
323 copyFile(themeDir, WMGetFromPLString(file));
325 newPath = wstrdup(p + 1);
326 WMDeleteFromPLArray(value, 1);
327 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
328 free(newPath);
329 } else {
330 findCopyFile(themeDir, WMGetFromPLString(file));
333 file = WMGetFromPLArray(value, 2);
335 p = strrchr(WMGetFromPLString(file), '/');
336 if (p) {
337 copyFile(themeDir, WMGetFromPLString(file));
339 newPath = wstrdup(p + 1);
340 WMDeleteFromPLArray(value, 2);
341 WMInsertInPLArray(value, 2, WMCreatePLString(newPath));
342 free(newPath);
343 } else {
344 findCopyFile(themeDir, WMGetFromPLString(file));
351 int main(int argc, char **argv)
353 WMPropList *prop, *style, *key, *val;
354 char *path, *p;
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' },
363 { NULL, 0, NULL, 0 }
366 while ((ch = getopt_long(argc, argv, "ptvh", longopts, NULL)) != -1)
367 switch(ch) {
368 case 'v':
369 printf("%s (Window Maker %s)\n", __progname, VERSION);
370 return 0;
371 /* NOTREACHED */
372 case 'h':
373 print_help(1, 0);
374 /* NOTREACHED */
375 case 'p':
376 make_pack = 1;
377 theme_too = 1;
378 break;
379 case 't':
380 theme_too = 1;
381 case 0:
382 break;
383 default:
384 print_help(0, 1);
385 /* NOTREACHED */
388 argc -= optind;
389 argv += optind;
391 if (argc != 1)
392 print_help(0, 1);
394 style_file = argv[0];
395 while ((p = strchr(style_file, '/')) != NULL)
396 *p = '_';
398 if (style_file && !make_pack) /* what's this? */
399 print_help(0, 1);
401 if (make_pack && !style_file) {
402 printf("%s: you must supply a name for the theme pack\n", __progname);
403 return 1;
406 WMPLSetCaseSensitive(False);
408 path = wdefaultspathfordomain("WindowMaker");
410 prop = WMReadPropListFromFile(path);
411 if (!prop) {
412 printf("%s: could not load WindowMaker configuration file \"%s\".\n", __progname, path);
413 return 1;
416 /* get global value */
417 path = wglobaldefaultspathfordomain("WindowMaker");
419 val = WMReadPropListFromFile(path);
420 if (val) {
421 WMMergePLDictionaries(val, prop, True);
422 WMReleasePropList(prop);
423 prop = val;
426 style = WMCreatePLDictionary(NULL, NULL);
428 for (i = 0; options[i] != NULL; i++) {
429 key = WMCreatePLString(options[i]);
431 val = WMGetFromPLDictionary(prop, key);
432 if (val) {
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);
443 wfree(newfont);
446 WMPutInPLDictionary(style, key, val);
447 WMReleasePropList(val);
449 WMReleasePropList(key);
452 val = WMGetFromPLDictionary(prop, WMCreatePLString("PixmapPath"));
453 if (val)
454 PixmapPath = val;
456 if (theme_too) {
457 for (i = 0; theme_options[i] != NULL; i++) {
458 key = WMCreatePLString(theme_options[i]);
460 val = WMGetFromPLDictionary(prop, key);
461 if (val)
462 WMPutInPLDictionary(style, key, val);
466 if (make_pack) {
467 char *path;
469 makeThemePack(style, style_file);
471 path = wmalloc(strlen(ThemePath) + 32);
472 strcpy(path, ThemePath);
473 strcat(path, "/style");
474 WMWritePropListToFile(style, path);
475 wfree(path);
476 } else {
477 if (style_file) {
478 WMWritePropListToFile(style, style_file);
479 } else {
480 puts(WMGetPropListDescription(style, True));
483 return 0;