]> icculus.org git repositories - dana/openbox.git/blob - src/screen.cc
put the Python.h header in the .cc
[dana/openbox.git] / src / screen.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #include "config.h"
4
5 #include "screen.hh"
6 #include "client.hh"
7 #include "openbox.hh"
8 #include "frame.hh"
9 #include "bindings.hh"
10 #include "python.hh"
11 #include "otk/display.hh"
12 #include "otk/property.hh"
13
14 extern "C" {
15 #ifdef    HAVE_UNISTD_H
16 #  include <sys/types.h>
17 #  include <unistd.h>
18 #endif // HAVE_UNISTD_H
19
20 #include "gettext.h"
21 #define _(str) gettext(str)
22 }
23
24 #include <vector>
25 #include <algorithm>
26 #include <cstdio>
27 #include <cstring>
28
29 static bool running;
30 static int anotherWMRunning(Display *display, XErrorEvent *) {
31   printf(_("Another window manager already running on display %s.\n"),
32          DisplayString(display));
33   running = true;
34   return -1;
35 }
36
37
38 namespace ob {
39
40
41 Screen::Screen(int screen)
42   : _number(screen)
43 {
44   assert(screen >= 0); assert(screen < ScreenCount(**otk::display));
45   _info = otk::display->screenInfo(screen);
46
47   ::running = false;
48   XErrorHandler old = XSetErrorHandler(::anotherWMRunning);
49   XSelectInput(**otk::display, _info->rootWindow(),
50                Screen::event_mask);
51   XSync(**otk::display, false);
52   XSetErrorHandler(old);
53
54   _managed = !::running;
55   if (! _managed) return; // was unable to manage the screen
56
57 #ifdef DEBUG
58   printf(_("Managing screen %d: visual 0x%lx, depth %d\n"),
59          _number, XVisualIDFromVisual(_info->visual()), _info->depth());
60 #endif
61
62   otk::Property::set(_info->rootWindow(), otk::Property::atoms.openbox_pid,
63                      otk::Property::atoms.cardinal, (unsigned long) getpid());
64
65   // set the mouse cursor for the root window (the default cursor)
66   XDefineCursor(**otk::display, _info->rootWindow(),
67                 openbox->cursors().session);
68
69   // XXX: initialize the screen's style
70   /*
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);
79       ::exit(1);
80     }
81   }
82   _style.load(sconfig);
83   */
84   otk::display->renderControl(_number)->
85     drawRoot(*otk::RenderStyle::style(_number)->rootColor());
86
87   // set up notification of netwm support
88   changeSupportedAtoms();
89
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);
96
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
105
106   _desktop = 0;
107   
108   if (!python_get_long("NUMBER_OF_DESKTOPS", (long*)&_num_desktops))
109     _num_desktops = 1;
110   changeNumDesktops(_num_desktops); // set the hint
111
112   changeDesktop(0); // set the hint
113
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);
119
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);
127   
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
131
132   updateDesktopLayout();
133
134   // register this class as the event handler for the root window
135   openbox->registerHandler(_info->rootWindow(), this);
136
137   // call the python Startup callbacks
138   EventData data(_number, 0, EventAction::Startup, 0);
139   openbox->bindings()->fireEvent(&data);
140 }
141
142
143 Screen::~Screen()
144 {
145   if (! _managed) return;
146
147   XSelectInput(**otk::display, _info->rootWindow(), NoEventMask);
148   
149   // unmanage all windows
150   while (!clients.empty())
151     unmanageWindow(clients.front());
152
153   // call the python Shutdown callbacks
154   EventData data(_number, 0, EventAction::Shutdown, 0);
155   openbox->bindings()->fireEvent(&data);
156
157   XDestroyWindow(**otk::display, _focuswindow);
158   XDestroyWindow(**otk::display, _supportwindow);
159 }
160
161
162 void Screen::manageExisting()
163 {
164   unsigned int i, j, nchild;
165   Window r, p, *children;
166   XQueryTree(**otk::display, _info->rootWindow(), &r, &p,
167              &children, &nchild);
168
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;
172
173     XWMHints *wmhints = XGetWMHints(**otk::display,
174                                     children[i]);
175
176     if (wmhints) {
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) {
181             children[j] = None;
182             break;
183           }
184         }
185       }
186
187       XFree(wmhints);
188     }
189   }
190
191   // manage shown windows
192   for (i = 0; i < nchild; ++i) {
193     if (children[i] == None)
194       continue;
195
196     XWindowAttributes attrib;
197     if (XGetWindowAttributes(**otk::display, children[i], &attrib)) {
198       if (attrib.override_redirect) continue;
199
200       if (attrib.map_state != IsUnmapped) {
201         manageWindow(children[i]);
202       }
203     }
204   }
205
206   XFree(children);
207 }
208
209 void Screen::updateDesktopLayout()
210 {
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;
217   
218   // defaults
219   _layout.orientation = DesktopLayout::Horizontal;
220   _layout.start_corner = DesktopLayout::TopLeft;
221   _layout.rows = 1;
222   _layout.columns = _num_desktops;
223
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,
228                          &num, &data)) {
229     if (num >= 4) {
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;
238
239       // fill in a zero rows/columns
240       if (!(data[1] == 0 && data[2] == 0)) { // both 0's is bad data..
241         if (data[1] == 0) {
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];
245         }
246         _layout.columns = data[1];
247         _layout.rows = data[2];
248       }
249
250       // bounds checking
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) /
254             _layout.rows)
255           _layout.columns = (_num_desktops + _num_desktops % _layout.rows) /
256             _layout.rows;
257       } else {
258         if (_layout.columns > _num_desktops) _layout.columns = _num_desktops;
259         if (_layout.rows > (_num_desktops + _num_desktops % _layout.columns) /
260             _layout.columns)
261           _layout.rows = (_num_desktops + _num_desktops % _layout.columns) /
262             _layout.columns;
263       }
264     }
265     delete [] data;
266   }
267 }
268
269 void Screen::updateStruts()
270 {
271   struct ApplyStrut {
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);
277     }
278   } apply;
279
280   StrutList::iterator sit, send = _struts.end();
281   // reset them all
282   for (sit = _struts.begin(); sit != send; ++sit)
283     sit->left = sit->right = sit->top = sit->bottom = 0;
284
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
288     
289     unsigned int desk = (*it)->desktop();
290     const otk::Strut &s = (*it)->strut();
291
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);
297     else
298       assert(false); // invalid desktop otherwise..
299     // apply to the 'all desktops' strut
300     apply(_struts.back(), s);
301   }
302   changeWorkArea();
303 }
304
305
306 void Screen::changeWorkArea()
307 {
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];
311 /*
312 #ifdef    XINERAMA
313   // reset to the full areas
314   if (isXineramaActive())
315     xineramaUsableArea = getXineramaAreas();
316 #endif // XINERAMA
317 */
318   
319     _area[i] = otk::Rect(_struts[i].left, _struts[i].top,
320                          _info->size().width() - (_struts[i].left +
321                                                   _struts[i].right),
322                          _info->size().height() - (_struts[i].top +
323                                                    _struts[i].bottom));
324     
325 /*
326 #ifdef    XINERAMA
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());
334       }
335       if (xit->y() < usableArea.y()) {
336         xit->setY(usableArea.y());
337         xit->setHeight(xit->height() - usableArea.y());
338       }
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());
343     }
344   }
345 #endif // XINERAMA
346 */
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)
353             (*it)->remaximize();
354         } else {
355           // the 'all desktops' size
356           if ((*it)->desktop() == 0xffffffff)
357             (*it)->remaximize();
358         }
359     }
360
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();
367     }
368   }
369   otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_workarea,
370                      otk::Property::atoms.cardinal, dims, 4 * _num_desktops);
371   delete [] dims;
372 }
373
374
375 void Screen::changeSupportedAtoms()
376 {
377   // create the netwm support window
378   _supportwindow = XCreateSimpleWindow(**otk::display,
379                                        _info->rootWindow(),
380                                        0, 0, 1, 1, 0, 0, 0);
381
382   // set supporting window
383   otk::Property::set(_info->rootWindow(),
384                      otk::Property::atoms.net_supporting_wm_check,
385                      otk::Property::atoms.window, _supportwindow);
386
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);
393
394   
395   Atom supported[] = {
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,
423 /*
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,
430 */
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,
442
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,
454   };
455   const int num_supported = sizeof(supported)/sizeof(Atom);
456
457   otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_supported,
458                      otk::Property::atoms.atom, supported, num_supported);
459 }
460
461
462 void Screen::changeClientList()
463 {
464   Window *windows;
465   unsigned int size = clients.size();
466
467   // create an array of the window ids
468   if (size > 0) {
469     Window *win_it;
470     
471     windows = new Window[size];
472     win_it = windows;
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();
477   } else
478     windows = (Window*) 0;
479
480   otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_client_list,
481                      otk::Property::atoms.window, windows, size);
482
483   if (size)
484     delete [] windows;
485
486   changeStackingList();
487 }
488
489
490 void Screen::changeStackingList()
491 {
492   Window *windows;
493   unsigned int size = _stacking.size();
494
495   assert(size == clients.size()); // just making sure.. :)
496
497   
498   // create an array of the window ids (from bottom to top, reverse order!)
499   if (size > 0) {
500     Window *win_it;
501     
502     windows = new Window[size];
503     win_it = windows;
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();
508   } else
509     windows = (Window*) 0;
510
511   otk::Property::set(_info->rootWindow(),
512                      otk::Property::atoms.net_client_list_stacking,
513                      otk::Property::atoms.window, windows, size);
514
515   if (size)
516     delete [] windows;
517 }
518
519
520 void Screen::manageWindow(Window window)
521 {
522   Client *client = 0;
523   XWMHints *wmhint;
524   XSetWindowAttributes attrib_set;
525   XEvent e;
526   XWindowAttributes attrib;
527
528   otk::display->grab();
529
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);
535     
536     otk::display->ungrab();
537     return; // don't manage it
538   }
539   
540   if (!XGetWindowAttributes(**otk::display, window, &attrib) ||
541       attrib.override_redirect) {
542     otk::display->ungrab();
543     return; // don't manage it
544   }
545   
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!
551
552       otk::display->ungrab();
553       XFree(wmhint);
554       return;
555     }
556     XFree(wmhint);
557   }
558
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);
564
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);
571
572   // we dont want a border on the client
573   client->toggleClientBorder(false);
574   
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);
578
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);
587
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);
592   delete [] w;
593
594   // reparent the client to the frame
595   client->frame->grabClient();
596
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);
602   }
603
604   EventData ddata(_number, client, EventAction::DisplayingWindow, 0);
605   openbox->bindings()->fireEvent(&ddata);
606
607   client->showhide();
608
609   client->applyStartupState();
610
611   otk::display->ungrab();
612
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!)
617   updateStruts();
618   // this puts into the stacking order, then raises it
619   _stacking.push_back(client);
620   raiseWindow(client);
621   // update the root properties
622   changeClientList();
623
624   openbox->bindings()->grabButtons(true, client);
625
626   EventData ndata(_number, client, EventAction::NewWindow, 0);
627   openbox->bindings()->fireEvent(&ndata);
628
629 #ifdef DEBUG
630   printf("Managed window 0x%lx frame 0x%lx\n",
631          window, client->frame->window());
632 #endif
633 }
634
635
636 void Screen::unmanageWindow(Client *client)
637 {
638   Frame *frame = client->frame;
639
640   // call the python CLOSEWINDOW binding 
641   EventData data(_number, client, EventAction::CloseWindow, 0);
642   openbox->bindings()->fireEvent(&data);
643
644   openbox->bindings()->grabButtons(false, client);
645
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);
651   delete [] w;
652   // unregister for handling events
653   openbox->clearHandler(client->window());
654   
655   // remove the window from our save set
656   XChangeSaveSet(**otk::display, client->window(), SetModeDelete);
657
658   // we dont want events no more
659   XSelectInput(**otk::display, client->window(), NoEventMask);
660
661   frame->hide();
662
663   // give the client its border back
664   client->toggleClientBorder(true);
665
666   // reparent the window out of the frame
667   frame->releaseClient();
668
669 #ifdef DEBUG
670   Window framewin = client->frame->window();
671 #endif
672   delete client->frame;
673   client->frame = 0;
674
675   // remove from the stacking order
676   _stacking.remove(client);
677
678   // remove from the screen's list
679   clients.remove(client);
680
681   // once the client is out of the list, update our strut to remove it's
682   // influence
683   updateStruts();
684
685   // unset modal before dropping our focus
686   client->_modal = false;
687   
688   // unfocus the client (calls the focus callbacks)
689   client->unfocus();
690
691 #ifdef DEBUG
692   printf("Unmanaged window 0x%lx frame 0x%lx\n", client->window(), framewin);
693 #endif
694   
695   delete client;
696
697   // update the root properties
698   changeClientList();
699 }
700
701 void Screen::lowerWindow(Client *client)
702 {
703   Window wins[2];  // only ever restack 2 windows.
704
705   assert(!_stacking.empty()); // this would be bad
706
707   ClientList::iterator it = --_stacking.end();
708   const ClientList::iterator end = _stacking.begin();
709
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());
714
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
719
720     _stacking.remove(client);
721     _stacking.insert(it, client);
722   } else {
723     for (; it != end && (*it)->layer() < client->layer(); --it);
724     if (*it == client) return;          // already the bottom, return
725
726     wins[0] = (*it)->frame->window();
727     wins[1] = client->frame->window();
728
729     _stacking.remove(client);
730     _stacking.insert(++it, client);
731   }
732
733   XRestackWindows(**otk::display, wins, 2);
734   changeStackingList();
735 }
736
737 void Screen::raiseWindow(Client *client)
738 {
739   Window wins[2];  // only ever restack 2 windows.
740
741   assert(!_stacking.empty()); // this would be bad
742
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);
746   
747   // remove the client before looking so we can't run into ourselves
748   _stacking.remove(client);
749   
750   ClientList::iterator it = _stacking.begin();
751   const ClientList::iterator end = _stacking.end();
752
753   // the stacking list is from highest to lowest
754   for (; it != end && ((*it)->layer() > client->layer() || m == *it); ++it);
755
756   /*
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.
759   */
760   wins[0] = (it == _stacking.begin() ? _focuswindow :
761              ((*(--ClientList::const_iterator(it)))->frame->window()));
762   wins[1] = client->frame->window();
763
764   _stacking.insert(it, client);
765
766   XRestackWindows(**otk::display, wins, 2);
767
768   changeStackingList(); 
769 }
770
771 void Screen::changeDesktop(unsigned int desktop)
772 {
773   if (desktop >= _num_desktops) return;
774
775   printf("Moving to desktop %u\n", desktop);
776   
777   unsigned int old = _desktop;
778   
779   _desktop = desktop;
780   otk::Property::set(_info->rootWindow(),
781                      otk::Property::atoms.net_current_desktop,
782                      otk::Property::atoms.cardinal, _desktop);
783
784   if (old == _desktop) return;
785
786   ClientList::iterator it, end = clients.end();
787   for (it = clients.begin(); it != end; ++it)
788     (*it)->showhide();
789
790   // force the callbacks to fire
791   if (!openbox->focusedClient())
792     openbox->setFocusedClient(0);
793 }
794
795 void Screen::changeNumDesktops(unsigned int num)
796 {
797   assert(num > 0);
798   
799   if (!(num > 0)) return;
800
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) {
806       XEvent ce;
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);
815     }
816   }
817
818   _num_desktops = num;
819   otk::Property::set(_info->rootWindow(),
820                      otk::Property::atoms.net_number_of_desktops,
821                      otk::Property::atoms.cardinal, _num_desktops);
822
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);
830   delete [] viewport;
831
832   // change our struts/area to match
833   _area.resize(_num_desktops + 1);
834   _struts.resize(_num_desktops + 1);
835   updateStruts();
836
837   // the number of rows/columns will differ
838   updateDesktopLayout();
839
840   // change our desktop if we're on one that no longer exists!
841   if (_desktop >= _num_desktops)
842     changeDesktop(_num_desktops - 1);
843 }
844
845
846 void Screen::updateDesktopNames()
847 {
848   unsigned long num = (unsigned) -1;
849   
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");
856 }
857
858
859 void Screen::setDesktopName(unsigned int i, const otk::ustring &name)
860 {
861   if (i >= _num_desktops) return;
862
863   otk::Property::StringVect newnames = _desktop_names;
864   newnames[i] = name;
865   otk::Property::set(_info->rootWindow(),
866                      otk::Property::atoms.net_desktop_names,
867                      otk::Property::utf8, newnames);
868 }
869
870 otk::ustring Screen::desktopName(unsigned int i) const
871 {
872   if (i >= _num_desktops) return "";
873   return _desktop_names[i];
874 }
875
876 const otk::Rect& Screen::area(unsigned int desktop) const {
877   assert(desktop < _num_desktops || desktop == 0xffffffff);
878   if (desktop < _num_desktops)
879     return _area[desktop];
880   else
881     return _area[_num_desktops];
882 }
883
884 void Screen::installColormap(bool install) const
885 {
886   if (install)
887     XInstallColormap(**otk::display, _info->colormap());
888   else
889     XUninstallColormap(**otk::display, _info->colormap());
890 }
891
892 void Screen::showDesktop(bool show)
893 {
894   if (show == _showing_desktop) return; // no change
895
896   // save the window focus, and restore it when leaving the show-desktop mode
897   static Window saved_focus = 0;
898   if (show) {
899     Client *c = openbox->focusedClient();
900     if (c) saved_focus = c->window();
901   }
902   
903   _showing_desktop = show;
904
905   ClientList::iterator it, end = clients.end();
906   for (it = clients.begin(); it != end; ++it) {
907     if ((*it)->type() == Client::Type_Desktop) {
908       if (show)
909         (*it)->focus();
910     } else
911       (*it)->showhide();
912   }
913
914   if (!show) {
915     Client *f = openbox->focusedClient();
916     if (!f || f->type() == Client::Type_Desktop) {
917       Client *c = openbox->findClient(saved_focus);
918       if (c) c->focus();
919     }
920   }
921
922   otk::Property::set(_info->rootWindow(),
923                      otk::Property::atoms.net_showing_desktop,
924                      otk::Property::atoms.cardinal,
925                      show ? 1 : 0);
926 }
927
928 void Screen::propertyHandler(const XPropertyEvent &e)
929 {
930   otk::EventHandler::propertyHandler(e);
931
932   // compress changes to a single property into a single change
933   XEvent ce;
934   while (XCheckTypedWindowEvent(**otk::display, _info->rootWindow(),
935                                 e.type, &ce)) {
936     // XXX: it would be nice to compress ALL changes to a property, not just
937     //      changes in a row without other props between.
938     if (ce.xproperty.atom != e.atom) {
939       XPutBackEvent(**otk::display, &ce);
940       break;
941     }
942   }
943
944   if (e.atom == otk::Property::atoms.net_desktop_names)
945     updateDesktopNames();
946   else if (e.atom == otk::Property::atoms.net_desktop_layout)
947     updateDesktopLayout();
948 }
949
950
951 void Screen::clientMessageHandler(const XClientMessageEvent &e)
952 {
953   otk::EventHandler::clientMessageHandler(e);
954
955   if (e.format != 32) return;
956
957   if (e.message_type == otk::Property::atoms.net_current_desktop) {
958     changeDesktop(e.data.l[0]);
959   } else if (e.message_type == otk::Property::atoms.net_number_of_desktops) {
960     changeNumDesktops(e.data.l[0]);
961   } else if (e.message_type == otk::Property::atoms.net_showing_desktop) {
962     showDesktop(e.data.l[0] != 0);
963   }
964 }
965
966
967 void Screen::mapRequestHandler(const XMapRequestEvent &e)
968 {
969   otk::EventHandler::mapRequestHandler(e);
970
971 #ifdef    DEBUG
972   printf("MapRequest for 0x%lx\n", e.window);
973 #endif // DEBUG
974
975   Client *c = openbox->findClient(e.window);
976   if (c) {
977 #ifdef DEBUG
978     printf("DEBUG: MAP REQUEST CAUGHT IN SCREEN. IGNORED.\n");
979 #endif
980   } else {
981     if (_showing_desktop)
982       showDesktop(false); // leave showing-the-desktop mode
983     manageWindow(e.window);
984   }
985 }
986
987 }