]> git.sesse.net Git - vlc/blob - modules/gui/skins/x11/x11_dragdrop.cpp
df2be434b78017fdcafe7242ad8aab7ea42c2dde
[vlc] / modules / gui / skins / x11 / x11_dragdrop.cpp
1 /*****************************************************************************
2  * x11_dragdrop.cpp: X11 implementation of the drag & drop
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: x11_dragdrop.cpp,v 1.6 2003/06/22 15:07:13 asmax Exp $
6  *
7  * Authors: Cyril Deguet     <asmax@videolan.org>
8  *
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.
13  *
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.
18  *
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,
22  * USA.
23  *****************************************************************************/
24
25 #ifdef X11_SKINS
26
27 //--- X11 -------------------------------------------------------------------
28 #include <X11/Xlib.h>
29 #include <X11/Xatom.h>
30
31 //--- VLC -------------------------------------------------------------------
32 #include <vlc/intf.h>
33
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"
41
42 #include <string.h>
43
44
45 //---------------------------------------------------------------------------
46 X11DropObject::X11DropObject( intf_thread_t *_p_intf, Window win)
47 {
48     p_intf = _p_intf;
49     Win = win;
50     display = p_intf->p_sys->display;
51 }
52 //---------------------------------------------------------------------------
53 X11DropObject::~X11DropObject()
54 {
55 }
56 //---------------------------------------------------------------------------
57 void X11DropObject::DndEnter( ldata_t data )
58 {
59     Window src = data[0];
60
61     // Retrieve available data types
62     list<string> dataTypes;
63     if( data[1] & 1 )   // more than 3 data types ?
64     {
65         Atom type;
66         int format;
67         unsigned long nitems, nbytes;
68         Atom *dataList;
69         XLOCK;
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 );
74         XUNLOCK;
75         for( unsigned long i=0; i<nitems; i++ )
76         {
77             XLOCK;
78             string dataType = XGetAtomName( display, dataList[i] );
79             XUNLOCK;
80             dataTypes.push_back( dataType );
81         }
82         XFree( (void*)dataList );
83     }
84     else
85     {
86         for( int i=2; i<5; i++ )
87         {
88             if( data[i] != None )
89             {
90                 XLOCK;
91                 string dataType = XGetAtomName( display, data[i] );
92                 XUNLOCK;
93                 dataTypes.push_back( dataType );
94             }
95         }
96     }
97
98     // Find the right target
99     target = None;
100     list<string>::iterator it;
101     for( it = dataTypes.begin(); it != dataTypes.end(); it++ )
102     {
103         if( *it == "text/plain" || *it == "STRING" )
104         {
105             XLOCK;
106             target = XInternAtom( display, (*it).c_str(), 0 );
107             XUNLOCK;
108             break;
109         }
110     }
111 }
112 //---------------------------------------------------------------------------
113 void X11DropObject::DndPosition( ldata_t data )
114 {   
115     Window src = data[0];
116     Time time = data[2];
117     
118     XLOCK;
119     Atom selectionAtom = XInternAtom( display, "XdndSelection", 0 );
120     Atom targetAtom = XInternAtom( display, "text/plain", 0 );
121     Atom propAtom = XInternAtom( display, "VLC_SELECTION", 0 );
122    
123     Atom actionAtom = XInternAtom( display, "XdndActionCopy", 0 );
124     Atom typeAtom = XInternAtom( display, "XdndFinished", 0 );
125
126     // Convert the selection into the given target
127     // NEEDED or it doesn't work !!!
128     XConvertSelection( display, selectionAtom, targetAtom, propAtom, src, 
129                        time );
130  
131     actionAtom = XInternAtom( display, "XdndActionCopy", 0 );
132     typeAtom = XInternAtom( display, "XdndStatus", 0 );
133
134     XEvent event;
135     event.type = ClientMessage;
136     event.xclient.window = src;
137     event.xclient.display = display;
138     event.xclient.message_type = typeAtom;
139     event.xclient.format = 32;
140     event.xclient.data.l[0] = Win;
141     if( target != None )
142     {
143         event.xclient.data.l[1] = 1;          // accept the drop
144     }
145     else
146     {
147         event.xclient.data.l[1] = 0;          // do not accept
148     }
149     int w, h;
150     OSAPI_GetScreenSize( w, h );
151     event.xclient.data.l[2] = 0;
152     event.xclient.data.l[3] = (w << 16) | h;
153     event.xclient.data.l[4] = actionAtom;
154  
155     // Tell the source whether we accept the drop
156     XSendEvent( display, src, False, 0, &event );
157     XUNLOCK;
158 }
159 //---------------------------------------------------------------------------
160 void X11DropObject::DndLeave( ldata_t data )
161 {
162 }
163 //---------------------------------------------------------------------------
164 void X11DropObject::DndDrop( ldata_t data )
165 {
166     Window src = data[0];
167     Time time = data[2];
168
169     XLOCK;
170     Atom selectionAtom = XInternAtom( display, "XdndSelection", 0 );
171     Atom targetAtom = XInternAtom( display, "text/plain", 0 );
172     Atom propAtom = XInternAtom( display, "VLC_SELECTION", 0 );
173    
174     Atom actionAtom = XInternAtom( display, "XdndActionCopy", 0 );
175     Atom typeAtom = XInternAtom( display, "XdndFinished", 0 );
176
177     // Convert the selection into the given target
178     XConvertSelection( display, selectionAtom, targetAtom, propAtom, src, 
179                        time );
180
181     // Read the selection 
182     Atom type;
183     int format;
184     unsigned long nitems, nbytes;
185     char *buffer;
186     XGetWindowProperty( display, src, propAtom, 0, 1024, False, 
187                         AnyPropertyType, &type, &format, &nitems, &nbytes, 
188                         (unsigned char**)&buffer );
189     string selection = "";
190     if( buffer != NULL )
191     {
192         selection = buffer;
193     }
194     XFree( buffer );
195     XUNLOCK;
196  
197     if( selection != "" )
198     {
199         // TODO: multiple files handling
200         string::size_type end = selection.find( "\n", 0 );
201         selection = selection.substr( 0, end -1 );     
202         end = selection.find( "\r", 0 );
203         selection = selection.substr( 0, end -1 );
204
205         // Find the protocol, if any
206         string::size_type pos = selection.find( ":", 0 );
207         if( selection.find("///", pos + 1 ) == pos + 1 )
208         {
209             selection.erase( pos + 1, 2 );
210         }
211     
212         char *name = new char[selection.size()+1];
213         strncpy( name, selection.c_str(), selection.size()+1 );
214         OSAPI_PostMessage( NULL, VLC_DROP, (unsigned int)name, 0 );
215     }
216     
217     // Tell the source we accepted the drop
218     XEvent event;
219     event.type = ClientMessage;
220     event.xclient.window = src;
221     event.xclient.display = display;
222     event.xclient.message_type = typeAtom;
223     event.xclient.format = 32;
224     event.xclient.data.l[0] = Win;
225     event.xclient.data.l[1] = 1;          // drop accepted
226     event.xclient.data.l[2] = actionAtom;
227     XLOCK;
228     XSendEvent( display, src, False, 0, &event );
229     XUNLOCK;
230 }
231
232 #endif