]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/window.cc
add iconifying
[mikachu/openbox.git] / util / epist / window.cc
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // window.cc for Epistophy - a key handler for NETWM/EWMH window managers.
3 // Copyright (c) 2002 - 2002 Ben Jansens <ben at orodu.net>
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
22
23 #ifdef    HAVE_CONFIG_H
24 #  include "../../config.h"
25 #endif // HAVE_CONFIG_H
26
27 #include <iostream>
28
29 using std::cout;
30 using std::endl;
31 using std::hex;
32 using std::dec;
33
34 #include "epist.hh"
35 #include "screen.hh"
36 #include "window.hh"
37 #include "../../src/XAtom.hh"
38
39 XWindow::XWindow(epist *epist, screen *screen, Window window)
40   : _epist(epist), _screen(screen), _xatom(epist->xatom()), _window(window) {
41
42   _unmapped = false;
43
44   XSelectInput(_epist->getXDisplay(), _window,
45                PropertyChangeMask | StructureNotifyMask);
46   updateState();
47   updateDesktop();
48   updateTitle();
49   updateClass();
50
51   _epist->addWindow(this);
52 }
53
54
55 XWindow::~XWindow() {
56   if (! _unmapped)
57     XSelectInput(_epist->getXDisplay(), _window, None);
58   _epist->removeWindow(this);
59 }
60
61
62 void XWindow::updateState() {
63   // set the defaults
64   _shaded = _iconic = _max_vert = _max_horz = false;
65   
66   unsigned long num = (unsigned) -1;
67   Atom *state;
68   if (! _xatom->getValue(_window, XAtom::net_wm_state, XAtom::atom,
69                          num, &state))
70     return;
71   for (unsigned long i = 0; i < num; ++i) {
72     if (state[i] == _xatom->getAtom(XAtom::net_wm_state_maximized_vert))
73       _max_vert = true;
74     if (state[i] == _xatom->getAtom(XAtom::net_wm_state_maximized_horz))
75       _max_horz = true;
76     if (state[i] == _xatom->getAtom(XAtom::net_wm_state_shaded))
77       _shaded = true;
78     if (state[i] == _xatom->getAtom(XAtom::net_wm_state_hidden))
79       _iconic = true;
80   }
81
82   delete [] state;
83 }
84
85
86 void XWindow::updateDesktop() {
87   if (! _xatom->getValue(_window, XAtom::net_wm_desktop, XAtom::cardinal,
88                          static_cast<unsigned long>(_desktop)))
89     _desktop = 0;
90 }
91
92
93 void XWindow::updateTitle() {
94   _title = "";
95   
96   // try netwm
97   if (! _xatom->getValue(_window, XAtom::net_wm_name, XAtom::utf8, _title)) {
98     // try old x stuff
99     _xatom->getValue(_window, XAtom::wm_name, XAtom::ansi, _title);
100   }
101
102   if (_title.empty())
103     _title = "Unnamed";
104 }
105
106
107 void XWindow::updateClass() {
108   // set the defaults
109   _app_name = _app_class = "";
110
111   XAtom::StringVect v;
112   unsigned long num = 2;
113
114   if (! _xatom->getValue(_window, XAtom::wm_class, XAtom::ansi, num, v))
115     return;
116
117   if (num > 0) _app_name = v[0];
118   if (num > 1) _app_class = v[1];
119 }
120
121
122 void XWindow::processEvent(const XEvent &e) {
123   assert(e.xany.window == _window);
124
125   switch (e.type) {
126   case PropertyNotify:
127     // a client window
128     if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_state))
129       updateState();
130     else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_desktop))
131       updateDesktop();
132     else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_name) ||
133              e.xproperty.atom == _xatom->getAtom(XAtom::wm_name))
134       updateTitle();
135     else if (e.xproperty.atom == _xatom->getAtom(XAtom::wm_class))
136       updateClass();
137     break;
138   case DestroyNotify:
139   case UnmapNotify:
140     _unmapped = true;
141     break;
142   }
143 }
144   
145
146 void XWindow::shade(const bool sh) const {
147   _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
148                             _window, (sh ? 1 : 0),
149                             _xatom->getAtom(XAtom::net_wm_state_shaded));
150 }
151
152
153 void XWindow::close() const {
154   _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_close_window,
155                             _window);
156 }
157
158
159 void XWindow::raise() const {
160   XRaiseWindow(_epist->getXDisplay(), _window);
161 }
162
163
164 void XWindow::lower() const {
165   XLowerWindow(_epist->getXDisplay(), _window);
166 }
167
168
169 void XWindow::iconify() const {
170   _xatom->sendClientMessage(_screen->rootWindow(), XAtom::wm_change_state,
171                             _window, IconicState);
172 }