]> git.sesse.net Git - vlc/blob - modules/gui/skins2/x11/x11_window.cpp
skins2(Linux): reactivate mouse on a video window (like dblclick for fullscreen)
[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 )
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         attr.override_redirect = True;
58         valuemask = CWBackingStore | CWOverrideRedirect |
59                     CWBackPixel | CWEventMask;
60
61         name_type = "Fullscreen";
62     }
63     else if( type == GenericWindow::VoutWindow )
64     {
65         m_wnd_parent = pParentWindow->m_wnd;
66
67         int i_screen = DefaultScreen( XDISPLAY );
68
69         attr.event_mask = ExposureMask | StructureNotifyMask;
70         attr.backing_store = Always;
71         attr.background_pixel = BlackPixel( XDISPLAY, i_screen );
72         valuemask = CWBackingStore | CWBackPixel | CWEventMask;
73
74         name_type = "VoutWindow";
75     }
76     else
77     {
78         m_wnd_parent = DefaultRootWindow( XDISPLAY );
79
80         attr.event_mask = ExposureMask | StructureNotifyMask;
81         valuemask = CWEventMask;
82
83         name_type = "TopWindow";
84     }
85
86     // Create the window
87     m_wnd = XCreateWindow( XDISPLAY, m_wnd_parent, -10, 0, 1, 1, 0, 0,
88                            InputOutput, CopyFromParent, valuemask, &attr );
89
90     // wait for X server to process the previous commands
91     XSync( XDISPLAY, false );
92
93     // Set the colormap for 8bpp mode
94     if( XPIXELSIZE == 1 )
95     {
96         XSetWindowColormap( XDISPLAY, m_wnd, m_rDisplay.getColormap() );
97     }
98
99     // Select events received by the window
100     long event_mask;
101     if( type == GenericWindow::VoutWindow )
102     {
103         event_mask =  ExposureMask|KeyPressMask|
104                       LeaveWindowMask|FocusChangeMask;
105     }
106     else
107     {
108         event_mask =  ExposureMask|KeyPressMask|
109                       PointerMotionMask|ButtonPressMask|ButtonReleaseMask|
110                       LeaveWindowMask|FocusChangeMask;
111     }
112     XSelectInput( XDISPLAY, m_wnd, event_mask );
113
114     // Store a pointer on the generic window in a map
115     X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
116     pFactory->m_windowMap[m_wnd] = &rWindow;
117
118     // Changing decorations
119     struct {
120         unsigned long flags;
121         unsigned long functions;
122         unsigned long decorations;
123         signed   long input_mode;
124         unsigned long status;
125     } motifWmHints;
126     Atom hints_atom = XInternAtom( XDISPLAY, "_MOTIF_WM_HINTS", False );
127     motifWmHints.flags = 2;    // MWM_HINTS_DECORATIONS;
128     motifWmHints.decorations = 0;
129     XChangeProperty( XDISPLAY, m_wnd, hints_atom, hints_atom, 32,
130                      PropModeReplace, (unsigned char *)&motifWmHints,
131                      sizeof( motifWmHints ) / sizeof( uint32_t ) );
132
133     // Drag & drop
134     if( m_dragDrop )
135     {
136         // Create a Dnd object for this window
137         m_pDropTarget = new X11DragDrop( getIntf(), m_rDisplay, m_wnd,
138                                          playOnDrop );
139
140         // Register the window as a drop target
141         Atom xdndAtom = XInternAtom( XDISPLAY, "XdndAware", False );
142         char xdndVersion = 4;
143         XChangeProperty( XDISPLAY, m_wnd, xdndAtom, XA_ATOM, 32,
144                          PropModeReplace, (unsigned char *)&xdndVersion, 1 );
145
146         // Store a pointer to be used in X11Loop
147         pFactory->m_dndMap[m_wnd] = m_pDropTarget;
148     }
149
150     // Change the window title
151     string name_window = "VLC (" + name_type + ")";
152     XStoreName( XDISPLAY, m_wnd, name_window.c_str() );
153
154     // Associate the window to the main "parent" window
155     XSetTransientForHint( XDISPLAY, m_wnd, m_rDisplay.getMainWindow() );
156
157 }
158
159
160 X11Window::~X11Window()
161 {
162     X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
163     pFactory->m_windowMap[m_wnd] = NULL;
164     pFactory->m_dndMap[m_wnd] = NULL;
165
166     if( m_dragDrop )
167     {
168         delete m_pDropTarget;
169     }
170     XDestroyWindow( XDISPLAY, m_wnd );
171     XSync( XDISPLAY, False );
172 }
173
174 void X11Window::reparent( void* OSHandle, int x, int y, int w, int h )
175 {
176     // Reparent the window
177     Window new_parent =
178            OSHandle ? (Window) OSHandle : DefaultRootWindow( XDISPLAY );
179
180     if( w && h )
181         XResizeWindow( XDISPLAY, m_wnd, w, h );
182
183     XReparentWindow( XDISPLAY, m_wnd, new_parent, x, y);
184     m_wnd_parent = new_parent;
185 }
186
187
188 void X11Window::show() const
189 {
190     // Map the window
191     XMapRaised( XDISPLAY, m_wnd );
192 }
193
194
195 void X11Window::hide() const
196 {
197     // Unmap the window
198     XUnmapWindow( XDISPLAY, m_wnd );
199 }
200
201
202 void X11Window::moveResize( int left, int top, int width, int height ) const
203 {
204     if( width && height )
205         XMoveResizeWindow( XDISPLAY, m_wnd, left, top, width, height );
206     else
207         XMoveWindow( XDISPLAY, m_wnd, left, top );
208 }
209
210
211 void X11Window::raise() const
212 {
213     XRaiseWindow( XDISPLAY, m_wnd );
214 }
215
216
217 void X11Window::setOpacity( uint8_t value ) const
218 {
219     Atom opaq = XInternAtom(XDISPLAY, "_NET_WM_WINDOW_OPACITY", False);
220     if( 255==value )
221         XDeleteProperty(XDISPLAY, m_wnd, opaq);
222     else
223     {
224         uint32_t opacity = value * ((uint32_t)-1/255);
225         XChangeProperty(XDISPLAY, m_wnd, opaq, XA_CARDINAL, 32,
226                         PropModeReplace, (unsigned char *) &opacity, 1L);
227     }
228     XSync( XDISPLAY, False );
229 }
230
231
232 void X11Window::toggleOnTop( bool onTop ) const
233 {
234     int i_ret, i_format;
235     unsigned long i, i_items, i_bytesafter;
236     Atom net_wm_supported, net_wm_state, net_wm_state_on_top,net_wm_state_above;
237     union { Atom *p_atom; unsigned char *p_char; } p_args;
238
239     p_args.p_atom = NULL;
240
241     net_wm_supported = XInternAtom( XDISPLAY, "_NET_SUPPORTED", False );
242
243     i_ret = XGetWindowProperty( XDISPLAY, DefaultRootWindow( XDISPLAY ),
244                                 net_wm_supported,
245                                 0, 16384, False, AnyPropertyType,
246                                 &net_wm_supported,
247                                 &i_format, &i_items, &i_bytesafter,
248                                 (unsigned char **)&p_args );
249
250     if( i_ret != Success || i_items == 0 ) return; /* Not supported */
251
252     net_wm_state = XInternAtom( XDISPLAY, "_NET_WM_STATE", False );
253     net_wm_state_on_top = XInternAtom( XDISPLAY, "_NET_WM_STATE_STAYS_ON_TOP",
254                                        False );
255
256     for( i = 0; i < i_items; i++ )
257     {
258         if( p_args.p_atom[i] == net_wm_state_on_top ) break;
259     }
260
261     if( i == i_items )
262     { /* use _NET_WM_STATE_ABOVE if window manager
263        * doesn't handle _NET_WM_STATE_STAYS_ON_TOP */
264
265         net_wm_state_above = XInternAtom( XDISPLAY, "_NET_WM_STATE_ABOVE",
266                                           False);
267         for( i = 0; i < i_items; i++ )
268         {
269             if( p_args.p_atom[i] == net_wm_state_above ) break;
270         }
271  
272         XFree( p_args.p_atom );
273         if( i == i_items )
274             return; /* Not supported */
275
276         /* Switch "on top" status */
277         XClientMessageEvent event;
278         memset( &event, 0, sizeof( XClientMessageEvent ) );
279
280         event.type = ClientMessage;
281         event.message_type = net_wm_state;
282         event.display = XDISPLAY;
283         event.window = m_wnd;
284         event.format = 32;
285         event.data.l[ 0 ] = onTop; /* set property */
286         event.data.l[ 1 ] = net_wm_state_above;
287
288         XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
289                     False, SubstructureRedirectMask, (XEvent*)&event );
290         return;
291     }
292
293     XFree( p_args.p_atom );
294
295     /* Switch "on top" status */
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_on_top;
306
307     XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
308                 False, SubstructureRedirectMask, (XEvent*)&event );
309 }
310
311 #endif