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