embedded_debugger.mm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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["ime_update"] = &EmbeddedDebugger::_msg_ime_update;
  67. parse_message_handlers["joy_add"] = &EmbeddedDebugger::_msg_joy_add;
  68. parse_message_handlers["joy_del"] = &EmbeddedDebugger::_msg_joy_del;
  69. }
  70. Error EmbeddedDebugger::_msg_window_size(const Array &p_args) {
  71. Size2i size = p_args[0];
  72. ds->window_set_size(size);
  73. return OK;
  74. }
  75. Error EmbeddedDebugger::_msg_mouse_set_mode(const Array &p_args) {
  76. DisplayServer::MouseMode mode = p_args[0];
  77. ds->mouse_set_mode(mode);
  78. return OK;
  79. }
  80. Error EmbeddedDebugger::_msg_event(const Array &p_args) {
  81. Input *input = Input::get_singleton();
  82. if (!input) {
  83. // Ignore if we've received an event before the process has initialized.
  84. return OK;
  85. }
  86. PackedByteArray data = p_args[0];
  87. Ref<InputEvent> event;
  88. decode_input_event(data, event);
  89. {
  90. Ref<InputEventMouse> e = event;
  91. if (e.is_valid()) {
  92. input->set_mouse_position(e->get_position());
  93. }
  94. }
  95. {
  96. Ref<InputEventMagnifyGesture> e = event;
  97. if (e.is_valid()) {
  98. input->set_mouse_position(e->get_position());
  99. }
  100. }
  101. {
  102. Ref<InputEventPanGesture> e = event;
  103. if (e.is_valid()) {
  104. input->set_mouse_position(e->get_position());
  105. }
  106. }
  107. if (event.is_valid()) {
  108. input->parse_input_event(event);
  109. }
  110. return OK;
  111. }
  112. Error EmbeddedDebugger::_msg_win_event(const Array &p_args) {
  113. DisplayServer::WindowEvent win_event = p_args[0];
  114. ds->send_window_event(win_event, DisplayServer::MAIN_WINDOW_ID);
  115. if (win_event == DisplayServer::WindowEvent::WINDOW_EVENT_MOUSE_EXIT) {
  116. Input::get_singleton()->release_pressed_events();
  117. }
  118. return OK;
  119. }
  120. Error EmbeddedDebugger::_msg_ime_update(const Array &p_args) {
  121. ERR_FAIL_COND_V_MSG(p_args.size() != 2, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'ime_update' message.");
  122. String ime_text = p_args[0];
  123. Vector2i ime_selection = p_args[1];
  124. ds->update_im_text(ime_selection, ime_text);
  125. return OK;
  126. }
  127. Error EmbeddedDebugger::_msg_joy_add(const Array &p_args) {
  128. ERR_FAIL_COND_V_MSG(p_args.size() != 2, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'joy_add' message.");
  129. int idx = p_args[0];
  130. String name = p_args[1];
  131. ds->joy_add(idx, name);
  132. return OK;
  133. }
  134. Error EmbeddedDebugger::_msg_joy_del(const Array &p_args) {
  135. ERR_FAIL_COND_V_MSG(p_args.size() != 1, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'joy_del' message.");
  136. int idx = p_args[0];
  137. ds->joy_del(idx);
  138. return OK;
  139. }
  140. Error EmbeddedDebugger::parse_message(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured) {
  141. EmbeddedDebugger *self = static_cast<EmbeddedDebugger *>(p_user);
  142. r_captured = true;
  143. ParseMessageFunc *fn_ptr = parse_message_handlers.getptr(p_msg);
  144. if (fn_ptr) {
  145. return (self->**fn_ptr)(p_args);
  146. } else {
  147. // Any other messages with this prefix should be ignored.
  148. WARN_PRINT("Unknown message: " + p_msg);
  149. return ERR_SKIP;
  150. }
  151. }
  152. #endif