]> git.sesse.net Git - vlc/blob - plugins/mga/xmga.c
89ef9946e947165dc8e99222ca63d02f59ce6140
[vlc] / plugins / mga / xmga.c
1 /*****************************************************************************
2  * xmga.c : X11 MGA plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 1998-2001 VideoLAN
5  * $Id: xmga.c,v 1.13 2002/05/06 21:05:26 gbazin Exp $
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@netcourrier.com>
10  *      
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <errno.h>                                                 /* ENOMEM */
30 #include <stdlib.h>                                                /* free() */
31 #include <string.h>                                            /* strerror() */
32
33 #include <videolan/vlc.h>
34
35 #ifdef HAVE_MACHINE_PARAM_H
36 /* BSD */
37 #include <machine/param.h>
38 #include <sys/types.h>                                     /* typedef ushort */
39 #include <sys/ipc.h>
40 #endif
41
42 #ifndef WIN32
43 #include <netinet/in.h>                               /* BSD: struct in_addr */
44 #endif
45
46 #include <sys/shm.h>                                   /* shmget(), shmctl() */
47 #include <X11/Xlib.h>
48 #include <X11/Xutil.h>
49 #include <X11/keysym.h>
50 #include <X11/extensions/XShm.h>
51 #include <X11/extensions/dpms.h>
52
53 #include "video.h"
54 #include "video_output.h"
55
56 #include "interface.h"
57 #include "netutils.h"                                 /* network_ChannelJoin */
58
59 #include "stream_control.h"                 /* needed by input_ext-intf.h... */
60 #include "input_ext-intf.h"
61
62 //#include "mga.h"
63
64 /*****************************************************************************
65  * Local prototypes
66  *****************************************************************************/
67 static void vout_getfunctions( function_list_t * );
68
69 static int  vout_Create    ( vout_thread_t * );
70 static void vout_Destroy   ( vout_thread_t * );
71 static void vout_Render    ( vout_thread_t *, picture_t * );
72 static void vout_Display   ( vout_thread_t *, picture_t * );
73 static int  vout_Manage    ( vout_thread_t * );
74 static int  vout_Init      ( vout_thread_t * );
75 static void vout_End       ( vout_thread_t * );
76
77 static int  CreateWindow   ( vout_thread_t * );
78 static void DestroyWindow  ( vout_thread_t * );
79
80 static int  NewPicture     ( vout_thread_t *, picture_t * );
81 static void FreePicture    ( vout_thread_t *, picture_t * );
82
83 static void ToggleFullScreen      ( vout_thread_t * );
84
85 static void EnableXScreenSaver    ( vout_thread_t * );
86 static void DisableXScreenSaver   ( vout_thread_t * );
87
88 static void CreateCursor   ( vout_thread_t * );
89 static void DestroyCursor  ( vout_thread_t * );
90 static void ToggleCursor   ( vout_thread_t * );
91
92 /*****************************************************************************
93  * Building configuration tree
94  *****************************************************************************/
95
96 #define ALT_FS_TEXT N_("alternate fullscreen method")
97 #define ALT_FS_LONGTEXT N_( \
98     "There are two ways to make a fullscreen window, unfortunately each one " \
99     "has its drawbacks.\n" \
100     "1) Let the window manager handle your fullscreen window (default). But " \
101     "things like taskbars will likely show on top of the video.\n" \
102     "2) Completly bypass the window manager, but then nothing will be able " \
103     "to show on top of the video.")
104
105 #define DISPLAY_TEXT N_("X11 display name")
106 #define DISPLAY_LONGTEXT N_( \
107     "Specify the X11 hardware display you want to use.\nBy default vlc will " \
108     "use the value of the DISPLAY environment variable.")
109
110 MODULE_CONFIG_START
111 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
112 ADD_STRING  ( "xmga_display", NULL, NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT )
113 ADD_BOOL    ( "xmga_altfullscreen", NULL, ALT_FS_TEXT, ALT_FS_LONGTEXT )
114 MODULE_CONFIG_STOP
115
116 MODULE_INIT_START
117     SET_DESCRIPTION( _("X11 MGA module") )
118     ADD_CAPABILITY( VOUT, 60 )
119     ADD_SHORTCUT( "xmga" )
120 MODULE_INIT_STOP
121
122 MODULE_ACTIVATE_START
123     vout_getfunctions( &p_module->p_functions->vout );
124 MODULE_ACTIVATE_STOP
125
126 MODULE_DEACTIVATE_START
127 MODULE_DEACTIVATE_STOP
128
129 /*****************************************************************************
130  * vout_sys_t: video output method descriptor
131  *****************************************************************************
132  * This structure is part of the video output thread descriptor.
133  * It describes the X11 and XVideo specific properties of an output thread.
134  *****************************************************************************/
135 typedef struct vout_sys_s
136 {
137     /* Internal settings and properties */
138     Display *           p_display;                        /* display pointer */
139
140     Visual *            p_visual;                          /* visual pointer */
141     int                 i_screen;                           /* screen number */
142     Window              window;                               /* root window */
143     GC                  gc;              /* graphic context instance handler */
144
145     boolean_t           b_shm;               /* shared memory extension flag */
146
147 #ifdef MODULE_NAME_IS_xvideo
148     Window              yuv_window;   /* sub-window for displaying yuv video
149                                                                         data */
150     int                 i_xvport;
151 #else
152     Colormap            colormap;               /* colormap used (8bpp only) */
153
154     int                 i_screen_depth;
155     int                 i_bytes_per_pixel;
156     int                 i_bytes_per_line;
157     int                 i_red_mask;
158     int                 i_green_mask;
159     int                 i_blue_mask;
160 #endif
161
162     /* X11 generic properties */
163     Atom                wm_protocols;
164     Atom                wm_delete_window;
165
166     int                 i_width;                     /* width of main window */
167     int                 i_height;                   /* height of main window */
168     boolean_t           b_altfullscreen;          /* which fullscreen method */
169
170     /* Backup of window position and size before fullscreen switch */
171     int                 i_width_backup;
172     int                 i_height_backup;
173     int                 i_xpos_backup;
174     int                 i_ypos_backup;
175     int                 i_width_backup_2;
176     int                 i_height_backup_2;
177     int                 i_xpos_backup_2;
178     int                 i_ypos_backup_2;
179
180     /* Screen saver properties */
181     int                 i_ss_timeout;                             /* timeout */
182     int                 i_ss_interval;           /* interval between changes */
183     int                 i_ss_blanking;                      /* blanking mode */
184     int                 i_ss_exposure;                      /* exposure mode */
185     BOOL                b_ss_dpms;                              /* DPMS mode */
186
187     /* Mouse pointer properties */
188     boolean_t           b_mouse_pointer_visible;
189     mtime_t             i_time_mouse_last_moved; /* used to auto-hide pointer*/
190     Cursor              blank_cursor;                   /* the hidden cursor */
191     mtime_t             i_time_button_last_pressed;   /* to track dbl-clicks */
192     Pixmap              cursor_pixmap;
193
194 } vout_sys_t;
195
196 /*****************************************************************************
197  * picture_sys_t: direct buffer method descriptor
198  *****************************************************************************
199  * This structure is part of the picture descriptor, it describes the
200  * XVideo specific properties of a direct buffer.
201  *****************************************************************************/
202 typedef struct picture_sys_s
203 {
204 } picture_sys_t;
205
206 /*****************************************************************************
207  * mwmhints_t: window manager hints
208  *****************************************************************************
209  * Fullscreen needs to be able to hide the wm decorations so we provide
210  * this structure to make it easier.
211  *****************************************************************************/
212 #define MWM_HINTS_DECORATIONS   (1L << 1)
213 #define PROP_MWM_HINTS_ELEMENTS 5
214 typedef struct mwmhints_s
215 {
216     u32 flags;
217     u32 functions;
218     u32 decorations;
219     s32 input_mode;
220     u32 status;
221 } mwmhints_t;
222
223 /*****************************************************************************
224  * Chroma defines
225  *****************************************************************************/
226 #ifdef MODULE_NAME_IS_xvideo
227 #   define MAX_DIRECTBUFFERS 5
228 #else
229 #   define MAX_DIRECTBUFFERS 2
230 #endif
231
232 /*****************************************************************************
233  * Seeking function TODO: put this in a generic location !
234  *****************************************************************************/
235 static __inline__ void vout_Seek( off_t i_seek )
236 {
237     off_t i_tell;
238
239     vlc_mutex_lock( &p_input_bank->lock );
240     if( p_input_bank->pp_input[0] != NULL )
241     {
242 #define S p_input_bank->pp_input[0]->stream
243         i_tell = S.p_selected_area->i_tell + i_seek * (off_t)50 * S.i_mux_rate;
244
245         i_tell = ( i_tell <= 0 /*S.p_selected_area->i_start*/ )
246                    ? 0 /*S.p_selected_area->i_start*/
247                    : ( i_tell >= S.p_selected_area->i_size )
248                        ? S.p_selected_area->i_size
249                        : i_tell;
250
251         input_Seek( p_input_bank->pp_input[0], i_tell );
252 #undef S
253     }
254     vlc_mutex_unlock( &p_input_bank->lock );
255 }
256
257 /*****************************************************************************
258  * Functions exported as capabilities. They are declared as static so that
259  * we don't pollute the namespace too much.
260  *****************************************************************************/
261 static void vout_getfunctions( function_list_t * p_function_list )
262 {
263     p_function_list->functions.vout.pf_create     = vout_Create;
264     p_function_list->functions.vout.pf_init       = vout_Init;
265     p_function_list->functions.vout.pf_end        = vout_End;
266     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
267     p_function_list->functions.vout.pf_manage     = vout_Manage;
268     p_function_list->functions.vout.pf_render     = vout_Render;
269     p_function_list->functions.vout.pf_display    = vout_Display;
270 }
271
272 /*****************************************************************************
273  * vout_Create: allocate X11 video thread output method
274  *****************************************************************************
275  * This function allocate and initialize a X11 vout method. It uses some of the
276  * vout properties to choose the window size, and change them according to the
277  * actual properties of the display.
278  *****************************************************************************/
279 static int vout_Create( vout_thread_t *p_vout )
280 {
281     char *psz_display;
282
283     /* Allocate structure */
284     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
285     if( p_vout->p_sys == NULL )
286     {
287         intf_ErrMsg( "vout error: %s", strerror(ENOMEM) );
288         return( 1 );
289     }
290
291     /* Open display, unsing the "display" config variable or the DISPLAY
292      * environment variable */
293     psz_display = config_GetPszVariable( "xmga_display" );
294     p_vout->p_sys->p_display = XOpenDisplay( psz_display );
295
296     if( p_vout->p_sys->p_display == NULL )                          /* error */
297     {
298         intf_ErrMsg( "vout error: cannot open display %s",
299                      XDisplayName( psz_display ) );
300         free( p_vout->p_sys );
301         if( psz_display ) free( psz_display );
302         return( 1 );
303     }
304     if( psz_display ) free( psz_display );
305
306     p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
307
308     /* Create blank cursor (for mouse cursor autohiding) */
309     p_vout->p_sys->b_mouse_pointer_visible = 1;
310     CreateCursor( p_vout );
311
312     /* Spawn base window - this window will include the video output window,
313      * but also command buttons, subtitles and other indicators */
314     if( CreateWindow( p_vout ) )
315     {
316         intf_ErrMsg( "vout error: cannot create X11 window" );
317         DestroyCursor( p_vout );
318         XCloseDisplay( p_vout->p_sys->p_display );
319         free( p_vout->p_sys );
320         return( 1 );
321     }
322
323     /* Disable screen saver */
324     DisableXScreenSaver( p_vout );
325
326     /* Misc init */
327     p_vout->p_sys->b_altfullscreen = 0;
328
329     return( 0 );
330 }
331
332 /*****************************************************************************
333  * vout_Destroy: destroy X11 video thread output method
334  *****************************************************************************
335  * Terminate an output method created by vout_CreateOutputMethod
336  *****************************************************************************/
337 static void vout_Destroy( vout_thread_t *p_vout )
338 {
339     /* Restore cursor if it was blanked */
340     if( !p_vout->p_sys->b_mouse_pointer_visible )
341     {
342         ToggleCursor( p_vout );
343     }
344
345     DestroyCursor( p_vout );
346     EnableXScreenSaver( p_vout );
347     DestroyWindow( p_vout );
348
349     XCloseDisplay( p_vout->p_sys->p_display );
350
351     /* Destroy structure */
352     free( p_vout->p_sys );
353 }
354
355 /*****************************************************************************
356  * vout_Init: initialize X11 video thread output method
357  *****************************************************************************
358  * This function create the XImages needed by the output thread. It is called
359  * at the beginning of the thread, but also each time the window is resized.
360  *****************************************************************************/
361 static int vout_Init( vout_thread_t *p_vout )
362 {
363     int i_index;
364     picture_t *p_pic;
365
366     I_OUTPUTPICTURES = 0;
367
368 #ifdef MODULE_NAME_IS_xvideo
369     /* Initialize the output structure; we already found an XVideo port,
370      * and the corresponding chroma we will be using. Since we can
371      * arbitrary scale, stick to the coordinates and aspect. */
372     p_vout->output.i_width  = p_vout->render.i_width;
373     p_vout->output.i_height = p_vout->render.i_height;
374     p_vout->output.i_aspect = p_vout->render.i_aspect;
375
376 #else
377     /* Initialize the output structure: RGB with square pixels, whatever
378      * the input format is, since it's the only format we know */
379     switch( p_vout->p_sys->i_screen_depth )
380     {
381         case 8: /* FIXME: set the palette */
382             p_vout->output.i_chroma = FOURCC_RGB2; break;
383         case 15:
384             p_vout->output.i_chroma = FOURCC_RV15; break;
385         case 16:
386             p_vout->output.i_chroma = FOURCC_RV16; break;
387         case 24:
388             p_vout->output.i_chroma = FOURCC_RV24; break;
389         case 32:
390             p_vout->output.i_chroma = FOURCC_RV32; break;
391         default:
392             intf_ErrMsg( "vout error: unknown screen depth" );
393             return( 0 );
394     }
395
396     p_vout->output.i_width = p_vout->p_sys->i_width;
397     p_vout->output.i_height = p_vout->p_sys->i_height;
398
399     /* Assume we have square pixels */
400     p_vout->output.i_aspect = p_vout->p_sys->i_width
401                                * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
402 #endif
403
404     /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
405     while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
406     {
407         p_pic = NULL;
408
409         /* Find an empty picture slot */
410         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
411         {
412             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
413             {
414                 p_pic = p_vout->p_picture + i_index;
415                 break;
416             }
417         }
418
419         /* Allocate the picture */
420         if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
421         {
422             break;
423         }
424
425         p_pic->i_status = DESTROYED_PICTURE;
426         p_pic->i_type   = DIRECT_PICTURE;
427
428         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
429
430         I_OUTPUTPICTURES++;
431     }
432
433     return( 0 );
434 }
435
436 /*****************************************************************************
437  * vout_Render: render previously calculated output
438  *****************************************************************************/
439 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
440 {
441     ;
442 }
443
444  /*****************************************************************************
445  * vout_Display: displays previously rendered output
446  *****************************************************************************
447  * This function sends the currently rendered image to X11 server.
448  * (The Xv extension takes care of "double-buffering".)
449  *****************************************************************************/
450 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
451 {
452     int i_width, i_height, i_x, i_y;
453
454     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
455                        &i_x, &i_y, &i_width, &i_height );
456 }
457
458 /*****************************************************************************
459  * vout_Manage: handle X11 events
460  *****************************************************************************
461  * This function should be called regularly by video output thread. It manages
462  * X11 events and allows window resizing. It returns a non null value on
463  * error.
464  *****************************************************************************/
465 static int vout_Manage( vout_thread_t *p_vout )
466 {
467     XEvent      xevent;                                         /* X11 event */
468     boolean_t   b_resized;                        /* window has been resized */
469     char        i_key;                                    /* ISO Latin-1 key */
470     KeySym      x_key_symbol;
471
472     /* Handle X11 events: ConfigureNotify events are parsed to know if the
473      * output window's size changed, MapNotify and UnmapNotify to know if the
474      * window is mapped (and if the display is useful), and ClientMessages
475      * to intercept window destruction requests */
476
477     b_resized = 0;
478     while( XCheckWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
479                               StructureNotifyMask | KeyPressMask |
480                               ButtonPressMask | ButtonReleaseMask | 
481                               PointerMotionMask | Button1MotionMask , &xevent )
482            == True )
483     {
484         /* ConfigureNotify event: prepare  */
485         if( (xevent.type == ConfigureNotify)
486           && ((xevent.xconfigure.width != p_vout->p_sys->i_width)
487              || (xevent.xconfigure.height != p_vout->p_sys->i_height)) )
488         {
489             /* Update dimensions */
490             b_resized = 1;
491             p_vout->i_changes |= VOUT_SIZE_CHANGE;
492             p_vout->p_sys->i_width = xevent.xconfigure.width;
493             p_vout->p_sys->i_height = xevent.xconfigure.height;
494         }
495         /* MapNotify event: change window status and disable screen saver */
496         else if( xevent.type == MapNotify)
497         {
498             if( (p_vout != NULL) && !p_vout->b_active )
499             {
500                 DisableXScreenSaver( p_vout );
501                 p_vout->b_active = 1;
502             }
503         }
504         /* UnmapNotify event: change window status and enable screen saver */
505         else if( xevent.type == UnmapNotify )
506         {
507             if( (p_vout != NULL) && p_vout->b_active )
508             {
509                 EnableXScreenSaver( p_vout );
510                 p_vout->b_active = 0;
511             }
512         }
513         /* Keyboard event */
514         else if( xevent.type == KeyPress )
515         {
516             /* We may have keys like F1 trough F12, ESC ... */
517             x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
518                                              xevent.xkey.keycode, 0 );
519             switch( x_key_symbol )
520             {
521                  case XK_Escape:
522                      p_main->p_intf->b_die = 1;
523                      break;
524                  case XK_Menu:
525                      p_main->p_intf->b_menu_change = 1;
526                      break;
527                  case XK_Left:
528                      vout_Seek( -5 );
529                      break;
530                  case XK_Right:
531                      vout_Seek( 5 );
532                      break;
533                  case XK_Up:
534                      vout_Seek( 60 );
535                      break;
536                  case XK_Down:
537                      vout_Seek( -60 );
538                      break;
539                  case XK_Home:
540                      input_Seek( p_input_bank->pp_input[0],
541                      p_input_bank->pp_input[0]->stream.p_selected_area->i_start );
542                      break;
543                  case XK_End:
544                      input_Seek( p_input_bank->pp_input[0],
545                      p_input_bank->pp_input[0]->stream.p_selected_area->i_size );
546                      break;
547                  case XK_Page_Up:
548                      vout_Seek( 900 );
549                      break;
550                  case XK_Page_Down:
551                      vout_Seek( -900 );
552                      break;
553                  case XK_space:
554                      input_SetStatus( p_input_bank->pp_input[0],
555                                       INPUT_STATUS_PAUSE );
556                      break;
557
558                  default:
559                      /* "Normal Keys"
560                       * The reason why I use this instead of XK_0 is that 
561                       * with XLookupString, we don't have to care about
562                       * keymaps. */
563
564                     if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
565                     {
566                         /* FIXME: handle stuff here */
567                         switch( i_key )
568                         {
569                         case 'q':
570                         case 'Q':
571                             p_main->p_intf->b_die = 1;
572                             break;
573                         case 'f':
574                         case 'F':
575                             p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
576                             break;
577
578                         case '0': network_ChannelJoin( 0 ); break;
579                         case '1': network_ChannelJoin( 1 ); break;
580                         case '2': network_ChannelJoin( 2 ); break;
581                         case '3': network_ChannelJoin( 3 ); break;
582                         case '4': network_ChannelJoin( 4 ); break;
583                         case '5': network_ChannelJoin( 5 ); break;
584                         case '6': network_ChannelJoin( 6 ); break;
585                         case '7': network_ChannelJoin( 7 ); break;
586                         case '8': network_ChannelJoin( 8 ); break;
587                         case '9': network_ChannelJoin( 9 ); break;
588
589                         default:
590                             break;
591                         }
592                     }
593                 break;
594             }
595         }
596         /* Mouse click */
597         else if( xevent.type == ButtonPress )
598         {
599             switch( ((XButtonEvent *)&xevent)->button )
600             {
601                 case Button1:
602                     /* In this part we will eventually manage
603                      * clicks for DVD navigation for instance. */
604
605                     /* detect double-clicks */
606                     if( ( ((XButtonEvent *)&xevent)->time -
607                           p_vout->p_sys->i_time_button_last_pressed ) < 300 )
608                     {
609                       p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
610                     }
611
612                     p_vout->p_sys->i_time_button_last_pressed =
613                         ((XButtonEvent *)&xevent)->time;
614                     break;
615
616                 case Button4:
617                     vout_Seek( 15 );
618                     break;
619
620                 case Button5:
621                     vout_Seek( -15 );
622                     break;
623             }
624         }
625         /* Mouse release */
626         else if( xevent.type == ButtonRelease )
627         {
628             switch( ((XButtonEvent *)&xevent)->button )
629             {
630                 case Button3:
631                     /* FIXME: need locking ! */
632                     p_main->p_intf->b_menu_change = 1;
633                     break;
634             }
635         }
636         /* Mouse move */
637         else if( xevent.type == MotionNotify )
638         {
639             p_vout->p_sys->i_time_mouse_last_moved = mdate();
640             if( ! p_vout->p_sys->b_mouse_pointer_visible )
641             {
642                 ToggleCursor( p_vout ); 
643             }
644         }
645         /* Other event */
646         else
647         {
648             intf_WarnMsg( 3, "vout: unhandled event %d received", xevent.type );
649         }
650     }
651
652     /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
653      * are handled - according to the man pages, the format is always 32
654      * in this case */
655     while( XCheckTypedEvent( p_vout->p_sys->p_display,
656                              ClientMessage, &xevent ) )
657     {
658         if( (xevent.xclient.message_type == p_vout->p_sys->wm_protocols)
659             && (xevent.xclient.data.l[0] == p_vout->p_sys->wm_delete_window ) )
660         {
661             p_main->p_intf->b_die = 1;
662         }
663     }
664
665     /*
666      * Fullscreen Change
667      */
668     if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
669     {
670         ToggleFullScreen( p_vout );
671         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
672
673     }
674
675     /*
676      * Size change
677      */
678     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
679     {
680         int i_width, i_height, i_x, i_y;
681
682         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
683
684         intf_WarnMsg( 3, "vout: video display resized (%dx%d)",
685                       p_vout->p_sys->i_width,
686                       p_vout->p_sys->i_height );
687  
688         vout_PlacePicture( p_vout, p_vout->p_sys->i_width,
689                            p_vout->p_sys->i_height,
690                            &i_x, &i_y, &i_width, &i_height );
691     }
692
693     /* Autohide Cursour */
694     if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
695     {
696         /* Hide the mouse automatically */
697         if( p_vout->p_sys->b_mouse_pointer_visible )
698         {
699             ToggleCursor( p_vout ); 
700         }
701     }
702
703     return 0;
704 }
705
706 /*****************************************************************************
707  * vout_End: terminate X11 video thread output method
708  *****************************************************************************
709  * Destroy the X11 XImages created by vout_Init. It is called at the end of
710  * the thread, but also each time the window is resized.
711  *****************************************************************************/
712 static void vout_End( vout_thread_t *p_vout )
713 {
714     int i_index;
715
716     /* Free the direct buffers we allocated */
717     for( i_index = I_OUTPUTPICTURES ; i_index ; )
718     {
719         i_index--;
720         FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
721     }
722 }
723
724 /* following functions are local */
725
726 /*****************************************************************************
727  * CreateWindow: open and set-up X11 main window
728  *****************************************************************************/
729 static int CreateWindow( vout_thread_t *p_vout )
730 {
731     XSizeHints              xsize_hints;
732     XSetWindowAttributes    xwindow_attributes;
733     XGCValues               xgcvalues;
734     XEvent                  xevent;
735
736     boolean_t               b_expose;
737     boolean_t               b_configure_notify;
738     boolean_t               b_map_notify;
739
740     /* Set main window's size */
741     p_vout->p_sys->i_width = p_vout->i_window_width;
742     p_vout->p_sys->i_height = p_vout->i_window_height;
743
744     /* Prepare window manager hints and properties */
745     xsize_hints.base_width          = p_vout->p_sys->i_width;
746     xsize_hints.base_height         = p_vout->p_sys->i_height;
747     xsize_hints.flags               = PSize;
748     p_vout->p_sys->wm_protocols     = XInternAtom( p_vout->p_sys->p_display,
749                                                    "WM_PROTOCOLS", True );
750     p_vout->p_sys->wm_delete_window = XInternAtom( p_vout->p_sys->p_display,
751                                                    "WM_DELETE_WINDOW", True );
752
753     /* Prepare window attributes */
754     xwindow_attributes.backing_store = Always;       /* save the hidden part */
755     xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
756                                                      p_vout->p_sys->i_screen);
757     xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
758     
759
760     /* Create the window and set hints - the window must receive
761      * ConfigureNotify events, and until it is displayed, Expose and
762      * MapNotify events. */
763
764     p_vout->p_sys->window =
765         XCreateWindow( p_vout->p_sys->p_display,
766                        DefaultRootWindow( p_vout->p_sys->p_display ),
767                        0, 0,
768                        p_vout->p_sys->i_width,
769                        p_vout->p_sys->i_height,
770                        0,
771                        0, InputOutput, 0,
772                        CWBackingStore | CWBackPixel | CWEventMask,
773                        &xwindow_attributes );
774
775     /* Set window manager hints and properties: size hints, command,
776      * window's name, and accepted protocols */
777     XSetWMNormalHints( p_vout->p_sys->p_display, p_vout->p_sys->window,
778                        &xsize_hints );
779     XSetCommand( p_vout->p_sys->p_display, p_vout->p_sys->window,
780                  p_main->ppsz_argv, p_main->i_argc );
781     XStoreName( p_vout->p_sys->p_display, p_vout->p_sys->window,
782                 VOUT_TITLE " (XMGA output)"
783               );
784
785     if( (p_vout->p_sys->wm_protocols == None)        /* use WM_DELETE_WINDOW */
786         || (p_vout->p_sys->wm_delete_window == None)
787         || !XSetWMProtocols( p_vout->p_sys->p_display, p_vout->p_sys->window,
788                              &p_vout->p_sys->wm_delete_window, 1 ) )
789     {
790         /* WM_DELETE_WINDOW is not supported by window manager */
791         intf_Msg( "vout error: missing or bad window manager" );
792     } 
793
794     /* Creation of a graphic context that doesn't generate a GraphicsExpose
795      * event when using functions like XCopyArea */
796     xgcvalues.graphics_exposures = False;
797     p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display,
798                                    p_vout->p_sys->window,
799                                    GCGraphicsExposures, &xgcvalues);
800
801     /* Send orders to server, and wait until window is displayed - three
802      * events must be received: a MapNotify event, an Expose event allowing
803      * drawing in the window, and a ConfigureNotify to get the window
804      * dimensions. Once those events have been received, only ConfigureNotify
805      * events need to be received. */
806     b_expose = 0;
807     b_configure_notify = 0;
808     b_map_notify = 0;
809     XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
810     do
811     {
812         XNextEvent( p_vout->p_sys->p_display, &xevent);
813         if( (xevent.type == Expose)
814             && (xevent.xexpose.window == p_vout->p_sys->window) )
815         {
816             b_expose = 1;
817         }
818         else if( (xevent.type == MapNotify)
819                  && (xevent.xmap.window == p_vout->p_sys->window) )
820         {
821             b_map_notify = 1;
822         }
823         else if( (xevent.type == ConfigureNotify)
824                  && (xevent.xconfigure.window == p_vout->p_sys->window) )
825         {
826             b_configure_notify = 1;
827             p_vout->p_sys->i_width = xevent.xconfigure.width;
828             p_vout->p_sys->i_height = xevent.xconfigure.height;
829         }
830     } while( !( b_expose && b_configure_notify && b_map_notify ) );
831
832     XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
833                   StructureNotifyMask | KeyPressMask |
834                   ButtonPressMask | ButtonReleaseMask | 
835                   PointerMotionMask );
836
837     /* If the cursor was formerly blank than blank it again */
838     if( !p_vout->p_sys->b_mouse_pointer_visible )
839     {
840         ToggleCursor( p_vout );
841         ToggleCursor( p_vout );
842     }
843
844     XSync( p_vout->p_sys->p_display, False );
845
846     /* At this stage, the window is open, displayed, and ready to
847      * receive data */
848
849     return( 0 );
850 }
851
852 /*****************************************************************************
853  * DestroyWindow: destroy the window
854  *****************************************************************************
855  *
856  *****************************************************************************/
857 static void DestroyWindow( vout_thread_t *p_vout )
858 {
859     XSync( p_vout->p_sys->p_display, False );
860
861     XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
862     XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
863     XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
864 }
865
866 /*****************************************************************************
867  * NewPicture: allocate a picture
868  *****************************************************************************
869  * Returns 0 on success, -1 otherwise
870  *****************************************************************************/
871 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
872 {
873     /* We know the chroma, allocate a buffer which will be used
874      * directly by the decoder */
875     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
876
877     if( p_pic->p_sys == NULL )
878     {
879         return -1;
880     }
881
882     /* XXX */
883
884     switch( p_vout->output.i_chroma )
885     {
886         /* XXX ?? */
887
888         default:
889             /* Unknown chroma, tell the guy to get lost */
890             free( p_pic->p_sys );
891             intf_ErrMsg( "vout error: never heard of chroma 0x%.8x (%4.4s)",
892                          p_vout->output.i_chroma,
893                          (char*)&p_vout->output.i_chroma );
894             p_pic->i_planes = 0;
895             return -1;
896     }
897
898     return 0;
899 }
900
901 /*****************************************************************************
902  * FreePicture: destroy a picture allocated with NewPicture
903  *****************************************************************************
904  * Destroy XImage AND associated data. If using Shm, detach shared memory
905  * segment from server and process, then free it. The XDestroyImage manpage
906  * says that both the image structure _and_ the data pointed to by the
907  * image structure are freed, so no need to free p_image->data.
908  *****************************************************************************/
909 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
910 {
911
912     XSync( p_vout->p_sys->p_display, False );
913
914     free( p_pic->p_sys );
915 }
916
917 /*****************************************************************************
918  * ToggleFullScreen: Enable or disable full screen mode
919  *****************************************************************************
920  * This function will switch between fullscreen and window mode.
921  *
922  *****************************************************************************/
923 static void ToggleFullScreen ( vout_thread_t *p_vout )
924 {
925     Atom prop;
926     mwmhints_t mwmhints;
927     int i_xpos, i_ypos, i_width, i_height;
928     XEvent xevent;
929     XSetWindowAttributes attributes;
930
931     p_vout->b_fullscreen = !p_vout->b_fullscreen;
932
933     if( p_vout->b_fullscreen )
934     {
935         Window next_parent, parent, *p_dummy, dummy1;
936         unsigned int dummy2, dummy3;
937
938         intf_WarnMsg( 3, "vout: entering fullscreen mode" );
939
940         /* Only check the fullscreen method when we actually go fullscreen,
941          * because to go back to window mode we need to know in which
942          * fullscreen mode we where */
943         p_vout->p_sys->b_altfullscreen =
944             config_GetIntVariable( "xmga_altfullscreen" );
945
946         /* Save current window coordinates so they can be restored when
947          * we exit from fullscreen mode. This is the tricky part because
948          * this heavily depends on the behaviour of the window manager.
949          * When you use XMoveWindow some window managers will adjust the top
950          * of the window to the coordinates you gave, but others will instead
951          * adjust the top of the client area to the coordinates
952          * (don't forget windows have decorations). */
953
954         /* First, get the position and size of the client area */
955         XGetGeometry( p_vout->p_sys->p_display,
956                       p_vout->p_sys->window,
957                       &dummy1,
958                       &dummy2,
959                       &dummy3,
960                       &p_vout->p_sys->i_width_backup_2,
961                       &p_vout->p_sys->i_height_backup_2,
962                       &dummy2, &dummy3 );
963         XTranslateCoordinates( p_vout->p_sys->p_display,
964                                p_vout->p_sys->window,
965                                DefaultRootWindow( p_vout->p_sys->p_display ),
966                                0,
967                                0,
968                                &p_vout->p_sys->i_xpos_backup_2,
969                                &p_vout->p_sys->i_ypos_backup_2,
970                                &dummy1 );
971
972         /* Then try to get the position and size of the whole window */
973
974         /* find the real parent of our window (created by the window manager),
975          * the one which is a direct child of the root window */
976         next_parent = parent = p_vout->p_sys->window;
977         while( next_parent != DefaultRootWindow( p_vout->p_sys->p_display ) )
978         {
979             parent = next_parent;
980             XQueryTree( p_vout->p_sys->p_display,
981                         parent,
982                         &dummy1,
983                         &next_parent,
984                         &p_dummy,
985                         &dummy2 );
986             XFree((void *)p_dummy);
987         }
988
989         XGetGeometry( p_vout->p_sys->p_display,
990                       p_vout->p_sys->window,
991                       &dummy1,
992                       &dummy2,
993                       &dummy3,
994                       &p_vout->p_sys->i_width_backup,
995                       &p_vout->p_sys->i_height_backup,
996                       &dummy2, &dummy3 );
997
998         XTranslateCoordinates( p_vout->p_sys->p_display,
999                                parent,
1000                                DefaultRootWindow( p_vout->p_sys->p_display ),
1001                                0,
1002                                0,
1003                                &p_vout->p_sys->i_xpos_backup,
1004                                &p_vout->p_sys->i_ypos_backup,
1005                                &dummy1 );
1006
1007         /* fullscreen window size and position */
1008         i_xpos = 0;
1009         i_ypos = 0;
1010         i_width = DisplayWidth( p_vout->p_sys->p_display,
1011                                 p_vout->p_sys->i_screen );
1012         i_height = DisplayHeight( p_vout->p_sys->p_display,
1013                                   p_vout->p_sys->i_screen );
1014
1015     }
1016     else
1017     {
1018         intf_WarnMsg( 3, "vout: leaving fullscreen mode" );
1019
1020         i_xpos = p_vout->p_sys->i_xpos_backup;
1021         i_ypos = p_vout->p_sys->i_ypos_backup;
1022         i_width = p_vout->p_sys->i_width_backup;
1023         i_height = p_vout->p_sys->i_height_backup;
1024     }
1025
1026     /* To my knowledge there are two ways to create a borderless window.
1027      * There's the generic way which is to tell x to bypass the window manager,
1028      * but this creates problems with the focus of other applications.
1029      * The other way is to use the motif property "_MOTIF_WM_HINTS" which
1030      * luckily seems to be supported by most window managers.
1031      */
1032     if( !p_vout->p_sys->b_altfullscreen )
1033     {
1034         mwmhints.flags = MWM_HINTS_DECORATIONS;
1035         mwmhints.decorations = !p_vout->b_fullscreen;
1036
1037         prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1038                             False );
1039         XChangeProperty( p_vout->p_sys->p_display, p_vout->p_sys->window,
1040                          prop, prop, 32, PropModeReplace,
1041                          (unsigned char *)&mwmhints,
1042                          PROP_MWM_HINTS_ELEMENTS );
1043     }
1044     else
1045     {
1046         /* brute force way to remove decorations */
1047         attributes.override_redirect = p_vout->b_fullscreen;
1048         XChangeWindowAttributes( p_vout->p_sys->p_display,
1049                                  p_vout->p_sys->window,
1050                                  CWOverrideRedirect,
1051                                  &attributes);
1052     }
1053
1054     /* We need to unmap and remap the window if we want the window 
1055      * manager to take our changes into effect */
1056     XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
1057
1058     XWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
1059                   StructureNotifyMask, &xevent );
1060     while( xevent.type != UnmapNotify )
1061         XWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
1062                       StructureNotifyMask, &xevent );
1063
1064     XMapRaised( p_vout->p_sys->p_display, p_vout->p_sys->window);
1065
1066     while( xevent.type != MapNotify )
1067         XWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
1068                       StructureNotifyMask, &xevent );
1069
1070     XMoveResizeWindow( p_vout->p_sys->p_display,
1071                        p_vout->p_sys->window,
1072                        i_xpos,
1073                        i_ypos,
1074                        i_width,
1075                        i_height );
1076
1077     /* Purge all ConfigureNotify events, this is needed to fix a bug where we
1078      * would lose the original size of the window */
1079     while( xevent.type != ConfigureNotify )
1080         XWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
1081                       StructureNotifyMask, &xevent );
1082     while( XCheckWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
1083                               StructureNotifyMask, &xevent ) );
1084
1085
1086     /* We need to check that the window was really restored where we wanted */
1087     if( !p_vout->b_fullscreen )
1088     {
1089         Window dummy1;
1090         unsigned int dummy2, dummy3, dummy4, dummy5;
1091
1092         /* Check the position */
1093         XTranslateCoordinates( p_vout->p_sys->p_display,
1094                                p_vout->p_sys->window,
1095                                DefaultRootWindow( p_vout->p_sys->p_display ),
1096                                0,
1097                                0,
1098                                &dummy2,
1099                                &dummy3,
1100                                &dummy1 );
1101         if( dummy2 != p_vout->p_sys->i_xpos_backup_2 ||
1102             dummy3 != p_vout->p_sys->i_ypos_backup_2 )
1103         {
1104             /* Ok it didn't work... second try */
1105
1106             XMoveWindow( p_vout->p_sys->p_display,
1107                          p_vout->p_sys->window,
1108                          p_vout->p_sys->i_xpos_backup_2,
1109                          p_vout->p_sys->i_ypos_backup_2 );
1110             
1111             /* Purge all ConfigureNotify events, this is needed to fix a bug
1112              * where we would lose the original size of the window */
1113             XWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
1114                           StructureNotifyMask, &xevent );
1115             while( xevent.type != ConfigureNotify )
1116                 XWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
1117                               StructureNotifyMask, &xevent );
1118             while( XCheckWindowEvent( p_vout->p_sys->p_display,
1119                                       p_vout->p_sys->window,
1120                                       StructureNotifyMask, &xevent ) );
1121         }
1122
1123         /* Check the size */
1124         XGetGeometry( p_vout->p_sys->p_display,
1125                       p_vout->p_sys->window,
1126                       &dummy1,
1127                       &dummy2,
1128                       &dummy3,
1129                       &dummy4,
1130                       &dummy5,
1131                       &dummy2, &dummy3 );
1132
1133         if( dummy4 != p_vout->p_sys->i_width_backup_2 ||
1134             dummy5 != p_vout->p_sys->i_height_backup_2 )
1135         {
1136             /* Ok it didn't work... third try */
1137
1138             XResizeWindow( p_vout->p_sys->p_display,
1139                          p_vout->p_sys->window,
1140                          p_vout->p_sys->i_width_backup_2,
1141                          p_vout->p_sys->i_height_backup_2 );
1142             
1143             /* Purge all ConfigureNotify events, this is needed to fix a bug
1144              * where we would lose the original size of the window */
1145             XWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
1146                           StructureNotifyMask, &xevent );
1147             while( xevent.type != ConfigureNotify )
1148                 XWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
1149                               StructureNotifyMask, &xevent );
1150             while( XCheckWindowEvent( p_vout->p_sys->p_display,
1151                                       p_vout->p_sys->window,
1152                                       StructureNotifyMask, &xevent ) );
1153         }
1154     }
1155
1156     if( p_vout->p_sys->b_altfullscreen )
1157         XSetInputFocus(p_vout->p_sys->p_display,
1158                        p_vout->p_sys->window,
1159                        RevertToParent,
1160                        CurrentTime);
1161
1162     /* signal that the size needs to be updated */
1163     p_vout->p_sys->i_width = i_width;
1164     p_vout->p_sys->i_height = i_height;
1165     p_vout->i_changes |= VOUT_SIZE_CHANGE;
1166
1167 }
1168
1169 /*****************************************************************************
1170  * EnableXScreenSaver: enable screen saver
1171  *****************************************************************************
1172  * This function enables the screen saver on a display after it has been
1173  * disabled by XDisableScreenSaver.
1174  * FIXME: what happens if multiple vlc sessions are running at the same
1175  *        time ???
1176  *****************************************************************************/
1177 static void EnableXScreenSaver( vout_thread_t *p_vout )
1178 {
1179     int dummy;
1180
1181     XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1182                      p_vout->p_sys->i_ss_interval,
1183                      p_vout->p_sys->i_ss_blanking,
1184                      p_vout->p_sys->i_ss_exposure );
1185
1186     /* Restore DPMS settings */
1187     if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1188     {
1189         if( p_vout->p_sys->b_ss_dpms )
1190         {
1191             DPMSEnable( p_vout->p_sys->p_display );
1192         }
1193     }
1194 }
1195
1196 /*****************************************************************************
1197  * DisableXScreenSaver: disable screen saver
1198  *****************************************************************************
1199  * See XEnableXScreenSaver
1200  *****************************************************************************/
1201 static void DisableXScreenSaver( vout_thread_t *p_vout )
1202 {
1203     int dummy;
1204
1205     /* Save screen saver informations */
1206     XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1207                      &p_vout->p_sys->i_ss_interval,
1208                      &p_vout->p_sys->i_ss_blanking,
1209                      &p_vout->p_sys->i_ss_exposure );
1210
1211     /* Disable screen saver */
1212     XSetScreenSaver( p_vout->p_sys->p_display, 0,
1213                      p_vout->p_sys->i_ss_interval,
1214                      p_vout->p_sys->i_ss_blanking,
1215                      p_vout->p_sys->i_ss_exposure );
1216
1217     /* Disable DPMS */
1218     if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1219     {
1220         CARD16 dummy;
1221         /* Save DPMS current state */
1222         DPMSInfo( p_vout->p_sys->p_display, &dummy,
1223                   &p_vout->p_sys->b_ss_dpms );
1224         DPMSDisable( p_vout->p_sys->p_display );
1225    }
1226 }
1227
1228 /*****************************************************************************
1229  * CreateCursor: create a blank mouse pointer
1230  *****************************************************************************/
1231 static void CreateCursor( vout_thread_t *p_vout )
1232 {
1233     XColor cursor_color;
1234
1235     p_vout->p_sys->cursor_pixmap =
1236         XCreatePixmap( p_vout->p_sys->p_display,
1237                        DefaultRootWindow( p_vout->p_sys->p_display ),
1238                        1, 1, 1 );
1239
1240     XParseColor( p_vout->p_sys->p_display,
1241                  XCreateColormap( p_vout->p_sys->p_display,
1242                                   DefaultRootWindow(
1243                                                     p_vout->p_sys->p_display ),
1244                                   DefaultVisual(
1245                                                 p_vout->p_sys->p_display,
1246                                                 p_vout->p_sys->i_screen ),
1247                                   AllocNone ),
1248                  "black", &cursor_color );
1249
1250     p_vout->p_sys->blank_cursor =
1251         XCreatePixmapCursor( p_vout->p_sys->p_display,
1252                              p_vout->p_sys->cursor_pixmap,
1253                              p_vout->p_sys->cursor_pixmap,
1254                              &cursor_color, &cursor_color, 1, 1 );
1255 }
1256
1257 /*****************************************************************************
1258  * DestroyCursor: destroy the blank mouse pointer
1259  *****************************************************************************/
1260 static void DestroyCursor( vout_thread_t *p_vout )
1261 {
1262     XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
1263 }
1264
1265 /*****************************************************************************
1266  * ToggleCursor: hide or show the mouse pointer
1267  *****************************************************************************
1268  * This function hides the X pointer if it is visible by setting the pointer
1269  * sprite to a blank one. To show it again, we disable the sprite.
1270  *****************************************************************************/
1271 static void ToggleCursor( vout_thread_t *p_vout )
1272 {
1273     if( p_vout->p_sys->b_mouse_pointer_visible )
1274     {
1275         XDefineCursor( p_vout->p_sys->p_display,
1276                        p_vout->p_sys->window,
1277                        p_vout->p_sys->blank_cursor );
1278         p_vout->p_sys->b_mouse_pointer_visible = 0;
1279     }
1280     else
1281     {
1282         XUndefineCursor( p_vout->p_sys->p_display, p_vout->p_sys->window );
1283         p_vout->p_sys->b_mouse_pointer_visible = 1;
1284     }
1285 }
1286