]> git.sesse.net Git - vlc/blob - modules/video_output/ggi.c
Plugins: include vlc_common.h directly instead of vlc/vlc.h
[vlc] / modules / video_output / ggi.c
1 /*****************************************************************************
2  * ggi.c : GGI plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <errno.h>                                                 /* ENOMEM */
29
30 #include <ggi/ggi.h>
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_interface.h>
39 #include <vlc_vout.h>
40
41 /*****************************************************************************
42  * Local prototypes
43  *****************************************************************************/
44 static int  Create    ( vlc_object_t * );
45 static void Destroy   ( vlc_object_t * );
46
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 * );
51
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 * );
55
56 /*****************************************************************************
57  * Module descriptor
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.")
64
65 vlc_module_begin();
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 );
70 vlc_module_end();
71
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  *****************************************************************************/
78 struct vout_sys_t
79 {
80     /* GGI system information */
81     ggi_visual_t        p_display;                         /* display device */
82
83     ggi_mode            mode;                             /* mode descriptor */
84     int                 i_bits_per_pixel;
85
86     /* Buffer information */
87     ggi_directbuffer *  pp_buffer[2];                             /* buffers */
88     int                 i_index;
89
90     bool          b_must_acquire;   /* must be acquired before writing */
91 };
92
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
98  * mode actually used.
99  *****************************************************************************/
100 static int Create( vlc_object_t *p_this )
101 {
102     vout_thread_t *p_vout = (vout_thread_t *)p_this;
103
104     /* Allocate structure */
105     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
106     if( p_vout->p_sys == NULL )
107         return( 1 );
108
109     /* Open and initialize device */
110     if( OpenDisplay( p_vout ) )
111     {
112         msg_Err( p_vout, "cannot initialize GGI display" );
113         free( p_vout->p_sys );
114         return( 1 );
115     }
116
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;
122
123     return( 0 );
124 }
125
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 )
132 {
133 #define p_b p_vout->p_sys->pp_buffer
134     int i_index;
135     picture_t *p_pic;
136
137     I_OUTPUTPICTURES = 0;
138
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
142                                * VOUT_ASPECT_FACTOR
143                                / p_vout->p_sys->mode.visible.y;
144
145     switch( p_vout->p_sys->i_bits_per_pixel )
146     {
147         case 8:
148             p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
149             p_vout->output.pf_setpalette = SetPalette;
150             break;
151         case 15:
152             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
153         case 16:
154             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
155         case 24:
156             p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
157         case 32:
158             p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
159         default:
160             msg_Err( p_vout, "unknown screen depth %i",
161                      p_vout->p_sys->i_bits_per_pixel );
162             return 0;
163     }
164
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;
169
170     p_pic = NULL;
171
172     /* Find an empty picture slot */
173     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
174     {
175         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
176         {
177             p_pic = p_vout->p_picture + i_index;
178             break;
179         }
180     }
181
182     if( p_pic == NULL )
183     {
184         return 0;
185     }
186
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;
194
195     p_pic->p->i_pitch = p_b[ 0 ]->buffer.plb.stride;
196
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 )
200     {
201         p_pic->p->i_visible_pitch = p_b[ 0 ]->buffer.plb.pixelformat->size
202                                      / 8 * p_vout->p_sys->mode.visible.x;
203     }
204     else
205     {
206         p_pic->p->i_visible_pitch = p_b[ 0 ]->buffer.plb.stride;
207     }
208
209     p_pic->i_planes = 1;
210
211     p_pic->i_status = DESTROYED_PICTURE;
212     p_pic->i_type   = DIRECT_PICTURE;
213
214     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
215
216     I_OUTPUTPICTURES++;
217
218     /* Acquire first buffer */
219     if( p_vout->p_sys->b_must_acquire )
220     {
221         ggiResourceAcquire( p_b[ 0 ]->resource,
222                             GGI_ACTYPE_WRITE );
223     }
224
225     /* Listen to the keyboard and the mouse buttons */
226     ggiSetEventMask( p_vout->p_sys->p_display,
227                      emKeyboard | emPtrButtonPress | emPtrButtonRelease );
228
229     /* Set asynchronous display mode -- usually quite faster */
230     ggiAddFlags( p_vout->p_sys->p_display, GGIFLAG_ASYNC );
231
232     return( 0 );
233 #undef p_b
234 }
235
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 )
242 {
243 #define p_b p_vout->p_sys->pp_buffer
244     /* Release buffer */
245     if( p_vout->p_sys->b_must_acquire )
246     {
247         ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
248     }
249 #undef p_b
250 }
251
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 )
258 {
259     vout_thread_t *p_vout = (vout_thread_t *)p_this;
260
261     CloseDisplay( p_vout );
262
263     free( p_vout->p_sys );
264 }
265
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 )
273 {
274     struct timeval tv = { 0, 1000 };                        /* 1 millisecond */
275     gii_event_mask mask;
276     gii_event      event;
277     vlc_value_t    val;
278
279     mask = emKeyboard | emPtrButtonPress | emPtrButtonRelease;
280
281     ggiEventPoll( p_vout->p_sys->p_display, mask, &tv );
282
283     while( ggiEventsQueued( p_vout->p_sys->p_display, mask) )
284     {
285         ggiEventRead( p_vout->p_sys->p_display, &event, mask);
286
287         switch( event.any.type )
288         {
289             case evKeyRelease:
290
291                 switch( event.key.sym )
292                 {
293                     case 'q':
294                     case 'Q':
295                     case GIIUC_Escape:
296                         vlc_object_kill( p_vout->p_libvlc );
297                         break;
298
299                     default:
300                         break;
301                 }
302                 break;
303
304             case evPtrButtonRelease:
305
306                 switch( event.pbutton.button )
307                 {
308                     case GII_PBUTTON_LEFT:
309                         val.b_bool = true;
310                         var_Set( p_vout, "mouse-clicked", val );
311                         break;
312
313                     case GII_PBUTTON_RIGHT:
314                         {
315                             intf_thread_t *p_intf;
316                             p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
317                                                               FIND_ANYWHERE );
318                             if( p_intf )
319                             {
320                                 p_intf->b_menu_change = 1;
321                                 vlc_object_release( p_intf );
322                             }
323                         }
324                         break;
325                 }
326                 break;
327
328             default:
329                 break;
330         }
331     }
332
333     return( 0 );
334 }
335
336 /*****************************************************************************
337  * Display: displays previously rendered output
338  *****************************************************************************/
339 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
340 {
341 #define p_b p_vout->p_sys->pp_buffer
342     p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
343
344     /* Change display frame */
345     if( p_vout->p_sys->b_must_acquire )
346     {
347         ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
348     }
349     ggiSetDisplayFrame( p_vout->p_sys->p_display,
350                         p_b[ p_vout->p_sys->i_index ]->frame );
351
352     /* Swap buffers and change write frame */
353     p_vout->p_sys->i_index ^= 1;
354     p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
355
356     if( p_vout->p_sys->b_must_acquire )
357     {
358         ggiResourceAcquire( p_b[ p_vout->p_sys->i_index ]->resource,
359                             GGI_ACTYPE_WRITE );
360     }
361     ggiSetWriteFrame( p_vout->p_sys->p_display,
362                       p_b[ p_vout->p_sys->i_index ]->frame );
363
364     /* Flush the output so that it actually displays */
365     ggiFlush( p_vout->p_sys->p_display );
366 #undef p_b
367 }
368
369 /* following functions are local */
370
371 /*****************************************************************************
372  * OpenDisplay: open and initialize GGI device
373  *****************************************************************************
374  * Open and initialize display according to preferences specified in the vout
375  * thread fields.
376  *****************************************************************************/
377 static int OpenDisplay( vout_thread_t *p_vout )
378 {
379 #define p_b p_vout->p_sys->pp_buffer
380     ggi_color   col_fg;                                  /* foreground color */
381     ggi_color   col_bg;                                  /* background color */
382     int         i_index;                               /* all purposes index */
383     char        *psz_display;
384
385     /* Initialize library */
386     if( ggiInit() )
387     {
388         msg_Err( p_vout, "cannot initialize GGI library" );
389         return( 1 );
390     }
391
392     /* Open display */
393     psz_display = config_GetPsz( p_vout, "ggi_display" );
394
395     p_vout->p_sys->p_display = ggiOpen( psz_display, NULL );
396     free( psz_display );
397
398     if( p_vout->p_sys->p_display == NULL )
399     {
400         msg_Err( p_vout, "cannot open GGI default display" );
401         ggiExit();
402         return( 1 );
403     }
404
405     /* Find most appropriate mode */
406     p_vout->p_sys->mode.frames =    2;                          /* 2 buffers */
407     p_vout->p_sys->mode.visible.x = config_GetInt( p_vout, "width" );
408     p_vout->p_sys->mode.visible.y = config_GetInt( p_vout, "height" );
409     p_vout->p_sys->mode.virt.x =    GGI_AUTO;
410     p_vout->p_sys->mode.virt.y =    GGI_AUTO;
411     p_vout->p_sys->mode.size.x =    GGI_AUTO;
412     p_vout->p_sys->mode.size.y =    GGI_AUTO;
413     p_vout->p_sys->mode.graphtype = GT_15BIT;        /* minimum usable depth */
414     p_vout->p_sys->mode.dpp.x =     GGI_AUTO;
415     p_vout->p_sys->mode.dpp.y =     GGI_AUTO;
416     ggiCheckMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode );
417
418     /* FIXME: Check that returned mode has some minimum properties */
419
420     /* Set mode */
421     if( ggiSetMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode ) )
422     {
423         msg_Err( p_vout, "cannot set GGI mode" );
424         ggiClose( p_vout->p_sys->p_display );
425         ggiExit();
426         return( 1 );
427     }
428
429     /* Check buffers properties */
430     p_vout->p_sys->b_must_acquire = 0;
431     for( i_index = 0; i_index < 2; i_index++ )
432     {
433         /* Get buffer address */
434         p_vout->p_sys->pp_buffer[ i_index ] =
435             (ggi_directbuffer *)ggiDBGetBuffer( p_vout->p_sys->p_display,
436                                                 i_index );
437         if( p_b[ i_index ] == NULL )
438         {
439             msg_Err( p_vout, "double buffering is not possible" );
440             ggiClose( p_vout->p_sys->p_display );
441             ggiExit();
442             return( 1 );
443         }
444
445         /* Check buffer properties */
446         if( ! ( p_b[ i_index ]->type & GGI_DB_SIMPLE_PLB )
447            || ( p_b[ i_index ]->page_size != 0 )
448            || ( p_b[ i_index ]->write == NULL )
449            || ( p_b[ i_index ]->noaccess != 0 )
450            || ( p_b[ i_index ]->align != 0 ) )
451         {
452             msg_Err( p_vout, "incorrect video memory type" );
453             ggiClose( p_vout->p_sys->p_display );
454             ggiExit();
455             return( 1 );
456         }
457
458         /* Check if buffer needs to be acquired before write */
459         if( ggiResourceMustAcquire( p_b[ i_index ]->resource ) )
460         {
461             p_vout->p_sys->b_must_acquire = 1;
462         }
463     }
464
465     /* Set graphic context colors */
466     col_fg.r = col_fg.g = col_fg.b = -1;
467     col_bg.r = col_bg.g = col_bg.b = 0;
468     if( ggiSetGCForeground(p_vout->p_sys->p_display,
469                            ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
470         ggiSetGCBackground(p_vout->p_sys->p_display,
471                            ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
472     {
473         msg_Err( p_vout, "cannot set colors" );
474         ggiClose( p_vout->p_sys->p_display );
475         ggiExit();
476         return( 1 );
477     }
478
479     /* Set clipping for text */
480     if( ggiSetGCClipping( p_vout->p_sys->p_display, 0, 0,
481                           p_vout->p_sys->mode.visible.x,
482                           p_vout->p_sys->mode.visible.y ) )
483     {
484         msg_Err( p_vout, "cannot set clipping" );
485         ggiClose( p_vout->p_sys->p_display );
486         ggiExit();
487         return( 1 );
488     }
489
490     /* FIXME: set palette in 8bpp */
491     p_vout->p_sys->i_bits_per_pixel = p_b[ 0 ]->buffer.plb.pixelformat->depth;
492
493     return( 0 );
494 #undef p_b
495 }
496
497 /*****************************************************************************
498  * CloseDisplay: close and reset GGI device
499  *****************************************************************************
500  * This function returns all resources allocated by OpenDisplay and restore
501  * the original state of the device.
502  *****************************************************************************/
503 static void CloseDisplay( vout_thread_t *p_vout )
504 {
505     /* Restore original mode and close display */
506     ggiClose( p_vout->p_sys->p_display );
507
508     /* Exit library */
509     ggiExit();
510 }
511
512 /*****************************************************************************
513  * SetPalette: sets an 8 bpp palette
514  *****************************************************************************/
515 static void SetPalette( vout_thread_t *p_vout,
516                         uint16_t *red, uint16_t *green, uint16_t *blue )
517 {
518     ggi_color colors[256];
519     int i;
520
521     /* Fill colors with color information */
522     for( i = 0; i < 256; i++ )
523     {
524         colors[ i ].r = red[ i ];
525         colors[ i ].g = green[ i ];
526         colors[ i ].b = blue[ i ];
527         colors[ i ].a = 0;
528     }
529
530     /* Set palette */
531     if( ggiSetPalette( p_vout->p_sys->p_display, 0, 256, colors ) < 0 )
532     {
533         msg_Err( p_vout, "failed to set palette" );
534     }
535 }
536