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>
54 #include "../src/wconfig.h"
56 #ifndef GLOBAL_DEFAULTS_SUBDIR
57 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
60 #include <WINGs/WINGs.h>
65 int count
; /* screen count, 0 = inactive */
68 #define PROG_VERSION "wmsetbg (Window Maker) 2.8"
70 #define WORKSPACE_COUNT (MAX_WORKSPACES+1)
80 WXineramaInfo xineInfo
;
84 Bool xineStretch
= False
;
87 Pixmap CurrentPixmap
= None
;
88 char *PixmapPath
= NULL
;
90 extern Pixmap
LoadJPEG(RContext
* rc
, char *file_name
, int *width
, int *height
);
91 extern char *__progname
;
93 typedef struct BackgroundTexture
{
101 Pixmap pixmap
; /* for all textures, including solid */
102 int width
; /* size of the pixmap */
108 xineInfo
.screens
= NULL
;
111 # ifdef SOLARIS_XINERAMA
112 if (XineramaGetState(dpy
, scr
)) {
113 XRectangle head
[MAXFRAMEBUFFERS
];
114 unsigned char hints
[MAXFRAMEBUFFERS
];
117 if (XineramaGetInfo(dpy
, scr
, head
, hints
, &xineInfo
.count
)) {
119 xineInfo
.screens
= wmalloc(sizeof(WMRect
) * (xineInfo
.count
+ 1));
121 for (i
= 0; i
< xineInfo
.count
; i
++) {
122 xineInfo
.screens
[i
].pos
.x
= head
[i
].x
;
123 xineInfo
.screens
[i
].pos
.y
= head
[i
].y
;
124 xineInfo
.screens
[i
].size
.width
= head
[i
].width
;
125 xineInfo
.screens
[i
].size
.height
= head
[i
].height
;
129 # else /* !SOLARIS_XINERAMA */
130 if (XineramaIsActive(dpy
)) {
131 XineramaScreenInfo
*xine_screens
;
134 xine_screens
= XineramaQueryScreens(dpy
, &xineInfo
.count
);
136 xineInfo
.screens
= wmalloc(sizeof(WMRect
) * (xineInfo
.count
+ 1));
138 for (i
= 0; i
< xineInfo
.count
; i
++) {
139 xineInfo
.screens
[i
].pos
.x
= xine_screens
[i
].x_org
;
140 xineInfo
.screens
[i
].pos
.y
= xine_screens
[i
].y_org
;
141 xineInfo
.screens
[i
].size
.width
= xine_screens
[i
].width
;
142 xineInfo
.screens
[i
].size
.height
= xine_screens
[i
].height
;
146 # endif /* !SOLARIS_XINERAMA */
147 #endif /* XINERAMA */
150 RImage
*loadImage(RContext
* rc
, char *file
)
155 if (access(file
, F_OK
) != 0) {
156 path
= wfindfile(PixmapPath
, file
);
158 wwarning("%s:could not find image file used in texture", file
);
162 path
= wstrdup(file
);
165 image
= RLoadImage(rc
, path
, 0);
167 wwarning("%s:could not load image file used in texture:%s", path
, RMessageForError(RErrorCode
));
175 applyImage(RContext
* rc
, BackgroundTexture
* texture
, RImage
* image
, char type
,
176 int x
, int y
, int width
, int height
)
181 switch (toupper(type
)) {
184 if (toupper(type
) == 'S') {
188 if (image
->width
* height
> image
->height
* width
) {
190 h
= (width
* image
->height
) / image
->width
;
192 w
= (height
* image
->width
) / image
->height
;
197 if (w
!= image
->width
|| h
!= image
->height
) {
201 simage
= RSmoothScaleImage(image
, w
, h
);
203 simage
= RScaleImage(image
, w
, h
);
207 wwarning("could not scale image:%s", RMessageForError(RErrorCode
));
219 if (!RConvertImage(rc
, image
, &pixmap
)) {
220 wwarning("could not convert texture:%s", RMessageForError(RErrorCode
));
224 if (image
->width
!= width
|| image
->height
!= height
) {
227 if (image
->height
< height
) {
229 y
+= (height
- h
) / 2;
232 sy
= (image
->height
- height
) / 2;
235 if (image
->width
< width
) {
237 x
+= (width
- w
) / 2;
240 sx
= (image
->width
- width
) / 2;
244 XCopyArea(dpy
, pixmap
, texture
->pixmap
, DefaultGC(dpy
, scr
), sx
, sy
, w
, h
, x
, y
);
246 XCopyArea(dpy
, pixmap
, texture
->pixmap
, DefaultGC(dpy
, scr
), 0, 0, width
, height
,
249 XFreePixmap(dpy
, pixmap
);
251 RReleaseImage(image
);
258 BackgroundTexture
*parseTexture(RContext
* rc
, char *text
)
260 BackgroundTexture
*texture
= NULL
;
261 WMPropList
*texarray
;
267 #define GETSTRORGOTO(val, str, i, label) \
268 val = WMGetFromPLArray(texarray, i);\
269 if (!WMIsPLString(val)) {\
270 wwarning("could not parse texture %s", text);\
273 str = WMGetFromPLString(val)
275 texarray
= WMCreatePropListFromDescription(text
);
276 if (!texarray
|| !WMIsPLArray(texarray
)
277 || (count
= WMGetPropListItemCount(texarray
)) < 2) {
279 wwarning("could not parse texture %s", text
);
281 WMReleasePropList(texarray
);
285 texture
= wmalloc(sizeof(BackgroundTexture
));
286 memset(texture
, 0, sizeof(BackgroundTexture
));
288 GETSTRORGOTO(val
, type
, 0, error
);
290 if (strcasecmp(type
, "solid") == 0) {
296 GETSTRORGOTO(val
, tmp
, 1, error
);
298 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
299 wwarning("could not parse color %s in texture %s", tmp
, text
);
302 XAllocColor(dpy
, DefaultColormap(dpy
, scr
), &color
);
304 pixmap
= XCreatePixmap(dpy
, root
, 8, 8, DefaultDepth(dpy
, scr
));
305 XSetForeground(dpy
, DefaultGC(dpy
, scr
), color
.pixel
);
306 XFillRectangle(dpy
, pixmap
, DefaultGC(dpy
, scr
), 0, 0, 8, 8);
308 texture
->pixmap
= pixmap
;
309 texture
->color
= color
;
312 } else if (strcasecmp(type
, "vgradient") == 0
313 || strcasecmp(type
, "dgradient") == 0 || strcasecmp(type
, "hgradient") == 0) {
315 RColor color1
, color2
;
321 GETSTRORGOTO(val
, tmp
, 1, error
);
323 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
324 wwarning("could not parse color %s in texture %s", tmp
, text
);
328 color1
.red
= color
.red
>> 8;
329 color1
.green
= color
.green
>> 8;
330 color1
.blue
= color
.blue
>> 8;
332 GETSTRORGOTO(val
, tmp
, 2, error
);
334 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
335 wwarning("could not parse color %s in texture %s", tmp
, text
);
339 color2
.red
= color
.red
>> 8;
340 color2
.green
= color
.green
>> 8;
341 color2
.blue
= color
.blue
>> 8;
346 gtype
= RHorizontalGradient
;
352 gtype
= RVerticalGradient
;
357 gtype
= RDiagonalGradient
;
363 image
= RRenderGradient(iwidth
, iheight
, &color1
, &color2
, gtype
);
366 wwarning("could not render gradient texture:%s", RMessageForError(RErrorCode
));
370 if (!RConvertImage(rc
, image
, &pixmap
)) {
371 wwarning("could not convert texture:%s", RMessageForError(RErrorCode
));
372 RReleaseImage(image
);
376 texture
->width
= image
->width
;
377 texture
->height
= image
->height
;
378 RReleaseImage(image
);
380 texture
->pixmap
= pixmap
;
381 } else if (strcasecmp(type
, "mvgradient") == 0
382 || strcasecmp(type
, "mdgradient") == 0 || strcasecmp(type
, "mhgradient") == 0) {
391 colors
= malloc(sizeof(RColor
*) * (count
- 1));
393 wwarning("out of memory while parsing texture");
396 memset(colors
, 0, sizeof(RColor
*) * (count
- 1));
398 for (i
= 2; i
< count
; i
++) {
399 val
= WMGetFromPLArray(texarray
, i
);
400 if (!WMIsPLString(val
)) {
401 wwarning("could not parse texture %s", text
);
403 for (j
= 0; colors
[j
] != NULL
; j
++)
408 tmp
= WMGetFromPLString(val
);
410 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
411 wwarning("could not parse color %s in texture %s", tmp
, text
);
413 for (j
= 0; colors
[j
] != NULL
; j
++)
418 if (!(colors
[i
- 2] = malloc(sizeof(RColor
)))) {
419 wwarning("out of memory while parsing texture");
421 for (j
= 0; colors
[j
] != NULL
; j
++)
427 colors
[i
- 2]->red
= color
.red
>> 8;
428 colors
[i
- 2]->green
= color
.green
>> 8;
429 colors
[i
- 2]->blue
= color
.blue
>> 8;
435 gtype
= RHorizontalGradient
;
441 gtype
= RVerticalGradient
;
446 gtype
= RDiagonalGradient
;
452 image
= RRenderMultiGradient(iwidth
, iheight
, colors
, gtype
);
454 for (j
= 0; colors
[j
] != NULL
; j
++)
459 wwarning("could not render gradient texture:%s", RMessageForError(RErrorCode
));
463 if (!RConvertImage(rc
, image
, &pixmap
)) {
464 wwarning("could not convert texture:%s", RMessageForError(RErrorCode
));
465 RReleaseImage(image
);
469 texture
->width
= image
->width
;
470 texture
->height
= image
->height
;
471 RReleaseImage(image
);
473 texture
->pixmap
= pixmap
;
474 } else if (strcasecmp(type
, "cpixmap") == 0
475 || strcasecmp(type
, "spixmap") == 0
476 || strcasecmp(type
, "mpixmap") == 0 || strcasecmp(type
, "tpixmap") == 0) {
478 Pixmap pixmap
= None
;
479 RImage
*image
= NULL
;
483 GETSTRORGOTO(val
, tmp
, 1, error
);
485 if (toupper(type[0]) == 'T' || toupper(type[0]) == 'C')
486 pixmap = LoadJPEG(rc, tmp, &iwidth, &iheight);
490 image
= loadImage(rc
, tmp
);
494 iwidth
= image
->width
;
495 iheight
= image
->height
;
498 GETSTRORGOTO(val
, tmp
, 2, error
);
500 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
501 wwarning("could not parse color %s in texture %s\n", tmp
, text
);
502 RReleaseImage(image
);
505 if (!XAllocColor(dpy
, DefaultColormap(dpy
, scr
), &color
)) {
506 rcolor
.red
= color
.red
>> 8;
507 rcolor
.green
= color
.green
>> 8;
508 rcolor
.blue
= color
.blue
>> 8;
509 RGetClosestXColor(rc
, &rcolor
, &color
);
515 /* for images with a transparent color */
516 if (image
->data
[3]) {
517 RCombineImageWithColor(image
, &rcolor
);
520 switch (toupper(type
[0])) {
522 texture
->width
= iwidth
;
523 texture
->height
= iheight
;
524 if (!pixmap
&& !RConvertImage(rc
, image
, &pixmap
)) {
525 wwarning("could not convert texture:%s", RMessageForError(RErrorCode
));
526 RReleaseImage(image
);
530 RReleaseImage(image
);
532 texture
->pixmap
= pixmap
;
533 texture
->color
= color
;
540 XCreatePixmap(dpy
, root
, scrWidth
, scrHeight
, DefaultDepth(dpy
, scr
));
541 XSetForeground(dpy
, DefaultGC(dpy
, scr
), color
.pixel
);
542 XFillRectangle(dpy
, tpixmap
, DefaultGC(dpy
, scr
), 0, 0, scrWidth
, scrHeight
);
544 texture
->pixmap
= tpixmap
;
545 texture
->color
= color
;
546 texture
->width
= scrWidth
;
547 texture
->height
= scrHeight
;
550 if (xineInfo
.count
&& ! xineStretch
) {
552 for (i
= 0; i
< xineInfo
.count
; ++i
) {
553 applyImage(rc
, texture
, image
, type
[0],
554 xineInfo
.screens
[i
].pos
.x
, xineInfo
.screens
[i
].pos
.y
,
555 xineInfo
.screens
[i
].size
.width
,
556 xineInfo
.screens
[i
].size
.height
);
559 applyImage(rc
, texture
, image
, type
[0], 0, 0, scrWidth
, scrHeight
);
561 #else /* !XINERAMA */
562 applyImage(rc
, texture
, image
, type
[0], 0, 0, scrWidth
, scrHeight
);
563 #endif /* !XINERAMA */
564 RReleaseImage(image
);
568 } else if (strcasecmp(type
, "thgradient") == 0
569 || strcasecmp(type
, "tvgradient") == 0 || strcasecmp(type
, "tdgradient") == 0) {
571 RColor color1
, color2
;
581 GETSTRORGOTO(val
, file
, 1, error
);
583 GETSTRORGOTO(val
, tmp
, 2, error
);
587 GETSTRORGOTO(val
, tmp
, 3, error
);
589 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
590 wwarning("could not parse color %s in texture %s", tmp
, text
);
594 color1
.red
= color
.red
>> 8;
595 color1
.green
= color
.green
>> 8;
596 color1
.blue
= color
.blue
>> 8;
598 GETSTRORGOTO(val
, tmp
, 4, error
);
600 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
601 wwarning("could not parse color %s in texture %s", tmp
, text
);
605 color2
.red
= color
.red
>> 8;
606 color2
.green
= color
.green
>> 8;
607 color2
.blue
= color
.blue
>> 8;
609 image
= loadImage(rc
, file
);
617 gtype
= RHorizontalGradient
;
619 theight
= image
->height
> scrHeight
? scrHeight
: image
->height
;
623 gtype
= RVerticalGradient
;
624 twidth
= image
->width
> scrWidth
? scrWidth
: image
->width
;
628 gtype
= RDiagonalGradient
;
633 gradient
= RRenderGradient(twidth
, theight
, &color1
, &color2
, gtype
);
636 wwarning("could not render texture:%s", RMessageForError(RErrorCode
));
637 RReleaseImage(gradient
);
638 RReleaseImage(image
);
642 tiled
= RMakeTiledImage(image
, twidth
, theight
);
644 wwarning("could not render texture:%s", RMessageForError(RErrorCode
));
645 RReleaseImage(gradient
);
646 RReleaseImage(image
);
649 RReleaseImage(image
);
651 RCombineImagesWithOpaqueness(tiled
, gradient
, opaq
);
652 RReleaseImage(gradient
);
654 if (!RConvertImage(rc
, tiled
, &pixmap
)) {
655 wwarning("could not convert texture:%s", RMessageForError(RErrorCode
));
656 RReleaseImage(tiled
);
659 texture
->width
= tiled
->width
;
660 texture
->height
= tiled
->height
;
662 RReleaseImage(tiled
);
664 texture
->pixmap
= pixmap
;
665 } else if (strcasecmp(type
, "function") == 0) {
667 void (*initFunc
) (Display
*, Colormap
);
668 RImage
*(*mainFunc
) (int, char **, int, int, int);
672 char *lib
, *func
, **argv
= 0;
677 goto function_cleanup
;
679 /* get the library name */
680 GETSTRORGOTO(val
, lib
, 1, function_cleanup
);
682 /* get the function name */
683 GETSTRORGOTO(val
, func
, 2, function_cleanup
);
686 argv
= (char **)wmalloc(argc
* sizeof(char *));
688 /* get the parameters */
690 for (i
= 0; i
< argc
- 1; i
++) {
691 GETSTRORGOTO(val
, tmp
, 3 + i
, function_cleanup
);
692 argv
[i
+ 1] = wstrdup(tmp
);
695 handle
= dlopen(lib
, RTLD_LAZY
);
697 wwarning("could not find library %s", lib
);
698 goto function_cleanup
;
701 initFunc
= dlsym(handle
, "initWindowMaker");
703 wwarning("could not initialize library %s", lib
);
704 goto function_cleanup
;
706 initFunc(dpy
, DefaultColormap(dpy
, scr
));
708 mainFunc
= dlsym(handle
, func
);
710 wwarning("could not find function %s::%s", lib
, func
);
711 goto function_cleanup
;
713 image
= mainFunc(argc
, argv
, scrWidth
, scrHeight
, 0);
715 if (!RConvertImage(rc
, image
, &pixmap
)) {
716 wwarning("could not convert texture:%s", RMessageForError(RErrorCode
));
717 goto function_cleanup
;
719 texture
->width
= scrWidth
;
720 texture
->height
= scrHeight
;
721 texture
->pixmap
= pixmap
;
727 for (i
= 0; i
< argc
; i
++) {
735 RReleaseImage(image
);
741 wwarning("function textures not supported");
745 wwarning("invalid texture type %s", text
);
749 texture
->spec
= wstrdup(text
);
757 WMReleasePropList(texarray
);
762 void freeTexture(BackgroundTexture
* texture
)
764 if (texture
->solid
) {
765 unsigned long pixel
[1];
767 pixel
[0] = texture
->color
.pixel
;
768 /* dont free black/white pixels */
769 if (pixel
[0] != BlackPixelOfScreen(DefaultScreenOfDisplay(dpy
))
770 && pixel
[0] != WhitePixelOfScreen(DefaultScreenOfDisplay(dpy
)))
771 XFreeColors(dpy
, DefaultColormap(dpy
, scr
), pixel
, 1, 0);
773 if (texture
->pixmap
) {
774 XFreePixmap(dpy
, texture
->pixmap
);
776 wfree(texture
->spec
);
780 void setupTexture(RContext
* rc
, BackgroundTexture
** textures
, int *maxTextures
, int workspace
, char *texture
)
782 BackgroundTexture
*newTexture
= NULL
;
785 /* unset the texture */
787 if (textures
[workspace
] != NULL
) {
788 textures
[workspace
]->refcount
--;
790 if (textures
[workspace
]->refcount
== 0)
791 freeTexture(textures
[workspace
]);
793 textures
[workspace
] = NULL
;
797 if (textures
[workspace
]
798 && strcasecmp(textures
[workspace
]->spec
, texture
) == 0) {
799 /* texture did not change */
803 /* check if the same texture is already created */
804 for (i
= 0; i
< *maxTextures
; i
++) {
805 if (textures
[i
] && strcasecmp(textures
[i
]->spec
, texture
) == 0) {
806 newTexture
= textures
[i
];
812 /* create the texture */
813 newTexture
= parseTexture(rc
, texture
);
818 if (textures
[workspace
] != NULL
) {
820 textures
[workspace
]->refcount
--;
822 if (textures
[workspace
]->refcount
== 0)
823 freeTexture(textures
[workspace
]);
826 newTexture
->refcount
++;
827 textures
[workspace
] = newTexture
;
829 if (*maxTextures
< workspace
)
830 *maxTextures
= workspace
;
833 Pixmap
duplicatePixmap(Pixmap pixmap
, int width
, int height
)
838 /* must open a new display or the RetainPermanent will
839 * leave stuff allocated in RContext unallocated after exit */
840 tmpDpy
= XOpenDisplay(display
);
842 wwarning("could not open display to update background image information");
848 copyP
= XCreatePixmap(tmpDpy
, root
, width
, height
, DefaultDepth(tmpDpy
, scr
));
849 XCopyArea(tmpDpy
, pixmap
, copyP
, DefaultGC(tmpDpy
, scr
), 0, 0, width
, height
, 0, 0);
850 XSync(tmpDpy
, False
);
852 XSetCloseDownMode(tmpDpy
, RetainPermanent
);
853 XCloseDisplay(tmpDpy
);
859 static int dummyErrorHandler(Display
* dpy
, XErrorEvent
* err
)
864 void setPixmapProperty(Pixmap pixmap
)
866 static Atom prop
= 0;
869 unsigned long length
, after
;
874 prop
= XInternAtom(dpy
, "_XROOTPMAP_ID", False
);
879 /* Clear out the old pixmap */
880 XGetWindowProperty(dpy
, root
, prop
, 0L, 1L, False
, AnyPropertyType
,
881 &type
, &format
, &length
, &after
, &data
);
883 if ((type
== XA_PIXMAP
) && (format
== 32) && (length
== 1)) {
884 XSetErrorHandler(dummyErrorHandler
);
885 XKillClient(dpy
, *((Pixmap
*) data
));
887 XSetErrorHandler(NULL
);
888 mode
= PropModeReplace
;
890 mode
= PropModeAppend
;
893 XChangeProperty(dpy
, root
, prop
, XA_PIXMAP
, 32, mode
, (unsigned char *)&pixmap
, 1);
895 XDeleteProperty(dpy
, root
, prop
);
901 void changeTexture(BackgroundTexture
* texture
)
907 if (texture
->solid
) {
908 XSetWindowBackground(dpy
, root
, texture
->color
.pixel
);
910 XSetWindowBackgroundPixmap(dpy
, root
, texture
->pixmap
);
912 XClearWindow(dpy
, root
);
919 pixmap
= duplicatePixmap(texture
->pixmap
, texture
->width
, texture
->height
);
921 setPixmapProperty(pixmap
);
925 int readmsg(int fd
, char *buffer
, int size
)
931 count
= read(fd
, buffer
, size
);
944 * sizeSntexture_spec - sets the texture for workspace n
945 * sizeCn - change background texture to the one for workspace n
946 * sizePpath - set the pixmap search path
949 * size = 4 bytes for length of the message data
951 void helperLoop(RContext
* rc
)
953 BackgroundTexture
*textures
[WORKSPACE_COUNT
];
955 char buffer
[2048], buf
[8];
959 memset(textures
, 0, WORKSPACE_COUNT
* sizeof(BackgroundTexture
*));
964 /* get length of message */
965 if (readmsg(0, buffer
, 4) < 0) {
966 wsyserror("error reading message from Window Maker");
974 memcpy(buf
, buffer
, 4);
979 if (readmsg(0, buffer
, size
) < 0) {
980 wsyserror("error reading message from Window Maker");
989 printf("RECEIVED %s\n", buffer
);
991 if (buffer
[0] != 'P' && buffer
[0] != 'K') {
992 memcpy(buf
, &buffer
[1], 4);
994 workspace
= atoi(buf
);
995 if (workspace
< 0 || workspace
>= WORKSPACE_COUNT
) {
996 wwarning("received message with invalid workspace number %i\n", workspace
);
1001 switch (buffer
[0]) {
1004 printf("set texture %s\n", &buffer
[5]);
1006 setupTexture(rc
, textures
, &maxTextures
, workspace
, &buffer
[5]);
1011 printf("change texture %i\n", workspace
);
1013 if (!textures
[workspace
]) {
1014 changeTexture(textures
[0]);
1016 changeTexture(textures
[workspace
]);
1022 printf("change pixmappath %s\n", &buffer
[1]);
1026 PixmapPath
= wstrdup(&buffer
[1]);
1031 printf("unset workspace %i\n", workspace
);
1033 setupTexture(rc
, textures
, &maxTextures
, workspace
, NULL
);
1038 printf("exit command\n");
1043 wwarning("unknown message received");
1049 void updateDomain(char *domain
, char *key
, char *texture
)
1051 char *program
= "wdwrite";
1053 /* here is a mem leak */
1054 system(wstrconcat("wdwrite ",
1055 wstrconcat(domain
, smooth
? " SmoothWorkspaceBack YES" : " SmoothWorkspaceBack NO")));
1057 execlp(program
, program
, domain
, key
, texture
, NULL
);
1058 wwarning("warning could not run \"%s\"", program
);
1061 static WMPropList
*getValueForKey(char *domain
, char *keyName
)
1064 WMPropList
*key
, *val
, *d
;
1066 key
= WMCreatePLString(keyName
);
1068 /* try to find PixmapPath in user defaults */
1069 path
= wdefaultspathfordomain(domain
);
1070 d
= WMReadPropListFromFile(path
);
1072 wwarning("could not open domain file %s", path
);
1076 if (d
&& !WMIsPLDictionary(d
)) {
1077 WMReleasePropList(d
);
1081 val
= WMGetFromPLDictionary(d
, key
);
1085 /* try to find PixmapPath in global defaults */
1087 path
= wglobaldefaultspathfordomain(domain
);
1089 wwarning("could not locate file for domain %s", domain
);
1092 d
= WMReadPropListFromFile(path
);
1096 if (d
&& !WMIsPLDictionary(d
)) {
1097 WMReleasePropList(d
);
1101 val
= WMGetFromPLDictionary(d
, key
);
1109 WMRetainPropList(val
);
1111 WMReleasePropList(key
);
1113 WMReleasePropList(d
);
1118 char *getPixmapPath(char *domain
)
1124 val
= getValueForKey(domain
, "PixmapPath");
1126 if (!val
|| !WMIsPLArray(val
)) {
1128 WMReleasePropList(val
);
1132 count
= WMGetPropListItemCount(val
);
1134 for (i
= 0; i
< count
; i
++) {
1137 v
= WMGetFromPLArray(val
, i
);
1138 if (!v
|| !WMIsPLString(v
)) {
1141 len
+= strlen(WMGetFromPLString(v
)) + 1;
1144 ptr
= data
= wmalloc(len
+ 1);
1147 for (i
= 0; i
< count
; i
++) {
1150 v
= WMGetFromPLArray(val
, i
);
1151 if (!v
|| !WMIsPLString(v
)) {
1154 strcpy(ptr
, WMGetFromPLString(v
));
1156 ptr
+= strlen(WMGetFromPLString(v
));
1164 WMReleasePropList(val
);
1169 char *getFullPixmapPath(char *file
)
1173 if (!PixmapPath
|| !(tmp
= wfindfile(PixmapPath
, file
))) {
1175 char *path
= wmalloc(bsize
);
1177 while (!getcwd(path
, bsize
)) {
1179 path
= wrealloc(path
, bsize
);
1182 tmp
= wstrconcat(path
, "/");
1184 path
= wstrconcat(tmp
, file
);
1190 /* the file is in the PixmapPath */
1193 return wstrdup(file
);
1204 printf("Usage: %s [options] [image]\n", __progname
);
1205 puts("Sets the workspace background to the specified image or a texture and");
1206 puts("optionally update Window Maker configuration");
1208 puts(" -display display to use");
1209 puts(" -d, --dither dither image");
1210 puts(" -m, --match match colors");
1211 puts(" -S, --smooth smooth scaled image");
1213 puts(" -X, --xinerama stretch image across Xinerama heads");
1215 puts(" -b, --back-color <color> background color");
1216 puts(" -t, --tile tile image");
1217 puts(" -e, --center center image");
1218 puts(" -s, --scale scale image (default)");
1219 puts(" -a, --maxscale scale image and keep aspect ratio");
1220 puts(" -u, --update-wmaker update WindowMaker domain database");
1221 puts(" -D, --update-domain <domain> update <domain> database");
1222 puts(" -c, --colors <cpc> colors per channel to use");
1223 puts(" -p, --parse <texture> proplist style texture specification");
1224 puts(" -w, --workspace <workspace> update background for the specified workspace");
1225 puts(" -v, --version show version of wmsetbg and exit");
1226 puts(" -h, --help show this help and exit");
1229 void changeTextureForWorkspace(char *domain
, char *texture
, int workspace
)
1231 WMPropList
*array
, *val
;
1235 val
= WMCreatePropListFromDescription(texture
);
1237 wwarning("could not parse texture %s", texture
);
1241 array
= getValueForKey("WindowMaker", "WorkspaceSpecificBack");
1244 array
= WMCreatePLArray(NULL
, NULL
);
1247 j
= WMGetPropListItemCount(array
);
1248 if (workspace
>= j
) {
1251 empty
= WMCreatePLArray(NULL
, NULL
);
1253 while (j
++ < workspace
- 1) {
1254 WMAddToPLArray(array
, empty
);
1256 WMAddToPLArray(array
, val
);
1258 WMDeleteFromPLArray(array
, workspace
);
1259 WMInsertInPLArray(array
, workspace
, val
);
1262 value
= WMGetPropListDescription(array
, False
);
1263 updateDomain(domain
, "WorkspaceSpecificBack", value
);
1266 int main(int argc
, char **argv
)
1271 RContextAttributes rattr
;
1272 char *style
= "spixmap";
1273 char *back_color
= "gray20";
1274 char *image_name
= NULL
;
1275 char *domain
= "WindowMaker";
1276 int update
= 0, cpc
= 4, render_mode
= RDitheredRendering
, obey_user
= 0;
1277 char *texture
= NULL
;
1280 signal(SIGINT
, SIG_DFL
);
1281 signal(SIGTERM
, SIG_DFL
);
1282 signal(SIGQUIT
, SIG_DFL
);
1283 signal(SIGSEGV
, SIG_DFL
);
1284 signal(SIGBUS
, SIG_DFL
);
1285 signal(SIGFPE
, SIG_DFL
);
1286 signal(SIGABRT
, SIG_DFL
);
1287 signal(SIGHUP
, SIG_DFL
);
1288 signal(SIGPIPE
, SIG_DFL
);
1289 signal(SIGCHLD
, SIG_DFL
);
1291 WMInitializeApplication("wmsetbg", &argc
, argv
);
1293 for (i
= 1; i
< argc
; i
++) {
1294 if (strcmp(argv
[i
], "-helper") == 0) {
1296 } else if (strcmp(argv
[i
], "-display") == 0) {
1299 wfatal("too few arguments for %s\n", argv
[i
- 1]);
1303 } else if (strcmp(argv
[i
], "-s") == 0 || strcmp(argv
[i
], "--scale") == 0) {
1305 } else if (strcmp(argv
[i
], "-t") == 0 || strcmp(argv
[i
], "--tile") == 0) {
1307 } else if (strcmp(argv
[i
], "-e") == 0 || strcmp(argv
[i
], "--center") == 0) {
1309 } else if (strcmp(argv
[i
], "-a") == 0 || strcmp(argv
[i
], "--maxscale") == 0) {
1311 } else if (strcmp(argv
[i
], "-d") == 0 || strcmp(argv
[i
], "--dither") == 0) {
1312 render_mode
= RDitheredRendering
;
1314 } else if (strcmp(argv
[i
], "-m") == 0 || strcmp(argv
[i
], "--match") == 0) {
1315 render_mode
= RBestMatchRendering
;
1317 } else if (strcmp(argv
[i
], "-S") == 0 || strcmp(argv
[i
], "--smooth") == 0) {
1320 } else if (strcmp(argv
[i
], "-X") == 0 || strcmp(argv
[i
], "--xinerama") == 0) {
1323 } else if (strcmp(argv
[i
], "-u") == 0 || strcmp(argv
[i
], "--update-wmaker") == 0) {
1325 } else if (strcmp(argv
[i
], "-D") == 0 || strcmp(argv
[i
], "--update-domain") == 0) {
1329 wfatal("too few arguments for %s\n", argv
[i
- 1]);
1332 domain
= wstrdup(argv
[i
]);
1333 } else if (strcmp(argv
[i
], "-c") == 0 || strcmp(argv
[i
], "--colors") == 0) {
1336 wfatal("too few arguments for %s\n", argv
[i
- 1]);
1339 if (sscanf(argv
[i
], "%i", &cpc
) != 1) {
1340 wfatal("bad value for colors per channel: \"%s\"\n", argv
[i
]);
1343 } else if (strcmp(argv
[i
], "-b") == 0 || strcmp(argv
[i
], "--back-color") == 0) {
1346 wfatal("too few arguments for %s\n", argv
[i
- 1]);
1349 back_color
= argv
[i
];
1350 } else if (strcmp(argv
[i
], "-p") == 0 || strcmp(argv
[i
], "--parse") == 0) {
1353 wfatal("too few arguments for %s\n", argv
[i
- 1]);
1357 } else if (strcmp(argv
[i
], "-w") == 0 || strcmp(argv
[i
], "--workspace") == 0) {
1360 wfatal("too few arguments for %s\n", argv
[i
- 1]);
1363 if (sscanf(argv
[i
], "%i", &workspace
) != 1) {
1364 wfatal("bad value for workspace number: \"%s\"", argv
[i
]);
1367 } else if (strcmp(argv
[i
], "-v") == 0 || strcmp(argv
[i
], "--version") == 0) {
1368 printf(PROG_VERSION
);
1370 } else if (strcmp(argv
[i
], "-h") == 0 || strcmp(argv
[i
], "--help") == 0) {
1373 } else if (argv
[i
][0] != '-') {
1374 image_name
= argv
[i
];
1376 printf("%s: invalid argument '%s'\n", __progname
, argv
[i
]);
1377 printf("Try '%s --help' for more information\n", __progname
);
1381 if (!image_name
&& !texture
&& !helperMode
) {
1382 printf("%s: you must specify a image file name or a texture\n", __progname
);
1383 printf("Try '%s --help' for more information\n", __progname
);
1387 PixmapPath
= getPixmapPath(domain
);
1390 /* carlos, don't remove this */
1391 #if 0 /* some problem with Alpha... TODO: check if its right */
1392 val
= WMGetFromPLDictionary(domain
, WMCreatePLString("SmoothWorkspaceBack"));
1394 val
= getValueForKey(domain
, "SmoothWorkspaceBack");
1397 if (val
&& WMIsPLString(val
) && strcasecmp(WMGetFromPLString(val
), "YES") == 0)
1401 dpy
= XOpenDisplay(display
);
1403 wfatal("could not open display");
1407 XSynchronize(dpy
, 1);
1410 root
= DefaultRootWindow(dpy
);
1412 scr
= DefaultScreen(dpy
);
1414 scrWidth
= WidthOfScreen(DefaultScreenOfDisplay(dpy
));
1415 scrHeight
= HeightOfScreen(DefaultScreenOfDisplay(dpy
));
1420 if (!obey_user
&& DefaultDepth(dpy
, scr
) <= 8)
1421 render_mode
= RDitheredRendering
;
1423 rattr
.flags
= RC_RenderMode
| RC_ColorsPerChannel
| RC_StandardColormap
| RC_DefaultVisual
;
1424 rattr
.render_mode
= render_mode
;
1425 rattr
.colors_per_channel
= cpc
;
1426 rattr
.standard_colormap_mode
= RCreateStdColormap
;
1428 rc
= RCreateContext(dpy
, scr
, &rattr
);
1431 rattr
.standard_colormap_mode
= RIgnoreStdColormap
;
1432 rc
= RCreateContext(dpy
, scr
, &rattr
);
1436 wfatal("could not initialize wrlib: %s", RMessageForError(RErrorCode
));
1441 /* lower priority, so that it wont use all the CPU */
1446 BackgroundTexture
*tex
;
1450 char *image_path
= getFullPixmapPath(image_name
);
1452 sprintf(buffer
, "(%s, \"%s\", %s)", style
, image_path
, back_color
);
1454 texture
= (char *)buffer
;
1457 if (update
&& workspace
< 0) {
1458 updateDomain(domain
, "WorkspaceBack", texture
);
1461 tex
= parseTexture(rc
, texture
);
1468 /* always update domain */
1469 changeTextureForWorkspace(domain
, texture
, workspace
);