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>
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"
32 #include <X11/Xatom.h>
36 // ignore all X errors
37 static int x11_error(::Display
*, XErrorEvent
*)
41 bsetroot::bsetroot(int argc
, char **argv
, char *dpy_name
,
42 bool multi_head
): display(dpy_name
, multi_head
) {
43 XSetErrorHandler(x11_error
); // silently handle all errors
45 bool mod
= False
, sol
= False
, grd
= False
;
46 int mod_x
= 0, mod_y
= 0;
48 for (int i
= 1; i
< argc
; i
++) {
49 if (! strcmp("-help", argv
[i
])) {
51 } else if ((! strcmp("-fg", argv
[i
])) ||
52 (! strcmp("-foreground", argv
[i
])) ||
53 (! strcmp("-from", argv
[i
]))) {
54 if ((++i
) >= argc
) usage(1);
57 } else if ((! strcmp("-bg", argv
[i
])) ||
58 (! strcmp("-background", argv
[i
])) ||
59 (! strcmp("-to", argv
[i
]))) {
60 if ((++i
) >= argc
) usage(1);
63 } else if (! strcmp("-solid", argv
[i
])) {
64 if ((++i
) >= argc
) usage(1);
68 } else if (! strcmp("-mod", argv
[i
])) {
69 if ((++i
) >= argc
) usage();
71 mod_x
= atoi(argv
[i
]);
73 if ((++i
) >= argc
) usage();
75 mod_y
= atoi(argv
[i
]);
77 if (mod_x
< 1) mod_x
= 1;
78 if (mod_y
< 1) mod_y
= 1;
81 } else if (! strcmp("-gradient", argv
[i
])) {
82 if ((++i
) >= argc
) usage();
86 } else if (! strcmp("-display", argv
[i
])) {
87 // -display passed through tests ealier... we
94 if ((mod
+ sol
+ grd
) != True
) {
96 "bsetroot: error: must specify one of: -solid, -mod, -gradient\n");
101 // keep the server grabbed while rendering all screens
102 XGrabServer(display
.XDisplay());
104 if (sol
&& ! fore
.empty())
106 else if (mod
&& mod_x
&& mod_y
&& ! (fore
.empty() || back
.empty()))
107 modula(mod_x
, mod_y
);
108 else if (grd
&& ! (grad
.empty() || fore
.empty() || back
.empty()))
113 // ungrab the server and discard any events
114 XUngrabServer(display
.XDisplay());
115 XSync(display
.XDisplay(), True
);
119 bsetroot::~bsetroot(void) {
120 XSetCloseDownMode(display
.XDisplay(), RetainPermanent
);
121 XKillClient(display
.XDisplay(), AllTemporary
);
125 // adapted from wmsetbg
126 void bsetroot::setPixmapProperty(int screen
, Pixmap pixmap
) {
127 Atom rootpmap_id
= XInternAtom(display
.XDisplay(), "_XROOTPMAP_ID", False
),
128 esetroot_id
= XInternAtom(display
.XDisplay(), "ESETROOT_PMAP_ID", False
);
130 const bt::ScreenInfo
&screen_info
= display
.screenInfo(screen
);
134 unsigned long length
, after
;
135 unsigned char *data
= 0;
136 Pixmap xrootpmap
= None
;
137 Pixmap esetrootpmap
= None
;
139 // Clear out the old _XROOTPMAP_ID property
140 XGetWindowProperty(display
.XDisplay(), screen_info
.rootWindow(),
141 rootpmap_id
, 0L, 1L, True
, AnyPropertyType
,
142 &type
, &format
, &length
, &after
, &data
);
144 if (data
&& type
== XA_PIXMAP
&& format
== 32 && length
== 1)
145 xrootpmap
= *(reinterpret_cast<Pixmap
*>(data
));
147 if (data
) XFree(data
);
149 // Clear out the old ESETROOT_PMAP_ID property
150 XGetWindowProperty(display
.XDisplay(), screen_info
.rootWindow(),
151 esetroot_id
, 0L, 1L, True
, AnyPropertyType
,
152 &type
, &format
, &length
, &after
, &data
);
154 if (data
&& type
== XA_PIXMAP
&& format
== 32 && length
== 1)
155 esetrootpmap
= *(reinterpret_cast<Pixmap
*>(data
));
157 if (data
) XFree(data
);
159 // Destroy the old pixmaps
161 XKillClient(display
.XDisplay(), xrootpmap
);
162 if (esetrootpmap
&& esetrootpmap
!= xrootpmap
)
163 XKillClient(display
.XDisplay(), esetrootpmap
);
166 XChangeProperty(display
.XDisplay(), screen_info
.rootWindow(),
167 rootpmap_id
, XA_PIXMAP
, 32, PropModeReplace
,
168 reinterpret_cast<unsigned char *>(&pixmap
), 1);
169 XChangeProperty(display
.XDisplay(), screen_info
.rootWindow(),
170 esetroot_id
, XA_PIXMAP
, 32, PropModeReplace
,
171 reinterpret_cast<unsigned char *>(&pixmap
), 1);
176 unsigned long bsetroot::duplicateColor(unsigned int screen
,
177 const bt::Color
&color
) {
179 When using a colormap that is not read-only, we need to
180 reallocate the color we are using. The application's color cache
181 will be freed on exit, and another client can allocate a new
182 color at the same pixel value, which will immediately change the
183 color of the root window.
185 this is not what we want, so we need to make sure that the pixel
186 we are using is doubly allocated, so that it stays around with the
187 pixmap. It will be released when we use
188 XKillClient(..., AllTemporary);
190 const bt::ScreenInfo
&screen_info
= display
.screenInfo(screen
);
191 unsigned long pixel
= color
.pixel(screen
);
193 xcolor
.pixel
= pixel
;
194 XQueryColor(display
.XDisplay(), screen_info
.colormap(), &xcolor
);
195 if (! XAllocColor(display
.XDisplay(), screen_info
.colormap(),
197 fprintf(stderr
, "warning: couldn't duplicate color %02x/%02x/%02x\n",
198 color
.red(), color
.green(), color
.blue());
204 void bsetroot::solid(void) {
205 bt::Color c
= bt::Color::namedColor(display
, 0, fore
);
207 for (unsigned int screen
= 0; screen
< display
.screenCount(); screen
++) {
208 const bt::ScreenInfo
&screen_info
= display
.screenInfo(screen
);
209 unsigned long pixel
= duplicateColor(screen
, c
);
211 XSetWindowBackground(display
.XDisplay(), screen_info
.rootWindow(), pixel
);
212 XClearWindow(display
.XDisplay(), screen_info
.rootWindow());
215 XCreatePixmap(display
.XDisplay(), screen_info
.rootWindow(),
216 8, 8, DefaultDepth(display
.XDisplay(), screen
));
218 bt::Pen
pen(screen
, c
);
219 XFillRectangle(display
.XDisplay(), pixmap
, pen
.gc(), 0, 0, 8, 8);
221 setPixmapProperty(screen
, pixmap
);
226 void bsetroot::modula(int x
, int y
) {
230 unsigned int screen
, i
;
232 bt::Color f
= bt::Color::namedColor(display
, 0, fore
),
233 b
= bt::Color::namedColor(display
, 0, back
);
235 for (pattern
= 0, screen
= 0; screen
< display
.screenCount(); screen
++) {
236 for (i
= 0; i
< 16; i
++) {
242 for (i
= 0; i
< 16; i
++) {
244 data
[(i
* 2)] = static_cast<char>(0xff);
245 data
[(i
* 2) + 1] = static_cast<char>(0xff);
247 data
[(i
* 2)] = pattern
& 0xff;
248 data
[(i
* 2) + 1] = (pattern
>> 8) & 0xff;
252 const bt::ScreenInfo
&screen_info
= display
.screenInfo(screen
);
255 gcv
.foreground
= duplicateColor(screen
, f
);
256 gcv
.background
= duplicateColor(screen
, b
);
258 GC gc
= XCreateGC(display
.XDisplay(), screen_info
.rootWindow(),
259 GCForeground
| GCBackground
, &gcv
);
261 Pixmap pixmap
= XCreatePixmap(display
.XDisplay(),
262 screen_info
.rootWindow(),
263 16, 16, screen_info
.depth());
266 XCreateBitmapFromData(display
.XDisplay(), screen_info
.rootWindow(),
268 XCopyPlane(display
.XDisplay(), bitmap
, pixmap
, gc
,
269 0, 0, 16, 16, 0, 0, 1l);
270 XFreeGC(display
.XDisplay(), gc
);
271 XFreePixmap(display
.XDisplay(), bitmap
);
273 XSetWindowBackgroundPixmap(display
.XDisplay(),
274 screen_info
.rootWindow(),
276 XClearWindow(display
.XDisplay(), screen_info
.rootWindow());
278 setPixmapProperty(screen
, pixmap
);
283 void bsetroot::gradient(void) {
285 we have to be sure that neither raised nor sunken is specified otherwise
286 odd looking borders appear. So we convert to lowercase then look for
287 'raised' or 'sunken' in the description and erase them. To be paranoid
288 the search is done in a loop.
290 std::string descr
= bt::tolower(grad
);
291 std::string::size_type pos
;
292 while ((pos
= descr
.find("raised")) != std::string::npos
)
293 descr
.erase(pos
, 6); // 6 is strlen raised
294 while ((pos
= descr
.find("sunken")) != std::string::npos
)
297 // now add on 'flat' to prevent the bevels from being added
300 bt::Color f
= bt::Color::namedColor(display
, 0, fore
);
301 bt::Color b
= bt::Color::namedColor(display
, 0, back
);
304 texture
.setDescription(descr
);
305 texture
.setColor1(f
);
306 texture
.setColor2(b
);
308 for (unsigned int screen
= 0; screen
< display
.screenCount(); screen
++) {
309 const bt::ScreenInfo
&screen_info
= display
.screenInfo(screen
);
311 bt::Image
image(screen_info
.width(), screen_info
.height());
312 Pixmap pixmap
= image
.render(display
, screen
, texture
);
314 XSetWindowBackgroundPixmap(display
.XDisplay(),
315 screen_info
.rootWindow(),
317 XClearWindow(display
.XDisplay(), screen_info
.rootWindow());
319 setPixmapProperty(screen
, pixmap
);
324 void bsetroot::usage(int exit_code
) {
327 "Copyright (c) 2001 - 2005 Sean 'Shaleh' Perry\n"
328 "Copyright (c) 1997 - 2000, 2002 - 2005 Bradley T Hughes\n");
330 " -display <string> use display connection\n"
331 " -mod <x> <y> modula pattern\n"
332 " -foreground, -fg <color> modula foreground color\n"
333 " -background, -bg <color> modula background color\n\n"
334 " -gradient <texture> gradient texture\n"
335 " -from <color> gradient start color\n"
336 " -to <color> gradient end color\n\n"
337 " -solid <color> solid color\n\n"
338 " -help print this help text and exit\n");
342 int main(int argc
, char **argv
) {
343 char *display_name
= 0;
344 bool multi_head
= False
;
346 for (int i
= 1; i
< argc
; i
++) {
347 if (! strcmp(argv
[i
], "-display")) {
348 // check for -display option
351 fprintf(stderr
, "error: '-display' requires an argument\n");
356 display_name
= argv
[i
];
357 } else if (! strcmp(argv
[i
], "-multi")) {
362 bsetroot
app(argc
, argv
, display_name
, multi_head
);