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