1
// FbWindow.cc for FbTk - fluxbox toolkit
2
// Copyright (c) 2002 - 2005 Henrik Kinnunen (fluxgen at fluxbox dot org)
4
// Permission is hereby granted, free of charge, to any person obtaining a
5
// copy of this software and associated documentation files (the "Software"),
6
// to deal in the Software without restriction, including without limitation
7
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
// and/or sell copies of the Software, and to permit persons to whom the
9
// Software is furnished to do so, subject to the following conditions:
11
// The above copyright notice and this permission notice shall be included in
12
// all copies or substantial portions of the Software.
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20
// DEALINGS IN THE SOFTWARE.
22
// $Id: FbWindow.cc 3998 2005-05-07 12:59:43Z simonb $
24
#include "FbWindow.hh"
25
#include "FbPixmap.hh"
27
#include "EventManager.hh"
30
#include "Transparent.hh"
34
#endif // HAVE_CONFIG_H
36
#include <X11/Xutil.h>
37
#include <X11/Xatom.h>
47
FbWindow::FbWindow():FbDrawable(), m_parent(0), m_screen_num(0), m_window(0), m_x(0), m_y(0),
48
m_width(0), m_height(0), m_border_width(0), m_depth(0), m_destroy(true),
49
m_lastbg_color_set(false), m_lastbg_color(0), m_lastbg_pm(0), m_renderer(0) {
53
FbWindow::FbWindow(const FbWindow& the_copy):FbDrawable(),
54
m_parent(the_copy.parent()),
55
m_screen_num(the_copy.screenNumber()), m_window(the_copy.window()),
56
m_x(the_copy.x()), m_y(the_copy.y()),
57
m_width(the_copy.width()), m_height(the_copy.height()),
58
m_border_width(the_copy.borderWidth()),
59
m_depth(the_copy.depth()), m_destroy(true),
60
m_lastbg_color_set(false), m_lastbg_color(0),
61
m_lastbg_pm(0), m_renderer(the_copy.m_renderer) {
62
the_copy.m_window = 0;
65
FbWindow::FbWindow(int screen_num,
67
unsigned int width, unsigned int height,
69
bool override_redirect,
75
m_screen_num(screen_num),
77
m_lastbg_color_set(false),
79
m_lastbg_pm(0), m_renderer(0) {
81
create(RootWindow(display(), screen_num),
82
x, y, width, height, eventmask,
83
override_redirect, save_unders, depth, class_type);
86
FbWindow::FbWindow(const FbWindow &parent,
87
int x, int y, unsigned int width, unsigned int height,
89
bool override_redirect,
91
int depth, int class_type):
93
m_screen_num(parent.screenNumber()),
95
m_lastbg_color_set(false), m_lastbg_color(0),
96
m_lastbg_pm(0), m_renderer(0) {
98
create(parent.window(), x, y, width, height, eventmask,
99
override_redirect, save_unders, depth, class_type);
104
FbWindow::FbWindow(Window client):FbDrawable(), m_parent(0),
108
m_width(1), m_height(1),
111
m_destroy(false), // don't destroy this window
112
m_lastbg_color_set(false), m_lastbg_color(0),
113
m_lastbg_pm(0), m_renderer(0) {
118
FbWindow::~FbWindow() {
120
// Need to free xrender pics before destroying window
121
if (m_transparent.get() != 0)
122
m_transparent.reset(0);
125
// so we don't get any dangling eventhandler for this window
126
FbTk::EventManager::instance()->remove(m_window);
128
XDestroyWindow(display(), m_window);
132
void FbWindow::setBackgroundColor(const FbTk::Color &bg_color) {
133
if (bg_color.isAllocated()) {
134
m_lastbg_color = bg_color.pixel();
135
m_lastbg_color_set = true;
138
m_lastbg_color_set = false;
141
updateBackground(false);
144
void FbWindow::setBackgroundPixmap(Pixmap bg_pixmap) {
145
m_lastbg_pm = bg_pixmap;
146
if (bg_pixmap != None)
147
m_lastbg_color_set = false;
149
updateBackground(false);
152
void FbWindow::updateBackground(bool only_if_alpha) {
153
Pixmap newbg = m_lastbg_pm;
154
unsigned char alpha = 255;
155
bool free_newbg = false;
157
if (m_lastbg_pm == None && !m_lastbg_color_set)
160
if (m_transparent.get() != 0)
161
alpha = m_transparent->alpha();
163
if (only_if_alpha && alpha == 255)
166
// still use bg buffer pixmap if not transparent
167
// cause it does nice caching things, assuming we have a renderer
168
if (m_lastbg_pm != ParentRelative && (m_renderer || alpha != 255)) {
169
// update source and destination if needed
170
Pixmap root = FbPixmap::getRootPixmap(screenNumber());
171
if (alpha != 255 && m_transparent->source() != root)
172
m_transparent->setSource(root, screenNumber());
174
FbPixmap newpm = FbPixmap(*this, width(), height(), depth());
175
free_newbg = true; // newpm gets released to newbg at end of block
176
GC gc = XCreateGC(display(), window(), 0, 0);
178
if (m_lastbg_pm == None && m_lastbg_color_set) {
179
XSetForeground(display(), gc, m_lastbg_color);
180
newpm.fillRectangle(gc, 0, 0, width(), height());
182
// copy from window if no color and no bg...
183
newpm.copyArea((m_lastbg_pm == None)?drawable():m_lastbg_pm, gc, 0, 0, 0, 0, width(), height());
185
XFreeGC(display(), gc);
188
m_transparent->setDest(newpm.drawable(), screenNumber());
192
const FbWindow *root_parent = parent();
193
// our position in parent ("root")
194
int root_x = x() + borderWidth(), root_y = y() + borderWidth();
195
if (root_parent != 0) {
196
root_x += root_parent->x() + root_parent->borderWidth();
197
root_y += root_parent->y() + root_parent->borderWidth();
198
while (root_parent->parent() != 0) {
199
root_parent = root_parent->parent();
200
root_x += root_parent->x() + root_parent->borderWidth();
201
root_y += root_parent->y() + root_parent->borderWidth();
205
// render background image from root pos to our window
207
m_transparent->render(root_x, root_y,
211
// render any foreground items
213
m_renderer->renderForeground(*this, newpm);
216
m_transparent->freeDest(); // it's only temporary, don't leave it hanging around
217
newbg = newpm.release();
221
XSetWindowBackgroundPixmap(display(), m_window, newbg);
222
else if (m_lastbg_color_set)
223
XSetWindowBackground(display(), m_window, m_lastbg_color);
226
XFreePixmap(display(), newbg);
229
void FbWindow::setBorderColor(const FbTk::Color &border_color) {
230
XSetWindowBorder(display(), m_window, border_color.pixel());
233
void FbWindow::setBorderWidth(unsigned int size) {
234
XSetWindowBorderWidth(display(), m_window, size);
235
m_border_width = size;
238
void FbWindow::setName(const char *name) {
239
XStoreName(display(), m_window, name);
242
void FbWindow::setEventMask(long mask) {
243
XSelectInput(display(), m_window, mask);
246
void FbWindow::clear() {
247
XClearWindow(display(), m_window);
250
void FbWindow::clearArea(int x, int y,
251
unsigned int width, unsigned int height,
253
// TODO: probably could call renderForeground here (with x,y,w,h)
254
XClearArea(display(), window(), x, y, width, height, exposures);
257
// If override_is_offset, then dest_override is a pixmap located at the_x, the_y
258
// with size the_width x the_height in the target window.
260
void FbWindow::updateTransparent(int the_x, int the_y, unsigned int the_width, unsigned int the_height, Pixmap dest_override, bool override_is_offset) {
262
if (!m_transparent.get())
265
if (width() == 0 || height() == 0)
268
if (!dest_override && (the_width == 0 && the_height == 0 || the_width == width() && the_height == height()) &&
269
the_x <= 0 && the_y <= 0) {
270
// do the whole thing
271
updateBackground(true);
276
dest_override = window();
278
if (the_width == 0 || the_height == 0) {
280
the_height = height();
283
if (the_x < 0 || the_y < 0) {
288
// update source and destination if needed
289
Pixmap root = FbPixmap::getRootPixmap(screenNumber());
290
if (m_transparent->source() != root)
291
m_transparent->setSource(root, screenNumber());
293
if (m_transparent->dest() != dest_override)
294
m_transparent->setDest(dest_override, screenNumber());
298
const FbWindow *root_parent = parent();
299
// our position in parent ("root")
300
int root_x = x() + borderWidth(), root_y = y() + borderWidth();
301
if (root_parent != 0) {
302
root_x += root_parent->x() + root_parent->borderWidth();
303
root_y += root_parent->y() + root_parent->borderWidth();
304
while (root_parent->parent() != 0) {
305
root_parent = root_parent->parent();
306
root_x += root_parent->x() + root_parent->borderWidth();
307
root_y += root_parent->y() + root_parent->borderWidth();
310
} // else toplevel window so we already have x, y set
312
// render background image from root pos to our window
313
m_transparent->render(root_x + the_x, root_y + the_y,
314
override_is_offset?0:the_x, override_is_offset?0:the_y,
315
the_width, the_height);
316
#endif // HAVE_XRENDER
319
void FbWindow::setAlpha(unsigned char alpha) {
321
if (FbTk::Transparent::haveComposite()) {
322
if (m_transparent.get() != 0)
323
m_transparent.reset(0);
325
// don't setOpaque, let controlling objects do that
326
// since it's only needed on toplevel windows
328
if (!FbTk::Transparent::haveRender())
331
if (m_transparent.get() == 0 && alpha < 255) {
332
m_transparent.reset(new Transparent(FbPixmap::getRootPixmap(screenNumber()), window(), alpha, screenNumber()));
333
} else if (alpha < 255 && alpha != m_transparent->alpha())
334
m_transparent->setAlpha(alpha);
335
else if (alpha == 255)
336
m_transparent.reset(0); // destroy transparent object
338
#endif // HAVE_XRENDER
341
unsigned char FbWindow::alpha() const {
343
if (m_transparent.get())
344
return m_transparent->alpha();
345
#endif // HAVE_XRENDER
349
FbWindow &FbWindow::operator = (const FbWindow &win) {
350
m_parent = win.parent();
351
m_screen_num = win.screenNumber();
352
m_window = win.window();
355
m_width = win.width();
356
m_height = win.height();
357
m_border_width = win.borderWidth();
358
m_depth = win.depth();
359
// take over this window
364
FbWindow &FbWindow::operator = (Window win) {
369
void FbWindow::setNew(Window win) {
371
if (m_window != 0 && m_destroy)
372
XDestroyWindow(display(), m_window);
378
XWindowAttributes attr;
381
if (XGetWindowAttributes(display(),
383
&attr) != 0 && attr.screen != 0) {
384
m_screen_num = XScreenNumberOfScreen(attr.screen);
388
m_width = attr.width;
390
if (attr.height <= 0)
393
m_height = attr.height;
397
m_depth = attr.depth;
398
m_border_width = attr.border_width;
404
void FbWindow::show() {
405
XMapWindow(display(), m_window);
408
void FbWindow::showSubwindows() {
409
XMapSubwindows(display(), m_window);
412
void FbWindow::hide() {
413
XUnmapWindow(display(), m_window);
416
void FbWindow::lower() {
417
XLowerWindow(display(), window());
420
void FbWindow::raise() {
421
XRaiseWindow(display(), window());
424
void FbWindow::setInputFocus(int revert_to, int time) {
425
XSetInputFocus(display(), window(), revert_to, time);
428
void FbWindow::setCursor(Cursor cur) {
429
XDefineCursor(display(), window(), cur);
432
void FbWindow::unsetCursor() {
433
XUndefineCursor(display(), window());
436
void FbWindow::reparent(const FbWindow &parent, int x, int y, bool continuing) {
437
XReparentWindow(display(), window(), parent.window(), x, y);
439
if (continuing) // we will continue managing this window after reparent
443
std::string FbWindow::textProperty(Atom property) const {
444
XTextProperty text_prop;
449
if (XGetTextProperty(display(), window(), &text_prop, property) == 0)
452
if (text_prop.value == 0 || text_prop.nitems == 0)
455
if (text_prop.encoding != XA_STRING) {
456
// still returns a "StringList" despite the different name
457
if (XmbTextPropertyToTextList(display(), &text_prop, &stringlist, &count) == 0 || count == 0)
460
if (XTextPropertyToStringList(&text_prop, &stringlist, &count) == 0 || count == 0)
466
XFreeStringList(stringlist);
470
bool FbWindow::property(Atom property,
471
long long_offset, long long_length,
474
Atom *actual_type_return,
475
int *actual_format_return,
476
unsigned long *nitems_return,
477
unsigned long *bytes_after_return,
478
unsigned char **prop_return) const {
479
if (XGetWindowProperty(display(), window(),
480
property, long_offset, long_length, do_delete,
481
req_type, actual_type_return,
482
actual_format_return, nitems_return,
483
bytes_after_return, prop_return) == Success)
489
void FbWindow::changeProperty(Atom property, Atom type,
495
XChangeProperty(display(), m_window, property, type,
500
void FbWindow::deleteProperty(Atom property) {
501
XDeleteProperty(display(), m_window, property);
504
int FbWindow::screenNumber() const {
508
long FbWindow::eventMask() const {
509
XWindowAttributes attrib;
510
XGetWindowAttributes(display(), window(),
512
return attrib.your_event_mask;
516
void FbWindow::setOpaque(unsigned char alpha) {
518
static Atom m_alphaatom = XInternAtom(display(), "_NET_WM_WINDOW_OPACITY", False);
519
unsigned int opacity = alpha << 24;
520
changeProperty(m_alphaatom, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &opacity, 1l);
521
#endif // HAVE_XRENDER
524
void FbWindow::updateGeometry() {
529
unsigned int border_width, depth;
530
XGetGeometry(display(), m_window, &root, &m_x, &m_y,
531
(unsigned int *)&m_width, (unsigned int *)&m_height,
532
&border_width, &depth);
536
void FbWindow::create(Window parent, int x, int y,
537
unsigned int width, unsigned int height,
538
long eventmask, bool override_redirect,
539
bool save_unders, int depth, int class_type) {
544
long valmask = CWEventMask;
545
XSetWindowAttributes values;
546
values.event_mask = eventmask;
548
if (override_redirect) {
549
valmask |= CWOverrideRedirect;
550
values.override_redirect = True;
554
valmask |= CWSaveUnder;
555
values.save_under = True;
558
m_window = XCreateWindow(display(), parent, x, y, width, height,
562
CopyFromParent, // visual
563
valmask, // create mask
564
&values); // create atrribs
572
void FbWindow::sendConfigureNotify(int x, int y,
573
unsigned int width, unsigned int height) {
574
Display *disp = FbTk::App::instance()->display();
576
event.type = ConfigureNotify;
578
event.xconfigure.display = disp;
579
event.xconfigure.event = window();
580
event.xconfigure.window = window();
581
event.xconfigure.x = x;
582
event.xconfigure.y = y;
583
event.xconfigure.width = width;
584
event.xconfigure.height = height;
586
event.xconfigure.border_width = 1;
588
event.xconfigure.above = None;
589
event.xconfigure.override_redirect = false;
591
XSendEvent(disp, window(), False, StructureNotifyMask, &event);
595
bool operator == (Window win, const FbWindow &fbwin) {
596
return win == fbwin.window();