1 // ---------------------------------------------------------------------------------
2 //
3 //  Copyright (C) 2003-2013 Fons Adriaensen <fons@linuxaudio.org>
4 //
5 //  This program is free software; you can redistribute it and/or modify
6 //  it under the terms of the GNU Lesser General Public License as published
7 //  by the Free Software Foundation; either version 2 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU Lesser General Public License for more details.
14 //
15 //  You should have received a copy of the GNU Lesser General Public
16 //  License along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 //
19 // ---------------------------------------------------------------------------------
20 
21 
22 #include "clxclient.h"
23 
24 
position(int x,int y)25 void X_hints::position (int x, int y)
26 {
27     _sh.flags |= PPosition;
28     _sh.x = x;
29     _sh.y = y;
30 }
31 
32 
size(int x,int y)33 void X_hints::size (int x, int y)
34 {
35     _sh.flags |= PSize;
36     _sh.width  = x;
37     _sh.height = y;
38 }
39 
40 
minsize(int x,int y)41 void X_hints::minsize (int x, int y)
42 {
43     _sh.flags |= PMinSize;
44     _sh.min_width  = x;
45     _sh.min_height = y;
46 }
47 
48 
maxsize(int x,int y)49 void X_hints::maxsize (int x, int y)
50 {
51     _sh.flags |= PMaxSize;
52     _sh.max_width  = x;
53     _sh.max_height = y;
54 }
55 
56 
sizeinc(int x,int y)57 void X_hints::sizeinc (int x, int y)
58 {
59     _sh.flags |= PResizeInc;
60     _sh.width_inc  = x;
61     _sh.height_inc = y;
62 }
63 
64 
input(int input)65 void X_hints::input (int input)
66 {
67     _mh.flags |= InputHint;
68     _mh.input = input;
69 }
70 
71 
state(int state)72 void X_hints::state (int state)
73 {
74     _mh.flags |= StateHint;
75     _mh.initial_state = state;
76 }
77 
group(Window group)78 void X_hints::group (Window group)
79 {
80     _mh.flags |= WindowGroupHint;
81     _mh.window_group = group;
82 }
83 
84 
85 
86 
X_window(X_display * disp)87 X_window::X_window (X_display *disp) :
88     _ebits (0), _disp (disp), _pwin (0), _next (0), _list (0)
89 {
90     _wind = DefaultRootWindow (disp->_dpy);
91 }
92 
93 
X_window(X_window * pwin,int xpos,int ypos,int xsize,int ysize,unsigned long bgcol,unsigned long bdcol,int bdpix)94 X_window::X_window (X_window *pwin, int xpos, int ypos, int xsize, int ysize,
95 		    unsigned long bgcol, unsigned long bdcol, int bdpix) :
96     _ebits (0), _disp (pwin->_disp), _pwin (pwin), _next (pwin->_list), _list (0)
97 {
98     _pwin->_list = this;
99     _wind = XCreateSimpleWindow (_disp->_dpy, pwin->_wind,
100                                  xpos, ypos, xsize, ysize,
101                                  bdpix, bdcol, bgcol);
102 }
103 
104 
~X_window(void)105 X_window::~X_window (void)
106 {
107     X_window *T;
108 
109     while (_list) delete _list;
110     if (_pwin)
111     {
112         T = _pwin->_list;
113         if (T == this) _pwin->_list = _next;
114         else
115 	{
116 	    while (T && T->_next != this) T = T->_next;
117             if (T) T->_next = _next;
118 	}
119         XDestroyWindow (_disp->_dpy, _wind);
120         XFlush (dpy ());
121     }
122 }
123 
124 
find(Window w)125 X_window *X_window::find (Window w)
126 {
127     X_window *T, *W;
128 
129     if (_wind == w) return this;
130     for (T = _list, W = 0; T && ! (W = T->find (w)); T = T->_next);
131     return W;
132 }
133 
134 
x_resize(int xs,int ys) const135 int X_window::x_resize (int xs, int ys) const
136 {
137     return XResizeWindow (_disp->_dpy, _wind, xs, ys);
138 }
139 
140 
x_move(int xp,int yp) const141 int X_window::x_move (int xp, int yp) const
142 {
143     return XMoveWindow (_disp->_dpy, _wind, xp, yp);
144 }
145 
146 
x_moveresize(int xp,int yp,int xs,int ys) const147 int X_window::x_moveresize (int xp, int yp, int xs, int ys) const
148 {
149     return XMoveResizeWindow (_disp->_dpy, _wind, xp, yp, xs, ys);
150 }
151 
152 
x_set_attrib(unsigned long mask,XSetWindowAttributes * attr) const153 int X_window::x_set_attrib (unsigned long mask, XSetWindowAttributes *attr) const
154 {
155     return XChangeWindowAttributes (_disp->_dpy, _wind, mask, attr);
156 }
157 
158 
x_set_win_gravity(int gravity) const159 int X_window::x_set_win_gravity (int gravity) const
160 {
161     XSetWindowAttributes attr;
162 
163     attr.win_gravity = gravity;
164     return XChangeWindowAttributes (_disp->_dpy, _wind, CWWinGravity, &attr);
165 }
166 
167 
x_set_bit_gravity(int gravity) const168 int X_window::x_set_bit_gravity (int gravity) const
169 {
170     XSetWindowAttributes attr;
171 
172     attr.bit_gravity = gravity;
173     return XChangeWindowAttributes (_disp->_dpy, _wind, CWBitGravity, &attr);
174 }
175 
176 
x_add_events(unsigned long events)177 int X_window::x_add_events (unsigned long events)
178 {
179     _ebits |= events;
180     return XSelectInput (_disp->_dpy, _wind, _ebits);
181 }
182 
183 
x_rem_events(unsigned long events)184 int X_window::x_rem_events (unsigned long events)
185 {
186     _ebits &= ~events;
187     return XSelectInput (_disp->_dpy, _wind, _ebits);
188 }
189 
190 
x_set_title(const char * title)191 int X_window::x_set_title (const char *title)
192 {
193     return XStoreName (_disp->_dpy, _wind, title);
194 }
195 
196 
x_set_background(unsigned long color)197 int X_window::x_set_background (unsigned long color)
198 {
199     return XSetWindowBackground (_disp->_dpy, _wind, color);
200 }
201 
202 
x_apply(X_hints * hints)203 void X_window::x_apply (X_hints *hints)
204 {
205     if (hints->_sh.flags) XSetWMNormalHints (_disp->_dpy, _wind, &(hints->_sh));
206     if (hints->_mh.flags) XSetWMHints (_disp->_dpy, _wind, &(hints->_mh));
207     XSetClassHint (_disp->_dpy, _wind, &(hints->_ch));
208     hints->_sh.flags = 0;
209     hints->_mh.flags = 0;
210 }
211 
212 
213 
X_rootwin(X_display * disp)214 X_rootwin::X_rootwin (X_display *disp) :
215     X_window (disp),
216     _object (0)
217 {
218     _disp->_xft = XftDrawCreate (_disp->_dpy, _wind, _disp->_dvi, _disp->_dcm);
219 }
220 
221 
~X_rootwin(void)222 X_rootwin::~X_rootwin (void)
223 {
224 //    if (_disp->_xft) XftDrawDestroy (_disp->_xft);
225 }
226 
227 
check_event(Display * dpy,XEvent * ev,char * arg)228 static Bool check_event (Display *dpy, XEvent *ev, char *arg)
229 {
230     return True;
231 }
232 
233 
handle_event(void)234 void X_rootwin::handle_event (void)
235 {
236     XEvent E;
237     while (XCheckIfEvent (_disp->_dpy, &E, &check_event, 0))
238     {
239 	handle_event (&E);
240     }
241 }
242 
243 
handle_event(XEvent * E)244 void X_rootwin::handle_event (XEvent *E)
245 {
246     X_window   *W;
247     Window      k;
248 
249     k = ((XAnyEvent *) E)->window;
250 //    if (XFilterEvent (E, k)) return;
251     if (_object && _window == k)
252     {
253         _object->handle_event (E);
254     }
255     else
256     {
257         W = find (k);
258         if (W && W != this)
259 	{
260             W->handle_event (E);
261 	    _window = k;
262 	    _object = W;
263 	}
264     }
265     XFlush (_disp->_dpy);
266 }
267 
268 
269