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