]> git.sesse.net Git - vlc/blob - modules/video_output/caca.c
caca: do not pretend to support resize
[vlc] / modules / video_output / caca.c
1 /*****************************************************************************
2  * caca.c: Color ASCII Art "vout display" module using libcaca
3  *****************************************************************************
4  * Copyright (C) 2003-2009 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Sam Hocevar <sam@zoy.org>
8  *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_vout_display.h>
36 #include <vlc_picture_pool.h>
37 #if !defined(_WIN32) && !defined(__APPLE__)
38 # ifdef X_DISPLAY_MISSING
39 #  error Xlib required due to XInitThreads
40 # endif
41 # include <vlc_xlib.h>
42 #endif
43
44 #include <caca.h>
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 static int  Open (vlc_object_t *);
50 static void Close(vlc_object_t *);
51
52 vlc_module_begin()
53     set_shortname("Caca")
54     set_category(CAT_VIDEO)
55     set_subcategory(SUBCAT_VIDEO_VOUT)
56     set_description(N_("Color ASCII art video output"))
57     set_capability("vout display", 15)
58     set_callbacks(Open, Close)
59 vlc_module_end()
60
61 /*****************************************************************************
62  * Local prototypes
63  *****************************************************************************/
64 static picture_pool_t *Pool  (vout_display_t *, unsigned);
65 static void           Prepare(vout_display_t *, picture_t *, subpicture_t *);
66 static void    PictureDisplay(vout_display_t *, picture_t *, subpicture_t *);
67 static int            Control(vout_display_t *, int, va_list);
68
69 /* */
70 static void Manage(vout_display_t *);
71 static void Refresh(vout_display_t *);
72 static void Place(vout_display_t *, vout_display_place_t *);
73
74 /* */
75 struct vout_display_sys_t {
76     cucul_canvas_t *cv;
77     caca_display_t *dp;
78     cucul_dither_t *dither;
79
80     picture_pool_t *pool;
81 };
82
83 /**
84  * This function initializes libcaca vout method.
85  */
86 static int Open(vlc_object_t *object)
87 {
88     vout_display_t *vd = (vout_display_t *)object;
89     vout_display_sys_t *sys;
90
91     if (vout_display_IsWindowed(vd))
92         return VLC_EGENERIC;
93 #if !defined(__APPLE__) && !defined(_WIN32)
94 # ifndef X_DISPLAY_MISSING
95     if (!vlc_xlib_init(object))
96         return VLC_EGENERIC;
97 # endif
98 #endif
99
100 #if defined(_WIN32)
101     CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
102     SMALL_RECT rect;
103     COORD coord;
104     HANDLE hstdout;
105
106     if (!AllocConsole()) {
107         msg_Err(vd, "cannot create console");
108         return VLC_EGENERIC;
109     }
110
111     hstdout =
112         CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
113                                   FILE_SHARE_READ | FILE_SHARE_WRITE,
114                                   NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
115     if (!hstdout || hstdout == INVALID_HANDLE_VALUE) {
116         msg_Err(vd, "cannot create screen buffer");
117         FreeConsole();
118         return VLC_EGENERIC;
119     }
120
121     if (!SetConsoleActiveScreenBuffer(hstdout)) {
122         msg_Err(vd, "cannot set active screen buffer");
123         FreeConsole();
124         return VLC_EGENERIC;
125     }
126
127     coord = GetLargestConsoleWindowSize(hstdout);
128     msg_Dbg(vd, "SetConsoleWindowInfo: %ix%i", coord.X, coord.Y);
129
130     /* Force size for now */
131     coord.X = 100;
132     coord.Y = 40;
133
134     if (!SetConsoleScreenBufferSize(hstdout, coord))
135         msg_Warn(vd, "SetConsoleScreenBufferSize %i %i",
136                   coord.X, coord.Y);
137
138     /* Get the current screen buffer size and window position. */
139     if (GetConsoleScreenBufferInfo(hstdout, &csbiInfo)) {
140         rect.Top = 0; rect.Left = 0;
141         rect.Right = csbiInfo.dwMaximumWindowSize.X - 1;
142         rect.Bottom = csbiInfo.dwMaximumWindowSize.Y - 1;
143         if (!SetConsoleWindowInfo(hstdout, TRUE, &rect))
144             msg_Dbg(vd, "SetConsoleWindowInfo failed: %ix%i",
145                      rect.Right, rect.Bottom);
146     }
147 #endif
148
149     /* Allocate structure */
150     vd->sys = sys = calloc(1, sizeof(*sys));
151     if (!sys)
152         goto error;
153
154     sys->cv = cucul_create_canvas(0, 0);
155     if (!sys->cv) {
156         msg_Err(vd, "cannot initialize libcucul");
157         goto error;
158     }
159
160     const char *driver = NULL;
161 #ifdef __APPLE__
162     // Make sure we don't try to open a window.
163     driver = "ncurses";
164 #endif
165
166     sys->dp = caca_create_display_with_driver(sys->cv, driver);
167     if (!sys->dp) {
168         msg_Err(vd, "cannot initialize libcaca");
169         goto error;
170     }
171
172     if (vd->cfg->display.title)
173         caca_set_display_title(sys->dp,
174                                vd->cfg->display.title);
175     else
176         caca_set_display_title(sys->dp,
177                                VOUT_TITLE "(Colour AsCii Art)");
178
179     /* Fix format */
180     video_format_t fmt = vd->fmt;
181     if (fmt.i_chroma != VLC_CODEC_RGB32) {
182         fmt.i_chroma = VLC_CODEC_RGB32;
183         fmt.i_rmask = 0x00ff0000;
184         fmt.i_gmask = 0x0000ff00;
185         fmt.i_bmask = 0x000000ff;
186     }
187
188     /* TODO */
189     vout_display_info_t info = vd->info;
190
191     /* Setup vout_display now that everything is fine */
192     vd->fmt = fmt;
193     vd->info = info;
194
195     vd->pool    = Pool;
196     vd->prepare = Prepare;
197     vd->display = PictureDisplay;
198     vd->control = Control;
199     vd->manage  = Manage;
200
201     /* Fix initial state */
202     vout_display_SendEventFullscreen(vd, false);
203     Refresh(vd);
204
205     return VLC_SUCCESS;
206
207 error:
208     if (sys) {
209         if (sys->pool)
210             picture_pool_Delete(sys->pool);
211         if (sys->dither)
212             cucul_free_dither(sys->dither);
213         if (sys->dp)
214             caca_free_display(sys->dp);
215         if (sys->cv)
216             cucul_free_canvas(sys->cv);
217
218         free(sys);
219     }
220 #if defined(_WIN32)
221     FreeConsole();
222 #endif
223     return VLC_EGENERIC;
224 }
225
226 /**
227  * Close a libcaca video output
228  */
229 static void Close(vlc_object_t *object)
230 {
231     vout_display_t *vd = (vout_display_t *)object;
232     vout_display_sys_t *sys = vd->sys;
233
234     if (sys->pool)
235         picture_pool_Delete(sys->pool);
236     if (sys->dither)
237         cucul_free_dither(sys->dither);
238     caca_free_display(sys->dp);
239     cucul_free_canvas(sys->cv);
240
241 #if defined(_WIN32)
242     FreeConsole();
243 #endif
244
245     free(sys);
246 }
247
248 /**
249  * Return a pool of direct buffers
250  */
251 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
252 {
253     vout_display_sys_t *sys = vd->sys;
254
255     if (!sys->pool)
256         sys->pool = picture_pool_NewFromFormat(&vd->fmt, count);
257     return sys->pool;
258 }
259
260 /**
261  * Prepare a picture for display */
262 static void Prepare(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
263 {
264     vout_display_sys_t *sys = vd->sys;
265
266     if (!sys->dither) {
267         /* Create the libcaca dither object */
268         sys->dither = cucul_create_dither(32,
269                                             vd->source.i_visible_width,
270                                             vd->source.i_visible_height,
271                                             picture->p[0].i_pitch,
272                                             vd->fmt.i_rmask,
273                                             vd->fmt.i_gmask,
274                                             vd->fmt.i_bmask,
275                                             0x00000000);
276
277         if (!sys->dither) {
278             msg_Err(vd, "could not create libcaca dither object");
279             return;
280         }
281     }
282
283     vout_display_place_t place;
284     Place(vd, &place);
285
286     cucul_set_color_ansi(sys->cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_BLACK);
287     cucul_clear_canvas(sys->cv);
288
289     const int crop_offset = vd->source.i_y_offset * picture->p->i_pitch +
290                             vd->source.i_x_offset * picture->p->i_pixel_pitch;
291     cucul_dither_bitmap(sys->cv, place.x, place.y,
292                         place.width, place.height,
293                         sys->dither,
294                         &picture->p->p_pixels[crop_offset]);
295     VLC_UNUSED(subpicture);
296 }
297
298 /**
299  * Display a picture
300  */
301 static void PictureDisplay(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
302 {
303     Refresh(vd);
304     picture_Release(picture);
305     VLC_UNUSED(subpicture);
306 }
307
308 /**
309  * Control for vout display
310  */
311 static int Control(vout_display_t *vd, int query, va_list args)
312 {
313     vout_display_sys_t *sys = vd->sys;
314
315     (void) args;
316
317     switch (query) {
318     case VOUT_DISPLAY_HIDE_MOUSE:
319         caca_set_mouse(sys->dp, 0);
320         return VLC_SUCCESS;
321
322     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
323     case VOUT_DISPLAY_CHANGE_ZOOM:
324     case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
325     case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
326         return VLC_EGENERIC;
327
328     case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
329         if (sys->dither)
330             cucul_free_dither(sys->dither);
331         sys->dither = NULL;
332         return VLC_SUCCESS;
333
334     default:
335         msg_Err(vd, "Unsupported query in vout display caca");
336         return VLC_EGENERIC;
337     }
338 }
339
340 /**
341  * Refresh the display and send resize event
342  */
343 static void Refresh(vout_display_t *vd)
344 {
345     vout_display_sys_t *sys = vd->sys;
346
347     /* */
348     caca_refresh_display(sys->dp);
349
350     /* */
351     const unsigned width  = caca_get_display_width(sys->dp);
352     const unsigned height = caca_get_display_height(sys->dp);
353
354     if (width  != vd->cfg->display.width ||
355         height != vd->cfg->display.height)
356         vout_display_SendEventDisplaySize(vd, width, height);
357 }
358
359 /**
360  * Compute the place in canvas unit.
361  */
362 static void Place(vout_display_t *vd, vout_display_place_t *place)
363 {
364     vout_display_sys_t *sys = vd->sys;
365
366     vout_display_PlacePicture(place, &vd->source, vd->cfg, false);
367
368     const int canvas_width   = cucul_get_canvas_width(sys->cv);
369     const int canvas_height  = cucul_get_canvas_height(sys->cv);
370     const int display_width  = caca_get_display_width(sys->dp);
371     const int display_height = caca_get_display_height(sys->dp);
372
373     if (display_width > 0 && display_height > 0) {
374         place->x      =  place->x      * canvas_width  / display_width;
375         place->y      =  place->y      * canvas_height / display_height;
376         place->width  = (place->width  * canvas_width  + display_width/2)  / display_width;
377         place->height = (place->height * canvas_height + display_height/2) / display_height;
378     } else {
379         place->x = 0;
380         place->y = 0;
381         place->width  = canvas_width;
382         place->height = display_height;
383     }
384 }
385
386 /* */
387 static const struct {
388     int caca;
389     int vlc;
390 } keys[] = {
391
392     { CACA_KEY_CTRL_A,  KEY_MODIFIER_CTRL | 'a' },
393     { CACA_KEY_CTRL_B,  KEY_MODIFIER_CTRL | 'b' },
394     { CACA_KEY_CTRL_C,  KEY_MODIFIER_CTRL | 'c' },
395     { CACA_KEY_CTRL_D,  KEY_MODIFIER_CTRL | 'd' },
396     { CACA_KEY_CTRL_E,  KEY_MODIFIER_CTRL | 'e' },
397     { CACA_KEY_CTRL_F,  KEY_MODIFIER_CTRL | 'f' },
398     { CACA_KEY_CTRL_G,  KEY_MODIFIER_CTRL | 'g' },
399     { CACA_KEY_BACKSPACE, KEY_BACKSPACE },
400     { CACA_KEY_TAB,     KEY_TAB },
401     { CACA_KEY_CTRL_J,  KEY_MODIFIER_CTRL | 'j' },
402     { CACA_KEY_CTRL_K,  KEY_MODIFIER_CTRL | 'k' },
403     { CACA_KEY_CTRL_L,  KEY_MODIFIER_CTRL | 'l' },
404     { CACA_KEY_RETURN,  KEY_ENTER },
405
406     { CACA_KEY_CTRL_N,  KEY_MODIFIER_CTRL | 'n' },
407     { CACA_KEY_CTRL_O,  KEY_MODIFIER_CTRL | 'o' },
408     { CACA_KEY_CTRL_P,  KEY_MODIFIER_CTRL | 'p' },
409     { CACA_KEY_CTRL_Q,  KEY_MODIFIER_CTRL | 'q' },
410     { CACA_KEY_CTRL_R,  KEY_MODIFIER_CTRL | 'r' },
411
412     { CACA_KEY_PAUSE,   -1 },
413     { CACA_KEY_CTRL_T,  KEY_MODIFIER_CTRL | 't' },
414     { CACA_KEY_CTRL_U,  KEY_MODIFIER_CTRL | 'u' },
415     { CACA_KEY_CTRL_V,  KEY_MODIFIER_CTRL | 'v' },
416     { CACA_KEY_CTRL_W,  KEY_MODIFIER_CTRL | 'w' },
417     { CACA_KEY_CTRL_X,  KEY_MODIFIER_CTRL | 'x' },
418     { CACA_KEY_CTRL_Y,  KEY_MODIFIER_CTRL | 'y' },
419     { CACA_KEY_CTRL_Z,  KEY_MODIFIER_CTRL | 'z' },
420
421     { CACA_KEY_ESCAPE,  KEY_ESC },
422     { CACA_KEY_DELETE,  KEY_DELETE },
423
424     { CACA_KEY_F1,      KEY_F1 },
425     { CACA_KEY_F2,      KEY_F2 },
426     { CACA_KEY_F3,      KEY_F3 },
427     { CACA_KEY_F4,      KEY_F4 },
428     { CACA_KEY_F5,      KEY_F5 },
429     { CACA_KEY_F6,      KEY_F6 },
430     { CACA_KEY_F7,      KEY_F7 },
431     { CACA_KEY_F8,      KEY_F8 },
432     { CACA_KEY_F9,      KEY_F9 },
433     { CACA_KEY_F10,     KEY_F10 },
434     { CACA_KEY_F11,     KEY_F11 },
435     { CACA_KEY_F12,     KEY_F12 },
436     { CACA_KEY_F13,     -1 },
437     { CACA_KEY_F14,     -1 },
438     { CACA_KEY_F15,     -1 },
439
440     { CACA_KEY_UP,      KEY_UP },
441     { CACA_KEY_DOWN,    KEY_DOWN },
442     { CACA_KEY_LEFT,    KEY_LEFT },
443     { CACA_KEY_RIGHT,   KEY_RIGHT },
444
445     { CACA_KEY_INSERT,  KEY_INSERT },
446     { CACA_KEY_HOME,    KEY_HOME },
447     { CACA_KEY_END,     KEY_END },
448     { CACA_KEY_PAGEUP,  KEY_PAGEUP },
449     { CACA_KEY_PAGEDOWN,KEY_PAGEDOWN },
450
451     /* */
452     { -1, -1 }
453 };
454
455 static const struct {
456     int caca;
457     int vlc;
458 } mouses[] = {
459     { 1, MOUSE_BUTTON_LEFT },
460     { 2, MOUSE_BUTTON_CENTER },
461     { 3, MOUSE_BUTTON_RIGHT },
462     { 4, MOUSE_BUTTON_WHEEL_UP },
463     { 5, MOUSE_BUTTON_WHEEL_DOWN },
464
465     /* */
466     { -1, -1 }
467 };
468
469 /**
470  * Proccess pending event
471  */
472 static void Manage(vout_display_t *vd)
473 {
474     vout_display_sys_t *sys = vd->sys;
475
476     struct caca_event ev;
477     while (caca_get_event(sys->dp, CACA_EVENT_ANY, &ev, 0) > 0) {
478         switch (caca_get_event_type(&ev)) {
479         case CACA_EVENT_KEY_PRESS: {
480             const int caca = caca_get_event_key_ch(&ev);
481
482             for (int i = 0; keys[i].caca != -1; i++) {
483                 if (keys[i].caca == caca) {
484                     const int vlc = keys[i].vlc;
485
486                     if (vlc >= 0)
487                         vout_display_SendEventKey(vd, vlc);
488                     return;
489                 }
490             }
491             if (caca >= 0x20 && caca <= 0x7f) {
492                 vout_display_SendEventKey(vd, caca);
493                 return;
494             }
495             break;
496         }
497         case CACA_EVENT_RESIZE:
498             vout_display_SendEventDisplaySize(vd, caca_get_event_resize_width(&ev),
499                                                   caca_get_event_resize_height(&ev));
500             break;
501         case CACA_EVENT_MOUSE_MOTION: {
502             vout_display_place_t place;
503             Place(vd, &place);
504
505             const unsigned x = vd->source.i_x_offset +
506                                (int64_t)(caca_get_event_mouse_x(&ev) - place.x) *
507                                     vd->source.i_visible_width / place.width;
508             const unsigned y = vd->source.i_y_offset +
509                                (int64_t)(caca_get_event_mouse_y(&ev) - place.y) *
510                                     vd->source.i_visible_height / place.height;
511
512             caca_set_mouse(sys->dp, 1);
513             vout_display_SendEventMouseMoved(vd, x, y);
514             break;
515         }
516         case CACA_EVENT_MOUSE_PRESS:
517         case CACA_EVENT_MOUSE_RELEASE: {
518             caca_set_mouse(sys->dp, 1);
519             const int caca = caca_get_event_mouse_button(&ev);
520             for (int i = 0; mouses[i].caca != -1; i++) {
521                 if (mouses[i].caca == caca) {
522                     if (caca_get_event_type(&ev) == CACA_EVENT_MOUSE_PRESS)
523                         vout_display_SendEventMousePressed(vd, mouses[i].vlc);
524                     else
525                         vout_display_SendEventMouseReleased(vd, mouses[i].vlc);
526                     return;
527                 }
528             }
529             break;
530         }
531         case CACA_EVENT_QUIT:
532             vout_display_SendEventClose(vd);
533             break;
534         default:
535             break;
536         }
537     }
538 }
539