+ Atom actions[9];
+ int num = 0;
+
+ actions[num++] = otk::Property::atoms.net_wm_action_change_desktop;
+
+ if (_functions & Func_Shade)
+ actions[num++] = otk::Property::atoms.net_wm_action_shade;
+ if (_functions & Func_Close)
+ actions[num++] = otk::Property::atoms.net_wm_action_close;
+ if (_functions & Func_Move)
+ actions[num++] = otk::Property::atoms.net_wm_action_move;
+ if (_functions & Func_Iconify)
+ actions[num++] = otk::Property::atoms.net_wm_action_minimize;
+ if (_functions & Func_Resize)
+ actions[num++] = otk::Property::atoms.net_wm_action_resize;
+ if (_functions & Func_Fullscreen)
+ actions[num++] = otk::Property::atoms.net_wm_action_fullscreen;
+ if (_functions & Func_Maximize) {
+ actions[num++] = otk::Property::atoms.net_wm_action_maximize_horz;
+ actions[num++] = otk::Property::atoms.net_wm_action_maximize_vert;
+ }
+
+ otk::Property::set(_window, otk::Property::atoms.net_wm_allowed_actions,
+ otk::Property::atoms.atom, actions, num);
+
+ // make sure the window isn't breaking any rules now
+
+ if (!(_functions & Func_Shade) && _shaded)
+ if (frame) shade(false);
+ else _shaded = false;
+ if (!(_functions & Func_Iconify) && _iconic)
+ if (frame) setDesktop(openbox->screen(_screen)->desktop());
+ else _iconic = false;
+ if (!(_functions & Func_Fullscreen) && _fullscreen)
+ if (frame) fullscreen(false);
+ else _fullscreen = false;
+ if (!(_functions & Func_Maximize) && (_max_horz || _max_vert))
+ if (frame) maximize(false, 0);
+ else _max_vert = _max_horz = false;
+}
+
+void Client::remaximize()
+{
+ int dir;
+ if (_max_horz && _max_vert)
+ dir = 0;
+ else if (_max_horz)
+ dir = 1;
+ else if (_max_vert)
+ dir = 2;
+ else
+ return; // not maximized
+ _max_horz = _max_vert = false;
+ maximize(true, dir, false);
+}
+
+void Client::applyStartupState()
+{
+ // these are in a carefully crafted order..
+
+ if (_iconic) {
+ _iconic = false;
+ iconify(true);
+ }
+ if (_fullscreen) {
+ _fullscreen = false;
+ fullscreen(true, false);
+ }
+ if (_shaded) {
+ _shaded = false;
+ shade(true);
+ }
+ if (_urgent)
+ fireUrgent();
+
+ if (_max_vert && _max_horz) {
+ _max_vert = _max_horz = false;
+ maximize(true, 0, false);
+ } else if (_max_vert) {
+ _max_vert = false;
+ maximize(true, 2, false);
+ } else if (_max_horz) {
+ _max_horz = false;
+ maximize(true, 1, false);
+ }
+
+ if (_skip_taskbar); // nothing to do for this
+ if (_skip_pager); // nothing to do for this
+ if (_modal); // nothing to do for this
+ if (_above); // nothing to do for this
+ if (_below); // nothing to do for this
+}
+
+void Client::fireUrgent()
+{
+ // call the python UrgentWindow callbacks
+ EventData data(_screen, this, EventAction::UrgentWindow, 0);
+ openbox->bindings()->fireEvent(&data);
+}
+
+void Client::shade(bool shade)
+{
+ if (!(_functions & Func_Shade) || // can't
+ _shaded == shade) return; // already done
+
+ // when we're iconic, don't change the wmstate
+ if (!_iconic)
+ _wmstate = shade ? IconicState : NormalState;
+ _shaded = shade;
+ changeState();
+ frame->adjustSize();
+}
+
+void Client::maximize(bool max, int dir, bool savearea)
+{
+ assert(dir == 0 || dir == 1 || dir == 2);
+ if (!(_functions & Func_Maximize)) return; // can't
+
+ // check if already done
+ if (max) {
+ if (dir == 0 && _max_horz && _max_vert) return;
+ if (dir == 1 && _max_horz) return;
+ if (dir == 2 && _max_vert) return;
+ } else {
+ if (dir == 0 && !_max_horz && !_max_vert) return;
+ if (dir == 1 && !_max_horz) return;
+ if (dir == 2 && !_max_vert) return;
+ }
+
+ const otk::Rect &a = openbox->screen(_screen)->area(_desktop);
+ int x = frame->area().x(), y = frame->area().y(),
+ w = _area.width(), h = _area.height();
+
+ if (max) {
+ if (savearea) {
+ long dimensions[4];
+ long *readdim;
+ unsigned long n = 4;
+
+ dimensions[0] = x;
+ dimensions[1] = y;
+ dimensions[2] = w;
+ dimensions[3] = h;
+
+ // get the property off the window and use it for the dimentions we are
+ // already maxed on
+ if (otk::Property::get(_window, otk::Property::atoms.openbox_premax,
+ otk::Property::atoms.cardinal, &n,
+ (long unsigned**) &readdim)) {
+ if (n >= 4) {
+ if (_max_horz) {
+ dimensions[0] = readdim[0];
+ dimensions[2] = readdim[2];
+ }
+ if (_max_vert) {
+ dimensions[1] = readdim[1];
+ dimensions[3] = readdim[3];
+ }
+ }
+ delete readdim;
+ }
+
+ otk::Property::set(_window, otk::Property::atoms.openbox_premax,
+ otk::Property::atoms.cardinal,
+ (long unsigned*)dimensions, 4);
+ }
+ if (dir == 0 || dir == 1) { // horz
+ x = a.x();
+ w = a.width();
+ }
+ if (dir == 0 || dir == 2) { // vert
+ y = a.y();
+ h = a.height() - frame->size().top - frame->size().bottom;
+ }
+ } else {
+ long *dimensions;
+ long unsigned n = 4;
+
+ if (otk::Property::get(_window, otk::Property::atoms.openbox_premax,
+ otk::Property::atoms.cardinal, &n,
+ (long unsigned**) &dimensions)) {
+ if (n >= 4) {
+ if (dir == 0 || dir == 1) { // horz
+ x = (signed int)dimensions[0];
+ w = (signed int)dimensions[2];
+ }
+ if (dir == 0 || dir == 2) { // vert
+ y = (signed int)dimensions[1];
+ h = (signed int)dimensions[3];
+ }
+ }
+ delete dimensions;
+ } else {
+ // pick some fallbacks...
+ if (dir == 0 || dir == 1) { // horz
+ x = a.x() + a.width() / 4;
+ w = a.width() / 2;
+ }
+ if (dir == 0 || dir == 2) { // vert
+ y = a.y() + a.height() / 4;
+ h = a.height() / 2;
+ }
+ }
+ }
+
+ if (dir == 0 || dir == 1) // horz
+ _max_horz = max;
+ if (dir == 0 || dir == 2) // vert
+ _max_vert = max;
+
+ if (!_max_horz && !_max_vert)
+ otk::Property::erase(_window, otk::Property::atoms.openbox_premax);
+
+ changeState(); // change the state hints on the client
+
+ frame->frameGravity(x, y); // figure out where the client should be going
+ internal_resize(TopLeft, w, h, true, x, y);
+}
+
+void Client::fullscreen(bool fs, bool savearea)
+{
+ static FunctionFlags saved_func;
+ static DecorationFlags saved_decor;
+
+ if (!(_functions & Func_Fullscreen) || // can't
+ _fullscreen == fs) return; // already done
+
+ _fullscreen = fs;
+ changeState(); // change the state hints on the client
+
+ int x = _area.x(), y = _area.y(), w = _area.width(), h = _area.height();
+
+ if (fs) {
+ // save the functions and remove them
+ saved_func = _functions;
+ _functions = _functions & (Func_Close | Func_Fullscreen | Func_Iconify);
+ // save the decorations and remove them
+ saved_decor = _decorations;
+ _decorations = 0;
+ if (savearea) {
+ long dimensions[4];
+ dimensions[0] = _area.x();
+ dimensions[1] = _area.y();
+ dimensions[2] = _area.width();
+ dimensions[3] = _area.height();
+ otk::Property::set(_window, otk::Property::atoms.openbox_premax,
+ otk::Property::atoms.cardinal,
+ (long unsigned*)dimensions, 4);
+ }
+ const otk::ScreenInfo *info = otk::display->screenInfo(_screen);
+ x = 0;
+ y = 0;
+ w = info->size().width();
+ h = info->size().height();
+ } else {
+ _functions = saved_func;
+ _decorations = saved_decor;
+
+ long *dimensions;
+ long unsigned n = 4;
+
+ if (otk::Property::get(_window, otk::Property::atoms.openbox_premax,
+ otk::Property::atoms.cardinal, &n,
+ (long unsigned**) &dimensions)) {
+ if (n >= 4) {
+ x = dimensions[0];
+ y = dimensions[1];
+ w = dimensions[2];
+ h = dimensions[3];
+ }
+ delete dimensions;
+ } else {
+ // pick some fallbacks...
+ const otk::Rect &a = openbox->screen(_screen)->area(_desktop);
+ x = a.x() + a.width() / 4;
+ y = a.y() + a.height() / 4;
+ w = a.width() / 2;
+ h = a.height() / 2;
+ }
+ }
+
+ changeAllowedActions(); // based on the new _functions
+
+ // when fullscreening, don't obey things like increments, fill the screen
+ internal_resize(TopLeft, w, h, !fs, x, y);
+
+ // raise (back) into our stacking layer
+ openbox->screen(_screen)->raiseWindow(this);
+
+ // try focus us when we go into fullscreen mode
+ if (fs) focus();
+}
+
+void Client::iconify(bool iconic, bool curdesk)
+{
+ if (_iconic == iconic) return; // nothing to do
+
+#ifdef DEBUG
+ printf("%sconifying window: 0x%lx\n", (iconic ? "I" : "Uni"), _window);
+#endif
+
+ _iconic = iconic;
+
+ if (_iconic) {
+ _wmstate = IconicState;
+ ignore_unmaps++;
+ // we unmap the client itself so that we can get MapRequest events, and
+ // because the ICCCM tells us to!
+ XUnmapWindow(**otk::display, _window);
+ } else {
+ if (curdesk)
+ setDesktop(openbox->screen(_screen)->desktop());
+ _wmstate = NormalState;
+ XMapWindow(**otk::display, _window);
+ }
+ changeState();
+ showhide();
+ openbox->screen(_screen)->updateStruts();
+}
+
+void Client::disableDecorations(DecorationFlags flags)
+{
+ _disabled_decorations = flags;
+ setupDecorAndFunctions();
+}
+
+void Client::installColormap(bool install) const
+{
+ XWindowAttributes wa;
+ if (XGetWindowAttributes(**otk::display, _window, &wa)) {
+ if (install)
+ XInstallColormap(**otk::display, wa.colormap);
+ else
+ XUninstallColormap(**otk::display, wa.colormap);
+ }
+}
+
+Client *Client::searchModalTree(Client *node, Client *skip)
+{
+ List::const_iterator it, end = node->_transients.end();
+ Client *ret;
+
+ for (it = node->_transients.begin(); it != end; ++it) {
+ if (*it == skip) continue; // circular?
+ if ((ret = searchModalTree(*it, skip))) return ret; // got one
+ if ((*it)->_modal) return *it; // got one
+ }
+ return 0;
+}
+
+Client *Client::findModalChild()
+{
+ return searchModalTree(this, this);
+}
+
+
+bool Client::focus()
+{
+ // if we have a modal child, then focus it, not us
+ Client *c = findModalChild();
+ if (c) return c->focus();
+
+ // won't try focus if the client doesn't want it, or if the window isn't
+ // visible on the screen
+ if (!(frame->visible() && (_can_focus || _focus_notify))) return false;
+
+ // do a check to see if the window has already been unmapped or destroyed
+ // do this intelligently while watching out for unmaps we've generated
+ // (ignore_unmaps > 0)
+ XEvent ev;
+ if (XCheckTypedWindowEvent(**otk::display, _window, DestroyNotify, &ev)) {
+ XPutBackEvent(**otk::display, &ev);
+ return false;
+ }
+ while (XCheckTypedWindowEvent(**otk::display, _window, UnmapNotify, &ev)) {
+ if (ignore_unmaps) {
+ unmapHandler(ev.xunmap);
+ } else {
+ XPutBackEvent(**otk::display, &ev);
+ return false;
+ }
+ }
+
+ if (_can_focus)
+ XSetInputFocus(**otk::display, _window,
+ RevertToNone, CurrentTime);
+
+ if (_focus_notify) {
+ XEvent ce;
+ ce.xclient.type = ClientMessage;
+ ce.xclient.message_type = otk::Property::atoms.wm_protocols;
+ ce.xclient.display = **otk::display;
+ ce.xclient.window = _window;
+ ce.xclient.format = 32;
+ ce.xclient.data.l[0] = otk::Property::atoms.wm_take_focus;
+ ce.xclient.data.l[1] = openbox->lastTime();
+ ce.xclient.data.l[2] = 0l;
+ ce.xclient.data.l[3] = 0l;
+ ce.xclient.data.l[4] = 0l;
+ XSendEvent(**otk::display, _window, False, NoEventMask, &ce);
+ }
+
+ XSync(**otk::display, False);
+ return true;
+}
+
+
+void Client::unfocus() const
+{
+ assert(openbox->focusedClient() == this);
+ openbox->setFocusedClient(0);
+}
+
+
+void Client::focusHandler(const XFocusChangeEvent &e)
+{
+#ifdef DEBUG
+// printf("FocusIn for 0x%lx\n", e.window);
+#endif // DEBUG
+
+ otk::EventHandler::focusHandler(e);
+
+ _focused = true;
+ frame->adjustFocus();
+
+ calcLayer(); // focus state can affect the stacking layer
+
+ openbox->setFocusedClient(this);
+}
+
+
+void Client::unfocusHandler(const XFocusChangeEvent &e)
+{
+#ifdef DEBUG
+// printf("FocusOut for 0x%lx\n", e.window);
+#endif // DEBUG
+
+ otk::EventHandler::unfocusHandler(e);
+
+ _focused = false;
+ frame->adjustFocus();
+
+ calcLayer(); // focus state can affect the stacking layer
+
+ if (openbox->focusedClient() == this)
+ openbox->setFocusedClient(0);
+}
+
+
+void Client::configureRequestHandler(const XConfigureRequestEvent &ec)
+{
+#ifdef DEBUG
+ printf("ConfigureRequest for 0x%lx\n", ec.window);
+#endif // DEBUG
+
+ otk::EventHandler::configureRequestHandler(ec);
+
+ // compress these
+ XConfigureRequestEvent e = ec;
+ XEvent ev;
+ while (XCheckTypedWindowEvent(**otk::display, window(), ConfigureRequest,
+ &ev)) {
+ // XXX if this causes bad things.. we can compress config req's with the
+ // same mask.
+ e.value_mask |= ev.xconfigurerequest.value_mask;
+ if (ev.xconfigurerequest.value_mask & CWX)
+ e.x = ev.xconfigurerequest.x;
+ if (ev.xconfigurerequest.value_mask & CWY)
+ e.y = ev.xconfigurerequest.y;
+ if (ev.xconfigurerequest.value_mask & CWWidth)
+ e.width = ev.xconfigurerequest.width;
+ if (ev.xconfigurerequest.value_mask & CWHeight)
+ e.height = ev.xconfigurerequest.height;
+ if (ev.xconfigurerequest.value_mask & CWBorderWidth)
+ e.border_width = ev.xconfigurerequest.border_width;
+ if (ev.xconfigurerequest.value_mask & CWStackMode)
+ e.detail = ev.xconfigurerequest.detail;
+ }
+
+ // if we are iconic (or shaded (fvwm does this)) ignore the event
+ if (_iconic || _shaded) return;
+
+ if (e.value_mask & CWBorderWidth)
+ _border_width = e.border_width;
+
+ // resize, then move, as specified in the EWMH section 7.7
+ if (e.value_mask & (CWWidth | CWHeight)) {
+ int w = (e.value_mask & CWWidth) ? e.width : _area.width();
+ int h = (e.value_mask & CWHeight) ? e.height : _area.height();
+
+ Corner corner;
+ switch (_gravity) {
+ case NorthEastGravity:
+ case EastGravity:
+ corner = TopRight;
+ break;
+ case SouthWestGravity:
+ case SouthGravity:
+ corner = BottomLeft;
+ break;
+ case SouthEastGravity:
+ corner = BottomRight;
+ break;
+ default: // NorthWest, Static, etc
+ corner = TopLeft;
+ }
+
+ // if moving AND resizing ...
+ if (e.value_mask & (CWX | CWY)) {
+ int x = (e.value_mask & CWX) ? e.x : _area.x();
+ int y = (e.value_mask & CWY) ? e.y : _area.y();
+ internal_resize(corner, w, h, false, x, y);
+ } else // if JUST resizing...
+ internal_resize(corner, w, h, false);
+ } else if (e.value_mask & (CWX | CWY)) { // if JUST moving...
+ int x = (e.value_mask & CWX) ? e.x : _area.x();
+ int y = (e.value_mask & CWY) ? e.y : _area.y();
+ internal_move(x, y);
+ }
+
+ if (e.value_mask & CWStackMode) {
+ switch (e.detail) {
+ case Below:
+ case BottomIf:
+ openbox->screen(_screen)->lowerWindow(this);
+ break;
+
+ case Above:
+ case TopIf:
+ default:
+ openbox->screen(_screen)->raiseWindow(this);
+ break;
+ }
+ }
+}
+
+
+void Client::unmapHandler(const XUnmapEvent &e)
+{
+ if (ignore_unmaps) {
+#ifdef DEBUG
+// printf("Ignored UnmapNotify for 0x%lx (event 0x%lx)\n", e.window, e.event);
+#endif // DEBUG
+ ignore_unmaps--;
+ return;
+ }
+
+#ifdef DEBUG
+ printf("UnmapNotify for 0x%lx\n", e.window);
+#endif // DEBUG
+
+ otk::EventHandler::unmapHandler(e);
+
+ // this deletes us etc
+ openbox->screen(_screen)->unmanageWindow(this);
+}
+
+
+void Client::destroyHandler(const XDestroyWindowEvent &e)
+{
+#ifdef DEBUG
+ printf("DestroyNotify for 0x%lx\n", e.window);
+#endif // DEBUG
+
+ otk::EventHandler::destroyHandler(e);
+
+ // this deletes us etc
+ openbox->screen(_screen)->unmanageWindow(this);
+}
+
+
+void Client::reparentHandler(const XReparentEvent &e)
+{
+ // this is when the client is first taken captive in the frame
+ if (e.parent == frame->plate()) return;
+
+#ifdef DEBUG
+ printf("ReparentNotify for 0x%lx\n", e.window);
+#endif // DEBUG
+
+ otk::EventHandler::reparentHandler(e);
+
+ /*
+ This event is quite rare and is usually handled in unmapHandler.
+ However, if the window is unmapped when the reparent event occurs,
+ the window manager never sees it because an unmap event is not sent
+ to an already unmapped window.
+ */
+
+ // we don't want the reparent event, put it back on the stack for the X
+ // server to deal with after we unmanage the window
+ XEvent ev;
+ ev.xreparent = e;
+ XPutBackEvent(**otk::display, &ev);
+
+ // this deletes us etc
+ openbox->screen(_screen)->unmanageWindow(this);
+}
+
+void Client::mapRequestHandler(const XMapRequestEvent &e)
+{
+#ifdef DEBUG
+ printf("MapRequest for already managed 0x%lx\n", e.window);
+#endif // DEBUG
+
+ assert(_iconic); // we shouldn't be able to get this unless we're iconic
+
+ // move to the current desktop (uniconify)
+ iconify(false);
+ // XXX: should we focus/raise the window? (basically a net_wm_active_window)