1 /*****************************************************************************
2 * ggi.c : GGI plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: ggi.c,v 1.12 2002/01/12 01:25:57 sam Exp $
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
30 #include <errno.h> /* ENOMEM */
34 #include <videolan/vlc.h>
37 #include "video_output.h"
40 #include "interface.h"
42 /*****************************************************************************
44 *****************************************************************************/
45 static void vout_getfunctions( function_list_t * p_function_list );
47 static int vout_Probe ( probedata_t *p_data );
48 static int vout_Create ( vout_thread_t * );
49 static int vout_Init ( vout_thread_t * );
50 static void vout_End ( vout_thread_t * );
51 static void vout_Destroy ( vout_thread_t * );
52 static int vout_Manage ( vout_thread_t * );
53 static void vout_Render ( vout_thread_t *, picture_t * );
54 static void vout_Display ( vout_thread_t *, picture_t * );
56 static int OpenDisplay ( vout_thread_t * );
57 static void CloseDisplay ( vout_thread_t * );
59 /*****************************************************************************
60 * Building configuration tree
61 *****************************************************************************/
66 SET_DESCRIPTION( "General Graphics Interface video output" )
67 ADD_CAPABILITY( VOUT, 30 )
72 vout_getfunctions( &p_module->p_functions->vout );
75 MODULE_DEACTIVATE_START
76 MODULE_DEACTIVATE_STOP
78 /*****************************************************************************
79 * vout_sys_t: video output GGI method descriptor
80 *****************************************************************************
81 * This structure is part of the video output thread descriptor.
82 * It describes the GGI specific properties of an output thread.
83 *****************************************************************************/
84 typedef struct vout_sys_s
86 /* GGI system informations */
87 ggi_visual_t p_display; /* display device */
89 ggi_mode mode; /* mode descriptor */
92 /* Buffer information */
93 ggi_directbuffer * pp_buffer[2]; /* buffers */
96 boolean_t b_must_acquire; /* must be acquired before writing */
99 /*****************************************************************************
100 * Functions exported as capabilities. They are declared as static so that
101 * we don't pollute the namespace too much.
102 *****************************************************************************/
103 static void vout_getfunctions( function_list_t * p_function_list )
105 p_function_list->pf_probe = vout_Probe;
106 p_function_list->functions.vout.pf_create = vout_Create;
107 p_function_list->functions.vout.pf_init = vout_Init;
108 p_function_list->functions.vout.pf_end = vout_End;
109 p_function_list->functions.vout.pf_destroy = vout_Destroy;
110 p_function_list->functions.vout.pf_manage = vout_Manage;
111 p_function_list->functions.vout.pf_render = vout_Render;
112 p_function_list->functions.vout.pf_display = vout_Display;
115 /*****************************************************************************
116 * vout_Probe: probe the video driver and return a score
117 *****************************************************************************
118 * This function tries to initialize GGI and returns a score to the
119 * plugin manager so that it can select the best plugin.
120 *****************************************************************************/
121 static int vout_Probe( probedata_t *p_data )
126 /*****************************************************************************
127 * vout_Create: allocate GGI video thread output method
128 *****************************************************************************
129 * This function allocate and initialize a GGI vout method. It uses some of the
130 * vout properties to choose the correct mode, and change them according to the
131 * mode actually used.
132 *****************************************************************************/
133 int vout_Create( vout_thread_t *p_vout )
135 /* Allocate structure */
136 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
137 if( p_vout->p_sys == NULL )
139 intf_ErrMsg( "vout error: %s", strerror(ENOMEM) );
143 /* Open and initialize device */
144 if( OpenDisplay( p_vout ) )
146 intf_ErrMsg( "vout error: can't initialize GGI display" );
147 free( p_vout->p_sys );
154 /*****************************************************************************
155 * vout_Init: initialize GGI video thread output method
156 *****************************************************************************
157 * This function initialize the GGI display device.
158 *****************************************************************************/
159 int vout_Init( vout_thread_t *p_vout )
164 I_OUTPUTPICTURES = 0;
166 p_vout->output.i_width = p_vout->p_sys->mode.visible.x;
167 p_vout->output.i_height = p_vout->p_sys->mode.visible.y;
168 p_vout->output.i_aspect = p_vout->p_sys->mode.visible.x
170 / p_vout->p_sys->mode.visible.y;
172 switch( p_vout->p_sys->i_bits_per_pixel )
174 case 8: /* FIXME: set the palette */
175 p_vout->output.i_chroma = FOURCC_BI_RGB; break;
177 p_vout->output.i_chroma = FOURCC_RV15; break;
179 p_vout->output.i_chroma = FOURCC_RV16; break;
181 p_vout->output.i_chroma = FOURCC_BI_BITFIELDS; break;
183 p_vout->output.i_chroma = FOURCC_BI_BITFIELDS; break;
185 intf_ErrMsg( "vout error: unknown screen depth" );
191 /* Find an empty picture slot */
192 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
194 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
196 p_pic = p_vout->p_picture + i_index;
206 #define p_b p_vout->p_sys->pp_buffer
207 /* We know the chroma, allocate a buffer which will be used
208 * directly by the decoder */
209 p_vout->p_sys->i_index = 0;
210 p_pic->p->p_pixels = p_b[ 0 ]->write;
211 p_pic->p->i_pixel_bytes = p_b[ 0 ]->buffer.plb.pixelformat->size / 8;
212 p_pic->p->i_lines = p_vout->p_sys->mode.visible.y;
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 )
218 p_pic->p->b_margin = 1;
219 p_pic->p->b_hidden = 1;
220 p_pic->p->i_pitch = p_b[ 0 ]->buffer.plb.stride;
221 p_pic->p->i_visible_bytes = p_b[ 0 ]->buffer.plb.pixelformat->size
222 / 8 * p_vout->p_sys->mode.visible.x;
226 p_pic->p->b_margin = 0;
227 p_pic->p->i_pitch = p_b[ 0 ]->buffer.plb.stride;
230 /* Only useful for bits_per_pixel != 8 */
231 p_pic->p->i_red_mask = p_b[ 0 ]->buffer.plb.pixelformat->red_mask;
232 p_pic->p->i_green_mask = p_b[ 0 ]->buffer.plb.pixelformat->green_mask;
233 p_pic->p->i_blue_mask = p_b[ 0 ]->buffer.plb.pixelformat->blue_mask;
237 p_pic->i_status = DESTROYED_PICTURE;
238 p_pic->i_type = DIRECT_PICTURE;
240 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
244 /* Acquire first buffer */
245 if( p_vout->p_sys->b_must_acquire )
247 ggiResourceAcquire( p_b[ 0 ]->resource,
251 /* Listen to the keyboard and the mouse buttons */
252 ggiSetEventMask( p_vout->p_sys->p_display,
253 emKeyboard | emPtrButtonPress | emPtrButtonRelease );
255 /* Set asynchronous display mode -- usually quite faster */
256 ggiAddFlags( p_vout->p_sys->p_display, GGIFLAG_ASYNC );
262 /*****************************************************************************
263 * vout_End: terminate GGI video thread output method
264 *****************************************************************************
265 * Terminate an output method created by vout_Create
266 *****************************************************************************/
267 void vout_End( vout_thread_t *p_vout )
269 #define p_b p_vout->p_sys->pp_buffer
271 if( p_vout->p_sys->b_must_acquire )
273 ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
278 /*****************************************************************************
279 * vout_Destroy: destroy GGI video thread output method
280 *****************************************************************************
281 * Terminate an output method created by vout_Create
282 *****************************************************************************/
283 void vout_Destroy( vout_thread_t *p_vout )
285 CloseDisplay( p_vout );
287 free( p_vout->p_sys );
290 /*****************************************************************************
291 * vout_Manage: handle GGI events
292 *****************************************************************************
293 * This function should be called regularly by video output thread. It returns
294 * a non null value if an error occured.
295 *****************************************************************************/
296 int vout_Manage( vout_thread_t *p_vout )
298 struct timeval tv = { 0, 1000 }; /* 1 millisecond */
302 mask = emKeyboard | emPtrButtonPress | emPtrButtonRelease;
304 ggiEventPoll( p_vout->p_sys->p_display, mask, &tv );
306 while( ggiEventsQueued( p_vout->p_sys->p_display, mask) )
308 ggiEventRead( p_vout->p_sys->p_display, &event, mask);
310 switch( event.any.type )
314 switch( event.key.sym )
319 /* FIXME pass message ! */
320 p_main->p_intf->b_die = 1;
328 case evPtrButtonRelease:
330 switch( event.pbutton.button )
332 case GII_PBUTTON_RIGHT:
333 /* FIXME: need locking ! */
334 p_main->p_intf->b_menu_change = 1;
347 /*****************************************************************************
348 * vout_Render: displays previously rendered output
349 *****************************************************************************/
350 void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
355 /*****************************************************************************
356 * vout_Display: displays previously rendered output
357 *****************************************************************************/
358 void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
360 #define p_b p_vout->p_sys->pp_buffer
361 p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
363 /* Change display frame */
364 if( p_vout->p_sys->b_must_acquire )
366 ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
368 ggiSetDisplayFrame( p_vout->p_sys->p_display,
369 p_b[ p_vout->p_sys->i_index ]->frame );
371 /* Swap buffers and change write frame */
372 p_vout->p_sys->i_index ^= 1;
373 p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
375 if( p_vout->p_sys->b_must_acquire )
377 ggiResourceAcquire( p_b[ p_vout->p_sys->i_index ]->resource,
380 ggiSetWriteFrame( p_vout->p_sys->p_display,
381 p_b[ p_vout->p_sys->i_index ]->frame );
383 /* Flush the output so that it actually displays */
384 ggiFlush( p_vout->p_sys->p_display );
388 /* following functions are local */
390 /*****************************************************************************
391 * OpenDisplay: open and initialize GGI device
392 *****************************************************************************
393 * Open and initialize display according to preferences specified in the vout
395 *****************************************************************************/
396 static int OpenDisplay( vout_thread_t *p_vout )
398 #define p_b p_vout->p_sys->pp_buffer
399 ggi_color col_fg; /* foreground color */
400 ggi_color col_bg; /* background color */
401 int i_index; /* all purposes index */
404 /* Initialize library */
407 intf_ErrMsg( "vout error: can't initialize GGI library" );
412 psz_display = main_GetPszVariable( VOUT_DISPLAY_VAR, NULL );
414 p_vout->p_sys->p_display = ggiOpen( psz_display, NULL );
416 if( p_vout->p_sys->p_display == NULL )
418 intf_ErrMsg( "vout error: can't open GGI default display" );
423 /* Find most appropriate mode */
424 p_vout->p_sys->mode.frames = 2; /* 2 buffers */
425 p_vout->p_sys->mode.visible.x = main_GetIntVariable( VOUT_WIDTH_VAR,
426 VOUT_WIDTH_DEFAULT );
427 p_vout->p_sys->mode.visible.y = main_GetIntVariable( VOUT_HEIGHT_VAR,
428 VOUT_HEIGHT_DEFAULT );
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 );
438 /* FIXME: Check that returned mode has some minimum properties */
441 if( ggiSetMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode ) )
443 intf_ErrMsg( "vout error: can't set GGI mode" );
444 ggiClose( p_vout->p_sys->p_display );
449 /* Check buffers properties */
450 p_vout->p_sys->b_must_acquire = 0;
451 for( i_index = 0; i_index < 2; i_index++ )
453 /* Get buffer address */
454 p_vout->p_sys->pp_buffer[ i_index ] =
455 (ggi_directbuffer *)ggiDBGetBuffer( p_vout->p_sys->p_display,
457 if( p_b[ i_index ] == NULL )
459 intf_ErrMsg( "vout error: double buffering is not possible" );
460 ggiClose( p_vout->p_sys->p_display );
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 ) )
472 intf_ErrMsg( "vout error: incorrect video memory type" );
473 ggiClose( p_vout->p_sys->p_display );
478 /* Check if buffer needs to be acquired before write */
479 if( ggiResourceMustAcquire( p_b[ i_index ]->resource ) )
481 p_vout->p_sys->b_must_acquire = 1;
485 if( p_vout->p_sys->b_must_acquire )
487 intf_DbgMsg("buffers must be acquired");
490 /* Set graphic context colors */
491 col_fg.r = col_fg.g = col_fg.b = -1;
492 col_bg.r = col_bg.g = col_bg.b = 0;
493 if( ggiSetGCForeground(p_vout->p_sys->p_display,
494 ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
495 ggiSetGCBackground(p_vout->p_sys->p_display,
496 ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
498 intf_ErrMsg( "vout error: can't set colors" );
499 ggiClose( p_vout->p_sys->p_display );
504 /* Set clipping for text */
505 if( ggiSetGCClipping( p_vout->p_sys->p_display, 0, 0,
506 p_vout->p_sys->mode.visible.x,
507 p_vout->p_sys->mode.visible.y ) )
509 intf_ErrMsg( "vout error: can't set clipping" );
510 ggiClose( p_vout->p_sys->p_display );
515 /* FIXME: set palette in 8bpp */
516 p_vout->p_sys->i_bits_per_pixel = p_b[ 0 ]->buffer.plb.pixelformat->depth;
522 /*****************************************************************************
523 * CloseDisplay: close and reset GGI device
524 *****************************************************************************
525 * This function returns all resources allocated by OpenDisplay and restore
526 * the original state of the device.
527 *****************************************************************************/
528 static void CloseDisplay( vout_thread_t *p_vout )
530 /* Restore original mode and close display */
531 ggiClose( p_vout->p_sys->p_display );