move screen.cc/hh to bbscreen.cc/hh
[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   assert(XGetWindowAttributes(otk::OBDisplay::display, _window, &wattrib));
276
277   _area.setRect(wattrib.x, wattrib.y, wattrib.width, wattrib.height);
278   _border_width = wattrib.border_width;
279 }
280
281
282 void OBClient::getState()
283 {
284   const otk::OBProperty *property = Openbox::instance->property();
285
286   _modal = _shaded = _max_horz = _max_vert = _fullscreen = _floating = false;
287   
288   unsigned long *state;
289   unsigned long num = (unsigned) -1;
290   
291   if (property->get(_window, otk::OBProperty::net_wm_state,
292                     otk::OBProperty::Atom_Atom, &num, &state)) {
293     for (unsigned long i = 0; i < num; ++i) {
294       if (state[i] == property->atom(otk::OBProperty::net_wm_state_modal))
295         _modal = true;
296       else if (state[i] ==
297                property->atom(otk::OBProperty::net_wm_state_shaded))
298         _shaded = true;
299       else if (state[i] ==
300                property->atom(otk::OBProperty::net_wm_state_fullscreen))
301         _fullscreen = true;
302       else if (state[i] ==
303                property->atom(otk::OBProperty::net_wm_state_maximized_vert))
304         _max_vert = true;
305       else if (state[i] ==
306                property->atom(otk::OBProperty::net_wm_state_maximized_horz))
307         _max_horz = true;
308     }
309
310     delete [] state;
311   }
312 }
313
314
315 void OBClient::getShaped()
316 {
317   _shaped = false;
318 #ifdef   SHAPE
319   if (otk::OBDisplay::shape()) {
320     int foo;
321     unsigned int ufoo;
322     int s;
323
324     XShapeSelectInput(otk::OBDisplay::display, _window, ShapeNotifyMask);
325
326     XShapeQueryExtents(otk::OBDisplay::display, _window, &s, &foo,
327                        &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo, &ufoo);
328     _shaped = (s != 0);
329   }
330 #endif // SHAPE
331 }
332
333
334 void OBClient::updateProtocols()
335 {
336   const otk::OBProperty *property = Openbox::instance->property();
337
338   Atom *proto;
339   int num_return = 0;
340
341   _focus_notify = false;
342   _decorations &= ~Decor_Close;
343   _functions &= ~Func_Close;
344
345   if (XGetWMProtocols(otk::OBDisplay::display, _window, &proto, &num_return)) {
346     for (int i = 0; i < num_return; ++i) {
347       if (proto[i] == property->atom(otk::OBProperty::wm_delete_window)) {
348         _decorations |= Decor_Close;
349         _functions |= Func_Close;
350         // XXX: update the decor?
351       } else if (proto[i] == property->atom(otk::OBProperty::wm_take_focus))
352         // if this protocol is requested, then the window will be notified
353         // by the window manager whenever it receives focus
354         _focus_notify = true;
355     }
356     XFree(proto);
357   }
358 }
359
360
361 void OBClient::updateNormalHints()
362 {
363   XSizeHints size;
364   long ret;
365
366   // defaults
367   _gravity = NorthWestGravity;
368   _inc_x = _inc_y = 1;
369   _base_x = _base_y = 0;
370   _min_x = _min_y = 0;
371   _max_x = _max_y = INT_MAX;
372
373   // XXX: might want to cancel any interactive resizing of the window at this
374   // point..
375
376   // get the hints from the window
377   if (XGetWMNormalHints(otk::OBDisplay::display, _window, &size, &ret)) {
378     _positioned = (size.flags & (PPosition|USPosition));
379
380     if (size.flags & PWinGravity)
381       _gravity = size.win_gravity;
382     
383     if (size.flags & PMinSize) {
384       _min_x = size.min_width;
385       _min_y = size.min_height;
386     }
387     
388     if (size.flags & PMaxSize) {
389       _max_x = size.max_width;
390       _max_y = size.max_height;
391     }
392     
393     if (size.flags & PBaseSize) {
394       _base_x = size.base_width;
395       _base_y = size.base_height;
396     }
397     
398     if (size.flags & PResizeInc) {
399       _inc_x = size.width_inc;
400       _inc_y = size.height_inc;
401     }
402   }
403 }
404
405
406 void OBClient::updateWMHints()
407 {
408   XWMHints *hints;
409
410   // assume a window takes input if it doesnt specify
411   _can_focus = true;
412   _urgent = false;
413   
414   if ((hints = XGetWMHints(otk::OBDisplay::display, _window)) != NULL) {
415     if (hints->flags & InputHint)
416       _can_focus = hints->input;
417
418     if (hints->flags & XUrgencyHint)
419       _urgent = true;
420
421     if (hints->flags & WindowGroupHint) {
422       if (hints->window_group != _group) {
423         // XXX: remove from the old group if there was one
424         _group = hints->window_group;
425         // XXX: do stuff with the group
426       }
427     } else // no group!
428       _group = None;
429
430     XFree(hints);
431   }
432 }
433
434
435 void OBClient::updateTitle()
436 {
437   const otk::OBProperty *property = Openbox::instance->property();
438
439   _title = "";
440   
441   // try netwm
442   if (! property->get(_window, otk::OBProperty::net_wm_name,
443                       otk::OBProperty::utf8, &_title)) {
444     // try old x stuff
445     property->get(_window, otk::OBProperty::wm_name,
446                   otk::OBProperty::ascii, &_title);
447   }
448
449   if (_title.empty())
450     _title = _("Unnamed Window");
451 }
452
453
454 void OBClient::updateIconTitle()
455 {
456   const otk::OBProperty *property = Openbox::instance->property();
457
458   _icon_title = "";
459   
460   // try netwm
461   if (! property->get(_window, otk::OBProperty::net_wm_icon_name,
462                       otk::OBProperty::utf8, &_icon_title)) {
463     // try old x stuff
464     property->get(_window, otk::OBProperty::wm_icon_name,
465                   otk::OBProperty::ascii, &_icon_title);
466   }
467
468   if (_title.empty())
469     _icon_title = _("Unnamed Window");
470 }
471
472
473 void OBClient::updateClass()
474 {
475   const otk::OBProperty *property = Openbox::instance->property();
476
477   // set the defaults
478   _app_name = _app_class = "";
479
480   otk::OBProperty::StringVect v;
481   unsigned long num = 2;
482
483   if (! property->get(_window, otk::OBProperty::wm_class,
484                       otk::OBProperty::ascii, &num, &v))
485     return;
486
487   if (num > 0) _app_name = v[0];
488   if (num > 1) _app_class = v[1];
489 }
490
491
492 void OBClient::update(const XPropertyEvent &e)
493 {
494   const otk::OBProperty *property = Openbox::instance->property();
495
496   if (e.atom == XA_WM_NORMAL_HINTS)
497     updateNormalHints();
498   else if (e.atom == XA_WM_HINTS)
499     updateWMHints();
500   else if (e.atom == property->atom(otk::OBProperty::net_wm_name) ||
501            e.atom == property->atom(otk::OBProperty::wm_name))
502     updateTitle();
503   else if (e.atom == property->atom(otk::OBProperty::net_wm_icon_name) ||
504            e.atom == property->atom(otk::OBProperty::wm_icon_name))
505     updateIconTitle();
506   else if (e.atom == property->atom(otk::OBProperty::wm_class))
507     updateClass();
508   else if (e.atom == property->atom(otk::OBProperty::wm_protocols))
509     updateProtocols();
510   // XXX: transient for hint
511   // XXX: strut hint
512 }
513
514
515 void OBClient::setWMState(long state)
516 {
517   if (state == _wmstate) return; // no change
518   
519   switch (state) {
520   case IconicState:
521     // XXX: cause it to iconify
522     break;
523   case NormalState:
524     // XXX: cause it to uniconify
525     break;
526   }
527   _wmstate = state;
528 }
529
530
531 void OBClient::setDesktop(long target)
532 {
533   assert(target >= 0);
534   //assert(target == 0xffffffff || target < MAX);
535   
536   // XXX: move the window to the new desktop
537   _desktop = target;
538 }
539
540
541 void OBClient::setState(StateAction action, long data1, long data2)
542 {
543   const otk::OBProperty *property = Openbox::instance->property();
544
545   if (!(action == State_Add || action == State_Remove ||
546         action == State_Toggle))
547     return; // an invalid action was passed to the client message, ignore it
548
549   for (int i = 0; i < 2; ++i) {
550     Atom state = i == 0 ? data1 : data2;
551     
552     if (! state) continue;
553
554     // if toggling, then pick whether we're adding or removing
555     if (action == State_Toggle) {
556       if (state == property->atom(otk::OBProperty::net_wm_state_modal))
557         action = _modal ? State_Remove : State_Add;
558       else if (state ==
559                property->atom(otk::OBProperty::net_wm_state_maximized_vert))
560         action = _max_vert ? State_Remove : State_Add;
561       else if (state ==
562                property->atom(otk::OBProperty::net_wm_state_maximized_horz))
563         action = _max_horz ? State_Remove : State_Add;
564       else if (state == property->atom(otk::OBProperty::net_wm_state_shaded))
565         action = _shaded ? State_Remove : State_Add;
566       else if (state ==
567                property->atom(otk::OBProperty::net_wm_state_fullscreen))
568         action = _fullscreen ? State_Remove : State_Add;
569       else if (state == property->atom(otk::OBProperty::net_wm_state_floating))
570         action = _floating ? State_Remove : State_Add;
571     }
572     
573     if (action == State_Add) {
574       if (state == property->atom(otk::OBProperty::net_wm_state_modal)) {
575         if (_modal) continue;
576         _modal = true;
577         // XXX: give it focus if another window has focus that shouldnt now
578       } else if (state ==
579                  property->atom(otk::OBProperty::net_wm_state_maximized_vert)){
580         if (_max_vert) continue;
581         _max_vert = true;
582         // XXX: resize the window etc
583       } else if (state ==
584                  property->atom(otk::OBProperty::net_wm_state_maximized_horz)){
585         if (_max_horz) continue;
586         _max_horz = true;
587         // XXX: resize the window etc
588       } else if (state ==
589                  property->atom(otk::OBProperty::net_wm_state_shaded)) {
590         if (_shaded) continue;
591         _shaded = true;
592         // XXX: hide the client window
593       } else if (state ==
594                  property->atom(otk::OBProperty::net_wm_state_fullscreen)) {
595         if (_fullscreen) continue;
596         _fullscreen = true;
597         // XXX: raise the window n shit
598       } else if (state ==
599                  property->atom(otk::OBProperty::net_wm_state_floating)) {
600         if (_floating) continue;
601         _floating = true;
602         // XXX: raise the window n shit
603       }
604
605     } else { // action == State_Remove
606       if (state == property->atom(otk::OBProperty::net_wm_state_modal)) {
607         if (!_modal) continue;
608         _modal = false;
609       } else if (state ==
610                  property->atom(otk::OBProperty::net_wm_state_maximized_vert)){
611         if (!_max_vert) continue;
612         _max_vert = false;
613         // XXX: resize the window etc
614       } else if (state ==
615                  property->atom(otk::OBProperty::net_wm_state_maximized_horz)){
616         if (!_max_horz) continue;
617         _max_horz = false;
618         // XXX: resize the window etc
619       } else if (state ==
620                  property->atom(otk::OBProperty::net_wm_state_shaded)) {
621         if (!_shaded) continue;
622         _shaded = false;
623         // XXX: show the client window
624       } else if (state ==
625                  property->atom(otk::OBProperty::net_wm_state_fullscreen)) {
626         if (!_fullscreen) continue;
627         _fullscreen = false;
628         // XXX: lower the window to its proper layer
629       } else if (state ==
630                  property->atom(otk::OBProperty::net_wm_state_floating)) {
631         if (!_floating) continue;
632         _floating = false;
633         // XXX: lower the window to its proper layer
634       }
635     }
636   }
637 }
638
639
640 void OBClient::update(const XClientMessageEvent &e)
641 {
642   if (e.format != 32) return;
643
644   const otk::OBProperty *property = Openbox::instance->property();
645   
646   if (e.message_type == property->atom(otk::OBProperty::wm_change_state))
647     setWMState(e.data.l[0]);
648   else if (e.message_type ==
649              property->atom(otk::OBProperty::net_wm_desktop))
650     setDesktop(e.data.l[0]);
651   else if (e.message_type == property->atom(otk::OBProperty::net_wm_state))
652     setState((StateAction)e.data.l[0], e.data.l[1], e.data.l[2]);
653 }
654
655
656 #if defined(SHAPE) || defined(DOXYGEN_IGNORE)
657 void OBClient::update(const XShapeEvent &e)
658 {
659   _shaped = e.shaped;
660 }
661 #endif
662
663
664 void OBClient::setArea(const otk::Rect &area)
665 {
666   _area = area;
667 }
668
669 }