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 along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * TODO: rewrite, too dirty
31 #include <X11/Xutil.h>
32 #include <X11/Xatom.h>
37 #include <sys/types.h>
40 #include "../config.h"
43 # ifdef SOLARIS_XINERAMA /* sucks */
44 # include <X11/extensions/xinerama.h>
46 # include <X11/extensions/Xinerama.h>
50 #include "../src/wconfig.h"
52 #ifndef GLOBAL_DEFAULTS_SUBDIR
53 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
56 #include <WINGs/WINGs.h>
61 int count
; /* screen count, 0 = inactive */
64 #define WORKSPACE_COUNT (MAX_WORKSPACES+1)
74 WXineramaInfo xineInfo
;
78 Bool xineStretch
= False
;
81 Pixmap CurrentPixmap
= None
;
82 char *PixmapPath
= NULL
;
84 extern Pixmap
LoadJPEG(RContext
* rc
, char *file_name
, int *width
, int *height
);
85 extern char *__progname
;
87 typedef struct BackgroundTexture
{
95 Pixmap pixmap
; /* for all textures, including solid */
96 int width
; /* size of the pixmap */
102 xineInfo
.screens
= NULL
;
105 # ifdef SOLARIS_XINERAMA
106 if (XineramaGetState(dpy
, scr
)) {
107 XRectangle head
[MAXFRAMEBUFFERS
];
108 unsigned char hints
[MAXFRAMEBUFFERS
];
111 if (XineramaGetInfo(dpy
, scr
, head
, hints
, &xineInfo
.count
)) {
113 xineInfo
.screens
= wmalloc(sizeof(WMRect
) * (xineInfo
.count
+ 1));
115 for (i
= 0; i
< xineInfo
.count
; i
++) {
116 xineInfo
.screens
[i
].pos
.x
= head
[i
].x
;
117 xineInfo
.screens
[i
].pos
.y
= head
[i
].y
;
118 xineInfo
.screens
[i
].size
.width
= head
[i
].width
;
119 xineInfo
.screens
[i
].size
.height
= head
[i
].height
;
123 # else /* !SOLARIS_XINERAMA */
124 if (XineramaIsActive(dpy
)) {
125 XineramaScreenInfo
*xine_screens
;
128 xine_screens
= XineramaQueryScreens(dpy
, &xineInfo
.count
);
130 xineInfo
.screens
= wmalloc(sizeof(WMRect
) * (xineInfo
.count
+ 1));
132 for (i
= 0; i
< xineInfo
.count
; i
++) {
133 xineInfo
.screens
[i
].pos
.x
= xine_screens
[i
].x_org
;
134 xineInfo
.screens
[i
].pos
.y
= xine_screens
[i
].y_org
;
135 xineInfo
.screens
[i
].size
.width
= xine_screens
[i
].width
;
136 xineInfo
.screens
[i
].size
.height
= xine_screens
[i
].height
;
140 # endif /* !SOLARIS_XINERAMA */
141 #endif /* XINERAMA */
144 RImage
*loadImage(RContext
* rc
, char *file
)
149 if (access(file
, F_OK
) != 0) {
150 path
= wfindfile(PixmapPath
, file
);
152 wwarning("%s:could not find image file used in texture", file
);
156 path
= wstrdup(file
);
159 image
= RLoadImage(rc
, path
, 0);
161 wwarning("%s:could not load image file used in texture:%s", path
, RMessageForError(RErrorCode
));
169 applyImage(RContext
* rc
, BackgroundTexture
* texture
, RImage
* image
, char type
,
170 int x
, int y
, int width
, int height
)
175 switch (toupper(type
)) {
179 if (toupper(type
) == 'S') {
182 } else if(toupper(type
) == 'F') {
183 if (image
->width
* height
> image
->height
* width
) {
184 w
= (height
* image
->width
) / image
->height
;
188 h
= (width
* image
->height
) / image
->width
;
191 if (image
->width
* height
> image
->height
* width
) {
193 h
= (width
* image
->height
) / image
->width
;
195 w
= (height
* image
->width
) / image
->height
;
200 if (w
!= image
->width
|| h
!= image
->height
) {
204 simage
= RSmoothScaleImage(image
, w
, h
);
206 simage
= RScaleImage(image
, w
, h
);
210 wwarning("could not scale image:%s", RMessageForError(RErrorCode
));
222 if (!RConvertImage(rc
, image
, &pixmap
)) {
223 wwarning("could not convert texture:%s", RMessageForError(RErrorCode
));
227 if (image
->width
!= width
|| image
->height
!= height
) {
230 if (image
->height
< height
) {
232 y
+= (height
- h
) / 2;
235 sy
= (image
->height
- height
) / 2;
238 if (image
->width
< width
) {
240 x
+= (width
- w
) / 2;
243 sx
= (image
->width
- width
) / 2;
247 XCopyArea(dpy
, pixmap
, texture
->pixmap
, DefaultGC(dpy
, scr
), sx
, sy
, w
, h
, x
, y
);
249 XCopyArea(dpy
, pixmap
, texture
->pixmap
, DefaultGC(dpy
, scr
), 0, 0, width
, height
,
252 XFreePixmap(dpy
, pixmap
);
254 RReleaseImage(image
);
261 BackgroundTexture
*parseTexture(RContext
* rc
, char *text
)
263 BackgroundTexture
*texture
= NULL
;
264 WMPropList
*texarray
;
270 #define GETSTRORGOTO(val, str, i, label) \
271 val = WMGetFromPLArray(texarray, i);\
272 if (!WMIsPLString(val)) {\
273 wwarning("could not parse texture %s", text);\
276 str = WMGetFromPLString(val)
278 texarray
= WMCreatePropListFromDescription(text
);
279 if (!texarray
|| !WMIsPLArray(texarray
)
280 || (count
= WMGetPropListItemCount(texarray
)) < 2) {
282 wwarning("could not parse texture %s", text
);
284 WMReleasePropList(texarray
);
288 texture
= wmalloc(sizeof(BackgroundTexture
));
290 GETSTRORGOTO(val
, type
, 0, error
);
292 if (strcasecmp(type
, "solid") == 0) {
298 GETSTRORGOTO(val
, tmp
, 1, error
);
300 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
301 wwarning("could not parse color %s in texture %s", tmp
, text
);
304 XAllocColor(dpy
, DefaultColormap(dpy
, scr
), &color
);
306 pixmap
= XCreatePixmap(dpy
, root
, 8, 8, DefaultDepth(dpy
, scr
));
307 XSetForeground(dpy
, DefaultGC(dpy
, scr
), color
.pixel
);
308 XFillRectangle(dpy
, pixmap
, DefaultGC(dpy
, scr
), 0, 0, 8, 8);
310 texture
->pixmap
= pixmap
;
311 texture
->color
= color
;
314 } else if (strcasecmp(type
, "vgradient") == 0
315 || strcasecmp(type
, "dgradient") == 0 || strcasecmp(type
, "hgradient") == 0) {
317 RColor color1
, color2
;
323 GETSTRORGOTO(val
, tmp
, 1, error
);
325 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
326 wwarning("could not parse color %s in texture %s", tmp
, text
);
330 color1
.red
= color
.red
>> 8;
331 color1
.green
= color
.green
>> 8;
332 color1
.blue
= color
.blue
>> 8;
334 GETSTRORGOTO(val
, tmp
, 2, error
);
336 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
337 wwarning("could not parse color %s in texture %s", tmp
, text
);
341 color2
.red
= color
.red
>> 8;
342 color2
.green
= color
.green
>> 8;
343 color2
.blue
= color
.blue
>> 8;
348 gtype
= RHorizontalGradient
;
354 gtype
= RVerticalGradient
;
359 gtype
= RDiagonalGradient
;
365 image
= RRenderGradient(iwidth
, iheight
, &color1
, &color2
, gtype
);
368 wwarning("could not render gradient texture:%s", RMessageForError(RErrorCode
));
372 if (!RConvertImage(rc
, image
, &pixmap
)) {
373 wwarning("could not convert texture:%s", RMessageForError(RErrorCode
));
374 RReleaseImage(image
);
378 texture
->width
= image
->width
;
379 texture
->height
= image
->height
;
380 RReleaseImage(image
);
382 texture
->pixmap
= pixmap
;
383 } else if (strcasecmp(type
, "mvgradient") == 0
384 || strcasecmp(type
, "mdgradient") == 0 || strcasecmp(type
, "mhgradient") == 0) {
393 colors
= malloc(sizeof(RColor
*) * (count
- 1));
395 wwarning("out of memory while parsing texture");
398 memset(colors
, 0, sizeof(RColor
*) * (count
- 1));
400 for (i
= 2; i
< count
; i
++) {
401 val
= WMGetFromPLArray(texarray
, i
);
402 if (!WMIsPLString(val
)) {
403 wwarning("could not parse texture %s", text
);
405 for (j
= 0; colors
[j
] != NULL
; j
++)
410 tmp
= WMGetFromPLString(val
);
412 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
413 wwarning("could not parse color %s in texture %s", tmp
, text
);
415 for (j
= 0; colors
[j
] != NULL
; j
++)
420 if (!(colors
[i
- 2] = malloc(sizeof(RColor
)))) {
421 wwarning("out of memory while parsing texture");
423 for (j
= 0; colors
[j
] != NULL
; j
++)
429 colors
[i
- 2]->red
= color
.red
>> 8;
430 colors
[i
- 2]->green
= color
.green
>> 8;
431 colors
[i
- 2]->blue
= color
.blue
>> 8;
437 gtype
= RHorizontalGradient
;
443 gtype
= RVerticalGradient
;
448 gtype
= RDiagonalGradient
;
454 image
= RRenderMultiGradient(iwidth
, iheight
, colors
, gtype
);
456 for (j
= 0; colors
[j
] != NULL
; j
++)
461 wwarning("could not render gradient texture:%s", RMessageForError(RErrorCode
));
465 if (!RConvertImage(rc
, image
, &pixmap
)) {
466 wwarning("could not convert texture:%s", RMessageForError(RErrorCode
));
467 RReleaseImage(image
);
471 texture
->width
= image
->width
;
472 texture
->height
= image
->height
;
473 RReleaseImage(image
);
475 texture
->pixmap
= pixmap
;
476 } else if (strcasecmp(type
, "cpixmap") == 0
477 || strcasecmp(type
, "spixmap") == 0 || strcasecmp(type
, "fpixmap") == 0
478 || strcasecmp(type
, "mpixmap") == 0 || strcasecmp(type
, "tpixmap") == 0) {
480 Pixmap pixmap
= None
;
481 RImage
*image
= NULL
;
482 int iwidth
= 0, iheight
= 0;
485 GETSTRORGOTO(val
, tmp
, 1, error
);
487 if (toupper(type[0]) == 'T' || toupper(type[0]) == 'C')
488 pixmap = LoadJPEG(rc, tmp, &iwidth, &iheight);
492 image
= loadImage(rc
, tmp
);
496 iwidth
= image
->width
;
497 iheight
= image
->height
;
500 GETSTRORGOTO(val
, tmp
, 2, error
);
502 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
503 wwarning("could not parse color %s in texture %s", tmp
, text
);
504 RReleaseImage(image
);
507 if (!XAllocColor(dpy
, DefaultColormap(dpy
, scr
), &color
)) {
508 rcolor
.red
= color
.red
>> 8;
509 rcolor
.green
= color
.green
>> 8;
510 rcolor
.blue
= color
.blue
>> 8;
511 RGetClosestXColor(rc
, &rcolor
, &color
);
517 /* for images with a transparent color */
518 if (image
->data
[3]) {
519 RCombineImageWithColor(image
, &rcolor
);
522 switch (toupper(type
[0])) {
524 texture
->width
= iwidth
;
525 texture
->height
= iheight
;
526 if (!pixmap
&& !RConvertImage(rc
, image
, &pixmap
)) {
527 wwarning("could not convert texture:%s", RMessageForError(RErrorCode
));
528 RReleaseImage(image
);
532 RReleaseImage(image
);
534 texture
->pixmap
= pixmap
;
535 texture
->color
= color
;
543 XCreatePixmap(dpy
, root
, scrWidth
, scrHeight
, DefaultDepth(dpy
, scr
));
544 XSetForeground(dpy
, DefaultGC(dpy
, scr
), color
.pixel
);
545 XFillRectangle(dpy
, tpixmap
, DefaultGC(dpy
, scr
), 0, 0, scrWidth
, scrHeight
);
547 texture
->pixmap
= tpixmap
;
548 texture
->color
= color
;
549 texture
->width
= scrWidth
;
550 texture
->height
= scrHeight
;
553 if (xineInfo
.count
&& ! xineStretch
) {
555 for (i
= 0; i
< xineInfo
.count
; ++i
) {
556 applyImage(rc
, texture
, image
, type
[0],
557 xineInfo
.screens
[i
].pos
.x
, xineInfo
.screens
[i
].pos
.y
,
558 xineInfo
.screens
[i
].size
.width
,
559 xineInfo
.screens
[i
].size
.height
);
562 applyImage(rc
, texture
, image
, type
[0], 0, 0, scrWidth
, scrHeight
);
564 #else /* !XINERAMA */
565 applyImage(rc
, texture
, image
, type
[0], 0, 0, scrWidth
, scrHeight
);
566 #endif /* !XINERAMA */
567 RReleaseImage(image
);
571 } else if (strcasecmp(type
, "thgradient") == 0
572 || strcasecmp(type
, "tvgradient") == 0 || strcasecmp(type
, "tdgradient") == 0) {
574 RColor color1
, color2
;
584 GETSTRORGOTO(val
, file
, 1, error
);
586 GETSTRORGOTO(val
, tmp
, 2, error
);
590 GETSTRORGOTO(val
, tmp
, 3, error
);
592 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
593 wwarning("could not parse color %s in texture %s", tmp
, text
);
597 color1
.red
= color
.red
>> 8;
598 color1
.green
= color
.green
>> 8;
599 color1
.blue
= color
.blue
>> 8;
601 GETSTRORGOTO(val
, tmp
, 4, error
);
603 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
604 wwarning("could not parse color %s in texture %s", tmp
, text
);
608 color2
.red
= color
.red
>> 8;
609 color2
.green
= color
.green
>> 8;
610 color2
.blue
= color
.blue
>> 8;
612 image
= loadImage(rc
, file
);
620 gtype
= RHorizontalGradient
;
622 theight
= image
->height
> scrHeight
? scrHeight
: image
->height
;
626 gtype
= RVerticalGradient
;
627 twidth
= image
->width
> scrWidth
? scrWidth
: image
->width
;
631 gtype
= RDiagonalGradient
;
636 gradient
= RRenderGradient(twidth
, theight
, &color1
, &color2
, gtype
);
639 wwarning("could not render texture:%s", RMessageForError(RErrorCode
));
640 RReleaseImage(gradient
);
641 RReleaseImage(image
);
645 tiled
= RMakeTiledImage(image
, twidth
, theight
);
647 wwarning("could not render texture:%s", RMessageForError(RErrorCode
));
648 RReleaseImage(gradient
);
649 RReleaseImage(image
);
652 RReleaseImage(image
);
654 RCombineImagesWithOpaqueness(tiled
, gradient
, opaq
);
655 RReleaseImage(gradient
);
657 if (!RConvertImage(rc
, tiled
, &pixmap
)) {
658 wwarning("could not convert texture:%s", RMessageForError(RErrorCode
));
659 RReleaseImage(tiled
);
662 texture
->width
= tiled
->width
;
663 texture
->height
= tiled
->height
;
665 RReleaseImage(tiled
);
667 texture
->pixmap
= pixmap
;
668 } else if (strcasecmp(type
, "function") == 0) {
669 /* Leave this in to handle the unlikely case of
670 * someone actually having function textures configured */
671 wwarning("function texture support has been removed");
674 wwarning("invalid texture type %s", text
);
678 texture
->spec
= wstrdup(text
);
686 WMReleasePropList(texarray
);
691 void freeTexture(BackgroundTexture
* texture
)
693 if (texture
->solid
) {
694 unsigned long pixel
[1];
696 pixel
[0] = texture
->color
.pixel
;
697 /* dont free black/white pixels */
698 if (pixel
[0] != BlackPixelOfScreen(DefaultScreenOfDisplay(dpy
))
699 && pixel
[0] != WhitePixelOfScreen(DefaultScreenOfDisplay(dpy
)))
700 XFreeColors(dpy
, DefaultColormap(dpy
, scr
), pixel
, 1, 0);
702 if (texture
->pixmap
) {
703 XFreePixmap(dpy
, texture
->pixmap
);
705 wfree(texture
->spec
);
709 void setupTexture(RContext
* rc
, BackgroundTexture
** textures
, int *maxTextures
, int workspace
, char *texture
)
711 BackgroundTexture
*newTexture
= NULL
;
714 /* unset the texture */
716 if (textures
[workspace
] != NULL
) {
717 textures
[workspace
]->refcount
--;
719 if (textures
[workspace
]->refcount
== 0)
720 freeTexture(textures
[workspace
]);
722 textures
[workspace
] = NULL
;
726 if (textures
[workspace
]
727 && strcasecmp(textures
[workspace
]->spec
, texture
) == 0) {
728 /* texture did not change */
732 /* check if the same texture is already created */
733 for (i
= 0; i
< *maxTextures
; i
++) {
734 if (textures
[i
] && strcasecmp(textures
[i
]->spec
, texture
) == 0) {
735 newTexture
= textures
[i
];
741 /* create the texture */
742 newTexture
= parseTexture(rc
, texture
);
747 if (textures
[workspace
] != NULL
) {
749 textures
[workspace
]->refcount
--;
751 if (textures
[workspace
]->refcount
== 0)
752 freeTexture(textures
[workspace
]);
755 newTexture
->refcount
++;
756 textures
[workspace
] = newTexture
;
758 if (*maxTextures
< workspace
)
759 *maxTextures
= workspace
;
762 Pixmap
duplicatePixmap(Pixmap pixmap
, int width
, int height
)
767 /* must open a new display or the RetainPermanent will
768 * leave stuff allocated in RContext unallocated after exit */
769 tmpDpy
= XOpenDisplay(display
);
771 wwarning("could not open display to update background image information");
777 copyP
= XCreatePixmap(tmpDpy
, root
, width
, height
, DefaultDepth(tmpDpy
, scr
));
778 XCopyArea(tmpDpy
, pixmap
, copyP
, DefaultGC(tmpDpy
, scr
), 0, 0, width
, height
, 0, 0);
779 XSync(tmpDpy
, False
);
781 XSetCloseDownMode(tmpDpy
, RetainPermanent
);
782 XCloseDisplay(tmpDpy
);
788 static int dummyErrorHandler(Display
* dpy
, XErrorEvent
* err
)
793 void setPixmapProperty(Pixmap pixmap
)
795 static Atom prop
= 0;
798 unsigned long length
, after
;
803 prop
= XInternAtom(dpy
, "_XROOTPMAP_ID", False
);
808 /* Clear out the old pixmap */
809 XGetWindowProperty(dpy
, root
, prop
, 0L, 1L, False
, AnyPropertyType
,
810 &type
, &format
, &length
, &after
, &data
);
812 if ((type
== XA_PIXMAP
) && (format
== 32) && (length
== 1)) {
813 XSetErrorHandler(dummyErrorHandler
);
814 XKillClient(dpy
, *((Pixmap
*) data
));
816 XSetErrorHandler(NULL
);
817 mode
= PropModeReplace
;
819 mode
= PropModeAppend
;
822 XChangeProperty(dpy
, root
, prop
, XA_PIXMAP
, 32, mode
, (unsigned char *)&pixmap
, 1);
824 XDeleteProperty(dpy
, root
, prop
);
830 void changeTexture(BackgroundTexture
* texture
)
836 if (texture
->solid
) {
837 XSetWindowBackground(dpy
, root
, texture
->color
.pixel
);
839 XSetWindowBackgroundPixmap(dpy
, root
, texture
->pixmap
);
841 XClearWindow(dpy
, root
);
848 pixmap
= duplicatePixmap(texture
->pixmap
, texture
->width
, texture
->height
);
850 setPixmapProperty(pixmap
);
854 int readmsg(int fd
, char *buffer
, int size
)
860 count
= read(fd
, buffer
, size
);
873 * sizeSntexture_spec - sets the texture for workspace n
874 * sizeCn - change background texture to the one for workspace n
875 * sizePpath - set the pixmap search path
878 * size = 4 bytes for length of the message data
880 void helperLoop(RContext
* rc
)
882 BackgroundTexture
*textures
[WORKSPACE_COUNT
];
884 char buffer
[2048], buf
[8];
888 memset(textures
, 0, WORKSPACE_COUNT
* sizeof(BackgroundTexture
*));
893 /* get length of message */
894 if (readmsg(0, buffer
, 4) < 0) {
895 werror("error reading message from Window Maker");
903 memcpy(buf
, buffer
, 4);
908 if (readmsg(0, buffer
, size
) < 0) {
909 werror("error reading message from Window Maker");
918 printf("RECEIVED %s\n", buffer
);
920 if (buffer
[0] != 'P' && buffer
[0] != 'K') {
921 memcpy(buf
, &buffer
[1], 4);
923 workspace
= atoi(buf
);
924 if (workspace
< 0 || workspace
>= WORKSPACE_COUNT
) {
925 wwarning("received message with invalid workspace number %i", workspace
);
933 printf("set texture %s\n", &buffer
[5]);
935 setupTexture(rc
, textures
, &maxTextures
, workspace
, &buffer
[5]);
940 printf("change texture %i\n", workspace
);
942 if (!textures
[workspace
]) {
943 changeTexture(textures
[0]);
945 changeTexture(textures
[workspace
]);
951 printf("change pixmappath %s\n", &buffer
[1]);
955 PixmapPath
= wstrdup(&buffer
[1]);
960 printf("unset workspace %i\n", workspace
);
962 setupTexture(rc
, textures
, &maxTextures
, workspace
, NULL
);
967 printf("exit command\n");
972 wwarning("unknown message received");
978 void updateDomain(char *domain
, char *key
, char *texture
)
980 char *program
= "wdwrite";
982 /* here is a mem leak */
983 system(wstrconcat("wdwrite ",
984 wstrconcat(domain
, smooth
? " SmoothWorkspaceBack YES" : " SmoothWorkspaceBack NO")));
986 execlp(program
, program
, domain
, key
, texture
, NULL
);
987 wwarning("warning could not run \"%s\"", program
);
990 static WMPropList
*getValueForKey(char *domain
, char *keyName
)
993 WMPropList
*key
, *val
, *d
;
995 key
= WMCreatePLString(keyName
);
997 /* try to find PixmapPath in user defaults */
998 path
= wdefaultspathfordomain(domain
);
999 d
= WMReadPropListFromFile(path
);
1001 wwarning("could not open domain file %s", path
);
1005 if (d
&& !WMIsPLDictionary(d
)) {
1006 WMReleasePropList(d
);
1010 val
= WMGetFromPLDictionary(d
, key
);
1014 /* try to find PixmapPath in global defaults */
1016 path
= wglobaldefaultspathfordomain(domain
);
1018 wwarning("could not locate file for domain %s", domain
);
1021 d
= WMReadPropListFromFile(path
);
1025 if (d
&& !WMIsPLDictionary(d
)) {
1026 WMReleasePropList(d
);
1030 val
= WMGetFromPLDictionary(d
, key
);
1038 WMRetainPropList(val
);
1040 WMReleasePropList(key
);
1042 WMReleasePropList(d
);
1047 char *getPixmapPath(char *domain
)
1053 val
= getValueForKey(domain
, "PixmapPath");
1055 if (!val
|| !WMIsPLArray(val
)) {
1057 WMReleasePropList(val
);
1061 count
= WMGetPropListItemCount(val
);
1063 for (i
= 0; i
< count
; i
++) {
1066 v
= WMGetFromPLArray(val
, i
);
1067 if (!v
|| !WMIsPLString(v
)) {
1070 len
+= strlen(WMGetFromPLString(v
)) + 1;
1073 ptr
= data
= wmalloc(len
+ 1);
1076 for (i
= 0; i
< count
; i
++) {
1079 v
= WMGetFromPLArray(val
, i
);
1080 if (!v
|| !WMIsPLString(v
)) {
1083 strcpy(ptr
, WMGetFromPLString(v
));
1085 ptr
+= strlen(WMGetFromPLString(v
));
1093 WMReleasePropList(val
);
1098 char *getFullPixmapPath(char *file
)
1102 if (!PixmapPath
|| !(tmp
= wfindfile(PixmapPath
, file
))) {
1104 char *path
= wmalloc(bsize
);
1106 while (!getcwd(path
, bsize
)) {
1108 path
= wrealloc(path
, bsize
);
1111 tmp
= wstrconcat(path
, "/");
1113 path
= wstrconcat(tmp
, file
);
1119 /* the file is in the PixmapPath */
1122 return wstrdup(file
);
1133 printf("Usage: %s [options] [image]\n", __progname
);
1134 puts("Sets the workspace background to the specified image or a texture and");
1135 puts("optionally update Window Maker configuration");
1137 puts(" -display display to use");
1138 puts(" -d, --dither dither image");
1139 puts(" -m, --match match colors");
1140 puts(" -S, --smooth smooth scaled image");
1142 puts(" -X, --xinerama stretch image across Xinerama heads");
1144 puts(" -b, --back-color <color> background color");
1145 puts(" -t, --tile tile image");
1146 puts(" -e, --center center image");
1147 puts(" -s, --scale scale image (default)");
1148 puts(" -a, --maxscale scale image and keep aspect ratio");
1149 puts(" -f, --fillscale scale image to fill screen and keep aspect ratio");
1150 puts(" -u, --update-wmaker update WindowMaker domain database");
1151 puts(" -D, --update-domain <domain> update <domain> database");
1152 puts(" -c, --colors <cpc> colors per channel to use");
1153 puts(" -p, --parse <texture> proplist style texture specification");
1154 puts(" -w, --workspace <workspace> update background for the specified workspace");
1155 puts(" -v, --version show version of wmsetbg and exit");
1156 puts(" -h, --help show this help and exit");
1159 void changeTextureForWorkspace(char *domain
, char *texture
, int workspace
)
1161 WMPropList
*array
, *val
;
1165 val
= WMCreatePropListFromDescription(texture
);
1167 wwarning("could not parse texture %s", texture
);
1171 array
= getValueForKey("WindowMaker", "WorkspaceSpecificBack");
1174 array
= WMCreatePLArray(NULL
, NULL
);
1177 j
= WMGetPropListItemCount(array
);
1178 if (workspace
>= j
) {
1181 empty
= WMCreatePLArray(NULL
, NULL
);
1183 while (j
++ < workspace
- 1) {
1184 WMAddToPLArray(array
, empty
);
1186 WMAddToPLArray(array
, val
);
1188 WMDeleteFromPLArray(array
, workspace
);
1189 WMInsertInPLArray(array
, workspace
, val
);
1192 value
= WMGetPropListDescription(array
, False
);
1193 updateDomain(domain
, "WorkspaceSpecificBack", value
);
1196 int main(int argc
, char **argv
)
1201 RContextAttributes rattr
;
1202 char *style
= "spixmap";
1203 char *back_color
= "gray20";
1204 char *image_name
= NULL
;
1205 char *domain
= "WindowMaker";
1206 int update
= 0, cpc
= 4, render_mode
= RDitheredRendering
, obey_user
= 0;
1207 char *texture
= NULL
;
1210 signal(SIGINT
, SIG_DFL
);
1211 signal(SIGTERM
, SIG_DFL
);
1212 signal(SIGQUIT
, SIG_DFL
);
1213 signal(SIGSEGV
, SIG_DFL
);
1214 signal(SIGBUS
, SIG_DFL
);
1215 signal(SIGFPE
, SIG_DFL
);
1216 signal(SIGABRT
, SIG_DFL
);
1217 signal(SIGHUP
, SIG_DFL
);
1218 signal(SIGPIPE
, SIG_DFL
);
1219 signal(SIGCHLD
, SIG_DFL
);
1221 WMInitializeApplication("wmsetbg", &argc
, argv
);
1223 for (i
= 1; i
< argc
; i
++) {
1224 if (strcmp(argv
[i
], "-helper") == 0) {
1226 } else if (strcmp(argv
[i
], "-display") == 0) {
1229 wfatal("too few arguments for %s", argv
[i
- 1]);
1233 } else if (strcmp(argv
[i
], "-s") == 0 || strcmp(argv
[i
], "--scale") == 0) {
1235 } else if (strcmp(argv
[i
], "-t") == 0 || strcmp(argv
[i
], "--tile") == 0) {
1237 } else if (strcmp(argv
[i
], "-e") == 0 || strcmp(argv
[i
], "--center") == 0) {
1239 } else if (strcmp(argv
[i
], "-a") == 0 || strcmp(argv
[i
], "--maxscale") == 0) {
1241 } else if (strcmp(argv
[i
], "-f") == 0 || strcmp(argv
[i
], "--fillscale") == 0) {
1243 } else if (strcmp(argv
[i
], "-d") == 0 || strcmp(argv
[i
], "--dither") == 0) {
1244 render_mode
= RDitheredRendering
;
1246 } else if (strcmp(argv
[i
], "-m") == 0 || strcmp(argv
[i
], "--match") == 0) {
1247 render_mode
= RBestMatchRendering
;
1249 } else if (strcmp(argv
[i
], "-S") == 0 || strcmp(argv
[i
], "--smooth") == 0) {
1252 } else if (strcmp(argv
[i
], "-X") == 0 || strcmp(argv
[i
], "--xinerama") == 0) {
1255 } else if (strcmp(argv
[i
], "-u") == 0 || strcmp(argv
[i
], "--update-wmaker") == 0) {
1257 } else if (strcmp(argv
[i
], "-D") == 0 || strcmp(argv
[i
], "--update-domain") == 0) {
1261 wfatal("too few arguments for %s", argv
[i
- 1]);
1264 domain
= wstrdup(argv
[i
]);
1265 } else if (strcmp(argv
[i
], "-c") == 0 || strcmp(argv
[i
], "--colors") == 0) {
1268 wfatal("too few arguments for %s", argv
[i
- 1]);
1271 if (sscanf(argv
[i
], "%i", &cpc
) != 1) {
1272 wfatal("bad value for colors per channel: \"%s\"", argv
[i
]);
1275 } else if (strcmp(argv
[i
], "-b") == 0 || strcmp(argv
[i
], "--back-color") == 0) {
1278 wfatal("too few arguments for %s", argv
[i
- 1]);
1281 back_color
= argv
[i
];
1282 } else if (strcmp(argv
[i
], "-p") == 0 || strcmp(argv
[i
], "--parse") == 0) {
1285 wfatal("too few arguments for %s", argv
[i
- 1]);
1289 } else if (strcmp(argv
[i
], "-w") == 0 || strcmp(argv
[i
], "--workspace") == 0) {
1292 wfatal("too few arguments for %s", argv
[i
- 1]);
1295 if (sscanf(argv
[i
], "%i", &workspace
) != 1) {
1296 wfatal("bad value for workspace number: \"%s\"", argv
[i
]);
1299 } else if (strcmp(argv
[i
], "-v") == 0 || strcmp(argv
[i
], "--version") == 0) {
1300 printf("%s (Window Maker %s)\n", __progname
, VERSION
);
1302 } else if (strcmp(argv
[i
], "-h") == 0 || strcmp(argv
[i
], "--help") == 0) {
1305 } else if (argv
[i
][0] != '-') {
1306 image_name
= argv
[i
];
1308 printf("%s: invalid argument '%s'\n", __progname
, argv
[i
]);
1309 printf("Try '%s --help' for more information\n", __progname
);
1313 if (!image_name
&& !texture
&& !helperMode
) {
1314 printf("%s: you must specify a image file name or a texture\n", __progname
);
1315 printf("Try '%s --help' for more information\n", __progname
);
1319 PixmapPath
= getPixmapPath(domain
);
1322 /* carlos, don't remove this */
1323 #if 0 /* some problem with Alpha... TODO: check if its right */
1324 val
= WMGetFromPLDictionary(domain
, WMCreatePLString("SmoothWorkspaceBack"));
1326 val
= getValueForKey(domain
, "SmoothWorkspaceBack");
1329 if (val
&& WMIsPLString(val
) && strcasecmp(WMGetFromPLString(val
), "YES") == 0)
1333 dpy
= XOpenDisplay(display
);
1335 wfatal("could not open display");
1339 XSynchronize(dpy
, 1);
1342 root
= DefaultRootWindow(dpy
);
1344 scr
= DefaultScreen(dpy
);
1346 scrWidth
= WidthOfScreen(DefaultScreenOfDisplay(dpy
));
1347 scrHeight
= HeightOfScreen(DefaultScreenOfDisplay(dpy
));
1352 if (!obey_user
&& DefaultDepth(dpy
, scr
) <= 8)
1353 render_mode
= RDitheredRendering
;
1355 rattr
.flags
= RC_RenderMode
| RC_ColorsPerChannel
| RC_StandardColormap
| RC_DefaultVisual
;
1356 rattr
.render_mode
= render_mode
;
1357 rattr
.colors_per_channel
= cpc
;
1358 rattr
.standard_colormap_mode
= RCreateStdColormap
;
1360 rc
= RCreateContext(dpy
, scr
, &rattr
);
1363 rattr
.standard_colormap_mode
= RIgnoreStdColormap
;
1364 rc
= RCreateContext(dpy
, scr
, &rattr
);
1368 wfatal("could not initialize wrlib: %s", RMessageForError(RErrorCode
));
1373 /* lower priority, so that it wont use all the CPU */
1378 BackgroundTexture
*tex
;
1382 char *image_path
= getFullPixmapPath(image_name
);
1384 sprintf(buffer
, "(%s, \"%s\", %s)", style
, image_path
, back_color
);
1386 texture
= (char *)buffer
;
1389 if (update
&& workspace
< 0) {
1390 updateDomain(domain
, "WorkspaceBack", texture
);
1393 tex
= parseTexture(rc
, texture
);
1400 /* always update domain */
1401 changeTextureForWorkspace(domain
, texture
, workspace
);