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