summaryrefslogtreecommitdiffstats
path: root/video/out/vo_caca.c
blob: 2d998bf91d421fb11bba063cc2d147ab36090197 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * video output driver for libcaca
 *
 * by Pigeon <pigeon@pigeond.net>
 *
 * Some functions/codes/ideas are from x11 and aalib vo
 *
 * TODO: support draw_alpha?
 *
 * This file is part of MPlayer.
 *
 * MPlayer 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.
 *
 * MPlayer 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 MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <assert.h>
#include <caca.h>

#include "config.h"
#include "video_out.h"
#include "sub/sub.h"
#include "libmpcodecs/mp_image.h"
#include "libmpcodecs/vfcap.h"

#include "input/keycodes.h"
#include "input/input.h"
#include "mp_msg.h"
#include "mp_fifo.h"

/* caca stuff */
static caca_canvas_t  *canvas;
static caca_display_t *display;
static caca_dither_t  *dither           = NULL;
static const char     *dither_antialias = "default";
static const char     *dither_charset   = "default";
static const char     *dither_color     = "default";
static const char     *dither_algo      = "none";

/* image infos */
static int image_format;
static int image_width;
static int image_height;

static int screen_w, screen_h;

/* We want 24bpp always for now */
static unsigned int bpp   = 24;
static unsigned int depth = 3;
static unsigned int rmask = 0xff0000;
static unsigned int gmask = 0x00ff00;
static unsigned int bmask = 0x0000ff;
static unsigned int amask = 0;

#define MESSAGE_SIZE     512
#define MESSAGE_DURATION   5

static time_t stoposd     = 0;
static int showosdmessage = 0;
static char osdmessagetext[MESSAGE_SIZE];
static char posbar[MESSAGE_SIZE];

static int osdx = 0, osdy = 0;
static int posbary = 2;

static void osdmessage(int duration, const char *fmt, ...)
{
    /* for outputting a centered string at the window bottom for a while */
    va_list ar;
    char m[MESSAGE_SIZE];

    va_start(ar, fmt);
    vsprintf(m, fmt, ar);
    va_end(ar);
    strcpy(osdmessagetext, m);

    showosdmessage = 1;
    stoposd        = time(NULL) + duration;
    osdx           = (screen_w - strlen(osdmessagetext)) / 2;
    posbar[0]      = '\0';
}

static void osdpercent(int duration, int min, int max, int val,
                       const char *desc, const char *unit)
{
    /* prints a bar for setting values */
    float step;
    int where, i;

    step  = (float)screen_w / (float)(max - min);
    where = (val - min) * step;
    osdmessage(duration, "%s: %i%s", desc, val, unit);
    posbar[0]            = '|';
    posbar[screen_w - 1] = '|';

    for (i = 0; i < screen_w; i++) {
        if (i == where)
            posbar[i] = '#';
        else
            posbar[i] = '-';
    }

    if (where != 0)
        posbar[0] = '|';

    if (where != (screen_w - 1))
        posbar[screen_w - 1] = '|';

    posbar[screen_w] = '\0';
}

static int resize(void)
{
    screen_w = caca_get_canvas_width(canvas);
    screen_h = caca_get_canvas_height(canvas);

    caca_free_dither(dither);

    dither = caca_create_dither(bpp, image_width, image_height,
                                depth * image_width,
                                rmask, gmask, bmask, amask);
    if (dither == NULL) {
        mp_msg(MSGT_VO, MSGL_FATAL, "vo_caca: caca_create_dither failed!\n");
        return ENOSYS;
    }

    /* Default libcaca features */
    caca_set_dither_antialias(dither, dither_antialias);
    caca_set_dither_charset(dither, dither_charset);
    caca_set_dither_color(dither, dither_color);
    caca_set_dither_algorithm(dither, dither_algo);

    return 0;
}

static int config(struct vo *vo, uint32_t width, uint32_t height,
                  uint32_t d_width, uint32_t d_height, uint32_t flags,
                  uint32_t format)
{
    image_height = height;
    image_width  = width;
    image_format = format;

    showosdmessage = 0;
    posbar[0]      = '\0';

    return resize();
}

static uint32_t draw_image(struct vo *vo, mp_image_t *mpi)
{
    assert(mpi->stride[0] == image_width * 3);
    caca_dither_bitmap(canvas, 0, 0, screen_w, screen_h, dither,
                       mpi->planes[0]);
    return true;
}

static int draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h,
                      int x, int y)
{
    return 0;
}

static void flip_page(struct vo *vo)
{
    if (showosdmessage) {
        if (time(NULL) >= stoposd) {
            showosdmessage = 0;
            if (*posbar)
                posbar[0] = '\0';
        } else {
            caca_put_str(canvas, osdx, osdy, osdmessagetext);
            if (*posbar)
                caca_put_str(canvas, 0, posbary, posbar);
        }
    }

    caca_refresh_display(display);
}

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;
        }
    }

    *str = list[0];
    *msg = list[1];
}

static const struct mp_keymap keysym_map[] = {
    {CACA_KEY_RETURN, KEY_ENTER}, {CACA_KEY_ESCAPE, KEY_ESC},
    {CACA_KEY_UP, KEY_DOWN}, {CACA_KEY_DOWN, KEY_DOWN},
    {CACA_KEY_LEFT, KEY_LEFT}, {CACA_KEY_RIGHT, KEY_RIGHT},
    {CACA_KEY_PAGEUP, KEY_PAGE_UP}, {CACA_KEY_PAGEDOWN, KEY_PAGE_DOWN},
    {CACA_KEY_HOME, KEY_HOME}, {CACA_KEY_END, KEY_END},
    {CACA_KEY_INSERT, KEY_INSERT}, {CACA_KEY_DELETE, KEY_DELETE},
    {CACA_KEY_BACKSPACE, KEY_BACKSPACE}, {CACA_KEY_TAB, KEY_TAB},
    {CACA_KEY_PAUSE, KEY_PAUSE},
    {CACA_KEY_F1, KEY_F+1}, {CACA_KEY_F2, KEY_F+2},
    {CACA_KEY_F3, KEY_F+3}, {CACA_KEY_F4, KEY_F+4},
    {CACA_KEY_F5, KEY_F+5}, {CACA_KEY_F6, KEY_F+6},
    {CACA_KEY_F7, KEY_F+7}, {CACA_KEY_F8, KEY_F+8},
    {CACA_KEY_F9, KEY_F+9}, {CACA_KEY_F10, KEY_F+10},
    {CACA_KEY_F11, KEY_F+11}, {CACA_KEY_F12, KEY_F+12},
    {CACA_KEY_F13, KEY_F+13}, {CACA_KEY_F14, KEY_F+14},
    {CACA_KEY_F15, KEY_F+15},
    {0, 0}
};

static void check_events(struct vo *vo)
{
    caca_event_t cev;
    while (caca_get_event(display, CACA_EVENT_ANY, &cev, 0)) {

        switch (cev.type) {
        case CACA_EVENT_RESIZE:
            caca_refresh_display(display);
            resize();
            break;
        case CACA_EVENT_QUIT:
            mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
            break;
        case CACA_EVENT_MOUSE_MOTION:
            vo_mouse_movement(vo, cev.data.mouse.x, cev.data.mouse.y);
            break;
        case CACA_EVENT_MOUSE_PRESS:
            if (!vo_nomouse_input)
                mplayer_put_key(vo->key_fifo,
                        (MOUSE_BTN0 + cev.data.mouse.button - 1) | MP_KEY_DOWN);
            break;
        case CACA_EVENT_MOUSE_RELEASE:
            if (!vo_nomouse_input)
                mplayer_put_key(vo->key_fifo,
                                MOUSE_BTN0 + cev.data.mouse.button - 1);
            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)
                mplayer_put_key(vo->key_fifo, mpkey);
            else
            switch (key) {
            case 'd':
            case 'D':
                /* Toggle dithering algorithm */
                set_next_str(caca_get_dither_algorithm_list(dither),
                             &dither_algo, &msg_name);
                caca_set_dither_algorithm(dither, dither_algo);
                osdmessage(MESSAGE_DURATION, "Using %s", msg_name);
                break;

            case 'a':
            case 'A':
                /* Toggle antialiasing method */
                set_next_str(caca_get_dither_antialias_list(dither),
                             &dither_antialias, &msg_name);
                caca_set_dither_antialias(dither, dither_antialias);
                osdmessage(MESSAGE_DURATION, "Using %s", msg_name);
                break;

            case 'h':
            case 'H':
                /* Toggle charset method */
                set_next_str(caca_get_dither_charset_list(dither),
                             &dither_charset, &msg_name);
                caca_set_dither_charset(dither, dither_charset);
                osdmessage(MESSAGE_DURATION, "Using %s", msg_name);
                break;

            case 'c':
            case 'C':
                /* Toggle color method */
                set_next_str(caca_get_dither_color_list(dither),
                             &dither_color, &msg_name);
                caca_set_dither_color(dither, dither_color);
                osdmessage(MESSAGE_DURATION, "Using %s", msg_name);
                break;

            default:
                if (key <= 255)
                    mplayer_put_key(vo->key_fifo, key);
                break;
            }
        }
        }
    }
}

static void uninit(struct vo *vo)
{
    caca_free_dither(dither);
    dither = NULL;
    caca_free_display(display);
    caca_free_canvas(canvas);
}

static void draw_osd(struct vo *vo, struct osd_state *osd)
{
    if (osd->progbar_type != -1)
        osdpercent(MESSAGE_DURATION, 0, 255, osd->progbar_value,
                   sub_osd_names[osd->progbar_type], "");
}

static int preinit(struct vo *vo, const char *arg)
{
    if (arg) {
        mp_msg(MSGT_VO, MSGL_ERR, "vo_caca: Unknown subdevice: %s\n", arg);
        return ENOSYS;
    }

    canvas = caca_create_canvas(0, 0);
    if (canvas == NULL) {
        mp_msg(MSGT_VO, MSGL_ERR, "vo_caca: failed to create canvas\n");
        return ENOSYS;
    }

    display = caca_create_display(canvas);

    if (display == NULL) {
        mp_msg(MSGT_VO, MSGL_ERR, "vo_caca: failed to create display\n");
        caca_free_canvas(canvas);
        return ENOSYS;
    }

    caca_set_display_title(display, "mpv");

    return 0;
}

static int query_format(uint32_t format)
{
    if (format == IMGFMT_BGR24)
        return VFCAP_OSD | VFCAP_CSP_SUPPORTED;

    return 0;
}

static int control(struct vo *vo, uint32_t request, void *data)
{
    switch (request) {
    case VOCTRL_QUERY_FORMAT:
        return query_format(*((uint32_t *)data));
    case VOCTRL_DRAW_IMAGE:
        return draw_image(vo, data);
    default:
        return VO_NOTIMPL;
    }
}

const struct vo_driver video_out_caca = {
    .is_new = false,
    .info = &(const vo_info_t) {
        "libcaca",
        "caca",
        "Pigeon <pigeon@pigeond.net>",
        ""
    },
    .preinit = preinit,
    .config = config,
    .control = control,
    .draw_slice = draw_slice,
    .draw_osd = draw_osd,
    .flip_page = flip_page,
    .check_events = check_events,
    .uninit = uninit,
};