1 /*****************************************************************************
2 * ggi.c : GGI plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: ggi.c,v 1.21 2002/06/01 12:31:59 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 */
38 /*****************************************************************************
40 *****************************************************************************/
41 static void vout_getfunctions( function_list_t * p_function_list );
43 static int vout_Create ( vout_thread_t * );
44 static int vout_Init ( vout_thread_t * );
45 static void vout_End ( vout_thread_t * );
46 static void vout_Destroy ( vout_thread_t * );
47 static int vout_Manage ( vout_thread_t * );
48 static void vout_Render ( vout_thread_t *, picture_t * );
49 static void vout_Display ( vout_thread_t *, picture_t * );
51 static int OpenDisplay ( vout_thread_t * );
52 static void CloseDisplay ( vout_thread_t * );
53 static void SetPalette ( vout_thread_t *, u16 *, u16 *, u16 * );
55 /*****************************************************************************
56 * Building configuration tree
57 *****************************************************************************/
58 #define DISPLAY_TEXT N_("X11 display name")
59 #define DISPLAY_LONGTEXT N_("Specify the X11 hardware display you want to use."\
60 "\nBy default vlc will use the value of the DISPLAY"\
61 " environment variable.")
64 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
65 ADD_STRING ( "ggi_display", NULL, NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT )
69 SET_DESCRIPTION( "General Graphics Interface video output" )
70 ADD_CAPABILITY( VOUT, 30 )
74 vout_getfunctions( &p_module->p_functions->vout );
77 MODULE_DEACTIVATE_START
78 MODULE_DEACTIVATE_STOP
80 /*****************************************************************************
81 * vout_sys_t: video output GGI method descriptor
82 *****************************************************************************
83 * This structure is part of the video output thread descriptor.
84 * It describes the GGI specific properties of an output thread.
85 *****************************************************************************/
88 /* GGI system informations */
89 ggi_visual_t p_display; /* display device */
91 ggi_mode mode; /* mode descriptor */
94 /* Buffer information */
95 ggi_directbuffer * pp_buffer[2]; /* buffers */
98 vlc_bool_t b_must_acquire; /* must be acquired before writing */
101 /*****************************************************************************
102 * Functions exported as capabilities. They are declared as static so that
103 * we don't pollute the namespace too much.
104 *****************************************************************************/
105 static void vout_getfunctions( function_list_t * p_function_list )
107 p_function_list->functions.vout.pf_create = vout_Create;
108 p_function_list->functions.vout.pf_init = vout_Init;
109 p_function_list->functions.vout.pf_end = vout_End;
110 p_function_list->functions.vout.pf_destroy = vout_Destroy;
111 p_function_list->functions.vout.pf_manage = vout_Manage;
112 p_function_list->functions.vout.pf_render = vout_Render;
113 p_function_list->functions.vout.pf_display = vout_Display;
116 /*****************************************************************************
117 * vout_Create: allocate GGI video thread output method
118 *****************************************************************************
119 * This function allocate and initialize a GGI vout method. It uses some of the
120 * vout properties to choose the correct mode, and change them according to the
121 * mode actually used.
122 *****************************************************************************/
123 int vout_Create( vout_thread_t *p_vout )
125 /* Allocate structure */
126 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
127 if( p_vout->p_sys == NULL )
129 msg_Err( p_vout, "out of memory" );
133 /* Open and initialize device */
134 if( OpenDisplay( p_vout ) )
136 msg_Err( p_vout, "cannot initialize GGI display" );
137 free( p_vout->p_sys );
144 /*****************************************************************************
145 * vout_Init: initialize GGI video thread output method
146 *****************************************************************************
147 * This function initialize the GGI display device.
148 *****************************************************************************/
149 int vout_Init( vout_thread_t *p_vout )
151 #define p_b p_vout->p_sys->pp_buffer
155 I_OUTPUTPICTURES = 0;
157 p_vout->output.i_width = p_vout->p_sys->mode.visible.x;
158 p_vout->output.i_height = p_vout->p_sys->mode.visible.y;
159 p_vout->output.i_aspect = p_vout->p_sys->mode.visible.x
161 / p_vout->p_sys->mode.visible.y;
163 switch( p_vout->p_sys->i_bits_per_pixel )
166 p_vout->output.i_chroma = FOURCC_RGB2;
167 p_vout->output.pf_setpalette = SetPalette;
170 p_vout->output.i_chroma = FOURCC_RV15; break;
172 p_vout->output.i_chroma = FOURCC_RV16; break;
174 p_vout->output.i_chroma = FOURCC_RV24; break;
176 p_vout->output.i_chroma = FOURCC_RV32; break;
178 msg_Err( p_vout, "unknown screen depth %i",
179 p_vout->p_sys->i_bits_per_pixel );
183 /* Only useful for bits_per_pixel != 8 */
184 p_vout->output.i_rmask = p_b[ 0 ]->buffer.plb.pixelformat->red_mask;
185 p_vout->output.i_gmask = p_b[ 0 ]->buffer.plb.pixelformat->green_mask;
186 p_vout->output.i_bmask = p_b[ 0 ]->buffer.plb.pixelformat->blue_mask;
190 /* Find an empty picture slot */
191 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
193 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
195 p_pic = p_vout->p_picture + i_index;
205 /* We know the chroma, allocate a buffer which will be used
206 * directly by the decoder */
207 p_vout->p_sys->i_index = 0;
208 p_pic->p->p_pixels = p_b[ 0 ]->write;
209 p_pic->p->i_pixel_bytes = p_b[ 0 ]->buffer.plb.pixelformat->size / 8;
210 p_pic->p->i_lines = p_vout->p_sys->mode.visible.y;
212 if( p_b[ 0 ]->buffer.plb.pixelformat->size / 8
213 * p_vout->p_sys->mode.visible.x
214 != p_b[ 0 ]->buffer.plb.stride )
216 p_pic->p->b_margin = 1;
217 p_pic->p->b_hidden = 1;
218 p_pic->p->i_pitch = p_b[ 0 ]->buffer.plb.stride;
219 p_pic->p->i_visible_bytes = p_b[ 0 ]->buffer.plb.pixelformat->size
220 / 8 * p_vout->p_sys->mode.visible.x;
224 p_pic->p->b_margin = 0;
225 p_pic->p->i_pitch = p_b[ 0 ]->buffer.plb.stride;
230 p_pic->i_status = DESTROYED_PICTURE;
231 p_pic->i_type = DIRECT_PICTURE;
233 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
237 /* Acquire first buffer */
238 if( p_vout->p_sys->b_must_acquire )
240 ggiResourceAcquire( p_b[ 0 ]->resource,
244 /* Listen to the keyboard and the mouse buttons */
245 ggiSetEventMask( p_vout->p_sys->p_display,
246 emKeyboard | emPtrButtonPress | emPtrButtonRelease );
248 /* Set asynchronous display mode -- usually quite faster */
249 ggiAddFlags( p_vout->p_sys->p_display, GGIFLAG_ASYNC );
255 /*****************************************************************************
256 * vout_End: terminate GGI video thread output method
257 *****************************************************************************
258 * Terminate an output method created by vout_Create
259 *****************************************************************************/
260 void vout_End( vout_thread_t *p_vout )
262 #define p_b p_vout->p_sys->pp_buffer
264 if( p_vout->p_sys->b_must_acquire )
266 ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
271 /*****************************************************************************
272 * vout_Destroy: destroy GGI video thread output method
273 *****************************************************************************
274 * Terminate an output method created by vout_Create
275 *****************************************************************************/
276 void vout_Destroy( vout_thread_t *p_vout )
278 CloseDisplay( p_vout );
280 free( p_vout->p_sys );
283 /*****************************************************************************
284 * vout_Manage: handle GGI events
285 *****************************************************************************
286 * This function should be called regularly by video output thread. It returns
287 * a non null value if an error occured.
288 *****************************************************************************/
289 int vout_Manage( vout_thread_t *p_vout )
291 struct timeval tv = { 0, 1000 }; /* 1 millisecond */
295 mask = emKeyboard | emPtrButtonPress | emPtrButtonRelease;
297 ggiEventPoll( p_vout->p_sys->p_display, mask, &tv );
299 while( ggiEventsQueued( p_vout->p_sys->p_display, mask) )
301 ggiEventRead( p_vout->p_sys->p_display, &event, mask);
303 switch( event.any.type )
307 switch( event.key.sym )
312 /* FIXME pass message ! */
313 p_vout->p_vlc->b_die = 1;
321 case evPtrButtonRelease:
323 switch( event.pbutton.button )
325 case GII_PBUTTON_RIGHT:
327 intf_thread_t *p_intf;
328 p_intf = vlc_object_find( p_vout->p_vlc,
329 VLC_OBJECT_INTF, FIND_CHILD );
332 p_intf->b_menu_change = 1;
333 vlc_object_release( p_intf );
348 /*****************************************************************************
349 * vout_Render: displays previously rendered output
350 *****************************************************************************/
351 void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
356 /*****************************************************************************
357 * vout_Display: displays previously rendered output
358 *****************************************************************************/
359 void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
361 #define p_b p_vout->p_sys->pp_buffer
362 p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
364 /* Change display frame */
365 if( p_vout->p_sys->b_must_acquire )
367 ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
369 ggiSetDisplayFrame( p_vout->p_sys->p_display,
370 p_b[ p_vout->p_sys->i_index ]->frame );
372 /* Swap buffers and change write frame */
373 p_vout->p_sys->i_index ^= 1;
374 p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
376 if( p_vout->p_sys->b_must_acquire )
378 ggiResourceAcquire( p_b[ p_vout->p_sys->i_index ]->resource,
381 ggiSetWriteFrame( p_vout->p_sys->p_display,
382 p_b[ p_vout->p_sys->i_index ]->frame );
384 /* Flush the output so that it actually displays */
385 ggiFlush( p_vout->p_sys->p_display );
389 /* following functions are local */
391 /*****************************************************************************
392 * OpenDisplay: open and initialize GGI device
393 *****************************************************************************
394 * Open and initialize display according to preferences specified in the vout
396 *****************************************************************************/
397 static int OpenDisplay( vout_thread_t *p_vout )
399 #define p_b p_vout->p_sys->pp_buffer
400 ggi_color col_fg; /* foreground color */
401 ggi_color col_bg; /* background color */
402 int i_index; /* all purposes index */
405 /* Initialize library */
408 msg_Err( p_vout, "cannot initialize GGI library" );
413 psz_display = config_GetPsz( p_vout, "ggi_display" );
415 p_vout->p_sys->p_display = ggiOpen( psz_display, NULL );
416 if( psz_display ) free( psz_display );
418 if( p_vout->p_sys->p_display == NULL )
420 msg_Err( p_vout, "cannot open GGI default display" );
425 /* Find most appropriate mode */
426 p_vout->p_sys->mode.frames = 2; /* 2 buffers */
427 p_vout->p_sys->mode.visible.x = config_GetInt( p_vout, "width" );
428 p_vout->p_sys->mode.visible.y = config_GetInt( p_vout, "height" );
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 msg_Err( p_vout, "cannot 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 msg_Err( p_vout, "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 msg_Err( p_vout, "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 /* Set graphic context colors */
486 col_fg.r = col_fg.g = col_fg.b = -1;
487 col_bg.r = col_bg.g = col_bg.b = 0;
488 if( ggiSetGCForeground(p_vout->p_sys->p_display,
489 ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
490 ggiSetGCBackground(p_vout->p_sys->p_display,
491 ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
493 msg_Err( p_vout, "cannot set colors" );
494 ggiClose( p_vout->p_sys->p_display );
499 /* Set clipping for text */
500 if( ggiSetGCClipping( p_vout->p_sys->p_display, 0, 0,
501 p_vout->p_sys->mode.visible.x,
502 p_vout->p_sys->mode.visible.y ) )
504 msg_Err( p_vout, "cannot set clipping" );
505 ggiClose( p_vout->p_sys->p_display );
510 /* FIXME: set palette in 8bpp */
511 p_vout->p_sys->i_bits_per_pixel = p_b[ 0 ]->buffer.plb.pixelformat->depth;
517 /*****************************************************************************
518 * CloseDisplay: close and reset GGI device
519 *****************************************************************************
520 * This function returns all resources allocated by OpenDisplay and restore
521 * the original state of the device.
522 *****************************************************************************/
523 static void CloseDisplay( vout_thread_t *p_vout )
525 /* Restore original mode and close display */
526 ggiClose( p_vout->p_sys->p_display );
532 /*****************************************************************************
533 * SetPalette: sets an 8 bpp palette
534 *****************************************************************************/
535 static void SetPalette( vout_thread_t *p_vout, u16 *red, u16 *green, u16 *blue )
537 ggi_color colors[256];
540 /* Fill colors with color information */
541 for( i = 0; i < 256; i++ )
543 colors[ i ].r = red[ i ];
544 colors[ i ].g = green[ i ];
545 colors[ i ].b = blue[ i ];
550 if( ggiSetPalette( p_vout->p_sys->p_display, 0, 256, colors ) < 0 )
552 msg_Err( p_vout, "failed setting palette" );