]> icculus.org git repositories - dana/openbox.git/blob - src/screen.cc
keep track of struts for each desktop
[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
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     long desk = (*it)->desktop();
221     const otk::Strut &s = (*it)->strut();
222
223     if (desk == (signed) 0xffffffff)
224       for (unsigned int i = 0, e = _struts.size(); i < e; ++i)
225         apply(_struts[i], s);
226     else if ((unsigned)desk < _struts.size())
227       apply(_struts[desk], s);
228     else if (desk == Client::ICONIC_DESKTOP)
229       continue; // skip for the 'all desktops' strut
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 (long 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() == (signed) 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   // if on the current desktop.. (or all desktops)
541   if (client->desktop() == _desktop ||
542       client->desktop() == (signed)0xffffffff) {
543     client->frame->show();
544   }
545
546   client->applyStartupState();
547
548   otk::display->ungrab();
549
550   // add to the screen's list
551   clients.push_back(client);
552   // once the client is in the list, update our strut to include the new
553   // client's (it is good that this happens after window placement!)
554   updateStruts();
555   // this puts into the stacking order, then raises it
556   _stacking.push_back(client);
557   raiseWindow(client);
558   // update the root properties
559   changeClientList();
560
561   openbox->bindings()->grabButtons(true, client);
562
563   EventData ndata(_number, client, EventAction::NewWindow, 0);
564   openbox->bindings()->fireEvent(&ndata);
565
566 #ifdef DEBUG
567   printf("Managed window 0x%lx frame 0x%lx\n",
568          window, client->frame->window());
569 #endif
570 }
571
572
573 void Screen::unmanageWindow(Client *client)
574 {
575   Frame *frame = client->frame;
576
577   // call the python CLOSEWINDOW binding 
578   EventData data(_number, client, EventAction::CloseWindow, 0);
579   openbox->bindings()->fireEvent(&data);
580
581   openbox->bindings()->grabButtons(false, client);
582
583   // remove from the wm's map
584   openbox->removeClient(client->window());
585   Window *w = frame->allWindows();
586   for (unsigned int i = 0; w[i]; ++i)
587     openbox->addClient(w[i], client);
588   delete [] w;
589   // unregister for handling events
590   openbox->clearHandler(client->window());
591   
592   // remove the window from our save set
593   XChangeSaveSet(**otk::display, client->window(), SetModeDelete);
594
595   // we dont want events no more
596   XSelectInput(**otk::display, client->window(), NoEventMask);
597
598   frame->hide();
599
600   // give the client its border back
601   client->toggleClientBorder(true);
602
603   // reparent the window out of the frame
604   frame->releaseClient();
605
606 #ifdef DEBUG
607   Window framewin = client->frame->window();
608 #endif
609   delete client->frame;
610   client->frame = 0;
611
612   // remove from the stacking order
613   _stacking.remove(client);
614
615   // remove from the screen's list
616   clients.remove(client);
617
618   // once the client is out of the list, update our strut to remove it's
619   // influence
620   updateStruts();
621
622   // unset modal before dropping our focus
623   client->_modal = false;
624   
625   // unfocus the client (calls the focus callbacks)
626   client->unfocus();
627
628 #ifdef DEBUG
629   printf("Unmanaged window 0x%lx frame 0x%lx\n", client->window(), framewin);
630 #endif
631   
632   delete client;
633
634   // update the root properties
635   changeClientList();
636 }
637
638 void Screen::lowerWindow(Client *client)
639 {
640   Window wins[2];  // only ever restack 2 windows.
641
642   assert(!_stacking.empty()); // this would be bad
643
644   ClientList::iterator it = --_stacking.end();
645   const ClientList::iterator end = _stacking.begin();
646
647   if (client->modal() && client->transientFor()) {
648     // don't let a modal window lower below its transient_for
649     it = std::find(_stacking.begin(), _stacking.end(), client->transientFor());
650     assert(it != _stacking.end());
651
652     wins[0] = (it == _stacking.begin() ? _focuswindow :
653                ((*(--ClientList::const_iterator(it)))->frame->window()));
654     wins[1] = client->frame->window();
655     if (wins[0] == wins[1]) return; // already right above the window
656
657     _stacking.remove(client);
658     _stacking.insert(it, client);
659   } else {
660     for (; it != end && (*it)->layer() < client->layer(); --it);
661     if (*it == client) return;          // already the bottom, return
662
663     wins[0] = (*it)->frame->window();
664     wins[1] = client->frame->window();
665
666     _stacking.remove(client);
667     _stacking.insert(++it, client);
668   }
669
670   XRestackWindows(**otk::display, wins, 2);
671   changeStackingList();
672 }
673
674 void Screen::raiseWindow(Client *client)
675 {
676   Window wins[2];  // only ever restack 2 windows.
677
678   assert(!_stacking.empty()); // this would be bad
679
680   Client *m = client->findModalChild();
681   // if we have a modal child, raise it instead, we'll go along tho later
682   if (m) raiseWindow(m);
683   
684   // remove the client before looking so we can't run into ourselves
685   _stacking.remove(client);
686   
687   ClientList::iterator it = _stacking.begin();
688   const ClientList::iterator end = _stacking.end();
689
690   // the stacking list is from highest to lowest
691 //  for (;it != end, ++it) {
692 //    if ((*it)->layer() <= client->layer() && m != *it) break;
693 //  }
694   for (; it != end && ((*it)->layer() > client->layer() || m == *it); ++it);
695
696   /*
697     if our new position is the top, we want to stack under the _focuswindow
698     otherwise, we want to stack under the previous window in the stack.
699   */
700   wins[0] = (it == _stacking.begin() ? _focuswindow :
701              ((*(--ClientList::const_iterator(it)))->frame->window()));
702   wins[1] = client->frame->window();
703
704   _stacking.insert(it, client);
705
706   XRestackWindows(**otk::display, wins, 2);
707
708   changeStackingList(); 
709 }
710
711 void Screen::changeDesktop(long desktop)
712 {
713   if (!(desktop >= 0 && desktop < _num_desktops)) return;
714
715   printf("Moving to desktop %ld\n", desktop);
716   
717   long old = _desktop;
718   
719   _desktop = desktop;
720   otk::Property::set(_info->rootWindow(),
721                      otk::Property::atoms.net_current_desktop,
722                      otk::Property::atoms.cardinal, _desktop);
723
724   if (old == _desktop) return;
725
726   ClientList::iterator it, end = clients.end();
727   for (it = clients.begin(); it != end; ++it) {
728     if ((*it)->desktop() == old) {
729       (*it)->frame->hide();
730     } else if ((*it)->desktop() == _desktop) {
731       (*it)->frame->show();
732     }
733   }
734
735   // force the callbacks to fire
736   if (!openbox->focusedClient())
737     openbox->setFocusedClient(0);
738 }
739
740 void Screen::changeNumDesktops(long num)
741 {
742   assert(num > 0);
743   
744   if (!(num > 0)) return;
745
746   // move windows on desktops that will no longer exist!
747   ClientList::iterator it, end = clients.end();
748   for (it = clients.begin(); it != end; ++it) {
749     int d = (*it)->desktop();
750     if (d >= num && !(d == (signed) 0xffffffff ||
751                       d == Client::ICONIC_DESKTOP)) {
752       XEvent ce;
753       ce.xclient.type = ClientMessage;
754       ce.xclient.message_type = otk::Property::atoms.net_wm_desktop;
755       ce.xclient.display = **otk::display;
756       ce.xclient.window = (*it)->window();
757       ce.xclient.format = 32;
758       ce.xclient.data.l[0] = num - 1;
759       XSendEvent(**otk::display, _info->rootWindow(), False,
760                  SubstructureNotifyMask | SubstructureRedirectMask, &ce);
761     }
762   }
763
764   _num_desktops = num;
765   otk::Property::set(_info->rootWindow(),
766                      otk::Property::atoms.net_number_of_desktops,
767                      otk::Property::atoms.cardinal, _num_desktops);
768
769   // set the viewport hint
770   unsigned long *viewport = new unsigned long[_num_desktops * 2];
771   memset(viewport, 0, sizeof(unsigned long) * _num_desktops * 2);
772   otk::Property::set(_info->rootWindow(),
773                      otk::Property::atoms.net_desktop_viewport,
774                      otk::Property::atoms.cardinal,
775                      viewport, _num_desktops * 2);
776   delete [] viewport;
777
778   // change our struts/area to match
779   _area.resize(_num_desktops + 1);
780   _struts.resize(_num_desktops + 1);
781   updateStruts();
782
783   // change our desktop if we're on one that no longer exists!
784   if (_desktop >= _num_desktops)
785     changeDesktop(_num_desktops - 1);
786 }
787
788
789 void Screen::updateDesktopNames()
790 {
791   unsigned long num = (unsigned) -1;
792   
793   if (!otk::Property::get(_info->rootWindow(),
794                           otk::Property::atoms.net_desktop_names,
795                           otk::Property::utf8, &num, &_desktop_names))
796     _desktop_names.clear();
797   while ((long)_desktop_names.size() < _num_desktops)
798     _desktop_names.push_back("Unnamed");
799 }
800
801
802 void Screen::setDesktopName(long i, const otk::ustring &name)
803 {
804   assert(i >= 0);
805
806   if (i >= _num_desktops) return;
807
808   otk::Property::StringVect newnames = _desktop_names;
809   newnames[i] = name;
810   otk::Property::set(_info->rootWindow(),
811                      otk::Property::atoms.net_desktop_names,
812                      otk::Property::utf8, newnames);
813 }
814
815
816 const otk::Rect& Screen::area(long desktop) const {
817   assert(desktop >= 0 || desktop == (signed) 0xffffffff);
818   assert(desktop < _num_desktops || desktop == (signed) 0xffffffff);
819   if (desktop >= 0 && desktop < _num_desktops)
820     return _area[desktop];
821   else
822     return _area[_num_desktops];
823 }
824
825 void Screen::installColormap(bool install) const
826 {
827   if (install)
828     XInstallColormap(**otk::display, _info->colormap());
829   else
830     XUninstallColormap(**otk::display, _info->colormap());
831 }
832
833
834 void Screen::propertyHandler(const XPropertyEvent &e)
835 {
836   otk::EventHandler::propertyHandler(e);
837
838   // compress changes to a single property into a single change
839   XEvent ce;
840   while (XCheckTypedWindowEvent(**otk::display, _info->rootWindow(),
841                                 e.type, &ce)) {
842     // XXX: it would be nice to compress ALL changes to a property, not just
843     //      changes in a row without other props between.
844     if (ce.xproperty.atom != e.atom) {
845       XPutBackEvent(**otk::display, &ce);
846       break;
847     }
848   }
849
850   if (e.atom == otk::Property::atoms.net_desktop_names)
851     updateDesktopNames();
852 }
853
854
855 void Screen::clientMessageHandler(const XClientMessageEvent &e)
856 {
857   otk::EventHandler::clientMessageHandler(e);
858
859   if (e.format != 32) return;
860
861   if (e.message_type == otk::Property::atoms.net_current_desktop) {
862     changeDesktop(e.data.l[0]);
863   } else if (e.message_type == otk::Property::atoms.net_number_of_desktops) {
864     changeNumDesktops(e.data.l[0]);
865   }
866 }
867
868
869 void Screen::mapRequestHandler(const XMapRequestEvent &e)
870 {
871   otk::EventHandler::mapRequestHandler(e);
872
873 #ifdef    DEBUG
874   printf("MapRequest for 0x%lx\n", e.window);
875 #endif // DEBUG
876
877   Client *c = openbox->findClient(e.window);
878   if (c) {
879 #ifdef DEBUG
880     printf("DEBUG: MAP REQUEST CAUGHT IN SCREEN. IGNORED.\n");
881 #endif
882   } else
883     manageWindow(e.window);
884 }
885
886 }