mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 20:45:43 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c3006b327b |
No files matched your search
@@ -1005,7 +1005,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
|
||||
}
|
||||
root.addView(controls)
|
||||
}
|
||||
keyboard = KeyboardHost(this, sendKeyEvent = ::dispatchImeKeyEvent, sendText = WaylandCompositor::textInputCommit).also { host ->
|
||||
keyboard = KeyboardHost(this, sendKeyEvent = ::dispatchImeKeyEvent, sendText = ::sendImeText).also { host ->
|
||||
host.onImeChanged = ::onPrimaryImeChanged
|
||||
root.addView(host)
|
||||
}
|
||||
@@ -3824,6 +3824,18 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
|
||||
|
||||
internal fun dispatchImeKeyEvent(event: KeyEvent): Boolean = dispatchKeyEvent(event, orderedImeKey = true)
|
||||
|
||||
internal fun sendImeText(text: String) {
|
||||
val keys = if (SessionState.mode == SessionService.MODE_DESKTOP) com.droiddeck.launcher.input.GuestKeys.softControlKeys(text) else null
|
||||
if (keys == null) {
|
||||
WaylandCompositor.textInputCommit(text)
|
||||
return
|
||||
}
|
||||
for (evdev in keys) {
|
||||
orderedImeKeySender(evdev, true)
|
||||
orderedImeKeySender(evdev, false)
|
||||
}
|
||||
}
|
||||
|
||||
override fun dispatchKeyEvent(event: KeyEvent): Boolean = dispatchKeyEvent(event, orderedImeKey = false)
|
||||
|
||||
private fun dispatchKeyEvent(event: KeyEvent, orderedImeKey: Boolean): Boolean {
|
||||
|
||||
@@ -36,6 +36,23 @@ object GuestKeys {
|
||||
fun isKeyboardEscape(keyCode: Int, scanCode: Int): Boolean =
|
||||
keyCode == KeyEvent.KEYCODE_BACK && scanCode == KEY_ESC
|
||||
|
||||
fun softControlKeys(text: String): IntArray? {
|
||||
if (text.isEmpty()) return null
|
||||
return IntArray(text.length) { i ->
|
||||
when (text[i]) {
|
||||
'\r', '\n' -> KEY_ENTER
|
||||
'\b' -> KEY_BACKSPACE
|
||||
'\u007f' -> KEY_DELETE
|
||||
'\t' -> KEY_TAB
|
||||
else -> return null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private const val KEY_ESC = 1
|
||||
private const val KEY_BACKSPACE = 14
|
||||
private const val KEY_TAB = 15
|
||||
private const val KEY_ENTER = 28
|
||||
private const val KEY_DELETE = 111
|
||||
private const val KEY_LEFTSHIFT = 42
|
||||
}
|
||||
@@ -219,14 +219,9 @@ object BottomDesktop {
|
||||
|
||||
fun sendText(text: String) {
|
||||
val target = service ?: return
|
||||
if (text.isNotEmpty() && text.all { it == '\r' || it == '\n' || it == '\b' || it == '\u007f' || it == '\t' }) {
|
||||
for (c in text) {
|
||||
val evdev = when (c) {
|
||||
'\r', '\n' -> 28
|
||||
'\b' -> 14
|
||||
'\u007f' -> 111
|
||||
else -> 15
|
||||
}
|
||||
val keys = com.droiddeck.launcher.input.GuestKeys.softControlKeys(text)
|
||||
if (keys != null) {
|
||||
for (evdev in keys) {
|
||||
sendKey(evdev, true)
|
||||
sendKey(evdev, false)
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@ import com.droiddeck.launcher.input.ControllerPrefs
|
||||
import com.droiddeck.launcher.input.DeckPanel
|
||||
import com.droiddeck.launcher.input.KeyboardHost
|
||||
import com.droiddeck.launcher.input.PadBridge
|
||||
import com.droiddeck.launcher.wayland.WaylandCompositor
|
||||
import com.droiddeck.launcher.input.SecondScreenMode
|
||||
import com.droiddeck.launcher.SessionActivity
|
||||
import com.droiddeck.launcher.session.BottomDesktop
|
||||
@@ -502,7 +501,7 @@ class SecondScreenPresentation(
|
||||
handled
|
||||
},
|
||||
sendText = { text ->
|
||||
WaylandCompositor.textInputCommit(text)
|
||||
sessionActivity.sendImeText(text)
|
||||
if (fieldKeyboard && text == "\r") root.post { closeFieldKeyboard() }
|
||||
},
|
||||
)
|
||||
|
||||
@@ -40,6 +40,14 @@ class GuestKeysTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun softControlTextMapsToEditingKeys() {
|
||||
assertArrayEquals(intArrayOf(28, 28, 14, 111, 15), GuestKeys.softControlKeys("\r\n\b\u007f\t"))
|
||||
}
|
||||
|
||||
@Test fun textWithAnyPrintableCharacterIsNotSoftControl() {
|
||||
for (text in listOf("", "a", "line\n", "\b中", " ")) assertNull(GuestKeys.softControlKeys(text))
|
||||
}
|
||||
|
||||
@Test fun normalizedEscapePreservesRepeatAndCancellation() {
|
||||
val event = KeyEvent(10, 20, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK,
|
||||
3, 0, 43, 1, KeyEvent.FLAG_CANCELED, InputDevice.SOURCE_KEYBOARD)
|
||||
|
||||
@@ -12,6 +12,7 @@ import android.view.inputmethod.InputConnection
|
||||
import com.droiddeck.launcher.SessionActivity
|
||||
import com.droiddeck.launcher.input.KeyboardHost
|
||||
import com.droiddeck.launcher.input.SecondScreenMode
|
||||
import com.droiddeck.launcher.session.SessionService
|
||||
import com.droiddeck.launcher.session.SessionState
|
||||
import com.droiddeck.launcher.wayland.WaylandCompositor
|
||||
import org.junit.After
|
||||
@@ -45,6 +46,7 @@ class SecondScreenImeTest {
|
||||
previousDeckPad = SessionState.deckPad
|
||||
SessionState.deckPad = false
|
||||
CompositorShadow.commits.clear()
|
||||
CompositorShadow.keys.clear()
|
||||
}
|
||||
|
||||
@After fun tearDown() {
|
||||
@@ -119,6 +121,37 @@ class SecondScreenImeTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun desktopSendsSoftEditingKeysAsOrderedKeysAndTextAsCommits() {
|
||||
SessionState.mode = SessionService.MODE_DESKTOP
|
||||
val fixture = keyboard(forField = false) {}
|
||||
try {
|
||||
assertTrue(fixture.connection.commitText("中文", 1))
|
||||
assertTrue(fixture.connection.deleteSurroundingText(1, 0))
|
||||
assertTrue(fixture.connection.performEditorAction(EditorInfo.IME_ACTION_NONE))
|
||||
idle()
|
||||
|
||||
assertEquals(listOf("中文"), CompositorShadow.commits)
|
||||
assertEquals(listOf(14 to 1, 14 to 0, 28 to 1, 28 to 0), CompositorShadow.keys)
|
||||
} finally {
|
||||
fixture.close()
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun nonDesktopSessionsKeepSoftEditingKeysOnTheTextChannel() {
|
||||
SessionState.mode = SessionService.MODE_STEAM
|
||||
val fixture = keyboard(forField = false) {}
|
||||
try {
|
||||
assertTrue(fixture.connection.deleteSurroundingText(1, 0))
|
||||
assertTrue(fixture.connection.performEditorAction(EditorInfo.IME_ACTION_NONE))
|
||||
idle()
|
||||
|
||||
assertEquals(listOf("\b", "\r"), CompositorShadow.commits)
|
||||
assertTrue(CompositorShadow.keys.isEmpty())
|
||||
} finally {
|
||||
fixture.close()
|
||||
}
|
||||
}
|
||||
|
||||
private fun keyboard(forField: Boolean, onClosed: () -> Unit): Fixture {
|
||||
val activity = Robolectric.buildActivity(SessionActivity::class.java).get()
|
||||
val display = activity.getSystemService(DisplayManager::class.java).getDisplay(Display.DEFAULT_DISPLAY)
|
||||
@@ -166,12 +199,17 @@ class SecondScreenImeTest {
|
||||
class CompositorShadow {
|
||||
companion object {
|
||||
val commits = mutableListOf<String>()
|
||||
val keys = mutableListOf<Pair<Int, Int>>()
|
||||
|
||||
@JvmStatic @Implementation fun __staticInitializer__() {}
|
||||
|
||||
@JvmStatic @Implementation fun textInputCommit(text: String) {
|
||||
commits += text
|
||||
}
|
||||
|
||||
@JvmStatic @Implementation fun nativeSendKeyOrdered(evdev: Int, state: Int) {
|
||||
keys += evdev to state
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ if [ -n "${DD_ANDROID_CLIPBOARD:-}" ]; then
|
||||
children+=("$!")
|
||||
fi
|
||||
if [ -n "${DD_LAUNCH_DIR:-}" ] && [ -x /usr/local/bin/droiddeck-text-input ]; then
|
||||
/usr/local/bin/droiddeck-text-input "$DD_LAUNCH_DIR/text-input.sock" &
|
||||
/usr/local/bin/droiddeck-text-input "$DD_LAUNCH_DIR/text-input.sock" "$DD_LAUNCH_DIR/ime-text.sock" &
|
||||
children+=("$!")
|
||||
fi
|
||||
[ "${#children[@]}" -gt 0 ] || exit 0
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#!/usr/bin/python3
|
||||
import errno
|
||||
import json
|
||||
import os
|
||||
import select
|
||||
import socket
|
||||
import stat
|
||||
import struct
|
||||
import sys
|
||||
import time
|
||||
@@ -11,6 +13,12 @@ DISPLAY_ID = 1
|
||||
REGISTRY_ID = 2
|
||||
METHOD_ID = 3
|
||||
SETTLE = 0.15
|
||||
TEXT_SOCKET = "ime-text.sock"
|
||||
MAX_TEXT = 65536
|
||||
LENGTH_MASK = 0x7FFFFFFF
|
||||
COMMIT_CHUNK = 3072
|
||||
CLIENT_TIMEOUT = 0.5
|
||||
CONTROLS = frozenset("\r\n\b\t\x7f")
|
||||
|
||||
|
||||
def wl_string(value):
|
||||
@@ -33,10 +41,7 @@ class Connection:
|
||||
def send(self, obj, opcode, payload=b""):
|
||||
self.sock.sendall(struct.pack("<II", obj, ((8 + len(payload)) << 16) | opcode) + payload)
|
||||
|
||||
def messages(self, timeout):
|
||||
ready, _, _ = select.select([self.sock], [], [], timeout)
|
||||
if not ready:
|
||||
return []
|
||||
def messages(self):
|
||||
data, _, _, _ = self.sock.recvmsg(65536, socket.CMSG_SPACE(16 * 4))
|
||||
if not data:
|
||||
raise EOFError
|
||||
@@ -74,6 +79,130 @@ class Report:
|
||||
print(f"droiddeck-ime: text field {'focused' if field else 'left'}", file=sys.stderr, flush=True)
|
||||
|
||||
|
||||
def recv_exact(client, count):
|
||||
data = b""
|
||||
while len(data) < count:
|
||||
chunk = client.recv(count - len(data))
|
||||
if not chunk:
|
||||
return None
|
||||
data += chunk
|
||||
return data
|
||||
|
||||
|
||||
def read_text(client):
|
||||
header = recv_exact(client, 4)
|
||||
if header is None:
|
||||
return None
|
||||
length = struct.unpack(">I", header)[0] & LENGTH_MASK
|
||||
if length < 1 or length > MAX_TEXT:
|
||||
return None
|
||||
body = recv_exact(client, length)
|
||||
if body is None or b"\0" in body:
|
||||
return None
|
||||
try:
|
||||
return body.decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
return None
|
||||
|
||||
|
||||
def chunks(text):
|
||||
piece, size = [], 0
|
||||
for character in text:
|
||||
width = len(character.encode("utf-8"))
|
||||
if size + width > COMMIT_CHUNK:
|
||||
yield "".join(piece)
|
||||
piece, size = [], 0
|
||||
piece.append(character)
|
||||
size += width
|
||||
if piece:
|
||||
yield "".join(piece)
|
||||
|
||||
|
||||
def commit(conn, context, serial, text):
|
||||
if context is None or not text or CONTROLS.intersection(text):
|
||||
return False
|
||||
for piece in chunks(text):
|
||||
conn.send(context, 1, struct.pack("<I", serial) + wl_string(piece))
|
||||
return True
|
||||
|
||||
|
||||
def serve_text(listener, conn, context, serial):
|
||||
try:
|
||||
client, _ = listener.accept()
|
||||
except OSError:
|
||||
return
|
||||
with client:
|
||||
try:
|
||||
client.settimeout(CLIENT_TIMEOUT)
|
||||
text = read_text(client)
|
||||
except OSError:
|
||||
return
|
||||
committed = text is not None and commit(conn, context, serial, text)
|
||||
try:
|
||||
client.sendall(b"\x01" if committed else b"\x00")
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def stale(path):
|
||||
try:
|
||||
info = os.lstat(path)
|
||||
except FileNotFoundError:
|
||||
return True
|
||||
if not stat.S_ISSOCK(info.st_mode):
|
||||
return False
|
||||
probe = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
try:
|
||||
probe.settimeout(CLIENT_TIMEOUT)
|
||||
probe.connect(path)
|
||||
except OSError as error:
|
||||
if error.errno not in (errno.ECONNREFUSED, errno.ENOENT):
|
||||
return False
|
||||
else:
|
||||
return False
|
||||
finally:
|
||||
probe.close()
|
||||
try:
|
||||
os.unlink(path)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
return True
|
||||
|
||||
|
||||
def listen(launch):
|
||||
if not launch:
|
||||
return None, None
|
||||
path = os.path.join(launch, TEXT_SOCKET)
|
||||
try:
|
||||
if not stale(path):
|
||||
return None, None
|
||||
listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
previous = os.umask(0o077)
|
||||
try:
|
||||
listener.bind(path)
|
||||
finally:
|
||||
os.umask(previous)
|
||||
listener.listen(4)
|
||||
info = os.stat(path)
|
||||
except OSError as error:
|
||||
print(f"droiddeck-ime: text socket unavailable: {error}", file=sys.stderr, flush=True)
|
||||
return None, None
|
||||
return listener, (path, info.st_dev, info.st_ino)
|
||||
|
||||
|
||||
def unlisten(listener, owned):
|
||||
if listener is None:
|
||||
return
|
||||
listener.close()
|
||||
path, device, inode = owned
|
||||
try:
|
||||
info = os.lstat(path)
|
||||
if (info.st_dev, info.st_ino) == (device, inode):
|
||||
os.unlink(path)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def connect():
|
||||
fd = os.environ.pop("WAYLAND_SOCKET", "")
|
||||
if fd.isdigit():
|
||||
@@ -87,14 +216,24 @@ def main():
|
||||
launch = os.environ.get("DD_LAUNCH_DIR", "")
|
||||
report = Report(os.environ.get("DD_IME_DIR") or (os.path.join(launch, "agent") if launch else ""))
|
||||
conn = Connection(connect())
|
||||
listener, owned = listen(launch)
|
||||
try:
|
||||
return run(conn, report, listener)
|
||||
finally:
|
||||
unlisten(listener, owned)
|
||||
|
||||
|
||||
def run(conn, report, listener):
|
||||
conn.send(DISPLAY_ID, 1, struct.pack("<I", REGISTRY_ID))
|
||||
sources = [conn.sock] + ([listener] if listener is not None else [])
|
||||
contexts = {}
|
||||
current = None
|
||||
pending = None
|
||||
while True:
|
||||
timeout = None if pending is None else max(0.0, pending - time.monotonic())
|
||||
ready, _, _ = select.select(sources, [], [], timeout)
|
||||
try:
|
||||
batch = conn.messages(timeout)
|
||||
batch = conn.messages() if conn.sock in ready else []
|
||||
except (EOFError, ConnectionError):
|
||||
report.write(False)
|
||||
return 0
|
||||
@@ -114,7 +253,7 @@ def main():
|
||||
+ struct.pack("<II", min(version, 1), METHOD_ID))
|
||||
elif obj == METHOD_ID and opcode == 0:
|
||||
(context,) = struct.unpack_from("<I", payload)
|
||||
contexts[context] = [0, 0]
|
||||
contexts[context] = [0, 0, 0]
|
||||
current = context
|
||||
pending = None
|
||||
report.write(True)
|
||||
@@ -127,9 +266,13 @@ def main():
|
||||
pending = time.monotonic() + SETTLE
|
||||
elif obj in contexts and opcode == 2:
|
||||
hint, purpose = struct.unpack_from("<II", payload)
|
||||
contexts[obj] = [hint, purpose]
|
||||
contexts[obj][:2] = [hint, purpose]
|
||||
if obj == current:
|
||||
report.write(True, hint, purpose)
|
||||
elif obj in contexts and opcode == 4:
|
||||
(contexts[obj][2],) = struct.unpack_from("<I", payload)
|
||||
if listener in ready:
|
||||
serve_text(listener, conn, current, contexts[current][2] if current in contexts else 0)
|
||||
if pending is not None and time.monotonic() >= pending:
|
||||
pending = None
|
||||
if current is None:
|
||||
|
||||
@@ -15,6 +15,7 @@ MAX_TEXT = 65536
|
||||
CANCELLABLE = 1 << 31
|
||||
CLIENT_TIMEOUT = 2.0
|
||||
PROBE_TIMEOUT = 0.5
|
||||
INPUT_METHOD_TIMEOUT = 1.0
|
||||
FOCUS_PROPERTY = b'GAMESCOPE_KEYBOARD_FOCUS_DISPLAY'
|
||||
ANY_PROPERTY_TYPE = 0
|
||||
PROPERTY_WORDS = 1024
|
||||
@@ -364,9 +365,25 @@ def _cancellation_probe(connection):
|
||||
return lambda: bool(poller.poll(0))
|
||||
|
||||
|
||||
def deliver(bridge, text, cancelled=None):
|
||||
def commit_to_input_method(path, text):
|
||||
data = text.encode('utf-8')
|
||||
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
try:
|
||||
client.settimeout(INPUT_METHOD_TIMEOUT)
|
||||
client.connect(path)
|
||||
client.sendall(struct.pack('>I', len(data)) + data)
|
||||
return client.recv(1) == b'\x01'
|
||||
except OSError:
|
||||
return False
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
|
||||
def deliver(bridge, text, cancelled=None, input_method=None):
|
||||
if cancelled is not None and cancelled():
|
||||
return False
|
||||
if input_method and commit_to_input_method(input_method, text):
|
||||
return True
|
||||
target = bridge.target_display()
|
||||
if target is None:
|
||||
return False
|
||||
@@ -376,7 +393,7 @@ def deliver(bridge, text, cancelled=None):
|
||||
return bridge.type_text(state, text, cancelled)
|
||||
|
||||
|
||||
def serve_connection(connection, bridge):
|
||||
def serve_connection(connection, bridge, input_method=None):
|
||||
connection.settimeout(CLIENT_TIMEOUT)
|
||||
request = read_request(connection)
|
||||
accepted = False
|
||||
@@ -384,7 +401,7 @@ def serve_connection(connection, bridge):
|
||||
text, cancellable = request
|
||||
cancelled = _cancellation_probe(connection) if cancellable else None
|
||||
try:
|
||||
accepted = deliver(bridge, text, cancelled)
|
||||
accepted = deliver(bridge, text, cancelled, input_method)
|
||||
except Exception:
|
||||
accepted = False
|
||||
answer(connection, accepted)
|
||||
@@ -412,7 +429,7 @@ def _release(path, owned):
|
||||
pass
|
||||
|
||||
|
||||
def serve(path, bridge):
|
||||
def serve(path, bridge, input_method=None):
|
||||
if not _remove_stale(path):
|
||||
print("droiddeck-text-input: refusing to replace %s" % path, file=sys.stderr)
|
||||
return 1
|
||||
@@ -438,7 +455,7 @@ def serve(path, bridge):
|
||||
break
|
||||
continue
|
||||
try:
|
||||
serve_connection(connection, bridge)
|
||||
serve_connection(connection, bridge, input_method)
|
||||
finally:
|
||||
connection.close()
|
||||
finally:
|
||||
@@ -492,8 +509,9 @@ def _adopt_parent_death():
|
||||
|
||||
|
||||
def main(argv):
|
||||
if len(argv) != 2 or not argv[1]:
|
||||
if len(argv) not in (2, 3) or not argv[1]:
|
||||
return 2
|
||||
input_method = argv[2] if len(argv) == 3 else None
|
||||
if not _adopt_parent_death():
|
||||
return 0
|
||||
os.umask(0o077)
|
||||
@@ -501,7 +519,7 @@ def main(argv):
|
||||
signal.signal(getattr(signal, name), _stop)
|
||||
bridge = XBridge()
|
||||
try:
|
||||
return serve(argv[1], bridge)
|
||||
return serve(argv[1], bridge, input_method)
|
||||
finally:
|
||||
bridge.close()
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import runpy
|
||||
import select
|
||||
import socket
|
||||
import struct
|
||||
import subprocess
|
||||
@@ -9,6 +11,7 @@ import time
|
||||
import unittest
|
||||
|
||||
IME = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin/droiddeck-ime'
|
||||
HELPER = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin/droiddeck-text-input'
|
||||
|
||||
|
||||
def message(obj, opcode, payload=b''):
|
||||
@@ -100,5 +103,86 @@ class DesktopImeTest(unittest.TestCase):
|
||||
self.assertEqual(self.proc.wait(5), 1)
|
||||
self.assertFalse(self.state(2)['textField'])
|
||||
|
||||
def focus(self, context=100, serial=0):
|
||||
self.server.sendall(message(3, 0, struct.pack('<I', context))
|
||||
+ message(context, 4, struct.pack('<I', serial)))
|
||||
self.assertTrue(self.state(1)['textField'])
|
||||
|
||||
def send_text(self, text):
|
||||
data = text.encode('utf-8')
|
||||
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
try:
|
||||
client.settimeout(5)
|
||||
client.connect(str(self.session / 'ime-text.sock'))
|
||||
client.sendall(struct.pack('>I', len(data)) + data)
|
||||
return client.recv(1)
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
def commit_message(self, context, serial, text):
|
||||
return message(context, 1, struct.pack('<I', serial) + wl_string(text))
|
||||
|
||||
def assert_compositor_quiet(self):
|
||||
readable, _, _ = select.select([self.server], [], [], 0.3)
|
||||
self.assertEqual([], readable, 'the input method sent the compositor something')
|
||||
|
||||
def test_text_is_committed_to_the_focused_field(self):
|
||||
self.focus(serial=5)
|
||||
self.assertEqual(b'\x01', self.send_text('中文 abc😀'))
|
||||
expected = self.commit_message(100, 5, '中文 abc😀')
|
||||
self.assertEqual(expected, self.read(len(expected)))
|
||||
|
||||
def test_text_is_refused_without_a_focused_field(self):
|
||||
self.assertEqual(b'\x00', self.send_text('abc'))
|
||||
self.assert_compositor_quiet()
|
||||
self.focus()
|
||||
self.server.sendall(message(3, 1, struct.pack('<I', 100)))
|
||||
self.assertEqual(self.read(8), message(100, 0))
|
||||
self.assertEqual(b'\x00', self.send_text('abc'))
|
||||
self.assert_compositor_quiet()
|
||||
|
||||
def test_soft_controls_are_left_to_key_events(self):
|
||||
self.focus()
|
||||
for text in ('\b', '\r', 'line\n', '\t'):
|
||||
self.assertEqual(b'\x00', self.send_text(text), repr(text))
|
||||
self.assert_compositor_quiet()
|
||||
|
||||
def test_long_text_is_split_on_character_boundaries(self):
|
||||
self.focus(serial=2)
|
||||
text = 'a' * 3071 + '中' + 'b' * 2000
|
||||
self.assertEqual(b'\x01', self.send_text(text))
|
||||
first = self.commit_message(100, 2, 'a' * 3071)
|
||||
second = self.commit_message(100, 2, '中' + 'b' * 2000)
|
||||
self.assertEqual(first + second, self.read(len(first) + len(second)))
|
||||
self.assertLessEqual(max(len(first), len(second)), 4096)
|
||||
|
||||
def test_text_socket_is_private_and_removed_on_exit(self):
|
||||
path = self.session / 'ime-text.sock'
|
||||
self.assertEqual(oct(path.stat().st_mode & 0o777), '0o600')
|
||||
self.server.close()
|
||||
self.assertEqual(self.proc.wait(5), 0)
|
||||
self.assertFalse(path.exists())
|
||||
|
||||
def test_x11_helper_hands_text_to_the_input_method_before_xtest(self):
|
||||
helper = runpy.run_path(str(HELPER))
|
||||
targets = []
|
||||
|
||||
class Bridge:
|
||||
def target_display(self):
|
||||
targets.append(True)
|
||||
return None
|
||||
|
||||
path = str(self.session / 'ime-text.sock')
|
||||
self.assertFalse(helper['deliver'](Bridge(), 'abc', None, path))
|
||||
self.assertEqual([True], targets, 'without a focused field the text must fall back to X11')
|
||||
self.focus(serial=1)
|
||||
self.assertTrue(helper['deliver'](Bridge(), 'abc', None, path))
|
||||
expected = self.commit_message(100, 1, 'abc')
|
||||
self.assertEqual(expected, self.read(len(expected)))
|
||||
self.assertEqual([True], targets, 'committed text must not also be typed through X11')
|
||||
self.assertFalse(helper['deliver'](Bridge(), 'abc', None, str(self.session / 'missing.sock')))
|
||||
self.assertEqual([True, True], targets)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user