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"
37 static int anotherWMRunning(Display *display, XErrorEvent *) {
38 printf(_("Another window manager already running on display %s.\n"),
39 DisplayString(display));
48 Screen::Screen(int screen)
49 : WidgetBase(WidgetBase::Type_Root),
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
65 printf(_("Managing screen %d: visual 0x%lx, depth %d\n"),
66 _number, XVisualIDFromVisual(_info->visual()), _info->depth());
68 openbox->property()->set(_info->rootWindow(),
69 otk::Property::openbox_pid,
70 otk::Property::Atom_Cardinal,
71 (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 // initialize the shit that is used for all drawing on the screen
78 _image_control = new otk::ImageControl(openbox->timerManager(),
80 _image_control->installRootColormap();
81 _root_cmap_installed = True;
83 // initialize the screen's style
84 _style.setImageControl(_image_control);
85 otk::ustring stylepath;
86 python_get_string("theme", &stylepath);
87 otk::Configuration sconfig(false);
88 sconfig.setFile(otk::expandTilde(stylepath.c_str()));
89 if (!sconfig.load()) {
90 sconfig.setFile(otk::expandTilde(DEFAULTSTYLE));
91 if (!sconfig.load()) {
92 printf(_("Unable to load default style: %s. Aborting.\n"), DEFAULTSTYLE);
98 // set up notification of netwm support
99 changeSupportedAtoms();
101 // Set the netwm properties for geometry
102 unsigned long geometry[] = { _info->width(),
104 openbox->property()->set(_info->rootWindow(),
105 otk::Property::net_desktop_geometry,
106 otk::Property::Atom_Cardinal,
109 // Set the net_desktop_names property
110 std::vector<otk::ustring> names;
111 python_get_stringlist("desktop_names", &names);
112 openbox->property()->set(_info->rootWindow(),
113 otk::Property::net_desktop_names,
116 // the above set() will cause the updateDesktopNames to fire right away so
117 // we have a list of desktop names
119 if (!python_get_long("number_of_desktops", &_num_desktops))
121 changeNumDesktops(_num_desktops); // set the hint
124 changeDesktop(0); // set the hint
126 // create the window which gets focus when no clients get it
127 XSetWindowAttributes attr;
128 attr.override_redirect = true;
129 _focuswindow = XCreateWindow(**otk::display, _info->rootWindow(),
130 -100, -100, 1, 1, 0, 0, InputOnly,
131 _info->visual(), CWOverrideRedirect, &attr);
132 XMapWindow(**otk::display, _focuswindow);
134 // these may be further updated if any pre-existing windows are found in
135 // the manageExising() function
136 changeClientList(); // initialize the client lists, which will be empty
137 calcArea(); // initialize the available working area
139 // register this class as the event handler for the root window
140 openbox->registerHandler(_info->rootWindow(), this);
142 // call the python Startup callbacks
143 EventData data(_number, 0, EventShutdown, 0);
144 openbox->bindings()->fireEvent(&data);
150 if (! _managed) return;
152 XSelectInput(**otk::display, _info->rootWindow(), NoEventMask);
154 // unmanage all windows
155 while (!clients.empty())
156 unmanageWindow(clients.front());
158 // call the python Shutdown callbacks
159 EventData data(_number, 0, EventShutdown, 0);
160 openbox->bindings()->fireEvent(&data);
162 XDestroyWindow(**otk::display, _focuswindow);
163 XDestroyWindow(**otk::display, _supportwindow);
165 delete _image_control;
169 void Screen::manageExisting()
171 unsigned int i, j, nchild;
172 Window r, p, *children;
173 XQueryTree(**otk::display, _info->rootWindow(), &r, &p,
176 // preen the window list of all icon windows... for better dockapp support
177 for (i = 0; i < nchild; i++) {
178 if (children[i] == None) continue;
180 XWMHints *wmhints = XGetWMHints(**otk::display,
184 if ((wmhints->flags & IconWindowHint) &&
185 (wmhints->icon_window != children[i])) {
186 for (j = 0; j < nchild; j++) {
187 if (children[j] == wmhints->icon_window) {
198 // manage shown windows
199 for (i = 0; i < nchild; ++i) {
200 if (children[i] == None)
203 XWindowAttributes attrib;
204 if (XGetWindowAttributes(**otk::display, children[i], &attrib)) {
205 if (attrib.override_redirect) continue;
207 if (attrib.map_state != IsUnmapped) {
208 manageWindow(children[i]);
217 void Screen::updateStrut()
219 _strut.left = _strut.right = _strut.top = _strut.bottom = 0;
221 Client::List::iterator it, end = clients.end();
222 for (it = clients.begin(); it != end; ++it) {
223 const otk::Strut &s = (*it)->strut();
224 _strut.left = std::max(_strut.left, s.left);
225 _strut.right = std::max(_strut.right, s.right);
226 _strut.top = std::max(_strut.top, s.top);
227 _strut.bottom = std::max(_strut.bottom, s.bottom);
233 void Screen::calcArea()
235 otk::Rect old_area = _area;
239 // reset to the full areas
240 if (isXineramaActive())
241 xineramaUsableArea = getXineramaAreas();
245 _area.setRect(_strut.left, _strut.top,
246 _info->width() - (_strut.left + _strut.right),
247 _info->height() - (_strut.top + _strut.bottom));
251 if (isXineramaActive()) {
252 // keep each of the ximerama-defined areas inside the strut
253 RectList::iterator xit, xend = xineramaUsableArea.end();
254 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
255 if (xit->x() < usableArea.x()) {
256 xit->setX(usableArea.x());
257 xit->setWidth(xit->width() - usableArea.x());
259 if (xit->y() < usableArea.y()) {
260 xit->setY(usableArea.y());
261 xit->setHeight(xit->height() - usableArea.y());
263 if (xit->x() + xit->width() > usableArea.width())
264 xit->setWidth(usableArea.width() - xit->x());
265 if (xit->y() + xit->height() > usableArea.height())
266 xit->setHeight(usableArea.height() - xit->y());
272 if (old_area != _area)
273 // XXX: re-maximize windows
279 void Screen::changeSupportedAtoms()
281 // create the netwm support window
282 _supportwindow = XCreateSimpleWindow(**otk::display,
284 0, 0, 1, 1, 0, 0, 0);
286 // set supporting window
287 openbox->property()->set(_info->rootWindow(),
288 otk::Property::net_supporting_wm_check,
289 otk::Property::Atom_Window,
292 //set properties on the supporting window
293 openbox->property()->set(_supportwindow,
294 otk::Property::net_wm_name,
297 openbox->property()->set(_supportwindow,
298 otk::Property::net_supporting_wm_check,
299 otk::Property::Atom_Window,
304 otk::Property::net_current_desktop,
305 otk::Property::net_number_of_desktops,
306 otk::Property::net_desktop_geometry,
307 otk::Property::net_desktop_viewport,
308 otk::Property::net_active_window,
309 otk::Property::net_workarea,
310 otk::Property::net_client_list,
311 otk::Property::net_client_list_stacking,
312 otk::Property::net_desktop_names,
313 otk::Property::net_close_window,
314 otk::Property::net_wm_name,
315 otk::Property::net_wm_visible_name,
316 otk::Property::net_wm_icon_name,
317 otk::Property::net_wm_visible_icon_name,
319 otk::Property::net_wm_desktop,
321 otk::Property::net_wm_strut,
322 otk::Property::net_wm_window_type,
323 otk::Property::net_wm_window_type_desktop,
324 otk::Property::net_wm_window_type_dock,
325 otk::Property::net_wm_window_type_toolbar,
326 otk::Property::net_wm_window_type_menu,
327 otk::Property::net_wm_window_type_utility,
328 otk::Property::net_wm_window_type_splash,
329 otk::Property::net_wm_window_type_dialog,
330 otk::Property::net_wm_window_type_normal,
332 otk::Property::net_wm_moveresize,
333 otk::Property::net_wm_moveresize_size_topleft,
334 otk::Property::net_wm_moveresize_size_topright,
335 otk::Property::net_wm_moveresize_size_bottomleft,
336 otk::Property::net_wm_moveresize_size_bottomright,
337 otk::Property::net_wm_moveresize_move,
340 otk::Property::net_wm_allowed_actions,
341 otk::Property::net_wm_action_move,
342 otk::Property::net_wm_action_resize,
343 otk::Property::net_wm_action_shade,
344 otk::Property::net_wm_action_maximize_horz,
345 otk::Property::net_wm_action_maximize_vert,
346 otk::Property::net_wm_action_change_desktop,
347 otk::Property::net_wm_action_close,
349 otk::Property::net_wm_state,
350 otk::Property::net_wm_state_modal,
351 otk::Property::net_wm_state_maximized_vert,
352 otk::Property::net_wm_state_maximized_horz,
353 otk::Property::net_wm_state_shaded,
354 otk::Property::net_wm_state_skip_taskbar,
355 otk::Property::net_wm_state_skip_pager,
356 otk::Property::net_wm_state_hidden,
357 otk::Property::net_wm_state_fullscreen,
358 otk::Property::net_wm_state_above,
359 otk::Property::net_wm_state_below,
361 const int num_supported = sizeof(supported)/sizeof(Atom);
363 // convert to the atom values
364 for (int i = 0; i < num_supported; ++i)
366 openbox->property()->atom((otk::Property::Atoms)supported[i]);
368 openbox->property()->set(_info->rootWindow(),
369 otk::Property::net_supported,
370 otk::Property::Atom_Atom,
371 supported, num_supported);
375 void Screen::changeClientList()
378 unsigned int size = clients.size();
380 // create an array of the window ids
384 windows = new Window[size];
386 Client::List::const_iterator it = clients.begin();
387 const Client::List::const_iterator end = clients.end();
388 for (; it != end; ++it, ++win_it)
389 *win_it = (*it)->window();
391 windows = (Window*) 0;
393 openbox->property()->set(_info->rootWindow(),
394 otk::Property::net_client_list,
395 otk::Property::Atom_Window,
401 changeStackingList();
405 void Screen::changeStackingList()
408 unsigned int size = _stacking.size();
410 assert(size == clients.size()); // just making sure.. :)
413 // create an array of the window ids
417 windows = new Window[size];
419 Client::List::const_iterator it = _stacking.begin();
420 const Client::List::const_iterator end = _stacking.end();
421 for (; it != end; ++it, ++win_it)
422 *win_it = (*it)->window();
424 windows = (Window*) 0;
426 openbox->property()->set(_info->rootWindow(),
427 otk::Property::net_client_list_stacking,
428 otk::Property::Atom_Window,
436 void Screen::changeWorkArea() {
437 unsigned long *dims = new unsigned long[4 * _num_desktops];
438 for (long i = 0; i < _num_desktops; ++i) {
439 // XXX: this could be different for each workspace
440 dims[(i * 4) + 0] = _area.x();
441 dims[(i * 4) + 1] = _area.y();
442 dims[(i * 4) + 2] = _area.width();
443 dims[(i * 4) + 3] = _area.height();
445 openbox->property()->set(_info->rootWindow(),
446 otk::Property::net_workarea,
447 otk::Property::Atom_Cardinal,
448 dims, 4 * _num_desktops);
453 void Screen::manageWindow(Window window)
457 XSetWindowAttributes attrib_set;
459 otk::display->grab();
461 // is the window a docking app
462 if ((wmhint = XGetWMHints(**otk::display, window))) {
463 if ((wmhint->flags & StateHint) &&
464 wmhint->initial_state == WithdrawnState) {
465 //slit->addClient(w); // XXX: make dock apps work!
466 otk::display->ungrab();
474 // choose the events we want to receive on the CLIENT window
475 attrib_set.event_mask = Client::event_mask;
476 attrib_set.do_not_propagate_mask = Client::no_propagate_mask;
477 XChangeWindowAttributes(**otk::display, window,
478 CWEventMask|CWDontPropagate, &attrib_set);
480 // create the Client class, which gets all of the hints on the window
481 client = new Client(_number, window);
482 // register for events
483 openbox->registerHandler(window, client);
484 // add to the wm's map
485 openbox->addClient(window, client);
487 // we dont want a border on the client
488 client->toggleClientBorder(false);
490 // specify that if we exit, the window should not be destroyed and should be
491 // reparented back to root automatically
492 XChangeSaveSet(**otk::display, window, SetModeInsert);
494 if (!(openbox->state() == Openbox::State_Starting ||
495 client->positionRequested())) {
496 // position the window intelligenty .. hopefully :)
497 // call the python PLACEWINDOW binding
498 EventData data(_number, client, EventPlaceWindow, 0);
499 openbox->bindings()->fireEvent(&data);
502 // create the decoration frame for the client window
503 client->frame = new Frame(client, &_style);
505 // add to the wm's map
506 openbox->addClient(client->frame->window(), client);
507 openbox->addClient(client->frame->plate(), client);
508 openbox->addClient(client->frame->titlebar(), client);
509 openbox->addClient(client->frame->label(), client);
510 openbox->addClient(client->frame->button_max(), client);
511 openbox->addClient(client->frame->button_iconify(), client);
512 openbox->addClient(client->frame->button_stick(), client);
513 openbox->addClient(client->frame->button_close(), client);
514 openbox->addClient(client->frame->handle(), client);
515 openbox->addClient(client->frame->grip_left(), client);
516 openbox->addClient(client->frame->grip_right(), client);
518 // reparent the client to the frame
519 client->frame->grabClient();
521 // if on the current desktop.. (or all desktops)
522 if (client->desktop() == _desktop ||
523 client->desktop() == (signed)0xffffffff) {
524 client->frame->show();
527 // XXX: handle any requested states such as maximized
529 otk::display->ungrab();
531 // add to the screen's list
532 clients.push_back(client);
533 // this puts into the stacking order, then raises it
534 _stacking.push_back(client);
535 restack(true, client);
536 // update the root properties
539 openbox->bindings()->grabButtons(true, client);
541 // call the python NEWWINDOW binding
542 EventData data(_number, client, EventNewWindow, 0);
543 openbox->bindings()->fireEvent(&data);
546 printf("Managed window 0x%lx\n", window);
551 void Screen::unmanageWindow(Client *client)
553 Frame *frame = client->frame;
555 // call the python CLOSEWINDOW binding
556 EventData data(_number, client, EventCloseWindow, 0);
557 openbox->bindings()->fireEvent(&data);
559 openbox->bindings()->grabButtons(false, client);
561 // remove from the wm's map
562 openbox->removeClient(client->window());
563 openbox->removeClient(frame->window());
564 openbox->removeClient(frame->plate());
565 openbox->removeClient(frame->titlebar());
566 openbox->removeClient(frame->label());
567 openbox->removeClient(frame->button_max());
568 openbox->removeClient(frame->button_iconify());
569 openbox->removeClient(frame->button_stick());
570 openbox->removeClient(frame->button_close());
571 openbox->removeClient(frame->handle());
572 openbox->removeClient(frame->grip_left());
573 openbox->removeClient(frame->grip_right());
574 // unregister for handling events
575 openbox->clearHandler(client->window());
577 // remove the window from our save set
578 XChangeSaveSet(**otk::display, client->window(), SetModeDelete);
580 // we dont want events no more
581 XSelectInput(**otk::display, client->window(), NoEventMask);
585 // give the client its border back
586 client->toggleClientBorder(true);
588 // reparent the window out of the frame
589 frame->releaseClient();
591 delete client->frame;
594 // remove from the stacking order
595 _stacking.remove(client);
597 // remove from the screen's list
598 clients.remove(client);
600 // unfocus the client (calls the focus callbacks)
604 printf("Unmanaged window 0x%lx\n", client->window());
609 // update the root properties
613 void Screen::restack(bool raise, Client *client)
615 const int layer = client->layer();
616 std::vector<Window> wins;
618 _stacking.remove(client);
620 // the stacking list is from highest to lowest
622 Client::List::iterator it = _stacking.begin(), end = _stacking.end();
623 // insert the windows above this window
624 for (; it != end; ++it) {
625 if ((*it)->layer() < layer || (raise && (*it)->layer() == layer))
627 wins.push_back((*it)->frame->window());
630 wins.push_back(client->frame->window());
631 _stacking.insert(it, client);
632 // insert the remaining below this window
633 for (; it != end; ++it)
634 wins.push_back((*it)->frame->window());
636 XRestackWindows(**otk::display, &wins[0], wins.size());
637 changeStackingList();
640 void Screen::changeDesktop(long desktop)
642 if (!(desktop >= 0 && desktop < _num_desktops)) return;
644 printf("Moving to desktop %ld\n", desktop);
649 openbox->property()->set(_info->rootWindow(),
650 otk::Property::net_current_desktop,
651 otk::Property::Atom_Cardinal,
654 if (old == _desktop) return;
656 Client::List::iterator it, end = clients.end();
657 for (it = clients.begin(); it != end; ++it) {
658 if ((*it)->desktop() == old) {
659 (*it)->frame->hide();
660 } else if ((*it)->desktop() == _desktop) {
661 (*it)->frame->show();
665 // force the callbacks to fire
666 if (!openbox->focusedClient())
667 openbox->setFocusedClient(0);
670 void Screen::changeNumDesktops(long num)
674 if (!(num > 0)) return;
676 // XXX: move windows on desktops that will no longer exist!
679 openbox->property()->set(_info->rootWindow(),
680 otk::Property::net_number_of_desktops,
681 otk::Property::Atom_Cardinal,
684 // set the viewport hint
685 unsigned long *viewport = new unsigned long[_num_desktops * 2];
686 memset(viewport, 0, sizeof(unsigned long) * _num_desktops * 2);
687 openbox->property()->set(_info->rootWindow(),
688 otk::Property::net_desktop_viewport,
689 otk::Property::Atom_Cardinal,
690 viewport, _num_desktops * 2);
693 // update the work area hint
698 void Screen::updateDesktopNames()
700 const otk::Property *property = openbox->property();
702 unsigned long num = (unsigned) -1;
704 if (!property->get(_info->rootWindow(),
705 otk::Property::net_desktop_names,
706 otk::Property::utf8, &num, &_desktop_names))
707 _desktop_names.clear();
708 while ((long)_desktop_names.size() < _num_desktops)
709 _desktop_names.push_back("Unnamed");
713 void Screen::setDesktopName(long i, const otk::ustring &name)
717 if (i >= _num_desktops) return;
719 const otk::Property *property = openbox->property();
721 otk::Property::StringVect newnames = _desktop_names;
723 property->set(_info->rootWindow(), otk::Property::net_desktop_names,
724 otk::Property::utf8, newnames);
728 void Screen::propertyHandler(const XPropertyEvent &e)
730 otk::EventHandler::propertyHandler(e);
732 const otk::Property *property = openbox->property();
734 // compress changes to a single property into a single change
736 while (XCheckTypedEvent(**otk::display, e.type, &ce)) {
737 // XXX: it would be nice to compress ALL changes to a property, not just
738 // changes in a row without other props between.
739 if (ce.xproperty.atom != e.atom) {
740 XPutBackEvent(**otk::display, &ce);
745 if (e.atom == property->atom(otk::Property::net_desktop_names))
746 updateDesktopNames();
750 void Screen::clientMessageHandler(const XClientMessageEvent &e)
752 otk::EventHandler::clientMessageHandler(e);
754 if (e.format != 32) return;
756 const otk::Property *property = openbox->property();
758 if (e.message_type == property->atom(otk::Property::net_current_desktop)) {
759 changeDesktop(e.data.l[0]);
760 } else if (e.message_type ==
761 property->atom(otk::Property::net_number_of_desktops)) {
762 changeNumDesktops(e.data.l[0]);
764 // XXX: so many client messages to handle here! ..or not.. they go to clients
768 void Screen::mapRequestHandler(const XMapRequestEvent &e)
770 otk::EventHandler::mapRequestHandler(e);
773 printf("MapRequest for 0x%lx\n", e.window);
777 MapRequest events come here even after the window exists instead of going
778 right to the client window, because of how they are sent and their struct
781 Client *c = openbox->findClient(e.window);
784 // send a net_active_window message
786 ce.xclient.type = ClientMessage;
787 ce.xclient.message_type =
788 openbox->property()->atom(otk::Property::net_active_window);
789 ce.xclient.display = **otk::display;
790 ce.xclient.window = c->window();
791 ce.xclient.format = 32;
792 ce.xclient.data.l[0] = 0l;
793 ce.xclient.data.l[1] = 0l;
794 ce.xclient.data.l[2] = 0l;
795 ce.xclient.data.l[3] = 0l;
796 ce.xclient.data.l[4] = 0l;
797 XSendEvent(**otk::display, _info->rootWindow(), false,
798 SubstructureRedirectMask | SubstructureNotifyMask,
801 manageWindow(e.window);