nsp_keys.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Keys module
  2. // Copyright (C) 2015 Legimet
  3. //
  4. // This file is part of Duktape-nspire.
  5. //
  6. // Duktape-nspire is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Lesser General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // Duktape-nspire is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Lesser General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Lesser General Public License
  17. // along with Duktape-nspire. If not, see <http://www.gnu.org/licenses/>.
  18. #include "duktape.h"
  19. #include <libndls.h>
  20. struct nsp_key {
  21. const char *key_name;
  22. const t_key *key_val;
  23. };
  24. #define KEY(a) {#a, &a}
  25. static const struct nsp_key nsp_key_consts[] = {
  26. KEY(KEY_NSPIRE_RET),
  27. KEY(KEY_NSPIRE_ENTER),
  28. KEY(KEY_NSPIRE_SPACE),
  29. KEY(KEY_NSPIRE_NEGATIVE),
  30. KEY(KEY_NSPIRE_Z),
  31. KEY(KEY_NSPIRE_PERIOD),
  32. KEY(KEY_NSPIRE_Y),
  33. KEY(KEY_NSPIRE_0),
  34. KEY(KEY_NSPIRE_X),
  35. KEY(KEY_NSPIRE_THETA),
  36. KEY(KEY_NSPIRE_COMMA),
  37. KEY(KEY_NSPIRE_PLUS),
  38. KEY(KEY_NSPIRE_W),
  39. KEY(KEY_NSPIRE_3),
  40. KEY(KEY_NSPIRE_V),
  41. KEY(KEY_NSPIRE_2),
  42. KEY(KEY_NSPIRE_U),
  43. KEY(KEY_NSPIRE_1),
  44. KEY(KEY_NSPIRE_T),
  45. KEY(KEY_NSPIRE_eEXP),
  46. KEY(KEY_NSPIRE_PI),
  47. KEY(KEY_NSPIRE_QUES),
  48. KEY(KEY_NSPIRE_QUESEXCL),
  49. KEY(KEY_NSPIRE_MINUS),
  50. KEY(KEY_NSPIRE_S),
  51. KEY(KEY_NSPIRE_6),
  52. KEY(KEY_NSPIRE_R),
  53. KEY(KEY_NSPIRE_5),
  54. KEY(KEY_NSPIRE_Q),
  55. KEY(KEY_NSPIRE_4),
  56. KEY(KEY_NSPIRE_P),
  57. KEY(KEY_NSPIRE_TENX),
  58. KEY(KEY_NSPIRE_EE),
  59. KEY(KEY_NSPIRE_COLON),
  60. KEY(KEY_NSPIRE_MULTIPLY),
  61. KEY(KEY_NSPIRE_O),
  62. KEY(KEY_NSPIRE_9),
  63. KEY(KEY_NSPIRE_N),
  64. KEY(KEY_NSPIRE_8),
  65. KEY(KEY_NSPIRE_M),
  66. KEY(KEY_NSPIRE_7),
  67. KEY(KEY_NSPIRE_L),
  68. KEY(KEY_NSPIRE_SQU),
  69. KEY(KEY_NSPIRE_II),
  70. KEY(KEY_NSPIRE_QUOTE),
  71. KEY(KEY_NSPIRE_DIVIDE),
  72. KEY(KEY_NSPIRE_K),
  73. KEY(KEY_NSPIRE_TAN),
  74. KEY(KEY_NSPIRE_J),
  75. KEY(KEY_NSPIRE_COS),
  76. KEY(KEY_NSPIRE_I),
  77. KEY(KEY_NSPIRE_SIN),
  78. KEY(KEY_NSPIRE_H),
  79. KEY(KEY_NSPIRE_EXP),
  80. KEY(KEY_NSPIRE_GTHAN),
  81. KEY(KEY_NSPIRE_APOSTROPHE),
  82. KEY(KEY_NSPIRE_CAT),
  83. KEY(KEY_NSPIRE_FRAC),
  84. KEY(KEY_NSPIRE_G),
  85. KEY(KEY_NSPIRE_RP),
  86. KEY(KEY_NSPIRE_F),
  87. KEY(KEY_NSPIRE_LP),
  88. KEY(KEY_NSPIRE_E),
  89. KEY(KEY_NSPIRE_VAR),
  90. KEY(KEY_NSPIRE_D),
  91. KEY(KEY_NSPIRE_DEL),
  92. KEY(KEY_NSPIRE_LTHAN),
  93. KEY(KEY_NSPIRE_FLAG),
  94. KEY(KEY_NSPIRE_CLICK),
  95. KEY(KEY_NSPIRE_C),
  96. KEY(KEY_NSPIRE_HOME),
  97. KEY(KEY_NSPIRE_B),
  98. KEY(KEY_NSPIRE_MENU),
  99. KEY(KEY_NSPIRE_A),
  100. KEY(KEY_NSPIRE_ESC),
  101. KEY(KEY_NSPIRE_BAR),
  102. KEY(KEY_NSPIRE_TAB),
  103. KEY(KEY_NSPIRE_EQU),
  104. KEY(KEY_NSPIRE_UP),
  105. KEY(KEY_NSPIRE_UPRIGHT),
  106. KEY(KEY_NSPIRE_RIGHT),
  107. KEY(KEY_NSPIRE_RIGHTDOWN),
  108. KEY(KEY_NSPIRE_DOWN),
  109. KEY(KEY_NSPIRE_DOWNLEFT),
  110. KEY(KEY_NSPIRE_LEFT),
  111. KEY(KEY_NSPIRE_LEFTUP),
  112. KEY(KEY_NSPIRE_SHIFT),
  113. KEY(KEY_NSPIRE_CTRL),
  114. KEY(KEY_NSPIRE_DOC),
  115. KEY(KEY_NSPIRE_TRIG),
  116. KEY(KEY_NSPIRE_SCRATCHPAD),
  117. KEY(KEY_84_DOWN),
  118. KEY(KEY_84_LEFT),
  119. KEY(KEY_84_RIGHT),
  120. KEY(KEY_84_UP),
  121. KEY(KEY_84_ENTER),
  122. KEY(KEY_84_PLUS),
  123. KEY(KEY_84_MINUS),
  124. KEY(KEY_84_MULTIPLY),
  125. KEY(KEY_84_DIVIDE),
  126. KEY(KEY_84_EXP),
  127. KEY(KEY_84_CLEAR),
  128. KEY(KEY_84_NEGATIVE),
  129. KEY(KEY_84_3),
  130. KEY(KEY_84_6),
  131. KEY(KEY_84_9),
  132. KEY(KEY_84_RP),
  133. KEY(KEY_84_TAN),
  134. KEY(KEY_84_VARS),
  135. KEY(KEY_84_PERIOD),
  136. KEY(KEY_84_2),
  137. KEY(KEY_84_5),
  138. KEY(KEY_84_8),
  139. KEY(KEY_84_LP),
  140. KEY(KEY_84_COS),
  141. KEY(KEY_84_PRGM),
  142. KEY(KEY_84_STAT),
  143. KEY(KEY_84_0),
  144. KEY(KEY_84_1),
  145. KEY(KEY_84_4),
  146. KEY(KEY_84_7),
  147. KEY(KEY_84_COMMA),
  148. KEY(KEY_84_SIN),
  149. KEY(KEY_84_APPS),
  150. KEY(KEY_84_X),
  151. KEY(KEY_84_STO),
  152. KEY(KEY_84_LN),
  153. KEY(KEY_84_LOG),
  154. KEY(KEY_84_SQU),
  155. KEY(KEY_84_INV),
  156. KEY(KEY_84_MATH),
  157. KEY(KEY_84_ALPHA),
  158. KEY(KEY_84_GRAPH),
  159. KEY(KEY_84_TRACE),
  160. KEY(KEY_84_ZOOM),
  161. KEY(KEY_84_WIND),
  162. KEY(KEY_84_YEQU),
  163. KEY(KEY_84_2ND),
  164. KEY(KEY_84_MODE),
  165. KEY(KEY_84_DEL)
  166. };
  167. static duk_ret_t nsp_keys_any_key_pressed(__attribute__((unused)) duk_context *ctx) {
  168. duk_push_boolean(ctx, any_key_pressed());
  169. return 1;
  170. }
  171. static duk_ret_t nsp_keys_is_key_pressed(duk_context *ctx) {
  172. int k = duk_require_int(ctx, 0);
  173. if (k < 0 || (unsigned int)k >= sizeof(nsp_key_consts) / sizeof(struct nsp_key)) {
  174. duk_push_error_object(ctx, DUK_ERR_ERROR, "invalid key: %d", k);
  175. duk_throw(ctx);
  176. }
  177. duk_push_boolean(ctx, isKeyPressed(*(nsp_key_consts[k].key_val)));
  178. return 1;
  179. }
  180. static duk_ret_t nsp_keys_on_key_pressed(__attribute__((unused)) duk_context *ctx) {
  181. duk_push_boolean(ctx, on_key_pressed());
  182. return 1;
  183. }
  184. static duk_ret_t nsp_keys_wait_key_pressed(__attribute__((unused)) duk_context *ctx) {
  185. wait_key_pressed();
  186. return 0;
  187. }
  188. static duk_ret_t nsp_keys_wait_no_key_pressed(__attribute__((unused)) duk_context *ctx) {
  189. wait_no_key_pressed();
  190. return 0;
  191. }
  192. static const duk_function_list_entry nsp_keys_funcs[] = {
  193. {"anyKeyPressed", nsp_keys_any_key_pressed, 0},
  194. {"isKeyPressed", nsp_keys_is_key_pressed, 1},
  195. {"onKeyPressed", nsp_keys_on_key_pressed, 0},
  196. {"waitKeyPressed", nsp_keys_wait_key_pressed, 0},
  197. {"waitNoKeyPressed", nsp_keys_wait_no_key_pressed, 0},
  198. {NULL, NULL, 0 }
  199. };
  200. duk_ret_t dukopen_nsp_keys(duk_context *ctx) {
  201. duk_idx_t idx = duk_push_object(ctx);
  202. duk_put_function_list(ctx, idx, nsp_keys_funcs);
  203. for (unsigned int i = 0; i < sizeof(nsp_key_consts) / sizeof(struct nsp_key); i++) {
  204. duk_push_int(ctx, i);
  205. duk_put_prop_string(ctx, idx, nsp_key_consts[i].key_name);
  206. }
  207. return 1;
  208. }