1 // bsetroot.cc for Openbox
2 // Copyright (c) 2002 - 2002 Ben Janens (ben@orodu.net)
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh at debian.org>
4 // Copyright (c) 1997 - 2000, 2002 Brad Hughes <bhughes at trolltech.com>
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
25 # include "../config.h"
26 #endif // HAVE_CONFIG_H
30 #endif // HAVE_STRING_H
34 #endif // HAVE_STDLIB_H
38 #endif // HAVE_STDIO_H
40 #include "../src/i18n.h"
41 #include "../src/BaseDisplay.h"
42 #include "../src/Image.h"
47 I18n i18n("openbox.cat");
49 bsetroot::bsetroot(int argc, char **argv, char *dpy_name)
50 : BaseDisplay(argv[0], dpy_name)
52 grad = fore = back = (char *) 0;
54 Bool mod = False, sol = False, grd = False;
55 int mod_x = 0, mod_y = 0;
57 for (int i = 1; i < argc; i++) {
58 if (! strcmp("-help", argv[i])) {
60 } else if ((! strcmp("-fg", argv[i])) ||
61 (! strcmp("-foreground", argv[i])) ||
62 (! strcmp("-from", argv[i]))) {
63 if ((++i) >= argc) usage(1);
66 } else if ((! strcmp("-bg", argv[i])) ||
67 (! strcmp("-background", argv[i])) ||
68 (! strcmp("-to", argv[i]))) {
69 if ((++i) >= argc) usage(1);
72 } else if (! strcmp("-solid", argv[i])) {
73 if ((++i) >= argc) usage(1);
77 } else if (! strcmp("-mod", argv[i])) {
78 if ((++i) >= argc) usage();
80 mod_x = atoi(argv[i]);
82 if ((++i) >= argc) usage();
84 mod_y = atoi(argv[i]);
86 if (mod_x < 1) mod_x = 1;
87 if (mod_y < 1) mod_y = 1;
90 } else if (! strcmp("-gradient", argv[i])) {
91 if ((++i) >= argc) usage();
95 } else if (! strcmp("-display", argv[i])) {
96 // -display passed through tests ealier... we just skip it now
102 if ((mod + sol + grd) != True) {
104 i18n(bsetrootSet, bsetrootMustSpecify,
105 "%s: error: must specify one of: -solid, -mod, -gradient\n"),
106 getApplicationName());
111 img_ctrl = new BImageControl*[numberOfScreens()];
112 for (unsigned int s = 0, n = numberOfScreens(); s < n; ++s)
113 img_ctrl[s] = new BImageControl(*this, *getScreenInfo(s), true);
115 if (sol && fore) solid();
116 else if (mod && mod_x && mod_y && fore && back) modula(mod_x, mod_y);
117 else if (grd && grad && fore && back) gradient();
122 bsetroot::~bsetroot(void) {
123 XSetCloseDownMode(getXDisplay(), RetainPermanent);
125 XKillClient(getXDisplay(), AllTemporary);
127 std::for_each(img_ctrl, img_ctrl + numberOfScreens(), PointerAssassin());
133 // adapted from wmsetbg
134 void bsetroot::setPixmapProperty(int screen, Pixmap pixmap) {
135 static Atom rootpmap_id = None, esetroot_id = None;
138 unsigned long length, after;
140 const ScreenInfo *screen_info = getScreenInfo(screen);
142 if (rootpmap_id == None) {
143 rootpmap_id = XInternAtom(getXDisplay(), "_XROOTPMAP_ID", False);
144 esetroot_id = XInternAtom(getXDisplay(), "ESETROOT_PMAP_ID", False);
147 XGrabServer(getXDisplay());
149 // Clear out the old pixmap
150 XGetWindowProperty(getXDisplay(), screen_info->getRootWindow(),
151 rootpmap_id, 0L, 1L, False, XA_PIXMAP,
152 &type, &format, &length, &after, &data);
153 if (type == XA_PIXMAP && format == 32) {
154 XKillClient(getXDisplay(), *((Pixmap *) data));
155 XSync(getXDisplay(), False);
160 XChangeProperty(getXDisplay(), screen_info->getRootWindow(),
161 rootpmap_id, XA_PIXMAP, 32, PropModeReplace,
162 (unsigned char *) &pixmap, 1);
163 XChangeProperty(getXDisplay(), screen_info->getRootWindow(),
164 esetroot_id, XA_PIXMAP, 32, PropModeReplace,
165 (unsigned char *) &pixmap, 1);
167 XDeleteProperty(getXDisplay(), screen_info->getRootWindow(),
169 XDeleteProperty(getXDisplay(), screen_info->getRootWindow(),
173 XUngrabServer(getXDisplay());
174 XFlush(getXDisplay());
178 // adapted from wmsetbg
179 Pixmap bsetroot::duplicatePixmap(int screen, Pixmap pixmap,
180 int width, int height) {
181 XSync(getXDisplay(), False);
183 Pixmap copyP = XCreatePixmap(getXDisplay(),
184 getScreenInfo(screen)->getRootWindow(),
186 DefaultDepth(getXDisplay(), screen));
187 XCopyArea(getXDisplay(), pixmap, copyP, DefaultGC(getXDisplay(), screen),
188 0, 0, width, height, 0, 0);
189 XSync(getXDisplay(), False);
195 void bsetroot::solid(void) {
196 for (unsigned int screen = 0, n = numberOfScreens(); screen < n; screen++) {
199 img_ctrl[screen]->parseColor(&c, fore);
200 if (! c.isAllocated()) c.setPixel(BlackPixel(getXDisplay(), screen));
202 const ScreenInfo *screen_info = getScreenInfo(screen);
204 XSetWindowBackground(getXDisplay(), screen_info->getRootWindow(),
206 XClearWindow(getXDisplay(), screen_info->getRootWindow());
208 Pixmap pixmap = XCreatePixmap(getXDisplay(),
209 screen_info->getRootWindow(),
210 8, 8, DefaultDepth(getXDisplay(), screen));
212 XSetForeground(getXDisplay(), DefaultGC(getXDisplay(), screen),
214 XFillRectangle(getXDisplay(), pixmap, DefaultGC(getXDisplay(), screen),
217 setPixmapProperty(screen, duplicatePixmap(screen, pixmap, 8, 8));
219 XFreePixmap(getXDisplay(), pixmap);
224 void bsetroot::modula(int x, int y) {
228 unsigned int screen, i;
229 unsigned int n = numberOfScreens();
231 for (pattern = 0, screen = 0; screen < n; screen++) {
232 for (i = 0; i < 16; i++) {
238 for (i = 0; i < 16; i++) {
240 data[(i * 2)] = (char) 0xff;
241 data[(i * 2) + 1] = (char) 0xff;
243 data[(i * 2)] = pattern & 0xff;
244 data[(i * 2) + 1] = (pattern >> 8) & 0xff;
252 const ScreenInfo *screen_info = getScreenInfo(screen);
255 XCreateBitmapFromData(getXDisplay(),
256 screen_info->getRootWindow(), data,
259 img_ctrl[screen]->parseColor(&f, fore);
260 img_ctrl[screen]->parseColor(&b, back);
262 if (! f.isAllocated()) f.setPixel(WhitePixel(getXDisplay(), screen));
263 if (! b.isAllocated()) b.setPixel(BlackPixel(getXDisplay(), screen));
266 gcv.foreground = f.getPixel();
267 gcv.background = b.getPixel();
269 gc = XCreateGC(getXDisplay(), screen_info->getRootWindow(),
270 GCForeground | GCBackground, &gcv);
272 Pixmap pixmap = XCreatePixmap(getXDisplay(),
273 screen_info->getRootWindow(),
274 16, 16, screen_info->getDepth());
276 XCopyPlane(getXDisplay(), bitmap, pixmap, gc,
277 0, 0, 16, 16, 0, 0, 1l);
278 XSetWindowBackgroundPixmap(getXDisplay(),
279 screen_info->getRootWindow(),
281 XClearWindow(getXDisplay(), screen_info->getRootWindow());
283 setPixmapProperty(screen,
284 duplicatePixmap(screen, pixmap, 16, 16));
286 XFreeGC(getXDisplay(), gc);
287 XFreePixmap(getXDisplay(), bitmap);
289 if (! (screen_info->getVisual()->c_class & 1))
290 XFreePixmap(getXDisplay(), pixmap);
295 void bsetroot::gradient(void) {
296 for (unsigned int screen = 0, n = numberOfScreens(); screen < n; screen++) {
298 img_ctrl[screen]->parseTexture(&texture, grad);
299 img_ctrl[screen]->parseColor(texture.getColor(), fore);
300 img_ctrl[screen]->parseColor(texture.getColorTo(), back);
301 const ScreenInfo *screen_info = getScreenInfo(screen);
303 if (! texture.getColor()->isAllocated())
304 texture.getColor()->setPixel(WhitePixel(getXDisplay(), screen));
305 if (! texture.getColorTo()->isAllocated())
306 texture.getColorTo()->setPixel(BlackPixel(getXDisplay(), screen));
309 img_ctrl[screen]->renderImage(screen_info->size().w(),
310 screen_info->size().h(),
313 XSetWindowBackgroundPixmap(getXDisplay(),
314 screen_info->getRootWindow(),
316 XClearWindow(getXDisplay(), screen_info->getRootWindow());
318 setPixmapProperty(screen,
319 duplicatePixmap(screen, pixmap,
320 screen_info->size().w(),
321 screen_info->size().h()));
323 if (! (screen_info->getVisual()->c_class & 1)) {
324 img_ctrl[screen]->removeImage(pixmap);
330 void bsetroot::usage(int exit_code) {
332 i18n(bsetrootSet, bsetrootUsage,
334 "Copyright (c) 1997-2000, 2002 Bradley T Hughes\n"
335 "Copyright (c) 2001-2002 Sean 'Shaleh' Perry\n\n"
336 " -display <string> display connection\n"
337 " -mod <x> <y> modula pattern\n"
338 " -foreground, -fg <color> modula foreground color\n"
339 " -background, -bg <color> modula background color\n\n"
340 " -gradient <texture> gradient texture\n"
341 " -from <color> gradient start color\n"
342 " -to <color> gradient end color\n\n"
343 " -solid <color> solid color\n\n"
344 " -help print this help text and exit\n"),
345 getApplicationName());
350 int main(int argc, char **argv) {
351 char *display_name = (char *) 0;
353 for (int i = 1; i < argc; i++) {
354 if (! strcmp(argv[i], "-display")) {
355 // check for -display option
358 fprintf(stderr, i18n(mainSet, mainDISPLAYRequiresArg,
359 "error: '-display' requires an argument\n"));
364 display_name = argv[i];
368 bsetroot app(argc, argv, display_name);