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