2017-06-01 18:12:49 +02:00
|
|
|
"""Displays bluetooth status (Bluez). Left mouse click launches manager app,
|
|
|
|
right click toggles bluetooth. Needs dbus-send to toggle bluetooth state.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* bluetooth.device : the device to read state from (default is hci0)
|
|
|
|
* bluetooth.manager : application to launch on click (blueman-manager)
|
2019-12-14 13:36:45 +01:00
|
|
|
* bluetooth.dbus_destination : dbus destination (defaults to org.blueman.Mechanism)
|
2019-12-18 22:39:48 +01:00
|
|
|
* bluetooth.dbus_destination_path : dbus destination path (defaults to /)
|
2019-12-18 22:53:58 +01:00
|
|
|
* bluetooth.right_click_popup : use popup menu when right-clicked (defaults to True)
|
2017-06-01 18:12:49 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import bumblebee.input
|
|
|
|
import bumblebee.output
|
|
|
|
import bumblebee.engine
|
|
|
|
import bumblebee.util
|
2017-06-02 02:25:40 +02:00
|
|
|
import bumblebee.popup
|
|
|
|
import logging
|
2017-06-01 18:12:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Module(bumblebee.engine.Module):
|
|
|
|
"""Bluetooth module."""
|
|
|
|
|
|
|
|
def __init__(self, engine, config):
|
|
|
|
"""Initialize."""
|
|
|
|
super(Module, self).__init__(engine, config,
|
|
|
|
bumblebee.output.Widget(
|
|
|
|
full_text=self.status))
|
|
|
|
|
|
|
|
device = self.parameter("device", "hci0")
|
|
|
|
self.manager = self.parameter("manager", "blueman-manager")
|
|
|
|
self._path = "/sys/class/bluetooth/{}".format(device)
|
|
|
|
self._status = "Off"
|
|
|
|
|
|
|
|
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE,
|
|
|
|
cmd=self.manager)
|
2019-12-18 22:53:58 +01:00
|
|
|
|
|
|
|
# determine whether to use pop-up menu or simply toggle the device on/off
|
|
|
|
right_click_popup = bumblebee.util.asbool(
|
|
|
|
self.parameter("right_click_popup", True))
|
|
|
|
|
|
|
|
if right_click_popup:
|
|
|
|
engine.input.register_callback(self,
|
|
|
|
button=bumblebee.input.RIGHT_MOUSE,
|
|
|
|
cmd=self.popup)
|
|
|
|
else:
|
|
|
|
engine.input.register_callback(self,
|
|
|
|
button=bumblebee.input.RIGHT_MOUSE,
|
|
|
|
cmd=self._toggle)
|
2017-06-01 18:12:49 +02:00
|
|
|
|
|
|
|
def status(self, widget):
|
|
|
|
"""Get status."""
|
|
|
|
return self._status
|
|
|
|
|
|
|
|
def update(self, widgets):
|
|
|
|
"""Update current state."""
|
|
|
|
if not os.path.exists(self._path):
|
|
|
|
self._status = "?"
|
|
|
|
return
|
|
|
|
|
|
|
|
# search for whichever rfkill directory available
|
|
|
|
try:
|
|
|
|
dirnames = next(os.walk(self._path))[1]
|
|
|
|
for dirname in dirnames:
|
|
|
|
m = re.match(r"rfkill[0-9]+", dirname)
|
|
|
|
if m is not None:
|
|
|
|
with open(os.path.join(self._path,
|
|
|
|
dirname,
|
|
|
|
'state'), 'r') as f:
|
|
|
|
state = int(f.read())
|
|
|
|
if state == 1:
|
|
|
|
self._status = "On"
|
|
|
|
else:
|
|
|
|
self._status = "Off"
|
|
|
|
return
|
|
|
|
|
|
|
|
except IOError:
|
|
|
|
self._status = "?"
|
|
|
|
|
|
|
|
def manager(self, widget):
|
|
|
|
"""Launch manager."""
|
|
|
|
bumblebee.util.execute(self.manager)
|
|
|
|
|
2017-06-02 02:25:40 +02:00
|
|
|
def popup(self, widget):
|
|
|
|
"""Show a popup menu."""
|
|
|
|
menu = bumblebee.popup.PopupMenu()
|
|
|
|
if self._status == "On":
|
|
|
|
menu.add_menuitem('Disable Bluetooth')
|
|
|
|
elif self._status == "Off":
|
|
|
|
menu.add_menuitem('Enable Bluetooth')
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
# show menu and get return code
|
|
|
|
ret = menu.show(widget)
|
|
|
|
if ret == 0:
|
|
|
|
# first (and only) item selected.
|
|
|
|
self._toggle()
|
|
|
|
|
2019-12-18 22:53:58 +01:00
|
|
|
def _toggle(self, widget=None):
|
2017-06-01 18:12:49 +02:00
|
|
|
"""Toggle bluetooth state."""
|
|
|
|
if self._status == "On":
|
|
|
|
state = "false"
|
|
|
|
else:
|
|
|
|
state = "true"
|
|
|
|
|
2019-12-14 13:36:45 +01:00
|
|
|
dst = self.parameter("dbus_destination", "org.blueman.Mechanism")
|
2019-12-18 22:39:48 +01:00
|
|
|
dst_path = self.parameter("dbus_destination_path", "/")
|
2019-12-14 13:36:45 +01:00
|
|
|
|
|
|
|
cmd = "dbus-send --system --print-reply --dest={}"\
|
2019-12-18 22:39:48 +01:00
|
|
|
" {} org.blueman.Mechanism.SetRfkillState"\
|
|
|
|
" boolean:{}".format(dst, dst_path, state)
|
2017-06-01 18:12:49 +02:00
|
|
|
|
2019-12-18 22:53:58 +01:00
|
|
|
logging.debug('bt: toggling bluetooth')
|
2017-06-01 18:12:49 +02:00
|
|
|
bumblebee.util.execute(cmd)
|
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
"""Get current state."""
|
|
|
|
state = []
|
|
|
|
|
|
|
|
if self._status == "?":
|
|
|
|
state = ["unknown"]
|
|
|
|
elif self._status == "On":
|
|
|
|
state = ["ON"]
|
|
|
|
else:
|
|
|
|
state = ["OFF"]
|
|
|
|
|
|
|
|
return state
|