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
)) {
137 if (toupper(type
) == 'S') {
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
);
497 texture
->pixmap
= pixmap
;
498 texture
->color
= color
;
504 Pixmap tpixmap
= XCreatePixmap( dpy
, root
, scrWidth
, scrHeight
, DefaultDepth(dpy
, scr
));
505 XFillRectangle(dpy
, tpixmap
, DefaultGC(dpy
, scr
), 0, 0, scrWidth
, scrHeight
);
507 texture
->pixmap
= tpixmap
;
508 texture
->color
= color
;
509 texture
->width
= scrWidth
;
510 texture
->height
= scrHeight
;
515 for (i
=0; i
<xine_count
; ++i
) {
516 applyImage(rc
, texture
, image
, type
[0],
517 xine_screens
[i
].x_org
, xine_screens
[i
].y_org
,
518 xine_screens
[i
].width
, xine_screens
[i
].height
);
521 applyImage(rc
, texture
, image
, type
[0], 0, 0, scrWidth
, scrHeight
);
523 #else /* !XINERAMA */
524 applyImage(rc
, texture
, image
, type
[0], 0, 0, scrWidth
, scrHeight
);
525 #endif /* !XINERAMA */
526 RReleaseImage(image
);
530 } else if (strcasecmp(type
, "thgradient")==0
531 || strcasecmp(type
, "tvgradient")==0
532 || strcasecmp(type
, "tdgradient")==0) {
534 RColor color1
, color2
;
544 GETSTRORGOTO(val
, file
, 1, error
);
546 GETSTRORGOTO(val
, tmp
, 2, error
);
550 GETSTRORGOTO(val
, tmp
, 3, error
);
552 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
553 wwarning("could not parse color %s in texture %s", tmp
, text
);
557 color1
.red
= color
.red
>> 8;
558 color1
.green
= color
.green
>> 8;
559 color1
.blue
= color
.blue
>> 8;
561 GETSTRORGOTO(val
, tmp
, 4, error
);
563 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
564 wwarning("could not parse color %s in texture %s", tmp
, text
);
568 color2
.red
= color
.red
>> 8;
569 color2
.green
= color
.green
>> 8;
570 color2
.blue
= color
.blue
>> 8;
572 image
= loadImage(rc
, file
);
580 gtype
= RHorizontalGradient
;
582 theight
= image
->height
> scrHeight
? scrHeight
: image
->height
;
586 gtype
= RVerticalGradient
;
587 twidth
= image
->width
> scrWidth
? scrWidth
: image
->width
;
591 gtype
= RDiagonalGradient
;
596 gradient
= RRenderGradient(twidth
, theight
, &color1
, &color2
, gtype
);
599 wwarning("could not render texture:%s",
600 RMessageForError(RErrorCode
));
601 RReleaseImage(gradient
);
602 RReleaseImage(image
);
606 tiled
= RMakeTiledImage(image
, twidth
, theight
);
608 wwarning("could not render texture:%s",
609 RMessageForError(RErrorCode
));
610 RReleaseImage(gradient
);
611 RReleaseImage(image
);
614 RReleaseImage(image
);
616 RCombineImagesWithOpaqueness(tiled
, gradient
, opaq
);
617 RReleaseImage(gradient
);
619 if (!RConvertImage(rc
, tiled
, &pixmap
)) {
620 wwarning("could not convert texture:%s",
621 RMessageForError(RErrorCode
));
622 RReleaseImage(tiled
);
625 texture
->width
= tiled
->width
;
626 texture
->height
= tiled
->height
;
628 RReleaseImage(tiled
);
630 texture
->pixmap
= pixmap
;
631 } else if (strcasecmp(type
, "function")==0) {
633 void (*initFunc
) (Display
*, Colormap
);
634 RImage
* (*mainFunc
) (int, char**, int, int, int);
638 char *lib
, *func
, **argv
= 0;
643 goto function_cleanup
;
645 /* get the library name */
646 GETSTRORGOTO(val
, lib
, 1, function_cleanup
);
648 /* get the function name */
649 GETSTRORGOTO(val
, func
, 2, function_cleanup
);
652 argv
= (char**)wmalloc(argc
* sizeof(char*));
654 /* get the parameters */
656 for (i
=0; i
<argc
-1; i
++) {
657 GETSTRORGOTO(val
, tmp
, 3+i
, function_cleanup
);
658 argv
[i
+1] = wstrdup(tmp
);
661 handle
= dlopen(lib
, RTLD_LAZY
);
663 wwarning("could not find library %s", lib
);
664 goto function_cleanup
;
667 initFunc
= dlsym(handle
, "initWindowMaker");
669 wwarning("could not initialize library %s", lib
);
670 goto function_cleanup
;
672 initFunc(dpy
, DefaultColormap(dpy
, scr
));
674 mainFunc
= dlsym(handle
, func
);
676 wwarning("could not find function %s::%s", lib
, func
);
677 goto function_cleanup
;
679 image
= mainFunc(argc
, argv
, scrWidth
, scrHeight
, 0);
681 if (!RConvertImage(rc
, image
, &pixmap
)) {
682 wwarning("could not convert texture:%s",
683 RMessageForError(RErrorCode
));
684 goto function_cleanup
;
686 texture
->width
= scrWidth
;
687 texture
->height
= scrHeight
;
688 texture
->pixmap
= pixmap
;
694 for (i
=0; i
<argc
; i
++) {
702 RReleaseImage(image
);
708 wwarning("function textures not supported");
712 wwarning("invalid texture type %s", text
);
716 texture
->spec
= wstrdup(text
);
724 WMReleasePropList(texarray
);
731 freeTexture(BackgroundTexture
*texture
)
733 if (texture
->solid
) {
736 pixel
[0] = texture
->color
.pixel
;
737 /* dont free black/white pixels */
738 if (pixel
[0]!=BlackPixelOfScreen(DefaultScreenOfDisplay(dpy
))
739 && pixel
[0]!=WhitePixelOfScreen(DefaultScreenOfDisplay(dpy
)))
740 XFreeColors(dpy
, DefaultColormap(dpy
, scr
), pixel
, 1, 0);
742 if (texture
->pixmap
) {
743 XFreePixmap(dpy
, texture
->pixmap
);
745 wfree(texture
->spec
);
751 setupTexture(RContext
*rc
, BackgroundTexture
**textures
, int *maxTextures
,
752 int workspace
, char *texture
)
754 BackgroundTexture
*newTexture
= NULL
;
757 /* unset the texture */
759 if (textures
[workspace
]!=NULL
) {
760 textures
[workspace
]->refcount
--;
762 if (textures
[workspace
]->refcount
== 0)
763 freeTexture(textures
[workspace
]);
765 textures
[workspace
] = NULL
;
769 if (textures
[workspace
]
770 && strcasecmp(textures
[workspace
]->spec
, texture
)==0) {
771 /* texture did not change */
775 /* check if the same texture is already created */
776 for (i
= 0; i
< *maxTextures
; i
++) {
777 if (textures
[i
] && strcasecmp(textures
[i
]->spec
, texture
)==0) {
778 newTexture
= textures
[i
];
784 /* create the texture */
785 newTexture
= parseTexture(rc
, texture
);
790 if (textures
[workspace
]!=NULL
) {
792 textures
[workspace
]->refcount
--;
794 if (textures
[workspace
]->refcount
== 0)
795 freeTexture(textures
[workspace
]);
798 newTexture
->refcount
++;
799 textures
[workspace
] = newTexture
;
801 if (*maxTextures
< workspace
)
802 *maxTextures
= workspace
;
808 duplicatePixmap(Pixmap pixmap
, int width
, int height
)
813 /* must open a new display or the RetainPermanent will
814 * leave stuff allocated in RContext unallocated after exit */
815 tmpDpy
= XOpenDisplay(display
);
817 wwarning("could not open display to update background image information");
823 copyP
= XCreatePixmap(tmpDpy
, root
, width
, height
,
824 DefaultDepth(tmpDpy
, scr
));
825 XCopyArea(tmpDpy
, pixmap
, copyP
, DefaultGC(tmpDpy
, scr
),
826 0, 0, width
, height
, 0, 0);
827 XSync(tmpDpy
, False
);
829 XSetCloseDownMode(tmpDpy
, RetainPermanent
);
830 XCloseDisplay(tmpDpy
);
838 dummyErrorHandler(Display
*dpy
, XErrorEvent
*err
)
844 setPixmapProperty(Pixmap pixmap
)
846 static Atom prop
= 0;
849 unsigned long length
, after
;
854 prop
= XInternAtom(dpy
, "_XROOTPMAP_ID", False
);
859 /* Clear out the old pixmap */
860 XGetWindowProperty(dpy
, root
, prop
, 0L, 1L, False
, AnyPropertyType
,
861 &type
, &format
, &length
, &after
, &data
);
863 if ((type
== XA_PIXMAP
) && (format
== 32) && (length
== 1)) {
864 XSetErrorHandler(dummyErrorHandler
);
865 XKillClient(dpy
, *((Pixmap
*)data
));
867 XSetErrorHandler(NULL
);
868 mode
= PropModeReplace
;
870 mode
= PropModeAppend
;
873 XChangeProperty(dpy
, root
, prop
, XA_PIXMAP
, 32, mode
,
874 (unsigned char *) &pixmap
, 1);
876 XDeleteProperty(dpy
, root
, prop
);
886 changeTexture(BackgroundTexture
*texture
)
892 if (texture
->solid
) {
893 XSetWindowBackground(dpy
, root
, texture
->color
.pixel
);
895 XSetWindowBackgroundPixmap(dpy
, root
, texture
->pixmap
);
897 XClearWindow(dpy
, root
);
904 pixmap
= duplicatePixmap(texture
->pixmap
, texture
->width
,
907 setPixmapProperty(pixmap
);
913 readmsg(int fd
, unsigned char *buffer
, int size
)
919 count
= read(fd
, buffer
, size
);
933 * sizeSntexture_spec - sets the texture for workspace n
934 * sizeCn - change background texture to the one for workspace n
935 * sizePpath - set the pixmap search path
938 * size = 4 bytes for length of the message data
941 helperLoop(RContext
*rc
)
943 BackgroundTexture
*textures
[WORKSPACE_COUNT
];
945 unsigned char buffer
[2048], buf
[8];
949 memset(textures
, 0, WORKSPACE_COUNT
*sizeof(BackgroundTexture
*));
955 /* get length of message */
956 if (readmsg(0, buffer
, 4) < 0) {
957 wsyserror("error reading message from Window Maker");
965 memcpy(buf
, buffer
, 4);
970 if (readmsg(0, buffer
, size
) < 0) {
971 wsyserror("error reading message from Window Maker");
980 printf("RECEIVED %s\n",buffer
);
982 if (buffer
[0]!='P' && buffer
[0]!='K') {
983 memcpy(buf
, &buffer
[1], 4);
985 workspace
= atoi(buf
);
986 if (workspace
< 0 || workspace
>= WORKSPACE_COUNT
) {
987 wwarning("received message with invalid workspace number %i\n",
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");
1043 updateDomain(char *domain
, char *key
, char *texture
)
1045 char *program
= "wdwrite";
1047 /* here is a mem leak */
1048 system(wstrconcat("wdwrite ",
1049 wstrconcat(domain
, smooth
? " SmoothWorkspaceBack YES"
1050 : " SmoothWorkspaceBack NO")));
1052 execlp(program
, program
, domain
, key
, texture
, NULL
);
1053 wwarning("warning could not run \"%s\"", program
);
1059 globalDefaultsPathForDomain(char *domain
)
1063 sprintf(path
, "%s/WindowMaker/%s", SYSCONFDIR
, domain
);
1065 return wstrdup(path
);
1070 getValueForKey(char *domain
, char *keyName
)
1073 WMPropList
*key
, *val
, *d
;
1075 key
= WMCreatePLString(keyName
);
1077 /* try to find PixmapPath in user defaults */
1078 path
= wdefaultspathfordomain(domain
);
1079 d
= WMReadPropListFromFile(path
);
1081 wwarning("could not open domain file %s", path
);
1085 if (d
&& !WMIsPLDictionary(d
)) {
1086 WMReleasePropList(d
);
1090 val
= WMGetFromPLDictionary(d
, key
);
1094 /* try to find PixmapPath in global defaults */
1096 path
= globalDefaultsPathForDomain(domain
);
1098 wwarning("could not locate file for domain %s", domain
);
1101 d
= WMReadPropListFromFile(path
);
1105 if (d
&& !WMIsPLDictionary(d
)) {
1106 WMReleasePropList(d
);
1110 val
= WMGetFromPLDictionary(d
, key
);
1118 WMRetainPropList(val
);
1120 WMReleasePropList(key
);
1122 WMReleasePropList(d
);
1130 getPixmapPath(char *domain
)
1136 val
= getValueForKey(domain
, "PixmapPath");
1138 if (!val
|| !WMIsPLArray(val
)) {
1140 WMReleasePropList(val
);
1144 count
= WMGetPropListItemCount(val
);
1146 for (i
=0; i
<count
; i
++) {
1149 v
= WMGetFromPLArray(val
, i
);
1150 if (!v
|| !WMIsPLString(v
)) {
1153 len
+= strlen(WMGetFromPLString(v
))+1;
1156 ptr
= data
= wmalloc(len
+1);
1159 for (i
=0; i
<count
; i
++) {
1162 v
= WMGetFromPLArray(val
, i
);
1163 if (!v
|| !WMIsPLString(v
)) {
1166 strcpy(ptr
, WMGetFromPLString(v
));
1168 ptr
+= strlen(WMGetFromPLString(v
));
1173 ptr
--; *(ptr
--) = 0;
1175 WMReleasePropList(val
);
1182 getFullPixmapPath(char *file
)
1186 if (!PixmapPath
|| !(tmp
= wfindfile(PixmapPath
, file
))) {
1188 char *path
= wmalloc(bsize
);
1190 while (!getcwd(path
, bsize
)) {
1192 path
= wrealloc(path
, bsize
);
1195 tmp
= wstrconcat(path
, "/");
1197 path
= wstrconcat(tmp
, file
);
1203 /* the file is in the PixmapPath */
1206 return wstrdup(file
);
1221 print_help(char *ProgName
)
1223 printf("Usage: %s [options] [image]\n", ProgName
);
1224 puts("Sets the workspace background to the specified image or a texture and optionally update Window Maker configuration");
1226 #define P(m) puts(m)
1227 P(" -display display to use");
1228 P(" -d, --dither dither image");
1229 P(" -m, --match match colors");
1230 P(" -S, --smooth smooth scaled image");
1231 P(" -b, --back-color <color> background color");
1232 P(" -t, --tile tile image");
1233 P(" -e, --center center image");
1234 P(" -s, --scale scale image (default)");
1235 P(" -a, --maxscale scale image and keep aspect ratio");
1236 P(" -u, --update-wmaker update WindowMaker domain database");
1237 P(" -D, --update-domain <domain> update <domain> database");
1238 P(" -c, --colors <cpc> colors per channel to use");
1239 P(" -p, --parse <texture> proplist style texture specification");
1240 P(" -w, --workspace <workspace> update background for the specified workspace");
1241 P(" --version show version of wmsetbg and exit");
1242 P(" --help show this help and exit");
1249 changeTextureForWorkspace(char *domain
, char *texture
, int workspace
)
1251 WMPropList
*array
, *val
;
1255 val
= WMCreatePropListFromDescription(texture
);
1257 wwarning("could not parse texture %s", texture
);
1261 array
= getValueForKey("WindowMaker", "WorkspaceSpecificBack");
1264 array
= WMCreatePLArray(NULL
, NULL
);
1267 j
= WMGetPropListItemCount(array
);
1268 if (workspace
>= j
) {
1271 empty
= WMCreatePLArray(NULL
, NULL
);
1273 while (j
++ < workspace
-1) {
1274 WMAddToPLArray(array
, empty
);
1276 WMAddToPLArray(array
, val
);
1278 WMDeleteFromPLArray(array
, workspace
);
1279 WMInsertInPLArray(array
, workspace
, val
);
1282 value
= WMGetPropListDescription(array
, False
);
1283 updateDomain(domain
, "WorkspaceSpecificBack", value
);
1288 main(int argc
, char **argv
)
1293 RContextAttributes rattr
;
1294 char *style
= "spixmap";
1295 char *back_color
= "gray20";
1296 char *image_name
= NULL
;
1297 char *domain
= "WindowMaker";
1298 int update
=0, cpc
=4, render_mode
=RDitheredRendering
, obey_user
=0;
1299 char *texture
= NULL
;
1302 signal(SIGINT
, SIG_DFL
);
1303 signal(SIGTERM
, SIG_DFL
);
1304 signal(SIGQUIT
, SIG_DFL
);
1305 signal(SIGSEGV
, SIG_DFL
);
1306 signal(SIGBUS
, SIG_DFL
);
1307 signal(SIGFPE
, SIG_DFL
);
1308 signal(SIGABRT
, SIG_DFL
);
1309 signal(SIGHUP
, SIG_DFL
);
1310 signal(SIGPIPE
, SIG_DFL
);
1311 signal(SIGCHLD
, SIG_DFL
);
1313 WMInitializeApplication("wmsetbg", &argc
, argv
);
1315 for (i
=1; i
<argc
; i
++) {
1316 if (strcmp(argv
[i
], "-helper")==0) {
1318 } else if (strcmp(argv
[i
], "-display")==0) {
1321 wfatal("too few arguments for %s\n", argv
[i
-1]);
1325 } else if (strcmp(argv
[i
], "-s")==0
1326 || strcmp(argv
[i
], "--scale")==0) {
1328 } else if (strcmp(argv
[i
], "-t")==0
1329 || strcmp(argv
[i
], "--tile")==0) {
1331 } else if (strcmp(argv
[i
], "-e")==0
1332 || strcmp(argv
[i
], "--center")==0) {
1334 } else if (strcmp(argv
[i
], "-a")==0
1335 || strcmp(argv
[i
], "--maxscale")==0) {
1337 } else if (strcmp(argv
[i
], "-d")==0
1338 || strcmp(argv
[i
], "--dither")==0) {
1339 render_mode
= RDitheredRendering
;
1341 } else if (strcmp(argv
[i
], "-m")==0
1342 || strcmp(argv
[i
], "--match")==0) {
1343 render_mode
= RBestMatchRendering
;
1345 } else if (strcmp(argv
[i
], "-S")==0
1346 || strcmp(argv
[i
], "--smooth")==0) {
1348 } else if (strcmp(argv
[i
], "-u")==0
1349 || strcmp(argv
[i
], "--update-wmaker")==0) {
1351 } else if (strcmp(argv
[i
], "-D")==0
1352 || strcmp(argv
[i
], "--update-domain")==0) {
1356 wfatal("too few arguments for %s\n", argv
[i
-1]);
1359 domain
= wstrdup(argv
[i
]);
1360 } else if (strcmp(argv
[i
], "-c")==0
1361 || strcmp(argv
[i
], "--colors")==0) {
1364 wfatal("too few arguments for %s\n", argv
[i
-1]);
1367 if (sscanf(argv
[i
], "%i", &cpc
)!=1) {
1368 wfatal("bad value for colors per channel: \"%s\"\n", argv
[i
]);
1371 } else if (strcmp(argv
[i
], "-b")==0
1372 || strcmp(argv
[i
], "--back-color")==0) {
1375 wfatal("too few arguments for %s\n", argv
[i
-1]);
1378 back_color
= argv
[i
];
1379 } else if (strcmp(argv
[i
], "-p")==0
1380 || strcmp(argv
[i
], "--parse")==0) {
1383 wfatal("too few arguments for %s\n", argv
[i
-1]);
1387 } else if (strcmp(argv
[i
], "-w")==0
1388 || strcmp(argv
[i
], "--workspace")==0) {
1391 wfatal("too few arguments for %s\n", argv
[i
-1]);
1394 if (sscanf(argv
[i
], "%i", &workspace
)!=1) {
1395 wfatal("bad value for workspace number: \"%s\"",
1399 } else if (strcmp(argv
[i
], "--version")==0) {
1401 printf(PROG_VERSION
);
1404 } else if (strcmp(argv
[i
], "--help")==0) {
1405 print_help(argv
[0]);
1407 } else if (argv
[i
][0] != '-') {
1408 image_name
= argv
[i
];
1410 printf("%s: invalid argument '%s'\n", argv
[0], argv
[i
]);
1411 printf("Try '%s --help' for more information\n", argv
[0]);
1415 if (!image_name
&& !texture
&& !helperMode
) {
1416 printf("%s: you must specify a image file name or a texture\n",
1418 printf("Try '%s --help' for more information\n", argv
[0]);
1423 PixmapPath
= getPixmapPath(domain
);
1426 #if 0 /* some problem with Alpha... TODO: check if its right */
1427 val
= WMGetFromPLDictionary(domain
,
1428 WMCreatePLString("SmoothWorkspaceBack"));
1430 val
= getValueForKey(domain
, "SmoothWorkspaceBack");
1433 if (val
&& WMIsPLString(val
) && strcasecmp(WMGetFromPLString(val
), "YES")==0)
1437 dpy
= XOpenDisplay(display
);
1439 wfatal("could not open display");
1443 XSynchronize(dpy
, 1);
1446 root
= DefaultRootWindow(dpy
);
1448 scr
= DefaultScreen(dpy
);
1450 scrWidth
= WidthOfScreen(DefaultScreenOfDisplay(dpy
));
1451 scrHeight
= HeightOfScreen(DefaultScreenOfDisplay(dpy
));
1455 xine_screens
= XineramaQueryScreens(dpy
, &xine_count
);
1459 if (!obey_user
&& DefaultDepth(dpy
, scr
) <= 8)
1460 render_mode
= RDitheredRendering
;
1462 rattr
.flags
= RC_RenderMode
| RC_ColorsPerChannel
1463 | RC_StandardColormap
| RC_DefaultVisual
;
1464 rattr
.render_mode
= render_mode
;
1465 rattr
.colors_per_channel
= cpc
;
1466 rattr
.standard_colormap_mode
= RCreateStdColormap
;
1468 rc
= RCreateContext(dpy
, scr
, &rattr
);
1471 rattr
.standard_colormap_mode
= RIgnoreStdColormap
;
1472 rc
= RCreateContext(dpy
, scr
, &rattr
);
1476 wfatal("could not initialize wrlib: %s",
1477 RMessageForError(RErrorCode
));
1482 /* lower priority, so that it wont use all the CPU */
1487 BackgroundTexture
*tex
;
1491 char *image_path
= getFullPixmapPath(image_name
);
1493 sprintf(buffer
, "(%s, \"%s\", %s)", style
, image_path
, back_color
);
1495 texture
= (char*)buffer
;
1498 if (update
&& workspace
< 0) {
1499 updateDomain(domain
, "WorkspaceBack", texture
);
1502 tex
= parseTexture(rc
, texture
);
1509 /* always update domain */
1510 changeTextureForWorkspace(domain
, texture
, workspace
);