1 /*****************************************************************************
2 * ggi.c : GGI plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <errno.h> /* ENOMEM */
33 #include <vlc_interface.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Create ( vlc_object_t * );
40 static void Destroy ( vlc_object_t * );
42 static int Init ( vout_thread_t * );
43 static void End ( vout_thread_t * );
44 static int Manage ( vout_thread_t * );
45 static void Display ( vout_thread_t *, picture_t * );
47 static int OpenDisplay ( vout_thread_t * );
48 static void CloseDisplay ( vout_thread_t * );
49 static void SetPalette ( vout_thread_t *, uint16_t *, uint16_t *, uint16_t * );
51 /*****************************************************************************
53 *****************************************************************************/
54 #define DISPLAY_TEXT N_("X11 display")
55 #define DISPLAY_LONGTEXT N_( \
56 "X11 hardware display to use.\n" \
57 "By default, VLC will use the value of the DISPLAY " \
58 "environment variable.")
61 add_string( "ggi-display", NULL, NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT, VLC_TRUE );
63 set_description( "General Graphics Interface video output" );
64 set_capability( "video output", 30 );
65 set_callbacks( Create, Destroy );
68 /*****************************************************************************
69 * vout_sys_t: video output GGI method descriptor
70 *****************************************************************************
71 * This structure is part of the video output thread descriptor.
72 * It describes the GGI specific properties of an output thread.
73 *****************************************************************************/
76 /* GGI system information */
77 ggi_visual_t p_display; /* display device */
79 ggi_mode mode; /* mode descriptor */
82 /* Buffer information */
83 ggi_directbuffer * pp_buffer[2]; /* buffers */
86 vlc_bool_t b_must_acquire; /* must be acquired before writing */
89 /*****************************************************************************
90 * Create: allocate GGI video thread output method
91 *****************************************************************************
92 * This function allocate and initialize a GGI vout method. It uses some of the
93 * vout properties to choose the correct mode, and change them according to the
95 *****************************************************************************/
96 static int Create( vlc_object_t *p_this )
98 vout_thread_t *p_vout = (vout_thread_t *)p_this;
100 /* Allocate structure */
101 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
102 if( p_vout->p_sys == NULL )
104 msg_Err( p_vout, "out of memory" );
108 /* Open and initialize device */
109 if( OpenDisplay( p_vout ) )
111 msg_Err( p_vout, "cannot initialize GGI display" );
112 free( p_vout->p_sys );
116 p_vout->pf_init = Init;
117 p_vout->pf_end = End;
118 p_vout->pf_manage = Manage;
119 p_vout->pf_render = NULL;
120 p_vout->pf_display = Display;
125 /*****************************************************************************
126 * Init: initialize GGI video thread output method
127 *****************************************************************************
128 * This function initialize the GGI display device.
129 *****************************************************************************/
130 static int Init( vout_thread_t *p_vout )
132 #define p_b p_vout->p_sys->pp_buffer
136 I_OUTPUTPICTURES = 0;
138 p_vout->output.i_width = p_vout->p_sys->mode.visible.x;
139 p_vout->output.i_height = p_vout->p_sys->mode.visible.y;
140 p_vout->output.i_aspect = p_vout->p_sys->mode.visible.x
142 / p_vout->p_sys->mode.visible.y;
144 switch( p_vout->p_sys->i_bits_per_pixel )
147 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
148 p_vout->output.pf_setpalette = SetPalette;
151 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
153 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
155 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
157 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
159 msg_Err( p_vout, "unknown screen depth %i",
160 p_vout->p_sys->i_bits_per_pixel );
164 /* Only useful for bits_per_pixel != 8 */
165 p_vout->output.i_rmask = p_b[ 0 ]->buffer.plb.pixelformat->red_mask;
166 p_vout->output.i_gmask = p_b[ 0 ]->buffer.plb.pixelformat->green_mask;
167 p_vout->output.i_bmask = p_b[ 0 ]->buffer.plb.pixelformat->blue_mask;
171 /* Find an empty picture slot */
172 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
174 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
176 p_pic = p_vout->p_picture + i_index;
186 /* We know the chroma, allocate a buffer which will be used
187 * directly by the decoder */
188 p_vout->p_sys->i_index = 0;
189 p_pic->p->p_pixels = p_b[ 0 ]->write;
190 p_pic->p->i_pixel_pitch = p_b[ 0 ]->buffer.plb.pixelformat->size / 8;
191 p_pic->p->i_lines = p_vout->p_sys->mode.visible.y;
192 p_pic->p->i_visible_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 occurred.
270 *****************************************************************************/
271 static int Manage( vout_thread_t *p_vout )
273 struct timeval tv = { 0, 1000 }; /* 1 millisecond */
278 mask = emKeyboard | emPtrButtonPress | emPtrButtonRelease;
280 ggiEventPoll( p_vout->p_sys->p_display, mask, &tv );
282 while( ggiEventsQueued( p_vout->p_sys->p_display, mask) )
284 ggiEventRead( p_vout->p_sys->p_display, &event, mask);
286 switch( event.any.type )
290 switch( event.key.sym )
295 vlc_object_kill( p_vout->p_libvlc );
303 case evPtrButtonRelease:
305 switch( event.pbutton.button )
307 case GII_PBUTTON_LEFT:
308 val.b_bool = VLC_TRUE;
309 var_Set( p_vout, "mouse-clicked", val );
312 case GII_PBUTTON_RIGHT:
314 intf_thread_t *p_intf;
315 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
319 p_intf->b_menu_change = 1;
320 vlc_object_release( p_intf );
335 /*****************************************************************************
336 * Display: displays previously rendered output
337 *****************************************************************************/
338 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
340 #define p_b p_vout->p_sys->pp_buffer
341 p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
343 /* Change display frame */
344 if( p_vout->p_sys->b_must_acquire )
346 ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
348 ggiSetDisplayFrame( p_vout->p_sys->p_display,
349 p_b[ p_vout->p_sys->i_index ]->frame );
351 /* Swap buffers and change write frame */
352 p_vout->p_sys->i_index ^= 1;
353 p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
355 if( p_vout->p_sys->b_must_acquire )
357 ggiResourceAcquire( p_b[ p_vout->p_sys->i_index ]->resource,
360 ggiSetWriteFrame( p_vout->p_sys->p_display,
361 p_b[ p_vout->p_sys->i_index ]->frame );
363 /* Flush the output so that it actually displays */
364 ggiFlush( p_vout->p_sys->p_display );
368 /* following functions are local */
370 /*****************************************************************************
371 * OpenDisplay: open and initialize GGI device
372 *****************************************************************************
373 * Open and initialize display according to preferences specified in the vout
375 *****************************************************************************/
376 static int OpenDisplay( vout_thread_t *p_vout )
378 #define p_b p_vout->p_sys->pp_buffer
379 ggi_color col_fg; /* foreground color */
380 ggi_color col_bg; /* background color */
381 int i_index; /* all purposes index */
384 /* Initialize library */
387 msg_Err( p_vout, "cannot initialize GGI library" );
392 psz_display = config_GetPsz( p_vout, "ggi_display" );
394 p_vout->p_sys->p_display = ggiOpen( psz_display, NULL );
395 if( psz_display ) free( psz_display );
397 if( p_vout->p_sys->p_display == NULL )
399 msg_Err( p_vout, "cannot open GGI default display" );
404 /* Find most appropriate mode */
405 p_vout->p_sys->mode.frames = 2; /* 2 buffers */
406 p_vout->p_sys->mode.visible.x = config_GetInt( p_vout, "width" );
407 p_vout->p_sys->mode.visible.y = config_GetInt( p_vout, "height" );
408 p_vout->p_sys->mode.virt.x = GGI_AUTO;
409 p_vout->p_sys->mode.virt.y = GGI_AUTO;
410 p_vout->p_sys->mode.size.x = GGI_AUTO;
411 p_vout->p_sys->mode.size.y = GGI_AUTO;
412 p_vout->p_sys->mode.graphtype = GT_15BIT; /* minimum usable depth */
413 p_vout->p_sys->mode.dpp.x = GGI_AUTO;
414 p_vout->p_sys->mode.dpp.y = GGI_AUTO;
415 ggiCheckMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode );
417 /* FIXME: Check that returned mode has some minimum properties */
420 if( ggiSetMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode ) )
422 msg_Err( p_vout, "cannot set GGI mode" );
423 ggiClose( p_vout->p_sys->p_display );
428 /* Check buffers properties */
429 p_vout->p_sys->b_must_acquire = 0;
430 for( i_index = 0; i_index < 2; i_index++ )
432 /* Get buffer address */
433 p_vout->p_sys->pp_buffer[ i_index ] =
434 (ggi_directbuffer *)ggiDBGetBuffer( p_vout->p_sys->p_display,
436 if( p_b[ i_index ] == NULL )
438 msg_Err( p_vout, "double buffering is not possible" );
439 ggiClose( p_vout->p_sys->p_display );
444 /* Check buffer properties */
445 if( ! ( p_b[ i_index ]->type & GGI_DB_SIMPLE_PLB )
446 || ( p_b[ i_index ]->page_size != 0 )
447 || ( p_b[ i_index ]->write == NULL )
448 || ( p_b[ i_index ]->noaccess != 0 )
449 || ( p_b[ i_index ]->align != 0 ) )
451 msg_Err( p_vout, "incorrect video memory type" );
452 ggiClose( p_vout->p_sys->p_display );
457 /* Check if buffer needs to be acquired before write */
458 if( ggiResourceMustAcquire( p_b[ i_index ]->resource ) )
460 p_vout->p_sys->b_must_acquire = 1;
464 /* Set graphic context colors */
465 col_fg.r = col_fg.g = col_fg.b = -1;
466 col_bg.r = col_bg.g = col_bg.b = 0;
467 if( ggiSetGCForeground(p_vout->p_sys->p_display,
468 ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
469 ggiSetGCBackground(p_vout->p_sys->p_display,
470 ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
472 msg_Err( p_vout, "cannot set colors" );
473 ggiClose( p_vout->p_sys->p_display );
478 /* Set clipping for text */
479 if( ggiSetGCClipping( p_vout->p_sys->p_display, 0, 0,
480 p_vout->p_sys->mode.visible.x,
481 p_vout->p_sys->mode.visible.y ) )
483 msg_Err( p_vout, "cannot set clipping" );
484 ggiClose( p_vout->p_sys->p_display );
489 /* FIXME: set palette in 8bpp */
490 p_vout->p_sys->i_bits_per_pixel = p_b[ 0 ]->buffer.plb.pixelformat->depth;
496 /*****************************************************************************
497 * CloseDisplay: close and reset GGI device
498 *****************************************************************************
499 * This function returns all resources allocated by OpenDisplay and restore
500 * the original state of the device.
501 *****************************************************************************/
502 static void CloseDisplay( vout_thread_t *p_vout )
504 /* Restore original mode and close display */
505 ggiClose( p_vout->p_sys->p_display );
511 /*****************************************************************************
512 * SetPalette: sets an 8 bpp palette
513 *****************************************************************************/
514 static void SetPalette( vout_thread_t *p_vout,
515 uint16_t *red, uint16_t *green, uint16_t *blue )
517 ggi_color colors[256];
520 /* Fill colors with color information */
521 for( i = 0; i < 256; i++ )
523 colors[ i ].r = red[ i ];
524 colors[ i ].g = green[ i ];
525 colors[ i ].b = blue[ i ];
530 if( ggiSetPalette( p_vout->p_sys->p_display, 0, 256, colors ) < 0 )
532 msg_Err( p_vout, "failed to set palette" );