embedded_process.cpp 13 KB

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