~marnold128/magic-keyboard

bd75aa9bd40a7b27ee3d8b287919c9067ecb867b — Matt Arnold 2 months ago b9122b7 main
quality fixes and spelling errors
5 files changed, 26 insertions(+), 36 deletions(-)

M mkd.py
M mkd/evdev.py
M mkd/fileutils.py
M mkd/ioutils.py
M mkd/statabuse.py
M mkd.py => mkd.py +16 -23
@@ 26,27 26,17 @@
import sys
import os
import queue
import tomllib
import syslog
import signal
from math import ceil, floor
from time import sleep
import socket
import threading
import gi

gi.require_version("Notify", "0.7")
from gi.repository import Notify, GLib
import evdev
import syslog
import multiprocessing
from evdev import InputDevice, categorize, ecodes, UInput
from evdev import ecodes, UInput
from mkd.ioutils import NullFile, SyslogFile
from mkd.fileutils import write_pid, pid_lock, get_config, read_script, HaltRequested
from mkd.evdev import (
    syn_key_press,
    syn_key_hold,
    syn_key_release,
    activate_device,
    release_device,
    STOP_VALUE,


@@ 54,20 44,25 @@ from mkd.evdev import (
    default_evread,
    Emulated_Device,
)
from mkd.misc import ContextDict, LostDeviceError
from mkd.misc import ContextDict

gi.require_version("Notify", "0.7")
from gi.repository import Notify  # noqa

stop_flag = threading.Event()
halt_in_progress = threading.Event()
evqueue = queue.Queue()
active_config = None
daemon_tmpfiles = ["~/.mkd.sock", "~/.mkd.pid"]
actual_send_notice = lambda m: Notify.Notification.new(
    "magic-keyboard", m, "dialog-information"
).show()

current_device = None
dispatch_event = None


def actual_send_notice(m):
    return Notify.Notification.new("magic-keyboard", m, "dialog-information").show()


def main():
    global dispatch_event
    if os.getuid() == 0 or os.geteuid == 0:


@@ 77,13 72,12 @@ def main():
        cfig = get_config(os.path.abspath("./mkd.conf"))
    else:
        cfig = get_config("~/.mkd.conf")
    if cfig == None:
    if cfig is None:
        print("Config Syntax Error")
        exit(2)
    if cfig.get("layout_file"):
        status, errors = read_script(cfig["layout_file"])
        if status == 0:

            exec(compile(errors, "layout", "exec"))
            print(vars)
            if "mk_evread" in vars():


@@ 111,7 105,7 @@ def main():
                    print(d)
            case "foreground":
                daemon_main(cfig)
            case other:
            case _:
                print("Unknown Subcommand")

        exit(0)


@@ 210,7 204,7 @@ def daemon_main(cfig):
        if event is not None:
            if event.type == ecodes.EV_KEY:
                rt = dispatch_event(evdev.util.categorize(event), script_context)
                if rt != None:
                if rt is None:
                    script_context = rt

        else:


@@ 291,7 285,7 @@ def uinput_thread(evqueue):
        if halt_in_progress.is_set():
            raise HaltRequested("Halt")
        keydata = evqueue.get()
        if type(keydata) == int and keydata == STOP_VALUE:
        if isinstance(keydata, int) and keydata == STOP_VALUE:
            break
        ui.write(*keydata)
        ui.syn()


@@ 307,9 301,8 @@ def uds_thread(sock):
    if stop_flag.is_set():  # we don't want to cause problems in cleanup
        return
    try:

        connection, client_address = sock.accept()
    except OSError as e:
    except OSError:
        sys.stderr.write("Socket cleanup happened SHUTDOWN NOW")
        halt_in_progress.set()
        stop_flag.set()


@@ 338,7 331,7 @@ def uds_thread(sock):
            connection.sendall("OK\n")
            pid = os.getpid()
            os.kill(pid, signal.SIGTERM)
        case other:
        case _:
            connection.sendall(b"unknown command\n")
            connection.close()


M mkd/evdev.py => mkd/evdev.py +5 -5
@@ 2,13 2,13 @@
Where all the evdev related functions go
"""

from math import ceil, floor
from math import ceil, floor  # noqa
from time import sleep
import evdev
from evdev import ecodes, InputDevice
from queue import Queue
from .misc import LostDeviceError

from .misc import ContextDict

DOWN = 1
UP = 0


@@ 22,7 22,7 @@ STOCK_LEDS = [
    (ecodes.LED_CAPSL, OFF),
    (ecodes.LED_SCROLLL, OFF),
]
from .misc import ContextDict


__devname = "sorcery-usb-keyboard"
__vendor = 0x0C


@@ 71,12 71,12 @@ def leds_loop(dev: InputDevice, hack: bool):
        for light in reversed(leds):
            try:
                dev.set_led(ecodes.ecodes[light[0]], ON)
            except OSError as e:
            except OSError:
                raise LostDeviceError("Lost device")
            sleep(0.25)
            try:
                dev.set_led(ecodes.ecodes[light[0]], OFF)
            except OSError as e:
            except OSError:
                raise LostDeviceError("Lost Device")
            sleep(0.25)
    else:

M mkd/fileutils.py => mkd/fileutils.py +1 -4
@@ 19,10 19,8 @@ def pid_lock(U_path):
        if os.path.exists(os.path.join("/proc", target_pid)):
            return True
        else:

            return False
    else:

        return False




@@ 42,12 40,11 @@ def get_config(U_path: str):


def read_script(u_path: str):

    with open(os.path.expanduser(u_path), "r") as f:
        code = f.read()

    try:
        ocode = compile(code, u_path, "exec")
        _ = compile(code, u_path, "exec")
        return 0, code
    except SyntaxError as e:
        return 1, str(e)

M mkd/ioutils.py => mkd/ioutils.py +2 -2
@@ 20,8 20,8 @@ class SyslogFile:
    """

    def write(self, msg):
        if type(msg) == str:
        if isinstance(msg, str):
            real = msg
        elif type(msg) == bytes:
        elif isinstance(msg, bytes):
            real = msg.decode("utf-8")
        syslog.syslog(syslog.LOG_DEBUG, real)

M mkd/statabuse.py => mkd/statabuse.py +2 -2
@@ 1,9 1,9 @@
"""
Here is the module where i abuse stats to calcualate
various input delays and things
The point of this project is to go at simulate human speeds. 
The point of this project is to go at simulate human speeds.
Under Normal circumstances. This is an accomidation device, not intended
to give super powers usually. The user will be able to configure the simulation 
to give super powers usually. The user will be able to configure the simulation
to their liking, or turn it off entirely
"""