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 */
37 #include <vlc_interface.h>
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Create ( vlc_object_t * );
44 static void Destroy ( vlc_object_t * );
46 static int Init ( vout_thread_t * );
47 static void End ( vout_thread_t * );
48 static int Manage ( vout_thread_t * );
49 static void 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 *, uint16_t *, uint16_t *, uint16_t * );
55 /*****************************************************************************
57 *****************************************************************************/
58 #define DISPLAY_TEXT N_("X11 display")
59 #define DISPLAY_LONGTEXT N_( \
60 "X11 hardware display to use.\n" \
61 "By default, VLC will use the value of the DISPLAY " \
62 "environment variable.")
65 add_string( "ggi-display", NULL, NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT, VLC_TRUE );
66 set_description( "General Graphics Interface video output" );
67 set_capability( "video output", 30 );
68 set_callbacks( Create, Destroy );
71 /*****************************************************************************
72 * vout_sys_t: video output GGI method descriptor
73 *****************************************************************************
74 * This structure is part of the video output thread descriptor.
75 * It describes the GGI specific properties of an output thread.
76 *****************************************************************************/
79 /* GGI system information */
80 ggi_visual_t p_display; /* display device */
82 ggi_mode mode; /* mode descriptor */
85 /* Buffer information */
86 ggi_directbuffer * pp_buffer[2]; /* buffers */
89 vlc_bool_t b_must_acquire; /* must be acquired before writing */
92 /*****************************************************************************
93 * Create: allocate GGI video thread output method
94 *****************************************************************************
95 * This function allocate and initialize a GGI vout method. It uses some of the
96 * vout properties to choose the correct mode, and change them according to the
98 *****************************************************************************/
99 static int Create( vlc_object_t *p_this )
101 vout_thread_t *p_vout = (vout_thread_t *)p_this;
103 /* Allocate structure */
104 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
105 if( p_vout->p_sys == NULL )
107 msg_Err( p_vout, "out of memory" );
111 /* Open and initialize device */
112 if( OpenDisplay( p_vout ) )
114 msg_Err( p_vout, "cannot initialize GGI display" );
115 free( p_vout->p_sys );
119 p_vout->pf_init = Init;
120 p_vout->pf_end = End;
121 p_vout->pf_manage = Manage;
122 p_vout->pf_render = NULL;
123 p_vout->pf_display = Display;
128 /*****************************************************************************
129 * Init: initialize GGI video thread output method
130 *****************************************************************************
131 * This function initialize the GGI display device.
132 *****************************************************************************/
133 static int Init( vout_thread_t *p_vout )
135 #define p_b p_vout->p_sys->pp_buffer
139 I_OUTPUTPICTURES = 0;
141 p_vout->output.i_width = p_vout->p_sys->mode.visible.x;
142 p_vout->output.i_height = p_vout->p_sys->mode.visible.y;
143 p_vout->output.i_aspect = p_vout->p_sys->mode.visible.x
145 / p_vout->p_sys->mode.visible.y;
147 switch( p_vout->p_sys->i_bits_per_pixel )
150 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
151 p_vout->output.pf_setpalette = SetPalette;
154 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
156 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
158 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
160 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
162 msg_Err( p_vout, "unknown screen depth %i",
163 p_vout->p_sys->i_bits_per_pixel );
167 /* Only useful for bits_per_pixel != 8 */
168 p_vout->output.i_rmask = p_b[ 0 ]->buffer.plb.pixelformat->red_mask;
169 p_vout->output.i_gmask = p_b[ 0 ]->buffer.plb.pixelformat->green_mask;
170 p_vout->output.i_bmask = p_b[ 0 ]->buffer.plb.pixelformat->blue_mask;
174 /* Find an empty picture slot */
175 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
177 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
179 p_pic = p_vout->p_picture + i_index;
189 /* We know the chroma, allocate a buffer which will be used
190 * directly by the decoder */
191 p_vout->p_sys->i_index = 0;
192 p_pic->p->p_pixels = p_b[ 0 ]->write;
193 p_pic->p->i_pixel_pitch = p_b[ 0 ]->buffer.plb.pixelformat->size / 8;
194 p_pic->p->i_lines = p_vout->p_sys->mode.visible.y;
195 p_pic->p->i_visible_lines = p_vout->p_sys->mode.visible.y;
197 p_pic->p->i_pitch = p_b[ 0 ]->buffer.plb.stride;
199 if( p_b[ 0 ]->buffer.plb.pixelformat->size / 8
200 * p_vout->p_sys->mode.visible.x
201 != p_b[ 0 ]->buffer.plb.stride )
203 p_pic->p->i_visible_pitch = p_b[ 0 ]->buffer.plb.pixelformat->size
204 / 8 * p_vout->p_sys->mode.visible.x;
208 p_pic->p->i_visible_pitch = p_b[ 0 ]->buffer.plb.stride;
213 p_pic->i_status = DESTROYED_PICTURE;
214 p_pic->i_type = DIRECT_PICTURE;
216 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
220 /* Acquire first buffer */
221 if( p_vout->p_sys->b_must_acquire )
223 ggiResourceAcquire( p_b[ 0 ]->resource,
227 /* Listen to the keyboard and the mouse buttons */
228 ggiSetEventMask( p_vout->p_sys->p_display,
229 emKeyboard | emPtrButtonPress | emPtrButtonRelease );
231 /* Set asynchronous display mode -- usually quite faster */
232 ggiAddFlags( p_vout->p_sys->p_display, GGIFLAG_ASYNC );
238 /*****************************************************************************
239 * End: terminate GGI video thread output method
240 *****************************************************************************
241 * Terminate an output method created by Create
242 *****************************************************************************/
243 static void End( vout_thread_t *p_vout )
245 #define p_b p_vout->p_sys->pp_buffer
247 if( p_vout->p_sys->b_must_acquire )
249 ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
254 /*****************************************************************************
255 * Destroy: destroy GGI video thread output method
256 *****************************************************************************
257 * Terminate an output method created by Create
258 *****************************************************************************/
259 static void Destroy( vlc_object_t *p_this )
261 vout_thread_t *p_vout = (vout_thread_t *)p_this;
263 CloseDisplay( p_vout );
265 free( p_vout->p_sys );
268 /*****************************************************************************
269 * Manage: handle GGI events
270 *****************************************************************************
271 * This function should be called regularly by video output thread. It returns
272 * a non null value if an error occurred.
273 *****************************************************************************/
274 static int Manage( vout_thread_t *p_vout )
276 struct timeval tv = { 0, 1000 }; /* 1 millisecond */
281 mask = emKeyboard | emPtrButtonPress | emPtrButtonRelease;
283 ggiEventPoll( p_vout->p_sys->p_display, mask, &tv );
285 while( ggiEventsQueued( p_vout->p_sys->p_display, mask) )
287 ggiEventRead( p_vout->p_sys->p_display, &event, mask);
289 switch( event.any.type )
293 switch( event.key.sym )
298 vlc_object_kill( p_vout->p_libvlc );
306 case evPtrButtonRelease:
308 switch( event.pbutton.button )
310 case GII_PBUTTON_LEFT:
311 val.b_bool = VLC_TRUE;
312 var_Set( p_vout, "mouse-clicked", val );
315 case GII_PBUTTON_RIGHT:
317 intf_thread_t *p_intf;
318 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
322 p_intf->b_menu_change = 1;
323 vlc_object_release( p_intf );
338 /*****************************************************************************
339 * Display: displays previously rendered output
340 *****************************************************************************/
341 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
343 #define p_b p_vout->p_sys->pp_buffer
344 p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
346 /* Change display frame */
347 if( p_vout->p_sys->b_must_acquire )
349 ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
351 ggiSetDisplayFrame( p_vout->p_sys->p_display,
352 p_b[ p_vout->p_sys->i_index ]->frame );
354 /* Swap buffers and change write frame */
355 p_vout->p_sys->i_index ^= 1;
356 p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
358 if( p_vout->p_sys->b_must_acquire )
360 ggiResourceAcquire( p_b[ p_vout->p_sys->i_index ]->resource,
363 ggiSetWriteFrame( p_vout->p_sys->p_display,
364 p_b[ p_vout->p_sys->i_index ]->frame );
366 /* Flush the output so that it actually displays */
367 ggiFlush( p_vout->p_sys->p_display );
371 /* following functions are local */
373 /*****************************************************************************
374 * OpenDisplay: open and initialize GGI device
375 *****************************************************************************
376 * Open and initialize display according to preferences specified in the vout
378 *****************************************************************************/
379 static int OpenDisplay( vout_thread_t *p_vout )
381 #define p_b p_vout->p_sys->pp_buffer
382 ggi_color col_fg; /* foreground color */
383 ggi_color col_bg; /* background color */
384 int i_index; /* all purposes index */
387 /* Initialize library */
390 msg_Err( p_vout, "cannot initialize GGI library" );
395 psz_display = config_GetPsz( p_vout, "ggi_display" );
397 p_vout->p_sys->p_display = ggiOpen( psz_display, NULL );
400 if( p_vout->p_sys->p_display == NULL )
402 msg_Err( p_vout, "cannot open GGI default display" );
407 /* Find most appropriate mode */
408 p_vout->p_sys->mode.frames = 2; /* 2 buffers */
409 p_vout->p_sys->mode.visible.x = config_GetInt( p_vout, "width" );
410 p_vout->p_sys->mode.visible.y = config_GetInt( p_vout, "height" );
411 p_vout->p_sys->mode.virt.x = GGI_AUTO;
412 p_vout->p_sys->mode.virt.y = GGI_AUTO;
413 p_vout->p_sys->mode.size.x = GGI_AUTO;
414 p_vout->p_sys->mode.size.y = GGI_AUTO;
415 p_vout->p_sys->mode.graphtype = GT_15BIT; /* minimum usable depth */
416 p_vout->p_sys->mode.dpp.x = GGI_AUTO;
417 p_vout->p_sys->mode.dpp.y = GGI_AUTO;
418 ggiCheckMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode );
420 /* FIXME: Check that returned mode has some minimum properties */
423 if( ggiSetMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode ) )
425 msg_Err( p_vout, "cannot set GGI mode" );
426 ggiClose( p_vout->p_sys->p_display );
431 /* Check buffers properties */
432 p_vout->p_sys->b_must_acquire = 0;
433 for( i_index = 0; i_index < 2; i_index++ )
435 /* Get buffer address */
436 p_vout->p_sys->pp_buffer[ i_index ] =
437 (ggi_directbuffer *)ggiDBGetBuffer( p_vout->p_sys->p_display,
439 if( p_b[ i_index ] == NULL )
441 msg_Err( p_vout, "double buffering is not possible" );
442 ggiClose( p_vout->p_sys->p_display );
447 /* Check buffer properties */
448 if( ! ( p_b[ i_index ]->type & GGI_DB_SIMPLE_PLB )
449 || ( p_b[ i_index ]->page_size != 0 )
450 || ( p_b[ i_index ]->write == NULL )
451 || ( p_b[ i_index ]->noaccess != 0 )
452 || ( p_b[ i_index ]->align != 0 ) )
454 msg_Err( p_vout, "incorrect video memory type" );
455 ggiClose( p_vout->p_sys->p_display );
460 /* Check if buffer needs to be acquired before write */
461 if( ggiResourceMustAcquire( p_b[ i_index ]->resource ) )
463 p_vout->p_sys->b_must_acquire = 1;
467 /* Set graphic context colors */
468 col_fg.r = col_fg.g = col_fg.b = -1;
469 col_bg.r = col_bg.g = col_bg.b = 0;
470 if( ggiSetGCForeground(p_vout->p_sys->p_display,
471 ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
472 ggiSetGCBackground(p_vout->p_sys->p_display,
473 ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
475 msg_Err( p_vout, "cannot set colors" );
476 ggiClose( p_vout->p_sys->p_display );
481 /* Set clipping for text */
482 if( ggiSetGCClipping( p_vout->p_sys->p_display, 0, 0,
483 p_vout->p_sys->mode.visible.x,
484 p_vout->p_sys->mode.visible.y ) )
486 msg_Err( p_vout, "cannot set clipping" );
487 ggiClose( p_vout->p_sys->p_display );
492 /* FIXME: set palette in 8bpp */
493 p_vout->p_sys->i_bits_per_pixel = p_b[ 0 ]->buffer.plb.pixelformat->depth;
499 /*****************************************************************************
500 * CloseDisplay: close and reset GGI device
501 *****************************************************************************
502 * This function returns all resources allocated by OpenDisplay and restore
503 * the original state of the device.
504 *****************************************************************************/
505 static void CloseDisplay( vout_thread_t *p_vout )
507 /* Restore original mode and close display */
508 ggiClose( p_vout->p_sys->p_display );
514 /*****************************************************************************
515 * SetPalette: sets an 8 bpp palette
516 *****************************************************************************/
517 static void SetPalette( vout_thread_t *p_vout,
518 uint16_t *red, uint16_t *green, uint16_t *blue )
520 ggi_color colors[256];
523 /* Fill colors with color information */
524 for( i = 0; i < 256; i++ )
526 colors[ i ].r = red[ i ];
527 colors[ i ].g = green[ i ];
528 colors[ i ].b = blue[ i ];
533 if( ggiSetPalette( p_vout->p_sys->p_display, 0, 256, colors ) < 0 )
535 msg_Err( p_vout, "failed to set palette" );