Module: CMusBt::Player

Defined in:
lib/cmus-bt/player.rb

Constant Summary collapse

EVENT_STRUCTURE =

from struct input_event on <linux/event.h>

"l!l!S!S!i!"
FUNC_TABLE =
{
  200 => :on_play,  # KEY_PLAYCD
  201 => :on_pause, # KEY_PAUSECD
  163 => :on_next,  # KEY_NEXTSONG
  165 => :on_prev,  # KEY_PREVIOUSSONG
}

Class Method Summary collapse

Class Method Details

.daemon(info) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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

.on_nextObject



88
89
90
91
# File 'lib/cmus-bt/player.rb', line 88

def on_next
  system("cmus-remote --next")
  $logger.info("NEXT")
end

.on_pauseObject



82
83
84
85
# File 'lib/cmus-bt/player.rb', line 82

def on_pause
  system("cmus-remote --pause")
  $logger.info("PAUSE")
end

.on_playObject



71
72
73
74
75
76
77
78
79
# 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_prevObject



94
95
96
97
# File 'lib/cmus-bt/player.rb', line 94

def on_prev
  system("cmus-remote --prev")
  $logger.info("PREV")
end

.queryObject



25
26
27
28
29
30
31
32
33
34
35
36
# 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_metaObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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

.statusObject



39
40
41
42
43
44
45
# 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