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