Do not switch workspace to follow new windows in others
[wmaker-crm.git] / util / setstyle.c
blob692fb6118a55c178d8b84e5bb825572fb6680090
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.
23 #define PROG_VERSION "setstyle (Window Maker) 0.6"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <X11/Xlib.h>
31 #include <WINGs/WUtil.h>
33 #include "../src/wconfig.h"
35 #define MAX_OPTIONS 128
37 char *FontOptions[] = {
38 "IconTitleFont",
39 "ClipTitleFont",
40 "DisplayFont",
41 "LargeDisplayFont",
42 "MenuTextFont",
43 "MenuTitleFont",
44 "WindowTitleFont",
45 NULL
48 char *CursorOptions[] = {
49 "NormalCursor",
50 "ArrowCursor",
51 "MoveCursor",
52 "ResizeCursor",
53 "TopLeftResizeCursor",
54 "TopRightResizeCursor",
55 "BottomLeftResizeCursor",
56 "BottomRightResizeCursor",
57 "VerticalResizeCursor",
58 "HorizontalResizeCursor",
59 "WaitCursor",
60 "QuestionCursor",
61 "TextCursor",
62 "SelectCursor",
63 NULL
66 char *ProgName;
67 int ignoreFonts = 0;
68 int ignoreCursors = 0;
70 Display *dpy;
72 extern char *convertFont(char *font, Bool keepXLFD);
74 WMPropList *readBlackBoxStyle(char *path);
76 static Bool isCursorOption(char *option)
78 int i;
80 for (i = 0; CursorOptions[i] != NULL; i++) {
81 if (strcasecmp(option, CursorOptions[i]) == 0) {
82 return True;
86 return False;
89 static Bool isFontOption(char *option)
91 int i;
93 for (i = 0; FontOptions[i] != NULL; i++) {
94 if (strcasecmp(option, FontOptions[i]) == 0) {
95 return True;
99 return False;
102 char *defaultsPathForDomain(char *domain)
104 static char path[1024];
105 char *gspath;
107 gspath = getenv("GNUSTEP_USER_ROOT");
108 if (gspath) {
109 strcpy(path, gspath);
110 strcat(path, "/");
111 } else {
112 char *home;
114 home = getenv("HOME");
115 if (!home) {
116 printf("%s:could not get HOME environment variable!\n", ProgName);
117 exit(0);
120 strcpy(path, home);
121 strcat(path, "/GNUstep/");
123 strcat(path, DEFAULTS_DIR);
124 strcat(path, "/");
125 strcat(path, domain);
127 return path;
130 void hackPathInTexture(WMPropList * texture, char *prefix)
132 WMPropList *type;
133 char *t;
135 /* get texture type */
136 type = WMGetFromPLArray(texture, 0);
137 t = WMGetFromPLString(type);
138 if (t == NULL)
139 return;
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) {
146 WMPropList *file;
147 char buffer[4018];
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) {
156 WMPropList *file;
157 char buffer[4018];
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)
177 WMPropList *keys;
178 WMPropList *key;
179 WMPropList *value;
180 int i;
182 keys = WMGetPLDictionaryKeys(style);
184 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
185 key = WMGetFromPLArray(keys, i);
187 value = WMGetFromPLDictionary(style, key);
188 if (!value)
189 continue;
191 if (strcasecmp(WMGetFromPLString(key), "WorkspaceSpecificBack") == 0) {
192 if (WMIsPLArray(value)) {
193 int j;
194 WMPropList *texture;
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);
206 } else {
208 if (WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
210 hackPathInTexture(value, prefix);
217 static WMPropList *getColor(WMPropList * texture)
219 WMPropList *value, *type;
220 char *str;
222 type = WMGetFromPLArray(texture, 0);
223 if (!type)
224 return NULL;
226 value = NULL;
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) {
233 WMPropList *c1, *c2;
234 int r1, g1, b1, r2, g2, b2;
235 char buffer[32];
237 c1 = WMGetFromPLArray(texture, 1);
238 c2 = WMGetFromPLArray(texture, 2);
239 if (!dpy) {
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);
244 } else {
245 value = c1;
247 } else {
248 XColor color1;
249 XColor color2;
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);
270 return value;
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:
277 * IconTitleColor
278 * IconTitleBack
280 void hackStyle(WMPropList * style)
282 WMPropList *keys, *tmp;
283 int foundIconTitle = 0, foundResizebarBack = 0;
284 int i;
286 keys = WMGetPLDictionaryKeys(style);
288 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
289 char *str;
291 tmp = WMGetFromPLArray(keys, i);
292 str = WMGetFromPLString(tmp);
293 if (str) {
294 if (ignoreFonts && isFontOption(str)) {
295 WMRemoveFromPLDictionary(style, tmp);
296 continue;
298 if (ignoreCursors && isCursorOption(str)) {
299 WMRemoveFromPLDictionary(style, tmp);
300 continue;
302 if (isFontOption(str)) {
303 WMPropList *value;
304 char *newfont, *oldfont;
306 value = WMGetFromPLDictionary(style, tmp);
307 if (value) {
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);
314 wfree(newfont);
318 if (strcasecmp(str, "IconTitleColor") == 0 || strcasecmp(str, "IconTitleBack") == 0) {
319 foundIconTitle = 1;
320 } else if (strcasecmp(str, "ResizebarBack") == 0) {
321 foundResizebarBack = 1;
326 if (!foundIconTitle) {
327 /* set the default values */
328 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleColor"));
329 if (tmp) {
330 WMPutInPLDictionary(style, WMCreatePLString("IconTitleColor"), tmp);
333 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleBack"));
334 if (tmp) {
335 WMPropList *value;
337 value = getColor(tmp);
339 if (value) {
340 WMPutInPLDictionary(style, WMCreatePLString("IconTitleBack"), value);
345 if (!foundResizebarBack) {
346 /* set the default values */
347 tmp = WMGetFromPLDictionary(style, WMCreatePLString("UTitleBack"));
348 if (tmp) {
349 WMPropList *value;
351 value = getColor(tmp);
353 if (value) {
354 WMPropList *t;
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"));
367 void print_help()
369 printf("Usage: %s [OPTIONS] FILE\n", ProgName);
370 puts("Reads style/theme configuration from FILE and updates Window Maker.");
371 puts("");
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");
380 /*puts("");
381 puts("Supported formats: blackbox"); */
384 #define F_BLACKBOX 1
386 int main(int argc, char **argv)
388 WMPropList *prop, *style;
389 char *path;
390 char *file = NULL;
391 struct stat statbuf;
392 int i;
393 int ignoreCount = 0;
394 char *ignoreList[MAX_OPTIONS];
396 dpy = XOpenDisplay("");
398 ProgName = argv[0];
400 if (argc < 2) {
401 printf("%s: missing argument\n", ProgName);
402 printf("Try '%s --help' for more information\n", ProgName);
403 exit(1);
406 for (i = 1; i < argc; i++) {
407 if (strcmp("--ignore", argv[i]) == 0) {
408 i++;
409 if (i == argc) {
410 printf("%s: missing argument for option --ignore\n", ProgName);
411 exit(1);
413 ignoreList[ignoreCount++] = argv[i];
415 } else if (strcmp("--no-fonts", argv[i]) == 0) {
416 ignoreFonts = 1;
417 } else if (strcmp("--no-cursors", argv[i]) == 0) {
418 ignoreCursors = 1;
419 } else if (strcmp("--version", argv[i]) == 0) {
420 puts(PROG_VERSION);
421 exit(0);
422 } else if (strcmp("--help", argv[i]) == 0) {
423 print_help();
424 exit(0);
425 #if 0
426 } else if (strcmp("--format", argv[i]) == 0) {
427 i++;
428 if (i == argc) {
429 printf("%s: missing argument for option --format\n", ProgName);
430 exit(1);
432 if (strcasecmp(argv[i], "blackbox") == 0) {
433 format = F_BLACKBOX;
434 } else {
435 printf("%s: unknown theme format '%s'\n", ProgName, argv[i]);
436 exit(1);
438 #endif
439 } else {
440 if (file) {
441 printf("%s: invalid argument '%s'\n", ProgName, argv[i]);
442 printf("Try '%s --help' for more information\n", ProgName);
443 exit(1);
445 file = argv[i];
449 WMPLSetCaseSensitive(False);
451 path = defaultsPathForDomain("WindowMaker");
453 prop = WMReadPropListFromFile(path);
454 if (!prop) {
455 perror(path);
456 printf("%s:could not load WindowMaker configuration file.\n", ProgName);
457 exit(1);
460 if (stat(file, &statbuf) < 0) {
461 perror(file);
462 exit(1);
464 #if 0
465 if (format == F_BLACKBOX) {
466 style = readBlackBoxStyle(file);
467 if (!style) {
468 printf("%s: could not open style file\n", ProgName);
469 exit(1);
471 } else
472 #endif
474 if (S_ISDIR(statbuf.st_mode)) {
475 char buffer[4018];
476 char *prefix;
477 /* theme pack */
479 if (*argv[argc - 1] != '/') {
480 if (!getcwd(buffer, 4000)) {
481 printf("%s: complete path for %s is too long\n", ProgName, file);
482 exit(1);
484 if (strlen(buffer) + strlen(file) > 4000) {
485 printf("%s: complete path for %s is too long\n", ProgName, file);
486 exit(1);
488 strcat(buffer, "/");
489 } else {
490 buffer[0] = 0;
492 strcat(buffer, file);
494 prefix = malloc(strlen(buffer) + 10);
495 if (!prefix) {
496 printf("%s: out of memory\n", ProgName);
497 exit(1);
499 strcpy(prefix, buffer);
501 strcat(buffer, "/style");
503 style = WMReadPropListFromFile(buffer);
504 if (!style) {
505 perror(buffer);
506 printf("%s:could not load style file.\n", ProgName);
507 exit(1);
510 hackPaths(style, prefix);
511 free(prefix);
512 } else {
513 /* normal style file */
515 style = WMReadPropListFromFile(file);
516 if (!style) {
517 perror(file);
518 printf("%s:could not load style file.\n", ProgName);
519 exit(1);
524 if (!WMIsPLDictionary(style)) {
525 printf("%s: '%s' is not a style file/theme\n", ProgName, file);
526 exit(1);
529 hackStyle(style);
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);
541 XEvent ev;
543 if (dpy) {
544 int i;
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);
558 XFlush(dpy);
562 exit(0);
565 #if 0
566 char *getToken(char *str, int i, char *buf)
571 static WMPropList *readBlackBoxStyle(char *path)
573 FILE *f;
574 char buffer[128], char token[128];
575 WMPropList *style, *p;
577 f = fopen(path, "rb");
578 if (!f) {
579 perror(path);
580 return NULL;
583 while (1) {
584 if (!fgets(buffer, 127, f))
585 break;
587 if (strncasecmp(buffer, "menu.title:", 11) == 0) {
592 #endif