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 ):
41 OSWindow( pIntf ), m_rDisplay( rDisplay ), m_pParent( pParentWindow ),
42 m_dragDrop( dragDrop )
46 m_wnd_parent = pParentWindow->m_wnd;
50 m_wnd_parent = DefaultRootWindow( XDISPLAY );
52 XSetWindowAttributes attr;
53 attr.event_mask = ExposureMask | StructureNotifyMask;
56 m_wnd = XCreateWindow( XDISPLAY, m_wnd_parent, -10, 0, 1, 1, 0, 0,
57 InputOutput, CopyFromParent, CWEventMask, &attr );
59 // Make sure window is created before returning
60 XMapWindow( XDISPLAY, m_wnd );
61 bool b_map_notify = false;
65 XWindowEvent( XDISPLAY, m_wnd, SubstructureNotifyMask |
66 StructureNotifyMask, &xevent);
67 if( (xevent.type == MapNotify)
68 && (xevent.xmap.window == m_wnd ) )
72 } while( ! b_map_notify );
73 XUnmapWindow( XDISPLAY, m_wnd );
75 // Set the colormap for 8bpp mode
78 XSetWindowColormap( XDISPLAY, m_wnd, m_rDisplay.getColormap() );
81 // Select events received by the window
82 XSelectInput( XDISPLAY, m_wnd, ExposureMask|KeyPressMask|
83 PointerMotionMask|ButtonPressMask|ButtonReleaseMask|
84 LeaveWindowMask|FocusChangeMask );
86 // Store a pointer on the generic window in a map
87 X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
88 pFactory->m_windowMap[m_wnd] = &rWindow;
90 // Changing decorations
93 unsigned long functions;
94 unsigned long decorations;
95 signed long input_mode;
98 Atom hints_atom = XInternAtom( XDISPLAY, "_MOTIF_WM_HINTS", False );
99 motifWmHints.flags = 2; // MWM_HINTS_DECORATIONS;
100 motifWmHints.decorations = 0;
101 XChangeProperty( XDISPLAY, m_wnd, hints_atom, hints_atom, 32,
102 PropModeReplace, (unsigned char *)&motifWmHints,
103 sizeof( motifWmHints ) / sizeof( uint32_t ) );
108 // Create a Dnd object for this window
109 m_pDropTarget = new X11DragDrop( getIntf(), m_rDisplay, m_wnd,
112 // Register the window as a drop target
113 Atom xdndAtom = XInternAtom( XDISPLAY, "XdndAware", False );
114 char xdndVersion = 4;
115 XChangeProperty( XDISPLAY, m_wnd, xdndAtom, XA_ATOM, 32,
116 PropModeReplace, (unsigned char *)&xdndVersion, 1 );
118 // Store a pointer to be used in X11Loop
119 pFactory->m_dndMap[m_wnd] = m_pDropTarget;
122 // Change the window title
123 XStoreName( XDISPLAY, m_wnd, "VLC" );
125 // Associate the window to the main "parent" window
126 XSetTransientForHint( XDISPLAY, m_wnd, m_rDisplay.getMainWindow() );
131 X11Window::~X11Window()
133 X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
134 pFactory->m_windowMap[m_wnd] = NULL;
135 pFactory->m_dndMap[m_wnd] = NULL;
139 delete m_pDropTarget;
141 XDestroyWindow( XDISPLAY, m_wnd );
142 XSync( XDISPLAY, False );
145 void X11Window::reparent( void* OSHandle, int x, int y, int w, int h )
147 // Reparent the window
149 OSHandle ? (Window) OSHandle : DefaultRootWindow( XDISPLAY );
152 XResizeWindow( XDISPLAY, m_wnd, w, h );
154 XReparentWindow( XDISPLAY, m_wnd, new_parent, x, y);
155 m_wnd_parent = new_parent;
159 void X11Window::show( int left, int top ) const
162 XMapRaised( XDISPLAY, m_wnd );
163 XMoveWindow( XDISPLAY, m_wnd, left, top );
167 void X11Window::hide() const
170 XUnmapWindow( XDISPLAY, m_wnd );
174 void X11Window::moveResize( int left, int top, int width, int height ) const
176 if( width && height )
177 XMoveResizeWindow( XDISPLAY, m_wnd, left, top, width, height );
179 XMoveWindow( XDISPLAY, m_wnd, left, top );
183 void X11Window::raise() const
185 XRaiseWindow( XDISPLAY, m_wnd );
189 void X11Window::setOpacity( uint8_t value ) const
191 // Sorry, the opacity cannot be changed :)
195 void X11Window::toggleOnTop( bool onTop ) const
198 unsigned long i, i_items, i_bytesafter;
199 Atom net_wm_supported, net_wm_state, net_wm_state_on_top,net_wm_state_above;
200 union { Atom *p_atom; unsigned char *p_char; } p_args;
202 p_args.p_atom = NULL;
204 net_wm_supported = XInternAtom( XDISPLAY, "_NET_SUPPORTED", False );
206 i_ret = XGetWindowProperty( XDISPLAY, DefaultRootWindow( XDISPLAY ),
208 0, 16384, False, AnyPropertyType,
210 &i_format, &i_items, &i_bytesafter,
211 (unsigned char **)&p_args );
213 if( i_ret != Success || i_items == 0 ) return; /* Not supported */
215 net_wm_state = XInternAtom( XDISPLAY, "_NET_WM_STATE", False );
216 net_wm_state_on_top = XInternAtom( XDISPLAY, "_NET_WM_STATE_STAYS_ON_TOP",
219 for( i = 0; i < i_items; i++ )
221 if( p_args.p_atom[i] == net_wm_state_on_top ) break;
225 { /* use _NET_WM_STATE_ABOVE if window manager
226 * doesn't handle _NET_WM_STATE_STAYS_ON_TOP */
228 net_wm_state_above = XInternAtom( XDISPLAY, "_NET_WM_STATE_ABOVE",
230 for( i = 0; i < i_items; i++ )
232 if( p_args.p_atom[i] == net_wm_state_above ) break;
235 XFree( p_args.p_atom );
237 return; /* Not supported */
239 /* Switch "on top" status */
240 XClientMessageEvent event;
241 memset( &event, 0, sizeof( XClientMessageEvent ) );
243 event.type = ClientMessage;
244 event.message_type = net_wm_state;
245 event.display = XDISPLAY;
246 event.window = m_wnd;
248 event.data.l[ 0 ] = onTop; /* set property */
249 event.data.l[ 1 ] = net_wm_state_above;
251 XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
252 False, SubstructureRedirectMask, (XEvent*)&event );
256 XFree( p_args.p_atom );
258 /* Switch "on top" status */
259 XClientMessageEvent event;
260 memset( &event, 0, sizeof( XClientMessageEvent ) );
262 event.type = ClientMessage;
263 event.message_type = net_wm_state;
264 event.display = XDISPLAY;
265 event.window = m_wnd;
267 event.data.l[ 0 ] = onTop; /* set property */
268 event.data.l[ 1 ] = net_wm_state_on_top;
270 XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
271 False, SubstructureRedirectMask, (XEvent*)&event );