Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Yubikey 没拔! From: https://cfp.vim-cn.com/cbf05?py
import sys
import subprocess
import notify2
import dbus
from gi.repository import GLib
from dbus.mainloop.glib import DBusGMainLoop
import yubico
def check(bus, msg):
if msg.get_path() != "/ScreenSaver":
return
args = msg.get_args_list()
if args[0]:
print("Screen locked", file=sys.stderr)
else:
print("Screen unlocked", file=sys.stderr)
return
try:
_ = yubico.find_yubikey(debug=False)
except yubico.yubico_exception.YubicoError:
return
else:
print("Found yubikey", file=sys.stderr)
n = notify2.Notification("Yubikey", "Yubikey 没拔!", "dialog-warning")
n.show()
subprocess.run(["loginctl", "unlock-session"])
if __name__ == '__main__':
DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
bus.add_match_string(
"type='signal'," +
"path='/ScreenSaver'," +
"interface='org.freedesktop.ScreenSaver'," +
"member='ActiveChanged'")
bus.add_message_filter(check)
notify2.init(sys.argv[0])
mainloop = GLib.MainLoop()
mainloop.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment