updated po files, fixed some bugs
[wmaker-crm.git] / util / wmsetbg.c
blob3c0cfcdc27b8de9f36379ae4db72399648f1d325
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.h>
50 #include <wraster.h>
52 #include <proplist.h>
54 #define PROG_VERSION "wmsetbg (Window Maker) 2.5"
57 #define WORKSPACE_COUNT (MAX_WORKSPACES+1)
60 Display *dpy;
61 char *display = "";
62 Window root;
63 int scr;
64 int scrWidth;
65 int scrHeight;
68 Bool smooth = False;
71 Pixmap CurrentPixmap = None;
72 char *PixmapPath = NULL;
75 extern Pixmap LoadJPEG(RContext *rc, char *file_name, int *width, int *height);
78 typedef struct BackgroundTexture {
79 int refcount;
81 int solid;
83 char *spec;
85 XColor color;
86 Pixmap pixmap; /* for all textures, including solid */
87 int width; /* size of the pixmap */
88 int height;
89 } BackgroundTexture;
93 RImage*
94 loadImage(RContext *rc, char *file)
96 char *path;
97 RImage *image;
99 if (access(file, F_OK)!=0) {
100 path = wfindfile(PixmapPath, file);
101 if (!path) {
102 wwarning("%s:could not find image file used in texture", file);
103 return NULL;
105 } else {
106 path = wstrdup(file);
109 image = RLoadImage(rc, path, 0);
110 if (!image) {
111 wwarning("%s:could not load image file used in texture:%s", path,
112 RMessageForError(RErrorCode));
114 free(path);
116 return image;
120 BackgroundTexture*
121 parseTexture(RContext *rc, char *text)
123 BackgroundTexture *texture = NULL;
124 proplist_t texarray;
125 proplist_t val;
126 int count;
127 char *tmp;
128 char *type;
130 #define GETSTRORGOTO(val, str, i, label) \
131 val = PLGetArrayElement(texarray, i);\
132 if (!PLIsString(val)) {\
133 wwarning("could not parse texture %s", text);\
134 goto label;\
136 str = PLGetString(val)
138 texarray = PLGetProplistWithDescription(text);
139 if (!texarray || !PLIsArray(texarray)
140 || (count = PLGetNumberOfElements(texarray)) < 2) {
142 wwarning("could not parse texture %s", text);
143 if (texarray)
144 PLRelease(texarray);
145 return NULL;
148 texture = wmalloc(sizeof(BackgroundTexture));
149 memset(texture, 0, sizeof(BackgroundTexture));
151 GETSTRORGOTO(val, type, 0, error);
153 if (strcasecmp(type, "solid")==0) {
154 XColor color;
155 Pixmap pixmap;
157 texture->solid = 1;
159 GETSTRORGOTO(val, tmp, 1, error);
161 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
162 wwarning("could not parse color %s in texture %s", tmp, text);
163 goto error;
165 XAllocColor(dpy, DefaultColormap(dpy, scr), &color);
167 pixmap = XCreatePixmap(dpy, root, 8, 8, DefaultDepth(dpy, scr));
168 XSetForeground(dpy, DefaultGC(dpy, scr), color.pixel);
169 XFillRectangle(dpy, pixmap, DefaultGC(dpy, scr), 0, 0, 8, 8);
171 texture->pixmap = pixmap;
172 texture->color = color;
173 texture->width = 8;
174 texture->height = 8;
175 } else if (strcasecmp(type, "vgradient")==0
176 || strcasecmp(type, "dgradient")==0
177 || strcasecmp(type, "hgradient")==0) {
178 XColor color;
179 RColor color1, color2;
180 RImage *image;
181 Pixmap pixmap;
182 int gtype;
183 int iwidth, iheight;
185 GETSTRORGOTO(val, tmp, 1, error);
187 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
188 wwarning("could not parse color %s in texture %s", tmp, text);
189 goto error;
192 color1.red = color.red >> 8;
193 color1.green = color.green >> 8;
194 color1.blue = color.blue >> 8;
196 GETSTRORGOTO(val, tmp, 2, error);
198 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
199 wwarning("could not parse color %s in texture %s", tmp, text);
200 goto error;
203 color2.red = color.red >> 8;
204 color2.green = color.green >> 8;
205 color2.blue = color.blue >> 8;
207 switch (type[0]) {
208 case 'h':
209 case 'H':
210 gtype = RHorizontalGradient;
211 iwidth = scrWidth;
212 iheight = 8;
213 break;
214 case 'V':
215 case 'v':
216 gtype = RVerticalGradient;
217 iwidth = 8;
218 iheight = scrHeight;
219 break;
220 default:
221 gtype = RDiagonalGradient;
222 iwidth = scrWidth;
223 iheight = scrHeight;
224 break;
227 image = RRenderGradient(iwidth, iheight, &color1, &color2, gtype);
229 if (!image) {
230 wwarning("could not render gradient texture:%s",
231 RMessageForError(RErrorCode));
232 goto error;
235 if (!RConvertImage(rc, image, &pixmap)) {
236 wwarning("could not convert texture:%s",
237 RMessageForError(RErrorCode));
238 RDestroyImage(image);
239 goto error;
242 texture->width = image->width;
243 texture->height = image->height;
244 RDestroyImage(image);
246 texture->pixmap = pixmap;
247 } else if (strcasecmp(type, "mvgradient")==0
248 || strcasecmp(type, "mdgradient")==0
249 || strcasecmp(type, "mhgradient")==0) {
250 XColor color;
251 RColor **colors;
252 RImage *image;
253 Pixmap pixmap;
254 int i, j;
255 int gtype;
256 int iwidth, iheight;
258 colors = malloc(sizeof(RColor*)*(count-1));
259 if (!colors) {
260 wwarning("out of memory while parsing texture");
261 goto error;
263 memset(colors, 0, sizeof(RColor*)*(count-1));
265 for (i = 2; i < count; i++) {
266 val = PLGetArrayElement(texarray, i);
267 if (!PLIsString(val)) {
268 wwarning("could not parse texture %s", text);
270 for (j = 0; colors[j]!=NULL; j++)
271 free(colors[j]);
272 free(colors);
273 goto error;
275 tmp = PLGetString(val);
277 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
278 wwarning("could not parse color %s in texture %s",
279 tmp, text);
281 for (j = 0; colors[j]!=NULL; j++)
282 free(colors[j]);
283 free(colors);
284 goto error;
286 if (!(colors[i-2] = malloc(sizeof(RColor)))) {
287 wwarning("out of memory while parsing texture");
289 for (j = 0; colors[j]!=NULL; j++)
290 free(colors[j]);
291 free(colors);
292 goto error;
295 colors[i-2]->red = color.red >> 8;
296 colors[i-2]->green = color.green >> 8;
297 colors[i-2]->blue = color.blue >> 8;
300 switch (type[1]) {
301 case 'h':
302 case 'H':
303 gtype = RHorizontalGradient;
304 iwidth = scrWidth;
305 iheight = 8;
306 break;
307 case 'V':
308 case 'v':
309 gtype = RVerticalGradient;
310 iwidth = 8;
311 iheight = scrHeight;
312 break;
313 default:
314 gtype = RDiagonalGradient;
315 iwidth = scrWidth;
316 iheight = scrHeight;
317 break;
320 image = RRenderMultiGradient(iwidth, iheight, colors, gtype);
322 for (j = 0; colors[j]!=NULL; j++)
323 free(colors[j]);
324 free(colors);
326 if (!image) {
327 wwarning("could not render gradient texture:%s",
328 RMessageForError(RErrorCode));
329 goto error;
332 if (!RConvertImage(rc, image, &pixmap)) {
333 wwarning("could not convert texture:%s",
334 RMessageForError(RErrorCode));
335 RDestroyImage(image);
336 goto error;
339 texture->width = image->width;
340 texture->height = image->height;
341 RDestroyImage(image);
343 texture->pixmap = pixmap;
344 } else if (strcasecmp(type, "cpixmap")==0
345 || strcasecmp(type, "spixmap")==0
346 || strcasecmp(type, "mpixmap")==0
347 || strcasecmp(type, "tpixmap")==0) {
348 XColor color;
349 Pixmap pixmap = None;
350 RImage *image = NULL;
351 int w, h;
352 int iwidth, iheight;
353 RColor rcolor;
356 GETSTRORGOTO(val, tmp, 1, error);
358 if (toupper(type[0]) == 'T' || toupper(type[0]) == 'C')
359 pixmap = LoadJPEG(rc, tmp, &iwidth, &iheight);
362 if (!pixmap) {
363 image = loadImage(rc, tmp);
364 if (!image) {
365 goto error;
367 iwidth = image->width;
368 iheight = image->height;
371 GETSTRORGOTO(val, tmp, 2, error);
373 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
374 wwarning("could not parse color %s in texture %s\n", tmp, text);
375 RDestroyImage(image);
376 goto error;
378 if (!XAllocColor(dpy, DefaultColormap(dpy, scr), &color)) {
379 rcolor.red = color.red >> 8;
380 rcolor.green = color.green >> 8;
381 rcolor.blue = color.blue >> 8;
382 RGetClosestXColor(rc, &rcolor, &color);
383 } else {
384 rcolor.red = 0;
385 rcolor.green = 0;
386 rcolor.blue = 0;
388 /* for images with a transparent color */
389 if (image->data[3]) {
390 RCombineImageWithColor(image, &rcolor);
393 switch (toupper(type[0])) {
394 case 'T':
395 texture->width = iwidth;
396 texture->height = iheight;
397 if (!pixmap && !RConvertImage(rc, image, &pixmap)) {
398 wwarning("could not convert texture:%s",
399 RMessageForError(RErrorCode));
400 RDestroyImage(image);
401 goto error;
403 if (image)
404 RDestroyImage(image);
405 break;
406 case 'S':
407 case 'M':
408 if (toupper(type[0])=='S') {
409 w = scrWidth;
410 h = scrHeight;
411 } else {
412 if (iwidth*scrHeight > iheight*scrWidth) {
413 w = scrWidth;
414 h = (scrWidth*iheight)/iwidth;
415 } else {
416 h = scrHeight;
417 w = (scrHeight*iwidth)/iheight;
421 RImage *simage;
423 if (smooth)
424 simage = RSmoothScaleImage(image, w, h);
425 else
426 simage = RScaleImage(image, w, h);
427 if (!simage) {
428 wwarning("could not scale image:%s",
429 RMessageForError(RErrorCode));
430 RDestroyImage(image);
431 goto error;
433 RDestroyImage(image);
434 image = simage;
435 iwidth = image->width;
436 iheight = image->height;
438 /* fall through */
439 case 'C':
441 Pixmap cpixmap;
443 if (!pixmap && !RConvertImage(rc, image, &pixmap)) {
444 wwarning("could not convert texture:%s",
445 RMessageForError(RErrorCode));
446 RDestroyImage(image);
447 goto error;
450 if (iwidth != scrWidth || iheight != scrHeight) {
451 int x, y, sx, sy, w, h;
453 cpixmap = XCreatePixmap(dpy, root, scrWidth, scrHeight,
454 DefaultDepth(dpy, scr));
456 XSetForeground(dpy, DefaultGC(dpy, scr), color.pixel);
457 XFillRectangle(dpy, cpixmap, DefaultGC(dpy, scr),
458 0, 0, scrWidth, scrHeight);
460 if (iheight < scrHeight) {
461 h = iheight;
462 y = (scrHeight - h)/2;
463 sy = 0;
464 } else {
465 sy = (iheight - scrHeight)/2;
466 y = 0;
467 h = scrHeight;
469 if (iwidth < scrWidth) {
470 w = iwidth;
471 x = (scrWidth - w)/2;
472 sx = 0;
473 } else {
474 sx = (iwidth - scrWidth)/2;
475 x = 0;
476 w = scrWidth;
479 XCopyArea(dpy, pixmap, cpixmap, DefaultGC(dpy, scr),
480 sx, sy, w, h, x, y);
481 XFreePixmap(dpy, pixmap);
482 pixmap = cpixmap;
484 if (image)
485 RDestroyImage(image);
487 texture->width = scrWidth;
488 texture->height = scrHeight;
490 break;
493 texture->pixmap = pixmap;
494 texture->color = color;
495 } else if (strcasecmp(type, "thgradient")==0
496 || strcasecmp(type, "tvgradient")==0
497 || strcasecmp(type, "tdgradient")==0) {
498 XColor color;
499 RColor color1, color2;
500 RImage *image;
501 RImage *gradient;
502 RImage *tiled;
503 Pixmap pixmap;
504 int opaq;
505 char *file;
506 int gtype;
507 int twidth, theight;
509 GETSTRORGOTO(val, file, 1, error);
511 GETSTRORGOTO(val, tmp, 2, error);
513 opaq = atoi(tmp);
515 GETSTRORGOTO(val, tmp, 3, error);
517 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
518 wwarning("could not parse color %s in texture %s", tmp, text);
519 goto error;
522 color1.red = color.red >> 8;
523 color1.green = color.green >> 8;
524 color1.blue = color.blue >> 8;
526 GETSTRORGOTO(val, tmp, 4, error);
528 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
529 wwarning("could not parse color %s in texture %s", tmp, text);
530 goto error;
533 color2.red = color.red >> 8;
534 color2.green = color.green >> 8;
535 color2.blue = color.blue >> 8;
537 image = loadImage(rc, file);
538 if (!image) {
539 goto error;
542 switch (type[1]) {
543 case 'h':
544 case 'H':
545 gtype = RHorizontalGradient;
546 twidth = scrWidth;
547 theight = image->height > scrHeight ? scrHeight : image->height;
548 break;
549 case 'V':
550 case 'v':
551 gtype = RVerticalGradient;
552 twidth = image->width > scrWidth ? scrWidth : image->width;
553 theight = scrHeight;
554 break;
555 default:
556 gtype = RDiagonalGradient;
557 twidth = scrWidth;
558 theight = scrHeight;
559 break;
561 gradient = RRenderGradient(twidth, theight, &color1, &color2, gtype);
563 if (!gradient) {
564 wwarning("could not render texture:%s",
565 RMessageForError(RErrorCode));
566 RDestroyImage(gradient);
567 RDestroyImage(image);
568 goto error;
571 tiled = RMakeTiledImage(image, twidth, theight);
572 if (!tiled) {
573 wwarning("could not render texture:%s",
574 RMessageForError(RErrorCode));
575 RDestroyImage(gradient);
576 RDestroyImage(image);
577 goto error;
579 RDestroyImage(image);
581 RCombineImagesWithOpaqueness(tiled, gradient, opaq);
582 RDestroyImage(gradient);
584 if (!RConvertImage(rc, tiled, &pixmap)) {
585 wwarning("could not convert texture:%s",
586 RMessageForError(RErrorCode));
587 RDestroyImage(tiled);
588 goto error;
590 texture->width = tiled->width;
591 texture->height = tiled->height;
593 RDestroyImage(tiled);
595 texture->pixmap = pixmap;
596 } else if (strcasecmp(type, "function")==0) {
597 #ifdef HAVE_DLFCN_H
598 void (*initFunc) (Display*, Colormap);
599 RImage* (*mainFunc) (int, char**, int, int, int);
600 Pixmap pixmap;
601 RImage *image = 0;
602 int success = 0;
603 char *lib, *func, **argv = 0;
604 void *handle = 0;
605 int i, argc;
607 if (count < 3)
608 goto function_cleanup;
610 /* get the library name */
611 GETSTRORGOTO(val, lib, 1, function_cleanup);
613 /* get the function name */
614 GETSTRORGOTO(val, func, 2, function_cleanup);
616 argc = count - 2;
617 argv = (char**)wmalloc(argc * sizeof(char*));
619 /* get the parameters */
620 argv[0] = func;
621 for (i=0; i<argc-1; i++) {
622 GETSTRORGOTO(val, tmp, 3+i, function_cleanup);
623 argv[i+1] = wstrdup(tmp);
626 handle = dlopen(lib, RTLD_LAZY);
627 if (!handle) {
628 wwarning("could not find library %s", lib);
629 goto function_cleanup;
632 initFunc = dlsym(handle, "initWindowMaker");
633 if (!initFunc) {
634 wwarning("could not initialize library %s", lib);
635 goto function_cleanup;
637 initFunc(dpy, DefaultColormap(dpy, scr));
639 mainFunc = dlsym(handle, func);
640 if (!mainFunc) {
641 wwarning("could not find function %s::%s", lib, func);
642 goto function_cleanup;
644 image = mainFunc(argc, argv, scrWidth, scrHeight, 0);
646 if (!RConvertImage(rc, image, &pixmap)) {
647 wwarning("could not convert texture:%s",
648 RMessageForError(RErrorCode));
649 goto function_cleanup;
651 texture->width = scrWidth;
652 texture->height = scrHeight;
653 texture->pixmap = pixmap;
654 success = 1;
656 function_cleanup:
657 if (argv) {
658 int i;
659 for (i=0; i<argc; i++) {
660 free(argv[i]);
663 if (handle) {
664 dlclose(handle);
666 if (image) {
667 RDestroyImage(image);
669 if (!success) {
670 goto error;
672 #else
673 wwarning("function textures not supported");
674 goto error;
675 #endif
676 } else {
677 wwarning("invalid texture type %s", text);
678 goto error;
681 texture->spec = wstrdup(text);
683 return texture;
685 error:
686 if (texture)
687 free(texture);
688 if (texarray)
689 PLRelease(texarray);
691 return NULL;
695 void
696 freeTexture(BackgroundTexture *texture)
698 if (texture->solid) {
699 long pixel[1];
701 pixel[0] = texture->color.pixel;
702 /* dont free black/white pixels */
703 if (pixel[0]!=BlackPixelOfScreen(DefaultScreenOfDisplay(dpy))
704 && pixel[0]!=WhitePixelOfScreen(DefaultScreenOfDisplay(dpy)))
705 XFreeColors(dpy, DefaultColormap(dpy, scr), pixel, 1, 0);
707 if (texture->pixmap) {
708 XFreePixmap(dpy, texture->pixmap);
710 free(texture->spec);
711 free(texture);
715 void
716 setupTexture(RContext *rc, BackgroundTexture **textures, int *maxTextures,
717 int workspace, char *texture)
719 BackgroundTexture *newTexture = NULL;
720 int i;
722 /* unset the texture */
723 if (!texture) {
724 if (textures[workspace]!=NULL) {
725 textures[workspace]->refcount--;
727 if (textures[workspace]->refcount == 0)
728 freeTexture(textures[workspace]);
730 textures[workspace] = NULL;
731 return;
734 if (textures[workspace]
735 && strcasecmp(textures[workspace]->spec, texture)==0) {
736 /* texture did not change */
737 return;
740 /* check if the same texture is already created */
741 for (i = 0; i < *maxTextures; i++) {
742 if (textures[i] && strcasecmp(textures[i]->spec, texture)==0) {
743 newTexture = textures[i];
744 break;
748 if (!newTexture) {
749 /* create the texture */
750 newTexture = parseTexture(rc, texture);
752 if (!newTexture)
753 return;
755 if (textures[workspace]!=NULL) {
757 textures[workspace]->refcount--;
759 if (textures[workspace]->refcount == 0)
760 freeTexture(textures[workspace]);
763 newTexture->refcount++;
764 textures[workspace] = newTexture;
766 if (*maxTextures < workspace)
767 *maxTextures = workspace;
772 Pixmap
773 duplicatePixmap(Pixmap pixmap, int width, int height)
775 Display *tmpDpy;
776 Pixmap copyP;
778 /* must open a new display or the RetainPermanent will
779 * leave stuff allocated in RContext unallocated after exit */
780 tmpDpy = XOpenDisplay(display);
781 if (!tmpDpy) {
782 wwarning("could not open display to update background image information");
784 return None;
785 } else {
786 XSync(dpy, False);
788 copyP = XCreatePixmap(tmpDpy, root, width, height,
789 DefaultDepth(tmpDpy, scr));
790 XCopyArea(tmpDpy, pixmap, copyP, DefaultGC(tmpDpy, scr),
791 0, 0, width, height, 0, 0);
792 XSync(tmpDpy, False);
794 XSetCloseDownMode(tmpDpy, RetainPermanent);
795 XCloseDisplay(tmpDpy);
798 return copyP;
802 static int
803 dummyErrorHandler(Display *dpy, XErrorEvent *err)
805 return 0;
808 void
809 setPixmapProperty(Pixmap pixmap)
811 static Atom prop = 0;
812 Atom type;
813 int format;
814 unsigned long length, after;
815 unsigned char *data;
816 int mode;
818 if (!prop) {
819 prop = XInternAtom(dpy, "_XROOTPMAP_ID", False);
822 XGrabServer(dpy);
824 /* Clear out the old pixmap */
825 XGetWindowProperty(dpy, root, prop, 0L, 1L, False, AnyPropertyType,
826 &type, &format, &length, &after, &data);
828 if ((type == XA_PIXMAP) && (format == 32) && (length == 1)) {
829 XSetErrorHandler(dummyErrorHandler);
830 XKillClient(dpy, *((Pixmap *)data));
831 XSync(dpy, False);
832 XSetErrorHandler(NULL);
833 mode = PropModeReplace;
834 } else {
835 mode = PropModeAppend;
837 if (pixmap)
838 XChangeProperty(dpy, root, prop, XA_PIXMAP, 32, mode,
839 (unsigned char *) &pixmap, 1);
840 else
841 XDeleteProperty(dpy, root, prop);
844 XUngrabServer(dpy);
845 XFlush(dpy);
850 void
851 changeTexture(BackgroundTexture *texture)
853 if (!texture) {
854 return;
857 if (texture->solid) {
858 XSetWindowBackground(dpy, root, texture->color.pixel);
859 } else {
860 XSetWindowBackgroundPixmap(dpy, root, texture->pixmap);
862 XClearWindow(dpy, root);
864 XSync(dpy, False);
867 Pixmap pixmap;
869 pixmap = duplicatePixmap(texture->pixmap, texture->width,
870 texture->height);
872 setPixmapProperty(pixmap);
878 readmsg(int fd, unsigned char *buffer, int size)
880 int count;
882 count = 0;
883 while (size>0) {
884 count = read(fd, buffer, size);
885 if (count < 0)
886 return -1;
887 size -= count;
888 buffer += count;
889 *buffer = 0;
892 return size;
897 * Message Format:
898 * sizeSntexture_spec - sets the texture for workspace n
899 * sizeCn - change background texture to the one for workspace n
900 * sizePpath - set the pixmap search path
902 * n is 4 bytes
903 * size = 4 bytes for length of the message data
905 void
906 helperLoop(RContext *rc)
908 BackgroundTexture *textures[WORKSPACE_COUNT];
909 int maxTextures = 0;
910 unsigned char buffer[2048], buf[8];
911 int size;
912 int errcount = 4;
914 memset(textures, 0, WORKSPACE_COUNT*sizeof(BackgroundTexture*));
917 while (1) {
918 int workspace;
920 /* get length of message */
921 if (readmsg(0, buffer, 4) < 0) {
922 wsyserror("error reading message from Window Maker");
923 errcount--;
924 if (errcount == 0) {
925 wfatal("quitting");
926 exit(1);
928 continue;
930 memcpy(buf, buffer, 4);
931 buf[4] = 0;
932 size = atoi(buf);
934 /* get message */
935 if (readmsg(0, buffer, size) < 0) {
936 wsyserror("error reading message from Window Maker");
937 errcount--;
938 if (errcount == 0) {
939 wfatal("quitting");
940 exit(1);
942 continue;
944 #ifdef DEBUG
945 printf("RECEIVED %s\n",buffer);
946 #endif
947 if (buffer[0]!='P' && buffer[0]!='K') {
948 memcpy(buf, &buffer[1], 4);
949 buf[4] = 0;
950 workspace = atoi(buf);
951 if (workspace < 0 || workspace >= WORKSPACE_COUNT) {
952 wwarning("received message with invalid workspace number %i\n",
953 workspace);
954 continue;
958 switch (buffer[0]) {
959 case 'S':
960 #ifdef DEBUG
961 printf("set texture %s\n", &buffer[5]);
962 #endif
963 setupTexture(rc, textures, &maxTextures, workspace, &buffer[5]);
964 break;
966 case 'C':
967 #ifdef DEBUG
968 printf("change texture %i\n", workspace);
969 #endif
970 if (!textures[workspace]) {
971 changeTexture(textures[0]);
972 } else {
973 changeTexture(textures[workspace]);
975 break;
977 case 'P':
978 #ifdef DEBUG
979 printf("change pixmappath %s\n", &buffer[1]);
980 #endif
981 if (PixmapPath)
982 free(PixmapPath);
983 PixmapPath = wstrdup(&buffer[1]);
984 break;
986 case 'U':
987 #ifdef DEBUG
988 printf("unset workspace %i\n", workspace);
989 #endif
990 setupTexture(rc, textures, &maxTextures, workspace, NULL);
991 break;
993 case 'K':
994 #ifdef DEBUG
995 printf("exit command\n");
996 #endif
997 exit(0);
999 default:
1000 wwarning("unknown message received");
1001 break;
1007 void
1008 updateDomain(char *domain, char *key, char *texture)
1010 char *program = "wdwrite";
1012 system(wstrappend("wdwrite ",
1013 wstrappend(domain, smooth ? " SmoothWorkspaceBack YES"
1014 : " SmoothWorkspaceBack NO")));
1016 execlp(program, program, domain, key, texture, NULL);
1017 wwarning("warning could not run \"%s\"", program);
1022 char*
1023 globalDefaultsPathForDomain(char *domain)
1025 char path[1024];
1027 sprintf(path, "%s/%s", SYSCONFDIR, domain);
1029 return wstrdup(path);
1033 proplist_t
1034 getValueForKey(char *domain, char *keyName)
1036 char *path;
1037 proplist_t key;
1038 proplist_t d;
1039 proplist_t val;
1041 key = PLMakeString(keyName);
1043 /* try to find PixmapPath in user defaults */
1044 path = wdefaultspathfordomain(domain);
1045 d = PLGetProplistWithPath(path);
1046 if (!d) {
1047 wwarning("could not open domain file %s", path);
1049 free(path);
1051 if (d && !PLIsDictionary(d)) {
1052 PLRelease(d);
1053 d = NULL;
1055 if (d) {
1056 val = PLGetDictionaryEntry(d, key);
1057 } else {
1058 val = NULL;
1060 /* try to find PixmapPath in global defaults */
1061 if (!val) {
1062 path = globalDefaultsPathForDomain(domain);
1063 if (!path) {
1064 wwarning("could not locate file for domain %s", domain);
1065 d = NULL;
1066 } else {
1067 d = PLGetProplistWithPath(path);
1068 free(path);
1071 if (d && !PLIsDictionary(d)) {
1072 PLRelease(d);
1073 d = NULL;
1075 if (d) {
1076 val = PLGetDictionaryEntry(d, key);
1078 } else {
1079 val = NULL;
1083 if (val)
1084 PLRetain(val);
1086 PLRelease(key);
1087 if (d)
1088 PLRelease(d);
1090 return val;
1095 char*
1096 getPixmapPath(char *domain)
1098 proplist_t val;
1099 char *ptr, *data;
1100 int len, i, count;
1102 val = getValueForKey(domain, "PixmapPath");
1104 if (!val || !PLIsArray(val)) {
1105 if (val)
1106 PLRelease(val);
1107 return wstrdup("");
1110 count = PLGetNumberOfElements(val);
1111 len = 0;
1112 for (i=0; i<count; i++) {
1113 proplist_t v;
1115 v = PLGetArrayElement(val, i);
1116 if (!v || !PLIsString(v)) {
1117 continue;
1119 len += strlen(PLGetString(v))+1;
1122 ptr = data = wmalloc(len+1);
1123 *ptr = 0;
1125 for (i=0; i<count; i++) {
1126 proplist_t v;
1128 v = PLGetArrayElement(val, i);
1129 if (!v || !PLIsString(v)) {
1130 continue;
1132 strcpy(ptr, PLGetString(v));
1134 ptr += strlen(PLGetString(v));
1135 *ptr = ':';
1136 ptr++;
1138 if (i>0)
1139 ptr--; *(ptr--) = 0;
1141 PLRelease(val);
1143 return data;
1147 void
1148 wAbort()
1150 wfatal("aborting");
1151 exit(1);
1156 void
1157 print_help(char *ProgName)
1159 printf("Usage: %s [options] [image]\n", ProgName);
1160 puts("Sets the workspace background to the specified image or a texture and optionally update Window Maker configuration");
1161 puts("");
1162 #define P(m) puts(m)
1163 P(" -display display to use");
1164 P(" -d, --dither dither image");
1165 P(" -m, --match match colors");
1166 P(" -S, --smooth smooth scaled image");
1167 P(" -b, --back-color <color> background color");
1168 P(" -t, --tile tile image");
1169 P(" -e, --center center image");
1170 P(" -s, --scale scale image (default)");
1171 P(" -a, --maxscale scale image and keep aspect ratio");
1172 P(" -u, --update-wmaker update WindowMaker domain database");
1173 P(" -D, --update-domain <domain> update <domain> database");
1174 P(" -c, --colors <cpc> colors per channel to use");
1175 P(" -p, --parse <texture> proplist style texture specification");
1176 P(" -w, --workspace <workspace> update background for the specified workspace");
1177 P(" --version show version of wmsetbg and exit");
1178 P(" --help show this help and exit");
1179 #undef P
1184 void
1185 changeTextureForWorkspace(char *domain, char *texture, int workspace)
1187 proplist_t array;
1188 proplist_t val;
1189 char *value;
1190 int j;
1192 val = PLGetProplistWithDescription(texture);
1193 if (!val) {
1194 wwarning("could not parse texture %s", texture);
1195 return;
1198 array = getValueForKey("WindowMaker", "WorkspaceSpecificBack");
1200 if (!array) {
1201 array = PLMakeArrayFromElements(NULL, NULL);
1204 j = PLGetNumberOfElements(array);
1205 if (workspace >= j) {
1206 proplist_t empty;
1208 empty = PLMakeArrayFromElements(NULL, NULL);
1210 while (j++ < workspace-1) {
1211 PLAppendArrayElement(array, empty);
1213 PLAppendArrayElement(array, val);
1214 } else {
1215 PLRemoveArrayElement(array, workspace);
1216 PLInsertArrayElement(array, val, workspace);
1219 value = PLGetDescription(array);
1220 updateDomain(domain, "WorkspaceSpecificBack", value);
1225 main(int argc, char **argv)
1227 int i;
1228 int helperMode = 0;
1229 RContext *rc;
1230 RContextAttributes rattr;
1231 char *style = "spixmap";
1232 char *back_color = "gray20";
1233 char *image_name = NULL;
1234 char *domain = "WindowMaker";
1235 int update=0, cpc=4, render_mode=RDitheredRendering, obey_user=0;
1236 char *texture = NULL;
1237 int workspace = -1;
1239 signal(SIGINT, SIG_DFL);
1240 signal(SIGTERM, SIG_DFL);
1241 signal(SIGQUIT, SIG_DFL);
1242 signal(SIGSEGV, SIG_DFL);
1243 signal(SIGBUS, SIG_DFL);
1244 signal(SIGFPE, SIG_DFL);
1245 signal(SIGABRT, SIG_DFL);
1246 signal(SIGHUP, SIG_DFL);
1247 signal(SIGPIPE, SIG_DFL);
1248 signal(SIGCHLD, SIG_DFL);
1250 WMInitializeApplication("wmsetbg", &argc, argv);
1252 for (i=1; i<argc; i++) {
1253 if (strcmp(argv[i], "-helper")==0) {
1254 helperMode = 1;
1255 } else if (strcmp(argv[i], "-display")==0) {
1256 i++;
1257 if (i>=argc) {
1258 wfatal("too few arguments for %s\n", argv[i-1]);
1259 exit(1);
1261 display = argv[i];
1262 } else if (strcmp(argv[i], "-s")==0
1263 || strcmp(argv[i], "--scale")==0) {
1264 style = "spixmap";
1265 } else if (strcmp(argv[i], "-t")==0
1266 || strcmp(argv[i], "--tile")==0) {
1267 style = "tpixmap";
1268 } else if (strcmp(argv[i], "-e")==0
1269 || strcmp(argv[i], "--center")==0) {
1270 style = "cpixmap";
1271 } else if (strcmp(argv[i], "-a")==0
1272 || strcmp(argv[i], "--maxscale")==0) {
1273 style = "mpixmap";
1274 } else if (strcmp(argv[i], "-d")==0
1275 || strcmp(argv[i], "--dither")==0) {
1276 render_mode = RDitheredRendering;
1277 obey_user++;
1278 } else if (strcmp(argv[i], "-m")==0
1279 || strcmp(argv[i], "--match")==0) {
1280 render_mode = RBestMatchRendering;
1281 obey_user++;
1282 } else if (strcmp(argv[i], "-S")==0
1283 || strcmp(argv[i], "--smooth")==0) {
1284 smooth = True;
1285 } else if (strcmp(argv[i], "-u")==0
1286 || strcmp(argv[i], "--update-wmaker")==0) {
1287 update++;
1288 } else if (strcmp(argv[i], "-D")==0
1289 || strcmp(argv[i], "--update-domain")==0) {
1290 update++;
1291 i++;
1292 if (i>=argc) {
1293 wfatal("too few arguments for %s\n", argv[i-1]);
1294 exit(1);
1296 domain = wstrdup(argv[i]);
1297 } else if (strcmp(argv[i], "-c")==0
1298 || strcmp(argv[i], "--colors")==0) {
1299 i++;
1300 if (i>=argc) {
1301 wfatal("too few arguments for %s\n", argv[i-1]);
1302 exit(1);
1304 if (sscanf(argv[i], "%i", &cpc)!=1) {
1305 wfatal("bad value for colors per channel: \"%s\"\n", argv[i]);
1306 exit(1);
1308 } else if (strcmp(argv[i], "-b")==0
1309 || strcmp(argv[i], "--back-color")==0) {
1310 i++;
1311 if (i>=argc) {
1312 wfatal("too few arguments for %s\n", argv[i-1]);
1313 exit(1);
1315 back_color = argv[i];
1316 } else if (strcmp(argv[i], "-p")==0
1317 || strcmp(argv[i], "--parse")==0) {
1318 i++;
1319 if (i>=argc) {
1320 wfatal("too few arguments for %s\n", argv[i-1]);
1321 exit(1);
1323 texture = argv[i];
1324 } else if (strcmp(argv[i], "-w")==0
1325 || strcmp(argv[i], "--workspace")==0) {
1326 i++;
1327 if (i>=argc) {
1328 wfatal("too few arguments for %s\n", argv[i-1]);
1329 exit(1);
1331 if (sscanf(argv[i], "%i", &workspace)!=1) {
1332 wfatal("bad value for workspace number: \"%s\"",
1333 argv[i]);
1334 exit(1);
1336 } else if (strcmp(argv[i], "--version")==0) {
1338 printf(PROG_VERSION);
1339 exit(0);
1341 } else if (strcmp(argv[i], "--help")==0) {
1342 print_help(argv[0]);
1343 exit(0);
1344 } else if (argv[i][0] != '-') {
1345 image_name = argv[i];
1346 } else {
1347 printf("%s: invalid argument '%s'\n", argv[0], argv[i]);
1348 printf("Try '%s --help' for more information\n", argv[0]);
1349 exit(1);
1352 if (!image_name && !texture && !helperMode) {
1353 printf("%s: you must specify a image file name or a texture\n",
1354 argv[0]);
1355 printf("Try '%s --help' for more information\n", argv[0]);
1356 exit(1);
1360 PixmapPath = getPixmapPath(domain);
1361 if (!smooth) {
1362 proplist_t val;
1363 #if 0 /* some problem with Alpha... TODO: check if its right */
1364 val = PLGetDictionaryEntry(domain,
1365 PLMakeString("SmoothWorkspaceBack"));
1366 #else
1367 val = getValueForKey(domain, "SmoothWorkspaceBack");
1368 #endif
1370 if (val && PLIsString(val) && strcasecmp(PLGetString(val), "YES")==0)
1371 smooth = True;
1374 dpy = XOpenDisplay(display);
1375 if (!dpy) {
1376 wfatal("could not open display");
1377 exit(1);
1379 #if 0
1380 XSynchronize(dpy, 1);
1381 #endif
1383 root = DefaultRootWindow(dpy);
1385 scr = DefaultScreen(dpy);
1387 scrWidth = WidthOfScreen(DefaultScreenOfDisplay(dpy));
1388 scrHeight = HeightOfScreen(DefaultScreenOfDisplay(dpy));
1390 if (!obey_user && DefaultDepth(dpy, scr) <= 8)
1391 render_mode = RDitheredRendering;
1393 rattr.flags = RC_RenderMode | RC_ColorsPerChannel | RC_DefaultVisual;
1394 rattr.render_mode = render_mode;
1395 rattr.colors_per_channel = cpc;
1397 rc = RCreateContext(dpy, scr, &rattr);
1399 if (helperMode) {
1400 /* lower priority, so that it wont use all the CPU */
1401 nice(1000);
1403 helperLoop(rc);
1404 } else {
1405 BackgroundTexture *tex;
1406 char buffer[4098];
1408 if (!texture) {
1409 sprintf(buffer, "(%s, \"%s\", %s)", style, image_name, back_color);
1410 texture = (char*)buffer;
1413 if (update && workspace < 0) {
1414 updateDomain(domain, "WorkspaceBack", texture);
1417 tex = parseTexture(rc, texture);
1418 if (!tex)
1419 exit(1);
1421 if (workspace<0)
1422 changeTexture(tex);
1423 else {
1424 /* always update domain */
1425 changeTextureForWorkspace(domain, texture, workspace);
1429 return 0;