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