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
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 "setstyle (Window Maker) 0.6"
31 #include <WINGs/WUtil.h>
33 #include "../src/wconfig.h"
35 #define MAX_OPTIONS 128
37 char *FontOptions
[] = {
48 char *CursorOptions
[] = {
53 "TopLeftResizeCursor",
54 "TopRightResizeCursor",
55 "BottomLeftResizeCursor",
56 "BottomRightResizeCursor",
57 "VerticalResizeCursor",
58 "HorizontalResizeCursor",
68 int ignoreCursors
= 0;
72 extern char *convertFont(char *font
, Bool keepXLFD
);
74 WMPropList
*readBlackBoxStyle(char *path
);
76 static Bool
isCursorOption(char *option
)
80 for (i
= 0; CursorOptions
[i
] != NULL
; i
++) {
81 if (strcasecmp(option
, CursorOptions
[i
]) == 0) {
89 static Bool
isFontOption(char *option
)
93 for (i
= 0; FontOptions
[i
] != NULL
; i
++) {
94 if (strcasecmp(option
, FontOptions
[i
]) == 0) {
102 char *defaultsPathForDomain(char *domain
)
104 static char path
[1024];
107 gspath
= getenv("GNUSTEP_USER_ROOT");
109 strcpy(path
, gspath
);
114 home
= getenv("HOME");
116 printf("%s:could not get HOME environment variable!\n", ProgName
);
121 strcat(path
, "/GNUstep/");
123 strcat(path
, DEFAULTS_DIR
);
125 strcat(path
, domain
);
130 void hackPathInTexture(WMPropList
* texture
, char *prefix
)
135 /* get texture type */
136 type
= WMGetFromPLArray(texture
, 0);
137 t
= WMGetFromPLString(type
);
140 if (strcasecmp(t
, "tpixmap") == 0
141 || strcasecmp(t
, "spixmap") == 0
142 || strcasecmp(t
, "mpixmap") == 0
143 || strcasecmp(t
, "cpixmap") == 0
144 || strcasecmp(t
, "tvgradient") == 0
145 || strcasecmp(t
, "thgradient") == 0 || strcasecmp(t
, "tdgradient") == 0) {
149 /* get pixmap file path */
150 file
= WMGetFromPLArray(texture
, 1);
151 sprintf(buffer
, "%s/%s", prefix
, WMGetFromPLString(file
));
152 /* replace path with full path */
153 WMDeleteFromPLArray(texture
, 1);
154 WMInsertInPLArray(texture
, 1, WMCreatePLString(buffer
));
155 } else if (strcasecmp(t
, "bitmap") == 0) {
159 /* get bitmap file path */
160 file
= WMGetFromPLArray(texture
, 1);
161 sprintf(buffer
, "%s/%s", prefix
, WMGetFromPLString(file
));
162 /* replace path with full path */
163 WMDeleteFromPLArray(texture
, 1);
164 WMInsertInPLArray(texture
, 1, WMCreatePLString(buffer
));
166 /* get mask file path */
167 file
= WMGetFromPLArray(texture
, 2);
168 sprintf(buffer
, "%s/%s", prefix
, WMGetFromPLString(file
));
169 /* replace path with full path */
170 WMDeleteFromPLArray(texture
, 2);
171 WMInsertInPLArray(texture
, 2, WMCreatePLString(buffer
));
175 void hackPaths(WMPropList
* style
, char *prefix
)
182 keys
= WMGetPLDictionaryKeys(style
);
184 for (i
= 0; i
< WMGetPropListItemCount(keys
); i
++) {
185 key
= WMGetFromPLArray(keys
, i
);
187 value
= WMGetFromPLDictionary(style
, key
);
191 if (strcasecmp(WMGetFromPLString(key
), "WorkspaceSpecificBack") == 0) {
192 if (WMIsPLArray(value
)) {
196 for (j
= 0; j
< WMGetPropListItemCount(value
); j
++) {
197 texture
= WMGetFromPLArray(value
, j
);
199 if (texture
&& WMIsPLArray(texture
)
200 && WMGetPropListItemCount(texture
) > 2) {
202 hackPathInTexture(texture
, prefix
);
208 if (WMIsPLArray(value
) && WMGetPropListItemCount(value
) > 2) {
210 hackPathInTexture(value
, prefix
);
217 static WMPropList
*getColor(WMPropList
* texture
)
219 WMPropList
*value
, *type
;
222 type
= WMGetFromPLArray(texture
, 0);
228 str
= WMGetFromPLString(type
);
229 if (strcasecmp(str
, "solid") == 0) {
230 value
= WMGetFromPLArray(texture
, 1);
231 } else if (strcasecmp(str
, "dgradient") == 0
232 || strcasecmp(str
, "hgradient") == 0 || strcasecmp(str
, "vgradient") == 0) {
234 int r1
, g1
, b1
, r2
, g2
, b2
;
237 c1
= WMGetFromPLArray(texture
, 1);
238 c2
= WMGetFromPLArray(texture
, 2);
240 if (sscanf(WMGetFromPLString(c1
), "#%2x%2x%2x", &r1
, &g1
, &b1
) == 3
241 && sscanf(WMGetFromPLString(c2
), "#%2x%2x%2x", &r2
, &g2
, &b2
) == 3) {
242 sprintf(buffer
, "#%02x%02x%02x", (r1
+ r2
) / 2, (g1
+ g2
) / 2, (b1
+ b2
) / 2);
243 value
= WMCreatePLString(buffer
);
251 XParseColor(dpy
, DefaultColormap(dpy
, DefaultScreen(dpy
)), WMGetFromPLString(c1
), &color1
);
252 XParseColor(dpy
, DefaultColormap(dpy
, DefaultScreen(dpy
)), WMGetFromPLString(c2
), &color2
);
254 sprintf(buffer
, "#%02x%02x%02x",
255 (color1
.red
+ color2
.red
) >> 9,
256 (color1
.green
+ color2
.green
) >> 9, (color1
.blue
+ color2
.blue
) >> 9);
257 value
= WMCreatePLString(buffer
);
259 } else if (strcasecmp(str
, "mdgradient") == 0
260 || strcasecmp(str
, "mhgradient") == 0 || strcasecmp(str
, "mvgradient") == 0) {
262 value
= WMGetFromPLArray(texture
, 1);
264 } else if (strcasecmp(str
, "tpixmap") == 0
265 || strcasecmp(str
, "cpixmap") == 0 || strcasecmp(str
, "spixmap") == 0) {
267 value
= WMGetFromPLArray(texture
, 2);
274 * since some of the options introduce incompatibilities, we will need
275 * to do a kluge here or the themes ppl will get real annoying.
276 * So, treat for the absence of the following options:
280 void hackStyle(WMPropList
* style
)
282 WMPropList
*keys
, *tmp
;
283 int foundIconTitle
= 0, foundResizebarBack
= 0;
286 keys
= WMGetPLDictionaryKeys(style
);
288 for (i
= 0; i
< WMGetPropListItemCount(keys
); i
++) {
291 tmp
= WMGetFromPLArray(keys
, i
);
292 str
= WMGetFromPLString(tmp
);
294 if (ignoreFonts
&& isFontOption(str
)) {
295 WMRemoveFromPLDictionary(style
, tmp
);
298 if (ignoreCursors
&& isCursorOption(str
)) {
299 WMRemoveFromPLDictionary(style
, tmp
);
302 if (isFontOption(str
)) {
304 char *newfont
, *oldfont
;
306 value
= WMGetFromPLDictionary(style
, tmp
);
308 oldfont
= WMGetFromPLString(value
);
309 newfont
= convertFont(oldfont
, False
);
310 if (newfont
!= oldfont
) {
311 value
= WMCreatePLString(newfont
);
312 WMPutInPLDictionary(style
, tmp
, value
);
313 WMReleasePropList(value
);
318 if (strcasecmp(str
, "IconTitleColor") == 0 || strcasecmp(str
, "IconTitleBack") == 0) {
320 } else if (strcasecmp(str
, "ResizebarBack") == 0) {
321 foundResizebarBack
= 1;
326 if (!foundIconTitle
) {
327 /* set the default values */
328 tmp
= WMGetFromPLDictionary(style
, WMCreatePLString("FTitleColor"));
330 WMPutInPLDictionary(style
, WMCreatePLString("IconTitleColor"), tmp
);
333 tmp
= WMGetFromPLDictionary(style
, WMCreatePLString("FTitleBack"));
337 value
= getColor(tmp
);
340 WMPutInPLDictionary(style
, WMCreatePLString("IconTitleBack"), value
);
345 if (!foundResizebarBack
) {
346 /* set the default values */
347 tmp
= WMGetFromPLDictionary(style
, WMCreatePLString("UTitleBack"));
351 value
= getColor(tmp
);
356 t
= WMCreatePLArray(WMCreatePLString("solid"), value
, NULL
);
357 WMPutInPLDictionary(style
, WMCreatePLString("ResizebarBack"), t
);
362 if (!WMGetFromPLDictionary(style
, WMCreatePLString("MenuStyle"))) {
363 WMPutInPLDictionary(style
, WMCreatePLString("MenuStyle"), WMCreatePLString("normal"));
369 printf("Usage: %s [OPTIONS] FILE\n", ProgName
);
370 puts("Reads style/theme configuration from FILE and updates Window Maker.");
372 puts(" --no-fonts ignore font related options");
373 puts(" --no-cursors ignore cursor related options");
374 puts(" --ignore <option> ignore changes in the specified option");
375 puts(" --help display this help and exit");
377 puts(" --format <format> specifies the format of the theme to be converted");
379 puts(" --version output version information and exit");
381 puts("Supported formats: blackbox"); */
386 int main(int argc
, char **argv
)
388 WMPropList
*prop
, *style
;
394 char *ignoreList
[MAX_OPTIONS
];
396 dpy
= XOpenDisplay("");
401 printf("%s: missing argument\n", ProgName
);
402 printf("Try '%s --help' for more information\n", ProgName
);
406 for (i
= 1; i
< argc
; i
++) {
407 if (strcmp("--ignore", argv
[i
]) == 0) {
410 printf("%s: missing argument for option --ignore\n", ProgName
);
413 ignoreList
[ignoreCount
++] = argv
[i
];
415 } else if (strcmp("--no-fonts", argv
[i
]) == 0) {
417 } else if (strcmp("--no-cursors", argv
[i
]) == 0) {
419 } else if (strcmp("--version", argv
[i
]) == 0) {
422 } else if (strcmp("--help", argv
[i
]) == 0) {
426 } else if (strcmp("--format", argv
[i
]) == 0) {
429 printf("%s: missing argument for option --format\n", ProgName
);
432 if (strcasecmp(argv
[i
], "blackbox") == 0) {
435 printf("%s: unknown theme format '%s'\n", ProgName
, argv
[i
]);
441 printf("%s: invalid argument '%s'\n", ProgName
, argv
[i
]);
442 printf("Try '%s --help' for more information\n", ProgName
);
449 WMPLSetCaseSensitive(False
);
451 path
= defaultsPathForDomain("WindowMaker");
453 prop
= WMReadPropListFromFile(path
);
456 printf("%s:could not load WindowMaker configuration file.\n", ProgName
);
460 if (stat(file
, &statbuf
) < 0) {
465 if (format
== F_BLACKBOX
) {
466 style
= readBlackBoxStyle(file
);
468 printf("%s: could not open style file\n", ProgName
);
474 if (S_ISDIR(statbuf
.st_mode
)) {
479 if (*argv
[argc
- 1] != '/') {
480 if (!getcwd(buffer
, 4000)) {
481 printf("%s: complete path for %s is too long\n", ProgName
, file
);
484 if (strlen(buffer
) + strlen(file
) > 4000) {
485 printf("%s: complete path for %s is too long\n", ProgName
, file
);
492 strcat(buffer
, file
);
494 prefix
= malloc(strlen(buffer
) + 10);
496 printf("%s: out of memory\n", ProgName
);
499 strcpy(prefix
, buffer
);
501 strcat(buffer
, "/style");
503 style
= WMReadPropListFromFile(buffer
);
506 printf("%s:could not load style file.\n", ProgName
);
510 hackPaths(style
, prefix
);
513 /* normal style file */
515 style
= WMReadPropListFromFile(file
);
518 printf("%s:could not load style file.\n", ProgName
);
524 if (!WMIsPLDictionary(style
)) {
525 printf("%s: '%s' is not a style file/theme\n", ProgName
, file
);
531 if (ignoreCount
> 0) {
532 for (i
= 0; i
< ignoreCount
; i
++) {
533 WMRemoveFromPLDictionary(style
, WMCreatePLString(ignoreList
[i
]));
537 WMMergePLDictionaries(prop
, style
, True
);
539 WMWritePropListToFile(prop
, path
, True
);
545 char *msg
= "Reconfigure";
547 memset(&ev
, 0, sizeof(XEvent
));
549 ev
.xclient
.type
= ClientMessage
;
550 ev
.xclient
.message_type
= XInternAtom(dpy
, "_WINDOWMAKER_COMMAND", False
);
551 ev
.xclient
.window
= DefaultRootWindow(dpy
);
552 ev
.xclient
.format
= 8;
554 for (i
= 0; i
<= strlen(msg
); i
++) {
555 ev
.xclient
.data
.b
[i
] = msg
[i
];
557 XSendEvent(dpy
, DefaultRootWindow(dpy
), False
, SubstructureRedirectMask
, &ev
);
566 char *getToken(char *str
, int i
, char *buf
)
571 static WMPropList
*readBlackBoxStyle(char *path
)
574 char buffer
[128], char token
[128];
575 WMPropList
*style
, *p
;
577 f
= fopen(path
, "rb");
584 if (!fgets(buffer
, 127, f
))
587 if (strncasecmp(buffer
, "menu.title:", 11) == 0) {