]> git.sesse.net Git - vlc/blob - modules/gui/skins2/x11/x11_window.cpp
e9e85de39cc8f67c7d0148623999a5afd5101c04
[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 #include <limits.h>
29
30 #include "../src/generic_window.hpp"
31 #include "../src/vlcproc.hpp"
32 #include "x11_window.hpp"
33 #include "x11_display.hpp"
34 #include "x11_graphics.hpp"
35 #include "x11_dragdrop.hpp"
36 #include "x11_factory.hpp"
37
38 #include <assert.h>
39
40 X11Window::X11Window( intf_thread_t *pIntf, GenericWindow &rWindow,
41                       X11Display &rDisplay, bool dragDrop, bool playOnDrop,
42                       X11Window *pParentWindow, GenericWindow::WindowType_t type ):
43     OSWindow( pIntf ), m_rDisplay( rDisplay ), m_pParent( pParentWindow ),
44     m_dragDrop( dragDrop ), m_type ( type )
45 {
46     XSetWindowAttributes attr;
47     unsigned long valuemask;
48     string name_type;
49
50     if( type == GenericWindow::FullscreenWindow )
51     {
52         m_wnd_parent = DefaultRootWindow( XDISPLAY );
53
54         int i_screen = DefaultScreen( XDISPLAY );
55
56         attr.event_mask = ExposureMask | StructureNotifyMask;
57         attr.background_pixel = BlackPixel( XDISPLAY, i_screen );
58         attr.backing_store = Always;
59         valuemask = CWBackingStore | CWBackPixel | CWEventMask;
60
61         if( NET_WM_STATE_FULLSCREEN == None )
62         {
63             attr.override_redirect = True;
64             valuemask = valuemask | CWOverrideRedirect;
65         }
66
67         name_type = "Fullscreen";
68     }
69     else if( type == GenericWindow::VoutWindow )
70     {
71         m_wnd_parent = pParentWindow->m_wnd;
72
73         int i_screen = DefaultScreen( XDISPLAY );
74
75         attr.event_mask = ExposureMask | StructureNotifyMask;
76         attr.backing_store = Always;
77         attr.background_pixel = BlackPixel( XDISPLAY, i_screen );
78         valuemask = CWBackingStore | CWBackPixel | CWEventMask;
79
80         name_type = "VoutWindow";
81     }
82     else
83     {
84         m_wnd_parent = DefaultRootWindow( XDISPLAY );
85
86         attr.event_mask = ExposureMask | StructureNotifyMask;
87         valuemask = CWEventMask;
88
89         name_type = "TopWindow";
90     }
91
92     // Create the window
93     m_wnd = XCreateWindow( XDISPLAY, m_wnd_parent, -10, 0, 10, 10, 0, 0,
94                            InputOutput, CopyFromParent, valuemask, &attr );
95
96     // wait for X server to process the previous commands
97     XSync( XDISPLAY, false );
98
99     // Set the colormap for 8bpp mode
100     if( XPIXELSIZE == 1 )
101     {
102         XSetWindowColormap( XDISPLAY, m_wnd, m_rDisplay.getColormap() );
103     }
104
105     // Select events received by the window
106     long event_mask;
107     if( type == GenericWindow::VoutWindow )
108     {
109         event_mask =  ExposureMask|KeyPressMask|
110                       LeaveWindowMask|FocusChangeMask;
111     }
112     else
113     {
114         event_mask =  ExposureMask|KeyPressMask|
115                       PointerMotionMask|ButtonPressMask|ButtonReleaseMask|
116                       LeaveWindowMask|FocusChangeMask;
117     }
118     XSelectInput( XDISPLAY, m_wnd, event_mask );
119
120     // Store a pointer on the generic window in a map
121     X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
122     pFactory->m_windowMap[m_wnd] = &rWindow;
123
124     // Changing decorations
125     struct {
126         unsigned long flags;
127         unsigned long functions;
128         unsigned long decorations;
129         signed   long input_mode;
130         unsigned long status;
131     } motifWmHints;
132     Atom hints_atom = XInternAtom( XDISPLAY, "_MOTIF_WM_HINTS", False );
133     motifWmHints.flags = 2;    // MWM_HINTS_DECORATIONS;
134     motifWmHints.decorations = 0;
135     XChangeProperty( XDISPLAY, m_wnd, hints_atom, hints_atom, 32,
136                      PropModeReplace, (unsigned char *)&motifWmHints,
137                      sizeof( motifWmHints ) / sizeof( uint32_t ) );
138
139     // Drag & drop
140     if( m_dragDrop )
141     {
142         // Create a Dnd object for this window
143         m_pDropTarget = new X11DragDrop( getIntf(), m_rDisplay, m_wnd,
144                                          playOnDrop );
145
146         // Register the window as a drop target
147         Atom xdndAtom = XInternAtom( XDISPLAY, "XdndAware", False );
148         char xdndVersion = 4;
149         XChangeProperty( XDISPLAY, m_wnd, xdndAtom, XA_ATOM, 32,
150                          PropModeReplace, (unsigned char *)&xdndVersion, 1 );
151
152         // Store a pointer to be used in X11Loop
153         pFactory->m_dndMap[m_wnd] = m_pDropTarget;
154     }
155
156     // Change the window title
157     string name_window = "VLC (" + name_type + ")";
158     XStoreName( XDISPLAY, m_wnd, name_window.c_str() );
159
160     // Associate the window to the main "parent" window
161     XSetTransientForHint( XDISPLAY, m_wnd, m_rDisplay.getMainWindow() );
162
163     // initialize Class Hint
164     XClassHint classhint;
165     classhint.res_name = (char*) "vlc";
166     classhint.res_class = (char*) "Vlc";
167     XSetClassHint( XDISPLAY, m_wnd, &classhint );
168
169     // initialize WM_CLIENT_MACHINE
170     char* hostname = NULL;
171     long host_name_max = sysconf( _SC_HOST_NAME_MAX );
172     if( host_name_max <= 0 )
173         host_name_max = _POSIX_HOST_NAME_MAX;
174     hostname = new char[host_name_max];
175     if( hostname && gethostname( hostname, host_name_max ) == 0 )
176     {
177         hostname[host_name_max - 1] = '\0';
178
179         XTextProperty textprop;
180         textprop.value = (unsigned char *) hostname;
181         textprop.encoding = XA_STRING;
182         textprop.format = 8;
183         textprop.nitems = strlen( hostname );
184         XSetWMClientMachine( XDISPLAY, m_wnd, &textprop);
185     }
186     delete[] hostname;
187
188     // initialize EWMH pid
189     pid_t pid = getpid();
190     assert(  NET_WM_PID != None );
191     XChangeProperty( XDISPLAY, m_wnd, NET_WM_PID, XA_CARDINAL, 32,
192                      PropModeReplace, (unsigned char *)&pid, 1 );
193
194 }
195
196
197 X11Window::~X11Window()
198 {
199     X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
200     pFactory->m_windowMap[m_wnd] = NULL;
201     pFactory->m_dndMap[m_wnd] = NULL;
202
203     if( m_dragDrop )
204     {
205         delete m_pDropTarget;
206     }
207     XDestroyWindow( XDISPLAY, m_wnd );
208     XSync( XDISPLAY, False );
209 }
210
211 void X11Window::reparent( void* OSHandle, int x, int y, int w, int h )
212 {
213     // Reparent the window
214     Window new_parent =
215            OSHandle ? (Window) OSHandle : DefaultRootWindow( XDISPLAY );
216
217     XReparentWindow( XDISPLAY, m_wnd, new_parent, x, y);
218     if( w && h )
219         XResizeWindow( XDISPLAY, m_wnd, w, h );
220
221     m_wnd_parent = new_parent;
222 }
223
224
225 void X11Window::show() const
226 {
227     // Map the window
228     if( m_type == GenericWindow::VoutWindow )
229     {
230        XLowerWindow( XDISPLAY, m_wnd );
231        XMapWindow( XDISPLAY, m_wnd );
232     }
233     else if( m_type == GenericWindow::FullscreenWindow )
234     {
235         XMapRaised( XDISPLAY, m_wnd );
236         setFullscreen();
237         // toggleOnTop( true );
238     }
239     else
240     {
241         XMapRaised( XDISPLAY, m_wnd );
242     }
243 }
244
245
246 void X11Window::hide() const
247 {
248     // Unmap the window
249     XUnmapWindow( XDISPLAY, m_wnd );
250 }
251
252 void X11Window::moveResize( int left, int top, int width, int height ) const
253 {
254     if( width && height )
255         XMoveResizeWindow( XDISPLAY, m_wnd, left, top, width, height );
256     else
257         XMoveWindow( XDISPLAY, m_wnd, left, top );
258 }
259
260
261 void X11Window::raise() const
262 {
263     XRaiseWindow( XDISPLAY, m_wnd );
264 }
265
266
267 void X11Window::setOpacity( uint8_t value ) const
268 {
269     if( NET_WM_WINDOW_OPACITY == None )
270         return;
271
272     if( 255==value )
273         XDeleteProperty(XDISPLAY, m_wnd, NET_WM_WINDOW_OPACITY);
274     else
275     {
276         uint32_t opacity = value * ((uint32_t)-1/255);
277         XChangeProperty(XDISPLAY, m_wnd, NET_WM_WINDOW_OPACITY, XA_CARDINAL, 32,
278                         PropModeReplace, (unsigned char *) &opacity, 1L);
279     }
280     XSync( XDISPLAY, False );
281 }
282
283
284 void X11Window::setFullscreen( ) const
285 {
286     if( NET_WM_STATE_FULLSCREEN != None )
287     {
288         XClientMessageEvent event;
289         memset( &event, 0, sizeof( XClientMessageEvent ) );
290
291         event.type = ClientMessage;
292         event.message_type = NET_WM_STATE;
293         event.display = XDISPLAY;
294         event.window = m_wnd;
295         event.format = 32;
296         event.data.l[ 0 ] = 1;
297         event.data.l[ 1 ] = NET_WM_STATE_FULLSCREEN;
298  
299         XSendEvent( XDISPLAY,
300                     DefaultRootWindow( XDISPLAY ),
301                     False, SubstructureNotifyMask|SubstructureRedirectMask,
302                     (XEvent*)&event );
303     }
304 }
305
306
307 void X11Window::toggleOnTop( bool onTop ) const
308 {
309     if( NET_WM_STAYS_ON_TOP != None )
310     {
311         /* Switch "on top" status */
312         XClientMessageEvent event;
313         memset( &event, 0, sizeof( XClientMessageEvent ) );
314
315         event.type = ClientMessage;
316         event.message_type = NET_WM_STATE;
317         event.display = XDISPLAY;
318         event.window = m_wnd;
319         event.format = 32;
320         event.data.l[ 0 ] = onTop; /* set property */
321         event.data.l[ 1 ] = NET_WM_STAYS_ON_TOP;
322
323         XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
324                     False, SubstructureNotifyMask|SubstructureRedirectMask, (XEvent*)&event );
325     }
326     else if( NET_WM_STATE_ABOVE != None )
327     {
328         /* Switch "above" state */
329         XClientMessageEvent event;
330         memset( &event, 0, sizeof( XClientMessageEvent ) );
331
332         event.type = ClientMessage;
333         event.message_type = NET_WM_STATE;
334         event.display = XDISPLAY;
335         event.window = m_wnd;
336         event.format = 32;
337         event.data.l[ 0 ] = onTop; /* set property */
338         event.data.l[ 1 ] = NET_WM_STATE_ABOVE;
339
340         XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
341                     False, SubstructureNotifyMask|SubstructureRedirectMask, (XEvent*)&event );
342     }
343 }
344
345 #endif