author | yan |
Mon, 06 Oct 2008 16:45:00 +0400 | |
changeset 1966 | 12a51fb0db0d |
parent 439 | 3488710b02f8 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
110
dc692e475ed0
6607163: Linux: Cannot copy image from Java to OpenOffice
son
parents:
2
diff
changeset
|
2 |
* Copyright 2003-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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.io.ByteArrayOutputStream; |
|
31 |
import java.io.IOException; |
|
32 |
||
33 |
import java.util.Hashtable; |
|
34 |
import java.util.Map; |
|
35 |
||
36 |
import sun.awt.AppContext; |
|
37 |
import sun.awt.SunToolkit; |
|
38 |
import sun.awt.UNIXToolkit; |
|
39 |
||
40 |
import sun.awt.datatransfer.DataTransferer; |
|
41 |
||
42 |
/** |
|
43 |
* A class which interfaces with the X11 selection service. |
|
44 |
*/ |
|
117 | 45 |
public final class XSelection { |
2 | 46 |
|
47 |
/* Maps atoms to XSelection instances. */ |
|
48 |
private static final Hashtable<XAtom, XSelection> table = new Hashtable<XAtom, XSelection>(); |
|
49 |
/* Prevents from parallel selection data request processing. */ |
|
50 |
private static final Object lock = new Object(); |
|
51 |
/* The property in which the owner should place the requested data. */ |
|
52 |
private static final XAtom selectionPropertyAtom = XAtom.get("XAWT_SELECTION"); |
|
53 |
/* The maximal length of the property data. */ |
|
54 |
public static final long MAX_LENGTH = 1000000; |
|
55 |
/* |
|
56 |
* The maximum data size for ChangeProperty request. |
|
57 |
* 100 is for the structure prepended to the request. |
|
58 |
*/ |
|
59 |
public static final int MAX_PROPERTY_SIZE; |
|
60 |
static { |
|
61 |
XToolkit.awtLock(); |
|
62 |
try { |
|
63 |
MAX_PROPERTY_SIZE = |
|
64 |
(int)(XlibWrapper.XMaxRequestSize(XToolkit.getDisplay()) * 4 - 100); |
|
65 |
} finally { |
|
66 |
XToolkit.awtUnlock(); |
|
67 |
} |
|
68 |
} |
|
69 |
||
70 |
/* The PropertyNotify event handler for incremental data transfer. */ |
|
71 |
private static final XEventDispatcher incrementalTransferHandler = |
|
72 |
new IncrementalTransferHandler(); |
|
73 |
/* The context for the current request - protected with awtLock. */ |
|
74 |
private static WindowPropertyGetter propertyGetter = null; |
|
75 |
||
76 |
// The orders of the lock acquisition: |
|
77 |
// XClipboard -> XSelection -> awtLock. |
|
78 |
// lock -> awtLock. |
|
79 |
||
80 |
/* The X atom for the underlying selection. */ |
|
81 |
private final XAtom selectionAtom; |
|
82 |
||
83 |
/* |
|
84 |
* Owner-related variables - protected with synchronized (this). |
|
85 |
*/ |
|
86 |
||
87 |
/* The contents supplied by the current owner. */ |
|
88 |
private Transferable contents = null; |
|
89 |
/* The format-to-flavor map for the current owner. */ |
|
90 |
private Map formatMap = null; |
|
91 |
/* The formats supported by the current owner was set. */ |
|
92 |
private long[] formats = null; |
|
93 |
/* The AppContext in which the current owner was set. */ |
|
94 |
private AppContext appContext = null; |
|
95 |
// The X server time of the last XConvertSelection() call; |
|
96 |
// protected with 'lock' and awtLock. |
|
97 |
private static long lastRequestServerTime; |
|
98 |
/* The time at which the current owner was set. */ |
|
99 |
private long ownershipTime = 0; |
|
100 |
// True if we are the owner of this selection. |
|
101 |
private boolean isOwner; |
|
117 | 102 |
private OwnershipListener ownershipListener = null; |
103 |
private final Object stateLock = new Object(); |
|
2 | 104 |
|
105 |
static { |
|
106 |
XToolkit.addEventDispatcher(XWindow.getXAWTRootWindow().getWindow(), |
|
107 |
new SelectionEventHandler()); |
|
108 |
} |
|
109 |
||
110 |
/* |
|
111 |
* Returns the XSelection object for the specified selection atom or |
|
112 |
* <code>null</code> if none exists. |
|
113 |
*/ |
|
114 |
static XSelection getSelection(XAtom atom) { |
|
115 |
return table.get(atom); |
|
116 |
} |
|
117 |
||
118 |
/** |
|
119 |
* Creates a selection object. |
|
120 |
* |
|
121 |
* @param atom the selection atom. |
|
122 |
* @param clpbrd the corresponding clipoboard |
|
123 |
* @exception NullPointerException if atom is <code>null</code>. |
|
124 |
*/ |
|
117 | 125 |
public XSelection(XAtom atom) { |
2 | 126 |
if (atom == null) { |
127 |
throw new NullPointerException("Null atom"); |
|
128 |
} |
|
129 |
selectionAtom = atom; |
|
130 |
table.put(selectionAtom, this); |
|
131 |
} |
|
132 |
||
133 |
public XAtom getSelectionAtom() { |
|
134 |
return selectionAtom; |
|
135 |
} |
|
136 |
||
137 |
public synchronized boolean setOwner(Transferable contents, Map formatMap, |
|
117 | 138 |
long[] formats, long time) |
139 |
{ |
|
2 | 140 |
long owner = XWindow.getXAWTRootWindow().getWindow(); |
141 |
long selection = selectionAtom.getAtom(); |
|
142 |
||
143 |
// ICCCM prescribes that CurrentTime should not be used for SetSelectionOwner. |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
144 |
if (time == XConstants.CurrentTime) { |
2 | 145 |
time = XToolkit.getCurrentServerTime(); |
146 |
} |
|
147 |
||
148 |
this.contents = contents; |
|
149 |
this.formatMap = formatMap; |
|
150 |
this.formats = formats; |
|
151 |
this.appContext = AppContext.getAppContext(); |
|
152 |
this.ownershipTime = time; |
|
153 |
||
154 |
XToolkit.awtLock(); |
|
155 |
try { |
|
156 |
XlibWrapper.XSetSelectionOwner(XToolkit.getDisplay(), |
|
157 |
selection, owner, time); |
|
158 |
if (XlibWrapper.XGetSelectionOwner(XToolkit.getDisplay(), |
|
117 | 159 |
selection) != owner) |
160 |
{ |
|
2 | 161 |
reset(); |
162 |
return false; |
|
163 |
} |
|
117 | 164 |
setOwnerProp(true); |
2 | 165 |
return true; |
166 |
} finally { |
|
167 |
XToolkit.awtUnlock(); |
|
168 |
} |
|
169 |
} |
|
170 |
||
171 |
/** |
|
172 |
* Blocks the current thread till SelectionNotify or PropertyNotify (in case of INCR transfer) arrives. |
|
173 |
*/ |
|
174 |
private static void waitForSelectionNotify(WindowPropertyGetter dataGetter) throws InterruptedException { |
|
175 |
long startTime = System.currentTimeMillis(); |
|
176 |
XToolkit.awtLock(); |
|
177 |
try { |
|
178 |
do { |
|
179 |
DataTransferer.getInstance().processDataConversionRequests(); |
|
180 |
XToolkit.awtLockWait(250); |
|
117 | 181 |
} while (propertyGetter == dataGetter && System.currentTimeMillis() < startTime + UNIXToolkit.getDatatransferTimeout()); |
2 | 182 |
} finally { |
183 |
XToolkit.awtUnlock(); |
|
184 |
} |
|
185 |
} |
|
186 |
||
187 |
/* |
|
188 |
* Returns the list of atoms that represent the targets for which an attempt |
|
189 |
* to convert the current selection will succeed. |
|
190 |
*/ |
|
191 |
public long[] getTargets(long time) { |
|
192 |
if (XToolkit.isToolkitThread()) { |
|
193 |
throw new Error("UNIMPLEMENTED"); |
|
194 |
} |
|
195 |
||
117 | 196 |
long[] targets = null; |
2 | 197 |
|
198 |
synchronized (lock) { |
|
199 |
WindowPropertyGetter targetsGetter = |
|
200 |
new WindowPropertyGetter(XWindow.getXAWTRootWindow().getWindow(), |
|
201 |
selectionPropertyAtom, 0, MAX_LENGTH, |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
202 |
true, XConstants.AnyPropertyType); |
2 | 203 |
|
204 |
try { |
|
205 |
XToolkit.awtLock(); |
|
206 |
try { |
|
207 |
propertyGetter = targetsGetter; |
|
208 |
lastRequestServerTime = time; |
|
209 |
||
210 |
XlibWrapper.XConvertSelection(XToolkit.getDisplay(), |
|
211 |
getSelectionAtom().getAtom(), |
|
212 |
XDataTransferer.TARGETS_ATOM.getAtom(), |
|
213 |
selectionPropertyAtom.getAtom(), |
|
214 |
XWindow.getXAWTRootWindow().getWindow(), |
|
215 |
time); |
|
216 |
||
217 |
// If the owner doesn't respond within the |
|
218 |
// SELECTION_TIMEOUT, we report conversion failure. |
|
219 |
try { |
|
220 |
waitForSelectionNotify(targetsGetter); |
|
221 |
} catch (InterruptedException ie) { |
|
222 |
return new long[0]; |
|
223 |
} finally { |
|
224 |
propertyGetter = null; |
|
225 |
} |
|
226 |
} finally { |
|
227 |
XToolkit.awtUnlock(); |
|
228 |
} |
|
117 | 229 |
targets = getFormats(targetsGetter); |
2 | 230 |
} finally { |
231 |
targetsGetter.dispose(); |
|
232 |
} |
|
233 |
} |
|
117 | 234 |
return targets; |
2 | 235 |
} |
236 |
||
117 | 237 |
static long[] getFormats(WindowPropertyGetter targetsGetter) { |
2 | 238 |
long[] formats = null; |
239 |
||
240 |
if (targetsGetter.isExecuted() && !targetsGetter.isDisposed() && |
|
241 |
(targetsGetter.getActualType() == XAtom.XA_ATOM || |
|
242 |
targetsGetter.getActualType() == XDataTransferer.TARGETS_ATOM.getAtom()) && |
|
110
dc692e475ed0
6607163: Linux: Cannot copy image from Java to OpenOffice
son
parents:
2
diff
changeset
|
243 |
targetsGetter.getActualFormat() == 32) |
dc692e475ed0
6607163: Linux: Cannot copy image from Java to OpenOffice
son
parents:
2
diff
changeset
|
244 |
{ |
dc692e475ed0
6607163: Linux: Cannot copy image from Java to OpenOffice
son
parents:
2
diff
changeset
|
245 |
// we accept property with TARGETS type to be compatible with old jdks |
dc692e475ed0
6607163: Linux: Cannot copy image from Java to OpenOffice
son
parents:
2
diff
changeset
|
246 |
// see 6607163 |
117 | 247 |
int count = targetsGetter.getNumberOfItems(); |
2 | 248 |
if (count > 0) { |
249 |
long atoms = targetsGetter.getData(); |
|
250 |
formats = new long[count]; |
|
251 |
for (int index = 0; index < count; index++) { |
|
252 |
formats[index] = |
|
253 |
Native.getLong(atoms+index*XAtom.getAtomSize()); |
|
254 |
} |
|
255 |
} |
|
256 |
} |
|
257 |
||
258 |
return formats != null ? formats : new long[0]; |
|
259 |
} |
|
260 |
||
261 |
/* |
|
262 |
* Requests the selection data in the specified format and returns |
|
263 |
* the data provided by the owner. |
|
264 |
*/ |
|
265 |
public byte[] getData(long format, long time) throws IOException { |
|
266 |
if (XToolkit.isToolkitThread()) { |
|
267 |
throw new Error("UNIMPLEMENTED"); |
|
268 |
} |
|
269 |
||
270 |
byte[] data = null; |
|
271 |
||
272 |
synchronized (lock) { |
|
273 |
WindowPropertyGetter dataGetter = |
|
274 |
new WindowPropertyGetter(XWindow.getXAWTRootWindow().getWindow(), |
|
275 |
selectionPropertyAtom, 0, MAX_LENGTH, |
|
276 |
false, // don't delete to handle INCR properly. |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
277 |
XConstants.AnyPropertyType); |
2 | 278 |
|
279 |
try { |
|
280 |
XToolkit.awtLock(); |
|
281 |
try { |
|
282 |
propertyGetter = dataGetter; |
|
283 |
lastRequestServerTime = time; |
|
284 |
||
285 |
XlibWrapper.XConvertSelection(XToolkit.getDisplay(), |
|
286 |
getSelectionAtom().getAtom(), |
|
287 |
format, |
|
288 |
selectionPropertyAtom.getAtom(), |
|
289 |
XWindow.getXAWTRootWindow().getWindow(), |
|
290 |
time); |
|
291 |
||
292 |
// If the owner doesn't respond within the |
|
293 |
// SELECTION_TIMEOUT, we report conversion failure. |
|
294 |
try { |
|
295 |
waitForSelectionNotify(dataGetter); |
|
296 |
} catch (InterruptedException ie) { |
|
297 |
return new byte[0]; |
|
298 |
} finally { |
|
299 |
propertyGetter = null; |
|
300 |
} |
|
301 |
} finally { |
|
302 |
XToolkit.awtUnlock(); |
|
303 |
} |
|
304 |
if (!dataGetter.isExecuted()) { |
|
305 |
throw new IOException("Owner timed out"); |
|
306 |
} |
|
307 |
||
308 |
if (dataGetter.isDisposed()) { |
|
309 |
throw new IOException("Owner failed to convert data"); |
|
310 |
} |
|
311 |
||
312 |
// Handle incremental transfer. |
|
313 |
if (dataGetter.getActualType() == |
|
314 |
XDataTransferer.INCR_ATOM.getAtom()) { |
|
315 |
||
316 |
if (dataGetter.getActualFormat() != 32) { |
|
317 |
throw new IOException("Unsupported INCR format: " + |
|
318 |
dataGetter.getActualFormat()); |
|
319 |
} |
|
320 |
||
117 | 321 |
int count = dataGetter.getNumberOfItems(); |
2 | 322 |
|
323 |
if (count <= 0) { |
|
324 |
throw new IOException("INCR data is missed."); |
|
325 |
} |
|
326 |
||
327 |
long ptr = dataGetter.getData(); |
|
328 |
||
329 |
int len = 0; |
|
330 |
||
331 |
{ |
|
332 |
// Following Xt sources use the last element. |
|
333 |
long longLength = Native.getLong(ptr, count-1); |
|
334 |
||
335 |
if (longLength <= 0) { |
|
336 |
return new byte[0]; |
|
337 |
} |
|
338 |
||
339 |
if (longLength > Integer.MAX_VALUE) { |
|
340 |
throw new IOException("Can't handle large data block: " |
|
341 |
+ longLength + " bytes"); |
|
342 |
} |
|
343 |
||
344 |
len = (int)longLength; |
|
345 |
} |
|
346 |
||
347 |
dataGetter.dispose(); |
|
348 |
||
349 |
ByteArrayOutputStream dataStream = new ByteArrayOutputStream(len); |
|
350 |
||
351 |
while (true) { |
|
352 |
WindowPropertyGetter incrDataGetter = |
|
353 |
new WindowPropertyGetter(XWindow.getXAWTRootWindow().getWindow(), |
|
354 |
selectionPropertyAtom, |
|
355 |
0, MAX_LENGTH, false, |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
356 |
XConstants.AnyPropertyType); |
2 | 357 |
|
358 |
try { |
|
359 |
XToolkit.awtLock(); |
|
360 |
XToolkit.addEventDispatcher(XWindow.getXAWTRootWindow().getWindow(), |
|
361 |
incrementalTransferHandler); |
|
362 |
||
363 |
propertyGetter = incrDataGetter; |
|
364 |
||
365 |
try { |
|
366 |
XlibWrapper.XDeleteProperty(XToolkit.getDisplay(), |
|
367 |
XWindow.getXAWTRootWindow().getWindow(), |
|
368 |
selectionPropertyAtom.getAtom()); |
|
369 |
||
370 |
// If the owner doesn't respond within the |
|
371 |
// SELECTION_TIMEOUT, we terminate incremental |
|
372 |
// transfer. |
|
373 |
waitForSelectionNotify(incrDataGetter); |
|
374 |
} catch (InterruptedException ie) { |
|
375 |
break; |
|
376 |
} finally { |
|
377 |
propertyGetter = null; |
|
378 |
XToolkit.removeEventDispatcher(XWindow.getXAWTRootWindow().getWindow(), |
|
379 |
incrementalTransferHandler); |
|
380 |
XToolkit.awtUnlock(); |
|
381 |
} |
|
382 |
||
383 |
// The owner didn't respond - terminate the transfer. |
|
384 |
if (!incrDataGetter.isExecuted()) { |
|
385 |
throw new IOException("Owner timed out"); |
|
386 |
} |
|
387 |
||
388 |
if (incrDataGetter.isDisposed()) { |
|
389 |
throw new IOException("Owner failed to convert data"); |
|
390 |
} |
|
391 |
||
392 |
if (incrDataGetter.getActualFormat() != 8) { |
|
393 |
throw new IOException("Unsupported data format: " + |
|
394 |
incrDataGetter.getActualFormat()); |
|
395 |
} |
|
396 |
||
117 | 397 |
count = incrDataGetter.getNumberOfItems(); |
2 | 398 |
|
399 |
if (count == 0) { |
|
400 |
break; |
|
401 |
} |
|
402 |
||
403 |
if (count > 0) { |
|
404 |
ptr = incrDataGetter.getData(); |
|
405 |
for (int index = 0; index < count; index++) { |
|
406 |
dataStream.write(Native.getByte(ptr + index)); |
|
407 |
} |
|
408 |
} |
|
409 |
||
410 |
data = dataStream.toByteArray(); |
|
411 |
||
412 |
} finally { |
|
413 |
incrDataGetter.dispose(); |
|
414 |
} |
|
415 |
} |
|
416 |
} else { |
|
417 |
XToolkit.awtLock(); |
|
418 |
try { |
|
419 |
XlibWrapper.XDeleteProperty(XToolkit.getDisplay(), |
|
420 |
XWindow.getXAWTRootWindow().getWindow(), |
|
421 |
selectionPropertyAtom.getAtom()); |
|
422 |
} finally { |
|
423 |
XToolkit.awtUnlock(); |
|
424 |
} |
|
425 |
||
426 |
if (dataGetter.getActualFormat() != 8) { |
|
427 |
throw new IOException("Unsupported data format: " + |
|
428 |
dataGetter.getActualFormat()); |
|
429 |
} |
|
430 |
||
117 | 431 |
int count = dataGetter.getNumberOfItems(); |
2 | 432 |
if (count > 0) { |
433 |
data = new byte[count]; |
|
434 |
long ptr = dataGetter.getData(); |
|
435 |
for (int index = 0; index < count; index++) { |
|
436 |
data[index] = Native.getByte(ptr + index); |
|
437 |
} |
|
438 |
} |
|
439 |
} |
|
440 |
} finally { |
|
441 |
dataGetter.dispose(); |
|
442 |
} |
|
443 |
} |
|
444 |
||
445 |
return data != null ? data : new byte[0]; |
|
446 |
} |
|
447 |
||
448 |
// To be MT-safe this method should be called under awtLock. |
|
449 |
boolean isOwner() { |
|
450 |
return isOwner; |
|
451 |
} |
|
452 |
||
117 | 453 |
// To be MT-safe this method should be called under awtLock. |
454 |
private void setOwnerProp(boolean f) { |
|
455 |
isOwner = f; |
|
456 |
fireOwnershipChanges(isOwner); |
|
457 |
} |
|
458 |
||
459 |
private void lostOwnership() { |
|
460 |
setOwnerProp(false); |
|
2 | 461 |
} |
462 |
||
463 |
public synchronized void reset() { |
|
464 |
contents = null; |
|
465 |
formatMap = null; |
|
466 |
formats = null; |
|
467 |
appContext = null; |
|
468 |
ownershipTime = 0; |
|
469 |
} |
|
470 |
||
471 |
// Converts the data to the 'format' and if the conversion succeeded stores |
|
472 |
// the data in the 'property' on the 'requestor' window. |
|
473 |
// Returns true if the conversion succeeded. |
|
474 |
private boolean convertAndStore(long requestor, long format, long property) { |
|
475 |
int dataFormat = 8; /* Can choose between 8,16,32. */ |
|
476 |
byte[] byteData = null; |
|
477 |
long nativeDataPtr = 0; |
|
478 |
int count = 0; |
|
479 |
||
480 |
try { |
|
481 |
SunToolkit.insertTargetMapping(this, appContext); |
|
482 |
||
483 |
byteData = DataTransferer.getInstance().convertData(this, |
|
484 |
contents, |
|
485 |
format, |
|
486 |
formatMap, |
|
487 |
XToolkit.isToolkitThread()); |
|
488 |
} catch (IOException ioe) { |
|
489 |
return false; |
|
490 |
} |
|
491 |
||
492 |
if (byteData == null) { |
|
493 |
return false; |
|
494 |
} |
|
495 |
||
496 |
count = byteData.length; |
|
497 |
||
498 |
try { |
|
499 |
if (count > 0) { |
|
500 |
if (count <= MAX_PROPERTY_SIZE) { |
|
501 |
nativeDataPtr = Native.toData(byteData); |
|
502 |
} else { |
|
503 |
// Initiate incremental data transfer. |
|
504 |
new IncrementalDataProvider(requestor, property, format, 8, |
|
505 |
byteData); |
|
506 |
||
507 |
nativeDataPtr = |
|
508 |
XlibWrapper.unsafe.allocateMemory(XAtom.getAtomSize()); |
|
509 |
||
510 |
Native.putLong(nativeDataPtr, (long)count); |
|
511 |
||
512 |
format = XDataTransferer.INCR_ATOM.getAtom(); |
|
513 |
dataFormat = 32; |
|
514 |
count = 1; |
|
515 |
} |
|
516 |
||
517 |
} |
|
518 |
||
519 |
XToolkit.awtLock(); |
|
520 |
try { |
|
521 |
XlibWrapper.XChangeProperty(XToolkit.getDisplay(), requestor, property, |
|
522 |
format, dataFormat, |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
523 |
XConstants.PropModeReplace, |
2 | 524 |
nativeDataPtr, count); |
525 |
} finally { |
|
526 |
XToolkit.awtUnlock(); |
|
527 |
} |
|
528 |
} finally { |
|
529 |
if (nativeDataPtr != 0) { |
|
530 |
XlibWrapper.unsafe.freeMemory(nativeDataPtr); |
|
531 |
nativeDataPtr = 0; |
|
532 |
} |
|
533 |
} |
|
534 |
||
535 |
return true; |
|
536 |
} |
|
537 |
||
538 |
private void handleSelectionRequest(XSelectionRequestEvent xsre) { |
|
539 |
long property = xsre.get_property(); |
|
117 | 540 |
final long requestor = xsre.get_requestor(); |
541 |
final long requestTime = xsre.get_time(); |
|
542 |
final long format = xsre.get_target(); |
|
2 | 543 |
boolean conversionSucceeded = false; |
544 |
||
545 |
if (ownershipTime != 0 && |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
546 |
(requestTime == XConstants.CurrentTime || requestTime >= ownershipTime)) |
117 | 547 |
{ |
2 | 548 |
// Handle MULTIPLE requests as per ICCCM. |
549 |
if (format == XDataTransferer.MULTIPLE_ATOM.getAtom()) { |
|
117 | 550 |
conversionSucceeded = handleMultipleRequest(requestor, property); |
2 | 551 |
} else { |
552 |
// Support for obsolete clients as per ICCCM. |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
553 |
if (property == XConstants.None) { |
2 | 554 |
property = format; |
555 |
} |
|
556 |
||
557 |
if (format == XDataTransferer.TARGETS_ATOM.getAtom()) { |
|
117 | 558 |
conversionSucceeded = handleTargetsRequest(property, requestor); |
2 | 559 |
} else { |
117 | 560 |
conversionSucceeded = convertAndStore(requestor, format, property); |
2 | 561 |
} |
562 |
} |
|
563 |
} |
|
564 |
||
565 |
if (!conversionSucceeded) { |
|
117 | 566 |
// None property indicates conversion failure. |
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
567 |
property = XConstants.None; |
2 | 568 |
} |
569 |
||
570 |
XSelectionEvent xse = new XSelectionEvent(); |
|
571 |
try { |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
572 |
xse.set_type(XConstants.SelectionNotify); |
2 | 573 |
xse.set_send_event(true); |
574 |
xse.set_requestor(requestor); |
|
575 |
xse.set_selection(selectionAtom.getAtom()); |
|
576 |
xse.set_target(format); |
|
577 |
xse.set_property(property); |
|
578 |
xse.set_time(requestTime); |
|
579 |
||
580 |
XToolkit.awtLock(); |
|
581 |
try { |
|
582 |
XlibWrapper.XSendEvent(XToolkit.getDisplay(), requestor, false, |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
583 |
XConstants.NoEventMask, xse.pData); |
2 | 584 |
} finally { |
585 |
XToolkit.awtUnlock(); |
|
586 |
} |
|
587 |
} finally { |
|
588 |
xse.dispose(); |
|
589 |
} |
|
590 |
} |
|
591 |
||
117 | 592 |
private boolean handleMultipleRequest(final long requestor, long property) { |
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
593 |
if (XConstants.None == property) { |
117 | 594 |
// The property cannot be None for a MULTIPLE request. |
595 |
return false; |
|
596 |
} |
|
597 |
||
598 |
boolean conversionSucceeded = false; |
|
599 |
||
600 |
// First retrieve the list of requested targets. |
|
601 |
WindowPropertyGetter wpg = |
|
602 |
new WindowPropertyGetter(requestor, XAtom.get(property), |
|
603 |
0, MAX_LENGTH, false, |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
604 |
XConstants.AnyPropertyType); |
117 | 605 |
try { |
606 |
wpg.execute(); |
|
607 |
||
608 |
if (wpg.getActualFormat() == 32 && (wpg.getNumberOfItems() % 2) == 0) { |
|
609 |
final long count = wpg.getNumberOfItems() / 2; |
|
610 |
final long pairsPtr = wpg.getData(); |
|
611 |
boolean writeBack = false; |
|
612 |
||
613 |
for (int i = 0; i < count; i++) { |
|
614 |
long target = Native.getLong(pairsPtr, 2 * i); |
|
615 |
long prop = Native.getLong(pairsPtr, 2 * i + 1); |
|
616 |
||
617 |
if (!convertAndStore(requestor, target, prop)) { |
|
618 |
// To report failure, we should replace the |
|
619 |
// target atom with 0 in the MULTIPLE property. |
|
620 |
Native.putLong(pairsPtr, 2 * i, 0); |
|
621 |
writeBack = true; |
|
622 |
} |
|
623 |
} |
|
624 |
if (writeBack) { |
|
625 |
XToolkit.awtLock(); |
|
626 |
try { |
|
627 |
XlibWrapper.XChangeProperty(XToolkit.getDisplay(), |
|
628 |
requestor, |
|
629 |
property, |
|
630 |
wpg.getActualType(), |
|
631 |
wpg.getActualFormat(), |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
632 |
XConstants.PropModeReplace, |
117 | 633 |
wpg.getData(), |
634 |
wpg.getNumberOfItems()); |
|
635 |
} finally { |
|
636 |
XToolkit.awtUnlock(); |
|
637 |
} |
|
638 |
} |
|
639 |
conversionSucceeded = true; |
|
640 |
} |
|
641 |
} finally { |
|
642 |
wpg.dispose(); |
|
2 | 643 |
} |
644 |
||
117 | 645 |
return conversionSucceeded; |
646 |
} |
|
647 |
||
648 |
private boolean handleTargetsRequest(long property, long requestor) |
|
649 |
throws IllegalStateException |
|
650 |
{ |
|
651 |
boolean conversionSucceeded = false; |
|
652 |
// Use a local copy to avoid synchronization. |
|
653 |
long[] formatsLocal = formats; |
|
654 |
||
655 |
if (formatsLocal == null) { |
|
656 |
throw new IllegalStateException("Not an owner."); |
|
657 |
} |
|
658 |
||
659 |
long nativeDataPtr = 0; |
|
660 |
||
661 |
try { |
|
662 |
final int count = formatsLocal.length; |
|
663 |
final int dataFormat = 32; |
|
2 | 664 |
|
117 | 665 |
if (count > 0) { |
666 |
nativeDataPtr = Native.allocateLongArray(count); |
|
667 |
Native.put(nativeDataPtr, formatsLocal); |
|
668 |
} |
|
669 |
||
670 |
conversionSucceeded = true; |
|
671 |
||
672 |
XToolkit.awtLock(); |
|
2 | 673 |
try { |
117 | 674 |
XlibWrapper.XChangeProperty(XToolkit.getDisplay(), requestor, |
675 |
property, XAtom.XA_ATOM, dataFormat, |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
676 |
XConstants.PropModeReplace, |
117 | 677 |
nativeDataPtr, count); |
2 | 678 |
} finally { |
117 | 679 |
XToolkit.awtUnlock(); |
680 |
} |
|
681 |
} finally { |
|
682 |
if (nativeDataPtr != 0) { |
|
683 |
XlibWrapper.unsafe.freeMemory(nativeDataPtr); |
|
684 |
nativeDataPtr = 0; |
|
2 | 685 |
} |
686 |
} |
|
117 | 687 |
return conversionSucceeded; |
688 |
} |
|
2 | 689 |
|
117 | 690 |
private void fireOwnershipChanges(final boolean isOwner) { |
691 |
OwnershipListener l = null; |
|
692 |
synchronized (stateLock) { |
|
693 |
l = ownershipListener; |
|
694 |
} |
|
695 |
if (null != l) { |
|
696 |
l.ownershipChanged(isOwner); |
|
697 |
} |
|
698 |
} |
|
699 |
||
700 |
void registerOwershipListener(OwnershipListener l) { |
|
701 |
synchronized (stateLock) { |
|
702 |
ownershipListener = l; |
|
703 |
} |
|
704 |
} |
|
705 |
||
706 |
void unregisterOwnershipListener() { |
|
707 |
synchronized (stateLock) { |
|
708 |
ownershipListener = null; |
|
2 | 709 |
} |
710 |
} |
|
711 |
||
712 |
private static class SelectionEventHandler implements XEventDispatcher { |
|
713 |
public void dispatchEvent(XEvent ev) { |
|
714 |
switch (ev.get_type()) { |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
715 |
case XConstants.SelectionNotify: { |
2 | 716 |
XToolkit.awtLock(); |
717 |
try { |
|
117 | 718 |
XSelectionEvent xse = ev.get_xselection(); |
2 | 719 |
// Ignore the SelectionNotify event if it is not the response to our last request. |
720 |
if (propertyGetter != null && xse.get_time() == lastRequestServerTime) { |
|
721 |
// The property will be None in case of convertion failure. |
|
722 |
if (xse.get_property() == selectionPropertyAtom.getAtom()) { |
|
723 |
propertyGetter.execute(); |
|
724 |
propertyGetter = null; |
|
725 |
} else if (xse.get_property() == 0) { |
|
726 |
propertyGetter.dispose(); |
|
727 |
propertyGetter = null; |
|
728 |
} |
|
729 |
} |
|
730 |
XToolkit.awtLockNotifyAll(); |
|
731 |
} finally { |
|
732 |
XToolkit.awtUnlock(); |
|
733 |
} |
|
734 |
break; |
|
735 |
} |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
736 |
case XConstants.SelectionRequest: { |
2 | 737 |
XSelectionRequestEvent xsre = ev.get_xselectionrequest(); |
738 |
long atom = xsre.get_selection(); |
|
739 |
XSelection selection = XSelection.getSelection(XAtom.get(atom)); |
|
740 |
||
741 |
if (selection != null) { |
|
742 |
selection.handleSelectionRequest(xsre); |
|
743 |
} |
|
744 |
break; |
|
745 |
} |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
746 |
case XConstants.SelectionClear: { |
2 | 747 |
XSelectionClearEvent xsce = ev.get_xselectionclear(); |
748 |
long atom = xsce.get_selection(); |
|
749 |
XSelection selection = XSelection.getSelection(XAtom.get(atom)); |
|
750 |
||
751 |
if (selection != null) { |
|
752 |
selection.lostOwnership(); |
|
753 |
} |
|
754 |
||
755 |
XToolkit.awtLock(); |
|
756 |
try { |
|
757 |
XToolkit.awtLockNotifyAll(); |
|
758 |
} finally { |
|
759 |
XToolkit.awtUnlock(); |
|
760 |
} |
|
761 |
break; |
|
762 |
} |
|
763 |
} |
|
764 |
} |
|
765 |
}; |
|
766 |
||
767 |
private static class IncrementalDataProvider implements XEventDispatcher { |
|
768 |
private final long requestor; |
|
769 |
private final long property; |
|
770 |
private final long target; |
|
771 |
private final int format; |
|
772 |
private final byte[] data; |
|
773 |
private int offset = 0; |
|
774 |
||
775 |
// NOTE: formats other than 8 are not supported. |
|
776 |
public IncrementalDataProvider(long requestor, long property, |
|
777 |
long target, int format, byte[] data) { |
|
778 |
if (format != 8) { |
|
779 |
throw new IllegalArgumentException("Unsupported format: " + format); |
|
780 |
} |
|
781 |
||
782 |
this.requestor = requestor; |
|
783 |
this.property = property; |
|
784 |
this.target = target; |
|
785 |
this.format = format; |
|
786 |
this.data = data; |
|
787 |
||
788 |
XWindowAttributes wattr = new XWindowAttributes(); |
|
789 |
try { |
|
790 |
XToolkit.awtLock(); |
|
791 |
try { |
|
792 |
XlibWrapper.XGetWindowAttributes(XToolkit.getDisplay(), requestor, |
|
793 |
wattr.pData); |
|
794 |
XlibWrapper.XSelectInput(XToolkit.getDisplay(), requestor, |
|
795 |
wattr.get_your_event_mask() | |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
796 |
XConstants.PropertyChangeMask); |
2 | 797 |
} finally { |
798 |
XToolkit.awtUnlock(); |
|
799 |
} |
|
800 |
} finally { |
|
801 |
wattr.dispose(); |
|
802 |
} |
|
803 |
XToolkit.addEventDispatcher(requestor, this); |
|
804 |
} |
|
805 |
||
806 |
public void dispatchEvent(XEvent ev) { |
|
807 |
switch (ev.get_type()) { |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
808 |
case XConstants.PropertyNotify: |
2 | 809 |
XPropertyEvent xpe = ev.get_xproperty(); |
810 |
if (xpe.get_window() == requestor && |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
811 |
xpe.get_state() == XConstants.PropertyDelete && |
2 | 812 |
xpe.get_atom() == property) { |
813 |
||
814 |
int count = data.length - offset; |
|
815 |
long nativeDataPtr = 0; |
|
816 |
if (count > MAX_PROPERTY_SIZE) { |
|
817 |
count = MAX_PROPERTY_SIZE; |
|
818 |
} |
|
819 |
||
820 |
if (count > 0) { |
|
821 |
nativeDataPtr = XlibWrapper.unsafe.allocateMemory(count); |
|
822 |
for (int i = 0; i < count; i++) { |
|
823 |
Native.putByte(nativeDataPtr+i, data[offset + i]); |
|
824 |
} |
|
825 |
} else { |
|
826 |
assert (count == 0); |
|
827 |
// All data has been transferred. |
|
828 |
// This zero-length data indicates end of transfer. |
|
829 |
XToolkit.removeEventDispatcher(requestor, this); |
|
830 |
} |
|
831 |
||
832 |
XToolkit.awtLock(); |
|
833 |
try { |
|
834 |
XlibWrapper.XChangeProperty(XToolkit.getDisplay(), |
|
835 |
requestor, property, |
|
836 |
target, format, |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
837 |
XConstants.PropModeReplace, |
2 | 838 |
nativeDataPtr, count); |
839 |
} finally { |
|
840 |
XToolkit.awtUnlock(); |
|
841 |
} |
|
842 |
if (nativeDataPtr != 0) { |
|
843 |
XlibWrapper.unsafe.freeMemory(nativeDataPtr); |
|
844 |
nativeDataPtr = 0; |
|
845 |
} |
|
846 |
||
847 |
offset += count; |
|
848 |
} |
|
849 |
} |
|
850 |
} |
|
851 |
} |
|
852 |
||
853 |
private static class IncrementalTransferHandler implements XEventDispatcher { |
|
854 |
public void dispatchEvent(XEvent ev) { |
|
855 |
switch (ev.get_type()) { |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
856 |
case XConstants.PropertyNotify: |
2 | 857 |
XPropertyEvent xpe = ev.get_xproperty(); |
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
117
diff
changeset
|
858 |
if (xpe.get_state() == XConstants.PropertyNewValue && |
2 | 859 |
xpe.get_atom() == selectionPropertyAtom.getAtom()) { |
860 |
XToolkit.awtLock(); |
|
861 |
try { |
|
862 |
if (propertyGetter != null) { |
|
863 |
propertyGetter.execute(); |
|
864 |
propertyGetter = null; |
|
865 |
} |
|
866 |
XToolkit.awtLockNotifyAll(); |
|
867 |
} finally { |
|
868 |
XToolkit.awtUnlock(); |
|
869 |
} |
|
870 |
} |
|
871 |
break; |
|
872 |
} |
|
873 |
} |
|
874 |
}; |
|
875 |
} |