embedded_process.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 + margin_top_left, (control_rect.size - get_margins_size()).maxi(1));
  111. if (window_size != Size2i()) {
  112. Rect2i desired_rect = Rect2i();
  113. if (!keep_aspect && control_rect.size.x >= window_size.x && control_rect.size.y >= window_size.y) {
  114. // Fixed at the desired size.
  115. desired_rect.size = window_size;
  116. } else {
  117. float ratio = MIN((float)control_rect.size.x / window_size.x, (float)control_rect.size.y / window_size.y);
  118. desired_rect.size = Size2i(window_size.x * ratio, window_size.y * ratio).maxi(1);
  119. }
  120. 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));
  121. return desired_rect;
  122. } else {
  123. // Stretch, use all the control area.
  124. return control_rect;
  125. }
  126. }
  127. Rect2i EmbeddedProcess::get_screen_embedded_window_rect() {
  128. Rect2i rect = _get_global_embedded_window_rect();
  129. if (window) {
  130. rect.position += window->get_position();
  131. }
  132. return rect;
  133. }
  134. int EmbeddedProcess::get_margin_size(Side p_side) const {
  135. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  136. switch (p_side) {
  137. case SIDE_LEFT:
  138. return margin_top_left.x;
  139. case SIDE_RIGHT:
  140. return margin_bottom_right.x;
  141. case SIDE_TOP:
  142. return margin_top_left.y;
  143. case SIDE_BOTTOM:
  144. return margin_bottom_right.y;
  145. }
  146. return 0;
  147. }
  148. Size2 EmbeddedProcess::get_margins_size() {
  149. return margin_top_left + margin_bottom_right;
  150. }
  151. bool EmbeddedProcess::is_embedding_in_progress() {
  152. return !timer_embedding->is_stopped();
  153. }
  154. bool EmbeddedProcess::is_embedding_completed() {
  155. return embedding_completed;
  156. }
  157. int EmbeddedProcess::get_embedded_pid() const {
  158. return current_process_id;
  159. }
  160. void EmbeddedProcess::embed_process(OS::ProcessID p_pid) {
  161. if (!window) {
  162. return;
  163. }
  164. ERR_FAIL_COND_MSG(!DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_WINDOW_EMBEDDING), "Embedded process not supported by this display server.");
  165. if (current_process_id != 0) {
  166. // Stop embedding the last process.
  167. OS::get_singleton()->kill(current_process_id);
  168. }
  169. reset();
  170. current_process_id = p_pid;
  171. start_embedding_time = OS::get_singleton()->get_ticks_msec();
  172. embedding_grab_focus = has_focus();
  173. set_process(true);
  174. set_notify_transform(true);
  175. // Attempt to embed the process, but if it has just started and the window is not ready yet,
  176. // we will retry in this case.
  177. _try_embed_process();
  178. }
  179. void EmbeddedProcess::reset() {
  180. if (current_process_id != 0 && embedding_completed) {
  181. DisplayServer::get_singleton()->remove_embedded_process(current_process_id);
  182. }
  183. current_process_id = 0;
  184. embedding_completed = false;
  185. start_embedding_time = 0;
  186. embedding_grab_focus = false;
  187. timer_embedding->stop();
  188. set_process(false);
  189. set_notify_transform(false);
  190. queue_redraw();
  191. }
  192. void EmbeddedProcess::_try_embed_process() {
  193. bool is_visible = is_visible_in_tree();
  194. 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);
  195. if (err == OK) {
  196. embedding_completed = true;
  197. queue_redraw();
  198. emit_signal(SNAME("embedding_completed"));
  199. } else if (err == ERR_DOES_NOT_EXIST) {
  200. if (OS::get_singleton()->get_ticks_msec() - start_embedding_time >= (uint64_t)embedding_timeout) {
  201. // Embedding process timed out.
  202. reset();
  203. emit_signal(SNAME("embedding_failed"));
  204. } else {
  205. // Tries another shot.
  206. timer_embedding->start();
  207. }
  208. } else {
  209. // Another unknown error.
  210. reset();
  211. emit_signal(SNAME("embedding_failed"));
  212. }
  213. }
  214. bool EmbeddedProcess::_is_embedded_process_updatable() {
  215. return window && current_process_id != 0 && embedding_completed;
  216. }
  217. void EmbeddedProcess::queue_update_embedded_process() {
  218. if (updated_embedded_process_queued || !_is_embedded_process_updatable()) {
  219. return;
  220. }
  221. updated_embedded_process_queued = true;
  222. callable_mp(this, &EmbeddedProcess::_update_embedded_process).call_deferred();
  223. }
  224. void EmbeddedProcess::_update_embedded_process() {
  225. updated_embedded_process_queued = false;
  226. if (!_is_embedded_process_updatable()) {
  227. return;
  228. }
  229. bool must_grab_focus = false;
  230. bool focus = has_focus();
  231. if (last_updated_embedded_process_focused != focus) {
  232. if (focus) {
  233. must_grab_focus = true;
  234. }
  235. last_updated_embedded_process_focused = focus;
  236. }
  237. DisplayServer::get_singleton()->embed_process(window->get_window_id(), current_process_id, get_screen_embedded_window_rect(), is_visible_in_tree(), must_grab_focus);
  238. emit_signal(SNAME("embedded_process_updated"));
  239. }
  240. void EmbeddedProcess::_timer_embedding_timeout() {
  241. _try_embed_process();
  242. }
  243. void EmbeddedProcess::_draw() {
  244. if (focused_process_id == current_process_id && has_focus() && focus_style_box.is_valid()) {
  245. Size2 size = get_size();
  246. Rect2 r = Rect2(Point2(), size);
  247. focus_style_box->draw(get_canvas_item(), r);
  248. }
  249. }
  250. void EmbeddedProcess::_check_mouse_over() {
  251. // This method checks if the mouse is over the embedded process while the current application is focused.
  252. // The goal is to give focus to the embedded process as soon as the mouse hovers over it,
  253. // allowing the user to interact with it immediately without needing to click first.
  254. 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)) {
  255. return;
  256. }
  257. bool focused = has_focus();
  258. // Not stealing focus from a textfield.
  259. if (!focused && get_viewport()->gui_get_focus_owner() && get_viewport()->gui_get_focus_owner()->is_text_field()) {
  260. return;
  261. }
  262. Vector2 mouse_position = DisplayServer::get_singleton()->mouse_get_position();
  263. Rect2i window_rect = get_screen_embedded_window_rect();
  264. if (!window_rect.has_point(mouse_position)) {
  265. return;
  266. }
  267. // Don't grab the focus if mouse over another window.
  268. DisplayServer::WindowID window_id_over = DisplayServer::get_singleton()->get_window_at_screen_position(mouse_position);
  269. if (window_id_over > 0 && window_id_over != window->get_window_id()) {
  270. return;
  271. }
  272. // When we already have the focus and the user moves the mouse over the embedded process,
  273. // we just need to refocus the process.
  274. if (focused) {
  275. queue_update_embedded_process();
  276. } else {
  277. grab_focus();
  278. queue_redraw();
  279. }
  280. }
  281. void EmbeddedProcess::_check_focused_process_id() {
  282. OS::ProcessID process_id = DisplayServer::get_singleton()->get_focused_process_id();
  283. if (process_id != focused_process_id) {
  284. focused_process_id = process_id;
  285. if (focused_process_id == current_process_id) {
  286. // The embedded process got the focus.
  287. emit_signal(SNAME("embedded_process_focused"));
  288. if (has_focus()) {
  289. // Redraw to updated the focus style.
  290. queue_redraw();
  291. } else {
  292. grab_focus();
  293. }
  294. } else if (has_focus()) {
  295. release_focus();
  296. }
  297. }
  298. }
  299. void EmbeddedProcess::_bind_methods() {
  300. ADD_SIGNAL(MethodInfo("embedding_completed"));
  301. ADD_SIGNAL(MethodInfo("embedding_failed"));
  302. ADD_SIGNAL(MethodInfo("embedded_process_updated"));
  303. ADD_SIGNAL(MethodInfo("embedded_process_focused"));
  304. }
  305. EmbeddedProcess::EmbeddedProcess() {
  306. timer_embedding = memnew(Timer);
  307. timer_embedding->set_wait_time(0.1);
  308. timer_embedding->set_one_shot(true);
  309. add_child(timer_embedding);
  310. timer_embedding->connect("timeout", callable_mp(this, &EmbeddedProcess::_timer_embedding_timeout));
  311. set_focus_mode(FOCUS_ALL);
  312. }
  313. EmbeddedProcess::~EmbeddedProcess() {
  314. if (current_process_id != 0) {
  315. // Stop embedding the last process.
  316. OS::get_singleton()->kill(current_process_id);
  317. reset();
  318. }
  319. }