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.7 2002/01/05 03:49:18 sam Exp $
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * David Kennedy <dkennedy@tinytoad.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <errno.h> /* ENOMEM */
30 #include <stdlib.h> /* free() */
31 #include <string.h> /* strerror() */
33 #include <videolan/vlc.h>
35 #ifdef HAVE_MACHINE_PARAM_H
37 #include <machine/param.h>
38 #include <sys/types.h> /* typedef ushort */
43 #include <netinet/in.h> /* BSD: struct in_addr */
46 #include <sys/shm.h> /* shmget(), shmctl() */
48 #include <X11/Xutil.h>
49 #include <X11/keysym.h>
50 #include <X11/extensions/XShm.h>
51 #include <X11/extensions/dpms.h>
53 #ifdef MODULE_NAME_IS_xvideo
54 # include <X11/extensions/Xv.h>
55 # include <X11/extensions/Xvlib.h>
59 #include "video_output.h"
62 #include "interface.h"
63 #include "netutils.h" /* network_ChannelJoin */
65 #include "stream_control.h" /* needed by input_ext-intf.h... */
66 #include "input_ext-intf.h"
68 #ifdef MODULE_NAME_IS_xvideo
69 # define IMAGE_TYPE XvImage
70 # define EXTRA_ARGS int i_xvport, int i_chroma
71 # define EXTRA_ARGS_SHM int i_xvport, int i_chroma, XShmSegmentInfo *p_shm
72 # define DATA_SIZE(p) (p)->data_size
73 # define IMAGE_FREE XFree /* There is nothing like XvDestroyImage */
75 # define IMAGE_TYPE XImage
76 # define EXTRA_ARGS Visual *p_visual, int i_depth, int i_bytes_per_pixel
77 # define EXTRA_ARGS_SHM Visual *p_visual, int i_depth, XShmSegmentInfo *p_shm
78 # define DATA_SIZE(p) ((p)->bytes_per_line * (p)->height)
79 # define IMAGE_FREE XDestroyImage
82 /*****************************************************************************
84 *****************************************************************************/
85 static int vout_Probe ( probedata_t * );
86 static int vout_Create ( vout_thread_t * );
87 static void vout_Destroy ( vout_thread_t * );
88 static void vout_Render ( vout_thread_t *, picture_t * );
89 static void vout_Display ( vout_thread_t *, picture_t * );
90 static int vout_Manage ( vout_thread_t * );
91 static int vout_Init ( vout_thread_t * );
92 static void vout_End ( vout_thread_t * );
94 static int InitDisplay ( vout_thread_t * );
96 static int CreateWindow ( vout_thread_t * );
97 static void DestroyWindow ( vout_thread_t * );
99 static int NewPicture ( vout_thread_t *, picture_t * );
100 static void FreePicture ( vout_thread_t *, picture_t * );
102 static IMAGE_TYPE *CreateImage ( Display *, EXTRA_ARGS, int, int );
103 static IMAGE_TYPE *CreateShmImage ( Display *, EXTRA_ARGS_SHM, int, int );
105 static void ToggleFullScreen ( vout_thread_t * );
107 static void EnableXScreenSaver ( vout_thread_t * );
108 static void DisableXScreenSaver ( vout_thread_t * );
110 static void CreateCursor ( vout_thread_t * );
111 static void DestroyCursor ( vout_thread_t * );
112 static void ToggleCursor ( vout_thread_t * );
114 #ifdef MODULE_NAME_IS_xvideo
115 static int XVideoGetPort ( Display *, u32, u32 * );
116 static void XVideoReleasePort ( Display *, int );
119 /*****************************************************************************
120 * vout_sys_t: video output method descriptor
121 *****************************************************************************
122 * This structure is part of the video output thread descriptor.
123 * It describes the X11 and XVideo specific properties of an output thread.
124 *****************************************************************************/
125 typedef struct vout_sys_s
127 /* Internal settings and properties */
128 Display * p_display; /* display pointer */
130 Visual * p_visual; /* visual pointer */
131 int i_screen; /* screen number */
132 Window window; /* root window */
133 GC gc; /* graphic context instance handler */
135 boolean_t b_shm; /* shared memory extension flag */
137 #ifdef MODULE_NAME_IS_xvideo
138 Window yuv_window; /* sub-window for displaying yuv video
142 Colormap colormap; /* colormap used (8bpp only) */
145 int i_bytes_per_pixel;
146 int i_bytes_per_line;
152 /* X11 generic properties */
154 Atom wm_delete_window;
156 int i_width; /* width of main window */
157 int i_height; /* height of main window */
159 /* Backup of window position and size before fullscreen switch */
165 /* Screen saver properties */
166 int i_ss_timeout; /* timeout */
167 int i_ss_interval; /* interval between changes */
168 int i_ss_blanking; /* blanking mode */
169 int i_ss_exposure; /* exposure mode */
170 BOOL b_ss_dpms; /* DPMS mode */
172 /* Mouse pointer properties */
173 boolean_t b_mouse_pointer_visible;
174 mtime_t i_time_mouse_last_moved; /* used to auto-hide pointer*/
175 Cursor blank_cursor; /* the hidden cursor */
176 Pixmap cursor_pixmap;
180 /*****************************************************************************
181 * picture_sys_t: direct buffer method descriptor
182 *****************************************************************************
183 * This structure is part of the picture descriptor, it describes the
184 * XVideo specific properties of a direct buffer.
185 *****************************************************************************/
186 typedef struct picture_sys_s
188 IMAGE_TYPE * p_image;
190 XShmSegmentInfo shminfo; /* shared memory zone information */
194 /*****************************************************************************
195 * mwmhints_t: window manager hints
196 *****************************************************************************
197 * Fullscreen needs to be able to hide the wm decorations so we provide
198 * this structure to make it easier.
199 *****************************************************************************/
200 #define MWM_HINTS_DECORATIONS (1L << 1)
201 #define PROP_MWM_HINTS_ELEMENTS 5
202 typedef struct mwmhints_s
211 /*****************************************************************************
213 *****************************************************************************/
214 #ifdef MODULE_NAME_IS_xvideo
215 # define MAX_DIRECTBUFFERS 5
217 # define MAX_DIRECTBUFFERS 2
220 /*****************************************************************************
221 * Seeking function TODO: put this in a generic location !
222 *****************************************************************************/
223 static __inline__ void vout_Seek( off_t i_seek )
225 #define area p_main->p_intf->p_input->stream.p_selected_area
226 off_t i_tell = area->i_tell;
228 i_tell += i_seek * (off_t)50 * p_main->p_intf->p_input->stream.i_mux_rate;
230 i_tell = ( i_tell <= 0/*area->i_start*/ ) ? 0/*area->i_start*/
231 : ( i_tell >= area->i_size ) ? area->i_size
234 input_Seek( p_main->p_intf->p_input, i_tell );
238 /*****************************************************************************
239 * Functions exported as capabilities. They are declared as static so that
240 * we don't pollute the namespace too much.
241 *****************************************************************************/
242 void _M( vout_getfunctions )( function_list_t * p_function_list )
244 p_function_list->pf_probe = vout_Probe;
245 p_function_list->functions.vout.pf_create = vout_Create;
246 p_function_list->functions.vout.pf_init = vout_Init;
247 p_function_list->functions.vout.pf_end = vout_End;
248 p_function_list->functions.vout.pf_destroy = vout_Destroy;
249 p_function_list->functions.vout.pf_manage = vout_Manage;
250 p_function_list->functions.vout.pf_render = vout_Render;
251 p_function_list->functions.vout.pf_display = vout_Display;
254 /*****************************************************************************
255 * vout_Probe: probe the video driver and return a score
256 *****************************************************************************
257 * This function tries to initialize SDL and returns a score to the
258 * plugin manager so that it can select the best plugin.
259 *****************************************************************************/
260 static int vout_Probe( probedata_t *p_data )
262 Display *p_display; /* display pointer */
264 #ifdef MODULE_NAME_IS_xvideo
265 int i_xvport, i_dummy;
268 /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
269 psz_display = XDisplayName( main_GetPszVariable(VOUT_DISPLAY_VAR, NULL) );
270 p_display = XOpenDisplay( psz_display );
271 if( p_display == NULL ) /* error */
273 intf_WarnMsg( 3, "vout: cannot open display %s", psz_display );
277 #ifdef MODULE_NAME_IS_xvideo
278 /* Check that there is an available XVideo port for this format */
279 i_xvport = XVideoGetPort( p_display, p_data->vout.i_chroma, &i_dummy );
282 /* It failed, but it's not completely lost ! We try to open an
283 * XVideo port for a YUY2 picture */
284 i_xvport = XVideoGetPort( p_display, FOURCC_YUY2, &i_dummy );
287 /* It failed, but it's not completely lost ! We try to open an
288 * XVideo port for a simple 16bpp RGB picture */
289 i_xvport = XVideoGetPort( p_display, FOURCC_RV16, &i_dummy );
292 XCloseDisplay( p_display );
297 XVideoReleasePort( p_display, i_xvport );
300 /* Clean-up everyting */
301 XCloseDisplay( p_display );
303 #ifdef MODULE_NAME_IS_xvideo
310 /*****************************************************************************
311 * vout_Create: allocate X11 video thread output method
312 *****************************************************************************
313 * This function allocate and initialize a X11 vout method. It uses some of the
314 * vout properties to choose the window size, and change them according to the
315 * actual properties of the display.
316 *****************************************************************************/
317 static int vout_Create( vout_thread_t *p_vout )
321 /* Allocate structure */
322 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
323 if( p_vout->p_sys == NULL )
325 intf_ErrMsg( "vout error: %s", strerror(ENOMEM) );
329 /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
330 psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
331 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
333 if( p_vout->p_sys->p_display == NULL ) /* error */
335 intf_ErrMsg( "vout error: cannot open display %s", psz_display );
336 free( p_vout->p_sys );
339 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
341 #ifdef MODULE_NAME_IS_xvideo
342 /* Check that we have access to an XVideo port providing this chroma */
343 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout->p_sys->p_display,
344 p_vout->render.i_chroma,
345 &p_vout->output.i_chroma );
346 if( p_vout->p_sys->i_xvport < 0 )
348 /* It failed, but it's not completely lost ! We try to open an
349 * XVideo port for an YUY2 picture. We'll need to do an YUV
350 * conversion, but at least it has got scaling. */
351 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout->p_sys->p_display,
353 &p_vout->output.i_chroma );
354 if( p_vout->p_sys->i_xvport < 0 )
356 /* It failed, but it's not completely lost ! We try to open an
357 * XVideo port for a simple 16bpp RGB picture. We'll need to do
358 * an YUV conversion, but at least it has got scaling. */
359 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout->p_sys->p_display,
361 &p_vout->output.i_chroma );
362 if( p_vout->p_sys->i_xvport < 0 )
364 XCloseDisplay( p_vout->p_sys->p_display );
365 free( p_vout->p_sys );
372 /* Create blank cursor (for mouse cursor autohiding) */
373 p_vout->p_sys->b_mouse_pointer_visible = 1;
374 CreateCursor( p_vout );
376 /* Spawn base window - this window will include the video output window,
377 * but also command buttons, subtitles and other indicators */
378 if( CreateWindow( p_vout ) )
380 intf_ErrMsg( "vout error: cannot create X11 window" );
381 DestroyCursor( p_vout );
382 XCloseDisplay( p_vout->p_sys->p_display );
383 free( p_vout->p_sys );
387 /* Open and initialize device. */
388 if( InitDisplay( p_vout ) )
390 intf_ErrMsg( "vout error: cannot initialize X11 display" );
391 DestroyCursor( p_vout );
392 DestroyWindow( p_vout );
393 XCloseDisplay( p_vout->p_sys->p_display );
394 free( p_vout->p_sys );
398 /* Disable screen saver and return */
399 DisableXScreenSaver( p_vout );
404 /*****************************************************************************
405 * vout_Destroy: destroy X11 video thread output method
406 *****************************************************************************
407 * Terminate an output method created by vout_CreateOutputMethod
408 *****************************************************************************/
409 static void vout_Destroy( vout_thread_t *p_vout )
411 /* Restore cursor if it was blanked */
412 if( !p_vout->p_sys->b_mouse_pointer_visible )
414 ToggleCursor( p_vout );
417 #ifdef MODULE_NAME_IS_xvideo
418 XVideoReleasePort( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport );
421 /* Destroy colormap */
422 if( p_vout->p_sys->i_screen_depth == 8 )
424 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
429 DestroyCursor( p_vout );
430 EnableXScreenSaver( p_vout );
431 DestroyWindow( p_vout );
433 XCloseDisplay( p_vout->p_sys->p_display );
435 /* Destroy structure */
436 free( p_vout->p_sys );
439 /*****************************************************************************
440 * vout_Init: initialize X11 video thread output method
441 *****************************************************************************
442 * This function create the XImages needed by the output thread. It is called
443 * at the beginning of the thread, but also each time the window is resized.
444 *****************************************************************************/
445 static int vout_Init( vout_thread_t *p_vout )
450 I_OUTPUTPICTURES = 0;
452 #ifdef MODULE_NAME_IS_xvideo
453 /* Initialize the output structure; we already found an XVideo port,
454 * and the corresponding chroma we will be using. Since we can
455 * arbitrary scale, stick to the coordinates and aspect. */
456 p_vout->output.i_width = p_vout->render.i_width;
457 p_vout->output.i_height = p_vout->render.i_height;
458 p_vout->output.i_aspect = p_vout->render.i_aspect;
461 /* Initialize the output structure: RGB with square pixels, whatever
462 * the input format is, since it's the only format we know */
463 switch( p_vout->p_sys->i_screen_depth )
465 case 8: /* FIXME: set the palette */
466 p_vout->output.i_chroma = FOURCC_BI_RGB; break;
468 p_vout->output.i_chroma = FOURCC_RV15; break;
470 p_vout->output.i_chroma = FOURCC_RV16; break;
472 p_vout->output.i_chroma = FOURCC_BI_BITFIELDS; break;
474 p_vout->output.i_chroma = FOURCC_BI_BITFIELDS; break;
476 intf_ErrMsg( "vout error: unknown screen depth" );
480 p_vout->output.i_width = p_vout->p_sys->i_width;
481 p_vout->output.i_height = p_vout->p_sys->i_height;
483 /* Assume we have square pixels */
484 p_vout->output.i_aspect = p_vout->p_sys->i_width
485 * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
488 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
489 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
493 /* Find an empty picture slot */
494 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
496 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
498 p_pic = p_vout->p_picture + i_index;
503 /* Allocate the picture */
504 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
509 p_pic->i_status = DESTROYED_PICTURE;
510 p_pic->i_type = DIRECT_PICTURE;
512 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
520 /*****************************************************************************
521 * vout_Render: render previously calculated output
522 *****************************************************************************/
523 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
528 /*****************************************************************************
529 * vout_Display: displays previously rendered output
530 *****************************************************************************
531 * This function sends the currently rendered image to X11 server.
532 * (The Xv extension takes care of "double-buffering".)
533 *****************************************************************************/
534 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
536 int i_width, i_height, i_x, i_y;
538 vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
539 &i_x, &i_y, &i_width, &i_height );
541 if( p_vout->p_sys->b_shm )
543 /* Display rendered image using shared memory extension */
544 #ifdef MODULE_NAME_IS_xvideo
545 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
546 p_vout->p_sys->yuv_window, p_vout->p_sys->gc,
547 p_pic->p_sys->p_image, 0 /*src_x*/, 0 /*src_y*/,
548 p_vout->output.i_width, p_vout->output.i_height,
549 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
550 False /* Don't put True here or you'll waste your CPU */ );
552 XShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->window,
553 p_vout->p_sys->gc, p_pic->p_sys->p_image,
554 0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
555 p_vout->output.i_width, p_vout->output.i_height,
556 False /* Don't put True here ! */ );
561 /* Use standard XPutImage -- this is gonna be slow ! */
562 #ifdef MODULE_NAME_IS_xvideo
563 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
564 p_vout->p_sys->yuv_window, p_vout->p_sys->gc,
565 p_pic->p_sys->p_image, 0 /*src_x*/, 0 /*src_y*/,
566 p_vout->output.i_width, p_vout->output.i_height,
567 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
569 XPutImage( p_vout->p_sys->p_display, p_vout->p_sys->window,
570 p_vout->p_sys->gc, p_pic->p_sys->p_image,
571 0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
572 p_vout->output.i_width, p_vout->output.i_height );
576 /* Make sure the command is sent now */
577 XFlush( p_vout->p_sys->p_display );
580 /*****************************************************************************
581 * vout_Manage: handle X11 events
582 *****************************************************************************
583 * This function should be called regularly by video output thread. It manages
584 * X11 events and allows window resizing. It returns a non null value on
586 *****************************************************************************/
587 static int vout_Manage( vout_thread_t *p_vout )
589 XEvent xevent; /* X11 event */
590 boolean_t b_resized; /* window has been resized */
591 char i_key; /* ISO Latin-1 key */
594 /* Handle X11 events: ConfigureNotify events are parsed to know if the
595 * output window's size changed, MapNotify and UnmapNotify to know if the
596 * window is mapped (and if the display is useful), and ClientMessages
597 * to intercept window destruction requests */
600 while( XCheckWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
601 StructureNotifyMask | KeyPressMask |
602 ButtonPressMask | ButtonReleaseMask |
603 PointerMotionMask | Button1MotionMask , &xevent )
606 /* ConfigureNotify event: prepare */
607 if( (xevent.type == ConfigureNotify)
608 && ((xevent.xconfigure.width != p_vout->p_sys->i_width)
609 || (xevent.xconfigure.height != p_vout->p_sys->i_height)) )
611 /* Update dimensions */
613 p_vout->i_changes |= VOUT_SIZE_CHANGE;
614 p_vout->p_sys->i_width = xevent.xconfigure.width;
615 p_vout->p_sys->i_height = xevent.xconfigure.height;
617 /* MapNotify event: change window status and disable screen saver */
618 else if( xevent.type == MapNotify)
620 if( (p_vout != NULL) && !p_vout->b_active )
622 DisableXScreenSaver( p_vout );
623 p_vout->b_active = 1;
626 /* UnmapNotify event: change window status and enable screen saver */
627 else if( xevent.type == UnmapNotify )
629 if( (p_vout != NULL) && p_vout->b_active )
631 EnableXScreenSaver( p_vout );
632 p_vout->b_active = 0;
636 else if( xevent.type == KeyPress )
638 /* We may have keys like F1 trough F12, ESC ... */
639 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
640 xevent.xkey.keycode, 0 );
641 switch( x_key_symbol )
644 p_main->p_intf->b_die = 1;
647 p_main->p_intf->b_menu_change = 1;
662 input_Seek( p_main->p_intf->p_input,
663 p_main->p_intf->p_input->stream.p_selected_area->i_start );
666 input_Seek( p_main->p_intf->p_input,
667 p_main->p_intf->p_input->stream.p_selected_area->i_size );
676 input_SetStatus( p_main->p_intf->p_input,
677 INPUT_STATUS_PAUSE );
682 * The reason why I use this instead of XK_0 is that
683 * with XLookupString, we don't have to care about
686 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
688 /* FIXME: handle stuff here */
693 p_main->p_intf->b_die = 1;
697 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
700 case '0': network_ChannelJoin( 0 ); break;
701 case '1': network_ChannelJoin( 1 ); break;
702 case '2': network_ChannelJoin( 2 ); break;
703 case '3': network_ChannelJoin( 3 ); break;
704 case '4': network_ChannelJoin( 4 ); break;
705 case '5': network_ChannelJoin( 5 ); break;
706 case '6': network_ChannelJoin( 6 ); break;
707 case '7': network_ChannelJoin( 7 ); break;
708 case '8': network_ChannelJoin( 8 ); break;
709 case '9': network_ChannelJoin( 9 ); break;
712 intf_DbgMsg( "vout: unhandled key '%c' (%i)",
713 (char)i_key, i_key );
721 else if( xevent.type == ButtonPress )
723 switch( ((XButtonEvent *)&xevent)->button )
726 /* In this part we will eventually manage
727 * clicks for DVD navigation for instance. For the
728 * moment just pause the stream. */
729 input_SetStatus( p_main->p_intf->p_input,
730 INPUT_STATUS_PAUSE );
743 else if( xevent.type == ButtonRelease )
745 switch( ((XButtonEvent *)&xevent)->button )
748 /* FIXME: need locking ! */
749 p_main->p_intf->b_menu_change = 1;
754 else if( xevent.type == MotionNotify )
756 p_vout->p_sys->i_time_mouse_last_moved = mdate();
757 if( ! p_vout->p_sys->b_mouse_pointer_visible )
759 ToggleCursor( p_vout );
765 intf_WarnMsg( 3, "vout: unhandled event %d received", xevent.type );
769 #ifdef MODULE_NAME_IS_xvideo
770 /* Handle events for YUV video output sub-window */
771 while( XCheckWindowEvent( p_vout->p_sys->p_display,
772 p_vout->p_sys->yuv_window,
773 ExposureMask, &xevent ) == True )
775 /* Window exposed (only handled if stream playback is paused) */
776 if( xevent.type == Expose )
778 if( ((XExposeEvent *)&xevent)->count == 0 )
780 /* (if this is the last a collection of expose events...) */
781 if( p_main->p_intf->p_input != NULL )
784 p_main->p_intf->p_input->stream.control.i_status )
786 /* XVideoDisplay( p_vout )*/;
794 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
795 * are handled - according to the man pages, the format is always 32
797 while( XCheckTypedEvent( p_vout->p_sys->p_display,
798 ClientMessage, &xevent ) )
800 if( (xevent.xclient.message_type == p_vout->p_sys->wm_protocols)
801 && (xevent.xclient.data.l[0] == p_vout->p_sys->wm_delete_window ) )
803 p_main->p_intf->b_die = 1;
807 intf_DbgMsg( "vout: unhandled ClientMessage received" );
814 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
816 ToggleFullScreen( p_vout );
817 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
822 #ifdef MODULE_NAME_IS_x11
824 * Handle vout window resizing
829 /* If interface window has been resized, change vout size */
830 intf_DbgMsg( "vout: resizing output window" );
831 p_vout->i_width = p_vout->p_sys->i_width;
832 p_vout->i_height = p_vout->p_sys->i_height;
833 p_vout->i_changes |= VOUT_SIZE_CHANGE;
835 else if( (p_vout->i_width != p_vout->p_sys->i_width) ||
836 (p_vout->i_height != p_vout->p_sys->i_height) )
838 /* If video output size has changed, change interface window size */
839 intf_DbgMsg( "vout: resizing output window" );
840 p_vout->p_sys->i_width = p_vout->i_width;
841 p_vout->p_sys->i_height = p_vout->i_height;
842 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
843 p_vout->p_sys->i_width, p_vout->p_sys->i_height );
846 * Color/Grayscale or gamma change: in 8bpp, just change the colormap
848 if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE)
849 && (p_vout->i_screen_depth == 8) )
851 /* FIXME: clear flags ?? */
857 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
859 intf_DbgMsg( "vout info: resizing window" );
860 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
863 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
864 p_vout->i_width, p_vout->i_height );
866 /* Destroy XImages to change their size */
869 /* Recreate XImages. If SysInit failed, the thread can't go on. */
870 if( vout_Init( p_vout ) )
872 intf_ErrMsg( "vout error: cannot resize display" );
876 /* Tell the video output thread that it will need to rebuild YUV
877 * tables. This is needed since conversion buffer size may have
879 p_vout->i_changes |= VOUT_YUV_CHANGE;
880 intf_Msg( "vout: video display resized (%dx%d)",
881 p_vout->i_width, p_vout->i_height);
888 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
890 int i_width, i_height, i_x, i_y;
892 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
894 intf_WarnMsg( 3, "vout: video display resized (%dx%d)",
895 p_vout->p_sys->i_width,
896 p_vout->p_sys->i_height );
898 vout_PlacePicture( p_vout, p_vout->p_sys->i_width,
899 p_vout->p_sys->i_height,
900 &i_x, &i_y, &i_width, &i_height );
902 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
905 XMoveWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
911 /* Autohide Cursour */
912 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
914 /* Hide the mouse automatically */
915 if( p_vout->p_sys->b_mouse_pointer_visible )
917 ToggleCursor( p_vout );
924 /*****************************************************************************
925 * vout_End: terminate X11 video thread output method
926 *****************************************************************************
927 * Destroy the X11 XImages created by vout_Init. It is called at the end of
928 * the thread, but also each time the window is resized.
929 *****************************************************************************/
930 static void vout_End( vout_thread_t *p_vout )
934 /* Free the direct buffers we allocated */
935 for( i_index = I_OUTPUTPICTURES ; i_index ; )
938 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
942 /* following functions are local */
944 /*****************************************************************************
945 * CreateWindow: open and set-up X11 main window
946 *****************************************************************************/
947 static int CreateWindow( vout_thread_t *p_vout )
949 XSizeHints xsize_hints;
950 XSetWindowAttributes xwindow_attributes;
955 boolean_t b_configure_notify;
956 boolean_t b_map_notify;
958 /* Set main window's size */
959 if( p_vout->render.i_height * p_vout->render.i_aspect
960 >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
962 p_vout->p_sys->i_width = p_vout->render.i_height
963 * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
964 p_vout->p_sys->i_height = p_vout->render.i_height;
968 p_vout->p_sys->i_width = p_vout->render.i_width;
969 p_vout->p_sys->i_height = p_vout->render.i_width
970 * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
974 if( p_vout->p_sys->i_width <= 300 && p_vout->p_sys->i_height <= 300 )
976 p_vout->p_sys->i_width <<= 1;
977 p_vout->p_sys->i_height <<= 1;
979 else if( p_vout->p_sys->i_width <= 400
980 && p_vout->p_sys->i_height <= 400 )
982 p_vout->p_sys->i_width += p_vout->p_sys->i_width >> 1;
983 p_vout->p_sys->i_height += p_vout->p_sys->i_height >> 1;
987 /* Prepare window manager hints and properties */
988 xsize_hints.base_width = p_vout->p_sys->i_width;
989 xsize_hints.base_height = p_vout->p_sys->i_height;
990 xsize_hints.flags = PSize;
991 p_vout->p_sys->wm_protocols = XInternAtom( p_vout->p_sys->p_display,
992 "WM_PROTOCOLS", True );
993 p_vout->p_sys->wm_delete_window = XInternAtom( p_vout->p_sys->p_display,
994 "WM_DELETE_WINDOW", True );
996 /* Prepare window attributes */
997 xwindow_attributes.backing_store = Always; /* save the hidden part */
998 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
999 p_vout->p_sys->i_screen);
1000 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
1003 /* Create the window and set hints - the window must receive
1004 * ConfigureNotify events, and until it is displayed, Expose and
1005 * MapNotify events. */
1007 p_vout->p_sys->window =
1008 XCreateWindow( p_vout->p_sys->p_display,
1009 DefaultRootWindow( p_vout->p_sys->p_display ),
1011 p_vout->p_sys->i_width,
1012 p_vout->p_sys->i_height,
1015 CWBackingStore | CWBackPixel | CWEventMask,
1016 &xwindow_attributes );
1018 /* Set window manager hints and properties: size hints, command,
1019 * window's name, and accepted protocols */
1020 XSetWMNormalHints( p_vout->p_sys->p_display, p_vout->p_sys->window,
1022 XSetCommand( p_vout->p_sys->p_display, p_vout->p_sys->window,
1023 p_main->ppsz_argv, p_main->i_argc );
1024 XStoreName( p_vout->p_sys->p_display, p_vout->p_sys->window,
1025 #ifdef MODULE_NAME_IS_x11
1026 VOUT_TITLE " (X11 output)"
1028 VOUT_TITLE " (XVideo output)"
1032 if( (p_vout->p_sys->wm_protocols == None) /* use WM_DELETE_WINDOW */
1033 || (p_vout->p_sys->wm_delete_window == None)
1034 || !XSetWMProtocols( p_vout->p_sys->p_display, p_vout->p_sys->window,
1035 &p_vout->p_sys->wm_delete_window, 1 ) )
1037 /* WM_DELETE_WINDOW is not supported by window manager */
1038 intf_Msg( "vout error: missing or bad window manager" );
1041 /* Creation of a graphic context that doesn't generate a GraphicsExpose
1042 * event when using functions like XCopyArea */
1043 xgcvalues.graphics_exposures = False;
1044 p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display,
1045 p_vout->p_sys->window,
1046 GCGraphicsExposures, &xgcvalues);
1048 /* Send orders to server, and wait until window is displayed - three
1049 * events must be received: a MapNotify event, an Expose event allowing
1050 * drawing in the window, and a ConfigureNotify to get the window
1051 * dimensions. Once those events have been received, only ConfigureNotify
1052 * events need to be received. */
1054 b_configure_notify = 0;
1056 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
1059 XNextEvent( p_vout->p_sys->p_display, &xevent);
1060 if( (xevent.type == Expose)
1061 && (xevent.xexpose.window == p_vout->p_sys->window) )
1065 else if( (xevent.type == MapNotify)
1066 && (xevent.xmap.window == p_vout->p_sys->window) )
1070 else if( (xevent.type == ConfigureNotify)
1071 && (xevent.xconfigure.window == p_vout->p_sys->window) )
1073 b_configure_notify = 1;
1074 p_vout->p_sys->i_width = xevent.xconfigure.width;
1075 p_vout->p_sys->i_height = xevent.xconfigure.height;
1077 } while( !( b_expose && b_configure_notify && b_map_notify ) );
1079 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
1080 StructureNotifyMask | KeyPressMask |
1081 ButtonPressMask | ButtonReleaseMask |
1082 PointerMotionMask );
1084 #ifdef MODULE_NAME_IS_x11
1085 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1087 /* Allocate a new palette */
1088 p_vout->p_sys->colormap =
1089 XCreateColormap( p_vout->p_sys->p_display,
1090 DefaultRootWindow( p_vout->p_sys->p_display ),
1091 DefaultVisual( p_vout->p_sys->p_display,
1092 p_vout->p_sys->i_screen ),
1095 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1096 XChangeWindowAttributes( p_vout->p_sys->p_display,
1097 p_vout->p_sys->window,
1098 CWColormap, &xwindow_attributes );
1102 /* Create YUV output sub-window. */
1103 p_vout->p_sys->yuv_window = XCreateSimpleWindow( p_vout->p_sys->p_display,
1104 p_vout->p_sys->window, 0, 0,
1105 p_vout->p_sys->i_width,
1106 p_vout->p_sys->i_height,
1108 BlackPixel( p_vout->p_sys->p_display,
1109 p_vout->p_sys->i_screen ),
1110 WhitePixel( p_vout->p_sys->p_display,
1111 p_vout->p_sys->i_screen ) );
1113 XSetWindowBackground( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
1114 BlackPixel(p_vout->p_sys->p_display, p_vout->p_sys->i_screen ) );
1116 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window );
1117 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
1121 /* If the cursor was formerly blank than blank it again */
1122 if( !p_vout->p_sys->b_mouse_pointer_visible )
1124 ToggleCursor( p_vout );
1125 ToggleCursor( p_vout );
1128 XSync( p_vout->p_sys->p_display, False );
1130 /* At this stage, the window is open, displayed, and ready to
1136 /*****************************************************************************
1137 * DestroyWindow: destroy the window
1138 *****************************************************************************
1140 *****************************************************************************/
1141 static void DestroyWindow( vout_thread_t *p_vout )
1143 XSync( p_vout->p_sys->p_display, False );
1145 #ifdef MODULE_NAME_IS_xvideo
1146 XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window );
1149 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
1150 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
1151 XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
1154 /*****************************************************************************
1155 * NewPicture: allocate a picture
1156 *****************************************************************************
1157 * Returns 0 on success, -1 otherwise
1158 *****************************************************************************/
1159 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1161 /* We know the chroma, allocate a buffer which will be used
1162 * directly by the decoder */
1163 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1165 if( p_pic->p_sys == NULL )
1170 if( p_vout->p_sys->b_shm )
1172 /* Create image using XShm extension */
1173 p_pic->p_sys->p_image =
1174 CreateShmImage( p_vout->p_sys->p_display,
1175 #ifdef MODULE_NAME_IS_xvideo
1176 p_vout->p_sys->i_xvport,
1177 p_vout->output.i_chroma,
1179 p_vout->p_sys->p_visual,
1180 p_vout->p_sys->i_screen_depth,
1182 &p_pic->p_sys->shminfo,
1183 p_vout->output.i_width, p_vout->output.i_height );
1187 /* Create image without XShm extension */
1188 p_pic->p_sys->p_image =
1189 CreateImage( p_vout->p_sys->p_display,
1190 #ifdef MODULE_NAME_IS_xvideo
1191 p_vout->p_sys->i_xvport,
1192 p_vout->output.i_chroma,
1194 p_vout->p_sys->p_visual,
1195 p_vout->p_sys->i_screen_depth,
1196 p_vout->p_sys->i_bytes_per_pixel,
1198 p_vout->output.i_width, p_vout->output.i_height );
1201 if( p_pic->p_sys->p_image == NULL )
1203 free( p_pic->p_sys );
1207 switch( p_vout->output.i_chroma )
1209 #ifdef MODULE_NAME_IS_xvideo
1212 p_pic->Y_PIXELS = p_pic->p_sys->p_image->data
1213 + p_pic->p_sys->p_image->offsets[0];
1214 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1215 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[0];
1216 p_pic->p[Y_PLANE].i_pixel_bytes = 1;
1217 p_pic->p[Y_PLANE].b_margin = 0;
1219 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1220 + p_pic->p_sys->p_image->offsets[1];
1221 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1222 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[1];
1223 p_pic->p[Y_PLANE].i_pixel_bytes = 1;
1224 p_pic->p[Y_PLANE].b_margin = 0;
1226 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1227 + p_pic->p_sys->p_image->offsets[2];
1228 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1229 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[2];
1230 p_pic->p[Y_PLANE].i_pixel_bytes = 1;
1231 p_pic->p[Y_PLANE].b_margin = 0;
1233 p_pic->i_planes = 3;
1238 p_pic->Y_PIXELS = p_pic->p_sys->p_image->data
1239 + p_pic->p_sys->p_image->offsets[0];
1240 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1241 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[0];
1242 p_pic->p[Y_PLANE].i_pixel_bytes = 1;
1243 p_pic->p[Y_PLANE].b_margin = 0;
1245 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1246 + p_pic->p_sys->p_image->offsets[2];
1247 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1248 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[2];
1249 p_pic->p[Y_PLANE].i_pixel_bytes = 1;
1250 p_pic->p[Y_PLANE].b_margin = 0;
1252 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1253 + p_pic->p_sys->p_image->offsets[1];
1254 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1255 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[1];
1256 p_pic->p[Y_PLANE].i_pixel_bytes = 1;
1257 p_pic->p[Y_PLANE].b_margin = 0;
1259 p_pic->i_planes = 3;
1264 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1265 + p_pic->p_sys->p_image->offsets[0];
1266 p_pic->p->i_lines = p_vout->output.i_height;
1267 /* XXX: this just looks so plain wrong... check it out ! */
1268 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0] / 4;
1269 p_pic->p->i_pixel_bytes = 4;
1270 p_pic->p->b_margin = 0;
1272 p_pic->i_planes = 1;
1278 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1279 + p_pic->p_sys->p_image->offsets[0];
1280 p_pic->p->i_lines = p_vout->output.i_height;
1281 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1282 p_pic->p->i_pixel_bytes = 4;
1283 p_pic->p->b_margin = 0;
1285 p_pic->i_planes = 1;
1290 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1291 + p_pic->p_sys->p_image->offsets[0];
1292 p_pic->p->i_lines = p_vout->output.i_height;
1293 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1294 p_pic->p->i_pixel_bytes = 2;
1295 p_pic->p->b_margin = 0;
1297 p_pic->p->i_red_mask = 0x001f;
1298 p_pic->p->i_green_mask = 0x07e0;
1299 p_pic->p->i_blue_mask = 0xf800;
1301 p_pic->i_planes = 1;
1306 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1307 + p_pic->p_sys->p_image->offsets[0];
1308 p_pic->p->i_lines = p_vout->output.i_height;
1309 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1310 p_pic->p->i_pixel_bytes = 2;
1311 p_pic->p->b_margin = 0;
1313 p_pic->p->i_red_mask = 0x001f;
1314 p_pic->p->i_green_mask = 0x03e0;
1315 p_pic->p->i_blue_mask = 0x7c00;
1317 p_pic->i_planes = 1;
1323 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1324 + p_pic->p_sys->p_image->xoffset;
1325 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1326 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1327 p_pic->p->i_pixel_bytes = p_pic->p_sys->p_image->depth;
1329 if( p_pic->p->i_pitch == 2 * p_pic->p_sys->p_image->width )
1331 p_pic->p->b_margin = 0;
1335 p_pic->p->b_margin = 1;
1336 p_pic->p->b_hidden = 1;
1337 p_pic->p->i_visible_bytes = 2 * p_pic->p_sys->p_image->width;
1340 p_pic->p->i_red_mask = p_pic->p_sys->p_image->red_mask;
1341 p_pic->p->i_green_mask = p_pic->p_sys->p_image->green_mask;
1342 p_pic->p->i_blue_mask = p_pic->p_sys->p_image->blue_mask;
1344 p_pic->i_planes = 1;
1350 /* Unknown chroma, tell the guy to get lost */
1351 IMAGE_FREE( p_pic->p_sys->p_image );
1352 free( p_pic->p_sys );
1353 intf_ErrMsg( "vout error: never heard of chroma 0x%.8x (%4.4s)",
1354 p_vout->output.i_chroma,
1355 (char*)&p_vout->output.i_chroma );
1356 p_pic->i_planes = 0;
1363 /*****************************************************************************
1364 * FreePicture: destroy a picture allocated with NewPicture
1365 *****************************************************************************
1366 * Destroy XImage AND associated data. If using Shm, detach shared memory
1367 * segment from server and process, then free it. The XDestroyImage manpage
1368 * says that both the image structure _and_ the data pointed to by the
1369 * image structure are freed, so no need to free p_image->data.
1370 *****************************************************************************/
1371 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1373 /* The order of operations is correct */
1374 if( p_vout->p_sys->b_shm )
1376 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
1377 IMAGE_FREE( p_pic->p_sys->p_image );
1379 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
1380 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
1382 intf_ErrMsg( "vout error: cannot detach shared memory (%s)",
1388 IMAGE_FREE( p_pic->p_sys->p_image );
1391 XSync( p_vout->p_sys->p_display, False );
1393 free( p_pic->p_sys );
1396 /*****************************************************************************
1397 * ToggleFullScreen: Enable or disable full screen mode
1398 *****************************************************************************
1399 * This function will switch between fullscreen and window mode.
1401 *****************************************************************************/
1402 static void ToggleFullScreen ( vout_thread_t *p_vout )
1405 mwmhints_t mwmhints;
1406 int i_xpos, i_ypos, i_width, i_height;
1408 p_vout->b_fullscreen = !p_vout->b_fullscreen;
1410 if( p_vout->b_fullscreen )
1412 Window next_parent, parent, *p_dummy, dummy1;
1413 unsigned int dummy2, dummy3;
1415 intf_WarnMsg( 3, "vout: entering fullscreen mode" );
1417 /* Save current window coordinates so they can be restored when
1418 * we exit from fullscreen mode */
1420 /* find the real parent, which means the which is a direct child of
1421 * the root window */
1422 next_parent = parent = p_vout->p_sys->window;
1423 while( next_parent != DefaultRootWindow( p_vout->p_sys->p_display ) )
1425 parent = next_parent;
1426 XQueryTree( p_vout->p_sys->p_display,
1432 XFree((void *)p_dummy);
1435 XGetGeometry( p_vout->p_sys->p_display,
1436 p_vout->p_sys->window,
1440 &p_vout->p_sys->i_width_backup,
1441 &p_vout->p_sys->i_height_backup,
1444 XTranslateCoordinates( p_vout->p_sys->p_display,
1446 DefaultRootWindow( p_vout->p_sys->p_display ),
1449 &p_vout->p_sys->i_xpos_backup,
1450 &p_vout->p_sys->i_ypos_backup,
1453 mwmhints.flags = MWM_HINTS_DECORATIONS;
1454 mwmhints.decorations = 0;
1458 i_width = DisplayWidth( p_vout->p_sys->p_display,
1459 p_vout->p_sys->i_screen );
1460 i_height = DisplayHeight( p_vout->p_sys->p_display,
1461 p_vout->p_sys->i_screen );
1464 /* Being a transient window allows us to really be fullscreen (display
1465 * over the taskbar for instance) but then we end-up with the same
1466 * result as with the brute force method */
1467 XSetTransientForHint( p_vout->p_sys->p_display,
1468 p_vout->p_sys->window, None );
1473 intf_WarnMsg( 3, "vout: leaving fullscreen mode" );
1475 mwmhints.flags = MWM_HINTS_DECORATIONS;
1476 mwmhints.decorations = 1;
1478 i_xpos = p_vout->p_sys->i_xpos_backup;
1479 i_ypos = p_vout->p_sys->i_ypos_backup;
1480 i_width = p_vout->p_sys->i_width_backup;
1481 i_height = p_vout->p_sys->i_height_backup;
1484 /* To my knowledge there are two ways to create a borderless window.
1485 * There's the generic way which is to tell x to bypass the window manager,
1486 * but this creates problems with the focus of other applications.
1487 * The other way is to use the motif property "_MOTIF_WM_HINTS" which
1488 * luckily seems to be supported by most window managers.
1490 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1492 XChangeProperty( p_vout->p_sys->p_display, p_vout->p_sys->window,
1493 prop, prop, 32, PropModeReplace,
1494 (unsigned char *)&mwmhints,
1495 PROP_MWM_HINTS_ELEMENTS );
1496 #if 0 /* brute force way to remove decorations */
1497 XSetWindowAttributes attributes;
1498 attributes.override_redirect = True;
1499 XChangeWindowAttributes( p_vout->p_sys->p_display,
1500 p_vout->p_sys->window,
1505 /* We need to unmap and remap the window if we want the window
1506 * manager to take our changes into effect */
1507 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
1508 XMapRaised( p_vout->p_sys->p_display, p_vout->p_sys->window);
1509 XMoveResizeWindow( p_vout->p_sys->p_display,
1510 p_vout->p_sys->window,
1515 XFlush( p_vout->p_sys->p_display );
1518 /*****************************************************************************
1519 * EnableXScreenSaver: enable screen saver
1520 *****************************************************************************
1521 * This function enables the screen saver on a display after it has been
1522 * disabled by XDisableScreenSaver.
1523 * FIXME: what happens if multiple vlc sessions are running at the same
1525 *****************************************************************************/
1526 static void EnableXScreenSaver( vout_thread_t *p_vout )
1530 intf_DbgMsg( "vout: enabling screen saver" );
1531 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1532 p_vout->p_sys->i_ss_interval,
1533 p_vout->p_sys->i_ss_blanking,
1534 p_vout->p_sys->i_ss_exposure );
1536 /* Restore DPMS settings */
1537 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1539 if( p_vout->p_sys->b_ss_dpms )
1541 DPMSEnable( p_vout->p_sys->p_display );
1546 /*****************************************************************************
1547 * DisableXScreenSaver: disable screen saver
1548 *****************************************************************************
1549 * See XEnableXScreenSaver
1550 *****************************************************************************/
1551 static void DisableXScreenSaver( vout_thread_t *p_vout )
1555 /* Save screen saver informations */
1556 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1557 &p_vout->p_sys->i_ss_interval,
1558 &p_vout->p_sys->i_ss_blanking,
1559 &p_vout->p_sys->i_ss_exposure );
1561 /* Disable screen saver */
1562 intf_DbgMsg( "vout: disabling screen saver" );
1563 XSetScreenSaver( p_vout->p_sys->p_display, 0,
1564 p_vout->p_sys->i_ss_interval,
1565 p_vout->p_sys->i_ss_blanking,
1566 p_vout->p_sys->i_ss_exposure );
1569 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1572 /* Save DPMS current state */
1573 DPMSInfo( p_vout->p_sys->p_display, &dummy,
1574 &p_vout->p_sys->b_ss_dpms );
1575 intf_DbgMsg( "vout: disabling DPMS" );
1576 DPMSDisable( p_vout->p_sys->p_display );
1580 /*****************************************************************************
1581 * CreateCursor: create a blank mouse pointer
1582 *****************************************************************************/
1583 static void CreateCursor( vout_thread_t *p_vout )
1585 XColor cursor_color;
1587 p_vout->p_sys->cursor_pixmap =
1588 XCreatePixmap( p_vout->p_sys->p_display,
1589 DefaultRootWindow( p_vout->p_sys->p_display ),
1592 XParseColor( p_vout->p_sys->p_display,
1593 XCreateColormap( p_vout->p_sys->p_display,
1595 p_vout->p_sys->p_display ),
1597 p_vout->p_sys->p_display,
1598 p_vout->p_sys->i_screen ),
1600 "black", &cursor_color );
1602 p_vout->p_sys->blank_cursor =
1603 XCreatePixmapCursor( p_vout->p_sys->p_display,
1604 p_vout->p_sys->cursor_pixmap,
1605 p_vout->p_sys->cursor_pixmap,
1606 &cursor_color, &cursor_color, 1, 1 );
1609 /*****************************************************************************
1610 * DestroyCursor: destroy the blank mouse pointer
1611 *****************************************************************************/
1612 static void DestroyCursor( vout_thread_t *p_vout )
1614 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
1617 /*****************************************************************************
1618 * ToggleCursor: hide or show the mouse pointer
1619 *****************************************************************************
1620 * This function hides the X pointer if it is visible by setting the pointer
1621 * sprite to a blank one. To show it again, we disable the sprite.
1622 *****************************************************************************/
1623 static void ToggleCursor( vout_thread_t *p_vout )
1625 if( p_vout->p_sys->b_mouse_pointer_visible )
1627 XDefineCursor( p_vout->p_sys->p_display,
1628 p_vout->p_sys->window,
1629 p_vout->p_sys->blank_cursor );
1630 p_vout->p_sys->b_mouse_pointer_visible = 0;
1634 XUndefineCursor( p_vout->p_sys->p_display, p_vout->p_sys->window );
1635 p_vout->p_sys->b_mouse_pointer_visible = 1;
1639 #ifdef MODULE_NAME_IS_xvideo
1640 /*****************************************************************************
1641 * XVideoGetPort: get YUV12 port
1642 *****************************************************************************/
1643 static int XVideoGetPort( Display *dpy, u32 i_chroma, u32 *pi_newchroma )
1645 XvAdaptorInfo *p_adaptor;
1647 int i_adaptor, i_num_adaptors, i_requested_adaptor;
1648 int i_selected_port;
1650 switch( XvQueryExtension( dpy, &i, &i, &i, &i, &i ) )
1655 case XvBadExtension:
1656 intf_WarnMsg( 3, "vout error: XvBadExtension" );
1660 intf_WarnMsg( 3, "vout error: XvBadAlloc" );
1664 intf_WarnMsg( 3, "vout error: XvQueryExtension failed" );
1668 switch( XvQueryAdaptors( dpy, DefaultRootWindow( dpy ),
1669 &i_num_adaptors, &p_adaptor ) )
1674 case XvBadExtension:
1675 intf_WarnMsg( 3, "vout error: XvBadExtension for XvQueryAdaptors" );
1679 intf_WarnMsg( 3, "vout error: XvBadAlloc for XvQueryAdaptors" );
1683 intf_WarnMsg( 3, "vout error: XvQueryAdaptors failed" );
1687 i_selected_port = -1;
1688 i_requested_adaptor = main_GetIntVariable( VOUT_XVADAPTOR_VAR, -1 );
1690 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
1692 XvImageFormatValues *p_formats;
1693 int i_format, i_num_formats;
1696 /* If we requested an adaptor and it's not this one, we aren't
1698 if( i_requested_adaptor != -1 && i_adaptor != i_requested_adaptor )
1703 /* If the adaptor doesn't have the required properties, skip it */
1704 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
1705 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
1710 /* Check that adaptor supports our requested format... */
1711 p_formats = XvListImageFormats( dpy, p_adaptor[i_adaptor].base_id,
1715 i_format < i_num_formats && ( i_selected_port == -1 );
1718 XvEncodingInfo *p_enc;
1719 int i_enc, i_num_encodings;
1720 XvAttribute *p_attr;
1721 int i_attr, i_num_attributes;
1723 /* If this is not the format we want, or at least a
1724 * similar one, forget it */
1725 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
1730 /* Look for the first available port supporting this format */
1731 for( i_port = p_adaptor[i_adaptor].base_id;
1732 ( i_port < p_adaptor[i_adaptor].base_id
1733 + p_adaptor[i_adaptor].num_ports )
1734 && ( i_selected_port == -1 );
1737 if( XvGrabPort( dpy, i_port, CurrentTime ) == Success )
1739 i_selected_port = i_port;
1740 *pi_newchroma = p_formats[ i_format ].id;
1744 /* If no free port was found, forget it */
1745 if( i_selected_port == -1 )
1750 /* If we found a port, print information about it */
1751 intf_WarnMsg( 3, "vout: found adaptor %i, port %i, "
1752 "image format 0x%x (%4.4s) %s",
1753 i_adaptor, i_selected_port,
1754 p_formats[ i_format ].id,
1755 (char *)&p_formats[ i_format ].id,
1756 ( p_formats[ i_format ].format
1757 == XvPacked ) ? "packed" : "planar" );
1759 intf_WarnMsg( 10, " encoding list:" );
1761 if( XvQueryEncodings( dpy, i_selected_port,
1762 &i_num_encodings, &p_enc )
1765 intf_WarnMsg( 10, " XvQueryEncodings failed" );
1769 for( i_enc = 0; i_enc < i_num_encodings; i_enc++ )
1771 intf_WarnMsg( 10, " id=%ld, name=%s, size=%ldx%ld,"
1772 " numerator=%d, denominator=%d",
1773 p_enc[i_enc].encoding_id, p_enc[i_enc].name,
1774 p_enc[i_enc].width, p_enc[i_enc].height,
1775 p_enc[i_enc].rate.numerator,
1776 p_enc[i_enc].rate.denominator );
1781 XvFreeEncodingInfo( p_enc );
1784 intf_WarnMsg( 10, " attribute list:" );
1785 p_attr = XvQueryPortAttributes( dpy, i_selected_port,
1786 &i_num_attributes );
1787 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
1790 " name=%s, flags=[%s%s ], min=%i, max=%i",
1791 p_attr[i_attr].name,
1792 (p_attr[i_attr].flags & XvGettable) ? " get" : "",
1793 (p_attr[i_attr].flags & XvSettable) ? " set" : "",
1794 p_attr[i_attr].min_value, p_attr[i_attr].max_value );
1797 if( p_attr != NULL )
1803 if( p_formats != NULL )
1810 if( i_num_adaptors > 0 )
1812 XvFreeAdaptorInfo( p_adaptor );
1815 if( i_selected_port == -1 )
1817 if( i_requested_adaptor == -1 )
1819 intf_WarnMsg( 3, "vout: no free XVideo port found for format "
1820 "0x%.8x (%4.4s)", i_chroma, (char*)&i_chroma );
1824 intf_WarnMsg( 3, "vout: XVideo adaptor %i does not have a free "
1825 "XVideo port for format 0x%.8x (%4.4s)",
1826 i_requested_adaptor, i_chroma, (char*)&i_chroma );
1830 return( i_selected_port );
1833 /*****************************************************************************
1834 * XVideoReleasePort: release YUV12 port
1835 *****************************************************************************/
1836 static void XVideoReleasePort( Display *dpy, int i_port )
1838 XvUngrabPort( dpy, i_port, CurrentTime );
1842 /*****************************************************************************
1843 * InitDisplay: open and initialize X11 device
1844 *****************************************************************************
1845 * Create a window according to video output given size, and set other
1846 * properties according to the display properties.
1847 *****************************************************************************/
1848 static int InitDisplay( vout_thread_t *p_vout )
1850 #ifdef MODULE_NAME_IS_x11
1851 XPixmapFormatValues * p_formats; /* pixmap formats */
1852 XVisualInfo * p_xvisual; /* visuals informations */
1853 XVisualInfo xvisual_template; /* visual template */
1854 int i_count; /* array size */
1858 /* FIXME : As of 2001-03-16, XFree4 for MacOS X does not support Xshm. */
1859 p_vout->p_sys->b_shm = 0;
1861 p_vout->p_sys->b_shm = ( XShmQueryExtension( p_vout->p_sys->p_display )
1864 if( !p_vout->p_sys->b_shm )
1866 intf_WarnMsg( 1, "vout warning: XShm video extension is unavailable" );
1869 #ifdef MODULE_NAME_IS_xvideo
1870 /* XXX The brightness and contrast values should be read from environment
1871 * XXX variables... */
1873 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
1874 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
1878 #ifdef MODULE_NAME_IS_x11
1879 /* Initialize structure */
1880 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
1882 /* Get screen depth */
1883 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
1884 p_vout->p_sys->i_screen );
1885 switch( p_vout->p_sys->i_screen_depth )
1889 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
1891 xvisual_template.screen = p_vout->p_sys->i_screen;
1892 xvisual_template.class = DirectColor;
1893 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1894 VisualScreenMask | VisualClassMask,
1895 &xvisual_template, &i_count );
1896 if( p_xvisual == NULL )
1898 intf_ErrMsg( "vout error: no PseudoColor visual available" );
1901 p_vout->p_sys->i_bytes_per_pixel = 1;
1908 * Screen depth is higher than 8bpp. TrueColor visual is used.
1910 xvisual_template.screen = p_vout->p_sys->i_screen;
1911 xvisual_template.class = TrueColor;
1912 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1913 VisualScreenMask | VisualClassMask,
1914 &xvisual_template, &i_count );
1915 if( p_xvisual == NULL )
1917 intf_ErrMsg( "vout error: no TrueColor visual available" );
1920 p_vout->p_sys->i_red_mask = p_xvisual->red_mask;
1921 p_vout->p_sys->i_green_mask = p_xvisual->green_mask;
1922 p_vout->p_sys->i_blue_mask = p_xvisual->blue_mask;
1924 /* There is no difference yet between 3 and 4 Bpp. The only way
1925 * to find the actual number of bytes per pixel is to list supported
1926 * pixmap formats. */
1927 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
1928 p_vout->p_sys->i_bytes_per_pixel = 0;
1930 for( ; i_count-- ; p_formats++ )
1932 /* Under XFree4.0, the list contains pixmap formats available
1933 * through all video depths ; so we have to check against current
1935 if( p_formats->depth == p_vout->p_sys->i_screen_depth )
1937 if( p_formats->bits_per_pixel / 8
1938 > p_vout->p_sys->i_bytes_per_pixel )
1940 p_vout->p_sys->i_bytes_per_pixel = p_formats->bits_per_pixel / 8;
1946 p_vout->p_sys->p_visual = p_xvisual->visual;
1953 /*****************************************************************************
1954 * CreateShmImage: create an XImage or XvImage using shared memory extension
1955 *****************************************************************************
1956 * Prepare an XImage or XvImage for display function.
1957 * The order of the operations respects the recommandations of the mit-shm
1958 * document by J.Corbet and K.Packard. Most of the parameters were copied from
1959 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
1960 *****************************************************************************/
1961 static IMAGE_TYPE * CreateShmImage( Display* p_display, EXTRA_ARGS_SHM,
1962 int i_width, int i_height )
1964 IMAGE_TYPE *p_image;
1966 /* Create XImage / XvImage */
1967 #ifdef MODULE_NAME_IS_xvideo
1968 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
1969 i_width, i_height, p_shm );
1971 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
1972 p_shm, i_width, i_height );
1974 if( p_image == NULL )
1976 intf_ErrMsg( "vout error: image creation failed." );
1980 /* Allocate shared memory segment - 0776 set the access permission
1981 * rights (like umask), they are not yet supported by all X servers */
1982 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
1983 if( p_shm->shmid < 0 )
1985 intf_ErrMsg( "vout error: cannot allocate shared image data (%s)",
1986 strerror( errno ) );
1987 IMAGE_FREE( p_image );
1991 /* Attach shared memory segment to process (read/write) */
1992 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
1993 if(! p_shm->shmaddr )
1995 intf_ErrMsg( "vout error: cannot attach shared memory (%s)",
1997 IMAGE_FREE( p_image );
1998 shmctl( p_shm->shmid, IPC_RMID, 0 );
2002 /* Read-only data. We won't be using XShmGetImage */
2003 p_shm->readOnly = True;
2005 /* Attach shared memory segment to X server */
2006 if( XShmAttach( p_display, p_shm ) == False )
2008 intf_ErrMsg( "vout error: cannot attach shared memory to X server" );
2009 IMAGE_FREE( p_image );
2010 shmctl( p_shm->shmid, IPC_RMID, 0 );
2011 shmdt( p_shm->shmaddr );
2015 /* Send image to X server. This instruction is required, since having
2016 * built a Shm XImage and not using it causes an error on XCloseDisplay */
2017 XSync( p_display, False );
2020 /* Mark the shm segment to be removed when there are no more
2021 * attachements, so it is automatic on process exit or after shmdt */
2022 shmctl( p_shm->shmid, IPC_RMID, 0 );
2028 /*****************************************************************************
2029 * CreateImage: create an XImage or XvImage
2030 *****************************************************************************
2031 * Create a simple image used as a buffer.
2032 *****************************************************************************/
2033 static IMAGE_TYPE * CreateImage( Display *p_display, EXTRA_ARGS,
2034 int i_width, int i_height )
2036 byte_t * p_data; /* image data storage zone */
2037 IMAGE_TYPE *p_image;
2038 #ifdef MODULE_NAME_IS_x11
2039 int i_quantum; /* XImage quantum (see below) */
2040 int i_bytes_per_line;
2043 /* Allocate memory for image */
2044 #ifdef MODULE_NAME_IS_xvideo
2045 p_data = (byte_t *) malloc( i_width * i_height * 2 ); /* XXX */
2047 i_bytes_per_line = i_width * i_bytes_per_pixel;
2048 p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
2052 intf_ErrMsg( "vout error: %s", strerror(ENOMEM));
2056 #ifdef MODULE_NAME_IS_x11
2057 /* Optimize the quantum of a scanline regarding its size - the quantum is
2058 a diviser of the number of bits between the start of two scanlines. */
2059 if( i_bytes_per_line & 0xf )
2063 else if( i_bytes_per_line & 0x10 )
2073 /* Create XImage. p_data will be automatically freed */
2074 #ifdef MODULE_NAME_IS_xvideo
2075 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2076 p_data, i_width, i_height );
2078 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2079 p_data, i_width, i_height, i_quantum, 0 );
2081 if( p_image == NULL )
2083 intf_ErrMsg( "vout error: XCreateImage() failed" );