]> git.sesse.net Git - vlc/blob - modules/video_output/fb.c
Used VLC_CODEC_* and vlc_fourcc_GetCodec when suitable.
[vlc] / modules / video_output / fb.c
1 /*****************************************************************************
2  * fb.c : framebuffer plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Jean-Paul Saman
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
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <errno.h>                                                 /* ENOMEM */
34 #include <signal.h>                                      /* SIGUSR1, SIGUSR2 */
35 #include <fcntl.h>                                                 /* open() */
36 #include <unistd.h>                                               /* close() */
37
38 #include <termios.h>                                       /* struct termios */
39 #include <sys/ioctl.h>
40 #include <sys/mman.h>                                              /* mmap() */
41
42 #include <linux/fb.h>
43 #include <linux/vt.h>                                                /* VT_* */
44 #include <linux/kd.h>                                                 /* KD* */
45
46 #include <vlc_common.h>
47 #include <vlc_plugin.h>
48 #include <vlc_vout.h>
49 #include <vlc_interface.h>
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 static int  Create    ( vlc_object_t * );
55 static void Destroy   ( vlc_object_t * );
56
57 static int  Init      ( vout_thread_t * );
58 static void End       ( vout_thread_t * );
59 static int  Manage    ( vout_thread_t * );
60 static void Display   ( vout_thread_t *, picture_t * );
61 static int  Control   ( vout_thread_t *, int, va_list );
62
63 static int  NewPicture     ( vout_thread_t *, picture_t * );
64 static void FreePicture    ( vout_thread_t *, picture_t * );
65
66 static int  OpenDisplay    ( vout_thread_t * );
67 static void CloseDisplay   ( vout_thread_t * );
68 static void SwitchDisplay  ( int i_signal );
69 static void TextMode       ( int i_tty );
70 static void GfxMode        ( int i_tty );
71
72 #define MAX_DIRECTBUFFERS 1
73
74 /*****************************************************************************
75  * Module descriptor
76  *****************************************************************************/
77 #define FB_DEV_VAR "fbdev"
78
79 #define DEVICE_TEXT N_("Framebuffer device")
80 #define DEVICE_LONGTEXT N_( \
81     "Framebuffer device to use for rendering (usually /dev/fb0).")
82
83 #define TTY_TEXT N_("Run fb on current tty.")
84 #define TTY_LONGTEXT N_( \
85     "Run framebuffer on current TTY device (default enabled). " \
86     "(disable tty handling with caution)" )
87
88 #define CHROMA_TEXT N_("Chroma used.")
89 #define CHROMA_LONGTEXT N_( \
90     "Force use of a specific chroma for output. Default is I420." )
91
92 #define ASPECT_RATIO_TEXT N_("Video aspect ratio")
93 #define ASPECT_RATIO_LONGTEXT N_( \
94     "Aspect ratio of the video image (4:3, 16:9). Default is square pixels." )
95
96 #define FB_MODE_TEXT N_("Framebuffer resolution to use.")
97 #define FB_MODE_LONGTEXT N_( \
98     "Select the resolution for the framebuffer. Currently it supports " \
99     "the values 0=QCIF 1=CIF 2=NTSC 3=PAL, 4=auto (default 4=auto)" )
100
101 #define HW_ACCEL_TEXT N_("Framebuffer uses hw acceleration.")
102 #define HW_ACCEL_LONGTEXT N_( \
103     "If your framebuffer supports hardware acceleration or does double buffering " \
104     "in hardware then you must disable this option. It then does double buffering " \
105     "in software." )
106
107 vlc_module_begin ()
108     set_shortname( "Framebuffer" )
109     set_category( CAT_VIDEO )
110     set_subcategory( SUBCAT_VIDEO_VOUT )
111     add_file( FB_DEV_VAR, "/dev/fb0", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
112               false )
113     add_bool( "fb-tty", 1, NULL, TTY_TEXT, TTY_LONGTEXT, true )
114     add_string( "fb-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
115                 true )
116     add_string( "fb-aspect-ratio", NULL, NULL, ASPECT_RATIO_TEXT,
117                 ASPECT_RATIO_LONGTEXT, true )
118     add_integer( "fb-mode", 4, NULL, FB_MODE_TEXT, FB_MODE_LONGTEXT,
119                  true )
120     add_bool( "fb-hw-accel", true, NULL, HW_ACCEL_TEXT, HW_ACCEL_LONGTEXT,
121               true )
122     set_description( N_("GNU/Linux framebuffer video output") )
123     set_capability( "video output", 30 )
124     set_callbacks( Create, Destroy )
125 vlc_module_end ()
126
127 /*****************************************************************************
128  * vout_sys_t: video output framebuffer method descriptor
129  *****************************************************************************
130  * This structure is part of the video output thread descriptor.
131  * It describes the FB specific properties of an output thread.
132  *****************************************************************************/
133 struct vout_sys_t
134 {
135     /* System information */
136     int                 i_tty;                          /* tty device handle */
137     bool                b_tty;
138     struct termios      old_termios;
139
140     /* Original configuration information */
141     struct sigaction            sig_usr1;           /* USR1 previous handler */
142     struct sigaction            sig_usr2;           /* USR2 previous handler */
143     struct vt_mode              vt_mode;                 /* previous VT mode */
144
145     /* Framebuffer information */
146     int                         i_fd;                       /* device handle */
147     struct fb_var_screeninfo    old_info;       /* original mode information */
148     struct fb_var_screeninfo    var_info;        /* current mode information */
149     bool                        b_pan;     /* does device supports panning ? */
150     struct fb_cmap              fb_cmap;                /* original colormap */
151     uint16_t                    *p_palette;              /* original palette */
152     bool                        b_hw_accel;          /* has hardware support */
153
154     /* Video information */
155     uint32_t i_width;
156     uint32_t i_height;
157     int      i_aspect;
158     int      i_bytes_per_pixel;
159     bool     b_auto;       /* Automatically adjust video size to fb size */
160     vlc_fourcc_t i_chroma;
161
162     /* Video memory */
163     uint8_t    *p_video;                                      /* base adress */
164     size_t      i_page_size;                                    /* page size */
165 };
166
167 struct picture_sys_t
168 {
169     uint8_t *    p_data;                                      /* base adress */
170 };
171
172 /*****************************************************************************
173  * Create: allocates FB video thread output method
174  *****************************************************************************
175  * This function allocates and initializes a FB vout method.
176  *****************************************************************************/
177 static int Create( vlc_object_t *p_this )
178 {
179     vout_thread_t *p_vout = (vout_thread_t *)p_this;
180     vout_sys_t    *p_sys;
181     char          *psz_chroma;
182     char          *psz_aspect;
183     int           i_mode;
184     struct sigaction    sig_tty;                 /* sigaction for tty change */
185     struct vt_mode      vt_mode;                          /* vt current mode */
186     struct termios      new_termios;
187
188     /* Allocate instance and initialize some members */
189     p_vout->p_sys = p_sys = calloc( 1, sizeof( vout_sys_t ) );
190     if( p_vout->p_sys == NULL )
191         return VLC_ENOMEM;
192
193     p_sys->p_video = MAP_FAILED;
194
195     p_vout->pf_init = Init;
196     p_vout->pf_end = End;
197     p_vout->pf_manage = Manage;
198     p_vout->pf_render = NULL;
199     p_vout->pf_display = Display;
200     p_vout->pf_control = Control;
201
202     /* Does the framebuffer uses hw acceleration? */
203     p_sys->b_hw_accel = var_CreateGetBool( p_vout, "fb-hw-accel" );
204
205     /* Set tty and fb devices */
206     p_sys->i_tty = 0; /* 0 == /dev/tty0 == current console */
207     p_sys->b_tty = var_CreateGetBool( p_vout, "fb-tty" );
208 #ifndef WIN32
209 #if defined(HAVE_ISATTY)
210     /* Check that stdin is a TTY */
211     if( p_sys->b_tty && !isatty( 0 ) )
212     {
213         msg_Warn( p_vout, "fd 0 is not a TTY" );
214         free( p_sys );
215         return VLC_EGENERIC;
216     }
217     else
218     {
219         msg_Warn( p_vout, "disabling tty handling, use with caution because "
220                           "there is no way to return to the tty." );
221     }
222 #endif
223 #endif
224
225     psz_chroma = var_CreateGetNonEmptyString( p_vout, "fb-chroma" );
226     if( psz_chroma )
227     {
228         if( strlen( psz_chroma ) == 4 )
229         {
230             p_sys->i_chroma = vlc_fourcc_GetCodec( VIDEO_ES,
231                                                    VLC_FOURCC( psz_chroma[0],
232                                                                psz_chroma[1],
233                                                                psz_chroma[2],
234                                                                psz_chroma[3] ) );
235             msg_Dbg( p_vout, "forcing chroma '%s'", psz_chroma );
236         }
237         else
238         {
239             msg_Warn( p_vout, "invalid chroma (%s), using defaults.",
240                       psz_chroma );
241         }
242         free( psz_chroma );
243     }
244
245     p_sys->i_aspect = -1;
246     psz_aspect = var_CreateGetNonEmptyString( p_vout, "fb-aspect-ratio" );
247     if( psz_aspect )
248     {
249         char *psz_parser = strchr( psz_aspect, ':' );
250
251         if( psz_parser )
252         {
253             *psz_parser++ = '\0';
254             p_sys->i_aspect = ( atoi( psz_aspect )
255                               * VOUT_ASPECT_FACTOR ) / atoi( psz_parser );
256         }
257         msg_Dbg( p_vout, "using aspect ratio %d:%d",
258                   atoi( psz_aspect ), atoi( psz_parser ) );
259
260         free( psz_aspect );
261     }
262
263     p_sys->b_auto = false;
264     i_mode = var_CreateGetInteger( p_vout, "fb-mode" );
265     switch( i_mode )
266     {
267         case 0: /* QCIF */
268             p_sys->i_width  = 176;
269             p_sys->i_height = 144;
270             break;
271         case 1: /* CIF */
272             p_sys->i_width  = 352;
273             p_sys->i_height = 288;
274             break;
275         case 2: /* NTSC */
276             p_sys->i_width  = 640;
277             p_sys->i_height = 480;
278             break;
279         case 3: /* PAL */
280             p_sys->i_width  = 704;
281             p_sys->i_height = 576;
282             break;
283         case 4:
284         default:
285             p_sys->b_auto = true;
286      }
287
288     /* tty handling */
289     if( p_sys->b_tty )
290     {
291         GfxMode( p_sys->i_tty );
292
293         /* Set keyboard settings */
294         if( tcgetattr(0, &p_vout->p_sys->old_termios) == -1 )
295         {
296             msg_Err( p_vout, "tcgetattr failed" );
297         }
298
299         if( tcgetattr(0, &new_termios) == -1 )
300         {
301             msg_Err( p_vout, "tcgetattr failed" );
302         }
303
304         /* new_termios.c_lflag &= ~ (ICANON | ISIG);
305         new_termios.c_lflag |= (ECHO | ECHOCTL); */
306         new_termios.c_lflag &= ~ (ICANON);
307         new_termios.c_lflag &= ~(ECHO | ECHOCTL);
308         new_termios.c_iflag = 0;
309         new_termios.c_cc[VMIN] = 1;
310         new_termios.c_cc[VTIME] = 0;
311
312         if( tcsetattr(0, TCSAFLUSH, &new_termios) == -1 )
313         {
314             msg_Err( p_vout, "tcsetattr failed" );
315         }
316
317         ioctl( p_sys->i_tty, VT_RELDISP, VT_ACKACQ );
318
319         /* Set-up tty signal handler to be aware of tty changes */
320         memset( &sig_tty, 0, sizeof( sig_tty ) );
321         sig_tty.sa_handler = SwitchDisplay;
322         sigemptyset( &sig_tty.sa_mask );
323         if( sigaction( SIGUSR1, &sig_tty, &p_vout->p_sys->sig_usr1 ) ||
324             sigaction( SIGUSR2, &sig_tty, &p_vout->p_sys->sig_usr2 ) )
325         {
326             msg_Err( p_vout, "cannot set signal handler (%m)" );
327             tcsetattr(0, 0, &p_vout->p_sys->old_termios);
328             TextMode( p_sys->i_tty );
329             free( p_vout->p_sys );
330             return VLC_EGENERIC;
331         }
332
333         /* Set-up tty according to new signal handler */
334         if( -1 == ioctl( p_sys->i_tty, VT_GETMODE, &p_vout->p_sys->vt_mode ) )
335         {
336             msg_Err( p_vout, "cannot get terminal mode (%m)" );
337             sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
338             sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
339             tcsetattr(0, 0, &p_vout->p_sys->old_termios);
340             TextMode( p_sys->i_tty );
341             free( p_vout->p_sys );
342             return VLC_EGENERIC;
343         }
344         memcpy( &vt_mode, &p_vout->p_sys->vt_mode, sizeof( vt_mode ) );
345         vt_mode.mode   = VT_PROCESS;
346         vt_mode.waitv  = 0;
347         vt_mode.relsig = SIGUSR1;
348         vt_mode.acqsig = SIGUSR2;
349
350         if( -1 == ioctl( p_sys->i_tty, VT_SETMODE, &vt_mode ) )
351         {
352             msg_Err( p_vout, "cannot set terminal mode (%m)" );
353             sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
354             sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
355             tcsetattr(0, 0, &p_vout->p_sys->old_termios);
356             TextMode( p_sys->i_tty );
357             free( p_vout->p_sys );
358             return VLC_EGENERIC;
359         }
360     }
361
362     if( OpenDisplay( p_vout ) )
363     {
364         Destroy( VLC_OBJECT(p_vout) );
365         return VLC_EGENERIC;
366     }
367
368     return VLC_SUCCESS;
369 }
370
371
372 /*****************************************************************************
373  * Destroy: destroy FB video thread output method
374  *****************************************************************************
375  * Terminate an output method created by Create
376  *****************************************************************************/
377 static void Destroy( vlc_object_t *p_this )
378 {
379     vout_thread_t *p_vout = (vout_thread_t *)p_this;
380
381     CloseDisplay( p_vout );
382
383     if( p_vout->p_sys->b_tty )
384     {
385         /* Reset the terminal */
386         ioctl( p_vout->p_sys->i_tty, VT_SETMODE, &p_vout->p_sys->vt_mode );
387
388         /* Remove signal handlers */
389         sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
390         sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
391
392         /* Reset the keyboard state */
393         tcsetattr( 0, 0, &p_vout->p_sys->old_termios );
394
395         /* Return to text mode */
396         TextMode( p_vout->p_sys->i_tty );
397     }
398
399     /* Destroy structure */
400     free( p_vout->p_sys );
401 }
402
403 /*****************************************************************************
404  * NewPicture: allocate a picture
405  *****************************************************************************
406  * Returns 0 on success, -1 otherwise
407  *****************************************************************************/
408 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
409 {
410     /* We know the chroma, allocate a buffer which will be used
411      * directly by the decoder */
412     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
413     if( p_pic->p_sys == NULL )
414     {
415         return VLC_ENOMEM;
416     }
417
418     /* Fill in picture_t fields */
419     vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
420                       p_vout->output.i_width, p_vout->output.i_height,
421                       p_vout->output.i_aspect );
422
423     p_pic->p_sys->p_data = malloc( p_vout->p_sys->i_page_size );
424     if( !p_pic->p_sys->p_data )
425     {
426         free( p_pic->p_sys );
427         p_pic->p_sys = NULL;
428         return VLC_ENOMEM;
429     }
430
431     p_pic->p->p_pixels = (uint8_t*) p_pic->p_sys->p_data;
432
433     p_pic->p->i_pixel_pitch = p_vout->p_sys->i_bytes_per_pixel;
434     p_pic->p->i_lines = p_vout->p_sys->var_info.yres;
435     p_pic->p->i_visible_lines = p_vout->p_sys->var_info.yres;
436
437     if( p_vout->p_sys->var_info.xres_virtual )
438     {
439         p_pic->p->i_pitch = p_vout->p_sys->var_info.xres_virtual
440                              * p_vout->p_sys->i_bytes_per_pixel;
441     }
442     else
443     {
444         p_pic->p->i_pitch = p_vout->p_sys->var_info.xres
445                              * p_vout->p_sys->i_bytes_per_pixel;
446     }
447
448     p_pic->p->i_visible_pitch = p_vout->p_sys->var_info.xres
449                                  * p_vout->p_sys->i_bytes_per_pixel;
450     p_pic->i_planes = 1;
451
452     return VLC_SUCCESS;
453 }
454
455 /*****************************************************************************
456  * FreePicture: destroy a picture allocated with NewPicture
457  *****************************************************************************
458  * Destroy Image AND associated data.
459  *****************************************************************************/
460 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
461 {
462     VLC_UNUSED(p_vout);
463     free( p_pic->p_sys->p_data );
464     free( p_pic->p_sys );
465     p_pic->p_sys = NULL;
466 }
467
468 /*****************************************************************************
469  * Init: initialize framebuffer video thread output method
470  *****************************************************************************/
471 static int Init( vout_thread_t *p_vout )
472 {
473     vout_sys_t *p_sys = p_vout->p_sys;
474     int i_index;
475     picture_t *p_pic = NULL;
476
477     I_OUTPUTPICTURES = 0;
478
479     p_vout->output.i_width  = p_vout->render.i_width;
480     p_vout->output.i_height = p_vout->render.i_height;
481     p_vout->output.i_aspect = p_vout->render.i_aspect;
482
483     p_vout->fmt_out = p_vout->fmt_in;
484     if( p_sys->i_chroma == 0 )
485     {
486         /* Initialize the output structure: RGB with square pixels, whatever
487          * the input format is, since it's the only format we know */
488         switch( p_sys->var_info.bits_per_pixel )
489         {
490         case 8: /* FIXME: set the palette */
491             p_vout->output.i_chroma = VLC_CODEC_RGB8; break;
492         case 15:
493             p_vout->output.i_chroma = VLC_CODEC_RGB15; break;
494         case 16:
495             p_vout->output.i_chroma = VLC_CODEC_RGB16; break;
496         case 24:
497             p_vout->output.i_chroma = VLC_CODEC_RGB24; break;
498         case 32:
499             p_vout->output.i_chroma = VLC_CODEC_RGB32; break;
500         default:
501             msg_Err( p_vout, "unknown screen depth %i",
502                      p_vout->p_sys->var_info.bits_per_pixel );
503             return VLC_EGENERIC;
504         }
505
506         if( p_sys->var_info.bits_per_pixel != 8 )
507         {
508             p_vout->output.i_rmask = ( (1 << p_sys->var_info.red.length) - 1 )
509                                  << p_sys->var_info.red.offset;
510             p_vout->output.i_gmask = ( (1 << p_sys->var_info.green.length) - 1 )
511                                  << p_sys->var_info.green.offset;
512             p_vout->output.i_bmask = ( (1 << p_sys->var_info.blue.length) - 1 )
513                                  << p_sys->var_info.blue.offset;
514         }
515     }
516     else
517     {
518         p_vout->output.i_chroma = p_sys->i_chroma;
519     }
520     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
521
522     p_vout->output.i_width =
523     p_vout->fmt_out.i_width =
524     p_vout->fmt_out.i_visible_width = p_sys->i_width;
525     p_vout->output.i_height =
526     p_vout->fmt_out.i_height =
527     p_vout->fmt_out.i_visible_height = p_sys->i_height;
528
529     /* Assume we have square pixels */
530     if( p_sys->i_aspect < 0 )
531     {
532         p_vout->output.i_aspect = ( p_sys->i_width
533                                   * VOUT_ASPECT_FACTOR ) / p_sys->i_height;
534     }
535     else p_vout->output.i_aspect = p_sys->i_aspect;
536
537     p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_sar_den = 1;
538     p_vout->fmt_out.i_aspect  = p_vout->render.i_aspect = p_vout->output.i_aspect;
539     p_vout->fmt_out.i_x_offset= p_vout->fmt_out.i_y_offset = 0;
540
541     /* Clear the screen */
542     memset( p_sys->p_video, 0, p_sys->i_page_size );
543
544     if( !p_sys->b_hw_accel )
545     {
546         /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
547         while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
548         {
549             p_pic = NULL;
550
551             /* Find an empty picture slot */
552             for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
553             {
554                 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
555                 {
556                     p_pic = p_vout->p_picture + i_index;
557                     break;
558                 }
559             }
560
561             /* Allocate the picture */
562             if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
563             {
564                 break;
565             }
566
567             p_pic->i_status = DESTROYED_PICTURE;
568             p_pic->i_type   = DIRECT_PICTURE;
569
570             PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
571
572             I_OUTPUTPICTURES++;
573         }
574     }
575     else
576     {
577         /* Try to initialize 1 direct buffer */
578         p_pic = NULL;
579
580         /* Find an empty picture slot */
581         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
582         {
583             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
584             {
585                 p_pic = p_vout->p_picture + i_index;
586                 break;
587             }
588         }
589
590         /* Allocate the picture */
591         if( p_pic == NULL )
592         {
593             return VLC_EGENERIC;
594         }
595
596         /* We know the chroma, allocate a buffer which will be used
597         * directly by the decoder */
598         p_pic->p->p_pixels = p_vout->p_sys->p_video;
599         p_pic->p->i_pixel_pitch = p_vout->p_sys->i_bytes_per_pixel;
600         p_pic->p->i_lines = p_vout->p_sys->var_info.yres;
601         p_pic->p->i_visible_lines = p_vout->p_sys->var_info.yres;
602
603         if( p_vout->p_sys->var_info.xres_virtual )
604         {
605             p_pic->p->i_pitch = p_vout->p_sys->var_info.xres_virtual
606                                 * p_vout->p_sys->i_bytes_per_pixel;
607         }
608         else
609         {
610             p_pic->p->i_pitch = p_vout->p_sys->var_info.xres
611                                 * p_vout->p_sys->i_bytes_per_pixel;
612         }
613
614         p_pic->p->i_visible_pitch = p_vout->p_sys->var_info.xres
615                                     * p_vout->p_sys->i_bytes_per_pixel;
616         p_pic->i_planes = 1;
617         p_pic->i_status = DESTROYED_PICTURE;
618         p_pic->i_type   = DIRECT_PICTURE;
619
620         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
621
622         I_OUTPUTPICTURES++;
623     }
624
625     return VLC_SUCCESS;
626 }
627
628 /*****************************************************************************
629  * End: terminate framebuffer video thread output method
630  *****************************************************************************/
631 static void End( vout_thread_t *p_vout )
632 {
633     if( !p_vout->p_sys->b_hw_accel )
634     {
635         int i_index;
636
637         /* Free the direct buffers we allocated */
638         for( i_index = I_OUTPUTPICTURES ; i_index ; )
639         {
640             i_index--;
641             FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
642         }
643
644     }
645     /* Clear the screen */
646     memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
647 }
648
649 /*****************************************************************************
650  * Control: control facility for the vout
651  *****************************************************************************/
652 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
653 {
654     (void) p_vout; (void) i_query; (void) args;
655     return VLC_EGENERIC;
656 }
657
658 /*****************************************************************************
659  * Manage: handle FB events
660  *****************************************************************************
661  * This function should be called regularly by video output thread. It manages
662  * console events. It returns a non null value on error.
663  *****************************************************************************/
664 static int Manage( vout_thread_t *p_vout )
665 {
666 #if 0
667     uint8_t buf;
668
669     if ( read(0, &buf, 1) == 1)
670     {
671         switch( buf )
672         {
673         case 'q':
674             libvlc_Quit( p_vout->p_libvlc );
675             break;
676
677         default:
678             break;
679         }
680     }
681 #endif
682
683     /*
684      * Size change
685      */
686     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
687     {
688         msg_Dbg( p_vout, "reinitializing framebuffer screen" );
689         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
690
691         /* Destroy XImages to change their size */
692         End( p_vout );
693
694         /* Recreate XImages. If SysInit failed, the thread can't go on. */
695         if( Init( p_vout ) )
696         {
697             msg_Err( p_vout, "cannot reinit framebuffer screen" );
698             return VLC_EGENERIC;
699         }
700
701         /* Clear screen */
702         memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
703
704 #if 0
705         /* Tell the video output thread that it will need to rebuild YUV
706          * tables. This is needed since conversion buffer size may have changed */
707         p_vout->i_changes |= VOUT_YUV_CHANGE;
708 #endif
709     }
710
711     return VLC_SUCCESS;
712 }
713
714 /*****************************************************************************
715  * Display: displays previously rendered output
716  *****************************************************************************
717  * This function send the currently rendered image to FB image, waits until
718  * it is displayed and switch the two rendering buffers, preparing next frame.
719  *****************************************************************************/
720 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
721 {
722 static int panned=0;
723     /* swap the two Y offsets if the drivers supports panning */
724     if( p_vout->p_sys->b_pan )
725     {
726         p_vout->p_sys->var_info.yoffset = 0;
727         /*p_vout->p_sys->var_info.yoffset = p_vout->p_sys->var_info.yres; */
728
729         /* the X offset should be 0, but who knows ...
730          * some other app might have played with the framebuffer */
731         p_vout->p_sys->var_info.xoffset = 0;
732
733         if( panned < 0 )
734         {
735             ioctl( p_vout->p_sys->i_fd,
736                    FBIOPAN_DISPLAY, &p_vout->p_sys->var_info );
737             panned++;
738         }
739     }
740
741     if( !p_vout->p_sys->b_hw_accel )
742     {
743         vlc_memcpy( p_vout->p_sys->p_video, p_pic->p->p_pixels,
744                     p_vout->p_sys->i_page_size );
745     }
746 }
747
748 #if 0
749 static void SetPalette( vout_thread_t *p_vout, uint16_t *red, uint16_t *green,
750                                                uint16_t *blue, uint16_t *transp )
751 {
752     struct fb_cmap cmap = { 0, 256, red, green, blue, transp };
753
754     ioctl( p_vout->p_sys->i_fd, FBIOPUTCMAP, &cmap );
755 }
756 #endif
757
758 /* following functions are local */
759
760 /*****************************************************************************
761  * OpenDisplay: initialize framebuffer
762  *****************************************************************************/
763 static int OpenDisplay( vout_thread_t *p_vout )
764 {
765     vout_sys_t *p_sys = (vout_sys_t *) p_vout->p_sys;
766     char *psz_device;                             /* framebuffer device path */
767     struct fb_fix_screeninfo    fix_info;     /* framebuffer fix information */
768
769     /* Open framebuffer device */
770     if( !(psz_device = config_GetPsz( p_vout, FB_DEV_VAR )) )
771     {
772         msg_Err( p_vout, "don't know which fb device to open" );
773         return VLC_EGENERIC;
774     }
775
776     p_sys->i_fd = open( psz_device, O_RDWR);
777     if( p_sys->i_fd == -1 )
778     {
779         msg_Err( p_vout, "cannot open %s (%m)", psz_device );
780         free( psz_device );
781         return VLC_EGENERIC;
782     }
783     free( psz_device );
784
785     /* Get framebuffer device information */
786     if( ioctl( p_sys->i_fd, FBIOGET_VSCREENINFO, &p_sys->var_info ) )
787     {
788         msg_Err( p_vout, "cannot get fb info (%m)" );
789         close( p_sys->i_fd );
790         return VLC_EGENERIC;
791     }
792
793     memcpy( &p_sys->old_info, &p_sys->var_info,
794             sizeof( struct fb_var_screeninfo ) );
795
796     /* Get some info on the framebuffer itself */
797     if( !p_sys->b_auto )
798     {
799         p_sys->var_info.xres = p_sys->var_info.xres_virtual = p_sys->i_width;
800         p_sys->var_info.yres = p_sys->var_info.yres_virtual = p_sys->i_height;
801         p_vout->fmt_out.i_width = p_sys->i_width;
802         p_vout->fmt_out.i_height = p_sys->i_height;
803     }
804
805     /* Set some attributes */
806     p_sys->var_info.activate = p_sys->b_tty
807                                ? FB_ACTIVATE_NXTOPEN
808                                : FB_ACTIVATE_NOW;
809     p_sys->var_info.xoffset =  0;
810     p_sys->var_info.yoffset =  0;
811
812     if( ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_sys->var_info ) )
813     {
814         msg_Err( p_vout, "cannot set fb info (%m)" );
815         close( p_sys->i_fd );
816         return VLC_EGENERIC;
817     }
818
819     /* Get some information again, in the definitive configuration */
820     if( ioctl( p_sys->i_fd, FBIOGET_FSCREENINFO, &fix_info )
821          || ioctl( p_sys->i_fd, FBIOGET_VSCREENINFO, &p_sys->var_info ) )
822     {
823         msg_Err( p_vout, "cannot get additional fb info (%m)" );
824
825         /* Restore fb config */
826         ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_sys->old_info );
827
828         close( p_sys->i_fd );
829         return VLC_EGENERIC;
830     }
831
832     /* If the fb has limitations on mode change,
833      * then keep the resolution of the fb */
834     if( (p_sys->i_height != p_sys->var_info.yres) ||
835         (p_sys->i_width != p_sys->var_info.xres) )
836     {
837         p_sys->b_auto = true;
838         msg_Warn( p_vout,
839                   "using framebuffer native resolution instead of requested (%ix%i)",
840                   p_sys->i_width, p_sys->i_height );
841     }
842     p_sys->i_height = p_sys->var_info.yres;
843     p_sys->i_width  = p_sys->var_info.xres_virtual
844                                ? p_sys->var_info.xres_virtual
845                                : p_sys->var_info.xres;
846
847     /* FIXME: if the image is full-size, it gets cropped on the left
848      * because of the xres / xres_virtual slight difference */
849     msg_Dbg( p_vout, "%ix%i (virtual %ix%i) (request %ix%i)",
850              p_sys->var_info.xres, p_sys->var_info.yres,
851              p_sys->var_info.xres_virtual,
852              p_sys->var_info.yres_virtual,
853              p_sys->i_width, p_sys->i_height );
854
855     p_sys->p_palette = NULL;
856     p_sys->b_pan = ( fix_info.ypanstep || fix_info.ywrapstep );
857
858     switch( p_sys->var_info.bits_per_pixel )
859     {
860     case 8:
861         p_sys->p_palette = malloc( 8 * 256 * sizeof( uint16_t ) );
862         if( !p_sys->p_palette )
863         {
864             /* Restore fb config */
865             ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_sys->old_info );
866
867             close( p_sys->i_fd );
868             return VLC_ENOMEM;
869         }
870         p_sys->fb_cmap.start = 0;
871         p_sys->fb_cmap.len = 256;
872         p_sys->fb_cmap.red = p_sys->p_palette;
873         p_sys->fb_cmap.green = p_sys->p_palette + 256 * sizeof( uint16_t );
874         p_sys->fb_cmap.blue = p_sys->p_palette + 2 * 256 * sizeof( uint16_t );
875         p_sys->fb_cmap.transp = p_sys->p_palette + 3 * 256 * sizeof( uint16_t );
876
877         /* Save the colormap */
878         ioctl( p_sys->i_fd, FBIOGETCMAP, &p_sys->fb_cmap );
879
880         p_sys->i_bytes_per_pixel = 1;
881         break;
882
883     case 15:
884     case 16:
885         p_sys->i_bytes_per_pixel = 2;
886         break;
887
888     case 24:
889         p_sys->i_bytes_per_pixel = 3;
890         break;
891
892     case 32:
893         p_sys->i_bytes_per_pixel = 4;
894         break;
895
896     default:
897         msg_Err( p_vout, "screen depth %d is not supported",
898                          p_sys->var_info.bits_per_pixel );
899
900         /* Restore fb config */
901         ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_sys->old_info );
902
903         close( p_sys->i_fd );
904         return VLC_EGENERIC;
905     }
906
907     p_sys->i_page_size = p_sys->i_width * p_sys->i_height *
908                          p_sys->i_bytes_per_pixel;
909
910     /* Map a framebuffer at the beginning */
911     p_sys->p_video = mmap( NULL, p_sys->i_page_size,
912                               PROT_READ | PROT_WRITE, MAP_SHARED,
913                               p_sys->i_fd, 0 );
914
915     if( p_sys->p_video == MAP_FAILED )
916     {
917         msg_Err( p_vout, "cannot map video memory (%m)" );
918
919         if( p_sys->var_info.bits_per_pixel == 8 )
920         {
921             free( p_sys->p_palette );
922             p_sys->p_palette = NULL;
923         }
924
925         /* Restore fb config */
926         ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
927
928         close( p_sys->i_fd );
929         return VLC_EGENERIC;
930     }
931
932     msg_Dbg( p_vout, "framebuffer type=%d, visual=%d, ypanstep=%d, "
933              "ywrap=%d, accel=%d", fix_info.type, fix_info.visual,
934              fix_info.ypanstep, fix_info.ywrapstep, fix_info.accel );
935     return VLC_SUCCESS;
936 }
937
938 /*****************************************************************************
939  * CloseDisplay: terminate FB video thread output method
940  *****************************************************************************/
941 static void CloseDisplay( vout_thread_t *p_vout )
942 {
943     if( p_vout->p_sys->p_video != MAP_FAILED )
944     {
945         /* Clear display */
946         memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
947         munmap( p_vout->p_sys->p_video, p_vout->p_sys->i_page_size );
948     }
949
950     if( p_vout->p_sys->i_fd >= 0 )
951     {
952         /* Restore palette */
953         if( p_vout->p_sys->var_info.bits_per_pixel == 8 )
954         {
955             ioctl( p_vout->p_sys->i_fd,
956                    FBIOPUTCMAP, &p_vout->p_sys->fb_cmap );
957             free( p_vout->p_sys->p_palette );
958             p_vout->p_sys->p_palette = NULL;
959         }
960
961         /* Restore fb config */
962         ioctl( p_vout->p_sys->i_fd,
963                FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
964
965         /* Close fb */
966         close( p_vout->p_sys->i_fd );
967     }
968 }
969
970 /*****************************************************************************
971  * SwitchDisplay: VT change signal handler
972  *****************************************************************************
973  * This function activates or deactivates the output of the thread. It is
974  * called by the VT driver, on terminal change.
975  *****************************************************************************/
976 static void SwitchDisplay( int i_signal )
977 {
978     VLC_UNUSED( i_signal );
979 #if 0
980     vout_thread_t *p_vout;
981
982     vlc_mutex_lock( &p_vout_bank->lock );
983
984     /* XXX: only test the first video output */
985     if( p_vout_bank->i_count )
986     {
987         p_vout = p_vout_bank->pp_vout[0];
988
989         switch( i_signal )
990         {
991         case SIGUSR1:                                /* vt has been released */
992             p_vout->b_active = 0;
993             ioctl( p_sys->i_tty, VT_RELDISP, 1 );
994             break;
995         case SIGUSR2:                                /* vt has been acquired */
996             p_vout->b_active = 1;
997             ioctl( p_sys->i_tty, VT_RELDISP, VT_ACTIVATE );
998             /* handle blanking */
999             vlc_mutex_lock( &p_vout->change_lock );
1000             p_vout->i_changes |= VOUT_SIZE_CHANGE;
1001             vlc_mutex_unlock( &p_vout->change_lock );
1002             break;
1003         }
1004     }
1005
1006     vlc_mutex_unlock( &p_vout_bank->lock );
1007 #endif
1008 }
1009
1010 /*****************************************************************************
1011  * TextMode and GfxMode : switch tty to text/graphic mode
1012  *****************************************************************************
1013  * These functions toggle the tty mode.
1014  *****************************************************************************/
1015 static void TextMode( int i_tty )
1016 {
1017     /* return to text mode */
1018     if( -1 == ioctl(i_tty, KDSETMODE, KD_TEXT) )
1019     {
1020         /*msg_Err( p_vout, "failed ioctl KDSETMODE KD_TEXT" );*/
1021     }
1022 }
1023
1024 static void GfxMode( int i_tty )
1025 {
1026     /* switch to graphic mode */
1027     if( -1 == ioctl(i_tty, KDSETMODE, KD_GRAPHICS) )
1028     {
1029         /*msg_Err( p_vout, "failed ioctl KDSETMODE KD_GRAPHICS" );*/
1030     }
1031 }