]> git.sesse.net Git - vlc/blob - modules/video_output/ggi.c
Remove _GNU_SOURCE and string.h too
[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 #include <vlc/vlc.h>
33 #include <vlc_interface.h>
34 #include <vlc_vout.h>
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static int  Create    ( vlc_object_t * );
40 static void Destroy   ( vlc_object_t * );
41
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 * );
46
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 * );
50
51 /*****************************************************************************
52  * Module descriptor
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.")
59
60 vlc_module_begin();
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 );
65 vlc_module_end();
66
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  *****************************************************************************/
73 struct vout_sys_t
74 {
75     /* GGI system information */
76     ggi_visual_t        p_display;                         /* display device */
77
78     ggi_mode            mode;                             /* mode descriptor */
79     int                 i_bits_per_pixel;
80
81     /* Buffer information */
82     ggi_directbuffer *  pp_buffer[2];                             /* buffers */
83     int                 i_index;
84
85     vlc_bool_t          b_must_acquire;   /* must be acquired before writing */
86 };
87
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
93  * mode actually used.
94  *****************************************************************************/
95 static int Create( vlc_object_t *p_this )
96 {
97     vout_thread_t *p_vout = (vout_thread_t *)p_this;
98
99     /* Allocate structure */
100     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
101     if( p_vout->p_sys == NULL )
102     {
103         msg_Err( p_vout, "out of memory" );
104         return( 1 );
105     }
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( 1 );
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( 0 );
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_FOURCC('R','G','B','2');
147             p_vout->output.pf_setpalette = SetPalette;
148             break;
149         case 15:
150             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
151         case 16:
152             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
153         case 24:
154             p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
155         case 32:
156             p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
157         default:
158             msg_Err( p_vout, "unknown screen depth %i",
159                      p_vout->p_sys->i_bits_per_pixel );
160             return 0;
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 0;
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( 0 );
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     vlc_value_t    val;
276
277     mask = emKeyboard | emPtrButtonPress | emPtrButtonRelease;
278
279     ggiEventPoll( p_vout->p_sys->p_display, mask, &tv );
280
281     while( ggiEventsQueued( p_vout->p_sys->p_display, mask) )
282     {
283         ggiEventRead( p_vout->p_sys->p_display, &event, mask);
284
285         switch( event.any.type )
286         {
287             case evKeyRelease:
288
289                 switch( event.key.sym )
290                 {
291                     case 'q':
292                     case 'Q':
293                     case GIIUC_Escape:
294                         vlc_object_kill( p_vout->p_libvlc );
295                         break;
296
297                     default:
298                         break;
299                 }
300                 break;
301
302             case evPtrButtonRelease:
303
304                 switch( event.pbutton.button )
305                 {
306                     case GII_PBUTTON_LEFT:
307                         val.b_bool = VLC_TRUE;
308                         var_Set( p_vout, "mouse-clicked", val );
309                         break;
310
311                     case GII_PBUTTON_RIGHT:
312                         {
313                             intf_thread_t *p_intf;
314                             p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
315                                                               FIND_ANYWHERE );
316                             if( p_intf )
317                             {
318                                 p_intf->b_menu_change = 1;
319                                 vlc_object_release( p_intf );
320                             }
321                         }
322                         break;
323                 }
324                 break;
325
326             default:
327                 break;
328         }
329     }
330
331     return( 0 );
332 }
333
334 /*****************************************************************************
335  * Display: displays previously rendered output
336  *****************************************************************************/
337 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
338 {
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;
341
342     /* Change display frame */
343     if( p_vout->p_sys->b_must_acquire )
344     {
345         ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
346     }
347     ggiSetDisplayFrame( p_vout->p_sys->p_display,
348                         p_b[ p_vout->p_sys->i_index ]->frame );
349
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;
353
354     if( p_vout->p_sys->b_must_acquire )
355     {
356         ggiResourceAcquire( p_b[ p_vout->p_sys->i_index ]->resource,
357                             GGI_ACTYPE_WRITE );
358     }
359     ggiSetWriteFrame( p_vout->p_sys->p_display,
360                       p_b[ p_vout->p_sys->i_index ]->frame );
361
362     /* Flush the output so that it actually displays */
363     ggiFlush( p_vout->p_sys->p_display );
364 #undef p_b
365 }
366
367 /* following functions are local */
368
369 /*****************************************************************************
370  * OpenDisplay: open and initialize GGI device
371  *****************************************************************************
372  * Open and initialize display according to preferences specified in the vout
373  * thread fields.
374  *****************************************************************************/
375 static int OpenDisplay( vout_thread_t *p_vout )
376 {
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 */
381     char        *psz_display;
382
383     /* Initialize library */
384     if( ggiInit() )
385     {
386         msg_Err( p_vout, "cannot initialize GGI library" );
387         return( 1 );
388     }
389
390     /* Open display */
391     psz_display = config_GetPsz( p_vout, "ggi_display" );
392
393     p_vout->p_sys->p_display = ggiOpen( psz_display, NULL );
394     if( psz_display ) free( psz_display );
395
396     if( p_vout->p_sys->p_display == NULL )
397     {
398         msg_Err( p_vout, "cannot open GGI default display" );
399         ggiExit();
400         return( 1 );
401     }
402
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 );
415
416     /* FIXME: Check that returned mode has some minimum properties */
417
418     /* Set mode */
419     if( ggiSetMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode ) )
420     {
421         msg_Err( p_vout, "cannot set GGI mode" );
422         ggiClose( p_vout->p_sys->p_display );
423         ggiExit();
424         return( 1 );
425     }
426
427     /* Check buffers properties */
428     p_vout->p_sys->b_must_acquire = 0;
429     for( i_index = 0; i_index < 2; i_index++ )
430     {
431         /* Get buffer address */
432         p_vout->p_sys->pp_buffer[ i_index ] =
433             (ggi_directbuffer *)ggiDBGetBuffer( p_vout->p_sys->p_display,
434                                                 i_index );
435         if( p_b[ i_index ] == NULL )
436         {
437             msg_Err( p_vout, "double buffering is not possible" );
438             ggiClose( p_vout->p_sys->p_display );
439             ggiExit();
440             return( 1 );
441         }
442
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 ) )
449         {
450             msg_Err( p_vout, "incorrect video memory type" );
451             ggiClose( p_vout->p_sys->p_display );
452             ggiExit();
453             return( 1 );
454         }
455
456         /* Check if buffer needs to be acquired before write */
457         if( ggiResourceMustAcquire( p_b[ i_index ]->resource ) )
458         {
459             p_vout->p_sys->b_must_acquire = 1;
460         }
461     }
462
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)) )
470     {
471         msg_Err( p_vout, "cannot set colors" );
472         ggiClose( p_vout->p_sys->p_display );
473         ggiExit();
474         return( 1 );
475     }
476
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 ) )
481     {
482         msg_Err( p_vout, "cannot set clipping" );
483         ggiClose( p_vout->p_sys->p_display );
484         ggiExit();
485         return( 1 );
486     }
487
488     /* FIXME: set palette in 8bpp */
489     p_vout->p_sys->i_bits_per_pixel = p_b[ 0 ]->buffer.plb.pixelformat->depth;
490
491     return( 0 );
492 #undef p_b
493 }
494
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 )
502 {
503     /* Restore original mode and close display */
504     ggiClose( p_vout->p_sys->p_display );
505
506     /* Exit library */
507     ggiExit();
508 }
509
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 )
515 {
516     ggi_color colors[256];
517     int i;
518
519     /* Fill colors with color information */
520     for( i = 0; i < 256; i++ )
521     {
522         colors[ i ].r = red[ i ];
523         colors[ i ].g = green[ i ];
524         colors[ i ].b = blue[ i ];
525         colors[ i ].a = 0;
526     }
527
528     /* Set palette */
529     if( ggiSetPalette( p_vout->p_sys->p_display, 0, 256, colors ) < 0 )
530     {
531         msg_Err( p_vout, "failed to set palette" );
532     }
533 }
534