Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "unx/saldisp.hxx"
31 : : #include "unx/saldata.hxx"
32 : :
33 : : #include <unistd.h>
34 : : #include <stdio.h>
35 : : #include <string.h>
36 : : #include <sys/time.h>
37 : :
38 : : #include "tools/prex.h"
39 : : #include <X11/Xatom.h>
40 : : #include <X11/keysym.h>
41 : : #include <X11/Xutil.h>
42 : : #include "tools/postx.h"
43 : : #if defined(LINUX) || defined(NETBSD) || defined (FREEBSD) || defined(OPENBSD)
44 : : #include <sys/poll.h>
45 : : #else
46 : : #include <poll.h>
47 : : #endif
48 : : #include <sal/alloca.h>
49 : : #include <sal/macros.h>
50 : :
51 : : #include <X11_selection.hxx>
52 : : #include <X11_clipboard.hxx>
53 : : #include <X11_transferable.hxx>
54 : : #include <X11_dndcontext.hxx>
55 : : #include <bmp.hxx>
56 : :
57 : : #include "vcl/svapp.hxx"
58 : :
59 : : // pointer bitmaps
60 : : #include <copydata_curs.h>
61 : : #include <copydata_mask.h>
62 : : #include <movedata_curs.h>
63 : : #include <movedata_mask.h>
64 : : #include <linkdata_curs.h>
65 : : #include <linkdata_mask.h>
66 : : #include <nodrop_curs.h>
67 : : #include <nodrop_mask.h>
68 : : #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
69 : : #include <com/sun/star/awt/MouseEvent.hpp>
70 : : #include <com/sun/star/awt/MouseButton.hpp>
71 : : #include <rtl/tencinfo.h>
72 : : #include <osl/process.h>
73 : :
74 : : #include <comphelper/processfactory.hxx>
75 : : #include <osl/mutex.hxx>
76 : :
77 : : #define DRAG_EVENT_MASK ButtonPressMask |\
78 : : ButtonReleaseMask |\
79 : : PointerMotionMask |\
80 : : EnterWindowMask |\
81 : : LeaveWindowMask
82 : :
83 : : namespace {
84 : :
85 : : namespace css = com::sun::star;
86 : :
87 : : }
88 : :
89 : : using namespace com::sun::star::datatransfer;
90 : : using namespace com::sun::star::datatransfer::dnd;
91 : : using namespace com::sun::star::lang;
92 : : using namespace com::sun::star::awt;
93 : : using namespace com::sun::star::uno;
94 : : using namespace com::sun::star::frame;
95 : : using namespace cppu;
96 : :
97 : : using namespace x11;
98 : :
99 : : using ::rtl::OUString;
100 : : using ::rtl::OUStringHash;
101 : : using ::rtl::OStringToOUString;
102 : :
103 : : // stubs to satisfy solaris compiler's rather rigid linking warning
104 : : extern "C"
105 : : {
106 : 0 : static void call_SelectionManager_run( void * pMgr )
107 : : {
108 : 0 : SelectionManager::run( pMgr );
109 : 0 : }
110 : :
111 : 0 : static void call_SelectionManager_runDragExecute( void * pMgr )
112 : : {
113 : 0 : SelectionManager::runDragExecute( pMgr );
114 : 0 : }
115 : : }
116 : :
117 : :
118 : : static const long nXdndProtocolRevision = 5;
119 : :
120 : : // mapping between mime types (or what the office thinks of mime types)
121 : : // and X convention types
122 : : struct NativeTypeEntry
123 : : {
124 : : Atom nAtom;
125 : : const char* pType; // Mime encoding on our side
126 : : const char* pNativeType; // string corresponding to nAtom for the case of nAtom being uninitialized
127 : : int nFormat; // the corresponding format
128 : : };
129 : :
130 : : // the convention for Xdnd is mime types as specified by the corresponding
131 : : // RFC's with the addition that text/plain without charset tag contains iso8859-1
132 : : // sadly some applications (e.g. gtk) do not honor the mimetype only rule,
133 : : // so for compatibility add UTF8_STRING
134 : : static NativeTypeEntry aXdndConversionTab[] =
135 : : {
136 : : { 0, "text/plain;charset=iso8859-1", "text/plain", 8 },
137 : : { 0, "text/plain;charset=utf-8", "UTF8_STRING", 8 }
138 : : };
139 : :
140 : : // for clipboard and primary selections there is only a convention for text
141 : : // that the encoding name of the text is taken as type in all capitalized letters
142 : : static NativeTypeEntry aNativeConversionTab[] =
143 : : {
144 : : { 0, "text/plain;charset=utf-16", "ISO10646-1", 16 },
145 : : { 0, "text/plain;charset=utf-8", "UTF8_STRING", 8 },
146 : : { 0, "text/plain;charset=utf-8", "UTF-8", 8 },
147 : : { 0, "text/plain;charset=utf-8", "text/plain;charset=UTF-8", 8 },
148 : : // ISO encodings
149 : : { 0, "text/plain;charset=iso8859-2", "ISO8859-2", 8 },
150 : : { 0, "text/plain;charset=iso8859-3", "ISO8859-3", 8 },
151 : : { 0, "text/plain;charset=iso8859-4", "ISO8859-4", 8 },
152 : : { 0, "text/plain;charset=iso8859-5", "ISO8859-5", 8 },
153 : : { 0, "text/plain;charset=iso8859-6", "ISO8859-6", 8 },
154 : : { 0, "text/plain;charset=iso8859-7", "ISO8859-7", 8 },
155 : : { 0, "text/plain;charset=iso8859-8", "ISO8859-8", 8 },
156 : : { 0, "text/plain;charset=iso8859-9", "ISO8859-9", 8 },
157 : : { 0, "text/plain;charset=iso8859-10", "ISO8859-10", 8 },
158 : : { 0, "text/plain;charset=iso8859-13", "ISO8859-13", 8 },
159 : : { 0, "text/plain;charset=iso8859-14", "ISO8859-14", 8 },
160 : : { 0, "text/plain;charset=iso8859-15", "ISO8859-15", 8 },
161 : : // asian encodings
162 : : { 0, "text/plain;charset=jisx0201.1976-0", "JISX0201.1976-0", 8 },
163 : : { 0, "text/plain;charset=jisx0208.1983-0", "JISX0208.1983-0", 8 },
164 : : { 0, "text/plain;charset=jisx0208.1990-0", "JISX0208.1990-0", 8 },
165 : : { 0, "text/plain;charset=jisx0212.1990-0", "JISX0212.1990-0", 8 },
166 : : { 0, "text/plain;charset=gb2312.1980-0", "GB2312.1980-0", 8 },
167 : : { 0, "text/plain;charset=ksc5601.1992-0", "KSC5601.1992-0", 8 },
168 : : // eastern european encodings
169 : : { 0, "text/plain;charset=koi8-r", "KOI8-R", 8 },
170 : : { 0, "text/plain;charset=koi8-u", "KOI8-U", 8 },
171 : : // String (== iso8859-1)
172 : : { XA_STRING, "text/plain;charset=iso8859-1", "STRING", 8 },
173 : : // special for compound text
174 : : { 0, "text/plain;charset=compound_text", "COMPOUND_TEXT", 8 },
175 : :
176 : : // PIXMAP
177 : : { XA_PIXMAP, "image/bmp", "PIXMAP", 32 }
178 : : };
179 : :
180 : 0 : rtl_TextEncoding x11::getTextPlainEncoding( const OUString& rMimeType )
181 : : {
182 : 0 : rtl_TextEncoding aEncoding = RTL_TEXTENCODING_DONTKNOW;
183 : 0 : OUString aMimeType( rMimeType.toAsciiLowerCase() );
184 : 0 : sal_Int32 nIndex = 0;
185 : 0 : if( aMimeType.getToken( 0, ';', nIndex ).equalsAsciiL( "text/plain" , 10 ) )
186 : : {
187 : 0 : if( aMimeType.getLength() == 10 ) // only "text/plain"
188 : 0 : aEncoding = RTL_TEXTENCODING_ISO_8859_1;
189 : : else
190 : : {
191 : 0 : while( nIndex != -1 )
192 : : {
193 : 0 : OUString aToken = aMimeType.getToken( 0, ';', nIndex );
194 : 0 : sal_Int32 nPos = 0;
195 : 0 : if( aToken.getToken( 0, '=', nPos ).equalsAsciiL( "charset", 7 ) )
196 : : {
197 : 0 : OString aEncToken = OUStringToOString( aToken.getToken( 0, '=', nPos ), RTL_TEXTENCODING_ISO_8859_1 );
198 : 0 : aEncoding = rtl_getTextEncodingFromUnixCharset( aEncToken.getStr() );
199 : 0 : if( aEncoding == RTL_TEXTENCODING_DONTKNOW )
200 : : {
201 : 0 : if( aEncToken.equalsIgnoreAsciiCase( "utf-8" ) )
202 : 0 : aEncoding = RTL_TEXTENCODING_UTF8;
203 : : }
204 : 0 : if( aEncoding != RTL_TEXTENCODING_DONTKNOW )
205 : 0 : break;
206 : : }
207 : 0 : }
208 : : }
209 : : }
210 : : #if OSL_DEBUG_LEVEL > 1
211 : : if( aEncoding == RTL_TEXTENCODING_DONTKNOW )
212 : : fprintf( stderr, "getTextPlainEncoding( %s ) failed\n", OUStringToOString( rMimeType, RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
213 : : #endif
214 : 0 : return aEncoding;
215 : : }
216 : :
217 : : // ------------------------------------------------------------------------
218 : :
219 : 0 : ::boost::unordered_map< OUString, SelectionManager*, OUStringHash >& SelectionManager::getInstances()
220 : : {
221 : 0 : static ::boost::unordered_map< OUString, SelectionManager*, OUStringHash > aInstances;
222 : 0 : return aInstances;
223 : : }
224 : :
225 : : // ------------------------------------------------------------------------
226 : :
227 : 0 : SelectionManager::SelectionManager() :
228 : : m_nIncrementalThreshold( 15*1024 ),
229 : : m_pDisplay( NULL ),
230 : : m_aThread( NULL ),
231 : : m_aDragExecuteThread( NULL ),
232 : : m_aWindow( None ),
233 : : m_nSelectionTimeout( 0 ),
234 : : m_nSelectionTimestamp( CurrentTime ),
235 : : m_bDropEnterSent( true ),
236 : : m_aCurrentDropWindow( None ),
237 : : m_nDropTime( None ),
238 : : m_nLastDropAction( 0 ),
239 : : m_nLastX( 0 ),
240 : : m_nLastY( 0 ),
241 : : m_nDropTimestamp( 0 ),
242 : : m_bDropWaitingForCompletion( false ),
243 : : m_aDropWindow( None ),
244 : : m_aDropProxy( None ),
245 : : m_aDragSourceWindow( None ),
246 : : m_nLastDragX( 0 ),
247 : : m_nLastDragY( 0 ),
248 : : m_nNoPosX( 0 ),
249 : : m_nNoPosY( 0 ),
250 : : m_nNoPosWidth( 0 ),
251 : : m_nNoPosHeight( 0 ),
252 : : m_nDragButton( 0 ),
253 : : m_nUserDragAction( 0 ),
254 : : m_nTargetAcceptAction( 0 ),
255 : : m_nSourceActions( 0 ),
256 : : m_bLastDropAccepted( false ),
257 : : m_bDropSuccess( false ),
258 : : m_bDropSent( false ),
259 : : m_bWaitingForPrimaryConversion( false ),
260 : : m_nDragTimestamp( None ),
261 : : m_aMoveCursor( None ),
262 : : m_aCopyCursor( None ),
263 : : m_aLinkCursor( None ),
264 : : m_aNoneCursor( None ),
265 : : m_aCurrentCursor( None ),
266 : : m_nCurrentProtocolVersion( nXdndProtocolRevision ),
267 : : m_nCLIPBOARDAtom( None ),
268 : : m_nTARGETSAtom( None ),
269 : : m_nTIMESTAMPAtom( None ),
270 : : m_nTEXTAtom( None ),
271 : : m_nINCRAtom( None ),
272 : : m_nCOMPOUNDAtom( None ),
273 : : m_nMULTIPLEAtom( None ),
274 : : m_nUTF16Atom( None ),
275 : : m_nImageBmpAtom( None ),
276 : : m_nXdndAware( None ),
277 : : m_nXdndEnter( None ),
278 : : m_nXdndLeave( None ),
279 : : m_nXdndPosition( None ),
280 : : m_nXdndStatus( None ),
281 : : m_nXdndDrop( None ),
282 : : m_nXdndFinished( None ),
283 : : m_nXdndSelection( None ),
284 : : m_nXdndTypeList( None ),
285 : : m_nXdndProxy( None ),
286 : : m_nXdndActionCopy( None ),
287 : : m_nXdndActionMove( None ),
288 : : m_nXdndActionLink( None ),
289 : : m_nXdndActionAsk( None ),
290 : : m_nXdndActionPrivate( None ),
291 : 0 : m_bShutDown( false )
292 : : {
293 : 0 : m_aDropEnterEvent.data.l[0] = None;
294 : 0 : m_aDragRunning.reset();
295 : 0 : }
296 : :
297 : 0 : XLIB_Cursor SelectionManager::createCursor( const unsigned char* pPointerData, const unsigned char* pMaskData, int width, int height, int hotX, int hotY )
298 : : {
299 : : Pixmap aPointer;
300 : : Pixmap aMask;
301 : : XColor aBlack, aWhite;
302 : :
303 : 0 : aBlack.pixel = BlackPixel( m_pDisplay, 0 );
304 : 0 : aBlack.red = aBlack.green = aBlack.blue = 0;
305 : 0 : aBlack.flags = DoRed | DoGreen | DoBlue;
306 : :
307 : 0 : aWhite.pixel = WhitePixel( m_pDisplay, 0 );
308 : 0 : aWhite.red = aWhite.green = aWhite.blue = 0xffff;
309 : 0 : aWhite.flags = DoRed | DoGreen | DoBlue;
310 : :
311 : : aPointer =
312 : : XCreateBitmapFromData( m_pDisplay,
313 : : m_aWindow,
314 : : reinterpret_cast<const char*>(pPointerData),
315 : : width,
316 : 0 : height );
317 : : aMask
318 : : = XCreateBitmapFromData( m_pDisplay,
319 : : m_aWindow,
320 : : reinterpret_cast<const char*>(pMaskData),
321 : : width,
322 : 0 : height );
323 : : XLIB_Cursor aCursor =
324 : : XCreatePixmapCursor( m_pDisplay, aPointer, aMask,
325 : : &aBlack, &aWhite,
326 : : hotX,
327 : 0 : hotY );
328 : 0 : XFreePixmap( m_pDisplay, aPointer );
329 : 0 : XFreePixmap( m_pDisplay, aMask );
330 : :
331 : 0 : return aCursor;
332 : : }
333 : :
334 : 0 : void SelectionManager::initialize( const Sequence< Any >& arguments ) throw (::com::sun::star::uno::Exception)
335 : : {
336 : 0 : osl::MutexGuard aGuard(m_aMutex);
337 : :
338 : 0 : if( ! m_xDisplayConnection.is() )
339 : : {
340 : : /*
341 : : * first argument must be a ::com::sun::star::awt::XDisplayConnection
342 : : * from this we will get the XEvents of the vcl event loop by
343 : : * registering us as XEventHandler on it.
344 : : *
345 : : * implementor's note:
346 : : * FIXME:
347 : : * finally the clipboard and XDND service is back in the module it belongs
348 : : * now cleanup and sharing of resources with the normal vcl event loop
349 : : * needs to be added. The display used whould be that of the normal event loop
350 : : * and synchronization should be done via the SolarMutex.
351 : : */
352 : 0 : if( arguments.getLength() > 0 )
353 : 0 : arguments.getConstArray()[0] >>= m_xDisplayConnection;
354 : 0 : if( ! m_xDisplayConnection.is() )
355 : : {
356 : : }
357 : : else
358 : 0 : m_xDisplayConnection->addEventHandler( Any(), this, ~0 );
359 : : }
360 : :
361 : 0 : if( !m_xBitmapConverter.is() )
362 : : {
363 : 0 : if( arguments.getLength() > 2 )
364 : 0 : arguments.getConstArray()[2] >>= m_xBitmapConverter;
365 : : }
366 : :
367 : 0 : if( ! m_pDisplay )
368 : : {
369 : 0 : OUString aUDisplay;
370 : 0 : if( m_xDisplayConnection.is() )
371 : : {
372 : 0 : Any aIdentifier;
373 : 0 : aIdentifier = m_xDisplayConnection->getIdentifier();
374 : 0 : aIdentifier >>= aUDisplay;
375 : : }
376 : :
377 : 0 : OString aDisplayName( OUStringToOString( aUDisplay, RTL_TEXTENCODING_ISO_8859_1 ) );
378 : :
379 : 0 : m_pDisplay = XOpenDisplay( aDisplayName.isEmpty() ? NULL : aDisplayName.getStr());
380 : :
381 : 0 : if( m_pDisplay )
382 : : {
383 : : #ifdef SYNCHRONIZE
384 : : XSynchronize( m_pDisplay, True );
385 : : #endif
386 : : // clipboard selection
387 : 0 : m_nCLIPBOARDAtom = getAtom( OUString("CLIPBOARD") );
388 : :
389 : : // special targets
390 : 0 : m_nTARGETSAtom = getAtom( OUString("TARGETS") );
391 : 0 : m_nTIMESTAMPAtom = getAtom( OUString("TIMESTAMP") );
392 : 0 : m_nTEXTAtom = getAtom( OUString("TEXT") );
393 : 0 : m_nINCRAtom = getAtom( OUString("INCR") );
394 : 0 : m_nCOMPOUNDAtom = getAtom( OUString("COMPOUND_TEXT") );
395 : 0 : m_nMULTIPLEAtom = getAtom( OUString("MULTIPLE") );
396 : 0 : m_nUTF16Atom = getAtom( OUString("ISO10646-1") );
397 : 0 : m_nImageBmpAtom = getAtom( OUString("image/bmp") );
398 : :
399 : : // Atoms for Xdnd protocol
400 : 0 : m_nXdndAware = getAtom( OUString("XdndAware") );
401 : 0 : m_nXdndEnter = getAtom( OUString("XdndEnter") );
402 : 0 : m_nXdndLeave = getAtom( OUString("XdndLeave") );
403 : 0 : m_nXdndPosition = getAtom( OUString("XdndPosition") );
404 : 0 : m_nXdndStatus = getAtom( OUString("XdndStatus") );
405 : 0 : m_nXdndDrop = getAtom( OUString("XdndDrop") );
406 : 0 : m_nXdndFinished = getAtom( OUString("XdndFinished") );
407 : 0 : m_nXdndSelection = getAtom( OUString("XdndSelection") );
408 : 0 : m_nXdndTypeList = getAtom( OUString("XdndTypeList") );
409 : 0 : m_nXdndProxy = getAtom( OUString("XdndProxy") );
410 : 0 : m_nXdndActionCopy = getAtom( OUString("XdndActionCopy") );
411 : 0 : m_nXdndActionMove = getAtom( OUString("XdndActionMove") );
412 : 0 : m_nXdndActionLink = getAtom( OUString("XdndActionLink") );
413 : 0 : m_nXdndActionAsk = getAtom( OUString("XdndActionAsk") );
414 : 0 : m_nXdndActionPrivate= getAtom( OUString("XdndActionPrivate") );
415 : :
416 : : // initialize map with member none
417 : 0 : m_aAtomToString[ 0 ]= OUString("None");
418 : 0 : m_aAtomToString[ XA_PRIMARY ] = OUString("PRIMARY");
419 : :
420 : : // create a (invisible) message window
421 : 0 : m_aWindow = XCreateSimpleWindow( m_pDisplay, DefaultRootWindow( m_pDisplay ),
422 : 0 : 10, 10, 10, 10, 0, 0, 1 );
423 : :
424 : : // initialize threshold for incremetal transfers
425 : : // ICCCM says it should be smaller that the max request size
426 : : // which in turn is guaranteed to be at least 16k bytes
427 : 0 : m_nIncrementalThreshold = XMaxRequestSize( m_pDisplay ) - 1024;
428 : :
429 : 0 : if( m_aWindow )
430 : : {
431 : : // initialize default cursors
432 : : m_aMoveCursor = createCursor( movedata_curs_bits,
433 : : movedata_mask_bits,
434 : : movedata_curs_width,
435 : : movedata_curs_height,
436 : : movedata_curs_x_hot,
437 : 0 : movedata_curs_y_hot );
438 : : m_aCopyCursor = createCursor( copydata_curs_bits,
439 : : copydata_mask_bits,
440 : : copydata_curs_width,
441 : : copydata_curs_height,
442 : : copydata_curs_x_hot,
443 : 0 : copydata_curs_y_hot );
444 : : m_aLinkCursor = createCursor( linkdata_curs_bits,
445 : : linkdata_mask_bits,
446 : : linkdata_curs_width,
447 : : linkdata_curs_height,
448 : : linkdata_curs_x_hot,
449 : 0 : linkdata_curs_y_hot );
450 : : m_aNoneCursor = createCursor( nodrop_curs_bits,
451 : : nodrop_mask_bits,
452 : : nodrop_curs_width,
453 : : nodrop_curs_height,
454 : : nodrop_curs_x_hot,
455 : 0 : nodrop_curs_y_hot );
456 : :
457 : :
458 : :
459 : :
460 : : // just interested in SelectionClear/Notify/Request and PropertyChange
461 : 0 : XSelectInput( m_pDisplay, m_aWindow, PropertyChangeMask );
462 : : // create the transferable for Drag operations
463 : 0 : m_xDropTransferable = new X11Transferable( *this, m_nXdndSelection );
464 : 0 : registerHandler( m_nXdndSelection, *this );
465 : :
466 : 0 : m_aThread = osl_createSuspendedThread( call_SelectionManager_run, this );
467 : 0 : if( m_aThread )
468 : 0 : osl_resumeThread( m_aThread );
469 : : #if OSL_DEBUG_LEVEL > 1
470 : : else
471 : : fprintf( stderr, "SelectionManager::initialize: creation of dispatch thread failed !\n" );
472 : : #endif
473 : : }
474 : 0 : }
475 : 0 : }
476 : 0 : }
477 : :
478 : : // ------------------------------------------------------------------------
479 : :
480 : 0 : SelectionManager::~SelectionManager()
481 : : {
482 : : #if OSL_DEBUG_LEVEL > 1
483 : : fprintf( stderr, "SelectionManager::~SelectionManager (%s)\n", m_pDisplay ? DisplayString(m_pDisplay) : "no display" );
484 : : #endif
485 : : {
486 : 0 : osl::MutexGuard aGuard( *osl::Mutex::getGlobalMutex() );
487 : :
488 : 0 : ::boost::unordered_map< OUString, SelectionManager*, OUStringHash >::iterator it;
489 : 0 : for( it = getInstances().begin(); it != getInstances().end(); ++it )
490 : 0 : if( it->second == this )
491 : : {
492 : 0 : getInstances().erase( it );
493 : 0 : break;
494 : 0 : }
495 : : }
496 : :
497 : 0 : if( m_aThread )
498 : : {
499 : 0 : osl_terminateThread( m_aThread );
500 : 0 : osl_joinWithThread( m_aThread );
501 : 0 : osl_destroyThread( m_aThread );
502 : : }
503 : :
504 : 0 : if( m_aDragExecuteThread )
505 : : {
506 : 0 : osl_terminateThread( m_aDragExecuteThread );
507 : 0 : osl_joinWithThread( m_aDragExecuteThread );
508 : 0 : m_aDragExecuteThread = NULL;
509 : : // thread handle is freed in dragDoDispatch()
510 : : }
511 : :
512 : 0 : osl::MutexGuard aGuard(m_aMutex);
513 : :
514 : : #if OSL_DEBUG_LEVEL > 1
515 : : fprintf( stderr, "shutting down SelectionManager\n" );
516 : : #endif
517 : :
518 : 0 : if( m_pDisplay )
519 : : {
520 : 0 : deregisterHandler( m_nXdndSelection );
521 : : // destroy message window
522 : 0 : if( m_aWindow )
523 : 0 : XDestroyWindow( m_pDisplay, m_aWindow );
524 : : // release cursors
525 : 0 : if (m_aMoveCursor != None)
526 : 0 : XFreeCursor(m_pDisplay, m_aMoveCursor);
527 : 0 : if (m_aCopyCursor != None)
528 : 0 : XFreeCursor(m_pDisplay, m_aCopyCursor);
529 : 0 : if (m_aLinkCursor != None)
530 : 0 : XFreeCursor(m_pDisplay, m_aLinkCursor);
531 : 0 : if (m_aNoneCursor != None)
532 : 0 : XFreeCursor(m_pDisplay, m_aNoneCursor);
533 : :
534 : : // paranoia setting, the drag thread should have
535 : : // done that already
536 : 0 : XUngrabPointer( m_pDisplay, CurrentTime );
537 : 0 : XUngrabKeyboard( m_pDisplay, CurrentTime );
538 : :
539 : 0 : XCloseDisplay( m_pDisplay );
540 : 0 : }
541 : 0 : }
542 : :
543 : : // ------------------------------------------------------------------------
544 : :
545 : 0 : SelectionAdaptor* SelectionManager::getAdaptor( Atom selection )
546 : : {
547 : : ::boost::unordered_map< Atom, Selection* >::iterator it =
548 : 0 : m_aSelections.find( selection );
549 : 0 : return it != m_aSelections.end() ? it->second->m_pAdaptor : NULL;
550 : : }
551 : :
552 : : // ------------------------------------------------------------------------
553 : :
554 : 0 : OUString SelectionManager::convertFromCompound( const char* pText, int nLen )
555 : : {
556 : 0 : osl::MutexGuard aGuard( m_aMutex );
557 : 0 : OUString aRet;
558 : 0 : if( nLen < 0 )
559 : 0 : nLen = strlen( pText );
560 : :
561 : 0 : char** pTextList = NULL;
562 : 0 : int nTexts = 0;
563 : :
564 : : XTextProperty aProp;
565 : 0 : aProp.value = (unsigned char*)pText;
566 : 0 : aProp.encoding = m_nCOMPOUNDAtom;
567 : 0 : aProp.format = 8;
568 : 0 : aProp.nitems = nLen;
569 : : XmbTextPropertyToTextList( m_pDisplay,
570 : : &aProp,
571 : : &pTextList,
572 : 0 : &nTexts );
573 : 0 : rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
574 : 0 : for( int i = 0; i < nTexts; i++ )
575 : 0 : aRet += OStringToOUString( pTextList[i], aEncoding );
576 : :
577 : 0 : if( pTextList )
578 : 0 : XFreeStringList( pTextList );
579 : :
580 : 0 : return aRet;
581 : : }
582 : :
583 : : // ------------------------------------------------------------------------
584 : :
585 : 0 : OString SelectionManager::convertToCompound( const OUString& rText )
586 : : {
587 : 0 : osl::MutexGuard aGuard( m_aMutex );
588 : : XTextProperty aProp;
589 : 0 : aProp.value = NULL;
590 : 0 : aProp.encoding = XA_STRING;
591 : 0 : aProp.format = 8;
592 : 0 : aProp.nitems = 0;
593 : :
594 : 0 : OString aRet( rText.getStr(), rText.getLength(), osl_getThreadTextEncoding() );
595 : 0 : char* pT = const_cast<char*>(aRet.getStr());
596 : :
597 : : XmbTextListToTextProperty( m_pDisplay,
598 : : &pT,
599 : : 1,
600 : : XCompoundTextStyle,
601 : 0 : &aProp );
602 : 0 : if( aProp.value )
603 : : {
604 : 0 : aRet = (char*)aProp.value;
605 : 0 : XFree( aProp.value );
606 : : #ifdef SOLARIS
607 : : /*
608 : : * for currently unknown reasons XmbTextListToTextProperty on Solaris returns
609 : : * no data in ISO8859-n encodings (at least for n = 1, 15)
610 : : * in these encodings the directly converted text does the
611 : : * trick, also.
612 : : */
613 : : if( aRet.isEmpty() && !rText.isEmpty() )
614 : : aRet = OUStringToOString( rText, osl_getThreadTextEncoding() );
615 : : #endif
616 : : }
617 : : else
618 : 0 : aRet = OString();
619 : :
620 : 0 : return aRet;
621 : : }
622 : :
623 : : // ------------------------------------------------------------------------
624 : :
625 : 0 : bool SelectionManager::convertData(
626 : : const css::uno::Reference< XTransferable >& xTransferable,
627 : : Atom nType,
628 : : Atom nSelection,
629 : : int& rFormat,
630 : : Sequence< sal_Int8 >& rData )
631 : : {
632 : 0 : bool bSuccess = false;
633 : :
634 : 0 : if( ! xTransferable.is() )
635 : 0 : return bSuccess;
636 : :
637 : : try
638 : : {
639 : :
640 : 0 : DataFlavor aFlavor;
641 : 0 : aFlavor.MimeType = convertTypeFromNative( nType, nSelection, rFormat );
642 : :
643 : 0 : sal_Int32 nIndex = 0;
644 : 0 : if( aFlavor.MimeType.getToken( 0, ';', nIndex ).compareToAscii( "text/plain" ) == 0 )
645 : : {
646 : 0 : if( aFlavor.MimeType.getToken( 0, ';', nIndex ).compareToAscii( "charset=utf-16" ) == 0 )
647 : 0 : aFlavor.DataType = getCppuType( (OUString *) 0 );
648 : : else
649 : 0 : aFlavor.DataType = getCppuType( (Sequence< sal_Int8 >*)0 );
650 : : }
651 : : else
652 : 0 : aFlavor.DataType = getCppuType( (Sequence< sal_Int8 >*)0 );
653 : :
654 : 0 : if( xTransferable->isDataFlavorSupported( aFlavor ) )
655 : : {
656 : 0 : Any aValue( xTransferable->getTransferData( aFlavor ) );
657 : 0 : if( aValue.getValueTypeClass() == TypeClass_STRING )
658 : : {
659 : 0 : OUString aString;
660 : 0 : aValue >>= aString;
661 : 0 : rData = Sequence< sal_Int8 >( (sal_Int8*)aString.getStr(), aString.getLength() * sizeof( sal_Unicode ) );
662 : 0 : bSuccess = true;
663 : : }
664 : 0 : else if( aValue.getValueType() == getCppuType( (Sequence< sal_Int8 >*)0 ) )
665 : : {
666 : 0 : aValue >>= rData;
667 : 0 : bSuccess = true;
668 : 0 : }
669 : : }
670 : 0 : else if( aFlavor.MimeType.compareToAscii( "text/plain", 10 ) == 0 )
671 : : {
672 : 0 : rtl_TextEncoding aEncoding = RTL_TEXTENCODING_DONTKNOW;
673 : 0 : bool bCompoundText = false;
674 : 0 : if( nType == m_nCOMPOUNDAtom )
675 : 0 : bCompoundText = true;
676 : : else
677 : 0 : aEncoding = getTextPlainEncoding( aFlavor.MimeType );
678 : 0 : if( aEncoding != RTL_TEXTENCODING_DONTKNOW || bCompoundText )
679 : : {
680 : 0 : aFlavor.MimeType = OUString("text/plain;charset=utf-16");
681 : 0 : aFlavor.DataType = getCppuType( (OUString *) 0 );
682 : 0 : if( xTransferable->isDataFlavorSupported( aFlavor ) )
683 : : {
684 : 0 : Any aValue( xTransferable->getTransferData( aFlavor ) );
685 : 0 : OUString aString;
686 : 0 : aValue >>= aString;
687 : 0 : OString aByteString( bCompoundText ? convertToCompound( aString ) : OUStringToOString( aString, aEncoding ) );
688 : 0 : rData = Sequence< sal_Int8 >( (sal_Int8*)aByteString.getStr(), aByteString.getLength() * sizeof( sal_Char ) );
689 : 0 : bSuccess = true;
690 : : }
691 : : }
692 : 0 : }
693 : : }
694 : : // various exceptions possible ... which all lead to a failed conversion
695 : : // so simplify here to a catch all
696 : 0 : catch(...)
697 : : {
698 : : }
699 : :
700 : 0 : return bSuccess;
701 : : }
702 : :
703 : : // ------------------------------------------------------------------------
704 : :
705 : 0 : SelectionManager& SelectionManager::get( const OUString& rDisplayName )
706 : : {
707 : 0 : osl::MutexGuard aGuard( *osl::Mutex::getGlobalMutex() );
708 : :
709 : 0 : OUString aDisplayName( rDisplayName );
710 : 0 : if( aDisplayName.isEmpty() )
711 : 0 : aDisplayName = OStringToOUString( getenv( "DISPLAY" ), RTL_TEXTENCODING_ISO_8859_1 );
712 : 0 : SelectionManager* pInstance = NULL;
713 : :
714 : 0 : ::boost::unordered_map< OUString, SelectionManager*, OUStringHash >::iterator it = getInstances().find( aDisplayName );
715 : 0 : if( it != getInstances().end() )
716 : 0 : pInstance = it->second;
717 : 0 : else pInstance = getInstances()[ aDisplayName ] = new SelectionManager();
718 : :
719 : 0 : return *pInstance;
720 : : }
721 : :
722 : : // ------------------------------------------------------------------------
723 : :
724 : 0 : const OUString& SelectionManager::getString( Atom aAtom )
725 : : {
726 : 0 : osl::MutexGuard aGuard(m_aMutex);
727 : :
728 : 0 : ::boost::unordered_map< Atom, OUString >::const_iterator it;
729 : 0 : if( ( it = m_aAtomToString.find( aAtom ) ) == m_aAtomToString.end() )
730 : : {
731 : 0 : static OUString aEmpty;
732 : 0 : char* pAtom = m_pDisplay ? XGetAtomName( m_pDisplay, aAtom ) : NULL;
733 : 0 : if( ! pAtom )
734 : 0 : return aEmpty;
735 : 0 : OUString aString( OStringToOUString( pAtom, RTL_TEXTENCODING_ISO_8859_1 ) );
736 : 0 : XFree( pAtom );
737 : 0 : m_aStringToAtom[ aString ] = aAtom;
738 : 0 : m_aAtomToString[ aAtom ] = aString;
739 : : }
740 : 0 : return m_aAtomToString[ aAtom ];
741 : : }
742 : :
743 : : // ------------------------------------------------------------------------
744 : :
745 : 0 : Atom SelectionManager::getAtom( const OUString& rString )
746 : : {
747 : 0 : osl::MutexGuard aGuard(m_aMutex);
748 : :
749 : 0 : ::boost::unordered_map< OUString, Atom, OUStringHash >::const_iterator it;
750 : 0 : if( ( it = m_aStringToAtom.find( rString ) ) == m_aStringToAtom.end() )
751 : : {
752 : : static Atom nNoDisplayAtoms = 1;
753 : 0 : Atom aAtom = m_pDisplay ? XInternAtom( m_pDisplay, OUStringToOString( rString, RTL_TEXTENCODING_ISO_8859_1 ).getStr(), False ) : nNoDisplayAtoms++;
754 : 0 : m_aStringToAtom[ rString ] = aAtom;
755 : 0 : m_aAtomToString[ aAtom ] = rString;
756 : : }
757 : 0 : return m_aStringToAtom[ rString ];
758 : : }
759 : :
760 : : // ------------------------------------------------------------------------
761 : :
762 : 0 : bool SelectionManager::requestOwnership( Atom selection )
763 : : {
764 : 0 : bool bSuccess = false;
765 : 0 : if( m_pDisplay && m_aWindow )
766 : : {
767 : 0 : osl::MutexGuard aGuard(m_aMutex);
768 : :
769 : 0 : SelectionAdaptor* pAdaptor = getAdaptor( selection );
770 : 0 : if( pAdaptor )
771 : : {
772 : 0 : XSetSelectionOwner( m_pDisplay, selection, m_aWindow, CurrentTime );
773 : 0 : if( XGetSelectionOwner( m_pDisplay, selection ) == m_aWindow )
774 : 0 : bSuccess = true;
775 : : #if OSL_DEBUG_LEVEL > 1
776 : : fprintf( stderr, "%s ownership for selection %s\n",
777 : : bSuccess ? "acquired" : "failed to acquire",
778 : : OUStringToOString( getString( selection ), RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
779 : : #endif
780 : 0 : Selection* pSel = m_aSelections[ selection ];
781 : 0 : pSel->m_bOwner = bSuccess;
782 : 0 : delete pSel->m_pPixmap;
783 : 0 : pSel->m_pPixmap = NULL;
784 : 0 : pSel->m_nOrigTimestamp = m_nSelectionTimestamp;
785 : 0 : }
786 : : #if OSL_DEBUG_LEVEL > 1
787 : : else
788 : : fprintf( stderr, "no adaptor for selection %s\n",
789 : : OUStringToOString( getString( selection ), RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
790 : :
791 : : if( pAdaptor->getTransferable().is() )
792 : : {
793 : : Sequence< DataFlavor > aTypes = pAdaptor->getTransferable()->getTransferDataFlavors();
794 : : for( int i = 0; i < aTypes.getLength(); i++ )
795 : : {
796 : : fprintf( stderr, " %s\n", OUStringToOString( aTypes.getConstArray()[i].MimeType, RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
797 : : }
798 : : }
799 : : #endif
800 : : }
801 : 0 : return bSuccess;
802 : : }
803 : :
804 : : // ------------------------------------------------------------------------
805 : :
806 : 0 : void SelectionManager::convertTypeToNative( const OUString& rType, Atom selection, int& rFormat, ::std::list< Atom >& rConversions, bool bPushFront )
807 : : {
808 : 0 : NativeTypeEntry* pTab = selection == m_nXdndSelection ? aXdndConversionTab : aNativeConversionTab;
809 : 0 : int nTabEntries = selection == m_nXdndSelection ? SAL_N_ELEMENTS(aXdndConversionTab) : SAL_N_ELEMENTS(aNativeConversionTab);
810 : :
811 : 0 : OString aType( OUStringToOString( rType, RTL_TEXTENCODING_ISO_8859_1 ) );
812 : 0 : rFormat = 0;
813 : 0 : for( int i = 0; i < nTabEntries; i++ )
814 : : {
815 : 0 : if( aType.equalsIgnoreAsciiCase( pTab[i].pType ) )
816 : : {
817 : 0 : if( ! pTab[i].nAtom )
818 : 0 : pTab[i].nAtom = getAtom( OStringToOUString( pTab[i].pNativeType, RTL_TEXTENCODING_ISO_8859_1 ) );
819 : 0 : rFormat = pTab[i].nFormat;
820 : 0 : if( bPushFront )
821 : 0 : rConversions.push_front( pTab[i].nAtom );
822 : : else
823 : 0 : rConversions.push_back( pTab[i].nAtom );
824 : 0 : if( pTab[i].nFormat == XA_PIXMAP )
825 : : {
826 : 0 : if( bPushFront )
827 : : {
828 : 0 : rConversions.push_front( XA_VISUALID );
829 : 0 : rConversions.push_front( XA_COLORMAP );
830 : : }
831 : : else
832 : : {
833 : 0 : rConversions.push_back( XA_VISUALID );
834 : 0 : rConversions.push_back( XA_COLORMAP );
835 : : }
836 : : }
837 : : }
838 : : }
839 : 0 : if( ! rFormat )
840 : 0 : rFormat = 8; // byte buffer
841 : 0 : if( bPushFront )
842 : 0 : rConversions.push_front( getAtom( rType ) );
843 : : else
844 : 0 : rConversions.push_back( getAtom( rType ) );
845 : 0 : };
846 : :
847 : : // ------------------------------------------------------------------------
848 : :
849 : 0 : void SelectionManager::getNativeTypeList( const Sequence< DataFlavor >& rTypes, std::list< Atom >& rOutTypeList, Atom targetselection )
850 : : {
851 : 0 : rOutTypeList.clear();
852 : :
853 : : int nFormat;
854 : 0 : int nFlavors = rTypes.getLength();
855 : 0 : const DataFlavor* pFlavors = rTypes.getConstArray();
856 : 0 : bool bHaveText = false;
857 : 0 : for( int i = 0; i < nFlavors; i++ )
858 : : {
859 : 0 : if( pFlavors[i].MimeType.compareToAscii( "text/plain", 10 ) == 0)
860 : 0 : bHaveText = true;
861 : : else
862 : 0 : convertTypeToNative( pFlavors[i].MimeType, targetselection, nFormat, rOutTypeList );
863 : : }
864 : 0 : if( bHaveText )
865 : : {
866 : 0 : if( targetselection != m_nXdndSelection )
867 : : {
868 : : // only mimetypes should go into Xdnd type list
869 : 0 : rOutTypeList.push_front( XA_STRING );
870 : 0 : rOutTypeList.push_front( m_nCOMPOUNDAtom );
871 : : }
872 : 0 : convertTypeToNative( OUString("text/plain;charset=utf-8"), targetselection, nFormat, rOutTypeList, true );
873 : : }
874 : 0 : if( targetselection != m_nXdndSelection )
875 : 0 : rOutTypeList.push_back( m_nMULTIPLEAtom );
876 : 0 : }
877 : :
878 : : // ------------------------------------------------------------------------
879 : :
880 : 0 : OUString SelectionManager::convertTypeFromNative( Atom nType, Atom selection, int& rFormat )
881 : : {
882 : 0 : NativeTypeEntry* pTab = (selection == m_nXdndSelection) ? aXdndConversionTab : aNativeConversionTab;
883 : 0 : int nTabEntries = (selection == m_nXdndSelection) ? SAL_N_ELEMENTS(aXdndConversionTab) : SAL_N_ELEMENTS(aNativeConversionTab);
884 : :
885 : 0 : for( int i = 0; i < nTabEntries; i++ )
886 : : {
887 : 0 : if( ! pTab[i].nAtom )
888 : 0 : pTab[i].nAtom = getAtom( OStringToOUString( pTab[i].pNativeType, RTL_TEXTENCODING_ISO_8859_1 ) );
889 : 0 : if( nType == pTab[i].nAtom )
890 : : {
891 : 0 : rFormat = pTab[i].nFormat;
892 : 0 : return OStringToOUString( pTab[i].pType, RTL_TEXTENCODING_ISO_8859_1 );
893 : : }
894 : : }
895 : 0 : rFormat = 8;
896 : 0 : return getString( nType );
897 : : }
898 : :
899 : : // ------------------------------------------------------------------------
900 : :
901 : 0 : bool SelectionManager::getPasteData( Atom selection, Atom type, Sequence< sal_Int8 >& rData )
902 : : {
903 : 0 : osl::ResettableMutexGuard aGuard(m_aMutex);
904 : 0 : ::boost::unordered_map< Atom, Selection* >::iterator it;
905 : 0 : bool bSuccess = false;
906 : :
907 : : #if OSL_DEBUG_LEVEL > 1
908 : : OUString aSelection( getString( selection ) );
909 : : OUString aType( getString( type ) );
910 : : fprintf( stderr, "getPasteData( %s, native: %s )\n",
911 : : OUStringToOString( aSelection, RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
912 : : OUStringToOString( aType, RTL_TEXTENCODING_ISO_8859_1 ).getStr()
913 : : );
914 : : #endif
915 : :
916 : 0 : if( ! m_pDisplay )
917 : 0 : return false;
918 : :
919 : 0 : it = m_aSelections.find( selection );
920 : 0 : if( it == m_aSelections.end() )
921 : 0 : return false;
922 : :
923 : 0 : XLIB_Window aSelectionOwner = XGetSelectionOwner( m_pDisplay, selection );
924 : 0 : if( aSelectionOwner == None )
925 : 0 : return false;
926 : 0 : if( aSelectionOwner == m_aWindow )
927 : : {
928 : : // probably bad timing led us here
929 : : #if OSL_DEBUG_LEVEL > 1
930 : : fprintf( stderr, "Innere Nabelschau\n" );
931 : : #endif
932 : 0 : return false;
933 : : }
934 : :
935 : : // ICCCM recommends to destroy property before convert request unless
936 : : // parameters are transported; we do only in case of MULTIPLE,
937 : : // so destroy property unless target is MULTIPLE
938 : 0 : if( type != m_nMULTIPLEAtom )
939 : 0 : XDeleteProperty( m_pDisplay, m_aWindow, selection );
940 : :
941 : 0 : XConvertSelection( m_pDisplay, selection, type, selection, m_aWindow, selection == m_nXdndSelection ? m_nDropTime : CurrentTime );
942 : 0 : it->second->m_eState = Selection::WaitingForResponse;
943 : 0 : it->second->m_aRequestedType = type;
944 : 0 : it->second->m_aData = Sequence< sal_Int8 >();
945 : 0 : it->second->m_aDataArrived.reset();
946 : : // really start the request; if we don't flush the
947 : : // queue the request won't leave it because there are no more
948 : : // X calls after this until the data arrived or timeout
949 : 0 : XFlush( m_pDisplay );
950 : :
951 : : // do a reschedule
952 : : struct timeval tv_last, tv_current;
953 : 0 : gettimeofday( &tv_last, NULL );
954 : 0 : tv_current = tv_last;
955 : :
956 : : XEvent aEvent;
957 : 0 : do
958 : : {
959 : 0 : bool bAdjustTime = false;
960 : : {
961 : 0 : bool bHandle = false;
962 : :
963 : 0 : if( XCheckTypedEvent( m_pDisplay,
964 : : PropertyNotify,
965 : : &aEvent
966 : 0 : ) )
967 : : {
968 : 0 : bHandle = true;
969 : 0 : if( aEvent.xproperty.window == m_aWindow
970 : : && aEvent.xproperty.atom == selection )
971 : 0 : bAdjustTime = true;
972 : : }
973 : : else
974 : 0 : if( XCheckTypedEvent( m_pDisplay,
975 : : SelectionClear,
976 : : &aEvent
977 : 0 : ) )
978 : : {
979 : 0 : bHandle = true;
980 : : }
981 : : else
982 : 0 : if( XCheckTypedEvent( m_pDisplay,
983 : : SelectionRequest,
984 : : &aEvent
985 : 0 : ) )
986 : 0 : bHandle = true;
987 : : else
988 : 0 : if( XCheckTypedEvent( m_pDisplay,
989 : : SelectionNotify,
990 : : &aEvent
991 : 0 : ) )
992 : : {
993 : 0 : bHandle = true;
994 : 0 : if( aEvent.xselection.selection == selection
995 : : && ( aEvent.xselection.requestor == m_aWindow ||
996 : : aEvent.xselection.requestor == m_aCurrentDropWindow )
997 : : )
998 : 0 : bAdjustTime = true;
999 : : }
1000 : : else
1001 : : {
1002 : : TimeValue aTVal;
1003 : 0 : aTVal.Seconds = 0;
1004 : 0 : aTVal.Nanosec = 100000000;
1005 : 0 : aGuard.clear();
1006 : 0 : osl_waitThread( &aTVal );
1007 : 0 : aGuard.reset();
1008 : : }
1009 : 0 : if( bHandle )
1010 : : {
1011 : 0 : aGuard.clear();
1012 : 0 : handleXEvent( aEvent );
1013 : 0 : aGuard.reset();
1014 : : }
1015 : : }
1016 : 0 : gettimeofday( &tv_current, NULL );
1017 : 0 : if( bAdjustTime )
1018 : 0 : tv_last = tv_current;
1019 : 0 : } while( ! it->second->m_aDataArrived.check() && (tv_current.tv_sec - tv_last.tv_sec) < getSelectionTimeout() );
1020 : :
1021 : : #if OSL_DEBUG_LEVEL > 1
1022 : : if( (tv_current.tv_sec - tv_last.tv_sec) > getSelectionTimeout() )
1023 : : fprintf( stderr, "timed out\n" );
1024 : : #endif
1025 : 0 : if( it->second->m_aDataArrived.check() &&
1026 : 0 : it->second->m_aData.getLength() )
1027 : : {
1028 : 0 : rData = it->second->m_aData;
1029 : 0 : bSuccess = true;
1030 : : }
1031 : : #if OSL_DEBUG_LEVEL > 1
1032 : : else
1033 : : fprintf( stderr, "conversion unsuccessfull\n" );
1034 : : #endif
1035 : 0 : return bSuccess;
1036 : : }
1037 : :
1038 : : // ------------------------------------------------------------------------
1039 : :
1040 : 0 : bool SelectionManager::getPasteData( Atom selection, const ::rtl::OUString& rType, Sequence< sal_Int8 >& rData )
1041 : : {
1042 : 0 : bool bSuccess = false;
1043 : :
1044 : 0 : ::boost::unordered_map< Atom, Selection* >::iterator it;
1045 : : {
1046 : 0 : osl::MutexGuard aGuard(m_aMutex);
1047 : :
1048 : 0 : it = m_aSelections.find( selection );
1049 : 0 : if( it == m_aSelections.end() )
1050 : 0 : return false;
1051 : : }
1052 : :
1053 : 0 : if( it->second->m_aTypes.getLength() == 0 )
1054 : : {
1055 : 0 : Sequence< DataFlavor > aFlavors;
1056 : 0 : getPasteDataTypes( selection, aFlavors );
1057 : 0 : if( it->second->m_aTypes.getLength() == 0 )
1058 : 0 : return false;
1059 : : }
1060 : :
1061 : 0 : const Sequence< DataFlavor >& rTypes( it->second->m_aTypes );
1062 : 0 : const std::vector< Atom >& rNativeTypes( it->second->m_aNativeTypes );
1063 : : #if OSL_DEBUG_LEVEL > 1
1064 : : fprintf( stderr, "getPasteData( \"%s\", \"%s\" )\n",
1065 : : OUStringToOString( getString( selection ), RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
1066 : : OUStringToOString( rType, RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
1067 : : #endif
1068 : :
1069 : 0 : if( rType.equalsAsciiL( "text/plain;charset=utf-16", 25 ) )
1070 : : {
1071 : : // lets see if we have UTF16 else try to find something convertible
1072 : 0 : if( it->second->m_aTypes.getLength() && ! it->second->m_bHaveUTF16 )
1073 : : {
1074 : 0 : Sequence< sal_Int8 > aData;
1075 : 0 : if( it->second->m_aUTF8Type != None &&
1076 : : getPasteData( selection,
1077 : 0 : it->second->m_aUTF8Type,
1078 : 0 : aData )
1079 : : )
1080 : : {
1081 : 0 : OUString aRet( (const sal_Char*)aData.getConstArray(), aData.getLength(), RTL_TEXTENCODING_UTF8 );
1082 : 0 : rData = Sequence< sal_Int8 >( (sal_Int8*)aRet.getStr(), (aRet.getLength()+1)*sizeof( sal_Unicode ) );
1083 : 0 : bSuccess = true;
1084 : : }
1085 : 0 : else if( it->second->m_bHaveCompound &&
1086 : : getPasteData( selection,
1087 : : m_nCOMPOUNDAtom,
1088 : 0 : aData )
1089 : : )
1090 : : {
1091 : 0 : OUString aRet( convertFromCompound( (const char*)aData.getConstArray(), aData.getLength() ) );
1092 : 0 : rData = Sequence< sal_Int8 >( (sal_Int8*)aRet.getStr(), (aRet.getLength()+1)*sizeof( sal_Unicode ) );
1093 : 0 : bSuccess = true;
1094 : : }
1095 : : else
1096 : : {
1097 : 0 : for( int i = 0; i < rTypes.getLength(); i++ )
1098 : : {
1099 : 0 : rtl_TextEncoding aEncoding = getTextPlainEncoding( rTypes.getConstArray()[i].MimeType );
1100 : 0 : if( aEncoding != RTL_TEXTENCODING_DONTKNOW &&
1101 : : aEncoding != RTL_TEXTENCODING_UNICODE &&
1102 : : getPasteData( selection,
1103 : 0 : rNativeTypes[i],
1104 : 0 : aData )
1105 : : )
1106 : : {
1107 : : #if OSL_DEBUG_LEVEL > 1
1108 : : fprintf( stderr, "using \"%s\" instead of \"%s\"\n",
1109 : : OUStringToOString( rTypes.getConstArray()[i].MimeType, RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
1110 : : OUStringToOString( rType, RTL_TEXTENCODING_ISO_8859_1 ).getStr()
1111 : : );
1112 : : #endif
1113 : 0 : OString aConvert( (sal_Char*)aData.getConstArray(), aData.getLength() );
1114 : 0 : OUString aUTF( OStringToOUString( aConvert, aEncoding ) );
1115 : 0 : rData = Sequence< sal_Int8 >( (sal_Int8*)aUTF.getStr(), (aUTF.getLength()+1)*sizeof( sal_Unicode ) );
1116 : 0 : bSuccess = true;
1117 : 0 : break;
1118 : : }
1119 : : }
1120 : 0 : }
1121 : : }
1122 : : }
1123 : 0 : else if( rType.equalsAsciiL( "image/bmp", 9 ) )
1124 : : {
1125 : : // #i83376# try if someone has the data in image/bmp already before
1126 : : // doing the PIXMAP stuff (e.g. the gimp has this)
1127 : 0 : bSuccess = getPasteData( selection, m_nImageBmpAtom, rData );
1128 : : #if OSL_DEBUG_LEVEL > 1
1129 : : if( bSuccess )
1130 : : fprintf( stderr, "got %d bytes of image/bmp\n", (int)rData.getLength() );
1131 : : #endif
1132 : 0 : if( ! bSuccess )
1133 : : {
1134 : 0 : Pixmap aPixmap = None;
1135 : 0 : Colormap aColormap = None;
1136 : :
1137 : : // prepare property for MULTIPLE request
1138 : 0 : Sequence< sal_Int8 > aData;
1139 : : Atom pTypes[4] = { XA_PIXMAP, XA_PIXMAP,
1140 : 0 : XA_COLORMAP, XA_COLORMAP };
1141 : : {
1142 : 0 : osl::MutexGuard aGuard(m_aMutex);
1143 : :
1144 : : XChangeProperty( m_pDisplay,
1145 : : m_aWindow,
1146 : : selection,
1147 : : XA_ATOM,
1148 : : 32,
1149 : : PropModeReplace,
1150 : : (unsigned char*)pTypes,
1151 : 0 : 4 );
1152 : : }
1153 : :
1154 : : // try MULTIPLE request
1155 : 0 : if( getPasteData( selection, m_nMULTIPLEAtom, aData ) )
1156 : : {
1157 : 0 : Atom* pReturnedTypes = (Atom*)aData.getArray();
1158 : 0 : if( pReturnedTypes[0] == XA_PIXMAP && pReturnedTypes[1] == XA_PIXMAP )
1159 : : {
1160 : 0 : osl::MutexGuard aGuard(m_aMutex);
1161 : :
1162 : 0 : Atom type = None;
1163 : 0 : int format = 0;
1164 : 0 : unsigned long nItems = 0;
1165 : 0 : unsigned long nBytes = 0;
1166 : 0 : unsigned char* pReturn = NULL;
1167 : 0 : XGetWindowProperty( m_pDisplay, m_aWindow, XA_PIXMAP, 0, 1, True, XA_PIXMAP, &type, &format, &nItems, &nBytes, &pReturn );
1168 : 0 : if( pReturn )
1169 : : {
1170 : 0 : if( type == XA_PIXMAP )
1171 : 0 : aPixmap = *(Pixmap*)pReturn;
1172 : 0 : XFree( pReturn );
1173 : 0 : pReturn = NULL;
1174 : 0 : if( pReturnedTypes[2] == XA_COLORMAP && pReturnedTypes[3] == XA_COLORMAP )
1175 : : {
1176 : 0 : XGetWindowProperty( m_pDisplay, m_aWindow, XA_COLORMAP, 0, 1, True, XA_COLORMAP, &type, &format, &nItems, &nBytes, &pReturn );
1177 : 0 : if( pReturn )
1178 : : {
1179 : 0 : if( type == XA_COLORMAP )
1180 : 0 : aColormap = *(Colormap*)pReturn;
1181 : 0 : XFree( pReturn );
1182 : : }
1183 : : }
1184 : 0 : }
1185 : : #if OSL_DEBUG_LEVEL > 1
1186 : : else
1187 : : {
1188 : : fprintf( stderr, "could not get PIXMAP property: type=%s, format=%d, items=%ld, bytes=%ld, ret=0x%p\n", OUStringToOString( getString( type ), RTL_TEXTENCODING_ISO_8859_1 ).getStr(), format, nItems, nBytes, pReturn );
1189 : : }
1190 : : #endif
1191 : : }
1192 : : }
1193 : :
1194 : 0 : if( aPixmap == None )
1195 : : {
1196 : : // perhaps two normal requests will work
1197 : 0 : if( getPasteData( selection, XA_PIXMAP, aData ) )
1198 : : {
1199 : 0 : aPixmap = *(Pixmap*)aData.getArray();
1200 : 0 : if( aColormap == None && getPasteData( selection, XA_COLORMAP, aData ) )
1201 : 0 : aColormap = *(Colormap*)aData.getArray();
1202 : : }
1203 : : }
1204 : :
1205 : : // convert data if possible
1206 : 0 : if( aPixmap != None )
1207 : : {
1208 : 0 : osl::MutexGuard aGuard(m_aMutex);
1209 : :
1210 : 0 : sal_Int32 nOutSize = 0;
1211 : 0 : sal_uInt8* pBytes = X11_getBmpFromPixmap( m_pDisplay, aPixmap, aColormap, nOutSize );
1212 : 0 : if( pBytes && nOutSize )
1213 : : {
1214 : 0 : rData = Sequence< sal_Int8 >( nOutSize );
1215 : 0 : memcpy( rData.getArray(), pBytes, nOutSize );
1216 : 0 : X11_freeBmp( pBytes );
1217 : 0 : bSuccess = true;
1218 : 0 : }
1219 : 0 : }
1220 : : }
1221 : : }
1222 : :
1223 : 0 : if( ! bSuccess )
1224 : : {
1225 : : int nFormat;
1226 : 0 : ::std::list< Atom > aTypes;
1227 : 0 : convertTypeToNative( rType, selection, nFormat, aTypes );
1228 : 0 : ::std::list< Atom >::const_iterator type_it;
1229 : 0 : Atom nSelectedType = None;
1230 : 0 : for( type_it = aTypes.begin(); type_it != aTypes.end() && nSelectedType == None; ++type_it )
1231 : : {
1232 : 0 : for( unsigned int i = 0; i < rNativeTypes.size() && nSelectedType == None; i++ )
1233 : 0 : if( rNativeTypes[i] == *type_it )
1234 : 0 : nSelectedType = *type_it;
1235 : : }
1236 : 0 : if( nSelectedType != None )
1237 : 0 : bSuccess = getPasteData( selection, nSelectedType, rData );
1238 : : }
1239 : : #if OSL_DEBUG_LEVEL > 1
1240 : : fprintf( stderr, "getPasteData for selection %s and data type %s returns %s, returned sequence has length %" SAL_PRIdINT32 "\n",
1241 : : OUStringToOString( getString( selection ), RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
1242 : : OUStringToOString( rType, RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
1243 : : bSuccess ? "true" : "false",
1244 : : rData.getLength()
1245 : : );
1246 : : #endif
1247 : 0 : return bSuccess;
1248 : : }
1249 : :
1250 : : // ------------------------------------------------------------------------
1251 : :
1252 : 0 : bool SelectionManager::getPasteDataTypes( Atom selection, Sequence< DataFlavor >& rTypes )
1253 : : {
1254 : 0 : ::boost::unordered_map< Atom, Selection* >::iterator it;
1255 : : {
1256 : 0 : osl::MutexGuard aGuard(m_aMutex);
1257 : :
1258 : 0 : it = m_aSelections.find( selection );
1259 : 0 : if( it != m_aSelections.end() &&
1260 : 0 : it->second->m_aTypes.getLength() &&
1261 : 0 : abs( it->second->m_nLastTimestamp - time( NULL ) ) < 2
1262 : : )
1263 : : {
1264 : 0 : rTypes = it->second->m_aTypes;
1265 : 0 : return true;
1266 : 0 : }
1267 : : }
1268 : :
1269 : 0 : bool bSuccess = false;
1270 : 0 : bool bHaveUTF16 = false;
1271 : 0 : Atom aUTF8Type = None;
1272 : 0 : bool bHaveCompound = false;
1273 : 0 : bool bHaveText = false;
1274 : 0 : Sequence< sal_Int8 > aAtoms;
1275 : :
1276 : 0 : if( selection == m_nXdndSelection )
1277 : : {
1278 : : // xdnd sends first three types with XdndEnter
1279 : : // if more than three types are supported then the XDndTypeList
1280 : : // property on the source window is used
1281 : 0 : if( m_aDropEnterEvent.data.l[0] && m_aCurrentDropWindow )
1282 : : {
1283 : 0 : if( m_aDropEnterEvent.data.l[1] & 1 )
1284 : : {
1285 : 0 : const unsigned int atomcount = 256;
1286 : : // more than three types; look in property
1287 : 0 : osl::MutexGuard aGuard(m_aMutex);
1288 : :
1289 : : Atom nType;
1290 : : int nFormat;
1291 : : unsigned long nItems, nBytes;
1292 : 0 : unsigned char* pBytes = NULL;
1293 : :
1294 : 0 : XGetWindowProperty( m_pDisplay, m_aDropEnterEvent.data.l[0],
1295 : : m_nXdndTypeList, 0, atomcount, False,
1296 : : XA_ATOM,
1297 : 0 : &nType, &nFormat, &nItems, &nBytes, &pBytes );
1298 : : #if OSL_DEBUG_LEVEL > 1
1299 : : fprintf( stderr, "have %ld data types in XdndTypeList\n", nItems );
1300 : : #endif
1301 : 0 : if( nItems == atomcount && nBytes > 0 )
1302 : : {
1303 : : // wow ... more than 256 types !
1304 : 0 : aAtoms.realloc( sizeof( Atom )*atomcount+nBytes );
1305 : 0 : memcpy( aAtoms.getArray(), pBytes, sizeof( Atom )*atomcount );
1306 : 0 : XFree( pBytes );
1307 : 0 : pBytes = NULL;
1308 : 0 : XGetWindowProperty( m_pDisplay, m_aDropEnterEvent.data.l[0],
1309 : : m_nXdndTypeList, atomcount, nBytes/sizeof(Atom),
1310 : : False, XA_ATOM,
1311 : 0 : &nType, &nFormat, &nItems, &nBytes, &pBytes );
1312 : : {
1313 : 0 : memcpy( aAtoms.getArray()+atomcount*sizeof(Atom), pBytes, nItems*sizeof(Atom) );
1314 : 0 : XFree( pBytes );
1315 : : }
1316 : : }
1317 : : else
1318 : : {
1319 : 0 : aAtoms.realloc( sizeof(Atom)*nItems );
1320 : 0 : memcpy( aAtoms.getArray(), pBytes, nItems*sizeof(Atom) );
1321 : 0 : XFree( pBytes );
1322 : 0 : }
1323 : : }
1324 : : else
1325 : : {
1326 : : // one to three types
1327 : 0 : int n = 0, i;
1328 : 0 : for( i = 0; i < 3; i++ )
1329 : 0 : if( m_aDropEnterEvent.data.l[2+i] )
1330 : 0 : n++;
1331 : : #if OSL_DEBUG_LEVEL > 1
1332 : : fprintf( stderr, "have %d data types in XdndEnter\n", n );
1333 : : #endif
1334 : 0 : aAtoms.realloc( sizeof(Atom)*n );
1335 : 0 : for( i = 0, n = 0; i < 3; i++ )
1336 : 0 : if( m_aDropEnterEvent.data.l[2+i] )
1337 : 0 : ((Atom*)aAtoms.getArray())[n++] = m_aDropEnterEvent.data.l[2+i];
1338 : : }
1339 : : }
1340 : : }
1341 : : // get data of type TARGETS
1342 : 0 : else if( ! getPasteData( selection, m_nTARGETSAtom, aAtoms ) )
1343 : 0 : aAtoms = Sequence< sal_Int8 >();
1344 : :
1345 : 0 : std::vector< Atom > aNativeTypes;
1346 : 0 : if( aAtoms.getLength() )
1347 : : {
1348 : 0 : sal_Int32 nAtoms = aAtoms.getLength() / sizeof(Atom);
1349 : 0 : Atom* pAtoms = (Atom*)aAtoms.getArray();
1350 : 0 : rTypes.realloc( nAtoms );
1351 : 0 : aNativeTypes.resize( nAtoms );
1352 : 0 : DataFlavor* pFlavors = rTypes.getArray();
1353 : 0 : sal_Int32 nNativeTypesIndex = 0;
1354 : 0 : while( nAtoms-- )
1355 : : {
1356 : : #if OSL_DEBUG_LEVEL > 1
1357 : : if( *pAtoms && *pAtoms < 0x01000000 )
1358 : : fprintf( stderr, "native type: %s\n", OUStringToOString( getString( *pAtoms ), RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
1359 : : #endif
1360 : 0 : if( *pAtoms == m_nCOMPOUNDAtom )
1361 : 0 : bHaveText = bHaveCompound = true;
1362 : 0 : else if( *pAtoms && *pAtoms < 0x01000000 )
1363 : : {
1364 : : int nFormat;
1365 : 0 : pFlavors->MimeType = convertTypeFromNative( *pAtoms, selection, nFormat );
1366 : 0 : pFlavors->DataType = getCppuType( (Sequence< sal_Int8 >*)0 );
1367 : 0 : sal_Int32 nIndex = 0;
1368 : 0 : if( pFlavors->MimeType.getToken( 0, ';', nIndex ).equalsAsciiL( "text/plain", 10 ) )
1369 : : {
1370 : 0 : OUString aToken(pFlavors->MimeType.getToken( 0, ';', nIndex ));
1371 : : // omit text/plain;charset=unicode since it is not well defined
1372 : 0 : if( aToken.compareToAscii( "charset=unicode" ) == 0 )
1373 : : {
1374 : 0 : pAtoms++;
1375 : 0 : continue;
1376 : : }
1377 : 0 : bHaveText = true;
1378 : 0 : if( aToken.compareToAscii( "charset=utf-16" ) == 0 )
1379 : : {
1380 : 0 : bHaveUTF16 = true;
1381 : 0 : pFlavors->DataType = getCppuType( (OUString*)0 );
1382 : : }
1383 : 0 : else if( aToken.compareToAscii( "charset=utf-8" ) == 0 )
1384 : : {
1385 : 0 : aUTF8Type = *pAtoms;
1386 : 0 : }
1387 : : }
1388 : 0 : pFlavors++;
1389 : 0 : aNativeTypes[ nNativeTypesIndex ] = *pAtoms;
1390 : 0 : nNativeTypesIndex++;
1391 : : }
1392 : 0 : pAtoms++;
1393 : : }
1394 : 0 : if( (pFlavors - rTypes.getArray()) < rTypes.getLength() )
1395 : 0 : rTypes.realloc(pFlavors - rTypes.getArray());
1396 : 0 : bSuccess = rTypes.getLength() ? true : false;
1397 : 0 : if( bHaveText && ! bHaveUTF16 )
1398 : : {
1399 : 0 : int i = 0;
1400 : :
1401 : 0 : int nNewFlavors = rTypes.getLength()+1;
1402 : 0 : Sequence< DataFlavor > aTemp( nNewFlavors );
1403 : 0 : for( i = 0; i < nNewFlavors-1; i++ )
1404 : 0 : aTemp.getArray()[i+1] = rTypes.getConstArray()[i];
1405 : 0 : aTemp.getArray()[0].MimeType = OUString("text/plain;charset=utf-16");
1406 : 0 : aTemp.getArray()[0].DataType = getCppuType( (OUString*)0 );
1407 : 0 : rTypes = aTemp;
1408 : :
1409 : 0 : std::vector< Atom > aNativeTemp( nNewFlavors );
1410 : 0 : for( i = 0; i < nNewFlavors-1; i++ )
1411 : 0 : aNativeTemp[ i + 1 ] = aNativeTypes[ i ];
1412 : 0 : aNativeTemp[0] = None;
1413 : 0 : aNativeTypes = aNativeTemp;
1414 : : }
1415 : : }
1416 : :
1417 : : {
1418 : 0 : osl::MutexGuard aGuard(m_aMutex);
1419 : :
1420 : 0 : it = m_aSelections.find( selection );
1421 : 0 : if( it != m_aSelections.end() )
1422 : : {
1423 : 0 : if( bSuccess )
1424 : : {
1425 : 0 : it->second->m_aTypes = rTypes;
1426 : 0 : it->second->m_aNativeTypes = aNativeTypes;
1427 : 0 : it->second->m_nLastTimestamp = time( NULL );
1428 : 0 : it->second->m_bHaveUTF16 = bHaveUTF16;
1429 : 0 : it->second->m_aUTF8Type = aUTF8Type;
1430 : 0 : it->second->m_bHaveCompound = bHaveCompound;
1431 : : }
1432 : : else
1433 : : {
1434 : 0 : it->second->m_aTypes = Sequence< DataFlavor >();
1435 : 0 : it->second->m_aNativeTypes = std::vector< Atom >();
1436 : 0 : it->second->m_nLastTimestamp = 0;
1437 : 0 : it->second->m_bHaveUTF16 = false;
1438 : 0 : it->second->m_aUTF8Type = None;
1439 : 0 : it->second->m_bHaveCompound = false;
1440 : : }
1441 : 0 : }
1442 : : }
1443 : :
1444 : : #if OSL_DEBUG_LEVEL > 1
1445 : : {
1446 : : fprintf( stderr, "SelectionManager::getPasteDataTypes( %s ) = %s\n", OUStringToOString( getString( selection ), RTL_TEXTENCODING_ISO_8859_1 ).getStr(), bSuccess ? "true" : "false" );
1447 : : for( int i = 0; i < rTypes.getLength(); i++ )
1448 : : fprintf( stderr, "type: %s\n", OUStringToOString( rTypes.getConstArray()[i].MimeType, RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
1449 : : }
1450 : : #endif
1451 : :
1452 : 0 : return bSuccess;
1453 : : }
1454 : :
1455 : : // ------------------------------------------------------------------------
1456 : :
1457 : 0 : PixmapHolder* SelectionManager::getPixmapHolder( Atom selection )
1458 : : {
1459 : 0 : boost::unordered_map< Atom, Selection* >::const_iterator it = m_aSelections.find( selection );
1460 : 0 : if( it == m_aSelections.end() )
1461 : 0 : return NULL;
1462 : 0 : if( ! it->second->m_pPixmap )
1463 : 0 : it->second->m_pPixmap = new PixmapHolder( m_pDisplay );
1464 : 0 : return it->second->m_pPixmap;
1465 : : }
1466 : :
1467 : 0 : static sal_Size GetTrueFormatSize(int nFormat)
1468 : : {
1469 : : // http://mail.gnome.org/archives/wm-spec-list/2003-March/msg00067.html
1470 : 0 : return nFormat == 32 ? sizeof(long) : nFormat/8;
1471 : : }
1472 : :
1473 : 0 : bool SelectionManager::sendData( SelectionAdaptor* pAdaptor,
1474 : : XLIB_Window requestor,
1475 : : Atom target,
1476 : : Atom property,
1477 : : Atom selection )
1478 : : {
1479 : 0 : osl::ResettableMutexGuard aGuard( m_aMutex );
1480 : :
1481 : : // handle targets related to image/bmp
1482 : 0 : if( target == XA_COLORMAP || target == XA_PIXMAP || target == XA_BITMAP || target == XA_VISUALID )
1483 : : {
1484 : 0 : PixmapHolder* pPixmap = getPixmapHolder( selection );
1485 : 0 : if( ! pPixmap ) return false;
1486 : 0 : XID nValue = None;
1487 : :
1488 : : // handle colormap request
1489 : 0 : if( target == XA_COLORMAP )
1490 : 0 : nValue = (XID)pPixmap->getColormap();
1491 : 0 : else if( target == XA_VISUALID )
1492 : 0 : nValue = (XID)pPixmap->getVisualID();
1493 : 0 : else if( target == XA_PIXMAP || target == XA_BITMAP )
1494 : : {
1495 : 0 : nValue = (XID)pPixmap->getPixmap();
1496 : 0 : if( nValue == None )
1497 : : {
1498 : : // first conversion
1499 : 0 : Sequence< sal_Int8 > aData;
1500 : : int nFormat;
1501 : 0 : aGuard.clear();
1502 : 0 : bool bConverted = convertData( pAdaptor->getTransferable(), target, selection, nFormat, aData );
1503 : 0 : aGuard.reset();
1504 : 0 : if( bConverted )
1505 : : {
1506 : : // get pixmap again since clearing the guard could have invalidated
1507 : : // the pixmap in another thread
1508 : 0 : pPixmap = getPixmapHolder( selection );
1509 : : // conversion succeeded, so aData contains image/bmp now
1510 : 0 : if( pPixmap->needsConversion( (const sal_uInt8*)aData.getConstArray() )
1511 : 0 : && m_xBitmapConverter.is() )
1512 : : {
1513 : : #if OSL_DEBUG_LEVEL > 1
1514 : : fprintf( stderr, "trying bitmap conversion\n" );
1515 : : #endif
1516 : 0 : css::uno::Reference<XBitmap> xBM( new BmpTransporter( aData ) );
1517 : 0 : Sequence<Any> aArgs(2), aOutArgs;
1518 : 0 : Sequence<sal_Int16> aOutIndex;
1519 : 0 : aArgs.getArray()[0] = makeAny( xBM );
1520 : 0 : aArgs.getArray()[1] = makeAny( (sal_uInt16)pPixmap->getDepth() );
1521 : 0 : aGuard.clear();
1522 : : try
1523 : : {
1524 : : Any aResult =
1525 : 0 : m_xBitmapConverter->invoke( OUString("convert-bitmap-depth"),
1526 : 0 : aArgs, aOutIndex, aOutArgs );
1527 : 0 : if( aResult >>= xBM )
1528 : 0 : aData = xBM->getDIB();
1529 : : }
1530 : 0 : catch(...)
1531 : : {
1532 : : #if OSL_DEBUG_LEVEL > 1
1533 : : fprintf( stderr, "exception in bitmap converter\n" );
1534 : : #endif
1535 : : }
1536 : 0 : aGuard.reset();
1537 : : }
1538 : : // get pixmap again since clearing the guard could have invalidated
1539 : : // the pixmap in another thread
1540 : 0 : pPixmap = getPixmapHolder( selection );
1541 : 0 : nValue = (XID)pPixmap->setBitmapData( (const sal_uInt8*)aData.getConstArray() );
1542 : : }
1543 : 0 : if( nValue == None )
1544 : 0 : return false;
1545 : : }
1546 : 0 : if( target == XA_BITMAP )
1547 : 0 : nValue = (XID)pPixmap->getBitmap();
1548 : : }
1549 : :
1550 : : XChangeProperty( m_pDisplay,
1551 : : requestor,
1552 : : property,
1553 : : target,
1554 : : 32,
1555 : : PropModeReplace,
1556 : : (const unsigned char*)&nValue,
1557 : 0 : 1);
1558 : 0 : return true;
1559 : : }
1560 : :
1561 : : /*
1562 : : * special target TEXT allows us to transfer
1563 : : * the data in an encoding of our choice
1564 : : * COMPOUND_TEXT will work with most applications
1565 : : */
1566 : 0 : if( target == m_nTEXTAtom )
1567 : 0 : target = m_nCOMPOUNDAtom;
1568 : :
1569 : 0 : Sequence< sal_Int8 > aData;
1570 : : int nFormat;
1571 : 0 : aGuard.clear();
1572 : 0 : bool bConverted = convertData( pAdaptor->getTransferable(), target, selection, nFormat, aData );
1573 : 0 : aGuard.reset();
1574 : 0 : if( bConverted )
1575 : : {
1576 : : // conversion succeeded
1577 : 0 : if( aData.getLength() > m_nIncrementalThreshold )
1578 : : {
1579 : : #if OSL_DEBUG_LEVEL > 1
1580 : : fprintf( stderr, "using INCR protocol\n" );
1581 : : boost::unordered_map< XLIB_Window, boost::unordered_map< Atom, IncrementalTransfer > >::const_iterator win_it = m_aIncrementals.find( requestor );
1582 : : if( win_it != m_aIncrementals.end() )
1583 : : {
1584 : : boost::unordered_map< Atom, IncrementalTransfer >::const_iterator inc_it = win_it->second.find( property );
1585 : : if( inc_it != win_it->second.end() )
1586 : : {
1587 : : const IncrementalTransfer& rInc = inc_it->second;
1588 : : fprintf( stderr, "premature end and new start for INCR transfer for window 0x%lx, property %s, type %s\n",
1589 : : rInc.m_aRequestor,
1590 : : OUStringToOString( getString( rInc.m_aProperty ), RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
1591 : : OUStringToOString( getString( rInc.m_aTarget ), RTL_TEXTENCODING_ISO_8859_1 ).getStr()
1592 : : );
1593 : : }
1594 : : }
1595 : : #endif
1596 : :
1597 : : // insert IncrementalTransfer
1598 : 0 : IncrementalTransfer& rInc = m_aIncrementals[ requestor ][ property ];
1599 : 0 : rInc.m_aData = aData;
1600 : 0 : rInc.m_nBufferPos = 0;
1601 : 0 : rInc.m_aRequestor = requestor;
1602 : 0 : rInc.m_aProperty = property;
1603 : 0 : rInc.m_aTarget = target;
1604 : 0 : rInc.m_nFormat = nFormat;
1605 : 0 : rInc.m_nTransferStartTime = time( NULL );
1606 : :
1607 : : // use incr protocol, signal start to requestor
1608 : 0 : long nMinSize = m_nIncrementalThreshold;
1609 : 0 : XSelectInput( m_pDisplay, requestor, PropertyChangeMask );
1610 : : XChangeProperty( m_pDisplay, requestor, property,
1611 : 0 : m_nINCRAtom, 32, PropModeReplace, (unsigned char*)&nMinSize, 1 );
1612 : 0 : XFlush( m_pDisplay );
1613 : : }
1614 : : else
1615 : : {
1616 : 0 : sal_Size nUnitSize = GetTrueFormatSize(nFormat);
1617 : : XChangeProperty( m_pDisplay,
1618 : : requestor,
1619 : : property,
1620 : : target,
1621 : : nFormat,
1622 : : PropModeReplace,
1623 : 0 : (const unsigned char*)aData.getConstArray(),
1624 : 0 : aData.getLength()/nUnitSize );
1625 : : }
1626 : : }
1627 : : #if OSL_DEBUG_LEVEL > 1
1628 : : else
1629 : : fprintf( stderr, "convertData failed for type: %s \n",
1630 : : OUStringToOString( convertTypeFromNative( target, selection, nFormat ), RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
1631 : : #endif
1632 : 0 : return bConverted;
1633 : : }
1634 : :
1635 : : // ------------------------------------------------------------------------
1636 : :
1637 : 0 : bool SelectionManager::handleSelectionRequest( XSelectionRequestEvent& rRequest )
1638 : : {
1639 : 0 : osl::ResettableMutexGuard aGuard( m_aMutex );
1640 : : #if OSL_DEBUG_LEVEL > 1
1641 : : fprintf( stderr, "handleSelectionRequest for selection %s and target %s\n",
1642 : : OUStringToOString( getString( rRequest.selection ), RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
1643 : : OUStringToOString( getString( rRequest.target ), RTL_TEXTENCODING_ISO_8859_1 ).getStr()
1644 : : );
1645 : : #endif
1646 : :
1647 : : XEvent aNotify;
1648 : :
1649 : 0 : aNotify.type = SelectionNotify;
1650 : 0 : aNotify.xselection.display = rRequest.display;
1651 : 0 : aNotify.xselection.send_event = True;
1652 : 0 : aNotify.xselection.requestor = rRequest.requestor;
1653 : 0 : aNotify.xselection.selection = rRequest.selection;
1654 : 0 : aNotify.xselection.time = rRequest.time;
1655 : 0 : aNotify.xselection.target = rRequest.target;
1656 : 0 : aNotify.xselection.property = None;
1657 : :
1658 : 0 : SelectionAdaptor* pAdaptor = getAdaptor( rRequest.selection );
1659 : : // ensure that we still own that selection
1660 : 0 : if( pAdaptor &&
1661 : 0 : XGetSelectionOwner( m_pDisplay, rRequest.selection ) == m_aWindow )
1662 : : {
1663 : 0 : css::uno::Reference< XTransferable > xTrans( pAdaptor->getTransferable() );
1664 : 0 : if( rRequest.target == m_nTARGETSAtom )
1665 : : {
1666 : : // someone requests our types
1667 : 0 : if( xTrans.is() )
1668 : : {
1669 : 0 : aGuard.clear();
1670 : 0 : Sequence< DataFlavor > aFlavors = xTrans->getTransferDataFlavors();
1671 : 0 : aGuard.reset();
1672 : :
1673 : 0 : ::std::list< Atom > aConversions;
1674 : 0 : getNativeTypeList( aFlavors, aConversions, rRequest.selection );
1675 : :
1676 : 0 : int i, nTypes = aConversions.size();
1677 : 0 : Atom* pTypes = (Atom*)alloca( nTypes * sizeof( Atom ) );
1678 : 0 : std::list< Atom >::const_iterator it;
1679 : 0 : for( i = 0, it = aConversions.begin(); i < nTypes; i++, ++it )
1680 : 0 : pTypes[i] = *it;
1681 : : XChangeProperty( m_pDisplay, rRequest.requestor, rRequest.property,
1682 : 0 : XA_ATOM, 32, PropModeReplace, (const unsigned char*)pTypes, nTypes );
1683 : 0 : aNotify.xselection.property = rRequest.property;
1684 : : #if OSL_DEBUG_LEVEL > 1
1685 : : fprintf( stderr, "sending type list:\n" );
1686 : : for( int k = 0; k < nTypes; k++ )
1687 : : fprintf( stderr, " %s\n", pTypes[k] ? XGetAtomName( m_pDisplay, pTypes[k] ) : "<None>" );
1688 : : #endif
1689 : : }
1690 : : }
1691 : 0 : else if( rRequest.target == m_nTIMESTAMPAtom )
1692 : : {
1693 : 0 : long nTimeStamp = (long)m_aSelections[rRequest.selection]->m_nOrigTimestamp;
1694 : : XChangeProperty( m_pDisplay, rRequest.requestor, rRequest.property,
1695 : 0 : XA_INTEGER, 32, PropModeReplace, (const unsigned char*)&nTimeStamp, 1 );
1696 : 0 : aNotify.xselection.property = rRequest.property;
1697 : : #if OSL_DEBUG_LEVEL > 1
1698 : : fprintf( stderr, "sending timestamp: %d\n", (int)nTimeStamp );
1699 : : #endif
1700 : : }
1701 : : else
1702 : : {
1703 : 0 : bool bEventSuccess = false;
1704 : 0 : if( rRequest.target == m_nMULTIPLEAtom )
1705 : : {
1706 : : // get all targets
1707 : 0 : Atom nType = None;
1708 : 0 : int nFormat = 0;
1709 : 0 : unsigned long nItems = 0, nBytes = 0;
1710 : 0 : unsigned char* pData = NULL;
1711 : :
1712 : : // get number of atoms
1713 : : XGetWindowProperty( m_pDisplay,
1714 : : rRequest.requestor,
1715 : : rRequest.property,
1716 : : 0, 0,
1717 : : False,
1718 : : AnyPropertyType,
1719 : : &nType, &nFormat,
1720 : : &nItems, &nBytes,
1721 : 0 : &pData );
1722 : 0 : if( nFormat == 32 && nBytes/4 )
1723 : : {
1724 : 0 : if( pData ) // ?? should not happen
1725 : : {
1726 : 0 : XFree( pData );
1727 : 0 : pData = NULL;
1728 : : }
1729 : : XGetWindowProperty( m_pDisplay,
1730 : : rRequest.requestor,
1731 : : rRequest.property,
1732 : : 0, nBytes/4,
1733 : : False,
1734 : : nType,
1735 : : &nType, &nFormat,
1736 : : &nItems, &nBytes,
1737 : 0 : &pData );
1738 : 0 : if( pData && nItems )
1739 : : {
1740 : : #if OSL_DEBUG_LEVEL > 1
1741 : : fprintf( stderr, "found %ld atoms in MULTIPLE request\n", nItems );
1742 : : #endif
1743 : 0 : bEventSuccess = true;
1744 : 0 : bool bResetAtoms = false;
1745 : 0 : Atom* pAtoms = (Atom*)pData;
1746 : 0 : aGuard.clear();
1747 : 0 : for( unsigned int i = 0; i < nItems; i += 2 )
1748 : : {
1749 : : #if OSL_DEBUG_LEVEL > 1
1750 : : fprintf( stderr, " %s => %s: ",
1751 : : OUStringToOString( getString( pAtoms[i] ), RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
1752 : : OUStringToOString( getString( pAtoms[i+1] ), RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
1753 : : #endif
1754 : 0 : bool bSuccess = sendData( pAdaptor, rRequest.requestor, pAtoms[i], pAtoms[i+1], rRequest.selection );
1755 : : #if OSL_DEBUG_LEVEL > 1
1756 : : fprintf( stderr, "%s\n", bSuccess ? "succeeded" : "failed" );
1757 : : #endif
1758 : 0 : if( ! bSuccess )
1759 : : {
1760 : 0 : pAtoms[i] = None;
1761 : 0 : bResetAtoms = true;
1762 : : }
1763 : : }
1764 : 0 : aGuard.reset();
1765 : 0 : if( bResetAtoms )
1766 : : XChangeProperty( m_pDisplay,
1767 : : rRequest.requestor,
1768 : : rRequest.property,
1769 : : XA_ATOM,
1770 : : 32,
1771 : : PropModeReplace,
1772 : : pData,
1773 : 0 : nBytes/4 );
1774 : : }
1775 : 0 : if( pData )
1776 : 0 : XFree( pData );
1777 : : }
1778 : : #if OSL_DEBUG_LEVEL > 1
1779 : : else
1780 : : {
1781 : : fprintf( stderr, "could not get type list from \"%s\" of type \"%s\" on requestor 0x%lx, requestor has properties:",
1782 : : OUStringToOString( getString( rRequest.property ), RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
1783 : : OUStringToOString( getString( nType ), RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
1784 : : rRequest.requestor );
1785 : : int nProps = 0;
1786 : : Atom* pProps = XListProperties( m_pDisplay, rRequest.requestor, &nProps );
1787 : : if( pProps )
1788 : : {
1789 : : for( int i = 0; i < nProps; i++ )
1790 : : fprintf( stderr, " \"%s\"", OUStringToOString( getString( pProps[i]), RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
1791 : : XFree( pProps );
1792 : : }
1793 : : fprintf( stderr, "\n" );
1794 : : }
1795 : : #endif
1796 : : }
1797 : : else
1798 : : {
1799 : 0 : aGuard.clear();
1800 : 0 : bEventSuccess = sendData( pAdaptor, rRequest.requestor, rRequest.target, rRequest.property, rRequest.selection );
1801 : 0 : aGuard.reset();
1802 : : }
1803 : 0 : if( bEventSuccess )
1804 : : {
1805 : 0 : aNotify.xselection.target = rRequest.target;
1806 : 0 : aNotify.xselection.property = rRequest.property;
1807 : : }
1808 : : }
1809 : 0 : aGuard.clear();
1810 : 0 : xTrans.clear();
1811 : 0 : aGuard.reset();
1812 : : }
1813 : 0 : XSendEvent( m_pDisplay, rRequest.requestor, False, 0, &aNotify );
1814 : :
1815 : 0 : if( rRequest.selection == XA_PRIMARY &&
1816 : : m_bWaitingForPrimaryConversion &&
1817 : 0 : m_xDragSourceListener.is() )
1818 : : {
1819 : 0 : DragSourceDropEvent dsde;
1820 : 0 : dsde.Source = static_cast< OWeakObject* >(this);
1821 : 0 : dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, rRequest.time, *this );
1822 : 0 : dsde.DragSource = static_cast< XDragSource* >(this);
1823 : 0 : if( aNotify.xselection.property != None )
1824 : : {
1825 : 0 : dsde.DropAction = DNDConstants::ACTION_COPY;
1826 : 0 : dsde.DropSuccess = sal_True;
1827 : : }
1828 : : else
1829 : : {
1830 : 0 : dsde.DropAction = DNDConstants::ACTION_NONE;
1831 : 0 : dsde.DropSuccess = sal_False;
1832 : : }
1833 : 0 : css::uno::Reference< XDragSourceListener > xListener( m_xDragSourceListener );
1834 : 0 : m_xDragSourceListener.clear();
1835 : 0 : aGuard.clear();
1836 : 0 : if( xListener.is() )
1837 : 0 : xListener->dragDropEnd( dsde );
1838 : : }
1839 : :
1840 : : // we handled the event in any case by answering
1841 : 0 : return true;
1842 : : }
1843 : :
1844 : : // ------------------------------------------------------------------------
1845 : :
1846 : 0 : bool SelectionManager::handleReceivePropertyNotify( XPropertyEvent& rNotify )
1847 : : {
1848 : 0 : osl::MutexGuard aGuard( m_aMutex );
1849 : : // data we requested arrived
1850 : : #if OSL_DEBUG_LEVEL > 1
1851 : : fprintf( stderr, "handleReceivePropertyNotify for property %s\n",
1852 : : OUStringToOString( getString( rNotify.atom ), RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
1853 : : #endif
1854 : 0 : bool bHandled = false;
1855 : :
1856 : : ::boost::unordered_map< Atom, Selection* >::iterator it =
1857 : 0 : m_aSelections.find( rNotify.atom );
1858 : 0 : if( it != m_aSelections.end() &&
1859 : : rNotify.state == PropertyNewValue &&
1860 : 0 : ( it->second->m_eState == Selection::WaitingForResponse ||
1861 : 0 : it->second->m_eState == Selection::WaitingForData ||
1862 : 0 : it->second->m_eState == Selection::IncrementalTransfer
1863 : : )
1864 : : )
1865 : : {
1866 : : // MULTIPLE requests are only complete after selection notify
1867 : 0 : if( it->second->m_aRequestedType == m_nMULTIPLEAtom &&
1868 : 0 : ( it->second->m_eState == Selection::WaitingForResponse ||
1869 : 0 : it->second->m_eState == Selection::WaitingForData ) )
1870 : 0 : return false;
1871 : :
1872 : 0 : bHandled = true;
1873 : :
1874 : 0 : Atom nType = None;
1875 : 0 : int nFormat = 0;
1876 : 0 : unsigned long nItems = 0, nBytes = 0;
1877 : 0 : unsigned char* pData = NULL;
1878 : :
1879 : : // get type and length
1880 : : XGetWindowProperty( m_pDisplay,
1881 : : rNotify.window,
1882 : : rNotify.atom,
1883 : : 0, 0,
1884 : : False,
1885 : : AnyPropertyType,
1886 : : &nType, &nFormat,
1887 : : &nItems, &nBytes,
1888 : 0 : &pData );
1889 : : #if OSL_DEBUG_LEVEL > 1
1890 : : fprintf( stderr, "found %ld bytes data of type %s and format %d, items = %ld\n",
1891 : : nBytes,
1892 : : OUStringToOString( getString( nType ), RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
1893 : : nFormat, nItems );
1894 : : #endif
1895 : 0 : if( pData )
1896 : : {
1897 : 0 : XFree( pData );
1898 : 0 : pData = NULL;
1899 : : }
1900 : :
1901 : 0 : if( nType == m_nINCRAtom )
1902 : : {
1903 : : // start data transfer
1904 : 0 : XDeleteProperty( m_pDisplay, rNotify.window, rNotify.atom );
1905 : 0 : it->second->m_eState = Selection::IncrementalTransfer;
1906 : : }
1907 : 0 : else if( nType != None )
1908 : : {
1909 : : XGetWindowProperty( m_pDisplay,
1910 : : rNotify.window,
1911 : : rNotify.atom,
1912 : : 0, nBytes/4 +1,
1913 : : True,
1914 : : nType,
1915 : : &nType, &nFormat,
1916 : : &nItems, &nBytes,
1917 : 0 : &pData );
1918 : : #if OSL_DEBUG_LEVEL > 1
1919 : : fprintf( stderr, "read %ld items data of type %s and format %d, %ld bytes left in property\n",
1920 : : nItems,
1921 : : OUStringToOString( getString( nType ), RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
1922 : : nFormat, nBytes );
1923 : : #endif
1924 : :
1925 : 0 : sal_Size nUnitSize = GetTrueFormatSize(nFormat);
1926 : :
1927 : 0 : if( it->second->m_eState == Selection::WaitingForData ||
1928 : 0 : it->second->m_eState == Selection::WaitingForResponse )
1929 : : {
1930 : : // copy data
1931 : 0 : it->second->m_aData = Sequence< sal_Int8 >( (sal_Int8*)pData, nItems*nUnitSize );
1932 : 0 : it->second->m_eState = Selection::Inactive;
1933 : 0 : it->second->m_aDataArrived.set();
1934 : : }
1935 : 0 : else if( it->second->m_eState == Selection::IncrementalTransfer )
1936 : : {
1937 : 0 : if( nItems )
1938 : : {
1939 : : // append data
1940 : 0 : Sequence< sal_Int8 > aData( it->second->m_aData.getLength() + nItems*nUnitSize );
1941 : 0 : memcpy( aData.getArray(), it->second->m_aData.getArray(), it->second->m_aData.getLength() );
1942 : 0 : memcpy( aData.getArray() + it->second->m_aData.getLength(), pData, nItems*nUnitSize );
1943 : 0 : it->second->m_aData = aData;
1944 : : }
1945 : : else
1946 : : {
1947 : 0 : it->second->m_eState = Selection::Inactive;
1948 : 0 : it->second->m_aDataArrived.set();
1949 : : }
1950 : : }
1951 : 0 : if( pData )
1952 : 0 : XFree( pData );
1953 : : }
1954 : 0 : else if( it->second->m_eState == Selection::IncrementalTransfer )
1955 : : {
1956 : 0 : it->second->m_eState = Selection::Inactive;
1957 : 0 : it->second->m_aDataArrived.set();
1958 : : }
1959 : : }
1960 : 0 : return bHandled;
1961 : : }
1962 : :
1963 : : // ------------------------------------------------------------------------
1964 : :
1965 : 0 : bool SelectionManager::handleSendPropertyNotify( XPropertyEvent& rNotify )
1966 : : {
1967 : 0 : osl::MutexGuard aGuard( m_aMutex );
1968 : :
1969 : : // ready for next part of a IncrementalTransfer
1970 : : #if OSL_DEBUG_LEVEL > 1
1971 : : fprintf( stderr, "handleSendPropertyNotify for property %s (%s)\n",
1972 : : OUStringToOString( getString( rNotify.atom ), RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
1973 : : rNotify.state == PropertyNewValue ? "new value" : ( rNotify.state == PropertyDelete ? "deleted" : "unknown")
1974 : : );
1975 : : #endif
1976 : :
1977 : 0 : bool bHandled = false;
1978 : : // feed incrementals
1979 : 0 : if( rNotify.state == PropertyDelete )
1980 : : {
1981 : 0 : boost::unordered_map< XLIB_Window, boost::unordered_map< Atom, IncrementalTransfer > >::iterator it;
1982 : 0 : it = m_aIncrementals.find( rNotify.window );
1983 : 0 : if( it != m_aIncrementals.end() )
1984 : : {
1985 : 0 : bHandled = true;
1986 : 0 : int nCurrentTime = time( NULL );
1987 : 0 : boost::unordered_map< Atom, IncrementalTransfer >::iterator inc_it;
1988 : : // throw out aborted transfers
1989 : 0 : std::list< Atom > aTimeouts;
1990 : 0 : for( inc_it = it->second.begin(); inc_it != it->second.end(); ++inc_it )
1991 : : {
1992 : 0 : if( (nCurrentTime - inc_it->second.m_nTransferStartTime) > (getSelectionTimeout()+2) )
1993 : : {
1994 : 0 : aTimeouts.push_back( inc_it->first );
1995 : : #if OSL_DEBUG_LEVEL > 1
1996 : : const IncrementalTransfer& rInc = inc_it->second;
1997 : : fprintf( stderr, "timeout on INCR transfer for window 0x%lx, property %s, type %s\n",
1998 : : rInc.m_aRequestor,
1999 : : OUStringToOString( getString( rInc.m_aProperty ), RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
2000 : : OUStringToOString( getString( rInc.m_aTarget ), RTL_TEXTENCODING_ISO_8859_1 ).getStr()
2001 : : );
2002 : : #endif
2003 : : }
2004 : : }
2005 : :
2006 : 0 : while( aTimeouts.begin() != aTimeouts.end() )
2007 : : {
2008 : : // transfer broken, might even be a new client with the
2009 : : // same window id
2010 : 0 : it->second.erase( aTimeouts.front() );
2011 : 0 : aTimeouts.pop_front();
2012 : : }
2013 : :
2014 : 0 : inc_it = it->second.find( rNotify.atom );
2015 : 0 : if( inc_it != it->second.end() )
2016 : : {
2017 : 0 : IncrementalTransfer& rInc = inc_it->second;
2018 : :
2019 : 0 : int nBytes = rInc.m_aData.getLength() - rInc.m_nBufferPos;
2020 : 0 : nBytes = (nBytes > m_nIncrementalThreshold) ? m_nIncrementalThreshold : nBytes;
2021 : 0 : if( nBytes < 0 ) // sanity check
2022 : 0 : nBytes = 0;
2023 : : #if OSL_DEBUG_LEVEL > 1
2024 : : fprintf( stderr, "pushing %d bytes: \"%.*s\"...\n",
2025 : : nBytes, nBytes > 32 ? 32 : nBytes,
2026 : : (const unsigned char*)rInc.m_aData.getConstArray()+rInc.m_nBufferPos );
2027 : : #endif
2028 : :
2029 : 0 : sal_Size nUnitSize = GetTrueFormatSize(rInc.m_nFormat);
2030 : :
2031 : : XChangeProperty( m_pDisplay,
2032 : : rInc.m_aRequestor,
2033 : : rInc.m_aProperty,
2034 : : rInc.m_aTarget,
2035 : : rInc.m_nFormat,
2036 : : PropModeReplace,
2037 : 0 : (const unsigned char*)rInc.m_aData.getConstArray()+rInc.m_nBufferPos,
2038 : 0 : nBytes/nUnitSize );
2039 : 0 : rInc.m_nBufferPos += nBytes;
2040 : 0 : rInc.m_nTransferStartTime = nCurrentTime;
2041 : :
2042 : 0 : if( nBytes == 0 ) // transfer finished
2043 : : {
2044 : : #if OSL_DEBUG_LEVEL > 1
2045 : : fprintf( stderr, "finished INCR transfer for window 0x%lx, property %s, type %s\n",
2046 : : rInc.m_aRequestor,
2047 : : OUStringToOString( getString( rInc.m_aProperty ), RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
2048 : : OUStringToOString( getString( rInc.m_aTarget ), RTL_TEXTENCODING_ISO_8859_1 ).getStr()
2049 : : );
2050 : : #endif
2051 : 0 : it->second.erase( inc_it );
2052 : : }
2053 : :
2054 : : }
2055 : : // eventually clean up the hash map
2056 : 0 : if( it->second.begin() == it->second.end() )
2057 : 0 : m_aIncrementals.erase( it );
2058 : : }
2059 : : }
2060 : 0 : return bHandled;
2061 : : }
2062 : :
2063 : : // ------------------------------------------------------------------------
2064 : :
2065 : 0 : bool SelectionManager::handleSelectionNotify( XSelectionEvent& rNotify )
2066 : : {
2067 : 0 : osl::MutexGuard aGuard( m_aMutex );
2068 : :
2069 : 0 : bool bHandled = false;
2070 : :
2071 : : // notification about success/failure of one of our conversion requests
2072 : : #if OSL_DEBUG_LEVEL > 1
2073 : : OUString aSelection( getString( rNotify.selection ) );
2074 : : OUString aProperty("None");
2075 : : if( rNotify.property )
2076 : : aProperty = getString( rNotify.property );
2077 : : fprintf( stderr, "handleSelectionNotify for selection %s and property %s (0x%lx)\n",
2078 : : OUStringToOString( aSelection, RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
2079 : : OUStringToOString( aProperty, RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
2080 : : rNotify.property
2081 : : );
2082 : : if( rNotify.requestor != m_aWindow && rNotify.requestor != m_aCurrentDropWindow )
2083 : : fprintf( stderr, "Warning: selection notify for unknown window 0x%lx\n", rNotify.requestor );
2084 : : #endif
2085 : : ::boost::unordered_map< Atom, Selection* >::iterator it =
2086 : 0 : m_aSelections.find( rNotify.selection );
2087 : 0 : if (
2088 : : (rNotify.requestor == m_aWindow || rNotify.requestor == m_aCurrentDropWindow) &&
2089 : 0 : it != m_aSelections.end() &&
2090 : : (
2091 : 0 : (it->second->m_eState == Selection::WaitingForResponse) ||
2092 : 0 : (it->second->m_eState == Selection::WaitingForData)
2093 : : )
2094 : : )
2095 : : {
2096 : 0 : bHandled = true;
2097 : 0 : if( it->second->m_aRequestedType == m_nMULTIPLEAtom )
2098 : : {
2099 : 0 : Atom nType = None;
2100 : 0 : int nFormat = 0;
2101 : 0 : unsigned long nItems = 0, nBytes = 0;
2102 : 0 : unsigned char* pData = NULL;
2103 : :
2104 : : // get type and length
2105 : : XGetWindowProperty( m_pDisplay,
2106 : : rNotify.requestor,
2107 : : rNotify.property,
2108 : : 0, 256,
2109 : : False,
2110 : : AnyPropertyType,
2111 : : &nType, &nFormat,
2112 : : &nItems, &nBytes,
2113 : 0 : &pData );
2114 : 0 : if( nBytes ) // HUGE request !!!
2115 : : {
2116 : 0 : if( pData )
2117 : 0 : XFree( pData );
2118 : : XGetWindowProperty( m_pDisplay,
2119 : : rNotify.requestor,
2120 : : rNotify.property,
2121 : : 0, 256+(nBytes+3)/4,
2122 : : False,
2123 : : AnyPropertyType,
2124 : : &nType, &nFormat,
2125 : : &nItems, &nBytes,
2126 : 0 : &pData );
2127 : : }
2128 : 0 : it->second->m_eState = Selection::Inactive;
2129 : 0 : sal_Size nUnitSize = GetTrueFormatSize(nFormat);
2130 : 0 : it->second->m_aData = Sequence< sal_Int8 >((sal_Int8*)pData, nItems * nUnitSize);
2131 : 0 : it->second->m_aDataArrived.set();
2132 : 0 : if( pData )
2133 : 0 : XFree( pData );
2134 : : }
2135 : : // WaitingForData can actually happen; some
2136 : : // applications (e.g. cmdtool on Solaris) first send
2137 : : // a success and then cancel it. Weird !
2138 : 0 : else if( rNotify.property == None )
2139 : : {
2140 : : // conversion failed, stop transfer
2141 : 0 : it->second->m_eState = Selection::Inactive;
2142 : 0 : it->second->m_aData = Sequence< sal_Int8 >();
2143 : 0 : it->second->m_aDataArrived.set();
2144 : : }
2145 : : // get the bytes, by INCR if necessary
2146 : : else
2147 : 0 : it->second->m_eState = Selection::WaitingForData;
2148 : : }
2149 : : #if OSL_DEBUG_LEVEL > 1
2150 : : else if( it != m_aSelections.end() )
2151 : : fprintf( stderr, "Warning: selection in state %d\n", it->second->m_eState );
2152 : : #endif
2153 : 0 : return bHandled;
2154 : : }
2155 : :
2156 : : // ------------------------------------------------------------------------
2157 : :
2158 : 0 : bool SelectionManager::handleDropEvent( XClientMessageEvent& rMessage )
2159 : : {
2160 : 0 : osl::ResettableMutexGuard aGuard(m_aMutex);
2161 : :
2162 : : // handle drop related events
2163 : 0 : XLIB_Window aSource = rMessage.data.l[0];
2164 : 0 : XLIB_Window aTarget = rMessage.window;
2165 : :
2166 : 0 : bool bHandled = false;
2167 : :
2168 : : ::boost::unordered_map< XLIB_Window, DropTargetEntry >::iterator it =
2169 : 0 : m_aDropTargets.find( aTarget );
2170 : :
2171 : : #if OSL_DEBUG_LEVEL > 1
2172 : : if( rMessage.message_type == m_nXdndEnter ||
2173 : : rMessage.message_type == m_nXdndLeave ||
2174 : : rMessage.message_type == m_nXdndDrop ||
2175 : : rMessage.message_type == m_nXdndPosition )
2176 : : {
2177 : : fprintf( stderr, "got drop event %s, ", OUStringToOString( getString( rMessage.message_type ), RTL_TEXTENCODING_ASCII_US).getStr() );
2178 : : if( it == m_aDropTargets.end() )
2179 : : fprintf( stderr, "but no target found\n" );
2180 : : else if( ! it->second.m_pTarget->m_bActive )
2181 : : fprintf( stderr, "but target is inactive\n" );
2182 : : else if( m_aDropEnterEvent.data.l[0] != None && (XLIB_Window)m_aDropEnterEvent.data.l[0] != aSource )
2183 : : fprintf( stderr, "but source 0x%lx is unknown (expected 0x%lx or 0)\n", aSource, m_aDropEnterEvent.data.l[0] );
2184 : : else
2185 : : fprintf( stderr, "processing.\n" );
2186 : : }
2187 : : #endif
2188 : :
2189 : 0 : if( it != m_aDropTargets.end() && it->second.m_pTarget->m_bActive &&
2190 : 0 : m_bDropWaitingForCompletion && m_aDropEnterEvent.data.l[0] )
2191 : : {
2192 : 0 : bHandled = true;
2193 : : OSL_FAIL( "someone forgot to call dropComplete ?" );
2194 : : // some listener forgot to call dropComplete in the last operation
2195 : : // let us end it now and accept the new enter event
2196 : 0 : aGuard.clear();
2197 : 0 : dropComplete( sal_False, m_aCurrentDropWindow, m_nDropTime );
2198 : 0 : aGuard.reset();
2199 : : }
2200 : :
2201 : 0 : if( it != m_aDropTargets.end() &&
2202 : 0 : it->second.m_pTarget->m_bActive &&
2203 : 0 : ( m_aDropEnterEvent.data.l[0] == None || XLIB_Window(m_aDropEnterEvent.data.l[0]) == aSource )
2204 : : )
2205 : : {
2206 : 0 : if( rMessage.message_type == m_nXdndEnter )
2207 : : {
2208 : 0 : bHandled = true;
2209 : 0 : m_aDropEnterEvent = rMessage;
2210 : 0 : m_bDropEnterSent = false;
2211 : 0 : m_aCurrentDropWindow = aTarget;
2212 : 0 : m_nCurrentProtocolVersion = m_aDropEnterEvent.data.l[1] >> 24;
2213 : : #if OSL_DEBUG_LEVEL > 1
2214 : : fprintf( stderr, "received XdndEnter on 0x%lx\n", aTarget );
2215 : : #endif
2216 : : }
2217 : 0 : else if(
2218 : : rMessage.message_type == m_nXdndPosition &&
2219 : 0 : aSource == XLIB_Window(m_aDropEnterEvent.data.l[0])
2220 : : )
2221 : : {
2222 : 0 : bHandled = true;
2223 : 0 : m_nDropTime = m_nCurrentProtocolVersion > 0 ? rMessage.data.l[3] : CurrentTime;
2224 : 0 : if( ! m_bDropEnterSent )
2225 : 0 : m_nDropTimestamp = m_nDropTime;
2226 : :
2227 : : XLIB_Window aChild;
2228 : : XTranslateCoordinates( m_pDisplay,
2229 : 0 : it->second.m_aRootWindow,
2230 : 0 : it->first,
2231 : 0 : rMessage.data.l[2] >> 16,
2232 : 0 : rMessage.data.l[2] & 0xffff,
2233 : : &m_nLastX, &m_nLastY,
2234 : 0 : &aChild );
2235 : :
2236 : : #if OSL_DEBUG_LEVEL > 1
2237 : : fprintf( stderr, "received XdndPosition on 0x%lx (%d, %d)\n", aTarget, m_nLastX, m_nLastY );
2238 : : #endif
2239 : 0 : DropTargetDragEnterEvent aEvent;
2240 : 0 : aEvent.Source = static_cast< XDropTarget* >(it->second.m_pTarget);
2241 : 0 : aEvent.Context = new DropTargetDragContext( m_aCurrentDropWindow, m_nDropTimestamp, *this );
2242 : 0 : aEvent.LocationX = m_nLastX;
2243 : 0 : aEvent.LocationY = m_nLastY;
2244 : 0 : aEvent.SourceActions = m_nSourceActions;
2245 : 0 : if( m_nCurrentProtocolVersion < 2 )
2246 : 0 : aEvent.DropAction = DNDConstants::ACTION_COPY;
2247 : 0 : else if( Atom(rMessage.data.l[4]) == m_nXdndActionCopy )
2248 : 0 : aEvent.DropAction = DNDConstants::ACTION_COPY;
2249 : 0 : else if( Atom(rMessage.data.l[4]) == m_nXdndActionMove )
2250 : 0 : aEvent.DropAction = DNDConstants::ACTION_MOVE;
2251 : 0 : else if( Atom(rMessage.data.l[4]) == m_nXdndActionLink )
2252 : 0 : aEvent.DropAction = DNDConstants::ACTION_LINK;
2253 : 0 : else if( Atom(rMessage.data.l[4]) == m_nXdndActionAsk )
2254 : : // currently no interface to implement ask
2255 : 0 : aEvent.DropAction = ~0;
2256 : : else
2257 : 0 : aEvent.DropAction = DNDConstants::ACTION_NONE;
2258 : :
2259 : 0 : m_nLastDropAction = aEvent.DropAction;
2260 : 0 : if( ! m_bDropEnterSent )
2261 : : {
2262 : 0 : m_bDropEnterSent = true;
2263 : 0 : aEvent.SupportedDataFlavors = m_xDropTransferable->getTransferDataFlavors();
2264 : 0 : aGuard.clear();
2265 : 0 : it->second->dragEnter( aEvent );
2266 : : }
2267 : : else
2268 : : {
2269 : 0 : aGuard.clear();
2270 : 0 : it->second->dragOver( aEvent );
2271 : 0 : }
2272 : : }
2273 : 0 : else if(
2274 : : rMessage.message_type == m_nXdndLeave &&
2275 : 0 : aSource == XLIB_Window(m_aDropEnterEvent.data.l[0])
2276 : : )
2277 : : {
2278 : 0 : bHandled = true;
2279 : : #if OSL_DEBUG_LEVEL > 1
2280 : : fprintf( stderr, "received XdndLeave on 0x%lx\n", aTarget );
2281 : : #endif
2282 : 0 : DropTargetEvent aEvent;
2283 : 0 : aEvent.Source = static_cast< XDropTarget* >(it->second.m_pTarget);
2284 : 0 : m_aDropEnterEvent.data.l[0] = None;
2285 : 0 : if( m_aCurrentDropWindow == aTarget )
2286 : 0 : m_aCurrentDropWindow = None;
2287 : 0 : m_nCurrentProtocolVersion = nXdndProtocolRevision;
2288 : 0 : aGuard.clear();
2289 : 0 : it->second->dragExit( aEvent );
2290 : : }
2291 : 0 : else if(
2292 : : rMessage.message_type == m_nXdndDrop &&
2293 : 0 : aSource == XLIB_Window(m_aDropEnterEvent.data.l[0])
2294 : : )
2295 : : {
2296 : 0 : bHandled = true;
2297 : 0 : m_nDropTime = m_nCurrentProtocolVersion > 0 ? rMessage.data.l[2] : CurrentTime;
2298 : :
2299 : : #if OSL_DEBUG_LEVEL > 1
2300 : : fprintf( stderr, "received XdndDrop on 0x%lx (%d, %d)\n", aTarget, m_nLastX, m_nLastY );
2301 : : #endif
2302 : 0 : if( m_bLastDropAccepted )
2303 : : {
2304 : 0 : DropTargetDropEvent aEvent;
2305 : 0 : aEvent.Source = static_cast< XDropTarget* >(it->second.m_pTarget);
2306 : 0 : aEvent.Context = new DropTargetDropContext( m_aCurrentDropWindow, m_nDropTimestamp, *this );
2307 : 0 : aEvent.LocationX = m_nLastX;
2308 : 0 : aEvent.LocationY = m_nLastY;
2309 : 0 : aEvent.DropAction = m_nLastDropAction;
2310 : : // there is nothing corresponding to source supported actions
2311 : : // every source can do link, copy and move
2312 : 0 : aEvent.SourceActions= m_nLastDropAction;
2313 : 0 : aEvent.Transferable = m_xDropTransferable;
2314 : :
2315 : 0 : m_bDropWaitingForCompletion = true;
2316 : 0 : aGuard.clear();
2317 : 0 : it->second->drop( aEvent );
2318 : : }
2319 : : else
2320 : : {
2321 : : #if OSL_DEBUG_LEVEL > 1
2322 : : fprintf( stderr, "XdndDrop canceled due to m_bLastDropAccepted = fale\n" );
2323 : : #endif
2324 : 0 : DropTargetEvent aEvent;
2325 : 0 : aEvent.Source = static_cast< XDropTarget* >(it->second.m_pTarget);
2326 : 0 : aGuard.clear();
2327 : 0 : it->second->dragExit( aEvent );
2328 : : // reset the drop status, notify source
2329 : 0 : dropComplete( sal_False, m_aCurrentDropWindow, m_nDropTime );
2330 : : }
2331 : : }
2332 : : }
2333 : 0 : return bHandled;
2334 : : }
2335 : :
2336 : : /*
2337 : : * methods for XDropTargetDropContext
2338 : : */
2339 : :
2340 : 0 : void SelectionManager::dropComplete( sal_Bool bSuccess, XLIB_Window aDropWindow, XLIB_Time )
2341 : : {
2342 : 0 : osl::ClearableMutexGuard aGuard(m_aMutex);
2343 : :
2344 : 0 : if( aDropWindow == m_aCurrentDropWindow )
2345 : : {
2346 : 0 : if( m_xDragSourceListener.is() )
2347 : : {
2348 : 0 : DragSourceDropEvent dsde;
2349 : 0 : dsde.Source = static_cast< OWeakObject* >(this);
2350 : 0 : dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, m_nDragTimestamp, *this );
2351 : 0 : dsde.DragSource = static_cast< XDragSource* >(this);
2352 : 0 : dsde.DropAction = getUserDragAction();
2353 : 0 : dsde.DropSuccess = bSuccess;
2354 : 0 : css::uno::Reference< XDragSourceListener > xListener = m_xDragSourceListener;
2355 : 0 : m_xDragSourceListener.clear();
2356 : :
2357 : 0 : aGuard.clear();
2358 : 0 : xListener->dragDropEnd( dsde );
2359 : : }
2360 : 0 : else if( m_aDropEnterEvent.data.l[0] && m_aCurrentDropWindow )
2361 : : {
2362 : : XEvent aEvent;
2363 : 0 : aEvent.xclient.type = ClientMessage;
2364 : 0 : aEvent.xclient.display = m_pDisplay;
2365 : 0 : aEvent.xclient.window = m_aDropEnterEvent.data.l[0];
2366 : 0 : aEvent.xclient.message_type = m_nXdndFinished;
2367 : 0 : aEvent.xclient.format = 32;
2368 : 0 : aEvent.xclient.data.l[0] = m_aCurrentDropWindow;
2369 : 0 : aEvent.xclient.data.l[1] = bSuccess ? 1 : 0;
2370 : 0 : aEvent.xclient.data.l[2] = 0;
2371 : 0 : aEvent.xclient.data.l[3] = 0;
2372 : 0 : aEvent.xclient.data.l[4] = 0;
2373 : 0 : if( bSuccess )
2374 : : {
2375 : 0 : if( m_nLastDropAction & DNDConstants::ACTION_MOVE )
2376 : 0 : aEvent.xclient.data.l[2] = m_nXdndActionMove;
2377 : 0 : else if( m_nLastDropAction & DNDConstants::ACTION_COPY )
2378 : 0 : aEvent.xclient.data.l[2] = m_nXdndActionCopy;
2379 : 0 : else if( m_nLastDropAction & DNDConstants::ACTION_LINK )
2380 : 0 : aEvent.xclient.data.l[2] = m_nXdndActionLink;
2381 : : }
2382 : :
2383 : : #if OSL_DEBUG_LEVEL > 1
2384 : : fprintf( stderr, "Sending XdndFinished to 0x%lx\n",
2385 : : m_aDropEnterEvent.data.l[0]
2386 : : );
2387 : : #endif
2388 : :
2389 : 0 : XSendEvent( m_pDisplay, m_aDropEnterEvent.data.l[0],
2390 : 0 : False, NoEventMask, & aEvent );
2391 : :
2392 : 0 : m_aDropEnterEvent.data.l[0] = None;
2393 : 0 : m_aCurrentDropWindow = None;
2394 : 0 : m_nCurrentProtocolVersion = nXdndProtocolRevision;
2395 : : }
2396 : 0 : m_bDropWaitingForCompletion = false;
2397 : : }
2398 : : else
2399 : 0 : OSL_FAIL( "dropComplete from invalid DropTargetDropContext" );
2400 : 0 : }
2401 : :
2402 : : /*
2403 : : * methods for XDropTargetDragContext
2404 : : */
2405 : :
2406 : : // ------------------------------------------------------------------------
2407 : :
2408 : 0 : void SelectionManager::sendDragStatus( Atom nDropAction )
2409 : : {
2410 : 0 : osl::ClearableMutexGuard aGuard(m_aMutex);
2411 : :
2412 : 0 : if( m_xDragSourceListener.is() )
2413 : : {
2414 : : sal_Int8 nNewDragAction;
2415 : 0 : if( nDropAction == m_nXdndActionMove )
2416 : 0 : nNewDragAction = DNDConstants::ACTION_MOVE;
2417 : 0 : else if( nDropAction == m_nXdndActionCopy )
2418 : 0 : nNewDragAction = DNDConstants::ACTION_COPY;
2419 : 0 : else if( nDropAction == m_nXdndActionLink )
2420 : 0 : nNewDragAction = DNDConstants::ACTION_LINK;
2421 : : else
2422 : 0 : nNewDragAction = DNDConstants::ACTION_NONE;
2423 : 0 : nNewDragAction &= m_nSourceActions;
2424 : :
2425 : 0 : if( nNewDragAction != m_nTargetAcceptAction )
2426 : : {
2427 : 0 : setCursor( getDefaultCursor( nNewDragAction ), m_aDropWindow, m_nDragTimestamp );
2428 : 0 : m_nTargetAcceptAction = nNewDragAction;
2429 : : }
2430 : :
2431 : 0 : DragSourceDragEvent dsde;
2432 : 0 : dsde.Source = static_cast< OWeakObject* >(this);
2433 : 0 : dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, m_nDragTimestamp, *this );
2434 : 0 : dsde.DragSource = static_cast< XDragSource* >(this);
2435 : 0 : dsde.DropAction = m_nSourceActions;
2436 : 0 : dsde.UserAction = getUserDragAction();
2437 : :
2438 : 0 : css::uno::Reference< XDragSourceListener > xListener( m_xDragSourceListener );
2439 : : // caution: do not change anything after this
2440 : 0 : aGuard.clear();
2441 : 0 : if( xListener.is() )
2442 : 0 : xListener->dragOver( dsde );
2443 : : }
2444 : 0 : else if( m_aDropEnterEvent.data.l[0] && m_aCurrentDropWindow )
2445 : : {
2446 : : XEvent aEvent;
2447 : 0 : aEvent.xclient.type = ClientMessage;
2448 : 0 : aEvent.xclient.display = m_pDisplay;
2449 : 0 : aEvent.xclient.window = m_aDropEnterEvent.data.l[0];
2450 : 0 : aEvent.xclient.message_type = m_nXdndStatus;
2451 : 0 : aEvent.xclient.format = 32;
2452 : 0 : aEvent.xclient.data.l[0] = m_aCurrentDropWindow;
2453 : 0 : aEvent.xclient.data.l[1] = 2;
2454 : 0 : if( nDropAction == m_nXdndActionMove ||
2455 : : nDropAction == m_nXdndActionLink ||
2456 : : nDropAction == m_nXdndActionCopy )
2457 : 0 : aEvent.xclient.data.l[1] |= 1;
2458 : 0 : aEvent.xclient.data.l[2] = 0;
2459 : 0 : aEvent.xclient.data.l[3] = 0;
2460 : 0 : aEvent.xclient.data.l[4] = m_nCurrentProtocolVersion > 1 ? nDropAction : 0;
2461 : :
2462 : : #if OSL_DEBUG_LEVEL > 1
2463 : : fprintf( stderr, "Sending XdndStatus to 0x%lx with action %s\n",
2464 : : m_aDropEnterEvent.data.l[0],
2465 : : OUStringToOString( getString( nDropAction ), RTL_TEXTENCODING_ISO_8859_1 ).getStr()
2466 : : );
2467 : : #endif
2468 : :
2469 : 0 : XSendEvent( m_pDisplay, m_aDropEnterEvent.data.l[0],
2470 : 0 : False, NoEventMask, & aEvent );
2471 : 0 : XFlush( m_pDisplay );
2472 : 0 : }
2473 : 0 : }
2474 : :
2475 : : // ------------------------------------------------------------------------
2476 : :
2477 : 0 : sal_Int8 SelectionManager::getUserDragAction() const
2478 : : {
2479 : 0 : return (m_nTargetAcceptAction != DNDConstants::ACTION_DEFAULT) ? m_nTargetAcceptAction : m_nUserDragAction;
2480 : : }
2481 : :
2482 : : // ------------------------------------------------------------------------
2483 : :
2484 : 0 : bool SelectionManager::updateDragAction( int modifierState )
2485 : : {
2486 : 0 : bool bRet = false;
2487 : :
2488 : 0 : sal_Int8 nNewDropAction = DNDConstants::ACTION_MOVE;
2489 : 0 : if( ( modifierState & ShiftMask ) && ! ( modifierState & ControlMask ) )
2490 : 0 : nNewDropAction = DNDConstants::ACTION_MOVE;
2491 : 0 : else if( ( modifierState & ControlMask ) && ! ( modifierState & ShiftMask ) )
2492 : 0 : nNewDropAction = DNDConstants::ACTION_COPY;
2493 : 0 : else if( ( modifierState & ShiftMask ) && ( modifierState & ControlMask ) )
2494 : 0 : nNewDropAction = DNDConstants::ACTION_LINK;
2495 : 0 : if( m_nCurrentProtocolVersion < 0 && m_aDropWindow != None )
2496 : 0 : nNewDropAction = DNDConstants::ACTION_COPY;
2497 : 0 : nNewDropAction &= m_nSourceActions;
2498 : :
2499 : 0 : if( ! ( modifierState & ( ControlMask | ShiftMask ) ) )
2500 : : {
2501 : 0 : if( ! nNewDropAction )
2502 : : {
2503 : : // default to an action so the user does not have to press
2504 : : // keys explicitly
2505 : 0 : if( m_nSourceActions & DNDConstants::ACTION_MOVE )
2506 : 0 : nNewDropAction = DNDConstants::ACTION_MOVE;
2507 : 0 : else if( m_nSourceActions & DNDConstants::ACTION_COPY )
2508 : 0 : nNewDropAction = DNDConstants::ACTION_COPY;
2509 : 0 : else if( m_nSourceActions & DNDConstants::ACTION_LINK )
2510 : 0 : nNewDropAction = DNDConstants::ACTION_LINK;
2511 : : }
2512 : 0 : nNewDropAction |= DNDConstants::ACTION_DEFAULT;
2513 : : }
2514 : :
2515 : 0 : if( nNewDropAction != m_nUserDragAction || m_nTargetAcceptAction != DNDConstants::ACTION_DEFAULT )
2516 : : {
2517 : : #if OSL_DEBUG_LEVEL > 1
2518 : : fprintf( stderr, "updateDragAction: %x -> %x\n", (int)m_nUserDragAction, (int)nNewDropAction );
2519 : : #endif
2520 : 0 : bRet = true;
2521 : 0 : m_nUserDragAction = nNewDropAction;
2522 : :
2523 : 0 : DragSourceDragEvent dsde;
2524 : 0 : dsde.Source = static_cast< OWeakObject* >(this);
2525 : 0 : dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, m_nDragTimestamp, *this );
2526 : 0 : dsde.DragSource = static_cast< XDragSource* >(this);
2527 : 0 : dsde.DropAction = m_nUserDragAction;
2528 : 0 : dsde.UserAction = m_nUserDragAction;
2529 : 0 : m_nTargetAcceptAction = DNDConstants::ACTION_DEFAULT; // invalidate last accept
2530 : 0 : m_xDragSourceListener->dropActionChanged( dsde );
2531 : : }
2532 : 0 : return bRet;
2533 : : }
2534 : :
2535 : : // ------------------------------------------------------------------------
2536 : :
2537 : 0 : void SelectionManager::sendDropPosition( bool bForce, XLIB_Time eventTime )
2538 : : {
2539 : 0 : osl::ClearableMutexGuard aGuard(m_aMutex);
2540 : :
2541 : 0 : if( m_bDropSent )
2542 : 0 : return;
2543 : :
2544 : : ::boost::unordered_map< XLIB_Window, DropTargetEntry >::const_iterator it =
2545 : 0 : m_aDropTargets.find( m_aDropWindow );
2546 : 0 : if( it != m_aDropTargets.end() )
2547 : : {
2548 : 0 : if( it->second.m_pTarget->m_bActive )
2549 : : {
2550 : : int x, y;
2551 : : XLIB_Window aChild;
2552 : 0 : XTranslateCoordinates( m_pDisplay, it->second.m_aRootWindow, m_aDropWindow, m_nLastDragX, m_nLastDragY, &x, &y, &aChild );
2553 : 0 : DropTargetDragEvent dtde;
2554 : 0 : dtde.Source = static_cast< OWeakObject* >(it->second.m_pTarget );
2555 : 0 : dtde.Context = new DropTargetDragContext( m_aCurrentDropWindow, m_nDropTimestamp, *this );
2556 : 0 : dtde.LocationX = x;
2557 : 0 : dtde.LocationY = y;
2558 : 0 : dtde.DropAction = getUserDragAction();
2559 : 0 : dtde.SourceActions = m_nSourceActions;
2560 : 0 : aGuard.clear();
2561 : 0 : it->second->dragOver( dtde );
2562 : : }
2563 : : }
2564 : 0 : else if( bForce ||
2565 : :
2566 : : m_nLastDragX < m_nNoPosX || m_nLastDragX >= m_nNoPosX+m_nNoPosWidth ||
2567 : : m_nLastDragY < m_nNoPosY || m_nLastDragY >= m_nNoPosY+m_nNoPosHeight
2568 : : )
2569 : : {
2570 : : // send XdndPosition
2571 : : XEvent aEvent;
2572 : 0 : aEvent.type = ClientMessage;
2573 : 0 : aEvent.xclient.display = m_pDisplay;
2574 : 0 : aEvent.xclient.format = 32;
2575 : 0 : aEvent.xclient.message_type = m_nXdndPosition;
2576 : 0 : aEvent.xclient.window = m_aDropWindow;
2577 : 0 : aEvent.xclient.data.l[0] = m_aWindow;
2578 : 0 : aEvent.xclient.data.l[1] = 0;
2579 : 0 : aEvent.xclient.data.l[2] = m_nLastDragX << 16 | (m_nLastDragY&0xffff);
2580 : 0 : aEvent.xclient.data.l[3] = eventTime;
2581 : :
2582 : 0 : if( m_nUserDragAction & DNDConstants::ACTION_COPY )
2583 : 0 : aEvent.xclient.data.l[4]=m_nXdndActionCopy;
2584 : 0 : else if( m_nUserDragAction & DNDConstants::ACTION_MOVE )
2585 : 0 : aEvent.xclient.data.l[4]=m_nXdndActionMove;
2586 : 0 : else if( m_nUserDragAction & DNDConstants::ACTION_LINK )
2587 : 0 : aEvent.xclient.data.l[4]=m_nXdndActionLink;
2588 : : else
2589 : 0 : aEvent.xclient.data.l[4]=m_nXdndActionCopy;
2590 : 0 : XSendEvent( m_pDisplay, m_aDropProxy, False, NoEventMask, &aEvent );
2591 : 0 : m_nNoPosX = m_nNoPosY = m_nNoPosWidth = m_nNoPosHeight = 0;
2592 : 0 : }
2593 : : }
2594 : :
2595 : : // ------------------------------------------------------------------------
2596 : :
2597 : 0 : bool SelectionManager::handleDragEvent( XEvent& rMessage )
2598 : : {
2599 : 0 : if( ! m_xDragSourceListener.is() )
2600 : 0 : return false;
2601 : :
2602 : 0 : osl::ResettableMutexGuard aGuard(m_aMutex);
2603 : :
2604 : 0 : bool bHandled = false;
2605 : :
2606 : : // for shortcut
2607 : : ::boost::unordered_map< XLIB_Window, DropTargetEntry >::const_iterator it =
2608 : 0 : m_aDropTargets.find( m_aDropWindow );
2609 : : #if OSL_DEBUG_LEVEL > 1
2610 : : switch( rMessage.type )
2611 : : {
2612 : : case ClientMessage:
2613 : : fprintf( stderr, "handleDragEvent: %s\n", OUStringToOString( getString( rMessage.xclient.message_type ), RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
2614 : : break;
2615 : : case MotionNotify:
2616 : : break;
2617 : : case EnterNotify:
2618 : : fprintf( stderr, "handleDragEvent: EnterNotify\n" );
2619 : : break;
2620 : : case LeaveNotify:
2621 : : fprintf( stderr, "handleDragEvent: LeaveNotify\n" );
2622 : : break;
2623 : : case ButtonPress:
2624 : : fprintf( stderr, "handleDragEvent: ButtonPress %d (m_nDragButton = %d)\n", rMessage.xbutton.button, m_nDragButton );
2625 : : break;
2626 : : case ButtonRelease:
2627 : : fprintf( stderr, "handleDragEvent: ButtonRelease %d (m_nDragButton = %d)\n", rMessage.xbutton.button, m_nDragButton );
2628 : : break;
2629 : : case XLIB_KeyPress:
2630 : : fprintf( stderr, "handleDragEvent: KeyPress\n" );
2631 : : break;
2632 : : case KeyRelease:
2633 : : fprintf( stderr, "handleDragEvent: KeyRelease\n" );
2634 : : break;
2635 : : default:
2636 : : fprintf( stderr, "handleDragEvent: <unknown type %d>\n", rMessage.type );
2637 : : break;
2638 : : }
2639 : : #endif
2640 : :
2641 : : // handle drag related events
2642 : 0 : if( rMessage.type == ClientMessage )
2643 : : {
2644 : 0 : if( Atom(rMessage.xclient.message_type) == m_nXdndStatus && Atom(rMessage.xclient.data.l[0]) == m_aDropWindow )
2645 : : {
2646 : 0 : bHandled = true;
2647 : 0 : DragSourceDragEvent dsde;
2648 : 0 : dsde.Source = static_cast< OWeakObject* >(this);
2649 : 0 : dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, m_nDragTimestamp, *this );
2650 : 0 : dsde.DragSource = static_cast< XDragSource* >( this );
2651 : 0 : dsde.UserAction = getUserDragAction();
2652 : 0 : dsde.DropAction = DNDConstants::ACTION_NONE;
2653 : 0 : m_bDropSuccess = rMessage.xclient.data.l[1] & 1 ? true : false;
2654 : : #if OSL_DEBUG_LEVEL > 1
2655 : : fprintf( stderr, "status drop action: accept = %s, %s\n",
2656 : : m_bDropSuccess ? "true" : "false",
2657 : : OUStringToOString( getString( rMessage.xclient.data.l[4] ), RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
2658 : : #endif
2659 : 0 : if( rMessage.xclient.data.l[1] & 1 )
2660 : : {
2661 : 0 : if( m_nCurrentProtocolVersion > 1 )
2662 : : {
2663 : 0 : if( Atom(rMessage.xclient.data.l[4]) == m_nXdndActionCopy )
2664 : 0 : dsde.DropAction = DNDConstants::ACTION_COPY;
2665 : 0 : else if( Atom(rMessage.xclient.data.l[4]) == m_nXdndActionMove )
2666 : 0 : dsde.DropAction = DNDConstants::ACTION_MOVE;
2667 : 0 : else if( Atom(rMessage.xclient.data.l[4]) == m_nXdndActionLink )
2668 : 0 : dsde.DropAction = DNDConstants::ACTION_LINK;
2669 : : }
2670 : : else
2671 : 0 : dsde.DropAction = DNDConstants::ACTION_COPY;
2672 : : }
2673 : 0 : m_nTargetAcceptAction = dsde.DropAction;
2674 : :
2675 : 0 : if( ! ( rMessage.xclient.data.l[1] & 2 ) )
2676 : : {
2677 : 0 : m_nNoPosX = rMessage.xclient.data.l[2] >> 16;
2678 : 0 : m_nNoPosY = rMessage.xclient.data.l[2] & 0xffff;
2679 : 0 : m_nNoPosWidth = rMessage.xclient.data.l[3] >> 16;
2680 : 0 : m_nNoPosHeight = rMessage.xclient.data.l[3] & 0xffff;
2681 : : }
2682 : : else
2683 : 0 : m_nNoPosX = m_nNoPosY = m_nNoPosWidth = m_nNoPosHeight = 0;
2684 : :
2685 : 0 : setCursor( getDefaultCursor( dsde.DropAction ), m_aDropWindow, m_nDragTimestamp );
2686 : 0 : aGuard.clear();
2687 : 0 : m_xDragSourceListener->dragOver( dsde );
2688 : : }
2689 : 0 : else if( Atom(rMessage.xclient.message_type) == m_nXdndFinished && m_aDropWindow == Atom(rMessage.xclient.data.l[0]) )
2690 : : {
2691 : 0 : bHandled = true;
2692 : : // notify the listener
2693 : 0 : DragSourceDropEvent dsde;
2694 : 0 : dsde.Source = static_cast< OWeakObject* >(this);
2695 : 0 : dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, m_nDragTimestamp, *this );
2696 : 0 : dsde.DragSource = static_cast< XDragSource* >(this);
2697 : 0 : dsde.DropAction = m_nTargetAcceptAction;
2698 : 0 : dsde.DropSuccess = m_bDropSuccess;
2699 : 0 : css::uno::Reference< XDragSourceListener > xListener( m_xDragSourceListener );
2700 : 0 : m_xDragSourceListener.clear();
2701 : 0 : aGuard.clear();
2702 : 0 : xListener->dragDropEnd( dsde );
2703 : : }
2704 : : }
2705 : 0 : else if( rMessage.type == MotionNotify ||
2706 : : rMessage.type == EnterNotify || rMessage.type == LeaveNotify
2707 : : )
2708 : : {
2709 : 0 : bHandled = true;
2710 : 0 : bool bForce = false;
2711 : 0 : int root_x = rMessage.type == MotionNotify ? rMessage.xmotion.x_root : rMessage.xcrossing.x_root;
2712 : 0 : int root_y = rMessage.type == MotionNotify ? rMessage.xmotion.y_root : rMessage.xcrossing.y_root;
2713 : 0 : XLIB_Window root = rMessage.type == MotionNotify ? rMessage.xmotion.root : rMessage.xcrossing.root;
2714 : 0 : m_nDragTimestamp = rMessage.type == MotionNotify ? rMessage.xmotion.time : rMessage.xcrossing.time;
2715 : :
2716 : 0 : aGuard.clear();
2717 : 0 : if( rMessage.type == MotionNotify )
2718 : : {
2719 : 0 : bForce = updateDragAction( rMessage.xmotion.state );
2720 : : }
2721 : 0 : updateDragWindow( root_x, root_y, root );
2722 : 0 : aGuard.reset();
2723 : :
2724 : 0 : if( m_nCurrentProtocolVersion >= 0 && m_aDropProxy != None )
2725 : : {
2726 : 0 : aGuard.clear();
2727 : 0 : sendDropPosition( bForce, rMessage.type == MotionNotify ? rMessage.xmotion.time : rMessage.xcrossing.time );
2728 : 0 : }
2729 : : }
2730 : 0 : else if( rMessage.type == XLIB_KeyPress || rMessage.type == KeyRelease )
2731 : : {
2732 : 0 : bHandled = true;
2733 : 0 : KeySym aKey = XkbKeycodeToKeysym( m_pDisplay, rMessage.xkey.keycode, 0, 0 );
2734 : 0 : if( aKey == XK_Escape )
2735 : : {
2736 : : // abort drag
2737 : 0 : if( it != m_aDropTargets.end() )
2738 : : {
2739 : 0 : DropTargetEvent dte;
2740 : 0 : dte.Source = static_cast< OWeakObject* >( it->second.m_pTarget );
2741 : 0 : aGuard.clear();
2742 : 0 : it->second.m_pTarget->dragExit( dte );
2743 : : }
2744 : 0 : else if( m_aDropProxy != None && m_nCurrentProtocolVersion >= 0 )
2745 : : {
2746 : : // send XdndLeave
2747 : : XEvent aEvent;
2748 : 0 : aEvent.type = ClientMessage;
2749 : 0 : aEvent.xclient.display = m_pDisplay;
2750 : 0 : aEvent.xclient.format = 32;
2751 : 0 : aEvent.xclient.message_type = m_nXdndLeave;
2752 : 0 : aEvent.xclient.window = m_aDropWindow;
2753 : 0 : aEvent.xclient.data.l[0] = m_aWindow;
2754 : 0 : memset( aEvent.xclient.data.l+1, 0, sizeof(long)*4);
2755 : 0 : m_aDropWindow = m_aDropProxy = None;
2756 : 0 : XSendEvent( m_pDisplay, m_aDropProxy, False, NoEventMask, &aEvent );
2757 : : }
2758 : : // notify the listener
2759 : 0 : DragSourceDropEvent dsde;
2760 : 0 : dsde.Source = static_cast< OWeakObject* >(this);
2761 : 0 : dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, m_nDragTimestamp, *this );
2762 : 0 : dsde.DragSource = static_cast< XDragSource* >(this);
2763 : 0 : dsde.DropAction = DNDConstants::ACTION_NONE;
2764 : 0 : dsde.DropSuccess = sal_False;
2765 : 0 : css::uno::Reference< XDragSourceListener > xListener( m_xDragSourceListener );
2766 : 0 : m_xDragSourceListener.clear();
2767 : 0 : aGuard.clear();
2768 : 0 : xListener->dragDropEnd( dsde );
2769 : : }
2770 : : else
2771 : : {
2772 : : /*
2773 : : * man page says: state is state immediate PRIOR to the
2774 : : * event. It would seem that this is a somewhat arguable
2775 : : * design decision.
2776 : : */
2777 : 0 : int nState = rMessage.xkey.state;
2778 : 0 : int nNewState = 0;
2779 : 0 : switch( aKey )
2780 : : {
2781 : : case XK_Shift_R:
2782 : 0 : case XK_Shift_L: nNewState = ShiftMask;break;
2783 : : case XK_Control_R:
2784 : 0 : case XK_Control_L: nNewState = ControlMask;break;
2785 : : // just interested in shift and ctrl for dnd
2786 : : }
2787 : 0 : if( rMessage.type == XLIB_KeyPress )
2788 : 0 : nState |= nNewState;
2789 : : else
2790 : 0 : nState &= ~nNewState;
2791 : 0 : aGuard.clear();
2792 : 0 : if( updateDragAction( nState ) )
2793 : 0 : sendDropPosition( true, rMessage.xkey.time );
2794 : 0 : }
2795 : : }
2796 : 0 : else if(
2797 : : ( rMessage.type == ButtonPress || rMessage.type == ButtonRelease ) &&
2798 : : rMessage.xbutton.button == m_nDragButton )
2799 : : {
2800 : 0 : bool bCancel = true;
2801 : 0 : if( m_aDropWindow != None )
2802 : : {
2803 : 0 : if( it != m_aDropTargets.end() )
2804 : : {
2805 : 0 : if( it->second.m_pTarget->m_bActive && m_nUserDragAction != DNDConstants::ACTION_NONE && m_bLastDropAccepted )
2806 : : {
2807 : 0 : bHandled = true;
2808 : : int x, y;
2809 : : XLIB_Window aChild;
2810 : 0 : XTranslateCoordinates( m_pDisplay, rMessage.xbutton.root, m_aDropWindow, rMessage.xbutton.x_root, rMessage.xbutton.y_root, &x, &y, &aChild );
2811 : 0 : DropTargetDropEvent dtde;
2812 : 0 : dtde.Source = static_cast< OWeakObject* >(it->second.m_pTarget );
2813 : 0 : dtde.Context = new DropTargetDropContext( m_aCurrentDropWindow, m_nDropTimestamp, *this );
2814 : 0 : dtde.LocationX = x;
2815 : 0 : dtde.LocationY = y;
2816 : 0 : dtde.DropAction = m_nUserDragAction;
2817 : 0 : dtde.SourceActions = m_nSourceActions;
2818 : 0 : dtde.Transferable = m_xDragSourceTransferable;
2819 : 0 : m_bDropSent = true;
2820 : 0 : m_nDropTimeout = time( NULL );
2821 : 0 : m_bDropWaitingForCompletion = true;
2822 : 0 : aGuard.clear();
2823 : 0 : it->second->drop( dtde );
2824 : 0 : bCancel = false;
2825 : : }
2826 : 0 : else bCancel = true;
2827 : : }
2828 : 0 : else if( m_nCurrentProtocolVersion >= 0 )
2829 : : {
2830 : 0 : bHandled = true;
2831 : :
2832 : : XEvent aEvent;
2833 : 0 : aEvent.type = ClientMessage;
2834 : 0 : aEvent.xclient.display = m_pDisplay;
2835 : 0 : aEvent.xclient.format = 32;
2836 : 0 : aEvent.xclient.message_type = m_nXdndDrop;
2837 : 0 : aEvent.xclient.window = m_aDropWindow;
2838 : 0 : aEvent.xclient.data.l[0] = m_aWindow;
2839 : 0 : aEvent.xclient.data.l[1] = 0;
2840 : 0 : aEvent.xclient.data.l[2] = rMessage.xbutton.time;
2841 : 0 : aEvent.xclient.data.l[3] = 0;
2842 : 0 : aEvent.xclient.data.l[4] = 0;
2843 : :
2844 : 0 : m_bDropSent = true;
2845 : 0 : m_nDropTimeout = time( NULL );
2846 : 0 : XSendEvent( m_pDisplay, m_aDropProxy, False, NoEventMask, &aEvent );
2847 : 0 : bCancel = false;
2848 : : }
2849 : : else
2850 : : {
2851 : : // dropping on non XdndWindows: acquire ownership of
2852 : : // PRIMARY and send a middle mouse button click down/up to
2853 : : // target window
2854 : 0 : SelectionAdaptor* pAdaptor = getAdaptor( XA_PRIMARY );
2855 : 0 : if( pAdaptor )
2856 : : {
2857 : 0 : bHandled = true;
2858 : :
2859 : : XLIB_Window aDummy;
2860 : : XEvent aEvent;
2861 : 0 : aEvent.type = ButtonPress;
2862 : 0 : aEvent.xbutton.display = m_pDisplay;
2863 : 0 : aEvent.xbutton.window = m_aDropWindow;
2864 : 0 : aEvent.xbutton.root = rMessage.xbutton.root;
2865 : 0 : aEvent.xbutton.subwindow = m_aDropWindow;
2866 : 0 : aEvent.xbutton.time = rMessage.xbutton.time+1;
2867 : 0 : aEvent.xbutton.x_root = rMessage.xbutton.x_root;
2868 : 0 : aEvent.xbutton.y_root = rMessage.xbutton.y_root;
2869 : 0 : aEvent.xbutton.state = rMessage.xbutton.state;
2870 : 0 : aEvent.xbutton.button = Button2;
2871 : 0 : aEvent.xbutton.same_screen = True;
2872 : : XTranslateCoordinates( m_pDisplay,
2873 : : rMessage.xbutton.root, m_aDropWindow,
2874 : : rMessage.xbutton.x_root, rMessage.xbutton.y_root,
2875 : : &aEvent.xbutton.x, &aEvent.xbutton.y,
2876 : 0 : &aDummy );
2877 : 0 : XSendEvent( m_pDisplay, m_aDropWindow, False, ButtonPressMask, &aEvent );
2878 : 0 : aEvent.xbutton.type = ButtonRelease;
2879 : 0 : aEvent.xbutton.time++;
2880 : 0 : aEvent.xbutton.state |= Button2Mask;
2881 : 0 : XSendEvent( m_pDisplay, m_aDropWindow, False, ButtonReleaseMask, &aEvent );
2882 : :
2883 : 0 : m_bDropSent = true;
2884 : 0 : m_nDropTimeout = time( NULL );
2885 : 0 : XSendEvent( m_pDisplay, m_aDropProxy, False, NoEventMask, &aEvent );
2886 : 0 : m_bWaitingForPrimaryConversion = true;
2887 : 0 : m_bDropSent = true;
2888 : 0 : m_nDropTimeout = time( NULL );
2889 : : // HACK :-)
2890 : 0 : aGuard.clear();
2891 : 0 : static_cast< X11Clipboard* >( pAdaptor )->setContents( m_xDragSourceTransferable, css::uno::Reference< ::com::sun::star::datatransfer::clipboard::XClipboardOwner >() );
2892 : 0 : aGuard.reset();
2893 : 0 : bCancel = false;
2894 : : }
2895 : : }
2896 : : }
2897 : 0 : if( bCancel )
2898 : : {
2899 : : // cancel drag
2900 : 0 : DragSourceDropEvent dsde;
2901 : 0 : dsde.Source = static_cast< OWeakObject* >(this);
2902 : 0 : dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, m_nDragTimestamp, *this );
2903 : 0 : dsde.DragSource = static_cast< XDragSource* >(this);
2904 : 0 : dsde.DropAction = DNDConstants::ACTION_NONE;
2905 : 0 : dsde.DropSuccess = sal_False;
2906 : 0 : css::uno::Reference< XDragSourceListener > xListener( m_xDragSourceListener );
2907 : 0 : m_xDragSourceListener.clear();
2908 : 0 : aGuard.clear();
2909 : 0 : xListener->dragDropEnd( dsde );
2910 : 0 : bHandled = true;
2911 : : }
2912 : : }
2913 : 0 : return bHandled;
2914 : : }
2915 : :
2916 : : // ------------------------------------------------------------------------
2917 : :
2918 : 0 : void SelectionManager::accept( sal_Int8 dragOperation, XLIB_Window aDropWindow, XLIB_Time )
2919 : : {
2920 : 0 : if( aDropWindow == m_aCurrentDropWindow )
2921 : : {
2922 : : #if OSL_DEBUG_LEVEL > 1
2923 : : fprintf( stderr, "accept: %x\n", dragOperation );
2924 : : #endif
2925 : 0 : Atom nAction = None;
2926 : 0 : dragOperation &= (DNDConstants::ACTION_MOVE | DNDConstants::ACTION_COPY | DNDConstants::ACTION_LINK);
2927 : 0 : if( dragOperation & DNDConstants::ACTION_MOVE )
2928 : 0 : nAction = m_nXdndActionMove;
2929 : 0 : else if( dragOperation & DNDConstants::ACTION_COPY )
2930 : 0 : nAction = m_nXdndActionCopy;
2931 : 0 : else if( dragOperation & DNDConstants::ACTION_LINK )
2932 : 0 : nAction = m_nXdndActionLink;
2933 : 0 : m_bLastDropAccepted = true;
2934 : 0 : sendDragStatus( nAction );
2935 : : }
2936 : 0 : }
2937 : :
2938 : : // ------------------------------------------------------------------------
2939 : :
2940 : 0 : void SelectionManager::reject( XLIB_Window aDropWindow, XLIB_Time )
2941 : : {
2942 : 0 : if( aDropWindow == m_aCurrentDropWindow )
2943 : : {
2944 : : #if OSL_DEBUG_LEVEL > 1
2945 : : fprintf( stderr, "reject\n" );
2946 : : #endif
2947 : 0 : m_bLastDropAccepted = false;
2948 : 0 : sendDragStatus( None );
2949 : 0 : if( m_bDropSent && m_xDragSourceListener.is() )
2950 : : {
2951 : 0 : DragSourceDropEvent dsde;
2952 : 0 : dsde.Source = static_cast< OWeakObject* >(this);
2953 : 0 : dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, m_nDragTimestamp, *this );
2954 : 0 : dsde.DragSource = static_cast< XDragSource* >(this);
2955 : 0 : dsde.DropAction = DNDConstants::ACTION_NONE;
2956 : 0 : dsde.DropSuccess = sal_False;
2957 : 0 : m_xDragSourceListener->dragDropEnd( dsde );
2958 : 0 : m_xDragSourceListener.clear();
2959 : : }
2960 : : }
2961 : 0 : }
2962 : :
2963 : : /*
2964 : : * XDragSource
2965 : : */
2966 : :
2967 : 0 : sal_Bool SelectionManager::isDragImageSupported() throw()
2968 : : {
2969 : 0 : return sal_False;
2970 : : }
2971 : :
2972 : : // ------------------------------------------------------------------------
2973 : :
2974 : 0 : sal_Int32 SelectionManager::getDefaultCursor( sal_Int8 dragAction ) throw()
2975 : : {
2976 : 0 : XLIB_Cursor aCursor = m_aNoneCursor;
2977 : 0 : if( dragAction & DNDConstants::ACTION_MOVE )
2978 : 0 : aCursor = m_aMoveCursor;
2979 : 0 : else if( dragAction & DNDConstants::ACTION_COPY )
2980 : 0 : aCursor = m_aCopyCursor;
2981 : 0 : else if( dragAction & DNDConstants::ACTION_LINK )
2982 : 0 : aCursor = m_aLinkCursor;
2983 : 0 : return aCursor;
2984 : : }
2985 : :
2986 : : // ------------------------------------------------------------------------
2987 : :
2988 : 0 : int SelectionManager::getXdndVersion( XLIB_Window aWindow, XLIB_Window& rProxy )
2989 : : {
2990 : 0 : Atom* pProperties = NULL;
2991 : 0 : int nProperties = 0;
2992 : : Atom nType;
2993 : : int nFormat;
2994 : : unsigned long nItems, nBytes;
2995 : 0 : unsigned char* pBytes = NULL;
2996 : :
2997 : 0 : int nVersion = -1;
2998 : 0 : rProxy = None;
2999 : :
3000 : : /*
3001 : : * XListProperties is used here to avoid unnecessary XGetWindowProperty calls
3002 : : * and therefore reducing latency penalty
3003 : : */
3004 : 0 : pProperties = XListProperties( m_pDisplay, aWindow, &nProperties );
3005 : : // first look for proxy
3006 : : int i;
3007 : 0 : for( i = 0; i < nProperties; i++ )
3008 : : {
3009 : 0 : if( pProperties[i] == m_nXdndProxy )
3010 : : {
3011 : : XGetWindowProperty( m_pDisplay, aWindow, m_nXdndProxy, 0, 1, False, XA_WINDOW,
3012 : 0 : &nType, &nFormat, &nItems, &nBytes, &pBytes );
3013 : 0 : if( pBytes )
3014 : : {
3015 : 0 : if( nType == XA_WINDOW )
3016 : 0 : rProxy = *(XLIB_Window*)pBytes;
3017 : 0 : XFree( pBytes );
3018 : 0 : pBytes = NULL;
3019 : 0 : if( rProxy != None )
3020 : : {
3021 : : // now check proxy whether it points to itself
3022 : : XGetWindowProperty( m_pDisplay, rProxy, m_nXdndProxy, 0, 1, False, XA_WINDOW,
3023 : 0 : &nType, &nFormat, &nItems, &nBytes, &pBytes );
3024 : 0 : if( pBytes )
3025 : : {
3026 : 0 : if( nType == XA_WINDOW && *(XLIB_Window*)pBytes != rProxy )
3027 : 0 : rProxy = None;
3028 : 0 : XFree( pBytes );
3029 : 0 : pBytes = NULL;
3030 : : }
3031 : : else
3032 : 0 : rProxy = None;
3033 : : }
3034 : : }
3035 : 0 : break;
3036 : : }
3037 : : }
3038 : 0 : XLIB_Window aAwareWindow = rProxy != None ? rProxy : aWindow;
3039 : :
3040 : : XGetWindowProperty( m_pDisplay, aAwareWindow, m_nXdndAware, 0, 1, False, XA_ATOM,
3041 : 0 : &nType, &nFormat, &nItems, &nBytes, &pBytes );
3042 : 0 : if( pBytes )
3043 : : {
3044 : 0 : if( nType == XA_ATOM )
3045 : 0 : nVersion = *(Atom*)pBytes;
3046 : 0 : XFree( pBytes );
3047 : : }
3048 : :
3049 : 0 : nVersion = nVersion > nXdndProtocolRevision ? nXdndProtocolRevision : nVersion;
3050 : :
3051 : 0 : return nVersion;
3052 : : }
3053 : :
3054 : : // ------------------------------------------------------------------------
3055 : :
3056 : 0 : void SelectionManager::updateDragWindow( int nX, int nY, XLIB_Window aRoot )
3057 : : {
3058 : 0 : osl::ResettableMutexGuard aGuard( m_aMutex );
3059 : :
3060 : 0 : css::uno::Reference< XDragSourceListener > xListener( m_xDragSourceListener );
3061 : :
3062 : 0 : m_nLastDragX = nX;
3063 : 0 : m_nLastDragY = nY;
3064 : :
3065 : 0 : XLIB_Window aParent = aRoot;
3066 : : XLIB_Window aChild;
3067 : 0 : XLIB_Window aNewProxy = None, aNewCurrentWindow = None;
3068 : 0 : int nNewProtocolVersion = -1;
3069 : : int nWinX, nWinY;
3070 : :
3071 : : // find the first XdndAware window or check if root window is
3072 : : // XdndAware or has XdndProxy
3073 : 0 : do
3074 : : {
3075 : 0 : XTranslateCoordinates( m_pDisplay, aRoot, aParent, nX, nY, &nWinX, &nWinY, &aChild );
3076 : 0 : if( aChild != None )
3077 : : {
3078 : 0 : if( aChild == m_aCurrentDropWindow && aChild != aRoot && m_nCurrentProtocolVersion >= 0 )
3079 : : {
3080 : 0 : aParent = aChild;
3081 : 0 : break;
3082 : : }
3083 : 0 : nNewProtocolVersion = getXdndVersion( aChild, aNewProxy );
3084 : 0 : aParent = aChild;
3085 : : }
3086 : : } while( aChild != None && nNewProtocolVersion < 0 );
3087 : :
3088 : 0 : aNewCurrentWindow = aParent;
3089 : 0 : if( aNewCurrentWindow == aRoot )
3090 : : {
3091 : : // no children, try root drop
3092 : 0 : nNewProtocolVersion = getXdndVersion( aNewCurrentWindow, aNewProxy );
3093 : 0 : if( nNewProtocolVersion < 3 )
3094 : : {
3095 : 0 : aNewCurrentWindow = aNewProxy = None;
3096 : 0 : nNewProtocolVersion = nXdndProtocolRevision;
3097 : : }
3098 : : }
3099 : :
3100 : :
3101 : 0 : DragSourceDragEvent dsde;
3102 : 0 : dsde.Source = static_cast< OWeakObject* >(this);
3103 : 0 : dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, m_nDragTimestamp, *this );
3104 : 0 : dsde.DragSource = static_cast< XDragSource* >(this);
3105 : 0 : dsde.DropAction = nNewProtocolVersion >= 0 ? m_nUserDragAction : DNDConstants::ACTION_COPY;
3106 : 0 : dsde.UserAction = nNewProtocolVersion >= 0 ? m_nUserDragAction : DNDConstants::ACTION_COPY;
3107 : :
3108 : 0 : ::boost::unordered_map< XLIB_Window, DropTargetEntry >::const_iterator it;
3109 : 0 : if( aNewCurrentWindow != m_aDropWindow )
3110 : : {
3111 : : #if OSL_DEBUG_LEVEL > 1
3112 : : fprintf( stderr, "drag left window 0x%lx (rev. %d), entered window 0x%lx (rev %d)\n", m_aDropWindow, m_nCurrentProtocolVersion, aNewCurrentWindow, nNewProtocolVersion );
3113 : : #endif
3114 : :
3115 : 0 : if( m_aDropWindow != None )
3116 : : {
3117 : 0 : it = m_aDropTargets.find( m_aDropWindow );
3118 : 0 : if( it != m_aDropTargets.end() )
3119 : : // shortcut for own drop targets
3120 : : {
3121 : 0 : DropTargetEvent dte;
3122 : 0 : dte.Source = static_cast< OWeakObject* >( it->second.m_pTarget );
3123 : 0 : aGuard.clear();
3124 : 0 : it->second.m_pTarget->dragExit( dte );
3125 : 0 : aGuard.reset();
3126 : : }
3127 : : else
3128 : : {
3129 : : // send old drop target a XdndLeave
3130 : : XEvent aEvent;
3131 : 0 : aEvent.type = ClientMessage;
3132 : 0 : aEvent.xclient.display = m_pDisplay;
3133 : 0 : aEvent.xclient.format = 32;
3134 : 0 : aEvent.xclient.message_type = m_nXdndLeave;
3135 : 0 : aEvent.xclient.window = m_aDropWindow;
3136 : 0 : aEvent.xclient.data.l[0] = m_aWindow;
3137 : 0 : aEvent.xclient.data.l[1] = 0;
3138 : 0 : XSendEvent( m_pDisplay, m_aDropProxy, False, NoEventMask, &aEvent );
3139 : : }
3140 : 0 : if( xListener.is() )
3141 : : {
3142 : 0 : aGuard.clear();
3143 : 0 : xListener->dragExit( dsde );
3144 : 0 : aGuard.reset();
3145 : : }
3146 : : }
3147 : :
3148 : 0 : m_nCurrentProtocolVersion = nNewProtocolVersion;
3149 : 0 : m_aDropWindow = aNewCurrentWindow;
3150 : 0 : m_aDropProxy = aNewProxy != None ? aNewProxy : m_aDropWindow;
3151 : :
3152 : 0 : it = m_aDropTargets.find( m_aDropWindow );
3153 : 0 : if( it != m_aDropTargets.end() && ! it->second.m_pTarget->m_bActive )
3154 : 0 : m_aDropProxy = None;
3155 : :
3156 : 0 : if( m_aDropProxy != None && xListener.is() )
3157 : : {
3158 : 0 : aGuard.clear();
3159 : 0 : xListener->dragEnter( dsde );
3160 : 0 : aGuard.reset();
3161 : : }
3162 : : // send XdndEnter
3163 : 0 : if( m_aDropProxy != None && m_nCurrentProtocolVersion >= 0 )
3164 : : {
3165 : 0 : it = m_aDropTargets.find( m_aDropWindow );
3166 : 0 : if( it != m_aDropTargets.end() )
3167 : : {
3168 : 0 : XTranslateCoordinates( m_pDisplay, aRoot, m_aDropWindow, nX, nY, &nWinX, &nWinY, &aChild );
3169 : 0 : DropTargetDragEnterEvent dtde;
3170 : 0 : dtde.Source = static_cast< OWeakObject* >( it->second.m_pTarget );
3171 : 0 : dtde.Context = new DropTargetDragContext( m_aCurrentDropWindow, m_nDropTimestamp, *this );
3172 : 0 : dtde.LocationX = nWinX;
3173 : 0 : dtde.LocationY = nWinY;
3174 : 0 : dtde.DropAction = m_nUserDragAction;
3175 : 0 : dtde.SourceActions = m_nSourceActions;
3176 : 0 : dtde.SupportedDataFlavors = m_xDragSourceTransferable->getTransferDataFlavors();
3177 : 0 : aGuard.clear();
3178 : 0 : it->second.m_pTarget->dragEnter( dtde );
3179 : 0 : aGuard.reset();
3180 : : }
3181 : : else
3182 : : {
3183 : : XEvent aEvent;
3184 : 0 : aEvent.type = ClientMessage;
3185 : 0 : aEvent.xclient.display = m_pDisplay;
3186 : 0 : aEvent.xclient.format = 32;
3187 : 0 : aEvent.xclient.message_type = m_nXdndEnter;
3188 : 0 : aEvent.xclient.window = m_aDropWindow;
3189 : 0 : aEvent.xclient.data.l[0] = m_aWindow;
3190 : 0 : aEvent.xclient.data.l[1] = m_nCurrentProtocolVersion << 24;
3191 : 0 : memset( aEvent.xclient.data.l + 2, 0, sizeof( long )*3 );
3192 : : // fill in data types
3193 : 0 : ::std::list< Atom > aConversions;
3194 : 0 : getNativeTypeList( m_aDragFlavors, aConversions, m_nXdndSelection );
3195 : 0 : if( aConversions.size() > 3 )
3196 : 0 : aEvent.xclient.data.l[1] |= 1;
3197 : 0 : ::std::list< Atom >::const_iterator type_it = aConversions.begin();
3198 : 0 : for( int i = 0; type_it != aConversions.end() && i < 3; i++, ++type_it )
3199 : 0 : aEvent.xclient.data.l[i+2] = *type_it;
3200 : 0 : XSendEvent( m_pDisplay, m_aDropProxy, False, NoEventMask, &aEvent );
3201 : : }
3202 : : }
3203 : 0 : m_nNoPosX = m_nNoPosY = m_nNoPosWidth = m_nNoPosHeight = 0;
3204 : : }
3205 : 0 : else if( m_aDropProxy != None && xListener.is() )
3206 : : {
3207 : 0 : aGuard.clear();
3208 : : // drag over for XdndAware windows comes when receiving XdndStatus
3209 : 0 : xListener->dragOver( dsde );
3210 : 0 : }
3211 : 0 : }
3212 : :
3213 : : // ------------------------------------------------------------------------
3214 : :
3215 : 0 : void SelectionManager::startDrag(
3216 : : const DragGestureEvent& trigger,
3217 : : sal_Int8 sourceActions,
3218 : : sal_Int32,
3219 : : sal_Int32,
3220 : : const css::uno::Reference< XTransferable >& transferable,
3221 : : const css::uno::Reference< XDragSourceListener >& listener
3222 : : ) throw()
3223 : : {
3224 : : #if OSL_DEBUG_LEVEL > 1
3225 : : fprintf( stderr, "startDrag( sourceActions = %x )\n", (int)sourceActions );
3226 : : #endif
3227 : :
3228 : 0 : DragSourceDropEvent aDragFailedEvent;
3229 : 0 : aDragFailedEvent.Source = static_cast< OWeakObject* >(this);
3230 : 0 : aDragFailedEvent.DragSource = static_cast< XDragSource* >(this);
3231 : 0 : aDragFailedEvent.DragSourceContext = new DragSourceContext( None, CurrentTime, *this );
3232 : 0 : aDragFailedEvent.DropAction = DNDConstants::ACTION_NONE;
3233 : 0 : aDragFailedEvent.DropSuccess = sal_False;
3234 : :
3235 : 0 : if( m_aDragRunning.check() )
3236 : : {
3237 : 0 : if( listener.is() )
3238 : 0 : listener->dragDropEnd( aDragFailedEvent );
3239 : :
3240 : : #if OSL_DEBUG_LEVEL > 1
3241 : : fprintf( stderr, "*** ERROR *** second drag and drop started.\n" );
3242 : : if( m_xDragSourceListener.is() )
3243 : : fprintf( stderr, "*** ERROR *** drag source listener already set.\n" );
3244 : : else
3245 : : fprintf( stderr, "*** ERROR *** drag thread already running.\n" );
3246 : : #endif
3247 : : return;
3248 : : }
3249 : :
3250 : 0 : SalFrame* pCaptureFrame = NULL;
3251 : :
3252 : : {
3253 : 0 : osl::ClearableMutexGuard aGuard(m_aMutex);
3254 : :
3255 : : // first get the current pointer position and the window that
3256 : : // the pointer is located in. since said window should be one
3257 : : // of our DropTargets at the time of executeDrag we can use
3258 : : // them for a start
3259 : : XLIB_Window aRoot, aParent, aChild;
3260 : : int root_x, root_y, win_x, win_y;
3261 : : unsigned int mask;
3262 : :
3263 : 0 : ::boost::unordered_map< XLIB_Window, DropTargetEntry >::const_iterator it;
3264 : 0 : it = m_aDropTargets.begin();
3265 : 0 : while( it != m_aDropTargets.end() )
3266 : : {
3267 : 0 : if( XQueryPointer( m_pDisplay, it->second.m_aRootWindow,
3268 : : &aRoot, &aParent,
3269 : : &root_x, &root_y,
3270 : : &win_x, &win_y,
3271 : 0 : &mask ) )
3272 : : {
3273 : 0 : aParent = it->second.m_aRootWindow;
3274 : 0 : break;
3275 : : }
3276 : 0 : ++it;
3277 : : }
3278 : :
3279 : : // don't start DnD if there is none of our windows on the same screen as
3280 : : // the pointer or if no mouse button is pressed
3281 : 0 : if( it == m_aDropTargets.end() || (mask & (Button1Mask|Button2Mask|Button3Mask)) == 0 )
3282 : : {
3283 : 0 : aGuard.clear();
3284 : 0 : if( listener.is() )
3285 : 0 : listener->dragDropEnd( aDragFailedEvent );
3286 : : return;
3287 : : }
3288 : :
3289 : : // try to find which of our drop targets is the drag source
3290 : : // if that drop target is deregistered we should stop executing
3291 : : // the drag (actually this is a poor substitute for an "endDrag"
3292 : : // method ).
3293 : 0 : m_aDragSourceWindow = None;
3294 : 0 : aParent = aRoot = it->second.m_aRootWindow;
3295 : 0 : do
3296 : : {
3297 : 0 : XTranslateCoordinates( m_pDisplay, aRoot, aParent, root_x, root_y, &win_x, &win_y, &aChild );
3298 : 0 : if( aChild != None && m_aDropTargets.find( aChild ) != m_aDropTargets.end() )
3299 : : {
3300 : 0 : m_aDragSourceWindow = aChild;
3301 : : #if OSL_DEBUG_LEVEL > 1
3302 : : fprintf( stderr, "found drag source window 0x%lx\n", m_aDragSourceWindow );
3303 : : #endif
3304 : 0 : break;
3305 : : }
3306 : 0 : aParent = aChild;
3307 : : } while( aChild != None );
3308 : :
3309 : :
3310 : : #if OSL_DEBUG_LEVEL > 1
3311 : : fprintf( stderr, "try to grab pointer ... " );
3312 : : #endif
3313 : : int nPointerGrabSuccess =
3314 : 0 : XGrabPointer( m_pDisplay, it->second.m_aRootWindow, True,
3315 : : DRAG_EVENT_MASK,
3316 : : GrabModeAsync, GrabModeAsync,
3317 : : None,
3318 : : None,
3319 : 0 : CurrentTime );
3320 : : /* if we could not grab the pointer here, there is a chance
3321 : : that the pointer is grabbed by the other vcl display (the main loop)
3322 : : so let's break that grab an reset it later
3323 : :
3324 : : remark: this whole code should really be molten into normal vcl so only
3325 : : one display is used ....
3326 : : */
3327 : 0 : if( nPointerGrabSuccess != GrabSuccess )
3328 : : {
3329 : 0 : osl::SolarMutex& rSolarMutex( Application::GetSolarMutex() );
3330 : 0 : if( rSolarMutex.tryToAcquire() )
3331 : : {
3332 : 0 : pCaptureFrame = GetGenericData()->GetSalDisplay()->GetCaptureFrame();
3333 : 0 : if( pCaptureFrame )
3334 : : {
3335 : 0 : GetGenericData()->GetSalDisplay()->CaptureMouse( NULL );
3336 : : nPointerGrabSuccess =
3337 : 0 : XGrabPointer( m_pDisplay, it->second.m_aRootWindow, True,
3338 : : DRAG_EVENT_MASK,
3339 : : GrabModeAsync, GrabModeAsync,
3340 : : None,
3341 : : None,
3342 : 0 : CurrentTime );
3343 : : }
3344 : : }
3345 : : }
3346 : : #if OSL_DEBUG_LEVEL > 1
3347 : : fprintf( stderr, "%d\n", nPointerGrabSuccess );
3348 : : #endif
3349 : : #if OSL_DEBUG_LEVEL > 1
3350 : : fprintf( stderr, "try to grab keyboard ... " );
3351 : : #endif
3352 : : int nKeyboardGrabSuccess =
3353 : 0 : XGrabKeyboard( m_pDisplay, it->second.m_aRootWindow, True,
3354 : 0 : GrabModeAsync, GrabModeAsync, CurrentTime );
3355 : : #if OSL_DEBUG_LEVEL > 1
3356 : : fprintf( stderr, "%d\n", nKeyboardGrabSuccess );
3357 : : #endif
3358 : 0 : if( nPointerGrabSuccess != GrabSuccess || nKeyboardGrabSuccess != GrabSuccess )
3359 : : {
3360 : 0 : if( nPointerGrabSuccess == GrabSuccess )
3361 : 0 : XUngrabPointer( m_pDisplay, CurrentTime );
3362 : 0 : if( nKeyboardGrabSuccess == GrabSuccess )
3363 : 0 : XUngrabKeyboard( m_pDisplay, CurrentTime );
3364 : 0 : XFlush( m_pDisplay );
3365 : 0 : aGuard.clear();
3366 : 0 : if( listener.is() )
3367 : 0 : listener->dragDropEnd( aDragFailedEvent );
3368 : 0 : if( pCaptureFrame )
3369 : : {
3370 : 0 : osl::SolarMutex& rSolarMutex( Application::GetSolarMutex() );
3371 : 0 : if( rSolarMutex.tryToAcquire() )
3372 : 0 : GetGenericData()->GetSalDisplay()->CaptureMouse( pCaptureFrame );
3373 : : #if OSL_DEBUG_LEVEL > 0
3374 : : else
3375 : : OSL_FAIL( "failed to acquire SolarMutex to reset capture frame" );
3376 : : #endif
3377 : : }
3378 : : return;
3379 : : }
3380 : :
3381 : 0 : m_xDragSourceTransferable = transferable;
3382 : 0 : m_xDragSourceListener = listener;
3383 : 0 : m_aDragFlavors = transferable->getTransferDataFlavors();
3384 : 0 : m_aCurrentCursor = None;
3385 : :
3386 : 0 : requestOwnership( m_nXdndSelection );
3387 : :
3388 : 0 : ::std::list< Atom > aConversions;
3389 : 0 : ::std::list< Atom >::const_iterator type_it;
3390 : 0 : getNativeTypeList( m_aDragFlavors, aConversions, m_nXdndSelection );
3391 : :
3392 : 0 : int nTypes = aConversions.size();
3393 : 0 : Atom* pTypes = (Atom*)alloca( sizeof(Atom)*nTypes );
3394 : 0 : type_it = aConversions.begin();
3395 : 0 : for( int n = 0; n < nTypes; n++, ++type_it )
3396 : 0 : pTypes[n] = *type_it;
3397 : :
3398 : 0 : XChangeProperty( m_pDisplay, m_aWindow, m_nXdndTypeList, XA_ATOM, 32, PropModeReplace, (unsigned char*)pTypes, nTypes );
3399 : :
3400 : 0 : m_nSourceActions = sourceActions | DNDConstants::ACTION_DEFAULT;
3401 : 0 : m_nUserDragAction = DNDConstants::ACTION_MOVE & m_nSourceActions;
3402 : 0 : if( ! m_nUserDragAction )
3403 : 0 : m_nUserDragAction = DNDConstants::ACTION_COPY & m_nSourceActions;
3404 : 0 : if( ! m_nUserDragAction )
3405 : 0 : m_nUserDragAction = DNDConstants::ACTION_LINK & m_nSourceActions;
3406 : 0 : m_nTargetAcceptAction = DNDConstants::ACTION_DEFAULT;
3407 : 0 : m_bDropSent = false;
3408 : 0 : m_bDropSuccess = false;
3409 : 0 : m_bWaitingForPrimaryConversion = false;
3410 : 0 : m_nDragButton = Button1; // default to left button
3411 : 0 : com::sun::star::awt::MouseEvent aEvent;
3412 : 0 : if( trigger.Event >>= aEvent )
3413 : : {
3414 : 0 : if( aEvent.Buttons & MouseButton::LEFT )
3415 : 0 : m_nDragButton = Button1;
3416 : 0 : else if( aEvent.Buttons & MouseButton::RIGHT )
3417 : 0 : m_nDragButton = Button3;
3418 : 0 : else if( aEvent.Buttons & MouseButton::MIDDLE )
3419 : 0 : m_nDragButton = Button2;
3420 : : }
3421 : : #if OSL_DEBUG_LEVEL > 1
3422 : : fprintf( stderr, "m_nUserDragAction = %x\n", (int)m_nUserDragAction );
3423 : : #endif
3424 : 0 : updateDragWindow( root_x, root_y, aRoot );
3425 : 0 : m_nUserDragAction = ~0;
3426 : 0 : updateDragAction( mask );
3427 : : }
3428 : :
3429 : 0 : m_aDragRunning.set();
3430 : 0 : m_aDragExecuteThread = osl_createSuspendedThread( call_SelectionManager_runDragExecute, this );
3431 : 0 : if( m_aDragExecuteThread )
3432 : 0 : osl_resumeThread( m_aDragExecuteThread );
3433 : : else
3434 : : {
3435 : : #if OSL_DEBUG_LEVEL > 1
3436 : : fprintf( stderr, "osl_createSuspendedThread failed for drag execute\n" );
3437 : : #endif
3438 : 0 : m_xDragSourceListener.clear();
3439 : 0 : m_xDragSourceTransferable.clear();
3440 : :
3441 : 0 : m_bDropSent = false;
3442 : 0 : m_bDropSuccess = false;
3443 : 0 : m_bWaitingForPrimaryConversion = false;
3444 : 0 : m_aDropWindow = None;
3445 : 0 : m_aDropProxy = None;
3446 : 0 : m_nCurrentProtocolVersion = nXdndProtocolRevision;
3447 : 0 : m_nNoPosX = 0;
3448 : 0 : m_nNoPosY = 0;
3449 : 0 : m_nNoPosWidth = 0;
3450 : 0 : m_nNoPosHeight = 0;
3451 : 0 : m_aCurrentCursor = None;
3452 : :
3453 : 0 : XUngrabPointer( m_pDisplay, CurrentTime );
3454 : 0 : XUngrabKeyboard( m_pDisplay, CurrentTime );
3455 : 0 : XFlush( m_pDisplay );
3456 : :
3457 : 0 : if( pCaptureFrame )
3458 : : {
3459 : 0 : osl::SolarMutex& rSolarMutex( Application::GetSolarMutex() );
3460 : 0 : if( rSolarMutex.tryToAcquire() )
3461 : 0 : GetGenericData()->GetSalDisplay()->CaptureMouse( pCaptureFrame );
3462 : : #if OSL_DEBUG_LEVEL > 0
3463 : : else
3464 : : OSL_FAIL( "failed to acquire SolarMutex to reset capture frame" );
3465 : : #endif
3466 : : }
3467 : :
3468 : 0 : m_aDragRunning.reset();
3469 : :
3470 : 0 : if( listener.is() )
3471 : 0 : listener->dragDropEnd( aDragFailedEvent );
3472 : 0 : }
3473 : : }
3474 : :
3475 : 0 : void SelectionManager::runDragExecute( void* pThis )
3476 : : {
3477 : 0 : SelectionManager* This = (SelectionManager*)pThis;
3478 : 0 : This->dragDoDispatch();
3479 : 0 : }
3480 : :
3481 : 0 : void SelectionManager::dragDoDispatch()
3482 : : {
3483 : :
3484 : : // do drag
3485 : : // m_xDragSourceListener will be cleared on finished drop
3486 : : #if OSL_DEBUG_LEVEL > 1
3487 : : fprintf( stderr, "begin executeDrag dispatching\n" );
3488 : : #endif
3489 : : TimeValue aTVal;
3490 : 0 : aTVal.Seconds = 0;
3491 : 0 : aTVal.Nanosec = 200000000;
3492 : 0 : oslThread aThread = m_aDragExecuteThread;
3493 : 0 : while( m_xDragSourceListener.is() && ( ! m_bDropSent || time(NULL)-m_nDropTimeout < 5 ) && osl_scheduleThread( aThread ) )
3494 : : {
3495 : : // let the thread in the run method do the dispatching
3496 : : // just look occasionally here whether drop timed out or is completed
3497 : 0 : osl_waitThread( &aTVal );
3498 : : }
3499 : : #if OSL_DEBUG_LEVEL > 1
3500 : : fprintf( stderr, "end executeDrag dispatching\n" );
3501 : : #endif
3502 : : {
3503 : 0 : osl::ClearableMutexGuard aGuard(m_aMutex);
3504 : :
3505 : 0 : css::uno::Reference< XDragSourceListener > xListener( m_xDragSourceListener );
3506 : 0 : css::uno::Reference< XTransferable > xTransferable( m_xDragSourceTransferable );
3507 : 0 : m_xDragSourceListener.clear();
3508 : 0 : m_xDragSourceTransferable.clear();
3509 : :
3510 : 0 : DragSourceDropEvent dsde;
3511 : 0 : dsde.Source = static_cast< OWeakObject* >(this);
3512 : 0 : dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, m_nDragTimestamp, *this );
3513 : 0 : dsde.DragSource = static_cast< XDragSource* >(this);
3514 : 0 : dsde.DropAction = DNDConstants::ACTION_NONE;
3515 : 0 : dsde.DropSuccess = sal_False;
3516 : :
3517 : : // cleanup after drag
3518 : 0 : if( m_bWaitingForPrimaryConversion )
3519 : 0 : getAdaptor( XA_PRIMARY )->clearTransferable();
3520 : :
3521 : 0 : m_bDropSent = false;
3522 : 0 : m_bDropSuccess = false;
3523 : 0 : m_bWaitingForPrimaryConversion = false;
3524 : 0 : m_aDropWindow = None;
3525 : 0 : m_aDropProxy = None;
3526 : 0 : m_nCurrentProtocolVersion = nXdndProtocolRevision;
3527 : 0 : m_nNoPosX = 0;
3528 : 0 : m_nNoPosY = 0;
3529 : 0 : m_nNoPosWidth = 0;
3530 : 0 : m_nNoPosHeight = 0;
3531 : 0 : m_aCurrentCursor = None;
3532 : :
3533 : 0 : XUngrabPointer( m_pDisplay, CurrentTime );
3534 : 0 : XUngrabKeyboard( m_pDisplay, CurrentTime );
3535 : 0 : XFlush( m_pDisplay );
3536 : :
3537 : 0 : m_aDragExecuteThread = NULL;
3538 : 0 : m_aDragRunning.reset();
3539 : :
3540 : 0 : aGuard.clear();
3541 : 0 : if( xListener.is() )
3542 : : {
3543 : 0 : xTransferable.clear();
3544 : 0 : xListener->dragDropEnd( dsde );
3545 : 0 : }
3546 : : }
3547 : 0 : osl_destroyThread( aThread );
3548 : 0 : }
3549 : :
3550 : : /*
3551 : : * XDragSourceContext
3552 : : */
3553 : :
3554 : 0 : sal_Int32 SelectionManager::getCurrentCursor()
3555 : : {
3556 : 0 : return m_aCurrentCursor;
3557 : : }
3558 : :
3559 : : // ------------------------------------------------------------------------
3560 : :
3561 : 0 : void SelectionManager::setCursor( sal_Int32 cursor, XLIB_Window aDropWindow, XLIB_Time )
3562 : : {
3563 : 0 : osl::MutexGuard aGuard( m_aMutex );
3564 : 0 : if( aDropWindow == m_aDropWindow && XLIB_Cursor(cursor) != m_aCurrentCursor )
3565 : : {
3566 : 0 : if( m_xDragSourceListener.is() && ! m_bDropSent )
3567 : : {
3568 : 0 : m_aCurrentCursor = cursor;
3569 : 0 : XChangeActivePointerGrab( m_pDisplay, DRAG_EVENT_MASK, cursor, CurrentTime );
3570 : 0 : XFlush( m_pDisplay );
3571 : : }
3572 : 0 : }
3573 : 0 : }
3574 : :
3575 : : // ------------------------------------------------------------------------
3576 : :
3577 : 0 : void SelectionManager::setImage( sal_Int32, XLIB_Window, XLIB_Time )
3578 : : {
3579 : 0 : }
3580 : :
3581 : : // ------------------------------------------------------------------------
3582 : :
3583 : 0 : void SelectionManager::transferablesFlavorsChanged()
3584 : : {
3585 : 0 : osl::MutexGuard aGuard(m_aMutex);
3586 : :
3587 : 0 : m_aDragFlavors = m_xDragSourceTransferable->getTransferDataFlavors();
3588 : : int i;
3589 : :
3590 : 0 : std::list< Atom > aConversions;
3591 : 0 : std::list< Atom >::const_iterator type_it;
3592 : :
3593 : 0 : getNativeTypeList( m_aDragFlavors, aConversions, m_nXdndSelection );
3594 : :
3595 : 0 : int nTypes = aConversions.size();
3596 : 0 : Atom* pTypes = (Atom*)alloca( sizeof(Atom)*aConversions.size() );
3597 : 0 : for( i = 0, type_it = aConversions.begin(); type_it != aConversions.end(); ++type_it, i++ )
3598 : 0 : pTypes[i] = *type_it;
3599 : 0 : XChangeProperty( m_pDisplay, m_aWindow, m_nXdndTypeList, XA_ATOM, 32, PropModeReplace, (unsigned char*)pTypes, nTypes );
3600 : :
3601 : 0 : if( m_aCurrentDropWindow != None && m_nCurrentProtocolVersion >= 0 )
3602 : : {
3603 : : // send synthetic leave and enter events
3604 : :
3605 : : XEvent aEvent;
3606 : :
3607 : 0 : aEvent.type = ClientMessage;
3608 : 0 : aEvent.xclient.display = m_pDisplay;
3609 : 0 : aEvent.xclient.format = 32;
3610 : 0 : aEvent.xclient.window = m_aDropWindow;
3611 : 0 : aEvent.xclient.data.l[0] = m_aWindow;
3612 : :
3613 : 0 : aEvent.xclient.message_type = m_nXdndLeave;
3614 : 0 : aEvent.xclient.data.l[1] = 0;
3615 : 0 : XSendEvent( m_pDisplay, m_aDropProxy, False, NoEventMask, &aEvent );
3616 : :
3617 : 0 : aEvent.xclient.message_type = m_nXdndEnter;
3618 : 0 : aEvent.xclient.data.l[1] = m_nCurrentProtocolVersion << 24;
3619 : 0 : memset( aEvent.xclient.data.l + 2, 0, sizeof( long )*3 );
3620 : : // fill in data types
3621 : 0 : if( nTypes > 3 )
3622 : 0 : aEvent.xclient.data.l[1] |= 1;
3623 : 0 : for( int j = 0; j < nTypes && j < 3; j++ )
3624 : 0 : aEvent.xclient.data.l[j+2] = pTypes[j];
3625 : :
3626 : 0 : XSendEvent( m_pDisplay, m_aDropProxy, False, NoEventMask, &aEvent );
3627 : 0 : }
3628 : 0 : }
3629 : :
3630 : : /*
3631 : : * dispatch loop
3632 : : */
3633 : :
3634 : : // ------------------------------------------------------------------------
3635 : :
3636 : 0 : bool SelectionManager::handleXEvent( XEvent& rEvent )
3637 : : {
3638 : : /*
3639 : : * since we are XConnectionListener to a second X display
3640 : : * to get client messages it is essential not to dispatch
3641 : : * events twice that we get on both connections
3642 : : *
3643 : : * between dispatching ButtonPress and startDrag
3644 : : * the user can already have released the mouse. The ButtonRelease
3645 : : * will then be dispatched in VCLs queue and never turn up here.
3646 : : * Which is not so good, since startDrag will XGrabPointer and
3647 : : * XGrabKeyboard -> solid lock.
3648 : : */
3649 : 0 : if( rEvent.xany.display != m_pDisplay
3650 : : && rEvent.type != ClientMessage
3651 : : && rEvent.type != ButtonPress
3652 : : && rEvent.type != ButtonRelease
3653 : : )
3654 : 0 : return false;
3655 : :
3656 : 0 : bool bHandled = false;
3657 : 0 : switch (rEvent.type)
3658 : : {
3659 : : case SelectionClear:
3660 : : {
3661 : 0 : osl::ClearableMutexGuard aGuard(m_aMutex);
3662 : : #if OSL_DEBUG_LEVEL > 1
3663 : : fprintf( stderr, "SelectionClear for selection %s\n",
3664 : : OUStringToOString( getString( rEvent.xselectionclear.selection ), RTL_TEXTENCODING_ISO_8859_1 ).getStr()
3665 : : );
3666 : : #endif
3667 : 0 : SelectionAdaptor* pAdaptor = getAdaptor( rEvent.xselectionclear.selection );
3668 : 0 : boost::unordered_map< Atom, Selection* >::iterator it( m_aSelections.find( rEvent.xselectionclear.selection ) );
3669 : 0 : if( it != m_aSelections.end() )
3670 : 0 : it->second->m_bOwner = false;
3671 : 0 : aGuard.clear();
3672 : 0 : if ( pAdaptor )
3673 : 0 : pAdaptor->clearTransferable();
3674 : : }
3675 : 0 : break;
3676 : :
3677 : : case SelectionRequest:
3678 : 0 : bHandled = handleSelectionRequest( rEvent.xselectionrequest );
3679 : 0 : break;
3680 : : case PropertyNotify:
3681 : 0 : if( rEvent.xproperty.window == m_aWindow ||
3682 : : rEvent.xproperty.window == m_aCurrentDropWindow
3683 : : )
3684 : 0 : bHandled = handleReceivePropertyNotify( rEvent.xproperty );
3685 : : else
3686 : 0 : bHandled = handleSendPropertyNotify( rEvent.xproperty );
3687 : 0 : break;
3688 : : case SelectionNotify:
3689 : 0 : bHandled = handleSelectionNotify( rEvent.xselection );
3690 : 0 : break;
3691 : : case ClientMessage:
3692 : : // messages from drag target
3693 : 0 : if( rEvent.xclient.message_type == m_nXdndStatus ||
3694 : : rEvent.xclient.message_type == m_nXdndFinished )
3695 : 0 : bHandled = handleDragEvent( rEvent );
3696 : : // messages from drag source
3697 : 0 : else if(
3698 : : rEvent.xclient.message_type == m_nXdndEnter ||
3699 : : rEvent.xclient.message_type == m_nXdndLeave ||
3700 : : rEvent.xclient.message_type == m_nXdndPosition ||
3701 : : rEvent.xclient.message_type == m_nXdndDrop
3702 : : )
3703 : 0 : bHandled = handleDropEvent( rEvent.xclient );
3704 : 0 : break;
3705 : : case EnterNotify:
3706 : : case LeaveNotify:
3707 : : case MotionNotify:
3708 : : case ButtonPress:
3709 : : case ButtonRelease:
3710 : : case XLIB_KeyPress:
3711 : : case KeyRelease:
3712 : 0 : bHandled = handleDragEvent( rEvent );
3713 : 0 : break;
3714 : : default:
3715 : : ;
3716 : : }
3717 : 0 : return bHandled;
3718 : : }
3719 : :
3720 : : // ------------------------------------------------------------------------
3721 : :
3722 : 0 : void SelectionManager::dispatchEvent( int millisec )
3723 : : {
3724 : : // acquire the mutex to prevent other threads
3725 : : // from using the same X connection
3726 : 0 : osl::ResettableMutexGuard aGuard(m_aMutex);
3727 : :
3728 : 0 : if( !XPending( m_pDisplay ))
3729 : : { // wait for any events if none are already queued
3730 : : pollfd aPollFD;
3731 : 0 : aPollFD.fd = XConnectionNumber( m_pDisplay );
3732 : 0 : aPollFD.events = POLLIN;
3733 : 0 : aPollFD.revents = 0;
3734 : : // release mutex for the time of waiting for possible data
3735 : 0 : aGuard.clear();
3736 : 0 : if( poll( &aPollFD, 1, millisec ) <= 0 )
3737 : 0 : return;
3738 : 0 : aGuard.reset();
3739 : : }
3740 : 0 : while( XPending( m_pDisplay ))
3741 : : {
3742 : : XEvent event;
3743 : 0 : XNextEvent( m_pDisplay, &event );
3744 : 0 : aGuard.clear();
3745 : 0 : handleXEvent( event );
3746 : 0 : aGuard.reset();
3747 : 0 : }
3748 : : }
3749 : :
3750 : : // ------------------------------------------------------------------------
3751 : :
3752 : 0 : void SelectionManager::run( void* pThis )
3753 : : {
3754 : : #if OSL_DEBUG_LEVEL > 1
3755 : : fprintf(stderr, "SelectionManager::run\n" );
3756 : : #endif
3757 : : // dispatch until the cows come home
3758 : :
3759 : 0 : SelectionManager* This = (SelectionManager*)pThis;
3760 : :
3761 : : timeval aLast;
3762 : 0 : gettimeofday( &aLast, 0 );
3763 : :
3764 : 0 : css::uno::Reference< XMultiServiceFactory > xFact( ::comphelper::getProcessServiceFactory() );
3765 : 0 : if( xFact.is() )
3766 : : {
3767 : 0 : This->m_xDesktop.set(xFact->createInstance( ::rtl::OUString("com.sun.star.frame.Desktop") ), UNO_QUERY);
3768 : 0 : if (This->m_xDesktop.is())
3769 : 0 : This->m_xDesktop->addTerminateListener(This);
3770 : : }
3771 : :
3772 : 0 : while( osl_scheduleThread(This->m_aThread) )
3773 : : {
3774 : 0 : This->dispatchEvent( 1000 );
3775 : :
3776 : : timeval aNow;
3777 : 0 : gettimeofday( &aNow, 0 );
3778 : :
3779 : 0 : if( (aNow.tv_sec - aLast.tv_sec) > 0 )
3780 : : {
3781 : 0 : osl::ClearableMutexGuard aGuard(This->m_aMutex);
3782 : 0 : std::list< std::pair< SelectionAdaptor*, css::uno::Reference< XInterface > > > aChangeList;
3783 : :
3784 : 0 : for( boost::unordered_map< Atom, Selection* >::iterator it = This->m_aSelections.begin(); it != This->m_aSelections.end(); ++it )
3785 : : {
3786 : 0 : if( it->first != This->m_nXdndSelection && ! it->second->m_bOwner )
3787 : : {
3788 : 0 : XLIB_Window aOwner = XGetSelectionOwner( This->m_pDisplay, it->first );
3789 : 0 : if( aOwner != it->second->m_aLastOwner )
3790 : : {
3791 : 0 : it->second->m_aLastOwner = aOwner;
3792 : : std::pair< SelectionAdaptor*, css::uno::Reference< XInterface > >
3793 : 0 : aKeep( it->second->m_pAdaptor, it->second->m_pAdaptor->getReference() );
3794 : 0 : aChangeList.push_back( aKeep );
3795 : : }
3796 : : }
3797 : : }
3798 : 0 : aGuard.clear();
3799 : 0 : while( aChangeList.begin() != aChangeList.end() )
3800 : : {
3801 : 0 : aChangeList.front().first->fireContentsChanged();
3802 : 0 : aChangeList.pop_front();
3803 : : }
3804 : 0 : aLast = aNow;
3805 : : }
3806 : 0 : }
3807 : : #if OSL_DEBUG_LEVEL > 1
3808 : : fprintf(stderr, "SelectionManager::run end\n" );
3809 : : #endif
3810 : 0 : }
3811 : :
3812 : 0 : void SelectionManager::shutdown() throw()
3813 : : {
3814 : : #if OSL_DEBUG_LEVEL > 1
3815 : : fprintf( stderr, "SelectionManager got app termination event\n" );
3816 : : #endif
3817 : :
3818 : 0 : osl::ResettableMutexGuard aGuard(m_aMutex);
3819 : :
3820 : 0 : if( m_bShutDown )
3821 : 0 : return;
3822 : 0 : m_bShutDown = true;
3823 : :
3824 : 0 : if ( m_xDesktop.is() )
3825 : 0 : m_xDesktop->removeTerminateListener(this);
3826 : :
3827 : 0 : if( m_xDisplayConnection.is() )
3828 : 0 : m_xDisplayConnection->removeEventHandler(Any(), this);
3829 : :
3830 : : // stop dispatching
3831 : 0 : if( m_aThread )
3832 : : {
3833 : 0 : osl_terminateThread( m_aThread );
3834 : : /*
3835 : : * Allow thread to finish before app exits to avoid pulling the carpet
3836 : : * out from under it if pasting is occuring during shutdown
3837 : : *
3838 : : * a) allow it to have the Mutex and
3839 : : * b) reschedule to allow it to complete callbacks to any
3840 : : * Application::GetSolarMutex protected regions, etc. e.g.
3841 : : * TransferableHelper::getTransferDataFlavors (via
3842 : : * SelectionManager::handleSelectionRequest) which it might
3843 : : * currently be trying to enter.
3844 : : *
3845 : : * Otherwise the thread may be left still waiting on a GlobalMutex
3846 : : * when that gets destroyed, letting the thread blow up and die
3847 : : * when enters the section in a now dead OOo instance.
3848 : : */
3849 : 0 : aGuard.clear();
3850 : 0 : while (osl_isThreadRunning(m_aThread))
3851 : : {
3852 : 0 : SolarMutexGuard guard2;
3853 : 0 : Application::Reschedule();
3854 : 0 : }
3855 : 0 : osl_joinWithThread( m_aThread );
3856 : 0 : osl_destroyThread( m_aThread );
3857 : 0 : m_aThread = NULL;
3858 : 0 : aGuard.reset();
3859 : : }
3860 : 0 : m_xDesktop.clear();
3861 : 0 : m_xDisplayConnection.clear();
3862 : 0 : m_xDropTransferable.clear();
3863 : : }
3864 : :
3865 : : // ------------------------------------------------------------------------
3866 : :
3867 : 0 : sal_Bool SelectionManager::handleEvent( const Any& event ) throw()
3868 : : {
3869 : 0 : Sequence< sal_Int8 > aSeq;
3870 : 0 : if( (event >>= aSeq) )
3871 : : {
3872 : 0 : XEvent* pEvent = (XEvent*)aSeq.getArray();
3873 : 0 : XLIB_Time nTimestamp = CurrentTime;
3874 : 0 : if( pEvent->type == ButtonPress || pEvent->type == ButtonRelease )
3875 : 0 : nTimestamp = pEvent->xbutton.time;
3876 : 0 : else if( pEvent->type == XLIB_KeyPress || pEvent->type == KeyRelease )
3877 : 0 : nTimestamp = pEvent->xkey.time;
3878 : 0 : else if( pEvent->type == MotionNotify )
3879 : 0 : nTimestamp = pEvent->xmotion.time;
3880 : 0 : else if( pEvent->type == PropertyNotify )
3881 : 0 : nTimestamp = pEvent->xproperty.time;
3882 : :
3883 : 0 : if( nTimestamp != CurrentTime )
3884 : : {
3885 : 0 : osl::MutexGuard aGuard(m_aMutex);
3886 : :
3887 : 0 : m_nSelectionTimestamp = nTimestamp;
3888 : : }
3889 : :
3890 : 0 : return sal_Bool( handleXEvent( *pEvent ) );
3891 : : }
3892 : : else
3893 : : {
3894 : : #if OSL_DEBUG_LEVEL > 1
3895 : : fprintf( stderr, "SelectionManager got downing event\n" );
3896 : : #endif
3897 : 0 : shutdown();
3898 : : }
3899 : 0 : return sal_True;
3900 : : }
3901 : :
3902 : 0 : void SAL_CALL SelectionManager::disposing( const ::com::sun::star::lang::EventObject& rEvt )
3903 : : throw( ::com::sun::star::uno::RuntimeException )
3904 : : {
3905 : 0 : if (rEvt.Source == m_xDesktop || rEvt.Source == m_xDisplayConnection)
3906 : 0 : shutdown();
3907 : 0 : }
3908 : :
3909 : 0 : void SAL_CALL SelectionManager::queryTermination( const ::com::sun::star::lang::EventObject& )
3910 : : throw( ::com::sun::star::frame::TerminationVetoException, ::com::sun::star::uno::RuntimeException )
3911 : : {
3912 : 0 : }
3913 : :
3914 : : /*
3915 : : * To be safe, shutdown needs to be called before the ~SfxApplication is called, waiting until
3916 : : * the downing event can be too late if paste are requested during shutdown and ~SfxApplication
3917 : : * has been called before vcl is shutdown
3918 : : */
3919 : 0 : void SAL_CALL SelectionManager::notifyTermination( const ::com::sun::star::lang::EventObject& rEvent )
3920 : : throw( ::com::sun::star::uno::RuntimeException )
3921 : : {
3922 : 0 : disposing(rEvent);
3923 : 0 : }
3924 : :
3925 : : // ------------------------------------------------------------------------
3926 : :
3927 : 0 : void SelectionManager::registerHandler( Atom selection, SelectionAdaptor& rAdaptor )
3928 : : {
3929 : 0 : osl::MutexGuard aGuard(m_aMutex);
3930 : :
3931 : 0 : Selection* pNewSelection = new Selection();
3932 : 0 : pNewSelection->m_pAdaptor = &rAdaptor;
3933 : 0 : pNewSelection->m_aAtom = selection;
3934 : 0 : m_aSelections[ selection ] = pNewSelection;
3935 : 0 : }
3936 : :
3937 : : // ------------------------------------------------------------------------
3938 : :
3939 : 0 : void SelectionManager::deregisterHandler( Atom selection )
3940 : : {
3941 : 0 : osl::MutexGuard aGuard(m_aMutex);
3942 : :
3943 : : ::boost::unordered_map< Atom, Selection* >::iterator it =
3944 : 0 : m_aSelections.find( selection );
3945 : 0 : if( it != m_aSelections.end() )
3946 : : {
3947 : 0 : delete it->second->m_pPixmap;
3948 : 0 : delete it->second;
3949 : 0 : m_aSelections.erase( it );
3950 : 0 : }
3951 : 0 : }
3952 : :
3953 : : // ------------------------------------------------------------------------
3954 : :
3955 : : static bool bWasError = false;
3956 : :
3957 : : extern "C"
3958 : : {
3959 : 0 : int local_xerror_handler(Display* , XErrorEvent*)
3960 : : {
3961 : 0 : bWasError = true;
3962 : 0 : return 0;
3963 : : }
3964 : : typedef int(*xerror_hdl_t)(Display*,XErrorEvent*);
3965 : : }
3966 : :
3967 : 0 : void SelectionManager::registerDropTarget( XLIB_Window aWindow, DropTarget* pTarget )
3968 : : {
3969 : 0 : osl::MutexGuard aGuard(m_aMutex);
3970 : :
3971 : : // sanity check
3972 : : ::boost::unordered_map< XLIB_Window, DropTargetEntry >::const_iterator it =
3973 : 0 : m_aDropTargets.find( aWindow );
3974 : 0 : if( it != m_aDropTargets.end() )
3975 : : OSL_FAIL( "attempt to register window as drop target twice" );
3976 : 0 : else if( aWindow && m_pDisplay )
3977 : : {
3978 : 0 : DropTargetEntry aEntry( pTarget );
3979 : 0 : bWasError=false;
3980 : : /* #i100000# ugly workaround: gtk sets its own XErrorHandler which is not suitable for us
3981 : : unfortunately XErrorHandler is not per display, so this is just and ugly hack
3982 : : Need to remove separate display and integrate clipboard/dnd into vcl's unx code ASAP
3983 : : */
3984 : 0 : xerror_hdl_t pOldHandler = XSetErrorHandler( local_xerror_handler );
3985 : 0 : XSelectInput( m_pDisplay, aWindow, PropertyChangeMask );
3986 : 0 : if( ! bWasError )
3987 : : {
3988 : : // set XdndAware
3989 : 0 : XChangeProperty( m_pDisplay, aWindow, m_nXdndAware, XA_ATOM, 32, PropModeReplace, (unsigned char*)&nXdndProtocolRevision, 1 );
3990 : 0 : if( ! bWasError )
3991 : : {
3992 : : // get root window of window (in 99.999% of all cases this will be
3993 : : // DefaultRootWindow( m_pDisplay )
3994 : : int x, y;
3995 : : unsigned int w, h, bw, d;
3996 : : XGetGeometry( m_pDisplay, aWindow, &aEntry.m_aRootWindow,
3997 : 0 : &x, &y, &w, &h, &bw, &d );
3998 : : }
3999 : : }
4000 : 0 : XSetErrorHandler( pOldHandler );
4001 : 0 : if(bWasError)
4002 : 0 : return;
4003 : 0 : m_aDropTargets[ aWindow ] = aEntry;
4004 : : }
4005 : : else
4006 : 0 : OSL_FAIL( "attempt to register None as drop target" );
4007 : : }
4008 : :
4009 : : // ------------------------------------------------------------------------
4010 : :
4011 : 0 : void SelectionManager::deregisterDropTarget( XLIB_Window aWindow )
4012 : : {
4013 : 0 : osl::ClearableMutexGuard aGuard(m_aMutex);
4014 : :
4015 : 0 : m_aDropTargets.erase( aWindow );
4016 : 0 : if( aWindow == m_aDragSourceWindow && m_aDragRunning.check() )
4017 : : {
4018 : : // abort drag
4019 : : boost::unordered_map< XLIB_Window, DropTargetEntry >::const_iterator it =
4020 : 0 : m_aDropTargets.find( m_aDropWindow );
4021 : 0 : if( it != m_aDropTargets.end() )
4022 : : {
4023 : 0 : DropTargetEvent dte;
4024 : 0 : dte.Source = static_cast< OWeakObject* >( it->second.m_pTarget );
4025 : 0 : aGuard.clear();
4026 : 0 : it->second.m_pTarget->dragExit( dte );
4027 : : }
4028 : 0 : else if( m_aDropProxy != None && m_nCurrentProtocolVersion >= 0 )
4029 : : {
4030 : : // send XdndLeave
4031 : : XEvent aEvent;
4032 : 0 : aEvent.type = ClientMessage;
4033 : 0 : aEvent.xclient.display = m_pDisplay;
4034 : 0 : aEvent.xclient.format = 32;
4035 : 0 : aEvent.xclient.message_type = m_nXdndLeave;
4036 : 0 : aEvent.xclient.window = m_aDropWindow;
4037 : 0 : aEvent.xclient.data.l[0] = m_aWindow;
4038 : 0 : memset( aEvent.xclient.data.l+1, 0, sizeof(long)*4);
4039 : 0 : m_aDropWindow = m_aDropProxy = None;
4040 : 0 : XSendEvent( m_pDisplay, m_aDropProxy, False, NoEventMask, &aEvent );
4041 : : }
4042 : : // notify the listener
4043 : 0 : DragSourceDropEvent dsde;
4044 : 0 : dsde.Source = static_cast< OWeakObject* >(this);
4045 : 0 : dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, m_nDragTimestamp, *this );
4046 : 0 : dsde.DragSource = static_cast< XDragSource* >(this);
4047 : 0 : dsde.DropAction = DNDConstants::ACTION_NONE;
4048 : 0 : dsde.DropSuccess = sal_False;
4049 : 0 : css::uno::Reference< XDragSourceListener > xListener( m_xDragSourceListener );
4050 : 0 : m_xDragSourceListener.clear();
4051 : 0 : aGuard.clear();
4052 : 0 : xListener->dragDropEnd( dsde );
4053 : 0 : }
4054 : 0 : }
4055 : :
4056 : : /*
4057 : : * SelectionAdaptor
4058 : : */
4059 : :
4060 : 0 : css::uno::Reference< XTransferable > SelectionManager::getTransferable() throw()
4061 : : {
4062 : 0 : return m_xDragSourceTransferable;
4063 : : }
4064 : :
4065 : : // ------------------------------------------------------------------------
4066 : :
4067 : 0 : void SelectionManager::clearTransferable() throw()
4068 : : {
4069 : 0 : m_xDragSourceTransferable.clear();
4070 : 0 : }
4071 : :
4072 : : // ------------------------------------------------------------------------
4073 : :
4074 : 0 : void SelectionManager::fireContentsChanged() throw()
4075 : : {
4076 : 0 : }
4077 : :
4078 : : // ------------------------------------------------------------------------
4079 : :
4080 : 0 : css::uno::Reference< XInterface > SelectionManager::getReference() throw()
4081 : : {
4082 : 0 : return css::uno::Reference< XInterface >( static_cast<OWeakObject*>(this) );
4083 : : }
4084 : :
4085 : : // ------------------------------------------------------------------------
4086 : :
4087 : : /*
4088 : : * SelectionManagerHolder
4089 : : */
4090 : :
4091 : 0 : SelectionManagerHolder::SelectionManagerHolder() :
4092 : : ::cppu::WeakComponentImplHelper3<
4093 : : XDragSource,
4094 : : XInitialization,
4095 : 0 : XServiceInfo > (m_aMutex)
4096 : : {
4097 : 0 : }
4098 : :
4099 : : // ------------------------------------------------------------------------
4100 : :
4101 : 0 : SelectionManagerHolder::~SelectionManagerHolder()
4102 : : {
4103 : 0 : }
4104 : :
4105 : : // ------------------------------------------------------------------------
4106 : :
4107 : 0 : void SelectionManagerHolder::initialize( const Sequence< Any >& arguments ) throw( ::com::sun::star::uno::Exception )
4108 : : {
4109 : 0 : OUString aDisplayName;
4110 : :
4111 : 0 : if( arguments.getLength() > 0 )
4112 : : {
4113 : 0 : css::uno::Reference< XDisplayConnection > xConn;
4114 : 0 : arguments.getConstArray()[0] >>= xConn;
4115 : 0 : if( xConn.is() )
4116 : : {
4117 : 0 : Any aIdentifier;
4118 : 0 : aIdentifier >>= aDisplayName;
4119 : 0 : }
4120 : : }
4121 : :
4122 : 0 : SelectionManager& rManager = SelectionManager::get( aDisplayName );
4123 : 0 : rManager.initialize( arguments );
4124 : 0 : m_xRealDragSource = static_cast< XDragSource* >(&rManager);
4125 : 0 : }
4126 : :
4127 : : /*
4128 : : * XDragSource
4129 : : */
4130 : :
4131 : 0 : sal_Bool SelectionManagerHolder::isDragImageSupported() throw()
4132 : : {
4133 : 0 : return m_xRealDragSource.is() ? m_xRealDragSource->isDragImageSupported() : sal_False;
4134 : : }
4135 : :
4136 : : // ------------------------------------------------------------------------
4137 : :
4138 : 0 : sal_Int32 SelectionManagerHolder::getDefaultCursor( sal_Int8 dragAction ) throw()
4139 : : {
4140 : 0 : return m_xRealDragSource.is() ? m_xRealDragSource->getDefaultCursor( dragAction ) : 0;
4141 : : }
4142 : :
4143 : : // ------------------------------------------------------------------------
4144 : :
4145 : 0 : void SelectionManagerHolder::startDrag(
4146 : : const ::com::sun::star::datatransfer::dnd::DragGestureEvent& trigger,
4147 : : sal_Int8 sourceActions, sal_Int32 cursor, sal_Int32 image,
4148 : : const css::uno::Reference< ::com::sun::star::datatransfer::XTransferable >& transferable,
4149 : : const css::uno::Reference< ::com::sun::star::datatransfer::dnd::XDragSourceListener >& listener
4150 : : ) throw()
4151 : : {
4152 : 0 : if( m_xRealDragSource.is() )
4153 : 0 : m_xRealDragSource->startDrag( trigger, sourceActions, cursor, image, transferable, listener );
4154 : 0 : }
4155 : :
4156 : : // ------------------------------------------------------------------------
4157 : :
4158 : : /*
4159 : : * XServiceInfo
4160 : : */
4161 : :
4162 : : // ------------------------------------------------------------------------
4163 : :
4164 : 0 : OUString SelectionManagerHolder::getImplementationName() throw()
4165 : : {
4166 : 0 : return OUString(XDND_IMPLEMENTATION_NAME);
4167 : : }
4168 : :
4169 : : // ------------------------------------------------------------------------
4170 : :
4171 : 0 : sal_Bool SelectionManagerHolder::supportsService( const OUString& ServiceName ) throw()
4172 : : {
4173 : 0 : Sequence < OUString > SupportedServicesNames = Xdnd_getSupportedServiceNames();
4174 : :
4175 : 0 : for ( sal_Int32 n = SupportedServicesNames.getLength(); n--; )
4176 : 0 : if (SupportedServicesNames[n].compareTo(ServiceName) == 0)
4177 : 0 : return sal_True;
4178 : :
4179 : 0 : return sal_False;
4180 : : }
4181 : :
4182 : : // ------------------------------------------------------------------------
4183 : :
4184 : 0 : Sequence< OUString > SelectionManagerHolder::getSupportedServiceNames() throw()
4185 : : {
4186 : 0 : return Xdnd_getSupportedServiceNames();
4187 : : }
4188 : :
4189 : :
4190 : : // ------------------------------------------------------------------------
4191 : :
4192 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|