From 1c47c30c3ff646b446c0632974c3397836196f2c Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Fri, 27 Oct 2017 22:07:37 +0200 Subject: update keyhandler --- tp-key-handler | 109 +++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 87 insertions(+), 22 deletions(-) diff --git a/tp-key-handler b/tp-key-handler index ec17cf2..dd13f4c 100755 --- a/tp-key-handler +++ b/tp-key-handler @@ -3,6 +3,7 @@ import gtk import dbus import dbus.service import subprocess +import time from dbus.mainloop.glib import DBusGMainLoop import pynotify @@ -15,12 +16,41 @@ class KeyhandlerDBUSService(dbus.service.Object): self.wnotice = pynotify.Notification("WLAN", "", "") self.mnotice = pynotify.Notification("Mouse", "", "") - def _showBacklightState(self): - popen = subprocess.Popen(['/usr/bin/xbacklight'], stdout = subprocess.PIPE) + def _lid_status_open(self): + lidstatus = "state: unknown" + with open('/proc/acpi/button/lid/LID/state', 'r') as lidstatefile: + lidstatus = lidstatefile.read() + lidstatus = lidstatus.strip().replace("state: ", "") + return lidstatus == "open" + + def _dock_status_docked(self): + popen = subprocess.Popen(['/usr/bin/lsusb', '-d', '17ef:1010']) popen.wait() - (stdout,stderr) = popen.communicate(None) - state = int(float((stdout))) - self.bnotice.set_hint_int32("value", state) + return not popen.returncode + + def _bluetooth_set_status(self, state): + bus = dbus.SystemBus() + dev = bus.get_object("org.bluez", u'/org/bluez/hci0') + iface = dbus.Interface(dev, 'org.freedesktop.DBus.Properties') + iface.Set("org.bluez.Adapter1", "Powered", state) + + def _bluetooth_get_status(self, state): + bus = dbus.SystemBus() + dev = bus.get_object("org.bluez", u'/org/bluez/hci0') + iface = dbus.Interface(dev, 'org.freedesktop.DBus.Properties') + return iface.Get("org.bluez.Adapter1", "Powered") + + def _getBacklight(self): + with open("/sys/class/backlight/intel_backlight/brightness", "r") as f: + return int(f.readline().strip()) + + def _setBacklight(self, value): + with open("/sys/class/backlight/intel_backlight/brightness", "w") as f: + f.write(str(value)) + + def _showBacklightState(self): + state = self._getBacklight() * 100 / 255 + self.bnotice.set_hint_int32("value", int(state)) self.bnotice.set_hint_string("synchronous", "brightness") self.bnotice.show() @@ -43,6 +73,12 @@ class KeyhandlerDBUSService(dbus.service.Object): self.vnotice.set_hint_string("synchronous", "volume") self.vnotice.show() + def _rfkill_get(self, rfid): + popen = subprocess.Popen(['/usr/sbin/rfkill', 'list', rfid], stdout = subprocess.PIPE) + popen.wait() + (stdout,stderr) = popen.communicate() + return stdout.split("\n")[1].split(" ")[2] == "no" + def _showMicState(self): popen = subprocess.Popen(['/usr/bin/amixer', '-c', 'PCH', 'get', 'Capture'], stdout = subprocess.PIPE) popen.wait() @@ -65,7 +101,7 @@ class KeyhandlerDBUSService(dbus.service.Object): @dbus.service.method('org.elektranox.keyhandler') def volumeMute(self): #popen = subprocess.Popen(['/usr/bin/amixer', '-c', 'PCH', 'set', 'Master', 'toggle']) - popen = subprocess.Popen(['/usr/bin/amixer', 'set', 'Master', 'toggle']) + popen = subprocess.Popen(['/usr/bin/pactl', 'set-sink-mute', 'alsa_output.pci-0000_00_1b.0.analog-stereo', 'toggle']) popen.wait() self._showVolumeState() @@ -90,30 +126,50 @@ class KeyhandlerDBUSService(dbus.service.Object): @dbus.service.method('org.elektranox.keyhandler') def backlightDec(self): - popen = subprocess.Popen(['/usr/bin/xbacklight','-dec', '5']) - popen.wait() + brightness = self._getBacklight() - 5 + if brightness < 0: + brightness = 0 + self._setBacklight(brightness) self._showBacklightState() @dbus.service.method('org.elektranox.keyhandler') def backlightInc(self): - popen = subprocess.Popen(['/usr/bin/xbacklight','-inc', '5']) - popen.wait() + brightness = self._getBacklight() + 5 + if brightness > 255: + brightness = 255 + self._setBacklight(brightness) self._showBacklightState() @dbus.service.method('org.elektranox.keyhandler') def XF86Display(self): - popen = subprocess.Popen(['/usr/bin/notify-send','XF86Display']) - popen.wait() - # TODO: add some fancy binding - pass + lidopen = self._lid_status_open() + docked = self._dock_status_docked() + if not docked: + popen = subprocess.Popen(['/usr/bin/xrandr','--output', 'eDP1', '--auto']) + popen.wait() + popen = subprocess.Popen(['/usr/bin/xrandr','--output', 'DP2-1', '--off']) + popen.wait() + popen = subprocess.Popen(['/usr/bin/notify-send','Not docked']) + popen.wait() + elif lidopen: + popen = subprocess.Popen(['/usr/bin/xrandr','--output', 'eDP1', '--auto']) + popen.wait() + popen = subprocess.Popen(['/usr/bin/xrandr','--output', 'DP2-1', '--right-of', 'eDP1', '--auto']) + popen.wait() + popen = subprocess.Popen(['/usr/bin/notify-send','Docked with open lid']) + popen.wait() + else: + popen = subprocess.Popen(['/usr/bin/xrandr','--output', 'DP2-1', '--right-of', 'eDP1', '--auto']) + popen.wait() + popen = subprocess.Popen(['/usr/bin/xrandr','--output', 'eDP1', '--off']) + popen.wait() + popen = subprocess.Popen(['/usr/bin/notify-send','Docked with closed lid']) + popen.wait() @dbus.service.method('org.elektranox.keyhandler') def WLAN(self): # button itself is handled by systemd - popen = subprocess.Popen(['/usr/sbin/rfkill', 'list', 'wlan'], stdout = subprocess.PIPE) - popen.wait() - (stdout,stderr) = popen.communicate(None) - state = stdout.split("\n")[1].split(" ")[2] == "no" + state = self._rfkill_get("wlan") if state: self.wnotice.set_property("icon-name", "network-wireless-symbolic") self.wnotice.set_property("summary", "enabled") @@ -151,10 +207,19 @@ class KeyhandlerDBUSService(dbus.service.Object): @dbus.service.method('org.elektranox.keyhandler') def XF86Explorer(self): - popen = subprocess.Popen(['/usr/bin/notify-send','XF86Explorer']) - popen.wait() - # TODO: add some fancy binding - pass + state = self._rfkill_get("bluetooth") + if state: + subprocess.call(['/usr/sbin/rfkill','block', 'bluetooth']) + self.wnotice.set_property("icon-name", "/usr/share/icons/gnome/scalable/status/bluetooth-disabled-symbolic.svg") + self.wnotice.set_property("summary", "RF blocked") + else: + subprocess.call(['/usr/sbin/rfkill','unblock', 'bluetooth']) + self.wnotice.set_property("icon-name", "/usr/share/icons/gnome/scalable/status/bluetooth-active-symbolic.svg") + self.wnotice.set_property("summary", "RF unblocked") + self.wnotice.show() + if not state: + time.sleep(1) + self._bluetooth_set_status(True) DBusGMainLoop(set_as_default=True) service = KeyhandlerDBUSService() -- cgit v1.2.3