1 // -*- mode++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Window.cc for Blackbox - an X11 Window manager
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
31 #endif // HAVE_STDLIB_H
35 #endif // HAVE_STRING_H
39 #endif // HAVE_STDIO_H
42 #include "../src/i18n.hh"
43 #include "../src/GCCache.hh"
44 #include "../src/Texture.hh"
45 #include "../src/Util.hh"
46 #include "bsetroot.hh"
51 bsetroot::bsetroot(int argc, char **argv, char *dpy_name)
52 : BaseDisplay(argv[0], dpy_name)
54 grad = fore = back = (char *) 0;
56 bool mod = False, sol = False, grd = False;
57 int mod_x = 0, mod_y = 0;
59 for (int i = 1; i < argc; i++) {
60 if (! strcmp("-help", argv[i])) {
62 } else if ((! strcmp("-fg", argv[i])) ||
63 (! strcmp("-foreground", argv[i])) ||
64 (! strcmp("-from", argv[i]))) {
65 if ((++i) >= argc) usage(1);
68 } else if ((! strcmp("-bg", argv[i])) ||
69 (! strcmp("-background", argv[i])) ||
70 (! strcmp("-to", argv[i]))) {
71 if ((++i) >= argc) usage(1);
74 } else if (! strcmp("-solid", argv[i])) {
75 if ((++i) >= argc) usage(1);
79 } else if (! strcmp("-mod", argv[i])) {
80 if ((++i) >= argc) usage();
82 mod_x = atoi(argv[i]);
84 if ((++i) >= argc) usage();
86 mod_y = atoi(argv[i]);
88 if (mod_x < 1) mod_x = 1;
89 if (mod_y < 1) mod_y = 1;
92 } else if (! strcmp("-gradient", argv[i])) {
93 if ((++i) >= argc) usage();
97 } else if (! strcmp("-display", argv[i])) {
98 // -display passed through tests ealier... we just skip it now
104 if ((mod + sol + grd) != True) {
106 i18n(bsetrootSet, bsetrootMustSpecify,
107 "%s: error: must specify one of: -solid, -mod, -gradient\n"),
108 getApplicationName());
113 img_ctrl = new BImageControl*[getNumberOfScreens()];
114 for (unsigned int s = 0; s < getNumberOfScreens(); ++s)
115 img_ctrl[s] = new BImageControl(this, getScreenInfo(s), True);
117 if (sol && fore) solid();
118 else if (mod && mod_x && mod_y && fore && back) modula(mod_x, mod_y);
119 else if (grd && grad && fore && back) gradient();
124 bsetroot::~bsetroot(void) {
125 XSetCloseDownMode(getXDisplay(), RetainPermanent);
127 XKillClient(getXDisplay(), AllTemporary);
129 std::for_each(img_ctrl, img_ctrl + getNumberOfScreens(), PointerAssassin());
135 // adapted from wmsetbg
136 void bsetroot::setPixmapProperty(int screen, Pixmap pixmap) {
137 static Atom rootpmap_id = None, esetroot_id = None;
140 unsigned long length, after;
142 const ScreenInfo *screen_info = getScreenInfo(screen);
144 if (rootpmap_id == None) {
145 rootpmap_id = XInternAtom(getXDisplay(), "_XROOTPMAP_ID", False);
146 esetroot_id = XInternAtom(getXDisplay(), "ESETROOT_PMAP_ID", False);
149 XGrabServer(getXDisplay());
151 /* Clear out the old pixmap */
152 XGetWindowProperty(getXDisplay(), screen_info->getRootWindow(),
153 rootpmap_id, 0L, 1L, False, XA_PIXMAP,
154 &type, &format, &length, &after, &data);
155 if (type == XA_PIXMAP && format == 32) {
156 XKillClient(getXDisplay(), *((Pixmap *) data));
157 XSync(getXDisplay(), False);
162 XChangeProperty(getXDisplay(), screen_info->getRootWindow(),
163 rootpmap_id, XA_PIXMAP, 32, PropModeReplace,
164 (unsigned char *) &pixmap, 1);
165 XChangeProperty(getXDisplay(), screen_info->getRootWindow(),
166 esetroot_id, XA_PIXMAP, 32, PropModeReplace,
167 (unsigned char *) &pixmap, 1);
169 XDeleteProperty(getXDisplay(), screen_info->getRootWindow(),
171 XDeleteProperty(getXDisplay(), screen_info->getRootWindow(),
175 XUngrabServer(getXDisplay());
176 XFlush(getXDisplay());
180 // adapted from wmsetbg
181 Pixmap bsetroot::duplicatePixmap(int screen, Pixmap pixmap,
182 int width, int height) {
183 XSync(getXDisplay(), False);
185 Pixmap copyP = XCreatePixmap(getXDisplay(),
186 getScreenInfo(screen)->getRootWindow(),
188 DefaultDepth(getXDisplay(), screen));
189 XCopyArea(getXDisplay(), pixmap, copyP, DefaultGC(getXDisplay(), screen),
190 0, 0, width, height, 0, 0);
191 XSync(getXDisplay(), False);
197 void bsetroot::solid(void) {
198 for (unsigned int screen = 0; screen < getNumberOfScreens(); screen++) {
199 BColor c(fore, this, screen);
200 const ScreenInfo *screen_info = getScreenInfo(screen);
202 XSetWindowBackground(getXDisplay(), screen_info->getRootWindow(),
204 XClearWindow(getXDisplay(), screen_info->getRootWindow());
206 Pixmap pixmap = XCreatePixmap(getXDisplay(),
207 screen_info->getRootWindow(),
208 8, 8, DefaultDepth(getXDisplay(), screen));
210 XFillRectangle(getXDisplay(), pixmap, pen.gc(), 0, 0, 8, 8);
212 setPixmapProperty(screen, duplicatePixmap(screen, pixmap, 8, 8));
214 XFreePixmap(getXDisplay(), pixmap);
219 void bsetroot::modula(int x, int y) {
223 unsigned int screen, i;
225 for (pattern = 0, screen = 0; screen < getNumberOfScreens(); screen++) {
226 for (i = 0; i < 16; i++) {
232 for (i = 0; i < 16; i++) {
234 data[(i * 2)] = static_cast<char>(0xff);
235 data[(i * 2) + 1] = static_cast<char>(0xff);
237 data[(i * 2)] = pattern & 0xff;
238 data[(i * 2) + 1] = (pattern >> 8) & 0xff;
242 BColor f(fore, this, screen), b(back, this, screen);
245 const ScreenInfo *screen_info = getScreenInfo(screen);
248 XCreateBitmapFromData(getXDisplay(),
249 screen_info->getRootWindow(), data,
253 gcv.foreground = f.pixel();
254 gcv.background = b.pixel();
256 gc = XCreateGC(getXDisplay(), screen_info->getRootWindow(),
257 GCForeground | GCBackground, &gcv);
259 Pixmap pixmap = XCreatePixmap(getXDisplay(),
260 screen_info->getRootWindow(),
261 16, 16, screen_info->getDepth());
263 XCopyPlane(getXDisplay(), bitmap, pixmap, gc,
264 0, 0, 16, 16, 0, 0, 1l);
265 XSetWindowBackgroundPixmap(getXDisplay(),
266 screen_info->getRootWindow(),
268 XClearWindow(getXDisplay(), screen_info->getRootWindow());
270 setPixmapProperty(screen,
271 duplicatePixmap(screen, pixmap, 16, 16));
273 XFreeGC(getXDisplay(), gc);
274 XFreePixmap(getXDisplay(), bitmap);
276 if (! (screen_info->getVisual()->c_class & 1))
277 XFreePixmap(getXDisplay(), pixmap);
282 void bsetroot::gradient(void) {
283 for (unsigned int screen = 0; screen < getNumberOfScreens(); screen++) {
284 BTexture texture(grad, this, screen, img_ctrl[screen]);
285 const ScreenInfo *screen_info = getScreenInfo(screen);
287 texture.setColor(BColor(fore, this, screen));
288 texture.setColorTo(BColor(back, this, screen));
291 img_ctrl[screen]->renderImage(screen_info->getWidth(),
292 screen_info->getHeight(),
295 XSetWindowBackgroundPixmap(getXDisplay(),
296 screen_info->getRootWindow(),
298 XClearWindow(getXDisplay(), screen_info->getRootWindow());
300 setPixmapProperty(screen,
301 duplicatePixmap(screen, pixmap,
302 screen_info->getWidth(),
303 screen_info->getHeight()));
305 if (! (screen_info->getVisual()->c_class & 1)) {
306 img_ctrl[screen]->removeImage(pixmap);
312 void bsetroot::usage(int exit_code) {
314 i18n(bsetrootSet, bsetrootUsage,
316 "Copyright (c) 1997-2000, 2002 Bradley T Hughes\n"
317 "Copyright (c) 2001-2002 Sean 'Shaleh' Perry\n\n"
318 " -display <string> display connection\n"
319 " -mod <x> <y> modula pattern\n"
320 " -foreground, -fg <color> modula foreground color\n"
321 " -background, -bg <color> modula background color\n\n"
322 " -gradient <texture> gradient texture\n"
323 " -from <color> gradient start color\n"
324 " -to <color> gradient end color\n\n"
325 " -solid <color> solid color\n\n"
326 " -help print this help text and exit\n"),
327 getApplicationName());
332 int main(int argc, char **argv) {
333 char *display_name = (char *) 0;
335 i18n.openCatalog("blackbox.cat");
337 for (int i = 1; i < argc; i++) {
338 if (! strcmp(argv[i], "-display")) {
339 // check for -display option
342 fprintf(stderr, i18n(mainSet, mainDISPLAYRequiresArg,
343 "error: '-display' requires an argument\n"));
348 display_name = argv[i];
352 bsetroot app(argc, argv, display_name);