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)
52 assert(screen >= 0); assert(screen < ScreenCount(**otk::display));
53 _info = otk::display->screenInfo(screen);
56 XErrorHandler old = XSetErrorHandler(::anotherWMRunning);
57 XSelectInput(**otk::display, _info->rootWindow(),
59 XSync(**otk::display, false);
60 XSetErrorHandler(old);
62 _managed = !::running;
63 if (! _managed) return; // was unable to manage the screen
66 printf(_("Managing screen %d: visual 0x%lx, depth %d\n"),
67 _number, XVisualIDFromVisual(_info->visual()), _info->depth());
70 otk::Property::set(_info->rootWindow(), otk::Property::atoms.openbox_pid,
71 otk::Property::atoms.cardinal, (unsigned long) getpid());
73 // set the mouse cursor for the root window (the default cursor)
74 XDefineCursor(**otk::display, _info->rootWindow(),
75 openbox->cursors().session);
77 // XXX: initialize the screen's style
79 otk::ustring stylepath;
80 python_get_string("THEME", &stylepath);
81 otk::Configuration sconfig(false);
82 sconfig.setFile(otk::expandTilde(stylepath.c_str()));
83 if (!sconfig.load()) {
84 sconfig.setFile(otk::expandTilde(DEFAULTSTYLE));
85 if (!sconfig.load()) {
86 printf(_("Unable to load default style: %s. Aborting.\n"), DEFAULTSTYLE);
92 otk::display->renderControl(_number)->
93 drawRoot(*otk::RenderStyle::style(_number)->rootColor());
95 // set up notification of netwm support
96 changeSupportedAtoms();
98 // Set the netwm properties for geometry
99 unsigned long geometry[] = { _info->size().width(),
100 _info->size().height() };
101 otk::Property::set(_info->rootWindow(),
102 otk::Property::atoms.net_desktop_geometry,
103 otk::Property::atoms.cardinal, geometry, 2);
105 // Set the net_desktop_names property
106 std::vector<otk::ustring> names;
107 python_get_stringlist("DESKTOP_NAMES", &names);
108 otk::Property::set(_info->rootWindow(),
109 otk::Property::atoms.net_desktop_names,
110 otk::Property::utf8, names);
111 // the above set() will cause the updateDesktopNames to fire right away so
112 // we have a list of desktop names
116 if (!python_get_long("NUMBER_OF_DESKTOPS", &_num_desktops))
118 changeNumDesktops(_num_desktops); // set the hint
120 changeDesktop(0); // set the hint
122 // create the window which gets focus when no clients get it
123 XSetWindowAttributes attr;
124 attr.override_redirect = true;
125 _focuswindow = XCreateWindow(**otk::display, _info->rootWindow(),
126 -100, -100, 1, 1, 0, 0, InputOnly,
127 _info->visual(), CWOverrideRedirect, &attr);
128 XMapRaised(**otk::display, _focuswindow);
130 // these may be further updated if any pre-existing windows are found in
131 // the manageExising() function
132 changeClientList(); // initialize the client lists, which will be empty
133 calcArea(); // initialize the available working area
135 // register this class as the event handler for the root window
136 openbox->registerHandler(_info->rootWindow(), this);
138 // call the python Startup callbacks
139 EventData data(_number, 0, EventAction::Startup, 0);
140 openbox->bindings()->fireEvent(&data);
146 if (! _managed) return;
148 XSelectInput(**otk::display, _info->rootWindow(), NoEventMask);
150 // unmanage all windows
151 while (!clients.empty())
152 unmanageWindow(clients.front());
154 // call the python Shutdown callbacks
155 EventData data(_number, 0, EventAction::Shutdown, 0);
156 openbox->bindings()->fireEvent(&data);
158 XDestroyWindow(**otk::display, _focuswindow);
159 XDestroyWindow(**otk::display, _supportwindow);
163 void Screen::manageExisting()
165 unsigned int i, j, nchild;
166 Window r, p, *children;
167 XQueryTree(**otk::display, _info->rootWindow(), &r, &p,
170 // preen the window list of all icon windows... for better dockapp support
171 for (i = 0; i < nchild; i++) {
172 if (children[i] == None) continue;
174 XWMHints *wmhints = XGetWMHints(**otk::display,
178 if ((wmhints->flags & IconWindowHint) &&
179 (wmhints->icon_window != children[i])) {
180 for (j = 0; j < nchild; j++) {
181 if (children[j] == wmhints->icon_window) {
192 // manage shown windows
193 for (i = 0; i < nchild; ++i) {
194 if (children[i] == None)
197 XWindowAttributes attrib;
198 if (XGetWindowAttributes(**otk::display, children[i], &attrib)) {
199 if (attrib.override_redirect) continue;
201 if (attrib.map_state != IsUnmapped) {
202 manageWindow(children[i]);
211 void Screen::updateStrut()
213 _strut.left = _strut.right = _strut.top = _strut.bottom = 0;
215 ClientList::iterator it, end = clients.end();
216 for (it = clients.begin(); it != end; ++it) {
217 const otk::Strut &s = (*it)->strut();
218 _strut.left = std::max(_strut.left, s.left);
219 _strut.right = std::max(_strut.right, s.right);
220 _strut.top = std::max(_strut.top, s.top);
221 _strut.bottom = std::max(_strut.bottom, s.bottom);
227 void Screen::calcArea()
229 otk::Rect old_area = _area;
233 // reset to the full areas
234 if (isXineramaActive())
235 xineramaUsableArea = getXineramaAreas();
239 _area = otk::Rect(_strut.left, _strut.top,
240 _info->size().width() - (_strut.left + _strut.right),
241 _info->size().height() - (_strut.top + _strut.bottom));
245 if (isXineramaActive()) {
246 // keep each of the ximerama-defined areas inside the strut
247 RectList::iterator xit, xend = xineramaUsableArea.end();
248 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
249 if (xit->x() < usableArea.x()) {
250 xit->setX(usableArea.x());
251 xit->setWidth(xit->width() - usableArea.x());
253 if (xit->y() < usableArea.y()) {
254 xit->setY(usableArea.y());
255 xit->setHeight(xit->height() - usableArea.y());
257 if (xit->x() + xit->width() > usableArea.width())
258 xit->setWidth(usableArea.width() - xit->x());
259 if (xit->y() + xit->height() > usableArea.height())
260 xit->setHeight(usableArea.height() - xit->y());
266 if (old_area != _area) {
267 // the area has changed, adjust all the maximized windows
268 ClientList::iterator it, end = clients.end();
269 for (it = clients.begin(); it != end; ++it)
277 void Screen::changeSupportedAtoms()
279 // create the netwm support window
280 _supportwindow = XCreateSimpleWindow(**otk::display,
282 0, 0, 1, 1, 0, 0, 0);
284 // set supporting window
285 otk::Property::set(_info->rootWindow(),
286 otk::Property::atoms.net_supporting_wm_check,
287 otk::Property::atoms.window, _supportwindow);
289 //set properties on the supporting window
290 otk::Property::set(_supportwindow, otk::Property::atoms.net_wm_name,
291 otk::Property::utf8, "Openbox");
292 otk::Property::set(_supportwindow,
293 otk::Property::atoms.net_supporting_wm_check,
294 otk::Property::atoms.window, _supportwindow);
298 otk::Property::atoms.net_current_desktop,
299 otk::Property::atoms.net_number_of_desktops,
300 otk::Property::atoms.net_desktop_geometry,
301 otk::Property::atoms.net_desktop_viewport,
302 otk::Property::atoms.net_active_window,
303 otk::Property::atoms.net_workarea,
304 otk::Property::atoms.net_client_list,
305 otk::Property::atoms.net_client_list_stacking,
306 otk::Property::atoms.net_desktop_names,
307 otk::Property::atoms.net_close_window,
308 otk::Property::atoms.net_wm_name,
309 otk::Property::atoms.net_wm_visible_name,
310 otk::Property::atoms.net_wm_icon_name,
311 otk::Property::atoms.net_wm_visible_icon_name,
313 otk::Property::atoms.net_wm_desktop,
315 otk::Property::atoms.net_wm_strut,
316 otk::Property::atoms.net_wm_window_type,
317 otk::Property::atoms.net_wm_window_type_desktop,
318 otk::Property::atoms.net_wm_window_type_dock,
319 otk::Property::atoms.net_wm_window_type_toolbar,
320 otk::Property::atoms.net_wm_window_type_menu,
321 otk::Property::atoms.net_wm_window_type_utility,
322 otk::Property::atoms.net_wm_window_type_splash,
323 otk::Property::atoms.net_wm_window_type_dialog,
324 otk::Property::atoms.net_wm_window_type_normal,
326 otk::Property::atoms.net_wm_moveresize,
327 otk::Property::atoms.net_wm_moveresize_size_topleft,
328 otk::Property::atoms.net_wm_moveresize_size_topright,
329 otk::Property::atoms.net_wm_moveresize_size_bottomleft,
330 otk::Property::atoms.net_wm_moveresize_size_bottomright,
331 otk::Property::atoms.net_wm_moveresize_move,
333 otk::Property::atoms.net_wm_allowed_actions,
334 otk::Property::atoms.net_wm_action_move,
335 otk::Property::atoms.net_wm_action_resize,
336 otk::Property::atoms.net_wm_action_minimize,
337 otk::Property::atoms.net_wm_action_shade,
338 /* otk::Property::atoms.net_wm_action_stick,*/
339 otk::Property::atoms.net_wm_action_maximize_horz,
340 otk::Property::atoms.net_wm_action_maximize_vert,
341 otk::Property::atoms.net_wm_action_fullscreen,
342 otk::Property::atoms.net_wm_action_change_desktop,
343 otk::Property::atoms.net_wm_action_close,
345 otk::Property::atoms.net_wm_state,
346 otk::Property::atoms.net_wm_state_modal,
347 otk::Property::atoms.net_wm_state_maximized_vert,
348 otk::Property::atoms.net_wm_state_maximized_horz,
349 otk::Property::atoms.net_wm_state_shaded,
350 otk::Property::atoms.net_wm_state_skip_taskbar,
351 otk::Property::atoms.net_wm_state_skip_pager,
352 otk::Property::atoms.net_wm_state_hidden,
353 otk::Property::atoms.net_wm_state_fullscreen,
354 otk::Property::atoms.net_wm_state_above,
355 otk::Property::atoms.net_wm_state_below,
357 const int num_supported = sizeof(supported)/sizeof(Atom);
359 otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_supported,
360 otk::Property::atoms.atom, supported, num_supported);
364 void Screen::changeClientList()
367 unsigned int size = clients.size();
369 // create an array of the window ids
373 windows = new Window[size];
375 ClientList::const_iterator it = clients.begin();
376 const ClientList::const_iterator end = clients.end();
377 for (; it != end; ++it, ++win_it)
378 *win_it = (*it)->window();
380 windows = (Window*) 0;
382 otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_client_list,
383 otk::Property::atoms.window, windows, size);
388 changeStackingList();
392 void Screen::changeStackingList()
395 unsigned int size = _stacking.size();
397 assert(size == clients.size()); // just making sure.. :)
400 // create an array of the window ids (from bottom to top, reverse order!)
404 windows = new Window[size];
406 ClientList::const_reverse_iterator it = _stacking.rbegin();
407 const ClientList::const_reverse_iterator end = _stacking.rend();
408 for (; it != end; ++it, ++win_it)
409 *win_it = (*it)->window();
411 windows = (Window*) 0;
413 otk::Property::set(_info->rootWindow(),
414 otk::Property::atoms.net_client_list_stacking,
415 otk::Property::atoms.window, windows, size);
422 void Screen::changeWorkArea() {
423 unsigned long *dims = new unsigned long[4 * _num_desktops];
424 for (long i = 0; i < _num_desktops; ++i) {
425 dims[(i * 4) + 0] = _area.x();
426 dims[(i * 4) + 1] = _area.y();
427 dims[(i * 4) + 2] = _area.width();
428 dims[(i * 4) + 3] = _area.height();
430 otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_workarea,
431 otk::Property::atoms.cardinal, dims, 4 * _num_desktops);
436 void Screen::manageWindow(Window window)
440 XSetWindowAttributes attrib_set;
442 XWindowAttributes attrib;
444 otk::display->grab();
446 // check if it has already been unmapped by the time we started mapping
447 // the grab does a sync so we don't have to here
448 if (XCheckTypedWindowEvent(**otk::display, window, DestroyNotify, &e) ||
449 XCheckTypedWindowEvent(**otk::display, window, UnmapNotify, &e)) {
450 XPutBackEvent(**otk::display, &e);
452 otk::display->ungrab();
453 return; // don't manage it
456 if (!XGetWindowAttributes(**otk::display, window, &attrib) ||
457 attrib.override_redirect) {
458 otk::display->ungrab();
459 return; // don't manage it
462 // is the window a docking app
463 if ((wmhint = XGetWMHints(**otk::display, window))) {
464 if ((wmhint->flags & StateHint) &&
465 wmhint->initial_state == WithdrawnState) {
466 //slit->addClient(w); // XXX: make dock apps work!
468 otk::display->ungrab();
475 // choose the events we want to receive on the CLIENT window
476 attrib_set.event_mask = Client::event_mask;
477 attrib_set.do_not_propagate_mask = Client::no_propagate_mask;
478 XChangeWindowAttributes(**otk::display, window,
479 CWEventMask|CWDontPropagate, &attrib_set);
481 // create the Client class, which gets all of the hints on the window
482 client = new Client(_number, window);
483 // register for events
484 openbox->registerHandler(window, client);
485 // add to the wm's map
486 openbox->addClient(window, client);
488 // we dont want a border on the client
489 client->toggleClientBorder(false);
491 // specify that if we exit, the window should not be destroyed and should be
492 // reparented back to root automatically
493 XChangeSaveSet(**otk::display, window, SetModeInsert);
495 // create the decoration frame for the client window
496 client->frame = new Frame(client);
497 // register the plate for events (map req's)
498 // this involves removing itself from the handler list first, since it is
499 // auto added to the list, being a widget. we won't get any events on the
500 // plate except for events for the client (SubstructureRedirectMask)
501 openbox->clearHandler(client->frame->plate());
502 openbox->registerHandler(client->frame->plate(), client);
504 // add to the wm's map
505 Window *w = client->frame->allWindows();
506 for (unsigned int i = 0; w[i]; ++i)
507 openbox->addClient(w[i], client);
510 // reparent the client to the frame
511 client->frame->grabClient();
513 if (openbox->state() != Openbox::State_Starting) {
514 // position the window intelligenty .. hopefully :)
515 // call the python PLACEWINDOW binding
516 EventData data(_number, client, EventAction::PlaceWindow, 0);
517 openbox->bindings()->fireEvent(&data);
520 EventData ddata(_number, client, EventAction::DisplayingWindow, 0);
521 openbox->bindings()->fireEvent(&ddata);
523 // if on the current desktop.. (or all desktops)
524 if (client->desktop() == _desktop ||
525 client->desktop() == (signed)0xffffffff) {
526 client->frame->show();
529 client->applyStartupState();
531 otk::display->ungrab();
533 // add to the screen's list
534 clients.push_back(client);
535 // once the client is in the list, update our strut to include the new
536 // client's (it is good that this happens after window placement!)
538 // this puts into the stacking order, then raises it
539 _stacking.push_back(client);
541 // update the root properties
544 openbox->bindings()->grabButtons(true, client);
546 EventData ndata(_number, client, EventAction::NewWindow, 0);
547 openbox->bindings()->fireEvent(&ndata);
550 printf("Managed window 0x%lx frame 0x%lx\n",
551 window, client->frame->window());
556 void Screen::unmanageWindow(Client *client)
558 Frame *frame = client->frame;
560 // call the python CLOSEWINDOW binding
561 EventData data(_number, client, EventAction::CloseWindow, 0);
562 openbox->bindings()->fireEvent(&data);
564 openbox->bindings()->grabButtons(false, client);
566 // remove from the wm's map
567 openbox->removeClient(client->window());
568 Window *w = frame->allWindows();
569 for (unsigned int i = 0; w[i]; ++i)
570 openbox->addClient(w[i], client);
572 // unregister for handling events
573 openbox->clearHandler(client->window());
575 // remove the window from our save set
576 XChangeSaveSet(**otk::display, client->window(), SetModeDelete);
578 // we dont want events no more
579 XSelectInput(**otk::display, client->window(), NoEventMask);
583 // give the client its border back
584 client->toggleClientBorder(true);
586 // reparent the window out of the frame
587 frame->releaseClient();
590 Window framewin = client->frame->window();
592 delete client->frame;
595 // remove from the stacking order
596 _stacking.remove(client);
598 // remove from the screen's list
599 clients.remove(client);
601 // once the client is out of the list, update our strut to remove it's
605 // unset modal before dropping our focus
606 client->setModal(false);
608 // unfocus the client (calls the focus callbacks)
612 printf("Unmanaged window 0x%lx frame 0x%lx\n", client->window(), framewin);
617 // update the root properties
621 void Screen::lowerWindow(Client *client)
623 Window wins[2]; // only ever restack 2 windows.
625 assert(!_stacking.empty()); // this would be bad
627 ClientList::iterator it = --_stacking.end();
628 const ClientList::iterator end = _stacking.begin();
630 if (client->modal() && client->transientFor()) {
631 // don't let a modal window lower below its transient_for
632 it = std::find(_stacking.begin(), _stacking.end(), client->transientFor());
633 assert(it != _stacking.end());
635 wins[0] = (it == _stacking.begin() ? _focuswindow :
636 ((*(--ClientList::const_iterator(it)))->frame->window()));
637 wins[1] = client->frame->window();
638 if (wins[0] == wins[1]) return; // already right above the window
640 _stacking.remove(client);
641 _stacking.insert(it, client);
643 for (; it != end && (*it)->layer() < client->layer(); --it);
644 if (*it == client) return; // already the bottom, return
646 wins[0] = (*it)->frame->window();
647 wins[1] = client->frame->window();
649 _stacking.remove(client);
650 _stacking.insert(++it, client);
653 XRestackWindows(**otk::display, wins, 2);
654 changeStackingList();
657 void Screen::raiseWindow(Client *client)
659 Window wins[2]; // only ever restack 2 windows.
661 assert(!_stacking.empty()); // this would be bad
663 // remove the client before looking so we can't run into ourselves
664 _stacking.remove(client);
666 ClientList::iterator it = _stacking.begin();
667 const ClientList::iterator end = _stacking.end();
669 // the stacking list is from highest to lowest
670 for (; it != end && (*it)->layer() > client->layer(); ++it);
673 if our new position is the top, we want to stack under the _focuswindow
674 otherwise, we want to stack under the previous window in the stack.
676 wins[0] = (it == _stacking.begin() ? _focuswindow :
677 ((*(--ClientList::const_iterator(it)))->frame->window()));
678 wins[1] = client->frame->window();
680 _stacking.insert(it, client);
682 XRestackWindows(**otk::display, wins, 2);
684 // if the window has a modal child, then raise it after us to put it on top
685 if (client->modalChild())
686 raiseWindow(client->modalChild());
688 changeStackingList(); // no need to do this twice!
691 void Screen::changeDesktop(long desktop)
693 if (!(desktop >= 0 && desktop < _num_desktops)) return;
695 printf("Moving to desktop %ld\n", desktop);
700 otk::Property::set(_info->rootWindow(),
701 otk::Property::atoms.net_current_desktop,
702 otk::Property::atoms.cardinal, _desktop);
704 if (old == _desktop) return;
706 ClientList::iterator it, end = clients.end();
707 for (it = clients.begin(); it != end; ++it) {
708 if ((*it)->desktop() == old) {
709 (*it)->frame->hide();
710 } else if ((*it)->desktop() == _desktop) {
711 (*it)->frame->show();
715 // force the callbacks to fire
716 if (!openbox->focusedClient())
717 openbox->setFocusedClient(0);
720 void Screen::changeNumDesktops(long num)
724 if (!(num > 0)) return;
726 // move windows on desktops that will no longer exist!
727 ClientList::iterator it, end = clients.end();
728 for (it = clients.begin(); it != end; ++it) {
729 int d = (*it)->desktop();
730 if (d >= num && !(d == (signed) 0xffffffff ||
731 d == Client::ICONIC_DESKTOP)) {
733 ce.xclient.type = ClientMessage;
734 ce.xclient.message_type = otk::Property::atoms.net_wm_desktop;
735 ce.xclient.display = **otk::display;
736 ce.xclient.window = (*it)->window();
737 ce.xclient.format = 32;
738 ce.xclient.data.l[0] = num - 1;
739 XSendEvent(**otk::display, _info->rootWindow(), False,
740 SubstructureNotifyMask | SubstructureRedirectMask, &ce);
745 otk::Property::set(_info->rootWindow(),
746 otk::Property::atoms.net_number_of_desktops,
747 otk::Property::atoms.cardinal, _num_desktops);
749 // set the viewport hint
750 unsigned long *viewport = new unsigned long[_num_desktops * 2];
751 memset(viewport, 0, sizeof(unsigned long) * _num_desktops * 2);
752 otk::Property::set(_info->rootWindow(),
753 otk::Property::atoms.net_desktop_viewport,
754 otk::Property::atoms.cardinal,
755 viewport, _num_desktops * 2);
758 // update the work area hint
761 // change our desktop if we're on one that no longer exists!
763 changeDesktop(num - 1);
767 void Screen::updateDesktopNames()
769 unsigned long num = (unsigned) -1;
771 if (!otk::Property::get(_info->rootWindow(),
772 otk::Property::atoms.net_desktop_names,
773 otk::Property::utf8, &num, &_desktop_names))
774 _desktop_names.clear();
775 while ((long)_desktop_names.size() < _num_desktops)
776 _desktop_names.push_back("Unnamed");
780 void Screen::setDesktopName(long i, const otk::ustring &name)
784 if (i >= _num_desktops) return;
786 otk::Property::StringVect newnames = _desktop_names;
788 otk::Property::set(_info->rootWindow(),
789 otk::Property::atoms.net_desktop_names,
790 otk::Property::utf8, newnames);
794 void Screen::installColormap(bool install) const
797 XInstallColormap(**otk::display, _info->colormap());
799 XUninstallColormap(**otk::display, _info->colormap());
803 void Screen::propertyHandler(const XPropertyEvent &e)
805 otk::EventHandler::propertyHandler(e);
807 // compress changes to a single property into a single change
809 while (XCheckTypedWindowEvent(**otk::display, _info->rootWindow(),
811 // XXX: it would be nice to compress ALL changes to a property, not just
812 // changes in a row without other props between.
813 if (ce.xproperty.atom != e.atom) {
814 XPutBackEvent(**otk::display, &ce);
819 if (e.atom == otk::Property::atoms.net_desktop_names)
820 updateDesktopNames();
824 void Screen::clientMessageHandler(const XClientMessageEvent &e)
826 otk::EventHandler::clientMessageHandler(e);
828 if (e.format != 32) return;
830 if (e.message_type == otk::Property::atoms.net_current_desktop) {
831 changeDesktop(e.data.l[0]);
832 } else if (e.message_type == otk::Property::atoms.net_number_of_desktops) {
833 changeNumDesktops(e.data.l[0]);
838 void Screen::mapRequestHandler(const XMapRequestEvent &e)
840 otk::EventHandler::mapRequestHandler(e);
843 printf("MapRequest for 0x%lx\n", e.window);
846 Client *c = openbox->findClient(e.window);
849 printf("DEBUG: MAP REQUEST CAUGHT IN SCREEN. IGNORED.\n");
852 manageWindow(e.window);