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.36 2003/10/26 23:03:47 sigmunau 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() */
39 #ifdef HAVE_MACHINE_PARAM_H
41 # include <machine/param.h>
42 # include <sys/types.h> /* typedef ushort */
47 # include <netinet/in.h> /* BSD: struct in_addr */
51 # include <sys/shm.h> /* shmget(), shmctl() */
56 #include <X11/Xutil.h>
57 #include <X11/keysym.h>
59 # include <X11/extensions/XShm.h>
61 #ifdef DPMSINFO_IN_DPMS_H
62 # include <X11/extensions/dpms.h>
65 #ifdef MODULE_NAME_IS_xvideo
66 # include <X11/extensions/Xv.h>
67 # include <X11/extensions/Xvlib.h>
71 # include <X11/extensions/Xinerama.h>
76 /*****************************************************************************
78 *****************************************************************************/
79 int E_(Activate) ( vlc_object_t * );
80 void E_(Deactivate) ( vlc_object_t * );
82 static int InitVideo ( vout_thread_t * );
83 static void EndVideo ( vout_thread_t * );
84 static void DisplayVideo ( vout_thread_t *, picture_t * );
85 static int ManageVideo ( vout_thread_t * );
87 static int InitDisplay ( vout_thread_t * );
89 static int CreateWindow ( vout_thread_t *, x11_window_t * );
90 static void DestroyWindow ( vout_thread_t *, x11_window_t * );
92 static int NewPicture ( vout_thread_t *, picture_t * );
93 static void FreePicture ( vout_thread_t *, picture_t * );
95 static IMAGE_TYPE *CreateImage ( vout_thread_t *,
96 Display *, EXTRA_ARGS, int, int );
98 static IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
99 Display *, EXTRA_ARGS_SHM, int, int );
102 static void ToggleFullScreen ( vout_thread_t * );
104 static void EnableXScreenSaver ( vout_thread_t * );
105 static void DisableXScreenSaver ( vout_thread_t * );
107 static void CreateCursor ( vout_thread_t * );
108 static void DestroyCursor ( vout_thread_t * );
109 static void ToggleCursor ( vout_thread_t * );
111 #ifdef MODULE_NAME_IS_xvideo
112 static int XVideoGetPort ( vout_thread_t *, vlc_fourcc_t, vlc_fourcc_t * );
113 static void XVideoReleasePort( vout_thread_t *, int );
116 #ifdef MODULE_NAME_IS_x11
117 static void SetPalette ( vout_thread_t *,
118 uint16_t *, uint16_t *, uint16_t * );
121 static void TestNetWMSupport( vout_thread_t * );
123 /*****************************************************************************
124 * Activate: allocate X11 video thread output method
125 *****************************************************************************
126 * This function allocate and initialize a X11 vout method. It uses some of the
127 * vout properties to choose the window size, and change them according to the
128 * actual properties of the display.
129 *****************************************************************************/
130 int E_(Activate) ( vlc_object_t *p_this )
132 vout_thread_t *p_vout = (vout_thread_t *)p_this;
134 #ifdef MODULE_NAME_IS_xvideo
136 vlc_fourcc_t i_chroma = 0;
137 vlc_bool_t b_chroma = 0;
140 p_vout->pf_init = InitVideo;
141 p_vout->pf_end = EndVideo;
142 p_vout->pf_manage = ManageVideo;
143 p_vout->pf_render = NULL;
144 p_vout->pf_display = DisplayVideo;
146 /* Allocate structure */
147 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
148 if( p_vout->p_sys == NULL )
150 msg_Err( p_vout, "out of memory" );
154 /* Open display, using the "display" config variable or the DISPLAY
155 * environment variable */
156 psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
158 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
160 if( p_vout->p_sys->p_display == NULL ) /* error */
162 msg_Err( p_vout, "cannot open display %s",
163 XDisplayName( psz_display ) );
164 free( p_vout->p_sys );
165 if( psz_display ) free( psz_display );
168 if( psz_display ) free( psz_display );
170 /* Get a screen ID matching the XOpenDisplay return value */
171 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
173 #ifdef MODULE_NAME_IS_xvideo
174 psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
177 if( strlen( psz_chroma ) >= 4 )
179 /* Do not use direct assignment because we are not sure of the
181 memcpy(&i_chroma, psz_chroma, 4);
190 msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
191 i_chroma, (char*)&i_chroma );
195 i_chroma = p_vout->render.i_chroma;
198 /* Check that we have access to an XVideo port providing this chroma */
199 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, VLC2X11_FOURCC(i_chroma),
200 &p_vout->output.i_chroma );
201 if( p_vout->p_sys->i_xvport < 0 )
203 /* If a specific chroma format was requested, then we don't try to
204 * be cleverer than the user. He knew pretty well what he wanted. */
207 XCloseDisplay( p_vout->p_sys->p_display );
208 free( p_vout->p_sys );
212 /* It failed, but it's not completely lost ! We try to open an
213 * XVideo port for an YUY2 picture. We'll need to do an YUV
214 * conversion, but at least it has got scaling. */
215 p_vout->p_sys->i_xvport =
216 XVideoGetPort( p_vout, X11_FOURCC('Y','U','Y','2'),
217 &p_vout->output.i_chroma );
218 if( p_vout->p_sys->i_xvport < 0 )
220 /* It failed, but it's not completely lost ! We try to open an
221 * XVideo port for a simple 16bpp RGB picture. We'll need to do
222 * an YUV conversion, but at least it has got scaling. */
223 p_vout->p_sys->i_xvport =
224 XVideoGetPort( p_vout, X11_FOURCC('R','V','1','6'),
225 &p_vout->output.i_chroma );
226 if( p_vout->p_sys->i_xvport < 0 )
228 XCloseDisplay( p_vout->p_sys->p_display );
229 free( p_vout->p_sys );
234 p_vout->output.i_chroma = X112VLC_FOURCC(p_vout->output.i_chroma);
237 /* Create blank cursor (for mouse cursor autohiding) */
238 p_vout->p_sys->i_time_mouse_last_moved = mdate();
239 p_vout->p_sys->b_mouse_pointer_visible = 1;
240 CreateCursor( p_vout );
242 /* Set main window's size */
243 p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
244 p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
246 /* Spawn base window - this window will include the video output window,
247 * but also command buttons, subtitles and other indicators */
248 if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
250 msg_Err( p_vout, "cannot create X11 window" );
251 DestroyCursor( p_vout );
252 XCloseDisplay( p_vout->p_sys->p_display );
253 free( p_vout->p_sys );
257 /* Open and initialize device. */
258 if( InitDisplay( p_vout ) )
260 msg_Err( p_vout, "cannot initialize X11 display" );
261 DestroyCursor( p_vout );
262 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
263 XCloseDisplay( p_vout->p_sys->p_display );
264 free( p_vout->p_sys );
268 /* Disable screen saver */
269 DisableXScreenSaver( p_vout );
272 p_vout->p_sys->b_altfullscreen = 0;
273 p_vout->p_sys->i_time_button_last_pressed = 0;
275 TestNetWMSupport( p_vout );
280 /*****************************************************************************
281 * Deactivate: destroy X11 video thread output method
282 *****************************************************************************
283 * Terminate an output method created by Open
284 *****************************************************************************/
285 void E_(Deactivate) ( vlc_object_t *p_this )
287 vout_thread_t *p_vout = (vout_thread_t *)p_this;
289 /* If the fullscreen window is still open, close it */
290 if( p_vout->b_fullscreen )
292 ToggleFullScreen( p_vout );
295 /* Restore cursor if it was blanked */
296 if( !p_vout->p_sys->b_mouse_pointer_visible )
298 ToggleCursor( p_vout );
301 #ifdef MODULE_NAME_IS_x11
302 /* Destroy colormap */
303 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
305 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
308 XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
311 DestroyCursor( p_vout );
312 EnableXScreenSaver( p_vout );
313 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
315 XCloseDisplay( p_vout->p_sys->p_display );
317 /* Destroy structure */
318 free( p_vout->p_sys );
321 /*****************************************************************************
322 * InitVideo: initialize X11 video thread output method
323 *****************************************************************************
324 * This function create the XImages needed by the output thread. It is called
325 * at the beginning of the thread, but also each time the window is resized.
326 *****************************************************************************/
327 static int InitVideo( vout_thread_t *p_vout )
332 I_OUTPUTPICTURES = 0;
334 #ifdef MODULE_NAME_IS_xvideo
335 /* Initialize the output structure; we already found an XVideo port,
336 * and the corresponding chroma we will be using. Since we can
337 * arbitrary scale, stick to the coordinates and aspect. */
338 p_vout->output.i_width = p_vout->render.i_width;
339 p_vout->output.i_height = p_vout->render.i_height;
340 p_vout->output.i_aspect = p_vout->render.i_aspect;
342 switch( p_vout->output.i_chroma )
344 case VLC_FOURCC('R','V','1','5'):
345 p_vout->output.i_rmask = 0x001f;
346 p_vout->output.i_gmask = 0x07e0;
347 p_vout->output.i_bmask = 0xf800;
349 case VLC_FOURCC('R','V','1','6'):
350 p_vout->output.i_rmask = 0x001f;
351 p_vout->output.i_gmask = 0x03e0;
352 p_vout->output.i_bmask = 0x7c00;
357 /* Initialize the output structure: RGB with square pixels, whatever
358 * the input format is, since it's the only format we know */
359 switch( p_vout->p_sys->i_screen_depth )
361 case 8: /* FIXME: set the palette */
362 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
364 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
366 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
368 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
370 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
372 msg_Err( p_vout, "unknown screen depth %i",
373 p_vout->p_sys->i_screen_depth );
377 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
378 p_vout->p_sys->p_win->i_height,
380 &p_vout->output.i_width, &p_vout->output.i_height );
382 /* Assume we have square pixels */
383 p_vout->output.i_aspect = p_vout->output.i_width
384 * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
387 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
388 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
392 /* Find an empty picture slot */
393 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
395 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
397 p_pic = p_vout->p_picture + i_index;
402 /* Allocate the picture */
403 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
408 p_pic->i_status = DESTROYED_PICTURE;
409 p_pic->i_type = DIRECT_PICTURE;
411 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
419 /*****************************************************************************
420 * DisplayVideo: displays previously rendered output
421 *****************************************************************************
422 * This function sends the currently rendered image to X11 server.
423 * (The Xv extension takes care of "double-buffering".)
424 *****************************************************************************/
425 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
427 int i_width, i_height, i_x, i_y;
429 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
430 p_vout->p_sys->p_win->i_height,
431 &i_x, &i_y, &i_width, &i_height );
433 #ifdef HAVE_SYS_SHM_H
434 if( p_vout->p_sys->b_shm )
436 /* Display rendered image using shared memory extension */
437 # ifdef MODULE_NAME_IS_xvideo
438 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
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*/,
442 p_vout->output.i_width, p_vout->output.i_height,
443 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
444 False /* Don't put True here or you'll waste your CPU */ );
446 XShmPutImage( p_vout->p_sys->p_display,
447 p_vout->p_sys->p_win->video_window,
448 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
449 0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
450 p_vout->output.i_width, p_vout->output.i_height,
451 False /* Don't put True here ! */ );
455 #endif /* HAVE_SYS_SHM_H */
457 /* Use standard XPutImage -- this is gonna be slow ! */
458 #ifdef MODULE_NAME_IS_xvideo
459 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
460 p_vout->p_sys->p_win->video_window,
461 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
462 0 /*src_x*/, 0 /*src_y*/,
463 p_vout->output.i_width, p_vout->output.i_height,
464 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
466 XPutImage( p_vout->p_sys->p_display,
467 p_vout->p_sys->p_win->video_window,
468 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
469 0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
470 p_vout->output.i_width, p_vout->output.i_height );
474 /* Make sure the command is sent now - do NOT use XFlush !*/
475 XSync( p_vout->p_sys->p_display, False );
478 /*****************************************************************************
479 * ManageVideo: handle X11 events
480 *****************************************************************************
481 * This function should be called regularly by video output thread. It manages
482 * X11 events and allows window resizing. It returns a non null value on
484 *****************************************************************************/
485 static int ManageVideo( vout_thread_t *p_vout )
487 XEvent xevent; /* X11 event */
488 char i_key; /* ISO Latin-1 key */
492 /* Handle X11 events: ConfigureNotify events are parsed to know if the
493 * output window's size changed, MapNotify and UnmapNotify to know if the
494 * window is mapped (and if the display is useful), and ClientMessages
495 * to intercept window destruction requests */
497 while( XCheckWindowEvent( p_vout->p_sys->p_display,
498 p_vout->p_sys->p_win->base_window,
499 StructureNotifyMask | KeyPressMask |
500 ButtonPressMask | ButtonReleaseMask |
501 PointerMotionMask | Button1MotionMask , &xevent )
504 /* ConfigureNotify event: prepare */
505 if( xevent.type == ConfigureNotify )
507 if( (unsigned int)xevent.xconfigure.width
508 != p_vout->p_sys->p_win->i_width
509 || (unsigned int)xevent.xconfigure.height
510 != p_vout->p_sys->p_win->i_height )
512 /* Update dimensions */
513 p_vout->i_changes |= VOUT_SIZE_CHANGE;
514 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
515 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
519 else if( xevent.type == KeyPress )
523 /* We may have keys like F1 trough F12, ESC ... */
524 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
525 xevent.xkey.keycode, 0 );
526 switch( (int)x_key_symbol )
567 val.i_int = KEY_ENTER;
573 val.i_int = KEY_MENU;
576 val.i_int = KEY_LEFT;
579 val.i_int = KEY_RIGHT;
585 val.i_int = KEY_DOWN;
588 val.i_int = KEY_HOME;
594 val.i_int = KEY_PAGEUP;
597 val.i_int = KEY_PAGEDOWN;
600 val.i_int = KEY_SPACE;
605 * The reason why I use this instead of XK_0 is that
606 * with XLookupString, we don't have to care about
609 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
611 /* FIXME: handle stuff here */
618 if ( xevent.xkey.state & ShiftMask )
620 val.i_int |= KEY_MODIFIER_SHIFT;
622 if ( xevent.xkey.state & ControlMask )
624 msg_Dbg( p_vout, "control pressed, key value is %x", val.i_int );
625 val.i_int |= KEY_MODIFIER_CTRL;
627 if ( xevent.xkey.state & Mod1Mask )
629 val.i_int |= KEY_MODIFIER_ALT;
631 if ( val.i_int == config_GetInt( p_vout, "fullscreen-key" ) )
633 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
637 var_Set( p_vout->p_vlc, "key-pressed", val );
642 else if( xevent.type == ButtonPress )
644 switch( ((XButtonEvent *)&xevent)->button )
647 var_Get( p_vout, "mouse-button-down", &val );
649 var_Set( p_vout, "mouse-button-down", val );
651 /* detect double-clicks */
652 if( ( ((XButtonEvent *)&xevent)->time -
653 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
655 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
658 p_vout->p_sys->i_time_button_last_pressed =
659 ((XButtonEvent *)&xevent)->time;
662 var_Get( p_vout, "mouse-button-down", &val );
664 var_Set( p_vout, "mouse-button-down", val );
668 var_Get( p_vout, "mouse-button-down", &val );
670 var_Set( p_vout, "mouse-button-down", val );
674 var_Get( p_vout, "mouse-button-down", &val );
676 var_Set( p_vout, "mouse-button-down", val );
677 input_Seek( p_vout, 15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
681 var_Get( p_vout, "mouse-button-down", &val );
683 var_Set( p_vout, "mouse-button-down", val );
684 input_Seek( p_vout, -15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
689 else if( xevent.type == ButtonRelease )
691 switch( ((XButtonEvent *)&xevent)->button )
694 var_Get( p_vout, "mouse-button-down", &val );
696 var_Set( p_vout, "mouse-button-down", val );
698 val.b_bool = VLC_TRUE;
699 var_Set( p_vout, "mouse-clicked", val );
704 playlist_t *p_playlist;
706 var_Get( p_vout, "mouse-button-down", &val );
708 var_Set( p_vout, "mouse-button-down", val );
710 p_playlist = vlc_object_find( p_vout,
713 if( p_playlist != NULL )
716 var_Get( p_playlist, "intf-show", &val );
717 val.b_bool = !val.b_bool;
718 var_Set( p_playlist, "intf-show", val );
719 vlc_object_release( p_playlist );
726 intf_thread_t *p_intf;
727 playlist_t *p_playlist;
729 var_Get( p_vout, "mouse-button-down", &val );
731 var_Set( p_vout, "mouse-button-down", val );
732 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
736 p_intf->b_menu_change = 1;
737 vlc_object_release( p_intf );
740 p_playlist = vlc_object_find( p_vout,
743 if( p_playlist != NULL )
746 var_Set( p_playlist, "intf-popupmenu", val );
747 vlc_object_release( p_playlist );
753 var_Get( p_vout, "mouse-button-down", &val );
755 var_Set( p_vout, "mouse-button-down", val );
759 var_Get( p_vout, "mouse-button-down", &val );
761 var_Set( p_vout, "mouse-button-down", val );
767 else if( xevent.type == MotionNotify )
769 int i_width, i_height, i_x, i_y;
772 /* somewhat different use for vout_PlacePicture:
773 * here the values are needed to give to mouse coordinates
774 * in the original picture space */
775 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
776 p_vout->p_sys->p_win->i_height,
777 &i_x, &i_y, &i_width, &i_height );
779 val.i_int = ( xevent.xmotion.x - i_x )
780 * p_vout->render.i_width / i_width;
781 var_Set( p_vout, "mouse-x", val );
782 val.i_int = ( xevent.xmotion.y - i_y )
783 * p_vout->render.i_height / i_height;
784 var_Set( p_vout, "mouse-y", val );
786 val.b_bool = VLC_TRUE;
787 var_Set( p_vout, "mouse-moved", val );
789 p_vout->p_sys->i_time_mouse_last_moved = mdate();
790 if( ! p_vout->p_sys->b_mouse_pointer_visible )
792 ToggleCursor( p_vout );
795 else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
796 || xevent.type == MapNotify
797 || xevent.type == UnmapNotify )
799 /* Ignore these events */
801 else /* Other events */
803 msg_Warn( p_vout, "unhandled event %d received", xevent.type );
807 /* Handle events for video output sub-window */
808 while( XCheckWindowEvent( p_vout->p_sys->p_display,
809 p_vout->p_sys->p_win->video_window,
810 ExposureMask, &xevent ) == True )
812 /* Window exposed (only handled if stream playback is paused) */
813 if( xevent.type == Expose )
815 if( ((XExposeEvent *)&xevent)->count == 0 )
817 /* (if this is the last a collection of expose events...) */
819 if( p_vout->p_vlc->p_input_bank->pp_input[0] != NULL )
821 if( PAUSE_S == p_vout->p_vlc->p_input_bank->pp_input[0]
822 ->stream.control.i_status )
824 /* XVideoDisplay( p_vout )*/;
832 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
833 * are handled - according to the man pages, the format is always 32
835 while( XCheckTypedEvent( p_vout->p_sys->p_display,
836 ClientMessage, &xevent ) )
838 if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
839 && ((Atom)xevent.xclient.data.l[0]
840 == p_vout->p_sys->p_win->wm_delete_window ) )
842 /* the user wants to close the window */
843 playlist_t * p_playlist =
844 (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
846 if( p_playlist != NULL )
848 playlist_Stop( p_playlist );
849 vlc_object_release( p_playlist );
857 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
861 /* Update the object variable and trigger callback */
862 val.b_bool = !p_vout->b_fullscreen;
863 var_Set( p_vout, "fullscreen", val );
865 ToggleFullScreen( p_vout );
866 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
872 * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
873 * the size flag inside the fullscreen routine)
875 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
877 int i_width, i_height, i_x, i_y;
879 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
881 #ifdef MODULE_NAME_IS_x11
882 /* We need to signal the vout thread about the size change because it
883 * is doing the rescaling */
884 p_vout->i_changes |= VOUT_SIZE_CHANGE;
887 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
888 p_vout->p_sys->p_win->i_height,
889 &i_x, &i_y, &i_width, &i_height );
891 XMoveResizeWindow( p_vout->p_sys->p_display,
892 p_vout->p_sys->p_win->video_window,
893 i_x, i_y, i_width, i_height );
896 /* Autohide Cursour */
897 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
899 /* Hide the mouse automatically */
900 if( p_vout->p_sys->b_mouse_pointer_visible )
902 ToggleCursor( p_vout );
909 /*****************************************************************************
910 * EndVideo: terminate X11 video thread output method
911 *****************************************************************************
912 * Destroy the X11 XImages created by Init. It is called at the end of
913 * the thread, but also each time the window is resized.
914 *****************************************************************************/
915 static void EndVideo( vout_thread_t *p_vout )
919 /* Free the direct buffers we allocated */
920 for( i_index = I_OUTPUTPICTURES ; i_index ; )
923 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
927 /* following functions are local */
929 /*****************************************************************************
930 * CreateWindow: open and set-up X11 main window
931 *****************************************************************************/
932 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
934 XSizeHints xsize_hints;
935 XSetWindowAttributes xwindow_attributes;
939 vlc_bool_t b_expose = VLC_FALSE;
940 vlc_bool_t b_configure_notify = VLC_FALSE;
941 vlc_bool_t b_map_notify = VLC_FALSE;
944 long long int i_drawable;
946 /* Prepare window manager hints and properties */
947 xsize_hints.base_width = p_win->i_width;
948 xsize_hints.base_height = p_win->i_height;
949 xsize_hints.flags = PSize;
950 p_win->wm_protocols =
951 XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
952 p_win->wm_delete_window =
953 XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
955 /* Prepare window attributes */
956 xwindow_attributes.backing_store = Always; /* save the hidden part */
957 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
958 p_vout->p_sys->i_screen);
959 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
961 /* Check whether someone provided us with a window ID */
962 var_Get( p_vout->p_vlc, "drawable", &val );
963 i_drawable = p_vout->b_fullscreen ? 0 : val.i_int;
967 /* Create the window and set hints - the window must receive
968 * ConfigureNotify events, and until it is displayed, Expose and
969 * MapNotify events. */
972 XCreateWindow( p_vout->p_sys->p_display,
973 DefaultRootWindow( p_vout->p_sys->p_display ),
975 p_win->i_width, p_win->i_height,
978 CWBackingStore | CWBackPixel | CWEventMask,
979 &xwindow_attributes );
981 if( !p_vout->b_fullscreen )
983 /* Set window manager hints and properties: size hints, command,
984 * window's name, and accepted protocols */
985 XSetWMNormalHints( p_vout->p_sys->p_display,
986 p_win->base_window, &xsize_hints );
987 XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
988 p_vout->p_vlc->ppsz_argv, p_vout->p_vlc->i_argc );
990 XStoreName( p_vout->p_sys->p_display, p_win->base_window,
991 #ifdef MODULE_NAME_IS_x11
992 VOUT_TITLE " (X11 output)"
994 VOUT_TITLE " (XVideo output)"
1001 /* Get the parent window's geometry information */
1003 unsigned int dummy2, dummy3;
1004 XGetGeometry( p_vout->p_sys->p_display, i_drawable,
1005 &dummy1, &dummy2, &dummy3,
1010 /* We are already configured */
1011 b_configure_notify = VLC_TRUE;
1013 /* From man XSelectInput: only one client at a time can select a
1014 * ButtonPress event, so we need to open a new window anyway. */
1015 p_win->base_window =
1016 XCreateWindow( p_vout->p_sys->p_display,
1019 p_win->i_width, p_win->i_height,
1021 0, CopyFromParent, 0,
1022 CWBackingStore | CWBackPixel | CWEventMask,
1023 &xwindow_attributes );
1026 if( (p_win->wm_protocols == None) /* use WM_DELETE_WINDOW */
1027 || (p_win->wm_delete_window == None)
1028 || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
1029 &p_win->wm_delete_window, 1 ) )
1031 /* WM_DELETE_WINDOW is not supported by window manager */
1032 msg_Warn( p_vout, "missing or bad window manager" );
1035 /* Creation of a graphic context that doesn't generate a GraphicsExpose
1036 * event when using functions like XCopyArea */
1037 xgcvalues.graphics_exposures = False;
1038 p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1040 GCGraphicsExposures, &xgcvalues );
1042 /* Send orders to server, and wait until window is displayed - three
1043 * events must be received: a MapNotify event, an Expose event allowing
1044 * drawing in the window, and a ConfigureNotify to get the window
1045 * dimensions. Once those events have been received, only
1046 * ConfigureNotify events need to be received. */
1047 XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1050 XNextEvent( p_vout->p_sys->p_display, &xevent);
1051 if( (xevent.type == Expose)
1052 && (xevent.xexpose.window == p_win->base_window) )
1054 b_expose = VLC_TRUE;
1056 else if( (xevent.type == MapNotify)
1057 && (xevent.xmap.window == p_win->base_window) )
1059 b_map_notify = VLC_TRUE;
1061 else if( (xevent.type == ConfigureNotify)
1062 && (xevent.xconfigure.window == p_win->base_window) )
1064 b_configure_notify = VLC_TRUE;
1065 p_win->i_width = xevent.xconfigure.width;
1066 p_win->i_height = xevent.xconfigure.height;
1068 } while( !( b_expose && b_configure_notify && b_map_notify ) );
1070 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1071 StructureNotifyMask | KeyPressMask |
1072 ButtonPressMask | ButtonReleaseMask |
1073 PointerMotionMask );
1075 #ifdef MODULE_NAME_IS_x11
1076 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1078 /* Allocate a new palette */
1079 p_vout->p_sys->colormap =
1080 XCreateColormap( p_vout->p_sys->p_display,
1081 DefaultRootWindow( p_vout->p_sys->p_display ),
1082 DefaultVisual( p_vout->p_sys->p_display,
1083 p_vout->p_sys->i_screen ),
1086 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1087 XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1088 CWColormap, &xwindow_attributes );
1092 /* Create video output sub-window. */
1093 p_win->video_window = XCreateSimpleWindow(
1094 p_vout->p_sys->p_display,
1095 p_win->base_window, 0, 0,
1096 p_win->i_width, p_win->i_height,
1098 BlackPixel( p_vout->p_sys->p_display,
1099 p_vout->p_sys->i_screen ),
1100 WhitePixel( p_vout->p_sys->p_display,
1101 p_vout->p_sys->i_screen ) );
1103 XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1104 BlackPixel( p_vout->p_sys->p_display,
1105 p_vout->p_sys->i_screen ) );
1107 XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1108 XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1111 /* make sure the video window will be centered in the next ManageVideo() */
1112 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1114 /* If the cursor was formerly blank than blank it again */
1115 if( !p_vout->p_sys->b_mouse_pointer_visible )
1117 ToggleCursor( p_vout );
1118 ToggleCursor( p_vout );
1121 /* Do NOT use XFlush here ! */
1122 XSync( p_vout->p_sys->p_display, False );
1124 /* At this stage, the window is open, displayed, and ready to
1126 p_vout->p_sys->p_win = p_win;
1131 /*****************************************************************************
1132 * DestroyWindow: destroy the window
1133 *****************************************************************************
1135 *****************************************************************************/
1136 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1138 /* Do NOT use XFlush here ! */
1139 XSync( p_vout->p_sys->p_display, False );
1141 XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1142 XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1144 XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1145 XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1148 /*****************************************************************************
1149 * NewPicture: allocate a picture
1150 *****************************************************************************
1151 * Returns 0 on success, -1 otherwise
1152 *****************************************************************************/
1153 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1155 #ifdef MODULE_NAME_IS_xvideo
1159 /* We know the chroma, allocate a buffer which will be used
1160 * directly by the decoder */
1161 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1163 if( p_pic->p_sys == NULL )
1168 /* Fill in picture_t fields */
1169 vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
1170 p_vout->output.i_width, p_vout->output.i_height,
1171 p_vout->output.i_aspect );
1173 #ifdef HAVE_SYS_SHM_H
1174 if( p_vout->p_sys->b_shm )
1176 /* Create image using XShm extension */
1177 p_pic->p_sys->p_image =
1178 CreateShmImage( p_vout, p_vout->p_sys->p_display,
1179 # ifdef MODULE_NAME_IS_xvideo
1180 p_vout->p_sys->i_xvport, p_vout->output.i_chroma,
1182 p_vout->p_sys->p_visual,
1183 p_vout->p_sys->i_screen_depth,
1185 &p_pic->p_sys->shminfo,
1186 p_vout->output.i_width, p_vout->output.i_height );
1189 #endif /* HAVE_SYS_SHM_H */
1191 /* Create image without XShm extension */
1192 p_pic->p_sys->p_image =
1193 CreateImage( p_vout, p_vout->p_sys->p_display,
1194 #ifdef MODULE_NAME_IS_xvideo
1195 p_vout->p_sys->i_xvport, p_vout->output.i_chroma,
1196 p_pic->format.i_bits_per_pixel,
1198 p_vout->p_sys->p_visual,
1199 p_vout->p_sys->i_screen_depth,
1200 p_vout->p_sys->i_bytes_per_pixel,
1202 p_vout->output.i_width, p_vout->output.i_height );
1205 if( p_pic->p_sys->p_image == NULL )
1207 free( p_pic->p_sys );
1211 switch( p_vout->output.i_chroma )
1213 #ifdef MODULE_NAME_IS_xvideo
1214 case VLC_FOURCC('I','4','2','0'):
1215 case VLC_FOURCC('Y','V','1','2'):
1216 case VLC_FOURCC('Y','2','1','1'):
1217 case VLC_FOURCC('Y','U','Y','2'):
1218 case VLC_FOURCC('U','Y','V','Y'):
1219 case VLC_FOURCC('R','V','1','5'):
1220 case VLC_FOURCC('R','V','1','6'):
1221 case VLC_FOURCC('R','V','2','4'): /* Fixme: pixel pitch == 4 ? */
1222 case VLC_FOURCC('R','V','3','2'):
1224 for( i_plane = 0; i_plane < p_pic->p_sys->p_image->num_planes;
1227 p_pic->p[i_plane].p_pixels = p_pic->p_sys->p_image->data
1228 + p_pic->p_sys->p_image->offsets[i_plane];
1229 p_pic->p[i_plane].i_pitch =
1230 p_pic->p_sys->p_image->pitches[i_plane];
1232 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
1234 /* U and V inverted compared to I420
1235 * Fixme: this should be handled by the vout core */
1236 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1237 + p_pic->p_sys->p_image->offsets[2];
1238 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1239 + p_pic->p_sys->p_image->offsets[1];
1244 case VLC_FOURCC('R','G','B','2'):
1245 case VLC_FOURCC('R','V','1','6'):
1246 case VLC_FOURCC('R','V','1','5'):
1247 case VLC_FOURCC('R','V','2','4'):
1248 case VLC_FOURCC('R','V','3','2'):
1250 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1251 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1252 + p_pic->p_sys->p_image->xoffset;
1253 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1255 /* p_pic->p->i_pixel_pitch = 4 for RV24 but this should be set
1256 * properly by vout_InitPicture() */
1257 p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
1258 * p_pic->p_sys->p_image->width;
1263 /* Unknown chroma, tell the guy to get lost */
1264 IMAGE_FREE( p_pic->p_sys->p_image );
1265 free( p_pic->p_sys );
1266 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1267 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1268 p_pic->i_planes = 0;
1275 /*****************************************************************************
1276 * FreePicture: destroy a picture allocated with NewPicture
1277 *****************************************************************************
1278 * Destroy XImage AND associated data. If using Shm, detach shared memory
1279 * segment from server and process, then free it. The XDestroyImage manpage
1280 * says that both the image structure _and_ the data pointed to by the
1281 * image structure are freed, so no need to free p_image->data.
1282 *****************************************************************************/
1283 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1285 /* The order of operations is correct */
1286 #ifdef HAVE_SYS_SHM_H
1287 if( p_vout->p_sys->b_shm )
1289 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
1290 IMAGE_FREE( p_pic->p_sys->p_image );
1292 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
1293 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
1295 msg_Err( p_vout, "cannot detach shared memory (%s)",
1302 IMAGE_FREE( p_pic->p_sys->p_image );
1305 /* Do NOT use XFlush here ! */
1306 XSync( p_vout->p_sys->p_display, False );
1308 free( p_pic->p_sys );
1311 /*****************************************************************************
1312 * ToggleFullScreen: Enable or disable full screen mode
1313 *****************************************************************************
1314 * This function will switch between fullscreen and window mode.
1315 *****************************************************************************/
1316 static void ToggleFullScreen ( vout_thread_t *p_vout )
1320 mwmhints_t mwmhints;
1321 XSetWindowAttributes attributes;
1323 #ifdef HAVE_XINERAMA
1327 p_vout->b_fullscreen = !p_vout->b_fullscreen;
1329 if( p_vout->b_fullscreen )
1331 msg_Dbg( p_vout, "entering fullscreen mode" );
1333 p_vout->p_sys->b_altfullscreen =
1334 config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
1336 XUnmapWindow( p_vout->p_sys->p_display,
1337 p_vout->p_sys->p_win->base_window);
1339 p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
1341 /* fullscreen window size and position */
1342 p_vout->p_sys->p_win->i_width =
1343 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1344 p_vout->p_sys->p_win->i_height =
1345 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1347 CreateWindow( p_vout, p_vout->p_sys->p_win );
1349 /* To my knowledge there are two ways to create a borderless window.
1350 * There's the generic way which is to tell x to bypass the window
1351 * manager, but this creates problems with the focus of other
1353 * The other way is to use the motif property "_MOTIF_WM_HINTS" which
1354 * luckily seems to be supported by most window managers. */
1355 if( !p_vout->p_sys->b_altfullscreen )
1357 mwmhints.flags = MWM_HINTS_DECORATIONS;
1358 mwmhints.decorations = False;
1360 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1362 XChangeProperty( p_vout->p_sys->p_display,
1363 p_vout->p_sys->p_win->base_window,
1364 prop, prop, 32, PropModeReplace,
1365 (unsigned char *)&mwmhints,
1366 PROP_MWM_HINTS_ELEMENTS );
1370 /* brute force way to remove decorations */
1371 attributes.override_redirect = True;
1372 XChangeWindowAttributes( p_vout->p_sys->p_display,
1373 p_vout->p_sys->p_win->base_window,
1377 /* Make sure the change is effective */
1378 XReparentWindow( p_vout->p_sys->p_display,
1379 p_vout->p_sys->p_win->base_window,
1380 DefaultRootWindow( p_vout->p_sys->p_display ),
1384 if( p_vout->p_sys->b_net_wm_state_fullscreen )
1386 XClientMessageEvent event;
1388 memset( &event, 0, sizeof( XClientMessageEvent ) );
1390 event.type = ClientMessage;
1391 event.message_type = p_vout->p_sys->net_wm_state;
1392 event.display = p_vout->p_sys->p_display;
1393 event.window = p_vout->p_sys->p_win->base_window;
1395 event.data.l[ 0 ] = 1; /* set property */
1396 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_fullscreen;
1398 XSendEvent( p_vout->p_sys->p_display,
1399 DefaultRootWindow( p_vout->p_sys->p_display ),
1400 False, SubstructureRedirectMask,
1404 /* Make sure the change is effective */
1405 XReparentWindow( p_vout->p_sys->p_display,
1406 p_vout->p_sys->p_win->base_window,
1407 DefaultRootWindow( p_vout->p_sys->p_display ),
1410 /* fullscreen window size and position */
1411 p_vout->p_sys->p_win->i_width =
1412 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1413 p_vout->p_sys->p_win->i_height =
1414 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1416 p_vout->p_sys->p_win->i_x = 0;
1417 p_vout->p_sys->p_win->i_y = 0;
1419 #ifdef HAVE_XINERAMA
1420 if( XineramaQueryExtension( p_vout->p_sys->p_display, &i_d1, &i_d2 ) &&
1421 XineramaIsActive( p_vout->p_sys->p_display ) )
1423 XineramaScreenInfo *screens; /* infos for xinerama */
1426 msg_Dbg( p_vout, "Using XFree Xinerama extension");
1428 #define SCREEN p_vout->p_sys->p_win->i_screen
1430 /* Get Informations about Xinerama (num of screens) */
1431 screens = XineramaQueryScreens( p_vout->p_sys->p_display,
1435 SCREEN = config_GetInt( p_vout,
1436 MODULE_STRING "-xineramascreen" );
1438 /* just check that user has entered a good value */
1439 if( SCREEN >= i_num_screens || SCREEN < 0 )
1441 msg_Dbg( p_vout, "requested screen number invalid" );
1445 /* Get the X/Y upper left corner coordinate of the above screen */
1446 p_vout->p_sys->p_win->i_x = screens[SCREEN].x_org;
1447 p_vout->p_sys->p_win->i_y = screens[SCREEN].y_org;
1449 /* Set the Height/width to the screen resolution */
1450 p_vout->p_sys->p_win->i_width = screens[SCREEN].width;
1451 p_vout->p_sys->p_win->i_height = screens[SCREEN].height;
1460 XMoveResizeWindow( p_vout->p_sys->p_display,
1461 p_vout->p_sys->p_win->base_window,
1462 p_vout->p_sys->p_win->i_x,
1463 p_vout->p_sys->p_win->i_y,
1464 p_vout->p_sys->p_win->i_width,
1465 p_vout->p_sys->p_win->i_height );
1469 msg_Dbg( p_vout, "leaving fullscreen mode" );
1470 DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
1471 p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
1473 XMapWindow( p_vout->p_sys->p_display,
1474 p_vout->p_sys->p_win->base_window);
1477 /* Unfortunately, using XSync() here is not enough to ensure the
1478 * window has already been mapped because the XMapWindow() request
1479 * has not necessarily been sent directly to our window (remember,
1480 * the call is first redirected to the window manager) */
1483 XWindowEvent( p_vout->p_sys->p_display,
1484 p_vout->p_sys->p_win->base_window,
1485 StructureNotifyMask, &xevent );
1486 } while( xevent.type != MapNotify );
1488 /* Be careful, this can generate a BadMatch error if the window is not
1489 * already mapped by the server (see above) */
1490 XSetInputFocus(p_vout->p_sys->p_display,
1491 p_vout->p_sys->p_win->base_window,
1495 /* signal that the size needs to be updated */
1496 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1499 /*****************************************************************************
1500 * EnableXScreenSaver: enable screen saver
1501 *****************************************************************************
1502 * This function enables the screen saver on a display after it has been
1503 * disabled by XDisableScreenSaver.
1504 * FIXME: what happens if multiple vlc sessions are running at the same
1506 *****************************************************************************/
1507 static void EnableXScreenSaver( vout_thread_t *p_vout )
1509 #ifdef DPMSINFO_IN_DPMS_H
1513 if( p_vout->p_sys->i_ss_timeout )
1515 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1516 p_vout->p_sys->i_ss_interval,
1517 p_vout->p_sys->i_ss_blanking,
1518 p_vout->p_sys->i_ss_exposure );
1521 /* Restore DPMS settings */
1522 #ifdef DPMSINFO_IN_DPMS_H
1523 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1525 if( p_vout->p_sys->b_ss_dpms )
1527 DPMSEnable( p_vout->p_sys->p_display );
1533 /*****************************************************************************
1534 * DisableXScreenSaver: disable screen saver
1535 *****************************************************************************
1536 * See XEnableXScreenSaver
1537 *****************************************************************************/
1538 static void DisableXScreenSaver( vout_thread_t *p_vout )
1540 #ifdef DPMSINFO_IN_DPMS_H
1544 /* Save screen saver informations */
1545 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1546 &p_vout->p_sys->i_ss_interval,
1547 &p_vout->p_sys->i_ss_blanking,
1548 &p_vout->p_sys->i_ss_exposure );
1550 /* Disable screen saver */
1551 if( p_vout->p_sys->i_ss_timeout )
1553 XSetScreenSaver( p_vout->p_sys->p_display, 0,
1554 p_vout->p_sys->i_ss_interval,
1555 p_vout->p_sys->i_ss_blanking,
1556 p_vout->p_sys->i_ss_exposure );
1560 #ifdef DPMSINFO_IN_DPMS_H
1561 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1564 /* Save DPMS current state */
1565 DPMSInfo( p_vout->p_sys->p_display, &unused,
1566 &p_vout->p_sys->b_ss_dpms );
1567 DPMSDisable( p_vout->p_sys->p_display );
1572 /*****************************************************************************
1573 * CreateCursor: create a blank mouse pointer
1574 *****************************************************************************/
1575 static void CreateCursor( vout_thread_t *p_vout )
1577 XColor cursor_color;
1579 p_vout->p_sys->cursor_pixmap =
1580 XCreatePixmap( p_vout->p_sys->p_display,
1581 DefaultRootWindow( p_vout->p_sys->p_display ),
1584 XParseColor( p_vout->p_sys->p_display,
1585 XCreateColormap( p_vout->p_sys->p_display,
1587 p_vout->p_sys->p_display ),
1589 p_vout->p_sys->p_display,
1590 p_vout->p_sys->i_screen ),
1592 "black", &cursor_color );
1594 p_vout->p_sys->blank_cursor =
1595 XCreatePixmapCursor( p_vout->p_sys->p_display,
1596 p_vout->p_sys->cursor_pixmap,
1597 p_vout->p_sys->cursor_pixmap,
1598 &cursor_color, &cursor_color, 1, 1 );
1601 /*****************************************************************************
1602 * DestroyCursor: destroy the blank mouse pointer
1603 *****************************************************************************/
1604 static void DestroyCursor( vout_thread_t *p_vout )
1606 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
1609 /*****************************************************************************
1610 * ToggleCursor: hide or show the mouse pointer
1611 *****************************************************************************
1612 * This function hides the X pointer if it is visible by setting the pointer
1613 * sprite to a blank one. To show it again, we disable the sprite.
1614 *****************************************************************************/
1615 static void ToggleCursor( vout_thread_t *p_vout )
1617 if( p_vout->p_sys->b_mouse_pointer_visible )
1619 XDefineCursor( p_vout->p_sys->p_display,
1620 p_vout->p_sys->p_win->base_window,
1621 p_vout->p_sys->blank_cursor );
1622 p_vout->p_sys->b_mouse_pointer_visible = 0;
1626 XUndefineCursor( p_vout->p_sys->p_display,
1627 p_vout->p_sys->p_win->base_window );
1628 p_vout->p_sys->b_mouse_pointer_visible = 1;
1632 #ifdef MODULE_NAME_IS_xvideo
1633 /*****************************************************************************
1634 * XVideoGetPort: get YUV12 port
1635 *****************************************************************************/
1636 static int XVideoGetPort( vout_thread_t *p_vout,
1637 vlc_fourcc_t i_chroma, vlc_fourcc_t *pi_newchroma )
1639 XvAdaptorInfo *p_adaptor;
1641 int i_adaptor, i_num_adaptors, i_requested_adaptor;
1642 int i_selected_port;
1644 switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
1649 case XvBadExtension:
1650 msg_Warn( p_vout, "XvBadExtension" );
1654 msg_Warn( p_vout, "XvBadAlloc" );
1658 msg_Warn( p_vout, "XvQueryExtension failed" );
1662 switch( XvQueryAdaptors( p_vout->p_sys->p_display,
1663 DefaultRootWindow( p_vout->p_sys->p_display ),
1664 &i_num_adaptors, &p_adaptor ) )
1669 case XvBadExtension:
1670 msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
1674 msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
1678 msg_Warn( p_vout, "XvQueryAdaptors failed" );
1682 i_selected_port = -1;
1683 i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
1685 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
1687 XvImageFormatValues *p_formats;
1688 int i_format, i_num_formats;
1691 /* If we requested an adaptor and it's not this one, we aren't
1693 if( i_requested_adaptor != -1 && i_adaptor != i_requested_adaptor )
1698 /* If the adaptor doesn't have the required properties, skip it */
1699 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
1700 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
1705 /* Check that adaptor supports our requested format... */
1706 p_formats = XvListImageFormats( p_vout->p_sys->p_display,
1707 p_adaptor[i_adaptor].base_id,
1711 i_format < i_num_formats && ( i_selected_port == -1 );
1714 /* Code removed, we can get this through xvinfo anyway */
1716 XvEncodingInfo *p_enc;
1717 int i_enc, i_num_encodings;
1718 XvAttribute *p_attr;
1719 int i_attr, i_num_attributes;
1722 /* If this is not the format we want, or at least a
1723 * similar one, forget it */
1724 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
1729 /* Look for the first available port supporting this format */
1730 for( i_port = p_adaptor[i_adaptor].base_id;
1731 ( i_port < (int)(p_adaptor[i_adaptor].base_id
1732 + p_adaptor[i_adaptor].num_ports) )
1733 && ( i_selected_port == -1 );
1736 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
1739 i_selected_port = i_port;
1740 *pi_newchroma = p_formats[ i_format ].id;
1744 /* If no free port was found, forget it */
1745 if( i_selected_port == -1 )
1750 /* If we found a port, print information about it */
1751 msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
1752 i_adaptor, i_selected_port, p_formats[ i_format ].id,
1753 (char *)&p_formats[ i_format ].id,
1754 ( p_formats[ i_format ].format == XvPacked ) ?
1755 "packed" : "planar" );
1758 msg_Dbg( p_vout, " encoding list:" );
1760 if( XvQueryEncodings( p_vout->p_sys->p_display, i_selected_port,
1761 &i_num_encodings, &p_enc )
1764 msg_Dbg( p_vout, " XvQueryEncodings failed" );
1768 for( i_enc = 0; i_enc < i_num_encodings; i_enc++ )
1770 msg_Dbg( p_vout, " id=%ld, name=%s, size=%ldx%ld,"
1771 " numerator=%d, denominator=%d",
1772 p_enc[i_enc].encoding_id, p_enc[i_enc].name,
1773 p_enc[i_enc].width, p_enc[i_enc].height,
1774 p_enc[i_enc].rate.numerator,
1775 p_enc[i_enc].rate.denominator );
1780 XvFreeEncodingInfo( p_enc );
1783 msg_Dbg( p_vout, " attribute list:" );
1784 p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
1786 &i_num_attributes );
1787 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
1789 msg_Dbg( p_vout, " name=%s, flags=[%s%s ], min=%i, max=%i",
1790 p_attr[i_attr].name,
1791 (p_attr[i_attr].flags & XvGettable) ? " get" : "",
1792 (p_attr[i_attr].flags & XvSettable) ? " set" : "",
1793 p_attr[i_attr].min_value, p_attr[i_attr].max_value );
1796 if( p_attr != NULL )
1803 if( p_formats != NULL )
1810 if( i_num_adaptors > 0 )
1812 XvFreeAdaptorInfo( p_adaptor );
1815 if( i_selected_port == -1 )
1817 int i_chroma_tmp = X112VLC_FOURCC( i_chroma );
1818 if( i_requested_adaptor == -1 )
1820 msg_Warn( p_vout, "no free XVideo port found for format "
1821 "0x%.8x (%4.4s)", i_chroma_tmp, (char*)&i_chroma_tmp );
1825 msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
1826 "XVideo port for format 0x%.8x (%4.4s)",
1827 i_requested_adaptor, i_chroma_tmp, (char*)&i_chroma_tmp );
1831 return i_selected_port;
1834 /*****************************************************************************
1835 * XVideoReleasePort: release YUV12 port
1836 *****************************************************************************/
1837 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
1839 XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
1843 /*****************************************************************************
1844 * InitDisplay: open and initialize X11 device
1845 *****************************************************************************
1846 * Create a window according to video output given size, and set other
1847 * properties according to the display properties.
1848 *****************************************************************************/
1849 static int InitDisplay( vout_thread_t *p_vout )
1851 #ifdef MODULE_NAME_IS_x11
1852 XPixmapFormatValues * p_formats; /* pixmap formats */
1853 XVisualInfo * p_xvisual; /* visuals informations */
1854 XVisualInfo xvisual_template; /* visual template */
1855 int i_count; /* array size */
1858 #ifdef HAVE_SYS_SHM_H
1859 p_vout->p_sys->b_shm = 0;
1861 if( config_GetInt( p_vout, MODULE_STRING "-shm" ) )
1864 /* FIXME: As of 2001-03-16, XFree4 for MacOS X does not support Xshm */
1866 p_vout->p_sys->b_shm =
1867 ( XShmQueryExtension( p_vout->p_sys->p_display ) == True );
1870 if( !p_vout->p_sys->b_shm )
1872 msg_Warn( p_vout, "XShm video extension is unavailable" );
1877 msg_Dbg( p_vout, "disabling XShm video extension" );
1881 msg_Warn( p_vout, "XShm video extension is unavailable" );
1885 #ifdef MODULE_NAME_IS_xvideo
1886 /* XXX The brightness and contrast values should be read from environment
1887 * XXX variables... */
1889 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
1890 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
1894 #ifdef MODULE_NAME_IS_x11
1895 /* Initialize structure */
1896 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
1898 /* Get screen depth */
1899 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
1900 p_vout->p_sys->i_screen );
1901 switch( p_vout->p_sys->i_screen_depth )
1905 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
1907 xvisual_template.screen = p_vout->p_sys->i_screen;
1908 xvisual_template.class = DirectColor;
1909 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1910 VisualScreenMask | VisualClassMask,
1911 &xvisual_template, &i_count );
1912 if( p_xvisual == NULL )
1914 msg_Err( p_vout, "no PseudoColor visual available" );
1915 return VLC_EGENERIC;
1917 p_vout->p_sys->i_bytes_per_pixel = 1;
1918 p_vout->output.pf_setpalette = SetPalette;
1925 * Screen depth is higher than 8bpp. TrueColor visual is used.
1927 xvisual_template.screen = p_vout->p_sys->i_screen;
1928 xvisual_template.class = TrueColor;
1929 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1930 VisualScreenMask | VisualClassMask,
1931 &xvisual_template, &i_count );
1932 if( p_xvisual == NULL )
1934 msg_Err( p_vout, "no TrueColor visual available" );
1935 return VLC_EGENERIC;
1938 p_vout->output.i_rmask = p_xvisual->red_mask;
1939 p_vout->output.i_gmask = p_xvisual->green_mask;
1940 p_vout->output.i_bmask = p_xvisual->blue_mask;
1942 /* There is no difference yet between 3 and 4 Bpp. The only way
1943 * to find the actual number of bytes per pixel is to list supported
1944 * pixmap formats. */
1945 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
1946 p_vout->p_sys->i_bytes_per_pixel = 0;
1948 for( ; i_count-- ; p_formats++ )
1950 /* Under XFree4.0, the list contains pixmap formats available
1951 * through all video depths ; so we have to check against current
1953 if( p_formats->depth == (int)p_vout->p_sys->i_screen_depth )
1955 if( p_formats->bits_per_pixel / 8
1956 > (int)p_vout->p_sys->i_bytes_per_pixel )
1958 p_vout->p_sys->i_bytes_per_pixel =
1959 p_formats->bits_per_pixel / 8;
1965 p_vout->p_sys->p_visual = p_xvisual->visual;
1972 #ifdef HAVE_SYS_SHM_H
1973 /*****************************************************************************
1974 * CreateShmImage: create an XImage or XvImage using shared memory extension
1975 *****************************************************************************
1976 * Prepare an XImage or XvImage for display function.
1977 * The order of the operations respects the recommandations of the mit-shm
1978 * document by J.Corbet and K.Packard. Most of the parameters were copied from
1979 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
1980 *****************************************************************************/
1981 static IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
1982 Display* p_display, EXTRA_ARGS_SHM,
1983 int i_width, int i_height )
1985 IMAGE_TYPE *p_image;
1987 /* Create XImage / XvImage */
1988 #ifdef MODULE_NAME_IS_xvideo
1989 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
1990 i_width, i_height, p_shm );
1992 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
1993 p_shm, i_width, i_height );
1995 if( p_image == NULL )
1997 msg_Err( p_vout, "image creation failed" );
2001 /* Allocate shared memory segment - 0776 set the access permission
2002 * rights (like umask), they are not yet supported by all X servers */
2003 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
2004 if( p_shm->shmid < 0 )
2006 msg_Err( p_vout, "cannot allocate shared image data (%s)",
2007 strerror( errno ) );
2008 IMAGE_FREE( p_image );
2012 /* Attach shared memory segment to process (read/write) */
2013 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2014 if(! p_shm->shmaddr )
2016 msg_Err( p_vout, "cannot attach shared memory (%s)",
2018 IMAGE_FREE( p_image );
2019 shmctl( p_shm->shmid, IPC_RMID, 0 );
2023 /* Read-only data. We won't be using XShmGetImage */
2024 p_shm->readOnly = True;
2026 /* Attach shared memory segment to X server */
2027 if( XShmAttach( p_display, p_shm ) == False )
2029 msg_Err( p_vout, "cannot attach shared memory to X server" );
2030 IMAGE_FREE( p_image );
2031 shmctl( p_shm->shmid, IPC_RMID, 0 );
2032 shmdt( p_shm->shmaddr );
2036 /* Send image to X server. This instruction is required, since having
2037 * built a Shm XImage and not using it causes an error on XCloseDisplay,
2038 * and remember NOT to use XFlush ! */
2039 XSync( p_display, False );
2042 /* Mark the shm segment to be removed when there are no more
2043 * attachements, so it is automatic on process exit or after shmdt */
2044 shmctl( p_shm->shmid, IPC_RMID, 0 );
2051 /*****************************************************************************
2052 * CreateImage: create an XImage or XvImage
2053 *****************************************************************************
2054 * Create a simple image used as a buffer.
2055 *****************************************************************************/
2056 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2057 Display *p_display, EXTRA_ARGS,
2058 int i_width, int i_height )
2060 byte_t * p_data; /* image data storage zone */
2061 IMAGE_TYPE *p_image;
2062 #ifdef MODULE_NAME_IS_x11
2063 int i_quantum; /* XImage quantum (see below) */
2064 int i_bytes_per_line;
2067 /* Allocate memory for image */
2068 #ifdef MODULE_NAME_IS_xvideo
2069 p_data = (byte_t *) malloc( i_width * i_height * i_bits_per_pixel / 8 );
2071 i_bytes_per_line = i_width * i_bytes_per_pixel;
2072 p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
2076 msg_Err( p_vout, "out of memory" );
2080 #ifdef MODULE_NAME_IS_x11
2081 /* Optimize the quantum of a scanline regarding its size - the quantum is
2082 a diviser of the number of bits between the start of two scanlines. */
2083 if( i_bytes_per_line & 0xf )
2087 else if( i_bytes_per_line & 0x10 )
2097 /* Create XImage. p_data will be automatically freed */
2098 #ifdef MODULE_NAME_IS_xvideo
2099 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2100 p_data, i_width, i_height );
2102 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2103 p_data, i_width, i_height, i_quantum, 0 );
2105 if( p_image == NULL )
2107 msg_Err( p_vout, "XCreateImage() failed" );
2115 #ifdef MODULE_NAME_IS_x11
2116 /*****************************************************************************
2117 * SetPalette: sets an 8 bpp palette
2118 *****************************************************************************
2119 * This function sets the palette given as an argument. It does not return
2120 * anything, but could later send information on which colors it was unable
2122 *****************************************************************************/
2123 static void SetPalette( vout_thread_t *p_vout,
2124 uint16_t *red, uint16_t *green, uint16_t *blue )
2127 XColor p_colors[255];
2129 /* allocate palette */
2130 for( i = 0; i < 255; i++ )
2132 /* kludge: colors are indexed reversely because color 255 seems
2133 * to be reserved for black even if we try to set it to white */
2134 p_colors[ i ].pixel = 255 - i;
2135 p_colors[ i ].pad = 0;
2136 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
2137 p_colors[ i ].red = red[ 255 - i ];
2138 p_colors[ i ].blue = blue[ 255 - i ];
2139 p_colors[ i ].green = green[ 255 - i ];
2142 XStoreColors( p_vout->p_sys->p_display,
2143 p_vout->p_sys->colormap, p_colors, 255 );
2147 /*****************************************************************************
2148 * TestNetWMSupport: tests for Extended Window Manager Hints support
2149 *****************************************************************************/
2150 static void TestNetWMSupport( vout_thread_t *p_vout )
2152 int i_ret, i_format;
2153 unsigned long i, i_items, i_bytesafter;
2154 Atom net_wm_supported, *p_args = NULL;
2156 p_vout->p_sys->b_net_wm_state_fullscreen = VLC_FALSE;
2157 p_vout->p_sys->b_net_wm_state_above = VLC_FALSE;
2158 p_vout->p_sys->b_net_wm_state_below = VLC_FALSE;
2159 p_vout->p_sys->b_net_wm_state_stays_on_top = VLC_FALSE;
2162 XInternAtom( p_vout->p_sys->p_display, "_NET_SUPPORTED", False );
2164 i_ret = XGetWindowProperty( p_vout->p_sys->p_display,
2165 DefaultRootWindow( p_vout->p_sys->p_display ),
2167 0, 16384, False, AnyPropertyType,
2169 &i_format, &i_items, &i_bytesafter,
2170 (unsigned char **)&p_args );
2172 if( i_ret != Success || i_items == 0 ) return;
2174 msg_Dbg( p_vout, "Window manager supports NetWM" );
2176 p_vout->p_sys->net_wm_state =
2177 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE", False );
2178 p_vout->p_sys->net_wm_state_fullscreen =
2179 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_FULLSCREEN",
2181 p_vout->p_sys->net_wm_state_above =
2182 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_ABOVE", False );
2183 p_vout->p_sys->net_wm_state_below =
2184 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_BELOW", False );
2185 p_vout->p_sys->net_wm_state_stays_on_top =
2186 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_STAYS_ON_TOP",
2189 for( i = 0; i < i_items; i++ )
2191 if( p_args[i] == p_vout->p_sys->net_wm_state_fullscreen )
2194 "Window manager supports _NET_WM_STATE_FULLSCREEN" );
2195 p_vout->p_sys->b_net_wm_state_fullscreen = VLC_TRUE;
2197 else if( p_args[i] == p_vout->p_sys->net_wm_state_above )
2199 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_ABOVE" );
2200 p_vout->p_sys->b_net_wm_state_above = VLC_TRUE;
2202 else if( p_args[i] == p_vout->p_sys->net_wm_state_below )
2204 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_BELOW" );
2205 p_vout->p_sys->b_net_wm_state_below = VLC_TRUE;
2207 else if( p_args[i] == p_vout->p_sys->net_wm_state_stays_on_top )
2210 "Window manager supports _NET_WM_STATE_STAYS_ON_TOP" );
2211 p_vout->p_sys->b_net_wm_state_stays_on_top = VLC_TRUE;