Change to the linux kernel coding style
[wmaker-crm.git] / util / setstyle.c
1 /* setstyle.c - loads style related options to wmaker
2 *
3 * WindowMaker window manager
4 *
5 * Copyright (c) 1997-2003 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.
11 *
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.
16 *
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.
21 */
22
23 #define PROG_VERSION "setstyle (Window Maker) 0.6"
24
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>
32
33 #include "../src/wconfig.h"
34
35 #define MAX_OPTIONS 128
36
37 char *FontOptions[] = {
38 "IconTitleFont",
39 "ClipTitleFont",
40 "DisplayFont",
41 "LargeDisplayFont",
42 "MenuTextFont",
43 "MenuTitleFont",
44 "WindowTitleFont",
45 NULL
46 };
47
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
64 };
65
66 char *ProgName;
67 int ignoreFonts = 0;
68 int ignoreCursors = 0;
69
70 Display *dpy;
71
72 extern char *convertFont(char *font, Bool keepXLFD);
73
74 WMPropList *readBlackBoxStyle(char *path);
75
76 static Bool isCursorOption(char *option)
77 {
78 int i;
79
80 for (i = 0; CursorOptions[i] != NULL; i++) {
81 if (strcasecmp(option, CursorOptions[i]) == 0) {
82 return True;
83 }
84 }
85
86 return False;
87 }
88
89 static Bool isFontOption(char *option)
90 {
91 int i;
92
93 for (i = 0; FontOptions[i] != NULL; i++) {
94 if (strcasecmp(option, FontOptions[i]) == 0) {
95 return True;
96 }
97 }
98
99 return False;
100 }
101
102 char *defaultsPathForDomain(char *domain)
103 {
104 static char path[1024];
105 char *gspath;
106
107 gspath = getenv("GNUSTEP_USER_ROOT");
108 if (gspath) {
109 strcpy(path, gspath);
110 strcat(path, "/");
111 } else {
112 char *home;
113
114 home = getenv("HOME");
115 if (!home) {
116 printf("%s:could not get HOME environment variable!\n", ProgName);
117 exit(0);
118 }
119
120 strcpy(path, home);
121 strcat(path, "/GNUstep/");
122 }
123 strcat(path, DEFAULTS_DIR);
124 strcat(path, "/");
125 strcat(path, domain);
126
127 return path;
128 }
129
130 void hackPathInTexture(WMPropList * texture, char *prefix)
131 {
132 WMPropList *type;
133 char *t;
134
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];
148
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];
158
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));
165
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));
172 }
173 }
174
175 void hackPaths(WMPropList * style, char *prefix)
176 {
177 WMPropList *keys;
178 WMPropList *key;
179 WMPropList *value;
180 int i;
181
182 keys = WMGetPLDictionaryKeys(style);
183
184 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
185 key = WMGetFromPLArray(keys, i);
186
187 value = WMGetFromPLDictionary(style, key);
188 if (!value)
189 continue;
190
191 if (strcasecmp(WMGetFromPLString(key), "WorkspaceSpecificBack") == 0) {
192 if (WMIsPLArray(value)) {
193 int j;
194 WMPropList *texture;
195
196 for (j = 0; j < WMGetPropListItemCount(value); j++) {
197 texture = WMGetFromPLArray(value, j);
198
199 if (texture && WMIsPLArray(texture)
200 && WMGetPropListItemCount(texture) > 2) {
201
202 hackPathInTexture(texture, prefix);
203 }
204 }
205 }
206 } else {
207
208 if (WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
209
210 hackPathInTexture(value, prefix);
211 }
212 }
213 }
214
215 }
216
217 static WMPropList *getColor(WMPropList * texture)
218 {
219 WMPropList *value, *type;
220 char *str;
221
222 type = WMGetFromPLArray(texture, 0);
223 if (!type)
224 return NULL;
225
226 value = NULL;
227
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];
236
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;
246 }
247 } else {
248 XColor color1;
249 XColor color2;
250
251 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)), WMGetFromPLString(c1), &color1);
252 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)), WMGetFromPLString(c2), &color2);
253
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);
258 }
259 } else if (strcasecmp(str, "mdgradient") == 0
260 || strcasecmp(str, "mhgradient") == 0 || strcasecmp(str, "mvgradient") == 0) {
261
262 value = WMGetFromPLArray(texture, 1);
263
264 } else if (strcasecmp(str, "tpixmap") == 0
265 || strcasecmp(str, "cpixmap") == 0 || strcasecmp(str, "spixmap") == 0) {
266
267 value = WMGetFromPLArray(texture, 2);
268 }
269
270 return value;
271 }
272
273 /*
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
279 */
280 void hackStyle(WMPropList * style)
281 {
282 WMPropList *keys, *tmp;
283 int foundIconTitle = 0, foundResizebarBack = 0;
284 int i;
285
286 keys = WMGetPLDictionaryKeys(style);
287
288 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
289 char *str;
290
291 tmp = WMGetFromPLArray(keys, i);
292 str = WMGetFromPLString(tmp);
293 if (str) {
294 if (ignoreFonts && isFontOption(str)) {
295 WMRemoveFromPLDictionary(style, tmp);
296 continue;
297 }
298 if (ignoreCursors && isCursorOption(str)) {
299 WMRemoveFromPLDictionary(style, tmp);
300 continue;
301 }
302 if (isFontOption(str)) {
303 WMPropList *value;
304 char *newfont, *oldfont;
305
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);
315 }
316 }
317 }
318 if (strcasecmp(str, "IconTitleColor") == 0 || strcasecmp(str, "IconTitleBack") == 0) {
319 foundIconTitle = 1;
320 } else if (strcasecmp(str, "ResizebarBack") == 0) {
321 foundResizebarBack = 1;
322 }
323 }
324 }
325
326 if (!foundIconTitle) {
327 /* set the default values */
328 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleColor"));
329 if (tmp) {
330 WMPutInPLDictionary(style, WMCreatePLString("IconTitleColor"), tmp);
331 }
332
333 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleBack"));
334 if (tmp) {
335 WMPropList *value;
336
337 value = getColor(tmp);
338
339 if (value) {
340 WMPutInPLDictionary(style, WMCreatePLString("IconTitleBack"), value);
341 }
342 }
343 }
344
345 if (!foundResizebarBack) {
346 /* set the default values */
347 tmp = WMGetFromPLDictionary(style, WMCreatePLString("UTitleBack"));
348 if (tmp) {
349 WMPropList *value;
350
351 value = getColor(tmp);
352
353 if (value) {
354 WMPropList *t;
355
356 t = WMCreatePLArray(WMCreatePLString("solid"), value, NULL);
357 WMPutInPLDictionary(style, WMCreatePLString("ResizebarBack"), t);
358 }
359 }
360 }
361
362 if (!WMGetFromPLDictionary(style, WMCreatePLString("MenuStyle"))) {
363 WMPutInPLDictionary(style, WMCreatePLString("MenuStyle"), WMCreatePLString("normal"));
364 }
365 }
366
367 void print_help()
368 {
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 /*
377 puts(" --format <format> specifies the format of the theme to be converted");
378 */
379 puts(" --version output version information and exit");
380 /*puts("");
381 puts("Supported formats: blackbox"); */
382 }
383
384 #define F_BLACKBOX 1
385
386 int main(int argc, char **argv)
387 {
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];
395
396 dpy = XOpenDisplay("");
397
398 ProgName = argv[0];
399
400 if (argc < 2) {
401 printf("%s: missing argument\n", ProgName);
402 printf("Try '%s --help' for more information\n", ProgName);
403 exit(1);
404 }
405
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);
412 }
413 ignoreList[ignoreCount++] = argv[i];
414
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);
431 }
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);
437 }
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);
444 }
445 file = argv[i];
446 }
447 }
448
449 WMPLSetCaseSensitive(False);
450
451 path = defaultsPathForDomain("WindowMaker");
452
453 prop = WMReadPropListFromFile(path);
454 if (!prop) {
455 perror(path);
456 printf("%s:could not load WindowMaker configuration file.\n", ProgName);
457 exit(1);
458 }
459
460 if (stat(file, &statbuf) < 0) {
461 perror(file);
462 exit(1);
463 }
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);
470 }
471 } else
472 #endif
473 {
474 if (S_ISDIR(statbuf.st_mode)) {
475 char buffer[4018];
476 char *prefix;
477 /* theme pack */
478
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);
483 }
484 if (strlen(buffer) + strlen(file) > 4000) {
485 printf("%s: complete path for %s is too long\n", ProgName, file);
486 exit(1);
487 }
488 strcat(buffer, "/");
489 } else {
490 buffer[0] = 0;
491 }
492 strcat(buffer, file);
493
494 prefix = malloc(strlen(buffer) + 10);
495 if (!prefix) {
496 printf("%s: out of memory\n", ProgName);
497 exit(1);
498 }
499 strcpy(prefix, buffer);
500
501 strcat(buffer, "/style");
502
503 style = WMReadPropListFromFile(buffer);
504 if (!style) {
505 perror(buffer);
506 printf("%s:could not load style file.\n", ProgName);
507 exit(1);
508 }
509
510 hackPaths(style, prefix);
511 free(prefix);
512 } else {
513 /* normal style file */
514
515 style = WMReadPropListFromFile(file);
516 if (!style) {
517 perror(file);
518 printf("%s:could not load style file.\n", ProgName);
519 exit(1);
520 }
521 }
522 }
523
524 if (!WMIsPLDictionary(style)) {
525 printf("%s: '%s' is not a style file/theme\n", ProgName, file);
526 exit(1);
527 }
528
529 hackStyle(style);
530
531 if (ignoreCount > 0) {
532 for (i = 0; i < ignoreCount; i++) {
533 WMRemoveFromPLDictionary(style, WMCreatePLString(ignoreList[i]));
534 }
535 }
536
537 WMMergePLDictionaries(prop, style, True);
538
539 WMWritePropListToFile(prop, path, True);
540 {
541 XEvent ev;
542
543 if (dpy) {
544 int i;
545 char *msg = "Reconfigure";
546
547 memset(&ev, 0, sizeof(XEvent));
548
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;
553
554 for (i = 0; i <= strlen(msg); i++) {
555 ev.xclient.data.b[i] = msg[i];
556 }
557 XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureRedirectMask, &ev);
558 XFlush(dpy);
559 }
560 }
561
562 exit(0);
563 }
564
565 #if 0
566 char *getToken(char *str, int i, char *buf)
567 {
568
569 }
570
571 static WMPropList *readBlackBoxStyle(char *path)
572 {
573 FILE *f;
574 char buffer[128], char token[128];
575 WMPropList *style, *p;
576
577 f = fopen(path, "rb");
578 if (!f) {
579 perror(path);
580 return NULL;
581 }
582
583 while (1) {
584 if (!fgets(buffer, 127, f))
585 break;
586
587 if (strncasecmp(buffer, "menu.title:", 11) == 0) {
588
589 }
590 }
591 }
592 #endif