update comment
[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 properties for geometry 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   // Set the net_desktop_names property
110   std::vector<std::string> names;
111   python_get_stringlist("desktop_names", &names);
112   _root.setDesktopNames(names);
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::OBDisplay::display, _info->rootWindow(),
118                                -100, -100, 1, 1, 0, 0, InputOnly,
119                                _info->visual(), CWOverrideRedirect, &attr);
120   XMapWindow(otk::OBDisplay::display, _focuswindow);
121   
122   // these may be further updated if any pre-existing windows are found in
123   // the manageExising() function
124   setClientList();     // initialize the client lists, which will be empty
125   calcArea();          // initialize the available working area
126 }
127
128
129 OBScreen::~OBScreen()
130 {
131   if (! _managed) return;
132
133   XSelectInput(otk::OBDisplay::display, _info->rootWindow(), NoEventMask);
134   
135   // unmanage all windows
136   while (!clients.empty())
137     unmanageWindow(clients.front());
138
139   XDestroyWindow(otk::OBDisplay::display, _focuswindow);
140   XDestroyWindow(otk::OBDisplay::display, _supportwindow);
141
142   delete _image_control;
143 }
144
145
146 void OBScreen::manageExisting()
147 {
148   unsigned int i, j, nchild;
149   Window r, p, *children;
150   XQueryTree(otk::OBDisplay::display, _info->rootWindow(), &r, &p,
151              &children, &nchild);
152
153   // preen the window list of all icon windows... for better dockapp support
154   for (i = 0; i < nchild; i++) {
155     if (children[i] == None) continue;
156
157     XWMHints *wmhints = XGetWMHints(otk::OBDisplay::display,
158                                     children[i]);
159
160     if (wmhints) {
161       if ((wmhints->flags & IconWindowHint) &&
162           (wmhints->icon_window != children[i])) {
163         for (j = 0; j < nchild; j++) {
164           if (children[j] == wmhints->icon_window) {
165             children[j] = None;
166             break;
167           }
168         }
169       }
170
171       XFree(wmhints);
172     }
173   }
174
175   // manage shown windows
176   for (i = 0; i < nchild; ++i) {
177     if (children[i] == None)
178       continue;
179
180     XWindowAttributes attrib;
181     if (XGetWindowAttributes(otk::OBDisplay::display, children[i], &attrib)) {
182       if (attrib.override_redirect) continue;
183
184       if (attrib.map_state != IsUnmapped) {
185         manageWindow(children[i]);
186       }
187     }
188   }
189
190   XFree(children);
191 }
192
193
194 //! Adds a window's strut to the screen's list of reserved spaces
195 void OBScreen::addStrut(otk::Strut *strut)
196 {
197   _struts.push_back(strut);
198 }
199
200
201 //! Removes a window's strut from the screen's list of reserved spaces
202 void OBScreen::removeStrut(otk::Strut *strut)
203 {
204   _struts.remove(strut);
205 }
206
207
208 void OBScreen::calcArea()
209 {
210   otk::Rect old_area = _area;
211
212 /*
213 #ifdef    XINERAMA
214   // reset to the full areas
215   if (isXineramaActive())
216     xineramaUsableArea = getXineramaAreas();
217 #endif // XINERAMA
218 */
219   
220   /* these values represent offsets from the screen edge
221    * we look for the biggest offset on each edge and then apply them
222    * all at once
223    * do not be confused by the similarity to the names of Rect's members
224    */
225   unsigned int current_left = 0, current_right = 0, current_top = 0,
226     current_bottom = 0;
227
228   StrutList::const_iterator it = _struts.begin(), end = _struts.end();
229
230   for(; it != end; ++it) {
231     otk::Strut *strut = *it;
232     if (strut->left > current_left)
233       current_left = strut->left;
234     if (strut->top > current_top)
235       current_top = strut->top;
236     if (strut->right > current_right)
237       current_right = strut->right;
238     if (strut->bottom > current_bottom)
239       current_bottom = strut->bottom;
240   }
241
242   _area.setRect(current_left, current_top,
243                 _info->width() - (current_left + current_right),
244                 _info->height() - (current_top + current_bottom));
245
246 /*
247 #ifdef    XINERAMA
248   if (isXineramaActive()) {
249     // keep each of the ximerama-defined areas inside the strut
250     RectList::iterator xit, xend = xineramaUsableArea.end();
251     for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
252       if (xit->x() < usableArea.x()) {
253         xit->setX(usableArea.x());
254         xit->setWidth(xit->width() - usableArea.x());
255       }
256       if (xit->y() < usableArea.y()) {
257         xit->setY(usableArea.y());
258         xit->setHeight(xit->height() - usableArea.y());
259       }
260       if (xit->x() + xit->width() > usableArea.width())
261         xit->setWidth(usableArea.width() - xit->x());
262       if (xit->y() + xit->height() > usableArea.height())
263         xit->setHeight(usableArea.height() - xit->y());
264     }
265   }
266 #endif // XINERAMA
267 */
268   
269   if (old_area != _area)
270     // XXX: re-maximize windows
271
272   setWorkArea();
273 }
274
275
276 void OBScreen::setSupportedAtoms()
277 {
278   // create the netwm support window
279   _supportwindow = XCreateSimpleWindow(otk::OBDisplay::display,
280                                        _info->rootWindow(),
281                                        0, 0, 1, 1, 0, 0, 0);
282   assert(_supportwindow != None);
283
284   // set supporting window
285   Openbox::instance->property()->set(_info->rootWindow(),
286                                      otk::OBProperty::net_supporting_wm_check,
287                                      otk::OBProperty::Atom_Window,
288                                      _supportwindow);
289
290   //set properties on the supporting window
291   Openbox::instance->property()->set(_supportwindow,
292                                      otk::OBProperty::net_wm_name,
293                                      otk::OBProperty::utf8,
294                                      "Openbox");
295   Openbox::instance->property()->set(_supportwindow,
296                                      otk::OBProperty::net_supporting_wm_check,
297                                      otk::OBProperty::Atom_Window,
298                                      _supportwindow);
299
300   
301   Atom supported[] = {
302 /*
303       otk::OBProperty::net_current_desktop,
304       otk::OBProperty::net_number_of_desktops,
305 */
306       otk::OBProperty::net_desktop_geometry,
307       otk::OBProperty::net_desktop_viewport,
308       otk::OBProperty::net_active_window,
309       otk::OBProperty::net_workarea,
310       otk::OBProperty::net_client_list,
311       otk::OBProperty::net_client_list_stacking,
312       otk::OBProperty::net_desktop_names,
313       otk::OBProperty::net_close_window,
314       otk::OBProperty::net_wm_name,
315       otk::OBProperty::net_wm_visible_name,
316       otk::OBProperty::net_wm_icon_name,
317       otk::OBProperty::net_wm_visible_icon_name,
318 /*
319       otk::OBProperty::net_wm_desktop,
320 */
321 /*
322       otk::OBProperty::net_wm_strut,
323 */
324       otk::OBProperty::net_wm_window_type,
325       otk::OBProperty::net_wm_window_type_desktop,
326       otk::OBProperty::net_wm_window_type_dock,
327       otk::OBProperty::net_wm_window_type_toolbar,
328       otk::OBProperty::net_wm_window_type_menu,
329       otk::OBProperty::net_wm_window_type_utility,
330       otk::OBProperty::net_wm_window_type_splash,
331       otk::OBProperty::net_wm_window_type_dialog,
332       otk::OBProperty::net_wm_window_type_normal,
333 /*
334       otk::OBProperty::net_wm_moveresize,
335       otk::OBProperty::net_wm_moveresize_size_topleft,
336       otk::OBProperty::net_wm_moveresize_size_topright,
337       otk::OBProperty::net_wm_moveresize_size_bottomleft,
338       otk::OBProperty::net_wm_moveresize_size_bottomright,
339       otk::OBProperty::net_wm_moveresize_move,
340 */
341 /*
342       otk::OBProperty::net_wm_allowed_actions,
343       otk::OBProperty::net_wm_action_move,
344       otk::OBProperty::net_wm_action_resize,
345       otk::OBProperty::net_wm_action_shade,
346       otk::OBProperty::net_wm_action_maximize_horz,
347       otk::OBProperty::net_wm_action_maximize_vert,
348       otk::OBProperty::net_wm_action_change_desktop,
349       otk::OBProperty::net_wm_action_close,
350 */
351       otk::OBProperty::net_wm_state,
352       otk::OBProperty::net_wm_state_modal,
353       otk::OBProperty::net_wm_state_maximized_vert,
354       otk::OBProperty::net_wm_state_maximized_horz,
355       otk::OBProperty::net_wm_state_shaded,
356       otk::OBProperty::net_wm_state_skip_taskbar,
357       otk::OBProperty::net_wm_state_skip_pager,
358       otk::OBProperty::net_wm_state_hidden,
359       otk::OBProperty::net_wm_state_fullscreen,
360       otk::OBProperty::net_wm_state_above,
361       otk::OBProperty::net_wm_state_below,
362     };
363   const int num_supported = sizeof(supported)/sizeof(Atom);
364
365   // convert to the atom values
366   for (int i = 0; i < num_supported; ++i)
367     supported[i] =
368       Openbox::instance->property()->atom((otk::OBProperty::Atoms)supported[i]);
369   
370   Openbox::instance->property()->set(_info->rootWindow(),
371                                      otk::OBProperty::net_supported,
372                                      otk::OBProperty::Atom_Atom,
373                                      supported, num_supported);
374 }
375
376
377 void OBScreen::setClientList()
378 {
379   Window *windows;
380   unsigned int size = clients.size();
381
382   // create an array of the window ids
383   if (size > 0) {
384     Window *win_it;
385     
386     windows = new Window[size];
387     win_it = windows;
388     ClientList::const_iterator it = clients.begin();
389     const ClientList::const_iterator end = clients.end();
390     for (; it != end; ++it, ++win_it)
391       *win_it = (*it)->window();
392   } else
393     windows = (Window*) 0;
394
395   Openbox::instance->property()->set(_info->rootWindow(),
396                                      otk::OBProperty::net_client_list,
397                                      otk::OBProperty::Atom_Window,
398                                      windows, size);
399
400   if (size)
401     delete [] windows;
402
403   setStackingList();
404 }
405
406
407 void OBScreen::setStackingList()
408 {
409   Window *windows;
410   unsigned int size = _stacking.size();
411
412   assert(size == clients.size()); // just making sure.. :)
413
414   
415   // create an array of the window ids
416   if (size > 0) {
417     Window *win_it;
418     
419     windows = new Window[size];
420     win_it = windows;
421     ClientList::const_iterator it = _stacking.begin();
422     const ClientList::const_iterator end = _stacking.end();
423     for (; it != end; ++it, ++win_it)
424       *win_it = (*it)->window();
425   } else
426     windows = (Window*) 0;
427
428   Openbox::instance->property()->set(_info->rootWindow(),
429                                      otk::OBProperty::net_client_list_stacking,
430                                      otk::OBProperty::Atom_Window,
431                                      windows, size);
432
433   if (size)
434     delete [] windows;
435 }
436
437
438 void OBScreen::setWorkArea() {
439   unsigned long area[] = { _area.x(), _area.y(),
440                            _area.width(), _area.height() };
441   Openbox::instance->property()->set(_info->rootWindow(),
442                                      otk::OBProperty::net_workarea,
443                                      otk::OBProperty::Atom_Cardinal,
444                                      area, 4);
445   /*
446   if (workspacesList.size() > 0) {
447     unsigned long *dims = new unsigned long[4 * workspacesList.size()];
448     for (unsigned int i = 0, m = workspacesList.size(); i < m; ++i) {
449       // XXX: this could be different for each workspace
450       const otk::Rect &area = availableArea();
451       dims[(i * 4) + 0] = area.x();
452       dims[(i * 4) + 1] = area.y();
453       dims[(i * 4) + 2] = area.width();
454       dims[(i * 4) + 3] = area.height();
455     }
456     xatom->set(getRootWindow(), otk::OBProperty::net_workarea,
457                otk::OBProperty::Atom_Cardinal,
458                dims, 4 * workspacesList.size());
459     delete [] dims;
460   } else
461     xatom->set(getRootWindow(), otk::OBProperty::net_workarea,
462                otk::OBProperty::Atom_Cardinal, 0, 0);
463   */
464 }
465
466
467 void OBScreen::manageWindow(Window window)
468 {
469   OBClient *client = 0;
470   XWMHints *wmhint;
471   XSetWindowAttributes attrib_set;
472
473   // is the window a docking app
474   if ((wmhint = XGetWMHints(otk::OBDisplay::display, window))) {
475     if ((wmhint->flags & StateHint) &&
476         wmhint->initial_state == WithdrawnState) {
477       //slit->addClient(w); // XXX: make dock apps work!
478       XFree(wmhint);
479       return;
480     }
481     XFree(wmhint);
482   }
483
484   otk::OBDisplay::grab();
485
486   // choose the events we want to receive on the CLIENT window
487   attrib_set.event_mask = OBClient::event_mask;
488   attrib_set.do_not_propagate_mask = OBClient::no_propagate_mask;
489   XChangeWindowAttributes(otk::OBDisplay::display, window,
490                           CWEventMask|CWDontPropagate, &attrib_set);
491
492   // create the OBClient class, which gets all of the hints on the window
493   client = new OBClient(_number, window);
494   // register for events
495   Openbox::instance->registerHandler(window, client);
496   // add to the wm's map
497   Openbox::instance->addClient(window, client);
498
499   // we dont want a border on the client
500   client->toggleClientBorder(false);
501   
502   // specify that if we exit, the window should not be destroyed and should be
503   // reparented back to root automatically
504   XChangeSaveSet(otk::OBDisplay::display, window, SetModeInsert);
505
506   if (!client->positionRequested()) {
507     // XXX: position the window intelligenty
508   }
509
510   // create the decoration frame for the client window
511   client->frame = new OBFrame(client, &_style);
512
513   // add to the wm's map
514   Openbox::instance->addClient(client->frame->window(), client);
515   Openbox::instance->addClient(client->frame->plate(), client);
516   Openbox::instance->addClient(client->frame->titlebar(), client);
517   Openbox::instance->addClient(client->frame->label(), client);
518   Openbox::instance->addClient(client->frame->button_max(), client);
519   Openbox::instance->addClient(client->frame->button_iconify(), client);
520   Openbox::instance->addClient(client->frame->button_stick(), client);
521   Openbox::instance->addClient(client->frame->button_close(), client);
522   Openbox::instance->addClient(client->frame->handle(), client);
523   Openbox::instance->addClient(client->frame->grip_left(), client);
524   Openbox::instance->addClient(client->frame->grip_right(), client);
525
526   // XXX: if on the current desktop..
527   client->frame->show();
528  
529   // XXX: handle any requested states such as maximized
530
531   otk::OBDisplay::ungrab();
532
533   // add to the screen's list
534   clients.push_back(client);
535   // this puts into the stacking order, then raises it
536   _stacking.push_back(client);
537   restack(true, client);
538   // update the root properties
539   setClientList();
540
541   Openbox::instance->bindings()->grabButtons(true, client);
542 }
543
544
545 void OBScreen::unmanageWindow(OBClient *client)
546 {
547   OBFrame *frame = client->frame;
548
549   Openbox::instance->bindings()->grabButtons(false, client);
550
551   // remove from the stacking order
552   _stacking.remove(client);
553   
554   // pass around focus if this window was focused XXX do this better!
555   if (Openbox::instance->focusedClient() == client) {
556     OBClient *newfocus = 0;
557     if (!_stacking.empty())
558       newfocus = _stacking.front();
559     if (! (newfocus && newfocus->focus()))
560       client->unfocus();
561   }
562
563   // remove from the wm's map
564   Openbox::instance->removeClient(client->window());
565   Openbox::instance->removeClient(frame->window());
566   Openbox::instance->removeClient(frame->plate());
567   Openbox::instance->removeClient(frame->titlebar());
568   Openbox::instance->removeClient(frame->label());
569   Openbox::instance->removeClient(frame->button_max());
570   Openbox::instance->removeClient(frame->button_iconify());
571   Openbox::instance->removeClient(frame->button_stick());
572   Openbox::instance->removeClient(frame->button_close());
573   Openbox::instance->removeClient(frame->handle());
574   Openbox::instance->removeClient(frame->grip_left());
575   Openbox::instance->removeClient(frame->grip_right());
576   // unregister for handling events
577   Openbox::instance->clearHandler(client->window());
578   
579   // remove the window from our save set
580   XChangeSaveSet(otk::OBDisplay::display, client->window(), SetModeDelete);
581
582   // we dont want events no more
583   XSelectInput(otk::OBDisplay::display, client->window(), NoEventMask);
584
585   frame->hide();
586
587   // give the client its border back
588   client->toggleClientBorder(true);
589
590   delete client->frame;
591   client->frame = 0;
592
593   // remove from the screen's list
594   clients.remove(client);
595   delete client;
596
597   // update the root properties
598   setClientList();
599 }
600
601 void OBScreen::restack(bool raise, OBClient *client)
602 {
603   const int layer = client->layer();
604   std::vector<Window> wins;
605
606   _stacking.remove(client);
607
608   // the stacking list is from highest to lowest
609   
610   ClientList::iterator it = _stacking.begin(), end = _stacking.end();
611   // insert the windows above this window
612   for (; it != end; ++it) {
613     if ((*it)->layer() < layer || (raise && (*it)->layer() == layer))
614       break;
615     wins.push_back((*it)->frame->window());
616   }
617   // insert our client
618   wins.push_back(client->frame->window());
619   _stacking.insert(it, client);
620   // insert the remaining below this window
621   for (; it != end; ++it)
622     wins.push_back((*it)->frame->window());
623
624   XRestackWindows(otk::OBDisplay::display, &wins[0], wins.size());
625   setStackingList();
626 }
627
628 }