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
)
365 Openbox::instance
->property()->atom((otk::OBProperty::Atoms
)supported
[i
]);
367 Openbox::instance
->property()->set(_info
->rootWindow(),
368 otk::OBProperty::net_supported
,
369 otk::OBProperty::Atom_Atom
,
370 supported
, num_supported
);
374 void OBScreen::setClientList()
377 unsigned int size
= clients
.size();
379 // create an array of the window ids
383 windows
= new Window
[size
];
385 ClientList::const_iterator it
= clients
.begin();
386 const ClientList::const_iterator end
= clients
.end();
387 for (; it
!= end
; ++it
, ++win_it
)
388 *win_it
= (*it
)->window();
390 windows
= (Window
*) 0;
392 Openbox::instance
->property()->set(_info
->rootWindow(),
393 otk::OBProperty::net_client_list
,
394 otk::OBProperty::Atom_Window
,
404 void OBScreen::setStackingList()
407 unsigned int size
= _stacking
.size();
409 assert(size
== clients
.size()); // just making sure.. :)
412 // create an array of the window ids
416 windows
= new Window
[size
];
418 ClientList::const_iterator it
= _stacking
.begin();
419 const ClientList::const_iterator end
= _stacking
.end();
420 for (; it
!= end
; ++it
, ++win_it
)
421 *win_it
= (*it
)->window();
423 windows
= (Window
*) 0;
425 Openbox::instance
->property()->set(_info
->rootWindow(),
426 otk::OBProperty::net_client_list_stacking
,
427 otk::OBProperty::Atom_Window
,
435 void OBScreen::setWorkArea() {
436 unsigned long area
[] = { _area
.x(), _area
.y(),
437 _area
.width(), _area
.height() };
438 Openbox::instance
->property()->set(_info
->rootWindow(),
439 otk::OBProperty::net_workarea
,
440 otk::OBProperty::Atom_Cardinal
,
443 if (workspacesList.size() > 0) {
444 unsigned long *dims = new unsigned long[4 * workspacesList.size()];
445 for (unsigned int i = 0, m = workspacesList.size(); i < m; ++i) {
446 // XXX: this could be different for each workspace
447 const otk::Rect &area = availableArea();
448 dims[(i * 4) + 0] = area.x();
449 dims[(i * 4) + 1] = area.y();
450 dims[(i * 4) + 2] = area.width();
451 dims[(i * 4) + 3] = area.height();
453 xatom->set(getRootWindow(), otk::OBProperty::net_workarea,
454 otk::OBProperty::Atom_Cardinal,
455 dims, 4 * workspacesList.size());
458 xatom->set(getRootWindow(), otk::OBProperty::net_workarea,
459 otk::OBProperty::Atom_Cardinal, 0, 0);
464 void OBScreen::manageWindow(Window window
)
466 OBClient
*client
= 0;
468 XSetWindowAttributes attrib_set
;
470 // is the window a docking app
471 if ((wmhint
= XGetWMHints(otk::OBDisplay::display
, window
))) {
472 if ((wmhint
->flags
& StateHint
) &&
473 wmhint
->initial_state
== WithdrawnState
) {
474 //slit->addClient(w); // XXX: make dock apps work!
481 otk::OBDisplay::grab();
483 // choose the events we want to receive on the CLIENT window
484 attrib_set
.event_mask
= OBClient::event_mask
;
485 attrib_set
.do_not_propagate_mask
= OBClient::no_propagate_mask
;
486 XChangeWindowAttributes(otk::OBDisplay::display
, window
,
487 CWEventMask
|CWDontPropagate
, &attrib_set
);
489 // create the OBClient class, which gets all of the hints on the window
490 client
= new OBClient(_number
, window
);
491 // register for events
492 Openbox::instance
->registerHandler(window
, client
);
493 // add to the wm's map
494 Openbox::instance
->addClient(window
, client
);
496 // we dont want a border on the client
497 client
->toggleClientBorder(false);
499 // specify that if we exit, the window should not be destroyed and should be
500 // reparented back to root automatically
501 XChangeSaveSet(otk::OBDisplay::display
, window
, SetModeInsert
);
503 if (!client
->positionRequested()) {
504 // XXX: position the window intelligenty
507 // create the decoration frame for the client window
508 client
->frame
= new OBFrame(client
, &_style
);
510 // add to the wm's map
511 Openbox::instance
->addClient(client
->frame
->window(), client
);
512 Openbox::instance
->addClient(client
->frame
->plate(), client
);
513 Openbox::instance
->addClient(client
->frame
->titlebar(), client
);
514 Openbox::instance
->addClient(client
->frame
->label(), client
);
515 Openbox::instance
->addClient(client
->frame
->button_max(), client
);
516 Openbox::instance
->addClient(client
->frame
->button_iconify(), client
);
517 Openbox::instance
->addClient(client
->frame
->button_stick(), client
);
518 Openbox::instance
->addClient(client
->frame
->button_close(), client
);
519 Openbox::instance
->addClient(client
->frame
->handle(), client
);
520 Openbox::instance
->addClient(client
->frame
->grip_left(), client
);
521 Openbox::instance
->addClient(client
->frame
->grip_right(), client
);
523 // XXX: if on the current desktop..
524 client
->frame
->show();
526 // XXX: handle any requested states such as shaded/maximized
528 otk::OBDisplay::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::instance
->bindings()->grabButtons(true, client
);
542 void OBScreen::unmanageWindow(OBClient
*client
)
544 OBFrame
*frame
= client
->frame
;
546 Openbox::instance
->bindings()->grabButtons(false, client
);
548 // remove from the stacking order
549 _stacking
.remove(client
);
551 // pass around focus if this window was focused XXX do this better!
552 if (Openbox::instance
->focusedClient() == client
) {
553 OBClient
*newfocus
= 0;
554 if (!_stacking
.empty())
555 newfocus
= _stacking
.front();
556 if (! (newfocus
&& newfocus
->focus()))
560 // remove from the wm's map
561 Openbox::instance
->removeClient(client
->window());
562 Openbox::instance
->removeClient(frame
->window());
563 Openbox::instance
->removeClient(frame
->plate());
564 Openbox::instance
->removeClient(frame
->titlebar());
565 Openbox::instance
->removeClient(frame
->label());
566 Openbox::instance
->removeClient(frame
->button_max());
567 Openbox::instance
->removeClient(frame
->button_iconify());
568 Openbox::instance
->removeClient(frame
->button_stick());
569 Openbox::instance
->removeClient(frame
->button_close());
570 Openbox::instance
->removeClient(frame
->handle());
571 Openbox::instance
->removeClient(frame
->grip_left());
572 Openbox::instance
->removeClient(frame
->grip_right());
573 // unregister for handling events
574 Openbox::instance
->clearHandler(client
->window());
576 // remove the window from our save set
577 XChangeSaveSet(otk::OBDisplay::display
, client
->window(), SetModeDelete
);
579 // we dont want events no more
580 XSelectInput(otk::OBDisplay::display
, client
->window(), NoEventMask
);
584 // give the client its border back
585 client
->toggleClientBorder(true);
587 delete client
->frame
;
590 // remove from the screen's list
591 clients
.remove(client
);
594 // update the root properties
598 void OBScreen::restack(bool raise
, OBClient
*client
)
600 const int layer
= client
->layer();
601 std::vector
<Window
> wins
;
603 _stacking
.remove(client
);
605 // the stacking list is from highest to lowest
607 ClientList::iterator it
= _stacking
.begin(), end
= _stacking
.end();
608 // insert the windows above this window
609 for (; it
!= end
; ++it
) {
610 if ((*it
)->layer() < layer
|| (raise
&& (*it
)->layer() == layer
))
612 wins
.push_back((*it
)->frame
->window());
615 wins
.push_back(client
->frame
->window());
616 _stacking
.insert(it
, client
);
617 // insert the remaining below this window
618 for (; it
!= end
; ++it
)
619 wins
.push_back((*it
)->frame
->window());
621 XRestackWindows(otk::OBDisplay::display
, &wins
[0], wins
.size());