1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
28 #include <X11/Xatom.h>
30 #include "x11_dragdrop.hpp"
31 #include "x11_display.hpp"
32 #include "x11_factory.hpp"
33 #include "../commands/cmd_add_item.hpp"
39 X11DragDrop::X11DragDrop( intf_thread_t *pIntf, X11Display &rDisplay,
40 Window win, bool playOnDrop ):
41 SkinObject( pIntf ), m_rDisplay( rDisplay ), m_wnd( win ),
42 m_playOnDrop( playOnDrop )
47 void X11DragDrop::dndEnter( ldata_t data )
51 // Retrieve available data types
52 list<string> dataTypes;
53 if( data[1] & 1 ) // More than 3 data types ?
57 unsigned long nitems, nbytes;
59 Atom typeListAtom = XInternAtom( XDISPLAY, "XdndTypeList", 0 );
60 XGetWindowProperty( XDISPLAY, src, typeListAtom, 0, 65536, False,
61 XA_ATOM, &type, &format, &nitems, &nbytes,
62 (unsigned char**)&dataList );
63 for( unsigned long i=0; i<nitems; i++ )
65 string dataType = XGetAtomName( XDISPLAY, dataList[i] );
66 dataTypes.push_back( dataType );
68 XFree( (void*)dataList );
72 for( int i = 2; i < 5; i++ )
76 string dataType = XGetAtomName( XDISPLAY, data[i] );
77 dataTypes.push_back( dataType );
82 // Find the right target
84 list<string>::iterator it;
85 for( it = dataTypes.begin(); it != dataTypes.end(); it++ )
87 if( *it == "text/plain" || *it == "STRING" )
89 m_target = XInternAtom( XDISPLAY, (*it).c_str(), 0 );
96 void X11DragDrop::dndPosition( ldata_t data )
101 Atom selectionAtom = XInternAtom( XDISPLAY, "XdndSelection", 0 );
102 Atom targetAtom = XInternAtom( XDISPLAY, "text/plain", 0 );
103 Atom propAtom = XInternAtom( XDISPLAY, "VLC_SELECTION", 0 );
105 Atom actionAtom = XInternAtom( XDISPLAY, "XdndActionCopy", 0 );
106 Atom typeAtom = XInternAtom( XDISPLAY, "XdndFinished", 0 );
108 // Convert the selection into the given target
109 // NEEDED or it doesn't work!
110 XConvertSelection( XDISPLAY, selectionAtom, targetAtom, propAtom, src,
113 actionAtom = XInternAtom( XDISPLAY, "XdndActionCopy", 0 );
114 typeAtom = XInternAtom( XDISPLAY, "XdndStatus", 0 );
117 event.type = ClientMessage;
118 event.xclient.window = src;
119 event.xclient.display = XDISPLAY;
120 event.xclient.message_type = typeAtom;
121 event.xclient.format = 32;
122 event.xclient.data.l[0] = m_wnd;
123 // Accept the drop (1), or not (0).
124 event.xclient.data.l[1] = m_target != None ? 1 : 0;
125 OSFactory *pOsFactory = X11Factory::instance( getIntf() );
126 int w = pOsFactory->getScreenWidth();
127 int h = pOsFactory->getScreenHeight();
128 event.xclient.data.l[2] = 0;
129 event.xclient.data.l[3] = (w << 16) | h;
130 event.xclient.data.l[4] = actionAtom;
132 // Tell the source whether we accept the drop
133 XSendEvent( XDISPLAY, src, False, 0, &event );
137 void X11DragDrop::dndLeave( ldata_t data )
142 void X11DragDrop::dndDrop( ldata_t data )
144 Window src = data[0];
147 Atom selectionAtom = XInternAtom( XDISPLAY, "XdndSelection", 0 );
148 Atom targetAtom = XInternAtom( XDISPLAY, "text/plain", 0 );
149 Atom propAtom = XInternAtom( XDISPLAY, "VLC_SELECTION", 0 );
151 Atom actionAtom = XInternAtom( XDISPLAY, "XdndActionCopy", 0 );
152 Atom typeAtom = XInternAtom( XDISPLAY, "XdndFinished", 0 );
154 // Convert the selection into the given target
155 XConvertSelection( XDISPLAY, selectionAtom, targetAtom, propAtom, src,
158 // Read the selection
161 unsigned long nitems, nbytes;
163 XGetWindowProperty( XDISPLAY, src, propAtom, 0, 1024, False,
164 AnyPropertyType, &type, &format, &nitems, &nbytes,
165 (unsigned char**)&buffer );
169 char* psz_dup = strdup( buffer );
170 char* psz_new = psz_dup;
171 while( psz_new && *psz_new )
173 char* psz_end = strchr( psz_new, '\n' );
177 CmdAddItem cmd( getIntf(), psz_new, m_playOnDrop );
180 psz_new = psz_end ? psz_end + 1 : NULL;
186 // Tell the source we accepted the drop
188 event.type = ClientMessage;
189 event.xclient.window = src;
190 event.xclient.display = XDISPLAY;
191 event.xclient.message_type = typeAtom;
192 event.xclient.format = 32;
193 event.xclient.data.l[0] = m_wnd;
194 event.xclient.data.l[1] = 1; // drop accepted
195 event.xclient.data.l[2] = actionAtom;
196 XSendEvent( XDISPLAY, src, False, 0, &event );