main
Shinyzenith 2 years ago
parent 88c4f8482f
commit 2e3a3f5227
No known key found for this signature in database
GPG Key ID: 6DD485917B553B7B

@ -1,9 +1,10 @@
#!/usr/bin/python3
from utils import SWHKD_UTILS
import grp
import pwd
import getpass
import grp
import libevdev
import pwd
import signal
import sys
class SWHKD:
@ -11,23 +12,28 @@ class SWHKD:
Main Class.
"""
def __init__(self):
signal.signal(signal.SIGINT, self.signalhandler)
signal.signal(signal.SIGTERM, self.signalhandler)
self.utils = SWHKD_UTILS()
self.user = getpass.getuser()
def run_swhkd(self):
def signalhandler(self,sig,frame):
self.utils.log_info('Quitting!')
sys.exit(0)
def run_swhkd(self):
groups = [g.gr_name for g in grp.getgrall() if self.user in g.gr_mem]
gid = pwd.getpwnam(self.user).pw_gid
groups.append(grp.getgrgid(gid).gr_name)
for group in groups:
if group.lower() == "input":
self.utils.log_info("User is in input group, proceeding.")
self.utils.log_warn("User is in input group, proceeding.")
break;
fd = open('/dev/input/event7','rb')
device = libevdev.Device(fd)
if not device.has(libevdev.EV_KEY.BTN_LEFT):
print('This does not look like a mouse device')
self.utils.log_error("Device is not a mouse.")
sys.exit(0)
while True:
@ -35,8 +41,8 @@ class SWHKD:
if not event.matches(libevdev.EV_KEY):
continue
if event.matches(libevdev.EV_KEY.BTN_LEFT):
print('Left button event')
self.utils.log_info('Left button event')
elif event.matches(libevdev.EV_KEY.BTN_RIGHT):
print('Right button event')
self.utils.log_info('Right button event')
SWHKD().run_swhkd()

@ -0,0 +1,91 @@
#!/usr/bin/env python3
import sys
import libevdev
def print_capabilities(l):
v = l.driver_version
print("Input driver version is {}.{}.{}".format(v >> 16, (v >> 8) & 0xff, v & 0xff))
id = l.id
print("Input device ID: bus {:#x} vendor {:#x} product {:#x} version {:#x}".format(
id["bustype"],
id["vendor"],
id["product"],
id["version"],
))
print("Input device name: {}".format(l.name))
print("Supported events:")
for t, cs in l.evbits.items():
print(" Event type {} ({})".format(t.value, t.name))
for c in cs:
if t in [libevdev.EV_LED, libevdev.EV_SND, libevdev.EV_SW]:
v = l.value[c]
print(" Event code {} ({}) state {}".format(c.value, c.name, v))
else:
print(" Event code {} ({})".format(c.value, c.name))
if t == libevdev.EV_ABS:
a = l.absinfo[c]
print(" {:10s} {:6d}".format('Value', a.value))
print(" {:10s} {:6d}".format('Minimum', a.minimum))
print(" {:10s} {:6d}".format('Maximum', a.maximum))
print(" {:10s} {:6d}".format('Fuzz', a.fuzz))
print(" {:10s} {:6d}".format('Flat', a.flat))
print(" {:10s} {:6d}".format('Resolution', a.resolution))
print("Properties:")
for p in l.properties:
print(" Property type {} ({})".format(p.value, p.name))
def print_event(e):
print("Event: time {}.{:06d}, ".format(e.sec, e.usec), end='')
if e.matches(libevdev.EV_SYN):
if e.matches(libevdev.EV_SYN.SYN_MT_REPORT):
print("++++++++++++++ {} ++++++++++++".format(e.code.name))
elif e.matches(libevdev.EV_SYN.SYN_DROPPED):
print(">>>>>>>>>>>>>> {} >>>>>>>>>>>>".format(e.code.name))
else:
print("-------------- {} ------------".format(e.code.name))
else:
print("type {:02x} {} code {:03x} {:20s} value {:4d}".format(e.type.value, e.type.name, e.code.value, e.code.name, e.value))
def main(args):
path = args[1]
try:
with open(path, "rb") as fd:
dev = libevdev.Device(fd)
print_capabilities(dev)
print("################################\n"
"# Waiting for events #\n"
"################################")
while True:
try:
for e in dev.events():
print_event(e)
except libevdev.EventsDroppedException:
for e in dev.sync():
print_event(e)
except KeyboardInterrupt:
pass
except IOError as e:
import errno
if e.errno == errno.EACCES:
print("Insufficient permissions to access {}".format(path))
elif e.errno == errno.ENOENT:
print("Device {} does not exist".format(path))
else:
raise e
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: {} /dev/input/eventX".format(sys.argv[0]))
sys.exit(1)
main(sys.argv)

@ -1,3 +1,5 @@
import time
class SWHKD_UTILS():
"""
Helper Functions.
@ -10,10 +12,10 @@ class SWHKD_UTILS():
self.COLOR_RESET="\033[0m"
def log_info(self, message:str):
print(f"{self.COLOR_GREEN}INFO:{self.COLOR_RESET} {message}")
print(f"{self.COLOR_GREEN}[{time.ctime()}] INFO:{self.COLOR_RESET} {message}")
def log_error(self, message:str):
print(f"{self.COLOR_RED}ERROR:{self.COLOR_RESET} {message}")
print(f"{self.COLOR_RED}[{time.ctime()}] ERROR:{self.COLOR_RESET} {message}")
def log_warn(self, message:str):
print(f"{self.COLOR_YELLOW}WARN:{self.COLOR_RESET} {message}")
print(f"{self.COLOR_YELLOW}[{time.ctime()}] WARN:{self.COLOR_RESET} {message}")

Loading…
Cancel
Save