| 1 | /**************************************************************************/ |
| 2 | /* godot_instance.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 "godot_instance.h" |
| 32 | |
| 33 | #include "core/extension/gdextension_manager.h" |
| 34 | #include "core/os/main_loop.h" |
| 35 | #include "main/main.h" |
| 36 | #include "servers/display/display_server.h" |
| 37 | |
| 38 | void GodotInstance::_bind_methods() { |
| 39 | ClassDB::bind_method(D_METHOD("start"), &GodotInstance::start); |
| 40 | ClassDB::bind_method(D_METHOD("is_started"), &GodotInstance::is_started); |
| 41 | ClassDB::bind_method(D_METHOD("iteration"), &GodotInstance::iteration); |
| 42 | ClassDB::bind_method(D_METHOD("focus_in"), &GodotInstance::focus_in); |
| 43 | ClassDB::bind_method(D_METHOD("focus_out"), &GodotInstance::focus_out); |
| 44 | ClassDB::bind_method(D_METHOD("pause"), &GodotInstance::pause); |
| 45 | ClassDB::bind_method(D_METHOD("resume"), &GodotInstance::resume); |
| 46 | } |
| 47 | |
| 48 | GodotInstance::GodotInstance() { |
| 49 | } |
| 50 | |
| 51 | GodotInstance::~GodotInstance() { |
| 52 | } |
| 53 | |
| 54 | bool GodotInstance::initialize(GDExtensionInitializationFunction p_init_func) { |
| 55 | print_verbose("Godot Instance initialization"); |
| 56 | GDExtensionManager *gdextension_manager = GDExtensionManager::get_singleton(); |
| 57 | GDExtensionConstPtr<const GDExtensionInitializationFunction> ptr((const GDExtensionInitializationFunction *)&p_init_func); |
| 58 | GDExtensionManager::LoadStatus status = gdextension_manager->load_extension_from_function("libgodot://main", ptr); |
| 59 | return status == GDExtensionManager::LoadStatus::LOAD_STATUS_OK; |
| 60 | } |
| 61 | |
| 62 | bool GodotInstance::start() { |
| 63 | print_verbose("GodotInstance::start()"); |
| 64 | Error err = Main::setup2(); |
| 65 | if (err != OK) { |
| 66 | return false; |
| 67 | } |
| 68 | started = Main::start() == EXIT_SUCCESS; |
| 69 | if (started) { |
| 70 | OS::get_singleton()->get_main_loop()->initialize(); |
| 71 | } |
| 72 | return started; |
| 73 | } |
| 74 | |
| 75 | bool GodotInstance::is_started() { |
| 76 | return started; |
| 77 | } |
| 78 | |
| 79 | bool GodotInstance::iteration() { |
| 80 | DisplayServer::get_singleton()->process_events(); |
| 81 | return Main::iteration(); |
| 82 | } |
| 83 | |
| 84 | void GodotInstance::stop() { |
| 85 | print_verbose("GodotInstance::stop()"); |
| 86 | if (started) { |
| 87 | OS::get_singleton()->get_main_loop()->finalize(); |
| 88 | } |
| 89 | started = false; |
| 90 | } |
| 91 | |
| 92 | void GodotInstance::focus_out() { |
| 93 | print_verbose("GodotInstance::focus_out()"); |
| 94 | if (started) { |
| 95 | if (OS::get_singleton()->get_main_loop()) { |
| 96 | OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void GodotInstance::focus_in() { |
| 102 | print_verbose("GodotInstance::focus_in()"); |
| 103 | if (started) { |
| 104 | if (OS::get_singleton()->get_main_loop()) { |
| 105 | OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void GodotInstance::pause() { |
| 111 | print_verbose("GodotInstance::pause()"); |
| 112 | if (started) { |
| 113 | if (OS::get_singleton()->get_main_loop()) { |
| 114 | OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_PAUSED); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void GodotInstance::resume() { |
| 120 | print_verbose("GodotInstance::resume()"); |
| 121 | if (started) { |
| 122 | if (OS::get_singleton()->get_main_loop()) { |
| 123 | OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_RESUMED); |
| 124 | } |
| 125 | } |
| 126 | } |