embedded_process.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. _check_focused_process_id();
  42. _check_mouse_over();
  43. // We need to detect when the control globally changes location or size on the screen.
  44. // NOTIFICATION_RESIZED and NOTIFICATION_WM_POSITION_CHANGED are not enough to detect
  45. // resized parent to siblings controls that can affect global position.
  46. Rect2i new_global_rect = get_global_rect();
  47. if (last_global_rect != new_global_rect) {
  48. last_global_rect = new_global_rect;
  49. _queue_update_embedded_process();
  50. }
  51. } break;
  52. case NOTIFICATION_DRAW: {
  53. _draw();
  54. } break;
  55. case NOTIFICATION_RESIZED:
  56. case NOTIFICATION_VISIBILITY_CHANGED:
  57. case NOTIFICATION_WM_POSITION_CHANGED: {
  58. _queue_update_embedded_process();
  59. } break;
  60. case NOTIFICATION_THEME_CHANGED: {
  61. focus_style_box = get_theme_stylebox(SNAME("FocusViewport"), EditorStringName(EditorStyles));
  62. Ref<StyleBoxFlat> focus_style_box_flat = focus_style_box;
  63. if (focus_style_box_flat.is_valid()) {
  64. margin_top_left = Point2i(focus_style_box_flat->get_border_width(SIDE_LEFT), focus_style_box_flat->get_border_width(SIDE_TOP));
  65. margin_bottom_right = Point2i(focus_style_box_flat->get_border_width(SIDE_RIGHT), focus_style_box_flat->get_border_width(SIDE_BOTTOM));
  66. } else if (focus_style_box.is_valid()) {
  67. margin_top_left = Point2i(focus_style_box->get_margin(SIDE_LEFT), focus_style_box->get_margin(SIDE_TOP));
  68. margin_bottom_right = Point2i(focus_style_box->get_margin(SIDE_RIGHT), focus_style_box->get_margin(SIDE_BOTTOM));
  69. } else {
  70. margin_top_left = Point2i();
  71. margin_bottom_right = Point2i();
  72. }
  73. } break;
  74. case NOTIFICATION_FOCUS_ENTER: {
  75. _queue_update_embedded_process();
  76. } break;
  77. case NOTIFICATION_APPLICATION_FOCUS_IN: {
  78. application_has_focus = true;
  79. if (embedded_process_was_focused) {
  80. embedded_process_was_focused = false;
  81. // Refocus the embedded process if it was focused when the application lost focus,
  82. // but do not refocus if the embedded process is currently focused (indicating it just lost focus)
  83. // or if the current window is a different popup or secondary window.
  84. if (embedding_completed && current_process_id != focused_process_id && window && window->has_focus()) {
  85. grab_focus();
  86. _queue_update_embedded_process();
  87. }
  88. }
  89. } break;
  90. case NOTIFICATION_APPLICATION_FOCUS_OUT: {
  91. application_has_focus = false;
  92. embedded_process_was_focused = embedding_completed && current_process_id == focused_process_id;
  93. } break;
  94. }
  95. }
  96. void EmbeddedProcess::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. }
  101. }
  102. void EmbeddedProcess::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. }
  107. }
  108. Rect2i EmbeddedProcess::_get_global_embedded_window_rect() {
  109. Rect2i control_rect = get_global_rect();
  110. control_rect = Rect2i(control_rect.position, control_rect.size.maxi(1));
  111. if (keep_aspect) {
  112. Rect2i desired_rect = control_rect;
  113. float ratio = MIN((float)control_rect.size.x / window_size.x, (float)control_rect.size.y / window_size.y);
  114. desired_rect.size = Size2i(window_size.x * ratio, window_size.y * ratio).maxi(1);
  115. 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));
  116. return desired_rect;
  117. } else {
  118. return control_rect;
  119. }
  120. }
  121. Rect2i EmbeddedProcess::get_screen_embedded_window_rect() {
  122. Rect2i rect = _get_global_embedded_window_rect();
  123. if (window) {
  124. rect.position += window->get_position();
  125. }
  126. // Removing margins to make space for the focus border style.
  127. return Rect2i(rect.position.x + margin_top_left.x, rect.position.y + margin_top_left.y, MAX(rect.size.x - (margin_top_left.x + margin_bottom_right.x), 1), MAX(rect.size.y - (margin_top_left.y + margin_bottom_right.y), 1));
  128. }
  129. bool EmbeddedProcess::is_embedding_in_progress() {
  130. return !timer_embedding->is_stopped();
  131. }
  132. bool EmbeddedProcess::is_embedding_completed() {
  133. return embedding_completed;
  134. }
  135. void EmbeddedProcess::embed_process(OS::ProcessID p_pid) {
  136. if (!window) {
  137. return;
  138. }
  139. ERR_FAIL_COND_MSG(!DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_WINDOW_EMBEDDING), "Embedded process not supported by this display server.");
  140. if (current_process_id != 0) {
  141. // Stop embedding the last process.
  142. OS::get_singleton()->kill(current_process_id);
  143. }
  144. reset();
  145. current_process_id = p_pid;
  146. start_embedding_time = OS::get_singleton()->get_ticks_msec();
  147. embedding_grab_focus = has_focus();
  148. set_process(true);
  149. set_notify_transform(true);
  150. // Attempt to embed the process, but if it has just started and the window is not ready yet,
  151. // we will retry in this case.
  152. _try_embed_process();
  153. }
  154. void EmbeddedProcess::reset() {
  155. if (current_process_id != 0 && embedding_completed) {
  156. DisplayServer::get_singleton()->remove_embedded_process(current_process_id);
  157. }
  158. current_process_id = 0;
  159. embedding_completed = false;
  160. start_embedding_time = 0;
  161. embedding_grab_focus = false;
  162. timer_embedding->stop();
  163. set_process(false);
  164. set_notify_transform(false);
  165. queue_redraw();
  166. }
  167. void EmbeddedProcess::_try_embed_process() {
  168. bool is_visible = is_visible_in_tree();
  169. 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);
  170. if (err == OK) {
  171. embedding_completed = true;
  172. queue_redraw();
  173. emit_signal(SNAME("embedding_completed"));
  174. } else if (err == ERR_DOES_NOT_EXIST) {
  175. if (OS::get_singleton()->get_ticks_msec() - start_embedding_time >= (uint64_t)embedding_timeout) {
  176. // Embedding process timed out.
  177. reset();
  178. emit_signal(SNAME("embedding_failed"));
  179. } else {
  180. // Tries another shot.
  181. timer_embedding->start();
  182. }
  183. } else {
  184. // Another unknown error.
  185. reset();
  186. emit_signal(SNAME("embedding_failed"));
  187. }
  188. }
  189. bool EmbeddedProcess::_is_embedded_process_updatable() {
  190. return window && current_process_id != 0 && embedding_completed;
  191. }
  192. void EmbeddedProcess::_queue_update_embedded_process() {
  193. if (updated_embedded_process_queued || !_is_embedded_process_updatable()) {
  194. return;
  195. }
  196. updated_embedded_process_queued = true;
  197. callable_mp(this, &EmbeddedProcess::_update_embedded_process).call_deferred();
  198. }
  199. void EmbeddedProcess::_update_embedded_process() {
  200. updated_embedded_process_queued = false;
  201. if (!_is_embedded_process_updatable()) {
  202. return;
  203. }
  204. bool must_grab_focus = false;
  205. bool focus = has_focus();
  206. if (last_updated_embedded_process_focused != focus) {
  207. if (focus) {
  208. must_grab_focus = true;
  209. }
  210. last_updated_embedded_process_focused = focus;
  211. }
  212. DisplayServer::get_singleton()->embed_process(window->get_window_id(), current_process_id, get_screen_embedded_window_rect(), is_visible_in_tree(), must_grab_focus);
  213. emit_signal(SNAME("embedded_process_updated"));
  214. }
  215. void EmbeddedProcess::_timer_embedding_timeout() {
  216. _try_embed_process();
  217. }
  218. void EmbeddedProcess::_draw() {
  219. if (focused_process_id == current_process_id && has_focus() && focus_style_box.is_valid()) {
  220. Size2 size = get_size();
  221. Rect2 r = Rect2(Point2(), size);
  222. focus_style_box->draw(get_canvas_item(), r);
  223. }
  224. }
  225. void EmbeddedProcess::_check_mouse_over() {
  226. // This method checks if the mouse is over the embedded process while the current application is focused.
  227. // The goal is to give focus to the embedded process as soon as the mouse hovers over it,
  228. // allowing the user to interact with it immediately without needing to click first.
  229. if (!is_visible_in_tree() || !embedding_completed || !application_has_focus || !window || !window->has_focus() || Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT) || Input::get_singleton()->is_mouse_button_pressed(MouseButton::RIGHT)) {
  230. return;
  231. }
  232. bool focused = has_focus();
  233. // Not stealing focus from a textfield.
  234. if (!focused && get_viewport()->gui_get_focus_owner() && get_viewport()->gui_get_focus_owner()->is_text_field()) {
  235. return;
  236. }
  237. Vector2 mouse_position = DisplayServer::get_singleton()->mouse_get_position();
  238. Rect2i window_rect = get_screen_embedded_window_rect();
  239. if (!window_rect.has_point(mouse_position)) {
  240. return;
  241. }
  242. // Don't grab the focus if mouse over another window.
  243. DisplayServer::WindowID window_id_over = DisplayServer::get_singleton()->get_window_at_screen_position(mouse_position);
  244. if (window_id_over > 0 && window_id_over != window->get_window_id()) {
  245. return;
  246. }
  247. // When we already have the focus and the user moves the mouse over the embedded process,
  248. // we just need to refocus the process.
  249. if (focused) {
  250. _queue_update_embedded_process();
  251. } else {
  252. grab_focus();
  253. queue_redraw();
  254. }
  255. }
  256. void EmbeddedProcess::_check_focused_process_id() {
  257. OS::ProcessID process_id = DisplayServer::get_singleton()->get_focused_process_id();
  258. if (process_id != focused_process_id) {
  259. focused_process_id = process_id;
  260. if (focused_process_id == current_process_id) {
  261. // The embedded process got the focus.
  262. emit_signal(SNAME("embedded_process_focused"));
  263. if (has_focus()) {
  264. // Redraw to updated the focus style.
  265. queue_redraw();
  266. } else {
  267. grab_focus();
  268. }
  269. } else if (has_focus()) {
  270. release_focus();
  271. }
  272. }
  273. }
  274. void EmbeddedProcess::_bind_methods() {
  275. ADD_SIGNAL(MethodInfo("embedding_completed"));
  276. ADD_SIGNAL(MethodInfo("embedding_failed"));
  277. ADD_SIGNAL(MethodInfo("embedded_process_updated"));
  278. ADD_SIGNAL(MethodInfo("embedded_process_focused"));
  279. }
  280. EmbeddedProcess::EmbeddedProcess() {
  281. timer_embedding = memnew(Timer);
  282. timer_embedding->set_wait_time(0.1);
  283. timer_embedding->set_one_shot(true);
  284. add_child(timer_embedding);
  285. timer_embedding->connect("timeout", callable_mp(this, &EmbeddedProcess::_timer_embedding_timeout));
  286. set_focus_mode(FOCUS_ALL);
  287. }
  288. EmbeddedProcess::~EmbeddedProcess() {
  289. if (current_process_id != 0) {
  290. // Stop embedding the last process.
  291. OS::get_singleton()->kill(current_process_id);
  292. reset();
  293. }
  294. }