1 /*****************************************************************************
2 * ggi.c : GGI plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: ggi.c,v 1.1 2002/08/13 11:59:36 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 int Create ( vlc_object_t * );
42 static void Destroy ( vlc_object_t * );
44 static int Init ( vout_thread_t * );
45 static void End ( vout_thread_t * );
46 static int Manage ( vout_thread_t * );
47 static void Display ( vout_thread_t *, picture_t * );
49 static int OpenDisplay ( vout_thread_t * );
50 static void CloseDisplay ( vout_thread_t * );
51 static void SetPalette ( vout_thread_t *, u16 *, u16 *, u16 * );
53 /*****************************************************************************
55 *****************************************************************************/
56 #define DISPLAY_TEXT N_("X11 display name")
57 #define DISPLAY_LONGTEXT N_("Specify the X11 hardware display you want to use."\
58 "\nBy default vlc will use the value of the DISPLAY"\
59 " environment variable.")
62 add_category_hint( N_("Miscellaneous"), NULL );
63 add_string( "ggi-display", NULL, NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT );
64 set_description( "General Graphics Interface video output" );
65 set_capability( "video output", 30 );
66 set_callbacks( Create, Destroy );
69 /*****************************************************************************
70 * vout_sys_t: video output GGI method descriptor
71 *****************************************************************************
72 * This structure is part of the video output thread descriptor.
73 * It describes the GGI specific properties of an output thread.
74 *****************************************************************************/
77 /* GGI system informations */
78 ggi_visual_t p_display; /* display device */
80 ggi_mode mode; /* mode descriptor */
83 /* Buffer information */
84 ggi_directbuffer * pp_buffer[2]; /* buffers */
87 vlc_bool_t b_must_acquire; /* must be acquired before writing */
90 /*****************************************************************************
91 * Create: allocate GGI video thread output method
92 *****************************************************************************
93 * This function allocate and initialize a GGI vout method. It uses some of the
94 * vout properties to choose the correct mode, and change them according to the
96 *****************************************************************************/
97 static int Create( vlc_object_t *p_this )
99 vout_thread_t *p_vout = (vout_thread_t *)p_this;
101 /* Allocate structure */
102 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
103 if( p_vout->p_sys == NULL )
105 msg_Err( p_vout, "out of memory" );
109 /* Open and initialize device */
110 if( OpenDisplay( p_vout ) )
112 msg_Err( p_vout, "cannot initialize GGI display" );
113 free( p_vout->p_sys );
117 p_vout->pf_init = Init;
118 p_vout->pf_end = End;
119 p_vout->pf_manage = Manage;
120 p_vout->pf_render = NULL;
121 p_vout->pf_display = Display;
126 /*****************************************************************************
127 * Init: initialize GGI video thread output method
128 *****************************************************************************
129 * This function initialize the GGI display device.
130 *****************************************************************************/
131 static int Init( vout_thread_t *p_vout )
133 #define p_b p_vout->p_sys->pp_buffer
137 I_OUTPUTPICTURES = 0;
139 p_vout->output.i_width = p_vout->p_sys->mode.visible.x;
140 p_vout->output.i_height = p_vout->p_sys->mode.visible.y;
141 p_vout->output.i_aspect = p_vout->p_sys->mode.visible.x
143 / p_vout->p_sys->mode.visible.y;
145 switch( p_vout->p_sys->i_bits_per_pixel )
148 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
149 p_vout->output.pf_setpalette = SetPalette;
152 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
154 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
156 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
158 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
160 msg_Err( p_vout, "unknown screen depth %i",
161 p_vout->p_sys->i_bits_per_pixel );
165 /* Only useful for bits_per_pixel != 8 */
166 p_vout->output.i_rmask = p_b[ 0 ]->buffer.plb.pixelformat->red_mask;
167 p_vout->output.i_gmask = p_b[ 0 ]->buffer.plb.pixelformat->green_mask;
168 p_vout->output.i_bmask = p_b[ 0 ]->buffer.plb.pixelformat->blue_mask;
172 /* Find an empty picture slot */
173 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
175 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
177 p_pic = p_vout->p_picture + i_index;
187 /* We know the chroma, allocate a buffer which will be used
188 * directly by the decoder */
189 p_vout->p_sys->i_index = 0;
190 p_pic->p->p_pixels = p_b[ 0 ]->write;
191 p_pic->p->i_pixel_pitch = p_b[ 0 ]->buffer.plb.pixelformat->size / 8;
192 p_pic->p->i_lines = p_vout->p_sys->mode.visible.y;
194 p_pic->p->i_pitch = p_b[ 0 ]->buffer.plb.stride;
196 if( p_b[ 0 ]->buffer.plb.pixelformat->size / 8
197 * p_vout->p_sys->mode.visible.x
198 != p_b[ 0 ]->buffer.plb.stride )
200 p_pic->p->i_visible_pitch = p_b[ 0 ]->buffer.plb.pixelformat->size
201 / 8 * p_vout->p_sys->mode.visible.x;
205 p_pic->p->i_visible_pitch = p_b[ 0 ]->buffer.plb.stride;
210 p_pic->i_status = DESTROYED_PICTURE;
211 p_pic->i_type = DIRECT_PICTURE;
213 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
217 /* Acquire first buffer */
218 if( p_vout->p_sys->b_must_acquire )
220 ggiResourceAcquire( p_b[ 0 ]->resource,
224 /* Listen to the keyboard and the mouse buttons */
225 ggiSetEventMask( p_vout->p_sys->p_display,
226 emKeyboard | emPtrButtonPress | emPtrButtonRelease );
228 /* Set asynchronous display mode -- usually quite faster */
229 ggiAddFlags( p_vout->p_sys->p_display, GGIFLAG_ASYNC );
235 /*****************************************************************************
236 * End: terminate GGI video thread output method
237 *****************************************************************************
238 * Terminate an output method created by Create
239 *****************************************************************************/
240 static void End( vout_thread_t *p_vout )
242 #define p_b p_vout->p_sys->pp_buffer
244 if( p_vout->p_sys->b_must_acquire )
246 ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
251 /*****************************************************************************
252 * Destroy: destroy GGI video thread output method
253 *****************************************************************************
254 * Terminate an output method created by Create
255 *****************************************************************************/
256 static void Destroy( vlc_object_t *p_this )
258 vout_thread_t *p_vout = (vout_thread_t *)p_this;
260 CloseDisplay( p_vout );
262 free( p_vout->p_sys );
265 /*****************************************************************************
266 * Manage: handle GGI events
267 *****************************************************************************
268 * This function should be called regularly by video output thread. It returns
269 * a non null value if an error occured.
270 *****************************************************************************/
271 static int Manage( vout_thread_t *p_vout )
273 struct timeval tv = { 0, 1000 }; /* 1 millisecond */
277 mask = emKeyboard | emPtrButtonPress | emPtrButtonRelease;
279 ggiEventPoll( p_vout->p_sys->p_display, mask, &tv );
281 while( ggiEventsQueued( p_vout->p_sys->p_display, mask) )
283 ggiEventRead( p_vout->p_sys->p_display, &event, mask);
285 switch( event.any.type )
289 switch( event.key.sym )
294 /* FIXME pass message ! */
295 p_vout->p_vlc->b_die = 1;
303 case evPtrButtonRelease:
305 switch( event.pbutton.button )
307 case GII_PBUTTON_RIGHT:
309 intf_thread_t *p_intf;
310 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
314 p_intf->b_menu_change = 1;
315 vlc_object_release( p_intf );
330 /*****************************************************************************
331 * Display: displays previously rendered output
332 *****************************************************************************/
333 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
335 #define p_b p_vout->p_sys->pp_buffer
336 p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
338 /* Change display frame */
339 if( p_vout->p_sys->b_must_acquire )
341 ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
343 ggiSetDisplayFrame( p_vout->p_sys->p_display,
344 p_b[ p_vout->p_sys->i_index ]->frame );
346 /* Swap buffers and change write frame */
347 p_vout->p_sys->i_index ^= 1;
348 p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
350 if( p_vout->p_sys->b_must_acquire )
352 ggiResourceAcquire( p_b[ p_vout->p_sys->i_index ]->resource,
355 ggiSetWriteFrame( p_vout->p_sys->p_display,
356 p_b[ p_vout->p_sys->i_index ]->frame );
358 /* Flush the output so that it actually displays */
359 ggiFlush( p_vout->p_sys->p_display );
363 /* following functions are local */
365 /*****************************************************************************
366 * OpenDisplay: open and initialize GGI device
367 *****************************************************************************
368 * Open and initialize display according to preferences specified in the vout
370 *****************************************************************************/
371 static int OpenDisplay( vout_thread_t *p_vout )
373 #define p_b p_vout->p_sys->pp_buffer
374 ggi_color col_fg; /* foreground color */
375 ggi_color col_bg; /* background color */
376 int i_index; /* all purposes index */
379 /* Initialize library */
382 msg_Err( p_vout, "cannot initialize GGI library" );
387 psz_display = config_GetPsz( p_vout, "ggi_display" );
389 p_vout->p_sys->p_display = ggiOpen( psz_display, NULL );
390 if( psz_display ) free( psz_display );
392 if( p_vout->p_sys->p_display == NULL )
394 msg_Err( p_vout, "cannot open GGI default display" );
399 /* Find most appropriate mode */
400 p_vout->p_sys->mode.frames = 2; /* 2 buffers */
401 p_vout->p_sys->mode.visible.x = config_GetInt( p_vout, "width" );
402 p_vout->p_sys->mode.visible.y = config_GetInt( p_vout, "height" );
403 p_vout->p_sys->mode.virt.x = GGI_AUTO;
404 p_vout->p_sys->mode.virt.y = GGI_AUTO;
405 p_vout->p_sys->mode.size.x = GGI_AUTO;
406 p_vout->p_sys->mode.size.y = GGI_AUTO;
407 p_vout->p_sys->mode.graphtype = GT_15BIT; /* minimum usable depth */
408 p_vout->p_sys->mode.dpp.x = GGI_AUTO;
409 p_vout->p_sys->mode.dpp.y = GGI_AUTO;
410 ggiCheckMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode );
412 /* FIXME: Check that returned mode has some minimum properties */
415 if( ggiSetMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode ) )
417 msg_Err( p_vout, "cannot set GGI mode" );
418 ggiClose( p_vout->p_sys->p_display );
423 /* Check buffers properties */
424 p_vout->p_sys->b_must_acquire = 0;
425 for( i_index = 0; i_index < 2; i_index++ )
427 /* Get buffer address */
428 p_vout->p_sys->pp_buffer[ i_index ] =
429 (ggi_directbuffer *)ggiDBGetBuffer( p_vout->p_sys->p_display,
431 if( p_b[ i_index ] == NULL )
433 msg_Err( p_vout, "double buffering is not possible" );
434 ggiClose( p_vout->p_sys->p_display );
439 /* Check buffer properties */
440 if( ! ( p_b[ i_index ]->type & GGI_DB_SIMPLE_PLB )
441 || ( p_b[ i_index ]->page_size != 0 )
442 || ( p_b[ i_index ]->write == NULL )
443 || ( p_b[ i_index ]->noaccess != 0 )
444 || ( p_b[ i_index ]->align != 0 ) )
446 msg_Err( p_vout, "incorrect video memory type" );
447 ggiClose( p_vout->p_sys->p_display );
452 /* Check if buffer needs to be acquired before write */
453 if( ggiResourceMustAcquire( p_b[ i_index ]->resource ) )
455 p_vout->p_sys->b_must_acquire = 1;
459 /* Set graphic context colors */
460 col_fg.r = col_fg.g = col_fg.b = -1;
461 col_bg.r = col_bg.g = col_bg.b = 0;
462 if( ggiSetGCForeground(p_vout->p_sys->p_display,
463 ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
464 ggiSetGCBackground(p_vout->p_sys->p_display,
465 ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
467 msg_Err( p_vout, "cannot set colors" );
468 ggiClose( p_vout->p_sys->p_display );
473 /* Set clipping for text */
474 if( ggiSetGCClipping( p_vout->p_sys->p_display, 0, 0,
475 p_vout->p_sys->mode.visible.x,
476 p_vout->p_sys->mode.visible.y ) )
478 msg_Err( p_vout, "cannot set clipping" );
479 ggiClose( p_vout->p_sys->p_display );
484 /* FIXME: set palette in 8bpp */
485 p_vout->p_sys->i_bits_per_pixel = p_b[ 0 ]->buffer.plb.pixelformat->depth;
491 /*****************************************************************************
492 * CloseDisplay: close and reset GGI device
493 *****************************************************************************
494 * This function returns all resources allocated by OpenDisplay and restore
495 * the original state of the device.
496 *****************************************************************************/
497 static void CloseDisplay( vout_thread_t *p_vout )
499 /* Restore original mode and close display */
500 ggiClose( p_vout->p_sys->p_display );
506 /*****************************************************************************
507 * SetPalette: sets an 8 bpp palette
508 *****************************************************************************/
509 static void SetPalette( vout_thread_t *p_vout, u16 *red, u16 *green, u16 *blue )
511 ggi_color colors[256];
514 /* Fill colors with color information */
515 for( i = 0; i < 256; i++ )
517 colors[ i ].r = red[ i ];
518 colors[ i ].g = green[ i ];
519 colors[ i ].b = blue[ i ];
524 if( ggiSetPalette( p_vout->p_sys->p_display, 0, 256, colors ) < 0 )
526 msg_Err( p_vout, "failed setting palette" );