]> git.sesse.net Git - vlc/blob - modules/gui/skins2/x11/x11_window.cpp
Removes trailing spaces. Removes tabs.
[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     Window parent;
45     if (pParentWindow)
46     {
47         parent = pParentWindow->m_wnd;
48     }
49     else
50     {
51         parent = DefaultRootWindow( XDISPLAY );
52     }
53     XSetWindowAttributes attr;
54
55     // Create the window
56     m_wnd = XCreateWindow( XDISPLAY, parent, 0, 0, 1, 1, 0, 0,
57                            InputOutput, CopyFromParent, 0, &attr );
58
59     // Set the colormap for 8bpp mode
60     if( XPIXELSIZE == 1 )
61     {
62         XSetWindowColormap( XDISPLAY, m_wnd, m_rDisplay.getColormap() );
63     }
64
65     // Select events received by the window
66     XSelectInput( XDISPLAY, m_wnd, ExposureMask|KeyPressMask|
67                   PointerMotionMask|ButtonPressMask|ButtonReleaseMask|
68                   LeaveWindowMask|FocusChangeMask );
69
70     // Store a pointer on the generic window in a map
71     X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
72     pFactory->m_windowMap[m_wnd] = &rWindow;
73
74     // Changing decorations
75     struct {
76         unsigned long flags;
77         unsigned long functions;
78         unsigned long decorations;
79         signed   long input_mode;
80         unsigned long status;
81     } motifWmHints;
82     Atom hints_atom = XInternAtom( XDISPLAY, "_MOTIF_WM_HINTS", False );
83     motifWmHints.flags = 2;    // MWM_HINTS_DECORATIONS;
84     motifWmHints.decorations = 0;
85     XChangeProperty( XDISPLAY, m_wnd, hints_atom, hints_atom, 32,
86                      PropModeReplace, (unsigned char *)&motifWmHints,
87                      sizeof( motifWmHints ) / sizeof( uint32_t ) );
88
89     // Drag & drop
90     if( m_dragDrop )
91     {
92         // Create a Dnd object for this window
93         m_pDropTarget = new X11DragDrop( getIntf(), m_rDisplay, m_wnd,
94                                          playOnDrop );
95
96         // Register the window as a drop target
97         Atom xdndAtom = XInternAtom( XDISPLAY, "XdndAware", False );
98         char xdndVersion = 4;
99         XChangeProperty( XDISPLAY, m_wnd, xdndAtom, XA_ATOM, 32,
100                          PropModeReplace, (unsigned char *)&xdndVersion, 1 );
101
102         // Store a pointer to be used in X11Loop
103         pFactory->m_dndMap[m_wnd] = m_pDropTarget;
104     }
105
106     // Change the window title
107     XStoreName( XDISPLAY, m_wnd, "VLC" );
108
109     // Associate the window to the main "parent" window
110     XSetTransientForHint( XDISPLAY, m_wnd, m_rDisplay.getMainWindow() );
111
112     // Set this window as a vout
113     if( m_pParent )
114     {
115         VlcProc::instance( getIntf() )->registerVoutWindow( (void*)m_wnd );
116     }
117
118 }
119
120
121 X11Window::~X11Window()
122 {
123     if( m_pParent )
124     {
125         VlcProc::instance( getIntf() )->unregisterVoutWindow( (void*)m_wnd );
126     }
127
128     X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
129     pFactory->m_windowMap[m_wnd] = NULL;
130     pFactory->m_dndMap[m_wnd] = NULL;
131
132     if( m_dragDrop )
133     {
134         delete m_pDropTarget;
135     }
136     XDestroyWindow( XDISPLAY, m_wnd );
137     XSync( XDISPLAY, False );
138 }
139
140
141 void X11Window::show( int left, int top ) const
142 {
143     // Map the window
144     XMapRaised( XDISPLAY, m_wnd );
145     XMoveWindow( XDISPLAY, m_wnd, left, top );
146 }
147
148
149 void X11Window::hide() const
150 {
151     // Unmap the window
152     XUnmapWindow( XDISPLAY, m_wnd );
153 }
154
155
156 void X11Window::moveResize( int left, int top, int width, int height ) const
157 {
158     XMoveResizeWindow( XDISPLAY, m_wnd, left, top, width, height );
159 }
160
161
162 void X11Window::raise() const
163 {
164     XRaiseWindow( XDISPLAY, m_wnd );
165 }
166
167
168 void X11Window::setOpacity( uint8_t value ) const
169 {
170     // Sorry, the opacity cannot be changed :)
171 }
172
173
174 void X11Window::toggleOnTop( bool onTop ) const
175 {
176     int i_ret, i_format;
177     unsigned long i, i_items, i_bytesafter;
178     Atom net_wm_supported, net_wm_state, net_wm_state_on_top,net_wm_state_above;
179     union { Atom *p_atom; unsigned char *p_char; } p_args;
180
181     p_args.p_atom = NULL;
182
183     net_wm_supported = XInternAtom( XDISPLAY, "_NET_SUPPORTED", False );
184
185     i_ret = XGetWindowProperty( XDISPLAY, DefaultRootWindow( XDISPLAY ),
186                                 net_wm_supported,
187                                 0, 16384, False, AnyPropertyType,
188                                 &net_wm_supported,
189                                 &i_format, &i_items, &i_bytesafter,
190                                 (unsigned char **)&p_args );
191
192     if( i_ret != Success || i_items == 0 ) return; /* Not supported */
193
194     net_wm_state = XInternAtom( XDISPLAY, "_NET_WM_STATE", False );
195     net_wm_state_on_top = XInternAtom( XDISPLAY, "_NET_WM_STATE_STAYS_ON_TOP",
196                                        False );
197
198     for( i = 0; i < i_items; i++ )
199     {
200         if( p_args.p_atom[i] == net_wm_state_on_top ) break;
201     }
202
203     if( i == i_items )
204     { /* use _NET_WM_STATE_ABOVE if window manager
205        * doesn't handle _NET_WM_STATE_STAYS_ON_TOP */
206
207         net_wm_state_above = XInternAtom( XDISPLAY, "_NET_WM_STATE_ABOVE",
208                                           False);
209         for( i = 0; i < i_items; i++ )
210         {
211             if( p_args.p_atom[i] == net_wm_state_above ) break;
212         }
213  
214         XFree( p_args.p_atom );
215         if( i == i_items )
216             return; /* Not supported */
217
218         /* Switch "on top" status */
219         XClientMessageEvent event;
220         memset( &event, 0, sizeof( XClientMessageEvent ) );
221
222         event.type = ClientMessage;
223         event.message_type = net_wm_state;
224         event.display = XDISPLAY;
225         event.window = m_wnd;
226         event.format = 32;
227         event.data.l[ 0 ] = onTop; /* set property */
228         event.data.l[ 1 ] = net_wm_state_above;
229
230         XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
231                     False, SubstructureRedirectMask, (XEvent*)&event );
232         return;
233     }
234
235     XFree( p_args.p_atom );
236
237     /* Switch "on top" status */
238     XClientMessageEvent event;
239     memset( &event, 0, sizeof( XClientMessageEvent ) );
240
241     event.type = ClientMessage;
242     event.message_type = net_wm_state;
243     event.display = XDISPLAY;
244     event.window = m_wnd;
245     event.format = 32;
246     event.data.l[ 0 ] = onTop; /* set property */
247     event.data.l[ 1 ] = net_wm_state_on_top;
248
249     XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
250                 False, SubstructureRedirectMask, (XEvent*)&event );
251 }
252
253 #endif