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 OBWidget(OBWidget::Type_Client),
30 frame(0), _screen(screen), _window(window)
37 // update EVERYTHING the first time!!
39 // the state is kinda assumed to be normal. is this right? XXX
40 _wmstate = NormalState;
41 // no default decors or functions, each has to be enabled
42 _decorations = _functions = 0;
48 // set the decorations and functions
51 // normal windows retain all of the possible decorations and
53 _decorations = Decor_Titlebar | Decor_Handle | Decor_Border |
54 Decor_Iconify | Decor_Maximize;
55 _functions = Func_Resize | Func_Move | Func_Iconify | Func_Maximize;
58 // dialogs cannot be maximized
59 _decorations &= ~Decor_Maximize;
60 _functions &= ~Func_Maximize;
66 // these windows get less functionality
67 _decorations &= ~(Decor_Iconify | Decor_Handle);
68 _functions &= ~(Func_Iconify | Func_Resize);
74 // none of these windows are manipulated by the window manager
80 getMwmHints(); // this fucks (in good ways) with the decors and functions
87 // XXX: updateTransientFor();
94 printf("Mapped window: 0x%lx\n"
95 " title: \t%s\t icon title: \t%s\n"
96 " app name: \t%s\t\t class: \t%s\n"
97 " position: \t%d, %d\t\t size: \t%d, %d\n"
98 " desktop: \t%lu\t\t group: \t0x%lx\n"
99 " type: \t%d\t\t min size \t%d, %d\n"
100 " base size \t%d, %d\t\t max size \t%d, %d\n"
101 " size incr \t%d, %d\t\t gravity \t%d\n"
102 " wm state \t%ld\t\t can be focused:\t%s\n"
103 " notify focus: \t%s\t\t urgent: \t%s\n"
104 " shaped: \t%s\t\t modal: \t%s\n"
105 " shaded: \t%s\t\t iconic: \t%s\n"
106 " vert maximized:\t%s\t\t horz maximized:\t%s\n"
107 " fullscreen: \t%s\t\t floating: \t%s\n"
108 " requested pos: \t%s\n",
114 _area.x(), _area.y(),
115 _area.width(), _area.height(),
125 _can_focus ? "yes" : "no",
126 _focus_notify ? "yes" : "no",
127 _urgent ? "yes" : "no",
128 _shaped ? "yes" : "no",
129 _modal ? "yes" : "no",
130 _shaded ? "yes" : "no",
131 _iconic ? "yes" : "no",
132 _max_vert ? "yes" : "no",
133 _max_horz ? "yes" : "no",
134 _fullscreen ? "yes" : "no",
135 _floating ? "yes" : "no",
136 _positioned ? "yes" : "no");
142 OBClient::~OBClient()
144 const otk::OBProperty *property = Openbox::instance->property();
146 // these values should not be persisted across a window unmapping/mapping
147 property->erase(_window, otk::OBProperty::net_wm_desktop);
148 property->erase(_window, otk::OBProperty::net_wm_state);
152 void OBClient::getDesktop()
154 const otk::OBProperty *property = Openbox::instance->property();
156 // defaults to the current desktop
157 _desktop = 0; // XXX: change this to the current desktop!
159 property->get(_window, otk::OBProperty::net_wm_desktop,
160 otk::OBProperty::Atom_Cardinal,
165 void OBClient::getType()
167 const otk::OBProperty *property = Openbox::instance->property();
169 _type = (WindowType) -1;
172 unsigned long num = (unsigned) -1;
173 if (property->get(_window, otk::OBProperty::net_wm_window_type,
174 otk::OBProperty::Atom_Atom,
176 // use the first value that we know about in the array
177 for (unsigned long i = 0; i < num; ++i) {
179 property->atom(otk::OBProperty::net_wm_window_type_desktop))
180 _type = Type_Desktop;
182 property->atom(otk::OBProperty::net_wm_window_type_dock))
185 property->atom(otk::OBProperty::net_wm_window_type_toolbar))
186 _type = Type_Toolbar;
188 property->atom(otk::OBProperty::net_wm_window_type_menu))
191 property->atom(otk::OBProperty::net_wm_window_type_utility))
192 _type = Type_Utility;
194 property->atom(otk::OBProperty::net_wm_window_type_splash))
197 property->atom(otk::OBProperty::net_wm_window_type_dialog))
200 property->atom(otk::OBProperty::net_wm_window_type_normal))
202 // else if (val[i] ==
203 // property->atom(otk::OBProperty::kde_net_wm_window_type_override))
204 // mwm_decorations = 0; // prevent this window from getting any decor
205 // XXX: make this work again
210 if (_type == (WindowType) -1) {
212 * the window type hint was not set, which means we either classify ourself
213 * as a normal window or a dialog, depending on if we are a transient.
215 // XXX: make this code work!
217 // _type = Type_Dialog;
224 void OBClient::getMwmHints()
226 const otk::OBProperty *property = Openbox::instance->property();
231 num = MwmHints::elements;
232 if (!property->get(_window, otk::OBProperty::motif_wm_hints,
233 otk::OBProperty::motif_wm_hints, &num,
234 (unsigned long **)&hints))
237 if (num < MwmHints::elements) {
242 // retrieved the hints
243 // Mwm Hints are applied subtractively to what has already been chosen for
244 // decor and functionality
246 if (hints->flags & MwmFlag_Decorations) {
247 if (! (hints->decorations & MwmDecor_All)) {
248 if (! (hints->decorations & MwmDecor_Border))
249 _decorations &= ~Decor_Border;
250 if (! (hints->decorations & MwmDecor_Handle))
251 _decorations &= ~Decor_Handle;
252 if (! (hints->decorations & MwmDecor_Title))
253 _decorations &= ~Decor_Titlebar;
254 if (! (hints->decorations & MwmDecor_Iconify))
255 _decorations &= ~Decor_Iconify;
256 if (! (hints->decorations & MwmDecor_Maximize))
257 _decorations &= ~Decor_Maximize;
261 if (hints->flags & MwmFlag_Functions) {
262 if (! (hints->functions & MwmFunc_All)) {
263 if (! (hints->functions & MwmFunc_Resize))
264 _functions &= ~Func_Resize;
265 if (! (hints->functions & MwmFunc_Move))
266 _functions &= ~Func_Move;
267 if (! (hints->functions & MwmFunc_Iconify))
268 _functions &= ~Func_Iconify;
269 if (! (hints->functions & MwmFunc_Maximize))
270 _functions &= ~Func_Maximize;
271 //if (! (hints->functions & MwmFunc_Close))
272 // _functions &= ~Func_Close;
279 void OBClient::getArea()
281 XWindowAttributes wattrib;
284 ret = XGetWindowAttributes(otk::OBDisplay::display, _window, &wattrib);
285 assert(ret != BadWindow);
287 _area.setRect(wattrib.x, wattrib.y, wattrib.width, wattrib.height);
288 _border_width = wattrib.border_width;
292 void OBClient::getState()
294 const otk::OBProperty *property = Openbox::instance->property();
296 _modal = _shaded = _max_horz = _max_vert = _fullscreen = _floating = false;
298 unsigned long *state;
299 unsigned long num = (unsigned) -1;
301 if (property->get(_window, otk::OBProperty::net_wm_state,
302 otk::OBProperty::Atom_Atom, &num, &state)) {
303 for (unsigned long i = 0; i < num; ++i) {
304 if (state[i] == property->atom(otk::OBProperty::net_wm_state_modal))
307 property->atom(otk::OBProperty::net_wm_state_shaded))
310 property->atom(otk::OBProperty::net_wm_state_fullscreen))
313 property->atom(otk::OBProperty::net_wm_state_maximized_vert))
316 property->atom(otk::OBProperty::net_wm_state_maximized_horz))
325 void OBClient::getShaped()
329 if (otk::OBDisplay::shape()) {
334 XShapeSelectInput(otk::OBDisplay::display, _window, ShapeNotifyMask);
336 XShapeQueryExtents(otk::OBDisplay::display, _window, &s, &foo,
337 &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo, &ufoo);
344 void OBClient::updateProtocols()
346 const otk::OBProperty *property = Openbox::instance->property();
351 _focus_notify = false;
352 _decorations &= ~Decor_Close;
353 _functions &= ~Func_Close;
355 if (XGetWMProtocols(otk::OBDisplay::display, _window, &proto, &num_return)) {
356 for (int i = 0; i < num_return; ++i) {
357 if (proto[i] == property->atom(otk::OBProperty::wm_delete_window)) {
358 _decorations |= Decor_Close;
359 _functions |= Func_Close;
360 // XXX: update the decor?
361 } else if (proto[i] == property->atom(otk::OBProperty::wm_take_focus))
362 // if this protocol is requested, then the window will be notified
363 // by the window manager whenever it receives focus
364 _focus_notify = true;
371 void OBClient::updateNormalHints()
375 int oldgravity = _gravity;
378 _gravity = NorthWestGravity;
379 _size_inc.setPoint(1, 1);
380 _base_size.setPoint(0, 0);
381 _min_size.setPoint(0, 0);
382 _max_size.setPoint(INT_MAX, INT_MAX);
384 // XXX: might want to cancel any interactive resizing of the window at this
387 // get the hints from the window
388 if (XGetWMNormalHints(otk::OBDisplay::display, _window, &size, &ret)) {
389 _positioned = (size.flags & (PPosition|USPosition));
391 if (size.flags & PWinGravity)
392 _gravity = size.win_gravity;
394 if (size.flags & PMinSize)
395 _min_size.setPoint(size.min_width, size.min_height);
397 if (size.flags & PMaxSize)
398 _max_size.setPoint(size.max_width, size.max_height);
400 if (size.flags & PBaseSize)
401 _base_size.setPoint(size.base_width, size.base_height);
403 if (size.flags & PResizeInc)
404 _size_inc.setPoint(size.width_inc, size.height_inc);
407 // if the client has a frame, i.e. has already been mapped and is
408 // changing its gravity
409 if (frame && _gravity != oldgravity) {
410 // move our idea of the client's position based on its new gravity
412 frame->frameGravity(x, y);
418 void OBClient::updateWMHints()
422 // assume a window takes input if it doesnt specify
426 if ((hints = XGetWMHints(otk::OBDisplay::display, _window)) != NULL) {
427 if (hints->flags & InputHint)
428 _can_focus = hints->input;
430 if (hints->flags & XUrgencyHint)
433 if (hints->flags & WindowGroupHint) {
434 if (hints->window_group != _group) {
435 // XXX: remove from the old group if there was one
436 _group = hints->window_group;
437 // XXX: do stuff with the group
447 void OBClient::updateTitle()
449 const otk::OBProperty *property = Openbox::instance->property();
454 if (! property->get(_window, otk::OBProperty::net_wm_name,
455 otk::OBProperty::utf8, &_title)) {
457 property->get(_window, otk::OBProperty::wm_name,
458 otk::OBProperty::ascii, &_title);
462 _title = _("Unnamed Window");
466 void OBClient::updateIconTitle()
468 const otk::OBProperty *property = Openbox::instance->property();
473 if (! property->get(_window, otk::OBProperty::net_wm_icon_name,
474 otk::OBProperty::utf8, &_icon_title)) {
476 property->get(_window, otk::OBProperty::wm_icon_name,
477 otk::OBProperty::ascii, &_icon_title);
481 _icon_title = _("Unnamed Window");
485 void OBClient::updateClass()
487 const otk::OBProperty *property = Openbox::instance->property();
490 _app_name = _app_class = "";
492 otk::OBProperty::StringVect v;
493 unsigned long num = 2;
495 if (! property->get(_window, otk::OBProperty::wm_class,
496 otk::OBProperty::ascii, &num, &v))
499 if (num > 0) _app_name = v[0];
500 if (num > 1) _app_class = v[1];
504 void OBClient::propertyHandler(const XPropertyEvent &e)
506 otk::OtkEventHandler::propertyHandler(e);
508 const otk::OBProperty *property = Openbox::instance->property();
510 // compress changes to a single property into a single change
512 while (XCheckTypedEvent(otk::OBDisplay::display, e.type, &ce)) {
513 // XXX: it would be nice to compress ALL changes to a property, not just
514 // changes in a row without other props between.
515 if (ce.xproperty.atom != e.atom) {
516 XPutBackEvent(otk::OBDisplay::display, &ce);
521 if (e.atom == XA_WM_NORMAL_HINTS)
523 else if (e.atom == XA_WM_HINTS)
525 else if (e.atom == property->atom(otk::OBProperty::net_wm_name) ||
526 e.atom == property->atom(otk::OBProperty::wm_name))
528 else if (e.atom == property->atom(otk::OBProperty::net_wm_icon_name) ||
529 e.atom == property->atom(otk::OBProperty::wm_icon_name))
531 else if (e.atom == property->atom(otk::OBProperty::wm_class))
533 else if (e.atom == property->atom(otk::OBProperty::wm_protocols))
535 // XXX: transient for hint
540 void OBClient::setWMState(long state)
542 if (state == _wmstate) return; // no change
546 // XXX: cause it to iconify
549 // XXX: cause it to uniconify
556 void OBClient::setDesktop(long target)
559 //assert(target == 0xffffffff || target < MAX);
561 // XXX: move the window to the new desktop
566 void OBClient::setState(StateAction action, long data1, long data2)
568 const otk::OBProperty *property = Openbox::instance->property();
570 if (!(action == State_Add || action == State_Remove ||
571 action == State_Toggle))
572 return; // an invalid action was passed to the client message, ignore it
574 for (int i = 0; i < 2; ++i) {
575 Atom state = i == 0 ? data1 : data2;
577 if (! state) continue;
579 // if toggling, then pick whether we're adding or removing
580 if (action == State_Toggle) {
581 if (state == property->atom(otk::OBProperty::net_wm_state_modal))
582 action = _modal ? State_Remove : State_Add;
584 property->atom(otk::OBProperty::net_wm_state_maximized_vert))
585 action = _max_vert ? State_Remove : State_Add;
587 property->atom(otk::OBProperty::net_wm_state_maximized_horz))
588 action = _max_horz ? State_Remove : State_Add;
589 else if (state == property->atom(otk::OBProperty::net_wm_state_shaded))
590 action = _shaded ? State_Remove : State_Add;
592 property->atom(otk::OBProperty::net_wm_state_fullscreen))
593 action = _fullscreen ? State_Remove : State_Add;
594 else if (state == property->atom(otk::OBProperty::net_wm_state_floating))
595 action = _floating ? State_Remove : State_Add;
598 if (action == State_Add) {
599 if (state == property->atom(otk::OBProperty::net_wm_state_modal)) {
600 if (_modal) continue;
602 // XXX: give it focus if another window has focus that shouldnt now
604 property->atom(otk::OBProperty::net_wm_state_maximized_vert)){
605 if (_max_vert) continue;
607 // XXX: resize the window etc
609 property->atom(otk::OBProperty::net_wm_state_maximized_horz)){
610 if (_max_horz) continue;
612 // XXX: resize the window etc
614 property->atom(otk::OBProperty::net_wm_state_shaded)) {
615 if (_shaded) continue;
617 // XXX: hide the client window
619 property->atom(otk::OBProperty::net_wm_state_fullscreen)) {
620 if (_fullscreen) continue;
622 // XXX: raise the window n shit
624 property->atom(otk::OBProperty::net_wm_state_floating)) {
625 if (_floating) continue;
627 // XXX: raise the window n shit
630 } else { // action == State_Remove
631 if (state == property->atom(otk::OBProperty::net_wm_state_modal)) {
632 if (!_modal) continue;
635 property->atom(otk::OBProperty::net_wm_state_maximized_vert)){
636 if (!_max_vert) continue;
638 // XXX: resize the window etc
640 property->atom(otk::OBProperty::net_wm_state_maximized_horz)){
641 if (!_max_horz) continue;
643 // XXX: resize the window etc
645 property->atom(otk::OBProperty::net_wm_state_shaded)) {
646 if (!_shaded) continue;
648 // XXX: show the client window
650 property->atom(otk::OBProperty::net_wm_state_fullscreen)) {
651 if (!_fullscreen) continue;
653 // XXX: lower the window to its proper layer
655 property->atom(otk::OBProperty::net_wm_state_floating)) {
656 if (!_floating) continue;
658 // XXX: lower the window to its proper layer
665 void OBClient::toggleClientBorder(bool addborder)
667 // adjust our idea of where the client is, based on its border. When the
668 // border is removed, the client should now be considered to be in a
669 // different position.
670 // when re-adding the border to the client, the same operation needs to be
672 int x = _area.x(), y = _area.y();
674 case NorthWestGravity:
676 case SouthWestGravity:
677 if (addborder) x += _border_width;
678 else x -= _border_width;
680 case NorthEastGravity:
682 case SouthEastGravity:
683 if (addborder) x -= _border_width * 2;
684 else x += _border_width * 2;
688 case NorthWestGravity:
690 case NorthEastGravity:
691 if (addborder) y += _border_width;
692 else y -= _border_width;
694 case SouthWestGravity:
696 case SouthEastGravity:
697 if (addborder) y -= _border_width * 2;
698 else y += _border_width * 2;
701 // no change for StaticGravity etc.
707 XSetWindowBorderWidth(otk::OBDisplay::display, _window, _border_width);
709 // move the client so it is back it the right spot _with_ its border!
710 XMoveWindow(otk::OBDisplay::display, _window, x, y);
712 XSetWindowBorderWidth(otk::OBDisplay::display, _window, 0);
716 void OBClient::clientMessageHandler(const XClientMessageEvent &e)
718 otk::OtkEventHandler::clientMessageHandler(e);
720 if (e.format != 32) return;
722 const otk::OBProperty *property = Openbox::instance->property();
724 if (e.message_type == property->atom(otk::OBProperty::wm_change_state)) {
725 // compress changes into a single change
726 bool compress = false;
728 while (XCheckTypedEvent(otk::OBDisplay::display, e.type, &ce)) {
729 // XXX: it would be nice to compress ALL messages of a type, not just
730 // messages in a row without other message types between.
731 if (ce.xclient.message_type != e.message_type) {
732 XPutBackEvent(otk::OBDisplay::display, &ce);
738 setWMState(ce.xclient.data.l[0]); // use the found event
740 setWMState(e.data.l[0]); // use the original event
741 } else if (e.message_type ==
742 property->atom(otk::OBProperty::net_wm_desktop)) {
743 // compress changes into a single change
744 bool compress = false;
746 while (XCheckTypedEvent(otk::OBDisplay::display, e.type, &ce)) {
747 // XXX: it would be nice to compress ALL messages of a type, not just
748 // messages in a row without other message types between.
749 if (ce.xclient.message_type != e.message_type) {
750 XPutBackEvent(otk::OBDisplay::display, &ce);
756 setDesktop(e.data.l[0]); // use the found event
758 setDesktop(e.data.l[0]); // use the original event
760 else if (e.message_type == property->atom(otk::OBProperty::net_wm_state))
761 // can't compress these
762 setState((StateAction)e.data.l[0], e.data.l[1], e.data.l[2]);
766 #if defined(SHAPE) || defined(DOXYGEN_IGNORE)
767 void OBClient::shapeHandler(const XShapeEvent &e)
769 otk::OtkEventHandler::shapeHandler(e);
776 void OBClient::resize(Corner anchor, int w, int h)
781 // is the window resizable? if it is not, then don't check its sizes, the
782 // client can do what it wants and the user can't change it anyhow
783 if (_min_size.x() <= _max_size.x() && _min_size.y() <= _max_size.y()) {
784 // smaller than min size or bigger than max size?
785 if (w < _min_size.x()) w = _min_size.x();
786 else if (w > _max_size.x()) w = _max_size.x();
787 if (h < _min_size.y()) h = _min_size.y();
788 else if (h > _max_size.y()) h = _max_size.y();
791 // keep to the increments
795 // store the logical size
796 _logical_size.setPoint(w, h);
804 int x = _area.x(), y = _area.y();
809 x -= w - _area.width();
812 y -= h - _area.height();
815 x -= w - _area.width();
816 y -= h - _area.height();
821 XResizeWindow(otk::OBDisplay::display, _window, w, h);
823 // resize the frame to match the request
829 void OBClient::move(int x, int y)
832 // move the frame to be in the requested position
833 frame->adjustPosition();
837 void OBClient::configureRequestHandler(const XConfigureRequestEvent &e)
839 OtkEventHandler::configureRequestHandler(e);
841 // XXX: if we are iconic (or shaded? (fvwm does that)) ignore the event
843 if (e.value_mask & CWBorderWidth)
844 _border_width = e.border_width;
846 // resize, then move, as specified in the EWMH section 7.7
847 if (e.value_mask & (CWWidth | CWHeight)) {
848 int w = (e.value_mask & CWWidth) ? e.width : _area.width();
849 int h = (e.value_mask & CWHeight) ? e.height : _area.height();
853 case NorthEastGravity:
857 case SouthWestGravity:
861 case SouthEastGravity:
862 corner = BottomRight;
864 default: // NorthWest, Static, etc
868 resize(corner, w, h);
871 if (e.value_mask & (CWX | CWY)) {
872 int x = (e.value_mask & CWX) ? e.x : _area.x();
873 int y = (e.value_mask & CWY) ? e.y : _area.y();
877 if (e.value_mask & CWStackMode) {
881 // XXX: lower the window
887 // XXX: raise the window
894 void OBClient::unmapHandler(const XUnmapEvent &e)
897 printf("UnmapNotify for 0x%lx\n", e.window);
905 OtkEventHandler::unmapHandler(e);
907 // this deletes us etc
908 Openbox::instance->screen(_screen)->unmanageWindow(this);
912 void OBClient::destroyHandler(const XDestroyWindowEvent &e)
915 printf("DestroyNotify for 0x%lx\n", e.window);
918 OtkEventHandler::destroyHandler(e);
920 // this deletes us etc
921 Openbox::instance->screen(_screen)->unmanageWindow(this);