module CMusBt::App

Public Class Methods

start(info) click to toggle source
# File lib/cmus-bt/main.rb, line 76
def start(info)
  case $mode
  when :list_device
    list_device

  when :run_app
    run_app(info)

  else
    raise("Not implement yet")
  end

rescue Exception => e
  STDERR.print("#{e.message}\n")
  exit(1)
end

Private Class Methods

availables() click to toggle source
# File lib/cmus-bt/main.rb, line 15
def availables
  ret = []
  a   = Device::Bluetooth.list
  b   = Device::Input.list

  a.each { |ai|
    b.each {|bi|
      if bi[:name] == ai[:address]
        ret << {
          :address  => ai[:address],
          :name     => ai[:name],
          :dev_file => bi[:dev_file],
        }
      end
    }
  }

  return ret
end
list_device() click to toggle source
# File lib/cmus-bt/main.rb, line 36
def list_device
  list = availables()
 
  if list.empty?
    print("Available device is not exist.\n")

  else
    list.each { |info| 
      print(" %17s    %s\n" % [info[:address], info[:name]])
    }
  end
end
run_app(targ) click to toggle source
# File lib/cmus-bt/main.rb, line 51
def run_app(targ)
  $logger.info("start #{APP_NAME} version #{CMusBt::VERSION}")
  list = availables()
  if list.empty?
    raise("available device is not exist")
  end

  if targ
    info = list.find {|inf| inf[:address] == targ || inf[:name] == targ}
    raise("specified device is not connected") if not info
  else
    info = list.first
  end

  thread = Thread.fork {Player.daemon(info)}
  status = system("cmus")
  raise("execute cmus failed (exit status #{$?}.") if not status

ensure
  thread&.raise
  thread&.join
  $logger.info("exit #{APP_NAME}")
end