1 // FbWindow.cc for FbTk - fluxbox toolkit
2 // Copyright (c) 2002 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
3 //
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:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
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.
21
22 // $Id: FbWindow.cc 4199 2006-02-16 06:53:05Z mathias $
23
24 #include "FbWindow.hh"
25 #include "FbPixmap.hh"
26
27 #include "EventManager.hh"
28 #include "Color.hh"
29 #include "App.hh"
30 #include "Transparent.hh"
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif // HAVE_CONFIG_H
35
36 #include <X11/Xutil.h>
37 #include <X11/Xatom.h>
38
39 #ifdef HAVE_CASSERT
40 #include <cassert>
41 #else
42 #include <assert.h>
43 #endif
44
45 namespace FbTk {
46
FbWindow()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) {
50
51 }
52
FbWindow(const FbWindow & the_copy)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;
63 }
64
FbWindow(int screen_num,int x,int y,unsigned int width,unsigned int height,long eventmask,bool override_redirect,bool save_unders,int depth,int class_type)65 FbWindow::FbWindow(int screen_num,
66 int x, int y,
67 unsigned int width, unsigned int height,
68 long eventmask,
69 bool override_redirect,
70 bool save_unders,
71 int depth,
72 int class_type):
73 FbDrawable(),
74 m_parent(0),
75 m_screen_num(screen_num),
76 m_destroy(true),
77 m_lastbg_color_set(false),
78 m_lastbg_color(0),
79 m_lastbg_pm(0), m_renderer(0) {
80
81 create(RootWindow(display(), screen_num),
82 x, y, width, height, eventmask,
83 override_redirect, save_unders, depth, class_type);
84 };
85
FbWindow(const FbWindow & parent,int x,int y,unsigned int width,unsigned int height,long eventmask,bool override_redirect,bool save_unders,int depth,int class_type)86 FbWindow::FbWindow(const FbWindow &parent,
87 int x, int y, unsigned int width, unsigned int height,
88 long eventmask,
89 bool override_redirect,
90 bool save_unders,
91 int depth, int class_type):
92 m_parent(&parent),
93 m_screen_num(parent.screenNumber()),
94 m_destroy(true),
95 m_lastbg_color_set(false), m_lastbg_color(0),
96 m_lastbg_pm(0), m_renderer(0) {
97
98 create(parent.window(), x, y, width, height, eventmask,
99 override_redirect, save_unders, depth, class_type);
100
101
102 };
103
FbWindow(Window client)104 FbWindow::FbWindow(Window client):FbDrawable(), m_parent(0),
105 m_screen_num(0),
106 m_window(0),
107 m_x(0), m_y(0),
108 m_width(1), m_height(1),
109 m_border_width(0),
110 m_depth(0),
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) {
114
115 setNew(client);
116 }
117
~FbWindow()118 FbWindow::~FbWindow() {
119
120 // Need to free xrender pics before destroying window
121 if (m_transparent.get() != 0)
122 m_transparent.reset(0);
123
124 if (m_window != 0) {
125 // so we don't get any dangling eventhandler for this window
126 FbTk::EventManager::instance()->remove(m_window);
127 if (m_destroy)
128 XDestroyWindow(display(), m_window);
129 }
130 }
131
setBackgroundColor(const FbTk::Color & bg_color)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;
136 m_lastbg_pm = None;
137 } else {
138 m_lastbg_color_set = false;
139 }
140
141 updateBackground(false);
142 }
143
setBackgroundPixmap(Pixmap bg_pixmap)144 void FbWindow::setBackgroundPixmap(Pixmap bg_pixmap) {
145 m_lastbg_pm = bg_pixmap;
146 if (bg_pixmap != None)
147 m_lastbg_color_set = false;
148
149 updateBackground(false);
150 }
151
updateBackground(bool only_if_alpha)152 void FbWindow::updateBackground(bool only_if_alpha) {
153 Pixmap newbg = m_lastbg_pm;
154 unsigned char alpha = 255;
155 bool free_newbg = false;
156
157 if (m_lastbg_pm == None && !m_lastbg_color_set)
158 return;
159
160 if (m_transparent.get() != 0)
161 alpha = m_transparent->alpha();
162
163 if (only_if_alpha && alpha == 255)
164 return;
165
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());
173
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);
177
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());
181 } else {
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());
184 }
185 XFreeGC(display(), gc);
186
187 if (alpha != 255)
188 m_transparent->setDest(newpm.drawable(), screenNumber());
189
190 // get root position
191
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();
202 }
203 }
204
205 // render background image from root pos to our window
206 if (alpha != 255)
207 m_transparent->render(root_x, root_y,
208 0, 0,
209 width(), height());
210
211 // render any foreground items
212 if (m_renderer)
213 m_renderer->renderForeground(*this, newpm);
214
215 if (alpha != 255)
216 m_transparent->freeDest(); // it's only temporary, don't leave it hanging around
217 newbg = newpm.release();
218 }
219
220 if (newbg != None)
221 XSetWindowBackgroundPixmap(display(), m_window, newbg);
222 else if (m_lastbg_color_set)
223 XSetWindowBackground(display(), m_window, m_lastbg_color);
224
225 if (free_newbg)
226 XFreePixmap(display(), newbg);
227 }
228
setBorderColor(const FbTk::Color & border_color)229 void FbWindow::setBorderColor(const FbTk::Color &border_color) {
230 XSetWindowBorder(display(), m_window, border_color.pixel());
231 }
232
setBorderWidth(unsigned int size)233 void FbWindow::setBorderWidth(unsigned int size) {
234 XSetWindowBorderWidth(display(), m_window, size);
235 m_border_width = size;
236 }
237
setName(const char * name)238 void FbWindow::setName(const char *name) {
239 XStoreName(display(), m_window, name);
240 }
241
setEventMask(long mask)242 void FbWindow::setEventMask(long mask) {
243 XSelectInput(display(), m_window, mask);
244 }
245
clear()246 void FbWindow::clear() {
247 XClearWindow(display(), m_window);
248 }
249
clearArea(int x,int y,unsigned int width,unsigned int height,bool exposures)250 void FbWindow::clearArea(int x, int y,
251 unsigned int width, unsigned int height,
252 bool exposures) {
253 // TODO: probably could call renderForeground here (with x,y,w,h)
254 XClearArea(display(), window(), x, y, width, height, exposures);
255 }
256
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.
259
updateTransparent(int the_x,int the_y,unsigned int the_width,unsigned int the_height,Pixmap dest_override,bool override_is_offset)260 void FbWindow::updateTransparent(int the_x, int the_y, unsigned int the_width,
261 unsigned int the_height, Pixmap dest_override, bool override_is_offset) {
262 #ifdef HAVE_XRENDER
263 if (!m_transparent.get())
264 return;
265
266 if (width() == 0 || height() == 0)
267 return;
268
269 if (!dest_override &&
270 (the_width == 0 && the_height == 0 ||
271 the_width == width() && the_height == height()) &&
272 the_x <= 0 && the_y <= 0) {
273 // do the whole thing
274 updateBackground(true);
275 return;
276 }
277
278 if (!dest_override)
279 dest_override = window();
280
281 if (the_width == 0 || the_height == 0) {
282 the_width = width();
283 the_height = height();
284 }
285
286 if (the_x < 0 || the_y < 0) {
287 the_x = 0;
288 the_y = 0;
289 }
290
291 // update source and destination if needed
292 Pixmap root = FbPixmap::getRootPixmap(screenNumber());
293 if (m_transparent->source() != root)
294 m_transparent->setSource(root, screenNumber());
295
296 if (m_transparent->dest() != dest_override)
297 m_transparent->setDest(dest_override, screenNumber());
298
299 // get root position
300
301 const FbWindow *root_parent = parent();
302 // our position in parent ("root")
303 int root_x = x() + borderWidth(), root_y = y() + borderWidth();
304 if (root_parent != 0) {
305 root_x += root_parent->x() + root_parent->borderWidth();
306 root_y += root_parent->y() + root_parent->borderWidth();
307 while (root_parent->parent() != 0) {
308 root_parent = root_parent->parent();
309 root_x += root_parent->x() + root_parent->borderWidth();
310 root_y += root_parent->y() + root_parent->borderWidth();
311 }
312
313 } // else toplevel window so we already have x, y set
314
315 // render background image from root pos to our window
316 m_transparent->render(root_x + the_x, root_y + the_y,
317 override_is_offset?0:the_x, override_is_offset?0:the_y,
318 the_width, the_height);
319 #endif // HAVE_XRENDER
320 }
321
setAlpha(unsigned char alpha)322 void FbWindow::setAlpha(unsigned char alpha) {
323 #ifdef HAVE_XRENDER
324 if (FbTk::Transparent::haveComposite()) {
325 if (m_transparent.get() != 0)
326 m_transparent.reset(0);
327
328 // don't setOpaque, let controlling objects do that
329 // since it's only needed on toplevel windows
330 } else {
331 if (!FbTk::Transparent::haveRender())
332 alpha = 255;
333
334 if (m_transparent.get() == 0 && alpha < 255) {
335 m_transparent.reset(new Transparent(FbPixmap::getRootPixmap(screenNumber()),
336 window(),
337 alpha, screenNumber()));
338 } else if (alpha < 255 && alpha != m_transparent->alpha())
339 m_transparent->setAlpha(alpha);
340 else if (alpha == 255)
341 m_transparent.reset(0); // destroy transparent object
342 }
343 #endif // HAVE_XRENDER
344 }
345
alpha() const346 unsigned char FbWindow::alpha() const {
347 #ifdef HAVE_XRENDER
348 if (m_transparent.get())
349 return m_transparent->alpha();
350 #endif // HAVE_XRENDER
351 return 255;
352 }
353
operator =(const FbWindow & win)354 FbWindow &FbWindow::operator = (const FbWindow &win) {
355 m_parent = win.parent();
356 m_screen_num = win.screenNumber();
357 m_window = win.window();
358 m_x = win.x();
359 m_y = win.y();
360 m_width = win.width();
361 m_height = win.height();
362 m_border_width = win.borderWidth();
363 m_depth = win.depth();
364 // take over this window
365 win.m_window = 0;
366 return *this;
367 }
368
operator =(Window win)369 FbWindow &FbWindow::operator = (Window win) {
370 setNew(win);
371 return *this;
372 }
373
setNew(Window win)374 void FbWindow::setNew(Window win) {
375
376 if (m_window != 0 && m_destroy)
377 XDestroyWindow(display(), m_window);
378
379 m_window = win;
380
381 if (m_window != 0) {
382 updateGeometry();
383 XWindowAttributes attr;
384 attr.screen = 0;
385 //get screen number
386 if (XGetWindowAttributes(display(),
387 m_window,
388 &attr) != 0 && attr.screen != 0) {
389 m_screen_num = XScreenNumberOfScreen(attr.screen);
390 if (attr.width <= 0)
391 m_width = 1;
392 else
393 m_width = attr.width;
394
395 if (attr.height <= 0)
396 m_height = 1;
397 else
398 m_height = attr.height;
399
400 m_x = attr.x;
401 m_y = attr.y;
402 m_depth = attr.depth;
403 m_border_width = attr.border_width;
404 }
405
406 }
407 }
408
show()409 void FbWindow::show() {
410 XMapWindow(display(), m_window);
411 }
412
showSubwindows()413 void FbWindow::showSubwindows() {
414 XMapSubwindows(display(), m_window);
415 }
416
hide()417 void FbWindow::hide() {
418 XUnmapWindow(display(), m_window);
419 }
420
lower()421 void FbWindow::lower() {
422 XLowerWindow(display(), window());
423 }
424
raise()425 void FbWindow::raise() {
426 XRaiseWindow(display(), window());
427 }
428
setInputFocus(int revert_to,int time)429 void FbWindow::setInputFocus(int revert_to, int time) {
430 XSetInputFocus(display(), window(), revert_to, time);
431 }
432
setCursor(Cursor cur)433 void FbWindow::setCursor(Cursor cur) {
434 XDefineCursor(display(), window(), cur);
435 }
436
unsetCursor()437 void FbWindow::unsetCursor() {
438 XUndefineCursor(display(), window());
439 }
440
reparent(const FbWindow & parent,int x,int y,bool continuing)441 void FbWindow::reparent(const FbWindow &parent, int x, int y, bool continuing) {
442 XReparentWindow(display(), window(), parent.window(), x, y);
443 m_parent = &parent;
444 if (continuing) // we will continue managing this window after reparent
445 updateGeometry();
446 }
447
textProperty(Atom property) const448 std::string FbWindow::textProperty(Atom property) const {
449 XTextProperty text_prop;
450 char ** stringlist;
451 int count;
452 std::string ret;
453
454 if (XGetTextProperty(display(), window(), &text_prop, property) == 0)
455 return "";
456
457 if (text_prop.value == 0 || text_prop.nitems == 0)
458 return "";
459
460 if (text_prop.encoding != XA_STRING) {
461 // still returns a "StringList" despite the different name
462 if (XmbTextPropertyToTextList(display(), &text_prop, &stringlist, &count) == 0 || count == 0)
463 return "";
464 } else {
465 if (XTextPropertyToStringList(&text_prop, &stringlist, &count) == 0 || count == 0)
466 return "";
467
468 }
469
470 ret = stringlist[0];
471 XFreeStringList(stringlist);
472 return ret;
473 }
474
property(Atom property,long long_offset,long long_length,bool do_delete,Atom req_type,Atom * actual_type_return,int * actual_format_return,unsigned long * nitems_return,unsigned long * bytes_after_return,unsigned char ** prop_return) const475 bool FbWindow::property(Atom property,
476 long long_offset, long long_length,
477 bool do_delete,
478 Atom req_type,
479 Atom *actual_type_return,
480 int *actual_format_return,
481 unsigned long *nitems_return,
482 unsigned long *bytes_after_return,
483 unsigned char **prop_return) const {
484 if (XGetWindowProperty(display(), window(),
485 property, long_offset, long_length, do_delete,
486 req_type, actual_type_return,
487 actual_format_return, nitems_return,
488 bytes_after_return, prop_return) == Success)
489 return true;
490
491 return false;
492 }
493
changeProperty(Atom property,Atom type,int format,int mode,unsigned char * data,int nelements)494 void FbWindow::changeProperty(Atom property, Atom type,
495 int format,
496 int mode,
497 unsigned char *data,
498 int nelements) {
499
500 XChangeProperty(display(), m_window, property, type,
501 format, mode,
502 data, nelements);
503 }
504
deleteProperty(Atom property)505 void FbWindow::deleteProperty(Atom property) {
506 XDeleteProperty(display(), m_window, property);
507 }
508
screenNumber() const509 int FbWindow::screenNumber() const {
510 return m_screen_num;
511 }
512
eventMask() const513 long FbWindow::eventMask() const {
514 XWindowAttributes attrib;
515 XGetWindowAttributes(display(), window(),
516 &attrib);
517 return attrib.your_event_mask;
518
519 }
520
setOpaque(unsigned char alpha)521 void FbWindow::setOpaque(unsigned char alpha) {
522 #ifdef HAVE_XRENDER
523 static Atom m_alphaatom = XInternAtom(display(), "_NET_WM_WINDOW_OPACITY", False);
524 unsigned int opacity = alpha << 24;
525 changeProperty(m_alphaatom, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &opacity, 1l);
526 #endif // HAVE_XRENDER
527 }
528
updateGeometry()529 void FbWindow::updateGeometry() {
530 if (m_window == 0)
531 return;
532
533 Window root;
534 unsigned int border_width, depth;
535 XGetGeometry(display(), m_window, &root, &m_x, &m_y,
536 (unsigned int *)&m_width, (unsigned int *)&m_height,
537 &border_width, &depth);
538 m_depth = depth;
539 }
540
create(Window parent,int x,int y,unsigned int width,unsigned int height,long eventmask,bool override_redirect,bool save_unders,int depth,int class_type)541 void FbWindow::create(Window parent, int x, int y,
542 unsigned int width, unsigned int height,
543 long eventmask, bool override_redirect,
544 bool save_unders, int depth, int class_type) {
545
546
547 m_border_width = 0;
548
549 long valmask = CWEventMask;
550 XSetWindowAttributes values;
551 values.event_mask = eventmask;
552
553 if (override_redirect) {
554 valmask |= CWOverrideRedirect;
555 values.override_redirect = True;
556 }
557
558 if (save_unders) {
559 valmask |= CWSaveUnder;
560 values.save_under = True;
561 }
562
563 m_window = XCreateWindow(display(), parent, x, y, width, height,
564 0, // border width
565 depth, // depth
566 class_type, // class
567 CopyFromParent, // visual
568 valmask, // create mask
569 &values); // create atrribs
570
571 assert(m_window);
572
573 updateGeometry();
574 }
575
576
sendConfigureNotify(int x,int y,unsigned int width,unsigned int height)577 void FbWindow::sendConfigureNotify(int x, int y,
578 unsigned int width, unsigned int height) {
579 Display *disp = FbTk::App::instance()->display();
580 XEvent event;
581 event.type = ConfigureNotify;
582
583 event.xconfigure.display = disp;
584 event.xconfigure.event = window();
585 event.xconfigure.window = window();
586 event.xconfigure.x = x;
587 event.xconfigure.y = y;
588 event.xconfigure.width = width;
589 event.xconfigure.height = height;
590 //!! TODO
591 event.xconfigure.border_width = 1;
592 //!! TODO
593 event.xconfigure.above = None;
594 event.xconfigure.override_redirect = false;
595
596 XSendEvent(disp, window(), False, StructureNotifyMask, &event);
597
598 }
599
operator ==(Window win,const FbWindow & fbwin)600 bool operator == (Window win, const FbWindow &fbwin) {
601 return win == fbwin.window();
602 }
603
604 };
605