display.c 44.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/*****************************************************************************
 * display.c: "vout display" managment
 *****************************************************************************
 * Copyright (C) 2009 Laurent Aimar
 * $Id$
 *
 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>

#include <vlc_common.h>
#include <vlc_video_splitter.h>
#include <vlc_vout_display.h>
#include <vlc_vout.h>

#include <libvlc.h>

#include "display.h"

#include "event.h"

static void SplitterClose(vout_display_t *vd);

/*****************************************************************************
 * FIXME/TODO see how to have direct rendering here (interact with vout.c)
 *****************************************************************************/
static picture_t *VideoBufferNew(filter_t *filter)
{
50
    vout_display_t *vd = (vout_display_t*)filter->p_owner;
51 52
    const video_format_t *fmt = &filter->fmt_out.video;

53 54 55 56 57
    assert(vd->fmt.i_chroma == fmt->i_chroma &&
           vd->fmt.i_width  == fmt->i_width  &&
           vd->fmt.i_height == fmt->i_height);

    return vout_display_Get(vd);
58 59 60 61 62 63 64
}
static void VideoBufferDelete(filter_t *filter, picture_t *picture)
{
    VLC_UNUSED(filter);
    picture_Release(picture);
}

65
static int  FilterAllocationInit(filter_t *filter, void *vd)
66 67 68
{
    filter->pf_vout_buffer_new = VideoBufferNew;
    filter->pf_vout_buffer_del = VideoBufferDelete;
69
    filter->p_owner            = vd;
70 71 72 73 74 75 76

    return VLC_SUCCESS;
}
static void FilterAllocationClean(filter_t *filter)
{
    filter->pf_vout_buffer_new = NULL;
    filter->pf_vout_buffer_del = NULL;
77
    filter->p_owner            = NULL;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
}

/*****************************************************************************
 *
 *****************************************************************************/

/**
 * It creates a new vout_display_t using the given configuration.
 */
static vout_display_t *vout_display_New(vlc_object_t *obj,
                                        const char *module, bool load_module,
                                        const video_format_t *fmt,
                                        const vout_display_cfg_t *cfg,
                                        vout_display_owner_t *owner)
{
    /* */
    vout_display_t *vd = vlc_object_create(obj, sizeof(*vd));

    /* */
    video_format_Copy(&vd->source, fmt);

    /* Picture buffer does not have the concept of aspect ratio */
    video_format_Copy(&vd->fmt, fmt);
    vd->fmt.i_aspect = 0;
    vd->fmt.i_sar_num = 0;
    vd->fmt.i_sar_den = 0;

    vd->info.is_slow = false;
    vd->info.has_double_click = false;
    vd->info.has_hide_mouse = false;
    vd->info.has_pictures_invalid = false;

    vd->cfg = cfg;
    vd->get = NULL;
    vd->prepare = NULL;
    vd->display = NULL;
    vd->control = NULL;
    vd->manage = NULL;
    vd->sys = NULL;

    vd->owner = *owner;

    vlc_object_attach(vd, obj);

    if (load_module) {
        vd->module = module_need(vd, "vout display", module, module && *module != '\0');
        if (!vd->module) {
            vlc_object_detach(vd);
            vlc_object_release(vd);
            return NULL;
        }
    } else {
        vd->module = NULL;
    }
    return vd;
}

/**
 * It deletes a vout_display_t
 */
static void vout_display_Delete(vout_display_t *vd)
{
    vlc_object_detach(vd);

    if (vd->module)
        module_unneed(vd, vd->module);

    vlc_object_release(vd);
}

/**
 * It controls a vout_display_t
 */
static int vout_display_Control(vout_display_t *vd, int query, ...)
{
    va_list args;
    int result;

    va_start(args, query);
    result = vd->control(vd, query, args);
    va_end(args);

    return result;
}
static void vout_display_Manage(vout_display_t *vd)
{
164 165
    if (vd->manage)
        vd->manage(vd);
166 167 168
}

/* */
169
void vout_display_GetDefaultDisplaySize(unsigned *width, unsigned *height,
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
                                        const video_format_t *source,
                                        const vout_display_cfg_t *cfg)
{
    if (cfg->display.width > 0 && cfg->display.height > 0) {
        *width  = cfg->display.width;
        *height = cfg->display.height;
    } else if (cfg->display.width > 0) {
        *width  = cfg->display.width;
        *height = (int64_t)source->i_visible_height * source->i_sar_den * cfg->display.width * cfg->display.sar.num /
            source->i_visible_width / source->i_sar_num / cfg->display.sar.den;
    } else if (cfg->display.height > 0) {
        *width  = (int64_t)source->i_visible_width * source->i_sar_num * cfg->display.height * cfg->display.sar.den /
            source->i_visible_height / source->i_sar_den / cfg->display.sar.num;
        *height = cfg->display.height;
    } else if (source->i_sar_num >= source->i_sar_den) {
        *width  = (int64_t)source->i_visible_width * source->i_sar_num * cfg->display.sar.den / source->i_sar_den / cfg->display.sar.num;
        *height = source->i_visible_height;
    } else {
        *width  = source->i_visible_width;
        *height = (int64_t)source->i_visible_height * source->i_sar_den * cfg->display.sar.num / source->i_sar_num / cfg->display.sar.den;
    }

    *width  = *width  * cfg->zoom.num / cfg->zoom.den;
    *height = *height * cfg->zoom.num / cfg->zoom.den;
}

/* */
void vout_display_PlacePicture(vout_display_place_t *place,
                               const video_format_t *source,
                               const vout_display_cfg_t *cfg,
                               bool do_clipping)
{
    /* */
    memset(place, 0, sizeof(*place));
    if (cfg->display.width <= 0 || cfg->display.height <= 0)
        return;

    /* */
208 209 210 211
    unsigned width;
    unsigned height;
    unsigned display_width;
    unsigned display_height;
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

    if (cfg->is_display_filled) {
        width  = source->i_visible_width;
        height = source->i_visible_height;
        display_width  = cfg->display.width;
        display_height = cfg->display.height;
    } else {
        vout_display_cfg_t cfg_tmp = *cfg;

        cfg_tmp.display.width  = 0;
        cfg_tmp.display.height = 0;
        vout_display_GetDefaultDisplaySize(&width, &height,
                                           source, &cfg_tmp);

        display_width  = width;
        display_height = height;
        if (do_clipping) {
            display_width  = __MIN(display_width,  cfg->display.width);
            display_height = __MIN(display_height, cfg->display.height);
        }
    }

    /* Compute the height if we use the width to fill up display_width */
    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;
    /* And the same but switching width/height */
    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;

    /* We keep the solution that avoid filling outside the display */
    if (scaled_width <= cfg->display.width) {
        place->width  = scaled_width;
        place->height = display_height;
    } else {
        place->width  = display_width;
        place->height = scaled_height;
    }

    /*  Compute position */
    switch (cfg->align.horizontal) {
    case VOUT_DISPLAY_ALIGN_LEFT:
        place->x = 0;
        break;
    case VOUT_DISPLAY_ALIGN_RIGHT:
        place->x = cfg->display.width - place->width;
        break;
    default:
        place->x = (cfg->display.width - place->width) / 2;
        break;
    }

    switch (cfg->align.vertical) {
    case VOUT_DISPLAY_ALIGN_TOP:
        place->y = 0;
        break;
    case VOUT_DISPLAY_ALIGN_BOTTOM:
        place->y = cfg->display.height - place->height;
        break;
    default:
        place->y = (cfg->display.height - place->height) / 2;
        break;
    }
}

struct vout_display_owner_sys_t {
    vout_thread_t   *vout;
    bool            is_wrapper;  /* Is the current display a wrapper */
    vout_display_t  *wrapper; /* Vout display wrapper */

    /* */
    vout_display_cfg_t cfg;
    bool     is_on_top_initial;
    struct {
        unsigned num;
        unsigned den;
    } sar_initial;

    /* */
    int  width_saved;
    int  height_saved;

    struct {
        unsigned num;
        unsigned den;
    } crop_saved;

    /* */
    bool ch_display_filled;
    bool is_display_filled;

    bool ch_zoom;
    struct {
        int  num;
        int  den;
    } zoom;

    bool ch_on_top;
    bool is_on_top;

    bool ch_sar;
    struct {
        unsigned num;
        unsigned den;
    } sar;

    bool ch_crop;
    struct {
        unsigned x;
        unsigned y;
        unsigned width;
        unsigned height;
        unsigned num;
        unsigned den;
    } crop;

    /* */
    video_format_t source;
    filter_chain_t *filters;
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

    /* Lock protecting the variables used by
     * VoutDisplayEvent(ie vout_display_SendEvent) */
    vlc_mutex_t lock;

    /* mouse state */
    struct {
        vlc_mouse_t state;

        mtime_t last_pressed;
        mtime_t last_moved;
        bool    is_hidden;
        bool    ch_activity;

        /* */
        mtime_t double_click_timeout;
        mtime_t hide_timeout;
    } mouse;

    bool reset_pictures;

    bool ch_fullscreen;
    bool is_fullscreen;

    bool ch_display_size;
    int  display_width;
    int  display_height;
    bool display_is_fullscreen;
356
    bool display_is_forced;
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
};

static void VoutDisplayCreateRender(vout_display_t *vd)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    osys->filters = NULL;

    video_format_t v_src = vd->source;
    v_src.i_aspect  = 0;
    v_src.i_sar_num = 0;
    v_src.i_sar_den = 0;

    video_format_t v_dst = vd->fmt;
    v_dst.i_aspect  = 0;
    v_dst.i_sar_num = 0;
    v_dst.i_sar_den = 0;

375 376 377 378 379 380 381 382
    video_format_t v_dst_cmp = v_dst;
    if ((v_src.i_chroma == VLC_CODEC_J420 && v_dst.i_chroma == VLC_CODEC_I420) ||
        (v_src.i_chroma == VLC_CODEC_J422 && v_dst.i_chroma == VLC_CODEC_I422) ||
        (v_src.i_chroma == VLC_CODEC_J440 && v_dst.i_chroma == VLC_CODEC_I440) ||
        (v_src.i_chroma == VLC_CODEC_J444 && v_dst.i_chroma == VLC_CODEC_I444))
        v_dst_cmp.i_chroma = v_src.i_chroma;

    const bool convert = memcmp(&v_src, &v_dst_cmp, sizeof(v_src)) != 0;
383 384 385
    if (!convert)
        return;

386 387
    msg_Err(vd, "A filter to adapt decoder to display is needed");

388 389
    osys->filters = filter_chain_New(vd, "video filter2", false,
                                     FilterAllocationInit,
390
                                     FilterAllocationClean, vd);
391 392 393 394 395 396 397 398 399
    assert(osys->filters); /* TODO critical */

    /* */
    es_format_t src;
    es_format_InitFromVideo(&src, &v_src);

    /* */
    es_format_t dst;

400 401
    filter_t *filter;
    for (int i = 0; i < 1 + (v_dst_cmp.i_chroma != v_dst.i_chroma); i++) {
402

403 404 405 406 407 408 409 410
        es_format_InitFromVideo(&dst, i == 0 ? &v_dst : &v_dst_cmp);

        filter_chain_Reset(osys->filters, &src, &dst);
        filter = filter_chain_AppendFilter(osys->filters,
                                           NULL, NULL, &src, &dst);
        if (filter)
            break;
    }
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
    if (!filter)
    {
        msg_Err(vd, "VoutDisplayCreateRender FAILED");
        /* TODO */
        assert(0);
    }
}

static void VoutDisplayDestroyRender(vout_display_t *vd)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    if (osys->filters)
        filter_chain_Delete(osys->filters);
}

static void VoutDisplayResetRender(vout_display_t *vd)
{
    VoutDisplayDestroyRender(vd);
    VoutDisplayCreateRender(vd);
}
432
static void VoutDisplayEventMouse(vout_display_t *vd, int event, va_list args)
433 434 435
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

436 437
    vlc_mutex_lock(&osys->lock);

438
    /* */
439 440
    vlc_mouse_t m = osys->mouse.state;
    bool is_ignored = false;
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457

    switch (event) {
    case VOUT_DISPLAY_EVENT_MOUSE_STATE: {
        const int x = (int)va_arg(args, int);
        const int y = (int)va_arg(args, int);
        const int button_mask = (int)va_arg(args, int);

        vlc_mouse_Init(&m);
        m.i_x = x;
        m.i_y = y;
        m.i_pressed = button_mask;
        break;
    }
    case VOUT_DISPLAY_EVENT_MOUSE_MOVED: {
        const int x = (int)va_arg(args, int);
        const int y = (int)va_arg(args, int);
        if (x != osys->mouse.state.i_x || y != osys->mouse.state.i_y) {
458
            //msg_Dbg(vd, "VoutDisplayEvent 'mouse' @%d,%d", x, y);
459 460 461 462

            m.i_x = x;
            m.i_y = y;
            m.b_double_click = false;
463 464
        } else {
            is_ignored = true;
465 466 467 468 469 470 471 472
        }
        break;
    }
    case VOUT_DISPLAY_EVENT_MOUSE_PRESSED:
    case VOUT_DISPLAY_EVENT_MOUSE_RELEASED: {
        const int button = (int)va_arg(args, int);
        const int button_mask = 1 << button;

473 474 475 476
        /* Ignore inconsistent event */
        if ((event == VOUT_DISPLAY_EVENT_MOUSE_PRESSED  &&  (osys->mouse.state.i_pressed & button_mask)) ||
            (event == VOUT_DISPLAY_EVENT_MOUSE_RELEASED && !(osys->mouse.state.i_pressed & button_mask))) {
            is_ignored = true;
477
            break;
478
        }
479 480 481 482 483 484 485 486 487 488 489

        /* */
        msg_Dbg(vd, "VoutDisplayEvent 'mouse button' %d t=%d", button, event);

        m.b_double_click = false;
        if (event == VOUT_DISPLAY_EVENT_MOUSE_PRESSED)
            m.i_pressed |= button_mask;
        else
            m.i_pressed &= ~button_mask;
        break;
    }
490
    case VOUT_DISPLAY_EVENT_MOUSE_DOUBLE_CLICK:
491 492 493
        msg_Dbg(vd, "VoutDisplayEvent 'double click'");

        m.b_double_click = true;
494 495 496 497 498
        break;
    default:
        assert(0);
    }

499 500
    if (is_ignored) {
        vlc_mutex_unlock(&osys->lock);
501
        return;
502
    }
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520

    /* Emulate double-click if needed */
    if (!vd->info.has_double_click &&
        vlc_mouse_HasPressed(&osys->mouse.state, &m, MOUSE_BUTTON_LEFT)) {
        const mtime_t i_date = mdate();

        if (i_date - osys->mouse.last_pressed < osys->mouse.double_click_timeout ) {
            m.b_double_click = true;
            osys->mouse.last_pressed = 0;
        } else {
            osys->mouse.last_pressed = mdate();
        }
    }

    /* */
    osys->mouse.state = m;

    /* */
521
    osys->mouse.ch_activity = true;
522 523 524
    if (!vd->info.has_hide_mouse)
        osys->mouse.last_moved = mdate();

525 526 527
    /* */
    vlc_mutex_unlock(&osys->lock);

528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
    /* */
    vout_SendEventMouseVisible(osys->vout);
    vout_SendDisplayEventMouse(osys->vout, &m);
}

static void VoutDisplayEvent(vout_display_t *vd, int event, va_list args)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    switch (event) {
    case VOUT_DISPLAY_EVENT_CLOSE: {
        msg_Dbg(vd, "VoutDisplayEvent 'close'");
        vout_SendEventClose(osys->vout);
        break;
    }
    case VOUT_DISPLAY_EVENT_KEY: {
        const int key = (int)va_arg(args, int);
        msg_Dbg(vd, "VoutDisplayEvent 'key' 0x%2.2x", key);
        vout_SendEventKey(osys->vout, key);
547 548
        break;
    }
549 550 551 552 553 554 555
    case VOUT_DISPLAY_EVENT_MOUSE_STATE:
    case VOUT_DISPLAY_EVENT_MOUSE_MOVED:
    case VOUT_DISPLAY_EVENT_MOUSE_PRESSED:
    case VOUT_DISPLAY_EVENT_MOUSE_RELEASED:
    case VOUT_DISPLAY_EVENT_MOUSE_DOUBLE_CLICK:
        VoutDisplayEventMouse(vd, event, args);
        break;
556 557 558 559 560 561

    case VOUT_DISPLAY_EVENT_FULLSCREEN: {
        const int is_fullscreen = (int)va_arg(args, int);

        msg_Dbg(vd, "VoutDisplayEvent 'fullscreen' %d", is_fullscreen);

562 563 564 565 566 567
        vlc_mutex_lock(&osys->lock);
        if (!is_fullscreen != !osys->is_fullscreen) {
            osys->ch_fullscreen = true;
            osys->is_fullscreen = is_fullscreen;
        }
        vlc_mutex_unlock(&osys->lock);
568 569 570
        break;
    }

571 572 573 574 575 576 577 578 579 580 581 582 583 584
    case VOUT_DISPLAY_EVENT_ON_TOP: {
        const int is_on_top = (int)va_arg(args, int);

        msg_Dbg(vd, "VoutDisplayEvent 'on top' %d", is_on_top);

        vlc_mutex_lock(&osys->lock);
        if (!is_on_top != !osys->is_on_top) {
            osys->ch_on_top = true;
            osys->is_on_top = is_on_top;
        }
        vlc_mutex_unlock(&osys->lock);
        break;
    }

585 586 587
    case VOUT_DISPLAY_EVENT_DISPLAY_SIZE: {
        const int width  = (int)va_arg(args, int);
        const int height = (int)va_arg(args, int);
588 589 590
        const bool is_fullscreen = (bool)va_arg(args, int);
        msg_Dbg(vd, "VoutDisplayEvent 'resize' %dx%d %s",
                width, height, is_fullscreen ? "fullscreen" : "window");
591 592

        /* */
593 594
        vlc_mutex_lock(&osys->lock);

595 596 597 598
        osys->ch_display_size       = true;
        osys->display_width         = width;
        osys->display_height        = height;
        osys->display_is_fullscreen = is_fullscreen;
599
        osys->display_is_forced     = false;
600 601

        vlc_mutex_unlock(&osys->lock);
602 603 604 605 606 607 608 609
        break;
    }

    case VOUT_DISPLAY_EVENT_PICTURES_INVALID: {
        msg_Warn(vd, "VoutDisplayEvent 'pictures invalid'");

        /* */
        assert(vd->info.has_pictures_invalid);
610 611

        vlc_mutex_lock(&osys->lock);
612
        osys->reset_pictures = true;
613
        vlc_mutex_unlock(&osys->lock);
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
        break;
    }
    default:
        msg_Err(vd, "VoutDisplayEvent received event %d", event);
        /* TODO add an assert when all event are handled */
        break;
    }
}

static vout_window_t *VoutDisplayNewWindow(vout_display_t *vd, const vout_window_cfg_t *cfg)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    return vout_NewDisplayWindow(osys->vout, vd, cfg);
}
static void VoutDisplayDelWindow(vout_display_t *vd, vout_window_t *window)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    vout_DeleteDisplayWindow(osys->vout, vd, window);
}

636
void vout_ManageDisplay(vout_display_t *vd, bool allow_reset_pictures)
637 638 639 640 641 642 643
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    vout_display_Manage(vd);

    /* Handle mouse timeout */
    const mtime_t date = mdate();
644 645 646 647
    bool  hide_mouse = false;

    vlc_mutex_lock(&osys->lock);

648 649
    if (!osys->mouse.is_hidden &&
        osys->mouse.last_moved + osys->mouse.hide_timeout < date) {
650 651 652 653 654 655 656 657
        osys->mouse.is_hidden = hide_mouse = true;
    } else if (osys->mouse.ch_activity) {
        osys->mouse.is_hidden = false;
    }
    osys->mouse.ch_activity = false;
    vlc_mutex_unlock(&osys->lock);

    if (hide_mouse) {
658 659 660 661 662 663 664
        if (!vd->info.has_hide_mouse) {
            msg_Dbg(vd, "auto hidding mouse");
            vout_display_Control(vd, VOUT_DISPLAY_HIDE_MOUSE);
        }
        vout_SendEventMouseHidden(osys->vout);
    }

665
    bool reset_render = false;
666
    for (;;) {
667 668 669 670 671 672 673

        vlc_mutex_lock(&osys->lock);

        bool ch_fullscreen  = osys->ch_fullscreen;
        bool is_fullscreen  = osys->is_fullscreen;
        osys->ch_fullscreen = false;

674 675 676 677
        bool ch_on_top  = osys->ch_on_top;
        bool is_on_top  = osys->is_on_top;
        osys->ch_on_top = false;

678 679 680 681
        bool ch_display_size       = osys->ch_display_size;
        int  display_width         = osys->display_width;
        int  display_height        = osys->display_height;
        bool display_is_fullscreen = osys->display_is_fullscreen;
682
        bool display_is_forced     = osys->display_is_forced;
683 684
        osys->ch_display_size = false;

685 686 687 688 689 690 691
        bool reset_pictures;
        if (allow_reset_pictures) {
            reset_pictures = osys->reset_pictures;
            osys->reset_pictures = false;
        } else {
            reset_pictures = false;
        }
692 693 694 695 696 697

        vlc_mutex_unlock(&osys->lock);

        if (!ch_fullscreen &&
            !ch_display_size &&
            !reset_pictures &&
698 699
            !osys->ch_display_filled &&
            !osys->ch_zoom &&
700
            !ch_on_top &&
701 702 703 704 705
            !osys->ch_sar &&
            !osys->ch_crop)
            break;

        /* */
706
        if (ch_fullscreen) {
707 708
            vout_display_cfg_t cfg = osys->cfg;

709
            cfg.is_fullscreen  = is_fullscreen;
710 711 712 713 714
            cfg.display.width  = cfg.is_fullscreen ? 0 : osys->width_saved;
            cfg.display.height = cfg.is_fullscreen ? 0 : osys->height_saved;

            if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_FULLSCREEN, &cfg)) {
                msg_Err(vd, "Failed to set fullscreen");
715
                is_fullscreen = osys->cfg.is_fullscreen;
716
            }
717
            osys->cfg.is_fullscreen = is_fullscreen;
718 719 720 721 722 723

            /* */
            vout_SendEventFullscreen(osys->vout, osys->cfg.is_fullscreen);
        }

        /* */
724
        if (ch_display_size) {
725
            vout_display_cfg_t cfg = osys->cfg;
726 727
            cfg.display.width  = display_width;
            cfg.display.height = display_height;
728

729
            if (!cfg.is_fullscreen != !display_is_fullscreen ||
730
                vout_display_Control(vd, VOUT_DISPLAY_CHANGE_DISPLAY_SIZE, &cfg, display_is_forced)) {
731
                if (!cfg.is_fullscreen == !display_is_fullscreen)
732
                    msg_Err(vd, "Failed to resize display");
733 734

                /* We ignore the resized */
735 736
                display_width  = osys->cfg.display.width;
                display_height = osys->cfg.display.height;
737
            }
738 739
            osys->cfg.display.width  = display_width;
            osys->cfg.display.height = display_height;
740

741 742 743
            if (!display_is_fullscreen) {
                osys->width_saved  = display_width;
                osys->height_saved = display_height;
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
            }
        }
        /* */
        if (osys->ch_display_filled) {
            vout_display_cfg_t cfg = osys->cfg;

            cfg.is_display_filled = osys->is_display_filled;

            if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_DISPLAY_FILLED, &cfg)) {
                msg_Err(vd, "Failed to change display filled state");
                osys->is_display_filled = osys->cfg.is_display_filled;
            }
            osys->cfg.is_display_filled = osys->is_display_filled;
            osys->ch_display_filled = false;

            vout_SendEventDisplayFilled(osys->vout, osys->cfg.is_display_filled);
        }
        /* */
        if (osys->ch_zoom) {
            vout_display_cfg_t cfg = osys->cfg;

            cfg.zoom.num = osys->zoom.num;
            cfg.zoom.den = osys->zoom.den;

            if (10 * cfg.zoom.num <= cfg.zoom.den) {
                cfg.zoom.num = 1;
                cfg.zoom.den = 10;
            } else if (cfg.zoom.num >= 10 * cfg.zoom.den) {
                cfg.zoom.num = 10;
                cfg.zoom.den = 1;
            }

            if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_ZOOM, &cfg)) {
                msg_Err(vd, "Failed to change zoom");
                osys->zoom.num = osys->cfg.zoom.num;
                osys->zoom.den = osys->cfg.zoom.den;
            } else if (cfg.is_display_filled) {
781 782 783
                const int display_width  = (int64_t)vd->source.i_width  * osys->zoom.num / osys->zoom.den;
                const int display_height = (int64_t)vd->source.i_height * osys->zoom.num / osys->zoom.den;

784 785 786 787 788 789 790 791 792
                vlc_mutex_lock(&osys->lock);

                osys->ch_display_size       = true;
                osys->display_width         = display_width;
                osys->display_height        = display_height;
                osys->display_is_fullscreen = osys->cfg.is_fullscreen;
                osys->display_is_forced     = true;

                vlc_mutex_unlock(&osys->lock);
793 794 795 796 797 798 799 800 801
            }

            osys->cfg.zoom.num = osys->zoom.num;
            osys->cfg.zoom.den = osys->zoom.den;
            osys->ch_zoom = false;

            vout_SendEventZoom(osys->vout, osys->cfg.zoom.num, osys->cfg.zoom.den);
        }
        /* */
802
        if (ch_on_top) {
803 804 805 806
            if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_ON_TOP, is_on_top)) {
                msg_Err(vd, "Failed to set on top");
                is_on_top = osys->is_on_top_initial;
            }
807
            osys->is_on_top_initial = is_on_top;
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904

            /* */
            vout_SendEventOnTop(osys->vout, osys->is_on_top_initial);
        }
        /* */
        if (osys->ch_sar) {
            video_format_t source = vd->source;

            if (osys->sar.num > 0 && osys->sar.den > 0) {
                source.i_sar_num = osys->sar.num;
                source.i_sar_den = osys->sar.den;
            } else {
                source.i_sar_num = osys->source.i_sar_num;
                source.i_sar_den = osys->source.i_sar_den;
            }

            if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_SOURCE_ASPECT, &source)) {
                /* There nothing much we can do. The only reason a vout display
                 * does not support it is because it need the core to add black border
                 * to the video for it.
                 * TODO add black borders ?
                 */
                msg_Err(vd, "Failed to change source AR");
                source = vd->source;
            }
            vd->source = source;
            osys->sar.num = source.i_sar_num;
            osys->sar.den = source.i_sar_den;
            osys->ch_sar  = false;

            /* */
            if (osys->sar.num == osys->source.i_sar_num &&
                osys->sar.den == osys->source.i_sar_den)
            {
                vout_SendEventSourceAspect(osys->vout, 0, 0);
            }
            else
            {
                unsigned dar_num, dar_den;
                vlc_ureduce( &dar_num, &dar_den,
                             osys->sar.num * vd->source.i_visible_width,
                             osys->sar.den * vd->source.i_visible_height,
                             65536);
                vout_SendEventSourceAspect(osys->vout, dar_num, dar_den);
            }
        }
        /* */
        if (osys->ch_crop) {
            video_format_t source = vd->source;
            unsigned crop_num = osys->crop.num;
            unsigned crop_den = osys->crop.den;

            source.i_x_offset       = osys->crop.x;
            source.i_y_offset       = osys->crop.y;
            source.i_visible_width  = osys->crop.width;
            source.i_visible_height = osys->crop.height;

            /* */
            const bool is_valid = source.i_x_offset < source.i_width &&
                                  source.i_y_offset < source.i_height &&
                                  source.i_x_offset + source.i_visible_width  <= source.i_width &&
                                  source.i_y_offset + source.i_visible_height <= source.i_height &&
                                  source.i_visible_width > 0 && source.i_visible_height > 0;

            if (!is_valid || vout_display_Control(vd, VOUT_DISPLAY_CHANGE_SOURCE_CROP, &source)) {
                if (is_valid)
                    msg_Err(vd, "Failed to change source crop TODO implement crop at core");
                else
                    msg_Err(vd, "Invalid crop requested");

                source = vd->source;
                crop_num = osys->crop_saved.num;
                crop_den = osys->crop_saved.den;
                /* FIXME implement cropping in the core if not supported by the
                 * vout module (easy)
                 */
            }
            vd->source = source;
            osys->crop.x      = source.i_x_offset;
            osys->crop.y      = source.i_y_offset;
            osys->crop.width  = source.i_visible_width;
            osys->crop.height = source.i_visible_height;
            osys->crop.num    = crop_num;
            osys->crop.den    = crop_den;
            osys->ch_crop = false;

            /* TODO fix when a ratio is used (complicated). */
            const unsigned left   = osys->crop.x - osys->source.i_x_offset;
            const unsigned top    = osys->crop.y - osys->source.i_y_offset;
            const unsigned right  = osys->source.i_visible_width  - (osys->crop.width  + osys->crop.x);
            const unsigned bottom = osys->source.i_visible_height - (osys->crop.height + osys->crop.y);
            vout_SendEventSourceCrop(osys->vout,
                                     osys->crop.num, osys->crop.den,
                                     left, top, right, bottom);
        }

        /* */
905
        if (reset_pictures) {
906 907 908 909
            if (vout_display_Control(vd, VOUT_DISPLAY_RESET_PICTURES)) {
                /* FIXME what to do here ? */
                msg_Err(vd, "Failed to reset pictures (probably fatal)");
            }
910
            reset_render = true;
911 912
        }
    }
913
    if (reset_render)
914 915 916 917 918 919 920
        VoutDisplayResetRender(vd);
}

bool vout_AreDisplayPicturesInvalid(vout_display_t *vd)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

921 922 923 924 925
    vlc_mutex_lock(&osys->lock);
    const bool reset_pictures = osys->reset_pictures;
    vlc_mutex_unlock(&osys->lock);

    return reset_pictures;
926 927 928 929 930 931 932 933 934 935 936 937 938
}

bool vout_IsDisplayFiltered(vout_display_t *vd)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    return osys->filters != NULL;
}

picture_t *vout_FilterDisplay(vout_display_t *vd, picture_t *picture)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

939
    assert(osys->filters);
940 941 942 943 944 945 946
    return filter_chain_VideoFilter(osys->filters, picture);
}

void vout_SetDisplayFullscreen(vout_display_t *vd, bool is_fullscreen)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

947
    vlc_mutex_lock(&osys->lock);
948 949 950 951
    if (!osys->is_fullscreen != !is_fullscreen) {
        osys->ch_fullscreen = true;
        osys->is_fullscreen = is_fullscreen;
    }
952
    vlc_mutex_unlock(&osys->lock);
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
}

void vout_SetDisplayFilled(vout_display_t *vd, bool is_filled)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    if (!osys->is_display_filled != !is_filled) {
        osys->ch_display_filled = true;
        osys->is_display_filled = is_filled;
    }
}

void vout_SetDisplayZoom(vout_display_t *vd, int num, int den)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    if (osys->zoom.num != num || osys->zoom.den != den) {
        osys->ch_zoom = true;
        osys->zoom.num = num;
        osys->zoom.den = den;
    }
}
void vout_SetDisplayOnTop(vout_display_t *vd, bool is_on_top)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

979
    vlc_mutex_lock(&osys->lock);
980 981 982 983
    if (!osys->is_on_top != !is_on_top) {
        osys->ch_on_top = true;
        osys->is_on_top = is_on_top;
    }
984
    vlc_mutex_unlock(&osys->lock);
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
}
void vout_SetDisplayAspect(vout_display_t *vd, unsigned sar_num, unsigned sar_den)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    if (osys->sar.num != sar_num || osys->sar.den != sar_den) {
        osys->ch_sar = true;
        osys->sar.num = sar_num;
        osys->sar.den = sar_den;
    }
}
void vout_SetDisplayCrop(vout_display_t *vd,
                         unsigned crop_num, unsigned crop_den,
                         unsigned x, unsigned y, unsigned width, unsigned height)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    if (osys->crop.x != x || osys->crop.y != y ||
        osys->crop.width  != width || osys->crop.height != height) {

        osys->crop.x      = x;
        osys->crop.y      = y;
        osys->crop.width  = width;
        osys->crop.height = height;
        osys->crop.num    = crop_num;
        osys->crop.den    = crop_den;

        osys->ch_crop = true;
    }
}


static vout_display_t *DisplayNew(vout_thread_t *vout,
                                  const video_format_t *source_org,
                                  const vout_display_state_t *state,
                                  const char *module,
                                  bool is_wrapper, vout_display_t *wrapper,
                                  mtime_t double_click_timeout,
                                  mtime_t hide_timeout,
                                  const vout_display_owner_t *owner_ptr)
{
    /* */
    vout_display_owner_sys_t *osys = calloc(1, sizeof(*osys));
    vout_display_cfg_t *cfg = &osys->cfg;

    *cfg = state->cfg;
    osys->is_on_top_initial = state->is_on_top;;
    osys->sar_initial.num = state->sar.num;
    osys->sar_initial.den = state->sar.den;
    vout_display_GetDefaultDisplaySize(&cfg->display.width, &cfg->display.height,
                                       source_org, cfg);

    osys->vout = vout;
    osys->is_wrapper = is_wrapper;
    osys->wrapper = wrapper;

1041 1042
    vlc_mutex_init(&osys->lock);

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
    vlc_mouse_Init(&osys->mouse.state);
    osys->mouse.last_moved = mdate();
    osys->mouse.double_click_timeout = double_click_timeout;
    osys->mouse.hide_timeout = hide_timeout;
    osys->is_fullscreen  = cfg->is_fullscreen;
    osys->width_saved    =
    osys->display_width  = cfg->display.width;
    osys->height_saved   =
    osys->display_height = cfg->display.height;
    osys->is_display_filled = cfg->is_display_filled;
    osys->zoom.num = cfg->zoom.num;
    osys->zoom.den = cfg->zoom.den;

    osys->source = *source_org;

    video_format_t source = *source_org;

    source.i_x_offset =
    osys->crop.x  = 0;
    source.i_y_offset =
    osys->crop.y  = 0;
    source.i_visible_width  =
    osys->crop.width    = source.i_width;
    source.i_visible_height =
    osys->crop.height   = source.i_height;
    osys->crop_saved.num = 0;
    osys->crop_saved.den = 0;
    osys->crop.num = 0;
    osys->crop.den = 0;

    osys->sar.num = osys->sar_initial.num ? osys->sar_initial.num : source.i_sar_num;
    osys->sar.den = osys->sar_initial.den ? osys->sar_initial.den : source.i_sar_den;

    vout_display_owner_t owner;
    if (owner_ptr) {
        owner = *owner_ptr;
    } else {
        owner.event      = VoutDisplayEvent;
        owner.window_new = VoutDisplayNewWindow;
        owner.window_del = VoutDisplayDelWindow;
    }
    owner.sys = osys;

    /* */
    vout_display_t *p_display = vout_display_New(VLC_OBJECT(vout),
                                                 module, !is_wrapper,
                                                 &source, cfg, &owner);
    if (!p_display) {
        free(osys);
        return NULL;
    }

    VoutDisplayCreateRender(p_display);

    /* Setup delayed request */
    if (osys->sar.num != source_org->i_sar_num ||
        osys->sar.den != source_org->i_sar_den)
        osys->ch_sar = true;
    if (osys->is_on_top)
        osys->ch_on_top = true;
    if (osys->crop.x      != source_org->i_x_offset ||
        osys->crop.y      != source_org->i_y_offset ||
        osys->crop.width  != source_org->i_visible_width ||
        osys->crop.height != source_org->i_visible_height)
        osys->ch_crop = true;

    return p_display;
}

void vout_DeleteDisplay(vout_display_t *vd, vout_display_state_t *state)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    if (state) {
        if (!osys->is_wrapper )
            state->cfg = osys->cfg;
        state->is_on_top = osys->is_on_top_initial;
        state->sar.num   = osys->sar_initial.num;
        state->sar.den   = osys->sar_initial.den;
    }

    VoutDisplayDestroyRender(vd);
    if (osys->is_wrapper)
        SplitterClose(vd);
    vout_display_Delete(vd);
1128
    vlc_mutex_destroy(&osys->lock);
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
    free(osys);
}

/*****************************************************************************
 *
 *****************************************************************************/
vout_display_t *vout_NewDisplay(vout_thread_t *vout,
                                const video_format_t *source,
                                const vout_display_state_t *state,
                                const char *module,
                                mtime_t double_click_timeout,
                                mtime_t hide_timeout)
{
    return DisplayNew(vout, source, state, module, false, NULL,
                      double_click_timeout, hide_timeout, NULL);
}

static void SplitterClose(vout_display_t *vd)
{
1148
    VLC_UNUSED(vd);
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
    assert(0);
}

#if 0
/*****************************************************************************
 *
 *****************************************************************************/
struct vout_display_sys_t {
    video_splitter_t *splitter;

    /* */
    int            count;
    picture_t      **picture;
    vout_display_t **display;
};
struct video_splitter_owner_t {
    vout_display_t *wrapper;
};

static vout_window_t *SplitterNewWindow(vout_display_t *vd, const vout_window_cfg_t *cfg_ptr)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    vout_window_cfg_t cfg = *cfg_ptr;
    cfg.is_standalone = true;
    cfg.x += 0;//output->window.i_x; FIXME
    cfg.y += 0;//output->window.i_y;

    return vout_NewDisplayWindow(osys->vout, vd, &cfg);
}
static void SplitterDelWindow(vout_display_t *vd, vout_window_t *window)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    vout_DeleteDisplayWindow(osys->vout, vd, window);
}
static void SplitterEvent(vout_display_t *vd, int event, va_list args)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    switch (event) {
#if 0
    case VOUT_DISPLAY_EVENT_MOUSE_STATE:
    case VOUT_DISPLAY_EVENT_MOUSE_MOVED:
    case VOUT_DISPLAY_EVENT_MOUSE_PRESSED:
    case VOUT_DISPLAY_EVENT_MOUSE_RELEASED:
        /* TODO */
        break;
#endif
    case VOUT_DISPLAY_EVENT_MOUSE_DOUBLE_CLICK:
    case VOUT_DISPLAY_EVENT_KEY:
    case VOUT_DISPLAY_EVENT_CLOSE:
    case VOUT_DISPLAY_EVENT_FULLSCREEN:
    case VOUT_DISPLAY_EVENT_DISPLAY_SIZE:
    case VOUT_DISPLAY_EVENT_PICTURES_INVALID:
        VoutDisplayEvent(vd, event, args);
        break;

    default:
        msg_Err(vd, "SplitterEvent TODO");
        break;
    }
}

static picture_t *SplitterGet(vout_display_t *vd)
{
    /* TODO pool ? */
    return picture_NewFromFormat(&vd->fmt);
}
static void SplitterPrepare(vout_display_t *vd, picture_t *picture)
{
    vout_display_sys_t *sys = vd->sys;

    picture_Hold(picture);

    if (video_splitter_Filter(sys->splitter, sys->picture, picture)) {
        for (int i = 0; i < sys->count; i++)
            sys->picture[i] = NULL;
        picture_Release(picture);
        return;
    }

    for (int i = 0; i < sys->count; i++) {
        /* */
1233
        /* FIXME now vout_FilterDisplay already return a direct buffer FIXME */
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
        sys->picture[i] = vout_FilterDisplay(sys->display[i], sys->picture[i]);
        if (!sys->picture[i])
            continue;

        /* */
        picture_t *direct = vout_display_Get(sys->display[i]);
        if (!direct) {
            msg_Err(vd, "Failed to get a direct buffer");
            picture_Release(sys->picture[i]);
            sys->picture[i] = NULL;
            continue;
        }

        /* FIXME not always needed (easy when there is a osys->filters) */
        picture_Copy(direct, sys->picture[i]);
        picture_Release(sys->picture[i]);
        sys->picture[i] = direct;

        vout_display_Prepare(sys->display[i], sys->picture[i]);
    }
}
static void SplitterDisplay(vout_display_t *vd, picture_t *picture)
{
    vout_display_sys_t *sys = vd->sys;

    for (int i = 0; i < sys->count; i++) {
        if (sys->picture[i])
            vout_display_Display(sys->display[i], sys->picture[i]);
    }
    picture_Release(picture);
}
static int SplitterControl(vout_display_t *vd, int query, va_list args)
{
    return VLC_EGENERIC;
}
static void SplitterManage(vout_display_t *vd)
{
    vout_display_sys_t *sys = vd->sys;

    for (int i = 0; i < sys->count; i++)
        vout_ManageDisplay(sys->display[i]);
}

static int SplitterPictureNew(video_splitter_t *splitter, picture_t *picture[])
{
    vout_display_sys_t *wsys = splitter->p_owner->wrapper->sys;

    for (int i = 0; i < wsys->count; i++) {
        /* TODO pool ? */
        picture[i] = picture_NewFromFormat(&wsys->display[i]->source);
        if (!picture[i]) {
            for (int j = 0; j < i; j++)
                picture_Release(picture[j]);
            return VLC_EGENERIC;
        }
    }
    return VLC_SUCCESS;
}
static void SplitterPictureDel(video_splitter_t *splitter, picture_t *picture[])
{
    vout_display_sys_t *wsys = splitter->p_owner->wrapper->sys;

    for (int i = 0; i < wsys->count; i++)
        picture_Release(picture[i]);
}
static void SplitterClose(vout_display_t *vd)
{
    vout_display_sys_t *sys = vd->sys;

    /* */
    video_splitter_t *splitter = sys->splitter;
    free(splitter->p_owner);
    video_splitter_Delete(splitter);

    /* */
    for (int i = 0; i < sys->count; i++)
        vout_DeleteDisplay(sys->display[i], NULL);
    TAB_CLEAN(sys->count, sys->display);
    free(sys->picture);

    free(sys);
}

vout_display_t *vout_NewSplitter(vout_thread_t *vout,
                                 const video_format_t *source,
                                 const vout_display_state_t *state,
                                 const char *module,
                                 const char *splitter_module,
                                 mtime_t double_click_timeout,
                                 mtime_t hide_timeout)
{
    video_splitter_t *splitter =
        video_splitter_New(VLC_OBJECT(vout), splitter_module, source);
    if (!splitter)
        return NULL;

    /* */
    vout_display_t *wrapper =
        DisplayNew(vout, source, state, module, true, NULL,
                    double_click_timeout, hide_timeout, NULL);
    if (!wrapper) {
        video_splitter_Delete(splitter);
        return NULL;
    }
    vout_display_sys_t *sys = malloc(sizeof(*sys));
    if (!sys)
        abort();
    sys->picture = calloc(splitter->i_output, sizeof(*sys->picture));
    if (!sys->picture )
        abort();
    sys->splitter = splitter;

    wrapper->get     = SplitterGet;
    wrapper->prepare = SplitterPrepare;
    wrapper->display = SplitterDisplay;
    wrapper->control = SplitterControl;
    wrapper->manage  = SplitterManage;
    wrapper->sys     = sys;

    /* */
    video_splitter_owner_t *owner = malloc(sizeof(*owner));
    if (!owner)
        abort();
    owner->wrapper = wrapper;
    splitter->p_owner = owner;
    splitter->pf_picture_new = SplitterPictureNew;
    splitter->pf_picture_del = SplitterPictureDel;

    /* */
    TAB_INIT(sys->count, sys->display);
    for (int i = 0; i < splitter->i_output; i++) {
        vout_display_owner_t owner;

        owner.event      = SplitterEvent;
        owner.window_new = SplitterNewWindow;
        owner.window_del = SplitterDelWindow;

        const video_splitter_output_t *output = &splitter->p_output[i];
        vout_display_state_t ostate;

        memset(&ostate, 0, sizeof(ostate));
        ostate.cfg.is_fullscreen = false;
        ostate.cfg.display = state->cfg.display;
        ostate.cfg.align.horizontal = 0; /* TODO */
        ostate.cfg.align.vertical = 0; /* TODO */
        ostate.cfg.is_display_filled = true;
        ostate.cfg.zoom.num = 1;
        ostate.cfg.zoom.den = 1;

        vout_display_t *vd = DisplayNew(vout, &output->fmt, &ostate,
                                           output->psz_module ? output->psz_module : module,
                                           false, wrapper,
                                           double_click_timeout, hide_timeout, &owner);
        if (!vd) {
            vout_DeleteDisplay(wrapper, NULL);
            return NULL;
        }
        TAB_APPEND(sys->count, sys->display, vd);
    }

    return wrapper;
}
#endif

/*****************************************************************************
 * TODO move out
 *****************************************************************************/
#include "vout_internal.h"
void vout_SendDisplayEventMouse(vout_thread_t *vout, const vlc_mouse_t *m)
{
    if (vlc_mouse_HasMoved(&vout->p->mouse, m)) {
        vout_SendEventMouseMoved(vout, m->i_x, m->i_y);
    }
    if (vlc_mouse_HasButton(&vout->p->mouse, m)) {
        static const int buttons[] = {
            MOUSE_BUTTON_LEFT,
            MOUSE_BUTTON_CENTER,
            MOUSE_BUTTON_RIGHT,
            MOUSE_BUTTON_WHEEL_UP,
            MOUSE_BUTTON_WHEEL_DOWN,
            -1
        };
        for (int i = 0; buttons[i] >= 0; i++) {
            const int button = buttons[i];
            if (vlc_mouse_HasPressed(&vout->p->mouse, m, button))
                vout_SendEventMousePressed(vout, button);
            else if (vlc_mouse_HasReleased(&vout->p->mouse, m, button))
                vout_SendEventMouseReleased(vout, button);
        }
    }
    if (m->b_double_click)
        vout_SendEventMouseDoubleClick(vout);
    vout->p->mouse = *m;
}
vout_window_t * vout_NewDisplayWindow(vout_thread_t *vout, vout_display_t *vd, const vout_window_cfg_t *cfg)
{
1430
    VLC_UNUSED(vd);
1431 1432 1433 1434 1435 1436 1437 1438 1439
    vout_window_cfg_t cfg_override = *cfg;

    if( !config_GetInt( vout, "embedded-video" ) )
        cfg_override.is_standalone = true;

    return vout_window_New(VLC_OBJECT(vout), NULL, &cfg_override);
}
void vout_DeleteDisplayWindow(vout_thread_t *vout, vout_display_t *vd, vout_window_t *window)
{
1440 1441
    VLC_UNUSED(vout);
    VLC_UNUSED(vd);
1442 1443 1444
    vout_window_Delete(window);
}