]> git.sesse.net Git - vlc/blob - modules/gui/skins2/x11/x11_window.cpp
podcast: implement "live" podcast item removal
[vlc] / modules / gui / skins2 / x11 / x11_window.cpp
1 /*****************************************************************************
2  * x11_window.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #ifdef X11_SKINS
26
27 #include <X11/Xatom.h>
28
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"
36
37
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 )
43 {
44     XSetWindowAttributes attr;
45     unsigned long valuemask;
46
47     if (pParentWindow)
48     {
49         m_wnd_parent = pParentWindow->m_wnd;
50
51         int i_screen = DefaultScreen( XDISPLAY );
52
53         attr.event_mask = ExposureMask | StructureNotifyMask;
54         attr.backing_store = Always;
55         attr.background_pixel = BlackPixel( XDISPLAY, i_screen );
56         valuemask = CWBackingStore | CWBackPixel | CWEventMask;
57     }
58     else
59     {
60         m_wnd_parent = DefaultRootWindow( XDISPLAY );
61
62         attr.event_mask = ExposureMask | StructureNotifyMask;
63         valuemask = CWEventMask;
64     }
65
66     // Create the window
67     m_wnd = XCreateWindow( XDISPLAY, m_wnd_parent, -10, 0, 1, 1, 0, 0,
68                            InputOutput, CopyFromParent, valuemask, &attr );
69
70     // wait for X server to process the previous commands
71     XSync( XDISPLAY, false );
72
73     // Set the colormap for 8bpp mode
74     if( XPIXELSIZE == 1 )
75     {
76         XSetWindowColormap( XDISPLAY, m_wnd, m_rDisplay.getColormap() );
77     }
78
79     // Select events received by the window
80     XSelectInput( XDISPLAY, m_wnd, ExposureMask|KeyPressMask|
81                   PointerMotionMask|ButtonPressMask|ButtonReleaseMask|
82                   LeaveWindowMask|FocusChangeMask );
83
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;
87
88     // Changing decorations
89     struct {
90         unsigned long flags;
91         unsigned long functions;
92         unsigned long decorations;
93         signed   long input_mode;
94         unsigned long status;
95     } motifWmHints;
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 ) );
102
103     // Drag & drop
104     if( m_dragDrop )
105     {
106         // Create a Dnd object for this window
107         m_pDropTarget = new X11DragDrop( getIntf(), m_rDisplay, m_wnd,
108                                          playOnDrop );
109
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 );
115
116         // Store a pointer to be used in X11Loop
117         pFactory->m_dndMap[m_wnd] = m_pDropTarget;
118     }
119
120     // Change the window title
121     XStoreName( XDISPLAY, m_wnd, "VLC" );
122
123     // Associate the window to the main "parent" window
124     XSetTransientForHint( XDISPLAY, m_wnd, m_rDisplay.getMainWindow() );
125
126 }
127
128
129 X11Window::~X11Window()
130 {
131     X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
132     pFactory->m_windowMap[m_wnd] = NULL;
133     pFactory->m_dndMap[m_wnd] = NULL;
134
135     if( m_dragDrop )
136     {
137         delete m_pDropTarget;
138     }
139     XDestroyWindow( XDISPLAY, m_wnd );
140     XSync( XDISPLAY, False );
141 }
142
143 void X11Window::reparent( void* OSHandle, int x, int y, int w, int h )
144 {
145     // Reparent the window
146     Window new_parent =
147            OSHandle ? (Window) OSHandle : DefaultRootWindow( XDISPLAY );
148
149     if( w && h )
150         XResizeWindow( XDISPLAY, m_wnd, w, h );
151
152     XReparentWindow( XDISPLAY, m_wnd, new_parent, x, y);
153     m_wnd_parent = new_parent;
154 }
155
156
157 void X11Window::show() const
158 {
159     // Map the window
160     XMapRaised( XDISPLAY, m_wnd );
161 }
162
163
164 void X11Window::hide() const
165 {
166     // Unmap the window
167     XUnmapWindow( XDISPLAY, m_wnd );
168 }
169
170
171 void X11Window::moveResize( int left, int top, int width, int height ) const
172 {
173     if( width && height )
174         XMoveResizeWindow( XDISPLAY, m_wnd, left, top, width, height );
175     else
176         XMoveWindow( XDISPLAY, m_wnd, left, top );
177 }
178
179
180 void X11Window::raise() const
181 {
182     XRaiseWindow( XDISPLAY, m_wnd );
183 }
184
185
186 void X11Window::setOpacity( uint8_t value ) const
187 {
188     // Sorry, the opacity cannot be changed :)
189 }
190
191
192 void X11Window::toggleOnTop( bool onTop ) const
193 {
194     int i_ret, i_format;
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;
198
199     p_args.p_atom = NULL;
200
201     net_wm_supported = XInternAtom( XDISPLAY, "_NET_SUPPORTED", False );
202
203     i_ret = XGetWindowProperty( XDISPLAY, DefaultRootWindow( XDISPLAY ),
204                                 net_wm_supported,
205                                 0, 16384, False, AnyPropertyType,
206                                 &net_wm_supported,
207                                 &i_format, &i_items, &i_bytesafter,
208                                 (unsigned char **)&p_args );
209
210     if( i_ret != Success || i_items == 0 ) return; /* Not supported */
211
212     net_wm_state = XInternAtom( XDISPLAY, "_NET_WM_STATE", False );
213     net_wm_state_on_top = XInternAtom( XDISPLAY, "_NET_WM_STATE_STAYS_ON_TOP",
214                                        False );
215
216     for( i = 0; i < i_items; i++ )
217     {
218         if( p_args.p_atom[i] == net_wm_state_on_top ) break;
219     }
220
221     if( i == i_items )
222     { /* use _NET_WM_STATE_ABOVE if window manager
223        * doesn't handle _NET_WM_STATE_STAYS_ON_TOP */
224
225         net_wm_state_above = XInternAtom( XDISPLAY, "_NET_WM_STATE_ABOVE",
226                                           False);
227         for( i = 0; i < i_items; i++ )
228         {
229             if( p_args.p_atom[i] == net_wm_state_above ) break;
230         }
231  
232         XFree( p_args.p_atom );
233         if( i == i_items )
234             return; /* Not supported */
235
236         /* Switch "on top" status */
237         XClientMessageEvent event;
238         memset( &event, 0, sizeof( XClientMessageEvent ) );
239
240         event.type = ClientMessage;
241         event.message_type = net_wm_state;
242         event.display = XDISPLAY;
243         event.window = m_wnd;
244         event.format = 32;
245         event.data.l[ 0 ] = onTop; /* set property */
246         event.data.l[ 1 ] = net_wm_state_above;
247
248         XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
249                     False, SubstructureRedirectMask, (XEvent*)&event );
250         return;
251     }
252
253     XFree( p_args.p_atom );
254
255     /* Switch "on top" status */
256     XClientMessageEvent event;
257     memset( &event, 0, sizeof( XClientMessageEvent ) );
258
259     event.type = ClientMessage;
260     event.message_type = net_wm_state;
261     event.display = XDISPLAY;
262     event.window = m_wnd;
263     event.format = 32;
264     event.data.l[ 0 ] = onTop; /* set property */
265     event.data.l[ 1 ] = net_wm_state_on_top;
266
267     XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
268                 False, SubstructureRedirectMask, (XEvent*)&event );
269 }
270
271 #endif