]> git.sesse.net Git - vlc/blob - modules/gui/skins2/x11/x11_window.cpp
skins(Linux) : fix crash with a few skins (e.g Orangeade) and xcb_x11
[vlc] / modules / gui / skins2 / x11 / x11_window.cpp
1 /*****************************************************************************
2  * x11_window.cpp
3  *****************************************************************************
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  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef X11_SKINS
26
27 #include <X11/Xatom.h>
28
29 #include "../src/generic_window.hpp"
30 #include "../src/vlcproc.hpp"
31 #include "x11_window.hpp"
32 #include "x11_display.hpp"
33 #include "x11_graphics.hpp"
34 #include "x11_dragdrop.hpp"
35 #include "x11_factory.hpp"
36
37
38 X11Window::X11Window( intf_thread_t *pIntf, GenericWindow &rWindow,
39                       X11Display &rDisplay, bool dragDrop, bool playOnDrop,
40                       X11Window *pParentWindow, GenericWindow::WindowType_t type ):
41     OSWindow( pIntf ), m_rDisplay( rDisplay ), m_pParent( pParentWindow ),
42     m_dragDrop( dragDrop ), m_type ( type )
43 {
44     XSetWindowAttributes attr;
45     unsigned long valuemask;
46     string name_type;
47
48     if( type == GenericWindow::FullscreenWindow )
49     {
50         m_wnd_parent = DefaultRootWindow( XDISPLAY );
51
52         int i_screen = DefaultScreen( XDISPLAY );
53
54         attr.event_mask = ExposureMask | StructureNotifyMask;
55         attr.background_pixel = BlackPixel( XDISPLAY, i_screen );
56         attr.backing_store = Always;
57         valuemask = CWBackingStore | CWBackPixel | CWEventMask;
58
59         if( NET_WM_STATE_FULLSCREEN == None )
60         {
61             attr.override_redirect = True;
62             valuemask = valuemask | CWOverrideRedirect;
63         }
64
65         name_type = "Fullscreen";
66     }
67     else if( type == GenericWindow::VoutWindow )
68     {
69         m_wnd_parent = pParentWindow->m_wnd;
70
71         int i_screen = DefaultScreen( XDISPLAY );
72
73         attr.event_mask = ExposureMask | StructureNotifyMask;
74         attr.backing_store = Always;
75         attr.background_pixel = BlackPixel( XDISPLAY, i_screen );
76         valuemask = CWBackingStore | CWBackPixel | CWEventMask;
77
78         name_type = "VoutWindow";
79     }
80     else
81     {
82         m_wnd_parent = DefaultRootWindow( XDISPLAY );
83
84         attr.event_mask = ExposureMask | StructureNotifyMask;
85         valuemask = CWEventMask;
86
87         name_type = "TopWindow";
88     }
89
90     // Create the window
91     m_wnd = XCreateWindow( XDISPLAY, m_wnd_parent, -10, 0, 10, 10, 0, 0,
92                            InputOutput, CopyFromParent, valuemask, &attr );
93
94     // wait for X server to process the previous commands
95     XSync( XDISPLAY, false );
96
97     // Set the colormap for 8bpp mode
98     if( XPIXELSIZE == 1 )
99     {
100         XSetWindowColormap( XDISPLAY, m_wnd, m_rDisplay.getColormap() );
101     }
102
103     // Select events received by the window
104     long event_mask;
105     if( type == GenericWindow::VoutWindow )
106     {
107         event_mask =  ExposureMask|KeyPressMask|
108                       LeaveWindowMask|FocusChangeMask;
109     }
110     else
111     {
112         event_mask =  ExposureMask|KeyPressMask|
113                       PointerMotionMask|ButtonPressMask|ButtonReleaseMask|
114                       LeaveWindowMask|FocusChangeMask;
115     }
116     XSelectInput( XDISPLAY, m_wnd, event_mask );
117
118     // Store a pointer on the generic window in a map
119     X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
120     pFactory->m_windowMap[m_wnd] = &rWindow;
121
122     // Changing decorations
123     struct {
124         unsigned long flags;
125         unsigned long functions;
126         unsigned long decorations;
127         signed   long input_mode;
128         unsigned long status;
129     } motifWmHints;
130     Atom hints_atom = XInternAtom( XDISPLAY, "_MOTIF_WM_HINTS", False );
131     motifWmHints.flags = 2;    // MWM_HINTS_DECORATIONS;
132     motifWmHints.decorations = 0;
133     XChangeProperty( XDISPLAY, m_wnd, hints_atom, hints_atom, 32,
134                      PropModeReplace, (unsigned char *)&motifWmHints,
135                      sizeof( motifWmHints ) / sizeof( uint32_t ) );
136
137     // Drag & drop
138     if( m_dragDrop )
139     {
140         // Create a Dnd object for this window
141         m_pDropTarget = new X11DragDrop( getIntf(), m_rDisplay, m_wnd,
142                                          playOnDrop );
143
144         // Register the window as a drop target
145         Atom xdndAtom = XInternAtom( XDISPLAY, "XdndAware", False );
146         char xdndVersion = 4;
147         XChangeProperty( XDISPLAY, m_wnd, xdndAtom, XA_ATOM, 32,
148                          PropModeReplace, (unsigned char *)&xdndVersion, 1 );
149
150         // Store a pointer to be used in X11Loop
151         pFactory->m_dndMap[m_wnd] = m_pDropTarget;
152     }
153
154     // Change the window title
155     string name_window = "VLC (" + name_type + ")";
156     XStoreName( XDISPLAY, m_wnd, name_window.c_str() );
157
158     // Associate the window to the main "parent" window
159     XSetTransientForHint( XDISPLAY, m_wnd, m_rDisplay.getMainWindow() );
160
161 }
162
163
164 X11Window::~X11Window()
165 {
166     X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
167     pFactory->m_windowMap[m_wnd] = NULL;
168     pFactory->m_dndMap[m_wnd] = NULL;
169
170     if( m_dragDrop )
171     {
172         delete m_pDropTarget;
173     }
174     XDestroyWindow( XDISPLAY, m_wnd );
175     XSync( XDISPLAY, False );
176 }
177
178 void X11Window::reparent( void* OSHandle, int x, int y, int w, int h )
179 {
180     // Reparent the window
181     Window new_parent =
182            OSHandle ? (Window) OSHandle : DefaultRootWindow( XDISPLAY );
183
184     XReparentWindow( XDISPLAY, m_wnd, new_parent, x, y);
185     if( w && h )
186         XResizeWindow( XDISPLAY, m_wnd, w, h );
187
188     m_wnd_parent = new_parent;
189 }
190
191
192 void X11Window::show() const
193 {
194     // Map the window
195     if( m_type == GenericWindow::VoutWindow )
196     {
197        XLowerWindow( XDISPLAY, m_wnd );
198        XMapWindow( XDISPLAY, m_wnd );
199     }
200     else if( m_type == GenericWindow::FullscreenWindow )
201     {
202         XMapRaised( XDISPLAY, m_wnd );
203         setFullscreen();
204         // toggleOnTop( true );
205     }
206     else
207     {
208         XMapRaised( XDISPLAY, m_wnd );
209     }
210 }
211
212
213 void X11Window::hide() const
214 {
215     // Unmap the window
216     XUnmapWindow( XDISPLAY, m_wnd );
217 }
218
219 void X11Window::moveResize( int left, int top, int width, int height ) const
220 {
221     if( width && height )
222         XMoveResizeWindow( XDISPLAY, m_wnd, left, top, width, height );
223     else
224         XMoveWindow( XDISPLAY, m_wnd, left, top );
225 }
226
227
228 void X11Window::raise() const
229 {
230     XRaiseWindow( XDISPLAY, m_wnd );
231 }
232
233
234 void X11Window::setOpacity( uint8_t value ) const
235 {
236     if( NET_WM_WINDOW_OPACITY == None )
237         return;
238
239     if( 255==value )
240         XDeleteProperty(XDISPLAY, m_wnd, NET_WM_WINDOW_OPACITY);
241     else
242     {
243         uint32_t opacity = value * ((uint32_t)-1/255);
244         XChangeProperty(XDISPLAY, m_wnd, NET_WM_WINDOW_OPACITY, XA_CARDINAL, 32,
245                         PropModeReplace, (unsigned char *) &opacity, 1L);
246     }
247     XSync( XDISPLAY, False );
248 }
249
250
251 void X11Window::setFullscreen( ) const
252 {
253     if( NET_WM_STATE_FULLSCREEN != None )
254     {
255         XClientMessageEvent event;
256         memset( &event, 0, sizeof( XClientMessageEvent ) );
257
258         event.type = ClientMessage;
259         event.message_type = NET_WM_STATE;
260         event.display = XDISPLAY;
261         event.window = m_wnd;
262         event.format = 32;
263         event.data.l[ 0 ] = 1;
264         event.data.l[ 1 ] = NET_WM_STATE_FULLSCREEN;
265  
266         XSendEvent( XDISPLAY,
267                     DefaultRootWindow( XDISPLAY ),
268                     False, SubstructureNotifyMask|SubstructureRedirectMask,
269                     (XEvent*)&event );
270     }
271 }
272
273
274 void X11Window::toggleOnTop( bool onTop ) const
275 {
276     if( NET_WM_STAYS_ON_TOP != None )
277     {
278         /* Switch "on top" status */
279         XClientMessageEvent event;
280         memset( &event, 0, sizeof( XClientMessageEvent ) );
281
282         event.type = ClientMessage;
283         event.message_type = NET_WM_STATE;
284         event.display = XDISPLAY;
285         event.window = m_wnd;
286         event.format = 32;
287         event.data.l[ 0 ] = onTop; /* set property */
288         event.data.l[ 1 ] = NET_WM_STAYS_ON_TOP;
289
290         XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
291                     False, SubstructureNotifyMask|SubstructureRedirectMask, (XEvent*)&event );
292     }
293     else if( NET_WM_STATE_ABOVE != None )
294     {
295         /* Switch "above" state */
296         XClientMessageEvent event;
297         memset( &event, 0, sizeof( XClientMessageEvent ) );
298
299         event.type = ClientMessage;
300         event.message_type = NET_WM_STATE;
301         event.display = XDISPLAY;
302         event.window = m_wnd;
303         event.format = 32;
304         event.data.l[ 0 ] = onTop; /* set property */
305         event.data.l[ 1 ] = NET_WM_STATE_ABOVE;
306
307         XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
308                     False, SubstructureNotifyMask|SubstructureRedirectMask, (XEvent*)&event );
309     }
310 }
311
312 #endif