1 /*****************************************************************************
2 * ggi.c : GGI plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: ggi.c,v 1.14 2002/02/19 00:50:19 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_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 * );
55 static int OpenDisplay ( vout_thread_t * );
56 static void CloseDisplay ( vout_thread_t * );
58 /*****************************************************************************
59 * Building configuration tree
60 *****************************************************************************/
65 SET_DESCRIPTION( "General Graphics Interface video output" )
66 ADD_CAPABILITY( VOUT, 30 )
71 vout_getfunctions( &p_module->p_functions->vout );
74 MODULE_DEACTIVATE_START
75 MODULE_DEACTIVATE_STOP
77 /*****************************************************************************
78 * vout_sys_t: video output GGI method descriptor
79 *****************************************************************************
80 * This structure is part of the video output thread descriptor.
81 * It describes the GGI specific properties of an output thread.
82 *****************************************************************************/
83 typedef struct vout_sys_s
85 /* GGI system informations */
86 ggi_visual_t p_display; /* display device */
88 ggi_mode mode; /* mode descriptor */
91 /* Buffer information */
92 ggi_directbuffer * pp_buffer[2]; /* buffers */
95 boolean_t b_must_acquire; /* must be acquired before writing */
98 /*****************************************************************************
99 * Functions exported as capabilities. They are declared as static so that
100 * we don't pollute the namespace too much.
101 *****************************************************************************/
102 static void vout_getfunctions( function_list_t * p_function_list )
104 p_function_list->functions.vout.pf_create = vout_Create;
105 p_function_list->functions.vout.pf_init = vout_Init;
106 p_function_list->functions.vout.pf_end = vout_End;
107 p_function_list->functions.vout.pf_destroy = vout_Destroy;
108 p_function_list->functions.vout.pf_manage = vout_Manage;
109 p_function_list->functions.vout.pf_render = vout_Render;
110 p_function_list->functions.vout.pf_display = vout_Display;
113 /*****************************************************************************
114 * vout_Create: allocate GGI video thread output method
115 *****************************************************************************
116 * This function allocate and initialize a GGI vout method. It uses some of the
117 * vout properties to choose the correct mode, and change them according to the
118 * mode actually used.
119 *****************************************************************************/
120 int vout_Create( vout_thread_t *p_vout )
122 /* Allocate structure */
123 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
124 if( p_vout->p_sys == NULL )
126 intf_ErrMsg( "vout error: %s", strerror(ENOMEM) );
130 /* Open and initialize device */
131 if( OpenDisplay( p_vout ) )
133 intf_ErrMsg( "vout error: can't initialize GGI display" );
134 free( p_vout->p_sys );
141 /*****************************************************************************
142 * vout_Init: initialize GGI video thread output method
143 *****************************************************************************
144 * This function initialize the GGI display device.
145 *****************************************************************************/
146 int vout_Init( vout_thread_t *p_vout )
151 I_OUTPUTPICTURES = 0;
153 p_vout->output.i_width = p_vout->p_sys->mode.visible.x;
154 p_vout->output.i_height = p_vout->p_sys->mode.visible.y;
155 p_vout->output.i_aspect = p_vout->p_sys->mode.visible.x
157 / p_vout->p_sys->mode.visible.y;
159 switch( p_vout->p_sys->i_bits_per_pixel )
161 case 8: /* FIXME: set the palette */
162 p_vout->output.i_chroma = FOURCC_BI_RGB; break;
164 p_vout->output.i_chroma = FOURCC_RV15; break;
166 p_vout->output.i_chroma = FOURCC_RV16; break;
168 p_vout->output.i_chroma = FOURCC_BI_BITFIELDS; break;
170 p_vout->output.i_chroma = FOURCC_BI_BITFIELDS; break;
172 intf_ErrMsg( "vout error: unknown screen depth" );
178 /* Find an empty picture slot */
179 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
181 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
183 p_pic = p_vout->p_picture + i_index;
193 #define p_b p_vout->p_sys->pp_buffer
194 /* We know the chroma, allocate a buffer which will be used
195 * directly by the decoder */
196 p_vout->p_sys->i_index = 0;
197 p_pic->p->p_pixels = p_b[ 0 ]->write;
198 p_pic->p->i_pixel_bytes = p_b[ 0 ]->buffer.plb.pixelformat->size / 8;
199 p_pic->p->i_lines = p_vout->p_sys->mode.visible.y;
201 if( p_b[ 0 ]->buffer.plb.pixelformat->size / 8
202 * p_vout->p_sys->mode.visible.x
203 != p_b[ 0 ]->buffer.plb.stride )
205 p_pic->p->b_margin = 1;
206 p_pic->p->b_hidden = 1;
207 p_pic->p->i_pitch = p_b[ 0 ]->buffer.plb.stride;
208 p_pic->p->i_visible_bytes = p_b[ 0 ]->buffer.plb.pixelformat->size
209 / 8 * p_vout->p_sys->mode.visible.x;
213 p_pic->p->b_margin = 0;
214 p_pic->p->i_pitch = p_b[ 0 ]->buffer.plb.stride;
217 /* Only useful for bits_per_pixel != 8 */
218 p_pic->p->i_red_mask = p_b[ 0 ]->buffer.plb.pixelformat->red_mask;
219 p_pic->p->i_green_mask = p_b[ 0 ]->buffer.plb.pixelformat->green_mask;
220 p_pic->p->i_blue_mask = p_b[ 0 ]->buffer.plb.pixelformat->blue_mask;
224 p_pic->i_status = DESTROYED_PICTURE;
225 p_pic->i_type = DIRECT_PICTURE;
227 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
231 /* Acquire first buffer */
232 if( p_vout->p_sys->b_must_acquire )
234 ggiResourceAcquire( p_b[ 0 ]->resource,
238 /* Listen to the keyboard and the mouse buttons */
239 ggiSetEventMask( p_vout->p_sys->p_display,
240 emKeyboard | emPtrButtonPress | emPtrButtonRelease );
242 /* Set asynchronous display mode -- usually quite faster */
243 ggiAddFlags( p_vout->p_sys->p_display, GGIFLAG_ASYNC );
249 /*****************************************************************************
250 * vout_End: terminate GGI video thread output method
251 *****************************************************************************
252 * Terminate an output method created by vout_Create
253 *****************************************************************************/
254 void vout_End( vout_thread_t *p_vout )
256 #define p_b p_vout->p_sys->pp_buffer
258 if( p_vout->p_sys->b_must_acquire )
260 ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
265 /*****************************************************************************
266 * vout_Destroy: destroy GGI video thread output method
267 *****************************************************************************
268 * Terminate an output method created by vout_Create
269 *****************************************************************************/
270 void vout_Destroy( vout_thread_t *p_vout )
272 CloseDisplay( p_vout );
274 free( p_vout->p_sys );
277 /*****************************************************************************
278 * vout_Manage: handle GGI events
279 *****************************************************************************
280 * This function should be called regularly by video output thread. It returns
281 * a non null value if an error occured.
282 *****************************************************************************/
283 int vout_Manage( vout_thread_t *p_vout )
285 struct timeval tv = { 0, 1000 }; /* 1 millisecond */
289 mask = emKeyboard | emPtrButtonPress | emPtrButtonRelease;
291 ggiEventPoll( p_vout->p_sys->p_display, mask, &tv );
293 while( ggiEventsQueued( p_vout->p_sys->p_display, mask) )
295 ggiEventRead( p_vout->p_sys->p_display, &event, mask);
297 switch( event.any.type )
301 switch( event.key.sym )
306 /* FIXME pass message ! */
307 p_main->p_intf->b_die = 1;
315 case evPtrButtonRelease:
317 switch( event.pbutton.button )
319 case GII_PBUTTON_RIGHT:
320 /* FIXME: need locking ! */
321 p_main->p_intf->b_menu_change = 1;
334 /*****************************************************************************
335 * vout_Render: displays previously rendered output
336 *****************************************************************************/
337 void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
342 /*****************************************************************************
343 * vout_Display: displays previously rendered output
344 *****************************************************************************/
345 void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
347 #define p_b p_vout->p_sys->pp_buffer
348 p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
350 /* Change display frame */
351 if( p_vout->p_sys->b_must_acquire )
353 ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
355 ggiSetDisplayFrame( p_vout->p_sys->p_display,
356 p_b[ p_vout->p_sys->i_index ]->frame );
358 /* Swap buffers and change write frame */
359 p_vout->p_sys->i_index ^= 1;
360 p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
362 if( p_vout->p_sys->b_must_acquire )
364 ggiResourceAcquire( p_b[ p_vout->p_sys->i_index ]->resource,
367 ggiSetWriteFrame( p_vout->p_sys->p_display,
368 p_b[ p_vout->p_sys->i_index ]->frame );
370 /* Flush the output so that it actually displays */
371 ggiFlush( p_vout->p_sys->p_display );
375 /* following functions are local */
377 /*****************************************************************************
378 * OpenDisplay: open and initialize GGI device
379 *****************************************************************************
380 * Open and initialize display according to preferences specified in the vout
382 *****************************************************************************/
383 static int OpenDisplay( vout_thread_t *p_vout )
385 #define p_b p_vout->p_sys->pp_buffer
386 ggi_color col_fg; /* foreground color */
387 ggi_color col_bg; /* background color */
388 int i_index; /* all purposes index */
391 /* Initialize library */
394 intf_ErrMsg( "vout error: can't initialize GGI library" );
399 psz_display = main_GetPszVariable( VOUT_DISPLAY_VAR, NULL );
401 p_vout->p_sys->p_display = ggiOpen( psz_display, NULL );
403 if( p_vout->p_sys->p_display == NULL )
405 intf_ErrMsg( "vout error: can't open GGI default display" );
410 /* Find most appropriate mode */
411 p_vout->p_sys->mode.frames = 2; /* 2 buffers */
412 p_vout->p_sys->mode.visible.x = main_GetIntVariable( VOUT_WIDTH_VAR,
413 VOUT_WIDTH_DEFAULT );
414 p_vout->p_sys->mode.visible.y = main_GetIntVariable( VOUT_HEIGHT_VAR,
415 VOUT_HEIGHT_DEFAULT );
416 p_vout->p_sys->mode.virt.x = GGI_AUTO;
417 p_vout->p_sys->mode.virt.y = GGI_AUTO;
418 p_vout->p_sys->mode.size.x = GGI_AUTO;
419 p_vout->p_sys->mode.size.y = GGI_AUTO;
420 p_vout->p_sys->mode.graphtype = GT_15BIT; /* minimum usable depth */
421 p_vout->p_sys->mode.dpp.x = GGI_AUTO;
422 p_vout->p_sys->mode.dpp.y = GGI_AUTO;
423 ggiCheckMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode );
425 /* FIXME: Check that returned mode has some minimum properties */
428 if( ggiSetMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode ) )
430 intf_ErrMsg( "vout error: can't set GGI mode" );
431 ggiClose( p_vout->p_sys->p_display );
436 /* Check buffers properties */
437 p_vout->p_sys->b_must_acquire = 0;
438 for( i_index = 0; i_index < 2; i_index++ )
440 /* Get buffer address */
441 p_vout->p_sys->pp_buffer[ i_index ] =
442 (ggi_directbuffer *)ggiDBGetBuffer( p_vout->p_sys->p_display,
444 if( p_b[ i_index ] == NULL )
446 intf_ErrMsg( "vout error: double buffering is not possible" );
447 ggiClose( p_vout->p_sys->p_display );
452 /* Check buffer properties */
453 if( ! ( p_b[ i_index ]->type & GGI_DB_SIMPLE_PLB )
454 || ( p_b[ i_index ]->page_size != 0 )
455 || ( p_b[ i_index ]->write == NULL )
456 || ( p_b[ i_index ]->noaccess != 0 )
457 || ( p_b[ i_index ]->align != 0 ) )
459 intf_ErrMsg( "vout error: incorrect video memory type" );
460 ggiClose( p_vout->p_sys->p_display );
465 /* Check if buffer needs to be acquired before write */
466 if( ggiResourceMustAcquire( p_b[ i_index ]->resource ) )
468 p_vout->p_sys->b_must_acquire = 1;
472 /* Set graphic context colors */
473 col_fg.r = col_fg.g = col_fg.b = -1;
474 col_bg.r = col_bg.g = col_bg.b = 0;
475 if( ggiSetGCForeground(p_vout->p_sys->p_display,
476 ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
477 ggiSetGCBackground(p_vout->p_sys->p_display,
478 ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
480 intf_ErrMsg( "vout error: can't set colors" );
481 ggiClose( p_vout->p_sys->p_display );
486 /* Set clipping for text */
487 if( ggiSetGCClipping( p_vout->p_sys->p_display, 0, 0,
488 p_vout->p_sys->mode.visible.x,
489 p_vout->p_sys->mode.visible.y ) )
491 intf_ErrMsg( "vout error: can't set clipping" );
492 ggiClose( p_vout->p_sys->p_display );
497 /* FIXME: set palette in 8bpp */
498 p_vout->p_sys->i_bits_per_pixel = p_b[ 0 ]->buffer.plb.pixelformat->depth;
504 /*****************************************************************************
505 * CloseDisplay: close and reset GGI device
506 *****************************************************************************
507 * This function returns all resources allocated by OpenDisplay and restore
508 * the original state of the device.
509 *****************************************************************************/
510 static void CloseDisplay( vout_thread_t *p_vout )
512 /* Restore original mode and close display */
513 ggiClose( p_vout->p_sys->p_display );