]> git.sesse.net Git - vlc/blob - modules/video_output/ggi.c
Make mouse-moved and mouse-clicked coordinates, remove mouse-x and -y
[vlc] / 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                         /*FIXME
307                         var_SetCoords( p_vout, "mouse-clicked", x, y );*/
308                         break;
309
310                     case GII_PBUTTON_RIGHT:
311                         var_SetBool( p_vout->p_libvlc, "intf-popupmenu", true );
312                         break;
313                 }
314                 break;
315
316             default:
317                 break;
318         }
319     }
320
321     return( 0 );
322 }
323
324 /*****************************************************************************
325  * Display: displays previously rendered output
326  *****************************************************************************/
327 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
328 {
329 #define p_b p_vout->p_sys->pp_buffer
330     p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
331
332     /* Change display frame */
333     if( p_vout->p_sys->b_must_acquire )
334     {
335         ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
336     }
337     ggiSetDisplayFrame( p_vout->p_sys->p_display,
338                         p_b[ p_vout->p_sys->i_index ]->frame );
339
340     /* Swap buffers and change write frame */
341     p_vout->p_sys->i_index ^= 1;
342     p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
343
344     if( p_vout->p_sys->b_must_acquire )
345     {
346         ggiResourceAcquire( p_b[ p_vout->p_sys->i_index ]->resource,
347                             GGI_ACTYPE_WRITE );
348     }
349     ggiSetWriteFrame( p_vout->p_sys->p_display,
350                       p_b[ p_vout->p_sys->i_index ]->frame );
351
352     /* Flush the output so that it actually displays */
353     ggiFlush( p_vout->p_sys->p_display );
354 #undef p_b
355 }
356
357 /* following functions are local */
358
359 /*****************************************************************************
360  * OpenDisplay: open and initialize GGI device
361  *****************************************************************************
362  * Open and initialize display according to preferences specified in the vout
363  * thread fields.
364  *****************************************************************************/
365 static int OpenDisplay( vout_thread_t *p_vout )
366 {
367 #define p_b p_vout->p_sys->pp_buffer
368     ggi_color   col_fg;                                  /* foreground color */
369     ggi_color   col_bg;                                  /* background color */
370     int         i_index;                               /* all purposes index */
371     char        *psz_display;
372
373     /* Initialize library */
374     if( ggiInit() )
375     {
376         msg_Err( p_vout, "cannot initialize GGI library" );
377         return VLC_EGENERIC;
378     }
379
380     /* Open display */
381     psz_display = var_InheritString( p_vout, "ggi-display" );
382
383     p_vout->p_sys->p_display = ggiOpen( psz_display, NULL );
384     free( psz_display );
385
386     if( p_vout->p_sys->p_display == NULL )
387     {
388         msg_Err( p_vout, "cannot open GGI default display" );
389         ggiExit();
390         return VLC_EGENERIC;
391     }
392
393     /* Find most appropriate mode */
394     p_vout->p_sys->mode.frames =    2;                          /* 2 buffers */
395     p_vout->p_sys->mode.visible.x = var_InheritInteger( p_vout, "width" );
396     p_vout->p_sys->mode.visible.y = var_InheritInteger( p_vout, "height" );
397     p_vout->p_sys->mode.virt.x =    GGI_AUTO;
398     p_vout->p_sys->mode.virt.y =    GGI_AUTO;
399     p_vout->p_sys->mode.size.x =    GGI_AUTO;
400     p_vout->p_sys->mode.size.y =    GGI_AUTO;
401     p_vout->p_sys->mode.graphtype = GT_15BIT;        /* minimum usable depth */
402     p_vout->p_sys->mode.dpp.x =     GGI_AUTO;
403     p_vout->p_sys->mode.dpp.y =     GGI_AUTO;
404     ggiCheckMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode );
405
406     /* FIXME: Check that returned mode has some minimum properties */
407
408     /* Set mode */
409     if( ggiSetMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode ) )
410     {
411         msg_Err( p_vout, "cannot set GGI mode" );
412         ggiClose( p_vout->p_sys->p_display );
413         ggiExit();
414         return VLC_EGENERIC;
415     }
416
417     /* Check buffers properties */
418     p_vout->p_sys->b_must_acquire = 0;
419     for( i_index = 0; i_index < 2; i_index++ )
420     {
421         /* Get buffer address */
422         p_vout->p_sys->pp_buffer[ i_index ] =
423             (ggi_directbuffer *)ggiDBGetBuffer( p_vout->p_sys->p_display,
424                                                 i_index );
425         if( p_b[ i_index ] == NULL )
426         {
427             msg_Err( p_vout, "double buffering is not possible" );
428             ggiClose( p_vout->p_sys->p_display );
429             ggiExit();
430             return VLC_EGENERIC;
431         }
432
433         /* Check buffer properties */
434         if( ! ( p_b[ i_index ]->type & GGI_DB_SIMPLE_PLB )
435            || ( p_b[ i_index ]->page_size != 0 )
436            || ( p_b[ i_index ]->write == NULL )
437            || ( p_b[ i_index ]->noaccess != 0 )
438            || ( p_b[ i_index ]->align != 0 ) )
439         {
440             msg_Err( p_vout, "incorrect video memory type" );
441             ggiClose( p_vout->p_sys->p_display );
442             ggiExit();
443             return VLC_EGENERIC;
444         }
445
446         /* Check if buffer needs to be acquired before write */
447         if( ggiResourceMustAcquire( p_b[ i_index ]->resource ) )
448         {
449             p_vout->p_sys->b_must_acquire = 1;
450         }
451     }
452
453     /* Set graphic context colors */
454     col_fg.r = col_fg.g = col_fg.b = -1;
455     col_bg.r = col_bg.g = col_bg.b = 0;
456     if( ggiSetGCForeground(p_vout->p_sys->p_display,
457                            ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
458         ggiSetGCBackground(p_vout->p_sys->p_display,
459                            ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
460     {
461         msg_Err( p_vout, "cannot set colors" );
462         ggiClose( p_vout->p_sys->p_display );
463         ggiExit();
464         return VLC_EGENERIC;
465     }
466
467     /* Set clipping for text */
468     if( ggiSetGCClipping( p_vout->p_sys->p_display, 0, 0,
469                           p_vout->p_sys->mode.visible.x,
470                           p_vout->p_sys->mode.visible.y ) )
471     {
472         msg_Err( p_vout, "cannot set clipping" );
473         ggiClose( p_vout->p_sys->p_display );
474         ggiExit();
475         return VLC_EGENERIC;
476     }
477
478     /* FIXME: set palette in 8bpp */
479     p_vout->p_sys->i_bits_per_pixel = p_b[ 0 ]->buffer.plb.pixelformat->depth;
480
481     return VLC_SUCCESS;
482 #undef p_b
483 }
484
485 /*****************************************************************************
486  * CloseDisplay: close and reset GGI device
487  *****************************************************************************
488  * This function returns all resources allocated by OpenDisplay and restore
489  * the original state of the device.
490  *****************************************************************************/
491 static void CloseDisplay( vout_thread_t *p_vout )
492 {
493     /* Restore original mode and close display */
494     ggiClose( p_vout->p_sys->p_display );
495
496     /* Exit library */
497     ggiExit();
498 }
499
500 /*****************************************************************************
501  * SetPalette: sets an 8 bpp palette
502  *****************************************************************************/
503 static void SetPalette( vout_thread_t *p_vout,
504                         uint16_t *red, uint16_t *green, uint16_t *blue )
505 {
506     ggi_color colors[256];
507     int i;
508
509     /* Fill colors with color information */
510     for( i = 0; i < 256; i++ )
511     {
512         colors[ i ].r = red[ i ];
513         colors[ i ].g = green[ i ];
514         colors[ i ].b = blue[ i ];
515         colors[ i ].a = 0;
516     }
517
518     /* Set palette */
519     if( ggiSetPalette( p_vout->p_sys->p_display, 0, 256, colors ) < 0 )
520     {
521         msg_Err( p_vout, "failed to set palette" );
522     }
523 }
524