vlcplugin_xcb.cpp 5.35 KB
Newer Older
1 2 3
/*****************************************************************************
 * vlcplugin_xcb.cpp: a VLC plugin for Mozilla (X interface)
 *****************************************************************************
4
 * Copyright (C) 2011-2012 VLC authors and VideoLAN
5 6
 * $Id$
 *
7 8
 * Authors: Cheng Sun <chengsun9@gmail.com>
 *          Sergey Radionov <RSATom@gmail.com>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * 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 "vlcplugin_xcb.h"

#include <cstring>
#include <cstdlib>

30 31 32 33 34 35 36
/* the latest version of XEmbed that we support */
#define PLUGIN_XEMBED_PROTOCOL_VERSION 1

/* Flags for _XEMBED_INFO */
#define XEMBED_MAPPED                   (1 << 0)


37 38 39 40 41 42 43 44 45 46 47 48 49
VlcPluginXcb::VlcPluginXcb(NPP instance, NPuint16_t mode) :
    VlcPluginBase(instance, mode)
{
    memset(&parent, 0, sizeof(Window));
    memset(&video, 0, sizeof(Window));
}

VlcPluginXcb::~VlcPluginXcb()
{
}

void VlcPluginXcb::set_player_window()
{
50
    libvlc_media_player_set_xwindow(get_player().get_mp(),
51 52 53 54 55
                                    (uint32_t) video);
}

void VlcPluginXcb::toggle_fullscreen()
{
56
    if (!get_options().get_enable_fs()) return;
57
    if (playlist_isplaying())
58
        libvlc_toggle_fullscreen(get_player().get_mp());
59 60 61 62
}

void VlcPluginXcb::set_fullscreen(int yes)
{
63
    if (!get_options().get_enable_fs()) return;
64
    if (playlist_isplaying())
65
        libvlc_set_fullscreen(get_player().get_mp(),yes);
66 67 68 69 70 71
}

int  VlcPluginXcb::get_fullscreen()
{
    int r = 0;
    if (playlist_isplaying())
72
        r = libvlc_get_fullscreen(get_player().get_mp());
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    return r;
}

bool VlcPluginXcb::create_windows()
{
    Display *npdisplay = ( (NPSetWindowCallbackStruct *) npwindow.ws_info )->display;
    Window socket = (Window) npwindow.window;
    conn = xcb_connect(XDisplayString(npdisplay), NULL);

    const xcb_setup_t *setup = xcb_get_setup(conn);
    xcb_screen_t *screen = xcb_setup_roots_iterator(setup).data;

    uint32_t xembed_info_buf[2] =
            { PLUGIN_XEMBED_PROTOCOL_VERSION, XEMBED_MAPPED };
    xcb_intern_atom_cookie_t cookie = xcb_intern_atom(conn, 0, strlen("_XEMBED_INFO"), "_XEMBED_INFO");
    xcb_atom_t xembed_info_atom = xcb_intern_atom_reply(conn, cookie, NULL)->atom;

    /* create windows */
    const uint32_t parent_values[] = {0x0FFFFF};
    parent = xcb_generate_id(conn);
    xcb_create_window(conn, XCB_COPY_FROM_PARENT, parent, socket,
                     /* FIXME: figure out why the window refuses to be resized larger than initial size */
                      0, 0, 20000, 20000, 0,
                      XCB_WINDOW_CLASS_INPUT_OUTPUT,
                      screen->root_visual, XCB_CW_EVENT_MASK, parent_values);
    xcb_change_property(conn, XCB_PROP_MODE_REPLACE, parent,
                        xembed_info_atom, xembed_info_atom,
                        32, 2, (void *) xembed_info_buf);
101 102

    colormap = screen->default_colormap;
103
    unsigned r = 0, g = 0, b = 0;
104
    HTMLColor2RGB(get_options().get_bg_color().c_str(), &r, &g, &b);
105 106 107 108 109 110 111 112 113
    xcb_alloc_color_reply_t *reply = xcb_alloc_color_reply(conn,
            xcb_alloc_color(conn, colormap,
                            (uint16_t) r << 8,
                            (uint16_t) g << 8,
                            (uint16_t) b << 8), NULL);
    colorpixel = reply->pixel;
    free(reply);

    const uint32_t video_values[] = {colorpixel, 0x0FFFFF};
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
    video = xcb_generate_id(conn);
    xcb_create_window(conn, 0, video, parent,
                      0, 0, 1, 1, 0,
                      XCB_WINDOW_CLASS_INPUT_OUTPUT,
                      screen->root_visual, XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK, video_values);
    xcb_map_window(conn, video);
    xcb_flush(conn);

    return true;
}

bool VlcPluginXcb::resize_windows()
{
    const uint32_t dims[] = {npwindow.width, npwindow.height};
    xcb_configure_window(conn, video, XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, dims);
    xcb_configure_window(conn, parent, XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, dims);

    xcb_query_tree_cookie_t query_cookie = xcb_query_tree(conn, video);
    xcb_query_tree_reply_t *query_reply  = xcb_query_tree_reply(conn, query_cookie, NULL);

    if (query_reply) {
        /* XXX: Make assumptions related to the window parenting structure in
           vlc/modules/video_output/x11/xcommon.c */
        xcb_window_t *children = xcb_query_tree_children(query_reply);
        xcb_configure_window(conn, children[query_reply->children_len - 1], XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, dims);
    }
    xcb_flush(conn);

    free(query_reply);
    return true;
}

bool VlcPluginXcb::destroy_windows()
{
    xcb_destroy_window(conn, parent);
149
    xcb_free_colors(conn, colormap, 0, 1, &colorpixel);
150 151
    xcb_disconnect(conn);
}