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.15 2003/05/05 16:09:38 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>
71 /*****************************************************************************
73 *****************************************************************************/
74 int E_(Activate) ( vlc_object_t * );
75 void E_(Deactivate) ( vlc_object_t * );
77 static int InitVideo ( vout_thread_t * );
78 static void EndVideo ( vout_thread_t * );
79 static void DisplayVideo ( vout_thread_t *, picture_t * );
80 static int ManageVideo ( vout_thread_t * );
82 static int InitDisplay ( vout_thread_t * );
84 static int CreateWindow ( vout_thread_t *, x11_window_t * );
85 static void DestroyWindow ( vout_thread_t *, x11_window_t * );
87 static int NewPicture ( vout_thread_t *, picture_t * );
88 static void FreePicture ( vout_thread_t *, picture_t * );
90 static IMAGE_TYPE *CreateImage ( vout_thread_t *,
91 Display *, EXTRA_ARGS, int, int );
93 static IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
94 Display *, EXTRA_ARGS_SHM, int, int );
97 static void ToggleFullScreen ( vout_thread_t * );
99 static void EnableXScreenSaver ( vout_thread_t * );
100 static void DisableXScreenSaver ( vout_thread_t * );
102 static void CreateCursor ( vout_thread_t * );
103 static void DestroyCursor ( vout_thread_t * );
104 static void ToggleCursor ( vout_thread_t * );
106 #ifdef MODULE_NAME_IS_xvideo
107 static int XVideoGetPort ( vout_thread_t *, vlc_fourcc_t, vlc_fourcc_t * );
108 static void XVideoReleasePort( vout_thread_t *, int );
111 #ifdef MODULE_NAME_IS_x11
112 static void SetPalette ( vout_thread_t *,
113 uint16_t *, uint16_t *, uint16_t * );
116 /*****************************************************************************
117 * Activate: allocate X11 video thread output method
118 *****************************************************************************
119 * This function allocate and initialize a X11 vout method. It uses some of the
120 * vout properties to choose the window size, and change them according to the
121 * actual properties of the display.
122 *****************************************************************************/
123 int E_(Activate) ( vlc_object_t *p_this )
125 vout_thread_t *p_vout = (vout_thread_t *)p_this;
127 #ifdef MODULE_NAME_IS_xvideo
129 vlc_fourcc_t i_chroma = 0;
130 vlc_bool_t b_chroma = 0;
133 p_vout->pf_init = InitVideo;
134 p_vout->pf_end = EndVideo;
135 p_vout->pf_manage = ManageVideo;
136 p_vout->pf_render = NULL;
137 p_vout->pf_display = DisplayVideo;
139 /* Allocate structure */
140 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
141 if( p_vout->p_sys == NULL )
143 msg_Err( p_vout, "out of memory" );
147 /* Open display, using the "display" config variable or the DISPLAY
148 * environment variable */
149 psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
151 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
153 if( p_vout->p_sys->p_display == NULL ) /* error */
155 msg_Err( p_vout, "cannot open display %s",
156 XDisplayName( psz_display ) );
157 free( p_vout->p_sys );
158 if( psz_display ) free( psz_display );
161 if( psz_display ) free( psz_display );
163 /* Get a screen ID matching the XOpenDisplay return value */
164 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
166 #ifdef MODULE_NAME_IS_xvideo
167 psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
170 if( strlen( psz_chroma ) >= 4 )
172 i_chroma = (unsigned char)psz_chroma[0] << 0;
173 i_chroma |= (unsigned char)psz_chroma[1] << 8;
174 i_chroma |= (unsigned char)psz_chroma[2] << 16;
175 i_chroma |= (unsigned char)psz_chroma[3] << 24;
185 msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
186 i_chroma, (char*)&i_chroma );
190 i_chroma = p_vout->render.i_chroma;
193 /* Check that we have access to an XVideo port providing this chroma */
194 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, i_chroma,
195 &p_vout->output.i_chroma );
196 if( p_vout->p_sys->i_xvport < 0 )
198 /* If a specific chroma format was requested, then we don't try to
199 * be cleverer than the user. He knew pretty well what he wanted. */
202 XCloseDisplay( p_vout->p_sys->p_display );
203 free( p_vout->p_sys );
207 /* It failed, but it's not completely lost ! We try to open an
208 * XVideo port for an YUY2 picture. We'll need to do an YUV
209 * conversion, but at least it has got scaling. */
210 p_vout->p_sys->i_xvport =
211 XVideoGetPort( p_vout, VLC_FOURCC('Y','U','Y','2'),
212 &p_vout->output.i_chroma );
213 if( p_vout->p_sys->i_xvport < 0 )
215 /* It failed, but it's not completely lost ! We try to open an
216 * XVideo port for a simple 16bpp RGB picture. We'll need to do
217 * an YUV conversion, but at least it has got scaling. */
218 p_vout->p_sys->i_xvport =
219 XVideoGetPort( p_vout, VLC_FOURCC('R','V','1','6'),
220 &p_vout->output.i_chroma );
221 if( p_vout->p_sys->i_xvport < 0 )
223 XCloseDisplay( p_vout->p_sys->p_display );
224 free( p_vout->p_sys );
231 /* Create blank cursor (for mouse cursor autohiding) */
232 p_vout->p_sys->i_time_mouse_last_moved = mdate();
233 p_vout->p_sys->b_mouse_pointer_visible = 1;
234 CreateCursor( p_vout );
236 /* Set main window's size */
237 p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
238 p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
240 /* Spawn base window - this window will include the video output window,
241 * but also command buttons, subtitles and other indicators */
242 if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
244 msg_Err( p_vout, "cannot create X11 window" );
245 DestroyCursor( p_vout );
246 XCloseDisplay( p_vout->p_sys->p_display );
247 free( p_vout->p_sys );
251 /* Open and initialize device. */
252 if( InitDisplay( p_vout ) )
254 msg_Err( p_vout, "cannot initialize X11 display" );
255 DestroyCursor( p_vout );
256 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
257 XCloseDisplay( p_vout->p_sys->p_display );
258 free( p_vout->p_sys );
262 /* Disable screen saver */
263 DisableXScreenSaver( p_vout );
266 p_vout->p_sys->b_altfullscreen = 0;
267 p_vout->p_sys->i_time_button_last_pressed = 0;
272 /*****************************************************************************
273 * Deactivate: destroy X11 video thread output method
274 *****************************************************************************
275 * Terminate an output method created by Open
276 *****************************************************************************/
277 void E_(Deactivate) ( vlc_object_t *p_this )
279 vout_thread_t *p_vout = (vout_thread_t *)p_this;
281 /* If the fullscreen window is still open, close it */
282 if( p_vout->b_fullscreen )
284 ToggleFullScreen( p_vout );
287 /* Restore cursor if it was blanked */
288 if( !p_vout->p_sys->b_mouse_pointer_visible )
290 ToggleCursor( p_vout );
293 #ifdef MODULE_NAME_IS_x11
294 /* Destroy colormap */
295 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
297 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
300 XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
303 DestroyCursor( p_vout );
304 EnableXScreenSaver( p_vout );
305 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
307 XCloseDisplay( p_vout->p_sys->p_display );
309 /* Destroy structure */
310 free( p_vout->p_sys );
313 /*****************************************************************************
314 * InitVideo: initialize X11 video thread output method
315 *****************************************************************************
316 * This function create the XImages needed by the output thread. It is called
317 * at the beginning of the thread, but also each time the window is resized.
318 *****************************************************************************/
319 static int InitVideo( vout_thread_t *p_vout )
324 I_OUTPUTPICTURES = 0;
326 #ifdef MODULE_NAME_IS_xvideo
327 /* Initialize the output structure; we already found an XVideo port,
328 * and the corresponding chroma we will be using. Since we can
329 * arbitrary scale, stick to the coordinates and aspect. */
330 p_vout->output.i_width = p_vout->render.i_width;
331 p_vout->output.i_height = p_vout->render.i_height;
332 p_vout->output.i_aspect = p_vout->render.i_aspect;
334 switch( p_vout->output.i_chroma )
336 case VLC_FOURCC('R','V','1','5'):
337 p_vout->output.i_rmask = 0x001f;
338 p_vout->output.i_gmask = 0x07e0;
339 p_vout->output.i_bmask = 0xf800;
341 case VLC_FOURCC('R','V','1','6'):
342 p_vout->output.i_rmask = 0x001f;
343 p_vout->output.i_gmask = 0x03e0;
344 p_vout->output.i_bmask = 0x7c00;
349 /* Initialize the output structure: RGB with square pixels, whatever
350 * the input format is, since it's the only format we know */
351 switch( p_vout->p_sys->i_screen_depth )
353 case 8: /* FIXME: set the palette */
354 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
356 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
358 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
360 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
362 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
364 msg_Err( p_vout, "unknown screen depth %i",
365 p_vout->p_sys->i_screen_depth );
369 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
370 p_vout->p_sys->p_win->i_height,
372 &p_vout->output.i_width, &p_vout->output.i_height );
374 /* Assume we have square pixels */
375 p_vout->output.i_aspect = p_vout->output.i_width
376 * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
379 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
380 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
384 /* Find an empty picture slot */
385 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
387 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
389 p_pic = p_vout->p_picture + i_index;
394 /* Allocate the picture */
395 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
400 p_pic->i_status = DESTROYED_PICTURE;
401 p_pic->i_type = DIRECT_PICTURE;
403 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
411 /*****************************************************************************
412 * DisplayVideo: displays previously rendered output
413 *****************************************************************************
414 * This function sends the currently rendered image to X11 server.
415 * (The Xv extension takes care of "double-buffering".)
416 *****************************************************************************/
417 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
419 int i_width, i_height, i_x, i_y;
421 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
422 p_vout->p_sys->p_win->i_height,
423 &i_x, &i_y, &i_width, &i_height );
425 #ifdef HAVE_SYS_SHM_H
426 if( p_vout->p_sys->b_shm )
428 /* Display rendered image using shared memory extension */
429 # ifdef MODULE_NAME_IS_xvideo
430 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
431 p_vout->p_sys->p_win->video_window,
432 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
433 0 /*src_x*/, 0 /*src_y*/,
434 p_vout->output.i_width, p_vout->output.i_height,
435 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
436 False /* Don't put True here or you'll waste your CPU */ );
438 XShmPutImage( p_vout->p_sys->p_display,
439 p_vout->p_sys->p_win->video_window,
440 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
441 0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
442 p_vout->output.i_width, p_vout->output.i_height,
443 False /* Don't put True here ! */ );
447 #endif /* HAVE_SYS_SHM_H */
449 /* Use standard XPutImage -- this is gonna be slow ! */
450 #ifdef MODULE_NAME_IS_xvideo
451 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
452 p_vout->p_sys->p_win->video_window,
453 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
454 0 /*src_x*/, 0 /*src_y*/,
455 p_vout->output.i_width, p_vout->output.i_height,
456 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
458 XPutImage( p_vout->p_sys->p_display,
459 p_vout->p_sys->p_win->video_window,
460 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
461 0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
462 p_vout->output.i_width, p_vout->output.i_height );
466 /* Make sure the command is sent now - do NOT use XFlush !*/
467 XSync( p_vout->p_sys->p_display, False );
470 /*****************************************************************************
471 * ManageVideo: handle X11 events
472 *****************************************************************************
473 * This function should be called regularly by video output thread. It manages
474 * X11 events and allows window resizing. It returns a non null value on
476 *****************************************************************************/
477 static int ManageVideo( vout_thread_t *p_vout )
479 XEvent xevent; /* X11 event */
480 char i_key; /* ISO Latin-1 key */
484 /* Handle X11 events: ConfigureNotify events are parsed to know if the
485 * output window's size changed, MapNotify and UnmapNotify to know if the
486 * window is mapped (and if the display is useful), and ClientMessages
487 * to intercept window destruction requests */
489 while( XCheckWindowEvent( p_vout->p_sys->p_display,
490 p_vout->p_sys->p_win->base_window,
491 StructureNotifyMask | KeyPressMask |
492 ButtonPressMask | ButtonReleaseMask |
493 PointerMotionMask | Button1MotionMask , &xevent )
496 /* ConfigureNotify event: prepare */
497 if( xevent.type == ConfigureNotify )
499 if( (unsigned int)xevent.xconfigure.width
500 != p_vout->p_sys->p_win->i_width
501 || (unsigned int)xevent.xconfigure.height
502 != p_vout->p_sys->p_win->i_height )
504 /* Update dimensions */
505 p_vout->i_changes |= VOUT_SIZE_CHANGE;
506 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
507 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
511 else if( xevent.type == KeyPress )
513 /* We may have keys like F1 trough F12, ESC ... */
514 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
515 xevent.xkey.keycode, 0 );
516 switch( (int)x_key_symbol )
519 if( p_vout->b_fullscreen )
521 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
525 p_vout->p_vlc->b_die = 1;
530 intf_thread_t *p_intf;
531 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
535 p_intf->b_menu_change = 1;
536 vlc_object_release( p_intf );
541 input_Seek( p_vout, -5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
544 input_Seek( p_vout, 5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
547 input_Seek( p_vout, 60, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
550 input_Seek( p_vout, -60, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
553 input_Seek( p_vout, 0, INPUT_SEEK_BYTES | INPUT_SEEK_SET );
556 input_Seek( p_vout, 0, INPUT_SEEK_BYTES | INPUT_SEEK_END );
559 input_Seek( p_vout, 900, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
562 input_Seek( p_vout, -900, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
565 input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
570 * The reason why I use this instead of XK_0 is that
571 * with XLookupString, we don't have to care about
574 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
576 /* FIXME: handle stuff here */
581 p_vout->p_vlc->b_die = 1;
585 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
596 else if( xevent.type == ButtonPress )
598 switch( ((XButtonEvent *)&xevent)->button )
601 var_Get( p_vout, "mouse-button-down", &val );
603 var_Set( p_vout, "mouse-button-down", val );
605 /* detect double-clicks */
606 if( ( ((XButtonEvent *)&xevent)->time -
607 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
609 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
612 p_vout->p_sys->i_time_button_last_pressed =
613 ((XButtonEvent *)&xevent)->time;
616 var_Get( p_vout, "mouse-button-down", &val );
618 var_Set( p_vout, "mouse-button-down", val );
622 var_Get( p_vout, "mouse-button-down", &val );
624 var_Set( p_vout, "mouse-button-down", val );
628 var_Get( p_vout, "mouse-button-down", &val );
630 var_Set( p_vout, "mouse-button-down", val );
631 input_Seek( p_vout, 15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
635 var_Get( p_vout, "mouse-button-down", &val );
637 var_Set( p_vout, "mouse-button-down", val );
638 input_Seek( p_vout, -15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
643 else if( xevent.type == ButtonRelease )
645 switch( ((XButtonEvent *)&xevent)->button )
648 var_Get( p_vout, "mouse-button-down", &val );
650 var_Set( p_vout, "mouse-button-down", val );
652 val.b_bool = VLC_TRUE;
653 var_Set( p_vout, "mouse-clicked", val );
657 var_Get( p_vout, "mouse-button-down", &val );
659 var_Set( p_vout, "mouse-button-down", val );
664 intf_thread_t *p_intf;
665 var_Get( p_vout, "mouse-button-down", &val );
667 var_Set( p_vout, "mouse-button-down", val );
668 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
672 p_intf->b_menu_change = 1;
673 vlc_object_release( p_intf );
679 var_Get( p_vout, "mouse-button-down", &val );
681 var_Set( p_vout, "mouse-button-down", val );
685 var_Get( p_vout, "mouse-button-down", &val );
687 var_Set( p_vout, "mouse-button-down", val );
693 else if( xevent.type == MotionNotify )
695 int i_width, i_height, i_x, i_y;
698 /* somewhat different use for vout_PlacePicture:
699 * here the values are needed to give to mouse coordinates
700 * in the original picture space */
701 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
702 p_vout->p_sys->p_win->i_height,
703 &i_x, &i_y, &i_width, &i_height );
705 val.i_int = ( xevent.xmotion.x - i_x )
706 * p_vout->render.i_width / i_width;
707 var_Set( p_vout, "mouse-x", val );
708 val.i_int = ( xevent.xmotion.y - i_y )
709 * p_vout->render.i_height / i_height;
710 var_Set( p_vout, "mouse-y", val );
712 val.b_bool = VLC_TRUE;
713 var_Set( p_vout, "mouse-moved", val );
715 p_vout->p_sys->i_time_mouse_last_moved = mdate();
716 if( ! p_vout->p_sys->b_mouse_pointer_visible )
718 ToggleCursor( p_vout );
721 else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
722 || xevent.type == MapNotify
723 || xevent.type == UnmapNotify )
725 /* Ignore these events */
727 else /* Other events */
729 msg_Warn( p_vout, "unhandled event %d received", xevent.type );
733 /* Handle events for video output sub-window */
734 while( XCheckWindowEvent( p_vout->p_sys->p_display,
735 p_vout->p_sys->p_win->video_window,
736 ExposureMask, &xevent ) == True )
738 /* Window exposed (only handled if stream playback is paused) */
739 if( xevent.type == Expose )
741 if( ((XExposeEvent *)&xevent)->count == 0 )
743 /* (if this is the last a collection of expose events...) */
745 if( p_vout->p_vlc->p_input_bank->pp_input[0] != NULL )
747 if( PAUSE_S == p_vout->p_vlc->p_input_bank->pp_input[0]
748 ->stream.control.i_status )
750 /* XVideoDisplay( p_vout )*/;
758 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
759 * are handled - according to the man pages, the format is always 32
761 while( XCheckTypedEvent( p_vout->p_sys->p_display,
762 ClientMessage, &xevent ) )
764 if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
765 && ((Atom)xevent.xclient.data.l[0]
766 == p_vout->p_sys->p_win->wm_delete_window ) )
768 p_vout->p_vlc->b_die = 1;
775 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
777 ToggleFullScreen( p_vout );
778 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
784 * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
785 * the size flag inside the fullscreen routine)
787 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
789 int i_width, i_height, i_x, i_y;
791 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
793 #ifdef MODULE_NAME_IS_x11
794 /* We need to signal the vout thread about the size change because it
795 * is doing the rescaling */
796 p_vout->i_changes |= VOUT_SIZE_CHANGE;
799 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
800 p_vout->p_sys->p_win->i_height,
801 &i_x, &i_y, &i_width, &i_height );
803 XResizeWindow( p_vout->p_sys->p_display,
804 p_vout->p_sys->p_win->video_window, i_width, i_height );
806 XMoveWindow( p_vout->p_sys->p_display,
807 p_vout->p_sys->p_win->video_window, i_x, i_y );
810 /* Autohide Cursour */
811 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
813 /* Hide the mouse automatically */
814 if( p_vout->p_sys->b_mouse_pointer_visible )
816 ToggleCursor( p_vout );
823 /*****************************************************************************
824 * EndVideo: terminate X11 video thread output method
825 *****************************************************************************
826 * Destroy the X11 XImages created by Init. It is called at the end of
827 * the thread, but also each time the window is resized.
828 *****************************************************************************/
829 static void EndVideo( vout_thread_t *p_vout )
833 /* Free the direct buffers we allocated */
834 for( i_index = I_OUTPUTPICTURES ; i_index ; )
837 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
841 /* following functions are local */
843 /*****************************************************************************
844 * CreateWindow: open and set-up X11 main window
845 *****************************************************************************/
846 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
848 XSizeHints xsize_hints;
849 XSetWindowAttributes xwindow_attributes;
854 vlc_bool_t b_configure_notify;
855 vlc_bool_t b_map_notify;
858 long long int i_drawable;
860 /* Prepare window manager hints and properties */
861 xsize_hints.base_width = p_win->i_width;
862 xsize_hints.base_height = p_win->i_height;
863 xsize_hints.flags = PSize;
864 p_win->wm_protocols =
865 XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
866 p_win->wm_delete_window =
867 XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
869 /* Prepare window attributes */
870 xwindow_attributes.backing_store = Always; /* save the hidden part */
871 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
872 p_vout->p_sys->i_screen);
873 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
875 /* Check whether someone provided us with a window ID */
876 var_Get( p_vout->p_vlc, "drawable", &val );
877 i_drawable = p_vout->b_fullscreen ? 0 : val.i_int;
881 p_win->b_owned = VLC_TRUE;
883 /* Create the window and set hints - the window must receive
884 * ConfigureNotify events, and until it is displayed, Expose and
885 * MapNotify events. */
888 XCreateWindow( p_vout->p_sys->p_display,
889 DefaultRootWindow( p_vout->p_sys->p_display ),
891 p_win->i_width, p_win->i_height,
894 CWBackingStore | CWBackPixel | CWEventMask,
895 &xwindow_attributes );
897 if( !p_vout->b_fullscreen )
899 /* Set window manager hints and properties: size hints, command,
900 * window's name, and accepted protocols */
901 XSetWMNormalHints( p_vout->p_sys->p_display,
902 p_win->base_window, &xsize_hints );
903 XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
904 p_vout->p_vlc->ppsz_argv, p_vout->p_vlc->i_argc );
906 XStoreName( p_vout->p_sys->p_display, p_win->base_window,
907 #ifdef MODULE_NAME_IS_x11
908 VOUT_TITLE " (X11 output)"
910 VOUT_TITLE " (XVideo output)"
917 p_win->b_owned = VLC_FALSE;
918 p_win->base_window = i_drawable;
920 XChangeWindowAttributes( p_vout->p_sys->p_display,
922 CWBackingStore | CWBackPixel | CWEventMask,
923 &xwindow_attributes );
926 if( (p_win->wm_protocols == None) /* use WM_DELETE_WINDOW */
927 || (p_win->wm_delete_window == None)
928 || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
929 &p_win->wm_delete_window, 1 ) )
931 /* WM_DELETE_WINDOW is not supported by window manager */
932 msg_Warn( p_vout, "missing or bad window manager" );
935 /* Creation of a graphic context that doesn't generate a GraphicsExpose
936 * event when using functions like XCopyArea */
937 xgcvalues.graphics_exposures = False;
938 p_win->gc = XCreateGC( p_vout->p_sys->p_display,
940 GCGraphicsExposures, &xgcvalues );
944 /* Send orders to server, and wait until window is displayed - three
945 * events must be received: a MapNotify event, an Expose event allowing
946 * drawing in the window, and a ConfigureNotify to get the window
947 * dimensions. Once those events have been received, only
948 * ConfigureNotify events need to be received. */
950 b_configure_notify = 0;
952 XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
955 XNextEvent( p_vout->p_sys->p_display, &xevent);
956 if( (xevent.type == Expose)
957 && (xevent.xexpose.window == p_win->base_window) )
961 else if( (xevent.type == MapNotify)
962 && (xevent.xmap.window == p_win->base_window) )
966 else if( (xevent.type == ConfigureNotify)
967 && (xevent.xconfigure.window == p_win->base_window) )
969 b_configure_notify = 1;
970 p_win->i_width = xevent.xconfigure.width;
971 p_win->i_height = xevent.xconfigure.height;
973 } while( !( b_expose && b_configure_notify && b_map_notify ) );
977 /* Get the window's geometry information */
979 unsigned int dummy2, dummy3;
980 XGetGeometry( p_vout->p_sys->p_display, p_win->base_window,
981 &dummy1, &dummy2, &dummy3,
987 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
988 StructureNotifyMask | KeyPressMask |
989 ButtonPressMask | ButtonReleaseMask |
992 #ifdef MODULE_NAME_IS_x11
993 if( p_win->b_owned &&
994 XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
996 /* Allocate a new palette */
997 p_vout->p_sys->colormap =
998 XCreateColormap( p_vout->p_sys->p_display,
999 DefaultRootWindow( p_vout->p_sys->p_display ),
1000 DefaultVisual( p_vout->p_sys->p_display,
1001 p_vout->p_sys->i_screen ),
1004 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1005 XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1006 CWColormap, &xwindow_attributes );
1010 /* Create video output sub-window. */
1011 p_win->video_window = XCreateSimpleWindow(
1012 p_vout->p_sys->p_display,
1013 p_win->base_window, 0, 0,
1014 p_win->i_width, p_win->i_height,
1016 BlackPixel( p_vout->p_sys->p_display,
1017 p_vout->p_sys->i_screen ),
1018 WhitePixel( p_vout->p_sys->p_display,
1019 p_vout->p_sys->i_screen ) );
1021 XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1022 BlackPixel( p_vout->p_sys->p_display,
1023 p_vout->p_sys->i_screen ) );
1025 XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1026 XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1029 /* make sure the video window will be centered in the next ManageVideo() */
1030 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1032 /* If the cursor was formerly blank than blank it again */
1033 if( !p_vout->p_sys->b_mouse_pointer_visible )
1035 ToggleCursor( p_vout );
1036 ToggleCursor( p_vout );
1039 /* Do NOT use XFlush here ! */
1040 XSync( p_vout->p_sys->p_display, False );
1042 /* At this stage, the window is open, displayed, and ready to
1044 p_vout->p_sys->p_win = p_win;
1049 /*****************************************************************************
1050 * DestroyWindow: destroy the window
1051 *****************************************************************************
1053 *****************************************************************************/
1054 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1056 /* Do NOT use XFlush here ! */
1057 XSync( p_vout->p_sys->p_display, False );
1059 XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1060 XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1062 if( p_win->b_owned )
1064 XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1065 XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1069 /*****************************************************************************
1070 * NewPicture: allocate a picture
1071 *****************************************************************************
1072 * Returns 0 on success, -1 otherwise
1073 *****************************************************************************/
1074 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1076 /* We know the chroma, allocate a buffer which will be used
1077 * directly by the decoder */
1078 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1080 if( p_pic->p_sys == NULL )
1085 #ifdef HAVE_SYS_SHM_H
1086 if( p_vout->p_sys->b_shm )
1088 /* Create image using XShm extension */
1089 p_pic->p_sys->p_image =
1090 CreateShmImage( p_vout, p_vout->p_sys->p_display,
1091 # ifdef MODULE_NAME_IS_xvideo
1092 p_vout->p_sys->i_xvport, p_vout->output.i_chroma,
1094 p_vout->p_sys->p_visual,
1095 p_vout->p_sys->i_screen_depth,
1097 &p_pic->p_sys->shminfo,
1098 p_vout->output.i_width, p_vout->output.i_height );
1101 #endif /* HAVE_SYS_SHM_H */
1103 /* Create image without XShm extension */
1104 p_pic->p_sys->p_image =
1105 CreateImage( p_vout, p_vout->p_sys->p_display,
1106 #ifdef MODULE_NAME_IS_xvideo
1107 p_vout->p_sys->i_xvport, p_vout->output.i_chroma,
1109 p_vout->p_sys->p_visual,
1110 p_vout->p_sys->i_screen_depth,
1111 p_vout->p_sys->i_bytes_per_pixel,
1113 p_vout->output.i_width, p_vout->output.i_height );
1116 if( p_pic->p_sys->p_image == NULL )
1118 free( p_pic->p_sys );
1122 switch( p_vout->output.i_chroma )
1124 #ifdef MODULE_NAME_IS_xvideo
1125 case VLC_FOURCC('I','4','2','0'):
1127 p_pic->Y_PIXELS = p_pic->p_sys->p_image->data
1128 + p_pic->p_sys->p_image->offsets[0];
1129 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1130 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[0];
1131 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1132 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width;
1134 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1135 + p_pic->p_sys->p_image->offsets[1];
1136 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1137 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[1];
1138 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1139 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
1141 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1142 + p_pic->p_sys->p_image->offsets[2];
1143 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1144 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[2];
1145 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1146 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
1148 p_pic->i_planes = 3;
1151 case VLC_FOURCC('Y','V','1','2'):
1153 p_pic->Y_PIXELS = p_pic->p_sys->p_image->data
1154 + p_pic->p_sys->p_image->offsets[0];
1155 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1156 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[0];
1157 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1158 p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p[Y_PLANE].i_pitch;
1159 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width;
1161 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1162 + p_pic->p_sys->p_image->offsets[2];
1163 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1164 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[2];
1165 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1166 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
1168 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1169 + p_pic->p_sys->p_image->offsets[1];
1170 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1171 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[1];
1172 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1173 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
1175 p_pic->i_planes = 3;
1178 case VLC_FOURCC('Y','2','1','1'):
1180 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1181 + p_pic->p_sys->p_image->offsets[0];
1182 p_pic->p->i_lines = p_vout->output.i_height;
1183 /* XXX: this just looks so plain wrong... check it out ! */
1184 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0] / 4;
1185 p_pic->p->i_pixel_pitch = 4;
1186 p_pic->p->i_visible_pitch = p_vout->output.i_width * 4;
1188 p_pic->i_planes = 1;
1191 case VLC_FOURCC('Y','U','Y','2'):
1192 case VLC_FOURCC('U','Y','V','Y'):
1194 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1195 + p_pic->p_sys->p_image->offsets[0];
1196 p_pic->p->i_lines = p_vout->output.i_height;
1197 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1198 p_pic->p->i_pixel_pitch = 4;
1199 p_pic->p->i_visible_pitch = p_vout->output.i_width * 4;
1201 p_pic->i_planes = 1;
1204 case VLC_FOURCC('R','V','1','5'):
1206 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1207 + p_pic->p_sys->p_image->offsets[0];
1208 p_pic->p->i_lines = p_vout->output.i_height;
1209 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1210 p_pic->p->i_pixel_pitch = 2;
1211 p_pic->p->i_visible_pitch = p_vout->output.i_width * 2;
1213 p_pic->i_planes = 1;
1216 case VLC_FOURCC('R','V','1','6'):
1218 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1219 + p_pic->p_sys->p_image->offsets[0];
1220 p_pic->p->i_lines = p_vout->output.i_height;
1221 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1222 p_pic->p->i_pixel_pitch = 2;
1223 p_pic->p->i_visible_pitch = p_vout->output.i_width * 2;
1225 p_pic->i_planes = 1;
1229 case VLC_FOURCC('R','G','B','2'):
1231 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1232 + p_pic->p_sys->p_image->xoffset;
1233 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1234 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1235 p_pic->p->i_pixel_pitch = p_pic->p_sys->p_image->depth;
1236 p_pic->p->i_visible_pitch = p_pic->p_sys->p_image->width;
1238 p_pic->i_planes = 1;
1242 case VLC_FOURCC('R','V','1','6'):
1243 case VLC_FOURCC('R','V','1','5'):
1245 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1246 + p_pic->p_sys->p_image->xoffset;
1247 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1248 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1249 p_pic->p->i_pixel_pitch = p_pic->p_sys->p_image->depth;
1250 p_pic->p->i_visible_pitch = 2 * p_pic->p_sys->p_image->width;
1252 p_pic->i_planes = 1;
1256 case VLC_FOURCC('R','V','3','2'):
1257 case VLC_FOURCC('R','V','2','4'):
1259 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1260 + p_pic->p_sys->p_image->xoffset;
1261 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1262 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1263 p_pic->p->i_pixel_pitch = p_pic->p_sys->p_image->depth;
1264 p_pic->p->i_visible_pitch = 4 * p_pic->p_sys->p_image->width;
1266 p_pic->i_planes = 1;
1272 /* Unknown chroma, tell the guy to get lost */
1273 IMAGE_FREE( p_pic->p_sys->p_image );
1274 free( p_pic->p_sys );
1275 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1276 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1277 p_pic->i_planes = 0;
1284 /*****************************************************************************
1285 * FreePicture: destroy a picture allocated with NewPicture
1286 *****************************************************************************
1287 * Destroy XImage AND associated data. If using Shm, detach shared memory
1288 * segment from server and process, then free it. The XDestroyImage manpage
1289 * says that both the image structure _and_ the data pointed to by the
1290 * image structure are freed, so no need to free p_image->data.
1291 *****************************************************************************/
1292 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1294 /* The order of operations is correct */
1295 #ifdef HAVE_SYS_SHM_H
1296 if( p_vout->p_sys->b_shm )
1298 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
1299 IMAGE_FREE( p_pic->p_sys->p_image );
1301 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
1302 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
1304 msg_Err( p_vout, "cannot detach shared memory (%s)",
1311 IMAGE_FREE( p_pic->p_sys->p_image );
1314 /* Do NOT use XFlush here ! */
1315 XSync( p_vout->p_sys->p_display, False );
1317 free( p_pic->p_sys );
1320 /*****************************************************************************
1321 * ToggleFullScreen: Enable or disable full screen mode
1322 *****************************************************************************
1323 * This function will switch between fullscreen and window mode.
1324 *****************************************************************************/
1325 static void ToggleFullScreen ( vout_thread_t *p_vout )
1329 mwmhints_t mwmhints;
1330 XSetWindowAttributes attributes;
1332 p_vout->b_fullscreen = !p_vout->b_fullscreen;
1334 if( p_vout->b_fullscreen )
1336 msg_Dbg( p_vout, "entering fullscreen mode" );
1338 p_vout->p_sys->b_altfullscreen =
1339 config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
1341 if( p_vout->p_sys->p_win->b_owned )
1343 XUnmapWindow( p_vout->p_sys->p_display,
1344 p_vout->p_sys->p_win->base_window);
1347 p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
1349 /* fullscreen window size and position */
1350 p_vout->p_sys->p_win->i_width =
1351 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1352 p_vout->p_sys->p_win->i_height =
1353 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1355 CreateWindow( p_vout, p_vout->p_sys->p_win );
1357 /* To my knowledge there are two ways to create a borderless window.
1358 * There's the generic way which is to tell x to bypass the window
1359 * manager, but this creates problems with the focus of other
1361 * The other way is to use the motif property "_MOTIF_WM_HINTS" which
1362 * luckily seems to be supported by most window managers. */
1363 if( !p_vout->p_sys->b_altfullscreen )
1365 mwmhints.flags = MWM_HINTS_DECORATIONS;
1366 mwmhints.decorations = False;
1368 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1370 XChangeProperty( p_vout->p_sys->p_display,
1371 p_vout->p_sys->p_win->base_window,
1372 prop, prop, 32, PropModeReplace,
1373 (unsigned char *)&mwmhints,
1374 PROP_MWM_HINTS_ELEMENTS );
1378 /* brute force way to remove decorations */
1379 attributes.override_redirect = True;
1380 XChangeWindowAttributes( p_vout->p_sys->p_display,
1381 p_vout->p_sys->p_win->base_window,
1386 /* Make sure the change is effective */
1387 XReparentWindow( p_vout->p_sys->p_display,
1388 p_vout->p_sys->p_win->base_window,
1389 DefaultRootWindow( p_vout->p_sys->p_display ),
1392 /* fullscreen window size and position */
1393 p_vout->p_sys->p_win->i_width =
1394 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1395 p_vout->p_sys->p_win->i_height =
1396 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1398 XMoveResizeWindow( p_vout->p_sys->p_display,
1399 p_vout->p_sys->p_win->base_window,
1401 p_vout->p_sys->p_win->i_width,
1402 p_vout->p_sys->p_win->i_height );
1406 msg_Dbg( p_vout, "leaving fullscreen mode" );
1407 DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
1408 p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
1410 if( p_vout->p_sys->p_win->b_owned )
1412 XMapWindow( p_vout->p_sys->p_display,
1413 p_vout->p_sys->p_win->base_window);
1417 /* Unfortunately, using XSync() here is not enough to ensure the
1418 * window has already been mapped because the XMapWindow() request
1419 * has not necessarily been sent directly to our window (remember,
1420 * the call is first redirected to the window manager) */
1421 if( p_vout->p_sys->p_win->b_owned )
1425 XWindowEvent( p_vout->p_sys->p_display,
1426 p_vout->p_sys->p_win->base_window,
1427 StructureNotifyMask, &xevent );
1428 } while( xevent.type != MapNotify );
1432 XSync( p_vout->p_sys->p_display, False );
1435 /* Becareful, this can generate a BadMatch error if the window is not
1436 * already mapped by the server (see above) */
1437 XSetInputFocus(p_vout->p_sys->p_display,
1438 p_vout->p_sys->p_win->base_window,
1442 /* signal that the size needs to be updated */
1443 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1446 /*****************************************************************************
1447 * EnableXScreenSaver: enable screen saver
1448 *****************************************************************************
1449 * This function enables the screen saver on a display after it has been
1450 * disabled by XDisableScreenSaver.
1451 * FIXME: what happens if multiple vlc sessions are running at the same
1453 *****************************************************************************/
1454 static void EnableXScreenSaver( vout_thread_t *p_vout )
1456 #ifdef DPMSINFO_IN_DPMS_H
1460 if( p_vout->p_sys->i_ss_timeout )
1462 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1463 p_vout->p_sys->i_ss_interval,
1464 p_vout->p_sys->i_ss_blanking,
1465 p_vout->p_sys->i_ss_exposure );
1468 /* Restore DPMS settings */
1469 #ifdef DPMSINFO_IN_DPMS_H
1470 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1472 if( p_vout->p_sys->b_ss_dpms )
1474 DPMSEnable( p_vout->p_sys->p_display );
1480 /*****************************************************************************
1481 * DisableXScreenSaver: disable screen saver
1482 *****************************************************************************
1483 * See XEnableXScreenSaver
1484 *****************************************************************************/
1485 static void DisableXScreenSaver( vout_thread_t *p_vout )
1487 #ifdef DPMSINFO_IN_DPMS_H
1491 /* Save screen saver informations */
1492 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1493 &p_vout->p_sys->i_ss_interval,
1494 &p_vout->p_sys->i_ss_blanking,
1495 &p_vout->p_sys->i_ss_exposure );
1497 /* Disable screen saver */
1498 if( p_vout->p_sys->i_ss_timeout )
1500 XSetScreenSaver( p_vout->p_sys->p_display, 0,
1501 p_vout->p_sys->i_ss_interval,
1502 p_vout->p_sys->i_ss_blanking,
1503 p_vout->p_sys->i_ss_exposure );
1507 #ifdef DPMSINFO_IN_DPMS_H
1508 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1511 /* Save DPMS current state */
1512 DPMSInfo( p_vout->p_sys->p_display, &unused,
1513 &p_vout->p_sys->b_ss_dpms );
1514 DPMSDisable( p_vout->p_sys->p_display );
1519 /*****************************************************************************
1520 * CreateCursor: create a blank mouse pointer
1521 *****************************************************************************/
1522 static void CreateCursor( vout_thread_t *p_vout )
1524 XColor cursor_color;
1526 p_vout->p_sys->cursor_pixmap =
1527 XCreatePixmap( p_vout->p_sys->p_display,
1528 DefaultRootWindow( p_vout->p_sys->p_display ),
1531 XParseColor( p_vout->p_sys->p_display,
1532 XCreateColormap( p_vout->p_sys->p_display,
1534 p_vout->p_sys->p_display ),
1536 p_vout->p_sys->p_display,
1537 p_vout->p_sys->i_screen ),
1539 "black", &cursor_color );
1541 p_vout->p_sys->blank_cursor =
1542 XCreatePixmapCursor( p_vout->p_sys->p_display,
1543 p_vout->p_sys->cursor_pixmap,
1544 p_vout->p_sys->cursor_pixmap,
1545 &cursor_color, &cursor_color, 1, 1 );
1548 /*****************************************************************************
1549 * DestroyCursor: destroy the blank mouse pointer
1550 *****************************************************************************/
1551 static void DestroyCursor( vout_thread_t *p_vout )
1553 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
1556 /*****************************************************************************
1557 * ToggleCursor: hide or show the mouse pointer
1558 *****************************************************************************
1559 * This function hides the X pointer if it is visible by setting the pointer
1560 * sprite to a blank one. To show it again, we disable the sprite.
1561 *****************************************************************************/
1562 static void ToggleCursor( vout_thread_t *p_vout )
1564 if( p_vout->p_sys->b_mouse_pointer_visible )
1566 XDefineCursor( p_vout->p_sys->p_display,
1567 p_vout->p_sys->p_win->base_window,
1568 p_vout->p_sys->blank_cursor );
1569 p_vout->p_sys->b_mouse_pointer_visible = 0;
1573 XUndefineCursor( p_vout->p_sys->p_display,
1574 p_vout->p_sys->p_win->base_window );
1575 p_vout->p_sys->b_mouse_pointer_visible = 1;
1579 #ifdef MODULE_NAME_IS_xvideo
1580 /*****************************************************************************
1581 * XVideoGetPort: get YUV12 port
1582 *****************************************************************************/
1583 static int XVideoGetPort( vout_thread_t *p_vout,
1584 vlc_fourcc_t i_chroma, vlc_fourcc_t *pi_newchroma )
1586 XvAdaptorInfo *p_adaptor;
1588 int i_adaptor, i_num_adaptors, i_requested_adaptor;
1589 int i_selected_port;
1591 switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
1596 case XvBadExtension:
1597 msg_Warn( p_vout, "XvBadExtension" );
1601 msg_Warn( p_vout, "XvBadAlloc" );
1605 msg_Warn( p_vout, "XvQueryExtension failed" );
1609 switch( XvQueryAdaptors( p_vout->p_sys->p_display,
1610 DefaultRootWindow( p_vout->p_sys->p_display ),
1611 &i_num_adaptors, &p_adaptor ) )
1616 case XvBadExtension:
1617 msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
1621 msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
1625 msg_Warn( p_vout, "XvQueryAdaptors failed" );
1629 i_selected_port = -1;
1630 i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
1632 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
1634 XvImageFormatValues *p_formats;
1635 int i_format, i_num_formats;
1638 /* If we requested an adaptor and it's not this one, we aren't
1640 if( i_requested_adaptor != -1 && i_adaptor != i_requested_adaptor )
1645 /* If the adaptor doesn't have the required properties, skip it */
1646 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
1647 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
1652 /* Check that adaptor supports our requested format... */
1653 p_formats = XvListImageFormats( p_vout->p_sys->p_display,
1654 p_adaptor[i_adaptor].base_id,
1658 i_format < i_num_formats && ( i_selected_port == -1 );
1661 /* Code removed, we can get this through xvinfo anyway */
1663 XvEncodingInfo *p_enc;
1664 int i_enc, i_num_encodings;
1665 XvAttribute *p_attr;
1666 int i_attr, i_num_attributes;
1669 /* If this is not the format we want, or at least a
1670 * similar one, forget it */
1671 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
1676 /* Look for the first available port supporting this format */
1677 for( i_port = p_adaptor[i_adaptor].base_id;
1678 ( i_port < (int)(p_adaptor[i_adaptor].base_id
1679 + p_adaptor[i_adaptor].num_ports) )
1680 && ( i_selected_port == -1 );
1683 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
1686 i_selected_port = i_port;
1687 *pi_newchroma = p_formats[ i_format ].id;
1691 /* If no free port was found, forget it */
1692 if( i_selected_port == -1 )
1697 /* If we found a port, print information about it */
1698 msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
1699 i_adaptor, i_selected_port, p_formats[ i_format ].id,
1700 (char *)&p_formats[ i_format ].id,
1701 ( p_formats[ i_format ].format == XvPacked ) ?
1702 "packed" : "planar" );
1705 msg_Dbg( p_vout, " encoding list:" );
1707 if( XvQueryEncodings( p_vout->p_sys->p_display, i_selected_port,
1708 &i_num_encodings, &p_enc )
1711 msg_Dbg( p_vout, " XvQueryEncodings failed" );
1715 for( i_enc = 0; i_enc < i_num_encodings; i_enc++ )
1717 msg_Dbg( p_vout, " id=%ld, name=%s, size=%ldx%ld,"
1718 " numerator=%d, denominator=%d",
1719 p_enc[i_enc].encoding_id, p_enc[i_enc].name,
1720 p_enc[i_enc].width, p_enc[i_enc].height,
1721 p_enc[i_enc].rate.numerator,
1722 p_enc[i_enc].rate.denominator );
1727 XvFreeEncodingInfo( p_enc );
1730 msg_Dbg( p_vout, " attribute list:" );
1731 p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
1733 &i_num_attributes );
1734 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
1736 msg_Dbg( p_vout, " name=%s, flags=[%s%s ], min=%i, max=%i",
1737 p_attr[i_attr].name,
1738 (p_attr[i_attr].flags & XvGettable) ? " get" : "",
1739 (p_attr[i_attr].flags & XvSettable) ? " set" : "",
1740 p_attr[i_attr].min_value, p_attr[i_attr].max_value );
1743 if( p_attr != NULL )
1750 if( p_formats != NULL )
1757 if( i_num_adaptors > 0 )
1759 XvFreeAdaptorInfo( p_adaptor );
1762 if( i_selected_port == -1 )
1764 if( i_requested_adaptor == -1 )
1766 msg_Warn( p_vout, "no free XVideo port found for format "
1767 "0x%.8x (%4.4s)", i_chroma, (char*)&i_chroma );
1771 msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
1772 "XVideo port for format 0x%.8x (%4.4s)",
1773 i_requested_adaptor, i_chroma, (char*)&i_chroma );
1777 return i_selected_port;
1780 /*****************************************************************************
1781 * XVideoReleasePort: release YUV12 port
1782 *****************************************************************************/
1783 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
1785 XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
1789 /*****************************************************************************
1790 * InitDisplay: open and initialize X11 device
1791 *****************************************************************************
1792 * Create a window according to video output given size, and set other
1793 * properties according to the display properties.
1794 *****************************************************************************/
1795 static int InitDisplay( vout_thread_t *p_vout )
1797 #ifdef MODULE_NAME_IS_x11
1798 XPixmapFormatValues * p_formats; /* pixmap formats */
1799 XVisualInfo * p_xvisual; /* visuals informations */
1800 XVisualInfo xvisual_template; /* visual template */
1801 int i_count; /* array size */
1804 #ifdef HAVE_SYS_SHM_H
1805 p_vout->p_sys->b_shm = 0;
1807 if( config_GetInt( p_vout, MODULE_STRING "-shm" ) )
1810 /* FIXME: As of 2001-03-16, XFree4 for MacOS X does not support Xshm */
1812 p_vout->p_sys->b_shm =
1813 ( XShmQueryExtension( p_vout->p_sys->p_display ) == True );
1816 if( !p_vout->p_sys->b_shm )
1818 msg_Warn( p_vout, "XShm video extension is unavailable" );
1823 msg_Dbg( p_vout, "disabling XShm video extension" );
1827 msg_Warn( p_vout, "XShm video extension is unavailable" );
1831 #ifdef MODULE_NAME_IS_xvideo
1832 /* XXX The brightness and contrast values should be read from environment
1833 * XXX variables... */
1835 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
1836 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
1840 #ifdef MODULE_NAME_IS_x11
1841 /* Initialize structure */
1842 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
1844 /* Get screen depth */
1845 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
1846 p_vout->p_sys->i_screen );
1847 switch( p_vout->p_sys->i_screen_depth )
1851 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
1853 xvisual_template.screen = p_vout->p_sys->i_screen;
1854 xvisual_template.class = DirectColor;
1855 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1856 VisualScreenMask | VisualClassMask,
1857 &xvisual_template, &i_count );
1858 if( p_xvisual == NULL )
1860 msg_Err( p_vout, "no PseudoColor visual available" );
1861 return VLC_EGENERIC;
1863 p_vout->p_sys->i_bytes_per_pixel = 1;
1864 p_vout->output.pf_setpalette = SetPalette;
1871 * Screen depth is higher than 8bpp. TrueColor visual is used.
1873 xvisual_template.screen = p_vout->p_sys->i_screen;
1874 xvisual_template.class = TrueColor;
1875 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1876 VisualScreenMask | VisualClassMask,
1877 &xvisual_template, &i_count );
1878 if( p_xvisual == NULL )
1880 msg_Err( p_vout, "no TrueColor visual available" );
1881 return VLC_EGENERIC;
1884 p_vout->output.i_rmask = p_xvisual->red_mask;
1885 p_vout->output.i_gmask = p_xvisual->green_mask;
1886 p_vout->output.i_bmask = p_xvisual->blue_mask;
1888 /* There is no difference yet between 3 and 4 Bpp. The only way
1889 * to find the actual number of bytes per pixel is to list supported
1890 * pixmap formats. */
1891 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
1892 p_vout->p_sys->i_bytes_per_pixel = 0;
1894 for( ; i_count-- ; p_formats++ )
1896 /* Under XFree4.0, the list contains pixmap formats available
1897 * through all video depths ; so we have to check against current
1899 if( p_formats->depth == (int)p_vout->p_sys->i_screen_depth )
1901 if( p_formats->bits_per_pixel / 8
1902 > (int)p_vout->p_sys->i_bytes_per_pixel )
1904 p_vout->p_sys->i_bytes_per_pixel =
1905 p_formats->bits_per_pixel / 8;
1911 p_vout->p_sys->p_visual = p_xvisual->visual;
1918 #ifdef HAVE_SYS_SHM_H
1919 /*****************************************************************************
1920 * CreateShmImage: create an XImage or XvImage using shared memory extension
1921 *****************************************************************************
1922 * Prepare an XImage or XvImage for display function.
1923 * The order of the operations respects the recommandations of the mit-shm
1924 * document by J.Corbet and K.Packard. Most of the parameters were copied from
1925 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
1926 *****************************************************************************/
1927 static IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
1928 Display* p_display, EXTRA_ARGS_SHM,
1929 int i_width, int i_height )
1931 IMAGE_TYPE *p_image;
1933 /* Create XImage / XvImage */
1934 #ifdef MODULE_NAME_IS_xvideo
1935 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
1936 i_width, i_height, p_shm );
1938 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
1939 p_shm, i_width, i_height );
1941 if( p_image == NULL )
1943 msg_Err( p_vout, "image creation failed" );
1947 /* Allocate shared memory segment - 0776 set the access permission
1948 * rights (like umask), they are not yet supported by all X servers */
1949 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
1950 if( p_shm->shmid < 0 )
1952 msg_Err( p_vout, "cannot allocate shared image data (%s)",
1953 strerror( errno ) );
1954 IMAGE_FREE( p_image );
1958 /* Attach shared memory segment to process (read/write) */
1959 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
1960 if(! p_shm->shmaddr )
1962 msg_Err( p_vout, "cannot attach shared memory (%s)",
1964 IMAGE_FREE( p_image );
1965 shmctl( p_shm->shmid, IPC_RMID, 0 );
1969 /* Read-only data. We won't be using XShmGetImage */
1970 p_shm->readOnly = True;
1972 /* Attach shared memory segment to X server */
1973 if( XShmAttach( p_display, p_shm ) == False )
1975 msg_Err( p_vout, "cannot attach shared memory to X server" );
1976 IMAGE_FREE( p_image );
1977 shmctl( p_shm->shmid, IPC_RMID, 0 );
1978 shmdt( p_shm->shmaddr );
1982 /* Send image to X server. This instruction is required, since having
1983 * built a Shm XImage and not using it causes an error on XCloseDisplay,
1984 * and remember NOT to use XFlush ! */
1985 XSync( p_display, False );
1988 /* Mark the shm segment to be removed when there are no more
1989 * attachements, so it is automatic on process exit or after shmdt */
1990 shmctl( p_shm->shmid, IPC_RMID, 0 );
1997 /*****************************************************************************
1998 * CreateImage: create an XImage or XvImage
1999 *****************************************************************************
2000 * Create a simple image used as a buffer.
2001 *****************************************************************************/
2002 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2003 Display *p_display, EXTRA_ARGS,
2004 int i_width, int i_height )
2006 byte_t * p_data; /* image data storage zone */
2007 IMAGE_TYPE *p_image;
2008 #ifdef MODULE_NAME_IS_x11
2009 int i_quantum; /* XImage quantum (see below) */
2010 int i_bytes_per_line;
2013 /* Allocate memory for image */
2014 #ifdef MODULE_NAME_IS_xvideo
2015 p_data = (byte_t *) malloc( i_width * i_height * 2 ); /* XXX */
2017 i_bytes_per_line = i_width * i_bytes_per_pixel;
2018 p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
2022 msg_Err( p_vout, "out of memory" );
2026 #ifdef MODULE_NAME_IS_x11
2027 /* Optimize the quantum of a scanline regarding its size - the quantum is
2028 a diviser of the number of bits between the start of two scanlines. */
2029 if( i_bytes_per_line & 0xf )
2033 else if( i_bytes_per_line & 0x10 )
2043 /* Create XImage. p_data will be automatically freed */
2044 #ifdef MODULE_NAME_IS_xvideo
2045 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2046 p_data, i_width, i_height );
2048 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2049 p_data, i_width, i_height, i_quantum, 0 );
2051 if( p_image == NULL )
2053 msg_Err( p_vout, "XCreateImage() failed" );
2061 #ifdef MODULE_NAME_IS_x11
2062 /*****************************************************************************
2063 * SetPalette: sets an 8 bpp palette
2064 *****************************************************************************
2065 * This function sets the palette given as an argument. It does not return
2066 * anything, but could later send information on which colors it was unable
2068 *****************************************************************************/
2069 static void SetPalette( vout_thread_t *p_vout,
2070 uint16_t *red, uint16_t *green, uint16_t *blue )
2073 XColor p_colors[255];
2075 /* allocate palette */
2076 for( i = 0; i < 255; i++ )
2078 /* kludge: colors are indexed reversely because color 255 seems
2079 * to be reserved for black even if we try to set it to white */
2080 p_colors[ i ].pixel = 255 - i;
2081 p_colors[ i ].pad = 0;
2082 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
2083 p_colors[ i ].red = red[ 255 - i ];
2084 p_colors[ i ].blue = blue[ 255 - i ];
2085 p_colors[ i ].green = green[ 255 - i ];
2088 XStoreColors( p_vout->p_sys->p_display,
2089 p_vout->p_sys->colormap, p_colors, 255 );