-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathibus_voice_engine.py
More file actions
317 lines (255 loc) · 9.13 KB
/
Copy pathibus_voice_engine.py
File metadata and controls
317 lines (255 loc) · 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python3
"""IBus input method engine for voice typing.
Receives text commands over a Unix socket from the voice typing STT pipeline
and uses IBus commit_text / update_preedit_text to atomically insert text
into any focused application.
This eliminates the need for uinput/ydotool key injection and works uniformly
across terminals (Ghostty, kitty) and GUI apps (Firefox, editors).
Architecture:
enhanced-voice-typing.py --[socket]--> ibus_voice_engine.py
--[IBus API]--> focused app
Socket protocol (newline-terminated commands):
preedit:TEXT - Show TEXT as underlined preedit (streaming partials)
commit:TEXT - Clear preedit and atomically commit TEXT
delete:N - Delete N characters before cursor
replace:N:TEXT - Delete N chars before cursor, then commit TEXT
"""
import os
import sys
import socket
import threading
import atexit
import gi
gi.require_version("IBus", "1.0")
from gi.repository import IBus, GLib # noqa: E402
RUNTIME_DIR = os.environ.get("XDG_RUNTIME_DIR", "/tmp")
IBUS_SOCKET_PATH = os.path.join(RUNTIME_DIR, f"voice-typing-ibus-{os.getuid()}.sock")
IBUS_CAPS_PATH = os.path.join(RUNTIME_DIR, f"voice-typing-ibus-caps-{os.getuid()}")
# Global reference to the active engine instance (set by factory)
_active_engine = None
_factory = None
class VoiceTypingEngine(IBus.Engine):
"""IBus engine that receives voice typing text over a socket."""
__gtype_name__ = "VoiceTypingEngine"
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._enabled = False
self._surrounding = False
def do_process_key_event(self, keyval, keycode, state):
"""Pass through all keyboard events.
We only inject text here; we do not intercept keys.
"""
return False
def do_enable(self):
self._enabled = True
print("IBus VoiceTyping engine enabled")
def do_disable(self):
self._enabled = False
self._hide_preedit()
print("IBus VoiceTyping engine disabled")
def do_set_capabilities(self, caps):
"""Track client capabilities — especially surrounding text support."""
self._surrounding = bool(caps & IBus.Capabilite.SURROUNDING_TEXT)
self._write_caps()
print(f"Client caps: surrounding_text={self._surrounding} (0x{caps:x})")
def do_focus_in(self):
self._write_caps()
def do_focus_out(self):
self._hide_preedit()
def _write_caps(self):
"""Write current capabilities to file for voice typing script."""
try:
with open(IBUS_CAPS_PATH, "w") as f:
f.write("surrounding\n" if self._surrounding else "basic\n")
except OSError:
pass
def do_reset(self):
self._hide_preedit()
def commit(self, text):
"""Atomically commit text to the focused application."""
ibus_text = IBus.Text.new_from_string(text)
self.commit_text(ibus_text)
def preedit(self, text):
"""Show preedit preview text. Underlined in terminals, plain in browsers."""
if not text:
self._hide_preedit()
return
ibus_text = IBus.Text.new_from_string(text)
if not self._surrounding:
# Terminal: underline as visual indicator that refinement is pending
ibus_text.append_attribute(
IBus.AttrType.UNDERLINE, IBus.AttrUnderline.SINGLE, 0, len(text)
)
self.update_preedit_text_with_mode(
ibus_text, len(text), True, IBus.PreeditFocusMode.CLEAR
)
def delete_chars(self, count):
"""Delete N characters before cursor."""
if count > 0:
self.delete_surrounding_text(-count, count)
def replace_chars(self, count, text):
"""Delete N chars then commit new text."""
if count > 0:
self.delete_surrounding_text(-count, count)
if text:
self.commit(text)
def _hide_preedit(self):
self.hide_preedit_text()
class VoiceTypingEngineFactory(IBus.Factory):
"""Factory that creates VoiceTypingEngine instances on demand from IBus."""
__gtype_name__ = "VoiceTypingEngineFactory"
_engine_count = 0
def __init__(self, bus):
self._bus = bus
super().__init__(object_path=IBus.PATH_FACTORY, connection=bus.get_connection())
def do_create_engine(self, engine_name):
global _active_engine
VoiceTypingEngineFactory._engine_count += 1
obj_path = (
f"/org/freedesktop/IBus/Engine/{VoiceTypingEngineFactory._engine_count}"
)
engine = VoiceTypingEngine(
engine_name=engine_name,
object_path=obj_path,
connection=self._bus.get_connection(),
)
_active_engine = engine
print(f"Created engine: {engine_name} at {obj_path}")
return engine
def _handle_socket_command(line):
"""Parse and dispatch a socket command to the IBus engine via GLib main thread."""
global _active_engine
if _active_engine is None:
return
if ":" not in line:
return
cmd, _, payload = line.partition(":")
cmd = cmd.strip()
if cmd == "preedit":
GLib.idle_add(_active_engine.preedit, payload)
elif cmd == "commit":
GLib.idle_add(_active_engine.preedit, "")
GLib.idle_add(_active_engine.commit, payload)
elif cmd == "delete":
try:
count = int(payload.strip())
GLib.idle_add(_active_engine.delete_chars, count)
except ValueError:
pass
elif cmd == "replace":
# Format: replace:N:new text
parts = payload.split(":", 1)
if len(parts) == 2:
try:
count = int(parts[0])
text = parts[1]
GLib.idle_add(_active_engine.replace_chars, count, text)
except ValueError:
pass
def _handle_client(conn):
"""Handle a persistent client connection, reading newline-terminated commands."""
buf = b""
try:
while True:
data = conn.recv(4096)
if not data:
break
buf += data
while b"\n" in buf:
line, buf = buf.split(b"\n", 1)
# Preserve payload whitespace. Streaming suffix commits depend on
# leading/trailing spaces remaining intact across the socket hop.
line_str = line.decode("utf-8", errors="replace")
if line_str:
_handle_socket_command(line_str)
except Exception:
pass
finally:
try:
conn.close()
except Exception:
pass
def _socket_listener():
"""Accept connections from voice typing STT pipeline."""
if os.path.exists(IBUS_SOCKET_PATH):
os.remove(IBUS_SOCKET_PATH)
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
old_umask = os.umask(0o077)
try:
server.bind(IBUS_SOCKET_PATH)
finally:
os.umask(old_umask)
os.chmod(IBUS_SOCKET_PATH, 0o600)
server.listen(2)
print(f"IBus socket listening: {IBUS_SOCKET_PATH}")
while True:
try:
conn, _ = server.accept()
client_thread = threading.Thread(
target=_handle_client, args=(conn,), daemon=True
)
client_thread.start()
except Exception as e:
print(f"Socket accept error: {e}")
continue
def _cleanup():
for path in (IBUS_SOCKET_PATH, IBUS_CAPS_PATH):
if os.path.exists(path):
try:
os.remove(path)
except Exception:
pass
def main():
global _factory
IBus.init()
bus = IBus.Bus()
if not bus.is_connected():
print("Cannot connect to IBus daemon")
sys.exit(1)
component = IBus.Component.new(
"org.freedesktop.IBus.VoiceTyping",
"Voice Typing Input Method",
"1.0",
"MIT",
"Voice Typing",
"",
"",
"voice-typing",
)
engine_desc = IBus.EngineDesc.new(
"voice-typing",
"Voice Typing",
"Voice-to-text input via streaming STT",
"en",
"MIT",
"Voice Typing",
"audio-input-microphone",
"us",
)
component.add_engine(engine_desc)
_factory = VoiceTypingEngineFactory(bus)
rc = bus.register_component(component)
print(f"register_component: {rc}")
rn = bus.request_name("org.freedesktop.IBus.VoiceTyping", 0)
print(f"request_name: {rn}")
# Try to verify the engine is listed
engines = bus.list_engines()
found = [e.get_name() for e in engines if "voice" in e.get_name().lower()]
print(f"Engines with 'voice': {found}")
# Start socket listener in background thread
socket_thread = threading.Thread(
target=_socket_listener, daemon=True, name="IBusSocketListener"
)
socket_thread.start()
print("IBus Voice Typing engine running")
print(f"Socket: {IBUS_SOCKET_PATH}")
atexit.register(_cleanup)
loop = GLib.MainLoop()
try:
loop.run()
except KeyboardInterrupt:
print("\nShutting down IBus Voice Typing engine")
finally:
_cleanup()
if __name__ == "__main__":
main()