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