Netsync: fix return type
[vlc.git] / doc / libvlc / gtk_player.c
blob8196476249248f5ef845a402f4bd1ae0e11cbc9d
1 // gcc -o gtk_player gtk_player.c `pkg-config --libs gtk+-2.0 libvlc` `pkg-config --cflags gtk+-2.0 libvlc`
3 /* License WTFPL http://sam.zoy.org/wtfpl/ */
4 /* Written by Vincent Schüßler */
6 #include <stdlib.h>
7 #include <gtk/gtk.h>
8 #include <gdk/gdkx.h>
9 #include <vlc/vlc.h>
11 #define BORDER_WIDTH 6
13 void destroy(GtkWidget *widget, gpointer data);
14 void player_widget_on_realize(GtkWidget *widget, gpointer data);
15 void on_open(GtkWidget *widget, gpointer data);
16 void open_media(const char* uri);
17 void play(void);
18 void pause_player(void);
19 void on_playpause(GtkWidget *widget, gpointer data);
20 void on_stop(GtkWidget *widget, gpointer data);
22 libvlc_media_player_t *media_player;
23 libvlc_instance_t *vlc_inst;
24 GtkWidget *playpause_button;
26 void destroy(GtkWidget *widget, gpointer data) {
27 gtk_main_quit();
30 void player_widget_on_realize(GtkWidget *widget, gpointer data) {
31 libvlc_media_player_set_xwindow(media_player, GDK_WINDOW_XID(gtk_widget_get_window(widget)));
34 void on_open(GtkWidget *widget, gpointer data) {
35 GtkWidget *dialog;
36 dialog = gtk_file_chooser_dialog_new("Choose Media", data, GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL,GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
37 if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
38 char *uri;
39 uri = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog));
40 open_media(uri);
41 g_free(uri);
43 gtk_widget_destroy(dialog);
46 void open_media(const char* uri) {
47 libvlc_media_t *media;
48 media = libvlc_media_new_location(vlc_inst, uri);
49 libvlc_media_player_set_media(media_player, media);
50 play();
51 libvlc_media_release(media);
54 void on_playpause(GtkWidget *widget, gpointer data) {
55 if(libvlc_media_player_is_playing(media_player) == 1) {
56 pause_player();
58 else {
59 play();
63 void on_stop(GtkWidget *widget, gpointer data) {
64 pause_player();
65 libvlc_media_player_stop(media_player);
68 void play(void) {
69 libvlc_media_player_play(media_player);
70 gtk_button_set_label(GTK_BUTTON(playpause_button), "gtk-media-pause");
73 void pause_player(void) {
74 libvlc_media_player_pause(media_player);
75 gtk_button_set_label(GTK_BUTTON(playpause_button), "gtk-media-play");
78 int main( int argc, char *argv[] ) {
79 GtkWidget *window,
80 *vbox,
81 *menubar,
82 *filemenu,
83 *fileitem,
84 *filemenu_openitem,
85 *player_widget,
86 *hbuttonbox,
87 *stop_button;
89 gtk_init (&argc, &argv);
90 // setup window
91 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
92 gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
93 g_signal_connect(window, "destroy", G_CALLBACK(destroy), NULL);
94 gtk_container_set_border_width (GTK_CONTAINER (window), 0);
95 gtk_window_set_title(GTK_WINDOW(window), "GTK+ libVLC Demo");
97 //setup vbox
98 vbox = gtk_vbox_new(FALSE, 0);
99 gtk_container_add(GTK_CONTAINER(window), vbox);
101 //setup menu
102 menubar = gtk_menu_bar_new();
103 filemenu = gtk_menu_new();
104 fileitem = gtk_menu_item_new_with_label ("File");
105 filemenu_openitem = gtk_menu_item_new_with_label("Open");
106 gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), filemenu_openitem);
107 gtk_menu_item_set_submenu(GTK_MENU_ITEM(fileitem), filemenu);
108 gtk_menu_bar_append(GTK_MENU_BAR(menubar), fileitem);
109 gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
110 g_signal_connect(filemenu_openitem, "activate", G_CALLBACK(on_open), window);
112 //setup player widget
113 player_widget = gtk_drawing_area_new();
114 gtk_box_pack_start(GTK_BOX(vbox), player_widget, TRUE, TRUE, 0);
116 //setup controls
117 //playpause_button = gtk_button_new_from_stock(GTK_STOCK_MEDIA_PLAY);
118 playpause_button = gtk_button_new_with_label("gtk-media-play");
119 gtk_button_set_use_stock(GTK_BUTTON(playpause_button), TRUE);
120 stop_button = gtk_button_new_from_stock(GTK_STOCK_MEDIA_STOP);
121 g_signal_connect(playpause_button, "clicked", G_CALLBACK(on_playpause), NULL);
122 g_signal_connect(stop_button, "clicked", G_CALLBACK(on_stop), NULL);
123 hbuttonbox = gtk_hbutton_box_new();
124 gtk_container_set_border_width(GTK_CONTAINER(hbuttonbox), BORDER_WIDTH);
125 gtk_button_box_set_layout(GTK_BUTTON_BOX(hbuttonbox), GTK_BUTTONBOX_START);
126 gtk_box_pack_start(GTK_BOX(hbuttonbox), playpause_button, FALSE, FALSE, 0);
127 gtk_box_pack_start(GTK_BOX(hbuttonbox), stop_button, FALSE, FALSE, 0);
128 gtk_box_pack_start(GTK_BOX(vbox), hbuttonbox, FALSE, FALSE, 0);
130 //setup vlc
131 vlc_inst = libvlc_new(0, NULL);
132 media_player = libvlc_media_player_new(vlc_inst);
133 g_signal_connect(G_OBJECT(player_widget), "realize", G_CALLBACK(player_widget_on_realize), NULL);
135 gtk_widget_show_all(window);
136 gtk_main ();
138 libvlc_media_player_release(media_player);
139 libvlc_release(vlc_inst);
140 return 0;