apply patch from 1696203, which enables window snapping for windows on all workspaces
[blackbox.git] / util / bsetroot.cc
blob8b3b0551f59a97374854420adf41df5b5298e18c
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // bsetroot - a background setting utility
3 // Copyright (c) 2001 - 2005 Sean 'Shaleh' Perry <shaleh at debian.org>
4 // Copyright (c) 1997 - 2000, 2002 - 2005
5 // Bradley T Hughes <bhughes at trolltech.com>
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a
8 // copy of this software and associated documentation files (the "Software"),
9 // to deal in the Software without restriction, including without limitation
10 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 // and/or sell copies of the Software, and to permit persons to whom the
12 // Software is furnished to do so, subject to the following conditions:
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 // DEALINGS IN THE SOFTWARE.
25 #include "bsetroot.hh"
27 #include <Pen.hh>
28 #include <Texture.hh>
30 #include <cctype>
32 #include <X11/Xatom.h>
33 #include <stdio.h>
34 #include <stdlib.h>
37 // ignore all X errors
38 static int x11_error(::Display *, XErrorEvent *)
39 { return 0; }
42 bsetroot::bsetroot(int argc, char **argv, char *dpy_name,
43 bool multi_head): display(dpy_name, multi_head) {
44 XSetErrorHandler(x11_error); // silently handle all errors
46 bool mod = False, sol = False, grd = False;
47 int mod_x = 0, mod_y = 0;
49 for (int i = 1; i < argc; i++) {
50 if (! strcmp("-help", argv[i])) {
51 usage();
52 } else if ((! strcmp("-fg", argv[i])) ||
53 (! strcmp("-foreground", argv[i])) ||
54 (! strcmp("-from", argv[i]))) {
55 if ((++i) >= argc) usage(1);
57 fore = argv[i];
58 } else if ((! strcmp("-bg", argv[i])) ||
59 (! strcmp("-background", argv[i])) ||
60 (! strcmp("-to", argv[i]))) {
61 if ((++i) >= argc) usage(1);
63 back = argv[i];
64 } else if (! strcmp("-solid", argv[i])) {
65 if ((++i) >= argc) usage(1);
67 fore = argv[i];
68 sol = True;
69 } else if (! strcmp("-mod", argv[i])) {
70 if ((++i) >= argc) usage();
72 mod_x = atoi(argv[i]);
74 if ((++i) >= argc) usage();
76 mod_y = atoi(argv[i]);
78 if (mod_x < 1) mod_x = 1;
79 if (mod_y < 1) mod_y = 1;
81 mod = True;
82 } else if (! strcmp("-gradient", argv[i])) {
83 if ((++i) >= argc) usage();
85 grad = argv[i];
86 grd = True;
87 } else if (! strcmp("-display", argv[i])) {
88 // -display passed through tests ealier... we
89 i++;
90 } else {
91 usage();
95 if ((mod + sol + grd) != True) {
96 fprintf(stderr,
97 "bsetroot: error: must specify one of: -solid, -mod, -gradient\n");
99 usage(2);
102 // keep the server grabbed while rendering all screens
103 XGrabServer(display.XDisplay());
105 if (sol && ! fore.empty())
106 solid();
107 else if (mod && mod_x && mod_y && ! (fore.empty() || back.empty()))
108 modula(mod_x, mod_y);
109 else if (grd && ! (grad.empty() || fore.empty() || back.empty()))
110 gradient();
111 else
112 usage();
114 // ungrab the server and discard any events
115 XUngrabServer(display.XDisplay());
116 XSync(display.XDisplay(), True);
120 bsetroot::~bsetroot(void) {
121 XSetCloseDownMode(display.XDisplay(), RetainPermanent);
122 XKillClient(display.XDisplay(), AllTemporary);
126 // adapted from wmsetbg
127 void bsetroot::setPixmapProperty(int screen, Pixmap pixmap) {
128 Atom rootpmap_id = XInternAtom(display.XDisplay(), "_XROOTPMAP_ID", False),
129 esetroot_id = XInternAtom(display.XDisplay(), "ESETROOT_PMAP_ID", False);
131 const bt::ScreenInfo &screen_info = display.screenInfo(screen);
133 Atom type;
134 int format;
135 unsigned long length, after;
136 unsigned char *data = 0;
137 Pixmap xrootpmap = None;
138 Pixmap esetrootpmap = None;
140 // Clear out the old _XROOTPMAP_ID property
141 XGetWindowProperty(display.XDisplay(), screen_info.rootWindow(),
142 rootpmap_id, 0L, 1L, True, AnyPropertyType,
143 &type, &format, &length, &after, &data);
145 if (data && type == XA_PIXMAP && format == 32 && length == 1)
146 xrootpmap = *(reinterpret_cast<Pixmap *>(data));
148 if (data) XFree(data);
150 // Clear out the old ESETROOT_PMAP_ID property
151 XGetWindowProperty(display.XDisplay(), screen_info.rootWindow(),
152 esetroot_id, 0L, 1L, True, AnyPropertyType,
153 &type, &format, &length, &after, &data);
155 if (data && type == XA_PIXMAP && format == 32 && length == 1)
156 esetrootpmap = *(reinterpret_cast<Pixmap *>(data));
158 if (data) XFree(data);
160 // Destroy the old pixmaps
161 if (xrootpmap)
162 XKillClient(display.XDisplay(), xrootpmap);
163 if (esetrootpmap && esetrootpmap != xrootpmap)
164 XKillClient(display.XDisplay(), esetrootpmap);
166 if (pixmap) {
167 XChangeProperty(display.XDisplay(), screen_info.rootWindow(),
168 rootpmap_id, XA_PIXMAP, 32, PropModeReplace,
169 reinterpret_cast<unsigned char *>(&pixmap), 1);
170 XChangeProperty(display.XDisplay(), screen_info.rootWindow(),
171 esetroot_id, XA_PIXMAP, 32, PropModeReplace,
172 reinterpret_cast<unsigned char *>(&pixmap), 1);
177 unsigned long bsetroot::duplicateColor(unsigned int screen,
178 const bt::Color &color) {
180 When using a colormap that is not read-only, we need to
181 reallocate the color we are using. The application's color cache
182 will be freed on exit, and another client can allocate a new
183 color at the same pixel value, which will immediately change the
184 color of the root window.
186 this is not what we want, so we need to make sure that the pixel
187 we are using is doubly allocated, so that it stays around with the
188 pixmap. It will be released when we use
189 XKillClient(..., AllTemporary);
191 const bt::ScreenInfo &screen_info = display.screenInfo(screen);
192 unsigned long pixel = color.pixel(screen);
193 XColor xcolor;
194 xcolor.pixel = pixel;
195 XQueryColor(display.XDisplay(), screen_info.colormap(), &xcolor);
196 if (! XAllocColor(display.XDisplay(), screen_info.colormap(),
197 &xcolor)) {
198 fprintf(stderr, "warning: couldn't duplicate color %02x/%02x/%02x\n",
199 color.red(), color.green(), color.blue());
201 return pixel;
205 void bsetroot::solid(void) {
206 bt::Color c = bt::Color::namedColor(display, 0, fore);
208 for (unsigned int screen = 0; screen < display.screenCount(); screen++) {
209 const bt::ScreenInfo &screen_info = display.screenInfo(screen);
210 unsigned long pixel = duplicateColor(screen, c);
212 XSetWindowBackground(display.XDisplay(), screen_info.rootWindow(), pixel);
213 XClearWindow(display.XDisplay(), screen_info.rootWindow());
215 Pixmap pixmap =
216 XCreatePixmap(display.XDisplay(), screen_info.rootWindow(),
217 8, 8, DefaultDepth(display.XDisplay(), screen));
219 bt::Pen pen(screen, c);
220 XFillRectangle(display.XDisplay(), pixmap, pen.gc(), 0, 0, 8, 8);
222 setPixmapProperty(screen, pixmap);
227 void bsetroot::modula(int x, int y) {
228 char data[32];
229 long pattern;
231 unsigned int screen, i;
233 bt::Color f = bt::Color::namedColor(display, 0, fore),
234 b = bt::Color::namedColor(display, 0, back);
236 for (pattern = 0, screen = 0; screen < display.screenCount(); screen++) {
237 for (i = 0; i < 16; i++) {
238 pattern <<= 1;
239 if ((i % x) == 0)
240 pattern |= 0x0001;
243 for (i = 0; i < 16; i++) {
244 if ((i % y) == 0) {
245 data[(i * 2)] = static_cast<char>(0xff);
246 data[(i * 2) + 1] = static_cast<char>(0xff);
247 } else {
248 data[(i * 2)] = pattern & 0xff;
249 data[(i * 2) + 1] = (pattern >> 8) & 0xff;
253 const bt::ScreenInfo &screen_info = display.screenInfo(screen);
255 XGCValues gcv;
256 gcv.foreground = duplicateColor(screen, f);
257 gcv.background = duplicateColor(screen, b);
259 GC gc = XCreateGC(display.XDisplay(), screen_info.rootWindow(),
260 GCForeground | GCBackground, &gcv);
262 Pixmap pixmap = XCreatePixmap(display.XDisplay(),
263 screen_info.rootWindow(),
264 16, 16, screen_info.depth());
266 Pixmap bitmap =
267 XCreateBitmapFromData(display.XDisplay(), screen_info.rootWindow(),
268 data, 16, 16);
269 XCopyPlane(display.XDisplay(), bitmap, pixmap, gc,
270 0, 0, 16, 16, 0, 0, 1l);
271 XFreeGC(display.XDisplay(), gc);
272 XFreePixmap(display.XDisplay(), bitmap);
274 XSetWindowBackgroundPixmap(display.XDisplay(),
275 screen_info.rootWindow(),
276 pixmap);
277 XClearWindow(display.XDisplay(), screen_info.rootWindow());
279 setPixmapProperty(screen, pixmap);
284 void bsetroot::gradient(void) {
286 we have to be sure that neither raised nor sunken is specified otherwise
287 odd looking borders appear. So we convert to lowercase then look for
288 'raised' or 'sunken' in the description and erase them. To be paranoid
289 the search is done in a loop.
291 std::string descr = bt::tolower(grad);
292 std::string::size_type pos;
293 while ((pos = descr.find("raised")) != std::string::npos)
294 descr.erase(pos, 6); // 6 is strlen raised
295 while ((pos = descr.find("sunken")) != std::string::npos)
296 descr.erase(pos, 6);
298 // now add on 'flat' to prevent the bevels from being added
299 descr += "flat";
301 bt::Color f = bt::Color::namedColor(display, 0, fore);
302 bt::Color b = bt::Color::namedColor(display, 0, back);
304 bt::Texture texture;
305 texture.setDescription(descr);
306 texture.setColor1(f);
307 texture.setColor2(b);
309 for (unsigned int screen = 0; screen < display.screenCount(); screen++) {
310 const bt::ScreenInfo &screen_info = display.screenInfo(screen);
312 bt::Image image(screen_info.width(), screen_info.height());
313 Pixmap pixmap = image.render(display, screen, texture);
315 XSetWindowBackgroundPixmap(display.XDisplay(),
316 screen_info.rootWindow(),
317 pixmap);
318 XClearWindow(display.XDisplay(), screen_info.rootWindow());
320 setPixmapProperty(screen, pixmap);
325 void bsetroot::usage(int exit_code) {
326 fprintf(stderr,
327 "bsetroot 3.1\n\n"
328 "Copyright (c) 2001 - 2005 Sean 'Shaleh' Perry\n"
329 "Copyright (c) 1997 - 2000, 2002 - 2005 Bradley T Hughes\n");
330 fprintf(stderr,
331 " -display <string> use display connection\n"
332 " -mod <x> <y> modula pattern\n"
333 " -foreground, -fg <color> modula foreground color\n"
334 " -background, -bg <color> modula background color\n\n"
335 " -gradient <texture> gradient texture\n"
336 " -from <color> gradient start color\n"
337 " -to <color> gradient end color\n\n"
338 " -solid <color> solid color\n\n"
339 " -help print this help text and exit\n");
340 exit(exit_code);
343 int main(int argc, char **argv) {
344 char *display_name = 0;
345 bool multi_head = False;
347 for (int i = 1; i < argc; i++) {
348 if (! strcmp(argv[i], "-display")) {
349 // check for -display option
351 if ((++i) >= argc) {
352 fprintf(stderr, "error: '-display' requires an argument\n");
354 ::exit(1);
357 display_name = argv[i];
358 } else if (! strcmp(argv[i], "-multi")) {
359 multi_head = True;
363 bsetroot app(argc, argv, display_name, multi_head);
364 return 0;