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"
33 static int anotherWMRunning(Display
*display
, XErrorEvent
*) {
34 printf(_("Another window manager already running on display %s.\n"),
35 DisplayString(display
));
44 OBScreen::OBScreen(int screen
)
48 assert(screen
>= 0); assert(screen
< ScreenCount(otk::OBDisplay::display
));
49 _info
= otk::OBDisplay::screenInfo(screen
);
52 XErrorHandler old
= XSetErrorHandler(::anotherWMRunning
);
53 XSelectInput(otk::OBDisplay::display
, _info
->rootWindow(),
54 OBScreen::event_mask
);
55 XSync(otk::OBDisplay::display
, false);
56 XSetErrorHandler(old
);
58 _managed
= !::running
;
59 if (! _managed
) return; // was unable to manage the screen
61 printf(_("Managing screen %d: visual 0x%lx, depth %d\n"),
62 _number
, XVisualIDFromVisual(_info
->visual()), _info
->depth());
64 Openbox::instance
->property()->set(_info
->rootWindow(),
65 otk::OBProperty::openbox_pid
,
66 otk::OBProperty::Atom_Cardinal
,
67 (unsigned long) getpid());
69 // set the mouse cursor for the root window (the default cursor)
70 XDefineCursor(otk::OBDisplay::display
, _info
->rootWindow(),
71 Openbox::instance
->cursors().session
);
73 // initialize the shit that is used for all drawing on the screen
74 _image_control
= new otk::BImageControl(Openbox::instance
->timerManager(),
76 _image_control
->installRootColormap();
77 _root_cmap_installed
= True
;
79 // initialize the screen's style
80 _style
.setImageControl(_image_control
);
81 std::string stylepath
;
82 python_get_string("theme", &stylepath
);
83 otk::Configuration
sconfig(false);
84 sconfig
.setFile(otk::expandTilde(stylepath
));
85 if (!sconfig
.load()) {
86 sconfig
.setFile(otk::expandTilde(DEFAULTSTYLE
));
87 if (!sconfig
.load()) {
88 printf(_("Unable to load default style: %s. Aborting.\n"), DEFAULTSTYLE
);
94 // set up notification of netwm support
97 // Set the netwm properties for geometry and viewport
98 unsigned long geometry
[] = { _info
->width(),
100 Openbox::instance
->property()->set(_info
->rootWindow(),
101 otk::OBProperty::net_desktop_geometry
,
102 otk::OBProperty::Atom_Cardinal
,
104 unsigned long viewport
[] = { 0, 0 };
105 Openbox::instance
->property()->set(_info
->rootWindow(),
106 otk::OBProperty::net_desktop_viewport
,
107 otk::OBProperty::Atom_Cardinal
,
110 // Set the net_desktop_names property
111 std::vector
<std::string
> names
;
112 python_get_stringlist("desktop_names", &names
);
113 _root
.setDesktopNames(names
);
115 // create the window which gets focus when no clients get it
116 XSetWindowAttributes attr
;
117 attr
.override_redirect
= true;
118 _focuswindow
= XCreateWindow(otk::OBDisplay::display
, _info
->rootWindow(),
119 -100, -100, 1, 1, 0, 0, InputOnly
,
120 _info
->visual(), CWOverrideRedirect
, &attr
);
121 XMapWindow(otk::OBDisplay::display
, _focuswindow
);
123 // these may be further updated if any pre-existing windows are found in
124 // the manageExising() function
125 setClientList(); // initialize the client lists, which will be empty
126 calcArea(); // initialize the available working area
130 OBScreen::~OBScreen()
132 if (! _managed
) return;
134 XSelectInput(otk::OBDisplay::display
, _info
->rootWindow(), NoEventMask
);
136 // unmanage all windows
137 while (!clients
.empty())
138 unmanageWindow(clients
.front());
140 XDestroyWindow(otk::OBDisplay::display
, _focuswindow
);
141 XDestroyWindow(otk::OBDisplay::display
, _supportwindow
);
143 delete _image_control
;
147 void OBScreen::manageExisting()
149 unsigned int i
, j
, nchild
;
150 Window r
, p
, *children
;
151 XQueryTree(otk::OBDisplay::display
, _info
->rootWindow(), &r
, &p
,
154 // preen the window list of all icon windows... for better dockapp support
155 for (i
= 0; i
< nchild
; i
++) {
156 if (children
[i
] == None
) continue;
158 XWMHints
*wmhints
= XGetWMHints(otk::OBDisplay::display
,
162 if ((wmhints
->flags
& IconWindowHint
) &&
163 (wmhints
->icon_window
!= children
[i
])) {
164 for (j
= 0; j
< nchild
; j
++) {
165 if (children
[j
] == wmhints
->icon_window
) {
176 // manage shown windows
177 for (i
= 0; i
< nchild
; ++i
) {
178 if (children
[i
] == None
)
181 XWindowAttributes attrib
;
182 if (XGetWindowAttributes(otk::OBDisplay::display
, children
[i
], &attrib
)) {
183 if (attrib
.override_redirect
) continue;
185 if (attrib
.map_state
!= IsUnmapped
) {
186 manageWindow(children
[i
]);
195 void OBScreen::updateStrut()
197 _strut
.left
= _strut
.right
= _strut
.top
= _strut
.bottom
= 0;
199 ClientList::iterator it
, end
= clients
.end();
200 for (it
= clients
.begin(); it
!= end
; ++it
) {
201 const otk::Strut
&s
= (*it
)->strut();
202 _strut
.left
= std::max(_strut
.left
, s
.left
);
203 _strut
.right
= std::max(_strut
.right
, s
.right
);
204 _strut
.top
= std::max(_strut
.top
, s
.top
);
205 _strut
.bottom
= std::max(_strut
.bottom
, s
.bottom
);
211 void OBScreen::calcArea()
213 otk::Rect old_area
= _area
;
217 // reset to the full areas
218 if (isXineramaActive())
219 xineramaUsableArea = getXineramaAreas();
223 _area
.setRect(_strut
.left
, _strut
.top
,
224 _info
->width() - (_strut
.left
+ _strut
.right
),
225 _info
->height() - (_strut
.top
+ _strut
.bottom
));
229 if (isXineramaActive()) {
230 // keep each of the ximerama-defined areas inside the strut
231 RectList::iterator xit, xend = xineramaUsableArea.end();
232 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
233 if (xit->x() < usableArea.x()) {
234 xit->setX(usableArea.x());
235 xit->setWidth(xit->width() - usableArea.x());
237 if (xit->y() < usableArea.y()) {
238 xit->setY(usableArea.y());
239 xit->setHeight(xit->height() - usableArea.y());
241 if (xit->x() + xit->width() > usableArea.width())
242 xit->setWidth(usableArea.width() - xit->x());
243 if (xit->y() + xit->height() > usableArea.height())
244 xit->setHeight(usableArea.height() - xit->y());
250 if (old_area
!= _area
)
251 // XXX: re-maximize windows
257 void OBScreen::setSupportedAtoms()
259 // create the netwm support window
260 _supportwindow
= XCreateSimpleWindow(otk::OBDisplay::display
,
262 0, 0, 1, 1, 0, 0, 0);
263 assert(_supportwindow
!= None
);
265 // set supporting window
266 Openbox::instance
->property()->set(_info
->rootWindow(),
267 otk::OBProperty::net_supporting_wm_check
,
268 otk::OBProperty::Atom_Window
,
271 //set properties on the supporting window
272 Openbox::instance
->property()->set(_supportwindow
,
273 otk::OBProperty::net_wm_name
,
274 otk::OBProperty::utf8
,
276 Openbox::instance
->property()->set(_supportwindow
,
277 otk::OBProperty::net_supporting_wm_check
,
278 otk::OBProperty::Atom_Window
,
284 otk::OBProperty::net_current_desktop,
285 otk::OBProperty::net_number_of_desktops,
287 otk::OBProperty::net_desktop_geometry
,
288 otk::OBProperty::net_desktop_viewport
,
289 otk::OBProperty::net_active_window
,
290 otk::OBProperty::net_workarea
,
291 otk::OBProperty::net_client_list
,
292 otk::OBProperty::net_client_list_stacking
,
293 otk::OBProperty::net_desktop_names
,
294 otk::OBProperty::net_close_window
,
295 otk::OBProperty::net_wm_name
,
296 otk::OBProperty::net_wm_visible_name
,
297 otk::OBProperty::net_wm_icon_name
,
298 otk::OBProperty::net_wm_visible_icon_name
,
300 otk::OBProperty::net_wm_desktop,
302 otk::OBProperty::net_wm_strut
,
303 otk::OBProperty::net_wm_window_type
,
304 otk::OBProperty::net_wm_window_type_desktop
,
305 otk::OBProperty::net_wm_window_type_dock
,
306 otk::OBProperty::net_wm_window_type_toolbar
,
307 otk::OBProperty::net_wm_window_type_menu
,
308 otk::OBProperty::net_wm_window_type_utility
,
309 otk::OBProperty::net_wm_window_type_splash
,
310 otk::OBProperty::net_wm_window_type_dialog
,
311 otk::OBProperty::net_wm_window_type_normal
,
313 otk::OBProperty::net_wm_moveresize,
314 otk::OBProperty::net_wm_moveresize_size_topleft,
315 otk::OBProperty::net_wm_moveresize_size_topright,
316 otk::OBProperty::net_wm_moveresize_size_bottomleft,
317 otk::OBProperty::net_wm_moveresize_size_bottomright,
318 otk::OBProperty::net_wm_moveresize_move,
321 otk::OBProperty::net_wm_allowed_actions,
322 otk::OBProperty::net_wm_action_move,
323 otk::OBProperty::net_wm_action_resize,
324 otk::OBProperty::net_wm_action_shade,
325 otk::OBProperty::net_wm_action_maximize_horz,
326 otk::OBProperty::net_wm_action_maximize_vert,
327 otk::OBProperty::net_wm_action_change_desktop,
328 otk::OBProperty::net_wm_action_close,
330 otk::OBProperty::net_wm_state
,
331 otk::OBProperty::net_wm_state_modal
,
332 otk::OBProperty::net_wm_state_maximized_vert
,
333 otk::OBProperty::net_wm_state_maximized_horz
,
334 otk::OBProperty::net_wm_state_shaded
,
335 otk::OBProperty::net_wm_state_skip_taskbar
,
336 otk::OBProperty::net_wm_state_skip_pager
,
337 otk::OBProperty::net_wm_state_hidden
,
338 otk::OBProperty::net_wm_state_fullscreen
,
339 otk::OBProperty::net_wm_state_above
,
340 otk::OBProperty::net_wm_state_below
,
342 const int num_supported
= sizeof(supported
)/sizeof(Atom
);
344 // convert to the atom values
345 for (int i
= 0; i
< num_supported
; ++i
)
347 Openbox::instance
->property()->atom((otk::OBProperty::Atoms
)supported
[i
]);
349 Openbox::instance
->property()->set(_info
->rootWindow(),
350 otk::OBProperty::net_supported
,
351 otk::OBProperty::Atom_Atom
,
352 supported
, num_supported
);
356 void OBScreen::setClientList()
359 unsigned int size
= clients
.size();
361 // create an array of the window ids
365 windows
= new Window
[size
];
367 ClientList::const_iterator it
= clients
.begin();
368 const ClientList::const_iterator end
= clients
.end();
369 for (; it
!= end
; ++it
, ++win_it
)
370 *win_it
= (*it
)->window();
372 windows
= (Window
*) 0;
374 Openbox::instance
->property()->set(_info
->rootWindow(),
375 otk::OBProperty::net_client_list
,
376 otk::OBProperty::Atom_Window
,
386 void OBScreen::setStackingList()
389 unsigned int size
= _stacking
.size();
391 assert(size
== clients
.size()); // just making sure.. :)
394 // create an array of the window ids
398 windows
= new Window
[size
];
400 ClientList::const_iterator it
= _stacking
.begin();
401 const ClientList::const_iterator end
= _stacking
.end();
402 for (; it
!= end
; ++it
, ++win_it
)
403 *win_it
= (*it
)->window();
405 windows
= (Window
*) 0;
407 Openbox::instance
->property()->set(_info
->rootWindow(),
408 otk::OBProperty::net_client_list_stacking
,
409 otk::OBProperty::Atom_Window
,
417 void OBScreen::setWorkArea() {
418 unsigned long area
[] = { _area
.x(), _area
.y(),
419 _area
.width(), _area
.height() };
420 Openbox::instance
->property()->set(_info
->rootWindow(),
421 otk::OBProperty::net_workarea
,
422 otk::OBProperty::Atom_Cardinal
,
425 if (workspacesList.size() > 0) {
426 unsigned long *dims = new unsigned long[4 * workspacesList.size()];
427 for (unsigned int i = 0, m = workspacesList.size(); i < m; ++i) {
428 // XXX: this could be different for each workspace
429 const otk::Rect &area = availableArea();
430 dims[(i * 4) + 0] = area.x();
431 dims[(i * 4) + 1] = area.y();
432 dims[(i * 4) + 2] = area.width();
433 dims[(i * 4) + 3] = area.height();
435 xatom->set(getRootWindow(), otk::OBProperty::net_workarea,
436 otk::OBProperty::Atom_Cardinal,
437 dims, 4 * workspacesList.size());
440 xatom->set(getRootWindow(), otk::OBProperty::net_workarea,
441 otk::OBProperty::Atom_Cardinal, 0, 0);
446 void OBScreen::manageWindow(Window window
)
448 OBClient
*client
= 0;
450 XSetWindowAttributes attrib_set
;
452 // is the window a docking app
453 if ((wmhint
= XGetWMHints(otk::OBDisplay::display
, window
))) {
454 if ((wmhint
->flags
& StateHint
) &&
455 wmhint
->initial_state
== WithdrawnState
) {
456 //slit->addClient(w); // XXX: make dock apps work!
463 otk::OBDisplay::grab();
465 // choose the events we want to receive on the CLIENT window
466 attrib_set
.event_mask
= OBClient::event_mask
;
467 attrib_set
.do_not_propagate_mask
= OBClient::no_propagate_mask
;
468 XChangeWindowAttributes(otk::OBDisplay::display
, window
,
469 CWEventMask
|CWDontPropagate
, &attrib_set
);
471 // create the OBClient class, which gets all of the hints on the window
472 client
= new OBClient(_number
, window
);
473 // register for events
474 Openbox::instance
->registerHandler(window
, client
);
475 // add to the wm's map
476 Openbox::instance
->addClient(window
, client
);
478 // we dont want a border on the client
479 client
->toggleClientBorder(false);
481 // specify that if we exit, the window should not be destroyed and should be
482 // reparented back to root automatically
483 XChangeSaveSet(otk::OBDisplay::display
, window
, SetModeInsert
);
485 if (!client
->positionRequested()) {
486 // XXX: position the window intelligenty
489 // create the decoration frame for the client window
490 client
->frame
= new OBFrame(client
, &_style
);
492 // add to the wm's map
493 Openbox::instance
->addClient(client
->frame
->window(), client
);
494 Openbox::instance
->addClient(client
->frame
->plate(), client
);
495 Openbox::instance
->addClient(client
->frame
->titlebar(), client
);
496 Openbox::instance
->addClient(client
->frame
->label(), client
);
497 Openbox::instance
->addClient(client
->frame
->button_max(), client
);
498 Openbox::instance
->addClient(client
->frame
->button_iconify(), client
);
499 Openbox::instance
->addClient(client
->frame
->button_stick(), client
);
500 Openbox::instance
->addClient(client
->frame
->button_close(), client
);
501 Openbox::instance
->addClient(client
->frame
->handle(), client
);
502 Openbox::instance
->addClient(client
->frame
->grip_left(), client
);
503 Openbox::instance
->addClient(client
->frame
->grip_right(), client
);
505 // XXX: if on the current desktop..
506 client
->frame
->show();
508 // XXX: handle any requested states such as maximized
510 otk::OBDisplay::ungrab();
512 // add to the screen's list
513 clients
.push_back(client
);
514 // this puts into the stacking order, then raises it
515 _stacking
.push_back(client
);
516 restack(true, client
);
517 // update the root properties
520 Openbox::instance
->bindings()->grabButtons(true, client
);
524 void OBScreen::unmanageWindow(OBClient
*client
)
526 OBFrame
*frame
= client
->frame
;
528 Openbox::instance
->bindings()->grabButtons(false, client
);
530 // remove from the stacking order
531 _stacking
.remove(client
);
533 // pass around focus if this window was focused XXX do this better!
534 if (Openbox::instance
->focusedClient() == client
) {
535 OBClient
*newfocus
= 0;
536 if (!_stacking
.empty())
537 newfocus
= _stacking
.front();
538 if (! (newfocus
&& newfocus
->focus()))
542 // remove from the wm's map
543 Openbox::instance
->removeClient(client
->window());
544 Openbox::instance
->removeClient(frame
->window());
545 Openbox::instance
->removeClient(frame
->plate());
546 Openbox::instance
->removeClient(frame
->titlebar());
547 Openbox::instance
->removeClient(frame
->label());
548 Openbox::instance
->removeClient(frame
->button_max());
549 Openbox::instance
->removeClient(frame
->button_iconify());
550 Openbox::instance
->removeClient(frame
->button_stick());
551 Openbox::instance
->removeClient(frame
->button_close());
552 Openbox::instance
->removeClient(frame
->handle());
553 Openbox::instance
->removeClient(frame
->grip_left());
554 Openbox::instance
->removeClient(frame
->grip_right());
555 // unregister for handling events
556 Openbox::instance
->clearHandler(client
->window());
558 // remove the window from our save set
559 XChangeSaveSet(otk::OBDisplay::display
, client
->window(), SetModeDelete
);
561 // we dont want events no more
562 XSelectInput(otk::OBDisplay::display
, client
->window(), NoEventMask
);
566 // give the client its border back
567 client
->toggleClientBorder(true);
569 delete client
->frame
;
572 // remove from the screen's list
573 clients
.remove(client
);
576 // update the root properties
580 void OBScreen::restack(bool raise
, OBClient
*client
)
582 const int layer
= client
->layer();
583 std::vector
<Window
> wins
;
585 _stacking
.remove(client
);
587 // the stacking list is from highest to lowest
589 ClientList::iterator it
= _stacking
.begin(), end
= _stacking
.end();
590 // insert the windows above this window
591 for (; it
!= end
; ++it
) {
592 if ((*it
)->layer() < layer
|| (raise
&& (*it
)->layer() == layer
))
594 wins
.push_back((*it
)->frame
->window());
597 wins
.push_back(client
->frame
->window());
598 _stacking
.insert(it
, client
);
599 // insert the remaining below this window
600 for (; it
!= end
; ++it
)
601 wins
.push_back((*it
)->frame
->window());
603 XRestackWindows(otk::OBDisplay::display
, &wins
[0], wins
.size());