module CMusBt::Player

Constants

EVENT_STRUCTURE

from struct input_event on <linux/event.h>

FUNC_TABLE

Public Class Methods

daemon(info) click to toggle source
# File lib/cmus-bt/player.rb, line 100
def daemon(info)
  io   = File.open(info[:dev_file], "rb")
  size = [0, 0, 0, 0, 0].pack(EVENT_STRUCTURE).bytesize

  until io.eof?
    tmp  = io.read(size).unpack(EVENT_STRUCTURE)
    type = tmp[2]
    code = tmp[3]
    val  = tmp[4]

    $logger.debug("input #{type}, #{code}, #{val}")

    next if type != 1 or val != 0

    FUNC_TABLE[code] && send(FUNC_TABLE[code])
  end

rescue RuntimeError
  $logger.debug("stop daemon")

ensure
  io&.close
end

Private Class Methods

on_next() click to toggle source
# File lib/cmus-bt/player.rb, line 88
def on_next
  system("cmus-remote --next")
  $logger.info("NEXT")
end
on_pause() click to toggle source
# File lib/cmus-bt/player.rb, line 82
def on_pause
  system("cmus-remote --pause")
  $logger.info("PAUSE")
end
on_play() click to toggle source
# File lib/cmus-bt/player.rb, line 71
def on_play
  if status == :playing
    system("cmus-remote --pause")
    $logger.info("PAUSE")
  else
    system("cmus-remote --play")
    $logger.info("PLAY")
  end
end
on_prev() click to toggle source
# File lib/cmus-bt/player.rb, line 94
def on_prev
  system("cmus-remote --prev")
  $logger.info("PREV")
end
query() click to toggle source
# File lib/cmus-bt/player.rb, line 25
def query
  begin
    status, ret = systemu("cmus-remote --query")
    raise if not status.success?

  rescue
    sleep 0.2
    retry
  end

  return ret
end
read_meta() click to toggle source
# File lib/cmus-bt/player.rb, line 48
def read_meta
  ret    = {}
  status = nil

  query.each_line { |l|
    case l
    when /^status (.+)/
      status = $1.to_sym

    when /^file (.+)/
      ret[:file] = File.basename($1)

    when /^tag (.+) (.+)/
      ret[$1.to_sym] = $2
    end
  }

  ret[:name] ||= ret[:file] if status == :playing

  return ret
end
status() click to toggle source
# File lib/cmus-bt/player.rb, line 39
def status
  ret = catch { |tag|
    query.each_line {|l| throw tag, $1.to_sym if /^status (.+)/ =~ l}
  }

  return ret
end