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>
55 #include "../src/wconfig.h"
57 #include <WINGs/WINGs.h>
63 int count
; /* screen count, 0 = inactive */
67 #define PROG_VERSION "wmsetbg (Window Maker) 2.8"
70 #define WORKSPACE_COUNT (MAX_WORKSPACES+1)
82 WXineramaInfo xineInfo
;
87 Pixmap CurrentPixmap
= None
;
88 char *PixmapPath
= NULL
;
91 extern Pixmap
LoadJPEG(RContext
*rc
, char *file_name
, int *width
, int *height
);
94 typedef struct BackgroundTexture
{
102 Pixmap pixmap
; /* for all textures, including solid */
103 int width
; /* size of the pixmap */
112 xineInfo
.screens
= NULL
;
115 # ifdef SOLARIS_XINERAMA
116 if (XineramaGetState(dpy
, scr
)) {
117 XRectangle head
[MAXFRAMEBUFFERS
];
118 unsigned char hints
[MAXFRAMEBUFFERS
];
121 if (XineramaGetInfo(dpy
, scr
, head
, hints
,
124 xineInfo
.screens
= wmalloc(sizeof(WMRect
)*(xineInfo
.count
+1));
126 for (i
=0; i
<xineInfo
.count
; i
++) {
127 xineInfo
.screens
[i
].pos
.x
= head
[i
].x
;
128 xineInfo
.screens
[i
].pos
.y
= head
[i
].y
;
129 xineInfo
.screens
[i
].size
.width
= head
[i
].width
;
130 xineInfo
.screens
[i
].size
.height
= head
[i
].height
;
134 # else /* !SOLARIS_XINERAMA */
135 if (XineramaIsActive(dpy
)) {
136 XineramaScreenInfo
*xine_screens
;
139 xine_screens
= XineramaQueryScreens(dpy
, &xineInfo
.count
);
141 xineInfo
.screens
= wmalloc(sizeof(WMRect
)*(xineInfo
.count
+1));
143 for (i
=0; i
<xineInfo
.count
; i
++) {
144 xineInfo
.screens
[i
].pos
.x
= xine_screens
[i
].x_org
;
145 xineInfo
.screens
[i
].pos
.y
= xine_screens
[i
].y_org
;
146 xineInfo
.screens
[i
].size
.width
= xine_screens
[i
].width
;
147 xineInfo
.screens
[i
].size
.height
= xine_screens
[i
].height
;
151 # endif /* !SOLARIS_XINERAMA */
152 #endif /* XINERAMA */
157 loadImage(RContext
*rc
, char *file
)
162 if (access(file
, F_OK
)!=0) {
163 path
= wfindfile(PixmapPath
, file
);
165 wwarning("%s:could not find image file used in texture", file
);
169 path
= wstrdup(file
);
172 image
= RLoadImage(rc
, path
, 0);
174 wwarning("%s:could not load image file used in texture:%s", path
,
175 RMessageForError(RErrorCode
));
184 applyImage(RContext
*rc
, BackgroundTexture
*texture
, RImage
*image
, char type
,
185 int x
, int y
, int width
, int height
)
190 switch (toupper(type
)) {
193 if (toupper(type
) == 'S') {
197 if (image
->width
*height
> image
->height
*width
) {
199 h
= (width
*image
->height
) / image
->width
;
201 w
= (height
*image
->width
) / image
->height
;
206 if (w
!= image
->width
|| h
!= image
->height
) {
210 simage
= RSmoothScaleImage(image
, w
, h
);
212 simage
= RScaleImage(image
, w
, h
);
216 wwarning("could not scale image:%s", RMessageForError(RErrorCode
));
228 if (!RConvertImage(rc
, image
, &pixmap
)) {
229 wwarning("could not convert texture:%s", RMessageForError(RErrorCode
));
233 if (image
->width
!= width
|| image
->height
!= height
) {
236 if (image
->height
< height
) {
238 y
+= (height
- h
) / 2;
241 sy
= (image
->height
- height
) / 2;
244 if (image
->width
< width
) {
246 x
+= (width
- w
) / 2;
249 sx
= (image
->width
- width
) / 2;
253 XCopyArea(dpy
, pixmap
, texture
->pixmap
, DefaultGC(dpy
, scr
), sx
, sy
, w
, h
, x
, y
);
255 XCopyArea(dpy
, pixmap
, texture
->pixmap
, DefaultGC(dpy
, scr
), 0, 0, width
, height
, x
, y
);
257 XFreePixmap(dpy
, pixmap
);
259 RReleaseImage( image
);
268 parseTexture(RContext
*rc
, char *text
)
270 BackgroundTexture
*texture
= NULL
;
271 WMPropList
*texarray
;
277 #define GETSTRORGOTO(val, str, i, label) \
278 val = WMGetFromPLArray(texarray, i);\
279 if (!WMIsPLString(val)) {\
280 wwarning("could not parse texture %s", text);\
283 str = WMGetFromPLString(val)
285 texarray
= WMCreatePropListFromDescription(text
);
286 if (!texarray
|| !WMIsPLArray(texarray
)
287 || (count
= WMGetPropListItemCount(texarray
)) < 2) {
289 wwarning("could not parse texture %s", text
);
291 WMReleasePropList(texarray
);
295 texture
= wmalloc(sizeof(BackgroundTexture
));
296 memset(texture
, 0, sizeof(BackgroundTexture
));
298 GETSTRORGOTO(val
, type
, 0, error
);
300 if (strcasecmp(type
, "solid")==0) {
306 GETSTRORGOTO(val
, tmp
, 1, error
);
308 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
309 wwarning("could not parse color %s in texture %s", tmp
, text
);
312 XAllocColor(dpy
, DefaultColormap(dpy
, scr
), &color
);
314 pixmap
= XCreatePixmap(dpy
, root
, 8, 8, DefaultDepth(dpy
, scr
));
315 XSetForeground(dpy
, DefaultGC(dpy
, scr
), color
.pixel
);
316 XFillRectangle(dpy
, pixmap
, DefaultGC(dpy
, scr
), 0, 0, 8, 8);
318 texture
->pixmap
= pixmap
;
319 texture
->color
= color
;
322 } else if (strcasecmp(type
, "vgradient")==0
323 || strcasecmp(type
, "dgradient")==0
324 || strcasecmp(type
, "hgradient")==0) {
326 RColor color1
, color2
;
332 GETSTRORGOTO(val
, tmp
, 1, error
);
334 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
335 wwarning("could not parse color %s in texture %s", tmp
, text
);
339 color1
.red
= color
.red
>> 8;
340 color1
.green
= color
.green
>> 8;
341 color1
.blue
= color
.blue
>> 8;
343 GETSTRORGOTO(val
, tmp
, 2, error
);
345 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
346 wwarning("could not parse color %s in texture %s", tmp
, text
);
350 color2
.red
= color
.red
>> 8;
351 color2
.green
= color
.green
>> 8;
352 color2
.blue
= color
.blue
>> 8;
357 gtype
= RHorizontalGradient
;
363 gtype
= RVerticalGradient
;
368 gtype
= RDiagonalGradient
;
374 image
= RRenderGradient(iwidth
, iheight
, &color1
, &color2
, gtype
);
377 wwarning("could not render gradient texture:%s",
378 RMessageForError(RErrorCode
));
382 if (!RConvertImage(rc
, image
, &pixmap
)) {
383 wwarning("could not convert texture:%s",
384 RMessageForError(RErrorCode
));
385 RReleaseImage(image
);
389 texture
->width
= image
->width
;
390 texture
->height
= image
->height
;
391 RReleaseImage(image
);
393 texture
->pixmap
= pixmap
;
394 } else if (strcasecmp(type
, "mvgradient")==0
395 || strcasecmp(type
, "mdgradient")==0
396 || strcasecmp(type
, "mhgradient")==0) {
405 colors
= malloc(sizeof(RColor
*)*(count
-1));
407 wwarning("out of memory while parsing texture");
410 memset(colors
, 0, sizeof(RColor
*)*(count
-1));
412 for (i
= 2; i
< count
; i
++) {
413 val
= WMGetFromPLArray(texarray
, i
);
414 if (!WMIsPLString(val
)) {
415 wwarning("could not parse texture %s", text
);
417 for (j
= 0; colors
[j
]!=NULL
; j
++)
422 tmp
= WMGetFromPLString(val
);
424 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
425 wwarning("could not parse color %s in texture %s",
428 for (j
= 0; colors
[j
]!=NULL
; j
++)
433 if (!(colors
[i
-2] = malloc(sizeof(RColor
)))) {
434 wwarning("out of memory while parsing texture");
436 for (j
= 0; colors
[j
]!=NULL
; j
++)
442 colors
[i
-2]->red
= color
.red
>> 8;
443 colors
[i
-2]->green
= color
.green
>> 8;
444 colors
[i
-2]->blue
= color
.blue
>> 8;
450 gtype
= RHorizontalGradient
;
456 gtype
= RVerticalGradient
;
461 gtype
= RDiagonalGradient
;
467 image
= RRenderMultiGradient(iwidth
, iheight
, colors
, gtype
);
469 for (j
= 0; colors
[j
]!=NULL
; j
++)
474 wwarning("could not render gradient texture:%s",
475 RMessageForError(RErrorCode
));
479 if (!RConvertImage(rc
, image
, &pixmap
)) {
480 wwarning("could not convert texture:%s",
481 RMessageForError(RErrorCode
));
482 RReleaseImage(image
);
486 texture
->width
= image
->width
;
487 texture
->height
= image
->height
;
488 RReleaseImage(image
);
490 texture
->pixmap
= pixmap
;
491 } else if (strcasecmp(type
, "cpixmap")==0
492 || strcasecmp(type
, "spixmap")==0
493 || strcasecmp(type
, "mpixmap")==0
494 || strcasecmp(type
, "tpixmap")==0) {
496 Pixmap pixmap
= None
;
497 RImage
*image
= NULL
;
502 GETSTRORGOTO(val
, tmp
, 1, error
);
504 if (toupper(type[0]) == 'T' || toupper(type[0]) == 'C')
505 pixmap = LoadJPEG(rc, tmp, &iwidth, &iheight);
509 image
= loadImage(rc
, tmp
);
513 iwidth
= image
->width
;
514 iheight
= image
->height
;
517 GETSTRORGOTO(val
, tmp
, 2, error
);
519 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
520 wwarning("could not parse color %s in texture %s\n", tmp
, text
);
521 RReleaseImage(image
);
524 if (!XAllocColor(dpy
, DefaultColormap(dpy
, scr
), &color
)) {
525 rcolor
.red
= color
.red
>> 8;
526 rcolor
.green
= color
.green
>> 8;
527 rcolor
.blue
= color
.blue
>> 8;
528 RGetClosestXColor(rc
, &rcolor
, &color
);
534 /* for images with a transparent color */
535 if (image
->data
[3]) {
536 RCombineImageWithColor(image
, &rcolor
);
539 switch (toupper(type
[0])) {
541 texture
->width
= iwidth
;
542 texture
->height
= iheight
;
543 if (!pixmap
&& !RConvertImage(rc
, image
, &pixmap
)) {
544 wwarning("could not convert texture:%s",
545 RMessageForError(RErrorCode
));
546 RReleaseImage(image
);
550 RReleaseImage(image
);
552 texture
->pixmap
= pixmap
;
553 texture
->color
= color
;
559 Pixmap tpixmap
= XCreatePixmap( dpy
, root
, scrWidth
, scrHeight
, DefaultDepth(dpy
, scr
));
560 XSetForeground(dpy
, DefaultGC(dpy
, scr
), color
.pixel
);
561 XFillRectangle(dpy
, tpixmap
, DefaultGC(dpy
, scr
), 0, 0, scrWidth
, scrHeight
);
563 texture
->pixmap
= tpixmap
;
564 texture
->color
= color
;
565 texture
->width
= scrWidth
;
566 texture
->height
= scrHeight
;
569 if (xineInfo
.count
) {
571 for (i
=0; i
<xineInfo
.count
; ++i
) {
572 applyImage(rc
, texture
, image
, type
[0],
573 xineInfo
.screens
[i
].pos
.x
, xineInfo
.screens
[i
].pos
.y
,
574 xineInfo
.screens
[i
].size
.width
, xineInfo
.screens
[i
].size
.height
);
577 applyImage(rc
, texture
, image
, type
[0], 0, 0, scrWidth
, scrHeight
);
579 #else /* !XINERAMA */
580 applyImage(rc
, texture
, image
, type
[0], 0, 0, scrWidth
, scrHeight
);
581 #endif /* !XINERAMA */
582 RReleaseImage(image
);
586 } else if (strcasecmp(type
, "thgradient")==0
587 || strcasecmp(type
, "tvgradient")==0
588 || strcasecmp(type
, "tdgradient")==0) {
590 RColor color1
, color2
;
600 GETSTRORGOTO(val
, file
, 1, error
);
602 GETSTRORGOTO(val
, tmp
, 2, error
);
606 GETSTRORGOTO(val
, tmp
, 3, error
);
608 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
609 wwarning("could not parse color %s in texture %s", tmp
, text
);
613 color1
.red
= color
.red
>> 8;
614 color1
.green
= color
.green
>> 8;
615 color1
.blue
= color
.blue
>> 8;
617 GETSTRORGOTO(val
, tmp
, 4, error
);
619 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
620 wwarning("could not parse color %s in texture %s", tmp
, text
);
624 color2
.red
= color
.red
>> 8;
625 color2
.green
= color
.green
>> 8;
626 color2
.blue
= color
.blue
>> 8;
628 image
= loadImage(rc
, file
);
636 gtype
= RHorizontalGradient
;
638 theight
= image
->height
> scrHeight
? scrHeight
: image
->height
;
642 gtype
= RVerticalGradient
;
643 twidth
= image
->width
> scrWidth
? scrWidth
: image
->width
;
647 gtype
= RDiagonalGradient
;
652 gradient
= RRenderGradient(twidth
, theight
, &color1
, &color2
, gtype
);
655 wwarning("could not render texture:%s",
656 RMessageForError(RErrorCode
));
657 RReleaseImage(gradient
);
658 RReleaseImage(image
);
662 tiled
= RMakeTiledImage(image
, twidth
, theight
);
664 wwarning("could not render texture:%s",
665 RMessageForError(RErrorCode
));
666 RReleaseImage(gradient
);
667 RReleaseImage(image
);
670 RReleaseImage(image
);
672 RCombineImagesWithOpaqueness(tiled
, gradient
, opaq
);
673 RReleaseImage(gradient
);
675 if (!RConvertImage(rc
, tiled
, &pixmap
)) {
676 wwarning("could not convert texture:%s",
677 RMessageForError(RErrorCode
));
678 RReleaseImage(tiled
);
681 texture
->width
= tiled
->width
;
682 texture
->height
= tiled
->height
;
684 RReleaseImage(tiled
);
686 texture
->pixmap
= pixmap
;
687 } else if (strcasecmp(type
, "function")==0) {
689 void (*initFunc
) (Display
*, Colormap
);
690 RImage
* (*mainFunc
) (int, char**, int, int, int);
694 char *lib
, *func
, **argv
= 0;
699 goto function_cleanup
;
701 /* get the library name */
702 GETSTRORGOTO(val
, lib
, 1, function_cleanup
);
704 /* get the function name */
705 GETSTRORGOTO(val
, func
, 2, function_cleanup
);
708 argv
= (char**)wmalloc(argc
* sizeof(char*));
710 /* get the parameters */
712 for (i
=0; i
<argc
-1; i
++) {
713 GETSTRORGOTO(val
, tmp
, 3+i
, function_cleanup
);
714 argv
[i
+1] = wstrdup(tmp
);
717 handle
= dlopen(lib
, RTLD_LAZY
);
719 wwarning("could not find library %s", lib
);
720 goto function_cleanup
;
723 initFunc
= dlsym(handle
, "initWindowMaker");
725 wwarning("could not initialize library %s", lib
);
726 goto function_cleanup
;
728 initFunc(dpy
, DefaultColormap(dpy
, scr
));
730 mainFunc
= dlsym(handle
, func
);
732 wwarning("could not find function %s::%s", lib
, func
);
733 goto function_cleanup
;
735 image
= mainFunc(argc
, argv
, scrWidth
, scrHeight
, 0);
737 if (!RConvertImage(rc
, image
, &pixmap
)) {
738 wwarning("could not convert texture:%s",
739 RMessageForError(RErrorCode
));
740 goto function_cleanup
;
742 texture
->width
= scrWidth
;
743 texture
->height
= scrHeight
;
744 texture
->pixmap
= pixmap
;
750 for (i
=0; i
<argc
; i
++) {
758 RReleaseImage(image
);
764 wwarning("function textures not supported");
768 wwarning("invalid texture type %s", text
);
772 texture
->spec
= wstrdup(text
);
780 WMReleasePropList(texarray
);
787 freeTexture(BackgroundTexture
*texture
)
789 if (texture
->solid
) {
792 pixel
[0] = texture
->color
.pixel
;
793 /* dont free black/white pixels */
794 if (pixel
[0]!=BlackPixelOfScreen(DefaultScreenOfDisplay(dpy
))
795 && pixel
[0]!=WhitePixelOfScreen(DefaultScreenOfDisplay(dpy
)))
796 XFreeColors(dpy
, DefaultColormap(dpy
, scr
), pixel
, 1, 0);
798 if (texture
->pixmap
) {
799 XFreePixmap(dpy
, texture
->pixmap
);
801 wfree(texture
->spec
);
807 setupTexture(RContext
*rc
, BackgroundTexture
**textures
, int *maxTextures
,
808 int workspace
, char *texture
)
810 BackgroundTexture
*newTexture
= NULL
;
813 /* unset the texture */
815 if (textures
[workspace
]!=NULL
) {
816 textures
[workspace
]->refcount
--;
818 if (textures
[workspace
]->refcount
== 0)
819 freeTexture(textures
[workspace
]);
821 textures
[workspace
] = NULL
;
825 if (textures
[workspace
]
826 && strcasecmp(textures
[workspace
]->spec
, texture
)==0) {
827 /* texture did not change */
831 /* check if the same texture is already created */
832 for (i
= 0; i
< *maxTextures
; i
++) {
833 if (textures
[i
] && strcasecmp(textures
[i
]->spec
, texture
)==0) {
834 newTexture
= textures
[i
];
840 /* create the texture */
841 newTexture
= parseTexture(rc
, texture
);
846 if (textures
[workspace
]!=NULL
) {
848 textures
[workspace
]->refcount
--;
850 if (textures
[workspace
]->refcount
== 0)
851 freeTexture(textures
[workspace
]);
854 newTexture
->refcount
++;
855 textures
[workspace
] = newTexture
;
857 if (*maxTextures
< workspace
)
858 *maxTextures
= workspace
;
864 duplicatePixmap(Pixmap pixmap
, int width
, int height
)
869 /* must open a new display or the RetainPermanent will
870 * leave stuff allocated in RContext unallocated after exit */
871 tmpDpy
= XOpenDisplay(display
);
873 wwarning("could not open display to update background image information");
879 copyP
= XCreatePixmap(tmpDpy
, root
, width
, height
,
880 DefaultDepth(tmpDpy
, scr
));
881 XCopyArea(tmpDpy
, pixmap
, copyP
, DefaultGC(tmpDpy
, scr
),
882 0, 0, width
, height
, 0, 0);
883 XSync(tmpDpy
, False
);
885 XSetCloseDownMode(tmpDpy
, RetainPermanent
);
886 XCloseDisplay(tmpDpy
);
894 dummyErrorHandler(Display
*dpy
, XErrorEvent
*err
)
900 setPixmapProperty(Pixmap pixmap
)
902 static Atom prop
= 0;
905 unsigned long length
, after
;
910 prop
= XInternAtom(dpy
, "_XROOTPMAP_ID", False
);
915 /* Clear out the old pixmap */
916 XGetWindowProperty(dpy
, root
, prop
, 0L, 1L, False
, AnyPropertyType
,
917 &type
, &format
, &length
, &after
, &data
);
919 if ((type
== XA_PIXMAP
) && (format
== 32) && (length
== 1)) {
920 XSetErrorHandler(dummyErrorHandler
);
921 XKillClient(dpy
, *((Pixmap
*)data
));
923 XSetErrorHandler(NULL
);
924 mode
= PropModeReplace
;
926 mode
= PropModeAppend
;
929 XChangeProperty(dpy
, root
, prop
, XA_PIXMAP
, 32, mode
,
930 (unsigned char *) &pixmap
, 1);
932 XDeleteProperty(dpy
, root
, prop
);
942 changeTexture(BackgroundTexture
*texture
)
948 if (texture
->solid
) {
949 XSetWindowBackground(dpy
, root
, texture
->color
.pixel
);
951 XSetWindowBackgroundPixmap(dpy
, root
, texture
->pixmap
);
953 XClearWindow(dpy
, root
);
960 pixmap
= duplicatePixmap(texture
->pixmap
, texture
->width
,
963 setPixmapProperty(pixmap
);
969 readmsg(int fd
, unsigned char *buffer
, int size
)
975 count
= read(fd
, buffer
, size
);
989 * sizeSntexture_spec - sets the texture for workspace n
990 * sizeCn - change background texture to the one for workspace n
991 * sizePpath - set the pixmap search path
994 * size = 4 bytes for length of the message data
997 helperLoop(RContext
*rc
)
999 BackgroundTexture
*textures
[WORKSPACE_COUNT
];
1000 int maxTextures
= 0;
1001 unsigned char buffer
[2048], buf
[8];
1005 memset(textures
, 0, WORKSPACE_COUNT
*sizeof(BackgroundTexture
*));
1011 /* get length of message */
1012 if (readmsg(0, buffer
, 4) < 0) {
1013 wsyserror("error reading message from Window Maker");
1015 if (errcount
== 0) {
1021 memcpy(buf
, buffer
, 4);
1026 if (readmsg(0, buffer
, size
) < 0) {
1027 wsyserror("error reading message from Window Maker");
1029 if (errcount
== 0) {
1036 printf("RECEIVED %s\n",buffer
);
1038 if (buffer
[0]!='P' && buffer
[0]!='K') {
1039 memcpy(buf
, &buffer
[1], 4);
1041 workspace
= atoi(buf
);
1042 if (workspace
< 0 || workspace
>= WORKSPACE_COUNT
) {
1043 wwarning("received message with invalid workspace number %i\n",
1049 switch (buffer
[0]) {
1052 printf("set texture %s\n", &buffer
[5]);
1054 setupTexture(rc
, textures
, &maxTextures
, workspace
, &buffer
[5]);
1059 printf("change texture %i\n", workspace
);
1061 if (!textures
[workspace
]) {
1062 changeTexture(textures
[0]);
1064 changeTexture(textures
[workspace
]);
1070 printf("change pixmappath %s\n", &buffer
[1]);
1074 PixmapPath
= wstrdup(&buffer
[1]);
1079 printf("unset workspace %i\n", workspace
);
1081 setupTexture(rc
, textures
, &maxTextures
, workspace
, NULL
);
1086 printf("exit command\n");
1091 wwarning("unknown message received");
1099 updateDomain(char *domain
, char *key
, char *texture
)
1101 char *program
= "wdwrite";
1103 /* here is a mem leak */
1104 system(wstrconcat("wdwrite ",
1105 wstrconcat(domain
, smooth
? " SmoothWorkspaceBack YES"
1106 : " SmoothWorkspaceBack NO")));
1108 execlp(program
, program
, domain
, key
, texture
, NULL
);
1109 wwarning("warning could not run \"%s\"", program
);
1115 globalDefaultsPathForDomain(char *domain
)
1119 sprintf(path
, "%s/WindowMaker/%s", SYSCONFDIR
, domain
);
1121 return wstrdup(path
);
1126 getValueForKey(char *domain
, char *keyName
)
1129 WMPropList
*key
, *val
, *d
;
1131 key
= WMCreatePLString(keyName
);
1133 /* try to find PixmapPath in user defaults */
1134 path
= wdefaultspathfordomain(domain
);
1135 d
= WMReadPropListFromFile(path
);
1137 wwarning("could not open domain file %s", path
);
1141 if (d
&& !WMIsPLDictionary(d
)) {
1142 WMReleasePropList(d
);
1146 val
= WMGetFromPLDictionary(d
, key
);
1150 /* try to find PixmapPath in global defaults */
1152 path
= globalDefaultsPathForDomain(domain
);
1154 wwarning("could not locate file for domain %s", domain
);
1157 d
= WMReadPropListFromFile(path
);
1161 if (d
&& !WMIsPLDictionary(d
)) {
1162 WMReleasePropList(d
);
1166 val
= WMGetFromPLDictionary(d
, key
);
1174 WMRetainPropList(val
);
1176 WMReleasePropList(key
);
1178 WMReleasePropList(d
);
1186 getPixmapPath(char *domain
)
1192 val
= getValueForKey(domain
, "PixmapPath");
1194 if (!val
|| !WMIsPLArray(val
)) {
1196 WMReleasePropList(val
);
1200 count
= WMGetPropListItemCount(val
);
1202 for (i
=0; i
<count
; i
++) {
1205 v
= WMGetFromPLArray(val
, i
);
1206 if (!v
|| !WMIsPLString(v
)) {
1209 len
+= strlen(WMGetFromPLString(v
))+1;
1212 ptr
= data
= wmalloc(len
+1);
1215 for (i
=0; i
<count
; i
++) {
1218 v
= WMGetFromPLArray(val
, i
);
1219 if (!v
|| !WMIsPLString(v
)) {
1222 strcpy(ptr
, WMGetFromPLString(v
));
1224 ptr
+= strlen(WMGetFromPLString(v
));
1229 ptr
--; *(ptr
--) = 0;
1231 WMReleasePropList(val
);
1238 getFullPixmapPath(char *file
)
1242 if (!PixmapPath
|| !(tmp
= wfindfile(PixmapPath
, file
))) {
1244 char *path
= wmalloc(bsize
);
1246 while (!getcwd(path
, bsize
)) {
1248 path
= wrealloc(path
, bsize
);
1251 tmp
= wstrconcat(path
, "/");
1253 path
= wstrconcat(tmp
, file
);
1259 /* the file is in the PixmapPath */
1262 return wstrdup(file
);
1277 print_help(char *ProgName
)
1279 printf("Usage: %s [options] [image]\n", ProgName
);
1280 puts("Sets the workspace background to the specified image or a texture and optionally update Window Maker configuration");
1282 #define P(m) puts(m)
1283 P(" -display display to use");
1284 P(" -d, --dither dither image");
1285 P(" -m, --match match colors");
1286 P(" -S, --smooth smooth scaled image");
1287 P(" -b, --back-color <color> background color");
1288 P(" -t, --tile tile image");
1289 P(" -e, --center center image");
1290 P(" -s, --scale scale image (default)");
1291 P(" -a, --maxscale scale image and keep aspect ratio");
1292 P(" -u, --update-wmaker update WindowMaker domain database");
1293 P(" -D, --update-domain <domain> update <domain> database");
1294 P(" -c, --colors <cpc> colors per channel to use");
1295 P(" -p, --parse <texture> proplist style texture specification");
1296 P(" -w, --workspace <workspace> update background for the specified workspace");
1297 P(" --version show version of wmsetbg and exit");
1298 P(" --help show this help and exit");
1305 changeTextureForWorkspace(char *domain
, char *texture
, int workspace
)
1307 WMPropList
*array
, *val
;
1311 val
= WMCreatePropListFromDescription(texture
);
1313 wwarning("could not parse texture %s", texture
);
1317 array
= getValueForKey("WindowMaker", "WorkspaceSpecificBack");
1320 array
= WMCreatePLArray(NULL
, NULL
);
1323 j
= WMGetPropListItemCount(array
);
1324 if (workspace
>= j
) {
1327 empty
= WMCreatePLArray(NULL
, NULL
);
1329 while (j
++ < workspace
-1) {
1330 WMAddToPLArray(array
, empty
);
1332 WMAddToPLArray(array
, val
);
1334 WMDeleteFromPLArray(array
, workspace
);
1335 WMInsertInPLArray(array
, workspace
, val
);
1338 value
= WMGetPropListDescription(array
, False
);
1339 updateDomain(domain
, "WorkspaceSpecificBack", value
);
1344 main(int argc
, char **argv
)
1349 RContextAttributes rattr
;
1350 char *style
= "spixmap";
1351 char *back_color
= "gray20";
1352 char *image_name
= NULL
;
1353 char *domain
= "WindowMaker";
1354 int update
=0, cpc
=4, render_mode
=RDitheredRendering
, obey_user
=0;
1355 char *texture
= NULL
;
1358 signal(SIGINT
, SIG_DFL
);
1359 signal(SIGTERM
, SIG_DFL
);
1360 signal(SIGQUIT
, SIG_DFL
);
1361 signal(SIGSEGV
, SIG_DFL
);
1362 signal(SIGBUS
, SIG_DFL
);
1363 signal(SIGFPE
, SIG_DFL
);
1364 signal(SIGABRT
, SIG_DFL
);
1365 signal(SIGHUP
, SIG_DFL
);
1366 signal(SIGPIPE
, SIG_DFL
);
1367 signal(SIGCHLD
, SIG_DFL
);
1369 WMInitializeApplication("wmsetbg", &argc
, argv
);
1371 for (i
=1; i
<argc
; i
++) {
1372 if (strcmp(argv
[i
], "-helper")==0) {
1374 } else if (strcmp(argv
[i
], "-display")==0) {
1377 wfatal("too few arguments for %s\n", argv
[i
-1]);
1381 } else if (strcmp(argv
[i
], "-s")==0
1382 || strcmp(argv
[i
], "--scale")==0) {
1384 } else if (strcmp(argv
[i
], "-t")==0
1385 || strcmp(argv
[i
], "--tile")==0) {
1387 } else if (strcmp(argv
[i
], "-e")==0
1388 || strcmp(argv
[i
], "--center")==0) {
1390 } else if (strcmp(argv
[i
], "-a")==0
1391 || strcmp(argv
[i
], "--maxscale")==0) {
1393 } else if (strcmp(argv
[i
], "-d")==0
1394 || strcmp(argv
[i
], "--dither")==0) {
1395 render_mode
= RDitheredRendering
;
1397 } else if (strcmp(argv
[i
], "-m")==0
1398 || strcmp(argv
[i
], "--match")==0) {
1399 render_mode
= RBestMatchRendering
;
1401 } else if (strcmp(argv
[i
], "-S")==0
1402 || strcmp(argv
[i
], "--smooth")==0) {
1404 } else if (strcmp(argv
[i
], "-u")==0
1405 || strcmp(argv
[i
], "--update-wmaker")==0) {
1407 } else if (strcmp(argv
[i
], "-D")==0
1408 || strcmp(argv
[i
], "--update-domain")==0) {
1412 wfatal("too few arguments for %s\n", argv
[i
-1]);
1415 domain
= wstrdup(argv
[i
]);
1416 } else if (strcmp(argv
[i
], "-c")==0
1417 || strcmp(argv
[i
], "--colors")==0) {
1420 wfatal("too few arguments for %s\n", argv
[i
-1]);
1423 if (sscanf(argv
[i
], "%i", &cpc
)!=1) {
1424 wfatal("bad value for colors per channel: \"%s\"\n", argv
[i
]);
1427 } else if (strcmp(argv
[i
], "-b")==0
1428 || strcmp(argv
[i
], "--back-color")==0) {
1431 wfatal("too few arguments for %s\n", argv
[i
-1]);
1434 back_color
= argv
[i
];
1435 } else if (strcmp(argv
[i
], "-p")==0
1436 || strcmp(argv
[i
], "--parse")==0) {
1439 wfatal("too few arguments for %s\n", argv
[i
-1]);
1443 } else if (strcmp(argv
[i
], "-w")==0
1444 || strcmp(argv
[i
], "--workspace")==0) {
1447 wfatal("too few arguments for %s\n", argv
[i
-1]);
1450 if (sscanf(argv
[i
], "%i", &workspace
)!=1) {
1451 wfatal("bad value for workspace number: \"%s\"",
1455 } else if (strcmp(argv
[i
], "--version")==0) {
1457 printf(PROG_VERSION
);
1460 } else if (strcmp(argv
[i
], "--help")==0) {
1461 print_help(argv
[0]);
1463 } else if (argv
[i
][0] != '-') {
1464 image_name
= argv
[i
];
1466 printf("%s: invalid argument '%s'\n", argv
[0], argv
[i
]);
1467 printf("Try '%s --help' for more information\n", argv
[0]);
1471 if (!image_name
&& !texture
&& !helperMode
) {
1472 printf("%s: you must specify a image file name or a texture\n",
1474 printf("Try '%s --help' for more information\n", argv
[0]);
1479 PixmapPath
= getPixmapPath(domain
);
1482 #if 0 /* some problem with Alpha... TODO: check if its right */
1483 val
= WMGetFromPLDictionary(domain
,
1484 WMCreatePLString("SmoothWorkspaceBack"));
1486 val
= getValueForKey(domain
, "SmoothWorkspaceBack");
1489 if (val
&& WMIsPLString(val
) && strcasecmp(WMGetFromPLString(val
), "YES")==0)
1493 dpy
= XOpenDisplay(display
);
1495 wfatal("could not open display");
1499 XSynchronize(dpy
, 1);
1502 root
= DefaultRootWindow(dpy
);
1504 scr
= DefaultScreen(dpy
);
1506 scrWidth
= WidthOfScreen(DefaultScreenOfDisplay(dpy
));
1507 scrHeight
= HeightOfScreen(DefaultScreenOfDisplay(dpy
));
1513 if (!obey_user
&& DefaultDepth(dpy
, scr
) <= 8)
1514 render_mode
= RDitheredRendering
;
1516 rattr
.flags
= RC_RenderMode
| RC_ColorsPerChannel
1517 | RC_StandardColormap
| RC_DefaultVisual
;
1518 rattr
.render_mode
= render_mode
;
1519 rattr
.colors_per_channel
= cpc
;
1520 rattr
.standard_colormap_mode
= RCreateStdColormap
;
1522 rc
= RCreateContext(dpy
, scr
, &rattr
);
1525 rattr
.standard_colormap_mode
= RIgnoreStdColormap
;
1526 rc
= RCreateContext(dpy
, scr
, &rattr
);
1530 wfatal("could not initialize wrlib: %s",
1531 RMessageForError(RErrorCode
));
1536 /* lower priority, so that it wont use all the CPU */
1541 BackgroundTexture
*tex
;
1545 char *image_path
= getFullPixmapPath(image_name
);
1547 sprintf(buffer
, "(%s, \"%s\", %s)", style
, image_path
, back_color
);
1549 texture
= (char*)buffer
;
1552 if (update
&& workspace
< 0) {
1553 updateDomain(domain
, "WorkspaceBack", texture
);
1556 tex
= parseTexture(rc
, texture
);
1563 /* always update domain */
1564 changeTextureForWorkspace(domain
, texture
, workspace
);