1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
7 # include <sys/types.h>
9 #endif // HAVE_UNISTD_H
12 #define _(str) gettext(str)
19 #include "bindings.hh"
21 #include "otk/display.hh"
22 #include "otk/property.hh"
30 static int anotherWMRunning(Display *display, XErrorEvent *) {
31 printf(_("Another window manager already running on display %s.\n"),
32 DisplayString(display));
41 Screen::Screen(int screen)
44 assert(screen >= 0); assert(screen < ScreenCount(**otk::display));
45 _info = otk::display->screenInfo(screen);
48 XErrorHandler old = XSetErrorHandler(::anotherWMRunning);
49 XSelectInput(**otk::display, _info->rootWindow(),
51 XSync(**otk::display, false);
52 XSetErrorHandler(old);
54 _managed = !::running;
55 if (! _managed) return; // was unable to manage the screen
58 printf(_("Managing screen %d: visual 0x%lx, depth %d\n"),
59 _number, XVisualIDFromVisual(_info->visual()), _info->depth());
62 otk::Property::set(_info->rootWindow(), otk::Property::atoms.openbox_pid,
63 otk::Property::atoms.cardinal, (unsigned long) getpid());
65 // set the mouse cursor for the root window (the default cursor)
66 XDefineCursor(**otk::display, _info->rootWindow(),
67 openbox->cursors().session);
69 // XXX: initialize the screen's style
71 otk::ustring stylepath;
72 python_get_string("THEME", &stylepath);
73 otk::Configuration sconfig(false);
74 sconfig.setFile(otk::expandTilde(stylepath.c_str()));
75 if (!sconfig.load()) {
76 sconfig.setFile(otk::expandTilde(DEFAULTSTYLE));
77 if (!sconfig.load()) {
78 printf(_("Unable to load default style: %s. Aborting.\n"), DEFAULTSTYLE);
84 otk::display->renderControl(_number)->
85 drawRoot(*otk::RenderStyle::style(_number)->rootColor());
87 // set up notification of netwm support
88 changeSupportedAtoms();
90 // Set the netwm properties for geometry
91 unsigned long geometry[] = { _info->size().width(),
92 _info->size().height() };
93 otk::Property::set(_info->rootWindow(),
94 otk::Property::atoms.net_desktop_geometry,
95 otk::Property::atoms.cardinal, geometry, 2);
97 // Set the net_desktop_names property
98 std::vector<otk::ustring> names;
99 python_get_stringlist("DESKTOP_NAMES", &names);
100 otk::Property::set(_info->rootWindow(),
101 otk::Property::atoms.net_desktop_names,
102 otk::Property::utf8, names);
103 // the above set() will cause the updateDesktopNames to fire right away so
104 // we have a list of desktop names
108 if (!python_get_long("NUMBER_OF_DESKTOPS", &_num_desktops))
110 changeNumDesktops(_num_desktops); // set the hint
112 changeDesktop(0); // set the hint
114 // create the window which gets focus when no clients get it
115 XSetWindowAttributes attr;
116 attr.override_redirect = true;
117 _focuswindow = XCreateWindow(**otk::display, _info->rootWindow(),
118 -100, -100, 1, 1, 0, 0, InputOnly,
119 _info->visual(), CWOverrideRedirect, &attr);
120 XMapRaised(**otk::display, _focuswindow);
122 // these may be further updated if any pre-existing windows are found in
123 // the manageExising() function
124 changeClientList(); // initialize the client lists, which will be empty
125 calcArea(); // initialize the available working area
127 // register this class as the event handler for the root window
128 openbox->registerHandler(_info->rootWindow(), this);
130 // call the python Startup callbacks
131 EventData data(_number, 0, EventAction::Startup, 0);
132 openbox->bindings()->fireEvent(&data);
138 if (! _managed) return;
140 XSelectInput(**otk::display, _info->rootWindow(), NoEventMask);
142 // unmanage all windows
143 while (!clients.empty())
144 unmanageWindow(clients.front());
146 // call the python Shutdown callbacks
147 EventData data(_number, 0, EventAction::Shutdown, 0);
148 openbox->bindings()->fireEvent(&data);
150 XDestroyWindow(**otk::display, _focuswindow);
151 XDestroyWindow(**otk::display, _supportwindow);
155 void Screen::manageExisting()
157 unsigned int i, j, nchild;
158 Window r, p, *children;
159 XQueryTree(**otk::display, _info->rootWindow(), &r, &p,
162 // preen the window list of all icon windows... for better dockapp support
163 for (i = 0; i < nchild; i++) {
164 if (children[i] == None) continue;
166 XWMHints *wmhints = XGetWMHints(**otk::display,
170 if ((wmhints->flags & IconWindowHint) &&
171 (wmhints->icon_window != children[i])) {
172 for (j = 0; j < nchild; j++) {
173 if (children[j] == wmhints->icon_window) {
184 // manage shown windows
185 for (i = 0; i < nchild; ++i) {
186 if (children[i] == None)
189 XWindowAttributes attrib;
190 if (XGetWindowAttributes(**otk::display, children[i], &attrib)) {
191 if (attrib.override_redirect) continue;
193 if (attrib.map_state != IsUnmapped) {
194 manageWindow(children[i]);
203 void Screen::updateStrut()
205 _strut.left = _strut.right = _strut.top = _strut.bottom = 0;
207 ClientList::iterator it, end = clients.end();
208 for (it = clients.begin(); it != end; ++it) {
209 const otk::Strut &s = (*it)->strut();
210 _strut.left = std::max(_strut.left, s.left);
211 _strut.right = std::max(_strut.right, s.right);
212 _strut.top = std::max(_strut.top, s.top);
213 _strut.bottom = std::max(_strut.bottom, s.bottom);
219 void Screen::calcArea()
221 otk::Rect old_area = _area;
225 // reset to the full areas
226 if (isXineramaActive())
227 xineramaUsableArea = getXineramaAreas();
231 _area = otk::Rect(_strut.left, _strut.top,
232 _info->size().width() - (_strut.left + _strut.right),
233 _info->size().height() - (_strut.top + _strut.bottom));
237 if (isXineramaActive()) {
238 // keep each of the ximerama-defined areas inside the strut
239 RectList::iterator xit, xend = xineramaUsableArea.end();
240 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
241 if (xit->x() < usableArea.x()) {
242 xit->setX(usableArea.x());
243 xit->setWidth(xit->width() - usableArea.x());
245 if (xit->y() < usableArea.y()) {
246 xit->setY(usableArea.y());
247 xit->setHeight(xit->height() - usableArea.y());
249 if (xit->x() + xit->width() > usableArea.width())
250 xit->setWidth(usableArea.width() - xit->x());
251 if (xit->y() + xit->height() > usableArea.height())
252 xit->setHeight(usableArea.height() - xit->y());
258 if (old_area != _area) {
259 // the area has changed, adjust all the maximized windows
260 ClientList::iterator it, end = clients.end();
261 for (it = clients.begin(); it != end; ++it)
269 void Screen::changeSupportedAtoms()
271 // create the netwm support window
272 _supportwindow = XCreateSimpleWindow(**otk::display,
274 0, 0, 1, 1, 0, 0, 0);
276 // set supporting window
277 otk::Property::set(_info->rootWindow(),
278 otk::Property::atoms.net_supporting_wm_check,
279 otk::Property::atoms.window, _supportwindow);
281 //set properties on the supporting window
282 otk::Property::set(_supportwindow, otk::Property::atoms.net_wm_name,
283 otk::Property::utf8, "Openbox");
284 otk::Property::set(_supportwindow,
285 otk::Property::atoms.net_supporting_wm_check,
286 otk::Property::atoms.window, _supportwindow);
290 otk::Property::atoms.net_current_desktop,
291 otk::Property::atoms.net_number_of_desktops,
292 otk::Property::atoms.net_desktop_geometry,
293 otk::Property::atoms.net_desktop_viewport,
294 otk::Property::atoms.net_active_window,
295 otk::Property::atoms.net_workarea,
296 otk::Property::atoms.net_client_list,
297 otk::Property::atoms.net_client_list_stacking,
298 otk::Property::atoms.net_desktop_names,
299 otk::Property::atoms.net_close_window,
300 otk::Property::atoms.net_wm_name,
301 otk::Property::atoms.net_wm_visible_name,
302 otk::Property::atoms.net_wm_icon_name,
303 otk::Property::atoms.net_wm_visible_icon_name,
305 otk::Property::atoms.net_wm_desktop,
307 otk::Property::atoms.net_wm_strut,
308 otk::Property::atoms.net_wm_window_type,
309 otk::Property::atoms.net_wm_window_type_desktop,
310 otk::Property::atoms.net_wm_window_type_dock,
311 otk::Property::atoms.net_wm_window_type_toolbar,
312 otk::Property::atoms.net_wm_window_type_menu,
313 otk::Property::atoms.net_wm_window_type_utility,
314 otk::Property::atoms.net_wm_window_type_splash,
315 otk::Property::atoms.net_wm_window_type_dialog,
316 otk::Property::atoms.net_wm_window_type_normal,
318 otk::Property::atoms.net_wm_moveresize,
319 otk::Property::atoms.net_wm_moveresize_size_topleft,
320 otk::Property::atoms.net_wm_moveresize_size_topright,
321 otk::Property::atoms.net_wm_moveresize_size_bottomleft,
322 otk::Property::atoms.net_wm_moveresize_size_bottomright,
323 otk::Property::atoms.net_wm_moveresize_move,
325 otk::Property::atoms.net_wm_allowed_actions,
326 otk::Property::atoms.net_wm_action_move,
327 otk::Property::atoms.net_wm_action_resize,
328 otk::Property::atoms.net_wm_action_minimize,
329 otk::Property::atoms.net_wm_action_shade,
330 /* otk::Property::atoms.net_wm_action_stick,*/
331 otk::Property::atoms.net_wm_action_maximize_horz,
332 otk::Property::atoms.net_wm_action_maximize_vert,
333 otk::Property::atoms.net_wm_action_fullscreen,
334 otk::Property::atoms.net_wm_action_change_desktop,
335 otk::Property::atoms.net_wm_action_close,
337 otk::Property::atoms.net_wm_state,
338 otk::Property::atoms.net_wm_state_modal,
339 otk::Property::atoms.net_wm_state_maximized_vert,
340 otk::Property::atoms.net_wm_state_maximized_horz,
341 otk::Property::atoms.net_wm_state_shaded,
342 otk::Property::atoms.net_wm_state_skip_taskbar,
343 otk::Property::atoms.net_wm_state_skip_pager,
344 otk::Property::atoms.net_wm_state_hidden,
345 otk::Property::atoms.net_wm_state_fullscreen,
346 otk::Property::atoms.net_wm_state_above,
347 otk::Property::atoms.net_wm_state_below,
349 const int num_supported = sizeof(supported)/sizeof(Atom);
351 otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_supported,
352 otk::Property::atoms.atom, supported, num_supported);
356 void Screen::changeClientList()
359 unsigned int size = clients.size();
361 // create an array of the window ids
365 windows = new Window[size];
367 ClientList::const_iterator it = clients.begin();
368 const ClientList::const_iterator end = clients.end();
369 for (; it != end; ++it, ++win_it)
370 *win_it = (*it)->window();
372 windows = (Window*) 0;
374 otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_client_list,
375 otk::Property::atoms.window, windows, size);
380 changeStackingList();
384 void Screen::changeStackingList()
387 unsigned int size = _stacking.size();
389 assert(size == clients.size()); // just making sure.. :)
392 // create an array of the window ids (from bottom to top, reverse order!)
396 windows = new Window[size];
398 ClientList::const_reverse_iterator it = _stacking.rbegin();
399 const ClientList::const_reverse_iterator end = _stacking.rend();
400 for (; it != end; ++it, ++win_it)
401 *win_it = (*it)->window();
403 windows = (Window*) 0;
405 otk::Property::set(_info->rootWindow(),
406 otk::Property::atoms.net_client_list_stacking,
407 otk::Property::atoms.window, windows, size);
414 void Screen::changeWorkArea() {
415 unsigned long *dims = new unsigned long[4 * _num_desktops];
416 for (long i = 0; i < _num_desktops; ++i) {
417 dims[(i * 4) + 0] = _area.x();
418 dims[(i * 4) + 1] = _area.y();
419 dims[(i * 4) + 2] = _area.width();
420 dims[(i * 4) + 3] = _area.height();
422 otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_workarea,
423 otk::Property::atoms.cardinal, dims, 4 * _num_desktops);
428 void Screen::manageWindow(Window window)
432 XSetWindowAttributes attrib_set;
434 XWindowAttributes attrib;
436 otk::display->grab();
438 // check if it has already been unmapped by the time we started mapping
439 // the grab does a sync so we don't have to here
440 if (XCheckTypedWindowEvent(**otk::display, window, DestroyNotify, &e) ||
441 XCheckTypedWindowEvent(**otk::display, window, UnmapNotify, &e)) {
442 XPutBackEvent(**otk::display, &e);
444 otk::display->ungrab();
445 return; // don't manage it
448 if (!XGetWindowAttributes(**otk::display, window, &attrib) ||
449 attrib.override_redirect) {
450 otk::display->ungrab();
451 return; // don't manage it
454 // is the window a docking app
455 if ((wmhint = XGetWMHints(**otk::display, window))) {
456 if ((wmhint->flags & StateHint) &&
457 wmhint->initial_state == WithdrawnState) {
458 //slit->addClient(w); // XXX: make dock apps work!
460 otk::display->ungrab();
467 // choose the events we want to receive on the CLIENT window
468 attrib_set.event_mask = Client::event_mask;
469 attrib_set.do_not_propagate_mask = Client::no_propagate_mask;
470 XChangeWindowAttributes(**otk::display, window,
471 CWEventMask|CWDontPropagate, &attrib_set);
473 // create the Client class, which gets all of the hints on the window
474 client = new Client(_number, window);
475 // register for events
476 openbox->registerHandler(window, client);
477 // add to the wm's map
478 openbox->addClient(window, client);
480 // we dont want a border on the client
481 client->toggleClientBorder(false);
483 // specify that if we exit, the window should not be destroyed and should be
484 // reparented back to root automatically
485 XChangeSaveSet(**otk::display, window, SetModeInsert);
487 // create the decoration frame for the client window
488 client->frame = new Frame(client);
489 // register the plate for events (map req's)
490 // this involves removing itself from the handler list first, since it is
491 // auto added to the list, being a widget. we won't get any events on the
492 // plate except for events for the client (SubstructureRedirectMask)
493 openbox->clearHandler(client->frame->plate());
494 openbox->registerHandler(client->frame->plate(), client);
496 // add to the wm's map
497 Window *w = client->frame->allWindows();
498 for (unsigned int i = 0; w[i]; ++i)
499 openbox->addClient(w[i], client);
502 // reparent the client to the frame
503 client->frame->grabClient();
505 if (openbox->state() != Openbox::State_Starting) {
506 // position the window intelligenty .. hopefully :)
507 // call the python PLACEWINDOW binding
508 EventData data(_number, client, EventAction::PlaceWindow, 0);
509 openbox->bindings()->fireEvent(&data);
512 EventData ddata(_number, client, EventAction::DisplayingWindow, 0);
513 openbox->bindings()->fireEvent(&ddata);
515 // if on the current desktop.. (or all desktops)
516 if (client->desktop() == _desktop ||
517 client->desktop() == (signed)0xffffffff) {
518 client->frame->show();
521 client->applyStartupState();
523 otk::display->ungrab();
525 // add to the screen's list
526 clients.push_back(client);
527 // once the client is in the list, update our strut to include the new
528 // client's (it is good that this happens after window placement!)
530 // this puts into the stacking order, then raises it
531 _stacking.push_back(client);
533 // update the root properties
536 openbox->bindings()->grabButtons(true, client);
538 EventData ndata(_number, client, EventAction::NewWindow, 0);
539 openbox->bindings()->fireEvent(&ndata);
542 printf("Managed window 0x%lx frame 0x%lx\n",
543 window, client->frame->window());
548 void Screen::unmanageWindow(Client *client)
550 Frame *frame = client->frame;
552 // call the python CLOSEWINDOW binding
553 EventData data(_number, client, EventAction::CloseWindow, 0);
554 openbox->bindings()->fireEvent(&data);
556 openbox->bindings()->grabButtons(false, client);
558 // remove from the wm's map
559 openbox->removeClient(client->window());
560 Window *w = frame->allWindows();
561 for (unsigned int i = 0; w[i]; ++i)
562 openbox->addClient(w[i], client);
564 // unregister for handling events
565 openbox->clearHandler(client->window());
567 // remove the window from our save set
568 XChangeSaveSet(**otk::display, client->window(), SetModeDelete);
570 // we dont want events no more
571 XSelectInput(**otk::display, client->window(), NoEventMask);
575 // give the client its border back
576 client->toggleClientBorder(true);
578 // reparent the window out of the frame
579 frame->releaseClient();
582 Window framewin = client->frame->window();
584 delete client->frame;
587 // remove from the stacking order
588 _stacking.remove(client);
590 // remove from the screen's list
591 clients.remove(client);
593 // once the client is out of the list, update our strut to remove it's
597 // unset modal before dropping our focus
598 client->setModal(false);
600 // unfocus the client (calls the focus callbacks)
604 printf("Unmanaged window 0x%lx frame 0x%lx\n", client->window(), framewin);
609 // update the root properties
613 void Screen::lowerWindow(Client *client)
615 Window wins[2]; // only ever restack 2 windows.
617 assert(!_stacking.empty()); // this would be bad
619 ClientList::iterator it = --_stacking.end();
620 const ClientList::iterator end = _stacking.begin();
622 if (client->modal() && client->transientFor()) {
623 // don't let a modal window lower below its transient_for
624 it = std::find(_stacking.begin(), _stacking.end(), client->transientFor());
625 assert(it != _stacking.end());
627 wins[0] = (it == _stacking.begin() ? _focuswindow :
628 ((*(--ClientList::const_iterator(it)))->frame->window()));
629 wins[1] = client->frame->window();
630 if (wins[0] == wins[1]) return; // already right above the window
632 _stacking.remove(client);
633 _stacking.insert(it, client);
635 for (; it != end && (*it)->layer() < client->layer(); --it);
636 if (*it == client) return; // already the bottom, return
638 wins[0] = (*it)->frame->window();
639 wins[1] = client->frame->window();
641 _stacking.remove(client);
642 _stacking.insert(++it, client);
645 XRestackWindows(**otk::display, wins, 2);
646 changeStackingList();
649 void Screen::raiseWindow(Client *client)
651 Window wins[2]; // only ever restack 2 windows.
653 assert(!_stacking.empty()); // this would be bad
655 // remove the client before looking so we can't run into ourselves
656 _stacking.remove(client);
658 ClientList::iterator it = _stacking.begin();
659 const ClientList::iterator end = _stacking.end();
661 // the stacking list is from highest to lowest
662 for (; it != end && (*it)->layer() > client->layer(); ++it);
665 if our new position is the top, we want to stack under the _focuswindow
666 otherwise, we want to stack under the previous window in the stack.
668 wins[0] = (it == _stacking.begin() ? _focuswindow :
669 ((*(--ClientList::const_iterator(it)))->frame->window()));
670 wins[1] = client->frame->window();
672 _stacking.insert(it, client);
674 XRestackWindows(**otk::display, wins, 2);
676 // if the window has a modal child, then raise it after us to put it on top
677 if (client->modalChild())
678 raiseWindow(client->modalChild());
680 changeStackingList(); // no need to do this twice!
683 void Screen::changeDesktop(long desktop)
685 if (!(desktop >= 0 && desktop < _num_desktops)) return;
687 printf("Moving to desktop %ld\n", desktop);
692 otk::Property::set(_info->rootWindow(),
693 otk::Property::atoms.net_current_desktop,
694 otk::Property::atoms.cardinal, _desktop);
696 if (old == _desktop) return;
698 ClientList::iterator it, end = clients.end();
699 for (it = clients.begin(); it != end; ++it) {
700 if ((*it)->desktop() == old) {
701 (*it)->frame->hide();
702 } else if ((*it)->desktop() == _desktop) {
703 (*it)->frame->show();
707 // force the callbacks to fire
708 if (!openbox->focusedClient())
709 openbox->setFocusedClient(0);
712 void Screen::changeNumDesktops(long num)
716 if (!(num > 0)) return;
718 // move windows on desktops that will no longer exist!
719 ClientList::iterator it, end = clients.end();
720 for (it = clients.begin(); it != end; ++it) {
721 int d = (*it)->desktop();
722 if (d >= num && !(d == (signed) 0xffffffff ||
723 d == Client::ICONIC_DESKTOP)) {
725 ce.xclient.type = ClientMessage;
726 ce.xclient.message_type = otk::Property::atoms.net_wm_desktop;
727 ce.xclient.display = **otk::display;
728 ce.xclient.window = (*it)->window();
729 ce.xclient.format = 32;
730 ce.xclient.data.l[0] = num - 1;
731 XSendEvent(**otk::display, _info->rootWindow(), False,
732 SubstructureNotifyMask | SubstructureRedirectMask, &ce);
737 otk::Property::set(_info->rootWindow(),
738 otk::Property::atoms.net_number_of_desktops,
739 otk::Property::atoms.cardinal, _num_desktops);
741 // set the viewport hint
742 unsigned long *viewport = new unsigned long[_num_desktops * 2];
743 memset(viewport, 0, sizeof(unsigned long) * _num_desktops * 2);
744 otk::Property::set(_info->rootWindow(),
745 otk::Property::atoms.net_desktop_viewport,
746 otk::Property::atoms.cardinal,
747 viewport, _num_desktops * 2);
750 // update the work area hint
753 // change our desktop if we're on one that no longer exists!
755 changeDesktop(num - 1);
759 void Screen::updateDesktopNames()
761 unsigned long num = (unsigned) -1;
763 if (!otk::Property::get(_info->rootWindow(),
764 otk::Property::atoms.net_desktop_names,
765 otk::Property::utf8, &num, &_desktop_names))
766 _desktop_names.clear();
767 while ((long)_desktop_names.size() < _num_desktops)
768 _desktop_names.push_back("Unnamed");
772 void Screen::setDesktopName(long i, const otk::ustring &name)
776 if (i >= _num_desktops) return;
778 otk::Property::StringVect newnames = _desktop_names;
780 otk::Property::set(_info->rootWindow(),
781 otk::Property::atoms.net_desktop_names,
782 otk::Property::utf8, newnames);
786 void Screen::installColormap(bool install) const
789 XInstallColormap(**otk::display, _info->colormap());
791 XUninstallColormap(**otk::display, _info->colormap());
795 void Screen::propertyHandler(const XPropertyEvent &e)
797 otk::EventHandler::propertyHandler(e);
799 // compress changes to a single property into a single change
801 while (XCheckTypedWindowEvent(**otk::display, _info->rootWindow(),
803 // XXX: it would be nice to compress ALL changes to a property, not just
804 // changes in a row without other props between.
805 if (ce.xproperty.atom != e.atom) {
806 XPutBackEvent(**otk::display, &ce);
811 if (e.atom == otk::Property::atoms.net_desktop_names)
812 updateDesktopNames();
816 void Screen::clientMessageHandler(const XClientMessageEvent &e)
818 otk::EventHandler::clientMessageHandler(e);
820 if (e.format != 32) return;
822 if (e.message_type == otk::Property::atoms.net_current_desktop) {
823 changeDesktop(e.data.l[0]);
824 } else if (e.message_type == otk::Property::atoms.net_number_of_desktops) {
825 changeNumDesktops(e.data.l[0]);
830 void Screen::mapRequestHandler(const XMapRequestEvent &e)
832 otk::EventHandler::mapRequestHandler(e);
835 printf("MapRequest for 0x%lx\n", e.window);
838 Client *c = openbox->findClient(e.window);
841 printf("DEBUG: MAP REQUEST CAUGHT IN SCREEN. IGNORED.\n");
844 manageWindow(e.window);