1 /*****************************************************************************
2 * x11_dragdrop.cpp: X11 implementation of the drag & drop
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: x11_dragdrop.cpp,v 1.4 2003/06/09 00:32:58 asmax Exp $
7 * Authors: Cyril Deguet <asmax@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111,
23 *****************************************************************************/
27 //--- X11 -------------------------------------------------------------------
29 #include <X11/Xatom.h>
31 //--- VLC -------------------------------------------------------------------
34 //--- SKIN ------------------------------------------------------------------
35 #include "../src/skin_common.h"
36 #include "../src/event.h"
37 #include "../os_api.h"
38 #include "../src/theme.h"
39 #include "../os_theme.h"
40 #include "x11_dragdrop.h"
45 //---------------------------------------------------------------------------
46 X11DropObject::X11DropObject( intf_thread_t *_p_intf, Window win)
50 display = p_intf->p_sys->display;
52 //---------------------------------------------------------------------------
53 X11DropObject::~X11DropObject()
56 //---------------------------------------------------------------------------
57 void X11DropObject::DndEnter( ldata_t data )
61 // Retrieve available data types
62 list<string> dataTypes;
63 if( data[1] & 1 ) // more than 3 data types ?
67 unsigned long nitems, nbytes;
70 Atom typeListAtom = XInternAtom( display, "XdndTypeList", 0 );
71 XGetWindowProperty( display, src, typeListAtom, 0, 65536, False,
72 XA_ATOM, &type, &format, &nitems, &nbytes,
73 (unsigned char**)&dataList );
75 for( unsigned long i=0; i<nitems; i++ )
78 string dataType = XGetAtomName( display, dataList[i] );
80 dataTypes.push_back( dataType );
82 XFree( (void*)dataList );
86 for( int i=2; i<5; i++ )
91 string dataType = XGetAtomName( display, data[i] );
93 dataTypes.push_back( dataType );
98 // Find the right target
100 list<string>::iterator it;
101 for( it = dataTypes.begin(); it != dataTypes.end(); it++ )
103 if( *it == "text/plain" || *it == "STRING" )
106 target = XInternAtom( display, (*it).c_str(), 0 );
112 //---------------------------------------------------------------------------
113 void X11DropObject::DndPosition( ldata_t data )
115 Window src = data[0];
118 Atom actionAtom = XInternAtom( display, "XdndActionCopy", 0 );
119 Atom typeAtom = XInternAtom( display, "XdndStatus", 0 );
122 event.type = ClientMessage;
123 event.xclient.window = src;
124 event.xclient.display = display;
125 event.xclient.message_type = typeAtom;
126 event.xclient.format = 32;
127 event.xclient.data.l[0] = Win;
130 event.xclient.data.l[1] = 1; // accept the drop
134 event.xclient.data.l[1] = 0; // do not accept
136 event.xclient.data.l[2] = 0; // empty rectangle
137 event.xclient.data.l[3] = 0;
138 event.xclient.data.l[4] = actionAtom;
140 // Tell the source whether we accept the drop
141 XSendEvent( display, src, False, 0, &event );
144 //---------------------------------------------------------------------------
145 void X11DropObject::DndLeave( ldata_t data )
148 //---------------------------------------------------------------------------
149 void X11DropObject::DndDrop( ldata_t data )
151 Window src = data[0];
155 Atom selectionAtom = XInternAtom( display, "XdndSelection", 0 );
156 Atom targetAtom = XInternAtom( display, "text/plain", 0 );
157 Atom propAtom = XInternAtom( display, "VLC_SELECTION", 0 );
159 Atom actionAtom = XInternAtom( display, "XdndActionCopy", 0 );
160 Atom typeAtom = XInternAtom( display, "XdndFinished", 0 );
162 // Convert the selection into the given target
163 XConvertSelection( display, selectionAtom, targetAtom, propAtom, src,
166 // Read the selection
169 unsigned long nitems, nbytes;
171 XGetWindowProperty( display, src, propAtom, 0, 1024, False,
172 AnyPropertyType, &type, &format, &nitems, &nbytes,
173 (unsigned char**)&buffer );
174 string selection = "";
182 if( selection != "" )
184 // TODO: multiple files handling
185 string::size_type end = selection.find( "\n", 0 );
186 selection = selection.substr( 0, end -1 );
187 end = selection.find( "\r", 0 );
188 selection = selection.substr( 0, end -1 );
190 // Find the protocol, if any
191 string::size_type pos = selection.find( ":", 0 );
192 if( selection.find("///", pos + 1 ) == pos + 1 )
194 selection.erase( pos + 1, 2 );
197 char *name = new char[selection.size()+1];
198 strncpy( name, selection.c_str(), selection.size()+1 );
199 msg_Dbg( p_intf, "drop: %s\n", name );
200 OSAPI_PostMessage( NULL, VLC_DROP, (unsigned int)name, 0 );
203 // Tell the source we accepted the drop
205 event.type = ClientMessage;
206 event.xclient.window = src;
207 event.xclient.display = display;
208 event.xclient.message_type = typeAtom;
209 event.xclient.format = 32;
210 event.xclient.data.l[0] = Win;
211 event.xclient.data.l[1] = 1; // drop accepted
212 event.xclient.data.l[2] = actionAtom;
214 XSendEvent( display, src, False, 0, &event );