embedded_debugger.mm 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**************************************************************************/
  2. /* embedded_debugger.mm */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "embedded_debugger.h"
  31. #include "display_server_embedded.h"
  32. #include "core/debugger/engine_debugger.h"
  33. #include "core/input/input_event_codec.h"
  34. #ifdef DEBUG_ENABLED
  35. HashMap<String, EmbeddedDebugger::ParseMessageFunc> EmbeddedDebugger::parse_message_handlers;
  36. #endif
  37. EmbeddedDebugger::EmbeddedDebugger(DisplayServerEmbedded *p_ds) {
  38. singleton = this;
  39. #ifdef DEBUG_ENABLED
  40. ds = p_ds;
  41. if (parse_message_handlers.is_empty()) {
  42. _init_parse_message_handlers();
  43. }
  44. EngineDebugger::register_message_capture("embed", EngineDebugger::Capture(this, EmbeddedDebugger::parse_message));
  45. #endif
  46. }
  47. EmbeddedDebugger::~EmbeddedDebugger() {
  48. singleton = nullptr;
  49. }
  50. void EmbeddedDebugger::initialize(DisplayServerEmbedded *p_ds) {
  51. if (EngineDebugger::is_active()) {
  52. memnew(EmbeddedDebugger(p_ds));
  53. }
  54. }
  55. void EmbeddedDebugger::deinitialize() {
  56. if (singleton) {
  57. memdelete(singleton);
  58. }
  59. }
  60. #ifdef DEBUG_ENABLED
  61. void EmbeddedDebugger::_init_parse_message_handlers() {
  62. parse_message_handlers["window_size"] = &EmbeddedDebugger::_msg_window_size;
  63. parse_message_handlers["mouse_set_mode"] = &EmbeddedDebugger::_msg_mouse_set_mode;
  64. parse_message_handlers["event"] = &EmbeddedDebugger::_msg_event;
  65. parse_message_handlers["win_event"] = &EmbeddedDebugger::_msg_win_event;
  66. parse_message_handlers["notification"] = &EmbeddedDebugger::_msg_notification;
  67. parse_message_handlers["ime_update"] = &EmbeddedDebugger::_msg_ime_update;
  68. parse_message_handlers["joy_add"] = &EmbeddedDebugger::_msg_joy_add;
  69. parse_message_handlers["joy_del"] = &EmbeddedDebugger::_msg_joy_del;
  70. parse_message_handlers["ds_state"] = &EmbeddedDebugger::_msg_ds_state;
  71. }
  72. Error EmbeddedDebugger::_msg_window_size(const Array &p_args) {
  73. ERR_FAIL_COND_V_MSG(p_args.size() != 1, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'window_size' message.");
  74. Size2i size = p_args[0];
  75. ds->window_set_size(size);
  76. return OK;
  77. }
  78. Error EmbeddedDebugger::_msg_mouse_set_mode(const Array &p_args) {
  79. ERR_FAIL_COND_V_MSG(p_args.size() != 1, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'mouse_set_mode' message.");
  80. DisplayServer::MouseMode mode = p_args[0];
  81. ds->mouse_set_mode(mode);
  82. return OK;
  83. }
  84. Error EmbeddedDebugger::_msg_event(const Array &p_args) {
  85. ERR_FAIL_COND_V_MSG(p_args.size() != 1, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'event' message.");
  86. Input *input = Input::get_singleton();
  87. if (!input) {
  88. // Ignore if we've received an event before the process has initialized.
  89. return OK;
  90. }
  91. PackedByteArray data = p_args[0];
  92. Ref<InputEvent> event;
  93. decode_input_event(data, event);
  94. {
  95. Ref<InputEventMouse> e = event;
  96. if (e.is_valid()) {
  97. input->set_mouse_position(e->get_position());
  98. }
  99. }
  100. {
  101. Ref<InputEventMagnifyGesture> e = event;
  102. if (e.is_valid()) {
  103. input->set_mouse_position(e->get_position());
  104. }
  105. }
  106. {
  107. Ref<InputEventPanGesture> e = event;
  108. if (e.is_valid()) {
  109. input->set_mouse_position(e->get_position());
  110. }
  111. }
  112. if (event.is_valid()) {
  113. input->parse_input_event(event);
  114. }
  115. return OK;
  116. }
  117. Error EmbeddedDebugger::_msg_win_event(const Array &p_args) {
  118. ERR_FAIL_COND_V_MSG(p_args.size() != 1, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'win_event' message.");
  119. DisplayServer::WindowEvent win_event = p_args[0];
  120. ds->send_window_event(win_event, DisplayServer::MAIN_WINDOW_ID);
  121. if (win_event == DisplayServer::WindowEvent::WINDOW_EVENT_MOUSE_EXIT) {
  122. Input::get_singleton()->release_pressed_events();
  123. }
  124. return OK;
  125. }
  126. Error EmbeddedDebugger::_msg_ime_update(const Array &p_args) {
  127. ERR_FAIL_COND_V_MSG(p_args.size() != 2, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'ime_update' message.");
  128. String ime_text = p_args[0];
  129. Vector2i ime_selection = p_args[1];
  130. ds->update_im_text(ime_selection, ime_text);
  131. return OK;
  132. }
  133. Error EmbeddedDebugger::_msg_notification(const Array &p_args) {
  134. ERR_FAIL_COND_V_MSG(p_args.size() != 1, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'notification' message.");
  135. int notification = p_args[0];
  136. if (OS::get_singleton()->get_main_loop()) {
  137. OS::get_singleton()->get_main_loop()->notification(notification);
  138. }
  139. return OK;
  140. }
  141. Error EmbeddedDebugger::_msg_joy_add(const Array &p_args) {
  142. ERR_FAIL_COND_V_MSG(p_args.size() != 2, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'joy_add' message.");
  143. int idx = p_args[0];
  144. String name = p_args[1];
  145. ds->joy_add(idx, name);
  146. return OK;
  147. }
  148. Error EmbeddedDebugger::_msg_joy_del(const Array &p_args) {
  149. ERR_FAIL_COND_V_MSG(p_args.size() != 1, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'joy_del' message.");
  150. int idx = p_args[0];
  151. ds->joy_del(idx);
  152. return OK;
  153. }
  154. Error EmbeddedDebugger::_msg_ds_state(const Array &p_args) {
  155. ERR_FAIL_COND_V_MSG(p_args.size() != 1, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'ds_state' message.");
  156. PackedByteArray data = p_args[0];
  157. DisplayServerEmbeddedState state;
  158. state.deserialize(data);
  159. ds->set_state(state);
  160. return OK;
  161. }
  162. Error EmbeddedDebugger::parse_message(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured) {
  163. EmbeddedDebugger *self = static_cast<EmbeddedDebugger *>(p_user);
  164. r_captured = true;
  165. ParseMessageFunc *fn_ptr = parse_message_handlers.getptr(p_msg);
  166. if (fn_ptr) {
  167. return (self->**fn_ptr)(p_args);
  168. } else {
  169. // Any other messages with this prefix should be ignored.
  170. WARN_PRINT("Unknown message: " + p_msg);
  171. return ERR_SKIP;
  172. }
  173. }
  174. #endif