Add explanatory comments to the #endif part of multiple inclusion guards.
[mplayer/greg.git] / libvo / vo_caca.c
blob3fd1fcd1cd4ca97309b6b52052cf89705ab7eded
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"
29 #include "mp_fifo.h"
31 #include <caca.h>
32 #ifdef CACA_API_VERSION_1
33 /* Include the pre-1.x compatibility header.
34 * Once libcaca 1.x is widespread, vo_caca should be fully
35 * converted to the new API. A patch exists:
36 * http://lists.mplayerhq.hu/pipermail/mplayer-dev-eng/2006-July/044674.html
38 #include <caca0.h>
39 #endif
41 static const vo_info_t info = {
42 "libcaca",
43 "caca",
44 "Pigeon <pigeon@pigeond.net>",
48 const LIBVO_EXTERN (caca)
50 /* caca stuff */
51 static struct caca_bitmap *cbitmap = NULL;
53 /* image infos */
54 static int image_format;
55 static int image_width;
56 static int image_height;
58 static int screen_w, screen_h;
60 /* We want 24bpp always for now */
61 static unsigned int bpp = 24;
62 static unsigned int depth = 3;
63 static unsigned int rmask = 0xff0000;
64 static unsigned int gmask = 0x00ff00;
65 static unsigned int bmask = 0x0000ff;
66 static unsigned int amask = 0;
68 #define MESSAGE_SIZE 512
69 #define MESSAGE_DURATION 5
71 static time_t stoposd = 0;
72 static int showosdmessage = 0;
73 static char osdmessagetext[MESSAGE_SIZE];
74 static char posbar[MESSAGE_SIZE];
76 static int osdx = 0, osdy = 0;
77 static int posbary = 2;
79 static void osdmessage(int duration, const char *fmt, ...)
82 * for outputting a centered string at the bottom
83 * of our window for a while
85 va_list ar;
86 char m[MESSAGE_SIZE];
88 va_start(ar, fmt);
89 vsprintf(m, fmt, ar);
90 va_end(ar);
91 strcpy(osdmessagetext, m);
93 showosdmessage = 1;
94 stoposd = time(NULL) + duration;
95 osdx = (screen_w - strlen (osdmessagetext)) / 2;
96 posbar[0] = '\0';
99 static void osdpercent(int duration, int min, int max, int val, const char *desc, const char *unit)
102 * prints a bar for setting values
104 float step;
105 int where, i;
107 step = (float)screen_w / (float)(max - min);
108 where = (val - min) * step;
109 osdmessage (duration, "%s: %i%s", desc, val, unit);
110 posbar[0] = '|';
111 posbar[screen_w - 1] = '|';
113 for (i = 0; i < screen_w; i++)
115 if (i == where)
116 posbar[i] = '#';
117 else
118 posbar[i] = '-';
121 if (where != 0)
122 posbar[0] = '|';
124 if (where != (screen_w - 1))
125 posbar[screen_w - 1] = '|';
127 posbar[screen_w] = '\0';
130 static int resize ()
132 screen_w = caca_get_width();
133 screen_h = caca_get_height();
135 if (cbitmap)
136 caca_free_bitmap(cbitmap);
138 cbitmap = caca_create_bitmap(bpp, image_width, image_height,
139 depth * image_width, rmask, gmask, bmask,
140 amask);
142 if (!cbitmap)
143 mp_msg(MSGT_VO, MSGL_FATAL, "vo_caca: caca_create_bitmap failed!\n");
145 return 0;
148 static int config(uint32_t width, uint32_t height, uint32_t d_width,
149 uint32_t d_height, uint32_t flags, char *title, uint32_t format)
151 image_height = height;
152 image_width = width;
153 image_format = format;
155 showosdmessage = 0;
156 posbar[0] = '\0';
158 return resize ();
161 static int draw_frame(uint8_t *src[])
163 caca_draw_bitmap(0, 0, screen_w, screen_h, cbitmap, src[0]);
164 return 0;
167 static int draw_slice(uint8_t *src[], int stride[], int w, int h, int x, int y)
169 return 0;
172 static void flip_page(void)
175 if (showosdmessage)
177 if (time(NULL) >= stoposd)
179 showosdmessage = 0;
180 if (*posbar)
181 posbar[0] = '\0';
182 } else {
183 caca_putstr(osdx, osdy, osdmessagetext);
185 if (*posbar)
186 caca_putstr(0, posbary, posbar);
190 caca_refresh();
193 static void check_events (void)
195 unsigned int cev;
197 if ((cev = caca_get_event(CACA_EVENT_ANY)))
199 if (cev & CACA_EVENT_RESIZE)
201 caca_refresh();
202 resize();
203 } else if (cev & CACA_EVENT_KEY_RELEASE)
205 int key = (cev & 0x00ffffff);
206 enum caca_feature cf;
208 switch (key) {
209 case 'd':
210 case 'D':
211 /* Toggle dithering method */
212 cf = 1 + caca_get_feature(CACA_DITHERING);
213 if (cf > CACA_DITHERING_MAX)
214 cf = CACA_DITHERING_MIN;
215 caca_set_feature(cf);
216 osdmessage(MESSAGE_DURATION, "Using %s", caca_get_feature_name(cf));
217 break;
219 case 'a':
220 case 'A':
221 /* Toggle antialiasing method */
222 cf = 1 + caca_get_feature(CACA_ANTIALIASING);
223 if (cf > CACA_ANTIALIASING_MAX)
224 cf = CACA_ANTIALIASING_MIN;
225 caca_set_feature(cf);
226 osdmessage(MESSAGE_DURATION, "Using %s", caca_get_feature_name(cf));
227 break;
229 case 'b':
230 case 'B':
231 /* Toggle background method */
232 cf = 1 + caca_get_feature(CACA_BACKGROUND);
233 if (cf > CACA_BACKGROUND_MAX)
234 cf = CACA_BACKGROUND_MIN;
235 caca_set_feature(cf);
236 osdmessage(MESSAGE_DURATION, "Using %s", caca_get_feature_name(cf));
237 break;
239 case CACA_KEY_UP:
240 mplayer_put_key(KEY_UP);
241 break;
242 case CACA_KEY_DOWN:
243 mplayer_put_key(KEY_DOWN);
244 break;
245 case CACA_KEY_LEFT:
246 mplayer_put_key(KEY_LEFT);
247 break;
248 case CACA_KEY_RIGHT:
249 mplayer_put_key(KEY_RIGHT);
250 break;
251 case CACA_KEY_ESCAPE:
252 mplayer_put_key(KEY_ESC);
253 break;
254 case CACA_KEY_PAGEUP:
255 mplayer_put_key(KEY_PAGE_UP);
256 break;
257 case CACA_KEY_PAGEDOWN:
258 mplayer_put_key(KEY_PAGE_DOWN);
259 break;
260 case CACA_KEY_RETURN:
261 mplayer_put_key(KEY_ENTER);
262 break;
263 case CACA_KEY_HOME:
264 mplayer_put_key(KEY_HOME);
265 break;
266 case CACA_KEY_END:
267 mplayer_put_key(KEY_END);
268 break;
269 default:
270 if (key <= 255)
271 mplayer_put_key (key);
272 break;
278 static void uninit(void)
280 caca_free_bitmap(cbitmap);
281 cbitmap = NULL;
282 caca_end();
286 static void draw_osd(void)
288 if (vo_osd_progbar_type != -1)
289 osdpercent(MESSAGE_DURATION, 0, 255,
290 vo_osd_progbar_value, sub_osd_names[vo_osd_progbar_type],
291 "");
294 static int preinit(const char *arg)
296 if (arg)
298 mp_msg(MSGT_VO, MSGL_ERR, "vo_caca: Unknown subdevice: %s\n", arg);
299 return ENOSYS;
302 if (caca_init())
304 mp_msg(MSGT_VO, MSGL_ERR, "vo_caca: failed to initialize\n");
305 return ENOSYS;
308 caca_set_window_title("MPlayer");
310 /* Default libcaca features */
311 caca_set_feature(CACA_ANTIALIASING_PREFILTER);
312 caca_set_feature(CACA_DITHERING_RANDOM);
314 return 0;
317 static int query_format(uint32_t format)
319 if (format == IMGFMT_BGR24)
320 return VFCAP_OSD | VFCAP_CSP_SUPPORTED;
322 return 0;
325 static int control(uint32_t request, void *data, ...)
327 switch(request)
329 case VOCTRL_QUERY_FORMAT:
330 return query_format(*((uint32_t *)data));
331 default:
332 return VO_NOTIMPL;