Reduce size of encoded key event by using base64 encoding

This commit is contained in:
Kovid Goyal 2017-02-10 15:34:06 +05:30
parent ded9cf227a
commit d9563e52c8
4 changed files with 143 additions and 140 deletions

View File

@ -5,124 +5,124 @@ See link:protocol-extensions.asciidoc#keyboard-handling[Keyboard Handling protoc
|=== |===
| Name | Encoded representation (base64) | Name | Encoded representation (base64)
| 0 | `BM` | 0 | `G`
| 1 | `BN` | 1 | `H`
| 2 | `BO` | 2 | `I`
| 3 | `BP` | 3 | `J`
| 4 | `BQ` | 4 | `K`
| 5 | `BR` | 5 | `L`
| 6 | `BS` | 6 | `M`
| 7 | `BT` | 7 | `N`
| 8 | `BU` | 8 | `O`
| 9 | `BV` | 9 | `P`
| A | `Bd` | A | `S`
| APOSTROPHE | `BD` | APOSTROPHE | `B`
| B | `Be` | B | `T`
| BACKSLASH | `CU` | BACKSLASH | `t`
| BACKSPACE | `HH` | BACKSPACE | `1`
| C | `Bf` | C | `U`
| CAPS LOCK | `Hc` | CAPS LOCK | `BA`
| COMMA | `BI` | COMMA | `C`
| D | `Bg` | D | `V`
| DELETE | `HJ` | DELETE | `3`
| DOWN | `HM` | DOWN | `6`
| E | `Bh` | E | `W`
| END | `HR` | END | `/`
| ENTER | `HF` | ENTER | `z`
| EQUAL | `BZ` | EQUAL | `R`
| ESCAPE | `HE` | ESCAPE | `y`
| F | `Bi` | F | `X`
| F1 | `IC` | F1 | `BF`
| F10 | `IL` | F10 | `BO`
| F11 | `IM` | F11 | `BP`
| F12 | `IN` | F12 | `BQ`
| F13 | `IO` | F13 | `BR`
| F14 | `IP` | F14 | `BS`
| F15 | `IQ` | F15 | `BT`
| F16 | `IR` | F16 | `BU`
| F17 | `IS` | F17 | `BV`
| F18 | `IT` | F18 | `BW`
| F19 | `IU` | F19 | `BX`
| F2 | `ID` | F2 | `BG`
| F20 | `IV` | F20 | `BY`
| F21 | `IW` | F21 | `BZ`
| F22 | `IX` | F22 | `Ba`
| F23 | `IY` | F23 | `Bb`
| F24 | `IZ` | F24 | `Bc`
| F25 | `Ia` | F25 | `Bd`
| F3 | `IE` | F3 | `BH`
| F4 | `IF` | F4 | `BI`
| F5 | `IG` | F5 | `BJ`
| F6 | `IH` | F6 | `BK`
| F7 | `II` | F7 | `BL`
| F8 | `IJ` | F8 | `BM`
| F9 | `IK` | F9 | `BN`
| G | `Bj` | G | `Y`
| GRAVE ACCENT | `CY` | GRAVE ACCENT | `v`
| H | `CA` | H | `Z`
| HOME | `HQ` | HOME | `+`
| I | `CB` | I | `a`
| INSERT | `HI` | INSERT | `2`
| J | `CC` | J | `b`
| K | `CD` | K | `c`
| KP 0 | `Ig` | KP 0 | `Be`
| KP 1 | `Ih` | KP 1 | `Bf`
| KP 2 | `Ii` | KP 2 | `Bg`
| KP 3 | `Ij` | KP 3 | `Bh`
| KP 4 | `JA` | KP 4 | `Bi`
| KP 5 | `JB` | KP 5 | `Bj`
| KP 6 | `JC` | KP 6 | `Bk`
| KP 7 | `JD` | KP 7 | `Bl`
| KP 8 | `JE` | KP 8 | `Bm`
| KP 9 | `JF` | KP 9 | `Bn`
| KP ADD | `JK` | KP ADD | `Bs`
| KP DECIMAL | `JG` | KP DECIMAL | `Bo`
| KP DIVIDE | `JH` | KP DIVIDE | `Bp`
| KP ENTER | `JL` | KP ENTER | `Bt`
| KP EQUAL | `JM` | KP EQUAL | `Bu`
| KP MULTIPLY | `JI` | KP MULTIPLY | `Bq`
| KP SUBTRACT | `JJ` | KP SUBTRACT | `Br`
| L | `CE` | L | `d`
| LEFT | `HL` | LEFT | `5`
| LEFT ALT | `JS` | LEFT ALT | `Bx`
| LEFT BRACKET | `CT` | LEFT BRACKET | `s`
| LEFT CONTROL | `JR` | LEFT CONTROL | `Bw`
| LEFT SHIFT | `JQ` | LEFT SHIFT | `Bv`
| LEFT SUPER | `JT` | LEFT SUPER | `By`
| M | `CF` | M | `e`
| MINUS | `BJ` | MINUS | `D`
| N | `CG` | N | `f`
| NUM LOCK | `He` | NUM LOCK | `BC`
| O | `CH` | O | `g`
| P | `CI` | P | `h`
| PAGE DOWN | `HP` | PAGE DOWN | `9`
| PAGE UP | `HO` | PAGE UP | `8`
| PAUSE | `Hg` | PAUSE | `BE`
| PERIOD | `BK` | PERIOD | `E`
| PRINT SCREEN | `Hf` | PRINT SCREEN | `BD`
| Q | `CJ` | Q | `i`
| R | `CK` | R | `j`
| RIGHT | `HK` | RIGHT | `4`
| RIGHT ALT | `JW` | RIGHT ALT | `B1`
| RIGHT BRACKET | `CV` | RIGHT BRACKET | `u`
| RIGHT CONTROL | `JV` | RIGHT CONTROL | `B0`
| RIGHT SHIFT | `JU` | RIGHT SHIFT | `Bz`
| RIGHT SUPER | `JX` | RIGHT SUPER | `B2`
| S | `CL` | S | `k`
| SCROLL LOCK | `Hd` | SCROLL LOCK | `BB`
| SEMICOLON | `BX` | SEMICOLON | `Q`
| SLASH | `BL` | SLASH | `F`
| SPACE | `g` | SPACE | `A`
| T | `CM` | T | `l`
| TAB | `HG` | TAB | `0`
| U | `CN` | U | `m`
| UP | `HN` | UP | `7`
| V | `CO` | V | `n`
| W | `CP` | W | `o`
| WORLD 1 | `ER` | WORLD 1 | `w`
| WORLD 2 | `ES` | WORLD 2 | `x`
| X | `CQ` | X | `p`
| Y | `CR` | Y | `q`
| Z | `CS` | Z | `r`
|=== |===

View File

@ -87,23 +87,29 @@ action_map = {defines.GLFW_PRESS: b'p', defines.GLFW_RELEASE: b'r', defines.GLFW
def base64_encode(integer, chars=string.ascii_uppercase + string.ascii_lowercase + string.digits + '+/'): def base64_encode(integer, chars=string.ascii_uppercase + string.ascii_lowercase + string.digits + '+/'):
ans = '' ans = ''
while integer > 0: while True:
integer, remainder = divmod(integer, 36) integer, remainder = divmod(integer, 64)
ans = chars[remainder] + ans ans = chars[remainder] + ans
if integer == 0:
break
return ans return ans
def key_extended_map(): def generate_key_extended_map(symbolic=False):
ans = {} ans = {}
for k in dir(defines): i = 0
if k.startswith('GLFW_KEY_'): keys = {a for a in dir(defines) if a.startswith('GLFW_KEY_')}
val = getattr(defines, k)
if val < defines.GLFW_KEY_LAST and val != defines.GLFW_KEY_UNKNOWN: for k in sorted(keys, key=lambda k: getattr(defines, k)):
ans[k[9:]] = base64_encode(val) val = getattr(defines, k)
if val < defines.GLFW_KEY_LAST and val != defines.GLFW_KEY_UNKNOWN:
key = k[9:] if symbolic else val
ans[key] = base64_encode(i)
i += 1
return ans return ans
key_extended_map = key_extended_map() key_extended_map = generate_key_extended_map()
def extended_key_event(key, scancode, mods, action): def extended_key_event(key, scancode, mods, action):
@ -113,7 +119,7 @@ def extended_key_event(key, scancode, mods, action):
return b'' return b''
if mods == 0 and key in (defines.GLFW_KEY_BACKSPACE, defines.GLFW_KEY_ENTER): if mods == 0 and key in (defines.GLFW_KEY_BACKSPACE, defines.GLFW_KEY_ENTER):
return smkx_key_map[key] return smkx_key_map[key]
return '\033_K{}{:x}:{}\033\\'.format(action_map[action], mods, key_extended_map[key]).encode('ascii') return '\033_K{}{}{}\033\\'.format(action_map[action], base64_encode(mods), key_extended_map[key]).encode('ascii')
def interpret_key_event(key, scancode, mods, window, action): def interpret_key_event(key, scancode, mods, window, action):

View File

@ -37,7 +37,7 @@ if raw != nraw:
raw = subprocess.check_output([ raw = subprocess.check_output([
'kitty', '-c', 'kitty', '-c',
'from kitty.keys import *; import json; print(json.dumps(key_extended_map))' 'from kitty.keys import *; import json; print(json.dumps(generate_key_extended_map(True)))'
]).decode('utf-8') ]).decode('utf-8')
key_map = json.loads(raw) key_map = json.loads(raw)
lines = [ lines = [

View File

@ -362,21 +362,18 @@ The escape sequence encodes the following properties:
* The actual key being pressed * The actual key being pressed
``` ```
<ESC>_K<type><modifiers>:<key><ESC>\ <ESC>_K<type><modifiers><key><ESC>\
``` ```
Where `<type>` is one of `p` -- press, `r` -- release and `t` -- repeat. Where `<type>` is one of `p` -- press, `r` -- release and `t` -- repeat.
Modifiers is a bitmask represented as a single hexadecimal digit in lower case. Modifiers is a bitmask represented as a single base64 digit. Shift -- `0x1`,
Shift -- `0x1`, Control -- `0x2`, Alt -- `0x4` and Super -- `0x8`. `<key>` is Control -- `0x2`, Alt -- `0x4` and Super -- `0x8`. `<key>` is a number
a number (encoded in base64) corresponding to the key pressed. The key name to (encoded in base64) corresponding to the key pressed. The key name to number
number mapping is defined in link:key_encoding.asciidoc[this table]. mapping is defined in link:key_encoding.asciidoc[this table].
For example: For example:
``` ```
<ESC>_Kp6:CQ<ESC>\ is <Ctrl>+<Alt>+x (press) <ESC>_KpGp<ESC>\ is <Ctrl>+<Alt>+x (press)
<ESC>_Krf:HO<ESC>\ is <Ctrl>+<Alt>+<Shift>+<Super>+PageUp (release) <ESC>_KrP8<ESC>\ is <Ctrl>+<Alt>+<Shift>+<Super>+PageUp (release)
``` ```
There is a `:` between the modifiers and the key-press so that in the future
more modifiers can be added, if needed.