- made deiconification not automatically focus window in sloppy focus
[wmaker-crm.git] / util / setstyle.c
blob50505e7017f86d5fb79792790e5216d87a9863a4
1 /* setstyle.c - loads style related options to wmaker
3 * WindowMaker window manager
4 *
5 * Copyright (c) 1997, 1998, 1999 Alfredo K. Kojima
6 *
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.5"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <proplist.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
32 #include <X11/Xlib.h>
34 #include <string.h>
36 #include "../src/wconfig.h"
38 #define MAX_OPTIONS 128
40 char *FontOptions[] = {
41 "IconTitleFont",
42 "ClipTitleFont",
43 "DisplayFont",
44 "LargeDisplayFont",
45 "MenuTextFont",
46 "MenuTitleFont",
47 "WindowTitleFont",
48 NULL
53 char *ProgName;
54 int ignoreFonts = 0;
56 Display *dpy;
58 char*
59 defaultsPathForDomain(char *domain)
61 char path[1024];
62 char *gspath, *tmp;
64 gspath = getenv("GNUSTEP_USER_ROOT");
65 if (gspath) {
66 strcpy(path, gspath);
67 strcat(path, "/");
68 } else {
69 char *home;
71 home = getenv("HOME");
72 if (!home) {
73 printf("%s:could not get HOME environment variable!\n", ProgName);
74 exit(0);
77 strcpy(path, home);
78 strcat(path, "/GNUstep/");
80 strcat(path, DEFAULTS_DIR);
81 strcat(path, "/");
82 strcat(path, domain);
84 tmp = malloc(strlen(path)+2);
85 strcpy(tmp, path);
87 return tmp;
92 void
93 hackPathInTexture(proplist_t texture, char *prefix)
95 proplist_t type;
96 char *t;
98 /* get texture type */
99 type = PLGetArrayElement(texture, 0);
100 t = PLGetString(type);
101 if (t && (strcasecmp(t, "tpixmap")==0
102 || strcasecmp(t, "spixmap")==0
103 || strcasecmp(t, "mpixmap")==0
104 || strcasecmp(t, "cpixmap")==0
105 || strcasecmp(t, "tvgradient")==0
106 || strcasecmp(t, "thgradient")==0
107 || strcasecmp(t, "tdgradient")==0)) {
108 proplist_t file;
109 char buffer[4018];
111 /* get pixmap file path */
112 file = PLGetArrayElement(texture, 1);
113 sprintf(buffer, "%s/%s", prefix, PLGetString(file));
114 /* replace path with full path */
115 PLRemoveArrayElement(texture, 1);
116 PLInsertArrayElement(texture, PLMakeString(buffer), 1);
121 void
122 hackPaths(proplist_t style, char *prefix)
124 proplist_t keys;
125 proplist_t key;
126 proplist_t value;
127 int i;
130 keys = PLGetAllDictionaryKeys(style);
132 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
133 key = PLGetArrayElement(keys, i);
135 value = PLGetDictionaryEntry(style, key);
136 if (!value)
137 continue;
139 if (strcasecmp(PLGetString(key), "WorkspaceSpecificBack")==0) {
140 if (PLIsArray(value)) {
141 int j;
142 proplist_t texture;
144 for (j = 0; j < PLGetNumberOfElements(value); j++) {
145 texture = PLGetArrayElement(value, j);
147 if (texture && PLIsArray(texture)
148 && PLGetNumberOfElements(texture) > 2) {
150 hackPathInTexture(texture, prefix);
154 } else {
156 if (PLIsArray(value) && PLGetNumberOfElements(value) > 2) {
158 hackPathInTexture(value, prefix);
166 static proplist_t
167 getColor(proplist_t texture)
169 proplist_t value, type;
170 char *str;
172 type = PLGetArrayElement(texture, 0);
173 if (!type)
174 return NULL;
176 value = NULL;
178 str = PLGetString(type);
179 if (strcasecmp(str, "solid")==0) {
180 value = PLGetArrayElement(texture, 1);
181 } else if (strcasecmp(str, "dgradient")==0
182 || strcasecmp(str, "hgradient")==0
183 || strcasecmp(str, "vgradient")==0) {
184 proplist_t c1, c2;
185 int r1, g1, b1, r2, g2, b2;
186 char buffer[32];
188 c1 = PLGetArrayElement(texture, 1);
189 c2 = PLGetArrayElement(texture, 2);
190 if (!dpy) {
191 if (sscanf(PLGetString(c1), "#%2x%2x%2x", &r1, &g1, &b1)==3
192 && sscanf(PLGetString(c2), "#%2x%2x%2x", &r2, &g2, &b2)==3) {
193 sprintf(buffer, "#%02x%02x%02x", (r1+r2)/2, (g1+g2)/2,
194 (b1+b2)/2);
195 value = PLMakeString(buffer);
196 } else {
197 value = c1;
199 } else {
200 XColor color1;
201 XColor color2;
203 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
204 PLGetString(c1), &color1);
205 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
206 PLGetString(c2), &color2);
208 sprintf(buffer, "#%02x%02x%02x",
209 (color1.red+color2.red)>>9,
210 (color1.green+color2.green)>>9,
211 (color1.blue+color2.blue)>>9);
212 value = PLMakeString(buffer);
214 } else if (strcasecmp(str, "mdgradient")==0
215 || strcasecmp(str, "mhgradient")==0
216 || strcasecmp(str, "mvgradient")==0) {
218 value = PLGetArrayElement(texture, 1);
220 } else if (strcasecmp(str, "tpixmap")==0
221 || strcasecmp(str, "cpixmap")==0
222 || strcasecmp(str, "spixmap")==0) {
224 value = PLGetArrayElement(texture, 2);
227 return value;
232 * since some of the options introduce incompatibilities, we will need
233 * to do a kluge here or the themes ppl will get real annoying.
234 * So, treat for the absence of the following options:
235 * IconTitleColor
236 * IconTitleBack
238 void
239 hackStyle(proplist_t style)
241 proplist_t keys;
242 proplist_t tmp;
243 int i;
244 int foundIconTitle = 0;
245 int foundResizebarBack = 0;
247 keys = PLGetAllDictionaryKeys(style);
249 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
250 char *str;
252 tmp = PLGetArrayElement(keys, i);
253 str = PLGetString(tmp);
254 if (str) {
255 int j, found;
257 if (ignoreFonts) {
258 for (j = 0, found = 0; FontOptions[j]!=NULL; j++) {
259 if (strcasecmp(str, FontOptions[j])==0) {
260 PLRemoveDictionaryEntry(style, tmp);
261 found = 1;
262 break;
265 if (found)
266 continue;
269 if (strcasecmp(str, "IconTitleColor")==0
270 || strcasecmp(str, "IconTitleBack")==0) {
271 foundIconTitle = 1;
272 } else if (strcasecmp(str, "ResizebarBack")==0) {
273 foundResizebarBack = 1;
278 if (!foundIconTitle) {
279 /* set the default values */
280 tmp = PLGetDictionaryEntry(style, PLMakeString("FTitleColor"));
281 if (tmp) {
282 PLInsertDictionaryEntry(style, PLMakeString("IconTitleColor"),
283 tmp);
286 tmp = PLGetDictionaryEntry(style, PLMakeString("FTitleBack"));
287 if (tmp) {
288 proplist_t value;
290 value = getColor(tmp);
292 if (value) {
293 PLInsertDictionaryEntry(style, PLMakeString("IconTitleBack"),
294 value);
299 if (!foundResizebarBack) {
300 /* set the default values */
301 tmp = PLGetDictionaryEntry(style, PLMakeString("UTitleBack"));
302 if (tmp) {
303 proplist_t value;
305 value = getColor(tmp);
307 if (value) {
308 proplist_t t;
310 t = PLMakeArrayFromElements(PLMakeString("solid"), value,
311 NULL);
312 PLInsertDictionaryEntry(style, PLMakeString("ResizebarBack"),
319 if (!PLGetDictionaryEntry(style, PLMakeString("MenuStyle"))) {
320 PLInsertDictionaryEntry(style, PLMakeString("MenuStyle"),
321 PLMakeString("normal"));
326 BOOL
327 StringCompareHook(proplist_t pl1, proplist_t pl2)
329 char *str1, *str2;
331 str1 = PLGetString(pl1);
332 str2 = PLGetString(pl2);
334 if (strcasecmp(str1, str2)==0)
335 return YES;
336 else
337 return NO;
342 void
343 print_help()
345 printf("Usage: %s [OPTIONS] FILE\n", ProgName);
346 puts("Reads style/theme configuration from FILE and updates Window Maker.");
347 puts("");
348 puts(" --no-fonts ignore font related options");
349 puts(" --ignore <option> ignore changes in the specified option");
350 puts(" --help display this help and exit");
351 puts(" --version output version information and exit");
355 int
356 main(int argc, char **argv)
358 proplist_t prop, style;
359 char *path;
360 char *file = NULL;
361 struct stat statbuf;
362 int i;
363 int ignoreCount = 0;
364 char *ignoreList[MAX_OPTIONS];
366 dpy = XOpenDisplay("");
368 ProgName = argv[0];
370 if (argc<2) {
371 printf("%s: missing argument\n", ProgName);
372 printf("Try '%s --help' for more information\n", ProgName);
373 exit(1);
376 for (i = 1; i < argc; i++) {
377 if (strcmp("--ignore", argv[i])==0) {
378 i++;
379 if (i == argc) {
380 printf("%s: missing argument for option --ignore", ProgName);
381 exit(1);
383 ignoreList[ignoreCount++] = argv[i];
385 } else if (strcmp("--no-fonts", argv[i])==0) {
386 ignoreFonts = 1;
387 } else if (strcmp("--version", argv[i])==0) {
388 puts(PROG_VERSION);
389 exit(0);
390 } else if (strcmp("--help", argv[i])==0) {
391 print_help();
392 exit(0);
393 } else {
394 if (file) {
395 printf("%s: invalid argument '%s'\n", ProgName, argv[i]);
396 printf("Try '%s --help' for more information\n", ProgName);
397 exit(1);
399 file = argv[i];
403 PLSetStringCmpHook(StringCompareHook);
405 path = defaultsPathForDomain("WindowMaker");
407 prop = PLGetProplistWithPath(path);
408 if (!prop) {
409 perror(path);
410 printf("%s:could not load WindowMaker configuration file.\n",
411 ProgName);
412 exit(1);
415 if (stat(file, &statbuf) < 0) {
416 perror(file);
417 exit(1);
420 if (S_ISDIR(statbuf.st_mode)) {
421 char buffer[4018];
422 char *prefix;
424 if (*argv[argc-1] != '/') {
425 if (!getcwd(buffer, 4000)) {
426 printf("%s: complete path for %s is too long\n", ProgName,
427 file);
428 exit(1);
430 if (strlen(buffer) + strlen(file) > 4000) {
431 printf("%s: complete path for %s is too long\n", ProgName,
432 file);
433 exit(1);
435 strcat(buffer, "/");
436 } else {
437 buffer[0] = 0;
439 strcat(buffer, file);
441 prefix = malloc(strlen(buffer)+10);
442 if (!prefix) {
443 printf("%s: out of memory\n", ProgName);
444 exit(1);
446 strcpy(prefix, buffer);
448 strcat(buffer, "/style");
450 style = PLGetProplistWithPath(buffer);
451 if (!style) {
452 perror(buffer);
453 printf("%s:could not load style file.\n", ProgName);
454 exit(1);
457 hackPaths(style, prefix);
458 free(prefix);
459 } else {
460 style = PLGetProplistWithPath(file);
461 if (!style) {
462 perror(file);
463 printf("%s:could not load style file.\n", ProgName);
464 exit(1);
468 if (!PLIsDictionary(style)) {
469 printf("%s: '%s' is not a style file/theme\n", ProgName, file);
470 exit(1);
473 hackStyle(style);
475 if (ignoreCount > 0) {
476 for (i = 0; i < ignoreCount; i++) {
477 PLRemoveDictionaryEntry(style, PLMakeString(ignoreList[i]));
481 PLMergeDictionaries(prop, style);
483 PLSave(prop, YES);
485 XEvent ev;
487 if (dpy) {
488 int i;
489 char *msg = "Reconfigure";
491 memset(&ev, 0, sizeof(XEvent));
493 ev.xclient.type = ClientMessage;
494 ev.xclient.message_type = XInternAtom(dpy, "_WINDOWMAKER_COMMAND",
495 False);
496 ev.xclient.window = DefaultRootWindow(dpy);
497 ev.xclient.format = 8;
499 for (i = 0; i <= strlen(msg); i++) {
500 ev.xclient.data.b[i] = msg[i];
502 XSendEvent(dpy, DefaultRootWindow(dpy), False,
503 SubstructureRedirectMask, &ev);
504 XFlush(dpy);
508 exit(0);