MPlayer frontend in Vala and GTK3

Programming issues and discussion
simargl
Site Admin
Posts: 466
Joined: 16 May 2013, 10:54
Contact:

MPlayer frontend in Vala and GTK3

Postby simargl » 30 Dec 2013, 17:43

This is very simple GUI fronted for mplayer. Elements you see on screenshots are fully functional:
- "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


Image

Image

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.

Scooby
Site Admin
Posts: 826
Joined: 09 Sep 2013, 16:52

Re: MPlayer frontend in Vala and GTK3

Postby Scooby » 30 Dec 2013, 20:11

Do you contruct GUI by hand or do you
use some editor like glade?

efgee
Expert
Posts: 115
Joined: 29 Dec 2013, 20:58

Re: MPlayer frontend in Vala and GTK3

Postby efgee » 30 Dec 2013, 20:48

Thank you for the code, it shows me your coding style... and get's me started to code in vala on alphaos.

BTW: is the code of the control-center and other apps you created available or closed source?
Asking because I would like to code a similar app for other things (of course for alphaos...)
No need to reinvent the wheel.

Thanks

simargl
Site Admin
Posts: 466
Joined: 16 May 2013, 10:54
Contact:

Re: MPlayer frontend in Vala and GTK3

Postby simargl » 30 Dec 2013, 22:59

efgee wrote:BTW: is the code of the control-center and other apps you created available or closed source?
Asking because I would like to code a similar app for other things (of course for alphaos...)
No need to reinvent the wheel.

Thanks


That would be awesome! :)

All apps are open source with GPL3 license, sources are in single mercurial repository https://bitbucket.org/simargl/alphaos.

When you clone that repo go to the directory def-scripts/03_extra, all current Vala applications are in there: control-center, emendo, simple-radio, taeni and wpset.

Scooby wrote:Do you contruct GUI by hand or do you
use some editor like glade?

It is all written just in Vala, making buttons, and adding to grid is easy anyway. Vala is generally great.

efgee
Expert
Posts: 115
Joined: 29 Dec 2013, 20:58

Re: MPlayer frontend in Vala and GTK3

Postby efgee » 30 Dec 2013, 23:38

Thank you for the info.
Never handled bitbucket before but I will try to get my hands on it.
Hope to have some free time the next few days...

What attracts me to alphaos is also the fact that you don't use phyton to build your tools but vala.
Thank you again.

simargl
Site Admin
Posts: 466
Joined: 16 May 2013, 10:54
Contact:

Re: MPlayer frontend in Vala and GTK3

Postby simargl » 09 Jan 2014, 12:51

This program is now called gmp-video, source code is available through alphaos bitbucket repository.

Scooby
Site Admin
Posts: 826
Joined: 09 Sep 2013, 16:52

Re: MPlayer frontend in Vala and GTK3

Postby Scooby » 10 Dec 2014, 18:34

is there a plan to add playlist support.

Missed it today :?


Return to “Scripting and Programming”

Who is online

Users browsing this forum: No registered users and 0 guests

cron