input.lisp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. ;;;; Copyright © 2023, Jaidyn Ann <jadedctrl@posteo.at>
  2. ;;;;
  3. ;;;; This program is free software: you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU General Public License as
  5. ;;;; published by the Free Software Foundation, either version 3 of
  6. ;;;; the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This program is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;;;; GNU General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU General Public License
  14. ;;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ;;;; FLORA-SEARCH-AURORA.INPUT ⌨
  16. ;;;; All input-related voodoo goes here: Input reading, translating, parsing, etc.
  17. (in-package :flora-search-aurora.input)
  18. ;; Yup, they're hardcoded like this. Horrid, ain't it? ^_^
  19. (defvar +qwerty-layout+
  20. '(#\q #\Q #\w #\W #\e #\E #\r #\R #\t #\T #\y #\Y #\u #\U #\i #\I #\o #\O #\p #\P #\[ #\{ #\] #\}
  21. #\a #\A #\s #\S #\d #\D #\f #\F #\g #\G #\h #\H #\j #\J #\k #\K #\l #\L #\; #\: #\' #\"
  22. #\z #\Z #\x #\X #\c #\C #\v #\V #\b #\B #\n #\N #\m #\M #\, #\< #\. #\> #\/ #\?))
  23. (defvar +dvorak-layout+
  24. '(#\' #\" #\, #\< #\. #\> #\p #\P #\y #\Y #\f #\F #\g #\G #\c #\C #\r #\R #\l #\L #\/ #\? #\= #\+
  25. #\a #\A #\o #\O #\e #\E #\u #\U #\i #\I #\d #\D #\h #\H #\t #\T #\n #\N #\s #\S #\- #\_
  26. #\; #\: #\q #\Q #\j #\J #\k #\K #\x #\X #\b #\B #\m #\M #\w #\W #\v #\V #\z #\Z))
  27. (defvar +numpad-game-layout+
  28. '((#\8 . ↑)(#\4 . ←)(#\2 . ↓)(#\6 . →)(#\7 . ↰)(#\9 . ↱)(#\1 . ↲)(#\3 . ↳)
  29. (#\5 . 🆗)(#\return . 🆗)
  30. (#\+ . ❎)(#\0 . ❎)))
  31. (defvar +arrows-game-layout+
  32. (append '((#\↑ . ↑)(#\← . ←)(#\↓ . ↓)(#\→ . →)
  33. (#\a . 🆗)(#\z . 🆗)(#\space . 🆗)(#\return . 🆗)
  34. (#\s . ❎)(#\x . ❎)(#\Esc . ❎))
  35. +numpad-game-layout+))
  36. (defvar +wasd-game-layout+
  37. (append '((#\w . ↑)(#\a . ←)(#\s . ↓)(#\d . →)
  38. (#\j . 🆗)(#\← . 🆗)(#\space . 🆗)(#\return . 🆗)
  39. (#\k . ❎)(#\↓ . ❎)(#\Esc . ❎))
  40. +numpad-game-layout+))
  41. (defvar +ijkl-game-layout+
  42. (append '((#\i . ↑)(#\j . ←)(#\k . ↓)(#\l . →)
  43. (#\a . 🆗)(#\z . 🆗)(#\← . 🆗)(#\space . 🆗)(#\return . 🆗)
  44. (#\s . ❎)(#\x . ❎)(#\↓ . ❎)(#\Esc . ❎))
  45. +numpad-game-layout+))
  46. (defparameter *keyboard* +qwerty-layout+)
  47. (defparameter *controls* +arrows-game-layout+)
  48. (defun read-char-plist (&optional (stream *standard-input*))
  49. "Reads a character directly from standard-input (sans buffering).
  50. Simple terminal escape codes (like arrow-keys) are translated into
  51. roughly-semantically-equal character representations — see the
  52. docstring of #'escape-code-to-character for more info."
  53. (let* ((char-1 (read-char stream))
  54. (char-2 (if (eq char-1 #\ESC) (read-char-no-hang stream))) ; Maybe escaped char or [.
  55. (char-3 (if (eq char-2 #\[) (read-char-no-hang stream))) ; Maybe end-of-sequence, or 1.
  56. (char-4 (if (eq char-3 #\1) (read-char-no-hang stream))) ; Maybe semicolon.
  57. (char-5 (if (eq char-4 #\;) (read-char-no-hang stream))) ; Maybe modifer-key.
  58. (char-6 (if (characterp char-5) (read-char-no-hang stream)))) ; Escaped char and EOS.
  59. ;; Let me explain! There are pretty much three input-cases we should care about:
  60. ;; * character
  61. ;; * ␛ character
  62. ;; * ␛ [ character
  63. ;; * ␛ [ 1 ; modifier character
  64. ;; This is by no means comprehensive, sorry: I didn't even try! But it suits my purposes. :-P
  65. (let ((the-char (or char-6 char-3 char-2 char-1))
  66. (modifier (case char-5
  67. (#\2 'shift)
  68. (#\3 'meta)
  69. (#\5 'control)))
  70. (escaped (eq char-1 #\ESC)))
  71. (list :char the-char :modifier modifier :escaped escaped))))
  72. (defun normalize-char-plist (char-plist &optional (layout *keyboard*))
  73. "Given a character input property list (as received from READ-CHAR-PLIST),
  74. massage the output into parsable, deescaped, QWERTY-according format."
  75. (let ((normalized (deescape-char-plist char-plist)))
  76. (setf (getf normalized :char)
  77. (qwertyize-char (getf normalized :char)
  78. layout))
  79. normalized))
  80. (defun qwertyize-char (char layout)
  81. "Given a char input in some layout, return the corresponding character in QWERTY.
  82. Not at all comprehensive, but probably-mostly-just-good-enough. ¯\_ (ツ)_/¯"
  83. (or (parallel-list-item char layout +qwerty-layout+)
  84. char))
  85. (defun read-gamefied-char-plist
  86. (&optional (stream *standard-input*) (layout *keyboard*) (game-layout *controls*))
  87. "Read a character directly from standard-input, then translating its character into the QWERTY
  88. equivalent, then parsing that character into semantic meaning. Results in a plist like so:
  89. (:char #\w :semantic ↑ :modifier nil :escaped nil)"
  90. (gameify-char-plist (normalize-char-plist (read-char-plist stream) layout)
  91. game-layout))
  92. (defun gameify-char-plist (char-plist &optional (game-layout *keyboard*))
  93. "Given a character input plist (as received by READ-CHAR-PLIST), return a
  94. char plist containing a :FUNCTION property, which contains one of several
  95. semantic symbols that match up for menus/gameplay: ↑, →, ←, ↓, 🆗, or ❎."
  96. (let ((semantic-value (cdr (assoc (getf char-plist :char) game-layout))))
  97. (if semantic-value
  98. (append (list :semantic semantic-value) char-plist)
  99. char-plist)))
  100. (defun deescape-char-plist (char-plist)
  101. "Translate escaped characters into somewhat-semantically-adjacent
  102. characters, like left arrow-key (escaped D) into ← (“LEFTWARDS ARROW”)."
  103. (list :modifier (getf char-plist :modifier)
  104. :char (if (getf char-plist :escaped)
  105. (case (getf char-plist :char)
  106. (#\A #\↑)
  107. (#\B #\↓)
  108. (#\C #\→)
  109. (#\D #\←)
  110. (otherwise (getf char-plist :char)))
  111. (getf char-plist :char))))
  112. ;;; ———————————————————————————————————
  113. ;;; Misc. utils
  114. ;;; ———————————————————————————————————
  115. (defun parallel-list-item (item-a list-a list-b &key (test #'eql))
  116. "Given two parallel lists and an item contained in the first list, return its
  117. corresponding item in the other list, by index."
  118. (let ((index (position item-a list-a :test test)))
  119. (if index
  120. (nth index list-b))))
  121. (defun pressed-enter-p ()
  122. "Whether or not the enter/return key has been pressed recently.
  123. Man, today’s a good day. Well, it wasn’t great, too be honest. Kind of bad,
  124. I slightly humiliated myself a tiny bit. But wow, I’m having such nice tea!
  125. Programming with nice tea! What a nice day this is. If you happen to be
  126. reading this, I hope your day is going well too!
  127. If not, have some tea on me: I’m paying. =w="
  128. (and (listen)
  129. (let ((input (getf (⌨:read-gamefied-char-plist) :semantic)))
  130. (or (eq input '⌨:🆗)
  131. (eq input '⌨:❎)))))