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 properties for geometry 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 // Set the net_desktop_names property
110 std::vector
<std::string
> names
;
111 python_get_stringlist("desktop_names", &names
);
112 _root
.setDesktopNames(names
);
114 // create the window which gets focus when no clients get it
115 XSetWindowAttributes attr
;
116 attr
.override_redirect
= true;
117 _focuswindow
= XCreateWindow(otk::OBDisplay::display
, _info
->rootWindow(),
118 -100, -100, 1, 1, 0, 0, InputOnly
,
119 _info
->visual(), CWOverrideRedirect
, &attr
);
120 XMapWindow(otk::OBDisplay::display
, _focuswindow
);
122 // these may be further updated if any pre-existing windows are found in
123 // the manageExising() function
124 setClientList(); // initialize the client lists, which will be empty
125 calcArea(); // initialize the available working area
129 OBScreen::~OBScreen()
131 if (! _managed
) return;
133 XSelectInput(otk::OBDisplay::display
, _info
->rootWindow(), NoEventMask
);
135 // unmanage all windows
136 while (!clients
.empty())
137 unmanageWindow(clients
.front());
139 XDestroyWindow(otk::OBDisplay::display
, _focuswindow
);
140 XDestroyWindow(otk::OBDisplay::display
, _supportwindow
);
142 delete _image_control
;
146 void OBScreen::manageExisting()
148 unsigned int i
, j
, nchild
;
149 Window r
, p
, *children
;
150 XQueryTree(otk::OBDisplay::display
, _info
->rootWindow(), &r
, &p
,
153 // preen the window list of all icon windows... for better dockapp support
154 for (i
= 0; i
< nchild
; i
++) {
155 if (children
[i
] == None
) continue;
157 XWMHints
*wmhints
= XGetWMHints(otk::OBDisplay::display
,
161 if ((wmhints
->flags
& IconWindowHint
) &&
162 (wmhints
->icon_window
!= children
[i
])) {
163 for (j
= 0; j
< nchild
; j
++) {
164 if (children
[j
] == wmhints
->icon_window
) {
175 // manage shown windows
176 for (i
= 0; i
< nchild
; ++i
) {
177 if (children
[i
] == None
)
180 XWindowAttributes attrib
;
181 if (XGetWindowAttributes(otk::OBDisplay::display
, children
[i
], &attrib
)) {
182 if (attrib
.override_redirect
) continue;
184 if (attrib
.map_state
!= IsUnmapped
) {
185 manageWindow(children
[i
]);
194 //! Adds a window's strut to the screen's list of reserved spaces
195 void OBScreen::addStrut(otk::Strut
*strut
)
197 _struts
.push_back(strut
);
201 //! Removes a window's strut from the screen's list of reserved spaces
202 void OBScreen::removeStrut(otk::Strut
*strut
)
204 _struts
.remove(strut
);
208 void OBScreen::calcArea()
210 otk::Rect old_area
= _area
;
214 // reset to the full areas
215 if (isXineramaActive())
216 xineramaUsableArea = getXineramaAreas();
220 /* these values represent offsets from the screen edge
221 * we look for the biggest offset on each edge and then apply them
223 * do not be confused by the similarity to the names of Rect's members
225 unsigned int current_left
= 0, current_right
= 0, current_top
= 0,
228 StrutList::const_iterator it
= _struts
.begin(), end
= _struts
.end();
230 for(; it
!= end
; ++it
) {
231 otk::Strut
*strut
= *it
;
232 if (strut
->left
> current_left
)
233 current_left
= strut
->left
;
234 if (strut
->top
> current_top
)
235 current_top
= strut
->top
;
236 if (strut
->right
> current_right
)
237 current_right
= strut
->right
;
238 if (strut
->bottom
> current_bottom
)
239 current_bottom
= strut
->bottom
;
242 _area
.setRect(current_left
, current_top
,
243 _info
->width() - (current_left
+ current_right
),
244 _info
->height() - (current_top
+ current_bottom
));
248 if (isXineramaActive()) {
249 // keep each of the ximerama-defined areas inside the strut
250 RectList::iterator xit, xend = xineramaUsableArea.end();
251 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
252 if (xit->x() < usableArea.x()) {
253 xit->setX(usableArea.x());
254 xit->setWidth(xit->width() - usableArea.x());
256 if (xit->y() < usableArea.y()) {
257 xit->setY(usableArea.y());
258 xit->setHeight(xit->height() - usableArea.y());
260 if (xit->x() + xit->width() > usableArea.width())
261 xit->setWidth(usableArea.width() - xit->x());
262 if (xit->y() + xit->height() > usableArea.height())
263 xit->setHeight(usableArea.height() - xit->y());
269 if (old_area
!= _area
)
270 // XXX: re-maximize windows
276 void OBScreen::setSupportedAtoms()
278 // create the netwm support window
279 _supportwindow
= XCreateSimpleWindow(otk::OBDisplay::display
,
281 0, 0, 1, 1, 0, 0, 0);
282 assert(_supportwindow
!= None
);
284 // set supporting window
285 Openbox::instance
->property()->set(_info
->rootWindow(),
286 otk::OBProperty::net_supporting_wm_check
,
287 otk::OBProperty::Atom_Window
,
290 //set properties on the supporting window
291 Openbox::instance
->property()->set(_supportwindow
,
292 otk::OBProperty::net_wm_name
,
293 otk::OBProperty::utf8
,
295 Openbox::instance
->property()->set(_supportwindow
,
296 otk::OBProperty::net_supporting_wm_check
,
297 otk::OBProperty::Atom_Window
,
303 otk::OBProperty::net_current_desktop,
304 otk::OBProperty::net_number_of_desktops,
306 otk::OBProperty::net_desktop_geometry
,
307 otk::OBProperty::net_desktop_viewport
,
308 otk::OBProperty::net_active_window
,
309 otk::OBProperty::net_workarea
,
310 otk::OBProperty::net_client_list
,
311 otk::OBProperty::net_client_list_stacking
,
312 otk::OBProperty::net_desktop_names
,
313 otk::OBProperty::net_close_window
,
314 otk::OBProperty::net_wm_name
,
315 otk::OBProperty::net_wm_visible_name
,
316 otk::OBProperty::net_wm_icon_name
,
317 otk::OBProperty::net_wm_visible_icon_name
,
319 otk::OBProperty::net_wm_desktop,
322 otk::OBProperty::net_wm_strut,
324 otk::OBProperty::net_wm_window_type
,
325 otk::OBProperty::net_wm_window_type_desktop
,
326 otk::OBProperty::net_wm_window_type_dock
,
327 otk::OBProperty::net_wm_window_type_toolbar
,
328 otk::OBProperty::net_wm_window_type_menu
,
329 otk::OBProperty::net_wm_window_type_utility
,
330 otk::OBProperty::net_wm_window_type_splash
,
331 otk::OBProperty::net_wm_window_type_dialog
,
332 otk::OBProperty::net_wm_window_type_normal
,
334 otk::OBProperty::net_wm_moveresize,
335 otk::OBProperty::net_wm_moveresize_size_topleft,
336 otk::OBProperty::net_wm_moveresize_size_topright,
337 otk::OBProperty::net_wm_moveresize_size_bottomleft,
338 otk::OBProperty::net_wm_moveresize_size_bottomright,
339 otk::OBProperty::net_wm_moveresize_move,
342 otk::OBProperty::net_wm_allowed_actions,
343 otk::OBProperty::net_wm_action_move,
344 otk::OBProperty::net_wm_action_resize,
345 otk::OBProperty::net_wm_action_shade,
346 otk::OBProperty::net_wm_action_maximize_horz,
347 otk::OBProperty::net_wm_action_maximize_vert,
348 otk::OBProperty::net_wm_action_change_desktop,
349 otk::OBProperty::net_wm_action_close,
351 otk::OBProperty::net_wm_state
,
352 otk::OBProperty::net_wm_state_modal
,
353 otk::OBProperty::net_wm_state_maximized_vert
,
354 otk::OBProperty::net_wm_state_maximized_horz
,
355 otk::OBProperty::net_wm_state_shaded
,
356 otk::OBProperty::net_wm_state_skip_taskbar
,
357 otk::OBProperty::net_wm_state_skip_pager
,
358 otk::OBProperty::net_wm_state_hidden
,
359 otk::OBProperty::net_wm_state_fullscreen
,
360 otk::OBProperty::net_wm_state_above
,
361 otk::OBProperty::net_wm_state_below
,
363 const int num_supported
= sizeof(supported
)/sizeof(Atom
);
365 // convert to the atom values
366 for (int i
= 0; i
< num_supported
; ++i
)
368 Openbox::instance
->property()->atom((otk::OBProperty::Atoms
)supported
[i
]);
370 Openbox::instance
->property()->set(_info
->rootWindow(),
371 otk::OBProperty::net_supported
,
372 otk::OBProperty::Atom_Atom
,
373 supported
, num_supported
);
377 void OBScreen::setClientList()
380 unsigned int size
= clients
.size();
382 // create an array of the window ids
386 windows
= new Window
[size
];
388 ClientList::const_iterator it
= clients
.begin();
389 const ClientList::const_iterator end
= clients
.end();
390 for (; it
!= end
; ++it
, ++win_it
)
391 *win_it
= (*it
)->window();
393 windows
= (Window
*) 0;
395 Openbox::instance
->property()->set(_info
->rootWindow(),
396 otk::OBProperty::net_client_list
,
397 otk::OBProperty::Atom_Window
,
407 void OBScreen::setStackingList()
410 unsigned int size
= _stacking
.size();
412 assert(size
== clients
.size()); // just making sure.. :)
415 // create an array of the window ids
419 windows
= new Window
[size
];
421 ClientList::const_iterator it
= _stacking
.begin();
422 const ClientList::const_iterator end
= _stacking
.end();
423 for (; it
!= end
; ++it
, ++win_it
)
424 *win_it
= (*it
)->window();
426 windows
= (Window
*) 0;
428 Openbox::instance
->property()->set(_info
->rootWindow(),
429 otk::OBProperty::net_client_list_stacking
,
430 otk::OBProperty::Atom_Window
,
438 void OBScreen::setWorkArea() {
439 unsigned long area
[] = { _area
.x(), _area
.y(),
440 _area
.width(), _area
.height() };
441 Openbox::instance
->property()->set(_info
->rootWindow(),
442 otk::OBProperty::net_workarea
,
443 otk::OBProperty::Atom_Cardinal
,
446 if (workspacesList.size() > 0) {
447 unsigned long *dims = new unsigned long[4 * workspacesList.size()];
448 for (unsigned int i = 0, m = workspacesList.size(); i < m; ++i) {
449 // XXX: this could be different for each workspace
450 const otk::Rect &area = availableArea();
451 dims[(i * 4) + 0] = area.x();
452 dims[(i * 4) + 1] = area.y();
453 dims[(i * 4) + 2] = area.width();
454 dims[(i * 4) + 3] = area.height();
456 xatom->set(getRootWindow(), otk::OBProperty::net_workarea,
457 otk::OBProperty::Atom_Cardinal,
458 dims, 4 * workspacesList.size());
461 xatom->set(getRootWindow(), otk::OBProperty::net_workarea,
462 otk::OBProperty::Atom_Cardinal, 0, 0);
467 void OBScreen::manageWindow(Window window
)
469 OBClient
*client
= 0;
471 XSetWindowAttributes attrib_set
;
473 // is the window a docking app
474 if ((wmhint
= XGetWMHints(otk::OBDisplay::display
, window
))) {
475 if ((wmhint
->flags
& StateHint
) &&
476 wmhint
->initial_state
== WithdrawnState
) {
477 //slit->addClient(w); // XXX: make dock apps work!
484 otk::OBDisplay::grab();
486 // choose the events we want to receive on the CLIENT window
487 attrib_set
.event_mask
= OBClient::event_mask
;
488 attrib_set
.do_not_propagate_mask
= OBClient::no_propagate_mask
;
489 XChangeWindowAttributes(otk::OBDisplay::display
, window
,
490 CWEventMask
|CWDontPropagate
, &attrib_set
);
492 // create the OBClient class, which gets all of the hints on the window
493 client
= new OBClient(_number
, window
);
494 // register for events
495 Openbox::instance
->registerHandler(window
, client
);
496 // add to the wm's map
497 Openbox::instance
->addClient(window
, client
);
499 // we dont want a border on the client
500 client
->toggleClientBorder(false);
502 // specify that if we exit, the window should not be destroyed and should be
503 // reparented back to root automatically
504 XChangeSaveSet(otk::OBDisplay::display
, window
, SetModeInsert
);
506 if (!client
->positionRequested()) {
507 // XXX: position the window intelligenty
510 // create the decoration frame for the client window
511 client
->frame
= new OBFrame(client
, &_style
);
513 // add to the wm's map
514 Openbox::instance
->addClient(client
->frame
->window(), client
);
515 Openbox::instance
->addClient(client
->frame
->plate(), client
);
516 Openbox::instance
->addClient(client
->frame
->titlebar(), client
);
517 Openbox::instance
->addClient(client
->frame
->label(), client
);
518 Openbox::instance
->addClient(client
->frame
->button_max(), client
);
519 Openbox::instance
->addClient(client
->frame
->button_iconify(), client
);
520 Openbox::instance
->addClient(client
->frame
->button_stick(), client
);
521 Openbox::instance
->addClient(client
->frame
->button_close(), client
);
522 Openbox::instance
->addClient(client
->frame
->handle(), client
);
523 Openbox::instance
->addClient(client
->frame
->grip_left(), client
);
524 Openbox::instance
->addClient(client
->frame
->grip_right(), client
);
526 // XXX: if on the current desktop..
527 client
->frame
->show();
529 // XXX: handle any requested states such as maximized
531 otk::OBDisplay::ungrab();
533 // add to the screen's list
534 clients
.push_back(client
);
535 // this puts into the stacking order, then raises it
536 _stacking
.push_back(client
);
537 restack(true, client
);
538 // update the root properties
541 Openbox::instance
->bindings()->grabButtons(true, client
);
545 void OBScreen::unmanageWindow(OBClient
*client
)
547 OBFrame
*frame
= client
->frame
;
549 Openbox::instance
->bindings()->grabButtons(false, client
);
551 // remove from the stacking order
552 _stacking
.remove(client
);
554 // pass around focus if this window was focused XXX do this better!
555 if (Openbox::instance
->focusedClient() == client
) {
556 OBClient
*newfocus
= 0;
557 if (!_stacking
.empty())
558 newfocus
= _stacking
.front();
559 if (! (newfocus
&& newfocus
->focus()))
563 // remove from the wm's map
564 Openbox::instance
->removeClient(client
->window());
565 Openbox::instance
->removeClient(frame
->window());
566 Openbox::instance
->removeClient(frame
->plate());
567 Openbox::instance
->removeClient(frame
->titlebar());
568 Openbox::instance
->removeClient(frame
->label());
569 Openbox::instance
->removeClient(frame
->button_max());
570 Openbox::instance
->removeClient(frame
->button_iconify());
571 Openbox::instance
->removeClient(frame
->button_stick());
572 Openbox::instance
->removeClient(frame
->button_close());
573 Openbox::instance
->removeClient(frame
->handle());
574 Openbox::instance
->removeClient(frame
->grip_left());
575 Openbox::instance
->removeClient(frame
->grip_right());
576 // unregister for handling events
577 Openbox::instance
->clearHandler(client
->window());
579 // remove the window from our save set
580 XChangeSaveSet(otk::OBDisplay::display
, client
->window(), SetModeDelete
);
582 // we dont want events no more
583 XSelectInput(otk::OBDisplay::display
, client
->window(), NoEventMask
);
587 // give the client its border back
588 client
->toggleClientBorder(true);
590 delete client
->frame
;
593 // remove from the screen's list
594 clients
.remove(client
);
597 // update the root properties
601 void OBScreen::restack(bool raise
, OBClient
*client
)
603 const int layer
= client
->layer();
604 std::vector
<Window
> wins
;
606 _stacking
.remove(client
);
608 // the stacking list is from highest to lowest
610 ClientList::iterator it
= _stacking
.begin(), end
= _stacking
.end();
611 // insert the windows above this window
612 for (; it
!= end
; ++it
) {
613 if ((*it
)->layer() < layer
|| (raise
&& (*it
)->layer() == layer
))
615 wins
.push_back((*it
)->frame
->window());
618 wins
.push_back(client
->frame
->window());
619 _stacking
.insert(it
, client
);
620 // insert the remaining below this window
621 for (; it
!= end
; ++it
)
622 wins
.push_back((*it
)->frame
->window());
624 XRestackWindows(otk::OBDisplay::display
, &wins
[0], wins
.size());