| 1 |
/**************************************************************************/ |
| 2 |
/* embedded_process_macos.mm */ |
| 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_macos.h" |
| 32 |
|
| 33 |
#include "platform/macos/display_server_embedded.h" |
| 34 |
#include "platform/macos/display_server_macos.h" |
| 35 |
|
| 36 |
#include "core/input/input_event_codec.h" |
| 37 |
#include "core/os/main_loop.h" |
| 38 |
#include "editor/debugger/script_editor_debugger.h" |
| 39 |
#include "editor/editor_main_screen.h" |
| 40 |
#include "editor/editor_node.h" |
| 41 |
#include "editor/settings/editor_settings.h" |
| 42 |
#include "scene/gui/control.h" |
| 43 |
#include "scene/main/window.h" |
| 44 |
|
| 45 |
void EmbeddedProcessMacOS::_notification(int p_what) { |
| 46 |
switch (p_what) { |
| 47 |
case NOTIFICATION_ENTER_TREE: { |
| 48 |
set_notify_transform(true); |
| 49 |
} break; |
| 50 |
case NOTIFICATION_TRANSFORM_CHANGED: |
| 51 |
case NOTIFICATION_VISIBILITY_CHANGED: { |
| 52 |
update_embedded_process(); |
| 53 |
} break; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
void EmbeddedProcessMacOS::update_embedded_process() { |
| 58 |
layer_host->set_rect(get_adjusted_embedded_window_rect(get_rect())); |
| 59 |
if (is_embedding_completed()) { |
| 60 |
ds->embed_process_update(window->get_window_id(), this); |
| 61 |
Rect2i rect = get_screen_embedded_window_rect(); |
| 62 |
script_debugger->send_message("embed:window_size", { rect.size }); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
void EmbeddedProcessMacOS::set_context_id(uint32_t p_context_id) { |
| 67 |
if (!window) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
context_id = p_context_id; |
| 72 |
|
| 73 |
_try_embed_process(); |
| 74 |
} |
| 75 |
|
| 76 |
void EmbeddedProcessMacOS::set_script_debugger(ScriptEditorDebugger *p_debugger) { |
| 77 |
script_debugger = p_debugger; |
| 78 |
layer_host->set_script_debugger(script_debugger); |
| 79 |
_try_embed_process(); |
| 80 |
} |
| 81 |
|
| 82 |
void EmbeddedProcessMacOS::embed_process(OS::ProcessID p_pid) { |
| 83 |
if (!window) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
if (current_process_id != 0) { |
| 88 |
// Stop embedding the last process. |
| 89 |
OS::get_singleton()->kill(current_process_id); |
| 90 |
} |
| 91 |
|
| 92 |
reset(); |
| 93 |
|
| 94 |
current_process_id = p_pid; |
| 95 |
embedding_state = EmbeddingState::IN_PROGRESS; |
| 96 |
// Attempt to embed the process, but if it has just started and the window is not ready yet, |
| 97 |
// we will retry in this case. |
| 98 |
_try_embed_process(); |
| 99 |
} |
| 100 |
|
| 101 |
void EmbeddedProcessMacOS::reset() { |
| 102 |
if (!ds) { |
| 103 |
ds = static_cast<DisplayServerMacOS *>(DisplayServer::get_singleton()); |
| 104 |
} |
| 105 |
if (current_process_id != 0 && is_embedding_completed()) { |
| 106 |
ds->remove_embedded_process(current_process_id); |
| 107 |
} |
| 108 |
DisplayServer *ds = DisplayServer::get_singleton(); |
| 109 |
for (int i = 0; i < DisplayServer::CURSOR_MAX; i++) { |
| 110 |
ds->cursor_set_custom_image(Ref<Resource>(), (DisplayServer::CursorShape)i, Vector2()); |
| 111 |
} |
| 112 |
if (ds->mouse_get_mode() != DisplayServer::MOUSE_MODE_VISIBLE) { |
| 113 |
ds->mouse_set_mode(DisplayServer::MOUSE_MODE_VISIBLE); |
| 114 |
} |
| 115 |
current_process_id = 0; |
| 116 |
embedding_state = EmbeddingState::IDLE; |
| 117 |
context_id = 0; |
| 118 |
script_debugger = nullptr; |
| 119 |
queue_redraw(); |
| 120 |
} |
| 121 |
|
| 122 |
void EmbeddedProcessMacOS::request_close() { |
| 123 |
if (current_process_id != 0 && is_embedding_completed()) { |
| 124 |
script_debugger->send_message("embed:win_event", { DisplayServer::WINDOW_EVENT_CLOSE_REQUEST }); |
| 125 |
} |
| 126 |
reset(); |
| 127 |
} |
| 128 |
|
| 129 |
void EmbeddedProcessMacOS::display_state_changed() { |
| 130 |
DisplayServerEmbeddedState state; |
| 131 |
state.screen_max_scale = ds->screen_get_max_scale(); |
| 132 |
state.screen_dpi = ds->screen_get_dpi(); |
| 133 |
DisplayServer::WindowID wid = window->get_window_id(); |
| 134 |
state.screen_window_scale = ds->screen_get_scale(ds->window_get_current_screen(wid)); |
| 135 |
state.display_id = ds->window_get_display_id(wid); |
| 136 |
|
| 137 |
PackedByteArray data; |
| 138 |
state.serialize(data); |
| 139 |
script_debugger->send_message("embed:ds_state", { data }); |
| 140 |
} |
| 141 |
|
| 142 |
void EmbeddedProcessMacOS::_try_embed_process() { |
| 143 |
if (current_process_id == 0 || script_debugger == nullptr || context_id == 0) { |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
DisplayServer::WindowID wid = window->get_window_id(); |
| 148 |
Error err = ds->embed_process_update(wid, this); |
| 149 |
if (err == OK) { |
| 150 |
layer_host->set_rect(get_adjusted_embedded_window_rect(get_rect())); |
| 151 |
|
| 152 |
// Replicate important DisplayServer state. |
| 153 |
display_state_changed(); |
| 154 |
|
| 155 |
Rect2i rect = get_screen_embedded_window_rect(); |
| 156 |
script_debugger->send_message("embed:window_size", { rect.size }); |
| 157 |
embedding_state = EmbeddingState::COMPLETED; |
| 158 |
queue_redraw(); |
| 159 |
emit_signal(SNAME("embedding_completed")); |
| 160 |
|
| 161 |
layer_host->grab_focus(); |
| 162 |
} else { |
| 163 |
// Another unknown error. |
| 164 |
reset(); |
| 165 |
emit_signal(SNAME("embedding_failed")); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
Rect2i EmbeddedProcessMacOS::get_adjusted_embedded_window_rect(const Rect2i &p_rect) const { |
| 170 |
Rect2i control_rect = Rect2i(p_rect.position + margin_top_left, (p_rect.size - get_margins_size()).maxi(1)); |
| 171 |
if (window_size != Size2i()) { |
| 172 |
Rect2i desired_rect; |
| 173 |
if (!keep_aspect && control_rect.size.x >= window_size.x && control_rect.size.y >= window_size.y) { |
| 174 |
// Fixed at the desired size. |
| 175 |
desired_rect.size = window_size; |
| 176 |
} else { |
| 177 |
float ratio = MIN((float)control_rect.size.x / window_size.x, (float)control_rect.size.y / window_size.y); |
| 178 |
desired_rect.size = Size2i(window_size.x * ratio, window_size.y * ratio).maxi(1); |
| 179 |
} |
| 180 |
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)); |
| 181 |
return desired_rect; |
| 182 |
} else { |
| 183 |
// Stretch, use all the control area. |
| 184 |
return control_rect; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
void EmbeddedProcessMacOS::mouse_set_mode(DisplayServer::MouseMode p_mode) { |
| 189 |
mouse_mode = p_mode; |
| 190 |
// If the mouse is anything other than visible, we must ensure the Game view is active and the layer focused. |
| 191 |
if (mouse_mode != DisplayServer::MOUSE_MODE_VISIBLE) { |
| 192 |
EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_GAME); |
| 193 |
layer_host->grab_focus(); |
| 194 |
} |
| 195 |
DisplayServer::get_singleton()->mouse_set_mode(p_mode); |
| 196 |
} |
| 197 |
|
| 198 |
EmbeddedProcessMacOS::EmbeddedProcessMacOS() : |
| 199 |
EmbeddedProcessBase() { |
| 200 |
layer_host = memnew(LayerHost(this)); |
| 201 |
add_child(layer_host); |
| 202 |
set_focus_mode(FOCUS_NONE); |
| 203 |
layer_host->set_focus_mode(FOCUS_ALL); |
| 204 |
layer_host->set_anchors_and_offsets_preset(PRESET_FULL_RECT); |
| 205 |
layer_host->set_custom_minimum_size(Size2(100, 100)); |
| 206 |
|
| 207 |
// This shortcut allows a user to forcibly release a captured mouse from within the editor, regardless of whether |
| 208 |
// the embedded process has implemented support to release the cursor. |
| 209 |
ED_SHORTCUT("game_view/release_mouse", TTRC("Release Mouse"), KeyModifierMask::ALT | Key::ESCAPE); |
| 210 |
} |
| 211 |
|
| 212 |
EmbeddedProcessMacOS::~EmbeddedProcessMacOS() { |
| 213 |
if (current_process_id != 0) { |
| 214 |
// Stop embedding the last process. |
| 215 |
OS::get_singleton()->kill(current_process_id); |
| 216 |
reset(); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
void LayerHost::_notification(int p_what) { |
| 221 |
switch (p_what) { |
| 222 |
case NOTIFICATION_MOUSE_ENTER: { |
| 223 |
DisplayServer *ds = DisplayServer::get_singleton(); |
| 224 |
for (const KeyValue<DisplayServer::CursorShape, CustomCursor> &E : custom_cursors) { |
| 225 |
ds->cursor_set_custom_image(E.value.image, E.key, E.value.hotspot); |
| 226 |
} |
| 227 |
if (script_debugger) { |
| 228 |
script_debugger->send_message("embed:win_event", { DisplayServer::WINDOW_EVENT_MOUSE_ENTER }); |
| 229 |
} |
| 230 |
if (get_window()->has_focus()) { |
| 231 |
grab_focus(); |
| 232 |
} |
| 233 |
} break; |
| 234 |
case NOTIFICATION_FOCUS_ENTER: { |
| 235 |
// Restore mouse capture, if necessary. |
| 236 |
if (!window_focused && script_debugger) { |
| 237 |
DisplayServer *ds = DisplayServer::get_singleton(); |
| 238 |
if (process->get_mouse_mode() != ds->mouse_get_mode()) { |
| 239 |
// Restore embedded process mouse mode. |
| 240 |
ds->mouse_set_mode(process->get_mouse_mode()); |
| 241 |
} |
| 242 |
script_debugger->send_message("embed:notification", { NOTIFICATION_APPLICATION_FOCUS_IN }); |
| 243 |
script_debugger->send_message("embed:win_event", { DisplayServer::WINDOW_EVENT_FOCUS_IN }); |
| 244 |
window_focused = true; |
| 245 |
} |
| 246 |
process->queue_redraw(); |
| 247 |
} break; |
| 248 |
case NOTIFICATION_MOUSE_EXIT: { |
| 249 |
DisplayServer *ds = DisplayServer::get_singleton(); |
| 250 |
for (int i = 0; i < DisplayServer::CURSOR_MAX; i++) { |
| 251 |
ds->cursor_set_custom_image(Ref<Resource>(), (DisplayServer::CursorShape)i, Vector2()); |
| 252 |
} |
| 253 |
if (script_debugger) { |
| 254 |
script_debugger->send_message("embed:win_event", { DisplayServer::WINDOW_EVENT_MOUSE_EXIT }); |
| 255 |
} |
| 256 |
} break; |
| 257 |
case NOTIFICATION_FOCUS_EXIT: { |
| 258 |
// Temporarily set mouse state back to visible, so the user can interact with the editor. |
| 259 |
if (window_focused && script_debugger) { |
| 260 |
DisplayServer *ds = DisplayServer::get_singleton(); |
| 261 |
if (ds->mouse_get_mode() != DisplayServer::MOUSE_MODE_VISIBLE) { |
| 262 |
ds->mouse_set_mode(DisplayServer::MOUSE_MODE_VISIBLE); |
| 263 |
} |
| 264 |
script_debugger->send_message("embed:win_event", { DisplayServer::WINDOW_EVENT_FOCUS_OUT }); |
| 265 |
script_debugger->send_message("embed:notification", { NOTIFICATION_APPLICATION_FOCUS_OUT }); |
| 266 |
window_focused = false; |
| 267 |
} |
| 268 |
process->queue_redraw(); |
| 269 |
} break; |
| 270 |
case MainLoop::NOTIFICATION_OS_IME_UPDATE: { |
| 271 |
if (script_debugger && has_focus()) { |
| 272 |
const String ime_text = DisplayServer::get_singleton()->ime_get_text(); |
| 273 |
const Vector2i ime_selection = DisplayServer::get_singleton()->ime_get_selection(); |
| 274 |
script_debugger->send_message("embed:ime_update", { ime_text, ime_selection }); |
| 275 |
} |
| 276 |
} break; |
| 277 |
case NOTIFICATION_EXIT_TREE: |
| 278 |
case NOTIFICATION_VISIBILITY_CHANGED: { |
| 279 |
if (!is_visible_in_tree()) { |
| 280 |
DisplayServer *ds = DisplayServer::get_singleton(); |
| 281 |
for (int i = 0; i < DisplayServer::CURSOR_MAX; i++) { |
| 282 |
ds->cursor_set_custom_image(Ref<Resource>(), (DisplayServer::CursorShape)i, Vector2()); |
| 283 |
} |
| 284 |
if (ds->mouse_get_mode() != DisplayServer::MOUSE_MODE_VISIBLE) { |
| 285 |
ds->mouse_set_mode(DisplayServer::MOUSE_MODE_VISIBLE); |
| 286 |
} |
| 287 |
} |
| 288 |
} break; |
| 289 |
case NOTIFICATION_APPLICATION_FOCUS_IN: |
| 290 |
case NOTIFICATION_WM_WINDOW_FOCUS_IN: { |
| 291 |
if (has_focus() && !window_focused && script_debugger) { |
| 292 |
DisplayServer *ds = DisplayServer::get_singleton(); |
| 293 |
if (process->get_mouse_mode() != ds->mouse_get_mode()) { |
| 294 |
// Restore embedded process mouse mode. |
| 295 |
ds->mouse_set_mode(process->get_mouse_mode()); |
| 296 |
if (process->get_mouse_mode() != DisplayServer::MOUSE_MODE_VISIBLE) { |
| 297 |
get_window()->grab_focus(); |
| 298 |
} |
| 299 |
} |
| 300 |
script_debugger->send_message("embed:notification", { NOTIFICATION_APPLICATION_FOCUS_IN }); |
| 301 |
script_debugger->send_message("embed:win_event", { DisplayServer::WINDOW_EVENT_FOCUS_IN }); |
| 302 |
window_focused = true; |
| 303 |
} |
| 304 |
} break; |
| 305 |
case NOTIFICATION_APPLICATION_FOCUS_OUT: |
| 306 |
case NOTIFICATION_WM_WINDOW_FOCUS_OUT: { |
| 307 |
if (has_focus() && window_focused && script_debugger) { |
| 308 |
DisplayServer *ds = DisplayServer::get_singleton(); |
| 309 |
if (ds->mouse_get_mode() != DisplayServer::MOUSE_MODE_VISIBLE) { |
| 310 |
ds->mouse_set_mode(DisplayServer::MOUSE_MODE_VISIBLE); |
| 311 |
} |
| 312 |
script_debugger->send_message("embed:win_event", { DisplayServer::WINDOW_EVENT_FOCUS_OUT }); |
| 313 |
script_debugger->send_message("embed:notification", { NOTIFICATION_APPLICATION_FOCUS_OUT }); |
| 314 |
window_focused = false; |
| 315 |
} |
| 316 |
} break; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
void LayerHost::cursor_set_custom_image(const Ref<Image> &p_image, DisplayServer::CursorShape p_shape, const Vector2 &p_hotspot) { |
| 321 |
custom_cursors[p_shape] = CustomCursor(p_image, p_hotspot); |
| 322 |
} |
| 323 |
|
| 324 |
void LayerHost::gui_input(const Ref<InputEvent> &p_event) { |
| 325 |
if (!process->is_embedding_completed()) { |
| 326 |
return; |
| 327 |
} |
| 328 |
|
| 329 |
if (p_event->is_pressed()) { |
| 330 |
if (ED_IS_SHORTCUT("game_view/release_mouse", p_event)) { |
| 331 |
DisplayServer *ds = DisplayServer::get_singleton(); |
| 332 |
if (ds->mouse_get_mode() != DisplayServer::MOUSE_MODE_VISIBLE) { |
| 333 |
ds->mouse_set_mode(DisplayServer::MOUSE_MODE_VISIBLE); |
| 334 |
if (script_debugger != nullptr) { |
| 335 |
script_debugger->send_message("embed:mouse_set_mode", { DisplayServer::MOUSE_MODE_VISIBLE }); |
| 336 |
} |
| 337 |
} |
| 338 |
accept_event(); |
| 339 |
return; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
Ref<InputEventJoypadMotion> jm = p_event; |
| 344 |
Ref<InputEventJoypadButton> jb = p_event; |
| 345 |
if (jm.is_valid() || jb.is_valid()) { |
| 346 |
accept_event(); |
| 347 |
return; |
| 348 |
} |
| 349 |
|
| 350 |
PackedByteArray data; |
| 351 |
if (encode_input_event(p_event, data)) { |
| 352 |
if (script_debugger != nullptr) { |
| 353 |
script_debugger->send_message("embed:event", { data }); |
| 354 |
} |
| 355 |
accept_event(); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
LayerHost::LayerHost(EmbeddedProcessMacOS *p_process) : |
| 360 |
process(p_process) {} |