Fixed a few improper macro usages
[wmaker-crm.git] / util / getstyle.c
blob66548e7cf5e76ee09ff81e8be8184ec3b7d87bdc
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.
22 #ifdef __GLIBC__
23 #define _GNU_SOURCE /* getopt_long */
24 #endif
26 #include "config.h"
28 #include <sys/types.h>
29 #include <sys/stat.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <getopt.h>
34 #include <libgen.h>
35 #include <limits.h>
36 #include <pwd.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <strings.h>
41 #include <unistd.h>
43 #ifdef HAVE_STDNORETURN
44 #include <stdnoreturn.h>
45 #endif
47 #include <WINGs/WUtil.h>
49 #include "common.h"
51 #define RETRY( x ) do { \
52 x; \
53 } while (errno == EINTR);
55 #ifndef PATH_MAX
56 #define PATH_MAX 1024
57 #endif
59 #include "../src/wconfig.h"
61 #ifndef GLOBAL_DEFAULTS_SUBDIR
62 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
63 #endif
65 /* table of style related options */
66 static char *options[] = {
67 "TitleJustify",
68 "ClipTitleFont",
69 "WindowTitleFont",
70 "MenuTitleFont",
71 "MenuTextFont",
72 "IconTitleFont",
73 "DisplayFont",
74 "LargeDisplayFont",
75 "WindowTitleExtendSpace",
76 "MenuTitleExtendSpace",
77 "MenuTextExtendSpace",
78 "HighlightColor",
79 "HighlightTextColor",
80 "ClipTitleColor",
81 "CClipTitleColor",
82 "FTitleColor",
83 "PTitleColor",
84 "UTitleColor",
85 "FTitleBack",
86 "PTitleBack",
87 "UTitleBack",
88 "ResizebarBack",
89 "MenuTitleColor",
90 "MenuTextColor",
91 "MenuDisabledColor",
92 "MenuTitleBack",
93 "MenuTextBack",
94 "IconBack",
95 "IconTitleColor",
96 "IconTitleBack",
97 "FrameBorderWidth",
98 "FrameBorderColor",
99 "FrameSelectedBorderColor",
100 "MenuStyle",
101 "WindowTitleExtendSpace",
102 "MenuTitleExtendSpace",
103 "MenuTextExtendSpace",
104 NULL
107 /* table of theme related options */
108 static char *theme_options[] = {
109 "WorkspaceBack",
110 "NormalCursor",
111 "ArrowCursor",
112 "MoveCursor",
113 "ResizeCursor",
114 "TopLeftResizeCursor",
115 "TopRightResizeCursor",
116 "BottomLeftResizeCursor",
117 "BottomRightResizeCursor",
118 "VerticalResizeCursor",
119 "HorizontalResizeCursor",
120 "WaitCursor",
121 "QuestionCursor",
122 "TextCursor",
123 "SelectCursor",
124 NULL
127 /* table of style related fonts */
129 static char *font_options[] = {
130 "ClipTitleFont",
131 "WindowTitleFont",
132 "MenuTitleFont",
133 "MenuTextFont",
134 "IconTitleFont",
135 "DisplayFont",
136 "LargeDisplayFont",
137 NULL
140 extern char *__progname;
142 WMPropList *PixmapPath = NULL;
144 char *ThemePath = NULL;
147 static noreturn void print_help(int print_usage, int exitval)
149 printf("Usage: %s [-t] [-p] [-h] [-v] [file]\n", __progname);
150 if (print_usage) {
151 puts("Retrieves style/theme configuration and outputs to ~/GNUstep/Library/WindowMaker/Themes/file.themed/style or to stdout");
152 puts("");
153 puts(" -h, --help display this help and exit");
154 puts(" -v, --version output version information and exit");
155 puts(" -t, --theme-options output theme related options when producing a style file");
156 puts(" -p, --pack produce output as a theme pack");
158 exit(exitval);
161 static Bool isFontOption(const 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;
174 static void findCopyFile(const char *dir, const char *file)
176 char *fullPath;
178 fullPath = wfindfileinarray(PixmapPath, file);
179 if (!fullPath) {
180 wwarning("Could not find file %s", file);
181 if (ThemePath)
182 (void)wrmdirhier(ThemePath);
184 wcopy_file(dir, fullPath, fullPath);
185 free(fullPath);
188 static void makeThemePack(WMPropList * style, const char *themeName)
190 WMPropList *keys;
191 WMPropList *key;
192 WMPropList *value;
193 int i;
194 size_t themeNameLen;
195 char *themeDir;
196 const char *t;
198 if ((t = wusergnusteppath()) == NULL)
199 return;
200 themeNameLen = strlen(t) + strlen(themeName) + 50;
201 themeDir = wmalloc(themeNameLen);
202 snprintf(themeDir, themeNameLen, "%s/Library/WindowMaker/Themes/%s.themed/", t, themeName);
203 ThemePath = themeDir;
205 if (!wmkdirhier(themeDir)) {
206 wwarning("Could not make theme dir %s\n", themeDir);
207 return;
210 keys = WMGetPLDictionaryKeys(style);
212 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
213 key = WMGetFromPLArray(keys, i);
215 value = WMGetFromPLDictionary(style, key);
216 if (value && WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
217 WMPropList *type;
218 char *t;
220 type = WMGetFromPLArray(value, 0);
221 t = WMGetFromPLString(type);
222 if (t == NULL)
223 continue;
225 if (strcasecmp(t, "tpixmap") == 0 ||
226 strcasecmp(t, "spixmap") == 0 ||
227 strcasecmp(t, "cpixmap") == 0 ||
228 strcasecmp(t, "mpixmap") == 0 ||
229 strcasecmp(t, "tdgradient") == 0 ||
230 strcasecmp(t, "tvgradient") == 0 ||
231 strcasecmp(t, "thgradient") == 0) {
233 WMPropList *file;
234 char *p;
235 char *newPath;
237 file = WMGetFromPLArray(value, 1);
239 p = strrchr(WMGetFromPLString(file), '/');
240 if (p) {
241 wcopy_file(themeDir, WMGetFromPLString(file), WMGetFromPLString(file));
243 newPath = wstrdup(p + 1);
244 WMDeleteFromPLArray(value, 1);
245 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
246 free(newPath);
247 } else {
248 findCopyFile(themeDir, WMGetFromPLString(file));
250 } else if (strcasecmp(t, "bitmap") == 0) {
252 WMPropList *file;
253 char *p;
254 char *newPath;
256 file = WMGetFromPLArray(value, 1);
258 p = strrchr(WMGetFromPLString(file), '/');
259 if (p) {
260 wcopy_file(themeDir, WMGetFromPLString(file), WMGetFromPLString(file));
262 newPath = wstrdup(p + 1);
263 WMDeleteFromPLArray(value, 1);
264 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
265 free(newPath);
266 } else {
267 findCopyFile(themeDir, WMGetFromPLString(file));
270 file = WMGetFromPLArray(value, 2);
272 p = strrchr(WMGetFromPLString(file), '/');
273 if (p) {
274 wcopy_file(themeDir, WMGetFromPLString(file), WMGetFromPLString(file));
276 newPath = wstrdup(p + 1);
277 WMDeleteFromPLArray(value, 2);
278 WMInsertInPLArray(value, 2, WMCreatePLString(newPath));
279 free(newPath);
280 } else {
281 findCopyFile(themeDir, WMGetFromPLString(file));
288 int main(int argc, char **argv)
290 WMPropList *prop, *style, *key, *val;
291 char *path;
292 int i, ch, theme_too = 0, make_pack = 0;
293 char *style_file = NULL;
295 struct option longopts[] = {
296 { "pack", no_argument, NULL, 'p' },
297 { "theme-options", no_argument, NULL, 't' },
298 { "version", no_argument, NULL, 'v' },
299 { "help", no_argument, NULL, 'h' },
300 { NULL, 0, NULL, 0 }
303 while ((ch = getopt_long(argc, argv, "ptvh", longopts, NULL)) != -1)
304 switch(ch) {
305 case 'v':
306 printf("%s (Window Maker %s)\n", __progname, VERSION);
307 return 0;
308 /* NOTREACHED */
309 case 'h':
310 print_help(1, 0);
311 /* NOTREACHED */
312 case 'p':
313 make_pack = 1;
314 theme_too = 1;
315 break;
316 case 't':
317 theme_too = 1;
318 case 0:
319 break;
320 default:
321 print_help(0, 1);
322 /* NOTREACHED */
325 /* At most one non-option ARGV-element is accepted (the theme name) */
326 if (argc - optind > 1)
327 print_help(0, 1);
329 if (argc - optind == 1)
330 style_file = argv[argc - 1];
332 if (make_pack && !style_file) {
333 printf("%s: you must supply a name for the theme pack\n", __progname);
334 return 1;
337 WMPLSetCaseSensitive(False);
339 path = wdefaultspathfordomain("WindowMaker");
341 prop = WMReadPropListFromFile(path);
342 if (!prop) {
343 printf("%s: could not load WindowMaker configuration file \"%s\".\n", __progname, path);
344 return 1;
347 /* get global value */
348 path = wglobaldefaultspathfordomain("WindowMaker");
350 val = WMReadPropListFromFile(path);
351 if (val) {
352 WMMergePLDictionaries(val, prop, True);
353 WMReleasePropList(prop);
354 prop = val;
357 style = WMCreatePLDictionary(NULL, NULL);
359 for (i = 0; options[i] != NULL; i++) {
360 key = WMCreatePLString(options[i]);
362 val = WMGetFromPLDictionary(prop, key);
363 if (val) {
364 WMRetainPropList(val);
365 if (isFontOption(options[i])) {
366 char *newfont, *oldfont;
368 oldfont = WMGetFromPLString(val);
369 newfont = convertFont(oldfont, False);
370 /* newfont is a reference to old if conversion is not needed */
371 if (newfont != oldfont) {
372 WMReleasePropList(val);
373 val = WMCreatePLString(newfont);
374 wfree(newfont);
377 WMPutInPLDictionary(style, key, val);
378 WMReleasePropList(val);
380 WMReleasePropList(key);
383 val = WMGetFromPLDictionary(prop, WMCreatePLString("PixmapPath"));
384 if (val)
385 PixmapPath = val;
387 if (theme_too) {
388 for (i = 0; theme_options[i] != NULL; i++) {
389 key = WMCreatePLString(theme_options[i]);
391 val = WMGetFromPLDictionary(prop, key);
392 if (val)
393 WMPutInPLDictionary(style, key, val);
397 if (make_pack) {
398 char *path;
400 makeThemePack(style, style_file);
402 path = wmalloc(strlen(ThemePath) + 32);
403 strcpy(path, ThemePath);
404 strcat(path, "/style");
405 WMWritePropListToFile(style, path);
406 wfree(path);
407 } else {
408 if (style_file) {
409 WMWritePropListToFile(style, style_file);
410 } else {
411 puts(WMGetPropListDescription(style, True));
414 return 0;