1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
10 #include "bbscreen.hh"
12 #include "otk/display.hh"
13 #include "otk/property.hh"
17 #include <X11/Xutil.h>
22 #define _(str) gettext(str)
27 OBClient::OBClient(int screen, Window window)
28 : otk::OtkEventHandler(),
29 _screen(screen), _window(window)
36 // update EVERYTHING the first time!!
38 // the state is kinda assumed to be normal. is this right? XXX
39 _wmstate = NormalState;
40 // no default decors or functions, each has to be enabled
41 _decorations = _functions = 0;
47 // set the decorations and functions
50 // normal windows retain all of the possible decorations and
52 _decorations = Decor_Titlebar | Decor_Handle | Decor_Border |
53 Decor_Iconify | Decor_Maximize;
54 _functions = Func_Resize | Func_Move | Func_Iconify | Func_Maximize;
57 // dialogs cannot be maximized
58 _decorations &= ~Decor_Maximize;
59 _functions &= ~Func_Maximize;
65 // these windows get less functionality
66 _decorations &= ~(Decor_Iconify | Decor_Handle);
67 _functions &= ~(Func_Iconify | Func_Resize);
73 // none of these windows are manipulated by the window manager
79 getMwmHints(); // this fucks (in good ways) with the decors and functions
86 // XXX: updateTransientFor();
93 printf("Mapped window: 0x%lx\n"
94 " title: \t%s\t icon title: \t%s\n"
95 " app name: \t%s\t\t class: \t%s\n"
96 " position: \t%d, %d\t\t size: \t%d, %d\n"
97 " desktop: \t%lu\t\t group: \t0x%lx\n"
98 " type: \t%d\t\t min size \t%d, %d\n"
99 " base size \t%d, %d\t\t max size \t%d, %d\n"
100 " size incr \t%d, %d\t\t gravity \t%d\n"
101 " wm state \t%ld\t\t can be focused:\t%s\n"
102 " notify focus: \t%s\t\t urgent: \t%s\n"
103 " shaped: \t%s\t\t modal: \t%s\n"
104 " shaded: \t%s\t\t iconic: \t%s\n"
105 " vert maximized:\t%s\t\t horz maximized:\t%s\n"
106 " fullscreen: \t%s\t\t floating: \t%s\n"
107 " requested pos: \t%s\n",
113 _area.x(), _area.y(),
114 _area.width(), _area.height(),
124 _can_focus ? "yes" : "no",
125 _focus_notify ? "yes" : "no",
126 _urgent ? "yes" : "no",
127 _shaped ? "yes" : "no",
128 _modal ? "yes" : "no",
129 _shaded ? "yes" : "no",
130 _iconic ? "yes" : "no",
131 _max_vert ? "yes" : "no",
132 _max_horz ? "yes" : "no",
133 _fullscreen ? "yes" : "no",
134 _floating ? "yes" : "no",
135 _positioned ? "yes" : "no");
141 OBClient::~OBClient()
143 const otk::OBProperty *property = Openbox::instance->property();
145 // these values should not be persisted across a window unmapping/mapping
146 property->erase(_window, otk::OBProperty::net_wm_desktop);
147 property->erase(_window, otk::OBProperty::net_wm_state);
151 void OBClient::getDesktop()
153 const otk::OBProperty *property = Openbox::instance->property();
155 // defaults to the current desktop
156 _desktop = 0; // XXX: change this to the current desktop!
158 property->get(_window, otk::OBProperty::net_wm_desktop,
159 otk::OBProperty::Atom_Cardinal,
164 void OBClient::getType()
166 const otk::OBProperty *property = Openbox::instance->property();
168 _type = (WindowType) -1;
171 unsigned long num = (unsigned) -1;
172 if (property->get(_window, otk::OBProperty::net_wm_window_type,
173 otk::OBProperty::Atom_Atom,
175 // use the first value that we know about in the array
176 for (unsigned long i = 0; i < num; ++i) {
178 property->atom(otk::OBProperty::net_wm_window_type_desktop))
179 _type = Type_Desktop;
181 property->atom(otk::OBProperty::net_wm_window_type_dock))
184 property->atom(otk::OBProperty::net_wm_window_type_toolbar))
185 _type = Type_Toolbar;
187 property->atom(otk::OBProperty::net_wm_window_type_menu))
190 property->atom(otk::OBProperty::net_wm_window_type_utility))
191 _type = Type_Utility;
193 property->atom(otk::OBProperty::net_wm_window_type_splash))
196 property->atom(otk::OBProperty::net_wm_window_type_dialog))
199 property->atom(otk::OBProperty::net_wm_window_type_normal))
201 // else if (val[i] ==
202 // property->atom(otk::OBProperty::kde_net_wm_window_type_override))
203 // mwm_decorations = 0; // prevent this window from getting any decor
204 // XXX: make this work again
209 if (_type == (WindowType) -1) {
211 * the window type hint was not set, which means we either classify ourself
212 * as a normal window or a dialog, depending on if we are a transient.
214 // XXX: make this code work!
216 // _type = Type_Dialog;
223 void OBClient::getMwmHints()
225 const otk::OBProperty *property = Openbox::instance->property();
230 num = MwmHints::elements;
231 if (!property->get(_window, otk::OBProperty::motif_wm_hints,
232 otk::OBProperty::motif_wm_hints, &num,
233 (unsigned long **)&hints))
236 if (num < MwmHints::elements) {
241 // retrieved the hints
242 // Mwm Hints are applied subtractively to what has already been chosen for
243 // decor and functionality
245 if (hints->flags & MwmFlag_Decorations) {
246 if (! (hints->decorations & MwmDecor_All)) {
247 if (! (hints->decorations & MwmDecor_Border))
248 _decorations &= ~Decor_Border;
249 if (! (hints->decorations & MwmDecor_Handle))
250 _decorations &= ~Decor_Handle;
251 if (! (hints->decorations & MwmDecor_Title))
252 _decorations &= ~Decor_Titlebar;
253 if (! (hints->decorations & MwmDecor_Iconify))
254 _decorations &= ~Decor_Iconify;
255 if (! (hints->decorations & MwmDecor_Maximize))
256 _decorations &= ~Decor_Maximize;
260 if (hints->flags & MwmFlag_Functions) {
261 if (! (hints->functions & MwmFunc_All)) {
262 if (! (hints->functions & MwmFunc_Resize))
263 _functions &= ~Func_Resize;
264 if (! (hints->functions & MwmFunc_Move))
265 _functions &= ~Func_Move;
266 if (! (hints->functions & MwmFunc_Iconify))
267 _functions &= ~Func_Iconify;
268 if (! (hints->functions & MwmFunc_Maximize))
269 _functions &= ~Func_Maximize;
270 //if (! (hints->functions & MwmFunc_Close))
271 // _functions &= ~Func_Close;
278 void OBClient::getArea()
280 XWindowAttributes wattrib;
283 ret = XGetWindowAttributes(otk::OBDisplay::display, _window, &wattrib);
284 assert(ret != BadWindow);
286 _area.setRect(wattrib.x, wattrib.y, wattrib.width, wattrib.height);
287 _border_width = wattrib.border_width;
291 void OBClient::getState()
293 const otk::OBProperty *property = Openbox::instance->property();
295 _modal = _shaded = _max_horz = _max_vert = _fullscreen = _floating = false;
297 unsigned long *state;
298 unsigned long num = (unsigned) -1;
300 if (property->get(_window, otk::OBProperty::net_wm_state,
301 otk::OBProperty::Atom_Atom, &num, &state)) {
302 for (unsigned long i = 0; i < num; ++i) {
303 if (state[i] == property->atom(otk::OBProperty::net_wm_state_modal))
306 property->atom(otk::OBProperty::net_wm_state_shaded))
309 property->atom(otk::OBProperty::net_wm_state_fullscreen))
312 property->atom(otk::OBProperty::net_wm_state_maximized_vert))
315 property->atom(otk::OBProperty::net_wm_state_maximized_horz))
324 void OBClient::getShaped()
328 if (otk::OBDisplay::shape()) {
333 XShapeSelectInput(otk::OBDisplay::display, _window, ShapeNotifyMask);
335 XShapeQueryExtents(otk::OBDisplay::display, _window, &s, &foo,
336 &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo, &ufoo);
343 void OBClient::updateProtocols()
345 const otk::OBProperty *property = Openbox::instance->property();
350 _focus_notify = false;
351 _decorations &= ~Decor_Close;
352 _functions &= ~Func_Close;
354 if (XGetWMProtocols(otk::OBDisplay::display, _window, &proto, &num_return)) {
355 for (int i = 0; i < num_return; ++i) {
356 if (proto[i] == property->atom(otk::OBProperty::wm_delete_window)) {
357 _decorations |= Decor_Close;
358 _functions |= Func_Close;
359 // XXX: update the decor?
360 } else if (proto[i] == property->atom(otk::OBProperty::wm_take_focus))
361 // if this protocol is requested, then the window will be notified
362 // by the window manager whenever it receives focus
363 _focus_notify = true;
370 void OBClient::updateNormalHints()
376 _gravity = NorthWestGravity;
377 _size_inc.setPoint(1, 1);
378 _base_size.setPoint(0, 0);
379 _min_size.setPoint(0, 0);
380 _max_size.setPoint(INT_MAX, INT_MAX);
382 // XXX: might want to cancel any interactive resizing of the window at this
385 // get the hints from the window
386 if (XGetWMNormalHints(otk::OBDisplay::display, _window, &size, &ret)) {
387 _positioned = (size.flags & (PPosition|USPosition));
389 if (size.flags & PWinGravity)
390 _gravity = size.win_gravity;
392 if (size.flags & PMinSize)
393 _min_size.setPoint(size.min_width, size.min_height);
395 if (size.flags & PMaxSize)
396 _max_size.setPoint(size.max_width, size.max_height);
398 if (size.flags & PBaseSize)
399 _base_size.setPoint(size.base_width, size.base_height);
401 if (size.flags & PResizeInc)
402 _size_inc.setPoint(size.width_inc, size.height_inc);
407 void OBClient::updateWMHints()
411 // assume a window takes input if it doesnt specify
415 if ((hints = XGetWMHints(otk::OBDisplay::display, _window)) != NULL) {
416 if (hints->flags & InputHint)
417 _can_focus = hints->input;
419 if (hints->flags & XUrgencyHint)
422 if (hints->flags & WindowGroupHint) {
423 if (hints->window_group != _group) {
424 // XXX: remove from the old group if there was one
425 _group = hints->window_group;
426 // XXX: do stuff with the group
436 void OBClient::updateTitle()
438 const otk::OBProperty *property = Openbox::instance->property();
443 if (! property->get(_window, otk::OBProperty::net_wm_name,
444 otk::OBProperty::utf8, &_title)) {
446 property->get(_window, otk::OBProperty::wm_name,
447 otk::OBProperty::ascii, &_title);
451 _title = _("Unnamed Window");
455 void OBClient::updateIconTitle()
457 const otk::OBProperty *property = Openbox::instance->property();
462 if (! property->get(_window, otk::OBProperty::net_wm_icon_name,
463 otk::OBProperty::utf8, &_icon_title)) {
465 property->get(_window, otk::OBProperty::wm_icon_name,
466 otk::OBProperty::ascii, &_icon_title);
470 _icon_title = _("Unnamed Window");
474 void OBClient::updateClass()
476 const otk::OBProperty *property = Openbox::instance->property();
479 _app_name = _app_class = "";
481 otk::OBProperty::StringVect v;
482 unsigned long num = 2;
484 if (! property->get(_window, otk::OBProperty::wm_class,
485 otk::OBProperty::ascii, &num, &v))
488 if (num > 0) _app_name = v[0];
489 if (num > 1) _app_class = v[1];
493 void OBClient::propertyHandler(const XPropertyEvent &e)
495 otk::OtkEventHandler::propertyHandler(e);
497 const otk::OBProperty *property = Openbox::instance->property();
499 // compress changes to a single property into a single change
501 while (XCheckTypedEvent(otk::OBDisplay::display, e.type, &ce)) {
502 // XXX: it would be nice to compress ALL changes to a property, not just
503 // changes in a row without other props between.
504 if (ce.xproperty.atom != e.atom) {
505 XPutBackEvent(otk::OBDisplay::display, &ce);
510 if (e.atom == XA_WM_NORMAL_HINTS)
512 else if (e.atom == XA_WM_HINTS)
514 else if (e.atom == property->atom(otk::OBProperty::net_wm_name) ||
515 e.atom == property->atom(otk::OBProperty::wm_name))
517 else if (e.atom == property->atom(otk::OBProperty::net_wm_icon_name) ||
518 e.atom == property->atom(otk::OBProperty::wm_icon_name))
520 else if (e.atom == property->atom(otk::OBProperty::wm_class))
522 else if (e.atom == property->atom(otk::OBProperty::wm_protocols))
524 // XXX: transient for hint
529 void OBClient::setWMState(long state)
531 if (state == _wmstate) return; // no change
535 // XXX: cause it to iconify
538 // XXX: cause it to uniconify
545 void OBClient::setDesktop(long target)
548 //assert(target == 0xffffffff || target < MAX);
550 // XXX: move the window to the new desktop
555 void OBClient::setState(StateAction action, long data1, long data2)
557 const otk::OBProperty *property = Openbox::instance->property();
559 if (!(action == State_Add || action == State_Remove ||
560 action == State_Toggle))
561 return; // an invalid action was passed to the client message, ignore it
563 for (int i = 0; i < 2; ++i) {
564 Atom state = i == 0 ? data1 : data2;
566 if (! state) continue;
568 // if toggling, then pick whether we're adding or removing
569 if (action == State_Toggle) {
570 if (state == property->atom(otk::OBProperty::net_wm_state_modal))
571 action = _modal ? State_Remove : State_Add;
573 property->atom(otk::OBProperty::net_wm_state_maximized_vert))
574 action = _max_vert ? State_Remove : State_Add;
576 property->atom(otk::OBProperty::net_wm_state_maximized_horz))
577 action = _max_horz ? State_Remove : State_Add;
578 else if (state == property->atom(otk::OBProperty::net_wm_state_shaded))
579 action = _shaded ? State_Remove : State_Add;
581 property->atom(otk::OBProperty::net_wm_state_fullscreen))
582 action = _fullscreen ? State_Remove : State_Add;
583 else if (state == property->atom(otk::OBProperty::net_wm_state_floating))
584 action = _floating ? State_Remove : State_Add;
587 if (action == State_Add) {
588 if (state == property->atom(otk::OBProperty::net_wm_state_modal)) {
589 if (_modal) continue;
591 // XXX: give it focus if another window has focus that shouldnt now
593 property->atom(otk::OBProperty::net_wm_state_maximized_vert)){
594 if (_max_vert) continue;
596 // XXX: resize the window etc
598 property->atom(otk::OBProperty::net_wm_state_maximized_horz)){
599 if (_max_horz) continue;
601 // XXX: resize the window etc
603 property->atom(otk::OBProperty::net_wm_state_shaded)) {
604 if (_shaded) continue;
606 // XXX: hide the client window
608 property->atom(otk::OBProperty::net_wm_state_fullscreen)) {
609 if (_fullscreen) continue;
611 // XXX: raise the window n shit
613 property->atom(otk::OBProperty::net_wm_state_floating)) {
614 if (_floating) continue;
616 // XXX: raise the window n shit
619 } else { // action == State_Remove
620 if (state == property->atom(otk::OBProperty::net_wm_state_modal)) {
621 if (!_modal) continue;
624 property->atom(otk::OBProperty::net_wm_state_maximized_vert)){
625 if (!_max_vert) continue;
627 // XXX: resize the window etc
629 property->atom(otk::OBProperty::net_wm_state_maximized_horz)){
630 if (!_max_horz) continue;
632 // XXX: resize the window etc
634 property->atom(otk::OBProperty::net_wm_state_shaded)) {
635 if (!_shaded) continue;
637 // XXX: show the client window
639 property->atom(otk::OBProperty::net_wm_state_fullscreen)) {
640 if (!_fullscreen) continue;
642 // XXX: lower the window to its proper layer
644 property->atom(otk::OBProperty::net_wm_state_floating)) {
645 if (!_floating) continue;
647 // XXX: lower the window to its proper layer
654 void OBClient::clientMessageHandler(const XClientMessageEvent &e)
656 otk::OtkEventHandler::clientMessageHandler(e);
658 if (e.format != 32) return;
660 const otk::OBProperty *property = Openbox::instance->property();
662 if (e.message_type == property->atom(otk::OBProperty::wm_change_state)) {
663 // compress changes into a single change
664 bool compress = false;
666 while (XCheckTypedEvent(otk::OBDisplay::display, e.type, &ce)) {
667 // XXX: it would be nice to compress ALL messages of a type, not just
668 // messages in a row without other message types between.
669 if (ce.xclient.message_type != e.message_type) {
670 XPutBackEvent(otk::OBDisplay::display, &ce);
676 setWMState(ce.xclient.data.l[0]); // use the found event
678 setWMState(e.data.l[0]); // use the original event
679 } else if (e.message_type ==
680 property->atom(otk::OBProperty::net_wm_desktop)) {
681 // compress changes into a single change
682 bool compress = false;
684 while (XCheckTypedEvent(otk::OBDisplay::display, e.type, &ce)) {
685 // XXX: it would be nice to compress ALL messages of a type, not just
686 // messages in a row without other message types between.
687 if (ce.xclient.message_type != e.message_type) {
688 XPutBackEvent(otk::OBDisplay::display, &ce);
694 setDesktop(e.data.l[0]); // use the found event
696 setDesktop(e.data.l[0]); // use the original event
698 else if (e.message_type == property->atom(otk::OBProperty::net_wm_state))
699 // can't compress these
700 setState((StateAction)e.data.l[0], e.data.l[1], e.data.l[2]);
704 #if defined(SHAPE) || defined(DOXYGEN_IGNORE)
705 void OBClient::shapeHandler(const XShapeEvent &e)
707 otk::OtkEventHandler::shapeHandler(e);
714 void OBClient::resize(Corner anchor, int w, int h)
719 // is the window resizable? if it is not, then don't check its sizes, the
720 // client can do what it wants and the user can't change it anyhow
721 if (_min_size.x() <= _max_size.x() && _min_size.y() <= _max_size.y()) {
722 // smaller than min size or bigger than max size?
723 if (w < _min_size.x()) w = _min_size.x();
724 else if (w > _max_size.x()) w = _max_size.x();
725 if (h < _min_size.y()) h = _min_size.y();
726 else if (h > _max_size.y()) h = _max_size.y();
729 // keep to the increments
733 // store the logical size
734 _logical_size.setPoint(w, h);
746 _area.setX(_area.x() - _area.width() - w);
749 _area.setY(_area.y() - _area.height() - h);
752 _area.setX(_area.x() - _area.width() - w);
753 _area.setY(_area.y() - _area.height() - h);
759 // resize the frame to match
764 void OBClient::move(int x, int y)
767 // move the frame to be in the requested position
768 frame->applyGravity();
772 void OBClient::configureRequestHandler(const XConfigureRequestEvent &e)
774 OtkEventHandler::configureRequestHandler(e);
776 // XXX: if we are iconic (or shaded? (fvwm does that)) ignore the event
778 if (e.value_mask & CWBorderWidth)
779 _border_width = e.border_width;
781 // resize, then move, as specified in the EWMH section 7.7
782 if (e.value_mask & (CWWidth | CWHeight)) {
783 int w = (e.value_mask & CWWidth) ? e.width : _area.width();
784 int h = (e.value_mask & CWHeight) ? e.height : _area.height();
788 case NorthEastGravity:
792 case SouthWestGravity:
796 case SouthEastGravity:
797 corner = BottomRight;
799 default: // NorthWest, Static, etc
803 resize(corner, w, h);
806 if (e.value_mask & (CWX | CWY)) {
807 int x = (e.value_mask & CWX) ? e.x : _area.x();
808 int y = (e.value_mask & CWY) ? e.y : _area.y();
812 if (e.value_mask & CWStackMode) {
816 // XXX: lower the window
822 // XXX: raise the window
829 void OBClient::unmapHandler(const XUnmapEvent &e)
832 printf("UnmapNotify for 0x%lx\n", e.window);
835 OtkEventHandler::unmapHandler(e);
837 // this deletes us etc
838 Openbox::instance->screen(_screen)->unmanageWindow(this);
842 void OBClient::destroyHandler(const XDestroyWindowEvent &e)
845 printf("DestroyNotify for 0x%lx\n", e.window);
848 OtkEventHandler::destroyHandler(e);
850 // this deletes us etc
851 Openbox::instance->screen(_screen)->unmanageWindow(this);