vlcwindowless_xcb.cpp 4.58 KB
Newer Older
Ludovic Fauvet's avatar
Ludovic Fauvet committed
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
/*****************************************************************************
 * vlcwindowless_XCB.cpp: a VLC plugin for Mozilla (XCB windowless)
 *****************************************************************************
 * Copyright © 2012 VideoLAN
 * $Id$
 *
 * Authors: Ludovic Fauvet <etix@videolan.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.
 *****************************************************************************/

#include "vlcwindowless_xcb.h"

26
#include <xcb/xcb.h>
Ludovic Fauvet's avatar
Ludovic Fauvet committed
27 28 29 30 31 32 33
#include <xcb/xproto.h>
#include <cstring>
#include <cstdlib>

VlcWindowlessXCB::VlcWindowlessXCB(NPP instance, NPuint16_t mode) :
    VlcWindowlessBase(instance, mode), m_conn(0), m_screen(0)
{
34
    if (!(m_conn = xcb_connect(NULL, NULL)))
Ludovic Fauvet's avatar
Ludovic Fauvet committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    {
        fprintf(stderr, "Can't connect to XCB\n");
        return;
    }

    /* Retrieve the setup */
    const xcb_setup_t *setup;
    if (!(setup = xcb_get_setup(m_conn)))
    {
        fprintf(stderr, "Can't get the XCB setup\n");
        return;
    }

    /* Get the first screen */
    m_screen = xcb_setup_roots_iterator(setup).data;
}

VlcWindowlessXCB::~VlcWindowlessXCB()
{
    xcb_disconnect(m_conn);
}

void VlcWindowlessXCB::drawBackground(xcb_drawable_t drawable)
{
    /* Obtain the background color */
    xcb_colormap_t colormap = m_screen->default_colormap;
    unsigned r = 0, g = 0, b = 0;
    HTMLColor2RGB(get_options().get_bg_color().c_str(), &r, &g, &b);
    xcb_alloc_color_reply_t *reply = xcb_alloc_color_reply(m_conn,
            xcb_alloc_color(m_conn, colormap,
                            (uint16_t) r << 8,
                            (uint16_t) g << 8,
                            (uint16_t) b << 8), NULL);
    uint32_t colorpixel = reply->pixel;
    free(reply);

    /* Prepare to fill the background */
    xcb_gcontext_t background = xcb_generate_id(m_conn);
73 74
    uint32_t        mask       = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
    uint32_t        values[2]  = {colorpixel, 0};
Ludovic Fauvet's avatar
Ludovic Fauvet committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    xcb_create_gc(m_conn, background, drawable, mask, values);
    xcb_rectangle_t rect;
    rect.x = 0;
    rect.y = 0;
    rect.width = npwindow.width;
    rect.height = npwindow.height;

    /* Fill the background */
    xcb_poly_fill_rectangle(m_conn, drawable, background, 1, &rect);
    xcb_free_gc(m_conn, background);
}

bool VlcWindowlessXCB::handle_event(void *event)
{
    XEvent *xevent = static_cast<XEvent *>(event);
    switch (xevent->type) {
    case GraphicsExpose:

93 94
        xcb_gcontext_t gc;
        xcb_void_cookie_t cookie;
95
        xcb_generic_error_t *err;
Ludovic Fauvet's avatar
Ludovic Fauvet committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        XGraphicsExposeEvent *xgeevent = reinterpret_cast<XGraphicsExposeEvent *>(xevent);

        /* Something went wrong during initialization */
        if (!m_conn || !m_screen)
            break;

        drawBackground(xgeevent->drawable);

        /* Validate video size */
        if (m_media_width == 0 || m_media_height == 0)
            break;

        /* Compute the position of the video */
        int left = (npwindow.width  - m_media_width)  / 2;
        int top  = (npwindow.height - m_media_height) / 2;

112
        gc = xcb_generate_id(m_conn);
Ludovic Fauvet's avatar
Ludovic Fauvet committed
113 114
        xcb_create_gc(m_conn, gc, xgeevent->drawable, 0, NULL);

115 116 117 118 119 120 121 122 123 124 125
        /* Push the frame in X11 */
        cookie = xcb_put_image_checked(
                    m_conn,
                    XCB_IMAGE_FORMAT_Z_PIXMAP,
                    xgeevent->drawable,
                    gc,
                    m_media_width,
                    m_media_height,
                    left, top,
                    0, 24,
                    m_media_width * m_media_height * 4,
126
                    (const uint8_t *)&m_frame_buf[0]);
127 128 129 130 131 132 133

        if (err = xcb_request_check(m_conn, cookie))
        {
            fprintf(stderr, "Unable to put picture into drawable. Error %d\n",
                            err->error_code);
            free(err);
        }
Ludovic Fauvet's avatar
Ludovic Fauvet committed
134 135 136 137 138 139 140

        /* Flush the the connection */
        xcb_flush(m_conn);
        xcb_free_gc(m_conn, gc);
    }
    return VlcWindowlessBase::handle_event(event);
}