1 /*****************************************************************************
2 * ggi.c : GGI plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: ggi.c,v 1.24 2002/07/23 00:39:17 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 = VLC_FOURCC('R','G','B','2');
167 p_vout->output.pf_setpalette = SetPalette;
170 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
172 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
174 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
176 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); 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_pitch = p_b[ 0 ]->buffer.plb.pixelformat->size / 8;
210 p_pic->p->i_lines = p_vout->p_sys->mode.visible.y;
212 p_pic->p->i_pitch = p_b[ 0 ]->buffer.plb.stride;
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->i_visible_pitch = p_b[ 0 ]->buffer.plb.pixelformat->size
219 / 8 * p_vout->p_sys->mode.visible.x;
223 p_pic->p->i_visible_pitch = p_b[ 0 ]->buffer.plb.stride;
228 p_pic->i_status = DESTROYED_PICTURE;
229 p_pic->i_type = DIRECT_PICTURE;
231 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
235 /* Acquire first buffer */
236 if( p_vout->p_sys->b_must_acquire )
238 ggiResourceAcquire( p_b[ 0 ]->resource,
242 /* Listen to the keyboard and the mouse buttons */
243 ggiSetEventMask( p_vout->p_sys->p_display,
244 emKeyboard | emPtrButtonPress | emPtrButtonRelease );
246 /* Set asynchronous display mode -- usually quite faster */
247 ggiAddFlags( p_vout->p_sys->p_display, GGIFLAG_ASYNC );
253 /*****************************************************************************
254 * vout_End: terminate GGI video thread output method
255 *****************************************************************************
256 * Terminate an output method created by vout_Create
257 *****************************************************************************/
258 void vout_End( vout_thread_t *p_vout )
260 #define p_b p_vout->p_sys->pp_buffer
262 if( p_vout->p_sys->b_must_acquire )
264 ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
269 /*****************************************************************************
270 * vout_Destroy: destroy GGI video thread output method
271 *****************************************************************************
272 * Terminate an output method created by vout_Create
273 *****************************************************************************/
274 void vout_Destroy( vout_thread_t *p_vout )
276 CloseDisplay( p_vout );
278 free( p_vout->p_sys );
281 /*****************************************************************************
282 * vout_Manage: handle GGI events
283 *****************************************************************************
284 * This function should be called regularly by video output thread. It returns
285 * a non null value if an error occured.
286 *****************************************************************************/
287 int vout_Manage( vout_thread_t *p_vout )
289 struct timeval tv = { 0, 1000 }; /* 1 millisecond */
293 mask = emKeyboard | emPtrButtonPress | emPtrButtonRelease;
295 ggiEventPoll( p_vout->p_sys->p_display, mask, &tv );
297 while( ggiEventsQueued( p_vout->p_sys->p_display, mask) )
299 ggiEventRead( p_vout->p_sys->p_display, &event, mask);
301 switch( event.any.type )
305 switch( event.key.sym )
310 /* FIXME pass message ! */
311 p_vout->p_vlc->b_die = 1;
319 case evPtrButtonRelease:
321 switch( event.pbutton.button )
323 case GII_PBUTTON_RIGHT:
325 intf_thread_t *p_intf;
326 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
330 p_intf->b_menu_change = 1;
331 vlc_object_release( p_intf );
346 /*****************************************************************************
347 * vout_Render: displays previously rendered output
348 *****************************************************************************/
349 void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
354 /*****************************************************************************
355 * vout_Display: displays previously rendered output
356 *****************************************************************************/
357 void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
359 #define p_b p_vout->p_sys->pp_buffer
360 p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
362 /* Change display frame */
363 if( p_vout->p_sys->b_must_acquire )
365 ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
367 ggiSetDisplayFrame( p_vout->p_sys->p_display,
368 p_b[ p_vout->p_sys->i_index ]->frame );
370 /* Swap buffers and change write frame */
371 p_vout->p_sys->i_index ^= 1;
372 p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
374 if( p_vout->p_sys->b_must_acquire )
376 ggiResourceAcquire( p_b[ p_vout->p_sys->i_index ]->resource,
379 ggiSetWriteFrame( p_vout->p_sys->p_display,
380 p_b[ p_vout->p_sys->i_index ]->frame );
382 /* Flush the output so that it actually displays */
383 ggiFlush( p_vout->p_sys->p_display );
387 /* following functions are local */
389 /*****************************************************************************
390 * OpenDisplay: open and initialize GGI device
391 *****************************************************************************
392 * Open and initialize display according to preferences specified in the vout
394 *****************************************************************************/
395 static int OpenDisplay( vout_thread_t *p_vout )
397 #define p_b p_vout->p_sys->pp_buffer
398 ggi_color col_fg; /* foreground color */
399 ggi_color col_bg; /* background color */
400 int i_index; /* all purposes index */
403 /* Initialize library */
406 msg_Err( p_vout, "cannot initialize GGI library" );
411 psz_display = config_GetPsz( p_vout, "ggi_display" );
413 p_vout->p_sys->p_display = ggiOpen( psz_display, NULL );
414 if( psz_display ) free( psz_display );
416 if( p_vout->p_sys->p_display == NULL )
418 msg_Err( p_vout, "cannot 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 = config_GetInt( p_vout, "width" );
426 p_vout->p_sys->mode.visible.y = config_GetInt( p_vout, "height" );
427 p_vout->p_sys->mode.virt.x = GGI_AUTO;
428 p_vout->p_sys->mode.virt.y = GGI_AUTO;
429 p_vout->p_sys->mode.size.x = GGI_AUTO;
430 p_vout->p_sys->mode.size.y = GGI_AUTO;
431 p_vout->p_sys->mode.graphtype = GT_15BIT; /* minimum usable depth */
432 p_vout->p_sys->mode.dpp.x = GGI_AUTO;
433 p_vout->p_sys->mode.dpp.y = GGI_AUTO;
434 ggiCheckMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode );
436 /* FIXME: Check that returned mode has some minimum properties */
439 if( ggiSetMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode ) )
441 msg_Err( p_vout, "cannot set GGI mode" );
442 ggiClose( p_vout->p_sys->p_display );
447 /* Check buffers properties */
448 p_vout->p_sys->b_must_acquire = 0;
449 for( i_index = 0; i_index < 2; i_index++ )
451 /* Get buffer address */
452 p_vout->p_sys->pp_buffer[ i_index ] =
453 (ggi_directbuffer *)ggiDBGetBuffer( p_vout->p_sys->p_display,
455 if( p_b[ i_index ] == NULL )
457 msg_Err( p_vout, "double buffering is not possible" );
458 ggiClose( p_vout->p_sys->p_display );
463 /* Check buffer properties */
464 if( ! ( p_b[ i_index ]->type & GGI_DB_SIMPLE_PLB )
465 || ( p_b[ i_index ]->page_size != 0 )
466 || ( p_b[ i_index ]->write == NULL )
467 || ( p_b[ i_index ]->noaccess != 0 )
468 || ( p_b[ i_index ]->align != 0 ) )
470 msg_Err( p_vout, "incorrect video memory type" );
471 ggiClose( p_vout->p_sys->p_display );
476 /* Check if buffer needs to be acquired before write */
477 if( ggiResourceMustAcquire( p_b[ i_index ]->resource ) )
479 p_vout->p_sys->b_must_acquire = 1;
483 /* Set graphic context colors */
484 col_fg.r = col_fg.g = col_fg.b = -1;
485 col_bg.r = col_bg.g = col_bg.b = 0;
486 if( ggiSetGCForeground(p_vout->p_sys->p_display,
487 ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
488 ggiSetGCBackground(p_vout->p_sys->p_display,
489 ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
491 msg_Err( p_vout, "cannot set colors" );
492 ggiClose( p_vout->p_sys->p_display );
497 /* Set clipping for text */
498 if( ggiSetGCClipping( p_vout->p_sys->p_display, 0, 0,
499 p_vout->p_sys->mode.visible.x,
500 p_vout->p_sys->mode.visible.y ) )
502 msg_Err( p_vout, "cannot set clipping" );
503 ggiClose( p_vout->p_sys->p_display );
508 /* FIXME: set palette in 8bpp */
509 p_vout->p_sys->i_bits_per_pixel = p_b[ 0 ]->buffer.plb.pixelformat->depth;
515 /*****************************************************************************
516 * CloseDisplay: close and reset GGI device
517 *****************************************************************************
518 * This function returns all resources allocated by OpenDisplay and restore
519 * the original state of the device.
520 *****************************************************************************/
521 static void CloseDisplay( vout_thread_t *p_vout )
523 /* Restore original mode and close display */
524 ggiClose( p_vout->p_sys->p_display );
530 /*****************************************************************************
531 * SetPalette: sets an 8 bpp palette
532 *****************************************************************************/
533 static void SetPalette( vout_thread_t *p_vout, u16 *red, u16 *green, u16 *blue )
535 ggi_color colors[256];
538 /* Fill colors with color information */
539 for( i = 0; i < 256; i++ )
541 colors[ i ].r = red[ i ];
542 colors[ i ].g = green[ i ];
543 colors[ i ].b = blue[ i ];
548 if( ggiSetPalette( p_vout->p_sys->p_display, 0, 256, colors ) < 0 )
550 msg_Err( p_vout, "failed setting palette" );