vlcplugin_gtk.cpp 19.2 KB
Newer Older
1 2 3
/*****************************************************************************
 * vlcplugin_gtk.cpp: a VLC plugin for Mozilla (GTK+ interface)
 *****************************************************************************
4
 * Copyright (C) 2002-2012 VLC authors and VideoLAN
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * $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
#include "vlcplugin_gtk.h"
#include <gdk/gdkx.h>
26
#include <gdk/gdkkeysyms.h>
27 28
#include <cstring>

Cheng Sun's avatar
Cheng Sun committed
29 30
static uint32_t get_xid(GtkWidget *widget)
{
31 32 33 34 35 36 37 38
    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),
39
    video_container(NULL),
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
40
    fullscreen_win(NULL),
41
    toolbar(NULL),
42 43
    time_slider(NULL),
    vol_slider(NULL),
44
    is_fullscreen(false),
45
    is_toolbar_visible(false),
46
    m_timer_update_timeout( 0 )
47
{
48
    memset(&video_xwindow, 0, sizeof(Window));
49 50 51 52 53 54
    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");
    }
55 56
}

57 58 59 60 61 62 63 64 65
VlcPluginGtk::~VlcPluginGtk()
{
    {
        std::lock_guard<std::mutex> lock( m_timer_lock );
        if ( m_timer_update_timeout != 0 )
            g_source_remove( m_timer_update_timeout );
    }
}

66 67
void VlcPluginGtk::set_player_window()
{
68 69
    player().get_mp().setXwindow( video_xwindow);
    player().get_mp().setMouseInput( false );
70 71 72 73
}

void VlcPluginGtk::toggle_fullscreen()
{
74
    set_fullscreen(!get_fullscreen());
75 76
}

77
void VlcPluginGtk::do_set_fullscreen(bool yes)
78
{
79 80 81 82 83
    /* 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
84 85
    XUnmapWindow(display, video_xwindow);
    XReparentWindow(display, video_xwindow,
86
                    gdk_x11_get_default_root_xwindow(), 0, 0);
Cheng Sun's avatar
Cheng Sun committed
87
    if (yes) {
88 89 90 91 92
        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);
93 94 95
        gtk_window_fullscreen(GTK_WINDOW(fullscreen_win));
    } else {
        gtk_widget_hide(fullscreen_win);
96 97 98 99
        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));
100 101
        gtk_widget_show_all(GTK_WIDGET(parent));
    }
102
    XSync(get_display(), false);
103
    XReparentWindow(display, video_xwindow, get_xid(video_container), 0, 0);
Cheng Sun's avatar
Cheng Sun committed
104

105 106
//    libvlc_set_fullscreen(libvlc_media_player, yes);
    g_signal_handler_unblock(video_container, video_container_size_handler_id);
107 108
    gtk_widget_queue_resize(video_container);
    update_controls();
109 110

    is_fullscreen = yes;
111 112
}

113 114
void VlcPluginGtk::set_fullscreen(int yes)
{
115
    if (!get_options().get_enable_fs()) return;
116 117 118 119 120 121 122 123
    if (yes == is_fullscreen) return;
    if (yes) {
        gtk_widget_show(fullscreen_win);
    } else {
        gtk_widget_hide(fullscreen_win);
    }
}

124
bool VlcPluginGtk::get_fullscreen()
125
{
126
    return is_fullscreen;
127 128
}

129
void VlcPluginGtk::set_toolbar_visible(bool yes)
130
{
131 132 133 134 135 136 137 138 139 140 141 142
    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);
    }
143 144
    resize_windows();
    gtk_container_resize_children(GTK_CONTAINER(parent));
145
    is_toolbar_visible = yes;
146 147
}

148
bool VlcPluginGtk::get_toolbar_visible()
149
{
150
    return is_toolbar_visible;
151 152
}

153 154 155 156 157
void VlcPluginGtk::resize_video_xwindow(GdkRectangle *rect)
{
    Display *display = get_display();
    XResizeWindow(display, video_xwindow,
                  rect->width, rect->height);
158
    XSync(display, false);
159 160
}

161 162 163 164 165 166 167 168 169 170
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},
171 172
    {"gtk-volume-unmuted", clicked_Unmute},
    {GTK_STOCK_FULLSCREEN, clicked_Fullscreen}
173 174
};

Cheng Sun's avatar
Cheng Sun committed
175
static void toolbar_handler(GtkToolButton *btn, gpointer user_data)
176
{
Cheng Sun's avatar
Cheng Sun committed
177
    VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
178
    const gchar *stock_id = gtk_tool_button_get_stock_id(btn);
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
179 180 181
    for (auto a : tool_actions ) {
        if (!strcmp(stock_id, a.stock_id)) {
            plugin->control_handler(a.clicked);
182 183 184
            return;
        }
    }
Cheng Sun's avatar
Cheng Sun committed
185
    fprintf(stderr, "WARNING: No idea what toolbar button you just clicked on (%s)\n", stock_id?stock_id:"NULL");
186 187
}

188 189 190 191
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));
192 193 194 195
    if (!strcmp(stock_id, VLCPLUGINGTK_MENU_TOOLBAR)) {
        plugin->set_toolbar_visible(gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem)));
        return;
    }
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
196 197 198 199
    for (auto a : tool_actions)
    {
        if (!strcmp(stock_id, a.stock_id)) {
            plugin->control_handler(a.clicked);
200 201 202 203 204 205
            return;
        }
    }
    fprintf(stderr, "WARNING: No idea what menu item you just clicked on (%s)\n", stock_id?stock_id:"NULL");
}

206
void VlcPluginGtk::popup_menu()
207 208
{
    /* construct menu */
209
    GtkWidget *popupmenu = gtk_menu_new();
210 211 212 213
    GtkWidget *menuitem;

    /* play/pause */
    menuitem = gtk_image_menu_item_new_from_stock(
214
                        player().mlp().isPlaying() ?
215 216 217
                        GTK_STOCK_MEDIA_PAUSE :
                        GTK_STOCK_MEDIA_PLAY, NULL);
    g_signal_connect(G_OBJECT(menuitem), "activate", G_CALLBACK(menu_handler), this);
218
    gtk_menu_shell_append(GTK_MENU_SHELL(popupmenu), menuitem);
219 220 221 222
    /* 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);
223
    gtk_menu_shell_append(GTK_MENU_SHELL(popupmenu), menuitem);
224
    /* set fullscreen */
225
    if (get_options().get_enable_fs()) {
226 227 228 229 230
        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);
    }
231 232 233 234 235 236
    /* 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);
237

238
    /* show menu */
239
    gtk_widget_show_all(popupmenu);
240
    gtk_menu_attach_to_widget(GTK_MENU(popupmenu), video_container, NULL);
241 242
    gtk_menu_popup(GTK_MENU(popupmenu), NULL, NULL, NULL, NULL,
                   0, gtk_get_current_event_time());
243 244
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
245
static bool video_button_handler(GtkWidget*, GdkEventButton *event, gpointer user_data)
246 247 248
{
    VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
    if (event->button == 3 && event->type == GDK_BUTTON_PRESS) {
249
        plugin->popup_menu();
250 251
        return true;
    }
252 253 254
    if (event->button == 1 && event->type == GDK_2BUTTON_PRESS) {
        plugin->toggle_fullscreen();
    }
255 256 257
    return false;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
258
static bool video_popup_handler(GtkWidget*, gpointer user_data)
259
{
260
    VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
261
    plugin->popup_menu();
262 263 264
    return true;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
265
static bool video_size_handler(GtkWidget*, GdkRectangle *rect, gpointer user_data)
266
{
267 268 269 270 271
    VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
    plugin->resize_video_xwindow(rect);
    return true;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
272
static bool video_expose_handler(GtkWidget*, GdkEvent *event, gpointer user_data)
273 274 275 276 277 278 279
{
    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;

280 281 282 283 284 285 286 287 288
    int winwidth, winheight;
#   if GTK_CHECK_VERSION(2, 24, 0)
        winwidth  = gdk_window_get_width(window);
        winheight = gdk_window_get_height(window);
#   else
        gdk_drawable_get_size(GDK_DRAWABLE(window), &winwidth, &winheight);
#   endif

    int iconwidth  = gdk_pixbuf_get_width(cone_icon),
289
        iconheight = gdk_pixbuf_get_height(cone_icon);
290 291 292 293 294 295 296 297 298 299
    double widthratio  = (double) winwidth / iconwidth,
           heightratio = (double) winheight / iconheight;
    double sizeratio = widthratio < heightratio ? widthratio : heightratio;
    if (sizeratio < 1.0) {
        cone_icon = gdk_pixbuf_scale_simple(cone_icon, iconwidth * sizeratio, iconheight * sizeratio, GDK_INTERP_BILINEAR);
        if (!cone_icon) return false;
        iconwidth  = gdk_pixbuf_get_width(cone_icon);
        iconheight = gdk_pixbuf_get_height(cone_icon);
    }

300 301 302 303 304
    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);
305
    cairo_destroy(cr);
306 307 308 309

    return true;
}

310
gboolean VlcPluginGtk::update_time_slider(gpointer user_data)
311
{
Cheng Sun's avatar
Cheng Sun committed
312
    VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
313

314 315 316 317 318
    std::lock_guard<std::mutex> lock( plugin->m_timer_lock );

    auto pos = plugin->player().get_mp().position() * 100.0f;
    gtk_range_set_value( GTK_RANGE( plugin->time_slider ), pos );
    plugin->m_timer_update_timeout = 0;
319 320 321
    return FALSE;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
322
static bool time_slider_handler(GtkRange*, GtkScrollType, gdouble value, gpointer user_data)
323 324
{
    VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
325
    plugin->getMD().setPosition( value / 100.0 );
326 327 328
    return false;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
329
static bool vol_slider_handler(GtkRange*, GtkScrollType, gdouble value, gpointer user_data)
330 331
{
    VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
332
    plugin->getMD().setVolume( value );
333
    return false;
334 335
}

336 337 338
static void fullscreen_win_visibility_handler(GtkWidget *widget, gpointer user_data)
{
    VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
339
    plugin->do_set_fullscreen(gtk_widget_get_visible(widget));
340 341
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
342
static gboolean fullscreen_win_keypress_handler(GtkWidget*, GdkEventKey *event, gpointer user_data)
343 344 345 346
{
    VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
    switch (event->keyval)
    {
347
    case GDK_KEY_space:
348
        plugin->player().mlp().pause();
349
        return True;
350 351 352 353 354 355 356 357
    case GDK_KEY_Escape:
        plugin->set_fullscreen(false);
        return True;
    default:
        return False;
    }
}

358 359
void VlcPluginGtk::update_controls()
{
360
    auto state = player().get_mp().state();
361 362 363 364
    if ( state == libvlc_Stopped || state == libvlc_Ended || state == libvlc_Error ) {
        XUnmapWindow(display, video_xwindow);
    } else {
        XMapWindow(display, video_xwindow);
365 366
    }

367 368 369 370
    if (get_toolbar_visible()) {
        GtkToolItem *toolbutton;

        /* play/pause button */
371
        const gchar *stock_id = player().mlp().isPlaying() ? GTK_STOCK_MEDIA_PAUSE : GTK_STOCK_MEDIA_PLAY;
372 373 374 375 376 377 378 379 380
        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);
        }
381

382
        /* toolbar sensitivity */
383
        gtk_widget_set_sensitive( toolbar, true );
384

385
        /* time slider */
386
        if (!player().get_mp().isSeekable()) {
387 388 389 390 391
            gtk_widget_set_sensitive(time_slider, false);
            gtk_range_set_value(GTK_RANGE(time_slider), 0);
        } else {
            gtk_widget_set_sensitive(time_slider, true);
        }
392

393 394
        gtk_widget_show_all(toolbar);
    }
395 396 397 398
}

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

401
    Window socket = (Window) npwindow.window;
402
    GdkColor color_bg;
403
    gdk_color_parse(get_options().get_bg_color().c_str(), &color_bg);
404 405

    parent = gtk_plug_new(socket);
406
    gtk_widget_modify_bg(parent, GTK_STATE_NORMAL, &color_bg);
407 408 409 410
    gtk_widget_add_events(parent,
            GDK_BUTTON_PRESS_MASK
          | GDK_BUTTON_RELEASE_MASK);
    g_signal_connect(G_OBJECT(parent), "button-press-event", G_CALLBACK(video_button_handler), this);
411 412 413 414

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

415
    video_container = gtk_drawing_area_new();
416
    gtk_widget_modify_bg(video_container, GTK_STATE_NORMAL, &color_bg);
417
    gtk_widget_add_events(video_container,
418 419
            GDK_BUTTON_PRESS_MASK
          | GDK_BUTTON_RELEASE_MASK);
420
    g_signal_connect(G_OBJECT(video_container), "expose-event", G_CALLBACK(video_expose_handler), this);
421 422 423
    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);
424 425 426

    gtk_widget_show_all(parent);

427 428
    /* fullscreen top-level */
    fullscreen_win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
429
    gtk_widget_modify_bg(fullscreen_win, GTK_STATE_NORMAL, &color_bg);
430 431 432 433
    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);
434
    g_signal_connect(G_OBJECT(fullscreen_win), "key_press_event", G_CALLBACK(fullscreen_win_keypress_handler), this);
435 436 437 438 439 440

    /* 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();
441 442 443 444 445
    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);
446 447
    video_xwindow = XCreateSimpleWindow(display, get_xid(video_container), 0, 0,
                   1, 1,
448
                   0, bg_color.pixel, bg_color.pixel);
449 450 451 452 453 454

    /* 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
455 456 457

    /*** TOOLBAR ***/

458
    toolbar = gtk_toolbar_new();
459
    g_object_ref(G_OBJECT(toolbar));
460
    gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
Cheng Sun's avatar
Cheng Sun committed
461
    gtk_toolbar_set_icon_size(GTK_TOOLBAR(toolbar), GTK_ICON_SIZE_SMALL_TOOLBAR);
462 463 464 465 466
    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);
467 468 469 470 471 472 473 474 475
    m_player.get_mp().eventManager().onPaused([this, toolitem]() {
        gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(toolitem), GTK_STOCK_MEDIA_PLAY);
    });
    m_player.get_mp().eventManager().onPlaying([this, toolitem]() {
        gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(toolitem), GTK_STOCK_MEDIA_PAUSE);
    });
    m_player.get_mp().eventManager().onStopped([this, toolitem]() {
        gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(toolitem), GTK_STOCK_MEDIA_PLAY);
    });
476 477 478 479 480 481 482 483 484
    /* 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);
485
    gtk_range_set_increments(GTK_RANGE(time_slider), 2, 10);
486 487 488 489
    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);
490 491 492 493 494
    m_player.get_mp().eventManager().onPositionChanged([this](float) {
        std::lock_guard<std::mutex> lock( m_timer_lock );
        if ( m_timer_update_timeout == 0 )
            m_timer_update_timeout = g_timeout_add( 100, update_time_slider, (gpointer)this);
    });
495

496 497
    /* volume slider */
    toolitem = gtk_tool_item_new();
498
    vol_slider = gtk_hscale_new_with_range(0, 200, 10);
499
    gtk_range_set_increments(GTK_RANGE(vol_slider), 5, 20);
500 501 502 503 504 505 506 507
    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);

508 509 510 511 512 513 514 515 516
    return true;
}

bool VlcPluginGtk::resize_windows()
{
    GtkRequisition req;
    req.width = npwindow.width;
    req.height = npwindow.height;
    gtk_widget_size_request(parent, &req);
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
517
    return true;
518 519 520 521
}

bool VlcPluginGtk::destroy_windows()
{
522
    Display *display = get_display();
523 524 525 526

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

527 528 529 530
    /* destroy GTK top-levels */
    gtk_widget_destroy(parent);
    gtk_widget_destroy(fullscreen_win);

531
    /* free colors */
532 533
    Colormap colormap = DefaultColormap(display, DefaultScreen(display));
    XFreeColors(display, colormap, &bg_color.pixel, 1, 0);
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
534
    return true;
535
}