diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
commit | 940b4d1848e8c70ab7642901a68594e8016caffc (patch) | |
tree | eb72f344ee6c3d9b80a7ecc079ea79e9fba8676d /vcl/unx/generic/dtrans | |
parent | Initial commit. (diff) | |
download | libreoffice-940b4d1848e8c70ab7642901a68594e8016caffc.tar.xz libreoffice-940b4d1848e8c70ab7642901a68594e8016caffc.zip |
Adding upstream version 1:7.0.4.upstream/1%7.0.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vcl/unx/generic/dtrans')
21 files changed, 6894 insertions, 0 deletions
diff --git a/vcl/unx/generic/dtrans/X11_clipboard.cxx b/vcl/unx/generic/dtrans/X11_clipboard.cxx new file mode 100644 index 000000000..17104ca75 --- /dev/null +++ b/vcl/unx/generic/dtrans/X11_clipboard.cxx @@ -0,0 +1,231 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <X11/Xatom.h> +#include "X11_clipboard.hxx" +#include "X11_transferable.hxx" +#include <com/sun/star/datatransfer/clipboard/RenderingCapabilities.hpp> +#include <cppuhelper/supportsservice.hxx> +#include <rtl/ref.hxx> +#include <sal/log.hxx> + +#if OSL_DEBUG_LEVEL > 1 +#include <stdio.h> +#endif + +using namespace com::sun::star::datatransfer; +using namespace com::sun::star::datatransfer::clipboard; +using namespace com::sun::star::lang; +using namespace com::sun::star::uno; +using namespace com::sun::star::awt; +using namespace cppu; +using namespace osl; +using namespace x11; + +X11Clipboard::X11Clipboard( SelectionManager& rManager, Atom aSelection ) : + ::cppu::WeakComponentImplHelper< + css::datatransfer::clipboard::XSystemClipboard, + css::lang::XServiceInfo + >( rManager.getMutex() ), + + m_xSelectionManager( &rManager ), + m_aSelection( aSelection ) +{ +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "creating instance of X11Clipboard (this=" + << this << ")."); +#endif +} + +css::uno::Reference<css::datatransfer::clipboard::XClipboard> +X11Clipboard::create( SelectionManager& rManager, Atom aSelection ) +{ + rtl::Reference<X11Clipboard> cb(new X11Clipboard(rManager, aSelection)); + if( aSelection != None ) + { + rManager.registerHandler(aSelection, *cb); + } + else + { + rManager.registerHandler(XA_PRIMARY, *cb); + rManager.registerHandler(rManager.getAtom("CLIPBOARD"), *cb); + } + return cb.get(); +} + +X11Clipboard::~X11Clipboard() +{ + MutexGuard aGuard( *Mutex::getGlobalMutex() ); + +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "shutting down instance of X11Clipboard (this=" + << this + << ", Selection=\"" + << m_xSelectionManager->getString( m_aSelection ) + << "\")."); +#endif + + if( m_aSelection != None ) + m_xSelectionManager->deregisterHandler( m_aSelection ); + else + { + m_xSelectionManager->deregisterHandler( XA_PRIMARY ); + m_xSelectionManager->deregisterHandler( m_xSelectionManager->getAtom( "CLIPBOARD" ) ); + } +} + +void X11Clipboard::fireChangedContentsEvent() +{ + ClearableMutexGuard aGuard( m_xSelectionManager->getMutex() ); +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "X11Clipboard::fireChangedContentsEvent for " + << m_xSelectionManager->getString( m_aSelection ) + << " (" << m_aListeners.size() << " listeners)."); +#endif + ::std::vector< Reference< XClipboardListener > > listeners( m_aListeners ); + aGuard.clear(); + + ClipboardEvent aEvent( static_cast<OWeakObject*>(this), m_aContents); + for (auto const& listener : listeners) + { + if( listener.is() ) + listener->changedContents(aEvent); + } +} + +void X11Clipboard::clearContents() +{ + ClearableMutexGuard aGuard(m_xSelectionManager->getMutex()); + // protect against deletion during outside call + Reference< XClipboard > xThis( static_cast<XClipboard*>(this)); + // copy member references on stack so they can be called + // without having the mutex + Reference< XClipboardOwner > xOwner( m_aOwner ); + Reference< XTransferable > xKeepAlive( m_aContents ); + // clear members + m_aOwner.clear(); + m_aContents.clear(); + + // release the mutex + aGuard.clear(); + + // inform previous owner of lost ownership + if ( xOwner.is() ) + xOwner->lostOwnership(xThis, m_aContents); +} + +Reference< XTransferable > SAL_CALL X11Clipboard::getContents() +{ + MutexGuard aGuard(m_xSelectionManager->getMutex()); + + if( ! m_aContents.is() ) + m_aContents = new X11Transferable( SelectionManager::get(), m_aSelection ); + return m_aContents; +} + +void SAL_CALL X11Clipboard::setContents( + const Reference< XTransferable >& xTrans, + const Reference< XClipboardOwner >& xClipboardOwner ) +{ + // remember old values for callbacks before setting the new ones. + ClearableMutexGuard aGuard(m_xSelectionManager->getMutex()); + + Reference< XClipboardOwner > oldOwner( m_aOwner ); + m_aOwner = xClipboardOwner; + + Reference< XTransferable > oldContents( m_aContents ); + m_aContents = xTrans; + + aGuard.clear(); + + // for now request ownership for both selections + if( m_aSelection != None ) + m_xSelectionManager->requestOwnership( m_aSelection ); + else + { + m_xSelectionManager->requestOwnership( XA_PRIMARY ); + m_xSelectionManager->requestOwnership( m_xSelectionManager->getAtom( "CLIPBOARD" ) ); + } + + // notify old owner on loss of ownership + if( oldOwner.is() ) + oldOwner->lostOwnership(static_cast < XClipboard * > (this), oldContents); + + // notify all listeners on content changes + fireChangedContentsEvent(); +} + +OUString SAL_CALL X11Clipboard::getName() +{ + return m_xSelectionManager->getString( m_aSelection ); +} + +sal_Int8 SAL_CALL X11Clipboard::getRenderingCapabilities() +{ + return RenderingCapabilities::Delayed; +} + +void SAL_CALL X11Clipboard::addClipboardListener( const Reference< XClipboardListener >& listener ) +{ + MutexGuard aGuard( m_xSelectionManager->getMutex() ); + m_aListeners.push_back( listener ); +} + +void SAL_CALL X11Clipboard::removeClipboardListener( const Reference< XClipboardListener >& listener ) +{ + MutexGuard aGuard( m_xSelectionManager->getMutex() ); + m_aListeners.erase( std::remove(m_aListeners.begin(), m_aListeners.end(), listener), m_aListeners.end() ); +} + +Reference< XTransferable > X11Clipboard::getTransferable() +{ + return getContents(); +} + +void X11Clipboard::clearTransferable() +{ + clearContents(); +} + +void X11Clipboard::fireContentsChanged() +{ + fireChangedContentsEvent(); +} + +Reference< XInterface > X11Clipboard::getReference() throw() +{ + return Reference< XInterface >( static_cast< OWeakObject* >(this) ); +} + +OUString SAL_CALL X11Clipboard::getImplementationName( ) +{ + return X11_CLIPBOARD_IMPLEMENTATION_NAME; +} + +sal_Bool SAL_CALL X11Clipboard::supportsService( const OUString& ServiceName ) +{ + return cppu::supportsService(this, ServiceName); +} + +Sequence< OUString > SAL_CALL X11Clipboard::getSupportedServiceNames( ) +{ + return X11Clipboard_getSupportedServiceNames(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/generic/dtrans/X11_clipboard.hxx b/vcl/unx/generic/dtrans/X11_clipboard.hxx new file mode 100644 index 000000000..4092d3f67 --- /dev/null +++ b/vcl/unx/generic/dtrans/X11_clipboard.hxx @@ -0,0 +1,114 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_VCL_UNX_GENERIC_DTRANS_X11_CLIPBOARD_HXX +#define INCLUDED_VCL_UNX_GENERIC_DTRANS_X11_CLIPBOARD_HXX + +#include "X11_selection.hxx" + +#include <com/sun/star/datatransfer/clipboard/XSystemClipboard.hpp> +#include <cppuhelper/compbase.hxx> + +#define X11_CLIPBOARD_IMPLEMENTATION_NAME "com.sun.star.datatransfer.X11ClipboardSupport" + +namespace x11 { + + class X11Clipboard : + public ::cppu::WeakComponentImplHelper < + css::datatransfer::clipboard::XSystemClipboard, + css::lang::XServiceInfo + >, + public SelectionAdaptor + { + css::uno::Reference< css::datatransfer::XTransferable > m_aContents; + css::uno::Reference< css::datatransfer::clipboard::XClipboardOwner > m_aOwner; + + rtl::Reference<SelectionManager> m_xSelectionManager; + ::std::vector< css::uno::Reference< css::datatransfer::clipboard::XClipboardListener > > m_aListeners; + Atom m_aSelection; + + X11Clipboard( SelectionManager& rManager, Atom aSelection ); + + friend class SelectionManager; + + void fireChangedContentsEvent(); + void clearContents(); + + public: + + static css::uno::Reference<css::datatransfer::clipboard::XClipboard> + create( SelectionManager& rManager, Atom aSelection ); + + virtual ~X11Clipboard() override; + + /* + * XServiceInfo + */ + + virtual OUString SAL_CALL getImplementationName( ) override; + + virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override; + + virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) override; + + /* + * XClipboard + */ + + virtual css::uno::Reference< css::datatransfer::XTransferable > SAL_CALL getContents() override; + + virtual void SAL_CALL setContents( + const css::uno::Reference< css::datatransfer::XTransferable >& xTrans, + const css::uno::Reference< css::datatransfer::clipboard::XClipboardOwner >& xClipboardOwner ) override; + + virtual OUString SAL_CALL getName() override; + + /* + * XClipboardEx + */ + + virtual sal_Int8 SAL_CALL getRenderingCapabilities() override; + + /* + * XClipboardNotifier + */ + virtual void SAL_CALL addClipboardListener( + const css::uno::Reference< css::datatransfer::clipboard::XClipboardListener >& listener ) override; + + virtual void SAL_CALL removeClipboardListener( + const css::uno::Reference< css::datatransfer::clipboard::XClipboardListener >& listener ) override; + + /* + * SelectionAdaptor + */ + virtual css::uno::Reference< css::datatransfer::XTransferable > getTransferable() override; + virtual void clearTransferable() override; + virtual void fireContentsChanged() override; + virtual css::uno::Reference< css::uno::XInterface > getReference() throw() override; + }; + + css::uno::Sequence< OUString > X11Clipboard_getSupportedServiceNames(); + css::uno::Reference< css::uno::XInterface > SAL_CALL X11Clipboard_createInstance( + const css::uno::Reference< css::lang::XMultiServiceFactory > & xMultiServiceFactory); + +} // namepspace + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/generic/dtrans/X11_dndcontext.cxx b/vcl/unx/generic/dtrans/X11_dndcontext.cxx new file mode 100644 index 000000000..638c47387 --- /dev/null +++ b/vcl/unx/generic/dtrans/X11_dndcontext.cxx @@ -0,0 +1,118 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include "X11_dndcontext.hxx" +#include "X11_selection.hxx" + +using namespace cppu; +using namespace x11; + +/* + * DropTargetDropContext + */ + +DropTargetDropContext::DropTargetDropContext( + ::Window aDropWindow, + SelectionManager& rManager ) : + m_aDropWindow( aDropWindow ), + m_xManager( &rManager ) +{ +} + +DropTargetDropContext::~DropTargetDropContext() +{ +} + +void DropTargetDropContext::acceptDrop( sal_Int8 dragOperation ) +{ + m_xManager->accept( dragOperation, m_aDropWindow ); +} + +void DropTargetDropContext::rejectDrop() +{ + m_xManager->reject( m_aDropWindow ); +} + +void DropTargetDropContext::dropComplete( sal_Bool success ) +{ + m_xManager->dropComplete( success, m_aDropWindow ); +} + +/* + * DropTargetDragContext + */ + +DropTargetDragContext::DropTargetDragContext( + ::Window aDropWindow, + SelectionManager& rManager ) : + m_aDropWindow( aDropWindow ), + m_xManager( &rManager ) +{ +} + +DropTargetDragContext::~DropTargetDragContext() +{ +} + +void DropTargetDragContext::acceptDrag( sal_Int8 dragOperation ) +{ + m_xManager->accept( dragOperation, m_aDropWindow ); +} + +void DropTargetDragContext::rejectDrag() +{ + m_xManager->reject( m_aDropWindow ); +} + +/* + * DragSourceContext + */ + +DragSourceContext::DragSourceContext( + ::Window aDropWindow, + SelectionManager& rManager ) : + m_aDropWindow( aDropWindow ), + m_xManager( &rManager ) +{ +} + +DragSourceContext::~DragSourceContext() +{ +} + +sal_Int32 DragSourceContext::getCurrentCursor() +{ + return m_xManager->getCurrentCursor(); +} + +void DragSourceContext::setCursor( sal_Int32 cursorId ) +{ + m_xManager->setCursor( cursorId, m_aDropWindow ); +} + +void DragSourceContext::setImage( sal_Int32 ) +{ +} + +void DragSourceContext::transferablesFlavorsChanged() +{ + m_xManager->transferablesFlavorsChanged(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/generic/dtrans/X11_dndcontext.hxx b/vcl/unx/generic/dtrans/X11_dndcontext.hxx new file mode 100644 index 000000000..18c33666b --- /dev/null +++ b/vcl/unx/generic/dtrans/X11_dndcontext.hxx @@ -0,0 +1,83 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_VCL_UNX_GENERIC_DTRANS_X11_DNDCONTEXT_HXX +#define INCLUDED_VCL_UNX_GENERIC_DTRANS_X11_DNDCONTEXT_HXX + +#include <com/sun/star/datatransfer/dnd/XDragSourceContext.hpp> +#include <com/sun/star/datatransfer/dnd/XDropTargetDropContext.hpp> +#include <com/sun/star/datatransfer/dnd/XDropTargetDragContext.hpp> +#include <cppuhelper/implbase.hxx> +#include <rtl/ref.hxx> + +#include <X11/X.h> + +namespace x11 { + + class SelectionManager; + + class DropTargetDropContext : + public ::cppu::WeakImplHelper<css::datatransfer::dnd::XDropTargetDropContext> + { + ::Window m_aDropWindow; + rtl::Reference<SelectionManager> m_xManager; + public: + DropTargetDropContext( ::Window, SelectionManager& ); + virtual ~DropTargetDropContext() override; + + // XDropTargetDropContext + virtual void SAL_CALL acceptDrop( sal_Int8 dragOperation ) override; + virtual void SAL_CALL rejectDrop() override; + virtual void SAL_CALL dropComplete( sal_Bool success ) override; + }; + + class DropTargetDragContext : + public ::cppu::WeakImplHelper<css::datatransfer::dnd::XDropTargetDragContext> + { + ::Window m_aDropWindow; + rtl::Reference<SelectionManager> m_xManager; + public: + DropTargetDragContext( ::Window, SelectionManager& ); + virtual ~DropTargetDragContext() override; + + // XDropTargetDragContext + virtual void SAL_CALL acceptDrag( sal_Int8 dragOperation ) override; + virtual void SAL_CALL rejectDrag() override; + }; + + class DragSourceContext : + public ::cppu::WeakImplHelper<css::datatransfer::dnd::XDragSourceContext> + { + ::Window m_aDropWindow; + rtl::Reference<SelectionManager> m_xManager; + public: + DragSourceContext( ::Window, SelectionManager& ); + virtual ~DragSourceContext() override; + + // XDragSourceContext + virtual sal_Int32 SAL_CALL getCurrentCursor() override; + virtual void SAL_CALL setCursor( sal_Int32 cursorId ) override; + virtual void SAL_CALL setImage( sal_Int32 imageId ) override; + virtual void SAL_CALL transferablesFlavorsChanged() override; + }; +} // namespace + +#endif // INCLUDED_VCL_UNX_GENERIC_DTRANS_X11_DNDCONTEXT_HXX + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/generic/dtrans/X11_droptarget.cxx b/vcl/unx/generic/dtrans/X11_droptarget.cxx new file mode 100644 index 000000000..5e1320fe3 --- /dev/null +++ b/vcl/unx/generic/dtrans/X11_droptarget.cxx @@ -0,0 +1,177 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <cppuhelper/supportsservice.hxx> +#include "X11_selection.hxx" + +using namespace x11; +using namespace com::sun::star::uno; +using namespace com::sun::star::lang; +using namespace com::sun::star::awt; +using namespace com::sun::star::datatransfer; +using namespace com::sun::star::datatransfer::dnd; + +DropTarget::DropTarget() : + ::cppu::WeakComponentImplHelper< + XDropTarget, + XInitialization, + XServiceInfo + >( m_aMutex ), + m_bActive( false ), + m_nDefaultActions( 0 ), + m_aTargetWindow( None ) +{ +} + +DropTarget::~DropTarget() +{ + if( m_xSelectionManager.is() ) + m_xSelectionManager->deregisterDropTarget( m_aTargetWindow ); +} + +void DropTarget::initialize( const Sequence< Any >& arguments ) +{ + if( arguments.getLength() > 1 ) + { + OUString aDisplayName; + Reference< XDisplayConnection > xConn; + arguments.getConstArray()[0] >>= xConn; + if( xConn.is() ) + { + Any aIdentifier; + aIdentifier >>= aDisplayName; + } + + m_xSelectionManager = &SelectionManager::get( aDisplayName ); + m_xSelectionManager->initialize( arguments ); + + if( m_xSelectionManager->getDisplay() ) // #136582# sanity check + { + sal_IntPtr aWindow = None; + arguments.getConstArray()[1] >>= aWindow; + m_xSelectionManager->registerDropTarget( aWindow, this ); + m_aTargetWindow = aWindow; + m_bActive = true; + } + } +} + +void DropTarget::addDropTargetListener( const Reference< XDropTargetListener >& xListener ) +{ + ::osl::Guard< ::osl::Mutex > aGuard( m_aMutex ); + + m_aListeners.push_back( xListener ); +} + +void DropTarget::removeDropTargetListener( const Reference< XDropTargetListener >& xListener ) +{ + ::osl::Guard< ::osl::Mutex > aGuard( m_aMutex ); + + m_aListeners.erase( std::remove(m_aListeners.begin(), m_aListeners.end(), xListener), m_aListeners.end() ); +} + +sal_Bool DropTarget::isActive() +{ + return m_bActive; +} + +void DropTarget::setActive( sal_Bool active ) +{ + ::osl::Guard< ::osl::Mutex > aGuard( m_aMutex ); + + m_bActive = active; +} + +sal_Int8 DropTarget::getDefaultActions() +{ + return m_nDefaultActions; +} + +void DropTarget::setDefaultActions( sal_Int8 actions ) +{ + ::osl::Guard< ::osl::Mutex > aGuard( m_aMutex ); + + m_nDefaultActions = actions; +} + +void DropTarget::drop( const DropTargetDropEvent& dtde ) throw() +{ + osl::ClearableGuard< ::osl::Mutex > aGuard( m_aMutex ); + std::vector< Reference< XDropTargetListener > > aListeners( m_aListeners ); + aGuard.clear(); + + for (auto const& listener : aListeners) + { + listener->drop(dtde); + } +} + +void DropTarget::dragEnter( const DropTargetDragEnterEvent& dtde ) throw() +{ + osl::ClearableGuard< ::osl::Mutex > aGuard( m_aMutex ); + std::vector< Reference< XDropTargetListener > > aListeners( m_aListeners ); + aGuard.clear(); + + for (auto const& listener : aListeners) + { + listener->dragEnter(dtde); + } +} + +void DropTarget::dragExit( const DropTargetEvent& dte ) throw() +{ + osl::ClearableGuard< ::osl::Mutex > aGuard( m_aMutex ); + std::vector< Reference< XDropTargetListener > > aListeners( m_aListeners ); + aGuard.clear(); + + for (auto const& listener : aListeners) + { + listener->dragExit(dte); + } +} + +void DropTarget::dragOver( const DropTargetDragEvent& dtde ) throw() +{ + osl::ClearableGuard< ::osl::Mutex > aGuard( m_aMutex ); + std::vector< Reference< XDropTargetListener > > aListeners( m_aListeners ); + aGuard.clear(); + + for (auto const& listener : aListeners) + { + listener->dragOver(dtde); + } +} + +// XServiceInfo +OUString DropTarget::getImplementationName() +{ + return XDND_DROPTARGET_IMPLEMENTATION_NAME; +} + +sal_Bool DropTarget::supportsService( const OUString& ServiceName ) +{ + return cppu::supportsService(this, ServiceName); +} + +Sequence< OUString > DropTarget::getSupportedServiceNames() +{ + return Xdnd_dropTarget_getSupportedServiceNames(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/generic/dtrans/X11_selection.cxx b/vcl/unx/generic/dtrans/X11_selection.cxx new file mode 100644 index 000000000..3e15173ba --- /dev/null +++ b/vcl/unx/generic/dtrans/X11_selection.cxx @@ -0,0 +1,4157 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> +#include <sal/log.hxx> + +#include <cstdlib> + +#include <unx/saldisp.hxx> + +#include <unistd.h> +#include <string.h> +#include <sys/time.h> + +#include <X11/Xlib.h> +#include <X11/Xutil.h> +#include <X11/XKBlib.h> +#include <X11/Xatom.h> +#include <X11/keysym.h> + +#if defined(NETBSD) || defined (FREEBSD) || defined(OPENBSD) +#include <sys/poll.h> +#else +#include <poll.h> +#endif + +#include <sal/macros.h> + +#include "X11_selection.hxx" +#include "X11_clipboard.hxx" +#include "X11_transferable.hxx" +#include "X11_dndcontext.hxx" +#include "bmp.hxx" + +#include <vcl/svapp.hxx> + +// pointer bitmaps +#include "copydata_curs.h" +#include "copydata_mask.h" +#include "movedata_curs.h" +#include "movedata_mask.h" +#include "linkdata_curs.h" +#include "linkdata_mask.h" +#include "nodrop_curs.h" +#include "nodrop_mask.h" +#include <com/sun/star/datatransfer/dnd/DNDConstants.hpp> +#include <com/sun/star/awt/MouseEvent.hpp> +#include <com/sun/star/awt/MouseButton.hpp> +#include <com/sun/star/frame/Desktop.hpp> +#include <rtl/tencinfo.h> +#include <rtl/ustrbuf.hxx> + +#include <comphelper/processfactory.hxx> +#include <comphelper/solarmutex.hxx> + +#include <cppuhelper/supportsservice.hxx> +#include <algorithm> + +static constexpr auto DRAG_EVENT_MASK = ButtonPressMask | + ButtonReleaseMask | + PointerMotionMask | + EnterWindowMask | + LeaveWindowMask; + +using namespace com::sun::star::datatransfer; +using namespace com::sun::star::datatransfer::dnd; +using namespace com::sun::star::lang; +using namespace com::sun::star::awt; +using namespace com::sun::star::uno; +using namespace com::sun::star::frame; +using namespace cppu; + +using namespace x11; + +// stubs to satisfy solaris compiler's rather rigid linking warning +extern "C" +{ + static void call_SelectionManager_run( void * pMgr ) + { + SelectionManager::run( pMgr ); + } + + static void call_SelectionManager_runDragExecute( void * pMgr ) + { + osl_setThreadName("SelectionManager::runDragExecute()"); + SelectionManager::runDragExecute( pMgr ); + } +} + +static const long nXdndProtocolRevision = 5; + +namespace { + +// mapping between mime types (or what the office thinks of mime types) +// and X convention types +struct NativeTypeEntry +{ + Atom nAtom; + const char* pType; // Mime encoding on our side + const char* pNativeType; // string corresponding to nAtom for the case of nAtom being uninitialized + int nFormat; // the corresponding format +}; + +} + +// the convention for Xdnd is mime types as specified by the corresponding +// RFC's with the addition that text/plain without charset tag contains iso8859-1 +// sadly some applications (e.g. gtk) do not honor the mimetype only rule, +// so for compatibility add UTF8_STRING +static NativeTypeEntry aXdndConversionTab[] = +{ + { 0, "text/plain;charset=iso8859-1", "text/plain", 8 }, + { 0, "text/plain;charset=utf-8", "UTF8_STRING", 8 } +}; + +// for clipboard and primary selections there is only a convention for text +// that the encoding name of the text is taken as type in all capitalized letters +static NativeTypeEntry aNativeConversionTab[] = +{ + { 0, "text/plain;charset=utf-16", "ISO10646-1", 16 }, + { 0, "text/plain;charset=utf-8", "UTF8_STRING", 8 }, + { 0, "text/plain;charset=utf-8", "UTF-8", 8 }, + { 0, "text/plain;charset=utf-8", "text/plain;charset=UTF-8", 8 }, + // ISO encodings + { 0, "text/plain;charset=iso8859-2", "ISO8859-2", 8 }, + { 0, "text/plain;charset=iso8859-3", "ISO8859-3", 8 }, + { 0, "text/plain;charset=iso8859-4", "ISO8859-4", 8 }, + { 0, "text/plain;charset=iso8859-5", "ISO8859-5", 8 }, + { 0, "text/plain;charset=iso8859-6", "ISO8859-6", 8 }, + { 0, "text/plain;charset=iso8859-7", "ISO8859-7", 8 }, + { 0, "text/plain;charset=iso8859-8", "ISO8859-8", 8 }, + { 0, "text/plain;charset=iso8859-9", "ISO8859-9", 8 }, + { 0, "text/plain;charset=iso8859-10", "ISO8859-10", 8 }, + { 0, "text/plain;charset=iso8859-13", "ISO8859-13", 8 }, + { 0, "text/plain;charset=iso8859-14", "ISO8859-14", 8 }, + { 0, "text/plain;charset=iso8859-15", "ISO8859-15", 8 }, + // asian encodings + { 0, "text/plain;charset=jisx0201.1976-0", "JISX0201.1976-0", 8 }, + { 0, "text/plain;charset=jisx0208.1983-0", "JISX0208.1983-0", 8 }, + { 0, "text/plain;charset=jisx0208.1990-0", "JISX0208.1990-0", 8 }, + { 0, "text/plain;charset=jisx0212.1990-0", "JISX0212.1990-0", 8 }, + { 0, "text/plain;charset=gb2312.1980-0", "GB2312.1980-0", 8 }, + { 0, "text/plain;charset=ksc5601.1992-0", "KSC5601.1992-0", 8 }, + // eastern european encodings + { 0, "text/plain;charset=koi8-r", "KOI8-R", 8 }, + { 0, "text/plain;charset=koi8-u", "KOI8-U", 8 }, + // String (== iso8859-1) + { XA_STRING, "text/plain;charset=iso8859-1", "STRING", 8 }, + // special for compound text + { 0, "text/plain;charset=compound_text", "COMPOUND_TEXT", 8 }, + + // PIXMAP + { XA_PIXMAP, "image/bmp", "PIXMAP", 32 } +}; + +rtl_TextEncoding x11::getTextPlainEncoding( const OUString& rMimeType ) +{ + rtl_TextEncoding aEncoding = RTL_TEXTENCODING_DONTKNOW; + OUString aMimeType( rMimeType.toAsciiLowerCase() ); + sal_Int32 nIndex = 0; + if( aMimeType.getToken( 0, ';', nIndex ) == "text/plain" ) + { + if( aMimeType.getLength() == 10 ) // only "text/plain" + aEncoding = RTL_TEXTENCODING_ISO_8859_1; + else + { + while( nIndex != -1 ) + { + OUString aToken = aMimeType.getToken( 0, ';', nIndex ); + sal_Int32 nPos = 0; + if( aToken.getToken( 0, '=', nPos ) == "charset" ) + { + OString aEncToken = OUStringToOString( aToken.getToken( 0, '=', nPos ), RTL_TEXTENCODING_ISO_8859_1 ); + aEncoding = rtl_getTextEncodingFromUnixCharset( aEncToken.getStr() ); + if( aEncoding == RTL_TEXTENCODING_DONTKNOW ) + { + if( aEncToken.equalsIgnoreAsciiCase( "utf-8" ) ) + aEncoding = RTL_TEXTENCODING_UTF8; + } + if( aEncoding != RTL_TEXTENCODING_DONTKNOW ) + break; + } + } + } + } +#if OSL_DEBUG_LEVEL > 1 + SAL_WARN_IF(aEncoding == RTL_TEXTENCODING_DONTKNOW, + "vcl.unx.dtrans", "getTextPlainEncoding( " + << rMimeType << " ) failed."); +#endif + return aEncoding; +} + +std::unordered_map< OUString, SelectionManager* >& SelectionManager::getInstances() +{ + static std::unordered_map< OUString, SelectionManager* > aInstances; + return aInstances; +} + +SelectionManager::SelectionManager() : + m_nIncrementalThreshold( 15*1024 ), + m_pDisplay( nullptr ), + m_aThread( nullptr ), + m_aDragExecuteThread( nullptr ), + m_aWindow( None ), + m_nSelectionTimeout( 0 ), + m_nSelectionTimestamp( CurrentTime ), + m_bDropEnterSent( true ), + m_aCurrentDropWindow( None ), + m_nDropTime( None ), + m_nLastDropAction( 0 ), + m_nLastX( 0 ), + m_nLastY( 0 ), + m_bDropWaitingForCompletion( false ), + m_aDropWindow( None ), + m_aDropProxy( None ), + m_aDragSourceWindow( None ), + m_nLastDragX( 0 ), + m_nLastDragY( 0 ), + m_nNoPosX( 0 ), + m_nNoPosY( 0 ), + m_nNoPosWidth( 0 ), + m_nNoPosHeight( 0 ), + m_nDragButton( 0 ), + m_nUserDragAction( 0 ), + m_nTargetAcceptAction( 0 ), + m_nSourceActions( 0 ), + m_bLastDropAccepted( false ), + m_bDropSuccess( false ), + m_bDropSent( false ), + m_nDropTimeout( 0 ), + m_bWaitingForPrimaryConversion( false ), + m_aMoveCursor( None ), + m_aCopyCursor( None ), + m_aLinkCursor( None ), + m_aNoneCursor( None ), + m_aCurrentCursor( None ), + m_nCurrentProtocolVersion( nXdndProtocolRevision ), + m_nTARGETSAtom( None ), + m_nTIMESTAMPAtom( None ), + m_nTEXTAtom( None ), + m_nINCRAtom( None ), + m_nCOMPOUNDAtom( None ), + m_nMULTIPLEAtom( None ), + m_nImageBmpAtom( None ), + m_nXdndAware( None ), + m_nXdndEnter( None ), + m_nXdndLeave( None ), + m_nXdndPosition( None ), + m_nXdndStatus( None ), + m_nXdndDrop( None ), + m_nXdndFinished( None ), + m_nXdndSelection( None ), + m_nXdndTypeList( None ), + m_nXdndProxy( None ), + m_nXdndActionCopy( None ), + m_nXdndActionMove( None ), + m_nXdndActionLink( None ), + m_nXdndActionAsk( None ), + m_bShutDown( false ) +{ + memset(&m_aDropEnterEvent, 0, sizeof(m_aDropEnterEvent)); + m_EndThreadPipe[0] = 0; + m_EndThreadPipe[1] = 0; + m_aDragRunning.reset(); +} + +Cursor SelectionManager::createCursor( const unsigned char* pPointerData, const unsigned char* pMaskData, int width, int height, int hotX, int hotY ) +{ + Pixmap aPointer; + Pixmap aMask; + XColor aBlack, aWhite; + + aBlack.pixel = BlackPixel( m_pDisplay, 0 ); + aBlack.red = aBlack.green = aBlack.blue = 0; + aBlack.flags = DoRed | DoGreen | DoBlue; + + aWhite.pixel = WhitePixel( m_pDisplay, 0 ); + aWhite.red = aWhite.green = aWhite.blue = 0xffff; + aWhite.flags = DoRed | DoGreen | DoBlue; + + aPointer = + XCreateBitmapFromData( m_pDisplay, + m_aWindow, + reinterpret_cast<const char*>(pPointerData), + width, + height ); + aMask + = XCreateBitmapFromData( m_pDisplay, + m_aWindow, + reinterpret_cast<const char*>(pMaskData), + width, + height ); + Cursor aCursor = + XCreatePixmapCursor( m_pDisplay, aPointer, aMask, + &aBlack, &aWhite, + hotX, + hotY ); + XFreePixmap( m_pDisplay, aPointer ); + XFreePixmap( m_pDisplay, aMask ); + + return aCursor; +} + +void SelectionManager::initialize( const Sequence< Any >& arguments ) +{ + osl::MutexGuard aGuard(m_aMutex); + + if( ! m_xDisplayConnection.is() ) + { + /* + * first argument must be a css::awt::XDisplayConnection + * from this we will get the XEvents of the vcl event loop by + * registering us as XEventHandler on it. + * + * implementor's note: + * FIXME: + * finally the clipboard and XDND service is back in the module it belongs + * now cleanup and sharing of resources with the normal vcl event loop + * needs to be added. The display used would be that of the normal event loop + * and synchronization should be done via the SolarMutex. + */ + if( arguments.hasElements() ) + arguments.getConstArray()[0] >>= m_xDisplayConnection; + if( ! m_xDisplayConnection.is() ) + { + } + else + m_xDisplayConnection->addEventHandler( Any(), this, ~0 ); + } + + if( ! m_pDisplay ) + { + OUString aUDisplay; + if( m_xDisplayConnection.is() ) + { + Any aIdentifier = m_xDisplayConnection->getIdentifier(); + aIdentifier >>= aUDisplay; + } + + OString aDisplayName( OUStringToOString( aUDisplay, RTL_TEXTENCODING_ISO_8859_1 ) ); + + m_pDisplay = XOpenDisplay( aDisplayName.isEmpty() ? nullptr : aDisplayName.getStr()); + + if( m_pDisplay ) + { +#ifdef SYNCHRONIZE + XSynchronize( m_pDisplay, True ); +#endif + // special targets + m_nTARGETSAtom = getAtom( "TARGETS" ); + m_nTIMESTAMPAtom = getAtom( "TIMESTAMP" ); + m_nTEXTAtom = getAtom( "TEXT" ); + m_nINCRAtom = getAtom( "INCR" ); + m_nCOMPOUNDAtom = getAtom( "COMPOUND_TEXT" ); + m_nMULTIPLEAtom = getAtom( "MULTIPLE" ); + m_nImageBmpAtom = getAtom( "image/bmp" ); + + // Atoms for Xdnd protocol + m_nXdndAware = getAtom( "XdndAware" ); + m_nXdndEnter = getAtom( "XdndEnter" ); + m_nXdndLeave = getAtom( "XdndLeave" ); + m_nXdndPosition = getAtom( "XdndPosition" ); + m_nXdndStatus = getAtom( "XdndStatus" ); + m_nXdndDrop = getAtom( "XdndDrop" ); + m_nXdndFinished = getAtom( "XdndFinished" ); + m_nXdndSelection = getAtom( "XdndSelection" ); + m_nXdndTypeList = getAtom( "XdndTypeList" ); + m_nXdndProxy = getAtom( "XdndProxy" ); + m_nXdndActionCopy = getAtom( "XdndActionCopy" ); + m_nXdndActionMove = getAtom( "XdndActionMove" ); + m_nXdndActionLink = getAtom( "XdndActionLink" ); + m_nXdndActionAsk = getAtom( "XdndActionAsk" ); + + // initialize map with member none + m_aAtomToString[ 0 ]= "None"; + m_aAtomToString[ XA_PRIMARY ] = "PRIMARY"; + + // create a (invisible) message window + m_aWindow = XCreateSimpleWindow( m_pDisplay, DefaultRootWindow( m_pDisplay ), + 10, 10, 10, 10, 0, 0, 1 ); + + // initialize threshold for incremental transfers + // ICCCM says it should be smaller that the max request size + // which in turn is guaranteed to be at least 16k bytes + m_nIncrementalThreshold = XMaxRequestSize( m_pDisplay ) - 1024; + + if( m_aWindow ) + { + // initialize default cursors + m_aMoveCursor = createCursor( movedata_curs_bits, + movedata_mask_bits, + movedata_curs_width, + movedata_curs_height, + movedata_curs_x_hot, + movedata_curs_y_hot ); + m_aCopyCursor = createCursor( copydata_curs_bits, + copydata_mask_bits, + copydata_curs_width, + copydata_curs_height, + copydata_curs_x_hot, + copydata_curs_y_hot ); + m_aLinkCursor = createCursor( linkdata_curs_bits, + linkdata_mask_bits, + linkdata_curs_width, + linkdata_curs_height, + linkdata_curs_x_hot, + linkdata_curs_y_hot ); + m_aNoneCursor = createCursor( nodrop_curs_bits, + nodrop_mask_bits, + nodrop_curs_width, + nodrop_curs_height, + nodrop_curs_x_hot, + nodrop_curs_y_hot ); + + // just interested in SelectionClear/Notify/Request and PropertyChange + XSelectInput( m_pDisplay, m_aWindow, PropertyChangeMask ); + // create the transferable for Drag operations + m_xDropTransferable = new X11Transferable( *this, m_nXdndSelection ); + registerHandler( m_nXdndSelection, *this ); + + m_aThread = osl_createSuspendedThread( call_SelectionManager_run, this ); + if( m_aThread ) + osl_resumeThread( m_aThread ); +#if OSL_DEBUG_LEVEL > 1 + else + SAL_WARN("vcl.unx.dtrans", "SelectionManager::initialize: " + << "creation of dispatch thread failed !."); +#endif + + if (pipe(m_EndThreadPipe) != 0) { +#if OSL_DEBUG_LEVEL > 1 + SAL_WARN("vcl.unx.dtrans", "Failed to create endThreadPipe."); +#endif + m_EndThreadPipe[0] = m_EndThreadPipe[1] = 0; + } + } + } + } +} + +SelectionManager::~SelectionManager() +{ +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "SelectionManager::~SelectionManager (" + << (m_pDisplay ? DisplayString(m_pDisplay) : "no display") + << ")."); +#endif + { + osl::MutexGuard aGuard( *osl::Mutex::getGlobalMutex() ); + + auto it = std::find_if(getInstances().begin(), getInstances().end(), + [&](const std::pair< OUString, SelectionManager* >& rInstance) { return rInstance.second == this; }); + if( it != getInstances().end() ) + getInstances().erase( it ); + } + + if( m_aThread ) + { + osl_terminateThread( m_aThread ); + osl_joinWithThread( m_aThread ); + osl_destroyThread( m_aThread ); + } + + if( m_aDragExecuteThread ) + { + osl_terminateThread( m_aDragExecuteThread ); + osl_joinWithThread( m_aDragExecuteThread ); + m_aDragExecuteThread = nullptr; + // thread handle is freed in dragDoDispatch() + } + + osl::MutexGuard aGuard(m_aMutex); + +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "shutting down SelectionManager."); +#endif + + if( m_pDisplay ) + { + deregisterHandler( m_nXdndSelection ); + // destroy message window + if( m_aWindow ) + XDestroyWindow( m_pDisplay, m_aWindow ); + // release cursors + if (m_aMoveCursor != None) + XFreeCursor(m_pDisplay, m_aMoveCursor); + if (m_aCopyCursor != None) + XFreeCursor(m_pDisplay, m_aCopyCursor); + if (m_aLinkCursor != None) + XFreeCursor(m_pDisplay, m_aLinkCursor); + if (m_aNoneCursor != None) + XFreeCursor(m_pDisplay, m_aNoneCursor); + + // paranoia setting, the drag thread should have + // done that already + XUngrabPointer( m_pDisplay, CurrentTime ); + XUngrabKeyboard( m_pDisplay, CurrentTime ); + + XCloseDisplay( m_pDisplay ); + } +} + +SelectionAdaptor* SelectionManager::getAdaptor( Atom selection ) +{ + std::unordered_map< Atom, Selection* >::iterator it = + m_aSelections.find( selection ); + return it != m_aSelections.end() ? it->second->m_pAdaptor : nullptr; +} + +OUString SelectionManager::convertFromCompound( const char* pText, int nLen ) +{ + osl::MutexGuard aGuard( m_aMutex ); + OUStringBuffer aRet; + if( nLen < 0 ) + nLen = strlen( pText ); + + char** pTextList = nullptr; + int nTexts = 0; + + XTextProperty aProp; + aProp.value = reinterpret_cast<unsigned char *>(const_cast<char *>(pText)); + aProp.encoding = m_nCOMPOUNDAtom; + aProp.format = 8; + aProp.nitems = nLen; + XmbTextPropertyToTextList( m_pDisplay, + &aProp, + &pTextList, + &nTexts ); + rtl_TextEncoding aEncoding = osl_getThreadTextEncoding(); + for( int i = 0; i < nTexts; i++ ) + aRet.append(OStringToOUString( pTextList[i], aEncoding )); + + if( pTextList ) + XFreeStringList( pTextList ); + + return aRet.makeStringAndClear(); +} + +OString SelectionManager::convertToCompound( const OUString& rText ) +{ + osl::MutexGuard aGuard( m_aMutex ); + XTextProperty aProp; + aProp.value = nullptr; + aProp.encoding = XA_STRING; + aProp.format = 8; + aProp.nitems = 0; + + OString aRet( rText.getStr(), rText.getLength(), osl_getThreadTextEncoding() ); + char* pT = const_cast<char*>(aRet.getStr()); + + XmbTextListToTextProperty( m_pDisplay, + &pT, + 1, + XCompoundTextStyle, + &aProp ); + if( aProp.value ) + { + aRet = reinterpret_cast<char*>(aProp.value); + XFree( aProp.value ); +#ifdef __sun + /* + * for currently unknown reasons XmbTextListToTextProperty on Solaris returns + * no data in ISO8859-n encodings (at least for n = 1, 15) + * in these encodings the directly converted text does the + * trick, also. + */ + if( aRet.isEmpty() && !rText.isEmpty() ) + aRet = OUStringToOString( rText, osl_getThreadTextEncoding() ); +#endif + } + else + aRet.clear(); + + return aRet; +} + +bool SelectionManager::convertData( + const css::uno::Reference< XTransferable >& xTransferable, + Atom nType, + Atom nSelection, + int& rFormat, + Sequence< sal_Int8 >& rData ) +{ + bool bSuccess = false; + + if( ! xTransferable.is() ) + return bSuccess; + + try + { + + DataFlavor aFlavor; + aFlavor.MimeType = convertTypeFromNative( nType, nSelection, rFormat ); + + sal_Int32 nIndex = 0; + if( aFlavor.MimeType.getToken( 0, ';', nIndex ) == "text/plain" ) + { + if( aFlavor.MimeType.getToken( 0, ';', nIndex ) == "charset=utf-16" ) + aFlavor.DataType = cppu::UnoType<OUString>::get(); + else + aFlavor.DataType = cppu::UnoType<Sequence< sal_Int8 >>::get(); + } + else + aFlavor.DataType = cppu::UnoType<Sequence< sal_Int8 >>::get(); + + if( xTransferable->isDataFlavorSupported( aFlavor ) ) + { + Any aValue( xTransferable->getTransferData( aFlavor ) ); + if( aValue.getValueTypeClass() == TypeClass_STRING ) + { + OUString aString; + aValue >>= aString; + rData = Sequence< sal_Int8 >( reinterpret_cast<sal_Int8 const *>(aString.getStr()), aString.getLength() * sizeof( sal_Unicode ) ); + bSuccess = true; + } + else if( aValue.getValueType() == cppu::UnoType<Sequence< sal_Int8 >>::get() ) + { + aValue >>= rData; + bSuccess = true; + } + } + else if( aFlavor.MimeType.startsWith("text/plain") ) + { + rtl_TextEncoding aEncoding = RTL_TEXTENCODING_DONTKNOW; + bool bCompoundText = false; + if( nType == m_nCOMPOUNDAtom ) + bCompoundText = true; + else + aEncoding = getTextPlainEncoding( aFlavor.MimeType ); + if( aEncoding != RTL_TEXTENCODING_DONTKNOW || bCompoundText ) + { + aFlavor.MimeType = "text/plain;charset=utf-16"; + aFlavor.DataType = cppu::UnoType<OUString>::get(); + if( xTransferable->isDataFlavorSupported( aFlavor ) ) + { + Any aValue( xTransferable->getTransferData( aFlavor ) ); + OUString aString; + aValue >>= aString; + OString aByteString( bCompoundText ? convertToCompound( aString ) : OUStringToOString( aString, aEncoding ) ); + rData = Sequence< sal_Int8 >( reinterpret_cast<sal_Int8 const *>(aByteString.getStr()), aByteString.getLength() * sizeof( char ) ); + bSuccess = true; + } + } + } + } + // various exceptions possible ... which all lead to a failed conversion + // so simplify here to a catch all + catch(...) + { + } + + return bSuccess; +} + +SelectionManager& SelectionManager::get( const OUString& rDisplayName ) +{ + osl::MutexGuard aGuard( *osl::Mutex::getGlobalMutex() ); + + OUString aDisplayName( rDisplayName ); + if( aDisplayName.isEmpty() ) + aDisplayName = OStringToOUString( getenv( "DISPLAY" ), RTL_TEXTENCODING_ISO_8859_1 ); + SelectionManager* pInstance = nullptr; + + std::unordered_map< OUString, SelectionManager* >::iterator it = getInstances().find( aDisplayName ); + if( it != getInstances().end() ) + pInstance = it->second; + else pInstance = getInstances()[ aDisplayName ] = new SelectionManager(); + + return *pInstance; +} + +OUString SelectionManager::getString( Atom aAtom ) +{ + osl::MutexGuard aGuard(m_aMutex); + + if( m_aAtomToString.find( aAtom ) == m_aAtomToString.end() ) + { + char* pAtom = m_pDisplay ? XGetAtomName( m_pDisplay, aAtom ) : nullptr; + if( ! pAtom ) + return OUString(); + OUString aString( OStringToOUString( pAtom, RTL_TEXTENCODING_ISO_8859_1 ) ); + XFree( pAtom ); + m_aStringToAtom[ aString ] = aAtom; + m_aAtomToString[ aAtom ] = aString; + } + return m_aAtomToString[ aAtom ]; +} + +Atom SelectionManager::getAtom( const OUString& rString ) +{ + osl::MutexGuard aGuard(m_aMutex); + + if( m_aStringToAtom.find( rString ) == m_aStringToAtom.end() ) + { + static Atom nNoDisplayAtoms = 1; + Atom aAtom = m_pDisplay ? XInternAtom( m_pDisplay, OUStringToOString( rString, RTL_TEXTENCODING_ISO_8859_1 ).getStr(), False ) : nNoDisplayAtoms++; + m_aStringToAtom[ rString ] = aAtom; + m_aAtomToString[ aAtom ] = rString; + } + return m_aStringToAtom[ rString ]; +} + +bool SelectionManager::requestOwnership( Atom selection ) +{ + bool bSuccess = false; + if( m_pDisplay && m_aWindow ) + { + osl::MutexGuard aGuard(m_aMutex); + + SelectionAdaptor* pAdaptor = getAdaptor( selection ); + if( pAdaptor ) + { + XSetSelectionOwner( m_pDisplay, selection, m_aWindow, CurrentTime ); + if( XGetSelectionOwner( m_pDisplay, selection ) == m_aWindow ) + bSuccess = true; + +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", + (bSuccess ? "acquired" : "failed to acquire") + << " ownership for selection " + << getString( selection )); +#endif + + Selection* pSel = m_aSelections[ selection ]; + pSel->m_bOwner = bSuccess; + delete pSel->m_pPixmap; + pSel->m_pPixmap = nullptr; + pSel->m_nOrigTimestamp = m_nSelectionTimestamp; + } +#if OSL_DEBUG_LEVEL > 1 + else + SAL_WARN("vcl.unx.dtrans", "no adaptor for selection " + << getString( selection )); + + if( pAdaptor->getTransferable().is() ) + { + Sequence< DataFlavor > aTypes = pAdaptor->getTransferable()->getTransferDataFlavors(); + for( int i = 0; i < aTypes.getLength(); i++ ) + { + SAL_INFO("vcl.unx.dtrans", " " << aTypes.getConstArray()[i].MimeType); + } + } +#endif + } + return bSuccess; +} + +void SelectionManager::convertTypeToNative( const OUString& rType, Atom selection, int& rFormat, ::std::list< Atom >& rConversions, bool bPushFront ) +{ + NativeTypeEntry* pTab = selection == m_nXdndSelection ? aXdndConversionTab : aNativeConversionTab; + int nTabEntries = selection == m_nXdndSelection ? SAL_N_ELEMENTS(aXdndConversionTab) : SAL_N_ELEMENTS(aNativeConversionTab); + + OString aType( OUStringToOString( rType, RTL_TEXTENCODING_ISO_8859_1 ) ); + SAL_INFO( "vcl.unx.dtrans", "convertTypeToNative " << aType ); + rFormat = 0; + for( int i = 0; i < nTabEntries; i++ ) + { + if( aType.equalsIgnoreAsciiCase( pTab[i].pType ) ) + { + if( ! pTab[i].nAtom ) + pTab[i].nAtom = getAtom( OStringToOUString( pTab[i].pNativeType, RTL_TEXTENCODING_ISO_8859_1 ) ); + rFormat = pTab[i].nFormat; + if( bPushFront ) + rConversions.push_front( pTab[i].nAtom ); + else + rConversions.push_back( pTab[i].nAtom ); + if( pTab[i].nFormat == XA_PIXMAP ) + { + if( bPushFront ) + { + rConversions.push_front( XA_VISUALID ); + rConversions.push_front( XA_COLORMAP ); + } + else + { + rConversions.push_back( XA_VISUALID ); + rConversions.push_back( XA_COLORMAP ); + } + } + } + } + if( ! rFormat ) + rFormat = 8; // byte buffer + if( bPushFront ) + rConversions.push_front( getAtom( rType ) ); + else + rConversions.push_back( getAtom( rType ) ); +}; + +void SelectionManager::getNativeTypeList( const Sequence< DataFlavor >& rTypes, std::list< Atom >& rOutTypeList, Atom targetselection ) +{ + rOutTypeList.clear(); + + int nFormat; + bool bHaveText = false; + for( const auto& rFlavor : rTypes ) + { + if( rFlavor.MimeType.startsWith("text/plain")) + bHaveText = true; + else + convertTypeToNative( rFlavor.MimeType, targetselection, nFormat, rOutTypeList ); + } + if( bHaveText ) + { + if( targetselection != m_nXdndSelection ) + { + // only mimetypes should go into Xdnd type list + rOutTypeList.push_front( XA_STRING ); + rOutTypeList.push_front( m_nCOMPOUNDAtom ); + } + convertTypeToNative( "text/plain;charset=utf-8", targetselection, nFormat, rOutTypeList, true ); + } + if( targetselection != m_nXdndSelection ) + rOutTypeList.push_back( m_nMULTIPLEAtom ); +} + +OUString SelectionManager::convertTypeFromNative( Atom nType, Atom selection, int& rFormat ) +{ + NativeTypeEntry* pTab = (selection == m_nXdndSelection) ? aXdndConversionTab : aNativeConversionTab; + int nTabEntries = (selection == m_nXdndSelection) ? SAL_N_ELEMENTS(aXdndConversionTab) : SAL_N_ELEMENTS(aNativeConversionTab); + + for( int i = 0; i < nTabEntries; i++ ) + { + if( ! pTab[i].nAtom ) + pTab[i].nAtom = getAtom( OStringToOUString( pTab[i].pNativeType, RTL_TEXTENCODING_ISO_8859_1 ) ); + if( nType == pTab[i].nAtom ) + { + rFormat = pTab[i].nFormat; + return OStringToOUString( pTab[i].pType, RTL_TEXTENCODING_ISO_8859_1 ); + } + } + rFormat = 8; + return getString( nType ); +} + +bool SelectionManager::getPasteData( Atom selection, Atom type, Sequence< sal_Int8 >& rData ) +{ + osl::ResettableMutexGuard aGuard(m_aMutex); + std::unordered_map< Atom, Selection* >::iterator it; + bool bSuccess = false; + +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "getPasteData( " << getString( selection ) + << ", native: " << getString( type ) << " )."); +#endif + + if( ! m_pDisplay ) + return false; + + it = m_aSelections.find( selection ); + if( it == m_aSelections.end() ) + return false; + + ::Window aSelectionOwner = XGetSelectionOwner( m_pDisplay, selection ); + if( aSelectionOwner == None ) + return false; + if( aSelectionOwner == m_aWindow ) + { + // probably bad timing led us here +#if OSL_DEBUG_LEVEL > 1 + SAL_WARN("vcl.unx.dtrans", "Innere Nabelschau."); +#endif + return false; + } + + // ICCCM recommends to destroy property before convert request unless + // parameters are transported; we do only in case of MULTIPLE, + // so destroy property unless target is MULTIPLE + if( type != m_nMULTIPLEAtom ) + XDeleteProperty( m_pDisplay, m_aWindow, selection ); + + XConvertSelection( m_pDisplay, selection, type, selection, m_aWindow, selection == m_nXdndSelection ? m_nDropTime : CurrentTime ); + it->second->m_eState = Selection::WaitingForResponse; + it->second->m_aRequestedType = type; + it->second->m_aData = Sequence< sal_Int8 >(); + it->second->m_aDataArrived.reset(); + // really start the request; if we don't flush the + // queue the request won't leave it because there are no more + // X calls after this until the data arrived or timeout + XFlush( m_pDisplay ); + + // do a reschedule + struct timeval tv_last, tv_current; + gettimeofday( &tv_last, nullptr ); + tv_current = tv_last; + + XEvent aEvent; + do + { + bool bAdjustTime = false; + { + bool bHandle = false; + + if( XCheckTypedEvent( m_pDisplay, + PropertyNotify, + &aEvent + ) ) + { + bHandle = true; + if( aEvent.xproperty.window == m_aWindow + && aEvent.xproperty.atom == selection ) + bAdjustTime = true; + } + else if( XCheckTypedEvent( m_pDisplay, + SelectionClear, + &aEvent + ) ) + { + bHandle = true; + } + else if( XCheckTypedEvent( m_pDisplay, + SelectionRequest, + &aEvent + ) ) + { + bHandle = true; + } + else if( XCheckTypedEvent( m_pDisplay, + SelectionNotify, + &aEvent + ) ) + { + bHandle = true; + if( aEvent.xselection.selection == selection + && ( aEvent.xselection.requestor == m_aWindow || + aEvent.xselection.requestor == m_aCurrentDropWindow ) + ) + bAdjustTime = true; + } + else + { + aGuard.clear(); + osl::Thread::wait(std::chrono::milliseconds(100)); + aGuard.reset(); + } + if( bHandle ) + { + aGuard.clear(); + handleXEvent( aEvent ); + aGuard.reset(); + } + } + gettimeofday( &tv_current, nullptr ); + if( bAdjustTime ) + tv_last = tv_current; + } while( ! it->second->m_aDataArrived.check() && (tv_current.tv_sec - tv_last.tv_sec) < getSelectionTimeout() ); + +#if OSL_DEBUG_LEVEL > 1 + SAL_WARN_IF((tv_current.tv_sec - tv_last.tv_sec) > getSelectionTimeout(), + "vcl.unx.dtrans", "timed out."); +#endif + + if( it->second->m_aDataArrived.check() && + it->second->m_aData.getLength() ) + { + rData = it->second->m_aData; + bSuccess = true; + } +#if OSL_DEBUG_LEVEL > 1 + else + SAL_WARN("vcl.unx.dtrans", "conversion unsuccessful."); +#endif + return bSuccess; +} + +bool SelectionManager::getPasteData( Atom selection, const OUString& rType, Sequence< sal_Int8 >& rData ) +{ + bool bSuccess = false; + + std::unordered_map< Atom, Selection* >::iterator it; + { + osl::MutexGuard aGuard(m_aMutex); + + it = m_aSelections.find( selection ); + if( it == m_aSelections.end() ) + return false; + } + + if( it->second->m_aTypes.getLength() == 0 ) + { + Sequence< DataFlavor > aFlavors; + getPasteDataTypes( selection, aFlavors ); + if( it->second->m_aTypes.getLength() == 0 ) + return false; + } + + const Sequence< DataFlavor >& rTypes( it->second->m_aTypes ); + const std::vector< Atom >& rNativeTypes( it->second->m_aNativeTypes ); + +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "getPasteData( \"" + << getString( selection ) + << "\", \"" + << rType << "\" )."); +#endif + + if( rType == "text/plain;charset=utf-16" ) + { + // lets see if we have UTF16 else try to find something convertible + if( it->second->m_aTypes.getLength() && ! it->second->m_bHaveUTF16 ) + { + Sequence< sal_Int8 > aData; + if( it->second->m_aUTF8Type != None && + getPasteData( selection, + it->second->m_aUTF8Type, + aData ) + ) + { + OUString aRet( reinterpret_cast<const char*>(aData.getConstArray()), aData.getLength(), RTL_TEXTENCODING_UTF8 ); + rData = Sequence< sal_Int8 >( reinterpret_cast<sal_Int8 const *>(aRet.getStr()), (aRet.getLength()+1)*sizeof( sal_Unicode ) ); + bSuccess = true; + } + else if( it->second->m_bHaveCompound && + getPasteData( selection, + m_nCOMPOUNDAtom, + aData ) + ) + { + OUString aRet( convertFromCompound( reinterpret_cast<const char*>(aData.getConstArray()), aData.getLength() ) ); + rData = Sequence< sal_Int8 >( reinterpret_cast<sal_Int8 const *>(aRet.getStr()), (aRet.getLength()+1)*sizeof( sal_Unicode ) ); + bSuccess = true; + } + else + { + for( int i = 0; i < rTypes.getLength(); i++ ) + { + rtl_TextEncoding aEncoding = getTextPlainEncoding( rTypes.getConstArray()[i].MimeType ); + if( aEncoding != RTL_TEXTENCODING_DONTKNOW && + aEncoding != RTL_TEXTENCODING_UNICODE && + getPasteData( selection, + rNativeTypes[i], + aData ) + ) + { +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "using \"" + << rTypes.getConstArray()[i].MimeType + << "\" instead of \"" + << rType + << "\"."); +#endif + + OString aConvert( reinterpret_cast<char const *>(aData.getConstArray()), aData.getLength() ); + OUString aUTF( OStringToOUString( aConvert, aEncoding ) ); + rData = Sequence< sal_Int8 >( reinterpret_cast<sal_Int8 const *>(aUTF.getStr()), (aUTF.getLength()+1)*sizeof( sal_Unicode ) ); + bSuccess = true; + break; + } + } + } + } + } + else if( rType == "image/bmp" ) + { + // #i83376# try if someone has the data in image/bmp already before + // doing the PIXMAP stuff (e.g. the Gimp has this) + bSuccess = getPasteData( selection, m_nImageBmpAtom, rData ); +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO_IF(bSuccess, "vcl.unx.dtrans", + "got " << (int) rData.getLength() << " bytes of image/bmp."); +#endif + if( ! bSuccess ) + { + Pixmap aPixmap = None; + Colormap aColormap = None; + + // prepare property for MULTIPLE request + Sequence< sal_Int8 > aData; + Atom const pTypes[4] = { XA_PIXMAP, XA_PIXMAP, XA_COLORMAP, XA_COLORMAP }; + { + osl::MutexGuard aGuard(m_aMutex); + + XChangeProperty( m_pDisplay, + m_aWindow, + selection, + XA_ATOM, + 32, + PropModeReplace, + reinterpret_cast<unsigned char const *>(pTypes), + 4 ); + } + + // try MULTIPLE request + if( getPasteData( selection, m_nMULTIPLEAtom, aData ) ) + { + Atom* pReturnedTypes = reinterpret_cast<Atom*>(aData.getArray()); + if( pReturnedTypes[0] == XA_PIXMAP && pReturnedTypes[1] == XA_PIXMAP ) + { + osl::MutexGuard aGuard(m_aMutex); + + Atom type = None; + int format = 0; + unsigned long nItems = 0; + unsigned long nBytes = 0; + unsigned char* pReturn = nullptr; + XGetWindowProperty( m_pDisplay, m_aWindow, XA_PIXMAP, 0, 1, True, XA_PIXMAP, &type, &format, &nItems, &nBytes, &pReturn ); + if( pReturn ) + { + if( type == XA_PIXMAP ) + aPixmap = *reinterpret_cast<Pixmap*>(pReturn); + XFree( pReturn ); + pReturn = nullptr; + if( pReturnedTypes[2] == XA_COLORMAP && pReturnedTypes[3] == XA_COLORMAP ) + { + XGetWindowProperty( m_pDisplay, m_aWindow, XA_COLORMAP, 0, 1, True, XA_COLORMAP, &type, &format, &nItems, &nBytes, &pReturn ); + if( pReturn ) + { + if( type == XA_COLORMAP ) + aColormap = *reinterpret_cast<Colormap*>(pReturn); + XFree( pReturn ); + } + } + } +#if OSL_DEBUG_LEVEL > 1 + else + { + SAL_WARN("vcl.unx.dtrans", "could not get PIXMAP property: type=" + << getString( type ) + << ", format=" << format + << ", items=" << nItems + << ", bytes=" << nBytes + << ", ret=0x" << pReturn); + } +#endif + } + } + + if( aPixmap == None ) + { + // perhaps two normal requests will work + if( getPasteData( selection, XA_PIXMAP, aData ) ) + { + aPixmap = *reinterpret_cast<Pixmap*>(aData.getArray()); + if( aColormap == None && getPasteData( selection, XA_COLORMAP, aData ) ) + aColormap = *reinterpret_cast<Colormap*>(aData.getArray()); + } + } + + // convert data if possible + if( aPixmap != None ) + { + osl::MutexGuard aGuard(m_aMutex); + + sal_Int32 nOutSize = 0; + sal_uInt8* pBytes = X11_getBmpFromPixmap( m_pDisplay, aPixmap, aColormap, nOutSize ); + if( pBytes ) + { + if( nOutSize ) + { + rData = Sequence< sal_Int8 >( nOutSize ); + memcpy( rData.getArray(), pBytes, nOutSize ); + bSuccess = true; + } + std::free( pBytes ); + } + } + } + } + + if( ! bSuccess ) + { + int nFormat; + ::std::list< Atom > aTypes; + convertTypeToNative( rType, selection, nFormat, aTypes ); + Atom nSelectedType = None; + for (auto const& type : aTypes) + { + for( auto const & nativeType: rNativeTypes ) + if(nativeType == type) + { + nSelectedType = type; + if (nSelectedType != None) + break; + } + } + if( nSelectedType != None ) + bSuccess = getPasteData( selection, nSelectedType, rData ); + } +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "getPasteData for selection " + << getString( selection ) + << " and data type " + << rType + << " returns " + << ( bSuccess ? "true" : "false") + << ", returned sequence has length " + << rData.getLength() << "."); +#endif + return bSuccess; +} + +bool SelectionManager::getPasteDataTypes( Atom selection, Sequence< DataFlavor >& rTypes ) +{ + std::unordered_map< Atom, Selection* >::iterator it; + { + osl::MutexGuard aGuard(m_aMutex); + + it = m_aSelections.find( selection ); + if( it != m_aSelections.end() && + it->second->m_aTypes.getLength() && + std::abs( it->second->m_nLastTimestamp - time( nullptr ) ) < 2 + ) + { + rTypes = it->second->m_aTypes; + return true; + } + } + + bool bSuccess = false; + bool bHaveUTF16 = false; + Atom aUTF8Type = None; + bool bHaveCompound = false; + Sequence< sal_Int8 > aAtoms; + + if( selection == m_nXdndSelection ) + { + // xdnd sends first three types with XdndEnter + // if more than three types are supported then the XDndTypeList + // property on the source window is used + if( m_aDropEnterEvent.data.l[0] && m_aCurrentDropWindow ) + { + if( m_aDropEnterEvent.data.l[1] & 1 ) + { + const unsigned int atomcount = 256; + // more than three types; look in property + osl::MutexGuard aGuard(m_aMutex); + + Atom nType; + int nFormat; + unsigned long nItems, nBytes; + unsigned char* pBytes = nullptr; + + XGetWindowProperty( m_pDisplay, m_aDropEnterEvent.data.l[0], + m_nXdndTypeList, 0, atomcount, False, + XA_ATOM, + &nType, &nFormat, &nItems, &nBytes, &pBytes ); +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "have " + << nItems + << " data types in XdndTypeList."); +#endif + if( nItems == atomcount && nBytes > 0 ) + { + // wow ... more than 256 types ! + aAtoms.realloc( sizeof( Atom )*atomcount+nBytes ); + memcpy( aAtoms.getArray(), pBytes, sizeof( Atom )*atomcount ); + XFree( pBytes ); + pBytes = nullptr; + XGetWindowProperty( m_pDisplay, m_aDropEnterEvent.data.l[0], + m_nXdndTypeList, atomcount, nBytes/sizeof(Atom), + False, XA_ATOM, + &nType, &nFormat, &nItems, &nBytes, &pBytes ); + { + memcpy( aAtoms.getArray()+atomcount*sizeof(Atom), pBytes, nItems*sizeof(Atom) ); + XFree( pBytes ); + } + } + else + { + aAtoms.realloc( sizeof(Atom)*nItems ); + memcpy( aAtoms.getArray(), pBytes, nItems*sizeof(Atom) ); + XFree( pBytes ); + } + } + else + { + // one to three types + int n = 0, i; + for( i = 0; i < 3; i++ ) + if( m_aDropEnterEvent.data.l[2+i] ) + n++; +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "have " + << n + << " data types in XdndEnter."); +#endif + aAtoms.realloc( sizeof(Atom)*n ); + for( i = 0, n = 0; i < 3; i++ ) + if( m_aDropEnterEvent.data.l[2+i] ) + reinterpret_cast<Atom*>(aAtoms.getArray())[n++] = m_aDropEnterEvent.data.l[2+i]; + } + } + } + // get data of type TARGETS + else if( ! getPasteData( selection, m_nTARGETSAtom, aAtoms ) ) + aAtoms = Sequence< sal_Int8 >(); + + std::vector< Atom > aNativeTypes; + if( aAtoms.hasElements() ) + { + sal_Int32 nAtoms = aAtoms.getLength() / sizeof(Atom); + Atom* pAtoms = reinterpret_cast<Atom*>(aAtoms.getArray()); + rTypes.realloc( nAtoms ); + aNativeTypes.resize( nAtoms ); + DataFlavor* pFlavors = rTypes.getArray(); + sal_Int32 nNativeTypesIndex = 0; + bool bHaveText = false; + while( nAtoms-- ) + { + SAL_INFO_IF(*pAtoms && *pAtoms < 0x01000000, "vcl.unx.dtrans", + "getPasteDataTypes: available: \"" << getString(*pAtoms) << "\""); + if( *pAtoms == m_nCOMPOUNDAtom ) + bHaveText = bHaveCompound = true; + else if( *pAtoms && *pAtoms < 0x01000000 ) + { + int nFormat; + pFlavors->MimeType = convertTypeFromNative( *pAtoms, selection, nFormat ); + pFlavors->DataType = cppu::UnoType<Sequence< sal_Int8 >>::get(); + sal_Int32 nIndex = 0; + if( pFlavors->MimeType.getToken( 0, ';', nIndex ) == "text/plain" ) + { + OUString aToken(pFlavors->MimeType.getToken( 0, ';', nIndex )); + // omit text/plain;charset=unicode since it is not well defined + if( aToken == "charset=unicode" ) + { + pAtoms++; + continue; + } + bHaveText = true; + if( aToken == "charset=utf-16" ) + { + bHaveUTF16 = true; + pFlavors->DataType = cppu::UnoType<OUString>::get(); + } + else if( aToken == "charset=utf-8" ) + { + aUTF8Type = *pAtoms; + } + } + pFlavors++; + aNativeTypes[ nNativeTypesIndex ] = *pAtoms; + nNativeTypesIndex++; + } + pAtoms++; + } + if( (pFlavors - rTypes.getArray()) < rTypes.getLength() ) + rTypes.realloc(pFlavors - rTypes.getArray()); + bSuccess = rTypes.hasElements(); + if( bHaveText && ! bHaveUTF16 ) + { + int i = 0; + + int nNewFlavors = rTypes.getLength()+1; + Sequence< DataFlavor > aTemp( nNewFlavors ); + for( i = 0; i < nNewFlavors-1; i++ ) + aTemp.getArray()[i+1] = rTypes.getConstArray()[i]; + aTemp.getArray()[0].MimeType = "text/plain;charset=utf-16"; + aTemp.getArray()[0].DataType = cppu::UnoType<OUString>::get(); + rTypes = aTemp; + + std::vector< Atom > aNativeTemp( nNewFlavors ); + for( i = 0; i < nNewFlavors-1; i++ ) + aNativeTemp[ i + 1 ] = aNativeTypes[ i ]; + aNativeTemp[0] = None; + aNativeTypes = aNativeTemp; + } + } + + { + osl::MutexGuard aGuard(m_aMutex); + + it = m_aSelections.find( selection ); + if( it != m_aSelections.end() ) + { + if( bSuccess ) + { + it->second->m_aTypes = rTypes; + it->second->m_aNativeTypes = aNativeTypes; + it->second->m_nLastTimestamp = time( nullptr ); + it->second->m_bHaveUTF16 = bHaveUTF16; + it->second->m_aUTF8Type = aUTF8Type; + it->second->m_bHaveCompound = bHaveCompound; + } + else + { + it->second->m_aTypes = Sequence< DataFlavor >(); + it->second->m_aNativeTypes = std::vector< Atom >(); + it->second->m_nLastTimestamp = 0; + it->second->m_bHaveUTF16 = false; + it->second->m_aUTF8Type = None; + it->second->m_bHaveCompound = false; + } + } + } + +#if OSL_DEBUG_LEVEL > 1 + { + SAL_INFO("vcl.unx.dtrans", "SelectionManager::getPasteDataTypes( " + << getString( selection ) + << " ) = " + << (bSuccess ? "true" : "false")); + for( int i = 0; i < rTypes.getLength(); i++ ) + SAL_INFO("vcl.unx.dtrans", "type: " << rTypes.getConstArray()[i].MimeType); + } +#endif + + return bSuccess; +} + +PixmapHolder* SelectionManager::getPixmapHolder( Atom selection ) +{ + std::unordered_map< Atom, Selection* >::const_iterator it = m_aSelections.find( selection ); + if( it == m_aSelections.end() ) + return nullptr; + if( ! it->second->m_pPixmap ) + it->second->m_pPixmap = new PixmapHolder( m_pDisplay ); + return it->second->m_pPixmap; +} + +static std::size_t GetTrueFormatSize(int nFormat) +{ + // http://mail.gnome.org/archives/wm-spec-list/2003-March/msg00067.html + return nFormat == 32 ? sizeof(long) : nFormat/8; +} + +bool SelectionManager::sendData( SelectionAdaptor* pAdaptor, + ::Window requestor, + Atom target, + Atom property, + Atom selection ) +{ + osl::ResettableMutexGuard aGuard( m_aMutex ); + + // handle targets related to image/bmp + if( target == XA_COLORMAP || target == XA_PIXMAP || target == XA_BITMAP || target == XA_VISUALID ) + { + PixmapHolder* pPixmap = getPixmapHolder( selection ); + if( ! pPixmap ) return false; + XID nValue = None; + + // handle colormap request + if( target == XA_COLORMAP ) + nValue = static_cast<XID>(pPixmap->getColormap()); + else if( target == XA_VISUALID ) + nValue = static_cast<XID>(pPixmap->getVisualID()); + else if( target == XA_PIXMAP || target == XA_BITMAP ) + { + nValue = static_cast<XID>(pPixmap->getPixmap()); + if( nValue == None ) + { + // first conversion + Sequence< sal_Int8 > aData; + int nFormat; + aGuard.clear(); + bool bConverted = convertData( pAdaptor->getTransferable(), target, selection, nFormat, aData ); + aGuard.reset(); + if( bConverted ) + { + // get pixmap again since clearing the guard could have invalidated + // the pixmap in another thread + pPixmap = getPixmapHolder( selection ); + // conversion succeeded, so aData contains image/bmp now + if( pPixmap->needsConversion( reinterpret_cast<const sal_uInt8*>(aData.getConstArray()) ) ) + { + SAL_INFO( "vcl.unx.dtrans", "trying bitmap conversion" ); + int depth = pPixmap->getDepth(); + aGuard.clear(); + aData = convertBitmapDepth(aData, depth); + aGuard.reset(); + } + // get pixmap again since clearing the guard could have invalidated + // the pixmap in another thread + pPixmap = getPixmapHolder( selection ); + nValue = static_cast<XID>(pPixmap->setBitmapData( reinterpret_cast<const sal_uInt8*>(aData.getConstArray()) )); + } + if( nValue == None ) + return false; + } + if( target == XA_BITMAP ) + nValue = static_cast<XID>(pPixmap->getBitmap()); + } + + XChangeProperty( m_pDisplay, + requestor, + property, + target, + 32, + PropModeReplace, + reinterpret_cast<const unsigned char*>(&nValue), + 1); + return true; + } + + /* + * special target TEXT allows us to transfer + * the data in an encoding of our choice + * COMPOUND_TEXT will work with most applications + */ + if( target == m_nTEXTAtom ) + target = m_nCOMPOUNDAtom; + + Sequence< sal_Int8 > aData; + int nFormat; + aGuard.clear(); + bool bConverted = convertData( pAdaptor->getTransferable(), target, selection, nFormat, aData ); + aGuard.reset(); + if( bConverted ) + { + // conversion succeeded + if( aData.getLength() > m_nIncrementalThreshold ) + { +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "using INCR protocol."); + std::unordered_map< ::Window, std::unordered_map< Atom, IncrementalTransfer > >::const_iterator win_it = m_aIncrementals.find( requestor ); + if( win_it != m_aIncrementals.end() ) + { + std::unordered_map< Atom, IncrementalTransfer >::const_iterator inc_it = win_it->second.find( property ); + if( inc_it != win_it->second.end() ) + { + const IncrementalTransfer& rInc = inc_it->second; + SAL_INFO("vcl.unx.dtrans", "premature end and new start for INCR transfer for window " + << std::showbase << std::hex + << rInc.m_aRequestor + << ", property " + << getString( rInc.m_aProperty ) + << ", type " + << getString( rInc.m_aTarget )); + } + } +#endif + + // insert IncrementalTransfer + IncrementalTransfer& rInc = m_aIncrementals[ requestor ][ property ]; + rInc.m_aData = aData; + rInc.m_nBufferPos = 0; + rInc.m_aRequestor = requestor; + rInc.m_aProperty = property; + rInc.m_aTarget = target; + rInc.m_nFormat = nFormat; + rInc.m_nTransferStartTime = time( nullptr ); + + // use incr protocol, signal start to requestor + long nMinSize = m_nIncrementalThreshold; + XSelectInput( m_pDisplay, requestor, PropertyChangeMask ); + XChangeProperty( m_pDisplay, requestor, property, + m_nINCRAtom, 32, PropModeReplace, reinterpret_cast<unsigned char*>(&nMinSize), 1 ); + XFlush( m_pDisplay ); + } + else + { + std::size_t nUnitSize = GetTrueFormatSize(nFormat); + XChangeProperty( m_pDisplay, + requestor, + property, + target, + nFormat, + PropModeReplace, + reinterpret_cast<const unsigned char*>(aData.getConstArray()), + aData.getLength()/nUnitSize ); + } + } +#if OSL_DEBUG_LEVEL > 1 + else + SAL_WARN("vcl.unx.dtrans", "convertData failed for type: " + << convertTypeFromNative( target, selection, nFormat )); +#endif + return bConverted; +} + +bool SelectionManager::handleSelectionRequest( XSelectionRequestEvent& rRequest ) +{ + osl::ResettableMutexGuard aGuard( m_aMutex ); + +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "handleSelectionRequest for selection " + << getString( rRequest.selection ) + << " and target " + << getString( rRequest.target )); +#endif + + XEvent aNotify; + + aNotify.type = SelectionNotify; + aNotify.xselection.display = rRequest.display; + aNotify.xselection.send_event = True; + aNotify.xselection.requestor = rRequest.requestor; + aNotify.xselection.selection = rRequest.selection; + aNotify.xselection.time = rRequest.time; + aNotify.xselection.target = rRequest.target; + aNotify.xselection.property = None; + + SelectionAdaptor* pAdaptor = getAdaptor( rRequest.selection ); + // ensure that we still own that selection + if( pAdaptor && + XGetSelectionOwner( m_pDisplay, rRequest.selection ) == m_aWindow ) + { + css::uno::Reference< XTransferable > xTrans( pAdaptor->getTransferable() ); + if( rRequest.target == m_nTARGETSAtom ) + { + // someone requests our types + if( xTrans.is() ) + { + aGuard.clear(); + Sequence< DataFlavor > aFlavors = xTrans->getTransferDataFlavors(); + aGuard.reset(); + + ::std::list< Atom > aConversions; + getNativeTypeList( aFlavors, aConversions, rRequest.selection ); + + int i, nTypes = aConversions.size(); + Atom* pTypes = static_cast<Atom*>(alloca( nTypes * sizeof( Atom ) )); + std::list< Atom >::const_iterator it; + for( i = 0, it = aConversions.begin(); i < nTypes; i++, ++it ) + pTypes[i] = *it; + XChangeProperty( m_pDisplay, rRequest.requestor, rRequest.property, + XA_ATOM, 32, PropModeReplace, reinterpret_cast<unsigned char*>(pTypes), nTypes ); + aNotify.xselection.property = rRequest.property; +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "sending type list:"); + for( int k = 0; k < nTypes; k++ ) + SAL_INFO("vcl.unx.dtrans", " " + << (pTypes[k] ? XGetAtomName( m_pDisplay, pTypes[k] ) : + "<None>")); +#endif + } + } + else if( rRequest.target == m_nTIMESTAMPAtom ) + { + long nTimeStamp = static_cast<long>(m_aSelections[rRequest.selection]->m_nOrigTimestamp); + XChangeProperty( m_pDisplay, rRequest.requestor, rRequest.property, + XA_INTEGER, 32, PropModeReplace, reinterpret_cast<unsigned char*>(&nTimeStamp), 1 ); + aNotify.xselection.property = rRequest.property; +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "sending timestamp: " << (int)nTimeStamp); +#endif + } + else + { + bool bEventSuccess = false; + if( rRequest.target == m_nMULTIPLEAtom ) + { + // get all targets + Atom nType = None; + int nFormat = 0; + unsigned long nItems = 0, nBytes = 0; + unsigned char* pData = nullptr; + + // get number of atoms + XGetWindowProperty( m_pDisplay, + rRequest.requestor, + rRequest.property, + 0, 0, + False, + AnyPropertyType, + &nType, &nFormat, + &nItems, &nBytes, + &pData ); + if( nFormat == 32 && nBytes/4 ) + { + if( pData ) // ?? should not happen + { + XFree( pData ); + pData = nullptr; + } + XGetWindowProperty( m_pDisplay, + rRequest.requestor, + rRequest.property, + 0, nBytes/4, + False, + nType, + &nType, &nFormat, + &nItems, &nBytes, + &pData ); + if( pData && nItems ) + { +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "found " + << nItems + << " atoms in MULTIPLE request."); +#endif + bEventSuccess = true; + bool bResetAtoms = false; + Atom* pAtoms = reinterpret_cast<Atom*>(pData); + aGuard.clear(); + for( unsigned long i = 0; i < nItems; i += 2 ) + { +#if OSL_DEBUG_LEVEL > 1 + std::ostringstream oss; + oss << " " + << getString( pAtoms[i] ) + << " => " + << getString( pAtoms[i+1] ) + << ": "; +#endif + + bool bSuccess = sendData( pAdaptor, rRequest.requestor, pAtoms[i], pAtoms[i+1], rRequest.selection ); +#if OSL_DEBUG_LEVEL > 1 + oss << (bSuccess ? "succeeded" : "failed"); + SAL_INFO("vcl.unx.dtrans", oss.str()); +#endif + if( ! bSuccess ) + { + pAtoms[i] = None; + bResetAtoms = true; + } + } + aGuard.reset(); + if( bResetAtoms ) + XChangeProperty( m_pDisplay, + rRequest.requestor, + rRequest.property, + XA_ATOM, + 32, + PropModeReplace, + pData, + nBytes/4 ); + } + if( pData ) + XFree( pData ); + } +#if OSL_DEBUG_LEVEL > 1 + else + { + std::ostringstream oss; + oss << "could not get type list from \"" + << getString( rRequest.property ) + << "\" of type \"" + << getString( nType ) + << "\" on requestor " + << std::showbase << std::hex + << rRequest.requestor + << ", requestor has properties:"; + + int nProps = 0; + Atom* pProps = XListProperties( m_pDisplay, rRequest.requestor, &nProps ); + if( pProps ) + { + for( int i = 0; i < nProps; i++ ) + oss << " \"" << getString( pProps[i]) << "\""; + XFree( pProps ); + } + SAL_INFO("vcl.unx.dtrans", oss.str()); + } +#endif + } + else + { + aGuard.clear(); + bEventSuccess = sendData( pAdaptor, rRequest.requestor, rRequest.target, rRequest.property, rRequest.selection ); + aGuard.reset(); + } + if( bEventSuccess ) + { + aNotify.xselection.target = rRequest.target; + aNotify.xselection.property = rRequest.property; + } + } + aGuard.clear(); + xTrans.clear(); + aGuard.reset(); + } + XSendEvent( m_pDisplay, rRequest.requestor, False, 0, &aNotify ); + + if( rRequest.selection == XA_PRIMARY && + m_bWaitingForPrimaryConversion && + m_xDragSourceListener.is() ) + { + DragSourceDropEvent dsde; + dsde.Source = static_cast< OWeakObject* >(this); + dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, *this ); + dsde.DragSource = static_cast< XDragSource* >(this); + if( aNotify.xselection.property != None ) + { + dsde.DropAction = DNDConstants::ACTION_COPY; + dsde.DropSuccess = true; + } + else + { + dsde.DropAction = DNDConstants::ACTION_NONE; + dsde.DropSuccess = false; + } + css::uno::Reference< XDragSourceListener > xListener( m_xDragSourceListener ); + m_xDragSourceListener.clear(); + aGuard.clear(); + if( xListener.is() ) + xListener->dragDropEnd( dsde ); + } + + // we handled the event in any case by answering + return true; +} + +bool SelectionManager::handleReceivePropertyNotify( XPropertyEvent const & rNotify ) +{ + osl::MutexGuard aGuard( m_aMutex ); + // data we requested arrived +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "handleReceivePropertyNotify for property " + << getString( rNotify.atom )); +#endif + bool bHandled = false; + + std::unordered_map< Atom, Selection* >::iterator it = + m_aSelections.find( rNotify.atom ); + if( it != m_aSelections.end() && + rNotify.state == PropertyNewValue && + ( it->second->m_eState == Selection::WaitingForResponse || + it->second->m_eState == Selection::WaitingForData || + it->second->m_eState == Selection::IncrementalTransfer + ) + ) + { + // MULTIPLE requests are only complete after selection notify + if( it->second->m_aRequestedType == m_nMULTIPLEAtom && + ( it->second->m_eState == Selection::WaitingForResponse || + it->second->m_eState == Selection::WaitingForData ) ) + return false; + + bHandled = true; + + Atom nType = None; + int nFormat = 0; + unsigned long nItems = 0, nBytes = 0; + unsigned char* pData = nullptr; + + // get type and length + XGetWindowProperty( m_pDisplay, + rNotify.window, + rNotify.atom, + 0, 0, + False, + AnyPropertyType, + &nType, &nFormat, + &nItems, &nBytes, + &pData ); +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "found " + << nBytes + << " bytes data of type " + << getString( nType ) + << " and format " + << nFormat + << ", items = " + << nItems); +#endif + if( pData ) + { + XFree( pData ); + pData = nullptr; + } + + if( nType == m_nINCRAtom ) + { + // start data transfer + XDeleteProperty( m_pDisplay, rNotify.window, rNotify.atom ); + it->second->m_eState = Selection::IncrementalTransfer; + } + else if( nType != None ) + { + XGetWindowProperty( m_pDisplay, + rNotify.window, + rNotify.atom, + 0, nBytes/4 +1, + True, + nType, + &nType, &nFormat, + &nItems, &nBytes, + &pData ); + +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "read " + << nItems + << " items data of type " + << getString( nType ) + << " and format " + << nFormat + << ", " + << nBytes + << " bytes left in property."); +#endif + + std::size_t nUnitSize = GetTrueFormatSize(nFormat); + + if( it->second->m_eState == Selection::WaitingForData || + it->second->m_eState == Selection::WaitingForResponse ) + { + // copy data + it->second->m_aData = Sequence< sal_Int8 >( reinterpret_cast<sal_Int8*>(pData), nItems*nUnitSize ); + it->second->m_eState = Selection::Inactive; + it->second->m_aDataArrived.set(); + } + else if( it->second->m_eState == Selection::IncrementalTransfer ) + { + if( nItems ) + { + // append data + Sequence< sal_Int8 > aData( it->second->m_aData.getLength() + nItems*nUnitSize ); + memcpy( aData.getArray(), it->second->m_aData.getArray(), it->second->m_aData.getLength() ); + memcpy( aData.getArray() + it->second->m_aData.getLength(), pData, nItems*nUnitSize ); + it->second->m_aData = aData; + } + else + { + it->second->m_eState = Selection::Inactive; + it->second->m_aDataArrived.set(); + } + } + if( pData ) + XFree( pData ); + } + else if( it->second->m_eState == Selection::IncrementalTransfer ) + { + it->second->m_eState = Selection::Inactive; + it->second->m_aDataArrived.set(); + } + } + return bHandled; +} + +bool SelectionManager::handleSendPropertyNotify( XPropertyEvent const & rNotify ) +{ + osl::MutexGuard aGuard( m_aMutex ); + + // ready for next part of an IncrementalTransfer +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "handleSendPropertyNotify for property " + << getString( rNotify.atom ) + << " (" + << (rNotify.state == PropertyNewValue ? + "new value" : + (rNotify.state == PropertyDelete ? + "deleted" : + "unknown")) + << ")."); +#endif + + bool bHandled = false; + // feed incrementals + if( rNotify.state == PropertyDelete ) + { + auto it = m_aIncrementals.find( rNotify.window ); + if( it != m_aIncrementals.end() ) + { + bHandled = true; + int nCurrentTime = time( nullptr ); + // throw out aborted transfers + std::vector< Atom > aTimeouts; + for (auto const& incrementalTransfer : it->second) + { + if( (nCurrentTime - incrementalTransfer.second.m_nTransferStartTime) > (getSelectionTimeout()+2) ) + { + aTimeouts.push_back( incrementalTransfer.first ); +#if OSL_DEBUG_LEVEL > 1 + const IncrementalTransfer& rInc = incrementalTransfer.second; + SAL_INFO("vcl.unx.dtrans", + "timeout on INCR transfer for window " + << std::showbase << std::hex + << rInc.m_aRequestor + << ", property " + << getString( rInc.m_aProperty ) + << ", type " + << getString( rInc.m_aTarget )); +#endif + } + } + + for (auto const& timeout : aTimeouts) + { + // transfer broken, might even be a new client with the + // same window id + it->second.erase( timeout ); + } + aTimeouts.clear(); + + auto inc_it = it->second.find( rNotify.atom ); + if( inc_it != it->second.end() ) + { + IncrementalTransfer& rInc = inc_it->second; + + int nBytes = rInc.m_aData.getLength() - rInc.m_nBufferPos; + nBytes = std::min(nBytes, m_nIncrementalThreshold); + if( nBytes < 0 ) // sanity check + nBytes = 0; +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "pushing " + << nBytes + << " bytes: \"" + << std::setw(std::min(nBytes, 32)) + << ((const unsigned char*) + rInc.m_aData.getConstArray()+rInc.m_nBufferPos) + << "\"..."); +#endif + std::size_t nUnitSize = GetTrueFormatSize(rInc.m_nFormat); + + XChangeProperty( m_pDisplay, + rInc.m_aRequestor, + rInc.m_aProperty, + rInc.m_aTarget, + rInc.m_nFormat, + PropModeReplace, + reinterpret_cast<const unsigned char*>(rInc.m_aData.getConstArray())+rInc.m_nBufferPos, + nBytes/nUnitSize ); + rInc.m_nBufferPos += nBytes; + rInc.m_nTransferStartTime = nCurrentTime; + + if( nBytes == 0 ) // transfer finished + { +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "finished INCR transfer for " + << "window " + << std::showbase << std::hex + << rInc.m_aRequestor + << ", property " + << getString( rInc.m_aProperty ) + << ", type " + << getString( rInc.m_aTarget )); +#endif + it->second.erase( inc_it ); + } + + } + // eventually clean up the hash map + if( it->second.empty() ) + m_aIncrementals.erase( it ); + } + } + return bHandled; +} + +bool SelectionManager::handleSelectionNotify( XSelectionEvent const & rNotify ) +{ + osl::MutexGuard aGuard( m_aMutex ); + + bool bHandled = false; + + // notification about success/failure of one of our conversion requests +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "handleSelectionNotify for selection " + << getString( rNotify.selection ) + << " and property " + << (rNotify.property ? getString( rNotify.property ) : "None") + << " (" + << std::showbase << std::hex + << rNotify.property + << ")."); + SAL_WARN_IF(rNotify.requestor != m_aWindow && + rNotify.requestor != m_aCurrentDropWindow, + "vcl.unx.dtrans", "selection notify for unknown window " + << std::showbase << std::hex + << rNotify.requestor); +#endif + std::unordered_map< Atom, Selection* >::iterator it = + m_aSelections.find( rNotify.selection ); + if ( + (rNotify.requestor == m_aWindow || rNotify.requestor == m_aCurrentDropWindow) && + it != m_aSelections.end() && + ( + (it->second->m_eState == Selection::WaitingForResponse) || + (it->second->m_eState == Selection::WaitingForData) + ) + ) + { + bHandled = true; + if( it->second->m_aRequestedType == m_nMULTIPLEAtom ) + { + Atom nType = None; + int nFormat = 0; + unsigned long nItems = 0, nBytes = 0; + unsigned char* pData = nullptr; + + // get type and length + XGetWindowProperty( m_pDisplay, + rNotify.requestor, + rNotify.property, + 0, 256, + False, + AnyPropertyType, + &nType, &nFormat, + &nItems, &nBytes, + &pData ); + if( nBytes ) // HUGE request !!! + { + if( pData ) + XFree( pData ); + XGetWindowProperty( m_pDisplay, + rNotify.requestor, + rNotify.property, + 0, 256+(nBytes+3)/4, + False, + AnyPropertyType, + &nType, &nFormat, + &nItems, &nBytes, + &pData ); + } + it->second->m_eState = Selection::Inactive; + std::size_t nUnitSize = GetTrueFormatSize(nFormat); + it->second->m_aData = Sequence< sal_Int8 >(reinterpret_cast<sal_Int8*>(pData), nItems * nUnitSize); + it->second->m_aDataArrived.set(); + if( pData ) + XFree( pData ); + } + // WaitingForData can actually happen; some + // applications (e.g. cmdtool on Solaris) first send + // a success and then cancel it. Weird ! + else if( rNotify.property == None ) + { + // conversion failed, stop transfer + it->second->m_eState = Selection::Inactive; + it->second->m_aData = Sequence< sal_Int8 >(); + it->second->m_aDataArrived.set(); + } + // get the bytes, by INCR if necessary + else + it->second->m_eState = Selection::WaitingForData; + } +#if OSL_DEBUG_LEVEL > 1 + else if( it != m_aSelections.end() ) + SAL_WARN("vcl.unx.dtrans", "selection in state " << it->second->m_eState); +#endif + return bHandled; +} + +bool SelectionManager::handleDropEvent( XClientMessageEvent const & rMessage ) +{ + osl::ResettableMutexGuard aGuard(m_aMutex); + + // handle drop related events + ::Window aSource = rMessage.data.l[0]; + ::Window aTarget = rMessage.window; + + bool bHandled = false; + + std::unordered_map< ::Window, DropTargetEntry >::iterator it = + m_aDropTargets.find( aTarget ); + +#if OSL_DEBUG_LEVEL > 1 + if( rMessage.message_type == m_nXdndEnter || + rMessage.message_type == m_nXdndLeave || + rMessage.message_type == m_nXdndDrop || + rMessage.message_type == m_nXdndPosition ) + { + std::ostringstream oss; + oss << "got drop event " + << getString( rMessage.message_type ) + << ", "; + + if( it == m_aDropTargets.end() ) + oss << "but no target found."; + else if( ! it->second.m_pTarget->m_bActive ) + oss << "but target is inactive."; + else if( m_aDropEnterEvent.data.l[0] != None && (::Window)m_aDropEnterEvent.data.l[0] != aSource ) + oss << "but source " + << std::showbase << std::hex + << aSource + << " is unknown (expected " + << m_aDropEnterEvent.data.l[0] + << " or 0)."; + else + oss << "processing."; + SAL_INFO("vcl.unx.dtrans", oss.str()); + } +#endif + + if( it != m_aDropTargets.end() && it->second.m_pTarget->m_bActive && + m_bDropWaitingForCompletion && m_aDropEnterEvent.data.l[0] ) + { + bHandled = true; + OSL_FAIL( "someone forgot to call dropComplete ?" ); + // some listener forgot to call dropComplete in the last operation + // let us end it now and accept the new enter event + aGuard.clear(); + dropComplete( false, m_aCurrentDropWindow ); + aGuard.reset(); + } + + if( it != m_aDropTargets.end() && + it->second.m_pTarget->m_bActive && + ( m_aDropEnterEvent.data.l[0] == None || ::Window(m_aDropEnterEvent.data.l[0]) == aSource ) + ) + { + if( rMessage.message_type == m_nXdndEnter ) + { + bHandled = true; + m_aDropEnterEvent = rMessage; + m_bDropEnterSent = false; + m_aCurrentDropWindow = aTarget; + m_nCurrentProtocolVersion = m_aDropEnterEvent.data.l[1] >> 24; +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "received XdndEnter on " + << std::showbase << std::hex + << aTarget); +#endif + } + else if( + rMessage.message_type == m_nXdndPosition && + aSource == ::Window(m_aDropEnterEvent.data.l[0]) + ) + { + bHandled = true; + m_nDropTime = m_nCurrentProtocolVersion > 0 ? rMessage.data.l[3] : CurrentTime; + + ::Window aChild; + XTranslateCoordinates( m_pDisplay, + it->second.m_aRootWindow, + it->first, + rMessage.data.l[2] >> 16, + rMessage.data.l[2] & 0xffff, + &m_nLastX, &m_nLastY, + &aChild ); +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "received XdndPosition on " + << std::showbase << std::hex + << aTarget + << " (" + << std::dec + << m_nLastX + << ", " + << m_nLastY + << ")."); +#endif + DropTargetDragEnterEvent aEvent; + aEvent.Source = static_cast< XDropTarget* >(it->second.m_pTarget); + aEvent.Context = new DropTargetDragContext( m_aCurrentDropWindow, *this ); + aEvent.LocationX = m_nLastX; + aEvent.LocationY = m_nLastY; + aEvent.SourceActions = m_nSourceActions; + if( m_nCurrentProtocolVersion < 2 ) + aEvent.DropAction = DNDConstants::ACTION_COPY; + else if( Atom(rMessage.data.l[4]) == m_nXdndActionCopy ) + aEvent.DropAction = DNDConstants::ACTION_COPY; + else if( Atom(rMessage.data.l[4]) == m_nXdndActionMove ) + aEvent.DropAction = DNDConstants::ACTION_MOVE; + else if( Atom(rMessage.data.l[4]) == m_nXdndActionLink ) + aEvent.DropAction = DNDConstants::ACTION_LINK; + else if( Atom(rMessage.data.l[4]) == m_nXdndActionAsk ) + // currently no interface to implement ask + aEvent.DropAction = ~0; + else + aEvent.DropAction = DNDConstants::ACTION_NONE; + + m_nLastDropAction = aEvent.DropAction; + if( ! m_bDropEnterSent ) + { + m_bDropEnterSent = true; + aEvent.SupportedDataFlavors = m_xDropTransferable->getTransferDataFlavors(); + aGuard.clear(); + it->second->dragEnter( aEvent ); + } + else + { + aGuard.clear(); + it->second->dragOver( aEvent ); + } + } + else if( + rMessage.message_type == m_nXdndLeave && + aSource == ::Window(m_aDropEnterEvent.data.l[0]) + ) + { + bHandled = true; +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "received XdndLeave on " + << std::showbase << std::hex + << aTarget); +#endif + DropTargetEvent aEvent; + aEvent.Source = static_cast< XDropTarget* >(it->second.m_pTarget); + m_aDropEnterEvent.data.l[0] = None; + if( m_aCurrentDropWindow == aTarget ) + m_aCurrentDropWindow = None; + m_nCurrentProtocolVersion = nXdndProtocolRevision; + aGuard.clear(); + it->second->dragExit( aEvent ); + } + else if( + rMessage.message_type == m_nXdndDrop && + aSource == ::Window(m_aDropEnterEvent.data.l[0]) + ) + { + bHandled = true; + m_nDropTime = m_nCurrentProtocolVersion > 0 ? rMessage.data.l[2] : CurrentTime; +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "received XdndDrop on " + << std::showbase << std::hex + << aTarget + << " (" + << m_nLastX + << ", " + << m_nLastY + << ")."); +#endif + if( m_bLastDropAccepted ) + { + DropTargetDropEvent aEvent; + aEvent.Source = static_cast< XDropTarget* >(it->second.m_pTarget); + aEvent.Context = new DropTargetDropContext( m_aCurrentDropWindow, *this ); + aEvent.LocationX = m_nLastX; + aEvent.LocationY = m_nLastY; + aEvent.DropAction = m_nLastDropAction; + // there is nothing corresponding to source supported actions + // every source can do link, copy and move + aEvent.SourceActions= m_nLastDropAction; + aEvent.Transferable = m_xDropTransferable; + + m_bDropWaitingForCompletion = true; + aGuard.clear(); + it->second->drop( aEvent ); + } + else + { +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "XdndDrop canceled due to " + << "m_bLastDropAccepted = false." ); +#endif + DropTargetEvent aEvent; + aEvent.Source = static_cast< XDropTarget* >(it->second.m_pTarget); + aGuard.clear(); + it->second->dragExit( aEvent ); + // reset the drop status, notify source + dropComplete( false, m_aCurrentDropWindow ); + } + } + } + return bHandled; +} + +/* + * methods for XDropTargetDropContext + */ + +void SelectionManager::dropComplete( bool bSuccess, ::Window aDropWindow ) +{ + osl::ClearableMutexGuard aGuard(m_aMutex); + + if( aDropWindow == m_aCurrentDropWindow ) + { + if( m_xDragSourceListener.is() ) + { + DragSourceDropEvent dsde; + dsde.Source = static_cast< OWeakObject* >(this); + dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, *this ); + dsde.DragSource = static_cast< XDragSource* >(this); + dsde.DropAction = getUserDragAction(); + dsde.DropSuccess = bSuccess; + css::uno::Reference< XDragSourceListener > xListener = m_xDragSourceListener; + m_xDragSourceListener.clear(); + + aGuard.clear(); + xListener->dragDropEnd( dsde ); + } + else if( m_aDropEnterEvent.data.l[0] && m_aCurrentDropWindow ) + { + XEvent aEvent; + aEvent.xclient.type = ClientMessage; + aEvent.xclient.display = m_pDisplay; + aEvent.xclient.window = m_aDropEnterEvent.data.l[0]; + aEvent.xclient.message_type = m_nXdndFinished; + aEvent.xclient.format = 32; + aEvent.xclient.data.l[0] = m_aCurrentDropWindow; + aEvent.xclient.data.l[1] = bSuccess ? 1 : 0; + aEvent.xclient.data.l[2] = 0; + aEvent.xclient.data.l[3] = 0; + aEvent.xclient.data.l[4] = 0; + if( bSuccess ) + { + if( m_nLastDropAction & DNDConstants::ACTION_MOVE ) + aEvent.xclient.data.l[2] = m_nXdndActionMove; + else if( m_nLastDropAction & DNDConstants::ACTION_COPY ) + aEvent.xclient.data.l[2] = m_nXdndActionCopy; + else if( m_nLastDropAction & DNDConstants::ACTION_LINK ) + aEvent.xclient.data.l[2] = m_nXdndActionLink; + } +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "Sending XdndFinished to " + << std::showbase << std::hex + << m_aDropEnterEvent.data.l[0]); +#endif + XSendEvent( m_pDisplay, m_aDropEnterEvent.data.l[0], + False, NoEventMask, & aEvent ); + + m_aDropEnterEvent.data.l[0] = None; + m_aCurrentDropWindow = None; + m_nCurrentProtocolVersion = nXdndProtocolRevision; + } + m_bDropWaitingForCompletion = false; + } + else + OSL_FAIL( "dropComplete from invalid DropTargetDropContext" ); +} + +/* + * methods for XDropTargetDragContext + */ + +void SelectionManager::sendDragStatus( Atom nDropAction ) +{ + osl::ClearableMutexGuard aGuard(m_aMutex); + + if( m_xDragSourceListener.is() ) + { + sal_Int8 nNewDragAction; + if( nDropAction == m_nXdndActionMove ) + nNewDragAction = DNDConstants::ACTION_MOVE; + else if( nDropAction == m_nXdndActionCopy ) + nNewDragAction = DNDConstants::ACTION_COPY; + else if( nDropAction == m_nXdndActionLink ) + nNewDragAction = DNDConstants::ACTION_LINK; + else + nNewDragAction = DNDConstants::ACTION_NONE; + nNewDragAction &= m_nSourceActions; + + if( nNewDragAction != m_nTargetAcceptAction ) + { + setCursor( getDefaultCursor( nNewDragAction ), m_aDropWindow ); + m_nTargetAcceptAction = nNewDragAction; + } + + DragSourceDragEvent dsde; + dsde.Source = static_cast< OWeakObject* >(this); + dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, *this ); + dsde.DragSource = static_cast< XDragSource* >(this); + dsde.DropAction = m_nSourceActions; + dsde.UserAction = getUserDragAction(); + + css::uno::Reference< XDragSourceListener > xListener( m_xDragSourceListener ); + // caution: do not change anything after this + aGuard.clear(); + if( xListener.is() ) + xListener->dragOver( dsde ); + } + else if( m_aDropEnterEvent.data.l[0] && m_aCurrentDropWindow ) + { + XEvent aEvent; + aEvent.xclient.type = ClientMessage; + aEvent.xclient.display = m_pDisplay; + aEvent.xclient.window = m_aDropEnterEvent.data.l[0]; + aEvent.xclient.message_type = m_nXdndStatus; + aEvent.xclient.format = 32; + aEvent.xclient.data.l[0] = m_aCurrentDropWindow; + aEvent.xclient.data.l[1] = 2; + if( nDropAction == m_nXdndActionMove || + nDropAction == m_nXdndActionLink || + nDropAction == m_nXdndActionCopy ) + aEvent.xclient.data.l[1] |= 1; + aEvent.xclient.data.l[2] = 0; + aEvent.xclient.data.l[3] = 0; + aEvent.xclient.data.l[4] = m_nCurrentProtocolVersion > 1 ? nDropAction : 0; + +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "Sending XdndStatus to " + << std::showbase << std::hex + << m_aDropEnterEvent.data.l[0] + << " with action " + << getString( nDropAction )); +#endif + + XSendEvent( m_pDisplay, m_aDropEnterEvent.data.l[0], + False, NoEventMask, & aEvent ); + XFlush( m_pDisplay ); + } +} + +sal_Int8 SelectionManager::getUserDragAction() const +{ + return (m_nTargetAcceptAction != DNDConstants::ACTION_DEFAULT) ? m_nTargetAcceptAction : m_nUserDragAction; +} + +bool SelectionManager::updateDragAction( int modifierState ) +{ + bool bRet = false; + + sal_Int8 nNewDropAction = DNDConstants::ACTION_MOVE; + if( ( modifierState & ShiftMask ) && ! ( modifierState & ControlMask ) ) + nNewDropAction = DNDConstants::ACTION_MOVE; + else if( ( modifierState & ControlMask ) && ! ( modifierState & ShiftMask ) ) + nNewDropAction = DNDConstants::ACTION_COPY; + else if( ( modifierState & ShiftMask ) && ( modifierState & ControlMask ) ) + nNewDropAction = DNDConstants::ACTION_LINK; + if( m_nCurrentProtocolVersion < 0 && m_aDropWindow != None ) + nNewDropAction = DNDConstants::ACTION_COPY; + nNewDropAction &= m_nSourceActions; + + if( ! ( modifierState & ( ControlMask | ShiftMask ) ) ) + { + if( ! nNewDropAction ) + { + // default to an action so the user does not have to press + // keys explicitly + if( m_nSourceActions & DNDConstants::ACTION_MOVE ) + nNewDropAction = DNDConstants::ACTION_MOVE; + else if( m_nSourceActions & DNDConstants::ACTION_COPY ) + nNewDropAction = DNDConstants::ACTION_COPY; + else if( m_nSourceActions & DNDConstants::ACTION_LINK ) + nNewDropAction = DNDConstants::ACTION_LINK; + } + nNewDropAction |= DNDConstants::ACTION_DEFAULT; + } + + if( nNewDropAction != m_nUserDragAction || m_nTargetAcceptAction != DNDConstants::ACTION_DEFAULT ) + { +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "updateDragAction: " + << std::hex + << (int)m_nUserDragAction + << " -> " + << (int)nNewDropAction); +#endif + bRet = true; + m_nUserDragAction = nNewDropAction; + + DragSourceDragEvent dsde; + dsde.Source = static_cast< OWeakObject* >(this); + dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, *this ); + dsde.DragSource = static_cast< XDragSource* >(this); + dsde.DropAction = m_nUserDragAction; + dsde.UserAction = m_nUserDragAction; + m_nTargetAcceptAction = DNDConstants::ACTION_DEFAULT; // invalidate last accept + m_xDragSourceListener->dropActionChanged( dsde ); + } + return bRet; +} + +void SelectionManager::sendDropPosition( bool bForce, Time eventTime ) +{ + osl::ClearableMutexGuard aGuard(m_aMutex); + + if( m_bDropSent ) + return; + + std::unordered_map< ::Window, DropTargetEntry >::const_iterator it = + m_aDropTargets.find( m_aDropWindow ); + if( it != m_aDropTargets.end() ) + { + if( it->second.m_pTarget->m_bActive ) + { + int x, y; + ::Window aChild; + XTranslateCoordinates( m_pDisplay, it->second.m_aRootWindow, m_aDropWindow, m_nLastDragX, m_nLastDragY, &x, &y, &aChild ); + DropTargetDragEvent dtde; + dtde.Source = static_cast< OWeakObject* >(it->second.m_pTarget ); + dtde.Context = new DropTargetDragContext( m_aCurrentDropWindow, *this ); + dtde.LocationX = x; + dtde.LocationY = y; + dtde.DropAction = getUserDragAction(); + dtde.SourceActions = m_nSourceActions; + aGuard.clear(); + it->second->dragOver( dtde ); + } + } + else if( bForce || + + m_nLastDragX < m_nNoPosX || m_nLastDragX >= m_nNoPosX+m_nNoPosWidth || + m_nLastDragY < m_nNoPosY || m_nLastDragY >= m_nNoPosY+m_nNoPosHeight + ) + { + // send XdndPosition + XEvent aEvent; + aEvent.type = ClientMessage; + aEvent.xclient.display = m_pDisplay; + aEvent.xclient.format = 32; + aEvent.xclient.message_type = m_nXdndPosition; + aEvent.xclient.window = m_aDropWindow; + aEvent.xclient.data.l[0] = m_aWindow; + aEvent.xclient.data.l[1] = 0; + aEvent.xclient.data.l[2] = m_nLastDragX << 16 | (m_nLastDragY&0xffff); + aEvent.xclient.data.l[3] = eventTime; + + if( m_nUserDragAction & DNDConstants::ACTION_COPY ) + aEvent.xclient.data.l[4]=m_nXdndActionCopy; + else if( m_nUserDragAction & DNDConstants::ACTION_MOVE ) + aEvent.xclient.data.l[4]=m_nXdndActionMove; + else if( m_nUserDragAction & DNDConstants::ACTION_LINK ) + aEvent.xclient.data.l[4]=m_nXdndActionLink; + else + aEvent.xclient.data.l[4]=m_nXdndActionCopy; + XSendEvent( m_pDisplay, m_aDropProxy, False, NoEventMask, &aEvent ); + m_nNoPosX = m_nNoPosY = m_nNoPosWidth = m_nNoPosHeight = 0; + } +} + +bool SelectionManager::handleDragEvent( XEvent const & rMessage ) +{ + if( ! m_xDragSourceListener.is() ) + return false; + + osl::ResettableMutexGuard aGuard(m_aMutex); + + bool bHandled = false; + + // for shortcut + std::unordered_map< ::Window, DropTargetEntry >::const_iterator it = + m_aDropTargets.find( m_aDropWindow ); + +#if OSL_DEBUG_LEVEL > 1 + switch( rMessage.type ) + { + case ClientMessage: + SAL_INFO("vcl.unx.dtrans", "handleDragEvent: " + << getString( rMessage.xclient.message_type )); + break; + case MotionNotify: + break; + case EnterNotify: + SAL_INFO("vcl.unx.dtrans", "handleDragEvent: EnterNotify."); + break; + case LeaveNotify: + SAL_INFO("vcl.unx.dtrans", "handleDragEvent: LeaveNotify."); + break; + case ButtonPress: + SAL_INFO("vcl.unx.dtrans", "handleDragEvent: ButtonPress " + << rMessage.xbutton.button + << " (m_nDragButton = " + << m_nDragButton + << ")."); + break; + case ButtonRelease: + SAL_INFO("vcl.unx.dtrans", "handleDragEvent: ButtonRelease " + << rMessage.xbutton.button + << " (m_nDragButton = " + << m_nDragButton + << ")."); + break; + case KeyPress: + SAL_INFO("vcl.unx.dtrans", "handleDragEvent: KeyPress."); + break; + case KeyRelease: + SAL_INFO("vcl.unx.dtrans", "handleDragEvent: KeyRelease."); + break; + default: + SAL_INFO("vcl.unx.dtrans", "handleDragEvent: <unknown type " + << rMessage.type + << ">."); + break; + } +#endif + + // handle drag related events + if( rMessage.type == ClientMessage ) + { + if( rMessage.xclient.message_type == m_nXdndStatus && Atom(rMessage.xclient.data.l[0]) == m_aDropWindow ) + { + bHandled = true; + DragSourceDragEvent dsde; + dsde.Source = static_cast< OWeakObject* >(this); + dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, *this ); + dsde.DragSource = static_cast< XDragSource* >( this ); + dsde.UserAction = getUserDragAction(); + dsde.DropAction = DNDConstants::ACTION_NONE; + m_bDropSuccess = (rMessage.xclient.data.l[1] & 1) != 0; +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "status drop action: accept = " + << (m_bDropSuccess ? "true" : "false") + << ", " + << getString( rMessage.xclient.data.l[4] )); +#endif + if( rMessage.xclient.data.l[1] & 1 ) + { + if( m_nCurrentProtocolVersion > 1 ) + { + if( Atom(rMessage.xclient.data.l[4]) == m_nXdndActionCopy ) + dsde.DropAction = DNDConstants::ACTION_COPY; + else if( Atom(rMessage.xclient.data.l[4]) == m_nXdndActionMove ) + dsde.DropAction = DNDConstants::ACTION_MOVE; + else if( Atom(rMessage.xclient.data.l[4]) == m_nXdndActionLink ) + dsde.DropAction = DNDConstants::ACTION_LINK; + } + else + dsde.DropAction = DNDConstants::ACTION_COPY; + } + m_nTargetAcceptAction = dsde.DropAction; + + if( ! ( rMessage.xclient.data.l[1] & 2 ) ) + { + m_nNoPosX = rMessage.xclient.data.l[2] >> 16; + m_nNoPosY = rMessage.xclient.data.l[2] & 0xffff; + m_nNoPosWidth = rMessage.xclient.data.l[3] >> 16; + m_nNoPosHeight = rMessage.xclient.data.l[3] & 0xffff; + } + else + m_nNoPosX = m_nNoPosY = m_nNoPosWidth = m_nNoPosHeight = 0; + + setCursor( getDefaultCursor( dsde.DropAction ), m_aDropWindow ); + aGuard.clear(); + m_xDragSourceListener->dragOver( dsde ); + } + else if( rMessage.xclient.message_type == m_nXdndFinished && m_aDropWindow == Atom(rMessage.xclient.data.l[0]) ) + { + bHandled = true; + // notify the listener + DragSourceDropEvent dsde; + dsde.Source = static_cast< OWeakObject* >(this); + dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, *this ); + dsde.DragSource = static_cast< XDragSource* >(this); + dsde.DropAction = m_nTargetAcceptAction; + dsde.DropSuccess = m_bDropSuccess; + css::uno::Reference< XDragSourceListener > xListener( m_xDragSourceListener ); + m_xDragSourceListener.clear(); + aGuard.clear(); + xListener->dragDropEnd( dsde ); + } + } + else if( rMessage.type == MotionNotify || + rMessage.type == EnterNotify || rMessage.type == LeaveNotify + ) + { + bHandled = true; + bool bForce = false; + int root_x = rMessage.type == MotionNotify ? rMessage.xmotion.x_root : rMessage.xcrossing.x_root; + int root_y = rMessage.type == MotionNotify ? rMessage.xmotion.y_root : rMessage.xcrossing.y_root; + ::Window root = rMessage.type == MotionNotify ? rMessage.xmotion.root : rMessage.xcrossing.root; + + aGuard.clear(); + if( rMessage.type == MotionNotify ) + { + bForce = updateDragAction( rMessage.xmotion.state ); + } + updateDragWindow( root_x, root_y, root ); + aGuard.reset(); + + if( m_nCurrentProtocolVersion >= 0 && m_aDropProxy != None ) + { + aGuard.clear(); + sendDropPosition( bForce, rMessage.type == MotionNotify ? rMessage.xmotion.time : rMessage.xcrossing.time ); + } + } + else if( rMessage.type == KeyPress || rMessage.type == KeyRelease ) + { + bHandled = true; + KeySym aKey = XkbKeycodeToKeysym( m_pDisplay, rMessage.xkey.keycode, 0, 0 ); + if( aKey == XK_Escape ) + { + // abort drag + if( it != m_aDropTargets.end() ) + { + DropTargetEvent dte; + dte.Source = static_cast< OWeakObject* >( it->second.m_pTarget ); + aGuard.clear(); + it->second.m_pTarget->dragExit( dte ); + aGuard.reset(); + } + else if( m_aDropProxy != None && m_nCurrentProtocolVersion >= 0 ) + { + // send XdndLeave + XEvent aEvent; + aEvent.type = ClientMessage; + aEvent.xclient.display = m_pDisplay; + aEvent.xclient.format = 32; + aEvent.xclient.message_type = m_nXdndLeave; + aEvent.xclient.window = m_aDropWindow; + aEvent.xclient.data.l[0] = m_aWindow; + memset( aEvent.xclient.data.l+1, 0, sizeof(long)*4); + m_aDropWindow = m_aDropProxy = None; + XSendEvent( m_pDisplay, m_aDropProxy, False, NoEventMask, &aEvent ); + } + // notify the listener + DragSourceDropEvent dsde; + dsde.Source = static_cast< OWeakObject* >(this); + dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, *this ); + dsde.DragSource = static_cast< XDragSource* >(this); + dsde.DropAction = DNDConstants::ACTION_NONE; + dsde.DropSuccess = false; + css::uno::Reference< XDragSourceListener > xListener( m_xDragSourceListener ); + m_xDragSourceListener.clear(); + aGuard.clear(); + xListener->dragDropEnd( dsde ); + } + else + { + /* + * man page says: state is state immediate PRIOR to the + * event. It would seem that this is a somewhat arguable + * design decision. + */ + int nState = rMessage.xkey.state; + int nNewState = 0; + switch( aKey ) + { + case XK_Shift_R: + case XK_Shift_L: nNewState = ShiftMask;break; + case XK_Control_R: + case XK_Control_L: nNewState = ControlMask;break; + // just interested in shift and ctrl for dnd + } + if( rMessage.type == KeyPress ) + nState |= nNewState; + else + nState &= ~nNewState; + aGuard.clear(); + if( updateDragAction( nState ) ) + sendDropPosition( true, rMessage.xkey.time ); + } + } + else if( + ( rMessage.type == ButtonPress || rMessage.type == ButtonRelease ) && + rMessage.xbutton.button == m_nDragButton ) + { + bool bCancel = true; + if( m_aDropWindow != None ) + { + if( it != m_aDropTargets.end() ) + { + if( it->second.m_pTarget->m_bActive && m_nUserDragAction != DNDConstants::ACTION_NONE && m_bLastDropAccepted ) + { + bHandled = true; + int x, y; + ::Window aChild; + XTranslateCoordinates( m_pDisplay, rMessage.xbutton.root, m_aDropWindow, rMessage.xbutton.x_root, rMessage.xbutton.y_root, &x, &y, &aChild ); + DropTargetDropEvent dtde; + dtde.Source = static_cast< OWeakObject* >(it->second.m_pTarget ); + dtde.Context = new DropTargetDropContext( m_aCurrentDropWindow, *this ); + dtde.LocationX = x; + dtde.LocationY = y; + dtde.DropAction = m_nUserDragAction; + dtde.SourceActions = m_nSourceActions; + dtde.Transferable = m_xDragSourceTransferable; + m_bDropSent = true; + m_nDropTimeout = time( nullptr ); + m_bDropWaitingForCompletion = true; + aGuard.clear(); + it->second->drop( dtde ); + bCancel = false; + } + else bCancel = true; + } + else if( m_nCurrentProtocolVersion >= 0 ) + { + bHandled = true; + + XEvent aEvent; + aEvent.type = ClientMessage; + aEvent.xclient.display = m_pDisplay; + aEvent.xclient.format = 32; + aEvent.xclient.message_type = m_nXdndDrop; + aEvent.xclient.window = m_aDropWindow; + aEvent.xclient.data.l[0] = m_aWindow; + aEvent.xclient.data.l[1] = 0; + aEvent.xclient.data.l[2] = rMessage.xbutton.time; + aEvent.xclient.data.l[3] = 0; + aEvent.xclient.data.l[4] = 0; + + m_bDropSent = true; + m_nDropTimeout = time( nullptr ); + XSendEvent( m_pDisplay, m_aDropProxy, False, NoEventMask, &aEvent ); + bCancel = false; + } + else + { + // dropping on non XdndWindows: acquire ownership of + // PRIMARY and send a middle mouse button click down/up to + // target window + SelectionAdaptor* pAdaptor = getAdaptor( XA_PRIMARY ); + if( pAdaptor ) + { + bHandled = true; + + ::Window aDummy; + XEvent aEvent; + aEvent.type = ButtonPress; + aEvent.xbutton.display = m_pDisplay; + aEvent.xbutton.window = m_aDropWindow; + aEvent.xbutton.root = rMessage.xbutton.root; + aEvent.xbutton.subwindow = m_aDropWindow; + aEvent.xbutton.time = rMessage.xbutton.time+1; + aEvent.xbutton.x_root = rMessage.xbutton.x_root; + aEvent.xbutton.y_root = rMessage.xbutton.y_root; + aEvent.xbutton.state = rMessage.xbutton.state; + aEvent.xbutton.button = Button2; + aEvent.xbutton.same_screen = True; + XTranslateCoordinates( m_pDisplay, + rMessage.xbutton.root, m_aDropWindow, + rMessage.xbutton.x_root, rMessage.xbutton.y_root, + &aEvent.xbutton.x, &aEvent.xbutton.y, + &aDummy ); + XSendEvent( m_pDisplay, m_aDropWindow, False, ButtonPressMask, &aEvent ); + aEvent.xbutton.type = ButtonRelease; + aEvent.xbutton.time++; + aEvent.xbutton.state |= Button2Mask; + XSendEvent( m_pDisplay, m_aDropWindow, False, ButtonReleaseMask, &aEvent ); + + m_bDropSent = true; + m_nDropTimeout = time( nullptr ); + XSendEvent( m_pDisplay, m_aDropProxy, False, NoEventMask, &aEvent ); + m_bWaitingForPrimaryConversion = true; + m_bDropSent = true; + m_nDropTimeout = time( nullptr ); + // HACK :-) + aGuard.clear(); + static_cast< X11Clipboard* >( pAdaptor )->setContents( m_xDragSourceTransferable, css::uno::Reference< css::datatransfer::clipboard::XClipboardOwner >() ); + aGuard.reset(); + bCancel = false; + } + } + } + if( bCancel ) + { + // cancel drag + DragSourceDropEvent dsde; + dsde.Source = static_cast< OWeakObject* >(this); + dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, *this ); + dsde.DragSource = static_cast< XDragSource* >(this); + dsde.DropAction = DNDConstants::ACTION_NONE; + dsde.DropSuccess = false; + css::uno::Reference< XDragSourceListener > xListener( m_xDragSourceListener ); + m_xDragSourceListener.clear(); + aGuard.clear(); + xListener->dragDropEnd( dsde ); + bHandled = true; + } + } + return bHandled; +} + +void SelectionManager::accept( sal_Int8 dragOperation, ::Window aDropWindow ) +{ + if( aDropWindow == m_aCurrentDropWindow ) + { +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "accept: " << std::hex << dragOperation); +#endif + Atom nAction = None; + dragOperation &= (DNDConstants::ACTION_MOVE | DNDConstants::ACTION_COPY | DNDConstants::ACTION_LINK); + if( dragOperation & DNDConstants::ACTION_MOVE ) + nAction = m_nXdndActionMove; + else if( dragOperation & DNDConstants::ACTION_COPY ) + nAction = m_nXdndActionCopy; + else if( dragOperation & DNDConstants::ACTION_LINK ) + nAction = m_nXdndActionLink; + m_bLastDropAccepted = true; + sendDragStatus( nAction ); + } +} + +void SelectionManager::reject( ::Window aDropWindow ) +{ + if( aDropWindow == m_aCurrentDropWindow ) + { +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "reject."); +#endif + m_bLastDropAccepted = false; + sendDragStatus( None ); + if( m_bDropSent && m_xDragSourceListener.is() ) + { + DragSourceDropEvent dsde; + dsde.Source = static_cast< OWeakObject* >(this); + dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, *this ); + dsde.DragSource = static_cast< XDragSource* >(this); + dsde.DropAction = DNDConstants::ACTION_NONE; + dsde.DropSuccess = false; + m_xDragSourceListener->dragDropEnd( dsde ); + m_xDragSourceListener.clear(); + } + } +} + +/* + * XDragSource + */ + +sal_Bool SelectionManager::isDragImageSupported() +{ + return false; +} + +sal_Int32 SelectionManager::getDefaultCursor( sal_Int8 dragAction ) +{ + Cursor aCursor = m_aNoneCursor; + if( dragAction & DNDConstants::ACTION_MOVE ) + aCursor = m_aMoveCursor; + else if( dragAction & DNDConstants::ACTION_COPY ) + aCursor = m_aCopyCursor; + else if( dragAction & DNDConstants::ACTION_LINK ) + aCursor = m_aLinkCursor; + return aCursor; +} + +int SelectionManager::getXdndVersion( ::Window aWindow, ::Window& rProxy ) +{ + Atom* pProperties = nullptr; + int nProperties = 0; + Atom nType; + int nFormat; + unsigned long nItems, nBytes; + unsigned char* pBytes = nullptr; + + int nVersion = -1; + rProxy = None; + + /* + * XListProperties is used here to avoid unnecessary XGetWindowProperty calls + * and therefore reducing latency penalty + */ + pProperties = XListProperties( m_pDisplay, aWindow, &nProperties ); + // first look for proxy + int i; + for( i = 0; i < nProperties; i++ ) + { + if( pProperties[i] == m_nXdndProxy ) + { + XGetWindowProperty( m_pDisplay, aWindow, m_nXdndProxy, 0, 1, False, XA_WINDOW, + &nType, &nFormat, &nItems, &nBytes, &pBytes ); + if( pBytes ) + { + if( nType == XA_WINDOW ) + rProxy = *reinterpret_cast< ::Window* >(pBytes); + XFree( pBytes ); + pBytes = nullptr; + if( rProxy != None ) + { + // now check proxy whether it points to itself + XGetWindowProperty( m_pDisplay, rProxy, m_nXdndProxy, 0, 1, False, XA_WINDOW, + &nType, &nFormat, &nItems, &nBytes, &pBytes ); + if( pBytes ) + { + if( nType == XA_WINDOW && *reinterpret_cast< ::Window* >(pBytes) != rProxy ) + rProxy = None; + XFree( pBytes ); + pBytes = nullptr; + } + else + rProxy = None; + } + } + break; + } + } + if ( pProperties ) + XFree (pProperties); + + ::Window aAwareWindow = rProxy != None ? rProxy : aWindow; + + XGetWindowProperty( m_pDisplay, aAwareWindow, m_nXdndAware, 0, 1, False, XA_ATOM, + &nType, &nFormat, &nItems, &nBytes, &pBytes ); + if( pBytes ) + { + if( nType == XA_ATOM ) + nVersion = *reinterpret_cast<Atom*>(pBytes); + XFree( pBytes ); + } + + nVersion = std::min<int>(nVersion, nXdndProtocolRevision); + + return nVersion; +} + +void SelectionManager::updateDragWindow( int nX, int nY, ::Window aRoot ) +{ + osl::ResettableMutexGuard aGuard( m_aMutex ); + + css::uno::Reference< XDragSourceListener > xListener( m_xDragSourceListener ); + + m_nLastDragX = nX; + m_nLastDragY = nY; + + ::Window aParent = aRoot; + ::Window aChild; + ::Window aNewProxy = None, aNewCurrentWindow = None; + int nNewProtocolVersion = -1; + int nWinX, nWinY; + + // find the first XdndAware window or check if root window is + // XdndAware or has XdndProxy + do + { + XTranslateCoordinates( m_pDisplay, aRoot, aParent, nX, nY, &nWinX, &nWinY, &aChild ); + if( aChild != None ) + { + if( aChild == m_aCurrentDropWindow && aChild != aRoot && m_nCurrentProtocolVersion >= 0 ) + { + aParent = aChild; + break; + } + nNewProtocolVersion = getXdndVersion( aChild, aNewProxy ); + aParent = aChild; + } + } while( aChild != None && nNewProtocolVersion < 0 ); + + aNewCurrentWindow = aParent; + if( aNewCurrentWindow == aRoot ) + { + // no children, try root drop + nNewProtocolVersion = getXdndVersion( aNewCurrentWindow, aNewProxy ); + if( nNewProtocolVersion < 3 ) + { + aNewCurrentWindow = aNewProxy = None; + nNewProtocolVersion = nXdndProtocolRevision; + } + } + + DragSourceDragEvent dsde; + dsde.Source = static_cast< OWeakObject* >(this); + dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, *this ); + dsde.DragSource = static_cast< XDragSource* >(this); + dsde.DropAction = nNewProtocolVersion >= 0 ? m_nUserDragAction : DNDConstants::ACTION_COPY; + dsde.UserAction = nNewProtocolVersion >= 0 ? m_nUserDragAction : DNDConstants::ACTION_COPY; + + std::unordered_map< ::Window, DropTargetEntry >::const_iterator it; + if( aNewCurrentWindow != m_aDropWindow ) + { +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "drag left window " + << std::showbase << std::hex + << m_aDropWindow + << std::dec + << " (rev. " + << m_nCurrentProtocolVersion + << "), entered window " + << std::showbase << std::hex + << aNewCurrentWindow + << " (rev " + << std::dec + << nNewProtocolVersion + << ")."); +#endif + if( m_aDropWindow != None ) + { + it = m_aDropTargets.find( m_aDropWindow ); + if( it != m_aDropTargets.end() ) + // shortcut for own drop targets + { + DropTargetEvent dte; + dte.Source = static_cast< OWeakObject* >( it->second.m_pTarget ); + aGuard.clear(); + it->second.m_pTarget->dragExit( dte ); + aGuard.reset(); + } + else + { + // send old drop target a XdndLeave + XEvent aEvent; + aEvent.type = ClientMessage; + aEvent.xclient.display = m_pDisplay; + aEvent.xclient.format = 32; + aEvent.xclient.message_type = m_nXdndLeave; + aEvent.xclient.window = m_aDropWindow; + aEvent.xclient.data.l[0] = m_aWindow; + aEvent.xclient.data.l[1] = 0; + XSendEvent( m_pDisplay, m_aDropProxy, False, NoEventMask, &aEvent ); + } + if( xListener.is() ) + { + aGuard.clear(); + xListener->dragExit( dsde ); + aGuard.reset(); + } + } + + m_nCurrentProtocolVersion = nNewProtocolVersion; + m_aDropWindow = aNewCurrentWindow; + m_aDropProxy = aNewProxy != None ? aNewProxy : m_aDropWindow; + + it = m_aDropTargets.find( m_aDropWindow ); + if( it != m_aDropTargets.end() && ! it->second.m_pTarget->m_bActive ) + m_aDropProxy = None; + + if( m_aDropProxy != None && xListener.is() ) + { + aGuard.clear(); + xListener->dragEnter( dsde ); + aGuard.reset(); + } + // send XdndEnter + if( m_aDropProxy != None && m_nCurrentProtocolVersion >= 0 ) + { + it = m_aDropTargets.find( m_aDropWindow ); + if( it != m_aDropTargets.end() ) + { + XTranslateCoordinates( m_pDisplay, aRoot, m_aDropWindow, nX, nY, &nWinX, &nWinY, &aChild ); + DropTargetDragEnterEvent dtde; + dtde.Source = static_cast< OWeakObject* >( it->second.m_pTarget ); + dtde.Context = new DropTargetDragContext( m_aCurrentDropWindow, *this ); + dtde.LocationX = nWinX; + dtde.LocationY = nWinY; + dtde.DropAction = m_nUserDragAction; + dtde.SourceActions = m_nSourceActions; + dtde.SupportedDataFlavors = m_xDragSourceTransferable->getTransferDataFlavors(); + aGuard.clear(); + it->second.m_pTarget->dragEnter( dtde ); + aGuard.reset(); + } + else + { + XEvent aEvent; + aEvent.type = ClientMessage; + aEvent.xclient.display = m_pDisplay; + aEvent.xclient.format = 32; + aEvent.xclient.message_type = m_nXdndEnter; + aEvent.xclient.window = m_aDropWindow; + aEvent.xclient.data.l[0] = m_aWindow; + aEvent.xclient.data.l[1] = m_nCurrentProtocolVersion << 24; + memset( aEvent.xclient.data.l + 2, 0, sizeof( long )*3 ); + // fill in data types + ::std::list< Atom > aConversions; + getNativeTypeList( m_aDragFlavors, aConversions, m_nXdndSelection ); + if( aConversions.size() > 3 ) + aEvent.xclient.data.l[1] |= 1; + ::std::list< Atom >::const_iterator type_it = aConversions.begin(); + for( int i = 0; type_it != aConversions.end() && i < 3; i++, ++type_it ) + aEvent.xclient.data.l[i+2] = *type_it; + XSendEvent( m_pDisplay, m_aDropProxy, False, NoEventMask, &aEvent ); + } + } + m_nNoPosX = m_nNoPosY = m_nNoPosWidth = m_nNoPosHeight = 0; + } + else if( m_aDropProxy != None && xListener.is() ) + { + aGuard.clear(); + // drag over for XdndAware windows comes when receiving XdndStatus + xListener->dragOver( dsde ); + } +} + +void SelectionManager::startDrag( + const DragGestureEvent& trigger, + sal_Int8 sourceActions, + sal_Int32, + sal_Int32, + const css::uno::Reference< XTransferable >& transferable, + const css::uno::Reference< XDragSourceListener >& listener + ) +{ +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "startDrag( sourceActions = " + << std::hex + << (int)sourceActions + << " )."); +#endif + DragSourceDropEvent aDragFailedEvent; + aDragFailedEvent.Source = static_cast< OWeakObject* >(this); + aDragFailedEvent.DragSource = static_cast< XDragSource* >(this); + aDragFailedEvent.DragSourceContext = new DragSourceContext( None, *this ); + aDragFailedEvent.DropAction = DNDConstants::ACTION_NONE; + aDragFailedEvent.DropSuccess = false; + + if( m_aDragRunning.check() ) + { + if( listener.is() ) + listener->dragDropEnd( aDragFailedEvent ); + +#if OSL_DEBUG_LEVEL > 1 + SAL_WARN("vcl.unx.dtrans", + "*** ERROR *** second drag and drop started."); + if( m_xDragSourceListener.is() ) + SAL_WARN("vcl.unx.dtrans", + "*** ERROR *** drag source listener already set."); + else + SAL_WARN("vcl.unx.dtrans", + "*** ERROR *** drag thread already running."); +#endif + return; + } + + SalFrame* pCaptureFrame = nullptr; + + { + osl::ClearableMutexGuard aGuard(m_aMutex); + + // first get the current pointer position and the window that + // the pointer is located in. since said window should be one + // of our DropTargets at the time of executeDrag we can use + // them for a start + ::Window aRoot, aParent, aChild; + int root_x(0), root_y(0), win_x(0), win_y(0); + unsigned int mask(0); + + bool bPointerFound = false; + for (auto const& dropTarget : m_aDropTargets) + { + if( XQueryPointer( m_pDisplay, dropTarget.second.m_aRootWindow, + &aRoot, &aParent, + &root_x, &root_y, + &win_x, &win_y, + &mask ) ) + { + aParent = dropTarget.second.m_aRootWindow; + aRoot = aParent; + bPointerFound = true; + break; + } + } + + // don't start DnD if there is none of our windows on the same screen as + // the pointer or if no mouse button is pressed + if( !bPointerFound || (mask & (Button1Mask|Button2Mask|Button3Mask)) == 0 ) + { + aGuard.clear(); + if( listener.is() ) + listener->dragDropEnd( aDragFailedEvent ); + return; + } + + // try to find which of our drop targets is the drag source + // if that drop target is deregistered we should stop executing + // the drag (actually this is a poor substitute for an "endDrag" + // method ). + m_aDragSourceWindow = None; + do + { + XTranslateCoordinates( m_pDisplay, aRoot, aParent, root_x, root_y, &win_x, &win_y, &aChild ); + if( aChild != None && m_aDropTargets.find( aChild ) != m_aDropTargets.end() ) + { + m_aDragSourceWindow = aChild; +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "found drag source window " + << std::showbase << std::hex + << m_aDragSourceWindow); +#endif + break; + } + aParent = aChild; + } while( aChild != None ); + +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "try to grab pointer ..."); +#endif + int nPointerGrabSuccess = + XGrabPointer( m_pDisplay, aRoot, True, + DRAG_EVENT_MASK, + GrabModeAsync, GrabModeAsync, + None, + None, + CurrentTime ); + /* if we could not grab the pointer here, there is a chance + that the pointer is grabbed by the other vcl display (the main loop) + so let's break that grab and reset it later + + remark: this whole code should really be molten into normal vcl so only + one display is used... + */ + if( nPointerGrabSuccess != GrabSuccess ) + { + comphelper::SolarMutex& rSolarMutex( Application::GetSolarMutex() ); + if( rSolarMutex.tryToAcquire() ) + { + pCaptureFrame = vcl_sal::getSalDisplay(GetGenericUnixSalData())->GetCaptureFrame(); + if( pCaptureFrame ) + { + vcl_sal::getSalDisplay(GetGenericUnixSalData())->CaptureMouse( nullptr ); + nPointerGrabSuccess = + XGrabPointer( m_pDisplay, aRoot, True, + DRAG_EVENT_MASK, + GrabModeAsync, GrabModeAsync, + None, + None, + CurrentTime ); + } + } + } +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "... grabbed pointer: " + << nPointerGrabSuccess); + SAL_INFO("vcl.unx.dtrans", "try to grab keyboard ..."); +#endif + int nKeyboardGrabSuccess = + XGrabKeyboard( m_pDisplay, aRoot, True, + GrabModeAsync, GrabModeAsync, CurrentTime ); +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "... grabbed keyboard: " + << nKeyboardGrabSuccess); +#endif + if( nPointerGrabSuccess != GrabSuccess || nKeyboardGrabSuccess != GrabSuccess ) + { + if( nPointerGrabSuccess == GrabSuccess ) + XUngrabPointer( m_pDisplay, CurrentTime ); + if( nKeyboardGrabSuccess == GrabSuccess ) + XUngrabKeyboard( m_pDisplay, CurrentTime ); + XFlush( m_pDisplay ); + aGuard.clear(); + if( listener.is() ) + listener->dragDropEnd( aDragFailedEvent ); + if( pCaptureFrame ) + { + comphelper::SolarMutex& rSolarMutex( Application::GetSolarMutex() ); + if( rSolarMutex.tryToAcquire() ) + vcl_sal::getSalDisplay(GetGenericUnixSalData())->CaptureMouse( pCaptureFrame ); +#if OSL_DEBUG_LEVEL > 0 + else + OSL_FAIL( "failed to acquire SolarMutex to reset capture frame" ); +#endif + } + return; + } + + m_xDragSourceTransferable = transferable; + m_xDragSourceListener = listener; + m_aDragFlavors = transferable->getTransferDataFlavors(); + m_aCurrentCursor = None; + + requestOwnership( m_nXdndSelection ); + + ::std::list< Atom > aConversions; + getNativeTypeList( m_aDragFlavors, aConversions, m_nXdndSelection ); + + Atom* pTypes = static_cast<Atom*>(alloca( sizeof(Atom)*aConversions.size() )); + int nTypes = 0; + for (auto const& conversion : aConversions) + pTypes[nTypes++] = conversion; + + XChangeProperty( m_pDisplay, m_aWindow, m_nXdndTypeList, XA_ATOM, 32, PropModeReplace, reinterpret_cast<unsigned char*>(pTypes), nTypes ); + + m_nSourceActions = sourceActions | DNDConstants::ACTION_DEFAULT; + m_nUserDragAction = DNDConstants::ACTION_MOVE & m_nSourceActions; + if( ! m_nUserDragAction ) + m_nUserDragAction = DNDConstants::ACTION_COPY & m_nSourceActions; + if( ! m_nUserDragAction ) + m_nUserDragAction = DNDConstants::ACTION_LINK & m_nSourceActions; + m_nTargetAcceptAction = DNDConstants::ACTION_DEFAULT; + m_bDropSent = false; + m_bDropSuccess = false; + m_bWaitingForPrimaryConversion = false; + m_nDragButton = Button1; // default to left button + css::awt::MouseEvent aEvent; + if( trigger.Event >>= aEvent ) + { + if( aEvent.Buttons & MouseButton::LEFT ) + m_nDragButton = Button1; + else if( aEvent.Buttons & MouseButton::RIGHT ) + m_nDragButton = Button3; + else if( aEvent.Buttons & MouseButton::MIDDLE ) + m_nDragButton = Button2; + } +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "m_nUserDragAction = " + << std::hex + << (int)m_nUserDragAction); +#endif + updateDragWindow( root_x, root_y, aRoot ); + m_nUserDragAction = ~0; + updateDragAction( mask ); + } + + m_aDragRunning.set(); + m_aDragExecuteThread = osl_createSuspendedThread( call_SelectionManager_runDragExecute, this ); + if( m_aDragExecuteThread ) + osl_resumeThread( m_aDragExecuteThread ); + else + { +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "osl_createSuspendedThread failed " + << "for drag execute."); +#endif + m_xDragSourceListener.clear(); + m_xDragSourceTransferable.clear(); + + m_bDropSent = false; + m_bDropSuccess = false; + m_bWaitingForPrimaryConversion = false; + m_aDropWindow = None; + m_aDropProxy = None; + m_nCurrentProtocolVersion = nXdndProtocolRevision; + m_nNoPosX = 0; + m_nNoPosY = 0; + m_nNoPosWidth = 0; + m_nNoPosHeight = 0; + m_aCurrentCursor = None; + + XUngrabPointer( m_pDisplay, CurrentTime ); + XUngrabKeyboard( m_pDisplay, CurrentTime ); + XFlush( m_pDisplay ); + + if( pCaptureFrame ) + { + comphelper::SolarMutex& rSolarMutex( Application::GetSolarMutex() ); + if( rSolarMutex.tryToAcquire() ) + vcl_sal::getSalDisplay(GetGenericUnixSalData())->CaptureMouse( pCaptureFrame ); +#if OSL_DEBUG_LEVEL > 0 + else + OSL_FAIL( "failed to acquire SolarMutex to reset capture frame" ); +#endif + } + + m_aDragRunning.reset(); + + if( listener.is() ) + listener->dragDropEnd( aDragFailedEvent ); + } +} + +void SelectionManager::runDragExecute( void* pThis ) +{ + SelectionManager* This = static_cast<SelectionManager*>(pThis); + This->dragDoDispatch(); +} + +void SelectionManager::dragDoDispatch() +{ + + // do drag + // m_xDragSourceListener will be cleared on finished drop +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "begin executeDrag dispatching."); +#endif + oslThread aThread = m_aDragExecuteThread; + while( m_xDragSourceListener.is() && ( ! m_bDropSent || time(nullptr)-m_nDropTimeout < 5 ) && osl_scheduleThread( aThread ) ) + { + // let the thread in the run method do the dispatching + // just look occasionally here whether drop timed out or is completed + osl::Thread::wait(std::chrono::milliseconds(200)); + } +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "end executeDrag dispatching."); +#endif + { + osl::ClearableMutexGuard aGuard(m_aMutex); + + css::uno::Reference< XDragSourceListener > xListener( m_xDragSourceListener ); + css::uno::Reference< XTransferable > xTransferable( m_xDragSourceTransferable ); + m_xDragSourceListener.clear(); + m_xDragSourceTransferable.clear(); + + DragSourceDropEvent dsde; + dsde.Source = static_cast< OWeakObject* >(this); + dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, *this ); + dsde.DragSource = static_cast< XDragSource* >(this); + dsde.DropAction = DNDConstants::ACTION_NONE; + dsde.DropSuccess = false; + + // cleanup after drag + if( m_bWaitingForPrimaryConversion ) + { + SelectionAdaptor* pAdaptor = getAdaptor( XA_PRIMARY ); + if (pAdaptor) + pAdaptor->clearTransferable(); + } + + m_bDropSent = false; + m_bDropSuccess = false; + m_bWaitingForPrimaryConversion = false; + m_aDropWindow = None; + m_aDropProxy = None; + m_nCurrentProtocolVersion = nXdndProtocolRevision; + m_nNoPosX = 0; + m_nNoPosY = 0; + m_nNoPosWidth = 0; + m_nNoPosHeight = 0; + m_aCurrentCursor = None; + + XUngrabPointer( m_pDisplay, CurrentTime ); + XUngrabKeyboard( m_pDisplay, CurrentTime ); + XFlush( m_pDisplay ); + + m_aDragExecuteThread = nullptr; + m_aDragRunning.reset(); + + aGuard.clear(); + if( xListener.is() ) + { + xTransferable.clear(); + xListener->dragDropEnd( dsde ); + } + } + osl_destroyThread( aThread ); +} + +/* + * XDragSourceContext + */ + + +void SelectionManager::setCursor( sal_Int32 cursor, ::Window aDropWindow ) +{ + osl::MutexGuard aGuard( m_aMutex ); + if( aDropWindow == m_aDropWindow && Cursor(cursor) != m_aCurrentCursor ) + { + if( m_xDragSourceListener.is() && ! m_bDropSent ) + { + m_aCurrentCursor = cursor; + XChangeActivePointerGrab( m_pDisplay, DRAG_EVENT_MASK, cursor, CurrentTime ); + XFlush( m_pDisplay ); + } + } +} + +void SelectionManager::transferablesFlavorsChanged() +{ + osl::MutexGuard aGuard(m_aMutex); + + m_aDragFlavors = m_xDragSourceTransferable->getTransferDataFlavors(); + + std::list< Atom > aConversions; + + getNativeTypeList( m_aDragFlavors, aConversions, m_nXdndSelection ); + + Atom* pTypes = static_cast<Atom*>(alloca( sizeof(Atom)*aConversions.size() )); + int nTypes = 0; + for (auto const& conversion : aConversions) + pTypes[nTypes++] = conversion; + XChangeProperty( m_pDisplay, m_aWindow, m_nXdndTypeList, XA_ATOM, 32, PropModeReplace, reinterpret_cast<unsigned char*>(pTypes), nTypes ); + + if( m_aCurrentDropWindow == None || m_nCurrentProtocolVersion < 0 ) + return; + + // send synthetic leave and enter events + + XEvent aEvent; + + aEvent.type = ClientMessage; + aEvent.xclient.display = m_pDisplay; + aEvent.xclient.format = 32; + aEvent.xclient.window = m_aDropWindow; + aEvent.xclient.data.l[0] = m_aWindow; + + aEvent.xclient.message_type = m_nXdndLeave; + aEvent.xclient.data.l[1] = 0; + XSendEvent( m_pDisplay, m_aDropProxy, False, NoEventMask, &aEvent ); + + aEvent.xclient.message_type = m_nXdndEnter; + aEvent.xclient.data.l[1] = m_nCurrentProtocolVersion << 24; + memset( aEvent.xclient.data.l + 2, 0, sizeof( long )*3 ); + // fill in data types + if( nTypes > 3 ) + aEvent.xclient.data.l[1] |= 1; + for( int j = 0; j < nTypes && j < 3; j++ ) + aEvent.xclient.data.l[j+2] = pTypes[j]; + + XSendEvent( m_pDisplay, m_aDropProxy, False, NoEventMask, &aEvent ); + +} + +/* + * dispatch loop + */ + +bool SelectionManager::handleXEvent( XEvent& rEvent ) +{ + /* + * since we are XConnectionListener to a second X display + * to get client messages it is essential not to dispatch + * events twice that we get on both connections + * + * between dispatching ButtonPress and startDrag + * the user can already have released the mouse. The ButtonRelease + * will then be dispatched in VCLs queue and never turn up here. + * Which is not so good, since startDrag will XGrabPointer and + * XGrabKeyboard -> solid lock. + */ + if( rEvent.xany.display != m_pDisplay + && rEvent.type != ClientMessage + && rEvent.type != ButtonPress + && rEvent.type != ButtonRelease + ) + return false; + + bool bHandled = false; + switch (rEvent.type) + { + case SelectionClear: + { + osl::ClearableMutexGuard aGuard(m_aMutex); +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "SelectionClear for selection " + << getString( rEvent.xselectionclear.selection )); +#endif + SelectionAdaptor* pAdaptor = getAdaptor( rEvent.xselectionclear.selection ); + std::unordered_map< Atom, Selection* >::iterator it( m_aSelections.find( rEvent.xselectionclear.selection ) ); + if( it != m_aSelections.end() ) + it->second->m_bOwner = false; + aGuard.clear(); + if ( pAdaptor ) + pAdaptor->clearTransferable(); + } + break; + + case SelectionRequest: + bHandled = handleSelectionRequest( rEvent.xselectionrequest ); + break; + case PropertyNotify: + if( rEvent.xproperty.window == m_aWindow || + rEvent.xproperty.window == m_aCurrentDropWindow + ) + bHandled = handleReceivePropertyNotify( rEvent.xproperty ); + else + bHandled = handleSendPropertyNotify( rEvent.xproperty ); + break; + case SelectionNotify: + bHandled = handleSelectionNotify( rEvent.xselection ); + break; + case ClientMessage: + // messages from drag target + if( rEvent.xclient.message_type == m_nXdndStatus || + rEvent.xclient.message_type == m_nXdndFinished ) + bHandled = handleDragEvent( rEvent ); + // messages from drag source + else if( + rEvent.xclient.message_type == m_nXdndEnter || + rEvent.xclient.message_type == m_nXdndLeave || + rEvent.xclient.message_type == m_nXdndPosition || + rEvent.xclient.message_type == m_nXdndDrop + ) + bHandled = handleDropEvent( rEvent.xclient ); + break; + case EnterNotify: + case LeaveNotify: + case MotionNotify: + case ButtonPress: + case ButtonRelease: + case KeyPress: + case KeyRelease: + bHandled = handleDragEvent( rEvent ); + break; + default: + ; + } + return bHandled; +} + +void SelectionManager::dispatchEvent( int millisec ) +{ + // acquire the mutex to prevent other threads + // from using the same X connection + osl::ResettableMutexGuard aGuard(m_aMutex); + + if( !XPending( m_pDisplay )) + { + int nfds = 1; + // wait for any events if none are already queued + pollfd aPollFD[2]; + aPollFD[0].fd = XConnectionNumber( m_pDisplay ); + aPollFD[0].events = POLLIN; + aPollFD[0].revents = 0; + + // on infinite timeout we need endthreadpipe monitoring too + if (millisec < 0) + { + aPollFD[1].fd = m_EndThreadPipe[0]; + aPollFD[1].events = POLLIN | POLLERR; + aPollFD[1].revents = 0; + nfds = 2; + } + + // release mutex for the time of waiting for possible data + aGuard.clear(); + if( poll( aPollFD, nfds, millisec ) <= 0 ) + return; + aGuard.reset(); + } + while( XPending( m_pDisplay )) + { + XEvent event; + XNextEvent( m_pDisplay, &event ); + aGuard.clear(); + handleXEvent( event ); + aGuard.reset(); + } +} + +void SelectionManager::run( void* pThis ) +{ +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "SelectionManager::run."); +#endif + osl::Thread::setName("SelectionManager"); + // dispatch until the cows come home + + SelectionManager* This = static_cast<SelectionManager*>(pThis); + + timeval aLast; + gettimeofday( &aLast, nullptr ); + + css::uno::Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() ); + This->m_xDesktop.set( Desktop::create(xContext) ); + This->m_xDesktop->addTerminateListener(This); + + // if end thread pipe properly initialized, allow infinite wait in poll + // otherwise, fallback on 1 sec timeout + const int timeout = (This->m_EndThreadPipe[0] != This->m_EndThreadPipe[1]) ? -1 : 1000; + + while( osl_scheduleThread(This->m_aThread) ) + { + This->dispatchEvent( timeout ); + + timeval aNow; + gettimeofday( &aNow, nullptr ); + + if( (aNow.tv_sec - aLast.tv_sec) > 0 ) + { + osl::ClearableMutexGuard aGuard(This->m_aMutex); + std::vector< std::pair< SelectionAdaptor*, css::uno::Reference< XInterface > > > aChangeVector; + + for (auto const& selection : This->m_aSelections) + { + if( selection.first != This->m_nXdndSelection && ! selection.second->m_bOwner ) + { + ::Window aOwner = XGetSelectionOwner( This->m_pDisplay, selection.first ); + if( aOwner != selection.second->m_aLastOwner ) + { + selection.second->m_aLastOwner = aOwner; + std::pair< SelectionAdaptor*, css::uno::Reference< XInterface > > + aKeep( selection.second->m_pAdaptor, selection.second->m_pAdaptor->getReference() ); + aChangeVector.push_back( aKeep ); + } + } + } + aGuard.clear(); + for (auto const& change : aChangeVector) + { + change.first->fireContentsChanged(); + } + aLast = aNow; + } + } + // close write end on exit so write() fails and other thread does not block + // forever + close(This->m_EndThreadPipe[1]); + close(This->m_EndThreadPipe[0]); +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "SelectionManager::run end."); +#endif +} + +void SelectionManager::shutdown() throw() +{ +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "SelectionManager got app termination event."); +#endif + + osl::ResettableMutexGuard aGuard(m_aMutex); + + if( m_bShutDown ) + return; + m_bShutDown = true; + + if ( m_xDesktop.is() ) + m_xDesktop->removeTerminateListener(this); + + if( m_xDisplayConnection.is() ) + m_xDisplayConnection->removeEventHandler(Any(), this); + + // stop dispatching + if( m_aThread ) + { + osl_terminateThread( m_aThread ); + /* + * Allow thread to finish before app exits to avoid pulling the carpet + * out from under it if pasting is occurring during shutdown + * + * a) allow it to have the Mutex and + * b) reschedule to allow it to complete callbacks to any + * Application::GetSolarMutex protected regions, etc. e.g. + * TransferableHelper::getTransferDataFlavors (via + * SelectionManager::handleSelectionRequest) which it might + * currently be trying to enter. + * + * Otherwise the thread may be left still waiting on a GlobalMutex + * when that gets destroyed, letting the thread blow up and die + * when enters the section in a now dead OOo instance. + */ + aGuard.clear(); + while (osl_isThreadRunning(m_aThread)) + { + { // drop mutex before write - otherwise may deadlock + SolarMutexGuard guard2; + Application::Reschedule(); + } + // trigger poll()'s wait end by writing a dummy value + char dummy=0; + dummy = write(m_EndThreadPipe[1], &dummy, 1); + } + osl_joinWithThread( m_aThread ); + osl_destroyThread( m_aThread ); + m_aThread = nullptr; + aGuard.reset(); + } + m_xDesktop.clear(); + m_xDisplayConnection.clear(); + m_xDropTransferable.clear(); +} + +sal_Bool SelectionManager::handleEvent(const Any& event) +{ + Sequence< sal_Int8 > aSeq; + if( event >>= aSeq ) + { + XEvent* pEvent = reinterpret_cast<XEvent*>(aSeq.getArray()); + Time nTimestamp = CurrentTime; + if( pEvent->type == ButtonPress || pEvent->type == ButtonRelease ) + nTimestamp = pEvent->xbutton.time; + else if( pEvent->type == KeyPress || pEvent->type == KeyRelease ) + nTimestamp = pEvent->xkey.time; + else if( pEvent->type == MotionNotify ) + nTimestamp = pEvent->xmotion.time; + else if( pEvent->type == PropertyNotify ) + nTimestamp = pEvent->xproperty.time; + + if( nTimestamp != CurrentTime ) + { + osl::MutexGuard aGuard(m_aMutex); + + m_nSelectionTimestamp = nTimestamp; + } + + return handleXEvent( *pEvent ); + } + else + { +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "SelectionManager got downing event."); +#endif + shutdown(); + } + return true; +} + +void SAL_CALL SelectionManager::disposing( const css::lang::EventObject& rEvt ) +{ + if (rEvt.Source == m_xDesktop || rEvt.Source == m_xDisplayConnection) + shutdown(); +} + +void SAL_CALL SelectionManager::queryTermination( const css::lang::EventObject& ) +{ +} + +/* + * To be safe, shutdown needs to be called before the ~SfxApplication is called, waiting until + * the downing event can be too late if paste are requested during shutdown and ~SfxApplication + * has been called before vcl is shutdown + */ +void SAL_CALL SelectionManager::notifyTermination( const css::lang::EventObject& rEvent ) +{ + disposing(rEvent); +} + +void SelectionManager::registerHandler( Atom selection, SelectionAdaptor& rAdaptor ) +{ + osl::MutexGuard aGuard(m_aMutex); + + Selection* pNewSelection = new Selection(); + pNewSelection->m_pAdaptor = &rAdaptor; + m_aSelections[ selection ] = pNewSelection; +} + +void SelectionManager::deregisterHandler( Atom selection ) +{ + osl::MutexGuard aGuard(m_aMutex); + + std::unordered_map< Atom, Selection* >::iterator it = + m_aSelections.find( selection ); + if( it != m_aSelections.end() ) + { + delete it->second->m_pPixmap; + delete it->second; + m_aSelections.erase( it ); + } +} + +static bool bWasError = false; + +extern "C" +{ + static int local_xerror_handler(Display* , XErrorEvent*) + { + bWasError = true; + return 0; + } + typedef int(*xerror_hdl_t)(Display*,XErrorEvent*); +} + +void SelectionManager::registerDropTarget( ::Window aWindow, DropTarget* pTarget ) +{ + osl::MutexGuard aGuard(m_aMutex); + + // sanity check + std::unordered_map< ::Window, DropTargetEntry >::const_iterator it = + m_aDropTargets.find( aWindow ); + if( it != m_aDropTargets.end() ) + OSL_FAIL( "attempt to register window as drop target twice" ); + else if( aWindow && m_pDisplay ) + { + DropTargetEntry aEntry( pTarget ); + bWasError=false; + /* #i100000# ugly workaround: gtk sets its own XErrorHandler which is not suitable for us + unfortunately XErrorHandler is not per display, so this is just and ugly hack + Need to remove separate display and integrate clipboard/dnd into vcl's unx code ASAP + */ + xerror_hdl_t pOldHandler = XSetErrorHandler( local_xerror_handler ); + XSelectInput( m_pDisplay, aWindow, PropertyChangeMask ); + if( ! bWasError ) + { + // set XdndAware + XChangeProperty( m_pDisplay, aWindow, m_nXdndAware, XA_ATOM, 32, PropModeReplace, reinterpret_cast<unsigned char const *>(&nXdndProtocolRevision), 1 ); + if( ! bWasError ) + { + // get root window of window (in 99.999% of all cases this will be + // DefaultRootWindow( m_pDisplay ) + int x, y; + unsigned int w, h, bw, d; + XGetGeometry( m_pDisplay, aWindow, &aEntry.m_aRootWindow, + &x, &y, &w, &h, &bw, &d ); + } + } + XSetErrorHandler( pOldHandler ); + if(bWasError) + return; + m_aDropTargets[ aWindow ] = aEntry; + } + else + OSL_FAIL( "attempt to register None as drop target" ); +} + +void SelectionManager::deregisterDropTarget( ::Window aWindow ) +{ + osl::ClearableMutexGuard aGuard(m_aMutex); + + m_aDropTargets.erase( aWindow ); + if( aWindow != m_aDragSourceWindow || !m_aDragRunning.check() ) + return; + + // abort drag + std::unordered_map< ::Window, DropTargetEntry >::const_iterator it = + m_aDropTargets.find( m_aDropWindow ); + if( it != m_aDropTargets.end() ) + { + DropTargetEvent dte; + dte.Source = static_cast< OWeakObject* >( it->second.m_pTarget ); + aGuard.clear(); + it->second.m_pTarget->dragExit( dte ); + } + else if( m_aDropProxy != None && m_nCurrentProtocolVersion >= 0 ) + { + // send XdndLeave + XEvent aEvent; + aEvent.type = ClientMessage; + aEvent.xclient.display = m_pDisplay; + aEvent.xclient.format = 32; + aEvent.xclient.message_type = m_nXdndLeave; + aEvent.xclient.window = m_aDropWindow; + aEvent.xclient.data.l[0] = m_aWindow; + memset( aEvent.xclient.data.l+1, 0, sizeof(long)*4); + m_aDropWindow = m_aDropProxy = None; + XSendEvent( m_pDisplay, m_aDropProxy, False, NoEventMask, &aEvent ); + } + // notify the listener + DragSourceDropEvent dsde; + dsde.Source = static_cast< OWeakObject* >(this); + dsde.DragSourceContext = new DragSourceContext( m_aDropWindow, *this ); + dsde.DragSource = static_cast< XDragSource* >(this); + dsde.DropAction = DNDConstants::ACTION_NONE; + dsde.DropSuccess = false; + css::uno::Reference< XDragSourceListener > xListener( m_xDragSourceListener ); + m_xDragSourceListener.clear(); + aGuard.clear(); + xListener->dragDropEnd( dsde ); + +} + +/* + * SelectionAdaptor + */ + +css::uno::Reference< XTransferable > SelectionManager::getTransferable() throw() +{ + return m_xDragSourceTransferable; +} + +void SelectionManager::clearTransferable() throw() +{ + m_xDragSourceTransferable.clear(); +} + +void SelectionManager::fireContentsChanged() throw() +{ +} + +css::uno::Reference< XInterface > SelectionManager::getReference() throw() +{ + return css::uno::Reference< XInterface >( static_cast<OWeakObject*>(this) ); +} + +/* + * SelectionManagerHolder + */ + +SelectionManagerHolder::SelectionManagerHolder() : + ::cppu::WeakComponentImplHelper< + XDragSource, + XInitialization, + XServiceInfo > (m_aMutex) +{ +} + +SelectionManagerHolder::~SelectionManagerHolder() +{ +} + +void SelectionManagerHolder::initialize( const Sequence< Any >& arguments ) +{ + OUString aDisplayName; + + if( arguments.hasElements() ) + { + css::uno::Reference< XDisplayConnection > xConn; + arguments.getConstArray()[0] >>= xConn; + if( xConn.is() ) + { + Any aIdentifier; + aIdentifier >>= aDisplayName; + } + } + + SelectionManager& rManager = SelectionManager::get( aDisplayName ); + rManager.initialize( arguments ); + m_xRealDragSource = static_cast< XDragSource* >(&rManager); +} + +/* + * XDragSource + */ + +sal_Bool SelectionManagerHolder::isDragImageSupported() +{ + return m_xRealDragSource.is() && m_xRealDragSource->isDragImageSupported(); +} + +sal_Int32 SelectionManagerHolder::getDefaultCursor( sal_Int8 dragAction ) +{ + return m_xRealDragSource.is() ? m_xRealDragSource->getDefaultCursor( dragAction ) : 0; +} + +void SelectionManagerHolder::startDrag( + const css::datatransfer::dnd::DragGestureEvent& trigger, + sal_Int8 sourceActions, sal_Int32 cursor, sal_Int32 image, + const css::uno::Reference< css::datatransfer::XTransferable >& transferable, + const css::uno::Reference< css::datatransfer::dnd::XDragSourceListener >& listener + ) +{ + if( m_xRealDragSource.is() ) + m_xRealDragSource->startDrag( trigger, sourceActions, cursor, image, transferable, listener ); +} + +/* + * XServiceInfo + */ + +OUString SelectionManagerHolder::getImplementationName() +{ + return XDND_IMPLEMENTATION_NAME; +} + +sal_Bool SelectionManagerHolder::supportsService( const OUString& ServiceName ) +{ + return cppu::supportsService(this, ServiceName); +} + +Sequence< OUString > SelectionManagerHolder::getSupportedServiceNames() +{ + return Xdnd_getSupportedServiceNames(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/generic/dtrans/X11_selection.hxx b/vcl/unx/generic/dtrans/X11_selection.hxx new file mode 100644 index 000000000..5bcabe9bd --- /dev/null +++ b/vcl/unx/generic/dtrans/X11_selection.hxx @@ -0,0 +1,501 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_VCL_UNX_GENERIC_DTRANS_X11_SELECTION_HXX +#define INCLUDED_VCL_UNX_GENERIC_DTRANS_X11_SELECTION_HXX + +#include <cppuhelper/compbase.hxx> +#include <cppuhelper/implbase.hxx> +#include <com/sun/star/datatransfer/XTransferable.hpp> +#include <com/sun/star/datatransfer/dnd/XDropTarget.hpp> +#include <com/sun/star/datatransfer/dnd/XDragSource.hpp> +#include <com/sun/star/awt/XDisplayConnection.hpp> +#include <com/sun/star/lang/XInitialization.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/lang/XMultiServiceFactory.hpp> +#include <com/sun/star/frame/XDesktop2.hpp> +#include <osl/thread.h> +#include <osl/conditn.hxx> +#include <rtl/ref.hxx> + +#include <list> +#include <unordered_map> +#include <vector> + +#include <X11/Xlib.h> + +#define XDND_IMPLEMENTATION_NAME "com.sun.star.datatransfer.dnd.XdndSupport" +#define XDND_DROPTARGET_IMPLEMENTATION_NAME "com.sun.star.datatransfer.dnd.XdndDropTarget" + +namespace x11 { + + class PixmapHolder; // in bmp.hxx + class SelectionManager; + + rtl_TextEncoding getTextPlainEncoding( const OUString& rMimeType ); + + class SelectionAdaptor + { + public: + virtual css::uno::Reference< css::datatransfer::XTransferable > getTransferable() = 0; + virtual void clearTransferable() = 0; + virtual void fireContentsChanged() = 0; + virtual css::uno::Reference< css::uno::XInterface > getReference() = 0; + // returns a reference that will keep the SelectionAdaptor alive until the + // reference is released + + protected: + ~SelectionAdaptor() {} + }; + + class DropTarget : + public ::cppu::WeakComponentImplHelper< + css::datatransfer::dnd::XDropTarget, + css::lang::XInitialization, + css::lang::XServiceInfo + > + { + public: + ::osl::Mutex m_aMutex; + bool m_bActive; + sal_Int8 m_nDefaultActions; + ::Window m_aTargetWindow; + rtl::Reference<SelectionManager> + m_xSelectionManager; + ::std::vector< css::uno::Reference< css::datatransfer::dnd::XDropTargetListener > > + m_aListeners; + + DropTarget(); + virtual ~DropTarget() override; + + // convenience functions that loop over listeners + void dragEnter( const css::datatransfer::dnd::DropTargetDragEnterEvent& dtde ) throw(); + void dragExit( const css::datatransfer::dnd::DropTargetEvent& dte ) throw(); + void dragOver( const css::datatransfer::dnd::DropTargetDragEvent& dtde ) throw(); + void drop( const css::datatransfer::dnd::DropTargetDropEvent& dtde ) throw(); + + // XInitialization + virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& args ) override; + + // XDropTarget + virtual void SAL_CALL addDropTargetListener( const css::uno::Reference< css::datatransfer::dnd::XDropTargetListener >& ) override; + virtual void SAL_CALL removeDropTargetListener( const css::uno::Reference< css::datatransfer::dnd::XDropTargetListener >& ) override; + virtual sal_Bool SAL_CALL isActive() override; + virtual void SAL_CALL setActive( sal_Bool active ) override; + virtual sal_Int8 SAL_CALL getDefaultActions() override; + virtual void SAL_CALL setDefaultActions( sal_Int8 actions ) override; + + // XServiceInfo + virtual OUString SAL_CALL getImplementationName() override; + virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override; + virtual css::uno::Sequence< OUString > + SAL_CALL getSupportedServiceNames() override; + }; + + class SelectionManagerHolder : + public ::cppu::WeakComponentImplHelper< + css::datatransfer::dnd::XDragSource, + css::lang::XInitialization, + css::lang::XServiceInfo + > + { + ::osl::Mutex m_aMutex; + css::uno::Reference< css::datatransfer::dnd::XDragSource > + m_xRealDragSource; + public: + SelectionManagerHolder(); + virtual ~SelectionManagerHolder() override; + + // XServiceInfo + virtual OUString SAL_CALL getImplementationName() override; + virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override; + virtual css::uno::Sequence< OUString > + SAL_CALL getSupportedServiceNames() override; + + // XInitialization + virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& arguments ) override; + + // XDragSource + virtual sal_Bool SAL_CALL isDragImageSupported() override; + virtual sal_Int32 SAL_CALL getDefaultCursor( sal_Int8 dragAction ) override; + virtual void SAL_CALL startDrag( + const css::datatransfer::dnd::DragGestureEvent& trigger, + sal_Int8 sourceActions, sal_Int32 cursor, sal_Int32 image, + const css::uno::Reference< css::datatransfer::XTransferable >& transferable, + const css::uno::Reference< css::datatransfer::dnd::XDragSourceListener >& listener + ) override; + + }; + + class SelectionManager : + public ::cppu::WeakImplHelper< + css::datatransfer::dnd::XDragSource, + css::lang::XInitialization, + css::awt::XEventHandler, + css::frame::XTerminateListener + >, + public SelectionAdaptor + { + static std::unordered_map< OUString, SelectionManager* >& getInstances(); + + // for INCR type selection transfer + // INCR protocol is used if the data cannot + // be transported at once but in parts + // IncrementalTransfer holds the bytes to be transmitted + // as well as the current position + // INCR triggers the delivery of the next part by deleting the + // property used to transfer the data + struct IncrementalTransfer + { + css::uno::Sequence< sal_Int8 > m_aData; + int m_nBufferPos; + ::Window m_aRequestor; + Atom m_aProperty; + Atom m_aTarget; + int m_nFormat; + int m_nTransferStartTime; + }; + int m_nIncrementalThreshold; + + // a struct to hold the data associated with a selection + struct Selection + { + enum State + { + Inactive, WaitingForResponse, WaitingForData, IncrementalTransfer + }; + + State m_eState; + SelectionAdaptor* m_pAdaptor; + ::osl::Condition m_aDataArrived; + css::uno::Sequence< sal_Int8 > m_aData; + css::uno::Sequence< css::datatransfer::DataFlavor > + m_aTypes; + std::vector< Atom > m_aNativeTypes; + // this is used for caching + // m_aTypes is invalid after 2 seconds + // m_aNativeTypes contains the corresponding original atom + Atom m_aRequestedType; + // m_aRequestedType is only valid while WaitingForResponse and WaitingFotData + int m_nLastTimestamp; + bool m_bHaveUTF16; + Atom m_aUTF8Type; + bool m_bHaveCompound; + bool m_bOwner; + ::Window m_aLastOwner; + PixmapHolder* m_pPixmap; + // m_nOrigTimestamp contains the Timestamp at which the selection + // was acquired; needed for TimeSTAMP target + Time m_nOrigTimestamp; + + Selection() : m_eState( Inactive ), + m_pAdaptor( nullptr ), + m_aRequestedType( None ), + m_nLastTimestamp( 0 ), + m_bHaveUTF16( false ), + m_aUTF8Type( None ), + m_bHaveCompound( false ), + m_bOwner( false ), + m_aLastOwner( None ), + m_pPixmap( nullptr ), + m_nOrigTimestamp( CurrentTime ) + {} + }; + + // a struct to hold data associated with a XDropTarget + struct DropTargetEntry + { + DropTarget* m_pTarget; + ::Window m_aRootWindow; + + DropTargetEntry() : m_pTarget( nullptr ), m_aRootWindow( None ) {} + explicit DropTargetEntry( DropTarget* pTarget ) : + m_pTarget( pTarget ), + m_aRootWindow( None ) + {} + DropTargetEntry( const DropTargetEntry& rEntry ) : + m_pTarget( rEntry.m_pTarget ), + m_aRootWindow( rEntry.m_aRootWindow ) + {} + + DropTarget* operator->() const { return m_pTarget; } + DropTargetEntry& operator=(const DropTargetEntry& rEntry) + { m_pTarget = rEntry.m_pTarget; m_aRootWindow = rEntry.m_aRootWindow; return *this; } + }; + + // internal data + Display* m_pDisplay; + oslThread m_aThread; + int m_EndThreadPipe[2]; + oslThread m_aDragExecuteThread; + ::osl::Condition m_aDragRunning; + ::Window m_aWindow; + css::uno::Reference< css::frame::XDesktop2 > m_xDesktop; + css::uno::Reference< css::awt::XDisplayConnection > + m_xDisplayConnection; + sal_Int32 m_nSelectionTimeout; + Time m_nSelectionTimestamp; + + // members used for Xdnd + + // drop only + + // contains the XdndEnterEvent of a drop action running + // with one of our targets. The data.l[0] member + // (containing the drag source ::Window) is set + // to None while that is not the case + XClientMessageEvent m_aDropEnterEvent; + // set to false on XdndEnter + // set to true on first XdndPosition or XdndLeave + bool m_bDropEnterSent; + ::Window m_aCurrentDropWindow; + // Time code of XdndDrop + Time m_nDropTime; + sal_Int8 m_nLastDropAction; + // XTransferable for Xdnd with foreign drag source + css::uno::Reference< css::datatransfer::XTransferable > + m_xDropTransferable; + int m_nLastX, m_nLastY; + // set to true when calling drop() + // if another XdndEnter is received this shows that + // someone forgot to call dropComplete - we should reset + // and react to the new drop + bool m_bDropWaitingForCompletion; + + // drag only + + // None if no Dnd action is running with us as source + ::Window m_aDropWindow; + // either m_aDropWindow or its XdndProxy + ::Window m_aDropProxy; + ::Window m_aDragSourceWindow; + // XTransferable for Xdnd when we are drag source + css::uno::Reference< css::datatransfer::XTransferable > + m_xDragSourceTransferable; + css::uno::Reference< css::datatransfer::dnd::XDragSourceListener > + m_xDragSourceListener; + // root coordinates + int m_nLastDragX, m_nLastDragY; + css::uno::Sequence< css::datatransfer::DataFlavor > + m_aDragFlavors; + // the rectangle the pointer must leave until a new XdndPosition should + // be sent. empty unless the drop target told to fill + int m_nNoPosX, m_nNoPosY, m_nNoPosWidth, m_nNoPosHeight; + unsigned int m_nDragButton; + sal_Int8 m_nUserDragAction; + sal_Int8 m_nTargetAcceptAction; + sal_Int8 m_nSourceActions; + bool m_bLastDropAccepted; + bool m_bDropSuccess; + bool m_bDropSent; + time_t m_nDropTimeout; + bool m_bWaitingForPrimaryConversion; + + // drag cursors + Cursor m_aMoveCursor; + Cursor m_aCopyCursor; + Cursor m_aLinkCursor; + Cursor m_aNoneCursor; + Cursor m_aCurrentCursor; + + // drag and drop + + int m_nCurrentProtocolVersion; + std::unordered_map< ::Window, DropTargetEntry > + m_aDropTargets; + + // some special atoms that are needed often + Atom m_nTARGETSAtom; + Atom m_nTIMESTAMPAtom; + Atom m_nTEXTAtom; + Atom m_nINCRAtom; + Atom m_nCOMPOUNDAtom; + Atom m_nMULTIPLEAtom; + Atom m_nImageBmpAtom; + Atom m_nXdndAware; + Atom m_nXdndEnter; + Atom m_nXdndLeave; + Atom m_nXdndPosition; + Atom m_nXdndStatus; + Atom m_nXdndDrop; + Atom m_nXdndFinished; + Atom m_nXdndSelection; + Atom m_nXdndTypeList; + Atom m_nXdndProxy; + Atom m_nXdndActionCopy; + Atom m_nXdndActionMove; + Atom m_nXdndActionLink; + Atom m_nXdndActionAsk; + + // caching for atoms + std::unordered_map< Atom, OUString > + m_aAtomToString; + std::unordered_map< OUString, Atom > + m_aStringToAtom; + + // the registered selections + std::unordered_map< Atom, Selection* > + m_aSelections; + // IncrementalTransfers in progress + std::unordered_map< ::Window, std::unordered_map< Atom, IncrementalTransfer > > + m_aIncrementals; + + // do not use X11 multithreading capabilities + // since this leads to deadlocks in different Xlib implementations + // (XFree as well as Xsun) use an own mutex instead + ::osl::Mutex m_aMutex; + bool m_bShutDown; + + SelectionManager(); + virtual ~SelectionManager() override; + + SelectionAdaptor* getAdaptor( Atom selection ); + PixmapHolder* getPixmapHolder( Atom selection ); + + // handle various events + bool handleSelectionRequest( XSelectionRequestEvent& rRequest ); + bool handleSendPropertyNotify( XPropertyEvent const & rNotify ); + bool handleReceivePropertyNotify( XPropertyEvent const & rNotify ); + bool handleSelectionNotify( XSelectionEvent const & rNotify ); + bool handleDragEvent( XEvent const & rMessage ); + bool handleDropEvent( XClientMessageEvent const & rMessage ); + + // dnd helpers + void sendDragStatus( Atom nDropAction ); + void sendDropPosition( bool bForce, Time eventTime ); + bool updateDragAction( int modifierState ); + int getXdndVersion( ::Window aXLIB_Window, ::Window& rProxy ); + Cursor createCursor( const unsigned char* pPointerData, const unsigned char* pMaskData, int width, int height, int hotX, int hotY ); + // coordinates on root ::Window + void updateDragWindow( int nX, int nY, ::Window aRoot ); + + bool getPasteData( Atom selection, Atom type, css::uno::Sequence< sal_Int8 >& rData ); + // returns true if conversion was successful + bool convertData( const css::uno::Reference< css::datatransfer::XTransferable >& xTransferable, + Atom nType, + Atom nSelection, + int & rFormat, + css::uno::Sequence< sal_Int8 >& rData ); + bool sendData( SelectionAdaptor* pAdaptor, ::Window requestor, Atom target, Atom property, Atom selection ); + + // thread dispatch loop + public: + // public for extern "C" stub + static void run( void* ); + private: + void dispatchEvent( int millisec ); + // drag thread dispatch + public: + // public for extern "C" stub + static void runDragExecute( void* ); + private: + void dragDoDispatch(); + bool handleXEvent( XEvent& rEvent ); + + // compound text conversion + OString convertToCompound( const OUString& rText ); + OUString convertFromCompound( const char* pText, int nLen ); + + sal_Int8 getUserDragAction() const; + sal_Int32 getSelectionTimeout(); + public: + static SelectionManager& get( const OUString& rDisplayName = OUString() ); + + Display * getDisplay() { return m_pDisplay; }; + + void registerHandler( Atom selection, SelectionAdaptor& rAdaptor ); + void deregisterHandler( Atom selection ); + bool requestOwnership( Atom selection ); + + // allow for synchronization over one mutex for XClipboard + osl::Mutex& getMutex() { return m_aMutex; } + + Atom getAtom( const OUString& rString ); + OUString getString( Atom nAtom ); + + // type conversion + // note: convertTypeToNative does NOT clear the list, so you can append + // multiple types to the same list + void convertTypeToNative( const OUString& rType, Atom selection, int& rFormat, ::std::list< Atom >& rConversions, bool bPushFront = false ); + OUString convertTypeFromNative( Atom nType, Atom selection, int& rFormat ); + void getNativeTypeList( const css::uno::Sequence< css::datatransfer::DataFlavor >& rTypes, std::list< Atom >& rOutTypeList, Atom targetselection ); + + // methods for transferable + bool getPasteDataTypes( Atom selection, css::uno::Sequence< css::datatransfer::DataFlavor >& rTypes ); + bool getPasteData( Atom selection, const OUString& rType, css::uno::Sequence< sal_Int8 >& rData ); + + // for XDropTarget to register/deregister itself + void registerDropTarget( ::Window aXLIB_Window, DropTarget* pTarget ); + void deregisterDropTarget( ::Window aXLIB_Window ); + + // for XDropTarget{Drag|Drop}Context + void accept( sal_Int8 dragOperation, ::Window aDropXLIB_Window ); + void reject( ::Window aDropXLIB_Window ); + void dropComplete( bool success, ::Window aDropXLIB_Window ); + + // for XDragSourceContext + sal_Int32 getCurrentCursor() const { return m_aCurrentCursor;} + void setCursor( sal_Int32 cursor, ::Window aDropXLIB_Window ); + void transferablesFlavorsChanged(); + + void shutdown() throw(); + + // XInitialization + virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& arguments ) override; + + // XEventHandler + virtual sal_Bool SAL_CALL handleEvent(const css::uno::Any& event) override; + + // XDragSource + virtual sal_Bool SAL_CALL isDragImageSupported() override; + virtual sal_Int32 SAL_CALL getDefaultCursor( sal_Int8 dragAction ) override; + virtual void SAL_CALL startDrag( + const css::datatransfer::dnd::DragGestureEvent& trigger, + sal_Int8 sourceActions, sal_Int32 cursor, sal_Int32 image, + const css::uno::Reference< css::datatransfer::XTransferable >& transferable, + const css::uno::Reference< css::datatransfer::dnd::XDragSourceListener >& listener + ) override; + + // SelectionAdaptor for XdndSelection Drag (we are drag source) + virtual css::uno::Reference< css::datatransfer::XTransferable > getTransferable() throw() override; + virtual void clearTransferable() throw() override; + virtual void fireContentsChanged() throw() override; + virtual css::uno::Reference< css::uno::XInterface > getReference() throw() override; + + // XEventListener + virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override; + + // XTerminateListener + virtual void SAL_CALL queryTermination( const css::lang::EventObject& aEvent ) override; + virtual void SAL_CALL notifyTermination( const css::lang::EventObject& aEvent ) override; + }; + + css::uno::Sequence< OUString > Xdnd_getSupportedServiceNames(); + css::uno::Reference< css::uno::XInterface > SAL_CALL Xdnd_createInstance( + const css::uno::Reference< css::lang::XMultiServiceFactory > & xMultiServiceFactory); + + css::uno::Sequence< OUString > Xdnd_dropTarget_getSupportedServiceNames(); + css::uno::Reference< css::uno::XInterface > SAL_CALL Xdnd_dropTarget_createInstance( + const css::uno::Reference< css::lang::XMultiServiceFactory > & xMultiServiceFactory); + +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/generic/dtrans/X11_service.cxx b/vcl/unx/generic/dtrans/X11_service.cxx new file mode 100644 index 000000000..2b9c1299b --- /dev/null +++ b/vcl/unx/generic/dtrans/X11_service.cxx @@ -0,0 +1,84 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <unx/salinst.h> + +#include "X11_clipboard.hxx" +#include <com/sun/star/lang/IllegalArgumentException.hpp> + +using namespace cppu; +using namespace com::sun::star::uno; +using namespace com::sun::star::lang; +using namespace com::sun::star::datatransfer::clipboard; +using namespace com::sun::star::awt; +using namespace x11; + +Sequence< OUString > x11::X11Clipboard_getSupportedServiceNames() +{ + return { "com.sun.star.datatransfer.clipboard.SystemClipboard" }; +} + +Sequence< OUString > x11::Xdnd_getSupportedServiceNames() +{ + return { "com.sun.star.datatransfer.dnd.X11DragSource" }; +} + +Sequence< OUString > x11::Xdnd_dropTarget_getSupportedServiceNames() +{ + return { "com.sun.star.datatransfer.dnd.X11DropTarget" }; +} + +css::uno::Reference< XInterface > X11SalInstance::CreateClipboard( const Sequence< Any >& arguments ) +{ + SelectionManager& rManager = SelectionManager::get(); + css::uno::Sequence<css::uno::Any> mgrArgs(1); + mgrArgs[0] <<= Application::GetDisplayConnection(); + rManager.initialize(mgrArgs); + + OUString sel; + if (!arguments.hasElements()) { + sel = "CLIPBOARD"; + } else if (arguments.getLength() != 1 || !(arguments[0] >>= sel)) { + throw css::lang::IllegalArgumentException( + "bad X11SalInstance::CreateClipboard arguments", + css::uno::Reference<css::uno::XInterface>(), -1); + } + Atom nSelection = rManager.getAtom(sel); + + std::unordered_map< Atom, css::uno::Reference< XClipboard > >::iterator it = m_aInstances.find( nSelection ); + if( it != m_aInstances.end() ) + return it->second; + + css::uno::Reference<css::datatransfer::clipboard::XClipboard> pClipboard = X11Clipboard::create( rManager, nSelection ); + m_aInstances[ nSelection ] = pClipboard; + + return pClipboard; +} + +css::uno::Reference< XInterface > X11SalInstance::CreateDragSource() +{ + return css::uno::Reference < XInterface >( static_cast<OWeakObject *>(new SelectionManagerHolder()) ); +} + +css::uno::Reference< XInterface > X11SalInstance::CreateDropTarget() +{ + return css::uno::Reference < XInterface >( static_cast<OWeakObject *>(new DropTarget()) ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/generic/dtrans/X11_transferable.cxx b/vcl/unx/generic/dtrans/X11_transferable.cxx new file mode 100644 index 000000000..1310f70bc --- /dev/null +++ b/vcl/unx/generic/dtrans/X11_transferable.cxx @@ -0,0 +1,101 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include "X11_transferable.hxx" +#include <X11/Xatom.h> +#include <com/sun/star/datatransfer/UnsupportedFlavorException.hpp> +#include <com/sun/star/io/IOException.hpp> +#include <sal/log.hxx> + +using namespace com::sun::star::datatransfer; +using namespace com::sun::star::lang; +using namespace com::sun::star::io; +using namespace com::sun::star::uno; +using namespace cppu; +using namespace osl; + +using namespace x11; + +X11Transferable::X11Transferable( + SelectionManager& rManager, + Atom selection + ) : + m_rManager( rManager ), + m_aSelection( selection ) +{ +} + +X11Transferable::~X11Transferable() +{ +} + +Any SAL_CALL X11Transferable::getTransferData( const DataFlavor& rFlavor ) +{ + Any aRet; + Sequence< sal_Int8 > aData; + bool bSuccess = m_rManager.getPasteData( m_aSelection ? m_aSelection : XA_PRIMARY, rFlavor.MimeType, aData ); + if( ! bSuccess && m_aSelection == 0 ) + bSuccess = m_rManager.getPasteData( m_rManager.getAtom( "CLIPBOARD" ), rFlavor.MimeType, aData ); + + if( ! bSuccess ) + { + throw UnsupportedFlavorException( rFlavor.MimeType, static_cast < XTransferable * > ( this ) ); + } + if( rFlavor.MimeType.equalsIgnoreAsciiCase( "text/plain;charset=utf-16" ) ) + { + int nLen = aData.getLength()/2; + if( reinterpret_cast<sal_Unicode const *>(aData.getConstArray())[nLen-1] == 0 ) + nLen--; + OUString aString( reinterpret_cast<sal_Unicode const *>(aData.getConstArray()), nLen ); + SAL_INFO( "vcl.unx.dtrans", "X11Transferable::getTransferData( \"" << rFlavor.MimeType << "\" )\n -> \"" << aString << "\""); + aRet <<= aString.replaceAll("\r\n", "\n"); + } + else + aRet <<= aData; + return aRet; +} + +Sequence< DataFlavor > SAL_CALL X11Transferable::getTransferDataFlavors() +{ + Sequence< DataFlavor > aFlavorList; + bool bSuccess = m_rManager.getPasteDataTypes( m_aSelection ? m_aSelection : XA_PRIMARY, aFlavorList ); + if( ! bSuccess && m_aSelection == 0 ) + m_rManager.getPasteDataTypes( m_rManager.getAtom( "CLIPBOARD" ), aFlavorList ); + + return aFlavorList; +} + +sal_Bool SAL_CALL X11Transferable::isDataFlavorSupported( const DataFlavor& aFlavor ) +{ + if( aFlavor.DataType != cppu::UnoType<Sequence< sal_Int8 >>::get() ) + { + if( ! aFlavor.MimeType.equalsIgnoreAsciiCase( "text/plain;charset=utf-16" ) && + aFlavor.DataType == cppu::UnoType<OUString>::get() ) + return false; + } + + Sequence< DataFlavor > aFlavors( getTransferDataFlavors() ); + return std::any_of(aFlavors.begin(), aFlavors.end(), + [&aFlavor](const DataFlavor& rFlavor) { + return aFlavor.MimeType.equalsIgnoreAsciiCase( rFlavor.MimeType ) + && aFlavor.DataType == rFlavor.DataType; + }); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/generic/dtrans/X11_transferable.hxx b/vcl/unx/generic/dtrans/X11_transferable.hxx new file mode 100644 index 000000000..0de101f6d --- /dev/null +++ b/vcl/unx/generic/dtrans/X11_transferable.hxx @@ -0,0 +1,53 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_VCL_UNX_GENERIC_DTRANS_X11_TRANSFERABLE_HXX +#define INCLUDED_VCL_UNX_GENERIC_DTRANS_X11_TRANSFERABLE_HXX + +#include "X11_selection.hxx" +#include <com/sun/star/datatransfer/XTransferable.hpp> + +#include <cppuhelper/implbase.hxx> + +namespace x11 { + + class X11Transferable : public ::cppu::WeakImplHelper< css::datatransfer::XTransferable > + { + SelectionManager& m_rManager; + Atom m_aSelection; + public: + X11Transferable( SelectionManager& rManager, Atom selection ); + virtual ~X11Transferable() override; + + /* + * XTransferable + */ + + virtual css::uno::Any SAL_CALL getTransferData( const css::datatransfer::DataFlavor& aFlavor ) override; + + virtual css::uno::Sequence< css::datatransfer::DataFlavor > SAL_CALL getTransferDataFlavors( ) override; + + virtual sal_Bool SAL_CALL isDataFlavorSupported( const css::datatransfer::DataFlavor& aFlavor ) override; + }; + +} // namespace + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/generic/dtrans/bmp.cxx b/vcl/unx/generic/dtrans/bmp.cxx new file mode 100644 index 000000000..7f0111a61 --- /dev/null +++ b/vcl/unx/generic/dtrans/bmp.cxx @@ -0,0 +1,786 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <tools/stream.hxx> + +#include <vcl/dibtools.hxx> +#include <vcl/svapp.hxx> +#include <vcl/bitmap.hxx> +#include <vcl/bitmapex.hxx> +#include <vcl/BitmapSimpleColorQuantizationFilter.hxx> + +#include <sal/log.hxx> +#include <unx/x11/xlimits.hxx> + +#include "bmp.hxx" + +using namespace x11; + +/* + * helper functions + */ + +static void writeLE( sal_uInt16 nNumber, sal_uInt8* pBuffer ) +{ + pBuffer[ 0 ] = (nNumber & 0xff); + pBuffer[ 1 ] = ((nNumber>>8)&0xff); +} + +static void writeLE( sal_uInt32 nNumber, sal_uInt8* pBuffer ) +{ + pBuffer[ 0 ] = (nNumber & 0xff); + pBuffer[ 1 ] = ((nNumber>>8)&0xff); + pBuffer[ 2 ] = ((nNumber>>16)&0xff); + pBuffer[ 3 ] = ((nNumber>>24)&0xff); +} + +static sal_uInt16 readLE16( const sal_uInt8* pBuffer ) +{ + //This is untainted data which comes from a controlled source + //so, using a byte-swapping pattern which coverity doesn't + //detect as such + //http://security.coverity.com/blog/2014/Apr/on-detecting-heartbleed-with-static-analysis.html + sal_uInt16 v = pBuffer[1]; v <<= 8; + v |= pBuffer[0]; + return v; +} + +static sal_uInt32 readLE32( const sal_uInt8* pBuffer ) +{ + //This is untainted data which comes from a controlled source + //so, using a byte-swapping pattern which coverity doesn't + //detect as such + //http://security.coverity.com/blog/2014/Apr/on-detecting-heartbleed-with-static-analysis.html + sal_uInt32 v = pBuffer[3]; v <<= 8; + v |= pBuffer[2]; v <<= 8; + v |= pBuffer[1]; v <<= 8; + v |= pBuffer[0]; + return v; +} + +/* + * scanline helpers + */ + +static void X11_writeScanlinePixel( unsigned long nColor, sal_uInt8* pScanline, int depth, int x ) +{ + switch( depth ) + { + case 1: + pScanline[ x/8 ] &= ~(1 << (x&7)); + pScanline[ x/8 ] |= ((nColor & 1) << (x&7)); + break; + case 4: + pScanline[ x/2 ] &= ((x&1) ? 0x0f : 0xf0); + pScanline[ x/2 ] |= ((x&1) ? (nColor & 0x0f) : ((nColor & 0x0f) << 4)); + break; + default: + case 8: + pScanline[ x ] = (nColor & 0xff); + break; + } +} + +static sal_uInt8* X11_getPaletteBmpFromImage( + Display* pDisplay, + XImage* pImage, + Colormap aColormap, + sal_Int32& rOutSize + ) +{ + sal_uInt32 nColors = 0; + + rOutSize = 0; + + sal_uInt8* pBuffer = nullptr; + sal_uInt32 nHeaderSize, nScanlineSize; + sal_uInt16 nBitCount; + // determine header and scanline size + switch( pImage->depth ) + { + case 1: + nHeaderSize = 64; + nScanlineSize = (pImage->width+31)/32; + nBitCount = 1; + break; + case 4: + nHeaderSize = 72; + nScanlineSize = (pImage->width+1)/2; + nBitCount = 4; + break; + default: + case 8: + nHeaderSize = 1084; + nScanlineSize = pImage->width; + nBitCount = 8; + break; + } + // adjust scan lines to begin on %4 boundaries + if( nScanlineSize & 3 ) + { + nScanlineSize &= 0xfffffffc; + nScanlineSize += 4; + } + + // allocate buffer to hold header and scanlines, initialize to zero + rOutSize = nHeaderSize + nScanlineSize*pImage->height; + pBuffer = static_cast<sal_uInt8*>(rtl_allocateZeroMemory( rOutSize )); + for( int y = 0; y < pImage->height; y++ ) + { + sal_uInt8* pScanline = pBuffer + nHeaderSize + (pImage->height-1-y)*nScanlineSize; + for( int x = 0; x < pImage->width; x++ ) + { + unsigned long nPixel = XGetPixel( pImage, x, y ); + if( nPixel >= nColors ) + nColors = nPixel+1; + X11_writeScanlinePixel( nPixel, pScanline, pImage->depth, x ); + } + } + + // fill in header fields + pBuffer[ 0 ] = 'B'; + pBuffer[ 1 ] = 'M'; + + writeLE( nHeaderSize, pBuffer+10 ); + writeLE( sal_uInt32(40), pBuffer+14 ); + writeLE( static_cast<sal_uInt32>(pImage->width), pBuffer+18 ); + writeLE( static_cast<sal_uInt32>(pImage->height), pBuffer+22 ); + writeLE( sal_uInt16(1), pBuffer+26 ); + writeLE( nBitCount, pBuffer+28 ); + writeLE( static_cast<sal_uInt32>(DisplayWidth(pDisplay,DefaultScreen(pDisplay))*1000/DisplayWidthMM(pDisplay,DefaultScreen(pDisplay))), pBuffer+38); + writeLE( static_cast<sal_uInt32>(DisplayHeight(pDisplay,DefaultScreen(pDisplay))*1000/DisplayHeightMM(pDisplay,DefaultScreen(pDisplay))), pBuffer+42); + writeLE( nColors, pBuffer+46 ); + writeLE( nColors, pBuffer+50 ); + + XColor aColors[256]; + if( nColors > (1U << nBitCount) ) // paranoia + nColors = (1U << nBitCount); + for( unsigned long nPixel = 0; nPixel < nColors; nPixel++ ) + { + aColors[nPixel].flags = DoRed | DoGreen | DoBlue; + aColors[nPixel].pixel = nPixel; + } + XQueryColors( pDisplay, aColormap, aColors, nColors ); + for( sal_uInt32 i = 0; i < nColors; i++ ) + { + pBuffer[ 54 + i*4 ] = static_cast<sal_uInt8>(aColors[i].blue >> 8); + pBuffer[ 55 + i*4 ] = static_cast<sal_uInt8>(aColors[i].green >> 8); + pBuffer[ 56 + i*4 ] = static_cast<sal_uInt8>(aColors[i].red >> 8); + } + + // done + + return pBuffer; +} + +static unsigned long doRightShift( unsigned long nValue, int nShift ) +{ + return (nShift > 0) ? (nValue >> nShift) : (nValue << (-nShift)); +} + +static unsigned long doLeftShift( unsigned long nValue, int nShift ) +{ + return (nShift > 0) ? (nValue << nShift) : (nValue >> (-nShift)); +} + +static void getShift( unsigned long nMask, int& rShift, int& rSigBits, int& rShift2 ) +{ + unsigned long nUseMask = nMask; + rShift = 0; + while( nMask & 0xffffff00 ) + { + rShift++; + nMask >>= 1; + } + if( rShift == 0 ) + while( ! (nMask & 0x00000080) ) + { + rShift--; + nMask <<= 1; + } + + int nRotate = sizeof(unsigned long)*8 - rShift; + rSigBits = 0; + nMask = doRightShift( nUseMask, rShift) ; + while( nRotate-- ) + { + if( nMask & 1 ) + rSigBits++; + nMask >>= 1; + } + + rShift2 = 0; + if( rSigBits < 8 ) + rShift2 = 8-rSigBits; +} + +static sal_uInt8* X11_getTCBmpFromImage( + Display* pDisplay, + XImage* pImage, + sal_Int32& rOutSize, + int nScreenNo + ) +{ + // get masks from visual info (guesswork) + XVisualInfo aVInfo; + if( ! XMatchVisualInfo( pDisplay, nScreenNo, pImage->depth, TrueColor, &aVInfo ) ) + return nullptr; + + rOutSize = 0; + + sal_uInt8* pBuffer = nullptr; + sal_uInt32 nHeaderSize = 60; + sal_uInt32 nScanlineSize = pImage->width*3; + + // adjust scan lines to begin on %4 boundaries + if( nScanlineSize & 3 ) + { + nScanlineSize &= 0xfffffffc; + nScanlineSize += 4; + } + int nRedShift, nRedSig, nRedShift2 = 0; + getShift( aVInfo.red_mask, nRedShift, nRedSig, nRedShift2 ); + int nGreenShift, nGreenSig, nGreenShift2 = 0; + getShift( aVInfo.green_mask, nGreenShift, nGreenSig, nGreenShift2 ); + int nBlueShift, nBlueSig, nBlueShift2 = 0; + getShift( aVInfo.blue_mask, nBlueShift, nBlueSig, nBlueShift2 ); + + // allocate buffer to hold header and scanlines, initialize to zero + rOutSize = nHeaderSize + nScanlineSize*pImage->height; + pBuffer = static_cast<sal_uInt8*>(rtl_allocateZeroMemory( rOutSize )); + for( int y = 0; y < pImage->height; y++ ) + { + sal_uInt8* pScanline = pBuffer + nHeaderSize + (pImage->height-1-y)*nScanlineSize; + for( int x = 0; x < pImage->width; x++ ) + { + unsigned long nPixel = XGetPixel( pImage, x, y ); + + sal_uInt8 nValue = static_cast<sal_uInt8>(doRightShift( nPixel&aVInfo.blue_mask, nBlueShift)); + if( nBlueShift2 ) + nValue |= (nValue >> nBlueShift2 ); + *pScanline++ = nValue; + + nValue = static_cast<sal_uInt8>(doRightShift( nPixel&aVInfo.green_mask, nGreenShift)); + if( nGreenShift2 ) + nValue |= (nValue >> nGreenShift2 ); + *pScanline++ = nValue; + + nValue = static_cast<sal_uInt8>(doRightShift( nPixel&aVInfo.red_mask, nRedShift)); + if( nRedShift2 ) + nValue |= (nValue >> nRedShift2 ); + *pScanline++ = nValue; + } + } + + // fill in header fields + pBuffer[ 0 ] = 'B'; + pBuffer[ 1 ] = 'M'; + + writeLE( nHeaderSize, pBuffer+10 ); + writeLE( sal_uInt32(40), pBuffer+14 ); + writeLE( static_cast<sal_uInt32>(pImage->width), pBuffer+18 ); + writeLE( static_cast<sal_uInt32>(pImage->height), pBuffer+22 ); + writeLE( sal_uInt16(1), pBuffer+26 ); + writeLE( sal_uInt16(24), pBuffer+28 ); + writeLE( static_cast<sal_uInt32>(DisplayWidth(pDisplay,DefaultScreen(pDisplay))*1000/DisplayWidthMM(pDisplay,DefaultScreen(pDisplay))), pBuffer+38); + writeLE( static_cast<sal_uInt32>(DisplayHeight(pDisplay,DefaultScreen(pDisplay))*1000/DisplayHeightMM(pDisplay,DefaultScreen(pDisplay))), pBuffer+42); + + // done + + return pBuffer; +} + +sal_uInt8* x11::X11_getBmpFromPixmap( + Display* pDisplay, + Drawable aDrawable, + Colormap aColormap, + sal_Int32& rOutSize + ) +{ + // get geometry of drawable + ::Window aRoot; + int x,y; + unsigned int w, h, bw, d; + XGetGeometry( pDisplay, aDrawable, &aRoot, &x, &y, &w, &h, &bw, &d ); + + // find which screen we are on + int nScreenNo = ScreenCount( pDisplay ); + while( nScreenNo-- ) + { + if( RootWindow( pDisplay, nScreenNo ) == aRoot ) + break; + } + if( nScreenNo < 0 ) + return nullptr; + + if( aColormap == None ) + aColormap = DefaultColormap( pDisplay, nScreenNo ); + + // get the image + XImage* pImage = XGetImage( pDisplay, aDrawable, 0, 0, w, h, AllPlanes, ZPixmap ); + if( ! pImage ) + return nullptr; + + sal_uInt8* pBmp = d <= 8 ? + X11_getPaletteBmpFromImage( pDisplay, pImage, aColormap, rOutSize ) : + X11_getTCBmpFromImage( pDisplay, pImage, rOutSize, nScreenNo ); + XDestroyImage( pImage ); + + return pBmp; +} + +/* + * PixmapHolder + */ + +PixmapHolder::PixmapHolder( Display* pDisplay ) + : m_pDisplay(pDisplay) + , m_aColormap(None) + , m_aPixmap(None) + , m_aBitmap(None) + , m_nRedShift(0) + , m_nGreenShift(0) + , m_nBlueShift(0) + , m_nBlueShift2Mask(0) + , m_nRedShift2Mask(0) + , m_nGreenShift2Mask(0) +{ + /* try to get a 24 bit true color visual, if that fails, + * revert to default visual + */ + if( ! XMatchVisualInfo( m_pDisplay, DefaultScreen( m_pDisplay ), 24, TrueColor, &m_aInfo ) ) + { +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "PixmapHolder reverting to default visual."); +#endif + Visual* pVisual = DefaultVisual( m_pDisplay, DefaultScreen( m_pDisplay ) ); + m_aInfo.screen = DefaultScreen( m_pDisplay ); + m_aInfo.visual = pVisual; + m_aInfo.visualid = pVisual->visualid; + m_aInfo.c_class = pVisual->c_class; + m_aInfo.red_mask = pVisual->red_mask; + m_aInfo.green_mask = pVisual->green_mask; + m_aInfo.blue_mask = pVisual->blue_mask; + m_aInfo.depth = DefaultDepth( m_pDisplay, m_aInfo.screen ); + } + m_aColormap = DefaultColormap( m_pDisplay, m_aInfo.screen ); +#if OSL_DEBUG_LEVEL > 1 + static const char* pClasses[] = + { "StaticGray", "GrayScale", "StaticColor", "PseudoColor", "TrueColor", "DirectColor" }; + SAL_INFO("vcl.unx.dtrans", "PixmapHolder visual: id = " + << std::showbase << std::hex + << m_aInfo.visualid + << ", class = " + << ((m_aInfo.c_class >= 0 && + unsigned(m_aInfo.c_class) < + SAL_N_ELEMENTS(pClasses)) ? + pClasses[m_aInfo.c_class] : + "<unknown>") + << " (" + << std::dec + << m_aInfo.c_class + << "), depth=" + << m_aInfo.depth + << "; color map = " + << std::showbase << std::hex + << m_aColormap); +#endif + if( m_aInfo.c_class == TrueColor ) + { + int nRedShift2(0); + int nGreenShift2(0); + int nBlueShift2(0); + int nRedSig, nGreenSig, nBlueSig; + getShift( m_aInfo.red_mask, m_nRedShift, nRedSig, nRedShift2 ); + getShift( m_aInfo.green_mask, m_nGreenShift, nGreenSig, nGreenShift2 ); + getShift( m_aInfo.blue_mask, m_nBlueShift, nBlueSig, nBlueShift2 ); + + m_nBlueShift2Mask = nBlueShift2 ? ~static_cast<unsigned long>((1<<nBlueShift2)-1) : ~0L; + m_nGreenShift2Mask = nGreenShift2 ? ~static_cast<unsigned long>((1<<nGreenShift2)-1) : ~0L; + m_nRedShift2Mask = nRedShift2 ? ~static_cast<unsigned long>((1<<nRedShift2)-1) : ~0L; + } +} + +PixmapHolder::~PixmapHolder() +{ + if( m_aPixmap != None ) + XFreePixmap( m_pDisplay, m_aPixmap ); + if( m_aBitmap != None ) + XFreePixmap( m_pDisplay, m_aBitmap ); +} + +unsigned long PixmapHolder::getTCPixel( sal_uInt8 r, sal_uInt8 g, sal_uInt8 b ) const +{ + unsigned long nPixel = 0; + unsigned long nValue = static_cast<unsigned long>(b); + nValue &= m_nBlueShift2Mask; + nPixel |= doLeftShift( nValue, m_nBlueShift ); + + nValue = static_cast<unsigned long>(g); + nValue &= m_nGreenShift2Mask; + nPixel |= doLeftShift( nValue, m_nGreenShift ); + + nValue = static_cast<unsigned long>(r); + nValue &= m_nRedShift2Mask; + nPixel |= doLeftShift( nValue, m_nRedShift ); + + return nPixel; +} + +void PixmapHolder::setBitmapDataPalette( const sal_uInt8* pData, XImage* pImage ) +{ + // setup palette + XColor aPalette[256]; + + sal_uInt32 nColors = readLE32( pData+32 ); + sal_uInt32 nWidth = readLE32( pData+4 ); + sal_uInt32 nHeight = readLE32( pData+8 ); + sal_uInt16 nDepth = readLE16( pData+14 ); + + for( sal_uInt32 i = 0 ; i < nColors; i++ ) + { + if( m_aInfo.c_class != TrueColor ) + { + //This is untainted data which comes from a controlled source + //so, using a byte-swapping pattern which coverity doesn't + //detect as such + //http://security.coverity.com/blog/2014/Apr/on-detecting-heartbleed-with-static-analysis.html + aPalette[i].red = static_cast<unsigned short>(pData[42 + i*4]); + aPalette[i].red <<= 8; + aPalette[i].red |= static_cast<unsigned short>(pData[42 + i*4]); + + aPalette[i].green = static_cast<unsigned short>(pData[41 + i*4]); + aPalette[i].green <<= 8; + aPalette[i].green |= static_cast<unsigned short>(pData[41 + i*4]); + + aPalette[i].blue = static_cast<unsigned short>(pData[40 + i*4]); + aPalette[i].blue <<= 8; + aPalette[i].blue |= static_cast<unsigned short>(pData[40 + i*4]); + XAllocColor( m_pDisplay, m_aColormap, aPalette+i ); + } + else + aPalette[i].pixel = getTCPixel( pData[42+i*4], pData[41+i*4], pData[40+i*4] ); + } + const sal_uInt8* pBMData = pData + readLE32( pData ) + 4*nColors; + + sal_uInt32 nScanlineSize = 0; + switch( nDepth ) + { + case 1: + nScanlineSize = (nWidth+31)/32; + break; + case 4: + nScanlineSize = (nWidth+1)/2; + break; + case 8: + nScanlineSize = nWidth; + break; + } + // adjust scan lines to begin on %4 boundaries + if( nScanlineSize & 3 ) + { + nScanlineSize &= 0xfffffffc; + nScanlineSize += 4; + } + + // allocate buffer to hold header and scanlines, initialize to zero + for( unsigned int y = 0; y < nHeight; y++ ) + { + const sal_uInt8* pScanline = pBMData + (nHeight-1-y)*nScanlineSize; + for( unsigned int x = 0; x < nWidth; x++ ) + { + int nCol = 0; + switch( nDepth ) + { + case 1: nCol = (pScanline[ x/8 ] & (0x80 >> (x&7))) != 0 ? 0 : 1; break; + case 4: + if( x & 1 ) + nCol = static_cast<int>(pScanline[ x/2 ] >> 4); + else + nCol = static_cast<int>(pScanline[ x/2 ] & 0x0f); + break; + case 8: nCol = static_cast<int>(pScanline[x]); + } + XPutPixel( pImage, x, y, aPalette[nCol].pixel ); + } + } +} + +void PixmapHolder::setBitmapDataTCDither( const sal_uInt8* pData, XImage* pImage ) +{ + XColor aPalette[216]; + + int nNonAllocs = 0; + + for( int r = 0; r < 6; r++ ) + { + for( int g = 0; g < 6; g++ ) + { + for( int b = 0; b < 6; b++ ) + { + int i = r*36+g*6+b; + aPalette[i].red = r == 5 ? 0xffff : r*10922; + aPalette[i].green = g == 5 ? 0xffff : g*10922; + aPalette[i].blue = b == 5 ? 0xffff : b*10922; + aPalette[i].pixel = 0; + if( ! XAllocColor( m_pDisplay, m_aColormap, aPalette+i ) ) + nNonAllocs++; + } + } + } + + if( nNonAllocs ) + { + XColor aRealPalette[256]; + int nColors = 1 << m_aInfo.depth; + int i; + for( i = 0; i < nColors; i++ ) + aRealPalette[i].pixel = static_cast<unsigned long>(i); + XQueryColors( m_pDisplay, m_aColormap, aRealPalette, nColors ); + for( i = 0; i < nColors; i++ ) + { + sal_uInt8 nIndex = + 36*static_cast<sal_uInt8>(aRealPalette[i].red/10923) + + 6*static_cast<sal_uInt8>(aRealPalette[i].green/10923) + + static_cast<sal_uInt8>(aRealPalette[i].blue/10923); + if( aPalette[nIndex].pixel == 0 ) + aPalette[nIndex] = aRealPalette[i]; + } + } + + sal_uInt32 nWidth = readLE32( pData+4 ); + sal_uInt32 nHeight = readLE32( pData+8 ); + + const sal_uInt8* pBMData = pData + readLE32( pData ); + sal_uInt32 nScanlineSize = nWidth*3; + // adjust scan lines to begin on %4 boundaries + if( nScanlineSize & 3 ) + { + nScanlineSize &= 0xfffffffc; + nScanlineSize += 4; + } + + for( int y = 0; y < static_cast<int>(nHeight); y++ ) + { + const sal_uInt8* pScanline = pBMData + (nHeight-1-static_cast<sal_uInt32>(y))*nScanlineSize; + for( int x = 0; x < static_cast<int>(nWidth); x++ ) + { + sal_uInt8 b = pScanline[3*x]; + sal_uInt8 g = pScanline[3*x+1]; + sal_uInt8 r = pScanline[3*x+2]; + sal_uInt8 i = 36*(r/43) + 6*(g/43) + (b/43); + + XPutPixel( pImage, x, y, aPalette[ i ].pixel ); + } + } +} + +void PixmapHolder::setBitmapDataTC( const sal_uInt8* pData, XImage* pImage ) +{ + sal_uInt32 nWidth = readLE32( pData+4 ); + sal_uInt32 nHeight = readLE32( pData+8 ); + + if (!nWidth || !nHeight) + return; + + const sal_uInt8* pBMData = pData + readLE32( pData ); + sal_uInt32 nScanlineSize = nWidth*3; + // adjust scan lines to begin on %4 boundaries + if( nScanlineSize & 3 ) + { + nScanlineSize &= 0xfffffffc; + nScanlineSize += 4; + } + + for( int y = 0; y < static_cast<int>(nHeight); y++ ) + { + const sal_uInt8* pScanline = pBMData + (nHeight-1-static_cast<sal_uInt32>(y))*nScanlineSize; + for( int x = 0; x < static_cast<int>(nWidth); x++ ) + { + unsigned long nPixel = getTCPixel( pScanline[3*x+2], pScanline[3*x+1], pScanline[3*x] ); + XPutPixel( pImage, x, y, nPixel ); + } + } +} + +bool PixmapHolder::needsConversion( const sal_uInt8* pData ) +{ + if( pData[0] != 'B' || pData[1] != 'M' ) + return true; + + pData = pData+14; + sal_uInt32 nDepth = readLE32( pData+14 ); + if( nDepth == 24 ) + { + if( m_aInfo.c_class != TrueColor ) + return true; + } + else if( nDepth != static_cast<sal_uInt32>(m_aInfo.depth) ) + { + if( m_aInfo.c_class != TrueColor ) + return true; + } + + return false; +} + +Pixmap PixmapHolder::setBitmapData( const sal_uInt8* pData ) +{ + if( pData[0] != 'B' || pData[1] != 'M' ) + return None; + + pData = pData+14; + + // reject compressed data + if( readLE32( pData + 16 ) != 0 ) + return None; + + sal_uInt32 nWidth = readLE32( pData+4 ); + sal_uInt32 nHeight = readLE32( pData+8 ); + + if( m_aPixmap != None ) + { + XFreePixmap( m_pDisplay, m_aPixmap ); + m_aPixmap = None; + } + if( m_aBitmap != None ) + { + XFreePixmap( m_pDisplay, m_aBitmap ); + m_aBitmap = None; + } + + m_aPixmap = limitXCreatePixmap( m_pDisplay, + RootWindow( m_pDisplay, m_aInfo.screen ), + nWidth, nHeight, m_aInfo.depth ); + + if( m_aPixmap != None ) + { + XImage aImage; + aImage.width = static_cast<int>(nWidth); + aImage.height = static_cast<int>(nHeight); + aImage.xoffset = 0; + aImage.format = ZPixmap; + aImage.data = nullptr; + aImage.byte_order = ImageByteOrder( m_pDisplay ); + aImage.bitmap_unit = BitmapUnit( m_pDisplay ); + aImage.bitmap_bit_order = BitmapBitOrder( m_pDisplay ); + aImage.bitmap_pad = BitmapPad( m_pDisplay ); + aImage.depth = m_aInfo.depth; + aImage.red_mask = m_aInfo.red_mask; + aImage.green_mask = m_aInfo.green_mask; + aImage.blue_mask = m_aInfo.blue_mask; + aImage.bytes_per_line = 0; // filled in by XInitImage + if( m_aInfo.depth <= 8 ) + aImage.bits_per_pixel = m_aInfo.depth; + else + aImage.bits_per_pixel = 8*((m_aInfo.depth+7)/8); + aImage.obdata = nullptr; + + XInitImage( &aImage ); + aImage.data = static_cast<char*>(std::malloc( nHeight*aImage.bytes_per_line )); + + if( readLE32( pData+14 ) == 24 ) + { + if( m_aInfo.c_class == TrueColor ) + setBitmapDataTC( pData, &aImage ); + else + setBitmapDataTCDither( pData, &aImage ); + } + else + setBitmapDataPalette( pData, &aImage ); + + // put the image + XPutImage( m_pDisplay, + m_aPixmap, + DefaultGC( m_pDisplay, m_aInfo.screen ), + &aImage, + 0, 0, + 0, 0, + nWidth, nHeight ); + + // clean up + std::free( aImage.data ); + + // prepare bitmap (mask) + m_aBitmap = limitXCreatePixmap( m_pDisplay, + RootWindow( m_pDisplay, m_aInfo.screen ), + nWidth, nHeight, 1 ); + XGCValues aVal; + aVal.function = GXcopy; + aVal.foreground = 0xffffffff; + GC aGC = XCreateGC( m_pDisplay, m_aBitmap, GCFunction | GCForeground, &aVal ); + XFillRectangle( m_pDisplay, m_aBitmap, aGC, 0, 0, nWidth, nHeight ); + XFreeGC( m_pDisplay, aGC ); + } + + return m_aPixmap; +} + +css::uno::Sequence<sal_Int8> x11::convertBitmapDepth( + css::uno::Sequence<sal_Int8> const & data, int depth) +{ + if (depth < 4) { + depth = 1; + } else if (depth < 8) { + depth = 4; + } else if (depth > 8 && depth < 24) { + depth = 24; + } + SolarMutexGuard g; + SvMemoryStream in( + const_cast<sal_Int8 *>(data.getConstArray()), data.getLength(), + StreamMode::READ); + Bitmap bm; + ReadDIB(bm, in, true); + if (bm.GetBitCount() == 24 && depth <= 8) { + bm.Dither(); + } + if (bm.GetBitCount() != depth) { + switch (depth) { + case 1: + bm.Convert(BmpConversion::N1BitThreshold); + break; + case 4: + { + BitmapEx aBmpEx(bm); + BitmapFilter::Filter(aBmpEx, BitmapSimpleColorQuantizationFilter(1<<4)); + bm = aBmpEx.GetBitmap(); + } + break; + + case 8: + { + BitmapEx aBmpEx(bm); + BitmapFilter::Filter(aBmpEx, BitmapSimpleColorQuantizationFilter(1<<8)); + bm = aBmpEx.GetBitmap(); + } + break; + + case 24: + bm.Convert(BmpConversion::N24Bit); + break; + } + } + SvMemoryStream out; + WriteDIB(bm, out, false, true); + return css::uno::Sequence<sal_Int8>( + static_cast<sal_Int8 const *>(out.GetData()), out.GetEndOfData()); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/generic/dtrans/bmp.hxx b/vcl/unx/generic/dtrans/bmp.hxx new file mode 100644 index 000000000..8d24ff41d --- /dev/null +++ b/vcl/unx/generic/dtrans/bmp.hxx @@ -0,0 +1,78 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_VCL_UNX_GENERIC_DTRANS_BMP_HXX +#define INCLUDED_VCL_UNX_GENERIC_DTRANS_BMP_HXX + +#include <X11/Xlib.h> +#include <X11/Xutil.h> + +#include <com/sun/star/uno/Sequence.hxx> +#include <sal/types.h> + +namespace x11 { + +// helper methods +sal_uInt8* X11_getBmpFromPixmap( Display* pDisplay, + Drawable aDrawable, + Colormap aColormap, + sal_Int32& rOutSize ); + +class PixmapHolder +{ + Display* m_pDisplay; + Colormap m_aColormap; + Pixmap m_aPixmap; + Pixmap m_aBitmap; + XVisualInfo m_aInfo; + + int m_nRedShift; + int m_nGreenShift; + int m_nBlueShift; + unsigned long m_nBlueShift2Mask, m_nRedShift2Mask, m_nGreenShift2Mask; + + // these expect data pointers to bitmapinfo header + void setBitmapDataTC( const sal_uInt8* pData, XImage* pImage ); + void setBitmapDataTCDither( const sal_uInt8* pData, XImage* pImage ); + void setBitmapDataPalette( const sal_uInt8* pData, XImage* pImage ); + + unsigned long getTCPixel( sal_uInt8 r, sal_uInt8 g, sal_uInt8 b ) const; +public: + PixmapHolder( Display* pDisplay ); + ~PixmapHolder(); + + // accepts bitmap file (including bitmap file header) + Pixmap setBitmapData( const sal_uInt8* pData ); + bool needsConversion( const sal_uInt8* pData ); + + Colormap getColormap() const { return m_aColormap; } + Pixmap getPixmap() const { return m_aPixmap; } + Pixmap getBitmap() const { return m_aBitmap; } + VisualID getVisualID() const { return m_aInfo.visualid; } + int getDepth() const { return m_aInfo.depth; } +}; + +css::uno::Sequence<sal_Int8> convertBitmapDepth( + css::uno::Sequence<sal_Int8> const & data, int depth); + +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/generic/dtrans/config.cxx b/vcl/unx/generic/dtrans/config.cxx new file mode 100644 index 000000000..6b40fc16c --- /dev/null +++ b/vcl/unx/generic/dtrans/config.cxx @@ -0,0 +1,123 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <o3tl/any.hxx> +#include <sal/log.hxx> +#include <unotools/configitem.hxx> + +#include "X11_selection.hxx" + +#define SETTINGS_CONFIGNODE "VCL/Settings/Transfer" +#define SELECTION_PROPERTY "SelectionTimeout" + +namespace x11 +{ + +namespace { + +class DtransX11ConfigItem : public ::utl::ConfigItem +{ + sal_Int32 m_nSelectionTimeout; + + virtual void Notify( const css::uno::Sequence< OUString >& rPropertyNames ) override; + virtual void ImplCommit() override; + +public: + DtransX11ConfigItem(); + + sal_Int32 getSelectionTimeout() const { return m_nSelectionTimeout; } +}; + +} + +} + +using namespace com::sun::star::lang; +using namespace com::sun::star::uno; +using namespace x11; + +sal_Int32 SelectionManager::getSelectionTimeout() +{ + if( m_nSelectionTimeout < 1 ) + { + DtransX11ConfigItem aCfg; + m_nSelectionTimeout = aCfg.getSelectionTimeout(); +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "initialized selection timeout to " + << m_nSelectionTimeout + << " seconds."); +#endif + } + return m_nSelectionTimeout; +} + +/* + * DtransX11ConfigItem constructor + */ + +DtransX11ConfigItem::DtransX11ConfigItem() : + ConfigItem( SETTINGS_CONFIGNODE, + ConfigItemMode::NONE ), + m_nSelectionTimeout( 3 ) +{ + Sequence<OUString> aKeys { SELECTION_PROPERTY }; + const Sequence< Any > aValues = GetProperties( aKeys ); +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "found " + << aValues.getLength() + << " properties for " + << SELECTION_PROPERTY); +#endif + for( Any const & value : aValues ) + { + if( auto pLine = o3tl::tryAccess<OUString>(value) ) + { + if( !pLine->isEmpty() ) + { + m_nSelectionTimeout = pLine->toInt32(); + if( m_nSelectionTimeout < 1 ) + m_nSelectionTimeout = 1; + } +#if OSL_DEBUG_LEVEL > 1 + SAL_INFO("vcl.unx.dtrans", "found SelectionTimeout \"" << *pLine << "\"."); +#endif + } +#if OSL_DEBUG_LEVEL > 1 + else + SAL_INFO("vcl.unx.dtrans", "found SelectionTimeout of type \"" + << value.getValueType().getTypeName() << "\"."); +#endif + } +} + +void DtransX11ConfigItem::ImplCommit() +{ + // for the clipboard service this is readonly, so + // there is nothing to commit +} + +/* + * DtransX11ConfigItem::Notify + */ + +void DtransX11ConfigItem::Notify( const Sequence< OUString >& /*rPropertyNames*/ ) +{ +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/generic/dtrans/copydata_curs.h b/vcl/unx/generic/dtrans/copydata_curs.h new file mode 100644 index 000000000..4cc36ebde --- /dev/null +++ b/vcl/unx/generic/dtrans/copydata_curs.h @@ -0,0 +1,36 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#define copydata_curs_width 32 +#define copydata_curs_height 32 +#define copydata_curs_x_hot 1 +#define copydata_curs_y_hot 1 +static unsigned char copydata_curs_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, + 0x0e, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, + 0x7e, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x00, 0xfe, 0x01, 0x00, 0x00, + 0xfe, 0x03, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x6e, 0x00, 0x00, 0x00, + 0x66, 0x00, 0x00, 0x00, 0xc2, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, + 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x10, 0x53, 0x00, 0x00, + 0x28, 0xa3, 0x00, 0x00, 0x10, 0x40, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, + 0x10, 0xf0, 0x1f, 0x00, 0x08, 0xf0, 0x1f, 0x00, 0x10, 0xf0, 0x1e, 0x00, + 0xa8, 0xf2, 0x1e, 0x00, 0x50, 0x35, 0x18, 0x00, 0x00, 0xf0, 0x1e, 0x00, + 0x00, 0xf0, 0x1e, 0x00, 0x00, 0xf0, 0x1f, 0x00, 0x00, 0xf0, 0x1f, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/generic/dtrans/copydata_mask.h b/vcl/unx/generic/dtrans/copydata_mask.h new file mode 100644 index 000000000..a3538c952 --- /dev/null +++ b/vcl/unx/generic/dtrans/copydata_mask.h @@ -0,0 +1,36 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#define copydata_mask_width 32 +#define copydata_mask_height 32 +#define copydata_mask_x_hot 1 +#define copydata_mask_y_hot 1 +static unsigned char copydata_mask_bits[] = { + 0x07, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, + 0x3f, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, + 0xff, 0x01, 0x00, 0x00, 0xff, 0x03, 0x00, 0x00, 0xff, 0x07, 0x00, 0x00, + 0xff, 0x07, 0x00, 0x00, 0xff, 0x07, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, + 0xff, 0x01, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0xe7, 0x03, 0x00, 0x00, + 0xe0, 0x03, 0x00, 0x00, 0xf8, 0xff, 0x00, 0x00, 0xfc, 0xff, 0x01, 0x00, + 0xfc, 0xff, 0x01, 0x00, 0xfc, 0xff, 0x01, 0x00, 0x3c, 0xf8, 0x3f, 0x00, + 0x3c, 0xf8, 0x3f, 0x00, 0x3c, 0xf8, 0x3f, 0x00, 0xfc, 0xff, 0x3f, 0x00, + 0xfc, 0xff, 0x3f, 0x00, 0xfc, 0xff, 0x3f, 0x00, 0xf8, 0xff, 0x3f, 0x00, + 0x00, 0xf8, 0x3f, 0x00, 0x00, 0xf8, 0x3f, 0x00, 0x00, 0xf8, 0x3f, 0x00, + 0x00, 0xf8, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/generic/dtrans/linkdata_curs.h b/vcl/unx/generic/dtrans/linkdata_curs.h new file mode 100644 index 000000000..8a4e6db38 --- /dev/null +++ b/vcl/unx/generic/dtrans/linkdata_curs.h @@ -0,0 +1,36 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#define linkdata_curs_width 32 +#define linkdata_curs_height 32 +#define linkdata_curs_x_hot 1 +#define linkdata_curs_y_hot 1 +static unsigned char linkdata_curs_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, + 0x0e, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, + 0x7e, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x00, 0xfe, 0x01, 0x00, 0x00, + 0xfe, 0x03, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x6e, 0x00, 0x00, 0x00, + 0x66, 0x00, 0x00, 0x00, 0xc2, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, + 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x10, 0x53, 0x00, 0x00, + 0x28, 0xa3, 0x00, 0x00, 0x10, 0x40, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, + 0x10, 0xf0, 0x1f, 0x00, 0x08, 0x70, 0x18, 0x00, 0x10, 0xf0, 0x18, 0x00, + 0xa8, 0x72, 0x18, 0x00, 0x50, 0x35, 0x1a, 0x00, 0x00, 0x30, 0x1f, 0x00, + 0x00, 0xb0, 0x1f, 0x00, 0x00, 0x70, 0x1f, 0x00, 0x00, 0xf0, 0x1f, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/generic/dtrans/linkdata_mask.h b/vcl/unx/generic/dtrans/linkdata_mask.h new file mode 100644 index 000000000..a1875a8e0 --- /dev/null +++ b/vcl/unx/generic/dtrans/linkdata_mask.h @@ -0,0 +1,36 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#define linkdata_mask_width 32 +#define linkdata_mask_height 32 +#define linkdata_mask_x_hot 1 +#define linkdata_mask_y_hot 1 +static unsigned char linkdata_mask_bits[] = { + 0x07, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, + 0x3f, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, + 0xff, 0x01, 0x00, 0x00, 0xff, 0x03, 0x00, 0x00, 0xff, 0x07, 0x00, 0x00, + 0xff, 0x07, 0x00, 0x00, 0xff, 0x07, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, + 0xff, 0x01, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0xe7, 0x03, 0x00, 0x00, + 0xe0, 0x03, 0x00, 0x00, 0xf8, 0xff, 0x00, 0x00, 0xfc, 0xff, 0x01, 0x00, + 0xfc, 0xff, 0x01, 0x00, 0xfc, 0xff, 0x01, 0x00, 0x3c, 0xf8, 0x3f, 0x00, + 0x3c, 0xf8, 0x3f, 0x00, 0x3c, 0xf8, 0x3f, 0x00, 0xfc, 0xff, 0x3f, 0x00, + 0xfc, 0xff, 0x3f, 0x00, 0xfc, 0xff, 0x3f, 0x00, 0xf8, 0xff, 0x3f, 0x00, + 0x00, 0xf8, 0x3f, 0x00, 0x00, 0xf8, 0x3f, 0x00, 0x00, 0xf8, 0x3f, 0x00, + 0x00, 0xf8, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/generic/dtrans/movedata_curs.h b/vcl/unx/generic/dtrans/movedata_curs.h new file mode 100644 index 000000000..b253ce70c --- /dev/null +++ b/vcl/unx/generic/dtrans/movedata_curs.h @@ -0,0 +1,36 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#define movedata_curs_width 32 +#define movedata_curs_height 32 +#define movedata_curs_x_hot 1 +#define movedata_curs_y_hot 1 +static unsigned char movedata_curs_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, + 0x0e, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, + 0x7e, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x00, 0xfe, 0x01, 0x00, 0x00, + 0xfe, 0x03, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x6e, 0x00, 0x00, 0x00, + 0x66, 0x00, 0x00, 0x00, 0xc2, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, + 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x10, 0x53, 0x00, 0x00, + 0x28, 0xa3, 0x00, 0x00, 0x10, 0x40, 0x00, 0x00, 0x08, 0x80, 0x00, 0x00, + 0x10, 0x40, 0x00, 0x00, 0x08, 0x80, 0x00, 0x00, 0x10, 0x40, 0x00, 0x00, + 0xa8, 0xaa, 0x00, 0x00, 0x50, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/generic/dtrans/movedata_mask.h b/vcl/unx/generic/dtrans/movedata_mask.h new file mode 100644 index 000000000..d317b1556 --- /dev/null +++ b/vcl/unx/generic/dtrans/movedata_mask.h @@ -0,0 +1,36 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#define movedata_mask_width 32 +#define movedata_mask_height 32 +#define movedata_mask_x_hot 1 +#define movedata_mask_y_hot 1 +static unsigned char movedata_mask_bits[] = { + 0x07, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, + 0x3f, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, + 0xff, 0x01, 0x00, 0x00, 0xff, 0x03, 0x00, 0x00, 0xff, 0x07, 0x00, 0x00, + 0xff, 0x07, 0x00, 0x00, 0xff, 0x07, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, + 0xff, 0x01, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0xe7, 0x03, 0x00, 0x00, + 0xe0, 0x03, 0x00, 0x00, 0xf8, 0xff, 0x00, 0x00, 0xfc, 0xff, 0x01, 0x00, + 0xfc, 0xff, 0x01, 0x00, 0xfc, 0xff, 0x01, 0x00, 0x3c, 0xe0, 0x01, 0x00, + 0x3c, 0xe0, 0x01, 0x00, 0x3c, 0xe0, 0x01, 0x00, 0xfc, 0xff, 0x01, 0x00, + 0xfc, 0xff, 0x01, 0x00, 0xfc, 0xff, 0x01, 0x00, 0xf8, 0xff, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/generic/dtrans/nodrop_curs.h b/vcl/unx/generic/dtrans/nodrop_curs.h new file mode 100644 index 000000000..958257518 --- /dev/null +++ b/vcl/unx/generic/dtrans/nodrop_curs.h @@ -0,0 +1,36 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#define nodrop_curs_width 32 +#define nodrop_curs_height 32 +#define nodrop_curs_x_hot 9 +#define nodrop_curs_y_hot 9 +static unsigned char nodrop_curs_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0xc0, 0x0f, 0x00, 0x00, 0xf0, 0x3f, 0x00, 0x00, + 0xf8, 0x7f, 0x00, 0x00, 0x7c, 0xf8, 0x00, 0x00, 0x1c, 0xfc, 0x00, 0x00, + 0x1e, 0xfe, 0x01, 0x00, 0x0e, 0xdf, 0x01, 0x00, 0x8e, 0xcf, 0x01, 0x00, + 0xce, 0xc7, 0x01, 0x00, 0xee, 0xc3, 0x01, 0x00, 0xfe, 0xe1, 0x01, 0x00, + 0xfc, 0xe0, 0x00, 0x00, 0x7c, 0xf8, 0x00, 0x00, 0xf8, 0x7f, 0x00, 0x00, + 0xf0, 0x3f, 0x00, 0x00, 0xc0, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/generic/dtrans/nodrop_mask.h b/vcl/unx/generic/dtrans/nodrop_mask.h new file mode 100644 index 000000000..662a30064 --- /dev/null +++ b/vcl/unx/generic/dtrans/nodrop_mask.h @@ -0,0 +1,36 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#define nodrop_mask_width 32 +#define nodrop_mask_height 32 +#define nodrop_mask_x_hot 9 +#define nodrop_mask_y_hot 9 +static unsigned char nodrop_mask_bits[] = { + 0xc0, 0x0f, 0x00, 0x00, 0xf0, 0x3f, 0x00, 0x00, 0xf8, 0x7f, 0x00, 0x00, + 0xfc, 0xff, 0x00, 0x00, 0xfe, 0xff, 0x01, 0x00, 0x7e, 0xfe, 0x01, 0x00, + 0x3f, 0xff, 0x03, 0x00, 0x9f, 0xff, 0x03, 0x00, 0xdf, 0xff, 0x03, 0x00, + 0xff, 0xef, 0x03, 0x00, 0xff, 0xe7, 0x03, 0x00, 0xff, 0xf3, 0x03, 0x00, + 0xfe, 0xf9, 0x01, 0x00, 0xfe, 0xff, 0x01, 0x00, 0xfc, 0xff, 0x00, 0x00, + 0xf8, 0x7f, 0x00, 0x00, 0xf0, 0x3f, 0x00, 0x00, 0xc0, 0x0f, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |