Class: Gtk2Mp3

Inherits:
Object
  • Object
show all
Extended by:
Rafini::Empty
Defined in:
lib/gtk2mp3.rb,
lib/gtk2mp3/gui.rb,
lib/gtk2mp3/mpd.rb,
lib/gtk2mp3/config.rb

Constant Summary collapse

VERSION =
'3.0.210919'
HELP =
<<~HELP
  Usage:
    gtk2mp3 [:options+]
  Options:
    -h --help
    -v --version
    --minime       \t Real minime
    --notoggle     \t Minime wont toggle decorated and keep above
    --notdecorated \t Dont decorate window
    --update       \t Updates and sets playlist to the entire collection
  # Note:
  # Requires MPD/MPC.
  # See https://www.musicpd.org/clients/mpc/.
HELP
CONFIG =
{
  TOOLBOX: [:horizontal],
  toolbox: h0,
  toolbox!: [:TOOLBOX,:toolbox],

  NEXT_BUTTON: [label: 'Next!'],
  next_button: h0,
  next_button!: [:NEXT_BUTTON, :next_button, 'clicked'],

  STOP_BUTTON: [label: 'Stop'],
  stop_button: h0,
  stop_button!: [:STOP_BUTTON, :stop_button, 'clicked'],

  ID_LABEL: a0,
  id_label: {set_selectable: true},
  id_label!: [:ID_LABEL, :id_label],

  HelpFile: 'https://github.com/carlosjhr64/gtk2mp3',
  Logo: "#{File.dirname File.dirname __dir__}/data/logo.png",
  about_dialog: {
    set_program_name: 'Gtk2Mp3',
    set_version: VERSION.semantic(0..1),
    set_copyright: '(c) 2021 CarlosJHR64',
    set_comments: 'A MPD/MPC "Next!" Button',
    set_website: 'https://github.com/carlosjhr64/gtk2mp3',
    set_website_label: 'See it at GitHub!',
  },

  app_menu: {
    add_menu_item: [:minime!, :about!, :quit!]
  },
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stage, toolbar, options, &block) ⇒ Gtk2Mp3

Returns a new instance of Gtk2Mp3.



6
7
8
9
10
11
12
# File 'lib/gtk2mp3/gui.rb', line 6

def initialize(stage, toolbar, options, &block)
  @toolbox   = Such::Box.new toolbar, :toolbox!
  Such::Button.new(@toolbox, :next_button!){block.call :next}
  Such::Button.new(@toolbox, :stop_button!){block.call :stop}
  @label = Such::Label.new(stage, :id_label!)
  build_logo_menu(block)
end

Class Method Details

.initObject



18
19
20
21
22
# File 'lib/gtk2mp3.rb', line 18

def Gtk2Mp3.init
  require_relative 'gtk2mp3/mpd'
  Gtk2Mp3.system_mpd
  Gtk2Mp3.system_mpc if ARGV.include? '--update'
end

.mpc_command(command) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gtk2mp3/mpd.rb', line 26

def Gtk2Mp3.mpc_command(command)
  case command
  when :next
    current = `mpc current`.strip
    if current.empty?
      system 'mpc play'
    else
      # On my system, if I don't pause first, the sound fails.
      system 'mpc pause'
      system 'mpc next'
    end
  when :stop
    system 'mpc stop'
  end
end

.mpc_idleloop(gui) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/gtk2mp3/mpd.rb', line 42

def Gtk2Mp3.mpc_idleloop(gui)
  Thread.new do
    IO.popen('mpc idleloop', 'r') do |pipe|
      while line = pipe.gets
        gui.set_label File.basename(`mpc current`.strip, '.*')
      end
    end
  end
end

.runObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gtk2mp3.rb', line 24

def Gtk2Mp3.run
  # This is a Gtk3App.
  require 'gtk3app'
  # This Gem.
  require_relative 'gtk2mp3/config'
  require_relative 'gtk2mp3/gui'
  Gtk3App.run(klass:Gtk2Mp3) do  |stage, toolbar, options|
    gui = Gtk2Mp3.new(stage, toolbar, options){Gtk2Mp3.mpc_command _1}
    Gtk2Mp3.mpc_idleloop(gui)
  end
end

.system_mpcObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gtk2mp3/mpd.rb', line 12

def Gtk2Mp3.system_mpc
  # initialize playlist
  unless system 'mpc --wait update' and
      system 'mpc clear'            and
      system 'mpc ls | mpc add'     and
      system 'mpc consume off'      and
      system 'mpc repeat off'       and
      system 'mpc random on'        and
      system 'mpc single off'
    $stderr.puts %q(Could not initialize mpd's playlist)
    exit 76 # EX_PROTOCOL
  end
end

.system_mpdObject



2
3
4
5
6
7
8
9
10
# File 'lib/gtk2mp3/mpd.rb', line 2

def Gtk2Mp3.system_mpd
  # check if mpd is already running
  unless system 'ps -C mpd'
    unless system 'mpd'
      $stderr.puts 'Could not start the mpd daemon.'
      exit 69 # EX_UNAVAILABLE
    end
  end
end

Instance Method Details

#build_logo_menu(block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gtk2mp3/gui.rb', line 14

def build_logo_menu(block)
  Gtk3App.logo_press_event do |button|
    case button
    when 1
      block.call :next
    when 2
      block.call :stop
    when 3
      # Gtk3App's main menu
    end
  end
end

#set_label(text) ⇒ Object



2
3
4
# File 'lib/gtk2mp3/gui.rb', line 2

def set_label(text)
  @label.text = text
end