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
)) {
178 if (toupper(type
) == 'S') {
182 if (image
->width
* height
> image
->height
* width
) {
184 h
= (width
* image
->height
) / image
->width
;
186 w
= (height
* image
->width
) / image
->height
;
191 if (w
!= image
->width
|| h
!= image
->height
) {
195 simage
= RSmoothScaleImage(image
, w
, h
);
197 simage
= RScaleImage(image
, w
, h
);
201 wwarning("could not scale image:%s", RMessageForError(RErrorCode
));
213 if (!RConvertImage(rc
, image
, &pixmap
)) {
214 wwarning("could not convert texture:%s", RMessageForError(RErrorCode
));
218 if (image
->width
!= width
|| image
->height
!= height
) {
221 if (image
->height
< height
) {
223 y
+= (height
- h
) / 2;
226 sy
= (image
->height
- height
) / 2;
229 if (image
->width
< width
) {
231 x
+= (width
- w
) / 2;
234 sx
= (image
->width
- width
) / 2;
238 XCopyArea(dpy
, pixmap
, texture
->pixmap
, DefaultGC(dpy
, scr
), sx
, sy
, w
, h
, x
, y
);
240 XCopyArea(dpy
, pixmap
, texture
->pixmap
, DefaultGC(dpy
, scr
), 0, 0, width
, height
,
243 XFreePixmap(dpy
, pixmap
);
245 RReleaseImage(image
);
252 BackgroundTexture
*parseTexture(RContext
* rc
, char *text
)
254 BackgroundTexture
*texture
= NULL
;
255 WMPropList
*texarray
;
261 #define GETSTRORGOTO(val, str, i, label) \
262 val = WMGetFromPLArray(texarray, i);\
263 if (!WMIsPLString(val)) {\
264 wwarning("could not parse texture %s", text);\
267 str = WMGetFromPLString(val)
269 texarray
= WMCreatePropListFromDescription(text
);
270 if (!texarray
|| !WMIsPLArray(texarray
)
271 || (count
= WMGetPropListItemCount(texarray
)) < 2) {
273 wwarning("could not parse texture %s", text
);
275 WMReleasePropList(texarray
);
279 texture
= wmalloc(sizeof(BackgroundTexture
));
280 memset(texture
, 0, sizeof(BackgroundTexture
));
282 GETSTRORGOTO(val
, type
, 0, error
);
284 if (strcasecmp(type
, "solid") == 0) {
290 GETSTRORGOTO(val
, tmp
, 1, error
);
292 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
293 wwarning("could not parse color %s in texture %s", tmp
, text
);
296 XAllocColor(dpy
, DefaultColormap(dpy
, scr
), &color
);
298 pixmap
= XCreatePixmap(dpy
, root
, 8, 8, DefaultDepth(dpy
, scr
));
299 XSetForeground(dpy
, DefaultGC(dpy
, scr
), color
.pixel
);
300 XFillRectangle(dpy
, pixmap
, DefaultGC(dpy
, scr
), 0, 0, 8, 8);
302 texture
->pixmap
= pixmap
;
303 texture
->color
= color
;
306 } else if (strcasecmp(type
, "vgradient") == 0
307 || strcasecmp(type
, "dgradient") == 0 || strcasecmp(type
, "hgradient") == 0) {
309 RColor color1
, color2
;
315 GETSTRORGOTO(val
, tmp
, 1, error
);
317 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
318 wwarning("could not parse color %s in texture %s", tmp
, text
);
322 color1
.red
= color
.red
>> 8;
323 color1
.green
= color
.green
>> 8;
324 color1
.blue
= color
.blue
>> 8;
326 GETSTRORGOTO(val
, tmp
, 2, error
);
328 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
329 wwarning("could not parse color %s in texture %s", tmp
, text
);
333 color2
.red
= color
.red
>> 8;
334 color2
.green
= color
.green
>> 8;
335 color2
.blue
= color
.blue
>> 8;
340 gtype
= RHorizontalGradient
;
346 gtype
= RVerticalGradient
;
351 gtype
= RDiagonalGradient
;
357 image
= RRenderGradient(iwidth
, iheight
, &color1
, &color2
, gtype
);
360 wwarning("could not render gradient texture:%s", RMessageForError(RErrorCode
));
364 if (!RConvertImage(rc
, image
, &pixmap
)) {
365 wwarning("could not convert texture:%s", RMessageForError(RErrorCode
));
366 RReleaseImage(image
);
370 texture
->width
= image
->width
;
371 texture
->height
= image
->height
;
372 RReleaseImage(image
);
374 texture
->pixmap
= pixmap
;
375 } else if (strcasecmp(type
, "mvgradient") == 0
376 || strcasecmp(type
, "mdgradient") == 0 || strcasecmp(type
, "mhgradient") == 0) {
385 colors
= malloc(sizeof(RColor
*) * (count
- 1));
387 wwarning("out of memory while parsing texture");
390 memset(colors
, 0, sizeof(RColor
*) * (count
- 1));
392 for (i
= 2; i
< count
; i
++) {
393 val
= WMGetFromPLArray(texarray
, i
);
394 if (!WMIsPLString(val
)) {
395 wwarning("could not parse texture %s", text
);
397 for (j
= 0; colors
[j
] != NULL
; j
++)
402 tmp
= WMGetFromPLString(val
);
404 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
405 wwarning("could not parse color %s in texture %s", tmp
, text
);
407 for (j
= 0; colors
[j
] != NULL
; j
++)
412 if (!(colors
[i
- 2] = malloc(sizeof(RColor
)))) {
413 wwarning("out of memory while parsing texture");
415 for (j
= 0; colors
[j
] != NULL
; j
++)
421 colors
[i
- 2]->red
= color
.red
>> 8;
422 colors
[i
- 2]->green
= color
.green
>> 8;
423 colors
[i
- 2]->blue
= color
.blue
>> 8;
429 gtype
= RHorizontalGradient
;
435 gtype
= RVerticalGradient
;
440 gtype
= RDiagonalGradient
;
446 image
= RRenderMultiGradient(iwidth
, iheight
, colors
, gtype
);
448 for (j
= 0; colors
[j
] != NULL
; j
++)
453 wwarning("could not render gradient texture:%s", RMessageForError(RErrorCode
));
457 if (!RConvertImage(rc
, image
, &pixmap
)) {
458 wwarning("could not convert texture:%s", RMessageForError(RErrorCode
));
459 RReleaseImage(image
);
463 texture
->width
= image
->width
;
464 texture
->height
= image
->height
;
465 RReleaseImage(image
);
467 texture
->pixmap
= pixmap
;
468 } else if (strcasecmp(type
, "cpixmap") == 0
469 || strcasecmp(type
, "spixmap") == 0
470 || strcasecmp(type
, "mpixmap") == 0 || strcasecmp(type
, "tpixmap") == 0) {
472 Pixmap pixmap
= None
;
473 RImage
*image
= NULL
;
474 int iwidth
= 0, iheight
= 0;
477 GETSTRORGOTO(val
, tmp
, 1, error
);
479 if (toupper(type[0]) == 'T' || toupper(type[0]) == 'C')
480 pixmap = LoadJPEG(rc, tmp, &iwidth, &iheight);
484 image
= loadImage(rc
, tmp
);
488 iwidth
= image
->width
;
489 iheight
= image
->height
;
492 GETSTRORGOTO(val
, tmp
, 2, error
);
494 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
495 wwarning("could not parse color %s in texture %s", tmp
, text
);
496 RReleaseImage(image
);
499 if (!XAllocColor(dpy
, DefaultColormap(dpy
, scr
), &color
)) {
500 rcolor
.red
= color
.red
>> 8;
501 rcolor
.green
= color
.green
>> 8;
502 rcolor
.blue
= color
.blue
>> 8;
503 RGetClosestXColor(rc
, &rcolor
, &color
);
509 /* for images with a transparent color */
510 if (image
->data
[3]) {
511 RCombineImageWithColor(image
, &rcolor
);
514 switch (toupper(type
[0])) {
516 texture
->width
= iwidth
;
517 texture
->height
= iheight
;
518 if (!pixmap
&& !RConvertImage(rc
, image
, &pixmap
)) {
519 wwarning("could not convert texture:%s", RMessageForError(RErrorCode
));
520 RReleaseImage(image
);
524 RReleaseImage(image
);
526 texture
->pixmap
= pixmap
;
527 texture
->color
= color
;
534 XCreatePixmap(dpy
, root
, scrWidth
, scrHeight
, DefaultDepth(dpy
, scr
));
535 XSetForeground(dpy
, DefaultGC(dpy
, scr
), color
.pixel
);
536 XFillRectangle(dpy
, tpixmap
, DefaultGC(dpy
, scr
), 0, 0, scrWidth
, scrHeight
);
538 texture
->pixmap
= tpixmap
;
539 texture
->color
= color
;
540 texture
->width
= scrWidth
;
541 texture
->height
= scrHeight
;
544 if (xineInfo
.count
&& ! xineStretch
) {
546 for (i
= 0; i
< xineInfo
.count
; ++i
) {
547 applyImage(rc
, texture
, image
, type
[0],
548 xineInfo
.screens
[i
].pos
.x
, xineInfo
.screens
[i
].pos
.y
,
549 xineInfo
.screens
[i
].size
.width
,
550 xineInfo
.screens
[i
].size
.height
);
553 applyImage(rc
, texture
, image
, type
[0], 0, 0, scrWidth
, scrHeight
);
555 #else /* !XINERAMA */
556 applyImage(rc
, texture
, image
, type
[0], 0, 0, scrWidth
, scrHeight
);
557 #endif /* !XINERAMA */
558 RReleaseImage(image
);
562 } else if (strcasecmp(type
, "thgradient") == 0
563 || strcasecmp(type
, "tvgradient") == 0 || strcasecmp(type
, "tdgradient") == 0) {
565 RColor color1
, color2
;
575 GETSTRORGOTO(val
, file
, 1, error
);
577 GETSTRORGOTO(val
, tmp
, 2, error
);
581 GETSTRORGOTO(val
, tmp
, 3, error
);
583 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
584 wwarning("could not parse color %s in texture %s", tmp
, text
);
588 color1
.red
= color
.red
>> 8;
589 color1
.green
= color
.green
>> 8;
590 color1
.blue
= color
.blue
>> 8;
592 GETSTRORGOTO(val
, tmp
, 4, error
);
594 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
595 wwarning("could not parse color %s in texture %s", tmp
, text
);
599 color2
.red
= color
.red
>> 8;
600 color2
.green
= color
.green
>> 8;
601 color2
.blue
= color
.blue
>> 8;
603 image
= loadImage(rc
, file
);
611 gtype
= RHorizontalGradient
;
613 theight
= image
->height
> scrHeight
? scrHeight
: image
->height
;
617 gtype
= RVerticalGradient
;
618 twidth
= image
->width
> scrWidth
? scrWidth
: image
->width
;
622 gtype
= RDiagonalGradient
;
627 gradient
= RRenderGradient(twidth
, theight
, &color1
, &color2
, gtype
);
630 wwarning("could not render texture:%s", RMessageForError(RErrorCode
));
631 RReleaseImage(gradient
);
632 RReleaseImage(image
);
636 tiled
= RMakeTiledImage(image
, twidth
, theight
);
638 wwarning("could not render texture:%s", RMessageForError(RErrorCode
));
639 RReleaseImage(gradient
);
640 RReleaseImage(image
);
643 RReleaseImage(image
);
645 RCombineImagesWithOpaqueness(tiled
, gradient
, opaq
);
646 RReleaseImage(gradient
);
648 if (!RConvertImage(rc
, tiled
, &pixmap
)) {
649 wwarning("could not convert texture:%s", RMessageForError(RErrorCode
));
650 RReleaseImage(tiled
);
653 texture
->width
= tiled
->width
;
654 texture
->height
= tiled
->height
;
656 RReleaseImage(tiled
);
658 texture
->pixmap
= pixmap
;
659 } else if (strcasecmp(type
, "function") == 0) {
660 /* Leave this in to handle the unlikely case of
661 * someone actually having function textures configured */
662 wwarning("function texture support has been removed");
665 wwarning("invalid texture type %s", text
);
669 texture
->spec
= wstrdup(text
);
677 WMReleasePropList(texarray
);
682 void freeTexture(BackgroundTexture
* texture
)
684 if (texture
->solid
) {
685 unsigned long pixel
[1];
687 pixel
[0] = texture
->color
.pixel
;
688 /* dont free black/white pixels */
689 if (pixel
[0] != BlackPixelOfScreen(DefaultScreenOfDisplay(dpy
))
690 && pixel
[0] != WhitePixelOfScreen(DefaultScreenOfDisplay(dpy
)))
691 XFreeColors(dpy
, DefaultColormap(dpy
, scr
), pixel
, 1, 0);
693 if (texture
->pixmap
) {
694 XFreePixmap(dpy
, texture
->pixmap
);
696 wfree(texture
->spec
);
700 void setupTexture(RContext
* rc
, BackgroundTexture
** textures
, int *maxTextures
, int workspace
, char *texture
)
702 BackgroundTexture
*newTexture
= NULL
;
705 /* unset the texture */
707 if (textures
[workspace
] != NULL
) {
708 textures
[workspace
]->refcount
--;
710 if (textures
[workspace
]->refcount
== 0)
711 freeTexture(textures
[workspace
]);
713 textures
[workspace
] = NULL
;
717 if (textures
[workspace
]
718 && strcasecmp(textures
[workspace
]->spec
, texture
) == 0) {
719 /* texture did not change */
723 /* check if the same texture is already created */
724 for (i
= 0; i
< *maxTextures
; i
++) {
725 if (textures
[i
] && strcasecmp(textures
[i
]->spec
, texture
) == 0) {
726 newTexture
= textures
[i
];
732 /* create the texture */
733 newTexture
= parseTexture(rc
, texture
);
738 if (textures
[workspace
] != NULL
) {
740 textures
[workspace
]->refcount
--;
742 if (textures
[workspace
]->refcount
== 0)
743 freeTexture(textures
[workspace
]);
746 newTexture
->refcount
++;
747 textures
[workspace
] = newTexture
;
749 if (*maxTextures
< workspace
)
750 *maxTextures
= workspace
;
753 Pixmap
duplicatePixmap(Pixmap pixmap
, int width
, int height
)
758 /* must open a new display or the RetainPermanent will
759 * leave stuff allocated in RContext unallocated after exit */
760 tmpDpy
= XOpenDisplay(display
);
762 wwarning("could not open display to update background image information");
768 copyP
= XCreatePixmap(tmpDpy
, root
, width
, height
, DefaultDepth(tmpDpy
, scr
));
769 XCopyArea(tmpDpy
, pixmap
, copyP
, DefaultGC(tmpDpy
, scr
), 0, 0, width
, height
, 0, 0);
770 XSync(tmpDpy
, False
);
772 XSetCloseDownMode(tmpDpy
, RetainPermanent
);
773 XCloseDisplay(tmpDpy
);
779 static int dummyErrorHandler(Display
* dpy
, XErrorEvent
* err
)
784 void setPixmapProperty(Pixmap pixmap
)
786 static Atom prop
= 0;
789 unsigned long length
, after
;
794 prop
= XInternAtom(dpy
, "_XROOTPMAP_ID", False
);
799 /* Clear out the old pixmap */
800 XGetWindowProperty(dpy
, root
, prop
, 0L, 1L, False
, AnyPropertyType
,
801 &type
, &format
, &length
, &after
, &data
);
803 if ((type
== XA_PIXMAP
) && (format
== 32) && (length
== 1)) {
804 XSetErrorHandler(dummyErrorHandler
);
805 XKillClient(dpy
, *((Pixmap
*) data
));
807 XSetErrorHandler(NULL
);
808 mode
= PropModeReplace
;
810 mode
= PropModeAppend
;
813 XChangeProperty(dpy
, root
, prop
, XA_PIXMAP
, 32, mode
, (unsigned char *)&pixmap
, 1);
815 XDeleteProperty(dpy
, root
, prop
);
821 void changeTexture(BackgroundTexture
* texture
)
827 if (texture
->solid
) {
828 XSetWindowBackground(dpy
, root
, texture
->color
.pixel
);
830 XSetWindowBackgroundPixmap(dpy
, root
, texture
->pixmap
);
832 XClearWindow(dpy
, root
);
839 pixmap
= duplicatePixmap(texture
->pixmap
, texture
->width
, texture
->height
);
841 setPixmapProperty(pixmap
);
845 int readmsg(int fd
, char *buffer
, int size
)
851 count
= read(fd
, buffer
, size
);
864 * sizeSntexture_spec - sets the texture for workspace n
865 * sizeCn - change background texture to the one for workspace n
866 * sizePpath - set the pixmap search path
869 * size = 4 bytes for length of the message data
871 void helperLoop(RContext
* rc
)
873 BackgroundTexture
*textures
[WORKSPACE_COUNT
];
875 char buffer
[2048], buf
[8];
879 memset(textures
, 0, WORKSPACE_COUNT
* sizeof(BackgroundTexture
*));
884 /* get length of message */
885 if (readmsg(0, buffer
, 4) < 0) {
886 werror("error reading message from Window Maker");
894 memcpy(buf
, buffer
, 4);
899 if (readmsg(0, buffer
, size
) < 0) {
900 werror("error reading message from Window Maker");
909 printf("RECEIVED %s\n", buffer
);
911 if (buffer
[0] != 'P' && buffer
[0] != 'K') {
912 memcpy(buf
, &buffer
[1], 4);
914 workspace
= atoi(buf
);
915 if (workspace
< 0 || workspace
>= WORKSPACE_COUNT
) {
916 wwarning("received message with invalid workspace number %i", workspace
);
924 printf("set texture %s\n", &buffer
[5]);
926 setupTexture(rc
, textures
, &maxTextures
, workspace
, &buffer
[5]);
931 printf("change texture %i\n", workspace
);
933 if (!textures
[workspace
]) {
934 changeTexture(textures
[0]);
936 changeTexture(textures
[workspace
]);
942 printf("change pixmappath %s\n", &buffer
[1]);
946 PixmapPath
= wstrdup(&buffer
[1]);
951 printf("unset workspace %i\n", workspace
);
953 setupTexture(rc
, textures
, &maxTextures
, workspace
, NULL
);
958 printf("exit command\n");
963 wwarning("unknown message received");
969 void updateDomain(char *domain
, char *key
, char *texture
)
971 char *program
= "wdwrite";
973 /* here is a mem leak */
974 system(wstrconcat("wdwrite ",
975 wstrconcat(domain
, smooth
? " SmoothWorkspaceBack YES" : " SmoothWorkspaceBack NO")));
977 execlp(program
, program
, domain
, key
, texture
, NULL
);
978 wwarning("warning could not run \"%s\"", program
);
981 static WMPropList
*getValueForKey(char *domain
, char *keyName
)
984 WMPropList
*key
, *val
, *d
;
986 key
= WMCreatePLString(keyName
);
988 /* try to find PixmapPath in user defaults */
989 path
= wdefaultspathfordomain(domain
);
990 d
= WMReadPropListFromFile(path
);
992 wwarning("could not open domain file %s", path
);
996 if (d
&& !WMIsPLDictionary(d
)) {
997 WMReleasePropList(d
);
1001 val
= WMGetFromPLDictionary(d
, key
);
1005 /* try to find PixmapPath in global defaults */
1007 path
= wglobaldefaultspathfordomain(domain
);
1009 wwarning("could not locate file for domain %s", domain
);
1012 d
= WMReadPropListFromFile(path
);
1016 if (d
&& !WMIsPLDictionary(d
)) {
1017 WMReleasePropList(d
);
1021 val
= WMGetFromPLDictionary(d
, key
);
1029 WMRetainPropList(val
);
1031 WMReleasePropList(key
);
1033 WMReleasePropList(d
);
1038 char *getPixmapPath(char *domain
)
1044 val
= getValueForKey(domain
, "PixmapPath");
1046 if (!val
|| !WMIsPLArray(val
)) {
1048 WMReleasePropList(val
);
1052 count
= WMGetPropListItemCount(val
);
1054 for (i
= 0; i
< count
; i
++) {
1057 v
= WMGetFromPLArray(val
, i
);
1058 if (!v
|| !WMIsPLString(v
)) {
1061 len
+= strlen(WMGetFromPLString(v
)) + 1;
1064 ptr
= data
= wmalloc(len
+ 1);
1067 for (i
= 0; i
< count
; i
++) {
1070 v
= WMGetFromPLArray(val
, i
);
1071 if (!v
|| !WMIsPLString(v
)) {
1074 strcpy(ptr
, WMGetFromPLString(v
));
1076 ptr
+= strlen(WMGetFromPLString(v
));
1084 WMReleasePropList(val
);
1089 char *getFullPixmapPath(char *file
)
1093 if (!PixmapPath
|| !(tmp
= wfindfile(PixmapPath
, file
))) {
1095 char *path
= wmalloc(bsize
);
1097 while (!getcwd(path
, bsize
)) {
1099 path
= wrealloc(path
, bsize
);
1102 tmp
= wstrconcat(path
, "/");
1104 path
= wstrconcat(tmp
, file
);
1110 /* the file is in the PixmapPath */
1113 return wstrdup(file
);
1124 printf("Usage: %s [options] [image]\n", __progname
);
1125 puts("Sets the workspace background to the specified image or a texture and");
1126 puts("optionally update Window Maker configuration");
1128 puts(" -display display to use");
1129 puts(" -d, --dither dither image");
1130 puts(" -m, --match match colors");
1131 puts(" -S, --smooth smooth scaled image");
1133 puts(" -X, --xinerama stretch image across Xinerama heads");
1135 puts(" -b, --back-color <color> background color");
1136 puts(" -t, --tile tile image");
1137 puts(" -e, --center center image");
1138 puts(" -s, --scale scale image (default)");
1139 puts(" -a, --maxscale scale image and keep aspect ratio");
1140 puts(" -u, --update-wmaker update WindowMaker domain database");
1141 puts(" -D, --update-domain <domain> update <domain> database");
1142 puts(" -c, --colors <cpc> colors per channel to use");
1143 puts(" -p, --parse <texture> proplist style texture specification");
1144 puts(" -w, --workspace <workspace> update background for the specified workspace");
1145 puts(" -v, --version show version of wmsetbg and exit");
1146 puts(" -h, --help show this help and exit");
1149 void changeTextureForWorkspace(char *domain
, char *texture
, int workspace
)
1151 WMPropList
*array
, *val
;
1155 val
= WMCreatePropListFromDescription(texture
);
1157 wwarning("could not parse texture %s", texture
);
1161 array
= getValueForKey("WindowMaker", "WorkspaceSpecificBack");
1164 array
= WMCreatePLArray(NULL
, NULL
);
1167 j
= WMGetPropListItemCount(array
);
1168 if (workspace
>= j
) {
1171 empty
= WMCreatePLArray(NULL
, NULL
);
1173 while (j
++ < workspace
- 1) {
1174 WMAddToPLArray(array
, empty
);
1176 WMAddToPLArray(array
, val
);
1178 WMDeleteFromPLArray(array
, workspace
);
1179 WMInsertInPLArray(array
, workspace
, val
);
1182 value
= WMGetPropListDescription(array
, False
);
1183 updateDomain(domain
, "WorkspaceSpecificBack", value
);
1186 int main(int argc
, char **argv
)
1191 RContextAttributes rattr
;
1192 char *style
= "spixmap";
1193 char *back_color
= "gray20";
1194 char *image_name
= NULL
;
1195 char *domain
= "WindowMaker";
1196 int update
= 0, cpc
= 4, render_mode
= RDitheredRendering
, obey_user
= 0;
1197 char *texture
= NULL
;
1200 signal(SIGINT
, SIG_DFL
);
1201 signal(SIGTERM
, SIG_DFL
);
1202 signal(SIGQUIT
, SIG_DFL
);
1203 signal(SIGSEGV
, SIG_DFL
);
1204 signal(SIGBUS
, SIG_DFL
);
1205 signal(SIGFPE
, SIG_DFL
);
1206 signal(SIGABRT
, SIG_DFL
);
1207 signal(SIGHUP
, SIG_DFL
);
1208 signal(SIGPIPE
, SIG_DFL
);
1209 signal(SIGCHLD
, SIG_DFL
);
1211 WMInitializeApplication("wmsetbg", &argc
, argv
);
1213 for (i
= 1; i
< argc
; i
++) {
1214 if (strcmp(argv
[i
], "-helper") == 0) {
1216 } else if (strcmp(argv
[i
], "-display") == 0) {
1219 wfatal("too few arguments for %s", argv
[i
- 1]);
1223 } else if (strcmp(argv
[i
], "-s") == 0 || strcmp(argv
[i
], "--scale") == 0) {
1225 } else if (strcmp(argv
[i
], "-t") == 0 || strcmp(argv
[i
], "--tile") == 0) {
1227 } else if (strcmp(argv
[i
], "-e") == 0 || strcmp(argv
[i
], "--center") == 0) {
1229 } else if (strcmp(argv
[i
], "-a") == 0 || strcmp(argv
[i
], "--maxscale") == 0) {
1231 } else if (strcmp(argv
[i
], "-d") == 0 || strcmp(argv
[i
], "--dither") == 0) {
1232 render_mode
= RDitheredRendering
;
1234 } else if (strcmp(argv
[i
], "-m") == 0 || strcmp(argv
[i
], "--match") == 0) {
1235 render_mode
= RBestMatchRendering
;
1237 } else if (strcmp(argv
[i
], "-S") == 0 || strcmp(argv
[i
], "--smooth") == 0) {
1240 } else if (strcmp(argv
[i
], "-X") == 0 || strcmp(argv
[i
], "--xinerama") == 0) {
1243 } else if (strcmp(argv
[i
], "-u") == 0 || strcmp(argv
[i
], "--update-wmaker") == 0) {
1245 } else if (strcmp(argv
[i
], "-D") == 0 || strcmp(argv
[i
], "--update-domain") == 0) {
1249 wfatal("too few arguments for %s", argv
[i
- 1]);
1252 domain
= wstrdup(argv
[i
]);
1253 } else if (strcmp(argv
[i
], "-c") == 0 || strcmp(argv
[i
], "--colors") == 0) {
1256 wfatal("too few arguments for %s", argv
[i
- 1]);
1259 if (sscanf(argv
[i
], "%i", &cpc
) != 1) {
1260 wfatal("bad value for colors per channel: \"%s\"", argv
[i
]);
1263 } else if (strcmp(argv
[i
], "-b") == 0 || strcmp(argv
[i
], "--back-color") == 0) {
1266 wfatal("too few arguments for %s", argv
[i
- 1]);
1269 back_color
= argv
[i
];
1270 } else if (strcmp(argv
[i
], "-p") == 0 || strcmp(argv
[i
], "--parse") == 0) {
1273 wfatal("too few arguments for %s", argv
[i
- 1]);
1277 } else if (strcmp(argv
[i
], "-w") == 0 || strcmp(argv
[i
], "--workspace") == 0) {
1280 wfatal("too few arguments for %s", argv
[i
- 1]);
1283 if (sscanf(argv
[i
], "%i", &workspace
) != 1) {
1284 wfatal("bad value for workspace number: \"%s\"", argv
[i
]);
1287 } else if (strcmp(argv
[i
], "-v") == 0 || strcmp(argv
[i
], "--version") == 0) {
1288 printf("%s (Window Maker %s)\n", __progname
, VERSION
);
1290 } else if (strcmp(argv
[i
], "-h") == 0 || strcmp(argv
[i
], "--help") == 0) {
1293 } else if (argv
[i
][0] != '-') {
1294 image_name
= argv
[i
];
1296 printf("%s: invalid argument '%s'\n", __progname
, argv
[i
]);
1297 printf("Try '%s --help' for more information\n", __progname
);
1301 if (!image_name
&& !texture
&& !helperMode
) {
1302 printf("%s: you must specify a image file name or a texture\n", __progname
);
1303 printf("Try '%s --help' for more information\n", __progname
);
1307 PixmapPath
= getPixmapPath(domain
);
1310 /* carlos, don't remove this */
1311 #if 0 /* some problem with Alpha... TODO: check if its right */
1312 val
= WMGetFromPLDictionary(domain
, WMCreatePLString("SmoothWorkspaceBack"));
1314 val
= getValueForKey(domain
, "SmoothWorkspaceBack");
1317 if (val
&& WMIsPLString(val
) && strcasecmp(WMGetFromPLString(val
), "YES") == 0)
1321 dpy
= XOpenDisplay(display
);
1323 wfatal("could not open display");
1327 XSynchronize(dpy
, 1);
1330 root
= DefaultRootWindow(dpy
);
1332 scr
= DefaultScreen(dpy
);
1334 scrWidth
= WidthOfScreen(DefaultScreenOfDisplay(dpy
));
1335 scrHeight
= HeightOfScreen(DefaultScreenOfDisplay(dpy
));
1340 if (!obey_user
&& DefaultDepth(dpy
, scr
) <= 8)
1341 render_mode
= RDitheredRendering
;
1343 rattr
.flags
= RC_RenderMode
| RC_ColorsPerChannel
| RC_StandardColormap
| RC_DefaultVisual
;
1344 rattr
.render_mode
= render_mode
;
1345 rattr
.colors_per_channel
= cpc
;
1346 rattr
.standard_colormap_mode
= RCreateStdColormap
;
1348 rc
= RCreateContext(dpy
, scr
, &rattr
);
1351 rattr
.standard_colormap_mode
= RIgnoreStdColormap
;
1352 rc
= RCreateContext(dpy
, scr
, &rattr
);
1356 wfatal("could not initialize wrlib: %s", RMessageForError(RErrorCode
));
1361 /* lower priority, so that it wont use all the CPU */
1366 BackgroundTexture
*tex
;
1370 char *image_path
= getFullPixmapPath(image_name
);
1372 sprintf(buffer
, "(%s, \"%s\", %s)", style
, image_path
, back_color
);
1374 texture
= (char *)buffer
;
1377 if (update
&& workspace
< 0) {
1378 updateDomain(domain
, "WorkspaceBack", texture
);
1381 tex
= parseTexture(rc
, texture
);
1388 /* always update domain */
1389 changeTextureForWorkspace(domain
, texture
, workspace
);