<commit_before>/*****************************************************************************
 * fb.c : framebuffer plugin for vlc
 *****************************************************************************
 * Copyright (C) 2000, 2001 the VideoLAN team
 * $Id$
 *
 * Authors: <NAME> <<EMAIL>>
 *          <NAME>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <errno.h>                                                 /* ENOMEM */
#include <signal.h>                                      /* SIGUSR1, SIGUSR2 */
#include <fcntl.h>                                                 /* open() */
#include <unistd.h>                                               /* close() */

#include <termios.h>                                       /* struct termios */
#include <sys/ioctl.h>
#include <sys/mman.h>                                              /* mmap() */

#include <linux/fb.h>
#include <linux/vt.h>                                                /* VT_* */
#include <linux/kd.h>                                                 /* KD* */

#include <vlc/vlc.h>
#include <vlc_vout.h>

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Create    ( vlc_object_t * );
static void Destroy   ( vlc_object_t * );

static int  Init      ( vout_thread_t * );
static void End       ( vout_thread_t * );
static int  Manage    ( vout_thread_t * );
static void Display   ( vout_thread_t *, picture_t * );
static int  Control   ( vout_thread_t *, int, va_list );

static int  OpenDisplay    ( vout_thread_t * );
static void CloseDisplay   ( vout_thread_t * );
static void SwitchDisplay  ( int i_signal );
static void TextMode       ( int i_tty );
static void GfxMode        ( int i_tty );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
#define FB_DEV_VAR "fbdev"

#define DEVICE_TEXT N_("Framebuffer device")
#define DEVICE_LONGTEXT N_( \
    "Framebuffer device to use for rendering (usually /dev/fb0).")

#define TTY_TEXT N_("Run fb on current tty.")
#define TTY_LONGTEXT N_( \
    "Run framebuffer on current TTY device (default enabled). " \
    "(disable tty handling with caution)" );

#define CHROMA_TEXT N_("Chroma used.")
#define CHROMA_LONGTEXT N_( \
    "Force use of a specific chroma for output. Default is I420." )

#define ASPECT_RATIO_TEXT N_("Video aspect ratio")
#define ASPECT_RATIO_LONGTEXT N_( \
    "Aspect ratio of the video image (4:3, 16:9). Default is square pixels." )

#define FB_MODE_TEXT N_("Framebuffer resolution to use.")
#define FB_MODE_LONGTEXT N_( \
    "Select the resolution for the framebuffer. Currently it supports " \
    "the values 0=QCIF 1=CIF 2=NTSC 3=PAL (default 3=PAL)" )

vlc_module_begin();
    set_shortname( "Framebuffer" );
    set_category( CAT_VIDEO );
    set_subcategory( SUBCAT_VIDEO_VOUT );
    add_file( FB_DEV_VAR, "/dev/fb0", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
              VLC_FALSE );
    add_bool( "fb-tty", 1, NULL, TTY_TEXT, TTY_LONGTEXT, VLC_TRUE );
    add_string( "fb-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
                VLC_TRUE );
    add_string( "fb-aspect-ratio", NULL, NULL, ASPECT_RATIO_TEXT,
                ASPECT_RATIO_LONGTEXT, VLC_TRUE );
    add_integer( "fb-mode", 3, NULL, FB_MODE_TEXT, FB_MODE_LONGTEXT,
                 VLC_TRUE );
    set_description( _("GNU/Linux console framebuffer video output") );
    set_capability( "video output", 30 );
    set_callbacks( Create, Destroy );
vlc_module_end();

/*****************************************************************************
 * vout_sys_t: video output framebuffer method descriptor
 *****************************************************************************
 * This structure is part of the video output thread descriptor.
 * It describes the FB specific properties of an output thread.
 *****************************************************************************/
struct vout_sys_t
{
    /* System information */
    int                 i_tty;                          /* tty device handle */
    vlc_bool_t          b_tty;
    struct termios      old_termios;

    /* Original configuration information */
    struct sigaction            sig_usr1;           /* USR1 previous handler */
    struct sigaction            sig_usr2;           /* USR2 previous handler */
    struct vt_mode              vt_mode;                 /* previous VT mode */

    /* Framebuffer information */
    int                         i_fd;                       /* device handle */
    struct fb_var_screeninfo    old_info;       /* original mode information */
    struct fb_var_screeninfo    var_info;        /* current mode information */
    vlc_bool_t                  b_pan;     /* does device supports panning ? */
    struct fb_cmap              fb_cmap;                /* original colormap */
    uint16_t                    *p_palette;              /* original palette */

    /* Video information */
    int i_width;
    int i_height;
    int i_aspect;
    int i_bytes_per_pixel;
    vlc_fourcc_t i_chroma;

    /* Video memory */
    byte_t *    p_video;                                      /* base adress */
    size_t      i_page_size;                                    /* page size */
};

/*****************************************************************************
 * Create: allocates FB video thread output method
 *****************************************************************************
 * This function allocates and initializes a FB vout method.
 *****************************************************************************/
static int Create( vlc_object_t *p_this )
{
    vout_thread_t *p_vout = (vout_thread_t *)p_this;
    vout_sys_t    *p_sys;
    char          *psz_chroma;
    char          *psz_aspect;
    int           i_mode;
    struct sigaction    sig_tty;                 /* sigaction for tty change */
    struct vt_mode      vt_mode;                          /* vt current mode */
    struct termios      new_termios;

    /* Allocate instance and initialize some members */
    p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
    if( p_vout->p_sys == NULL )
    {
        msg_Err( p_vout, "out of memory" );
        return VLC_ENOMEM;
    };
    memset( p_sys, 0, sizeof(vout_sys_t) );

    p_vout->pf_init = Init;
    p_vout->pf_end = End;
    p_vout->pf_manage = Manage;
    p_vout->pf_render = NULL;
    p_vout->pf_display = Display;
    p_vout->pf_control = Control;

    /* Set tty and fb devices */
    p_sys->i_tty = 0; /* 0 == /dev/tty0 == current console */
    p_sys->b_tty = var_CreateGetBool( p_vout, "fb-tty" );
#ifndef WIN32
#if defined(HAVE_ISATTY)
    /* Check that stdin is a TTY */
    if( !p_sys->b_tty && !isatty( 0 ) )
    {
        msg_Warn( p_vout, "fd 0 is not a TTY" );
        return VLC_EGENERIC;
    }
    else
    {
        msg_Warn( p_vout, "disabling tty handling, use with caution because "
                          "there is no way to return to the tty." );
    }
#endif
#endif

    psz_chroma = var_CreateGetNonEmptyString( p_vout, "fb-chroma" );
    if( psz_chroma )
    {
        if( strlen( psz_chroma ) == 4 )
        {
            p_sys->i_chroma = VLC_FOURCC( psz_chroma[0],
                                   psz_chroma[1],
                                   psz_chroma[2],
                                   psz_chroma[3] );
            msg_Dbg( p_vout, "forcing chroma '%s'", psz_chroma );
        }
        else<commit_msg>Only test the terminal if --fb-tty is used.<commit_after>/*****************************************************************************
 * fb.c : framebuffer plugin for vlc
 *****************************************************************************
 * Copyright (C) 2000, 2001 the VideoLAN team
 * $Id$
 *
 * Authors: <NAME> <<EMAIL>>
 *          <NAME>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <errno.h>                                                 /* ENOMEM */
#include <signal.h>                                      /* SIGUSR1, SIGUSR2 */
#include <fcntl.h>                                                 /* open() */
#include <unistd.h>                                               /* close() */

#include <termios.h>                                       /* struct termios */
#include <sys/ioctl.h>
#include <sys/mman.h>                                              /* mmap() */

#include <linux/fb.h>
#include <linux/vt.h>                                                /* VT_* */
#include <linux/kd.h>                                                 /* KD* */

#include <vlc/vlc.h>
#include <vlc_vout.h>

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Create    ( vlc_object_t * );
static void Destroy   ( vlc_object_t * );

static int  Init      ( vout_thread_t * );
static void End       ( vout_thread_t * );
static int  Manage    ( vout_thread_t * );
static void Display   ( vout_thread_t *, picture_t * );
static int  Control   ( vout_thread_t *, int, va_list );

static int  OpenDisplay    ( vout_thread_t * );
static void CloseDisplay   ( vout_thread_t * );
static void SwitchDisplay  ( int i_signal );
static void TextMode       ( int i_tty );
static void GfxMode        ( int i_tty );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
#define FB_DEV_VAR "fbdev"

#define DEVICE_TEXT N_("Framebuffer device")
#define DEVICE_LONGTEXT N_( \
    "Framebuffer device to use for rendering (usually /dev/fb0).")

#define TTY_TEXT N_("Run fb on current tty.")
#define TTY_LONGTEXT N_( \
    "Run framebuffer on current TTY device (default enabled). " \
    "(disable tty handling with caution)" );

#define CHROMA_TEXT N_("Chroma used.")
#define CHROMA_LONGTEXT N_( \
    "Force use of a specific chroma for output. Default is I420." )

#define ASPECT_RATIO_TEXT N_("Video aspect ratio")
#define ASPECT_RATIO_LONGTEXT N_( \
    "Aspect ratio of the video image (4:3, 16:9). Default is square pixels." )

#define FB_MODE_TEXT N_("Framebuffer resolution to use.")
#define FB_MODE_LONGTEXT N_( \
    "Select the resolution for the framebuffer. Currently it supports " \
    "the values 0=QCIF 1=CIF 2=NTSC 3=PAL (default 3=PAL)" )

vlc_module_begin();
    set_shortname( "Framebuffer" );
    set_category( CAT_VIDEO );
    set_subcategory( SUBCAT_VIDEO_VOUT );
    add_file( FB_DEV_VAR, "/dev/fb0", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
              VLC_FALSE );
    add_bool( "fb-tty", 1, NULL, TTY_TEXT, TTY_LONGTEXT, VLC_TRUE );
    add_string( "fb-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
                VLC_TRUE );
    add_string( "fb-aspect-ratio", NULL, NULL, ASPECT_RATIO_TEXT,
                ASPECT_RATIO_LONGTEXT, VLC_TRUE );
    add_integer( "fb-mode", 3, NULL, FB_MODE_TEXT, FB_MODE_LONGTEXT,
                 VLC_TRUE );
    set_description( _("GNU/Linux console framebuffer video output") );
    set_capability( "video output", 30 );
    set_callbacks( Create, Destroy );
vlc_module_end();

/*****************************************************************************
 * vout_sys_t: video output framebuffer method descriptor
 *****************************************************************************
 * This structure is part of the video output thread descriptor.
 * It describes the FB specific properties of an output thread.
 *****************************************************************************/
struct vout_sys_t
{
    /* System information */
    int                 i_tty;                          /* tty device handle */
    vlc_bool_t          b_tty;
    struct termios      old_termios;

    /* Original configuration information */
    struct sigaction            sig_usr1;           /* USR1 previous handler */
    struct sigaction            sig_usr2;           /* USR2 previous handler */
    struct vt_mode              vt_mode;                 /* previous VT mode */

    /* Framebuffer information */
    int                         i_fd;                       /* device handle */
    struct fb_var_screeninfo    old_info;       /* original mode information */
    struct fb_var_screeninfo    var_info;        /* current mode information */
    vlc_bool_t                  b_pan;     /* does device supports panning ? */
    struct fb_cmap              fb_cmap;                /* original colormap */
    uint16_t                    *p_palette;              /* original palette */

    /* Video information */
    int i_width;
    int i_height;
    int i_aspect;
    int i_bytes_per_pixel;
    vlc_fourcc_t i_chroma;

    /* Video memory */
    byte_t *    p_video;                                      /* base adress */
    size_t      i_page_size;                                    /* page size */
};

/*****************************************************************************
 * Create: allocates FB video thread output method
 *****************************************************************************
 * This function allocates and initializes a FB vout method.
 *****************************************************************************/
static int Create( vlc_object_t *p_this )
{
    vout_thread_t *p_vout = (vout_thread_t *)p_this;
    vout_sys_t    *p_sys;
    char          *psz_chroma;
    char          *psz_aspect;
    int           i_mode;
    struct sigaction    sig_tty;                 /* sigaction for tty change */
    struct vt_mode      vt_mode;                          /* vt current mode */
    struct termios      new_termios;

    /* Allocate instance and initialize some members */
    p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
    if( p_vout->p_sys == NULL )
    {
        msg_Err( p_vout, "out of memory" );
        return VLC_ENOMEM;
    };
    memset( p_sys, 0, sizeof(vout_sys_t) );

    p_vout->pf_init = Init;
    p_vout->pf_end = End;
    p_vout->pf_manage = Manage;
    p_vout->pf_render = NULL;
    p_vout->pf_display = Display;
    p_vout->pf_control = Control;

    /* Set tty and fb devices */
    p_sys->i_tty = 0; /* 0 == /dev/tty0 == current console */
    p_sys->b_tty = var_CreateGetBool( p_vout, "fb-tty" );
#ifndef WIN32
#if defined(HAVE_ISATTY)
    /* Check that stdin is a TTY */
    if( p_sys->b_tty && !isatty( 0 ) )
    {
        msg_Warn( p_vout, "fd 0 is not a TTY" );
        return VLC_EGENERIC;
    }
    else
    {
        msg_Warn( p_vout, "disabling tty handling, use with caution because "
                          "there is no way to return to the tty." );
    }
#endif
#endif

    psz_chroma = var_CreateGetNonEmptyString( p_vout, "fb-chroma" );
    if( psz_chroma )
    {
        if( strlen( psz_chroma ) == 4 )
        {
            p_sys->i_chroma = VLC_FOURCC( psz_chroma[0],
                                   psz_chroma[1],
                                   psz_chroma[2],
                                   psz_chroma[3] );
            msg_Dbg( p_vout, "forcing chroma '%s'", psz_chroma );
        }
        else
{'fraction_non_alphanumeric': 0.2177612027271138, 'fraction_numerical': 0.0069925994988637025, 'mean_word_length': 2.3823413480488767, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2728008', 'n_tokens_mistral': 4946, 'n_tokens_neox': 4673, 'n_words': 1346}