skins2(Linux): _WM_NET_PID requires WM_CLIENT_MACHINE to be set
[vlc/solaris.git] / modules / gui / skins2 / x11 / x11_window.cpp
blob84b24497381c5a40700dac6078599d0d2dcd6b39
1 /*****************************************************************************
2 * x11_window.cpp
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
5 * $Id$
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 *****************************************************************************/
25 #ifdef X11_SKINS
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, GenericWindow::WindowType_t type ):
41 OSWindow( pIntf ), m_rDisplay( rDisplay ), m_pParent( pParentWindow ),
42 m_dragDrop( dragDrop ), m_type ( type )
44 XSetWindowAttributes attr;
45 unsigned long valuemask;
46 string name_type;
48 if( type == GenericWindow::FullscreenWindow )
50 m_wnd_parent = DefaultRootWindow( XDISPLAY );
52 int i_screen = DefaultScreen( XDISPLAY );
54 attr.event_mask = ExposureMask | StructureNotifyMask;
55 attr.background_pixel = BlackPixel( XDISPLAY, i_screen );
56 attr.backing_store = Always;
57 valuemask = CWBackingStore | CWBackPixel | CWEventMask;
59 if( NET_WM_STATE_FULLSCREEN == None )
61 attr.override_redirect = True;
62 valuemask = valuemask | CWOverrideRedirect;
65 name_type = "Fullscreen";
67 else if( type == GenericWindow::VoutWindow )
69 m_wnd_parent = pParentWindow->m_wnd;
71 int i_screen = DefaultScreen( XDISPLAY );
73 attr.event_mask = ExposureMask | StructureNotifyMask;
74 attr.backing_store = Always;
75 attr.background_pixel = BlackPixel( XDISPLAY, i_screen );
76 valuemask = CWBackingStore | CWBackPixel | CWEventMask;
78 name_type = "VoutWindow";
80 else
82 m_wnd_parent = DefaultRootWindow( XDISPLAY );
84 attr.event_mask = ExposureMask | StructureNotifyMask;
85 valuemask = CWEventMask;
87 name_type = "TopWindow";
90 // Create the window
91 m_wnd = XCreateWindow( XDISPLAY, m_wnd_parent, -10, 0, 10, 10, 0, 0,
92 InputOutput, CopyFromParent, valuemask, &attr );
94 // wait for X server to process the previous commands
95 XSync( XDISPLAY, false );
97 // Set the colormap for 8bpp mode
98 if( XPIXELSIZE == 1 )
100 XSetWindowColormap( XDISPLAY, m_wnd, m_rDisplay.getColormap() );
103 // Select events received by the window
104 long event_mask;
105 if( type == GenericWindow::VoutWindow )
107 event_mask = ExposureMask|KeyPressMask|
108 LeaveWindowMask|FocusChangeMask;
110 else
112 event_mask = ExposureMask|KeyPressMask|
113 PointerMotionMask|ButtonPressMask|ButtonReleaseMask|
114 LeaveWindowMask|FocusChangeMask;
116 XSelectInput( XDISPLAY, m_wnd, event_mask );
118 // Store a pointer on the generic window in a map
119 X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
120 pFactory->m_windowMap[m_wnd] = &rWindow;
122 // Changing decorations
123 struct {
124 unsigned long flags;
125 unsigned long functions;
126 unsigned long decorations;
127 signed long input_mode;
128 unsigned long status;
129 } motifWmHints;
130 Atom hints_atom = XInternAtom( XDISPLAY, "_MOTIF_WM_HINTS", False );
131 motifWmHints.flags = 2; // MWM_HINTS_DECORATIONS;
132 motifWmHints.decorations = 0;
133 XChangeProperty( XDISPLAY, m_wnd, hints_atom, hints_atom, 32,
134 PropModeReplace, (unsigned char *)&motifWmHints,
135 sizeof( motifWmHints ) / sizeof( uint32_t ) );
137 // Drag & drop
138 if( m_dragDrop )
140 // Create a Dnd object for this window
141 m_pDropTarget = new X11DragDrop( getIntf(), m_rDisplay, m_wnd,
142 playOnDrop );
144 // Register the window as a drop target
145 Atom xdndAtom = XInternAtom( XDISPLAY, "XdndAware", False );
146 char xdndVersion = 4;
147 XChangeProperty( XDISPLAY, m_wnd, xdndAtom, XA_ATOM, 32,
148 PropModeReplace, (unsigned char *)&xdndVersion, 1 );
150 // Store a pointer to be used in X11Loop
151 pFactory->m_dndMap[m_wnd] = m_pDropTarget;
154 // Change the window title
155 string name_window = "VLC (" + name_type + ")";
156 XStoreName( XDISPLAY, m_wnd, name_window.c_str() );
158 // Associate the window to the main "parent" window
159 XSetTransientForHint( XDISPLAY, m_wnd, m_rDisplay.getMainWindow() );
161 // initialize Class Hint
162 XClassHint classhint;
163 classhint.res_name = (char*) "vlc";
164 classhint.res_class = (char*) "Vlc";
165 XSetClassHint( XDISPLAY, m_wnd, &classhint );
167 // initialize WM_CLIENT_MACHINE
168 char* hostname = NULL;
169 long host_name_max = sysconf( _SC_HOST_NAME_MAX );
170 if( host_name_max <= 0 )
171 host_name_max = _POSIX_HOST_NAME_MAX;
172 hostname = new char[host_name_max];
173 if( hostname && gethostname( hostname, host_name_max ) == 0 )
175 hostname[host_name_max - 1] = '\0';
177 XTextProperty textprop;
178 textprop.value = (unsigned char *) hostname;
179 textprop.encoding = XA_STRING;
180 textprop.format = 8;
181 textprop.nitems = strlen( hostname );
182 XSetWMClientMachine( XDISPLAY, m_wnd, &textprop);
184 delete hostname;
186 // initialize EWMH pid
187 pid_t pid = getpid();
188 XChangeProperty( XDISPLAY, m_wnd, NET_WM_PID, XA_CARDINAL, 32,
189 PropModeReplace, (unsigned char *)&pid, 1 );
194 X11Window::~X11Window()
196 X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
197 pFactory->m_windowMap[m_wnd] = NULL;
198 pFactory->m_dndMap[m_wnd] = NULL;
200 if( m_dragDrop )
202 delete m_pDropTarget;
204 XDestroyWindow( XDISPLAY, m_wnd );
205 XSync( XDISPLAY, False );
208 void X11Window::reparent( void* OSHandle, int x, int y, int w, int h )
210 // Reparent the window
211 Window new_parent =
212 OSHandle ? (Window) OSHandle : DefaultRootWindow( XDISPLAY );
214 XReparentWindow( XDISPLAY, m_wnd, new_parent, x, y);
215 if( w && h )
216 XResizeWindow( XDISPLAY, m_wnd, w, h );
218 m_wnd_parent = new_parent;
222 void X11Window::show() const
224 // Map the window
225 if( m_type == GenericWindow::VoutWindow )
227 XLowerWindow( XDISPLAY, m_wnd );
228 XMapWindow( XDISPLAY, m_wnd );
230 else if( m_type == GenericWindow::FullscreenWindow )
232 XMapRaised( XDISPLAY, m_wnd );
233 setFullscreen();
234 // toggleOnTop( true );
236 else
238 XMapRaised( XDISPLAY, m_wnd );
243 void X11Window::hide() const
245 // Unmap the window
246 XUnmapWindow( XDISPLAY, m_wnd );
249 void X11Window::moveResize( int left, int top, int width, int height ) const
251 if( width && height )
252 XMoveResizeWindow( XDISPLAY, m_wnd, left, top, width, height );
253 else
254 XMoveWindow( XDISPLAY, m_wnd, left, top );
258 void X11Window::raise() const
260 XRaiseWindow( XDISPLAY, m_wnd );
264 void X11Window::setOpacity( uint8_t value ) const
266 if( NET_WM_WINDOW_OPACITY == None )
267 return;
269 if( 255==value )
270 XDeleteProperty(XDISPLAY, m_wnd, NET_WM_WINDOW_OPACITY);
271 else
273 uint32_t opacity = value * ((uint32_t)-1/255);
274 XChangeProperty(XDISPLAY, m_wnd, NET_WM_WINDOW_OPACITY, XA_CARDINAL, 32,
275 PropModeReplace, (unsigned char *) &opacity, 1L);
277 XSync( XDISPLAY, False );
281 void X11Window::setFullscreen( ) const
283 if( NET_WM_STATE_FULLSCREEN != None )
285 XClientMessageEvent event;
286 memset( &event, 0, sizeof( XClientMessageEvent ) );
288 event.type = ClientMessage;
289 event.message_type = NET_WM_STATE;
290 event.display = XDISPLAY;
291 event.window = m_wnd;
292 event.format = 32;
293 event.data.l[ 0 ] = 1;
294 event.data.l[ 1 ] = NET_WM_STATE_FULLSCREEN;
296 XSendEvent( XDISPLAY,
297 DefaultRootWindow( XDISPLAY ),
298 False, SubstructureNotifyMask|SubstructureRedirectMask,
299 (XEvent*)&event );
304 void X11Window::toggleOnTop( bool onTop ) const
306 if( NET_WM_STAYS_ON_TOP != None )
308 /* Switch "on top" status */
309 XClientMessageEvent event;
310 memset( &event, 0, sizeof( XClientMessageEvent ) );
312 event.type = ClientMessage;
313 event.message_type = NET_WM_STATE;
314 event.display = XDISPLAY;
315 event.window = m_wnd;
316 event.format = 32;
317 event.data.l[ 0 ] = onTop; /* set property */
318 event.data.l[ 1 ] = NET_WM_STAYS_ON_TOP;
320 XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
321 False, SubstructureNotifyMask|SubstructureRedirectMask, (XEvent*)&event );
323 else if( NET_WM_STATE_ABOVE != None )
325 /* Switch "above" state */
326 XClientMessageEvent event;
327 memset( &event, 0, sizeof( XClientMessageEvent ) );
329 event.type = ClientMessage;
330 event.message_type = NET_WM_STATE;
331 event.display = XDISPLAY;
332 event.window = m_wnd;
333 event.format = 32;
334 event.data.l[ 0 ] = onTop; /* set property */
335 event.data.l[ 1 ] = NET_WM_STATE_ABOVE;
337 XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
338 False, SubstructureNotifyMask|SubstructureRedirectMask, (XEvent*)&event );
342 #endif