1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
7 # include <sys/types.h>
9 #endif // HAVE_UNISTD_H
12 #define _(str) gettext(str)
19 #include "bindings.hh"
21 #include "otk/display.hh"
22 #include "otk/property.hh"
30 static int anotherWMRunning(Display
*display
, XErrorEvent
*) {
31 printf(_("Another window manager already running on display %s.\n"),
32 DisplayString(display
));
41 Screen::Screen(int screen
)
44 assert(screen
>= 0); assert(screen
< ScreenCount(**otk::display
));
45 _info
= otk::display
->screenInfo(screen
);
48 XErrorHandler old
= XSetErrorHandler(::anotherWMRunning
);
49 XSelectInput(**otk::display
, _info
->rootWindow(),
51 XSync(**otk::display
, false);
52 XSetErrorHandler(old
);
54 _managed
= !::running
;
55 if (! _managed
) return; // was unable to manage the screen
58 printf(_("Managing screen %d: visual 0x%lx, depth %d\n"),
59 _number
, XVisualIDFromVisual(_info
->visual()), _info
->depth());
62 otk::Property::set(_info
->rootWindow(), otk::Property::atoms
.openbox_pid
,
63 otk::Property::atoms
.cardinal
, (unsigned long) getpid());
65 // set the mouse cursor for the root window (the default cursor)
66 XDefineCursor(**otk::display
, _info
->rootWindow(),
67 openbox
->cursors().session
);
69 // XXX: initialize the screen's style
71 otk::ustring stylepath;
72 python_get_string("THEME", &stylepath);
73 otk::Configuration sconfig(false);
74 sconfig.setFile(otk::expandTilde(stylepath.c_str()));
75 if (!sconfig.load()) {
76 sconfig.setFile(otk::expandTilde(DEFAULTSTYLE));
77 if (!sconfig.load()) {
78 printf(_("Unable to load default style: %s. Aborting.\n"), DEFAULTSTYLE);
84 otk::display
->renderControl(_number
)->
85 drawRoot(*otk::RenderStyle::style(_number
)->rootColor());
87 // set up notification of netwm support
88 changeSupportedAtoms();
90 // Set the netwm properties for geometry
91 unsigned long geometry
[] = { _info
->size().width(),
92 _info
->size().height() };
93 otk::Property::set(_info
->rootWindow(),
94 otk::Property::atoms
.net_desktop_geometry
,
95 otk::Property::atoms
.cardinal
, geometry
, 2);
97 // Set the net_desktop_names property
98 std::vector
<otk::ustring
> names
;
99 python_get_stringlist("DESKTOP_NAMES", &names
);
100 otk::Property::set(_info
->rootWindow(),
101 otk::Property::atoms
.net_desktop_names
,
102 otk::Property::utf8
, names
);
103 // the above set() will cause the updateDesktopNames to fire right away so
104 // we have a list of desktop names
108 if (!python_get_long("NUMBER_OF_DESKTOPS", (long*)&_num_desktops
))
110 changeNumDesktops(_num_desktops
); // set the hint
112 changeDesktop(0); // set the hint
114 // don't start in showing-desktop mode
115 _showing_desktop
= false;
116 otk::Property::set(_info
->rootWindow(),
117 otk::Property::atoms
.net_showing_desktop
,
118 otk::Property::atoms
.cardinal
, 0);
120 // create the window which gets focus when no clients get it
121 XSetWindowAttributes attr
;
122 attr
.override_redirect
= true;
123 _focuswindow
= XCreateWindow(**otk::display
, _info
->rootWindow(),
124 -100, -100, 1, 1, 0, 0, InputOnly
,
125 _info
->visual(), CWOverrideRedirect
, &attr
);
126 XMapRaised(**otk::display
, _focuswindow
);
128 // these may be further updated if any pre-existing windows are found in
129 // the manageExising() function
130 changeClientList(); // initialize the client lists, which will be empty
132 updateDesktopLayout();
134 // register this class as the event handler for the root window
135 openbox
->registerHandler(_info
->rootWindow(), this);
137 // call the python Startup callbacks
138 EventData
data(_number
, 0, EventAction::Startup
, 0);
139 openbox
->bindings()->fireEvent(&data
);
145 if (! _managed
) return;
147 XSelectInput(**otk::display
, _info
->rootWindow(), NoEventMask
);
149 // unmanage all windows
150 while (!clients
.empty())
151 unmanageWindow(clients
.front());
153 // call the python Shutdown callbacks
154 EventData
data(_number
, 0, EventAction::Shutdown
, 0);
155 openbox
->bindings()->fireEvent(&data
);
157 XDestroyWindow(**otk::display
, _focuswindow
);
158 XDestroyWindow(**otk::display
, _supportwindow
);
162 void Screen::manageExisting()
164 unsigned int i
, j
, nchild
;
165 Window r
, p
, *children
;
166 XQueryTree(**otk::display
, _info
->rootWindow(), &r
, &p
,
169 // preen the window list of all icon windows... for better dockapp support
170 for (i
= 0; i
< nchild
; i
++) {
171 if (children
[i
] == None
) continue;
173 XWMHints
*wmhints
= XGetWMHints(**otk::display
,
177 if ((wmhints
->flags
& IconWindowHint
) &&
178 (wmhints
->icon_window
!= children
[i
])) {
179 for (j
= 0; j
< nchild
; j
++) {
180 if (children
[j
] == wmhints
->icon_window
) {
191 // manage shown windows
192 for (i
= 0; i
< nchild
; ++i
) {
193 if (children
[i
] == None
)
196 XWindowAttributes attrib
;
197 if (XGetWindowAttributes(**otk::display
, children
[i
], &attrib
)) {
198 if (attrib
.override_redirect
) continue;
200 if (attrib
.map_state
!= IsUnmapped
) {
201 manageWindow(children
[i
]);
209 void Screen::updateDesktopLayout()
211 //const unsigned long _NET_WM_ORIENTATION_HORZ = 0;
212 const unsigned long _NET_WM_ORIENTATION_VERT
= 1;
213 //const unsigned long _NET_WM_TOPLEFT = 0;
214 const unsigned long _NET_WM_TOPRIGHT
= 1;
215 const unsigned long _NET_WM_BOTTOMRIGHT
= 2;
216 const unsigned long _NET_WM_BOTTOMLEFT
= 3;
219 _layout
.orientation
= DesktopLayout::Horizontal
;
220 _layout
.start_corner
= DesktopLayout::TopLeft
;
222 _layout
.columns
= _num_desktops
;
224 unsigned long *data
, num
= 4;
225 if (otk::Property::get(_info
->rootWindow(),
226 otk::Property::atoms
.net_desktop_layout
,
227 otk::Property::atoms
.cardinal
,
230 if (data
[0] == _NET_WM_ORIENTATION_VERT
)
231 _layout
.orientation
= DesktopLayout::Vertical
;
232 if (data
[3] == _NET_WM_TOPRIGHT
)
233 _layout
.start_corner
= DesktopLayout::TopRight
;
234 else if (data
[3] == _NET_WM_BOTTOMRIGHT
)
235 _layout
.start_corner
= DesktopLayout::BottomRight
;
236 else if (data
[3] == _NET_WM_BOTTOMLEFT
)
237 _layout
.start_corner
= DesktopLayout::BottomLeft
;
239 // fill in a zero rows/columns
240 if (!(data
[1] == 0 && data
[2] == 0)) { // both 0's is bad data..
242 data
[1] = (_num_desktops
+ _num_desktops
% data
[2]) / data
[2];
243 } else if (data
[2] == 0) {
244 data
[2] = (_num_desktops
+ _num_desktops
% data
[1]) / data
[1];
246 _layout
.columns
= data
[1];
247 _layout
.rows
= data
[2];
251 if (_layout
.orientation
== DesktopLayout::Horizontal
) {
252 if (_layout
.rows
> _num_desktops
) _layout
.rows
= _num_desktops
;
253 if (_layout
.columns
> (_num_desktops
+ _num_desktops
% _layout
.rows
) /
255 _layout
.columns
= (_num_desktops
+ _num_desktops
% _layout
.rows
) /
258 if (_layout
.columns
> _num_desktops
) _layout
.columns
= _num_desktops
;
259 if (_layout
.rows
> (_num_desktops
+ _num_desktops
% _layout
.columns
) /
261 _layout
.rows
= (_num_desktops
+ _num_desktops
% _layout
.columns
) /
269 void Screen::updateStruts()
272 void operator()(otk::Strut
&self
, const otk::Strut
&other
) {
273 self
.left
= std::max(self
.left
, other
.left
);
274 self
.right
= std::max(self
.right
, other
.right
);
275 self
.top
= std::max(self
.top
, other
.top
);
276 self
.bottom
= std::max(self
.bottom
, other
.bottom
);
280 StrutList::iterator sit
, send
= _struts
.end();
282 for (sit
= _struts
.begin(); sit
!= send
; ++sit
)
283 sit
->left
= sit
->right
= sit
->top
= sit
->bottom
= 0;
285 ClientList::const_iterator it
, end
= clients
.end();
286 for (it
= clients
.begin(); it
!= end
; ++it
) {
287 if ((*it
)->iconic()) continue; // these dont count in the strut
289 unsigned int desk
= (*it
)->desktop();
290 const otk::Strut
&s
= (*it
)->strut();
292 if (desk
== 0xffffffff)
293 for (unsigned int i
= 0, e
= _struts
.size(); i
< e
; ++i
)
294 apply(_struts
[i
], s
);
295 else if (desk
< _struts
.size())
296 apply(_struts
[desk
], s
);
298 assert(false); // invalid desktop otherwise..
299 // apply to the 'all desktops' strut
300 apply(_struts
.back(), s
);
306 void Screen::changeWorkArea()
308 unsigned long *dims
= new unsigned long[4 * _num_desktops
];
309 for (unsigned int i
= 0; i
< _num_desktops
+ 1; ++i
) {
310 otk::Rect old_area
= _area
[i
];
313 // reset to the full areas
314 if (isXineramaActive())
315 xineramaUsableArea = getXineramaAreas();
319 _area
[i
] = otk::Rect(_struts
[i
].left
, _struts
[i
].top
,
320 _info
->size().width() - (_struts
[i
].left
+
322 _info
->size().height() - (_struts
[i
].top
+
327 if (isXineramaActive()) {
328 // keep each of the ximerama-defined areas inside the strut
329 RectList::iterator xit, xend = xineramaUsableArea.end();
330 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
331 if (xit->x() < usableArea.x()) {
332 xit->setX(usableArea.x());
333 xit->setWidth(xit->width() - usableArea.x());
335 if (xit->y() < usableArea.y()) {
336 xit->setY(usableArea.y());
337 xit->setHeight(xit->height() - usableArea.y());
339 if (xit->x() + xit->width() > usableArea.width())
340 xit->setWidth(usableArea.width() - xit->x());
341 if (xit->y() + xit->height() > usableArea.height())
342 xit->setHeight(usableArea.height() - xit->y());
347 if (old_area
!= _area
[i
]) {
348 // the area has changed, adjust all the maximized windows
349 ClientList::iterator it
, end
= clients
.end();
350 for (it
= clients
.begin(); it
!= end
; ++it
)
351 if (i
< _num_desktops
) {
352 if ((*it
)->desktop() == i
)
355 // the 'all desktops' size
356 if ((*it
)->desktop() == 0xffffffff)
361 // don't set these for the 'all desktops' area
362 if (i
< _num_desktops
) {
363 dims
[(i
* 4) + 0] = _area
[i
].x();
364 dims
[(i
* 4) + 1] = _area
[i
].y();
365 dims
[(i
* 4) + 2] = _area
[i
].width();
366 dims
[(i
* 4) + 3] = _area
[i
].height();
369 otk::Property::set(_info
->rootWindow(), otk::Property::atoms
.net_workarea
,
370 otk::Property::atoms
.cardinal
, dims
, 4 * _num_desktops
);
375 void Screen::changeSupportedAtoms()
377 // create the netwm support window
378 _supportwindow
= XCreateSimpleWindow(**otk::display
,
380 0, 0, 1, 1, 0, 0, 0);
382 // set supporting window
383 otk::Property::set(_info
->rootWindow(),
384 otk::Property::atoms
.net_supporting_wm_check
,
385 otk::Property::atoms
.window
, _supportwindow
);
387 //set properties on the supporting window
388 otk::Property::set(_supportwindow
, otk::Property::atoms
.net_wm_name
,
389 otk::Property::utf8
, "Openbox");
390 otk::Property::set(_supportwindow
,
391 otk::Property::atoms
.net_supporting_wm_check
,
392 otk::Property::atoms
.window
, _supportwindow
);
396 otk::Property::atoms
.net_current_desktop
,
397 otk::Property::atoms
.net_number_of_desktops
,
398 otk::Property::atoms
.net_desktop_geometry
,
399 otk::Property::atoms
.net_desktop_viewport
,
400 otk::Property::atoms
.net_active_window
,
401 otk::Property::atoms
.net_workarea
,
402 otk::Property::atoms
.net_client_list
,
403 otk::Property::atoms
.net_client_list_stacking
,
404 otk::Property::atoms
.net_desktop_names
,
405 otk::Property::atoms
.net_close_window
,
406 otk::Property::atoms
.net_desktop_layout
,
407 otk::Property::atoms
.net_showing_desktop
,
408 otk::Property::atoms
.net_wm_name
,
409 otk::Property::atoms
.net_wm_visible_name
,
410 otk::Property::atoms
.net_wm_icon_name
,
411 otk::Property::atoms
.net_wm_visible_icon_name
,
412 otk::Property::atoms
.net_wm_desktop
,
413 otk::Property::atoms
.net_wm_strut
,
414 otk::Property::atoms
.net_wm_window_type
,
415 otk::Property::atoms
.net_wm_window_type_desktop
,
416 otk::Property::atoms
.net_wm_window_type_dock
,
417 otk::Property::atoms
.net_wm_window_type_toolbar
,
418 otk::Property::atoms
.net_wm_window_type_menu
,
419 otk::Property::atoms
.net_wm_window_type_utility
,
420 otk::Property::atoms
.net_wm_window_type_splash
,
421 otk::Property::atoms
.net_wm_window_type_dialog
,
422 otk::Property::atoms
.net_wm_window_type_normal
,
424 otk::Property::atoms.net_wm_moveresize,
425 otk::Property::atoms.net_wm_moveresize_size_topleft,
426 otk::Property::atoms.net_wm_moveresize_size_topright,
427 otk::Property::atoms.net_wm_moveresize_size_bottomleft,
428 otk::Property::atoms.net_wm_moveresize_size_bottomright,
429 otk::Property::atoms.net_wm_moveresize_move,
431 otk::Property::atoms
.net_wm_allowed_actions
,
432 otk::Property::atoms
.net_wm_action_move
,
433 otk::Property::atoms
.net_wm_action_resize
,
434 otk::Property::atoms
.net_wm_action_minimize
,
435 otk::Property::atoms
.net_wm_action_shade
,
436 /* otk::Property::atoms.net_wm_action_stick,*/
437 otk::Property::atoms
.net_wm_action_maximize_horz
,
438 otk::Property::atoms
.net_wm_action_maximize_vert
,
439 otk::Property::atoms
.net_wm_action_fullscreen
,
440 otk::Property::atoms
.net_wm_action_change_desktop
,
441 otk::Property::atoms
.net_wm_action_close
,
443 otk::Property::atoms
.net_wm_state
,
444 otk::Property::atoms
.net_wm_state_modal
,
445 otk::Property::atoms
.net_wm_state_maximized_vert
,
446 otk::Property::atoms
.net_wm_state_maximized_horz
,
447 otk::Property::atoms
.net_wm_state_shaded
,
448 otk::Property::atoms
.net_wm_state_skip_taskbar
,
449 otk::Property::atoms
.net_wm_state_skip_pager
,
450 otk::Property::atoms
.net_wm_state_hidden
,
451 otk::Property::atoms
.net_wm_state_fullscreen
,
452 otk::Property::atoms
.net_wm_state_above
,
453 otk::Property::atoms
.net_wm_state_below
,
455 const int num_supported
= sizeof(supported
)/sizeof(Atom
);
457 otk::Property::set(_info
->rootWindow(), otk::Property::atoms
.net_supported
,
458 otk::Property::atoms
.atom
, supported
, num_supported
);
462 void Screen::changeClientList()
465 unsigned int size
= clients
.size();
467 // create an array of the window ids
471 windows
= new Window
[size
];
473 ClientList::const_iterator it
= clients
.begin();
474 const ClientList::const_iterator end
= clients
.end();
475 for (; it
!= end
; ++it
, ++win_it
)
476 *win_it
= (*it
)->window();
478 windows
= (Window
*) 0;
480 otk::Property::set(_info
->rootWindow(), otk::Property::atoms
.net_client_list
,
481 otk::Property::atoms
.window
, windows
, size
);
486 changeStackingList();
490 void Screen::changeStackingList()
493 unsigned int size
= _stacking
.size();
495 assert(size
== clients
.size()); // just making sure.. :)
498 // create an array of the window ids (from bottom to top, reverse order!)
502 windows
= new Window
[size
];
504 ClientList::const_reverse_iterator it
= _stacking
.rbegin();
505 const ClientList::const_reverse_iterator end
= _stacking
.rend();
506 for (; it
!= end
; ++it
, ++win_it
)
507 *win_it
= (*it
)->window();
509 windows
= (Window
*) 0;
511 otk::Property::set(_info
->rootWindow(),
512 otk::Property::atoms
.net_client_list_stacking
,
513 otk::Property::atoms
.window
, windows
, size
);
520 void Screen::manageWindow(Window window
)
524 XSetWindowAttributes attrib_set
;
526 XWindowAttributes attrib
;
528 otk::display
->grab();
530 // check if it has already been unmapped by the time we started mapping
531 // the grab does a sync so we don't have to here
532 if (XCheckTypedWindowEvent(**otk::display
, window
, DestroyNotify
, &e
) ||
533 XCheckTypedWindowEvent(**otk::display
, window
, UnmapNotify
, &e
)) {
534 XPutBackEvent(**otk::display
, &e
);
536 otk::display
->ungrab();
537 return; // don't manage it
540 if (!XGetWindowAttributes(**otk::display
, window
, &attrib
) ||
541 attrib
.override_redirect
) {
542 otk::display
->ungrab();
543 return; // don't manage it
546 // is the window a docking app
547 if ((wmhint
= XGetWMHints(**otk::display
, window
))) {
548 if ((wmhint
->flags
& StateHint
) &&
549 wmhint
->initial_state
== WithdrawnState
) {
550 //slit->addClient(w); // XXX: make dock apps work!
552 otk::display
->ungrab();
559 // choose the events we want to receive on the CLIENT window
560 attrib_set
.event_mask
= Client::event_mask
;
561 attrib_set
.do_not_propagate_mask
= Client::no_propagate_mask
;
562 XChangeWindowAttributes(**otk::display
, window
,
563 CWEventMask
|CWDontPropagate
, &attrib_set
);
565 // create the Client class, which gets all of the hints on the window
566 client
= new Client(_number
, window
);
567 // register for events
568 openbox
->registerHandler(window
, client
);
569 // add to the wm's map
570 openbox
->addClient(window
, client
);
572 // we dont want a border on the client
573 client
->toggleClientBorder(false);
575 // specify that if we exit, the window should not be destroyed and should be
576 // reparented back to root automatically
577 XChangeSaveSet(**otk::display
, window
, SetModeInsert
);
579 // create the decoration frame for the client window
580 client
->frame
= new Frame(client
);
581 // register the plate for events (map req's)
582 // this involves removing itself from the handler list first, since it is
583 // auto added to the list, being a widget. we won't get any events on the
584 // plate except for events for the client (SubstructureRedirectMask)
585 openbox
->clearHandler(client
->frame
->plate());
586 openbox
->registerHandler(client
->frame
->plate(), client
);
588 // add to the wm's map
589 Window
*w
= client
->frame
->allWindows();
590 for (unsigned int i
= 0; w
[i
]; ++i
)
591 openbox
->addClient(w
[i
], client
);
594 // reparent the client to the frame
595 client
->frame
->grabClient();
597 if (openbox
->state() != Openbox::State_Starting
) {
598 // position the window intelligenty .. hopefully :)
599 // call the python PLACEWINDOW binding
600 EventData
data(_number
, client
, EventAction::PlaceWindow
, 0);
601 openbox
->bindings()->fireEvent(&data
);
604 EventData
ddata(_number
, client
, EventAction::DisplayingWindow
, 0);
605 openbox
->bindings()->fireEvent(&ddata
);
609 client
->applyStartupState();
611 otk::display
->ungrab();
613 // add to the screen's list
614 clients
.push_back(client
);
615 // once the client is in the list, update our strut to include the new
616 // client's (it is good that this happens after window placement!)
618 // this puts into the stacking order, then raises it
619 _stacking
.push_back(client
);
621 // update the root properties
624 openbox
->bindings()->grabButtons(true, client
);
626 EventData
ndata(_number
, client
, EventAction::NewWindow
, 0);
627 openbox
->bindings()->fireEvent(&ndata
);
630 printf("Managed window 0x%lx frame 0x%lx\n",
631 window
, client
->frame
->window());
636 void Screen::unmanageWindow(Client
*client
)
638 Frame
*frame
= client
->frame
;
640 // call the python CLOSEWINDOW binding
641 EventData
data(_number
, client
, EventAction::CloseWindow
, 0);
642 openbox
->bindings()->fireEvent(&data
);
644 openbox
->bindings()->grabButtons(false, client
);
646 // remove from the wm's map
647 openbox
->removeClient(client
->window());
648 Window
*w
= frame
->allWindows();
649 for (unsigned int i
= 0; w
[i
]; ++i
)
650 openbox
->addClient(w
[i
], client
);
652 // unregister for handling events
653 openbox
->clearHandler(client
->window());
655 // remove the window from our save set
656 XChangeSaveSet(**otk::display
, client
->window(), SetModeDelete
);
658 // we dont want events no more
659 XSelectInput(**otk::display
, client
->window(), NoEventMask
);
663 // give the client its border back
664 client
->toggleClientBorder(true);
666 // reparent the window out of the frame
667 frame
->releaseClient();
670 Window framewin
= client
->frame
->window();
672 delete client
->frame
;
675 // remove from the stacking order
676 _stacking
.remove(client
);
678 // remove from the screen's list
679 clients
.remove(client
);
681 // once the client is out of the list, update our strut to remove it's
685 // unset modal before dropping our focus
686 client
->_modal
= false;
688 // unfocus the client (calls the focus callbacks)
692 printf("Unmanaged window 0x%lx frame 0x%lx\n", client
->window(), framewin
);
697 // update the root properties
701 void Screen::lowerWindow(Client
*client
)
703 Window wins
[2]; // only ever restack 2 windows.
705 assert(!_stacking
.empty()); // this would be bad
707 ClientList::iterator it
= --_stacking
.end();
708 const ClientList::iterator end
= _stacking
.begin();
710 if (client
->modal() && client
->transientFor()) {
711 // don't let a modal window lower below its transient_for
712 it
= std::find(_stacking
.begin(), _stacking
.end(), client
->transientFor());
713 assert(it
!= _stacking
.end());
715 wins
[0] = (it
== _stacking
.begin() ? _focuswindow
:
716 ((*(--ClientList::const_iterator(it
)))->frame
->window()));
717 wins
[1] = client
->frame
->window();
718 if (wins
[0] == wins
[1]) return; // already right above the window
720 _stacking
.remove(client
);
721 _stacking
.insert(it
, client
);
723 for (; it
!= end
&& (*it
)->layer() < client
->layer(); --it
);
724 if (*it
== client
) return; // already the bottom, return
726 wins
[0] = (*it
)->frame
->window();
727 wins
[1] = client
->frame
->window();
729 _stacking
.remove(client
);
730 _stacking
.insert(++it
, client
);
733 XRestackWindows(**otk::display
, wins
, 2);
734 changeStackingList();
737 void Screen::raiseWindow(Client
*client
)
739 Window wins
[2]; // only ever restack 2 windows.
741 assert(!_stacking
.empty()); // this would be bad
743 Client
*m
= client
->findModalChild();
744 // if we have a modal child, raise it instead, we'll go along tho later
745 if (m
) raiseWindow(m
);
747 // remove the client before looking so we can't run into ourselves
748 _stacking
.remove(client
);
750 ClientList::iterator it
= _stacking
.begin();
751 const ClientList::iterator end
= _stacking
.end();
753 // the stacking list is from highest to lowest
754 for (; it
!= end
&& ((*it
)->layer() > client
->layer() || m
== *it
); ++it
);
757 if our new position is the top, we want to stack under the _focuswindow
758 otherwise, we want to stack under the previous window in the stack.
760 wins
[0] = (it
== _stacking
.begin() ? _focuswindow
:
761 ((*(--ClientList::const_iterator(it
)))->frame
->window()));
762 wins
[1] = client
->frame
->window();
764 _stacking
.insert(it
, client
);
766 XRestackWindows(**otk::display
, wins
, 2);
768 changeStackingList();
771 void Screen::changeDesktop(unsigned int desktop
)
773 if (desktop
>= _num_desktops
) return;
775 printf("Moving to desktop %u\n", desktop
);
777 unsigned int old
= _desktop
;
780 otk::Property::set(_info
->rootWindow(),
781 otk::Property::atoms
.net_current_desktop
,
782 otk::Property::atoms
.cardinal
, _desktop
);
784 if (old
== _desktop
) return;
786 ClientList::iterator it
, end
= clients
.end();
787 for (it
= clients
.begin(); it
!= end
; ++it
)
790 // force the callbacks to fire
791 if (!openbox
->focusedClient())
792 openbox
->setFocusedClient(0);
795 void Screen::changeNumDesktops(unsigned int num
)
799 if (!(num
> 0)) return;
801 // move windows on desktops that will no longer exist!
802 ClientList::iterator it
, end
= clients
.end();
803 for (it
= clients
.begin(); it
!= end
; ++it
) {
804 unsigned int d
= (*it
)->desktop();
805 if (d
>= num
&& d
!= 0xffffffff) {
807 ce
.xclient
.type
= ClientMessage
;
808 ce
.xclient
.message_type
= otk::Property::atoms
.net_wm_desktop
;
809 ce
.xclient
.display
= **otk::display
;
810 ce
.xclient
.window
= (*it
)->window();
811 ce
.xclient
.format
= 32;
812 ce
.xclient
.data
.l
[0] = num
- 1;
813 XSendEvent(**otk::display
, _info
->rootWindow(), False
,
814 SubstructureNotifyMask
| SubstructureRedirectMask
, &ce
);
819 otk::Property::set(_info
->rootWindow(),
820 otk::Property::atoms
.net_number_of_desktops
,
821 otk::Property::atoms
.cardinal
, _num_desktops
);
823 // set the viewport hint
824 unsigned long *viewport
= new unsigned long[_num_desktops
* 2];
825 memset(viewport
, 0, sizeof(unsigned long) * _num_desktops
* 2);
826 otk::Property::set(_info
->rootWindow(),
827 otk::Property::atoms
.net_desktop_viewport
,
828 otk::Property::atoms
.cardinal
,
829 viewport
, _num_desktops
* 2);
832 // change our struts/area to match
833 _area
.resize(_num_desktops
+ 1);
834 _struts
.resize(_num_desktops
+ 1);
837 // the number of rows/columns will differ
838 updateDesktopLayout();
840 // change our desktop if we're on one that no longer exists!
841 if (_desktop
>= _num_desktops
)
842 changeDesktop(_num_desktops
- 1);
846 void Screen::updateDesktopNames()
848 unsigned long num
= (unsigned) -1;
850 if (!otk::Property::get(_info
->rootWindow(),
851 otk::Property::atoms
.net_desktop_names
,
852 otk::Property::utf8
, &num
, &_desktop_names
))
853 _desktop_names
.clear();
854 while (_desktop_names
.size() < _num_desktops
)
855 _desktop_names
.push_back("Unnamed");
859 void Screen::setDesktopName(unsigned int i
, const otk::ustring
&name
)
861 if (i
>= _num_desktops
) return;
863 otk::Property::StringVect newnames
= _desktop_names
;
865 otk::Property::set(_info
->rootWindow(),
866 otk::Property::atoms
.net_desktop_names
,
867 otk::Property::utf8
, newnames
);
871 const otk::Rect
& Screen::area(unsigned int desktop
) const {
872 assert(desktop
< _num_desktops
|| desktop
== 0xffffffff);
873 if (desktop
< _num_desktops
)
874 return _area
[desktop
];
876 return _area
[_num_desktops
];
879 void Screen::installColormap(bool install
) const
882 XInstallColormap(**otk::display
, _info
->colormap());
884 XUninstallColormap(**otk::display
, _info
->colormap());
887 void Screen::showDesktop(bool show
)
889 if (show
== _showing_desktop
) return; // no change
891 // save the window focus, and restore it when leaving the show-desktop mode
892 static Window saved_focus
= 0;
894 Client
*c
= openbox
->focusedClient();
895 if (c
) saved_focus
= c
->window();
898 _showing_desktop
= show
;
900 ClientList::iterator it
, end
= clients
.end();
901 for (it
= clients
.begin(); it
!= end
; ++it
) {
902 if ((*it
)->type() == Client::Type_Desktop
) {
910 Client
*f
= openbox
->focusedClient();
911 if (!f
|| f
->type() == Client::Type_Desktop
) {
912 Client
*c
= openbox
->findClient(saved_focus
);
917 otk::Property::set(_info
->rootWindow(),
918 otk::Property::atoms
.net_showing_desktop
,
919 otk::Property::atoms
.cardinal
,
923 void Screen::propertyHandler(const XPropertyEvent
&e
)
925 otk::EventHandler::propertyHandler(e
);
927 // compress changes to a single property into a single change
929 while (XCheckTypedWindowEvent(**otk::display
, _info
->rootWindow(),
931 // XXX: it would be nice to compress ALL changes to a property, not just
932 // changes in a row without other props between.
933 if (ce
.xproperty
.atom
!= e
.atom
) {
934 XPutBackEvent(**otk::display
, &ce
);
939 if (e
.atom
== otk::Property::atoms
.net_desktop_names
)
940 updateDesktopNames();
941 else if (e
.atom
== otk::Property::atoms
.net_desktop_layout
)
942 updateDesktopLayout();
946 void Screen::clientMessageHandler(const XClientMessageEvent
&e
)
948 otk::EventHandler::clientMessageHandler(e
);
950 if (e
.format
!= 32) return;
952 if (e
.message_type
== otk::Property::atoms
.net_current_desktop
) {
953 changeDesktop(e
.data
.l
[0]);
954 } else if (e
.message_type
== otk::Property::atoms
.net_number_of_desktops
) {
955 changeNumDesktops(e
.data
.l
[0]);
956 } else if (e
.message_type
== otk::Property::atoms
.net_showing_desktop
) {
957 showDesktop(e
.data
.l
[0] != 0);
962 void Screen::mapRequestHandler(const XMapRequestEvent
&e
)
964 otk::EventHandler::mapRequestHandler(e
);
967 printf("MapRequest for 0x%lx\n", e
.window
);
970 Client
*c
= openbox
->findClient(e
.window
);
973 printf("DEBUG: MAP REQUEST CAUGHT IN SCREEN. IGNORED.\n");
976 if (_showing_desktop
)
977 showDesktop(false); // leave showing-the-desktop mode
978 manageWindow(e
.window
);