Qt: it's Qt::Key_Backspace not Qt::Key_Back
[vlc.git] / modules / video_output / aa.c
blob925ecb3ad4fd4ba9e2993398e02a228f9b5c5659
1 /*****************************************************************************
2 * aa.c: "vout display" module using aalib
3 *****************************************************************************
4 * Copyright (C) 2002-2009 the VideoLAN team
5 * $Id$
7 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_vout_display.h>
34 #include <vlc_picture_pool.h>
36 #include <assert.h>
37 #include <aalib.h>
39 /* TODO
40 * - what about RGB palette ?
42 /*****************************************************************************
43 * Module descriptor
44 *****************************************************************************/
45 static int Open (vlc_object_t *);
46 static void Close(vlc_object_t *);
48 vlc_module_begin()
49 set_shortname(N_("ASCII Art"))
50 set_category(CAT_VIDEO)
51 set_subcategory(SUBCAT_VIDEO_VOUT)
52 set_description(N_("ASCII-art video output"))
53 set_capability("vout display", 10)
54 add_shortcut("aalib")
55 set_callbacks(Open, Close)
56 vlc_module_end()
58 /*****************************************************************************
59 * Local prototypes
60 *****************************************************************************/
61 static picture_pool_t *Pool (vout_display_t *, unsigned);
62 static void Prepare(vout_display_t *, picture_t *);
63 static void Display(vout_display_t *, picture_t *);
64 static int Control(vout_display_t *, int, va_list);
66 /* */
67 static void Manage(vout_display_t *);
69 /* */
70 struct vout_display_sys_t {
71 struct aa_context* aa_context;
72 aa_palette palette;
74 vout_display_cfg_t state;
75 picture_pool_t *pool;
78 /**
79 * This function allocates and initializes a aa vout method.
81 static int Open(vlc_object_t *object)
83 vout_display_t *vd = (vout_display_t *)object;
84 vout_display_sys_t *sys;
86 /* Allocate structure */
87 vd->sys = sys = calloc(1, sizeof(*sys));
88 if (!sys)
89 return VLC_ENOMEM;
91 /* Don't parse any options, but take $AAOPTS into account */
92 aa_parseoptions(NULL, NULL, NULL, NULL);
94 /* */
95 sys->aa_context = aa_autoinit(&aa_defparams);
96 if (!sys->aa_context) {
97 msg_Err(vd, "cannot initialize aalib");
98 goto error;
101 aa_autoinitkbd(sys->aa_context, 0);
102 aa_autoinitmouse(sys->aa_context, AA_MOUSEALLMASK);
104 /* */
105 video_format_t fmt = vd->fmt;
106 fmt.i_chroma = VLC_CODEC_RGB8;
107 fmt.i_width = aa_imgwidth(sys->aa_context);
108 fmt.i_height = aa_imgheight(sys->aa_context);
110 /* */
111 vout_display_info_t info = vd->info;
112 info.has_pictures_invalid = true;
114 /* Setup vout_display now that everything is fine */
115 vd->fmt = fmt;
116 vd->info = info;
118 vd->pool = Pool;
119 vd->prepare = Prepare;
120 vd->display = Display;
121 vd->control = Control;
122 vd->manage = Manage;
124 /* Inspect initial configuration and send correction events
125 * FIXME how to handle aspect ratio with aa ? */
126 sys->state = *vd->cfg;
127 sys->state.is_fullscreen = false;
128 vout_display_SendEventFullscreen(vd, false);
129 vout_display_SendEventDisplaySize(vd, fmt.i_width, fmt.i_height, false);
131 return VLC_SUCCESS;
133 error:
134 if (sys && sys->aa_context)
135 aa_close(sys->aa_context);
136 free(sys);
137 return VLC_EGENERIC;
141 * Close a aa video output method
143 static void Close(vlc_object_t *object)
145 vout_display_t *vd = (vout_display_t *)object;
146 vout_display_sys_t *sys = vd->sys;
148 if (sys->pool)
149 picture_pool_Delete(sys->pool);
150 aa_close(sys->aa_context);
151 free(sys);
155 * Return a pool of direct buffers
157 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
159 vout_display_sys_t *sys = vd->sys;
160 VLC_UNUSED(count);
162 if (!sys->pool) {
163 picture_resource_t rsc;
165 memset(&rsc, 0, sizeof(rsc));
166 rsc.p[0].p_pixels = aa_image(sys->aa_context);
167 rsc.p[0].i_pitch = aa_imgwidth(sys->aa_context);
168 rsc.p[0].i_lines = aa_imgheight(sys->aa_context);
170 picture_t *p_picture = picture_NewFromResource(&vd->fmt, &rsc);
171 if (!p_picture)
172 return NULL;
174 sys->pool = picture_pool_New(1, &p_picture);
176 return sys->pool;
180 * Prepare a picture for display */
181 static void Prepare(vout_display_t *vd, picture_t *picture)
183 vout_display_sys_t *sys = vd->sys;
185 assert(vd->fmt.i_width == aa_imgwidth(sys->aa_context) &&
186 vd->fmt.i_height == aa_imgheight(sys->aa_context));
188 #if 0
189 if (picture->format.p_palette) {
190 for (int i = 0; i < 256; i++) {
191 aa_setpalette(vd->sys->palette, 256 - i,
192 red[ i ], green[ i ], blue[ i ]);
195 #else
196 VLC_UNUSED(picture);
197 #endif
199 aa_fastrender(sys->aa_context, 0, 0,
200 vd->fmt.i_width, vd->fmt.i_height);
204 * Display a picture
206 static void Display(vout_display_t *vd, picture_t *picture)
208 vout_display_sys_t *sys = vd->sys;
210 aa_flush(sys->aa_context);
211 picture_Release(picture);
215 * Control for vout display
217 static int Control(vout_display_t *vd, int query, va_list args)
219 VLC_UNUSED(args);
220 vout_display_sys_t *sys = vd->sys;
222 switch (query) {
223 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
224 /* We have to ignore what is requested */
225 vout_display_SendEventPicturesInvalid(vd);
226 return VLC_SUCCESS;
228 case VOUT_DISPLAY_RESET_PICTURES:
229 if (sys->pool)
230 picture_pool_Delete(sys->pool);
231 sys->pool = NULL;
233 vd->fmt.i_width = aa_imgwidth(sys->aa_context);
234 vd->fmt.i_height = aa_imgheight(sys->aa_context);
235 return VLC_SUCCESS;
237 case VOUT_DISPLAY_HIDE_MOUSE:
238 aa_hidemouse(sys->aa_context);
239 return VLC_SUCCESS;
241 default:
242 msg_Err(vd, "Unsupported query in vout display aalib");
243 return VLC_EGENERIC;
249 * Proccess pending event
251 static void Manage(vout_display_t *vd)
253 vout_display_sys_t *sys = vd->sys;
255 for (;;) {
256 const int event = aa_getevent(sys->aa_context, 0);
257 if (!event)
258 return;
260 switch (event) {
261 case AA_MOUSE: {
262 int x, y;
263 int button;
264 int vlc;
265 aa_getmouse(sys->aa_context, &x, &y, &button);
267 vlc = 0;
268 if (button & AA_BUTTON1)
269 vlc |= 1 << MOUSE_BUTTON_LEFT;
270 if (button & AA_BUTTON2)
271 vlc |= 1 << MOUSE_BUTTON_CENTER;
272 if (button & AA_BUTTON3)
273 vlc |= 1 << MOUSE_BUTTON_RIGHT;
275 vout_display_SendEventMouseState(vd, x, y, vlc);
277 aa_showcursor(sys->aa_context); /* Not perfect, we show it on click too */
278 break;
281 case AA_RESIZE:
282 aa_resize(sys->aa_context);
283 vout_display_SendEventDisplaySize(vd,
284 aa_imgwidth(sys->aa_context),
285 aa_imgheight(sys->aa_context), false);
286 break;
288 /* TODO keys support to complete */
289 case AA_UP:
290 vout_display_SendEventKey(vd, KEY_UP);
291 break;
292 case AA_DOWN:
293 vout_display_SendEventKey(vd, KEY_DOWN);
294 break;
295 case AA_RIGHT:
296 vout_display_SendEventKey(vd, KEY_RIGHT);
297 break;
298 case AA_LEFT:
299 vout_display_SendEventKey(vd, KEY_LEFT);
300 break;
301 case AA_BACKSPACE:
302 vout_display_SendEventKey(vd, KEY_BACKSPACE);
303 break;
304 case AA_ESC:
305 vout_display_SendEventKey(vd, KEY_ESC);
306 break;
307 default:
308 if (event >= 0x20 && event <= 0x7f)
309 vout_display_SendEventKey(vd, event);
310 break;