Skip to content

Instantly share code, notes, and snippets.

@tjvr
Last active Apr 13, 2019
Embed
What would you like to do?
Flirc DBUS MPRIS Daemon
[Unit]
Description=Control MPRIS media players with your Flirc
[Service]
ExecStart=/usr/bin/flirc_mpris.py
Restart=always
[Install]
WantedBy=default.target
#!/usr/bin/python3
import evdev
import mpris2
from evdev import ecodes
dev = evdev.InputDevice('/dev/input/by-id/usb-flirc.tv_flirc-event-kbd')
mappings = {
"KEY_NEXTSONG": "Next",
"KEY_PREVIOUSSONG": "Previous",
"KEY_PLAYPAUSE": "PlayPause",
"KEY_PAUSE": "Pause",
"KEY_PLAY": "Play",
"KEY_STOP": "Stop",
}
def handle(event: evdev.KeyEvent):
if event.keystate != evdev.KeyEvent.key_down:
return
try:
uri = mpris2.get_players_uri()
except StopIteration:
print("No media player found")
return
method_name = mappings.get(event.keycode)
if not method_name:
return
player = mpris2.Player(dbus_interface_info={'dbus_uri': next(mpris2.get_players_uri())})
print("player.{}()".format(method_name))
method = getattr(player, method_name)
method()
for event in dev.read_loop():
if event.type != ecodes.EV_KEY:
continue
key_event = evdev.categorize(event)
print(key_event)
handle(key_event)
evdev==1.2.0
mpris2==1.0.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment