- Added support for NET_WM_NAME/NET_WM_ICON_NAME
[wmaker-crm.git] / util / setstyle.c
blobf4d6563ffeb2ad2917ec7f9482335ef494b8db66
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,
20 * USA.
24 #define PROG_VERSION "setstyle (Window Maker) 0.6"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <X11/Xlib.h>
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[] = {
43 "IconTitleFont",
44 "ClipTitleFont",
45 "DisplayFont",
46 "LargeDisplayFont",
47 "MenuTextFont",
48 "MenuTitleFont",
49 "WindowTitleFont",
50 NULL
53 char *CursorOptions[] = {
54 "NormalCursor",
55 "ArrowCursor",
56 "MoveCursor",
57 "ResizeCursor",
58 "TopLeftResizeCursor",
59 "TopRightResizeCursor",
60 "BottomLeftResizeCursor",
61 "BottomRightResizeCursor",
62 "VerticalResizeCursor",
63 "HorizontalResizeCursor",
64 "WaitCursor",
65 "QuestionCursor",
66 "TextCursor",
67 "SelectCursor",
68 NULL
73 char *ProgName;
74 int ignoreFonts = 0;
75 int ignoreCursors = 0;
77 Display *dpy;
79 extern char* convertFont(char *font, Bool keepXLFD);
81 WMPropList *readBlackBoxStyle(char *path);
84 static Bool
85 isCursorOption(char *option)
87 int i;
89 for (i=0; CursorOptions[i]!=NULL; i++) {
90 if (strcasecmp(option, CursorOptions[i])==0) {
91 return True;
95 return False;
99 static Bool
100 isFontOption(char *option)
102 int i;
104 for (i=0; FontOptions[i]!=NULL; i++) {
105 if (strcasecmp(option, FontOptions[i])==0) {
106 return True;
110 return False;
114 char*
115 defaultsPathForDomain(char *domain)
117 static char path[1024];
118 char *gspath;
120 gspath = getenv("GNUSTEP_USER_ROOT");
121 if (gspath) {
122 strcpy(path, gspath);
123 strcat(path, "/");
124 } else {
125 char *home;
127 home = getenv("HOME");
128 if (!home) {
129 printf("%s:could not get HOME environment variable!\n", ProgName);
130 exit(0);
133 strcpy(path, home);
134 strcat(path, "/GNUstep/");
136 strcat(path, DEFAULTS_DIR);
137 strcat(path, "/");
138 strcat(path, domain);
140 return path;
145 void
146 hackPathInTexture(WMPropList *texture, char *prefix)
148 WMPropList *type;
149 char *t;
151 /* get texture type */
152 type = WMGetFromPLArray(texture, 0);
153 t = WMGetFromPLString(type);
154 if (t == NULL)
155 return;
156 if (strcasecmp(t, "tpixmap")==0
157 || strcasecmp(t, "spixmap")==0
158 || strcasecmp(t, "mpixmap")==0
159 || strcasecmp(t, "cpixmap")==0
160 || strcasecmp(t, "tvgradient")==0
161 || strcasecmp(t, "thgradient")==0
162 || strcasecmp(t, "tdgradient")==0) {
163 WMPropList *file;
164 char buffer[4018];
166 /* get pixmap file path */
167 file = WMGetFromPLArray(texture, 1);
168 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
169 /* replace path with full path */
170 WMDeleteFromPLArray(texture, 1);
171 WMInsertInPLArray(texture, 1, WMCreatePLString(buffer));
172 } else if (strcasecmp(t, "bitmap") == 0) {
173 WMPropList *file;
174 char buffer[4018];
176 /* get bitmap file path */
177 file = WMGetFromPLArray(texture, 1);
178 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
179 /* replace path with full path */
180 WMDeleteFromPLArray(texture, 1);
181 WMInsertInPLArray(texture, 1, WMCreatePLString(buffer));
183 /* get mask file path */
184 file = WMGetFromPLArray(texture, 2);
185 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
186 /* replace path with full path */
187 WMDeleteFromPLArray(texture, 2);
188 WMInsertInPLArray(texture, 2, WMCreatePLString(buffer));
193 void
194 hackPaths(WMPropList *style, char *prefix)
196 WMPropList *keys;
197 WMPropList *key;
198 WMPropList *value;
199 int i;
202 keys = WMGetPLDictionaryKeys(style);
204 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
205 key = WMGetFromPLArray(keys, i);
207 value = WMGetFromPLDictionary(style, key);
208 if (!value)
209 continue;
211 if (strcasecmp(WMGetFromPLString(key), "WorkspaceSpecificBack")==0) {
212 if (WMIsPLArray(value)) {
213 int j;
214 WMPropList *texture;
216 for (j = 0; j < WMGetPropListItemCount(value); j++) {
217 texture = WMGetFromPLArray(value, j);
219 if (texture && WMIsPLArray(texture)
220 && WMGetPropListItemCount(texture) > 2) {
222 hackPathInTexture(texture, prefix);
226 } else {
228 if (WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
230 hackPathInTexture(value, prefix);
238 static WMPropList*
239 getColor(WMPropList *texture)
241 WMPropList *value, *type;
242 char *str;
244 type = WMGetFromPLArray(texture, 0);
245 if (!type)
246 return NULL;
248 value = NULL;
250 str = WMGetFromPLString(type);
251 if (strcasecmp(str, "solid")==0) {
252 value = WMGetFromPLArray(texture, 1);
253 } else if (strcasecmp(str, "dgradient")==0
254 || strcasecmp(str, "hgradient")==0
255 || strcasecmp(str, "vgradient")==0) {
256 WMPropList *c1, *c2;
257 int r1, g1, b1, r2, g2, b2;
258 char buffer[32];
260 c1 = WMGetFromPLArray(texture, 1);
261 c2 = WMGetFromPLArray(texture, 2);
262 if (!dpy) {
263 if (sscanf(WMGetFromPLString(c1), "#%2x%2x%2x", &r1, &g1, &b1)==3
264 && sscanf(WMGetFromPLString(c2), "#%2x%2x%2x", &r2, &g2, &b2)==3) {
265 sprintf(buffer, "#%02x%02x%02x", (r1+r2)/2, (g1+g2)/2,
266 (b1+b2)/2);
267 value = WMCreatePLString(buffer);
268 } else {
269 value = c1;
271 } else {
272 XColor color1;
273 XColor color2;
275 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
276 WMGetFromPLString(c1), &color1);
277 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
278 WMGetFromPLString(c2), &color2);
280 sprintf(buffer, "#%02x%02x%02x",
281 (color1.red+color2.red)>>9,
282 (color1.green+color2.green)>>9,
283 (color1.blue+color2.blue)>>9);
284 value = WMCreatePLString(buffer);
286 } else if (strcasecmp(str, "mdgradient")==0
287 || strcasecmp(str, "mhgradient")==0
288 || strcasecmp(str, "mvgradient")==0) {
290 value = WMGetFromPLArray(texture, 1);
292 } else if (strcasecmp(str, "tpixmap")==0
293 || strcasecmp(str, "cpixmap")==0
294 || strcasecmp(str, "spixmap")==0) {
296 value = WMGetFromPLArray(texture, 2);
299 return value;
304 * since some of the options introduce incompatibilities, we will need
305 * to do a kluge here or the themes ppl will get real annoying.
306 * So, treat for the absence of the following options:
307 * IconTitleColor
308 * IconTitleBack
310 void
311 hackStyle(WMPropList *style)
313 WMPropList *keys, *tmp;
314 int foundIconTitle = 0, foundResizebarBack = 0;
315 int i;
317 keys = WMGetPLDictionaryKeys(style);
319 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
320 char *str;
322 tmp = WMGetFromPLArray(keys, i);
323 str = WMGetFromPLString(tmp);
324 if (str) {
325 if (ignoreFonts && isFontOption(str)) {
326 WMRemoveFromPLDictionary(style, tmp);
327 continue;
329 if (ignoreCursors && isCursorOption(str)) {
330 WMRemoveFromPLDictionary(style, tmp);
331 continue;
333 if (isFontOption(str)) {
334 WMPropList *value;
335 char *newfont, *oldfont;
337 value = WMGetFromPLDictionary(style, tmp);
338 if (value) {
339 oldfont = WMGetFromPLString(value);
340 newfont = convertFont(oldfont, False);
341 if (newfont != oldfont) {
342 value = WMCreatePLString(newfont);
343 WMPutInPLDictionary(style, tmp, value);
344 WMReleasePropList(value);
345 wfree(newfont);
349 if (strcasecmp(str, "IconTitleColor")==0
350 || strcasecmp(str, "IconTitleBack")==0) {
351 foundIconTitle = 1;
352 } else if (strcasecmp(str, "ResizebarBack")==0) {
353 foundResizebarBack = 1;
358 if (!foundIconTitle) {
359 /* set the default values */
360 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleColor"));
361 if (tmp) {
362 WMPutInPLDictionary(style, WMCreatePLString("IconTitleColor"),
363 tmp);
366 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleBack"));
367 if (tmp) {
368 WMPropList *value;
370 value = getColor(tmp);
372 if (value) {
373 WMPutInPLDictionary(style, WMCreatePLString("IconTitleBack"),
374 value);
379 if (!foundResizebarBack) {
380 /* set the default values */
381 tmp = WMGetFromPLDictionary(style, WMCreatePLString("UTitleBack"));
382 if (tmp) {
383 WMPropList *value;
385 value = getColor(tmp);
387 if (value) {
388 WMPropList *t;
390 t = WMCreatePLArray(WMCreatePLString("solid"), value,
391 NULL);
392 WMPutInPLDictionary(style, WMCreatePLString("ResizebarBack"),
399 if (!WMGetFromPLDictionary(style, WMCreatePLString("MenuStyle"))) {
400 WMPutInPLDictionary(style, WMCreatePLString("MenuStyle"),
401 WMCreatePLString("normal"));
406 void
407 print_help()
409 printf("Usage: %s [OPTIONS] FILE\n", ProgName);
410 puts("Reads style/theme configuration from FILE and updates Window Maker.");
411 puts("");
412 puts(" --no-fonts ignore font related options");
413 puts(" --no-cursors ignore cursor related options");
414 puts(" --ignore <option> ignore changes in the specified option");
415 puts(" --help display this help and exit");
417 puts(" --format <format> specifies the format of the theme to be converted");
419 puts(" --version output version information and exit");
420 /*puts("");
421 puts("Supported formats: blackbox");*/
425 #define F_BLACKBOX 1
428 main(int argc, char **argv)
430 WMPropList *prop, *style;
431 char *path;
432 char *file = NULL;
433 struct stat statbuf;
434 int i;
435 int ignoreCount = 0;
436 char *ignoreList[MAX_OPTIONS];
438 dpy = XOpenDisplay("");
440 ProgName = argv[0];
442 if (argc<2) {
443 printf("%s: missing argument\n", ProgName);
444 printf("Try '%s --help' for more information\n", ProgName);
445 exit(1);
448 for (i = 1; i < argc; i++) {
449 if (strcmp("--ignore", argv[i])==0) {
450 i++;
451 if (i == argc) {
452 printf("%s: missing argument for option --ignore\n", ProgName);
453 exit(1);
455 ignoreList[ignoreCount++] = argv[i];
457 } else if (strcmp("--no-fonts", argv[i])==0) {
458 ignoreFonts = 1;
459 } else if (strcmp("--no-cursors", argv[i])==0) {
460 ignoreCursors = 1;
461 } else if (strcmp("--version", argv[i])==0) {
462 puts(PROG_VERSION);
463 exit(0);
464 } else if (strcmp("--help", argv[i])==0) {
465 print_help();
466 exit(0);
467 #if 0
468 } else if (strcmp("--format", argv[i])==0) {
469 i++;
470 if (i == argc) {
471 printf("%s: missing argument for option --format\n", ProgName);
472 exit(1);
474 if (strcasecmp(argv[i], "blackbox")==0) {
475 format = F_BLACKBOX;
476 } else {
477 printf("%s: unknown theme format '%s'\n", ProgName, argv[i]);
478 exit(1);
480 #endif
481 } else {
482 if (file) {
483 printf("%s: invalid argument '%s'\n", ProgName, argv[i]);
484 printf("Try '%s --help' for more information\n", ProgName);
485 exit(1);
487 file = argv[i];
491 WMPLSetCaseSensitive(False);
493 path = defaultsPathForDomain("WindowMaker");
495 prop = WMReadPropListFromFile(path);
496 if (!prop) {
497 perror(path);
498 printf("%s:could not load WindowMaker configuration file.\n",
499 ProgName);
500 exit(1);
503 if (stat(file, &statbuf) < 0) {
504 perror(file);
505 exit(1);
507 #if 0
508 if (format == F_BLACKBOX) {
509 style = readBlackBoxStyle(file);
510 if (!style) {
511 printf("%s: could not open style file\n", ProgName);
512 exit(1);
514 } else
515 #endif
517 if (S_ISDIR(statbuf.st_mode)) {
518 char buffer[4018];
519 char *prefix;
520 /* theme pack */
522 if (*argv[argc-1] != '/') {
523 if (!getcwd(buffer, 4000)) {
524 printf("%s: complete path for %s is too long\n", ProgName,
525 file);
526 exit(1);
528 if (strlen(buffer) + strlen(file) > 4000) {
529 printf("%s: complete path for %s is too long\n", ProgName,
530 file);
531 exit(1);
533 strcat(buffer, "/");
534 } else {
535 buffer[0] = 0;
537 strcat(buffer, file);
539 prefix = malloc(strlen(buffer)+10);
540 if (!prefix) {
541 printf("%s: out of memory\n", ProgName);
542 exit(1);
544 strcpy(prefix, buffer);
546 strcat(buffer, "/style");
548 style = WMReadPropListFromFile(buffer);
549 if (!style) {
550 perror(buffer);
551 printf("%s:could not load style file.\n", ProgName);
552 exit(1);
555 hackPaths(style, prefix);
556 free(prefix);
557 } else {
558 /* normal style file */
560 style = WMReadPropListFromFile(file);
561 if (!style) {
562 perror(file);
563 printf("%s:could not load style file.\n", ProgName);
564 exit(1);
569 if (!WMIsPLDictionary(style)) {
570 printf("%s: '%s' is not a style file/theme\n", ProgName, file);
571 exit(1);
574 hackStyle(style);
576 if (ignoreCount > 0) {
577 for (i = 0; i < ignoreCount; i++) {
578 WMRemoveFromPLDictionary(style, WMCreatePLString(ignoreList[i]));
582 WMMergePLDictionaries(prop, style, True);
584 WMWritePropListToFile(prop, path, True);
586 XEvent ev;
588 if (dpy) {
589 int i;
590 char *msg = "Reconfigure";
592 memset(&ev, 0, sizeof(XEvent));
594 ev.xclient.type = ClientMessage;
595 ev.xclient.message_type = XInternAtom(dpy, "_WINDOWMAKER_COMMAND",
596 False);
597 ev.xclient.window = DefaultRootWindow(dpy);
598 ev.xclient.format = 8;
600 for (i = 0; i <= strlen(msg); i++) {
601 ev.xclient.data.b[i] = msg[i];
603 XSendEvent(dpy, DefaultRootWindow(dpy), False,
604 SubstructureRedirectMask, &ev);
605 XFlush(dpy);
609 exit(0);
613 #if 0
614 char*
615 getToken(char *str, int i, char *buf)
621 static WMPropList*
622 readBlackBoxStyle(char *path)
624 FILE *f;
625 char buffer[128], char token[128];
626 WMPropList *style, *p;
628 f = fopen(path, "rb");
629 if (!f) {
630 perror(path);
631 return NULL;
634 while (1) {
635 if (!fgets(buffer, 127, f))
636 break;
638 if (strncasecmp(buffer, "menu.title:", 11)==0) {
643 #endif