2
|
1 |
/*
|
|
2 |
* Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.awt.X11;
|
|
27 |
|
|
28 |
import java.awt.datatransfer.Transferable;
|
|
29 |
|
|
30 |
import java.awt.dnd.DnDConstants;
|
|
31 |
import java.awt.dnd.InvalidDnDOperationException;
|
|
32 |
|
|
33 |
import java.util.Map;
|
|
34 |
|
|
35 |
import sun.misc.Unsafe;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* XDragSourceProtocol implementation for Motif DnD protocol.
|
|
39 |
*
|
|
40 |
* @since 1.5
|
|
41 |
*/
|
|
42 |
class MotifDnDDragSourceProtocol extends XDragSourceProtocol
|
|
43 |
implements XEventDispatcher {
|
|
44 |
|
|
45 |
private static final Unsafe unsafe = XlibWrapper.unsafe;
|
|
46 |
|
|
47 |
private long targetEnterServerTime = XlibWrapper.CurrentTime;
|
|
48 |
|
|
49 |
protected MotifDnDDragSourceProtocol(XDragSourceProtocolListener listener) {
|
|
50 |
super(listener);
|
|
51 |
XToolkit.addEventDispatcher(XWindow.getXAWTRootWindow().getWindow(), this);
|
|
52 |
}
|
|
53 |
|
|
54 |
/**
|
|
55 |
* Creates an instance associated with the specified listener.
|
|
56 |
*
|
|
57 |
* @throws NullPointerException if listener is <code>null</code>.
|
|
58 |
*/
|
|
59 |
static XDragSourceProtocol createInstance(XDragSourceProtocolListener listener) {
|
|
60 |
return new MotifDnDDragSourceProtocol(listener);
|
|
61 |
}
|
|
62 |
|
|
63 |
public String getProtocolName() {
|
|
64 |
return XDragAndDropProtocols.MotifDnD;
|
|
65 |
}
|
|
66 |
|
|
67 |
protected void initializeDragImpl(int actions, Transferable contents,
|
|
68 |
Map formatMap, long[] formats)
|
|
69 |
throws InvalidDnDOperationException,
|
|
70 |
IllegalArgumentException, XException {
|
|
71 |
|
|
72 |
long window = XDragSourceProtocol.getDragSourceWindow();
|
|
73 |
|
|
74 |
/* Write the Motif DnD initiator info on the root XWindow. */
|
|
75 |
try {
|
|
76 |
int index = MotifDnDConstants.getIndexForTargetList(formats);
|
|
77 |
|
|
78 |
MotifDnDConstants.writeDragInitiatorInfoStruct(window, index);
|
|
79 |
} catch (XException xe) {
|
|
80 |
cleanup();
|
|
81 |
throw xe;
|
|
82 |
} catch (InvalidDnDOperationException idoe) {
|
|
83 |
cleanup();
|
|
84 |
throw idoe;
|
|
85 |
}
|
|
86 |
|
|
87 |
if (!MotifDnDConstants.MotifDnDSelection.setOwner(contents, formatMap,
|
|
88 |
formats,
|
|
89 |
XlibWrapper.CurrentTime)) {
|
|
90 |
cleanup();
|
|
91 |
throw new InvalidDnDOperationException("Cannot acquire selection ownership");
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
|
95 |
/**
|
|
96 |
* Processes the specified client message event.
|
|
97 |
*
|
|
98 |
* @returns true if the event was successfully processed.
|
|
99 |
*/
|
|
100 |
public boolean processClientMessage(XClientMessageEvent xclient) {
|
|
101 |
if (xclient.get_message_type() !=
|
|
102 |
MotifDnDConstants.XA_MOTIF_DRAG_AND_DROP_MESSAGE.getAtom()) {
|
|
103 |
return false;
|
|
104 |
}
|
|
105 |
|
|
106 |
long data = xclient.get_data();
|
|
107 |
byte reason = (byte)(unsafe.getByte(data) &
|
|
108 |
MotifDnDConstants.MOTIF_MESSAGE_REASON_MASK);
|
|
109 |
byte origin = (byte)(unsafe.getByte(data) &
|
|
110 |
MotifDnDConstants.MOTIF_MESSAGE_SENDER_MASK);
|
|
111 |
byte byteOrder = unsafe.getByte(data + 1);
|
|
112 |
boolean swapNeeded = byteOrder != MotifDnDConstants.getByteOrderByte();
|
|
113 |
int action = DnDConstants.ACTION_NONE;
|
|
114 |
int x = 0;
|
|
115 |
int y = 0;
|
|
116 |
|
|
117 |
/* Only receiver messages should be handled. */
|
|
118 |
if (origin != MotifDnDConstants.MOTIF_MESSAGE_FROM_RECEIVER) {
|
|
119 |
return false;
|
|
120 |
}
|
|
121 |
|
|
122 |
switch (reason) {
|
|
123 |
case MotifDnDConstants.DROP_SITE_ENTER:
|
|
124 |
case MotifDnDConstants.DROP_SITE_LEAVE:
|
|
125 |
case MotifDnDConstants.DRAG_MOTION:
|
|
126 |
case MotifDnDConstants.OPERATION_CHANGED:
|
|
127 |
break;
|
|
128 |
default:
|
|
129 |
// Unknown reason.
|
|
130 |
return false;
|
|
131 |
}
|
|
132 |
|
|
133 |
int t = unsafe.getInt(data + 4);
|
|
134 |
if (swapNeeded) {
|
|
135 |
t = MotifDnDConstants.Swapper.swap(t);
|
|
136 |
}
|
|
137 |
long time = t;
|
|
138 |
|
|
139 |
/* Discard events from the previous receiver. */
|
|
140 |
if (targetEnterServerTime == XlibWrapper.CurrentTime ||
|
|
141 |
time < targetEnterServerTime) {
|
|
142 |
return true;
|
|
143 |
}
|
|
144 |
|
|
145 |
if (reason != MotifDnDConstants.DROP_SITE_LEAVE) {
|
|
146 |
short flags = unsafe.getShort(data + 2);
|
|
147 |
if (swapNeeded) {
|
|
148 |
flags = MotifDnDConstants.Swapper.swap(flags);
|
|
149 |
}
|
|
150 |
|
|
151 |
byte status = (byte)((flags & MotifDnDConstants.MOTIF_DND_STATUS_MASK) >>
|
|
152 |
MotifDnDConstants.MOTIF_DND_STATUS_SHIFT);
|
|
153 |
byte motif_action = (byte)((flags & MotifDnDConstants.MOTIF_DND_ACTION_MASK) >>
|
|
154 |
MotifDnDConstants.MOTIF_DND_ACTION_SHIFT);
|
|
155 |
|
|
156 |
if (status == MotifDnDConstants.MOTIF_VALID_DROP_SITE) {
|
|
157 |
action = MotifDnDConstants.getJavaActionsForMotifActions(motif_action);
|
|
158 |
} else {
|
|
159 |
action = DnDConstants.ACTION_NONE;
|
|
160 |
}
|
|
161 |
|
|
162 |
short tx = unsafe.getShort(data + 8);
|
|
163 |
short ty = unsafe.getShort(data + 10);
|
|
164 |
if (swapNeeded) {
|
|
165 |
tx = MotifDnDConstants.Swapper.swap(tx);
|
|
166 |
ty = MotifDnDConstants.Swapper.swap(ty);
|
|
167 |
}
|
|
168 |
x = tx;
|
|
169 |
y = ty;
|
|
170 |
}
|
|
171 |
|
|
172 |
getProtocolListener().handleDragReply(action, x, y);
|
|
173 |
|
|
174 |
return true;
|
|
175 |
}
|
|
176 |
|
|
177 |
public TargetWindowInfo getTargetWindowInfo(long window) {
|
|
178 |
assert XToolkit.isAWTLockHeldByCurrentThread();
|
|
179 |
|
|
180 |
WindowPropertyGetter wpg =
|
|
181 |
new WindowPropertyGetter(window,
|
|
182 |
MotifDnDConstants.XA_MOTIF_DRAG_RECEIVER_INFO,
|
|
183 |
0, 0xFFFF, false,
|
|
184 |
XlibWrapper.AnyPropertyType);
|
|
185 |
|
|
186 |
try {
|
|
187 |
int status = wpg.execute(XToolkit.IgnoreBadWindowHandler);
|
|
188 |
|
|
189 |
/*
|
|
190 |
* DragICCI.h:
|
|
191 |
*
|
|
192 |
* typedef struct _xmDragReceiverInfoStruct{
|
|
193 |
* BYTE byte_order;
|
|
194 |
* BYTE protocol_version;
|
|
195 |
* BYTE drag_protocol_style;
|
|
196 |
* BYTE pad1;
|
|
197 |
* CARD32 proxy_window B32;
|
|
198 |
* CARD16 num_drop_sites B16;
|
|
199 |
* CARD16 pad2 B16;
|
|
200 |
* CARD32 heap_offset B32;
|
|
201 |
* } xmDragReceiverInfoStruct;
|
|
202 |
*/
|
|
203 |
if (status == (int)XlibWrapper.Success && wpg.getData() != 0 &&
|
|
204 |
wpg.getActualType() != 0 && wpg.getActualFormat() == 8 &&
|
|
205 |
wpg.getNumberOfItems() >=
|
|
206 |
MotifDnDConstants.MOTIF_RECEIVER_INFO_SIZE) {
|
|
207 |
|
|
208 |
long data = wpg.getData();
|
|
209 |
byte byteOrderByte = unsafe.getByte(data);
|
|
210 |
byte dragProtocolStyle = unsafe.getByte(data + 2);
|
|
211 |
switch (dragProtocolStyle) {
|
|
212 |
case MotifDnDConstants.MOTIF_PREFER_PREREGISTER_STYLE :
|
|
213 |
case MotifDnDConstants.MOTIF_PREFER_DYNAMIC_STYLE :
|
|
214 |
case MotifDnDConstants.MOTIF_DYNAMIC_STYLE :
|
|
215 |
case MotifDnDConstants.MOTIF_PREFER_RECEIVER_STYLE :
|
|
216 |
int proxy = unsafe.getInt(data + 4);
|
|
217 |
if (byteOrderByte != MotifDnDConstants.getByteOrderByte()) {
|
|
218 |
proxy = MotifDnDConstants.Swapper.swap(proxy);
|
|
219 |
}
|
|
220 |
|
|
221 |
int protocolVersion = unsafe.getByte(data + 1);
|
|
222 |
|
|
223 |
return new TargetWindowInfo(proxy, protocolVersion);
|
|
224 |
default:
|
|
225 |
// Unsupported protocol style.
|
|
226 |
return null;
|
|
227 |
}
|
|
228 |
} else {
|
|
229 |
return null;
|
|
230 |
}
|
|
231 |
} finally {
|
|
232 |
wpg.dispose();
|
|
233 |
}
|
|
234 |
}
|
|
235 |
|
|
236 |
public void sendEnterMessage(long[] formats,
|
|
237 |
int sourceAction, int sourceActions, long time) {
|
|
238 |
assert XToolkit.isAWTLockHeldByCurrentThread();
|
|
239 |
assert getTargetWindow() != 0;
|
|
240 |
assert formats != null;
|
|
241 |
|
|
242 |
targetEnterServerTime = time;
|
|
243 |
|
|
244 |
XClientMessageEvent msg = new XClientMessageEvent();
|
|
245 |
try {
|
|
246 |
msg.set_type(XlibWrapper.ClientMessage);
|
|
247 |
msg.set_window(getTargetWindow());
|
|
248 |
msg.set_format(8);
|
|
249 |
msg.set_message_type(MotifDnDConstants.XA_MOTIF_DRAG_AND_DROP_MESSAGE.getAtom());
|
|
250 |
|
|
251 |
long data = msg.get_data();
|
|
252 |
int flags =
|
|
253 |
(MotifDnDConstants.getMotifActionsForJavaActions(sourceAction) <<
|
|
254 |
MotifDnDConstants.MOTIF_DND_ACTION_SHIFT) |
|
|
255 |
(MotifDnDConstants.getMotifActionsForJavaActions(sourceActions) <<
|
|
256 |
MotifDnDConstants.MOTIF_DND_ACTIONS_SHIFT);
|
|
257 |
|
|
258 |
unsafe.putByte(data,
|
|
259 |
(byte)(MotifDnDConstants.TOP_LEVEL_ENTER |
|
|
260 |
MotifDnDConstants.MOTIF_MESSAGE_FROM_INITIATOR));
|
|
261 |
unsafe.putByte(data + 1,
|
|
262 |
MotifDnDConstants.getByteOrderByte());
|
|
263 |
unsafe.putShort(data + 2, (short)flags);
|
|
264 |
unsafe.putInt(data + 4, (int)time);
|
|
265 |
unsafe.putInt(data + 8, (int)XDragSourceProtocol.getDragSourceWindow());
|
|
266 |
unsafe.putInt(data + 12, (int)MotifDnDConstants.XA_MOTIF_ATOM_0.getAtom());
|
|
267 |
|
|
268 |
XlibWrapper.XSendEvent(XToolkit.getDisplay(),
|
|
269 |
getTargetProxyWindow(),
|
|
270 |
false, XlibWrapper.NoEventMask,
|
|
271 |
msg.pData);
|
|
272 |
} finally {
|
|
273 |
msg.dispose();
|
|
274 |
}
|
|
275 |
}
|
|
276 |
|
|
277 |
public void sendMoveMessage(int xRoot, int yRoot,
|
|
278 |
int sourceAction, int sourceActions, long time) {
|
|
279 |
assert XToolkit.isAWTLockHeldByCurrentThread();
|
|
280 |
assert getTargetWindow() != 0;
|
|
281 |
|
|
282 |
XClientMessageEvent msg = new XClientMessageEvent();
|
|
283 |
try {
|
|
284 |
msg.set_type(XlibWrapper.ClientMessage);
|
|
285 |
msg.set_window(getTargetWindow());
|
|
286 |
msg.set_format(8);
|
|
287 |
msg.set_message_type(MotifDnDConstants.XA_MOTIF_DRAG_AND_DROP_MESSAGE.getAtom());
|
|
288 |
|
|
289 |
long data = msg.get_data();
|
|
290 |
int flags =
|
|
291 |
(MotifDnDConstants.getMotifActionsForJavaActions(sourceAction) <<
|
|
292 |
MotifDnDConstants.MOTIF_DND_ACTION_SHIFT) |
|
|
293 |
(MotifDnDConstants.getMotifActionsForJavaActions(sourceActions) <<
|
|
294 |
MotifDnDConstants.MOTIF_DND_ACTIONS_SHIFT);
|
|
295 |
|
|
296 |
unsafe.putByte(data,
|
|
297 |
(byte)(MotifDnDConstants.DRAG_MOTION |
|
|
298 |
MotifDnDConstants.MOTIF_MESSAGE_FROM_INITIATOR));
|
|
299 |
unsafe.putByte(data + 1,
|
|
300 |
MotifDnDConstants.getByteOrderByte());
|
|
301 |
unsafe.putShort(data + 2, (short)flags);
|
|
302 |
unsafe.putInt(data + 4, (int)time);
|
|
303 |
unsafe.putShort(data + 8, (short)xRoot);
|
|
304 |
unsafe.putShort(data + 10, (short)yRoot);
|
|
305 |
|
|
306 |
XlibWrapper.XSendEvent(XToolkit.getDisplay(),
|
|
307 |
getTargetProxyWindow(),
|
|
308 |
false, XlibWrapper.NoEventMask,
|
|
309 |
msg.pData);
|
|
310 |
} finally {
|
|
311 |
msg.dispose();
|
|
312 |
}
|
|
313 |
}
|
|
314 |
|
|
315 |
public void sendLeaveMessage(long time) {
|
|
316 |
assert XToolkit.isAWTLockHeldByCurrentThread();
|
|
317 |
assert getTargetWindow() != 0;
|
|
318 |
|
|
319 |
XClientMessageEvent msg = new XClientMessageEvent();
|
|
320 |
try {
|
|
321 |
msg.set_type(XlibWrapper.ClientMessage);
|
|
322 |
msg.set_window(getTargetWindow());
|
|
323 |
msg.set_format(8);
|
|
324 |
msg.set_message_type(MotifDnDConstants.XA_MOTIF_DRAG_AND_DROP_MESSAGE.getAtom());
|
|
325 |
|
|
326 |
long data = msg.get_data();
|
|
327 |
|
|
328 |
unsafe.putByte(data,
|
|
329 |
(byte)(MotifDnDConstants.TOP_LEVEL_LEAVE |
|
|
330 |
MotifDnDConstants.MOTIF_MESSAGE_FROM_INITIATOR));
|
|
331 |
unsafe.putByte(data + 1,
|
|
332 |
MotifDnDConstants.getByteOrderByte());
|
|
333 |
unsafe.putShort(data + 2, (short)0);
|
|
334 |
unsafe.putInt(data + 4, (int)time);
|
|
335 |
unsafe.putInt(data + 8, (int)XDragSourceProtocol.getDragSourceWindow());
|
|
336 |
|
|
337 |
XlibWrapper.XSendEvent(XToolkit.getDisplay(),
|
|
338 |
getTargetProxyWindow(),
|
|
339 |
false, XlibWrapper.NoEventMask,
|
|
340 |
msg.pData);
|
|
341 |
} finally {
|
|
342 |
msg.dispose();
|
|
343 |
}
|
|
344 |
}
|
|
345 |
|
|
346 |
protected void sendDropMessage(int xRoot, int yRoot,
|
|
347 |
int sourceAction, int sourceActions,
|
|
348 |
long time) {
|
|
349 |
assert XToolkit.isAWTLockHeldByCurrentThread();
|
|
350 |
assert getTargetWindow() != 0;
|
|
351 |
|
|
352 |
/*
|
|
353 |
* Motif drop sites expect TOP_LEVEL_LEAVE before DROP_START.
|
|
354 |
*/
|
|
355 |
sendLeaveMessage(time);
|
|
356 |
|
|
357 |
XClientMessageEvent msg = new XClientMessageEvent();
|
|
358 |
try {
|
|
359 |
msg.set_type(XlibWrapper.ClientMessage);
|
|
360 |
msg.set_window(getTargetWindow());
|
|
361 |
msg.set_format(8);
|
|
362 |
msg.set_message_type(MotifDnDConstants.XA_MOTIF_DRAG_AND_DROP_MESSAGE.getAtom());
|
|
363 |
|
|
364 |
long data = msg.get_data();
|
|
365 |
int flags =
|
|
366 |
(MotifDnDConstants.getMotifActionsForJavaActions(sourceAction) <<
|
|
367 |
MotifDnDConstants.MOTIF_DND_ACTION_SHIFT) |
|
|
368 |
(MotifDnDConstants.getMotifActionsForJavaActions(sourceActions) <<
|
|
369 |
MotifDnDConstants.MOTIF_DND_ACTIONS_SHIFT);
|
|
370 |
|
|
371 |
unsafe.putByte(data,
|
|
372 |
(byte)(MotifDnDConstants.DROP_START |
|
|
373 |
MotifDnDConstants.MOTIF_MESSAGE_FROM_INITIATOR));
|
|
374 |
unsafe.putByte(data + 1,
|
|
375 |
MotifDnDConstants.getByteOrderByte());
|
|
376 |
unsafe.putShort(data + 2, (short)flags);
|
|
377 |
unsafe.putInt(data + 4, (int)time);
|
|
378 |
unsafe.putShort(data + 8, (short)xRoot);
|
|
379 |
unsafe.putShort(data + 10, (short)yRoot);
|
|
380 |
unsafe.putInt(data + 12, (int)MotifDnDConstants.XA_MOTIF_ATOM_0.getAtom());
|
|
381 |
unsafe.putInt(data + 16, (int)XDragSourceProtocol.getDragSourceWindow());
|
|
382 |
|
|
383 |
XlibWrapper.XSendEvent(XToolkit.getDisplay(),
|
|
384 |
getTargetProxyWindow(),
|
|
385 |
false, XlibWrapper.NoEventMask,
|
|
386 |
msg.pData);
|
|
387 |
} finally {
|
|
388 |
msg.dispose();
|
|
389 |
}
|
|
390 |
}
|
|
391 |
|
|
392 |
public boolean processProxyModeEvent(XClientMessageEvent xclient,
|
|
393 |
long sourceWindow) {
|
|
394 |
// Motif DnD for XEmbed is not implemented.
|
|
395 |
return false;
|
|
396 |
}
|
|
397 |
|
|
398 |
public void cleanupTargetInfo() {
|
|
399 |
super.cleanupTargetInfo();
|
|
400 |
targetEnterServerTime = XlibWrapper.CurrentTime;
|
|
401 |
}
|
|
402 |
|
|
403 |
public void dispatchEvent(XEvent ev) {
|
|
404 |
switch (ev.get_type()) {
|
|
405 |
case XlibWrapper.SelectionRequest:
|
|
406 |
XSelectionRequestEvent xsre = ev.get_xselectionrequest();
|
|
407 |
long atom = xsre.get_selection();
|
|
408 |
|
|
409 |
if (atom == MotifDnDConstants.XA_MOTIF_ATOM_0.getAtom()) {
|
|
410 |
long target = xsre.get_target();
|
|
411 |
if (target == MotifDnDConstants.XA_XmTRANSFER_SUCCESS.getAtom()) {
|
|
412 |
getProtocolListener().handleDragFinished(true);
|
|
413 |
} else if (target == MotifDnDConstants.XA_XmTRANSFER_FAILURE.getAtom()) {
|
|
414 |
getProtocolListener().handleDragFinished(false);
|
|
415 |
}
|
|
416 |
}
|
|
417 |
break;
|
|
418 |
}
|
|
419 |
}
|
|
420 |
}
|