all repos — openbox @ d017db67e4d77fd7af3f070803a773df0a5768eb

openbox fork - make it a bit more like ryudo

src/client.cc (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-

#include "client.hh"
#include "screen.hh"
#include "openbox.hh"
#include "otk/display.hh"
#include "otk/property.hh"

extern "C" {
#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include <assert.h>

#include "gettext.h"
#define _(str) gettext(str)
}

namespace ob {

OBClient::OBClient(BScreen *screen, Window window)
  : _screen(screen), _window(window)
{
  assert(_screen);
  assert(window);

  // initialize vars to false/invalid values
  _group = 0;
  _gravity = _base_x = _base_y = _inc_x = _inc_y = _max_x = _max_y = _min_x =
    _min_y = -1;
  _state = -1;
  _type = Type_Normal;
  _desktop = 0xffffffff - 1;
  _can_focus = _urgent = _focus_notify = _shaped = _modal = _shaded =
    _max_horz = _max_vert = _fullscreen = _floating = false;
  
  
  // update EVERYTHING the first time!!
}

OBClient::~OBClient()
{
}


void OBClient::updateNormalHints()
{
  XSizeHints size;
  long ret;

  // defaults
  _gravity = NorthWestGravity;
  _inc_x = _inc_y = 1;
  _base_x = _base_y = 0;

  // get the hints from the window
  if (XGetWMNormalHints(otk::OBDisplay::display, _window, &size, &ret)) {
    if (size.flags & PWinGravity)
      _gravity = size.win_gravity;
    if (size.flags & PBaseSize) {
      _base_x = size.base_width;
      _base_y = size.base_height;
    }
    if (size.flags & PResizeInc) {
      _inc_x = size.width_inc;
      _inc_y = size.height_inc;
    }
  }
}


void OBClient::updateWMHints()
{
  XWMHints *hints;

  // assume a window takes input if it doesnt specify
  _can_focus = true;
  _urgent = false;
  
  if ((hints = XGetWMHints(otk::OBDisplay::display, _window)) != NULL) {
    if (hints->flags & InputHint)
      _can_focus = hints->input;
    if (hints->flags & XUrgencyHint)
      _urgent = true;
    if (hints->flags & WindowGroupHint)
      if (hints->window_group != _group) {
        // XXX: remove from the old group if there was one
        _group = hints->window_group;
        // XXX: do stuff with the group
      }
    XFree(hints);
  }
}


void OBClient::updateTitle()
{
  const otk::OBProperty *property = Openbox::instance->property();

  _title = "";
  
  // try netwm
  if (! property->get(_window, otk::OBProperty::net_wm_name,
                      otk::OBProperty::utf8, &_title)) {
    // try old x stuff
    property->get(_window, otk::OBProperty::wm_name,
                  otk::OBProperty::ascii, &_title);
  }

  if (_title.empty())
    _title = _("Unnamed Window");
}


void OBClient::updateClass()
{
  const otk::OBProperty *property = Openbox::instance->property();

  // set the defaults
  _app_name = _app_class = "";

  otk::OBProperty::StringVect v;
  unsigned long num = 2;

  if (! property->get(_window, otk::OBProperty::wm_class,
                      otk::OBProperty::ascii, &num, &v))
    return;

  if (num > 0) _app_name = v[0];
  if (num > 1) _app_class = v[1];
}


void OBClient::update(const XPropertyEvent &e)
{
  const otk::OBProperty *property = Openbox::instance->property();

  if (e.atom == XA_WM_NORMAL_HINTS)
    updateNormalHints();
  else if (e.atom == XA_WM_HINTS)
    updateWMHints();
  else if (e.atom == property->atom(otk::OBProperty::net_wm_name) ||
           e.atom == property->atom(otk::OBProperty::wm_name) ||
           e.atom == property->atom(otk::OBProperty::net_wm_icon_name) ||
           e.atom == property->atom(otk::OBProperty::wm_icon_name))
    updateTitle();
  else if (e.atom == property->atom(otk::OBProperty::wm_class))
    updateClass();
}


void OBClient::setWMState(long state)
{
  if (state == _state) return; // no change
  
  switch (state) {
  case IconicState:
    // XXX: cause it to iconify
    break;
  case NormalState:
    // XXX: cause it to uniconify
    break;
  }
  _state = state;
}


void OBClient::setDesktop(long target)
{
  assert(target >= 0);
  //assert(target == 0xffffffff || target < MAX);
  
  // XXX: move the window to the new desktop
  _desktop = target;
}


void OBClient::setState(StateAction action, long data1, long data2)
{
  const otk::OBProperty *property = Openbox::instance->property();

  if (!(action == State_Add || action == State_Remove ||
        action == State_Toggle))
    return; // an invalid action was passed to the client message, ignore it

  for (int i = 0; i < 2; ++i) {
    Atom state = i == 0 ? data1 : data2;
    
    if (! state) continue;

    // if toggling, then pick whether we're adding or removing
    if (action == State_Toggle) {
      if (state == property->atom(otk::OBProperty::net_wm_state_modal))
        action = _modal ? State_Remove : State_Add;
      else if (state ==
               property->atom(otk::OBProperty::net_wm_state_maximized_vert))
        action = _max_vert ? State_Remove : State_Add;
      else if (state ==
               property->atom(otk::OBProperty::net_wm_state_maximized_horz))
        action = _max_horz ? State_Remove : State_Add;
      else if (state == property->atom(otk::OBProperty::net_wm_state_shaded))
        action = _shaded ? State_Remove : State_Add;
      else if (state ==
               property->atom(otk::OBProperty::net_wm_state_fullscreen))
        action = _fullscreen ? State_Remove : State_Add;
      else if (state == property->atom(otk::OBProperty::net_wm_state_floating))
        action = _floating ? State_Remove : State_Add;
    }
    
    if (action == State_Add) {
      if (state == property->atom(otk::OBProperty::net_wm_state_modal)) {
        if (_modal) continue;
        _modal = true;
        // XXX: give it focus if another window has focus that shouldnt now
      } else if (state ==
                 property->atom(otk::OBProperty::net_wm_state_maximized_vert)){
        if (_max_vert) continue;
        _max_vert = true;
        // XXX: resize the window etc
      } else if (state ==
                 property->atom(otk::OBProperty::net_wm_state_maximized_horz)){
        if (_max_horz) continue;
        _max_horz = true;
        // XXX: resize the window etc
      } else if (state ==
                 property->atom(otk::OBProperty::net_wm_state_shaded)) {
        if (_shaded) continue;
        _shaded = true;
        // XXX: hide the client window
      } else if (state ==
                 property->atom(otk::OBProperty::net_wm_state_fullscreen)) {
        if (_fullscreen) continue;
        _fullscreen = true;
        // XXX: raise the window n shit
      } else if (state ==
                 property->atom(otk::OBProperty::net_wm_state_floating)) {
        if (_floating) continue;
        _floating = true;
        // XXX: raise the window n shit
      }

    } else { // action == State_Remove
      if (state == property->atom(otk::OBProperty::net_wm_state_modal)) {
        if (!_modal) continue;
        _modal = false;
      } else if (state ==
                 property->atom(otk::OBProperty::net_wm_state_maximized_vert)){
        if (!_max_vert) continue;
        _max_vert = false;
        // XXX: resize the window etc
      } else if (state ==
                 property->atom(otk::OBProperty::net_wm_state_maximized_horz)){
        if (!_max_horz) continue;
        _max_horz = false;
        // XXX: resize the window etc
      } else if (state ==
                 property->atom(otk::OBProperty::net_wm_state_shaded)) {
        if (!_shaded) continue;
        _shaded = false;
        // XXX: show the client window
      } else if (state ==
                 property->atom(otk::OBProperty::net_wm_state_fullscreen)) {
        if (!_fullscreen) continue;
        _fullscreen = false;
        // XXX: lower the window to its proper layer
      } else if (state ==
                 property->atom(otk::OBProperty::net_wm_state_floating)) {
        if (!_floating) continue;
        _floating = false;
        // XXX: lower the window to its proper layer
      }
    }
  }
}


void OBClient::update(const XClientMessageEvent &e)
{
  if (e.format != 32) return;

  const otk::OBProperty *property = Openbox::instance->property();
  
  if (e.message_type == property->atom(otk::OBProperty::wm_change_state))
    setWMState(e.data.l[0]);
  else if (e.message_type ==
             property->atom(otk::OBProperty::net_wm_desktop))
    setDesktop(e.data.l[0]);
  else if (e.message_type == property->atom(otk::OBProperty::net_wm_state))
    setState((StateAction)e.data.l[0], e.data.l[1], e.data.l[2]);
}

}