LGPL
[vlc.git] / src / video_output / display.c
1 /*****************************************************************************
2  * display.c: "vout display" managment
3  *****************************************************************************
4  * Copyright (C) 2009 Laurent Aimar
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ 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 #include <assert.h>
31
32 #include <vlc_common.h>
33 #include <vlc_video_splitter.h>
34 #include <vlc_vout_display.h>
35 #include <vlc_vout.h>
36 #include <vlc_block.h>
37 #include <vlc_modules.h>
38
39 #include <libvlc.h>
40
41 #include "display.h"
42
43 #include "event.h"
44
45 /* It must be present as long as a vout_display_t must be created using a dummy
46  * vout (as an opengl provider) */
47 #define ALLOW_DUMMY_VOUT
48
49 static void SplitterClose(vout_display_t *vd);
50
51 /*****************************************************************************
52  * FIXME/TODO see how to have direct rendering here (interact with vout.c)
53  *****************************************************************************/
54 static picture_t *VideoBufferNew(filter_t *filter)
55 {
56     vout_display_t *vd = (vout_display_t*)filter->p_owner;
57     const video_format_t *fmt = &filter->fmt_out.video;
58
59     assert(vd->fmt.i_chroma == fmt->i_chroma &&
60            vd->fmt.i_width  == fmt->i_width  &&
61            vd->fmt.i_height == fmt->i_height);
62
63     picture_pool_t *pool = vout_display_Pool(vd, 3);
64     if (!pool)
65         return NULL;
66     return picture_pool_Get(pool);
67 }
68 static void VideoBufferDelete(filter_t *filter, picture_t *picture)
69 {
70     VLC_UNUSED(filter);
71     picture_Release(picture);
72 }
73
74 static int  FilterAllocationInit(filter_t *filter, void *vd)
75 {
76     filter->pf_video_buffer_new = VideoBufferNew;
77     filter->pf_video_buffer_del = VideoBufferDelete;
78     filter->p_owner             = vd;
79
80     return VLC_SUCCESS;
81 }
82 static void FilterAllocationClean(filter_t *filter)
83 {
84     filter->pf_video_buffer_new = NULL;
85     filter->pf_video_buffer_del = NULL;
86     filter->p_owner             = NULL;
87 }
88
89 /*****************************************************************************
90  *
91  *****************************************************************************/
92
93 /**
94  * It creates a new vout_display_t using the given configuration.
95  */
96 static vout_display_t *vout_display_New(vlc_object_t *obj,
97                                         const char *module, bool load_module,
98                                         const video_format_t *fmt,
99                                         const vout_display_cfg_t *cfg,
100                                         vout_display_owner_t *owner)
101 {
102     /* */
103     vout_display_t *vd = vlc_custom_create(obj, sizeof(*vd), "vout display" );
104
105     /* */
106     video_format_Copy(&vd->source, fmt);
107
108     /* Picture buffer does not have the concept of aspect ratio */
109     video_format_Copy(&vd->fmt, fmt);
110     vd->fmt.i_sar_num = 0;
111     vd->fmt.i_sar_den = 0;
112
113     vd->info.is_slow = false;
114     vd->info.has_double_click = false;
115     vd->info.has_hide_mouse = false;
116     vd->info.has_pictures_invalid = false;
117     vd->info.has_event_thread = false;
118     vd->info.subpicture_chromas = NULL;
119
120     vd->cfg = cfg;
121     vd->pool = NULL;
122     vd->prepare = NULL;
123     vd->display = NULL;
124     vd->control = NULL;
125     vd->manage = NULL;
126     vd->sys = NULL;
127
128     vd->owner = *owner;
129
130     if (load_module) {
131         vd->module = module_need(vd, "vout display", module, module && *module != '\0');
132         if (!vd->module) {
133             vlc_object_release(vd);
134             return NULL;
135         }
136     } else {
137         vd->module = NULL;
138     }
139     return vd;
140 }
141
142 /**
143  * It deletes a vout_display_t
144  */
145 static void vout_display_Delete(vout_display_t *vd)
146 {
147     if (vd->module)
148         module_unneed(vd, vd->module);
149
150     vlc_object_release(vd);
151 }
152
153 /**
154  * It controls a vout_display_t
155  */
156 static int vout_display_Control(vout_display_t *vd, int query, ...)
157 {
158     va_list args;
159     int result;
160
161     va_start(args, query);
162     result = vd->control(vd, query, args);
163     va_end(args);
164
165     return result;
166 }
167
168 static void vout_display_Manage(vout_display_t *vd)
169 {
170     if (vd->manage)
171         vd->manage(vd);
172 }
173
174 /* */
175 void vout_display_GetDefaultDisplaySize(unsigned *width, unsigned *height,
176                                         const video_format_t *source,
177                                         const vout_display_cfg_t *cfg)
178 {
179     if (cfg->display.width > 0 && cfg->display.height > 0) {
180         *width  = cfg->display.width;
181         *height = cfg->display.height;
182     } else if (cfg->display.width > 0) {
183         *width  = cfg->display.width;
184         *height = (int64_t)source->i_visible_height * source->i_sar_den * cfg->display.width * cfg->display.sar.num /
185             source->i_visible_width / source->i_sar_num / cfg->display.sar.den;
186     } else if (cfg->display.height > 0) {
187         *width  = (int64_t)source->i_visible_width * source->i_sar_num * cfg->display.height * cfg->display.sar.den /
188             source->i_visible_height / source->i_sar_den / cfg->display.sar.num;
189         *height = cfg->display.height;
190     } else if (source->i_sar_num >= source->i_sar_den) {
191         *width  = (int64_t)source->i_visible_width * source->i_sar_num * cfg->display.sar.den / source->i_sar_den / cfg->display.sar.num;
192         *height = source->i_visible_height;
193     } else {
194         *width  = source->i_visible_width;
195         *height = (int64_t)source->i_visible_height * source->i_sar_den * cfg->display.sar.num / source->i_sar_num / cfg->display.sar.den;
196     }
197
198     *width  = *width  * cfg->zoom.num / cfg->zoom.den;
199     *height = *height * cfg->zoom.num / cfg->zoom.den;
200 }
201
202 /* */
203 void vout_display_PlacePicture(vout_display_place_t *place,
204                                const video_format_t *source,
205                                const vout_display_cfg_t *cfg,
206                                bool do_clipping)
207 {
208     /* */
209     memset(place, 0, sizeof(*place));
210     if (cfg->display.width <= 0 || cfg->display.height <= 0)
211         return;
212
213     /* */
214     unsigned display_width;
215     unsigned display_height;
216
217     if (cfg->is_display_filled) {
218         display_width  = cfg->display.width;
219         display_height = cfg->display.height;
220     } else {
221         vout_display_cfg_t cfg_tmp = *cfg;
222
223         cfg_tmp.display.width  = 0;
224         cfg_tmp.display.height = 0;
225         vout_display_GetDefaultDisplaySize(&display_width, &display_height,
226                                            source, &cfg_tmp);
227
228         if (do_clipping) {
229             display_width  = __MIN(display_width,  cfg->display.width);
230             display_height = __MIN(display_height, cfg->display.height);
231         }
232     }
233
234     const unsigned width  = source->i_visible_width;
235     const unsigned height = source->i_visible_height;
236     /* Compute the height if we use the width to fill up display_width */
237     const int64_t scaled_height = (int64_t)height * display_width  * cfg->display.sar.num * source->i_sar_den / width  / source->i_sar_num / cfg->display.sar.den;
238     /* And the same but switching width/height */
239     const int64_t scaled_width  = (int64_t)width  * display_height * cfg->display.sar.den * source->i_sar_num / height / source->i_sar_den / cfg->display.sar.num;
240
241     /* We keep the solution that avoid filling outside the display */
242     if (scaled_width <= cfg->display.width) {
243         place->width  = scaled_width;
244         place->height = display_height;
245     } else {
246         place->width  = display_width;
247         place->height = scaled_height;
248     }
249
250     /*  Compute position */
251     switch (cfg->align.horizontal) {
252     case VOUT_DISPLAY_ALIGN_LEFT:
253         place->x = 0;
254         break;
255     case VOUT_DISPLAY_ALIGN_RIGHT:
256         place->x = cfg->display.width - place->width;
257         break;
258     default:
259         place->x = ((int)cfg->display.width - (int)place->width) / 2;
260         break;
261     }
262
263     switch (cfg->align.vertical) {
264     case VOUT_DISPLAY_ALIGN_TOP:
265         place->y = 0;
266         break;
267     case VOUT_DISPLAY_ALIGN_BOTTOM:
268         place->y = cfg->display.height - place->height;
269         break;
270     default:
271         place->y = ((int)cfg->display.height - (int)place->height) / 2;
272         break;
273     }
274 }
275
276 struct vout_display_owner_sys_t {
277     vout_thread_t   *vout;
278     bool            is_wrapper;  /* Is the current display a wrapper */
279     vout_display_t  *wrapper; /* Vout display wrapper */
280
281     /* */
282     vout_display_cfg_t cfg;
283     unsigned     wm_state_initial;
284     struct {
285         unsigned num;
286         unsigned den;
287     } sar_initial;
288
289     /* */
290     unsigned width_saved;
291     unsigned height_saved;
292
293     struct {
294         unsigned num;
295         unsigned den;
296     } crop_saved;
297
298     /* */
299     bool ch_display_filled;
300     bool is_display_filled;
301
302     bool ch_zoom;
303     struct {
304         int  num;
305         int  den;
306     } zoom;
307
308     bool ch_wm_state;
309     unsigned wm_state;
310
311     bool ch_sar;
312     struct {
313         unsigned num;
314         unsigned den;
315     } sar;
316
317     bool ch_crop;
318     struct {
319         int      left;
320         int      top;
321         int      right;
322         int      bottom;
323         unsigned num;
324         unsigned den;
325     } crop;
326
327     /* */
328     video_format_t source;
329     filter_chain_t *filters;
330
331     /* Lock protecting the variables used by
332      * VoutDisplayEvent(ie vout_display_SendEvent) */
333     vlc_mutex_t lock;
334
335     /* mouse state */
336     struct {
337         vlc_mouse_t state;
338
339         mtime_t last_pressed;
340         mtime_t last_moved;
341         bool    is_hidden;
342         bool    ch_activity;
343
344         /* */
345         mtime_t double_click_timeout;
346         mtime_t hide_timeout;
347     } mouse;
348
349     bool reset_pictures;
350
351     bool ch_fullscreen;
352     bool is_fullscreen;
353
354     bool ch_display_size;
355     int  display_width;
356     int  display_height;
357     bool display_is_fullscreen;
358     bool display_is_forced;
359
360     int  fit_window;
361
362     struct {
363         vlc_thread_t thread;
364         block_fifo_t *fifo;
365     } event;
366
367 #ifdef ALLOW_DUMMY_VOUT
368     vlc_mouse_t vout_mouse;
369 #endif
370 };
371
372 static void DummyVoutSendDisplayEventMouse(vout_thread_t *, vlc_mouse_t *fallback, const vlc_mouse_t *m);
373
374 static void VoutDisplayCreateRender(vout_display_t *vd)
375 {
376     vout_display_owner_sys_t *osys = vd->owner.sys;
377
378     osys->filters = NULL;
379
380     video_format_t v_src = vd->source;
381     v_src.i_sar_num = 0;
382     v_src.i_sar_den = 0;
383
384     video_format_t v_dst = vd->fmt;
385     v_dst.i_sar_num = 0;
386     v_dst.i_sar_den = 0;
387
388     video_format_t v_dst_cmp = v_dst;
389     if ((v_src.i_chroma == VLC_CODEC_J420 && v_dst.i_chroma == VLC_CODEC_I420) ||
390         (v_src.i_chroma == VLC_CODEC_J422 && v_dst.i_chroma == VLC_CODEC_I422) ||
391         (v_src.i_chroma == VLC_CODEC_J440 && v_dst.i_chroma == VLC_CODEC_I440) ||
392         (v_src.i_chroma == VLC_CODEC_J444 && v_dst.i_chroma == VLC_CODEC_I444))
393         v_dst_cmp.i_chroma = v_src.i_chroma;
394
395     const bool convert = memcmp(&v_src, &v_dst_cmp, sizeof(v_src)) != 0;
396     if (!convert)
397         return;
398
399     msg_Dbg(vd, "A filter to adapt decoder to display is needed");
400
401     osys->filters = filter_chain_New(vd, "video filter2", false,
402                                      FilterAllocationInit,
403                                      FilterAllocationClean, vd);
404     assert(osys->filters); /* TODO critical */
405
406     /* */
407     es_format_t src;
408     es_format_InitFromVideo(&src, &v_src);
409
410     /* */
411     es_format_t dst;
412
413     filter_t *filter;
414     for (int i = 0; i < 1 + (v_dst_cmp.i_chroma != v_dst.i_chroma); i++) {
415
416         es_format_InitFromVideo(&dst, i == 0 ? &v_dst : &v_dst_cmp);
417
418         filter_chain_Reset(osys->filters, &src, &dst);
419         filter = filter_chain_AppendFilter(osys->filters,
420                                            NULL, NULL, &src, &dst);
421         if (filter)
422             break;
423     }
424     if (!filter)
425         msg_Err(vd, "Failed to adapt decoder format to display");
426 }
427
428 static void VoutDisplayDestroyRender(vout_display_t *vd)
429 {
430     vout_display_owner_sys_t *osys = vd->owner.sys;
431
432     if (osys->filters)
433         filter_chain_Delete(osys->filters);
434 }
435
436 static void VoutDisplayResetRender(vout_display_t *vd)
437 {
438     VoutDisplayDestroyRender(vd);
439     VoutDisplayCreateRender(vd);
440 }
441 static void VoutDisplayEventMouse(vout_display_t *vd, int event, va_list args)
442 {
443     vout_display_owner_sys_t *osys = vd->owner.sys;
444
445     vlc_mutex_lock(&osys->lock);
446
447     /* */
448     vlc_mouse_t m = osys->mouse.state;
449     bool is_ignored = false;
450
451     switch (event) {
452     case VOUT_DISPLAY_EVENT_MOUSE_STATE: {
453         const int x = (int)va_arg(args, int);
454         const int y = (int)va_arg(args, int);
455         const int button_mask = (int)va_arg(args, int);
456
457         vlc_mouse_Init(&m);
458         m.i_x = x;
459         m.i_y = y;
460         m.i_pressed = button_mask;
461         break;
462     }
463     case VOUT_DISPLAY_EVENT_MOUSE_MOVED: {
464         const int x = (int)va_arg(args, int);
465         const int y = (int)va_arg(args, int);
466
467         //msg_Dbg(vd, "VoutDisplayEvent 'mouse' @%d,%d", x, y);
468
469         m.i_x = x;
470         m.i_y = y;
471         m.b_double_click = false;
472         break;
473     }
474     case VOUT_DISPLAY_EVENT_MOUSE_PRESSED:
475     case VOUT_DISPLAY_EVENT_MOUSE_RELEASED: {
476         const int button = (int)va_arg(args, int);
477         const int button_mask = 1 << button;
478
479         /* Ignore inconsistent event */
480         if ((event == VOUT_DISPLAY_EVENT_MOUSE_PRESSED  &&  (osys->mouse.state.i_pressed & button_mask)) ||
481             (event == VOUT_DISPLAY_EVENT_MOUSE_RELEASED && !(osys->mouse.state.i_pressed & button_mask))) {
482             is_ignored = true;
483             break;
484         }
485
486         /* */
487         msg_Dbg(vd, "VoutDisplayEvent 'mouse button' %d t=%d", button, event);
488
489         m.b_double_click = false;
490         if (event == VOUT_DISPLAY_EVENT_MOUSE_PRESSED)
491             m.i_pressed |= button_mask;
492         else
493             m.i_pressed &= ~button_mask;
494         break;
495     }
496     case VOUT_DISPLAY_EVENT_MOUSE_DOUBLE_CLICK:
497         msg_Dbg(vd, "VoutDisplayEvent 'double click'");
498
499         m.b_double_click = true;
500         break;
501     default:
502         assert(0);
503     }
504
505     if (is_ignored) {
506         vlc_mutex_unlock(&osys->lock);
507         return;
508     }
509
510     /* Emulate double-click if needed */
511     if (!vd->info.has_double_click &&
512         vlc_mouse_HasPressed(&osys->mouse.state, &m, MOUSE_BUTTON_LEFT)) {
513         const mtime_t i_date = mdate();
514
515         if (i_date - osys->mouse.last_pressed < osys->mouse.double_click_timeout ) {
516             m.b_double_click = true;
517             osys->mouse.last_pressed = 0;
518         } else {
519             osys->mouse.last_pressed = mdate();
520         }
521     }
522
523     /* */
524     osys->mouse.state = m;
525
526     /* */
527     osys->mouse.ch_activity = true;
528     if (!vd->info.has_hide_mouse)
529         osys->mouse.last_moved = mdate();
530
531     /* */
532     vout_SendEventMouseVisible(osys->vout);
533 #ifdef ALLOW_DUMMY_VOUT
534     DummyVoutSendDisplayEventMouse(osys->vout, &osys->vout_mouse, &m);
535 #else
536     vout_SendDisplayEventMouse(osys->vout, &m);
537 #endif
538     vlc_mutex_unlock(&osys->lock);
539 }
540
541 VLC_NORETURN
542 static void *VoutDisplayEventKeyDispatch(void *data)
543 {
544     vout_display_owner_sys_t *osys = data;
545
546     for (;;) {
547         block_t *event = block_FifoGet(osys->event.fifo);
548
549         int cancel = vlc_savecancel();
550
551         int key;
552         memcpy(&key, event->p_buffer, sizeof(key));
553         vout_SendEventKey(osys->vout, key);
554         block_Release(event);
555
556         vlc_restorecancel(cancel);
557     }
558 }
559
560 static void VoutDisplayEventKey(vout_display_t *vd, int key)
561 {
562     vout_display_owner_sys_t *osys = vd->owner.sys;
563
564     if (!osys->event.fifo) {
565         osys->event.fifo = block_FifoNew();
566         if (!osys->event.fifo)
567             return;
568         if (vlc_clone(&osys->event.thread, VoutDisplayEventKeyDispatch,
569                       osys, VLC_THREAD_PRIORITY_LOW)) {
570             block_FifoRelease(osys->event.fifo);
571             osys->event.fifo = NULL;
572             return;
573         }
574     }
575     block_t *event = block_Alloc(sizeof(key));
576     if (event) {
577         memcpy(event->p_buffer, &key, sizeof(key));
578         block_FifoPut(osys->event.fifo, event);
579     }
580 }
581
582 static void VoutDisplayEvent(vout_display_t *vd, int event, va_list args)
583 {
584     vout_display_owner_sys_t *osys = vd->owner.sys;
585
586     switch (event) {
587     case VOUT_DISPLAY_EVENT_CLOSE: {
588         msg_Dbg(vd, "VoutDisplayEvent 'close'");
589         vout_SendEventClose(osys->vout);
590         break;
591     }
592     case VOUT_DISPLAY_EVENT_KEY: {
593         const int key = (int)va_arg(args, int);
594         msg_Dbg(vd, "VoutDisplayEvent 'key' 0x%2.2x", key);
595         if (vd->info.has_event_thread)
596             vout_SendEventKey(osys->vout, key);
597         else
598             VoutDisplayEventKey(vd, key);
599         break;
600     }
601     case VOUT_DISPLAY_EVENT_MOUSE_STATE:
602     case VOUT_DISPLAY_EVENT_MOUSE_MOVED:
603     case VOUT_DISPLAY_EVENT_MOUSE_PRESSED:
604     case VOUT_DISPLAY_EVENT_MOUSE_RELEASED:
605     case VOUT_DISPLAY_EVENT_MOUSE_DOUBLE_CLICK:
606         VoutDisplayEventMouse(vd, event, args);
607         break;
608
609     case VOUT_DISPLAY_EVENT_FULLSCREEN: {
610         const int is_fullscreen = (int)va_arg(args, int);
611
612         msg_Dbg(vd, "VoutDisplayEvent 'fullscreen' %d", is_fullscreen);
613
614         vlc_mutex_lock(&osys->lock);
615         if (!is_fullscreen != !osys->is_fullscreen) {
616             osys->ch_fullscreen = true;
617             osys->is_fullscreen = is_fullscreen;
618         }
619         vlc_mutex_unlock(&osys->lock);
620         break;
621     }
622
623     case VOUT_DISPLAY_EVENT_WINDOW_STATE: {
624         const unsigned state = va_arg(args, unsigned);
625
626         msg_Dbg(vd, "VoutDisplayEvent 'window state' %u", state);
627
628         vlc_mutex_lock(&osys->lock);
629         if (state != osys->wm_state) {
630             osys->ch_wm_state = true;
631             osys->wm_state = state;
632         }
633         vlc_mutex_unlock(&osys->lock);
634         break;
635     }
636
637     case VOUT_DISPLAY_EVENT_DISPLAY_SIZE: {
638         const int width  = (int)va_arg(args, int);
639         const int height = (int)va_arg(args, int);
640         const bool is_fullscreen = (bool)va_arg(args, int);
641         msg_Dbg(vd, "VoutDisplayEvent 'resize' %dx%d %s",
642                 width, height, is_fullscreen ? "fullscreen" : "window");
643
644         /* */
645         vlc_mutex_lock(&osys->lock);
646
647         osys->ch_display_size       = true;
648         osys->display_width         = width;
649         osys->display_height        = height;
650         osys->display_is_fullscreen = is_fullscreen;
651         osys->display_is_forced     = false;
652
653         vlc_mutex_unlock(&osys->lock);
654         break;
655     }
656
657     case VOUT_DISPLAY_EVENT_PICTURES_INVALID: {
658         msg_Warn(vd, "VoutDisplayEvent 'pictures invalid'");
659
660         /* */
661         assert(vd->info.has_pictures_invalid);
662
663         vlc_mutex_lock(&osys->lock);
664         osys->reset_pictures = true;
665         vlc_mutex_unlock(&osys->lock);
666         break;
667     }
668     default:
669         msg_Err(vd, "VoutDisplayEvent received event %d", event);
670         /* TODO add an assert when all event are handled */
671         break;
672     }
673 }
674
675 static vout_window_t *VoutDisplayNewWindow(vout_display_t *vd, const vout_window_cfg_t *cfg)
676 {
677     vout_display_owner_sys_t *osys = vd->owner.sys;
678
679 #ifdef ALLOW_DUMMY_VOUT
680     if (!osys->vout->p) {
681         vout_window_cfg_t cfg_override = *cfg;
682
683         if (!var_InheritBool(osys->vout, "embedded-video"))
684             cfg_override.is_standalone = true;
685
686         return vout_window_New(VLC_OBJECT(osys->vout), "$window", &cfg_override);
687     }
688 #endif
689     return vout_NewDisplayWindow(osys->vout, vd, cfg);
690 }
691 static void VoutDisplayDelWindow(vout_display_t *vd, vout_window_t *window)
692 {
693     vout_display_owner_sys_t *osys = vd->owner.sys;
694
695 #ifdef ALLOW_DUMMY_VOUT
696     if (!osys->vout->p) {
697         if( window)
698             vout_window_Delete(window);
699         return;
700     }
701 #endif
702     vout_DeleteDisplayWindow(osys->vout, vd, window);
703 }
704
705 static void VoutDisplayFitWindow(vout_display_t *vd, bool default_size)
706 {
707     vout_display_owner_sys_t *osys = vd->owner.sys;
708     vout_display_cfg_t cfg = osys->cfg;
709
710     if (!cfg.is_display_filled)
711         return;
712
713     cfg.display.width = 0;
714     if (default_size) {
715         cfg.display.height = 0;
716     } else {
717         cfg.display.height = osys->height_saved;
718         cfg.zoom.num = 1;
719         cfg.zoom.den = 1;
720     }
721
722     unsigned display_width;
723     unsigned display_height;
724     vout_display_GetDefaultDisplaySize(&display_width, &display_height,
725                                        &vd->source, &cfg);
726
727     vlc_mutex_lock(&osys->lock);
728
729     osys->ch_display_size       = true;
730     osys->display_width         = display_width;
731     osys->display_height        = display_height;
732     osys->display_is_fullscreen = osys->cfg.is_fullscreen;
733     osys->display_is_forced     = true;
734
735     vlc_mutex_unlock(&osys->lock);
736 }
737
738 static void VoutDisplayCropRatio(int *left, int *top, int *right, int *bottom,
739                                  const video_format_t *source,
740                                  unsigned num, unsigned den)
741 {
742     unsigned scaled_width  = (uint64_t)source->i_visible_height * num * source->i_sar_den / den / source->i_sar_num;
743     unsigned scaled_height = (uint64_t)source->i_visible_width  * den * source->i_sar_num / num / source->i_sar_den;
744
745     if (scaled_width < source->i_visible_width) {
746         *left   = (source->i_visible_width - scaled_width) / 2;
747         *top    = 0;
748         *right  = *left + scaled_width;
749         *bottom = *top  + source->i_visible_height;
750     } else {
751         *left   = 0;
752         *top    = (source->i_visible_height - scaled_height) / 2;
753         *right  = *left + source->i_visible_width;
754         *bottom = *top  + scaled_height;
755     }
756 }
757
758 void vout_ManageDisplay(vout_display_t *vd, bool allow_reset_pictures)
759 {
760     vout_display_owner_sys_t *osys = vd->owner.sys;
761
762     vout_display_Manage(vd);
763
764     /* Handle mouse timeout */
765     const mtime_t date = mdate();
766     bool  hide_mouse = false;
767
768     vlc_mutex_lock(&osys->lock);
769
770     if (!osys->mouse.is_hidden &&
771         osys->mouse.last_moved + osys->mouse.hide_timeout < date) {
772         osys->mouse.is_hidden = hide_mouse = true;
773     } else if (osys->mouse.ch_activity) {
774         osys->mouse.is_hidden = false;
775     }
776     osys->mouse.ch_activity = false;
777     vlc_mutex_unlock(&osys->lock);
778
779     if (hide_mouse) {
780         if (!vd->info.has_hide_mouse) {
781             msg_Dbg(vd, "auto hiding mouse cursor");
782             vout_display_Control(vd, VOUT_DISPLAY_HIDE_MOUSE);
783         }
784         vout_SendEventMouseHidden(osys->vout);
785     }
786
787     bool reset_render = false;
788     for (;;) {
789
790         vlc_mutex_lock(&osys->lock);
791
792         bool ch_fullscreen  = osys->ch_fullscreen;
793         bool is_fullscreen  = osys->is_fullscreen;
794         osys->ch_fullscreen = false;
795
796         bool ch_wm_state  = osys->ch_wm_state;
797         unsigned wm_state  = osys->wm_state;
798         osys->ch_wm_state = false;
799
800         bool ch_display_size       = osys->ch_display_size;
801         int  display_width         = osys->display_width;
802         int  display_height        = osys->display_height;
803         bool display_is_fullscreen = osys->display_is_fullscreen;
804         bool display_is_forced     = osys->display_is_forced;
805         osys->ch_display_size = false;
806
807         bool reset_pictures;
808         if (allow_reset_pictures) {
809             reset_pictures = osys->reset_pictures;
810             osys->reset_pictures = false;
811         } else {
812             reset_pictures = false;
813         }
814
815         vlc_mutex_unlock(&osys->lock);
816
817         if (!ch_fullscreen &&
818             !ch_display_size &&
819             !reset_pictures &&
820             !osys->ch_display_filled &&
821             !osys->ch_zoom &&
822             !ch_wm_state &&
823             !osys->ch_sar &&
824             !osys->ch_crop) {
825
826             if (!osys->cfg.is_fullscreen && osys->fit_window != 0) {
827                 VoutDisplayFitWindow(vd, osys->fit_window == -1);
828                 osys->fit_window = 0;
829                 continue;
830             }
831             break;
832         }
833
834         /* */
835         if (ch_fullscreen) {
836             vout_display_cfg_t cfg = osys->cfg;
837
838             cfg.is_fullscreen  = is_fullscreen;
839             cfg.display.width  = cfg.is_fullscreen ? 0 : osys->width_saved;
840             cfg.display.height = cfg.is_fullscreen ? 0 : osys->height_saved;
841
842             if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_FULLSCREEN, &cfg)) {
843                 msg_Err(vd, "Failed to set fullscreen");
844                 is_fullscreen = osys->cfg.is_fullscreen;
845             } else if (!is_fullscreen) {
846                 vout_display_Control(vd, VOUT_DISPLAY_CHANGE_DISPLAY_SIZE, &cfg, true);
847             }
848             osys->cfg.is_fullscreen = is_fullscreen;
849
850             /* */
851             vout_SendEventFullscreen(osys->vout, osys->cfg.is_fullscreen);
852         }
853
854         /* */
855         if (ch_display_size) {
856             vout_display_cfg_t cfg = osys->cfg;
857             cfg.display.width  = display_width;
858             cfg.display.height = display_height;
859
860             if (!cfg.is_fullscreen != !display_is_fullscreen ||
861                 vout_display_Control(vd, VOUT_DISPLAY_CHANGE_DISPLAY_SIZE, &cfg, display_is_forced)) {
862                 if (!cfg.is_fullscreen == !display_is_fullscreen)
863                     msg_Err(vd, "Failed to resize display");
864
865                 /* We ignore the resized */
866                 display_width  = osys->cfg.display.width;
867                 display_height = osys->cfg.display.height;
868             }
869             osys->cfg.display.width  = display_width;
870             osys->cfg.display.height = display_height;
871
872             if (!display_is_fullscreen) {
873                 osys->width_saved  = display_width;
874                 osys->height_saved = display_height;
875             }
876         }
877         /* */
878         if (osys->ch_display_filled) {
879             vout_display_cfg_t cfg = osys->cfg;
880
881             cfg.is_display_filled = osys->is_display_filled;
882
883             if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_DISPLAY_FILLED, &cfg)) {
884                 msg_Err(vd, "Failed to change display filled state");
885                 osys->is_display_filled = osys->cfg.is_display_filled;
886             }
887             osys->cfg.is_display_filled = osys->is_display_filled;
888             osys->ch_display_filled = false;
889
890             vout_SendEventDisplayFilled(osys->vout, osys->cfg.is_display_filled);
891         }
892         /* */
893         if (osys->ch_zoom) {
894             vout_display_cfg_t cfg = osys->cfg;
895
896             cfg.zoom.num = osys->zoom.num;
897             cfg.zoom.den = osys->zoom.den;
898
899             if (10 * cfg.zoom.num <= cfg.zoom.den) {
900                 cfg.zoom.num = 1;
901                 cfg.zoom.den = 10;
902             } else if (cfg.zoom.num >= 10 * cfg.zoom.den) {
903                 cfg.zoom.num = 10;
904                 cfg.zoom.den = 1;
905             }
906
907             if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_ZOOM, &cfg)) {
908                 msg_Err(vd, "Failed to change zoom");
909                 osys->zoom.num = osys->cfg.zoom.num;
910                 osys->zoom.den = osys->cfg.zoom.den;
911             } else {
912                 osys->fit_window = -1;
913             }
914
915             osys->cfg.zoom.num = osys->zoom.num;
916             osys->cfg.zoom.den = osys->zoom.den;
917             osys->ch_zoom = false;
918
919             vout_SendEventZoom(osys->vout, osys->cfg.zoom.num, osys->cfg.zoom.den);
920         }
921         /* */
922         if (ch_wm_state) {
923             if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_WINDOW_STATE, wm_state)) {
924                 msg_Err(vd, "Failed to set on top");
925                 wm_state = osys->wm_state;
926             }
927             osys->wm_state_initial = wm_state;
928
929             /* */
930             vout_SendEventOnTop(osys->vout, osys->wm_state_initial);
931         }
932         /* */
933         if (osys->ch_sar) {
934             video_format_t source = vd->source;
935
936             if (osys->sar.num > 0 && osys->sar.den > 0) {
937                 source.i_sar_num = osys->sar.num;
938                 source.i_sar_den = osys->sar.den;
939             } else {
940                 source.i_sar_num = osys->source.i_sar_num;
941                 source.i_sar_den = osys->source.i_sar_den;
942             }
943
944             if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_SOURCE_ASPECT, &source)) {
945                 /* There nothing much we can do. The only reason a vout display
946                  * does not support it is because it need the core to add black border
947                  * to the video for it.
948                  * TODO add black borders ?
949                  */
950                 msg_Err(vd, "Failed to change source AR");
951                 source = vd->source;
952             } else if (!osys->fit_window) {
953                 osys->fit_window = 1;
954             }
955             vd->source = source;
956             osys->sar.num = source.i_sar_num;
957             osys->sar.den = source.i_sar_den;
958             osys->ch_sar  = false;
959
960             /* */
961             if (osys->sar.num == osys->source.i_sar_num &&
962                 osys->sar.den == osys->source.i_sar_den)
963             {
964                 vout_SendEventSourceAspect(osys->vout, 0, 0);
965             }
966             else
967             {
968                 unsigned dar_num, dar_den;
969                 vlc_ureduce( &dar_num, &dar_den,
970                              osys->sar.num * vd->source.i_visible_width,
971                              osys->sar.den * vd->source.i_visible_height,
972                              65536);
973                 vout_SendEventSourceAspect(osys->vout, dar_num, dar_den);
974             }
975             /* If a crop ratio is requested, recompute the parameters */
976             if (osys->crop.num > 0 && osys->crop.den > 0)
977                 osys->ch_crop = true;
978         }
979         /* */
980         if (osys->ch_crop) {
981             video_format_t source = vd->source;
982
983             unsigned crop_num = osys->crop.num;
984             unsigned crop_den = osys->crop.den;
985             if (crop_num > 0 && crop_den > 0) {
986                 video_format_t fmt = osys->source;
987                 fmt.i_sar_num = source.i_sar_num;
988                 fmt.i_sar_den = source.i_sar_den;
989                 VoutDisplayCropRatio(&osys->crop.left,  &osys->crop.top,
990                                      &osys->crop.right, &osys->crop.bottom,
991                                      &fmt, crop_num, crop_den);
992             }
993             const int right_max  = osys->source.i_x_offset + osys->source.i_visible_width;
994             const int bottom_max = osys->source.i_y_offset + osys->source.i_visible_height;
995             int left   = VLC_CLIP((int)osys->source.i_x_offset + osys->crop.left,
996                                 0, right_max - 1);
997             int top    = VLC_CLIP((int)osys->source.i_y_offset + osys->crop.top,
998                                 0, bottom_max - 1);
999             int right, bottom;
1000             if (osys->crop.right <= 0)
1001                 right = (int)(osys->source.i_x_offset + osys->source.i_visible_width) + osys->crop.right;
1002             else
1003                 right = (int)osys->source.i_x_offset + osys->crop.right;
1004             right = VLC_CLIP(right, left + 1, right_max);
1005             if (osys->crop.bottom <= 0)
1006                 bottom = (int)(osys->source.i_y_offset + osys->source.i_visible_height) + osys->crop.bottom;
1007             else
1008                 bottom = (int)osys->source.i_y_offset + osys->crop.bottom;
1009             bottom = VLC_CLIP(bottom, top + 1, bottom_max);
1010
1011             source.i_x_offset       = left;
1012             source.i_y_offset       = top;
1013             source.i_visible_width  = right - left;
1014             source.i_visible_height = bottom - top;
1015             video_format_Print(VLC_OBJECT(vd), "SOURCE ", &osys->source);
1016             video_format_Print(VLC_OBJECT(vd), "CROPPED", &source);
1017             if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_SOURCE_CROP, &source)) {
1018                 msg_Err(vd, "Failed to change source crop TODO implement crop at core");
1019
1020                 source = vd->source;
1021                 crop_num = osys->crop_saved.num;
1022                 crop_den = osys->crop_saved.den;
1023                 /* FIXME implement cropping in the core if not supported by the
1024                  * vout module (easy)
1025                  */
1026             } else if (!osys->fit_window) {
1027                 osys->fit_window = 1;
1028             }
1029             vd->source = source;
1030             osys->crop.left   = source.i_x_offset - osys->source.i_x_offset;
1031             osys->crop.top    = source.i_y_offset - osys->source.i_y_offset;
1032             /* FIXME for right/bottom we should keep the 'type' border vs window */
1033             osys->crop.right  = (source.i_x_offset + source.i_visible_width) -
1034                                 (osys->source.i_x_offset + osys->source.i_visible_width);
1035             osys->crop.bottom = (source.i_y_offset + source.i_visible_height) -
1036                                 (osys->source.i_y_offset + osys->source.i_visible_height);
1037             osys->crop.num    = crop_num;
1038             osys->crop.den    = crop_den;
1039             osys->ch_crop = false;
1040
1041             vout_SendEventSourceCrop(osys->vout,
1042                                      osys->crop.num, osys->crop.den,
1043                                      osys->crop.left, osys->crop.top,
1044                                      -osys->crop.right, -osys->crop.bottom);
1045         }
1046
1047         /* */
1048         if (reset_pictures) {
1049             if (vout_display_Control(vd, VOUT_DISPLAY_RESET_PICTURES)) {
1050                 /* FIXME what to do here ? */
1051                 msg_Err(vd, "Failed to reset pictures (probably fatal)");
1052             }
1053             reset_render = true;
1054         }
1055     }
1056     if (reset_render)
1057         VoutDisplayResetRender(vd);
1058 }
1059
1060 bool vout_AreDisplayPicturesInvalid(vout_display_t *vd)
1061 {
1062     vout_display_owner_sys_t *osys = vd->owner.sys;
1063
1064     vlc_mutex_lock(&osys->lock);
1065     const bool reset_pictures = osys->reset_pictures;
1066     vlc_mutex_unlock(&osys->lock);
1067
1068     return reset_pictures;
1069 }
1070
1071 bool vout_IsDisplayFiltered(vout_display_t *vd)
1072 {
1073     vout_display_owner_sys_t *osys = vd->owner.sys;
1074
1075     return osys->filters != NULL;
1076 }
1077
1078 picture_t *vout_FilterDisplay(vout_display_t *vd, picture_t *picture)
1079 {
1080     vout_display_owner_sys_t *osys = vd->owner.sys;
1081
1082     assert(osys->filters);
1083     if (filter_chain_GetLength(osys->filters) <= 0) {
1084         picture_Release(picture);
1085         return NULL;
1086     }
1087     return filter_chain_VideoFilter(osys->filters, picture);
1088 }
1089
1090 void vout_UpdateDisplaySourceProperties(vout_display_t *vd, const video_format_t *source)
1091 {
1092     vout_display_owner_sys_t *osys = vd->owner.sys;
1093
1094     if (source->i_sar_num * osys->source.i_sar_den !=
1095         source->i_sar_den * osys->source.i_sar_num) {
1096
1097         osys->source.i_sar_num = source->i_sar_num;
1098         osys->source.i_sar_den = source->i_sar_den;
1099         vlc_ureduce(&osys->source.i_sar_num, &osys->source.i_sar_den,
1100                     osys->source.i_sar_num, osys->source.i_sar_den, 0);
1101
1102         /* FIXME it will override any AR that the user would have forced */
1103         osys->ch_sar = true;
1104         osys->sar.num = osys->source.i_sar_num;
1105         osys->sar.den = osys->source.i_sar_den;
1106     }
1107     if (source->i_x_offset       != osys->source.i_x_offset ||
1108         source->i_y_offset       != osys->source.i_y_offset ||
1109         source->i_visible_width  != osys->source.i_visible_width ||
1110         source->i_visible_height != osys->source.i_visible_height) {
1111
1112         video_format_CopyCrop(&osys->source, source);
1113
1114         /* Force the vout to reapply the current user crop settings over the new decoder
1115          * crop settings. */
1116         osys->ch_crop = true;
1117     }
1118 }
1119
1120 void vout_SetDisplayFullscreen(vout_display_t *vd, bool is_fullscreen)
1121 {
1122     vout_display_owner_sys_t *osys = vd->owner.sys;
1123
1124     vlc_mutex_lock(&osys->lock);
1125     if (!osys->is_fullscreen != !is_fullscreen) {
1126         osys->ch_fullscreen = true;
1127         osys->is_fullscreen = is_fullscreen;
1128     }
1129     vlc_mutex_unlock(&osys->lock);
1130 }
1131
1132 void vout_SetDisplayFilled(vout_display_t *vd, bool is_filled)
1133 {
1134     vout_display_owner_sys_t *osys = vd->owner.sys;
1135
1136     if (!osys->is_display_filled != !is_filled) {
1137         osys->ch_display_filled = true;
1138         osys->is_display_filled = is_filled;
1139     }
1140 }
1141
1142 void vout_SetDisplayZoom(vout_display_t *vd, int num, int den)
1143 {
1144     vout_display_owner_sys_t *osys = vd->owner.sys;
1145
1146     if (osys->is_display_filled ||
1147         osys->zoom.num != num || osys->zoom.den != den) {
1148         osys->ch_zoom = true;
1149         osys->zoom.num = num;
1150         osys->zoom.den = den;
1151     }
1152 }
1153
1154 void vout_SetWindowState(vout_display_t *vd, unsigned state)
1155 {
1156     vout_display_owner_sys_t *osys = vd->owner.sys;
1157
1158     vlc_mutex_lock(&osys->lock);
1159     if (osys->wm_state != state) {
1160         osys->ch_wm_state = true;
1161         osys->wm_state = state;
1162     }
1163     vlc_mutex_unlock(&osys->lock);
1164 }
1165
1166 void vout_SetDisplayAspect(vout_display_t *vd, unsigned dar_num, unsigned dar_den)
1167 {
1168     vout_display_owner_sys_t *osys = vd->owner.sys;
1169
1170     unsigned sar_num, sar_den;
1171     if (dar_num > 0 && dar_den > 0) {
1172         sar_num = dar_num * osys->source.i_visible_height;
1173         sar_den = dar_den * osys->source.i_visible_width;
1174         vlc_ureduce(&sar_num, &sar_den, sar_num, sar_den, 0);
1175     } else {
1176         sar_num = 0;
1177         sar_den = 0;
1178     }
1179
1180     if (osys->sar.num != sar_num || osys->sar.den != sar_den) {
1181         osys->ch_sar = true;
1182         osys->sar.num = sar_num;
1183         osys->sar.den = sar_den;
1184     }
1185 }
1186 void vout_SetDisplayCrop(vout_display_t *vd,
1187                          unsigned crop_num, unsigned crop_den,
1188                          unsigned left, unsigned top, int right, int bottom)
1189 {
1190     vout_display_owner_sys_t *osys = vd->owner.sys;
1191
1192     if (osys->crop.left  != (int)left  || osys->crop.top != (int)top ||
1193         osys->crop.right != right || osys->crop.bottom != bottom ||
1194         (crop_num > 0 && crop_den > 0 &&
1195          (crop_num != osys->crop.num || crop_den != osys->crop.den))) {
1196
1197         osys->crop.left   = left;
1198         osys->crop.top    = top;
1199         osys->crop.right  = right;
1200         osys->crop.bottom = bottom;
1201         osys->crop.num    = crop_num;
1202         osys->crop.den    = crop_den;
1203
1204         osys->ch_crop = true;
1205     }
1206 }
1207
1208 struct vlc_gl_t *vout_GetDisplayOpengl(vout_display_t *vd)
1209 {
1210     struct vlc_gl_t *gl;
1211     if (vout_display_Control(vd, VOUT_DISPLAY_GET_OPENGL, &gl))
1212         return NULL;
1213     return gl;
1214 }
1215
1216 static vout_display_t *DisplayNew(vout_thread_t *vout,
1217                                   const video_format_t *source_org,
1218                                   const vout_display_state_t *state,
1219                                   const char *module,
1220                                   bool is_wrapper, vout_display_t *wrapper,
1221                                   mtime_t double_click_timeout,
1222                                   mtime_t hide_timeout,
1223                                   const vout_display_owner_t *owner_ptr)
1224 {
1225     /* */
1226     vout_display_owner_sys_t *osys = calloc(1, sizeof(*osys));
1227     vout_display_cfg_t *cfg = &osys->cfg;
1228
1229     *cfg = state->cfg;
1230     osys->wm_state_initial = -1;
1231     osys->sar_initial.num = state->sar.num;
1232     osys->sar_initial.den = state->sar.den;
1233     vout_display_GetDefaultDisplaySize(&cfg->display.width, &cfg->display.height,
1234                                        source_org, cfg);
1235
1236     osys->vout = vout;
1237     osys->is_wrapper = is_wrapper;
1238     osys->wrapper = wrapper;
1239
1240     vlc_mutex_init(&osys->lock);
1241
1242     vlc_mouse_Init(&osys->mouse.state);
1243     osys->mouse.last_moved = mdate();
1244     osys->mouse.double_click_timeout = double_click_timeout;
1245     osys->mouse.hide_timeout = hide_timeout;
1246     osys->is_fullscreen  = cfg->is_fullscreen;
1247     osys->display_width  = cfg->display.width;
1248     osys->display_height = cfg->display.height;
1249     osys->is_display_filled = cfg->is_display_filled;
1250     osys->width_saved    = cfg->display.width;
1251     osys->height_saved   = cfg->display.height;
1252     if (osys->is_fullscreen) {
1253         vout_display_cfg_t cfg_windowed = *cfg;
1254         cfg_windowed.is_fullscreen  = false;
1255         cfg_windowed.display.width  = 0;
1256         cfg_windowed.display.height = 0;
1257         vout_display_GetDefaultDisplaySize(&osys->width_saved,
1258                                            &osys->height_saved,
1259                                            source_org, &cfg_windowed);
1260     }
1261     osys->zoom.num = cfg->zoom.num;
1262     osys->zoom.den = cfg->zoom.den;
1263     osys->wm_state = state->wm_state;
1264     osys->fit_window = 0;
1265     osys->event.fifo = NULL;
1266
1267     osys->source = *source_org;
1268     osys->crop.left   = 0;
1269     osys->crop.top    = 0;
1270     osys->crop.right  = 0;
1271     osys->crop.bottom = 0;
1272     osys->crop_saved.num = 0;
1273     osys->crop_saved.den = 0;
1274     osys->crop.num = 0;
1275     osys->crop.den = 0;
1276
1277     osys->sar.num = osys->sar_initial.num ? osys->sar_initial.num : source_org->i_sar_num;
1278     osys->sar.den = osys->sar_initial.den ? osys->sar_initial.den : source_org->i_sar_den;
1279 #ifdef ALLOW_DUMMY_VOUT
1280     vlc_mouse_Init(&osys->vout_mouse);
1281 #endif
1282
1283     vout_display_owner_t owner;
1284     if (owner_ptr) {
1285         owner = *owner_ptr;
1286     } else {
1287         owner.event      = VoutDisplayEvent;
1288         owner.window_new = VoutDisplayNewWindow;
1289         owner.window_del = VoutDisplayDelWindow;
1290     }
1291     owner.sys = osys;
1292
1293     /* */
1294     video_format_t source = *source_org;
1295
1296     source.i_x_offset = 0;
1297     source.i_y_offset = 0;
1298     source.i_visible_width  = source.i_width;
1299     source.i_visible_height = source.i_height;
1300
1301     vout_display_t *p_display = vout_display_New(VLC_OBJECT(vout),
1302                                                  module, !is_wrapper,
1303                                                  &source, cfg, &owner);
1304     if (!p_display) {
1305         free(osys);
1306         return NULL;
1307     }
1308
1309     VoutDisplayCreateRender(p_display);
1310
1311     /* Setup delayed request */
1312     if (osys->sar.num != source.i_sar_num ||
1313         osys->sar.den != source.i_sar_den)
1314         osys->ch_sar = true;
1315     if (osys->wm_state != osys->wm_state_initial)
1316         osys->ch_wm_state = true;
1317     if (source.i_x_offset       != source_org->i_x_offset ||
1318         source.i_y_offset       != source_org->i_y_offset ||
1319         source.i_visible_width  != source_org->i_visible_width ||
1320         source.i_visible_height != source_org->i_visible_height)
1321         osys->ch_crop = true;
1322
1323     return p_display;
1324 }
1325
1326 void vout_DeleteDisplay(vout_display_t *vd, vout_display_state_t *state)
1327 {
1328     vout_display_owner_sys_t *osys = vd->owner.sys;
1329
1330     if (state) {
1331         if (!osys->is_wrapper )
1332             state->cfg = osys->cfg;
1333         state->wm_state = osys->wm_state;
1334         state->sar.num  = osys->sar_initial.num;
1335         state->sar.den  = osys->sar_initial.den;
1336     }
1337
1338     VoutDisplayDestroyRender(vd);
1339     if (osys->is_wrapper)
1340         SplitterClose(vd);
1341     vout_display_Delete(vd);
1342     if (osys->event.fifo) {
1343         vlc_cancel(osys->event.thread);
1344         vlc_join(osys->event.thread, NULL);
1345         block_FifoRelease(osys->event.fifo);
1346     }
1347     vlc_mutex_destroy(&osys->lock);
1348     free(osys);
1349 }
1350
1351 /*****************************************************************************
1352  *
1353  *****************************************************************************/
1354 vout_display_t *vout_NewDisplay(vout_thread_t *vout,
1355                                 const video_format_t *source,
1356                                 const vout_display_state_t *state,
1357                                 const char *module,
1358                                 mtime_t double_click_timeout,
1359                                 mtime_t hide_timeout)
1360 {
1361     return DisplayNew(vout, source, state, module, false, NULL,
1362                       double_click_timeout, hide_timeout, NULL);
1363 }
1364
1365 /*****************************************************************************
1366  *
1367  *****************************************************************************/
1368 struct vout_display_sys_t {
1369     picture_pool_t   *pool;
1370     video_splitter_t *splitter;
1371
1372     /* */
1373     int            count;
1374     picture_t      **picture;
1375     vout_display_t **display;
1376 };
1377 struct video_splitter_owner_t {
1378     vout_display_t *wrapper;
1379 };
1380
1381 static vout_window_t *SplitterNewWindow(vout_display_t *vd, const vout_window_cfg_t *cfg_ptr)
1382 {
1383     vout_display_owner_sys_t *osys = vd->owner.sys;
1384
1385     vout_window_cfg_t cfg = *cfg_ptr;
1386     cfg.is_standalone = true;
1387     cfg.x += 0;//output->window.i_x; FIXME
1388     cfg.y += 0;//output->window.i_y;
1389
1390     return vout_NewDisplayWindow(osys->vout, vd, &cfg);
1391 }
1392 static void SplitterDelWindow(vout_display_t *vd, vout_window_t *window)
1393 {
1394     vout_display_owner_sys_t *osys = vd->owner.sys;
1395
1396     vout_DeleteDisplayWindow(osys->vout, vd, window);
1397 }
1398 static void SplitterEvent(vout_display_t *vd, int event, va_list args)
1399 {
1400     //vout_display_owner_sys_t *osys = vd->owner.sys;
1401
1402     switch (event) {
1403 #if 0
1404     case VOUT_DISPLAY_EVENT_MOUSE_STATE:
1405     case VOUT_DISPLAY_EVENT_MOUSE_MOVED:
1406     case VOUT_DISPLAY_EVENT_MOUSE_PRESSED:
1407     case VOUT_DISPLAY_EVENT_MOUSE_RELEASED:
1408         /* TODO */
1409         break;
1410 #endif
1411     case VOUT_DISPLAY_EVENT_MOUSE_DOUBLE_CLICK:
1412     case VOUT_DISPLAY_EVENT_KEY:
1413     case VOUT_DISPLAY_EVENT_CLOSE:
1414     case VOUT_DISPLAY_EVENT_FULLSCREEN:
1415     case VOUT_DISPLAY_EVENT_DISPLAY_SIZE:
1416     case VOUT_DISPLAY_EVENT_PICTURES_INVALID:
1417         VoutDisplayEvent(vd, event, args);
1418         break;
1419
1420     default:
1421         msg_Err(vd, "SplitterEvent TODO");
1422         break;
1423     }
1424 }
1425
1426 static picture_pool_t *SplitterPool(vout_display_t *vd, unsigned count)
1427 {
1428     vout_display_sys_t *sys = vd->sys;
1429     if (!sys->pool)
1430         sys->pool = picture_pool_NewFromFormat(&vd->fmt, count);
1431     return sys->pool;
1432 }
1433 static void SplitterPrepare(vout_display_t *vd,
1434                             picture_t *picture,
1435                             subpicture_t *subpicture)
1436 {
1437     vout_display_sys_t *sys = vd->sys;
1438
1439     picture_Hold(picture);
1440     assert(!subpicture);
1441
1442     if (video_splitter_Filter(sys->splitter, sys->picture, picture)) {
1443         for (int i = 0; i < sys->count; i++)
1444             sys->picture[i] = NULL;
1445         picture_Release(picture);
1446         return;
1447     }
1448
1449     for (int i = 0; i < sys->count; i++) {
1450         if (vout_IsDisplayFiltered(sys->display[i]))
1451             sys->picture[i] = vout_FilterDisplay(sys->display[i], sys->picture[i]);
1452         if (sys->picture[i])
1453             vout_display_Prepare(sys->display[i], sys->picture[i], NULL);
1454     }
1455 }
1456 static void SplitterDisplay(vout_display_t *vd,
1457                             picture_t *picture,
1458                             subpicture_t *subpicture)
1459 {
1460     vout_display_sys_t *sys = vd->sys;
1461
1462     assert(!subpicture);
1463     for (int i = 0; i < sys->count; i++) {
1464         if (sys->picture[i])
1465             vout_display_Display(sys->display[i], sys->picture[i], NULL);
1466     }
1467     picture_Release(picture);
1468 }
1469 static int SplitterControl(vout_display_t *vd, int query, va_list args)
1470 {
1471     (void)vd; (void)query; (void)args;
1472     return VLC_EGENERIC;
1473 }
1474 static void SplitterManage(vout_display_t *vd)
1475 {
1476     vout_display_sys_t *sys = vd->sys;
1477
1478     for (int i = 0; i < sys->count; i++)
1479         vout_ManageDisplay(sys->display[i], true);
1480 }
1481
1482 static int SplitterPictureNew(video_splitter_t *splitter, picture_t *picture[])
1483 {
1484     vout_display_sys_t *wsys = splitter->p_owner->wrapper->sys;
1485
1486     for (int i = 0; i < wsys->count; i++) {
1487         if (vout_IsDisplayFiltered(wsys->display[i])) {
1488             /* TODO use a pool ? */
1489             picture[i] = picture_NewFromFormat(&wsys->display[i]->source);
1490         } else {
1491             picture_pool_t *pool = vout_display_Pool(wsys->display[i], 1);
1492             picture[i] = pool ? picture_pool_Get(pool) : NULL;
1493         }
1494         if (!picture[i]) {
1495             for (int j = 0; j < i; j++)
1496                 picture_Release(picture[j]);
1497             return VLC_EGENERIC;
1498         }
1499     }
1500     return VLC_SUCCESS;
1501 }
1502 static void SplitterPictureDel(video_splitter_t *splitter, picture_t *picture[])
1503 {
1504     vout_display_sys_t *wsys = splitter->p_owner->wrapper->sys;
1505
1506     for (int i = 0; i < wsys->count; i++)
1507         picture_Release(picture[i]);
1508 }
1509 static void SplitterClose(vout_display_t *vd)
1510 {
1511     vout_display_sys_t *sys = vd->sys;
1512
1513     /* */
1514     video_splitter_t *splitter = sys->splitter;
1515     free(splitter->p_owner);
1516     video_splitter_Delete(splitter);
1517
1518     if (sys->pool)
1519         picture_pool_Delete(sys->pool);
1520
1521     /* */
1522     for (int i = 0; i < sys->count; i++)
1523         vout_DeleteDisplay(sys->display[i], NULL);
1524     TAB_CLEAN(sys->count, sys->display);
1525     free(sys->picture);
1526
1527     free(sys);
1528 }
1529
1530 vout_display_t *vout_NewSplitter(vout_thread_t *vout,
1531                                  const video_format_t *source,
1532                                  const vout_display_state_t *state,
1533                                  const char *module,
1534                                  const char *splitter_module,
1535                                  mtime_t double_click_timeout,
1536                                  mtime_t hide_timeout)
1537 {
1538     video_splitter_t *splitter =
1539         video_splitter_New(VLC_OBJECT(vout), splitter_module, source);
1540     if (!splitter)
1541         return NULL;
1542
1543     /* */
1544     vout_display_t *wrapper =
1545         DisplayNew(vout, source, state, module, true, NULL,
1546                     double_click_timeout, hide_timeout, NULL);
1547     if (!wrapper) {
1548         video_splitter_Delete(splitter);
1549         return NULL;
1550     }
1551     vout_display_sys_t *sys = malloc(sizeof(*sys));
1552     if (!sys)
1553         abort();
1554     sys->picture = calloc(splitter->i_output, sizeof(*sys->picture));
1555     if (!sys->picture )
1556         abort();
1557     sys->splitter = splitter;
1558     sys->pool     = NULL;
1559
1560     wrapper->pool    = SplitterPool;
1561     wrapper->prepare = SplitterPrepare;
1562     wrapper->display = SplitterDisplay;
1563     wrapper->control = SplitterControl;
1564     wrapper->manage  = SplitterManage;
1565     wrapper->sys     = sys;
1566
1567     /* */
1568     video_splitter_owner_t *owner = malloc(sizeof(*owner));
1569     if (!owner)
1570         abort();
1571     owner->wrapper = wrapper;
1572     splitter->p_owner = owner;
1573     splitter->pf_picture_new = SplitterPictureNew;
1574     splitter->pf_picture_del = SplitterPictureDel;
1575
1576     /* */
1577     TAB_INIT(sys->count, sys->display);
1578     for (int i = 0; i < splitter->i_output; i++) {
1579         vout_display_owner_t owner;
1580
1581         owner.event      = SplitterEvent;
1582         owner.window_new = SplitterNewWindow;
1583         owner.window_del = SplitterDelWindow;
1584
1585         const video_splitter_output_t *output = &splitter->p_output[i];
1586         vout_display_state_t ostate;
1587
1588         memset(&ostate, 0, sizeof(ostate));
1589         ostate.cfg.is_fullscreen = false;
1590         ostate.cfg.display = state->cfg.display;
1591         ostate.cfg.align.horizontal = 0; /* TODO */
1592         ostate.cfg.align.vertical = 0; /* TODO */
1593         ostate.cfg.is_display_filled = true;
1594         ostate.cfg.zoom.num = 1;
1595         ostate.cfg.zoom.den = 1;
1596
1597         vout_display_t *vd = DisplayNew(vout, &output->fmt, &ostate,
1598                                            output->psz_module ? output->psz_module : module,
1599                                            false, wrapper,
1600                                            double_click_timeout, hide_timeout, &owner);
1601         if (!vd) {
1602             vout_DeleteDisplay(wrapper, NULL);
1603             return NULL;
1604         }
1605         TAB_APPEND(sys->count, sys->display, vd);
1606     }
1607
1608     return wrapper;
1609 }
1610
1611 /*****************************************************************************
1612  * TODO move out
1613  *****************************************************************************/
1614 #include "vout_internal.h"
1615 void vout_SendDisplayEventMouse(vout_thread_t *vout, const vlc_mouse_t *m)
1616 {
1617     vlc_mouse_t tmp1, tmp2;
1618
1619     /* The check on spu is needed as long as ALLOW_DUMMY_VOUT is defined */
1620     if (vout->p->spu && spu_ProcessMouse( vout->p->spu, m, &vout->p->display.vd->source))
1621         return;
1622
1623     vlc_mutex_lock( &vout->p->filter.lock );
1624     if (vout->p->filter.chain_static && vout->p->filter.chain_interactive) {
1625         if (!filter_chain_MouseFilter(vout->p->filter.chain_interactive, &tmp1, m))
1626             m = &tmp1;
1627         if (!filter_chain_MouseFilter(vout->p->filter.chain_static,      &tmp2, m))
1628             m = &tmp2;
1629     }
1630     vlc_mutex_unlock( &vout->p->filter.lock );
1631
1632     if (vlc_mouse_HasMoved(&vout->p->mouse, m)) {
1633         vout_SendEventMouseMoved(vout, m->i_x, m->i_y);
1634     }
1635     if (vlc_mouse_HasButton(&vout->p->mouse, m)) {
1636         for (unsigned button = 0; button < MOUSE_BUTTON_MAX; button++) {
1637             if (vlc_mouse_HasPressed(&vout->p->mouse, m, button))
1638                 vout_SendEventMousePressed(vout, button);
1639             else if (vlc_mouse_HasReleased(&vout->p->mouse, m, button))
1640                 vout_SendEventMouseReleased(vout, button);
1641         }
1642     }
1643     if (m->b_double_click)
1644         vout_SendEventMouseDoubleClick(vout);
1645     vout->p->mouse = *m;
1646 }
1647 #ifdef ALLOW_DUMMY_VOUT
1648 static void DummyVoutSendDisplayEventMouse(vout_thread_t *vout, vlc_mouse_t *fallback, const vlc_mouse_t *m)
1649 {
1650     vout_thread_sys_t p;
1651
1652     if (!vout->p) {
1653         p.mouse = *fallback;
1654         vlc_mutex_init(&p.filter.lock);
1655         p.filter.chain_static = NULL;
1656         p.filter.chain_interactive = NULL;
1657         p.spu = NULL;
1658         vout->p = &p;
1659     }
1660     vout_SendDisplayEventMouse(vout, m);
1661     if (vout->p == &p) {
1662         vlc_mutex_destroy(&p.filter.lock);
1663         *fallback = p.mouse;
1664         vout->p = NULL;
1665     }
1666 }
1667 #endif
1668