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