]> icculus.org git repositories - mikachu/openbox.git/blob - util/bsetroot.cc
use the raw screen size not the strut adjusted size to determine max_height and width...
[mikachu/openbox.git] / util / bsetroot.cc
1 // -*- mode++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // bsetroot.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>
5 //
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:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
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.
23
24 #ifdef    HAVE_CONFIG_H
25 #  include "../config.h"
26 #endif // HAVE_CONFIG_H
27
28 extern "C" {
29 #ifdef HAVE_STDLIB_H
30 #  include <stdlib.h>
31 #endif // HAVE_STDLIB_H
32
33 #ifdef HAVE_STRING_H
34 #  include <string.h>
35 #endif // HAVE_STRING_H
36
37 #ifdef    HAVE_STDIO_H
38 #  include <stdio.h>
39 #endif // HAVE_STDIO_H
40 }
41
42 #include "../src/i18n.hh"
43 #include "../src/GCCache.hh"
44 #include "../src/Texture.hh"
45 #include "../src/Util.hh"
46 #include "bsetroot.hh"
47
48
49 I18n i18n;
50
51 bsetroot::bsetroot(int argc, char **argv, char *dpy_name)
52   : BaseDisplay(argv[0], dpy_name)
53 {
54   grad = fore = back = (char *) 0;
55
56   bool mod = False, sol = False, grd = False;
57   int mod_x = 0, mod_y = 0;
58
59   for (int i = 1; i < argc; i++) {
60     if (! strcmp("-help", argv[i])) {
61       usage();
62     } else if ((! strcmp("-fg", argv[i])) ||
63                (! strcmp("-foreground", argv[i])) ||
64                (! strcmp("-from", argv[i]))) {
65       if ((++i) >= argc) usage(1);
66
67       fore = argv[i];
68     } else if ((! strcmp("-bg", argv[i])) ||
69                (! strcmp("-background", argv[i])) ||
70                (! strcmp("-to", argv[i]))) {
71       if ((++i) >= argc) usage(1);
72
73       back = argv[i];
74     } else if (! strcmp("-solid", argv[i])) {
75       if ((++i) >= argc) usage(1);
76
77       fore = argv[i];
78       sol = True;
79     } else if (! strcmp("-mod", argv[i])) {
80       if ((++i) >= argc) usage();
81
82       mod_x = atoi(argv[i]);
83
84       if ((++i) >= argc) usage();
85
86       mod_y = atoi(argv[i]);
87
88       if (mod_x < 1) mod_x = 1;
89       if (mod_y < 1) mod_y = 1;
90
91       mod = True;
92     } else if (! strcmp("-gradient", argv[i])) {
93       if ((++i) >= argc) usage();
94
95       grad = argv[i];
96       grd = True;
97     } else if (! strcmp("-display", argv[i])) {
98       // -display passed through tests ealier... we just skip it now
99       i++;
100     } else
101       usage();
102   }
103
104   if ((mod + sol + grd) != True) {
105     fprintf(stderr,
106             i18n(bsetrootSet, bsetrootMustSpecify,
107                  "%s: error: must specify one of: -solid, -mod, -gradient\n"),
108             getApplicationName());
109
110     usage(2);
111   }
112
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);
116
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();
120   else usage();
121 }
122
123
124 bsetroot::~bsetroot(void) {
125   XSetCloseDownMode(getXDisplay(), RetainPermanent);
126
127   XKillClient(getXDisplay(), AllTemporary);
128
129   std::for_each(img_ctrl, img_ctrl + getNumberOfScreens(), PointerAssassin());
130
131   delete [] img_ctrl;
132 }
133
134
135 // adapted from wmsetbg
136 void bsetroot::setPixmapProperty(int screen, Pixmap pixmap) {
137   static Atom rootpmap_id = None, esetroot_id = None;
138   Atom type;
139   int format;
140   unsigned long length, after;
141   unsigned char *data;
142   const ScreenInfo *screen_info = getScreenInfo(screen);
143
144   if (rootpmap_id == None) {
145     rootpmap_id = XInternAtom(getXDisplay(), "_XROOTPMAP_ID", False);
146     esetroot_id = XInternAtom(getXDisplay(), "ESETROOT_PMAP_ID", False);
147   }
148
149   XGrabServer(getXDisplay());
150
151   /* Clear out the old pixmap */
152   XGetWindowProperty(getXDisplay(), screen_info->getRootWindow(),
153                      rootpmap_id, 0L, 1L, False, AnyPropertyType,
154                      &type, &format, &length, &after, &data);
155
156   if ((type == XA_PIXMAP) && (format == 32) && (length == 1)) {
157     unsigned char* data_esetroot = 0;
158     XGetWindowProperty(getXDisplay(), screen_info->getRootWindow(),
159                        esetroot_id, 0L, 1L, False, AnyPropertyType,
160                        &type, &format, &length, &after, &data_esetroot);
161     if (data && data_esetroot && *((Pixmap *) data)) {
162       XKillClient(getXDisplay(), *((Pixmap *) data));
163       XSync(getXDisplay(), False);
164       XFree(data_esetroot);
165     }
166     XFree(data);
167   }
168
169   if (pixmap) {
170     XChangeProperty(getXDisplay(), screen_info->getRootWindow(),
171                     rootpmap_id, XA_PIXMAP, 32, PropModeReplace,
172                     (unsigned char *) &pixmap, 1);
173     XChangeProperty(getXDisplay(), screen_info->getRootWindow(),
174                     esetroot_id, XA_PIXMAP, 32, PropModeReplace,
175                     (unsigned char *) &pixmap, 1);
176   } else {
177     XDeleteProperty(getXDisplay(), screen_info->getRootWindow(),
178                     rootpmap_id);
179     XDeleteProperty(getXDisplay(), screen_info->getRootWindow(),
180                     esetroot_id);
181   }
182
183   XUngrabServer(getXDisplay());
184   XFlush(getXDisplay());
185 }
186
187
188 // adapted from wmsetbg
189 Pixmap bsetroot::duplicatePixmap(int screen, Pixmap pixmap,
190                                  int width, int height) {
191   XSync(getXDisplay(), False);
192
193   Pixmap copyP = XCreatePixmap(getXDisplay(),
194                                getScreenInfo(screen)->getRootWindow(),
195                                width, height,
196                                DefaultDepth(getXDisplay(), screen));
197   XCopyArea(getXDisplay(), pixmap, copyP, DefaultGC(getXDisplay(), screen),
198             0, 0, width, height, 0, 0);
199   XSync(getXDisplay(), False);
200
201   return copyP;
202 }
203
204
205 void bsetroot::solid(void) {
206   for (unsigned int screen = 0; screen < getNumberOfScreens(); screen++) {
207     BColor c(fore, this, screen);
208     const ScreenInfo *screen_info = getScreenInfo(screen);
209
210     XSetWindowBackground(getXDisplay(), screen_info->getRootWindow(),
211                          c.pixel());
212     XClearWindow(getXDisplay(), screen_info->getRootWindow());
213
214     Pixmap pixmap = XCreatePixmap(getXDisplay(),
215                                   screen_info->getRootWindow(),
216                                   8, 8, DefaultDepth(getXDisplay(), screen));
217     BPen pen(c);
218     XFillRectangle(getXDisplay(), pixmap, pen.gc(), 0, 0, 8, 8);
219
220     setPixmapProperty(screen, duplicatePixmap(screen, pixmap, 8, 8));
221
222     XFreePixmap(getXDisplay(), pixmap);
223   }
224 }
225
226
227 void bsetroot::modula(int x, int y) {
228   char data[32];
229   long pattern;
230
231   unsigned int screen, i;
232
233   for (pattern = 0, screen = 0; screen < getNumberOfScreens(); screen++) {
234     for (i = 0; i < 16; i++) {
235       pattern <<= 1;
236       if ((i % x) == 0)
237         pattern |= 0x0001;
238     }
239
240     for (i = 0; i < 16; i++) {
241       if ((i %  y) == 0) {
242         data[(i * 2)] = static_cast<char>(0xff);
243         data[(i * 2) + 1] = static_cast<char>(0xff);
244       } else {
245         data[(i * 2)] = pattern & 0xff;
246         data[(i * 2) + 1] = (pattern >> 8) & 0xff;
247       }
248     }
249
250     BColor f(fore, this, screen), b(back, this, screen);
251     GC gc;
252     Pixmap bitmap;
253     const ScreenInfo *screen_info = getScreenInfo(screen);
254
255     bitmap =
256       XCreateBitmapFromData(getXDisplay(),
257                             screen_info->getRootWindow(), data,
258                             16, 16);
259
260     XGCValues gcv;
261     gcv.foreground = f.pixel();
262     gcv.background = b.pixel();
263
264     gc = XCreateGC(getXDisplay(), screen_info->getRootWindow(),
265                    GCForeground | GCBackground, &gcv);
266
267     Pixmap pixmap = XCreatePixmap(getXDisplay(),
268                                   screen_info->getRootWindow(),
269                                   16, 16, screen_info->getDepth());
270
271     XCopyPlane(getXDisplay(), bitmap, pixmap, gc,
272                0, 0, 16, 16, 0, 0, 1l);
273     XSetWindowBackgroundPixmap(getXDisplay(),
274                                screen_info->getRootWindow(),
275                                pixmap);
276     XClearWindow(getXDisplay(), screen_info->getRootWindow());
277
278     setPixmapProperty(screen,
279                       duplicatePixmap(screen, pixmap, 16, 16));
280
281     XFreeGC(getXDisplay(), gc);
282     XFreePixmap(getXDisplay(), bitmap);
283
284     if (! (screen_info->getVisual()->c_class & 1))
285       XFreePixmap(getXDisplay(), pixmap);
286   }
287 }
288
289
290 void bsetroot::gradient(void) {
291   for (unsigned int screen = 0; screen < getNumberOfScreens(); screen++) {
292     BTexture texture(grad, this, screen, img_ctrl[screen]);
293     const ScreenInfo *screen_info = getScreenInfo(screen);
294
295     texture.setColor(BColor(fore, this, screen));
296     texture.setColorTo(BColor(back, this, screen));
297
298     Pixmap pixmap =
299       img_ctrl[screen]->renderImage(screen_info->getWidth(),
300                                     screen_info->getHeight(),
301                                     texture);
302
303     XSetWindowBackgroundPixmap(getXDisplay(),
304                                screen_info->getRootWindow(),
305                                pixmap);
306     XClearWindow(getXDisplay(), screen_info->getRootWindow());
307
308     setPixmapProperty(screen,
309                       duplicatePixmap(screen, pixmap,
310                                       screen_info->getWidth(),
311                                       screen_info->getHeight()));
312
313     if (! (screen_info->getVisual()->c_class & 1)) {
314       img_ctrl[screen]->removeImage(pixmap);
315     }
316   }
317 }
318
319
320 void bsetroot::usage(int exit_code) {
321     fprintf(stderr,
322             i18n(bsetrootSet, bsetrootUsage,
323                  "%s 2.0\n\n"
324                  "Copyright (c) 1997-2000, 2002 Bradley T Hughes\n"
325                  "Copyright (c) 2001-2002 Sean 'Shaleh' Perry\n\n"
326                  "  -display <string>        display connection\n"
327                  "  -mod <x> <y>             modula pattern\n"
328                  "  -foreground, -fg <color> modula foreground color\n"
329                  "  -background, -bg <color> modula background color\n\n"
330                  "  -gradient <texture>      gradient texture\n"
331                  "  -from <color>            gradient start color\n"
332                  "  -to <color>              gradient end color\n\n"
333                  "  -solid <color>           solid color\n\n"
334                  "  -help                    print this help text and exit\n"),
335             getApplicationName());
336
337     exit(exit_code);
338 }
339
340 int main(int argc, char **argv) {
341   char *display_name = (char *) 0;
342
343   i18n.openCatalog("blackbox.cat");
344
345   for (int i = 1; i < argc; i++) {
346     if (! strcmp(argv[i], "-display")) {
347       // check for -display option
348
349       if ((++i) >= argc) {
350         fprintf(stderr, i18n(mainSet, mainDISPLAYRequiresArg,
351                              "error: '-display' requires an argument\n"));
352
353         ::exit(1);
354       }
355
356       display_name = argv[i];
357     }
358   }
359
360   bsetroot app(argc, argv, display_name);
361
362   return 0;
363 }