support pseudocolor and greyscale displays by, using solid colors instead of gradients
[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   otk::Strut old = _strut;  
212   _strut.left = _strut.right = _strut.top = _strut.bottom = 0;
213
214   Client::List::iterator it, end = clients.end();
215   for (it = clients.begin(); it != end; ++it) {
216     const otk::Strut &s = (*it)->strut();
217     _strut.left = std::max(_strut.left, s.left);
218     _strut.right = std::max(_strut.right, s.right);
219     _strut.top = std::max(_strut.top, s.top);
220     _strut.bottom = std::max(_strut.bottom, s.bottom);
221   }
222   calcArea();
223
224   if (!(old == _strut)) {
225     // the strut has changed, adjust all the maximized windows
226     for (it = clients.begin(); it != end; ++it)
227       (*it)->remaximize();
228   }
229 }
230
231
232 void Screen::calcArea()
233 {
234 //  otk::Rect old_area = _area;
235
236 /*
237 #ifdef    XINERAMA
238   // reset to the full areas
239   if (isXineramaActive())
240     xineramaUsableArea = getXineramaAreas();
241 #endif // XINERAMA
242 */
243   
244   _area.setRect(_strut.left, _strut.top,
245                 _info->width() - (_strut.left + _strut.right),
246                 _info->height() - (_strut.top + _strut.bottom));
247
248 /*
249 #ifdef    XINERAMA
250   if (isXineramaActive()) {
251     // keep each of the ximerama-defined areas inside the strut
252     RectList::iterator xit, xend = xineramaUsableArea.end();
253     for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
254       if (xit->x() < usableArea.x()) {
255         xit->setX(usableArea.x());
256         xit->setWidth(xit->width() - usableArea.x());
257       }
258       if (xit->y() < usableArea.y()) {
259         xit->setY(usableArea.y());
260         xit->setHeight(xit->height() - usableArea.y());
261       }
262       if (xit->x() + xit->width() > usableArea.width())
263         xit->setWidth(usableArea.width() - xit->x());
264       if (xit->y() + xit->height() > usableArea.height())
265         xit->setHeight(usableArea.height() - xit->y());
266     }
267   }
268 #endif // XINERAMA
269 */
270   
271   //if (old_area != _area)
272     // XXX: re-maximize windows
273
274   changeWorkArea();
275 }
276
277
278 void Screen::changeSupportedAtoms()
279 {
280   // create the netwm support window
281   _supportwindow = XCreateSimpleWindow(**otk::display,
282                                        _info->rootWindow(),
283                                        0, 0, 1, 1, 0, 0, 0);
284
285   // set supporting window
286   otk::Property::set(_info->rootWindow(),
287                      otk::Property::atoms.net_supporting_wm_check,
288                      otk::Property::atoms.window, _supportwindow);
289
290   //set properties on the supporting window
291   otk::Property::set(_supportwindow, otk::Property::atoms.net_wm_name,
292                      otk::Property::utf8, "Openbox");
293   otk::Property::set(_supportwindow,
294                      otk::Property::atoms.net_supporting_wm_check,
295                      otk::Property::atoms.window, _supportwindow);
296
297   
298   Atom supported[] = {
299     otk::Property::atoms.net_current_desktop,
300     otk::Property::atoms.net_number_of_desktops,
301     otk::Property::atoms.net_desktop_geometry,
302     otk::Property::atoms.net_desktop_viewport,
303     otk::Property::atoms.net_active_window,
304     otk::Property::atoms.net_workarea,
305     otk::Property::atoms.net_client_list,
306     otk::Property::atoms.net_client_list_stacking,
307     otk::Property::atoms.net_desktop_names,
308     otk::Property::atoms.net_close_window,
309     otk::Property::atoms.net_wm_name,
310     otk::Property::atoms.net_wm_visible_name,
311     otk::Property::atoms.net_wm_icon_name,
312     otk::Property::atoms.net_wm_visible_icon_name,
313 /*
314     otk::Property::atoms.net_wm_desktop,
315 */
316     otk::Property::atoms.net_wm_strut,
317     otk::Property::atoms.net_wm_window_type,
318     otk::Property::atoms.net_wm_window_type_desktop,
319     otk::Property::atoms.net_wm_window_type_dock,
320     otk::Property::atoms.net_wm_window_type_toolbar,
321     otk::Property::atoms.net_wm_window_type_menu,
322     otk::Property::atoms.net_wm_window_type_utility,
323     otk::Property::atoms.net_wm_window_type_splash,
324     otk::Property::atoms.net_wm_window_type_dialog,
325     otk::Property::atoms.net_wm_window_type_normal,
326 /*
327     otk::Property::atoms.net_wm_moveresize,
328     otk::Property::atoms.net_wm_moveresize_size_topleft,
329     otk::Property::atoms.net_wm_moveresize_size_topright,
330     otk::Property::atoms.net_wm_moveresize_size_bottomleft,
331     otk::Property::atoms.net_wm_moveresize_size_bottomright,
332     otk::Property::atoms.net_wm_moveresize_move,
333 */
334     otk::Property::atoms.net_wm_allowed_actions,
335     otk::Property::atoms.net_wm_action_move,
336     otk::Property::atoms.net_wm_action_resize,
337     otk::Property::atoms.net_wm_action_minimize,
338     otk::Property::atoms.net_wm_action_shade,
339 /*    otk::Property::atoms.net_wm_action_stick,*/
340     otk::Property::atoms.net_wm_action_maximize_horz,
341     otk::Property::atoms.net_wm_action_maximize_vert,
342     otk::Property::atoms.net_wm_action_fullscreen,
343     otk::Property::atoms.net_wm_action_change_desktop,
344     otk::Property::atoms.net_wm_action_close,
345
346     otk::Property::atoms.net_wm_state,
347     otk::Property::atoms.net_wm_state_modal,
348     otk::Property::atoms.net_wm_state_maximized_vert,
349     otk::Property::atoms.net_wm_state_maximized_horz,
350     otk::Property::atoms.net_wm_state_shaded,
351     otk::Property::atoms.net_wm_state_skip_taskbar,
352     otk::Property::atoms.net_wm_state_skip_pager,
353     otk::Property::atoms.net_wm_state_hidden,
354     otk::Property::atoms.net_wm_state_fullscreen,
355     otk::Property::atoms.net_wm_state_above,
356     otk::Property::atoms.net_wm_state_below,
357   };
358   const int num_supported = sizeof(supported)/sizeof(Atom);
359
360   otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_supported,
361                      otk::Property::atoms.atom, supported, num_supported);
362 }
363
364
365 void Screen::changeClientList()
366 {
367   Window *windows;
368   unsigned int size = clients.size();
369
370   // create an array of the window ids
371   if (size > 0) {
372     Window *win_it;
373     
374     windows = new Window[size];
375     win_it = windows;
376     Client::List::const_iterator it = clients.begin();
377     const Client::List::const_iterator end = clients.end();
378     for (; it != end; ++it, ++win_it)
379       *win_it = (*it)->window();
380   } else
381     windows = (Window*) 0;
382
383   otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_client_list,
384                      otk::Property::atoms.window, windows, size);
385
386   if (size)
387     delete [] windows;
388
389   changeStackingList();
390 }
391
392
393 void Screen::changeStackingList()
394 {
395   Window *windows;
396   unsigned int size = _stacking.size();
397
398   assert(size == clients.size()); // just making sure.. :)
399
400   
401   // create an array of the window ids (from bottom to top, reverse order!)
402   if (size > 0) {
403     Window *win_it;
404     
405     windows = new Window[size];
406     win_it = windows;
407     Client::List::const_reverse_iterator it = _stacking.rbegin();
408     const Client::List::const_reverse_iterator end = _stacking.rend();
409     for (; it != end; ++it, ++win_it)
410       *win_it = (*it)->window();
411   } else
412     windows = (Window*) 0;
413
414   otk::Property::set(_info->rootWindow(),
415                      otk::Property::atoms.net_client_list_stacking,
416                      otk::Property::atoms.window, windows, size);
417
418   if (size)
419     delete [] windows;
420 }
421
422
423 void Screen::changeWorkArea() {
424   unsigned long *dims = new unsigned long[4 * _num_desktops];
425   for (long i = 0; i < _num_desktops; ++i) {
426     // XXX: this could be different for each workspace
427     dims[(i * 4) + 0] = _area.x();
428     dims[(i * 4) + 1] = _area.y();
429     dims[(i * 4) + 2] = _area.width();
430     dims[(i * 4) + 3] = _area.height();
431   }
432   otk::Property::set(_info->rootWindow(), otk::Property::atoms.net_workarea,
433                      otk::Property::atoms.cardinal, dims, 4 * _num_desktops);
434   delete [] dims;
435 }
436
437
438 void Screen::manageWindow(Window window)
439 {
440   Client *client = 0;
441   XWMHints *wmhint;
442   XSetWindowAttributes attrib_set;
443   XEvent e;
444   XWindowAttributes attrib;
445
446   otk::display->grab();
447
448   // check if it has already been unmapped by the time we started mapping
449   // the grab does a sync so we don't have to here
450   if (XCheckTypedWindowEvent(**otk::display, window, DestroyNotify, &e) ||
451       XCheckTypedWindowEvent(**otk::display, window, UnmapNotify, &e)) {
452     XPutBackEvent(**otk::display, &e);
453     
454     otk::display->ungrab();
455     return; // don't manage it
456   }
457   
458   if (!XGetWindowAttributes(**otk::display, window, &attrib) ||
459       attrib.override_redirect) {
460     otk::display->ungrab();
461     return; // don't manage it
462   }
463   
464   // is the window a docking app
465   if ((wmhint = XGetWMHints(**otk::display, window))) {
466     if ((wmhint->flags & StateHint) &&
467         wmhint->initial_state == WithdrawnState) {
468       //slit->addClient(w); // XXX: make dock apps work!
469
470       otk::display->ungrab();
471       XFree(wmhint);
472       return;
473     }
474     XFree(wmhint);
475   }
476
477   // choose the events we want to receive on the CLIENT window
478   attrib_set.event_mask = Client::event_mask;
479   attrib_set.do_not_propagate_mask = Client::no_propagate_mask;
480   XChangeWindowAttributes(**otk::display, window,
481                           CWEventMask|CWDontPropagate, &attrib_set);
482
483   // create the Client class, which gets all of the hints on the window
484   client = new Client(_number, window);
485   // register for events
486   openbox->registerHandler(window, client);
487   // add to the wm's map
488   openbox->addClient(window, client);
489
490   // we dont want a border on the client
491   client->toggleClientBorder(false);
492   
493   // specify that if we exit, the window should not be destroyed and should be
494   // reparented back to root automatically
495   XChangeSaveSet(**otk::display, window, SetModeInsert);
496
497   // create the decoration frame for the client window
498   client->frame = new Frame(client, &_style);
499   // register the plate for events (map req's)
500   // this involves removing itself from the handler list first, since it is
501   // auto added to the list, being a widget. we won't get any events on the
502   // plate except for events for the client (SubstructureRedirectMask)
503   openbox->clearHandler(client->frame->plate());
504   openbox->registerHandler(client->frame->plate(), client);
505
506   // add to the wm's map
507   openbox->addClient(client->frame->window(), client);
508   openbox->addClient(client->frame->plate(), client);
509   openbox->addClient(client->frame->titlebar(), client);
510   openbox->addClient(client->frame->label(), client);
511   openbox->addClient(client->frame->button_max(), client);
512   openbox->addClient(client->frame->button_iconify(), client);
513   openbox->addClient(client->frame->button_alldesk(), client);
514   openbox->addClient(client->frame->button_close(), client);
515   openbox->addClient(client->frame->handle(), client);
516   openbox->addClient(client->frame->grip_left(), client);
517   openbox->addClient(client->frame->grip_right(), client);
518
519   // reparent the client to the frame
520   client->frame->grabClient();
521
522   if (openbox->state() != Openbox::State_Starting) {
523     // position the window intelligenty .. hopefully :)
524     // call the python PLACEWINDOW binding
525     EventData data(_number, client, EventAction::PlaceWindow, 0);
526     openbox->bindings()->fireEvent(&data);
527   }
528
529   EventData ddata(_number, client, EventAction::DisplayingWindow, 0);
530   openbox->bindings()->fireEvent(&ddata);
531
532   // if on the current desktop.. (or all desktops)
533   if (client->desktop() == _desktop ||
534       client->desktop() == (signed)0xffffffff) {
535     client->frame->show();
536   }
537
538   client->applyStartupState();
539
540   otk::display->ungrab();
541
542   // add to the screen's list
543   clients.push_back(client);
544   // once the client is in the list, update our strut to include the new
545   // client's (it is good that this happens after window placement!)
546   updateStrut();
547   // this puts into the stacking order, then raises it
548   _stacking.push_back(client);
549   raiseWindow(client);
550   // update the root properties
551   changeClientList();
552
553   openbox->bindings()->grabButtons(true, client);
554
555   EventData ndata(_number, client, EventAction::NewWindow, 0);
556   openbox->bindings()->fireEvent(&ndata);
557
558 #ifdef DEBUG
559   printf("Managed window 0x%lx frame 0x%lx\n",
560          window, client->frame->window());
561 #endif
562 }
563
564
565 void Screen::unmanageWindow(Client *client)
566 {
567   Frame *frame = client->frame;
568
569   // call the python CLOSEWINDOW binding 
570   EventData data(_number, client, EventAction::CloseWindow, 0);
571   openbox->bindings()->fireEvent(&data);
572
573   openbox->bindings()->grabButtons(false, client);
574
575   // remove from the wm's map
576   openbox->removeClient(client->window());
577   openbox->removeClient(frame->window());
578   openbox->removeClient(frame->plate());
579   openbox->removeClient(frame->titlebar());
580   openbox->removeClient(frame->label());
581   openbox->removeClient(frame->button_max());
582   openbox->removeClient(frame->button_iconify());
583   openbox->removeClient(frame->button_alldesk());
584   openbox->removeClient(frame->button_close());
585   openbox->removeClient(frame->handle());
586   openbox->removeClient(frame->grip_left());
587   openbox->removeClient(frame->grip_right());
588   // unregister for handling events
589   openbox->clearHandler(client->window());
590   
591   // remove the window from our save set
592   XChangeSaveSet(**otk::display, client->window(), SetModeDelete);
593
594   // we dont want events no more
595   XSelectInput(**otk::display, client->window(), NoEventMask);
596
597   frame->hide();
598
599   // give the client its border back
600   client->toggleClientBorder(true);
601
602   // reparent the window out of the frame
603   frame->releaseClient();
604
605 #ifdef DEBUG
606   Window framewin = client->frame->window();
607 #endif
608   delete client->frame;
609   client->frame = 0;
610
611   // remove from the stacking order
612   _stacking.remove(client);
613
614   // remove from the screen's list
615   clients.remove(client);
616
617   // once the client is out of the list, update our strut to remove it's
618   // influence
619   updateStrut();
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   Client::List::iterator it = --_stacking.end();
641   const Client::List::iterator end = _stacking.begin();
642
643   for (; it != end && (*it)->layer() < client->layer(); --it);
644   if (*it == client) return;          // already the bottom, return
645
646   wins[0] = (*it)->frame->window();
647   wins[1] = client->frame->window();
648
649   _stacking.remove(client);
650   _stacking.insert(++it, client);
651
652   XRestackWindows(**otk::display, wins, 2);
653   changeStackingList();
654 }
655
656 void Screen::raiseWindow(Client *client)
657 {
658   Window wins[2];  // only ever restack 2 windows.
659
660   assert(!_stacking.empty()); // this would be bad
661
662   // remove the client before looking so we can't run into ourselves
663   _stacking.remove(client);
664   
665   Client::List::iterator it = _stacking.begin();
666   const Client::List::iterator end = _stacking.end();
667
668   // the stacking list is from highest to lowest
669   for (; it != end && (*it)->layer() > client->layer(); ++it);
670
671   /*
672     if our new position is the top, we want to stack under the _focuswindow
673     otherwise, we want to stack under the previous window in the stack.
674   */
675   wins[0] = (it == _stacking.begin() ? _focuswindow :
676              ((*(--Client::List::const_iterator(it)))->frame->window()));
677   wins[1] = client->frame->window();
678
679   _stacking.insert(it, client);
680
681   XRestackWindows(**otk::display, wins, 2);
682   changeStackingList();
683 }
684
685 void Screen::changeDesktop(long desktop)
686 {
687   if (!(desktop >= 0 && desktop < _num_desktops)) return;
688
689   printf("Moving to desktop %ld\n", desktop);
690   
691   long old = _desktop;
692   
693   _desktop = desktop;
694   otk::Property::set(_info->rootWindow(),
695                      otk::Property::atoms.net_current_desktop,
696                      otk::Property::atoms.cardinal, _desktop);
697
698   if (old == _desktop) return;
699
700   Client::List::iterator it, end = clients.end();
701   for (it = clients.begin(); it != end; ++it) {
702     if ((*it)->desktop() == old) {
703       (*it)->frame->hide();
704     } else if ((*it)->desktop() == _desktop) {
705       (*it)->frame->show();
706     }
707   }
708
709   // force the callbacks to fire
710   if (!openbox->focusedClient())
711     openbox->setFocusedClient(0);
712 }
713
714 void Screen::changeNumDesktops(long num)
715 {
716   assert(num > 0);
717   
718   if (!(num > 0)) return;
719
720   // XXX: move windows on desktops that will no longer exist!
721   
722   _num_desktops = num;
723   otk::Property::set(_info->rootWindow(),
724                      otk::Property::atoms.net_number_of_desktops,
725                      otk::Property::atoms.cardinal, _num_desktops);
726
727   // set the viewport hint
728   unsigned long *viewport = new unsigned long[_num_desktops * 2];
729   memset(viewport, 0, sizeof(unsigned long) * _num_desktops * 2);
730   otk::Property::set(_info->rootWindow(),
731                      otk::Property::atoms.net_desktop_viewport,
732                      otk::Property::atoms.cardinal,
733                      viewport, _num_desktops * 2);
734   delete [] viewport;
735
736   // update the work area hint
737   changeWorkArea();
738 }
739
740
741 void Screen::updateDesktopNames()
742 {
743   unsigned long num = (unsigned) -1;
744   
745   if (!otk::Property::get(_info->rootWindow(),
746                           otk::Property::atoms.net_desktop_names,
747                           otk::Property::utf8, &num, &_desktop_names))
748     _desktop_names.clear();
749   while ((long)_desktop_names.size() < _num_desktops)
750     _desktop_names.push_back("Unnamed");
751 }
752
753
754 void Screen::setDesktopName(long i, const otk::ustring &name)
755 {
756   assert(i >= 0);
757
758   if (i >= _num_desktops) return;
759
760   otk::Property::StringVect newnames = _desktop_names;
761   newnames[i] = name;
762   otk::Property::set(_info->rootWindow(),
763                      otk::Property::atoms.net_desktop_names,
764                      otk::Property::utf8, newnames);
765 }
766
767
768 void Screen::propertyHandler(const XPropertyEvent &e)
769 {
770   otk::EventHandler::propertyHandler(e);
771
772   // compress changes to a single property into a single change
773   XEvent ce;
774   while (XCheckTypedEvent(**otk::display, e.type, &ce)) {
775     // XXX: it would be nice to compress ALL changes to a property, not just
776     //      changes in a row without other props between.
777     if (ce.xproperty.atom != e.atom) {
778       XPutBackEvent(**otk::display, &ce);
779       break;
780     }
781   }
782
783   if (e.atom == otk::Property::atoms.net_desktop_names)
784     updateDesktopNames();
785 }
786
787
788 void Screen::clientMessageHandler(const XClientMessageEvent &e)
789 {
790   otk::EventHandler::clientMessageHandler(e);
791
792   if (e.format != 32) return;
793
794   if (e.message_type == otk::Property::atoms.net_current_desktop) {
795     changeDesktop(e.data.l[0]);
796   } else if (e.message_type == otk::Property::atoms.net_number_of_desktops) {
797     changeNumDesktops(e.data.l[0]);
798   }
799   // XXX: so many client messages to handle here! ..or not.. they go to clients
800 }
801
802
803 void Screen::mapRequestHandler(const XMapRequestEvent &e)
804 {
805   otk::EventHandler::mapRequestHandler(e);
806
807 #ifdef    DEBUG
808   printf("MapRequest for 0x%lx\n", e.window);
809 #endif // DEBUG
810
811   Client *c = openbox->findClient(e.window);
812   if (c) {
813 #ifdef DEBUG
814     printf("DEBUG: MAP REQUEST CAUGHT IN SCREEN. IGNORED.\n");
815 #endif
816   } else
817     manageWindow(e.window);
818 }
819
820 }