actual scripts
This commit is contained in:
81
detect_bluetooth.py
Executable file
81
detect_bluetooth.py
Executable file
@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# Ensure that the default device gets set to bluetooth when a bluetooth
|
||||
# device is plugged in
|
||||
#
|
||||
# This allows the system volume meter to set the volume
|
||||
#
|
||||
# Also make it not full volume too (currently 50%)
|
||||
#
|
||||
# Timothy Allen <tim@treehouse.org.za>
|
||||
|
||||
import dbus
|
||||
import gobject
|
||||
import atexit
|
||||
import re
|
||||
|
||||
from subprocess import Popen, PIPE
|
||||
from dbus.mainloop.glib import DBusGMainLoop
|
||||
|
||||
def get_sinks():
|
||||
sinks = Popen(["pacmd", "list-sinks"], shell=False, stdout=PIPE).communicate()[0]
|
||||
m = re.findall(r"name: <(.*?)>", sinks)
|
||||
return m
|
||||
|
||||
def get_default_sink():
|
||||
sinks = Popen(["pacmd", "info"], shell=False, stdout=PIPE).communicate()[0]
|
||||
m = re.findall(r"Default sink name: (.*?)\s", sinks)
|
||||
return m
|
||||
|
||||
def set_default_sink(sink):
|
||||
print "Setting sink: %s" % sink
|
||||
Popen(["pacmd", "set-default-sink", sink], shell=False, stdout=PIPE).communicate()[0]
|
||||
|
||||
# Volume is a percent
|
||||
def set_volume(sink, volume):
|
||||
print "Setting volume for sink %s to: %d" % (sink, volume)
|
||||
volume = str(int(65536*(float(str(volume).strip("%"))/100)))
|
||||
Popen(["pacmd", "set-sink-volume", sink, volume], shell=False, stdout=PIPE).communicate()[0]
|
||||
|
||||
|
||||
def device_change(name, value, iface=None, path=None):
|
||||
#print "Name: %s \nValue: %s\nInterface: %s \nPath: %s" % (name, value, iface, path)
|
||||
if (value == "connected"):
|
||||
# set path as default pulse device
|
||||
for sink in get_sinks():
|
||||
if re.match("bluez", sink):
|
||||
set_default_sink(sink)
|
||||
# change volume
|
||||
set_volume(sink, volume)
|
||||
# elif (value == "disconnected"):
|
||||
# # change default device is there's one sink left
|
||||
# sinks = get_sinks()
|
||||
# if isinstance(sinks, basestring):
|
||||
# set_default_sink(sink)
|
||||
# else:
|
||||
# set_default_sink(initial_sink)
|
||||
|
||||
# Volume is a value out of 100
|
||||
volume=50
|
||||
|
||||
loop = gobject.MainLoop()
|
||||
dbus_loop = DBusGMainLoop()
|
||||
bus = dbus.SystemBus(mainloop=dbus_loop)
|
||||
|
||||
#bluez = bus.get_object("org.bluez", "/");
|
||||
#manager = dbus.Interface(bluez, "org.bluez.Manager")
|
||||
#adapter = dbus.Interface(manager.DefaultAdapter(), "org.bluez.Adapter")
|
||||
#proxy = bus.get_object("org.bluez", manager.DefaultAdapter())
|
||||
|
||||
bus.add_signal_receiver(device_change,
|
||||
signal_name=None,
|
||||
dbus_interface="org.bluez.Audio",
|
||||
interface_keyword="iface",
|
||||
path_keyword="path")
|
||||
|
||||
initial_sink = get_default_sink();
|
||||
atexit.register(loop.quit)
|
||||
|
||||
loop.run()
|
||||
|
||||
# vi: filetype=python:expandtab:shiftwidth=4:softtabstop=4:tw=0
|
Reference in New Issue
Block a user