]> git.sesse.net Git - vlc/blob - plugins/x11/vout_x11.c
*Changed the level arg in intf_WarnMsg so that it is more logical: the
[vlc] / plugins / x11 / vout_x11.c
1 /*****************************************************************************
2  * vout_x11.c: X11 video output display method
3  *****************************************************************************
4  * Copyright (C) 1998, 1999, 2000 VideoLAN
5  * $Id: vout_x11.c,v 1.24 2001/05/07 03:14:09 stef Exp $
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          David Kennedy <dkennedy@tinytoad.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 #define MODULE_NAME x11
27 #include "modules_inner.h"
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #include "defs.h"
33
34 #include <errno.h>                                                 /* ENOMEM */
35 #include <stdlib.h>                                                /* free() */
36 #include <string.h>                                            /* strerror() */
37
38 #ifdef HAVE_MACHINE_PARAM_H
39 /* BSD */
40 #include <machine/param.h>
41 #include <sys/types.h>                                     /* typedef ushort */
42 #include <sys/ipc.h>
43 #endif
44
45 #include <sys/shm.h>                                   /* shmget(), shmctl() */
46 #include <X11/Xlib.h>
47 #include <X11/Xutil.h>
48 #include <X11/keysym.h>
49 #include <X11/extensions/XShm.h>
50
51 #include "config.h"
52 #include "common.h"
53 #include "threads.h"
54 #include "mtime.h"
55 #include "tests.h"
56 #include "modules.h"
57
58 #include "video.h"
59 #include "video_output.h"
60
61 #include "interface.h"
62 #include "intf_msg.h"
63
64 #include "netutils.h"                                 /* network_ChannelJoin */
65
66 #include "main.h"
67
68 /*****************************************************************************
69  * vout_sys_t: video output X11 method descriptor
70  *****************************************************************************
71  * This structure is part of the video output thread descriptor.
72  * It describes the X11 specific properties of an output thread. X11 video
73  * output is performed through regular resizable windows. Windows can be
74  * dynamically resized to adapt to the size of the streams.
75  *****************************************************************************/
76 typedef struct vout_sys_s
77 {
78     /* User settings */
79     boolean_t           b_shm;               /* shared memory extension flag */
80
81     /* Internal settings and properties */
82     Display *           p_display;                        /* display pointer */
83     Visual *            p_visual;                          /* visual pointer */
84     int                 i_screen;                           /* screen number */
85     Window              window;                               /* root window */
86     GC                  gc;              /* graphic context instance handler */
87     Colormap            colormap;               /* colormap used (8bpp only) */
88
89     /* Display buffers and shared memory information */
90     XImage *            p_ximage[2];                       /* XImage pointer */
91     XShmSegmentInfo     shm_info[2];       /* shared memory zone information */
92
93     /* X11 generic properties */
94     Atom                wm_protocols;
95     Atom                wm_delete_window;
96
97     int                 i_width;                     /* width of main window */
98     int                 i_height;                   /* height of main window */
99
100     /* Screen saver properties */
101     int                 i_ss_timeout;                             /* timeout */
102     int                 i_ss_interval;           /* interval between changes */
103     int                 i_ss_blanking;                      /* blanking mode */
104     int                 i_ss_exposure;                      /* exposure mode */
105
106     /* Auto-hide cursor */
107     mtime_t     i_lastmoved;
108     
109     /* Mouse pointer properties */
110     boolean_t           b_mouse;         /* is the mouse pointer displayed ? */
111
112 } vout_sys_t;
113
114 /* Fullscreen needs to be able to hide the wm decorations */
115 #define MWM_HINTS_DECORATIONS   (1L << 1)
116 #define PROP_MWM_HINTS_ELEMENTS 5
117 typedef struct mwmhints_s
118 {
119     u32 flags;
120     u32 functions;
121     u32 decorations;
122     s32 input_mode;
123     u32 status;
124 } mwmhints_t;
125
126 /*****************************************************************************
127  * Local prototypes
128  *****************************************************************************/
129 static int  vout_Probe     ( probedata_t *p_data );
130 static int  vout_Create    ( struct vout_thread_s * );
131 static int  vout_Init      ( struct vout_thread_s * );
132 static void vout_End       ( struct vout_thread_s * );
133 static void vout_Destroy   ( struct vout_thread_s * );
134 static int  vout_Manage    ( struct vout_thread_s * );
135 static void vout_Display   ( struct vout_thread_s * );
136 static void vout_SetPalette( struct vout_thread_s *, u16*, u16*, u16*, u16* );
137
138 static int  X11CreateWindow     ( vout_thread_t *p_vout );
139 static int  X11InitDisplay      ( vout_thread_t *p_vout, char *psz_display );
140
141 static int  X11CreateImage      ( vout_thread_t *p_vout, XImage **pp_ximage );
142 static void X11DestroyImage     ( XImage *p_ximage );
143 static int  X11CreateShmImage   ( vout_thread_t *p_vout, XImage **pp_ximage,
144                                   XShmSegmentInfo *p_shm_info );
145 static void X11DestroyShmImage  ( vout_thread_t *p_vout, XImage *p_ximage,
146                                   XShmSegmentInfo *p_shm_info );
147
148 static void X11TogglePointer            ( vout_thread_t *p_vout );
149 static void X11EnableScreenSaver        ( vout_thread_t *p_vout );
150 static void X11DisableScreenSaver       ( vout_thread_t *p_vout );
151
152 /*****************************************************************************
153  * Functions exported as capabilities. They are declared as static so that
154  * we don't pollute the namespace too much.
155  *****************************************************************************/
156 void _M( vout_getfunctions )( function_list_t * p_function_list )
157 {
158     p_function_list->pf_probe = vout_Probe;
159     p_function_list->functions.vout.pf_create     = vout_Create;
160     p_function_list->functions.vout.pf_init       = vout_Init;
161     p_function_list->functions.vout.pf_end        = vout_End;
162     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
163     p_function_list->functions.vout.pf_manage     = vout_Manage;
164     p_function_list->functions.vout.pf_display    = vout_Display;
165     p_function_list->functions.vout.pf_setpalette = vout_SetPalette;
166 }
167
168 /*****************************************************************************
169  * vout_Probe: probe the video driver and return a score
170  *****************************************************************************
171  * This function tries to initialize SDL and returns a score to the
172  * plugin manager so that it can select the best plugin.
173  *****************************************************************************/
174 static int vout_Probe( probedata_t *p_data )
175 {
176     if( TestMethod( VOUT_METHOD_VAR, "x11" ) )
177     {
178         return( 999 );
179     }
180
181     return( 50 );
182 }
183
184 /*****************************************************************************
185  * vout_Create: allocate X11 video thread output method
186  *****************************************************************************
187  * This function allocate and initialize a X11 vout method. It uses some of the
188  * vout properties to choose the window size, and change them according to the
189  * actual properties of the display.
190  *****************************************************************************/
191 static int vout_Create( vout_thread_t *p_vout )
192 {
193     char *psz_display;
194
195     /* Allocate structure */
196     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
197     if( p_vout->p_sys == NULL )
198     {
199         intf_ErrMsg( "vout error: %s", strerror(ENOMEM) );
200         return( 1 );
201     }
202
203     /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
204     psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
205     p_vout->p_sys->p_display = XOpenDisplay( psz_display );
206
207     if( p_vout->p_sys->p_display == NULL )                          /* error */
208     {
209         intf_ErrMsg( "vout error: cannot open display %s", psz_display );
210         free( p_vout->p_sys );
211         return( 1 );
212     }
213     p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
214
215     p_vout->b_fullscreen
216         = main_GetIntVariable( VOUT_FULLSCREEN_VAR, VOUT_FULLSCREEN_DEFAULT );
217
218     /* Spawn base window - this window will include the video output window,
219      * but also command buttons, subtitles and other indicators */
220
221     if( X11CreateWindow( p_vout ) )
222     {
223         intf_ErrMsg( "vout error: cannot create X11 window" );
224         XCloseDisplay( p_vout->p_sys->p_display );
225         free( p_vout->p_sys );
226         return( 1 );
227     }
228
229     /* Open and initialize device. This function issues its own error messages.
230      * Since XLib is usually not thread-safe, we can't use the same display
231      * pointer than the interface or another thread. However, the root window
232      * id is still valid. */
233     if( X11InitDisplay( p_vout, psz_display ) )
234     {
235         intf_ErrMsg( "vout error: cannot initialize X11 display" );
236         XCloseDisplay( p_vout->p_sys->p_display );
237         free( p_vout->p_sys );
238         return( 1 );
239     }
240
241     p_vout->p_sys->b_mouse = 1;
242
243     /* Disable screen saver and return */
244     X11DisableScreenSaver( p_vout );
245
246     return( 0 );
247 }
248
249 /*****************************************************************************
250  * vout_Init: initialize X11 video thread output method
251  *****************************************************************************
252  * This function create the XImages needed by the output thread. It is called
253  * at the beginning of the thread, but also each time the window is resized.
254  *****************************************************************************/
255 static int vout_Init( vout_thread_t *p_vout )
256 {
257     int i_err;
258
259 #ifdef SYS_DARWIN1_3
260     /* FIXME : As of 2001-03-16, XFree4 for MacOS X does not support Xshm. */
261     p_vout->p_sys->b_shm = 0;
262 #endif
263
264     /* Create XImages using XShm extension - on failure, fall back to regular
265      * way (and destroy the first image if it was created successfully) */
266     if( p_vout->p_sys->b_shm )
267     {
268         /* Create first image */
269         i_err = X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[0],
270                                    &p_vout->p_sys->shm_info[0] );
271         if( !i_err )                         /* first image has been created */
272         {
273             /* Create second image */
274             if( X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[1],
275                                    &p_vout->p_sys->shm_info[1] ) )
276             {                             /* error creating the second image */
277                 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
278                                     &p_vout->p_sys->shm_info[0] );
279                 i_err = 1;
280             }
281         }
282         if( i_err )                                      /* an error occured */
283         {
284             intf_Msg( "vout: XShm video extension unavailable" );
285             p_vout->p_sys->b_shm = 0;
286         }
287     }
288
289     /* Create XImages without XShm extension */
290     if( !p_vout->p_sys->b_shm )
291     {
292         if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[0] ) )
293         {
294             intf_ErrMsg( "vout error: cannot create images" );
295             p_vout->p_sys->p_ximage[0] = NULL;
296             p_vout->p_sys->p_ximage[1] = NULL;
297             return( 1 );
298         }
299         if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[1] ) )
300         {
301             intf_ErrMsg( "vout error: cannot create images" );
302             X11DestroyImage( p_vout->p_sys->p_ximage[0] );
303             p_vout->p_sys->p_ximage[0] = NULL;
304             p_vout->p_sys->p_ximage[1] = NULL;
305             return( 1 );
306         }
307     }
308
309     /* Set bytes per line and initialize buffers */
310     p_vout->i_bytes_per_line = p_vout->p_sys->p_ximage[0]->bytes_per_line;
311     vout_SetBuffers( p_vout, p_vout->p_sys->p_ximage[ 0 ]->data,
312                      p_vout->p_sys->p_ximage[ 1 ]->data );
313
314     /* Set date for autohiding cursor */
315     p_vout->p_sys->i_lastmoved = mdate();
316     
317     return( 0 );
318 }
319
320 /*****************************************************************************
321  * vout_End: terminate X11 video thread output method
322  *****************************************************************************
323  * Destroy the X11 XImages created by vout_Init. It is called at the end of
324  * the thread, but also each time the window is resized.
325  *****************************************************************************/
326 static void vout_End( vout_thread_t *p_vout )
327 {
328     if( p_vout->p_sys->b_shm )                             /* Shm XImages... */
329     {
330         X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
331                             &p_vout->p_sys->shm_info[0] );
332         X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[1],
333                             &p_vout->p_sys->shm_info[1] );
334     }
335     else                                          /* ...or regular XImages */
336     {
337         X11DestroyImage( p_vout->p_sys->p_ximage[0] );
338         X11DestroyImage( p_vout->p_sys->p_ximage[1] );
339     }
340 }
341
342 /*****************************************************************************
343  * vout_Destroy: destroy X11 video thread output method
344  *****************************************************************************
345  * Terminate an output method created by vout_CreateOutputMethod
346  *****************************************************************************/
347 static void vout_Destroy( vout_thread_t *p_vout )
348 {
349     /* Enable screen saver */
350     X11EnableScreenSaver( p_vout );
351
352     /* Destroy colormap */
353     if( p_vout->i_screen_depth == 8 )
354     {
355         XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
356     }
357
358     /* Destroy window */
359     XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
360     XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
361     XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
362
363     XCloseDisplay( p_vout->p_sys->p_display );
364
365     /* Destroy structure */
366     free( p_vout->p_sys );
367 }
368
369 /*****************************************************************************
370  * vout_Manage: handle X11 events
371  *****************************************************************************
372  * This function should be called regularly by video output thread. It manages
373  * X11 events and allows window resizing. It returns a non null value on
374  * error.
375  *****************************************************************************/
376 static int vout_Manage( vout_thread_t *p_vout )
377 {
378     XEvent      xevent;                                         /* X11 event */
379     boolean_t   b_resized;                        /* window has been resized */
380     char        i_key;                                    /* ISO Latin-1 key */
381     KeySym      x_key_symbol;
382
383     /* Handle X11 events: ConfigureNotify events are parsed to know if the
384      * output window's size changed, MapNotify and UnmapNotify to know if the
385      * window is mapped (and if the display is useful), and ClientMessages
386      * to intercept window destruction requests */
387
388     b_resized = 0;
389     while( XCheckWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
390                               StructureNotifyMask | KeyPressMask |
391                               ButtonPressMask | ButtonReleaseMask | 
392                               PointerMotionMask | Button1MotionMask , &xevent )
393            == True )
394     {
395         /* ConfigureNotify event: prepare  */
396         if( (xevent.type == ConfigureNotify)
397             && ((xevent.xconfigure.width != p_vout->p_sys->i_width)
398                 || (xevent.xconfigure.height != p_vout->p_sys->i_height)) )
399         {
400             /* Update dimensions */
401             b_resized = 1;
402             p_vout->p_sys->i_width = xevent.xconfigure.width;
403             p_vout->p_sys->i_height = xevent.xconfigure.height;
404         }
405         /* MapNotify event: change window status and disable screen saver */
406         else if( xevent.type == MapNotify)
407         {
408             if( (p_vout != NULL) && !p_vout->b_active )
409             {
410                 X11DisableScreenSaver( p_vout );
411                 p_vout->b_active = 1;
412             }
413         }
414         /* UnmapNotify event: change window status and enable screen saver */
415         else if( xevent.type == UnmapNotify )
416         {
417             if( (p_vout != NULL) && p_vout->b_active )
418             {
419                 X11EnableScreenSaver( p_vout );
420                 p_vout->b_active = 0;
421             }
422         }
423         /* Keyboard event */
424         else if( xevent.type == KeyPress )
425         {
426             /* We may have keys like F1 trough F12, ESC ... */
427             x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
428                                              xevent.xkey.keycode, 0 );
429             switch( x_key_symbol )
430             {
431                  case XK_Escape:
432                      p_main->p_intf->b_die = 1;
433                      break;
434                  case XK_Menu:
435                      p_main->p_intf->b_menu_change = 1;
436                      break;
437                  default:
438                      /* "Normal Keys"
439                       * The reason why I use this instead of XK_0 is that 
440                       * with XLookupString, we don't have to care about
441                       * keymaps. */
442
443                     if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
444                     {
445                         /* FIXME: handle stuff here */
446                         switch( i_key )
447                         {
448                         case 'q':
449                         case 'Q':
450                             p_main->p_intf->b_die = 1;
451                             break;
452                         case 'f':
453                         case 'F':
454                             p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
455                             break;
456                         case '0':
457                             network_ChannelJoin( 0 );
458                             break;
459                         case '1':
460                             network_ChannelJoin( 1 );
461                             break;
462                         case '2':
463                             network_ChannelJoin( 2 );
464                             break;
465                         case '3':
466                             network_ChannelJoin( 3 );
467                             break;
468                         case '4':
469                             network_ChannelJoin( 4 );
470                             break;
471                         case '5':
472                             network_ChannelJoin( 5 );
473                             break;
474                         case '6':
475                             network_ChannelJoin( 6 );
476                             break;
477                         case '7':
478                             network_ChannelJoin( 7 );
479                             break;
480                         case '8':
481                             network_ChannelJoin( 8 );
482                             break;
483                         case '9':
484                             network_ChannelJoin( 9 );
485                             break;
486                         default:
487                             if( intf_ProcessKey( p_main->p_intf, 
488                                                  (char )i_key ) )
489                             {
490                                intf_DbgMsg( "vout: unhandled key '%c' (%i)", 
491                                             (char)i_key, i_key );
492                             }
493                             break;
494                         }
495                     }
496                 break;
497             }
498         }
499         /* Mouse click */
500         else if( xevent.type == ButtonPress )
501         {
502             switch( ((XButtonEvent *)&xevent)->button )
503             {
504                 case Button1:
505                     /* in this part we will eventually manage
506                      * clicks for DVD navigation for instance */
507                     break;
508             }
509         }
510         /* Mouse release */
511         else if( xevent.type == ButtonRelease )
512         {
513             switch( ((XButtonEvent *)&xevent)->button )
514             {
515                 case Button3:
516                     /* FIXME: need locking ! */
517                     p_main->p_intf->b_menu_change = 1;
518                     break;
519             }
520         }
521         /* Mouse move */
522         else if( xevent.type == MotionNotify )
523         {
524             p_vout->p_sys->i_lastmoved = mdate();
525             if( ! p_vout->p_sys->b_mouse )
526             {
527                 X11TogglePointer( p_vout ); 
528             }
529         }
530         /* Other event */
531         else
532         {
533             intf_WarnMsg( 3, "vout: unhandled event %d received", xevent.type );
534         }
535     }
536
537     /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
538      * are handled - according to the man pages, the format is always 32
539      * in this case */
540     while( XCheckTypedEvent( p_vout->p_sys->p_display,
541                              ClientMessage, &xevent ) )
542     {
543         if( (xevent.xclient.message_type == p_vout->p_sys->wm_protocols)
544             && (xevent.xclient.data.l[0] == p_vout->p_sys->wm_delete_window ) )
545         {
546             p_main->p_intf->b_die = 1;
547         }
548         else
549         {
550             intf_DbgMsg( "vout: unhandled ClientMessage received" );
551         }
552     }
553
554     if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
555     {
556         char *psz_display;
557         /* Open display, unsing 'vlc_display' or the DISPLAY
558          * environment variable */
559         psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
560
561         intf_DbgMsg( "vout: changing full-screen status" );
562
563         p_vout->b_fullscreen = !p_vout->b_fullscreen;
564         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
565
566         /* Get rid of the old window */
567         XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
568         XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
569
570         /* And create a new one */
571         if( X11CreateWindow( p_vout ) )
572         {
573             intf_ErrMsg( "vout error: cannot create X11 window" );
574             XCloseDisplay( p_vout->p_sys->p_display );
575
576             free( p_vout->p_sys );
577             return( 1 );
578         }
579
580         if( X11InitDisplay( p_vout, psz_display ) )
581         {
582             intf_ErrMsg( "vout error: cannot initialize X11 display" );
583             XCloseDisplay( p_vout->p_sys->p_display );
584             free( p_vout->p_sys );
585             return( 1 );
586         }
587         /* We've changed the size, update it */
588         p_vout->i_changes |= VOUT_SIZE_CHANGE;
589     }
590
591     /*
592      * Handle vout window resizing
593      */
594     if( b_resized )
595     {
596         /* If interface window has been resized, change vout size */
597         intf_DbgMsg( "vout: resizing output window" );
598         p_vout->i_width =  p_vout->p_sys->i_width;
599         p_vout->i_height = p_vout->p_sys->i_height;
600         p_vout->i_changes |= VOUT_SIZE_CHANGE;
601     }
602     else if( (p_vout->i_width  != p_vout->p_sys->i_width) ||
603              (p_vout->i_height != p_vout->p_sys->i_height) )
604     {
605         /* If video output size has changed, change interface window size */
606         intf_DbgMsg( "vout: resizing output window" );
607         p_vout->p_sys->i_width =    p_vout->i_width;
608         p_vout->p_sys->i_height =   p_vout->i_height;
609         XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
610                        p_vout->p_sys->i_width, p_vout->p_sys->i_height );
611     }
612     /*
613      * Color/Grayscale or gamma change: in 8bpp, just change the colormap
614      */
615     if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE)
616         && (p_vout->i_screen_depth == 8) )
617     {
618         /* FIXME: clear flags ?? */
619     }
620
621     /*
622      * Size change
623      */
624     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
625     {
626         intf_DbgMsg( "vout info: resizing window" );
627         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
628
629         /* Resize window */
630         XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
631                        p_vout->i_width, p_vout->i_height );
632
633         /* Destroy XImages to change their size */
634         vout_End( p_vout );
635
636         /* Recreate XImages. If SysInit failed, the thread can't go on. */
637         if( vout_Init( p_vout ) )
638         {
639             intf_ErrMsg( "vout error: cannot resize display" );
640             return( 1 );
641        }
642
643         /* Tell the video output thread that it will need to rebuild YUV
644          * tables. This is needed since conversion buffer size may have
645          * changed */
646         p_vout->i_changes |= VOUT_YUV_CHANGE;
647         intf_Msg( "vout: video display resized (%dx%d)",
648                   p_vout->i_width, p_vout->i_height);
649     }
650
651     /* Autohide Cursour */
652     if( mdate() - p_vout->p_sys->i_lastmoved > 2000000 )
653     {
654         /* Hide the mouse automatically */
655         if( p_vout->p_sys->b_mouse )
656         {
657             X11TogglePointer( p_vout ); 
658         }
659     }
660
661     
662     return 0;
663 }
664
665 /*****************************************************************************
666  * vout_Display: displays previously rendered output
667  *****************************************************************************
668  * This function send the currently rendered image to X11 server, wait until
669  * it is displayed and switch the two rendering buffer, preparing next frame.
670  *****************************************************************************/
671 static void vout_Display( vout_thread_t *p_vout )
672 {
673     if( p_vout->p_sys->b_shm)                                /* XShm is used */
674     {
675         /* Display rendered image using shared memory extension */
676         XShmPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
677                      p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
678                      0, 0, 0, 0,
679                      p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
680                      p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height, True);
681
682         /* Send the order to the X server */
683         XSync(p_vout->p_sys->p_display, False);
684     }
685     else                                /* regular X11 capabilities are used */
686     {
687         XPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
688                   p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
689                   0, 0, 0, 0,
690                   p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
691                   p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height);
692
693         /* Send the order to the X server */
694         XSync(p_vout->p_sys->p_display, False);
695     }
696 }
697
698 /*****************************************************************************
699  * vout_SetPalette: sets an 8 bpp palette
700  *****************************************************************************
701  * This function sets the palette given as an argument. It does not return
702  * anything, but could later send information on which colors it was unable
703  * to set.
704  *****************************************************************************/
705 static void vout_SetPalette( p_vout_thread_t p_vout,
706                              u16 *red, u16 *green, u16 *blue, u16 *transp )
707 {
708     int i, j;
709     XColor p_colors[255];
710
711     intf_DbgMsg( "vout: Palette change called" );
712
713     /* allocate palette */
714     for( i = 0, j = 255; i < 255; i++, j-- )
715     {
716         /* kludge: colors are indexed reversely because color 255 seems
717          * to be reserved for black even if we try to set it to white */
718         p_colors[ i ].pixel = j;
719         p_colors[ i ].pad   = 0;
720         p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
721         p_colors[ i ].red   = red[ j ];
722         p_colors[ i ].blue  = blue[ j ];
723         p_colors[ i ].green = green[ j ];
724     }
725
726     XStoreColors( p_vout->p_sys->p_display,
727                   p_vout->p_sys->colormap, p_colors, 256 );
728 }
729
730 /* following functions are local */
731
732 /*****************************************************************************
733  * X11CreateWindow: open and set-up X11 main window
734  *****************************************************************************/
735 static int X11CreateWindow( vout_thread_t *p_vout )
736 {
737     XSizeHints              xsize_hints;
738     XSetWindowAttributes    xwindow_attributes;
739     XGCValues               xgcvalues;
740     XEvent                  xevent;
741     Atom                    prop;
742     mwmhints_t              mwmhints;
743
744     boolean_t               b_expose;
745     boolean_t               b_configure_notify;
746     boolean_t               b_map_notify;
747
748     /* If we're full screen, we're full screen! */
749     if( p_vout->b_fullscreen ) 
750     {
751         p_vout->p_sys->i_width = DisplayWidth( p_vout->p_sys->p_display, 
752                                                p_vout->p_sys->i_screen );
753         p_vout->p_sys->i_height =  DisplayHeight( p_vout->p_sys->p_display, 
754                                                   p_vout->p_sys->i_screen ); 
755         p_vout->i_width =  p_vout->p_sys->i_width;
756         p_vout->i_height = p_vout->p_sys->i_height;
757     }
758     else
759     {
760         /* Set main window's size */
761         p_vout->p_sys->i_width =  main_GetIntVariable( VOUT_WIDTH_VAR,
762                                                        VOUT_WIDTH_DEFAULT );
763         p_vout->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
764                                                        VOUT_HEIGHT_DEFAULT );
765         p_vout->i_width =  p_vout->p_sys->i_width;
766         p_vout->i_height = p_vout->p_sys->i_height;
767     }
768
769     /* Prepare window manager hints and properties */
770     xsize_hints.base_width          = p_vout->p_sys->i_width;
771     xsize_hints.base_height         = p_vout->p_sys->i_height;
772     xsize_hints.flags               = PSize;
773     p_vout->p_sys->wm_protocols     = XInternAtom( p_vout->p_sys->p_display,
774                                                    "WM_PROTOCOLS", True );
775     p_vout->p_sys->wm_delete_window = XInternAtom( p_vout->p_sys->p_display,
776                                                    "WM_DELETE_WINDOW", True );
777
778     /* Prepare window attributes */
779     xwindow_attributes.backing_store = Always;       /* save the hidden part */
780     xwindow_attributes.background_pixel = BlackPixel( p_vout->p_sys->p_display,
781                                                       p_vout->p_sys->i_screen );
782     xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
783     
784
785     /* Create the window and set hints - the window must receive ConfigureNotify
786      * events, and, until it is displayed, Expose and MapNotify events. */
787
788     p_vout->p_sys->window =
789         XCreateWindow( p_vout->p_sys->p_display,
790                        DefaultRootWindow( p_vout->p_sys->p_display ),
791                        0, 0,
792                        p_vout->p_sys->i_width, p_vout->p_sys->i_height, 0,
793                        0, InputOutput, 0,
794                        CWBackingStore | CWBackPixel | CWEventMask,
795                        &xwindow_attributes );
796
797     if ( p_vout->b_fullscreen )
798     {
799         prop = XInternAtom(p_vout->p_sys->p_display, "_MOTIF_WM_HINTS", False);
800         mwmhints.flags = MWM_HINTS_DECORATIONS;
801         mwmhints.decorations = 0;
802         XChangeProperty( p_vout->p_sys->p_display, p_vout->p_sys->window,
803                          prop, prop, 32, PropModeReplace,
804                          (unsigned char *)&mwmhints, PROP_MWM_HINTS_ELEMENTS );
805
806         XSetTransientForHint( p_vout->p_sys->p_display,
807                               p_vout->p_sys->window, None );
808         XRaiseWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
809     }
810
811     /* Set window manager hints and properties: size hints, command,
812      * window's name, and accepted protocols */
813     XSetWMNormalHints( p_vout->p_sys->p_display, p_vout->p_sys->window,
814                        &xsize_hints );
815     XSetCommand( p_vout->p_sys->p_display, p_vout->p_sys->window,
816                  p_main->ppsz_argv, p_main->i_argc );
817     XStoreName( p_vout->p_sys->p_display, p_vout->p_sys->window,
818                 VOUT_TITLE " (X11 output)" );
819
820     if( (p_vout->p_sys->wm_protocols == None)        /* use WM_DELETE_WINDOW */
821         || (p_vout->p_sys->wm_delete_window == None)
822         || !XSetWMProtocols( p_vout->p_sys->p_display, p_vout->p_sys->window,
823                              &p_vout->p_sys->wm_delete_window, 1 ) )
824     {
825         /* WM_DELETE_WINDOW is not supported by window manager */
826         intf_Msg( "vout error: missing or bad window manager" );
827     }
828
829     /* Creation of a graphic context that doesn't generate a GraphicsExpose
830      * event when using functions like XCopyArea */
831     xgcvalues.graphics_exposures = False;
832     p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display,
833                                    p_vout->p_sys->window,
834                                    GCGraphicsExposures, &xgcvalues);
835
836     /* Send orders to server, and wait until window is displayed - three
837      * events must be received: a MapNotify event, an Expose event allowing
838      * drawing in the window, and a ConfigureNotify to get the window
839      * dimensions. Once those events have been received, only ConfigureNotify
840      * events need to be received. */
841     b_expose = 0;
842     b_configure_notify = 0;
843     b_map_notify = 0;
844     XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
845     do
846     {
847         XNextEvent( p_vout->p_sys->p_display, &xevent);
848         if( (xevent.type == Expose)
849             && (xevent.xexpose.window == p_vout->p_sys->window) )
850         {
851             b_expose = 1;
852         }
853         else if( (xevent.type == MapNotify)
854                  && (xevent.xmap.window == p_vout->p_sys->window) )
855         {
856             b_map_notify = 1;
857         }
858         else if( (xevent.type == ConfigureNotify)
859                  && (xevent.xconfigure.window == p_vout->p_sys->window) )
860         {
861             b_configure_notify = 1;
862             p_vout->p_sys->i_width = xevent.xconfigure.width;
863             p_vout->p_sys->i_height = xevent.xconfigure.height;
864         }
865     } while( !( b_expose && b_configure_notify && b_map_notify ) );
866
867     XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
868                   StructureNotifyMask | KeyPressMask |
869                   ButtonPressMask | ButtonReleaseMask | 
870                   PointerMotionMask );
871
872     if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
873     {
874         /* Allocate a new palette */
875         p_vout->p_sys->colormap =
876             XCreateColormap( p_vout->p_sys->p_display,
877                              DefaultRootWindow( p_vout->p_sys->p_display ),
878                              DefaultVisual( p_vout->p_sys->p_display,
879                                             p_vout->p_sys->i_screen ),
880                              AllocAll );
881
882         xwindow_attributes.colormap = p_vout->p_sys->colormap;
883         XChangeWindowAttributes( p_vout->p_sys->p_display,
884                                  p_vout->p_sys->window,
885                                  CWColormap, &xwindow_attributes );
886     }
887
888     if( p_vout->b_fullscreen )
889     {
890         XSetInputFocus( p_vout->p_sys->p_display, p_vout->p_sys->window,
891                         RevertToNone, CurrentTime );
892         XMoveWindow( p_vout->p_sys->p_display, p_vout->p_sys->window, 0, 0 );
893     }
894
895     /* At this stage, the window is open, displayed, and ready to
896      * receive data */
897
898     return( 0 );
899 }
900
901 /*****************************************************************************
902  * X11InitDisplay: open and initialize X11 device
903  *****************************************************************************
904  * Create a window according to video output given size, and set other
905  * properties according to the display properties.
906  *****************************************************************************/
907 static int X11InitDisplay( vout_thread_t *p_vout, char *psz_display )
908 {
909     XPixmapFormatValues *       p_formats;                 /* pixmap formats */
910     XVisualInfo *               p_xvisual;           /* visuals informations */
911     XVisualInfo                 xvisual_template;         /* visual template */
912     int                         i_count;                       /* array size */
913
914
915
916     /* Initialize structure */
917     p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
918     p_vout->p_sys->b_shm    = ( XShmQueryExtension( p_vout->p_sys->p_display )
919                                  == True );
920     if( !p_vout->p_sys->b_shm )
921     {
922         intf_Msg( "vout: XShm video extension is not available" );
923     }
924
925     /* Get screen depth */
926     p_vout->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
927                                             p_vout->p_sys->i_screen );
928     switch( p_vout->i_screen_depth )
929     {
930     case 8:
931         /*
932          * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
933          */
934         xvisual_template.screen =   p_vout->p_sys->i_screen;
935         xvisual_template.class =    DirectColor;
936         p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
937                                     VisualScreenMask | VisualClassMask,
938                                     &xvisual_template, &i_count );
939         if( p_xvisual == NULL )
940         {
941             intf_ErrMsg( "vout error: no PseudoColor visual available" );
942             return( 1 );
943         }
944         p_vout->i_bytes_per_pixel = 1;
945         break;
946     case 15:
947     case 16:
948     case 24:
949     default:
950         /*
951          * Screen depth is higher than 8bpp. TrueColor visual is used.
952          */
953         xvisual_template.screen =   p_vout->p_sys->i_screen;
954         xvisual_template.class =    TrueColor;
955         p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
956                                     VisualScreenMask | VisualClassMask,
957                                     &xvisual_template, &i_count );
958         if( p_xvisual == NULL )
959         {
960             intf_ErrMsg( "vout error: no TrueColor visual available" );
961             return( 1 );
962         }
963         p_vout->i_red_mask =        p_xvisual->red_mask;
964         p_vout->i_green_mask =      p_xvisual->green_mask;
965         p_vout->i_blue_mask =       p_xvisual->blue_mask;
966
967         /* There is no difference yet between 3 and 4 Bpp. The only way
968          * to find the actual number of bytes per pixel is to list supported
969          * pixmap formats. */
970         p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
971         p_vout->i_bytes_per_pixel = 0;
972
973         for( ; i_count-- ; p_formats++ )
974         {
975             /* Under XFree4.0, the list contains pixmap formats available
976              * through all video depths ; so we have to check against current
977              * depth. */
978             if( p_formats->depth == p_vout->i_screen_depth )
979             {
980                 if( p_formats->bits_per_pixel / 8
981                         > p_vout->i_bytes_per_pixel )
982                 {
983                     p_vout->i_bytes_per_pixel = p_formats->bits_per_pixel / 8;
984                 }
985             }
986         }
987         break;
988     }
989     p_vout->p_sys->p_visual = p_xvisual->visual;
990     XFree( p_xvisual );
991
992     return( 0 );
993 }
994
995 /*****************************************************************************
996  * X11CreateImage: create an XImage
997  *****************************************************************************
998  * Create a simple XImage used as a buffer.
999  *****************************************************************************/
1000 static int X11CreateImage( vout_thread_t *p_vout, XImage **pp_ximage )
1001 {
1002     byte_t *    pb_data;                          /* image data storage zone */
1003     int         i_quantum;                     /* XImage quantum (see below) */
1004
1005     /* Allocate memory for image */
1006     p_vout->i_bytes_per_line = p_vout->i_width * p_vout->i_bytes_per_pixel;
1007     pb_data = (byte_t *) malloc( p_vout->i_bytes_per_line * p_vout->i_height );
1008     if( !pb_data )                                                  /* error */
1009     {
1010         intf_ErrMsg( "vout error: %s", strerror(ENOMEM));
1011         return( 1 );
1012     }
1013
1014     /* Optimize the quantum of a scanline regarding its size - the quantum is
1015        a diviser of the number of bits between the start of two scanlines. */
1016     if( !(( p_vout->i_bytes_per_line ) % 32) )
1017     {
1018         i_quantum = 32;
1019     }
1020     else
1021     {
1022         if( !(( p_vout->i_bytes_per_line ) % 16) )
1023         {
1024             i_quantum = 16;
1025         }
1026         else
1027         {
1028             i_quantum = 8;
1029         }
1030     }
1031
1032     /* Create XImage */
1033     *pp_ximage = XCreateImage( p_vout->p_sys->p_display,
1034                                p_vout->p_sys->p_visual, p_vout->i_screen_depth,
1035                                ZPixmap, 0, pb_data,
1036                                p_vout->i_width, p_vout->i_height, i_quantum, 0);
1037     if(! *pp_ximage )                                               /* error */
1038     {
1039         intf_ErrMsg( "vout error: XCreateImage() failed" );
1040         free( pb_data );
1041         return( 1 );
1042     }
1043
1044     return 0;
1045 }
1046
1047 /*****************************************************************************
1048  * X11CreateShmImage: create an XImage using shared memory extension
1049  *****************************************************************************
1050  * Prepare an XImage for DisplayX11ShmImage function.
1051  * The order of the operations respects the recommandations of the mit-shm
1052  * document by J.Corbet and K.Packard. Most of the parameters were copied from
1053  * there.
1054  *****************************************************************************/
1055 static int X11CreateShmImage( vout_thread_t *p_vout, XImage **pp_ximage,
1056                               XShmSegmentInfo *p_shm_info)
1057 {
1058     /* Create XImage */
1059     *pp_ximage =
1060         XShmCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
1061                          p_vout->i_screen_depth, ZPixmap, 0,
1062                          p_shm_info, p_vout->i_width, p_vout->i_height );
1063     if(! *pp_ximage )                                               /* error */
1064     {
1065         intf_ErrMsg( "vout error: XShmCreateImage() failed" );
1066         return( 1 );
1067     }
1068
1069     /* Allocate shared memory segment - 0777 set the access permission
1070      * rights (like umask), they are not yet supported by X servers */
1071     p_shm_info->shmid =
1072         shmget( IPC_PRIVATE, (*pp_ximage)->bytes_per_line
1073                                  * (*pp_ximage)->height, IPC_CREAT | 0777);
1074     if( p_shm_info->shmid < 0)                                      /* error */
1075     {
1076         intf_ErrMsg( "vout error: cannot allocate shared image data (%s)",
1077                     strerror(errno));
1078         XDestroyImage( *pp_ximage );
1079         return( 1 );
1080     }
1081
1082     /* Attach shared memory segment to process (read/write) */
1083     p_shm_info->shmaddr = (*pp_ximage)->data = shmat(p_shm_info->shmid, 0, 0);
1084     if(! p_shm_info->shmaddr )
1085     {                                                               /* error */
1086         intf_ErrMsg( "vout error: cannot attach shared memory (%s)",
1087                     strerror(errno));
1088         shmctl( p_shm_info->shmid, IPC_RMID, 0 );      /* free shared memory */
1089         XDestroyImage( *pp_ximage );
1090         return( 1 );
1091     }
1092
1093     /* Mark the shm segment to be removed when there will be no more
1094      * attachements, so it is automatic on process exit or after shmdt */
1095     shmctl( p_shm_info->shmid, IPC_RMID, 0 );
1096
1097     /* Attach shared memory segment to X server (read only) */
1098     p_shm_info->readOnly = True;
1099     if( XShmAttach( p_vout->p_sys->p_display, p_shm_info )
1100          == False )                                                 /* error */
1101     {
1102         intf_ErrMsg( "vout error: cannot attach shared memory to X11 server" );
1103         shmdt( p_shm_info->shmaddr );   /* detach shared memory from process
1104                                          * and automatic free */
1105         XDestroyImage( *pp_ximage );
1106         return( 1 );
1107     }
1108
1109     /* Send image to X server. This instruction is required, since having
1110      * built a Shm XImage and not using it causes an error on XCloseDisplay */
1111     XFlush( p_vout->p_sys->p_display );
1112     return( 0 );
1113 }
1114
1115 /*****************************************************************************
1116  * X11DestroyImage: destroy an XImage
1117  *****************************************************************************
1118  * Destroy XImage AND associated data. If pointer is NULL, the image won't be
1119  * destroyed (see vout_ManageOutputMethod())
1120  *****************************************************************************/
1121 static void X11DestroyImage( XImage *p_ximage )
1122 {
1123     if( p_ximage != NULL )
1124     {
1125         XDestroyImage( p_ximage );                     /* no free() required */
1126     }
1127 }
1128
1129 /*****************************************************************************
1130  * X11DestroyShmImage
1131  *****************************************************************************
1132  * Destroy XImage AND associated data. Detach shared memory segment from
1133  * server and process, then free it. If pointer is NULL, the image won't be
1134  * destroyed (see vout_ManageOutputMethod())
1135  *****************************************************************************/
1136 static void X11DestroyShmImage( vout_thread_t *p_vout, XImage *p_ximage,
1137                                 XShmSegmentInfo *p_shm_info )
1138 {
1139     /* If pointer is NULL, do nothing */
1140     if( p_ximage == NULL )
1141     {
1142         return;
1143     }
1144
1145     XShmDetach( p_vout->p_sys->p_display, p_shm_info );/* detach from server */
1146     XDestroyImage( p_ximage );
1147
1148     if( shmdt( p_shm_info->shmaddr ) )  /* detach shared memory from process */
1149     {                                   /* also automatic freeing...         */
1150         intf_ErrMsg( "vout error: cannot detach shared memory (%s)",
1151                      strerror(errno) );
1152     }
1153 }
1154
1155
1156 /* WAZAAAAAAAAAAA */
1157
1158 /*****************************************************************************
1159  * X11EnableScreenSaver: enable screen saver
1160  *****************************************************************************
1161  * This function enable the screen saver on a display after it had been
1162  * disabled by XDisableScreenSaver. Both functions use a counter mechanism to
1163  * know wether the screen saver can be activated or not: if n successive calls
1164  * are made to XDisableScreenSaver, n successive calls to XEnableScreenSaver
1165  * will be required before the screen saver could effectively be activated.
1166  *****************************************************************************/
1167 void X11EnableScreenSaver( vout_thread_t *p_vout )
1168 {
1169     intf_DbgMsg( "vout: enabling screen saver" );
1170     XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1171                      p_vout->p_sys->i_ss_interval,
1172                      p_vout->p_sys->i_ss_blanking,
1173                      p_vout->p_sys->i_ss_exposure );
1174 }
1175
1176 /*****************************************************************************
1177  * X11DisableScreenSaver: disable screen saver
1178  *****************************************************************************
1179  * See XEnableScreenSaver
1180  *****************************************************************************/
1181 void X11DisableScreenSaver( vout_thread_t *p_vout )
1182 {
1183     /* Save screen saver informations */
1184     XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1185                      &p_vout->p_sys->i_ss_interval,
1186                      &p_vout->p_sys->i_ss_blanking,
1187                      &p_vout->p_sys->i_ss_exposure );
1188
1189     /* Disable screen saver */
1190     intf_DbgMsg( "vout: disabling screen saver" );
1191     XSetScreenSaver( p_vout->p_sys->p_display, 0,
1192                      p_vout->p_sys->i_ss_interval,
1193                      p_vout->p_sys->i_ss_blanking,
1194                      p_vout->p_sys->i_ss_exposure );
1195 }
1196
1197 /*****************************************************************************
1198  * X11TogglePointer: hide or show the mouse pointer
1199  *****************************************************************************
1200  * This function hides the X pointer if it is visible by putting it at
1201  * coordinates (32,32) and setting the pointer sprite to a blank one. To
1202  * show it again, we disable the sprite and restore the original coordinates.
1203  *****************************************************************************/
1204 void X11TogglePointer( vout_thread_t *p_vout )
1205 {
1206     static Cursor cursor;
1207     static boolean_t b_cursor = 0;
1208
1209     if( p_vout->p_sys->b_mouse )
1210     {
1211         p_vout->p_sys->b_mouse = 0;
1212
1213         if( !b_cursor )
1214         {
1215             XColor color;
1216             Pixmap blank = XCreatePixmap( p_vout->p_sys->p_display,
1217                                DefaultRootWindow(p_vout->p_sys->p_display),
1218                                1, 1, 1 );
1219
1220             XParseColor( p_vout->p_sys->p_display,
1221                          XCreateColormap( p_vout->p_sys->p_display,
1222                                           DefaultRootWindow(
1223                                                   p_vout->p_sys->p_display ),
1224                                           DefaultVisual(
1225                                                   p_vout->p_sys->p_display,
1226                                                   p_vout->p_sys->i_screen ),
1227                                           AllocNone ),
1228                          "black", &color );
1229
1230             cursor = XCreatePixmapCursor( p_vout->p_sys->p_display,
1231                            blank, blank, &color, &color, 1, 1 );
1232
1233             b_cursor = 1;
1234         }
1235         XDefineCursor( p_vout->p_sys->p_display,
1236                        p_vout->p_sys->window, cursor );
1237     }
1238     else
1239     {
1240         p_vout->p_sys->b_mouse = 1;
1241
1242         XUndefineCursor( p_vout->p_sys->p_display, p_vout->p_sys->window );
1243     }
1244 }
1245