2004-04-06 00:04:48 +00:00
|
|
|
/*
|
2009-02-08 03:27:30 +00:00
|
|
|
* video output driver for libcaca
|
|
|
|
*
|
2004-04-06 00:04:48 +00:00
|
|
|
* by Pigeon <pigeon@pigeond.net>
|
|
|
|
*
|
|
|
|
* Some functions/codes/ideas are from x11 and aalib vo
|
|
|
|
*
|
2009-02-08 03:27:30 +00:00
|
|
|
* TODO: support draw_alpha?
|
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
2009-02-08 03:27:30 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
2009-02-08 03:27:30 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2009-02-08 03:27:30 +00:00
|
|
|
* 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
|
2015-04-13 07:36:54 +00:00
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2004-04-06 00:04:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
2012-08-06 21:55:47 +00:00
|
|
|
#include <assert.h>
|
2011-11-09 02:26:58 +00:00
|
|
|
#include <caca.h>
|
2004-04-06 00:04:48 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "vo.h"
|
|
|
|
#include "video/mp_image.h"
|
2004-04-06 00:04:48 +00:00
|
|
|
|
2013-12-17 00:23:09 +00:00
|
|
|
#include "input/keycodes.h"
|
|
|
|
#include "input/input.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2013-12-17 00:23:09 +00:00
|
|
|
#include "input/input.h"
|
2004-04-06 00:04:48 +00:00
|
|
|
|
2014-06-10 21:27:03 +00:00
|
|
|
struct priv {
|
|
|
|
caca_canvas_t *canvas;
|
|
|
|
caca_display_t *display;
|
|
|
|
caca_dither_t *dither;
|
|
|
|
uint8_t *dither_buffer;
|
|
|
|
const char *dither_antialias;
|
|
|
|
const char *dither_charset;
|
|
|
|
const char *dither_color;
|
|
|
|
const char *dither_algo;
|
|
|
|
|
|
|
|
/* image infos */
|
|
|
|
int image_format;
|
|
|
|
int image_width;
|
|
|
|
int image_height;
|
|
|
|
|
|
|
|
int screen_w, screen_h;
|
|
|
|
};
|
2004-04-06 00:04:48 +00:00
|
|
|
|
|
|
|
/* We want 24bpp always for now */
|
2014-06-10 21:27:03 +00:00
|
|
|
static const unsigned int bpp = 24;
|
|
|
|
static const unsigned int depth = 3;
|
|
|
|
static const unsigned int rmask = 0xff0000;
|
|
|
|
static const unsigned int gmask = 0x00ff00;
|
|
|
|
static const unsigned int bmask = 0x0000ff;
|
|
|
|
static const unsigned int amask = 0;
|
2004-04-06 00:04:48 +00:00
|
|
|
|
2013-08-23 21:30:09 +00:00
|
|
|
static int resize(struct vo *vo)
|
2004-04-06 00:04:48 +00:00
|
|
|
{
|
2014-06-10 21:27:03 +00:00
|
|
|
struct priv *priv = vo->priv;
|
|
|
|
priv->screen_w = caca_get_canvas_width(priv->canvas);
|
|
|
|
priv->screen_h = caca_get_canvas_height(priv->canvas);
|
2004-04-06 00:04:48 +00:00
|
|
|
|
2014-06-10 21:27:03 +00:00
|
|
|
caca_free_dither(priv->dither);
|
|
|
|
talloc_free(priv->dither_buffer);
|
2004-04-06 00:04:48 +00:00
|
|
|
|
2014-06-10 21:27:03 +00:00
|
|
|
priv->dither = caca_create_dither(bpp, priv->image_width, priv->image_height,
|
|
|
|
depth * priv->image_width,
|
2011-11-09 02:26:58 +00:00
|
|
|
rmask, gmask, bmask, amask);
|
2014-06-10 21:27:03 +00:00
|
|
|
if (priv->dither == NULL) {
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_FATAL(vo, "caca_create_dither failed!\n");
|
2014-01-24 20:22:25 +00:00
|
|
|
return -1;
|
2011-11-09 02:26:58 +00:00
|
|
|
}
|
2014-06-10 21:27:03 +00:00
|
|
|
priv->dither_buffer =
|
|
|
|
talloc_array(NULL, uint8_t, depth * priv->image_width * priv->image_height);
|
2004-04-06 00:04:48 +00:00
|
|
|
|
2011-11-09 02:26:58 +00:00
|
|
|
/* Default libcaca features */
|
2014-06-10 21:27:03 +00:00
|
|
|
caca_set_dither_antialias(priv->dither, priv->dither_antialias);
|
|
|
|
caca_set_dither_charset(priv->dither, priv->dither_charset);
|
|
|
|
caca_set_dither_color(priv->dither, priv->dither_color);
|
|
|
|
caca_set_dither_algorithm(priv->dither, priv->dither_algo);
|
2004-04-06 00:04:48 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-24 20:22:25 +00:00
|
|
|
static int reconfig(struct vo *vo, struct mp_image_params *params, int flags)
|
2004-04-06 00:04:48 +00:00
|
|
|
{
|
2014-06-10 21:27:03 +00:00
|
|
|
struct priv *priv = vo->priv;
|
|
|
|
priv->image_height = params->h;
|
|
|
|
priv->image_width = params->w;
|
|
|
|
priv->image_format = params->imgfmt;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2013-08-23 21:30:09 +00:00
|
|
|
return resize(vo);
|
2004-04-06 00:04:48 +00:00
|
|
|
}
|
|
|
|
|
2012-11-04 14:56:04 +00:00
|
|
|
static void draw_image(struct vo *vo, mp_image_t *mpi)
|
2004-04-06 00:04:48 +00:00
|
|
|
{
|
2014-06-10 21:27:03 +00:00
|
|
|
struct priv *priv = vo->priv;
|
|
|
|
memcpy_pic(priv->dither_buffer, mpi->planes[0], priv->image_width * depth, priv->image_height,
|
|
|
|
priv->image_width * depth, mpi->stride[0]);
|
|
|
|
caca_dither_bitmap(priv->canvas, 0, 0, priv->screen_w, priv->screen_h, priv->dither, priv->dither_buffer);
|
2014-06-17 21:05:50 +00:00
|
|
|
talloc_free(mpi);
|
2004-04-06 00:04:48 +00:00
|
|
|
}
|
|
|
|
|
2012-08-06 21:55:47 +00:00
|
|
|
static void flip_page(struct vo *vo)
|
2004-04-06 00:04:48 +00:00
|
|
|
{
|
2014-06-10 21:27:03 +00:00
|
|
|
struct priv *priv = vo->priv;
|
|
|
|
caca_refresh_display(priv->display);
|
2011-11-09 02:26:58 +00:00
|
|
|
}
|
2004-04-06 00:04:48 +00:00
|
|
|
|
2011-11-09 02:26:58 +00:00
|
|
|
static void set_next_str(const char * const *list, const char **str,
|
|
|
|
const char **msg)
|
|
|
|
{
|
|
|
|
int ind;
|
|
|
|
for (ind = 0; list[ind]; ind += 2) {
|
|
|
|
if (strcmp(list[ind], *str) == 0) {
|
|
|
|
if (list[ind + 2] == NULL)
|
|
|
|
ind = -2;
|
|
|
|
*str = list[ind + 2];
|
|
|
|
*msg = list[ind + 3];
|
|
|
|
return;
|
|
|
|
}
|
2004-04-06 00:04:48 +00:00
|
|
|
}
|
|
|
|
|
2011-11-09 02:26:58 +00:00
|
|
|
*str = list[0];
|
|
|
|
*msg = list[1];
|
2004-04-06 00:04:48 +00:00
|
|
|
}
|
|
|
|
|
2011-11-09 02:26:58 +00:00
|
|
|
static const struct mp_keymap keysym_map[] = {
|
2013-02-12 00:34:04 +00:00
|
|
|
{CACA_KEY_RETURN, MP_KEY_ENTER}, {CACA_KEY_ESCAPE, MP_KEY_ESC},
|
|
|
|
{CACA_KEY_UP, MP_KEY_UP}, {CACA_KEY_DOWN, MP_KEY_DOWN},
|
|
|
|
{CACA_KEY_LEFT, MP_KEY_LEFT}, {CACA_KEY_RIGHT, MP_KEY_RIGHT},
|
|
|
|
{CACA_KEY_PAGEUP, MP_KEY_PAGE_UP}, {CACA_KEY_PAGEDOWN, MP_KEY_PAGE_DOWN},
|
|
|
|
{CACA_KEY_HOME, MP_KEY_HOME}, {CACA_KEY_END, MP_KEY_END},
|
|
|
|
{CACA_KEY_INSERT, MP_KEY_INSERT}, {CACA_KEY_DELETE, MP_KEY_DELETE},
|
|
|
|
{CACA_KEY_BACKSPACE, MP_KEY_BACKSPACE}, {CACA_KEY_TAB, MP_KEY_TAB},
|
|
|
|
{CACA_KEY_PAUSE, MP_KEY_PAUSE},
|
|
|
|
{CACA_KEY_F1, MP_KEY_F+1}, {CACA_KEY_F2, MP_KEY_F+2},
|
|
|
|
{CACA_KEY_F3, MP_KEY_F+3}, {CACA_KEY_F4, MP_KEY_F+4},
|
|
|
|
{CACA_KEY_F5, MP_KEY_F+5}, {CACA_KEY_F6, MP_KEY_F+6},
|
|
|
|
{CACA_KEY_F7, MP_KEY_F+7}, {CACA_KEY_F8, MP_KEY_F+8},
|
|
|
|
{CACA_KEY_F9, MP_KEY_F+9}, {CACA_KEY_F10, MP_KEY_F+10},
|
|
|
|
{CACA_KEY_F11, MP_KEY_F+11}, {CACA_KEY_F12, MP_KEY_F+12},
|
|
|
|
{CACA_KEY_F13, MP_KEY_F+13}, {CACA_KEY_F14, MP_KEY_F+14},
|
|
|
|
{CACA_KEY_F15, MP_KEY_F+15},
|
2011-11-09 02:26:58 +00:00
|
|
|
{0, 0}
|
|
|
|
};
|
|
|
|
|
2012-08-06 21:55:47 +00:00
|
|
|
static void check_events(struct vo *vo)
|
2004-04-06 00:04:48 +00:00
|
|
|
{
|
2014-06-10 21:27:03 +00:00
|
|
|
struct priv *priv = vo->priv;
|
|
|
|
|
2011-11-09 02:26:58 +00:00
|
|
|
caca_event_t cev;
|
2014-06-10 21:27:03 +00:00
|
|
|
while (caca_get_event(priv->display, CACA_EVENT_ANY, &cev, 0)) {
|
2011-11-09 02:26:58 +00:00
|
|
|
|
|
|
|
switch (cev.type) {
|
|
|
|
case CACA_EVENT_RESIZE:
|
2014-06-10 21:27:03 +00:00
|
|
|
caca_refresh_display(priv->display);
|
2013-08-23 21:30:09 +00:00
|
|
|
resize(vo);
|
2011-11-09 02:26:58 +00:00
|
|
|
break;
|
|
|
|
case CACA_EVENT_QUIT:
|
2013-07-02 12:04:36 +00:00
|
|
|
mp_input_put_key(vo->input_ctx, MP_KEY_CLOSE_WIN);
|
2011-11-09 02:26:58 +00:00
|
|
|
break;
|
|
|
|
case CACA_EVENT_MOUSE_MOTION:
|
2014-07-27 19:53:29 +00:00
|
|
|
mp_input_set_mouse_pos(vo->input_ctx, cev.data.mouse.x, cev.data.mouse.y);
|
2011-11-09 02:26:58 +00:00
|
|
|
break;
|
|
|
|
case CACA_EVENT_MOUSE_PRESS:
|
2014-07-27 19:53:29 +00:00
|
|
|
mp_input_put_key(vo->input_ctx,
|
2013-02-12 00:34:04 +00:00
|
|
|
(MP_MOUSE_BTN0 + cev.data.mouse.button - 1) | MP_KEY_STATE_DOWN);
|
2011-11-09 02:26:58 +00:00
|
|
|
break;
|
|
|
|
case CACA_EVENT_MOUSE_RELEASE:
|
2014-07-27 19:53:29 +00:00
|
|
|
mp_input_put_key(vo->input_ctx,
|
2013-07-01 21:54:59 +00:00
|
|
|
(MP_MOUSE_BTN0 + cev.data.mouse.button - 1) | MP_KEY_STATE_UP);
|
2011-11-09 02:26:58 +00:00
|
|
|
break;
|
|
|
|
case CACA_EVENT_KEY_PRESS:
|
|
|
|
{
|
|
|
|
int key = cev.data.key.ch;
|
|
|
|
int mpkey = lookup_keymap_table(keysym_map, key);
|
|
|
|
const char *msg_name;
|
|
|
|
|
|
|
|
if (mpkey)
|
2013-07-02 12:04:36 +00:00
|
|
|
mp_input_put_key(vo->input_ctx, mpkey);
|
2011-11-09 02:26:58 +00:00
|
|
|
else
|
|
|
|
switch (key) {
|
|
|
|
case 'd':
|
|
|
|
case 'D':
|
|
|
|
/* Toggle dithering algorithm */
|
2014-06-10 21:27:03 +00:00
|
|
|
set_next_str(caca_get_dither_algorithm_list(priv->dither),
|
|
|
|
&priv->dither_algo, &msg_name);
|
|
|
|
caca_set_dither_algorithm(priv->dither, priv->dither_algo);
|
2011-11-09 02:26:58 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'a':
|
|
|
|
case 'A':
|
|
|
|
/* Toggle antialiasing method */
|
2014-06-10 21:27:03 +00:00
|
|
|
set_next_str(caca_get_dither_antialias_list(priv->dither),
|
|
|
|
&priv->dither_antialias, &msg_name);
|
|
|
|
caca_set_dither_antialias(priv->dither, priv->dither_antialias);
|
2011-11-09 02:26:58 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
case 'H':
|
|
|
|
/* Toggle charset method */
|
2014-06-10 21:27:03 +00:00
|
|
|
set_next_str(caca_get_dither_charset_list(priv->dither),
|
|
|
|
&priv->dither_charset, &msg_name);
|
|
|
|
caca_set_dither_charset(priv->dither, priv->dither_charset);
|
2011-11-09 02:26:58 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
case 'C':
|
|
|
|
/* Toggle color method */
|
2014-06-10 21:27:03 +00:00
|
|
|
set_next_str(caca_get_dither_color_list(priv->dither),
|
|
|
|
&priv->dither_color, &msg_name);
|
|
|
|
caca_set_dither_color(priv->dither, priv->dither_color);
|
2011-11-09 02:26:58 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (key <= 255)
|
2013-07-02 12:04:36 +00:00
|
|
|
mp_input_put_key(vo->input_ctx, key);
|
2011-11-09 02:26:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-04-06 00:04:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-06 21:55:47 +00:00
|
|
|
static void uninit(struct vo *vo)
|
2004-04-06 00:04:48 +00:00
|
|
|
{
|
2014-06-10 21:27:03 +00:00
|
|
|
struct priv *priv = vo->priv;
|
|
|
|
caca_free_dither(priv->dither);
|
|
|
|
priv->dither = NULL;
|
|
|
|
talloc_free(priv->dither_buffer);
|
|
|
|
priv->dither_buffer = NULL;
|
|
|
|
caca_free_display(priv->display);
|
|
|
|
caca_free_canvas(priv->canvas);
|
2004-04-06 00:04:48 +00:00
|
|
|
}
|
|
|
|
|
2013-07-22 20:52:42 +00:00
|
|
|
static int preinit(struct vo *vo)
|
2004-04-06 00:04:48 +00:00
|
|
|
{
|
2014-06-10 21:27:03 +00:00
|
|
|
struct priv *priv = vo->priv;
|
|
|
|
|
|
|
|
priv->dither_antialias = "default";
|
|
|
|
priv->dither_charset = "default";
|
|
|
|
priv->dither_color = "default";
|
|
|
|
priv->dither_algo = "none";
|
|
|
|
|
|
|
|
priv->canvas = caca_create_canvas(0, 0);
|
|
|
|
if (priv->canvas == NULL) {
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_ERR(vo, "failed to create canvas\n");
|
2011-11-09 02:26:58 +00:00
|
|
|
return ENOSYS;
|
2004-04-06 00:04:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-10 21:27:03 +00:00
|
|
|
priv->display = caca_create_display(priv->canvas);
|
2004-04-06 00:04:48 +00:00
|
|
|
|
2014-06-10 21:27:03 +00:00
|
|
|
if (priv->display == NULL) {
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_ERR(vo, "failed to create display\n");
|
2014-06-10 21:27:03 +00:00
|
|
|
caca_free_canvas(priv->canvas);
|
2011-11-09 02:26:58 +00:00
|
|
|
return ENOSYS;
|
|
|
|
}
|
|
|
|
|
2014-06-10 21:27:03 +00:00
|
|
|
caca_set_display_title(priv->display, "mpv");
|
2004-04-06 00:04:48 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-21 21:08:24 +00:00
|
|
|
static int query_format(struct vo *vo, int format)
|
2004-04-06 00:04:48 +00:00
|
|
|
{
|
2015-01-21 21:08:24 +00:00
|
|
|
return format == IMGFMT_BGR24;
|
2004-04-06 00:04:48 +00:00
|
|
|
}
|
|
|
|
|
2012-08-06 21:55:47 +00:00
|
|
|
static int control(struct vo *vo, uint32_t request, void *data)
|
2004-04-06 00:04:48 +00:00
|
|
|
{
|
2013-05-16 12:12:01 +00:00
|
|
|
switch (request) {
|
|
|
|
case VOCTRL_CHECK_EVENTS:
|
|
|
|
check_events(vo);
|
|
|
|
return VO_TRUE;
|
|
|
|
}
|
2012-11-04 15:24:18 +00:00
|
|
|
return VO_NOTIMPL;
|
2004-04-06 00:04:48 +00:00
|
|
|
}
|
2012-08-06 21:55:47 +00:00
|
|
|
|
|
|
|
const struct vo_driver video_out_caca = {
|
2013-10-23 17:06:14 +00:00
|
|
|
.name = "caca",
|
|
|
|
.description = "libcaca",
|
2012-08-06 21:55:47 +00:00
|
|
|
.preinit = preinit,
|
2012-11-04 15:24:18 +00:00
|
|
|
.query_format = query_format,
|
2014-01-24 20:22:25 +00:00
|
|
|
.reconfig = reconfig,
|
2012-08-06 21:55:47 +00:00
|
|
|
.control = control,
|
2012-11-04 14:56:04 +00:00
|
|
|
.draw_image = draw_image,
|
2012-08-06 21:55:47 +00:00
|
|
|
.flip_page = flip_page,
|
|
|
|
.uninit = uninit,
|
2014-06-10 21:27:03 +00:00
|
|
|
.priv_size = sizeof(struct priv),
|
2012-08-06 21:55:47 +00:00
|
|
|
};
|