Skip to content

Commit

Permalink
Removed hard dependency on header files
Browse files Browse the repository at this point in the history
Changes implemented by adding a new method to actions.py which gets
the "event code" of a key. This method is used in the ydotool.py
script to get the event code of a key and then use it to send the
key press event to the system.
Has a helper dictionary to map key names to event codes in
input_event_codes.py

Also I cleaned up some code; removed the socket thing.
  • Loading branch information
relma2 committed Dec 27, 2024
1 parent b832c48 commit c57a477
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 51 deletions.
20 changes: 1 addition & 19 deletions src/picker/abstractionhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,5 @@
def is_installed(executable: str) -> bool:
return shutil.which(executable) is not None


def is_wayland() -> bool:
return "WAYLAND_DISPLAY" in os.environ

def get_event_code(char: str, pressrelease: bool = True) -> str:
keypress = "KEY_" + char.upper()
# Consult file /usr/include/linux/input-event-codes.h for the event code
event_code = ""
with open("/usr/include/linux/input-event-codes.h") as file:
for line in file:
# Line is of the form #define keypress event_code
if keypress in line.split():
event_code = line.split()[2]
break

# pressrelease is a bool indicating whether the event code is for a key press or release
if pressrelease:
return f"{event_code}:1"
else:
return f"{event_code}:0"
return "WAYLAND_DISPLAY" in os.environ
4 changes: 4 additions & 0 deletions src/picker/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .clipboarder.clipboarder import Clipboarder
from .models import Action
from .typer.typer import Typer
from .input_event_codes import keycodes


def execute_action(
Expand Down Expand Up @@ -32,3 +33,6 @@ def execute_action(

def __get_codepoints(char: str) -> str:
return "-".join(f"{ord(c):x}" for c in char)

def __get_event_code(char: str) -> str:
return str(keycodes["KEY_" + char.upper()])
2 changes: 1 addition & 1 deletion src/picker/argument_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __parse_arguments(only_known: bool) -> argparse.Namespace:
metavar="FILE",
help="Read characters from this file instead, one entry per line",
)
parser.add_argument("--prompt", "-r", dest="prompt", action="store", default="😀 ", help="Set rofimoj's prompt")
parser.add_argument("--prompt", "-r", dest="prompt", action="store", default="😀 ", help="Set rofimoji's prompt")
parser.add_argument(
"--selector-args",
dest="selector_args",
Expand Down
88 changes: 88 additions & 0 deletions src/picker/input_event_codes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Consulted file /usr/include/linux/input-event-codes.h for the event codes
keycodes = {
"KEY_RESERVED":0,
"KEY_ESC":1,
"KEY_1":2,
"KEY_2":3,
"KEY_3":4,
"KEY_4":5,
"KEY_5":6,
"KEY_6":7,
"KEY_7":8,
"KEY_8":9,
"KEY_9":10,
"KEY_0":11,
"KEY_MINUS":12,
"KEY_EQUAL":13,
"KEY_BACKSPACE":14,
"KEY_TAB":15,
"KEY_Q":16,
"KEY_W":17,
"KEY_E":18,
"KEY_R":19,
"KEY_T":20,
"KEY_Y":21,
"KEY_U":22,
"KEY_I":23,
"KEY_O":24,
"KEY_P":25,
"KEY_LEFTBRACE":26,
"KEY_RIGHTBRACE":27,
"KEY_ENTER":28,
"KEY_LEFTCTRL":29,
"KEY_A":30,
"KEY_S":31,
"KEY_D":32,
"KEY_F":33,
"KEY_G":34,
"KEY_H":35,
"KEY_J":36,
"KEY_K":37,
"KEY_L":38,
"KEY_SEMICOLON":39,
"KEY_APOSTROPHE":40,
"KEY_GRAVE":41,
"KEY_LEFTSHIFT":42,
"KEY_BACKSLASH":43,
"KEY_Z":44,
"KEY_X":45,
"KEY_C":46,
"KEY_V":47,
"KEY_B":48,
"KEY_N":49,
"KEY_M":50,
"KEY_COMMA":51,
"KEY_DOT":52,
"KEY_SLASH":53,
"KEY_RIGHTSHIFT":54,
"KEY_KPASTERISK":55,
"KEY_LEFTALT":56,
"KEY_SPACE":57,
"KEY_CAPSLOCK":58,
"KEY_F1":59,
"KEY_F2":60,
"KEY_F3":61,
"KEY_F4":62,
"KEY_F5":63,
"KEY_F6":64,
"KEY_F7":65,
"KEY_F8":66,
"KEY_F9":67,
"KEY_F10":68,
"KEY_NUMLOCK":69,
"KEY_SCROLLLOCK":70,
"KEY_KP7":71,
"KEY_KP8":72,
"KEY_KP9":73,
"KEY_KPMINUS":74,
"KEY_KP4":75,
"KEY_KP5":76,
"KEY_KP6":77,
"KEY_KPPLUS":78,
"KEY_KP1":79,
"KEY_KP2":80,
"KEY_KP3":81,
"KEY_KP0":82,
"KEY_KPDOT":83,
"KEY_INSERT": 110,
}
48 changes: 17 additions & 31 deletions src/picker/typer/ydotool.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import os
import subprocess as sp
from subprocess import run

from ..abstractionhelper import get_event_code, is_installed
from ..action import __get_codepoints as get_codepoints
from ..abstractionhelper import is_installed
from ..action import __get_codepoints as get_codepoints, __get_event_code as get_event_code
from .typer import Typer

class YdotoolTyper(Typer):
def __init__(self):
super().__init__()

self.socket = "/run/user/1000/.ydotool_socket"
if "YDOTOOL_SOCKET" in os.environ:
self.socket = os.environ["YDOTOOL_SOCKET"]

@staticmethod
def name():
return "ydotool"

@staticmethod
def supported():
try:
return is_installed("ydotool")
except sp.CalledProcessError:
return False
return is_installed("ydotool")

def get_active_window(self):
return "not possible with ydotool"
Expand All @@ -37,27 +25,25 @@ def type_characters(self, characters: str, active_window: str) -> None:
unicode_code_point = get_codepoints(character)

# Get keypresses for Ctrl, Shift, U, and the unicode code point
Ctrl = get_event_code("LeftCtrl")
Shift = get_event_code("LeftShift")
U = get_event_code("U")
Ctrl_release = get_event_code("LeftCtrl", False)
Shift_release = get_event_code("LeftShift", False)
U_release = get_event_code("U", False)
Ctrl = get_event_code("LeftCtrl") + ":1"
Shift = get_event_code("LeftShift") + ":1"
U_press = get_event_code("U") + ":1"
Ctrl_release = get_event_code("LeftCtrl") + ":0"
Shift_release = get_event_code("LeftShift") + ":0"
U_release = get_event_code("U") + ":0"
points = []

for point in unicode_code_point:
points.append(get_event_code(point))
points.append(get_event_code(point, False))
points.append(get_event_code(point) + ":1")
points.append(get_event_code(point) + ":0")

# Send the event codes to ydotool
the_array = ["ydotool", "key", "--key-delay", "1", Ctrl, Shift, U, U_release] + points + [Shift_release, Ctrl_release]
run(the_array, env=os.environ.copy().update({"YDOTOOL_SOCKET": self.socket}))
run(["ydotool", "key", Ctrl, Shift, U_press, U_release] + points + [Shift_release, Ctrl_release])

def insert_from_clipboard(self, active_window: str) -> None:
Shift = get_event_code("LeftShift")
Shift_release = get_event_code("LeftShift", False)
Insert = get_event_code("Insert")
Insert_release = get_event_code("Insert", False)
Shift = get_event_code("LeftShift") + ":1"
Shift_release = get_event_code("LeftShift") + ":0"
Insert = get_event_code("Insert") + ":1"
Insert_release = get_event_code("Insert") + ":0"

run(["ydotool", "key", Shift, Insert, Insert_release, Shift_release],
env=os.environ.copy().update({"YDOTOOL_SOCKET": self.socket}))
run(["ydotool", "key", Shift, Insert, Insert_release, Shift_release])

0 comments on commit c57a477

Please sign in to comment.