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