embedded_process_macos.mm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**************************************************************************/
  2. /* embedded_process_macos.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_process_macos.h"
  31. #include "platform/macos/display_server_macos.h"
  32. #include "core/input/input_event_codec.h"
  33. #include "editor/debugger/script_editor_debugger.h"
  34. #include "editor/editor_settings.h"
  35. #include "scene/gui/control.h"
  36. #include "scene/main/window.h"
  37. void EmbeddedProcessMacOS::_notification(int p_what) {
  38. switch (p_what) {
  39. case NOTIFICATION_RESIZED:
  40. case NOTIFICATION_VISIBILITY_CHANGED: {
  41. update_embedded_process();
  42. } break;
  43. }
  44. }
  45. void EmbeddedProcessMacOS::update_embedded_process() const {
  46. layer_host->set_rect(get_adjusted_embedded_window_rect(get_rect()));
  47. if (is_embedding_completed()) {
  48. ds->embed_process_update(window->get_window_id(), this);
  49. Rect2i rect = get_screen_embedded_window_rect();
  50. script_debugger->send_message("embed:window_size", { rect.size });
  51. }
  52. }
  53. void EmbeddedProcessMacOS::set_context_id(uint32_t p_context_id) {
  54. if (!window) {
  55. return;
  56. }
  57. context_id = p_context_id;
  58. _try_embed_process();
  59. }
  60. void EmbeddedProcessMacOS::set_script_debugger(ScriptEditorDebugger *p_debugger) {
  61. script_debugger = p_debugger;
  62. layer_host->set_script_debugger(script_debugger);
  63. _try_embed_process();
  64. }
  65. void EmbeddedProcessMacOS::embed_process(OS::ProcessID p_pid) {
  66. if (!window) {
  67. return;
  68. }
  69. if (current_process_id != 0) {
  70. // Stop embedding the last process.
  71. OS::get_singleton()->kill(current_process_id);
  72. }
  73. reset();
  74. current_process_id = p_pid;
  75. embedding_state = EmbeddingState::IN_PROGRESS;
  76. // Attempt to embed the process, but if it has just started and the window is not ready yet,
  77. // we will retry in this case.
  78. _try_embed_process();
  79. }
  80. void EmbeddedProcessMacOS::_joy_connection_changed(int p_index, bool p_connected) const {
  81. if (!script_debugger) {
  82. return;
  83. }
  84. if (p_connected) {
  85. String name = Input::get_singleton()->get_joy_name(p_index);
  86. script_debugger->send_message("embed:joy_add", { p_index, name });
  87. } else {
  88. script_debugger->send_message("embed:joy_del", { p_index });
  89. }
  90. }
  91. void EmbeddedProcessMacOS::reset() {
  92. if (!ds) {
  93. ds = static_cast<DisplayServerMacOS *>(DisplayServer::get_singleton());
  94. }
  95. if (current_process_id != 0 && is_embedding_completed()) {
  96. ds->remove_embedded_process(current_process_id);
  97. }
  98. current_process_id = 0;
  99. embedding_state = EmbeddingState::IDLE;
  100. context_id = 0;
  101. script_debugger = nullptr;
  102. queue_redraw();
  103. }
  104. void EmbeddedProcessMacOS::request_close() {
  105. if (current_process_id != 0 && is_embedding_completed()) {
  106. ds->request_close_embedded_process(current_process_id);
  107. }
  108. }
  109. void EmbeddedProcessMacOS::_try_embed_process() {
  110. if (current_process_id == 0 || script_debugger == nullptr || context_id == 0) {
  111. return;
  112. }
  113. Error err = ds->embed_process_update(window->get_window_id(), this);
  114. if (err == OK) {
  115. Rect2i rect = get_screen_embedded_window_rect();
  116. script_debugger->send_message("embed:window_size", { rect.size });
  117. embedding_state = EmbeddingState::COMPLETED;
  118. queue_redraw();
  119. emit_signal(SNAME("embedding_completed"));
  120. // Replicate some of the DisplayServer state.
  121. {
  122. Dictionary state;
  123. state["screen_get_max_scale"] = ds->screen_get_max_scale();
  124. // script_debugger->send_message("embed:ds_state", { state });
  125. }
  126. // Send initial joystick state.
  127. {
  128. Input *input = Input::get_singleton();
  129. TypedArray<int> joy_pads = input->get_connected_joypads();
  130. for (const Variant &idx : joy_pads) {
  131. String name = input->get_joy_name(idx);
  132. script_debugger->send_message("embed:joy_add", { idx, name });
  133. }
  134. }
  135. layer_host->grab_focus();
  136. } else {
  137. // Another unknown error.
  138. reset();
  139. emit_signal(SNAME("embedding_failed"));
  140. }
  141. }
  142. Rect2i EmbeddedProcessMacOS::get_adjusted_embedded_window_rect(const Rect2i &p_rect) const {
  143. Rect2i control_rect = Rect2i(p_rect.position + margin_top_left, (p_rect.size - get_margins_size()).maxi(1));
  144. if (window_size != Size2i()) {
  145. Rect2i desired_rect;
  146. if (!keep_aspect && control_rect.size.x >= window_size.x && control_rect.size.y >= window_size.y) {
  147. // Fixed at the desired size.
  148. desired_rect.size = window_size;
  149. } else {
  150. float ratio = MIN((float)control_rect.size.x / window_size.x, (float)control_rect.size.y / window_size.y);
  151. desired_rect.size = Size2i(window_size.x * ratio, window_size.y * ratio).maxi(1);
  152. }
  153. desired_rect.position = Size2i(control_rect.position.x + ((control_rect.size.x - desired_rect.size.x) / 2), control_rect.position.y + ((control_rect.size.y - desired_rect.size.y) / 2));
  154. return desired_rect;
  155. } else {
  156. // Stretch, use all the control area.
  157. return control_rect;
  158. }
  159. }
  160. void EmbeddedProcessMacOS::mouse_set_mode(DisplayServer::MouseMode p_mode) {
  161. DisplayServer::get_singleton()->mouse_set_mode(p_mode);
  162. }
  163. EmbeddedProcessMacOS::EmbeddedProcessMacOS() :
  164. EmbeddedProcessBase() {
  165. layer_host = memnew(LayerHost);
  166. add_child(layer_host);
  167. layer_host->set_focus_mode(FOCUS_ALL);
  168. layer_host->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  169. layer_host->set_custom_minimum_size(Size2(100, 100));
  170. Input *input = Input::get_singleton();
  171. input->connect(SNAME("joy_connection_changed"), callable_mp(this, &EmbeddedProcessMacOS::_joy_connection_changed));
  172. // This shortcut allows a user to forcibly release a captured mouse from within the editor, regardless of whether
  173. // the embedded process has implemented support to release the cursor.
  174. ED_SHORTCUT("game_view/release_mouse", TTRC("Release Mouse"), KeyModifierMask::ALT | Key::ESCAPE);
  175. }
  176. void LayerHost::_notification(int p_what) {
  177. switch (p_what) {
  178. case NOTIFICATION_FOCUS_ENTER: {
  179. if (script_debugger) {
  180. script_debugger->send_message("embed:win_event", { DisplayServer::WINDOW_EVENT_MOUSE_ENTER });
  181. }
  182. } break;
  183. case NOTIFICATION_FOCUS_EXIT: {
  184. if (script_debugger) {
  185. script_debugger->send_message("embed:win_event", { DisplayServer::WINDOW_EVENT_MOUSE_EXIT });
  186. }
  187. } break;
  188. case MainLoop::NOTIFICATION_OS_IME_UPDATE: {
  189. if (script_debugger && has_focus()) {
  190. const String ime_text = DisplayServer::get_singleton()->ime_get_text();
  191. const Vector2i ime_selection = DisplayServer::get_singleton()->ime_get_selection();
  192. script_debugger->send_message("embed:ime_update", { ime_text, ime_selection });
  193. }
  194. } break;
  195. }
  196. }
  197. void LayerHost::gui_input(const Ref<InputEvent> &p_event) {
  198. if (!script_debugger) {
  199. return;
  200. }
  201. if (p_event->is_pressed()) {
  202. if (ED_IS_SHORTCUT("game_view/release_mouse", p_event)) {
  203. DisplayServer *ds = DisplayServer::get_singleton();
  204. if (ds->mouse_get_mode() != DisplayServer::MOUSE_MODE_VISIBLE) {
  205. ds->mouse_set_mode(DisplayServer::MOUSE_MODE_VISIBLE);
  206. script_debugger->send_message("embed:mouse_set_mode", { DisplayServer::MOUSE_MODE_VISIBLE });
  207. }
  208. accept_event();
  209. return;
  210. }
  211. }
  212. PackedByteArray data;
  213. if (encode_input_event(p_event, data)) {
  214. script_debugger->send_message("embed:event", { data });
  215. accept_event();
  216. }
  217. }