keysym.rs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use miniquad::{KeyCode, MouseButton};
  19. pub trait KeyCodeAsStr {
  20. fn to_str(&self) -> &str;
  21. }
  22. impl KeyCodeAsStr for KeyCode {
  23. fn to_str(&self) -> &str {
  24. match self {
  25. Self::Space => " ",
  26. Self::Apostrophe => "'",
  27. Self::Comma => ",",
  28. Self::Minus => "-",
  29. Self::Period => ".",
  30. Self::Slash => "/",
  31. Self::Key0 => "0",
  32. Self::Key1 => "1",
  33. Self::Key2 => "2",
  34. Self::Key3 => "3",
  35. Self::Key4 => "4",
  36. Self::Key5 => "5",
  37. Self::Key6 => "6",
  38. Self::Key7 => "7",
  39. Self::Key8 => "8",
  40. Self::Key9 => "9",
  41. Self::Semicolon => ";",
  42. Self::Equal => "=",
  43. Self::A => "A",
  44. Self::B => "B",
  45. Self::C => "C",
  46. Self::D => "D",
  47. Self::E => "E",
  48. Self::F => "F",
  49. Self::G => "G",
  50. Self::H => "H",
  51. Self::I => "I",
  52. Self::J => "J",
  53. Self::K => "K",
  54. Self::L => "L",
  55. Self::M => "M",
  56. Self::N => "N",
  57. Self::O => "O",
  58. Self::P => "P",
  59. Self::Q => "Q",
  60. Self::R => "R",
  61. Self::S => "S",
  62. Self::T => "T",
  63. Self::U => "U",
  64. Self::V => "V",
  65. Self::W => "W",
  66. Self::X => "X",
  67. Self::Y => "Y",
  68. Self::Z => "Z",
  69. Self::LeftBracket => "(",
  70. Self::Backslash => "\\",
  71. Self::RightBracket => ")",
  72. Self::GraveAccent => "`",
  73. Self::World1 => "World1",
  74. Self::World2 => "World2",
  75. Self::Escape => "Escape",
  76. Self::Enter => "\n",
  77. Self::Tab => "Tab",
  78. Self::Backspace => "Backspace",
  79. Self::Insert => "Insert",
  80. Self::Delete => "Delete",
  81. Self::Right => "Right",
  82. Self::Left => "Left",
  83. Self::Down => "Down",
  84. Self::Up => "Up",
  85. Self::PageUp => "PageUp",
  86. Self::PageDown => "PageDown",
  87. Self::Home => "Home",
  88. Self::End => "End",
  89. Self::CapsLock => "CapsLock",
  90. Self::ScrollLock => "ScrollLock",
  91. Self::NumLock => "NumLock",
  92. Self::PrintScreen => "PrintScreen",
  93. Self::Pause => "Pause",
  94. Self::F1 => "F1",
  95. Self::F2 => "F2",
  96. Self::F3 => "F3",
  97. Self::F4 => "F4",
  98. Self::F5 => "F5",
  99. Self::F6 => "F6",
  100. Self::F7 => "F7",
  101. Self::F8 => "F8",
  102. Self::F9 => "F9",
  103. Self::F10 => "F10",
  104. Self::F11 => "F11",
  105. Self::F12 => "F12",
  106. Self::F13 => "F13",
  107. Self::F14 => "F14",
  108. Self::F15 => "F15",
  109. Self::F16 => "F16",
  110. Self::F17 => "F17",
  111. Self::F18 => "F18",
  112. Self::F19 => "F19",
  113. Self::F20 => "F20",
  114. Self::F21 => "F21",
  115. Self::F22 => "F22",
  116. Self::F23 => "F23",
  117. Self::F24 => "F24",
  118. Self::F25 => "F25",
  119. Self::Kp0 => "Kp0",
  120. Self::Kp1 => "Kp1",
  121. Self::Kp2 => "Kp2",
  122. Self::Kp3 => "Kp3",
  123. Self::Kp4 => "Kp4",
  124. Self::Kp5 => "Kp5",
  125. Self::Kp6 => "Kp6",
  126. Self::Kp7 => "Kp7",
  127. Self::Kp8 => "Kp8",
  128. Self::Kp9 => "Kp9",
  129. Self::KpDecimal => "KpDecimal",
  130. Self::KpDivide => "KpDivide",
  131. Self::KpMultiply => "KpMultiply",
  132. Self::KpSubtract => "KpSubtract",
  133. Self::KpAdd => "KpAdd",
  134. Self::KpEnter => "KpEnter",
  135. Self::KpEqual => "KpEqual",
  136. Self::LeftShift => "LeftShift",
  137. Self::LeftControl => "LeftControl",
  138. Self::LeftAlt => "LeftAlt",
  139. Self::LeftSuper => "LeftSuper",
  140. Self::RightShift => "RightShift",
  141. Self::RightControl => "RightControl",
  142. Self::RightAlt => "RightAlt",
  143. Self::RightSuper => "RightSuper",
  144. Self::Menu => "Menu",
  145. Self::Unknown => "Unknown",
  146. }
  147. }
  148. }
  149. pub trait KeyCodeAsU16 {
  150. fn to_u16(&self) -> u16;
  151. fn from_u16(keysym: u16) -> Self;
  152. }
  153. impl KeyCodeAsU16 for KeyCode {
  154. fn to_u16(&self) -> u16 {
  155. match self {
  156. Self::Space => 0x0020,
  157. Self::Apostrophe => 0x0027,
  158. Self::Comma => 0x002c,
  159. Self::Minus => 0x002d,
  160. Self::Period => 0x002e,
  161. Self::Slash => 0x002f,
  162. Self::Key0 => 0x0030,
  163. Self::Key1 => 0x0031,
  164. Self::Key2 => 0x0032,
  165. Self::Key3 => 0x0033,
  166. Self::Key4 => 0x0034,
  167. Self::Key5 => 0x0035,
  168. Self::Key6 => 0x0036,
  169. Self::Key7 => 0x0037,
  170. Self::Key8 => 0x0038,
  171. Self::Key9 => 0x0039,
  172. Self::Semicolon => 0x003b,
  173. Self::Equal => 0x003d,
  174. Self::A => 0x0041,
  175. Self::B => 0x0042,
  176. Self::C => 0x0043,
  177. Self::D => 0x0044,
  178. Self::E => 0x0045,
  179. Self::F => 0x0046,
  180. Self::G => 0x0047,
  181. Self::H => 0x0048,
  182. Self::I => 0x0049,
  183. Self::J => 0x004a,
  184. Self::K => 0x004b,
  185. Self::L => 0x004c,
  186. Self::M => 0x004d,
  187. Self::N => 0x004e,
  188. Self::O => 0x004f,
  189. Self::P => 0x0050,
  190. Self::Q => 0x0051,
  191. Self::R => 0x0052,
  192. Self::S => 0x0053,
  193. Self::T => 0x0054,
  194. Self::U => 0x0055,
  195. Self::V => 0x0056,
  196. Self::W => 0x0057,
  197. Self::X => 0x0058,
  198. Self::Y => 0x0059,
  199. Self::Z => 0x005a,
  200. Self::LeftBracket => 0x005b,
  201. Self::Backslash => 0x005c,
  202. Self::RightBracket => 0x005d,
  203. Self::GraveAccent => 0x0060,
  204. Self::World1 => 0x0100,
  205. Self::World2 => 0x0101,
  206. Self::Escape => 0xff1b,
  207. Self::Enter => 0xff0d,
  208. Self::Tab => 0xff09,
  209. Self::Backspace => 0xff08,
  210. Self::Insert => 0xff63,
  211. Self::Delete => 0xffff,
  212. Self::Right => 0xff53,
  213. Self::Left => 0xff51,
  214. Self::Down => 0xff54,
  215. Self::Up => 0xff52,
  216. Self::PageUp => 0xff55,
  217. Self::PageDown => 0xff56,
  218. Self::Home => 0xff50,
  219. Self::End => 0xff57,
  220. Self::CapsLock => 0xffe5,
  221. Self::ScrollLock => 0xff14,
  222. Self::NumLock => 0xff7f,
  223. Self::PrintScreen => 0xfd1d,
  224. Self::Pause => 0xff13,
  225. Self::F1 => 0xffbe,
  226. Self::F2 => 0xffbf,
  227. Self::F3 => 0xffc0,
  228. Self::F4 => 0xffc1,
  229. Self::F5 => 0xffc2,
  230. Self::F6 => 0xffc3,
  231. Self::F7 => 0xffc4,
  232. Self::F8 => 0xffc5,
  233. Self::F9 => 0xffc6,
  234. Self::F10 => 0xffc7,
  235. Self::F11 => 0xffc8,
  236. Self::F12 => 0xffc9,
  237. Self::F13 => 0xffca,
  238. Self::F14 => 0xffcb,
  239. Self::F15 => 0xffcc,
  240. Self::F16 => 0xffcd,
  241. Self::F17 => 0xffce,
  242. Self::F18 => 0xffcf,
  243. Self::F19 => 0xffd0,
  244. Self::F20 => 0xffd1,
  245. Self::F21 => 0xffd2,
  246. Self::F22 => 0xffd3,
  247. Self::F23 => 0xffd4,
  248. Self::F24 => 0xffd5,
  249. Self::F25 => 0xffd6,
  250. Self::Kp0 => 0xffb0,
  251. Self::Kp1 => 0xffb1,
  252. Self::Kp2 => 0xffb2,
  253. Self::Kp3 => 0xffb3,
  254. Self::Kp4 => 0xffb4,
  255. Self::Kp5 => 0xffb5,
  256. Self::Kp6 => 0xffb6,
  257. Self::Kp7 => 0xffb7,
  258. Self::Kp8 => 0xffb8,
  259. Self::Kp9 => 0xffb9,
  260. Self::KpDecimal => 0xffae,
  261. Self::KpDivide => 0xffaf,
  262. Self::KpMultiply => 0xffaa,
  263. Self::KpSubtract => 0xffad,
  264. Self::KpAdd => 0xffab,
  265. Self::KpEnter => 0xff8d,
  266. Self::KpEqual => 0xffbd,
  267. Self::LeftShift => 0xffe1,
  268. Self::LeftControl => 0xffe3,
  269. Self::LeftAlt => 0xffe9,
  270. Self::LeftSuper => 0xffeb,
  271. Self::RightShift => 0xffe2,
  272. Self::RightControl => 0xffe4,
  273. Self::RightAlt => 0xffea,
  274. Self::RightSuper => 0xffec,
  275. Self::Menu => 0xff67,
  276. Self::Unknown => 0x01ff,
  277. }
  278. }
  279. fn from_u16(keysym: u16) -> Self {
  280. match keysym {
  281. 0x0020 => Self::Space,
  282. 0x0027 => Self::Apostrophe,
  283. 0x002c => Self::Comma,
  284. 0x002d => Self::Minus,
  285. 0x002e => Self::Period,
  286. 0x002f => Self::Slash,
  287. 0x0030 => Self::Key0,
  288. 0x0031 => Self::Key1,
  289. 0x0032 => Self::Key2,
  290. 0x0033 => Self::Key3,
  291. 0x0034 => Self::Key4,
  292. 0x0035 => Self::Key5,
  293. 0x0036 => Self::Key6,
  294. 0x0037 => Self::Key7,
  295. 0x0038 => Self::Key8,
  296. 0x0039 => Self::Key9,
  297. 0x003b => Self::Semicolon,
  298. 0x003d => Self::Equal,
  299. 0x0041 => Self::A,
  300. 0x0042 => Self::B,
  301. 0x0043 => Self::C,
  302. 0x0044 => Self::D,
  303. 0x0045 => Self::E,
  304. 0x0046 => Self::F,
  305. 0x0047 => Self::G,
  306. 0x0048 => Self::H,
  307. 0x0049 => Self::I,
  308. 0x004a => Self::J,
  309. 0x004b => Self::K,
  310. 0x004c => Self::L,
  311. 0x004d => Self::M,
  312. 0x004e => Self::N,
  313. 0x004f => Self::O,
  314. 0x0050 => Self::P,
  315. 0x0051 => Self::Q,
  316. 0x0052 => Self::R,
  317. 0x0053 => Self::S,
  318. 0x0054 => Self::T,
  319. 0x0055 => Self::U,
  320. 0x0056 => Self::V,
  321. 0x0057 => Self::W,
  322. 0x0058 => Self::X,
  323. 0x0059 => Self::Y,
  324. 0x005a => Self::Z,
  325. 0x005b => Self::LeftBracket,
  326. 0x005c => Self::Backslash,
  327. 0x005d => Self::RightBracket,
  328. 0x0060 => Self::GraveAccent,
  329. 0x0100 => Self::World1,
  330. 0x0101 => Self::World2,
  331. 0xff1b => Self::Escape,
  332. 0xff0d => Self::Enter,
  333. 0xff09 => Self::Tab,
  334. 0xff08 => Self::Backspace,
  335. 0xff63 => Self::Insert,
  336. 0xffff => Self::Delete,
  337. 0xff53 => Self::Right,
  338. 0xff51 => Self::Left,
  339. 0xff54 => Self::Down,
  340. 0xff52 => Self::Up,
  341. 0xff55 => Self::PageUp,
  342. 0xff56 => Self::PageDown,
  343. 0xff50 => Self::Home,
  344. 0xff57 => Self::End,
  345. 0xffe5 => Self::CapsLock,
  346. 0xff14 => Self::ScrollLock,
  347. 0xff7f => Self::NumLock,
  348. 0xfd1d => Self::PrintScreen,
  349. 0xff13 => Self::Pause,
  350. 0xffbe => Self::F1,
  351. 0xffbf => Self::F2,
  352. 0xffc0 => Self::F3,
  353. 0xffc1 => Self::F4,
  354. 0xffc2 => Self::F5,
  355. 0xffc3 => Self::F6,
  356. 0xffc4 => Self::F7,
  357. 0xffc5 => Self::F8,
  358. 0xffc6 => Self::F9,
  359. 0xffc7 => Self::F10,
  360. 0xffc8 => Self::F11,
  361. 0xffc9 => Self::F12,
  362. 0xffca => Self::F13,
  363. 0xffcb => Self::F14,
  364. 0xffcc => Self::F15,
  365. 0xffcd => Self::F16,
  366. 0xffce => Self::F17,
  367. 0xffcf => Self::F18,
  368. 0xffd0 => Self::F19,
  369. 0xffd1 => Self::F20,
  370. 0xffd2 => Self::F21,
  371. 0xffd3 => Self::F22,
  372. 0xffd4 => Self::F23,
  373. 0xffd5 => Self::F24,
  374. 0xffd6 => Self::F25,
  375. 0xffb0 => Self::Kp0,
  376. 0xffb1 => Self::Kp1,
  377. 0xffb2 => Self::Kp2,
  378. 0xffb3 => Self::Kp3,
  379. 0xffb4 => Self::Kp4,
  380. 0xffb5 => Self::Kp5,
  381. 0xffb6 => Self::Kp6,
  382. 0xffb7 => Self::Kp7,
  383. 0xffb8 => Self::Kp8,
  384. 0xffb9 => Self::Kp9,
  385. 0xffae => Self::KpDecimal,
  386. 0xffaf => Self::KpDivide,
  387. 0xffaa => Self::KpMultiply,
  388. 0xffad => Self::KpSubtract,
  389. 0xffab => Self::KpAdd,
  390. 0xff8d => Self::KpEnter,
  391. 0xffbd => Self::KpEqual,
  392. 0xffe1 => Self::LeftShift,
  393. 0xffe3 => Self::LeftControl,
  394. 0xffe9 => Self::LeftAlt,
  395. 0xffeb => Self::LeftSuper,
  396. 0xffe2 => Self::RightShift,
  397. 0xffe4 => Self::RightControl,
  398. 0xffea => Self::RightAlt,
  399. 0xffec => Self::RightSuper,
  400. 0xff67 => Self::Menu,
  401. _ => Self::Unknown,
  402. }
  403. }
  404. }
  405. pub trait MouseButtonAsU8 {
  406. fn to_u8(&self) -> u8;
  407. fn from_u8(btn: u8) -> Self;
  408. }
  409. impl MouseButtonAsU8 for MouseButton {
  410. fn to_u8(&self) -> u8 {
  411. match self {
  412. Self::Left => 0,
  413. Self::Middle => 1,
  414. Self::Right => 2,
  415. Self::Unknown => 3,
  416. }
  417. }
  418. fn from_u8(btn: u8) -> Self {
  419. match btn {
  420. 0 => Self::Left,
  421. 1 => Self::Middle,
  422. 2 => Self::Right,
  423. _ => Self::Unknown,
  424. }
  425. }
  426. }