]> git.sesse.net Git - vlc/blob - modules/video_output/aa.c
Revert "contrib: make AUTOPOINT point to the script installed by gettext"
[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 #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)
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
123     /* */
124     vout_display_info_t info = vd->info;
125     info.has_pictures_invalid = true;
126
127     /* Setup vout_display now that everything is fine */
128     vd->fmt = fmt;
129     vd->info = info;
130
131     vd->pool    = Pool;
132     vd->prepare = Prepare;
133     vd->display = PictureDisplay;
134     vd->control = Control;
135     vd->manage  = Manage;
136
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);
143
144     return VLC_SUCCESS;
145
146 error:
147     if (sys && sys->aa_context)
148         aa_close(sys->aa_context);
149     free(sys);
150     return VLC_EGENERIC;
151 }
152
153 /**
154  * Close a aa video output method
155  */
156 static void Close(vlc_object_t *object)
157 {
158     vout_display_t *vd = (vout_display_t *)object;
159     vout_display_sys_t *sys = vd->sys;
160
161     if (sys->pool)
162         picture_pool_Delete(sys->pool);
163     aa_close(sys->aa_context);
164     free(sys);
165 }
166
167 /**
168  * Return a pool of direct buffers
169  */
170 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
171 {
172     vout_display_sys_t *sys = vd->sys;
173     VLC_UNUSED(count);
174
175     if (!sys->pool) {
176         picture_resource_t rsc;
177
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);
182
183         picture_t *p_picture = picture_NewFromResource(&vd->fmt, &rsc);
184         if (!p_picture)
185             return NULL;
186
187         sys->pool = picture_pool_New(1, &p_picture);
188     }
189     return sys->pool;
190 }
191
192 /**
193  * Prepare a picture for display */
194 static void Prepare(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
195 {
196     vout_display_sys_t *sys = vd->sys;
197
198     assert(vd->fmt.i_width  == aa_imgwidth(sys->aa_context) &&
199            vd->fmt.i_height == aa_imgheight(sys->aa_context));
200
201 #if 0
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 ]);
206         }
207     }
208 #else
209     VLC_UNUSED(picture);
210 #endif
211     VLC_UNUSED(subpicture);
212
213     aa_fastrender(sys->aa_context, 0, 0,
214                   vd->fmt.i_width, vd->fmt.i_height);
215 }
216
217 /**
218  * Display a picture
219  */
220 static void PictureDisplay(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
221 {
222     vout_display_sys_t *sys = vd->sys;
223
224     aa_flush(sys->aa_context);
225     picture_Release(picture);
226     VLC_UNUSED(subpicture);
227 }
228
229 /**
230  * Control for vout display
231  */
232 static int Control(vout_display_t *vd, int query, va_list args)
233 {
234     VLC_UNUSED(args);
235     vout_display_sys_t *sys = vd->sys;
236
237     switch (query) {
238     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
239         /* We have to ignore what is requested */
240         vout_display_SendEventPicturesInvalid(vd);
241         return VLC_SUCCESS;
242
243     case VOUT_DISPLAY_RESET_PICTURES:
244         if (sys->pool)
245             picture_pool_Delete(sys->pool);
246         sys->pool = NULL;
247
248         vd->fmt.i_width  = aa_imgwidth(sys->aa_context);
249         vd->fmt.i_height = aa_imgheight(sys->aa_context);
250         return VLC_SUCCESS;
251
252     case VOUT_DISPLAY_HIDE_MOUSE:
253         aa_hidemouse(sys->aa_context);
254         return VLC_SUCCESS;
255
256     default:
257         msg_Err(vd, "Unsupported query in vout display aalib");
258         return VLC_EGENERIC;
259     }
260 }
261
262
263 /**
264  * Proccess pending event
265  */
266 static void Manage(vout_display_t *vd)
267 {
268     vout_display_sys_t *sys = vd->sys;
269
270     for (;;) {
271         const int event = aa_getevent(sys->aa_context, 0);
272         if (!event)
273             return;
274
275         switch (event) {
276         case AA_MOUSE: {
277             int x, y;
278             int button;
279             int vlc;
280             aa_getmouse(sys->aa_context, &x, &y, &button);
281
282             vlc = 0;
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;
289
290             vout_display_SendEventMouseState(vd, x, y, vlc);
291
292             aa_showcursor(sys->aa_context); /* Not perfect, we show it on click too */
293             break;
294         }
295
296         case AA_RESIZE:
297             aa_resize(sys->aa_context);
298             vout_display_SendEventDisplaySize(vd,
299                                               aa_imgwidth(sys->aa_context),
300                                               aa_imgheight(sys->aa_context), false);
301             break;
302
303         /* TODO keys support to complete */
304         case AA_UP:
305             vout_display_SendEventKey(vd, KEY_UP);
306             break;
307         case AA_DOWN:
308             vout_display_SendEventKey(vd, KEY_DOWN);
309             break;
310         case AA_RIGHT:
311             vout_display_SendEventKey(vd, KEY_RIGHT);
312             break;
313         case AA_LEFT:
314             vout_display_SendEventKey(vd, KEY_LEFT);
315             break;
316         case AA_BACKSPACE:
317             vout_display_SendEventKey(vd, KEY_BACKSPACE);
318             break;
319         case AA_ESC:
320             vout_display_SendEventKey(vd, KEY_ESC);
321             break;
322         default:
323             if (event >= 0x20 && event <= 0x7f)
324                 vout_display_SendEventKey(vd, event);
325             break;
326         }
327     }
328 }
329