1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
10 #include "otk/display.hh"
11 #include "otk/property.hh"
15 #include <X11/Xutil.h>
20 #define _(str) gettext(str)
25 OBClient::OBClient(int screen, Window window)
26 : _screen(screen), _window(window)
30 // update EVERYTHING the first time!!
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;
41 // set the decorations and functions
44 // normal windows retain all of the possible decorations and
46 _decorations = Decor_Titlebar | Decor_Handle | Decor_Border |
47 Decor_Iconify | Decor_Maximize;
48 _functions = Func_Resize | Func_Move | Func_Iconify | Func_Maximize;
51 // dialogs cannot be maximized
52 _decorations &= ~Decor_Maximize;
53 _functions &= ~Func_Maximize;
59 // these windows get less functionality
60 _decorations &= ~(Decor_Iconify | Decor_Handle);
61 _functions &= ~(Func_Iconify | Func_Resize);
67 // none of these windows are manipulated by the window manager
73 getMwmHints(); // this fucks (in good ways) with the decors and functions
80 // XXX: updateTransientFor();
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",
107 _area.x(), _area.y(),
108 _area.width(), _area.height(),
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");
135 OBClient::~OBClient()
137 const otk::OBProperty *property = Openbox::instance->property();
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);
145 void OBClient::getDesktop()
147 const otk::OBProperty *property = Openbox::instance->property();
149 // defaults to the current desktop
150 _desktop = 0; // XXX: change this to the current desktop!
152 property->get(_window, otk::OBProperty::net_wm_desktop,
153 otk::OBProperty::Atom_Cardinal,
158 void OBClient::getType()
160 const otk::OBProperty *property = Openbox::instance->property();
162 _type = (WindowType) -1;
165 unsigned long num = (unsigned) -1;
166 if (property->get(_window, otk::OBProperty::net_wm_window_type,
167 otk::OBProperty::Atom_Atom,
169 // use the first value that we know about in the array
170 for (unsigned long i = 0; i < num; ++i) {
172 property->atom(otk::OBProperty::net_wm_window_type_desktop))
173 _type = Type_Desktop;
175 property->atom(otk::OBProperty::net_wm_window_type_dock))
178 property->atom(otk::OBProperty::net_wm_window_type_toolbar))
179 _type = Type_Toolbar;
181 property->atom(otk::OBProperty::net_wm_window_type_menu))
184 property->atom(otk::OBProperty::net_wm_window_type_utility))
185 _type = Type_Utility;
187 property->atom(otk::OBProperty::net_wm_window_type_splash))
190 property->atom(otk::OBProperty::net_wm_window_type_dialog))
193 property->atom(otk::OBProperty::net_wm_window_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
203 if (_type == (WindowType) -1) {
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.
208 // XXX: make this code work!
210 // _type = Type_Dialog;
217 void OBClient::getMwmHints()
219 const otk::OBProperty *property = Openbox::instance->property();
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))
230 if (num < MwmHints::elements) {
235 // retrieved the hints
236 // Mwm Hints are applied subtractively to what has already been chosen for
237 // decor and functionality
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;
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;
272 void OBClient::getArea()
274 XWindowAttributes wattrib;
275 assert(XGetWindowAttributes(otk::OBDisplay::display, _window, &wattrib));
277 _area.setRect(wattrib.x, wattrib.y, wattrib.width, wattrib.height);
278 _border_width = wattrib.border_width;
282 void OBClient::getState()
284 const otk::OBProperty *property = Openbox::instance->property();
286 _modal = _shaded = _max_horz = _max_vert = _fullscreen = _floating = false;
288 unsigned long *state;
289 unsigned long num = (unsigned) -1;
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))
297 property->atom(otk::OBProperty::net_wm_state_shaded))
300 property->atom(otk::OBProperty::net_wm_state_fullscreen))
303 property->atom(otk::OBProperty::net_wm_state_maximized_vert))
306 property->atom(otk::OBProperty::net_wm_state_maximized_horz))
315 void OBClient::getShaped()
319 if (otk::OBDisplay::shape()) {
323 XShapeQueryExtents(otk::OBDisplay::display, client.window, &_shaped, &foo,
324 &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo, &ufoo);
330 void OBClient::updateProtocols()
332 const otk::OBProperty *property = Openbox::instance->property();
337 _focus_notify = false;
338 _decorations &= ~Decor_Close;
339 _functions &= ~Func_Close;
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;
357 void OBClient::updateNormalHints()
363 _gravity = NorthWestGravity;
365 _base_x = _base_y = 0;
367 _max_x = _max_y = INT_MAX;
369 // XXX: might want to cancel any interactive resizing of the window at this
372 // get the hints from the window
373 if (XGetWMNormalHints(otk::OBDisplay::display, _window, &size, &ret)) {
374 _positioned = (size.flags & (PPosition|USPosition));
376 if (size.flags & PWinGravity)
377 _gravity = size.win_gravity;
379 if (size.flags & PMinSize) {
380 _min_x = size.min_width;
381 _min_y = size.min_height;
384 if (size.flags & PMaxSize) {
385 _max_x = size.max_width;
386 _max_y = size.max_height;
389 if (size.flags & PBaseSize) {
390 _base_x = size.base_width;
391 _base_y = size.base_height;
394 if (size.flags & PResizeInc) {
395 _inc_x = size.width_inc;
396 _inc_y = size.height_inc;
402 void OBClient::updateWMHints()
406 // assume a window takes input if it doesnt specify
410 if ((hints = XGetWMHints(otk::OBDisplay::display, _window)) != NULL) {
411 if (hints->flags & InputHint)
412 _can_focus = hints->input;
414 if (hints->flags & XUrgencyHint)
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
431 void OBClient::updateTitle()
433 const otk::OBProperty *property = Openbox::instance->property();
438 if (! property->get(_window, otk::OBProperty::net_wm_name,
439 otk::OBProperty::utf8, &_title)) {
441 property->get(_window, otk::OBProperty::wm_name,
442 otk::OBProperty::ascii, &_title);
446 _title = _("Unnamed Window");
450 void OBClient::updateIconTitle()
452 const otk::OBProperty *property = Openbox::instance->property();
457 if (! property->get(_window, otk::OBProperty::net_wm_icon_name,
458 otk::OBProperty::utf8, &_icon_title)) {
460 property->get(_window, otk::OBProperty::wm_icon_name,
461 otk::OBProperty::ascii, &_icon_title);
465 _icon_title = _("Unnamed Window");
469 void OBClient::updateClass()
471 const otk::OBProperty *property = Openbox::instance->property();
474 _app_name = _app_class = "";
476 otk::OBProperty::StringVect v;
477 unsigned long num = 2;
479 if (! property->get(_window, otk::OBProperty::wm_class,
480 otk::OBProperty::ascii, &num, &v))
483 if (num > 0) _app_name = v[0];
484 if (num > 1) _app_class = v[1];
488 void OBClient::update(const XPropertyEvent &e)
490 const otk::OBProperty *property = Openbox::instance->property();
492 if (e.atom == XA_WM_NORMAL_HINTS)
494 else if (e.atom == XA_WM_HINTS)
496 else if (e.atom == property->atom(otk::OBProperty::net_wm_name) ||
497 e.atom == property->atom(otk::OBProperty::wm_name))
499 else if (e.atom == property->atom(otk::OBProperty::net_wm_icon_name) ||
500 e.atom == property->atom(otk::OBProperty::wm_icon_name))
502 else if (e.atom == property->atom(otk::OBProperty::wm_class))
504 else if (e.atom == property->atom(otk::OBProperty::wm_protocols))
506 // XXX: transient for hint
511 void OBClient::setWMState(long state)
513 if (state == _wmstate) return; // no change
517 // XXX: cause it to iconify
520 // XXX: cause it to uniconify
527 void OBClient::setDesktop(long target)
530 //assert(target == 0xffffffff || target < MAX);
532 // XXX: move the window to the new desktop
537 void OBClient::setState(StateAction action, long data1, long data2)
539 const otk::OBProperty *property = Openbox::instance->property();
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
545 for (int i = 0; i < 2; ++i) {
546 Atom state = i == 0 ? data1 : data2;
548 if (! state) continue;
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;
555 property->atom(otk::OBProperty::net_wm_state_maximized_vert))
556 action = _max_vert ? State_Remove : State_Add;
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;
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;
569 if (action == State_Add) {
570 if (state == property->atom(otk::OBProperty::net_wm_state_modal)) {
571 if (_modal) continue;
573 // XXX: give it focus if another window has focus that shouldnt now
575 property->atom(otk::OBProperty::net_wm_state_maximized_vert)){
576 if (_max_vert) continue;
578 // XXX: resize the window etc
580 property->atom(otk::OBProperty::net_wm_state_maximized_horz)){
581 if (_max_horz) continue;
583 // XXX: resize the window etc
585 property->atom(otk::OBProperty::net_wm_state_shaded)) {
586 if (_shaded) continue;
588 // XXX: hide the client window
590 property->atom(otk::OBProperty::net_wm_state_fullscreen)) {
591 if (_fullscreen) continue;
593 // XXX: raise the window n shit
595 property->atom(otk::OBProperty::net_wm_state_floating)) {
596 if (_floating) continue;
598 // XXX: raise the window n shit
601 } else { // action == State_Remove
602 if (state == property->atom(otk::OBProperty::net_wm_state_modal)) {
603 if (!_modal) continue;
606 property->atom(otk::OBProperty::net_wm_state_maximized_vert)){
607 if (!_max_vert) continue;
609 // XXX: resize the window etc
611 property->atom(otk::OBProperty::net_wm_state_maximized_horz)){
612 if (!_max_horz) continue;
614 // XXX: resize the window etc
616 property->atom(otk::OBProperty::net_wm_state_shaded)) {
617 if (!_shaded) continue;
619 // XXX: show the client window
621 property->atom(otk::OBProperty::net_wm_state_fullscreen)) {
622 if (!_fullscreen) continue;
624 // XXX: lower the window to its proper layer
626 property->atom(otk::OBProperty::net_wm_state_floating)) {
627 if (!_floating) continue;
629 // XXX: lower the window to its proper layer
636 void OBClient::update(const XClientMessageEvent &e)
638 if (e.format != 32) return;
640 const otk::OBProperty *property = Openbox::instance->property();
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]);
652 void OBClient::setArea(const otk::Rect &area)