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(_info, true);
79 _image_control->installRootColormap();
80 _root_cmap_installed = True;
82 // initialize the screen's style
83 _style.setImageControl(_image_control);
84 otk::ustring stylepath;
85 python_get_string("theme", &stylepath);
86 otk::Configuration sconfig(false);
87 sconfig.setFile(otk::expandTilde(stylepath.c_str()));
88 if (!sconfig.load()) {
89 sconfig.setFile(otk::expandTilde(DEFAULTSTYLE));
90 if (!sconfig.load()) {
91 printf(_("Unable to load default style: %s. Aborting.\n"), DEFAULTSTYLE);
97 // set up notification of netwm support
98 changeSupportedAtoms();
100 // Set the netwm properties for geometry
101 unsigned long geometry[] = { _info->width(),
103 openbox->property()->set(_info->rootWindow(),
104 otk::Property::net_desktop_geometry,
105 otk::Property::Atom_Cardinal,
108 // Set the net_desktop_names property
109 std::vector<otk::ustring> names;
110 python_get_stringlist("desktop_names", &names);
111 openbox->property()->set(_info->rootWindow(),
112 otk::Property::net_desktop_names,
115 // the above set() will cause the updateDesktopNames to fire right away so
116 // we have a list of desktop names
118 if (!python_get_long("number_of_desktops", &_num_desktops))
120 changeNumDesktops(_num_desktops); // set the hint
123 changeDesktop(0); // set the hint
125 // create the window which gets focus when no clients get it
126 XSetWindowAttributes attr;
127 attr.override_redirect = true;
128 _focuswindow = XCreateWindow(**otk::display, _info->rootWindow(),
129 -100, -100, 1, 1, 0, 0, InputOnly,
130 _info->visual(), CWOverrideRedirect, &attr);
131 XMapWindow(**otk::display, _focuswindow);
133 // these may be further updated if any pre-existing windows are found in
134 // the manageExising() function
135 changeClientList(); // initialize the client lists, which will be empty
136 calcArea(); // initialize the available working area
138 // register this class as the event handler for the root window
139 openbox->registerHandler(_info->rootWindow(), this);
141 // call the python Startup callbacks
142 EventData data(_number, 0, EventShutdown, 0);
143 openbox->bindings()->fireEvent(&data);
149 if (! _managed) return;
151 XSelectInput(**otk::display, _info->rootWindow(), NoEventMask);
153 // unmanage all windows
154 while (!clients.empty())
155 unmanageWindow(clients.front());
157 // call the python Shutdown callbacks
158 EventData data(_number, 0, EventShutdown, 0);
159 openbox->bindings()->fireEvent(&data);
161 XDestroyWindow(**otk::display, _focuswindow);
162 XDestroyWindow(**otk::display, _supportwindow);
164 delete _image_control;
168 void Screen::manageExisting()
170 unsigned int i, j, nchild;
171 Window r, p, *children;
172 XQueryTree(**otk::display, _info->rootWindow(), &r, &p,
175 // preen the window list of all icon windows... for better dockapp support
176 for (i = 0; i < nchild; i++) {
177 if (children[i] == None) continue;
179 XWMHints *wmhints = XGetWMHints(**otk::display,
183 if ((wmhints->flags & IconWindowHint) &&
184 (wmhints->icon_window != children[i])) {
185 for (j = 0; j < nchild; j++) {
186 if (children[j] == wmhints->icon_window) {
197 // manage shown windows
198 for (i = 0; i < nchild; ++i) {
199 if (children[i] == None)
202 XWindowAttributes attrib;
203 if (XGetWindowAttributes(**otk::display, children[i], &attrib)) {
204 if (attrib.override_redirect) continue;
206 if (attrib.map_state != IsUnmapped) {
207 manageWindow(children[i]);
216 void Screen::updateStrut()
218 _strut.left = _strut.right = _strut.top = _strut.bottom = 0;
220 Client::List::iterator it, end = clients.end();
221 for (it = clients.begin(); it != end; ++it) {
222 const otk::Strut &s = (*it)->strut();
223 _strut.left = std::max(_strut.left, s.left);
224 _strut.right = std::max(_strut.right, s.right);
225 _strut.top = std::max(_strut.top, s.top);
226 _strut.bottom = std::max(_strut.bottom, s.bottom);
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 openbox->property()->set(_info->rootWindow(),
287 otk::Property::net_supporting_wm_check,
288 otk::Property::Atom_Window,
291 //set properties on the supporting window
292 openbox->property()->set(_supportwindow,
293 otk::Property::net_wm_name,
296 openbox->property()->set(_supportwindow,
297 otk::Property::net_supporting_wm_check,
298 otk::Property::Atom_Window,
303 otk::Property::net_current_desktop,
304 otk::Property::net_number_of_desktops,
305 otk::Property::net_desktop_geometry,
306 otk::Property::net_desktop_viewport,
307 otk::Property::net_active_window,
308 otk::Property::net_workarea,
309 otk::Property::net_client_list,
310 otk::Property::net_client_list_stacking,
311 otk::Property::net_desktop_names,
312 otk::Property::net_close_window,
313 otk::Property::net_wm_name,
314 otk::Property::net_wm_visible_name,
315 otk::Property::net_wm_icon_name,
316 otk::Property::net_wm_visible_icon_name,
318 otk::Property::net_wm_desktop,
320 otk::Property::net_wm_strut,
321 otk::Property::net_wm_window_type,
322 otk::Property::net_wm_window_type_desktop,
323 otk::Property::net_wm_window_type_dock,
324 otk::Property::net_wm_window_type_toolbar,
325 otk::Property::net_wm_window_type_menu,
326 otk::Property::net_wm_window_type_utility,
327 otk::Property::net_wm_window_type_splash,
328 otk::Property::net_wm_window_type_dialog,
329 otk::Property::net_wm_window_type_normal,
331 otk::Property::net_wm_moveresize,
332 otk::Property::net_wm_moveresize_size_topleft,
333 otk::Property::net_wm_moveresize_size_topright,
334 otk::Property::net_wm_moveresize_size_bottomleft,
335 otk::Property::net_wm_moveresize_size_bottomright,
336 otk::Property::net_wm_moveresize_move,
339 otk::Property::net_wm_allowed_actions,
340 otk::Property::net_wm_action_move,
341 otk::Property::net_wm_action_resize,
342 otk::Property::net_wm_action_shade,
343 otk::Property::net_wm_action_maximize_horz,
344 otk::Property::net_wm_action_maximize_vert,
345 otk::Property::net_wm_action_change_desktop,
346 otk::Property::net_wm_action_close,
348 otk::Property::net_wm_state,
349 otk::Property::net_wm_state_modal,
350 otk::Property::net_wm_state_maximized_vert,
351 otk::Property::net_wm_state_maximized_horz,
352 otk::Property::net_wm_state_shaded,
353 otk::Property::net_wm_state_skip_taskbar,
354 otk::Property::net_wm_state_skip_pager,
355 otk::Property::net_wm_state_hidden,
356 otk::Property::net_wm_state_fullscreen,
357 otk::Property::net_wm_state_above,
358 otk::Property::net_wm_state_below,
360 const int num_supported = sizeof(supported)/sizeof(Atom);
362 // convert to the atom values
363 for (int i = 0; i < num_supported; ++i)
365 openbox->property()->atom((otk::Property::Atoms)supported[i]);
367 openbox->property()->set(_info->rootWindow(),
368 otk::Property::net_supported,
369 otk::Property::Atom_Atom,
370 supported, num_supported);
374 void Screen::changeClientList()
377 unsigned int size = clients.size();
379 // create an array of the window ids
383 windows = new Window[size];
385 Client::List::const_iterator it = clients.begin();
386 const Client::List::const_iterator end = clients.end();
387 for (; it != end; ++it, ++win_it)
388 *win_it = (*it)->window();
390 windows = (Window*) 0;
392 openbox->property()->set(_info->rootWindow(),
393 otk::Property::net_client_list,
394 otk::Property::Atom_Window,
400 changeStackingList();
404 void Screen::changeStackingList()
407 unsigned int size = _stacking.size();
409 assert(size == clients.size()); // just making sure.. :)
412 // create an array of the window ids (from bottom to top, reverse order!)
416 windows = new Window[size];
418 Client::List::const_reverse_iterator it = _stacking.rbegin();
419 const Client::List::const_reverse_iterator end = _stacking.rend();
420 for (; it != end; ++it, ++win_it)
421 *win_it = (*it)->window();
423 windows = (Window*) 0;
425 openbox->property()->set(_info->rootWindow(),
426 otk::Property::net_client_list_stacking,
427 otk::Property::Atom_Window,
435 void Screen::changeWorkArea() {
436 unsigned long *dims = new unsigned long[4 * _num_desktops];
437 for (long i = 0; i < _num_desktops; ++i) {
438 // XXX: this could be different for each workspace
439 dims[(i * 4) + 0] = _area.x();
440 dims[(i * 4) + 1] = _area.y();
441 dims[(i * 4) + 2] = _area.width();
442 dims[(i * 4) + 3] = _area.height();
444 openbox->property()->set(_info->rootWindow(),
445 otk::Property::net_workarea,
446 otk::Property::Atom_Cardinal,
447 dims, 4 * _num_desktops);
452 void Screen::manageWindow(Window window)
456 XSetWindowAttributes attrib_set;
458 otk::display->grab();
460 // is the window a docking app
461 if ((wmhint = XGetWMHints(**otk::display, window))) {
462 if ((wmhint->flags & StateHint) &&
463 wmhint->initial_state == WithdrawnState) {
464 //slit->addClient(w); // XXX: make dock apps work!
465 otk::display->ungrab();
473 // choose the events we want to receive on the CLIENT window
474 attrib_set.event_mask = Client::event_mask;
475 attrib_set.do_not_propagate_mask = Client::no_propagate_mask;
476 XChangeWindowAttributes(**otk::display, window,
477 CWEventMask|CWDontPropagate, &attrib_set);
479 // create the Client class, which gets all of the hints on the window
480 client = new Client(_number, window);
481 // register for events
482 openbox->registerHandler(window, client);
483 // add to the wm's map
484 openbox->addClient(window, client);
486 // we dont want a border on the client
487 client->toggleClientBorder(false);
489 // specify that if we exit, the window should not be destroyed and should be
490 // reparented back to root automatically
491 XChangeSaveSet(**otk::display, window, SetModeInsert);
493 if (!(openbox->state() == Openbox::State_Starting ||
494 client->positionRequested())) {
495 // position the window intelligenty .. hopefully :)
496 // call the python PLACEWINDOW binding
497 EventData data(_number, client, EventPlaceWindow, 0);
498 openbox->bindings()->fireEvent(&data);
501 // create the decoration frame for the client window
502 client->frame = new Frame(client, &_style);
504 // add to the wm's map
505 openbox->addClient(client->frame->window(), client);
506 openbox->addClient(client->frame->plate(), client);
507 openbox->addClient(client->frame->titlebar(), client);
508 openbox->addClient(client->frame->label(), client);
509 openbox->addClient(client->frame->button_max(), client);
510 openbox->addClient(client->frame->button_iconify(), client);
511 openbox->addClient(client->frame->button_stick(), client);
512 openbox->addClient(client->frame->button_close(), client);
513 openbox->addClient(client->frame->handle(), client);
514 openbox->addClient(client->frame->grip_left(), client);
515 openbox->addClient(client->frame->grip_right(), client);
517 // reparent the client to the frame
518 client->frame->grabClient();
520 // if on the current desktop.. (or all desktops)
521 if (client->desktop() == _desktop ||
522 client->desktop() == (signed)0xffffffff) {
523 client->frame->show();
526 // XXX: handle any requested states such as maximized
528 otk::display->ungrab();
530 // add to the screen's list
531 clients.push_back(client);
532 // this puts into the stacking order, then raises it
533 _stacking.push_back(client);
534 restack(true, client);
535 // update the root properties
538 openbox->bindings()->grabButtons(true, client);
540 // call the python NEWWINDOW binding
541 EventData data(_number, client, EventNewWindow, 0);
542 openbox->bindings()->fireEvent(&data);
545 printf("Managed window 0x%lx\n", window);
550 void Screen::unmanageWindow(Client *client)
552 Frame *frame = client->frame;
554 // call the python CLOSEWINDOW binding
555 EventData data(_number, client, EventCloseWindow, 0);
556 openbox->bindings()->fireEvent(&data);
558 openbox->bindings()->grabButtons(false, client);
560 // remove from the wm's map
561 openbox->removeClient(client->window());
562 openbox->removeClient(frame->window());
563 openbox->removeClient(frame->plate());
564 openbox->removeClient(frame->titlebar());
565 openbox->removeClient(frame->label());
566 openbox->removeClient(frame->button_max());
567 openbox->removeClient(frame->button_iconify());
568 openbox->removeClient(frame->button_stick());
569 openbox->removeClient(frame->button_close());
570 openbox->removeClient(frame->handle());
571 openbox->removeClient(frame->grip_left());
572 openbox->removeClient(frame->grip_right());
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();
590 delete client->frame;
593 // remove from the stacking order
594 _stacking.remove(client);
596 // remove from the screen's list
597 clients.remove(client);
599 // unfocus the client (calls the focus callbacks)
603 printf("Unmanaged window 0x%lx\n", client->window());
608 // update the root properties
612 void Screen::restack(bool raise, Client *client)
614 const int layer = client->layer();
615 std::vector<Window> wins;
617 _stacking.remove(client);
619 // the stacking list is from highest to lowest
621 Client::List::iterator it = _stacking.begin(), end = _stacking.end();
622 // insert the windows above this window
623 for (; it != end; ++it) {
624 if ((*it)->layer() < layer || (raise && (*it)->layer() == layer))
626 wins.push_back((*it)->frame->window());
629 wins.push_back(client->frame->window());
630 _stacking.insert(it, client);
631 // insert the remaining below this window
632 for (; it != end; ++it)
633 wins.push_back((*it)->frame->window());
635 XRestackWindows(**otk::display, &wins[0], wins.size());
636 changeStackingList();
639 void Screen::changeDesktop(long desktop)
641 if (!(desktop >= 0 && desktop < _num_desktops)) return;
643 printf("Moving to desktop %ld\n", desktop);
648 openbox->property()->set(_info->rootWindow(),
649 otk::Property::net_current_desktop,
650 otk::Property::Atom_Cardinal,
653 if (old == _desktop) return;
655 Client::List::iterator it, end = clients.end();
656 for (it = clients.begin(); it != end; ++it) {
657 if ((*it)->desktop() == old) {
658 (*it)->frame->hide();
659 } else if ((*it)->desktop() == _desktop) {
660 (*it)->frame->show();
664 // force the callbacks to fire
665 if (!openbox->focusedClient())
666 openbox->setFocusedClient(0);
669 void Screen::changeNumDesktops(long num)
673 if (!(num > 0)) return;
675 // XXX: move windows on desktops that will no longer exist!
678 openbox->property()->set(_info->rootWindow(),
679 otk::Property::net_number_of_desktops,
680 otk::Property::Atom_Cardinal,
683 // set the viewport hint
684 unsigned long *viewport = new unsigned long[_num_desktops * 2];
685 memset(viewport, 0, sizeof(unsigned long) * _num_desktops * 2);
686 openbox->property()->set(_info->rootWindow(),
687 otk::Property::net_desktop_viewport,
688 otk::Property::Atom_Cardinal,
689 viewport, _num_desktops * 2);
692 // update the work area hint
697 void Screen::updateDesktopNames()
699 const otk::Property *property = openbox->property();
701 unsigned long num = (unsigned) -1;
703 if (!property->get(_info->rootWindow(),
704 otk::Property::net_desktop_names,
705 otk::Property::utf8, &num, &_desktop_names))
706 _desktop_names.clear();
707 while ((long)_desktop_names.size() < _num_desktops)
708 _desktop_names.push_back("Unnamed");
712 void Screen::setDesktopName(long i, const otk::ustring &name)
716 if (i >= _num_desktops) return;
718 const otk::Property *property = openbox->property();
720 otk::Property::StringVect newnames = _desktop_names;
722 property->set(_info->rootWindow(), otk::Property::net_desktop_names,
723 otk::Property::utf8, newnames);
727 void Screen::propertyHandler(const XPropertyEvent &e)
729 otk::EventHandler::propertyHandler(e);
731 const otk::Property *property = openbox->property();
733 // compress changes to a single property into a single change
735 while (XCheckTypedEvent(**otk::display, e.type, &ce)) {
736 // XXX: it would be nice to compress ALL changes to a property, not just
737 // changes in a row without other props between.
738 if (ce.xproperty.atom != e.atom) {
739 XPutBackEvent(**otk::display, &ce);
744 if (e.atom == property->atom(otk::Property::net_desktop_names))
745 updateDesktopNames();
749 void Screen::clientMessageHandler(const XClientMessageEvent &e)
751 otk::EventHandler::clientMessageHandler(e);
753 if (e.format != 32) return;
755 const otk::Property *property = openbox->property();
757 if (e.message_type == property->atom(otk::Property::net_current_desktop)) {
758 changeDesktop(e.data.l[0]);
759 } else if (e.message_type ==
760 property->atom(otk::Property::net_number_of_desktops)) {
761 changeNumDesktops(e.data.l[0]);
763 // XXX: so many client messages to handle here! ..or not.. they go to clients
767 void Screen::mapRequestHandler(const XMapRequestEvent &e)
769 otk::EventHandler::mapRequestHandler(e);
772 printf("MapRequest for 0x%lx\n", e.window);
776 MapRequest events come here even after the window exists instead of going
777 right to the client window, because of how they are sent and their struct
780 Client *c = openbox->findClient(e.window);
783 // send a net_active_window message
785 ce.xclient.type = ClientMessage;
786 ce.xclient.message_type =
787 openbox->property()->atom(otk::Property::net_active_window);
788 ce.xclient.display = **otk::display;
789 ce.xclient.window = c->window();
790 ce.xclient.format = 32;
791 ce.xclient.data.l[0] = 0l;
792 ce.xclient.data.l[1] = 0l;
793 ce.xclient.data.l[2] = 0l;
794 ce.xclient.data.l[3] = 0l;
795 ce.xclient.data.l[4] = 0l;
796 XSendEvent(**otk::display, _info->rootWindow(), false,
797 SubstructureRedirectMask | SubstructureNotifyMask,
800 manageWindow(e.window);