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.24 2003/07/28 22:46:00 gbazin Exp $
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * David Kennedy <dkennedy@tinytoad.com>
10 * Gildas Bazin <gbazin@netcourrier.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <errno.h> /* ENOMEM */
31 #include <stdlib.h> /* free() */
32 #include <string.h> /* strerror() */
38 #ifdef HAVE_MACHINE_PARAM_H
40 # include <machine/param.h>
41 # include <sys/types.h> /* typedef ushort */
46 # include <netinet/in.h> /* BSD: struct in_addr */
50 # include <sys/shm.h> /* shmget(), shmctl() */
55 #include <X11/Xutil.h>
56 #include <X11/keysym.h>
58 # include <X11/extensions/XShm.h>
60 #ifdef DPMSINFO_IN_DPMS_H
61 # include <X11/extensions/dpms.h>
64 #ifdef MODULE_NAME_IS_xvideo
65 # include <X11/extensions/Xv.h>
66 # include <X11/extensions/Xvlib.h>
70 # include <X11/extensions/Xinerama.h>
75 /*****************************************************************************
77 *****************************************************************************/
78 int E_(Activate) ( vlc_object_t * );
79 void E_(Deactivate) ( vlc_object_t * );
81 static int InitVideo ( vout_thread_t * );
82 static void EndVideo ( vout_thread_t * );
83 static void DisplayVideo ( vout_thread_t *, picture_t * );
84 static int ManageVideo ( vout_thread_t * );
86 static int InitDisplay ( vout_thread_t * );
88 static int CreateWindow ( vout_thread_t *, x11_window_t * );
89 static void DestroyWindow ( vout_thread_t *, x11_window_t * );
91 static int NewPicture ( vout_thread_t *, picture_t * );
92 static void FreePicture ( vout_thread_t *, picture_t * );
94 static IMAGE_TYPE *CreateImage ( vout_thread_t *,
95 Display *, EXTRA_ARGS, int, int );
97 static IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
98 Display *, EXTRA_ARGS_SHM, int, int );
101 static void ToggleFullScreen ( vout_thread_t * );
103 static void EnableXScreenSaver ( vout_thread_t * );
104 static void DisableXScreenSaver ( vout_thread_t * );
106 static void CreateCursor ( vout_thread_t * );
107 static void DestroyCursor ( vout_thread_t * );
108 static void ToggleCursor ( vout_thread_t * );
110 #ifdef MODULE_NAME_IS_xvideo
111 static int XVideoGetPort ( vout_thread_t *, vlc_fourcc_t, vlc_fourcc_t * );
112 static void XVideoReleasePort( vout_thread_t *, int );
115 #ifdef MODULE_NAME_IS_x11
116 static void SetPalette ( vout_thread_t *,
117 uint16_t *, uint16_t *, uint16_t * );
120 /*****************************************************************************
121 * Activate: allocate X11 video thread output method
122 *****************************************************************************
123 * This function allocate and initialize a X11 vout method. It uses some of the
124 * vout properties to choose the window size, and change them according to the
125 * actual properties of the display.
126 *****************************************************************************/
127 int E_(Activate) ( vlc_object_t *p_this )
129 vout_thread_t *p_vout = (vout_thread_t *)p_this;
131 #ifdef MODULE_NAME_IS_xvideo
133 vlc_fourcc_t i_chroma = 0;
134 vlc_bool_t b_chroma = 0;
137 p_vout->pf_init = InitVideo;
138 p_vout->pf_end = EndVideo;
139 p_vout->pf_manage = ManageVideo;
140 p_vout->pf_render = NULL;
141 p_vout->pf_display = DisplayVideo;
143 /* Allocate structure */
144 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
145 if( p_vout->p_sys == NULL )
147 msg_Err( p_vout, "out of memory" );
151 /* Open display, using the "display" config variable or the DISPLAY
152 * environment variable */
153 psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
155 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
157 if( p_vout->p_sys->p_display == NULL ) /* error */
159 msg_Err( p_vout, "cannot open display %s",
160 XDisplayName( psz_display ) );
161 free( p_vout->p_sys );
162 if( psz_display ) free( psz_display );
165 if( psz_display ) free( psz_display );
167 /* Get a screen ID matching the XOpenDisplay return value */
168 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
170 #ifdef MODULE_NAME_IS_xvideo
171 psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
174 if( strlen( psz_chroma ) >= 4 )
176 /* Do not use direct assignment because we are not sure of the
178 memcpy(&i_chroma, psz_chroma, 4);
187 msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
188 i_chroma, (char*)&i_chroma );
192 i_chroma = p_vout->render.i_chroma;
195 /* Check that we have access to an XVideo port providing this chroma */
196 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, VLC2X11_FOURCC(i_chroma),
197 &p_vout->output.i_chroma );
198 if( p_vout->p_sys->i_xvport < 0 )
200 /* If a specific chroma format was requested, then we don't try to
201 * be cleverer than the user. He knew pretty well what he wanted. */
204 XCloseDisplay( p_vout->p_sys->p_display );
205 free( p_vout->p_sys );
209 /* It failed, but it's not completely lost ! We try to open an
210 * XVideo port for an YUY2 picture. We'll need to do an YUV
211 * conversion, but at least it has got scaling. */
212 p_vout->p_sys->i_xvport =
213 XVideoGetPort( p_vout, X11_FOURCC('Y','U','Y','2'),
214 &p_vout->output.i_chroma );
215 if( p_vout->p_sys->i_xvport < 0 )
217 /* It failed, but it's not completely lost ! We try to open an
218 * XVideo port for a simple 16bpp RGB picture. We'll need to do
219 * an YUV conversion, but at least it has got scaling. */
220 p_vout->p_sys->i_xvport =
221 XVideoGetPort( p_vout, X11_FOURCC('R','V','1','6'),
222 &p_vout->output.i_chroma );
223 if( p_vout->p_sys->i_xvport < 0 )
225 XCloseDisplay( p_vout->p_sys->p_display );
226 free( p_vout->p_sys );
231 p_vout->output.i_chroma = X112VLC_FOURCC(p_vout->output.i_chroma);
234 /* Create blank cursor (for mouse cursor autohiding) */
235 p_vout->p_sys->i_time_mouse_last_moved = mdate();
236 p_vout->p_sys->b_mouse_pointer_visible = 1;
237 CreateCursor( p_vout );
239 /* Set main window's size */
240 p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
241 p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
243 /* Spawn base window - this window will include the video output window,
244 * but also command buttons, subtitles and other indicators */
245 if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
247 msg_Err( p_vout, "cannot create X11 window" );
248 DestroyCursor( p_vout );
249 XCloseDisplay( p_vout->p_sys->p_display );
250 free( p_vout->p_sys );
254 /* Open and initialize device. */
255 if( InitDisplay( p_vout ) )
257 msg_Err( p_vout, "cannot initialize X11 display" );
258 DestroyCursor( p_vout );
259 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
260 XCloseDisplay( p_vout->p_sys->p_display );
261 free( p_vout->p_sys );
265 /* Disable screen saver */
266 DisableXScreenSaver( p_vout );
269 p_vout->p_sys->b_altfullscreen = 0;
270 p_vout->p_sys->i_time_button_last_pressed = 0;
275 /*****************************************************************************
276 * Deactivate: destroy X11 video thread output method
277 *****************************************************************************
278 * Terminate an output method created by Open
279 *****************************************************************************/
280 void E_(Deactivate) ( vlc_object_t *p_this )
282 vout_thread_t *p_vout = (vout_thread_t *)p_this;
284 /* If the fullscreen window is still open, close it */
285 if( p_vout->b_fullscreen )
287 ToggleFullScreen( p_vout );
290 /* Restore cursor if it was blanked */
291 if( !p_vout->p_sys->b_mouse_pointer_visible )
293 ToggleCursor( p_vout );
296 #ifdef MODULE_NAME_IS_x11
297 /* Destroy colormap */
298 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
300 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
303 XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
306 DestroyCursor( p_vout );
307 EnableXScreenSaver( p_vout );
308 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
310 XCloseDisplay( p_vout->p_sys->p_display );
312 /* Destroy structure */
313 free( p_vout->p_sys );
316 /*****************************************************************************
317 * InitVideo: initialize X11 video thread output method
318 *****************************************************************************
319 * This function create the XImages needed by the output thread. It is called
320 * at the beginning of the thread, but also each time the window is resized.
321 *****************************************************************************/
322 static int InitVideo( vout_thread_t *p_vout )
327 I_OUTPUTPICTURES = 0;
329 #ifdef MODULE_NAME_IS_xvideo
330 /* Initialize the output structure; we already found an XVideo port,
331 * and the corresponding chroma we will be using. Since we can
332 * arbitrary scale, stick to the coordinates and aspect. */
333 p_vout->output.i_width = p_vout->render.i_width;
334 p_vout->output.i_height = p_vout->render.i_height;
335 p_vout->output.i_aspect = p_vout->render.i_aspect;
337 switch( p_vout->output.i_chroma )
339 case VLC_FOURCC('R','V','1','5'):
340 p_vout->output.i_rmask = 0x001f;
341 p_vout->output.i_gmask = 0x07e0;
342 p_vout->output.i_bmask = 0xf800;
344 case VLC_FOURCC('R','V','1','6'):
345 p_vout->output.i_rmask = 0x001f;
346 p_vout->output.i_gmask = 0x03e0;
347 p_vout->output.i_bmask = 0x7c00;
352 /* Initialize the output structure: RGB with square pixels, whatever
353 * the input format is, since it's the only format we know */
354 switch( p_vout->p_sys->i_screen_depth )
356 case 8: /* FIXME: set the palette */
357 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
359 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
361 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
363 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
365 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
367 msg_Err( p_vout, "unknown screen depth %i",
368 p_vout->p_sys->i_screen_depth );
372 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
373 p_vout->p_sys->p_win->i_height,
375 &p_vout->output.i_width, &p_vout->output.i_height );
377 /* Assume we have square pixels */
378 p_vout->output.i_aspect = p_vout->output.i_width
379 * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
382 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
383 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
387 /* Find an empty picture slot */
388 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
390 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
392 p_pic = p_vout->p_picture + i_index;
397 /* Allocate the picture */
398 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
403 p_pic->i_status = DESTROYED_PICTURE;
404 p_pic->i_type = DIRECT_PICTURE;
406 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
414 /*****************************************************************************
415 * DisplayVideo: displays previously rendered output
416 *****************************************************************************
417 * This function sends the currently rendered image to X11 server.
418 * (The Xv extension takes care of "double-buffering".)
419 *****************************************************************************/
420 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
422 int i_width, i_height, i_x, i_y;
424 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
425 p_vout->p_sys->p_win->i_height,
426 &i_x, &i_y, &i_width, &i_height );
428 #ifdef HAVE_SYS_SHM_H
429 if( p_vout->p_sys->b_shm )
431 /* Display rendered image using shared memory extension */
432 # ifdef MODULE_NAME_IS_xvideo
433 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
434 p_vout->p_sys->p_win->video_window,
435 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
436 0 /*src_x*/, 0 /*src_y*/,
437 p_vout->output.i_width, p_vout->output.i_height,
438 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
439 False /* Don't put True here or you'll waste your CPU */ );
441 XShmPutImage( p_vout->p_sys->p_display,
442 p_vout->p_sys->p_win->video_window,
443 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
444 0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
445 p_vout->output.i_width, p_vout->output.i_height,
446 False /* Don't put True here ! */ );
450 #endif /* HAVE_SYS_SHM_H */
452 /* Use standard XPutImage -- this is gonna be slow ! */
453 #ifdef MODULE_NAME_IS_xvideo
454 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
455 p_vout->p_sys->p_win->video_window,
456 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
457 0 /*src_x*/, 0 /*src_y*/,
458 p_vout->output.i_width, p_vout->output.i_height,
459 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
461 XPutImage( p_vout->p_sys->p_display,
462 p_vout->p_sys->p_win->video_window,
463 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
464 0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
465 p_vout->output.i_width, p_vout->output.i_height );
469 /* Make sure the command is sent now - do NOT use XFlush !*/
470 XSync( p_vout->p_sys->p_display, False );
473 /*****************************************************************************
474 * ManageVideo: handle X11 events
475 *****************************************************************************
476 * This function should be called regularly by video output thread. It manages
477 * X11 events and allows window resizing. It returns a non null value on
479 *****************************************************************************/
480 static int ManageVideo( vout_thread_t *p_vout )
482 XEvent xevent; /* X11 event */
483 char i_key; /* ISO Latin-1 key */
487 /* Handle X11 events: ConfigureNotify events are parsed to know if the
488 * output window's size changed, MapNotify and UnmapNotify to know if the
489 * window is mapped (and if the display is useful), and ClientMessages
490 * to intercept window destruction requests */
492 while( XCheckWindowEvent( p_vout->p_sys->p_display,
493 p_vout->p_sys->p_win->base_window,
494 StructureNotifyMask | KeyPressMask |
495 ButtonPressMask | ButtonReleaseMask |
496 PointerMotionMask | Button1MotionMask , &xevent )
499 /* ConfigureNotify event: prepare */
500 if( xevent.type == ConfigureNotify )
502 if( (unsigned int)xevent.xconfigure.width
503 != p_vout->p_sys->p_win->i_width
504 || (unsigned int)xevent.xconfigure.height
505 != p_vout->p_sys->p_win->i_height )
507 /* Update dimensions */
508 p_vout->i_changes |= VOUT_SIZE_CHANGE;
509 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
510 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
514 else if( xevent.type == KeyPress )
516 /* We may have keys like F1 trough F12, ESC ... */
517 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
518 xevent.xkey.keycode, 0 );
519 switch( (int)x_key_symbol )
522 if( p_vout->b_fullscreen )
524 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
528 p_vout->p_vlc->b_die = 1;
533 intf_thread_t *p_intf;
534 playlist_t * p_playlist;
536 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
540 p_intf->b_menu_change = 1;
541 vlc_object_release( p_intf );
544 p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
546 if( p_playlist != NULL )
549 var_Set( p_playlist, "intf-popupmenu", val );
550 vlc_object_release( p_playlist );
555 /* input_Seek( p_vout, -5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR ); */
556 val.psz_string = "LEFT";
557 var_Set( p_vout, "key-pressed", val );
560 /* input_Seek( p_vout, 5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR ); */
561 val.psz_string = "RIGHT";
562 var_Set( p_vout, "key-pressed", val );
565 /* input_Seek( p_vout, 60, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR ); */
566 val.psz_string = "UP";
567 var_Set( p_vout, "key-pressed", val );
570 /* input_Seek( p_vout, -60, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR ); */
571 val.psz_string = "DOWN";
572 var_Set( p_vout, "key-pressed", val );
576 val.psz_string = "ENTER";
577 var_Set( p_vout, "key-pressed", val );
580 input_Seek( p_vout, 0, INPUT_SEEK_BYTES | INPUT_SEEK_SET );
583 input_Seek( p_vout, 0, INPUT_SEEK_BYTES | INPUT_SEEK_END );
586 input_Seek( p_vout, 5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
589 input_Seek( p_vout, -5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
592 input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
597 * The reason why I use this instead of XK_0 is that
598 * with XLookupString, we don't have to care about
601 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
603 /* FIXME: handle stuff here */
608 p_vout->p_vlc->b_die = 1;
612 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
623 else if( xevent.type == ButtonPress )
625 switch( ((XButtonEvent *)&xevent)->button )
628 var_Get( p_vout, "mouse-button-down", &val );
630 var_Set( p_vout, "mouse-button-down", val );
632 /* detect double-clicks */
633 if( ( ((XButtonEvent *)&xevent)->time -
634 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
636 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
639 p_vout->p_sys->i_time_button_last_pressed =
640 ((XButtonEvent *)&xevent)->time;
643 var_Get( p_vout, "mouse-button-down", &val );
645 var_Set( p_vout, "mouse-button-down", val );
649 var_Get( p_vout, "mouse-button-down", &val );
651 var_Set( p_vout, "mouse-button-down", val );
655 var_Get( p_vout, "mouse-button-down", &val );
657 var_Set( p_vout, "mouse-button-down", val );
658 input_Seek( p_vout, 15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
662 var_Get( p_vout, "mouse-button-down", &val );
664 var_Set( p_vout, "mouse-button-down", val );
665 input_Seek( p_vout, -15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
670 else if( xevent.type == ButtonRelease )
672 switch( ((XButtonEvent *)&xevent)->button )
675 var_Get( p_vout, "mouse-button-down", &val );
677 var_Set( p_vout, "mouse-button-down", val );
679 val.b_bool = VLC_TRUE;
680 var_Set( p_vout, "mouse-clicked", val );
685 playlist_t *p_playlist;
687 var_Get( p_vout, "mouse-button-down", &val );
689 var_Set( p_vout, "mouse-button-down", val );
691 p_playlist = vlc_object_find( p_vout,
694 if( p_playlist != NULL )
697 var_Get( p_playlist, "intf-show", &val );
698 val.b_bool = !val.b_bool;
699 var_Set( p_playlist, "intf-show", val );
700 vlc_object_release( p_playlist );
707 intf_thread_t *p_intf;
708 playlist_t *p_playlist;
710 var_Get( p_vout, "mouse-button-down", &val );
712 var_Set( p_vout, "mouse-button-down", val );
713 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
717 p_intf->b_menu_change = 1;
718 vlc_object_release( p_intf );
721 p_playlist = vlc_object_find( p_vout,
724 if( p_playlist != NULL )
727 var_Set( p_playlist, "intf-popupmenu", val );
728 vlc_object_release( p_playlist );
734 var_Get( p_vout, "mouse-button-down", &val );
736 var_Set( p_vout, "mouse-button-down", val );
740 var_Get( p_vout, "mouse-button-down", &val );
742 var_Set( p_vout, "mouse-button-down", val );
748 else if( xevent.type == MotionNotify )
750 int i_width, i_height, i_x, i_y;
753 /* somewhat different use for vout_PlacePicture:
754 * here the values are needed to give to mouse coordinates
755 * in the original picture space */
756 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
757 p_vout->p_sys->p_win->i_height,
758 &i_x, &i_y, &i_width, &i_height );
760 val.i_int = ( xevent.xmotion.x - i_x )
761 * p_vout->render.i_width / i_width;
762 var_Set( p_vout, "mouse-x", val );
763 val.i_int = ( xevent.xmotion.y - i_y )
764 * p_vout->render.i_height / i_height;
765 var_Set( p_vout, "mouse-y", val );
767 val.b_bool = VLC_TRUE;
768 var_Set( p_vout, "mouse-moved", val );
770 p_vout->p_sys->i_time_mouse_last_moved = mdate();
771 if( ! p_vout->p_sys->b_mouse_pointer_visible )
773 ToggleCursor( p_vout );
776 else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
777 || xevent.type == MapNotify
778 || xevent.type == UnmapNotify )
780 /* Ignore these events */
782 else /* Other events */
784 msg_Warn( p_vout, "unhandled event %d received", xevent.type );
788 /* Handle events for video output sub-window */
789 while( XCheckWindowEvent( p_vout->p_sys->p_display,
790 p_vout->p_sys->p_win->video_window,
791 ExposureMask, &xevent ) == True )
793 /* Window exposed (only handled if stream playback is paused) */
794 if( xevent.type == Expose )
796 if( ((XExposeEvent *)&xevent)->count == 0 )
798 /* (if this is the last a collection of expose events...) */
800 if( p_vout->p_vlc->p_input_bank->pp_input[0] != NULL )
802 if( PAUSE_S == p_vout->p_vlc->p_input_bank->pp_input[0]
803 ->stream.control.i_status )
805 /* XVideoDisplay( p_vout )*/;
813 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
814 * are handled - according to the man pages, the format is always 32
816 while( XCheckTypedEvent( p_vout->p_sys->p_display,
817 ClientMessage, &xevent ) )
819 if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
820 && ((Atom)xevent.xclient.data.l[0]
821 == p_vout->p_sys->p_win->wm_delete_window ) )
823 p_vout->p_vlc->b_die = 1;
830 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
834 /* Update the object variable and trigger callback */
835 val.b_bool = !p_vout->b_fullscreen;
836 var_Set( p_vout, "fullscreen", val );
838 ToggleFullScreen( p_vout );
839 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
845 * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
846 * the size flag inside the fullscreen routine)
848 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
850 int i_width, i_height, i_x, i_y;
852 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
854 #ifdef MODULE_NAME_IS_x11
855 /* We need to signal the vout thread about the size change because it
856 * is doing the rescaling */
857 p_vout->i_changes |= VOUT_SIZE_CHANGE;
860 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
861 p_vout->p_sys->p_win->i_height,
862 &i_x, &i_y, &i_width, &i_height );
864 XResizeWindow( p_vout->p_sys->p_display,
865 p_vout->p_sys->p_win->video_window, i_width, i_height );
867 XMoveWindow( p_vout->p_sys->p_display,
868 p_vout->p_sys->p_win->video_window, i_x, i_y );
871 /* Autohide Cursour */
872 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
874 /* Hide the mouse automatically */
875 if( p_vout->p_sys->b_mouse_pointer_visible )
877 ToggleCursor( p_vout );
884 /*****************************************************************************
885 * EndVideo: terminate X11 video thread output method
886 *****************************************************************************
887 * Destroy the X11 XImages created by Init. It is called at the end of
888 * the thread, but also each time the window is resized.
889 *****************************************************************************/
890 static void EndVideo( vout_thread_t *p_vout )
894 /* Free the direct buffers we allocated */
895 for( i_index = I_OUTPUTPICTURES ; i_index ; )
898 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
902 /* following functions are local */
904 /*****************************************************************************
905 * CreateWindow: open and set-up X11 main window
906 *****************************************************************************/
907 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
909 XSizeHints xsize_hints;
910 XSetWindowAttributes xwindow_attributes;
915 vlc_bool_t b_configure_notify;
916 vlc_bool_t b_map_notify;
919 long long int i_drawable;
921 /* Prepare window manager hints and properties */
922 xsize_hints.base_width = p_win->i_width;
923 xsize_hints.base_height = p_win->i_height;
924 xsize_hints.flags = PSize;
925 p_win->wm_protocols =
926 XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
927 p_win->wm_delete_window =
928 XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
930 /* Prepare window attributes */
931 xwindow_attributes.backing_store = Always; /* save the hidden part */
932 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
933 p_vout->p_sys->i_screen);
934 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
936 /* Check whether someone provided us with a window ID */
937 var_Get( p_vout->p_vlc, "drawable", &val );
938 i_drawable = p_vout->b_fullscreen ? 0 : val.i_int;
942 p_win->b_owned = VLC_TRUE;
944 /* Create the window and set hints - the window must receive
945 * ConfigureNotify events, and until it is displayed, Expose and
946 * MapNotify events. */
949 XCreateWindow( p_vout->p_sys->p_display,
950 DefaultRootWindow( p_vout->p_sys->p_display ),
952 p_win->i_width, p_win->i_height,
955 CWBackingStore | CWBackPixel | CWEventMask,
956 &xwindow_attributes );
958 if( !p_vout->b_fullscreen )
960 /* Set window manager hints and properties: size hints, command,
961 * window's name, and accepted protocols */
962 XSetWMNormalHints( p_vout->p_sys->p_display,
963 p_win->base_window, &xsize_hints );
964 XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
965 p_vout->p_vlc->ppsz_argv, p_vout->p_vlc->i_argc );
967 XStoreName( p_vout->p_sys->p_display, p_win->base_window,
968 #ifdef MODULE_NAME_IS_x11
969 VOUT_TITLE " (X11 output)"
971 VOUT_TITLE " (XVideo output)"
978 p_win->b_owned = VLC_FALSE;
979 p_win->base_window = i_drawable;
981 XChangeWindowAttributes( p_vout->p_sys->p_display,
983 CWBackingStore | CWBackPixel | CWEventMask,
984 &xwindow_attributes );
987 if( (p_win->wm_protocols == None) /* use WM_DELETE_WINDOW */
988 || (p_win->wm_delete_window == None)
989 || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
990 &p_win->wm_delete_window, 1 ) )
992 /* WM_DELETE_WINDOW is not supported by window manager */
993 msg_Warn( p_vout, "missing or bad window manager" );
996 /* Creation of a graphic context that doesn't generate a GraphicsExpose
997 * event when using functions like XCopyArea */
998 xgcvalues.graphics_exposures = False;
999 p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1001 GCGraphicsExposures, &xgcvalues );
1003 if( p_win->b_owned )
1005 /* Send orders to server, and wait until window is displayed - three
1006 * events must be received: a MapNotify event, an Expose event allowing
1007 * drawing in the window, and a ConfigureNotify to get the window
1008 * dimensions. Once those events have been received, only
1009 * ConfigureNotify events need to be received. */
1011 b_configure_notify = 0;
1013 XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1016 XNextEvent( p_vout->p_sys->p_display, &xevent);
1017 if( (xevent.type == Expose)
1018 && (xevent.xexpose.window == p_win->base_window) )
1022 else if( (xevent.type == MapNotify)
1023 && (xevent.xmap.window == p_win->base_window) )
1027 else if( (xevent.type == ConfigureNotify)
1028 && (xevent.xconfigure.window == p_win->base_window) )
1030 b_configure_notify = 1;
1031 p_win->i_width = xevent.xconfigure.width;
1032 p_win->i_height = xevent.xconfigure.height;
1034 } while( !( b_expose && b_configure_notify && b_map_notify ) );
1038 /* Get the window's geometry information */
1040 unsigned int dummy2, dummy3;
1041 XGetGeometry( p_vout->p_sys->p_display, p_win->base_window,
1042 &dummy1, &dummy2, &dummy3,
1048 /* When we don't own the window we cannot put ButtonPressMask in the list,
1049 * because according to the XSelectInput manpage, only one client at a
1050 * time can select a ButtonPress event. Programs such as Mozilla may be
1051 * already selecting this event. */
1052 if( p_win->b_owned )
1053 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1054 StructureNotifyMask | KeyPressMask |
1055 ButtonPressMask | ButtonReleaseMask |
1056 PointerMotionMask );
1058 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1059 StructureNotifyMask );
1061 #ifdef MODULE_NAME_IS_x11
1062 if( p_win->b_owned &&
1063 XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1065 /* Allocate a new palette */
1066 p_vout->p_sys->colormap =
1067 XCreateColormap( p_vout->p_sys->p_display,
1068 DefaultRootWindow( p_vout->p_sys->p_display ),
1069 DefaultVisual( p_vout->p_sys->p_display,
1070 p_vout->p_sys->i_screen ),
1073 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1074 XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1075 CWColormap, &xwindow_attributes );
1079 /* Create video output sub-window. */
1080 p_win->video_window = XCreateSimpleWindow(
1081 p_vout->p_sys->p_display,
1082 p_win->base_window, 0, 0,
1083 p_win->i_width, p_win->i_height,
1085 BlackPixel( p_vout->p_sys->p_display,
1086 p_vout->p_sys->i_screen ),
1087 WhitePixel( p_vout->p_sys->p_display,
1088 p_vout->p_sys->i_screen ) );
1090 XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1091 BlackPixel( p_vout->p_sys->p_display,
1092 p_vout->p_sys->i_screen ) );
1094 XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1095 XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1098 /* make sure the video window will be centered in the next ManageVideo() */
1099 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1101 /* If the cursor was formerly blank than blank it again */
1102 if( !p_vout->p_sys->b_mouse_pointer_visible )
1104 ToggleCursor( p_vout );
1105 ToggleCursor( p_vout );
1108 /* Do NOT use XFlush here ! */
1109 XSync( p_vout->p_sys->p_display, False );
1111 /* At this stage, the window is open, displayed, and ready to
1113 p_vout->p_sys->p_win = p_win;
1118 /*****************************************************************************
1119 * DestroyWindow: destroy the window
1120 *****************************************************************************
1122 *****************************************************************************/
1123 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1125 /* Do NOT use XFlush here ! */
1126 XSync( p_vout->p_sys->p_display, False );
1128 XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1129 XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1131 if( p_win->b_owned )
1133 XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1134 XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1138 /*****************************************************************************
1139 * NewPicture: allocate a picture
1140 *****************************************************************************
1141 * Returns 0 on success, -1 otherwise
1142 *****************************************************************************/
1143 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1145 /* We know the chroma, allocate a buffer which will be used
1146 * directly by the decoder */
1147 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1149 if( p_pic->p_sys == NULL )
1154 #ifdef HAVE_SYS_SHM_H
1155 if( p_vout->p_sys->b_shm )
1157 /* Create image using XShm extension */
1158 p_pic->p_sys->p_image =
1159 CreateShmImage( p_vout, p_vout->p_sys->p_display,
1160 # ifdef MODULE_NAME_IS_xvideo
1161 p_vout->p_sys->i_xvport, p_vout->output.i_chroma,
1163 p_vout->p_sys->p_visual,
1164 p_vout->p_sys->i_screen_depth,
1166 &p_pic->p_sys->shminfo,
1167 p_vout->output.i_width, p_vout->output.i_height );
1170 #endif /* HAVE_SYS_SHM_H */
1172 /* Create image without XShm extension */
1173 p_pic->p_sys->p_image =
1174 CreateImage( p_vout, p_vout->p_sys->p_display,
1175 #ifdef MODULE_NAME_IS_xvideo
1176 p_vout->p_sys->i_xvport, p_vout->output.i_chroma,
1178 p_vout->p_sys->p_visual,
1179 p_vout->p_sys->i_screen_depth,
1180 p_vout->p_sys->i_bytes_per_pixel,
1182 p_vout->output.i_width, p_vout->output.i_height );
1185 if( p_pic->p_sys->p_image == NULL )
1187 free( p_pic->p_sys );
1191 switch( p_vout->output.i_chroma )
1193 #ifdef MODULE_NAME_IS_xvideo
1194 case VLC_FOURCC('I','4','2','0'):
1196 p_pic->Y_PIXELS = p_pic->p_sys->p_image->data
1197 + p_pic->p_sys->p_image->offsets[0];
1198 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1199 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[0];
1200 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1201 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width;
1203 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1204 + p_pic->p_sys->p_image->offsets[1];
1205 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1206 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[1];
1207 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1208 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
1210 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1211 + p_pic->p_sys->p_image->offsets[2];
1212 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1213 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[2];
1214 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1215 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
1217 p_pic->i_planes = 3;
1220 case VLC_FOURCC('Y','V','1','2'):
1222 p_pic->Y_PIXELS = p_pic->p_sys->p_image->data
1223 + p_pic->p_sys->p_image->offsets[0];
1224 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1225 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[0];
1226 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1227 p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p[Y_PLANE].i_pitch;
1228 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width;
1230 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1231 + p_pic->p_sys->p_image->offsets[2];
1232 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1233 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[2];
1234 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1235 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
1237 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1238 + p_pic->p_sys->p_image->offsets[1];
1239 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1240 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[1];
1241 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1242 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
1244 p_pic->i_planes = 3;
1247 case VLC_FOURCC('Y','2','1','1'):
1249 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1250 + p_pic->p_sys->p_image->offsets[0];
1251 p_pic->p->i_lines = p_vout->output.i_height;
1252 /* XXX: this just looks so plain wrong... check it out ! */
1253 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0] / 4;
1254 p_pic->p->i_pixel_pitch = 4;
1255 p_pic->p->i_visible_pitch = p_vout->output.i_width * 4;
1257 p_pic->i_planes = 1;
1260 case VLC_FOURCC('Y','U','Y','2'):
1261 case VLC_FOURCC('U','Y','V','Y'):
1263 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1264 + p_pic->p_sys->p_image->offsets[0];
1265 p_pic->p->i_lines = p_vout->output.i_height;
1266 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1267 p_pic->p->i_pixel_pitch = 4;
1268 p_pic->p->i_visible_pitch = p_vout->output.i_width * 4;
1270 p_pic->i_planes = 1;
1273 case VLC_FOURCC('R','V','1','5'):
1275 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1276 + p_pic->p_sys->p_image->offsets[0];
1277 p_pic->p->i_lines = p_vout->output.i_height;
1278 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1279 p_pic->p->i_pixel_pitch = 2;
1280 p_pic->p->i_visible_pitch = p_vout->output.i_width * 2;
1282 p_pic->i_planes = 1;
1285 case VLC_FOURCC('R','V','1','6'):
1287 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1288 + p_pic->p_sys->p_image->offsets[0];
1289 p_pic->p->i_lines = p_vout->output.i_height;
1290 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1291 p_pic->p->i_pixel_pitch = 2;
1292 p_pic->p->i_visible_pitch = p_vout->output.i_width * 2;
1294 p_pic->i_planes = 1;
1298 case VLC_FOURCC('R','G','B','2'):
1300 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1301 + p_pic->p_sys->p_image->xoffset;
1302 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1303 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1304 p_pic->p->i_pixel_pitch = p_pic->p_sys->p_image->depth;
1305 p_pic->p->i_visible_pitch = p_pic->p_sys->p_image->width;
1307 p_pic->i_planes = 1;
1311 case VLC_FOURCC('R','V','1','6'):
1312 case VLC_FOURCC('R','V','1','5'):
1314 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1315 + p_pic->p_sys->p_image->xoffset;
1316 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1317 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1318 p_pic->p->i_pixel_pitch = p_pic->p_sys->p_image->depth;
1319 p_pic->p->i_visible_pitch = 2 * p_pic->p_sys->p_image->width;
1321 p_pic->i_planes = 1;
1325 case VLC_FOURCC('R','V','3','2'):
1326 case VLC_FOURCC('R','V','2','4'):
1328 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1329 + p_pic->p_sys->p_image->xoffset;
1330 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1331 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1332 p_pic->p->i_pixel_pitch = p_pic->p_sys->p_image->depth;
1333 p_pic->p->i_visible_pitch = 4 * p_pic->p_sys->p_image->width;
1335 p_pic->i_planes = 1;
1341 /* Unknown chroma, tell the guy to get lost */
1342 IMAGE_FREE( p_pic->p_sys->p_image );
1343 free( p_pic->p_sys );
1344 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1345 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1346 p_pic->i_planes = 0;
1353 /*****************************************************************************
1354 * FreePicture: destroy a picture allocated with NewPicture
1355 *****************************************************************************
1356 * Destroy XImage AND associated data. If using Shm, detach shared memory
1357 * segment from server and process, then free it. The XDestroyImage manpage
1358 * says that both the image structure _and_ the data pointed to by the
1359 * image structure are freed, so no need to free p_image->data.
1360 *****************************************************************************/
1361 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1363 /* The order of operations is correct */
1364 #ifdef HAVE_SYS_SHM_H
1365 if( p_vout->p_sys->b_shm )
1367 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
1368 IMAGE_FREE( p_pic->p_sys->p_image );
1370 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
1371 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
1373 msg_Err( p_vout, "cannot detach shared memory (%s)",
1380 IMAGE_FREE( p_pic->p_sys->p_image );
1383 /* Do NOT use XFlush here ! */
1384 XSync( p_vout->p_sys->p_display, False );
1386 free( p_pic->p_sys );
1389 /*****************************************************************************
1390 * ToggleFullScreen: Enable or disable full screen mode
1391 *****************************************************************************
1392 * This function will switch between fullscreen and window mode.
1393 *****************************************************************************/
1394 static void ToggleFullScreen ( vout_thread_t *p_vout )
1398 mwmhints_t mwmhints;
1399 XSetWindowAttributes attributes;
1401 #ifdef HAVE_XINERAMA
1405 p_vout->b_fullscreen = !p_vout->b_fullscreen;
1407 if( p_vout->b_fullscreen )
1409 msg_Dbg( p_vout, "entering fullscreen mode" );
1411 p_vout->p_sys->b_altfullscreen =
1412 config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
1414 if( p_vout->p_sys->p_win->b_owned )
1416 XUnmapWindow( p_vout->p_sys->p_display,
1417 p_vout->p_sys->p_win->base_window);
1420 p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
1422 /* fullscreen window size and position */
1423 p_vout->p_sys->p_win->i_width =
1424 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1425 p_vout->p_sys->p_win->i_height =
1426 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1428 CreateWindow( p_vout, p_vout->p_sys->p_win );
1430 /* To my knowledge there are two ways to create a borderless window.
1431 * There's the generic way which is to tell x to bypass the window
1432 * manager, but this creates problems with the focus of other
1434 * The other way is to use the motif property "_MOTIF_WM_HINTS" which
1435 * luckily seems to be supported by most window managers. */
1436 if( !p_vout->p_sys->b_altfullscreen )
1438 mwmhints.flags = MWM_HINTS_DECORATIONS;
1439 mwmhints.decorations = False;
1441 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1443 XChangeProperty( p_vout->p_sys->p_display,
1444 p_vout->p_sys->p_win->base_window,
1445 prop, prop, 32, PropModeReplace,
1446 (unsigned char *)&mwmhints,
1447 PROP_MWM_HINTS_ELEMENTS );
1451 /* brute force way to remove decorations */
1452 attributes.override_redirect = True;
1453 XChangeWindowAttributes( p_vout->p_sys->p_display,
1454 p_vout->p_sys->p_win->base_window,
1459 /* Make sure the change is effective */
1460 XReparentWindow( p_vout->p_sys->p_display,
1461 p_vout->p_sys->p_win->base_window,
1462 DefaultRootWindow( p_vout->p_sys->p_display ),
1465 /* fullscreen window size and position */
1466 p_vout->p_sys->p_win->i_width =
1467 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1468 p_vout->p_sys->p_win->i_height =
1469 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1471 p_vout->p_sys->p_win->i_x = 0;
1472 p_vout->p_sys->p_win->i_y = 0;
1474 #ifdef HAVE_XINERAMA
1475 if( XineramaQueryExtension( p_vout->p_sys->p_display, &i_d1, &i_d2 ) &&
1476 XineramaIsActive( p_vout->p_sys->p_display ) )
1478 XineramaScreenInfo *screens; /* infos for xinerama */
1481 msg_Dbg( p_vout, "Using XFree Xinerama extension");
1483 #define SCREEN p_vout->p_sys->p_win->i_screen
1485 /* Get Informations about Xinerama (num of screens) */
1486 screens = XineramaQueryScreens( p_vout->p_sys->p_display,
1490 SCREEN = config_GetInt( p_vout,
1491 MODULE_STRING "-xineramascreen" );
1493 /* just check that user has entered a good value */
1494 if( SCREEN >= i_num_screens || SCREEN < 0 )
1496 msg_Dbg( p_vout, "requested screen number invalid" );
1500 /* Get the X/Y upper left corner coordinate of the above screen */
1501 p_vout->p_sys->p_win->i_x = screens[SCREEN].x_org;
1502 p_vout->p_sys->p_win->i_y = screens[SCREEN].y_org;
1504 /* Set the Height/width to the screen resolution */
1505 p_vout->p_sys->p_win->i_width = screens[SCREEN].width;
1506 p_vout->p_sys->p_win->i_height = screens[SCREEN].height;
1515 XMoveResizeWindow( p_vout->p_sys->p_display,
1516 p_vout->p_sys->p_win->base_window,
1517 p_vout->p_sys->p_win->i_x,
1518 p_vout->p_sys->p_win->i_y,
1519 p_vout->p_sys->p_win->i_width,
1520 p_vout->p_sys->p_win->i_height );
1524 msg_Dbg( p_vout, "leaving fullscreen mode" );
1525 DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
1526 p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
1528 if( p_vout->p_sys->p_win->b_owned )
1530 XMapWindow( p_vout->p_sys->p_display,
1531 p_vout->p_sys->p_win->base_window);
1535 /* Unfortunately, using XSync() here is not enough to ensure the
1536 * window has already been mapped because the XMapWindow() request
1537 * has not necessarily been sent directly to our window (remember,
1538 * the call is first redirected to the window manager) */
1539 if( p_vout->p_sys->p_win->b_owned )
1543 XWindowEvent( p_vout->p_sys->p_display,
1544 p_vout->p_sys->p_win->base_window,
1545 StructureNotifyMask, &xevent );
1546 } while( xevent.type != MapNotify );
1550 XSync( p_vout->p_sys->p_display, False );
1553 /* Becareful, this can generate a BadMatch error if the window is not
1554 * already mapped by the server (see above) */
1555 XSetInputFocus(p_vout->p_sys->p_display,
1556 p_vout->p_sys->p_win->base_window,
1560 /* signal that the size needs to be updated */
1561 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1564 /*****************************************************************************
1565 * EnableXScreenSaver: enable screen saver
1566 *****************************************************************************
1567 * This function enables the screen saver on a display after it has been
1568 * disabled by XDisableScreenSaver.
1569 * FIXME: what happens if multiple vlc sessions are running at the same
1571 *****************************************************************************/
1572 static void EnableXScreenSaver( vout_thread_t *p_vout )
1574 #ifdef DPMSINFO_IN_DPMS_H
1578 if( p_vout->p_sys->i_ss_timeout )
1580 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1581 p_vout->p_sys->i_ss_interval,
1582 p_vout->p_sys->i_ss_blanking,
1583 p_vout->p_sys->i_ss_exposure );
1586 /* Restore DPMS settings */
1587 #ifdef DPMSINFO_IN_DPMS_H
1588 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1590 if( p_vout->p_sys->b_ss_dpms )
1592 DPMSEnable( p_vout->p_sys->p_display );
1598 /*****************************************************************************
1599 * DisableXScreenSaver: disable screen saver
1600 *****************************************************************************
1601 * See XEnableXScreenSaver
1602 *****************************************************************************/
1603 static void DisableXScreenSaver( vout_thread_t *p_vout )
1605 #ifdef DPMSINFO_IN_DPMS_H
1609 /* Save screen saver informations */
1610 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1611 &p_vout->p_sys->i_ss_interval,
1612 &p_vout->p_sys->i_ss_blanking,
1613 &p_vout->p_sys->i_ss_exposure );
1615 /* Disable screen saver */
1616 if( p_vout->p_sys->i_ss_timeout )
1618 XSetScreenSaver( p_vout->p_sys->p_display, 0,
1619 p_vout->p_sys->i_ss_interval,
1620 p_vout->p_sys->i_ss_blanking,
1621 p_vout->p_sys->i_ss_exposure );
1625 #ifdef DPMSINFO_IN_DPMS_H
1626 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1629 /* Save DPMS current state */
1630 DPMSInfo( p_vout->p_sys->p_display, &unused,
1631 &p_vout->p_sys->b_ss_dpms );
1632 DPMSDisable( p_vout->p_sys->p_display );
1637 /*****************************************************************************
1638 * CreateCursor: create a blank mouse pointer
1639 *****************************************************************************/
1640 static void CreateCursor( vout_thread_t *p_vout )
1642 XColor cursor_color;
1644 p_vout->p_sys->cursor_pixmap =
1645 XCreatePixmap( p_vout->p_sys->p_display,
1646 DefaultRootWindow( p_vout->p_sys->p_display ),
1649 XParseColor( p_vout->p_sys->p_display,
1650 XCreateColormap( p_vout->p_sys->p_display,
1652 p_vout->p_sys->p_display ),
1654 p_vout->p_sys->p_display,
1655 p_vout->p_sys->i_screen ),
1657 "black", &cursor_color );
1659 p_vout->p_sys->blank_cursor =
1660 XCreatePixmapCursor( p_vout->p_sys->p_display,
1661 p_vout->p_sys->cursor_pixmap,
1662 p_vout->p_sys->cursor_pixmap,
1663 &cursor_color, &cursor_color, 1, 1 );
1666 /*****************************************************************************
1667 * DestroyCursor: destroy the blank mouse pointer
1668 *****************************************************************************/
1669 static void DestroyCursor( vout_thread_t *p_vout )
1671 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
1674 /*****************************************************************************
1675 * ToggleCursor: hide or show the mouse pointer
1676 *****************************************************************************
1677 * This function hides the X pointer if it is visible by setting the pointer
1678 * sprite to a blank one. To show it again, we disable the sprite.
1679 *****************************************************************************/
1680 static void ToggleCursor( vout_thread_t *p_vout )
1682 if( p_vout->p_sys->b_mouse_pointer_visible )
1684 XDefineCursor( p_vout->p_sys->p_display,
1685 p_vout->p_sys->p_win->base_window,
1686 p_vout->p_sys->blank_cursor );
1687 p_vout->p_sys->b_mouse_pointer_visible = 0;
1691 XUndefineCursor( p_vout->p_sys->p_display,
1692 p_vout->p_sys->p_win->base_window );
1693 p_vout->p_sys->b_mouse_pointer_visible = 1;
1697 #ifdef MODULE_NAME_IS_xvideo
1698 /*****************************************************************************
1699 * XVideoGetPort: get YUV12 port
1700 *****************************************************************************/
1701 static int XVideoGetPort( vout_thread_t *p_vout,
1702 vlc_fourcc_t i_chroma, vlc_fourcc_t *pi_newchroma )
1704 XvAdaptorInfo *p_adaptor;
1706 int i_adaptor, i_num_adaptors, i_requested_adaptor;
1707 int i_selected_port;
1709 switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
1714 case XvBadExtension:
1715 msg_Warn( p_vout, "XvBadExtension" );
1719 msg_Warn( p_vout, "XvBadAlloc" );
1723 msg_Warn( p_vout, "XvQueryExtension failed" );
1727 switch( XvQueryAdaptors( p_vout->p_sys->p_display,
1728 DefaultRootWindow( p_vout->p_sys->p_display ),
1729 &i_num_adaptors, &p_adaptor ) )
1734 case XvBadExtension:
1735 msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
1739 msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
1743 msg_Warn( p_vout, "XvQueryAdaptors failed" );
1747 i_selected_port = -1;
1748 i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
1750 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
1752 XvImageFormatValues *p_formats;
1753 int i_format, i_num_formats;
1756 /* If we requested an adaptor and it's not this one, we aren't
1758 if( i_requested_adaptor != -1 && i_adaptor != i_requested_adaptor )
1763 /* If the adaptor doesn't have the required properties, skip it */
1764 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
1765 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
1770 /* Check that adaptor supports our requested format... */
1771 p_formats = XvListImageFormats( p_vout->p_sys->p_display,
1772 p_adaptor[i_adaptor].base_id,
1776 i_format < i_num_formats && ( i_selected_port == -1 );
1779 /* Code removed, we can get this through xvinfo anyway */
1781 XvEncodingInfo *p_enc;
1782 int i_enc, i_num_encodings;
1783 XvAttribute *p_attr;
1784 int i_attr, i_num_attributes;
1787 /* If this is not the format we want, or at least a
1788 * similar one, forget it */
1789 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
1794 /* Look for the first available port supporting this format */
1795 for( i_port = p_adaptor[i_adaptor].base_id;
1796 ( i_port < (int)(p_adaptor[i_adaptor].base_id
1797 + p_adaptor[i_adaptor].num_ports) )
1798 && ( i_selected_port == -1 );
1801 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
1804 i_selected_port = i_port;
1805 *pi_newchroma = p_formats[ i_format ].id;
1809 /* If no free port was found, forget it */
1810 if( i_selected_port == -1 )
1815 /* If we found a port, print information about it */
1816 msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
1817 i_adaptor, i_selected_port, p_formats[ i_format ].id,
1818 (char *)&p_formats[ i_format ].id,
1819 ( p_formats[ i_format ].format == XvPacked ) ?
1820 "packed" : "planar" );
1823 msg_Dbg( p_vout, " encoding list:" );
1825 if( XvQueryEncodings( p_vout->p_sys->p_display, i_selected_port,
1826 &i_num_encodings, &p_enc )
1829 msg_Dbg( p_vout, " XvQueryEncodings failed" );
1833 for( i_enc = 0; i_enc < i_num_encodings; i_enc++ )
1835 msg_Dbg( p_vout, " id=%ld, name=%s, size=%ldx%ld,"
1836 " numerator=%d, denominator=%d",
1837 p_enc[i_enc].encoding_id, p_enc[i_enc].name,
1838 p_enc[i_enc].width, p_enc[i_enc].height,
1839 p_enc[i_enc].rate.numerator,
1840 p_enc[i_enc].rate.denominator );
1845 XvFreeEncodingInfo( p_enc );
1848 msg_Dbg( p_vout, " attribute list:" );
1849 p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
1851 &i_num_attributes );
1852 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
1854 msg_Dbg( p_vout, " name=%s, flags=[%s%s ], min=%i, max=%i",
1855 p_attr[i_attr].name,
1856 (p_attr[i_attr].flags & XvGettable) ? " get" : "",
1857 (p_attr[i_attr].flags & XvSettable) ? " set" : "",
1858 p_attr[i_attr].min_value, p_attr[i_attr].max_value );
1861 if( p_attr != NULL )
1868 if( p_formats != NULL )
1875 if( i_num_adaptors > 0 )
1877 XvFreeAdaptorInfo( p_adaptor );
1880 if( i_selected_port == -1 )
1882 int i_chroma_tmp = X112VLC_FOURCC( i_chroma );
1883 if( i_requested_adaptor == -1 )
1885 msg_Warn( p_vout, "no free XVideo port found for format "
1886 "0x%.8x (%4.4s)", i_chroma_tmp, (char*)&i_chroma_tmp );
1890 msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
1891 "XVideo port for format 0x%.8x (%4.4s)",
1892 i_requested_adaptor, i_chroma_tmp, (char*)&i_chroma_tmp );
1896 return i_selected_port;
1899 /*****************************************************************************
1900 * XVideoReleasePort: release YUV12 port
1901 *****************************************************************************/
1902 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
1904 XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
1908 /*****************************************************************************
1909 * InitDisplay: open and initialize X11 device
1910 *****************************************************************************
1911 * Create a window according to video output given size, and set other
1912 * properties according to the display properties.
1913 *****************************************************************************/
1914 static int InitDisplay( vout_thread_t *p_vout )
1916 #ifdef MODULE_NAME_IS_x11
1917 XPixmapFormatValues * p_formats; /* pixmap formats */
1918 XVisualInfo * p_xvisual; /* visuals informations */
1919 XVisualInfo xvisual_template; /* visual template */
1920 int i_count; /* array size */
1923 #ifdef HAVE_SYS_SHM_H
1924 p_vout->p_sys->b_shm = 0;
1926 if( config_GetInt( p_vout, MODULE_STRING "-shm" ) )
1929 /* FIXME: As of 2001-03-16, XFree4 for MacOS X does not support Xshm */
1931 p_vout->p_sys->b_shm =
1932 ( XShmQueryExtension( p_vout->p_sys->p_display ) == True );
1935 if( !p_vout->p_sys->b_shm )
1937 msg_Warn( p_vout, "XShm video extension is unavailable" );
1942 msg_Dbg( p_vout, "disabling XShm video extension" );
1946 msg_Warn( p_vout, "XShm video extension is unavailable" );
1950 #ifdef MODULE_NAME_IS_xvideo
1951 /* XXX The brightness and contrast values should be read from environment
1952 * XXX variables... */
1954 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
1955 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
1959 #ifdef MODULE_NAME_IS_x11
1960 /* Initialize structure */
1961 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
1963 /* Get screen depth */
1964 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
1965 p_vout->p_sys->i_screen );
1966 switch( p_vout->p_sys->i_screen_depth )
1970 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
1972 xvisual_template.screen = p_vout->p_sys->i_screen;
1973 xvisual_template.class = DirectColor;
1974 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1975 VisualScreenMask | VisualClassMask,
1976 &xvisual_template, &i_count );
1977 if( p_xvisual == NULL )
1979 msg_Err( p_vout, "no PseudoColor visual available" );
1980 return VLC_EGENERIC;
1982 p_vout->p_sys->i_bytes_per_pixel = 1;
1983 p_vout->output.pf_setpalette = SetPalette;
1990 * Screen depth is higher than 8bpp. TrueColor visual is used.
1992 xvisual_template.screen = p_vout->p_sys->i_screen;
1993 xvisual_template.class = TrueColor;
1994 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1995 VisualScreenMask | VisualClassMask,
1996 &xvisual_template, &i_count );
1997 if( p_xvisual == NULL )
1999 msg_Err( p_vout, "no TrueColor visual available" );
2000 return VLC_EGENERIC;
2003 p_vout->output.i_rmask = p_xvisual->red_mask;
2004 p_vout->output.i_gmask = p_xvisual->green_mask;
2005 p_vout->output.i_bmask = p_xvisual->blue_mask;
2007 /* There is no difference yet between 3 and 4 Bpp. The only way
2008 * to find the actual number of bytes per pixel is to list supported
2009 * pixmap formats. */
2010 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
2011 p_vout->p_sys->i_bytes_per_pixel = 0;
2013 for( ; i_count-- ; p_formats++ )
2015 /* Under XFree4.0, the list contains pixmap formats available
2016 * through all video depths ; so we have to check against current
2018 if( p_formats->depth == (int)p_vout->p_sys->i_screen_depth )
2020 if( p_formats->bits_per_pixel / 8
2021 > (int)p_vout->p_sys->i_bytes_per_pixel )
2023 p_vout->p_sys->i_bytes_per_pixel =
2024 p_formats->bits_per_pixel / 8;
2030 p_vout->p_sys->p_visual = p_xvisual->visual;
2037 #ifdef HAVE_SYS_SHM_H
2038 /*****************************************************************************
2039 * CreateShmImage: create an XImage or XvImage using shared memory extension
2040 *****************************************************************************
2041 * Prepare an XImage or XvImage for display function.
2042 * The order of the operations respects the recommandations of the mit-shm
2043 * document by J.Corbet and K.Packard. Most of the parameters were copied from
2044 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
2045 *****************************************************************************/
2046 static IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
2047 Display* p_display, EXTRA_ARGS_SHM,
2048 int i_width, int i_height )
2050 IMAGE_TYPE *p_image;
2052 /* Create XImage / XvImage */
2053 #ifdef MODULE_NAME_IS_xvideo
2054 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2055 i_width, i_height, p_shm );
2057 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2058 p_shm, i_width, i_height );
2060 if( p_image == NULL )
2062 msg_Err( p_vout, "image creation failed" );
2066 /* Allocate shared memory segment - 0776 set the access permission
2067 * rights (like umask), they are not yet supported by all X servers */
2068 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
2069 if( p_shm->shmid < 0 )
2071 msg_Err( p_vout, "cannot allocate shared image data (%s)",
2072 strerror( errno ) );
2073 IMAGE_FREE( p_image );
2077 /* Attach shared memory segment to process (read/write) */
2078 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2079 if(! p_shm->shmaddr )
2081 msg_Err( p_vout, "cannot attach shared memory (%s)",
2083 IMAGE_FREE( p_image );
2084 shmctl( p_shm->shmid, IPC_RMID, 0 );
2088 /* Read-only data. We won't be using XShmGetImage */
2089 p_shm->readOnly = True;
2091 /* Attach shared memory segment to X server */
2092 if( XShmAttach( p_display, p_shm ) == False )
2094 msg_Err( p_vout, "cannot attach shared memory to X server" );
2095 IMAGE_FREE( p_image );
2096 shmctl( p_shm->shmid, IPC_RMID, 0 );
2097 shmdt( p_shm->shmaddr );
2101 /* Send image to X server. This instruction is required, since having
2102 * built a Shm XImage and not using it causes an error on XCloseDisplay,
2103 * and remember NOT to use XFlush ! */
2104 XSync( p_display, False );
2107 /* Mark the shm segment to be removed when there are no more
2108 * attachements, so it is automatic on process exit or after shmdt */
2109 shmctl( p_shm->shmid, IPC_RMID, 0 );
2116 /*****************************************************************************
2117 * CreateImage: create an XImage or XvImage
2118 *****************************************************************************
2119 * Create a simple image used as a buffer.
2120 *****************************************************************************/
2121 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2122 Display *p_display, EXTRA_ARGS,
2123 int i_width, int i_height )
2125 byte_t * p_data; /* image data storage zone */
2126 IMAGE_TYPE *p_image;
2127 #ifdef MODULE_NAME_IS_x11
2128 int i_quantum; /* XImage quantum (see below) */
2129 int i_bytes_per_line;
2132 /* Allocate memory for image */
2133 #ifdef MODULE_NAME_IS_xvideo
2134 p_data = (byte_t *) malloc( i_width * i_height * 2 ); /* XXX */
2136 i_bytes_per_line = i_width * i_bytes_per_pixel;
2137 p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
2141 msg_Err( p_vout, "out of memory" );
2145 #ifdef MODULE_NAME_IS_x11
2146 /* Optimize the quantum of a scanline regarding its size - the quantum is
2147 a diviser of the number of bits between the start of two scanlines. */
2148 if( i_bytes_per_line & 0xf )
2152 else if( i_bytes_per_line & 0x10 )
2162 /* Create XImage. p_data will be automatically freed */
2163 #ifdef MODULE_NAME_IS_xvideo
2164 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2165 p_data, i_width, i_height );
2167 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2168 p_data, i_width, i_height, i_quantum, 0 );
2170 if( p_image == NULL )
2172 msg_Err( p_vout, "XCreateImage() failed" );
2180 #ifdef MODULE_NAME_IS_x11
2181 /*****************************************************************************
2182 * SetPalette: sets an 8 bpp palette
2183 *****************************************************************************
2184 * This function sets the palette given as an argument. It does not return
2185 * anything, but could later send information on which colors it was unable
2187 *****************************************************************************/
2188 static void SetPalette( vout_thread_t *p_vout,
2189 uint16_t *red, uint16_t *green, uint16_t *blue )
2192 XColor p_colors[255];
2194 /* allocate palette */
2195 for( i = 0; i < 255; i++ )
2197 /* kludge: colors are indexed reversely because color 255 seems
2198 * to be reserved for black even if we try to set it to white */
2199 p_colors[ i ].pixel = 255 - i;
2200 p_colors[ i ].pad = 0;
2201 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
2202 p_colors[ i ].red = red[ 255 - i ];
2203 p_colors[ i ].blue = blue[ 255 - i ];
2204 p_colors[ i ].green = green[ 255 - i ];
2207 XStoreColors( p_vout->p_sys->p_display,
2208 p_vout->p_sys->colormap, p_colors, 255 );