1 /*****************************************************************************
2 * xcommon.c: Functions common to the X11 and XVideo plugins
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN
5 * $Id: xcommon.c,v 1.18 2003/05/25 20:16:26 gbazin Exp $
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * David Kennedy <dkennedy@tinytoad.com>
10 * Gildas Bazin <gbazin@netcourrier.com>
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() */
38 #ifdef HAVE_MACHINE_PARAM_H
40 # include <machine/param.h>
41 # include <sys/types.h> /* typedef ushort */
46 # include <netinet/in.h> /* BSD: struct in_addr */
50 # include <sys/shm.h> /* shmget(), shmctl() */
55 #include <X11/Xutil.h>
56 #include <X11/keysym.h>
58 # include <X11/extensions/XShm.h>
60 #ifdef DPMSINFO_IN_DPMS_H
61 # include <X11/extensions/dpms.h>
64 #ifdef MODULE_NAME_IS_xvideo
65 # include <X11/extensions/Xv.h>
66 # include <X11/extensions/Xvlib.h>
70 # include <X11/extensions/Xinerama.h>
75 /*****************************************************************************
77 *****************************************************************************/
78 int E_(Activate) ( vlc_object_t * );
79 void E_(Deactivate) ( vlc_object_t * );
81 static int InitVideo ( vout_thread_t * );
82 static void EndVideo ( vout_thread_t * );
83 static void DisplayVideo ( vout_thread_t *, picture_t * );
84 static int ManageVideo ( vout_thread_t * );
86 static int InitDisplay ( vout_thread_t * );
88 static int CreateWindow ( vout_thread_t *, x11_window_t * );
89 static void DestroyWindow ( vout_thread_t *, x11_window_t * );
91 static int NewPicture ( vout_thread_t *, picture_t * );
92 static void FreePicture ( vout_thread_t *, picture_t * );
94 static IMAGE_TYPE *CreateImage ( vout_thread_t *,
95 Display *, EXTRA_ARGS, int, int );
97 static IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
98 Display *, EXTRA_ARGS_SHM, int, int );
101 static void ToggleFullScreen ( vout_thread_t * );
103 static void EnableXScreenSaver ( vout_thread_t * );
104 static void DisableXScreenSaver ( vout_thread_t * );
106 static void CreateCursor ( vout_thread_t * );
107 static void DestroyCursor ( vout_thread_t * );
108 static void ToggleCursor ( vout_thread_t * );
110 #ifdef MODULE_NAME_IS_xvideo
111 static int XVideoGetPort ( vout_thread_t *, vlc_fourcc_t, vlc_fourcc_t * );
112 static void XVideoReleasePort( vout_thread_t *, int );
115 #ifdef MODULE_NAME_IS_x11
116 static void SetPalette ( vout_thread_t *,
117 uint16_t *, uint16_t *, uint16_t * );
120 /*****************************************************************************
121 * Activate: allocate X11 video thread output method
122 *****************************************************************************
123 * This function allocate and initialize a X11 vout method. It uses some of the
124 * vout properties to choose the window size, and change them according to the
125 * actual properties of the display.
126 *****************************************************************************/
127 int E_(Activate) ( vlc_object_t *p_this )
129 vout_thread_t *p_vout = (vout_thread_t *)p_this;
131 #ifdef MODULE_NAME_IS_xvideo
133 vlc_fourcc_t i_chroma = 0;
134 vlc_bool_t b_chroma = 0;
137 p_vout->pf_init = InitVideo;
138 p_vout->pf_end = EndVideo;
139 p_vout->pf_manage = ManageVideo;
140 p_vout->pf_render = NULL;
141 p_vout->pf_display = DisplayVideo;
143 /* Allocate structure */
144 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
145 if( p_vout->p_sys == NULL )
147 msg_Err( p_vout, "out of memory" );
151 /* Open display, using the "display" config variable or the DISPLAY
152 * environment variable */
153 psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
155 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
157 if( p_vout->p_sys->p_display == NULL ) /* error */
159 msg_Err( p_vout, "cannot open display %s",
160 XDisplayName( psz_display ) );
161 free( p_vout->p_sys );
162 if( psz_display ) free( psz_display );
165 if( psz_display ) free( psz_display );
167 /* Get a screen ID matching the XOpenDisplay return value */
168 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
170 #ifdef MODULE_NAME_IS_xvideo
171 psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
174 if( strlen( psz_chroma ) >= 4 )
176 i_chroma = (unsigned char)psz_chroma[0] << 0;
177 i_chroma |= (unsigned char)psz_chroma[1] << 8;
178 i_chroma |= (unsigned char)psz_chroma[2] << 16;
179 i_chroma |= (unsigned char)psz_chroma[3] << 24;
189 msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
190 i_chroma, (char*)&i_chroma );
194 i_chroma = p_vout->render.i_chroma;
197 /* Check that we have access to an XVideo port providing this chroma */
198 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, i_chroma,
199 &p_vout->output.i_chroma );
200 if( p_vout->p_sys->i_xvport < 0 )
202 /* If a specific chroma format was requested, then we don't try to
203 * be cleverer than the user. He knew pretty well what he wanted. */
206 XCloseDisplay( p_vout->p_sys->p_display );
207 free( p_vout->p_sys );
211 /* It failed, but it's not completely lost ! We try to open an
212 * XVideo port for an YUY2 picture. We'll need to do an YUV
213 * conversion, but at least it has got scaling. */
214 p_vout->p_sys->i_xvport =
215 XVideoGetPort( p_vout, VLC_FOURCC('Y','U','Y','2'),
216 &p_vout->output.i_chroma );
217 if( p_vout->p_sys->i_xvport < 0 )
219 /* It failed, but it's not completely lost ! We try to open an
220 * XVideo port for a simple 16bpp RGB picture. We'll need to do
221 * an YUV conversion, but at least it has got scaling. */
222 p_vout->p_sys->i_xvport =
223 XVideoGetPort( p_vout, VLC_FOURCC('R','V','1','6'),
224 &p_vout->output.i_chroma );
225 if( p_vout->p_sys->i_xvport < 0 )
227 XCloseDisplay( p_vout->p_sys->p_display );
228 free( p_vout->p_sys );
235 /* Create blank cursor (for mouse cursor autohiding) */
236 p_vout->p_sys->i_time_mouse_last_moved = mdate();
237 p_vout->p_sys->b_mouse_pointer_visible = 1;
238 CreateCursor( p_vout );
240 /* Set main window's size */
241 p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
242 p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
244 /* Spawn base window - this window will include the video output window,
245 * but also command buttons, subtitles and other indicators */
246 if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
248 msg_Err( p_vout, "cannot create X11 window" );
249 DestroyCursor( p_vout );
250 XCloseDisplay( p_vout->p_sys->p_display );
251 free( p_vout->p_sys );
255 /* Open and initialize device. */
256 if( InitDisplay( p_vout ) )
258 msg_Err( p_vout, "cannot initialize X11 display" );
259 DestroyCursor( p_vout );
260 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
261 XCloseDisplay( p_vout->p_sys->p_display );
262 free( p_vout->p_sys );
266 /* Disable screen saver */
267 DisableXScreenSaver( p_vout );
270 p_vout->p_sys->b_altfullscreen = 0;
271 p_vout->p_sys->i_time_button_last_pressed = 0;
276 /*****************************************************************************
277 * Deactivate: destroy X11 video thread output method
278 *****************************************************************************
279 * Terminate an output method created by Open
280 *****************************************************************************/
281 void E_(Deactivate) ( vlc_object_t *p_this )
283 vout_thread_t *p_vout = (vout_thread_t *)p_this;
285 /* If the fullscreen window is still open, close it */
286 if( p_vout->b_fullscreen )
288 ToggleFullScreen( p_vout );
291 /* Restore cursor if it was blanked */
292 if( !p_vout->p_sys->b_mouse_pointer_visible )
294 ToggleCursor( p_vout );
297 #ifdef MODULE_NAME_IS_x11
298 /* Destroy colormap */
299 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
301 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
304 XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
307 DestroyCursor( p_vout );
308 EnableXScreenSaver( p_vout );
309 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
311 XCloseDisplay( p_vout->p_sys->p_display );
313 /* Destroy structure */
314 free( p_vout->p_sys );
317 /*****************************************************************************
318 * InitVideo: initialize X11 video thread output method
319 *****************************************************************************
320 * This function create the XImages needed by the output thread. It is called
321 * at the beginning of the thread, but also each time the window is resized.
322 *****************************************************************************/
323 static int InitVideo( vout_thread_t *p_vout )
328 I_OUTPUTPICTURES = 0;
330 #ifdef MODULE_NAME_IS_xvideo
331 /* Initialize the output structure; we already found an XVideo port,
332 * and the corresponding chroma we will be using. Since we can
333 * arbitrary scale, stick to the coordinates and aspect. */
334 p_vout->output.i_width = p_vout->render.i_width;
335 p_vout->output.i_height = p_vout->render.i_height;
336 p_vout->output.i_aspect = p_vout->render.i_aspect;
338 switch( p_vout->output.i_chroma )
340 case VLC_FOURCC('R','V','1','5'):
341 p_vout->output.i_rmask = 0x001f;
342 p_vout->output.i_gmask = 0x07e0;
343 p_vout->output.i_bmask = 0xf800;
345 case VLC_FOURCC('R','V','1','6'):
346 p_vout->output.i_rmask = 0x001f;
347 p_vout->output.i_gmask = 0x03e0;
348 p_vout->output.i_bmask = 0x7c00;
353 /* Initialize the output structure: RGB with square pixels, whatever
354 * the input format is, since it's the only format we know */
355 switch( p_vout->p_sys->i_screen_depth )
357 case 8: /* FIXME: set the palette */
358 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
360 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
362 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
364 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
366 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
368 msg_Err( p_vout, "unknown screen depth %i",
369 p_vout->p_sys->i_screen_depth );
373 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
374 p_vout->p_sys->p_win->i_height,
376 &p_vout->output.i_width, &p_vout->output.i_height );
378 /* Assume we have square pixels */
379 p_vout->output.i_aspect = p_vout->output.i_width
380 * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
383 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
384 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
388 /* Find an empty picture slot */
389 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
391 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
393 p_pic = p_vout->p_picture + i_index;
398 /* Allocate the picture */
399 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
404 p_pic->i_status = DESTROYED_PICTURE;
405 p_pic->i_type = DIRECT_PICTURE;
407 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
415 /*****************************************************************************
416 * DisplayVideo: displays previously rendered output
417 *****************************************************************************
418 * This function sends the currently rendered image to X11 server.
419 * (The Xv extension takes care of "double-buffering".)
420 *****************************************************************************/
421 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
423 int i_width, i_height, i_x, i_y;
425 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
426 p_vout->p_sys->p_win->i_height,
427 &i_x, &i_y, &i_width, &i_height );
429 #ifdef HAVE_SYS_SHM_H
430 if( p_vout->p_sys->b_shm )
432 /* Display rendered image using shared memory extension */
433 # ifdef MODULE_NAME_IS_xvideo
434 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
435 p_vout->p_sys->p_win->video_window,
436 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
437 0 /*src_x*/, 0 /*src_y*/,
438 p_vout->output.i_width, p_vout->output.i_height,
439 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
440 False /* Don't put True here or you'll waste your CPU */ );
442 XShmPutImage( p_vout->p_sys->p_display,
443 p_vout->p_sys->p_win->video_window,
444 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
445 0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
446 p_vout->output.i_width, p_vout->output.i_height,
447 False /* Don't put True here ! */ );
451 #endif /* HAVE_SYS_SHM_H */
453 /* Use standard XPutImage -- this is gonna be slow ! */
454 #ifdef MODULE_NAME_IS_xvideo
455 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
456 p_vout->p_sys->p_win->video_window,
457 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
458 0 /*src_x*/, 0 /*src_y*/,
459 p_vout->output.i_width, p_vout->output.i_height,
460 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
462 XPutImage( p_vout->p_sys->p_display,
463 p_vout->p_sys->p_win->video_window,
464 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
465 0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
466 p_vout->output.i_width, p_vout->output.i_height );
470 /* Make sure the command is sent now - do NOT use XFlush !*/
471 XSync( p_vout->p_sys->p_display, False );
474 /*****************************************************************************
475 * ManageVideo: handle X11 events
476 *****************************************************************************
477 * This function should be called regularly by video output thread. It manages
478 * X11 events and allows window resizing. It returns a non null value on
480 *****************************************************************************/
481 static int ManageVideo( vout_thread_t *p_vout )
483 XEvent xevent; /* X11 event */
484 char i_key; /* ISO Latin-1 key */
488 /* Handle X11 events: ConfigureNotify events are parsed to know if the
489 * output window's size changed, MapNotify and UnmapNotify to know if the
490 * window is mapped (and if the display is useful), and ClientMessages
491 * to intercept window destruction requests */
493 while( XCheckWindowEvent( p_vout->p_sys->p_display,
494 p_vout->p_sys->p_win->base_window,
495 StructureNotifyMask | KeyPressMask |
496 ButtonPressMask | ButtonReleaseMask |
497 PointerMotionMask | Button1MotionMask , &xevent )
500 /* ConfigureNotify event: prepare */
501 if( xevent.type == ConfigureNotify )
503 if( (unsigned int)xevent.xconfigure.width
504 != p_vout->p_sys->p_win->i_width
505 || (unsigned int)xevent.xconfigure.height
506 != p_vout->p_sys->p_win->i_height )
508 /* Update dimensions */
509 p_vout->i_changes |= VOUT_SIZE_CHANGE;
510 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
511 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
515 else if( xevent.type == KeyPress )
517 /* We may have keys like F1 trough F12, ESC ... */
518 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
519 xevent.xkey.keycode, 0 );
520 switch( (int)x_key_symbol )
523 if( p_vout->b_fullscreen )
525 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
529 p_vout->p_vlc->b_die = 1;
534 intf_thread_t *p_intf;
535 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
539 p_intf->b_menu_change = 1;
540 vlc_object_release( p_intf );
545 input_Seek( p_vout, -5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
548 input_Seek( p_vout, 5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
551 input_Seek( p_vout, 60, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
554 input_Seek( p_vout, -60, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
557 input_Seek( p_vout, 0, INPUT_SEEK_BYTES | INPUT_SEEK_SET );
560 input_Seek( p_vout, 0, INPUT_SEEK_BYTES | INPUT_SEEK_END );
563 input_Seek( p_vout, 900, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
566 input_Seek( p_vout, -900, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
569 input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
574 * The reason why I use this instead of XK_0 is that
575 * with XLookupString, we don't have to care about
578 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
580 /* FIXME: handle stuff here */
585 p_vout->p_vlc->b_die = 1;
589 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
600 else if( xevent.type == ButtonPress )
602 switch( ((XButtonEvent *)&xevent)->button )
605 var_Get( p_vout, "mouse-button-down", &val );
607 var_Set( p_vout, "mouse-button-down", val );
609 /* detect double-clicks */
610 if( ( ((XButtonEvent *)&xevent)->time -
611 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
613 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
616 p_vout->p_sys->i_time_button_last_pressed =
617 ((XButtonEvent *)&xevent)->time;
620 var_Get( p_vout, "mouse-button-down", &val );
622 var_Set( p_vout, "mouse-button-down", val );
626 var_Get( p_vout, "mouse-button-down", &val );
628 var_Set( p_vout, "mouse-button-down", val );
632 var_Get( p_vout, "mouse-button-down", &val );
634 var_Set( p_vout, "mouse-button-down", val );
635 input_Seek( p_vout, 15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
639 var_Get( p_vout, "mouse-button-down", &val );
641 var_Set( p_vout, "mouse-button-down", val );
642 input_Seek( p_vout, -15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
647 else if( xevent.type == ButtonRelease )
649 switch( ((XButtonEvent *)&xevent)->button )
652 var_Get( p_vout, "mouse-button-down", &val );
654 var_Set( p_vout, "mouse-button-down", val );
656 val.b_bool = VLC_TRUE;
657 var_Set( p_vout, "mouse-clicked", val );
661 var_Get( p_vout, "mouse-button-down", &val );
663 var_Set( p_vout, "mouse-button-down", val );
668 intf_thread_t *p_intf;
669 var_Get( p_vout, "mouse-button-down", &val );
671 var_Set( p_vout, "mouse-button-down", val );
672 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
676 p_intf->b_menu_change = 1;
677 vlc_object_release( p_intf );
683 var_Get( p_vout, "mouse-button-down", &val );
685 var_Set( p_vout, "mouse-button-down", val );
689 var_Get( p_vout, "mouse-button-down", &val );
691 var_Set( p_vout, "mouse-button-down", val );
697 else if( xevent.type == MotionNotify )
699 int i_width, i_height, i_x, i_y;
702 /* somewhat different use for vout_PlacePicture:
703 * here the values are needed to give to mouse coordinates
704 * in the original picture space */
705 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
706 p_vout->p_sys->p_win->i_height,
707 &i_x, &i_y, &i_width, &i_height );
709 val.i_int = ( xevent.xmotion.x - i_x )
710 * p_vout->render.i_width / i_width;
711 var_Set( p_vout, "mouse-x", val );
712 val.i_int = ( xevent.xmotion.y - i_y )
713 * p_vout->render.i_height / i_height;
714 var_Set( p_vout, "mouse-y", val );
716 val.b_bool = VLC_TRUE;
717 var_Set( p_vout, "mouse-moved", val );
719 p_vout->p_sys->i_time_mouse_last_moved = mdate();
720 if( ! p_vout->p_sys->b_mouse_pointer_visible )
722 ToggleCursor( p_vout );
725 else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
726 || xevent.type == MapNotify
727 || xevent.type == UnmapNotify )
729 /* Ignore these events */
731 else /* Other events */
733 msg_Warn( p_vout, "unhandled event %d received", xevent.type );
737 /* Handle events for video output sub-window */
738 while( XCheckWindowEvent( p_vout->p_sys->p_display,
739 p_vout->p_sys->p_win->video_window,
740 ExposureMask, &xevent ) == True )
742 /* Window exposed (only handled if stream playback is paused) */
743 if( xevent.type == Expose )
745 if( ((XExposeEvent *)&xevent)->count == 0 )
747 /* (if this is the last a collection of expose events...) */
749 if( p_vout->p_vlc->p_input_bank->pp_input[0] != NULL )
751 if( PAUSE_S == p_vout->p_vlc->p_input_bank->pp_input[0]
752 ->stream.control.i_status )
754 /* XVideoDisplay( p_vout )*/;
762 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
763 * are handled - according to the man pages, the format is always 32
765 while( XCheckTypedEvent( p_vout->p_sys->p_display,
766 ClientMessage, &xevent ) )
768 if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
769 && ((Atom)xevent.xclient.data.l[0]
770 == p_vout->p_sys->p_win->wm_delete_window ) )
772 p_vout->p_vlc->b_die = 1;
779 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
783 /* Update the object variable and trigger callback */
784 val.b_bool = !p_vout->b_fullscreen;
785 var_Set( p_vout, "fullscreen", val );
787 ToggleFullScreen( p_vout );
788 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
794 * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
795 * the size flag inside the fullscreen routine)
797 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
799 int i_width, i_height, i_x, i_y;
801 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
803 #ifdef MODULE_NAME_IS_x11
804 /* We need to signal the vout thread about the size change because it
805 * is doing the rescaling */
806 p_vout->i_changes |= VOUT_SIZE_CHANGE;
809 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
810 p_vout->p_sys->p_win->i_height,
811 &i_x, &i_y, &i_width, &i_height );
813 XResizeWindow( p_vout->p_sys->p_display,
814 p_vout->p_sys->p_win->video_window, i_width, i_height );
816 XMoveWindow( p_vout->p_sys->p_display,
817 p_vout->p_sys->p_win->video_window, i_x, i_y );
820 /* Autohide Cursour */
821 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
823 /* Hide the mouse automatically */
824 if( p_vout->p_sys->b_mouse_pointer_visible )
826 ToggleCursor( p_vout );
833 /*****************************************************************************
834 * EndVideo: terminate X11 video thread output method
835 *****************************************************************************
836 * Destroy the X11 XImages created by Init. It is called at the end of
837 * the thread, but also each time the window is resized.
838 *****************************************************************************/
839 static void EndVideo( vout_thread_t *p_vout )
843 /* Free the direct buffers we allocated */
844 for( i_index = I_OUTPUTPICTURES ; i_index ; )
847 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
851 /* following functions are local */
853 /*****************************************************************************
854 * CreateWindow: open and set-up X11 main window
855 *****************************************************************************/
856 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
858 XSizeHints xsize_hints;
859 XSetWindowAttributes xwindow_attributes;
864 vlc_bool_t b_configure_notify;
865 vlc_bool_t b_map_notify;
868 long long int i_drawable;
870 /* Prepare window manager hints and properties */
871 xsize_hints.base_width = p_win->i_width;
872 xsize_hints.base_height = p_win->i_height;
873 xsize_hints.flags = PSize;
874 p_win->wm_protocols =
875 XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
876 p_win->wm_delete_window =
877 XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
879 /* Prepare window attributes */
880 xwindow_attributes.backing_store = Always; /* save the hidden part */
881 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
882 p_vout->p_sys->i_screen);
883 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
885 /* Check whether someone provided us with a window ID */
886 var_Get( p_vout->p_vlc, "drawable", &val );
887 i_drawable = p_vout->b_fullscreen ? 0 : val.i_int;
891 p_win->b_owned = VLC_TRUE;
893 /* Create the window and set hints - the window must receive
894 * ConfigureNotify events, and until it is displayed, Expose and
895 * MapNotify events. */
898 XCreateWindow( p_vout->p_sys->p_display,
899 DefaultRootWindow( p_vout->p_sys->p_display ),
901 p_win->i_width, p_win->i_height,
904 CWBackingStore | CWBackPixel | CWEventMask,
905 &xwindow_attributes );
907 if( !p_vout->b_fullscreen )
909 /* Set window manager hints and properties: size hints, command,
910 * window's name, and accepted protocols */
911 XSetWMNormalHints( p_vout->p_sys->p_display,
912 p_win->base_window, &xsize_hints );
913 XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
914 p_vout->p_vlc->ppsz_argv, p_vout->p_vlc->i_argc );
916 XStoreName( p_vout->p_sys->p_display, p_win->base_window,
917 #ifdef MODULE_NAME_IS_x11
918 VOUT_TITLE " (X11 output)"
920 VOUT_TITLE " (XVideo output)"
927 p_win->b_owned = VLC_FALSE;
928 p_win->base_window = i_drawable;
930 XChangeWindowAttributes( p_vout->p_sys->p_display,
932 CWBackingStore | CWBackPixel | CWEventMask,
933 &xwindow_attributes );
936 if( (p_win->wm_protocols == None) /* use WM_DELETE_WINDOW */
937 || (p_win->wm_delete_window == None)
938 || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
939 &p_win->wm_delete_window, 1 ) )
941 /* WM_DELETE_WINDOW is not supported by window manager */
942 msg_Warn( p_vout, "missing or bad window manager" );
945 /* Creation of a graphic context that doesn't generate a GraphicsExpose
946 * event when using functions like XCopyArea */
947 xgcvalues.graphics_exposures = False;
948 p_win->gc = XCreateGC( p_vout->p_sys->p_display,
950 GCGraphicsExposures, &xgcvalues );
954 /* Send orders to server, and wait until window is displayed - three
955 * events must be received: a MapNotify event, an Expose event allowing
956 * drawing in the window, and a ConfigureNotify to get the window
957 * dimensions. Once those events have been received, only
958 * ConfigureNotify events need to be received. */
960 b_configure_notify = 0;
962 XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
965 XNextEvent( p_vout->p_sys->p_display, &xevent);
966 if( (xevent.type == Expose)
967 && (xevent.xexpose.window == p_win->base_window) )
971 else if( (xevent.type == MapNotify)
972 && (xevent.xmap.window == p_win->base_window) )
976 else if( (xevent.type == ConfigureNotify)
977 && (xevent.xconfigure.window == p_win->base_window) )
979 b_configure_notify = 1;
980 p_win->i_width = xevent.xconfigure.width;
981 p_win->i_height = xevent.xconfigure.height;
983 } while( !( b_expose && b_configure_notify && b_map_notify ) );
987 /* Get the window's geometry information */
989 unsigned int dummy2, dummy3;
990 XGetGeometry( p_vout->p_sys->p_display, p_win->base_window,
991 &dummy1, &dummy2, &dummy3,
997 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
998 StructureNotifyMask | KeyPressMask |
999 ButtonPressMask | ButtonReleaseMask |
1000 PointerMotionMask );
1002 #ifdef MODULE_NAME_IS_x11
1003 if( p_win->b_owned &&
1004 XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1006 /* Allocate a new palette */
1007 p_vout->p_sys->colormap =
1008 XCreateColormap( p_vout->p_sys->p_display,
1009 DefaultRootWindow( p_vout->p_sys->p_display ),
1010 DefaultVisual( p_vout->p_sys->p_display,
1011 p_vout->p_sys->i_screen ),
1014 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1015 XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1016 CWColormap, &xwindow_attributes );
1020 /* Create video output sub-window. */
1021 p_win->video_window = XCreateSimpleWindow(
1022 p_vout->p_sys->p_display,
1023 p_win->base_window, 0, 0,
1024 p_win->i_width, p_win->i_height,
1026 BlackPixel( p_vout->p_sys->p_display,
1027 p_vout->p_sys->i_screen ),
1028 WhitePixel( p_vout->p_sys->p_display,
1029 p_vout->p_sys->i_screen ) );
1031 XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1032 BlackPixel( p_vout->p_sys->p_display,
1033 p_vout->p_sys->i_screen ) );
1035 XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1036 XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1039 /* make sure the video window will be centered in the next ManageVideo() */
1040 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1042 /* If the cursor was formerly blank than blank it again */
1043 if( !p_vout->p_sys->b_mouse_pointer_visible )
1045 ToggleCursor( p_vout );
1046 ToggleCursor( p_vout );
1049 /* Do NOT use XFlush here ! */
1050 XSync( p_vout->p_sys->p_display, False );
1052 /* At this stage, the window is open, displayed, and ready to
1054 p_vout->p_sys->p_win = p_win;
1059 /*****************************************************************************
1060 * DestroyWindow: destroy the window
1061 *****************************************************************************
1063 *****************************************************************************/
1064 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1066 /* Do NOT use XFlush here ! */
1067 XSync( p_vout->p_sys->p_display, False );
1069 XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1070 XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1072 if( p_win->b_owned )
1074 XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1075 XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1079 /*****************************************************************************
1080 * NewPicture: allocate a picture
1081 *****************************************************************************
1082 * Returns 0 on success, -1 otherwise
1083 *****************************************************************************/
1084 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1086 /* We know the chroma, allocate a buffer which will be used
1087 * directly by the decoder */
1088 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1090 if( p_pic->p_sys == NULL )
1095 #ifdef HAVE_SYS_SHM_H
1096 if( p_vout->p_sys->b_shm )
1098 /* Create image using XShm extension */
1099 p_pic->p_sys->p_image =
1100 CreateShmImage( p_vout, p_vout->p_sys->p_display,
1101 # ifdef MODULE_NAME_IS_xvideo
1102 p_vout->p_sys->i_xvport, p_vout->output.i_chroma,
1104 p_vout->p_sys->p_visual,
1105 p_vout->p_sys->i_screen_depth,
1107 &p_pic->p_sys->shminfo,
1108 p_vout->output.i_width, p_vout->output.i_height );
1111 #endif /* HAVE_SYS_SHM_H */
1113 /* Create image without XShm extension */
1114 p_pic->p_sys->p_image =
1115 CreateImage( p_vout, p_vout->p_sys->p_display,
1116 #ifdef MODULE_NAME_IS_xvideo
1117 p_vout->p_sys->i_xvport, p_vout->output.i_chroma,
1119 p_vout->p_sys->p_visual,
1120 p_vout->p_sys->i_screen_depth,
1121 p_vout->p_sys->i_bytes_per_pixel,
1123 p_vout->output.i_width, p_vout->output.i_height );
1126 if( p_pic->p_sys->p_image == NULL )
1128 free( p_pic->p_sys );
1132 switch( p_vout->output.i_chroma )
1134 #ifdef MODULE_NAME_IS_xvideo
1135 case VLC_FOURCC('I','4','2','0'):
1137 p_pic->Y_PIXELS = p_pic->p_sys->p_image->data
1138 + p_pic->p_sys->p_image->offsets[0];
1139 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1140 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[0];
1141 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1142 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width;
1144 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1145 + p_pic->p_sys->p_image->offsets[1];
1146 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1147 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[1];
1148 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1149 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
1151 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1152 + p_pic->p_sys->p_image->offsets[2];
1153 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1154 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[2];
1155 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1156 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
1158 p_pic->i_planes = 3;
1161 case VLC_FOURCC('Y','V','1','2'):
1163 p_pic->Y_PIXELS = p_pic->p_sys->p_image->data
1164 + p_pic->p_sys->p_image->offsets[0];
1165 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1166 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[0];
1167 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1168 p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p[Y_PLANE].i_pitch;
1169 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width;
1171 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1172 + p_pic->p_sys->p_image->offsets[2];
1173 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1174 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[2];
1175 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1176 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
1178 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1179 + p_pic->p_sys->p_image->offsets[1];
1180 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1181 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[1];
1182 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1183 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
1185 p_pic->i_planes = 3;
1188 case VLC_FOURCC('Y','2','1','1'):
1190 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1191 + p_pic->p_sys->p_image->offsets[0];
1192 p_pic->p->i_lines = p_vout->output.i_height;
1193 /* XXX: this just looks so plain wrong... check it out ! */
1194 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0] / 4;
1195 p_pic->p->i_pixel_pitch = 4;
1196 p_pic->p->i_visible_pitch = p_vout->output.i_width * 4;
1198 p_pic->i_planes = 1;
1201 case VLC_FOURCC('Y','U','Y','2'):
1202 case VLC_FOURCC('U','Y','V','Y'):
1204 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1205 + p_pic->p_sys->p_image->offsets[0];
1206 p_pic->p->i_lines = p_vout->output.i_height;
1207 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1208 p_pic->p->i_pixel_pitch = 4;
1209 p_pic->p->i_visible_pitch = p_vout->output.i_width * 4;
1211 p_pic->i_planes = 1;
1214 case VLC_FOURCC('R','V','1','5'):
1216 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1217 + p_pic->p_sys->p_image->offsets[0];
1218 p_pic->p->i_lines = p_vout->output.i_height;
1219 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1220 p_pic->p->i_pixel_pitch = 2;
1221 p_pic->p->i_visible_pitch = p_vout->output.i_width * 2;
1223 p_pic->i_planes = 1;
1226 case VLC_FOURCC('R','V','1','6'):
1228 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1229 + p_pic->p_sys->p_image->offsets[0];
1230 p_pic->p->i_lines = p_vout->output.i_height;
1231 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1232 p_pic->p->i_pixel_pitch = 2;
1233 p_pic->p->i_visible_pitch = p_vout->output.i_width * 2;
1235 p_pic->i_planes = 1;
1239 case VLC_FOURCC('R','G','B','2'):
1241 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1242 + p_pic->p_sys->p_image->xoffset;
1243 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1244 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1245 p_pic->p->i_pixel_pitch = p_pic->p_sys->p_image->depth;
1246 p_pic->p->i_visible_pitch = p_pic->p_sys->p_image->width;
1248 p_pic->i_planes = 1;
1252 case VLC_FOURCC('R','V','1','6'):
1253 case VLC_FOURCC('R','V','1','5'):
1255 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1256 + p_pic->p_sys->p_image->xoffset;
1257 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1258 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1259 p_pic->p->i_pixel_pitch = p_pic->p_sys->p_image->depth;
1260 p_pic->p->i_visible_pitch = 2 * p_pic->p_sys->p_image->width;
1262 p_pic->i_planes = 1;
1266 case VLC_FOURCC('R','V','3','2'):
1267 case VLC_FOURCC('R','V','2','4'):
1269 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1270 + p_pic->p_sys->p_image->xoffset;
1271 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1272 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1273 p_pic->p->i_pixel_pitch = p_pic->p_sys->p_image->depth;
1274 p_pic->p->i_visible_pitch = 4 * p_pic->p_sys->p_image->width;
1276 p_pic->i_planes = 1;
1282 /* Unknown chroma, tell the guy to get lost */
1283 IMAGE_FREE( p_pic->p_sys->p_image );
1284 free( p_pic->p_sys );
1285 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1286 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1287 p_pic->i_planes = 0;
1294 /*****************************************************************************
1295 * FreePicture: destroy a picture allocated with NewPicture
1296 *****************************************************************************
1297 * Destroy XImage AND associated data. If using Shm, detach shared memory
1298 * segment from server and process, then free it. The XDestroyImage manpage
1299 * says that both the image structure _and_ the data pointed to by the
1300 * image structure are freed, so no need to free p_image->data.
1301 *****************************************************************************/
1302 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1304 /* The order of operations is correct */
1305 #ifdef HAVE_SYS_SHM_H
1306 if( p_vout->p_sys->b_shm )
1308 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
1309 IMAGE_FREE( p_pic->p_sys->p_image );
1311 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
1312 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
1314 msg_Err( p_vout, "cannot detach shared memory (%s)",
1321 IMAGE_FREE( p_pic->p_sys->p_image );
1324 /* Do NOT use XFlush here ! */
1325 XSync( p_vout->p_sys->p_display, False );
1327 free( p_pic->p_sys );
1330 /*****************************************************************************
1331 * ToggleFullScreen: Enable or disable full screen mode
1332 *****************************************************************************
1333 * This function will switch between fullscreen and window mode.
1334 *****************************************************************************/
1335 static void ToggleFullScreen ( vout_thread_t *p_vout )
1339 mwmhints_t mwmhints;
1340 XSetWindowAttributes attributes;
1342 #ifdef HAVE_XINERAMA
1346 p_vout->b_fullscreen = !p_vout->b_fullscreen;
1348 if( p_vout->b_fullscreen )
1350 msg_Dbg( p_vout, "entering fullscreen mode" );
1352 p_vout->p_sys->b_altfullscreen =
1353 config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
1355 if( p_vout->p_sys->p_win->b_owned )
1357 XUnmapWindow( p_vout->p_sys->p_display,
1358 p_vout->p_sys->p_win->base_window);
1361 p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
1363 /* fullscreen window size and position */
1364 p_vout->p_sys->p_win->i_width =
1365 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1366 p_vout->p_sys->p_win->i_height =
1367 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1369 CreateWindow( p_vout, p_vout->p_sys->p_win );
1371 /* To my knowledge there are two ways to create a borderless window.
1372 * There's the generic way which is to tell x to bypass the window
1373 * manager, but this creates problems with the focus of other
1375 * The other way is to use the motif property "_MOTIF_WM_HINTS" which
1376 * luckily seems to be supported by most window managers. */
1377 if( !p_vout->p_sys->b_altfullscreen )
1379 mwmhints.flags = MWM_HINTS_DECORATIONS;
1380 mwmhints.decorations = False;
1382 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1384 XChangeProperty( p_vout->p_sys->p_display,
1385 p_vout->p_sys->p_win->base_window,
1386 prop, prop, 32, PropModeReplace,
1387 (unsigned char *)&mwmhints,
1388 PROP_MWM_HINTS_ELEMENTS );
1392 /* brute force way to remove decorations */
1393 attributes.override_redirect = True;
1394 XChangeWindowAttributes( p_vout->p_sys->p_display,
1395 p_vout->p_sys->p_win->base_window,
1400 /* Make sure the change is effective */
1401 XReparentWindow( p_vout->p_sys->p_display,
1402 p_vout->p_sys->p_win->base_window,
1403 DefaultRootWindow( p_vout->p_sys->p_display ),
1406 /* fullscreen window size and position */
1407 p_vout->p_sys->p_win->i_width =
1408 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1409 p_vout->p_sys->p_win->i_height =
1410 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1412 p_vout->p_sys->p_win->i_x = 0;
1413 p_vout->p_sys->p_win->i_y = 0;
1415 #ifdef HAVE_XINERAMA
1416 if( XineramaQueryExtension( p_vout->p_sys->p_display, &i_d1, &i_d2 ) &&
1417 XineramaIsActive( p_vout->p_sys->p_display ) )
1419 XineramaScreenInfo *screens; /* infos for xinerama */
1422 msg_Dbg( p_vout, "Using XFree Xinerama extension");
1424 #define SCREEN p_vout->p_sys->p_win->i_screen
1426 /* Get Informations about Xinerama (num of screens) */
1427 screens = XineramaQueryScreens( p_vout->p_sys->p_display,
1431 SCREEN = config_GetInt( p_vout,
1432 MODULE_STRING "-xineramascreen" );
1434 /* just check that user has entered a good value */
1435 if( SCREEN >= i_num_screens || SCREEN < 0 )
1437 msg_Dbg( p_vout, "requested screen number invalid" );
1441 /* Get the X/Y upper left corner coordinate of the above screen */
1442 p_vout->p_sys->p_win->i_x = screens[SCREEN].x_org;
1443 p_vout->p_sys->p_win->i_y = screens[SCREEN].y_org;
1445 /* Set the Height/width to the screen resolution */
1446 p_vout->p_sys->p_win->i_width = screens[SCREEN].width;
1447 p_vout->p_sys->p_win->i_height = screens[SCREEN].height;
1456 XMoveResizeWindow( p_vout->p_sys->p_display,
1457 p_vout->p_sys->p_win->base_window,
1458 p_vout->p_sys->p_win->i_x,
1459 p_vout->p_sys->p_win->i_y,
1460 p_vout->p_sys->p_win->i_width,
1461 p_vout->p_sys->p_win->i_height );
1465 msg_Dbg( p_vout, "leaving fullscreen mode" );
1466 DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
1467 p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
1469 if( p_vout->p_sys->p_win->b_owned )
1471 XMapWindow( p_vout->p_sys->p_display,
1472 p_vout->p_sys->p_win->base_window);
1476 /* Unfortunately, using XSync() here is not enough to ensure the
1477 * window has already been mapped because the XMapWindow() request
1478 * has not necessarily been sent directly to our window (remember,
1479 * the call is first redirected to the window manager) */
1480 if( p_vout->p_sys->p_win->b_owned )
1484 XWindowEvent( p_vout->p_sys->p_display,
1485 p_vout->p_sys->p_win->base_window,
1486 StructureNotifyMask, &xevent );
1487 } while( xevent.type != MapNotify );
1491 XSync( p_vout->p_sys->p_display, False );
1494 /* Becareful, this can generate a BadMatch error if the window is not
1495 * already mapped by the server (see above) */
1496 XSetInputFocus(p_vout->p_sys->p_display,
1497 p_vout->p_sys->p_win->base_window,
1501 /* signal that the size needs to be updated */
1502 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1505 /*****************************************************************************
1506 * EnableXScreenSaver: enable screen saver
1507 *****************************************************************************
1508 * This function enables the screen saver on a display after it has been
1509 * disabled by XDisableScreenSaver.
1510 * FIXME: what happens if multiple vlc sessions are running at the same
1512 *****************************************************************************/
1513 static void EnableXScreenSaver( vout_thread_t *p_vout )
1515 #ifdef DPMSINFO_IN_DPMS_H
1519 if( p_vout->p_sys->i_ss_timeout )
1521 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1522 p_vout->p_sys->i_ss_interval,
1523 p_vout->p_sys->i_ss_blanking,
1524 p_vout->p_sys->i_ss_exposure );
1527 /* Restore DPMS settings */
1528 #ifdef DPMSINFO_IN_DPMS_H
1529 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1531 if( p_vout->p_sys->b_ss_dpms )
1533 DPMSEnable( p_vout->p_sys->p_display );
1539 /*****************************************************************************
1540 * DisableXScreenSaver: disable screen saver
1541 *****************************************************************************
1542 * See XEnableXScreenSaver
1543 *****************************************************************************/
1544 static void DisableXScreenSaver( vout_thread_t *p_vout )
1546 #ifdef DPMSINFO_IN_DPMS_H
1550 /* Save screen saver informations */
1551 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1552 &p_vout->p_sys->i_ss_interval,
1553 &p_vout->p_sys->i_ss_blanking,
1554 &p_vout->p_sys->i_ss_exposure );
1556 /* Disable screen saver */
1557 if( p_vout->p_sys->i_ss_timeout )
1559 XSetScreenSaver( p_vout->p_sys->p_display, 0,
1560 p_vout->p_sys->i_ss_interval,
1561 p_vout->p_sys->i_ss_blanking,
1562 p_vout->p_sys->i_ss_exposure );
1566 #ifdef DPMSINFO_IN_DPMS_H
1567 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1570 /* Save DPMS current state */
1571 DPMSInfo( p_vout->p_sys->p_display, &unused,
1572 &p_vout->p_sys->b_ss_dpms );
1573 DPMSDisable( p_vout->p_sys->p_display );
1578 /*****************************************************************************
1579 * CreateCursor: create a blank mouse pointer
1580 *****************************************************************************/
1581 static void CreateCursor( vout_thread_t *p_vout )
1583 XColor cursor_color;
1585 p_vout->p_sys->cursor_pixmap =
1586 XCreatePixmap( p_vout->p_sys->p_display,
1587 DefaultRootWindow( p_vout->p_sys->p_display ),
1590 XParseColor( p_vout->p_sys->p_display,
1591 XCreateColormap( p_vout->p_sys->p_display,
1593 p_vout->p_sys->p_display ),
1595 p_vout->p_sys->p_display,
1596 p_vout->p_sys->i_screen ),
1598 "black", &cursor_color );
1600 p_vout->p_sys->blank_cursor =
1601 XCreatePixmapCursor( p_vout->p_sys->p_display,
1602 p_vout->p_sys->cursor_pixmap,
1603 p_vout->p_sys->cursor_pixmap,
1604 &cursor_color, &cursor_color, 1, 1 );
1607 /*****************************************************************************
1608 * DestroyCursor: destroy the blank mouse pointer
1609 *****************************************************************************/
1610 static void DestroyCursor( vout_thread_t *p_vout )
1612 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
1615 /*****************************************************************************
1616 * ToggleCursor: hide or show the mouse pointer
1617 *****************************************************************************
1618 * This function hides the X pointer if it is visible by setting the pointer
1619 * sprite to a blank one. To show it again, we disable the sprite.
1620 *****************************************************************************/
1621 static void ToggleCursor( vout_thread_t *p_vout )
1623 if( p_vout->p_sys->b_mouse_pointer_visible )
1625 XDefineCursor( p_vout->p_sys->p_display,
1626 p_vout->p_sys->p_win->base_window,
1627 p_vout->p_sys->blank_cursor );
1628 p_vout->p_sys->b_mouse_pointer_visible = 0;
1632 XUndefineCursor( p_vout->p_sys->p_display,
1633 p_vout->p_sys->p_win->base_window );
1634 p_vout->p_sys->b_mouse_pointer_visible = 1;
1638 #ifdef MODULE_NAME_IS_xvideo
1639 /*****************************************************************************
1640 * XVideoGetPort: get YUV12 port
1641 *****************************************************************************/
1642 static int XVideoGetPort( vout_thread_t *p_vout,
1643 vlc_fourcc_t i_chroma, vlc_fourcc_t *pi_newchroma )
1645 XvAdaptorInfo *p_adaptor;
1647 int i_adaptor, i_num_adaptors, i_requested_adaptor;
1648 int i_selected_port;
1650 switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
1655 case XvBadExtension:
1656 msg_Warn( p_vout, "XvBadExtension" );
1660 msg_Warn( p_vout, "XvBadAlloc" );
1664 msg_Warn( p_vout, "XvQueryExtension failed" );
1668 switch( XvQueryAdaptors( p_vout->p_sys->p_display,
1669 DefaultRootWindow( p_vout->p_sys->p_display ),
1670 &i_num_adaptors, &p_adaptor ) )
1675 case XvBadExtension:
1676 msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
1680 msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
1684 msg_Warn( p_vout, "XvQueryAdaptors failed" );
1688 i_selected_port = -1;
1689 i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
1691 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
1693 XvImageFormatValues *p_formats;
1694 int i_format, i_num_formats;
1697 /* If we requested an adaptor and it's not this one, we aren't
1699 if( i_requested_adaptor != -1 && i_adaptor != i_requested_adaptor )
1704 /* If the adaptor doesn't have the required properties, skip it */
1705 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
1706 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
1711 /* Check that adaptor supports our requested format... */
1712 p_formats = XvListImageFormats( p_vout->p_sys->p_display,
1713 p_adaptor[i_adaptor].base_id,
1717 i_format < i_num_formats && ( i_selected_port == -1 );
1720 /* Code removed, we can get this through xvinfo anyway */
1722 XvEncodingInfo *p_enc;
1723 int i_enc, i_num_encodings;
1724 XvAttribute *p_attr;
1725 int i_attr, i_num_attributes;
1728 /* If this is not the format we want, or at least a
1729 * similar one, forget it */
1730 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
1735 /* Look for the first available port supporting this format */
1736 for( i_port = p_adaptor[i_adaptor].base_id;
1737 ( i_port < (int)(p_adaptor[i_adaptor].base_id
1738 + p_adaptor[i_adaptor].num_ports) )
1739 && ( i_selected_port == -1 );
1742 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
1745 i_selected_port = i_port;
1746 *pi_newchroma = p_formats[ i_format ].id;
1750 /* If no free port was found, forget it */
1751 if( i_selected_port == -1 )
1756 /* If we found a port, print information about it */
1757 msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
1758 i_adaptor, i_selected_port, p_formats[ i_format ].id,
1759 (char *)&p_formats[ i_format ].id,
1760 ( p_formats[ i_format ].format == XvPacked ) ?
1761 "packed" : "planar" );
1764 msg_Dbg( p_vout, " encoding list:" );
1766 if( XvQueryEncodings( p_vout->p_sys->p_display, i_selected_port,
1767 &i_num_encodings, &p_enc )
1770 msg_Dbg( p_vout, " XvQueryEncodings failed" );
1774 for( i_enc = 0; i_enc < i_num_encodings; i_enc++ )
1776 msg_Dbg( p_vout, " id=%ld, name=%s, size=%ldx%ld,"
1777 " numerator=%d, denominator=%d",
1778 p_enc[i_enc].encoding_id, p_enc[i_enc].name,
1779 p_enc[i_enc].width, p_enc[i_enc].height,
1780 p_enc[i_enc].rate.numerator,
1781 p_enc[i_enc].rate.denominator );
1786 XvFreeEncodingInfo( p_enc );
1789 msg_Dbg( p_vout, " attribute list:" );
1790 p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
1792 &i_num_attributes );
1793 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
1795 msg_Dbg( p_vout, " name=%s, flags=[%s%s ], min=%i, max=%i",
1796 p_attr[i_attr].name,
1797 (p_attr[i_attr].flags & XvGettable) ? " get" : "",
1798 (p_attr[i_attr].flags & XvSettable) ? " set" : "",
1799 p_attr[i_attr].min_value, p_attr[i_attr].max_value );
1802 if( p_attr != NULL )
1809 if( p_formats != NULL )
1816 if( i_num_adaptors > 0 )
1818 XvFreeAdaptorInfo( p_adaptor );
1821 if( i_selected_port == -1 )
1823 if( i_requested_adaptor == -1 )
1825 msg_Warn( p_vout, "no free XVideo port found for format "
1826 "0x%.8x (%4.4s)", i_chroma, (char*)&i_chroma );
1830 msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
1831 "XVideo port for format 0x%.8x (%4.4s)",
1832 i_requested_adaptor, i_chroma, (char*)&i_chroma );
1836 return i_selected_port;
1839 /*****************************************************************************
1840 * XVideoReleasePort: release YUV12 port
1841 *****************************************************************************/
1842 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
1844 XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
1848 /*****************************************************************************
1849 * InitDisplay: open and initialize X11 device
1850 *****************************************************************************
1851 * Create a window according to video output given size, and set other
1852 * properties according to the display properties.
1853 *****************************************************************************/
1854 static int InitDisplay( vout_thread_t *p_vout )
1856 #ifdef MODULE_NAME_IS_x11
1857 XPixmapFormatValues * p_formats; /* pixmap formats */
1858 XVisualInfo * p_xvisual; /* visuals informations */
1859 XVisualInfo xvisual_template; /* visual template */
1860 int i_count; /* array size */
1863 #ifdef HAVE_SYS_SHM_H
1864 p_vout->p_sys->b_shm = 0;
1866 if( config_GetInt( p_vout, MODULE_STRING "-shm" ) )
1869 /* FIXME: As of 2001-03-16, XFree4 for MacOS X does not support Xshm */
1871 p_vout->p_sys->b_shm =
1872 ( XShmQueryExtension( p_vout->p_sys->p_display ) == True );
1875 if( !p_vout->p_sys->b_shm )
1877 msg_Warn( p_vout, "XShm video extension is unavailable" );
1882 msg_Dbg( p_vout, "disabling XShm video extension" );
1886 msg_Warn( p_vout, "XShm video extension is unavailable" );
1890 #ifdef MODULE_NAME_IS_xvideo
1891 /* XXX The brightness and contrast values should be read from environment
1892 * XXX variables... */
1894 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
1895 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
1899 #ifdef MODULE_NAME_IS_x11
1900 /* Initialize structure */
1901 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
1903 /* Get screen depth */
1904 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
1905 p_vout->p_sys->i_screen );
1906 switch( p_vout->p_sys->i_screen_depth )
1910 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
1912 xvisual_template.screen = p_vout->p_sys->i_screen;
1913 xvisual_template.class = DirectColor;
1914 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1915 VisualScreenMask | VisualClassMask,
1916 &xvisual_template, &i_count );
1917 if( p_xvisual == NULL )
1919 msg_Err( p_vout, "no PseudoColor visual available" );
1920 return VLC_EGENERIC;
1922 p_vout->p_sys->i_bytes_per_pixel = 1;
1923 p_vout->output.pf_setpalette = SetPalette;
1930 * Screen depth is higher than 8bpp. TrueColor visual is used.
1932 xvisual_template.screen = p_vout->p_sys->i_screen;
1933 xvisual_template.class = TrueColor;
1934 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1935 VisualScreenMask | VisualClassMask,
1936 &xvisual_template, &i_count );
1937 if( p_xvisual == NULL )
1939 msg_Err( p_vout, "no TrueColor visual available" );
1940 return VLC_EGENERIC;
1943 p_vout->output.i_rmask = p_xvisual->red_mask;
1944 p_vout->output.i_gmask = p_xvisual->green_mask;
1945 p_vout->output.i_bmask = p_xvisual->blue_mask;
1947 /* There is no difference yet between 3 and 4 Bpp. The only way
1948 * to find the actual number of bytes per pixel is to list supported
1949 * pixmap formats. */
1950 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
1951 p_vout->p_sys->i_bytes_per_pixel = 0;
1953 for( ; i_count-- ; p_formats++ )
1955 /* Under XFree4.0, the list contains pixmap formats available
1956 * through all video depths ; so we have to check against current
1958 if( p_formats->depth == (int)p_vout->p_sys->i_screen_depth )
1960 if( p_formats->bits_per_pixel / 8
1961 > (int)p_vout->p_sys->i_bytes_per_pixel )
1963 p_vout->p_sys->i_bytes_per_pixel =
1964 p_formats->bits_per_pixel / 8;
1970 p_vout->p_sys->p_visual = p_xvisual->visual;
1977 #ifdef HAVE_SYS_SHM_H
1978 /*****************************************************************************
1979 * CreateShmImage: create an XImage or XvImage using shared memory extension
1980 *****************************************************************************
1981 * Prepare an XImage or XvImage for display function.
1982 * The order of the operations respects the recommandations of the mit-shm
1983 * document by J.Corbet and K.Packard. Most of the parameters were copied from
1984 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
1985 *****************************************************************************/
1986 static IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
1987 Display* p_display, EXTRA_ARGS_SHM,
1988 int i_width, int i_height )
1990 IMAGE_TYPE *p_image;
1992 /* Create XImage / XvImage */
1993 #ifdef MODULE_NAME_IS_xvideo
1994 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
1995 i_width, i_height, p_shm );
1997 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
1998 p_shm, i_width, i_height );
2000 if( p_image == NULL )
2002 msg_Err( p_vout, "image creation failed" );
2006 /* Allocate shared memory segment - 0776 set the access permission
2007 * rights (like umask), they are not yet supported by all X servers */
2008 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
2009 if( p_shm->shmid < 0 )
2011 msg_Err( p_vout, "cannot allocate shared image data (%s)",
2012 strerror( errno ) );
2013 IMAGE_FREE( p_image );
2017 /* Attach shared memory segment to process (read/write) */
2018 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2019 if(! p_shm->shmaddr )
2021 msg_Err( p_vout, "cannot attach shared memory (%s)",
2023 IMAGE_FREE( p_image );
2024 shmctl( p_shm->shmid, IPC_RMID, 0 );
2028 /* Read-only data. We won't be using XShmGetImage */
2029 p_shm->readOnly = True;
2031 /* Attach shared memory segment to X server */
2032 if( XShmAttach( p_display, p_shm ) == False )
2034 msg_Err( p_vout, "cannot attach shared memory to X server" );
2035 IMAGE_FREE( p_image );
2036 shmctl( p_shm->shmid, IPC_RMID, 0 );
2037 shmdt( p_shm->shmaddr );
2041 /* Send image to X server. This instruction is required, since having
2042 * built a Shm XImage and not using it causes an error on XCloseDisplay,
2043 * and remember NOT to use XFlush ! */
2044 XSync( p_display, False );
2047 /* Mark the shm segment to be removed when there are no more
2048 * attachements, so it is automatic on process exit or after shmdt */
2049 shmctl( p_shm->shmid, IPC_RMID, 0 );
2056 /*****************************************************************************
2057 * CreateImage: create an XImage or XvImage
2058 *****************************************************************************
2059 * Create a simple image used as a buffer.
2060 *****************************************************************************/
2061 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2062 Display *p_display, EXTRA_ARGS,
2063 int i_width, int i_height )
2065 byte_t * p_data; /* image data storage zone */
2066 IMAGE_TYPE *p_image;
2067 #ifdef MODULE_NAME_IS_x11
2068 int i_quantum; /* XImage quantum (see below) */
2069 int i_bytes_per_line;
2072 /* Allocate memory for image */
2073 #ifdef MODULE_NAME_IS_xvideo
2074 p_data = (byte_t *) malloc( i_width * i_height * 2 ); /* XXX */
2076 i_bytes_per_line = i_width * i_bytes_per_pixel;
2077 p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
2081 msg_Err( p_vout, "out of memory" );
2085 #ifdef MODULE_NAME_IS_x11
2086 /* Optimize the quantum of a scanline regarding its size - the quantum is
2087 a diviser of the number of bits between the start of two scanlines. */
2088 if( i_bytes_per_line & 0xf )
2092 else if( i_bytes_per_line & 0x10 )
2102 /* Create XImage. p_data will be automatically freed */
2103 #ifdef MODULE_NAME_IS_xvideo
2104 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2105 p_data, i_width, i_height );
2107 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2108 p_data, i_width, i_height, i_quantum, 0 );
2110 if( p_image == NULL )
2112 msg_Err( p_vout, "XCreateImage() failed" );
2120 #ifdef MODULE_NAME_IS_x11
2121 /*****************************************************************************
2122 * SetPalette: sets an 8 bpp palette
2123 *****************************************************************************
2124 * This function sets the palette given as an argument. It does not return
2125 * anything, but could later send information on which colors it was unable
2127 *****************************************************************************/
2128 static void SetPalette( vout_thread_t *p_vout,
2129 uint16_t *red, uint16_t *green, uint16_t *blue )
2132 XColor p_colors[255];
2134 /* allocate palette */
2135 for( i = 0; i < 255; i++ )
2137 /* kludge: colors are indexed reversely because color 255 seems
2138 * to be reserved for black even if we try to set it to white */
2139 p_colors[ i ].pixel = 255 - i;
2140 p_colors[ i ].pad = 0;
2141 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
2142 p_colors[ i ].red = red[ 255 - i ];
2143 p_colors[ i ].blue = blue[ 255 - i ];
2144 p_colors[ i ].green = green[ 255 - i ];
2147 XStoreColors( p_vout->p_sys->p_display,
2148 p_vout->p_sys->colormap, p_colors, 255 );