1 /*
2  * video output driver for libcaca
3  *
4  * by Pigeon <pigeon@pigeond.net>
5  *
6  * Some functions/codes/ideas are from x11 and aalib vo
7  *
8  * TODO: support draw_alpha?
9  *
10  * This file is part of MPlayer.
11  *
12  * MPlayer is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * MPlayer is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with MPlayer; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <time.h>
33 #include <errno.h>
34 #include <caca.h>
35 
36 #include "config.h"
37 #include "video_out.h"
38 #define NO_DRAW_SLICE
39 #include "video_out_internal.h"
40 #include "sub/sub.h"
41 
42 #include "osdep/keycodes.h"
43 #include "input/input.h"
44 #include "input/mouse.h"
45 #include "mp_msg.h"
46 #include "mp_fifo.h"
47 
48 static const vo_info_t info = {
49     "libcaca",
50     "caca",
51     "Pigeon <pigeon@pigeond.net>",
52     ""
53 };
54 
55 const LIBVO_EXTERN(caca)
56 
57 /* caca stuff */
58 static caca_canvas_t  *canvas;
59 static caca_display_t *display;
60 static caca_dither_t  *dither           = NULL;
61 static const char     *dither_antialias = "default";
62 static const char     *dither_charset   = "default";
63 static const char     *dither_color     = "default";
64 static const char     *dither_algo      = "none";
65 
66 /* image infos */
67 static int image_format;
68 static int image_width;
69 static int image_height;
70 
71 static int screen_w, screen_h;
72 
73 /* We want 24bpp always for now */
74 static unsigned int bpp   = 24;
75 static unsigned int depth = 3;
76 static unsigned int rmask = 0xff0000;
77 static unsigned int gmask = 0x00ff00;
78 static unsigned int bmask = 0x0000ff;
79 static unsigned int amask = 0;
80 
81 #define MESSAGE_SIZE     512
82 #define MESSAGE_DURATION   5
83 
84 static time_t stoposd     = 0;
85 static int showosdmessage = 0;
86 static char osdmessagetext[MESSAGE_SIZE];
87 static char posbar[MESSAGE_SIZE];
88 
89 static int osdx = 0, osdy = 0;
90 static int posbary = 2;
91 
osdmessage(int duration,const char * fmt,...)92 static void osdmessage(int duration, const char *fmt, ...)
93 {
94     /* for outputting a centered string at the window bottom for a while */
95     va_list ar;
96     char m[MESSAGE_SIZE];
97 
98     va_start(ar, fmt);
99     vsprintf(m, fmt, ar);
100     va_end(ar);
101     strcpy(osdmessagetext, m);
102 
103     showosdmessage = 1;
104     stoposd        = time(NULL) + duration;
105     osdx           = (screen_w - strlen(osdmessagetext)) / 2;
106     posbar[0]      = '\0';
107 }
108 
osdpercent(int duration,int min,int max,int val,const char * desc,const char * unit)109 static void osdpercent(int duration, int min, int max, int val,
110                        const char *desc, const char *unit)
111 {
112     /* prints a bar for setting values */
113     float step;
114     int where, i;
115 
116     step  = (float)screen_w / (float)(max - min);
117     where = (val - min) * step;
118     osdmessage(duration, "%s: %i%s", desc, val, unit);
119     posbar[0]            = '|';
120     posbar[screen_w - 1] = '|';
121 
122     for (i = 0; i < screen_w; i++) {
123         if (i == where)
124             posbar[i] = '#';
125         else
126             posbar[i] = '-';
127     }
128 
129     if (where != 0)
130         posbar[0] = '|';
131 
132     if (where != (screen_w - 1))
133         posbar[screen_w - 1] = '|';
134 
135     posbar[screen_w] = '\0';
136 }
137 
resize(void)138 static int resize(void)
139 {
140     screen_w = caca_get_canvas_width(canvas);
141     screen_h = caca_get_canvas_height(canvas);
142 
143     caca_free_dither(dither);
144 
145     dither = caca_create_dither(bpp, image_width, image_height,
146                                 depth * image_width,
147                                 rmask, gmask, bmask, amask);
148     if (dither == NULL) {
149         mp_msg(MSGT_VO, MSGL_FATAL, "vo_caca: caca_create_dither failed!\n");
150         return ENOSYS;
151     }
152 
153     /* Default libcaca features */
154     caca_set_dither_antialias(dither, dither_antialias);
155     caca_set_dither_charset(dither, dither_charset);
156     caca_set_dither_color(dither, dither_color);
157     caca_set_dither_algorithm(dither, dither_algo);
158 
159     return 0;
160 }
161 
config(uint32_t width,uint32_t height,uint32_t d_width,uint32_t d_height,uint32_t flags,char * title,uint32_t format)162 static int config(uint32_t width, uint32_t height, uint32_t d_width,
163                   uint32_t d_height, uint32_t flags, char *title,
164                   uint32_t format)
165 {
166     image_height = height;
167     image_width  = width;
168     image_format = format;
169 
170     showosdmessage = 0;
171     posbar[0]      = '\0';
172 
173     return resize();
174 }
175 
draw_frame(uint8_t * src[])176 static int draw_frame(uint8_t *src[])
177 {
178     caca_dither_bitmap(canvas, 0, 0, screen_w, screen_h, dither, src[0]);
179     return 0;
180 }
181 
flip_page(void)182 static void flip_page(void)
183 {
184     if (showosdmessage) {
185         if (time(NULL) >= stoposd) {
186             showosdmessage = 0;
187             if (*posbar)
188                 posbar[0] = '\0';
189         } else {
190             caca_put_str(canvas, osdx, osdy, osdmessagetext);
191             if (*posbar)
192                 caca_put_str(canvas, 0, posbary, posbar);
193         }
194     }
195 
196     caca_refresh_display(display);
197 }
198 
set_next_str(const char * const * list,const char ** str,const char ** msg)199 static void set_next_str(const char * const *list, const char **str,
200                          const char **msg)
201 {
202     int ind;
203     for (ind = 0; list[ind]; ind += 2) {
204         if (strcmp(list[ind], *str) == 0) {
205             if (list[ind + 2] == NULL)
206                 ind = -2;
207             *str = list[ind + 2];
208             *msg = list[ind + 3];
209             return;
210         }
211     }
212 
213     *str = list[0];
214     *msg = list[1];
215 }
216 
217 static const struct mp_keymap keysym_map[] = {
218     {CACA_KEY_RETURN, KEY_ENTER}, {CACA_KEY_ESCAPE, KEY_ESC},
219     {CACA_KEY_UP, KEY_UP}, {CACA_KEY_DOWN, KEY_DOWN},
220     {CACA_KEY_LEFT, KEY_LEFT}, {CACA_KEY_RIGHT, KEY_RIGHT},
221     {CACA_KEY_PAGEUP, KEY_PAGE_UP}, {CACA_KEY_PAGEDOWN, KEY_PAGE_DOWN},
222     {CACA_KEY_HOME, KEY_HOME}, {CACA_KEY_END, KEY_END},
223     {CACA_KEY_INSERT, KEY_INSERT}, {CACA_KEY_DELETE, KEY_DELETE},
224     {CACA_KEY_BACKSPACE, KEY_BACKSPACE}, {CACA_KEY_TAB, KEY_TAB},
225     {CACA_KEY_PAUSE, KEY_PAUSE},
226     {CACA_KEY_F1, KEY_F+1}, {CACA_KEY_F2, KEY_F+2},
227     {CACA_KEY_F3, KEY_F+3}, {CACA_KEY_F4, KEY_F+4},
228     {CACA_KEY_F5, KEY_F+5}, {CACA_KEY_F6, KEY_F+6},
229     {CACA_KEY_F7, KEY_F+7}, {CACA_KEY_F8, KEY_F+8},
230     {CACA_KEY_F9, KEY_F+9}, {CACA_KEY_F10, KEY_F+10},
231     {CACA_KEY_F11, KEY_F+11}, {CACA_KEY_F12, KEY_F+12},
232     {CACA_KEY_F13, KEY_F+13}, {CACA_KEY_F14, KEY_F+14},
233     {CACA_KEY_F15, KEY_F+15},
234     {0, 0}
235 };
236 
check_events(void)237 static void check_events(void)
238 {
239     caca_event_t cev;
240     while (caca_get_event(display, CACA_EVENT_ANY, &cev, 0)) {
241 
242         switch (cev.type) {
243         case CACA_EVENT_RESIZE:
244             caca_refresh_display(display);
245             resize();
246             break;
247         case CACA_EVENT_QUIT:
248             mplayer_put_key(KEY_CLOSE_WIN);
249             break;
250         case CACA_EVENT_MOUSE_MOTION:
251             vo_mouse_movement(cev.data.mouse.x, cev.data.mouse.y);
252             break;
253         case CACA_EVENT_MOUSE_PRESS:
254             if (!vo_nomouse_input)
255                 mplayer_put_key((MOUSE_BTN0 + cev.data.mouse.button - 1) | MP_KEY_DOWN);
256             break;
257         case CACA_EVENT_MOUSE_RELEASE:
258             if (!vo_nomouse_input)
259                 mplayer_put_key(MOUSE_BTN0 + cev.data.mouse.button - 1);
260             break;
261         case CACA_EVENT_KEY_PRESS:
262         {
263             int key = cev.data.key.ch;
264             int mpkey = lookup_keymap_table(keysym_map, key);
265             const char *msg_name;
266 
267             if (mpkey)
268                 mplayer_put_key(mpkey);
269             else
270             switch (key) {
271             case 'd':
272             case 'D':
273                 /* Toggle dithering algorithm */
274                 set_next_str(caca_get_dither_algorithm_list(dither), &dither_algo, &msg_name);
275                 caca_set_dither_algorithm(dither, dither_algo);
276                 osdmessage(MESSAGE_DURATION, "Using %s", msg_name);
277                 break;
278 
279             case 'a':
280             case 'A':
281                 /* Toggle antialiasing method */
282                 set_next_str(caca_get_dither_antialias_list(dither), &dither_antialias, &msg_name);
283                 caca_set_dither_antialias(dither, dither_antialias);
284                 osdmessage(MESSAGE_DURATION, "Using %s", msg_name);
285                 break;
286 
287             case 'h':
288             case 'H':
289                 /* Toggle charset method */
290                 set_next_str(caca_get_dither_charset_list(dither), &dither_charset, &msg_name);
291                 caca_set_dither_charset(dither, dither_charset);
292                 osdmessage(MESSAGE_DURATION, "Using %s", msg_name);
293                 break;
294 
295             case 'c':
296             case 'C':
297                 /* Toggle color method */
298                 set_next_str(caca_get_dither_color_list(dither), &dither_color, &msg_name);
299                 caca_set_dither_color(dither, dither_color);
300                 osdmessage(MESSAGE_DURATION, "Using %s", msg_name);
301                 break;
302 
303             default:
304                 if (key <= 255)
305                     mplayer_put_key(key);
306                 break;
307             }
308         }
309         }
310     }
311 }
312 
uninit(void)313 static void uninit(void)
314 {
315     caca_free_dither(dither);
316     dither = NULL;
317     caca_free_display(display);
318     caca_free_canvas(canvas);
319 }
320 
321 
draw_osd(void)322 static void draw_osd(void)
323 {
324     if (vo_osd_progbar_type != -1)
325         osdpercent(MESSAGE_DURATION, 0, 255, vo_osd_progbar_value,
326                    sub_osd_names[vo_osd_progbar_type], "");
327 }
328 
preinit(const char * arg)329 static int preinit(const char *arg)
330 {
331     if (arg) {
332         mp_msg(MSGT_VO, MSGL_ERR, "vo_caca: Unknown subdevice: %s\n", arg);
333         return ENOSYS;
334     }
335 
336     canvas = caca_create_canvas(0, 0);
337     if (canvas == NULL) {
338         mp_msg(MSGT_VO, MSGL_ERR, "vo_caca: failed to create canvas\n");
339         return ENOSYS;
340     }
341 
342     display = caca_create_display(canvas);
343 
344     if (display == NULL) {
345         mp_msg(MSGT_VO, MSGL_ERR, "vo_caca: failed to create display\n");
346         caca_free_canvas(canvas);
347         return ENOSYS;
348     }
349 
350     caca_set_display_title(display, "MPlayer");
351 
352     return 0;
353 }
354 
query_format(uint32_t format)355 static int query_format(uint32_t format)
356 {
357     if (format == IMGFMT_BGR24)
358         return VFCAP_OSD | VFCAP_CSP_SUPPORTED;
359 
360     return 0;
361 }
362 
control(uint32_t request,void * data)363 static int control(uint32_t request, void *data)
364 {
365     switch (request) {
366     case VOCTRL_QUERY_FORMAT:
367         return query_format(*((uint32_t *)data));
368     default:
369         return VO_NOTIMPL;
370     }
371 }
372