embedded_process.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /**************************************************************************/
  2. /* embedded_process.cpp */
  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.h"
  31. #include "editor/editor_string_names.h"
  32. #include "scene/main/window.h"
  33. #include "scene/resources/style_box_flat.h"
  34. #include "scene/theme/theme_db.h"
  35. void EmbeddedProcess::_notification(int p_what) {
  36. switch (p_what) {
  37. case NOTIFICATION_ENTER_TREE: {
  38. window = get_window();
  39. } break;
  40. case NOTIFICATION_PROCESS: {
  41. if (updated_embedded_process_queued) {
  42. updated_embedded_process_queued = false;
  43. _update_embedded_process();
  44. }
  45. } break;
  46. case NOTIFICATION_DRAW: {
  47. _draw();
  48. } break;
  49. case NOTIFICATION_RESIZED:
  50. case NOTIFICATION_VISIBILITY_CHANGED:
  51. case NOTIFICATION_WM_POSITION_CHANGED: {
  52. queue_update_embedded_process();
  53. } break;
  54. case NOTIFICATION_THEME_CHANGED: {
  55. focus_style_box = get_theme_stylebox(SNAME("FocusViewport"), EditorStringName(EditorStyles));
  56. Ref<StyleBoxFlat> focus_style_box_flat = focus_style_box;
  57. if (focus_style_box_flat.is_valid()) {
  58. margin_top_left = Point2i(focus_style_box_flat->get_border_width(SIDE_LEFT), focus_style_box_flat->get_border_width(SIDE_TOP));
  59. margin_bottom_right = Point2i(focus_style_box_flat->get_border_width(SIDE_RIGHT), focus_style_box_flat->get_border_width(SIDE_BOTTOM));
  60. } else if (focus_style_box.is_valid()) {
  61. margin_top_left = Point2i(focus_style_box->get_margin(SIDE_LEFT), focus_style_box->get_margin(SIDE_TOP));
  62. margin_bottom_right = Point2i(focus_style_box->get_margin(SIDE_RIGHT), focus_style_box->get_margin(SIDE_BOTTOM));
  63. } else {
  64. margin_top_left = Point2i();
  65. margin_bottom_right = Point2i();
  66. }
  67. } break;
  68. case NOTIFICATION_FOCUS_ENTER: {
  69. queue_update_embedded_process();
  70. } break;
  71. case NOTIFICATION_APPLICATION_FOCUS_IN: {
  72. application_has_focus = true;
  73. last_application_focus_time = OS::get_singleton()->get_ticks_msec();
  74. } break;
  75. case NOTIFICATION_APPLICATION_FOCUS_OUT: {
  76. application_has_focus = false;
  77. } break;
  78. }
  79. }
  80. void EmbeddedProcess::set_window_size(const Size2i p_window_size) {
  81. if (window_size != p_window_size) {
  82. window_size = p_window_size;
  83. queue_update_embedded_process();
  84. }
  85. }
  86. void EmbeddedProcess::set_keep_aspect(bool p_keep_aspect) {
  87. if (keep_aspect != p_keep_aspect) {
  88. keep_aspect = p_keep_aspect;
  89. queue_update_embedded_process();
  90. }
  91. }
  92. Rect2i EmbeddedProcess::get_adjusted_embedded_window_rect(Rect2i p_rect) {
  93. Rect2i control_rect = Rect2i(p_rect.position + margin_top_left, (p_rect.size - get_margins_size()).maxi(1));
  94. if (window) {
  95. control_rect.position += window->get_position();
  96. }
  97. if (window_size != Size2i()) {
  98. Rect2i desired_rect = Rect2i();
  99. if (!keep_aspect && control_rect.size.x >= window_size.x && control_rect.size.y >= window_size.y) {
  100. // Fixed at the desired size.
  101. desired_rect.size = window_size;
  102. } else {
  103. float ratio = MIN((float)control_rect.size.x / window_size.x, (float)control_rect.size.y / window_size.y);
  104. desired_rect.size = Size2i(window_size.x * ratio, window_size.y * ratio).maxi(1);
  105. }
  106. 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));
  107. return desired_rect;
  108. } else {
  109. // Stretch, use all the control area.
  110. return control_rect;
  111. }
  112. }
  113. Rect2i EmbeddedProcess::get_screen_embedded_window_rect() {
  114. return get_adjusted_embedded_window_rect(get_global_rect());
  115. }
  116. int EmbeddedProcess::get_margin_size(Side p_side) const {
  117. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  118. switch (p_side) {
  119. case SIDE_LEFT:
  120. return margin_top_left.x;
  121. case SIDE_RIGHT:
  122. return margin_bottom_right.x;
  123. case SIDE_TOP:
  124. return margin_top_left.y;
  125. case SIDE_BOTTOM:
  126. return margin_bottom_right.y;
  127. }
  128. return 0;
  129. }
  130. Size2 EmbeddedProcess::get_margins_size() {
  131. return margin_top_left + margin_bottom_right;
  132. }
  133. bool EmbeddedProcess::is_embedding_in_progress() {
  134. return !timer_embedding->is_stopped();
  135. }
  136. bool EmbeddedProcess::is_embedding_completed() {
  137. return embedding_completed;
  138. }
  139. int EmbeddedProcess::get_embedded_pid() const {
  140. return current_process_id;
  141. }
  142. void EmbeddedProcess::embed_process(OS::ProcessID p_pid) {
  143. if (!window) {
  144. return;
  145. }
  146. ERR_FAIL_COND_MSG(!DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_WINDOW_EMBEDDING), "Embedded process not supported by this display server.");
  147. if (current_process_id != 0) {
  148. // Stop embedding the last process.
  149. OS::get_singleton()->kill(current_process_id);
  150. }
  151. reset();
  152. current_process_id = p_pid;
  153. start_embedding_time = OS::get_singleton()->get_ticks_msec();
  154. embedding_grab_focus = has_focus();
  155. timer_update_embedded_process->start();
  156. set_process(true);
  157. set_notify_transform(true);
  158. // Attempt to embed the process, but if it has just started and the window is not ready yet,
  159. // we will retry in this case.
  160. _try_embed_process();
  161. }
  162. void EmbeddedProcess::reset() {
  163. if (current_process_id != 0 && embedding_completed) {
  164. DisplayServer::get_singleton()->remove_embedded_process(current_process_id);
  165. }
  166. current_process_id = 0;
  167. embedding_completed = false;
  168. start_embedding_time = 0;
  169. embedding_grab_focus = false;
  170. timer_embedding->stop();
  171. timer_update_embedded_process->stop();
  172. set_process(false);
  173. set_notify_transform(false);
  174. queue_redraw();
  175. }
  176. void EmbeddedProcess::request_close() {
  177. if (current_process_id != 0 && embedding_completed) {
  178. DisplayServer::get_singleton()->request_close_embedded_process(current_process_id);
  179. }
  180. }
  181. void EmbeddedProcess::_try_embed_process() {
  182. bool is_visible = is_visible_in_tree();
  183. Error err = DisplayServer::get_singleton()->embed_process(window->get_window_id(), current_process_id, get_screen_embedded_window_rect(), is_visible, is_visible && application_has_focus && embedding_grab_focus);
  184. if (err == OK) {
  185. embedding_completed = true;
  186. queue_redraw();
  187. emit_signal(SNAME("embedding_completed"));
  188. } else if (err == ERR_DOES_NOT_EXIST) {
  189. if (OS::get_singleton()->get_ticks_msec() - start_embedding_time >= (uint64_t)embedding_timeout) {
  190. // Embedding process timed out.
  191. reset();
  192. emit_signal(SNAME("embedding_failed"));
  193. } else {
  194. // Tries another shot.
  195. timer_embedding->start();
  196. }
  197. } else {
  198. // Another unknown error.
  199. reset();
  200. emit_signal(SNAME("embedding_failed"));
  201. }
  202. }
  203. bool EmbeddedProcess::_is_embedded_process_updatable() {
  204. return window && current_process_id != 0 && embedding_completed;
  205. }
  206. void EmbeddedProcess::queue_update_embedded_process() {
  207. updated_embedded_process_queued = true;
  208. }
  209. void EmbeddedProcess::_timer_update_embedded_process_timeout() {
  210. _check_focused_process_id();
  211. _check_mouse_over();
  212. if (!updated_embedded_process_queued) {
  213. // We need to detect when the control globally changes location or size on the screen.
  214. // NOTIFICATION_RESIZED and NOTIFICATION_WM_POSITION_CHANGED are not enough to detect
  215. // resized parent to siblings controls that can affect global position.
  216. Rect2i new_global_rect = get_global_rect();
  217. if (last_global_rect != new_global_rect) {
  218. last_global_rect = new_global_rect;
  219. queue_update_embedded_process();
  220. }
  221. }
  222. }
  223. void EmbeddedProcess::_update_embedded_process() {
  224. if (!_is_embedded_process_updatable()) {
  225. return;
  226. }
  227. bool must_grab_focus = false;
  228. bool focus = has_focus();
  229. if (last_updated_embedded_process_focused != focus) {
  230. if (focus) {
  231. must_grab_focus = true;
  232. }
  233. last_updated_embedded_process_focused = focus;
  234. }
  235. DisplayServer::get_singleton()->embed_process(window->get_window_id(), current_process_id, get_screen_embedded_window_rect(), is_visible_in_tree(), must_grab_focus);
  236. emit_signal(SNAME("embedded_process_updated"));
  237. }
  238. void EmbeddedProcess::_timer_embedding_timeout() {
  239. _try_embed_process();
  240. }
  241. void EmbeddedProcess::_draw() {
  242. if (focused_process_id == current_process_id && has_focus() && focus_style_box.is_valid()) {
  243. Size2 size = get_size();
  244. Rect2 r = Rect2(Point2(), size);
  245. focus_style_box->draw(get_canvas_item(), r);
  246. }
  247. }
  248. void EmbeddedProcess::_check_mouse_over() {
  249. // This method checks if the mouse is over the embedded process while the current application is focused.
  250. // The goal is to give focus to the embedded process as soon as the mouse hovers over it,
  251. // allowing the user to interact with it immediately without needing to click first.
  252. if (!embedding_completed || !application_has_focus || !window || has_focus() || !is_visible_in_tree() || !window->has_focus() || Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT) || Input::get_singleton()->is_mouse_button_pressed(MouseButton::RIGHT)) {
  253. return;
  254. }
  255. // Before checking whether the mouse is truly inside the embedded process, ensure
  256. // the editor has enough time to re-render. When a breakpoint is hit in the script editor,
  257. // `_check_mouse_over` may be triggered before the editor hides the game workspace.
  258. // This prevents the embedded process from regaining focus immediately after the editor has taken it.
  259. if (OS::get_singleton()->get_ticks_msec() - last_application_focus_time < 500) {
  260. return;
  261. }
  262. // Input::is_mouse_button_pressed is not sufficient to detect the mouse button state
  263. // while the floating game window is being resized.
  264. BitField<MouseButtonMask> mouse_button_mask = DisplayServer::get_singleton()->mouse_get_button_state();
  265. if (!mouse_button_mask.is_empty()) {
  266. return;
  267. }
  268. // Not stealing focus from a textfield.
  269. if (get_viewport()->gui_get_focus_owner() && get_viewport()->gui_get_focus_owner()->is_text_field()) {
  270. return;
  271. }
  272. Vector2 mouse_position = DisplayServer::get_singleton()->mouse_get_position();
  273. Rect2i window_rect = get_screen_embedded_window_rect();
  274. if (!window_rect.has_point(mouse_position)) {
  275. return;
  276. }
  277. // Don't grab the focus if mouse over another window.
  278. DisplayServer::WindowID window_id_over = DisplayServer::get_singleton()->get_window_at_screen_position(mouse_position);
  279. if (window_id_over > 0 && window_id_over != window->get_window_id()) {
  280. return;
  281. }
  282. // Check if there's an exclusive popup, an open menu, or a tooltip.
  283. // We don't want to grab focus to prevent the game window from coming to the front of the modal window
  284. // or the open menu from closing when the mouse cursor moves outside the menu and over the embedded game.
  285. Vector<DisplayServer::WindowID> wl = DisplayServer::get_singleton()->get_window_list();
  286. for (const DisplayServer::WindowID &window_id : wl) {
  287. Window *w = Window::get_from_id(window_id);
  288. if (w && (w->is_exclusive() || w->get_flag(Window::FLAG_POPUP))) {
  289. return;
  290. }
  291. }
  292. // Force "regrabbing" the game window focus.
  293. last_updated_embedded_process_focused = false;
  294. grab_focus();
  295. queue_redraw();
  296. }
  297. void EmbeddedProcess::_check_focused_process_id() {
  298. OS::ProcessID process_id = DisplayServer::get_singleton()->get_focused_process_id();
  299. if (process_id != focused_process_id) {
  300. focused_process_id = process_id;
  301. if (focused_process_id == current_process_id) {
  302. // The embedded process got the focus.
  303. // Refocus the current model when focusing the embedded process.
  304. Window *modal_window = _get_current_modal_window();
  305. if (!modal_window) {
  306. emit_signal(SNAME("embedded_process_focused"));
  307. if (has_focus()) {
  308. // Redraw to updated the focus style.
  309. queue_redraw();
  310. } else {
  311. grab_focus();
  312. }
  313. }
  314. } else if (has_focus()) {
  315. release_focus();
  316. }
  317. }
  318. // Ensure that the opened modal dialog is refocused when the focused process is the embedded process.
  319. if (!application_has_focus && focused_process_id == current_process_id) {
  320. Window *modal_window = _get_current_modal_window();
  321. if (modal_window) {
  322. if (modal_window->get_mode() == Window::MODE_MINIMIZED) {
  323. modal_window->set_mode(Window::MODE_WINDOWED);
  324. }
  325. callable_mp(modal_window, &Window::grab_focus).call_deferred();
  326. }
  327. }
  328. }
  329. Window *EmbeddedProcess::_get_current_modal_window() {
  330. Vector<DisplayServer::WindowID> wl = DisplayServer::get_singleton()->get_window_list();
  331. for (const DisplayServer::WindowID &window_id : wl) {
  332. Window *w = Window::get_from_id(window_id);
  333. if (!w) {
  334. continue;
  335. }
  336. if (w->is_exclusive()) {
  337. return w;
  338. }
  339. }
  340. return nullptr;
  341. }
  342. void EmbeddedProcess::_bind_methods() {
  343. ADD_SIGNAL(MethodInfo("embedding_completed"));
  344. ADD_SIGNAL(MethodInfo("embedding_failed"));
  345. ADD_SIGNAL(MethodInfo("embedded_process_updated"));
  346. ADD_SIGNAL(MethodInfo("embedded_process_focused"));
  347. }
  348. EmbeddedProcess::EmbeddedProcess() {
  349. timer_embedding = memnew(Timer);
  350. timer_embedding->set_wait_time(0.1);
  351. timer_embedding->set_one_shot(true);
  352. add_child(timer_embedding);
  353. timer_embedding->connect("timeout", callable_mp(this, &EmbeddedProcess::_timer_embedding_timeout));
  354. timer_update_embedded_process = memnew(Timer);
  355. timer_update_embedded_process->set_wait_time(0.1);
  356. add_child(timer_update_embedded_process);
  357. timer_update_embedded_process->connect("timeout", callable_mp(this, &EmbeddedProcess::_timer_update_embedded_process_timeout));
  358. set_focus_mode(FOCUS_ALL);
  359. }
  360. EmbeddedProcess::~EmbeddedProcess() {
  361. if (current_process_id != 0) {
  362. // Stop embedding the last process.
  363. OS::get_singleton()->kill(current_process_id);
  364. reset();
  365. }
  366. }