keys.hh (3200B)
1 #ifndef keys_hh_INCLUDED 2 #define keys_hh_INCLUDED 3 4 #include "coord.hh" 5 #include "flags.hh" 6 #include "hash.hh" 7 #include "meta.hh" 8 #include "optional.hh" 9 #include "unicode.hh" 10 #include "vector.hh" 11 12 #include <cstdint> 13 14 namespace Kakoune 15 { 16 17 struct Key 18 { 19 enum class MouseButton 20 { 21 Left, 22 Middle, 23 Right 24 }; 25 enum class Modifiers : int 26 { 27 None = 0, 28 Control = 1 << 0, 29 Alt = 1 << 1, 30 Shift = 1 << 2, 31 32 MousePress = 1 << 3, 33 MouseRelease = 1 << 4, 34 MousePos = 1 << 5, 35 MouseButtonMask= 0b11 << 6, 36 37 Scroll = 1 << 8, 38 Resize = 1 << 9, 39 MenuSelect = 1 << 10, 40 }; 41 enum NamedKey : Codepoint 42 { 43 // use UTF-16 surrogate pairs range 44 Backspace = 0xD800, 45 Delete, 46 Escape, 47 Return, 48 Up, 49 Down, 50 Left, 51 Right, 52 PageUp, 53 PageDown, 54 Home, 55 End, 56 Insert, 57 Tab, 58 Space, 59 F1, 60 F2, 61 F3, 62 F4, 63 F5, 64 F6, 65 F7, 66 F8, 67 F9, 68 F10, 69 F11, 70 F12, 71 FocusIn, 72 FocusOut, 73 Invalid, 74 }; 75 76 Modifiers modifiers = {}; 77 Codepoint key = {}; 78 79 constexpr Key(Modifiers modifiers, Codepoint key) 80 : modifiers(modifiers), key(key) {} 81 82 constexpr Key(Codepoint key) 83 : modifiers(Modifiers::None), key(key) {} 84 85 constexpr Key() = default; 86 87 constexpr uint64_t val() const { return (uint64_t)modifiers << 32 | key; } 88 89 constexpr bool operator==(Key other) const { return val() == other.val(); } 90 constexpr auto operator<=>(Key other) const { return val() <=> other.val(); } 91 92 constexpr DisplayCoord coord() const { return {(int)((int32_t) (key & 0xFFFF0000) >> 16), (int)(key & 0x0000FFFF)}; } 93 constexpr MouseButton mouse_button() { return MouseButton{((int)modifiers & (int)Modifiers::MouseButtonMask) >> 6}; } 94 constexpr int scroll_amount() { return (int32_t)modifiers >> 16; } 95 static Modifiers to_modifier(MouseButton button) { return Key::Modifiers{((int)button << 6) & (int)Modifiers::MouseButtonMask}; } 96 97 Optional<Codepoint> codepoint() const; 98 }; 99 100 constexpr bool with_bit_ops(Meta::Type<Key::Modifiers>) { return true; } 101 102 using KeyList = Vector<Key, MemoryDomain::Mapping>; 103 104 class String; 105 class StringView; 106 107 KeyList parse_keys(StringView str); 108 String to_string(Key key); 109 StringView to_string(Key::MouseButton button); 110 Key::MouseButton str_to_button(StringView str); 111 112 constexpr Key shift(Key key) 113 { 114 return { key.modifiers | Key::Modifiers::Shift, key.key }; 115 } 116 constexpr Key alt(Key key) 117 { 118 return { key.modifiers | Key::Modifiers::Alt, key.key }; 119 } 120 constexpr Key ctrl(Key key) 121 { 122 return { key.modifiers | Key::Modifiers::Control, key.key }; 123 } 124 125 constexpr Codepoint encode_coord(DisplayCoord coord) { return (Codepoint)(((int)coord.line << 16) | ((int)coord.column & 0x0000FFFF)); } 126 127 constexpr Key resize(DisplayCoord dim) { return { Key::Modifiers::Resize, encode_coord(dim) }; } 128 129 constexpr size_t hash_value(const Key& key) { return hash_values(key.modifiers, key.key); } 130 131 } 132 133 #endif // keys_hh_INCLUDED