1 /*****************************************************************************
2 * aa.c: "vout display" module using aalib
3 *****************************************************************************
4 * Copyright (C) 2002-2009 the VideoLAN team
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 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_vout_display.h>
34 #include <vlc_picture_pool.h>
35 #include "keythread.h"
41 * - what about RGB palette ?
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open (vlc_object_t *);
47 static void Close(vlc_object_t *);
50 set_shortname(N_("ASCII Art"))
51 set_category(CAT_VIDEO)
52 set_subcategory(SUBCAT_VIDEO_VOUT)
53 set_description(N_("ASCII-art video output"))
54 set_capability("vout display", 10)
56 set_callbacks(Open, Close)
59 /*****************************************************************************
61 *****************************************************************************/
62 static picture_pool_t *Pool (vout_display_t *, unsigned);
63 static void Prepare(vout_display_t *, picture_t *);
64 static void Display(vout_display_t *, picture_t *);
65 static int Control(vout_display_t *, int, va_list);
68 static void Manage(vout_display_t *);
71 struct vout_display_sys_t {
72 struct aa_context* aa_context;
75 vout_display_cfg_t state;
81 * This function allocates and initializes a aa vout method.
83 static int Open(vlc_object_t *object)
85 vout_display_t *vd = (vout_display_t *)object;
86 vout_display_sys_t *sys;
88 /* Allocate structure */
89 vd->sys = sys = calloc(1, sizeof(*sys));
93 /* Don't parse any options, but take $AAOPTS into account */
94 aa_parseoptions(NULL, NULL, NULL, NULL);
97 sys->aa_context = aa_autoinit(&aa_defparams);
98 if (!sys->aa_context) {
99 msg_Err(vd, "cannot initialize aalib");
102 vout_display_DeleteWindow(vd, NULL);
104 aa_autoinitkbd(sys->aa_context, 0);
105 aa_autoinitmouse(sys->aa_context, AA_MOUSEALLMASK);
108 video_format_t fmt = vd->fmt;
109 fmt.i_chroma = VLC_CODEC_RGB8;
110 fmt.i_width = aa_imgwidth(sys->aa_context);
111 fmt.i_height = aa_imgheight(sys->aa_context);
114 vout_display_info_t info = vd->info;
115 info.has_pictures_invalid = true;
117 /* Setup vout_display now that everything is fine */
122 vd->prepare = Prepare;
123 vd->display = Display;
124 vd->control = Control;
127 /* Inspect initial configuration and send correction events
128 * FIXME how to handle aspect ratio with aa ? */
129 sys->state = *vd->cfg;
130 sys->state.is_fullscreen = false;
131 vout_display_SendEventFullscreen(vd, false);
132 vout_display_SendEventDisplaySize(vd, fmt.i_width, fmt.i_height, false);
134 sys->keys = vlc_CreateKeyThread(vd);
138 if (sys && sys->aa_context)
139 aa_close(sys->aa_context);
145 * Close a aa video output method
147 static void Close(vlc_object_t *object)
149 vout_display_t *vd = (vout_display_t *)object;
150 vout_display_sys_t *sys = vd->sys;
152 vlc_DestroyKeyTread(sys->keys);
154 picture_pool_Delete(sys->pool);
155 aa_close(sys->aa_context);
160 * Return a pool of direct buffers
162 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
164 vout_display_sys_t *sys = vd->sys;
168 picture_resource_t rsc;
170 memset(&rsc, 0, sizeof(rsc));
171 rsc.p[0].p_pixels = aa_image(sys->aa_context);
172 rsc.p[0].i_pitch = aa_imgwidth(sys->aa_context);
173 rsc.p[0].i_lines = aa_imgheight(sys->aa_context);
175 picture_t *p_picture = picture_NewFromResource(&vd->fmt, &rsc);
179 sys->pool = picture_pool_New(1, &p_picture);
185 * Prepare a picture for display */
186 static void Prepare(vout_display_t *vd, picture_t *picture)
188 vout_display_sys_t *sys = vd->sys;
190 assert(vd->fmt.i_width == aa_imgwidth(sys->aa_context) &&
191 vd->fmt.i_height == aa_imgheight(sys->aa_context));
194 if (picture->format.p_palette) {
195 for (int i = 0; i < 256; i++) {
196 aa_setpalette(vd->sys->palette, 256 - i,
197 red[ i ], green[ i ], blue[ i ]);
204 aa_fastrender(sys->aa_context, 0, 0,
205 vd->fmt.i_width, vd->fmt.i_height);
211 static void Display(vout_display_t *vd, picture_t *picture)
213 vout_display_sys_t *sys = vd->sys;
215 aa_flush(sys->aa_context);
216 picture_Release(picture);
220 * Control for vout display
222 static int Control(vout_display_t *vd, int query, va_list args)
225 vout_display_sys_t *sys = vd->sys;
228 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
229 /* We have to ignore what is requested */
230 vout_display_SendEventPicturesInvalid(vd);
233 case VOUT_DISPLAY_RESET_PICTURES:
235 picture_pool_Delete(sys->pool);
238 vd->fmt.i_width = aa_imgwidth(sys->aa_context);
239 vd->fmt.i_height = aa_imgheight(sys->aa_context);
242 case VOUT_DISPLAY_HIDE_MOUSE:
243 aa_hidemouse(sys->aa_context);
247 msg_Err(vd, "Unsupported query in vout display aalib");
254 * Proccess pending event
256 static void Manage(vout_display_t *vd)
258 vout_display_sys_t *sys = vd->sys;
261 const int event = aa_getevent(sys->aa_context, 0);
270 aa_getmouse(sys->aa_context, &x, &y, &button);
273 if (button & AA_BUTTON1)
274 vlc |= 1 << MOUSE_BUTTON_LEFT;
275 if (button & AA_BUTTON2)
276 vlc |= 1 << MOUSE_BUTTON_CENTER;
277 if (button & AA_BUTTON3)
278 vlc |= 1 << MOUSE_BUTTON_RIGHT;
280 vout_display_SendEventMouseState(vd, x, y, vlc);
282 aa_showcursor(sys->aa_context); /* Not perfect, we show it on click too */
287 aa_resize(sys->aa_context);
288 vout_display_SendEventDisplaySize(vd,
289 aa_imgwidth(sys->aa_context),
290 aa_imgheight(sys->aa_context), false);
293 /* TODO keys support to complete */
295 vlc_EmitKey(sys->keys, KEY_UP);
298 vlc_EmitKey(sys->keys, KEY_DOWN);
301 vlc_EmitKey(sys->keys, KEY_RIGHT);
304 vlc_EmitKey(sys->keys, KEY_LEFT);
307 vlc_EmitKey(sys->keys, KEY_BACKSPACE);
310 vlc_EmitKey(sys->keys, KEY_ESC);
313 if (event >= 0x20 && event <= 0x7f)
314 vlc_EmitKey(sys->keys, event);