1 /* wmsetbg.c- sets root window background image and also works as
2 * workspace background setting helper for wmaker
4 * WindowMaker window manager
6 * Copyright (c) 1998-2003 Alfredo K. Kojima
7 * Copyright (c) 1998-2003 Dan Pascu
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,
26 * TODO: rewrite, too dirty
32 #include <X11/Xutil.h>
33 #include <X11/Xatom.h>
37 #include <sys/types.h>
40 #include "../src/config.h"
43 # ifdef SOLARIS_XINERAMA /* sucks */
44 # include <X11/extensions/xinerama.h>
46 # include <X11/extensions/Xinerama.h>
54 #include "../src/wconfig.h"
56 #include <WINGs/WINGs.h>
61 int count; /* screen count, 0 = inactive */
64 #define PROG_VERSION "wmsetbg (Window Maker) 2.8"
66 #define WORKSPACE_COUNT (MAX_WORKSPACES+1)
76 WXineramaInfo xineInfo;
80 Pixmap CurrentPixmap = None;
81 char *PixmapPath = NULL;
83 extern Pixmap LoadJPEG(RContext * rc, char *file_name, int *width, int *height);
85 typedef struct BackgroundTexture {
93 Pixmap pixmap; /* for all textures, including solid */
94 int width; /* size of the pixmap */
100 xineInfo.screens = NULL;
103 # ifdef SOLARIS_XINERAMA
104 if (XineramaGetState(dpy, scr)) {
105 XRectangle head[MAXFRAMEBUFFERS];
106 unsigned char hints[MAXFRAMEBUFFERS];
109 if (XineramaGetInfo(dpy, scr, head, hints, &xineInfo.count)) {
111 xineInfo.screens = wmalloc(sizeof(WMRect) * (xineInfo.count + 1));
113 for (i = 0; i < xineInfo.count; i++) {
114 xineInfo.screens[i].pos.x = head[i].x;
115 xineInfo.screens[i].pos.y = head[i].y;
116 xineInfo.screens[i].size.width = head[i].width;
117 xineInfo.screens[i].size.height = head[i].height;
121 # else /* !SOLARIS_XINERAMA */
122 if (XineramaIsActive(dpy)) {
123 XineramaScreenInfo *xine_screens;
126 xine_screens = XineramaQueryScreens(dpy, &xineInfo.count);
128 xineInfo.screens = wmalloc(sizeof(WMRect) * (xineInfo.count + 1));
130 for (i = 0; i < xineInfo.count; i++) {
131 xineInfo.screens[i].pos.x = xine_screens[i].x_org;
132 xineInfo.screens[i].pos.y = xine_screens[i].y_org;
133 xineInfo.screens[i].size.width = xine_screens[i].width;
134 xineInfo.screens[i].size.height = xine_screens[i].height;
138 # endif /* !SOLARIS_XINERAMA */
139 #endif /* XINERAMA */
142 RImage *loadImage(RContext * rc, char *file)
147 if (access(file, F_OK) != 0) {
148 path = wfindfile(PixmapPath, file);
150 wwarning("%s:could not find image file used in texture", file);
154 path = wstrdup(file);
157 image = RLoadImage(rc, path, 0);
159 wwarning("%s:could not load image file used in texture:%s", path, RMessageForError(RErrorCode));
167 applyImage(RContext * rc, BackgroundTexture * texture, RImage * image, char type,
168 int x, int y, int width, int height)
173 switch (toupper(type)) {
176 if (toupper(type) == 'S') {
180 if (image->width * height > image->height * width) {
182 h = (width * image->height) / image->width;
184 w = (height * image->width) / image->height;
189 if (w != image->width || h != image->height) {
193 simage = RSmoothScaleImage(image, w, h);
195 simage = RScaleImage(image, w, h);
199 wwarning("could not scale image:%s", RMessageForError(RErrorCode));
211 if (!RConvertImage(rc, image, &pixmap)) {
212 wwarning("could not convert texture:%s", RMessageForError(RErrorCode));
216 if (image->width != width || image->height != height) {
219 if (image->height < height) {
221 y += (height - h) / 2;
224 sy = (image->height - height) / 2;
227 if (image->width < width) {
229 x += (width - w) / 2;
232 sx = (image->width - width) / 2;
236 XCopyArea(dpy, pixmap, texture->pixmap, DefaultGC(dpy, scr), sx, sy, w, h, x, y);
238 XCopyArea(dpy, pixmap, texture->pixmap, DefaultGC(dpy, scr), 0, 0, width, height,
241 XFreePixmap(dpy, pixmap);
243 RReleaseImage(image);
250 BackgroundTexture *parseTexture(RContext * rc, char *text)
252 BackgroundTexture *texture = NULL;
253 WMPropList *texarray;
259 #define GETSTRORGOTO(val, str, i, label) \
260 val = WMGetFromPLArray(texarray, i);\
261 if (!WMIsPLString(val)) {\
262 wwarning("could not parse texture %s", text);\
265 str = WMGetFromPLString(val)
267 texarray = WMCreatePropListFromDescription(text);
268 if (!texarray || !WMIsPLArray(texarray)
269 || (count = WMGetPropListItemCount(texarray)) < 2) {
271 wwarning("could not parse texture %s", text);
273 WMReleasePropList(texarray);
277 texture = wmalloc(sizeof(BackgroundTexture));
278 memset(texture, 0, sizeof(BackgroundTexture));
280 GETSTRORGOTO(val, type, 0, error);
282 if (strcasecmp(type, "solid") == 0) {
288 GETSTRORGOTO(val, tmp, 1, error);
290 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
291 wwarning("could not parse color %s in texture %s", tmp, text);
294 XAllocColor(dpy, DefaultColormap(dpy, scr), &color);
296 pixmap = XCreatePixmap(dpy, root, 8, 8, DefaultDepth(dpy, scr));
297 XSetForeground(dpy, DefaultGC(dpy, scr), color.pixel);
298 XFillRectangle(dpy, pixmap, DefaultGC(dpy, scr), 0, 0, 8, 8);
300 texture->pixmap = pixmap;
301 texture->color = color;
304 } else if (strcasecmp(type, "vgradient") == 0
305 || strcasecmp(type, "dgradient") == 0 || strcasecmp(type, "hgradient") == 0) {
307 RColor color1, color2;
313 GETSTRORGOTO(val, tmp, 1, error);
315 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
316 wwarning("could not parse color %s in texture %s", tmp, text);
320 color1.red = color.red >> 8;
321 color1.green = color.green >> 8;
322 color1.blue = color.blue >> 8;
324 GETSTRORGOTO(val, tmp, 2, error);
326 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
327 wwarning("could not parse color %s in texture %s", tmp, text);
331 color2.red = color.red >> 8;
332 color2.green = color.green >> 8;
333 color2.blue = color.blue >> 8;
338 gtype = RHorizontalGradient;
344 gtype = RVerticalGradient;
349 gtype = RDiagonalGradient;
355 image = RRenderGradient(iwidth, iheight, &color1, &color2, gtype);
358 wwarning("could not render gradient texture:%s", RMessageForError(RErrorCode));
362 if (!RConvertImage(rc, image, &pixmap)) {
363 wwarning("could not convert texture:%s", RMessageForError(RErrorCode));
364 RReleaseImage(image);
368 texture->width = image->width;
369 texture->height = image->height;
370 RReleaseImage(image);
372 texture->pixmap = pixmap;
373 } else if (strcasecmp(type, "mvgradient") == 0
374 || strcasecmp(type, "mdgradient") == 0 || strcasecmp(type, "mhgradient") == 0) {
383 colors = malloc(sizeof(RColor *) * (count - 1));
385 wwarning("out of memory while parsing texture");
388 memset(colors, 0, sizeof(RColor *) * (count - 1));
390 for (i = 2; i < count; i++) {
391 val = WMGetFromPLArray(texarray, i);
392 if (!WMIsPLString(val)) {
393 wwarning("could not parse texture %s", text);
395 for (j = 0; colors[j] != NULL; j++)
400 tmp = WMGetFromPLString(val);
402 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
403 wwarning("could not parse color %s in texture %s", tmp, text);
405 for (j = 0; colors[j] != NULL; j++)
410 if (!(colors[i - 2] = malloc(sizeof(RColor)))) {
411 wwarning("out of memory while parsing texture");
413 for (j = 0; colors[j] != NULL; j++)
419 colors[i - 2]->red = color.red >> 8;
420 colors[i - 2]->green = color.green >> 8;
421 colors[i - 2]->blue = color.blue >> 8;
427 gtype = RHorizontalGradient;
433 gtype = RVerticalGradient;
438 gtype = RDiagonalGradient;
444 image = RRenderMultiGradient(iwidth, iheight, colors, gtype);
446 for (j = 0; colors[j] != NULL; j++)
451 wwarning("could not render gradient texture:%s", RMessageForError(RErrorCode));
455 if (!RConvertImage(rc, image, &pixmap)) {
456 wwarning("could not convert texture:%s", RMessageForError(RErrorCode));
457 RReleaseImage(image);
461 texture->width = image->width;
462 texture->height = image->height;
463 RReleaseImage(image);
465 texture->pixmap = pixmap;
466 } else if (strcasecmp(type, "cpixmap") == 0
467 || strcasecmp(type, "spixmap") == 0
468 || strcasecmp(type, "mpixmap") == 0 || strcasecmp(type, "tpixmap") == 0) {
470 Pixmap pixmap = None;
471 RImage *image = NULL;
475 GETSTRORGOTO(val, tmp, 1, error);
477 if (toupper(type[0]) == 'T' || toupper(type[0]) == 'C')
478 pixmap = LoadJPEG(rc, tmp, &iwidth, &iheight);
482 image = loadImage(rc, tmp);
486 iwidth = image->width;
487 iheight = image->height;
490 GETSTRORGOTO(val, tmp, 2, error);
492 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
493 wwarning("could not parse color %s in texture %s\n", tmp, text);
494 RReleaseImage(image);
497 if (!XAllocColor(dpy, DefaultColormap(dpy, scr), &color)) {
498 rcolor.red = color.red >> 8;
499 rcolor.green = color.green >> 8;
500 rcolor.blue = color.blue >> 8;
501 RGetClosestXColor(rc, &rcolor, &color);
507 /* for images with a transparent color */
508 if (image->data[3]) {
509 RCombineImageWithColor(image, &rcolor);
512 switch (toupper(type[0])) {
514 texture->width = iwidth;
515 texture->height = iheight;
516 if (!pixmap && !RConvertImage(rc, image, &pixmap)) {
517 wwarning("could not convert texture:%s", RMessageForError(RErrorCode));
518 RReleaseImage(image);
522 RReleaseImage(image);
524 texture->pixmap = pixmap;
525 texture->color = color;
532 XCreatePixmap(dpy, root, scrWidth, scrHeight, DefaultDepth(dpy, scr));
533 XSetForeground(dpy, DefaultGC(dpy, scr), color.pixel);
534 XFillRectangle(dpy, tpixmap, DefaultGC(dpy, scr), 0, 0, scrWidth, scrHeight);
536 texture->pixmap = tpixmap;
537 texture->color = color;
538 texture->width = scrWidth;
539 texture->height = scrHeight;
542 if (xineInfo.count) {
544 for (i = 0; i < xineInfo.count; ++i) {
545 applyImage(rc, texture, image, type[0],
546 xineInfo.screens[i].pos.x, xineInfo.screens[i].pos.y,
547 xineInfo.screens[i].size.width,
548 xineInfo.screens[i].size.height);
551 applyImage(rc, texture, image, type[0], 0, 0, scrWidth, scrHeight);
553 #else /* !XINERAMA */
554 applyImage(rc, texture, image, type[0], 0, 0, scrWidth, scrHeight);
555 #endif /* !XINERAMA */
556 RReleaseImage(image);
560 } else if (strcasecmp(type, "thgradient") == 0
561 || strcasecmp(type, "tvgradient") == 0 || strcasecmp(type, "tdgradient") == 0) {
563 RColor color1, color2;
573 GETSTRORGOTO(val, file, 1, error);
575 GETSTRORGOTO(val, tmp, 2, error);
579 GETSTRORGOTO(val, tmp, 3, error);
581 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
582 wwarning("could not parse color %s in texture %s", tmp, text);
586 color1.red = color.red >> 8;
587 color1.green = color.green >> 8;
588 color1.blue = color.blue >> 8;
590 GETSTRORGOTO(val, tmp, 4, error);
592 if (!XParseColor(dpy, DefaultColormap(dpy, scr), tmp, &color)) {
593 wwarning("could not parse color %s in texture %s", tmp, text);
597 color2.red = color.red >> 8;
598 color2.green = color.green >> 8;
599 color2.blue = color.blue >> 8;
601 image = loadImage(rc, file);
609 gtype = RHorizontalGradient;
611 theight = image->height > scrHeight ? scrHeight : image->height;
615 gtype = RVerticalGradient;
616 twidth = image->width > scrWidth ? scrWidth : image->width;
620 gtype = RDiagonalGradient;
625 gradient = RRenderGradient(twidth, theight, &color1, &color2, gtype);
628 wwarning("could not render texture:%s", RMessageForError(RErrorCode));
629 RReleaseImage(gradient);
630 RReleaseImage(image);
634 tiled = RMakeTiledImage(image, twidth, theight);
636 wwarning("could not render texture:%s", RMessageForError(RErrorCode));
637 RReleaseImage(gradient);
638 RReleaseImage(image);
641 RReleaseImage(image);
643 RCombineImagesWithOpaqueness(tiled, gradient, opaq);
644 RReleaseImage(gradient);
646 if (!RConvertImage(rc, tiled, &pixmap)) {
647 wwarning("could not convert texture:%s", RMessageForError(RErrorCode));
648 RReleaseImage(tiled);
651 texture->width = tiled->width;
652 texture->height = tiled->height;
654 RReleaseImage(tiled);
656 texture->pixmap = pixmap;
657 } else if (strcasecmp(type, "function") == 0) {
659 void (*initFunc) (Display *, Colormap);
660 RImage *(*mainFunc) (int, char **, int, int, int);
664 char *lib, *func, **argv = 0;
669 goto function_cleanup;
671 /* get the library name */
672 GETSTRORGOTO(val, lib, 1, function_cleanup);
674 /* get the function name */
675 GETSTRORGOTO(val, func, 2, function_cleanup);
678 argv = (char **)wmalloc(argc * sizeof(char *));
680 /* get the parameters */
682 for (i = 0; i < argc - 1; i++) {
683 GETSTRORGOTO(val, tmp, 3 + i, function_cleanup);
684 argv[i + 1] = wstrdup(tmp);
687 handle = dlopen(lib, RTLD_LAZY);
689 wwarning("could not find library %s", lib);
690 goto function_cleanup;
693 initFunc = dlsym(handle, "initWindowMaker");
695 wwarning("could not initialize library %s", lib);
696 goto function_cleanup;
698 initFunc(dpy, DefaultColormap(dpy, scr));
700 mainFunc = dlsym(handle, func);
702 wwarning("could not find function %s::%s", lib, func);
703 goto function_cleanup;
705 image = mainFunc(argc, argv, scrWidth, scrHeight, 0);
707 if (!RConvertImage(rc, image, &pixmap)) {
708 wwarning("could not convert texture:%s", RMessageForError(RErrorCode));
709 goto function_cleanup;
711 texture->width = scrWidth;
712 texture->height = scrHeight;
713 texture->pixmap = pixmap;
719 for (i = 0; i < argc; i++) {
727 RReleaseImage(image);
733 wwarning("function textures not supported");
737 wwarning("invalid texture type %s", text);
741 texture->spec = wstrdup(text);
749 WMReleasePropList(texarray);
754 void freeTexture(BackgroundTexture * texture)
756 if (texture->solid) {
757 unsigned long pixel[1];
759 pixel[0] = texture->color.pixel;
760 /* dont free black/white pixels */
761 if (pixel[0] != BlackPixelOfScreen(DefaultScreenOfDisplay(dpy))
762 && pixel[0] != WhitePixelOfScreen(DefaultScreenOfDisplay(dpy)))
763 XFreeColors(dpy, DefaultColormap(dpy, scr), pixel, 1, 0);
765 if (texture->pixmap) {
766 XFreePixmap(dpy, texture->pixmap);
768 wfree(texture->spec);
772 void setupTexture(RContext * rc, BackgroundTexture ** textures, int *maxTextures, int workspace, char *texture)
774 BackgroundTexture *newTexture = NULL;
777 /* unset the texture */
779 if (textures[workspace] != NULL) {
780 textures[workspace]->refcount--;
782 if (textures[workspace]->refcount == 0)
783 freeTexture(textures[workspace]);
785 textures[workspace] = NULL;
789 if (textures[workspace]
790 && strcasecmp(textures[workspace]->spec, texture) == 0) {
791 /* texture did not change */
795 /* check if the same texture is already created */
796 for (i = 0; i < *maxTextures; i++) {
797 if (textures[i] && strcasecmp(textures[i]->spec, texture) == 0) {
798 newTexture = textures[i];
804 /* create the texture */
805 newTexture = parseTexture(rc, texture);
810 if (textures[workspace] != NULL) {
812 textures[workspace]->refcount--;
814 if (textures[workspace]->refcount == 0)
815 freeTexture(textures[workspace]);
818 newTexture->refcount++;
819 textures[workspace] = newTexture;
821 if (*maxTextures < workspace)
822 *maxTextures = workspace;
825 Pixmap duplicatePixmap(Pixmap pixmap, int width, int height)
830 /* must open a new display or the RetainPermanent will
831 * leave stuff allocated in RContext unallocated after exit */
832 tmpDpy = XOpenDisplay(display);
834 wwarning("could not open display to update background image information");
840 copyP = XCreatePixmap(tmpDpy, root, width, height, DefaultDepth(tmpDpy, scr));
841 XCopyArea(tmpDpy, pixmap, copyP, DefaultGC(tmpDpy, scr), 0, 0, width, height, 0, 0);
842 XSync(tmpDpy, False);
844 XSetCloseDownMode(tmpDpy, RetainPermanent);
845 XCloseDisplay(tmpDpy);
851 static int dummyErrorHandler(Display * dpy, XErrorEvent * err)
856 void setPixmapProperty(Pixmap pixmap)
858 static Atom prop = 0;
861 unsigned long length, after;
866 prop = XInternAtom(dpy, "_XROOTPMAP_ID", False);
871 /* Clear out the old pixmap */
872 XGetWindowProperty(dpy, root, prop, 0L, 1L, False, AnyPropertyType,
873 &type, &format, &length, &after, &data);
875 if ((type == XA_PIXMAP) && (format == 32) && (length == 1)) {
876 XSetErrorHandler(dummyErrorHandler);
877 XKillClient(dpy, *((Pixmap *) data));
879 XSetErrorHandler(NULL);
880 mode = PropModeReplace;
882 mode = PropModeAppend;
885 XChangeProperty(dpy, root, prop, XA_PIXMAP, 32, mode, (unsigned char *)&pixmap, 1);
887 XDeleteProperty(dpy, root, prop);
893 void changeTexture(BackgroundTexture * texture)
899 if (texture->solid) {
900 XSetWindowBackground(dpy, root, texture->color.pixel);
902 XSetWindowBackgroundPixmap(dpy, root, texture->pixmap);
904 XClearWindow(dpy, root);
911 pixmap = duplicatePixmap(texture->pixmap, texture->width, texture->height);
913 setPixmapProperty(pixmap);
917 int readmsg(int fd, char *buffer, int size)
923 count = read(fd, buffer, size);
936 * sizeSntexture_spec - sets the texture for workspace n
937 * sizeCn - change background texture to the one for workspace n
938 * sizePpath - set the pixmap search path
941 * size = 4 bytes for length of the message data
943 void helperLoop(RContext * rc)
945 BackgroundTexture *textures[WORKSPACE_COUNT];
947 char buffer[2048], buf[8];
951 memset(textures, 0, WORKSPACE_COUNT * sizeof(BackgroundTexture *));
956 /* get length of message */
957 if (readmsg(0, buffer, 4) < 0) {
958 wsyserror("error reading message from Window Maker");
966 memcpy(buf, buffer, 4);
971 if (readmsg(0, buffer, size) < 0) {
972 wsyserror("error reading message from Window Maker");
981 printf("RECEIVED %s\n", buffer);
983 if (buffer[0] != 'P' && buffer[0] != 'K') {
984 memcpy(buf, &buffer[1], 4);
986 workspace = atoi(buf);
987 if (workspace < 0 || workspace >= WORKSPACE_COUNT) {
988 wwarning("received message with invalid workspace number %i\n", workspace);
996 printf("set texture %s\n", &buffer[5]);
998 setupTexture(rc, textures, &maxTextures, workspace, &buffer[5]);
1003 printf("change texture %i\n", workspace);
1005 if (!textures[workspace]) {
1006 changeTexture(textures[0]);
1008 changeTexture(textures[workspace]);
1014 printf("change pixmappath %s\n", &buffer[1]);
1018 PixmapPath = wstrdup(&buffer[1]);
1023 printf("unset workspace %i\n", workspace);
1025 setupTexture(rc, textures, &maxTextures, workspace, NULL);
1030 printf("exit command\n");
1035 wwarning("unknown message received");
1041 void updateDomain(char *domain, char *key, char *texture)
1043 char *program = "wdwrite";
1045 /* here is a mem leak */
1046 system(wstrconcat("wdwrite ",
1047 wstrconcat(domain, smooth ? " SmoothWorkspaceBack YES" : " SmoothWorkspaceBack NO")));
1049 execlp(program, program, domain, key, texture, NULL);
1050 wwarning("warning could not run \"%s\"", program);
1053 char *globalDefaultsPathForDomain(char *domain)
1057 sprintf(path, "%s/WindowMaker/%s", SYSCONFDIR, domain);
1059 return wstrdup(path);
1062 static WMPropList *getValueForKey(char *domain, char *keyName)
1065 WMPropList *key, *val, *d;
1067 key = WMCreatePLString(keyName);
1069 /* try to find PixmapPath in user defaults */
1070 path = wdefaultspathfordomain(domain);
1071 d = WMReadPropListFromFile(path);
1073 wwarning("could not open domain file %s", path);
1077 if (d && !WMIsPLDictionary(d)) {
1078 WMReleasePropList(d);
1082 val = WMGetFromPLDictionary(d, key);
1086 /* try to find PixmapPath in global defaults */
1088 path = globalDefaultsPathForDomain(domain);
1090 wwarning("could not locate file for domain %s", domain);
1093 d = WMReadPropListFromFile(path);
1097 if (d && !WMIsPLDictionary(d)) {
1098 WMReleasePropList(d);
1102 val = WMGetFromPLDictionary(d, key);
1110 WMRetainPropList(val);
1112 WMReleasePropList(key);
1114 WMReleasePropList(d);
1119 char *getPixmapPath(char *domain)
1125 val = getValueForKey(domain, "PixmapPath");
1127 if (!val || !WMIsPLArray(val)) {
1129 WMReleasePropList(val);
1133 count = WMGetPropListItemCount(val);
1135 for (i = 0; i < count; i++) {
1138 v = WMGetFromPLArray(val, i);
1139 if (!v || !WMIsPLString(v)) {
1142 len += strlen(WMGetFromPLString(v)) + 1;
1145 ptr = data = wmalloc(len + 1);
1148 for (i = 0; i < count; i++) {
1151 v = WMGetFromPLArray(val, i);
1152 if (!v || !WMIsPLString(v)) {
1155 strcpy(ptr, WMGetFromPLString(v));
1157 ptr += strlen(WMGetFromPLString(v));
1165 WMReleasePropList(val);
1170 char *getFullPixmapPath(char *file)
1174 if (!PixmapPath || !(tmp = wfindfile(PixmapPath, file))) {
1176 char *path = wmalloc(bsize);
1178 while (!getcwd(path, bsize)) {
1180 path = wrealloc(path, bsize);
1183 tmp = wstrconcat(path, "/");
1185 path = wstrconcat(tmp, file);
1191 /* the file is in the PixmapPath */
1194 return wstrdup(file);
1203 void print_help(char *ProgName)
1205 printf("Usage: %s [options] [image]\n", ProgName);
1206 puts("Sets the workspace background to the specified image or a texture and optionally update Window Maker configuration");
1208 #define P(m) puts(m)
1209 P(" -display display to use");
1210 P(" -d, --dither dither image");
1211 P(" -m, --match match colors");
1212 P(" -S, --smooth smooth scaled image");
1213 P(" -b, --back-color <color> background color");
1214 P(" -t, --tile tile image");
1215 P(" -e, --center center image");
1216 P(" -s, --scale scale image (default)");
1217 P(" -a, --maxscale scale image and keep aspect ratio");
1218 P(" -u, --update-wmaker update WindowMaker domain database");
1219 P(" -D, --update-domain <domain> update <domain> database");
1220 P(" -c, --colors <cpc> colors per channel to use");
1221 P(" -p, --parse <texture> proplist style texture specification");
1222 P(" -w, --workspace <workspace> update background for the specified workspace");
1223 P(" --version show version of wmsetbg and exit");
1224 P(" --help show this help and exit");
1228 void changeTextureForWorkspace(char *domain, char *texture, int workspace)
1230 WMPropList *array, *val;
1234 val = WMCreatePropListFromDescription(texture);
1236 wwarning("could not parse texture %s", texture);
1240 array = getValueForKey("WindowMaker", "WorkspaceSpecificBack");
1243 array = WMCreatePLArray(NULL, NULL);
1246 j = WMGetPropListItemCount(array);
1247 if (workspace >= j) {
1250 empty = WMCreatePLArray(NULL, NULL);
1252 while (j++ < workspace - 1) {
1253 WMAddToPLArray(array, empty);
1255 WMAddToPLArray(array, val);
1257 WMDeleteFromPLArray(array, workspace);
1258 WMInsertInPLArray(array, workspace, val);
1261 value = WMGetPropListDescription(array, False);
1262 updateDomain(domain, "WorkspaceSpecificBack", value);
1265 int main(int argc, char **argv)
1270 RContextAttributes rattr;
1271 char *style = "spixmap";
1272 char *back_color = "gray20";
1273 char *image_name = NULL;
1274 char *domain = "WindowMaker";
1275 int update = 0, cpc = 4, render_mode = RDitheredRendering, obey_user = 0;
1276 char *texture = NULL;
1279 signal(SIGINT, SIG_DFL);
1280 signal(SIGTERM, SIG_DFL);
1281 signal(SIGQUIT, SIG_DFL);
1282 signal(SIGSEGV, SIG_DFL);
1283 signal(SIGBUS, SIG_DFL);
1284 signal(SIGFPE, SIG_DFL);
1285 signal(SIGABRT, SIG_DFL);
1286 signal(SIGHUP, SIG_DFL);
1287 signal(SIGPIPE, SIG_DFL);
1288 signal(SIGCHLD, SIG_DFL);
1290 WMInitializeApplication("wmsetbg", &argc, argv);
1292 for (i = 1; i < argc; i++) {
1293 if (strcmp(argv[i], "-helper") == 0) {
1295 } else if (strcmp(argv[i], "-display") == 0) {
1298 wfatal("too few arguments for %s\n", argv[i - 1]);
1302 } else if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--scale") == 0) {
1304 } else if (strcmp(argv[i], "-t") == 0 || strcmp(argv[i], "--tile") == 0) {
1306 } else if (strcmp(argv[i], "-e") == 0 || strcmp(argv[i], "--center") == 0) {
1308 } else if (strcmp(argv[i], "-a") == 0 || strcmp(argv[i], "--maxscale") == 0) {
1310 } else if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--dither") == 0) {
1311 render_mode = RDitheredRendering;
1313 } else if (strcmp(argv[i], "-m") == 0 || strcmp(argv[i], "--match") == 0) {
1314 render_mode = RBestMatchRendering;
1316 } else if (strcmp(argv[i], "-S") == 0 || strcmp(argv[i], "--smooth") == 0) {
1318 } else if (strcmp(argv[i], "-u") == 0 || strcmp(argv[i], "--update-wmaker") == 0) {
1320 } else if (strcmp(argv[i], "-D") == 0 || strcmp(argv[i], "--update-domain") == 0) {
1324 wfatal("too few arguments for %s\n", argv[i - 1]);
1327 domain = wstrdup(argv[i]);
1328 } else if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--colors") == 0) {
1331 wfatal("too few arguments for %s\n", argv[i - 1]);
1334 if (sscanf(argv[i], "%i", &cpc) != 1) {
1335 wfatal("bad value for colors per channel: \"%s\"\n", argv[i]);
1338 } else if (strcmp(argv[i], "-b") == 0 || strcmp(argv[i], "--back-color") == 0) {
1341 wfatal("too few arguments for %s\n", argv[i - 1]);
1344 back_color = argv[i];
1345 } else if (strcmp(argv[i], "-p") == 0 || strcmp(argv[i], "--parse") == 0) {
1348 wfatal("too few arguments for %s\n", argv[i - 1]);
1352 } else if (strcmp(argv[i], "-w") == 0 || strcmp(argv[i], "--workspace") == 0) {
1355 wfatal("too few arguments for %s\n", argv[i - 1]);
1358 if (sscanf(argv[i], "%i", &workspace) != 1) {
1359 wfatal("bad value for workspace number: \"%s\"", argv[i]);
1362 } else if (strcmp(argv[i], "--version") == 0) {
1364 printf(PROG_VERSION);
1367 } else if (strcmp(argv[i], "--help") == 0) {
1368 print_help(argv[0]);
1370 } else if (argv[i][0] != '-') {
1371 image_name = argv[i];
1373 printf("%s: invalid argument '%s'\n", argv[0], argv[i]);
1374 printf("Try '%s --help' for more information\n", argv[0]);
1378 if (!image_name && !texture && !helperMode) {
1379 printf("%s: you must specify a image file name or a texture\n", argv[0]);
1380 printf("Try '%s --help' for more information\n", argv[0]);
1384 PixmapPath = getPixmapPath(domain);
1387 #if 0 /* some problem with Alpha... TODO: check if its right */
1388 val = WMGetFromPLDictionary(domain, WMCreatePLString("SmoothWorkspaceBack"));
1390 val = getValueForKey(domain, "SmoothWorkspaceBack");
1393 if (val && WMIsPLString(val) && strcasecmp(WMGetFromPLString(val), "YES") == 0)
1397 dpy = XOpenDisplay(display);
1399 wfatal("could not open display");
1403 XSynchronize(dpy, 1);
1406 root = DefaultRootWindow(dpy);
1408 scr = DefaultScreen(dpy);
1410 scrWidth = WidthOfScreen(DefaultScreenOfDisplay(dpy));
1411 scrHeight = HeightOfScreen(DefaultScreenOfDisplay(dpy));
1416 if (!obey_user && DefaultDepth(dpy, scr) <= 8)
1417 render_mode = RDitheredRendering;
1419 rattr.flags = RC_RenderMode | RC_ColorsPerChannel | RC_StandardColormap | RC_DefaultVisual;
1420 rattr.render_mode = render_mode;
1421 rattr.colors_per_channel = cpc;
1422 rattr.standard_colormap_mode = RCreateStdColormap;
1424 rc = RCreateContext(dpy, scr, &rattr);
1427 rattr.standard_colormap_mode = RIgnoreStdColormap;
1428 rc = RCreateContext(dpy, scr, &rattr);
1432 wfatal("could not initialize wrlib: %s", RMessageForError(RErrorCode));
1437 /* lower priority, so that it wont use all the CPU */
1442 BackgroundTexture *tex;
1446 char *image_path = getFullPixmapPath(image_name);
1448 sprintf(buffer, "(%s, \"%s\", %s)", style, image_path, back_color);
1450 texture = (char *)buffer;
1453 if (update && workspace < 0) {
1454 updateDomain(domain, "WorkspaceBack", texture);
1457 tex = parseTexture(rc, texture);
1464 /* always update domain */
1465 changeTextureForWorkspace(domain, texture, workspace);