rpc_keyboard.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #pragma once
  2. #include <core/common_defines.h>
  3. #include <core/mutex.h>
  4. #include <core/pubsub.h>
  5. #define RECORD_RPC_KEYBOARD "rpckeyboard"
  6. #define RPC_KEYBOARD_KEY_RIGHT '\x13'
  7. #define RPC_KEYBOARD_KEY_LEFT '\x14'
  8. #define RPC_KEYBOARD_KEY_ENTER '\x0D'
  9. #define RPC_KEYBOARD_KEY_BACKSPACE '\x08'
  10. typedef enum
  11. {
  12. // Unknown error occurred
  13. RpcKeyboardChatpadStatusError,
  14. // The chatpad worker is stopped
  15. RpcKeyboardChatpadStatusStopped,
  16. // The chatpad worker is started, but not ready
  17. RpcKeyboardChatpadStatusStarted,
  18. // The chatpad worker is ready and got response from chatpad
  19. RpcKeyboardChatpadStatusReady,
  20. } RpcKeyboardChatpadStatus;
  21. typedef struct RpcKeyboard RpcKeyboard;
  22. typedef enum
  23. {
  24. // Replacement text was provided by the user
  25. RpcKeyboardEventTypeTextEntered,
  26. // A single character was provided by the user
  27. RpcKeyboardEventTypeCharEntered,
  28. // A macro was entered by the user
  29. RpcKeyboardEventTypeMacroEntered,
  30. } RpcKeyboardEventType;
  31. typedef struct
  32. {
  33. // The mutex to protect the data, call furi_mutex_acquire/furi_mutex_release.
  34. FuriMutex *mutex;
  35. // The text message, macro or character.
  36. char message[256];
  37. // The length of the message.
  38. uint16_t length;
  39. // The newline enabled flag, allow newline to submit text.
  40. bool newline_enabled;
  41. } RpcKeyboardEventData;
  42. typedef struct
  43. {
  44. RpcKeyboardEventType type;
  45. RpcKeyboardEventData data;
  46. } RpcKeyboardEvent;
  47. typedef FuriPubSub *(*RpcKeyboardGetPubsub)(RpcKeyboard *rpc_keyboard);
  48. typedef void (*RpcKeyboardNewlineEnable)(RpcKeyboard *rpc_keyboard, bool enable);
  49. typedef void (*RpcKeyboardPublishCharFn)(RpcKeyboard *keyboard, char character);
  50. typedef void (*RpcKeyboardPublishMacroFn)(RpcKeyboard *rpc_keyboard, char macro);
  51. typedef char *(*RpcKeyboardGetMacroFn)(RpcKeyboard *rpc_keyboard, char macro);
  52. typedef void (*RpcKeyboardSetMacroFn)(RpcKeyboard *rpc_keyboard, char macro, char *value);
  53. typedef void (*RpcKeyboardChatpadStartFn)(RpcKeyboard *rpc_keyboard);
  54. typedef void (*RpcKeyboardChatpadStopFn)(RpcKeyboard *rpc_keyboard);
  55. typedef RpcKeyboardChatpadStatus (*RpcKeyboardChatpadStatusFn)(RpcKeyboard *rpc_keyboard);
  56. typedef struct RpcKeyboardFunctions RpcKeyboardFunctions;
  57. struct RpcKeyboardFunctions
  58. {
  59. uint16_t major;
  60. uint16_t minor;
  61. RpcKeyboardGetPubsub fn_get_pubsub;
  62. RpcKeyboardNewlineEnable fn_newline_enable;
  63. RpcKeyboardPublishCharFn fn_publish_char;
  64. RpcKeyboardPublishMacroFn fn_publish_macro;
  65. RpcKeyboardGetMacroFn fn_get_macro;
  66. RpcKeyboardSetMacroFn fn_set_macro;
  67. RpcKeyboardChatpadStartFn fn_chatpad_start;
  68. RpcKeyboardChatpadStopFn fn_chatpad_stop;
  69. RpcKeyboardChatpadStatusFn fn_chatpad_status;
  70. };
  71. /**
  72. * @brief STARTUP - Register the remote keyboard.
  73. */
  74. void rpc_keyboard_register(void);
  75. /**
  76. * @brief UNUSED - Unregister the remote keyboard.
  77. */
  78. void rpc_keyboard_release(void);
  79. /**
  80. * @brief Get the pubsub object for the remote keyboard.
  81. * @details This function returns the pubsub object, use to subscribe to keyboard events.
  82. * @param[in] rpc_keyboard pointer to the RECORD_RPC_KEYBOARD.
  83. * @return FuriPubSub* pointer to the pubsub object.
  84. */
  85. FuriPubSub *rpc_keyboard_get_pubsub(RpcKeyboard *rpc_keyboard);
  86. /**
  87. * @brief Enable or disable newline character submitting the text.
  88. * @param[in] rpc_keyboard pointer to the RECORD_RPC_KEYBOARD.
  89. * @param[in] enable true to enable, false to disable.
  90. */
  91. void rpc_keyboard_newline_enable(RpcKeyboard *rpc_keyboard, bool enable);
  92. /**
  93. * @brief Publish the replacement text to the remote keyboard.
  94. * @param[in] rpc_keyboard pointer to the RECORD_RPC_KEYBOARD.
  95. * @param[in] bytes pointer to the text buffer.
  96. * @param[in] buffer_size size of the text buffer.
  97. */
  98. void rpc_keyboard_publish_text(RpcKeyboard *rpc_keyboard, uint8_t *bytes, uint32_t buffer_size);
  99. /**
  100. * @brief Publish a single key pressed on the remote keyboard.
  101. * @param[in] rpc_keyboard pointer to the RECORD_RPC_KEYBOARD.
  102. * @param[in] character the character that was pressed.
  103. */
  104. void rpc_keyboard_publish_char(RpcKeyboard *rpc_keyboard, char character);
  105. /**
  106. * @brief Publish a macro key pressed on the remote keyboard.
  107. * @param[in] rpc_keyboard pointer to the RECORD_RPC_KEYBOARD.
  108. * @param[in] character the macro key that was pressed.
  109. */
  110. void rpc_keyboard_publish_macro(RpcKeyboard *rpc_keyboard, char macro);
  111. /**
  112. * @brief Get the macro text associated with a macro key.
  113. * @param[in] rpc_keyboard pointer to the RECORD_RPC_KEYBOARD.
  114. * @param[in] macro the macro key.
  115. * @return char* pointer to the macro text. NULL if the macro key is not set. User must free the memory.
  116. */
  117. char *rpc_keyboard_get_macro(RpcKeyboard *rpc_keyboard, char macro);
  118. /**
  119. * @brief Set the macro text associated with a macro key.
  120. * @param[in] rpc_keyboard pointer to the RECORD_RPC_KEYBOARD.
  121. * @param[in] macro the macro key.
  122. * @param[in] value the macro text.
  123. */
  124. void rpc_keyboard_set_macro(RpcKeyboard *rpc_keyboard, char macro, char *value);
  125. /**
  126. * @brief Initializes the chatpad and starts listening for keypresses.
  127. * @param[in] rpc_keyboard pointer to the RECORD_RPC_KEYBOARD.
  128. */
  129. void rpc_keyboard_chatpad_start(RpcKeyboard *rpc_keyboard);
  130. /**
  131. * @brief Stops the chatpad & frees resources.
  132. * @param[in] rpc_keyboard pointer to the RECORD_RPC_KEYBOARD.
  133. */
  134. void rpc_keyboard_chatpad_stop(RpcKeyboard *rpc_keyboard);
  135. /**
  136. * @brief Get the status of the chatpad.
  137. * @param[in] rpc_keyboard pointer to the RECORD_RPC_KEYBOARD.
  138. * @return RpcKeyboardChatpadStatus the status of the chatpad.
  139. */
  140. RpcKeyboardChatpadStatus rpc_keyboard_chatpad_status(RpcKeyboard *rpc_keyboard);