Fix spacing on WPrefs Misc panel
[wmaker-crm.git] / util / getstyle.c
blob1023c10b7827bd87be07413940b4b85d7bc48a37
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, *dst;
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 RETRY( fclose(src) )
234 wfree(dstpath);
235 return;
238 void findCopyFile(char *dir, char *file)
240 char *fullPath;
242 fullPath = wfindfileinarray(PixmapPath, file);
243 if (!fullPath) {
244 char buffer[4000];
246 sprintf(buffer, "could not find file %s", file);
247 abortar(buffer);
249 copyFile(dir, fullPath);
250 free(fullPath);
253 void makeThemePack(WMPropList * style, char *themeName)
255 WMPropList *keys;
256 WMPropList *key;
257 WMPropList *value;
258 int i;
259 size_t themeNameLen;
260 char *themeDir, *t;
262 if ((t = wusergnusteppath()) == NULL)
263 return;
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))
269 return;
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) {
278 WMPropList *type;
279 char *t;
281 type = WMGetFromPLArray(value, 0);
282 t = WMGetFromPLString(type);
283 if (t == NULL)
284 continue;
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) {
294 WMPropList *file;
295 char *p;
296 char *newPath;
298 file = WMGetFromPLArray(value, 1);
300 p = strrchr(WMGetFromPLString(file), '/');
301 if (p) {
302 copyFile(themeDir, WMGetFromPLString(file));
304 newPath = wstrdup(p + 1);
305 WMDeleteFromPLArray(value, 1);
306 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
307 free(newPath);
308 } else {
309 findCopyFile(themeDir, WMGetFromPLString(file));
311 } else if (strcasecmp(t, "bitmap") == 0) {
313 WMPropList *file;
314 char *p;
315 char *newPath;
317 file = WMGetFromPLArray(value, 1);
319 p = strrchr(WMGetFromPLString(file), '/');
320 if (p) {
321 copyFile(themeDir, WMGetFromPLString(file));
323 newPath = wstrdup(p + 1);
324 WMDeleteFromPLArray(value, 1);
325 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
326 free(newPath);
327 } else {
328 findCopyFile(themeDir, WMGetFromPLString(file));
331 file = WMGetFromPLArray(value, 2);
333 p = strrchr(WMGetFromPLString(file), '/');
334 if (p) {
335 copyFile(themeDir, WMGetFromPLString(file));
337 newPath = wstrdup(p + 1);
338 WMDeleteFromPLArray(value, 2);
339 WMInsertInPLArray(value, 2, WMCreatePLString(newPath));
340 free(newPath);
341 } else {
342 findCopyFile(themeDir, WMGetFromPLString(file));
349 int main(int argc, char **argv)
351 WMPropList *prop, *style, *key, *val;
352 char *path, *p;
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' },
361 { NULL, 0, NULL, 0 }
364 while ((ch = getopt_long(argc, argv, "ptvh", longopts, NULL)) != -1)
365 switch(ch) {
366 case 'v':
367 printf("%s (Window Maker %s)\n", __progname, VERSION);
368 return 0;
369 /* NOTREACHED */
370 case 'h':
371 print_help(1, 0);
372 /* NOTREACHED */
373 case 'p':
374 make_pack = 1;
375 theme_too = 1;
376 break;
377 case 't':
378 theme_too = 1;
379 case 0:
380 break;
381 default:
382 print_help(0, 1);
383 /* NOTREACHED */
386 argc -= optind;
387 argv += optind;
389 if (argc != 1)
390 print_help(0, 1);
392 style_file = argv[0];
393 while ((p = strchr(style_file, '/')) != NULL)
394 *p = '_';
396 if (style_file && !make_pack) /* what's this? */
397 print_help(0, 1);
399 if (make_pack && !style_file) {
400 printf("%s: you must supply a name for the theme pack\n", __progname);
401 return 1;
404 WMPLSetCaseSensitive(False);
406 path = wdefaultspathfordomain("WindowMaker");
408 prop = WMReadPropListFromFile(path);
409 if (!prop) {
410 printf("%s: could not load WindowMaker configuration file \"%s\".\n", __progname, path);
411 return 1;
414 /* get global value */
415 path = wglobaldefaultspathfordomain("WindowMaker");
417 val = WMReadPropListFromFile(path);
418 if (val) {
419 WMMergePLDictionaries(val, prop, True);
420 WMReleasePropList(prop);
421 prop = val;
424 style = WMCreatePLDictionary(NULL, NULL);
426 for (i = 0; options[i] != NULL; i++) {
427 key = WMCreatePLString(options[i]);
429 val = WMGetFromPLDictionary(prop, key);
430 if (val) {
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);
441 wfree(newfont);
444 WMPutInPLDictionary(style, key, val);
445 WMReleasePropList(val);
447 WMReleasePropList(key);
450 val = WMGetFromPLDictionary(prop, WMCreatePLString("PixmapPath"));
451 if (val)
452 PixmapPath = val;
454 if (theme_too) {
455 for (i = 0; theme_options[i] != NULL; i++) {
456 key = WMCreatePLString(theme_options[i]);
458 val = WMGetFromPLDictionary(prop, key);
459 if (val)
460 WMPutInPLDictionary(style, key, val);
464 if (make_pack) {
465 char *path;
467 makeThemePack(style, style_file);
469 path = wmalloc(strlen(ThemePath) + 32);
470 strcpy(path, ThemePath);
471 strcat(path, "/style");
472 WMWritePropListToFile(style, path);
473 wfree(path);
474 } else {
475 if (style_file) {
476 WMWritePropListToFile(style, style_file);
477 } else {
478 puts(WMGetPropListDescription(style, True));
481 return 0;