vlcplugin_gtk.cpp 16.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
/*****************************************************************************
 * vlcplugin_gtk.cpp: a VLC plugin for Mozilla (GTK+ interface)
 *****************************************************************************
 * Copyright (C) 2002-2010 the VideoLAN team
 * $Id$
 *
 * Authors: Cheng Sun <chengsun9@gmail.com>
 *
 * 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.
 *****************************************************************************/

24 25 26 27
#include "vlcplugin_gtk.h"
#include <gdk/gdkx.h>
#include <cstring>

Cheng Sun's avatar
Cheng Sun committed
28 29
static uint32_t get_xid(GtkWidget *widget)
{
30 31 32 33 34 35 36 37
    GdkDrawable *video_drawable = gtk_widget_get_window(widget);
    return (uint32_t)gdk_x11_drawable_get_xid(video_drawable);
}

VlcPluginGtk::VlcPluginGtk(NPP instance, NPuint16_t mode) :
    VlcPluginBase(instance, mode),
    parent(NULL),
    parent_vbox(NULL),
38 39 40
    video_container(NULL),
    toolbar(NULL),
    fullscreen_win(NULL),
41 42
    is_fullscreen(false),
    is_toolbar_visible(false)
43
{
44
    memset(&video_xwindow, 0, sizeof(Window));
45 46 47 48 49 50
    GtkIconTheme *icon_theme = gtk_icon_theme_get_default();
    cone_icon = gdk_pixbuf_copy(gtk_icon_theme_load_icon(
                    icon_theme, "vlc", 128, GTK_ICON_LOOKUP_FORCE_SIZE, NULL));
    if (!cone_icon) {
        fprintf(stderr, "WARNING: could not load VLC icon\n");
    }
51 52 53 54 55 56 57 58 59
}

VlcPluginGtk::~VlcPluginGtk()
{
}

void VlcPluginGtk::set_player_window()
{
    libvlc_media_player_set_xwindow(libvlc_media_player,
60
                                    video_xwindow);
61
    libvlc_video_set_mouse_input(libvlc_media_player, 0);
62 63 64 65
}

void VlcPluginGtk::toggle_fullscreen()
{
66
    set_fullscreen(!get_fullscreen());
67 68
}

69
void VlcPluginGtk::do_set_fullscreen(bool yes)
70
{
71 72 73 74 75
    /* we have to reparent windows */
    /* note that the xid of video_container changes after reparenting */
    Display *display = get_display();
    g_signal_handler_block(video_container, video_container_size_handler_id);

Cheng Sun's avatar
Cheng Sun committed
76 77
    XUnmapWindow(display, video_xwindow);
    XReparentWindow(display, video_xwindow,
78
                    gdk_x11_get_default_root_xwindow(), 0, 0);
Cheng Sun's avatar
Cheng Sun committed
79
    if (yes) {
80 81 82 83 84
        g_object_ref(G_OBJECT(parent_vbox));
        gtk_container_remove(GTK_CONTAINER(parent), parent_vbox);
        gtk_container_add(GTK_CONTAINER(fullscreen_win), parent_vbox);
        g_object_unref(G_OBJECT(parent_vbox));
        gtk_widget_show_all(fullscreen_win);
85 86 87
        gtk_window_fullscreen(GTK_WINDOW(fullscreen_win));
    } else {
        gtk_widget_hide(fullscreen_win);
88 89 90 91
        g_object_ref(G_OBJECT(parent_vbox));
        gtk_container_remove(GTK_CONTAINER(fullscreen_win), parent_vbox);
        gtk_container_add(GTK_CONTAINER(parent), parent_vbox);
        g_object_unref(G_OBJECT(parent_vbox));
92 93
        gtk_widget_show_all(GTK_WIDGET(parent));
    }
94
    XSync(get_display(), false);
95
    XReparentWindow(display, video_xwindow, get_xid(video_container), 0, 0);
Cheng Sun's avatar
Cheng Sun committed
96

97 98
//    libvlc_set_fullscreen(libvlc_media_player, yes);
    g_signal_handler_unblock(video_container, video_container_size_handler_id);
99 100
    gtk_widget_queue_resize(video_container);
    update_controls();
101 102

    is_fullscreen = yes;
103 104
}

105 106 107 108 109 110 111 112 113 114 115
void VlcPluginGtk::set_fullscreen(int yes)
{
    if (yes == is_fullscreen) return;
    if (yes) {
        gtk_widget_show(fullscreen_win);
    } else {
        gtk_widget_hide(fullscreen_win);
    }
}

int VlcPluginGtk::get_fullscreen()
116
{
117
    return is_fullscreen;
118 119
}

120
void VlcPluginGtk::set_toolbar_visible(bool yes)
121
{
122 123 124 125 126 127 128 129 130 131 132 133
    if (yes == is_toolbar_visible) return;

    if (yes) {
        gtk_box_pack_start(GTK_BOX(parent_vbox), toolbar, false, false, 0);
        gtk_widget_show_all(toolbar);
        update_controls();
        g_object_unref(G_OBJECT(toolbar));
    } else {
        g_object_ref(G_OBJECT(toolbar));
        gtk_widget_hide(toolbar);
        gtk_container_remove(GTK_CONTAINER(parent_vbox), toolbar);
    }
134 135
    resize_windows();
    gtk_container_resize_children(GTK_CONTAINER(parent));
136
    is_toolbar_visible = yes;
137 138
}

139
bool VlcPluginGtk::get_toolbar_visible()
140
{
141
    return is_toolbar_visible;
142 143
}

144 145 146 147 148
void VlcPluginGtk::resize_video_xwindow(GdkRectangle *rect)
{
    Display *display = get_display();
    XResizeWindow(display, video_xwindow,
                  rect->width, rect->height);
149
    XSync(display, false);
150 151
}

152 153 154 155 156 157 158 159 160 161
struct tool_actions_t
{
    const gchar *stock_id;
    vlc_toolbar_clicked_t clicked;
};
static const tool_actions_t tool_actions[] = {
    {GTK_STOCK_MEDIA_PLAY, clicked_Play},
    {GTK_STOCK_MEDIA_PAUSE, clicked_Pause},
    {GTK_STOCK_MEDIA_STOP, clicked_Stop},
    {"gtk-volume-muted", clicked_Mute},
162 163
    {"gtk-volume-unmuted", clicked_Unmute},
    {GTK_STOCK_FULLSCREEN, clicked_Fullscreen}
164 165
};

Cheng Sun's avatar
Cheng Sun committed
166
static void toolbar_handler(GtkToolButton *btn, gpointer user_data)
167
{
Cheng Sun's avatar
Cheng Sun committed
168
    VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
169 170 171 172 173 174 175
    const gchar *stock_id = gtk_tool_button_get_stock_id(btn);
    for (int i = 0; i < sizeof(tool_actions)/sizeof(tool_actions_t); ++i) {
        if (!strcmp(stock_id, tool_actions[i].stock_id)) {
            plugin->control_handler(tool_actions[i].clicked);
            return;
        }
    }
Cheng Sun's avatar
Cheng Sun committed
176
    fprintf(stderr, "WARNING: No idea what toolbar button you just clicked on (%s)\n", stock_id?stock_id:"NULL");
177 178
}

179 180 181 182
static void menu_handler(GtkMenuItem *menuitem, gpointer user_data)
{
    VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
    const gchar *stock_id = gtk_menu_item_get_label(GTK_MENU_ITEM(menuitem));
183 184 185 186
    if (!strcmp(stock_id, VLCPLUGINGTK_MENU_TOOLBAR)) {
        plugin->set_toolbar_visible(gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem)));
        return;
    }
187 188 189 190 191 192 193 194 195
    for (int i = 0; i < sizeof(tool_actions)/sizeof(tool_actions_t); ++i) {
        if (!strcmp(stock_id, tool_actions[i].stock_id)) {
            plugin->control_handler(tool_actions[i].clicked);
            return;
        }
    }
    fprintf(stderr, "WARNING: No idea what menu item you just clicked on (%s)\n", stock_id?stock_id:"NULL");
}

196
void VlcPluginGtk::popup_menu()
197 198
{
    /* construct menu */
199
    GtkWidget *popupmenu = gtk_menu_new();
200 201 202 203 204 205 206 207
    GtkWidget *menuitem;

    /* play/pause */
    menuitem = gtk_image_menu_item_new_from_stock(
                        playlist_isplaying() ?
                        GTK_STOCK_MEDIA_PAUSE :
                        GTK_STOCK_MEDIA_PLAY, NULL);
    g_signal_connect(G_OBJECT(menuitem), "activate", G_CALLBACK(menu_handler), this);
208
    gtk_menu_shell_append(GTK_MENU_SHELL(popupmenu), menuitem);
209 210 211 212
    /* stop */
    menuitem = gtk_image_menu_item_new_from_stock(
                                GTK_STOCK_MEDIA_STOP, NULL);
    g_signal_connect(G_OBJECT(menuitem), "activate", G_CALLBACK(menu_handler), this);
213
    gtk_menu_shell_append(GTK_MENU_SHELL(popupmenu), menuitem);
214 215 216 217 218
    /* set fullscreen */
    menuitem = gtk_image_menu_item_new_from_stock(
                                GTK_STOCK_FULLSCREEN, NULL);
    g_signal_connect(G_OBJECT(menuitem), "activate", G_CALLBACK(menu_handler), this);
    gtk_menu_shell_append(GTK_MENU_SHELL(popupmenu), menuitem);
219 220 221 222 223 224
    /* toolbar */
    menuitem = gtk_check_menu_item_new_with_label(
                                VLCPLUGINGTK_MENU_TOOLBAR);
    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), get_toolbar_visible());
    g_signal_connect(G_OBJECT(menuitem), "activate", G_CALLBACK(menu_handler), this);
    gtk_menu_shell_append(GTK_MENU_SHELL(popupmenu), menuitem);
225

226
    /* show menu */
227
    gtk_widget_show_all(popupmenu);
228
    gtk_menu_attach_to_widget(GTK_MENU(popupmenu), video_container, NULL);
229 230
    gtk_menu_popup(GTK_MENU(popupmenu), NULL, NULL, NULL, NULL,
                   0, gtk_get_current_event_time());
231 232 233 234 235 236
}

static bool video_button_handler(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
{
    VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
    if (event->button == 3 && event->type == GDK_BUTTON_PRESS) {
237
        plugin->popup_menu();
238 239
        return true;
    }
240 241 242
    if (event->button == 1 && event->type == GDK_2BUTTON_PRESS) {
        plugin->toggle_fullscreen();
    }
243 244 245
    return false;
}

246 247
static bool video_popup_handler(GtkWidget *widget, gpointer user_data)
{
248
    VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
249
    plugin->popup_menu();
250 251 252
    return true;
}

253 254
static bool video_size_handler(GtkWidget *widget, GdkRectangle *rect, gpointer user_data)
{
255 256 257 258 259
    VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
    plugin->resize_video_xwindow(rect);
    return true;
}

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
static bool video_expose_handler(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
    VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
    GdkEventExpose *event_expose = (GdkEventExpose *) event;
    GdkWindow *window = event_expose->window;
    GdkPixbuf *cone_icon = plugin->cone_icon;
    if (!cone_icon) return false;

    int winwidth   = gdk_window_get_width(window),
        winheight  = gdk_window_get_height(window),
        iconwidth  = gdk_pixbuf_get_width(cone_icon),
        iconheight = gdk_pixbuf_get_height(cone_icon);
    cairo_t *cr = gdk_cairo_create(window);
    gdk_cairo_set_source_pixbuf(cr, cone_icon,
            (winwidth-iconwidth)/2.0, (winheight-iconheight)/2.0);
    gdk_cairo_region(cr, event_expose->region);
    cairo_fill(cr);

    return true;
}

Cheng Sun's avatar
Cheng Sun committed
281
static bool time_slider_handler(GtkRange *range, GtkScrollType scroll, gdouble value, gpointer user_data)
282
{
Cheng Sun's avatar
Cheng Sun committed
283
    VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
284 285 286 287
    libvlc_media_player_set_position(plugin->getMD(), value/100.0);
    return false;
}

Cheng Sun's avatar
Cheng Sun committed
288
static bool vol_slider_handler(GtkRange *range, GtkScrollType scroll, gdouble value, gpointer user_data)
289
{
Cheng Sun's avatar
Cheng Sun committed
290
    VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
291 292
    libvlc_audio_set_volume(plugin->getMD(), value);
    return false;
293 294
}

295 296 297
static void fullscreen_win_visibility_handler(GtkWidget *widget, gpointer user_data)
{
    VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
298
    plugin->do_set_fullscreen(gtk_widget_get_visible(widget));
299 300
}

301 302
void VlcPluginGtk::update_controls()
{
303 304 305 306 307 308 309 310
    if (libvlc_media_player) {
        if (!libvlc_media_player_is_playing(libvlc_media_player)) {
            XUnmapWindow(display, video_xwindow);
        } else {
            XMapWindow(display, video_xwindow);
        }
    }

311 312 313 314 315 316 317 318 319 320 321 322 323 324
    if (get_toolbar_visible()) {
        GtkToolItem *toolbutton;

        /* play/pause button */
        const gchar *stock_id = playlist_isplaying() ? GTK_STOCK_MEDIA_PAUSE : GTK_STOCK_MEDIA_PLAY;
        toolbutton = gtk_toolbar_get_nth_item(GTK_TOOLBAR(toolbar), 0);
        if (strcmp(gtk_tool_button_get_stock_id(GTK_TOOL_BUTTON(toolbutton)), stock_id)) {
            gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(toolbutton), stock_id);
            /* work around firefox not displaying the icon properly after change */
            g_object_ref(toolbutton);
            gtk_container_remove(GTK_CONTAINER(toolbar), GTK_WIDGET(toolbutton));
            gtk_toolbar_insert(GTK_TOOLBAR(toolbar), toolbutton, 0);
            g_object_unref(toolbutton);
        }
325

326 327 328 329 330 331 332 333 334 335
        /* time slider */
        if (!libvlc_media_player ||
                !libvlc_media_player_is_seekable(libvlc_media_player)) {
            gtk_widget_set_sensitive(time_slider, false);
            gtk_range_set_value(GTK_RANGE(time_slider), 0);
        } else {
            gtk_widget_set_sensitive(time_slider, true);
            gdouble timepos = 100*libvlc_media_player_get_position(libvlc_media_player);
            gtk_range_set_value(GTK_RANGE(time_slider), timepos);
        }
336

337 338
        gtk_widget_show_all(toolbar);
    }
339 340 341 342
}

bool VlcPluginGtk::create_windows()
{
343 344
    display = ( (NPSetWindowCallbackStruct *) npwindow.ws_info )->display;

345
    Window socket = (Window) npwindow.window;
346 347
    GdkColor color_bg;
    gdk_color_parse(psz_bgcolor, &color_bg);
348 349

    parent = gtk_plug_new(socket);
350
    gtk_widget_modify_bg(parent, GTK_STATE_NORMAL, &color_bg);
351 352 353 354

    parent_vbox = gtk_vbox_new(false, 0);
    gtk_container_add(GTK_CONTAINER(parent), parent_vbox);

355
    video_container = gtk_drawing_area_new();
356
    gtk_widget_modify_bg(video_container, GTK_STATE_NORMAL, &color_bg);
357
    gtk_widget_add_events(video_container,
358 359
            GDK_BUTTON_PRESS_MASK
          | GDK_BUTTON_RELEASE_MASK);
360
    g_signal_connect(G_OBJECT(video_container), "expose-event", G_CALLBACK(video_expose_handler), this);
361 362 363
    g_signal_connect(G_OBJECT(video_container), "button-press-event", G_CALLBACK(video_button_handler), this);
    g_signal_connect(G_OBJECT(video_container), "popup-menu", G_CALLBACK(video_popup_handler), this);
    gtk_box_pack_start(GTK_BOX(parent_vbox), video_container, true, true, 0);
364 365 366

    gtk_widget_show_all(parent);

367 368
    /* fullscreen top-level */
    fullscreen_win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
369
    gtk_widget_modify_bg(fullscreen_win, GTK_STATE_NORMAL, &color_bg);
370 371 372 373 374 375 376 377 378 379
    gtk_window_set_decorated(GTK_WINDOW(fullscreen_win), false);
    g_signal_connect(G_OBJECT(fullscreen_win), "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), this);
    g_signal_connect(G_OBJECT(fullscreen_win), "show", G_CALLBACK(fullscreen_win_visibility_handler), this);
    g_signal_connect(G_OBJECT(fullscreen_win), "hide", G_CALLBACK(fullscreen_win_visibility_handler), this);

    /* actual video window */
    /* libvlc is handed this window's xid. A raw X window is used because
     * GTK+ is incapable of reparenting without changing xid
     */
    Display *display = get_display();
380 381 382 383 384
    Colormap colormap = DefaultColormap(display, DefaultScreen(display));
    bg_color.red   = color_bg.red;
    bg_color.green = color_bg.green;
    bg_color.blue  = color_bg.blue;
    XAllocColor(display, colormap, &bg_color);
385 386
    video_xwindow = XCreateSimpleWindow(display, get_xid(video_container), 0, 0,
                   1, 1,
387
                   0, bg_color.pixel, bg_color.pixel);
388 389 390 391 392 393

    /* connect video_container resizes to video_xwindow */
    video_container_size_handler_id = g_signal_connect(
                G_OBJECT(video_container), "size-allocate",
                G_CALLBACK(video_size_handler), this);
    gtk_widget_queue_resize_no_redraw(video_container);
Cheng Sun's avatar
Cheng Sun committed
394 395 396

    /*** TOOLBAR ***/

397
    toolbar = gtk_toolbar_new();
398
    g_object_ref(G_OBJECT(toolbar));
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
    gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
    GtkToolItem *toolitem;
    /* play/pause */
    toolitem = gtk_tool_button_new_from_stock(GTK_STOCK_MEDIA_PLAY);
    g_signal_connect(G_OBJECT(toolitem), "clicked", G_CALLBACK(toolbar_handler), this);
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), toolitem, -1);
    /* stop */
    toolitem = gtk_tool_button_new_from_stock(GTK_STOCK_MEDIA_STOP);
    g_signal_connect(G_OBJECT(toolitem), "clicked", G_CALLBACK(toolbar_handler), this);
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), toolitem, -1);

    /* time slider */
    toolitem = gtk_tool_item_new();
    time_slider = gtk_hscale_new_with_range(0, 100, 10);
    gtk_scale_set_draw_value(GTK_SCALE(time_slider), false);
    g_signal_connect(G_OBJECT(time_slider), "change-value", G_CALLBACK(time_slider_handler), this);
    gtk_container_add(GTK_CONTAINER(toolitem), time_slider);
    gtk_tool_item_set_expand(toolitem, true);
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), toolitem, -1);
    
    /* volume slider */
    toolitem = gtk_tool_item_new();
    GtkWidget *vol_slider = gtk_hscale_new_with_range(0, 200, 10);
    gtk_scale_set_draw_value(GTK_SCALE(vol_slider), false);
    g_signal_connect(G_OBJECT(vol_slider), "change-value", G_CALLBACK(vol_slider_handler), this);
    gtk_range_set_value(GTK_RANGE(vol_slider), 100);
    gtk_widget_set_size_request(vol_slider, 100, -1);
    gtk_container_add(GTK_CONTAINER(toolitem), vol_slider);
    gtk_tool_item_set_expand(toolitem, false);
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), toolitem, -1);

430 431 432 433 434 435 436 437 438 439 440 441 442
    return true;
}

bool VlcPluginGtk::resize_windows()
{
    GtkRequisition req;
    req.width = npwindow.width;
    req.height = npwindow.height;
    gtk_widget_size_request(parent, &req);
}

bool VlcPluginGtk::destroy_windows()
{
443
    Display *display = get_display();
444 445 446 447

    /* destroy x window */
    XDestroyWindow(display, video_xwindow);

448 449 450 451
    /* destroy GTK top-levels */
    gtk_widget_destroy(parent);
    gtk_widget_destroy(fullscreen_win);

452
    /* free colors */
453 454
    Colormap colormap = DefaultColormap(display, DefaultScreen(display));
    XFreeColors(display, colormap, &bg_color.pixel, 1, 0);
455
}