embedded_debugger.mm 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #include "core/os/main_loop.h"
  35. #ifdef DEBUG_ENABLED
  36. HashMap<String, EmbeddedDebugger::ParseMessageFunc> EmbeddedDebugger::parse_message_handlers;
  37. #endif
  38. EmbeddedDebugger::EmbeddedDebugger(DisplayServerEmbedded *p_ds) {
  39. singleton = this;
  40. #ifdef DEBUG_ENABLED
  41. ds = p_ds;
  42. if (parse_message_handlers.is_empty()) {
  43. _init_parse_message_handlers();
  44. }
  45. EngineDebugger::register_message_capture("embed", EngineDebugger::Capture(this, EmbeddedDebugger::parse_message));
  46. #endif
  47. }
  48. EmbeddedDebugger::~EmbeddedDebugger() {
  49. singleton = nullptr;
  50. }
  51. void EmbeddedDebugger::initialize(DisplayServerEmbedded *p_ds) {
  52. if (EngineDebugger::is_active()) {
  53. memnew(EmbeddedDebugger(p_ds));
  54. }
  55. }
  56. void EmbeddedDebugger::deinitialize() {
  57. if (singleton) {
  58. memdelete(singleton);
  59. }
  60. }
  61. #ifdef DEBUG_ENABLED
  62. void EmbeddedDebugger::_init_parse_message_handlers() {
  63. parse_message_handlers["window_size"] = &EmbeddedDebugger::_msg_window_size;
  64. parse_message_handlers["mouse_set_mode"] = &EmbeddedDebugger::_msg_mouse_set_mode;
  65. parse_message_handlers["event"] = &EmbeddedDebugger::_msg_event;
  66. parse_message_handlers["win_event"] = &EmbeddedDebugger::_msg_win_event;
  67. parse_message_handlers["notification"] = &EmbeddedDebugger::_msg_notification;
  68. parse_message_handlers["ime_update"] = &EmbeddedDebugger::_msg_ime_update;
  69. parse_message_handlers["ds_state"] = &EmbeddedDebugger::_msg_ds_state;
  70. }
  71. Error EmbeddedDebugger::_msg_window_size(const Array &p_args) {
  72. ERR_FAIL_COND_V_MSG(p_args.size() != 1, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'window_size' message.");
  73. Size2i size = p_args[0];
  74. ds->_window_set_size(size);
  75. return OK;
  76. }
  77. Error EmbeddedDebugger::_msg_mouse_set_mode(const Array &p_args) {
  78. ERR_FAIL_COND_V_MSG(p_args.size() != 1, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'mouse_set_mode' message.");
  79. DisplayServer::MouseMode mode = p_args[0];
  80. ds->mouse_set_mode(mode);
  81. return OK;
  82. }
  83. Error EmbeddedDebugger::_msg_event(const Array &p_args) {
  84. ERR_FAIL_COND_V_MSG(p_args.size() != 1, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'event' message.");
  85. Input *input = Input::get_singleton();
  86. if (!input) {
  87. // Ignore if we've received an event before the process has initialized.
  88. return OK;
  89. }
  90. PackedByteArray data = p_args[0];
  91. Ref<InputEvent> event;
  92. decode_input_event(data, event);
  93. {
  94. Ref<InputEventMouse> e = event;
  95. if (e.is_valid()) {
  96. input->set_mouse_position(e->get_position());
  97. }
  98. }
  99. {
  100. Ref<InputEventMagnifyGesture> e = event;
  101. if (e.is_valid()) {
  102. input->set_mouse_position(e->get_position());
  103. }
  104. }
  105. {
  106. Ref<InputEventPanGesture> e = event;
  107. if (e.is_valid()) {
  108. input->set_mouse_position(e->get_position());
  109. }
  110. }
  111. if (event.is_valid()) {
  112. input->parse_input_event(event);
  113. }
  114. return OK;
  115. }
  116. Error EmbeddedDebugger::_msg_win_event(const Array &p_args) {
  117. ERR_FAIL_COND_V_MSG(p_args.size() != 1, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'win_event' message.");
  118. DisplayServer::WindowEvent win_event = p_args[0];
  119. ds->send_window_event(win_event, DisplayServer::MAIN_WINDOW_ID);
  120. if (win_event == DisplayServer::WindowEvent::WINDOW_EVENT_MOUSE_EXIT) {
  121. Input::get_singleton()->release_pressed_events();
  122. }
  123. return OK;
  124. }
  125. Error EmbeddedDebugger::_msg_ime_update(const Array &p_args) {
  126. ERR_FAIL_COND_V_MSG(p_args.size() != 2, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'ime_update' message.");
  127. String ime_text = p_args[0];
  128. Vector2i ime_selection = p_args[1];
  129. ds->update_im_text(ime_selection, ime_text);
  130. return OK;
  131. }
  132. Error EmbeddedDebugger::_msg_notification(const Array &p_args) {
  133. ERR_FAIL_COND_V_MSG(p_args.size() != 1, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'notification' message.");
  134. int notification = p_args[0];
  135. if (OS::get_singleton()->get_main_loop()) {
  136. OS::get_singleton()->get_main_loop()->notification(notification);
  137. }
  138. return OK;
  139. }
  140. Error EmbeddedDebugger::_msg_ds_state(const Array &p_args) {
  141. ERR_FAIL_COND_V_MSG(p_args.size() != 1, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'ds_state' message.");
  142. PackedByteArray data = p_args[0];
  143. DisplayServerEmbeddedState state;
  144. state.deserialize(data);
  145. ds->set_state(state);
  146. return OK;
  147. }
  148. Error EmbeddedDebugger::parse_message(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured) {
  149. EmbeddedDebugger *self = static_cast<EmbeddedDebugger *>(p_user);
  150. r_captured = true;
  151. ParseMessageFunc *fn_ptr = parse_message_handlers.getptr(p_msg);
  152. if (fn_ptr) {
  153. return (self->**fn_ptr)(p_args);
  154. } else {
  155. // Any other messages with this prefix should be ignored.
  156. WARN_PRINT("Unknown message: " + p_msg);
  157. return ERR_SKIP;
  158. }
  159. }
  160. #endif