1 /* setstyle.c - loads style related options to wmaker
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.
23 #define _GNU_SOURCE /* getopt_long */
37 #include <WINGs/WUtil.h>
39 #include "../src/wconfig.h"
43 #define MAX_OPTIONS 128
45 char *FontOptions
[] = {
56 char *CursorOptions
[] = {
61 "TopLeftResizeCursor",
62 "TopRightResizeCursor",
63 "BottomLeftResizeCursor",
64 "BottomRightResizeCursor",
65 "VerticalResizeCursor",
66 "HorizontalResizeCursor",
74 extern char *__progname
;
76 int ignoreCursors
= 0;
81 static Bool
isCursorOption(const char *option
)
85 for (i
= 0; CursorOptions
[i
] != NULL
; i
++) {
86 if (strcasecmp(option
, CursorOptions
[i
]) == 0) {
94 static Bool
isFontOption(const char *option
)
98 for (i
= 0; FontOptions
[i
] != NULL
; i
++) {
99 if (strcasecmp(option
, FontOptions
[i
]) == 0) {
108 * finds elements in `texture' that reference external files,
109 * prepends `prefix' to these files. `prefix' is a path component
110 * that qualifies the external references to be absolute, possibly
111 * pending further expansion
113 static void hackPathInTexture(WMPropList
* texture
, const char *prefix
)
118 /* get texture type */
119 type
= WMGetFromPLArray(texture
, 0);
120 t
= WMGetFromPLString(type
);
124 if (strcasecmp(t
, "tpixmap") == 0 ||
125 strcasecmp(t
, "spixmap") == 0 ||
126 strcasecmp(t
, "mpixmap") == 0 ||
127 strcasecmp(t
, "cpixmap") == 0 ||
128 strcasecmp(t
, "tvgradient") == 0 ||
129 strcasecmp(t
, "thgradient") == 0 ||
130 strcasecmp(t
, "tdgradient") == 0) {
134 /* get pixmap file path */
135 file
= WMGetFromPLArray(texture
, 1);
136 sprintf(buffer
, "%s/%s", prefix
, WMGetFromPLString(file
));
137 /* replace path with full path */
138 WMDeleteFromPLArray(texture
, 1);
139 WMInsertInPLArray(texture
, 1, WMCreatePLString(buffer
));
141 } else if (strcasecmp(t
, "bitmap") == 0) {
145 /* get bitmap file path */
146 file
= WMGetFromPLArray(texture
, 1);
147 sprintf(buffer
, "%s/%s", prefix
, WMGetFromPLString(file
));
148 /* replace path with full path */
149 WMDeleteFromPLArray(texture
, 1);
150 WMInsertInPLArray(texture
, 1, WMCreatePLString(buffer
));
152 /* get mask file path */
153 file
= WMGetFromPLArray(texture
, 2);
154 sprintf(buffer
, "%s/%s", prefix
, WMGetFromPLString(file
));
155 /* replace path with full path */
156 WMDeleteFromPLArray(texture
, 2);
157 WMInsertInPLArray(texture
, 2, WMCreatePLString(buffer
));
161 static void hackPaths(WMPropList
* style
, const char *prefix
)
168 keys
= WMGetPLDictionaryKeys(style
);
170 for (i
= 0; i
< WMGetPropListItemCount(keys
); i
++) {
171 key
= WMGetFromPLArray(keys
, i
);
173 value
= WMGetFromPLDictionary(style
, key
);
177 if (strcasecmp(WMGetFromPLString(key
), "WorkspaceSpecificBack") == 0) {
178 if (WMIsPLArray(value
)) {
182 for (j
= 0; j
< WMGetPropListItemCount(value
); j
++) {
183 texture
= WMGetFromPLArray(value
, j
);
185 if (texture
&& WMIsPLArray(texture
)
186 && WMGetPropListItemCount(texture
) > 2) {
188 hackPathInTexture(texture
, prefix
);
194 if (WMIsPLArray(value
) && WMGetPropListItemCount(value
) > 2) {
196 hackPathInTexture(value
, prefix
);
203 static WMPropList
*getColor(WMPropList
* texture
)
205 WMPropList
*value
, *type
;
208 type
= WMGetFromPLArray(texture
, 0);
214 str
= WMGetFromPLString(type
);
215 if (strcasecmp(str
, "solid") == 0) {
216 value
= WMGetFromPLArray(texture
, 1);
217 } else if (strcasecmp(str
, "dgradient") == 0
218 || strcasecmp(str
, "hgradient") == 0 || strcasecmp(str
, "vgradient") == 0) {
220 int r1
, g1
, b1
, r2
, g2
, b2
;
223 c1
= WMGetFromPLArray(texture
, 1);
224 c2
= WMGetFromPLArray(texture
, 2);
226 if (sscanf(WMGetFromPLString(c1
), "#%2x%2x%2x", &r1
, &g1
, &b1
) == 3
227 && sscanf(WMGetFromPLString(c2
), "#%2x%2x%2x", &r2
, &g2
, &b2
) == 3) {
228 sprintf(buffer
, "#%02x%02x%02x", (r1
+ r2
) / 2, (g1
+ g2
) / 2, (b1
+ b2
) / 2);
229 value
= WMCreatePLString(buffer
);
237 XParseColor(dpy
, DefaultColormap(dpy
, DefaultScreen(dpy
)), WMGetFromPLString(c1
), &color1
);
238 XParseColor(dpy
, DefaultColormap(dpy
, DefaultScreen(dpy
)), WMGetFromPLString(c2
), &color2
);
240 sprintf(buffer
, "#%02x%02x%02x",
241 (color1
.red
+ color2
.red
) >> 9,
242 (color1
.green
+ color2
.green
) >> 9, (color1
.blue
+ color2
.blue
) >> 9);
243 value
= WMCreatePLString(buffer
);
245 } else if (strcasecmp(str
, "mdgradient") == 0
246 || strcasecmp(str
, "mhgradient") == 0 || strcasecmp(str
, "mvgradient") == 0) {
248 value
= WMGetFromPLArray(texture
, 1);
250 } else if (strcasecmp(str
, "tpixmap") == 0
251 || strcasecmp(str
, "cpixmap") == 0 || strcasecmp(str
, "spixmap") == 0) {
253 value
= WMGetFromPLArray(texture
, 2);
260 * since some of the options introduce incompatibilities, we will need
261 * to do a kluge here or the themes ppl will get real annoying.
262 * So, treat for the absence of the following options:
266 static void hackStyle(WMPropList
* style
)
268 WMPropList
*keys
, *tmp
;
269 int foundIconTitle
= 0, foundResizebarBack
= 0;
272 keys
= WMGetPLDictionaryKeys(style
);
274 for (i
= 0; i
< WMGetPropListItemCount(keys
); i
++) {
277 tmp
= WMGetFromPLArray(keys
, i
);
278 str
= WMGetFromPLString(tmp
);
280 if (ignoreFonts
&& isFontOption(str
)) {
281 WMRemoveFromPLDictionary(style
, tmp
);
284 if (ignoreCursors
&& isCursorOption(str
)) {
285 WMRemoveFromPLDictionary(style
, tmp
);
288 if (isFontOption(str
)) {
290 char *newfont
, *oldfont
;
292 value
= WMGetFromPLDictionary(style
, tmp
);
294 oldfont
= WMGetFromPLString(value
);
295 newfont
= convertFont(oldfont
, False
);
296 if (newfont
!= oldfont
) {
297 value
= WMCreatePLString(newfont
);
298 WMPutInPLDictionary(style
, tmp
, value
);
299 WMReleasePropList(value
);
304 if (strcasecmp(str
, "IconTitleColor") == 0 || strcasecmp(str
, "IconTitleBack") == 0) {
306 } else if (strcasecmp(str
, "ResizebarBack") == 0) {
307 foundResizebarBack
= 1;
312 if (!foundIconTitle
) {
313 /* set the default values */
314 tmp
= WMGetFromPLDictionary(style
, WMCreatePLString("FTitleColor"));
316 WMPutInPLDictionary(style
, WMCreatePLString("IconTitleColor"), tmp
);
319 tmp
= WMGetFromPLDictionary(style
, WMCreatePLString("FTitleBack"));
323 value
= getColor(tmp
);
326 WMPutInPLDictionary(style
, WMCreatePLString("IconTitleBack"), value
);
331 if (!foundResizebarBack
) {
332 /* set the default values */
333 tmp
= WMGetFromPLDictionary(style
, WMCreatePLString("UTitleBack"));
337 value
= getColor(tmp
);
342 t
= WMCreatePLArray(WMCreatePLString("solid"), value
, NULL
);
343 WMPutInPLDictionary(style
, WMCreatePLString("ResizebarBack"), t
);
348 if (!WMGetFromPLDictionary(style
, WMCreatePLString("MenuStyle"))) {
349 WMPutInPLDictionary(style
, WMCreatePLString("MenuStyle"), WMCreatePLString("normal"));
353 static void print_help(int print_usage
, int exitval
)
355 printf("Usage: %s [OPTIONS] FILE\n", __progname
);
357 puts("Reads style/theme configuration from FILE and updates Window Maker.");
359 puts(" --no-fonts ignore font related options");
360 puts(" --no-cursors ignore cursor related options");
361 puts(" --ignore <option> ignore changes in the specified option");
362 puts(" -h, --help display this help and exit");
363 puts(" -v, --version output version information and exit");
368 int main(int argc
, char **argv
)
370 WMPropList
*prop
, *style
;
374 int i
, ch
, ignflag
= 0;
376 char *ignoreList
[MAX_OPTIONS
];
379 struct option longopts
[] = {
380 { "version", no_argument
, NULL
, 'v' },
381 { "help", no_argument
, NULL
, 'h' },
382 { "no-fonts", no_argument
, &ignoreFonts
, 1 },
383 { "no-cursors", no_argument
, &ignoreCursors
, 1 },
384 { "ignore", required_argument
, &ignflag
, 1 },
388 while ((ch
= getopt_long(argc
, argv
, "hv", longopts
, NULL
)) != -1)
391 printf("%s (Window Maker %s)\n", __progname
, VERSION
);
399 if (ignoreCount
>= MAX_OPTIONS
) {
400 printf("Maximum %d `ignore' arguments\n", MAX_OPTIONS
);
403 ignoreList
[ignoreCount
++] = optarg
;
420 WMPLSetCaseSensitive(False
);
422 path
= wdefaultspathfordomain("WindowMaker");
424 prop
= WMReadPropListFromFile(path
);
427 printf("%s: could not load WindowMaker configuration file.\n", __progname
);
431 if (stat(file
, &st
) < 0) {
435 if (S_ISDIR(st
.st_mode
)) { /* theme pack */
439 if (realpath(file
, buf
) == NULL
) {
443 strncat(buf
, "/style", sizeof(buf
) - strlen(buf
));
445 if (stat(buf
, &st
) != 0 || !S_ISREG(st
.st_mode
)) { /* maybe symlink too? */
446 printf("%s: %s: style file not found or not a file\n", __progname
, buf
);
450 style
= WMReadPropListFromFile(buf
);
453 printf("%s: could not load style file.\n", __progname
);
457 buf
[strlen(buf
) - 6 /* strlen("/style") */] = '\0';
458 homedir
= wstrdup(wgethomedir());
459 if (strlen(homedir
) > 1 && /* this is insane, wgethomedir() returns `/' on error */
460 strncmp(homedir
, buf
, strlen(homedir
)) == 0) {
461 /* theme pack is under ${HOME}; exchange ${HOME} part
462 * for `~' so it gets portable references to the user home dir */
464 memmove(buf
+ 1, buf
+ strlen(homedir
), strlen(buf
) - strlen(homedir
) + 1);
468 hackPaths(style
, buf
); /* this will prefix pixmaps in the style
469 * with absolute(ish) references */
471 } else { /* normal style file */
473 style
= WMReadPropListFromFile(file
);
476 printf("%s:could not load style file.\n", __progname
);
481 if (!WMIsPLDictionary(style
)) {
482 printf("%s: '%s' is not a style file/theme\n", __progname
, file
);
488 if (ignoreCount
> 0) {
489 for (i
= 0; i
< ignoreCount
; i
++) {
490 WMRemoveFromPLDictionary(style
, WMCreatePLString(ignoreList
[i
]));
494 WMMergePLDictionaries(prop
, style
, True
);
496 WMWritePropListToFile(prop
, path
);
498 dpy
= XOpenDisplay("");
500 memset(&ev
, 0, sizeof(XEvent
));
502 ev
.xclient
.type
= ClientMessage
;
503 ev
.xclient
.message_type
= XInternAtom(dpy
, "_WINDOWMAKER_COMMAND", False
);
504 ev
.xclient
.window
= DefaultRootWindow(dpy
);
505 ev
.xclient
.format
= 8;
506 strncpy(ev
.xclient
.data
.b
, "Reconfigure", sizeof(ev
.xclient
.data
.b
));
508 XSendEvent(dpy
, DefaultRootWindow(dpy
), False
, SubstructureRedirectMask
, &ev
);