1 /*****************************************************************************
2 * xcommon.c: Functions common to the X11 and XVideo plugins
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN (Centrale Réseaux) and its contributors
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Sam Hocevar <sam@zoy.org>
9 * David Kennedy <dkennedy@tinytoad.com>
10 * Gildas Bazin <gbazin@videolan.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <errno.h> /* ENOMEM */
31 #include <stdlib.h> /* free() */
32 #include <string.h> /* strerror() */
39 #ifdef HAVE_MACHINE_PARAM_H
41 # include <machine/param.h>
42 # include <sys/types.h> /* typedef ushort */
47 # include <netinet/in.h> /* BSD: struct in_addr */
51 # include <sys/shm.h> /* shmget(), shmctl() */
55 #include <X11/Xproto.h>
57 #include <X11/Xutil.h>
58 #include <X11/keysym.h>
60 # include <X11/extensions/XShm.h>
62 #ifdef DPMSINFO_IN_DPMS_H
63 # include <X11/extensions/dpms.h>
66 #ifdef MODULE_NAME_IS_xvideo
67 # include <X11/extensions/Xv.h>
68 # include <X11/extensions/Xvlib.h>
71 #ifdef MODULE_NAME_IS_glx
76 # include <X11/extensions/Xinerama.h>
81 /*****************************************************************************
83 *****************************************************************************/
84 int E_(Activate) ( vlc_object_t * );
85 void E_(Deactivate) ( vlc_object_t * );
87 static int InitVideo ( vout_thread_t * );
88 static void EndVideo ( vout_thread_t * );
89 static void DisplayVideo ( vout_thread_t *, picture_t * );
90 static int ManageVideo ( vout_thread_t * );
91 static int Control ( vout_thread_t *, int, va_list );
93 static int InitDisplay ( vout_thread_t * );
95 static int CreateWindow ( vout_thread_t *, x11_window_t * );
96 static void DestroyWindow ( vout_thread_t *, x11_window_t * );
98 static int NewPicture ( vout_thread_t *, picture_t * );
99 static void FreePicture ( vout_thread_t *, picture_t * );
101 static IMAGE_TYPE *CreateImage ( vout_thread_t *,
102 Display *, EXTRA_ARGS, int, int );
103 #ifdef HAVE_SYS_SHM_H
104 static IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
105 Display *, EXTRA_ARGS_SHM, int, int );
106 static vlc_bool_t b_shm = VLC_TRUE;
109 static void ToggleFullScreen ( vout_thread_t * );
111 static void EnableXScreenSaver ( vout_thread_t * );
112 static void DisableXScreenSaver ( vout_thread_t * );
114 static void CreateCursor ( vout_thread_t * );
115 static void DestroyCursor ( vout_thread_t * );
116 static void ToggleCursor ( vout_thread_t * );
118 #ifdef MODULE_NAME_IS_xvideo
119 static int XVideoGetPort ( vout_thread_t *, vlc_fourcc_t, vlc_fourcc_t * );
120 static void XVideoReleasePort( vout_thread_t *, int );
123 #ifdef MODULE_NAME_IS_x11
124 static void SetPalette ( vout_thread_t *,
125 uint16_t *, uint16_t *, uint16_t * );
128 static void TestNetWMSupport( vout_thread_t * );
129 static int ConvertKey( int );
131 static int WindowOnTop( vout_thread_t *, vlc_bool_t );
133 static int X11ErrorHandler( Display *, XErrorEvent * );
135 /*****************************************************************************
136 * Activate: allocate X11 video thread output method
137 *****************************************************************************
138 * This function allocate and initialize a X11 vout method. It uses some of the
139 * vout properties to choose the window size, and change them according to the
140 * actual properties of the display.
141 *****************************************************************************/
142 int E_(Activate) ( vlc_object_t *p_this )
144 vout_thread_t *p_vout = (vout_thread_t *)p_this;
148 #ifdef MODULE_NAME_IS_xvideo
150 vlc_fourcc_t i_chroma = 0;
151 vlc_bool_t b_chroma = 0;
154 p_vout->pf_init = InitVideo;
155 p_vout->pf_end = EndVideo;
156 p_vout->pf_manage = ManageVideo;
157 p_vout->pf_render = NULL;
158 p_vout->pf_display = DisplayVideo;
159 p_vout->pf_control = Control;
161 /* Allocate structure */
162 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
163 if( p_vout->p_sys == NULL )
165 msg_Err( p_vout, "out of memory" );
169 vlc_mutex_init( p_vout, &p_vout->p_sys->lock );
171 /* Open display, using the "display" config variable or the DISPLAY
172 * environment variable */
173 psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
175 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
177 if( p_vout->p_sys->p_display == NULL ) /* error */
179 msg_Err( p_vout, "cannot open display %s",
180 XDisplayName( psz_display ) );
181 free( p_vout->p_sys );
182 if( psz_display ) free( psz_display );
185 if( psz_display ) free( psz_display );
187 /* Replace error handler so we can intercept some non-fatal errors */
188 XSetErrorHandler( X11ErrorHandler );
190 /* Get a screen ID matching the XOpenDisplay return value */
191 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
193 #ifdef MODULE_NAME_IS_xvideo
194 psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
197 if( strlen( psz_chroma ) >= 4 )
199 /* Do not use direct assignment because we are not sure of the
201 memcpy(&i_chroma, psz_chroma, 4);
210 msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
211 i_chroma, (char*)&i_chroma );
215 i_chroma = p_vout->render.i_chroma;
218 /* Check that we have access to an XVideo port providing this chroma */
219 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, VLC2X11_FOURCC(i_chroma),
220 &p_vout->output.i_chroma );
221 if( p_vout->p_sys->i_xvport < 0 )
223 /* If a specific chroma format was requested, then we don't try to
224 * be cleverer than the user. He knew pretty well what he wanted. */
227 XCloseDisplay( p_vout->p_sys->p_display );
228 free( p_vout->p_sys );
232 /* It failed, but it's not completely lost ! We try to open an
233 * XVideo port for an YUY2 picture. We'll need to do an YUV
234 * conversion, but at least it has got scaling. */
235 p_vout->p_sys->i_xvport =
236 XVideoGetPort( p_vout, X11_FOURCC('Y','U','Y','2'),
237 &p_vout->output.i_chroma );
238 if( p_vout->p_sys->i_xvport < 0 )
240 /* It failed, but it's not completely lost ! We try to open an
241 * XVideo port for a simple 16bpp RGB picture. We'll need to do
242 * an YUV conversion, but at least it has got scaling. */
243 p_vout->p_sys->i_xvport =
244 XVideoGetPort( p_vout, X11_FOURCC('R','V','1','6'),
245 &p_vout->output.i_chroma );
246 if( p_vout->p_sys->i_xvport < 0 )
248 XCloseDisplay( p_vout->p_sys->p_display );
249 free( p_vout->p_sys );
254 p_vout->output.i_chroma = X112VLC_FOURCC(p_vout->output.i_chroma);
257 /* Create blank cursor (for mouse cursor autohiding) */
258 p_vout->p_sys->i_time_mouse_last_moved = mdate();
259 p_vout->p_sys->b_mouse_pointer_visible = 1;
260 CreateCursor( p_vout );
262 /* Set main window's size */
263 p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
264 p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
266 /* Spawn base window - this window will include the video output window,
267 * but also command buttons, subtitles and other indicators */
268 if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
270 msg_Err( p_vout, "cannot create X11 window" );
271 DestroyCursor( p_vout );
272 XCloseDisplay( p_vout->p_sys->p_display );
273 free( p_vout->p_sys );
277 /* Open and initialize device. */
278 if( InitDisplay( p_vout ) )
280 msg_Err( p_vout, "cannot initialize X11 display" );
281 DestroyCursor( p_vout );
282 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
283 XCloseDisplay( p_vout->p_sys->p_display );
284 free( p_vout->p_sys );
288 /* Disable screen saver */
289 DisableXScreenSaver( p_vout );
292 p_vout->p_sys->b_altfullscreen = 0;
293 p_vout->p_sys->i_time_button_last_pressed = 0;
295 TestNetWMSupport( p_vout );
297 /* Variable to indicate if the window should be on top of others */
298 /* Trigger a callback right now */
299 var_Get( p_vout, "video-on-top", &val );
300 var_Set( p_vout, "video-on-top", val );
305 /*****************************************************************************
306 * Deactivate: destroy X11 video thread output method
307 *****************************************************************************
308 * Terminate an output method created by Open
309 *****************************************************************************/
310 void E_(Deactivate) ( vlc_object_t *p_this )
312 vout_thread_t *p_vout = (vout_thread_t *)p_this;
314 /* If the fullscreen window is still open, close it */
315 if( p_vout->b_fullscreen )
317 ToggleFullScreen( p_vout );
320 /* Restore cursor if it was blanked */
321 if( !p_vout->p_sys->b_mouse_pointer_visible )
323 ToggleCursor( p_vout );
326 #ifdef MODULE_NAME_IS_x11
327 /* Destroy colormap */
328 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
330 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
332 #elif defined(MODULE_NAME_IS_xvideo)
333 XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
336 DestroyCursor( p_vout );
337 EnableXScreenSaver( p_vout );
338 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
340 XCloseDisplay( p_vout->p_sys->p_display );
342 /* Destroy structure */
343 vlc_mutex_destroy( &p_vout->p_sys->lock );
344 free( p_vout->p_sys );
347 /*****************************************************************************
348 * InitVideo: initialize X11 video thread output method
349 *****************************************************************************
350 * This function create the XImages needed by the output thread. It is called
351 * at the beginning of the thread, but also each time the window is resized.
352 *****************************************************************************/
353 static int InitVideo( vout_thread_t *p_vout )
358 I_OUTPUTPICTURES = 0;
360 #ifdef MODULE_NAME_IS_xvideo
361 /* Initialize the output structure; we already found an XVideo port,
362 * and the corresponding chroma we will be using. Since we can
363 * arbitrary scale, stick to the coordinates and aspect. */
364 p_vout->output.i_width = p_vout->render.i_width;
365 p_vout->output.i_height = p_vout->render.i_height;
366 p_vout->output.i_aspect = p_vout->render.i_aspect;
368 switch( p_vout->output.i_chroma )
370 case VLC_FOURCC('R','V','1','5'):
371 p_vout->output.i_rmask = 0x001f;
372 p_vout->output.i_gmask = 0x07e0;
373 p_vout->output.i_bmask = 0xf800;
375 case VLC_FOURCC('R','V','1','6'):
376 p_vout->output.i_rmask = 0x001f;
377 p_vout->output.i_gmask = 0x03e0;
378 p_vout->output.i_bmask = 0x7c00;
382 #elif defined(MODULE_NAME_IS_x11)
383 /* Initialize the output structure: RGB with square pixels, whatever
384 * the input format is, since it's the only format we know */
385 switch( p_vout->p_sys->i_screen_depth )
387 case 8: /* FIXME: set the palette */
388 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
390 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
392 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
395 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
397 msg_Err( p_vout, "unknown screen depth %i",
398 p_vout->p_sys->i_screen_depth );
402 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
403 p_vout->p_sys->p_win->i_height,
405 &p_vout->output.i_width, &p_vout->output.i_height );
407 /* Assume we have square pixels */
408 p_vout->output.i_aspect = p_vout->output.i_width
409 * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
412 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
413 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
417 /* Find an empty picture slot */
418 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
420 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
422 p_pic = p_vout->p_picture + i_index;
427 /* Allocate the picture */
428 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
433 p_pic->i_status = DESTROYED_PICTURE;
434 p_pic->i_type = DIRECT_PICTURE;
436 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
441 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
443 /* U and V inverted compared to I420
444 * Fixme: this should be handled by the vout core */
445 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
451 /*****************************************************************************
452 * DisplayVideo: displays previously rendered output
453 *****************************************************************************
454 * This function sends the currently rendered image to X11 server.
455 * (The Xv extension takes care of "double-buffering".)
456 *****************************************************************************/
457 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
459 int i_width, i_height, i_x, i_y;
461 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
462 p_vout->p_sys->p_win->i_height,
463 &i_x, &i_y, &i_width, &i_height );
465 vlc_mutex_lock( &p_vout->p_sys->lock );
467 #ifdef HAVE_SYS_SHM_H
468 if( p_vout->p_sys->b_shm )
470 /* Display rendered image using shared memory extension */
471 # ifdef MODULE_NAME_IS_xvideo
472 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
473 p_vout->p_sys->p_win->video_window,
474 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
475 0 /*src_x*/, 0 /*src_y*/,
476 p_vout->output.i_width, p_vout->output.i_height,
477 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
478 False /* Don't put True here or you'll waste your CPU */ );
480 XShmPutImage( p_vout->p_sys->p_display,
481 p_vout->p_sys->p_win->video_window,
482 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
483 0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
484 p_vout->output.i_width, p_vout->output.i_height,
485 False /* Don't put True here ! */ );
489 #endif /* HAVE_SYS_SHM_H */
491 /* Use standard XPutImage -- this is gonna be slow ! */
492 #ifdef MODULE_NAME_IS_xvideo
493 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
494 p_vout->p_sys->p_win->video_window,
495 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
496 0 /*src_x*/, 0 /*src_y*/,
497 p_vout->output.i_width, p_vout->output.i_height,
498 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
500 XPutImage( p_vout->p_sys->p_display,
501 p_vout->p_sys->p_win->video_window,
502 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
503 0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
504 p_vout->output.i_width, p_vout->output.i_height );
508 /* Make sure the command is sent now - do NOT use XFlush !*/
509 XSync( p_vout->p_sys->p_display, False );
511 vlc_mutex_unlock( &p_vout->p_sys->lock );
514 /*****************************************************************************
515 * ManageVideo: handle X11 events
516 *****************************************************************************
517 * This function should be called regularly by video output thread. It manages
518 * X11 events and allows window resizing. It returns a non null value on
520 *****************************************************************************/
521 static int ManageVideo( vout_thread_t *p_vout )
523 XEvent xevent; /* X11 event */
526 vlc_mutex_lock( &p_vout->p_sys->lock );
528 /* Handle events from the owner window */
529 if( p_vout->p_sys->p_win->owner_window )
531 while( XCheckWindowEvent( p_vout->p_sys->p_display,
532 p_vout->p_sys->p_win->owner_window,
533 StructureNotifyMask, &xevent ) == True )
535 /* ConfigureNotify event: prepare */
536 if( xevent.type == ConfigureNotify )
538 /* Update dimensions */
539 XResizeWindow( p_vout->p_sys->p_display,
540 p_vout->p_sys->p_win->base_window,
541 xevent.xconfigure.width,
542 xevent.xconfigure.height );
547 /* Handle X11 events: ConfigureNotify events are parsed to know if the
548 * output window's size changed, MapNotify and UnmapNotify to know if the
549 * window is mapped (and if the display is useful), and ClientMessages
550 * to intercept window destruction requests */
552 while( XCheckWindowEvent( p_vout->p_sys->p_display,
553 p_vout->p_sys->p_win->base_window,
554 StructureNotifyMask | KeyPressMask |
555 ButtonPressMask | ButtonReleaseMask |
556 PointerMotionMask | Button1MotionMask , &xevent )
559 /* ConfigureNotify event: prepare */
560 if( xevent.type == ConfigureNotify )
562 if( (unsigned int)xevent.xconfigure.width
563 != p_vout->p_sys->p_win->i_width
564 || (unsigned int)xevent.xconfigure.height
565 != p_vout->p_sys->p_win->i_height )
567 /* Update dimensions */
568 p_vout->i_changes |= VOUT_SIZE_CHANGE;
569 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
570 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
574 else if( xevent.type == KeyPress )
576 unsigned int state = xevent.xkey.state;
578 char i_key; /* ISO Latin-1 key */
580 /* We may have keys like F1 trough F12, ESC ... */
581 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
582 xevent.xkey.keycode, 0 );
583 val.i_int = ConvertKey( (int)x_key_symbol );
585 xevent.xkey.state &= ~ShiftMask;
586 xevent.xkey.state &= ~ControlMask;
587 xevent.xkey.state &= ~Mod1Mask;
590 XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
593 * The reason why I use this instead of XK_0 is that
594 * with XLookupString, we don't have to care about
601 if( state & ShiftMask )
603 val.i_int |= KEY_MODIFIER_SHIFT;
605 if( state & ControlMask )
607 val.i_int |= KEY_MODIFIER_CTRL;
609 if( state & Mod1Mask )
611 val.i_int |= KEY_MODIFIER_ALT;
613 var_Set( p_vout->p_vlc, "key-pressed", val );
617 else if( xevent.type == ButtonPress )
619 switch( ((XButtonEvent *)&xevent)->button )
622 var_Get( p_vout, "mouse-button-down", &val );
624 var_Set( p_vout, "mouse-button-down", val );
626 /* detect double-clicks */
627 if( ( ((XButtonEvent *)&xevent)->time -
628 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
630 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
633 p_vout->p_sys->i_time_button_last_pressed =
634 ((XButtonEvent *)&xevent)->time;
637 var_Get( p_vout, "mouse-button-down", &val );
639 var_Set( p_vout, "mouse-button-down", val );
643 var_Get( p_vout, "mouse-button-down", &val );
645 var_Set( p_vout, "mouse-button-down", val );
649 var_Get( p_vout, "mouse-button-down", &val );
651 var_Set( p_vout, "mouse-button-down", val );
655 var_Get( p_vout, "mouse-button-down", &val );
657 var_Set( p_vout, "mouse-button-down", val );
662 else if( xevent.type == ButtonRelease )
664 switch( ((XButtonEvent *)&xevent)->button )
667 var_Get( p_vout, "mouse-button-down", &val );
669 var_Set( p_vout, "mouse-button-down", val );
671 val.b_bool = VLC_TRUE;
672 var_Set( p_vout, "mouse-clicked", val );
677 playlist_t *p_playlist;
679 var_Get( p_vout, "mouse-button-down", &val );
681 var_Set( p_vout, "mouse-button-down", val );
683 p_playlist = vlc_object_find( p_vout,
686 if( p_playlist != NULL )
689 var_Get( p_playlist, "intf-show", &val );
690 val.b_bool = !val.b_bool;
691 var_Set( p_playlist, "intf-show", val );
692 vlc_object_release( p_playlist );
699 intf_thread_t *p_intf;
700 playlist_t *p_playlist;
702 var_Get( p_vout, "mouse-button-down", &val );
704 var_Set( p_vout, "mouse-button-down", val );
705 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
709 p_intf->b_menu_change = 1;
710 vlc_object_release( p_intf );
713 p_playlist = vlc_object_find( p_vout,
716 if( p_playlist != NULL )
718 vlc_value_t val; val.b_bool = VLC_TRUE;
719 var_Set( p_playlist, "intf-popupmenu", val );
720 vlc_object_release( p_playlist );
726 var_Get( p_vout, "mouse-button-down", &val );
728 var_Set( p_vout, "mouse-button-down", val );
732 var_Get( p_vout, "mouse-button-down", &val );
734 var_Set( p_vout, "mouse-button-down", val );
740 else if( xevent.type == MotionNotify )
742 int i_width, i_height, i_x, i_y;
745 /* somewhat different use for vout_PlacePicture:
746 * here the values are needed to give to mouse coordinates
747 * in the original picture space */
748 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
749 p_vout->p_sys->p_win->i_height,
750 &i_x, &i_y, &i_width, &i_height );
752 val.i_int = ( xevent.xmotion.x - i_x )
753 * p_vout->render.i_width / i_width;
754 var_Set( p_vout, "mouse-x", val );
755 val.i_int = ( xevent.xmotion.y - i_y )
756 * p_vout->render.i_height / i_height;
757 var_Set( p_vout, "mouse-y", val );
759 val.b_bool = VLC_TRUE;
760 var_Set( p_vout, "mouse-moved", val );
762 p_vout->p_sys->i_time_mouse_last_moved = mdate();
763 if( ! p_vout->p_sys->b_mouse_pointer_visible )
765 ToggleCursor( p_vout );
768 else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
769 || xevent.type == MapNotify
770 || xevent.type == UnmapNotify )
772 /* Ignore these events */
774 else /* Other events */
776 msg_Warn( p_vout, "unhandled event %d received", xevent.type );
780 /* Handle events for video output sub-window */
781 while( XCheckWindowEvent( p_vout->p_sys->p_display,
782 p_vout->p_sys->p_win->video_window,
783 ExposureMask, &xevent ) == True )
785 /* Window exposed (only handled if stream playback is paused) */
786 if( xevent.type == Expose )
788 if( ((XExposeEvent *)&xevent)->count == 0 )
790 /* (if this is the last a collection of expose events...) */
792 if( p_vout->p_vlc->p_input_bank->pp_input[0] != NULL )
794 if( PAUSE_S == p_vout->p_vlc->p_input_bank->pp_input[0]
795 ->stream.control.i_status )
797 /* XVideoDisplay( p_vout )*/;
805 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
806 * are handled - according to the man pages, the format is always 32
808 while( XCheckTypedEvent( p_vout->p_sys->p_display,
809 ClientMessage, &xevent ) )
811 if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
812 && ((Atom)xevent.xclient.data.l[0]
813 == p_vout->p_sys->p_win->wm_delete_window ) )
815 /* the user wants to close the window */
816 playlist_t * p_playlist =
817 (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
819 if( p_playlist != NULL )
821 playlist_Stop( p_playlist );
822 vlc_object_release( p_playlist );
830 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
834 /* Update the object variable and trigger callback */
835 val.b_bool = !p_vout->b_fullscreen;
836 var_Set( p_vout, "fullscreen", val );
838 ToggleFullScreen( p_vout );
839 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
845 * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
846 * the size flag inside the fullscreen routine)
848 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
850 int i_width, i_height, i_x, i_y;
852 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
854 #ifdef MODULE_NAME_IS_x11
855 /* We need to signal the vout thread about the size change because it
856 * is doing the rescaling */
857 p_vout->i_changes |= VOUT_SIZE_CHANGE;
860 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
861 p_vout->p_sys->p_win->i_height,
862 &i_x, &i_y, &i_width, &i_height );
864 XMoveResizeWindow( p_vout->p_sys->p_display,
865 p_vout->p_sys->p_win->video_window,
866 i_x, i_y, i_width, i_height );
869 /* Autohide Cursour */
870 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
872 /* Hide the mouse automatically */
873 if( p_vout->p_sys->b_mouse_pointer_visible )
875 ToggleCursor( p_vout );
879 vlc_mutex_unlock( &p_vout->p_sys->lock );
884 /*****************************************************************************
885 * EndVideo: terminate X11 video thread output method
886 *****************************************************************************
887 * Destroy the X11 XImages created by Init. It is called at the end of
888 * the thread, but also each time the window is resized.
889 *****************************************************************************/
890 static void EndVideo( vout_thread_t *p_vout )
894 /* Free the direct buffers we allocated */
895 for( i_index = I_OUTPUTPICTURES ; i_index ; )
898 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
902 /* following functions are local */
904 /*****************************************************************************
905 * CreateWindow: open and set-up X11 main window
906 *****************************************************************************/
907 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
909 XSizeHints xsize_hints;
910 XSetWindowAttributes xwindow_attributes;
914 vlc_bool_t b_expose = VLC_FALSE;
915 vlc_bool_t b_configure_notify = VLC_FALSE;
916 vlc_bool_t b_map_notify = VLC_FALSE;
918 /* Prepare window manager hints and properties */
919 p_win->wm_protocols =
920 XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
921 p_win->wm_delete_window =
922 XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
924 /* Never have a 0-pixel-wide window */
925 xsize_hints.min_width = 2;
926 xsize_hints.min_height = 1;
928 /* Prepare window attributes */
929 xwindow_attributes.backing_store = Always; /* save the hidden part */
930 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
931 p_vout->p_sys->i_screen);
932 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
934 if( !p_vout->b_fullscreen )
936 p_win->owner_window =
937 (Window)vout_RequestWindow( p_vout, &p_win->i_x, &p_win->i_y,
938 &p_win->i_width, &p_win->i_height );
940 xsize_hints.base_width = xsize_hints.width = p_win->i_width;
941 xsize_hints.base_height = xsize_hints.height = p_win->i_height;
942 xsize_hints.flags = PSize | PMinSize;
944 if( p_win->i_x >=0 || p_win->i_y >= 0 )
946 xsize_hints.x = p_win->i_x;
947 xsize_hints.y = p_win->i_y;
948 xsize_hints.flags |= PPosition;
953 /* Fullscreen window size and position */
954 p_win->owner_window = 0;
955 p_win->i_x = p_win->i_y = 0;
957 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
959 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
962 if( !p_win->owner_window )
964 /* Create the window and set hints - the window must receive
965 * ConfigureNotify events, and until it is displayed, Expose and
966 * MapNotify events. */
969 XCreateWindow( p_vout->p_sys->p_display,
970 DefaultRootWindow( p_vout->p_sys->p_display ),
971 p_win->i_x, p_win->i_y,
972 p_win->i_width, p_win->i_height,
975 CWBackingStore | CWBackPixel | CWEventMask,
976 &xwindow_attributes );
978 if( !p_vout->b_fullscreen )
980 /* Set window manager hints and properties: size hints, command,
981 * window's name, and accepted protocols */
982 XSetWMNormalHints( p_vout->p_sys->p_display,
983 p_win->base_window, &xsize_hints );
984 XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
985 p_vout->p_vlc->ppsz_argv, p_vout->p_vlc->i_argc );
987 if( !var_GetBool( p_vout, "video-deco") )
992 mwmhints.flags = MWM_HINTS_DECORATIONS;
993 mwmhints.decorations = False;
995 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
998 XChangeProperty( p_vout->p_sys->p_display,
1000 prop, prop, 32, PropModeReplace,
1001 (unsigned char *)&mwmhints,
1002 PROP_MWM_HINTS_ELEMENTS );
1006 XStoreName( p_vout->p_sys->p_display, p_win->base_window,
1007 #ifdef MODULE_NAME_IS_x11
1008 VOUT_TITLE " (X11 output)"
1009 #elif defined(MODULE_NAME_IS_glx)
1010 VOUT_TITLE " (GLX output)"
1012 VOUT_TITLE " (XVideo output)"
1021 unsigned int dummy2, dummy3;
1023 /* Select events we are interested in. */
1024 XSelectInput( p_vout->p_sys->p_display, p_win->owner_window,
1025 StructureNotifyMask );
1027 /* Get the parent window's geometry information */
1028 XGetGeometry( p_vout->p_sys->p_display, p_win->owner_window,
1029 &dummy1, &dummy2, &dummy3,
1034 /* We are already configured */
1035 b_configure_notify = VLC_TRUE;
1037 /* From man XSelectInput: only one client at a time can select a
1038 * ButtonPress event, so we need to open a new window anyway. */
1039 p_win->base_window =
1040 XCreateWindow( p_vout->p_sys->p_display,
1041 p_win->owner_window,
1043 p_win->i_width, p_win->i_height,
1045 0, CopyFromParent, 0,
1046 CWBackingStore | CWBackPixel | CWEventMask,
1047 &xwindow_attributes );
1050 if( (p_win->wm_protocols == None) /* use WM_DELETE_WINDOW */
1051 || (p_win->wm_delete_window == None)
1052 || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
1053 &p_win->wm_delete_window, 1 ) )
1055 /* WM_DELETE_WINDOW is not supported by window manager */
1056 msg_Warn( p_vout, "missing or bad window manager" );
1059 /* Creation of a graphic context that doesn't generate a GraphicsExpose
1060 * event when using functions like XCopyArea */
1061 xgcvalues.graphics_exposures = False;
1062 p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1064 GCGraphicsExposures, &xgcvalues );
1066 /* Send orders to server, and wait until window is displayed - three
1067 * events must be received: a MapNotify event, an Expose event allowing
1068 * drawing in the window, and a ConfigureNotify to get the window
1069 * dimensions. Once those events have been received, only
1070 * ConfigureNotify events need to be received. */
1071 XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1074 XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1075 SubstructureNotifyMask | StructureNotifyMask |
1076 ExposureMask, &xevent);
1077 if( (xevent.type == Expose)
1078 && (xevent.xexpose.window == p_win->base_window) )
1080 b_expose = VLC_TRUE;
1081 /* ConfigureNotify isn't sent if there isn't a window manager.
1082 * Expose should be the last event to be received so it should
1083 * be fine to assume we won't receive it anymore. */
1084 b_configure_notify = VLC_TRUE;
1086 else if( (xevent.type == MapNotify)
1087 && (xevent.xmap.window == p_win->base_window) )
1089 b_map_notify = VLC_TRUE;
1091 else if( (xevent.type == ConfigureNotify)
1092 && (xevent.xconfigure.window == p_win->base_window) )
1094 b_configure_notify = VLC_TRUE;
1095 p_win->i_width = xevent.xconfigure.width;
1096 p_win->i_height = xevent.xconfigure.height;
1098 } while( !( b_expose && b_configure_notify && b_map_notify ) );
1100 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1101 StructureNotifyMask | KeyPressMask |
1102 ButtonPressMask | ButtonReleaseMask |
1103 PointerMotionMask );
1105 #ifdef MODULE_NAME_IS_x11
1106 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1108 /* Allocate a new palette */
1109 p_vout->p_sys->colormap =
1110 XCreateColormap( p_vout->p_sys->p_display,
1111 DefaultRootWindow( p_vout->p_sys->p_display ),
1112 DefaultVisual( p_vout->p_sys->p_display,
1113 p_vout->p_sys->i_screen ),
1116 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1117 XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1118 CWColormap, &xwindow_attributes );
1122 /* Create video output sub-window. */
1123 p_win->video_window = XCreateSimpleWindow(
1124 p_vout->p_sys->p_display,
1125 p_win->base_window, 0, 0,
1126 p_win->i_width, p_win->i_height,
1128 BlackPixel( p_vout->p_sys->p_display,
1129 p_vout->p_sys->i_screen ),
1130 WhitePixel( p_vout->p_sys->p_display,
1131 p_vout->p_sys->i_screen ) );
1133 XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1134 BlackPixel( p_vout->p_sys->p_display,
1135 p_vout->p_sys->i_screen ) );
1137 XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1138 XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1141 /* make sure the video window will be centered in the next ManageVideo() */
1142 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1144 /* If the cursor was formerly blank than blank it again */
1145 if( !p_vout->p_sys->b_mouse_pointer_visible )
1147 ToggleCursor( p_vout );
1148 ToggleCursor( p_vout );
1151 /* Do NOT use XFlush here ! */
1152 XSync( p_vout->p_sys->p_display, False );
1154 /* At this stage, the window is open, displayed, and ready to
1156 p_vout->p_sys->p_win = p_win;
1161 /*****************************************************************************
1162 * DestroyWindow: destroy the window
1163 *****************************************************************************
1165 *****************************************************************************/
1166 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1168 /* Do NOT use XFlush here ! */
1169 XSync( p_vout->p_sys->p_display, False );
1171 if( p_win->video_window != None )
1172 XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1174 XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1176 XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1177 XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1179 if( p_win->owner_window )
1180 vout_ReleaseWindow( p_vout, (void *)p_win->owner_window );
1183 /*****************************************************************************
1184 * NewPicture: allocate a picture
1185 *****************************************************************************
1186 * Returns 0 on success, -1 otherwise
1187 *****************************************************************************/
1188 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1190 #ifndef MODULE_NAME_IS_glx
1192 #ifdef MODULE_NAME_IS_xvideo
1196 /* We know the chroma, allocate a buffer which will be used
1197 * directly by the decoder */
1198 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1200 if( p_pic->p_sys == NULL )
1205 /* Fill in picture_t fields */
1206 vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
1207 p_vout->output.i_width, p_vout->output.i_height,
1208 p_vout->output.i_aspect );
1210 #ifdef HAVE_SYS_SHM_H
1211 if( p_vout->p_sys->b_shm )
1213 /* Create image using XShm extension */
1214 p_pic->p_sys->p_image =
1215 CreateShmImage( p_vout, p_vout->p_sys->p_display,
1216 # ifdef MODULE_NAME_IS_xvideo
1217 p_vout->p_sys->i_xvport,
1218 VLC2X11_FOURCC(p_vout->output.i_chroma),
1220 p_vout->p_sys->p_visual,
1221 p_vout->p_sys->i_screen_depth,
1223 &p_pic->p_sys->shminfo,
1224 p_vout->output.i_width, p_vout->output.i_height );
1227 if( !p_vout->p_sys->b_shm || !p_pic->p_sys->p_image )
1228 #endif /* HAVE_SYS_SHM_H */
1230 /* Create image without XShm extension */
1231 p_pic->p_sys->p_image =
1232 CreateImage( p_vout, p_vout->p_sys->p_display,
1233 #ifdef MODULE_NAME_IS_xvideo
1234 p_vout->p_sys->i_xvport,
1235 VLC2X11_FOURCC(p_vout->output.i_chroma),
1236 p_pic->format.i_bits_per_pixel,
1238 p_vout->p_sys->p_visual,
1239 p_vout->p_sys->i_screen_depth,
1240 p_vout->p_sys->i_bytes_per_pixel,
1242 p_vout->output.i_width, p_vout->output.i_height );
1244 #ifdef HAVE_SYS_SHM_H
1245 if( p_pic->p_sys->p_image && p_vout->p_sys->b_shm )
1247 msg_Warn( p_vout, "couldn't create SHM image, disabling SHM." );
1248 p_vout->p_sys->b_shm = VLC_FALSE;
1250 #endif /* HAVE_SYS_SHM_H */
1253 if( p_pic->p_sys->p_image == NULL )
1255 free( p_pic->p_sys );
1259 switch( p_vout->output.i_chroma )
1261 #ifdef MODULE_NAME_IS_xvideo
1262 case VLC_FOURCC('I','4','2','0'):
1263 case VLC_FOURCC('Y','V','1','2'):
1264 case VLC_FOURCC('Y','2','1','1'):
1265 case VLC_FOURCC('Y','U','Y','2'):
1266 case VLC_FOURCC('U','Y','V','Y'):
1267 case VLC_FOURCC('R','V','1','5'):
1268 case VLC_FOURCC('R','V','1','6'):
1269 case VLC_FOURCC('R','V','2','4'): /* Fixme: pixel pitch == 4 ? */
1270 case VLC_FOURCC('R','V','3','2'):
1272 for( i_plane = 0; i_plane < p_pic->p_sys->p_image->num_planes;
1275 p_pic->p[i_plane].p_pixels = p_pic->p_sys->p_image->data
1276 + p_pic->p_sys->p_image->offsets[i_plane];
1277 p_pic->p[i_plane].i_pitch =
1278 p_pic->p_sys->p_image->pitches[i_plane];
1280 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
1282 /* U and V inverted compared to I420
1283 * Fixme: this should be handled by the vout core */
1284 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1285 + p_pic->p_sys->p_image->offsets[2];
1286 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1287 + p_pic->p_sys->p_image->offsets[1];
1292 case VLC_FOURCC('R','G','B','2'):
1293 case VLC_FOURCC('R','V','1','6'):
1294 case VLC_FOURCC('R','V','1','5'):
1295 case VLC_FOURCC('R','V','2','4'):
1296 case VLC_FOURCC('R','V','3','2'):
1298 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1299 p_pic->p->i_visible_lines = p_pic->p_sys->p_image->height;
1300 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1301 + p_pic->p_sys->p_image->xoffset;
1302 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1304 /* p_pic->p->i_pixel_pitch = 4 for RV24 but this should be set
1305 * properly by vout_InitPicture() */
1306 p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
1307 * p_pic->p_sys->p_image->width;
1312 /* Unknown chroma, tell the guy to get lost */
1313 IMAGE_FREE( p_pic->p_sys->p_image );
1314 free( p_pic->p_sys );
1315 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1316 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1317 p_pic->i_planes = 0;
1321 #endif /* !MODULE_NAME_IS_glx */
1326 /*****************************************************************************
1327 * FreePicture: destroy a picture allocated with NewPicture
1328 *****************************************************************************
1329 * Destroy XImage AND associated data. If using Shm, detach shared memory
1330 * segment from server and process, then free it. The XDestroyImage manpage
1331 * says that both the image structure _and_ the data pointed to by the
1332 * image structure are freed, so no need to free p_image->data.
1333 *****************************************************************************/
1334 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1336 /* The order of operations is correct */
1337 #ifdef HAVE_SYS_SHM_H
1338 if( p_vout->p_sys->b_shm )
1340 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
1341 IMAGE_FREE( p_pic->p_sys->p_image );
1343 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
1344 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
1346 msg_Err( p_vout, "cannot detach shared memory (%s)",
1353 IMAGE_FREE( p_pic->p_sys->p_image );
1356 /* Do NOT use XFlush here ! */
1357 XSync( p_vout->p_sys->p_display, False );
1359 free( p_pic->p_sys );
1362 /*****************************************************************************
1363 * ToggleFullScreen: Enable or disable full screen mode
1364 *****************************************************************************
1365 * This function will switch between fullscreen and window mode.
1366 *****************************************************************************/
1367 static void ToggleFullScreen ( vout_thread_t *p_vout )
1371 mwmhints_t mwmhints;
1372 XSetWindowAttributes attributes;
1374 #ifdef HAVE_XINERAMA
1378 p_vout->b_fullscreen = !p_vout->b_fullscreen;
1380 if( p_vout->b_fullscreen )
1382 msg_Dbg( p_vout, "entering fullscreen mode" );
1384 p_vout->p_sys->b_altfullscreen =
1385 config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
1387 XUnmapWindow( p_vout->p_sys->p_display,
1388 p_vout->p_sys->p_win->base_window );
1390 p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
1392 CreateWindow( p_vout, p_vout->p_sys->p_win );
1393 XDestroyWindow( p_vout->p_sys->p_display,
1394 p_vout->p_sys->fullscreen_window.video_window );
1395 XReparentWindow( p_vout->p_sys->p_display,
1396 p_vout->p_sys->original_window.video_window,
1397 p_vout->p_sys->fullscreen_window.base_window, 0, 0 );
1398 p_vout->p_sys->fullscreen_window.video_window =
1399 p_vout->p_sys->original_window.video_window;
1401 /* To my knowledge there are two ways to create a borderless window.
1402 * There's the generic way which is to tell x to bypass the window
1403 * manager, but this creates problems with the focus of other
1405 * The other way is to use the motif property "_MOTIF_WM_HINTS" which
1406 * luckily seems to be supported by most window managers. */
1407 if( !p_vout->p_sys->b_altfullscreen )
1409 mwmhints.flags = MWM_HINTS_DECORATIONS;
1410 mwmhints.decorations = False;
1412 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1414 XChangeProperty( p_vout->p_sys->p_display,
1415 p_vout->p_sys->p_win->base_window,
1416 prop, prop, 32, PropModeReplace,
1417 (unsigned char *)&mwmhints,
1418 PROP_MWM_HINTS_ELEMENTS );
1422 /* brute force way to remove decorations */
1423 attributes.override_redirect = True;
1424 XChangeWindowAttributes( p_vout->p_sys->p_display,
1425 p_vout->p_sys->p_win->base_window,
1429 /* Make sure the change is effective */
1430 XReparentWindow( p_vout->p_sys->p_display,
1431 p_vout->p_sys->p_win->base_window,
1432 DefaultRootWindow( p_vout->p_sys->p_display ),
1436 if( p_vout->p_sys->b_net_wm_state_fullscreen )
1438 XClientMessageEvent event;
1440 memset( &event, 0, sizeof( XClientMessageEvent ) );
1442 event.type = ClientMessage;
1443 event.message_type = p_vout->p_sys->net_wm_state;
1444 event.display = p_vout->p_sys->p_display;
1445 event.window = p_vout->p_sys->p_win->base_window;
1447 event.data.l[ 0 ] = 1; /* set property */
1448 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_fullscreen;
1450 XSendEvent( p_vout->p_sys->p_display,
1451 DefaultRootWindow( p_vout->p_sys->p_display ),
1452 False, SubstructureRedirectMask,
1456 /* Make sure the change is effective */
1457 XReparentWindow( p_vout->p_sys->p_display,
1458 p_vout->p_sys->p_win->base_window,
1459 DefaultRootWindow( p_vout->p_sys->p_display ),
1462 #ifdef HAVE_XINERAMA
1463 if( XineramaQueryExtension( p_vout->p_sys->p_display, &i_d1, &i_d2 ) &&
1464 XineramaIsActive( p_vout->p_sys->p_display ) )
1466 XineramaScreenInfo *screens; /* infos for xinerama */
1469 msg_Dbg( p_vout, "using XFree Xinerama extension");
1471 #define SCREEN p_vout->p_sys->p_win->i_screen
1473 /* Get Information about Xinerama (num of screens) */
1474 screens = XineramaQueryScreens( p_vout->p_sys->p_display,
1478 SCREEN = config_GetInt( p_vout,
1479 MODULE_STRING "-xineramascreen" );
1481 /* just check that user has entered a good value */
1482 if( SCREEN >= i_num_screens || SCREEN < 0 )
1484 msg_Dbg( p_vout, "requested screen number invalid" );
1488 /* Get the X/Y upper left corner coordinate of the above screen */
1489 p_vout->p_sys->p_win->i_x = screens[SCREEN].x_org;
1490 p_vout->p_sys->p_win->i_y = screens[SCREEN].y_org;
1492 /* Set the Height/width to the screen resolution */
1493 p_vout->p_sys->p_win->i_width = screens[SCREEN].width;
1494 p_vout->p_sys->p_win->i_height = screens[SCREEN].height;
1504 /* The window wasn't necessarily created at the requested size */
1505 p_vout->p_sys->p_win->i_x = p_vout->p_sys->p_win->i_y = 0;
1506 p_vout->p_sys->p_win->i_width =
1507 DisplayWidth( p_vout->p_sys->p_display,
1508 p_vout->p_sys->i_screen );
1509 p_vout->p_sys->p_win->i_height =
1510 DisplayHeight( p_vout->p_sys->p_display,
1511 p_vout->p_sys->i_screen );
1514 XMoveResizeWindow( p_vout->p_sys->p_display,
1515 p_vout->p_sys->p_win->base_window,
1516 p_vout->p_sys->p_win->i_x,
1517 p_vout->p_sys->p_win->i_y,
1518 p_vout->p_sys->p_win->i_width,
1519 p_vout->p_sys->p_win->i_height );
1523 msg_Dbg( p_vout, "leaving fullscreen mode" );
1525 XReparentWindow( p_vout->p_sys->p_display,
1526 p_vout->p_sys->original_window.video_window,
1527 p_vout->p_sys->original_window.base_window, 0, 0 );
1529 p_vout->p_sys->fullscreen_window.video_window = None;
1530 DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
1531 p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
1533 XMapWindow( p_vout->p_sys->p_display,
1534 p_vout->p_sys->p_win->base_window );
1537 /* Unfortunately, using XSync() here is not enough to ensure the
1538 * window has already been mapped because the XMapWindow() request
1539 * has not necessarily been sent directly to our window (remember,
1540 * the call is first redirected to the window manager) */
1543 XWindowEvent( p_vout->p_sys->p_display,
1544 p_vout->p_sys->p_win->base_window,
1545 StructureNotifyMask, &xevent );
1546 } while( xevent.type != MapNotify );
1548 /* Be careful, this can generate a BadMatch error if the window is not
1549 * already mapped by the server (see above) */
1550 XSetInputFocus(p_vout->p_sys->p_display,
1551 p_vout->p_sys->p_win->base_window,
1555 /* signal that the size needs to be updated */
1556 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1559 /*****************************************************************************
1560 * EnableXScreenSaver: enable screen saver
1561 *****************************************************************************
1562 * This function enables the screen saver on a display after it has been
1563 * disabled by XDisableScreenSaver.
1564 * FIXME: what happens if multiple vlc sessions are running at the same
1566 *****************************************************************************/
1567 static void EnableXScreenSaver( vout_thread_t *p_vout )
1569 #ifdef DPMSINFO_IN_DPMS_H
1573 if( p_vout->p_sys->i_ss_timeout )
1575 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1576 p_vout->p_sys->i_ss_interval,
1577 p_vout->p_sys->i_ss_blanking,
1578 p_vout->p_sys->i_ss_exposure );
1581 /* Restore DPMS settings */
1582 #ifdef DPMSINFO_IN_DPMS_H
1583 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1585 if( p_vout->p_sys->b_ss_dpms )
1587 DPMSEnable( p_vout->p_sys->p_display );
1593 /*****************************************************************************
1594 * DisableXScreenSaver: disable screen saver
1595 *****************************************************************************
1596 * See XEnableXScreenSaver
1597 *****************************************************************************/
1598 static void DisableXScreenSaver( vout_thread_t *p_vout )
1600 #ifdef DPMSINFO_IN_DPMS_H
1604 /* Save screen saver information */
1605 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1606 &p_vout->p_sys->i_ss_interval,
1607 &p_vout->p_sys->i_ss_blanking,
1608 &p_vout->p_sys->i_ss_exposure );
1610 /* Disable screen saver */
1611 if( p_vout->p_sys->i_ss_timeout )
1613 XSetScreenSaver( p_vout->p_sys->p_display, 0,
1614 p_vout->p_sys->i_ss_interval,
1615 p_vout->p_sys->i_ss_blanking,
1616 p_vout->p_sys->i_ss_exposure );
1620 #ifdef DPMSINFO_IN_DPMS_H
1621 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1624 /* Save DPMS current state */
1625 DPMSInfo( p_vout->p_sys->p_display, &unused,
1626 &p_vout->p_sys->b_ss_dpms );
1627 DPMSDisable( p_vout->p_sys->p_display );
1632 /*****************************************************************************
1633 * CreateCursor: create a blank mouse pointer
1634 *****************************************************************************/
1635 static void CreateCursor( vout_thread_t *p_vout )
1637 XColor cursor_color;
1639 p_vout->p_sys->cursor_pixmap =
1640 XCreatePixmap( p_vout->p_sys->p_display,
1641 DefaultRootWindow( p_vout->p_sys->p_display ),
1644 XParseColor( p_vout->p_sys->p_display,
1645 XCreateColormap( p_vout->p_sys->p_display,
1647 p_vout->p_sys->p_display ),
1649 p_vout->p_sys->p_display,
1650 p_vout->p_sys->i_screen ),
1652 "black", &cursor_color );
1654 p_vout->p_sys->blank_cursor =
1655 XCreatePixmapCursor( p_vout->p_sys->p_display,
1656 p_vout->p_sys->cursor_pixmap,
1657 p_vout->p_sys->cursor_pixmap,
1658 &cursor_color, &cursor_color, 1, 1 );
1661 /*****************************************************************************
1662 * DestroyCursor: destroy the blank mouse pointer
1663 *****************************************************************************/
1664 static void DestroyCursor( vout_thread_t *p_vout )
1666 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
1669 /*****************************************************************************
1670 * ToggleCursor: hide or show the mouse pointer
1671 *****************************************************************************
1672 * This function hides the X pointer if it is visible by setting the pointer
1673 * sprite to a blank one. To show it again, we disable the sprite.
1674 *****************************************************************************/
1675 static void ToggleCursor( vout_thread_t *p_vout )
1677 if( p_vout->p_sys->b_mouse_pointer_visible )
1679 XDefineCursor( p_vout->p_sys->p_display,
1680 p_vout->p_sys->p_win->base_window,
1681 p_vout->p_sys->blank_cursor );
1682 p_vout->p_sys->b_mouse_pointer_visible = 0;
1686 XUndefineCursor( p_vout->p_sys->p_display,
1687 p_vout->p_sys->p_win->base_window );
1688 p_vout->p_sys->b_mouse_pointer_visible = 1;
1692 #ifdef MODULE_NAME_IS_xvideo
1693 /*****************************************************************************
1694 * XVideoGetPort: get YUV12 port
1695 *****************************************************************************/
1696 static int XVideoGetPort( vout_thread_t *p_vout,
1697 vlc_fourcc_t i_chroma, vlc_fourcc_t *pi_newchroma )
1699 XvAdaptorInfo *p_adaptor;
1701 int i_adaptor, i_num_adaptors, i_requested_adaptor;
1702 int i_selected_port;
1704 switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
1709 case XvBadExtension:
1710 msg_Warn( p_vout, "XvBadExtension" );
1714 msg_Warn( p_vout, "XvBadAlloc" );
1718 msg_Warn( p_vout, "XvQueryExtension failed" );
1722 switch( XvQueryAdaptors( p_vout->p_sys->p_display,
1723 DefaultRootWindow( p_vout->p_sys->p_display ),
1724 &i_num_adaptors, &p_adaptor ) )
1729 case XvBadExtension:
1730 msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
1734 msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
1738 msg_Warn( p_vout, "XvQueryAdaptors failed" );
1742 i_selected_port = -1;
1743 i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
1745 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
1747 XvImageFormatValues *p_formats;
1748 int i_format, i_num_formats;
1751 /* If we requested an adaptor and it's not this one, we aren't
1753 if( i_requested_adaptor != -1 && i_adaptor != i_requested_adaptor )
1758 /* If the adaptor doesn't have the required properties, skip it */
1759 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
1760 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
1765 /* Check that adaptor supports our requested format... */
1766 p_formats = XvListImageFormats( p_vout->p_sys->p_display,
1767 p_adaptor[i_adaptor].base_id,
1771 i_format < i_num_formats && ( i_selected_port == -1 );
1774 XvAttribute *p_attr;
1775 int i_attr, i_num_attributes;
1777 /* If this is not the format we want, or at least a
1778 * similar one, forget it */
1779 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
1784 /* Look for the first available port supporting this format */
1785 for( i_port = p_adaptor[i_adaptor].base_id;
1786 ( i_port < (int)(p_adaptor[i_adaptor].base_id
1787 + p_adaptor[i_adaptor].num_ports) )
1788 && ( i_selected_port == -1 );
1791 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
1794 i_selected_port = i_port;
1795 *pi_newchroma = p_formats[ i_format ].id;
1799 /* If no free port was found, forget it */
1800 if( i_selected_port == -1 )
1805 /* If we found a port, print information about it */
1806 msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
1807 i_adaptor, i_selected_port, p_formats[ i_format ].id,
1808 (char *)&p_formats[ i_format ].id,
1809 ( p_formats[ i_format ].format == XvPacked ) ?
1810 "packed" : "planar" );
1812 /* Make sure XV_AUTOPAINT_COLORKEY is set */
1813 p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
1815 &i_num_attributes );
1817 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
1819 if( !strcmp( p_attr[i_attr].name, "XV_AUTOPAINT_COLORKEY" ) )
1821 const Atom autopaint =
1822 XInternAtom( p_vout->p_sys->p_display,
1823 "XV_AUTOPAINT_COLORKEY", False );
1824 XvSetPortAttribute( p_vout->p_sys->p_display,
1825 i_selected_port, autopaint, 1 );
1830 if( p_attr != NULL )
1836 if( p_formats != NULL )
1843 if( i_num_adaptors > 0 )
1845 XvFreeAdaptorInfo( p_adaptor );
1848 if( i_selected_port == -1 )
1850 int i_chroma_tmp = X112VLC_FOURCC( i_chroma );
1851 if( i_requested_adaptor == -1 )
1853 msg_Warn( p_vout, "no free XVideo port found for format "
1854 "0x%.8x (%4.4s)", i_chroma_tmp, (char*)&i_chroma_tmp );
1858 msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
1859 "XVideo port for format 0x%.8x (%4.4s)",
1860 i_requested_adaptor, i_chroma_tmp, (char*)&i_chroma_tmp );
1864 return i_selected_port;
1867 /*****************************************************************************
1868 * XVideoReleasePort: release YUV12 port
1869 *****************************************************************************/
1870 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
1872 XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
1876 /*****************************************************************************
1877 * InitDisplay: open and initialize X11 device
1878 *****************************************************************************
1879 * Create a window according to video output given size, and set other
1880 * properties according to the display properties.
1881 *****************************************************************************/
1882 static int InitDisplay( vout_thread_t *p_vout )
1884 #ifdef MODULE_NAME_IS_x11
1885 XPixmapFormatValues * p_formats; /* pixmap formats */
1886 XVisualInfo * p_xvisual; /* visuals information */
1887 XVisualInfo xvisual_template; /* visual template */
1888 int i_count; /* array size */
1891 #ifdef HAVE_SYS_SHM_H
1892 p_vout->p_sys->b_shm = 0;
1894 if( config_GetInt( p_vout, MODULE_STRING "-shm" ) )
1897 /* FIXME: As of 2001-03-16, XFree4 for MacOS X does not support Xshm */
1899 p_vout->p_sys->b_shm =
1900 ( XShmQueryExtension( p_vout->p_sys->p_display ) == True );
1903 if( !p_vout->p_sys->b_shm )
1905 msg_Warn( p_vout, "XShm video extension is unavailable" );
1910 msg_Dbg( p_vout, "disabling XShm video extension" );
1914 msg_Warn( p_vout, "XShm video extension is unavailable" );
1918 #ifdef MODULE_NAME_IS_xvideo
1919 /* XXX The brightness and contrast values should be read from environment
1920 * XXX variables... */
1922 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
1923 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
1927 #ifdef MODULE_NAME_IS_x11
1928 /* Initialize structure */
1929 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
1931 /* Get screen depth */
1932 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
1933 p_vout->p_sys->i_screen );
1934 switch( p_vout->p_sys->i_screen_depth )
1938 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
1940 xvisual_template.screen = p_vout->p_sys->i_screen;
1941 xvisual_template.class = DirectColor;
1942 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1943 VisualScreenMask | VisualClassMask,
1944 &xvisual_template, &i_count );
1945 if( p_xvisual == NULL )
1947 msg_Err( p_vout, "no PseudoColor visual available" );
1948 return VLC_EGENERIC;
1950 p_vout->p_sys->i_bytes_per_pixel = 1;
1951 p_vout->output.pf_setpalette = SetPalette;
1958 * Screen depth is higher than 8bpp. TrueColor visual is used.
1960 xvisual_template.screen = p_vout->p_sys->i_screen;
1961 xvisual_template.class = TrueColor;
1962 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1963 VisualScreenMask | VisualClassMask,
1964 &xvisual_template, &i_count );
1965 if( p_xvisual == NULL )
1967 msg_Err( p_vout, "no TrueColor visual available" );
1968 return VLC_EGENERIC;
1971 p_vout->output.i_rmask = p_xvisual->red_mask;
1972 p_vout->output.i_gmask = p_xvisual->green_mask;
1973 p_vout->output.i_bmask = p_xvisual->blue_mask;
1975 /* There is no difference yet between 3 and 4 Bpp. The only way
1976 * to find the actual number of bytes per pixel is to list supported
1977 * pixmap formats. */
1978 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
1979 p_vout->p_sys->i_bytes_per_pixel = 0;
1981 for( ; i_count-- ; p_formats++ )
1983 /* Under XFree4.0, the list contains pixmap formats available
1984 * through all video depths ; so we have to check against current
1986 if( p_formats->depth == (int)p_vout->p_sys->i_screen_depth )
1988 if( p_formats->bits_per_pixel / 8
1989 > (int)p_vout->p_sys->i_bytes_per_pixel )
1991 p_vout->p_sys->i_bytes_per_pixel =
1992 p_formats->bits_per_pixel / 8;
1998 p_vout->p_sys->p_visual = p_xvisual->visual;
2005 #ifdef HAVE_SYS_SHM_H
2006 /*****************************************************************************
2007 * CreateShmImage: create an XImage or XvImage using shared memory extension
2008 *****************************************************************************
2009 * Prepare an XImage or XvImage for display function.
2010 * The order of the operations respects the recommandations of the mit-shm
2011 * document by J.Corbet and K.Packard. Most of the parameters were copied from
2012 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
2013 *****************************************************************************/
2014 static IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
2015 Display* p_display, EXTRA_ARGS_SHM,
2016 int i_width, int i_height )
2018 IMAGE_TYPE *p_image;
2021 /* Create XImage / XvImage */
2022 #ifdef MODULE_NAME_IS_xvideo
2023 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2024 i_width, i_height, p_shm );
2026 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2027 p_shm, i_width, i_height );
2029 if( p_image == NULL )
2031 msg_Err( p_vout, "image creation failed" );
2035 /* Allocate shared memory segment - 0776 set the access permission
2036 * rights (like umask), they are not yet supported by all X servers */
2037 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
2038 if( p_shm->shmid < 0 )
2040 msg_Err( p_vout, "cannot allocate shared image data (%s)",
2041 strerror( errno ) );
2042 IMAGE_FREE( p_image );
2046 /* Attach shared memory segment to process (read/write) */
2047 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2048 if(! p_shm->shmaddr )
2050 msg_Err( p_vout, "cannot attach shared memory (%s)",
2052 IMAGE_FREE( p_image );
2053 shmctl( p_shm->shmid, IPC_RMID, 0 );
2057 /* Read-only data. We won't be using XShmGetImage */
2058 p_shm->readOnly = True;
2060 /* Attach shared memory segment to X server */
2061 XSynchronize( p_display, True );
2063 result = XShmAttach( p_display, p_shm );
2064 if( result == False || !b_shm )
2066 msg_Err( p_vout, "cannot attach shared memory to X server" );
2067 IMAGE_FREE( p_image );
2068 shmctl( p_shm->shmid, IPC_RMID, 0 );
2069 shmdt( p_shm->shmaddr );
2072 XSynchronize( p_display, False );
2074 /* Send image to X server. This instruction is required, since having
2075 * built a Shm XImage and not using it causes an error on XCloseDisplay,
2076 * and remember NOT to use XFlush ! */
2077 XSync( p_display, False );
2080 /* Mark the shm segment to be removed when there are no more
2081 * attachements, so it is automatic on process exit or after shmdt */
2082 shmctl( p_shm->shmid, IPC_RMID, 0 );
2089 /*****************************************************************************
2090 * CreateImage: create an XImage or XvImage
2091 *****************************************************************************
2092 * Create a simple image used as a buffer.
2093 *****************************************************************************/
2094 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2095 Display *p_display, EXTRA_ARGS,
2096 int i_width, int i_height )
2098 byte_t * p_data; /* image data storage zone */
2099 IMAGE_TYPE *p_image;
2100 #ifdef MODULE_NAME_IS_x11
2101 int i_quantum; /* XImage quantum (see below) */
2102 int i_bytes_per_line;
2105 /* Allocate memory for image */
2106 #ifdef MODULE_NAME_IS_xvideo
2107 p_data = (byte_t *) malloc( i_width * i_height * i_bits_per_pixel / 8 );
2108 #elif defined(MODULE_NAME_IS_x11)
2109 i_bytes_per_line = i_width * i_bytes_per_pixel;
2110 p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
2114 msg_Err( p_vout, "out of memory" );
2118 #ifdef MODULE_NAME_IS_x11
2119 /* Optimize the quantum of a scanline regarding its size - the quantum is
2120 a diviser of the number of bits between the start of two scanlines. */
2121 if( i_bytes_per_line & 0xf )
2125 else if( i_bytes_per_line & 0x10 )
2135 /* Create XImage. p_data will be automatically freed */
2136 #ifdef MODULE_NAME_IS_xvideo
2137 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2138 p_data, i_width, i_height );
2139 #elif defined(MODULE_NAME_IS_x11)
2140 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2141 p_data, i_width, i_height, i_quantum, 0 );
2143 if( p_image == NULL )
2145 msg_Err( p_vout, "XCreateImage() failed" );
2153 /*****************************************************************************
2154 * X11ErrorHandler: replace error handler so we can intercept some of them
2155 *****************************************************************************/
2156 static int X11ErrorHandler( Display * display, XErrorEvent * event )
2158 /* Ingnore errors on XSetInputFocus()
2159 * (they happen when a window is not yet mapped) */
2160 if( event->request_code == X_SetInputFocus )
2162 fprintf(stderr, "XSetInputFocus failed\n");
2166 if( event->request_code == 150 /* MIT-SHM */ &&
2167 event->minor_code == X_ShmAttach )
2169 fprintf(stderr, "XShmAttach failed\n");
2174 XSetErrorHandler(NULL);
2175 return (XSetErrorHandler(X11ErrorHandler))( display, event );
2178 #ifdef MODULE_NAME_IS_x11
2179 /*****************************************************************************
2180 * SetPalette: sets an 8 bpp palette
2181 *****************************************************************************
2182 * This function sets the palette given as an argument. It does not return
2183 * anything, but could later send information on which colors it was unable
2185 *****************************************************************************/
2186 static void SetPalette( vout_thread_t *p_vout,
2187 uint16_t *red, uint16_t *green, uint16_t *blue )
2190 XColor p_colors[255];
2192 /* allocate palette */
2193 for( i = 0; i < 255; i++ )
2195 /* kludge: colors are indexed reversely because color 255 seems
2196 * to be reserved for black even if we try to set it to white */
2197 p_colors[ i ].pixel = 255 - i;
2198 p_colors[ i ].pad = 0;
2199 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
2200 p_colors[ i ].red = red[ 255 - i ];
2201 p_colors[ i ].blue = blue[ 255 - i ];
2202 p_colors[ i ].green = green[ 255 - i ];
2205 XStoreColors( p_vout->p_sys->p_display,
2206 p_vout->p_sys->colormap, p_colors, 255 );
2210 /*****************************************************************************
2211 * Control: control facility for the vout
2212 *****************************************************************************/
2213 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
2221 if( p_vout->p_sys->p_win->owner_window )
2222 return vout_ControlWindow( p_vout,
2223 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
2225 f_arg = va_arg( args, double );
2227 vlc_mutex_lock( &p_vout->p_sys->lock );
2229 /* Update dimensions */
2230 /* FIXME: export InitWindowSize() from vout core */
2231 XResizeWindow( p_vout->p_sys->p_display,
2232 p_vout->p_sys->p_win->base_window,
2233 p_vout->i_window_width * f_arg,
2234 p_vout->i_window_height * f_arg );
2236 vlc_mutex_unlock( &p_vout->p_sys->lock );
2240 vlc_mutex_lock( &p_vout->p_sys->lock );
2241 XUnmapWindow( p_vout->p_sys->p_display,
2242 p_vout->p_sys->original_window.base_window );
2243 vlc_mutex_unlock( &p_vout->p_sys->lock );
2247 vlc_mutex_lock( &p_vout->p_sys->lock );
2248 XReparentWindow( p_vout->p_sys->p_display,
2249 p_vout->p_sys->original_window.base_window,
2250 DefaultRootWindow( p_vout->p_sys->p_display ),
2252 XSync( p_vout->p_sys->p_display, False );
2253 p_vout->p_sys->original_window.owner_window = 0;
2254 vlc_mutex_unlock( &p_vout->p_sys->lock );
2255 return vout_vaControlDefault( p_vout, i_query, args );
2257 case VOUT_SET_STAY_ON_TOP:
2258 if( p_vout->p_sys->p_win->owner_window )
2259 return vout_ControlWindow( p_vout,
2260 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
2262 b_arg = va_arg( args, vlc_bool_t );
2263 vlc_mutex_lock( &p_vout->p_sys->lock );
2264 WindowOnTop( p_vout, b_arg );
2265 vlc_mutex_unlock( &p_vout->p_sys->lock );
2269 return vout_vaControlDefault( p_vout, i_query, args );
2273 /*****************************************************************************
2274 * TestNetWMSupport: tests for Extended Window Manager Hints support
2275 *****************************************************************************/
2276 static void TestNetWMSupport( vout_thread_t *p_vout )
2278 int i_ret, i_format;
2279 unsigned long i, i_items, i_bytesafter;
2280 Atom net_wm_supported;
2281 union { Atom *p_atom; unsigned char *p_char; } p_args;
2283 p_args.p_atom = NULL;
2285 p_vout->p_sys->b_net_wm_state_fullscreen = VLC_FALSE;
2286 p_vout->p_sys->b_net_wm_state_above = VLC_FALSE;
2287 p_vout->p_sys->b_net_wm_state_below = VLC_FALSE;
2288 p_vout->p_sys->b_net_wm_state_stays_on_top = VLC_FALSE;
2291 XInternAtom( p_vout->p_sys->p_display, "_NET_SUPPORTED", False );
2293 i_ret = XGetWindowProperty( p_vout->p_sys->p_display,
2294 DefaultRootWindow( p_vout->p_sys->p_display ),
2296 0, 16384, False, AnyPropertyType,
2298 &i_format, &i_items, &i_bytesafter,
2299 (unsigned char **)&p_args );
2301 if( i_ret != Success || i_items == 0 ) return;
2303 msg_Dbg( p_vout, "Window manager supports NetWM" );
2305 p_vout->p_sys->net_wm_state =
2306 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE", False );
2307 p_vout->p_sys->net_wm_state_fullscreen =
2308 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_FULLSCREEN",
2310 p_vout->p_sys->net_wm_state_above =
2311 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_ABOVE", False );
2312 p_vout->p_sys->net_wm_state_below =
2313 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_BELOW", False );
2314 p_vout->p_sys->net_wm_state_stays_on_top =
2315 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_STAYS_ON_TOP",
2318 for( i = 0; i < i_items; i++ )
2320 if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_fullscreen )
2323 "Window manager supports _NET_WM_STATE_FULLSCREEN" );
2324 p_vout->p_sys->b_net_wm_state_fullscreen = VLC_TRUE;
2326 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_above )
2328 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_ABOVE" );
2329 p_vout->p_sys->b_net_wm_state_above = VLC_TRUE;
2331 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_below )
2333 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_BELOW" );
2334 p_vout->p_sys->b_net_wm_state_below = VLC_TRUE;
2336 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_stays_on_top )
2339 "Window manager supports _NET_WM_STATE_STAYS_ON_TOP" );
2340 p_vout->p_sys->b_net_wm_state_stays_on_top = VLC_TRUE;
2344 XFree( p_args.p_atom );
2347 /*****************************************************************************
2348 * Key events handling
2349 *****************************************************************************/
2355 } x11keys_to_vlckeys[] =
2357 { XK_F1, KEY_F1 }, { XK_F2, KEY_F2 }, { XK_F3, KEY_F3 }, { XK_F4, KEY_F4 },
2358 { XK_F5, KEY_F5 }, { XK_F6, KEY_F6 }, { XK_F7, KEY_F7 }, { XK_F8, KEY_F8 },
2359 { XK_F9, KEY_F9 }, { XK_F10, KEY_F10 }, { XK_F11, KEY_F11 },
2360 { XK_F12, KEY_F12 },
2362 { XK_Return, KEY_ENTER },
2363 { XK_KP_Enter, KEY_ENTER },
2364 { XK_space, KEY_SPACE },
2365 { XK_Escape, KEY_ESC },
2367 { XK_Menu, KEY_MENU },
2368 { XK_Left, KEY_LEFT },
2369 { XK_Right, KEY_RIGHT },
2371 { XK_Down, KEY_DOWN },
2373 { XK_Home, KEY_HOME },
2374 { XK_End, KEY_END },
2375 { XK_Page_Up, KEY_PAGEUP },
2376 { XK_Page_Down, KEY_PAGEDOWN },
2378 { XK_Insert, KEY_INSERT },
2379 { XK_Delete, KEY_DELETE },
2384 static int ConvertKey( int i_key )
2388 for( i = 0; x11keys_to_vlckeys[i].i_x11key != 0; i++ )
2390 if( x11keys_to_vlckeys[i].i_x11key == i_key )
2392 return x11keys_to_vlckeys[i].i_vlckey;
2399 /*****************************************************************************
2400 * WindowOnTop: Switches the "always on top" state of the video window.
2401 *****************************************************************************/
2402 static int WindowOnTop( vout_thread_t *p_vout, vlc_bool_t b_on_top )
2404 if( p_vout->p_sys->b_net_wm_state_stays_on_top )
2406 XClientMessageEvent event;
2408 memset( &event, 0, sizeof( XClientMessageEvent ) );
2410 event.type = ClientMessage;
2411 event.message_type = p_vout->p_sys->net_wm_state;
2412 event.display = p_vout->p_sys->p_display;
2413 event.window = p_vout->p_sys->p_win->base_window;
2415 event.data.l[ 0 ] = b_on_top; /* set property */
2416 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_stays_on_top;
2418 XSendEvent( p_vout->p_sys->p_display,
2419 DefaultRootWindow( p_vout->p_sys->p_display ),
2420 False, SubstructureRedirectMask,