| 1 |
/**************************************************************************/ |
| 2 |
/* embedded_debugger.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_debugger.h" |
| 32 |
|
| 33 |
#include "display_server_embedded.h" |
| 34 |
|
| 35 |
#include "core/debugger/engine_debugger.h" |
| 36 |
#include "core/input/input_event_codec.h" |
| 37 |
#include "core/os/main_loop.h" |
| 38 |
|
| 39 |
#ifdef DEBUG_ENABLED |
| 40 |
HashMap<String, EmbeddedDebugger::ParseMessageFunc> EmbeddedDebugger::parse_message_handlers; |
| 41 |
#endif |
| 42 |
|
| 43 |
EmbeddedDebugger::EmbeddedDebugger(DisplayServerEmbedded *p_ds) { |
| 44 |
singleton = this; |
| 45 |
|
| 46 |
#ifdef DEBUG_ENABLED |
| 47 |
ds = p_ds; |
| 48 |
if (parse_message_handlers.is_empty()) { |
| 49 |
_init_parse_message_handlers(); |
| 50 |
} |
| 51 |
EngineDebugger::register_message_capture("embed", EngineDebugger::Capture(this, EmbeddedDebugger::parse_message)); |
| 52 |
#endif |
| 53 |
} |
| 54 |
|
| 55 |
EmbeddedDebugger::~EmbeddedDebugger() { |
| 56 |
singleton = nullptr; |
| 57 |
} |
| 58 |
|
| 59 |
void EmbeddedDebugger::initialize(DisplayServerEmbedded *p_ds) { |
| 60 |
if (EngineDebugger::is_active()) { |
| 61 |
memnew(EmbeddedDebugger(p_ds)); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
void EmbeddedDebugger::deinitialize() { |
| 66 |
if (singleton) { |
| 67 |
memdelete(singleton); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
#ifdef DEBUG_ENABLED |
| 72 |
void EmbeddedDebugger::_init_parse_message_handlers() { |
| 73 |
parse_message_handlers["window_size"] = &EmbeddedDebugger::_msg_window_size; |
| 74 |
parse_message_handlers["mouse_set_mode"] = &EmbeddedDebugger::_msg_mouse_set_mode; |
| 75 |
parse_message_handlers["event"] = &EmbeddedDebugger::_msg_event; |
| 76 |
parse_message_handlers["win_event"] = &EmbeddedDebugger::_msg_win_event; |
| 77 |
parse_message_handlers["notification"] = &EmbeddedDebugger::_msg_notification; |
| 78 |
parse_message_handlers["ime_update"] = &EmbeddedDebugger::_msg_ime_update; |
| 79 |
parse_message_handlers["ds_state"] = &EmbeddedDebugger::_msg_ds_state; |
| 80 |
} |
| 81 |
|
| 82 |
Error EmbeddedDebugger::_msg_window_size(const Array &p_args) { |
| 83 |
ERR_FAIL_COND_V_MSG(p_args.size() != 1, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'window_size' message."); |
| 84 |
Size2i size = p_args[0]; |
| 85 |
ds->_window_set_size(size); |
| 86 |
return OK; |
| 87 |
} |
| 88 |
|
| 89 |
Error EmbeddedDebugger::_msg_mouse_set_mode(const Array &p_args) { |
| 90 |
ERR_FAIL_COND_V_MSG(p_args.size() != 1, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'mouse_set_mode' message."); |
| 91 |
DisplayServer::MouseMode mode = p_args[0]; |
| 92 |
ds->mouse_set_mode(mode); |
| 93 |
return OK; |
| 94 |
} |
| 95 |
|
| 96 |
Error EmbeddedDebugger::_msg_event(const Array &p_args) { |
| 97 |
ERR_FAIL_COND_V_MSG(p_args.size() != 1, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'event' message."); |
| 98 |
Input *input = Input::get_singleton(); |
| 99 |
if (!input) { |
| 100 |
// Ignore if we've received an event before the process has initialized. |
| 101 |
return OK; |
| 102 |
} |
| 103 |
|
| 104 |
PackedByteArray data = p_args[0]; |
| 105 |
Ref<InputEvent> event; |
| 106 |
decode_input_event(data, event); |
| 107 |
|
| 108 |
{ |
| 109 |
Ref<InputEventMouse> e = event; |
| 110 |
if (e.is_valid()) { |
| 111 |
input->set_mouse_position(e->get_position()); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
{ |
| 116 |
Ref<InputEventMagnifyGesture> e = event; |
| 117 |
if (e.is_valid()) { |
| 118 |
input->set_mouse_position(e->get_position()); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
{ |
| 123 |
Ref<InputEventPanGesture> e = event; |
| 124 |
if (e.is_valid()) { |
| 125 |
input->set_mouse_position(e->get_position()); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
if (event.is_valid()) { |
| 130 |
input->parse_input_event(event); |
| 131 |
} |
| 132 |
|
| 133 |
return OK; |
| 134 |
} |
| 135 |
|
| 136 |
Error EmbeddedDebugger::_msg_win_event(const Array &p_args) { |
| 137 |
ERR_FAIL_COND_V_MSG(p_args.size() != 1, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'win_event' message."); |
| 138 |
DisplayServer::WindowEvent win_event = p_args[0]; |
| 139 |
ds->send_window_event(win_event, DisplayServer::MAIN_WINDOW_ID); |
| 140 |
if (win_event == DisplayServer::WindowEvent::WINDOW_EVENT_MOUSE_EXIT) { |
| 141 |
Input::get_singleton()->release_pressed_events(); |
| 142 |
} |
| 143 |
return OK; |
| 144 |
} |
| 145 |
|
| 146 |
Error EmbeddedDebugger::_msg_ime_update(const Array &p_args) { |
| 147 |
ERR_FAIL_COND_V_MSG(p_args.size() != 2, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'ime_update' message."); |
| 148 |
String ime_text = p_args[0]; |
| 149 |
Vector2i ime_selection = p_args[1]; |
| 150 |
ds->update_im_text(ime_selection, ime_text); |
| 151 |
return OK; |
| 152 |
} |
| 153 |
|
| 154 |
Error EmbeddedDebugger::_msg_notification(const Array &p_args) { |
| 155 |
ERR_FAIL_COND_V_MSG(p_args.size() != 1, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'notification' message."); |
| 156 |
int notification = p_args[0]; |
| 157 |
if (OS::get_singleton()->get_main_loop()) { |
| 158 |
OS::get_singleton()->get_main_loop()->notification(notification); |
| 159 |
} |
| 160 |
return OK; |
| 161 |
} |
| 162 |
|
| 163 |
Error EmbeddedDebugger::_msg_ds_state(const Array &p_args) { |
| 164 |
ERR_FAIL_COND_V_MSG(p_args.size() != 1, ERR_INVALID_PARAMETER, "Invalid number of arguments for 'ds_state' message."); |
| 165 |
PackedByteArray data = p_args[0]; |
| 166 |
DisplayServerEmbeddedState state; |
| 167 |
state.deserialize(data); |
| 168 |
ds->set_state(state); |
| 169 |
return OK; |
| 170 |
} |
| 171 |
|
| 172 |
Error EmbeddedDebugger::parse_message(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured) { |
| 173 |
EmbeddedDebugger *self = static_cast<EmbeddedDebugger *>(p_user); |
| 174 |
r_captured = true; |
| 175 |
|
| 176 |
ParseMessageFunc *fn_ptr = parse_message_handlers.getptr(p_msg); |
| 177 |
if (fn_ptr) { |
| 178 |
return (self->**fn_ptr)(p_args); |
| 179 |
} else { |
| 180 |
// Any other messages with this prefix should be ignored. |
| 181 |
WARN_PRINT("Unknown message: " + p_msg); |
| 182 |
return ERR_SKIP; |
| 183 |
} |
| 184 |
} |
| 185 |
#endif |