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