1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
10 #endif // HAVE_STDIO_H
13 # include <sys/types.h>
15 #endif // HAVE_UNISTD_H
18 #define _(str) gettext(str)
25 #include "bindings.hh"
27 #include "otk/display.hh"
32 static int anotherWMRunning(Display *display, XErrorEvent *) {
33 printf(_("Another window manager already running on display %s.\n"),
34 DisplayString(display));
43 OBScreen::OBScreen(int screen)
47 assert(screen >= 0); assert(screen < ScreenCount(otk::OBDisplay::display));
48 _info = otk::OBDisplay::screenInfo(screen);
51 XErrorHandler old = XSetErrorHandler(::anotherWMRunning);
52 XSelectInput(otk::OBDisplay::display, _info->rootWindow(),
53 OBScreen::event_mask);
54 XSync(otk::OBDisplay::display, false);
55 XSetErrorHandler(old);
57 _managed = !::running;
58 if (! _managed) return; // was unable to manage the screen
60 printf(_("Managing screen %d: visual 0x%lx, depth %d\n"),
61 _number, XVisualIDFromVisual(_info->visual()), _info->depth());
63 Openbox::instance->property()->set(_info->rootWindow(),
64 otk::OBProperty::openbox_pid,
65 otk::OBProperty::Atom_Cardinal,
66 (unsigned long) getpid());
68 // set the mouse cursor for the root window (the default cursor)
69 XDefineCursor(otk::OBDisplay::display, _info->rootWindow(),
70 Openbox::instance->cursors().session);
72 // initialize the shit that is used for all drawing on the screen
73 _image_control = new otk::BImageControl(Openbox::instance->timerManager(),
75 _image_control->installRootColormap();
76 _root_cmap_installed = True;
78 // initialize the screen's style
79 _style.setImageControl(_image_control);
80 std::string stylepath;
81 python_get_string("theme", &stylepath);
82 otk::Configuration sconfig(false);
83 sconfig.setFile(otk::expandTilde(stylepath));
84 if (!sconfig.load()) {
85 sconfig.setFile(otk::expandTilde(DEFAULTSTYLE));
86 if (!sconfig.load()) {
87 printf(_("Unable to load default style: %s. Aborting.\n"), DEFAULTSTYLE);
93 // set up notification of netwm support
96 // Set the netwm atoms for geomtery and viewport
97 unsigned long geometry[] = { _info->width(),
99 Openbox::instance->property()->set(_info->rootWindow(),
100 otk::OBProperty::net_desktop_geometry,
101 otk::OBProperty::Atom_Cardinal,
103 unsigned long viewport[] = { 0, 0 };
104 Openbox::instance->property()->set(_info->rootWindow(),
105 otk::OBProperty::net_desktop_viewport,
106 otk::OBProperty::Atom_Cardinal,
109 // create the window which gets focus when no clients get it
110 XSetWindowAttributes attr;
111 attr.override_redirect = true;
112 _focuswindow = XCreateWindow(otk::OBDisplay::display, _info->rootWindow(),
113 -100, -100, 1, 1, 0, 0, InputOnly,
114 _info->visual(), CWOverrideRedirect, &attr);
115 XMapWindow(otk::OBDisplay::display, _focuswindow);
117 // these may be further updated if any pre-existing windows are found in
118 // the manageExising() function
119 setClientList(); // initialize the client lists, which will be empty
120 calcArea(); // initialize the available working area
124 OBScreen::~OBScreen()
126 if (! _managed) return;
128 XSelectInput(otk::OBDisplay::display, _info->rootWindow(), NoEventMask);
130 // unmanage all windows
131 while (!clients.empty())
132 unmanageWindow(clients.front());
134 XDestroyWindow(otk::OBDisplay::display, _focuswindow);
135 XDestroyWindow(otk::OBDisplay::display, _supportwindow);
137 delete _image_control;
141 void OBScreen::manageExisting()
143 unsigned int i, j, nchild;
144 Window r, p, *children;
145 XQueryTree(otk::OBDisplay::display, _info->rootWindow(), &r, &p,
148 // preen the window list of all icon windows... for better dockapp support
149 for (i = 0; i < nchild; i++) {
150 if (children[i] == None) continue;
152 XWMHints *wmhints = XGetWMHints(otk::OBDisplay::display,
156 if ((wmhints->flags & IconWindowHint) &&
157 (wmhints->icon_window != children[i])) {
158 for (j = 0; j < nchild; j++) {
159 if (children[j] == wmhints->icon_window) {
170 // manage shown windows
171 for (i = 0; i < nchild; ++i) {
172 if (children[i] == None)
175 XWindowAttributes attrib;
176 if (XGetWindowAttributes(otk::OBDisplay::display, children[i], &attrib)) {
177 if (attrib.override_redirect) continue;
179 if (attrib.map_state != IsUnmapped) {
180 manageWindow(children[i]);
189 //! Adds a window's strut to the screen's list of reserved spaces
190 void OBScreen::addStrut(otk::Strut *strut)
192 _struts.push_back(strut);
196 //! Removes a window's strut from the screen's list of reserved spaces
197 void OBScreen::removeStrut(otk::Strut *strut)
199 _struts.remove(strut);
203 void OBScreen::calcArea()
205 otk::Rect old_area = _area;
209 // reset to the full areas
210 if (isXineramaActive())
211 xineramaUsableArea = getXineramaAreas();
215 /* these values represent offsets from the screen edge
216 * we look for the biggest offset on each edge and then apply them
218 * do not be confused by the similarity to the names of Rect's members
220 unsigned int current_left = 0, current_right = 0, current_top = 0,
223 StrutList::const_iterator it = _struts.begin(), end = _struts.end();
225 for(; it != end; ++it) {
226 otk::Strut *strut = *it;
227 if (strut->left > current_left)
228 current_left = strut->left;
229 if (strut->top > current_top)
230 current_top = strut->top;
231 if (strut->right > current_right)
232 current_right = strut->right;
233 if (strut->bottom > current_bottom)
234 current_bottom = strut->bottom;
237 _area.setRect(current_left, current_top,
238 _info->width() - (current_left + current_right),
239 _info->height() - (current_top + current_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 // XXX: re-maximize windows
271 void OBScreen::setSupportedAtoms()
273 // create the netwm support window
274 _supportwindow = XCreateSimpleWindow(otk::OBDisplay::display,
276 0, 0, 1, 1, 0, 0, 0);
277 assert(_supportwindow != None);
279 // set supporting window
280 Openbox::instance->property()->set(_info->rootWindow(),
281 otk::OBProperty::net_supporting_wm_check,
282 otk::OBProperty::Atom_Window,
285 //set properties on the supporting window
286 Openbox::instance->property()->set(_supportwindow,
287 otk::OBProperty::net_wm_name,
288 otk::OBProperty::utf8,
290 Openbox::instance->property()->set(_supportwindow,
291 otk::OBProperty::net_supporting_wm_check,
292 otk::OBProperty::Atom_Window,
298 otk::OBProperty::net_current_desktop,
299 otk::OBProperty::net_number_of_desktops,
301 otk::OBProperty::net_desktop_geometry,
302 otk::OBProperty::net_desktop_viewport,
303 otk::OBProperty::net_active_window,
304 otk::OBProperty::net_workarea,
305 otk::OBProperty::net_client_list,
306 otk::OBProperty::net_client_list_stacking,
308 otk::OBProperty::net_desktop_names,
310 otk::OBProperty::net_close_window,
311 otk::OBProperty::net_wm_name,
312 otk::OBProperty::net_wm_visible_name,
313 otk::OBProperty::net_wm_icon_name,
314 otk::OBProperty::net_wm_visible_icon_name,
316 otk::OBProperty::net_wm_desktop,
319 otk::OBProperty::net_wm_strut,
321 otk::OBProperty::net_wm_window_type,
322 otk::OBProperty::net_wm_window_type_desktop,
323 otk::OBProperty::net_wm_window_type_dock,
324 otk::OBProperty::net_wm_window_type_toolbar,
325 otk::OBProperty::net_wm_window_type_menu,
326 otk::OBProperty::net_wm_window_type_utility,
327 otk::OBProperty::net_wm_window_type_splash,
328 otk::OBProperty::net_wm_window_type_dialog,
329 otk::OBProperty::net_wm_window_type_normal,
331 otk::OBProperty::net_wm_moveresize,
332 otk::OBProperty::net_wm_moveresize_size_topleft,
333 otk::OBProperty::net_wm_moveresize_size_topright,
334 otk::OBProperty::net_wm_moveresize_size_bottomleft,
335 otk::OBProperty::net_wm_moveresize_size_bottomright,
336 otk::OBProperty::net_wm_moveresize_move,
339 otk::OBProperty::net_wm_allowed_actions,
340 otk::OBProperty::net_wm_action_move,
341 otk::OBProperty::net_wm_action_resize,
342 otk::OBProperty::net_wm_action_shade,
343 otk::OBProperty::net_wm_action_maximize_horz,
344 otk::OBProperty::net_wm_action_maximize_vert,
345 otk::OBProperty::net_wm_action_change_desktop,
346 otk::OBProperty::net_wm_action_close,
348 otk::OBProperty::net_wm_state,
349 otk::OBProperty::net_wm_state_modal,
350 otk::OBProperty::net_wm_state_maximized_vert,
351 otk::OBProperty::net_wm_state_maximized_horz,
352 otk::OBProperty::net_wm_state_shaded,
353 otk::OBProperty::net_wm_state_skip_taskbar,
354 otk::OBProperty::net_wm_state_skip_pager,
355 otk::OBProperty::net_wm_state_hidden,
356 otk::OBProperty::net_wm_state_fullscreen,
357 otk::OBProperty::net_wm_state_above,
358 otk::OBProperty::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)
364 supported[i] = Openbox::instance->property()->atom(supported[i]);
366 Openbox::instance->property()->set(_info->rootWindow(),
367 otk::OBProperty::net_supported,
368 otk::OBProperty::Atom_Atom,
369 supported, num_supported);
373 void OBScreen::setClientList()
376 unsigned int size = clients.size();
378 // create an array of the window ids
382 windows = new Window[size];
384 ClientList::const_iterator it = clients.begin();
385 const ClientList::const_iterator end = clients.end();
386 for (; it != end; ++it, ++win_it)
387 *win_it = (*it)->window();
389 windows = (Window*) 0;
391 Openbox::instance->property()->set(_info->rootWindow(),
392 otk::OBProperty::net_client_list,
393 otk::OBProperty::Atom_Window,
403 void OBScreen::setStackingList()
406 unsigned int size = _stacking.size();
408 assert(size == clients.size()); // just making sure.. :)
411 // create an array of the window ids
415 windows = new Window[size];
417 ClientList::const_iterator it = _stacking.begin();
418 const ClientList::const_iterator end = _stacking.end();
419 for (; it != end; ++it, ++win_it)
420 *win_it = (*it)->window();
422 windows = (Window*) 0;
424 Openbox::instance->property()->set(_info->rootWindow(),
425 otk::OBProperty::net_client_list_stacking,
426 otk::OBProperty::Atom_Window,
434 void OBScreen::setWorkArea() {
435 unsigned long area[] = { _area.x(), _area.y(),
436 _area.width(), _area.height() };
437 Openbox::instance->property()->set(_info->rootWindow(),
438 otk::OBProperty::net_workarea,
439 otk::OBProperty::Atom_Cardinal,
442 if (workspacesList.size() > 0) {
443 unsigned long *dims = new unsigned long[4 * workspacesList.size()];
444 for (unsigned int i = 0, m = workspacesList.size(); i < m; ++i) {
445 // XXX: this could be different for each workspace
446 const otk::Rect &area = availableArea();
447 dims[(i * 4) + 0] = area.x();
448 dims[(i * 4) + 1] = area.y();
449 dims[(i * 4) + 2] = area.width();
450 dims[(i * 4) + 3] = area.height();
452 xatom->set(getRootWindow(), otk::OBProperty::net_workarea,
453 otk::OBProperty::Atom_Cardinal,
454 dims, 4 * workspacesList.size());
457 xatom->set(getRootWindow(), otk::OBProperty::net_workarea,
458 otk::OBProperty::Atom_Cardinal, 0, 0);
463 void OBScreen::manageWindow(Window window)
465 OBClient *client = 0;
467 XSetWindowAttributes attrib_set;
469 // is the window a docking app
470 if ((wmhint = XGetWMHints(otk::OBDisplay::display, window))) {
471 if ((wmhint->flags & StateHint) &&
472 wmhint->initial_state == WithdrawnState) {
473 //slit->addClient(w); // XXX: make dock apps work!
480 otk::OBDisplay::grab();
482 // choose the events we want to receive on the CLIENT window
483 attrib_set.event_mask = OBClient::event_mask;
484 attrib_set.do_not_propagate_mask = OBClient::no_propagate_mask;
485 XChangeWindowAttributes(otk::OBDisplay::display, window,
486 CWEventMask|CWDontPropagate, &attrib_set);
488 // create the OBClient class, which gets all of the hints on the window
489 client = new OBClient(_number, window);
490 // register for events
491 Openbox::instance->registerHandler(window, client);
492 // add to the wm's map
493 Openbox::instance->addClient(window, client);
495 // we dont want a border on the client
496 client->toggleClientBorder(false);
498 // specify that if we exit, the window should not be destroyed and should be
499 // reparented back to root automatically
500 XChangeSaveSet(otk::OBDisplay::display, window, SetModeInsert);
502 if (!client->positionRequested()) {
503 // XXX: position the window intelligenty
506 // create the decoration frame for the client window
507 client->frame = new OBFrame(client, &_style);
509 // add to the wm's map
510 Openbox::instance->addClient(client->frame->window(), client);
511 Openbox::instance->addClient(client->frame->plate(), client);
512 Openbox::instance->addClient(client->frame->titlebar(), client);
513 Openbox::instance->addClient(client->frame->label(), client);
514 Openbox::instance->addClient(client->frame->button_max(), client);
515 Openbox::instance->addClient(client->frame->button_iconify(), client);
516 Openbox::instance->addClient(client->frame->button_stick(), client);
517 Openbox::instance->addClient(client->frame->button_close(), client);
518 Openbox::instance->addClient(client->frame->handle(), client);
519 Openbox::instance->addClient(client->frame->grip_left(), client);
520 Openbox::instance->addClient(client->frame->grip_right(), client);
522 // XXX: if on the current desktop..
523 client->frame->show();
525 // XXX: handle any requested states such as shaded/maximized
527 otk::OBDisplay::ungrab();
529 // add to the screen's list
530 clients.push_back(client);
531 // this puts into the stacking order, then raises it
532 _stacking.push_back(client);
533 restack(true, client);
534 // update the root properties
537 Openbox::instance->bindings()->grabButtons(true, client);
541 void OBScreen::unmanageWindow(OBClient *client)
543 OBFrame *frame = client->frame;
545 Openbox::instance->bindings()->grabButtons(false, client);
547 // remove from the stacking order
548 _stacking.remove(client);
550 // pass around focus if this window was focused XXX do this better!
551 if (Openbox::instance->focusedClient() == client) {
552 OBClient *newfocus = 0;
553 if (!_stacking.empty())
554 newfocus = _stacking.front();
555 if (! (newfocus && newfocus->focus()))
559 // remove from the wm's map
560 Openbox::instance->removeClient(client->window());
561 Openbox::instance->removeClient(frame->window());
562 Openbox::instance->removeClient(frame->plate());
563 Openbox::instance->removeClient(frame->titlebar());
564 Openbox::instance->removeClient(frame->label());
565 Openbox::instance->removeClient(frame->button_max());
566 Openbox::instance->removeClient(frame->button_iconify());
567 Openbox::instance->removeClient(frame->button_stick());
568 Openbox::instance->removeClient(frame->button_close());
569 Openbox::instance->removeClient(frame->handle());
570 Openbox::instance->removeClient(frame->grip_left());
571 Openbox::instance->removeClient(frame->grip_right());
572 // unregister for handling events
573 Openbox::instance->clearHandler(client->window());
575 // remove the window from our save set
576 XChangeSaveSet(otk::OBDisplay::display, client->window(), SetModeDelete);
578 // we dont want events no more
579 XSelectInput(otk::OBDisplay::display, client->window(), NoEventMask);
583 // give the client its border back
584 client->toggleClientBorder(true);
586 delete client->frame;
589 // remove from the screen's list
590 clients.remove(client);
593 // update the root properties
597 void OBScreen::restack(bool raise, OBClient *client)
599 const int layer = client->layer();
600 std::vector<Window> wins;
602 _stacking.remove(client);
604 // the stacking list is from highest to lowest
606 ClientList::iterator it = _stacking.begin(), end = _stacking.end();
607 // insert the windows above this window
608 for (; it != end; ++it) {
609 if ((*it)->layer() < layer || (raise && (*it)->layer() == layer))
611 wins.push_back((*it)->frame->window());
614 wins.push_back(client->frame->window());
615 _stacking.insert(it, client);
616 // insert the remaining below this window
617 for (; it != end; ++it)
618 wins.push_back((*it)->frame->window());
620 XRestackWindows(otk::OBDisplay::display, &wins[0], wins.size());