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