Another round of subtitle code cleanup
[mplayer/glamo.git] / libvo / vo_caca.c
blob86c46fa7e713e579a1df22319780c5d4030cfa8d
1 /*
2 * MPlayer
3 *
4 * Video driver for libcaca
5 *
6 * by Pigeon <pigeon@pigeond.net>
8 * Some functions/codes/ideas are from x11 and aalib vo
10 * TODO: support those draw_alpha stuff?
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <time.h>
20 #include <errno.h>
22 #include "config.h"
23 #include "video_out.h"
24 #include "video_out_internal.h"
25 #include "sub.h"
27 #include "osdep/keycodes.h"
28 #include "mp_msg.h"
30 #include <caca.h>
31 #ifdef CACA_API_VERSION_1
32 /* Include the pre-1.x compatibility header.
33 * Once libcaca 1.x is widespread, vo_caca should be fully
34 * converted to the new API. A patch exists:
35 * http://lists.mplayerhq.hu/pipermail/mplayer-dev-eng/2006-July/044674.html
37 #include <caca0.h>
38 #endif
40 static vo_info_t info = {
41 "libcaca",
42 "caca",
43 "Pigeon <pigeon@pigeond.net>",
47 LIBVO_EXTERN (caca)
49 /* caca stuff */
50 static struct caca_bitmap *cbitmap = NULL;
52 /* image infos */
53 static int image_format;
54 static int image_width;
55 static int image_height;
57 static int screen_w, screen_h;
59 /* We want 24bpp always for now */
60 static unsigned int bpp = 24;
61 static unsigned int depth = 3;
62 static unsigned int rmask = 0xff0000;
63 static unsigned int gmask = 0x00ff00;
64 static unsigned int bmask = 0x0000ff;
65 static unsigned int amask = 0;
67 #define MESSAGE_SIZE 512
68 #define MESSAGE_DURATION 5
70 static time_t stoposd = 0;
71 static int showosdmessage = 0;
72 static char osdmessagetext[MESSAGE_SIZE];
73 static char posbar[MESSAGE_SIZE];
75 static int osdx = 0, osdy = 0;
76 static int posbary = 2;
78 static void osdmessage(int duration, const char *fmt, ...)
81 * for outputting a centered string at the bottom
82 * of our window for a while
84 va_list ar;
85 char m[MESSAGE_SIZE];
87 va_start(ar, fmt);
88 vsprintf(m, fmt, ar);
89 va_end(ar);
90 strcpy(osdmessagetext, m);
92 showosdmessage = 1;
93 stoposd = time(NULL) + duration;
94 osdx = (screen_w - strlen (osdmessagetext)) / 2;
95 posbar[0] = '\0';
98 static void osdpercent(int duration, int min, int max, int val, const char *desc, const char *unit)
101 * prints a bar for setting values
103 float step;
104 int where, i;
106 step = (float)screen_w / (float)(max - min);
107 where = (val - min) * step;
108 osdmessage (duration, "%s: %i%s", desc, val, unit);
109 posbar[0] = '|';
110 posbar[screen_w - 1] = '|';
112 for (i = 0; i < screen_w; i++)
114 if (i == where)
115 posbar[i] = '#';
116 else
117 posbar[i] = '-';
120 if (where != 0)
121 posbar[0] = '|';
123 if (where != (screen_w - 1))
124 posbar[screen_w - 1] = '|';
126 posbar[screen_w] = '\0';
129 static int resize ()
131 screen_w = caca_get_width();
132 screen_h = caca_get_height();
134 if (cbitmap)
135 caca_free_bitmap(cbitmap);
137 cbitmap = caca_create_bitmap(bpp, image_width, image_height,
138 depth * image_width, rmask, gmask, bmask,
139 amask);
141 if (!cbitmap)
142 mp_msg(MSGT_VO, MSGL_FATAL, "vo_caca: caca_create_bitmap failed!\n");
144 return 0;
147 static int config(uint32_t width, uint32_t height, uint32_t d_width,
148 uint32_t d_height, uint32_t flags, char *title, uint32_t format)
150 image_height = height;
151 image_width = width;
152 image_format = format;
154 showosdmessage = 0;
155 posbar[0] = '\0';
157 return resize ();
160 static int draw_frame(uint8_t *src[])
162 caca_draw_bitmap(0, 0, screen_w, screen_h, cbitmap, src[0]);
163 return 0;
166 static int draw_slice(uint8_t *src[], int stride[], int w, int h, int x, int y)
168 return 0;
171 static void flip_page(void)
174 if (showosdmessage)
176 if (time(NULL) >= stoposd)
178 showosdmessage = 0;
179 if (posbar)
180 posbar[0] = '\0';
181 } else {
182 caca_putstr(osdx, osdy, osdmessagetext);
184 if (posbar)
185 caca_putstr(0, posbary, posbar);
189 caca_refresh();
192 static void check_events (void)
194 unsigned int cev;
196 if ((cev = caca_get_event(CACA_EVENT_ANY)))
198 if (cev & CACA_EVENT_RESIZE)
200 caca_refresh();
201 resize();
202 } else if (cev & CACA_EVENT_KEY_RELEASE)
204 int key = (cev & 0x00ffffff);
205 enum caca_feature cf;
207 switch (key) {
208 case 'd':
209 case 'D':
210 /* Toggle dithering method */
211 cf = 1 + caca_get_feature(CACA_DITHERING);
212 if (cf > CACA_DITHERING_MAX)
213 cf = CACA_DITHERING_MIN;
214 caca_set_feature(cf);
215 osdmessage(MESSAGE_DURATION, "Using %s", caca_get_feature_name(cf));
216 break;
218 case 'a':
219 case 'A':
220 /* Toggle antialiasing method */
221 cf = 1 + caca_get_feature(CACA_ANTIALIASING);
222 if (cf > CACA_ANTIALIASING_MAX)
223 cf = CACA_ANTIALIASING_MIN;
224 caca_set_feature(cf);
225 osdmessage(MESSAGE_DURATION, "Using %s", caca_get_feature_name(cf));
226 break;
228 case 'b':
229 case 'B':
230 /* Toggle background method */
231 cf = 1 + caca_get_feature(CACA_BACKGROUND);
232 if (cf > CACA_BACKGROUND_MAX)
233 cf = CACA_BACKGROUND_MIN;
234 caca_set_feature(cf);
235 osdmessage(MESSAGE_DURATION, "Using %s", caca_get_feature_name(cf));
236 break;
238 case CACA_KEY_UP:
239 mplayer_put_key(KEY_UP);
240 break;
241 case CACA_KEY_DOWN:
242 mplayer_put_key(KEY_DOWN);
243 break;
244 case CACA_KEY_LEFT:
245 mplayer_put_key(KEY_LEFT);
246 break;
247 case CACA_KEY_RIGHT:
248 mplayer_put_key(KEY_RIGHT);
249 break;
250 case CACA_KEY_ESCAPE:
251 mplayer_put_key(KEY_ESC);
252 break;
253 case CACA_KEY_PAGEUP:
254 mplayer_put_key(KEY_PAGE_UP);
255 break;
256 case CACA_KEY_PAGEDOWN:
257 mplayer_put_key(KEY_PAGE_DOWN);
258 break;
259 case CACA_KEY_RETURN:
260 mplayer_put_key(KEY_ENTER);
261 break;
262 case CACA_KEY_HOME:
263 mplayer_put_key(KEY_HOME);
264 break;
265 case CACA_KEY_END:
266 mplayer_put_key(KEY_END);
267 break;
268 default:
269 if (key <= 255)
270 mplayer_put_key (key);
271 break;
277 static void uninit(void)
279 caca_free_bitmap(cbitmap);
280 cbitmap = NULL;
281 caca_end();
285 static void draw_osd(void)
287 if (vo_osd_progbar_type != -1)
288 osdpercent(MESSAGE_DURATION, 0, 255,
289 vo_osd_progbar_value, __sub_osd_names[vo_osd_progbar_type],
290 "");
293 static int preinit(const char *arg)
295 if (arg)
297 mp_msg(MSGT_VO, MSGL_ERR, "vo_caca: Unknown subdevice: %s\n", arg);
298 return ENOSYS;
301 if (caca_init())
303 mp_msg(MSGT_VO, MSGL_ERR, "vo_caca: failed to initialize\n");
304 return ENOSYS;
307 caca_set_window_title("MPlayer");
309 /* Default libcaca features */
310 caca_set_feature(CACA_ANTIALIASING_PREFILTER);
311 caca_set_feature(CACA_DITHERING_RANDOM);
313 return 0;
316 static int query_format(uint32_t format)
318 if (format == IMGFMT_BGR24)
319 return VFCAP_OSD | VFCAP_CSP_SUPPORTED;
321 return 0;
324 static int control(uint32_t request, void *data, ...)
326 switch(request)
328 case VOCTRL_QUERY_FORMAT:
329 return query_format(*((uint32_t *)data));
330 default:
331 return VO_NOTIMPL;