Debian Codegraph @

input/kitty.go

raw
1 package input
2
3 import (
4 "unicode"
5 "unicode/utf8"
6
7 "github.com/charmbracelet/x/ansi"
8 "github.com/charmbracelet/x/ansi/kitty"
9 )
10
11 // KittyGraphicsEvent represents a Kitty Graphics response event.
12 //
13 // See https://sw.kovidgoyal.net/kitty/graphics-protocol/
14 type KittyGraphicsEvent struct {
15 Options kitty.Options
16 Payload []byte
17 }
18
19 // KittyEnhancementsEvent represents a Kitty enhancements event.
20 type KittyEnhancementsEvent int
21
22 // Kitty keyboard enhancement constants.
23 // See https://sw.kovidgoyal.net/kitty/keyboard-protocol/#progressive-enhancement
24 const (
25 KittyDisambiguateEscapeCodes KittyEnhancementsEvent = 1 << iota
26 KittyReportEventTypes
27 KittyReportAlternateKeys
28 KittyReportAllKeysAsEscapeCodes
29 KittyReportAssociatedText
30 )
31
32 // Contains reports whether m contains the given enhancements.
33 func (e KittyEnhancementsEvent) Contains(enhancements KittyEnhancementsEvent) bool {
34 return e&enhancements == enhancements
35 }
36
37 // Kitty Clipboard Control Sequences.
38 var kittyKeyMap = map[int]Key{
39 ansi.BS: {Code: KeyBackspace},
40 ansi.HT: {Code: KeyTab},
41 ansi.CR: {Code: KeyEnter},
42 ansi.ESC: {Code: KeyEscape},
43 ansi.DEL: {Code: KeyBackspace},
44
45 57344: {Code: KeyEscape},
46 57345: {Code: KeyEnter},
47 57346: {Code: KeyTab},
48 57347: {Code: KeyBackspace},
49 57348: {Code: KeyInsert},
50 57349: {Code: KeyDelete},
51 57350: {Code: KeyLeft},
52 57351: {Code: KeyRight},
53 57352: {Code: KeyUp},
54 57353: {Code: KeyDown},
55 57354: {Code: KeyPgUp},
56 57355: {Code: KeyPgDown},
57 57356: {Code: KeyHome},
58 57357: {Code: KeyEnd},
59 57358: {Code: KeyCapsLock},
60 57359: {Code: KeyScrollLock},
61 57360: {Code: KeyNumLock},
62 57361: {Code: KeyPrintScreen},
63 57362: {Code: KeyPause},
64 57363: {Code: KeyMenu},
65 57364: {Code: KeyF1},
66 57365: {Code: KeyF2},
67 57366: {Code: KeyF3},
68 57367: {Code: KeyF4},
69 57368: {Code: KeyF5},
70 57369: {Code: KeyF6},
71 57370: {Code: KeyF7},
72 57371: {Code: KeyF8},
73 57372: {Code: KeyF9},
74 57373: {Code: KeyF10},
75 57374: {Code: KeyF11},
76 57375: {Code: KeyF12},
77 57376: {Code: KeyF13},
78 57377: {Code: KeyF14},
79 57378: {Code: KeyF15},
80 57379: {Code: KeyF16},
81 57380: {Code: KeyF17},
82 57381: {Code: KeyF18},
83 57382: {Code: KeyF19},
84 57383: {Code: KeyF20},
85 57384: {Code: KeyF21},
86 57385: {Code: KeyF22},
87 57386: {Code: KeyF23},
88 57387: {Code: KeyF24},
89 57388: {Code: KeyF25},
90 57389: {Code: KeyF26},
91 57390: {Code: KeyF27},
92 57391: {Code: KeyF28},
93 57392: {Code: KeyF29},
94 57393: {Code: KeyF30},
95 57394: {Code: KeyF31},
96 57395: {Code: KeyF32},
97 57396: {Code: KeyF33},
98 57397: {Code: KeyF34},
99 57398: {Code: KeyF35},
100 57399: {Code: KeyKp0},
101 57400: {Code: KeyKp1},
102 57401: {Code: KeyKp2},
103 57402: {Code: KeyKp3},
104 57403: {Code: KeyKp4},
105 57404: {Code: KeyKp5},
106 57405: {Code: KeyKp6},
107 57406: {Code: KeyKp7},
108 57407: {Code: KeyKp8},
109 57408: {Code: KeyKp9},
110 57409: {Code: KeyKpDecimal},
111 57410: {Code: KeyKpDivide},
112 57411: {Code: KeyKpMultiply},
113 57412: {Code: KeyKpMinus},
114 57413: {Code: KeyKpPlus},
115 57414: {Code: KeyKpEnter},
116 57415: {Code: KeyKpEqual},
117 57416: {Code: KeyKpSep},
118 57417: {Code: KeyKpLeft},
119 57418: {Code: KeyKpRight},
120 57419: {Code: KeyKpUp},
121 57420: {Code: KeyKpDown},
122 57421: {Code: KeyKpPgUp},
123 57422: {Code: KeyKpPgDown},
124 57423: {Code: KeyKpHome},
125 57424: {Code: KeyKpEnd},
126 57425: {Code: KeyKpInsert},
127 57426: {Code: KeyKpDelete},
128 57427: {Code: KeyKpBegin},
129 57428: {Code: KeyMediaPlay},
130 57429: {Code: KeyMediaPause},
131 57430: {Code: KeyMediaPlayPause},
132 57431: {Code: KeyMediaReverse},
133 57432: {Code: KeyMediaStop},
134 57433: {Code: KeyMediaFastForward},
135 57434: {Code: KeyMediaRewind},
136 57435: {Code: KeyMediaNext},
137 57436: {Code: KeyMediaPrev},
138 57437: {Code: KeyMediaRecord},
139 57438: {Code: KeyLowerVol},
140 57439: {Code: KeyRaiseVol},
141 57440: {Code: KeyMute},
142 57441: {Code: KeyLeftShift},
143 57442: {Code: KeyLeftCtrl},
144 57443: {Code: KeyLeftAlt},
145 57444: {Code: KeyLeftSuper},
146 57445: {Code: KeyLeftHyper},
147 57446: {Code: KeyLeftMeta},
148 57447: {Code: KeyRightShift},
149 57448: {Code: KeyRightCtrl},
150 57449: {Code: KeyRightAlt},
151 57450: {Code: KeyRightSuper},
152 57451: {Code: KeyRightHyper},
153 57452: {Code: KeyRightMeta},
154 57453: {Code: KeyIsoLevel3Shift},
155 57454: {Code: KeyIsoLevel5Shift},
156 }
157
158 func init() {
159 // These are some faulty C0 mappings some terminals such as WezTerm have
160 // and doesn't follow the specs.
161 kittyKeyMap[ansi.NUL] = Key{Code: KeySpace, Mod: ModCtrl}
162 for i := ansi.SOH; i <= ansi.SUB; i++ {
163 if _, ok := kittyKeyMap[i]; !ok {
164 kittyKeyMap[i] = Key{Code: rune(i + 0x60), Mod: ModCtrl}
165 }
166 }
167 for i := ansi.FS; i <= ansi.US; i++ {
168 if _, ok := kittyKeyMap[i]; !ok {
169 kittyKeyMap[i] = Key{Code: rune(i + 0x40), Mod: ModCtrl}
170 }
171 }
172 }
173
174 const (
175 kittyShift = 1 << iota
176 kittyAlt
177 kittyCtrl
178 kittySuper
179 kittyHyper
180 kittyMeta
181 kittyCapsLock
182 kittyNumLock
183 )
184
185 func fromKittyMod(mod int) KeyMod {
186 var m KeyMod
187 if mod&kittyShift != 0 {
188 m |= ModShift
189 }
190 if mod&kittyAlt != 0 {
191 m |= ModAlt
192 }
193 if mod&kittyCtrl != 0 {
194 m |= ModCtrl
195 }
196 if mod&kittySuper != 0 {
197 m |= ModSuper
198 }
199 if mod&kittyHyper != 0 {
200 m |= ModHyper
201 }
202 if mod&kittyMeta != 0 {
203 m |= ModMeta
204 }
205 if mod&kittyCapsLock != 0 {
206 m |= ModCapsLock
207 }
208 if mod&kittyNumLock != 0 {
209 m |= ModNumLock
210 }
211 return m
212 }
213
214 // parseKittyKeyboard parses a Kitty Keyboard Protocol sequence.
215 //
216 // In `CSI u`, this is parsed as:
217 //
218 // CSI codepoint ; modifiers u
219 // codepoint: ASCII Dec value
220 //
221 // The Kitty Keyboard Protocol extends this with optional components that can be
222 // enabled progressively. The full sequence is parsed as:
223 //
224 // CSI unicode-key-code:alternate-key-codes ; modifiers:event-type ; text-as-codepoints u
225 //
226 // See https://sw.kovidgoyal.net/kitty/keyboard-protocol/
227 func parseKittyKeyboard(params ansi.Params) (Event Event) {
228 var isRelease bool
229 var key Key
230
231 // The index of parameters separated by semicolons ';'. Sub parameters are
232 // separated by colons ':'.
233 var paramIdx int
234 var sudIdx int // The sub parameter index
235 for _, p := range params {
236 // Kitty Keyboard Protocol has 3 optional components.
237 switch paramIdx {
238 case 0:
239 switch sudIdx {
240 case 0:
241 var foundKey bool
242 code := p.Param(1) // CSI u has a default value of 1
243 key, foundKey = kittyKeyMap[code]
244 if !foundKey {
245 r := rune(code)
246 if !utf8.ValidRune(r) {
247 r = utf8.RuneError
248 }
249
250 key.Code = r
251 }
252
253 case 2:
254 // shifted key + base key
255 if b := rune(p.Param(1)); unicode.IsPrint(b) {
256 // XXX: When alternate key reporting is enabled, the protocol
257 // can return 3 things, the unicode codepoint of the key,
258 // the shifted codepoint of the key, and the standard
259 // PC-101 key layout codepoint.
260 // This is useful to create an unambiguous mapping of keys
261 // when using a different language layout.
262 key.BaseCode = b
263 }
264 fallthrough
265
266 case 1:
267 // shifted key
268 if s := rune(p.Param(1)); unicode.IsPrint(s) {
269 // XXX: We swap keys here because we want the shifted key
270 // to be the Rune that is returned by the event.
271 // For example, shift+a should produce "A" not "a".
272 // In such a case, we set AltRune to the original key "a"
273 // and Rune to "A".
274 key.ShiftedCode = s
275 }
276 }
277 case 1:
278 switch sudIdx {
279 case 0:
280 mod := p.Param(1)
281 if mod > 1 {
282 key.Mod = fromKittyMod(mod - 1)
283 if key.Mod > ModShift {
284 // XXX: We need to clear the text if we have a modifier key
285 // other than a [ModShift] key.
286 key.Text = ""
287 }
288 }
289
290 case 1:
291 switch p.Param(1) {
292 case 2:
293 key.IsRepeat = true
294 case 3:
295 isRelease = true
296 }
297 case 2:
298 }
299 case 2:
300 if code := p.Param(0); code != 0 {
301 key.Text += string(rune(code))
302 }
303 }
304
305 sudIdx++
306 if !p.HasMore() {
307 paramIdx++
308 sudIdx = 0
309 }
310 }
311
312 //nolint:nestif
313 if len(key.Text) == 0 && unicode.IsPrint(key.Code) &&
314 (key.Mod <= ModShift || key.Mod == ModCapsLock || key.Mod == ModShift|ModCapsLock) {
315 if key.Mod == 0 {
316 key.Text = string(key.Code)
317 } else {
318 desiredCase := unicode.ToLower
319 if key.Mod.Contains(ModShift) || key.Mod.Contains(ModCapsLock) {
320 desiredCase = unicode.ToUpper
321 }
322 if key.ShiftedCode != 0 {
323 key.Text = string(key.ShiftedCode)
324 } else {
325 key.Text = string(desiredCase(key.Code))
326 }
327 }
328 }
329
330 if isRelease {
331 return KeyReleaseEvent(key)
332 }
333
334 return KeyPressEvent(key)
335 }
336
337 // parseKittyKeyboardExt parses a Kitty Keyboard Protocol sequence extensions
338 // for non CSI u sequences. This includes things like CSI A, SS3 A and others,
339 // and CSI ~.
340 func parseKittyKeyboardExt(params ansi.Params, k KeyPressEvent) Event {
341 // Handle Kitty keyboard protocol
342 if len(params) > 2 && // We have at least 3 parameters
343 params[0].Param(1) == 1 && // The first parameter is 1 (defaults to 1)
344 params[1].HasMore() { // The second parameter is a subparameter (separated by a ":")
345 switch params[2].Param(1) { // The third parameter is the event type (defaults to 1)
346 case 2:
347 k.IsRepeat = true
348 case 3:
349 return KeyReleaseEvent(k)
350 }
351 }
352 return k
353 }