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.8 2002/12/06 16:34:08 sam 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>
69 #include "netutils.h" /* network_ChannelJoin */
73 /*****************************************************************************
75 *****************************************************************************/
76 int E_(Activate) ( vlc_object_t * );
77 void E_(Deactivate) ( vlc_object_t * );
79 static int InitVideo ( vout_thread_t * );
80 static void EndVideo ( vout_thread_t * );
81 static void DisplayVideo ( vout_thread_t *, picture_t * );
82 static int ManageVideo ( vout_thread_t * );
84 static int InitDisplay ( vout_thread_t * );
86 static int CreateWindow ( vout_thread_t *, x11_window_t * );
87 static void DestroyWindow ( vout_thread_t *, x11_window_t * );
89 static int NewPicture ( vout_thread_t *, picture_t * );
90 static void FreePicture ( vout_thread_t *, picture_t * );
92 static IMAGE_TYPE *CreateImage ( vout_thread_t *,
93 Display *, EXTRA_ARGS, int, int );
95 static IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
96 Display *, EXTRA_ARGS_SHM, int, int );
99 static void ToggleFullScreen ( vout_thread_t * );
101 static void EnableXScreenSaver ( vout_thread_t * );
102 static void DisableXScreenSaver ( vout_thread_t * );
104 static void CreateCursor ( vout_thread_t * );
105 static void DestroyCursor ( vout_thread_t * );
106 static void ToggleCursor ( vout_thread_t * );
108 #ifdef MODULE_NAME_IS_xvideo
109 static int XVideoGetPort ( vout_thread_t *, vlc_fourcc_t, vlc_fourcc_t * );
110 static void XVideoReleasePort( vout_thread_t *, int );
113 #ifdef MODULE_NAME_IS_x11
114 static void SetPalette ( vout_thread_t *, u16 *, u16 *, u16 * );
117 /*****************************************************************************
118 * Activate: allocate X11 video thread output method
119 *****************************************************************************
120 * This function allocate and initialize a X11 vout method. It uses some of the
121 * vout properties to choose the window size, and change them according to the
122 * actual properties of the display.
123 *****************************************************************************/
124 int E_(Activate) ( vlc_object_t *p_this )
126 vout_thread_t *p_vout = (vout_thread_t *)p_this;
128 #ifdef MODULE_NAME_IS_xvideo
130 vlc_fourcc_t i_chroma = 0;
131 vlc_bool_t b_chroma = 0;
134 p_vout->pf_init = InitVideo;
135 p_vout->pf_end = EndVideo;
136 p_vout->pf_manage = ManageVideo;
137 p_vout->pf_render = NULL;
138 p_vout->pf_display = DisplayVideo;
140 /* Allocate structure */
141 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
142 if( p_vout->p_sys == NULL )
144 msg_Err( p_vout, "out of memory" );
148 /* Open display, using the "display" config variable or the DISPLAY
149 * environment variable */
150 psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
152 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
154 if( p_vout->p_sys->p_display == NULL ) /* error */
156 msg_Err( p_vout, "cannot open display %s",
157 XDisplayName( psz_display ) );
158 free( p_vout->p_sys );
159 if( psz_display ) free( psz_display );
162 if( psz_display ) free( psz_display );
164 /* Get a screen ID matching the XOpenDisplay return value */
165 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
167 #ifdef MODULE_NAME_IS_xvideo
168 psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
171 if( strlen( psz_chroma ) >= 4 )
173 i_chroma = (unsigned char)psz_chroma[0] << 0;
174 i_chroma |= (unsigned char)psz_chroma[1] << 8;
175 i_chroma |= (unsigned char)psz_chroma[2] << 16;
176 i_chroma |= (unsigned char)psz_chroma[3] << 24;
186 msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
187 i_chroma, (char*)&i_chroma );
191 i_chroma = p_vout->render.i_chroma;
194 /* Check that we have access to an XVideo port providing this chroma */
195 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, i_chroma,
196 &p_vout->output.i_chroma );
197 if( p_vout->p_sys->i_xvport < 0 )
199 /* If a specific chroma format was requested, then we don't try to
200 * be cleverer than the user. He knew pretty well what he wanted. */
203 XCloseDisplay( p_vout->p_sys->p_display );
204 free( p_vout->p_sys );
208 /* It failed, but it's not completely lost ! We try to open an
209 * XVideo port for an YUY2 picture. We'll need to do an YUV
210 * conversion, but at least it has got scaling. */
211 p_vout->p_sys->i_xvport =
212 XVideoGetPort( p_vout, VLC_FOURCC('Y','U','Y','2'),
213 &p_vout->output.i_chroma );
214 if( p_vout->p_sys->i_xvport < 0 )
216 /* It failed, but it's not completely lost ! We try to open an
217 * XVideo port for a simple 16bpp RGB picture. We'll need to do
218 * an YUV conversion, but at least it has got scaling. */
219 p_vout->p_sys->i_xvport =
220 XVideoGetPort( p_vout, VLC_FOURCC('R','V','1','6'),
221 &p_vout->output.i_chroma );
222 if( p_vout->p_sys->i_xvport < 0 )
224 XCloseDisplay( p_vout->p_sys->p_display );
225 free( p_vout->p_sys );
232 /* Create blank cursor (for mouse cursor autohiding) */
233 p_vout->p_sys->i_time_mouse_last_moved = mdate();
234 p_vout->p_sys->b_mouse_pointer_visible = 1;
235 CreateCursor( p_vout );
237 /* Set main window's size */
238 p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
239 p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
241 /* Spawn base window - this window will include the video output window,
242 * but also command buttons, subtitles and other indicators */
243 if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
245 msg_Err( p_vout, "cannot create X11 window" );
246 DestroyCursor( p_vout );
247 XCloseDisplay( p_vout->p_sys->p_display );
248 free( p_vout->p_sys );
252 /* Open and initialize device. */
253 if( InitDisplay( p_vout ) )
255 msg_Err( p_vout, "cannot initialize X11 display" );
256 DestroyCursor( p_vout );
257 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
258 XCloseDisplay( p_vout->p_sys->p_display );
259 free( p_vout->p_sys );
263 /* Disable screen saver */
264 DisableXScreenSaver( p_vout );
267 p_vout->p_sys->b_altfullscreen = 0;
268 p_vout->p_sys->i_time_button_last_pressed = 0;
273 /*****************************************************************************
274 * Deactivate: destroy X11 video thread output method
275 *****************************************************************************
276 * Terminate an output method created by Open
277 *****************************************************************************/
278 void E_(Deactivate) ( vlc_object_t *p_this )
280 vout_thread_t *p_vout = (vout_thread_t *)p_this;
282 /* If the fullscreen window is still open, close it */
283 if( p_vout->b_fullscreen )
285 ToggleFullScreen( p_vout );
288 /* Restore cursor if it was blanked */
289 if( !p_vout->p_sys->b_mouse_pointer_visible )
291 ToggleCursor( p_vout );
294 #ifdef MODULE_NAME_IS_x11
295 /* Destroy colormap */
296 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
298 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
301 XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
304 DestroyCursor( p_vout );
305 EnableXScreenSaver( p_vout );
306 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
308 XCloseDisplay( p_vout->p_sys->p_display );
310 /* Destroy structure */
311 free( p_vout->p_sys );
314 /*****************************************************************************
315 * InitVideo: initialize X11 video thread output method
316 *****************************************************************************
317 * This function create the XImages needed by the output thread. It is called
318 * at the beginning of the thread, but also each time the window is resized.
319 *****************************************************************************/
320 static int InitVideo( vout_thread_t *p_vout )
325 I_OUTPUTPICTURES = 0;
327 #ifdef MODULE_NAME_IS_xvideo
328 /* Initialize the output structure; we already found an XVideo port,
329 * and the corresponding chroma we will be using. Since we can
330 * arbitrary scale, stick to the coordinates and aspect. */
331 p_vout->output.i_width = p_vout->render.i_width;
332 p_vout->output.i_height = p_vout->render.i_height;
333 p_vout->output.i_aspect = p_vout->render.i_aspect;
335 switch( p_vout->output.i_chroma )
337 case VLC_FOURCC('R','V','1','5'):
338 p_vout->output.i_rmask = 0x001f;
339 p_vout->output.i_gmask = 0x07e0;
340 p_vout->output.i_bmask = 0xf800;
342 case VLC_FOURCC('R','V','1','6'):
343 p_vout->output.i_rmask = 0x001f;
344 p_vout->output.i_gmask = 0x03e0;
345 p_vout->output.i_bmask = 0x7c00;
350 /* Initialize the output structure: RGB with square pixels, whatever
351 * the input format is, since it's the only format we know */
352 switch( p_vout->p_sys->i_screen_depth )
354 case 8: /* FIXME: set the palette */
355 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
357 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
359 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
361 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
363 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
365 msg_Err( p_vout, "unknown screen depth %i",
366 p_vout->p_sys->i_screen_depth );
370 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
371 p_vout->p_sys->p_win->i_height,
373 &p_vout->output.i_width, &p_vout->output.i_height );
375 /* Assume we have square pixels */
376 p_vout->output.i_aspect = p_vout->output.i_width
377 * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
380 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
381 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
385 /* Find an empty picture slot */
386 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
388 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
390 p_pic = p_vout->p_picture + i_index;
395 /* Allocate the picture */
396 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
401 p_pic->i_status = DESTROYED_PICTURE;
402 p_pic->i_type = DIRECT_PICTURE;
404 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
412 /*****************************************************************************
413 * DisplayVideo: displays previously rendered output
414 *****************************************************************************
415 * This function sends the currently rendered image to X11 server.
416 * (The Xv extension takes care of "double-buffering".)
417 *****************************************************************************/
418 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
420 int i_width, i_height, i_x, i_y;
422 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
423 p_vout->p_sys->p_win->i_height,
424 &i_x, &i_y, &i_width, &i_height );
426 #ifdef HAVE_SYS_SHM_H
427 if( p_vout->p_sys->b_shm )
429 /* Display rendered image using shared memory extension */
430 # ifdef MODULE_NAME_IS_xvideo
431 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
432 p_vout->p_sys->p_win->video_window,
433 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
434 0 /*src_x*/, 0 /*src_y*/,
435 p_vout->output.i_width, p_vout->output.i_height,
436 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
437 False /* Don't put True here or you'll waste your CPU */ );
439 XShmPutImage( p_vout->p_sys->p_display,
440 p_vout->p_sys->p_win->video_window,
441 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
442 0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
443 p_vout->output.i_width, p_vout->output.i_height,
444 False /* Don't put True here ! */ );
448 #endif /* HAVE_SYS_SHM_H */
450 /* Use standard XPutImage -- this is gonna be slow ! */
451 #ifdef MODULE_NAME_IS_xvideo
452 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
453 p_vout->p_sys->p_win->video_window,
454 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
455 0 /*src_x*/, 0 /*src_y*/,
456 p_vout->output.i_width, p_vout->output.i_height,
457 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
459 XPutImage( p_vout->p_sys->p_display,
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*/, 0 /*dest_x*/, 0 /*dest_y*/,
463 p_vout->output.i_width, p_vout->output.i_height );
467 /* Make sure the command is sent now - do NOT use XFlush !*/
468 XSync( p_vout->p_sys->p_display, False );
471 /*****************************************************************************
472 * ManageVideo: handle X11 events
473 *****************************************************************************
474 * This function should be called regularly by video output thread. It manages
475 * X11 events and allows window resizing. It returns a non null value on
477 *****************************************************************************/
478 static int ManageVideo( vout_thread_t *p_vout )
480 XEvent xevent; /* X11 event */
481 char i_key; /* ISO Latin-1 key */
485 /* Handle X11 events: ConfigureNotify events are parsed to know if the
486 * output window's size changed, MapNotify and UnmapNotify to know if the
487 * window is mapped (and if the display is useful), and ClientMessages
488 * to intercept window destruction requests */
490 while( XCheckWindowEvent( p_vout->p_sys->p_display,
491 p_vout->p_sys->p_win->base_window,
492 StructureNotifyMask | KeyPressMask |
493 ButtonPressMask | ButtonReleaseMask |
494 PointerMotionMask | Button1MotionMask , &xevent )
497 /* ConfigureNotify event: prepare */
498 if( xevent.type == ConfigureNotify )
500 if( (unsigned int)xevent.xconfigure.width
501 != p_vout->p_sys->p_win->i_width
502 || (unsigned int)xevent.xconfigure.height
503 != p_vout->p_sys->p_win->i_height )
505 /* Update dimensions */
506 p_vout->i_changes |= VOUT_SIZE_CHANGE;
507 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
508 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
512 else if( xevent.type == KeyPress )
514 /* We may have keys like F1 trough F12, ESC ... */
515 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
516 xevent.xkey.keycode, 0 );
517 switch( (int)x_key_symbol )
520 if( p_vout->b_fullscreen )
522 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
526 p_vout->p_vlc->b_die = 1;
531 intf_thread_t *p_intf;
532 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
536 p_intf->b_menu_change = 1;
537 vlc_object_release( p_intf );
542 input_Seek( p_vout, -5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
545 input_Seek( p_vout, 5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
548 input_Seek( p_vout, 60, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
551 input_Seek( p_vout, -60, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
554 input_Seek( p_vout, 0, INPUT_SEEK_BYTES | INPUT_SEEK_SET );
557 input_Seek( p_vout, 0, INPUT_SEEK_BYTES | INPUT_SEEK_END );
560 input_Seek( p_vout, 900, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
563 input_Seek( p_vout, -900, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
566 input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
569 case XK_F1: network_ChannelJoin( p_vout, 1 ); break;
570 case XK_F2: network_ChannelJoin( p_vout, 2 ); break;
571 case XK_F3: network_ChannelJoin( p_vout, 3 ); break;
572 case XK_F4: network_ChannelJoin( p_vout, 4 ); break;
573 case XK_F5: network_ChannelJoin( p_vout, 5 ); break;
574 case XK_F6: network_ChannelJoin( p_vout, 6 ); break;
575 case XK_F7: network_ChannelJoin( p_vout, 7 ); break;
576 case XK_F8: network_ChannelJoin( p_vout, 8 ); break;
577 case XK_F9: network_ChannelJoin( p_vout, 9 ); break;
578 case XK_F10: network_ChannelJoin( p_vout, 10 ); break;
579 case XK_F11: network_ChannelJoin( p_vout, 11 ); break;
580 case XK_F12: network_ChannelJoin( p_vout, 12 ); break;
584 * The reason why I use this instead of XK_0 is that
585 * with XLookupString, we don't have to care about
588 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
590 /* FIXME: handle stuff here */
595 p_vout->p_vlc->b_die = 1;
599 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
610 else if( xevent.type == ButtonPress )
612 switch( ((XButtonEvent *)&xevent)->button )
616 /* detect double-clicks */
617 if( ( ((XButtonEvent *)&xevent)->time -
618 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
620 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
623 p_vout->p_sys->i_time_button_last_pressed =
624 ((XButtonEvent *)&xevent)->time;
628 input_Seek( p_vout, 15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
632 input_Seek( p_vout, -15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
637 else if( xevent.type == ButtonRelease )
639 switch( ((XButtonEvent *)&xevent)->button )
642 val.b_bool = VLC_TRUE;
643 var_Set( p_vout, "mouse-clicked", val );
648 intf_thread_t *p_intf;
649 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
653 p_intf->b_menu_change = 1;
654 vlc_object_release( p_intf );
661 else if( xevent.type == MotionNotify )
663 int i_width, i_height, i_x, i_y;
666 /* somewhat different use for vout_PlacePicture:
667 * here the values are needed to give to mouse coordinates
668 * in the original picture space */
669 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
670 p_vout->p_sys->p_win->i_height,
671 &i_x, &i_y, &i_width, &i_height );
673 val.i_int = ( xevent.xmotion.x - i_x )
674 * p_vout->render.i_width / i_width;
675 var_Set( p_vout, "mouse-x", val );
676 val.i_int = ( xevent.xmotion.y - i_y )
677 * p_vout->render.i_height / i_height;
678 var_Set( p_vout, "mouse-y", val );
680 val.b_bool = VLC_TRUE;
681 var_Set( p_vout, "mouse-moved", val );
683 p_vout->p_sys->i_time_mouse_last_moved = mdate();
684 if( ! p_vout->p_sys->b_mouse_pointer_visible )
686 ToggleCursor( p_vout );
689 /* Reparent move -- XXX: why are we getting this ? */
690 else if( xevent.type == ReparentNotify )
697 msg_Warn( p_vout, "unhandled event %d received", xevent.type );
701 /* Handle events for video output sub-window */
702 while( XCheckWindowEvent( p_vout->p_sys->p_display,
703 p_vout->p_sys->p_win->video_window,
704 ExposureMask, &xevent ) == True )
706 /* Window exposed (only handled if stream playback is paused) */
707 if( xevent.type == Expose )
709 if( ((XExposeEvent *)&xevent)->count == 0 )
711 /* (if this is the last a collection of expose events...) */
713 if( p_vout->p_vlc->p_input_bank->pp_input[0] != NULL )
715 if( PAUSE_S == p_vout->p_vlc->p_input_bank->pp_input[0]
716 ->stream.control.i_status )
718 /* XVideoDisplay( p_vout )*/;
726 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
727 * are handled - according to the man pages, the format is always 32
729 while( XCheckTypedEvent( p_vout->p_sys->p_display,
730 ClientMessage, &xevent ) )
732 if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
733 && (xevent.xclient.data.l[0]
734 == p_vout->p_sys->p_win->wm_delete_window ) )
736 p_vout->p_vlc->b_die = 1;
743 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
745 ToggleFullScreen( p_vout );
746 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
752 * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
753 * the size flag inside the fullscreen routine)
755 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
757 int i_width, i_height, i_x, i_y;
759 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
761 msg_Dbg( p_vout, "video display resized (%dx%d)",
762 p_vout->p_sys->p_win->i_width,
763 p_vout->p_sys->p_win->i_height );
765 #ifdef MODULE_NAME_IS_x11
766 /* We need to signal the vout thread about the size change because it
767 * is doing the rescaling */
768 p_vout->i_changes |= VOUT_SIZE_CHANGE;
771 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
772 p_vout->p_sys->p_win->i_height,
773 &i_x, &i_y, &i_width, &i_height );
775 XResizeWindow( p_vout->p_sys->p_display,
776 p_vout->p_sys->p_win->video_window, i_width, i_height );
778 XMoveWindow( p_vout->p_sys->p_display,
779 p_vout->p_sys->p_win->video_window, i_x, i_y );
782 /* Autohide Cursour */
783 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
785 /* Hide the mouse automatically */
786 if( p_vout->p_sys->b_mouse_pointer_visible )
788 ToggleCursor( p_vout );
795 /*****************************************************************************
796 * EndVideo: terminate X11 video thread output method
797 *****************************************************************************
798 * Destroy the X11 XImages created by Init. It is called at the end of
799 * the thread, but also each time the window is resized.
800 *****************************************************************************/
801 static void EndVideo( vout_thread_t *p_vout )
805 /* Free the direct buffers we allocated */
806 for( i_index = I_OUTPUTPICTURES ; i_index ; )
809 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
813 /* following functions are local */
815 /*****************************************************************************
816 * CreateWindow: open and set-up X11 main window
817 *****************************************************************************/
818 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
820 XSizeHints xsize_hints;
821 XSetWindowAttributes xwindow_attributes;
826 vlc_bool_t b_configure_notify;
827 vlc_bool_t b_map_notify;
829 long long int i_drawable;
831 /* Prepare window manager hints and properties */
832 xsize_hints.base_width = p_win->i_width;
833 xsize_hints.base_height = p_win->i_height;
834 xsize_hints.flags = PSize;
835 p_win->wm_protocols =
836 XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
837 p_win->wm_delete_window =
838 XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
840 /* Prepare window attributes */
841 xwindow_attributes.backing_store = Always; /* save the hidden part */
842 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
843 p_vout->p_sys->i_screen);
844 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
846 /* Check whether someone provided us with a window ID */
847 i_drawable = p_vout->b_fullscreen ?
848 -1 : config_GetInt( p_vout, MODULE_STRING "-drawable");
850 if( i_drawable == -1 )
852 p_win->b_owned = VLC_TRUE;
854 /* Create the window and set hints - the window must receive
855 * ConfigureNotify events, and until it is displayed, Expose and
856 * MapNotify events. */
859 XCreateWindow( p_vout->p_sys->p_display,
860 DefaultRootWindow( p_vout->p_sys->p_display ),
862 p_win->i_width, p_win->i_height,
865 CWBackingStore | CWBackPixel | CWEventMask,
866 &xwindow_attributes );
868 if( !p_vout->b_fullscreen )
870 /* Set window manager hints and properties: size hints, command,
871 * window's name, and accepted protocols */
872 XSetWMNormalHints( p_vout->p_sys->p_display,
873 p_win->base_window, &xsize_hints );
874 XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
875 p_vout->p_vlc->ppsz_argv, p_vout->p_vlc->i_argc );
877 XStoreName( p_vout->p_sys->p_display, p_win->base_window,
878 #ifdef MODULE_NAME_IS_x11
879 VOUT_TITLE " (X11 output)"
881 VOUT_TITLE " (XVideo output)"
888 p_win->b_owned = VLC_FALSE;
889 p_win->base_window = i_drawable;
891 XChangeWindowAttributes( p_vout->p_sys->p_display,
893 CWBackingStore | CWBackPixel | CWEventMask,
894 &xwindow_attributes );
897 if( (p_win->wm_protocols == None) /* use WM_DELETE_WINDOW */
898 || (p_win->wm_delete_window == None)
899 || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
900 &p_win->wm_delete_window, 1 ) )
902 /* WM_DELETE_WINDOW is not supported by window manager */
903 msg_Warn( p_vout, "missing or bad window manager" );
906 /* Creation of a graphic context that doesn't generate a GraphicsExpose
907 * event when using functions like XCopyArea */
908 xgcvalues.graphics_exposures = False;
909 p_win->gc = XCreateGC( p_vout->p_sys->p_display,
911 GCGraphicsExposures, &xgcvalues );
915 /* Send orders to server, and wait until window is displayed - three
916 * events must be received: a MapNotify event, an Expose event allowing
917 * drawing in the window, and a ConfigureNotify to get the window
918 * dimensions. Once those events have been received, only
919 * ConfigureNotify events need to be received. */
921 b_configure_notify = 0;
923 XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
926 XNextEvent( p_vout->p_sys->p_display, &xevent);
927 if( (xevent.type == Expose)
928 && (xevent.xexpose.window == p_win->base_window) )
932 else if( (xevent.type == MapNotify)
933 && (xevent.xmap.window == p_win->base_window) )
937 else if( (xevent.type == ConfigureNotify)
938 && (xevent.xconfigure.window == p_win->base_window) )
940 b_configure_notify = 1;
941 p_win->i_width = xevent.xconfigure.width;
942 p_win->i_height = xevent.xconfigure.height;
944 } while( !( b_expose && b_configure_notify && b_map_notify ) );
948 /* Get the window's geometry information */
950 unsigned int dummy2, dummy3;
951 XGetGeometry( p_vout->p_sys->p_display, p_win->base_window,
952 &dummy1, &dummy2, &dummy3,
958 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
959 StructureNotifyMask | KeyPressMask |
960 ButtonPressMask | ButtonReleaseMask |
963 #ifdef MODULE_NAME_IS_x11
964 if( p_win->b_owned &&
965 XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
967 /* Allocate a new palette */
968 p_vout->p_sys->colormap =
969 XCreateColormap( p_vout->p_sys->p_display,
970 DefaultRootWindow( p_vout->p_sys->p_display ),
971 DefaultVisual( p_vout->p_sys->p_display,
972 p_vout->p_sys->i_screen ),
975 xwindow_attributes.colormap = p_vout->p_sys->colormap;
976 XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
977 CWColormap, &xwindow_attributes );
981 /* Create video output sub-window. */
982 p_win->video_window = XCreateSimpleWindow(
983 p_vout->p_sys->p_display,
984 p_win->base_window, 0, 0,
985 p_win->i_width, p_win->i_height,
987 BlackPixel( p_vout->p_sys->p_display,
988 p_vout->p_sys->i_screen ),
989 WhitePixel( p_vout->p_sys->p_display,
990 p_vout->p_sys->i_screen ) );
992 XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
993 BlackPixel( p_vout->p_sys->p_display,
994 p_vout->p_sys->i_screen ) );
996 XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
997 XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1000 /* make sure the video window will be centered in the next ManageVideo() */
1001 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1003 /* If the cursor was formerly blank than blank it again */
1004 if( !p_vout->p_sys->b_mouse_pointer_visible )
1006 ToggleCursor( p_vout );
1007 ToggleCursor( p_vout );
1010 /* Do NOT use XFlush here ! */
1011 XSync( p_vout->p_sys->p_display, False );
1013 /* At this stage, the window is open, displayed, and ready to
1015 p_vout->p_sys->p_win = p_win;
1020 /*****************************************************************************
1021 * DestroyWindow: destroy the window
1022 *****************************************************************************
1024 *****************************************************************************/
1025 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1027 /* Do NOT use XFlush here ! */
1028 XSync( p_vout->p_sys->p_display, False );
1030 XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1031 XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1033 if( p_win->b_owned )
1035 XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1036 XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1040 /*****************************************************************************
1041 * NewPicture: allocate a picture
1042 *****************************************************************************
1043 * Returns 0 on success, -1 otherwise
1044 *****************************************************************************/
1045 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1047 /* We know the chroma, allocate a buffer which will be used
1048 * directly by the decoder */
1049 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1051 if( p_pic->p_sys == NULL )
1056 #ifdef HAVE_SYS_SHM_H
1057 if( p_vout->p_sys->b_shm )
1059 /* Create image using XShm extension */
1060 p_pic->p_sys->p_image =
1061 CreateShmImage( p_vout, p_vout->p_sys->p_display,
1062 # ifdef MODULE_NAME_IS_xvideo
1063 p_vout->p_sys->i_xvport, p_vout->output.i_chroma,
1065 p_vout->p_sys->p_visual,
1066 p_vout->p_sys->i_screen_depth,
1068 &p_pic->p_sys->shminfo,
1069 p_vout->output.i_width, p_vout->output.i_height );
1072 #endif /* HAVE_SYS_SHM_H */
1074 /* Create image without XShm extension */
1075 p_pic->p_sys->p_image =
1076 CreateImage( p_vout, p_vout->p_sys->p_display,
1077 #ifdef MODULE_NAME_IS_xvideo
1078 p_vout->p_sys->i_xvport, p_vout->output.i_chroma,
1080 p_vout->p_sys->p_visual,
1081 p_vout->p_sys->i_screen_depth,
1082 p_vout->p_sys->i_bytes_per_pixel,
1084 p_vout->output.i_width, p_vout->output.i_height );
1087 if( p_pic->p_sys->p_image == NULL )
1089 free( p_pic->p_sys );
1093 switch( p_vout->output.i_chroma )
1095 #ifdef MODULE_NAME_IS_xvideo
1096 case VLC_FOURCC('I','4','2','0'):
1098 p_pic->Y_PIXELS = p_pic->p_sys->p_image->data
1099 + p_pic->p_sys->p_image->offsets[0];
1100 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1101 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[0];
1102 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1103 p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p[Y_PLANE].i_pitch;
1105 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1106 + p_pic->p_sys->p_image->offsets[1];
1107 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1108 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[1];
1109 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1110 p_pic->p[U_PLANE].i_visible_pitch = p_pic->p[U_PLANE].i_pitch;
1112 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1113 + p_pic->p_sys->p_image->offsets[2];
1114 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1115 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[2];
1116 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1117 p_pic->p[V_PLANE].i_visible_pitch = p_pic->p[V_PLANE].i_pitch;
1119 p_pic->i_planes = 3;
1122 case VLC_FOURCC('Y','V','1','2'):
1124 p_pic->Y_PIXELS = p_pic->p_sys->p_image->data
1125 + p_pic->p_sys->p_image->offsets[0];
1126 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1127 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[0];
1128 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1129 p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p[Y_PLANE].i_pitch;
1131 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1132 + p_pic->p_sys->p_image->offsets[2];
1133 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1134 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[2];
1135 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1136 p_pic->p[U_PLANE].i_visible_pitch = p_pic->p[U_PLANE].i_pitch;
1138 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1139 + p_pic->p_sys->p_image->offsets[1];
1140 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1141 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[1];
1142 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1143 p_pic->p[V_PLANE].i_visible_pitch = p_pic->p[V_PLANE].i_pitch;
1145 p_pic->i_planes = 3;
1148 case VLC_FOURCC('Y','2','1','1'):
1150 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1151 + p_pic->p_sys->p_image->offsets[0];
1152 p_pic->p->i_lines = p_vout->output.i_height;
1153 /* XXX: this just looks so plain wrong... check it out ! */
1154 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0] / 4;
1155 p_pic->p->i_pixel_pitch = 4;
1156 p_pic->p->i_visible_pitch = p_pic->p->i_pitch;
1158 p_pic->i_planes = 1;
1161 case VLC_FOURCC('Y','U','Y','2'):
1162 case VLC_FOURCC('U','Y','V','Y'):
1164 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1165 + p_pic->p_sys->p_image->offsets[0];
1166 p_pic->p->i_lines = p_vout->output.i_height;
1167 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1168 p_pic->p->i_pixel_pitch = 4;
1169 p_pic->p->i_visible_pitch = p_pic->p->i_pitch;
1171 p_pic->i_planes = 1;
1174 case VLC_FOURCC('R','V','1','5'):
1176 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1177 + p_pic->p_sys->p_image->offsets[0];
1178 p_pic->p->i_lines = p_vout->output.i_height;
1179 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1180 p_pic->p->i_pixel_pitch = 2;
1181 p_pic->p->i_visible_pitch = p_pic->p->i_pitch;
1183 p_pic->i_planes = 1;
1186 case VLC_FOURCC('R','V','1','6'):
1188 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1189 + p_pic->p_sys->p_image->offsets[0];
1190 p_pic->p->i_lines = p_vout->output.i_height;
1191 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1192 p_pic->p->i_pixel_pitch = 2;
1193 p_pic->p->i_visible_pitch = p_pic->p->i_pitch;
1195 p_pic->i_planes = 1;
1199 case VLC_FOURCC('R','G','B','2'):
1201 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1202 + p_pic->p_sys->p_image->xoffset;
1203 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1204 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1205 p_pic->p->i_pixel_pitch = p_pic->p_sys->p_image->depth;
1206 p_pic->p->i_visible_pitch = p_pic->p_sys->p_image->width;
1208 p_pic->i_planes = 1;
1212 case VLC_FOURCC('R','V','1','6'):
1213 case VLC_FOURCC('R','V','1','5'):
1215 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1216 + p_pic->p_sys->p_image->xoffset;
1217 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1218 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1219 p_pic->p->i_pixel_pitch = p_pic->p_sys->p_image->depth;
1220 p_pic->p->i_visible_pitch = 2 * p_pic->p_sys->p_image->width;
1222 p_pic->i_planes = 1;
1226 case VLC_FOURCC('R','V','3','2'):
1227 case VLC_FOURCC('R','V','2','4'):
1229 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1230 + p_pic->p_sys->p_image->xoffset;
1231 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1232 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1233 p_pic->p->i_pixel_pitch = p_pic->p_sys->p_image->depth;
1234 p_pic->p->i_visible_pitch = 4 * p_pic->p_sys->p_image->width;
1236 p_pic->i_planes = 1;
1242 /* Unknown chroma, tell the guy to get lost */
1243 IMAGE_FREE( p_pic->p_sys->p_image );
1244 free( p_pic->p_sys );
1245 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1246 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1247 p_pic->i_planes = 0;
1254 /*****************************************************************************
1255 * FreePicture: destroy a picture allocated with NewPicture
1256 *****************************************************************************
1257 * Destroy XImage AND associated data. If using Shm, detach shared memory
1258 * segment from server and process, then free it. The XDestroyImage manpage
1259 * says that both the image structure _and_ the data pointed to by the
1260 * image structure are freed, so no need to free p_image->data.
1261 *****************************************************************************/
1262 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1264 /* The order of operations is correct */
1265 #ifdef HAVE_SYS_SHM_H
1266 if( p_vout->p_sys->b_shm )
1268 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
1269 IMAGE_FREE( p_pic->p_sys->p_image );
1271 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
1272 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
1274 msg_Err( p_vout, "cannot detach shared memory (%s)",
1281 IMAGE_FREE( p_pic->p_sys->p_image );
1284 /* Do NOT use XFlush here ! */
1285 XSync( p_vout->p_sys->p_display, False );
1287 free( p_pic->p_sys );
1290 /*****************************************************************************
1291 * ToggleFullScreen: Enable or disable full screen mode
1292 *****************************************************************************
1293 * This function will switch between fullscreen and window mode.
1294 *****************************************************************************/
1295 static void ToggleFullScreen ( vout_thread_t *p_vout )
1299 mwmhints_t mwmhints;
1300 XSetWindowAttributes attributes;
1302 p_vout->b_fullscreen = !p_vout->b_fullscreen;
1304 if( p_vout->b_fullscreen )
1306 msg_Dbg( p_vout, "entering fullscreen mode" );
1308 p_vout->p_sys->b_altfullscreen =
1309 config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
1311 if( p_vout->p_sys->p_win->b_owned )
1313 XUnmapWindow( p_vout->p_sys->p_display,
1314 p_vout->p_sys->p_win->base_window);
1317 p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
1319 /* fullscreen window size and position */
1320 p_vout->p_sys->p_win->i_width =
1321 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1322 p_vout->p_sys->p_win->i_height =
1323 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1325 CreateWindow( p_vout, p_vout->p_sys->p_win );
1327 /* To my knowledge there are two ways to create a borderless window.
1328 * There's the generic way which is to tell x to bypass the window
1329 * manager, but this creates problems with the focus of other
1331 * The other way is to use the motif property "_MOTIF_WM_HINTS" which
1332 * luckily seems to be supported by most window managers. */
1333 if( !p_vout->p_sys->b_altfullscreen )
1335 mwmhints.flags = MWM_HINTS_DECORATIONS;
1336 mwmhints.decorations = False;
1338 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1340 XChangeProperty( p_vout->p_sys->p_display,
1341 p_vout->p_sys->p_win->base_window,
1342 prop, prop, 32, PropModeReplace,
1343 (unsigned char *)&mwmhints,
1344 PROP_MWM_HINTS_ELEMENTS );
1348 /* brute force way to remove decorations */
1349 attributes.override_redirect = True;
1350 XChangeWindowAttributes( p_vout->p_sys->p_display,
1351 p_vout->p_sys->p_win->base_window,
1356 /* Make sure the change is effective */
1357 XReparentWindow( p_vout->p_sys->p_display,
1358 p_vout->p_sys->p_win->base_window,
1359 DefaultRootWindow( p_vout->p_sys->p_display ),
1362 /* fullscreen window size and position */
1363 p_vout->p_sys->p_win->i_width =
1364 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1365 p_vout->p_sys->p_win->i_height =
1366 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1368 XMoveResizeWindow( p_vout->p_sys->p_display,
1369 p_vout->p_sys->p_win->base_window,
1371 p_vout->p_sys->p_win->i_width,
1372 p_vout->p_sys->p_win->i_height );
1376 msg_Dbg( p_vout, "leaving fullscreen mode" );
1377 DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
1378 p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
1380 if( p_vout->p_sys->p_win->b_owned )
1382 XMapWindow( p_vout->p_sys->p_display,
1383 p_vout->p_sys->p_win->base_window);
1387 /* Unfortunately, using XSync() here is not enough to ensure the
1388 * window has already been mapped because the XMapWindow() request
1389 * has not necessarily been sent directly to our window (remember,
1390 * the call is first redirected to the window manager) */
1391 if( p_vout->p_sys->p_win->b_owned )
1395 XWindowEvent( p_vout->p_sys->p_display,
1396 p_vout->p_sys->p_win->base_window,
1397 StructureNotifyMask, &xevent );
1398 } while( xevent.type != MapNotify );
1402 XSync( p_vout->p_sys->p_display, False );
1405 /* Becareful, this can generate a BadMatch error if the window is not
1406 * already mapped by the server (see above) */
1407 XSetInputFocus(p_vout->p_sys->p_display,
1408 p_vout->p_sys->p_win->base_window,
1412 /* signal that the size needs to be updated */
1413 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1416 /*****************************************************************************
1417 * EnableXScreenSaver: enable screen saver
1418 *****************************************************************************
1419 * This function enables the screen saver on a display after it has been
1420 * disabled by XDisableScreenSaver.
1421 * FIXME: what happens if multiple vlc sessions are running at the same
1423 *****************************************************************************/
1424 static void EnableXScreenSaver( vout_thread_t *p_vout )
1426 #ifdef DPMSINFO_IN_DPMS_H
1430 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1431 p_vout->p_sys->i_ss_interval,
1432 p_vout->p_sys->i_ss_blanking,
1433 p_vout->p_sys->i_ss_exposure );
1435 /* Restore DPMS settings */
1436 #ifdef DPMSINFO_IN_DPMS_H
1437 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1439 if( p_vout->p_sys->b_ss_dpms )
1441 DPMSEnable( p_vout->p_sys->p_display );
1447 /*****************************************************************************
1448 * DisableXScreenSaver: disable screen saver
1449 *****************************************************************************
1450 * See XEnableXScreenSaver
1451 *****************************************************************************/
1452 static void DisableXScreenSaver( vout_thread_t *p_vout )
1454 #ifdef DPMSINFO_IN_DPMS_H
1458 /* Save screen saver informations */
1459 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1460 &p_vout->p_sys->i_ss_interval,
1461 &p_vout->p_sys->i_ss_blanking,
1462 &p_vout->p_sys->i_ss_exposure );
1464 /* Disable screen saver */
1465 XSetScreenSaver( p_vout->p_sys->p_display, 0,
1466 p_vout->p_sys->i_ss_interval,
1467 p_vout->p_sys->i_ss_blanking,
1468 p_vout->p_sys->i_ss_exposure );
1471 #ifdef DPMSINFO_IN_DPMS_H
1472 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1475 /* Save DPMS current state */
1476 DPMSInfo( p_vout->p_sys->p_display, &unused,
1477 &p_vout->p_sys->b_ss_dpms );
1478 DPMSDisable( p_vout->p_sys->p_display );
1483 /*****************************************************************************
1484 * CreateCursor: create a blank mouse pointer
1485 *****************************************************************************/
1486 static void CreateCursor( vout_thread_t *p_vout )
1488 XColor cursor_color;
1490 p_vout->p_sys->cursor_pixmap =
1491 XCreatePixmap( p_vout->p_sys->p_display,
1492 DefaultRootWindow( p_vout->p_sys->p_display ),
1495 XParseColor( p_vout->p_sys->p_display,
1496 XCreateColormap( p_vout->p_sys->p_display,
1498 p_vout->p_sys->p_display ),
1500 p_vout->p_sys->p_display,
1501 p_vout->p_sys->i_screen ),
1503 "black", &cursor_color );
1505 p_vout->p_sys->blank_cursor =
1506 XCreatePixmapCursor( p_vout->p_sys->p_display,
1507 p_vout->p_sys->cursor_pixmap,
1508 p_vout->p_sys->cursor_pixmap,
1509 &cursor_color, &cursor_color, 1, 1 );
1512 /*****************************************************************************
1513 * DestroyCursor: destroy the blank mouse pointer
1514 *****************************************************************************/
1515 static void DestroyCursor( vout_thread_t *p_vout )
1517 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
1520 /*****************************************************************************
1521 * ToggleCursor: hide or show the mouse pointer
1522 *****************************************************************************
1523 * This function hides the X pointer if it is visible by setting the pointer
1524 * sprite to a blank one. To show it again, we disable the sprite.
1525 *****************************************************************************/
1526 static void ToggleCursor( vout_thread_t *p_vout )
1528 if( p_vout->p_sys->b_mouse_pointer_visible )
1530 XDefineCursor( p_vout->p_sys->p_display,
1531 p_vout->p_sys->p_win->base_window,
1532 p_vout->p_sys->blank_cursor );
1533 p_vout->p_sys->b_mouse_pointer_visible = 0;
1537 XUndefineCursor( p_vout->p_sys->p_display,
1538 p_vout->p_sys->p_win->base_window );
1539 p_vout->p_sys->b_mouse_pointer_visible = 1;
1543 #ifdef MODULE_NAME_IS_xvideo
1544 /*****************************************************************************
1545 * XVideoGetPort: get YUV12 port
1546 *****************************************************************************/
1547 static int XVideoGetPort( vout_thread_t *p_vout,
1548 vlc_fourcc_t i_chroma, vlc_fourcc_t *pi_newchroma )
1550 XvAdaptorInfo *p_adaptor;
1552 int i_adaptor, i_num_adaptors, i_requested_adaptor;
1553 int i_selected_port;
1555 switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
1560 case XvBadExtension:
1561 msg_Warn( p_vout, "XvBadExtension" );
1565 msg_Warn( p_vout, "XvBadAlloc" );
1569 msg_Warn( p_vout, "XvQueryExtension failed" );
1573 switch( XvQueryAdaptors( p_vout->p_sys->p_display,
1574 DefaultRootWindow( p_vout->p_sys->p_display ),
1575 &i_num_adaptors, &p_adaptor ) )
1580 case XvBadExtension:
1581 msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
1585 msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
1589 msg_Warn( p_vout, "XvQueryAdaptors failed" );
1593 i_selected_port = -1;
1594 i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
1596 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
1598 XvImageFormatValues *p_formats;
1599 int i_format, i_num_formats;
1602 /* If we requested an adaptor and it's not this one, we aren't
1604 if( i_requested_adaptor != -1 && i_adaptor != i_requested_adaptor )
1609 /* If the adaptor doesn't have the required properties, skip it */
1610 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
1611 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
1616 /* Check that adaptor supports our requested format... */
1617 p_formats = XvListImageFormats( p_vout->p_sys->p_display,
1618 p_adaptor[i_adaptor].base_id,
1622 i_format < i_num_formats && ( i_selected_port == -1 );
1625 /* Code removed, we can get this through xvinfo anyway */
1627 XvEncodingInfo *p_enc;
1628 int i_enc, i_num_encodings;
1629 XvAttribute *p_attr;
1630 int i_attr, i_num_attributes;
1633 /* If this is not the format we want, or at least a
1634 * similar one, forget it */
1635 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
1640 /* Look for the first available port supporting this format */
1641 for( i_port = p_adaptor[i_adaptor].base_id;
1642 ( i_port < (int)(p_adaptor[i_adaptor].base_id
1643 + p_adaptor[i_adaptor].num_ports) )
1644 && ( i_selected_port == -1 );
1647 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
1650 i_selected_port = i_port;
1651 *pi_newchroma = p_formats[ i_format ].id;
1655 /* If no free port was found, forget it */
1656 if( i_selected_port == -1 )
1661 /* If we found a port, print information about it */
1662 msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
1663 i_adaptor, i_selected_port, p_formats[ i_format ].id,
1664 (char *)&p_formats[ i_format ].id,
1665 ( p_formats[ i_format ].format == XvPacked ) ?
1666 "packed" : "planar" );
1669 msg_Dbg( p_vout, " encoding list:" );
1671 if( XvQueryEncodings( p_vout->p_sys->p_display, i_selected_port,
1672 &i_num_encodings, &p_enc )
1675 msg_Dbg( p_vout, " XvQueryEncodings failed" );
1679 for( i_enc = 0; i_enc < i_num_encodings; i_enc++ )
1681 msg_Dbg( p_vout, " id=%ld, name=%s, size=%ldx%ld,"
1682 " numerator=%d, denominator=%d",
1683 p_enc[i_enc].encoding_id, p_enc[i_enc].name,
1684 p_enc[i_enc].width, p_enc[i_enc].height,
1685 p_enc[i_enc].rate.numerator,
1686 p_enc[i_enc].rate.denominator );
1691 XvFreeEncodingInfo( p_enc );
1694 msg_Dbg( p_vout, " attribute list:" );
1695 p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
1697 &i_num_attributes );
1698 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
1700 msg_Dbg( p_vout, " name=%s, flags=[%s%s ], min=%i, max=%i",
1701 p_attr[i_attr].name,
1702 (p_attr[i_attr].flags & XvGettable) ? " get" : "",
1703 (p_attr[i_attr].flags & XvSettable) ? " set" : "",
1704 p_attr[i_attr].min_value, p_attr[i_attr].max_value );
1707 if( p_attr != NULL )
1714 if( p_formats != NULL )
1721 if( i_num_adaptors > 0 )
1723 XvFreeAdaptorInfo( p_adaptor );
1726 if( i_selected_port == -1 )
1728 if( i_requested_adaptor == -1 )
1730 msg_Warn( p_vout, "no free XVideo port found for format "
1731 "0x%.8x (%4.4s)", i_chroma, (char*)&i_chroma );
1735 msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
1736 "XVideo port for format 0x%.8x (%4.4s)",
1737 i_requested_adaptor, i_chroma, (char*)&i_chroma );
1741 return i_selected_port;
1744 /*****************************************************************************
1745 * XVideoReleasePort: release YUV12 port
1746 *****************************************************************************/
1747 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
1749 XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
1753 /*****************************************************************************
1754 * InitDisplay: open and initialize X11 device
1755 *****************************************************************************
1756 * Create a window according to video output given size, and set other
1757 * properties according to the display properties.
1758 *****************************************************************************/
1759 static int InitDisplay( vout_thread_t *p_vout )
1761 #ifdef MODULE_NAME_IS_x11
1762 XPixmapFormatValues * p_formats; /* pixmap formats */
1763 XVisualInfo * p_xvisual; /* visuals informations */
1764 XVisualInfo xvisual_template; /* visual template */
1765 int i_count; /* array size */
1768 #ifdef HAVE_SYS_SHM_H
1769 p_vout->p_sys->b_shm = 0;
1771 if( config_GetInt( p_vout, MODULE_STRING "-shm" ) )
1774 /* FIXME: As of 2001-03-16, XFree4 for MacOS X does not support Xshm */
1776 p_vout->p_sys->b_shm =
1777 ( XShmQueryExtension( p_vout->p_sys->p_display ) == True );
1780 if( !p_vout->p_sys->b_shm )
1782 msg_Warn( p_vout, "XShm video extension is unavailable" );
1787 msg_Dbg( p_vout, "disabling XShm video extension" );
1791 msg_Warn( p_vout, "XShm video extension is unavailable" );
1795 #ifdef MODULE_NAME_IS_xvideo
1796 /* XXX The brightness and contrast values should be read from environment
1797 * XXX variables... */
1799 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
1800 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
1804 #ifdef MODULE_NAME_IS_x11
1805 /* Initialize structure */
1806 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
1808 /* Get screen depth */
1809 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
1810 p_vout->p_sys->i_screen );
1811 switch( p_vout->p_sys->i_screen_depth )
1815 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
1817 xvisual_template.screen = p_vout->p_sys->i_screen;
1818 xvisual_template.class = DirectColor;
1819 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1820 VisualScreenMask | VisualClassMask,
1821 &xvisual_template, &i_count );
1822 if( p_xvisual == NULL )
1824 msg_Err( p_vout, "no PseudoColor visual available" );
1825 return VLC_EGENERIC;
1827 p_vout->p_sys->i_bytes_per_pixel = 1;
1828 p_vout->output.pf_setpalette = SetPalette;
1835 * Screen depth is higher than 8bpp. TrueColor visual is used.
1837 xvisual_template.screen = p_vout->p_sys->i_screen;
1838 xvisual_template.class = TrueColor;
1839 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1840 VisualScreenMask | VisualClassMask,
1841 &xvisual_template, &i_count );
1842 if( p_xvisual == NULL )
1844 msg_Err( p_vout, "no TrueColor visual available" );
1845 return VLC_EGENERIC;
1848 p_vout->output.i_rmask = p_xvisual->red_mask;
1849 p_vout->output.i_gmask = p_xvisual->green_mask;
1850 p_vout->output.i_bmask = p_xvisual->blue_mask;
1852 /* There is no difference yet between 3 and 4 Bpp. The only way
1853 * to find the actual number of bytes per pixel is to list supported
1854 * pixmap formats. */
1855 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
1856 p_vout->p_sys->i_bytes_per_pixel = 0;
1858 for( ; i_count-- ; p_formats++ )
1860 /* Under XFree4.0, the list contains pixmap formats available
1861 * through all video depths ; so we have to check against current
1863 if( p_formats->depth == (int)p_vout->p_sys->i_screen_depth )
1865 if( p_formats->bits_per_pixel / 8
1866 > (int)p_vout->p_sys->i_bytes_per_pixel )
1868 p_vout->p_sys->i_bytes_per_pixel =
1869 p_formats->bits_per_pixel / 8;
1875 p_vout->p_sys->p_visual = p_xvisual->visual;
1882 #ifdef HAVE_SYS_SHM_H
1883 /*****************************************************************************
1884 * CreateShmImage: create an XImage or XvImage using shared memory extension
1885 *****************************************************************************
1886 * Prepare an XImage or XvImage for display function.
1887 * The order of the operations respects the recommandations of the mit-shm
1888 * document by J.Corbet and K.Packard. Most of the parameters were copied from
1889 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
1890 *****************************************************************************/
1891 static IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
1892 Display* p_display, EXTRA_ARGS_SHM,
1893 int i_width, int i_height )
1895 IMAGE_TYPE *p_image;
1897 /* Create XImage / XvImage */
1898 #ifdef MODULE_NAME_IS_xvideo
1899 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
1900 i_width, i_height, p_shm );
1902 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
1903 p_shm, i_width, i_height );
1905 if( p_image == NULL )
1907 msg_Err( p_vout, "image creation failed" );
1911 /* Allocate shared memory segment - 0776 set the access permission
1912 * rights (like umask), they are not yet supported by all X servers */
1913 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
1914 if( p_shm->shmid < 0 )
1916 msg_Err( p_vout, "cannot allocate shared image data (%s)",
1917 strerror( errno ) );
1918 IMAGE_FREE( p_image );
1922 /* Attach shared memory segment to process (read/write) */
1923 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
1924 if(! p_shm->shmaddr )
1926 msg_Err( p_vout, "cannot attach shared memory (%s)",
1928 IMAGE_FREE( p_image );
1929 shmctl( p_shm->shmid, IPC_RMID, 0 );
1933 /* Read-only data. We won't be using XShmGetImage */
1934 p_shm->readOnly = True;
1936 /* Attach shared memory segment to X server */
1937 if( XShmAttach( p_display, p_shm ) == False )
1939 msg_Err( p_vout, "cannot attach shared memory to X server" );
1940 IMAGE_FREE( p_image );
1941 shmctl( p_shm->shmid, IPC_RMID, 0 );
1942 shmdt( p_shm->shmaddr );
1946 /* Send image to X server. This instruction is required, since having
1947 * built a Shm XImage and not using it causes an error on XCloseDisplay,
1948 * and remember NOT to use XFlush ! */
1949 XSync( p_display, False );
1952 /* Mark the shm segment to be removed when there are no more
1953 * attachements, so it is automatic on process exit or after shmdt */
1954 shmctl( p_shm->shmid, IPC_RMID, 0 );
1961 /*****************************************************************************
1962 * CreateImage: create an XImage or XvImage
1963 *****************************************************************************
1964 * Create a simple image used as a buffer.
1965 *****************************************************************************/
1966 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
1967 Display *p_display, EXTRA_ARGS,
1968 int i_width, int i_height )
1970 byte_t * p_data; /* image data storage zone */
1971 IMAGE_TYPE *p_image;
1972 #ifdef MODULE_NAME_IS_x11
1973 int i_quantum; /* XImage quantum (see below) */
1974 int i_bytes_per_line;
1977 /* Allocate memory for image */
1978 #ifdef MODULE_NAME_IS_xvideo
1979 p_data = (byte_t *) malloc( i_width * i_height * 2 ); /* XXX */
1981 i_bytes_per_line = i_width * i_bytes_per_pixel;
1982 p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
1986 msg_Err( p_vout, "out of memory" );
1990 #ifdef MODULE_NAME_IS_x11
1991 /* Optimize the quantum of a scanline regarding its size - the quantum is
1992 a diviser of the number of bits between the start of two scanlines. */
1993 if( i_bytes_per_line & 0xf )
1997 else if( i_bytes_per_line & 0x10 )
2007 /* Create XImage. p_data will be automatically freed */
2008 #ifdef MODULE_NAME_IS_xvideo
2009 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2010 p_data, i_width, i_height );
2012 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2013 p_data, i_width, i_height, i_quantum, 0 );
2015 if( p_image == NULL )
2017 msg_Err( p_vout, "XCreateImage() failed" );
2025 #ifdef MODULE_NAME_IS_x11
2026 /*****************************************************************************
2027 * SetPalette: sets an 8 bpp palette
2028 *****************************************************************************
2029 * This function sets the palette given as an argument. It does not return
2030 * anything, but could later send information on which colors it was unable
2032 *****************************************************************************/
2033 static void SetPalette( vout_thread_t *p_vout, u16 *red, u16 *green, u16 *blue )
2036 XColor p_colors[255];
2038 /* allocate palette */
2039 for( i = 0; i < 255; i++ )
2041 /* kludge: colors are indexed reversely because color 255 seems
2042 * to be reserved for black even if we try to set it to white */
2043 p_colors[ i ].pixel = 255 - i;
2044 p_colors[ i ].pad = 0;
2045 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
2046 p_colors[ i ].red = red[ 255 - i ];
2047 p_colors[ i ].blue = blue[ 255 - i ];
2048 p_colors[ i ].green = green[ 255 - i ];
2051 XStoreColors( p_vout->p_sys->p_display,
2052 p_vout->p_sys->colormap, p_colors, 255 );