1 /*****************************************************************************
2 * xcommon.c: Functions common to the X11 and XVideo plugins
3 *****************************************************************************
4 * Copyright (C) 1998-2001 the VideoLAN team
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Sam Hocevar <sam@zoy.org>
9 * David Kennedy <dkennedy@tinytoad.com>
10 * Gildas Bazin <gbazin@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <errno.h> /* ENOMEM */
31 #include <stdlib.h> /* free() */
32 #include <string.h> /* strerror() */
39 #ifdef HAVE_MACHINE_PARAM_H
41 # include <machine/param.h>
42 # include <sys/types.h> /* typedef ushort */
47 # include <netinet/in.h> /* BSD: struct in_addr */
51 # include <sys/shm.h> /* shmget(), shmctl() */
55 #include <X11/Xproto.h>
57 #include <X11/Xutil.h>
58 #include <X11/keysym.h>
60 # include <X11/extensions/XShm.h>
62 #ifdef DPMSINFO_IN_DPMS_H
63 # include <X11/extensions/dpms.h>
66 #ifdef MODULE_NAME_IS_xvideo
67 # include <X11/extensions/Xv.h>
68 # include <X11/extensions/Xvlib.h>
71 #ifdef MODULE_NAME_IS_glx
76 # include <X11/extensions/Xinerama.h>
81 /*****************************************************************************
83 *****************************************************************************/
84 int E_(Activate) ( vlc_object_t * );
85 void E_(Deactivate) ( vlc_object_t * );
87 static int InitVideo ( vout_thread_t * );
88 static void EndVideo ( vout_thread_t * );
89 static void DisplayVideo ( vout_thread_t *, picture_t * );
90 static int ManageVideo ( vout_thread_t * );
91 static int Control ( vout_thread_t *, int, va_list );
93 static int InitDisplay ( vout_thread_t * );
95 static int CreateWindow ( vout_thread_t *, x11_window_t * );
96 static void DestroyWindow ( vout_thread_t *, x11_window_t * );
98 static int NewPicture ( vout_thread_t *, picture_t * );
99 static void FreePicture ( vout_thread_t *, picture_t * );
101 static IMAGE_TYPE *CreateImage ( vout_thread_t *,
102 Display *, EXTRA_ARGS, int, int );
103 #ifdef HAVE_SYS_SHM_H
104 static IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
105 Display *, EXTRA_ARGS_SHM, int, int );
106 static vlc_bool_t b_shm = VLC_TRUE;
109 static void ToggleFullScreen ( vout_thread_t * );
111 static void EnableXScreenSaver ( vout_thread_t * );
112 static void DisableXScreenSaver ( vout_thread_t * );
114 static void CreateCursor ( vout_thread_t * );
115 static void DestroyCursor ( vout_thread_t * );
116 static void ToggleCursor ( vout_thread_t * );
118 #ifdef MODULE_NAME_IS_xvideo
119 static int XVideoGetPort ( vout_thread_t *, vlc_fourcc_t, vlc_fourcc_t * );
120 static void XVideoReleasePort( vout_thread_t *, int );
123 #ifdef MODULE_NAME_IS_x11
124 static void SetPalette ( vout_thread_t *,
125 uint16_t *, uint16_t *, uint16_t * );
128 static void TestNetWMSupport( vout_thread_t * );
129 static int ConvertKey( int );
131 static int WindowOnTop( vout_thread_t *, vlc_bool_t );
133 static int X11ErrorHandler( Display *, XErrorEvent * );
135 /*****************************************************************************
136 * Activate: allocate X11 video thread output method
137 *****************************************************************************
138 * This function allocate and initialize a X11 vout method. It uses some of the
139 * vout properties to choose the window size, and change them according to the
140 * actual properties of the display.
141 *****************************************************************************/
142 int E_(Activate) ( vlc_object_t *p_this )
144 vout_thread_t *p_vout = (vout_thread_t *)p_this;
148 #ifdef MODULE_NAME_IS_xvideo
150 vlc_fourcc_t i_chroma = 0;
151 vlc_bool_t b_chroma = 0;
154 p_vout->pf_init = InitVideo;
155 p_vout->pf_end = EndVideo;
156 p_vout->pf_manage = ManageVideo;
157 p_vout->pf_render = NULL;
158 p_vout->pf_display = DisplayVideo;
159 p_vout->pf_control = Control;
161 /* Allocate structure */
162 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
163 if( p_vout->p_sys == NULL )
165 msg_Err( p_vout, "out of memory" );
169 vlc_mutex_init( p_vout, &p_vout->p_sys->lock );
171 /* Open display, using the "display" config variable or the DISPLAY
172 * environment variable */
173 psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
175 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
177 if( p_vout->p_sys->p_display == NULL ) /* error */
179 msg_Err( p_vout, "cannot open display %s",
180 XDisplayName( psz_display ) );
181 free( p_vout->p_sys );
182 if( psz_display ) free( psz_display );
185 if( psz_display ) free( psz_display );
187 /* Replace error handler so we can intercept some non-fatal errors */
188 XSetErrorHandler( X11ErrorHandler );
190 /* Get a screen ID matching the XOpenDisplay return value */
191 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
193 #ifdef MODULE_NAME_IS_xvideo
194 psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
197 if( strlen( psz_chroma ) >= 4 )
199 /* Do not use direct assignment because we are not sure of the
201 memcpy(&i_chroma, psz_chroma, 4);
210 msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
211 i_chroma, (char*)&i_chroma );
215 i_chroma = p_vout->render.i_chroma;
218 /* Check that we have access to an XVideo port providing this chroma */
219 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, VLC2X11_FOURCC(i_chroma),
220 &p_vout->output.i_chroma );
221 if( p_vout->p_sys->i_xvport < 0 )
223 /* If a specific chroma format was requested, then we don't try to
224 * be cleverer than the user. He knew pretty well what he wanted. */
227 XCloseDisplay( p_vout->p_sys->p_display );
228 free( p_vout->p_sys );
232 /* It failed, but it's not completely lost ! We try to open an
233 * XVideo port for an YUY2 picture. We'll need to do an YUV
234 * conversion, but at least it has got scaling. */
235 p_vout->p_sys->i_xvport =
236 XVideoGetPort( p_vout, X11_FOURCC('Y','U','Y','2'),
237 &p_vout->output.i_chroma );
238 if( p_vout->p_sys->i_xvport < 0 )
240 /* It failed, but it's not completely lost ! We try to open an
241 * XVideo port for a simple 16bpp RGB picture. We'll need to do
242 * an YUV conversion, but at least it has got scaling. */
243 p_vout->p_sys->i_xvport =
244 XVideoGetPort( p_vout, X11_FOURCC('R','V','1','6'),
245 &p_vout->output.i_chroma );
246 if( p_vout->p_sys->i_xvport < 0 )
248 XCloseDisplay( p_vout->p_sys->p_display );
249 free( p_vout->p_sys );
254 p_vout->output.i_chroma = X112VLC_FOURCC(p_vout->output.i_chroma);
257 /* Create blank cursor (for mouse cursor autohiding) */
258 p_vout->p_sys->i_time_mouse_last_moved = mdate();
259 p_vout->p_sys->b_mouse_pointer_visible = 1;
260 CreateCursor( p_vout );
262 /* Set main window's size */
263 p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
264 p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
265 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
266 /* Spawn base window - this window will include the video output window,
267 * but also command buttons, subtitles and other indicators */
268 if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
270 msg_Err( p_vout, "cannot create X11 window" );
271 DestroyCursor( p_vout );
272 XCloseDisplay( p_vout->p_sys->p_display );
273 free( p_vout->p_sys );
277 /* Open and initialize device. */
278 if( InitDisplay( p_vout ) )
280 msg_Err( p_vout, "cannot initialize X11 display" );
281 DestroyCursor( p_vout );
282 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
283 XCloseDisplay( p_vout->p_sys->p_display );
284 free( p_vout->p_sys );
288 /* Disable screen saver */
289 DisableXScreenSaver( p_vout );
292 p_vout->p_sys->b_altfullscreen = 0;
293 p_vout->p_sys->i_time_button_last_pressed = 0;
295 TestNetWMSupport( p_vout );
297 /* Variable to indicate if the window should be on top of others */
298 /* Trigger a callback right now */
299 var_Get( p_vout, "video-on-top", &val );
300 var_Set( p_vout, "video-on-top", val );
305 /*****************************************************************************
306 * Deactivate: destroy X11 video thread output method
307 *****************************************************************************
308 * Terminate an output method created by Open
309 *****************************************************************************/
310 void E_(Deactivate) ( vlc_object_t *p_this )
312 vout_thread_t *p_vout = (vout_thread_t *)p_this;
314 /* If the fullscreen window is still open, close it */
315 if( p_vout->b_fullscreen )
317 ToggleFullScreen( p_vout );
320 /* Restore cursor if it was blanked */
321 if( !p_vout->p_sys->b_mouse_pointer_visible )
323 ToggleCursor( p_vout );
326 #ifdef MODULE_NAME_IS_x11
327 /* Destroy colormap */
328 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
330 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
332 #elif defined(MODULE_NAME_IS_xvideo)
333 XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
336 DestroyCursor( p_vout );
337 EnableXScreenSaver( p_vout );
338 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
340 XCloseDisplay( p_vout->p_sys->p_display );
342 /* Destroy structure */
343 vlc_mutex_destroy( &p_vout->p_sys->lock );
344 free( p_vout->p_sys );
347 /*****************************************************************************
348 * InitVideo: initialize X11 video thread output method
349 *****************************************************************************
350 * This function create the XImages needed by the output thread. It is called
351 * at the beginning of the thread, but also each time the window is resized.
352 *****************************************************************************/
353 static int InitVideo( vout_thread_t *p_vout )
358 I_OUTPUTPICTURES = 0;
360 #ifdef MODULE_NAME_IS_xvideo
361 /* Initialize the output structure; we already found an XVideo port,
362 * and the corresponding chroma we will be using. Since we can
363 * arbitrary scale, stick to the coordinates and aspect. */
364 p_vout->output.i_width = p_vout->render.i_width;
365 p_vout->output.i_height = p_vout->render.i_height;
366 p_vout->output.i_aspect = p_vout->render.i_aspect;
368 p_vout->fmt_out = p_vout->fmt_in;
369 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
371 switch( p_vout->output.i_chroma )
373 case VLC_FOURCC('R','V','1','5'):
374 p_vout->output.i_rmask = 0x001f;
375 p_vout->output.i_gmask = 0x07e0;
376 p_vout->output.i_bmask = 0xf800;
378 case VLC_FOURCC('R','V','1','6'):
379 p_vout->output.i_rmask = 0x001f;
380 p_vout->output.i_gmask = 0x03e0;
381 p_vout->output.i_bmask = 0x7c00;
385 #elif defined(MODULE_NAME_IS_x11)
386 /* Initialize the output structure: RGB with square pixels, whatever
387 * the input format is, since it's the only format we know */
388 switch( p_vout->p_sys->i_screen_depth )
390 case 8: /* FIXME: set the palette */
391 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
393 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
395 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
398 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
400 msg_Err( p_vout, "unknown screen depth %i",
401 p_vout->p_sys->i_screen_depth );
405 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
406 p_vout->p_sys->p_win->i_height,
408 &p_vout->fmt_out.i_visible_width,
409 &p_vout->fmt_out.i_visible_height );
411 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
413 p_vout->output.i_width = p_vout->fmt_out.i_width =
414 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_width /
415 p_vout->fmt_in.i_visible_width;
416 p_vout->output.i_height = p_vout->fmt_out.i_height =
417 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_height /
418 p_vout->fmt_in.i_visible_height;
419 p_vout->fmt_out.i_x_offset =
420 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_x_offset /
421 p_vout->fmt_in.i_visible_width;
422 p_vout->fmt_out.i_y_offset =
423 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_y_offset /
424 p_vout->fmt_in.i_visible_height;
426 p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_sar_den = 1;
427 p_vout->output.i_aspect = p_vout->fmt_out.i_aspect =
428 p_vout->fmt_out.i_width * VOUT_ASPECT_FACTOR /p_vout->fmt_out.i_height;
430 msg_Dbg( p_vout, "x11 image size %ix%i (%i,%i,%ix%i)",
431 p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
432 p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
433 p_vout->fmt_out.i_visible_width,
434 p_vout->fmt_out.i_visible_height );
437 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
438 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
442 /* Find an empty picture slot */
443 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
445 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
447 p_pic = p_vout->p_picture + i_index;
452 /* Allocate the picture */
453 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
458 p_pic->i_status = DESTROYED_PICTURE;
459 p_pic->i_type = DIRECT_PICTURE;
461 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
466 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
468 /* U and V inverted compared to I420
469 * Fixme: this should be handled by the vout core */
470 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
471 p_vout->fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
477 /*****************************************************************************
478 * DisplayVideo: displays previously rendered output
479 *****************************************************************************
480 * This function sends the currently rendered image to X11 server.
481 * (The Xv extension takes care of "double-buffering".)
482 *****************************************************************************/
483 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
485 int i_width, i_height, i_x, i_y;
487 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
488 p_vout->p_sys->p_win->i_height,
489 &i_x, &i_y, &i_width, &i_height );
491 vlc_mutex_lock( &p_vout->p_sys->lock );
493 #ifdef HAVE_SYS_SHM_H
494 if( p_vout->p_sys->b_shm )
496 /* Display rendered image using shared memory extension */
497 # ifdef MODULE_NAME_IS_xvideo
498 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
499 p_vout->p_sys->p_win->video_window,
500 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
501 p_vout->fmt_out.i_x_offset,
502 p_vout->fmt_out.i_y_offset,
503 p_vout->fmt_out.i_visible_width,
504 p_vout->fmt_out.i_visible_height,
505 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
506 False /* Don't put True here or you'll waste your CPU */ );
508 XShmPutImage( p_vout->p_sys->p_display,
509 p_vout->p_sys->p_win->video_window,
510 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
511 p_vout->fmt_out.i_x_offset,
512 p_vout->fmt_out.i_y_offset,
513 0 /*dest_x*/, 0 /*dest_y*/,
514 p_vout->fmt_out.i_visible_width,
515 p_vout->fmt_out.i_visible_height,
516 False /* Don't put True here ! */ );
520 #endif /* HAVE_SYS_SHM_H */
522 /* Use standard XPutImage -- this is gonna be slow ! */
523 #ifdef MODULE_NAME_IS_xvideo
524 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
525 p_vout->p_sys->p_win->video_window,
526 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
527 p_vout->fmt_out.i_x_offset,
528 p_vout->fmt_out.i_y_offset,
529 p_vout->fmt_out.i_visible_width,
530 p_vout->fmt_out.i_visible_height,
531 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
533 XPutImage( p_vout->p_sys->p_display,
534 p_vout->p_sys->p_win->video_window,
535 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
536 p_vout->fmt_out.i_x_offset,
537 p_vout->fmt_out.i_y_offset,
538 0 /*dest_x*/, 0 /*dest_y*/,
539 p_vout->fmt_out.i_visible_width,
540 p_vout->fmt_out.i_visible_height );
544 /* Make sure the command is sent now - do NOT use XFlush !*/
545 XSync( p_vout->p_sys->p_display, False );
547 vlc_mutex_unlock( &p_vout->p_sys->lock );
550 /*****************************************************************************
551 * ManageVideo: handle X11 events
552 *****************************************************************************
553 * This function should be called regularly by video output thread. It manages
554 * X11 events and allows window resizing. It returns a non null value on
556 *****************************************************************************/
557 static int ManageVideo( vout_thread_t *p_vout )
559 XEvent xevent; /* X11 event */
562 vlc_mutex_lock( &p_vout->p_sys->lock );
564 /* Handle events from the owner window */
565 if( p_vout->p_sys->p_win->owner_window )
567 while( XCheckWindowEvent( p_vout->p_sys->p_display,
568 p_vout->p_sys->p_win->owner_window,
569 StructureNotifyMask, &xevent ) == True )
571 /* ConfigureNotify event: prepare */
572 if( xevent.type == ConfigureNotify )
574 /* Update dimensions */
575 XResizeWindow( p_vout->p_sys->p_display,
576 p_vout->p_sys->p_win->base_window,
577 xevent.xconfigure.width,
578 xevent.xconfigure.height );
583 /* Handle X11 events: ConfigureNotify events are parsed to know if the
584 * output window's size changed, MapNotify and UnmapNotify to know if the
585 * window is mapped (and if the display is useful), and ClientMessages
586 * to intercept window destruction requests */
588 while( XCheckWindowEvent( p_vout->p_sys->p_display,
589 p_vout->p_sys->p_win->base_window,
590 StructureNotifyMask | KeyPressMask |
591 ButtonPressMask | ButtonReleaseMask |
592 PointerMotionMask | Button1MotionMask , &xevent )
595 /* ConfigureNotify event: prepare */
596 if( xevent.type == ConfigureNotify )
598 if( (unsigned int)xevent.xconfigure.width
599 != p_vout->p_sys->p_win->i_width
600 || (unsigned int)xevent.xconfigure.height
601 != p_vout->p_sys->p_win->i_height )
603 /* Update dimensions */
604 p_vout->i_changes |= VOUT_SIZE_CHANGE;
605 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
606 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
610 else if( xevent.type == KeyPress )
612 unsigned int state = xevent.xkey.state;
614 char i_key; /* ISO Latin-1 key */
616 /* We may have keys like F1 trough F12, ESC ... */
617 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
618 xevent.xkey.keycode, 0 );
619 val.i_int = ConvertKey( (int)x_key_symbol );
621 xevent.xkey.state &= ~ShiftMask;
622 xevent.xkey.state &= ~ControlMask;
623 xevent.xkey.state &= ~Mod1Mask;
626 XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
629 * The reason why I use this instead of XK_0 is that
630 * with XLookupString, we don't have to care about
637 if( state & ShiftMask )
639 val.i_int |= KEY_MODIFIER_SHIFT;
641 if( state & ControlMask )
643 val.i_int |= KEY_MODIFIER_CTRL;
645 if( state & Mod1Mask )
647 val.i_int |= KEY_MODIFIER_ALT;
649 var_Set( p_vout->p_vlc, "key-pressed", val );
653 else if( xevent.type == ButtonPress )
655 switch( ((XButtonEvent *)&xevent)->button )
658 var_Get( p_vout, "mouse-button-down", &val );
660 var_Set( p_vout, "mouse-button-down", val );
662 /* detect double-clicks */
663 if( ( ((XButtonEvent *)&xevent)->time -
664 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
666 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
669 p_vout->p_sys->i_time_button_last_pressed =
670 ((XButtonEvent *)&xevent)->time;
673 var_Get( p_vout, "mouse-button-down", &val );
675 var_Set( p_vout, "mouse-button-down", val );
679 var_Get( p_vout, "mouse-button-down", &val );
681 var_Set( p_vout, "mouse-button-down", val );
685 var_Get( p_vout, "mouse-button-down", &val );
687 var_Set( p_vout, "mouse-button-down", val );
691 var_Get( p_vout, "mouse-button-down", &val );
693 var_Set( p_vout, "mouse-button-down", val );
698 else if( xevent.type == ButtonRelease )
700 switch( ((XButtonEvent *)&xevent)->button )
703 var_Get( p_vout, "mouse-button-down", &val );
705 var_Set( p_vout, "mouse-button-down", val );
707 val.b_bool = VLC_TRUE;
708 var_Set( p_vout, "mouse-clicked", val );
713 playlist_t *p_playlist;
715 var_Get( p_vout, "mouse-button-down", &val );
717 var_Set( p_vout, "mouse-button-down", val );
719 p_playlist = vlc_object_find( p_vout,
722 if( p_playlist != NULL )
725 var_Get( p_playlist, "intf-show", &val );
726 val.b_bool = !val.b_bool;
727 var_Set( p_playlist, "intf-show", val );
728 vlc_object_release( p_playlist );
735 intf_thread_t *p_intf;
736 playlist_t *p_playlist;
738 var_Get( p_vout, "mouse-button-down", &val );
740 var_Set( p_vout, "mouse-button-down", val );
741 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
745 p_intf->b_menu_change = 1;
746 vlc_object_release( p_intf );
749 p_playlist = vlc_object_find( p_vout,
752 if( p_playlist != NULL )
754 vlc_value_t val; val.b_bool = VLC_TRUE;
755 var_Set( p_playlist, "intf-popupmenu", val );
756 vlc_object_release( p_playlist );
762 var_Get( p_vout, "mouse-button-down", &val );
764 var_Set( p_vout, "mouse-button-down", val );
768 var_Get( p_vout, "mouse-button-down", &val );
770 var_Set( p_vout, "mouse-button-down", val );
776 else if( xevent.type == MotionNotify )
778 int i_width, i_height, i_x, i_y;
781 /* somewhat different use for vout_PlacePicture:
782 * here the values are needed to give to mouse coordinates
783 * in the original picture space */
784 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
785 p_vout->p_sys->p_win->i_height,
786 &i_x, &i_y, &i_width, &i_height );
788 val.i_int = ( xevent.xmotion.x - i_x ) *
789 p_vout->fmt_in.i_visible_width / i_width +
790 p_vout->fmt_in.i_x_offset;
791 var_Set( p_vout, "mouse-x", val );
792 val.i_int = ( xevent.xmotion.y - i_y ) *
793 p_vout->fmt_in.i_visible_height / i_height +
794 p_vout->fmt_in.i_y_offset;
795 var_Set( p_vout, "mouse-y", val );
797 val.b_bool = VLC_TRUE;
798 var_Set( p_vout, "mouse-moved", val );
800 p_vout->p_sys->i_time_mouse_last_moved = mdate();
801 if( ! p_vout->p_sys->b_mouse_pointer_visible )
803 ToggleCursor( p_vout );
806 else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
807 || xevent.type == MapNotify
808 || xevent.type == UnmapNotify )
810 /* Ignore these events */
812 else /* Other events */
814 msg_Warn( p_vout, "unhandled event %d received", xevent.type );
818 /* Handle events for video output sub-window */
819 while( XCheckWindowEvent( p_vout->p_sys->p_display,
820 p_vout->p_sys->p_win->video_window,
821 ExposureMask, &xevent ) == True )
823 /* Window exposed (only handled if stream playback is paused) */
824 if( xevent.type == Expose )
826 if( ((XExposeEvent *)&xevent)->count == 0 )
828 /* (if this is the last a collection of expose events...) */
830 if( p_vout->p_vlc->p_input_bank->pp_input[0] != NULL )
832 if( PAUSE_S == p_vout->p_vlc->p_input_bank->pp_input[0]
833 ->stream.control.i_status )
835 /* XVideoDisplay( p_vout )*/;
843 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
844 * are handled - according to the man pages, the format is always 32
846 while( XCheckTypedEvent( p_vout->p_sys->p_display,
847 ClientMessage, &xevent ) )
849 if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
850 && ((Atom)xevent.xclient.data.l[0]
851 == p_vout->p_sys->p_win->wm_delete_window ) )
853 /* the user wants to close the window */
854 playlist_t * p_playlist =
855 (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
857 if( p_playlist != NULL )
859 playlist_Stop( p_playlist );
860 vlc_object_release( p_playlist );
868 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
872 /* Update the object variable and trigger callback */
873 val.b_bool = !p_vout->b_fullscreen;
874 var_Set( p_vout, "fullscreen", val );
876 ToggleFullScreen( p_vout );
877 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
880 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
881 p_vout->i_changes & VOUT_ASPECT_CHANGE )
883 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
884 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
886 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
887 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
888 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
889 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
890 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
891 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
892 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
893 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
895 p_vout->i_changes |= VOUT_SIZE_CHANGE;
901 * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
902 * the size flag inside the fullscreen routine)
904 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
906 int i_width, i_height, i_x, i_y;
908 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
910 #ifdef MODULE_NAME_IS_x11
911 /* We need to signal the vout thread about the size change because it
912 * is doing the rescaling */
913 p_vout->i_changes |= VOUT_SIZE_CHANGE;
916 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
917 p_vout->p_sys->p_win->i_height,
918 &i_x, &i_y, &i_width, &i_height );
920 XMoveResizeWindow( p_vout->p_sys->p_display,
921 p_vout->p_sys->p_win->video_window,
922 i_x, i_y, i_width, i_height );
925 /* Autohide Cursour */
926 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
928 /* Hide the mouse automatically */
929 if( p_vout->p_sys->b_mouse_pointer_visible )
931 ToggleCursor( p_vout );
935 vlc_mutex_unlock( &p_vout->p_sys->lock );
940 /*****************************************************************************
941 * EndVideo: terminate X11 video thread output method
942 *****************************************************************************
943 * Destroy the X11 XImages created by Init. It is called at the end of
944 * the thread, but also each time the window is resized.
945 *****************************************************************************/
946 static void EndVideo( vout_thread_t *p_vout )
950 /* Free the direct buffers we allocated */
951 for( i_index = I_OUTPUTPICTURES ; i_index ; )
954 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
958 /* following functions are local */
960 /*****************************************************************************
961 * CreateWindow: open and set-up X11 main window
962 *****************************************************************************/
963 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
965 XSizeHints xsize_hints;
966 XSetWindowAttributes xwindow_attributes;
970 vlc_bool_t b_expose = VLC_FALSE;
971 vlc_bool_t b_configure_notify = VLC_FALSE;
972 vlc_bool_t b_map_notify = VLC_FALSE;
975 /* Prepare window manager hints and properties */
976 p_win->wm_protocols =
977 XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
978 p_win->wm_delete_window =
979 XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
981 /* Never have a 0-pixel-wide window */
982 xsize_hints.min_width = 2;
983 xsize_hints.min_height = 1;
985 /* Prepare window attributes */
986 xwindow_attributes.backing_store = Always; /* save the hidden part */
987 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
988 p_vout->p_sys->i_screen);
989 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
991 if( !p_vout->b_fullscreen )
993 p_win->owner_window =
994 (Window)vout_RequestWindow( p_vout, &p_win->i_x, &p_win->i_y,
995 &p_win->i_width, &p_win->i_height );
997 xsize_hints.base_width = xsize_hints.width = p_win->i_width;
998 xsize_hints.base_height = xsize_hints.height = p_win->i_height;
999 xsize_hints.flags = PSize | PMinSize;
1001 if( p_win->i_x >=0 || p_win->i_y >= 0 )
1003 xsize_hints.x = p_win->i_x;
1004 xsize_hints.y = p_win->i_y;
1005 xsize_hints.flags |= PPosition;
1010 /* Fullscreen window size and position */
1011 p_win->owner_window = 0;
1012 p_win->i_x = p_win->i_y = 0;
1014 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1016 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1019 if( !p_win->owner_window )
1021 /* Create the window and set hints - the window must receive
1022 * ConfigureNotify events, and until it is displayed, Expose and
1023 * MapNotify events. */
1025 p_win->base_window =
1026 XCreateWindow( p_vout->p_sys->p_display,
1027 DefaultRootWindow( p_vout->p_sys->p_display ),
1028 p_win->i_x, p_win->i_y,
1029 p_win->i_width, p_win->i_height,
1032 CWBackingStore | CWBackPixel | CWEventMask,
1033 &xwindow_attributes );
1035 if( !p_vout->b_fullscreen )
1037 /* Set window manager hints and properties: size hints, command,
1038 * window's name, and accepted protocols */
1039 XSetWMNormalHints( p_vout->p_sys->p_display,
1040 p_win->base_window, &xsize_hints );
1041 XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
1042 p_vout->p_vlc->ppsz_argv, p_vout->p_vlc->i_argc );
1044 if( !var_GetBool( p_vout, "video-deco") )
1047 mwmhints_t mwmhints;
1049 mwmhints.flags = MWM_HINTS_DECORATIONS;
1050 mwmhints.decorations = False;
1052 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1055 XChangeProperty( p_vout->p_sys->p_display,
1057 prop, prop, 32, PropModeReplace,
1058 (unsigned char *)&mwmhints,
1059 PROP_MWM_HINTS_ELEMENTS );
1063 var_Get( p_vout, "video-title", &val );
1064 if( !val.psz_string || !*val.psz_string )
1066 XStoreName( p_vout->p_sys->p_display, p_win->base_window,
1067 #ifdef MODULE_NAME_IS_x11
1068 VOUT_TITLE " (X11 output)"
1069 #elif defined(MODULE_NAME_IS_glx)
1070 VOUT_TITLE " (GLX output)"
1072 VOUT_TITLE " (XVideo output)"
1078 XStoreName( p_vout->p_sys->p_display,
1079 p_win->base_window, val.psz_string );
1087 unsigned int dummy2, dummy3;
1089 /* Select events we are interested in. */
1090 XSelectInput( p_vout->p_sys->p_display, p_win->owner_window,
1091 StructureNotifyMask );
1093 /* Get the parent window's geometry information */
1094 XGetGeometry( p_vout->p_sys->p_display, p_win->owner_window,
1095 &dummy1, &dummy2, &dummy3,
1100 /* We are already configured */
1101 b_configure_notify = VLC_TRUE;
1103 /* From man XSelectInput: only one client at a time can select a
1104 * ButtonPress event, so we need to open a new window anyway. */
1105 p_win->base_window =
1106 XCreateWindow( p_vout->p_sys->p_display,
1107 p_win->owner_window,
1109 p_win->i_width, p_win->i_height,
1111 0, CopyFromParent, 0,
1112 CWBackingStore | CWBackPixel | CWEventMask,
1113 &xwindow_attributes );
1116 if( (p_win->wm_protocols == None) /* use WM_DELETE_WINDOW */
1117 || (p_win->wm_delete_window == None)
1118 || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
1119 &p_win->wm_delete_window, 1 ) )
1121 /* WM_DELETE_WINDOW is not supported by window manager */
1122 msg_Warn( p_vout, "missing or bad window manager" );
1125 /* Creation of a graphic context that doesn't generate a GraphicsExpose
1126 * event when using functions like XCopyArea */
1127 xgcvalues.graphics_exposures = False;
1128 p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1130 GCGraphicsExposures, &xgcvalues );
1132 /* Send orders to server, and wait until window is displayed - three
1133 * events must be received: a MapNotify event, an Expose event allowing
1134 * drawing in the window, and a ConfigureNotify to get the window
1135 * dimensions. Once those events have been received, only
1136 * ConfigureNotify events need to be received. */
1137 XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1140 XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1141 SubstructureNotifyMask | StructureNotifyMask |
1142 ExposureMask, &xevent);
1143 if( (xevent.type == Expose)
1144 && (xevent.xexpose.window == p_win->base_window) )
1146 b_expose = VLC_TRUE;
1147 /* ConfigureNotify isn't sent if there isn't a window manager.
1148 * Expose should be the last event to be received so it should
1149 * be fine to assume we won't receive it anymore. */
1150 b_configure_notify = VLC_TRUE;
1152 else if( (xevent.type == MapNotify)
1153 && (xevent.xmap.window == p_win->base_window) )
1155 b_map_notify = VLC_TRUE;
1157 else if( (xevent.type == ConfigureNotify)
1158 && (xevent.xconfigure.window == p_win->base_window) )
1160 b_configure_notify = VLC_TRUE;
1161 p_win->i_width = xevent.xconfigure.width;
1162 p_win->i_height = xevent.xconfigure.height;
1164 } while( !( b_expose && b_configure_notify && b_map_notify ) );
1166 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1167 StructureNotifyMask | KeyPressMask |
1168 ButtonPressMask | ButtonReleaseMask |
1169 PointerMotionMask );
1171 #ifdef MODULE_NAME_IS_x11
1172 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1174 /* Allocate a new palette */
1175 p_vout->p_sys->colormap =
1176 XCreateColormap( p_vout->p_sys->p_display,
1177 DefaultRootWindow( p_vout->p_sys->p_display ),
1178 DefaultVisual( p_vout->p_sys->p_display,
1179 p_vout->p_sys->i_screen ),
1182 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1183 XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1184 CWColormap, &xwindow_attributes );
1188 /* Create video output sub-window. */
1189 p_win->video_window = XCreateSimpleWindow(
1190 p_vout->p_sys->p_display,
1191 p_win->base_window, 0, 0,
1192 p_win->i_width, p_win->i_height,
1194 BlackPixel( p_vout->p_sys->p_display,
1195 p_vout->p_sys->i_screen ),
1196 WhitePixel( p_vout->p_sys->p_display,
1197 p_vout->p_sys->i_screen ) );
1199 XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1200 BlackPixel( p_vout->p_sys->p_display,
1201 p_vout->p_sys->i_screen ) );
1203 XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1204 XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1207 /* make sure the video window will be centered in the next ManageVideo() */
1208 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1210 /* If the cursor was formerly blank than blank it again */
1211 if( !p_vout->p_sys->b_mouse_pointer_visible )
1213 ToggleCursor( p_vout );
1214 ToggleCursor( p_vout );
1217 /* Do NOT use XFlush here ! */
1218 XSync( p_vout->p_sys->p_display, False );
1220 /* At this stage, the window is open, displayed, and ready to
1222 p_vout->p_sys->p_win = p_win;
1227 /*****************************************************************************
1228 * DestroyWindow: destroy the window
1229 *****************************************************************************
1231 *****************************************************************************/
1232 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1234 /* Do NOT use XFlush here ! */
1235 XSync( p_vout->p_sys->p_display, False );
1237 if( p_win->video_window != None )
1238 XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1240 XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1242 XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1243 XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1245 if( p_win->owner_window )
1246 vout_ReleaseWindow( p_vout, (void *)p_win->owner_window );
1249 /*****************************************************************************
1250 * NewPicture: allocate a picture
1251 *****************************************************************************
1252 * Returns 0 on success, -1 otherwise
1253 *****************************************************************************/
1254 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1256 #ifndef MODULE_NAME_IS_glx
1258 #ifdef MODULE_NAME_IS_xvideo
1262 /* We know the chroma, allocate a buffer which will be used
1263 * directly by the decoder */
1264 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1266 if( p_pic->p_sys == NULL )
1271 /* Fill in picture_t fields */
1272 vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
1273 p_vout->output.i_width, p_vout->output.i_height,
1274 p_vout->output.i_aspect );
1276 #ifdef HAVE_SYS_SHM_H
1277 if( p_vout->p_sys->b_shm )
1279 /* Create image using XShm extension */
1280 p_pic->p_sys->p_image =
1281 CreateShmImage( p_vout, p_vout->p_sys->p_display,
1282 # ifdef MODULE_NAME_IS_xvideo
1283 p_vout->p_sys->i_xvport,
1284 VLC2X11_FOURCC(p_vout->output.i_chroma),
1286 p_vout->p_sys->p_visual,
1287 p_vout->p_sys->i_screen_depth,
1289 &p_pic->p_sys->shminfo,
1290 p_vout->output.i_width, p_vout->output.i_height );
1293 if( !p_vout->p_sys->b_shm || !p_pic->p_sys->p_image )
1294 #endif /* HAVE_SYS_SHM_H */
1296 /* Create image without XShm extension */
1297 p_pic->p_sys->p_image =
1298 CreateImage( p_vout, p_vout->p_sys->p_display,
1299 #ifdef MODULE_NAME_IS_xvideo
1300 p_vout->p_sys->i_xvport,
1301 VLC2X11_FOURCC(p_vout->output.i_chroma),
1302 p_pic->format.i_bits_per_pixel,
1304 p_vout->p_sys->p_visual,
1305 p_vout->p_sys->i_screen_depth,
1306 p_vout->p_sys->i_bytes_per_pixel,
1308 p_vout->output.i_width, p_vout->output.i_height );
1310 #ifdef HAVE_SYS_SHM_H
1311 if( p_pic->p_sys->p_image && p_vout->p_sys->b_shm )
1313 msg_Warn( p_vout, "couldn't create SHM image, disabling SHM." );
1314 p_vout->p_sys->b_shm = VLC_FALSE;
1316 #endif /* HAVE_SYS_SHM_H */
1319 if( p_pic->p_sys->p_image == NULL )
1321 free( p_pic->p_sys );
1325 switch( p_vout->output.i_chroma )
1327 #ifdef MODULE_NAME_IS_xvideo
1328 case VLC_FOURCC('I','4','2','0'):
1329 case VLC_FOURCC('Y','V','1','2'):
1330 case VLC_FOURCC('Y','2','1','1'):
1331 case VLC_FOURCC('Y','U','Y','2'):
1332 case VLC_FOURCC('U','Y','V','Y'):
1333 case VLC_FOURCC('R','V','1','5'):
1334 case VLC_FOURCC('R','V','1','6'):
1335 case VLC_FOURCC('R','V','2','4'): /* Fixme: pixel pitch == 4 ? */
1336 case VLC_FOURCC('R','V','3','2'):
1338 for( i_plane = 0; i_plane < p_pic->p_sys->p_image->num_planes;
1341 p_pic->p[i_plane].p_pixels = p_pic->p_sys->p_image->data
1342 + p_pic->p_sys->p_image->offsets[i_plane];
1343 p_pic->p[i_plane].i_pitch =
1344 p_pic->p_sys->p_image->pitches[i_plane];
1346 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
1348 /* U and V inverted compared to I420
1349 * Fixme: this should be handled by the vout core */
1350 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1351 + p_pic->p_sys->p_image->offsets[2];
1352 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1353 + p_pic->p_sys->p_image->offsets[1];
1358 case VLC_FOURCC('R','G','B','2'):
1359 case VLC_FOURCC('R','V','1','6'):
1360 case VLC_FOURCC('R','V','1','5'):
1361 case VLC_FOURCC('R','V','2','4'):
1362 case VLC_FOURCC('R','V','3','2'):
1364 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1365 p_pic->p->i_visible_lines = p_pic->p_sys->p_image->height;
1366 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1367 + p_pic->p_sys->p_image->xoffset;
1368 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1370 /* p_pic->p->i_pixel_pitch = 4 for RV24 but this should be set
1371 * properly by vout_InitPicture() */
1372 p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
1373 * p_pic->p_sys->p_image->width;
1378 /* Unknown chroma, tell the guy to get lost */
1379 IMAGE_FREE( p_pic->p_sys->p_image );
1380 free( p_pic->p_sys );
1381 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1382 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1383 p_pic->i_planes = 0;
1387 #endif /* !MODULE_NAME_IS_glx */
1392 /*****************************************************************************
1393 * FreePicture: destroy a picture allocated with NewPicture
1394 *****************************************************************************
1395 * Destroy XImage AND associated data. If using Shm, detach shared memory
1396 * segment from server and process, then free it. The XDestroyImage manpage
1397 * says that both the image structure _and_ the data pointed to by the
1398 * image structure are freed, so no need to free p_image->data.
1399 *****************************************************************************/
1400 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1402 /* The order of operations is correct */
1403 #ifdef HAVE_SYS_SHM_H
1404 if( p_vout->p_sys->b_shm )
1406 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
1407 IMAGE_FREE( p_pic->p_sys->p_image );
1409 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
1410 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
1412 msg_Err( p_vout, "cannot detach shared memory (%s)",
1419 IMAGE_FREE( p_pic->p_sys->p_image );
1422 /* Do NOT use XFlush here ! */
1423 XSync( p_vout->p_sys->p_display, False );
1425 free( p_pic->p_sys );
1428 /*****************************************************************************
1429 * ToggleFullScreen: Enable or disable full screen mode
1430 *****************************************************************************
1431 * This function will switch between fullscreen and window mode.
1432 *****************************************************************************/
1433 static void ToggleFullScreen ( vout_thread_t *p_vout )
1437 mwmhints_t mwmhints;
1438 XSetWindowAttributes attributes;
1440 #ifdef HAVE_XINERAMA
1444 p_vout->b_fullscreen = !p_vout->b_fullscreen;
1446 if( p_vout->b_fullscreen )
1448 msg_Dbg( p_vout, "entering fullscreen mode" );
1450 p_vout->p_sys->b_altfullscreen =
1451 config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
1453 XUnmapWindow( p_vout->p_sys->p_display,
1454 p_vout->p_sys->p_win->base_window );
1456 p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
1458 CreateWindow( p_vout, p_vout->p_sys->p_win );
1459 XDestroyWindow( p_vout->p_sys->p_display,
1460 p_vout->p_sys->fullscreen_window.video_window );
1461 XReparentWindow( p_vout->p_sys->p_display,
1462 p_vout->p_sys->original_window.video_window,
1463 p_vout->p_sys->fullscreen_window.base_window, 0, 0 );
1464 p_vout->p_sys->fullscreen_window.video_window =
1465 p_vout->p_sys->original_window.video_window;
1467 /* To my knowledge there are two ways to create a borderless window.
1468 * There's the generic way which is to tell x to bypass the window
1469 * manager, but this creates problems with the focus of other
1471 * The other way is to use the motif property "_MOTIF_WM_HINTS" which
1472 * luckily seems to be supported by most window managers. */
1473 if( !p_vout->p_sys->b_altfullscreen )
1475 mwmhints.flags = MWM_HINTS_DECORATIONS;
1476 mwmhints.decorations = False;
1478 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1480 XChangeProperty( p_vout->p_sys->p_display,
1481 p_vout->p_sys->p_win->base_window,
1482 prop, prop, 32, PropModeReplace,
1483 (unsigned char *)&mwmhints,
1484 PROP_MWM_HINTS_ELEMENTS );
1488 /* brute force way to remove decorations */
1489 attributes.override_redirect = True;
1490 XChangeWindowAttributes( p_vout->p_sys->p_display,
1491 p_vout->p_sys->p_win->base_window,
1495 /* Make sure the change is effective */
1496 XReparentWindow( p_vout->p_sys->p_display,
1497 p_vout->p_sys->p_win->base_window,
1498 DefaultRootWindow( p_vout->p_sys->p_display ),
1502 if( p_vout->p_sys->b_net_wm_state_fullscreen )
1504 XClientMessageEvent event;
1506 memset( &event, 0, sizeof( XClientMessageEvent ) );
1508 event.type = ClientMessage;
1509 event.message_type = p_vout->p_sys->net_wm_state;
1510 event.display = p_vout->p_sys->p_display;
1511 event.window = p_vout->p_sys->p_win->base_window;
1513 event.data.l[ 0 ] = 1; /* set property */
1514 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_fullscreen;
1516 XSendEvent( p_vout->p_sys->p_display,
1517 DefaultRootWindow( p_vout->p_sys->p_display ),
1518 False, SubstructureRedirectMask,
1522 /* Make sure the change is effective */
1523 XReparentWindow( p_vout->p_sys->p_display,
1524 p_vout->p_sys->p_win->base_window,
1525 DefaultRootWindow( p_vout->p_sys->p_display ),
1528 #ifdef HAVE_XINERAMA
1529 if( XineramaQueryExtension( p_vout->p_sys->p_display, &i_d1, &i_d2 ) &&
1530 XineramaIsActive( p_vout->p_sys->p_display ) )
1532 XineramaScreenInfo *screens; /* infos for xinerama */
1535 msg_Dbg( p_vout, "using XFree Xinerama extension");
1537 #define SCREEN p_vout->p_sys->p_win->i_screen
1539 /* Get Information about Xinerama (num of screens) */
1540 screens = XineramaQueryScreens( p_vout->p_sys->p_display,
1543 SCREEN = config_GetInt( p_vout,
1544 MODULE_STRING "-xineramascreen" );
1546 /* just check that user has entered a good value */
1547 if( SCREEN >= i_num_screens || SCREEN < 0 )
1549 msg_Dbg( p_vout, "requested screen number invalid (%d/%d)", SCREEN, i_num_screens );
1553 /* Get the X/Y upper left corner coordinate of the above screen */
1554 p_vout->p_sys->p_win->i_x = screens[SCREEN].x_org;
1555 p_vout->p_sys->p_win->i_y = screens[SCREEN].y_org;
1557 /* Set the Height/width to the screen resolution */
1558 p_vout->p_sys->p_win->i_width = screens[SCREEN].width;
1559 p_vout->p_sys->p_win->i_height = screens[SCREEN].height;
1569 /* The window wasn't necessarily created at the requested size */
1570 p_vout->p_sys->p_win->i_x = p_vout->p_sys->p_win->i_y = 0;
1571 p_vout->p_sys->p_win->i_width =
1572 DisplayWidth( p_vout->p_sys->p_display,
1573 p_vout->p_sys->i_screen );
1574 p_vout->p_sys->p_win->i_height =
1575 DisplayHeight( p_vout->p_sys->p_display,
1576 p_vout->p_sys->i_screen );
1579 XMoveResizeWindow( p_vout->p_sys->p_display,
1580 p_vout->p_sys->p_win->base_window,
1581 p_vout->p_sys->p_win->i_x,
1582 p_vout->p_sys->p_win->i_y,
1583 p_vout->p_sys->p_win->i_width,
1584 p_vout->p_sys->p_win->i_height );
1588 msg_Dbg( p_vout, "leaving fullscreen mode" );
1590 XReparentWindow( p_vout->p_sys->p_display,
1591 p_vout->p_sys->original_window.video_window,
1592 p_vout->p_sys->original_window.base_window, 0, 0 );
1594 p_vout->p_sys->fullscreen_window.video_window = None;
1595 DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
1596 p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
1598 XMapWindow( p_vout->p_sys->p_display,
1599 p_vout->p_sys->p_win->base_window );
1602 /* Unfortunately, using XSync() here is not enough to ensure the
1603 * window has already been mapped because the XMapWindow() request
1604 * has not necessarily been sent directly to our window (remember,
1605 * the call is first redirected to the window manager) */
1608 XWindowEvent( p_vout->p_sys->p_display,
1609 p_vout->p_sys->p_win->base_window,
1610 StructureNotifyMask, &xevent );
1611 } while( xevent.type != MapNotify );
1613 /* Be careful, this can generate a BadMatch error if the window is not
1614 * already mapped by the server (see above) */
1615 XSetInputFocus(p_vout->p_sys->p_display,
1616 p_vout->p_sys->p_win->base_window,
1620 /* signal that the size needs to be updated */
1621 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1624 /*****************************************************************************
1625 * EnableXScreenSaver: enable screen saver
1626 *****************************************************************************
1627 * This function enables the screen saver on a display after it has been
1628 * disabled by XDisableScreenSaver.
1629 * FIXME: what happens if multiple vlc sessions are running at the same
1631 *****************************************************************************/
1632 static void EnableXScreenSaver( vout_thread_t *p_vout )
1634 #ifdef DPMSINFO_IN_DPMS_H
1638 if( p_vout->p_sys->i_ss_timeout )
1640 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1641 p_vout->p_sys->i_ss_interval,
1642 p_vout->p_sys->i_ss_blanking,
1643 p_vout->p_sys->i_ss_exposure );
1646 /* Restore DPMS settings */
1647 #ifdef DPMSINFO_IN_DPMS_H
1648 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1650 if( p_vout->p_sys->b_ss_dpms )
1652 DPMSEnable( p_vout->p_sys->p_display );
1658 /*****************************************************************************
1659 * DisableXScreenSaver: disable screen saver
1660 *****************************************************************************
1661 * See XEnableXScreenSaver
1662 *****************************************************************************/
1663 static void DisableXScreenSaver( vout_thread_t *p_vout )
1665 #ifdef DPMSINFO_IN_DPMS_H
1669 /* Save screen saver information */
1670 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1671 &p_vout->p_sys->i_ss_interval,
1672 &p_vout->p_sys->i_ss_blanking,
1673 &p_vout->p_sys->i_ss_exposure );
1675 /* Disable screen saver */
1676 if( p_vout->p_sys->i_ss_timeout )
1678 XSetScreenSaver( p_vout->p_sys->p_display, 0,
1679 p_vout->p_sys->i_ss_interval,
1680 p_vout->p_sys->i_ss_blanking,
1681 p_vout->p_sys->i_ss_exposure );
1685 #ifdef DPMSINFO_IN_DPMS_H
1686 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1689 /* Save DPMS current state */
1690 DPMSInfo( p_vout->p_sys->p_display, &unused,
1691 &p_vout->p_sys->b_ss_dpms );
1692 DPMSDisable( p_vout->p_sys->p_display );
1697 /*****************************************************************************
1698 * CreateCursor: create a blank mouse pointer
1699 *****************************************************************************/
1700 static void CreateCursor( vout_thread_t *p_vout )
1702 XColor cursor_color;
1704 p_vout->p_sys->cursor_pixmap =
1705 XCreatePixmap( p_vout->p_sys->p_display,
1706 DefaultRootWindow( p_vout->p_sys->p_display ),
1709 XParseColor( p_vout->p_sys->p_display,
1710 XCreateColormap( p_vout->p_sys->p_display,
1712 p_vout->p_sys->p_display ),
1714 p_vout->p_sys->p_display,
1715 p_vout->p_sys->i_screen ),
1717 "black", &cursor_color );
1719 p_vout->p_sys->blank_cursor =
1720 XCreatePixmapCursor( p_vout->p_sys->p_display,
1721 p_vout->p_sys->cursor_pixmap,
1722 p_vout->p_sys->cursor_pixmap,
1723 &cursor_color, &cursor_color, 1, 1 );
1726 /*****************************************************************************
1727 * DestroyCursor: destroy the blank mouse pointer
1728 *****************************************************************************/
1729 static void DestroyCursor( vout_thread_t *p_vout )
1731 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
1734 /*****************************************************************************
1735 * ToggleCursor: hide or show the mouse pointer
1736 *****************************************************************************
1737 * This function hides the X pointer if it is visible by setting the pointer
1738 * sprite to a blank one. To show it again, we disable the sprite.
1739 *****************************************************************************/
1740 static void ToggleCursor( vout_thread_t *p_vout )
1742 if( p_vout->p_sys->b_mouse_pointer_visible )
1744 XDefineCursor( p_vout->p_sys->p_display,
1745 p_vout->p_sys->p_win->base_window,
1746 p_vout->p_sys->blank_cursor );
1747 p_vout->p_sys->b_mouse_pointer_visible = 0;
1751 XUndefineCursor( p_vout->p_sys->p_display,
1752 p_vout->p_sys->p_win->base_window );
1753 p_vout->p_sys->b_mouse_pointer_visible = 1;
1757 #ifdef MODULE_NAME_IS_xvideo
1758 /*****************************************************************************
1759 * XVideoGetPort: get YUV12 port
1760 *****************************************************************************/
1761 static int XVideoGetPort( vout_thread_t *p_vout,
1762 vlc_fourcc_t i_chroma, vlc_fourcc_t *pi_newchroma )
1764 XvAdaptorInfo *p_adaptor;
1766 int i_adaptor, i_num_adaptors, i_requested_adaptor;
1767 int i_selected_port;
1769 switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
1774 case XvBadExtension:
1775 msg_Warn( p_vout, "XvBadExtension" );
1779 msg_Warn( p_vout, "XvBadAlloc" );
1783 msg_Warn( p_vout, "XvQueryExtension failed" );
1787 switch( XvQueryAdaptors( p_vout->p_sys->p_display,
1788 DefaultRootWindow( p_vout->p_sys->p_display ),
1789 &i_num_adaptors, &p_adaptor ) )
1794 case XvBadExtension:
1795 msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
1799 msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
1803 msg_Warn( p_vout, "XvQueryAdaptors failed" );
1807 i_selected_port = -1;
1808 i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
1810 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
1812 XvImageFormatValues *p_formats;
1813 int i_format, i_num_formats;
1816 /* If we requested an adaptor and it's not this one, we aren't
1818 if( i_requested_adaptor != -1 && i_adaptor != i_requested_adaptor )
1823 /* If the adaptor doesn't have the required properties, skip it */
1824 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
1825 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
1830 /* Check that adaptor supports our requested format... */
1831 p_formats = XvListImageFormats( p_vout->p_sys->p_display,
1832 p_adaptor[i_adaptor].base_id,
1836 i_format < i_num_formats && ( i_selected_port == -1 );
1839 XvAttribute *p_attr;
1840 int i_attr, i_num_attributes;
1842 /* If this is not the format we want, or at least a
1843 * similar one, forget it */
1844 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
1849 /* Look for the first available port supporting this format */
1850 for( i_port = p_adaptor[i_adaptor].base_id;
1851 ( i_port < (int)(p_adaptor[i_adaptor].base_id
1852 + p_adaptor[i_adaptor].num_ports) )
1853 && ( i_selected_port == -1 );
1856 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
1859 i_selected_port = i_port;
1860 *pi_newchroma = p_formats[ i_format ].id;
1864 /* If no free port was found, forget it */
1865 if( i_selected_port == -1 )
1870 /* If we found a port, print information about it */
1871 msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
1872 i_adaptor, i_selected_port, p_formats[ i_format ].id,
1873 (char *)&p_formats[ i_format ].id,
1874 ( p_formats[ i_format ].format == XvPacked ) ?
1875 "packed" : "planar" );
1877 /* Make sure XV_AUTOPAINT_COLORKEY is set */
1878 p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
1880 &i_num_attributes );
1882 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
1884 if( !strcmp( p_attr[i_attr].name, "XV_AUTOPAINT_COLORKEY" ) )
1886 const Atom autopaint =
1887 XInternAtom( p_vout->p_sys->p_display,
1888 "XV_AUTOPAINT_COLORKEY", False );
1889 XvSetPortAttribute( p_vout->p_sys->p_display,
1890 i_selected_port, autopaint, 1 );
1895 if( p_attr != NULL )
1901 if( p_formats != NULL )
1908 if( i_num_adaptors > 0 )
1910 XvFreeAdaptorInfo( p_adaptor );
1913 if( i_selected_port == -1 )
1915 int i_chroma_tmp = X112VLC_FOURCC( i_chroma );
1916 if( i_requested_adaptor == -1 )
1918 msg_Warn( p_vout, "no free XVideo port found for format "
1919 "0x%.8x (%4.4s)", i_chroma_tmp, (char*)&i_chroma_tmp );
1923 msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
1924 "XVideo port for format 0x%.8x (%4.4s)",
1925 i_requested_adaptor, i_chroma_tmp, (char*)&i_chroma_tmp );
1929 return i_selected_port;
1932 /*****************************************************************************
1933 * XVideoReleasePort: release YUV12 port
1934 *****************************************************************************/
1935 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
1937 XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
1941 /*****************************************************************************
1942 * InitDisplay: open and initialize X11 device
1943 *****************************************************************************
1944 * Create a window according to video output given size, and set other
1945 * properties according to the display properties.
1946 *****************************************************************************/
1947 static int InitDisplay( vout_thread_t *p_vout )
1949 #ifdef MODULE_NAME_IS_x11
1950 XPixmapFormatValues * p_formats; /* pixmap formats */
1951 XVisualInfo * p_xvisual; /* visuals information */
1952 XVisualInfo xvisual_template; /* visual template */
1953 int i_count; /* array size */
1956 #ifdef HAVE_SYS_SHM_H
1957 p_vout->p_sys->b_shm = 0;
1959 if( config_GetInt( p_vout, MODULE_STRING "-shm" ) )
1962 /* FIXME: As of 2001-03-16, XFree4 for MacOS X does not support Xshm */
1964 p_vout->p_sys->b_shm =
1965 ( XShmQueryExtension( p_vout->p_sys->p_display ) == True );
1968 if( !p_vout->p_sys->b_shm )
1970 msg_Warn( p_vout, "XShm video extension is unavailable" );
1975 msg_Dbg( p_vout, "disabling XShm video extension" );
1979 msg_Warn( p_vout, "XShm video extension is unavailable" );
1983 #ifdef MODULE_NAME_IS_xvideo
1984 /* XXX The brightness and contrast values should be read from environment
1985 * XXX variables... */
1987 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
1988 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
1992 #ifdef MODULE_NAME_IS_x11
1993 /* Initialize structure */
1994 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
1996 /* Get screen depth */
1997 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
1998 p_vout->p_sys->i_screen );
1999 switch( p_vout->p_sys->i_screen_depth )
2003 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
2005 xvisual_template.screen = p_vout->p_sys->i_screen;
2006 xvisual_template.class = DirectColor;
2007 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2008 VisualScreenMask | VisualClassMask,
2009 &xvisual_template, &i_count );
2010 if( p_xvisual == NULL )
2012 msg_Err( p_vout, "no PseudoColor visual available" );
2013 return VLC_EGENERIC;
2015 p_vout->p_sys->i_bytes_per_pixel = 1;
2016 p_vout->output.pf_setpalette = SetPalette;
2023 * Screen depth is higher than 8bpp. TrueColor visual is used.
2025 xvisual_template.screen = p_vout->p_sys->i_screen;
2026 xvisual_template.class = TrueColor;
2027 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2028 VisualScreenMask | VisualClassMask,
2029 &xvisual_template, &i_count );
2030 if( p_xvisual == NULL )
2032 msg_Err( p_vout, "no TrueColor visual available" );
2033 return VLC_EGENERIC;
2036 p_vout->output.i_rmask = p_xvisual->red_mask;
2037 p_vout->output.i_gmask = p_xvisual->green_mask;
2038 p_vout->output.i_bmask = p_xvisual->blue_mask;
2040 /* There is no difference yet between 3 and 4 Bpp. The only way
2041 * to find the actual number of bytes per pixel is to list supported
2042 * pixmap formats. */
2043 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
2044 p_vout->p_sys->i_bytes_per_pixel = 0;
2046 for( ; i_count-- ; p_formats++ )
2048 /* Under XFree4.0, the list contains pixmap formats available
2049 * through all video depths ; so we have to check against current
2051 if( p_formats->depth == (int)p_vout->p_sys->i_screen_depth )
2053 if( p_formats->bits_per_pixel / 8
2054 > (int)p_vout->p_sys->i_bytes_per_pixel )
2056 p_vout->p_sys->i_bytes_per_pixel =
2057 p_formats->bits_per_pixel / 8;
2063 p_vout->p_sys->p_visual = p_xvisual->visual;
2070 #ifdef HAVE_SYS_SHM_H
2071 /*****************************************************************************
2072 * CreateShmImage: create an XImage or XvImage using shared memory extension
2073 *****************************************************************************
2074 * Prepare an XImage or XvImage for display function.
2075 * The order of the operations respects the recommandations of the mit-shm
2076 * document by J.Corbet and K.Packard. Most of the parameters were copied from
2077 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
2078 *****************************************************************************/
2079 static IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
2080 Display* p_display, EXTRA_ARGS_SHM,
2081 int i_width, int i_height )
2083 IMAGE_TYPE *p_image;
2086 /* Create XImage / XvImage */
2087 #ifdef MODULE_NAME_IS_xvideo
2089 /* Make sure the buffer is aligned to multiple of 16 */
2090 i_height = ( i_height + 15 ) >> 4 << 4;
2091 i_width = ( i_width + 15 ) >> 4 << 4;
2093 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2094 i_width, i_height, p_shm );
2096 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2097 p_shm, i_width, i_height );
2099 if( p_image == NULL )
2101 msg_Err( p_vout, "image creation failed" );
2105 /* Allocate shared memory segment - 0776 set the access permission
2106 * rights (like umask), they are not yet supported by all X servers */
2107 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
2108 if( p_shm->shmid < 0 )
2110 msg_Err( p_vout, "cannot allocate shared image data (%s)",
2111 strerror( errno ) );
2112 IMAGE_FREE( p_image );
2116 /* Attach shared memory segment to process (read/write) */
2117 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2118 if(! p_shm->shmaddr )
2120 msg_Err( p_vout, "cannot attach shared memory (%s)",
2122 IMAGE_FREE( p_image );
2123 shmctl( p_shm->shmid, IPC_RMID, 0 );
2127 /* Read-only data. We won't be using XShmGetImage */
2128 p_shm->readOnly = True;
2130 /* Attach shared memory segment to X server */
2131 XSynchronize( p_display, True );
2133 result = XShmAttach( p_display, p_shm );
2134 if( result == False || !b_shm )
2136 msg_Err( p_vout, "cannot attach shared memory to X server" );
2137 IMAGE_FREE( p_image );
2138 shmctl( p_shm->shmid, IPC_RMID, 0 );
2139 shmdt( p_shm->shmaddr );
2142 XSynchronize( p_display, False );
2144 /* Send image to X server. This instruction is required, since having
2145 * built a Shm XImage and not using it causes an error on XCloseDisplay,
2146 * and remember NOT to use XFlush ! */
2147 XSync( p_display, False );
2150 /* Mark the shm segment to be removed when there are no more
2151 * attachements, so it is automatic on process exit or after shmdt */
2152 shmctl( p_shm->shmid, IPC_RMID, 0 );
2159 /*****************************************************************************
2160 * CreateImage: create an XImage or XvImage
2161 *****************************************************************************
2162 * Create a simple image used as a buffer.
2163 *****************************************************************************/
2164 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2165 Display *p_display, EXTRA_ARGS,
2166 int i_width, int i_height )
2168 byte_t * p_data; /* image data storage zone */
2169 IMAGE_TYPE *p_image;
2170 #ifdef MODULE_NAME_IS_x11
2171 int i_quantum; /* XImage quantum (see below) */
2172 int i_bytes_per_line;
2175 /* Allocate memory for image */
2176 #ifdef MODULE_NAME_IS_xvideo
2178 /* Make sure the buffer is aligned to multiple of 16 */
2179 i_height = ( i_height + 15 ) >> 4 << 4;
2180 i_width = ( i_width + 15 ) >> 4 << 4;
2182 p_data = (byte_t *) malloc( i_width * i_height * i_bits_per_pixel / 8 );
2183 #elif defined(MODULE_NAME_IS_x11)
2184 i_bytes_per_line = i_width * i_bytes_per_pixel;
2185 p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
2189 msg_Err( p_vout, "out of memory" );
2193 #ifdef MODULE_NAME_IS_x11
2194 /* Optimize the quantum of a scanline regarding its size - the quantum is
2195 a diviser of the number of bits between the start of two scanlines. */
2196 if( i_bytes_per_line & 0xf )
2200 else if( i_bytes_per_line & 0x10 )
2210 /* Create XImage. p_data will be automatically freed */
2211 #ifdef MODULE_NAME_IS_xvideo
2212 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2213 p_data, i_width, i_height );
2214 #elif defined(MODULE_NAME_IS_x11)
2215 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2216 p_data, i_width, i_height, i_quantum, 0 );
2218 if( p_image == NULL )
2220 msg_Err( p_vout, "XCreateImage() failed" );
2228 /*****************************************************************************
2229 * X11ErrorHandler: replace error handler so we can intercept some of them
2230 *****************************************************************************/
2231 static int X11ErrorHandler( Display * display, XErrorEvent * event )
2233 /* Ingnore errors on XSetInputFocus()
2234 * (they happen when a window is not yet mapped) */
2235 if( event->request_code == X_SetInputFocus )
2237 fprintf(stderr, "XSetInputFocus failed\n");
2241 if( event->request_code == 150 /* MIT-SHM */ &&
2242 event->minor_code == X_ShmAttach )
2244 fprintf(stderr, "XShmAttach failed\n");
2249 XSetErrorHandler(NULL);
2250 return (XSetErrorHandler(X11ErrorHandler))( display, event );
2253 #ifdef MODULE_NAME_IS_x11
2254 /*****************************************************************************
2255 * SetPalette: sets an 8 bpp palette
2256 *****************************************************************************
2257 * This function sets the palette given as an argument. It does not return
2258 * anything, but could later send information on which colors it was unable
2260 *****************************************************************************/
2261 static void SetPalette( vout_thread_t *p_vout,
2262 uint16_t *red, uint16_t *green, uint16_t *blue )
2265 XColor p_colors[255];
2267 /* allocate palette */
2268 for( i = 0; i < 255; i++ )
2270 /* kludge: colors are indexed reversely because color 255 seems
2271 * to be reserved for black even if we try to set it to white */
2272 p_colors[ i ].pixel = 255 - i;
2273 p_colors[ i ].pad = 0;
2274 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
2275 p_colors[ i ].red = red[ 255 - i ];
2276 p_colors[ i ].blue = blue[ 255 - i ];
2277 p_colors[ i ].green = green[ 255 - i ];
2280 XStoreColors( p_vout->p_sys->p_display,
2281 p_vout->p_sys->colormap, p_colors, 255 );
2285 /*****************************************************************************
2286 * Control: control facility for the vout
2287 *****************************************************************************/
2288 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
2291 unsigned int i_width, i_height;
2292 unsigned int *pi_width, *pi_height;
2297 if( p_vout->p_sys->p_win->owner_window )
2298 return vout_ControlWindow( p_vout,
2299 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
2301 pi_width = va_arg( args, unsigned int * );
2302 pi_height = va_arg( args, unsigned int * );
2304 vlc_mutex_lock( &p_vout->p_sys->lock );
2305 *pi_width = p_vout->p_sys->p_win->i_width;
2306 *pi_height = p_vout->p_sys->p_win->i_height;
2307 vlc_mutex_unlock( &p_vout->p_sys->lock );
2311 if( p_vout->p_sys->p_win->owner_window )
2312 return vout_ControlWindow( p_vout,
2313 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
2315 vlc_mutex_lock( &p_vout->p_sys->lock );
2317 i_width = va_arg( args, unsigned int );
2318 i_height = va_arg( args, unsigned int );
2319 if( !i_width ) i_width = p_vout->i_window_width;
2320 if( !i_height ) i_height = p_vout->i_window_height;
2322 /* Update dimensions */
2323 XResizeWindow( p_vout->p_sys->p_display,
2324 p_vout->p_sys->p_win->base_window,
2325 i_width, i_height );
2327 vlc_mutex_unlock( &p_vout->p_sys->lock );
2331 vlc_mutex_lock( &p_vout->p_sys->lock );
2332 XUnmapWindow( p_vout->p_sys->p_display,
2333 p_vout->p_sys->original_window.base_window );
2334 vlc_mutex_unlock( &p_vout->p_sys->lock );
2338 vlc_mutex_lock( &p_vout->p_sys->lock );
2339 XReparentWindow( p_vout->p_sys->p_display,
2340 p_vout->p_sys->original_window.base_window,
2341 DefaultRootWindow( p_vout->p_sys->p_display ),
2343 XSync( p_vout->p_sys->p_display, False );
2344 p_vout->p_sys->original_window.owner_window = 0;
2345 vlc_mutex_unlock( &p_vout->p_sys->lock );
2346 return vout_vaControlDefault( p_vout, i_query, args );
2348 case VOUT_SET_STAY_ON_TOP:
2349 if( p_vout->p_sys->p_win->owner_window )
2350 return vout_ControlWindow( p_vout,
2351 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
2353 b_arg = va_arg( args, vlc_bool_t );
2354 vlc_mutex_lock( &p_vout->p_sys->lock );
2355 WindowOnTop( p_vout, b_arg );
2356 vlc_mutex_unlock( &p_vout->p_sys->lock );
2360 return vout_vaControlDefault( p_vout, i_query, args );
2364 /*****************************************************************************
2365 * TestNetWMSupport: tests for Extended Window Manager Hints support
2366 *****************************************************************************/
2367 static void TestNetWMSupport( vout_thread_t *p_vout )
2369 int i_ret, i_format;
2370 unsigned long i, i_items, i_bytesafter;
2371 Atom net_wm_supported;
2372 union { Atom *p_atom; unsigned char *p_char; } p_args;
2374 p_args.p_atom = NULL;
2376 p_vout->p_sys->b_net_wm_state_fullscreen = VLC_FALSE;
2377 p_vout->p_sys->b_net_wm_state_above = VLC_FALSE;
2378 p_vout->p_sys->b_net_wm_state_below = VLC_FALSE;
2379 p_vout->p_sys->b_net_wm_state_stays_on_top = VLC_FALSE;
2382 XInternAtom( p_vout->p_sys->p_display, "_NET_SUPPORTED", False );
2384 i_ret = XGetWindowProperty( p_vout->p_sys->p_display,
2385 DefaultRootWindow( p_vout->p_sys->p_display ),
2387 0, 16384, False, AnyPropertyType,
2389 &i_format, &i_items, &i_bytesafter,
2390 (unsigned char **)&p_args );
2392 if( i_ret != Success || i_items == 0 ) return;
2394 msg_Dbg( p_vout, "Window manager supports NetWM" );
2396 p_vout->p_sys->net_wm_state =
2397 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE", False );
2398 p_vout->p_sys->net_wm_state_fullscreen =
2399 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_FULLSCREEN",
2401 p_vout->p_sys->net_wm_state_above =
2402 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_ABOVE", False );
2403 p_vout->p_sys->net_wm_state_below =
2404 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_BELOW", False );
2405 p_vout->p_sys->net_wm_state_stays_on_top =
2406 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_STAYS_ON_TOP",
2409 for( i = 0; i < i_items; i++ )
2411 if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_fullscreen )
2414 "Window manager supports _NET_WM_STATE_FULLSCREEN" );
2415 p_vout->p_sys->b_net_wm_state_fullscreen = VLC_TRUE;
2417 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_above )
2419 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_ABOVE" );
2420 p_vout->p_sys->b_net_wm_state_above = VLC_TRUE;
2422 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_below )
2424 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_BELOW" );
2425 p_vout->p_sys->b_net_wm_state_below = VLC_TRUE;
2427 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_stays_on_top )
2430 "Window manager supports _NET_WM_STATE_STAYS_ON_TOP" );
2431 p_vout->p_sys->b_net_wm_state_stays_on_top = VLC_TRUE;
2435 XFree( p_args.p_atom );
2438 /*****************************************************************************
2439 * Key events handling
2440 *****************************************************************************/
2446 } x11keys_to_vlckeys[] =
2448 { XK_F1, KEY_F1 }, { XK_F2, KEY_F2 }, { XK_F3, KEY_F3 }, { XK_F4, KEY_F4 },
2449 { XK_F5, KEY_F5 }, { XK_F6, KEY_F6 }, { XK_F7, KEY_F7 }, { XK_F8, KEY_F8 },
2450 { XK_F9, KEY_F9 }, { XK_F10, KEY_F10 }, { XK_F11, KEY_F11 },
2451 { XK_F12, KEY_F12 },
2453 { XK_Return, KEY_ENTER },
2454 { XK_KP_Enter, KEY_ENTER },
2455 { XK_space, KEY_SPACE },
2456 { XK_Escape, KEY_ESC },
2458 { XK_Menu, KEY_MENU },
2459 { XK_Left, KEY_LEFT },
2460 { XK_Right, KEY_RIGHT },
2462 { XK_Down, KEY_DOWN },
2464 { XK_Home, KEY_HOME },
2465 { XK_End, KEY_END },
2466 { XK_Page_Up, KEY_PAGEUP },
2467 { XK_Page_Down, KEY_PAGEDOWN },
2469 { XK_Insert, KEY_INSERT },
2470 { XK_Delete, KEY_DELETE },
2475 static int ConvertKey( int i_key )
2479 for( i = 0; x11keys_to_vlckeys[i].i_x11key != 0; i++ )
2481 if( x11keys_to_vlckeys[i].i_x11key == i_key )
2483 return x11keys_to_vlckeys[i].i_vlckey;
2490 /*****************************************************************************
2491 * WindowOnTop: Switches the "always on top" state of the video window.
2492 *****************************************************************************/
2493 static int WindowOnTop( vout_thread_t *p_vout, vlc_bool_t b_on_top )
2495 if( p_vout->p_sys->b_net_wm_state_stays_on_top )
2497 XClientMessageEvent event;
2499 memset( &event, 0, sizeof( XClientMessageEvent ) );
2501 event.type = ClientMessage;
2502 event.message_type = p_vout->p_sys->net_wm_state;
2503 event.display = p_vout->p_sys->p_display;
2504 event.window = p_vout->p_sys->p_win->base_window;
2506 event.data.l[ 0 ] = b_on_top; /* set property */
2507 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_stays_on_top;
2509 XSendEvent( p_vout->p_sys->p_display,
2510 DefaultRootWindow( p_vout->p_sys->p_display ),
2511 False, SubstructureRedirectMask,