embedded_process_macos.mm 11 KB

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