embedded_process.cpp 16 KB

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