]> git.sesse.net Git - vlc/blob - modules/video_output/aa.c
5c9a256823674c6e9da2dd16d2e754a61610646c
[vlc] / modules / video_output / aa.c
1 /*****************************************************************************
2  * aa.c: "vout display" module using aalib
3  *****************************************************************************
4  * Copyright (C) 2002-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_vout_display.h>
34 #include <vlc_picture_pool.h>
35
36 #include <assert.h>
37 #include <aalib.h>
38
39 /* TODO
40  * - what about RGB palette ?
41  */
42 /*****************************************************************************
43  * Module descriptor
44  *****************************************************************************/
45 static int  Open (vlc_object_t *);
46 static void Close(vlc_object_t *);
47
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()
57
58 /*****************************************************************************
59  * Local prototypes
60  *****************************************************************************/
61 static picture_t *Get    (vout_display_t *);
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);
65
66 /* */
67 static void Manage(vout_display_t *);
68
69 /* */
70 struct vout_display_sys_t {
71     struct aa_context*  aa_context;
72     aa_palette          palette;
73
74     vout_display_cfg_t  state;
75     picture_pool_t      *pool;
76 };
77
78 /**
79  * This function allocates and initializes a aa vout method.
80  */
81 static int Open(vlc_object_t *object)
82 {
83     vout_display_t *vd = (vout_display_t *)object;
84     vout_display_sys_t *sys;
85
86     /* Allocate structure */
87     vd->sys = sys = calloc(1, sizeof(*sys));
88     if (!sys)
89         return VLC_ENOMEM;
90
91     /* Don't parse any options, but take $AAOPTS into account */
92     aa_parseoptions(NULL, NULL, NULL, NULL);
93
94     /* */
95     sys->aa_context = aa_autoinit(&aa_defparams);
96     if (!sys->aa_context) {
97         msg_Err(vd, "cannot initialize aalib");
98         goto error;
99     }
100
101     aa_autoinitkbd(sys->aa_context, 0);
102     aa_autoinitmouse(sys->aa_context, AA_MOUSEALLMASK);
103
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);
109
110     /* */
111     vout_display_info_t info = vd->info;
112     info.has_pictures_invalid = true;
113
114     /* Setup vout_display now that everything is fine */
115     vd->fmt = fmt;
116     vd->info = info;
117
118     vd->get = Get;
119     vd->prepare = Prepare;
120     vd->display = Display;
121     vd->control = Control;
122     vd->manage = Manage;
123
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);
130
131     return VLC_SUCCESS;
132
133 error:
134     if (sys && sys->aa_context)
135         aa_close(sys->aa_context);
136     free(sys);
137     return VLC_EGENERIC;
138 }
139
140 /**
141  * Close a aa video output method
142  */
143 static void Close(vlc_object_t *object)
144 {
145     vout_display_t *vd = (vout_display_t *)object;
146     vout_display_sys_t *sys = vd->sys;
147
148     if (sys->pool)
149         picture_pool_Delete(sys->pool);
150     aa_close(sys->aa_context);
151     free(sys);
152 }
153
154 /**
155  * Return a direct buffer
156  */
157 static picture_t *Get(vout_display_t *vd)
158 {
159     vout_display_sys_t *sys = vd->sys;
160
161     if (!sys->pool) {
162         picture_resource_t rsc;
163
164         memset(&rsc, 0, sizeof(rsc));
165         rsc.p[0].p_pixels = aa_image(sys->aa_context);
166         rsc.p[0].i_pitch = aa_imgwidth(sys->aa_context);
167         rsc.p[0].i_lines = aa_imgheight(sys->aa_context);
168
169         picture_t *p_picture = picture_NewFromResource(&vd->fmt, &rsc);
170         if (!p_picture)
171             return NULL;
172
173         sys->pool = picture_pool_New(1, &p_picture);
174         if (!sys->pool)
175             return NULL;
176     }
177
178     return picture_pool_Get(sys->pool);
179 }
180
181 /**
182  * Prepare a picture for display */
183 static void Prepare(vout_display_t *vd, picture_t *picture)
184 {
185     vout_display_sys_t *sys = vd->sys;
186
187     assert(vd->fmt.i_width  == aa_imgwidth(sys->aa_context) &&
188            vd->fmt.i_height == aa_imgheight(sys->aa_context));
189
190 #if 0
191     if (picture->format.p_palette) {
192         for (int i = 0; i < 256; i++) {
193             aa_setpalette(vd->sys->palette, 256 - i,
194                            red[ i ], green[ i ], blue[ i ]);
195         }
196     }
197 #endif
198
199     aa_fastrender(sys->aa_context, 0, 0,
200                   vd->fmt.i_width, vd->fmt.i_height);
201 }
202
203 /**
204  * Display a picture
205  */
206 static void Display(vout_display_t *vd, picture_t *picture)
207 {
208     vout_display_sys_t *sys = vd->sys;
209
210     aa_flush(sys->aa_context);
211     picture_Release(picture);
212 }
213
214 /**
215  * Control for vout display
216  */
217 static int Control(vout_display_t *vd, int query, va_list args)
218 {
219     vout_display_sys_t *sys = vd->sys;
220
221     switch (query) {
222     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
223         /* We have to ignore what is requested */
224         vout_display_SendEventPicturesInvalid(vd);
225         return VLC_SUCCESS;
226
227     case VOUT_DISPLAY_RESET_PICTURES:
228         if (sys->pool)
229             picture_pool_Delete(sys->pool);
230         sys->pool = NULL;
231
232         vd->fmt.i_width  = aa_imgwidth(sys->aa_context);
233         vd->fmt.i_height = aa_imgheight(sys->aa_context);
234         return VLC_SUCCESS;
235
236     case VOUT_DISPLAY_HIDE_MOUSE:
237         aa_hidemouse(sys->aa_context);
238         return VLC_SUCCESS;
239
240     default:
241         msg_Err(vd, "Unsupported query in vout display aalib");
242         return VLC_EGENERIC;
243     }
244 }
245
246
247 /**
248  * Proccess pending event
249  */
250 static void Manage(vout_display_t *vd)
251 {
252     vout_display_sys_t *sys = vd->sys;
253
254     for (;;) {
255         const int event = aa_getevent(sys->aa_context, 0);
256         if (!event)
257             return;
258
259         switch (event) {
260         case AA_MOUSE: {
261             int x, y;
262             int button;
263             int vlc;
264             aa_getmouse(sys->aa_context, &x, &y, &button);
265
266             vlc = 0;
267             if (button & AA_BUTTON1)
268                 vlc |= 1 << MOUSE_BUTTON_LEFT;
269             if (button & AA_BUTTON2)
270                 vlc |= 1 << MOUSE_BUTTON_CENTER;
271             if (button & AA_BUTTON3)
272                 vlc |= 1 << MOUSE_BUTTON_RIGHT;
273
274             vout_display_SendEventMouseState(vd, x, y, vlc);
275
276             aa_showcursor(sys->aa_context); /* Not perfect, we show it on click too */
277             break;
278         }
279
280         case AA_RESIZE:
281             aa_resize(sys->aa_context);
282             vout_display_SendEventDisplaySize(vd,
283                                               aa_imgwidth(sys->aa_context),
284                                               aa_imgheight(sys->aa_context));
285             break;
286
287         /* TODO keys support to complete */
288         case AA_UP:
289             vout_display_SendEventKey(vd, KEY_UP);
290             break;
291         case AA_DOWN:
292             vout_display_SendEventKey(vd, KEY_DOWN);
293             break;
294         case AA_RIGHT:
295             vout_display_SendEventKey(vd, KEY_RIGHT);
296             break;
297         case AA_LEFT:
298             vout_display_SendEventKey(vd, KEY_LEFT);
299             break;
300         case AA_BACKSPACE:
301             vout_display_SendEventKey(vd, KEY_BACKSPACE);
302             break;
303         case AA_ESC:
304             vout_display_SendEventKey(vd, KEY_ESC);
305             break;
306         case 0x20:
307                 vout_display_SendEventKey(vd, KEY_SPACE);
308                 break;
309         default:
310             if (event >= 0x20 && event <= 0x7f)
311                 vout_display_SendEventKey(vd, event);
312             break;
313         }
314     }
315 }
316