-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathicccm2.c
248 lines (221 loc) · 6.25 KB
/
icccm2.c
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
/* -*-c-*- */
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see: <http://www.gnu.org/licenses/>
*/
#include "config.h"
#include <stdio.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <X11/Xatom.h>
#include "fvwm.h"
#include "externs.h"
#include "execcontext.h"
#include "misc.h"
#include "screen.h"
#include "eventmask.h"
Time managing_since;
Atom _XA_WM_SX;
Atom _XA_MANAGER;
Atom _XA_ATOM_PAIR;
Atom _XA_WM_COLORMAP_NOTIFY;
#define _XA_TARGETS conversion_targets[0]
#define _XA_MULTIPLE conversion_targets[1]
#define _XA_TIMESTAMP conversion_targets[2]
#define _XA_VERSION conversion_targets[3]
#define MAX_TARGETS 4
long conversion_targets[MAX_TARGETS];
long icccm_version[] = { 2, 0 };
void
SetupICCCM2(Bool replace_wm)
{
Window running_wm_win;
XSetWindowAttributes attr;
XEvent xev;
XClientMessageEvent ev;
char wm_sx[20];
sprintf(wm_sx, "WM_S%lu", Scr.screen);
_XA_WM_SX = XInternAtom(dpy, wm_sx, False);
_XA_MANAGER = XInternAtom(dpy, "MANAGER", False);
_XA_ATOM_PAIR = XInternAtom(dpy, "ATOM_PAIR", False);
_XA_TARGETS = XInternAtom(dpy, "TARGETS", False);
_XA_MULTIPLE = XInternAtom(dpy, "MULTIPLE", False);
_XA_TIMESTAMP = XInternAtom(dpy, "TIMESTAMP", False);
_XA_VERSION = XInternAtom(dpy, "VERSION", False);
_XA_WM_COLORMAP_NOTIFY = XInternAtom(dpy, "WM_COLORMAP_NOTIFY", False);
/* Check for a running ICCCM 2.0 compliant WM */
running_wm_win = XGetSelectionOwner(dpy, _XA_WM_SX);
if (running_wm_win != None)
{
if (!replace_wm)
{
fvwm_debug(__func__,
"another ICCCM 2.0 compliant WM is running,"
" try -replace");
exit(1);
}
/* We need to know when the old manager is gone.
Thus we wait until it destroys running_wm_win. */
attr.event_mask = StructureNotifyMask;
XChangeWindowAttributes(
dpy, running_wm_win, CWEventMask, &attr);
}
/* We are not yet in the event loop, thus fev_get_evtime() will not
* be ready. Have to get a timestamp manually by provoking a
* PropertyNotify. */
managing_since = get_server_time();
XSetSelectionOwner(dpy, _XA_WM_SX, Scr.NoFocusWin, managing_since);
if (XGetSelectionOwner(dpy, _XA_WM_SX) != Scr.NoFocusWin)
{
fvwm_debug(__func__,
"failed to acquire selection ownership");
exit(1);
}
/* Announce ourself as the new wm */
ev.type = ClientMessage;
ev.window = Scr.Root;
ev.message_type = _XA_MANAGER;
ev.format = 32;
ev.data.l[0] = managing_since;
ev.data.l[1] = _XA_WM_SX;
FSendEvent(dpy, Scr.Root, False, StructureNotifyMask,(XEvent*)&ev);
if (running_wm_win != None) {
/* Wait for the old wm to finish. */
/* FIXME: need a timeout here. */
do {
FWindowEvent(
dpy, running_wm_win, StructureNotifyMask, &xev);
} while (xev.type != DestroyNotify);
}
/* restore NoFocusWin event mask */
attr.event_mask = XEVMASK_NOFOCUSW;
XChangeWindowAttributes(dpy, Scr.NoFocusWin, CWEventMask, &attr);
return;
}
/* We must make sure that we have released SubstructureRedirect
before we destroy manager_win, so that another wm can start
successfully. */
void
CloseICCCM2(void)
{
XSelectInput(dpy, Scr.Root, NoEventMask);
XFlush(dpy);
return;
}
/* FIXME: property change actually succeeded */
static Bool
convertProperty(Window w, Atom target, Atom property)
{
if (target == _XA_TARGETS)
{
XChangeProperty(
dpy, w, property, XA_ATOM, 32, PropModeReplace,
(unsigned char *)conversion_targets, MAX_TARGETS);
}
else if (target == _XA_TIMESTAMP)
{
long local_managing_since;
local_managing_since = managing_since;
XChangeProperty(
dpy, w, property, XA_INTEGER, 32, PropModeReplace,
(unsigned char *)&local_managing_since, 1);
}
else if (target == _XA_VERSION)
{
XChangeProperty(
dpy, w, property, XA_INTEGER, 32, PropModeReplace,
(unsigned char *)icccm_version, 2);
}
else
{
return False;
}
/* FIXME: This is ugly. We should rather select for
PropertyNotify on the window, return to the main loop,
and send the SelectionNotify once we are sure the property
has arrived. Problem: this needs a list of pending
SelectionNotifys. */
XFlush(dpy);
return True;
}
void
icccm2_handle_selection_request(const XEvent *e)
{
Atom type;
unsigned long *adata;
int i, format;
unsigned long num, rest;
unsigned char *data;
XSelectionRequestEvent ev = e->xselectionrequest;
XSelectionEvent reply;
reply.type = SelectionNotify;
reply.display = dpy;
reply.requestor = ev.requestor;
reply.selection = ev.selection;
reply.target = ev.target;
reply.property = None;
reply.time = ev.time;
if (ev.target == _XA_MULTIPLE)
{
if (ev.property != None)
{
XGetWindowProperty(
dpy, ev.requestor, ev.property, 0L, 256L, False,
_XA_ATOM_PAIR, &type, &format, &num, &rest,
&data);
/* FIXME: to be 100% correct, should deal with
* rest > 0, but since we have 4 possible targets, we
* will hardly ever meet multiple requests with a
* length > 8
*/
adata = (unsigned long *)data;
for(i = 0; i < num; i += 2)
{
if (!convertProperty(
ev.requestor, adata[i],
adata[i+1]))
{
adata[i+1] = None;
}
}
XChangeProperty(
dpy, ev.requestor, ev.property, _XA_ATOM_PAIR,
32, PropModeReplace, data, num);
XFree(data);
}
}
else
{
if (ev.property == None)
{
ev.property = ev.target;
}
if (convertProperty(ev.requestor, ev.target, ev.property))
{
reply.property = ev.property;
}
}
FSendEvent(dpy, ev.requestor, False, 0L,(XEvent*)&reply);
XFlush(dpy);
return;
}
/* If another wm is requesting ownership of the selection,
we receive a SelectionClear event. In that case, we have to
release all resources and destroy manager_win. Done() calls
CloseICCCM2() after undecorating all windows. */
void
icccm2_handle_selection_clear(void)
{
Done(0, NULL);
return;
}