1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
10 #endif // HAVE_STDIO_H
14 #endif // HAVE_STRING_H
17 # include <sys/types.h>
19 #endif // HAVE_UNISTD_H
22 #define _(str) gettext(str)
29 #include "bindings.hh"
31 #include "otk/display.hh"
32 #include "otk/property.hh"
38 static int anotherWMRunning(Display *display, XErrorEvent *) {
39 printf(_("Another window manager already running on display %s.\n"),
40 DisplayString(display));
49 Screen::Screen(int screen)
50 : WidgetBase(WidgetBase::Type_Root),
53 assert(screen >= 0); assert(screen < ScreenCount(**otk::display));
54 _info = otk::display->screenInfo(screen);
57 XErrorHandler old = XSetErrorHandler(::anotherWMRunning);
58 XSelectInput(**otk::display, _info->rootWindow(),
60 XSync(**otk::display, false);
61 XSetErrorHandler(old);
63 _managed = !::running;
64 if (! _managed) return; // was unable to manage the screen
67 printf(_("Managing screen %d: visual 0x%lx, depth %d\n"),
68 _number, XVisualIDFromVisual(_info->visual()), _info->depth());
71 otk::Property::set(_info->rootWindow(), otk::Property::atoms.openbox_pid,
72 otk::Property::atoms.cardinal, (unsigned long) getpid());
74 // set the mouse cursor for the root window (the default cursor)
75 XDefineCursor(**otk::display, _info->rootWindow(),
76 openbox->cursors().session);
78 // XXX: initialize the screen's style
80 otk::ustring stylepath;
81 python_get_string("THEME", &stylepath);
82 otk::Configuration sconfig(false);
83 sconfig.setFile(otk::expandTilde(stylepath.c_str()));
84 if (!sconfig.load()) {
85 sconfig.setFile(otk::expandTilde(DEFAULTSTYLE));
86 if (!sconfig.load()) {
87 printf(_("Unable to load default style: %s. Aborting.\n"), DEFAULTSTYLE);
93 otk::display->renderControl(_number)->
94 drawRoot(*otk::RenderStyle::style(_number)->rootColor());
96 // set up notification of netwm support
97 changeSupportedAtoms();
99 // Set the netwm properties for geometry
100 unsigned long geometry[] = { _info->size().width(),
101 _info->size().height() };
102 otk::Property::set(_info->rootWindow(),
103 otk::Property::atoms.net_desktop_geometry,
104 otk::Property::atoms.cardinal, geometry, 2);
106 // Set the net_desktop_names property
107 std::vector<otk::ustring> names;
108 python_get_stringlist("DESKTOP_NAMES", &names);
109 otk::Property::set(_info->rootWindow(),
110 otk::Property::atoms.net_desktop_names,
111 otk::Property::utf8, names);
112 // the above set() will cause the updateDesktopNames to fire right away so
113 // we have a list of desktop names
117 if (!python_get_long("NUMBER_OF_DESKTOPS", &_num_desktops))
119 changeNumDesktops(_num_desktops); // set the hint
121 changeDesktop(0); // set the hint
123 // create the window which gets focus when no clients get it
124 XSetWindowAttributes attr;
125 attr.override_redirect = true;
126 _focuswindow = XCreateWindow(**otk::display, _info->rootWindow(),
127 -100, -100, 1, 1, 0, 0, InputOnly,
128 _info->visual(), CWOverrideRedirect, &attr);
129 XMapRaised(**otk::display, _focuswindow);
131 // these may be further updated if any pre-existing windows are found in
132 // the manageExising() function
133 changeClientList(); // initialize the client lists, which will be empty
134 calcArea(); // initialize the available working area
136 // register this class as the event handler for the root window
137 openbox->registerHandler(_info->rootWindow(), this);
139 // call the python Startup callbacks
140 EventData data(_number, 0, EventAction::Startup, 0);
141 openbox->bindings()->fireEvent(&data);
147 if (! _managed) return;
149 XSelectInput(**otk::display, _info->rootWindow(), NoEventMask);
151 // unmanage all windows
152 while (!clients.empty())
153 unmanageWindow(clients.front());
155 // call the python Shutdown callbacks
156 EventData data(_number, 0, EventAction::Shutdown, 0);
157 openbox->bindings()->fireEvent(&data);
159 XDestroyWindow(**otk::display, _focuswindow);
160 XDestroyWindow(**otk::display, _supportwindow);
164 void Screen::manageExisting()
166 unsigned int i, j, nchild;
167 Window r, p, *children;
168 XQueryTree(**otk::display, _info->rootWindow(), &r, &p,
171 // preen the window list of all icon windows... for better dockapp support
172 for (i = 0; i < nchild; i++) {
173 if (children[i] == None) continue;
175 XWMHints *wmhints = XGetWMHints(**otk::display,
179 if ((wmhints->flags & IconWindowHint) &&
180 (wmhints->icon_window != children[i])) {
181 for (j = 0; j < nchild; j++) {
182 if (children[j] == wmhints->icon_window) {
193 // manage shown windows
194 for (i = 0; i < nchild; ++i) {
195 if (children[i] == None)
198 XWindowAttributes attrib;
199 if (XGetWindowAttributes(**otk::display, children[i], &attrib)) {
200 if (attrib.override_redirect) continue;
202 if (attrib.map_state != IsUnmapped) {
203 manageWindow(children[i]);
212 void Screen::updateStrut()
214 _strut.left = _strut.right = _strut.top = _strut.bottom = 0;
216 ClientList::iterator it, end = clients.end();
217 for (it = clients.begin(); it != end; ++it) {
218 const otk::Strut &s = (*it)->strut();
219 _strut.left = std::max(_strut.left, s.left);
220 _strut.right = std::max(_strut.right, s.right);
221 _strut.top = std::max(_strut.top, s.top);
222 _strut.bottom = std::max(_strut.bottom, s.bottom);
228 void Screen::calcArea()
230 otk::Rect old_area = _area;
234 // reset to the full areas
235 if (isXineramaActive())
236 xineramaUsableArea = getXineramaAreas();
240 _area = otk::Rect(_strut.left, _strut.top,
241 _info->size().width() - (_strut.left + _strut.right),
242 _info->size().height() - (_strut.top + _strut.bottom));
246 if (isXineramaActive()) {
247 // keep each of the ximerama-defined areas inside the strut
248 RectList::iterator xit, xend = xineramaUsableArea.end();
249 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
250 if (xit->x() < usableArea.x()) {
251 xit->setX(usableArea.x());
252 xit->setWidth(xit->width() - usableArea.x());
254 if (xit->y() < usableArea.y()) {
255 xit->setY(usableArea.y());
256 xit->setHeight(xit->height() - usableArea.y());
258 if (xit->x() + xit->width() > usableArea.width())
259 xit->setWidth(usableArea.width() - xit->x());
260 if (xit->y() + xit->height() > usableArea.height())
261 xit->setHeight(usableArea.height() - xit->y());
267 if (old_area != _area) {
268 // the area has changed, adjust all the maximized windows
269 ClientList::iterator it, end = clients.end();
270 for (it = clients.begin(); it != end; ++it)
278 void Screen::changeSupportedAtoms()
280 // create the netwm support window
281 _supportwindow = XCreateSimpleWindow(**otk::display,
283 0, 0, 1, 1, 0, 0, 0);
285 // set supporting window
286 otk::Property::set(_info->rootWindow(),
287 otk::Property::atoms.net_supporting_wm_check,
288 otk::Property::atoms.window, _supportwindow);
290 //set properties on the supporting window
291 otk::Property::set(_supportwindow, otk::Property::atoms.net_wm_name,
292 otk::Property::utf8, "Openbox");
293 otk::Property::set(_supportwindow,
294 otk::Property::atoms.net_supporting_wm_check,
295 otk::Property::atoms.window, _supportwindow);
299 otk::Property::atoms.net_current_desktop,
300 otk::Property::atoms.net_number_of_desktops,
301 otk::Property::atoms.net_desktop_geometry,
302 otk::Property::atoms.net_desktop_viewport,
303 otk::Property::atoms.net_active_window,
304 otk::Property::atoms.net_workarea,
305 otk::Property::atoms.net_client_list,
306 otk::Property::atoms.net_client_list_stacking,
307 otk::Property::atoms.net_desktop_names,
308 otk::Property::atoms.net_close_window,
309 otk::Property::atoms.net_wm_name,
310 otk::Property::atoms.net_wm_visible_name,
311 otk::Property::atoms.net_wm_icon_name,
312 otk::Property::atoms.net_wm_visible_icon_name,
314 otk::Property::atoms.net_wm_desktop,
316 otk::Property::atoms.net_wm_strut,
317 otk::Property::atoms.net_wm_window_type,
318 otk::Property::atoms.net_wm_window_type_desktop,
319 otk::Property::atoms.net_wm_window_type_dock,
320 otk::Property::atoms.net_wm_window_type_toolbar,
321 otk::Property::atoms.net_wm_window_type_menu,
322 otk::Property::atoms.net_wm_window_type_utility,
323 otk::Property::atoms.net_wm_window_type_splash,
324 otk::Property::atoms.net_wm_window_type_dialog,
325 otk::Property::atoms.net_wm_window_type_normal,
327 otk::Property::atoms.net_wm_moveresize,
328 otk::Property::atoms.net_wm_moveresize_size_topleft,
329 otk::Property::atoms.net_wm_moveresize_size_topright,
330 otk::Property::atoms.net_wm_moveresize_size_bottomleft,
331 otk::Property::atoms.net_wm_moveresize_size_bottomright,
332 otk::Property::atoms.net_wm_moveresize_move,
334 otk::Property::atoms.net_wm_allowed_actions,
335 otk::Property::atoms.net_wm_action_move,
336 otk::Property::atoms.net_wm_action_resize,
337 otk::Property::atoms.net_wm_action_minimize,
338 otk::Property::atoms.net_wm_action_shade,
339 /* otk::Property::atoms.net_wm_action_stick,*/
340 otk::Property::atoms.net_wm_action_maximize_horz,
341 otk::Property::atoms.net_wm_action_maximize_vert,
342 otk::Property::atoms.net_wm_action_fullscreen,
343 otk::Property::atoms.net_wm_action_change_desktop,
344 otk::Property::atoms.net_wm_action_close,
346 otk::Property::atoms.net_wm_state,
347 otk::Property::atoms.net_wm_state_modal,
348 otk::Property::atoms.net_wm_state_maximized_vert,
349 otk::Property::atoms.net_wm_state_maximized_horz,
350 otk::Property::atoms.net_wm_state_shaded,
351 otk::Property::atoms.net_wm_state_skip_taskbar,
352 otk::Property::atoms.net_wm_state_skip_pager,
353 otk::Property::atoms.net_wm_state_hidden,
354 otk::Property::atoms.net_wm_state_fullscreen,
355 otk::Property::atoms.net_wm_state_above,
356 otk::Property::atoms.net_wm_state_below,
358 const int num_supported = sizeof(supported)/sizeof(Atom);
360 otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_supported,
361 otk::Property::atoms.atom, supported, num_supported);
365 void Screen::changeClientList()
368 unsigned int size = clients.size();
370 // create an array of the window ids
374 windows = new Window[size];
376 ClientList::const_iterator it = clients.begin();
377 const ClientList::const_iterator end = clients.end();
378 for (; it != end; ++it, ++win_it)
379 *win_it = (*it)->window();
381 windows = (Window*) 0;
383 otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_client_list,
384 otk::Property::atoms.window, windows, size);
389 changeStackingList();
393 void Screen::changeStackingList()
396 unsigned int size = _stacking.size();
398 assert(size == clients.size()); // just making sure.. :)
401 // create an array of the window ids (from bottom to top, reverse order!)
405 windows = new Window[size];
407 ClientList::const_reverse_iterator it = _stacking.rbegin();
408 const ClientList::const_reverse_iterator end = _stacking.rend();
409 for (; it != end; ++it, ++win_it)
410 *win_it = (*it)->window();
412 windows = (Window*) 0;
414 otk::Property::set(_info->rootWindow(),
415 otk::Property::atoms.net_client_list_stacking,
416 otk::Property::atoms.window, windows, size);
423 void Screen::changeWorkArea() {
424 unsigned long *dims = new unsigned long[4 * _num_desktops];
425 for (long i = 0; i < _num_desktops; ++i) {
426 dims[(i * 4) + 0] = _area.x();
427 dims[(i * 4) + 1] = _area.y();
428 dims[(i * 4) + 2] = _area.width();
429 dims[(i * 4) + 3] = _area.height();
431 otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_workarea,
432 otk::Property::atoms.cardinal, dims, 4 * _num_desktops);
437 void Screen::manageWindow(Window window)
441 XSetWindowAttributes attrib_set;
443 XWindowAttributes attrib;
445 otk::display->grab();
447 // check if it has already been unmapped by the time we started mapping
448 // the grab does a sync so we don't have to here
449 if (XCheckTypedWindowEvent(**otk::display, window, DestroyNotify, &e) ||
450 XCheckTypedWindowEvent(**otk::display, window, UnmapNotify, &e)) {
451 XPutBackEvent(**otk::display, &e);
453 otk::display->ungrab();
454 return; // don't manage it
457 if (!XGetWindowAttributes(**otk::display, window, &attrib) ||
458 attrib.override_redirect) {
459 otk::display->ungrab();
460 return; // don't manage it
463 // is the window a docking app
464 if ((wmhint = XGetWMHints(**otk::display, window))) {
465 if ((wmhint->flags & StateHint) &&
466 wmhint->initial_state == WithdrawnState) {
467 //slit->addClient(w); // XXX: make dock apps work!
469 otk::display->ungrab();
476 // choose the events we want to receive on the CLIENT window
477 attrib_set.event_mask = Client::event_mask;
478 attrib_set.do_not_propagate_mask = Client::no_propagate_mask;
479 XChangeWindowAttributes(**otk::display, window,
480 CWEventMask|CWDontPropagate, &attrib_set);
482 // create the Client class, which gets all of the hints on the window
483 client = new Client(_number, window);
484 // register for events
485 openbox->registerHandler(window, client);
486 // add to the wm's map
487 openbox->addClient(window, client);
489 // we dont want a border on the client
490 client->toggleClientBorder(false);
492 // specify that if we exit, the window should not be destroyed and should be
493 // reparented back to root automatically
494 XChangeSaveSet(**otk::display, window, SetModeInsert);
496 // create the decoration frame for the client window
497 client->frame = new Frame(client);
498 // register the plate for events (map req's)
499 // this involves removing itself from the handler list first, since it is
500 // auto added to the list, being a widget. we won't get any events on the
501 // plate except for events for the client (SubstructureRedirectMask)
502 openbox->clearHandler(client->frame->plate());
503 openbox->registerHandler(client->frame->plate(), client);
505 // add to the wm's map
506 Window *w = client->frame->allWindows();
507 for (unsigned int i = 0; w[i]; ++i)
508 openbox->addClient(w[i], client);
511 // reparent the client to the frame
512 client->frame->grabClient();
514 if (openbox->state() != Openbox::State_Starting) {
515 // position the window intelligenty .. hopefully :)
516 // call the python PLACEWINDOW binding
517 EventData data(_number, client, EventAction::PlaceWindow, 0);
518 openbox->bindings()->fireEvent(&data);
521 EventData ddata(_number, client, EventAction::DisplayingWindow, 0);
522 openbox->bindings()->fireEvent(&ddata);
524 // if on the current desktop.. (or all desktops)
525 if (client->desktop() == _desktop ||
526 client->desktop() == (signed)0xffffffff) {
527 client->frame->show();
530 client->applyStartupState();
532 otk::display->ungrab();
534 // add to the screen's list
535 clients.push_back(client);
536 // once the client is in the list, update our strut to include the new
537 // client's (it is good that this happens after window placement!)
539 // this puts into the stacking order, then raises it
540 _stacking.push_back(client);
542 // update the root properties
545 openbox->bindings()->grabButtons(true, client);
547 EventData ndata(_number, client, EventAction::NewWindow, 0);
548 openbox->bindings()->fireEvent(&ndata);
551 printf("Managed window 0x%lx frame 0x%lx\n",
552 window, client->frame->window());
557 void Screen::unmanageWindow(Client *client)
559 Frame *frame = client->frame;
561 // call the python CLOSEWINDOW binding
562 EventData data(_number, client, EventAction::CloseWindow, 0);
563 openbox->bindings()->fireEvent(&data);
565 openbox->bindings()->grabButtons(false, client);
567 // remove from the wm's map
568 openbox->removeClient(client->window());
569 Window *w = frame->allWindows();
570 for (unsigned int i = 0; w[i]; ++i)
571 openbox->addClient(w[i], client);
573 // unregister for handling events
574 openbox->clearHandler(client->window());
576 // remove the window from our save set
577 XChangeSaveSet(**otk::display, client->window(), SetModeDelete);
579 // we dont want events no more
580 XSelectInput(**otk::display, client->window(), NoEventMask);
584 // give the client its border back
585 client->toggleClientBorder(true);
587 // reparent the window out of the frame
588 frame->releaseClient();
591 Window framewin = client->frame->window();
593 delete client->frame;
596 // remove from the stacking order
597 _stacking.remove(client);
599 // remove from the screen's list
600 clients.remove(client);
602 // once the client is out of the list, update our strut to remove it's
606 // unset modal before dropping our focus
607 client->setModal(false);
609 // unfocus the client (calls the focus callbacks)
613 printf("Unmanaged window 0x%lx frame 0x%lx\n", client->window(), framewin);
618 // update the root properties
622 void Screen::lowerWindow(Client *client)
624 Window wins[2]; // only ever restack 2 windows.
626 assert(!_stacking.empty()); // this would be bad
628 ClientList::iterator it = --_stacking.end();
629 const ClientList::iterator end = _stacking.begin();
631 if (client->modal() && client->transientFor()) {
632 // don't let a modal window lower below its transient_for
633 it = std::find(_stacking.begin(), _stacking.end(), client->transientFor());
634 assert(it != _stacking.end());
636 wins[0] = (it == _stacking.begin() ? _focuswindow :
637 ((*(--ClientList::const_iterator(it)))->frame->window()));
638 wins[1] = client->frame->window();
639 if (wins[0] == wins[1]) return; // already right above the window
641 _stacking.remove(client);
642 _stacking.insert(it, client);
644 for (; it != end && (*it)->layer() < client->layer(); --it);
645 if (*it == client) return; // already the bottom, return
647 wins[0] = (*it)->frame->window();
648 wins[1] = client->frame->window();
650 _stacking.remove(client);
651 _stacking.insert(++it, client);
654 XRestackWindows(**otk::display, wins, 2);
655 changeStackingList();
658 void Screen::raiseWindow(Client *client)
660 Window wins[2]; // only ever restack 2 windows.
662 assert(!_stacking.empty()); // this would be bad
664 // remove the client before looking so we can't run into ourselves
665 _stacking.remove(client);
667 ClientList::iterator it = _stacking.begin();
668 const ClientList::iterator end = _stacking.end();
670 // the stacking list is from highest to lowest
671 for (; it != end && (*it)->layer() > client->layer(); ++it);
674 if our new position is the top, we want to stack under the _focuswindow
675 otherwise, we want to stack under the previous window in the stack.
677 wins[0] = (it == _stacking.begin() ? _focuswindow :
678 ((*(--ClientList::const_iterator(it)))->frame->window()));
679 wins[1] = client->frame->window();
681 _stacking.insert(it, client);
683 XRestackWindows(**otk::display, wins, 2);
685 // if the window has a modal child, then raise it after us to put it on top
686 if (client->modalChild())
687 raiseWindow(client->modalChild());
689 changeStackingList(); // no need to do this twice!
692 void Screen::changeDesktop(long desktop)
694 if (!(desktop >= 0 && desktop < _num_desktops)) return;
696 printf("Moving to desktop %ld\n", desktop);
701 otk::Property::set(_info->rootWindow(),
702 otk::Property::atoms.net_current_desktop,
703 otk::Property::atoms.cardinal, _desktop);
705 if (old == _desktop) return;
707 ClientList::iterator it, end = clients.end();
708 for (it = clients.begin(); it != end; ++it) {
709 if ((*it)->desktop() == old) {
710 (*it)->frame->hide();
711 } else if ((*it)->desktop() == _desktop) {
712 (*it)->frame->show();
716 // force the callbacks to fire
717 if (!openbox->focusedClient())
718 openbox->setFocusedClient(0);
721 void Screen::changeNumDesktops(long num)
725 if (!(num > 0)) return;
727 // move windows on desktops that will no longer exist!
728 ClientList::iterator it, end = clients.end();
729 for (it = clients.begin(); it != end; ++it) {
730 int d = (*it)->desktop();
731 if (d >= num && !(d == (signed) 0xffffffff ||
732 d == Client::ICONIC_DESKTOP)) {
734 ce.xclient.type = ClientMessage;
735 ce.xclient.message_type = otk::Property::atoms.net_wm_desktop;
736 ce.xclient.display = **otk::display;
737 ce.xclient.window = (*it)->window();
738 ce.xclient.format = 32;
739 ce.xclient.data.l[0] = num - 1;
740 XSendEvent(**otk::display, _info->rootWindow(), False,
741 SubstructureNotifyMask | SubstructureRedirectMask, &ce);
746 otk::Property::set(_info->rootWindow(),
747 otk::Property::atoms.net_number_of_desktops,
748 otk::Property::atoms.cardinal, _num_desktops);
750 // set the viewport hint
751 unsigned long *viewport = new unsigned long[_num_desktops * 2];
752 memset(viewport, 0, sizeof(unsigned long) * _num_desktops * 2);
753 otk::Property::set(_info->rootWindow(),
754 otk::Property::atoms.net_desktop_viewport,
755 otk::Property::atoms.cardinal,
756 viewport, _num_desktops * 2);
759 // update the work area hint
762 // change our desktop if we're on one that no longer exists!
764 changeDesktop(num - 1);
768 void Screen::updateDesktopNames()
770 unsigned long num = (unsigned) -1;
772 if (!otk::Property::get(_info->rootWindow(),
773 otk::Property::atoms.net_desktop_names,
774 otk::Property::utf8, &num, &_desktop_names))
775 _desktop_names.clear();
776 while ((long)_desktop_names.size() < _num_desktops)
777 _desktop_names.push_back("Unnamed");
781 void Screen::setDesktopName(long i, const otk::ustring &name)
785 if (i >= _num_desktops) return;
787 otk::Property::StringVect newnames = _desktop_names;
789 otk::Property::set(_info->rootWindow(),
790 otk::Property::atoms.net_desktop_names,
791 otk::Property::utf8, newnames);
795 void Screen::installColormap(bool install) const
798 XInstallColormap(**otk::display, _info->colormap());
800 XUninstallColormap(**otk::display, _info->colormap());
804 void Screen::propertyHandler(const XPropertyEvent &e)
806 otk::EventHandler::propertyHandler(e);
808 // compress changes to a single property into a single change
810 while (XCheckTypedWindowEvent(**otk::display, _info->rootWindow(),
812 // XXX: it would be nice to compress ALL changes to a property, not just
813 // changes in a row without other props between.
814 if (ce.xproperty.atom != e.atom) {
815 XPutBackEvent(**otk::display, &ce);
820 if (e.atom == otk::Property::atoms.net_desktop_names)
821 updateDesktopNames();
825 void Screen::clientMessageHandler(const XClientMessageEvent &e)
827 otk::EventHandler::clientMessageHandler(e);
829 if (e.format != 32) return;
831 if (e.message_type == otk::Property::atoms.net_current_desktop) {
832 changeDesktop(e.data.l[0]);
833 } else if (e.message_type == otk::Property::atoms.net_number_of_desktops) {
834 changeNumDesktops(e.data.l[0]);
839 void Screen::mapRequestHandler(const XMapRequestEvent &e)
841 otk::EventHandler::mapRequestHandler(e);
844 printf("MapRequest for 0x%lx\n", e.window);
847 Client *c = openbox->findClient(e.window);
850 printf("DEBUG: MAP REQUEST CAUGHT IN SCREEN. IGNORED.\n");
853 manageWindow(e.window);