1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
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.
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.
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 *****************************************************************************/
27 #include <X11/Xatom.h>
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"
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 )
44 XSetWindowAttributes attr;
45 unsigned long valuemask;
48 if( type == GenericWindow::FullscreenWindow )
50 m_wnd_parent = DefaultRootWindow( XDISPLAY );
52 int i_screen = DefaultScreen( XDISPLAY );
54 attr.event_mask = ExposureMask | StructureNotifyMask;
55 attr.background_pixel = BlackPixel( XDISPLAY, i_screen );
56 attr.backing_store = Always;
57 valuemask = CWBackingStore | CWBackPixel | CWEventMask;
59 if( NET_WM_STATE_FULLSCREEN == None )
61 attr.override_redirect = True;
62 valuemask = valuemask | CWOverrideRedirect;
65 name_type = "Fullscreen";
67 else if( type == GenericWindow::VoutWindow )
69 m_wnd_parent = pParentWindow->m_wnd;
71 int i_screen = DefaultScreen( XDISPLAY );
73 attr.event_mask = ExposureMask | StructureNotifyMask;
74 attr.backing_store = Always;
75 attr.background_pixel = BlackPixel( XDISPLAY, i_screen );
76 valuemask = CWBackingStore | CWBackPixel | CWEventMask;
78 name_type = "VoutWindow";
82 m_wnd_parent = DefaultRootWindow( XDISPLAY );
84 attr.event_mask = ExposureMask | StructureNotifyMask;
85 valuemask = CWEventMask;
87 name_type = "TopWindow";
91 m_wnd = XCreateWindow( XDISPLAY, m_wnd_parent, -10, 0, 10, 10, 0, 0,
92 InputOutput, CopyFromParent, valuemask, &attr );
94 // wait for X server to process the previous commands
95 XSync( XDISPLAY, false );
97 // Set the colormap for 8bpp mode
100 XSetWindowColormap( XDISPLAY, m_wnd, m_rDisplay.getColormap() );
103 // Select events received by the window
105 if( type == GenericWindow::VoutWindow )
107 event_mask = ExposureMask|KeyPressMask|
108 LeaveWindowMask|FocusChangeMask;
112 event_mask = ExposureMask|KeyPressMask|
113 PointerMotionMask|ButtonPressMask|ButtonReleaseMask|
114 LeaveWindowMask|FocusChangeMask;
116 XSelectInput( XDISPLAY, m_wnd, event_mask );
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;
122 // Changing decorations
125 unsigned long functions;
126 unsigned long decorations;
127 signed long input_mode;
128 unsigned long status;
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 ) );
140 // Create a Dnd object for this window
141 m_pDropTarget = new X11DragDrop( getIntf(), m_rDisplay, m_wnd,
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 );
150 // Store a pointer to be used in X11Loop
151 pFactory->m_dndMap[m_wnd] = m_pDropTarget;
154 // Change the window title
155 string name_window = "VLC (" + name_type + ")";
156 XStoreName( XDISPLAY, m_wnd, name_window.c_str() );
158 // Associate the window to the main "parent" window
159 XSetTransientForHint( XDISPLAY, m_wnd, m_rDisplay.getMainWindow() );
164 X11Window::~X11Window()
166 X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
167 pFactory->m_windowMap[m_wnd] = NULL;
168 pFactory->m_dndMap[m_wnd] = NULL;
172 delete m_pDropTarget;
174 XDestroyWindow( XDISPLAY, m_wnd );
175 XSync( XDISPLAY, False );
178 void X11Window::reparent( void* OSHandle, int x, int y, int w, int h )
180 // Reparent the window
182 OSHandle ? (Window) OSHandle : DefaultRootWindow( XDISPLAY );
184 XReparentWindow( XDISPLAY, m_wnd, new_parent, x, y);
186 XResizeWindow( XDISPLAY, m_wnd, w, h );
188 m_wnd_parent = new_parent;
192 void X11Window::show() const
195 if( m_type == GenericWindow::VoutWindow )
197 XLowerWindow( XDISPLAY, m_wnd );
198 XMapWindow( XDISPLAY, m_wnd );
200 else if( m_type == GenericWindow::FullscreenWindow )
202 XMapRaised( XDISPLAY, m_wnd );
204 // toggleOnTop( true );
208 XMapRaised( XDISPLAY, m_wnd );
213 void X11Window::hide() const
216 XUnmapWindow( XDISPLAY, m_wnd );
219 void X11Window::moveResize( int left, int top, int width, int height ) const
221 if( width && height )
222 XMoveResizeWindow( XDISPLAY, m_wnd, left, top, width, height );
224 XMoveWindow( XDISPLAY, m_wnd, left, top );
228 void X11Window::raise() const
230 XRaiseWindow( XDISPLAY, m_wnd );
234 void X11Window::setOpacity( uint8_t value ) const
236 if( NET_WM_WINDOW_OPACITY == None )
240 XDeleteProperty(XDISPLAY, m_wnd, NET_WM_WINDOW_OPACITY);
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);
247 XSync( XDISPLAY, False );
251 void X11Window::setFullscreen( ) const
253 if( NET_WM_STATE_FULLSCREEN != None )
255 XClientMessageEvent event;
256 memset( &event, 0, sizeof( XClientMessageEvent ) );
258 event.type = ClientMessage;
259 event.message_type = NET_WM_STATE;
260 event.display = XDISPLAY;
261 event.window = m_wnd;
263 event.data.l[ 0 ] = 1;
264 event.data.l[ 1 ] = NET_WM_STATE_FULLSCREEN;
266 XSendEvent( XDISPLAY,
267 DefaultRootWindow( XDISPLAY ),
268 False, SubstructureNotifyMask|SubstructureRedirectMask,
274 void X11Window::toggleOnTop( bool onTop ) const
276 if( NET_WM_STAYS_ON_TOP != None )
278 /* Switch "on top" status */
279 XClientMessageEvent event;
280 memset( &event, 0, sizeof( XClientMessageEvent ) );
282 event.type = ClientMessage;
283 event.message_type = NET_WM_STATE;
284 event.display = XDISPLAY;
285 event.window = m_wnd;
287 event.data.l[ 0 ] = onTop; /* set property */
288 event.data.l[ 1 ] = NET_WM_STAYS_ON_TOP;
290 XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
291 False, SubstructureNotifyMask|SubstructureRedirectMask, (XEvent*)&event );
293 else if( NET_WM_STATE_ABOVE != None )
295 /* Switch "above" state */
296 XClientMessageEvent event;
297 memset( &event, 0, sizeof( XClientMessageEvent ) );
299 event.type = ClientMessage;
300 event.message_type = NET_WM_STATE;
301 event.display = XDISPLAY;
302 event.window = m_wnd;
304 event.data.l[ 0 ] = onTop; /* set property */
305 event.data.l[ 1 ] = NET_WM_STATE_ABOVE;
307 XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
308 False, SubstructureNotifyMask|SubstructureRedirectMask, (XEvent*)&event );