fixed compilation prob
[wmaker-crm.git] / util / setstyle.c
blob22193e636c24964e50a8d07b95375d642b9b6323
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;
60 proplist_t readBlackBoxStyle(char *path);
64 char*
65 defaultsPathForDomain(char *domain)
67 char path[1024];
68 char *gspath, *tmp;
70 gspath = getenv("GNUSTEP_USER_ROOT");
71 if (gspath) {
72 strcpy(path, gspath);
73 strcat(path, "/");
74 } else {
75 char *home;
77 home = getenv("HOME");
78 if (!home) {
79 printf("%s:could not get HOME environment variable!\n", ProgName);
80 exit(0);
83 strcpy(path, home);
84 strcat(path, "/GNUstep/");
86 strcat(path, DEFAULTS_DIR);
87 strcat(path, "/");
88 strcat(path, domain);
90 tmp = malloc(strlen(path)+2);
91 strcpy(tmp, path);
93 return tmp;
98 void
99 hackPathInTexture(proplist_t texture, char *prefix)
101 proplist_t type;
102 char *t;
104 /* get texture type */
105 type = PLGetArrayElement(texture, 0);
106 t = PLGetString(type);
107 if (t && (strcasecmp(t, "tpixmap")==0
108 || strcasecmp(t, "spixmap")==0
109 || strcasecmp(t, "mpixmap")==0
110 || strcasecmp(t, "cpixmap")==0
111 || strcasecmp(t, "tvgradient")==0
112 || strcasecmp(t, "thgradient")==0
113 || strcasecmp(t, "tdgradient")==0)) {
114 proplist_t file;
115 char buffer[4018];
117 /* get pixmap file path */
118 file = PLGetArrayElement(texture, 1);
119 sprintf(buffer, "%s/%s", prefix, PLGetString(file));
120 /* replace path with full path */
121 PLRemoveArrayElement(texture, 1);
122 PLInsertArrayElement(texture, PLMakeString(buffer), 1);
127 void
128 hackPaths(proplist_t style, char *prefix)
130 proplist_t keys;
131 proplist_t key;
132 proplist_t value;
133 int i;
136 keys = PLGetAllDictionaryKeys(style);
138 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
139 key = PLGetArrayElement(keys, i);
141 value = PLGetDictionaryEntry(style, key);
142 if (!value)
143 continue;
145 if (strcasecmp(PLGetString(key), "WorkspaceSpecificBack")==0) {
146 if (PLIsArray(value)) {
147 int j;
148 proplist_t texture;
150 for (j = 0; j < PLGetNumberOfElements(value); j++) {
151 texture = PLGetArrayElement(value, j);
153 if (texture && PLIsArray(texture)
154 && PLGetNumberOfElements(texture) > 2) {
156 hackPathInTexture(texture, prefix);
160 } else {
162 if (PLIsArray(value) && PLGetNumberOfElements(value) > 2) {
164 hackPathInTexture(value, prefix);
172 static proplist_t
173 getColor(proplist_t texture)
175 proplist_t value, type;
176 char *str;
178 type = PLGetArrayElement(texture, 0);
179 if (!type)
180 return NULL;
182 value = NULL;
184 str = PLGetString(type);
185 if (strcasecmp(str, "solid")==0) {
186 value = PLGetArrayElement(texture, 1);
187 } else if (strcasecmp(str, "dgradient")==0
188 || strcasecmp(str, "hgradient")==0
189 || strcasecmp(str, "vgradient")==0) {
190 proplist_t c1, c2;
191 int r1, g1, b1, r2, g2, b2;
192 char buffer[32];
194 c1 = PLGetArrayElement(texture, 1);
195 c2 = PLGetArrayElement(texture, 2);
196 if (!dpy) {
197 if (sscanf(PLGetString(c1), "#%2x%2x%2x", &r1, &g1, &b1)==3
198 && sscanf(PLGetString(c2), "#%2x%2x%2x", &r2, &g2, &b2)==3) {
199 sprintf(buffer, "#%02x%02x%02x", (r1+r2)/2, (g1+g2)/2,
200 (b1+b2)/2);
201 value = PLMakeString(buffer);
202 } else {
203 value = c1;
205 } else {
206 XColor color1;
207 XColor color2;
209 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
210 PLGetString(c1), &color1);
211 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
212 PLGetString(c2), &color2);
214 sprintf(buffer, "#%02x%02x%02x",
215 (color1.red+color2.red)>>9,
216 (color1.green+color2.green)>>9,
217 (color1.blue+color2.blue)>>9);
218 value = PLMakeString(buffer);
220 } else if (strcasecmp(str, "mdgradient")==0
221 || strcasecmp(str, "mhgradient")==0
222 || strcasecmp(str, "mvgradient")==0) {
224 value = PLGetArrayElement(texture, 1);
226 } else if (strcasecmp(str, "tpixmap")==0
227 || strcasecmp(str, "cpixmap")==0
228 || strcasecmp(str, "spixmap")==0) {
230 value = PLGetArrayElement(texture, 2);
233 return value;
238 * since some of the options introduce incompatibilities, we will need
239 * to do a kluge here or the themes ppl will get real annoying.
240 * So, treat for the absence of the following options:
241 * IconTitleColor
242 * IconTitleBack
244 void
245 hackStyle(proplist_t style)
247 proplist_t keys;
248 proplist_t tmp;
249 int i;
250 int foundIconTitle = 0;
251 int foundResizebarBack = 0;
253 keys = PLGetAllDictionaryKeys(style);
255 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
256 char *str;
258 tmp = PLGetArrayElement(keys, i);
259 str = PLGetString(tmp);
260 if (str) {
261 int j, found;
263 if (ignoreFonts) {
264 for (j = 0, found = 0; FontOptions[j]!=NULL; j++) {
265 if (strcasecmp(str, FontOptions[j])==0) {
266 PLRemoveDictionaryEntry(style, tmp);
267 found = 1;
268 break;
271 if (found)
272 continue;
275 if (strcasecmp(str, "IconTitleColor")==0
276 || strcasecmp(str, "IconTitleBack")==0) {
277 foundIconTitle = 1;
278 } else if (strcasecmp(str, "ResizebarBack")==0) {
279 foundResizebarBack = 1;
284 if (!foundIconTitle) {
285 /* set the default values */
286 tmp = PLGetDictionaryEntry(style, PLMakeString("FTitleColor"));
287 if (tmp) {
288 PLInsertDictionaryEntry(style, PLMakeString("IconTitleColor"),
289 tmp);
292 tmp = PLGetDictionaryEntry(style, PLMakeString("FTitleBack"));
293 if (tmp) {
294 proplist_t value;
296 value = getColor(tmp);
298 if (value) {
299 PLInsertDictionaryEntry(style, PLMakeString("IconTitleBack"),
300 value);
305 if (!foundResizebarBack) {
306 /* set the default values */
307 tmp = PLGetDictionaryEntry(style, PLMakeString("UTitleBack"));
308 if (tmp) {
309 proplist_t value;
311 value = getColor(tmp);
313 if (value) {
314 proplist_t t;
316 t = PLMakeArrayFromElements(PLMakeString("solid"), value,
317 NULL);
318 PLInsertDictionaryEntry(style, PLMakeString("ResizebarBack"),
325 if (!PLGetDictionaryEntry(style, PLMakeString("MenuStyle"))) {
326 PLInsertDictionaryEntry(style, PLMakeString("MenuStyle"),
327 PLMakeString("normal"));
332 BOOL
333 StringCompareHook(proplist_t pl1, proplist_t pl2)
335 char *str1, *str2;
337 str1 = PLGetString(pl1);
338 str2 = PLGetString(pl2);
340 if (strcasecmp(str1, str2)==0)
341 return YES;
342 else
343 return NO;
347 void
348 print_help()
350 printf("Usage: %s [OPTIONS] FILE\n", ProgName);
351 puts("Reads style/theme configuration from FILE and updates Window Maker.");
352 puts("");
353 puts(" --no-fonts ignore font related options");
354 puts(" --ignore <option> ignore changes in the specified option");
355 puts(" --help display this help and exit");
357 puts(" --format <format> specifies the format of the theme to be converted");
359 puts(" --version output version information and exit");
360 puts("");
361 puts("Supported formats: blackbox");
365 #define F_BLACKBOX 1
367 int
368 main(int argc, char **argv)
370 proplist_t prop, style;
371 char *path;
372 char *file = NULL;
373 struct stat statbuf;
374 int i;
375 int ignoreCount = 0;
376 char *ignoreList[MAX_OPTIONS];
377 int format = 0;
379 dpy = XOpenDisplay("");
381 ProgName = argv[0];
383 if (argc<2) {
384 printf("%s: missing argument\n", ProgName);
385 printf("Try '%s --help' for more information\n", ProgName);
386 exit(1);
389 for (i = 1; i < argc; i++) {
390 if (strcmp("--ignore", argv[i])==0) {
391 i++;
392 if (i == argc) {
393 printf("%s: missing argument for option --ignore\n", ProgName);
394 exit(1);
396 ignoreList[ignoreCount++] = argv[i];
398 } else if (strcmp("--no-fonts", argv[i])==0) {
399 ignoreFonts = 1;
400 } else if (strcmp("--version", argv[i])==0) {
401 puts(PROG_VERSION);
402 exit(0);
403 } else if (strcmp("--help", argv[i])==0) {
404 print_help();
405 exit(0);
406 #if 0
407 } else if (strcmp("--format", argv[i])==0) {
408 i++;
409 if (i == argc) {
410 printf("%s: missing argument for option --format\n", ProgName);
411 exit(1);
413 if (strcasecmp(argv[i], "blackbox")==0) {
414 format = F_BLACKBOX;
415 } else {
416 printf("%s: unknown theme format '%s'\n", ProgName, argv[i]);
417 exit(1);
419 #endif
420 } else {
421 if (file) {
422 printf("%s: invalid argument '%s'\n", ProgName, argv[i]);
423 printf("Try '%s --help' for more information\n", ProgName);
424 exit(1);
426 file = argv[i];
430 PLSetStringCmpHook(StringCompareHook);
432 path = defaultsPathForDomain("WindowMaker");
434 prop = PLGetProplistWithPath(path);
435 if (!prop) {
436 perror(path);
437 printf("%s:could not load WindowMaker configuration file.\n",
438 ProgName);
439 exit(1);
442 if (stat(file, &statbuf) < 0) {
443 perror(file);
444 exit(1);
446 #if 0
447 if (format == F_BLACKBOX) {
448 style = readBlackBoxStyle(file);
449 if (!style) {
450 printf("%s: could not open style file\n", ProgName);
451 exit(1);
453 } else
454 #endif
456 if (S_ISDIR(statbuf.st_mode)) {
457 char buffer[4018];
458 char *prefix;
459 /* theme pack */
461 if (*argv[argc-1] != '/') {
462 if (!getcwd(buffer, 4000)) {
463 printf("%s: complete path for %s is too long\n", ProgName,
464 file);
465 exit(1);
467 if (strlen(buffer) + strlen(file) > 4000) {
468 printf("%s: complete path for %s is too long\n", ProgName,
469 file);
470 exit(1);
472 strcat(buffer, "/");
473 } else {
474 buffer[0] = 0;
476 strcat(buffer, file);
478 prefix = malloc(strlen(buffer)+10);
479 if (!prefix) {
480 printf("%s: out of memory\n", ProgName);
481 exit(1);
483 strcpy(prefix, buffer);
485 strcat(buffer, "/style");
487 style = PLGetProplistWithPath(buffer);
488 if (!style) {
489 perror(buffer);
490 printf("%s:could not load style file.\n", ProgName);
491 exit(1);
494 hackPaths(style, prefix);
495 free(prefix);
496 } else {
497 /* normal style file */
499 style = PLGetProplistWithPath(file);
500 if (!style) {
501 perror(file);
502 printf("%s:could not load style file.\n", ProgName);
503 exit(1);
508 if (!PLIsDictionary(style)) {
509 printf("%s: '%s' is not a style file/theme\n", ProgName, file);
510 exit(1);
513 hackStyle(style);
515 if (ignoreCount > 0) {
516 for (i = 0; i < ignoreCount; i++) {
517 PLRemoveDictionaryEntry(style, PLMakeString(ignoreList[i]));
521 PLMergeDictionaries(prop, style);
523 PLSave(prop, YES);
525 XEvent ev;
527 if (dpy) {
528 int i;
529 char *msg = "Reconfigure";
531 memset(&ev, 0, sizeof(XEvent));
533 ev.xclient.type = ClientMessage;
534 ev.xclient.message_type = XInternAtom(dpy, "_WINDOWMAKER_COMMAND",
535 False);
536 ev.xclient.window = DefaultRootWindow(dpy);
537 ev.xclient.format = 8;
539 for (i = 0; i <= strlen(msg); i++) {
540 ev.xclient.data.b[i] = msg[i];
542 XSendEvent(dpy, DefaultRootWindow(dpy), False,
543 SubstructureRedirectMask, &ev);
544 XFlush(dpy);
548 exit(0);
552 #if 0
553 char*
554 getToken(char *str, int i, char *buf)
560 proplist_t
561 readBlackBoxStyle(char *path)
563 FILE *f;
564 char buffer[128], char token[128];
565 proplist_t style;
566 proplist_t p;
568 f = fopen(path, "r");
569 if (!f) {
570 perror(path);
571 return NULL;
574 while (1) {
575 if (!fgets(buffer, 127, f))
576 break;
578 if (strncasecmp(buffer, "menu.title:", 11)==0) {
583 #endif