]> git.sesse.net Git - vlc/blob - modules/video_output/aa.c
Force the X11 video outputs when libvlc_*_set_xwindow() is used
[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_pool_t *Pool   (vout_display_t *, unsigned);
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     vout_display_DeleteWindow(vd, NULL);
101
102     aa_autoinitkbd(sys->aa_context, 0);
103     aa_autoinitmouse(sys->aa_context, AA_MOUSEALLMASK);
104
105     /* */
106     video_format_t fmt = vd->fmt;
107     fmt.i_chroma = VLC_CODEC_RGB8;
108     fmt.i_width  = aa_imgwidth(sys->aa_context);
109     fmt.i_height = aa_imgheight(sys->aa_context);
110
111     /* */
112     vout_display_info_t info = vd->info;
113     info.has_pictures_invalid = true;
114
115     /* Setup vout_display now that everything is fine */
116     vd->fmt = fmt;
117     vd->info = info;
118
119     vd->pool    = Pool;
120     vd->prepare = Prepare;
121     vd->display = Display;
122     vd->control = Control;
123     vd->manage  = Manage;
124
125     /* Inspect initial configuration and send correction events
126      * FIXME how to handle aspect ratio with aa ? */
127     sys->state = *vd->cfg;
128     sys->state.is_fullscreen = false;
129     vout_display_SendEventFullscreen(vd, false);
130     vout_display_SendEventDisplaySize(vd, fmt.i_width, fmt.i_height, false);
131
132     return VLC_SUCCESS;
133
134 error:
135     if (sys && sys->aa_context)
136         aa_close(sys->aa_context);
137     free(sys);
138     return VLC_EGENERIC;
139 }
140
141 /**
142  * Close a aa video output method
143  */
144 static void Close(vlc_object_t *object)
145 {
146     vout_display_t *vd = (vout_display_t *)object;
147     vout_display_sys_t *sys = vd->sys;
148
149     if (sys->pool)
150         picture_pool_Delete(sys->pool);
151     aa_close(sys->aa_context);
152     free(sys);
153 }
154
155 /**
156  * Return a pool of direct buffers
157  */
158 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
159 {
160     vout_display_sys_t *sys = vd->sys;
161     VLC_UNUSED(count);
162
163     if (!sys->pool) {
164         picture_resource_t rsc;
165
166         memset(&rsc, 0, sizeof(rsc));
167         rsc.p[0].p_pixels = aa_image(sys->aa_context);
168         rsc.p[0].i_pitch = aa_imgwidth(sys->aa_context);
169         rsc.p[0].i_lines = aa_imgheight(sys->aa_context);
170
171         picture_t *p_picture = picture_NewFromResource(&vd->fmt, &rsc);
172         if (!p_picture)
173             return NULL;
174
175         sys->pool = picture_pool_New(1, &p_picture);
176     }
177     return sys->pool;
178 }
179
180 /**
181  * Prepare a picture for display */
182 static void Prepare(vout_display_t *vd, picture_t *picture)
183 {
184     vout_display_sys_t *sys = vd->sys;
185
186     assert(vd->fmt.i_width  == aa_imgwidth(sys->aa_context) &&
187            vd->fmt.i_height == aa_imgheight(sys->aa_context));
188
189 #if 0
190     if (picture->format.p_palette) {
191         for (int i = 0; i < 256; i++) {
192             aa_setpalette(vd->sys->palette, 256 - i,
193                            red[ i ], green[ i ], blue[ i ]);
194         }
195     }
196 #else
197     VLC_UNUSED(picture);
198 #endif
199
200     aa_fastrender(sys->aa_context, 0, 0,
201                   vd->fmt.i_width, vd->fmt.i_height);
202 }
203
204 /**
205  * Display a picture
206  */
207 static void Display(vout_display_t *vd, picture_t *picture)
208 {
209     vout_display_sys_t *sys = vd->sys;
210
211     aa_flush(sys->aa_context);
212     picture_Release(picture);
213 }
214
215 /**
216  * Control for vout display
217  */
218 static int Control(vout_display_t *vd, int query, va_list args)
219 {
220     VLC_UNUSED(args);
221     vout_display_sys_t *sys = vd->sys;
222
223     switch (query) {
224     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
225         /* We have to ignore what is requested */
226         vout_display_SendEventPicturesInvalid(vd);
227         return VLC_SUCCESS;
228
229     case VOUT_DISPLAY_RESET_PICTURES:
230         if (sys->pool)
231             picture_pool_Delete(sys->pool);
232         sys->pool = NULL;
233
234         vd->fmt.i_width  = aa_imgwidth(sys->aa_context);
235         vd->fmt.i_height = aa_imgheight(sys->aa_context);
236         return VLC_SUCCESS;
237
238     case VOUT_DISPLAY_HIDE_MOUSE:
239         aa_hidemouse(sys->aa_context);
240         return VLC_SUCCESS;
241
242     default:
243         msg_Err(vd, "Unsupported query in vout display aalib");
244         return VLC_EGENERIC;
245     }
246 }
247
248
249 /**
250  * Proccess pending event
251  */
252 static void Manage(vout_display_t *vd)
253 {
254     vout_display_sys_t *sys = vd->sys;
255
256     for (;;) {
257         const int event = aa_getevent(sys->aa_context, 0);
258         if (!event)
259             return;
260
261         switch (event) {
262         case AA_MOUSE: {
263             int x, y;
264             int button;
265             int vlc;
266             aa_getmouse(sys->aa_context, &x, &y, &button);
267
268             vlc = 0;
269             if (button & AA_BUTTON1)
270                 vlc |= 1 << MOUSE_BUTTON_LEFT;
271             if (button & AA_BUTTON2)
272                 vlc |= 1 << MOUSE_BUTTON_CENTER;
273             if (button & AA_BUTTON3)
274                 vlc |= 1 << MOUSE_BUTTON_RIGHT;
275
276             vout_display_SendEventMouseState(vd, x, y, vlc);
277
278             aa_showcursor(sys->aa_context); /* Not perfect, we show it on click too */
279             break;
280         }
281
282         case AA_RESIZE:
283             aa_resize(sys->aa_context);
284             vout_display_SendEventDisplaySize(vd,
285                                               aa_imgwidth(sys->aa_context),
286                                               aa_imgheight(sys->aa_context), false);
287             break;
288
289         /* TODO keys support to complete */
290         case AA_UP:
291             vout_display_SendEventKey(vd, KEY_UP);
292             break;
293         case AA_DOWN:
294             vout_display_SendEventKey(vd, KEY_DOWN);
295             break;
296         case AA_RIGHT:
297             vout_display_SendEventKey(vd, KEY_RIGHT);
298             break;
299         case AA_LEFT:
300             vout_display_SendEventKey(vd, KEY_LEFT);
301             break;
302         case AA_BACKSPACE:
303             vout_display_SendEventKey(vd, KEY_BACKSPACE);
304             break;
305         case AA_ESC:
306             vout_display_SendEventKey(vd, KEY_ESC);
307             break;
308         default:
309             if (event >= 0x20 && event <= 0x7f)
310                 vout_display_SendEventKey(vd, event);
311             break;
312         }
313     }
314 }
315