2018-03-05 01:15:29 +00:00
|
|
|
/* Video Player
|
2020-09-13 18:59:33 +00:00
|
|
|
* #Keywords: GtkVideo, GtkMediaStream, GtkMediaFile, GdkPaintable
|
|
|
|
* #Keywords: GtkMediaControls
|
2018-03-05 01:15:29 +00:00
|
|
|
*
|
|
|
|
* This is a simple video player using just GTK widgets.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
static GtkWidget *window = NULL;
|
|
|
|
|
|
|
|
static void
|
2022-10-28 15:38:57 +00:00
|
|
|
open_dialog_response_cb (GObject *source,
|
|
|
|
GAsyncResult *result,
|
|
|
|
void *user_data)
|
2018-03-05 01:15:29 +00:00
|
|
|
{
|
2022-10-28 15:38:57 +00:00
|
|
|
GtkFileDialog *dialog = GTK_FILE_DIALOG (source);
|
|
|
|
GtkWidget *video = user_data;
|
|
|
|
GFile *file;
|
2018-03-05 01:15:29 +00:00
|
|
|
|
2022-10-28 15:38:57 +00:00
|
|
|
file = gtk_file_dialog_open_finish (dialog, result, NULL);
|
|
|
|
if (file)
|
2018-03-05 01:15:29 +00:00
|
|
|
{
|
|
|
|
gtk_video_set_file (GTK_VIDEO (video), file);
|
|
|
|
g_object_unref (file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
open_clicked_cb (GtkWidget *button,
|
|
|
|
GtkWidget *video)
|
|
|
|
{
|
2022-10-28 15:38:57 +00:00
|
|
|
GtkFileDialog *dialog;
|
2020-08-10 02:27:32 +00:00
|
|
|
GtkFileFilter *filter;
|
2022-10-28 15:38:57 +00:00
|
|
|
GListStore *filters;
|
2018-03-05 01:15:29 +00:00
|
|
|
|
2022-10-28 15:38:57 +00:00
|
|
|
dialog = gtk_file_dialog_new ();
|
|
|
|
gtk_file_dialog_set_title (dialog, "Select a video");
|
|
|
|
|
|
|
|
filters = g_list_store_new (GTK_TYPE_FILE_FILTER);
|
2020-09-13 05:50:31 +00:00
|
|
|
|
2021-03-30 03:39:25 +00:00
|
|
|
filter = gtk_file_filter_new ();
|
|
|
|
gtk_file_filter_add_pattern (filter, "*");
|
|
|
|
gtk_file_filter_set_name (filter, "All Files");
|
2022-10-28 15:38:57 +00:00
|
|
|
g_list_store_append (filters, filter);
|
2021-03-30 03:39:25 +00:00
|
|
|
g_object_unref (filter);
|
|
|
|
|
|
|
|
filter = gtk_file_filter_new ();
|
|
|
|
gtk_file_filter_add_mime_type (filter, "image/*");
|
|
|
|
gtk_file_filter_set_name (filter, "Images");
|
2022-10-28 15:38:57 +00:00
|
|
|
g_list_store_append (filters, filter);
|
2021-03-30 03:39:25 +00:00
|
|
|
g_object_unref (filter);
|
|
|
|
|
2020-08-10 02:27:32 +00:00
|
|
|
filter = gtk_file_filter_new ();
|
|
|
|
gtk_file_filter_add_mime_type (filter, "video/*");
|
|
|
|
gtk_file_filter_set_name (filter, "Video");
|
2022-10-28 15:38:57 +00:00
|
|
|
g_list_store_append (filters, filter);
|
|
|
|
|
|
|
|
gtk_file_dialog_set_current_filter (dialog, filter);
|
2020-08-10 02:27:32 +00:00
|
|
|
g_object_unref (filter);
|
2021-03-30 03:39:25 +00:00
|
|
|
|
2022-10-28 15:38:57 +00:00
|
|
|
gtk_file_dialog_set_filters (dialog, G_LIST_MODEL (filters));
|
|
|
|
g_object_unref (filters);
|
|
|
|
|
|
|
|
gtk_file_dialog_open (dialog,
|
|
|
|
GTK_WINDOW (gtk_widget_get_root (button)),
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
open_dialog_response_cb, video);
|
2018-03-05 01:15:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-13 05:50:31 +00:00
|
|
|
static void
|
|
|
|
logo_clicked_cb (GtkWidget *button,
|
|
|
|
gpointer video)
|
|
|
|
{
|
|
|
|
GFile *file;
|
|
|
|
|
|
|
|
file = g_file_new_for_uri ("resource:///images/gtk-logo.webm");
|
|
|
|
gtk_video_set_file (GTK_VIDEO (video), file);
|
|
|
|
g_object_unref (file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bbb_clicked_cb (GtkWidget *button,
|
|
|
|
gpointer video)
|
|
|
|
{
|
|
|
|
GFile *file;
|
|
|
|
|
|
|
|
file = g_file_new_for_uri ("https://download.blender.org/peach/trailer/trailer_400p.ogg");
|
|
|
|
gtk_video_set_file (GTK_VIDEO (video), file);
|
|
|
|
g_object_unref (file);
|
|
|
|
}
|
|
|
|
|
2018-03-05 01:15:29 +00:00
|
|
|
static void
|
|
|
|
fullscreen_clicked_cb (GtkWidget *button,
|
|
|
|
gpointer unused)
|
|
|
|
{
|
2020-03-07 14:01:21 +00:00
|
|
|
GtkWidget *widget_window = GTK_WIDGET (gtk_widget_get_root (button));
|
2018-03-05 01:15:29 +00:00
|
|
|
|
2020-03-07 14:01:21 +00:00
|
|
|
gtk_window_fullscreen (GTK_WINDOW (widget_window));
|
2018-03-05 01:15:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-13 06:31:41 +00:00
|
|
|
static gboolean
|
|
|
|
toggle_fullscreen (GtkWidget *widget,
|
|
|
|
GVariant *args,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GdkSurface *surface;
|
|
|
|
GdkToplevelState state;
|
|
|
|
|
|
|
|
surface = gtk_native_get_surface (GTK_NATIVE (widget));
|
|
|
|
state = gdk_toplevel_get_state (GDK_TOPLEVEL (surface));
|
|
|
|
|
|
|
|
if (state & GDK_TOPLEVEL_STATE_FULLSCREEN)
|
|
|
|
gtk_window_unfullscreen (GTK_WINDOW (widget));
|
|
|
|
else
|
|
|
|
gtk_window_fullscreen (GTK_WINDOW (widget));
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-03-05 01:15:29 +00:00
|
|
|
GtkWidget *
|
|
|
|
do_video_player (GtkWidget *do_widget)
|
|
|
|
{
|
|
|
|
GtkWidget *title;
|
|
|
|
GtkWidget *video;
|
2020-09-13 05:50:31 +00:00
|
|
|
GtkWidget *button;
|
|
|
|
GtkWidget *image;
|
2018-03-05 01:15:29 +00:00
|
|
|
GtkWidget *fullscreen_button;
|
2020-09-13 06:31:41 +00:00
|
|
|
GtkEventController *controller;
|
2018-03-05 01:15:29 +00:00
|
|
|
|
|
|
|
if (!window)
|
|
|
|
{
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2018-03-05 01:15:29 +00:00
|
|
|
gtk_window_set_display (GTK_WINDOW (window),
|
|
|
|
gtk_widget_get_display (do_widget));
|
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Video Player");
|
2020-08-10 02:34:17 +00:00
|
|
|
gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);
|
2020-05-09 16:03:11 +00:00
|
|
|
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
|
2018-03-05 01:15:29 +00:00
|
|
|
|
|
|
|
video = gtk_video_new ();
|
2020-09-13 05:50:31 +00:00
|
|
|
gtk_video_set_autoplay (GTK_VIDEO (video), TRUE);
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), video);
|
2018-03-05 01:15:29 +00:00
|
|
|
|
|
|
|
title = gtk_header_bar_new ();
|
|
|
|
gtk_window_set_titlebar (GTK_WINDOW (window), title);
|
|
|
|
|
2020-09-13 05:50:31 +00:00
|
|
|
button = gtk_button_new_with_mnemonic ("_Open");
|
|
|
|
g_signal_connect (button, "clicked", G_CALLBACK (open_clicked_cb), video);
|
|
|
|
gtk_header_bar_pack_start (GTK_HEADER_BAR (title), button);
|
|
|
|
|
|
|
|
button = gtk_button_new ();
|
|
|
|
image = gtk_image_new_from_resource ("/cursors/images/gtk_logo_cursor.png");
|
|
|
|
gtk_image_set_pixel_size (GTK_IMAGE (image), 24);
|
|
|
|
gtk_button_set_child (GTK_BUTTON (button), image);
|
|
|
|
g_signal_connect (button, "clicked", G_CALLBACK (logo_clicked_cb), video);
|
|
|
|
gtk_header_bar_pack_start (GTK_HEADER_BAR (title), button);
|
|
|
|
|
|
|
|
button = gtk_button_new ();
|
|
|
|
image = gtk_image_new_from_resource ("/video-player/bbb.png");
|
|
|
|
gtk_image_set_pixel_size (GTK_IMAGE (image), 24);
|
|
|
|
gtk_button_set_child (GTK_BUTTON (button), image);
|
|
|
|
g_signal_connect (button, "clicked", G_CALLBACK (bbb_clicked_cb), video);
|
|
|
|
gtk_header_bar_pack_start (GTK_HEADER_BAR (title), button);
|
2018-03-05 01:15:29 +00:00
|
|
|
|
|
|
|
fullscreen_button = gtk_button_new_from_icon_name ("view-fullscreen-symbolic");
|
|
|
|
g_signal_connect (fullscreen_button, "clicked", G_CALLBACK (fullscreen_clicked_cb), NULL);
|
|
|
|
gtk_header_bar_pack_end (GTK_HEADER_BAR (title), fullscreen_button);
|
2020-09-13 06:31:41 +00:00
|
|
|
|
|
|
|
controller = gtk_shortcut_controller_new ();
|
|
|
|
gtk_shortcut_controller_set_scope (GTK_SHORTCUT_CONTROLLER (controller),
|
|
|
|
GTK_SHORTCUT_SCOPE_GLOBAL);
|
|
|
|
gtk_widget_add_controller (window, controller);
|
|
|
|
gtk_shortcut_controller_add_shortcut (GTK_SHORTCUT_CONTROLLER (controller),
|
|
|
|
gtk_shortcut_new (gtk_keyval_trigger_new (GDK_KEY_F11, 0),
|
|
|
|
gtk_callback_action_new (toggle_fullscreen, NULL, NULL)));
|
2018-03-05 01:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!gtk_widget_get_visible (window))
|
2022-11-28 04:03:14 +00:00
|
|
|
gtk_widget_set_visible (window, TRUE);
|
2018-03-05 01:15:29 +00:00
|
|
|
else
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (window));
|
2018-03-05 01:15:29 +00:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|