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