Fixed autoconf stuff in libwmfun, and patched wmsetbg to work with dynamically
[wmaker-crm.git] / util / wmsetbg.c
blobc61c2c2a396c011ef7f7cafa4828dac0c0cc4ab3
1 /* wmsetbg.c- sets root window background image and also works as
2 * workspace background setting helper for wmaker
4 * WindowMaker window manager
5 *
6 * Copyright (c) 1998, 1999 Alfredo K. Kojima
7 * Copyright (c) 1998 Dan Pascu
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 * USA.
26 * TODO: rewrite, too dirty
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <X11/Xlib.h>
33 #include <X11/Xutil.h>
34 #include <X11/Xatom.h>
35 #include <string.h>
36 #include <pwd.h>
37 #include <signal.h>
38 #include <sys/types.h>
39 #include <ctype.h>
41 #include "../src/config.h"
43 #ifdef HAVE_DLFCN_H
44 #include <dlfcn.h>
45 #endif
47 #include "../src/wconfig.h"
49 #include "../WINGs/WINGs.h"
50 #include "../WINGs/WUtil.h"
51 #include "../wrlib/wraster.h"
53 #include <proplist.h>
55 #define PROG_VERSION "wmsetbg (Window Maker) 2.1"
58 #define WORKSPACE_COUNT (MAX_WORKSPACES+1)
61 Display *dpy;
62 char *display = "";
63 Window root;
64 int scr;
65 int scrWidth;
66 int scrHeight;
68 Pixmap CurrentPixmap = None;
69 char *PixmapPath = NULL;
72 extern Pixmap LoadJPEG(RContext *rc, char *file_name, int *width, int *height);
75 typedef struct BackgroundTexture {
76 int refcount;
78 int solid;
80 char *spec;
82 XColor color;
83 Pixmap pixmap; /* for all textures, including solid */
84 int width; /* size of the pixmap */
85 int height;
86 } BackgroundTexture;
90 RImage*
91 loadImage(RContext *rc, char *file)
93 char *path;
94 RImage *image;
96 if (access(file, F_OK)!=0) {
97 path = wfindfile(PixmapPath, file);
98 if (!path) {
99 wwarning("%s:could not find image file used in texture", file);
100 return NULL;
102 } else {
103 path = wstrdup(file);
106 image = RLoadImage(rc, path, 0);
107 if (!image) {
108 wwarning("%s:could not load image file used in texture:%s", path,
109 RMessageForError(RErrorCode));
111 free(path);
113 return image;
117 BackgroundTexture*
118 parseTexture(RContext *rc, char *text)
120 BackgroundTexture *texture = NULL;
121 proplist_t texarray;
122 proplist_t val;
123 int count;
124 char *tmp;
125 char *type;
127 #define GETSTRORGOTO(val, str, i, label) \
128 val = PLGetArrayElement(texarray, i);\
129 if (!PLIsString(val)) {\
130 wwarning("could not parse texture %s", text);\
131 goto label;\
133 str = PLGetString(val)
135 texarray = PLGetProplistWithDescription(text);
136 if (!texarray || !PLIsArray(texarray)
137 || (count = PLGetNumberOfElements(texarray)) < 2) {
139 wwarning("could not parse texture %s", text);
140 if (texarray)
141 PLRelease(texarray);
142 return NULL;
145 texture = wmalloc(sizeof(BackgroundTexture));
146 memset(texture, 0, sizeof(BackgroundTexture));
148 GETSTRORGOTO(val, type, 0, error);
150 if (strcasecmp(type, "solid")==0) {
151 XColor color;
152 Pixmap pixmap;
154 texture->solid = 1;
156 GETSTRORGOTO(val, tmp, 1, error);
158 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
159 wwarning("could not parse color %s in texture %s", tmp, text);
160 goto error;
162 XAllocColor(dpy, DefaultColormap(dpy, scr), &color);
164 pixmap = XCreatePixmap(dpy, root, 8, 8, DefaultDepth(dpy, scr));
165 XSetForeground(dpy, DefaultGC(dpy, scr), color.pixel);
166 XFillRectangle(dpy, pixmap, DefaultGC(dpy, scr), 0, 0, 8, 8);
168 texture->pixmap = pixmap;
169 texture->color = color;
170 texture->width = 8;
171 texture->height = 8;
172 } else if (strcasecmp(type, "vgradient")==0
173 || strcasecmp(type, "dgradient")==0
174 || strcasecmp(type, "hgradient")==0) {
175 XColor color;
176 RColor color1, color2;
177 RImage *image;
178 Pixmap pixmap;
179 int gtype;
180 int iwidth, iheight;
182 GETSTRORGOTO(val, tmp, 1, error);
184 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
185 wwarning("could not parse color %s in texture %s", tmp, text);
186 goto error;
189 color1.red = color.red >> 8;
190 color1.green = color.green >> 8;
191 color1.blue = color.blue >> 8;
193 GETSTRORGOTO(val, tmp, 2, error);
195 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
196 wwarning("could not parse color %s in texture %s", tmp, text);
197 goto error;
200 color2.red = color.red >> 8;
201 color2.green = color.green >> 8;
202 color2.blue = color.blue >> 8;
204 switch (type[0]) {
205 case 'h':
206 case 'H':
207 gtype = RHorizontalGradient;
208 iwidth = scrWidth;
209 iheight = 1;
210 break;
211 case 'V':
212 case 'v':
213 gtype = RVerticalGradient;
214 iwidth = 1;
215 iheight = scrHeight;
216 break;
217 default:
218 gtype = RDiagonalGradient;
219 iwidth = scrWidth;
220 iheight = scrHeight;
221 break;
224 image = RRenderGradient(iwidth, iheight, &color1, &color2, gtype);
226 if (!image) {
227 wwarning("could not render gradient texture:%s",
228 RMessageForError(RErrorCode));
229 goto error;
232 if (!RConvertImage(rc, image, &pixmap)) {
233 wwarning("could not convert texture:%s",
234 RMessageForError(RErrorCode));
235 RDestroyImage(image);
236 goto error;
239 texture->width = image->width;
240 texture->height = image->height;
241 RDestroyImage(image);
243 texture->pixmap = pixmap;
244 } else if (strcasecmp(type, "mvgradient")==0
245 || strcasecmp(type, "mdgradient")==0
246 || strcasecmp(type, "mhgradient")==0) {
247 XColor color;
248 RColor **colors;
249 RImage *image;
250 Pixmap pixmap;
251 int i, j;
252 int gtype;
253 int iwidth, iheight;
255 colors = malloc(sizeof(RColor*)*(count-1));
256 if (!colors) {
257 wwarning("out of memory while parsing texture");
258 goto error;
260 memset(colors, 0, sizeof(RColor*)*(count-1));
262 for (i = 2; i < count; i++) {
263 val = PLGetArrayElement(texarray, i);
264 if (!PLIsString(val)) {
265 wwarning("could not parse texture %s", text);
267 for (j = 0; colors[j]!=NULL; j++)
268 free(colors[j]);
269 free(colors);
270 goto error;
272 tmp = PLGetString(val);
274 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
275 wwarning("could not parse color %s in texture %s",
276 tmp, text);
278 for (j = 0; colors[j]!=NULL; j++)
279 free(colors[j]);
280 free(colors);
281 goto error;
283 if (!(colors[i-2] = malloc(sizeof(RColor)))) {
284 wwarning("out of memory while parsing texture");
286 for (j = 0; colors[j]!=NULL; j++)
287 free(colors[j]);
288 free(colors);
289 goto error;
292 colors[i-2]->red = color.red >> 8;
293 colors[i-2]->green = color.green >> 8;
294 colors[i-2]->blue = color.blue >> 8;
297 switch (type[1]) {
298 case 'h':
299 case 'H':
300 gtype = RHorizontalGradient;
301 iwidth = scrWidth;
302 iheight = 1;
303 break;
304 case 'V':
305 case 'v':
306 gtype = RVerticalGradient;
307 iwidth = 1;
308 iheight = scrHeight;
309 break;
310 default:
311 gtype = RDiagonalGradient;
312 iwidth = scrWidth;
313 iheight = scrHeight;
314 break;
317 image = RRenderMultiGradient(iwidth, iheight, colors, gtype);
319 for (j = 0; colors[j]!=NULL; j++)
320 free(colors[j]);
321 free(colors);
323 if (!image) {
324 wwarning("could not render gradient texture:%s",
325 RMessageForError(RErrorCode));
326 goto error;
329 if (!RConvertImage(rc, image, &pixmap)) {
330 wwarning("could not convert texture:%s",
331 RMessageForError(RErrorCode));
332 RDestroyImage(image);
333 goto error;
336 texture->width = image->width;
337 texture->height = image->height;
338 RDestroyImage(image);
340 texture->pixmap = pixmap;
341 } else if (strcasecmp(type, "cpixmap")==0
342 || strcasecmp(type, "spixmap")==0
343 || strcasecmp(type, "mpixmap")==0
344 || strcasecmp(type, "tpixmap")==0) {
345 XColor color;
346 Pixmap pixmap = None;
347 RImage *image = NULL;
348 int w, h;
349 int iwidth, iheight;
351 GETSTRORGOTO(val, tmp, 1, error);
353 if (toupper(type[0]) == 'T' || toupper(type[0]) == 'C')
354 pixmap = LoadJPEG(rc, tmp, &iwidth, &iheight);
357 if (!pixmap) {
358 image = loadImage(rc, tmp);
359 if (!image) {
360 goto error;
362 iwidth = image->width;
363 iheight = image->height;
366 GETSTRORGOTO(val, tmp, 2, error);
368 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
369 wwarning("could not parse color %s in texture %s\n", tmp, text);
370 RDestroyImage(image);
371 goto error;
373 if (!XAllocColor(dpy, DefaultColormap(dpy, scr), &color)) {
374 RColor rcolor;
376 rcolor.red = color.red >> 8;
377 rcolor.green = color.green >> 8;
378 rcolor.blue = color.blue >> 8;
379 RGetClosestXColor(rc, &rcolor, &color);
381 switch (toupper(type[0])) {
382 case 'T':
383 texture->width = iwidth;
384 texture->height = iheight;
385 if (!pixmap && !RConvertImage(rc, image, &pixmap)) {
386 wwarning("could not convert texture:%s",
387 RMessageForError(RErrorCode));
388 RDestroyImage(image);
389 goto error;
391 if (image)
392 RDestroyImage(image);
393 break;
394 case 'S':
395 case 'M':
396 if (toupper(type[0])=='S') {
397 w = scrWidth;
398 h = scrHeight;
399 } else {
400 if (iwidth*scrHeight > iheight*scrWidth) {
401 w = scrWidth;
402 h = (scrWidth*iheight)/iwidth;
403 } else {
404 h = scrHeight;
405 w = (scrHeight*iwidth)/iheight;
409 RImage *simage;
411 simage = RScaleImage(image, w, h);
412 if (!simage) {
413 wwarning("could not scale image:%s",
414 RMessageForError(RErrorCode));
415 RDestroyImage(image);
416 goto error;
418 RDestroyImage(image);
419 image = simage;
420 iwidth = image->width;
421 iheight = image->height;
423 /* fall through */
424 case 'C':
426 Pixmap cpixmap;
428 if (!pixmap && !RConvertImage(rc, image, &pixmap)) {
429 wwarning("could not convert texture:%s",
430 RMessageForError(RErrorCode));
431 RDestroyImage(image);
432 goto error;
435 if (iwidth != scrWidth || iheight != scrHeight) {
436 int x, y, sx, sy, w, h;
438 cpixmap = XCreatePixmap(dpy, root, scrWidth, scrHeight,
439 DefaultDepth(dpy, scr));
441 XSetForeground(dpy, DefaultGC(dpy, scr), color.pixel);
442 XFillRectangle(dpy, cpixmap, DefaultGC(dpy, scr),
443 0, 0, scrWidth, scrHeight);
445 if (iheight < scrHeight) {
446 h = iheight;
447 y = (scrHeight - h)/2;
448 sy = 0;
449 } else {
450 sy = (iheight - scrHeight)/2;
451 y = 0;
452 h = scrHeight;
454 if (iwidth < scrWidth) {
455 w = iwidth;
456 x = (scrWidth - w)/2;
457 sx = 0;
458 } else {
459 sx = (iwidth - scrWidth)/2;
460 x = 0;
461 w = scrWidth;
464 XCopyArea(dpy, pixmap, cpixmap, DefaultGC(dpy, scr),
465 sx, sy, w, h, x, y);
466 XFreePixmap(dpy, pixmap);
467 pixmap = cpixmap;
469 if (image)
470 RDestroyImage(image);
472 texture->width = scrWidth;
473 texture->height = scrHeight;
475 break;
478 texture->pixmap = pixmap;
479 texture->color = color;
480 } else if (strcasecmp(type, "thgradient")==0
481 || strcasecmp(type, "tvgradient")==0
482 || strcasecmp(type, "tdgradient")==0) {
483 XColor color;
484 RColor color1, color2;
485 RImage *image;
486 RImage *gradient;
487 RImage *tiled;
488 Pixmap pixmap;
489 int opaq;
490 char *file;
491 int gtype;
492 int twidth, theight;
494 GETSTRORGOTO(val, file, 1, error);
496 GETSTRORGOTO(val, tmp, 2, error);
498 opaq = atoi(tmp);
500 GETSTRORGOTO(val, tmp, 3, error);
502 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
503 wwarning("could not parse color %s in texture %s", tmp, text);
504 goto error;
507 color1.red = color.red >> 8;
508 color1.green = color.green >> 8;
509 color1.blue = color.blue >> 8;
511 GETSTRORGOTO(val, tmp, 4, error);
513 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
514 wwarning("could not parse color %s in texture %s", tmp, text);
515 goto error;
518 color2.red = color.red >> 8;
519 color2.green = color.green >> 8;
520 color2.blue = color.blue >> 8;
522 image = loadImage(rc, file);
523 if (!image) {
524 RDestroyImage(gradient);
525 goto error;
528 switch (type[1]) {
529 case 'h':
530 case 'H':
531 gtype = RHorizontalGradient;
532 twidth = scrWidth;
533 theight = image->height > scrHeight ? scrHeight : image->height;
534 break;
535 case 'V':
536 case 'v':
537 gtype = RVerticalGradient;
538 twidth = image->width > scrWidth ? scrWidth : image->width;
539 theight = scrHeight;
540 break;
541 default:
542 gtype = RDiagonalGradient;
543 twidth = scrWidth;
544 theight = scrHeight;
545 break;
547 gradient = RRenderGradient(twidth, theight, &color1, &color2, gtype);
549 if (!gradient) {
550 wwarning("could not render texture:%s",
551 RMessageForError(RErrorCode));
552 RDestroyImage(gradient);
553 goto error;
556 tiled = RMakeTiledImage(image, twidth, theight);
557 if (!tiled) {
558 wwarning("could not render texture:%s",
559 RMessageForError(RErrorCode));
560 RDestroyImage(gradient);
561 RDestroyImage(image);
562 goto error;
564 RDestroyImage(image);
566 RCombineImagesWithOpaqueness(tiled, gradient, opaq);
567 RDestroyImage(gradient);
569 if (!RConvertImage(rc, tiled, &pixmap)) {
570 wwarning("could not convert texture:%s",
571 RMessageForError(RErrorCode));
572 RDestroyImage(image);
573 goto error;
575 texture->width = tiled->width;
576 texture->height = tiled->height;
578 RDestroyImage(tiled);
580 texture->pixmap = pixmap;
581 } else if (strcasecmp(type, "function")==0) {
582 #ifdef HAVE_DLFCN_H
583 void (*initFunc) (Display*, Colormap);
584 RImage* (*mainFunc) (int, char**, int, int, int);
585 Pixmap pixmap;
586 RImage *image = 0;
587 int success = 0;
588 char *lib, *func, **argv = 0;
589 void *handle = 0;
590 int i, argc;
592 if (count < 3)
593 goto function_cleanup;
595 /* get the library name */
596 GETSTRORGOTO(val, lib, 1, function_cleanup);
598 /* get the function name */
599 GETSTRORGOTO(val, func, 2, function_cleanup);
601 argc = count - 2;
602 argv = (char**)wmalloc(argc * sizeof(char*));
604 /* get the parameters */
605 argv[0] = func;
606 for (i=0; i<argc-1; i++) {
607 GETSTRORGOTO(val, tmp, 3+i, function_cleanup);
608 argv[i+1] = wstrdup(tmp);
611 handle = dlopen(lib, RTLD_LAZY);
612 if (!handle) {
613 wwarning(_("could not find library %s"), lib);
614 goto function_cleanup;
617 initFunc = dlsym(handle, "initWindowMaker");
618 if (!initFunc) {
619 wwarning(_("could not initialize library %s"), lib);
620 goto function_cleanup;
622 initFunc(dpy, DefaultColormap(dpy, scr));
624 mainFunc = dlsym(handle, func);
625 if (!mainFunc) {
626 wwarning(_("could not find function %s::%s"), lib, func);
627 goto function_cleanup;
629 image = mainFunc(argc, argv, scrWidth, scrHeight, 0);
631 if (!RConvertImage(rc, image, &pixmap)) {
632 wwarning("could not convert texture:%s",
633 RMessageForError(RErrorCode));
634 goto function_cleanup;
636 texture->width = scrWidth;
637 texture->height = scrHeight;
638 texture->pixmap = pixmap;
639 success = 1;
641 function_cleanup:
642 if (argv) {
643 int i;
644 for (i=0; i<argc; i++) {
645 free(argv[i]);
648 if (handle) {
649 dlclose(handle);
651 if (image) {
652 RDestroyImage(image);
654 if (!success) {
655 goto error;
657 #else
658 wwarning("function textures not supported");
659 goto error;
660 #endif
661 } else {
662 wwarning("invalid texture type %s", text);
663 goto error;
666 texture->spec = wstrdup(text);
668 return texture;
670 error:
671 if (texture)
672 free(texture);
673 if (texarray)
674 PLRelease(texarray);
676 return NULL;
680 void
681 freeTexture(BackgroundTexture *texture)
683 if (texture->solid) {
684 long pixel[1];
686 pixel[0] = texture->color.pixel;
687 /* dont free black/white pixels */
688 if (pixel[0]!=BlackPixelOfScreen(DefaultScreenOfDisplay(dpy))
689 && pixel[0]!=WhitePixelOfScreen(DefaultScreenOfDisplay(dpy)))
690 XFreeColors(dpy, DefaultColormap(dpy, scr), pixel, 1, 0);
692 free(texture->spec);
693 free(texture);
697 void
698 setupTexture(RContext *rc, BackgroundTexture **textures, int *maxTextures,
699 int workspace, char *texture)
701 BackgroundTexture *newTexture = NULL;
702 int i;
704 /* unset the texture */
705 if (!texture) {
706 if (textures[workspace]!=NULL) {
707 textures[workspace]->refcount--;
709 if (textures[workspace]->refcount == 0)
710 freeTexture(textures[workspace]);
712 textures[workspace] = NULL;
713 return;
716 if (textures[workspace]
717 && strcasecmp(textures[workspace]->spec, texture)==0) {
718 /* texture did not change */
719 return;
722 /* check if the same texture is already created */
723 for (i = 0; i < *maxTextures; i++) {
724 if (textures[i] && strcasecmp(textures[i]->spec, texture)==0) {
725 newTexture = textures[i];
726 break;
730 if (!newTexture) {
731 /* create the texture */
732 newTexture = parseTexture(rc, texture);
734 if (!newTexture)
735 return;
737 if (textures[workspace]!=NULL) {
739 textures[workspace]->refcount--;
741 if (textures[workspace]->refcount == 0)
742 freeTexture(textures[workspace]);
745 newTexture->refcount++;
746 textures[workspace] = newTexture;
748 if (*maxTextures < workspace)
749 *maxTextures = workspace;
754 Pixmap
755 duplicatePixmap(Pixmap pixmap, int width, int height)
757 Display *tmpDpy;
758 Pixmap copyP;
760 /* must open a new display or the RetainPermanent will
761 * leave stuff allocated in RContext unallocated after exit */
762 tmpDpy = XOpenDisplay(display);
763 if (!tmpDpy) {
764 wwarning("could not open display to update background image information");
766 return None;
767 } else {
768 XSync(dpy, False);
770 copyP = XCreatePixmap(tmpDpy, root, width, height,
771 DefaultDepth(tmpDpy, scr));
772 XCopyArea(tmpDpy, pixmap, copyP, DefaultGC(tmpDpy, scr),
773 0, 0, width, height, 0, 0);
774 XSync(tmpDpy, False);
776 XSetCloseDownMode(tmpDpy, RetainPermanent);
777 XCloseDisplay(tmpDpy);
780 return copyP;
784 static int
785 dummyErrorHandler(Display *dpy, XErrorEvent *err)
787 return 0;
790 void
791 setPixmapProperty(Pixmap pixmap)
793 static Atom prop = 0;
794 Atom type;
795 int format;
796 unsigned long length, after;
797 unsigned char *data;
798 int mode;
800 if (!prop) {
801 prop = XInternAtom(dpy, "_XROOTPMAP_ID", False);
804 XGrabServer(dpy);
806 /* Clear out the old pixmap */
807 XGetWindowProperty(dpy, root, prop, 0L, 1L, False, AnyPropertyType,
808 &type, &format, &length, &after, &data);
810 if ((type == XA_PIXMAP) && (format == 32) && (length == 1)) {
811 XSetErrorHandler(dummyErrorHandler);
812 XKillClient(dpy, *((Pixmap *)data));
813 XSync(dpy, False);
814 XSetErrorHandler(NULL);
815 mode = PropModeReplace;
816 } else {
817 mode = PropModeAppend;
819 if (pixmap)
820 XChangeProperty(dpy, root, prop, XA_PIXMAP, 32, mode,
821 (unsigned char *) &pixmap, 1);
822 else
823 XDeleteProperty(dpy, root, prop);
826 XUngrabServer(dpy);
827 XFlush(dpy);
832 void
833 changeTexture(BackgroundTexture *texture)
835 if (!texture)
836 return;
838 if (texture->solid) {
839 XSetWindowBackground(dpy, root, texture->color.pixel);
840 } else {
841 XSetWindowBackgroundPixmap(dpy, root, texture->pixmap);
843 XClearWindow(dpy, root);
845 XSync(dpy, False);
848 Pixmap pixmap;
850 pixmap = duplicatePixmap(texture->pixmap, texture->width,
851 texture->height);
853 setPixmapProperty(pixmap);
859 readmsg(int fd, unsigned char *buffer, int size)
861 int count;
863 count = 0;
864 while (size>0) {
865 count = read(fd, buffer, size);
866 if (count < 0)
867 return -1;
868 size -= count;
869 buffer += count;
870 *buffer = 0;
873 return size;
878 * Message Format:
879 * sizeSntexture_spec - sets the texture for workspace n
880 * sizeCn - change background texture to the one for workspace n
881 * sizePpath - set the pixmap search path
883 * n is 4 bytes
884 * size = 4 bytes for length of the message data
886 void
887 helperLoop(RContext *rc)
889 BackgroundTexture *textures[WORKSPACE_COUNT];
890 int maxTextures = 0;
891 unsigned char buffer[2048], buf[8];
892 int size;
893 int errcount = 4;
895 memset(textures, 0, WORKSPACE_COUNT*sizeof(BackgroundTexture*));
898 while (1) {
899 int workspace;
901 /* get length of message */
902 if (readmsg(0, buffer, 4) < 0) {
903 wsyserror("error reading message from Window Maker");
904 errcount--;
905 if (errcount == 0) {
906 wfatal("quitting");
907 exit(1);
909 continue;
911 memcpy(buf, buffer, 4);
912 buf[4] = 0;
913 size = atoi(buf);
915 /* get message */
916 if (readmsg(0, buffer, size) < 0) {
917 wsyserror("error reading message from Window Maker");
918 errcount--;
919 if (errcount == 0) {
920 wfatal("quitting");
921 exit(1);
923 continue;
925 #ifdef DEBUG
926 printf("RECEIVED %s\n",buffer);
927 #endif
928 if (buffer[0]!='P' && buffer[0]!='K') {
929 memcpy(buf, &buffer[1], 4);
930 buf[4] = 0;
931 workspace = atoi(buf);
932 if (workspace < 0 || workspace >= WORKSPACE_COUNT) {
933 wwarning("received message with invalid workspace number %i\n",
934 workspace);
935 continue;
939 switch (buffer[0]) {
940 case 'S':
941 #ifdef DEBUG
942 printf("set texture %s\n", &buffer[5]);
943 #endif
944 setupTexture(rc, textures, &maxTextures, workspace, &buffer[5]);
945 break;
947 case 'C':
948 #ifdef DEBUG
949 printf("change texture %i\n", workspace);
950 #endif
951 if (!textures[workspace])
952 changeTexture(textures[0]);
953 else
954 changeTexture(textures[workspace]);
955 break;
957 case 'P':
958 #ifdef DEBUG
959 printf("change pixmappath %s\n", &buffer[1]);
960 #endif
961 if (PixmapPath)
962 free(PixmapPath);
963 PixmapPath = wstrdup(&buffer[1]);
964 break;
966 case 'U':
967 #ifdef DEBUG
968 printf("unset workspace %i\n", workspace);
969 #endif
970 setupTexture(rc, textures, &maxTextures, workspace, NULL);
971 break;
973 case 'K':
974 #ifdef DEBUG
975 printf("exit command\n");
976 #endif
977 exit(0);
979 default:
980 wwarning("unknown message received");
981 break;
987 void
988 updateDomain(char *domain, char *key, char *texture)
990 char *program = "wdwrite";
992 execlp(program, program, domain, key, texture, NULL);
993 wwarning("warning could not run \"%s\"", program);
998 char*
999 globalDefaultsPathForDomain(char *domain)
1001 char path[1024];
1003 sprintf(path, "%s/%s", SYSCONFDIR, domain);
1005 return wstrdup(path);
1009 proplist_t
1010 getValueForKey(char *domain, char *keyName)
1012 char *path;
1013 proplist_t key;
1014 proplist_t d;
1015 proplist_t val;
1017 key = PLMakeString(keyName);
1019 /* try to find PixmapPath in user defaults */
1020 path = wdefaultspathfordomain(domain);
1021 d = PLGetProplistWithPath(path);
1022 if (!d) {
1023 wwarning("could not open domain file %s", path);
1025 free(path);
1027 if (d && !PLIsDictionary(d)) {
1028 PLRelease(d);
1029 d = NULL;
1031 if (d) {
1032 val = PLGetDictionaryEntry(d, key);
1033 } else {
1034 val = NULL;
1036 /* try to find PixmapPath in global defaults */
1037 if (!val) {
1038 path = globalDefaultsPathForDomain(domain);
1039 if (!path) {
1040 wwarning("could not locate file for domain %s", domain);
1041 d = NULL;
1042 } else {
1043 d = PLGetProplistWithPath(path);
1044 free(path);
1047 if (d && !PLIsDictionary(d)) {
1048 PLRelease(d);
1049 d = NULL;
1051 if (d) {
1052 val = PLGetDictionaryEntry(d, key);
1054 } else {
1055 val = NULL;
1059 if (val)
1060 PLRetain(val);
1062 PLRelease(key);
1063 if (d)
1064 PLRelease(d);
1066 return val;
1069 char*
1070 getPixmapPath(char *domain)
1072 proplist_t val;
1073 char *ptr, *data;
1074 int len, i, count;
1076 val = getValueForKey(domain, "PixmapPath");
1078 if (!val || !PLIsArray(val)) {
1079 if (val)
1080 PLRelease(val);
1081 return wstrdup("");
1084 count = PLGetNumberOfElements(val);
1085 len = 0;
1086 for (i=0; i<count; i++) {
1087 proplist_t v;
1089 v = PLGetArrayElement(val, i);
1090 if (!v || !PLIsString(v)) {
1091 continue;
1093 len += strlen(PLGetString(v))+1;
1096 ptr = data = wmalloc(len+1);
1097 *ptr = 0;
1099 for (i=0; i<count; i++) {
1100 proplist_t v;
1102 v = PLGetArrayElement(val, i);
1103 if (!v || !PLIsString(v)) {
1104 continue;
1106 strcpy(ptr, PLGetString(v));
1108 ptr += strlen(PLGetString(v));
1109 *ptr = ':';
1110 ptr++;
1112 if (i>0)
1113 ptr--; *(ptr--) = 0;
1115 PLRelease(val);
1117 return data;
1121 void
1122 wAbort()
1124 wfatal("aborting");
1125 exit(1);
1130 void
1131 print_help(char *ProgName)
1133 printf("Usage: %s [options] [image]\n", ProgName);
1134 puts("Sets the workspace background to the specified image or a texture and optionally update Window Maker configuration");
1135 puts("");
1136 #define P(m) puts(m)
1137 P(" -display display to use");
1138 P(" -d, --dither dither image");
1139 P(" -m, --match match colors");
1140 P(" -b, --back-color <color> background color");
1141 P(" -t, --tile tile image");
1142 P(" -e, --center center image");
1143 P(" -s, --scale scale image (default)");
1144 P(" -a, --maxscale scale image and keep aspect ratio");
1145 P(" -u, --update-wmaker update WindowMaker domain database");
1146 P(" -D, --update-domain <domain> update <domain> database");
1147 P(" -c, --colors <cpc> colors per channel to use");
1148 P(" -p, --parse <texture> proplist style texture specification");
1149 P(" -w, --workspace <workspace> update background for the specified workspace");
1150 P(" --version show version of wmsetbg and exit");
1151 P(" --help show this help and exit");
1152 #undef P
1157 void
1158 changeTextureForWorkspace(char *domain, char *texture, int workspace)
1160 proplist_t array;
1161 proplist_t val;
1162 char *value;
1163 int j;
1165 val = PLGetProplistWithDescription(texture);
1166 if (!val) {
1167 wwarning("could not parse texture %s", texture);
1168 return;
1171 array = getValueForKey("WindowMaker", "WorkspaceSpecificBack");
1173 if (!array) {
1174 array = PLMakeArrayFromElements(NULL, NULL);
1177 j = PLGetNumberOfElements(array);
1178 if (workspace >= j) {
1179 proplist_t empty;
1181 empty = PLMakeArrayFromElements(NULL, NULL);
1183 while (j++ < workspace-1) {
1184 PLAppendArrayElement(array, empty);
1186 PLAppendArrayElement(array, val);
1187 } else {
1188 PLRemoveArrayElement(array, workspace);
1189 PLInsertArrayElement(array, val, workspace);
1192 value = PLGetDescription(array);
1193 updateDomain(domain, "WorkspaceSpecificBack", value);
1198 main(int argc, char **argv)
1200 int i;
1201 int helperMode = 0;
1202 RContext *rc;
1203 RContextAttributes rattr;
1204 char *style = "spixmap";
1205 char *back_color = "gray20";
1206 char *image_name = NULL;
1207 char *domain = "WindowMaker";
1208 int update=0, cpc=4, render_mode=RM_DITHER, obey_user=0;
1209 char *texture = NULL;
1210 int workspace = -1;
1212 signal(SIGINT, SIG_DFL);
1213 signal(SIGTERM, SIG_DFL);
1214 signal(SIGQUIT, SIG_DFL);
1215 signal(SIGSEGV, SIG_DFL);
1216 signal(SIGBUS, SIG_DFL);
1217 signal(SIGFPE, SIG_DFL);
1218 signal(SIGABRT, SIG_DFL);
1219 signal(SIGHUP, SIG_DFL);
1220 signal(SIGPIPE, SIG_DFL);
1221 signal(SIGCHLD, SIG_DFL);
1223 WMInitializeApplication("wmsetbg", &argc, argv);
1225 for (i=1; i<argc; i++) {
1226 if (strcmp(argv[i], "-helper")==0) {
1227 helperMode = 1;
1228 } else if (strcmp(argv[i], "-display")==0) {
1229 i++;
1230 if (i>=argc) {
1231 wfatal("too few arguments for %s\n", argv[i-1]);
1232 exit(1);
1234 display = argv[i];
1235 } else if (strcmp(argv[i], "-s")==0
1236 || strcmp(argv[i], "--scale")==0) {
1237 style = "spixmap";
1238 } else if (strcmp(argv[i], "-t")==0
1239 || strcmp(argv[i], "--tile")==0) {
1240 style = "tpixmap";
1241 } else if (strcmp(argv[i], "-e")==0
1242 || strcmp(argv[i], "--center")==0) {
1243 style = "cpixmap";
1244 } else if (strcmp(argv[i], "-a")==0
1245 || strcmp(argv[i], "--maxscale")==0) {
1246 style = "mpixmap";
1247 } else if (strcmp(argv[i], "-d")==0
1248 || strcmp(argv[i], "--dither")==0) {
1249 render_mode = RM_DITHER;
1250 obey_user++;
1251 } else if (strcmp(argv[i], "-m")==0
1252 || strcmp(argv[i], "--match")==0) {
1253 render_mode = RM_MATCH;
1254 obey_user++;
1255 } else if (strcmp(argv[i], "-u")==0
1256 || strcmp(argv[i], "--update-wmaker")==0) {
1257 update++;
1258 } else if (strcmp(argv[i], "-D")==0
1259 || strcmp(argv[i], "--update-domain")==0) {
1260 update++;
1261 i++;
1262 if (i>=argc) {
1263 wfatal("too few arguments for %s\n", argv[i-1]);
1264 exit(1);
1266 domain = wstrdup(argv[i]);
1267 } else if (strcmp(argv[i], "-c")==0
1268 || strcmp(argv[i], "--colors")==0) {
1269 i++;
1270 if (i>=argc) {
1271 wfatal("too few arguments for %s\n", argv[i-1]);
1272 exit(1);
1274 if (sscanf(argv[i], "%i", &cpc)!=1) {
1275 wfatal("bad value for colors per channel: \"%s\"\n", argv[i]);
1276 exit(1);
1278 } else if (strcmp(argv[i], "-b")==0
1279 || strcmp(argv[i], "--back-color")==0) {
1280 i++;
1281 if (i>=argc) {
1282 wfatal("too few arguments for %s\n", argv[i-1]);
1283 exit(1);
1285 back_color = argv[i];
1286 } else if (strcmp(argv[i], "-p")==0
1287 || strcmp(argv[i], "--parse")==0) {
1288 i++;
1289 if (i>=argc) {
1290 wfatal("too few arguments for %s\n", argv[i-1]);
1291 exit(1);
1293 texture = argv[i];
1294 } else if (strcmp(argv[i], "-w")==0
1295 || strcmp(argv[i], "--workspace")==0) {
1296 i++;
1297 if (i>=argc) {
1298 wfatal("too few arguments for %s\n", argv[i-1]);
1299 exit(1);
1301 if (sscanf(argv[i], "%i", &workspace)!=1) {
1302 wfatal("bad value for workspace number: \"%s\"",
1303 argv[i]);
1304 exit(1);
1306 } else if (strcmp(argv[i], "--version")==0) {
1308 printf(PROG_VERSION);
1309 exit(0);
1311 } else if (strcmp(argv[i], "--help")==0) {
1312 print_help(argv[0]);
1313 exit(0);
1314 } else if (argv[i][0] != '-') {
1315 image_name = argv[i];
1316 } else {
1317 printf("%s: invalid argument '%s'\n", argv[0], argv[i]);
1318 printf("Try '%s --help' for more information\n", argv[0]);
1319 exit(1);
1322 if (!image_name && !texture && !helperMode) {
1323 printf("%s: you must specify a image file name or a texture\n",
1324 argv[0]);
1325 printf("Try '%s --help' for more information\n", argv[0]);
1326 exit(1);
1330 PixmapPath = getPixmapPath(domain);
1332 dpy = XOpenDisplay(display);
1333 if (!dpy) {
1334 wfatal("could not open display");
1335 exit(1);
1337 #if 0
1338 XSynchronize(dpy, 1);
1339 #endif
1341 root = DefaultRootWindow(dpy);
1343 scr = DefaultScreen(dpy);
1345 scrWidth = WidthOfScreen(DefaultScreenOfDisplay(dpy));
1346 scrHeight = HeightOfScreen(DefaultScreenOfDisplay(dpy));
1348 if (!obey_user && DefaultDepth(dpy, scr) <= 8)
1349 render_mode = RM_DITHER;
1351 rattr.flags = RC_RenderMode | RC_ColorsPerChannel | RC_DefaultVisual;
1352 rattr.render_mode = render_mode;
1353 rattr.colors_per_channel = cpc;
1355 rc = RCreateContext(dpy, scr, &rattr);
1357 if (helperMode) {
1358 /* lower priority, so that it wont use all the CPU */
1359 nice(1000);
1361 helperLoop(rc);
1362 } else {
1363 BackgroundTexture *tex;
1364 char buffer[4098];
1366 if (!texture) {
1367 sprintf(buffer, "(%s, \"%s\", %s)", style, image_name, back_color);
1368 texture = (char*)buffer;
1371 if (update && workspace < 0) {
1372 updateDomain(domain, "WorkspaceBack", texture);
1375 tex = parseTexture(rc, texture);
1376 if (!tex)
1377 exit(1);
1379 if (workspace<0)
1380 changeTexture(tex);
1381 else {
1382 /* always update domain */
1383 changeTextureForWorkspace(domain, texture, workspace-1);
1387 return 0;