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.44 2002/07/20 18:01:43 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() */
54 #include <X11/Xutil.h>
55 #include <X11/keysym.h>
57 # include <X11/extensions/XShm.h>
59 #ifdef DPMSINFO_IN_DPMS_H
60 # include <X11/extensions/dpms.h>
63 #ifdef MODULE_NAME_IS_xvideo
64 # include <X11/extensions/Xv.h>
65 # include <X11/extensions/Xvlib.h>
70 #include "netutils.h" /* network_ChannelJoin */
72 /*****************************************************************************
74 *****************************************************************************/
75 #ifdef MODULE_NAME_IS_xvideo
76 # define IMAGE_TYPE XvImage
77 # define EXTRA_ARGS int i_xvport, int i_chroma
78 # define EXTRA_ARGS_SHM int i_xvport, int i_chroma, XShmSegmentInfo *p_shm
79 # define DATA_SIZE(p) (p)->data_size
80 # define IMAGE_FREE XFree /* There is nothing like XvDestroyImage */
82 # define IMAGE_TYPE XImage
83 # define EXTRA_ARGS Visual *p_visual, int i_depth, int i_bytes_per_pixel
84 # define EXTRA_ARGS_SHM Visual *p_visual, int i_depth, XShmSegmentInfo *p_shm
85 # define DATA_SIZE(p) ((p)->bytes_per_line * (p)->height)
86 # define IMAGE_FREE XDestroyImage
89 VLC_DECLARE_STRUCT(x11_window_t)
91 /*****************************************************************************
93 *****************************************************************************/
94 static int vout_Create ( vout_thread_t * );
95 static void vout_Destroy ( vout_thread_t * );
96 static void vout_Render ( vout_thread_t *, picture_t * );
97 static void vout_Display ( vout_thread_t *, picture_t * );
98 static int vout_Manage ( vout_thread_t * );
99 static int vout_Init ( vout_thread_t * );
100 static void vout_End ( vout_thread_t * );
102 static int InitDisplay ( vout_thread_t * );
104 static int CreateWindow ( vout_thread_t *, x11_window_t * );
105 static void DestroyWindow ( vout_thread_t *, x11_window_t * );
107 static int NewPicture ( vout_thread_t *, picture_t * );
108 static void FreePicture ( vout_thread_t *, picture_t * );
110 static IMAGE_TYPE *CreateImage ( vout_thread_t *,
111 Display *, EXTRA_ARGS, int, int );
112 #ifdef HAVE_SYS_SHM_H
113 static IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
114 Display *, EXTRA_ARGS_SHM, int, int );
117 static void ToggleFullScreen ( vout_thread_t * );
119 static void EnableXScreenSaver ( vout_thread_t * );
120 static void DisableXScreenSaver ( vout_thread_t * );
122 static void CreateCursor ( vout_thread_t * );
123 static void DestroyCursor ( vout_thread_t * );
124 static void ToggleCursor ( vout_thread_t * );
126 #ifdef MODULE_NAME_IS_xvideo
127 static int XVideoGetPort ( vout_thread_t *, u32, u32 * );
128 static void XVideoReleasePort ( vout_thread_t *, int );
131 #ifdef MODULE_NAME_IS_x11
132 static void SetPalette ( vout_thread_t *, u16 *, u16 *, u16 * );
135 /*****************************************************************************
136 * x11_window_t: X11 window descriptor
137 *****************************************************************************
138 * This structure contains all the data necessary to describe an X11 window.
139 *****************************************************************************/
142 Window base_window; /* base window */
143 Window video_window; /* sub-window for displaying video */
144 GC gc; /* graphic context instance handler */
145 int i_width; /* width of main window */
146 int i_height; /* height of main window */
148 Atom wm_delete_window;
151 /*****************************************************************************
152 * vout_sys_t: video output method descriptor
153 *****************************************************************************
154 * This structure is part of the video output thread descriptor.
155 * It describes the X11 and XVideo specific properties of an output thread.
156 *****************************************************************************/
159 /* Internal settings and properties */
160 Display * p_display; /* display pointer */
162 Visual * p_visual; /* visual pointer */
163 int i_screen; /* screen number */
165 /* Our current window */
166 x11_window_t * p_win;
168 /* Our two windows */
169 x11_window_t original_window;
170 x11_window_t fullscreen_window;
172 /* X11 generic properties */
173 vlc_bool_t b_altfullscreen; /* which fullscreen method */
174 vlc_bool_t b_createwindow; /* are we the base window's owner ? */
175 #ifdef HAVE_SYS_SHM_H
176 vlc_bool_t b_shm; /* shared memory extension flag */
179 #ifdef MODULE_NAME_IS_xvideo
182 Colormap colormap; /* colormap used (8bpp only) */
185 int i_bytes_per_pixel;
186 int i_bytes_per_line;
189 /* Screen saver properties */
190 int i_ss_timeout; /* timeout */
191 int i_ss_interval; /* interval between changes */
192 int i_ss_blanking; /* blanking mode */
193 int i_ss_exposure; /* exposure mode */
194 #ifdef DPMSINFO_IN_DPMS_H
195 BOOL b_ss_dpms; /* DPMS mode */
198 /* Mouse pointer properties */
199 vlc_bool_t b_mouse_pointer_visible;
200 mtime_t i_time_mouse_last_moved; /* used to auto-hide pointer*/
201 Cursor blank_cursor; /* the hidden cursor */
202 mtime_t i_time_button_last_pressed; /* to track dbl-clicks */
203 Pixmap cursor_pixmap;
206 /*****************************************************************************
207 * picture_sys_t: direct buffer method descriptor
208 *****************************************************************************
209 * This structure is part of the picture descriptor, it describes the
210 * XVideo specific properties of a direct buffer.
211 *****************************************************************************/
214 IMAGE_TYPE * p_image;
216 #ifdef HAVE_SYS_SHM_H
217 XShmSegmentInfo shminfo; /* shared memory zone information */
221 /*****************************************************************************
222 * mwmhints_t: window manager hints
223 *****************************************************************************
224 * Fullscreen needs to be able to hide the wm decorations so we provide
225 * this structure to make it easier.
226 *****************************************************************************/
227 #define MWM_HINTS_DECORATIONS (1L << 1)
228 #define PROP_MWM_HINTS_ELEMENTS 5
229 typedef struct mwmhints_t
238 /*****************************************************************************
240 *****************************************************************************/
241 #ifdef MODULE_NAME_IS_xvideo
242 # define MAX_DIRECTBUFFERS 10
244 # define MAX_DIRECTBUFFERS 2
247 /*****************************************************************************
248 * Functions exported as capabilities. They are declared as static so that
249 * we don't pollute the namespace too much.
250 *****************************************************************************/
251 void _M( vout_getfunctions )( function_list_t * p_function_list )
253 p_function_list->functions.vout.pf_create = vout_Create;
254 p_function_list->functions.vout.pf_init = vout_Init;
255 p_function_list->functions.vout.pf_end = vout_End;
256 p_function_list->functions.vout.pf_destroy = vout_Destroy;
257 p_function_list->functions.vout.pf_manage = vout_Manage;
258 p_function_list->functions.vout.pf_render = vout_Render;
259 p_function_list->functions.vout.pf_display = vout_Display;
262 /*****************************************************************************
263 * vout_Create: allocate X11 video thread output method
264 *****************************************************************************
265 * This function allocate and initialize a X11 vout method. It uses some of the
266 * vout properties to choose the window size, and change them according to the
267 * actual properties of the display.
268 *****************************************************************************/
269 static int vout_Create( vout_thread_t *p_vout )
272 #ifdef MODULE_NAME_IS_xvideo
275 vlc_bool_t b_chroma = 0;
278 /* Allocate structure */
279 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
280 if( p_vout->p_sys == NULL )
282 msg_Err( p_vout, "out of memory" );
286 /* Open display, unsing the "display" config variable or the DISPLAY
287 * environment variable */
288 psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
290 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
292 if( p_vout->p_sys->p_display == NULL ) /* error */
294 msg_Err( p_vout, "cannot open display %s",
295 XDisplayName( psz_display ) );
296 free( p_vout->p_sys );
297 if( psz_display ) free( psz_display );
300 if( psz_display ) free( psz_display );
302 /* Get a screen ID matching the XOpenDisplay return value */
303 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
305 #ifdef MODULE_NAME_IS_xvideo
306 psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
309 if( strlen( psz_chroma ) >= 4 )
311 i_chroma = (unsigned char)psz_chroma[0] << 0;
312 i_chroma |= (unsigned char)psz_chroma[1] << 8;
313 i_chroma |= (unsigned char)psz_chroma[2] << 16;
314 i_chroma |= (unsigned char)psz_chroma[3] << 24;
324 msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
325 i_chroma, (char*)&i_chroma );
329 i_chroma = p_vout->render.i_chroma;
332 /* Check that we have access to an XVideo port providing this chroma */
333 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, i_chroma,
334 &p_vout->output.i_chroma );
335 if( p_vout->p_sys->i_xvport < 0 )
337 /* If a specific chroma format was requested, then we don't try to
338 * be cleverer than the user. He knows pretty well what he wants. */
341 XCloseDisplay( p_vout->p_sys->p_display );
342 free( p_vout->p_sys );
346 /* It failed, but it's not completely lost ! We try to open an
347 * XVideo port for an YUY2 picture. We'll need to do an YUV
348 * conversion, but at least it has got scaling. */
349 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, FOURCC_YUY2,
350 &p_vout->output.i_chroma );
351 if( p_vout->p_sys->i_xvport < 0 )
353 /* It failed, but it's not completely lost ! We try to open an
354 * XVideo port for a simple 16bpp RGB picture. We'll need to do
355 * an YUV conversion, but at least it has got scaling. */
356 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, FOURCC_RV16,
357 &p_vout->output.i_chroma );
358 if( p_vout->p_sys->i_xvport < 0 )
360 XCloseDisplay( p_vout->p_sys->p_display );
361 free( p_vout->p_sys );
368 /* Create blank cursor (for mouse cursor autohiding) */
369 p_vout->p_sys->i_time_mouse_last_moved = mdate();
370 p_vout->p_sys->b_mouse_pointer_visible = 1;
371 CreateCursor( p_vout );
373 /* Set main window's size */
374 p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
375 p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
377 /* Spawn base window - this window will include the video output window,
378 * but also command buttons, subtitles and other indicators */
379 if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
381 msg_Err( p_vout, "cannot create X11 window" );
382 DestroyCursor( p_vout );
383 XCloseDisplay( p_vout->p_sys->p_display );
384 free( p_vout->p_sys );
388 /* Open and initialize device. */
389 if( InitDisplay( p_vout ) )
391 msg_Err( p_vout, "cannot initialize X11 display" );
392 DestroyCursor( p_vout );
393 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
394 XCloseDisplay( p_vout->p_sys->p_display );
395 free( p_vout->p_sys );
399 /* Disable screen saver */
400 DisableXScreenSaver( p_vout );
403 p_vout->p_sys->b_altfullscreen = 0;
404 p_vout->p_sys->i_time_button_last_pressed = 0;
409 /*****************************************************************************
410 * vout_Destroy: destroy X11 video thread output method
411 *****************************************************************************
412 * Terminate an output method created by vout_CreateOutputMethod
413 *****************************************************************************/
414 static void vout_Destroy( vout_thread_t *p_vout )
416 /* If the fullscreen window is still open, close it */
417 if( p_vout->b_fullscreen )
419 ToggleFullScreen( p_vout );
422 /* Restore cursor if it was blanked */
423 if( !p_vout->p_sys->b_mouse_pointer_visible )
425 ToggleCursor( p_vout );
428 #ifdef MODULE_NAME_IS_x11
429 /* Destroy colormap */
430 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
432 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
435 XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
438 DestroyCursor( p_vout );
439 EnableXScreenSaver( p_vout );
440 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
442 XCloseDisplay( p_vout->p_sys->p_display );
444 /* Destroy structure */
445 free( p_vout->p_sys );
448 /*****************************************************************************
449 * vout_Init: initialize X11 video thread output method
450 *****************************************************************************
451 * This function create the XImages needed by the output thread. It is called
452 * at the beginning of the thread, but also each time the window is resized.
453 *****************************************************************************/
454 static int vout_Init( vout_thread_t *p_vout )
459 I_OUTPUTPICTURES = 0;
461 #ifdef MODULE_NAME_IS_xvideo
462 /* Initialize the output structure; we already found an XVideo port,
463 * and the corresponding chroma we will be using. Since we can
464 * arbitrary scale, stick to the coordinates and aspect. */
465 p_vout->output.i_width = p_vout->render.i_width;
466 p_vout->output.i_height = p_vout->render.i_height;
467 p_vout->output.i_aspect = p_vout->render.i_aspect;
469 switch( p_vout->output.i_chroma )
472 p_vout->output.i_rmask = 0x001f;
473 p_vout->output.i_gmask = 0x07e0;
474 p_vout->output.i_bmask = 0xf800;
477 p_vout->output.i_rmask = 0x001f;
478 p_vout->output.i_gmask = 0x03e0;
479 p_vout->output.i_bmask = 0x7c00;
484 /* Initialize the output structure: RGB with square pixels, whatever
485 * the input format is, since it's the only format we know */
486 switch( p_vout->p_sys->i_screen_depth )
488 case 8: /* FIXME: set the palette */
489 p_vout->output.i_chroma = FOURCC_RGB2; break;
491 p_vout->output.i_chroma = FOURCC_RV15; break;
493 p_vout->output.i_chroma = FOURCC_RV16; break;
495 p_vout->output.i_chroma = FOURCC_RV24; break;
497 p_vout->output.i_chroma = FOURCC_RV24; break;
499 msg_Err( p_vout, "unknown screen depth %i",
500 p_vout->p_sys->i_screen_depth );
504 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
505 p_vout->p_sys->p_win->i_height,
507 &p_vout->output.i_width, &p_vout->output.i_height );
509 /* Assume we have square pixels */
510 p_vout->output.i_aspect = p_vout->output.i_width
511 * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
514 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
515 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
519 /* Find an empty picture slot */
520 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
522 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
524 p_pic = p_vout->p_picture + i_index;
529 /* Allocate the picture */
530 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
535 p_pic->i_status = DESTROYED_PICTURE;
536 p_pic->i_type = DIRECT_PICTURE;
538 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
546 /*****************************************************************************
547 * vout_Render: render previously calculated output
548 *****************************************************************************/
549 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
554 /*****************************************************************************
555 * vout_Display: displays previously rendered output
556 *****************************************************************************
557 * This function sends the currently rendered image to X11 server.
558 * (The Xv extension takes care of "double-buffering".)
559 *****************************************************************************/
560 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
562 int i_width, i_height, i_x, i_y;
564 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
565 p_vout->p_sys->p_win->i_height,
566 &i_x, &i_y, &i_width, &i_height );
568 #ifdef HAVE_SYS_SHM_H
569 if( p_vout->p_sys->b_shm )
571 /* Display rendered image using shared memory extension */
572 # ifdef MODULE_NAME_IS_xvideo
573 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
574 p_vout->p_sys->p_win->video_window,
575 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
576 0 /*src_x*/, 0 /*src_y*/,
577 p_vout->output.i_width, p_vout->output.i_height,
578 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
579 False /* Don't put True here or you'll waste your CPU */ );
581 XShmPutImage( p_vout->p_sys->p_display,
582 p_vout->p_sys->p_win->video_window,
583 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
584 0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
585 p_vout->output.i_width, p_vout->output.i_height,
586 False /* Don't put True here ! */ );
590 #endif /* HAVE_SYS_SHM_H */
592 /* Use standard XPutImage -- this is gonna be slow ! */
593 #ifdef MODULE_NAME_IS_xvideo
594 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
595 p_vout->p_sys->p_win->video_window,
596 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
597 0 /*src_x*/, 0 /*src_y*/,
598 p_vout->output.i_width, p_vout->output.i_height,
599 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
601 XPutImage( p_vout->p_sys->p_display,
602 p_vout->p_sys->p_win->video_window,
603 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
604 0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
605 p_vout->output.i_width, p_vout->output.i_height );
609 /* Make sure the command is sent now - do NOT use XFlush !*/
610 XSync( p_vout->p_sys->p_display, False );
613 /*****************************************************************************
614 * vout_Manage: handle X11 events
615 *****************************************************************************
616 * This function should be called regularly by video output thread. It manages
617 * X11 events and allows window resizing. It returns a non null value on
619 *****************************************************************************/
620 static int vout_Manage( vout_thread_t *p_vout )
622 XEvent xevent; /* X11 event */
623 char i_key; /* ISO Latin-1 key */
626 /* Handle X11 events: ConfigureNotify events are parsed to know if the
627 * output window's size changed, MapNotify and UnmapNotify to know if the
628 * window is mapped (and if the display is useful), and ClientMessages
629 * to intercept window destruction requests */
631 while( XCheckWindowEvent( p_vout->p_sys->p_display,
632 p_vout->p_sys->p_win->base_window,
633 StructureNotifyMask | KeyPressMask |
634 ButtonPressMask | ButtonReleaseMask |
635 PointerMotionMask | Button1MotionMask , &xevent )
638 /* ConfigureNotify event: prepare */
639 if( xevent.type == ConfigureNotify )
641 if( (xevent.xconfigure.width != p_vout->p_sys->p_win->i_width)
642 || (xevent.xconfigure.height != p_vout->p_sys->p_win->i_height) )
644 /* Update dimensions */
645 p_vout->i_changes |= VOUT_SIZE_CHANGE;
646 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
647 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
651 else if( xevent.type == KeyPress )
653 /* We may have keys like F1 trough F12, ESC ... */
654 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
655 xevent.xkey.keycode, 0 );
656 switch( x_key_symbol )
659 if( p_vout->b_fullscreen )
661 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
665 p_vout->p_vlc->b_die = 1;
670 intf_thread_t *p_intf;
671 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
675 p_intf->b_menu_change = 1;
676 vlc_object_release( p_intf );
681 input_Seek( p_vout, -5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
684 input_Seek( p_vout, 5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
687 input_Seek( p_vout, 60, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
690 input_Seek( p_vout, -60, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
693 input_Seek( p_vout, 0, INPUT_SEEK_BYTES | INPUT_SEEK_SET );
696 input_Seek( p_vout, 0, INPUT_SEEK_BYTES | INPUT_SEEK_END );
699 input_Seek( p_vout, 900, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
702 input_Seek( p_vout, -900, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
705 input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
708 case XK_F1: network_ChannelJoin( p_vout, 1 ); break;
709 case XK_F2: network_ChannelJoin( p_vout, 2 ); break;
710 case XK_F3: network_ChannelJoin( p_vout, 3 ); break;
711 case XK_F4: network_ChannelJoin( p_vout, 4 ); break;
712 case XK_F5: network_ChannelJoin( p_vout, 5 ); break;
713 case XK_F6: network_ChannelJoin( p_vout, 6 ); break;
714 case XK_F7: network_ChannelJoin( p_vout, 7 ); break;
715 case XK_F8: network_ChannelJoin( p_vout, 8 ); break;
716 case XK_F9: network_ChannelJoin( p_vout, 9 ); break;
717 case XK_F10: network_ChannelJoin( p_vout, 10 ); break;
718 case XK_F11: network_ChannelJoin( p_vout, 11 ); break;
719 case XK_F12: network_ChannelJoin( p_vout, 12 ); break;
723 * The reason why I use this instead of XK_0 is that
724 * with XLookupString, we don't have to care about
727 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
729 /* FIXME: handle stuff here */
734 p_vout->p_vlc->b_die = 1;
738 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
749 else if( xevent.type == ButtonPress )
751 int i_width, i_height, i_x, i_y;
753 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
754 p_vout->p_sys->p_win->i_height,
755 &i_x, &i_y, &i_width, &i_height );
757 p_vout->i_mouse_x = ( xevent.xmotion.x - i_x )
758 * p_vout->render.i_width / i_width;
759 p_vout->i_mouse_y = ( xevent.xmotion.y - i_y )
760 * p_vout->render.i_height / i_height;
761 p_vout->i_mouse_button = 1;
763 switch( ((XButtonEvent *)&xevent)->button )
766 /* In this part we will eventually manage
767 * clicks for DVD navigation for instance. */
769 /* detect double-clicks */
770 if( ( ((XButtonEvent *)&xevent)->time -
771 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
773 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
776 p_vout->p_sys->i_time_button_last_pressed =
777 ((XButtonEvent *)&xevent)->time;
781 input_Seek( p_vout, 15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
785 input_Seek( p_vout, -15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
790 else if( xevent.type == ButtonRelease )
792 switch( ((XButtonEvent *)&xevent)->button )
796 intf_thread_t *p_intf;
797 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
801 p_intf->b_menu_change = 1;
802 vlc_object_release( p_intf );
809 else if( xevent.type == MotionNotify )
811 int i_width, i_height, i_x, i_y;
813 /* somewhat different use for vout_PlacePicture:
814 * here the values are needed to give to mouse coordinates
815 * in the original picture space */
816 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
817 p_vout->p_sys->p_win->i_height,
818 &i_x, &i_y, &i_width, &i_height );
820 p_vout->i_mouse_x = ( xevent.xmotion.x - i_x )
821 * p_vout->render.i_width / i_width;
822 p_vout->i_mouse_y = ( xevent.xmotion.y - i_y )
823 * p_vout->render.i_height / i_height;
825 p_vout->p_sys->i_time_mouse_last_moved = mdate();
826 if( ! p_vout->p_sys->b_mouse_pointer_visible )
828 ToggleCursor( p_vout );
831 /* Reparent move -- XXX: why are we getting this ? */
832 else if( xevent.type == ReparentNotify )
839 msg_Warn( p_vout, "unhandled event %d received", xevent.type );
843 /* Handle events for video output sub-window */
844 while( XCheckWindowEvent( p_vout->p_sys->p_display,
845 p_vout->p_sys->p_win->video_window,
846 ExposureMask, &xevent ) == True )
848 /* Window exposed (only handled if stream playback is paused) */
849 if( xevent.type == Expose )
851 if( ((XExposeEvent *)&xevent)->count == 0 )
853 /* (if this is the last a collection of expose events...) */
855 if( p_vout->p_vlc->p_input_bank->pp_input[0] != NULL )
857 if( PAUSE_S == p_vout->p_vlc->p_input_bank->pp_input[0]
858 ->stream.control.i_status )
860 /* XVideoDisplay( p_vout )*/;
868 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
869 * are handled - according to the man pages, the format is always 32
871 while( XCheckTypedEvent( p_vout->p_sys->p_display,
872 ClientMessage, &xevent ) )
874 if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
875 && (xevent.xclient.data.l[0]
876 == p_vout->p_sys->p_win->wm_delete_window ) )
878 p_vout->p_vlc->b_die = 1;
885 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
887 ToggleFullScreen( p_vout );
888 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
894 * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
895 * the size flag inside the fullscreen routine)
897 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
899 int i_width, i_height, i_x, i_y;
901 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
903 msg_Dbg( p_vout, "video display resized (%dx%d)",
904 p_vout->p_sys->p_win->i_width,
905 p_vout->p_sys->p_win->i_height );
907 #ifdef MODULE_NAME_IS_x11
908 /* We need to signal the vout thread about the size change because it
909 * is doing the rescaling */
910 p_vout->i_changes |= VOUT_SIZE_CHANGE;
913 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
914 p_vout->p_sys->p_win->i_height,
915 &i_x, &i_y, &i_width, &i_height );
917 XResizeWindow( p_vout->p_sys->p_display,
918 p_vout->p_sys->p_win->video_window, i_width, i_height );
920 XMoveWindow( p_vout->p_sys->p_display,
921 p_vout->p_sys->p_win->video_window, i_x, i_y );
924 /* Autohide Cursour */
925 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
927 /* Hide the mouse automatically */
928 if( p_vout->p_sys->b_mouse_pointer_visible )
930 ToggleCursor( p_vout );
937 /*****************************************************************************
938 * vout_End: terminate X11 video thread output method
939 *****************************************************************************
940 * Destroy the X11 XImages created by vout_Init. It is called at the end of
941 * the thread, but also each time the window is resized.
942 *****************************************************************************/
943 static void vout_End( vout_thread_t *p_vout )
947 /* Free the direct buffers we allocated */
948 for( i_index = I_OUTPUTPICTURES ; i_index ; )
951 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
955 /* following functions are local */
957 /*****************************************************************************
958 * CreateWindow: open and set-up X11 main window
959 *****************************************************************************/
960 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
962 XSizeHints xsize_hints;
963 XSetWindowAttributes xwindow_attributes;
968 vlc_bool_t b_configure_notify;
969 vlc_bool_t b_map_notify;
971 long long int i_drawable;
973 /* Prepare window manager hints and properties */
974 xsize_hints.base_width = p_win->i_width;
975 xsize_hints.base_height = p_win->i_height;
976 xsize_hints.flags = PSize;
977 p_win->wm_protocols =
978 XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
979 p_win->wm_delete_window =
980 XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
982 /* Prepare window attributes */
983 xwindow_attributes.backing_store = Always; /* save the hidden part */
984 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
985 p_vout->p_sys->i_screen);
986 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
988 /* Check whether someone provided us with a window ID */
989 i_drawable = p_vout->b_fullscreen ?
990 -1 : config_GetInt( p_vout, MODULE_STRING "-drawable");
992 if( i_drawable == -1 )
994 p_vout->p_sys->b_createwindow = 1;
996 /* Create the window and set hints - the window must receive
997 * ConfigureNotify events, and until it is displayed, Expose and
998 * MapNotify events. */
1000 p_win->base_window =
1001 XCreateWindow( p_vout->p_sys->p_display,
1002 DefaultRootWindow( p_vout->p_sys->p_display ),
1004 p_win->i_width, p_win->i_height,
1007 CWBackingStore | CWBackPixel | CWEventMask,
1008 &xwindow_attributes );
1010 if( !p_vout->b_fullscreen )
1012 /* Set window manager hints and properties: size hints, command,
1013 * window's name, and accepted protocols */
1014 XSetWMNormalHints( p_vout->p_sys->p_display,
1015 p_win->base_window, &xsize_hints );
1016 XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
1017 p_vout->p_vlc->ppsz_argv, p_vout->p_vlc->i_argc );
1019 XStoreName( p_vout->p_sys->p_display, p_win->base_window,
1020 #ifdef MODULE_NAME_IS_x11
1021 VOUT_TITLE " (X11 output)"
1023 VOUT_TITLE " (XVideo output)"
1030 p_vout->p_sys->b_createwindow = 0;
1031 p_win->base_window = i_drawable;
1033 XChangeWindowAttributes( p_vout->p_sys->p_display,
1035 CWBackingStore | CWBackPixel | CWEventMask,
1036 &xwindow_attributes );
1039 if( (p_win->wm_protocols == None) /* use WM_DELETE_WINDOW */
1040 || (p_win->wm_delete_window == None)
1041 || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
1042 &p_win->wm_delete_window, 1 ) )
1044 /* WM_DELETE_WINDOW is not supported by window manager */
1045 msg_Warn( p_vout, "missing or bad window manager" );
1048 /* Creation of a graphic context that doesn't generate a GraphicsExpose
1049 * event when using functions like XCopyArea */
1050 xgcvalues.graphics_exposures = False;
1051 p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1053 GCGraphicsExposures, &xgcvalues );
1055 if( p_vout->p_sys->b_createwindow )
1057 /* Send orders to server, and wait until window is displayed - three
1058 * events must be received: a MapNotify event, an Expose event allowing
1059 * drawing in the window, and a ConfigureNotify to get the window
1060 * dimensions. Once those events have been received, only
1061 * ConfigureNotify events need to be received. */
1063 b_configure_notify = 0;
1065 XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1068 XNextEvent( p_vout->p_sys->p_display, &xevent);
1069 if( (xevent.type == Expose)
1070 && (xevent.xexpose.window == p_win->base_window) )
1074 else if( (xevent.type == MapNotify)
1075 && (xevent.xmap.window == p_win->base_window) )
1079 else if( (xevent.type == ConfigureNotify)
1080 && (xevent.xconfigure.window == p_win->base_window) )
1082 b_configure_notify = 1;
1083 p_win->i_width = xevent.xconfigure.width;
1084 p_win->i_height = xevent.xconfigure.height;
1086 } while( !( b_expose && b_configure_notify && b_map_notify ) );
1090 /* Get the window's geometry information */
1092 unsigned int dummy2, dummy3;
1093 XGetGeometry( p_vout->p_sys->p_display, p_win->base_window,
1094 &dummy1, &dummy2, &dummy3,
1100 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1101 StructureNotifyMask | KeyPressMask |
1102 ButtonPressMask | ButtonReleaseMask |
1103 PointerMotionMask );
1105 #ifdef MODULE_NAME_IS_x11
1106 if( p_vout->p_sys->b_createwindow &&
1107 XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1109 /* Allocate a new palette */
1110 p_vout->p_sys->colormap =
1111 XCreateColormap( p_vout->p_sys->p_display,
1112 DefaultRootWindow( p_vout->p_sys->p_display ),
1113 DefaultVisual( p_vout->p_sys->p_display,
1114 p_vout->p_sys->i_screen ),
1117 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1118 XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1119 CWColormap, &xwindow_attributes );
1123 /* Create video output sub-window. */
1124 p_win->video_window = XCreateSimpleWindow(
1125 p_vout->p_sys->p_display,
1126 p_win->base_window, 0, 0,
1127 p_win->i_width, p_win->i_height,
1129 BlackPixel( p_vout->p_sys->p_display,
1130 p_vout->p_sys->i_screen ),
1131 WhitePixel( p_vout->p_sys->p_display,
1132 p_vout->p_sys->i_screen ) );
1134 XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1135 BlackPixel( p_vout->p_sys->p_display,
1136 p_vout->p_sys->i_screen ) );
1138 XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1139 XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1142 /* make sure the video window will be centered in the next vout_Manage() */
1143 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1145 /* If the cursor was formerly blank than blank it again */
1146 if( !p_vout->p_sys->b_mouse_pointer_visible )
1148 ToggleCursor( p_vout );
1149 ToggleCursor( p_vout );
1152 /* Do NOT use XFlush here ! */
1153 XSync( p_vout->p_sys->p_display, False );
1155 /* At this stage, the window is open, displayed, and ready to
1157 p_vout->p_sys->p_win = p_win;
1162 /*****************************************************************************
1163 * DestroyWindow: destroy the window
1164 *****************************************************************************
1166 *****************************************************************************/
1167 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1169 /* Do NOT use XFlush here ! */
1170 XSync( p_vout->p_sys->p_display, False );
1172 XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1173 XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1174 XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1175 XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1178 /*****************************************************************************
1179 * NewPicture: allocate a picture
1180 *****************************************************************************
1181 * Returns 0 on success, -1 otherwise
1182 *****************************************************************************/
1183 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1185 /* We know the chroma, allocate a buffer which will be used
1186 * directly by the decoder */
1187 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1189 if( p_pic->p_sys == NULL )
1194 #ifdef HAVE_SYS_SHM_H
1195 if( p_vout->p_sys->b_shm )
1197 /* Create image using XShm extension */
1198 p_pic->p_sys->p_image =
1199 CreateShmImage( p_vout, p_vout->p_sys->p_display,
1200 # ifdef MODULE_NAME_IS_xvideo
1201 p_vout->p_sys->i_xvport, p_vout->output.i_chroma,
1203 p_vout->p_sys->p_visual,
1204 p_vout->p_sys->i_screen_depth,
1206 &p_pic->p_sys->shminfo,
1207 p_vout->output.i_width, p_vout->output.i_height );
1210 #endif /* HAVE_SYS_SHM_H */
1212 /* Create image without XShm extension */
1213 p_pic->p_sys->p_image =
1214 CreateImage( p_vout, p_vout->p_sys->p_display,
1215 #ifdef MODULE_NAME_IS_xvideo
1216 p_vout->p_sys->i_xvport, p_vout->output.i_chroma,
1218 p_vout->p_sys->p_visual,
1219 p_vout->p_sys->i_screen_depth,
1220 p_vout->p_sys->i_bytes_per_pixel,
1222 p_vout->output.i_width, p_vout->output.i_height );
1225 if( p_pic->p_sys->p_image == NULL )
1227 free( p_pic->p_sys );
1231 switch( p_vout->output.i_chroma )
1233 #ifdef MODULE_NAME_IS_xvideo
1236 p_pic->Y_PIXELS = p_pic->p_sys->p_image->data
1237 + p_pic->p_sys->p_image->offsets[0];
1238 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1239 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[0];
1240 p_pic->p[Y_PLANE].i_pixel_bytes = 1;
1241 p_pic->p[Y_PLANE].b_margin = 0;
1243 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1244 + p_pic->p_sys->p_image->offsets[1];
1245 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1246 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[1];
1247 p_pic->p[U_PLANE].i_pixel_bytes = 1;
1248 p_pic->p[U_PLANE].b_margin = 0;
1250 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1251 + p_pic->p_sys->p_image->offsets[2];
1252 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1253 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[2];
1254 p_pic->p[V_PLANE].i_pixel_bytes = 1;
1255 p_pic->p[V_PLANE].b_margin = 0;
1257 p_pic->i_planes = 3;
1262 p_pic->Y_PIXELS = p_pic->p_sys->p_image->data
1263 + p_pic->p_sys->p_image->offsets[0];
1264 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1265 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[0];
1266 p_pic->p[Y_PLANE].i_pixel_bytes = 1;
1267 p_pic->p[Y_PLANE].b_margin = 0;
1269 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1270 + p_pic->p_sys->p_image->offsets[2];
1271 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1272 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[2];
1273 p_pic->p[U_PLANE].i_pixel_bytes = 1;
1274 p_pic->p[U_PLANE].b_margin = 0;
1276 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1277 + p_pic->p_sys->p_image->offsets[1];
1278 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1279 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[1];
1280 p_pic->p[V_PLANE].i_pixel_bytes = 1;
1281 p_pic->p[V_PLANE].b_margin = 0;
1283 p_pic->i_planes = 3;
1288 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1289 + p_pic->p_sys->p_image->offsets[0];
1290 p_pic->p->i_lines = p_vout->output.i_height;
1291 /* XXX: this just looks so plain wrong... check it out ! */
1292 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0] / 4;
1293 p_pic->p->i_pixel_bytes = 4;
1294 p_pic->p->b_margin = 0;
1296 p_pic->i_planes = 1;
1302 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1303 + p_pic->p_sys->p_image->offsets[0];
1304 p_pic->p->i_lines = p_vout->output.i_height;
1305 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1306 p_pic->p->i_pixel_bytes = 4;
1307 p_pic->p->b_margin = 0;
1309 p_pic->i_planes = 1;
1314 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1315 + p_pic->p_sys->p_image->offsets[0];
1316 p_pic->p->i_lines = p_vout->output.i_height;
1317 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1318 p_pic->p->i_pixel_bytes = 2;
1319 p_pic->p->b_margin = 0;
1321 p_pic->i_planes = 1;
1326 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1327 + p_pic->p_sys->p_image->offsets[0];
1328 p_pic->p->i_lines = p_vout->output.i_height;
1329 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1330 p_pic->p->i_pixel_bytes = 2;
1331 p_pic->p->b_margin = 0;
1333 p_pic->i_planes = 1;
1339 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1340 + p_pic->p_sys->p_image->xoffset;
1341 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1342 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1343 p_pic->p->i_pixel_bytes = p_pic->p_sys->p_image->depth;
1345 if( p_pic->p->i_pitch == p_pic->p_sys->p_image->width )
1347 p_pic->p->b_margin = 0;
1351 p_pic->p->b_margin = 1;
1352 p_pic->p->b_hidden = 1;
1353 p_pic->p->i_visible_bytes = p_pic->p_sys->p_image->width;
1356 p_pic->i_planes = 1;
1363 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1364 + p_pic->p_sys->p_image->xoffset;
1365 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1366 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1367 p_pic->p->i_pixel_bytes = p_pic->p_sys->p_image->depth;
1369 if( p_pic->p->i_pitch == 2 * p_pic->p_sys->p_image->width )
1371 p_pic->p->b_margin = 0;
1375 p_pic->p->b_margin = 1;
1376 p_pic->p->b_hidden = 1;
1377 p_pic->p->i_visible_bytes = 2 * p_pic->p_sys->p_image->width;
1380 p_pic->i_planes = 1;
1387 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1388 + p_pic->p_sys->p_image->xoffset;
1389 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1390 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1391 p_pic->p->i_pixel_bytes = p_pic->p_sys->p_image->depth;
1393 if( p_pic->p->i_pitch == 4 * p_pic->p_sys->p_image->width )
1395 p_pic->p->b_margin = 0;
1399 p_pic->p->b_margin = 1;
1400 p_pic->p->b_hidden = 1;
1401 p_pic->p->i_visible_bytes = 4 * p_pic->p_sys->p_image->width;
1404 p_pic->i_planes = 1;
1410 /* Unknown chroma, tell the guy to get lost */
1411 IMAGE_FREE( p_pic->p_sys->p_image );
1412 free( p_pic->p_sys );
1413 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1414 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1415 p_pic->i_planes = 0;
1422 /*****************************************************************************
1423 * FreePicture: destroy a picture allocated with NewPicture
1424 *****************************************************************************
1425 * Destroy XImage AND associated data. If using Shm, detach shared memory
1426 * segment from server and process, then free it. The XDestroyImage manpage
1427 * says that both the image structure _and_ the data pointed to by the
1428 * image structure are freed, so no need to free p_image->data.
1429 *****************************************************************************/
1430 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1432 /* The order of operations is correct */
1433 #ifdef HAVE_SYS_SHM_H
1434 if( p_vout->p_sys->b_shm )
1436 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
1437 IMAGE_FREE( p_pic->p_sys->p_image );
1439 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
1440 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
1442 msg_Err( p_vout, "cannot detach shared memory (%s)",
1449 IMAGE_FREE( p_pic->p_sys->p_image );
1452 /* Do NOT use XFlush here ! */
1453 XSync( p_vout->p_sys->p_display, False );
1455 free( p_pic->p_sys );
1458 /*****************************************************************************
1459 * ToggleFullScreen: Enable or disable full screen mode
1460 *****************************************************************************
1461 * This function will switch between fullscreen and window mode.
1462 *****************************************************************************/
1463 static void ToggleFullScreen ( vout_thread_t *p_vout )
1466 mwmhints_t mwmhints;
1467 XSetWindowAttributes attributes;
1469 p_vout->b_fullscreen = !p_vout->b_fullscreen;
1471 if( p_vout->b_fullscreen )
1473 msg_Dbg( p_vout, "entering fullscreen mode" );
1474 p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
1476 /* Only check the fullscreen method when we actually go fullscreen,
1477 * because to go back to window mode we need to know in which
1478 * fullscreen mode we were */
1479 p_vout->p_sys->b_altfullscreen =
1480 config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
1482 /* fullscreen window size and position */
1483 p_vout->p_sys->p_win->i_width =
1484 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1485 p_vout->p_sys->p_win->i_height =
1486 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1488 CreateWindow( p_vout, p_vout->p_sys->p_win );
1490 /* To my knowledge there are two ways to create a borderless window.
1491 * There's the generic way which is to tell x to bypass the window
1492 * manager, but this creates problems with the focus of other
1494 * The other way is to use the motif property "_MOTIF_WM_HINTS" which
1495 * luckily seems to be supported by most window managers. */
1496 if( !p_vout->p_sys->b_altfullscreen )
1498 mwmhints.flags = MWM_HINTS_DECORATIONS;
1499 mwmhints.decorations = !p_vout->b_fullscreen;
1501 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1503 XChangeProperty( p_vout->p_sys->p_display,
1504 p_vout->p_sys->p_win->base_window,
1505 prop, prop, 32, PropModeReplace,
1506 (unsigned char *)&mwmhints,
1507 PROP_MWM_HINTS_ELEMENTS );
1511 /* brute force way to remove decorations */
1512 attributes.override_redirect = p_vout->b_fullscreen;
1513 XChangeWindowAttributes( p_vout->p_sys->p_display,
1514 p_vout->p_sys->p_win->base_window,
1519 XReparentWindow( p_vout->p_sys->p_display,
1520 p_vout->p_sys->p_win->base_window,
1521 DefaultRootWindow( p_vout->p_sys->p_display ),
1523 XMoveResizeWindow( p_vout->p_sys->p_display,
1524 p_vout->p_sys->p_win->base_window,
1526 p_vout->p_sys->p_win->i_width,
1527 p_vout->p_sys->p_win->i_height );
1531 msg_Dbg( p_vout, "leaving fullscreen mode" );
1532 DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
1533 p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
1536 XSync( p_vout->p_sys->p_display, True );
1538 if( !p_vout->b_fullscreen || p_vout->p_sys->b_altfullscreen )
1540 XSetInputFocus(p_vout->p_sys->p_display,
1541 p_vout->p_sys->p_win->base_window,
1546 /* signal that the size needs to be updated */
1547 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1550 /*****************************************************************************
1551 * EnableXScreenSaver: enable screen saver
1552 *****************************************************************************
1553 * This function enables the screen saver on a display after it has been
1554 * disabled by XDisableScreenSaver.
1555 * FIXME: what happens if multiple vlc sessions are running at the same
1557 *****************************************************************************/
1558 static void EnableXScreenSaver( vout_thread_t *p_vout )
1560 #ifdef DPMSINFO_IN_DPMS_H
1564 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1565 p_vout->p_sys->i_ss_interval,
1566 p_vout->p_sys->i_ss_blanking,
1567 p_vout->p_sys->i_ss_exposure );
1569 /* Restore DPMS settings */
1570 #ifdef DPMSINFO_IN_DPMS_H
1571 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1573 if( p_vout->p_sys->b_ss_dpms )
1575 DPMSEnable( p_vout->p_sys->p_display );
1581 /*****************************************************************************
1582 * DisableXScreenSaver: disable screen saver
1583 *****************************************************************************
1584 * See XEnableXScreenSaver
1585 *****************************************************************************/
1586 static void DisableXScreenSaver( vout_thread_t *p_vout )
1588 #ifdef DPMSINFO_IN_DPMS_H
1592 /* Save screen saver informations */
1593 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1594 &p_vout->p_sys->i_ss_interval,
1595 &p_vout->p_sys->i_ss_blanking,
1596 &p_vout->p_sys->i_ss_exposure );
1598 /* Disable screen saver */
1599 XSetScreenSaver( p_vout->p_sys->p_display, 0,
1600 p_vout->p_sys->i_ss_interval,
1601 p_vout->p_sys->i_ss_blanking,
1602 p_vout->p_sys->i_ss_exposure );
1605 #ifdef DPMSINFO_IN_DPMS_H
1606 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1609 /* Save DPMS current state */
1610 DPMSInfo( p_vout->p_sys->p_display, &unused,
1611 &p_vout->p_sys->b_ss_dpms );
1612 DPMSDisable( p_vout->p_sys->p_display );
1617 /*****************************************************************************
1618 * CreateCursor: create a blank mouse pointer
1619 *****************************************************************************/
1620 static void CreateCursor( vout_thread_t *p_vout )
1622 XColor cursor_color;
1624 p_vout->p_sys->cursor_pixmap =
1625 XCreatePixmap( p_vout->p_sys->p_display,
1626 DefaultRootWindow( p_vout->p_sys->p_display ),
1629 XParseColor( p_vout->p_sys->p_display,
1630 XCreateColormap( p_vout->p_sys->p_display,
1632 p_vout->p_sys->p_display ),
1634 p_vout->p_sys->p_display,
1635 p_vout->p_sys->i_screen ),
1637 "black", &cursor_color );
1639 p_vout->p_sys->blank_cursor =
1640 XCreatePixmapCursor( p_vout->p_sys->p_display,
1641 p_vout->p_sys->cursor_pixmap,
1642 p_vout->p_sys->cursor_pixmap,
1643 &cursor_color, &cursor_color, 1, 1 );
1646 /*****************************************************************************
1647 * DestroyCursor: destroy the blank mouse pointer
1648 *****************************************************************************/
1649 static void DestroyCursor( vout_thread_t *p_vout )
1651 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
1654 /*****************************************************************************
1655 * ToggleCursor: hide or show the mouse pointer
1656 *****************************************************************************
1657 * This function hides the X pointer if it is visible by setting the pointer
1658 * sprite to a blank one. To show it again, we disable the sprite.
1659 *****************************************************************************/
1660 static void ToggleCursor( vout_thread_t *p_vout )
1662 if( p_vout->p_sys->b_mouse_pointer_visible )
1664 XDefineCursor( p_vout->p_sys->p_display,
1665 p_vout->p_sys->p_win->base_window,
1666 p_vout->p_sys->blank_cursor );
1667 p_vout->p_sys->b_mouse_pointer_visible = 0;
1671 XUndefineCursor( p_vout->p_sys->p_display,
1672 p_vout->p_sys->p_win->base_window );
1673 p_vout->p_sys->b_mouse_pointer_visible = 1;
1677 #ifdef MODULE_NAME_IS_xvideo
1678 /*****************************************************************************
1679 * XVideoGetPort: get YUV12 port
1680 *****************************************************************************/
1681 static int XVideoGetPort( vout_thread_t *p_vout,
1682 u32 i_chroma, u32 *pi_newchroma )
1684 XvAdaptorInfo *p_adaptor;
1686 int i_adaptor, i_num_adaptors, i_requested_adaptor;
1687 int i_selected_port;
1689 switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
1694 case XvBadExtension:
1695 msg_Warn( p_vout, "XvBadExtension" );
1699 msg_Warn( p_vout, "XvBadAlloc" );
1703 msg_Warn( p_vout, "XvQueryExtension failed" );
1707 switch( XvQueryAdaptors( p_vout->p_sys->p_display,
1708 DefaultRootWindow( p_vout->p_sys->p_display ),
1709 &i_num_adaptors, &p_adaptor ) )
1714 case XvBadExtension:
1715 msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
1719 msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
1723 msg_Warn( p_vout, "XvQueryAdaptors failed" );
1727 i_selected_port = -1;
1728 i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
1730 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
1732 XvImageFormatValues *p_formats;
1733 int i_format, i_num_formats;
1736 /* If we requested an adaptor and it's not this one, we aren't
1738 if( i_requested_adaptor != -1 && i_adaptor != i_requested_adaptor )
1743 /* If the adaptor doesn't have the required properties, skip it */
1744 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
1745 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
1750 /* Check that adaptor supports our requested format... */
1751 p_formats = XvListImageFormats( p_vout->p_sys->p_display,
1752 p_adaptor[i_adaptor].base_id,
1756 i_format < i_num_formats && ( i_selected_port == -1 );
1759 /* Code removed, we can get this through xvinfo anyway */
1761 XvEncodingInfo *p_enc;
1762 int i_enc, i_num_encodings;
1763 XvAttribute *p_attr;
1764 int i_attr, i_num_attributes;
1767 /* If this is not the format we want, or at least a
1768 * similar one, forget it */
1769 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
1774 /* Look for the first available port supporting this format */
1775 for( i_port = p_adaptor[i_adaptor].base_id;
1776 ( i_port < p_adaptor[i_adaptor].base_id
1777 + p_adaptor[i_adaptor].num_ports )
1778 && ( i_selected_port == -1 );
1781 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
1784 i_selected_port = i_port;
1785 *pi_newchroma = p_formats[ i_format ].id;
1789 /* If no free port was found, forget it */
1790 if( i_selected_port == -1 )
1795 /* If we found a port, print information about it */
1796 msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
1797 i_adaptor, i_selected_port, p_formats[ i_format ].id,
1798 (char *)&p_formats[ i_format ].id,
1799 ( p_formats[ i_format ].format == XvPacked ) ?
1800 "packed" : "planar" );
1803 msg_Dbg( p_vout, " encoding list:" );
1805 if( XvQueryEncodings( p_vout->p_sys->p_display, i_selected_port,
1806 &i_num_encodings, &p_enc )
1809 msg_Dbg( p_vout, " XvQueryEncodings failed" );
1813 for( i_enc = 0; i_enc < i_num_encodings; i_enc++ )
1815 msg_Dbg( p_vout, " id=%ld, name=%s, size=%ldx%ld,"
1816 " numerator=%d, denominator=%d",
1817 p_enc[i_enc].encoding_id, p_enc[i_enc].name,
1818 p_enc[i_enc].width, p_enc[i_enc].height,
1819 p_enc[i_enc].rate.numerator,
1820 p_enc[i_enc].rate.denominator );
1825 XvFreeEncodingInfo( p_enc );
1828 msg_Dbg( p_vout, " attribute list:" );
1829 p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
1831 &i_num_attributes );
1832 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
1834 msg_Dbg( p_vout, " name=%s, flags=[%s%s ], min=%i, max=%i",
1835 p_attr[i_attr].name,
1836 (p_attr[i_attr].flags & XvGettable) ? " get" : "",
1837 (p_attr[i_attr].flags & XvSettable) ? " set" : "",
1838 p_attr[i_attr].min_value, p_attr[i_attr].max_value );
1841 if( p_attr != NULL )
1848 if( p_formats != NULL )
1855 if( i_num_adaptors > 0 )
1857 XvFreeAdaptorInfo( p_adaptor );
1860 if( i_selected_port == -1 )
1862 if( i_requested_adaptor == -1 )
1864 msg_Warn( p_vout, "no free XVideo port found for format "
1865 "0x%.8x (%4.4s)", i_chroma, (char*)&i_chroma );
1869 msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
1870 "XVideo port for format 0x%.8x (%4.4s)",
1871 i_requested_adaptor, i_chroma, (char*)&i_chroma );
1875 return( i_selected_port );
1878 /*****************************************************************************
1879 * XVideoReleasePort: release YUV12 port
1880 *****************************************************************************/
1881 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
1883 XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
1887 /*****************************************************************************
1888 * InitDisplay: open and initialize X11 device
1889 *****************************************************************************
1890 * Create a window according to video output given size, and set other
1891 * properties according to the display properties.
1892 *****************************************************************************/
1893 static int InitDisplay( vout_thread_t *p_vout )
1895 #ifdef MODULE_NAME_IS_x11
1896 XPixmapFormatValues * p_formats; /* pixmap formats */
1897 XVisualInfo * p_xvisual; /* visuals informations */
1898 XVisualInfo xvisual_template; /* visual template */
1899 int i_count; /* array size */
1902 #ifdef HAVE_SYS_SHM_H
1903 p_vout->p_sys->b_shm = 0;
1905 if( config_GetInt( p_vout, MODULE_STRING "-shm" ) )
1908 /* FIXME: As of 2001-03-16, XFree4 for MacOS X does not support Xshm */
1910 p_vout->p_sys->b_shm =
1911 ( XShmQueryExtension( p_vout->p_sys->p_display ) == True );
1914 if( !p_vout->p_sys->b_shm )
1916 msg_Warn( p_vout, "XShm video extension is unavailable" );
1921 msg_Dbg( p_vout, "disabling XShm video extension" );
1925 msg_Warn( p_vout, "XShm video extension is unavailable" );
1929 #ifdef MODULE_NAME_IS_xvideo
1930 /* XXX The brightness and contrast values should be read from environment
1931 * XXX variables... */
1933 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
1934 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
1938 #ifdef MODULE_NAME_IS_x11
1939 /* Initialize structure */
1940 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
1942 /* Get screen depth */
1943 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
1944 p_vout->p_sys->i_screen );
1945 switch( p_vout->p_sys->i_screen_depth )
1949 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
1951 xvisual_template.screen = p_vout->p_sys->i_screen;
1952 xvisual_template.class = DirectColor;
1953 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1954 VisualScreenMask | VisualClassMask,
1955 &xvisual_template, &i_count );
1956 if( p_xvisual == NULL )
1958 msg_Err( p_vout, "no PseudoColor visual available" );
1961 p_vout->p_sys->i_bytes_per_pixel = 1;
1962 p_vout->output.pf_setpalette = SetPalette;
1969 * Screen depth is higher than 8bpp. TrueColor visual is used.
1971 xvisual_template.screen = p_vout->p_sys->i_screen;
1972 xvisual_template.class = TrueColor;
1973 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1974 VisualScreenMask | VisualClassMask,
1975 &xvisual_template, &i_count );
1976 if( p_xvisual == NULL )
1978 msg_Err( p_vout, "no TrueColor visual available" );
1982 p_vout->output.i_rmask = p_xvisual->red_mask;
1983 p_vout->output.i_gmask = p_xvisual->green_mask;
1984 p_vout->output.i_bmask = p_xvisual->blue_mask;
1986 /* There is no difference yet between 3 and 4 Bpp. The only way
1987 * to find the actual number of bytes per pixel is to list supported
1988 * pixmap formats. */
1989 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
1990 p_vout->p_sys->i_bytes_per_pixel = 0;
1992 for( ; i_count-- ; p_formats++ )
1994 /* Under XFree4.0, the list contains pixmap formats available
1995 * through all video depths ; so we have to check against current
1997 if( p_formats->depth == p_vout->p_sys->i_screen_depth )
1999 if( p_formats->bits_per_pixel / 8
2000 > p_vout->p_sys->i_bytes_per_pixel )
2002 p_vout->p_sys->i_bytes_per_pixel = p_formats->bits_per_pixel / 8;
2008 p_vout->p_sys->p_visual = p_xvisual->visual;
2015 #ifdef HAVE_SYS_SHM_H
2016 /*****************************************************************************
2017 * CreateShmImage: create an XImage or XvImage using shared memory extension
2018 *****************************************************************************
2019 * Prepare an XImage or XvImage for display function.
2020 * The order of the operations respects the recommandations of the mit-shm
2021 * document by J.Corbet and K.Packard. Most of the parameters were copied from
2022 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
2023 *****************************************************************************/
2024 static IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
2025 Display* p_display, EXTRA_ARGS_SHM,
2026 int i_width, int i_height )
2028 IMAGE_TYPE *p_image;
2030 /* Create XImage / XvImage */
2031 #ifdef MODULE_NAME_IS_xvideo
2032 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2033 i_width, i_height, p_shm );
2035 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2036 p_shm, i_width, i_height );
2038 if( p_image == NULL )
2040 msg_Err( p_vout, "image creation failed" );
2044 /* Allocate shared memory segment - 0776 set the access permission
2045 * rights (like umask), they are not yet supported by all X servers */
2046 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
2047 if( p_shm->shmid < 0 )
2049 msg_Err( p_vout, "cannot allocate shared image data (%s)",
2050 strerror( errno ) );
2051 IMAGE_FREE( p_image );
2055 /* Attach shared memory segment to process (read/write) */
2056 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2057 if(! p_shm->shmaddr )
2059 msg_Err( p_vout, "cannot attach shared memory (%s)",
2061 IMAGE_FREE( p_image );
2062 shmctl( p_shm->shmid, IPC_RMID, 0 );
2066 /* Read-only data. We won't be using XShmGetImage */
2067 p_shm->readOnly = True;
2069 /* Attach shared memory segment to X server */
2070 if( XShmAttach( p_display, p_shm ) == False )
2072 msg_Err( p_vout, "cannot attach shared memory to X server" );
2073 IMAGE_FREE( p_image );
2074 shmctl( p_shm->shmid, IPC_RMID, 0 );
2075 shmdt( p_shm->shmaddr );
2079 /* Send image to X server. This instruction is required, since having
2080 * built a Shm XImage and not using it causes an error on XCloseDisplay,
2081 * and remember NOT to use XFlush ! */
2082 XSync( p_display, False );
2085 /* Mark the shm segment to be removed when there are no more
2086 * attachements, so it is automatic on process exit or after shmdt */
2087 shmctl( p_shm->shmid, IPC_RMID, 0 );
2094 /*****************************************************************************
2095 * CreateImage: create an XImage or XvImage
2096 *****************************************************************************
2097 * Create a simple image used as a buffer.
2098 *****************************************************************************/
2099 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2100 Display *p_display, EXTRA_ARGS,
2101 int i_width, int i_height )
2103 byte_t * p_data; /* image data storage zone */
2104 IMAGE_TYPE *p_image;
2105 #ifdef MODULE_NAME_IS_x11
2106 int i_quantum; /* XImage quantum (see below) */
2107 int i_bytes_per_line;
2110 /* Allocate memory for image */
2111 #ifdef MODULE_NAME_IS_xvideo
2112 p_data = (byte_t *) malloc( i_width * i_height * 2 ); /* XXX */
2114 i_bytes_per_line = i_width * i_bytes_per_pixel;
2115 p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
2119 msg_Err( p_vout, "out of memory" );
2123 #ifdef MODULE_NAME_IS_x11
2124 /* Optimize the quantum of a scanline regarding its size - the quantum is
2125 a diviser of the number of bits between the start of two scanlines. */
2126 if( i_bytes_per_line & 0xf )
2130 else if( i_bytes_per_line & 0x10 )
2140 /* Create XImage. p_data will be automatically freed */
2141 #ifdef MODULE_NAME_IS_xvideo
2142 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2143 p_data, i_width, i_height );
2145 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2146 p_data, i_width, i_height, i_quantum, 0 );
2148 if( p_image == NULL )
2150 msg_Err( p_vout, "XCreateImage() failed" );
2158 #ifdef MODULE_NAME_IS_x11
2159 /*****************************************************************************
2160 * SetPalette: sets an 8 bpp palette
2161 *****************************************************************************
2162 * This function sets the palette given as an argument. It does not return
2163 * anything, but could later send information on which colors it was unable
2165 *****************************************************************************/
2166 static void SetPalette( vout_thread_t *p_vout, u16 *red, u16 *green, u16 *blue )
2169 XColor p_colors[255];
2171 /* allocate palette */
2172 for( i = 0; i < 255; i++ )
2174 /* kludge: colors are indexed reversely because color 255 seems
2175 * to be reserved for black even if we try to set it to white */
2176 p_colors[ i ].pixel = 255 - i;
2177 p_colors[ i ].pad = 0;
2178 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
2179 p_colors[ i ].red = red[ 255 - i ];
2180 p_colors[ i ].blue = blue[ 255 - i ];
2181 p_colors[ i ].green = green[ 255 - i ];
2184 XStoreColors( p_vout->p_sys->p_display,
2185 p_vout->p_sys->colormap, p_colors, 255 );