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