Skip to content

Commit c57a477

Browse files
committed
Removed hard dependency on header files
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.
1 parent b832c48 commit c57a477

File tree

5 files changed

+111
-51
lines changed

5 files changed

+111
-51
lines changed

src/picker/abstractionhelper.py

+1-19
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,5 @@
55
def is_installed(executable: str) -> bool:
66
return shutil.which(executable) is not None
77

8-
98
def is_wayland() -> bool:
10-
return "WAYLAND_DISPLAY" in os.environ
11-
12-
def get_event_code(char: str, pressrelease: bool = True) -> str:
13-
keypress = "KEY_" + char.upper()
14-
# Consult file /usr/include/linux/input-event-codes.h for the event code
15-
event_code = ""
16-
with open("/usr/include/linux/input-event-codes.h") as file:
17-
for line in file:
18-
# Line is of the form #define keypress event_code
19-
if keypress in line.split():
20-
event_code = line.split()[2]
21-
break
22-
23-
# pressrelease is a bool indicating whether the event code is for a key press or release
24-
if pressrelease:
25-
return f"{event_code}:1"
26-
else:
27-
return f"{event_code}:0"
9+
return "WAYLAND_DISPLAY" in os.environ

src/picker/action.py

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .clipboarder.clipboarder import Clipboarder
44
from .models import Action
55
from .typer.typer import Typer
6+
from .input_event_codes import keycodes
67

78

89
def execute_action(
@@ -32,3 +33,6 @@ def execute_action(
3233

3334
def __get_codepoints(char: str) -> str:
3435
return "-".join(f"{ord(c):x}" for c in char)
36+
37+
def __get_event_code(char: str) -> str:
38+
return str(keycodes["KEY_" + char.upper()])

src/picker/argument_parsing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def __parse_arguments(only_known: bool) -> argparse.Namespace:
6666
metavar="FILE",
6767
help="Read characters from this file instead, one entry per line",
6868
)
69-
parser.add_argument("--prompt", "-r", dest="prompt", action="store", default="😀 ", help="Set rofimoj's prompt")
69+
parser.add_argument("--prompt", "-r", dest="prompt", action="store", default="😀 ", help="Set rofimoji's prompt")
7070
parser.add_argument(
7171
"--selector-args",
7272
dest="selector_args",

src/picker/input_event_codes.py

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Consulted file /usr/include/linux/input-event-codes.h for the event codes
2+
keycodes = {
3+
"KEY_RESERVED":0,
4+
"KEY_ESC":1,
5+
"KEY_1":2,
6+
"KEY_2":3,
7+
"KEY_3":4,
8+
"KEY_4":5,
9+
"KEY_5":6,
10+
"KEY_6":7,
11+
"KEY_7":8,
12+
"KEY_8":9,
13+
"KEY_9":10,
14+
"KEY_0":11,
15+
"KEY_MINUS":12,
16+
"KEY_EQUAL":13,
17+
"KEY_BACKSPACE":14,
18+
"KEY_TAB":15,
19+
"KEY_Q":16,
20+
"KEY_W":17,
21+
"KEY_E":18,
22+
"KEY_R":19,
23+
"KEY_T":20,
24+
"KEY_Y":21,
25+
"KEY_U":22,
26+
"KEY_I":23,
27+
"KEY_O":24,
28+
"KEY_P":25,
29+
"KEY_LEFTBRACE":26,
30+
"KEY_RIGHTBRACE":27,
31+
"KEY_ENTER":28,
32+
"KEY_LEFTCTRL":29,
33+
"KEY_A":30,
34+
"KEY_S":31,
35+
"KEY_D":32,
36+
"KEY_F":33,
37+
"KEY_G":34,
38+
"KEY_H":35,
39+
"KEY_J":36,
40+
"KEY_K":37,
41+
"KEY_L":38,
42+
"KEY_SEMICOLON":39,
43+
"KEY_APOSTROPHE":40,
44+
"KEY_GRAVE":41,
45+
"KEY_LEFTSHIFT":42,
46+
"KEY_BACKSLASH":43,
47+
"KEY_Z":44,
48+
"KEY_X":45,
49+
"KEY_C":46,
50+
"KEY_V":47,
51+
"KEY_B":48,
52+
"KEY_N":49,
53+
"KEY_M":50,
54+
"KEY_COMMA":51,
55+
"KEY_DOT":52,
56+
"KEY_SLASH":53,
57+
"KEY_RIGHTSHIFT":54,
58+
"KEY_KPASTERISK":55,
59+
"KEY_LEFTALT":56,
60+
"KEY_SPACE":57,
61+
"KEY_CAPSLOCK":58,
62+
"KEY_F1":59,
63+
"KEY_F2":60,
64+
"KEY_F3":61,
65+
"KEY_F4":62,
66+
"KEY_F5":63,
67+
"KEY_F6":64,
68+
"KEY_F7":65,
69+
"KEY_F8":66,
70+
"KEY_F9":67,
71+
"KEY_F10":68,
72+
"KEY_NUMLOCK":69,
73+
"KEY_SCROLLLOCK":70,
74+
"KEY_KP7":71,
75+
"KEY_KP8":72,
76+
"KEY_KP9":73,
77+
"KEY_KPMINUS":74,
78+
"KEY_KP4":75,
79+
"KEY_KP5":76,
80+
"KEY_KP6":77,
81+
"KEY_KPPLUS":78,
82+
"KEY_KP1":79,
83+
"KEY_KP2":80,
84+
"KEY_KP3":81,
85+
"KEY_KP0":82,
86+
"KEY_KPDOT":83,
87+
"KEY_INSERT": 110,
88+
}

src/picker/typer/ydotool.py

+17-31
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
1-
import os
2-
import subprocess as sp
31
from subprocess import run
42

5-
from ..abstractionhelper import get_event_code, is_installed
6-
from ..action import __get_codepoints as get_codepoints
3+
from ..abstractionhelper import is_installed
4+
from ..action import __get_codepoints as get_codepoints, __get_event_code as get_event_code
75
from .typer import Typer
86

97
class YdotoolTyper(Typer):
10-
def __init__(self):
11-
super().__init__()
12-
13-
self.socket = "/run/user/1000/.ydotool_socket"
14-
if "YDOTOOL_SOCKET" in os.environ:
15-
self.socket = os.environ["YDOTOOL_SOCKET"]
16-
178
@staticmethod
189
def name():
1910
return "ydotool"
2011

2112
@staticmethod
2213
def supported():
23-
try:
24-
return is_installed("ydotool")
25-
except sp.CalledProcessError:
26-
return False
14+
return is_installed("ydotool")
2715

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

3927
# Get keypresses for Ctrl, Shift, U, and the unicode code point
40-
Ctrl = get_event_code("LeftCtrl")
41-
Shift = get_event_code("LeftShift")
42-
U = get_event_code("U")
43-
Ctrl_release = get_event_code("LeftCtrl", False)
44-
Shift_release = get_event_code("LeftShift", False)
45-
U_release = get_event_code("U", False)
28+
Ctrl = get_event_code("LeftCtrl") + ":1"
29+
Shift = get_event_code("LeftShift") + ":1"
30+
U_press = get_event_code("U") + ":1"
31+
Ctrl_release = get_event_code("LeftCtrl") + ":0"
32+
Shift_release = get_event_code("LeftShift") + ":0"
33+
U_release = get_event_code("U") + ":0"
4634
points = []
4735

4836
for point in unicode_code_point:
49-
points.append(get_event_code(point))
50-
points.append(get_event_code(point, False))
37+
points.append(get_event_code(point) + ":1")
38+
points.append(get_event_code(point) + ":0")
5139

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

5643
def insert_from_clipboard(self, active_window: str) -> None:
57-
Shift = get_event_code("LeftShift")
58-
Shift_release = get_event_code("LeftShift", False)
59-
Insert = get_event_code("Insert")
60-
Insert_release = get_event_code("Insert", False)
44+
Shift = get_event_code("LeftShift") + ":1"
45+
Shift_release = get_event_code("LeftShift") + ":0"
46+
Insert = get_event_code("Insert") + ":1"
47+
Insert_release = get_event_code("Insert") + ":0"
6148

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

0 commit comments

Comments
 (0)