set the net support atoms
[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_UNISTD_H
13 #  include <sys/types.h>
14 #  include <unistd.h>
15 #endif // HAVE_UNISTD_H
16
17 #include "gettext.h"
18 #define _(str) gettext(str)
19 }
20
21 #include "screen.hh"
22 #include "client.hh"
23 #include "openbox.hh"
24 #include "frame.hh"
25 #include "bindings.hh"
26 #include "python.hh"
27 #include "otk/display.hh"
28
29 #include <vector>
30
31 static bool running;
32 static int anotherWMRunning(Display *display, XErrorEvent *) {
33   printf(_("Another window manager already running on display %s.\n"),
34          DisplayString(display));
35   running = true;
36   return -1;
37 }
38
39
40 namespace ob {
41
42
43 OBScreen::OBScreen(int screen)
44   : _number(screen),
45     _root(screen)
46 {
47   assert(screen >= 0); assert(screen < ScreenCount(otk::OBDisplay::display));
48   _info = otk::OBDisplay::screenInfo(screen);
49
50   ::running = false;
51   XErrorHandler old = XSetErrorHandler(::anotherWMRunning);
52   XSelectInput(otk::OBDisplay::display, _info->rootWindow(),
53                OBScreen::event_mask);
54   XSync(otk::OBDisplay::display, false);
55   XSetErrorHandler(old);
56
57   _managed = !::running;
58   if (! _managed) return; // was unable to manage the screen
59
60   printf(_("Managing screen %d: visual 0x%lx, depth %d\n"),
61          _number, XVisualIDFromVisual(_info->visual()), _info->depth());
62
63   Openbox::instance->property()->set(_info->rootWindow(),
64                                      otk::OBProperty::openbox_pid,
65                                      otk::OBProperty::Atom_Cardinal,
66                                      (unsigned long) getpid());
67
68   // set the mouse cursor for the root window (the default cursor)
69   XDefineCursor(otk::OBDisplay::display, _info->rootWindow(),
70                 Openbox::instance->cursors().session);
71
72   // initialize the shit that is used for all drawing on the screen
73   _image_control = new otk::BImageControl(Openbox::instance->timerManager(),
74                                           _info, true);
75   _image_control->installRootColormap();
76   _root_cmap_installed = True;
77
78   // initialize the screen's style
79   _style.setImageControl(_image_control);
80   std::string stylepath;
81   python_get_string("theme", &stylepath);
82   otk::Configuration sconfig(false);
83   sconfig.setFile(otk::expandTilde(stylepath));
84   if (!sconfig.load()) {
85     sconfig.setFile(otk::expandTilde(DEFAULTSTYLE));
86     if (!sconfig.load()) {
87       printf(_("Unable to load default style: %s. Aborting.\n"), DEFAULTSTYLE);
88       ::exit(1);
89     }
90   }
91   _style.load(sconfig);
92
93   // set up notification of netwm support
94   setSupportedAtoms();
95
96   // Set the netwm atoms for geomtery and viewport
97   unsigned long geometry[] = { _info->width(),
98                                _info->height() };
99   Openbox::instance->property()->set(_info->rootWindow(),
100                                      otk::OBProperty::net_desktop_geometry,
101                                      otk::OBProperty::Atom_Cardinal,
102                                      geometry, 2);
103   unsigned long viewport[] = { 0, 0 };
104   Openbox::instance->property()->set(_info->rootWindow(),
105                                      otk::OBProperty::net_desktop_viewport,
106                                      otk::OBProperty::Atom_Cardinal,
107                                      viewport, 2);
108
109   // create the window which gets focus when no clients get it
110   XSetWindowAttributes attr;
111   attr.override_redirect = true;
112   _focuswindow = XCreateWindow(otk::OBDisplay::display, _info->rootWindow(),
113                                -100, -100, 1, 1, 0, 0, InputOnly,
114                                _info->visual(), CWOverrideRedirect, &attr);
115   XMapWindow(otk::OBDisplay::display, _focuswindow);
116   
117   // these may be further updated if any pre-existing windows are found in
118   // the manageExising() function
119   setClientList();     // initialize the client lists, which will be empty
120   calcArea();          // initialize the available working area
121 }
122
123
124 OBScreen::~OBScreen()
125 {
126   if (! _managed) return;
127
128   XSelectInput(otk::OBDisplay::display, _info->rootWindow(), NoEventMask);
129   
130   // unmanage all windows
131   while (!clients.empty())
132     unmanageWindow(clients.front());
133
134   XDestroyWindow(otk::OBDisplay::display, _focuswindow);
135   XDestroyWindow(otk::OBDisplay::display, _supportwindow);
136
137   delete _image_control;
138 }
139
140
141 void OBScreen::manageExisting()
142 {
143   unsigned int i, j, nchild;
144   Window r, p, *children;
145   XQueryTree(otk::OBDisplay::display, _info->rootWindow(), &r, &p,
146              &children, &nchild);
147
148   // preen the window list of all icon windows... for better dockapp support
149   for (i = 0; i < nchild; i++) {
150     if (children[i] == None) continue;
151
152     XWMHints *wmhints = XGetWMHints(otk::OBDisplay::display,
153                                     children[i]);
154
155     if (wmhints) {
156       if ((wmhints->flags & IconWindowHint) &&
157           (wmhints->icon_window != children[i])) {
158         for (j = 0; j < nchild; j++) {
159           if (children[j] == wmhints->icon_window) {
160             children[j] = None;
161             break;
162           }
163         }
164       }
165
166       XFree(wmhints);
167     }
168   }
169
170   // manage shown windows
171   for (i = 0; i < nchild; ++i) {
172     if (children[i] == None)
173       continue;
174
175     XWindowAttributes attrib;
176     if (XGetWindowAttributes(otk::OBDisplay::display, children[i], &attrib)) {
177       if (attrib.override_redirect) continue;
178
179       if (attrib.map_state != IsUnmapped) {
180         manageWindow(children[i]);
181       }
182     }
183   }
184
185   XFree(children);
186 }
187
188
189 //! Adds a window's strut to the screen's list of reserved spaces
190 void OBScreen::addStrut(otk::Strut *strut)
191 {
192   _struts.push_back(strut);
193 }
194
195
196 //! Removes a window's strut from the screen's list of reserved spaces
197 void OBScreen::removeStrut(otk::Strut *strut)
198 {
199   _struts.remove(strut);
200 }
201
202
203 void OBScreen::calcArea()
204 {
205   otk::Rect old_area = _area;
206
207 /*
208 #ifdef    XINERAMA
209   // reset to the full areas
210   if (isXineramaActive())
211     xineramaUsableArea = getXineramaAreas();
212 #endif // XINERAMA
213 */
214   
215   /* these values represent offsets from the screen edge
216    * we look for the biggest offset on each edge and then apply them
217    * all at once
218    * do not be confused by the similarity to the names of Rect's members
219    */
220   unsigned int current_left = 0, current_right = 0, current_top = 0,
221     current_bottom = 0;
222
223   StrutList::const_iterator it = _struts.begin(), end = _struts.end();
224
225   for(; it != end; ++it) {
226     otk::Strut *strut = *it;
227     if (strut->left > current_left)
228       current_left = strut->left;
229     if (strut->top > current_top)
230       current_top = strut->top;
231     if (strut->right > current_right)
232       current_right = strut->right;
233     if (strut->bottom > current_bottom)
234       current_bottom = strut->bottom;
235   }
236
237   _area.setRect(current_left, current_top,
238                 _info->width() - (current_left + current_right),
239                 _info->height() - (current_top + current_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     // XXX: re-maximize windows
266
267   setWorkArea();
268 }
269
270
271 void OBScreen::setSupportedAtoms()
272 {
273   // create the netwm support window
274   _supportwindow = XCreateSimpleWindow(otk::OBDisplay::display,
275                                        _info->rootWindow(),
276                                        0, 0, 1, 1, 0, 0, 0);
277   assert(_supportwindow != None);
278
279   // set supporting window
280   Openbox::instance->property()->set(_info->rootWindow(),
281                                      otk::OBProperty::net_supporting_wm_check,
282                                      otk::OBProperty::Atom_Window,
283                                      _supportwindow);
284
285   //set properties on the supporting window
286   Openbox::instance->property()->set(_supportwindow,
287                                      otk::OBProperty::net_wm_name,
288                                      otk::OBProperty::utf8,
289                                      "Openbox");
290   Openbox::instance->property()->set(_supportwindow,
291                                      otk::OBProperty::net_supporting_wm_check,
292                                      otk::OBProperty::Atom_Window,
293                                      _supportwindow);
294
295   
296   Atom supported[] = {
297 /*
298       otk::OBProperty::net_current_desktop,
299       otk::OBProperty::net_number_of_desktops,
300 */
301       otk::OBProperty::net_desktop_geometry,
302       otk::OBProperty::net_desktop_viewport,
303       otk::OBProperty::net_active_window,
304       otk::OBProperty::net_workarea,
305       otk::OBProperty::net_client_list,
306       otk::OBProperty::net_client_list_stacking,
307 /*
308       otk::OBProperty::net_desktop_names,
309 */
310       otk::OBProperty::net_close_window,
311       otk::OBProperty::net_wm_name,
312       otk::OBProperty::net_wm_visible_name,
313       otk::OBProperty::net_wm_icon_name,
314       otk::OBProperty::net_wm_visible_icon_name,
315 /*
316       otk::OBProperty::net_wm_desktop,
317 */
318 /*
319       otk::OBProperty::net_wm_strut,
320 */
321       otk::OBProperty::net_wm_window_type,
322       otk::OBProperty::net_wm_window_type_desktop,
323       otk::OBProperty::net_wm_window_type_dock,
324       otk::OBProperty::net_wm_window_type_toolbar,
325       otk::OBProperty::net_wm_window_type_menu,
326       otk::OBProperty::net_wm_window_type_utility,
327       otk::OBProperty::net_wm_window_type_splash,
328       otk::OBProperty::net_wm_window_type_dialog,
329       otk::OBProperty::net_wm_window_type_normal,
330 /*
331       otk::OBProperty::net_wm_moveresize,
332       otk::OBProperty::net_wm_moveresize_size_topleft,
333       otk::OBProperty::net_wm_moveresize_size_topright,
334       otk::OBProperty::net_wm_moveresize_size_bottomleft,
335       otk::OBProperty::net_wm_moveresize_size_bottomright,
336       otk::OBProperty::net_wm_moveresize_move,
337 */
338 /*
339       otk::OBProperty::net_wm_allowed_actions,
340       otk::OBProperty::net_wm_action_move,
341       otk::OBProperty::net_wm_action_resize,
342       otk::OBProperty::net_wm_action_shade,
343       otk::OBProperty::net_wm_action_maximize_horz,
344       otk::OBProperty::net_wm_action_maximize_vert,
345       otk::OBProperty::net_wm_action_change_desktop,
346       otk::OBProperty::net_wm_action_close,
347 */
348       otk::OBProperty::net_wm_state,
349       otk::OBProperty::net_wm_state_modal,
350       otk::OBProperty::net_wm_state_maximized_vert,
351       otk::OBProperty::net_wm_state_maximized_horz,
352       otk::OBProperty::net_wm_state_shaded,
353       otk::OBProperty::net_wm_state_skip_taskbar,
354       otk::OBProperty::net_wm_state_skip_pager,
355       otk::OBProperty::net_wm_state_hidden,
356       otk::OBProperty::net_wm_state_fullscreen,
357       otk::OBProperty::net_wm_state_above,
358       otk::OBProperty::net_wm_state_below,
359     };
360   const int num_supported = sizeof(supported)/sizeof(Atom);
361
362   // convert to the atom values
363   for (int i = 0; i < num_supported; ++i)
364     supported[i] = Openbox::instance->property()->atom(supported[i]);
365   
366   Openbox::instance->property()->set(_info->rootWindow(),
367                                      otk::OBProperty::net_supported,
368                                      otk::OBProperty::Atom_Atom,
369                                      supported, num_supported);
370 }
371
372
373 void OBScreen::setClientList()
374 {
375   Window *windows;
376   unsigned int size = clients.size();
377
378   // create an array of the window ids
379   if (size > 0) {
380     Window *win_it;
381     
382     windows = new Window[size];
383     win_it = windows;
384     ClientList::const_iterator it = clients.begin();
385     const ClientList::const_iterator end = clients.end();
386     for (; it != end; ++it, ++win_it)
387       *win_it = (*it)->window();
388   } else
389     windows = (Window*) 0;
390
391   Openbox::instance->property()->set(_info->rootWindow(),
392                                      otk::OBProperty::net_client_list,
393                                      otk::OBProperty::Atom_Window,
394                                      windows, size);
395
396   if (size)
397     delete [] windows;
398
399   setStackingList();
400 }
401
402
403 void OBScreen::setStackingList()
404 {
405   Window *windows;
406   unsigned int size = _stacking.size();
407
408   assert(size == clients.size()); // just making sure.. :)
409
410   
411   // create an array of the window ids
412   if (size > 0) {
413     Window *win_it;
414     
415     windows = new Window[size];
416     win_it = windows;
417     ClientList::const_iterator it = _stacking.begin();
418     const ClientList::const_iterator end = _stacking.end();
419     for (; it != end; ++it, ++win_it)
420       *win_it = (*it)->window();
421   } else
422     windows = (Window*) 0;
423
424   Openbox::instance->property()->set(_info->rootWindow(),
425                                      otk::OBProperty::net_client_list_stacking,
426                                      otk::OBProperty::Atom_Window,
427                                      windows, size);
428
429   if (size)
430     delete [] windows;
431 }
432
433
434 void OBScreen::setWorkArea() {
435   unsigned long area[] = { _area.x(), _area.y(),
436                            _area.width(), _area.height() };
437   Openbox::instance->property()->set(_info->rootWindow(),
438                                      otk::OBProperty::net_workarea,
439                                      otk::OBProperty::Atom_Cardinal,
440                                      area, 4);
441   /*
442   if (workspacesList.size() > 0) {
443     unsigned long *dims = new unsigned long[4 * workspacesList.size()];
444     for (unsigned int i = 0, m = workspacesList.size(); i < m; ++i) {
445       // XXX: this could be different for each workspace
446       const otk::Rect &area = availableArea();
447       dims[(i * 4) + 0] = area.x();
448       dims[(i * 4) + 1] = area.y();
449       dims[(i * 4) + 2] = area.width();
450       dims[(i * 4) + 3] = area.height();
451     }
452     xatom->set(getRootWindow(), otk::OBProperty::net_workarea,
453                otk::OBProperty::Atom_Cardinal,
454                dims, 4 * workspacesList.size());
455     delete [] dims;
456   } else
457     xatom->set(getRootWindow(), otk::OBProperty::net_workarea,
458                otk::OBProperty::Atom_Cardinal, 0, 0);
459   */
460 }
461
462
463 void OBScreen::manageWindow(Window window)
464 {
465   OBClient *client = 0;
466   XWMHints *wmhint;
467   XSetWindowAttributes attrib_set;
468
469   // is the window a docking app
470   if ((wmhint = XGetWMHints(otk::OBDisplay::display, window))) {
471     if ((wmhint->flags & StateHint) &&
472         wmhint->initial_state == WithdrawnState) {
473       //slit->addClient(w); // XXX: make dock apps work!
474       XFree(wmhint);
475       return;
476     }
477     XFree(wmhint);
478   }
479
480   otk::OBDisplay::grab();
481
482   // choose the events we want to receive on the CLIENT window
483   attrib_set.event_mask = OBClient::event_mask;
484   attrib_set.do_not_propagate_mask = OBClient::no_propagate_mask;
485   XChangeWindowAttributes(otk::OBDisplay::display, window,
486                           CWEventMask|CWDontPropagate, &attrib_set);
487
488   // create the OBClient class, which gets all of the hints on the window
489   client = new OBClient(_number, window);
490   // register for events
491   Openbox::instance->registerHandler(window, client);
492   // add to the wm's map
493   Openbox::instance->addClient(window, client);
494
495   // we dont want a border on the client
496   client->toggleClientBorder(false);
497   
498   // specify that if we exit, the window should not be destroyed and should be
499   // reparented back to root automatically
500   XChangeSaveSet(otk::OBDisplay::display, window, SetModeInsert);
501
502   if (!client->positionRequested()) {
503     // XXX: position the window intelligenty
504   }
505
506   // create the decoration frame for the client window
507   client->frame = new OBFrame(client, &_style);
508
509   // add to the wm's map
510   Openbox::instance->addClient(client->frame->window(), client);
511   Openbox::instance->addClient(client->frame->plate(), client);
512   Openbox::instance->addClient(client->frame->titlebar(), client);
513   Openbox::instance->addClient(client->frame->label(), client);
514   Openbox::instance->addClient(client->frame->button_max(), client);
515   Openbox::instance->addClient(client->frame->button_iconify(), client);
516   Openbox::instance->addClient(client->frame->button_stick(), client);
517   Openbox::instance->addClient(client->frame->button_close(), client);
518   Openbox::instance->addClient(client->frame->handle(), client);
519   Openbox::instance->addClient(client->frame->grip_left(), client);
520   Openbox::instance->addClient(client->frame->grip_right(), client);
521
522   // XXX: if on the current desktop..
523   client->frame->show();
524  
525   // XXX: handle any requested states such as shaded/maximized
526
527   otk::OBDisplay::ungrab();
528
529   // add to the screen's list
530   clients.push_back(client);
531   // this puts into the stacking order, then raises it
532   _stacking.push_back(client);
533   restack(true, client);
534   // update the root properties
535   setClientList();
536
537   Openbox::instance->bindings()->grabButtons(true, client);
538 }
539
540
541 void OBScreen::unmanageWindow(OBClient *client)
542 {
543   OBFrame *frame = client->frame;
544
545   Openbox::instance->bindings()->grabButtons(false, client);
546
547   // remove from the stacking order
548   _stacking.remove(client);
549   
550   // pass around focus if this window was focused XXX do this better!
551   if (Openbox::instance->focusedClient() == client) {
552     OBClient *newfocus = 0;
553     if (!_stacking.empty())
554       newfocus = _stacking.front();
555     if (! (newfocus && newfocus->focus()))
556       client->unfocus();
557   }
558
559   // remove from the wm's map
560   Openbox::instance->removeClient(client->window());
561   Openbox::instance->removeClient(frame->window());
562   Openbox::instance->removeClient(frame->plate());
563   Openbox::instance->removeClient(frame->titlebar());
564   Openbox::instance->removeClient(frame->label());
565   Openbox::instance->removeClient(frame->button_max());
566   Openbox::instance->removeClient(frame->button_iconify());
567   Openbox::instance->removeClient(frame->button_stick());
568   Openbox::instance->removeClient(frame->button_close());
569   Openbox::instance->removeClient(frame->handle());
570   Openbox::instance->removeClient(frame->grip_left());
571   Openbox::instance->removeClient(frame->grip_right());
572   // unregister for handling events
573   Openbox::instance->clearHandler(client->window());
574   
575   // remove the window from our save set
576   XChangeSaveSet(otk::OBDisplay::display, client->window(), SetModeDelete);
577
578   // we dont want events no more
579   XSelectInput(otk::OBDisplay::display, client->window(), NoEventMask);
580
581   frame->hide();
582
583   // give the client its border back
584   client->toggleClientBorder(true);
585
586   delete client->frame;
587   client->frame = 0;
588
589   // remove from the screen's list
590   clients.remove(client);
591   delete client;
592
593   // update the root properties
594   setClientList();
595 }
596
597 void OBScreen::restack(bool raise, OBClient *client)
598 {
599   const int layer = client->layer();
600   std::vector<Window> wins;
601
602   _stacking.remove(client);
603
604   // the stacking list is from highest to lowest
605   
606   ClientList::iterator it = _stacking.begin(), end = _stacking.end();
607   // insert the windows above this window
608   for (; it != end; ++it) {
609     if ((*it)->layer() < layer || (raise && (*it)->layer() == layer))
610       break;
611     wins.push_back((*it)->frame->window());
612   }
613   // insert our client
614   wins.push_back(client->frame->window());
615   _stacking.insert(it, client);
616   // insert the remaining below this window
617   for (; it != end; ++it)
618     wins.push_back((*it)->frame->window());
619
620   XRestackWindows(otk::OBDisplay::display, &wins[0], wins.size());
621   setStackingList();
622 }
623
624 }