]> git.sesse.net Git - vlc/blob - modules/video_output/aa.c
sftp: change item b_net
[vlc] / modules / video_output / aa.c
1 /*****************************************************************************
2  * aa.c: "vout display" module using aalib
3  *****************************************************************************
4  * Copyright (C) 2002-2009 VLC authors and VideoLAN
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 it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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 #ifndef _WIN32
40 # ifdef X_DISPLAY_MISSING
41 #  error Xlib required due to XInitThreads
42 # endif
43 # include <vlc_xlib.h>
44 #endif
45
46 /* TODO
47  * - what about RGB palette ?
48  */
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52 static int  Open (vlc_object_t *);
53 static void Close(vlc_object_t *);
54
55 vlc_module_begin()
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*/0)
61     add_shortcut("aalib")
62     set_callbacks(Open, Close)
63 vlc_module_end()
64
65 /*****************************************************************************
66  * Local prototypes
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);
72
73 /* */
74 static void Manage(vout_display_t *);
75
76 /* */
77 struct vout_display_sys_t {
78     struct aa_context*  aa_context;
79     aa_palette          palette;
80
81     vout_display_cfg_t  state;
82     picture_pool_t      *pool;
83 };
84
85 /**
86  * This function allocates and initializes a aa vout method.
87  */
88 static int Open(vlc_object_t *object)
89 {
90     vout_display_t *vd = (vout_display_t *)object;
91     vout_display_sys_t *sys;
92
93 #ifndef _WIN32
94     if (!vlc_xlib_init (object))
95         return VLC_EGENERIC;
96 #endif
97
98     /* Allocate structure */
99     vd->sys = sys = calloc(1, sizeof(*sys));
100     if (!sys)
101         return VLC_ENOMEM;
102
103     /* Don't parse any options, but take $AAOPTS into account */
104     aa_parseoptions(NULL, NULL, NULL, NULL);
105
106     /* */
107     sys->aa_context = aa_autoinit(&aa_defparams);
108     if (!sys->aa_context) {
109         msg_Err(vd, "cannot initialize aalib");
110         goto error;
111     }
112     vout_display_DeleteWindow(vd, NULL);
113
114     aa_autoinitkbd(sys->aa_context, 0);
115     aa_autoinitmouse(sys->aa_context, AA_MOUSEALLMASK);
116
117     /* */
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);
122     fmt.i_visible_width = fmt.i_width;
123     fmt.i_visible_height = fmt.i_height;
124
125     /* */
126     vout_display_info_t info = vd->info;
127     info.has_pictures_invalid = true;
128
129     /* Setup vout_display now that everything is fine */
130     vd->fmt = fmt;
131     vd->info = info;
132
133     vd->pool    = Pool;
134     vd->prepare = Prepare;
135     vd->display = PictureDisplay;
136     vd->control = Control;
137     vd->manage  = Manage;
138
139     /* Inspect initial configuration and send correction events
140      * FIXME how to handle aspect ratio with aa ? */
141     sys->state = *vd->cfg;
142     sys->state.is_fullscreen = false;
143     vout_display_SendEventFullscreen(vd, false);
144     vout_display_SendEventDisplaySize(vd, fmt.i_width, fmt.i_height);
145
146     return VLC_SUCCESS;
147
148 error:
149     if (sys && sys->aa_context)
150         aa_close(sys->aa_context);
151     free(sys);
152     return VLC_EGENERIC;
153 }
154
155 /**
156  * Close a aa video output method
157  */
158 static void Close(vlc_object_t *object)
159 {
160     vout_display_t *vd = (vout_display_t *)object;
161     vout_display_sys_t *sys = vd->sys;
162
163     if (sys->pool)
164         picture_pool_Release(sys->pool);
165     aa_close(sys->aa_context);
166     free(sys);
167 }
168
169 /**
170  * Return a pool of direct buffers
171  */
172 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
173 {
174     vout_display_sys_t *sys = vd->sys;
175     VLC_UNUSED(count);
176
177     if (!sys->pool) {
178         picture_resource_t rsc;
179
180         memset(&rsc, 0, sizeof(rsc));
181         rsc.p[0].p_pixels = aa_image(sys->aa_context);
182         rsc.p[0].i_pitch = aa_imgwidth(sys->aa_context);
183         rsc.p[0].i_lines = aa_imgheight(sys->aa_context);
184
185         picture_t *p_picture = picture_NewFromResource(&vd->fmt, &rsc);
186         if (!p_picture)
187             return NULL;
188
189         sys->pool = picture_pool_New(1, &p_picture);
190     }
191     return sys->pool;
192 }
193
194 /**
195  * Prepare a picture for display */
196 static void Prepare(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
197 {
198     vout_display_sys_t *sys = vd->sys;
199
200     assert(vd->fmt.i_width  == aa_imgwidth(sys->aa_context) &&
201            vd->fmt.i_height == aa_imgheight(sys->aa_context));
202
203 #if 0
204     if (picture->format.p_palette) {
205         for (int i = 0; i < 256; i++) {
206             aa_setpalette(vd->sys->palette, 256 - i,
207                            red[ i ], green[ i ], blue[ i ]);
208         }
209     }
210 #else
211     VLC_UNUSED(picture);
212 #endif
213     VLC_UNUSED(subpicture);
214
215     aa_fastrender(sys->aa_context, 0, 0,
216                   vd->fmt.i_width, vd->fmt.i_height);
217 }
218
219 /**
220  * Display a picture
221  */
222 static void PictureDisplay(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
223 {
224     vout_display_sys_t *sys = vd->sys;
225
226     aa_flush(sys->aa_context);
227     picture_Release(picture);
228     VLC_UNUSED(subpicture);
229 }
230
231 /**
232  * Control for vout display
233  */
234 static int Control(vout_display_t *vd, int query, va_list args)
235 {
236     VLC_UNUSED(args);
237     vout_display_sys_t *sys = vd->sys;
238
239     switch (query) {
240     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
241         /* We have to ignore what is requested */
242         vout_display_SendEventPicturesInvalid(vd);
243         return VLC_SUCCESS;
244
245     case VOUT_DISPLAY_RESET_PICTURES:
246         if (sys->pool)
247             picture_pool_Release(sys->pool);
248         sys->pool = NULL;
249
250         vd->fmt.i_width  = aa_imgwidth(sys->aa_context);
251         vd->fmt.i_height = aa_imgheight(sys->aa_context);
252         return VLC_SUCCESS;
253
254     case VOUT_DISPLAY_HIDE_MOUSE:
255         aa_hidemouse(sys->aa_context);
256         return VLC_SUCCESS;
257
258     default:
259         msg_Err(vd, "Unsupported query in vout display aalib");
260         return VLC_EGENERIC;
261     }
262 }
263
264
265 /**
266  * Proccess pending event
267  */
268 static void Manage(vout_display_t *vd)
269 {
270     vout_display_sys_t *sys = vd->sys;
271
272     for (;;) {
273         const int event = aa_getevent(sys->aa_context, 0);
274         if (!event)
275             return;
276
277         switch (event) {
278         case AA_MOUSE: {
279             int x, y;
280             int button;
281             int vlc;
282             aa_getmouse(sys->aa_context, &x, &y, &button);
283
284             vlc = 0;
285             if (button & AA_BUTTON1)
286                 vlc |= 1 << MOUSE_BUTTON_LEFT;
287             if (button & AA_BUTTON2)
288                 vlc |= 1 << MOUSE_BUTTON_CENTER;
289             if (button & AA_BUTTON3)
290                 vlc |= 1 << MOUSE_BUTTON_RIGHT;
291
292             vout_display_SendEventMouseState(vd, x, y, vlc);
293
294             aa_showcursor(sys->aa_context); /* Not perfect, we show it on click too */
295             break;
296         }
297
298         case AA_RESIZE:
299             aa_resize(sys->aa_context);
300             vout_display_SendEventDisplaySize(vd,
301                                               aa_imgwidth(sys->aa_context),
302                                               aa_imgheight(sys->aa_context));
303             break;
304
305         /* TODO keys support to complete */
306         case AA_UP:
307             vout_display_SendEventKey(vd, KEY_UP);
308             break;
309         case AA_DOWN:
310             vout_display_SendEventKey(vd, KEY_DOWN);
311             break;
312         case AA_RIGHT:
313             vout_display_SendEventKey(vd, KEY_RIGHT);
314             break;
315         case AA_LEFT:
316             vout_display_SendEventKey(vd, KEY_LEFT);
317             break;
318         case AA_BACKSPACE:
319             vout_display_SendEventKey(vd, KEY_BACKSPACE);
320             break;
321         case AA_ESC:
322             vout_display_SendEventKey(vd, KEY_ESC);
323             break;
324         default:
325             if (event >= 0x20 && event <= 0x7f)
326                 vout_display_SendEventKey(vd, event);
327             break;
328         }
329     }
330 }
331