]> git.sesse.net Git - vlc/blob - modules/video_output/fb.c
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[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( psz_chroma[0],
231                                    psz_chroma[1],
232                                    psz_chroma[2],
233                                    psz_chroma[3] );
234             msg_Dbg( p_vout, "forcing chroma '%s'", psz_chroma );
235         }
236         else
237         {
238             msg_Warn( p_vout, "invalid chroma (%s), using defaults.",
239                       psz_chroma );
240         }
241         free( psz_chroma );
242     }
243
244     p_sys->i_aspect = -1;
245     psz_aspect = var_CreateGetNonEmptyString( p_vout, "fb-aspect-ratio" );
246     if( psz_aspect )
247     {
248         char *psz_parser = strchr( psz_aspect, ':' );
249
250         if( psz_parser )
251         {
252             *psz_parser++ = '\0';
253             p_sys->i_aspect = ( atoi( psz_aspect )
254                               * VOUT_ASPECT_FACTOR ) / atoi( psz_parser );
255         }
256         msg_Dbg( p_vout, "using aspect ratio %d:%d",
257                   atoi( psz_aspect ), atoi( psz_parser ) );
258
259         free( psz_aspect );
260     }
261
262     p_sys->b_auto = 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 = true;
285      }
286
287     /* tty handling */
288     if( p_sys->b_tty )
289     {
290         GfxMode( p_sys->i_tty );
291
292         /* Set keyboard settings */
293         if( tcgetattr(0, &p_vout->p_sys->old_termios) == -1 )
294         {
295             msg_Err( p_vout, "tcgetattr failed" );
296         }
297
298         if( tcgetattr(0, &new_termios) == -1 )
299         {
300             msg_Err( p_vout, "tcgetattr failed" );
301         }
302
303         /* new_termios.c_lflag &= ~ (ICANON | ISIG);
304         new_termios.c_lflag |= (ECHO | ECHOCTL); */
305         new_termios.c_lflag &= ~ (ICANON);
306         new_termios.c_lflag &= ~(ECHO | ECHOCTL);
307         new_termios.c_iflag = 0;
308         new_termios.c_cc[VMIN] = 1;
309         new_termios.c_cc[VTIME] = 0;
310
311         if( tcsetattr(0, TCSAFLUSH, &new_termios) == -1 )
312         {
313             msg_Err( p_vout, "tcsetattr failed" );
314         }
315
316         ioctl( p_sys->i_tty, VT_RELDISP, VT_ACKACQ );
317
318         /* Set-up tty signal handler to be aware of tty changes */
319         memset( &sig_tty, 0, sizeof( sig_tty ) );
320         sig_tty.sa_handler = SwitchDisplay;
321         sigemptyset( &sig_tty.sa_mask );
322         if( sigaction( SIGUSR1, &sig_tty, &p_vout->p_sys->sig_usr1 ) ||
323             sigaction( SIGUSR2, &sig_tty, &p_vout->p_sys->sig_usr2 ) )
324         {
325             msg_Err( p_vout, "cannot set signal handler (%m)" );
326             tcsetattr(0, 0, &p_vout->p_sys->old_termios);
327             TextMode( p_sys->i_tty );
328             free( p_vout->p_sys );
329             return VLC_EGENERIC;
330         }
331
332         /* Set-up tty according to new signal handler */
333         if( -1 == ioctl( p_sys->i_tty, VT_GETMODE, &p_vout->p_sys->vt_mode ) )
334         {
335             msg_Err( p_vout, "cannot get terminal mode (%m)" );
336             sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
337             sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
338             tcsetattr(0, 0, &p_vout->p_sys->old_termios);
339             TextMode( p_sys->i_tty );
340             free( p_vout->p_sys );
341             return VLC_EGENERIC;
342         }
343         memcpy( &vt_mode, &p_vout->p_sys->vt_mode, sizeof( vt_mode ) );
344         vt_mode.mode   = VT_PROCESS;
345         vt_mode.waitv  = 0;
346         vt_mode.relsig = SIGUSR1;
347         vt_mode.acqsig = SIGUSR2;
348
349         if( -1 == ioctl( p_sys->i_tty, VT_SETMODE, &vt_mode ) )
350         {
351             msg_Err( p_vout, "cannot set terminal mode (%m)" );
352             sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
353             sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
354             tcsetattr(0, 0, &p_vout->p_sys->old_termios);
355             TextMode( p_sys->i_tty );
356             free( p_vout->p_sys );
357             return VLC_EGENERIC;
358         }
359     }
360
361     if( OpenDisplay( p_vout ) )
362     {
363         Destroy( VLC_OBJECT(p_vout) );
364         return VLC_EGENERIC;
365     }
366
367     return VLC_SUCCESS;
368 }
369
370
371 /*****************************************************************************
372  * Destroy: destroy FB video thread output method
373  *****************************************************************************
374  * Terminate an output method created by Create
375  *****************************************************************************/
376 static void Destroy( vlc_object_t *p_this )
377 {
378     vout_thread_t *p_vout = (vout_thread_t *)p_this;
379
380     CloseDisplay( p_vout );
381
382     if( p_vout->p_sys->b_tty )
383     {
384         /* Reset the terminal */
385         ioctl( p_vout->p_sys->i_tty, VT_SETMODE, &p_vout->p_sys->vt_mode );
386
387         /* Remove signal handlers */
388         sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
389         sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
390
391         /* Reset the keyboard state */
392         tcsetattr( 0, 0, &p_vout->p_sys->old_termios );
393
394         /* Return to text mode */
395         TextMode( p_vout->p_sys->i_tty );
396     }
397
398     /* Destroy structure */
399     free( p_vout->p_sys );
400 }
401
402 /*****************************************************************************
403  * NewPicture: allocate a picture
404  *****************************************************************************
405  * Returns 0 on success, -1 otherwise
406  *****************************************************************************/
407 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
408 {
409     /* We know the chroma, allocate a buffer which will be used
410      * directly by the decoder */
411     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
412     if( p_pic->p_sys == NULL )
413     {
414         return VLC_ENOMEM;
415     }
416
417     /* Fill in picture_t fields */
418     vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
419                       p_vout->output.i_width, p_vout->output.i_height,
420                       p_vout->output.i_aspect );
421
422     p_pic->p_sys->p_data = malloc( p_vout->p_sys->i_page_size );
423     if( !p_pic->p_sys->p_data )
424     {
425         free( p_pic->p_sys );
426         p_pic->p_sys = NULL;
427         return VLC_ENOMEM;
428     }
429
430     p_pic->p->p_pixels = (uint8_t*) p_pic->p_sys->p_data;
431
432     p_pic->p->i_pixel_pitch = p_vout->p_sys->i_bytes_per_pixel;
433     p_pic->p->i_lines = p_vout->p_sys->var_info.yres;
434     p_pic->p->i_visible_lines = p_vout->p_sys->var_info.yres;
435
436     if( p_vout->p_sys->var_info.xres_virtual )
437     {
438         p_pic->p->i_pitch = p_vout->p_sys->var_info.xres_virtual
439                              * p_vout->p_sys->i_bytes_per_pixel;
440     }
441     else
442     {
443         p_pic->p->i_pitch = p_vout->p_sys->var_info.xres
444                              * p_vout->p_sys->i_bytes_per_pixel;
445     }
446
447     p_pic->p->i_visible_pitch = p_vout->p_sys->var_info.xres
448                                  * p_vout->p_sys->i_bytes_per_pixel;
449     p_pic->i_planes = 1;
450
451     return VLC_SUCCESS;
452 }
453
454 /*****************************************************************************
455  * FreePicture: destroy a picture allocated with NewPicture
456  *****************************************************************************
457  * Destroy Image AND associated data.
458  *****************************************************************************/
459 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
460 {
461     VLC_UNUSED(p_vout);
462     free( p_pic->p_sys->p_data );
463     free( p_pic->p_sys );
464     p_pic->p_sys = NULL;
465 }
466
467 /*****************************************************************************
468  * Init: initialize framebuffer video thread output method
469  *****************************************************************************/
470 static int Init( vout_thread_t *p_vout )
471 {
472     vout_sys_t *p_sys = p_vout->p_sys;
473     int i_index;
474     picture_t *p_pic = NULL;
475
476     I_OUTPUTPICTURES = 0;
477
478     p_vout->output.i_width  = p_vout->render.i_width;
479     p_vout->output.i_height = p_vout->render.i_height;
480     p_vout->output.i_aspect = p_vout->render.i_aspect;
481
482     p_vout->fmt_out = p_vout->fmt_in;
483     if( p_sys->i_chroma == 0 )
484     {
485         /* Initialize the output structure: RGB with square pixels, whatever
486          * the input format is, since it's the only format we know */
487         switch( p_sys->var_info.bits_per_pixel )
488         {
489         case 8: /* FIXME: set the palette */
490             p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
491         case 15:
492             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
493         case 16:
494             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
495         case 24:
496             p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
497         case 32:
498             p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
499         default:
500             msg_Err( p_vout, "unknown screen depth %i",
501                      p_vout->p_sys->var_info.bits_per_pixel );
502             return VLC_EGENERIC;
503         }
504
505         if( p_sys->var_info.bits_per_pixel != 8 )
506         {
507             p_vout->output.i_rmask = ( (1 << p_sys->var_info.red.length) - 1 )
508                                  << p_sys->var_info.red.offset;
509             p_vout->output.i_gmask = ( (1 << p_sys->var_info.green.length) - 1 )
510                                  << p_sys->var_info.green.offset;
511             p_vout->output.i_bmask = ( (1 << p_sys->var_info.blue.length) - 1 )
512                                  << p_sys->var_info.blue.offset;
513         }
514     }
515     else
516     {
517         p_vout->output.i_chroma = p_sys->i_chroma;
518     }
519     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
520
521     p_vout->output.i_width =
522     p_vout->fmt_out.i_width =
523     p_vout->fmt_out.i_visible_width = p_sys->i_width;
524     p_vout->output.i_height =
525     p_vout->fmt_out.i_height =
526     p_vout->fmt_out.i_visible_height = p_sys->i_height;
527
528     /* Assume we have square pixels */
529     if( p_sys->i_aspect < 0 )
530     {
531         p_vout->output.i_aspect = ( p_sys->i_width
532                                   * VOUT_ASPECT_FACTOR ) / p_sys->i_height;
533     }
534     else p_vout->output.i_aspect = p_sys->i_aspect;
535
536     p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_sar_den = 1;
537     p_vout->fmt_out.i_aspect  = p_vout->render.i_aspect = p_vout->output.i_aspect;
538     p_vout->fmt_out.i_x_offset= p_vout->fmt_out.i_y_offset = 0;
539
540     /* Clear the screen */
541     memset( p_sys->p_video, 0, p_sys->i_page_size );
542
543     if( !p_sys->b_hw_accel )
544     {
545         /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
546         while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
547         {
548             p_pic = NULL;
549
550             /* Find an empty picture slot */
551             for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
552             {
553                 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
554                 {
555                     p_pic = p_vout->p_picture + i_index;
556                     break;
557                 }
558             }
559
560             /* Allocate the picture */
561             if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
562             {
563                 break;
564             }
565
566             p_pic->i_status = DESTROYED_PICTURE;
567             p_pic->i_type   = DIRECT_PICTURE;
568
569             PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
570
571             I_OUTPUTPICTURES++;
572         }
573     }
574     else
575     {
576         /* Try to initialize 1 direct buffer */
577         p_pic = NULL;
578
579         /* Find an empty picture slot */
580         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
581         {
582             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
583             {
584                 p_pic = p_vout->p_picture + i_index;
585                 break;
586             }
587         }
588
589         /* Allocate the picture */
590         if( p_pic == NULL )
591         {
592             return VLC_EGENERIC;
593         }
594
595         /* We know the chroma, allocate a buffer which will be used
596         * directly by the decoder */
597         p_pic->p->p_pixels = p_vout->p_sys->p_video;
598         p_pic->p->i_pixel_pitch = p_vout->p_sys->i_bytes_per_pixel;
599         p_pic->p->i_lines = p_vout->p_sys->var_info.yres;
600         p_pic->p->i_visible_lines = p_vout->p_sys->var_info.yres;
601
602         if( p_vout->p_sys->var_info.xres_virtual )
603         {
604             p_pic->p->i_pitch = p_vout->p_sys->var_info.xres_virtual
605                                 * p_vout->p_sys->i_bytes_per_pixel;
606         }
607         else
608         {
609             p_pic->p->i_pitch = p_vout->p_sys->var_info.xres
610                                 * p_vout->p_sys->i_bytes_per_pixel;
611         }
612
613         p_pic->p->i_visible_pitch = p_vout->p_sys->var_info.xres
614                                     * p_vout->p_sys->i_bytes_per_pixel;
615         p_pic->i_planes = 1;
616         p_pic->i_status = DESTROYED_PICTURE;
617         p_pic->i_type   = DIRECT_PICTURE;
618
619         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
620
621         I_OUTPUTPICTURES++;
622     }
623
624     return VLC_SUCCESS;
625 }
626
627 /*****************************************************************************
628  * End: terminate framebuffer video thread output method
629  *****************************************************************************/
630 static void End( vout_thread_t *p_vout )
631 {
632     if( !p_vout->p_sys->b_hw_accel )
633     {
634         int i_index;
635
636         /* Free the direct buffers we allocated */
637         for( i_index = I_OUTPUTPICTURES ; i_index ; )
638         {
639             i_index--;
640             FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
641         }
642
643     }
644     /* Clear the screen */
645     memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
646 }
647
648 /*****************************************************************************
649  * Control: control facility for the vout
650  *****************************************************************************/
651 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
652 {
653     (void) p_vout; (void) i_query; (void) args;
654     return VLC_EGENERIC;
655 }
656
657 /*****************************************************************************
658  * Manage: handle FB events
659  *****************************************************************************
660  * This function should be called regularly by video output thread. It manages
661  * console events. It returns a non null value on error.
662  *****************************************************************************/
663 static int Manage( vout_thread_t *p_vout )
664 {
665 #if 0
666     uint8_t buf;
667
668     if ( read(0, &buf, 1) == 1)
669     {
670         switch( buf )
671         {
672         case 'q':
673             libvlc_Quit( p_vout->p_libvlc );
674             break;
675
676         default:
677             break;
678         }
679     }
680 #endif
681
682     /*
683      * Size change
684      */
685     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
686     {
687         msg_Dbg( p_vout, "reinitializing framebuffer screen" );
688         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
689
690         /* Destroy XImages to change their size */
691         End( p_vout );
692
693         /* Recreate XImages. If SysInit failed, the thread can't go on. */
694         if( Init( p_vout ) )
695         {
696             msg_Err( p_vout, "cannot reinit framebuffer screen" );
697             return VLC_EGENERIC;
698         }
699
700         /* Clear screen */
701         memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
702
703 #if 0
704         /* Tell the video output thread that it will need to rebuild YUV
705          * tables. This is needed since conversion buffer size may have changed */
706         p_vout->i_changes |= VOUT_YUV_CHANGE;
707 #endif
708     }
709
710     return VLC_SUCCESS;
711 }
712
713 /*****************************************************************************
714  * Display: displays previously rendered output
715  *****************************************************************************
716  * This function send the currently rendered image to FB image, waits until
717  * it is displayed and switch the two rendering buffers, preparing next frame.
718  *****************************************************************************/
719 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
720 {
721 static int panned=0;
722     /* swap the two Y offsets if the drivers supports panning */
723     if( p_vout->p_sys->b_pan )
724     {
725         p_vout->p_sys->var_info.yoffset = 0;
726         /*p_vout->p_sys->var_info.yoffset = p_vout->p_sys->var_info.yres; */
727
728         /* the X offset should be 0, but who knows ...
729          * some other app might have played with the framebuffer */
730         p_vout->p_sys->var_info.xoffset = 0;
731
732         if( panned < 0 )
733         {
734             ioctl( p_vout->p_sys->i_fd,
735                    FBIOPAN_DISPLAY, &p_vout->p_sys->var_info );
736             panned++;
737         }
738     }
739
740     if( !p_vout->p_sys->b_hw_accel )
741     {
742         vlc_memcpy( p_vout->p_sys->p_video, p_pic->p->p_pixels,
743                     p_vout->p_sys->i_page_size );
744     }
745 }
746
747 #if 0
748 static void SetPalette( vout_thread_t *p_vout, uint16_t *red, uint16_t *green,
749                                                uint16_t *blue, uint16_t *transp )
750 {
751     struct fb_cmap cmap = { 0, 256, red, green, blue, transp };
752
753     ioctl( p_vout->p_sys->i_fd, FBIOPUTCMAP, &cmap );
754 }
755 #endif
756
757 /* following functions are local */
758
759 /*****************************************************************************
760  * OpenDisplay: initialize framebuffer
761  *****************************************************************************/
762 static int OpenDisplay( vout_thread_t *p_vout )
763 {
764     vout_sys_t *p_sys = (vout_sys_t *) p_vout->p_sys;
765     char *psz_device;                             /* framebuffer device path */
766     struct fb_fix_screeninfo    fix_info;     /* framebuffer fix information */
767
768     /* Open framebuffer device */
769     if( !(psz_device = config_GetPsz( p_vout, FB_DEV_VAR )) )
770     {
771         msg_Err( p_vout, "don't know which fb device to open" );
772         return VLC_EGENERIC;
773     }
774
775     p_sys->i_fd = open( psz_device, O_RDWR);
776     if( p_sys->i_fd == -1 )
777     {
778         msg_Err( p_vout, "cannot open %s (%m)", psz_device );
779         free( psz_device );
780         return VLC_EGENERIC;
781     }
782     free( psz_device );
783
784     /* Get framebuffer device information */
785     if( ioctl( p_sys->i_fd, FBIOGET_VSCREENINFO, &p_sys->var_info ) )
786     {
787         msg_Err( p_vout, "cannot get fb info (%m)" );
788         close( p_sys->i_fd );
789         return VLC_EGENERIC;
790     }
791
792     memcpy( &p_sys->old_info, &p_sys->var_info,
793             sizeof( struct fb_var_screeninfo ) );
794
795     /* Get some info on the framebuffer itself */
796     if( !p_sys->b_auto )
797     {
798         p_sys->var_info.xres = p_sys->var_info.xres_virtual = p_sys->i_width;
799         p_sys->var_info.yres = p_sys->var_info.yres_virtual = p_sys->i_height;
800         p_vout->fmt_out.i_width = p_sys->i_width;
801         p_vout->fmt_out.i_height = p_sys->i_height;
802     }
803
804     /* Set some attributes */
805     p_sys->var_info.activate = p_sys->b_tty
806                                ? FB_ACTIVATE_NXTOPEN
807                                : FB_ACTIVATE_NOW;
808     p_sys->var_info.xoffset =  0;
809     p_sys->var_info.yoffset =  0;
810
811     if( ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_sys->var_info ) )
812     {
813         msg_Err( p_vout, "cannot set fb info (%m)" );
814         close( p_sys->i_fd );
815         return VLC_EGENERIC;
816     }
817
818     /* Get some information again, in the definitive configuration */
819     if( ioctl( p_sys->i_fd, FBIOGET_FSCREENINFO, &fix_info )
820          || ioctl( p_sys->i_fd, FBIOGET_VSCREENINFO, &p_sys->var_info ) )
821     {
822         msg_Err( p_vout, "cannot get additional fb info (%m)" );
823
824         /* Restore fb config */
825         ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_sys->old_info );
826
827         close( p_sys->i_fd );
828         return VLC_EGENERIC;
829     }
830
831     /* If the fb has limitations on mode change,
832      * then keep the resolution of the fb */
833     if( (p_sys->i_height != p_sys->var_info.yres) ||
834         (p_sys->i_width != p_sys->var_info.xres) )
835     {
836         p_sys->b_auto = true;
837         msg_Warn( p_vout,
838                   "using framebuffer native resolution instead of requested (%ix%i)",
839                   p_sys->i_width, p_sys->i_height );
840     }
841     p_sys->i_height = p_sys->var_info.yres;
842     p_sys->i_width  = p_sys->var_info.xres_virtual
843                                ? p_sys->var_info.xres_virtual
844                                : p_sys->var_info.xres;
845
846     /* FIXME: if the image is full-size, it gets cropped on the left
847      * because of the xres / xres_virtual slight difference */
848     msg_Dbg( p_vout, "%ix%i (virtual %ix%i) (request %ix%i)",
849              p_sys->var_info.xres, p_sys->var_info.yres,
850              p_sys->var_info.xres_virtual,
851              p_sys->var_info.yres_virtual,
852              p_sys->i_width, p_sys->i_height );
853
854     p_sys->p_palette = NULL;
855     p_sys->b_pan = ( fix_info.ypanstep || fix_info.ywrapstep );
856
857     switch( p_sys->var_info.bits_per_pixel )
858     {
859     case 8:
860         p_sys->p_palette = malloc( 8 * 256 * sizeof( uint16_t ) );
861         if( !p_sys->p_palette )
862         {
863             /* Restore fb config */
864             ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_sys->old_info );
865
866             close( p_sys->i_fd );
867             return VLC_ENOMEM;
868         }
869         p_sys->fb_cmap.start = 0;
870         p_sys->fb_cmap.len = 256;
871         p_sys->fb_cmap.red = p_sys->p_palette;
872         p_sys->fb_cmap.green = p_sys->p_palette + 256 * sizeof( uint16_t );
873         p_sys->fb_cmap.blue = p_sys->p_palette + 2 * 256 * sizeof( uint16_t );
874         p_sys->fb_cmap.transp = p_sys->p_palette + 3 * 256 * sizeof( uint16_t );
875
876         /* Save the colormap */
877         ioctl( p_sys->i_fd, FBIOGETCMAP, &p_sys->fb_cmap );
878
879         p_sys->i_bytes_per_pixel = 1;
880         break;
881
882     case 15:
883     case 16:
884         p_sys->i_bytes_per_pixel = 2;
885         break;
886
887     case 24:
888         p_sys->i_bytes_per_pixel = 3;
889         break;
890
891     case 32:
892         p_sys->i_bytes_per_pixel = 4;
893         break;
894
895     default:
896         msg_Err( p_vout, "screen depth %d is not supported",
897                          p_sys->var_info.bits_per_pixel );
898
899         /* Restore fb config */
900         ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_sys->old_info );
901
902         close( p_sys->i_fd );
903         return VLC_EGENERIC;
904     }
905
906     p_sys->i_page_size = p_sys->i_width * p_sys->i_height *
907                          p_sys->i_bytes_per_pixel;
908
909     /* Map a framebuffer at the beginning */
910     p_sys->p_video = mmap( NULL, p_sys->i_page_size,
911                               PROT_READ | PROT_WRITE, MAP_SHARED,
912                               p_sys->i_fd, 0 );
913
914     if( p_sys->p_video == MAP_FAILED )
915     {
916         msg_Err( p_vout, "cannot map video memory (%m)" );
917
918         if( p_sys->var_info.bits_per_pixel == 8 )
919         {
920             free( p_sys->p_palette );
921             p_sys->p_palette = NULL;
922         }
923
924         /* Restore fb config */
925         ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
926
927         close( p_sys->i_fd );
928         return VLC_EGENERIC;
929     }
930
931     msg_Dbg( p_vout, "framebuffer type=%d, visual=%d, ypanstep=%d, "
932              "ywrap=%d, accel=%d", fix_info.type, fix_info.visual,
933              fix_info.ypanstep, fix_info.ywrapstep, fix_info.accel );
934     return VLC_SUCCESS;
935 }
936
937 /*****************************************************************************
938  * CloseDisplay: terminate FB video thread output method
939  *****************************************************************************/
940 static void CloseDisplay( vout_thread_t *p_vout )
941 {
942     if( p_vout->p_sys->p_video != MAP_FAILED )
943     {
944         /* Clear display */
945         memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
946         munmap( p_vout->p_sys->p_video, p_vout->p_sys->i_page_size );
947     }
948
949     if( p_vout->p_sys->i_fd >= 0 )
950     {
951         /* Restore palette */
952         if( p_vout->p_sys->var_info.bits_per_pixel == 8 )
953         {
954             ioctl( p_vout->p_sys->i_fd,
955                    FBIOPUTCMAP, &p_vout->p_sys->fb_cmap );
956             free( p_vout->p_sys->p_palette );
957             p_vout->p_sys->p_palette = NULL;
958         }
959
960         /* Restore fb config */
961         ioctl( p_vout->p_sys->i_fd,
962                FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
963
964         /* Close fb */
965         close( p_vout->p_sys->i_fd );
966     }
967 }
968
969 /*****************************************************************************
970  * SwitchDisplay: VT change signal handler
971  *****************************************************************************
972  * This function activates or deactivates the output of the thread. It is
973  * called by the VT driver, on terminal change.
974  *****************************************************************************/
975 static void SwitchDisplay( int i_signal )
976 {
977     VLC_UNUSED( i_signal );
978 #if 0
979     vout_thread_t *p_vout;
980
981     vlc_mutex_lock( &p_vout_bank->lock );
982
983     /* XXX: only test the first video output */
984     if( p_vout_bank->i_count )
985     {
986         p_vout = p_vout_bank->pp_vout[0];
987
988         switch( i_signal )
989         {
990         case SIGUSR1:                                /* vt has been released */
991             p_vout->b_active = 0;
992             ioctl( p_sys->i_tty, VT_RELDISP, 1 );
993             break;
994         case SIGUSR2:                                /* vt has been acquired */
995             p_vout->b_active = 1;
996             ioctl( p_sys->i_tty, VT_RELDISP, VT_ACTIVATE );
997             /* handle blanking */
998             vlc_mutex_lock( &p_vout->change_lock );
999             p_vout->i_changes |= VOUT_SIZE_CHANGE;
1000             vlc_mutex_unlock( &p_vout->change_lock );
1001             break;
1002         }
1003     }
1004
1005     vlc_mutex_unlock( &p_vout_bank->lock );
1006 #endif
1007 }
1008
1009 /*****************************************************************************
1010  * TextMode and GfxMode : switch tty to text/graphic mode
1011  *****************************************************************************
1012  * These functions toggle the tty mode.
1013  *****************************************************************************/
1014 static void TextMode( int i_tty )
1015 {
1016     /* return to text mode */
1017     if( -1 == ioctl(i_tty, KDSETMODE, KD_TEXT) )
1018     {
1019         /*msg_Err( p_vout, "failed ioctl KDSETMODE KD_TEXT" );*/
1020     }
1021 }
1022
1023 static void GfxMode( int i_tty )
1024 {
1025     /* switch to graphic mode */
1026     if( -1 == ioctl(i_tty, KDSETMODE, KD_GRAPHICS) )
1027     {
1028         /*msg_Err( p_vout, "failed ioctl KDSETMODE KD_GRAPHICS" );*/
1029     }
1030 }