vout modules: remove uneeded includes.
[vlc.git] / modules / video_output / ggi.c
1 /*****************************************************************************
2  * ggi.c : GGI plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2009 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 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <ggi/ggi.h>
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_interface.h>
37 #include <vlc_vout.h>
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static int  Create    ( vlc_object_t * );
43 static void Destroy   ( vlc_object_t * );
44
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 * );
49
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 * );
53
54 /*****************************************************************************
55  * Module descriptor
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.")
62
63 vlc_module_begin ()
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 )
68 vlc_module_end ()
69
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  *****************************************************************************/
76 struct vout_sys_t
77 {
78     /* GGI system information */
79     ggi_visual_t        p_display;                         /* display device */
80
81     ggi_mode            mode;                             /* mode descriptor */
82     int                 i_bits_per_pixel;
83
84     /* Buffer information */
85     ggi_directbuffer *  pp_buffer[2];                             /* buffers */
86     int                 i_index;
87
88     bool          b_must_acquire;   /* must be acquired before writing */
89 };
90
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
96  * mode actually used.
97  *****************************************************************************/
98 static int Create( vlc_object_t *p_this )
99 {
100     vout_thread_t *p_vout = (vout_thread_t *)p_this;
101
102     /* Allocate structure */
103     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
104     if( p_vout->p_sys == NULL )
105         return VLC_ENOMEM;
106
107     /* Open and initialize device */
108     if( OpenDisplay( p_vout ) )
109     {
110         msg_Err( p_vout, "cannot initialize GGI display" );
111         free( p_vout->p_sys );
112         return VLC_EGENERIC;
113     }
114
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;
120
121     return VLC_SUCCESS;
122 }
123
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 )
130 {
131 #define p_b p_vout->p_sys->pp_buffer
132     int i_index;
133     picture_t *p_pic;
134
135     I_OUTPUTPICTURES = 0;
136
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
140                                * VOUT_ASPECT_FACTOR
141                                / p_vout->p_sys->mode.visible.y;
142
143     switch( p_vout->p_sys->i_bits_per_pixel )
144     {
145         case 8:
146             p_vout->output.i_chroma = VLC_CODEC_RGB8;
147             p_vout->output.pf_setpalette = SetPalette;
148             break;
149         case 15:
150             p_vout->output.i_chroma = VLC_CODEC_RGB15; break;
151         case 16:
152             p_vout->output.i_chroma = VLC_CODEC_RGB16; break;
153         case 24:
154             p_vout->output.i_chroma = VLC_CODEC_RGB24; break;
155         case 32:
156             p_vout->output.i_chroma = VLC_CODEC_RGB32; break;
157         default:
158             msg_Err( p_vout, "unknown screen depth %i",
159                      p_vout->p_sys->i_bits_per_pixel );
160             return VLC_EGENERIC;
161     }
162
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;
167
168     p_pic = NULL;
169
170     /* Find an empty picture slot */
171     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
172     {
173         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
174         {
175             p_pic = p_vout->p_picture + i_index;
176             break;
177         }
178     }
179
180     if( p_pic == NULL )
181     {
182         return VLC_EGENERIC;
183     }
184
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;
192
193     p_pic->p->i_pitch = p_b[ 0 ]->buffer.plb.stride;
194
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 )
198     {
199         p_pic->p->i_visible_pitch = p_b[ 0 ]->buffer.plb.pixelformat->size
200                                      / 8 * p_vout->p_sys->mode.visible.x;
201     }
202     else
203     {
204         p_pic->p->i_visible_pitch = p_b[ 0 ]->buffer.plb.stride;
205     }
206
207     p_pic->i_planes = 1;
208
209     p_pic->i_status = DESTROYED_PICTURE;
210     p_pic->i_type   = DIRECT_PICTURE;
211
212     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
213
214     I_OUTPUTPICTURES++;
215
216     /* Acquire first buffer */
217     if( p_vout->p_sys->b_must_acquire )
218     {
219         ggiResourceAcquire( p_b[ 0 ]->resource,
220                             GGI_ACTYPE_WRITE );
221     }
222
223     /* Listen to the keyboard and the mouse buttons */
224     ggiSetEventMask( p_vout->p_sys->p_display,
225                      emKeyboard | emPtrButtonPress | emPtrButtonRelease );
226
227     /* Set asynchronous display mode -- usually quite faster */
228     ggiAddFlags( p_vout->p_sys->p_display, GGIFLAG_ASYNC );
229
230     return VLC_SUCCESS;
231 #undef p_b
232 }
233
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 )
240 {
241 #define p_b p_vout->p_sys->pp_buffer
242     /* Release buffer */
243     if( p_vout->p_sys->b_must_acquire )
244     {
245         ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
246     }
247 #undef p_b
248 }
249
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 )
256 {
257     vout_thread_t *p_vout = (vout_thread_t *)p_this;
258
259     CloseDisplay( p_vout );
260
261     free( p_vout->p_sys );
262 }
263
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 )
271 {
272     struct timeval tv = { 0, 1000 };                        /* 1 millisecond */
273     gii_event_mask mask;
274     gii_event      event;
275
276     mask = emKeyboard | emPtrButtonPress | emPtrButtonRelease;
277
278     ggiEventPoll( p_vout->p_sys->p_display, mask, &tv );
279
280     while( ggiEventsQueued( p_vout->p_sys->p_display, mask) )
281     {
282         ggiEventRead( p_vout->p_sys->p_display, &event, mask);
283
284         switch( event.any.type )
285         {
286             case evKeyRelease:
287
288                 switch( event.key.sym )
289                 {
290                     case 'q':
291                     case 'Q':
292                     case GIIUC_Escape:
293                         libvlc_Quit( p_vout->p_libvlc );
294                         break;
295
296                     default:
297                         break;
298                 }
299                 break;
300
301             case evPtrButtonRelease:
302
303                 switch( event.pbutton.button )
304                 {
305                     case GII_PBUTTON_LEFT:
306                         var_SetBool( p_vout, "mouse-clicked", true );
307                         break;
308
309                     case GII_PBUTTON_RIGHT:
310                         var_SetBool( p_vout->p_libvlc, "intf-popupmenu", true );
311                         break;
312                 }
313                 break;
314
315             default:
316                 break;
317         }
318     }
319
320     return( 0 );
321 }
322
323 /*****************************************************************************
324  * Display: displays previously rendered output
325  *****************************************************************************/
326 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
327 {
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;
330
331     /* Change display frame */
332     if( p_vout->p_sys->b_must_acquire )
333     {
334         ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
335     }
336     ggiSetDisplayFrame( p_vout->p_sys->p_display,
337                         p_b[ p_vout->p_sys->i_index ]->frame );
338
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;
342
343     if( p_vout->p_sys->b_must_acquire )
344     {
345         ggiResourceAcquire( p_b[ p_vout->p_sys->i_index ]->resource,
346                             GGI_ACTYPE_WRITE );
347     }
348     ggiSetWriteFrame( p_vout->p_sys->p_display,
349                       p_b[ p_vout->p_sys->i_index ]->frame );
350
351     /* Flush the output so that it actually displays */
352     ggiFlush( p_vout->p_sys->p_display );
353 #undef p_b
354 }
355
356 /* following functions are local */
357
358 /*****************************************************************************
359  * OpenDisplay: open and initialize GGI device
360  *****************************************************************************
361  * Open and initialize display according to preferences specified in the vout
362  * thread fields.
363  *****************************************************************************/
364 static int OpenDisplay( vout_thread_t *p_vout )
365 {
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 */
370     char        *psz_display;
371
372     /* Initialize library */
373     if( ggiInit() )
374     {
375         msg_Err( p_vout, "cannot initialize GGI library" );
376         return VLC_EGENERIC;
377     }
378
379     /* Open display */
380     psz_display = config_GetPsz( p_vout, "ggi-display" );
381
382     p_vout->p_sys->p_display = ggiOpen( psz_display, NULL );
383     free( psz_display );
384
385     if( p_vout->p_sys->p_display == NULL )
386     {
387         msg_Err( p_vout, "cannot open GGI default display" );
388         ggiExit();
389         return VLC_EGENERIC;
390     }
391
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 );
404
405     /* FIXME: Check that returned mode has some minimum properties */
406
407     /* Set mode */
408     if( ggiSetMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode ) )
409     {
410         msg_Err( p_vout, "cannot set GGI mode" );
411         ggiClose( p_vout->p_sys->p_display );
412         ggiExit();
413         return VLC_EGENERIC;
414     }
415
416     /* Check buffers properties */
417     p_vout->p_sys->b_must_acquire = 0;
418     for( i_index = 0; i_index < 2; i_index++ )
419     {
420         /* Get buffer address */
421         p_vout->p_sys->pp_buffer[ i_index ] =
422             (ggi_directbuffer *)ggiDBGetBuffer( p_vout->p_sys->p_display,
423                                                 i_index );
424         if( p_b[ i_index ] == NULL )
425         {
426             msg_Err( p_vout, "double buffering is not possible" );
427             ggiClose( p_vout->p_sys->p_display );
428             ggiExit();
429             return VLC_EGENERIC;
430         }
431
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 ) )
438         {
439             msg_Err( p_vout, "incorrect video memory type" );
440             ggiClose( p_vout->p_sys->p_display );
441             ggiExit();
442             return VLC_EGENERIC;
443         }
444
445         /* Check if buffer needs to be acquired before write */
446         if( ggiResourceMustAcquire( p_b[ i_index ]->resource ) )
447         {
448             p_vout->p_sys->b_must_acquire = 1;
449         }
450     }
451
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)) )
459     {
460         msg_Err( p_vout, "cannot set colors" );
461         ggiClose( p_vout->p_sys->p_display );
462         ggiExit();
463         return VLC_EGENERIC;
464     }
465
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 ) )
470     {
471         msg_Err( p_vout, "cannot set clipping" );
472         ggiClose( p_vout->p_sys->p_display );
473         ggiExit();
474         return VLC_EGENERIC;
475     }
476
477     /* FIXME: set palette in 8bpp */
478     p_vout->p_sys->i_bits_per_pixel = p_b[ 0 ]->buffer.plb.pixelformat->depth;
479
480     return VLC_SUCCESS;
481 #undef p_b
482 }
483
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 )
491 {
492     /* Restore original mode and close display */
493     ggiClose( p_vout->p_sys->p_display );
494
495     /* Exit library */
496     ggiExit();
497 }
498
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 )
504 {
505     ggi_color colors[256];
506     int i;
507
508     /* Fill colors with color information */
509     for( i = 0; i < 256; i++ )
510     {
511         colors[ i ].r = red[ i ];
512         colors[ i ].g = green[ i ];
513         colors[ i ].b = blue[ i ];
514         colors[ i ].a = 0;
515     }
516
517     /* Set palette */
518     if( ggiSetPalette( p_vout->p_sys->p_display, 0, 256, colors ) < 0 )
519     {
520         msg_Err( p_vout, "failed to set palette" );
521     }
522 }
523