contrib: move macOS compilation flags to the script where they belong and generalize...
[vlc.git] / 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 #include "event_thread.h"
47
48 /* TODO
49  * - what about RGB palette ?
50  */
51 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54 static int  Open (vlc_object_t *);
55 static void Close(vlc_object_t *);
56
57 vlc_module_begin()
58     set_shortname(N_("ASCII Art"))
59     set_category(CAT_VIDEO)
60     set_subcategory(SUBCAT_VIDEO_VOUT)
61     set_description(N_("ASCII-art video output"))
62     set_capability("vout display", /*10*/0)
63     add_shortcut("aalib")
64     set_callbacks(Open, Close)
65 vlc_module_end()
66
67 /*****************************************************************************
68  * Local prototypes
69  *****************************************************************************/
70 static picture_pool_t *Pool   (vout_display_t *, unsigned);
71 static void            Prepare(vout_display_t *, picture_t *, subpicture_t *);
72 static void            PictureDisplay(vout_display_t *, picture_t *, subpicture_t *);
73 static int             Control(vout_display_t *, int, va_list);
74
75 /* */
76 static void Manage(vout_display_t *);
77
78 /* */
79 struct vout_display_sys_t {
80     struct aa_context*  aa_context;
81     aa_palette          palette;
82
83     picture_pool_t      *pool;
84     vout_display_event_thread_t *et;
85 };
86
87 /**
88  * This function allocates and initializes a aa vout method.
89  */
90 static int Open(vlc_object_t *object)
91 {
92     vout_display_t *vd = (vout_display_t *)object;
93     vout_display_sys_t *sys;
94
95 #ifndef _WIN32
96     if (!vlc_xlib_init (object))
97         return VLC_EGENERIC;
98 #endif
99
100     /* Allocate structure */
101     vd->sys = sys = calloc(1, sizeof(*sys));
102     if (!sys)
103         return VLC_ENOMEM;
104
105     /* Don't parse any options, but take $AAOPTS into account */
106     aa_parseoptions(NULL, NULL, NULL, NULL);
107
108     /* */
109     sys->aa_context = aa_autoinit(&aa_defparams);
110     if (!sys->aa_context) {
111         msg_Err(vd, "cannot initialize aalib");
112         goto error;
113     }
114     vout_display_DeleteWindow(vd, NULL);
115
116     sys->et = VoutDisplayEventCreateThread(vd);
117
118     aa_autoinitkbd(sys->aa_context, 0);
119     aa_autoinitmouse(sys->aa_context, AA_MOUSEALLMASK);
120
121     /* */
122     video_format_t fmt = vd->fmt;
123     fmt.i_chroma = VLC_CODEC_RGB8;
124     fmt.i_width  = aa_imgwidth(sys->aa_context);
125     fmt.i_height = aa_imgheight(sys->aa_context);
126     fmt.i_visible_width = fmt.i_width;
127     fmt.i_visible_height = fmt.i_height;
128
129     /* Setup vout_display now that everything is fine */
130     vd->fmt = fmt;
131     vd->info.has_pictures_invalid = true;
132     vd->info.needs_hide_mouse = true;
133
134     vd->pool    = Pool;
135     vd->prepare = Prepare;
136     vd->display = PictureDisplay;
137     vd->control = Control;
138     vd->manage  = Manage;
139
140     /* Inspect initial configuration and send correction events
141      * FIXME how to handle aspect ratio with aa ? */
142     vout_display_SendEventDisplaySize(vd, fmt.i_width, fmt.i_height);
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_Release(sys->pool);
163     VoutDisplayEventKillThread(sys->et);
164     aa_close(sys->aa_context);
165     free(sys);
166 }
167
168 /**
169  * Return a pool of direct buffers
170  */
171 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
172 {
173     vout_display_sys_t *sys = vd->sys;
174     VLC_UNUSED(count);
175
176     if (!sys->pool) {
177         picture_resource_t rsc;
178
179         memset(&rsc, 0, sizeof(rsc));
180         rsc.p[0].p_pixels = aa_image(sys->aa_context);
181         rsc.p[0].i_pitch = aa_imgwidth(sys->aa_context);
182         rsc.p[0].i_lines = aa_imgheight(sys->aa_context);
183
184         picture_t *p_picture = picture_NewFromResource(&vd->fmt, &rsc);
185         if (!p_picture)
186             return NULL;
187
188         sys->pool = picture_pool_New(1, &p_picture);
189     }
190     return sys->pool;
191 }
192
193 /**
194  * Prepare a picture for display */
195 static void Prepare(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
196 {
197     vout_display_sys_t *sys = vd->sys;
198
199     assert(vd->fmt.i_width  == aa_imgwidth(sys->aa_context) &&
200            vd->fmt.i_height == aa_imgheight(sys->aa_context));
201
202 #if 0
203     if (picture->format.p_palette) {
204         for (int i = 0; i < 256; i++) {
205             aa_setpalette(vd->sys->palette, 256 - i,
206                            red[ i ], green[ i ], blue[ i ]);
207         }
208     }
209 #else
210     VLC_UNUSED(picture);
211 #endif
212     VLC_UNUSED(subpicture);
213
214     aa_fastrender(sys->aa_context, 0, 0,
215                   vd->fmt.i_width, vd->fmt.i_height);
216 }
217
218 /**
219  * Display a picture
220  */
221 static void PictureDisplay(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
222 {
223     vout_display_sys_t *sys = vd->sys;
224
225     aa_flush(sys->aa_context);
226     picture_Release(picture);
227     VLC_UNUSED(subpicture);
228 }
229
230 /**
231  * Control for vout display
232  */
233 static int Control(vout_display_t *vd, int query, va_list args)
234 {
235     VLC_UNUSED(args);
236     vout_display_sys_t *sys = vd->sys;
237
238     switch (query) {
239     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
240         /* We have to ignore what is requested */
241         vout_display_SendEventPicturesInvalid(vd);
242         return VLC_SUCCESS;
243
244     case VOUT_DISPLAY_RESET_PICTURES:
245         if (sys->pool)
246             picture_pool_Release(sys->pool);
247         sys->pool = NULL;
248
249         vd->fmt.i_width  = aa_imgwidth(sys->aa_context);
250         vd->fmt.i_height = aa_imgheight(sys->aa_context);
251         return VLC_SUCCESS;
252
253     case VOUT_DISPLAY_HIDE_MOUSE:
254         aa_hidemouse(sys->aa_context);
255         return VLC_SUCCESS;
256
257     default:
258         msg_Err(vd, "Unsupported query in vout display aalib");
259         return VLC_EGENERIC;
260     }
261 }
262
263
264 /**
265  * Proccess pending event
266  */
267 static void Manage(vout_display_t *vd)
268 {
269     vout_display_sys_t *sys = vd->sys;
270
271     for (;;) {
272         const int event = aa_getevent(sys->aa_context, 0);
273         if (!event)
274             return;
275
276         switch (event) {
277         case AA_MOUSE: {
278             int x, y;
279             int button;
280             int vlc;
281             aa_getmouse(sys->aa_context, &x, &y, &button);
282
283             vlc = 0;
284             if (button & AA_BUTTON1)
285                 vlc |= 1 << MOUSE_BUTTON_LEFT;
286             if (button & AA_BUTTON2)
287                 vlc |= 1 << MOUSE_BUTTON_CENTER;
288             if (button & AA_BUTTON3)
289                 vlc |= 1 << MOUSE_BUTTON_RIGHT;
290
291             vout_display_SendEventMouseState(vd, x, y, vlc);
292
293             aa_showcursor(sys->aa_context); /* Not perfect, we show it on click too */
294             break;
295         }
296
297         case AA_RESIZE:
298             aa_resize(sys->aa_context);
299             vout_display_SendEventDisplaySize(vd,
300                                               aa_imgwidth(sys->aa_context),
301                                               aa_imgheight(sys->aa_context));
302             break;
303
304         /* TODO keys support to complete */
305         case AA_UP:
306             vout_display_SendEventKey(vd, KEY_UP);
307             break;
308         case AA_DOWN:
309             vout_display_SendEventKey(vd, KEY_DOWN);
310             break;
311         case AA_RIGHT:
312             vout_display_SendEventKey(vd, KEY_RIGHT);
313             break;
314         case AA_LEFT:
315             vout_display_SendEventKey(vd, KEY_LEFT);
316             break;
317         case AA_BACKSPACE:
318             vout_display_SendEventKey(vd, KEY_BACKSPACE);
319             break;
320         case AA_ESC:
321             vout_display_SendEventKey(vd, KEY_ESC);
322             break;
323         default:
324             if (event >= 0x20 && event <= 0x7f)
325                 vout_display_SendEventKey(vd, event);
326             break;
327         }
328     }
329 }
330