* ./evc/plugins.vcp.in: fixed plugin entry points for WinCE.
[vlc.git] / modules / video_output / ggi.c
1 /*****************************************************************************
2  * ggi.c : GGI plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: ggi.c,v 1.2 2002/11/10 18:04:22 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 int  Create    ( vlc_object_t * );
42 static void Destroy   ( vlc_object_t * );
43
44 static int  Init      ( vout_thread_t * );
45 static void End       ( vout_thread_t * );                         
46 static int  Manage    ( vout_thread_t * );               
47 static void Display   ( vout_thread_t *, picture_t * );            
48
49 static int  OpenDisplay    ( vout_thread_t * );
50 static void CloseDisplay   ( vout_thread_t * );
51 static void SetPalette     ( vout_thread_t *, u16 *, u16 *, u16 * );
52
53 /*****************************************************************************
54  * Module descriptor
55  *****************************************************************************/
56 #define DISPLAY_TEXT N_("X11 display name")
57 #define DISPLAY_LONGTEXT N_("Specify the X11 hardware display you want to use."\
58                            "\nBy default vlc will use the value of the DISPLAY"\
59                            " environment variable.")
60
61 vlc_module_begin();                                
62     add_category_hint( N_("Miscellaneous"), NULL );
63     add_string( "ggi-display", NULL, NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT );
64     set_description( "General Graphics Interface video output" );
65     set_capability( "video output", 30 );
66     set_callbacks( Create, Destroy );
67 vlc_module_end();
68
69 /*****************************************************************************
70  * vout_sys_t: video output GGI method descriptor
71  *****************************************************************************
72  * This structure is part of the video output thread descriptor.
73  * It describes the GGI specific properties of an output thread.
74  *****************************************************************************/
75 struct vout_sys_t
76 {
77     /* GGI system informations */
78     ggi_visual_t        p_display;                         /* display device */
79
80     ggi_mode            mode;                             /* mode descriptor */
81     int                 i_bits_per_pixel;
82
83     /* Buffer information */
84     ggi_directbuffer *  pp_buffer[2];                             /* buffers */
85     int                 i_index;
86
87     vlc_bool_t          b_must_acquire;   /* must be acquired before writing */
88 };
89
90 /*****************************************************************************
91  * Create: allocate GGI video thread output method
92  *****************************************************************************
93  * This function allocate and initialize a GGI vout method. It uses some of the
94  * vout properties to choose the correct mode, and change them according to the
95  * mode actually used.
96  *****************************************************************************/
97 static int Create( vlc_object_t *p_this )
98 {
99     vout_thread_t *p_vout = (vout_thread_t *)p_this;
100
101     /* Allocate structure */
102     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
103     if( p_vout->p_sys == NULL )
104     {
105         msg_Err( p_vout, "out of memory" );
106         return( 1 );
107     }
108
109     /* Open and initialize device */
110     if( OpenDisplay( p_vout ) )
111     {
112         msg_Err( p_vout, "cannot initialize GGI display" );
113         free( p_vout->p_sys );
114         return( 1 );
115     }
116
117     p_vout->pf_init = Init;
118     p_vout->pf_end = End;
119     p_vout->pf_manage = Manage;
120     p_vout->pf_render = NULL;
121     p_vout->pf_display = Display;
122
123     return( 0 );
124 }
125
126 /*****************************************************************************
127  * Init: initialize GGI video thread output method
128  *****************************************************************************
129  * This function initialize the GGI display device.
130  *****************************************************************************/
131 static int Init( vout_thread_t *p_vout )
132 {
133 #define p_b p_vout->p_sys->pp_buffer
134     int i_index;
135     picture_t *p_pic;
136
137     I_OUTPUTPICTURES = 0;
138
139     p_vout->output.i_width  = p_vout->p_sys->mode.visible.x;
140     p_vout->output.i_height = p_vout->p_sys->mode.visible.y;
141     p_vout->output.i_aspect = p_vout->p_sys->mode.visible.x
142                                * VOUT_ASPECT_FACTOR
143                                / p_vout->p_sys->mode.visible.y;
144
145     switch( p_vout->p_sys->i_bits_per_pixel )
146     {
147         case 8:
148             p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
149             p_vout->output.pf_setpalette = SetPalette;
150             break;
151         case 15:
152             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
153         case 16:
154             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
155         case 24:
156             p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
157         case 32:
158             p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
159         default:
160             msg_Err( p_vout, "unknown screen depth %i",
161                      p_vout->p_sys->i_bits_per_pixel );
162             return 0;
163     }
164
165     /* Only useful for bits_per_pixel != 8 */
166     p_vout->output.i_rmask = p_b[ 0 ]->buffer.plb.pixelformat->red_mask;
167     p_vout->output.i_gmask = p_b[ 0 ]->buffer.plb.pixelformat->green_mask;
168     p_vout->output.i_bmask = p_b[ 0 ]->buffer.plb.pixelformat->blue_mask;
169
170     p_pic = NULL;
171
172     /* Find an empty picture slot */
173     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
174     {
175         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
176         {
177             p_pic = p_vout->p_picture + i_index;
178             break;
179         }
180     }
181
182     if( p_pic == NULL )
183     {
184         return 0;
185     }
186
187     /* We know the chroma, allocate a buffer which will be used
188      * directly by the decoder */
189     p_vout->p_sys->i_index = 0;
190     p_pic->p->p_pixels = p_b[ 0 ]->write;
191     p_pic->p->i_pixel_pitch = p_b[ 0 ]->buffer.plb.pixelformat->size / 8;
192     p_pic->p->i_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 occured.
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                         p_vout->p_vlc->b_die = 1;
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, u16 *red, u16 *green, u16 *blue )
515 {
516     ggi_color colors[256];
517     int i;
518   
519     /* Fill colors with color information */
520     for( i = 0; i < 256; i++ )
521     {
522         colors[ i ].r = red[ i ];
523         colors[ i ].g = green[ i ];
524         colors[ i ].b = blue[ i ];
525         colors[ i ].a = 0;
526     }
527
528     /* Set palette */
529     if( ggiSetPalette( p_vout->p_sys->p_display, 0, 256, colors ) < 0 )
530     {
531         msg_Err( p_vout, "failed setting palette" );
532     }
533 }
534