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 #include <X11/extensions/Xinerama.h>
50 #include "../src/wconfig.h"
52 #include <WINGs/WINGs.h>
56 #define PROG_VERSION "wmsetbg (Window Maker) 2.7"
59 #define WORKSPACE_COUNT (MAX_WORKSPACES+1)
71 XineramaScreenInfo
*xine_screens
;
78 Pixmap CurrentPixmap
= None
;
79 char *PixmapPath
= NULL
;
82 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 */
101 loadImage(RContext
*rc
, char *file
)
106 if (access(file
, F_OK
)!=0) {
107 path
= wfindfile(PixmapPath
, file
);
109 wwarning("%s:could not find image file used in texture", file
);
113 path
= wstrdup(file
);
116 image
= RLoadImage(rc
, path
, 0);
118 wwarning("%s:could not load image file used in texture:%s", path
,
119 RMessageForError(RErrorCode
));
128 applyImage(RContext
*rc
, BackgroundTexture
*texture
, RImage
*image
, char type
,
129 int x
, int y
, int width
, int height
)
134 switch (toupper(type
)) {
141 if (image
->width
*height
> image
->height
*width
) {
143 h
= (width
*image
->height
) / image
->width
;
145 w
= (height
*image
->width
) / image
->height
;
150 if (w
!= image
->width
|| h
!= image
->height
) {
154 simage
= RSmoothScaleImage(image
, w
, h
);
156 simage
= RScaleImage(image
, w
, h
);
160 wwarning("could not scale image:%s", RMessageForError(RErrorCode
));
172 if (!RConvertImage(rc
, image
, &pixmap
)) {
173 wwarning("could not convert texture:%s", RMessageForError(RErrorCode
));
177 if (image
->width
!= width
|| image
->height
!= height
) {
180 if (image
->height
< height
) {
182 y
+= (height
- h
) / 2;
185 sy
= (image
->height
- height
) / 2;
188 if (image
->width
< width
) {
190 x
+= (width
- w
) / 2;
193 sx
= (image
->width
- width
) / 2;
197 XCopyArea(dpy
, pixmap
, texture
->pixmap
, DefaultGC(dpy
, scr
), sx
, sy
, w
, h
, x
, y
);
199 XCopyArea(dpy
, pixmap
, texture
->pixmap
, DefaultGC(dpy
, scr
), 0, 0, width
, height
, x
, y
);
201 XFreePixmap(dpy
, pixmap
);
203 RReleaseImage( image
);
212 parseTexture(RContext
*rc
, char *text
)
214 BackgroundTexture
*texture
= NULL
;
215 WMPropList
*texarray
;
221 #define GETSTRORGOTO(val, str, i, label) \
222 val = WMGetFromPLArray(texarray, i);\
223 if (!WMIsPLString(val)) {\
224 wwarning("could not parse texture %s", text);\
227 str = WMGetFromPLString(val)
229 texarray
= WMCreatePropListFromDescription(text
);
230 if (!texarray
|| !WMIsPLArray(texarray
)
231 || (count
= WMGetPropListItemCount(texarray
)) < 2) {
233 wwarning("could not parse texture %s", text
);
235 WMReleasePropList(texarray
);
239 texture
= wmalloc(sizeof(BackgroundTexture
));
240 memset(texture
, 0, sizeof(BackgroundTexture
));
242 GETSTRORGOTO(val
, type
, 0, error
);
244 if (strcasecmp(type
, "solid")==0) {
250 GETSTRORGOTO(val
, tmp
, 1, error
);
252 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
253 wwarning("could not parse color %s in texture %s", tmp
, text
);
256 XAllocColor(dpy
, DefaultColormap(dpy
, scr
), &color
);
258 pixmap
= XCreatePixmap(dpy
, root
, 8, 8, DefaultDepth(dpy
, scr
));
259 XSetForeground(dpy
, DefaultGC(dpy
, scr
), color
.pixel
);
260 XFillRectangle(dpy
, pixmap
, DefaultGC(dpy
, scr
), 0, 0, 8, 8);
262 texture
->pixmap
= pixmap
;
263 texture
->color
= color
;
266 } else if (strcasecmp(type
, "vgradient")==0
267 || strcasecmp(type
, "dgradient")==0
268 || strcasecmp(type
, "hgradient")==0) {
270 RColor color1
, color2
;
276 GETSTRORGOTO(val
, tmp
, 1, error
);
278 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
279 wwarning("could not parse color %s in texture %s", tmp
, text
);
283 color1
.red
= color
.red
>> 8;
284 color1
.green
= color
.green
>> 8;
285 color1
.blue
= color
.blue
>> 8;
287 GETSTRORGOTO(val
, tmp
, 2, error
);
289 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
290 wwarning("could not parse color %s in texture %s", tmp
, text
);
294 color2
.red
= color
.red
>> 8;
295 color2
.green
= color
.green
>> 8;
296 color2
.blue
= color
.blue
>> 8;
301 gtype
= RHorizontalGradient
;
307 gtype
= RVerticalGradient
;
312 gtype
= RDiagonalGradient
;
318 image
= RRenderGradient(iwidth
, iheight
, &color1
, &color2
, gtype
);
321 wwarning("could not render gradient texture:%s",
322 RMessageForError(RErrorCode
));
326 if (!RConvertImage(rc
, image
, &pixmap
)) {
327 wwarning("could not convert texture:%s",
328 RMessageForError(RErrorCode
));
329 RReleaseImage(image
);
333 texture
->width
= image
->width
;
334 texture
->height
= image
->height
;
335 RReleaseImage(image
);
337 texture
->pixmap
= pixmap
;
338 } else if (strcasecmp(type
, "mvgradient")==0
339 || strcasecmp(type
, "mdgradient")==0
340 || strcasecmp(type
, "mhgradient")==0) {
349 colors
= malloc(sizeof(RColor
*)*(count
-1));
351 wwarning("out of memory while parsing texture");
354 memset(colors
, 0, sizeof(RColor
*)*(count
-1));
356 for (i
= 2; i
< count
; i
++) {
357 val
= WMGetFromPLArray(texarray
, i
);
358 if (!WMIsPLString(val
)) {
359 wwarning("could not parse texture %s", text
);
361 for (j
= 0; colors
[j
]!=NULL
; j
++)
366 tmp
= WMGetFromPLString(val
);
368 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
369 wwarning("could not parse color %s in texture %s",
372 for (j
= 0; colors
[j
]!=NULL
; j
++)
377 if (!(colors
[i
-2] = malloc(sizeof(RColor
)))) {
378 wwarning("out of memory while parsing texture");
380 for (j
= 0; colors
[j
]!=NULL
; j
++)
386 colors
[i
-2]->red
= color
.red
>> 8;
387 colors
[i
-2]->green
= color
.green
>> 8;
388 colors
[i
-2]->blue
= color
.blue
>> 8;
394 gtype
= RHorizontalGradient
;
400 gtype
= RVerticalGradient
;
405 gtype
= RDiagonalGradient
;
411 image
= RRenderMultiGradient(iwidth
, iheight
, colors
, gtype
);
413 for (j
= 0; colors
[j
]!=NULL
; j
++)
418 wwarning("could not render gradient texture:%s",
419 RMessageForError(RErrorCode
));
423 if (!RConvertImage(rc
, image
, &pixmap
)) {
424 wwarning("could not convert texture:%s",
425 RMessageForError(RErrorCode
));
426 RReleaseImage(image
);
430 texture
->width
= image
->width
;
431 texture
->height
= image
->height
;
432 RReleaseImage(image
);
434 texture
->pixmap
= pixmap
;
435 } else if (strcasecmp(type
, "cpixmap")==0
436 || strcasecmp(type
, "spixmap")==0
437 || strcasecmp(type
, "mpixmap")==0
438 || strcasecmp(type
, "tpixmap")==0) {
440 Pixmap pixmap
= None
;
441 RImage
*image
= NULL
;
447 GETSTRORGOTO(val
, tmp
, 1, error
);
449 if (toupper(type[0]) == 'T' || toupper(type[0]) == 'C')
450 pixmap = LoadJPEG(rc, tmp, &iwidth, &iheight);
454 image
= loadImage(rc
, tmp
);
458 iwidth
= image
->width
;
459 iheight
= image
->height
;
462 GETSTRORGOTO(val
, tmp
, 2, error
);
464 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
465 wwarning("could not parse color %s in texture %s\n", tmp
, text
);
466 RReleaseImage(image
);
469 if (!XAllocColor(dpy
, DefaultColormap(dpy
, scr
), &color
)) {
470 rcolor
.red
= color
.red
>> 8;
471 rcolor
.green
= color
.green
>> 8;
472 rcolor
.blue
= color
.blue
>> 8;
473 RGetClosestXColor(rc
, &rcolor
, &color
);
479 /* for images with a transparent color */
480 if (image
->data
[3]) {
481 RCombineImageWithColor(image
, &rcolor
);
484 switch (toupper(type
[0])) {
486 texture
->width
= iwidth
;
487 texture
->height
= iheight
;
488 if (!pixmap
&& !RConvertImage(rc
, image
, &pixmap
)) {
489 wwarning("could not convert texture:%s",
490 RMessageForError(RErrorCode
));
491 RReleaseImage(image
);
495 RReleaseImage(image
);
500 if (toupper(type
[0])=='S') {
504 if (iwidth
*scrHeight
> iheight
*scrWidth
) {
506 h
= (scrWidth
*iheight
)/iwidth
;
509 w
= (scrHeight
*iwidth
)/iheight
;
512 if (w
!= image
->width
|| h
!= image
->height
) {
516 simage
= RSmoothScaleImage(image
, w
, h
);
518 simage
= RScaleImage(image
, w
, h
);
520 wwarning("could not scale image:%s",
521 RMessageForError(RErrorCode
));
522 RReleaseImage(image
);
525 RReleaseImage(image
);
528 iwidth
= image
->width
;
529 iheight
= image
->height
;
536 if (!pixmap
&& !RConvertImage(rc
, image
, &pixmap
)) {
537 wwarning("could not convert texture:%s",
538 RMessageForError(RErrorCode
));
539 RReleaseImage(image
);
543 if (iwidth
!= scrWidth
|| iheight
!= scrHeight
) {
544 int x
, y
, sx
, sy
, w
, h
;
546 cpixmap
= XCreatePixmap(dpy
, root
, scrWidth
, scrHeight
,
547 DefaultDepth(dpy
, scr
));
549 XSetForeground(dpy
, DefaultGC(dpy
, scr
), color
.pixel
);
550 XFillRectangle(dpy
, cpixmap
, DefaultGC(dpy
, scr
),
551 0, 0, scrWidth
, scrHeight
);
553 if (iheight
< scrHeight
) {
555 y
= (scrHeight
- h
)/2;
558 sy
= (iheight
- scrHeight
)/2;
562 if (iwidth
< scrWidth
) {
564 x
= (scrWidth
- w
)/2;
567 sx
= (iwidth
- scrWidth
)/2;
572 XCopyArea(dpy
, pixmap
, cpixmap
, DefaultGC(dpy
, scr
),
574 XFreePixmap(dpy
, pixmap
);
578 RReleaseImage(image
);
580 texture
->width
= scrWidth
;
581 texture
->height
= scrHeight
;
587 Pixmap tpixmap
= XCreatePixmap( dpy
, root
, scrWidth
, scrHeight
, DefaultDepth(dpy
, scr
));
588 XFillRectangle(dpy
, tpixmap
, DefaultGC(dpy
, scr
), 0, 0, scrWidth
, scrHeight
);
590 texture
->pixmap
= tpixmap
;
591 texture
->color
= color
;
592 texture
->width
= scrWidth
;
593 texture
->height
= scrHeight
;
598 for (i
=0; i
<xine_count
; ++i
) {
599 applyImage(rc
, texture
, image
, type
[0],
600 xine_screens
[i
].x_org
, xine_screens
[i
].y_org
,
601 xine_screens
[i
].width
, xine_screens
[i
].height
);
604 applyImage(rc
, texture
, image
, type
[0], 0, 0, scrWidth
, scrHeight
);
607 applyImage(rc
, texture
, image
, type
[0], 0, 0, scrWidth
, scrHeight
);
609 RReleaseImage(image
);
616 texture
->pixmap
= pixmap
;
617 texture
->color
= color
;
619 } else if (strcasecmp(type
, "thgradient")==0
620 || strcasecmp(type
, "tvgradient")==0
621 || strcasecmp(type
, "tdgradient")==0) {
623 RColor color1
, color2
;
633 GETSTRORGOTO(val
, file
, 1, error
);
635 GETSTRORGOTO(val
, tmp
, 2, error
);
639 GETSTRORGOTO(val
, tmp
, 3, error
);
641 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
642 wwarning("could not parse color %s in texture %s", tmp
, text
);
646 color1
.red
= color
.red
>> 8;
647 color1
.green
= color
.green
>> 8;
648 color1
.blue
= color
.blue
>> 8;
650 GETSTRORGOTO(val
, tmp
, 4, error
);
652 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
653 wwarning("could not parse color %s in texture %s", tmp
, text
);
657 color2
.red
= color
.red
>> 8;
658 color2
.green
= color
.green
>> 8;
659 color2
.blue
= color
.blue
>> 8;
661 image
= loadImage(rc
, file
);
669 gtype
= RHorizontalGradient
;
671 theight
= image
->height
> scrHeight
? scrHeight
: image
->height
;
675 gtype
= RVerticalGradient
;
676 twidth
= image
->width
> scrWidth
? scrWidth
: image
->width
;
680 gtype
= RDiagonalGradient
;
685 gradient
= RRenderGradient(twidth
, theight
, &color1
, &color2
, gtype
);
688 wwarning("could not render texture:%s",
689 RMessageForError(RErrorCode
));
690 RReleaseImage(gradient
);
691 RReleaseImage(image
);
695 tiled
= RMakeTiledImage(image
, twidth
, theight
);
697 wwarning("could not render texture:%s",
698 RMessageForError(RErrorCode
));
699 RReleaseImage(gradient
);
700 RReleaseImage(image
);
703 RReleaseImage(image
);
705 RCombineImagesWithOpaqueness(tiled
, gradient
, opaq
);
706 RReleaseImage(gradient
);
708 if (!RConvertImage(rc
, tiled
, &pixmap
)) {
709 wwarning("could not convert texture:%s",
710 RMessageForError(RErrorCode
));
711 RReleaseImage(tiled
);
714 texture
->width
= tiled
->width
;
715 texture
->height
= tiled
->height
;
717 RReleaseImage(tiled
);
719 texture
->pixmap
= pixmap
;
720 } else if (strcasecmp(type
, "function")==0) {
722 void (*initFunc
) (Display
*, Colormap
);
723 RImage
* (*mainFunc
) (int, char**, int, int, int);
727 char *lib
, *func
, **argv
= 0;
732 goto function_cleanup
;
734 /* get the library name */
735 GETSTRORGOTO(val
, lib
, 1, function_cleanup
);
737 /* get the function name */
738 GETSTRORGOTO(val
, func
, 2, function_cleanup
);
741 argv
= (char**)wmalloc(argc
* sizeof(char*));
743 /* get the parameters */
745 for (i
=0; i
<argc
-1; i
++) {
746 GETSTRORGOTO(val
, tmp
, 3+i
, function_cleanup
);
747 argv
[i
+1] = wstrdup(tmp
);
750 handle
= dlopen(lib
, RTLD_LAZY
);
752 wwarning("could not find library %s", lib
);
753 goto function_cleanup
;
756 initFunc
= dlsym(handle
, "initWindowMaker");
758 wwarning("could not initialize library %s", lib
);
759 goto function_cleanup
;
761 initFunc(dpy
, DefaultColormap(dpy
, scr
));
763 mainFunc
= dlsym(handle
, func
);
765 wwarning("could not find function %s::%s", lib
, func
);
766 goto function_cleanup
;
768 image
= mainFunc(argc
, argv
, scrWidth
, scrHeight
, 0);
770 if (!RConvertImage(rc
, image
, &pixmap
)) {
771 wwarning("could not convert texture:%s",
772 RMessageForError(RErrorCode
));
773 goto function_cleanup
;
775 texture
->width
= scrWidth
;
776 texture
->height
= scrHeight
;
777 texture
->pixmap
= pixmap
;
783 for (i
=0; i
<argc
; i
++) {
791 RReleaseImage(image
);
797 wwarning("function textures not supported");
801 wwarning("invalid texture type %s", text
);
805 texture
->spec
= wstrdup(text
);
813 WMReleasePropList(texarray
);
820 freeTexture(BackgroundTexture
*texture
)
822 if (texture
->solid
) {
825 pixel
[0] = texture
->color
.pixel
;
826 /* dont free black/white pixels */
827 if (pixel
[0]!=BlackPixelOfScreen(DefaultScreenOfDisplay(dpy
))
828 && pixel
[0]!=WhitePixelOfScreen(DefaultScreenOfDisplay(dpy
)))
829 XFreeColors(dpy
, DefaultColormap(dpy
, scr
), pixel
, 1, 0);
831 if (texture
->pixmap
) {
832 XFreePixmap(dpy
, texture
->pixmap
);
834 wfree(texture
->spec
);
840 setupTexture(RContext
*rc
, BackgroundTexture
**textures
, int *maxTextures
,
841 int workspace
, char *texture
)
843 BackgroundTexture
*newTexture
= NULL
;
846 /* unset the texture */
848 if (textures
[workspace
]!=NULL
) {
849 textures
[workspace
]->refcount
--;
851 if (textures
[workspace
]->refcount
== 0)
852 freeTexture(textures
[workspace
]);
854 textures
[workspace
] = NULL
;
858 if (textures
[workspace
]
859 && strcasecmp(textures
[workspace
]->spec
, texture
)==0) {
860 /* texture did not change */
864 /* check if the same texture is already created */
865 for (i
= 0; i
< *maxTextures
; i
++) {
866 if (textures
[i
] && strcasecmp(textures
[i
]->spec
, texture
)==0) {
867 newTexture
= textures
[i
];
873 /* create the texture */
874 newTexture
= parseTexture(rc
, texture
);
879 if (textures
[workspace
]!=NULL
) {
881 textures
[workspace
]->refcount
--;
883 if (textures
[workspace
]->refcount
== 0)
884 freeTexture(textures
[workspace
]);
887 newTexture
->refcount
++;
888 textures
[workspace
] = newTexture
;
890 if (*maxTextures
< workspace
)
891 *maxTextures
= workspace
;
897 duplicatePixmap(Pixmap pixmap
, int width
, int height
)
902 /* must open a new display or the RetainPermanent will
903 * leave stuff allocated in RContext unallocated after exit */
904 tmpDpy
= XOpenDisplay(display
);
906 wwarning("could not open display to update background image information");
912 copyP
= XCreatePixmap(tmpDpy
, root
, width
, height
,
913 DefaultDepth(tmpDpy
, scr
));
914 XCopyArea(tmpDpy
, pixmap
, copyP
, DefaultGC(tmpDpy
, scr
),
915 0, 0, width
, height
, 0, 0);
916 XSync(tmpDpy
, False
);
918 XSetCloseDownMode(tmpDpy
, RetainPermanent
);
919 XCloseDisplay(tmpDpy
);
927 dummyErrorHandler(Display
*dpy
, XErrorEvent
*err
)
933 setPixmapProperty(Pixmap pixmap
)
935 static Atom prop
= 0;
938 unsigned long length
, after
;
943 prop
= XInternAtom(dpy
, "_XROOTPMAP_ID", False
);
948 /* Clear out the old pixmap */
949 XGetWindowProperty(dpy
, root
, prop
, 0L, 1L, False
, AnyPropertyType
,
950 &type
, &format
, &length
, &after
, &data
);
952 if ((type
== XA_PIXMAP
) && (format
== 32) && (length
== 1)) {
953 XSetErrorHandler(dummyErrorHandler
);
954 XKillClient(dpy
, *((Pixmap
*)data
));
956 XSetErrorHandler(NULL
);
957 mode
= PropModeReplace
;
959 mode
= PropModeAppend
;
962 XChangeProperty(dpy
, root
, prop
, XA_PIXMAP
, 32, mode
,
963 (unsigned char *) &pixmap
, 1);
965 XDeleteProperty(dpy
, root
, prop
);
975 changeTexture(BackgroundTexture
*texture
)
981 if (texture
->solid
) {
982 XSetWindowBackground(dpy
, root
, texture
->color
.pixel
);
984 XSetWindowBackgroundPixmap(dpy
, root
, texture
->pixmap
);
986 XClearWindow(dpy
, root
);
993 pixmap
= duplicatePixmap(texture
->pixmap
, texture
->width
,
996 setPixmapProperty(pixmap
);
1002 readmsg(int fd
, unsigned char *buffer
, int size
)
1008 count
= read(fd
, buffer
, size
);
1022 * sizeSntexture_spec - sets the texture for workspace n
1023 * sizeCn - change background texture to the one for workspace n
1024 * sizePpath - set the pixmap search path
1027 * size = 4 bytes for length of the message data
1030 helperLoop(RContext
*rc
)
1032 BackgroundTexture
*textures
[WORKSPACE_COUNT
];
1033 int maxTextures
= 0;
1034 unsigned char buffer
[2048], buf
[8];
1038 memset(textures
, 0, WORKSPACE_COUNT
*sizeof(BackgroundTexture
*));
1044 /* get length of message */
1045 if (readmsg(0, buffer
, 4) < 0) {
1046 wsyserror("error reading message from Window Maker");
1048 if (errcount
== 0) {
1054 memcpy(buf
, buffer
, 4);
1059 if (readmsg(0, buffer
, size
) < 0) {
1060 wsyserror("error reading message from Window Maker");
1062 if (errcount
== 0) {
1069 printf("RECEIVED %s\n",buffer
);
1071 if (buffer
[0]!='P' && buffer
[0]!='K') {
1072 memcpy(buf
, &buffer
[1], 4);
1074 workspace
= atoi(buf
);
1075 if (workspace
< 0 || workspace
>= WORKSPACE_COUNT
) {
1076 wwarning("received message with invalid workspace number %i\n",
1082 switch (buffer
[0]) {
1085 printf("set texture %s\n", &buffer
[5]);
1087 setupTexture(rc
, textures
, &maxTextures
, workspace
, &buffer
[5]);
1092 printf("change texture %i\n", workspace
);
1094 if (!textures
[workspace
]) {
1095 changeTexture(textures
[0]);
1097 changeTexture(textures
[workspace
]);
1103 printf("change pixmappath %s\n", &buffer
[1]);
1107 PixmapPath
= wstrdup(&buffer
[1]);
1112 printf("unset workspace %i\n", workspace
);
1114 setupTexture(rc
, textures
, &maxTextures
, workspace
, NULL
);
1119 printf("exit command\n");
1124 wwarning("unknown message received");
1132 updateDomain(char *domain
, char *key
, char *texture
)
1134 char *program
= "wdwrite";
1136 /* here is a mem leak */
1137 system(wstrconcat("wdwrite ",
1138 wstrconcat(domain
, smooth
? " SmoothWorkspaceBack YES"
1139 : " SmoothWorkspaceBack NO")));
1141 execlp(program
, program
, domain
, key
, texture
, NULL
);
1142 wwarning("warning could not run \"%s\"", program
);
1148 globalDefaultsPathForDomain(char *domain
)
1152 sprintf(path
, "%s/WindowMaker/%s", SYSCONFDIR
, domain
);
1154 return wstrdup(path
);
1159 getValueForKey(char *domain
, char *keyName
)
1162 WMPropList
*key
, *val
, *d
;
1164 key
= WMCreatePLString(keyName
);
1166 /* try to find PixmapPath in user defaults */
1167 path
= wdefaultspathfordomain(domain
);
1168 d
= WMReadPropListFromFile(path
);
1170 wwarning("could not open domain file %s", path
);
1174 if (d
&& !WMIsPLDictionary(d
)) {
1175 WMReleasePropList(d
);
1179 val
= WMGetFromPLDictionary(d
, key
);
1183 /* try to find PixmapPath in global defaults */
1185 path
= globalDefaultsPathForDomain(domain
);
1187 wwarning("could not locate file for domain %s", domain
);
1190 d
= WMReadPropListFromFile(path
);
1194 if (d
&& !WMIsPLDictionary(d
)) {
1195 WMReleasePropList(d
);
1199 val
= WMGetFromPLDictionary(d
, key
);
1207 WMRetainPropList(val
);
1209 WMReleasePropList(key
);
1211 WMReleasePropList(d
);
1219 getPixmapPath(char *domain
)
1225 val
= getValueForKey(domain
, "PixmapPath");
1227 if (!val
|| !WMIsPLArray(val
)) {
1229 WMReleasePropList(val
);
1233 count
= WMGetPropListItemCount(val
);
1235 for (i
=0; i
<count
; i
++) {
1238 v
= WMGetFromPLArray(val
, i
);
1239 if (!v
|| !WMIsPLString(v
)) {
1242 len
+= strlen(WMGetFromPLString(v
))+1;
1245 ptr
= data
= wmalloc(len
+1);
1248 for (i
=0; i
<count
; i
++) {
1251 v
= WMGetFromPLArray(val
, i
);
1252 if (!v
|| !WMIsPLString(v
)) {
1255 strcpy(ptr
, WMGetFromPLString(v
));
1257 ptr
+= strlen(WMGetFromPLString(v
));
1262 ptr
--; *(ptr
--) = 0;
1264 WMReleasePropList(val
);
1271 getFullPixmapPath(char *file
)
1275 if (!PixmapPath
|| !(tmp
= wfindfile(PixmapPath
, file
))) {
1277 char *path
= wmalloc(bsize
);
1279 while (!getcwd(path
, bsize
)) {
1281 path
= wrealloc(path
, bsize
);
1284 tmp
= wstrconcat(path
, "/");
1286 path
= wstrconcat(tmp
, file
);
1292 /* the file is in the PixmapPath */
1295 return wstrdup(file
);
1310 print_help(char *ProgName
)
1312 printf("Usage: %s [options] [image]\n", ProgName
);
1313 puts("Sets the workspace background to the specified image or a texture and optionally update Window Maker configuration");
1315 #define P(m) puts(m)
1316 P(" -display display to use");
1317 P(" -d, --dither dither image");
1318 P(" -m, --match match colors");
1319 P(" -S, --smooth smooth scaled image");
1320 P(" -b, --back-color <color> background color");
1321 P(" -t, --tile tile image");
1322 P(" -e, --center center image");
1323 P(" -s, --scale scale image (default)");
1324 P(" -a, --maxscale scale image and keep aspect ratio");
1325 P(" -u, --update-wmaker update WindowMaker domain database");
1326 P(" -D, --update-domain <domain> update <domain> database");
1327 P(" -c, --colors <cpc> colors per channel to use");
1328 P(" -p, --parse <texture> proplist style texture specification");
1329 P(" -w, --workspace <workspace> update background for the specified workspace");
1330 P(" --version show version of wmsetbg and exit");
1331 P(" --help show this help and exit");
1338 changeTextureForWorkspace(char *domain
, char *texture
, int workspace
)
1340 WMPropList
*array
, *val
;
1344 val
= WMCreatePropListFromDescription(texture
);
1346 wwarning("could not parse texture %s", texture
);
1350 array
= getValueForKey("WindowMaker", "WorkspaceSpecificBack");
1353 array
= WMCreatePLArray(NULL
, NULL
);
1356 j
= WMGetPropListItemCount(array
);
1357 if (workspace
>= j
) {
1360 empty
= WMCreatePLArray(NULL
, NULL
);
1362 while (j
++ < workspace
-1) {
1363 WMAddToPLArray(array
, empty
);
1365 WMAddToPLArray(array
, val
);
1367 WMDeleteFromPLArray(array
, workspace
);
1368 WMInsertInPLArray(array
, workspace
, val
);
1371 value
= WMGetPropListDescription(array
, False
);
1372 updateDomain(domain
, "WorkspaceSpecificBack", value
);
1377 main(int argc
, char **argv
)
1382 RContextAttributes rattr
;
1383 char *style
= "spixmap";
1384 char *back_color
= "gray20";
1385 char *image_name
= NULL
;
1386 char *domain
= "WindowMaker";
1387 int update
=0, cpc
=4, render_mode
=RDitheredRendering
, obey_user
=0;
1388 char *texture
= NULL
;
1391 signal(SIGINT
, SIG_DFL
);
1392 signal(SIGTERM
, SIG_DFL
);
1393 signal(SIGQUIT
, SIG_DFL
);
1394 signal(SIGSEGV
, SIG_DFL
);
1395 signal(SIGBUS
, SIG_DFL
);
1396 signal(SIGFPE
, SIG_DFL
);
1397 signal(SIGABRT
, SIG_DFL
);
1398 signal(SIGHUP
, SIG_DFL
);
1399 signal(SIGPIPE
, SIG_DFL
);
1400 signal(SIGCHLD
, SIG_DFL
);
1402 WMInitializeApplication("wmsetbg", &argc
, argv
);
1404 for (i
=1; i
<argc
; i
++) {
1405 if (strcmp(argv
[i
], "-helper")==0) {
1407 } else if (strcmp(argv
[i
], "-display")==0) {
1410 wfatal("too few arguments for %s\n", argv
[i
-1]);
1414 } else if (strcmp(argv
[i
], "-s")==0
1415 || strcmp(argv
[i
], "--scale")==0) {
1417 } else if (strcmp(argv
[i
], "-t")==0
1418 || strcmp(argv
[i
], "--tile")==0) {
1420 } else if (strcmp(argv
[i
], "-e")==0
1421 || strcmp(argv
[i
], "--center")==0) {
1423 } else if (strcmp(argv
[i
], "-a")==0
1424 || strcmp(argv
[i
], "--maxscale")==0) {
1426 } else if (strcmp(argv
[i
], "-d")==0
1427 || strcmp(argv
[i
], "--dither")==0) {
1428 render_mode
= RDitheredRendering
;
1430 } else if (strcmp(argv
[i
], "-m")==0
1431 || strcmp(argv
[i
], "--match")==0) {
1432 render_mode
= RBestMatchRendering
;
1434 } else if (strcmp(argv
[i
], "-S")==0
1435 || strcmp(argv
[i
], "--smooth")==0) {
1437 } else if (strcmp(argv
[i
], "-u")==0
1438 || strcmp(argv
[i
], "--update-wmaker")==0) {
1440 } else if (strcmp(argv
[i
], "-D")==0
1441 || strcmp(argv
[i
], "--update-domain")==0) {
1445 wfatal("too few arguments for %s\n", argv
[i
-1]);
1448 domain
= wstrdup(argv
[i
]);
1449 } else if (strcmp(argv
[i
], "-c")==0
1450 || strcmp(argv
[i
], "--colors")==0) {
1453 wfatal("too few arguments for %s\n", argv
[i
-1]);
1456 if (sscanf(argv
[i
], "%i", &cpc
)!=1) {
1457 wfatal("bad value for colors per channel: \"%s\"\n", argv
[i
]);
1460 } else if (strcmp(argv
[i
], "-b")==0
1461 || strcmp(argv
[i
], "--back-color")==0) {
1464 wfatal("too few arguments for %s\n", argv
[i
-1]);
1467 back_color
= argv
[i
];
1468 } else if (strcmp(argv
[i
], "-p")==0
1469 || strcmp(argv
[i
], "--parse")==0) {
1472 wfatal("too few arguments for %s\n", argv
[i
-1]);
1476 } else if (strcmp(argv
[i
], "-w")==0
1477 || strcmp(argv
[i
], "--workspace")==0) {
1480 wfatal("too few arguments for %s\n", argv
[i
-1]);
1483 if (sscanf(argv
[i
], "%i", &workspace
)!=1) {
1484 wfatal("bad value for workspace number: \"%s\"",
1488 } else if (strcmp(argv
[i
], "--version")==0) {
1490 printf(PROG_VERSION
);
1493 } else if (strcmp(argv
[i
], "--help")==0) {
1494 print_help(argv
[0]);
1496 } else if (argv
[i
][0] != '-') {
1497 image_name
= argv
[i
];
1499 printf("%s: invalid argument '%s'\n", argv
[0], argv
[i
]);
1500 printf("Try '%s --help' for more information\n", argv
[0]);
1504 if (!image_name
&& !texture
&& !helperMode
) {
1505 printf("%s: you must specify a image file name or a texture\n",
1507 printf("Try '%s --help' for more information\n", argv
[0]);
1512 PixmapPath
= getPixmapPath(domain
);
1515 #if 0 /* some problem with Alpha... TODO: check if its right */
1516 val
= WMGetFromPLDictionary(domain
,
1517 WMCreatePLString("SmoothWorkspaceBack"));
1519 val
= getValueForKey(domain
, "SmoothWorkspaceBack");
1522 if (val
&& WMIsPLString(val
) && strcasecmp(WMGetFromPLString(val
), "YES")==0)
1526 dpy
= XOpenDisplay(display
);
1528 wfatal("could not open display");
1532 XSynchronize(dpy
, 1);
1535 root
= DefaultRootWindow(dpy
);
1537 scr
= DefaultScreen(dpy
);
1539 scrWidth
= WidthOfScreen(DefaultScreenOfDisplay(dpy
));
1540 scrHeight
= HeightOfScreen(DefaultScreenOfDisplay(dpy
));
1544 xine_screens
= XineramaQueryScreens(dpy
, &xine_count
);
1548 if (!obey_user
&& DefaultDepth(dpy
, scr
) <= 8)
1549 render_mode
= RDitheredRendering
;
1551 rattr
.flags
= RC_RenderMode
| RC_ColorsPerChannel
1552 | RC_StandardColormap
| RC_DefaultVisual
;
1553 rattr
.render_mode
= render_mode
;
1554 rattr
.colors_per_channel
= cpc
;
1555 rattr
.standard_colormap_mode
= RCreateStdColormap
;
1557 rc
= RCreateContext(dpy
, scr
, &rattr
);
1560 rattr
.standard_colormap_mode
= RIgnoreStdColormap
;
1561 rc
= RCreateContext(dpy
, scr
, &rattr
);
1565 wfatal("could not initialize wrlib: %s",
1566 RMessageForError(RErrorCode
));
1571 /* lower priority, so that it wont use all the CPU */
1576 BackgroundTexture
*tex
;
1580 char *image_path
= getFullPixmapPath(image_name
);
1582 sprintf(buffer
, "(%s, \"%s\", %s)", style
, image_path
, back_color
);
1584 texture
= (char*)buffer
;
1587 if (update
&& workspace
< 0) {
1588 updateDomain(domain
, "WorkspaceBack", texture
);
1591 tex
= parseTexture(rc
, texture
);
1598 /* always update domain */
1599 changeTextureForWorkspace(domain
, texture
, workspace
);