]> icculus.org git repositories - dana/openbox.git/blob - src/screen.cc
only show prints for debug builds
[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     _style(screen, "")
53 {
54   assert(screen >= 0); assert(screen < ScreenCount(**otk::display));
55   _info = otk::display->screenInfo(screen);
56
57   ::running = false;
58   XErrorHandler old = XSetErrorHandler(::anotherWMRunning);
59   XSelectInput(**otk::display, _info->rootWindow(),
60                Screen::event_mask);
61   XSync(**otk::display, false);
62   XSetErrorHandler(old);
63
64   _managed = !::running;
65   if (! _managed) return; // was unable to manage the screen
66
67 #ifdef DEBUG
68   printf(_("Managing screen %d: visual 0x%lx, depth %d\n"),
69          _number, XVisualIDFromVisual(_info->visual()), _info->depth());
70 #endif
71
72   otk::Property::set(_info->rootWindow(), otk::Property::atoms.openbox_pid,
73                      otk::Property::atoms.cardinal, (unsigned long) getpid());
74
75   // set the mouse cursor for the root window (the default cursor)
76   XDefineCursor(**otk::display, _info->rootWindow(),
77                 openbox->cursors().session);
78
79   // XXX: initialize the screen's style
80   /*
81   otk::ustring stylepath;
82   python_get_string("THEME", &stylepath);
83   otk::Configuration sconfig(false);
84   sconfig.setFile(otk::expandTilde(stylepath.c_str()));
85   if (!sconfig.load()) {
86     sconfig.setFile(otk::expandTilde(DEFAULTSTYLE));
87     if (!sconfig.load()) {
88       printf(_("Unable to load default style: %s. Aborting.\n"), DEFAULTSTYLE);
89       ::exit(1);
90     }
91   }
92   _style.load(sconfig);
93   */
94   otk::display->renderControl(_number)->drawRoot(*_style.rootColor());
95
96   // set up notification of netwm support
97   changeSupportedAtoms();
98
99   // Set the netwm properties for geometry
100   unsigned long geometry[] = { _info->width(),
101                                _info->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.setRect(_strut.left, _strut.top,
241                 _info->width() - (_strut.left + _strut.right),
242                 _info->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, &_style);
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   openbox->addClient(client->frame->window(), client);
507   openbox->addClient(client->frame->plate(), client);
508   openbox->addClient(client->frame->titlebar(), client);
509   openbox->addClient(client->frame->label(), client);
510   openbox->addClient(client->frame->button_max(), client);
511   openbox->addClient(client->frame->button_iconify(), client);
512   openbox->addClient(client->frame->button_alldesk(), client);
513   openbox->addClient(client->frame->button_close(), client);
514   openbox->addClient(client->frame->handle(), client);
515   openbox->addClient(client->frame->grip_left(), client);
516   openbox->addClient(client->frame->grip_right(), client);
517
518   // reparent the client to the frame
519   client->frame->grabClient();
520
521   if (openbox->state() != Openbox::State_Starting) {
522     // position the window intelligenty .. hopefully :)
523     // call the python PLACEWINDOW binding
524     EventData data(_number, client, EventAction::PlaceWindow, 0);
525     openbox->bindings()->fireEvent(&data);
526   }
527
528   EventData ddata(_number, client, EventAction::DisplayingWindow, 0);
529   openbox->bindings()->fireEvent(&ddata);
530
531   // if on the current desktop.. (or all desktops)
532   if (client->desktop() == _desktop ||
533       client->desktop() == (signed)0xffffffff) {
534     client->frame->show();
535   }
536
537   client->applyStartupState();
538
539   otk::display->ungrab();
540
541   // add to the screen's list
542   clients.push_back(client);
543   // once the client is in the list, update our strut to include the new
544   // client's (it is good that this happens after window placement!)
545   updateStrut();
546   // this puts into the stacking order, then raises it
547   _stacking.push_back(client);
548   raiseWindow(client);
549   // update the root properties
550   changeClientList();
551
552   openbox->bindings()->grabButtons(true, client);
553
554   EventData ndata(_number, client, EventAction::NewWindow, 0);
555   openbox->bindings()->fireEvent(&ndata);
556
557 #ifdef DEBUG
558   printf("Managed window 0x%lx frame 0x%lx\n",
559          window, client->frame->window());
560 #endif
561 }
562
563
564 void Screen::unmanageWindow(Client *client)
565 {
566   Frame *frame = client->frame;
567
568   // call the python CLOSEWINDOW binding 
569   EventData data(_number, client, EventAction::CloseWindow, 0);
570   openbox->bindings()->fireEvent(&data);
571
572   openbox->bindings()->grabButtons(false, client);
573
574   // remove from the wm's map
575   openbox->removeClient(client->window());
576   openbox->removeClient(frame->window());
577   openbox->removeClient(frame->plate());
578   openbox->removeClient(frame->titlebar());
579   openbox->removeClient(frame->label());
580   openbox->removeClient(frame->button_max());
581   openbox->removeClient(frame->button_iconify());
582   openbox->removeClient(frame->button_alldesk());
583   openbox->removeClient(frame->button_close());
584   openbox->removeClient(frame->handle());
585   openbox->removeClient(frame->grip_left());
586   openbox->removeClient(frame->grip_right());
587   // unregister for handling events
588   openbox->clearHandler(client->window());
589   
590   // remove the window from our save set
591   XChangeSaveSet(**otk::display, client->window(), SetModeDelete);
592
593   // we dont want events no more
594   XSelectInput(**otk::display, client->window(), NoEventMask);
595
596   frame->hide();
597
598   // give the client its border back
599   client->toggleClientBorder(true);
600
601   // reparent the window out of the frame
602   frame->releaseClient();
603
604 #ifdef DEBUG
605   Window framewin = client->frame->window();
606 #endif
607   delete client->frame;
608   client->frame = 0;
609
610   // remove from the stacking order
611   _stacking.remove(client);
612
613   // remove from the screen's list
614   clients.remove(client);
615
616   // once the client is out of the list, update our strut to remove it's
617   // influence
618   updateStrut();
619
620   // unset modal before dropping our focus
621   client->setModal(false);
622   
623   // unfocus the client (calls the focus callbacks)
624   client->unfocus();
625
626 #ifdef DEBUG
627   printf("Unmanaged window 0x%lx frame 0x%lx\n", client->window(), framewin);
628 #endif
629   
630   delete client;
631
632   // update the root properties
633   changeClientList();
634 }
635
636 void Screen::lowerWindow(Client *client)
637 {
638   Window wins[2];  // only ever restack 2 windows.
639
640   assert(!_stacking.empty()); // this would be bad
641
642   ClientList::iterator it = --_stacking.end();
643   const ClientList::iterator end = _stacking.begin();
644
645   if (client->modal() && client->transientFor()) {
646     // don't let a modal window lower below its transient_for
647     it = std::find(_stacking.begin(), _stacking.end(), client->transientFor());
648     assert(it != _stacking.end());
649
650     wins[0] = (it == _stacking.begin() ? _focuswindow :
651                ((*(--ClientList::const_iterator(it)))->frame->window()));
652     wins[1] = client->frame->window();
653     if (wins[0] == wins[1]) return; // already right above the window
654
655     _stacking.remove(client);
656     _stacking.insert(it, client);
657   } else {
658     for (; it != end && (*it)->layer() < client->layer(); --it);
659     if (*it == client) return;          // already the bottom, return
660
661     wins[0] = (*it)->frame->window();
662     wins[1] = client->frame->window();
663
664     _stacking.remove(client);
665     _stacking.insert(++it, client);
666   }
667
668   XRestackWindows(**otk::display, wins, 2);
669   changeStackingList();
670 }
671
672 void Screen::raiseWindow(Client *client)
673 {
674   Window wins[2];  // only ever restack 2 windows.
675
676   assert(!_stacking.empty()); // this would be bad
677
678   // remove the client before looking so we can't run into ourselves
679   _stacking.remove(client);
680   
681   ClientList::iterator it = _stacking.begin();
682   const ClientList::iterator end = _stacking.end();
683
684   // the stacking list is from highest to lowest
685   for (; it != end && (*it)->layer() > client->layer(); ++it);
686
687   /*
688     if our new position is the top, we want to stack under the _focuswindow
689     otherwise, we want to stack under the previous window in the stack.
690   */
691   wins[0] = (it == _stacking.begin() ? _focuswindow :
692              ((*(--ClientList::const_iterator(it)))->frame->window()));
693   wins[1] = client->frame->window();
694
695   _stacking.insert(it, client);
696
697   XRestackWindows(**otk::display, wins, 2);
698
699   // if the window has a modal child, then raise it after us to put it on top
700   if (client->modalChild())
701     raiseWindow(client->modalChild());
702   else
703     changeStackingList(); // no need to do this twice!
704 }
705
706 void Screen::changeDesktop(long desktop)
707 {
708   if (!(desktop >= 0 && desktop < _num_desktops)) return;
709
710   printf("Moving to desktop %ld\n", desktop);
711   
712   long old = _desktop;
713   
714   _desktop = desktop;
715   otk::Property::set(_info->rootWindow(),
716                      otk::Property::atoms.net_current_desktop,
717                      otk::Property::atoms.cardinal, _desktop);
718
719   if (old == _desktop) return;
720
721   ClientList::iterator it, end = clients.end();
722   for (it = clients.begin(); it != end; ++it) {
723     if ((*it)->desktop() == old) {
724       (*it)->frame->hide();
725     } else if ((*it)->desktop() == _desktop) {
726       (*it)->frame->show();
727     }
728   }
729
730   // force the callbacks to fire
731   if (!openbox->focusedClient())
732     openbox->setFocusedClient(0);
733 }
734
735 void Screen::changeNumDesktops(long num)
736 {
737   assert(num > 0);
738   
739   if (!(num > 0)) return;
740
741   // move windows on desktops that will no longer exist!
742   ClientList::iterator it, end = clients.end();
743   for (it = clients.begin(); it != end; ++it) {
744     int d = (*it)->desktop();
745     if (d >= num && !(d == (signed) 0xffffffff ||
746                       d == Client::ICONIC_DESKTOP)) {
747       XEvent ce;
748       ce.xclient.type = ClientMessage;
749       ce.xclient.message_type = otk::Property::atoms.net_wm_desktop;
750       ce.xclient.display = **otk::display;
751       ce.xclient.window = (*it)->window();
752       ce.xclient.format = 32;
753       ce.xclient.data.l[0] = num - 1;
754       XSendEvent(**otk::display, _info->rootWindow(), False,
755                  SubstructureNotifyMask | SubstructureRedirectMask, &ce);
756     }
757   }
758
759   _num_desktops = num;
760   otk::Property::set(_info->rootWindow(),
761                      otk::Property::atoms.net_number_of_desktops,
762                      otk::Property::atoms.cardinal, _num_desktops);
763
764   // set the viewport hint
765   unsigned long *viewport = new unsigned long[_num_desktops * 2];
766   memset(viewport, 0, sizeof(unsigned long) * _num_desktops * 2);
767   otk::Property::set(_info->rootWindow(),
768                      otk::Property::atoms.net_desktop_viewport,
769                      otk::Property::atoms.cardinal,
770                      viewport, _num_desktops * 2);
771   delete [] viewport;
772
773   // update the work area hint
774   changeWorkArea();
775
776   // change our desktop if we're on one that no longer exists!
777   if (_desktop >= num)
778     changeDesktop(num - 1);
779 }
780
781
782 void Screen::updateDesktopNames()
783 {
784   unsigned long num = (unsigned) -1;
785   
786   if (!otk::Property::get(_info->rootWindow(),
787                           otk::Property::atoms.net_desktop_names,
788                           otk::Property::utf8, &num, &_desktop_names))
789     _desktop_names.clear();
790   while ((long)_desktop_names.size() < _num_desktops)
791     _desktop_names.push_back("Unnamed");
792 }
793
794
795 void Screen::setDesktopName(long i, const otk::ustring &name)
796 {
797   assert(i >= 0);
798
799   if (i >= _num_desktops) return;
800
801   otk::Property::StringVect newnames = _desktop_names;
802   newnames[i] = name;
803   otk::Property::set(_info->rootWindow(),
804                      otk::Property::atoms.net_desktop_names,
805                      otk::Property::utf8, newnames);
806 }
807
808
809 void Screen::installColormap(bool install) const
810 {
811   if (install)
812     XInstallColormap(**otk::display, _info->colormap());
813   else
814     XUninstallColormap(**otk::display, _info->colormap());
815 }
816
817
818 void Screen::propertyHandler(const XPropertyEvent &e)
819 {
820   otk::EventHandler::propertyHandler(e);
821
822   // compress changes to a single property into a single change
823   XEvent ce;
824   while (XCheckTypedWindowEvent(**otk::display, _info->rootWindow(),
825                                 e.type, &ce)) {
826     // XXX: it would be nice to compress ALL changes to a property, not just
827     //      changes in a row without other props between.
828     if (ce.xproperty.atom != e.atom) {
829       XPutBackEvent(**otk::display, &ce);
830       break;
831     }
832   }
833
834   if (e.atom == otk::Property::atoms.net_desktop_names)
835     updateDesktopNames();
836 }
837
838
839 void Screen::clientMessageHandler(const XClientMessageEvent &e)
840 {
841   otk::EventHandler::clientMessageHandler(e);
842
843   if (e.format != 32) return;
844
845   if (e.message_type == otk::Property::atoms.net_current_desktop) {
846     changeDesktop(e.data.l[0]);
847   } else if (e.message_type == otk::Property::atoms.net_number_of_desktops) {
848     changeNumDesktops(e.data.l[0]);
849   }
850 }
851
852
853 void Screen::mapRequestHandler(const XMapRequestEvent &e)
854 {
855   otk::EventHandler::mapRequestHandler(e);
856
857 #ifdef    DEBUG
858   printf("MapRequest for 0x%lx\n", e.window);
859 #endif // DEBUG
860
861   Client *c = openbox->findClient(e.window);
862   if (c) {
863 #ifdef DEBUG
864     printf("DEBUG: MAP REQUEST CAUGHT IN SCREEN. IGNORED.\n");
865 #endif
866   } else
867     manageWindow(e.window);
868 }
869
870 }