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 */
39 #ifdef HAVE_STDNORETURN
40 #include <stdnoreturn.h>
43 #include <WINGs/WUtil.h>
45 #include "../src/wconfig.h"
49 #define MAX_OPTIONS 128
51 char *FontOptions
[] = {
62 char *CursorOptions
[] = {
67 "TopLeftResizeCursor",
68 "TopRightResizeCursor",
69 "BottomLeftResizeCursor",
70 "BottomRightResizeCursor",
71 "VerticalResizeCursor",
72 "HorizontalResizeCursor",
80 extern char *__progname
;
82 int ignoreCursors
= 0;
87 static Bool
isCursorOption(const char *option
)
91 for (i
= 0; CursorOptions
[i
] != NULL
; i
++) {
92 if (strcasecmp(option
, CursorOptions
[i
]) == 0) {
100 static Bool
isFontOption(const char *option
)
104 for (i
= 0; FontOptions
[i
] != NULL
; i
++) {
105 if (strcasecmp(option
, FontOptions
[i
]) == 0) {
114 * finds elements in `texture' that reference external files,
115 * prepends `prefix' to these files. `prefix' is a path component
116 * that qualifies the external references to be absolute, possibly
117 * pending further expansion
119 static void hackPathInTexture(WMPropList
* texture
, const char *prefix
)
124 /* get texture type */
125 type
= WMGetFromPLArray(texture
, 0);
126 t
= WMGetFromPLString(type
);
130 if (strcasecmp(t
, "tpixmap") == 0 ||
131 strcasecmp(t
, "spixmap") == 0 ||
132 strcasecmp(t
, "mpixmap") == 0 ||
133 strcasecmp(t
, "cpixmap") == 0 ||
134 strcasecmp(t
, "tvgradient") == 0 ||
135 strcasecmp(t
, "thgradient") == 0 ||
136 strcasecmp(t
, "tdgradient") == 0) {
140 /* get pixmap file path */
141 file
= WMGetFromPLArray(texture
, 1);
142 sprintf(buffer
, "%s/%s", prefix
, WMGetFromPLString(file
));
143 /* replace path with full path */
144 WMDeleteFromPLArray(texture
, 1);
145 WMInsertInPLArray(texture
, 1, WMCreatePLString(buffer
));
147 } else if (strcasecmp(t
, "bitmap") == 0) {
151 /* get bitmap file path */
152 file
= WMGetFromPLArray(texture
, 1);
153 sprintf(buffer
, "%s/%s", prefix
, WMGetFromPLString(file
));
154 /* replace path with full path */
155 WMDeleteFromPLArray(texture
, 1);
156 WMInsertInPLArray(texture
, 1, WMCreatePLString(buffer
));
158 /* get mask file path */
159 file
= WMGetFromPLArray(texture
, 2);
160 sprintf(buffer
, "%s/%s", prefix
, WMGetFromPLString(file
));
161 /* replace path with full path */
162 WMDeleteFromPLArray(texture
, 2);
163 WMInsertInPLArray(texture
, 2, WMCreatePLString(buffer
));
167 static void hackPaths(WMPropList
* style
, const char *prefix
)
174 keys
= WMGetPLDictionaryKeys(style
);
176 for (i
= 0; i
< WMGetPropListItemCount(keys
); i
++) {
177 key
= WMGetFromPLArray(keys
, i
);
179 value
= WMGetFromPLDictionary(style
, key
);
183 if (strcasecmp(WMGetFromPLString(key
), "WorkspaceSpecificBack") == 0) {
184 if (WMIsPLArray(value
)) {
188 for (j
= 0; j
< WMGetPropListItemCount(value
); j
++) {
189 texture
= WMGetFromPLArray(value
, j
);
191 if (texture
&& WMIsPLArray(texture
)
192 && WMGetPropListItemCount(texture
) > 2) {
194 hackPathInTexture(texture
, prefix
);
200 if (WMIsPLArray(value
) && WMGetPropListItemCount(value
) > 2) {
202 hackPathInTexture(value
, prefix
);
209 static WMPropList
*getColor(WMPropList
* texture
)
211 WMPropList
*value
, *type
;
214 type
= WMGetFromPLArray(texture
, 0);
220 str
= WMGetFromPLString(type
);
221 if (strcasecmp(str
, "solid") == 0) {
222 value
= WMGetFromPLArray(texture
, 1);
223 } else if (strcasecmp(str
, "dgradient") == 0
224 || strcasecmp(str
, "hgradient") == 0 || strcasecmp(str
, "vgradient") == 0) {
226 int r1
, g1
, b1
, r2
, g2
, b2
;
229 c1
= WMGetFromPLArray(texture
, 1);
230 c2
= WMGetFromPLArray(texture
, 2);
232 if (sscanf(WMGetFromPLString(c1
), "#%2x%2x%2x", &r1
, &g1
, &b1
) == 3
233 && sscanf(WMGetFromPLString(c2
), "#%2x%2x%2x", &r2
, &g2
, &b2
) == 3) {
234 sprintf(buffer
, "#%02x%02x%02x", (r1
+ r2
) / 2, (g1
+ g2
) / 2, (b1
+ b2
) / 2);
235 value
= WMCreatePLString(buffer
);
243 XParseColor(dpy
, DefaultColormap(dpy
, DefaultScreen(dpy
)), WMGetFromPLString(c1
), &color1
);
244 XParseColor(dpy
, DefaultColormap(dpy
, DefaultScreen(dpy
)), WMGetFromPLString(c2
), &color2
);
246 sprintf(buffer
, "#%02x%02x%02x",
247 (color1
.red
+ color2
.red
) >> 9,
248 (color1
.green
+ color2
.green
) >> 9, (color1
.blue
+ color2
.blue
) >> 9);
249 value
= WMCreatePLString(buffer
);
251 } else if (strcasecmp(str
, "mdgradient") == 0
252 || strcasecmp(str
, "mhgradient") == 0 || strcasecmp(str
, "mvgradient") == 0) {
254 value
= WMGetFromPLArray(texture
, 1);
256 } else if (strcasecmp(str
, "tpixmap") == 0
257 || strcasecmp(str
, "cpixmap") == 0 || strcasecmp(str
, "spixmap") == 0) {
259 value
= WMGetFromPLArray(texture
, 2);
266 * since some of the options introduce incompatibilities, we will need
267 * to do a kluge here or the themes ppl will get real annoying.
268 * So, treat for the absence of the following options:
272 static void hackStyle(WMPropList
* style
)
274 WMPropList
*keys
, *tmp
;
275 int foundIconTitle
= 0, foundResizebarBack
= 0;
278 keys
= WMGetPLDictionaryKeys(style
);
280 for (i
= 0; i
< WMGetPropListItemCount(keys
); i
++) {
283 tmp
= WMGetFromPLArray(keys
, i
);
284 str
= WMGetFromPLString(tmp
);
286 if (ignoreFonts
&& isFontOption(str
)) {
287 WMRemoveFromPLDictionary(style
, tmp
);
290 if (ignoreCursors
&& isCursorOption(str
)) {
291 WMRemoveFromPLDictionary(style
, tmp
);
294 if (isFontOption(str
)) {
296 char *newfont
, *oldfont
;
298 value
= WMGetFromPLDictionary(style
, tmp
);
300 oldfont
= WMGetFromPLString(value
);
301 newfont
= convertFont(oldfont
, False
);
302 if (newfont
!= oldfont
) {
303 value
= WMCreatePLString(newfont
);
304 WMPutInPLDictionary(style
, tmp
, value
);
305 WMReleasePropList(value
);
310 if (strcasecmp(str
, "IconTitleColor") == 0 || strcasecmp(str
, "IconTitleBack") == 0) {
312 } else if (strcasecmp(str
, "ResizebarBack") == 0) {
313 foundResizebarBack
= 1;
318 if (!foundIconTitle
) {
319 /* set the default values */
320 tmp
= WMGetFromPLDictionary(style
, WMCreatePLString("FTitleColor"));
322 WMPutInPLDictionary(style
, WMCreatePLString("IconTitleColor"), tmp
);
325 tmp
= WMGetFromPLDictionary(style
, WMCreatePLString("FTitleBack"));
329 value
= getColor(tmp
);
332 WMPutInPLDictionary(style
, WMCreatePLString("IconTitleBack"), value
);
337 if (!foundResizebarBack
) {
338 /* set the default values */
339 tmp
= WMGetFromPLDictionary(style
, WMCreatePLString("UTitleBack"));
343 value
= getColor(tmp
);
348 t
= WMCreatePLArray(WMCreatePLString("solid"), value
, NULL
);
349 WMPutInPLDictionary(style
, WMCreatePLString("ResizebarBack"), t
);
354 if (!WMGetFromPLDictionary(style
, WMCreatePLString("MenuStyle"))) {
355 WMPutInPLDictionary(style
, WMCreatePLString("MenuStyle"), WMCreatePLString("normal"));
359 static noreturn
void print_help(int print_usage
, int exitval
)
361 printf("Usage: %s [OPTIONS] FILE\n", __progname
);
363 puts("Reads style/theme configuration from FILE and updates Window Maker.");
365 puts(" --no-fonts ignore font related options");
366 puts(" --no-cursors ignore cursor related options");
367 puts(" --ignore <option> ignore changes in the specified option");
368 puts(" -h, --help display this help and exit");
369 puts(" -v, --version output version information and exit");
374 int main(int argc
, char **argv
)
376 WMPropList
*prop
, *style
;
380 int i
, ch
, ignflag
= 0;
382 char *ignoreList
[MAX_OPTIONS
];
385 struct option longopts
[] = {
386 { "version", no_argument
, NULL
, 'v' },
387 { "help", no_argument
, NULL
, 'h' },
388 { "no-fonts", no_argument
, &ignoreFonts
, 1 },
389 { "no-cursors", no_argument
, &ignoreCursors
, 1 },
390 { "ignore", required_argument
, &ignflag
, 1 },
394 while ((ch
= getopt_long(argc
, argv
, "hv", longopts
, NULL
)) != -1)
397 printf("%s (Window Maker %s)\n", __progname
, VERSION
);
405 if (ignoreCount
>= MAX_OPTIONS
) {
406 printf("Maximum %d `ignore' arguments\n", MAX_OPTIONS
);
409 ignoreList
[ignoreCount
++] = optarg
;
426 WMPLSetCaseSensitive(False
);
428 path
= wdefaultspathfordomain("WindowMaker");
430 prop
= WMReadPropListFromFile(path
);
433 printf("%s: could not load WindowMaker configuration file.\n", __progname
);
437 if (stat(file
, &st
) < 0) {
441 if (S_ISDIR(st
.st_mode
)) { /* theme pack */
445 if (realpath(file
, buf
) == NULL
) {
449 strncat(buf
, "/style", sizeof(buf
) - strlen(buf
) - 1);
451 if (stat(buf
, &st
) != 0 || !S_ISREG(st
.st_mode
)) { /* maybe symlink too? */
452 printf("%s: %s: style file not found or not a file\n", __progname
, buf
);
456 style
= WMReadPropListFromFile(buf
);
459 printf("%s: could not load style file.\n", __progname
);
463 buf
[strlen(buf
) - 6 /* strlen("/style") */] = '\0';
464 homedir
= wstrdup(wgethomedir());
465 if (strlen(homedir
) > 1 && /* this is insane, wgethomedir() returns `/' on error */
466 strncmp(homedir
, buf
, strlen(homedir
)) == 0) {
467 /* theme pack is under ${HOME}; exchange ${HOME} part
468 * for `~' so it gets portable references to the user home dir */
470 memmove(buf
+ 1, buf
+ strlen(homedir
), strlen(buf
) - strlen(homedir
) + 1);
474 hackPaths(style
, buf
); /* this will prefix pixmaps in the style
475 * with absolute(ish) references */
477 } else { /* normal style file */
479 style
= WMReadPropListFromFile(file
);
482 printf("%s:could not load style file.\n", __progname
);
487 if (!WMIsPLDictionary(style
)) {
488 printf("%s: '%s' is not a style file/theme\n", __progname
, file
);
494 if (ignoreCount
> 0) {
495 for (i
= 0; i
< ignoreCount
; i
++) {
496 WMRemoveFromPLDictionary(style
, WMCreatePLString(ignoreList
[i
]));
500 WMMergePLDictionaries(prop
, style
, True
);
502 WMWritePropListToFile(prop
, path
);
504 dpy
= XOpenDisplay("");
506 memset(&ev
, 0, sizeof(XEvent
));
508 ev
.xclient
.type
= ClientMessage
;
509 ev
.xclient
.message_type
= XInternAtom(dpy
, "_WINDOWMAKER_COMMAND", False
);
510 ev
.xclient
.window
= DefaultRootWindow(dpy
);
511 ev
.xclient
.format
= 8;
512 strncpy(ev
.xclient
.data
.b
, "Reconfigure", sizeof(ev
.xclient
.data
.b
));
514 XSendEvent(dpy
, DefaultRootWindow(dpy
), False
, SubstructureRedirectMask
, &ev
);