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),
54 assert(screen >= 0); assert(screen < ScreenCount(**otk::display));
55 _info = otk::display->screenInfo(screen);
58 XErrorHandler old = XSetErrorHandler(::anotherWMRunning);
59 XSelectInput(**otk::display, _info->rootWindow(),
61 XSync(**otk::display, false);
62 XSetErrorHandler(old);
64 _managed = !::running;
65 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());
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)->drawRoot(*_style.rootColor());
94 // set up notification of netwm support
95 changeSupportedAtoms();
97 // Set the netwm properties for geometry
98 unsigned long geometry[] = { _info->width(),
100 otk::Property::set(_info->rootWindow(),
101 otk::Property::atoms.net_desktop_geometry,
102 otk::Property::atoms.cardinal, geometry, 2);
104 // Set the net_desktop_names property
105 std::vector<otk::ustring> names;
106 python_get_stringlist("desktop_names", &names);
107 otk::Property::set(_info->rootWindow(),
108 otk::Property::atoms.net_desktop_names,
109 otk::Property::utf8, names);
110 // the above set() will cause the updateDesktopNames to fire right away so
111 // we have a list of desktop names
113 if (!python_get_long("number_of_desktops", &_num_desktops))
115 changeNumDesktops(_num_desktops); // set the hint
118 changeDesktop(0); // set the hint
120 // create the window which gets focus when no clients get it
121 XSetWindowAttributes attr;
122 attr.override_redirect = true;
123 _focuswindow = XCreateWindow(**otk::display, _info->rootWindow(),
124 -100, -100, 1, 1, 0, 0, InputOnly,
125 _info->visual(), CWOverrideRedirect, &attr);
126 XMapRaised(**otk::display, _focuswindow);
128 // these may be further updated if any pre-existing windows are found in
129 // the manageExising() function
130 changeClientList(); // initialize the client lists, which will be empty
131 calcArea(); // initialize the available working area
133 // register this class as the event handler for the root window
134 openbox->registerHandler(_info->rootWindow(), this);
136 // call the python Startup callbacks
137 EventData data(_number, 0, EventAction::Startup, 0);
138 openbox->bindings()->fireEvent(&data);
144 if (! _managed) return;
146 XSelectInput(**otk::display, _info->rootWindow(), NoEventMask);
148 // unmanage all windows
149 while (!clients.empty())
150 unmanageWindow(clients.front());
152 // call the python Shutdown callbacks
153 EventData data(_number, 0, EventAction::Shutdown, 0);
154 openbox->bindings()->fireEvent(&data);
156 XDestroyWindow(**otk::display, _focuswindow);
157 XDestroyWindow(**otk::display, _supportwindow);
161 void Screen::manageExisting()
163 unsigned int i, j, nchild;
164 Window r, p, *children;
165 XQueryTree(**otk::display, _info->rootWindow(), &r, &p,
168 // preen the window list of all icon windows... for better dockapp support
169 for (i = 0; i < nchild; i++) {
170 if (children[i] == None) continue;
172 XWMHints *wmhints = XGetWMHints(**otk::display,
176 if ((wmhints->flags & IconWindowHint) &&
177 (wmhints->icon_window != children[i])) {
178 for (j = 0; j < nchild; j++) {
179 if (children[j] == wmhints->icon_window) {
190 // manage shown windows
191 for (i = 0; i < nchild; ++i) {
192 if (children[i] == None)
195 XWindowAttributes attrib;
196 if (XGetWindowAttributes(**otk::display, children[i], &attrib)) {
197 if (attrib.override_redirect) continue;
199 if (attrib.map_state != IsUnmapped) {
200 manageWindow(children[i]);
209 void Screen::updateStrut()
211 otk::Strut old = _strut;
212 _strut.left = _strut.right = _strut.top = _strut.bottom = 0;
214 Client::List::iterator it, end = clients.end();
215 for (it = clients.begin(); it != end; ++it) {
216 const otk::Strut &s = (*it)->strut();
217 _strut.left = std::max(_strut.left, s.left);
218 _strut.right = std::max(_strut.right, s.right);
219 _strut.top = std::max(_strut.top, s.top);
220 _strut.bottom = std::max(_strut.bottom, s.bottom);
224 if (!(old == _strut)) {
225 // the strut has changed, adjust all the maximized windows
226 for (it = clients.begin(); it != end; ++it)
232 void Screen::calcArea()
234 // otk::Rect old_area = _area;
238 // reset to the full areas
239 if (isXineramaActive())
240 xineramaUsableArea = getXineramaAreas();
244 _area.setRect(_strut.left, _strut.top,
245 _info->width() - (_strut.left + _strut.right),
246 _info->height() - (_strut.top + _strut.bottom));
250 if (isXineramaActive()) {
251 // keep each of the ximerama-defined areas inside the strut
252 RectList::iterator xit, xend = xineramaUsableArea.end();
253 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
254 if (xit->x() < usableArea.x()) {
255 xit->setX(usableArea.x());
256 xit->setWidth(xit->width() - usableArea.x());
258 if (xit->y() < usableArea.y()) {
259 xit->setY(usableArea.y());
260 xit->setHeight(xit->height() - usableArea.y());
262 if (xit->x() + xit->width() > usableArea.width())
263 xit->setWidth(usableArea.width() - xit->x());
264 if (xit->y() + xit->height() > usableArea.height())
265 xit->setHeight(usableArea.height() - xit->y());
271 //if (old_area != _area)
272 // XXX: re-maximize windows
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 Client::List::const_iterator it = clients.begin();
377 const Client::List::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 Client::List::const_reverse_iterator it = _stacking.rbegin();
408 const Client::List::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 // XXX: this could be different for each workspace
427 dims[(i * 4) + 0] = _area.x();
428 dims[(i * 4) + 1] = _area.y();
429 dims[(i * 4) + 2] = _area.width();
430 dims[(i * 4) + 3] = _area.height();
432 otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_workarea,
433 otk::Property::atoms.cardinal, dims, 4 * _num_desktops);
438 void Screen::manageWindow(Window window)
442 XSetWindowAttributes attrib_set;
444 XWindowAttributes attrib;
446 otk::display->grab();
448 // check if it has already been unmapped by the time we started mapping
449 // the grab does a sync so we don't have to here
450 if (XCheckTypedWindowEvent(**otk::display, window, DestroyNotify, &e) ||
451 XCheckTypedWindowEvent(**otk::display, window, UnmapNotify, &e)) {
452 XPutBackEvent(**otk::display, &e);
454 otk::display->ungrab();
455 return; // don't manage it
458 if (!XGetWindowAttributes(**otk::display, window, &attrib) ||
459 attrib.override_redirect) {
460 otk::display->ungrab();
461 return; // don't manage it
464 // is the window a docking app
465 if ((wmhint = XGetWMHints(**otk::display, window))) {
466 if ((wmhint->flags & StateHint) &&
467 wmhint->initial_state == WithdrawnState) {
468 //slit->addClient(w); // XXX: make dock apps work!
470 otk::display->ungrab();
477 // choose the events we want to receive on the CLIENT window
478 attrib_set.event_mask = Client::event_mask;
479 attrib_set.do_not_propagate_mask = Client::no_propagate_mask;
480 XChangeWindowAttributes(**otk::display, window,
481 CWEventMask|CWDontPropagate, &attrib_set);
483 // create the Client class, which gets all of the hints on the window
484 client = new Client(_number, window);
485 // register for events
486 openbox->registerHandler(window, client);
487 // add to the wm's map
488 openbox->addClient(window, client);
490 // we dont want a border on the client
491 client->toggleClientBorder(false);
493 // specify that if we exit, the window should not be destroyed and should be
494 // reparented back to root automatically
495 XChangeSaveSet(**otk::display, window, SetModeInsert);
497 // create the decoration frame for the client window
498 client->frame = new Frame(client, &_style);
499 // register the plate for events (map req's)
500 // this involves removing itself from the handler list first, since it is
501 // auto added to the list, being a widget. we won't get any events on the
502 // plate except for events for the client (SubstructureRedirectMask)
503 openbox->clearHandler(client->frame->plate());
504 openbox->registerHandler(client->frame->plate(), client);
506 // add to the wm's map
507 openbox->addClient(client->frame->window(), client);
508 openbox->addClient(client->frame->plate(), client);
509 openbox->addClient(client->frame->titlebar(), client);
510 openbox->addClient(client->frame->label(), client);
511 openbox->addClient(client->frame->button_max(), client);
512 openbox->addClient(client->frame->button_iconify(), client);
513 openbox->addClient(client->frame->button_alldesk(), client);
514 openbox->addClient(client->frame->button_close(), client);
515 openbox->addClient(client->frame->handle(), client);
516 openbox->addClient(client->frame->grip_left(), client);
517 openbox->addClient(client->frame->grip_right(), client);
519 // reparent the client to the frame
520 client->frame->grabClient();
522 if (openbox->state() != Openbox::State_Starting) {
523 // position the window intelligenty .. hopefully :)
524 // call the python PLACEWINDOW binding
525 EventData data(_number, client, EventAction::PlaceWindow, 0);
526 openbox->bindings()->fireEvent(&data);
529 EventData ddata(_number, client, EventAction::DisplayingWindow, 0);
530 openbox->bindings()->fireEvent(&ddata);
532 // if on the current desktop.. (or all desktops)
533 if (client->desktop() == _desktop ||
534 client->desktop() == (signed)0xffffffff) {
535 client->frame->show();
538 client->applyStartupState();
540 otk::display->ungrab();
542 // add to the screen's list
543 clients.push_back(client);
544 // once the client is in the list, update our strut to include the new
545 // client's (it is good that this happens after window placement!)
547 // this puts into the stacking order, then raises it
548 _stacking.push_back(client);
550 // update the root properties
553 openbox->bindings()->grabButtons(true, client);
555 EventData ndata(_number, client, EventAction::NewWindow, 0);
556 openbox->bindings()->fireEvent(&ndata);
559 printf("Managed window 0x%lx frame 0x%lx\n",
560 window, client->frame->window());
565 void Screen::unmanageWindow(Client *client)
567 Frame *frame = client->frame;
569 // call the python CLOSEWINDOW binding
570 EventData data(_number, client, EventAction::CloseWindow, 0);
571 openbox->bindings()->fireEvent(&data);
573 openbox->bindings()->grabButtons(false, client);
575 // remove from the wm's map
576 openbox->removeClient(client->window());
577 openbox->removeClient(frame->window());
578 openbox->removeClient(frame->plate());
579 openbox->removeClient(frame->titlebar());
580 openbox->removeClient(frame->label());
581 openbox->removeClient(frame->button_max());
582 openbox->removeClient(frame->button_iconify());
583 openbox->removeClient(frame->button_alldesk());
584 openbox->removeClient(frame->button_close());
585 openbox->removeClient(frame->handle());
586 openbox->removeClient(frame->grip_left());
587 openbox->removeClient(frame->grip_right());
588 // unregister for handling events
589 openbox->clearHandler(client->window());
591 // remove the window from our save set
592 XChangeSaveSet(**otk::display, client->window(), SetModeDelete);
594 // we dont want events no more
595 XSelectInput(**otk::display, client->window(), NoEventMask);
599 // give the client its border back
600 client->toggleClientBorder(true);
602 // reparent the window out of the frame
603 frame->releaseClient();
606 Window framewin = client->frame->window();
608 delete client->frame;
611 // remove from the stacking order
612 _stacking.remove(client);
614 // remove from the screen's list
615 clients.remove(client);
617 // once the client is out of the list, update our strut to remove it's
621 // unfocus the client (calls the focus callbacks)
625 printf("Unmanaged window 0x%lx frame 0x%lx\n", client->window(), framewin);
630 // update the root properties
634 void Screen::lowerWindow(Client *client)
636 Window wins[2]; // only ever restack 2 windows.
638 assert(!_stacking.empty()); // this would be bad
640 Client::List::iterator it = --_stacking.end();
641 const Client::List::iterator end = _stacking.begin();
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);
652 XRestackWindows(**otk::display, wins, 2);
653 changeStackingList();
656 void Screen::raiseWindow(Client *client)
658 Window wins[2]; // only ever restack 2 windows.
660 assert(!_stacking.empty()); // this would be bad
662 // remove the client before looking so we can't run into ourselves
663 _stacking.remove(client);
665 Client::List::iterator it = _stacking.begin();
666 const Client::List::iterator end = _stacking.end();
668 // the stacking list is from highest to lowest
669 for (; it != end && (*it)->layer() > client->layer(); ++it);
672 if our new position is the top, we want to stack under the _focuswindow
673 otherwise, we want to stack under the previous window in the stack.
675 wins[0] = (it == _stacking.begin() ? _focuswindow :
676 ((*(--Client::List::const_iterator(it)))->frame->window()));
677 wins[1] = client->frame->window();
679 _stacking.insert(it, client);
681 XRestackWindows(**otk::display, wins, 2);
682 changeStackingList();
685 void Screen::changeDesktop(long desktop)
687 if (!(desktop >= 0 && desktop < _num_desktops)) return;
689 printf("Moving to desktop %ld\n", desktop);
694 otk::Property::set(_info->rootWindow(),
695 otk::Property::atoms.net_current_desktop,
696 otk::Property::atoms.cardinal, _desktop);
698 if (old == _desktop) return;
700 Client::List::iterator it, end = clients.end();
701 for (it = clients.begin(); it != end; ++it) {
702 if ((*it)->desktop() == old) {
703 (*it)->frame->hide();
704 } else if ((*it)->desktop() == _desktop) {
705 (*it)->frame->show();
709 // force the callbacks to fire
710 if (!openbox->focusedClient())
711 openbox->setFocusedClient(0);
714 void Screen::changeNumDesktops(long num)
718 if (!(num > 0)) return;
720 // XXX: move windows on desktops that will no longer exist!
723 otk::Property::set(_info->rootWindow(),
724 otk::Property::atoms.net_number_of_desktops,
725 otk::Property::atoms.cardinal, _num_desktops);
727 // set the viewport hint
728 unsigned long *viewport = new unsigned long[_num_desktops * 2];
729 memset(viewport, 0, sizeof(unsigned long) * _num_desktops * 2);
730 otk::Property::set(_info->rootWindow(),
731 otk::Property::atoms.net_desktop_viewport,
732 otk::Property::atoms.cardinal,
733 viewport, _num_desktops * 2);
736 // update the work area hint
741 void Screen::updateDesktopNames()
743 unsigned long num = (unsigned) -1;
745 if (!otk::Property::get(_info->rootWindow(),
746 otk::Property::atoms.net_desktop_names,
747 otk::Property::utf8, &num, &_desktop_names))
748 _desktop_names.clear();
749 while ((long)_desktop_names.size() < _num_desktops)
750 _desktop_names.push_back("Unnamed");
754 void Screen::setDesktopName(long i, const otk::ustring &name)
758 if (i >= _num_desktops) return;
760 otk::Property::StringVect newnames = _desktop_names;
762 otk::Property::set(_info->rootWindow(),
763 otk::Property::atoms.net_desktop_names,
764 otk::Property::utf8, newnames);
768 void Screen::propertyHandler(const XPropertyEvent &e)
770 otk::EventHandler::propertyHandler(e);
772 // compress changes to a single property into a single change
774 while (XCheckTypedEvent(**otk::display, e.type, &ce)) {
775 // XXX: it would be nice to compress ALL changes to a property, not just
776 // changes in a row without other props between.
777 if (ce.xproperty.atom != e.atom) {
778 XPutBackEvent(**otk::display, &ce);
783 if (e.atom == otk::Property::atoms.net_desktop_names)
784 updateDesktopNames();
788 void Screen::clientMessageHandler(const XClientMessageEvent &e)
790 otk::EventHandler::clientMessageHandler(e);
792 if (e.format != 32) return;
794 if (e.message_type == otk::Property::atoms.net_current_desktop) {
795 changeDesktop(e.data.l[0]);
796 } else if (e.message_type == otk::Property::atoms.net_number_of_desktops) {
797 changeNumDesktops(e.data.l[0]);
799 // XXX: so many client messages to handle here! ..or not.. they go to clients
803 void Screen::mapRequestHandler(const XMapRequestEvent &e)
805 otk::EventHandler::mapRequestHandler(e);
808 printf("MapRequest for 0x%lx\n", e.window);
811 Client *c = openbox->findClient(e.window);
814 printf("DEBUG: MAP REQUEST CAUGHT IN SCREEN. IGNORED.\n");
817 manageWindow(e.window);