Fix non-I18N build
[wmaker-crm.git] / util / getstyle.c
blob69b37bbb3c1ec5b20ca16c9ce417a71f2032b2e0
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 #define PROG_VERSION "getstyle (Window Maker) 0.6"
25 #include <sys/types.h>
26 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <pwd.h>
35 #include <limits.h>
36 #include <assert.h>
37 #include <libgen.h>
38 #include <WINGs/WUtil.h>
40 #ifndef PATH_MAX
41 #define PATH_MAX 1024
42 #endif
44 #include "../src/wconfig.h"
46 #ifndef GLOBAL_DEFAULTS_SUBDIR
47 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
48 #endif
50 /* table of style related options */
51 static char *options[] = {
52 "TitleJustify",
53 "ClipTitleFont",
54 "WindowTitleFont",
55 "MenuTitleFont",
56 "MenuTextFont",
57 "IconTitleFont",
58 "DisplayFont",
59 "LargeDisplayFont",
60 "WindowTitleExtendSpace",
61 "MenuTitleExtendSpace",
62 "MenuTextExtendSpace",
63 "HighlightColor",
64 "HighlightTextColor",
65 "ClipTitleColor",
66 "CClipTitleColor",
67 "FTitleColor",
68 "PTitleColor",
69 "UTitleColor",
70 "FTitleBack",
71 "PTitleBack",
72 "UTitleBack",
73 "ResizebarBack",
74 "MenuTitleColor",
75 "MenuTextColor",
76 "MenuDisabledColor",
77 "MenuTitleBack",
78 "MenuTextBack",
79 "IconBack",
80 "IconTitleColor",
81 "IconTitleBack",
82 "MenuStyle",
83 "WindowTitleExtendSpace",
84 "MenuTitleExtendSpace",
85 "MenuTextExtendSpace",
86 NULL
89 /* table of theme related options */
90 static char *theme_options[] = {
91 "WorkspaceBack",
92 "NormalCursor",
93 "ArrowCursor",
94 "MoveCursor",
95 "ResizeCursor",
96 "TopLeftResizeCursor",
97 "TopRightResizeCursor",
98 "BottomLeftResizeCursor",
99 "BottomRightResizeCursor",
100 "VerticalResizeCursor",
101 "HorizontalResizeCursor",
102 "WaitCursor",
103 "QuestionCursor",
104 "TextCursor",
105 "SelectCursor",
106 NULL
109 /* table of style related fonts */
111 static char *font_options[] = {
112 "ClipTitleFont",
113 "WindowTitleFont",
114 "MenuTitleFont",
115 "MenuTextFont",
116 "IconTitleFont",
117 "DisplayFont",
118 "LargeDisplayFont",
119 NULL
122 extern char *__progname;
124 WMPropList *PixmapPath = NULL;
126 char *ThemePath = NULL;
128 extern char *convertFont(char *font, Bool keepXLFD);
130 void print_help()
132 printf("Usage: %s [OPTIONS] [FILE]\n", __progname);
133 puts("Retrieves style/theme configuration and output to FILE or to stdout");
134 puts("");
135 puts(" -t, --theme-options output theme related options when producing a style file");
136 puts(" -p, --pack produce output as a theme pack");
137 puts(" -h, --help display this help and exit");
138 puts(" -v, --version output version information and exit");
141 void abortar(char *reason)
143 printf("%s: %s\n", __progname, reason);
144 if (ThemePath) {
145 printf("Removing unfinished theme pack\n");
146 (void)wrmdirhier(ThemePath);
148 exit(1);
151 static Bool isFontOption(char *option)
153 int i;
155 for (i = 0; font_options[i] != NULL; i++) {
156 if (strcasecmp(option, font_options[i]) == 0) {
157 return True;
161 return False;
165 * it is more or less assumed that this function will only
166 * copy reasonably-sized files
168 void copyFile(char *dir, char *file)
170 int from_fd, to_fd;
171 size_t block, len;
172 char buf[4096];
173 struct stat st;
174 char *dst;
176 /* only to a directory */
177 if (stat(dir, &st) != 0 || !S_ISDIR(st.st_mode))
178 return;
179 /* only copy files */
180 if (stat(file, &st) != 0 || !S_ISREG(st.st_mode))
181 return;
183 len = strlen(dir) + 1 /* / */ + strlen(file) + 1 /* '\0' */;
184 dst = wmalloc(len);
185 snprintf(dst, len, "%s/%s", dir, basename(file));
186 buf[len] = '\0';
188 if ((to_fd = open(dst, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == -1) {
189 wfree(dst);
190 return;
192 wfree(dst);
193 if ((from_fd = open(file, O_RDONLY)) == -1) {
194 (void)close(to_fd);
195 return;
198 /* XXX: signal handling */
199 while ((block = read(from_fd, &buf, sizeof(buf))) > 0)
200 write(to_fd, &buf, block);
202 (void)fsync(to_fd);
203 (void)fchmod(to_fd, st.st_mode);
204 (void)close(to_fd);
205 (void)close(from_fd);
208 void findCopyFile(char *dir, char *file)
210 char *fullPath;
212 fullPath = wfindfileinarray(PixmapPath, file);
213 if (!fullPath) {
214 char buffer[4000];
216 sprintf(buffer, "could not find file %s", file);
217 abortar(buffer);
219 copyFile(dir, fullPath);
220 free(fullPath);
223 void makeThemePack(WMPropList * style, char *themeName)
225 WMPropList *keys;
226 WMPropList *key;
227 WMPropList *value;
228 int i;
229 size_t themeNameLen;
230 char *themeDir, *t;
232 if ((t = wusergnusteppath()) == NULL)
233 return;
234 themeNameLen = strlen(t) + 1 /* / */ + strlen(themeName) + 8 /* ".themed/" */ + 1 /* '\0' */;
235 themeDir = wmalloc(themeNameLen);
236 snprintf(themeDir, themeNameLen, "%s/%s.themed/", t, themeName);
237 ThemePath = themeDir;
238 if (!wmkdirhier(themeDir))
239 return;
241 keys = WMGetPLDictionaryKeys(style);
243 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
244 key = WMGetFromPLArray(keys, i);
246 value = WMGetFromPLDictionary(style, key);
247 if (value && WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
248 WMPropList *type;
249 char *t;
251 type = WMGetFromPLArray(value, 0);
252 t = WMGetFromPLString(type);
253 if (t == NULL)
254 continue;
256 if (strcasecmp(t, "tpixmap") == 0
257 || strcasecmp(t, "spixmap") == 0
258 || strcasecmp(t, "cpixmap") == 0
259 || strcasecmp(t, "mpixmap") == 0
260 || strcasecmp(t, "tdgradient") == 0
261 || strcasecmp(t, "tvgradient") == 0 || strcasecmp(t, "thgradient") == 0) {
262 WMPropList *file;
263 char *p;
264 char *newPath;
266 file = WMGetFromPLArray(value, 1);
268 p = strrchr(WMGetFromPLString(file), '/');
269 if (p) {
270 copyFile(themeDir, WMGetFromPLString(file));
272 newPath = wstrdup(p + 1);
273 WMDeleteFromPLArray(value, 1);
274 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
275 free(newPath);
276 } else {
277 findCopyFile(themeDir, WMGetFromPLString(file));
279 } else if (strcasecmp(t, "bitmap") == 0) {
280 WMPropList *file;
281 char *p;
282 char *newPath;
284 file = WMGetFromPLArray(value, 1);
286 p = strrchr(WMGetFromPLString(file), '/');
287 if (p) {
288 copyFile(themeDir, WMGetFromPLString(file));
290 newPath = wstrdup(p + 1);
291 WMDeleteFromPLArray(value, 1);
292 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
293 free(newPath);
294 } else {
295 findCopyFile(themeDir, WMGetFromPLString(file));
298 file = WMGetFromPLArray(value, 2);
300 p = strrchr(WMGetFromPLString(file), '/');
301 if (p) {
302 copyFile(themeDir, WMGetFromPLString(file));
304 newPath = wstrdup(p + 1);
305 WMDeleteFromPLArray(value, 2);
306 WMInsertInPLArray(value, 2, WMCreatePLString(newPath));
307 free(newPath);
308 } else {
309 findCopyFile(themeDir, WMGetFromPLString(file));
316 int main(int argc, char **argv)
318 WMPropList *prop, *style, *key, *val;
319 char *path, *p;
320 int i, theme_too = 0, make_pack = 0;
321 char *style_file = NULL;
323 if (argc > 1) {
324 for (i = 1; i < argc; i++) {
325 if (strcmp(argv[i], "-p") == 0 || strcmp(argv[i], "--pack") == 0) {
326 make_pack = 1;
327 theme_too = 1;
328 } else if (strcmp(argv[i], "-t") == 0 || strcmp(argv[i], "--theme-options") == 0) {
329 theme_too = 1;
330 } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
331 print_help();
332 exit(0);
333 } else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
334 puts(PROG_VERSION);
335 exit(0);
336 } else {
337 if (style_file != NULL) {
338 printf("%s: invalid argument '%s'\n", __progname,
339 style_file[0] == '-' ? style_file : argv[i]);
340 printf("Try '%s --help' for more information\n", __progname);
341 exit(1);
343 style_file = argv[i];
344 while ((p = strchr(style_file, '/')) != NULL)
345 *p = '_';
350 if (style_file && !make_pack) {
351 print_help();
352 exit(1);
355 if (make_pack && !style_file) {
356 printf("%s: you must supply a name for the theme pack\n", __progname);
357 exit(1);
360 WMPLSetCaseSensitive(False);
362 path = wdefaultspathfordomain("WindowMaker");
364 prop = WMReadPropListFromFile(path);
365 if (!prop) {
366 printf("%s: could not load WindowMaker configuration file \"%s\".\n", __progname, path);
367 exit(1);
370 /* get global value */
371 path = wglobaldefaultspathfordomain("WindowMaker");
373 val = WMReadPropListFromFile(path);
374 if (val) {
375 WMMergePLDictionaries(val, prop, True);
376 WMReleasePropList(prop);
377 prop = val;
380 style = WMCreatePLDictionary(NULL, NULL);
382 for (i = 0; options[i] != NULL; i++) {
383 key = WMCreatePLString(options[i]);
385 val = WMGetFromPLDictionary(prop, key);
386 if (val) {
387 WMRetainPropList(val);
388 if (isFontOption(options[i])) {
389 char *newfont, *oldfont;
391 oldfont = WMGetFromPLString(val);
392 newfont = convertFont(oldfont, False);
393 /* newfont is a reference to old if conversion is not needed */
394 if (newfont != oldfont) {
395 WMReleasePropList(val);
396 val = WMCreatePLString(newfont);
397 wfree(newfont);
400 WMPutInPLDictionary(style, key, val);
401 WMReleasePropList(val);
403 WMReleasePropList(key);
406 val = WMGetFromPLDictionary(prop, WMCreatePLString("PixmapPath"));
407 if (val)
408 PixmapPath = val;
410 if (theme_too) {
411 for (i = 0; theme_options[i] != NULL; i++) {
412 key = WMCreatePLString(theme_options[i]);
414 val = WMGetFromPLDictionary(prop, key);
415 if (val)
416 WMPutInPLDictionary(style, key, val);
420 if (make_pack) {
421 char *path;
423 makeThemePack(style, style_file);
425 path = wmalloc(strlen(ThemePath) + 32);
426 strcpy(path, ThemePath);
427 strcat(path, "/style");
428 WMWritePropListToFile(style, path);
429 wfree(path);
430 } else {
431 if (style_file) {
432 WMWritePropListToFile(style, style_file);
433 } else {
434 puts(WMGetPropListDescription(style, True));
437 exit(0);