]> icculus.org git repositories - mikachu/openbox.git/blob - src/client.cc
handle map events with the Openbox class
[mikachu/openbox.git] / src / client.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 #include "client.hh"
8 #include "bbscreen.hh"
9 #include "openbox.hh"
10 #include "otk/display.hh"
11 #include "otk/property.hh"
12
13 extern "C" {
14 #include <X11/Xlib.h>
15 #include <X11/Xutil.h>
16
17 #include <assert.h>
18
19 #include "gettext.h"
20 #define _(str) gettext(str)
21 }
22
23 namespace ob {
24
25 OBClient::OBClient(int screen, Window window)
26   : otk::OtkEventHandler(),
27     _screen(screen), _window(window)
28 {
29   assert(screen >= 0);
30   assert(window);
31
32   Openbox::instance->registerHandler(_window, this);
33
34   ignore_unmaps = 0;
35   
36   // update EVERYTHING the first time!!
37
38   // the state is kinda assumed to be normal. is this right? XXX
39   _wmstate = NormalState;
40   // no default decors or functions, each has to be enabled
41   _decorations = _functions = 0;
42   
43   getArea();
44   getDesktop();
45   getType();
46
47   // set the decorations and functions
48   switch (_type) {
49   case Type_Normal:
50     // normal windows retain all of the possible decorations and
51     // functionality
52     _decorations = Decor_Titlebar | Decor_Handle | Decor_Border |
53                    Decor_Iconify | Decor_Maximize;
54     _functions = Func_Resize | Func_Move | Func_Iconify | Func_Maximize;
55
56   case Type_Dialog:
57     // dialogs cannot be maximized
58     _decorations &= ~Decor_Maximize;
59     _functions &= ~Func_Maximize;
60     break;
61
62   case Type_Menu:
63   case Type_Toolbar:
64   case Type_Utility:
65     // these windows get less functionality
66     _decorations &= ~(Decor_Iconify | Decor_Handle);
67     _functions &= ~(Func_Iconify | Func_Resize);
68     break;
69
70   case Type_Desktop:
71   case Type_Dock:
72   case Type_Splash:
73     // none of these windows are manipulated by the window manager
74     _decorations = 0;
75     _functions = 0;
76     break;
77   }
78   
79   getMwmHints(); // this fucks (in good ways) with the decors and functions
80   getState();
81   getShaped();
82
83   updateProtocols();
84   updateNormalHints();
85   updateWMHints();
86   // XXX: updateTransientFor();
87   updateTitle();
88   updateIconTitle();
89   updateClass();
90
91 /*
92 #ifdef DEBUG
93   printf("Mapped window: 0x%lx\n"
94          "  title:         \t%s\t  icon title:    \t%s\n"
95          "  app name:      \t%s\t\t  class:         \t%s\n"
96          "  position:      \t%d, %d\t\t  size:          \t%d, %d\n"
97          "  desktop:       \t%lu\t\t  group:         \t0x%lx\n"
98          "  type:          \t%d\t\t  min size       \t%d, %d\n"
99          "  base size      \t%d, %d\t\t  max size       \t%d, %d\n"
100          "  size incr      \t%d, %d\t\t  gravity        \t%d\n"
101          "  wm state       \t%ld\t\t  can be focused:\t%s\n"
102          "  notify focus:  \t%s\t\t  urgent:        \t%s\n"
103          "  shaped:        \t%s\t\t  modal:         \t%s\n"
104          "  shaded:        \t%s\t\t  iconic:        \t%s\n"
105          "  vert maximized:\t%s\t\t  horz maximized:\t%s\n"
106          "  fullscreen:    \t%s\t\t  floating:      \t%s\n"
107          "  requested pos: \t%s\n",
108          _window,
109          _title.c_str(),
110          _icon_title.c_str(),
111          _app_name.c_str(),
112          _app_class.c_str(),
113          _area.x(), _area.y(),
114          _area.width(), _area.height(),
115          _desktop,
116          _group,
117          _type,
118          _min_x, _min_y,
119          _base_x, _base_y,
120          _max_x, _max_y,
121          _inc_x, _inc_y,
122          _gravity,
123          _wmstate,
124          _can_focus ? "yes" : "no",
125          _focus_notify ? "yes" : "no",
126          _urgent ? "yes" : "no",
127          _shaped ? "yes" : "no",
128          _modal ? "yes" : "no",
129          _shaded ? "yes" : "no",
130          _iconic ? "yes" : "no",
131          _max_vert ? "yes" : "no",
132          _max_horz ? "yes" : "no",
133          _fullscreen ? "yes" : "no",
134          _floating ? "yes" : "no",
135          _positioned ? "yes" : "no");
136 #endif
137 */
138 }
139
140
141 OBClient::~OBClient()
142 {
143   const otk::OBProperty *property = Openbox::instance->property();
144
145   // these values should not be persisted across a window unmapping/mapping
146   property->erase(_window, otk::OBProperty::net_wm_desktop);
147   property->erase(_window, otk::OBProperty::net_wm_state);
148 }
149
150
151 void OBClient::getDesktop()
152 {
153   const otk::OBProperty *property = Openbox::instance->property();
154
155   // defaults to the current desktop
156   _desktop = 0; // XXX: change this to the current desktop!
157
158   property->get(_window, otk::OBProperty::net_wm_desktop,
159                 otk::OBProperty::Atom_Cardinal,
160                 &_desktop);
161 }
162
163
164 void OBClient::getType()
165 {
166   const otk::OBProperty *property = Openbox::instance->property();
167
168   _type = (WindowType) -1;
169   
170   unsigned long *val;
171   unsigned long num = (unsigned) -1;
172   if (property->get(_window, otk::OBProperty::net_wm_window_type,
173                     otk::OBProperty::Atom_Atom,
174                     &num, &val)) {
175     // use the first value that we know about in the array
176     for (unsigned long i = 0; i < num; ++i) {
177       if (val[i] ==
178           property->atom(otk::OBProperty::net_wm_window_type_desktop))
179         _type = Type_Desktop;
180       else if (val[i] ==
181                property->atom(otk::OBProperty::net_wm_window_type_dock))
182         _type = Type_Dock;
183       else if (val[i] ==
184                property->atom(otk::OBProperty::net_wm_window_type_toolbar))
185         _type = Type_Toolbar;
186       else if (val[i] ==
187                property->atom(otk::OBProperty::net_wm_window_type_menu))
188         _type = Type_Menu;
189       else if (val[i] ==
190                property->atom(otk::OBProperty::net_wm_window_type_utility))
191         _type = Type_Utility;
192       else if (val[i] ==
193                property->atom(otk::OBProperty::net_wm_window_type_splash))
194         _type = Type_Splash;
195       else if (val[i] ==
196                property->atom(otk::OBProperty::net_wm_window_type_dialog))
197         _type = Type_Dialog;
198       else if (val[i] ==
199                property->atom(otk::OBProperty::net_wm_window_type_normal))
200         _type = Type_Normal;
201 //      else if (val[i] ==
202 //               property->atom(otk::OBProperty::kde_net_wm_window_type_override))
203 //        mwm_decorations = 0; // prevent this window from getting any decor
204       // XXX: make this work again
205     }
206     delete val;
207   }
208     
209   if (_type == (WindowType) -1) {
210     /*
211      * the window type hint was not set, which means we either classify ourself
212      * as a normal window or a dialog, depending on if we are a transient.
213      */
214     // XXX: make this code work!
215     //if (isTransient())
216     //  _type = Type_Dialog;
217     //else
218       _type = Type_Normal;
219   }
220 }
221
222
223 void OBClient::getMwmHints()
224 {
225   const otk::OBProperty *property = Openbox::instance->property();
226
227   unsigned long num;
228   MwmHints *hints;
229
230   num = MwmHints::elements;
231   if (!property->get(_window, otk::OBProperty::motif_wm_hints,
232                      otk::OBProperty::motif_wm_hints, &num,
233                      (unsigned long **)&hints))
234     return;
235   
236   if (num < MwmHints::elements) {
237     delete [] hints;
238     return;
239   }
240
241   // retrieved the hints
242   // Mwm Hints are applied subtractively to what has already been chosen for
243   // decor and functionality
244
245   if (hints->flags & MwmFlag_Decorations) {
246     if (! (hints->decorations & MwmDecor_All)) {
247       if (! (hints->decorations & MwmDecor_Border))
248         _decorations &= ~Decor_Border;
249       if (! (hints->decorations & MwmDecor_Handle))
250         _decorations &= ~Decor_Handle;
251       if (! (hints->decorations & MwmDecor_Title))
252         _decorations &= ~Decor_Titlebar;
253       if (! (hints->decorations & MwmDecor_Iconify))
254         _decorations &= ~Decor_Iconify;
255       if (! (hints->decorations & MwmDecor_Maximize))
256         _decorations &= ~Decor_Maximize;
257     }
258   }
259
260   if (hints->flags & MwmFlag_Functions) {
261     if (! (hints->functions & MwmFunc_All)) {
262       if (! (hints->functions & MwmFunc_Resize))
263         _functions &= ~Func_Resize;
264       if (! (hints->functions & MwmFunc_Move))
265         _functions &= ~Func_Move;
266       if (! (hints->functions & MwmFunc_Iconify))
267         _functions &= ~Func_Iconify;
268       if (! (hints->functions & MwmFunc_Maximize))
269         _functions &= ~Func_Maximize;
270       //if (! (hints->functions & MwmFunc_Close))
271       //  _functions &= ~Func_Close;
272     }
273   }
274   delete [] hints;
275 }
276
277
278 void OBClient::getArea()
279 {
280   XWindowAttributes wattrib;
281   Status ret;
282   
283   ret = XGetWindowAttributes(otk::OBDisplay::display, _window, &wattrib);
284   assert(ret != BadWindow);
285
286   _area.setRect(wattrib.x, wattrib.y, wattrib.width, wattrib.height);
287   _border_width = wattrib.border_width;
288 }
289
290
291 void OBClient::getState()
292 {
293   const otk::OBProperty *property = Openbox::instance->property();
294
295   _modal = _shaded = _max_horz = _max_vert = _fullscreen = _floating = false;
296   
297   unsigned long *state;
298   unsigned long num = (unsigned) -1;
299   
300   if (property->get(_window, otk::OBProperty::net_wm_state,
301                     otk::OBProperty::Atom_Atom, &num, &state)) {
302     for (unsigned long i = 0; i < num; ++i) {
303       if (state[i] == property->atom(otk::OBProperty::net_wm_state_modal))
304         _modal = true;
305       else if (state[i] ==
306                property->atom(otk::OBProperty::net_wm_state_shaded))
307         _shaded = true;
308       else if (state[i] ==
309                property->atom(otk::OBProperty::net_wm_state_fullscreen))
310         _fullscreen = true;
311       else if (state[i] ==
312                property->atom(otk::OBProperty::net_wm_state_maximized_vert))
313         _max_vert = true;
314       else if (state[i] ==
315                property->atom(otk::OBProperty::net_wm_state_maximized_horz))
316         _max_horz = true;
317     }
318
319     delete [] state;
320   }
321 }
322
323
324 void OBClient::getShaped()
325 {
326   _shaped = false;
327 #ifdef   SHAPE
328   if (otk::OBDisplay::shape()) {
329     int foo;
330     unsigned int ufoo;
331     int s;
332
333     XShapeSelectInput(otk::OBDisplay::display, _window, ShapeNotifyMask);
334
335     XShapeQueryExtents(otk::OBDisplay::display, _window, &s, &foo,
336                        &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo, &ufoo);
337     _shaped = (s != 0);
338   }
339 #endif // SHAPE
340 }
341
342
343 void OBClient::updateProtocols()
344 {
345   const otk::OBProperty *property = Openbox::instance->property();
346
347   Atom *proto;
348   int num_return = 0;
349
350   _focus_notify = false;
351   _decorations &= ~Decor_Close;
352   _functions &= ~Func_Close;
353
354   if (XGetWMProtocols(otk::OBDisplay::display, _window, &proto, &num_return)) {
355     for (int i = 0; i < num_return; ++i) {
356       if (proto[i] == property->atom(otk::OBProperty::wm_delete_window)) {
357         _decorations |= Decor_Close;
358         _functions |= Func_Close;
359         // XXX: update the decor?
360       } else if (proto[i] == property->atom(otk::OBProperty::wm_take_focus))
361         // if this protocol is requested, then the window will be notified
362         // by the window manager whenever it receives focus
363         _focus_notify = true;
364     }
365     XFree(proto);
366   }
367 }
368
369
370 void OBClient::updateNormalHints()
371 {
372   XSizeHints size;
373   long ret;
374
375   // defaults
376   _gravity = NorthWestGravity;
377   _inc_x = _inc_y = 1;
378   _base_x = _base_y = 0;
379   _min_x = _min_y = 0;
380   _max_x = _max_y = INT_MAX;
381
382   // XXX: might want to cancel any interactive resizing of the window at this
383   // point..
384
385   // get the hints from the window
386   if (XGetWMNormalHints(otk::OBDisplay::display, _window, &size, &ret)) {
387     _positioned = (size.flags & (PPosition|USPosition));
388
389     if (size.flags & PWinGravity)
390       _gravity = size.win_gravity;
391     
392     if (size.flags & PMinSize) {
393       _min_x = size.min_width;
394       _min_y = size.min_height;
395     }
396     
397     if (size.flags & PMaxSize) {
398       _max_x = size.max_width;
399       _max_y = size.max_height;
400     }
401     
402     if (size.flags & PBaseSize) {
403       _base_x = size.base_width;
404       _base_y = size.base_height;
405     }
406     
407     if (size.flags & PResizeInc) {
408       _inc_x = size.width_inc;
409       _inc_y = size.height_inc;
410     }
411   }
412 }
413
414
415 void OBClient::updateWMHints()
416 {
417   XWMHints *hints;
418
419   // assume a window takes input if it doesnt specify
420   _can_focus = true;
421   _urgent = false;
422   
423   if ((hints = XGetWMHints(otk::OBDisplay::display, _window)) != NULL) {
424     if (hints->flags & InputHint)
425       _can_focus = hints->input;
426
427     if (hints->flags & XUrgencyHint)
428       _urgent = true;
429
430     if (hints->flags & WindowGroupHint) {
431       if (hints->window_group != _group) {
432         // XXX: remove from the old group if there was one
433         _group = hints->window_group;
434         // XXX: do stuff with the group
435       }
436     } else // no group!
437       _group = None;
438
439     XFree(hints);
440   }
441 }
442
443
444 void OBClient::updateTitle()
445 {
446   const otk::OBProperty *property = Openbox::instance->property();
447
448   _title = "";
449   
450   // try netwm
451   if (! property->get(_window, otk::OBProperty::net_wm_name,
452                       otk::OBProperty::utf8, &_title)) {
453     // try old x stuff
454     property->get(_window, otk::OBProperty::wm_name,
455                   otk::OBProperty::ascii, &_title);
456   }
457
458   if (_title.empty())
459     _title = _("Unnamed Window");
460 }
461
462
463 void OBClient::updateIconTitle()
464 {
465   const otk::OBProperty *property = Openbox::instance->property();
466
467   _icon_title = "";
468   
469   // try netwm
470   if (! property->get(_window, otk::OBProperty::net_wm_icon_name,
471                       otk::OBProperty::utf8, &_icon_title)) {
472     // try old x stuff
473     property->get(_window, otk::OBProperty::wm_icon_name,
474                   otk::OBProperty::ascii, &_icon_title);
475   }
476
477   if (_title.empty())
478     _icon_title = _("Unnamed Window");
479 }
480
481
482 void OBClient::updateClass()
483 {
484   const otk::OBProperty *property = Openbox::instance->property();
485
486   // set the defaults
487   _app_name = _app_class = "";
488
489   otk::OBProperty::StringVect v;
490   unsigned long num = 2;
491
492   if (! property->get(_window, otk::OBProperty::wm_class,
493                       otk::OBProperty::ascii, &num, &v))
494     return;
495
496   if (num > 0) _app_name = v[0];
497   if (num > 1) _app_class = v[1];
498 }
499
500
501 void OBClient::propertyHandler(const XPropertyEvent &e)
502 {
503   otk::OtkEventHandler::propertyHandler(e);
504   
505   const otk::OBProperty *property = Openbox::instance->property();
506
507   if (e.atom == XA_WM_NORMAL_HINTS)
508     updateNormalHints();
509   else if (e.atom == XA_WM_HINTS)
510     updateWMHints();
511   else if (e.atom == property->atom(otk::OBProperty::net_wm_name) ||
512            e.atom == property->atom(otk::OBProperty::wm_name))
513     updateTitle();
514   else if (e.atom == property->atom(otk::OBProperty::net_wm_icon_name) ||
515            e.atom == property->atom(otk::OBProperty::wm_icon_name))
516     updateIconTitle();
517   else if (e.atom == property->atom(otk::OBProperty::wm_class))
518     updateClass();
519   else if (e.atom == property->atom(otk::OBProperty::wm_protocols))
520     updateProtocols();
521   // XXX: transient for hint
522   // XXX: strut hint
523 }
524
525
526 void OBClient::setWMState(long state)
527 {
528   if (state == _wmstate) return; // no change
529   
530   switch (state) {
531   case IconicState:
532     // XXX: cause it to iconify
533     break;
534   case NormalState:
535     // XXX: cause it to uniconify
536     break;
537   }
538   _wmstate = state;
539 }
540
541
542 void OBClient::setDesktop(long target)
543 {
544   assert(target >= 0);
545   //assert(target == 0xffffffff || target < MAX);
546   
547   // XXX: move the window to the new desktop
548   _desktop = target;
549 }
550
551
552 void OBClient::setState(StateAction action, long data1, long data2)
553 {
554   const otk::OBProperty *property = Openbox::instance->property();
555
556   if (!(action == State_Add || action == State_Remove ||
557         action == State_Toggle))
558     return; // an invalid action was passed to the client message, ignore it
559
560   for (int i = 0; i < 2; ++i) {
561     Atom state = i == 0 ? data1 : data2;
562     
563     if (! state) continue;
564
565     // if toggling, then pick whether we're adding or removing
566     if (action == State_Toggle) {
567       if (state == property->atom(otk::OBProperty::net_wm_state_modal))
568         action = _modal ? State_Remove : State_Add;
569       else if (state ==
570                property->atom(otk::OBProperty::net_wm_state_maximized_vert))
571         action = _max_vert ? State_Remove : State_Add;
572       else if (state ==
573                property->atom(otk::OBProperty::net_wm_state_maximized_horz))
574         action = _max_horz ? State_Remove : State_Add;
575       else if (state == property->atom(otk::OBProperty::net_wm_state_shaded))
576         action = _shaded ? State_Remove : State_Add;
577       else if (state ==
578                property->atom(otk::OBProperty::net_wm_state_fullscreen))
579         action = _fullscreen ? State_Remove : State_Add;
580       else if (state == property->atom(otk::OBProperty::net_wm_state_floating))
581         action = _floating ? State_Remove : State_Add;
582     }
583     
584     if (action == State_Add) {
585       if (state == property->atom(otk::OBProperty::net_wm_state_modal)) {
586         if (_modal) continue;
587         _modal = true;
588         // XXX: give it focus if another window has focus that shouldnt now
589       } else if (state ==
590                  property->atom(otk::OBProperty::net_wm_state_maximized_vert)){
591         if (_max_vert) continue;
592         _max_vert = true;
593         // XXX: resize the window etc
594       } else if (state ==
595                  property->atom(otk::OBProperty::net_wm_state_maximized_horz)){
596         if (_max_horz) continue;
597         _max_horz = true;
598         // XXX: resize the window etc
599       } else if (state ==
600                  property->atom(otk::OBProperty::net_wm_state_shaded)) {
601         if (_shaded) continue;
602         _shaded = true;
603         // XXX: hide the client window
604       } else if (state ==
605                  property->atom(otk::OBProperty::net_wm_state_fullscreen)) {
606         if (_fullscreen) continue;
607         _fullscreen = true;
608         // XXX: raise the window n shit
609       } else if (state ==
610                  property->atom(otk::OBProperty::net_wm_state_floating)) {
611         if (_floating) continue;
612         _floating = true;
613         // XXX: raise the window n shit
614       }
615
616     } else { // action == State_Remove
617       if (state == property->atom(otk::OBProperty::net_wm_state_modal)) {
618         if (!_modal) continue;
619         _modal = false;
620       } else if (state ==
621                  property->atom(otk::OBProperty::net_wm_state_maximized_vert)){
622         if (!_max_vert) continue;
623         _max_vert = false;
624         // XXX: resize the window etc
625       } else if (state ==
626                  property->atom(otk::OBProperty::net_wm_state_maximized_horz)){
627         if (!_max_horz) continue;
628         _max_horz = false;
629         // XXX: resize the window etc
630       } else if (state ==
631                  property->atom(otk::OBProperty::net_wm_state_shaded)) {
632         if (!_shaded) continue;
633         _shaded = false;
634         // XXX: show the client window
635       } else if (state ==
636                  property->atom(otk::OBProperty::net_wm_state_fullscreen)) {
637         if (!_fullscreen) continue;
638         _fullscreen = false;
639         // XXX: lower the window to its proper layer
640       } else if (state ==
641                  property->atom(otk::OBProperty::net_wm_state_floating)) {
642         if (!_floating) continue;
643         _floating = false;
644         // XXX: lower the window to its proper layer
645       }
646     }
647   }
648 }
649
650
651 void OBClient::clientMessageHandler(const XClientMessageEvent &e)
652 {
653   otk::OtkEventHandler::clientMessageHandler(e);
654   
655   if (e.format != 32) return;
656
657   const otk::OBProperty *property = Openbox::instance->property();
658   
659   if (e.message_type == property->atom(otk::OBProperty::wm_change_state))
660     setWMState(e.data.l[0]);
661   else if (e.message_type ==
662              property->atom(otk::OBProperty::net_wm_desktop))
663     setDesktop(e.data.l[0]);
664   else if (e.message_type == property->atom(otk::OBProperty::net_wm_state))
665     setState((StateAction)e.data.l[0], e.data.l[1], e.data.l[2]);
666 }
667
668
669 #if defined(SHAPE) || defined(DOXYGEN_IGNORE)
670 void OBClient::shapeHandler(const XShapeEvent &e)
671 {
672   otk::OtkEventHandler::shapeHandler(e);
673   
674   _shaped = e.shaped;
675 }
676 #endif
677
678
679 void OBClient::setArea(const otk::Rect &area)
680 {
681   _area = area;
682 }
683
684 }