]> git.sesse.net Git - vlc/blob - modules/video_output/ggi.c
input options whitelisting, step 2 (refs #1371)
[vlc] / modules / video_output / ggi.c
1 /*****************************************************************************
2  * ggi.c : GGI plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <errno.h>                                                 /* ENOMEM */
29
30 #include <ggi/ggi.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc_interface.h>
34 #include <vlc_vout.h>
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static int  Create    ( vlc_object_t * );
40 static void Destroy   ( vlc_object_t * );
41
42 static int  Init      ( vout_thread_t * );
43 static void End       ( vout_thread_t * );
44 static int  Manage    ( vout_thread_t * );
45 static void Display   ( vout_thread_t *, picture_t * );
46
47 static int  OpenDisplay    ( vout_thread_t * );
48 static void CloseDisplay   ( vout_thread_t * );
49 static void SetPalette     ( vout_thread_t *, uint16_t *, uint16_t *, uint16_t * );
50
51 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54 #define DISPLAY_TEXT N_("X11 display")
55 #define DISPLAY_LONGTEXT N_( \
56             "X11 hardware display to use.\n" \
57             "By default, VLC will use the value of the DISPLAY " \
58             "environment variable.")
59
60 vlc_module_begin();
61     add_string( "ggi-display", NULL, NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT, VLC_TRUE );
62         change_safe();
63     set_description( "General Graphics Interface video output" );
64     set_capability( "video output", 30 );
65     set_callbacks( Create, Destroy );
66 vlc_module_end();
67
68 /*****************************************************************************
69  * vout_sys_t: video output GGI method descriptor
70  *****************************************************************************
71  * This structure is part of the video output thread descriptor.
72  * It describes the GGI specific properties of an output thread.
73  *****************************************************************************/
74 struct vout_sys_t
75 {
76     /* GGI system information */
77     ggi_visual_t        p_display;                         /* display device */
78
79     ggi_mode            mode;                             /* mode descriptor */
80     int                 i_bits_per_pixel;
81
82     /* Buffer information */
83     ggi_directbuffer *  pp_buffer[2];                             /* buffers */
84     int                 i_index;
85
86     vlc_bool_t          b_must_acquire;   /* must be acquired before writing */
87 };
88
89 /*****************************************************************************
90  * Create: allocate GGI video thread output method
91  *****************************************************************************
92  * This function allocate and initialize a GGI vout method. It uses some of the
93  * vout properties to choose the correct mode, and change them according to the
94  * mode actually used.
95  *****************************************************************************/
96 static int Create( vlc_object_t *p_this )
97 {
98     vout_thread_t *p_vout = (vout_thread_t *)p_this;
99
100     /* Allocate structure */
101     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
102     if( p_vout->p_sys == NULL )
103     {
104         msg_Err( p_vout, "out of memory" );
105         return( 1 );
106     }
107
108     /* Open and initialize device */
109     if( OpenDisplay( p_vout ) )
110     {
111         msg_Err( p_vout, "cannot initialize GGI display" );
112         free( p_vout->p_sys );
113         return( 1 );
114     }
115
116     p_vout->pf_init = Init;
117     p_vout->pf_end = End;
118     p_vout->pf_manage = Manage;
119     p_vout->pf_render = NULL;
120     p_vout->pf_display = Display;
121
122     return( 0 );
123 }
124
125 /*****************************************************************************
126  * Init: initialize GGI video thread output method
127  *****************************************************************************
128  * This function initialize the GGI display device.
129  *****************************************************************************/
130 static int Init( vout_thread_t *p_vout )
131 {
132 #define p_b p_vout->p_sys->pp_buffer
133     int i_index;
134     picture_t *p_pic;
135
136     I_OUTPUTPICTURES = 0;
137
138     p_vout->output.i_width  = p_vout->p_sys->mode.visible.x;
139     p_vout->output.i_height = p_vout->p_sys->mode.visible.y;
140     p_vout->output.i_aspect = p_vout->p_sys->mode.visible.x
141                                * VOUT_ASPECT_FACTOR
142                                / p_vout->p_sys->mode.visible.y;
143
144     switch( p_vout->p_sys->i_bits_per_pixel )
145     {
146         case 8:
147             p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
148             p_vout->output.pf_setpalette = SetPalette;
149             break;
150         case 15:
151             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
152         case 16:
153             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
154         case 24:
155             p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
156         case 32:
157             p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
158         default:
159             msg_Err( p_vout, "unknown screen depth %i",
160                      p_vout->p_sys->i_bits_per_pixel );
161             return 0;
162     }
163
164     /* Only useful for bits_per_pixel != 8 */
165     p_vout->output.i_rmask = p_b[ 0 ]->buffer.plb.pixelformat->red_mask;
166     p_vout->output.i_gmask = p_b[ 0 ]->buffer.plb.pixelformat->green_mask;
167     p_vout->output.i_bmask = p_b[ 0 ]->buffer.plb.pixelformat->blue_mask;
168
169     p_pic = NULL;
170
171     /* Find an empty picture slot */
172     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
173     {
174         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
175         {
176             p_pic = p_vout->p_picture + i_index;
177             break;
178         }
179     }
180
181     if( p_pic == NULL )
182     {
183         return 0;
184     }
185
186     /* We know the chroma, allocate a buffer which will be used
187      * directly by the decoder */
188     p_vout->p_sys->i_index = 0;
189     p_pic->p->p_pixels = p_b[ 0 ]->write;
190     p_pic->p->i_pixel_pitch = p_b[ 0 ]->buffer.plb.pixelformat->size / 8;
191     p_pic->p->i_lines = p_vout->p_sys->mode.visible.y;
192     p_pic->p->i_visible_lines = p_vout->p_sys->mode.visible.y;
193
194     p_pic->p->i_pitch = p_b[ 0 ]->buffer.plb.stride;
195
196     if( p_b[ 0 ]->buffer.plb.pixelformat->size / 8
197          * p_vout->p_sys->mode.visible.x
198         != p_b[ 0 ]->buffer.plb.stride )
199     {
200         p_pic->p->i_visible_pitch = p_b[ 0 ]->buffer.plb.pixelformat->size
201                                      / 8 * p_vout->p_sys->mode.visible.x;
202     }
203     else
204     {
205         p_pic->p->i_visible_pitch = p_b[ 0 ]->buffer.plb.stride;
206     }
207
208     p_pic->i_planes = 1;
209
210     p_pic->i_status = DESTROYED_PICTURE;
211     p_pic->i_type   = DIRECT_PICTURE;
212
213     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
214
215     I_OUTPUTPICTURES++;
216
217     /* Acquire first buffer */
218     if( p_vout->p_sys->b_must_acquire )
219     {
220         ggiResourceAcquire( p_b[ 0 ]->resource,
221                             GGI_ACTYPE_WRITE );
222     }
223
224     /* Listen to the keyboard and the mouse buttons */
225     ggiSetEventMask( p_vout->p_sys->p_display,
226                      emKeyboard | emPtrButtonPress | emPtrButtonRelease );
227
228     /* Set asynchronous display mode -- usually quite faster */
229     ggiAddFlags( p_vout->p_sys->p_display, GGIFLAG_ASYNC );
230
231     return( 0 );
232 #undef p_b
233 }
234
235 /*****************************************************************************
236  * End: terminate GGI video thread output method
237  *****************************************************************************
238  * Terminate an output method created by Create
239  *****************************************************************************/
240 static void End( vout_thread_t *p_vout )
241 {
242 #define p_b p_vout->p_sys->pp_buffer
243     /* Release buffer */
244     if( p_vout->p_sys->b_must_acquire )
245     {
246         ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
247     }
248 #undef p_b
249 }
250
251 /*****************************************************************************
252  * Destroy: destroy GGI video thread output method
253  *****************************************************************************
254  * Terminate an output method created by Create
255  *****************************************************************************/
256 static void Destroy( vlc_object_t *p_this )
257 {
258     vout_thread_t *p_vout = (vout_thread_t *)p_this;
259
260     CloseDisplay( p_vout );
261
262     free( p_vout->p_sys );
263 }
264
265 /*****************************************************************************
266  * Manage: handle GGI events
267  *****************************************************************************
268  * This function should be called regularly by video output thread. It returns
269  * a non null value if an error occurred.
270  *****************************************************************************/
271 static int Manage( vout_thread_t *p_vout )
272 {
273     struct timeval tv = { 0, 1000 };                        /* 1 millisecond */
274     gii_event_mask mask;
275     gii_event      event;
276     vlc_value_t    val;
277
278     mask = emKeyboard | emPtrButtonPress | emPtrButtonRelease;
279
280     ggiEventPoll( p_vout->p_sys->p_display, mask, &tv );
281
282     while( ggiEventsQueued( p_vout->p_sys->p_display, mask) )
283     {
284         ggiEventRead( p_vout->p_sys->p_display, &event, mask);
285
286         switch( event.any.type )
287         {
288             case evKeyRelease:
289
290                 switch( event.key.sym )
291                 {
292                     case 'q':
293                     case 'Q':
294                     case GIIUC_Escape:
295                         vlc_object_kill( p_vout->p_libvlc );
296                         break;
297
298                     default:
299                         break;
300                 }
301                 break;
302
303             case evPtrButtonRelease:
304
305                 switch( event.pbutton.button )
306                 {
307                     case GII_PBUTTON_LEFT:
308                         val.b_bool = VLC_TRUE;
309                         var_Set( p_vout, "mouse-clicked", val );
310                         break;
311
312                     case GII_PBUTTON_RIGHT:
313                         {
314                             intf_thread_t *p_intf;
315                             p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
316                                                               FIND_ANYWHERE );
317                             if( p_intf )
318                             {
319                                 p_intf->b_menu_change = 1;
320                                 vlc_object_release( p_intf );
321                             }
322                         }
323                         break;
324                 }
325                 break;
326
327             default:
328                 break;
329         }
330     }
331
332     return( 0 );
333 }
334
335 /*****************************************************************************
336  * Display: displays previously rendered output
337  *****************************************************************************/
338 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
339 {
340 #define p_b p_vout->p_sys->pp_buffer
341     p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
342
343     /* Change display frame */
344     if( p_vout->p_sys->b_must_acquire )
345     {
346         ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
347     }
348     ggiSetDisplayFrame( p_vout->p_sys->p_display,
349                         p_b[ p_vout->p_sys->i_index ]->frame );
350
351     /* Swap buffers and change write frame */
352     p_vout->p_sys->i_index ^= 1;
353     p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
354
355     if( p_vout->p_sys->b_must_acquire )
356     {
357         ggiResourceAcquire( p_b[ p_vout->p_sys->i_index ]->resource,
358                             GGI_ACTYPE_WRITE );
359     }
360     ggiSetWriteFrame( p_vout->p_sys->p_display,
361                       p_b[ p_vout->p_sys->i_index ]->frame );
362
363     /* Flush the output so that it actually displays */
364     ggiFlush( p_vout->p_sys->p_display );
365 #undef p_b
366 }
367
368 /* following functions are local */
369
370 /*****************************************************************************
371  * OpenDisplay: open and initialize GGI device
372  *****************************************************************************
373  * Open and initialize display according to preferences specified in the vout
374  * thread fields.
375  *****************************************************************************/
376 static int OpenDisplay( vout_thread_t *p_vout )
377 {
378 #define p_b p_vout->p_sys->pp_buffer
379     ggi_color   col_fg;                                  /* foreground color */
380     ggi_color   col_bg;                                  /* background color */
381     int         i_index;                               /* all purposes index */
382     char        *psz_display;
383
384     /* Initialize library */
385     if( ggiInit() )
386     {
387         msg_Err( p_vout, "cannot initialize GGI library" );
388         return( 1 );
389     }
390
391     /* Open display */
392     psz_display = config_GetPsz( p_vout, "ggi_display" );
393
394     p_vout->p_sys->p_display = ggiOpen( psz_display, NULL );
395     if( psz_display ) free( psz_display );
396
397     if( p_vout->p_sys->p_display == NULL )
398     {
399         msg_Err( p_vout, "cannot open GGI default display" );
400         ggiExit();
401         return( 1 );
402     }
403
404     /* Find most appropriate mode */
405     p_vout->p_sys->mode.frames =    2;                          /* 2 buffers */
406     p_vout->p_sys->mode.visible.x = config_GetInt( p_vout, "width" );
407     p_vout->p_sys->mode.visible.y = config_GetInt( p_vout, "height" );
408     p_vout->p_sys->mode.virt.x =    GGI_AUTO;
409     p_vout->p_sys->mode.virt.y =    GGI_AUTO;
410     p_vout->p_sys->mode.size.x =    GGI_AUTO;
411     p_vout->p_sys->mode.size.y =    GGI_AUTO;
412     p_vout->p_sys->mode.graphtype = GT_15BIT;        /* minimum usable depth */
413     p_vout->p_sys->mode.dpp.x =     GGI_AUTO;
414     p_vout->p_sys->mode.dpp.y =     GGI_AUTO;
415     ggiCheckMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode );
416
417     /* FIXME: Check that returned mode has some minimum properties */
418
419     /* Set mode */
420     if( ggiSetMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode ) )
421     {
422         msg_Err( p_vout, "cannot set GGI mode" );
423         ggiClose( p_vout->p_sys->p_display );
424         ggiExit();
425         return( 1 );
426     }
427
428     /* Check buffers properties */
429     p_vout->p_sys->b_must_acquire = 0;
430     for( i_index = 0; i_index < 2; i_index++ )
431     {
432         /* Get buffer address */
433         p_vout->p_sys->pp_buffer[ i_index ] =
434             (ggi_directbuffer *)ggiDBGetBuffer( p_vout->p_sys->p_display,
435                                                 i_index );
436         if( p_b[ i_index ] == NULL )
437         {
438             msg_Err( p_vout, "double buffering is not possible" );
439             ggiClose( p_vout->p_sys->p_display );
440             ggiExit();
441             return( 1 );
442         }
443
444         /* Check buffer properties */
445         if( ! ( p_b[ i_index ]->type & GGI_DB_SIMPLE_PLB )
446            || ( p_b[ i_index ]->page_size != 0 )
447            || ( p_b[ i_index ]->write == NULL )
448            || ( p_b[ i_index ]->noaccess != 0 )
449            || ( p_b[ i_index ]->align != 0 ) )
450         {
451             msg_Err( p_vout, "incorrect video memory type" );
452             ggiClose( p_vout->p_sys->p_display );
453             ggiExit();
454             return( 1 );
455         }
456
457         /* Check if buffer needs to be acquired before write */
458         if( ggiResourceMustAcquire( p_b[ i_index ]->resource ) )
459         {
460             p_vout->p_sys->b_must_acquire = 1;
461         }
462     }
463
464     /* Set graphic context colors */
465     col_fg.r = col_fg.g = col_fg.b = -1;
466     col_bg.r = col_bg.g = col_bg.b = 0;
467     if( ggiSetGCForeground(p_vout->p_sys->p_display,
468                            ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
469         ggiSetGCBackground(p_vout->p_sys->p_display,
470                            ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
471     {
472         msg_Err( p_vout, "cannot set colors" );
473         ggiClose( p_vout->p_sys->p_display );
474         ggiExit();
475         return( 1 );
476     }
477
478     /* Set clipping for text */
479     if( ggiSetGCClipping( p_vout->p_sys->p_display, 0, 0,
480                           p_vout->p_sys->mode.visible.x,
481                           p_vout->p_sys->mode.visible.y ) )
482     {
483         msg_Err( p_vout, "cannot set clipping" );
484         ggiClose( p_vout->p_sys->p_display );
485         ggiExit();
486         return( 1 );
487     }
488
489     /* FIXME: set palette in 8bpp */
490     p_vout->p_sys->i_bits_per_pixel = p_b[ 0 ]->buffer.plb.pixelformat->depth;
491
492     return( 0 );
493 #undef p_b
494 }
495
496 /*****************************************************************************
497  * CloseDisplay: close and reset GGI device
498  *****************************************************************************
499  * This function returns all resources allocated by OpenDisplay and restore
500  * the original state of the device.
501  *****************************************************************************/
502 static void CloseDisplay( vout_thread_t *p_vout )
503 {
504     /* Restore original mode and close display */
505     ggiClose( p_vout->p_sys->p_display );
506
507     /* Exit library */
508     ggiExit();
509 }
510
511 /*****************************************************************************
512  * SetPalette: sets an 8 bpp palette
513  *****************************************************************************/
514 static void SetPalette( vout_thread_t *p_vout,
515                         uint16_t *red, uint16_t *green, uint16_t *blue )
516 {
517     ggi_color colors[256];
518     int i;
519
520     /* Fill colors with color information */
521     for( i = 0; i < 256; i++ )
522     {
523         colors[ i ].r = red[ i ];
524         colors[ i ].g = green[ i ];
525         colors[ i ].b = blue[ i ];
526         colors[ i ].a = 0;
527     }
528
529     /* Set palette */
530     if( ggiSetPalette( p_vout->p_sys->p_display, 0, 256, colors ) < 0 )
531     {
532         msg_Err( p_vout, "failed to set palette" );
533     }
534 }
535