util: Added a few missing 'static' attributes for functions
[wmaker-crm.git] / util / getstyle.c
blobad7a493d6fa09277203f38c7976b44d36af63579
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 <sys/types.h>
27 #include <sys/stat.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <getopt.h>
32 #include <libgen.h>
33 #include <limits.h>
34 #include <pwd.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <strings.h>
39 #include <unistd.h>
41 #include <WINGs/WUtil.h>
43 #include "common.h"
45 #define RETRY( x ) do { \
46 x; \
47 } while (errno == EINTR);
49 #ifndef PATH_MAX
50 #define PATH_MAX 1024
51 #endif
53 #include "../src/wconfig.h"
55 #ifndef GLOBAL_DEFAULTS_SUBDIR
56 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
57 #endif
59 /* table of style related options */
60 static char *options[] = {
61 "TitleJustify",
62 "ClipTitleFont",
63 "WindowTitleFont",
64 "MenuTitleFont",
65 "MenuTextFont",
66 "IconTitleFont",
67 "DisplayFont",
68 "LargeDisplayFont",
69 "WindowTitleExtendSpace",
70 "MenuTitleExtendSpace",
71 "MenuTextExtendSpace",
72 "HighlightColor",
73 "HighlightTextColor",
74 "ClipTitleColor",
75 "CClipTitleColor",
76 "FTitleColor",
77 "PTitleColor",
78 "UTitleColor",
79 "FTitleBack",
80 "PTitleBack",
81 "UTitleBack",
82 "ResizebarBack",
83 "MenuTitleColor",
84 "MenuTextColor",
85 "MenuDisabledColor",
86 "MenuTitleBack",
87 "MenuTextBack",
88 "IconBack",
89 "IconTitleColor",
90 "IconTitleBack",
91 "FrameBorderWidth",
92 "FrameBorderColor",
93 "FrameSelectedBorderColor",
94 "MenuStyle",
95 "WindowTitleExtendSpace",
96 "MenuTitleExtendSpace",
97 "MenuTextExtendSpace",
98 NULL
101 /* table of theme related options */
102 static char *theme_options[] = {
103 "WorkspaceBack",
104 "NormalCursor",
105 "ArrowCursor",
106 "MoveCursor",
107 "ResizeCursor",
108 "TopLeftResizeCursor",
109 "TopRightResizeCursor",
110 "BottomLeftResizeCursor",
111 "BottomRightResizeCursor",
112 "VerticalResizeCursor",
113 "HorizontalResizeCursor",
114 "WaitCursor",
115 "QuestionCursor",
116 "TextCursor",
117 "SelectCursor",
118 NULL
121 /* table of style related fonts */
123 static char *font_options[] = {
124 "ClipTitleFont",
125 "WindowTitleFont",
126 "MenuTitleFont",
127 "MenuTextFont",
128 "IconTitleFont",
129 "DisplayFont",
130 "LargeDisplayFont",
131 NULL
134 extern char *__progname;
136 WMPropList *PixmapPath = NULL;
138 char *ThemePath = NULL;
141 static void print_help(int print_usage, int exitval)
143 printf("Usage: %s [-t] [-p] [-h] [-v] [file]\n", __progname);
144 if (print_usage) {
145 puts("Retrieves style/theme configuration and outputs to ~/GNUstep/Library/WindowMaker/Themes/file.themed/style or to stdout");
146 puts("");
147 puts(" -h, --help display this help and exit");
148 puts(" -v, --version output version information and exit");
149 puts(" -t, --theme-options output theme related options when producing a style file");
150 puts(" -p, --pack produce output as a theme pack");
152 exit(exitval);
155 static Bool isFontOption(const char *option)
157 int i;
159 for (i = 0; font_options[i] != NULL; i++) {
160 if (strcasecmp(option, font_options[i]) == 0) {
161 return True;
165 return False;
168 static void findCopyFile(const char *dir, const char *file)
170 char *fullPath;
172 fullPath = wfindfileinarray(PixmapPath, file);
173 if (!fullPath) {
174 wwarning("Could not find file %s", file);
175 if (ThemePath)
176 (void)wrmdirhier(ThemePath);
178 wcopy_file(dir, fullPath, fullPath);
179 free(fullPath);
182 static void makeThemePack(WMPropList * style, const char *themeName)
184 WMPropList *keys;
185 WMPropList *key;
186 WMPropList *value;
187 int i;
188 size_t themeNameLen;
189 char *themeDir;
190 const char *t;
192 if ((t = wusergnusteppath()) == NULL)
193 return;
194 themeNameLen = strlen(t) + strlen(themeName) + 50;
195 themeDir = wmalloc(themeNameLen);
196 snprintf(themeDir, themeNameLen, "%s/Library/WindowMaker/Themes/%s.themed/", t, themeName);
197 ThemePath = themeDir;
199 if (!wmkdirhier(themeDir)) {
200 wwarning("Could not make theme dir %s\n", themeDir);
201 return;
204 keys = WMGetPLDictionaryKeys(style);
206 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
207 key = WMGetFromPLArray(keys, i);
209 value = WMGetFromPLDictionary(style, key);
210 if (value && WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
211 WMPropList *type;
212 char *t;
214 type = WMGetFromPLArray(value, 0);
215 t = WMGetFromPLString(type);
216 if (t == NULL)
217 continue;
219 if (strcasecmp(t, "tpixmap") == 0 ||
220 strcasecmp(t, "spixmap") == 0 ||
221 strcasecmp(t, "cpixmap") == 0 ||
222 strcasecmp(t, "mpixmap") == 0 ||
223 strcasecmp(t, "tdgradient") == 0 ||
224 strcasecmp(t, "tvgradient") == 0 ||
225 strcasecmp(t, "thgradient") == 0) {
227 WMPropList *file;
228 char *p;
229 char *newPath;
231 file = WMGetFromPLArray(value, 1);
233 p = strrchr(WMGetFromPLString(file), '/');
234 if (p) {
235 wcopy_file(themeDir, WMGetFromPLString(file), WMGetFromPLString(file));
237 newPath = wstrdup(p + 1);
238 WMDeleteFromPLArray(value, 1);
239 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
240 free(newPath);
241 } else {
242 findCopyFile(themeDir, WMGetFromPLString(file));
244 } else if (strcasecmp(t, "bitmap") == 0) {
246 WMPropList *file;
247 char *p;
248 char *newPath;
250 file = WMGetFromPLArray(value, 1);
252 p = strrchr(WMGetFromPLString(file), '/');
253 if (p) {
254 wcopy_file(themeDir, WMGetFromPLString(file), WMGetFromPLString(file));
256 newPath = wstrdup(p + 1);
257 WMDeleteFromPLArray(value, 1);
258 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
259 free(newPath);
260 } else {
261 findCopyFile(themeDir, WMGetFromPLString(file));
264 file = WMGetFromPLArray(value, 2);
266 p = strrchr(WMGetFromPLString(file), '/');
267 if (p) {
268 wcopy_file(themeDir, WMGetFromPLString(file), WMGetFromPLString(file));
270 newPath = wstrdup(p + 1);
271 WMDeleteFromPLArray(value, 2);
272 WMInsertInPLArray(value, 2, WMCreatePLString(newPath));
273 free(newPath);
274 } else {
275 findCopyFile(themeDir, WMGetFromPLString(file));
282 int main(int argc, char **argv)
284 WMPropList *prop, *style, *key, *val;
285 char *path;
286 int i, ch, theme_too = 0, make_pack = 0;
287 char *style_file = NULL;
289 struct option longopts[] = {
290 { "pack", no_argument, NULL, 'p' },
291 { "theme-options", no_argument, NULL, 't' },
292 { "version", no_argument, NULL, 'v' },
293 { "help", no_argument, NULL, 'h' },
294 { NULL, 0, NULL, 0 }
297 while ((ch = getopt_long(argc, argv, "ptvh", longopts, NULL)) != -1)
298 switch(ch) {
299 case 'v':
300 printf("%s (Window Maker %s)\n", __progname, VERSION);
301 return 0;
302 /* NOTREACHED */
303 case 'h':
304 print_help(1, 0);
305 /* NOTREACHED */
306 case 'p':
307 make_pack = 1;
308 theme_too = 1;
309 break;
310 case 't':
311 theme_too = 1;
312 case 0:
313 break;
314 default:
315 print_help(0, 1);
316 /* NOTREACHED */
319 /* At most one non-option ARGV-element is accepted (the theme name) */
320 if (argc - optind > 1)
321 print_help(0, 1);
323 if (argc - optind == 1)
324 style_file = argv[argc - 1];
326 if (make_pack && !style_file) {
327 printf("%s: you must supply a name for the theme pack\n", __progname);
328 return 1;
331 WMPLSetCaseSensitive(False);
333 path = wdefaultspathfordomain("WindowMaker");
335 prop = WMReadPropListFromFile(path);
336 if (!prop) {
337 printf("%s: could not load WindowMaker configuration file \"%s\".\n", __progname, path);
338 return 1;
341 /* get global value */
342 path = wglobaldefaultspathfordomain("WindowMaker");
344 val = WMReadPropListFromFile(path);
345 if (val) {
346 WMMergePLDictionaries(val, prop, True);
347 WMReleasePropList(prop);
348 prop = val;
351 style = WMCreatePLDictionary(NULL, NULL);
353 for (i = 0; options[i] != NULL; i++) {
354 key = WMCreatePLString(options[i]);
356 val = WMGetFromPLDictionary(prop, key);
357 if (val) {
358 WMRetainPropList(val);
359 if (isFontOption(options[i])) {
360 char *newfont, *oldfont;
362 oldfont = WMGetFromPLString(val);
363 newfont = convertFont(oldfont, False);
364 /* newfont is a reference to old if conversion is not needed */
365 if (newfont != oldfont) {
366 WMReleasePropList(val);
367 val = WMCreatePLString(newfont);
368 wfree(newfont);
371 WMPutInPLDictionary(style, key, val);
372 WMReleasePropList(val);
374 WMReleasePropList(key);
377 val = WMGetFromPLDictionary(prop, WMCreatePLString("PixmapPath"));
378 if (val)
379 PixmapPath = val;
381 if (theme_too) {
382 for (i = 0; theme_options[i] != NULL; i++) {
383 key = WMCreatePLString(theme_options[i]);
385 val = WMGetFromPLDictionary(prop, key);
386 if (val)
387 WMPutInPLDictionary(style, key, val);
391 if (make_pack) {
392 char *path;
394 makeThemePack(style, style_file);
396 path = wmalloc(strlen(ThemePath) + 32);
397 strcpy(path, ThemePath);
398 strcat(path, "/style");
399 WMWritePropListToFile(style, path);
400 wfree(path);
401 } else {
402 if (style_file) {
403 WMWritePropListToFile(style, style_file);
404 } else {
405 puts(WMGetPropListDescription(style, True));
408 return 0;