]> git.sesse.net Git - vlc/blob - modules/video_output/ggi.c
* ./modules/audio_output/oss.c: we spare a variable by using p_aout->b_die
[vlc] / 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.1 2002/08/13 11:59:36 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
277     mask = emKeyboard | emPtrButtonPress | emPtrButtonRelease;
278
279     ggiEventPoll( p_vout->p_sys->p_display, mask, &tv );
280     
281     while( ggiEventsQueued( p_vout->p_sys->p_display, mask) )
282     {
283         ggiEventRead( p_vout->p_sys->p_display, &event, mask);
284
285         switch( event.any.type )
286         {
287             case evKeyRelease:
288
289                 switch( event.key.sym )
290                 {
291                     case 'q':
292                     case 'Q':
293                     case GIIUC_Escape:
294                         /* FIXME pass message ! */
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_RIGHT:
308                         {
309                             intf_thread_t *p_intf;
310                             p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
311                                                               FIND_ANYWHERE );
312                             if( p_intf )
313                             {
314                                 p_intf->b_menu_change = 1;
315                                 vlc_object_release( p_intf );
316                             }
317                         }
318                         break;
319                 }
320                 break;
321
322             default:
323                 break;
324         }
325     }
326
327     return( 0 );
328 }
329
330 /*****************************************************************************
331  * Display: displays previously rendered output
332  *****************************************************************************/
333 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
334 {
335 #define p_b p_vout->p_sys->pp_buffer
336     p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
337
338     /* Change display frame */
339     if( p_vout->p_sys->b_must_acquire )
340     {
341         ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
342     }
343     ggiSetDisplayFrame( p_vout->p_sys->p_display,
344                         p_b[ p_vout->p_sys->i_index ]->frame );
345
346     /* Swap buffers and change write frame */
347     p_vout->p_sys->i_index ^= 1;
348     p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
349
350     if( p_vout->p_sys->b_must_acquire )
351     {
352         ggiResourceAcquire( p_b[ p_vout->p_sys->i_index ]->resource,
353                             GGI_ACTYPE_WRITE );
354     }
355     ggiSetWriteFrame( p_vout->p_sys->p_display,
356                       p_b[ p_vout->p_sys->i_index ]->frame );
357
358     /* Flush the output so that it actually displays */
359     ggiFlush( p_vout->p_sys->p_display );
360 #undef p_b
361 }
362
363 /* following functions are local */
364
365 /*****************************************************************************
366  * OpenDisplay: open and initialize GGI device
367  *****************************************************************************
368  * Open and initialize display according to preferences specified in the vout
369  * thread fields.
370  *****************************************************************************/
371 static int OpenDisplay( vout_thread_t *p_vout )
372 {
373 #define p_b p_vout->p_sys->pp_buffer
374     ggi_color   col_fg;                                  /* foreground color */
375     ggi_color   col_bg;                                  /* background color */
376     int         i_index;                               /* all purposes index */
377     char        *psz_display;
378
379     /* Initialize library */
380     if( ggiInit() )
381     {
382         msg_Err( p_vout, "cannot initialize GGI library" );
383         return( 1 );
384     }
385
386     /* Open display */
387     psz_display = config_GetPsz( p_vout, "ggi_display" );
388
389     p_vout->p_sys->p_display = ggiOpen( psz_display, NULL );
390     if( psz_display ) free( psz_display );
391
392     if( p_vout->p_sys->p_display == NULL )
393     {
394         msg_Err( p_vout, "cannot open GGI default display" );
395         ggiExit();
396         return( 1 );
397     }
398
399     /* Find most appropriate mode */
400     p_vout->p_sys->mode.frames =    2;                          /* 2 buffers */
401     p_vout->p_sys->mode.visible.x = config_GetInt( p_vout, "width" );
402     p_vout->p_sys->mode.visible.y = config_GetInt( p_vout, "height" );
403     p_vout->p_sys->mode.virt.x =    GGI_AUTO;
404     p_vout->p_sys->mode.virt.y =    GGI_AUTO;
405     p_vout->p_sys->mode.size.x =    GGI_AUTO;
406     p_vout->p_sys->mode.size.y =    GGI_AUTO;
407     p_vout->p_sys->mode.graphtype = GT_15BIT;        /* minimum usable depth */
408     p_vout->p_sys->mode.dpp.x =     GGI_AUTO;
409     p_vout->p_sys->mode.dpp.y =     GGI_AUTO;
410     ggiCheckMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode );
411
412     /* FIXME: Check that returned mode has some minimum properties */
413
414     /* Set mode */
415     if( ggiSetMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode ) )
416     {
417         msg_Err( p_vout, "cannot set GGI mode" );
418         ggiClose( p_vout->p_sys->p_display );
419         ggiExit();
420         return( 1 );
421     }
422
423     /* Check buffers properties */
424     p_vout->p_sys->b_must_acquire = 0;
425     for( i_index = 0; i_index < 2; i_index++ )
426     {
427         /* Get buffer address */
428         p_vout->p_sys->pp_buffer[ i_index ] =
429             (ggi_directbuffer *)ggiDBGetBuffer( p_vout->p_sys->p_display,
430                                                 i_index );
431         if( p_b[ i_index ] == NULL )
432         {
433             msg_Err( p_vout, "double buffering is not possible" );
434             ggiClose( p_vout->p_sys->p_display );
435             ggiExit();
436             return( 1 );
437         }
438
439         /* Check buffer properties */
440         if( ! ( p_b[ i_index ]->type & GGI_DB_SIMPLE_PLB )
441            || ( p_b[ i_index ]->page_size != 0 )
442            || ( p_b[ i_index ]->write == NULL )
443            || ( p_b[ i_index ]->noaccess != 0 )
444            || ( p_b[ i_index ]->align != 0 ) )
445         {
446             msg_Err( p_vout, "incorrect video memory type" );
447             ggiClose( p_vout->p_sys->p_display );
448             ggiExit();
449             return( 1 );
450         }
451
452         /* Check if buffer needs to be acquired before write */
453         if( ggiResourceMustAcquire( p_b[ i_index ]->resource ) )
454         {
455             p_vout->p_sys->b_must_acquire = 1;
456         }
457     }
458
459     /* Set graphic context colors */
460     col_fg.r = col_fg.g = col_fg.b = -1;
461     col_bg.r = col_bg.g = col_bg.b = 0;
462     if( ggiSetGCForeground(p_vout->p_sys->p_display,
463                            ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
464         ggiSetGCBackground(p_vout->p_sys->p_display,
465                            ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
466     {
467         msg_Err( p_vout, "cannot set colors" );
468         ggiClose( p_vout->p_sys->p_display );
469         ggiExit();
470         return( 1 );
471     }
472
473     /* Set clipping for text */
474     if( ggiSetGCClipping( p_vout->p_sys->p_display, 0, 0,
475                           p_vout->p_sys->mode.visible.x,
476                           p_vout->p_sys->mode.visible.y ) )
477     {
478         msg_Err( p_vout, "cannot set clipping" );
479         ggiClose( p_vout->p_sys->p_display );
480         ggiExit();
481         return( 1 );
482     }
483
484     /* FIXME: set palette in 8bpp */
485     p_vout->p_sys->i_bits_per_pixel = p_b[ 0 ]->buffer.plb.pixelformat->depth;
486
487     return( 0 );
488 #undef p_b
489 }
490
491 /*****************************************************************************
492  * CloseDisplay: close and reset GGI device
493  *****************************************************************************
494  * This function returns all resources allocated by OpenDisplay and restore
495  * the original state of the device.
496  *****************************************************************************/
497 static void CloseDisplay( vout_thread_t *p_vout )
498 {
499     /* Restore original mode and close display */
500     ggiClose( p_vout->p_sys->p_display );
501
502     /* Exit library */
503     ggiExit();
504 }
505
506 /*****************************************************************************
507  * SetPalette: sets an 8 bpp palette
508  *****************************************************************************/
509 static void SetPalette( vout_thread_t *p_vout, u16 *red, u16 *green, u16 *blue )
510 {
511     ggi_color colors[256];
512     int i;
513   
514     /* Fill colors with color information */
515     for( i = 0; i < 256; i++ )
516     {
517         colors[ i ].r = red[ i ];
518         colors[ i ].g = green[ i ];
519         colors[ i ].b = blue[ i ];
520         colors[ i ].a = 0;
521     }
522
523     /* Set palette */
524     if( ggiSetPalette( p_vout->p_sys->p_display, 0, 256, colors ) < 0 )
525     {
526         msg_Err( p_vout, "failed setting palette" );
527     }
528 }
529