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>
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"
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 )
46 XSetWindowAttributes attr;
47 unsigned long valuemask;
50 if( type == GenericWindow::FullscreenWindow )
52 m_wnd_parent = DefaultRootWindow( XDISPLAY );
54 int i_screen = DefaultScreen( XDISPLAY );
56 attr.event_mask = ExposureMask | StructureNotifyMask;
57 attr.background_pixel = BlackPixel( XDISPLAY, i_screen );
58 attr.backing_store = Always;
59 valuemask = CWBackingStore | CWBackPixel | CWEventMask;
61 if( NET_WM_STATE_FULLSCREEN == None )
63 attr.override_redirect = True;
64 valuemask = valuemask | CWOverrideRedirect;
67 name_type = "Fullscreen";
69 else if( type == GenericWindow::VoutWindow )
71 m_wnd_parent = pParentWindow->m_wnd;
73 int i_screen = DefaultScreen( XDISPLAY );
75 attr.event_mask = ExposureMask | StructureNotifyMask;
76 attr.backing_store = Always;
77 attr.background_pixel = BlackPixel( XDISPLAY, i_screen );
78 valuemask = CWBackingStore | CWBackPixel | CWEventMask;
80 name_type = "VoutWindow";
84 m_wnd_parent = DefaultRootWindow( XDISPLAY );
86 attr.event_mask = ExposureMask | StructureNotifyMask;
87 valuemask = CWEventMask;
89 name_type = "TopWindow";
93 m_wnd = XCreateWindow( XDISPLAY, m_wnd_parent, -10, 0, 10, 10, 0, 0,
94 InputOutput, CopyFromParent, valuemask, &attr );
96 // wait for X server to process the previous commands
97 XSync( XDISPLAY, false );
99 // Set the colormap for 8bpp mode
100 if( XPIXELSIZE == 1 )
102 XSetWindowColormap( XDISPLAY, m_wnd, m_rDisplay.getColormap() );
105 // Select events received by the window
107 if( type == GenericWindow::VoutWindow )
109 event_mask = ExposureMask|KeyPressMask|
110 LeaveWindowMask|FocusChangeMask;
114 event_mask = ExposureMask|KeyPressMask|
115 PointerMotionMask|ButtonPressMask|ButtonReleaseMask|
116 LeaveWindowMask|FocusChangeMask;
118 XSelectInput( XDISPLAY, m_wnd, event_mask );
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;
124 // Changing decorations
127 unsigned long functions;
128 unsigned long decorations;
129 signed long input_mode;
130 unsigned long status;
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 ) );
142 // Create a Dnd object for this window
143 m_pDropTarget = new X11DragDrop( getIntf(), m_rDisplay, m_wnd,
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 );
152 // Store a pointer to be used in X11Loop
153 pFactory->m_dndMap[m_wnd] = m_pDropTarget;
156 // Change the window title
157 string name_window = "VLC (" + name_type + ")";
158 XStoreName( XDISPLAY, m_wnd, name_window.c_str() );
160 // Associate the window to the main "parent" window
161 XSetTransientForHint( XDISPLAY, m_wnd, m_rDisplay.getMainWindow() );
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 );
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 )
177 hostname[host_name_max - 1] = '\0';
179 XTextProperty textprop;
180 textprop.value = (unsigned char *) hostname;
181 textprop.encoding = XA_STRING;
183 textprop.nitems = strlen( hostname );
184 XSetWMClientMachine( XDISPLAY, m_wnd, &textprop);
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 );
197 X11Window::~X11Window()
199 X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
200 pFactory->m_windowMap[m_wnd] = NULL;
201 pFactory->m_dndMap[m_wnd] = NULL;
205 delete m_pDropTarget;
207 XDestroyWindow( XDISPLAY, m_wnd );
208 XSync( XDISPLAY, False );
211 void X11Window::reparent( void* OSHandle, int x, int y, int w, int h )
213 // Reparent the window
215 OSHandle ? (Window) OSHandle : DefaultRootWindow( XDISPLAY );
217 XReparentWindow( XDISPLAY, m_wnd, new_parent, x, y);
219 XResizeWindow( XDISPLAY, m_wnd, w, h );
221 m_wnd_parent = new_parent;
225 void X11Window::show() const
228 if( m_type == GenericWindow::VoutWindow )
230 XLowerWindow( XDISPLAY, m_wnd );
231 XMapWindow( XDISPLAY, m_wnd );
233 else if( m_type == GenericWindow::FullscreenWindow )
235 XMapRaised( XDISPLAY, m_wnd );
237 // toggleOnTop( true );
241 XMapRaised( XDISPLAY, m_wnd );
246 void X11Window::hide() const
249 XUnmapWindow( XDISPLAY, m_wnd );
252 void X11Window::moveResize( int left, int top, int width, int height ) const
254 if( width && height )
255 XMoveResizeWindow( XDISPLAY, m_wnd, left, top, width, height );
257 XMoveWindow( XDISPLAY, m_wnd, left, top );
261 void X11Window::raise() const
263 XRaiseWindow( XDISPLAY, m_wnd );
267 void X11Window::setOpacity( uint8_t value ) const
269 if( NET_WM_WINDOW_OPACITY == None )
273 XDeleteProperty(XDISPLAY, m_wnd, NET_WM_WINDOW_OPACITY);
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);
280 XSync( XDISPLAY, False );
284 void X11Window::setFullscreen( ) const
286 if( NET_WM_STATE_FULLSCREEN != None )
288 XClientMessageEvent event;
289 memset( &event, 0, sizeof( XClientMessageEvent ) );
291 event.type = ClientMessage;
292 event.message_type = NET_WM_STATE;
293 event.display = XDISPLAY;
294 event.window = m_wnd;
296 event.data.l[ 0 ] = 1;
297 event.data.l[ 1 ] = NET_WM_STATE_FULLSCREEN;
299 XSendEvent( XDISPLAY,
300 DefaultRootWindow( XDISPLAY ),
301 False, SubstructureNotifyMask|SubstructureRedirectMask,
307 void X11Window::toggleOnTop( bool onTop ) const
309 if( NET_WM_STAYS_ON_TOP != None )
311 /* Switch "on top" status */
312 XClientMessageEvent event;
313 memset( &event, 0, sizeof( XClientMessageEvent ) );
315 event.type = ClientMessage;
316 event.message_type = NET_WM_STATE;
317 event.display = XDISPLAY;
318 event.window = m_wnd;
320 event.data.l[ 0 ] = onTop; /* set property */
321 event.data.l[ 1 ] = NET_WM_STAYS_ON_TOP;
323 XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
324 False, SubstructureNotifyMask|SubstructureRedirectMask, (XEvent*)&event );
326 else if( NET_WM_STATE_ABOVE != None )
328 /* Switch "above" state */
329 XClientMessageEvent event;
330 memset( &event, 0, sizeof( XClientMessageEvent ) );
332 event.type = ClientMessage;
333 event.message_type = NET_WM_STATE;
334 event.display = XDISPLAY;
335 event.window = m_wnd;
337 event.data.l[ 0 ] = onTop; /* set property */
338 event.data.l[ 1 ] = NET_WM_STATE_ABOVE;
340 XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
341 False, SubstructureNotifyMask|SubstructureRedirectMask, (XEvent*)&event );