Important information: Google announced that, from September 2026, Android devices will require ALL apps to be signed by Google, effectively leading to an iOS situation. Value your right to a computer that does what you want; do not tolerate this monopolistic practice! Contact me if you don't understand why it is bad. Click to learn more.

 main.py

View raw Download
text/plain • 17.53 kiB
Python script, ASCII text executable
        
            
1
"""
2
Kineboard: an experimental touchscreen keyboard using swipes to allow
3
having fewer touch targets.
4
Copyright 2025, roundabout-host.com <vlad@roundabout-host.com>
5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public Licence as published by
7
the Free Software Foundation, either version 3 of the Licence, or
8
(at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public Licence for more details.
13
You should have received a copy of the GNU General Public Licence
14
along with this program. If not, see <https://www.gnu.org/licenses/>.
15
"""
16
17
import itertools
18
import sys
19
import os
20
import time
21
22
import ruamel.yaml as yaml
23
from pathlib import Path
24
25
os.environ["GI_TYPELIB_PATH"] = "/usr/local/lib/x86_64-linux-gnu/girepository-1.0"
26
from ctypes import CDLL
27
CDLL("libgtk4-layer-shell.so")
28
29
import gi
30
gi.require_version("Gtk", "4.0")
31
gi.require_version("Gtk4LayerShell", "1.0")
32
from gi.repository import Gtk, Gdk, GObject, Gtk4LayerShell, GLib, Gio
33
34
from pywayland.client import Display, EventQueue
35
from pywayland.protocol.wayland import WlRegistry, WlSeat
36
from pywayland.protocol.input_method_unstable_v1 import (
37
ZwpInputMethodContextV1,
38
ZwpInputMethodV1
39
)
40
from pywayland.protocol.input_method_unstable_v2 import (
41
ZwpInputMethodV2,
42
ZwpInputMethodManagerV2
43
)
44
from pywayland.protocol.virtual_keyboard_unstable_v1 import ZwpVirtualKeyboardManagerV1
45
46
47
import ctypes
48
from cffi import FFI
49
ffi = FFI()
50
ffi.cdef("""
51
void * gdk_wayland_display_get_wl_display (void * display);
52
void * gdk_wayland_surface_get_wl_surface (void * surface);
53
void * gdk_wayland_monitor_get_wl_output (void * monitor);
54
""")
55
gtk = ffi.dlopen("libgtk-4.so.1")
56
57
58
module_directory = Path(__file__).resolve().parent
59
60
61
custom_css = """
62
.kineboard-selected-character {
63
text-decoration: underline;
64
}
65
"""
66
css_provider = Gtk.CssProvider()
67
css_provider.load_from_data(custom_css)
68
Gtk.StyleContext.add_provider_for_display(
69
Gdk.Display.get_default(),
70
css_provider,
71
100
72
)
73
74
75
def get_layout_directories():
76
data_home = Path(os.getenv("XDG_DATA_HOME", Path.home() / ".local" / "share"))
77
data_dirs = [Path(d) for d in os.getenv("XDG_DATA_DIRS", "/usr/local/share:/usr/share").split(":")]
78
all_paths = [data_home / "kineboard" / "layouts"] + [d / "kineboard" / "layouts" for d in data_dirs]
79
return [d for d in all_paths if d.is_dir()]
80
81
82
class GestureZone(GObject.GEnum):
83
CENTRAL = 0
84
TOP_LEFT = 1
85
TOP_RIGHT = 2
86
BOTTOM_RIGHT = 3
87
BOTTOM_LEFT = 4
88
89
90
class SwipeButton(Gtk.Frame):
91
@GObject.Signal
92
def top_left(self):
93
pass
94
95
@GObject.Signal
96
def top_right(self):
97
pass
98
99
@GObject.Signal
100
def bottom_left(self):
101
pass
102
103
@GObject.Signal
104
def bottom_right(self):
105
pass
106
107
@GObject.Signal
108
def central(self):
109
pass
110
111
@GObject.Signal(arg_types=(int,))
112
def drag_changed(self, zone):
113
pass
114
115
def __init__(self, **kwargs):
116
Gtk.Frame.__init__(self, **kwargs)
117
self.gesture_drag = Gtk.GestureDrag()
118
self.gesture_drag.connect("drag-end", self.finish_drag)
119
self.gesture_drag.connect("drag-update", self.move_drag)
120
self.gesture_drag.connect("drag-begin", self.begin_drag)
121
self.add_controller(self.gesture_drag)
122
123
def finish_drag(self, gesture, offset_x, offset_y):
124
if abs(offset_x) / self.get_width() + abs(offset_y) / self.get_height() > 1 / 2:
125
if offset_x > 0 and offset_y > 0:
126
self.emit("bottom_right")
127
elif offset_x <= 0 and offset_y > 0:
128
self.emit("bottom_left")
129
elif offset_x > 0 and offset_y <= 0:
130
self.emit("top_right")
131
elif offset_x <= 0 and offset_y <= 0:
132
self.emit("top_left")
133
else:
134
self.emit("central")
135
136
def move_drag(self, gesture, offset_x, offset_y):
137
if abs(offset_x) / self.get_width() + abs(offset_y) / self.get_height() > 1 / 2:
138
if offset_x > 0 and offset_y > 0:
139
self.emit("drag_changed", GestureZone.BOTTOM_RIGHT)
140
elif offset_x <= 0 and offset_y > 0:
141
self.emit("drag_changed", GestureZone.BOTTOM_LEFT)
142
elif offset_x > 0 and offset_y <= 0:
143
self.emit("drag_changed", GestureZone.TOP_RIGHT)
144
elif offset_x <= 0 and offset_y <= 0:
145
self.emit("drag_changed", GestureZone.TOP_LEFT)
146
else:
147
self.emit("drag_changed", GestureZone.CENTRAL)
148
149
def begin_drag(self, gesture, start_x, start_y):
150
self.emit("drag_changed", GestureZone.CENTRAL)
151
152
153
class SwipeTyper(SwipeButton):
154
@GObject.Signal(arg_types=(str,))
155
def typed(self, character):
156
# Since the drag is finished, remove the underlines
157
for label in self.label_map.values():
158
label.remove_css_class("kineboard-selected-character")
159
160
def underline(self, zone):
161
for label in self.label_map.values():
162
label.remove_css_class("kineboard-selected-character")
163
self.label_map[zone].add_css_class("kineboard-selected-character")
164
165
def __init__(self, tl, tr, bl, br, c, **kwargs):
166
SwipeButton.__init__(self, **kwargs)
167
self.tl = tl
168
self.tr = tr
169
self.bl = bl
170
self.br = br
171
self.c = c
172
self.grid = Gtk.Grid(hexpand=True, vexpand=True, row_homogeneous=True, column_homogeneous=True, margin_top=4, margin_bottom=4, margin_start=4, margin_end=4)
173
self.set_child(self.grid)
174
self.tl_label = Gtk.Label(label=tl)
175
self.grid.attach(self.tl_label, 0, 0, 1, 1)
176
self.tr_label = Gtk.Label(label=tr)
177
self.grid.attach(self.tr_label, 2, 0, 1, 1)
178
self.bl_label = Gtk.Label(label=bl)
179
self.grid.attach(self.bl_label, 0, 2, 1, 1)
180
self.br_label = Gtk.Label(label=br)
181
self.grid.attach(self.br_label, 2, 2, 1, 1)
182
self.c_label = Gtk.Label(label=c)
183
self.grid.attach(self.c_label, 1, 1, 1, 1)
184
self.label_map = {
185
GestureZone.CENTRAL: self.c_label,
186
GestureZone.TOP_LEFT: self.tl_label,
187
GestureZone.TOP_RIGHT: self.tr_label,
188
GestureZone.BOTTOM_RIGHT: self.br_label,
189
GestureZone.BOTTOM_LEFT: self.bl_label
190
}
191
self.connect("top_left", lambda p: self.emit("typed", tl))
192
self.connect("top_right", lambda p: self.emit("typed", tr))
193
self.connect("bottom_left", lambda p: self.emit("typed", bl))
194
self.connect("bottom_right", lambda p: self.emit("typed", br))
195
self.connect("central", lambda p: self.emit("typed", c))
196
self.connect("drag_changed", lambda p, zone: self.underline(zone))
197
198
199
class Plane(Gtk.Grid):
200
def __init__(self, app, data, **kwargs):
201
Gtk.Grid.__init__(self, **kwargs)
202
self.app = app
203
self.all_characters = ""
204
for j, column in enumerate(data):
205
for k, key in enumerate(column):
206
widget = SwipeTyper(key["top_left"], key["top_right"], key["bottom_left"],
207
key["bottom_right"], key["central"])
208
widget.connect("typed", self.app.typed)
209
self.attach(widget, k, j, 1, 1)
210
self.all_characters += key["top_left"] + key["top_right"] + key["bottom_left"] + key["bottom_right"] + key["central"]
211
212
213
214
def make_xkb_keymap_from_unicodes(unicodes):
215
symbols = [
216
"key <SP00> { [ BackSpace ] };",
217
"key <SP01> { [ Return ] };",
218
]
219
keycodes = [
220
"<SP00> = 4094;",
221
"<SP01> = 4095;",
222
]
223
for i, char in enumerate(unicodes):
224
keycode = 0x1000 + i
225
keysym = f"U{ord(char):04X}"
226
symbols.append(f"key <K{i:03X}> {{ [ {keysym} ] }};")
227
keycodes.append(f"<K{i:03X}> = {keycode};")
228
229
return ("xkb_keymap { xkb_keycodes \"kineboard\" { minimum = 8; maximum = 65535; "
230
+ " ".join(keycodes) + " }; xkb_symbols \"kineboard\" { " + " ".join(symbols)
231
+ " }; xkb_types \"kineboard\" { type \"ONE_LEVEL\" { modifiers = none; level_name[Level1] = \"Any\"; }; }; xkb_compatibility \"kineboard\" {}; };")
232
233
234
from gi.repository import Gio, GLib
235
236
237
def register_client(client_id: str):
238
bus = Gio.bus_get_sync(Gio.BusType.SESSION, None)
239
240
proxy = Gio.DBusProxy.new_sync(
241
bus,
242
Gio.DBusProxyFlags.DO_NOT_LOAD_PROPERTIES |
243
Gio.DBusProxyFlags.DO_NOT_AUTO_START_AT_CONSTRUCTION,
244
None,
245
"org.gnome.SessionManager",
246
"/org/gnome/SessionManager",
247
"org.gnome.SessionManager",
248
None
249
)
250
251
startup_id = GLib.getenv("DESKTOP_AUTOSTART_ID") or ""
252
253
def on_registered(proxy, result, loop):
254
try:
255
proxy.call_finish(result)
256
except Exception as e:
257
print("cannot register client:", e)
258
loop.quit()
259
260
loop = GLib.MainLoop()
261
262
proxy.call(
263
"RegisterClient",
264
GLib.Variant("(ss)", (client_id, startup_id)),
265
Gio.DBusCallFlags.NONE,
266
-1,
267
None,
268
lambda p, r, l: on_registered(p, r, loop),
269
loop
270
)
271
272
loop.run()
273
return proxy
274
275
276
class Kineboard(Gtk.Application):
277
def __init__(self):
278
Gtk.Application.__init__(
279
self,
280
application_id="sm.puri.OSK0",
281
flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE
282
)
283
self.revealer = None
284
self.switcher = None
285
self.character_index = {}
286
self.keymap_fd = None
287
self.vk = None
288
self.keymap_text = ""
289
self.all_characters = ""
290
self.context = None
291
self.input_method_manager = None
292
self.input_method = None
293
self.registry = None
294
self.box = None
295
self.window = None
296
self.stack = None
297
self.display = None
298
self.wl_display = None
299
self.seat = None
300
self.vkm = None
301
self.serial = 0
302
self.add_main_option(
303
"invoke",
304
ord("i"),
305
GLib.OptionFlags.NONE,
306
GLib.OptionArg.NONE,
307
"Manually bring up the keyboard",
308
None
309
)
310
self.add_main_option(
311
"hide",
312
ord("h"),
313
GLib.OptionFlags.NONE,
314
GLib.OptionArg.NONE,
315
"Hide the keyboard",
316
None
317
)
318
319
def toggle_sidebar(self, button):
320
self.revealer.set_reveal_child(not self.revealer.get_reveal_child())
321
322
def do_startup(self):
323
Gtk.Application.do_startup(self)
324
register_client("sm.puri.OSK0")
325
self.display = Gdk.Display.get_default()
326
self.window = Gtk.Window(application=self, title="Demo", focus_on_click=False, focusable=False, can_focus=False)
327
Gtk4LayerShell.init_for_window(self.window)
328
Gtk4LayerShell.set_keyboard_mode(self.window, Gtk4LayerShell.KeyboardMode.NONE)
329
Gtk4LayerShell.set_anchor(self.window, Gtk4LayerShell.Edge.BOTTOM, True)
330
Gtk4LayerShell.set_anchor(self.window, Gtk4LayerShell.Edge.LEFT, True)
331
Gtk4LayerShell.set_anchor(self.window, Gtk4LayerShell.Edge.RIGHT, True)
332
Gtk4LayerShell.auto_exclusive_zone_enable(self.window)
333
Gtk4LayerShell.set_namespace(self.window, "osk")
334
self.box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
335
self.window.set_child(self.box)
336
self.stack = Gtk.Stack()
337
self.box.append(self.stack)
338
layouts = itertools.chain.from_iterable((f for f in path.iterdir() if f.is_file()) for path in get_layout_directories())
339
self.all_characters = " "
340
self.switcher = Gtk.StackSidebar(stack=self.stack)
341
self.revealer = Gtk.Revealer(child=self.switcher, transition_type=Gtk.RevealerTransitionType.SLIDE_RIGHT)
342
self.box.prepend(self.revealer)
343
for layout in layouts:
344
stack = Gtk.Stack()
345
346
yaml_loader = yaml.YAML(typ="rt")
347
data = yaml_loader.load(layout)
348
349
controls = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=8, margin_top=0,
350
margin_bottom=8, margin_start=8, margin_end=8)
351
352
layout_button = Gtk.Button(child=Gtk.Image.new_from_icon_name("edit-symbolic"))
353
layout_button.set_size_request(48, -1)
354
layout_button.update_property([Gtk.AccessibleProperty.LABEL], ["Switch layout"])
355
layout_button.connect("clicked", self.toggle_sidebar)
356
controls.append(layout_button)
357
358
if len(data["keys"]) >= 2:
359
shift_button = Gtk.ToggleButton(
360
child=Gtk.Image.new_from_icon_name("go-up-symbolic")
361
)
362
shift_button.set_size_request(48, -1)
363
shift_button.update_property([Gtk.AccessibleProperty.LABEL], ["Shift"])
364
controls.append(shift_button)
365
366
def shift(button: Gtk.ToggleButton):
367
stack.set_visible_child_name(f"plane-{int(button.get_active())}")
368
369
shift_button.connect("toggled", shift)
370
371
space_bar = Gtk.Button(hexpand=True)
372
space_bar.update_property([Gtk.AccessibleProperty.LABEL], ["Space"])
373
space_bar.connect("clicked", lambda x: self.typed(x, " "))
374
controls.append(space_bar)
375
376
backspace_button = Gtk.Button(child=Gtk.Image.new_from_icon_name("go-previous-symbolic"))
377
backspace_button.set_size_request(48, -1)
378
backspace_button.update_property([Gtk.AccessibleProperty.LABEL], ["Backspace"])
379
backspace_button.connect("clicked", lambda x: self.send_keysym(0xFF6))
380
controls.append(backspace_button)
381
382
enter_button = Gtk.Button(child=Gtk.Image.new_from_icon_name("keyboard-enter-symbolic"))
383
enter_button.set_size_request(48, -1)
384
enter_button.update_property([Gtk.AccessibleProperty.LABEL], ["Enter"])
385
enter_button.add_css_class("suggested-action")
386
enter_button.connect("clicked", lambda x: self.send_keysym(0xFF7))
387
controls.append(enter_button)
388
389
for i, layer in enumerate(data["keys"]):
390
grid = Plane(self, layer, row_spacing=8, column_spacing=8, row_homogeneous=True,
391
column_homogeneous=True, margin_top=8, margin_bottom=8,
392
margin_end=8, margin_start=8)
393
self.all_characters += grid.all_characters
394
stack.add_named(grid, f"plane-{i}")
395
396
outer_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
397
outer_box.append(stack)
398
outer_box.append(controls)
399
self.stack.add_titled(outer_box, data["symbol"], data["symbol"])
400
401
ctypes.pythonapi.PyCapsule_GetPointer.restype = ctypes.c_void_p
402
ctypes.pythonapi.PyCapsule_GetPointer.argtypes = (ctypes.py_object,)
403
404
self.wl_display = Display()
405
wl_display_ptr = gtk.gdk_wayland_display_get_wl_display(
406
ffi.cast("void *", ctypes.pythonapi.PyCapsule_GetPointer(self.display.__gpointer__, None)))
407
self.wl_display._ptr = wl_display_ptr
408
self.registry = self.wl_display.get_registry()
409
self.registry.dispatcher["global"] = self.on_global
410
self.registry.dispatcher["global_remove"] = self.on_global_remove
411
self.wl_display.roundtrip()
412
413
if self.input_method_manager:
414
self.input_method = self.input_method_manager.get_input_method(self.seat)
415
self.input_method.dispatcher["activate"] = self.activate_input
416
self.input_method.dispatcher["deactivate"] = self.deactivate_input
417
else:
418
dialog = Gtk.AlertDialog(message="Input method protocol not supported.")
419
dialog.show()
420
421
self.keymap_text = make_xkb_keymap_from_unicodes(self.all_characters)
422
self.character_index = {}
423
for i, character in enumerate(self.all_characters):
424
self.character_index[character] = i
425
self.vk = self.vkm.create_virtual_keyboard(self.seat)
426
427
self.keymap_fd = os.memfd_create("keymap")
428
os.write(self.keymap_fd, self.keymap_text.encode("utf-8"))
429
os.lseek(self.keymap_fd, 0, os.SEEK_SET)
430
self.vk.keymap(1, self.keymap_fd, len(self.keymap_text.encode("utf-8")))
431
432
def activate_input(self, im):
433
self.window.present()
434
435
def deactivate_input(self, im):
436
self.window.set_visible(False)
437
self.serial = 0
438
439
def on_global(self, registry, name, interface, version):
440
if interface == "zwp_input_method_manager_v2":
441
self.input_method_manager = registry.bind(name, ZwpInputMethodManagerV2, version)
442
elif interface == "wl_seat":
443
self.seat = registry.bind(name, WlSeat, version)
444
elif interface == "zwp_virtual_keyboard_manager_v1":
445
self.vkm = registry.bind(name, ZwpVirtualKeyboardManagerV1, version)
446
447
def on_global_remove(self, registry, name):
448
pass
449
450
def send_keysym(self, key):
451
clock = int(time.monotonic() * 1000)
452
if self.vk:
453
self.vk.key(clock, key, 1)
454
self.vk.key(clock, key, 0)
455
456
def typed(self, button, characters):
457
for char in characters:
458
self.send_keysym(0xFF8 + self.character_index[char])
459
460
def do_activate(self):
461
Gtk.Application.do_activate(self)
462
# self.window.present()
463
464
def do_command_line(self, command_line: Gio.ApplicationCommandLine):
465
options = command_line.get_options_dict()
466
args = command_line.get_arguments()[1:]
467
if options.contains("invoke"):
468
self.activate_input(None, None)
469
elif options.contains("hide"):
470
self.deactivate_input(None, None)
471
return 0
472
473
474
if __name__ == "__main__":
475
kineboard = Kineboard()
476
kineboard.run(sys.argv)
477