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 )
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 attr.override_redirect = True;
58 valuemask = CWBackingStore | CWOverrideRedirect |
59 CWBackPixel | CWEventMask;
61 name_type = "Fullscreen";
63 else if( type == GenericWindow::VoutWindow )
65 m_wnd_parent = pParentWindow->m_wnd;
67 int i_screen = DefaultScreen( XDISPLAY );
69 attr.event_mask = ExposureMask | StructureNotifyMask;
70 attr.backing_store = Always;
71 attr.background_pixel = BlackPixel( XDISPLAY, i_screen );
72 valuemask = CWBackingStore | CWBackPixel | CWEventMask;
74 name_type = "VoutWindow";
78 m_wnd_parent = DefaultRootWindow( XDISPLAY );
80 attr.event_mask = ExposureMask | StructureNotifyMask;
81 valuemask = CWEventMask;
83 name_type = "TopWindow";
87 m_wnd = XCreateWindow( XDISPLAY, m_wnd_parent, -10, 0, 1, 1, 0, 0,
88 InputOutput, CopyFromParent, valuemask, &attr );
90 // wait for X server to process the previous commands
91 XSync( XDISPLAY, false );
93 // Set the colormap for 8bpp mode
96 XSetWindowColormap( XDISPLAY, m_wnd, m_rDisplay.getColormap() );
99 // Select events received by the window
101 if( type == GenericWindow::VoutWindow )
103 event_mask = ExposureMask|KeyPressMask|
104 LeaveWindowMask|FocusChangeMask;
108 event_mask = ExposureMask|KeyPressMask|
109 PointerMotionMask|ButtonPressMask|ButtonReleaseMask|
110 LeaveWindowMask|FocusChangeMask;
112 XSelectInput( XDISPLAY, m_wnd, event_mask );
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;
118 // Changing decorations
121 unsigned long functions;
122 unsigned long decorations;
123 signed long input_mode;
124 unsigned long status;
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 ) );
136 // Create a Dnd object for this window
137 m_pDropTarget = new X11DragDrop( getIntf(), m_rDisplay, m_wnd,
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 );
146 // Store a pointer to be used in X11Loop
147 pFactory->m_dndMap[m_wnd] = m_pDropTarget;
150 // Change the window title
151 string name_window = "VLC (" + name_type + ")";
152 XStoreName( XDISPLAY, m_wnd, name_window.c_str() );
154 // Associate the window to the main "parent" window
155 XSetTransientForHint( XDISPLAY, m_wnd, m_rDisplay.getMainWindow() );
160 X11Window::~X11Window()
162 X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
163 pFactory->m_windowMap[m_wnd] = NULL;
164 pFactory->m_dndMap[m_wnd] = NULL;
168 delete m_pDropTarget;
170 XDestroyWindow( XDISPLAY, m_wnd );
171 XSync( XDISPLAY, False );
174 void X11Window::reparent( void* OSHandle, int x, int y, int w, int h )
176 // Reparent the window
178 OSHandle ? (Window) OSHandle : DefaultRootWindow( XDISPLAY );
181 XResizeWindow( XDISPLAY, m_wnd, w, h );
183 XReparentWindow( XDISPLAY, m_wnd, new_parent, x, y);
184 m_wnd_parent = new_parent;
188 void X11Window::show() const
191 XMapRaised( XDISPLAY, m_wnd );
195 void X11Window::hide() const
198 XUnmapWindow( XDISPLAY, m_wnd );
202 void X11Window::moveResize( int left, int top, int width, int height ) const
204 if( width && height )
205 XMoveResizeWindow( XDISPLAY, m_wnd, left, top, width, height );
207 XMoveWindow( XDISPLAY, m_wnd, left, top );
211 void X11Window::raise() const
213 XRaiseWindow( XDISPLAY, m_wnd );
217 void X11Window::setOpacity( uint8_t value ) const
219 Atom opaq = XInternAtom(XDISPLAY, "_NET_WM_WINDOW_OPACITY", False);
221 XDeleteProperty(XDISPLAY, m_wnd, opaq);
224 uint32_t opacity = value * ((uint32_t)-1/255);
225 XChangeProperty(XDISPLAY, m_wnd, opaq, XA_CARDINAL, 32,
226 PropModeReplace, (unsigned char *) &opacity, 1L);
228 XSync( XDISPLAY, False );
232 void X11Window::toggleOnTop( bool onTop ) const
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;
239 p_args.p_atom = NULL;
241 net_wm_supported = XInternAtom( XDISPLAY, "_NET_SUPPORTED", False );
243 i_ret = XGetWindowProperty( XDISPLAY, DefaultRootWindow( XDISPLAY ),
245 0, 16384, False, AnyPropertyType,
247 &i_format, &i_items, &i_bytesafter,
248 (unsigned char **)&p_args );
250 if( i_ret != Success || i_items == 0 ) return; /* Not supported */
252 net_wm_state = XInternAtom( XDISPLAY, "_NET_WM_STATE", False );
253 net_wm_state_on_top = XInternAtom( XDISPLAY, "_NET_WM_STATE_STAYS_ON_TOP",
256 for( i = 0; i < i_items; i++ )
258 if( p_args.p_atom[i] == net_wm_state_on_top ) break;
262 { /* use _NET_WM_STATE_ABOVE if window manager
263 * doesn't handle _NET_WM_STATE_STAYS_ON_TOP */
265 net_wm_state_above = XInternAtom( XDISPLAY, "_NET_WM_STATE_ABOVE",
267 for( i = 0; i < i_items; i++ )
269 if( p_args.p_atom[i] == net_wm_state_above ) break;
272 XFree( p_args.p_atom );
274 return; /* Not supported */
276 /* Switch "on top" status */
277 XClientMessageEvent event;
278 memset( &event, 0, sizeof( XClientMessageEvent ) );
280 event.type = ClientMessage;
281 event.message_type = net_wm_state;
282 event.display = XDISPLAY;
283 event.window = m_wnd;
285 event.data.l[ 0 ] = onTop; /* set property */
286 event.data.l[ 1 ] = net_wm_state_above;
288 XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
289 False, SubstructureRedirectMask, (XEvent*)&event );
293 XFree( p_args.p_atom );
295 /* Switch "on top" status */
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_on_top;
307 XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
308 False, SubstructureRedirectMask, (XEvent*)&event );