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 );
62 set_description( "General Graphics Interface video output" );
63 set_capability( "video output", 30 );
64 set_callbacks( Create, Destroy );
67 /*****************************************************************************
68 * vout_sys_t: video output GGI method descriptor
69 *****************************************************************************
70 * This structure is part of the video output thread descriptor.
71 * It describes the GGI specific properties of an output thread.
72 *****************************************************************************/
75 /* GGI system information */
76 ggi_visual_t p_display; /* display device */
78 ggi_mode mode; /* mode descriptor */
81 /* Buffer information */
82 ggi_directbuffer * pp_buffer[2]; /* buffers */
85 vlc_bool_t b_must_acquire; /* must be acquired before writing */
88 /*****************************************************************************
89 * Create: allocate GGI video thread output method
90 *****************************************************************************
91 * This function allocate and initialize a GGI vout method. It uses some of the
92 * vout properties to choose the correct mode, and change them according to the
94 *****************************************************************************/
95 static int Create( vlc_object_t *p_this )
97 vout_thread_t *p_vout = (vout_thread_t *)p_this;
99 /* Allocate structure */
100 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
101 if( p_vout->p_sys == NULL )
103 msg_Err( p_vout, "out of memory" );
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_FOURCC('R','G','B','2');
147 p_vout->output.pf_setpalette = SetPalette;
150 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
152 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
154 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
156 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); 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 */
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 vlc_object_kill( p_vout->p_libvlc );
302 case evPtrButtonRelease:
304 switch( event.pbutton.button )
306 case GII_PBUTTON_LEFT:
307 val.b_bool = VLC_TRUE;
308 var_Set( p_vout, "mouse-clicked", val );
311 case GII_PBUTTON_RIGHT:
313 intf_thread_t *p_intf;
314 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
318 p_intf->b_menu_change = 1;
319 vlc_object_release( p_intf );
334 /*****************************************************************************
335 * Display: displays previously rendered output
336 *****************************************************************************/
337 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
339 #define p_b p_vout->p_sys->pp_buffer
340 p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
342 /* Change display frame */
343 if( p_vout->p_sys->b_must_acquire )
345 ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
347 ggiSetDisplayFrame( p_vout->p_sys->p_display,
348 p_b[ p_vout->p_sys->i_index ]->frame );
350 /* Swap buffers and change write frame */
351 p_vout->p_sys->i_index ^= 1;
352 p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
354 if( p_vout->p_sys->b_must_acquire )
356 ggiResourceAcquire( p_b[ p_vout->p_sys->i_index ]->resource,
359 ggiSetWriteFrame( p_vout->p_sys->p_display,
360 p_b[ p_vout->p_sys->i_index ]->frame );
362 /* Flush the output so that it actually displays */
363 ggiFlush( p_vout->p_sys->p_display );
367 /* following functions are local */
369 /*****************************************************************************
370 * OpenDisplay: open and initialize GGI device
371 *****************************************************************************
372 * Open and initialize display according to preferences specified in the vout
374 *****************************************************************************/
375 static int OpenDisplay( vout_thread_t *p_vout )
377 #define p_b p_vout->p_sys->pp_buffer
378 ggi_color col_fg; /* foreground color */
379 ggi_color col_bg; /* background color */
380 int i_index; /* all purposes index */
383 /* Initialize library */
386 msg_Err( p_vout, "cannot initialize GGI library" );
391 psz_display = config_GetPsz( p_vout, "ggi_display" );
393 p_vout->p_sys->p_display = ggiOpen( psz_display, NULL );
394 if( psz_display ) free( psz_display );
396 if( p_vout->p_sys->p_display == NULL )
398 msg_Err( p_vout, "cannot open GGI default display" );
403 /* Find most appropriate mode */
404 p_vout->p_sys->mode.frames = 2; /* 2 buffers */
405 p_vout->p_sys->mode.visible.x = config_GetInt( p_vout, "width" );
406 p_vout->p_sys->mode.visible.y = config_GetInt( p_vout, "height" );
407 p_vout->p_sys->mode.virt.x = GGI_AUTO;
408 p_vout->p_sys->mode.virt.y = GGI_AUTO;
409 p_vout->p_sys->mode.size.x = GGI_AUTO;
410 p_vout->p_sys->mode.size.y = GGI_AUTO;
411 p_vout->p_sys->mode.graphtype = GT_15BIT; /* minimum usable depth */
412 p_vout->p_sys->mode.dpp.x = GGI_AUTO;
413 p_vout->p_sys->mode.dpp.y = GGI_AUTO;
414 ggiCheckMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode );
416 /* FIXME: Check that returned mode has some minimum properties */
419 if( ggiSetMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode ) )
421 msg_Err( p_vout, "cannot set GGI mode" );
422 ggiClose( p_vout->p_sys->p_display );
427 /* Check buffers properties */
428 p_vout->p_sys->b_must_acquire = 0;
429 for( i_index = 0; i_index < 2; i_index++ )
431 /* Get buffer address */
432 p_vout->p_sys->pp_buffer[ i_index ] =
433 (ggi_directbuffer *)ggiDBGetBuffer( p_vout->p_sys->p_display,
435 if( p_b[ i_index ] == NULL )
437 msg_Err( p_vout, "double buffering is not possible" );
438 ggiClose( p_vout->p_sys->p_display );
443 /* Check buffer properties */
444 if( ! ( p_b[ i_index ]->type & GGI_DB_SIMPLE_PLB )
445 || ( p_b[ i_index ]->page_size != 0 )
446 || ( p_b[ i_index ]->write == NULL )
447 || ( p_b[ i_index ]->noaccess != 0 )
448 || ( p_b[ i_index ]->align != 0 ) )
450 msg_Err( p_vout, "incorrect video memory type" );
451 ggiClose( p_vout->p_sys->p_display );
456 /* Check if buffer needs to be acquired before write */
457 if( ggiResourceMustAcquire( p_b[ i_index ]->resource ) )
459 p_vout->p_sys->b_must_acquire = 1;
463 /* Set graphic context colors */
464 col_fg.r = col_fg.g = col_fg.b = -1;
465 col_bg.r = col_bg.g = col_bg.b = 0;
466 if( ggiSetGCForeground(p_vout->p_sys->p_display,
467 ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
468 ggiSetGCBackground(p_vout->p_sys->p_display,
469 ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
471 msg_Err( p_vout, "cannot set colors" );
472 ggiClose( p_vout->p_sys->p_display );
477 /* Set clipping for text */
478 if( ggiSetGCClipping( p_vout->p_sys->p_display, 0, 0,
479 p_vout->p_sys->mode.visible.x,
480 p_vout->p_sys->mode.visible.y ) )
482 msg_Err( p_vout, "cannot set clipping" );
483 ggiClose( p_vout->p_sys->p_display );
488 /* FIXME: set palette in 8bpp */
489 p_vout->p_sys->i_bits_per_pixel = p_b[ 0 ]->buffer.plb.pixelformat->depth;
495 /*****************************************************************************
496 * CloseDisplay: close and reset GGI device
497 *****************************************************************************
498 * This function returns all resources allocated by OpenDisplay and restore
499 * the original state of the device.
500 *****************************************************************************/
501 static void CloseDisplay( vout_thread_t *p_vout )
503 /* Restore original mode and close display */
504 ggiClose( p_vout->p_sys->p_display );
510 /*****************************************************************************
511 * SetPalette: sets an 8 bpp palette
512 *****************************************************************************/
513 static void SetPalette( vout_thread_t *p_vout,
514 uint16_t *red, uint16_t *green, uint16_t *blue )
516 ggi_color colors[256];
519 /* Fill colors with color information */
520 for( i = 0; i < 256; i++ )
522 colors[ i ].r = red[ i ];
523 colors[ i ].g = green[ i ];
524 colors[ i ].b = blue[ i ];
529 if( ggiSetPalette( p_vout->p_sys->p_display, 0, 256, colors ) < 0 )
531 msg_Err( p_vout, "failed to set palette" );