+void Screen::restack(bool raise, Client *client)
+{
+ const int layer = client->layer();
+ std::vector<Window> wins;
+
+ _stacking.remove(client);
+
+ // the stacking list is from highest to lowest
+
+ Client::List::iterator it = _stacking.begin(), end = _stacking.end();
+ // insert the windows above this window
+ for (; it != end; ++it) {
+ if ((*it)->layer() < layer || (raise && (*it)->layer() == layer))
+ break;
+ wins.push_back((*it)->frame->window());
+ }
+ // insert our client
+ wins.push_back(client->frame->window());
+ _stacking.insert(it, client);
+ // insert the remaining below this window
+ for (; it != end; ++it)
+ wins.push_back((*it)->frame->window());
+
+ XRestackWindows(otk::Display::display, &wins[0], wins.size());
+ changeStackingList();
+}
+
+void Screen::changeDesktop(long desktop)
+{
+ if (!(desktop >= 0 && desktop < _num_desktops)) return;
+
+ printf("Moving to desktop %ld\n", desktop);
+
+ long old = _desktop;
+
+ _desktop = desktop;
+ openbox->property()->set(_info->rootWindow(),
+ otk::Property::net_current_desktop,
+ otk::Property::Atom_Cardinal,
+ _desktop);
+
+ if (old == _desktop) return;
+
+ Client::List::iterator it, end = clients.end();
+ for (it = clients.begin(); it != end; ++it) {
+ if ((*it)->desktop() == old) {
+ (*it)->frame->hide();
+ } else if ((*it)->desktop() == _desktop) {
+ (*it)->frame->show();
+ }
+ }
+
+ // force the callbacks to fire
+ if (!openbox->focusedClient())
+ openbox->setFocusedClient(0);
+}
+
+void Screen::changeNumDesktops(long num)
+{
+ assert(num > 0);
+
+ if (!(num > 0)) return;
+
+ // XXX: move windows on desktops that will no longer exist!
+
+ _num_desktops = num;
+ openbox->property()->set(_info->rootWindow(),
+ otk::Property::net_number_of_desktops,
+ otk::Property::Atom_Cardinal,
+ _num_desktops);
+
+ // set the viewport hint
+ unsigned long *viewport = new unsigned long[_num_desktops * 2];
+ memset(viewport, 0, sizeof(unsigned long) * _num_desktops * 2);
+ openbox->property()->set(_info->rootWindow(),
+ otk::Property::net_desktop_viewport,
+ otk::Property::Atom_Cardinal,
+ viewport, _num_desktops * 2);
+ delete [] viewport;
+
+ // update the work area hint
+ changeWorkArea();
+}
+
+
+void Screen::updateDesktopNames()
+{
+ const otk::Property *property = openbox->property();
+
+ unsigned long num = (unsigned) -1;
+
+ if (!property->get(_info->rootWindow(),
+ otk::Property::net_desktop_names,
+ otk::Property::utf8, &num, &_desktop_names))
+ _desktop_names.clear();
+ while ((long)_desktop_names.size() < _num_desktops)
+ _desktop_names.push_back("Unnamed");
+}
+
+
+void Screen::setDesktopName(long i, const otk::ustring &name)
+{
+ assert(i >= 0);
+
+ if (i >= _num_desktops) return;
+
+ const otk::Property *property = openbox->property();
+
+ otk::Property::StringVect newnames = _desktop_names;
+ newnames[i] = name;
+ property->set(_info->rootWindow(), otk::Property::net_desktop_names,
+ otk::Property::utf8, newnames);
+}
+
+
+void Screen::propertyHandler(const XPropertyEvent &e)
+{
+ otk::EventHandler::propertyHandler(e);
+
+ const otk::Property *property = openbox->property();
+
+ // compress changes to a single property into a single change
+ XEvent ce;
+ while (XCheckTypedEvent(otk::Display::display, e.type, &ce)) {
+ // XXX: it would be nice to compress ALL changes to a property, not just
+ // changes in a row without other props between.
+ if (ce.xproperty.atom != e.atom) {
+ XPutBackEvent(otk::Display::display, &ce);
+ break;
+ }
+ }
+
+ if (e.atom == property->atom(otk::Property::net_desktop_names))
+ updateDesktopNames();
+}
+
+
+void Screen::clientMessageHandler(const XClientMessageEvent &e)
+{
+ otk::EventHandler::clientMessageHandler(e);
+
+ if (e.format != 32) return;
+
+ const otk::Property *property = openbox->property();
+
+ if (e.message_type == property->atom(otk::Property::net_current_desktop)) {
+ changeDesktop(e.data.l[0]);
+ } else if (e.message_type ==
+ property->atom(otk::Property::net_number_of_desktops)) {
+ changeNumDesktops(e.data.l[0]);
+ }
+ // XXX: so many client messages to handle here! ..or not.. they go to clients
+}
+
+
+void Screen::mapRequestHandler(const XMapRequestEvent &e)
+{
+ otk::EventHandler::mapRequestHandler(e);
+
+#ifdef DEBUG
+ printf("MapRequest for 0x%lx\n", e.window);
+#endif // DEBUG
+
+ /*
+ MapRequest events come here even after the window exists instead of going
+ right to the client window, because of how they are sent and their struct
+ layout.
+ */
+ Client *c = openbox->findClient(e.window);
+
+ if (c) {
+ // send a net_active_window message
+ XEvent ce;
+ ce.xclient.type = ClientMessage;
+ ce.xclient.message_type =
+ openbox->property()->atom(otk::Property::net_active_window);
+ ce.xclient.display = otk::Display::display;
+ ce.xclient.window = c->window();
+ ce.xclient.format = 32;
+ ce.xclient.data.l[0] = 0l;
+ ce.xclient.data.l[1] = 0l;
+ ce.xclient.data.l[2] = 0l;
+ ce.xclient.data.l[3] = 0l;
+ ce.xclient.data.l[4] = 0l;
+ XSendEvent(otk::Display::display, _info->rootWindow(), false,
+ SubstructureRedirectMask | SubstructureNotifyMask,
+ &ce);
+ } else
+ manageWindow(e.window);
+}