+ } else if (e.message_type == otk::Property::atoms.net_close_window) {
+#ifdef DEBUG
+ printf("net_close_window for 0x%lx\n", _window);
+#endif
+ close();
+ } else if (e.message_type == otk::Property::atoms.net_active_window) {
+#ifdef DEBUG
+ printf("net_active_window for 0x%lx\n", _window);
+#endif
+ if (_iconic)
+ setDesktop(openbox->screen(_screen)->desktop());
+ if (_shaded)
+ shade(false);
+ // XXX: deiconify
+ focus();
+ openbox->screen(_screen)->raiseWindow(this);
+ }
+}
+
+
+#if defined(SHAPE)
+void Client::shapeHandler(const XShapeEvent &e)
+{
+ otk::EventHandler::shapeHandler(e);
+
+ if (e.kind == ShapeBounding) {
+ _shaped = e.shaped;
+ frame->adjustShape();
+ }
+}
+#endif
+
+
+void Client::resize(Corner anchor, int w, int h)
+{
+ if (!(_functions & Func_Resize)) return;
+ internal_resize(anchor, w, h);
+}
+
+
+void Client::internal_resize(Corner anchor, int w, int h, bool user,
+ int x, int y)
+{
+ w -= _base_size.x();
+ h -= _base_size.y();
+
+ // for interactive resizing. have to move half an increment in each
+ // direction.
+ w += _size_inc.x() / 2;
+ h += _size_inc.y() / 2;
+
+ if (user) {
+ // if this is a user-requested resize, then check against min/max sizes
+ // and aspect ratios
+
+ // smaller than min size or bigger than max size?
+ if (w < _min_size.x()) w = _min_size.x();
+ else if (w > _max_size.x()) w = _max_size.x();
+ if (h < _min_size.y()) h = _min_size.y();
+ else if (h > _max_size.y()) h = _max_size.y();
+
+ // adjust the height ot match the width for the aspect ratios
+ if (_min_ratio)
+ if (h * _min_ratio > w) h = static_cast<int>(w / _min_ratio);
+ if (_max_ratio)
+ if (h * _max_ratio < w) h = static_cast<int>(w / _max_ratio);
+ }
+
+ // keep to the increments
+ w /= _size_inc.x();
+ h /= _size_inc.y();
+
+ // you cannot resize to nothing
+ if (w < 1) w = 1;
+ if (h < 1) h = 1;
+
+ // store the logical size
+ _logical_size.setPoint(w, h);
+
+ w *= _size_inc.x();
+ h *= _size_inc.y();
+
+ w += _base_size.x();
+ h += _base_size.y();
+
+ if (x == INT_MIN || y == INT_MIN) {
+ x = _area.x();
+ y = _area.y();
+ switch (anchor) {
+ case TopLeft:
+ break;
+ case TopRight:
+ x -= w - _area.width();
+ break;
+ case BottomLeft:
+ y -= h - _area.height();
+ break;
+ case BottomRight:
+ x -= w - _area.width();
+ y -= h - _area.height();
+ break;
+ }
+ }
+
+ _area.setSize(w, h);
+
+ XResizeWindow(**otk::display, _window, w, h);
+
+ // resize the frame to match the request
+ frame->adjustSize();
+ internal_move(x, y);
+}
+
+
+void Client::move(int x, int y)
+{
+ if (!(_functions & Func_Move)) return;
+ internal_move(x, y);
+}
+
+
+void Client::internal_move(int x, int y)
+{
+ _area.setPos(x, y);
+
+ // move the frame to be in the requested position
+ if (frame) { // this can be called while mapping, before frame exists
+ frame->adjustPosition();
+
+ // send synthetic configure notify (we don't need to if we aren't mapped
+ // yet)
+ XEvent event;
+ event.type = ConfigureNotify;
+ event.xconfigure.display = **otk::display;
+ event.xconfigure.event = _window;
+ event.xconfigure.window = _window;
+ event.xconfigure.x = x;
+ event.xconfigure.y = y;
+ event.xconfigure.width = _area.width();
+ event.xconfigure.height = _area.height();
+ event.xconfigure.border_width = _border_width;
+ event.xconfigure.above = frame->window();
+ event.xconfigure.override_redirect = False;
+ XSendEvent(event.xconfigure.display, event.xconfigure.window, False,
+ StructureNotifyMask, &event);
+ }
+}
+
+
+void Client::close()
+{
+ XEvent ce;
+
+ if (!(_functions & Func_Close)) return;
+
+ // XXX: itd be cool to do timeouts and shit here for killing the client's
+ // process off
+ // like... if the window is around after 5 seconds, then the close button
+ // turns a nice red, and if this function is called again, the client is
+ // explicitly killed.
+
+ 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_delete_window;
+ ce.xclient.data.l[1] = CurrentTime;
+ 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);
+}
+
+
+void Client::changeState()
+{
+ unsigned long state[2];
+ state[0] = _wmstate;
+ state[1] = None;
+ otk::Property::set(_window, otk::Property::atoms.wm_state,
+ otk::Property::atoms.wm_state, state, 2);
+
+ Atom netstate[10];
+ int num = 0;
+ if (_modal)
+ netstate[num++] = otk::Property::atoms.net_wm_state_modal;
+ if (_shaded)
+ netstate[num++] = otk::Property::atoms.net_wm_state_shaded;
+ if (_iconic)
+ netstate[num++] = otk::Property::atoms.net_wm_state_hidden;
+ if (_skip_taskbar)
+ netstate[num++] = otk::Property::atoms.net_wm_state_skip_taskbar;
+ if (_skip_pager)
+ netstate[num++] = otk::Property::atoms.net_wm_state_skip_pager;
+ if (_fullscreen)
+ netstate[num++] = otk::Property::atoms.net_wm_state_fullscreen;
+ if (_max_vert)
+ netstate[num++] = otk::Property::atoms.net_wm_state_maximized_vert;
+ if (_max_horz)
+ netstate[num++] = otk::Property::atoms.net_wm_state_maximized_horz;
+ if (_above)
+ netstate[num++] = otk::Property::atoms.net_wm_state_above;
+ if (_below)
+ netstate[num++] = otk::Property::atoms.net_wm_state_below;
+ otk::Property::set(_window, otk::Property::atoms.net_wm_state,
+ otk::Property::atoms.atom, netstate, num);
+
+ calcLayer();
+
+ if (frame)
+ frame->adjustState();
+}
+
+
+void Client::changeAllowedActions(void)
+{
+ 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);
+}
+
+
+void Client::applyStartupState()
+{
+ // these are in a carefully crafted order..
+
+ if (_iconic) {
+ _iconic = false;
+ setDesktop(ICONIC_DESKTOP);
+ }
+ if (_fullscreen) {
+ _fullscreen = false;
+ fullscreen(true);
+ }
+ if (_shaded) {
+ _shaded = false;
+ shade(true);
+ }
+ if (_urgent)
+ fireUrgent();
+
+ if (_max_vert); // XXX: incomplete
+ if (_max_horz); // XXX: incomplete
+
+ 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::fullscreen(bool fs)
+{
+ static FunctionFlags saved_func;
+ static DecorationFlags saved_decor;
+ static otk::Rect saved_area;
+ static otk::Point saved_logical_size;
+
+ if (!(_functions & Func_Fullscreen) || // can't
+ _fullscreen == fs) return; // already done
+
+ _fullscreen = fs;
+ changeState(); // change the state hints on the client
+
+ 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;
+ // save the area and adjust it (we don't call internal resize here for
+ // constraints on the size, etc, we just make it fullscreen).
+ saved_area = _area;
+ const otk::ScreenInfo *info = otk::display->screenInfo(_screen);
+ _area.setRect(0, 0, info->width(), info->height());
+ saved_logical_size = _logical_size;
+ _logical_size.setPoint((info->width() - _base_size.x()) / _size_inc.x(),
+ (info->height() - _base_size.y()) / _size_inc.y());
+ } else {
+ _functions = saved_func;
+ _decorations = saved_decor;
+ _area = saved_area;
+ _logical_size = saved_logical_size;
+ }
+
+ changeAllowedActions(); // based on the new _functions
+
+ frame->adjustSize(); // drop/replace the decor's and resize
+ frame->adjustPosition(); // get (back) in position!
+
+ // raise (back) into our stacking layer
+ openbox->screen(_screen)->raiseWindow(this);
+
+ // try focus us when we go into fullscreen mode
+ if (fs) focus();
+}
+
+
+bool Client::focus()
+{
+ // won't try focus if the client doesn't want it, or if the window isn't
+ // visible on the screen
+ if (!(frame->isVisible() && (_can_focus || _focus_notify))) return false;
+
+ if (_focused) return true;
+
+ // 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
+{
+ if (!_focused) return;
+
+ 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);
+
+ frame->focus();
+ _focused = true;
+
+ 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);
+
+ frame->unfocus();
+ _focused = false;
+
+ if (openbox->focusedClient() == this)
+ openbox->setFocusedClient(0);
+}
+
+
+void Client::configureRequestHandler(const XConfigureRequestEvent &e)
+{
+#ifdef DEBUG
+ printf("ConfigureRequest for 0x%lx\n", e.window);
+#endif // DEBUG
+
+ otk::EventHandler::configureRequestHandler(e);
+
+ // 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);