class Gtk2Mp3::GUI

Constants

ID
MUTEX

Public Class Methods

new(program) click to toggle source
# File lib/gtk2mp3/gui.rb, line 78
def initialize(program)
  # Build
  window,minime,menu = program.window,program.mini_menu,program.app_menu
  vbox = Such::Box.new(window, :VBOX)
  hbox = Such::Box.new(vbox, :HBOX)
  Such::Button.new(hbox, :next_button!){next_song!} if CONFIG[:BUTTONS].include?(:next_button!)
  Such::Button.new(hbox, :stop_button!){stop_song!} if CONFIG[:BUTTONS].include?(:stop_button!)
  @label = Such::Label.new(vbox, :id_label!)
  menu.each{|_|_.destroy if _.key==:fs! or _.key==:help!}
  minime.each{|_|_.destroy}
  minime.add_menu_item(:stop_item!){stop_song!} if CONFIG[:ITEMS].include?(:stop_item!)
  minime.add_menu_item(:next_item!){next_song!} if CONFIG[:ITEMS].include?(:next_item!)

  # Inits
  @db = File.exist?(_=CONFIG[:DBM]) ?  JSON.parse(File.read(_)) :  {}
  @list = `mpc listall`.lines.map{|_|ID[_]}.uniq
  # A fuzzy delete of possibly gone keys...
  @db.keys.each{|id| down(id) unless @list.include?(id)}

  # Run
  @skipped=@playing=nil
  next_song!
  @time,@played = Time.now,@playing
  Thread.new do
    sleep(1) # mpd needs a little time to settle
    mpc_idle_player
  end
  window.show_all

  # Handle Signal.trap
  GLib::Timeout.add(500) do
    case Gtk2Mp3.signal!
    when :USR1
      next_song! 
    when :USR2
      stop_song!
    when :TERM
      Gtk3App.quit!
    end
    true # repeat
  end
end

Public Instance Methods

db_update() click to toggle source
# File lib/gtk2mp3/gui.rb, line 46
def db_update
  if @skipped
    up(@skipped) if Time.now - @time < CONFIG[:PLAYED]/2
    down(@skipped) if Time.now - @time > CONFIG[:PLAYED]
  elsif @played
    down(@played)
    next_song
  end
  @time,@played,@skipped = Time.now,@playing,nil
end
down(id) click to toggle source

decrementing count makes the id more likely to be played

# File lib/gtk2mp3/gui.rb, line 29
def down(id)
  if count=@db[id]
    count -= 1
    if count>0
      @db[id] = count
    else
      @db.delete(id)
    end
  end
end
finalize() click to toggle source
# File lib/gtk2mp3/gui.rb, line 121
def finalize
  stop_song!
  File.write(CONFIG[:DBM], JSON.pretty_generate(@db))
end
mpc_idle_player() click to toggle source
# File lib/gtk2mp3/gui.rb, line 57
def mpc_idle_player
  loop do
    system "mpc idle player #{TO_DEV_NULL}"
    MUTEX.synchronize{db_update}
  end
end
next_song() click to toggle source
# File lib/gtk2mp3/gui.rb, line 22
def next_song
  id = random_song
  @label.text = @playing = ID[`mpc -f '%file%' searchplay filename #{id.shellescape}`]
end
next_song!() click to toggle source
# File lib/gtk2mp3/gui.rb, line 64
def next_song!
  MUTEX.synchronize do
    @skipped = @playing
    next_song
  end
end
play?(id) click to toggle source
# File lib/gtk2mp3/gui.rb, line 10
def play?(id)
  rand(@db[id].to_i+1)==0
end
random_song() click to toggle source
# File lib/gtk2mp3/gui.rb, line 14
def random_song
  n = @list.length
  loop do
    id = @list[rand(n)]
    return id if play?(id)
  end
end
stop_song!() click to toggle source
# File lib/gtk2mp3/gui.rb, line 71
def stop_song!
  MUTEX.synchronize do
    @skipped = @playing = @played = nil
    system "mpc stop #{TO_DEV_NULL}"
  end
end
up(id) click to toggle source

incrementing count makes the id less likely to be played

# File lib/gtk2mp3/gui.rb, line 42
def up(id)
  @db[id] = @db[id].to_i+1
end