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 $
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 * );
57 static void SetPalette ( vout_thread_t *, u16 *, u16 *, u16 * );
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.")
68 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
69 ADD_STRING ( "ggi_display", NULL, NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT )
73 SET_DESCRIPTION( "General Graphics Interface video output" )
74 ADD_CAPABILITY( VOUT, 30 )
79 vout_getfunctions( &p_module->p_functions->vout );
82 MODULE_DEACTIVATE_START
83 MODULE_DEACTIVATE_STOP
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
93 /* GGI system informations */
94 ggi_visual_t p_display; /* display device */
96 ggi_mode mode; /* mode descriptor */
99 /* Buffer information */
100 ggi_directbuffer * pp_buffer[2]; /* buffers */
103 boolean_t b_must_acquire; /* must be acquired before writing */
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 )
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;
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 )
130 /* Allocate structure */
131 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
132 if( p_vout->p_sys == NULL )
134 intf_ErrMsg( "vout error: %s", strerror(ENOMEM) );
138 /* Open and initialize device */
139 if( OpenDisplay( p_vout ) )
141 intf_ErrMsg( "vout error: can't initialize GGI display" );
142 free( p_vout->p_sys );
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 )
156 #define p_b p_vout->p_sys->pp_buffer
160 I_OUTPUTPICTURES = 0;
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
166 / p_vout->p_sys->mode.visible.y;
168 switch( p_vout->p_sys->i_bits_per_pixel )
171 p_vout->output.i_chroma = FOURCC_RGB2;
172 p_vout->output.pf_setpalette = SetPalette;
175 p_vout->output.i_chroma = FOURCC_RV15; break;
177 p_vout->output.i_chroma = FOURCC_RV16; break;
179 p_vout->output.i_chroma = FOURCC_RV24; break;
181 p_vout->output.i_chroma = FOURCC_RV32; break;
183 intf_ErrMsg( "vout error: unknown screen depth" );
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;
194 /* Find an empty picture slot */
195 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
197 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
199 p_pic = p_vout->p_picture + i_index;
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;
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 )
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;
228 p_pic->p->b_margin = 0;
229 p_pic->p->i_pitch = p_b[ 0 ]->buffer.plb.stride;
234 p_pic->i_status = DESTROYED_PICTURE;
235 p_pic->i_type = DIRECT_PICTURE;
237 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
241 /* Acquire first buffer */
242 if( p_vout->p_sys->b_must_acquire )
244 ggiResourceAcquire( p_b[ 0 ]->resource,
248 /* Listen to the keyboard and the mouse buttons */
249 ggiSetEventMask( p_vout->p_sys->p_display,
250 emKeyboard | emPtrButtonPress | emPtrButtonRelease );
252 /* Set asynchronous display mode -- usually quite faster */
253 ggiAddFlags( p_vout->p_sys->p_display, GGIFLAG_ASYNC );
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 )
266 #define p_b p_vout->p_sys->pp_buffer
268 if( p_vout->p_sys->b_must_acquire )
270 ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
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 )
282 CloseDisplay( p_vout );
284 free( p_vout->p_sys );
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 )
295 struct timeval tv = { 0, 1000 }; /* 1 millisecond */
299 mask = emKeyboard | emPtrButtonPress | emPtrButtonRelease;
301 ggiEventPoll( p_vout->p_sys->p_display, mask, &tv );
303 while( ggiEventsQueued( p_vout->p_sys->p_display, mask) )
305 ggiEventRead( p_vout->p_sys->p_display, &event, mask);
307 switch( event.any.type )
311 switch( event.key.sym )
316 /* FIXME pass message ! */
317 p_main->p_intf->b_die = 1;
325 case evPtrButtonRelease:
327 switch( event.pbutton.button )
329 case GII_PBUTTON_RIGHT:
330 /* FIXME: need locking ! */
331 p_main->p_intf->b_menu_change = 1;
344 /*****************************************************************************
345 * vout_Render: displays previously rendered output
346 *****************************************************************************/
347 void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
352 /*****************************************************************************
353 * vout_Display: displays previously rendered output
354 *****************************************************************************/
355 void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
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;
360 /* Change display frame */
361 if( p_vout->p_sys->b_must_acquire )
363 ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
365 ggiSetDisplayFrame( p_vout->p_sys->p_display,
366 p_b[ p_vout->p_sys->i_index ]->frame );
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;
372 if( p_vout->p_sys->b_must_acquire )
374 ggiResourceAcquire( p_b[ p_vout->p_sys->i_index ]->resource,
377 ggiSetWriteFrame( p_vout->p_sys->p_display,
378 p_b[ p_vout->p_sys->i_index ]->frame );
380 /* Flush the output so that it actually displays */
381 ggiFlush( p_vout->p_sys->p_display );
385 /* following functions are local */
387 /*****************************************************************************
388 * OpenDisplay: open and initialize GGI device
389 *****************************************************************************
390 * Open and initialize display according to preferences specified in the vout
392 *****************************************************************************/
393 static int OpenDisplay( vout_thread_t *p_vout )
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 */
401 /* Initialize library */
404 intf_ErrMsg( "vout error: can't initialize GGI library" );
409 psz_display = config_GetPszVariable( "ggi_display" );
411 p_vout->p_sys->p_display = ggiOpen( psz_display, NULL );
412 if( psz_display ) free( psz_display );
414 if( p_vout->p_sys->p_display == NULL )
416 intf_ErrMsg( "vout error: can't open GGI default display" );
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 );
434 /* FIXME: Check that returned mode has some minimum properties */
437 if( ggiSetMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode ) )
439 intf_ErrMsg( "vout error: can't set GGI mode" );
440 ggiClose( p_vout->p_sys->p_display );
445 /* Check buffers properties */
446 p_vout->p_sys->b_must_acquire = 0;
447 for( i_index = 0; i_index < 2; i_index++ )
449 /* Get buffer address */
450 p_vout->p_sys->pp_buffer[ i_index ] =
451 (ggi_directbuffer *)ggiDBGetBuffer( p_vout->p_sys->p_display,
453 if( p_b[ i_index ] == NULL )
455 intf_ErrMsg( "vout error: double buffering is not possible" );
456 ggiClose( p_vout->p_sys->p_display );
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 ) )
468 intf_ErrMsg( "vout error: incorrect video memory type" );
469 ggiClose( p_vout->p_sys->p_display );
474 /* Check if buffer needs to be acquired before write */
475 if( ggiResourceMustAcquire( p_b[ i_index ]->resource ) )
477 p_vout->p_sys->b_must_acquire = 1;
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)) )
489 intf_ErrMsg( "vout error: can't set colors" );
490 ggiClose( p_vout->p_sys->p_display );
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 ) )
500 intf_ErrMsg( "vout error: can't set clipping" );
501 ggiClose( p_vout->p_sys->p_display );
506 /* FIXME: set palette in 8bpp */
507 p_vout->p_sys->i_bits_per_pixel = p_b[ 0 ]->buffer.plb.pixelformat->depth;
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 )
521 /* Restore original mode and close display */
522 ggiClose( p_vout->p_sys->p_display );
528 /*****************************************************************************
529 * SetPalette: sets an 8 bpp palette
530 *****************************************************************************/
531 static void SetPalette( vout_thread_t *p_vout, u16 *red, u16 *green, u16 *blue )
533 ggi_color colors[256];
536 /* Fill colors with color information */
537 for( i = 0; i < 256; i++ )
539 colors[ i ].r = red[ i ];
540 colors[ i ].g = green[ i ];
541 colors[ i ].b = blue[ i ];
546 if( ggiSetPalette( p_vout->p_sys->p_display, 0, 256, colors ) < 0 )
548 intf_ErrMsg( "vout error: failed setting palette" );