]> git.sesse.net Git - vlc/blob - modules/gui/skins2/x11/x11_window.cpp
skins2(Linux): delete needs [] in C++ for arrays
[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     // initialize Class Hint
162     XClassHint classhint;
163     classhint.res_name = (char*) "vlc";
164     classhint.res_class = (char*) "Vlc";
165     XSetClassHint( XDISPLAY, m_wnd, &classhint );
166
167     // initialize WM_CLIENT_MACHINE
168     char* hostname = NULL;
169     long host_name_max = sysconf( _SC_HOST_NAME_MAX );
170     if( host_name_max <= 0 )
171         host_name_max = _POSIX_HOST_NAME_MAX;
172     hostname = new char[host_name_max];
173     if( hostname && gethostname( hostname, host_name_max ) == 0 )
174     {
175         hostname[host_name_max - 1] = '\0';
176
177         XTextProperty textprop;
178         textprop.value = (unsigned char *) hostname;
179         textprop.encoding = XA_STRING;
180         textprop.format = 8;
181         textprop.nitems = strlen( hostname );
182         XSetWMClientMachine( XDISPLAY, m_wnd, &textprop);
183     }
184     delete[] hostname;
185
186     // initialize EWMH pid
187     pid_t pid = getpid();
188     XChangeProperty( XDISPLAY, m_wnd, NET_WM_PID, XA_CARDINAL, 32,
189                      PropModeReplace, (unsigned char *)&pid, 1 );
190
191 }
192
193
194 X11Window::~X11Window()
195 {
196     X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
197     pFactory->m_windowMap[m_wnd] = NULL;
198     pFactory->m_dndMap[m_wnd] = NULL;
199
200     if( m_dragDrop )
201     {
202         delete m_pDropTarget;
203     }
204     XDestroyWindow( XDISPLAY, m_wnd );
205     XSync( XDISPLAY, False );
206 }
207
208 void X11Window::reparent( void* OSHandle, int x, int y, int w, int h )
209 {
210     // Reparent the window
211     Window new_parent =
212            OSHandle ? (Window) OSHandle : DefaultRootWindow( XDISPLAY );
213
214     XReparentWindow( XDISPLAY, m_wnd, new_parent, x, y);
215     if( w && h )
216         XResizeWindow( XDISPLAY, m_wnd, w, h );
217
218     m_wnd_parent = new_parent;
219 }
220
221
222 void X11Window::show() const
223 {
224     // Map the window
225     if( m_type == GenericWindow::VoutWindow )
226     {
227        XLowerWindow( XDISPLAY, m_wnd );
228        XMapWindow( XDISPLAY, m_wnd );
229     }
230     else if( m_type == GenericWindow::FullscreenWindow )
231     {
232         XMapRaised( XDISPLAY, m_wnd );
233         setFullscreen();
234         // toggleOnTop( true );
235     }
236     else
237     {
238         XMapRaised( XDISPLAY, m_wnd );
239     }
240 }
241
242
243 void X11Window::hide() const
244 {
245     // Unmap the window
246     XUnmapWindow( XDISPLAY, m_wnd );
247 }
248
249 void X11Window::moveResize( int left, int top, int width, int height ) const
250 {
251     if( width && height )
252         XMoveResizeWindow( XDISPLAY, m_wnd, left, top, width, height );
253     else
254         XMoveWindow( XDISPLAY, m_wnd, left, top );
255 }
256
257
258 void X11Window::raise() const
259 {
260     XRaiseWindow( XDISPLAY, m_wnd );
261 }
262
263
264 void X11Window::setOpacity( uint8_t value ) const
265 {
266     if( NET_WM_WINDOW_OPACITY == None )
267         return;
268
269     if( 255==value )
270         XDeleteProperty(XDISPLAY, m_wnd, NET_WM_WINDOW_OPACITY);
271     else
272     {
273         uint32_t opacity = value * ((uint32_t)-1/255);
274         XChangeProperty(XDISPLAY, m_wnd, NET_WM_WINDOW_OPACITY, XA_CARDINAL, 32,
275                         PropModeReplace, (unsigned char *) &opacity, 1L);
276     }
277     XSync( XDISPLAY, False );
278 }
279
280
281 void X11Window::setFullscreen( ) const
282 {
283     if( NET_WM_STATE_FULLSCREEN != None )
284     {
285         XClientMessageEvent event;
286         memset( &event, 0, sizeof( XClientMessageEvent ) );
287
288         event.type = ClientMessage;
289         event.message_type = NET_WM_STATE;
290         event.display = XDISPLAY;
291         event.window = m_wnd;
292         event.format = 32;
293         event.data.l[ 0 ] = 1;
294         event.data.l[ 1 ] = NET_WM_STATE_FULLSCREEN;
295  
296         XSendEvent( XDISPLAY,
297                     DefaultRootWindow( XDISPLAY ),
298                     False, SubstructureNotifyMask|SubstructureRedirectMask,
299                     (XEvent*)&event );
300     }
301 }
302
303
304 void X11Window::toggleOnTop( bool onTop ) const
305 {
306     if( NET_WM_STAYS_ON_TOP != None )
307     {
308         /* Switch "on top" status */
309         XClientMessageEvent event;
310         memset( &event, 0, sizeof( XClientMessageEvent ) );
311
312         event.type = ClientMessage;
313         event.message_type = NET_WM_STATE;
314         event.display = XDISPLAY;
315         event.window = m_wnd;
316         event.format = 32;
317         event.data.l[ 0 ] = onTop; /* set property */
318         event.data.l[ 1 ] = NET_WM_STAYS_ON_TOP;
319
320         XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
321                     False, SubstructureNotifyMask|SubstructureRedirectMask, (XEvent*)&event );
322     }
323     else if( NET_WM_STATE_ABOVE != None )
324     {
325         /* Switch "above" state */
326         XClientMessageEvent event;
327         memset( &event, 0, sizeof( XClientMessageEvent ) );
328
329         event.type = ClientMessage;
330         event.message_type = NET_WM_STATE;
331         event.display = XDISPLAY;
332         event.window = m_wnd;
333         event.format = 32;
334         event.data.l[ 0 ] = onTop; /* set property */
335         event.data.l[ 1 ] = NET_WM_STATE_ABOVE;
336
337         XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
338                     False, SubstructureNotifyMask|SubstructureRedirectMask, (XEvent*)&event );
339     }
340 }
341
342 #endif