]> git.sesse.net Git - vlc/blob - plugins/fb/fb.c
fb12103493d0a14da87d5675165f8921f7a0c8f4
[vlc] / plugins / fb / fb.c
1 /*****************************************************************************
2  * fb.c : framebuffer plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: fb.c,v 1.21 2002/07/23 00:39:17 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *      
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <errno.h>                                                 /* ENOMEM */
28 #include <signal.h>                                      /* SIGUSR1, SIGUSR2 */
29 #include <stdlib.h>                                                /* free() */
30 #include <string.h>                                            /* strerror() */
31 #include <fcntl.h>                                                 /* open() */
32 #include <unistd.h>                                               /* close() */
33
34 #include <termios.h>                                       /* struct termios */
35 #include <sys/ioctl.h>
36 #include <sys/mman.h>                                              /* mmap() */
37
38 #include <linux/fb.h>
39 #include <linux/vt.h>                                                /* VT_* */
40 #include <linux/kd.h>                                                 /* KD* */
41
42 #include <vlc/vlc.h>
43 #include <vlc/vout.h>
44
45 /*****************************************************************************
46  * Capabilities defined in the other files.
47  *****************************************************************************/
48 static void vout_getfunctions( function_list_t * p_function_list );
49
50 static int  vout_Create    ( vout_thread_t * );
51 static void vout_Destroy   ( vout_thread_t * );
52 static void vout_Render    ( vout_thread_t *, picture_t * );
53 static void vout_Display   ( vout_thread_t *, picture_t * );
54 static int  vout_Manage    ( vout_thread_t * );
55 static int  vout_Init      ( vout_thread_t * );
56 static void vout_End       ( vout_thread_t * );
57
58 static int  OpenDisplay    ( vout_thread_t * );
59 static void CloseDisplay   ( vout_thread_t * );
60 static void SwitchDisplay  ( int i_signal );
61 static void TextMode       ( int i_tty );
62 static void GfxMode        ( int i_tty );
63
64 /*****************************************************************************
65  * Building configuration tree
66  *****************************************************************************/
67 #define FB_DEV_VAR "fbdev"
68
69 MODULE_CONFIG_START
70 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
71 ADD_STRING  ( FB_DEV_VAR, "/dev/fb0", NULL, N_("framebuffer device"), NULL )
72 MODULE_CONFIG_STOP
73
74 MODULE_INIT_START
75     SET_DESCRIPTION( _("Linux console framebuffer module") )
76     ADD_CAPABILITY( VOUT, 30 )
77 MODULE_INIT_STOP
78
79 MODULE_ACTIVATE_START
80     vout_getfunctions( &p_module->p_functions->vout );
81 MODULE_ACTIVATE_STOP
82
83 MODULE_DEACTIVATE_START
84 MODULE_DEACTIVATE_STOP
85
86 /*****************************************************************************
87  * vout_sys_t: video output framebuffer method descriptor
88  *****************************************************************************
89  * This structure is part of the video output thread descriptor.
90  * It describes the FB specific properties of an output thread.
91  *****************************************************************************/
92 struct vout_sys_t
93 {
94     /* System informations */
95     int                 i_tty;                          /* tty device handle */
96     struct termios      old_termios;
97
98     /* Original configuration informations */
99     struct sigaction            sig_usr1;           /* USR1 previous handler */
100     struct sigaction            sig_usr2;           /* USR2 previous handler */
101     struct vt_mode              vt_mode;                 /* previous VT mode */
102
103     /* Framebuffer information */
104     int                         i_fd;                       /* device handle */
105     struct fb_var_screeninfo    old_info;      /* original mode informations */
106     struct fb_var_screeninfo    var_info;       /* current mode informations */
107     vlc_bool_t                  b_pan;     /* does device supports panning ? */
108     struct fb_cmap              fb_cmap;                /* original colormap */
109     u16                         *p_palette;              /* original palette */
110
111     /* Video information */
112     int i_width;
113     int i_height;
114     int i_bytes_per_pixel;
115
116     /* Video memory */
117     byte_t *    p_video;                                      /* base adress */
118     size_t      i_page_size;                                    /* page size */
119 };
120
121 /*****************************************************************************
122  * Functions exported as capabilities. They are declared as static so that
123  * we don't pollute the namespace too much.
124  *****************************************************************************/
125 static void vout_getfunctions( function_list_t * p_function_list )
126 {
127     p_function_list->functions.vout.pf_create     = vout_Create;
128     p_function_list->functions.vout.pf_init       = vout_Init;
129     p_function_list->functions.vout.pf_end        = vout_End;
130     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
131     p_function_list->functions.vout.pf_manage     = vout_Manage;
132     p_function_list->functions.vout.pf_render     = vout_Render;
133     p_function_list->functions.vout.pf_display    = vout_Display;
134 }
135
136 /*****************************************************************************
137  * vout_Create: allocates FB video thread output method
138  *****************************************************************************
139  * This function allocates and initializes a FB vout method.
140  *****************************************************************************/
141 static int vout_Create( vout_thread_t *p_vout )
142 {
143     struct sigaction    sig_tty;                 /* sigaction for tty change */
144     struct vt_mode      vt_mode;                          /* vt current mode */
145     struct termios      new_termios;
146
147     /* Allocate instance and initialize some members */
148     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
149     if( p_vout->p_sys == NULL )
150     {
151         return( 1 );
152     };
153
154     /* Set tty and fb devices */
155     p_vout->p_sys->i_tty = 0;           /* 0 == /dev/tty0 == current console */
156
157     GfxMode( p_vout->p_sys->i_tty );
158
159     /* Set keyboard settings */
160     if (tcgetattr(0, &p_vout->p_sys->old_termios) == -1)
161     {
162         msg_Err( p_vout, "tcgetattr failed" );
163     }
164
165     if (tcgetattr(0, &new_termios) == -1)
166     {
167         msg_Err( p_vout, "tcgetattr failed" );
168     }
169
170  /* new_termios.c_lflag &= ~ (ICANON | ISIG);
171     new_termios.c_lflag |= (ECHO | ECHOCTL); */
172     new_termios.c_lflag &= ~ (ICANON);
173     new_termios.c_lflag &= ~(ECHO | ECHOCTL);
174     new_termios.c_iflag = 0;
175     new_termios.c_cc[VMIN] = 1;
176     new_termios.c_cc[VTIME] = 0;
177
178     if (tcsetattr(0, TCSAFLUSH, &new_termios) == -1)
179     {
180         msg_Err( p_vout, "tcsetattr failed" );
181     }
182
183     ioctl( p_vout->p_sys->i_tty, VT_RELDISP, VT_ACKACQ );
184
185     /* Set-up tty signal handler to be aware of tty changes */
186     memset( &sig_tty, 0, sizeof( sig_tty ) );
187     sig_tty.sa_handler = SwitchDisplay;
188     sigemptyset( &sig_tty.sa_mask );
189     if( sigaction( SIGUSR1, &sig_tty, &p_vout->p_sys->sig_usr1 ) ||
190         sigaction( SIGUSR2, &sig_tty, &p_vout->p_sys->sig_usr2 ) )
191     {
192         msg_Err( p_vout, "cannot set signal handler (%s)", strerror(errno) );
193         tcsetattr(0, 0, &p_vout->p_sys->old_termios);
194         TextMode( p_vout->p_sys->i_tty );
195         free( p_vout->p_sys );
196         return( 1 );
197     }
198
199     /* Set-up tty according to new signal handler */
200     if( -1 == ioctl( p_vout->p_sys->i_tty,
201                      VT_GETMODE, &p_vout->p_sys->vt_mode ) )
202     {
203         msg_Err( p_vout, "cannot get terminal mode (%s)", strerror(errno) );
204         sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
205         sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
206         tcsetattr(0, 0, &p_vout->p_sys->old_termios);
207         TextMode( p_vout->p_sys->i_tty );
208         free( p_vout->p_sys );
209         return( 1 );
210     }
211     memcpy( &vt_mode, &p_vout->p_sys->vt_mode, sizeof( vt_mode ) );
212     vt_mode.mode   = VT_PROCESS;
213     vt_mode.waitv  = 0;
214     vt_mode.relsig = SIGUSR1;
215     vt_mode.acqsig = SIGUSR2;
216
217     if( -1 == ioctl( p_vout->p_sys->i_tty, VT_SETMODE, &vt_mode ) )
218     {
219         msg_Err( p_vout, "cannot set terminal mode (%s)", strerror(errno) );
220         sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
221         sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
222         tcsetattr(0, 0, &p_vout->p_sys->old_termios);
223         TextMode( p_vout->p_sys->i_tty );
224         free( p_vout->p_sys );
225         return( 1 );
226     }
227
228     if( OpenDisplay( p_vout ) )
229     {
230         ioctl( p_vout->p_sys->i_tty, VT_SETMODE, &p_vout->p_sys->vt_mode );
231         sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
232         sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
233         tcsetattr(0, 0, &p_vout->p_sys->old_termios);
234         TextMode( p_vout->p_sys->i_tty );
235         free( p_vout->p_sys );
236         return( 1 );
237     }
238
239     return( 0 );
240 }
241
242 /*****************************************************************************
243  * vout_Init: initialize framebuffer video thread output method
244  *****************************************************************************/
245 static int vout_Init( vout_thread_t *p_vout )
246 {
247     int i_index;
248     picture_t *p_pic;
249
250     I_OUTPUTPICTURES = 0;
251
252     /* Initialize the output structure: RGB with square pixels, whatever
253      * the input format is, since it's the only format we know */
254     switch( p_vout->p_sys->var_info.bits_per_pixel )
255     {
256         case 8: /* FIXME: set the palette */
257             p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
258         case 15:
259             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
260         case 16:
261             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
262         case 24:
263             p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
264         case 32:
265             p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
266         default:
267             msg_Err( p_vout, "unknown screen depth %i",
268                      p_vout->p_sys->var_info.bits_per_pixel );
269             return 0;
270     }
271
272     /* Only useful for p_vout->p_sys->var_info.bits_per_pixel != 8 */
273     p_vout->output.i_rmask = ( (1 << p_vout->p_sys->var_info.red.length) - 1 )
274                      << p_vout->p_sys->var_info.red.offset;
275     p_vout->output.i_gmask = ( (1 << p_vout->p_sys->var_info.green.length) - 1 )
276                      << p_vout->p_sys->var_info.green.offset;
277     p_vout->output.i_bmask = ( (1 << p_vout->p_sys->var_info.blue.length) - 1 )
278                      << p_vout->p_sys->var_info.blue.offset;
279
280     p_vout->output.i_width = p_vout->p_sys->i_width;
281     p_vout->output.i_height = p_vout->p_sys->i_height;
282
283     /* Assume we have square pixels */
284     p_vout->output.i_aspect = p_vout->p_sys->i_width
285                                * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
286
287     /* Clear the screen */
288     memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
289
290     /* Try to initialize 1 direct buffer */
291     p_pic = NULL;
292
293     /* Find an empty picture slot */
294     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
295     {
296         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
297         {
298             p_pic = p_vout->p_picture + i_index;
299             break;
300         }
301     }
302
303     /* Allocate the picture */
304     if( p_pic == NULL )
305     {
306         return 0;
307     }
308
309     /* We know the chroma, allocate a buffer which will be used
310      * directly by the decoder */
311     p_pic->p->p_pixels = p_vout->p_sys->p_video;
312     p_pic->p->i_pixel_pitch = p_vout->p_sys->i_bytes_per_pixel;
313     p_pic->p->i_lines = p_vout->p_sys->var_info.yres;
314
315     if( p_vout->p_sys->var_info.xres_virtual )
316     {
317         p_pic->p->i_pitch = p_vout->p_sys->var_info.xres_virtual
318                              * p_vout->p_sys->i_bytes_per_pixel;
319     }
320     else
321     {
322         p_pic->p->i_pitch = p_vout->p_sys->var_info.xres
323                              * p_vout->p_sys->i_bytes_per_pixel;
324     }
325
326     p_pic->p->i_visible_pitch = p_vout->p_sys->var_info.xres
327                                  * p_vout->p_sys->i_bytes_per_pixel;
328
329     p_pic->i_planes = 1;
330
331     p_pic->i_status = DESTROYED_PICTURE;
332     p_pic->i_type   = DIRECT_PICTURE;
333
334     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
335
336     I_OUTPUTPICTURES++;
337
338     return 0;
339 }
340
341 /*****************************************************************************
342  * vout_End: terminate framebuffer video thread output method
343  *****************************************************************************/
344 static void vout_End( vout_thread_t *p_vout )
345 {
346     /* Clear the screen */
347     memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
348 }
349
350 /*****************************************************************************
351  * vout_Destroy: destroy FB video thread output method
352  *****************************************************************************
353  * Terminate an output method created by vout_CreateOutputMethod
354  *****************************************************************************/
355 static void vout_Destroy( vout_thread_t *p_vout )
356 {
357     CloseDisplay( p_vout );
358
359     /* Reset the terminal */
360     ioctl( p_vout->p_sys->i_tty, VT_SETMODE, &p_vout->p_sys->vt_mode );
361
362     /* Remove signal handlers */
363     sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
364     sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
365
366     /* Reset the keyboard state */
367     tcsetattr( 0, 0, &p_vout->p_sys->old_termios );
368
369     /* Return to text mode */
370     TextMode( p_vout->p_sys->i_tty );
371
372     /* Destroy structure */
373     free( p_vout->p_sys );
374 }
375
376 /*****************************************************************************
377  * vout_Manage: handle FB events
378  *****************************************************************************
379  * This function should be called regularly by video output thread. It manages
380  * console events. It returns a non null value on error.
381  *****************************************************************************/
382 static int vout_Manage( vout_thread_t *p_vout )
383 {
384 #if 0
385     u8 buf;
386
387     if ( read(0, &buf, 1) == 1)
388     {
389         switch( buf )
390         {
391         case 'q':
392             p_vout->p_vlc->b_die = 1;
393             break;
394
395         default:
396             break;
397         }
398     }
399 #endif
400
401     /*
402      * Size change
403      */
404     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
405     {
406         msg_Dbg( p_vout, "reinitializing framebuffer screen" );
407         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
408
409         /* Destroy XImages to change their size */
410         vout_End( p_vout );
411
412         /* Recreate XImages. If SysInit failed, the thread can't go on. */
413         if( vout_Init( p_vout ) )
414         {
415             msg_Err( p_vout, "cannot reinit framebuffer screen" );
416             return( 1 );
417         }
418
419         /* Clear screen */
420         memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
421
422 #if 0
423         /* Tell the video output thread that it will need to rebuild YUV
424          * tables. This is needed since conversion buffer size may have changed */
425         p_vout->i_changes |= VOUT_YUV_CHANGE;
426 #endif
427     }
428
429     return 0;
430 }
431
432 /*****************************************************************************
433  * vout_Render: renders previously calculated output
434  *****************************************************************************/
435 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
436 {
437     ;
438 }
439
440 /*****************************************************************************
441  * vout_Display: displays previously rendered output
442  *****************************************************************************
443  * This function send the currently rendered image to FB image, waits until
444  * it is displayed and switch the two rendering buffers, preparing next frame.
445  *****************************************************************************/
446 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
447 {
448     /* swap the two Y offsets if the drivers supports panning */
449     if( p_vout->p_sys->b_pan )
450     {
451         p_vout->p_sys->var_info.yoffset = 0;
452         //p_vout->p_sys->var_info.yoffset = p_vout->p_sys->var_info.yres;
453    
454         /* the X offset should be 0, but who knows ...
455          * some other app might have played with the framebuffer */
456         p_vout->p_sys->var_info.xoffset = 0;
457
458         ioctl( p_vout->p_sys->i_fd,
459                FBIOPAN_DISPLAY, &p_vout->p_sys->var_info );
460     }
461 }
462
463 #if 0
464 static void vout_SetPalette( vout_thread_t *p_vout,
465                              u16 *red, u16 *green, u16 *blue, u16 *transp )
466 {
467     struct fb_cmap cmap = { 0, 256, red, green, blue, transp };
468
469     ioctl( p_vout->p_sys->i_fd, FBIOPUTCMAP, &cmap );
470 }
471 #endif
472
473 /* following functions are local */
474
475 /*****************************************************************************
476  * OpenDisplay: initialize framebuffer
477  *****************************************************************************/
478 static int OpenDisplay( vout_thread_t *p_vout )
479 {
480     char *psz_device;                             /* framebuffer device path */
481     struct fb_fix_screeninfo    fix_info;     /* framebuffer fix information */
482
483     /* Open framebuffer device */
484     if( !(psz_device = config_GetPsz( p_vout, FB_DEV_VAR )) )
485     {
486         msg_Err( p_vout, "don't know which fb device to open" );
487         return( 1 );
488     }
489
490     p_vout->p_sys->i_fd = open( psz_device, O_RDWR);
491
492     if( p_vout->p_sys->i_fd == -1 )
493     {
494         msg_Err( p_vout, "cannot open %s (%s)", psz_device, strerror(errno) );
495         free( psz_device );
496         return( 1 );
497     }
498     free( psz_device );
499
500     /* Get framebuffer device informations */
501     if( ioctl( p_vout->p_sys->i_fd,
502                FBIOGET_VSCREENINFO, &p_vout->p_sys->var_info ) )
503     {
504         msg_Err( p_vout, "cannot get fb info (%s)", strerror(errno) );
505         close( p_vout->p_sys->i_fd );
506         return( 1 );
507     }
508
509     if( ioctl( p_vout->p_sys->i_fd,
510                FBIOGET_VSCREENINFO, &p_vout->p_sys->old_info ) )
511     {
512         msg_Err( p_vout, "cannot get 2nd fb info (%s)", strerror(errno) );
513         close( p_vout->p_sys->i_fd );
514         return( 1 );
515     }
516
517     /* Set some attributes */
518     p_vout->p_sys->var_info.activate = FB_ACTIVATE_NXTOPEN;
519     p_vout->p_sys->var_info.xoffset =  0;
520     p_vout->p_sys->var_info.yoffset =  0;
521
522     if( ioctl( p_vout->p_sys->i_fd,
523                FBIOPUT_VSCREENINFO, &p_vout->p_sys->var_info ) )
524     {
525         msg_Err( p_vout, "cannot set fb info (%s)", strerror(errno) );
526         close( p_vout->p_sys->i_fd );
527         return( 1 );
528     }
529
530     /* Get some informations again, in the definitive configuration */
531     if( ioctl( p_vout->p_sys->i_fd, FBIOGET_FSCREENINFO, &fix_info )
532          || ioctl( p_vout->p_sys->i_fd,
533                    FBIOGET_VSCREENINFO, &p_vout->p_sys->var_info ) )
534     {
535         msg_Err( p_vout, "cannot get additional fb info (%s)",
536                           strerror(errno) );
537
538         /* Restore fb config */
539         ioctl( p_vout->p_sys->i_fd,
540                FBIOPUT_VSCREENINFO, &p_vout->p_sys->var_info );
541
542         close( p_vout->p_sys->i_fd );
543         return( 1 );
544     }
545
546     /* FIXME: if the image is full-size, it gets cropped on the left
547      * because of the xres / xres_virtual slight difference */
548     msg_Dbg( p_vout, "%ix%i (virtual %ix%i)",
549              p_vout->p_sys->var_info.xres, p_vout->p_sys->var_info.yres,
550              p_vout->p_sys->var_info.xres_virtual,
551              p_vout->p_sys->var_info.yres_virtual );
552
553     p_vout->p_sys->i_height = p_vout->p_sys->var_info.yres;
554     p_vout->p_sys->i_width  = p_vout->p_sys->var_info.xres_virtual
555                                ? p_vout->p_sys->var_info.xres_virtual
556                                : p_vout->p_sys->var_info.xres;
557
558     p_vout->p_sys->p_palette = NULL;
559     p_vout->p_sys->b_pan = ( fix_info.ypanstep || fix_info.ywrapstep );
560
561     switch( p_vout->p_sys->var_info.bits_per_pixel )
562     {
563     case 8:
564         p_vout->p_sys->p_palette = malloc( 8 * 256 * sizeof( u16 ) );
565         p_vout->p_sys->fb_cmap.start = 0;
566         p_vout->p_sys->fb_cmap.len = 256;
567         p_vout->p_sys->fb_cmap.red = p_vout->p_sys->p_palette;
568         p_vout->p_sys->fb_cmap.green = p_vout->p_sys->p_palette + 256 * sizeof( u16 );
569         p_vout->p_sys->fb_cmap.blue = p_vout->p_sys->p_palette + 2 * 256 * sizeof( u16 );
570         p_vout->p_sys->fb_cmap.transp = p_vout->p_sys->p_palette + 3 * 256 * sizeof( u16 );
571
572         /* Save the colormap */
573         ioctl( p_vout->p_sys->i_fd, FBIOGETCMAP, &p_vout->p_sys->fb_cmap );
574
575         p_vout->p_sys->i_bytes_per_pixel = 1;
576         break;
577
578     case 15:
579     case 16:
580         p_vout->p_sys->i_bytes_per_pixel = 2;
581         break;
582
583     case 24:
584         p_vout->p_sys->i_bytes_per_pixel = 3;
585         break;
586
587     case 32:
588         p_vout->p_sys->i_bytes_per_pixel = 4;
589         break;
590
591     default:
592         msg_Err( p_vout, "screen depth %d is not supported",
593                          p_vout->p_sys->var_info.bits_per_pixel );
594
595         /* Restore fb config */
596         ioctl( p_vout->p_sys->i_fd,
597                FBIOPUT_VSCREENINFO, &p_vout->p_sys->var_info );
598
599         close( p_vout->p_sys->i_fd );
600         return 1;
601     }
602
603     p_vout->p_sys->i_page_size = p_vout->p_sys->i_width *
604                 p_vout->p_sys->i_height * p_vout->p_sys->i_bytes_per_pixel;
605
606     /* Map a framebuffer at the beginning */
607     p_vout->p_sys->p_video = mmap( 0, p_vout->p_sys->i_page_size,
608                                    PROT_READ | PROT_WRITE, MAP_SHARED,
609                                    p_vout->p_sys->i_fd, 0 );
610
611     if( p_vout->p_sys->p_video == ((void*)-1) )
612     {
613         msg_Err( p_vout, "cannot map video memory (%s)", strerror(errno) );
614
615         if( p_vout->p_sys->var_info.bits_per_pixel == 8 )
616         {
617             free( p_vout->p_sys->p_palette );
618         }
619
620         /* Restore fb config */
621         ioctl( p_vout->p_sys->i_fd,
622                FBIOPUT_VSCREENINFO, &p_vout->p_sys->var_info );
623
624         close( p_vout->p_sys->i_fd );
625         return( 1 );
626     }
627
628     msg_Dbg( p_vout, "framebuffer type=%d, visual=%d, ypanstep=%d, "
629              "ywrap=%d, accel=%d", fix_info.type, fix_info.visual,
630              fix_info.ypanstep, fix_info.ywrapstep, fix_info.accel );
631     return( 0 );
632 }
633
634 /*****************************************************************************
635  * CloseDisplay: terminate FB video thread output method
636  *****************************************************************************/
637 static void CloseDisplay( vout_thread_t *p_vout )
638 {
639     /* Clear display */
640     memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
641
642     /* Restore palette */
643     if( p_vout->p_sys->var_info.bits_per_pixel == 8 );
644     {
645         ioctl( p_vout->p_sys->i_fd,
646                FBIOPUTCMAP, &p_vout->p_sys->fb_cmap );
647         free( p_vout->p_sys->p_palette );
648     }
649
650     /* Restore fb config */
651     ioctl( p_vout->p_sys->i_fd,
652            FBIOPUT_VSCREENINFO, &p_vout->p_sys->var_info );
653
654     /* Close fb */
655     close( p_vout->p_sys->i_fd );
656 }
657
658 /*****************************************************************************
659  * SwitchDisplay: VT change signal handler
660  *****************************************************************************
661  * This function activates or deactivates the output of the thread. It is
662  * called by the VT driver, on terminal change.
663  *****************************************************************************/
664 static void SwitchDisplay(int i_signal)
665 {
666 #if 0
667     vout_thread_t *p_vout;
668
669     vlc_mutex_lock( &p_vout_bank->lock );
670
671     /* XXX: only test the first video output */
672     if( p_vout_bank->i_count )
673     {
674         p_vout = p_vout_bank->pp_vout[0];
675
676         switch( i_signal )
677         {
678         case SIGUSR1:                                /* vt has been released */
679             p_vout->b_active = 0;
680             ioctl( p_vout->p_sys->i_tty, VT_RELDISP, 1 );
681             break;
682         case SIGUSR2:                                /* vt has been acquired */
683             p_vout->b_active = 1;
684             ioctl( p_vout->p_sys->i_tty, VT_RELDISP, VT_ACTIVATE );
685             /* handle blanking */
686             vlc_mutex_lock( &p_vout->change_lock );
687             p_vout->i_changes |= VOUT_SIZE_CHANGE;
688             vlc_mutex_unlock( &p_vout->change_lock );
689             break;
690         }
691     }
692
693     vlc_mutex_unlock( &p_vout_bank->lock );
694 #endif
695 }
696
697 /*****************************************************************************
698  * TextMode and GfxMode : switch tty to text/graphic mode
699  *****************************************************************************
700  * These functions toggle the tty mode.
701  *****************************************************************************/
702 static void TextMode( int i_tty )
703 {
704     /* return to text mode */
705     if( -1 == ioctl(i_tty, KDSETMODE, KD_TEXT) )
706     {
707 //X        msg_Err( p_vout, "failed ioctl KDSETMODE KD_TEXT" );
708     }
709 }
710
711 static void GfxMode( int i_tty )
712 {
713     /* switch to graphic mode */
714     if( -1 == ioctl(i_tty, KDSETMODE, KD_GRAPHICS) )
715     {
716 //X        msg_Err( p_vout, "failed ioctl KDSETMODE KD_GRAPHICS" );
717     }
718 }
719