]> git.sesse.net Git - vlc/blobdiff - modules/video_output/aa.c
aa: fix compilation (collision with Xlib symbols).
[vlc] / modules / video_output / aa.c
index a46998fb0259a8397ede28b656b068f7068205c0..420767b587ead5383276bd47a4d67cb171294f02 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * vout_aa.c: Aa video output display method for testing purposes
+ * aa.c: "vout display" module using aalib
  *****************************************************************************
  * Copyright (C) 2002-2009 the VideoLAN team
  * $Id$
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <errno.h>                                                 /* ENOMEM */
-
-#include <aalib.h>
-
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
 
 #include <vlc_common.h>
 #include <vlc_plugin.h>
-#include <vlc_vout.h>
-#include <vlc_interface.h>
-
-/*****************************************************************************
- * Local prototypes
- *****************************************************************************/
-static int  Create    ( vlc_object_t * );
-static void Destroy   ( vlc_object_t * );
+#include <vlc_vout_display.h>
+#include <vlc_picture_pool.h>
 
-static int  Init      ( vout_thread_t * );
-static void End       ( vout_thread_t * );
-static int  Manage    ( vout_thread_t * );
-static void Render    ( vout_thread_t *, picture_t * );
-static void Display   ( vout_thread_t *, picture_t * );
+#include <assert.h>
+#include <aalib.h>
 
-static void SetPalette     ( vout_thread_t *, uint16_t *, uint16_t *, uint16_t * );
+#ifndef WIN32
+# ifdef X_DISPLAY_MISSING
+#  error Xlib required due to XInitThreads
+# endif
+# include <vlc_xlib.h>
+#endif
 
+/* TODO
+ * - what about RGB palette ?
+ */
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
-vlc_module_begin ()
-    set_shortname( N_("ASCII Art"))
-    set_category( CAT_VIDEO )
-    set_subcategory( SUBCAT_VIDEO_VOUT )
-    set_description( N_("ASCII-art video output") )
-    set_capability( "video output", 10 )
-    add_shortcut( "aalib" )
-    set_callbacks( Create, Destroy )
-vlc_module_end ()
+static int  Open (vlc_object_t *);
+static void Close(vlc_object_t *);
+
+vlc_module_begin()
+    set_shortname(N_("ASCII Art"))
+    set_category(CAT_VIDEO)
+    set_subcategory(SUBCAT_VIDEO_VOUT)
+    set_description(N_("ASCII-art video output"))
+    set_capability("vout display", 10)
+    add_shortcut("aalib")
+    set_callbacks(Open, Close)
+vlc_module_end()
 
 /*****************************************************************************
- * vout_sys_t: aa video output method descriptor
- *****************************************************************************
- * This structure is part of the video output thread descriptor.
- * It describes the aa specific properties of an output thread.
+ * Local prototypes
  *****************************************************************************/
-struct vout_sys_t
-{
+static picture_pool_t *Pool   (vout_display_t *, unsigned);
+static void            Prepare(vout_display_t *, picture_t *);
+static void            PictureDisplay(vout_display_t *, picture_t *);
+static int             Control(vout_display_t *, int, va_list);
+
+/* */
+static void Manage(vout_display_t *);
+
+/* */
+struct vout_display_sys_t {
     struct aa_context*  aa_context;
     aa_palette          palette;
-    int                 i_width;                     /* width of main window */
-    int                 i_height;                   /* height of main window */
+
+    vout_display_cfg_t  state;
+    picture_pool_t      *pool;
 };
 
-/*****************************************************************************
- * Create: allocates aa video thread output method
- *****************************************************************************
+/**
  * This function allocates and initializes a aa vout method.
- *****************************************************************************/
-static int Create( vlc_object_t *p_this )
+ */
+static int Open(vlc_object_t *object)
 {
-    vout_thread_t *p_vout = (vout_thread_t *)p_this;
+    vout_display_t *vd = (vout_display_t *)object;
+    vout_display_sys_t *sys;
+
+#ifndef WIN32
+    if (!vlc_xlib_init (object))
+        return VLC_EGENERIC;
+#endif
 
     /* Allocate structure */
-    p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
-    if( p_vout->p_sys == NULL )
+    vd->sys = sys = calloc(1, sizeof(*sys));
+    if (!sys)
         return VLC_ENOMEM;
 
     /* Don't parse any options, but take $AAOPTS into account */
-    aa_parseoptions( NULL, NULL, NULL, NULL );
+    aa_parseoptions(NULL, NULL, NULL, NULL);
 
-    if (!(p_vout->p_sys->aa_context = aa_autoinit(&aa_defparams)))
-    {
-        msg_Err( p_vout, "cannot initialize aalib" );
-        free( p_vout->p_sys );
-        return VLC_EGENERIC;
+    /* */
+    sys->aa_context = aa_autoinit(&aa_defparams);
+    if (!sys->aa_context) {
+        msg_Err(vd, "cannot initialize aalib");
+        goto error;
     }
-
-    p_vout->pf_init = Init;
-    p_vout->pf_end = End;
-    p_vout->pf_manage = Manage;
-    p_vout->pf_render = Render;
-    p_vout->pf_display = Display;
-
-    p_vout->p_sys->i_width = aa_imgwidth(p_vout->p_sys->aa_context);
-    p_vout->p_sys->i_height = aa_imgheight(p_vout->p_sys->aa_context);
-    aa_autoinitkbd( p_vout->p_sys->aa_context, 0 );
-    aa_autoinitmouse( p_vout->p_sys->aa_context, AA_MOUSEPRESSMASK );
-    aa_hidemouse( p_vout->p_sys->aa_context );
+    vout_display_DeleteWindow(vd, NULL);
+
+    aa_autoinitkbd(sys->aa_context, 0);
+    aa_autoinitmouse(sys->aa_context, AA_MOUSEALLMASK);
+
+    /* */
+    video_format_t fmt = vd->fmt;
+    fmt.i_chroma = VLC_CODEC_RGB8;
+    fmt.i_width  = aa_imgwidth(sys->aa_context);
+    fmt.i_height = aa_imgheight(sys->aa_context);
+
+    /* */
+    vout_display_info_t info = vd->info;
+    info.has_pictures_invalid = true;
+
+    /* Setup vout_display now that everything is fine */
+    vd->fmt = fmt;
+    vd->info = info;
+
+    vd->pool    = Pool;
+    vd->prepare = Prepare;
+    vd->display = PictureDisplay;
+    vd->control = Control;
+    vd->manage  = Manage;
+
+    /* Inspect initial configuration and send correction events
+     * FIXME how to handle aspect ratio with aa ? */
+    sys->state = *vd->cfg;
+    sys->state.is_fullscreen = false;
+    vout_display_SendEventFullscreen(vd, false);
+    vout_display_SendEventDisplaySize(vd, fmt.i_width, fmt.i_height, false);
 
     return VLC_SUCCESS;
+
+error:
+    if (sys && sys->aa_context)
+        aa_close(sys->aa_context);
+    free(sys);
+    return VLC_EGENERIC;
 }
 
-/*****************************************************************************
- * Init: initialize aa video thread output method
- *****************************************************************************/
-static int Init( vout_thread_t *p_vout )
+/**
+ * Close a aa video output method
+ */
+static void Close(vlc_object_t *object)
 {
-    int i_index;
-    picture_t *p_pic = NULL;
-
-    I_OUTPUTPICTURES = 0;
-
-    p_vout->output.i_chroma = VLC_CODEC_RGB8;
-    p_vout->output.i_width = p_vout->p_sys->i_width;
-    p_vout->output.i_height = p_vout->p_sys->i_height;
-    p_vout->output.i_aspect = p_vout->p_sys->i_width
-                               * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
-    p_vout->output.pf_setpalette = SetPalette;
-
-    /* Find an empty picture slot */
-    for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
-    {
-        if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
-        {
-            p_pic = p_vout->p_picture + i_index;
-            break;
-        }
-    }
+    vout_display_t *vd = (vout_display_t *)object;
+    vout_display_sys_t *sys = vd->sys;
 
-    if( p_pic == NULL )
-        return VLC_EGENERIC;
+    if (sys->pool)
+        picture_pool_Delete(sys->pool);
+    aa_close(sys->aa_context);
+    free(sys);
+}
 
-    /* Allocate the picture */
-    p_pic->p->p_pixels = aa_image( p_vout->p_sys->aa_context );
-    p_pic->p->i_lines = p_vout->p_sys->i_height;
-    p_pic->p->i_visible_lines = p_vout->p_sys->i_height;
-    p_pic->p->i_pitch = p_vout->p_sys->i_width;
-    p_pic->p->i_pixel_pitch = 1;
-    p_pic->p->i_visible_pitch = p_vout->p_sys->i_width;
-    p_pic->i_planes = 1;
+/**
+ * Return a pool of direct buffers
+ */
+static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
+{
+    vout_display_sys_t *sys = vd->sys;
+    VLC_UNUSED(count);
 
-    p_pic->i_status = DESTROYED_PICTURE;
-    p_pic->i_type   = DIRECT_PICTURE;
+    if (!sys->pool) {
+        picture_resource_t rsc;
 
-    PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
-    I_OUTPUTPICTURES++;
+        memset(&rsc, 0, sizeof(rsc));
+        rsc.p[0].p_pixels = aa_image(sys->aa_context);
+        rsc.p[0].i_pitch = aa_imgwidth(sys->aa_context);
+        rsc.p[0].i_lines = aa_imgheight(sys->aa_context);
 
-    return VLC_SUCCESS;
-}
+        picture_t *p_picture = picture_NewFromResource(&vd->fmt, &rsc);
+        if (!p_picture)
+            return NULL;
 
-/*****************************************************************************
- * End: terminate aa video thread output method
- *****************************************************************************/
-static void End( vout_thread_t *p_vout )
-{
-    ;
+        sys->pool = picture_pool_New(1, &p_picture);
+    }
+    return sys->pool;
 }
 
-/*****************************************************************************
- * Destroy: destroy aa video thread output method
- *****************************************************************************
- * Terminate an output method created by AaCreateOutputMethod
- *****************************************************************************/
-static void Destroy( vlc_object_t *p_this )
+/**
+ * Prepare a picture for display */
+static void Prepare(vout_display_t *vd, picture_t *picture)
 {
-    vout_thread_t *p_vout = (vout_thread_t *)p_this;
+    vout_display_sys_t *sys = vd->sys;
 
-    aa_close( p_vout->p_sys->aa_context );
-    free( p_vout->p_sys );
-}
+    assert(vd->fmt.i_width  == aa_imgwidth(sys->aa_context) &&
+           vd->fmt.i_height == aa_imgheight(sys->aa_context));
 
-/*****************************************************************************
- * Manage: handle aa events
- *****************************************************************************
- * This function should be called regularly by video output thread. It manages
- * console events. It returns a non null value on error.
- *****************************************************************************/
-static int Manage( vout_thread_t *p_vout )
-{
-    int event, x, y, b;
-    event = aa_getevent( p_vout->p_sys->aa_context, 0 );
-    switch ( event )
-    {
-    case AA_MOUSE:
-        aa_getmouse( p_vout->p_sys->aa_context, &x, &y, &b );
-        if ( b & AA_BUTTON3 )
-            var_SetBool( p_vout->p_libvlc, "intf-popupmenu", true );
-        break;
-    case AA_RESIZE:
-        p_vout->i_changes |= VOUT_SIZE_CHANGE;
-        aa_resize( p_vout->p_sys->aa_context );
-        p_vout->p_sys->i_width = aa_imgwidth( p_vout->p_sys->aa_context );
-        p_vout->p_sys->i_height = aa_imgheight( p_vout->p_sys->aa_context );
-        break;
-    default:
-        break;
+#if 0
+    if (picture->format.p_palette) {
+        for (int i = 0; i < 256; i++) {
+            aa_setpalette(vd->sys->palette, 256 - i,
+                           red[ i ], green[ i ], blue[ i ]);
+        }
     }
-    return VLC_SUCCESS;
+#else
+    VLC_UNUSED(picture);
+#endif
+
+    aa_fastrender(sys->aa_context, 0, 0,
+                  vd->fmt.i_width, vd->fmt.i_height);
 }
 
-/*****************************************************************************
- * Render: render previously calculated output
- *****************************************************************************/
-static void Render( vout_thread_t *p_vout, picture_t *p_pic )
+/**
+ * Display a picture
+ */
+static void PictureDisplay(vout_display_t *vd, picture_t *picture)
 {
-  aa_fastrender( p_vout->p_sys->aa_context, 0, 0,
-                 aa_imgwidth( p_vout->p_sys->aa_context ),
-                 aa_imgheight( p_vout->p_sys->aa_context ) );
+    vout_display_sys_t *sys = vd->sys;
+
+    aa_flush(sys->aa_context);
+    picture_Release(picture);
 }
 
-/*****************************************************************************
- * Display: displays previously rendered output
- *****************************************************************************/
-static void Display( vout_thread_t *p_vout, picture_t *p_pic )
+/**
+ * Control for vout display
+ */
+static int Control(vout_display_t *vd, int query, va_list args)
 {
-    /* No need to do anything, the fake direct buffers stay as they are */
-    unsigned int i_width, i_height, i_x, i_y;
+    VLC_UNUSED(args);
+    vout_display_sys_t *sys = vd->sys;
+
+    switch (query) {
+    case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
+        /* We have to ignore what is requested */
+        vout_display_SendEventPicturesInvalid(vd);
+        return VLC_SUCCESS;
+
+    case VOUT_DISPLAY_RESET_PICTURES:
+        if (sys->pool)
+            picture_pool_Delete(sys->pool);
+        sys->pool = NULL;
 
-    vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
-                       &i_x, &i_y, &i_width, &i_height );
+        vd->fmt.i_width  = aa_imgwidth(sys->aa_context);
+        vd->fmt.i_height = aa_imgheight(sys->aa_context);
+        return VLC_SUCCESS;
 
-    aa_flush(p_vout->p_sys->aa_context);
+    case VOUT_DISPLAY_HIDE_MOUSE:
+        aa_hidemouse(sys->aa_context);
+        return VLC_SUCCESS;
+
+    default:
+        msg_Err(vd, "Unsupported query in vout display aalib");
+        return VLC_EGENERIC;
+    }
 }
 
-/*****************************************************************************
- * SetPalette: set the 8bpp palette
- *****************************************************************************/
-static void SetPalette( vout_thread_t *p_vout,
-                        uint16_t *red, uint16_t *green, uint16_t *blue )
+
+/**
+ * Proccess pending event
+ */
+static void Manage(vout_display_t *vd)
 {
-    int i;
+    vout_display_sys_t *sys = vd->sys;
+
+    for (;;) {
+        const int event = aa_getevent(sys->aa_context, 0);
+        if (!event)
+            return;
+
+        switch (event) {
+        case AA_MOUSE: {
+            int x, y;
+            int button;
+            int vlc;
+            aa_getmouse(sys->aa_context, &x, &y, &button);
+
+            vlc = 0;
+            if (button & AA_BUTTON1)
+                vlc |= 1 << MOUSE_BUTTON_LEFT;
+            if (button & AA_BUTTON2)
+                vlc |= 1 << MOUSE_BUTTON_CENTER;
+            if (button & AA_BUTTON3)
+                vlc |= 1 << MOUSE_BUTTON_RIGHT;
+
+            vout_display_SendEventMouseState(vd, x, y, vlc);
+
+            aa_showcursor(sys->aa_context); /* Not perfect, we show it on click too */
+            break;
+        }
 
-    /* Fill colors with color information */
-    for( i = 0; i < 256; i++ )
-    {
-        aa_setpalette( p_vout->p_sys->palette, 256 -i,
-                       red[ i ], green[ i ], blue[ i ] );
+        case AA_RESIZE:
+            aa_resize(sys->aa_context);
+            vout_display_SendEventDisplaySize(vd,
+                                              aa_imgwidth(sys->aa_context),
+                                              aa_imgheight(sys->aa_context), false);
+            break;
+
+        /* TODO keys support to complete */
+        case AA_UP:
+            vout_display_SendEventKey(vd, KEY_UP);
+            break;
+        case AA_DOWN:
+            vout_display_SendEventKey(vd, KEY_DOWN);
+            break;
+        case AA_RIGHT:
+            vout_display_SendEventKey(vd, KEY_RIGHT);
+            break;
+        case AA_LEFT:
+            vout_display_SendEventKey(vd, KEY_LEFT);
+            break;
+        case AA_BACKSPACE:
+            vout_display_SendEventKey(vd, KEY_BACKSPACE);
+            break;
+        case AA_ESC:
+            vout_display_SendEventKey(vd, KEY_ESC);
+            break;
+        default:
+            if (event >= 0x20 && event <= 0x7f)
+                vout_display_SendEventKey(vd, event);
+            break;
+        }
     }
 }