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