embedded_process_macos.mm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. DisplayServer::WindowID wid = window->get_window_id();
  121. state.screen_window_scale = ds->screen_get_scale(ds->window_get_current_screen(wid));
  122. state.display_id = ds->window_get_display_id(wid);
  123. PackedByteArray data;
  124. state.serialize(data);
  125. script_debugger->send_message("embed:ds_state", { data });
  126. }
  127. void EmbeddedProcessMacOS::_try_embed_process() {
  128. if (current_process_id == 0 || script_debugger == nullptr || context_id == 0) {
  129. return;
  130. }
  131. DisplayServer::WindowID wid = window->get_window_id();
  132. Error err = ds->embed_process_update(wid, this);
  133. if (err == OK) {
  134. layer_host->set_rect(get_adjusted_embedded_window_rect(get_rect()));
  135. // Replicate important DisplayServer state.
  136. display_state_changed();
  137. Rect2i rect = get_screen_embedded_window_rect();
  138. script_debugger->send_message("embed:window_size", { rect.size });
  139. embedding_state = EmbeddingState::COMPLETED;
  140. queue_redraw();
  141. emit_signal(SNAME("embedding_completed"));
  142. // Send initial joystick state.
  143. {
  144. Input *input = Input::get_singleton();
  145. TypedArray<int> joy_pads = input->get_connected_joypads();
  146. for (const Variant &idx : joy_pads) {
  147. String name = input->get_joy_name(idx);
  148. script_debugger->send_message("embed:joy_add", { idx, name });
  149. }
  150. }
  151. layer_host->grab_focus();
  152. } else {
  153. // Another unknown error.
  154. reset();
  155. emit_signal(SNAME("embedding_failed"));
  156. }
  157. }
  158. Rect2i EmbeddedProcessMacOS::get_adjusted_embedded_window_rect(const Rect2i &p_rect) const {
  159. Rect2i control_rect = Rect2i(p_rect.position + margin_top_left, (p_rect.size - get_margins_size()).maxi(1));
  160. if (window_size != Size2i()) {
  161. Rect2i desired_rect;
  162. if (!keep_aspect && control_rect.size.x >= window_size.x && control_rect.size.y >= window_size.y) {
  163. // Fixed at the desired size.
  164. desired_rect.size = window_size;
  165. } else {
  166. float ratio = MIN((float)control_rect.size.x / window_size.x, (float)control_rect.size.y / window_size.y);
  167. desired_rect.size = Size2i(window_size.x * ratio, window_size.y * ratio).maxi(1);
  168. }
  169. 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));
  170. return desired_rect;
  171. } else {
  172. // Stretch, use all the control area.
  173. return control_rect;
  174. }
  175. }
  176. void EmbeddedProcessMacOS::mouse_set_mode(DisplayServer::MouseMode p_mode) {
  177. mouse_mode = p_mode;
  178. // If the mouse is anything other than visible, we must ensure the Game view is active and the layer focused.
  179. if (mouse_mode != DisplayServer::MOUSE_MODE_VISIBLE) {
  180. EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_GAME);
  181. layer_host->grab_focus();
  182. }
  183. DisplayServer::get_singleton()->mouse_set_mode(p_mode);
  184. }
  185. EmbeddedProcessMacOS::EmbeddedProcessMacOS() :
  186. EmbeddedProcessBase() {
  187. layer_host = memnew(LayerHost(this));
  188. add_child(layer_host);
  189. layer_host->set_focus_mode(FOCUS_ALL);
  190. layer_host->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  191. layer_host->set_custom_minimum_size(Size2(100, 100));
  192. Input *input = Input::get_singleton();
  193. input->connect(SNAME("joy_connection_changed"), callable_mp(this, &EmbeddedProcessMacOS::_joy_connection_changed));
  194. // This shortcut allows a user to forcibly release a captured mouse from within the editor, regardless of whether
  195. // the embedded process has implemented support to release the cursor.
  196. ED_SHORTCUT("game_view/release_mouse", TTRC("Release Mouse"), KeyModifierMask::ALT | Key::ESCAPE);
  197. }
  198. EmbeddedProcessMacOS::~EmbeddedProcessMacOS() {
  199. if (current_process_id != 0) {
  200. // Stop embedding the last process.
  201. OS::get_singleton()->kill(current_process_id);
  202. reset();
  203. }
  204. }
  205. void LayerHost::_notification(int p_what) {
  206. switch (p_what) {
  207. case NOTIFICATION_MOUSE_ENTER: {
  208. DisplayServer *ds = DisplayServer::get_singleton();
  209. for (const KeyValue<DisplayServer::CursorShape, CustomCursor> &E : custom_cursors) {
  210. ds->cursor_set_custom_image(E.value.image, E.key, E.value.hotspot);
  211. }
  212. if (script_debugger) {
  213. script_debugger->send_message("embed:win_event", { DisplayServer::WINDOW_EVENT_MOUSE_ENTER });
  214. }
  215. } break;
  216. case NOTIFICATION_FOCUS_ENTER: {
  217. // Restore mouse capture, if necessary.
  218. DisplayServer *ds = DisplayServer::get_singleton();
  219. if (process->get_mouse_mode() != ds->mouse_get_mode()) {
  220. // Restore embedded process mouse mode.
  221. ds->mouse_set_mode(process->get_mouse_mode());
  222. }
  223. if (!window_focused && script_debugger) {
  224. script_debugger->send_message("embed:win_event", { DisplayServer::WINDOW_EVENT_FOCUS_IN });
  225. window_focused = true;
  226. }
  227. } break;
  228. case NOTIFICATION_MOUSE_EXIT: {
  229. DisplayServer *ds = DisplayServer::get_singleton();
  230. for (int i = 0; i < DisplayServer::CURSOR_MAX; i++) {
  231. ds->cursor_set_custom_image(Ref<Resource>(), (DisplayServer::CursorShape)i, Vector2());
  232. }
  233. if (script_debugger) {
  234. script_debugger->send_message("embed:win_event", { DisplayServer::WINDOW_EVENT_MOUSE_EXIT });
  235. }
  236. } break;
  237. case NOTIFICATION_FOCUS_EXIT: {
  238. // Temporarily set mouse state back to visible, so the user can interact with the editor.
  239. DisplayServer *ds = DisplayServer::get_singleton();
  240. if (ds->mouse_get_mode() != DisplayServer::MOUSE_MODE_VISIBLE) {
  241. ds->mouse_set_mode(DisplayServer::MOUSE_MODE_VISIBLE);
  242. }
  243. if (window_focused && script_debugger) {
  244. script_debugger->send_message("embed:win_event", { DisplayServer::WINDOW_EVENT_FOCUS_OUT });
  245. window_focused = false;
  246. }
  247. } break;
  248. case MainLoop::NOTIFICATION_OS_IME_UPDATE: {
  249. if (script_debugger && has_focus()) {
  250. const String ime_text = DisplayServer::get_singleton()->ime_get_text();
  251. const Vector2i ime_selection = DisplayServer::get_singleton()->ime_get_selection();
  252. script_debugger->send_message("embed:ime_update", { ime_text, ime_selection });
  253. }
  254. } break;
  255. case NOTIFICATION_EXIT_TREE:
  256. case NOTIFICATION_VISIBILITY_CHANGED: {
  257. if (!is_visible_in_tree()) {
  258. DisplayServer *ds = DisplayServer::get_singleton();
  259. for (int i = 0; i < DisplayServer::CURSOR_MAX; i++) {
  260. ds->cursor_set_custom_image(Ref<Resource>(), (DisplayServer::CursorShape)i, Vector2());
  261. }
  262. }
  263. } break;
  264. case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
  265. if (!window_focused && script_debugger) {
  266. script_debugger->send_message("embed:win_event", { DisplayServer::WINDOW_EVENT_FOCUS_IN });
  267. window_focused = true;
  268. }
  269. } break;
  270. case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
  271. if (window_focused && script_debugger) {
  272. script_debugger->send_message("embed:win_event", { DisplayServer::WINDOW_EVENT_FOCUS_OUT });
  273. window_focused = false;
  274. }
  275. } break;
  276. case NOTIFICATION_APPLICATION_FOCUS_IN: {
  277. if (script_debugger) {
  278. script_debugger->send_message("embed:notification", { NOTIFICATION_APPLICATION_FOCUS_IN });
  279. }
  280. } break;
  281. case NOTIFICATION_APPLICATION_FOCUS_OUT: {
  282. if (script_debugger) {
  283. script_debugger->send_message("embed:notification", { NOTIFICATION_APPLICATION_FOCUS_OUT });
  284. }
  285. } break;
  286. }
  287. }
  288. void LayerHost::cursor_set_custom_image(const Ref<Image> &p_image, DisplayServer::CursorShape p_shape, const Vector2 &p_hotspot) {
  289. custom_cursors[p_shape] = CustomCursor(p_image, p_hotspot);
  290. }
  291. void LayerHost::gui_input(const Ref<InputEvent> &p_event) {
  292. if (!process->is_embedding_completed()) {
  293. return;
  294. }
  295. if (p_event->is_pressed()) {
  296. if (ED_IS_SHORTCUT("game_view/release_mouse", p_event)) {
  297. DisplayServer *ds = DisplayServer::get_singleton();
  298. if (ds->mouse_get_mode() != DisplayServer::MOUSE_MODE_VISIBLE) {
  299. ds->mouse_set_mode(DisplayServer::MOUSE_MODE_VISIBLE);
  300. script_debugger->send_message("embed:mouse_set_mode", { DisplayServer::MOUSE_MODE_VISIBLE });
  301. }
  302. accept_event();
  303. return;
  304. }
  305. }
  306. PackedByteArray data;
  307. if (encode_input_event(p_event, data)) {
  308. script_debugger->send_message("embed:event", { data });
  309. accept_event();
  310. }
  311. }
  312. LayerHost::LayerHost(EmbeddedProcessMacOS *p_process) :
  313. process(p_process) {}