]> icculus.org git repositories - dana/openbox.git/blob - src/screen.cc
remove includes for widgetbase.hh. fix bug with circular modal pointer.
[dana/openbox.git] / src / screen.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif
6
7 extern "C" {
8 #ifdef    HAVE_STDIO_H
9 #  include <stdio.h>
10 #endif // HAVE_STDIO_H
11
12 #ifdef    HAVE_STRING_H
13 #  include <string.h>
14 #endif // HAVE_STRING_H
15
16 #ifdef    HAVE_UNISTD_H
17 #  include <sys/types.h>
18 #  include <unistd.h>
19 #endif // HAVE_UNISTD_H
20
21 #include "gettext.h"
22 #define _(str) gettext(str)
23 }
24
25 #include "screen.hh"
26 #include "client.hh"
27 #include "openbox.hh"
28 #include "frame.hh"
29 #include "bindings.hh"
30 #include "python.hh"
31 #include "otk/display.hh"
32 #include "otk/property.hh"
33
34 #include <vector>
35 #include <algorithm>
36
37 static bool running;
38 static int anotherWMRunning(Display *display, XErrorEvent *) {
39   printf(_("Another window manager already running on display %s.\n"),
40          DisplayString(display));
41   running = true;
42   return -1;
43 }
44
45
46 namespace ob {
47
48
49 Screen::Screen(int screen)
50   : _number(screen)
51 {
52   assert(screen >= 0); assert(screen < ScreenCount(**otk::display));
53   _info = otk::display->screenInfo(screen);
54
55   ::running = false;
56   XErrorHandler old = XSetErrorHandler(::anotherWMRunning);
57   XSelectInput(**otk::display, _info->rootWindow(),
58                Screen::event_mask);
59   XSync(**otk::display, false);
60   XSetErrorHandler(old);
61
62   _managed = !::running;
63   if (! _managed) return; // was unable to manage the screen
64
65 #ifdef DEBUG
66   printf(_("Managing screen %d: visual 0x%lx, depth %d\n"),
67          _number, XVisualIDFromVisual(_info->visual()), _info->depth());
68 #endif
69
70   otk::Property::set(_info->rootWindow(), otk::Property::atoms.openbox_pid,
71                      otk::Property::atoms.cardinal, (unsigned long) getpid());
72
73   // set the mouse cursor for the root window (the default cursor)
74   XDefineCursor(**otk::display, _info->rootWindow(),
75                 openbox->cursors().session);
76
77   // XXX: initialize the screen's style
78   /*
79   otk::ustring stylepath;
80   python_get_string("THEME", &stylepath);
81   otk::Configuration sconfig(false);
82   sconfig.setFile(otk::expandTilde(stylepath.c_str()));
83   if (!sconfig.load()) {
84     sconfig.setFile(otk::expandTilde(DEFAULTSTYLE));
85     if (!sconfig.load()) {
86       printf(_("Unable to load default style: %s. Aborting.\n"), DEFAULTSTYLE);
87       ::exit(1);
88     }
89   }
90   _style.load(sconfig);
91   */
92   otk::display->renderControl(_number)->
93     drawRoot(*otk::RenderStyle::style(_number)->rootColor());
94
95   // set up notification of netwm support
96   changeSupportedAtoms();
97
98   // Set the netwm properties for geometry
99   unsigned long geometry[] = { _info->size().width(),
100                                _info->size().height() };
101   otk::Property::set(_info->rootWindow(),
102                      otk::Property::atoms.net_desktop_geometry,
103                      otk::Property::atoms.cardinal, geometry, 2);
104
105   // Set the net_desktop_names property
106   std::vector<otk::ustring> names;
107   python_get_stringlist("DESKTOP_NAMES", &names);
108   otk::Property::set(_info->rootWindow(),
109                      otk::Property::atoms.net_desktop_names,
110                      otk::Property::utf8, names);
111   // the above set() will cause the updateDesktopNames to fire right away so
112   // we have a list of desktop names
113
114   _desktop = 0;
115   
116   if (!python_get_long("NUMBER_OF_DESKTOPS", &_num_desktops))
117     _num_desktops = 1;
118   changeNumDesktops(_num_desktops); // set the hint
119
120   changeDesktop(0); // set the hint
121
122   // create the window which gets focus when no clients get it
123   XSetWindowAttributes attr;
124   attr.override_redirect = true;
125   _focuswindow = XCreateWindow(**otk::display, _info->rootWindow(),
126                                -100, -100, 1, 1, 0, 0, InputOnly,
127                                _info->visual(), CWOverrideRedirect, &attr);
128   XMapRaised(**otk::display, _focuswindow);
129   
130   // these may be further updated if any pre-existing windows are found in
131   // the manageExising() function
132   changeClientList();  // initialize the client lists, which will be empty
133   calcArea();          // initialize the available working area
134
135   // register this class as the event handler for the root window
136   openbox->registerHandler(_info->rootWindow(), this);
137
138   // call the python Startup callbacks
139   EventData data(_number, 0, EventAction::Startup, 0);
140   openbox->bindings()->fireEvent(&data);
141 }
142
143
144 Screen::~Screen()
145 {
146   if (! _managed) return;
147
148   XSelectInput(**otk::display, _info->rootWindow(), NoEventMask);
149   
150   // unmanage all windows
151   while (!clients.empty())
152     unmanageWindow(clients.front());
153
154   // call the python Shutdown callbacks
155   EventData data(_number, 0, EventAction::Shutdown, 0);
156   openbox->bindings()->fireEvent(&data);
157
158   XDestroyWindow(**otk::display, _focuswindow);
159   XDestroyWindow(**otk::display, _supportwindow);
160 }
161
162
163 void Screen::manageExisting()
164 {
165   unsigned int i, j, nchild;
166   Window r, p, *children;
167   XQueryTree(**otk::display, _info->rootWindow(), &r, &p,
168              &children, &nchild);
169
170   // preen the window list of all icon windows... for better dockapp support
171   for (i = 0; i < nchild; i++) {
172     if (children[i] == None) continue;
173
174     XWMHints *wmhints = XGetWMHints(**otk::display,
175                                     children[i]);
176
177     if (wmhints) {
178       if ((wmhints->flags & IconWindowHint) &&
179           (wmhints->icon_window != children[i])) {
180         for (j = 0; j < nchild; j++) {
181           if (children[j] == wmhints->icon_window) {
182             children[j] = None;
183             break;
184           }
185         }
186       }
187
188       XFree(wmhints);
189     }
190   }
191
192   // manage shown windows
193   for (i = 0; i < nchild; ++i) {
194     if (children[i] == None)
195       continue;
196
197     XWindowAttributes attrib;
198     if (XGetWindowAttributes(**otk::display, children[i], &attrib)) {
199       if (attrib.override_redirect) continue;
200
201       if (attrib.map_state != IsUnmapped) {
202         manageWindow(children[i]);
203       }
204     }
205   }
206
207   XFree(children);
208 }
209
210
211 void Screen::updateStrut()
212 {
213   _strut.left = _strut.right = _strut.top = _strut.bottom = 0;
214
215   ClientList::iterator it, end = clients.end();
216   for (it = clients.begin(); it != end; ++it) {
217     const otk::Strut &s = (*it)->strut();
218     _strut.left = std::max(_strut.left, s.left);
219     _strut.right = std::max(_strut.right, s.right);
220     _strut.top = std::max(_strut.top, s.top);
221     _strut.bottom = std::max(_strut.bottom, s.bottom);
222   }
223   calcArea();
224 }
225
226
227 void Screen::calcArea()
228 {
229   otk::Rect old_area = _area;
230
231 /*
232 #ifdef    XINERAMA
233   // reset to the full areas
234   if (isXineramaActive())
235     xineramaUsableArea = getXineramaAreas();
236 #endif // XINERAMA
237 */
238   
239   _area = otk::Rect(_strut.left, _strut.top,
240                     _info->size().width() - (_strut.left + _strut.right),
241                     _info->size().height() - (_strut.top + _strut.bottom));
242
243 /*
244 #ifdef    XINERAMA
245   if (isXineramaActive()) {
246     // keep each of the ximerama-defined areas inside the strut
247     RectList::iterator xit, xend = xineramaUsableArea.end();
248     for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
249       if (xit->x() < usableArea.x()) {
250         xit->setX(usableArea.x());
251         xit->setWidth(xit->width() - usableArea.x());
252       }
253       if (xit->y() < usableArea.y()) {
254         xit->setY(usableArea.y());
255         xit->setHeight(xit->height() - usableArea.y());
256       }
257       if (xit->x() + xit->width() > usableArea.width())
258         xit->setWidth(usableArea.width() - xit->x());
259       if (xit->y() + xit->height() > usableArea.height())
260         xit->setHeight(usableArea.height() - xit->y());
261     }
262   }
263 #endif // XINERAMA
264 */
265  
266   if (old_area != _area) {
267     // the area has changed, adjust all the maximized windows
268     ClientList::iterator it, end = clients.end();
269     for (it = clients.begin(); it != end; ++it)
270       (*it)->remaximize();
271   }
272
273   changeWorkArea();
274 }
275
276
277 void Screen::changeSupportedAtoms()
278 {
279   // create the netwm support window
280   _supportwindow = XCreateSimpleWindow(**otk::display,
281                                        _info->rootWindow(),
282                                        0, 0, 1, 1, 0, 0, 0);
283
284   // set supporting window
285   otk::Property::set(_info->rootWindow(),
286                      otk::Property::atoms.net_supporting_wm_check,
287                      otk::Property::atoms.window, _supportwindow);
288
289   //set properties on the supporting window
290   otk::Property::set(_supportwindow, otk::Property::atoms.net_wm_name,
291                      otk::Property::utf8, "Openbox");
292   otk::Property::set(_supportwindow,
293                      otk::Property::atoms.net_supporting_wm_check,
294                      otk::Property::atoms.window, _supportwindow);
295
296   
297   Atom supported[] = {
298     otk::Property::atoms.net_current_desktop,
299     otk::Property::atoms.net_number_of_desktops,
300     otk::Property::atoms.net_desktop_geometry,
301     otk::Property::atoms.net_desktop_viewport,
302     otk::Property::atoms.net_active_window,
303     otk::Property::atoms.net_workarea,
304     otk::Property::atoms.net_client_list,
305     otk::Property::atoms.net_client_list_stacking,
306     otk::Property::atoms.net_desktop_names,
307     otk::Property::atoms.net_close_window,
308     otk::Property::atoms.net_wm_name,
309     otk::Property::atoms.net_wm_visible_name,
310     otk::Property::atoms.net_wm_icon_name,
311     otk::Property::atoms.net_wm_visible_icon_name,
312 /*
313     otk::Property::atoms.net_wm_desktop,
314 */
315     otk::Property::atoms.net_wm_strut,
316     otk::Property::atoms.net_wm_window_type,
317     otk::Property::atoms.net_wm_window_type_desktop,
318     otk::Property::atoms.net_wm_window_type_dock,
319     otk::Property::atoms.net_wm_window_type_toolbar,
320     otk::Property::atoms.net_wm_window_type_menu,
321     otk::Property::atoms.net_wm_window_type_utility,
322     otk::Property::atoms.net_wm_window_type_splash,
323     otk::Property::atoms.net_wm_window_type_dialog,
324     otk::Property::atoms.net_wm_window_type_normal,
325 /*
326     otk::Property::atoms.net_wm_moveresize,
327     otk::Property::atoms.net_wm_moveresize_size_topleft,
328     otk::Property::atoms.net_wm_moveresize_size_topright,
329     otk::Property::atoms.net_wm_moveresize_size_bottomleft,
330     otk::Property::atoms.net_wm_moveresize_size_bottomright,
331     otk::Property::atoms.net_wm_moveresize_move,
332 */
333     otk::Property::atoms.net_wm_allowed_actions,
334     otk::Property::atoms.net_wm_action_move,
335     otk::Property::atoms.net_wm_action_resize,
336     otk::Property::atoms.net_wm_action_minimize,
337     otk::Property::atoms.net_wm_action_shade,
338 /*    otk::Property::atoms.net_wm_action_stick,*/
339     otk::Property::atoms.net_wm_action_maximize_horz,
340     otk::Property::atoms.net_wm_action_maximize_vert,
341     otk::Property::atoms.net_wm_action_fullscreen,
342     otk::Property::atoms.net_wm_action_change_desktop,
343     otk::Property::atoms.net_wm_action_close,
344
345     otk::Property::atoms.net_wm_state,
346     otk::Property::atoms.net_wm_state_modal,
347     otk::Property::atoms.net_wm_state_maximized_vert,
348     otk::Property::atoms.net_wm_state_maximized_horz,
349     otk::Property::atoms.net_wm_state_shaded,
350     otk::Property::atoms.net_wm_state_skip_taskbar,
351     otk::Property::atoms.net_wm_state_skip_pager,
352     otk::Property::atoms.net_wm_state_hidden,
353     otk::Property::atoms.net_wm_state_fullscreen,
354     otk::Property::atoms.net_wm_state_above,
355     otk::Property::atoms.net_wm_state_below,
356   };
357   const int num_supported = sizeof(supported)/sizeof(Atom);
358
359   otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_supported,
360                      otk::Property::atoms.atom, supported, num_supported);
361 }
362
363
364 void Screen::changeClientList()
365 {
366   Window *windows;
367   unsigned int size = clients.size();
368
369   // create an array of the window ids
370   if (size > 0) {
371     Window *win_it;
372     
373     windows = new Window[size];
374     win_it = windows;
375     ClientList::const_iterator it = clients.begin();
376     const ClientList::const_iterator end = clients.end();
377     for (; it != end; ++it, ++win_it)
378       *win_it = (*it)->window();
379   } else
380     windows = (Window*) 0;
381
382   otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_client_list,
383                      otk::Property::atoms.window, windows, size);
384
385   if (size)
386     delete [] windows;
387
388   changeStackingList();
389 }
390
391
392 void Screen::changeStackingList()
393 {
394   Window *windows;
395   unsigned int size = _stacking.size();
396
397   assert(size == clients.size()); // just making sure.. :)
398
399   
400   // create an array of the window ids (from bottom to top, reverse order!)
401   if (size > 0) {
402     Window *win_it;
403     
404     windows = new Window[size];
405     win_it = windows;
406     ClientList::const_reverse_iterator it = _stacking.rbegin();
407     const ClientList::const_reverse_iterator end = _stacking.rend();
408     for (; it != end; ++it, ++win_it)
409       *win_it = (*it)->window();
410   } else
411     windows = (Window*) 0;
412
413   otk::Property::set(_info->rootWindow(),
414                      otk::Property::atoms.net_client_list_stacking,
415                      otk::Property::atoms.window, windows, size);
416
417   if (size)
418     delete [] windows;
419 }
420
421
422 void Screen::changeWorkArea() {
423   unsigned long *dims = new unsigned long[4 * _num_desktops];
424   for (long i = 0; i < _num_desktops; ++i) {
425     dims[(i * 4) + 0] = _area.x();
426     dims[(i * 4) + 1] = _area.y();
427     dims[(i * 4) + 2] = _area.width();
428     dims[(i * 4) + 3] = _area.height();
429   }
430   otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_workarea,
431                      otk::Property::atoms.cardinal, dims, 4 * _num_desktops);
432   delete [] dims;
433 }
434
435
436 void Screen::manageWindow(Window window)
437 {
438   Client *client = 0;
439   XWMHints *wmhint;
440   XSetWindowAttributes attrib_set;
441   XEvent e;
442   XWindowAttributes attrib;
443
444   otk::display->grab();
445
446   // check if it has already been unmapped by the time we started mapping
447   // the grab does a sync so we don't have to here
448   if (XCheckTypedWindowEvent(**otk::display, window, DestroyNotify, &e) ||
449       XCheckTypedWindowEvent(**otk::display, window, UnmapNotify, &e)) {
450     XPutBackEvent(**otk::display, &e);
451     
452     otk::display->ungrab();
453     return; // don't manage it
454   }
455   
456   if (!XGetWindowAttributes(**otk::display, window, &attrib) ||
457       attrib.override_redirect) {
458     otk::display->ungrab();
459     return; // don't manage it
460   }
461   
462   // is the window a docking app
463   if ((wmhint = XGetWMHints(**otk::display, window))) {
464     if ((wmhint->flags & StateHint) &&
465         wmhint->initial_state == WithdrawnState) {
466       //slit->addClient(w); // XXX: make dock apps work!
467
468       otk::display->ungrab();
469       XFree(wmhint);
470       return;
471     }
472     XFree(wmhint);
473   }
474
475   // choose the events we want to receive on the CLIENT window
476   attrib_set.event_mask = Client::event_mask;
477   attrib_set.do_not_propagate_mask = Client::no_propagate_mask;
478   XChangeWindowAttributes(**otk::display, window,
479                           CWEventMask|CWDontPropagate, &attrib_set);
480
481   // create the Client class, which gets all of the hints on the window
482   client = new Client(_number, window);
483   // register for events
484   openbox->registerHandler(window, client);
485   // add to the wm's map
486   openbox->addClient(window, client);
487
488   // we dont want a border on the client
489   client->toggleClientBorder(false);
490   
491   // specify that if we exit, the window should not be destroyed and should be
492   // reparented back to root automatically
493   XChangeSaveSet(**otk::display, window, SetModeInsert);
494
495   // create the decoration frame for the client window
496   client->frame = new Frame(client);
497   // register the plate for events (map req's)
498   // this involves removing itself from the handler list first, since it is
499   // auto added to the list, being a widget. we won't get any events on the
500   // plate except for events for the client (SubstructureRedirectMask)
501   openbox->clearHandler(client->frame->plate());
502   openbox->registerHandler(client->frame->plate(), client);
503
504   // add to the wm's map
505   Window *w = client->frame->allWindows();
506   for (unsigned int i = 0; w[i]; ++i)
507     openbox->addClient(w[i], client);
508   delete [] w;
509
510   // reparent the client to the frame
511   client->frame->grabClient();
512
513   if (openbox->state() != Openbox::State_Starting) {
514     // position the window intelligenty .. hopefully :)
515     // call the python PLACEWINDOW binding
516     EventData data(_number, client, EventAction::PlaceWindow, 0);
517     openbox->bindings()->fireEvent(&data);
518   }
519
520   EventData ddata(_number, client, EventAction::DisplayingWindow, 0);
521   openbox->bindings()->fireEvent(&ddata);
522
523   // if on the current desktop.. (or all desktops)
524   if (client->desktop() == _desktop ||
525       client->desktop() == (signed)0xffffffff) {
526     client->frame->show();
527   }
528
529   client->applyStartupState();
530
531   otk::display->ungrab();
532
533   // add to the screen's list
534   clients.push_back(client);
535   // once the client is in the list, update our strut to include the new
536   // client's (it is good that this happens after window placement!)
537   updateStrut();
538   // this puts into the stacking order, then raises it
539   _stacking.push_back(client);
540   raiseWindow(client);
541   // update the root properties
542   changeClientList();
543
544   openbox->bindings()->grabButtons(true, client);
545
546   EventData ndata(_number, client, EventAction::NewWindow, 0);
547   openbox->bindings()->fireEvent(&ndata);
548
549 #ifdef DEBUG
550   printf("Managed window 0x%lx frame 0x%lx\n",
551          window, client->frame->window());
552 #endif
553 }
554
555
556 void Screen::unmanageWindow(Client *client)
557 {
558   Frame *frame = client->frame;
559
560   // call the python CLOSEWINDOW binding 
561   EventData data(_number, client, EventAction::CloseWindow, 0);
562   openbox->bindings()->fireEvent(&data);
563
564   openbox->bindings()->grabButtons(false, client);
565
566   // remove from the wm's map
567   openbox->removeClient(client->window());
568   Window *w = frame->allWindows();
569   for (unsigned int i = 0; w[i]; ++i)
570     openbox->addClient(w[i], client);
571   delete [] w;
572   // unregister for handling events
573   openbox->clearHandler(client->window());
574   
575   // remove the window from our save set
576   XChangeSaveSet(**otk::display, client->window(), SetModeDelete);
577
578   // we dont want events no more
579   XSelectInput(**otk::display, client->window(), NoEventMask);
580
581   frame->hide();
582
583   // give the client its border back
584   client->toggleClientBorder(true);
585
586   // reparent the window out of the frame
587   frame->releaseClient();
588
589 #ifdef DEBUG
590   Window framewin = client->frame->window();
591 #endif
592   delete client->frame;
593   client->frame = 0;
594
595   // remove from the stacking order
596   _stacking.remove(client);
597
598   // remove from the screen's list
599   clients.remove(client);
600
601   // once the client is out of the list, update our strut to remove it's
602   // influence
603   updateStrut();
604
605   // unset modal before dropping our focus
606   client->setModal(false);
607   
608   // unfocus the client (calls the focus callbacks)
609   client->unfocus();
610
611 #ifdef DEBUG
612   printf("Unmanaged window 0x%lx frame 0x%lx\n", client->window(), framewin);
613 #endif
614   
615   delete client;
616
617   // update the root properties
618   changeClientList();
619 }
620
621 void Screen::lowerWindow(Client *client)
622 {
623   Window wins[2];  // only ever restack 2 windows.
624
625   assert(!_stacking.empty()); // this would be bad
626
627   ClientList::iterator it = --_stacking.end();
628   const ClientList::iterator end = _stacking.begin();
629
630   if (client->modal() && client->transientFor()) {
631     // don't let a modal window lower below its transient_for
632     it = std::find(_stacking.begin(), _stacking.end(), client->transientFor());
633     assert(it != _stacking.end());
634
635     wins[0] = (it == _stacking.begin() ? _focuswindow :
636                ((*(--ClientList::const_iterator(it)))->frame->window()));
637     wins[1] = client->frame->window();
638     if (wins[0] == wins[1]) return; // already right above the window
639
640     _stacking.remove(client);
641     _stacking.insert(it, client);
642   } else {
643     for (; it != end && (*it)->layer() < client->layer(); --it);
644     if (*it == client) return;          // already the bottom, return
645
646     wins[0] = (*it)->frame->window();
647     wins[1] = client->frame->window();
648
649     _stacking.remove(client);
650     _stacking.insert(++it, client);
651   }
652
653   XRestackWindows(**otk::display, wins, 2);
654   changeStackingList();
655 }
656
657 void Screen::raiseWindow(Client *client)
658 {
659   Window wins[2];  // only ever restack 2 windows.
660
661   assert(!_stacking.empty()); // this would be bad
662
663   // remove the client before looking so we can't run into ourselves
664   _stacking.remove(client);
665   
666   ClientList::iterator it = _stacking.begin();
667   const ClientList::iterator end = _stacking.end();
668
669   // the stacking list is from highest to lowest
670   for (; it != end && (*it)->layer() > client->layer(); ++it);
671
672   /*
673     if our new position is the top, we want to stack under the _focuswindow
674     otherwise, we want to stack under the previous window in the stack.
675   */
676   wins[0] = (it == _stacking.begin() ? _focuswindow :
677              ((*(--ClientList::const_iterator(it)))->frame->window()));
678   wins[1] = client->frame->window();
679
680   _stacking.insert(it, client);
681
682   XRestackWindows(**otk::display, wins, 2);
683
684   // if the window has a modal child, then raise it after us to put it on top
685   if (client->modalChild())
686     raiseWindow(client->modalChild());
687   else
688     changeStackingList(); // no need to do this twice!
689 }
690
691 void Screen::changeDesktop(long desktop)
692 {
693   if (!(desktop >= 0 && desktop < _num_desktops)) return;
694
695   printf("Moving to desktop %ld\n", desktop);
696   
697   long old = _desktop;
698   
699   _desktop = desktop;
700   otk::Property::set(_info->rootWindow(),
701                      otk::Property::atoms.net_current_desktop,
702                      otk::Property::atoms.cardinal, _desktop);
703
704   if (old == _desktop) return;
705
706   ClientList::iterator it, end = clients.end();
707   for (it = clients.begin(); it != end; ++it) {
708     if ((*it)->desktop() == old) {
709       (*it)->frame->hide();
710     } else if ((*it)->desktop() == _desktop) {
711       (*it)->frame->show();
712     }
713   }
714
715   // force the callbacks to fire
716   if (!openbox->focusedClient())
717     openbox->setFocusedClient(0);
718 }
719
720 void Screen::changeNumDesktops(long num)
721 {
722   assert(num > 0);
723   
724   if (!(num > 0)) return;
725
726   // move windows on desktops that will no longer exist!
727   ClientList::iterator it, end = clients.end();
728   for (it = clients.begin(); it != end; ++it) {
729     int d = (*it)->desktop();
730     if (d >= num && !(d == (signed) 0xffffffff ||
731                       d == Client::ICONIC_DESKTOP)) {
732       XEvent ce;
733       ce.xclient.type = ClientMessage;
734       ce.xclient.message_type = otk::Property::atoms.net_wm_desktop;
735       ce.xclient.display = **otk::display;
736       ce.xclient.window = (*it)->window();
737       ce.xclient.format = 32;
738       ce.xclient.data.l[0] = num - 1;
739       XSendEvent(**otk::display, _info->rootWindow(), False,
740                  SubstructureNotifyMask | SubstructureRedirectMask, &ce);
741     }
742   }
743
744   _num_desktops = num;
745   otk::Property::set(_info->rootWindow(),
746                      otk::Property::atoms.net_number_of_desktops,
747                      otk::Property::atoms.cardinal, _num_desktops);
748
749   // set the viewport hint
750   unsigned long *viewport = new unsigned long[_num_desktops * 2];
751   memset(viewport, 0, sizeof(unsigned long) * _num_desktops * 2);
752   otk::Property::set(_info->rootWindow(),
753                      otk::Property::atoms.net_desktop_viewport,
754                      otk::Property::atoms.cardinal,
755                      viewport, _num_desktops * 2);
756   delete [] viewport;
757
758   // update the work area hint
759   changeWorkArea();
760
761   // change our desktop if we're on one that no longer exists!
762   if (_desktop >= num)
763     changeDesktop(num - 1);
764 }
765
766
767 void Screen::updateDesktopNames()
768 {
769   unsigned long num = (unsigned) -1;
770   
771   if (!otk::Property::get(_info->rootWindow(),
772                           otk::Property::atoms.net_desktop_names,
773                           otk::Property::utf8, &num, &_desktop_names))
774     _desktop_names.clear();
775   while ((long)_desktop_names.size() < _num_desktops)
776     _desktop_names.push_back("Unnamed");
777 }
778
779
780 void Screen::setDesktopName(long i, const otk::ustring &name)
781 {
782   assert(i >= 0);
783
784   if (i >= _num_desktops) return;
785
786   otk::Property::StringVect newnames = _desktop_names;
787   newnames[i] = name;
788   otk::Property::set(_info->rootWindow(),
789                      otk::Property::atoms.net_desktop_names,
790                      otk::Property::utf8, newnames);
791 }
792
793
794 void Screen::installColormap(bool install) const
795 {
796   if (install)
797     XInstallColormap(**otk::display, _info->colormap());
798   else
799     XUninstallColormap(**otk::display, _info->colormap());
800 }
801
802
803 void Screen::propertyHandler(const XPropertyEvent &e)
804 {
805   otk::EventHandler::propertyHandler(e);
806
807   // compress changes to a single property into a single change
808   XEvent ce;
809   while (XCheckTypedWindowEvent(**otk::display, _info->rootWindow(),
810                                 e.type, &ce)) {
811     // XXX: it would be nice to compress ALL changes to a property, not just
812     //      changes in a row without other props between.
813     if (ce.xproperty.atom != e.atom) {
814       XPutBackEvent(**otk::display, &ce);
815       break;
816     }
817   }
818
819   if (e.atom == otk::Property::atoms.net_desktop_names)
820     updateDesktopNames();
821 }
822
823
824 void Screen::clientMessageHandler(const XClientMessageEvent &e)
825 {
826   otk::EventHandler::clientMessageHandler(e);
827
828   if (e.format != 32) return;
829
830   if (e.message_type == otk::Property::atoms.net_current_desktop) {
831     changeDesktop(e.data.l[0]);
832   } else if (e.message_type == otk::Property::atoms.net_number_of_desktops) {
833     changeNumDesktops(e.data.l[0]);
834   }
835 }
836
837
838 void Screen::mapRequestHandler(const XMapRequestEvent &e)
839 {
840   otk::EventHandler::mapRequestHandler(e);
841
842 #ifdef    DEBUG
843   printf("MapRequest for 0x%lx\n", e.window);
844 #endif // DEBUG
845
846   Client *c = openbox->findClient(e.window);
847   if (c) {
848 #ifdef DEBUG
849     printf("DEBUG: MAP REQUEST CAUGHT IN SCREEN. IGNORED.\n");
850 #endif
851   } else
852     manageWindow(e.window);
853 }
854
855 }