mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-09 03:36:48 +00:00
feat(console): typed text follows the VM's keyboard layout
"Type it" sent each character's keysym and let QEMU pick the key from its own US map, with Shift added for the US shifted characters. A VM on another layout got other characters: on Turkish Q, '"' typed 'İ', '@' typed "'", and ğ, ü, ş, ı, ö, ç typed nothing. The Clipboard dialog now asks for the VM's keyboard layout and remembers it per browser. Each character is typed as the key that makes it on that layout (its scancode), with Shift, AltGr or both (AltGr first: pressed after Shift, the right Alt is Alt on the Linux console), so the VM's own layout makes the character. The layout is fixed when typing starts. Text with a character no key types on the layout is not typed at all, and the dialog lists those characters: a password or a command with a character left out would be a wrong one. A layout is one file in console/layouts/: its name and, for each key, the characters it types plainly, with Shift, with AltGr and with Shift+AltGr. The view offers every file, US first. dev/console_layout.py writes a file from the system's XKB data as libxkbcommon computes it (through ctypes, no new dependency), mapping keys to DOM codes with noVNC's table from QEMU's keycodemapdb; it lists the dead keys it leaves out, and drops the keys the keyboard model lacks (the key left of Z on US pc104 keyboards). English (US) and Turkish Q come with it; the README says how to add one.
This commit is contained in:
parent
2b54198965
commit
bfa0fc9fe5
10 changed files with 431 additions and 21 deletions
47
console/tests/test_layouts.py
Normal file
47
console/tests/test_layouts.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
"""The keyboard layout files the VNC console types text with."""
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from django.conf import settings
|
||||
from django.test import SimpleTestCase
|
||||
|
||||
from console.views import LAYOUTS_DIR
|
||||
|
||||
SCANCODES = Path(settings.BASE_DIR) / "static/js/novnc/core/input/xtscancodes.js"
|
||||
|
||||
|
||||
def unique_keys(pairs):
|
||||
keys = [key for key, _ in pairs]
|
||||
if len(keys) != len(set(keys)):
|
||||
raise ValueError(f"a key twice: {keys}")
|
||||
return dict(pairs)
|
||||
|
||||
|
||||
class LayoutFilesTestCase(SimpleTestCase):
|
||||
def test_each_layout_file_is_a_layout(self):
|
||||
# the key codes noVNC sends as scancodes
|
||||
codes = {line.split('"')[1] for line in SCANCODES.read_text().splitlines() if line.startswith(' "')}
|
||||
paths = sorted(LAYOUTS_DIR.glob("*.json"))
|
||||
self.assertIn("us", [path.stem for path in paths])
|
||||
for path in paths:
|
||||
with self.subTest(path.name):
|
||||
layout = json.loads(path.read_text(), object_pairs_hook=unique_keys)
|
||||
self.assertRegex(path.stem, r"^[a-z]+(-[a-z0-9_]+)?$")
|
||||
self.assertEqual(set(layout), {"name", "source", "keys"})
|
||||
self.assertTrue(layout["name"])
|
||||
for code, characters in layout["keys"].items():
|
||||
self.assertIn(code, codes)
|
||||
self.assertEqual(len(characters), 4, code)
|
||||
for character in characters:
|
||||
# one printable character, or none
|
||||
self.assertTrue(character is None or (len(character) == 1 and character.isprintable()),
|
||||
f"{code}: {character!r}")
|
||||
|
||||
def test_us_and_turkish_q_type_every_printable_ascii_character(self):
|
||||
ascii_characters = {chr(c) for c in range(0x21, 0x7F)}
|
||||
for layout_id in ("us", "tr"):
|
||||
with self.subTest(layout_id):
|
||||
layout = json.loads((LAYOUTS_DIR / f"{layout_id}.json").read_text())
|
||||
typed = {c for characters in layout["keys"].values() for c in characters if c}
|
||||
self.assertEqual(ascii_characters - typed, set())
|
||||
|
|
@ -8,6 +8,7 @@ from unittest.mock import MagicMock, patch
|
|||
from accounts.models import UserInstance
|
||||
from appsettings.models import AppSettings
|
||||
from computes.models import Compute
|
||||
from console.views import LAYOUTS_DIR
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.core.signing import TimestampSigner
|
||||
|
|
@ -250,6 +251,19 @@ class ConsoleViewsTestCase(TestCase):
|
|||
script = (Path(settings.BASE_DIR) / "static/js/console_vnc.js").read_text()
|
||||
self.assertIn("scheme + window.location.host + '/novncd/?token='", script)
|
||||
|
||||
@patch("console.views.wvmInstance")
|
||||
def test_the_vnc_page_offers_each_keyboard_layout_file(self, mock_wvm):
|
||||
mock_wvm.return_value.get_console_type.return_value = "vnc"
|
||||
mock_wvm.return_value.get_console_passwd.return_value = None
|
||||
self.client.force_login(self.alice)
|
||||
response = self.client.get(self.url)
|
||||
layouts = json.loads(html.fromstring(response.content).get_element_by_id("keyboard-layouts").text)
|
||||
files = sorted(path.stem for path in LAYOUTS_DIR.glob("*.json"))
|
||||
self.assertEqual(sorted(layouts), files)
|
||||
self.assertEqual(next(iter(layouts)), "us")
|
||||
for layout_id, layout in layouts.items():
|
||||
self.assertContains(response, f'<option value="{layout_id}">{layout["name"]}</option>', html=True)
|
||||
|
||||
@patch("console.views.wvmInstance")
|
||||
def test_the_vnc_page_starts_as_the_console_settings_say(self, mock_wvm):
|
||||
mock_wvm.return_value.get_console_type.return_value = "vnc"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue