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