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, 1999 Alfredo K. Kojima
7 * Copyright (c) 1998 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,
29 #include <X11/Xutil.h>
30 #include <X11/Xatom.h>
35 #include <sys/types.h>
37 #include "../src/wconfig.h"
46 #define WORKSPACE_COUNT (MAX_WORKSPACES+1)
56 Pixmap CurrentPixmap
= None
;
57 char *PixmapPath
= NULL
;
61 typedef struct BackgroundTexture
{
69 Pixmap pixmap
; /* for all textures, including solid */
70 int width
; /* size of the pixmap */
78 loadImage(RContext
*rc
, char *file
)
83 path
= wfindfile(PixmapPath
, file
);
85 wwarning("%s:could not find image file used in texture", file
);
89 image
= RLoadImage(rc
, path
, 0);
91 wwarning("%s:could not load image file used in texture:%s", path
,
92 RMessageForError(RErrorCode
));
102 parseTexture(RContext
*rc
, char *text
)
104 BackgroundTexture
*texture
= NULL
;
111 #define GETSTR(val, str, i) \
112 val = PLGetArrayElement(texarray, i);\
113 if (!PLIsString(val)) {\
114 wwarning("could not parse texture %s", text);\
117 str = PLGetString(val)
120 texarray
= PLGetProplistWithDescription(text
);
121 if (!texarray
|| !PLIsArray(texarray
)
122 || (count
= PLGetNumberOfElements(texarray
)) < 2) {
124 wwarning("could not parse texture %s", text
);
130 texture
= wmalloc(sizeof(BackgroundTexture
));
131 memset(texture
, 0, sizeof(BackgroundTexture
));
133 GETSTR(val
, type
, 0);
135 if (strcasecmp(type
, "solid")==0) {
143 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
144 wwarning("could not parse color %s in texture %s", tmp
, text
);
147 XAllocColor(dpy
, DefaultColormap(dpy
, scr
), &color
);
149 pixmap
= XCreatePixmap(dpy
, root
, 32, 32, DefaultDepth(dpy
, scr
));
150 XSetForeground(dpy
, DefaultGC(dpy
, scr
), color
.pixel
);
151 XFillRectangle(dpy
, pixmap
, DefaultGC(dpy
, scr
), 0, 0, 32, 32);
153 texture
->pixmap
= pixmap
;
154 texture
->color
= color
;
156 texture
->height
= 32;
157 } else if (strcasecmp(type
, "vgradient")==0
158 || strcasecmp(type
, "dgradient")==0
159 || strcasecmp(type
, "hgradient")==0) {
161 RColor color1
, color2
;
168 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
169 wwarning("could not parse color %s in texture %s", tmp
, text
);
173 color1
.red
= color
.red
>> 8;
174 color1
.green
= color
.green
>> 8;
175 color1
.blue
= color
.blue
>> 8;
179 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
180 wwarning("could not parse color %s in texture %s", tmp
, text
);
184 color2
.red
= color
.red
>> 8;
185 color2
.green
= color
.green
>> 8;
186 color2
.blue
= color
.blue
>> 8;
191 gtype
= RHorizontalGradient
;
195 gtype
= RVerticalGradient
;
198 gtype
= RDiagonalGradient
;
202 image
= RRenderGradient(scrWidth
, scrHeight
, &color1
, &color2
, gtype
);
205 wwarning("could not render gradient texture:%s",
206 RMessageForError(RErrorCode
));
210 if (!RConvertImage(rc
, image
, &pixmap
)) {
211 wwarning("could not convert texture:%s",
212 RMessageForError(RErrorCode
));
213 RDestroyImage(image
);
217 texture
->width
= image
->width
;
218 texture
->height
= image
->height
;
219 RDestroyImage(image
);
221 texture
->pixmap
= pixmap
;
222 } else if (strcasecmp(type
, "mvgradient")==0
223 || strcasecmp(type
, "mdgradient")==0
224 || strcasecmp(type
, "mhgradient")==0) {
233 colors
= malloc(sizeof(RColor
*)*(count
-1));
235 wwarning("out of memory while parsing texture");
238 memset(colors
, 0, sizeof(RColor
*)*(count
-1));
240 for (i
= 2; i
< count
; i
++) {
241 val
= PLGetArrayElement(texarray
, i
);
242 if (!PLIsString(val
)) {
243 wwarning("could not parse texture %s", text
);
245 for (j
= 0; colors
[j
]!=NULL
; j
++)
250 tmp
= PLGetString(val
);
252 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
253 wwarning("could not parse color %s in texture %s",
256 for (j
= 0; colors
[j
]!=NULL
; j
++)
261 if (!(colors
[i
-2] = malloc(sizeof(RColor
)))) {
262 wwarning("out of memory while parsing texture");
264 for (j
= 0; colors
[j
]!=NULL
; j
++)
270 colors
[i
-2]->red
= color
.red
>> 8;
271 colors
[i
-2]->green
= color
.green
>> 8;
272 colors
[i
-2]->blue
= color
.blue
>> 8;
278 gtype
= RHorizontalGradient
;
282 gtype
= RVerticalGradient
;
285 gtype
= RDiagonalGradient
;
289 image
= RRenderMultiGradient(scrWidth
, scrHeight
, colors
, gtype
);
291 for (j
= 0; colors
[j
]!=NULL
; j
++)
296 wwarning("could not render gradient texture:%s",
297 RMessageForError(RErrorCode
));
301 if (!RConvertImage(rc
, image
, &pixmap
)) {
302 wwarning("could not convert texture:%s",
303 RMessageForError(RErrorCode
));
304 RDestroyImage(image
);
308 texture
->width
= image
->width
;
309 texture
->height
= image
->height
;
310 RDestroyImage(image
);
312 texture
->pixmap
= pixmap
;
313 } else if (strcasecmp(type
, "cpixmap")==0
314 || strcasecmp(type
, "spixmap")==0
315 || strcasecmp(type
, "tpixmap")==0) {
322 image
= loadImage(rc
, tmp
);
329 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
330 wwarning("could not parse color %s in texture %s\n", tmp
, text
);
331 RDestroyImage(image
);
337 rcolor
.red
= color
.red
>> 8;
338 rcolor
.green
= color
.green
>> 8;
339 rcolor
.blue
= color
.blue
>> 8;
340 RGetClosestXColor(rc
, &rcolor
, &color
);
345 texture
->width
= image
->width
;
346 texture
->height
= image
->height
;
347 if (!RConvertImage(rc
, image
, &pixmap
)) {
348 wwarning("could not convert texture:%s",
349 RMessageForError(RErrorCode
));
350 RDestroyImage(image
);
360 if (image
->width
*scrHeight
> image
->height
*scrWidth
) {
362 h
= (scrWidth
*image
->height
)/image
->width
;
365 w
= (scrHeight
*image
->width
)/image
->height
;
368 simage
= RScaleImage(image
, w
, h
);
370 wwarning("could not scale image:%s",
371 RMessageForError(RErrorCode
));
372 RDestroyImage(image
);
375 RDestroyImage(image
);
384 if (!RConvertImage(rc
, image
, &pixmap
)) {
385 wwarning("could not convert texture:%s",
386 RMessageForError(RErrorCode
));
387 RDestroyImage(image
);
391 if (image
->width
!= scrWidth
|| image
->height
!= scrHeight
) {
392 int x
, y
, sx
, sy
, w
, h
;
394 cpixmap
= XCreatePixmap(dpy
, root
, scrWidth
, scrHeight
,
395 DefaultDepth(dpy
, scr
));
397 XSetForeground(dpy
, DefaultGC(dpy
, scr
), color
.pixel
);
398 XFillRectangle(dpy
, cpixmap
, DefaultGC(dpy
, scr
),
399 0, 0, scrWidth
, scrHeight
);
401 if (image
->height
< scrHeight
) {
403 y
= (scrHeight
- h
)/2;
406 sy
= (image
->height
- scrHeight
)/2;
410 if (image
->width
< scrWidth
) {
412 x
= (scrWidth
- w
)/2;
415 sx
= (image
->width
- scrWidth
)/2;
420 XCopyArea(dpy
, pixmap
, cpixmap
, DefaultGC(dpy
, scr
),
422 XFreePixmap(dpy
, pixmap
);
425 RDestroyImage(image
);
427 texture
->width
= scrWidth
;
428 texture
->height
= scrHeight
;
433 texture
->pixmap
= pixmap
;
434 texture
->color
= color
;
435 } else if (strcasecmp(type
, "thgradient")==0
436 || strcasecmp(type
, "tvgradient")==0
437 || strcasecmp(type
, "tdgradient")==0) {
439 RColor color1
, color2
;
449 GETSTR(val
, file
, 1);
457 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
458 wwarning("could not parse color %s in texture %s", tmp
, text
);
462 color1
.red
= color
.red
>> 8;
463 color1
.green
= color
.green
>> 8;
464 color1
.blue
= color
.blue
>> 8;
468 if (!XParseColor(dpy
, DefaultColormap(dpy
, scr
), tmp
, &color
)) {
469 wwarning("could not parse color %s in texture %s", tmp
, text
);
473 color2
.red
= color
.red
>> 8;
474 color2
.green
= color
.green
>> 8;
475 color2
.blue
= color
.blue
>> 8;
477 image
= loadImage(rc
, file
);
479 RDestroyImage(gradient
);
486 gtype
= RHorizontalGradient
;
488 theight
= image
->height
> scrHeight
? scrHeight
: image
->height
;
492 gtype
= RVerticalGradient
;
493 twidth
= image
->width
> scrWidth
? scrWidth
: image
->width
;
497 gtype
= RDiagonalGradient
;
502 gradient
= RRenderGradient(twidth
, theight
, &color1
, &color2
, gtype
);
505 wwarning("could not render texture:%s",
506 RMessageForError(RErrorCode
));
507 RDestroyImage(gradient
);
511 tiled
= RMakeTiledImage(image
, twidth
, theight
);
513 wwarning("could not render texture:%s",
514 RMessageForError(RErrorCode
));
515 RDestroyImage(gradient
);
516 RDestroyImage(image
);
519 RDestroyImage(image
);
521 RCombineImagesWithOpaqueness(tiled
, gradient
, opaq
);
522 RDestroyImage(gradient
);
524 if (!RConvertImage(rc
, tiled
, &pixmap
)) {
525 wwarning("could not convert texture:%s",
526 RMessageForError(RErrorCode
));
527 RDestroyImage(image
);
530 texture
->width
= tiled
->width
;
531 texture
->height
= tiled
->height
;
533 RDestroyImage(tiled
);
535 texture
->pixmap
= pixmap
;
537 wwarning("invalid texture type %s", text
);
541 texture
->spec
= wstrdup(text
);
556 freeTexture(BackgroundTexture
*texture
)
558 if (texture
->solid
) {
561 pixel
[0] = texture
->color
.pixel
;
562 /* dont free black/white pixels */
563 if (pixel
[0]!=BlackPixelOfScreen(DefaultScreenOfDisplay(dpy
))
564 && pixel
[0]!=WhitePixelOfScreen(DefaultScreenOfDisplay(dpy
)))
565 XFreeColors(dpy
, DefaultColormap(dpy
, scr
), pixel
, 1, 0);
573 setupTexture(RContext
*rc
, BackgroundTexture
**textures
, int *maxTextures
,
574 int workspace
, char *texture
)
576 BackgroundTexture
*newTexture
= NULL
;
579 /* unset the texture */
581 if (textures
[workspace
]!=NULL
) {
582 textures
[workspace
]->refcount
--;
584 if (textures
[workspace
]->refcount
== 0)
585 freeTexture(textures
[workspace
]);
587 textures
[workspace
] = NULL
;
591 if (textures
[workspace
]
592 && strcasecmp(textures
[workspace
]->spec
, texture
)==0) {
593 /* texture did not change */
597 /* check if the same texture is already created */
598 for (i
= 0; i
< *maxTextures
; i
++) {
599 if (textures
[i
] && strcasecmp(textures
[i
]->spec
, texture
)==0) {
600 newTexture
= textures
[i
];
606 /* create the texture */
607 newTexture
= parseTexture(rc
, texture
);
612 if (textures
[workspace
]!=NULL
) {
614 textures
[workspace
]->refcount
--;
616 if (textures
[workspace
]->refcount
== 0)
617 freeTexture(textures
[workspace
]);
620 newTexture
->refcount
++;
621 textures
[workspace
] = newTexture
;
623 if (*maxTextures
< workspace
)
624 *maxTextures
= workspace
;
630 duplicatePixmap(Pixmap pixmap
, int width
, int height
)
635 /* must open a new display or the RetainPermanent will
636 * leave stuff allocated in RContext unallocated after exit */
637 tmpDpy
= XOpenDisplay("");
639 wwarning("could not open display to update background image information");
645 copyP
= XCreatePixmap(tmpDpy
, root
, width
, height
,
646 DefaultDepth(tmpDpy
, scr
));
647 XCopyArea(tmpDpy
, pixmap
, copyP
, DefaultGC(tmpDpy
, scr
),
648 0, 0, width
, height
, 0, 0);
649 XSync(tmpDpy
, False
);
651 XSetCloseDownMode(tmpDpy
, RetainPermanent
);
652 XCloseDisplay(tmpDpy
);
660 setPixmapProperty(Pixmap pixmap
)
662 static Atom prop
= 0;
665 unsigned long length
, after
;
670 prop
= XInternAtom(dpy
, "_XROOTPMAP_ID", False
);
675 /* Clear out the old pixmap */
676 XGetWindowProperty(dpy
, root
, prop
, 0L, 1L, False
, AnyPropertyType
,
677 &type
, &format
, &length
, &after
, &data
);
679 if ((type
== XA_PIXMAP
) && (format
== 32) && (length
== 1)) {
680 XKillClient(dpy
, *((Pixmap
*)data
));
681 mode
= PropModeReplace
;
683 mode
= PropModeAppend
;
686 XChangeProperty(dpy
, root
, prop
, XA_PIXMAP
, 32, mode
,
687 (unsigned char *) &pixmap
, 1);
689 XDeleteProperty(dpy
, root
, prop
);
699 changeTexture(BackgroundTexture
*texture
)
704 if (texture
->solid
) {
705 XSetWindowBackground(dpy
, root
, texture
->color
.pixel
);
707 XSetWindowBackgroundPixmap(dpy
, root
, texture
->pixmap
);
709 XClearWindow(dpy
, root
);
716 pixmap
= duplicatePixmap(texture
->pixmap
, texture
->width
,
719 setPixmapProperty(pixmap
);
725 readmsg(int fd
, unsigned char *buffer
, int size
)
731 count
= read(fd
, buffer
, size
);
745 * sizeSntexture_spec - sets the texture for workspace n
746 * sizeCn - change background texture to the one for workspace n
747 * sizePpath - set the pixmap search path
750 * size = 4 bytes for length of the message data
753 helperLoop(RContext
*rc
)
755 BackgroundTexture
*textures
[WORKSPACE_COUNT
];
757 unsigned char buffer
[2048], buf
[8];
761 memset(textures
, 0, WORKSPACE_COUNT
*sizeof(BackgroundTexture
*));
767 /* get length of message */
768 if (readmsg(0, buffer
, 4) < 0) {
769 wsyserror("error reading message from Window Maker");
777 memcpy(buf
, buffer
, 4);
782 if (readmsg(0, buffer
, size
) < 0) {
783 wsyserror("error reading message from Window Maker");
792 printf("RECEIVED %s\n",buffer
);
794 if (buffer
[0]!='P') {
795 memcpy(buf
, &buffer
[1], 4);
797 workspace
= atoi(buf
);
798 if (workspace
< 0 || workspace
>= WORKSPACE_COUNT
) {
799 wwarning("received message with invalid workspace number %i\n",
808 printf("set texture %s\n", &buffer
[5]);
810 setupTexture(rc
, textures
, &maxTextures
, workspace
, &buffer
[5]);
815 printf("change texture %i\n", workspace
);
817 if (!textures
[workspace
])
818 changeTexture(textures
[0]);
820 changeTexture(textures
[workspace
]);
825 printf("change pixmappath %s\n", &buffer
[1]);
829 PixmapPath
= wstrdup(&buffer
[1]);
834 printf("unset workspace %i\n", workspace
);
836 setupTexture(rc
, textures
, &maxTextures
, workspace
, NULL
);
840 wwarning("unknown message received");
848 updateDomain(char *domain
, int workspace
, char *texture
)
850 char *program
= "wdwrite";
852 execlp(program
, program
, domain
, "WorkspaceBack", texture
, NULL
);
853 wwarning("warning could not run \"%s\"", program
);
859 getDomain(char *domain
)
865 path
= wdefaultspathfordomain(domain
);
867 wwarning("could not locate file for domain %s", domain
);
871 prop
= PLGetProplistWithPath(path
);
873 if (!prop
|| !PLIsDictionary(prop
)) {
874 wwarning("invalid domain file %s", path
);
887 getPixmapPath(proplist_t prop
)
894 key
= PLMakeString("PixmapPath");
895 val
= PLGetDictionaryEntry(prop
, key
);
897 if (!val
|| !PLIsArray(val
)) {
902 count
= PLGetNumberOfElements(val
);
904 for (i
=0; i
<count
; i
++) {
905 d
= PLGetArrayElement(val
, i
);
906 if (!d
|| !PLIsString(d
)) {
909 len
+= strlen(PLGetString(d
))+1;
912 ptr
= data
= wmalloc(len
+1);
915 for (i
=0; i
<count
; i
++) {
916 d
= PLGetArrayElement(val
, i
);
917 if (!d
|| !PLIsString(d
)) {
920 strcpy(ptr
, PLGetString(d
));
922 ptr
+= strlen(PLGetString(d
));
943 print_help(char *ProgName
)
945 printf("usage: %s [-options] image\n", ProgName
);
947 puts(" -d dither image");
948 puts(" -m match colors");
949 puts(" -b <color> background color");
950 puts(" -t tile image");
951 puts(" -e center image");
952 puts(" -s scale image (default)");
953 puts(" -u update WindowMaker domain database");
954 puts(" -D <domain> update <domain> database");
955 puts(" -c <cpc> colors per channel to use");
956 puts(" -p <texture> proplist style texture specification");
957 /* puts(" -w <workspace> update the background for the specified workspace");
964 changeTextureForWorkspace(proplist_t dict
, char *texture
, int workspace
)
973 val
= PLGetProplistWithDescription(texture
);
975 wwarning("could not parse texture %s", texture
);
979 key
= PLMakeString("WorkspaceSpecificBack");
980 array
= PLGetDictionaryEntry(dict
, key
);
982 array
= PLMakeArrayFromElements(NULL
, NULL
);
983 PLInsertDictionaryEntry(dict
, key
, array
);
986 j
= PLGetNumberOfElements(array
);
987 if (workspace
>= j
) {
990 empty
= PLMakeArrayFromElements(NULL
, NULL
);
992 while (j
++ < workspace
) {
993 PLAppendArrayElement(array
, empty
);
995 PLAppendArrayElement(array
, val
);
997 PLRemoveArrayElement(array
, workspace
);
998 PLInsertArrayElement(array
, val
, workspace
);
1006 main(int argc
, char **argv
)
1011 RContextAttributes rattr
;
1012 char *style
= "spixmap";
1013 char *back_color
= "gray20";
1014 char *image_name
= NULL
;
1015 char *domain
= "WindowMaker";
1016 int update
=0, cpc
=4, render_mode
=RM_DITHER
, obey_user
=0;
1017 char *texture
= NULL
;
1019 proplist_t domain_prop
;
1023 signal(SIGINT
, SIG_DFL
);
1024 signal(SIGTERM
, SIG_DFL
);
1025 signal(SIGQUIT
, SIG_DFL
);
1026 signal(SIGSEGV
, SIG_DFL
);
1027 signal(SIGBUS
, SIG_DFL
);
1028 signal(SIGFPE
, SIG_DFL
);
1029 signal(SIGABRT
, SIG_DFL
);
1030 signal(SIGHUP
, SIG_DFL
);
1031 signal(SIGPIPE
, SIG_DFL
);
1032 signal(SIGCHLD
, SIG_DFL
);
1034 WMInitializeApplication("wmsetbg", &argc
, argv
);
1036 for (i
=0; i
<argc
; i
++) {
1037 if (strcmp(argv
[i
], "-helper")==0) {
1039 } else if (strcmp(argv
[i
], "-s")==0) {
1041 } else if (strcmp(argv
[i
], "-t")==0) {
1043 } else if (strcmp(argv
[i
], "-e")==0) {
1045 } else if (strcmp(argv
[i
], "-d")==0) {
1046 render_mode
= RM_DITHER
;
1048 } else if (strcmp(argv
[i
], "-m")==0) {
1049 render_mode
= RM_MATCH
;
1051 } else if (strcmp(argv
[i
], "-u")==0) {
1053 } else if (strcmp(argv
[i
], "-D")==0) {
1057 wfatal("too few arguments for %s\n", argv
[i
-1]);
1060 domain
= wstrdup(argv
[i
]);
1061 } else if (strcmp(argv
[i
], "-c")==0) {
1064 wfatal("too few arguments for %s\n", argv
[i
-1]);
1067 if (sscanf(argv
[i
], "%i", &cpc
)!=1) {
1068 wfatal("bad value for colors per channel: \"%s\"\n", argv
[i
]);
1071 } else if (strcmp(argv
[i
], "-b")==0) {
1074 wfatal("too few arguments for %s\n", argv
[i
-1]);
1077 back_color
= argv
[i
];
1078 } else if (strcmp(argv
[i
], "-p")==0) {
1081 wfatal("too few arguments for %s\n", argv
[i
-1]);
1085 } else if (strcmp(argv
[i
], "-w")==0) {
1088 wfatal("too few arguments for %s\n", argv
[i
-1]);
1091 if (sscanf(argv
[i
], "%i", &workspace
)!=1) {
1092 wfatal("bad value for workspace number: \"%s\"",
1096 } else if (argv
[i
][0] != '-') {
1097 image_name
= argv
[i
];
1099 print_help(argv
[0]);
1104 domain_prop
= getDomain(domain
);
1106 PixmapPath
= getPixmapPath(domain_prop
);
1108 dpy
= XOpenDisplay("");
1110 wfatal("could not open display");
1113 XSynchronize(dpy
, 1);
1115 root
= DefaultRootWindow(dpy
);
1117 scr
= DefaultScreen(dpy
);
1119 scrWidth
= WidthOfScreen(DefaultScreenOfDisplay(dpy
));
1120 scrHeight
= HeightOfScreen(DefaultScreenOfDisplay(dpy
));
1122 if (!obey_user
&& DefaultDepth(dpy
, scr
) <= 8)
1123 render_mode
= RM_DITHER
;
1125 rattr
.flags
= RC_RenderMode
| RC_ColorsPerChannel
| RC_DefaultVisual
;
1126 rattr
.render_mode
= render_mode
;
1127 rattr
.colors_per_channel
= cpc
;
1129 rc
= RCreateContext(dpy
, scr
, &rattr
);
1132 /* lower priority, so that it wont use all the CPU */
1135 PLRelease(domain_prop
);
1139 BackgroundTexture
*tex
;
1143 sprintf(buffer
, "(%s, %s, %s)", style
, image_name
, back_color
);
1144 texture
= (char*)buffer
;
1147 if (update
&& workspace
< 0) {
1148 updateDomain(domain
, workspace
, texture
);
1151 tex
= parseTexture(rc
, texture
);
1158 changeTextureForWorkspace(domain_prop
, texture
, workspace
);