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.33 2003/10/24 21:27:06 gbazin Exp $
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * David Kennedy <dkennedy@tinytoad.com>
10 * Gildas Bazin <gbazin@netcourrier.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <errno.h> /* ENOMEM */
31 #include <stdlib.h> /* free() */
32 #include <string.h> /* strerror() */
38 #ifdef HAVE_MACHINE_PARAM_H
40 # include <machine/param.h>
41 # include <sys/types.h> /* typedef ushort */
46 # include <netinet/in.h> /* BSD: struct in_addr */
50 # include <sys/shm.h> /* shmget(), shmctl() */
55 #include <X11/Xutil.h>
56 #include <X11/keysym.h>
58 # include <X11/extensions/XShm.h>
60 #ifdef DPMSINFO_IN_DPMS_H
61 # include <X11/extensions/dpms.h>
64 #ifdef MODULE_NAME_IS_xvideo
65 # include <X11/extensions/Xv.h>
66 # include <X11/extensions/Xvlib.h>
70 # include <X11/extensions/Xinerama.h>
75 /*****************************************************************************
77 *****************************************************************************/
78 int E_(Activate) ( vlc_object_t * );
79 void E_(Deactivate) ( vlc_object_t * );
81 static int InitVideo ( vout_thread_t * );
82 static void EndVideo ( vout_thread_t * );
83 static void DisplayVideo ( vout_thread_t *, picture_t * );
84 static int ManageVideo ( vout_thread_t * );
86 static int InitDisplay ( vout_thread_t * );
88 static int CreateWindow ( vout_thread_t *, x11_window_t * );
89 static void DestroyWindow ( vout_thread_t *, x11_window_t * );
91 static int NewPicture ( vout_thread_t *, picture_t * );
92 static void FreePicture ( vout_thread_t *, picture_t * );
94 static IMAGE_TYPE *CreateImage ( vout_thread_t *,
95 Display *, EXTRA_ARGS, int, int );
97 static IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
98 Display *, EXTRA_ARGS_SHM, int, int );
101 static void ToggleFullScreen ( vout_thread_t * );
103 static void EnableXScreenSaver ( vout_thread_t * );
104 static void DisableXScreenSaver ( vout_thread_t * );
106 static void CreateCursor ( vout_thread_t * );
107 static void DestroyCursor ( vout_thread_t * );
108 static void ToggleCursor ( vout_thread_t * );
110 #ifdef MODULE_NAME_IS_xvideo
111 static int XVideoGetPort ( vout_thread_t *, vlc_fourcc_t, vlc_fourcc_t * );
112 static void XVideoReleasePort( vout_thread_t *, int );
115 #ifdef MODULE_NAME_IS_x11
116 static void SetPalette ( vout_thread_t *,
117 uint16_t *, uint16_t *, uint16_t * );
120 static void TestNetWMSupport( vout_thread_t * );
122 /*****************************************************************************
123 * Activate: allocate X11 video thread output method
124 *****************************************************************************
125 * This function allocate and initialize a X11 vout method. It uses some of the
126 * vout properties to choose the window size, and change them according to the
127 * actual properties of the display.
128 *****************************************************************************/
129 int E_(Activate) ( vlc_object_t *p_this )
131 vout_thread_t *p_vout = (vout_thread_t *)p_this;
133 #ifdef MODULE_NAME_IS_xvideo
135 vlc_fourcc_t i_chroma = 0;
136 vlc_bool_t b_chroma = 0;
139 p_vout->pf_init = InitVideo;
140 p_vout->pf_end = EndVideo;
141 p_vout->pf_manage = ManageVideo;
142 p_vout->pf_render = NULL;
143 p_vout->pf_display = DisplayVideo;
145 /* Allocate structure */
146 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
147 if( p_vout->p_sys == NULL )
149 msg_Err( p_vout, "out of memory" );
153 /* Open display, using the "display" config variable or the DISPLAY
154 * environment variable */
155 psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
157 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
159 if( p_vout->p_sys->p_display == NULL ) /* error */
161 msg_Err( p_vout, "cannot open display %s",
162 XDisplayName( psz_display ) );
163 free( p_vout->p_sys );
164 if( psz_display ) free( psz_display );
167 if( psz_display ) free( psz_display );
169 /* Get a screen ID matching the XOpenDisplay return value */
170 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
172 #ifdef MODULE_NAME_IS_xvideo
173 psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
176 if( strlen( psz_chroma ) >= 4 )
178 /* Do not use direct assignment because we are not sure of the
180 memcpy(&i_chroma, psz_chroma, 4);
189 msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
190 i_chroma, (char*)&i_chroma );
194 i_chroma = p_vout->render.i_chroma;
197 /* Check that we have access to an XVideo port providing this chroma */
198 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, VLC2X11_FOURCC(i_chroma),
199 &p_vout->output.i_chroma );
200 if( p_vout->p_sys->i_xvport < 0 )
202 /* If a specific chroma format was requested, then we don't try to
203 * be cleverer than the user. He knew pretty well what he wanted. */
206 XCloseDisplay( p_vout->p_sys->p_display );
207 free( p_vout->p_sys );
211 /* It failed, but it's not completely lost ! We try to open an
212 * XVideo port for an YUY2 picture. We'll need to do an YUV
213 * conversion, but at least it has got scaling. */
214 p_vout->p_sys->i_xvport =
215 XVideoGetPort( p_vout, X11_FOURCC('Y','U','Y','2'),
216 &p_vout->output.i_chroma );
217 if( p_vout->p_sys->i_xvport < 0 )
219 /* It failed, but it's not completely lost ! We try to open an
220 * XVideo port for a simple 16bpp RGB picture. We'll need to do
221 * an YUV conversion, but at least it has got scaling. */
222 p_vout->p_sys->i_xvport =
223 XVideoGetPort( p_vout, X11_FOURCC('R','V','1','6'),
224 &p_vout->output.i_chroma );
225 if( p_vout->p_sys->i_xvport < 0 )
227 XCloseDisplay( p_vout->p_sys->p_display );
228 free( p_vout->p_sys );
233 p_vout->output.i_chroma = X112VLC_FOURCC(p_vout->output.i_chroma);
236 /* Create blank cursor (for mouse cursor autohiding) */
237 p_vout->p_sys->i_time_mouse_last_moved = mdate();
238 p_vout->p_sys->b_mouse_pointer_visible = 1;
239 CreateCursor( p_vout );
241 /* Set main window's size */
242 p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
243 p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
245 /* Spawn base window - this window will include the video output window,
246 * but also command buttons, subtitles and other indicators */
247 if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
249 msg_Err( p_vout, "cannot create X11 window" );
250 DestroyCursor( p_vout );
251 XCloseDisplay( p_vout->p_sys->p_display );
252 free( p_vout->p_sys );
256 /* Open and initialize device. */
257 if( InitDisplay( p_vout ) )
259 msg_Err( p_vout, "cannot initialize X11 display" );
260 DestroyCursor( p_vout );
261 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
262 XCloseDisplay( p_vout->p_sys->p_display );
263 free( p_vout->p_sys );
267 /* Disable screen saver */
268 DisableXScreenSaver( p_vout );
271 p_vout->p_sys->b_altfullscreen = 0;
272 p_vout->p_sys->i_time_button_last_pressed = 0;
274 TestNetWMSupport( p_vout );
279 /*****************************************************************************
280 * Deactivate: destroy X11 video thread output method
281 *****************************************************************************
282 * Terminate an output method created by Open
283 *****************************************************************************/
284 void E_(Deactivate) ( vlc_object_t *p_this )
286 vout_thread_t *p_vout = (vout_thread_t *)p_this;
288 /* If the fullscreen window is still open, close it */
289 if( p_vout->b_fullscreen )
291 ToggleFullScreen( p_vout );
294 /* Restore cursor if it was blanked */
295 if( !p_vout->p_sys->b_mouse_pointer_visible )
297 ToggleCursor( p_vout );
300 #ifdef MODULE_NAME_IS_x11
301 /* Destroy colormap */
302 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
304 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
307 XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
310 DestroyCursor( p_vout );
311 EnableXScreenSaver( p_vout );
312 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
314 XCloseDisplay( p_vout->p_sys->p_display );
316 /* Destroy structure */
317 free( p_vout->p_sys );
320 /*****************************************************************************
321 * InitVideo: initialize X11 video thread output method
322 *****************************************************************************
323 * This function create the XImages needed by the output thread. It is called
324 * at the beginning of the thread, but also each time the window is resized.
325 *****************************************************************************/
326 static int InitVideo( vout_thread_t *p_vout )
331 I_OUTPUTPICTURES = 0;
333 #ifdef MODULE_NAME_IS_xvideo
334 /* Initialize the output structure; we already found an XVideo port,
335 * and the corresponding chroma we will be using. Since we can
336 * arbitrary scale, stick to the coordinates and aspect. */
337 p_vout->output.i_width = p_vout->render.i_width;
338 p_vout->output.i_height = p_vout->render.i_height;
339 p_vout->output.i_aspect = p_vout->render.i_aspect;
341 switch( p_vout->output.i_chroma )
343 case VLC_FOURCC('R','V','1','5'):
344 p_vout->output.i_rmask = 0x001f;
345 p_vout->output.i_gmask = 0x07e0;
346 p_vout->output.i_bmask = 0xf800;
348 case VLC_FOURCC('R','V','1','6'):
349 p_vout->output.i_rmask = 0x001f;
350 p_vout->output.i_gmask = 0x03e0;
351 p_vout->output.i_bmask = 0x7c00;
356 /* Initialize the output structure: RGB with square pixels, whatever
357 * the input format is, since it's the only format we know */
358 switch( p_vout->p_sys->i_screen_depth )
360 case 8: /* FIXME: set the palette */
361 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
363 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
365 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
367 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
369 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
371 msg_Err( p_vout, "unknown screen depth %i",
372 p_vout->p_sys->i_screen_depth );
376 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
377 p_vout->p_sys->p_win->i_height,
379 &p_vout->output.i_width, &p_vout->output.i_height );
381 /* Assume we have square pixels */
382 p_vout->output.i_aspect = p_vout->output.i_width
383 * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
386 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
387 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
391 /* Find an empty picture slot */
392 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
394 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
396 p_pic = p_vout->p_picture + i_index;
401 /* Allocate the picture */
402 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
407 p_pic->i_status = DESTROYED_PICTURE;
408 p_pic->i_type = DIRECT_PICTURE;
410 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
418 /*****************************************************************************
419 * DisplayVideo: displays previously rendered output
420 *****************************************************************************
421 * This function sends the currently rendered image to X11 server.
422 * (The Xv extension takes care of "double-buffering".)
423 *****************************************************************************/
424 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
426 int i_width, i_height, i_x, i_y;
428 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
429 p_vout->p_sys->p_win->i_height,
430 &i_x, &i_y, &i_width, &i_height );
432 #ifdef HAVE_SYS_SHM_H
433 if( p_vout->p_sys->b_shm )
435 /* Display rendered image using shared memory extension */
436 # ifdef MODULE_NAME_IS_xvideo
437 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
438 p_vout->p_sys->p_win->video_window,
439 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
440 0 /*src_x*/, 0 /*src_y*/,
441 p_vout->output.i_width, p_vout->output.i_height,
442 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
443 False /* Don't put True here or you'll waste your CPU */ );
445 XShmPutImage( p_vout->p_sys->p_display,
446 p_vout->p_sys->p_win->video_window,
447 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
448 0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
449 p_vout->output.i_width, p_vout->output.i_height,
450 False /* Don't put True here ! */ );
454 #endif /* HAVE_SYS_SHM_H */
456 /* Use standard XPutImage -- this is gonna be slow ! */
457 #ifdef MODULE_NAME_IS_xvideo
458 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
459 p_vout->p_sys->p_win->video_window,
460 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
461 0 /*src_x*/, 0 /*src_y*/,
462 p_vout->output.i_width, p_vout->output.i_height,
463 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
465 XPutImage( p_vout->p_sys->p_display,
466 p_vout->p_sys->p_win->video_window,
467 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
468 0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
469 p_vout->output.i_width, p_vout->output.i_height );
473 /* Make sure the command is sent now - do NOT use XFlush !*/
474 XSync( p_vout->p_sys->p_display, False );
477 /*****************************************************************************
478 * ManageVideo: handle X11 events
479 *****************************************************************************
480 * This function should be called regularly by video output thread. It manages
481 * X11 events and allows window resizing. It returns a non null value on
483 *****************************************************************************/
484 static int ManageVideo( vout_thread_t *p_vout )
486 XEvent xevent; /* X11 event */
487 char i_key; /* ISO Latin-1 key */
491 /* Handle X11 events: ConfigureNotify events are parsed to know if the
492 * output window's size changed, MapNotify and UnmapNotify to know if the
493 * window is mapped (and if the display is useful), and ClientMessages
494 * to intercept window destruction requests */
496 while( XCheckWindowEvent( p_vout->p_sys->p_display,
497 p_vout->p_sys->p_win->base_window,
498 StructureNotifyMask | KeyPressMask |
499 ButtonPressMask | ButtonReleaseMask |
500 PointerMotionMask | Button1MotionMask , &xevent )
503 /* ConfigureNotify event: prepare */
504 if( xevent.type == ConfigureNotify )
506 if( (unsigned int)xevent.xconfigure.width
507 != p_vout->p_sys->p_win->i_width
508 || (unsigned int)xevent.xconfigure.height
509 != p_vout->p_sys->p_win->i_height )
511 /* Update dimensions */
512 p_vout->i_changes |= VOUT_SIZE_CHANGE;
513 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
514 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
518 else if( xevent.type == KeyPress )
520 /* We may have keys like F1 trough F12, ESC ... */
521 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
522 xevent.xkey.keycode, 0 );
523 switch( (int)x_key_symbol )
526 if( p_vout->b_fullscreen )
528 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
532 /* the user wants to close the window */
533 playlist_t * p_playlist =
534 (playlist_t *)vlc_object_find( p_vout,
537 if( p_playlist != NULL )
539 playlist_Stop( p_playlist );
540 vlc_object_release( p_playlist );
546 intf_thread_t *p_intf;
547 playlist_t * p_playlist;
549 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
553 p_intf->b_menu_change = 1;
554 vlc_object_release( p_intf );
557 p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
559 if( p_playlist != NULL )
562 var_Set( p_playlist, "intf-popupmenu", val );
563 vlc_object_release( p_playlist );
568 /* input_Seek( p_vout, -5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR ); */
569 val.psz_string = "LEFT";
570 var_Set( p_vout, "key-pressed", val );
573 /* input_Seek( p_vout, 5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR ); */
574 val.psz_string = "RIGHT";
575 var_Set( p_vout, "key-pressed", val );
578 /* input_Seek( p_vout, 60, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR ); */
579 val.psz_string = "UP";
580 var_Set( p_vout, "key-pressed", val );
583 /* input_Seek( p_vout, -60, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR ); */
584 val.psz_string = "DOWN";
585 var_Set( p_vout, "key-pressed", val );
589 val.psz_string = "ENTER";
590 var_Set( p_vout, "key-pressed", val );
593 input_Seek( p_vout, 0, INPUT_SEEK_BYTES | INPUT_SEEK_SET );
596 input_Seek( p_vout, 0, INPUT_SEEK_BYTES | INPUT_SEEK_END );
599 input_Seek( p_vout, 10, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
602 input_Seek( p_vout, -10, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
605 input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
610 * The reason why I use this instead of XK_0 is that
611 * with XLookupString, we don't have to care about
614 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
616 /* FIXME: handle stuff here */
621 p_vout->p_vlc->b_die = 1;
625 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
636 else if( xevent.type == ButtonPress )
638 switch( ((XButtonEvent *)&xevent)->button )
641 var_Get( p_vout, "mouse-button-down", &val );
643 var_Set( p_vout, "mouse-button-down", val );
645 /* detect double-clicks */
646 if( ( ((XButtonEvent *)&xevent)->time -
647 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
649 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
652 p_vout->p_sys->i_time_button_last_pressed =
653 ((XButtonEvent *)&xevent)->time;
656 var_Get( p_vout, "mouse-button-down", &val );
658 var_Set( p_vout, "mouse-button-down", val );
662 var_Get( p_vout, "mouse-button-down", &val );
664 var_Set( p_vout, "mouse-button-down", val );
668 var_Get( p_vout, "mouse-button-down", &val );
670 var_Set( p_vout, "mouse-button-down", val );
671 input_Seek( p_vout, 15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
675 var_Get( p_vout, "mouse-button-down", &val );
677 var_Set( p_vout, "mouse-button-down", val );
678 input_Seek( p_vout, -15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
683 else if( xevent.type == ButtonRelease )
685 switch( ((XButtonEvent *)&xevent)->button )
688 var_Get( p_vout, "mouse-button-down", &val );
690 var_Set( p_vout, "mouse-button-down", val );
692 val.b_bool = VLC_TRUE;
693 var_Set( p_vout, "mouse-clicked", val );
698 playlist_t *p_playlist;
700 var_Get( p_vout, "mouse-button-down", &val );
702 var_Set( p_vout, "mouse-button-down", val );
704 p_playlist = vlc_object_find( p_vout,
707 if( p_playlist != NULL )
710 var_Get( p_playlist, "intf-show", &val );
711 val.b_bool = !val.b_bool;
712 var_Set( p_playlist, "intf-show", val );
713 vlc_object_release( p_playlist );
720 intf_thread_t *p_intf;
721 playlist_t *p_playlist;
723 var_Get( p_vout, "mouse-button-down", &val );
725 var_Set( p_vout, "mouse-button-down", val );
726 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
730 p_intf->b_menu_change = 1;
731 vlc_object_release( p_intf );
734 p_playlist = vlc_object_find( p_vout,
737 if( p_playlist != NULL )
740 var_Set( p_playlist, "intf-popupmenu", val );
741 vlc_object_release( p_playlist );
747 var_Get( p_vout, "mouse-button-down", &val );
749 var_Set( p_vout, "mouse-button-down", val );
753 var_Get( p_vout, "mouse-button-down", &val );
755 var_Set( p_vout, "mouse-button-down", val );
761 else if( xevent.type == MotionNotify )
763 int i_width, i_height, i_x, i_y;
766 /* somewhat different use for vout_PlacePicture:
767 * here the values are needed to give to mouse coordinates
768 * in the original picture space */
769 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
770 p_vout->p_sys->p_win->i_height,
771 &i_x, &i_y, &i_width, &i_height );
773 val.i_int = ( xevent.xmotion.x - i_x )
774 * p_vout->render.i_width / i_width;
775 var_Set( p_vout, "mouse-x", val );
776 val.i_int = ( xevent.xmotion.y - i_y )
777 * p_vout->render.i_height / i_height;
778 var_Set( p_vout, "mouse-y", val );
780 val.b_bool = VLC_TRUE;
781 var_Set( p_vout, "mouse-moved", val );
783 p_vout->p_sys->i_time_mouse_last_moved = mdate();
784 if( ! p_vout->p_sys->b_mouse_pointer_visible )
786 ToggleCursor( p_vout );
789 else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
790 || xevent.type == MapNotify
791 || xevent.type == UnmapNotify )
793 /* Ignore these events */
795 else /* Other events */
797 msg_Warn( p_vout, "unhandled event %d received", xevent.type );
801 /* Handle events for video output sub-window */
802 while( XCheckWindowEvent( p_vout->p_sys->p_display,
803 p_vout->p_sys->p_win->video_window,
804 ExposureMask, &xevent ) == True )
806 /* Window exposed (only handled if stream playback is paused) */
807 if( xevent.type == Expose )
809 if( ((XExposeEvent *)&xevent)->count == 0 )
811 /* (if this is the last a collection of expose events...) */
813 if( p_vout->p_vlc->p_input_bank->pp_input[0] != NULL )
815 if( PAUSE_S == p_vout->p_vlc->p_input_bank->pp_input[0]
816 ->stream.control.i_status )
818 /* XVideoDisplay( p_vout )*/;
826 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
827 * are handled - according to the man pages, the format is always 32
829 while( XCheckTypedEvent( p_vout->p_sys->p_display,
830 ClientMessage, &xevent ) )
832 if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
833 && ((Atom)xevent.xclient.data.l[0]
834 == p_vout->p_sys->p_win->wm_delete_window ) )
836 /* the user wants to close the window */
837 playlist_t * p_playlist =
838 (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
840 if( p_playlist != NULL )
842 playlist_Stop( p_playlist );
843 vlc_object_release( p_playlist );
851 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
855 /* Update the object variable and trigger callback */
856 val.b_bool = !p_vout->b_fullscreen;
857 var_Set( p_vout, "fullscreen", val );
859 ToggleFullScreen( p_vout );
860 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
866 * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
867 * the size flag inside the fullscreen routine)
869 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
871 int i_width, i_height, i_x, i_y;
873 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
875 #ifdef MODULE_NAME_IS_x11
876 /* We need to signal the vout thread about the size change because it
877 * is doing the rescaling */
878 p_vout->i_changes |= VOUT_SIZE_CHANGE;
881 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
882 p_vout->p_sys->p_win->i_height,
883 &i_x, &i_y, &i_width, &i_height );
885 XMoveResizeWindow( p_vout->p_sys->p_display,
886 p_vout->p_sys->p_win->video_window,
887 i_x, i_y, i_width, i_height );
890 /* Autohide Cursour */
891 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
893 /* Hide the mouse automatically */
894 if( p_vout->p_sys->b_mouse_pointer_visible )
896 ToggleCursor( p_vout );
903 /*****************************************************************************
904 * EndVideo: terminate X11 video thread output method
905 *****************************************************************************
906 * Destroy the X11 XImages created by Init. It is called at the end of
907 * the thread, but also each time the window is resized.
908 *****************************************************************************/
909 static void EndVideo( vout_thread_t *p_vout )
913 /* Free the direct buffers we allocated */
914 for( i_index = I_OUTPUTPICTURES ; i_index ; )
917 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
921 /* following functions are local */
923 /*****************************************************************************
924 * CreateWindow: open and set-up X11 main window
925 *****************************************************************************/
926 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
928 XSizeHints xsize_hints;
929 XSetWindowAttributes xwindow_attributes;
933 vlc_bool_t b_expose = VLC_FALSE;
934 vlc_bool_t b_configure_notify = VLC_FALSE;
935 vlc_bool_t b_map_notify = VLC_FALSE;
938 long long int i_drawable;
940 /* Prepare window manager hints and properties */
941 xsize_hints.base_width = p_win->i_width;
942 xsize_hints.base_height = p_win->i_height;
943 xsize_hints.flags = PSize;
944 p_win->wm_protocols =
945 XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
946 p_win->wm_delete_window =
947 XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
949 /* Prepare window attributes */
950 xwindow_attributes.backing_store = Always; /* save the hidden part */
951 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
952 p_vout->p_sys->i_screen);
953 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
955 /* Check whether someone provided us with a window ID */
956 var_Get( p_vout->p_vlc, "drawable", &val );
957 i_drawable = p_vout->b_fullscreen ? 0 : val.i_int;
961 /* Create the window and set hints - the window must receive
962 * ConfigureNotify events, and until it is displayed, Expose and
963 * MapNotify events. */
966 XCreateWindow( p_vout->p_sys->p_display,
967 DefaultRootWindow( p_vout->p_sys->p_display ),
969 p_win->i_width, p_win->i_height,
972 CWBackingStore | CWBackPixel | CWEventMask,
973 &xwindow_attributes );
975 if( !p_vout->b_fullscreen )
977 /* Set window manager hints and properties: size hints, command,
978 * window's name, and accepted protocols */
979 XSetWMNormalHints( p_vout->p_sys->p_display,
980 p_win->base_window, &xsize_hints );
981 XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
982 p_vout->p_vlc->ppsz_argv, p_vout->p_vlc->i_argc );
984 XStoreName( p_vout->p_sys->p_display, p_win->base_window,
985 #ifdef MODULE_NAME_IS_x11
986 VOUT_TITLE " (X11 output)"
988 VOUT_TITLE " (XVideo output)"
995 /* Get the parent window's geometry information */
997 unsigned int dummy2, dummy3;
998 XGetGeometry( p_vout->p_sys->p_display, i_drawable,
999 &dummy1, &dummy2, &dummy3,
1004 /* We are already configured */
1005 b_configure_notify = VLC_TRUE;
1007 /* From man XSelectInput: only one client at a time can select a
1008 * ButtonPress event, so we need to open a new window anyway. */
1009 p_win->base_window =
1010 XCreateWindow( p_vout->p_sys->p_display,
1013 p_win->i_width, p_win->i_height,
1015 0, CopyFromParent, 0,
1016 CWBackingStore | CWBackPixel | CWEventMask,
1017 &xwindow_attributes );
1020 if( (p_win->wm_protocols == None) /* use WM_DELETE_WINDOW */
1021 || (p_win->wm_delete_window == None)
1022 || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
1023 &p_win->wm_delete_window, 1 ) )
1025 /* WM_DELETE_WINDOW is not supported by window manager */
1026 msg_Warn( p_vout, "missing or bad window manager" );
1029 /* Creation of a graphic context that doesn't generate a GraphicsExpose
1030 * event when using functions like XCopyArea */
1031 xgcvalues.graphics_exposures = False;
1032 p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1034 GCGraphicsExposures, &xgcvalues );
1036 /* Send orders to server, and wait until window is displayed - three
1037 * events must be received: a MapNotify event, an Expose event allowing
1038 * drawing in the window, and a ConfigureNotify to get the window
1039 * dimensions. Once those events have been received, only
1040 * ConfigureNotify events need to be received. */
1041 XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1044 XNextEvent( p_vout->p_sys->p_display, &xevent);
1045 if( (xevent.type == Expose)
1046 && (xevent.xexpose.window == p_win->base_window) )
1048 b_expose = VLC_TRUE;
1050 else if( (xevent.type == MapNotify)
1051 && (xevent.xmap.window == p_win->base_window) )
1053 b_map_notify = VLC_TRUE;
1055 else if( (xevent.type == ConfigureNotify)
1056 && (xevent.xconfigure.window == p_win->base_window) )
1058 b_configure_notify = VLC_TRUE;
1059 p_win->i_width = xevent.xconfigure.width;
1060 p_win->i_height = xevent.xconfigure.height;
1062 } while( !( b_expose && b_configure_notify && b_map_notify ) );
1064 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1065 StructureNotifyMask | KeyPressMask |
1066 ButtonPressMask | ButtonReleaseMask |
1067 PointerMotionMask );
1069 #ifdef MODULE_NAME_IS_x11
1070 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1072 /* Allocate a new palette */
1073 p_vout->p_sys->colormap =
1074 XCreateColormap( p_vout->p_sys->p_display,
1075 DefaultRootWindow( p_vout->p_sys->p_display ),
1076 DefaultVisual( p_vout->p_sys->p_display,
1077 p_vout->p_sys->i_screen ),
1080 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1081 XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1082 CWColormap, &xwindow_attributes );
1086 /* Create video output sub-window. */
1087 p_win->video_window = XCreateSimpleWindow(
1088 p_vout->p_sys->p_display,
1089 p_win->base_window, 0, 0,
1090 p_win->i_width, p_win->i_height,
1092 BlackPixel( p_vout->p_sys->p_display,
1093 p_vout->p_sys->i_screen ),
1094 WhitePixel( p_vout->p_sys->p_display,
1095 p_vout->p_sys->i_screen ) );
1097 XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1098 BlackPixel( p_vout->p_sys->p_display,
1099 p_vout->p_sys->i_screen ) );
1101 XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1102 XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1105 /* make sure the video window will be centered in the next ManageVideo() */
1106 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1108 /* If the cursor was formerly blank than blank it again */
1109 if( !p_vout->p_sys->b_mouse_pointer_visible )
1111 ToggleCursor( p_vout );
1112 ToggleCursor( p_vout );
1115 /* Do NOT use XFlush here ! */
1116 XSync( p_vout->p_sys->p_display, False );
1118 /* At this stage, the window is open, displayed, and ready to
1120 p_vout->p_sys->p_win = p_win;
1125 /*****************************************************************************
1126 * DestroyWindow: destroy the window
1127 *****************************************************************************
1129 *****************************************************************************/
1130 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1132 /* Do NOT use XFlush here ! */
1133 XSync( p_vout->p_sys->p_display, False );
1135 XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1136 XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1138 XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1139 XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1142 /*****************************************************************************
1143 * NewPicture: allocate a picture
1144 *****************************************************************************
1145 * Returns 0 on success, -1 otherwise
1146 *****************************************************************************/
1147 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1149 #ifdef MODULE_NAME_IS_xvideo
1153 /* We know the chroma, allocate a buffer which will be used
1154 * directly by the decoder */
1155 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1157 if( p_pic->p_sys == NULL )
1162 /* Fill in picture_t fields */
1163 vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
1164 p_vout->output.i_width, p_vout->output.i_height,
1165 p_vout->output.i_aspect );
1167 #ifdef HAVE_SYS_SHM_H
1168 if( p_vout->p_sys->b_shm )
1170 /* Create image using XShm extension */
1171 p_pic->p_sys->p_image =
1172 CreateShmImage( p_vout, p_vout->p_sys->p_display,
1173 # ifdef MODULE_NAME_IS_xvideo
1174 p_vout->p_sys->i_xvport, p_vout->output.i_chroma,
1176 p_vout->p_sys->p_visual,
1177 p_vout->p_sys->i_screen_depth,
1179 &p_pic->p_sys->shminfo,
1180 p_vout->output.i_width, p_vout->output.i_height );
1183 #endif /* HAVE_SYS_SHM_H */
1185 /* Create image without XShm extension */
1186 p_pic->p_sys->p_image =
1187 CreateImage( p_vout, p_vout->p_sys->p_display,
1188 #ifdef MODULE_NAME_IS_xvideo
1189 p_vout->p_sys->i_xvport, p_vout->output.i_chroma,
1190 p_pic->format.i_bits_per_pixel,
1192 p_vout->p_sys->p_visual,
1193 p_vout->p_sys->i_screen_depth,
1194 p_vout->p_sys->i_bytes_per_pixel,
1196 p_vout->output.i_width, p_vout->output.i_height );
1199 if( p_pic->p_sys->p_image == NULL )
1201 free( p_pic->p_sys );
1205 switch( p_vout->output.i_chroma )
1207 #ifdef MODULE_NAME_IS_xvideo
1208 case VLC_FOURCC('I','4','2','0'):
1209 case VLC_FOURCC('Y','V','1','2'):
1210 case VLC_FOURCC('Y','2','1','1'):
1211 case VLC_FOURCC('Y','U','Y','2'):
1212 case VLC_FOURCC('U','Y','V','Y'):
1213 case VLC_FOURCC('R','V','1','5'):
1214 case VLC_FOURCC('R','V','1','6'):
1215 case VLC_FOURCC('R','V','2','4'): /* Fixme: pixel pitch == 4 ? */
1216 case VLC_FOURCC('R','V','3','2'):
1218 for( i_plane = 0; i_plane < p_pic->p_sys->p_image->num_planes;
1221 p_pic->p[i_plane].p_pixels = p_pic->p_sys->p_image->data
1222 + p_pic->p_sys->p_image->offsets[i_plane];
1223 p_pic->p[i_plane].i_pitch =
1224 p_pic->p_sys->p_image->pitches[i_plane];
1226 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
1228 /* U and V inverted compared to I420
1229 * Fixme: this should be handled by the vout core */
1230 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1231 + p_pic->p_sys->p_image->offsets[2];
1232 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1233 + p_pic->p_sys->p_image->offsets[1];
1238 case VLC_FOURCC('R','G','B','2'):
1239 case VLC_FOURCC('R','V','1','6'):
1240 case VLC_FOURCC('R','V','1','5'):
1241 case VLC_FOURCC('R','V','2','4'):
1242 case VLC_FOURCC('R','V','3','2'):
1244 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1245 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1246 + p_pic->p_sys->p_image->xoffset;
1247 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1249 /* p_pic->p->i_pixel_pitch = 4 for RV24 but this should be set
1250 * properly by vout_InitPicture() */
1251 p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
1252 * p_pic->p_sys->p_image->width;
1257 /* Unknown chroma, tell the guy to get lost */
1258 IMAGE_FREE( p_pic->p_sys->p_image );
1259 free( p_pic->p_sys );
1260 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1261 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1262 p_pic->i_planes = 0;
1269 /*****************************************************************************
1270 * FreePicture: destroy a picture allocated with NewPicture
1271 *****************************************************************************
1272 * Destroy XImage AND associated data. If using Shm, detach shared memory
1273 * segment from server and process, then free it. The XDestroyImage manpage
1274 * says that both the image structure _and_ the data pointed to by the
1275 * image structure are freed, so no need to free p_image->data.
1276 *****************************************************************************/
1277 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1279 /* The order of operations is correct */
1280 #ifdef HAVE_SYS_SHM_H
1281 if( p_vout->p_sys->b_shm )
1283 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
1284 IMAGE_FREE( p_pic->p_sys->p_image );
1286 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
1287 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
1289 msg_Err( p_vout, "cannot detach shared memory (%s)",
1296 IMAGE_FREE( p_pic->p_sys->p_image );
1299 /* Do NOT use XFlush here ! */
1300 XSync( p_vout->p_sys->p_display, False );
1302 free( p_pic->p_sys );
1305 /*****************************************************************************
1306 * ToggleFullScreen: Enable or disable full screen mode
1307 *****************************************************************************
1308 * This function will switch between fullscreen and window mode.
1309 *****************************************************************************/
1310 static void ToggleFullScreen ( vout_thread_t *p_vout )
1314 mwmhints_t mwmhints;
1315 XSetWindowAttributes attributes;
1317 #ifdef HAVE_XINERAMA
1321 p_vout->b_fullscreen = !p_vout->b_fullscreen;
1323 if( p_vout->b_fullscreen )
1325 msg_Dbg( p_vout, "entering fullscreen mode" );
1327 p_vout->p_sys->b_altfullscreen =
1328 config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
1330 XUnmapWindow( p_vout->p_sys->p_display,
1331 p_vout->p_sys->p_win->base_window);
1333 p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
1335 /* fullscreen window size and position */
1336 p_vout->p_sys->p_win->i_width =
1337 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1338 p_vout->p_sys->p_win->i_height =
1339 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1341 CreateWindow( p_vout, p_vout->p_sys->p_win );
1343 /* To my knowledge there are two ways to create a borderless window.
1344 * There's the generic way which is to tell x to bypass the window
1345 * manager, but this creates problems with the focus of other
1347 * The other way is to use the motif property "_MOTIF_WM_HINTS" which
1348 * luckily seems to be supported by most window managers. */
1349 if( !p_vout->p_sys->b_altfullscreen )
1351 mwmhints.flags = MWM_HINTS_DECORATIONS;
1352 mwmhints.decorations = False;
1354 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1356 XChangeProperty( p_vout->p_sys->p_display,
1357 p_vout->p_sys->p_win->base_window,
1358 prop, prop, 32, PropModeReplace,
1359 (unsigned char *)&mwmhints,
1360 PROP_MWM_HINTS_ELEMENTS );
1364 /* brute force way to remove decorations */
1365 attributes.override_redirect = True;
1366 XChangeWindowAttributes( p_vout->p_sys->p_display,
1367 p_vout->p_sys->p_win->base_window,
1371 /* Make sure the change is effective */
1372 XReparentWindow( p_vout->p_sys->p_display,
1373 p_vout->p_sys->p_win->base_window,
1374 DefaultRootWindow( p_vout->p_sys->p_display ),
1378 if( p_vout->p_sys->b_net_wm_state_fullscreen )
1380 XClientMessageEvent event;
1382 memset( &event, 0, sizeof( XClientMessageEvent ) );
1384 event.type = ClientMessage;
1385 event.message_type = p_vout->p_sys->net_wm_state;
1386 event.display = p_vout->p_sys->p_display;
1387 event.window = p_vout->p_sys->p_win->base_window;
1389 event.data.l[ 0 ] = 1; /* set property */
1390 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_fullscreen;
1392 XSendEvent( p_vout->p_sys->p_display,
1393 DefaultRootWindow( p_vout->p_sys->p_display ),
1394 False, SubstructureRedirectMask,
1398 /* Make sure the change is effective */
1399 XReparentWindow( p_vout->p_sys->p_display,
1400 p_vout->p_sys->p_win->base_window,
1401 DefaultRootWindow( p_vout->p_sys->p_display ),
1404 /* fullscreen window size and position */
1405 p_vout->p_sys->p_win->i_width =
1406 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1407 p_vout->p_sys->p_win->i_height =
1408 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1410 p_vout->p_sys->p_win->i_x = 0;
1411 p_vout->p_sys->p_win->i_y = 0;
1413 #ifdef HAVE_XINERAMA
1414 if( XineramaQueryExtension( p_vout->p_sys->p_display, &i_d1, &i_d2 ) &&
1415 XineramaIsActive( p_vout->p_sys->p_display ) )
1417 XineramaScreenInfo *screens; /* infos for xinerama */
1420 msg_Dbg( p_vout, "Using XFree Xinerama extension");
1422 #define SCREEN p_vout->p_sys->p_win->i_screen
1424 /* Get Informations about Xinerama (num of screens) */
1425 screens = XineramaQueryScreens( p_vout->p_sys->p_display,
1429 SCREEN = config_GetInt( p_vout,
1430 MODULE_STRING "-xineramascreen" );
1432 /* just check that user has entered a good value */
1433 if( SCREEN >= i_num_screens || SCREEN < 0 )
1435 msg_Dbg( p_vout, "requested screen number invalid" );
1439 /* Get the X/Y upper left corner coordinate of the above screen */
1440 p_vout->p_sys->p_win->i_x = screens[SCREEN].x_org;
1441 p_vout->p_sys->p_win->i_y = screens[SCREEN].y_org;
1443 /* Set the Height/width to the screen resolution */
1444 p_vout->p_sys->p_win->i_width = screens[SCREEN].width;
1445 p_vout->p_sys->p_win->i_height = screens[SCREEN].height;
1454 XMoveResizeWindow( p_vout->p_sys->p_display,
1455 p_vout->p_sys->p_win->base_window,
1456 p_vout->p_sys->p_win->i_x,
1457 p_vout->p_sys->p_win->i_y,
1458 p_vout->p_sys->p_win->i_width,
1459 p_vout->p_sys->p_win->i_height );
1463 msg_Dbg( p_vout, "leaving fullscreen mode" );
1464 DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
1465 p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
1467 XMapWindow( p_vout->p_sys->p_display,
1468 p_vout->p_sys->p_win->base_window);
1471 /* Unfortunately, using XSync() here is not enough to ensure the
1472 * window has already been mapped because the XMapWindow() request
1473 * has not necessarily been sent directly to our window (remember,
1474 * the call is first redirected to the window manager) */
1477 XWindowEvent( p_vout->p_sys->p_display,
1478 p_vout->p_sys->p_win->base_window,
1479 StructureNotifyMask, &xevent );
1480 } while( xevent.type != MapNotify );
1482 /* Be careful, this can generate a BadMatch error if the window is not
1483 * already mapped by the server (see above) */
1484 XSetInputFocus(p_vout->p_sys->p_display,
1485 p_vout->p_sys->p_win->base_window,
1489 /* signal that the size needs to be updated */
1490 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1493 /*****************************************************************************
1494 * EnableXScreenSaver: enable screen saver
1495 *****************************************************************************
1496 * This function enables the screen saver on a display after it has been
1497 * disabled by XDisableScreenSaver.
1498 * FIXME: what happens if multiple vlc sessions are running at the same
1500 *****************************************************************************/
1501 static void EnableXScreenSaver( vout_thread_t *p_vout )
1503 #ifdef DPMSINFO_IN_DPMS_H
1507 if( p_vout->p_sys->i_ss_timeout )
1509 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1510 p_vout->p_sys->i_ss_interval,
1511 p_vout->p_sys->i_ss_blanking,
1512 p_vout->p_sys->i_ss_exposure );
1515 /* Restore DPMS settings */
1516 #ifdef DPMSINFO_IN_DPMS_H
1517 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1519 if( p_vout->p_sys->b_ss_dpms )
1521 DPMSEnable( p_vout->p_sys->p_display );
1527 /*****************************************************************************
1528 * DisableXScreenSaver: disable screen saver
1529 *****************************************************************************
1530 * See XEnableXScreenSaver
1531 *****************************************************************************/
1532 static void DisableXScreenSaver( vout_thread_t *p_vout )
1534 #ifdef DPMSINFO_IN_DPMS_H
1538 /* Save screen saver informations */
1539 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1540 &p_vout->p_sys->i_ss_interval,
1541 &p_vout->p_sys->i_ss_blanking,
1542 &p_vout->p_sys->i_ss_exposure );
1544 /* Disable screen saver */
1545 if( p_vout->p_sys->i_ss_timeout )
1547 XSetScreenSaver( p_vout->p_sys->p_display, 0,
1548 p_vout->p_sys->i_ss_interval,
1549 p_vout->p_sys->i_ss_blanking,
1550 p_vout->p_sys->i_ss_exposure );
1554 #ifdef DPMSINFO_IN_DPMS_H
1555 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1558 /* Save DPMS current state */
1559 DPMSInfo( p_vout->p_sys->p_display, &unused,
1560 &p_vout->p_sys->b_ss_dpms );
1561 DPMSDisable( p_vout->p_sys->p_display );
1566 /*****************************************************************************
1567 * CreateCursor: create a blank mouse pointer
1568 *****************************************************************************/
1569 static void CreateCursor( vout_thread_t *p_vout )
1571 XColor cursor_color;
1573 p_vout->p_sys->cursor_pixmap =
1574 XCreatePixmap( p_vout->p_sys->p_display,
1575 DefaultRootWindow( p_vout->p_sys->p_display ),
1578 XParseColor( p_vout->p_sys->p_display,
1579 XCreateColormap( p_vout->p_sys->p_display,
1581 p_vout->p_sys->p_display ),
1583 p_vout->p_sys->p_display,
1584 p_vout->p_sys->i_screen ),
1586 "black", &cursor_color );
1588 p_vout->p_sys->blank_cursor =
1589 XCreatePixmapCursor( p_vout->p_sys->p_display,
1590 p_vout->p_sys->cursor_pixmap,
1591 p_vout->p_sys->cursor_pixmap,
1592 &cursor_color, &cursor_color, 1, 1 );
1595 /*****************************************************************************
1596 * DestroyCursor: destroy the blank mouse pointer
1597 *****************************************************************************/
1598 static void DestroyCursor( vout_thread_t *p_vout )
1600 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
1603 /*****************************************************************************
1604 * ToggleCursor: hide or show the mouse pointer
1605 *****************************************************************************
1606 * This function hides the X pointer if it is visible by setting the pointer
1607 * sprite to a blank one. To show it again, we disable the sprite.
1608 *****************************************************************************/
1609 static void ToggleCursor( vout_thread_t *p_vout )
1611 if( p_vout->p_sys->b_mouse_pointer_visible )
1613 XDefineCursor( p_vout->p_sys->p_display,
1614 p_vout->p_sys->p_win->base_window,
1615 p_vout->p_sys->blank_cursor );
1616 p_vout->p_sys->b_mouse_pointer_visible = 0;
1620 XUndefineCursor( p_vout->p_sys->p_display,
1621 p_vout->p_sys->p_win->base_window );
1622 p_vout->p_sys->b_mouse_pointer_visible = 1;
1626 #ifdef MODULE_NAME_IS_xvideo
1627 /*****************************************************************************
1628 * XVideoGetPort: get YUV12 port
1629 *****************************************************************************/
1630 static int XVideoGetPort( vout_thread_t *p_vout,
1631 vlc_fourcc_t i_chroma, vlc_fourcc_t *pi_newchroma )
1633 XvAdaptorInfo *p_adaptor;
1635 int i_adaptor, i_num_adaptors, i_requested_adaptor;
1636 int i_selected_port;
1638 switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
1643 case XvBadExtension:
1644 msg_Warn( p_vout, "XvBadExtension" );
1648 msg_Warn( p_vout, "XvBadAlloc" );
1652 msg_Warn( p_vout, "XvQueryExtension failed" );
1656 switch( XvQueryAdaptors( p_vout->p_sys->p_display,
1657 DefaultRootWindow( p_vout->p_sys->p_display ),
1658 &i_num_adaptors, &p_adaptor ) )
1663 case XvBadExtension:
1664 msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
1668 msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
1672 msg_Warn( p_vout, "XvQueryAdaptors failed" );
1676 i_selected_port = -1;
1677 i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
1679 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
1681 XvImageFormatValues *p_formats;
1682 int i_format, i_num_formats;
1685 /* If we requested an adaptor and it's not this one, we aren't
1687 if( i_requested_adaptor != -1 && i_adaptor != i_requested_adaptor )
1692 /* If the adaptor doesn't have the required properties, skip it */
1693 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
1694 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
1699 /* Check that adaptor supports our requested format... */
1700 p_formats = XvListImageFormats( p_vout->p_sys->p_display,
1701 p_adaptor[i_adaptor].base_id,
1705 i_format < i_num_formats && ( i_selected_port == -1 );
1708 /* Code removed, we can get this through xvinfo anyway */
1710 XvEncodingInfo *p_enc;
1711 int i_enc, i_num_encodings;
1712 XvAttribute *p_attr;
1713 int i_attr, i_num_attributes;
1716 /* If this is not the format we want, or at least a
1717 * similar one, forget it */
1718 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
1723 /* Look for the first available port supporting this format */
1724 for( i_port = p_adaptor[i_adaptor].base_id;
1725 ( i_port < (int)(p_adaptor[i_adaptor].base_id
1726 + p_adaptor[i_adaptor].num_ports) )
1727 && ( i_selected_port == -1 );
1730 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
1733 i_selected_port = i_port;
1734 *pi_newchroma = p_formats[ i_format ].id;
1738 /* If no free port was found, forget it */
1739 if( i_selected_port == -1 )
1744 /* If we found a port, print information about it */
1745 msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
1746 i_adaptor, i_selected_port, p_formats[ i_format ].id,
1747 (char *)&p_formats[ i_format ].id,
1748 ( p_formats[ i_format ].format == XvPacked ) ?
1749 "packed" : "planar" );
1752 msg_Dbg( p_vout, " encoding list:" );
1754 if( XvQueryEncodings( p_vout->p_sys->p_display, i_selected_port,
1755 &i_num_encodings, &p_enc )
1758 msg_Dbg( p_vout, " XvQueryEncodings failed" );
1762 for( i_enc = 0; i_enc < i_num_encodings; i_enc++ )
1764 msg_Dbg( p_vout, " id=%ld, name=%s, size=%ldx%ld,"
1765 " numerator=%d, denominator=%d",
1766 p_enc[i_enc].encoding_id, p_enc[i_enc].name,
1767 p_enc[i_enc].width, p_enc[i_enc].height,
1768 p_enc[i_enc].rate.numerator,
1769 p_enc[i_enc].rate.denominator );
1774 XvFreeEncodingInfo( p_enc );
1777 msg_Dbg( p_vout, " attribute list:" );
1778 p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
1780 &i_num_attributes );
1781 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
1783 msg_Dbg( p_vout, " name=%s, flags=[%s%s ], min=%i, max=%i",
1784 p_attr[i_attr].name,
1785 (p_attr[i_attr].flags & XvGettable) ? " get" : "",
1786 (p_attr[i_attr].flags & XvSettable) ? " set" : "",
1787 p_attr[i_attr].min_value, p_attr[i_attr].max_value );
1790 if( p_attr != NULL )
1797 if( p_formats != NULL )
1804 if( i_num_adaptors > 0 )
1806 XvFreeAdaptorInfo( p_adaptor );
1809 if( i_selected_port == -1 )
1811 int i_chroma_tmp = X112VLC_FOURCC( i_chroma );
1812 if( i_requested_adaptor == -1 )
1814 msg_Warn( p_vout, "no free XVideo port found for format "
1815 "0x%.8x (%4.4s)", i_chroma_tmp, (char*)&i_chroma_tmp );
1819 msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
1820 "XVideo port for format 0x%.8x (%4.4s)",
1821 i_requested_adaptor, i_chroma_tmp, (char*)&i_chroma_tmp );
1825 return i_selected_port;
1828 /*****************************************************************************
1829 * XVideoReleasePort: release YUV12 port
1830 *****************************************************************************/
1831 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
1833 XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
1837 /*****************************************************************************
1838 * InitDisplay: open and initialize X11 device
1839 *****************************************************************************
1840 * Create a window according to video output given size, and set other
1841 * properties according to the display properties.
1842 *****************************************************************************/
1843 static int InitDisplay( vout_thread_t *p_vout )
1845 #ifdef MODULE_NAME_IS_x11
1846 XPixmapFormatValues * p_formats; /* pixmap formats */
1847 XVisualInfo * p_xvisual; /* visuals informations */
1848 XVisualInfo xvisual_template; /* visual template */
1849 int i_count; /* array size */
1852 #ifdef HAVE_SYS_SHM_H
1853 p_vout->p_sys->b_shm = 0;
1855 if( config_GetInt( p_vout, MODULE_STRING "-shm" ) )
1858 /* FIXME: As of 2001-03-16, XFree4 for MacOS X does not support Xshm */
1860 p_vout->p_sys->b_shm =
1861 ( XShmQueryExtension( p_vout->p_sys->p_display ) == True );
1864 if( !p_vout->p_sys->b_shm )
1866 msg_Warn( p_vout, "XShm video extension is unavailable" );
1871 msg_Dbg( p_vout, "disabling XShm video extension" );
1875 msg_Warn( p_vout, "XShm video extension is unavailable" );
1879 #ifdef MODULE_NAME_IS_xvideo
1880 /* XXX The brightness and contrast values should be read from environment
1881 * XXX variables... */
1883 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
1884 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
1888 #ifdef MODULE_NAME_IS_x11
1889 /* Initialize structure */
1890 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
1892 /* Get screen depth */
1893 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
1894 p_vout->p_sys->i_screen );
1895 switch( p_vout->p_sys->i_screen_depth )
1899 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
1901 xvisual_template.screen = p_vout->p_sys->i_screen;
1902 xvisual_template.class = DirectColor;
1903 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1904 VisualScreenMask | VisualClassMask,
1905 &xvisual_template, &i_count );
1906 if( p_xvisual == NULL )
1908 msg_Err( p_vout, "no PseudoColor visual available" );
1909 return VLC_EGENERIC;
1911 p_vout->p_sys->i_bytes_per_pixel = 1;
1912 p_vout->output.pf_setpalette = SetPalette;
1919 * Screen depth is higher than 8bpp. TrueColor visual is used.
1921 xvisual_template.screen = p_vout->p_sys->i_screen;
1922 xvisual_template.class = TrueColor;
1923 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1924 VisualScreenMask | VisualClassMask,
1925 &xvisual_template, &i_count );
1926 if( p_xvisual == NULL )
1928 msg_Err( p_vout, "no TrueColor visual available" );
1929 return VLC_EGENERIC;
1932 p_vout->output.i_rmask = p_xvisual->red_mask;
1933 p_vout->output.i_gmask = p_xvisual->green_mask;
1934 p_vout->output.i_bmask = p_xvisual->blue_mask;
1936 /* There is no difference yet between 3 and 4 Bpp. The only way
1937 * to find the actual number of bytes per pixel is to list supported
1938 * pixmap formats. */
1939 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
1940 p_vout->p_sys->i_bytes_per_pixel = 0;
1942 for( ; i_count-- ; p_formats++ )
1944 /* Under XFree4.0, the list contains pixmap formats available
1945 * through all video depths ; so we have to check against current
1947 if( p_formats->depth == (int)p_vout->p_sys->i_screen_depth )
1949 if( p_formats->bits_per_pixel / 8
1950 > (int)p_vout->p_sys->i_bytes_per_pixel )
1952 p_vout->p_sys->i_bytes_per_pixel =
1953 p_formats->bits_per_pixel / 8;
1959 p_vout->p_sys->p_visual = p_xvisual->visual;
1966 #ifdef HAVE_SYS_SHM_H
1967 /*****************************************************************************
1968 * CreateShmImage: create an XImage or XvImage using shared memory extension
1969 *****************************************************************************
1970 * Prepare an XImage or XvImage for display function.
1971 * The order of the operations respects the recommandations of the mit-shm
1972 * document by J.Corbet and K.Packard. Most of the parameters were copied from
1973 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
1974 *****************************************************************************/
1975 static IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
1976 Display* p_display, EXTRA_ARGS_SHM,
1977 int i_width, int i_height )
1979 IMAGE_TYPE *p_image;
1981 /* Create XImage / XvImage */
1982 #ifdef MODULE_NAME_IS_xvideo
1983 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
1984 i_width, i_height, p_shm );
1986 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
1987 p_shm, i_width, i_height );
1989 if( p_image == NULL )
1991 msg_Err( p_vout, "image creation failed" );
1995 /* Allocate shared memory segment - 0776 set the access permission
1996 * rights (like umask), they are not yet supported by all X servers */
1997 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
1998 if( p_shm->shmid < 0 )
2000 msg_Err( p_vout, "cannot allocate shared image data (%s)",
2001 strerror( errno ) );
2002 IMAGE_FREE( p_image );
2006 /* Attach shared memory segment to process (read/write) */
2007 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2008 if(! p_shm->shmaddr )
2010 msg_Err( p_vout, "cannot attach shared memory (%s)",
2012 IMAGE_FREE( p_image );
2013 shmctl( p_shm->shmid, IPC_RMID, 0 );
2017 /* Read-only data. We won't be using XShmGetImage */
2018 p_shm->readOnly = True;
2020 /* Attach shared memory segment to X server */
2021 if( XShmAttach( p_display, p_shm ) == False )
2023 msg_Err( p_vout, "cannot attach shared memory to X server" );
2024 IMAGE_FREE( p_image );
2025 shmctl( p_shm->shmid, IPC_RMID, 0 );
2026 shmdt( p_shm->shmaddr );
2030 /* Send image to X server. This instruction is required, since having
2031 * built a Shm XImage and not using it causes an error on XCloseDisplay,
2032 * and remember NOT to use XFlush ! */
2033 XSync( p_display, False );
2036 /* Mark the shm segment to be removed when there are no more
2037 * attachements, so it is automatic on process exit or after shmdt */
2038 shmctl( p_shm->shmid, IPC_RMID, 0 );
2045 /*****************************************************************************
2046 * CreateImage: create an XImage or XvImage
2047 *****************************************************************************
2048 * Create a simple image used as a buffer.
2049 *****************************************************************************/
2050 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2051 Display *p_display, EXTRA_ARGS,
2052 int i_width, int i_height )
2054 byte_t * p_data; /* image data storage zone */
2055 IMAGE_TYPE *p_image;
2056 #ifdef MODULE_NAME_IS_x11
2057 int i_quantum; /* XImage quantum (see below) */
2058 int i_bytes_per_line;
2061 /* Allocate memory for image */
2062 #ifdef MODULE_NAME_IS_xvideo
2063 p_data = (byte_t *) malloc( i_width * i_height * i_bits_per_pixel / 8 );
2065 i_bytes_per_line = i_width * i_bytes_per_pixel;
2066 p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
2070 msg_Err( p_vout, "out of memory" );
2074 #ifdef MODULE_NAME_IS_x11
2075 /* Optimize the quantum of a scanline regarding its size - the quantum is
2076 a diviser of the number of bits between the start of two scanlines. */
2077 if( i_bytes_per_line & 0xf )
2081 else if( i_bytes_per_line & 0x10 )
2091 /* Create XImage. p_data will be automatically freed */
2092 #ifdef MODULE_NAME_IS_xvideo
2093 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2094 p_data, i_width, i_height );
2096 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2097 p_data, i_width, i_height, i_quantum, 0 );
2099 if( p_image == NULL )
2101 msg_Err( p_vout, "XCreateImage() failed" );
2109 #ifdef MODULE_NAME_IS_x11
2110 /*****************************************************************************
2111 * SetPalette: sets an 8 bpp palette
2112 *****************************************************************************
2113 * This function sets the palette given as an argument. It does not return
2114 * anything, but could later send information on which colors it was unable
2116 *****************************************************************************/
2117 static void SetPalette( vout_thread_t *p_vout,
2118 uint16_t *red, uint16_t *green, uint16_t *blue )
2121 XColor p_colors[255];
2123 /* allocate palette */
2124 for( i = 0; i < 255; i++ )
2126 /* kludge: colors are indexed reversely because color 255 seems
2127 * to be reserved for black even if we try to set it to white */
2128 p_colors[ i ].pixel = 255 - i;
2129 p_colors[ i ].pad = 0;
2130 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
2131 p_colors[ i ].red = red[ 255 - i ];
2132 p_colors[ i ].blue = blue[ 255 - i ];
2133 p_colors[ i ].green = green[ 255 - i ];
2136 XStoreColors( p_vout->p_sys->p_display,
2137 p_vout->p_sys->colormap, p_colors, 255 );
2141 /*****************************************************************************
2142 * TestNetWMSupport: tests for Extended Window Manager Hints support
2143 *****************************************************************************/
2144 static void TestNetWMSupport( vout_thread_t *p_vout )
2146 int i_ret, i_format;
2147 unsigned long i, i_items, i_bytesafter;
2148 Atom net_wm_supported, *p_args = NULL;
2150 p_vout->p_sys->b_net_wm_state_fullscreen = VLC_FALSE;
2151 p_vout->p_sys->b_net_wm_state_above = VLC_FALSE;
2152 p_vout->p_sys->b_net_wm_state_below = VLC_FALSE;
2153 p_vout->p_sys->b_net_wm_state_stays_on_top = VLC_FALSE;
2156 XInternAtom( p_vout->p_sys->p_display, "_NET_SUPPORTED", False );
2158 i_ret = XGetWindowProperty( p_vout->p_sys->p_display,
2159 DefaultRootWindow( p_vout->p_sys->p_display ),
2161 0, 16384, False, AnyPropertyType,
2163 &i_format, &i_items, &i_bytesafter,
2164 (unsigned char **)&p_args );
2166 if( i_ret != Success || i_items == 0 ) return;
2168 msg_Dbg( p_vout, "Window manager supports NetWM" );
2170 p_vout->p_sys->net_wm_state =
2171 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE", False );
2172 p_vout->p_sys->net_wm_state_fullscreen =
2173 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_FULLSCREEN",
2175 p_vout->p_sys->net_wm_state_above =
2176 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_ABOVE", False );
2177 p_vout->p_sys->net_wm_state_below =
2178 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_BELOW", False );
2179 p_vout->p_sys->net_wm_state_stays_on_top =
2180 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_STAYS_ON_TOP",
2183 for( i = 0; i < i_items; i++ )
2185 if( p_args[i] == p_vout->p_sys->net_wm_state_fullscreen )
2188 "Window manager supports _NET_WM_STATE_FULLSCREEN" );
2189 p_vout->p_sys->b_net_wm_state_fullscreen = VLC_TRUE;
2191 else if( p_args[i] == p_vout->p_sys->net_wm_state_above )
2193 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_ABOVE" );
2194 p_vout->p_sys->b_net_wm_state_above = VLC_TRUE;
2196 else if( p_args[i] == p_vout->p_sys->net_wm_state_below )
2198 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_BELOW" );
2199 p_vout->p_sys->b_net_wm_state_below = VLC_TRUE;
2201 else if( p_args[i] == p_vout->p_sys->net_wm_state_stays_on_top )
2204 "Window manager supports _NET_WM_STATE_STAYS_ON_TOP" );
2205 p_vout->p_sys->b_net_wm_state_stays_on_top = VLC_TRUE;