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