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 _strut.left = _strut.right = _strut.top = _strut.bottom = 0;
213 Client::List::iterator it, end = clients.end();
214 for (it = clients.begin(); it != end; ++it) {
215 const otk::Strut &s = (*it)->strut();
216 _strut.left = std::max(_strut.left, s.left);
217 _strut.right = std::max(_strut.right, s.right);
218 _strut.top = std::max(_strut.top, s.top);
219 _strut.bottom = std::max(_strut.bottom, s.bottom);
225 void Screen::calcArea()
227 otk::Rect old_area = _area;
231 // reset to the full areas
232 if (isXineramaActive())
233 xineramaUsableArea = getXineramaAreas();
237 _area.setRect(_strut.left, _strut.top,
238 _info->width() - (_strut.left + _strut.right),
239 _info->height() - (_strut.top + _strut.bottom));
243 if (isXineramaActive()) {
244 // keep each of the ximerama-defined areas inside the strut
245 RectList::iterator xit, xend = xineramaUsableArea.end();
246 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
247 if (xit->x() < usableArea.x()) {
248 xit->setX(usableArea.x());
249 xit->setWidth(xit->width() - usableArea.x());
251 if (xit->y() < usableArea.y()) {
252 xit->setY(usableArea.y());
253 xit->setHeight(xit->height() - usableArea.y());
255 if (xit->x() + xit->width() > usableArea.width())
256 xit->setWidth(usableArea.width() - xit->x());
257 if (xit->y() + xit->height() > usableArea.height())
258 xit->setHeight(usableArea.height() - xit->y());
264 if (old_area != _area)
265 // the area has changed, adjust all the maximized windows
266 for (it = clients.begin(); it != end; ++it)
273 void Screen::changeSupportedAtoms()
275 // create the netwm support window
276 _supportwindow = XCreateSimpleWindow(**otk::display,
278 0, 0, 1, 1, 0, 0, 0);
280 // set supporting window
281 otk::Property::set(_info->rootWindow(),
282 otk::Property::atoms.net_supporting_wm_check,
283 otk::Property::atoms.window, _supportwindow);
285 //set properties on the supporting window
286 otk::Property::set(_supportwindow, otk::Property::atoms.net_wm_name,
287 otk::Property::utf8, "Openbox");
288 otk::Property::set(_supportwindow,
289 otk::Property::atoms.net_supporting_wm_check,
290 otk::Property::atoms.window, _supportwindow);
294 otk::Property::atoms.net_current_desktop,
295 otk::Property::atoms.net_number_of_desktops,
296 otk::Property::atoms.net_desktop_geometry,
297 otk::Property::atoms.net_desktop_viewport,
298 otk::Property::atoms.net_active_window,
299 otk::Property::atoms.net_workarea,
300 otk::Property::atoms.net_client_list,
301 otk::Property::atoms.net_client_list_stacking,
302 otk::Property::atoms.net_desktop_names,
303 otk::Property::atoms.net_close_window,
304 otk::Property::atoms.net_wm_name,
305 otk::Property::atoms.net_wm_visible_name,
306 otk::Property::atoms.net_wm_icon_name,
307 otk::Property::atoms.net_wm_visible_icon_name,
309 otk::Property::atoms.net_wm_desktop,
311 otk::Property::atoms.net_wm_strut,
312 otk::Property::atoms.net_wm_window_type,
313 otk::Property::atoms.net_wm_window_type_desktop,
314 otk::Property::atoms.net_wm_window_type_dock,
315 otk::Property::atoms.net_wm_window_type_toolbar,
316 otk::Property::atoms.net_wm_window_type_menu,
317 otk::Property::atoms.net_wm_window_type_utility,
318 otk::Property::atoms.net_wm_window_type_splash,
319 otk::Property::atoms.net_wm_window_type_dialog,
320 otk::Property::atoms.net_wm_window_type_normal,
322 otk::Property::atoms.net_wm_moveresize,
323 otk::Property::atoms.net_wm_moveresize_size_topleft,
324 otk::Property::atoms.net_wm_moveresize_size_topright,
325 otk::Property::atoms.net_wm_moveresize_size_bottomleft,
326 otk::Property::atoms.net_wm_moveresize_size_bottomright,
327 otk::Property::atoms.net_wm_moveresize_move,
329 otk::Property::atoms.net_wm_allowed_actions,
330 otk::Property::atoms.net_wm_action_move,
331 otk::Property::atoms.net_wm_action_resize,
332 otk::Property::atoms.net_wm_action_minimize,
333 otk::Property::atoms.net_wm_action_shade,
334 /* otk::Property::atoms.net_wm_action_stick,*/
335 otk::Property::atoms.net_wm_action_maximize_horz,
336 otk::Property::atoms.net_wm_action_maximize_vert,
337 otk::Property::atoms.net_wm_action_fullscreen,
338 otk::Property::atoms.net_wm_action_change_desktop,
339 otk::Property::atoms.net_wm_action_close,
341 otk::Property::atoms.net_wm_state,
342 otk::Property::atoms.net_wm_state_modal,
343 otk::Property::atoms.net_wm_state_maximized_vert,
344 otk::Property::atoms.net_wm_state_maximized_horz,
345 otk::Property::atoms.net_wm_state_shaded,
346 otk::Property::atoms.net_wm_state_skip_taskbar,
347 otk::Property::atoms.net_wm_state_skip_pager,
348 otk::Property::atoms.net_wm_state_hidden,
349 otk::Property::atoms.net_wm_state_fullscreen,
350 otk::Property::atoms.net_wm_state_above,
351 otk::Property::atoms.net_wm_state_below,
353 const int num_supported = sizeof(supported)/sizeof(Atom);
355 otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_supported,
356 otk::Property::atoms.atom, supported, num_supported);
360 void Screen::changeClientList()
363 unsigned int size = clients.size();
365 // create an array of the window ids
369 windows = new Window[size];
371 Client::List::const_iterator it = clients.begin();
372 const Client::List::const_iterator end = clients.end();
373 for (; it != end; ++it, ++win_it)
374 *win_it = (*it)->window();
376 windows = (Window*) 0;
378 otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_client_list,
379 otk::Property::atoms.window, windows, size);
384 changeStackingList();
388 void Screen::changeStackingList()
391 unsigned int size = _stacking.size();
393 assert(size == clients.size()); // just making sure.. :)
396 // create an array of the window ids (from bottom to top, reverse order!)
400 windows = new Window[size];
402 Client::List::const_reverse_iterator it = _stacking.rbegin();
403 const Client::List::const_reverse_iterator end = _stacking.rend();
404 for (; it != end; ++it, ++win_it)
405 *win_it = (*it)->window();
407 windows = (Window*) 0;
409 otk::Property::set(_info->rootWindow(),
410 otk::Property::atoms.net_client_list_stacking,
411 otk::Property::atoms.window, windows, size);
418 void Screen::changeWorkArea() {
419 unsigned long *dims = new unsigned long[4 * _num_desktops];
420 for (long i = 0; i < _num_desktops; ++i) {
421 // XXX: this could be different for each workspace
422 dims[(i * 4) + 0] = _area.x();
423 dims[(i * 4) + 1] = _area.y();
424 dims[(i * 4) + 2] = _area.width();
425 dims[(i * 4) + 3] = _area.height();
427 otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_workarea,
428 otk::Property::atoms.cardinal, dims, 4 * _num_desktops);
433 void Screen::manageWindow(Window window)
437 XSetWindowAttributes attrib_set;
439 XWindowAttributes attrib;
441 otk::display->grab();
443 // check if it has already been unmapped by the time we started mapping
444 // the grab does a sync so we don't have to here
445 if (XCheckTypedWindowEvent(**otk::display, window, DestroyNotify, &e) ||
446 XCheckTypedWindowEvent(**otk::display, window, UnmapNotify, &e)) {
447 XPutBackEvent(**otk::display, &e);
449 otk::display->ungrab();
450 return; // don't manage it
453 if (!XGetWindowAttributes(**otk::display, window, &attrib) ||
454 attrib.override_redirect) {
455 otk::display->ungrab();
456 return; // don't manage it
459 // is the window a docking app
460 if ((wmhint = XGetWMHints(**otk::display, window))) {
461 if ((wmhint->flags & StateHint) &&
462 wmhint->initial_state == WithdrawnState) {
463 //slit->addClient(w); // XXX: make dock apps work!
465 otk::display->ungrab();
472 // choose the events we want to receive on the CLIENT window
473 attrib_set.event_mask = Client::event_mask;
474 attrib_set.do_not_propagate_mask = Client::no_propagate_mask;
475 XChangeWindowAttributes(**otk::display, window,
476 CWEventMask|CWDontPropagate, &attrib_set);
478 // create the Client class, which gets all of the hints on the window
479 client = new Client(_number, window);
480 // register for events
481 openbox->registerHandler(window, client);
482 // add to the wm's map
483 openbox->addClient(window, client);
485 // we dont want a border on the client
486 client->toggleClientBorder(false);
488 // specify that if we exit, the window should not be destroyed and should be
489 // reparented back to root automatically
490 XChangeSaveSet(**otk::display, window, SetModeInsert);
492 // create the decoration frame for the client window
493 client->frame = new Frame(client, &_style);
494 // register the plate for events (map req's)
495 // this involves removing itself from the handler list first, since it is
496 // auto added to the list, being a widget. we won't get any events on the
497 // plate except for events for the client (SubstructureRedirectMask)
498 openbox->clearHandler(client->frame->plate());
499 openbox->registerHandler(client->frame->plate(), client);
501 // add to the wm's map
502 openbox->addClient(client->frame->window(), client);
503 openbox->addClient(client->frame->plate(), client);
504 openbox->addClient(client->frame->titlebar(), client);
505 openbox->addClient(client->frame->label(), client);
506 openbox->addClient(client->frame->button_max(), client);
507 openbox->addClient(client->frame->button_iconify(), client);
508 openbox->addClient(client->frame->button_alldesk(), client);
509 openbox->addClient(client->frame->button_close(), client);
510 openbox->addClient(client->frame->handle(), client);
511 openbox->addClient(client->frame->grip_left(), client);
512 openbox->addClient(client->frame->grip_right(), client);
514 // reparent the client to the frame
515 client->frame->grabClient();
517 if (openbox->state() != Openbox::State_Starting) {
518 // position the window intelligenty .. hopefully :)
519 // call the python PLACEWINDOW binding
520 EventData data(_number, client, EventAction::PlaceWindow, 0);
521 openbox->bindings()->fireEvent(&data);
524 EventData ddata(_number, client, EventAction::DisplayingWindow, 0);
525 openbox->bindings()->fireEvent(&ddata);
527 // if on the current desktop.. (or all desktops)
528 if (client->desktop() == _desktop ||
529 client->desktop() == (signed)0xffffffff) {
530 client->frame->show();
533 client->applyStartupState();
535 otk::display->ungrab();
537 // add to the screen's list
538 clients.push_back(client);
539 // once the client is in the list, update our strut to include the new
540 // client's (it is good that this happens after window placement!)
542 // this puts into the stacking order, then raises it
543 _stacking.push_back(client);
545 // update the root properties
548 openbox->bindings()->grabButtons(true, client);
550 EventData ndata(_number, client, EventAction::NewWindow, 0);
551 openbox->bindings()->fireEvent(&ndata);
554 printf("Managed window 0x%lx frame 0x%lx\n",
555 window, client->frame->window());
560 void Screen::unmanageWindow(Client *client)
562 Frame *frame = client->frame;
564 // call the python CLOSEWINDOW binding
565 EventData data(_number, client, EventAction::CloseWindow, 0);
566 openbox->bindings()->fireEvent(&data);
568 openbox->bindings()->grabButtons(false, client);
570 // remove from the wm's map
571 openbox->removeClient(client->window());
572 openbox->removeClient(frame->window());
573 openbox->removeClient(frame->plate());
574 openbox->removeClient(frame->titlebar());
575 openbox->removeClient(frame->label());
576 openbox->removeClient(frame->button_max());
577 openbox->removeClient(frame->button_iconify());
578 openbox->removeClient(frame->button_alldesk());
579 openbox->removeClient(frame->button_close());
580 openbox->removeClient(frame->handle());
581 openbox->removeClient(frame->grip_left());
582 openbox->removeClient(frame->grip_right());
583 // unregister for handling events
584 openbox->clearHandler(client->window());
586 // remove the window from our save set
587 XChangeSaveSet(**otk::display, client->window(), SetModeDelete);
589 // we dont want events no more
590 XSelectInput(**otk::display, client->window(), NoEventMask);
594 // give the client its border back
595 client->toggleClientBorder(true);
597 // reparent the window out of the frame
598 frame->releaseClient();
601 Window framewin = client->frame->window();
603 delete client->frame;
606 // remove from the stacking order
607 _stacking.remove(client);
609 // remove from the screen's list
610 clients.remove(client);
612 // once the client is out of the list, update our strut to remove it's
616 // unfocus the client (calls the focus callbacks)
620 printf("Unmanaged window 0x%lx frame 0x%lx\n", client->window(), framewin);
625 // update the root properties
629 void Screen::lowerWindow(Client *client)
631 Window wins[2]; // only ever restack 2 windows.
633 assert(!_stacking.empty()); // this would be bad
635 Client::List::iterator it = --_stacking.end();
636 const Client::List::iterator end = _stacking.begin();
638 for (; it != end && (*it)->layer() < client->layer(); --it);
639 if (*it == client) return; // already the bottom, return
641 wins[0] = (*it)->frame->window();
642 wins[1] = client->frame->window();
644 _stacking.remove(client);
645 _stacking.insert(++it, client);
647 XRestackWindows(**otk::display, wins, 2);
648 changeStackingList();
651 void Screen::raiseWindow(Client *client)
653 Window wins[2]; // only ever restack 2 windows.
655 assert(!_stacking.empty()); // this would be bad
657 // remove the client before looking so we can't run into ourselves
658 _stacking.remove(client);
660 Client::List::iterator it = _stacking.begin();
661 const Client::List::iterator end = _stacking.end();
663 // the stacking list is from highest to lowest
664 for (; it != end && (*it)->layer() > client->layer(); ++it);
667 if our new position is the top, we want to stack under the _focuswindow
668 otherwise, we want to stack under the previous window in the stack.
670 wins[0] = (it == _stacking.begin() ? _focuswindow :
671 ((*(--Client::List::const_iterator(it)))->frame->window()));
672 wins[1] = client->frame->window();
674 _stacking.insert(it, client);
676 XRestackWindows(**otk::display, wins, 2);
677 changeStackingList();
680 void Screen::changeDesktop(long desktop)
682 if (!(desktop >= 0 && desktop < _num_desktops)) return;
684 printf("Moving to desktop %ld\n", desktop);
689 otk::Property::set(_info->rootWindow(),
690 otk::Property::atoms.net_current_desktop,
691 otk::Property::atoms.cardinal, _desktop);
693 if (old == _desktop) return;
695 Client::List::iterator it, end = clients.end();
696 for (it = clients.begin(); it != end; ++it) {
697 if ((*it)->desktop() == old) {
698 (*it)->frame->hide();
699 } else if ((*it)->desktop() == _desktop) {
700 (*it)->frame->show();
704 // force the callbacks to fire
705 if (!openbox->focusedClient())
706 openbox->setFocusedClient(0);
709 void Screen::changeNumDesktops(long num)
713 if (!(num > 0)) return;
715 // XXX: move windows on desktops that will no longer exist!
718 otk::Property::set(_info->rootWindow(),
719 otk::Property::atoms.net_number_of_desktops,
720 otk::Property::atoms.cardinal, _num_desktops);
722 // set the viewport hint
723 unsigned long *viewport = new unsigned long[_num_desktops * 2];
724 memset(viewport, 0, sizeof(unsigned long) * _num_desktops * 2);
725 otk::Property::set(_info->rootWindow(),
726 otk::Property::atoms.net_desktop_viewport,
727 otk::Property::atoms.cardinal,
728 viewport, _num_desktops * 2);
731 // update the work area hint
736 void Screen::updateDesktopNames()
738 unsigned long num = (unsigned) -1;
740 if (!otk::Property::get(_info->rootWindow(),
741 otk::Property::atoms.net_desktop_names,
742 otk::Property::utf8, &num, &_desktop_names))
743 _desktop_names.clear();
744 while ((long)_desktop_names.size() < _num_desktops)
745 _desktop_names.push_back("Unnamed");
749 void Screen::setDesktopName(long i, const otk::ustring &name)
753 if (i >= _num_desktops) return;
755 otk::Property::StringVect newnames = _desktop_names;
757 otk::Property::set(_info->rootWindow(),
758 otk::Property::atoms.net_desktop_names,
759 otk::Property::utf8, newnames);
763 void Screen::propertyHandler(const XPropertyEvent &e)
765 otk::EventHandler::propertyHandler(e);
767 // compress changes to a single property into a single change
769 while (XCheckTypedEvent(**otk::display, e.type, &ce)) {
770 // XXX: it would be nice to compress ALL changes to a property, not just
771 // changes in a row without other props between.
772 if (ce.xproperty.atom != e.atom) {
773 XPutBackEvent(**otk::display, &ce);
778 if (e.atom == otk::Property::atoms.net_desktop_names)
779 updateDesktopNames();
783 void Screen::clientMessageHandler(const XClientMessageEvent &e)
785 otk::EventHandler::clientMessageHandler(e);
787 if (e.format != 32) return;
789 if (e.message_type == otk::Property::atoms.net_current_desktop) {
790 changeDesktop(e.data.l[0]);
791 } else if (e.message_type == otk::Property::atoms.net_number_of_desktops) {
792 changeNumDesktops(e.data.l[0]);
794 // XXX: so many client messages to handle here! ..or not.. they go to clients
798 void Screen::mapRequestHandler(const XMapRequestEvent &e)
800 otk::EventHandler::mapRequestHandler(e);
803 printf("MapRequest for 0x%lx\n", e.window);
806 Client *c = openbox->findClient(e.window);
809 printf("DEBUG: MAP REQUEST CAUGHT IN SCREEN. IGNORED.\n");
812 manageWindow(e.window);