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>
40 # ifdef X_DISPLAY_MISSING
41 # error Xlib required due to XInitThreads
43 # include <vlc_xlib.h>
47 * - what about RGB palette ?
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Open (vlc_object_t *);
53 static void Close(vlc_object_t *);
56 set_shortname(N_("ASCII Art"))
57 set_category(CAT_VIDEO)
58 set_subcategory(SUBCAT_VIDEO_VOUT)
59 set_description(N_("ASCII-art video output"))
60 set_capability("vout display", 10)
62 set_callbacks(Open, Close)
65 /*****************************************************************************
67 *****************************************************************************/
68 static picture_pool_t *Pool (vout_display_t *, unsigned);
69 static void Prepare(vout_display_t *, picture_t *, subpicture_t *);
70 static void PictureDisplay(vout_display_t *, picture_t *, subpicture_t *);
71 static int Control(vout_display_t *, int, va_list);
74 static void Manage(vout_display_t *);
77 struct vout_display_sys_t {
78 struct aa_context* aa_context;
81 vout_display_cfg_t state;
86 * This function allocates and initializes a aa vout method.
88 static int Open(vlc_object_t *object)
90 vout_display_t *vd = (vout_display_t *)object;
91 vout_display_sys_t *sys;
94 if (!vlc_xlib_init (object))
98 /* Allocate structure */
99 vd->sys = sys = calloc(1, sizeof(*sys));
103 /* Don't parse any options, but take $AAOPTS into account */
104 aa_parseoptions(NULL, NULL, NULL, NULL);
107 sys->aa_context = aa_autoinit(&aa_defparams);
108 if (!sys->aa_context) {
109 msg_Err(vd, "cannot initialize aalib");
112 vout_display_DeleteWindow(vd, NULL);
114 aa_autoinitkbd(sys->aa_context, 0);
115 aa_autoinitmouse(sys->aa_context, AA_MOUSEALLMASK);
118 video_format_t fmt = vd->fmt;
119 fmt.i_chroma = VLC_CODEC_RGB8;
120 fmt.i_width = aa_imgwidth(sys->aa_context);
121 fmt.i_height = aa_imgheight(sys->aa_context);
124 vout_display_info_t info = vd->info;
125 info.has_pictures_invalid = true;
127 /* Setup vout_display now that everything is fine */
132 vd->prepare = Prepare;
133 vd->display = PictureDisplay;
134 vd->control = Control;
137 /* Inspect initial configuration and send correction events
138 * FIXME how to handle aspect ratio with aa ? */
139 sys->state = *vd->cfg;
140 sys->state.is_fullscreen = false;
141 vout_display_SendEventFullscreen(vd, false);
142 vout_display_SendEventDisplaySize(vd, fmt.i_width, fmt.i_height, false);
147 if (sys && sys->aa_context)
148 aa_close(sys->aa_context);
154 * Close a aa video output method
156 static void Close(vlc_object_t *object)
158 vout_display_t *vd = (vout_display_t *)object;
159 vout_display_sys_t *sys = vd->sys;
162 picture_pool_Delete(sys->pool);
163 aa_close(sys->aa_context);
168 * Return a pool of direct buffers
170 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
172 vout_display_sys_t *sys = vd->sys;
176 picture_resource_t rsc;
178 memset(&rsc, 0, sizeof(rsc));
179 rsc.p[0].p_pixels = aa_image(sys->aa_context);
180 rsc.p[0].i_pitch = aa_imgwidth(sys->aa_context);
181 rsc.p[0].i_lines = aa_imgheight(sys->aa_context);
183 picture_t *p_picture = picture_NewFromResource(&vd->fmt, &rsc);
187 sys->pool = picture_pool_New(1, &p_picture);
193 * Prepare a picture for display */
194 static void Prepare(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
196 vout_display_sys_t *sys = vd->sys;
198 assert(vd->fmt.i_width == aa_imgwidth(sys->aa_context) &&
199 vd->fmt.i_height == aa_imgheight(sys->aa_context));
202 if (picture->format.p_palette) {
203 for (int i = 0; i < 256; i++) {
204 aa_setpalette(vd->sys->palette, 256 - i,
205 red[ i ], green[ i ], blue[ i ]);
211 VLC_UNUSED(subpicture);
213 aa_fastrender(sys->aa_context, 0, 0,
214 vd->fmt.i_width, vd->fmt.i_height);
220 static void PictureDisplay(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
222 vout_display_sys_t *sys = vd->sys;
224 aa_flush(sys->aa_context);
225 picture_Release(picture);
226 VLC_UNUSED(subpicture);
230 * Control for vout display
232 static int Control(vout_display_t *vd, int query, va_list args)
235 vout_display_sys_t *sys = vd->sys;
238 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
239 /* We have to ignore what is requested */
240 vout_display_SendEventPicturesInvalid(vd);
243 case VOUT_DISPLAY_RESET_PICTURES:
245 picture_pool_Delete(sys->pool);
248 vd->fmt.i_width = aa_imgwidth(sys->aa_context);
249 vd->fmt.i_height = aa_imgheight(sys->aa_context);
252 case VOUT_DISPLAY_HIDE_MOUSE:
253 aa_hidemouse(sys->aa_context);
257 msg_Err(vd, "Unsupported query in vout display aalib");
264 * Proccess pending event
266 static void Manage(vout_display_t *vd)
268 vout_display_sys_t *sys = vd->sys;
271 const int event = aa_getevent(sys->aa_context, 0);
280 aa_getmouse(sys->aa_context, &x, &y, &button);
283 if (button & AA_BUTTON1)
284 vlc |= 1 << MOUSE_BUTTON_LEFT;
285 if (button & AA_BUTTON2)
286 vlc |= 1 << MOUSE_BUTTON_CENTER;
287 if (button & AA_BUTTON3)
288 vlc |= 1 << MOUSE_BUTTON_RIGHT;
290 vout_display_SendEventMouseState(vd, x, y, vlc);
292 aa_showcursor(sys->aa_context); /* Not perfect, we show it on click too */
297 aa_resize(sys->aa_context);
298 vout_display_SendEventDisplaySize(vd,
299 aa_imgwidth(sys->aa_context),
300 aa_imgheight(sys->aa_context), false);
303 /* TODO keys support to complete */
305 vout_display_SendEventKey(vd, KEY_UP);
308 vout_display_SendEventKey(vd, KEY_DOWN);
311 vout_display_SendEventKey(vd, KEY_RIGHT);
314 vout_display_SendEventKey(vd, KEY_LEFT);
317 vout_display_SendEventKey(vd, KEY_BACKSPACE);
320 vout_display_SendEventKey(vd, KEY_ESC);
323 if (event >= 0x20 && event <= 0x7f)
324 vout_display_SendEventKey(vd, event);