- "open file" and "open url" button
- play/pause toggle, backward, forward, stop buttons work with and control mplayer through slave mode
- to exit fullscreen, press Esc (double click will not work)
save this code to file test.vala
Code: Select all
/* valac --pkg gtk+-3.0 --pkg gdk-x11-3.0 test.vala */
/* Copyright (c) alphaOS
* Written by simargl <archpup-at-gmail-dot-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 3 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, see <http://www.gnu.org/licenses/>.
*/
using Gtk;
class program : Gtk.Window
{
Gtk.DrawingArea drawing_area;
X.Window xid;
Gtk.Window window;
string file;
int default_drawing_area_width = 790;
int default_drawing_area_height = 460;
public program()
{
drawing_area = new Gtk.DrawingArea();
drawing_area.set_size_request(default_drawing_area_width, default_drawing_area_height);
var button_open_file = new Gtk.Button.with_label("Open File");
var button_play_url = new Gtk.Button.with_label("Open URL");
var button_pause = new Gtk.Button.from_icon_name("media-playback-pause-symbolic", Gtk.IconSize.LARGE_TOOLBAR);
var button_rewind = new Gtk.Button.from_icon_name("media-skip-backward-symbolic", Gtk.IconSize.LARGE_TOOLBAR);
var button_forward = new Gtk.Button.from_icon_name("media-skip-forward-symbolic", Gtk.IconSize.LARGE_TOOLBAR);
var button_stop = new Gtk.Button.from_icon_name("media-playback-stop-symbolic", Gtk.IconSize.LARGE_TOOLBAR);
var button_full_screen = new Gtk.Button.from_icon_name("view-fullscreen-symbolic", Gtk.IconSize.LARGE_TOOLBAR);
button_open_file.clicked.connect(open_file);
button_play_url.clicked.connect(play_url);
button_pause.clicked.connect(music_pause);
button_rewind.clicked.connect(play_rewind);
button_forward.clicked.connect(play_forward);
button_stop.clicked.connect(stop_mplayer);
button_full_screen.clicked.connect(full_screen_switch);
var grid = new Gtk.Grid();
grid.attach(drawing_area, 0, 0, 16, 1);
grid.attach(button_open_file, 0, 1, 1, 1);
grid.attach(button_play_url, 1, 1, 1, 1);
grid.attach(button_pause, 6, 1, 1, 1);
grid.attach(button_rewind, 7, 1, 1, 1);
grid.attach(button_forward, 8, 1, 1, 1);
grid.attach(button_stop, 9, 1, 1, 1);
grid.attach(button_full_screen, 15, 1, 1, 1);
window = new Gtk.Window();
window.window_position = WindowPosition.CENTER;
window.add(grid);
window.show_all();
window.set_resizable(false);
window.set_icon_name("video-x-generic");
window.destroy.connect(exit_program);
xid = (ulong)Gdk.X11Window.get_xid(drawing_area.get_window());
}
private void open_file()
{
var dialog = new FileChooserDialog("Open File...", window, FileChooserAction.OPEN,
"gtk-cancel", Gtk.ResponseType.CANCEL,
"gtk-open", Gtk.ResponseType.ACCEPT);
var filter = new FileFilter();
filter.set_filter_name("All Media Files");
filter.add_mime_type("audio/*");
filter.add_mime_type("video/*");
dialog.add_filter(filter);
dialog.set_select_multiple(false);
if (file != null)
{
dialog.set_current_folder(Path.get_dirname(file));
}
if (dialog.run() == Gtk.ResponseType.ACCEPT)
{
file = dialog.get_filename();
play_file(file);
print (file);
}
dialog.destroy();
}
void play_file(string file_or_address)
{
stop_mplayer();
try
{
Process.spawn_command_line_sync("mkfifo /tmp/mplayer_fifo");
Process.spawn_command_line_async("mplayer -slave -quiet -input file=/tmp/mplayer_fifo -wid %u '%s' ".printf((uint)xid, file_or_address));
}
catch (GLib.Error e)
{
stderr.printf ("%s\n", e.message);
}
}
private void play_forward()
{
try
{
Process.spawn_command_line_async("sh -c \"echo seek +15 > /tmp/mplayer_fifo\"");
}
catch (GLib.Error e)
{
stderr.printf ("%s\n", e.message);
}
}
private void play_rewind()
{
try
{
Process.spawn_command_line_async("sh -c \"echo seek -15 > /tmp/mplayer_fifo\"");
}
catch (GLib.Error e)
{
stderr.printf ("%s\n", e.message);
}
}
private void music_pause()
{
try
{
Process.spawn_command_line_async("sh -c \"echo pause > /tmp/mplayer_fifo\"");
}
catch (GLib.Error e)
{
stderr.printf ("%s\n", e.message);
}
}
private void stop_mplayer()
{
try
{
Process.spawn_command_line_async("sh -c \"echo stop > /tmp/mplayer_fifo\"");
Process.spawn_command_line_sync("rm -f /tmp/mplayer_fifo");
}
catch (GLib.Error e)
{
stderr.printf ("%s\n", e.message);
}
}
private void full_screen_switch()
{
int width = Gdk.Screen.width();
int height = Gdk.Screen.height();
drawing_area.set_size_request(width, height);
window.fullscreen();
window.key_press_event.connect(keyboard_events);
}
private bool keyboard_events(Gdk.EventKey event)
{
string key = Gdk.keyval_name(event.keyval);
if(key=="Escape")
{
window.unfullscreen();
drawing_area.set_size_request(default_drawing_area_width, default_drawing_area_height);
}
return false;
}
private void play_url()
{
var play_url_dialog = new Gtk.Dialog();
play_url_dialog.title = "Open URL";
play_url_dialog.set_border_width(5);
play_url_dialog.set_property("skip-taskbar-hint", true);
play_url_dialog.set_resizable(false);
var play_url_label = new Gtk.Label("Open URL");
var play_url_entry = new Gtk.Entry();
play_url_entry.set_size_request(410, 0);
var grid = new Gtk.Grid();
grid.attach(play_url_label, 0, 0, 1, 1);
grid.attach(play_url_entry, 1, 0, 5, 1);
grid.set_column_spacing(25);
grid.set_column_homogeneous(true);
var content = play_url_dialog.get_content_area() as Gtk.Box;
content.pack_start(grid, true, true, 10);
play_url_dialog.add_button("Play", Gtk.ResponseType.OK);
play_url_dialog.add_button("Close", Gtk.ResponseType.CLOSE);
play_url_dialog.show_all();
if (play_url_dialog.run() == Gtk.ResponseType.OK)
{
play_file(play_url_entry.get_text());
}
play_url_dialog.destroy();
}
private void exit_program()
{
stop_mplayer();
Gtk.main_quit();
}
public static int main (string[] args)
{
Gtk.init(ref args);
new program();
Gtk.main();
return 0;
}
}
then, compile with
Code: Select all
valac --pkg gtk+-3.0 --pkg gdk-x11-3.0 test.vala
This program should work with any GTK3 version, since it doesn't use headerbar (mplayer playback is broken with added headerbar). I don't plan to use this instead of gnome mplayer, if that's not obvious. Just an experimental program for you to try out, add more options / improve.
---------
Edit: This program is now called gmp-video, source code is available through alphaos bitbucket repository.