1 /*****************************************************************************
2 * ggi.c : GGI plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2009 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 */
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_interface.h>
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Create ( vlc_object_t * );
45 static void Destroy ( vlc_object_t * );
47 static int Init ( vout_thread_t * );
48 static void End ( vout_thread_t * );
49 static int Manage ( vout_thread_t * );
50 static void Display ( vout_thread_t *, picture_t * );
52 static int OpenDisplay ( vout_thread_t * );
53 static void CloseDisplay ( vout_thread_t * );
54 static void SetPalette ( vout_thread_t *, uint16_t *, uint16_t *, uint16_t * );
56 /*****************************************************************************
58 *****************************************************************************/
59 #define DISPLAY_TEXT N_("X11 display")
60 #define DISPLAY_LONGTEXT N_( \
61 "X11 hardware display to use.\n" \
62 "By default, VLC will use the value of the DISPLAY " \
63 "environment variable.")
66 add_string( "ggi-display", NULL, NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT, true )
67 set_description( "General Graphics Interface video output" )
68 set_capability( "video output", 30 )
69 set_callbacks( Create, Destroy )
72 /*****************************************************************************
73 * vout_sys_t: video output GGI method descriptor
74 *****************************************************************************
75 * This structure is part of the video output thread descriptor.
76 * It describes the GGI specific properties of an output thread.
77 *****************************************************************************/
80 /* GGI system information */
81 ggi_visual_t p_display; /* display device */
83 ggi_mode mode; /* mode descriptor */
86 /* Buffer information */
87 ggi_directbuffer * pp_buffer[2]; /* buffers */
90 bool b_must_acquire; /* must be acquired before writing */
93 /*****************************************************************************
94 * Create: allocate GGI video thread output method
95 *****************************************************************************
96 * This function allocate and initialize a GGI vout method. It uses some of the
97 * vout properties to choose the correct mode, and change them according to the
99 *****************************************************************************/
100 static int Create( vlc_object_t *p_this )
102 vout_thread_t *p_vout = (vout_thread_t *)p_this;
104 /* Allocate structure */
105 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
106 if( p_vout->p_sys == NULL )
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;
193 p_pic->p->i_visible_lines = p_vout->p_sys->mode.visible.y;
195 p_pic->p->i_pitch = p_b[ 0 ]->buffer.plb.stride;
197 if( p_b[ 0 ]->buffer.plb.pixelformat->size / 8
198 * p_vout->p_sys->mode.visible.x
199 != p_b[ 0 ]->buffer.plb.stride )
201 p_pic->p->i_visible_pitch = p_b[ 0 ]->buffer.plb.pixelformat->size
202 / 8 * p_vout->p_sys->mode.visible.x;
206 p_pic->p->i_visible_pitch = p_b[ 0 ]->buffer.plb.stride;
211 p_pic->i_status = DESTROYED_PICTURE;
212 p_pic->i_type = DIRECT_PICTURE;
214 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
218 /* Acquire first buffer */
219 if( p_vout->p_sys->b_must_acquire )
221 ggiResourceAcquire( p_b[ 0 ]->resource,
225 /* Listen to the keyboard and the mouse buttons */
226 ggiSetEventMask( p_vout->p_sys->p_display,
227 emKeyboard | emPtrButtonPress | emPtrButtonRelease );
229 /* Set asynchronous display mode -- usually quite faster */
230 ggiAddFlags( p_vout->p_sys->p_display, GGIFLAG_ASYNC );
236 /*****************************************************************************
237 * End: terminate GGI video thread output method
238 *****************************************************************************
239 * Terminate an output method created by Create
240 *****************************************************************************/
241 static void End( vout_thread_t *p_vout )
243 #define p_b p_vout->p_sys->pp_buffer
245 if( p_vout->p_sys->b_must_acquire )
247 ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
252 /*****************************************************************************
253 * Destroy: destroy GGI video thread output method
254 *****************************************************************************
255 * Terminate an output method created by Create
256 *****************************************************************************/
257 static void Destroy( vlc_object_t *p_this )
259 vout_thread_t *p_vout = (vout_thread_t *)p_this;
261 CloseDisplay( p_vout );
263 free( p_vout->p_sys );
266 /*****************************************************************************
267 * Manage: handle GGI events
268 *****************************************************************************
269 * This function should be called regularly by video output thread. It returns
270 * a non null value if an error occurred.
271 *****************************************************************************/
272 static int Manage( vout_thread_t *p_vout )
274 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 libvlc_Quit( p_vout->p_libvlc );
303 case evPtrButtonRelease:
305 switch( event.pbutton.button )
307 case GII_PBUTTON_LEFT:
308 var_SetBool( p_vout, "mouse-clicked", true );
311 case GII_PBUTTON_RIGHT:
312 var_SetBool( p_vout->p_libvlc, "intf-popupmenu", true );
325 /*****************************************************************************
326 * Display: displays previously rendered output
327 *****************************************************************************/
328 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
330 #define p_b p_vout->p_sys->pp_buffer
331 p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
333 /* Change display frame */
334 if( p_vout->p_sys->b_must_acquire )
336 ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
338 ggiSetDisplayFrame( p_vout->p_sys->p_display,
339 p_b[ p_vout->p_sys->i_index ]->frame );
341 /* Swap buffers and change write frame */
342 p_vout->p_sys->i_index ^= 1;
343 p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
345 if( p_vout->p_sys->b_must_acquire )
347 ggiResourceAcquire( p_b[ p_vout->p_sys->i_index ]->resource,
350 ggiSetWriteFrame( p_vout->p_sys->p_display,
351 p_b[ p_vout->p_sys->i_index ]->frame );
353 /* Flush the output so that it actually displays */
354 ggiFlush( p_vout->p_sys->p_display );
358 /* following functions are local */
360 /*****************************************************************************
361 * OpenDisplay: open and initialize GGI device
362 *****************************************************************************
363 * Open and initialize display according to preferences specified in the vout
365 *****************************************************************************/
366 static int OpenDisplay( vout_thread_t *p_vout )
368 #define p_b p_vout->p_sys->pp_buffer
369 ggi_color col_fg; /* foreground color */
370 ggi_color col_bg; /* background color */
371 int i_index; /* all purposes index */
374 /* Initialize library */
377 msg_Err( p_vout, "cannot initialize GGI library" );
382 psz_display = config_GetPsz( p_vout, "ggi-display" );
384 p_vout->p_sys->p_display = ggiOpen( psz_display, NULL );
387 if( p_vout->p_sys->p_display == NULL )
389 msg_Err( p_vout, "cannot open GGI default display" );
394 /* Find most appropriate mode */
395 p_vout->p_sys->mode.frames = 2; /* 2 buffers */
396 p_vout->p_sys->mode.visible.x = config_GetInt( p_vout, "width" );
397 p_vout->p_sys->mode.visible.y = config_GetInt( p_vout, "height" );
398 p_vout->p_sys->mode.virt.x = GGI_AUTO;
399 p_vout->p_sys->mode.virt.y = GGI_AUTO;
400 p_vout->p_sys->mode.size.x = GGI_AUTO;
401 p_vout->p_sys->mode.size.y = GGI_AUTO;
402 p_vout->p_sys->mode.graphtype = GT_15BIT; /* minimum usable depth */
403 p_vout->p_sys->mode.dpp.x = GGI_AUTO;
404 p_vout->p_sys->mode.dpp.y = GGI_AUTO;
405 ggiCheckMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode );
407 /* FIXME: Check that returned mode has some minimum properties */
410 if( ggiSetMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode ) )
412 msg_Err( p_vout, "cannot set GGI mode" );
413 ggiClose( p_vout->p_sys->p_display );
418 /* Check buffers properties */
419 p_vout->p_sys->b_must_acquire = 0;
420 for( i_index = 0; i_index < 2; i_index++ )
422 /* Get buffer address */
423 p_vout->p_sys->pp_buffer[ i_index ] =
424 (ggi_directbuffer *)ggiDBGetBuffer( p_vout->p_sys->p_display,
426 if( p_b[ i_index ] == NULL )
428 msg_Err( p_vout, "double buffering is not possible" );
429 ggiClose( p_vout->p_sys->p_display );
434 /* Check buffer properties */
435 if( ! ( p_b[ i_index ]->type & GGI_DB_SIMPLE_PLB )
436 || ( p_b[ i_index ]->page_size != 0 )
437 || ( p_b[ i_index ]->write == NULL )
438 || ( p_b[ i_index ]->noaccess != 0 )
439 || ( p_b[ i_index ]->align != 0 ) )
441 msg_Err( p_vout, "incorrect video memory type" );
442 ggiClose( p_vout->p_sys->p_display );
447 /* Check if buffer needs to be acquired before write */
448 if( ggiResourceMustAcquire( p_b[ i_index ]->resource ) )
450 p_vout->p_sys->b_must_acquire = 1;
454 /* Set graphic context colors */
455 col_fg.r = col_fg.g = col_fg.b = -1;
456 col_bg.r = col_bg.g = col_bg.b = 0;
457 if( ggiSetGCForeground(p_vout->p_sys->p_display,
458 ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
459 ggiSetGCBackground(p_vout->p_sys->p_display,
460 ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
462 msg_Err( p_vout, "cannot set colors" );
463 ggiClose( p_vout->p_sys->p_display );
468 /* Set clipping for text */
469 if( ggiSetGCClipping( p_vout->p_sys->p_display, 0, 0,
470 p_vout->p_sys->mode.visible.x,
471 p_vout->p_sys->mode.visible.y ) )
473 msg_Err( p_vout, "cannot set clipping" );
474 ggiClose( p_vout->p_sys->p_display );
479 /* FIXME: set palette in 8bpp */
480 p_vout->p_sys->i_bits_per_pixel = p_b[ 0 ]->buffer.plb.pixelformat->depth;
486 /*****************************************************************************
487 * CloseDisplay: close and reset GGI device
488 *****************************************************************************
489 * This function returns all resources allocated by OpenDisplay and restore
490 * the original state of the device.
491 *****************************************************************************/
492 static void CloseDisplay( vout_thread_t *p_vout )
494 /* Restore original mode and close display */
495 ggiClose( p_vout->p_sys->p_display );
501 /*****************************************************************************
502 * SetPalette: sets an 8 bpp palette
503 *****************************************************************************/
504 static void SetPalette( vout_thread_t *p_vout,
505 uint16_t *red, uint16_t *green, uint16_t *blue )
507 ggi_color colors[256];
510 /* Fill colors with color information */
511 for( i = 0; i < 256; i++ )
513 colors[ i ].r = red[ i ];
514 colors[ i ].g = green[ i ];
515 colors[ i ].b = blue[ i ];
520 if( ggiSetPalette( p_vout->p_sys->p_display, 0, 256, colors ) < 0 )
522 msg_Err( p_vout, "failed to set palette" );