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 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_interface.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Create ( vlc_object_t * );
43 static void Destroy ( vlc_object_t * );
45 static int Init ( vout_thread_t * );
46 static void End ( vout_thread_t * );
47 static int Manage ( vout_thread_t * );
48 static void Display ( vout_thread_t *, picture_t * );
50 static int OpenDisplay ( vout_thread_t * );
51 static void CloseDisplay ( vout_thread_t * );
52 static void SetPalette ( vout_thread_t *, uint16_t *, uint16_t *, uint16_t * );
54 /*****************************************************************************
56 *****************************************************************************/
57 #define DISPLAY_TEXT N_("X11 display")
58 #define DISPLAY_LONGTEXT N_( \
59 "X11 hardware display to use.\n" \
60 "By default, VLC will use the value of the DISPLAY " \
61 "environment variable.")
64 add_string( "ggi-display", NULL, NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT, true )
65 set_description( "General Graphics Interface video output" )
66 set_capability( "video output", 30 )
67 set_callbacks( Create, Destroy )
70 /*****************************************************************************
71 * vout_sys_t: video output GGI method descriptor
72 *****************************************************************************
73 * This structure is part of the video output thread descriptor.
74 * It describes the GGI specific properties of an output thread.
75 *****************************************************************************/
78 /* GGI system information */
79 ggi_visual_t p_display; /* display device */
81 ggi_mode mode; /* mode descriptor */
84 /* Buffer information */
85 ggi_directbuffer * pp_buffer[2]; /* buffers */
88 bool b_must_acquire; /* must be acquired before writing */
91 /*****************************************************************************
92 * Create: allocate GGI video thread output method
93 *****************************************************************************
94 * This function allocate and initialize a GGI vout method. It uses some of the
95 * vout properties to choose the correct mode, and change them according to the
97 *****************************************************************************/
98 static int Create( vlc_object_t *p_this )
100 vout_thread_t *p_vout = (vout_thread_t *)p_this;
102 /* Allocate structure */
103 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
104 if( p_vout->p_sys == NULL )
107 /* Open and initialize device */
108 if( OpenDisplay( p_vout ) )
110 msg_Err( p_vout, "cannot initialize GGI display" );
111 free( p_vout->p_sys );
115 p_vout->pf_init = Init;
116 p_vout->pf_end = End;
117 p_vout->pf_manage = Manage;
118 p_vout->pf_render = NULL;
119 p_vout->pf_display = Display;
124 /*****************************************************************************
125 * Init: initialize GGI video thread output method
126 *****************************************************************************
127 * This function initialize the GGI display device.
128 *****************************************************************************/
129 static int Init( vout_thread_t *p_vout )
131 #define p_b p_vout->p_sys->pp_buffer
135 I_OUTPUTPICTURES = 0;
137 p_vout->output.i_width = p_vout->p_sys->mode.visible.x;
138 p_vout->output.i_height = p_vout->p_sys->mode.visible.y;
139 p_vout->output.i_aspect = p_vout->p_sys->mode.visible.x
141 / p_vout->p_sys->mode.visible.y;
143 switch( p_vout->p_sys->i_bits_per_pixel )
146 p_vout->output.i_chroma = VLC_CODEC_RGB8;
147 p_vout->output.pf_setpalette = SetPalette;
150 p_vout->output.i_chroma = VLC_CODEC_RGB15; break;
152 p_vout->output.i_chroma = VLC_CODEC_RGB16; break;
154 p_vout->output.i_chroma = VLC_CODEC_RGB24; break;
156 p_vout->output.i_chroma = VLC_CODEC_RGB32; break;
158 msg_Err( p_vout, "unknown screen depth %i",
159 p_vout->p_sys->i_bits_per_pixel );
163 /* Only useful for bits_per_pixel != 8 */
164 p_vout->output.i_rmask = p_b[ 0 ]->buffer.plb.pixelformat->red_mask;
165 p_vout->output.i_gmask = p_b[ 0 ]->buffer.plb.pixelformat->green_mask;
166 p_vout->output.i_bmask = p_b[ 0 ]->buffer.plb.pixelformat->blue_mask;
170 /* Find an empty picture slot */
171 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
173 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
175 p_pic = p_vout->p_picture + i_index;
185 /* We know the chroma, allocate a buffer which will be used
186 * directly by the decoder */
187 p_vout->p_sys->i_index = 0;
188 p_pic->p->p_pixels = p_b[ 0 ]->write;
189 p_pic->p->i_pixel_pitch = p_b[ 0 ]->buffer.plb.pixelformat->size / 8;
190 p_pic->p->i_lines = p_vout->p_sys->mode.visible.y;
191 p_pic->p->i_visible_lines = p_vout->p_sys->mode.visible.y;
193 p_pic->p->i_pitch = p_b[ 0 ]->buffer.plb.stride;
195 if( p_b[ 0 ]->buffer.plb.pixelformat->size / 8
196 * p_vout->p_sys->mode.visible.x
197 != p_b[ 0 ]->buffer.plb.stride )
199 p_pic->p->i_visible_pitch = p_b[ 0 ]->buffer.plb.pixelformat->size
200 / 8 * p_vout->p_sys->mode.visible.x;
204 p_pic->p->i_visible_pitch = p_b[ 0 ]->buffer.plb.stride;
209 p_pic->i_status = DESTROYED_PICTURE;
210 p_pic->i_type = DIRECT_PICTURE;
212 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
216 /* Acquire first buffer */
217 if( p_vout->p_sys->b_must_acquire )
219 ggiResourceAcquire( p_b[ 0 ]->resource,
223 /* Listen to the keyboard and the mouse buttons */
224 ggiSetEventMask( p_vout->p_sys->p_display,
225 emKeyboard | emPtrButtonPress | emPtrButtonRelease );
227 /* Set asynchronous display mode -- usually quite faster */
228 ggiAddFlags( p_vout->p_sys->p_display, GGIFLAG_ASYNC );
234 /*****************************************************************************
235 * End: terminate GGI video thread output method
236 *****************************************************************************
237 * Terminate an output method created by Create
238 *****************************************************************************/
239 static void End( vout_thread_t *p_vout )
241 #define p_b p_vout->p_sys->pp_buffer
243 if( p_vout->p_sys->b_must_acquire )
245 ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
250 /*****************************************************************************
251 * Destroy: destroy GGI video thread output method
252 *****************************************************************************
253 * Terminate an output method created by Create
254 *****************************************************************************/
255 static void Destroy( vlc_object_t *p_this )
257 vout_thread_t *p_vout = (vout_thread_t *)p_this;
259 CloseDisplay( p_vout );
261 free( p_vout->p_sys );
264 /*****************************************************************************
265 * Manage: handle GGI events
266 *****************************************************************************
267 * This function should be called regularly by video output thread. It returns
268 * a non null value if an error occurred.
269 *****************************************************************************/
270 static int Manage( vout_thread_t *p_vout )
272 struct timeval tv = { 0, 1000 }; /* 1 millisecond */
276 mask = emKeyboard | emPtrButtonPress | emPtrButtonRelease;
278 ggiEventPoll( p_vout->p_sys->p_display, mask, &tv );
280 while( ggiEventsQueued( p_vout->p_sys->p_display, mask) )
282 ggiEventRead( p_vout->p_sys->p_display, &event, mask);
284 switch( event.any.type )
288 switch( event.key.sym )
293 libvlc_Quit( p_vout->p_libvlc );
301 case evPtrButtonRelease:
303 switch( event.pbutton.button )
305 case GII_PBUTTON_LEFT:
306 var_SetBool( p_vout, "mouse-clicked", true );
309 case GII_PBUTTON_RIGHT:
310 var_SetBool( p_vout->p_libvlc, "intf-popupmenu", true );
323 /*****************************************************************************
324 * Display: displays previously rendered output
325 *****************************************************************************/
326 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
328 #define p_b p_vout->p_sys->pp_buffer
329 p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
331 /* Change display frame */
332 if( p_vout->p_sys->b_must_acquire )
334 ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
336 ggiSetDisplayFrame( p_vout->p_sys->p_display,
337 p_b[ p_vout->p_sys->i_index ]->frame );
339 /* Swap buffers and change write frame */
340 p_vout->p_sys->i_index ^= 1;
341 p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
343 if( p_vout->p_sys->b_must_acquire )
345 ggiResourceAcquire( p_b[ p_vout->p_sys->i_index ]->resource,
348 ggiSetWriteFrame( p_vout->p_sys->p_display,
349 p_b[ p_vout->p_sys->i_index ]->frame );
351 /* Flush the output so that it actually displays */
352 ggiFlush( p_vout->p_sys->p_display );
356 /* following functions are local */
358 /*****************************************************************************
359 * OpenDisplay: open and initialize GGI device
360 *****************************************************************************
361 * Open and initialize display according to preferences specified in the vout
363 *****************************************************************************/
364 static int OpenDisplay( vout_thread_t *p_vout )
366 #define p_b p_vout->p_sys->pp_buffer
367 ggi_color col_fg; /* foreground color */
368 ggi_color col_bg; /* background color */
369 int i_index; /* all purposes index */
372 /* Initialize library */
375 msg_Err( p_vout, "cannot initialize GGI library" );
380 psz_display = config_GetPsz( p_vout, "ggi-display" );
382 p_vout->p_sys->p_display = ggiOpen( psz_display, NULL );
385 if( p_vout->p_sys->p_display == NULL )
387 msg_Err( p_vout, "cannot open GGI default display" );
392 /* Find most appropriate mode */
393 p_vout->p_sys->mode.frames = 2; /* 2 buffers */
394 p_vout->p_sys->mode.visible.x = config_GetInt( p_vout, "width" );
395 p_vout->p_sys->mode.visible.y = config_GetInt( p_vout, "height" );
396 p_vout->p_sys->mode.virt.x = GGI_AUTO;
397 p_vout->p_sys->mode.virt.y = GGI_AUTO;
398 p_vout->p_sys->mode.size.x = GGI_AUTO;
399 p_vout->p_sys->mode.size.y = GGI_AUTO;
400 p_vout->p_sys->mode.graphtype = GT_15BIT; /* minimum usable depth */
401 p_vout->p_sys->mode.dpp.x = GGI_AUTO;
402 p_vout->p_sys->mode.dpp.y = GGI_AUTO;
403 ggiCheckMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode );
405 /* FIXME: Check that returned mode has some minimum properties */
408 if( ggiSetMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode ) )
410 msg_Err( p_vout, "cannot set GGI mode" );
411 ggiClose( p_vout->p_sys->p_display );
416 /* Check buffers properties */
417 p_vout->p_sys->b_must_acquire = 0;
418 for( i_index = 0; i_index < 2; i_index++ )
420 /* Get buffer address */
421 p_vout->p_sys->pp_buffer[ i_index ] =
422 (ggi_directbuffer *)ggiDBGetBuffer( p_vout->p_sys->p_display,
424 if( p_b[ i_index ] == NULL )
426 msg_Err( p_vout, "double buffering is not possible" );
427 ggiClose( p_vout->p_sys->p_display );
432 /* Check buffer properties */
433 if( ! ( p_b[ i_index ]->type & GGI_DB_SIMPLE_PLB )
434 || ( p_b[ i_index ]->page_size != 0 )
435 || ( p_b[ i_index ]->write == NULL )
436 || ( p_b[ i_index ]->noaccess != 0 )
437 || ( p_b[ i_index ]->align != 0 ) )
439 msg_Err( p_vout, "incorrect video memory type" );
440 ggiClose( p_vout->p_sys->p_display );
445 /* Check if buffer needs to be acquired before write */
446 if( ggiResourceMustAcquire( p_b[ i_index ]->resource ) )
448 p_vout->p_sys->b_must_acquire = 1;
452 /* Set graphic context colors */
453 col_fg.r = col_fg.g = col_fg.b = -1;
454 col_bg.r = col_bg.g = col_bg.b = 0;
455 if( ggiSetGCForeground(p_vout->p_sys->p_display,
456 ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
457 ggiSetGCBackground(p_vout->p_sys->p_display,
458 ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
460 msg_Err( p_vout, "cannot set colors" );
461 ggiClose( p_vout->p_sys->p_display );
466 /* Set clipping for text */
467 if( ggiSetGCClipping( p_vout->p_sys->p_display, 0, 0,
468 p_vout->p_sys->mode.visible.x,
469 p_vout->p_sys->mode.visible.y ) )
471 msg_Err( p_vout, "cannot set clipping" );
472 ggiClose( p_vout->p_sys->p_display );
477 /* FIXME: set palette in 8bpp */
478 p_vout->p_sys->i_bits_per_pixel = p_b[ 0 ]->buffer.plb.pixelformat->depth;
484 /*****************************************************************************
485 * CloseDisplay: close and reset GGI device
486 *****************************************************************************
487 * This function returns all resources allocated by OpenDisplay and restore
488 * the original state of the device.
489 *****************************************************************************/
490 static void CloseDisplay( vout_thread_t *p_vout )
492 /* Restore original mode and close display */
493 ggiClose( p_vout->p_sys->p_display );
499 /*****************************************************************************
500 * SetPalette: sets an 8 bpp palette
501 *****************************************************************************/
502 static void SetPalette( vout_thread_t *p_vout,
503 uint16_t *red, uint16_t *green, uint16_t *blue )
505 ggi_color colors[256];
508 /* Fill colors with color information */
509 for( i = 0; i < 256; i++ )
511 colors[ i ].r = red[ i ];
512 colors[ i ].g = green[ i ];
513 colors[ i ].b = blue[ i ];
518 if( ggiSetPalette( p_vout->p_sys->p_display, 0, 256, colors ) < 0 )
520 msg_Err( p_vout, "failed to set palette" );