2020-04-29 20:16:05 +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)
|
|
|
|
* bluetooth.dbus_destination : dbus destination (defaults to org.blueman.Mechanism)
|
|
|
|
* bluetooth.dbus_destination_path : dbus destination path (defaults to /)
|
|
|
|
* bluetooth.right_click_popup : use popup menu when right-clicked (defaults to True)
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import logging
|
|
|
|
|
2020-04-29 20:19:18 +02:00
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.input
|
2020-04-29 20:16:05 +02:00
|
|
|
|
2020-04-29 20:19:18 +02:00
|
|
|
import util.cli
|
|
|
|
import util.format
|
|
|
|
import util.popup
|
2020-04-29 20:16:05 +02:00
|
|
|
|
2020-04-29 20:19:18 +02:00
|
|
|
class Module(core.module.Module):
|
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.status))
|
2020-04-29 20:16:05 +02:00
|
|
|
|
2020-04-29 20:16:19 +02:00
|
|
|
device = self.parameter('device', 'hci0')
|
|
|
|
self.manager = self.parameter('manager', 'blueman-manager')
|
|
|
|
self._path = '/sys/class/bluetooth/{}'.format(device)
|
|
|
|
self._status = 'Off'
|
2020-04-29 20:16:05 +02:00
|
|
|
|
2020-04-29 20:19:18 +02:00
|
|
|
core.input.register(self, button=core.input.LEFT_MOUSE,
|
2020-04-29 20:16:05 +02:00
|
|
|
cmd=self.manager)
|
|
|
|
|
|
|
|
# determine whether to use pop-up menu or simply toggle the device on/off
|
2020-04-29 20:19:18 +02:00
|
|
|
right_click_popup = util.format.asbool(
|
2020-04-29 20:16:19 +02:00
|
|
|
self.parameter('right_click_popup', True))
|
2020-04-29 20:16:05 +02:00
|
|
|
|
|
|
|
if right_click_popup:
|
2020-04-29 20:19:18 +02:00
|
|
|
core.input.register(self,
|
|
|
|
button=core.input.RIGHT_MOUSE,
|
2020-04-29 20:16:05 +02:00
|
|
|
cmd=self.popup)
|
|
|
|
else:
|
2020-04-29 20:19:18 +02:00
|
|
|
core.input.register(self,
|
|
|
|
button=core.input.RIGHT_MOUSE,
|
2020-04-29 20:16:05 +02:00
|
|
|
cmd=self._toggle)
|
|
|
|
|
|
|
|
def status(self, widget):
|
|
|
|
"""Get status."""
|
|
|
|
return self._status
|
|
|
|
|
2020-04-29 20:19:18 +02:00
|
|
|
def update(self):
|
2020-04-29 20:16:05 +02:00
|
|
|
"""Update current state."""
|
|
|
|
if not os.path.exists(self._path):
|
2020-04-29 20:16:19 +02:00
|
|
|
self._status = '?'
|
2020-04-29 20:16:05 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
# search for whichever rfkill directory available
|
|
|
|
try:
|
|
|
|
dirnames = next(os.walk(self._path))[1]
|
|
|
|
for dirname in dirnames:
|
2020-04-29 20:16:19 +02:00
|
|
|
m = re.match(r'rfkill[0-9]+', dirname)
|
2020-04-29 20:16:05 +02:00
|
|
|
if m is not None:
|
|
|
|
with open(os.path.join(self._path,
|
|
|
|
dirname,
|
|
|
|
'state'), 'r') as f:
|
|
|
|
state = int(f.read())
|
|
|
|
if state == 1:
|
2020-04-29 20:16:19 +02:00
|
|
|
self._status = 'On'
|
2020-04-29 20:16:05 +02:00
|
|
|
else:
|
2020-04-29 20:16:19 +02:00
|
|
|
self._status = 'Off'
|
2020-04-29 20:16:05 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
except IOError:
|
2020-04-29 20:16:19 +02:00
|
|
|
self._status = '?'
|
2020-04-29 20:16:05 +02:00
|
|
|
|
|
|
|
def popup(self, widget):
|
|
|
|
"""Show a popup menu."""
|
2020-04-29 20:19:18 +02:00
|
|
|
menu = util.popup.PopupMenu()
|
2020-04-29 20:16:19 +02:00
|
|
|
if self._status == 'On':
|
2020-04-29 20:16:05 +02:00
|
|
|
menu.add_menuitem('Disable Bluetooth')
|
2020-04-29 20:16:19 +02:00
|
|
|
elif self._status == 'Off':
|
2020-04-29 20:16:05 +02:00
|
|
|
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()
|
|
|
|
|
|
|
|
def _toggle(self, widget=None):
|
|
|
|
"""Toggle bluetooth state."""
|
2020-04-29 20:16:19 +02:00
|
|
|
if self._status == 'On':
|
|
|
|
state = 'false'
|
2020-04-29 20:16:05 +02:00
|
|
|
else:
|
2020-04-29 20:16:19 +02:00
|
|
|
state = 'true'
|
2020-04-29 20:16:05 +02:00
|
|
|
|
2020-04-29 20:16:19 +02:00
|
|
|
dst = self.parameter('dbus_destination', 'org.blueman.Mechanism')
|
|
|
|
dst_path = self.parameter('dbus_destination_path', '/')
|
2020-04-29 20:16:05 +02:00
|
|
|
|
2020-04-29 20:16:19 +02:00
|
|
|
cmd = 'dbus-send --system --print-reply --dest={}'\
|
|
|
|
' {} org.blueman.Mechanism.SetRfkillState'\
|
|
|
|
' boolean:{}'.format(dst, dst_path, state)
|
2020-04-29 20:16:05 +02:00
|
|
|
|
|
|
|
logging.debug('bt: toggling bluetooth')
|
2020-04-29 20:19:18 +02:00
|
|
|
util.cli.execute(cmd)
|
2020-04-29 20:16:05 +02:00
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
"""Get current state."""
|
|
|
|
state = []
|
|
|
|
|
2020-04-29 20:16:19 +02:00
|
|
|
if self._status == '?':
|
|
|
|
state = ['unknown']
|
|
|
|
elif self._status == 'On':
|
|
|
|
state = ['ON']
|
2020-04-29 20:16:05 +02:00
|
|
|
else:
|
2020-04-29 20:16:19 +02:00
|
|
|
state = ['OFF']
|
2020-04-29 20:16:05 +02:00
|
|
|
|
|
|
|
return state
|
2020-04-29 20:19:18 +02:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|