]> git.sesse.net Git - vlc/blob - plugins/ggi/ggi.c
* ALL: changed "struct foo_s" into "struct foo_t" to make greppers happy.
[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.23 2002/07/20 18:01:42 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 = FOURCC_RGB2;
167             p_vout->output.pf_setpalette = SetPalette;
168             break;
169         case 15:
170             p_vout->output.i_chroma = FOURCC_RV15; break;
171         case 16:
172             p_vout->output.i_chroma = FOURCC_RV16; break;
173         case 24:
174             p_vout->output.i_chroma = FOURCC_RV24; break;
175         case 32:
176             p_vout->output.i_chroma = FOURCC_RV32; 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_bytes = p_b[ 0 ]->buffer.plb.pixelformat->size / 8;
210     p_pic->p->i_lines = p_vout->p_sys->mode.visible.y;
211
212     if( p_b[ 0 ]->buffer.plb.pixelformat->size / 8
213          * p_vout->p_sys->mode.visible.x
214         != p_b[ 0 ]->buffer.plb.stride )
215     {
216         p_pic->p->b_margin = 1;
217         p_pic->p->b_hidden = 1;
218         p_pic->p->i_pitch = p_b[ 0 ]->buffer.plb.stride;
219         p_pic->p->i_visible_bytes = p_b[ 0 ]->buffer.plb.pixelformat->size
220                                      / 8 * p_vout->p_sys->mode.visible.x;
221     }
222     else
223     {
224         p_pic->p->b_margin = 0;
225         p_pic->p->i_pitch = p_b[ 0 ]->buffer.plb.stride;
226     }
227
228     p_pic->i_planes = 1;
229
230     p_pic->i_status = DESTROYED_PICTURE;
231     p_pic->i_type   = DIRECT_PICTURE;
232
233     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
234
235     I_OUTPUTPICTURES++;
236
237     /* Acquire first buffer */
238     if( p_vout->p_sys->b_must_acquire )
239     {
240         ggiResourceAcquire( p_b[ 0 ]->resource,
241                             GGI_ACTYPE_WRITE );
242     }
243
244     /* Listen to the keyboard and the mouse buttons */
245     ggiSetEventMask( p_vout->p_sys->p_display,
246                      emKeyboard | emPtrButtonPress | emPtrButtonRelease );
247
248     /* Set asynchronous display mode -- usually quite faster */
249     ggiAddFlags( p_vout->p_sys->p_display, GGIFLAG_ASYNC );
250
251     return( 0 );
252 #undef p_b
253 }
254
255 /*****************************************************************************
256  * vout_End: terminate GGI video thread output method
257  *****************************************************************************
258  * Terminate an output method created by vout_Create
259  *****************************************************************************/
260 void vout_End( vout_thread_t *p_vout )
261 {
262 #define p_b p_vout->p_sys->pp_buffer
263     /* Release buffer */
264     if( p_vout->p_sys->b_must_acquire )
265     {
266         ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
267     }
268 #undef p_b
269 }
270
271 /*****************************************************************************
272  * vout_Destroy: destroy GGI video thread output method
273  *****************************************************************************
274  * Terminate an output method created by vout_Create
275  *****************************************************************************/
276 void vout_Destroy( vout_thread_t *p_vout )
277 {
278     CloseDisplay( p_vout );
279
280     free( p_vout->p_sys );
281 }
282
283 /*****************************************************************************
284  * vout_Manage: handle GGI events
285  *****************************************************************************
286  * This function should be called regularly by video output thread. It returns
287  * a non null value if an error occured.
288  *****************************************************************************/
289 int vout_Manage( vout_thread_t *p_vout )
290 {
291     struct timeval tv = { 0, 1000 };                        /* 1 millisecond */
292     gii_event_mask mask;
293     gii_event      event;
294
295     mask = emKeyboard | emPtrButtonPress | emPtrButtonRelease;
296
297     ggiEventPoll( p_vout->p_sys->p_display, mask, &tv );
298     
299     while( ggiEventsQueued( p_vout->p_sys->p_display, mask) )
300     {
301         ggiEventRead( p_vout->p_sys->p_display, &event, mask);
302
303         switch( event.any.type )
304         {
305             case evKeyRelease:
306
307                 switch( event.key.sym )
308                 {
309                     case 'q':
310                     case 'Q':
311                     case GIIUC_Escape:
312                         /* FIXME pass message ! */
313                         p_vout->p_vlc->b_die = 1;
314                         break;
315
316                     default:
317                         break;
318                 }
319                 break;
320
321             case evPtrButtonRelease:
322
323                 switch( event.pbutton.button )
324                 {
325                     case GII_PBUTTON_RIGHT:
326                         {
327                             intf_thread_t *p_intf;
328                             p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
329                                                               FIND_ANYWHERE );
330                             if( p_intf )
331                             {
332                                 p_intf->b_menu_change = 1;
333                                 vlc_object_release( p_intf );
334                             }
335                         }
336                         break;
337                 }
338                 break;
339
340             default:
341                 break;
342         }
343     }
344
345     return( 0 );
346 }
347
348 /*****************************************************************************
349  * vout_Render: displays previously rendered output
350  *****************************************************************************/
351 void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
352 {
353     ;
354 }
355
356 /*****************************************************************************
357  * vout_Display: displays previously rendered output
358  *****************************************************************************/
359 void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
360 {
361 #define p_b p_vout->p_sys->pp_buffer
362     p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
363
364     /* Change display frame */
365     if( p_vout->p_sys->b_must_acquire )
366     {
367         ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
368     }
369     ggiSetDisplayFrame( p_vout->p_sys->p_display,
370                         p_b[ p_vout->p_sys->i_index ]->frame );
371
372     /* Swap buffers and change write frame */
373     p_vout->p_sys->i_index ^= 1;
374     p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
375
376     if( p_vout->p_sys->b_must_acquire )
377     {
378         ggiResourceAcquire( p_b[ p_vout->p_sys->i_index ]->resource,
379                             GGI_ACTYPE_WRITE );
380     }
381     ggiSetWriteFrame( p_vout->p_sys->p_display,
382                       p_b[ p_vout->p_sys->i_index ]->frame );
383
384     /* Flush the output so that it actually displays */
385     ggiFlush( p_vout->p_sys->p_display );
386 #undef p_b
387 }
388
389 /* following functions are local */
390
391 /*****************************************************************************
392  * OpenDisplay: open and initialize GGI device
393  *****************************************************************************
394  * Open and initialize display according to preferences specified in the vout
395  * thread fields.
396  *****************************************************************************/
397 static int OpenDisplay( vout_thread_t *p_vout )
398 {
399 #define p_b p_vout->p_sys->pp_buffer
400     ggi_color   col_fg;                                  /* foreground color */
401     ggi_color   col_bg;                                  /* background color */
402     int         i_index;                               /* all purposes index */
403     char        *psz_display;
404
405     /* Initialize library */
406     if( ggiInit() )
407     {
408         msg_Err( p_vout, "cannot initialize GGI library" );
409         return( 1 );
410     }
411
412     /* Open display */
413     psz_display = config_GetPsz( p_vout, "ggi_display" );
414
415     p_vout->p_sys->p_display = ggiOpen( psz_display, NULL );
416     if( psz_display ) free( psz_display );
417
418     if( p_vout->p_sys->p_display == NULL )
419     {
420         msg_Err( p_vout, "cannot open GGI default display" );
421         ggiExit();
422         return( 1 );
423     }
424
425     /* Find most appropriate mode */
426     p_vout->p_sys->mode.frames =    2;                          /* 2 buffers */
427     p_vout->p_sys->mode.visible.x = config_GetInt( p_vout, "width" );
428     p_vout->p_sys->mode.visible.y = config_GetInt( p_vout, "height" );
429     p_vout->p_sys->mode.virt.x =    GGI_AUTO;
430     p_vout->p_sys->mode.virt.y =    GGI_AUTO;
431     p_vout->p_sys->mode.size.x =    GGI_AUTO;
432     p_vout->p_sys->mode.size.y =    GGI_AUTO;
433     p_vout->p_sys->mode.graphtype = GT_15BIT;        /* minimum usable depth */
434     p_vout->p_sys->mode.dpp.x =     GGI_AUTO;
435     p_vout->p_sys->mode.dpp.y =     GGI_AUTO;
436     ggiCheckMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode );
437
438     /* FIXME: Check that returned mode has some minimum properties */
439
440     /* Set mode */
441     if( ggiSetMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode ) )
442     {
443         msg_Err( p_vout, "cannot set GGI mode" );
444         ggiClose( p_vout->p_sys->p_display );
445         ggiExit();
446         return( 1 );
447     }
448
449     /* Check buffers properties */
450     p_vout->p_sys->b_must_acquire = 0;
451     for( i_index = 0; i_index < 2; i_index++ )
452     {
453         /* Get buffer address */
454         p_vout->p_sys->pp_buffer[ i_index ] =
455             (ggi_directbuffer *)ggiDBGetBuffer( p_vout->p_sys->p_display,
456                                                 i_index );
457         if( p_b[ i_index ] == NULL )
458         {
459             msg_Err( p_vout, "double buffering is not possible" );
460             ggiClose( p_vout->p_sys->p_display );
461             ggiExit();
462             return( 1 );
463         }
464
465         /* Check buffer properties */
466         if( ! ( p_b[ i_index ]->type & GGI_DB_SIMPLE_PLB )
467            || ( p_b[ i_index ]->page_size != 0 )
468            || ( p_b[ i_index ]->write == NULL )
469            || ( p_b[ i_index ]->noaccess != 0 )
470            || ( p_b[ i_index ]->align != 0 ) )
471         {
472             msg_Err( p_vout, "incorrect video memory type" );
473             ggiClose( p_vout->p_sys->p_display );
474             ggiExit();
475             return( 1 );
476         }
477
478         /* Check if buffer needs to be acquired before write */
479         if( ggiResourceMustAcquire( p_b[ i_index ]->resource ) )
480         {
481             p_vout->p_sys->b_must_acquire = 1;
482         }
483     }
484
485     /* Set graphic context colors */
486     col_fg.r = col_fg.g = col_fg.b = -1;
487     col_bg.r = col_bg.g = col_bg.b = 0;
488     if( ggiSetGCForeground(p_vout->p_sys->p_display,
489                            ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
490         ggiSetGCBackground(p_vout->p_sys->p_display,
491                            ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
492     {
493         msg_Err( p_vout, "cannot set colors" );
494         ggiClose( p_vout->p_sys->p_display );
495         ggiExit();
496         return( 1 );
497     }
498
499     /* Set clipping for text */
500     if( ggiSetGCClipping( p_vout->p_sys->p_display, 0, 0,
501                           p_vout->p_sys->mode.visible.x,
502                           p_vout->p_sys->mode.visible.y ) )
503     {
504         msg_Err( p_vout, "cannot set clipping" );
505         ggiClose( p_vout->p_sys->p_display );
506         ggiExit();
507         return( 1 );
508     }
509
510     /* FIXME: set palette in 8bpp */
511     p_vout->p_sys->i_bits_per_pixel = p_b[ 0 ]->buffer.plb.pixelformat->depth;
512
513     return( 0 );
514 #undef p_b
515 }
516
517 /*****************************************************************************
518  * CloseDisplay: close and reset GGI device
519  *****************************************************************************
520  * This function returns all resources allocated by OpenDisplay and restore
521  * the original state of the device.
522  *****************************************************************************/
523 static void CloseDisplay( vout_thread_t *p_vout )
524 {
525     /* Restore original mode and close display */
526     ggiClose( p_vout->p_sys->p_display );
527
528     /* Exit library */
529     ggiExit();
530 }
531
532 /*****************************************************************************
533  * SetPalette: sets an 8 bpp palette
534  *****************************************************************************/
535 static void SetPalette( vout_thread_t *p_vout, u16 *red, u16 *green, u16 *blue )
536 {
537     ggi_color colors[256];
538     int i;
539   
540     /* Fill colors with color information */
541     for( i = 0; i < 256; i++ )
542     {
543         colors[ i ].r = red[ i ];
544         colors[ i ].g = green[ i ];
545         colors[ i ].b = blue[ i ];
546         colors[ i ].a = 0;
547     }
548
549     /* Set palette */
550     if( ggiSetPalette( p_vout->p_sys->p_display, 0, 256, colors ) < 0 )
551     {
552         msg_Err( p_vout, "failed setting palette" );
553     }
554 }
555