diff --git a/backends/x/event.go b/backends/x/event.go index 76a5e61..e306795 100644 --- a/backends/x/event.go +++ b/backends/x/event.go @@ -59,8 +59,17 @@ func (backend *Backend) handleKeyPress ( event xevent.KeyPressEvent, ) { keyEvent := *event.KeyPressEvent - keySym := keybind.KeysymGet(backend.connection, keyEvent.Detail, 0) - // TODO: convert to keysym and then to a button value + keysym := keybind.KeysymGet(backend.connection, keyEvent.Detail, 0) + backend.channel <- stone.EventPress(keysymToButtonCode(keysym)) +} + +func (backend *Backend) handleKeyRelease ( + connection *xgbutil.XUtil, + event xevent.KeyPressEvent, +) { + keyEvent := *event.KeyPressEvent + keysym := keybind.KeysymGet(backend.connection, keyEvent.Detail, 0) + backend.channel <- stone.EventRelease(keysymToButtonCode(keysym)) } func (backend *Backend) handleMotionNotify ( diff --git a/backends/x/unicode.go b/backends/x/unicode.go new file mode 100644 index 0000000..1c0445f --- /dev/null +++ b/backends/x/unicode.go @@ -0,0 +1,83 @@ +package x + +import "github.com/jezek/xgb/xproto" +import "git.tebibyte.media/sashakoshka/stone" + +// when making changes to this file, look at keysymdef.h + +var buttonCodeTable = map[xproto.Keysym] stone.Button { + 0xFFFFFF: stone.ButtonUnknown, + + 0xFF63: stone.KeyInsert, + 0xFF67: stone.KeyMenu, + 0xFF61: stone.KeyPrintScreen, + 0xFF6B: stone.KeyPause, + 0xFFE5: stone.KeyCapsLock, + 0xFF14: stone.KeyScrollLock, + 0xFF7F: stone.KeyNumLock, + 0xFF08: stone.KeyBackspace, + 0xFF09: stone.KeyTab, + 0xFF0A: stone.KeyEnter, + 0xFF1B: stone.KeyEscape, + + 0xFF52: stone.KeyUp, + 0xFF54: stone.KeyDown, + 0xFF51: stone.KeyLeft, + 0xFF53: stone.KeyRight, + 0xFF55: stone.KeyPageUp, + 0xFF56: stone.KeyPageDown, + 0xFF50: stone.KeyHome, + 0xFF57: stone.KeyEnd, + + 0xFFE1: stone.KeyLeftShift, + 0xFFE2: stone.KeyRightShift, + 0xFFE3: stone.KeyLeftControl, + 0xFFE4: stone.KeyRightControl, + 0xFFE9: stone.KeyLeftAlt, + 0xFFEA: stone.KeyRightAlt, + 0xFFEB: stone.KeyLeftSuper, + 0xFFEC: stone.KeyRightSuper, + + 0xFFFF: stone.KeyDelete, + + 0xFFBE: stone.KeyF1, + 0xFFBF: stone.KeyF2, + 0xFFC0: stone.KeyF3, + 0xFFC1: stone.KeyF4, + 0xFFC2: stone.KeyF5, + 0xFFC3: stone.KeyF6, + 0xFFC4: stone.KeyF7, + 0xFFC5: stone.KeyF8, + 0xFFC6: stone.KeyF9, + 0xFFC7: stone.KeyF10, + 0xFFC8: stone.KeyF11, + 0xFFC9: stone.KeyF12, +} + +func keysymToButtonCode (keysym xproto.Keysym) (button stone.Button) { + var isControl bool + button, isControl = buttonCodeTable[keysym] + if isControl { return } + + // some X keysyms have a single bit set to 1 here. i believe this is to + // prevent conflicts with existing codes. if we mask it off we will get + // a correct utf-32 code point. + if keysym & 0xF000000 == 0x1000000 { + button = stone.Button(keysym & 0x0111111) + return + } + + // X keysyms like 0xFF.. or 0xFE.. are non-character keys. we already + // resolve these by looking them up in the button code table, so if any + // that we don't support pop up we should just silence them as it is + // effectively garbage data as far as stone applications are concerned. + if (keysym >> 8) == 0xFF || (keysym >> 8) == 0xFE { + button = stone.ButtonUnknown + return + } + + // if none of these things happened, we can safely (i think) assume that + // the keysym is an exact utf-32 code point. + button = stone.Button(keysym) + return +} diff --git a/input.go b/input.go index 438f3b0..d18ea7b 100644 --- a/input.go +++ b/input.go @@ -56,29 +56,16 @@ const ( MouseButtonBack Button = MouseButton8 MouseButtonForward Button = MouseButton9 - KeyF1 Button = 136 - KeyF2 Button = 137 - KeyF3 Button = 138 - KeyF4 Button = 139 - KeyF5 Button = 140 - KeyF6 Button = 141 - KeyF7 Button = 142 - KeyF8 Button = 143 - KeyF9 Button = 144 - KeyF10 Button = 145 - KeyF11 Button = 146 - KeyF12 Button = 147 - KeyF13 Button = 148 - KeyF14 Button = 149 - KeyF15 Button = 150 - KeyF16 Button = 151 - KeyF17 Button = 152 - KeyF18 Button = 153 - KeyF19 Button = 154 - KeyF20 Button = 155 - KeyF21 Button = 156 - KeyF22 Button = 157 - KeyF23 Button = 158 - KeyF24 Button = 159 - KeyF25 Button = 28 + KeyF1 Button = 144 + KeyF2 Button = 145 + KeyF3 Button = 146 + KeyF4 Button = 147 + KeyF5 Button = 148 + KeyF6 Button = 149 + KeyF7 Button = 150 + KeyF8 Button = 151 + KeyF9 Button = 152 + KeyF10 Button = 153 + KeyF11 Button = 154 + KeyF12 Button = 155 )