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,
23 #define PROG_VERSION "getstyle (Window Maker) 0.6"
25 #include <sys/types.h>
38 #include <WINGs/WUtil.h>
44 #include "../src/wconfig.h"
46 #ifndef GLOBAL_DEFAULTS_SUBDIR
47 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
50 /* table of style related options */
51 static char *options
[] = {
60 "WindowTitleExtendSpace",
61 "MenuTitleExtendSpace",
62 "MenuTextExtendSpace",
83 "WindowTitleExtendSpace",
84 "MenuTitleExtendSpace",
85 "MenuTextExtendSpace",
89 /* table of theme related options */
90 static char *theme_options
[] = {
96 "TopLeftResizeCursor",
97 "TopRightResizeCursor",
98 "BottomLeftResizeCursor",
99 "BottomRightResizeCursor",
100 "VerticalResizeCursor",
101 "HorizontalResizeCursor",
109 /* table of style related fonts */
111 static char *font_options
[] = {
122 extern char *__progname
;
124 WMPropList
*PixmapPath
= NULL
;
126 char *ThemePath
= NULL
;
128 extern char *convertFont(char *font
, Bool keepXLFD
);
132 printf("Usage: %s [OPTIONS] [FILE]\n", __progname
);
133 puts("Retrieves style/theme configuration and output to FILE or to stdout");
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
);
145 printf("Removing unfinished theme pack\n");
146 (void)wrmdirhier(ThemePath
);
151 static Bool
isFontOption(char *option
)
155 for (i
= 0; font_options
[i
] != NULL
; i
++) {
156 if (strcasecmp(option
, font_options
[i
]) == 0) {
165 * it is more or less assumed that this function will only
166 * copy reasonably-sized files
168 void copyFile(char *dir
, char *file
)
176 /* only to a directory */
177 if (stat(dir
, &st
) != 0 || !S_ISDIR(st
.st_mode
))
179 /* only copy files */
180 if (stat(file
, &st
) != 0 || !S_ISREG(st
.st_mode
))
183 len
= strlen(dir
) + 1 /* / */ + strlen(file
) + 1 /* '\0' */;
185 snprintf(dst
, len
, "%s/%s", dir
, basename(file
));
188 if ((to_fd
= open(dst
, O_RDWR
|O_CREAT
|O_TRUNC
, S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
)) == -1) {
193 if ((from_fd
= open(file
, O_RDONLY
)) == -1) {
198 /* XXX: signal handling */
199 while ((block
= read(from_fd
, &buf
, sizeof(buf
))) > 0)
200 write(to_fd
, &buf
, block
);
203 (void)fchmod(to_fd
, st
.st_mode
);
205 (void)close(from_fd
);
208 void findCopyFile(char *dir
, char *file
)
212 fullPath
= wfindfileinarray(PixmapPath
, file
);
216 sprintf(buffer
, "could not find file %s", file
);
219 copyFile(dir
, fullPath
);
223 void makeThemePack(WMPropList
* style
, char *themeName
)
232 if ((t
= wusergnusteppath()) == NULL
)
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
))
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) {
251 type
= WMGetFromPLArray(value
, 0);
252 t
= WMGetFromPLString(type
);
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) {
266 file
= WMGetFromPLArray(value
, 1);
268 p
= strrchr(WMGetFromPLString(file
), '/');
270 copyFile(themeDir
, WMGetFromPLString(file
));
272 newPath
= wstrdup(p
+ 1);
273 WMDeleteFromPLArray(value
, 1);
274 WMInsertInPLArray(value
, 1, WMCreatePLString(newPath
));
277 findCopyFile(themeDir
, WMGetFromPLString(file
));
279 } else if (strcasecmp(t
, "bitmap") == 0) {
284 file
= WMGetFromPLArray(value
, 1);
286 p
= strrchr(WMGetFromPLString(file
), '/');
288 copyFile(themeDir
, WMGetFromPLString(file
));
290 newPath
= wstrdup(p
+ 1);
291 WMDeleteFromPLArray(value
, 1);
292 WMInsertInPLArray(value
, 1, WMCreatePLString(newPath
));
295 findCopyFile(themeDir
, WMGetFromPLString(file
));
298 file
= WMGetFromPLArray(value
, 2);
300 p
= strrchr(WMGetFromPLString(file
), '/');
302 copyFile(themeDir
, WMGetFromPLString(file
));
304 newPath
= wstrdup(p
+ 1);
305 WMDeleteFromPLArray(value
, 2);
306 WMInsertInPLArray(value
, 2, WMCreatePLString(newPath
));
309 findCopyFile(themeDir
, WMGetFromPLString(file
));
316 int main(int argc
, char **argv
)
318 WMPropList
*prop
, *style
, *key
, *val
;
320 int i
, theme_too
= 0, make_pack
= 0;
321 char *style_file
= NULL
;
324 for (i
= 1; i
< argc
; i
++) {
325 if (strcmp(argv
[i
], "-p") == 0 || strcmp(argv
[i
], "--pack") == 0) {
328 } else if (strcmp(argv
[i
], "-t") == 0 || strcmp(argv
[i
], "--theme-options") == 0) {
330 } else if (strcmp(argv
[i
], "-h") == 0 || strcmp(argv
[i
], "--help") == 0) {
333 } else if (strcmp(argv
[i
], "-v") == 0 || strcmp(argv
[i
], "--version") == 0) {
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
);
343 style_file
= argv
[i
];
344 while ((p
= strchr(style_file
, '/')) != NULL
)
350 if (style_file
&& !make_pack
) {
355 if (make_pack
&& !style_file
) {
356 printf("%s: you must supply a name for the theme pack\n", __progname
);
360 WMPLSetCaseSensitive(False
);
362 path
= wdefaultspathfordomain("WindowMaker");
364 prop
= WMReadPropListFromFile(path
);
366 printf("%s: could not load WindowMaker configuration file \"%s\".\n", __progname
, path
);
370 /* get global value */
371 path
= wglobaldefaultspathfordomain("WindowMaker");
373 val
= WMReadPropListFromFile(path
);
375 WMMergePLDictionaries(val
, prop
, True
);
376 WMReleasePropList(prop
);
380 style
= WMCreatePLDictionary(NULL
, NULL
);
382 for (i
= 0; options
[i
] != NULL
; i
++) {
383 key
= WMCreatePLString(options
[i
]);
385 val
= WMGetFromPLDictionary(prop
, key
);
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
);
400 WMPutInPLDictionary(style
, key
, val
);
401 WMReleasePropList(val
);
403 WMReleasePropList(key
);
406 val
= WMGetFromPLDictionary(prop
, WMCreatePLString("PixmapPath"));
411 for (i
= 0; theme_options
[i
] != NULL
; i
++) {
412 key
= WMCreatePLString(theme_options
[i
]);
414 val
= WMGetFromPLDictionary(prop
, key
);
416 WMPutInPLDictionary(style
, key
, val
);
423 makeThemePack(style
, style_file
);
425 path
= wmalloc(strlen(ThemePath
) + 32);
426 strcpy(path
, ThemePath
);
427 strcat(path
, "/style");
428 WMWritePropListToFile(style
, path
);
432 WMWritePropListToFile(style
, style_file
);
434 puts(WMGetPropListDescription(style
, True
));