embedded_process.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. if (embedded_process_was_focused) {
  74. embedded_process_was_focused = false;
  75. // Refocus the embedded process if it was focused when the application lost focus,
  76. // but do not refocus if the embedded process is currently focused (indicating it just lost focus)
  77. // or if the current window is a different popup or secondary window.
  78. if (embedding_completed && current_process_id != focused_process_id && window && window->has_focus()) {
  79. grab_focus();
  80. queue_update_embedded_process();
  81. }
  82. }
  83. } break;
  84. case NOTIFICATION_APPLICATION_FOCUS_OUT: {
  85. application_has_focus = false;
  86. embedded_process_was_focused = embedding_completed && current_process_id == focused_process_id;
  87. } break;
  88. }
  89. }
  90. void EmbeddedProcess::set_window_size(const Size2i p_window_size) {
  91. if (window_size != p_window_size) {
  92. window_size = p_window_size;
  93. queue_update_embedded_process();
  94. }
  95. }
  96. void EmbeddedProcess::set_keep_aspect(bool p_keep_aspect) {
  97. if (keep_aspect != p_keep_aspect) {
  98. keep_aspect = p_keep_aspect;
  99. queue_update_embedded_process();
  100. }
  101. }
  102. Rect2i EmbeddedProcess::_get_global_embedded_window_rect() {
  103. Rect2i control_rect = get_global_rect();
  104. control_rect = Rect2i(control_rect.position + margin_top_left, (control_rect.size - get_margins_size()).maxi(1));
  105. if (window_size != Size2i()) {
  106. Rect2i desired_rect = Rect2i();
  107. if (!keep_aspect && control_rect.size.x >= window_size.x && control_rect.size.y >= window_size.y) {
  108. // Fixed at the desired size.
  109. desired_rect.size = window_size;
  110. } else {
  111. float ratio = MIN((float)control_rect.size.x / window_size.x, (float)control_rect.size.y / window_size.y);
  112. desired_rect.size = Size2i(window_size.x * ratio, window_size.y * ratio).maxi(1);
  113. }
  114. 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));
  115. return desired_rect;
  116. } else {
  117. // Stretch, use all the control area.
  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. return rect;
  127. }
  128. int EmbeddedProcess::get_margin_size(Side p_side) const {
  129. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  130. switch (p_side) {
  131. case SIDE_LEFT:
  132. return margin_top_left.x;
  133. case SIDE_RIGHT:
  134. return margin_bottom_right.x;
  135. case SIDE_TOP:
  136. return margin_top_left.y;
  137. case SIDE_BOTTOM:
  138. return margin_bottom_right.y;
  139. }
  140. return 0;
  141. }
  142. Size2 EmbeddedProcess::get_margins_size() {
  143. return margin_top_left + margin_bottom_right;
  144. }
  145. bool EmbeddedProcess::is_embedding_in_progress() {
  146. return !timer_embedding->is_stopped();
  147. }
  148. bool EmbeddedProcess::is_embedding_completed() {
  149. return embedding_completed;
  150. }
  151. int EmbeddedProcess::get_embedded_pid() const {
  152. return current_process_id;
  153. }
  154. void EmbeddedProcess::embed_process(OS::ProcessID p_pid) {
  155. if (!window) {
  156. return;
  157. }
  158. ERR_FAIL_COND_MSG(!DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_WINDOW_EMBEDDING), "Embedded process not supported by this display server.");
  159. if (current_process_id != 0) {
  160. // Stop embedding the last process.
  161. OS::get_singleton()->kill(current_process_id);
  162. }
  163. reset();
  164. current_process_id = p_pid;
  165. start_embedding_time = OS::get_singleton()->get_ticks_msec();
  166. embedding_grab_focus = has_focus();
  167. timer_update_embedded_process->start();
  168. set_process(true);
  169. set_notify_transform(true);
  170. // Attempt to embed the process, but if it has just started and the window is not ready yet,
  171. // we will retry in this case.
  172. _try_embed_process();
  173. }
  174. void EmbeddedProcess::reset() {
  175. if (current_process_id != 0 && embedding_completed) {
  176. DisplayServer::get_singleton()->remove_embedded_process(current_process_id);
  177. }
  178. current_process_id = 0;
  179. embedding_completed = false;
  180. start_embedding_time = 0;
  181. embedding_grab_focus = false;
  182. timer_embedding->stop();
  183. timer_update_embedded_process->stop();
  184. set_process(false);
  185. set_notify_transform(false);
  186. queue_redraw();
  187. }
  188. void EmbeddedProcess::request_close() {
  189. if (current_process_id != 0 && embedding_completed) {
  190. DisplayServer::get_singleton()->request_close_embedded_process(current_process_id);
  191. }
  192. }
  193. void EmbeddedProcess::_try_embed_process() {
  194. bool is_visible = is_visible_in_tree();
  195. 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);
  196. if (err == OK) {
  197. embedding_completed = true;
  198. queue_redraw();
  199. emit_signal(SNAME("embedding_completed"));
  200. } else if (err == ERR_DOES_NOT_EXIST) {
  201. if (OS::get_singleton()->get_ticks_msec() - start_embedding_time >= (uint64_t)embedding_timeout) {
  202. // Embedding process timed out.
  203. reset();
  204. emit_signal(SNAME("embedding_failed"));
  205. } else {
  206. // Tries another shot.
  207. timer_embedding->start();
  208. }
  209. } else {
  210. // Another unknown error.
  211. reset();
  212. emit_signal(SNAME("embedding_failed"));
  213. }
  214. }
  215. bool EmbeddedProcess::_is_embedded_process_updatable() {
  216. return window && current_process_id != 0 && embedding_completed;
  217. }
  218. void EmbeddedProcess::queue_update_embedded_process() {
  219. updated_embedded_process_queued = true;
  220. }
  221. void EmbeddedProcess::_timer_update_embedded_process_timeout() {
  222. _check_focused_process_id();
  223. _check_mouse_over();
  224. if (!updated_embedded_process_queued) {
  225. // We need to detect when the control globally changes location or size on the screen.
  226. // NOTIFICATION_RESIZED and NOTIFICATION_WM_POSITION_CHANGED are not enough to detect
  227. // resized parent to siblings controls that can affect global position.
  228. Rect2i new_global_rect = get_global_rect();
  229. if (last_global_rect != new_global_rect) {
  230. last_global_rect = new_global_rect;
  231. queue_update_embedded_process();
  232. }
  233. }
  234. }
  235. void EmbeddedProcess::_update_embedded_process() {
  236. if (!_is_embedded_process_updatable()) {
  237. return;
  238. }
  239. bool must_grab_focus = false;
  240. bool focus = has_focus();
  241. if (last_updated_embedded_process_focused != focus) {
  242. if (focus) {
  243. must_grab_focus = true;
  244. }
  245. last_updated_embedded_process_focused = focus;
  246. }
  247. DisplayServer::get_singleton()->embed_process(window->get_window_id(), current_process_id, get_screen_embedded_window_rect(), is_visible_in_tree(), must_grab_focus);
  248. emit_signal(SNAME("embedded_process_updated"));
  249. }
  250. void EmbeddedProcess::_timer_embedding_timeout() {
  251. _try_embed_process();
  252. }
  253. void EmbeddedProcess::_draw() {
  254. if (focused_process_id == current_process_id && has_focus() && focus_style_box.is_valid()) {
  255. Size2 size = get_size();
  256. Rect2 r = Rect2(Point2(), size);
  257. focus_style_box->draw(get_canvas_item(), r);
  258. }
  259. }
  260. void EmbeddedProcess::_check_mouse_over() {
  261. // This method checks if the mouse is over the embedded process while the current application is focused.
  262. // The goal is to give focus to the embedded process as soon as the mouse hovers over it,
  263. // allowing the user to interact with it immediately without needing to click first.
  264. 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)) {
  265. return;
  266. }
  267. bool focused = has_focus();
  268. // Not stealing focus from a textfield.
  269. if (!focused && 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. // When we already have the focus and the user moves the mouse over the embedded process,
  283. // we just need to refocus the process.
  284. if (focused) {
  285. queue_update_embedded_process();
  286. } else {
  287. grab_focus();
  288. queue_redraw();
  289. }
  290. }
  291. void EmbeddedProcess::_check_focused_process_id() {
  292. OS::ProcessID process_id = DisplayServer::get_singleton()->get_focused_process_id();
  293. if (process_id != focused_process_id) {
  294. focused_process_id = process_id;
  295. if (focused_process_id == current_process_id) {
  296. // The embedded process got the focus.
  297. emit_signal(SNAME("embedded_process_focused"));
  298. if (has_focus()) {
  299. // Redraw to updated the focus style.
  300. queue_redraw();
  301. } else {
  302. grab_focus();
  303. }
  304. } else if (has_focus()) {
  305. release_focus();
  306. }
  307. }
  308. }
  309. void EmbeddedProcess::_bind_methods() {
  310. ADD_SIGNAL(MethodInfo("embedding_completed"));
  311. ADD_SIGNAL(MethodInfo("embedding_failed"));
  312. ADD_SIGNAL(MethodInfo("embedded_process_updated"));
  313. ADD_SIGNAL(MethodInfo("embedded_process_focused"));
  314. }
  315. EmbeddedProcess::EmbeddedProcess() {
  316. timer_embedding = memnew(Timer);
  317. timer_embedding->set_wait_time(0.1);
  318. timer_embedding->set_one_shot(true);
  319. add_child(timer_embedding);
  320. timer_embedding->connect("timeout", callable_mp(this, &EmbeddedProcess::_timer_embedding_timeout));
  321. timer_update_embedded_process = memnew(Timer);
  322. timer_update_embedded_process->set_wait_time(0.1);
  323. add_child(timer_update_embedded_process);
  324. timer_update_embedded_process->connect("timeout", callable_mp(this, &EmbeddedProcess::_timer_update_embedded_process_timeout));
  325. set_focus_mode(FOCUS_ALL);
  326. }
  327. EmbeddedProcess::~EmbeddedProcess() {
  328. if (current_process_id != 0) {
  329. // Stop embedding the last process.
  330. OS::get_singleton()->kill(current_process_id);
  331. reset();
  332. }
  333. }