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 the netwm atoms for geomtery and viewport
94 unsigned long geometry
[] = { _info
->width(),
96 Openbox::instance
->property()->set(_info
->rootWindow(),
97 otk::OBProperty::net_desktop_geometry
,
98 otk::OBProperty::Atom_Cardinal
,
100 unsigned long viewport
[] = { 0, 0 };
101 Openbox::instance
->property()->set(_info
->rootWindow(),
102 otk::OBProperty::net_desktop_viewport
,
103 otk::OBProperty::Atom_Cardinal
,
106 // create the window which gets focus when no clients get it
107 XSetWindowAttributes attr
;
108 attr
.override_redirect
= true;
109 _focuswindow
= XCreateWindow(otk::OBDisplay::display
, _info
->rootWindow(),
110 -100, -100, 1, 1, 0, 0, InputOnly
,
111 _info
->visual(), CWOverrideRedirect
, &attr
);
112 XMapWindow(otk::OBDisplay::display
, _focuswindow
);
114 // these may be further updated if any pre-existing windows are found in
115 // the manageExising() function
116 setClientList(); // initialize the client lists, which will be empty
117 calcArea(); // initialize the available working area
121 OBScreen::~OBScreen()
123 if (! _managed
) return;
125 XSelectInput(otk::OBDisplay::display
, _info
->rootWindow(), NoEventMask
);
127 // unmanage all windows
128 while (!clients
.empty())
129 unmanageWindow(clients
.front());
131 XDestroyWindow(otk::OBDisplay::display
, _focuswindow
);
133 delete _image_control
;
137 void OBScreen::manageExisting()
139 unsigned int i
, j
, nchild
;
140 Window r
, p
, *children
;
141 XQueryTree(otk::OBDisplay::display
, _info
->rootWindow(), &r
, &p
,
144 // preen the window list of all icon windows... for better dockapp support
145 for (i
= 0; i
< nchild
; i
++) {
146 if (children
[i
] == None
) continue;
148 XWMHints
*wmhints
= XGetWMHints(otk::OBDisplay::display
,
152 if ((wmhints
->flags
& IconWindowHint
) &&
153 (wmhints
->icon_window
!= children
[i
])) {
154 for (j
= 0; j
< nchild
; j
++) {
155 if (children
[j
] == wmhints
->icon_window
) {
166 // manage shown windows
167 for (i
= 0; i
< nchild
; ++i
) {
168 if (children
[i
] == None
)
171 XWindowAttributes attrib
;
172 if (XGetWindowAttributes(otk::OBDisplay::display
, children
[i
], &attrib
)) {
173 if (attrib
.override_redirect
) continue;
175 if (attrib
.map_state
!= IsUnmapped
) {
176 manageWindow(children
[i
]);
185 //! Adds a window's strut to the screen's list of reserved spaces
186 void OBScreen::addStrut(otk::Strut
*strut
)
188 _struts
.push_back(strut
);
192 //! Removes a window's strut from the screen's list of reserved spaces
193 void OBScreen::removeStrut(otk::Strut
*strut
)
195 _struts
.remove(strut
);
199 void OBScreen::calcArea()
201 otk::Rect old_area
= _area
;
205 // reset to the full areas
206 if (isXineramaActive())
207 xineramaUsableArea = getXineramaAreas();
211 /* these values represent offsets from the screen edge
212 * we look for the biggest offset on each edge and then apply them
214 * do not be confused by the similarity to the names of Rect's members
216 unsigned int current_left
= 0, current_right
= 0, current_top
= 0,
219 StrutList::const_iterator it
= _struts
.begin(), end
= _struts
.end();
221 for(; it
!= end
; ++it
) {
222 otk::Strut
*strut
= *it
;
223 if (strut
->left
> current_left
)
224 current_left
= strut
->left
;
225 if (strut
->top
> current_top
)
226 current_top
= strut
->top
;
227 if (strut
->right
> current_right
)
228 current_right
= strut
->right
;
229 if (strut
->bottom
> current_bottom
)
230 current_bottom
= strut
->bottom
;
233 _area
.setRect(current_left
, current_top
,
234 _info
->width() - (current_left
+ current_right
),
235 _info
->height() - (current_top
+ current_bottom
));
239 if (isXineramaActive()) {
240 // keep each of the ximerama-defined areas inside the strut
241 RectList::iterator xit, xend = xineramaUsableArea.end();
242 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
243 if (xit->x() < usableArea.x()) {
244 xit->setX(usableArea.x());
245 xit->setWidth(xit->width() - usableArea.x());
247 if (xit->y() < usableArea.y()) {
248 xit->setY(usableArea.y());
249 xit->setHeight(xit->height() - usableArea.y());
251 if (xit->x() + xit->width() > usableArea.width())
252 xit->setWidth(usableArea.width() - xit->x());
253 if (xit->y() + xit->height() > usableArea.height())
254 xit->setHeight(usableArea.height() - xit->y());
260 if (old_area
!= _area
)
261 // XXX: re-maximize windows
267 void OBScreen::setClientList()
270 unsigned int size
= clients
.size();
272 // create an array of the window ids
276 windows
= new Window
[size
];
278 ClientList::const_iterator it
= clients
.begin();
279 const ClientList::const_iterator end
= clients
.end();
280 for (; it
!= end
; ++it
, ++win_it
)
281 *win_it
= (*it
)->window();
283 windows
= (Window
*) 0;
285 Openbox::instance
->property()->set(_info
->rootWindow(),
286 otk::OBProperty::net_client_list
,
287 otk::OBProperty::Atom_Window
,
297 void OBScreen::setStackingList()
300 unsigned int size
= _stacking
.size();
302 assert(size
== clients
.size()); // just making sure.. :)
305 // create an array of the window ids
309 windows
= new Window
[size
];
311 ClientList::const_iterator it
= _stacking
.begin();
312 const ClientList::const_iterator end
= _stacking
.end();
313 for (; it
!= end
; ++it
, ++win_it
)
314 *win_it
= (*it
)->window();
316 windows
= (Window
*) 0;
318 Openbox::instance
->property()->set(_info
->rootWindow(),
319 otk::OBProperty::net_client_list_stacking
,
320 otk::OBProperty::Atom_Window
,
328 void OBScreen::setWorkArea() {
329 unsigned long area
[] = { _area
.x(), _area
.y(),
330 _area
.width(), _area
.height() };
331 Openbox::instance
->property()->set(_info
->rootWindow(),
332 otk::OBProperty::net_workarea
,
333 otk::OBProperty::Atom_Cardinal
,
336 if (workspacesList.size() > 0) {
337 unsigned long *dims = new unsigned long[4 * workspacesList.size()];
338 for (unsigned int i = 0, m = workspacesList.size(); i < m; ++i) {
339 // XXX: this could be different for each workspace
340 const otk::Rect &area = availableArea();
341 dims[(i * 4) + 0] = area.x();
342 dims[(i * 4) + 1] = area.y();
343 dims[(i * 4) + 2] = area.width();
344 dims[(i * 4) + 3] = area.height();
346 xatom->set(getRootWindow(), otk::OBProperty::net_workarea,
347 otk::OBProperty::Atom_Cardinal,
348 dims, 4 * workspacesList.size());
351 xatom->set(getRootWindow(), otk::OBProperty::net_workarea,
352 otk::OBProperty::Atom_Cardinal, 0, 0);
357 void OBScreen::manageWindow(Window window
)
359 OBClient
*client
= 0;
361 XSetWindowAttributes attrib_set
;
363 // is the window a docking app
364 if ((wmhint
= XGetWMHints(otk::OBDisplay::display
, window
))) {
365 if ((wmhint
->flags
& StateHint
) &&
366 wmhint
->initial_state
== WithdrawnState
) {
367 //slit->addClient(w); // XXX: make dock apps work!
374 otk::OBDisplay::grab();
376 // choose the events we want to receive on the CLIENT window
377 attrib_set
.event_mask
= OBClient::event_mask
;
378 attrib_set
.do_not_propagate_mask
= OBClient::no_propagate_mask
;
379 XChangeWindowAttributes(otk::OBDisplay::display
, window
,
380 CWEventMask
|CWDontPropagate
, &attrib_set
);
382 // create the OBClient class, which gets all of the hints on the window
383 client
= new OBClient(_number
, window
);
384 // register for events
385 Openbox::instance
->registerHandler(window
, client
);
386 // add to the wm's map
387 Openbox::instance
->addClient(window
, client
);
389 // we dont want a border on the client
390 client
->toggleClientBorder(false);
392 // specify that if we exit, the window should not be destroyed and should be
393 // reparented back to root automatically
394 XChangeSaveSet(otk::OBDisplay::display
, window
, SetModeInsert
);
396 if (!client
->positionRequested()) {
397 // XXX: position the window intelligenty
400 // create the decoration frame for the client window
401 client
->frame
= new OBFrame(client
, &_style
);
403 // add to the wm's map
404 Openbox::instance
->addClient(client
->frame
->window(), client
);
405 Openbox::instance
->addClient(client
->frame
->plate(), client
);
406 Openbox::instance
->addClient(client
->frame
->titlebar(), client
);
407 Openbox::instance
->addClient(client
->frame
->label(), client
);
408 Openbox::instance
->addClient(client
->frame
->button_max(), client
);
409 Openbox::instance
->addClient(client
->frame
->button_iconify(), client
);
410 Openbox::instance
->addClient(client
->frame
->button_stick(), client
);
411 Openbox::instance
->addClient(client
->frame
->button_close(), client
);
412 Openbox::instance
->addClient(client
->frame
->handle(), client
);
413 Openbox::instance
->addClient(client
->frame
->grip_left(), client
);
414 Openbox::instance
->addClient(client
->frame
->grip_right(), client
);
416 // XXX: if on the current desktop..
417 client
->frame
->show();
419 // XXX: handle any requested states such as shaded/maximized
421 otk::OBDisplay::ungrab();
423 // add to the screen's list
424 clients
.push_back(client
);
425 // this puts into the stacking order, then raises it
426 _stacking
.push_back(client
);
427 restack(true, client
);
428 // update the root properties
431 Openbox::instance
->bindings()->grabButtons(true, client
);
435 void OBScreen::unmanageWindow(OBClient
*client
)
437 OBFrame
*frame
= client
->frame
;
439 Openbox::instance
->bindings()->grabButtons(false, client
);
441 // remove from the stacking order
442 _stacking
.remove(client
);
444 // pass around focus if this window was focused XXX do this better!
445 if (Openbox::instance
->focusedClient() == client
) {
446 OBClient
*newfocus
= 0;
447 if (!_stacking
.empty())
448 newfocus
= _stacking
.front();
449 if (! (newfocus
&& newfocus
->focus()))
453 // remove from the wm's map
454 Openbox::instance
->removeClient(client
->window());
455 Openbox::instance
->removeClient(frame
->window());
456 Openbox::instance
->removeClient(frame
->plate());
457 Openbox::instance
->removeClient(frame
->titlebar());
458 Openbox::instance
->removeClient(frame
->label());
459 Openbox::instance
->removeClient(frame
->button_max());
460 Openbox::instance
->removeClient(frame
->button_iconify());
461 Openbox::instance
->removeClient(frame
->button_stick());
462 Openbox::instance
->removeClient(frame
->button_close());
463 Openbox::instance
->removeClient(frame
->handle());
464 Openbox::instance
->removeClient(frame
->grip_left());
465 Openbox::instance
->removeClient(frame
->grip_right());
466 // unregister for handling events
467 Openbox::instance
->clearHandler(client
->window());
469 // remove the window from our save set
470 XChangeSaveSet(otk::OBDisplay::display
, client
->window(), SetModeDelete
);
472 // we dont want events no more
473 XSelectInput(otk::OBDisplay::display
, client
->window(), NoEventMask
);
477 // give the client its border back
478 client
->toggleClientBorder(true);
480 delete client
->frame
;
483 // remove from the screen's list
484 clients
.remove(client
);
487 // update the root properties
491 void OBScreen::restack(bool raise
, OBClient
*client
)
493 const int layer
= client
->layer();
494 std::vector
<Window
> wins
;
496 _stacking
.remove(client
);
498 // the stacking list is from highest to lowest
500 ClientList::iterator it
= _stacking
.begin(), end
= _stacking
.end();
501 // insert the windows above this window
502 for (; it
!= end
; ++it
) {
503 if ((*it
)->layer() < layer
|| (raise
&& (*it
)->layer() == layer
))
505 wins
.push_back((*it
)->frame
->window());
508 wins
.push_back(client
->frame
->window());
509 _stacking
.insert(it
, client
);
510 // insert the remaining below this window
511 for (; it
!= end
; ++it
)
512 wins
.push_back((*it
)->frame
->window());
514 XRestackWindows(otk::OBDisplay::display
, &wins
[0], wins
.size());