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