diff options
author | PastLeo | 2019-02-19 01:22:09 +0800 |
---|---|---|
committer | PastLeo | 2019-02-19 01:22:15 +0800 |
commit | d88fcb6ba3743e66503bd4843a86d46a43b3d05f (patch) | |
tree | 081afb400af8bbd1b4a5c035eab1f6e4eec83637 /fix-bt-a2dp.rb | |
parent | 309f8c29bbce9d1acebd40792ca1eecf6faeb62c (diff) | |
download | aur-fix-bt-a2dp.tar.gz |
allow to set profile of a device
Diffstat (limited to 'fix-bt-a2dp.rb')
-rwxr-xr-x | fix-bt-a2dp.rb | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/fix-bt-a2dp.rb b/fix-bt-a2dp.rb index cd049f6f4315..c5991800ae8c 100755 --- a/fix-bt-a2dp.rb +++ b/fix-bt-a2dp.rb @@ -2,6 +2,7 @@ RUNNING_FILE = '/tmp/.fix-bt-a2dp.running' USER_FILE = '/etc/.fix-bt-a2dp.user' +DEVICE_PROFILE_FILE = '/etc/.fix-bt-a2dp.device-profiles' ROOT_UID = 0 if system("which notify-send &> /dev/null") @@ -23,6 +24,11 @@ def command_failed(name, stdout) end end +DEVICE_PROFILE_MAPPING = + File.exist?(DEVICE_PROFILE_FILE) ? Hash[ + File.read(DEVICE_PROFILE_FILE).lines.map {|l| l.split('=') } + ] : {} + def bluetoothctl_info_value(v) return true if v.downcase == 'yes' return false if v.downcase == 'no' @@ -39,7 +45,7 @@ def bluetoothctl_info(mac) k end.map do |k, v| if v.count == 1 - [k, bluetoothctl_info_value(v[0][1])] + [k, bluetoothctl_info_value(v[0][1].to_s)] else [k, v.map {|vv| vv[1]}] end @@ -50,7 +56,7 @@ end def have_audio_sink?(uuid_values) uuid_value = uuid_values.kind_of?(Array) ? uuid_values.join : uuid_values - uuid_value.downcase.gsub(' ', '').include?("audiosink") + uuid_value.to_s.downcase.gsub(' ', '').include?("audiosink") end def enable_a2dp(name, mac, bluez_mac, info) @@ -64,14 +70,15 @@ def enable_a2dp(name, mac, bluez_mac, info) sleep 1 - puts ">> pacmd set-card-profile bluez_card.#{bluez_mac} a2dp_sink" - set_card_profile_out = `pacmd set-card-profile bluez_card.#{bluez_mac} a2dp_sink` + profile_name = DEVICE_PROFILE_MAPPING[name] || 'a2dp_sink' + puts ">> pacmd set-card-profile bluez_card.#{bluez_mac} #{profile_name}" + set_card_profile_out = `pacmd set-card-profile bluez_card.#{bluez_mac} #{profile_name}` unless $?.success? && set_card_profile_out == '' command_failed('pacmd set-card-profile', set_card_profile_out) end puts "a2dp enabled for #{name}!" - gui_notify("#{name} connected and set to A2DP") + gui_notify("#{name} connected and set to #{profile_name}") end def scan_enable_a2dp @@ -82,8 +89,8 @@ def scan_enable_a2dp command_failed('`pacmd list-sinks`', pacmd_sinks) unless $?.success? bt_sinks_no_a2dp = bt_devs.lines.map do |bt_dev_line| - bt_dev_line.split(' ') - end.map do |_, mac, name| + /^(\S+)\s(\S+)\s(.+)$/.match(bt_dev_line).to_a.drop(2) + end.map do |mac, name| [name, mac, "#{mac.gsub(':', '_')}", bluetoothctl_info(mac)] end.keep_if do |name, mac, bluez_mac, info| !info.empty? && @@ -132,7 +139,19 @@ def main end if Process.uid == ROOT_UID - if ARGV[0] == 'set-user' + if ARGV[0] == 'set-device-profile' + `rm -f #{DEVICE_PROFILE_FILE}` + name = ARGV[1] + profile = ARGV[2] + File.open(DEVICE_PROFILE_FILE, 'w') do |f| + f.write( + DEVICE_PROFILE_MAPPING + .merge(name => profile) + .to_a.map {|kv| kv.join('=')}.join("\n") + ) + end + exit 0 + elsif ARGV[0] == 'set-user' `rm -f #{USER_FILE}` user = ARGV[1].strip uid = `id -u #{user}`.strip |