x11_dragdrop.cpp 6.84 KB
Newer Older
Cyril Deguet's avatar
Cyril Deguet committed
1
2
3
4
/*****************************************************************************
 * x11_dragdrop.cpp: X11 implementation of the drag & drop
 *****************************************************************************
 * Copyright (C) 2003 VideoLAN
5
 * $Id: x11_dragdrop.cpp,v 1.4 2003/06/09 00:32:58 asmax Exp $
Cyril Deguet's avatar
Cyril Deguet committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 *
 * Authors: Cyril Deguet     <asmax@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111,
 * USA.
 *****************************************************************************/

#ifdef X11_SKINS

//--- X11 -------------------------------------------------------------------
#include <X11/Xlib.h>
29
#include <X11/Xatom.h>
Cyril Deguet's avatar
Cyril Deguet committed
30
31
32
33
34

//--- VLC -------------------------------------------------------------------
#include <vlc/intf.h>

//--- SKIN ------------------------------------------------------------------
35
#include "../src/skin_common.h"
Cyril Deguet's avatar
Cyril Deguet committed
36
37
#include "../src/event.h"
#include "../os_api.h"
38
39
#include "../src/theme.h"
#include "../os_theme.h"
Cyril Deguet's avatar
Cyril Deguet committed
40
41
#include "x11_dragdrop.h"

42
43
#include <string.h>

Cyril Deguet's avatar
Cyril Deguet committed
44
45

//---------------------------------------------------------------------------
46
X11DropObject::X11DropObject( intf_thread_t *_p_intf, Window win)
Cyril Deguet's avatar
Cyril Deguet committed
47
48
{
    p_intf = _p_intf;
49
50
    Win = win;
    display = p_intf->p_sys->display;
Cyril Deguet's avatar
Cyril Deguet committed
51
52
53
54
55
56
}
//---------------------------------------------------------------------------
X11DropObject::~X11DropObject()
{
}
//---------------------------------------------------------------------------
57
58
void X11DropObject::DndEnter( ldata_t data )
{
59
    Window src = data[0];
Cyril Deguet's avatar
Cyril Deguet committed
60

61
62
63
    // Retrieve available data types
    list<string> dataTypes;
    if( data[1] & 1 )   // more than 3 data types ?
Cyril Deguet's avatar
Cyril Deguet committed
64
    {
65
66
67
68
69
70
71
72
73
74
75
        Atom type;
        int format;
        unsigned long nitems, nbytes;
        Atom *dataList;
        XLOCK;
        Atom typeListAtom = XInternAtom( display, "XdndTypeList", 0 );
        XGetWindowProperty( display, src, typeListAtom, 0, 65536, False,
                            XA_ATOM, &type, &format, &nitems, &nbytes,  
                            (unsigned char**)&dataList );
        XUNLOCK;
        for( unsigned long i=0; i<nitems; i++ )
Cyril Deguet's avatar
Cyril Deguet committed
76
        {
77
78
79
80
81
82
83
84
85
86
87
88
            XLOCK;
            string dataType = XGetAtomName( display, dataList[i] );
            XUNLOCK;
            dataTypes.push_back( dataType );
        }
        XFree( (void*)dataList );
    }
    else
    {
        for( int i=2; i<5; i++ )
        {
            if( data[i] != None )
Cyril Deguet's avatar
Cyril Deguet committed
89
            {
90
91
92
93
                XLOCK;
                string dataType = XGetAtomName( display, data[i] );
                XUNLOCK;
                dataTypes.push_back( dataType );
Cyril Deguet's avatar
Cyril Deguet committed
94
95
96
97
            }
        }
    }

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    // Find the right target
    target = None;
    list<string>::iterator it;
    for( it = dataTypes.begin(); it != dataTypes.end(); it++ )
    {
        if( *it == "text/plain" || *it == "STRING" )
        {
            XLOCK;
            target = XInternAtom( display, (*it).c_str(), 0 );
            XUNLOCK;
            break;
        }
    }
}
//---------------------------------------------------------------------------
void X11DropObject::DndPosition( ldata_t data )
{   
    Window src = data[0];
    
    XLOCK;
    Atom actionAtom = XInternAtom( display, "XdndActionCopy", 0 );
    Atom typeAtom = XInternAtom( display, "XdndStatus", 0 );

    XEvent event;
    event.type = ClientMessage;
    event.xclient.window = src;
    event.xclient.display = display;
    event.xclient.message_type = typeAtom;
    event.xclient.format = 32;
    event.xclient.data.l[0] = Win;
    if( target != None )
Cyril Deguet's avatar
Cyril Deguet committed
129
    {
130
        event.xclient.data.l[1] = 1;          // accept the drop
Cyril Deguet's avatar
Cyril Deguet committed
131
132
133
    }
    else
    {
134
        event.xclient.data.l[1] = 0;          // do not accept
Cyril Deguet's avatar
Cyril Deguet committed
135
    }
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    event.xclient.data.l[2] = 0;              // empty rectangle
    event.xclient.data.l[3] = 0;
    event.xclient.data.l[4] = actionAtom;
 
    // Tell the source whether we accept the drop
    XSendEvent( display, src, False, 0, &event );
    XUNLOCK;
}
//---------------------------------------------------------------------------
void X11DropObject::DndLeave( ldata_t data )
{
}
//---------------------------------------------------------------------------
void X11DropObject::DndDrop( ldata_t data )
{
    Window src = data[0];
    Time time = data[2];
Cyril Deguet's avatar
Cyril Deguet committed
153

154
155
156
157
158
159
160
    XLOCK;
    Atom selectionAtom = XInternAtom( display, "XdndSelection", 0 );
    Atom targetAtom = XInternAtom( display, "text/plain", 0 );
    Atom propAtom = XInternAtom( display, "VLC_SELECTION", 0 );
   
    Atom actionAtom = XInternAtom( display, "XdndActionCopy", 0 );
    Atom typeAtom = XInternAtom( display, "XdndFinished", 0 );
Cyril Deguet's avatar
Cyril Deguet committed
161

162
163
164
    // Convert the selection into the given target
    XConvertSelection( display, selectionAtom, targetAtom, propAtom, src, 
                       time );
Cyril Deguet's avatar
Cyril Deguet committed
165

166
167
168
169
170
171
172
173
    // Read the selection 
    Atom type;
    int format;
    unsigned long nitems, nbytes;
    char *buffer;
    XGetWindowProperty( display, src, propAtom, 0, 1024, False, 
                        AnyPropertyType, &type, &format, &nitems, &nbytes, 
                        (unsigned char**)&buffer );
174
175
176
177
178
    string selection = "";
    if( buffer != NULL )
    {
        selection = buffer;
    }
179
180
    XFree( buffer );
    XUNLOCK;
181
182
 
    if( selection != "" )
183
    {
184
185
186
187
188
189
190
191
192
193
194
195
        // TODO: multiple files handling
        string::size_type end = selection.find( "\n", 0 );
        selection = selection.substr( 0, end -1 );     
        end = selection.find( "\r", 0 );
        selection = selection.substr( 0, end -1 );

        // Find the protocol, if any
        string::size_type pos = selection.find( ":", 0 );
        if( selection.find("///", pos + 1 ) == pos + 1 )
        {
            selection.erase( pos + 1, 2 );
        }
196
    
197
198
199
200
201
        char *name = new char[selection.size()+1];
        strncpy( name, selection.c_str(), selection.size()+1 );
        msg_Dbg( p_intf, "drop: %s\n", name );
        OSAPI_PostMessage( NULL, VLC_DROP, (unsigned int)name, 0 );
    }
202
203
204
205
206
207
208
209
210
211
212
213
214
215
    
    // Tell the source we accepted the drop
    XEvent event;
    event.type = ClientMessage;
    event.xclient.window = src;
    event.xclient.display = display;
    event.xclient.message_type = typeAtom;
    event.xclient.format = 32;
    event.xclient.data.l[0] = Win;
    event.xclient.data.l[1] = 1;          // drop accepted
    event.xclient.data.l[2] = actionAtom;
    XLOCK;
    XSendEvent( display, src, False, 0, &event );
    XUNLOCK;
Cyril Deguet's avatar
Cyril Deguet committed
216
}
217

Cyril Deguet's avatar
Cyril Deguet committed
218
#endif