x11_window.cpp 6.63 KB
Newer Older
1
2
3
/*****************************************************************************
 * x11_window.cpp
 *****************************************************************************
4
 * Copyright (C) 2003 the VideoLAN team
5
 * $Id$
6
7
 *
 * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8
 *          Olivier Teulière <ipkiss@via.ecp.fr>
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 * 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
dionoea's avatar
dionoea committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23
24
25
26
27
28
29
 *****************************************************************************/

#ifdef X11_SKINS

#include <X11/Xatom.h>

#include "../src/generic_window.hpp"
30
#include "../src/vlcproc.hpp"
31
32
33
34
35
36
37
38
#include "x11_window.hpp"
#include "x11_display.hpp"
#include "x11_graphics.hpp"
#include "x11_dragdrop.hpp"
#include "x11_factory.hpp"


X11Window::X11Window( intf_thread_t *pIntf, GenericWindow &rWindow,
39
40
                      X11Display &rDisplay, bool dragDrop, bool playOnDrop,
                      X11Window *pParentWindow ):
41
42
    OSWindow( pIntf ), m_rDisplay( rDisplay ), m_pParent( pParentWindow ),
    m_dragDrop( dragDrop )
43
{
44
45
46
47
48
49
50
51
52
    Window parent;
    if (pParentWindow)
    {
        parent = pParentWindow->m_wnd;
    }
    else
    {
        parent = DefaultRootWindow( XDISPLAY );
    }
53
54
55
    XSetWindowAttributes attr;

    // Create the window
56
    m_wnd = XCreateWindow( XDISPLAY, parent, 0, 0, 1, 1, 0, 0,
57
58
                           InputOutput, CopyFromParent, 0, &attr );

59
60
61
62
63
64
    // Set the colormap for 8bpp mode
    if( XPIXELSIZE == 1 )
    {
        XSetWindowColormap( XDISPLAY, m_wnd, m_rDisplay.getColormap() );
    }

65
    // Select events received by the window
66
67
    XSelectInput( XDISPLAY, m_wnd, ExposureMask|KeyPressMask|
                  PointerMotionMask|ButtonPressMask|ButtonReleaseMask|
68
                  LeaveWindowMask|FocusChangeMask );
69
70
71
72
73
74
75

    // Store a pointer on the generic window in a map
    X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
    pFactory->m_windowMap[m_wnd] = &rWindow;

    // Changing decorations
    struct {
76
77
78
79
80
        uint32_t flags;
        uint32_t functions;
        uint32_t decorations;
        int32_t  input_mode;
        uint32_t status;
81
82
83
84
85
86
    } motifWmHints;
    Atom hints_atom = XInternAtom( XDISPLAY, "_MOTIF_WM_HINTS", False );
    motifWmHints.flags = 2;    // MWM_HINTS_DECORATIONS;
    motifWmHints.decorations = 0;
    XChangeProperty( XDISPLAY, m_wnd, hints_atom, hints_atom, 32,
                     PropModeReplace, (unsigned char *)&motifWmHints,
87
                     sizeof( motifWmHints ) / sizeof( uint32_t ) );
88
89
90
91

    // Drag & drop
    if( m_dragDrop )
    {
92
        // Create a Dnd object for this window
93
94
95
        m_pDropTarget = new X11DragDrop( getIntf(), m_rDisplay, m_wnd,
                                         playOnDrop );

96
        // Register the window as a drop target
97
98
99
100
101
        Atom xdndAtom = XInternAtom( XDISPLAY, "XdndAware", False );
        char xdndVersion = 4;
        XChangeProperty( XDISPLAY, m_wnd, xdndAtom, XA_ATOM, 32,
                         PropModeReplace, (unsigned char *)&xdndVersion, 1 );

102
103
        // Store a pointer to be used in X11Loop
        pFactory->m_dndMap[m_wnd] = m_pDropTarget;
104
105
    }

106
    // Change the window title
107
    XStoreName( XDISPLAY, m_wnd, "VLC" );
108

109
110
111
    // Associate the window to the main "parent" window
    XSetTransientForHint( XDISPLAY, m_wnd, m_rDisplay.getMainWindow() );

112
    // Set this window as a vout
113
    if( m_pParent )
114
    {
115
        VlcProc::instance( getIntf() )->registerVoutWindow( (void*)m_wnd );
116
    }
117

118
119
120
121
122
}


X11Window::~X11Window()
{
123
124
    if( m_pParent )
    {
125
        VlcProc::instance( getIntf() )->unregisterVoutWindow( (void*)m_wnd );
126
127
    }

128
129
    X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
    pFactory->m_windowMap[m_wnd] = NULL;
130
    pFactory->m_dndMap[m_wnd] = NULL;
131
132
133
134
135
136
137
138
139
140

    if( m_dragDrop )
    {
        delete m_pDropTarget;
    }
    XDestroyWindow( XDISPLAY, m_wnd );
    XSync( XDISPLAY, False );
}


141
void X11Window::show( int left, int top ) const
142
143
144
145
146
147
148
{
    // Map the window
    XMapRaised( XDISPLAY, m_wnd );
    XMoveWindow( XDISPLAY, m_wnd, left, top );
}


149
void X11Window::hide() const
150
151
152
153
154
155
{
    // Unmap the window
    XUnmapWindow( XDISPLAY, m_wnd );
}


156
void X11Window::moveResize( int left, int top, int width, int height ) const
157
158
159
160
161
{
    XMoveResizeWindow( XDISPLAY, m_wnd, left, top, width, height );
}


162
void X11Window::raise() const
163
164
165
166
167
{
    XRaiseWindow( XDISPLAY, m_wnd );
}


168
void X11Window::setOpacity( uint8_t value ) const
169
170
171
172
173
{
    // Sorry, the opacity cannot be changed :)
}


174
void X11Window::toggleOnTop( bool onTop ) const
175
{
176
177
    int i_ret, i_format;
    unsigned long i, i_items, i_bytesafter;
178
179
180
181
    Atom net_wm_supported, net_wm_state, net_wm_state_on_top;
    union { Atom *p_atom; unsigned char *p_char; } p_args;

    p_args.p_atom = NULL;
182
183
184
185
186
187
188
189

    net_wm_supported = XInternAtom( XDISPLAY, "_NET_SUPPORTED", False );

    i_ret = XGetWindowProperty( XDISPLAY, DefaultRootWindow( XDISPLAY ),
                                net_wm_supported,
                                0, 16384, False, AnyPropertyType,
                                &net_wm_supported,
                                &i_format, &i_items, &i_bytesafter,
190
                                (unsigned char **)&p_args );
191
192
193
194
195
196
197
198
199

    if( i_ret != Success || i_items == 0 ) return; /* Not supported */

    net_wm_state = XInternAtom( XDISPLAY, "_NET_WM_STATE", False );
    net_wm_state_on_top = XInternAtom( XDISPLAY, "_NET_WM_STATE_STAYS_ON_TOP",
                                       False );

    for( i = 0; i < i_items; i++ )
    {
200
        if( p_args.p_atom[i] == net_wm_state_on_top ) break;
201
202
    }

203
    XFree( p_args.p_atom );
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
    if( i == i_items ) return; /* Not supported */

    /* Switch "on top" status */
    XClientMessageEvent event;
    memset( &event, 0, sizeof( XClientMessageEvent ) );

    event.type = ClientMessage;
    event.message_type = net_wm_state;
    event.display = XDISPLAY;
    event.window = m_wnd;
    event.format = 32;
    event.data.l[ 0 ] = onTop; /* set property */
    event.data.l[ 1 ] = net_wm_state_on_top;

    XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
                False, SubstructureRedirectMask, (XEvent*)&event );
220
221
222
}

#endif