embedded_process.cpp 16 KB

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