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,
24 #define PROG_VERSION "setstyle (Window Maker) 0.6"
32 #include <WINGs/WUtil.h>
36 #include "../src/wconfig.h"
38 #define MAX_OPTIONS 128
40 char *default_font
= "sans:pixelsize=12";
42 char *FontOptions
[] = {
53 char *CursorOptions
[] = {
58 "TopLeftResizeCursor",
59 "TopRightResizeCursor",
60 "BottomLeftResizeCursor",
61 "BottomRightResizeCursor",
62 "VerticalResizeCursor",
63 "HorizontalResizeCursor",
75 int ignoreCursors
= 0;
81 WMPropList
*readBlackBoxStyle(char *path
);
84 capitalize(char *element
)
86 unsigned int first
= 1;
90 for (p
= b
; *p
!= 0; p
++) {
91 if (isalpha(*p
) && first
) {
94 } else if (*p
== '-' || *p
== ' ') {
102 getElementFromXLFD(const char *xlfd
, int index
)
104 const char *p
= xlfd
;
106 if (*p
== '-' && --index
== 0) {
107 const char *end
= strchr(p
+ 1, '-');
110 if (end
== 0) end
= p
+ strlen(p
);
113 memcpy(buf
, p
+ 1, len
);
127 char *family
= getElementFromXLFD(xlfd
, 2);
128 char *size
= getElementFromXLFD(xlfd
, 7);
129 char *weight
= getElementFromXLFD(xlfd
, 3);
130 char *slant
= getElementFromXLFD(xlfd
, 4);
132 if (strcmp(family
, "*") != 0) {
133 Fcname
= wstrconcat(Fcname
, capitalize(family
));
135 if (strcmp(size
, "*") != 0) {
136 Fcname
= wstrconcat(Fcname
, ":pixelsize=");
137 Fcname
= wstrconcat(Fcname
, size
);
139 if (strcmp(weight
, "*") != 0) {
140 Fcname
= wstrconcat(Fcname
, ":style=");
141 Fcname
= wstrconcat(Fcname
, capitalize(weight
));
143 if (strcmp(slant
, "*") != 0) {
144 if (strcmp(slant
, "i") == 0) {
145 Fcname
= wstrconcat(Fcname
, ":slant=");
146 Fcname
= wstrconcat(Fcname
, "Italic");
147 } else if (strcmp(slant
, "o") == 0) {
148 Fcname
= wstrconcat(Fcname
, ":slant=");
149 Fcname
= wstrconcat(Fcname
, "Oblique");
150 } else if (strcmp(slant
, "ri") == 0) {
151 Fcname
= wstrconcat(Fcname
, ":slant=");
152 Fcname
= wstrconcat(Fcname
, "Rev Italic");
153 } else if (strcmp(slant
, "ro") == 0) {
154 Fcname
= wstrconcat(Fcname
, ":slant=");
155 Fcname
= wstrconcat(Fcname
, "Rev Oblique");
159 Fcname
= wstrdup(default_font
);
170 defaultsPathForDomain(char *domain
)
172 static char path
[1024];
175 gspath
= getenv("GNUSTEP_USER_ROOT");
177 strcpy(path
, gspath
);
182 home
= getenv("HOME");
184 printf("%s:could not get HOME environment variable!\n", ProgName
);
189 strcat(path
, "/GNUstep/");
191 strcat(path
, DEFAULTS_DIR
);
193 strcat(path
, domain
);
201 hackPathInTexture(WMPropList
*texture
, char *prefix
)
206 /* get texture type */
207 type
= WMGetFromPLArray(texture
, 0);
208 t
= WMGetFromPLString(type
);
211 if (strcasecmp(t
, "tpixmap")==0
212 || strcasecmp(t
, "spixmap")==0
213 || strcasecmp(t
, "mpixmap")==0
214 || strcasecmp(t
, "cpixmap")==0
215 || strcasecmp(t
, "tvgradient")==0
216 || strcasecmp(t
, "thgradient")==0
217 || strcasecmp(t
, "tdgradient")==0) {
221 /* get pixmap file path */
222 file
= WMGetFromPLArray(texture
, 1);
223 sprintf(buffer
, "%s/%s", prefix
, WMGetFromPLString(file
));
224 /* replace path with full path */
225 WMDeleteFromPLArray(texture
, 1);
226 WMInsertInPLArray(texture
, 1, WMCreatePLString(buffer
));
227 } else if (strcasecmp(t
, "bitmap") == 0) {
231 /* get bitmap file path */
232 file
= WMGetFromPLArray(texture
, 1);
233 sprintf(buffer
, "%s/%s", prefix
, WMGetFromPLString(file
));
234 /* replace path with full path */
235 WMDeleteFromPLArray(texture
, 1);
236 WMInsertInPLArray(texture
, 1, WMCreatePLString(buffer
));
238 /* get mask file path */
239 file
= WMGetFromPLArray(texture
, 2);
240 sprintf(buffer
, "%s/%s", prefix
, WMGetFromPLString(file
));
241 /* replace path with full path */
242 WMDeleteFromPLArray(texture
, 2);
243 WMInsertInPLArray(texture
, 2, WMCreatePLString(buffer
));
249 hackPaths(WMPropList
*style
, char *prefix
)
257 keys
= WMGetPLDictionaryKeys(style
);
259 for (i
= 0; i
< WMGetPropListItemCount(keys
); i
++) {
260 key
= WMGetFromPLArray(keys
, i
);
262 value
= WMGetFromPLDictionary(style
, key
);
266 if (strcasecmp(WMGetFromPLString(key
), "WorkspaceSpecificBack")==0) {
267 if (WMIsPLArray(value
)) {
271 for (j
= 0; j
< WMGetPropListItemCount(value
); j
++) {
272 texture
= WMGetFromPLArray(value
, j
);
274 if (texture
&& WMIsPLArray(texture
)
275 && WMGetPropListItemCount(texture
) > 2) {
277 hackPathInTexture(texture
, prefix
);
283 if (WMIsPLArray(value
) && WMGetPropListItemCount(value
) > 2) {
285 hackPathInTexture(value
, prefix
);
294 getColor(WMPropList
*texture
)
296 WMPropList
*value
, *type
;
299 type
= WMGetFromPLArray(texture
, 0);
305 str
= WMGetFromPLString(type
);
306 if (strcasecmp(str
, "solid")==0) {
307 value
= WMGetFromPLArray(texture
, 1);
308 } else if (strcasecmp(str
, "dgradient")==0
309 || strcasecmp(str
, "hgradient")==0
310 || strcasecmp(str
, "vgradient")==0) {
312 int r1
, g1
, b1
, r2
, g2
, b2
;
315 c1
= WMGetFromPLArray(texture
, 1);
316 c2
= WMGetFromPLArray(texture
, 2);
318 if (sscanf(WMGetFromPLString(c1
), "#%2x%2x%2x", &r1
, &g1
, &b1
)==3
319 && sscanf(WMGetFromPLString(c2
), "#%2x%2x%2x", &r2
, &g2
, &b2
)==3) {
320 sprintf(buffer
, "#%02x%02x%02x", (r1
+r2
)/2, (g1
+g2
)/2,
322 value
= WMCreatePLString(buffer
);
330 XParseColor(dpy
, DefaultColormap(dpy
, DefaultScreen(dpy
)),
331 WMGetFromPLString(c1
), &color1
);
332 XParseColor(dpy
, DefaultColormap(dpy
, DefaultScreen(dpy
)),
333 WMGetFromPLString(c2
), &color2
);
335 sprintf(buffer
, "#%02x%02x%02x",
336 (color1
.red
+color2
.red
)>>9,
337 (color1
.green
+color2
.green
)>>9,
338 (color1
.blue
+color2
.blue
)>>9);
339 value
= WMCreatePLString(buffer
);
341 } else if (strcasecmp(str
, "mdgradient")==0
342 || strcasecmp(str
, "mhgradient")==0
343 || strcasecmp(str
, "mvgradient")==0) {
345 value
= WMGetFromPLArray(texture
, 1);
347 } else if (strcasecmp(str
, "tpixmap")==0
348 || strcasecmp(str
, "cpixmap")==0
349 || strcasecmp(str
, "spixmap")==0) {
351 value
= WMGetFromPLArray(texture
, 2);
359 * since some of the options introduce incompatibilities, we will need
360 * to do a kluge here or the themes ppl will get real annoying.
361 * So, treat for the absence of the following options:
366 hackStyle(WMPropList
*style
)
368 WMPropList
*keys
, *tmp
;
369 int foundIconTitle
= 0, foundResizebarBack
= 0;
372 keys
= WMGetPLDictionaryKeys(style
);
374 for (i
= 0; i
< WMGetPropListItemCount(keys
); i
++) {
377 tmp
= WMGetFromPLArray(keys
, i
);
378 str
= WMGetFromPLString(tmp
);
383 for (j
= 0, found
= 0; FontOptions
[j
]!=NULL
; j
++) {
384 if (strcasecmp(str
, FontOptions
[j
])==0) {
385 WMRemoveFromPLDictionary(style
, tmp
);
394 for (j
= 0, found
= 0; CursorOptions
[j
] != NULL
; j
++) {
395 if (strcasecmp(str
, CursorOptions
[j
]) == 0) {
396 WMRemoveFromPLDictionary(style
, tmp
);
404 for (j
= 0; FontOptions
[j
]!=NULL
; j
++) {
405 if (strcasecmp(str
, FontOptions
[j
]) == 0) {
408 value
= WMGetFromPLDictionary(style
, tmp
);
409 oldfont
= WMGetFromPLString(value
);
410 if (oldfont
[0] == '-') {
411 if (!strchr(oldfont
, ',')) {
413 newfont
= xlfdToFc(oldfont
);
414 WMPutInPLDictionary(style
, tmp
, WMCreatePLString(newfont
));
417 wwarning("fontsets are not supported. replaced with default: %s", default_font
);
418 WMPutInPLDictionary(style
, tmp
,
419 WMCreatePLString(default_font
));
428 if (strcasecmp(str
, "IconTitleColor")==0
429 || strcasecmp(str
, "IconTitleBack")==0) {
431 } else if (strcasecmp(str
, "ResizebarBack")==0) {
432 foundResizebarBack
= 1;
437 if (!foundIconTitle
) {
438 /* set the default values */
439 tmp
= WMGetFromPLDictionary(style
, WMCreatePLString("FTitleColor"));
441 WMPutInPLDictionary(style
, WMCreatePLString("IconTitleColor"),
445 tmp
= WMGetFromPLDictionary(style
, WMCreatePLString("FTitleBack"));
449 value
= getColor(tmp
);
452 WMPutInPLDictionary(style
, WMCreatePLString("IconTitleBack"),
458 if (!foundResizebarBack
) {
459 /* set the default values */
460 tmp
= WMGetFromPLDictionary(style
, WMCreatePLString("UTitleBack"));
464 value
= getColor(tmp
);
469 t
= WMCreatePLArray(WMCreatePLString("solid"), value
,
471 WMPutInPLDictionary(style
, WMCreatePLString("ResizebarBack"),
478 if (!WMGetFromPLDictionary(style
, WMCreatePLString("MenuStyle"))) {
479 WMPutInPLDictionary(style
, WMCreatePLString("MenuStyle"),
480 WMCreatePLString("normal"));
488 printf("Usage: %s [OPTIONS] FILE\n", ProgName
);
489 puts("Reads style/theme configuration from FILE and updates Window Maker.");
491 puts(" --no-fonts ignore font related options");
492 puts(" --no-cursors ignore cursor related options");
493 puts(" --ignore <option> ignore changes in the specified option");
494 puts(" --help display this help and exit");
496 puts(" --format <format> specifies the format of the theme to be converted");
498 puts(" --version output version information and exit");
500 puts("Supported formats: blackbox");*/
507 main(int argc
, char **argv
)
509 WMPropList
*prop
, *style
;
515 char *ignoreList
[MAX_OPTIONS
];
517 dpy
= XOpenDisplay("");
522 printf("%s: missing argument\n", ProgName
);
523 printf("Try '%s --help' for more information\n", ProgName
);
527 for (i
= 1; i
< argc
; i
++) {
528 if (strcmp("--ignore", argv
[i
])==0) {
531 printf("%s: missing argument for option --ignore\n", ProgName
);
534 ignoreList
[ignoreCount
++] = argv
[i
];
536 } else if (strcmp("--no-fonts", argv
[i
])==0) {
538 } else if (strcmp("--no-cursors", argv
[i
])==0) {
540 } else if (strcmp("--version", argv
[i
])==0) {
543 } else if (strcmp("--help", argv
[i
])==0) {
547 } else if (strcmp("--format", argv
[i
])==0) {
550 printf("%s: missing argument for option --format\n", ProgName
);
553 if (strcasecmp(argv
[i
], "blackbox")==0) {
556 printf("%s: unknown theme format '%s'\n", ProgName
, argv
[i
]);
562 printf("%s: invalid argument '%s'\n", ProgName
, argv
[i
]);
563 printf("Try '%s --help' for more information\n", ProgName
);
570 WMPLSetCaseSensitive(False
);
572 path
= defaultsPathForDomain("WindowMaker");
574 prop
= WMReadPropListFromFile(path
);
577 printf("%s:could not load WindowMaker configuration file.\n",
582 if (stat(file
, &statbuf
) < 0) {
587 if (format
== F_BLACKBOX
) {
588 style
= readBlackBoxStyle(file
);
590 printf("%s: could not open style file\n", ProgName
);
596 if (S_ISDIR(statbuf
.st_mode
)) {
601 if (*argv
[argc
-1] != '/') {
602 if (!getcwd(buffer
, 4000)) {
603 printf("%s: complete path for %s is too long\n", ProgName
,
607 if (strlen(buffer
) + strlen(file
) > 4000) {
608 printf("%s: complete path for %s is too long\n", ProgName
,
616 strcat(buffer
, file
);
618 prefix
= malloc(strlen(buffer
)+10);
620 printf("%s: out of memory\n", ProgName
);
623 strcpy(prefix
, buffer
);
625 strcat(buffer
, "/style");
627 style
= WMReadPropListFromFile(buffer
);
630 printf("%s:could not load style file.\n", ProgName
);
634 hackPaths(style
, prefix
);
637 /* normal style file */
639 style
= WMReadPropListFromFile(file
);
642 printf("%s:could not load style file.\n", ProgName
);
648 if (!WMIsPLDictionary(style
)) {
649 printf("%s: '%s' is not a style file/theme\n", ProgName
, file
);
655 if (ignoreCount
> 0) {
656 for (i
= 0; i
< ignoreCount
; i
++) {
657 WMRemoveFromPLDictionary(style
, WMCreatePLString(ignoreList
[i
]));
661 WMMergePLDictionaries(prop
, style
, True
);
663 WMWritePropListToFile(prop
, path
, True
);
669 char *msg
= "Reconfigure";
671 memset(&ev
, 0, sizeof(XEvent
));
673 ev
.xclient
.type
= ClientMessage
;
674 ev
.xclient
.message_type
= XInternAtom(dpy
, "_WINDOWMAKER_COMMAND",
676 ev
.xclient
.window
= DefaultRootWindow(dpy
);
677 ev
.xclient
.format
= 8;
679 for (i
= 0; i
<= strlen(msg
); i
++) {
680 ev
.xclient
.data
.b
[i
] = msg
[i
];
682 XSendEvent(dpy
, DefaultRootWindow(dpy
), False
,
683 SubstructureRedirectMask
, &ev
);
694 getToken(char *str
, int i
, char *buf
)
701 readBlackBoxStyle(char *path
)
704 char buffer
[128], char token
[128];
705 WMPropList
*style
, *p
;
707 f
= fopen(path
, "rb");
714 if (!fgets(buffer
, 127, f
))
717 if (strncasecmp(buffer
, "menu.title:", 11)==0) {