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