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 )
44 XSetWindowAttributes attr;
45 unsigned long valuemask;
49 m_wnd_parent = pParentWindow->m_wnd;
51 int i_screen = DefaultScreen( XDISPLAY );
53 attr.event_mask = ExposureMask | StructureNotifyMask;
54 attr.backing_store = Always;
55 attr.background_pixel = BlackPixel( XDISPLAY, i_screen );
56 valuemask = CWBackingStore | CWBackPixel | CWEventMask;
60 m_wnd_parent = DefaultRootWindow( XDISPLAY );
62 attr.event_mask = ExposureMask | StructureNotifyMask;
63 valuemask = CWEventMask;
67 m_wnd = XCreateWindow( XDISPLAY, m_wnd_parent, -10, 0, 1, 1, 0, 0,
68 InputOutput, CopyFromParent, valuemask, &attr );
70 // wait for X server to process the previous commands
71 XSync( XDISPLAY, false );
73 // Set the colormap for 8bpp mode
76 XSetWindowColormap( XDISPLAY, m_wnd, m_rDisplay.getColormap() );
79 // Select events received by the window
80 XSelectInput( XDISPLAY, m_wnd, ExposureMask|KeyPressMask|
81 PointerMotionMask|ButtonPressMask|ButtonReleaseMask|
82 LeaveWindowMask|FocusChangeMask );
84 // Store a pointer on the generic window in a map
85 X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
86 pFactory->m_windowMap[m_wnd] = &rWindow;
88 // Changing decorations
91 unsigned long functions;
92 unsigned long decorations;
93 signed long input_mode;
96 Atom hints_atom = XInternAtom( XDISPLAY, "_MOTIF_WM_HINTS", False );
97 motifWmHints.flags = 2; // MWM_HINTS_DECORATIONS;
98 motifWmHints.decorations = 0;
99 XChangeProperty( XDISPLAY, m_wnd, hints_atom, hints_atom, 32,
100 PropModeReplace, (unsigned char *)&motifWmHints,
101 sizeof( motifWmHints ) / sizeof( uint32_t ) );
106 // Create a Dnd object for this window
107 m_pDropTarget = new X11DragDrop( getIntf(), m_rDisplay, m_wnd,
110 // Register the window as a drop target
111 Atom xdndAtom = XInternAtom( XDISPLAY, "XdndAware", False );
112 char xdndVersion = 4;
113 XChangeProperty( XDISPLAY, m_wnd, xdndAtom, XA_ATOM, 32,
114 PropModeReplace, (unsigned char *)&xdndVersion, 1 );
116 // Store a pointer to be used in X11Loop
117 pFactory->m_dndMap[m_wnd] = m_pDropTarget;
120 // Change the window title
121 XStoreName( XDISPLAY, m_wnd, "VLC" );
123 // Associate the window to the main "parent" window
124 XSetTransientForHint( XDISPLAY, m_wnd, m_rDisplay.getMainWindow() );
129 X11Window::~X11Window()
131 X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
132 pFactory->m_windowMap[m_wnd] = NULL;
133 pFactory->m_dndMap[m_wnd] = NULL;
137 delete m_pDropTarget;
139 XDestroyWindow( XDISPLAY, m_wnd );
140 XSync( XDISPLAY, False );
143 void X11Window::reparent( void* OSHandle, int x, int y, int w, int h )
145 // Reparent the window
147 OSHandle ? (Window) OSHandle : DefaultRootWindow( XDISPLAY );
150 XResizeWindow( XDISPLAY, m_wnd, w, h );
152 XReparentWindow( XDISPLAY, m_wnd, new_parent, x, y);
153 m_wnd_parent = new_parent;
157 void X11Window::show() const
160 XMapRaised( XDISPLAY, m_wnd );
164 void X11Window::hide() const
167 XUnmapWindow( XDISPLAY, m_wnd );
171 void X11Window::moveResize( int left, int top, int width, int height ) const
173 if( width && height )
174 XMoveResizeWindow( XDISPLAY, m_wnd, left, top, width, height );
176 XMoveWindow( XDISPLAY, m_wnd, left, top );
180 void X11Window::raise() const
182 XRaiseWindow( XDISPLAY, m_wnd );
186 void X11Window::setOpacity( uint8_t value ) const
188 // Sorry, the opacity cannot be changed :)
192 void X11Window::toggleOnTop( bool onTop ) const
195 unsigned long i, i_items, i_bytesafter;
196 Atom net_wm_supported, net_wm_state, net_wm_state_on_top,net_wm_state_above;
197 union { Atom *p_atom; unsigned char *p_char; } p_args;
199 p_args.p_atom = NULL;
201 net_wm_supported = XInternAtom( XDISPLAY, "_NET_SUPPORTED", False );
203 i_ret = XGetWindowProperty( XDISPLAY, DefaultRootWindow( XDISPLAY ),
205 0, 16384, False, AnyPropertyType,
207 &i_format, &i_items, &i_bytesafter,
208 (unsigned char **)&p_args );
210 if( i_ret != Success || i_items == 0 ) return; /* Not supported */
212 net_wm_state = XInternAtom( XDISPLAY, "_NET_WM_STATE", False );
213 net_wm_state_on_top = XInternAtom( XDISPLAY, "_NET_WM_STATE_STAYS_ON_TOP",
216 for( i = 0; i < i_items; i++ )
218 if( p_args.p_atom[i] == net_wm_state_on_top ) break;
222 { /* use _NET_WM_STATE_ABOVE if window manager
223 * doesn't handle _NET_WM_STATE_STAYS_ON_TOP */
225 net_wm_state_above = XInternAtom( XDISPLAY, "_NET_WM_STATE_ABOVE",
227 for( i = 0; i < i_items; i++ )
229 if( p_args.p_atom[i] == net_wm_state_above ) break;
232 XFree( p_args.p_atom );
234 return; /* Not supported */
236 /* Switch "on top" status */
237 XClientMessageEvent event;
238 memset( &event, 0, sizeof( XClientMessageEvent ) );
240 event.type = ClientMessage;
241 event.message_type = net_wm_state;
242 event.display = XDISPLAY;
243 event.window = m_wnd;
245 event.data.l[ 0 ] = onTop; /* set property */
246 event.data.l[ 1 ] = net_wm_state_above;
248 XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
249 False, SubstructureRedirectMask, (XEvent*)&event );
253 XFree( p_args.p_atom );
255 /* Switch "on top" status */
256 XClientMessageEvent event;
257 memset( &event, 0, sizeof( XClientMessageEvent ) );
259 event.type = ClientMessage;
260 event.message_type = net_wm_state;
261 event.display = XDISPLAY;
262 event.window = m_wnd;
264 event.data.l[ 0 ] = onTop; /* set property */
265 event.data.l[ 1 ] = net_wm_state_on_top;
267 XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
268 False, SubstructureRedirectMask, (XEvent*)&event );