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