<commit_before>/*****************************************************************************
 * vlcwindowless_XCB.cpp: a VLC plugin for Mozilla (XCB windowless)
 *****************************************************************************
 * Copyright © 2012 VideoLAN
 * $Id$
 *
 * Authors: <NAME> <<EMAIL>>
 *
 * 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"

#include <xcb/xcb.h>
#include <xcb/xproto.h>

#include <cstring>
#include <cstdlib>

VlcWindowlessXCB::VlcWindowlessXCB(NPP instance, NPuint16_t mode) :
    VlcWindowlessBase(instance, mode), m_conn(0), m_screen(0)
{
    if (!(m_conn = xcb_connect(NULL, NULL)))
    {
        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);
    uint32_t        mask       = XCB_GC_BACKGROUND | XCB_GC_GRAPHICS_EXPOSURES;
    uint32_t        values[2]  = {0, colorpixel};
    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:

        xcb_gcontext_t gc;
        xcb_void_cookie_t cookie;
        xcb_generic_error_t* err;
        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;

        /* TODO Expose:

        xcb_pixmap_t pmap = xcb_generate_id(m_conn);
        xcb_create_pixmap(m_conn, 24, pmap, xgeevent->drawable, m_media_width, m_media_height);

        gc = xcb_generate_id(m_conn);
        xcb_create_gc(m_conn, gc, xgeevent->drawable, 0, NULL);
        xcb_image_put(m_conn, pmap, gc, image, left, top, 0);
        xcb_copy_area(m_conn, pmap, xgeevent->drawable, gc, 0, 0, 0, 0, m_media_width, m_media_height);
        */

        /* Push the frame in X11 */
        gc = xcb_generate_id(m_conn);
        xcb_create_gc(m_conn, gc, xgeevent->drawable, 0, NULL);

        /* 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,
                    (const uint8_t*)&m_frame_buf[0]);

        if (err = xcb_request_check(m_conn, cookie))
        {
            fprintf(stderr, "Unable to put picture into drawable. Error %d\n",
                            err->error_code);
            free(err);
        }

        /* Flush the the connection */
        xcb_flush(m_conn);
        xcb_free_gc(m_conn, gc);
    }
    return VlcWindowlessBase::handle_event(event);
}<commit_msg>Remove commented out code and minor cosmetics<commit_after>/*****************************************************************************
 * vlcwindowless_XCB.cpp: a VLC plugin for Mozilla (XCB windowless)
 *****************************************************************************
 * Copyright © 2012 VideoLAN
 * $Id$
 *
 * Authors: <NAME> <<EMAIL>>
 *
 * 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"

#include <xcb/xcb.h>
#include <xcb/xproto.h>
#include <cstring>
#include <cstdlib>

VlcWindowlessXCB::VlcWindowlessXCB(NPP instance, NPuint16_t mode) :
    VlcWindowlessBase(instance, mode), m_conn(0), m_screen(0)
{
    if (!(m_conn = xcb_connect(NULL, NULL)))
    {
        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);
    uint32_t        mask       = XCB_GC_BACKGROUND | XCB_GC_GRAPHICS_EXPOSURES;
    uint32_t        values[2]  = {0, colorpixel};
    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:

        xcb_gcontext_t gc;
        xcb_void_cookie_t cookie;
        xcb_generic_error_t *err;
        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;

        gc = xcb_generate_id(m_conn);
        xcb_create_gc(m_conn, gc, xgeevent->drawable, 0, NULL);

        /* 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,
                    (const uint8_t *)&m_frame_buf[0]);

        if (err = xcb_request_check(m_conn, cookie))
        {
            fprintf(stderr, "Unable to put picture into drawable. Error %d\n",
                            err->error_code);
            free(err);
        }

        /* Flush the the connection */
        xcb_flush(m_conn);
        xcb_free_gc(m_conn, gc);
    }
    return VlcWindowlessBase::handle_event(event);
}
{'fraction_non_alphanumeric': 0.1428283032199455, 'fraction_numerical': 0.012516402543656002, 'mean_word_length': 2.5678790061217143, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3921643', 'n_tokens_mistral': 3271, 'n_tokens_neox': 3082, 'n_words': 791}