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