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"
39 X11Window::X11Window( intf_thread_t
*pIntf
, GenericWindow
&rWindow
,
40 X11Display
&rDisplay
, bool dragDrop
, bool playOnDrop
,
41 X11Window
*pParentWindow
, GenericWindow::WindowType_t type
):
42 OSWindow( pIntf
), m_rDisplay( rDisplay
), m_pParent( pParentWindow
),
43 m_dragDrop( dragDrop
), m_type ( type
)
45 XSetWindowAttributes attr
;
46 unsigned long valuemask
;
49 if( type
== GenericWindow::FullscreenWindow
)
51 m_wnd_parent
= DefaultRootWindow( XDISPLAY
);
53 int i_screen
= DefaultScreen( XDISPLAY
);
55 attr
.event_mask
= ExposureMask
| StructureNotifyMask
;
56 attr
.background_pixel
= BlackPixel( XDISPLAY
, i_screen
);
57 attr
.backing_store
= Always
;
58 valuemask
= CWBackingStore
| CWBackPixel
| CWEventMask
;
60 if( NET_WM_STATE_FULLSCREEN
== None
)
62 attr
.override_redirect
= True
;
63 valuemask
= valuemask
| CWOverrideRedirect
;
66 name_type
= "Fullscreen";
68 else if( type
== GenericWindow::VoutWindow
)
70 m_wnd_parent
= pParentWindow
->m_wnd
;
72 int i_screen
= DefaultScreen( XDISPLAY
);
74 attr
.event_mask
= ExposureMask
| StructureNotifyMask
;
75 attr
.backing_store
= Always
;
76 attr
.background_pixel
= BlackPixel( XDISPLAY
, i_screen
);
77 valuemask
= CWBackingStore
| CWBackPixel
| CWEventMask
;
79 name_type
= "VoutWindow";
83 m_wnd_parent
= DefaultRootWindow( XDISPLAY
);
85 attr
.event_mask
= ExposureMask
| StructureNotifyMask
;
86 valuemask
= CWEventMask
;
88 name_type
= "TopWindow";
92 m_wnd
= XCreateWindow( XDISPLAY
, m_wnd_parent
, -10, 0, 10, 10, 0, 0,
93 InputOutput
, CopyFromParent
, valuemask
, &attr
);
95 // wait for X server to process the previous commands
96 XSync( XDISPLAY
, false );
98 // Set the colormap for 8bpp mode
101 XSetWindowColormap( XDISPLAY
, m_wnd
, m_rDisplay
.getColormap() );
104 // Select events received by the window
106 if( type
== GenericWindow::VoutWindow
)
108 event_mask
= ExposureMask
|KeyPressMask
|
109 LeaveWindowMask
|FocusChangeMask
;
113 event_mask
= ExposureMask
|KeyPressMask
|
114 PointerMotionMask
|ButtonPressMask
|ButtonReleaseMask
|
115 LeaveWindowMask
|FocusChangeMask
;
117 XSelectInput( XDISPLAY
, m_wnd
, event_mask
);
119 // Store a pointer on the generic window in a map
120 X11Factory
*pFactory
= (X11Factory
*)X11Factory::instance( getIntf() );
121 pFactory
->m_windowMap
[m_wnd
] = &rWindow
;
123 // Changing decorations
126 unsigned long functions
;
127 unsigned long decorations
;
128 signed long input_mode
;
129 unsigned long status
;
131 Atom hints_atom
= XInternAtom( XDISPLAY
, "_MOTIF_WM_HINTS", False
);
132 motifWmHints
.flags
= 2; // MWM_HINTS_DECORATIONS;
133 motifWmHints
.decorations
= 0;
134 XChangeProperty( XDISPLAY
, m_wnd
, hints_atom
, hints_atom
, 32,
135 PropModeReplace
, (unsigned char *)&motifWmHints
,
136 sizeof( motifWmHints
) / sizeof( uint32_t ) );
141 // Create a Dnd object for this window
142 m_pDropTarget
= new X11DragDrop( getIntf(), m_rDisplay
, m_wnd
,
145 // Register the window as a drop target
146 Atom xdndAtom
= XInternAtom( XDISPLAY
, "XdndAware", False
);
147 char xdndVersion
= 4;
148 XChangeProperty( XDISPLAY
, m_wnd
, xdndAtom
, XA_ATOM
, 32,
149 PropModeReplace
, (unsigned char *)&xdndVersion
, 1 );
151 // Store a pointer to be used in X11Loop
152 pFactory
->m_dndMap
[m_wnd
] = m_pDropTarget
;
155 // Change the window title
156 string name_window
= "VLC (" + name_type
+ ")";
157 XStoreName( XDISPLAY
, m_wnd
, name_window
.c_str() );
159 // Associate the window to the main "parent" window
160 XSetTransientForHint( XDISPLAY
, m_wnd
, m_rDisplay
.getMainWindow() );
162 // initialize Class Hint
163 XClassHint classhint
;
164 classhint
.res_name
= (char*) "vlc";
165 classhint
.res_class
= (char*) "Vlc";
166 XSetClassHint( XDISPLAY
, m_wnd
, &classhint
);
168 // initialize WM_CLIENT_MACHINE
169 char* hostname
= NULL
;
170 long host_name_max
= sysconf( _SC_HOST_NAME_MAX
);
171 if( host_name_max
<= 0 )
172 host_name_max
= _POSIX_HOST_NAME_MAX
;
173 hostname
= new char[host_name_max
];
174 if( hostname
&& gethostname( hostname
, host_name_max
) == 0 )
176 hostname
[host_name_max
- 1] = '\0';
178 XTextProperty textprop
;
179 textprop
.value
= (unsigned char *) hostname
;
180 textprop
.encoding
= XA_STRING
;
182 textprop
.nitems
= strlen( hostname
);
183 XSetWMClientMachine( XDISPLAY
, m_wnd
, &textprop
);
187 // initialize EWMH pid
188 pid_t pid
= getpid();
189 assert( NET_WM_PID
!= None
);
190 XChangeProperty( XDISPLAY
, m_wnd
, NET_WM_PID
, XA_CARDINAL
, 32,
191 PropModeReplace
, (unsigned char *)&pid
, 1 );
196 X11Window::~X11Window()
198 X11Factory
*pFactory
= (X11Factory
*)X11Factory::instance( getIntf() );
199 pFactory
->m_windowMap
[m_wnd
] = NULL
;
200 pFactory
->m_dndMap
[m_wnd
] = NULL
;
204 delete m_pDropTarget
;
206 XDestroyWindow( XDISPLAY
, m_wnd
);
207 XSync( XDISPLAY
, False
);
210 void X11Window::reparent( void* OSHandle
, int x
, int y
, int w
, int h
)
212 // Reparent the window
214 OSHandle
? (Window
) OSHandle
: DefaultRootWindow( XDISPLAY
);
216 XReparentWindow( XDISPLAY
, m_wnd
, new_parent
, x
, y
);
218 XResizeWindow( XDISPLAY
, m_wnd
, w
, h
);
220 m_wnd_parent
= new_parent
;
224 void X11Window::show() const
227 if( m_type
== GenericWindow::VoutWindow
)
229 XLowerWindow( XDISPLAY
, m_wnd
);
230 XMapWindow( XDISPLAY
, m_wnd
);
232 else if( m_type
== GenericWindow::FullscreenWindow
)
234 XMapRaised( XDISPLAY
, m_wnd
);
236 // toggleOnTop( true );
240 XMapRaised( XDISPLAY
, m_wnd
);
245 void X11Window::hide() const
248 XUnmapWindow( XDISPLAY
, m_wnd
);
251 void X11Window::moveResize( int left
, int top
, int width
, int height
) const
253 if( width
&& height
)
254 XMoveResizeWindow( XDISPLAY
, m_wnd
, left
, top
, width
, height
);
256 XMoveWindow( XDISPLAY
, m_wnd
, left
, top
);
260 void X11Window::raise() const
262 XRaiseWindow( XDISPLAY
, m_wnd
);
266 void X11Window::setOpacity( uint8_t value
) const
268 if( NET_WM_WINDOW_OPACITY
== None
)
272 XDeleteProperty(XDISPLAY
, m_wnd
, NET_WM_WINDOW_OPACITY
);
275 uint32_t opacity
= value
* ((uint32_t)-1/255);
276 XChangeProperty(XDISPLAY
, m_wnd
, NET_WM_WINDOW_OPACITY
, XA_CARDINAL
, 32,
277 PropModeReplace
, (unsigned char *) &opacity
, 1L);
279 XSync( XDISPLAY
, False
);
283 void X11Window::setFullscreen( ) const
285 if( NET_WM_STATE_FULLSCREEN
!= None
)
287 XClientMessageEvent event
;
288 memset( &event
, 0, sizeof( XClientMessageEvent
) );
290 event
.type
= ClientMessage
;
291 event
.message_type
= NET_WM_STATE
;
292 event
.display
= XDISPLAY
;
293 event
.window
= m_wnd
;
295 event
.data
.l
[ 0 ] = 1;
296 event
.data
.l
[ 1 ] = NET_WM_STATE_FULLSCREEN
;
298 XSendEvent( XDISPLAY
,
299 DefaultRootWindow( XDISPLAY
),
300 False
, SubstructureNotifyMask
|SubstructureRedirectMask
,
306 void X11Window::toggleOnTop( bool onTop
) const
308 if( NET_WM_STAYS_ON_TOP
!= None
)
310 /* Switch "on top" status */
311 XClientMessageEvent event
;
312 memset( &event
, 0, sizeof( XClientMessageEvent
) );
314 event
.type
= ClientMessage
;
315 event
.message_type
= NET_WM_STATE
;
316 event
.display
= XDISPLAY
;
317 event
.window
= m_wnd
;
319 event
.data
.l
[ 0 ] = onTop
; /* set property */
320 event
.data
.l
[ 1 ] = NET_WM_STAYS_ON_TOP
;
322 XSendEvent( XDISPLAY
, DefaultRootWindow( XDISPLAY
),
323 False
, SubstructureNotifyMask
|SubstructureRedirectMask
, (XEvent
*)&event
);
325 else if( NET_WM_STATE_ABOVE
!= None
)
327 /* Switch "above" state */
328 XClientMessageEvent event
;
329 memset( &event
, 0, sizeof( XClientMessageEvent
) );
331 event
.type
= ClientMessage
;
332 event
.message_type
= NET_WM_STATE
;
333 event
.display
= XDISPLAY
;
334 event
.window
= m_wnd
;
336 event
.data
.l
[ 0 ] = onTop
; /* set property */
337 event
.data
.l
[ 1 ] = NET_WM_STATE_ABOVE
;
339 XSendEvent( XDISPLAY
, DefaultRootWindow( XDISPLAY
),
340 False
, SubstructureNotifyMask
|SubstructureRedirectMask
, (XEvent
*)&event
);