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 #define 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
);
85 isCursorOption(char *option
)
89 for (i
=0; CursorOptions
[i
]!=NULL
; i
++) {
90 if (strcasecmp(option
, CursorOptions
[i
])==0) {
100 isFontOption(char *option
)
104 for (i
=0; FontOptions
[i
]!=NULL
; i
++) {
105 if (strcasecmp(option
, FontOptions
[i
])==0) {
115 countChar(char *str
, char c
)
122 for (; *str
!=0; str
++) {
137 #define XLFD_TOKENS 14
140 getXLFDTokens(char *xlfd
)
142 static str tokens
[XLFD_TOKENS
];
146 if (!xlfd
|| countChar(xlfd
, '-')<XLFD_TOKENS
)
149 memset(tokens
, 0, sizeof(str
)*XLFD_TOKENS
);
153 for (ptr
=xlfd
, i
=0; i
<XLFD_TOKENS
&& len
>0; i
++) {
154 size
= strspn(ptr
, "-");
159 size
= strcspn(ptr
, "-");
163 tokens
[i
].len
= size
;
177 if (token
->len
==0 || token
->str
[0]=='*') {
180 for (res
=0, pos
=0; pos
<token
->len
; pos
++) {
181 c
= token
->str
[pos
] - '0';
192 mapSlantToName(str
*slant
)
194 if (slant
->len
==0 || slant
->str
[0]=='*')
197 switch(slant
->str
[0]) {
212 str
*tokens
, *family
, *weight
, *slant
;
213 char *name
, buf
[512];
216 tokens
= getXLFDTokens(xlfd
);
218 return wstrdup(DEFAULT_FONT
);
220 family
= &(tokens
[1]);
221 weight
= &(tokens
[2]);
222 slant
= &(tokens
[3]);
224 if (family
->len
==0 || family
->str
[0]=='*')
225 return wstrdup(DEFAULT_FONT
);
227 sprintf(buf
, "%.*s", family
->len
, family
->str
);
230 pixelsize
= strToInt(&tokens
[6]);
231 size
= strToInt(&tokens
[7]);
233 if (size
<=0 && pixelsize
<=0) {
234 name
= wstrappend(name
, ":pixelsize=12");
235 } else if (pixelsize
>0) {
236 /* if pixelsize is present size will be ignored so we skip it */
237 sprintf(buf
, ":pixelsize=%d", pixelsize
);
238 name
= wstrappend(name
, buf
);
240 sprintf(buf
, "-%d", size
/10);
241 name
= wstrappend(name
, buf
);
244 if (weight
->len
>0 && weight
->str
[0]!='*') {
245 sprintf(buf
, ":weight=%.*s", weight
->len
, weight
->str
);
246 name
= wstrappend(name
, buf
);
249 if (slant
->len
>0 && slant
->str
[0]!='*') {
250 sprintf(buf
, ":slant=%s", mapSlantToName(slant
));
251 name
= wstrappend(name
, buf
);
254 name
= wstrappend(name
, ":xlfd=");
255 name
= wstrappend(name
, xlfd
);
261 /* return converted font (if conversion is needed) else the original font */
263 convertFont(char *font
)
266 if (!strchr(font
, ',')) {
267 return xlfdToFc(font
);
269 wwarning("fontsets are not supported. replaced "
270 "with default %s", DEFAULT_FONT
);
271 return wstrdup(DEFAULT_FONT
);
280 defaultsPathForDomain(char *domain
)
282 static char path
[1024];
285 gspath
= getenv("GNUSTEP_USER_ROOT");
287 strcpy(path
, gspath
);
292 home
= getenv("HOME");
294 printf("%s:could not get HOME environment variable!\n", ProgName
);
299 strcat(path
, "/GNUstep/");
301 strcat(path
, DEFAULTS_DIR
);
303 strcat(path
, domain
);
311 hackPathInTexture(WMPropList
*texture
, char *prefix
)
316 /* get texture type */
317 type
= WMGetFromPLArray(texture
, 0);
318 t
= WMGetFromPLString(type
);
321 if (strcasecmp(t
, "tpixmap")==0
322 || strcasecmp(t
, "spixmap")==0
323 || strcasecmp(t
, "mpixmap")==0
324 || strcasecmp(t
, "cpixmap")==0
325 || strcasecmp(t
, "tvgradient")==0
326 || strcasecmp(t
, "thgradient")==0
327 || strcasecmp(t
, "tdgradient")==0) {
331 /* get pixmap file path */
332 file
= WMGetFromPLArray(texture
, 1);
333 sprintf(buffer
, "%s/%s", prefix
, WMGetFromPLString(file
));
334 /* replace path with full path */
335 WMDeleteFromPLArray(texture
, 1);
336 WMInsertInPLArray(texture
, 1, WMCreatePLString(buffer
));
337 } else if (strcasecmp(t
, "bitmap") == 0) {
341 /* get bitmap file path */
342 file
= WMGetFromPLArray(texture
, 1);
343 sprintf(buffer
, "%s/%s", prefix
, WMGetFromPLString(file
));
344 /* replace path with full path */
345 WMDeleteFromPLArray(texture
, 1);
346 WMInsertInPLArray(texture
, 1, WMCreatePLString(buffer
));
348 /* get mask file path */
349 file
= WMGetFromPLArray(texture
, 2);
350 sprintf(buffer
, "%s/%s", prefix
, WMGetFromPLString(file
));
351 /* replace path with full path */
352 WMDeleteFromPLArray(texture
, 2);
353 WMInsertInPLArray(texture
, 2, WMCreatePLString(buffer
));
359 hackPaths(WMPropList
*style
, char *prefix
)
367 keys
= WMGetPLDictionaryKeys(style
);
369 for (i
= 0; i
< WMGetPropListItemCount(keys
); i
++) {
370 key
= WMGetFromPLArray(keys
, i
);
372 value
= WMGetFromPLDictionary(style
, key
);
376 if (strcasecmp(WMGetFromPLString(key
), "WorkspaceSpecificBack")==0) {
377 if (WMIsPLArray(value
)) {
381 for (j
= 0; j
< WMGetPropListItemCount(value
); j
++) {
382 texture
= WMGetFromPLArray(value
, j
);
384 if (texture
&& WMIsPLArray(texture
)
385 && WMGetPropListItemCount(texture
) > 2) {
387 hackPathInTexture(texture
, prefix
);
393 if (WMIsPLArray(value
) && WMGetPropListItemCount(value
) > 2) {
395 hackPathInTexture(value
, prefix
);
404 getColor(WMPropList
*texture
)
406 WMPropList
*value
, *type
;
409 type
= WMGetFromPLArray(texture
, 0);
415 str
= WMGetFromPLString(type
);
416 if (strcasecmp(str
, "solid")==0) {
417 value
= WMGetFromPLArray(texture
, 1);
418 } else if (strcasecmp(str
, "dgradient")==0
419 || strcasecmp(str
, "hgradient")==0
420 || strcasecmp(str
, "vgradient")==0) {
422 int r1
, g1
, b1
, r2
, g2
, b2
;
425 c1
= WMGetFromPLArray(texture
, 1);
426 c2
= WMGetFromPLArray(texture
, 2);
428 if (sscanf(WMGetFromPLString(c1
), "#%2x%2x%2x", &r1
, &g1
, &b1
)==3
429 && sscanf(WMGetFromPLString(c2
), "#%2x%2x%2x", &r2
, &g2
, &b2
)==3) {
430 sprintf(buffer
, "#%02x%02x%02x", (r1
+r2
)/2, (g1
+g2
)/2,
432 value
= WMCreatePLString(buffer
);
440 XParseColor(dpy
, DefaultColormap(dpy
, DefaultScreen(dpy
)),
441 WMGetFromPLString(c1
), &color1
);
442 XParseColor(dpy
, DefaultColormap(dpy
, DefaultScreen(dpy
)),
443 WMGetFromPLString(c2
), &color2
);
445 sprintf(buffer
, "#%02x%02x%02x",
446 (color1
.red
+color2
.red
)>>9,
447 (color1
.green
+color2
.green
)>>9,
448 (color1
.blue
+color2
.blue
)>>9);
449 value
= WMCreatePLString(buffer
);
451 } else if (strcasecmp(str
, "mdgradient")==0
452 || strcasecmp(str
, "mhgradient")==0
453 || strcasecmp(str
, "mvgradient")==0) {
455 value
= WMGetFromPLArray(texture
, 1);
457 } else if (strcasecmp(str
, "tpixmap")==0
458 || strcasecmp(str
, "cpixmap")==0
459 || strcasecmp(str
, "spixmap")==0) {
461 value
= WMGetFromPLArray(texture
, 2);
469 * since some of the options introduce incompatibilities, we will need
470 * to do a kluge here or the themes ppl will get real annoying.
471 * So, treat for the absence of the following options:
476 hackStyle(WMPropList
*style
)
478 WMPropList
*keys
, *tmp
;
479 int foundIconTitle
= 0, foundResizebarBack
= 0;
482 keys
= WMGetPLDictionaryKeys(style
);
484 for (i
= 0; i
< WMGetPropListItemCount(keys
); i
++) {
487 tmp
= WMGetFromPLArray(keys
, i
);
488 str
= WMGetFromPLString(tmp
);
490 if (ignoreFonts
&& isFontOption(str
)) {
491 WMRemoveFromPLDictionary(style
, tmp
);
494 if (ignoreCursors
&& isCursorOption(str
)) {
495 WMRemoveFromPLDictionary(style
, tmp
);
498 if (isFontOption(str
)) {
500 char *newfont
, *oldfont
;
502 value
= WMGetFromPLDictionary(style
, tmp
);
504 oldfont
= WMGetFromPLString(value
);
505 newfont
= convertFont(oldfont
);
506 if (newfont
!= oldfont
) {
507 value
= WMCreatePLString(newfont
);
508 WMPutInPLDictionary(style
, tmp
, value
);
509 WMReleasePropList(value
);
514 if (strcasecmp(str
, "IconTitleColor")==0
515 || strcasecmp(str
, "IconTitleBack")==0) {
517 } else if (strcasecmp(str
, "ResizebarBack")==0) {
518 foundResizebarBack
= 1;
523 if (!foundIconTitle
) {
524 /* set the default values */
525 tmp
= WMGetFromPLDictionary(style
, WMCreatePLString("FTitleColor"));
527 WMPutInPLDictionary(style
, WMCreatePLString("IconTitleColor"),
531 tmp
= WMGetFromPLDictionary(style
, WMCreatePLString("FTitleBack"));
535 value
= getColor(tmp
);
538 WMPutInPLDictionary(style
, WMCreatePLString("IconTitleBack"),
544 if (!foundResizebarBack
) {
545 /* set the default values */
546 tmp
= WMGetFromPLDictionary(style
, WMCreatePLString("UTitleBack"));
550 value
= getColor(tmp
);
555 t
= WMCreatePLArray(WMCreatePLString("solid"), value
,
557 WMPutInPLDictionary(style
, WMCreatePLString("ResizebarBack"),
564 if (!WMGetFromPLDictionary(style
, WMCreatePLString("MenuStyle"))) {
565 WMPutInPLDictionary(style
, WMCreatePLString("MenuStyle"),
566 WMCreatePLString("normal"));
574 printf("Usage: %s [OPTIONS] FILE\n", ProgName
);
575 puts("Reads style/theme configuration from FILE and updates Window Maker.");
577 puts(" --no-fonts ignore font related options");
578 puts(" --no-cursors ignore cursor related options");
579 puts(" --ignore <option> ignore changes in the specified option");
580 puts(" --help display this help and exit");
582 puts(" --format <format> specifies the format of the theme to be converted");
584 puts(" --version output version information and exit");
586 puts("Supported formats: blackbox");*/
593 main(int argc
, char **argv
)
595 WMPropList
*prop
, *style
;
601 char *ignoreList
[MAX_OPTIONS
];
603 dpy
= XOpenDisplay("");
608 printf("%s: missing argument\n", ProgName
);
609 printf("Try '%s --help' for more information\n", ProgName
);
613 for (i
= 1; i
< argc
; i
++) {
614 if (strcmp("--ignore", argv
[i
])==0) {
617 printf("%s: missing argument for option --ignore\n", ProgName
);
620 ignoreList
[ignoreCount
++] = argv
[i
];
622 } else if (strcmp("--no-fonts", argv
[i
])==0) {
624 } else if (strcmp("--no-cursors", argv
[i
])==0) {
626 } else if (strcmp("--version", argv
[i
])==0) {
629 } else if (strcmp("--help", argv
[i
])==0) {
633 } else if (strcmp("--format", argv
[i
])==0) {
636 printf("%s: missing argument for option --format\n", ProgName
);
639 if (strcasecmp(argv
[i
], "blackbox")==0) {
642 printf("%s: unknown theme format '%s'\n", ProgName
, argv
[i
]);
648 printf("%s: invalid argument '%s'\n", ProgName
, argv
[i
]);
649 printf("Try '%s --help' for more information\n", ProgName
);
656 WMPLSetCaseSensitive(False
);
658 path
= defaultsPathForDomain("WindowMaker");
660 prop
= WMReadPropListFromFile(path
);
663 printf("%s:could not load WindowMaker configuration file.\n",
668 if (stat(file
, &statbuf
) < 0) {
673 if (format
== F_BLACKBOX
) {
674 style
= readBlackBoxStyle(file
);
676 printf("%s: could not open style file\n", ProgName
);
682 if (S_ISDIR(statbuf
.st_mode
)) {
687 if (*argv
[argc
-1] != '/') {
688 if (!getcwd(buffer
, 4000)) {
689 printf("%s: complete path for %s is too long\n", ProgName
,
693 if (strlen(buffer
) + strlen(file
) > 4000) {
694 printf("%s: complete path for %s is too long\n", ProgName
,
702 strcat(buffer
, file
);
704 prefix
= malloc(strlen(buffer
)+10);
706 printf("%s: out of memory\n", ProgName
);
709 strcpy(prefix
, buffer
);
711 strcat(buffer
, "/style");
713 style
= WMReadPropListFromFile(buffer
);
716 printf("%s:could not load style file.\n", ProgName
);
720 hackPaths(style
, prefix
);
723 /* normal style file */
725 style
= WMReadPropListFromFile(file
);
728 printf("%s:could not load style file.\n", ProgName
);
734 if (!WMIsPLDictionary(style
)) {
735 printf("%s: '%s' is not a style file/theme\n", ProgName
, file
);
741 if (ignoreCount
> 0) {
742 for (i
= 0; i
< ignoreCount
; i
++) {
743 WMRemoveFromPLDictionary(style
, WMCreatePLString(ignoreList
[i
]));
747 WMMergePLDictionaries(prop
, style
, True
);
749 WMWritePropListToFile(prop
, path
, True
);
755 char *msg
= "Reconfigure";
757 memset(&ev
, 0, sizeof(XEvent
));
759 ev
.xclient
.type
= ClientMessage
;
760 ev
.xclient
.message_type
= XInternAtom(dpy
, "_WINDOWMAKER_COMMAND",
762 ev
.xclient
.window
= DefaultRootWindow(dpy
);
763 ev
.xclient
.format
= 8;
765 for (i
= 0; i
<= strlen(msg
); i
++) {
766 ev
.xclient
.data
.b
[i
] = msg
[i
];
768 XSendEvent(dpy
, DefaultRootWindow(dpy
), False
,
769 SubstructureRedirectMask
, &ev
);
780 getToken(char *str
, int i
, char *buf
)
787 readBlackBoxStyle(char *path
)
790 char buffer
[128], char token
[128];
791 WMPropList
*style
, *p
;
793 f
= fopen(path
, "rb");
800 if (!fgets(buffer
, 127, f
))
803 if (strncasecmp(buffer
, "menu.title:", 11)==0) {