Collect low-hanging fruits
[wmaker-crm.git] / util / setstyle.c
blobcfa2b07435f66947fa8ab6d258fc2af4563110ed
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");
376 puts(" --version output version information and exit");
379 int main(int argc, char **argv)
381 WMPropList *prop, *style;
382 char *path;
383 char *file = NULL;
384 struct stat statbuf;
385 int i;
386 int ignoreCount = 0;
387 char *ignoreList[MAX_OPTIONS];
389 dpy = XOpenDisplay("");
391 ProgName = argv[0];
393 if (argc < 2) {
394 printf("%s: missing argument\n", ProgName);
395 printf("Try '%s --help' for more information\n", ProgName);
396 exit(1);
399 for (i = 1; i < argc; i++) {
400 if (strcmp("--ignore", argv[i]) == 0) {
401 i++;
402 if (i == argc) {
403 printf("%s: missing argument for option --ignore\n", ProgName);
404 exit(1);
406 ignoreList[ignoreCount++] = argv[i];
408 } else if (strcmp("--no-fonts", argv[i]) == 0) {
409 ignoreFonts = 1;
410 } else if (strcmp("--no-cursors", argv[i]) == 0) {
411 ignoreCursors = 1;
412 } else if (strcmp("--version", argv[i]) == 0) {
413 puts(PROG_VERSION);
414 exit(0);
415 } else if (strcmp("--help", argv[i]) == 0) {
416 print_help();
417 exit(0);
418 } else {
419 if (file) {
420 printf("%s: invalid argument '%s'\n", ProgName, argv[i]);
421 printf("Try '%s --help' for more information\n", ProgName);
422 exit(1);
424 file = argv[i];
428 WMPLSetCaseSensitive(False);
430 path = defaultsPathForDomain("WindowMaker");
432 prop = WMReadPropListFromFile(path);
433 if (!prop) {
434 perror(path);
435 printf("%s:could not load WindowMaker configuration file.\n", ProgName);
436 exit(1);
439 if (stat(file, &statbuf) < 0) {
440 perror(file);
441 exit(1);
443 if (S_ISDIR(statbuf.st_mode)) {
444 char buffer[4018];
445 char *prefix;
446 /* theme pack */
448 if (*argv[argc - 1] != '/') {
449 if (!getcwd(buffer, 4000)) {
450 printf("%s: complete path for %s is too long\n", ProgName, file);
451 exit(1);
453 if (strlen(buffer) + strlen(file) > 4000) {
454 printf("%s: complete path for %s is too long\n", ProgName, file);
455 exit(1);
457 strcat(buffer, "/");
458 } else {
459 buffer[0] = 0;
461 strcat(buffer, file);
463 prefix = malloc(strlen(buffer) + 10);
464 if (!prefix) {
465 printf("%s: out of memory\n", ProgName);
466 exit(1);
468 strcpy(prefix, buffer);
470 strcat(buffer, "/style");
472 style = WMReadPropListFromFile(buffer);
473 if (!style) {
474 perror(buffer);
475 printf("%s:could not load style file.\n", ProgName);
476 exit(1);
479 hackPaths(style, prefix);
480 free(prefix);
481 } else {
482 /* normal style file */
484 style = WMReadPropListFromFile(file);
485 if (!style) {
486 perror(file);
487 printf("%s:could not load style file.\n", ProgName);
488 exit(1);
492 if (!WMIsPLDictionary(style)) {
493 printf("%s: '%s' is not a style file/theme\n", ProgName, file);
494 exit(1);
497 hackStyle(style);
499 if (ignoreCount > 0) {
500 for (i = 0; i < ignoreCount; i++) {
501 WMRemoveFromPLDictionary(style, WMCreatePLString(ignoreList[i]));
505 WMMergePLDictionaries(prop, style, True);
507 WMWritePropListToFile(prop, path);
509 XEvent ev;
511 if (dpy) {
512 int i;
513 char *msg = "Reconfigure";
515 memset(&ev, 0, sizeof(XEvent));
517 ev.xclient.type = ClientMessage;
518 ev.xclient.message_type = XInternAtom(dpy, "_WINDOWMAKER_COMMAND", False);
519 ev.xclient.window = DefaultRootWindow(dpy);
520 ev.xclient.format = 8;
522 for (i = 0; i <= strlen(msg); i++) {
523 ev.xclient.data.b[i] = msg[i];
525 XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureRedirectMask, &ev);
526 XFlush(dpy);
530 exit(0);