parent
b63c19aef5
commit
ffd8b998b2
@ -1,60 +0,0 @@
|
||||
#!/usr/bin/python3
|
||||
import asyncio
|
||||
import getpass
|
||||
import grp
|
||||
import os
|
||||
import pwd
|
||||
import signal
|
||||
import sys
|
||||
|
||||
from utils.log import LOG_UTILS
|
||||
from utils.input import INPUT_UTILS
|
||||
from utils.config import CONFIG_PARSER
|
||||
|
||||
|
||||
class SWHKD:
|
||||
def __init__(self):
|
||||
signal.signal(signal.SIGINT, self.signalhandler)
|
||||
signal.signal(signal.SIGTERM, self.signalhandler)
|
||||
self.log_util = LOG_UTILS()
|
||||
self.input_util = INPUT_UTILS()
|
||||
self.user = getpass.getuser()
|
||||
self.config_parser = CONFIG_PARSER()
|
||||
|
||||
def signalhandler(self,sig,frame):
|
||||
print('\033[1;31mEXIT: Quitting SWHKD.\033[0m')
|
||||
sys.exit(0)
|
||||
|
||||
async def run_swhkd(self):
|
||||
if os.getuid() == 0:
|
||||
await self.log_util.log_error('Refusing to run SWHKD as root.')
|
||||
sys.exit(1)
|
||||
|
||||
# Permission check
|
||||
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)
|
||||
if "input" not in groups:
|
||||
await self.log_util.log_error("User is in not in input group, exiting.")
|
||||
sys.exit(1)
|
||||
|
||||
# Config parsing
|
||||
try:
|
||||
config = await self.config_parser.parse("{0}swhkd/config.json".format(os.environ.get("XDG_CONFIG_HOME")))
|
||||
except FileNotFoundError:
|
||||
try:
|
||||
config = await self.config_parser.parse("~/.config/swhkd/config.json")
|
||||
except FileNotFoundError:
|
||||
await self.log_util.log_error("Failed to parse config files.")
|
||||
sys.exit(1)
|
||||
|
||||
# Fetch events
|
||||
keyboards = await self.input_util.get_keyboard_devices()
|
||||
if not keyboards:
|
||||
await self.log_util.log_error("No keyboard devices found.")
|
||||
sys.exit(1)
|
||||
for keyboard in keyboards:
|
||||
await self.input_util.get_keyboard_events(keyboard)
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(SWHKD().run_swhkd())
|
@ -1,14 +0,0 @@
|
||||
SAMPLE JSON CONFIG
|
||||
First XDG_CONFIG_HOME/swhkd/config.json is checked
|
||||
and then ~/.config/swhkd/config.json
|
||||
Delete the first 4 lines before using this file.
|
||||
[
|
||||
{
|
||||
"combo": ["KEY_ENTER", "KEY_LEFTMETA"],
|
||||
"action": "foot -e ranger"
|
||||
},
|
||||
{
|
||||
"combo": ["KEY_ENTER", "KEY_LEFTCTRL"],
|
||||
"action": "pcmanfm"
|
||||
}
|
||||
]
|
@ -1,18 +0,0 @@
|
||||
#!/usr/bin/python3
|
||||
import json
|
||||
from . log import LOG_UTILS
|
||||
|
||||
class CONFIG_PARSER:
|
||||
def __init__(self):
|
||||
self.LOG_UTILS = LOG_UTILS()
|
||||
|
||||
async def parse(self,config_file):
|
||||
output=[]
|
||||
with open(config_file, 'r') as file:
|
||||
contents = json.loads(file.read())
|
||||
for arr in contents:
|
||||
pair=[]
|
||||
for key, value in arr.items():
|
||||
pair.append(value)
|
||||
output.append(pair)
|
||||
return output
|
@ -1,40 +0,0 @@
|
||||
import glob
|
||||
import libevdev
|
||||
import os
|
||||
from . log import LOG_UTILS
|
||||
|
||||
class INPUT_UTILS:
|
||||
def __init__(self):
|
||||
self.log_util=LOG_UTILS()
|
||||
|
||||
async def check_keyboard(self,device_path) -> bool :
|
||||
with open(device_path, 'rb') as fd:
|
||||
device = libevdev.Device(fd)
|
||||
if device.has(libevdev.EV_KEY.KEY_ENTER):
|
||||
await self.log_util.log_info("Device {} is a keyboard".format(device_path))
|
||||
return True
|
||||
else:
|
||||
await self.log_util.log_error("Device {} is not a keyboard".format(device_path))
|
||||
return False
|
||||
|
||||
async def get_keyboard_devices(self):
|
||||
devices = glob.glob('/dev/input/event*')
|
||||
keyboards = []
|
||||
for device in devices:
|
||||
out = await self.check_keyboard(device)
|
||||
if out ==True:
|
||||
keyboards.append(device)
|
||||
return keyboards
|
||||
|
||||
async def run_system_command(self,command:str) -> None:
|
||||
os.system(f"setsid -f {command} 1>/dev/null 2>&1 3>&1")
|
||||
|
||||
async def get_keyboard_events(self,device_path:str) -> None:
|
||||
with open(device_path, 'rb') as fd:
|
||||
device = libevdev.Device(fd)
|
||||
for event in device.events():
|
||||
if event.matches(libevdev.EV_MSC):
|
||||
continue
|
||||
if event.matches(libevdev.EV_SYN.SYN_REPORT):
|
||||
continue
|
||||
await self.log_util.log_info(event)
|
@ -1,19 +0,0 @@
|
||||
import sys
|
||||
import time
|
||||
|
||||
class LOG_UTILS():
|
||||
def __init__(self):
|
||||
self.COLOR_RED="\033[1;31m"
|
||||
self.COLOR_GREEN="\033[1;32m"
|
||||
self.COLOR_YELLOW="\033[1;33m"
|
||||
self.COLOR_BLUE="\033[1;34m"
|
||||
self.COLOR_RESET="\033[0m"
|
||||
|
||||
async def log_info(self, message:str):
|
||||
print(f"{self.COLOR_GREEN}[{time.ctime()}] INFO:{self.COLOR_RESET} {message}")
|
||||
|
||||
async def log_error(self, message:str):
|
||||
print(f"{self.COLOR_RED}[{time.ctime()}] ERROR:{self.COLOR_RESET} {message}", file=sys.stderr)
|
||||
|
||||
async def log_warn(self, message:str):
|
||||
print(f"{self.COLOR_YELLOW}[{time.ctime()}] WARN:{self.COLOR_RESET} {message}")
|
Loading…
Reference in new issue