// ===================================================================================================================== // fennec, a free and open source game engine // Copyright © 2025 Medusa Slockbower // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // ===================================================================================================================== #include #include #include #include #include #include #define FENNEC_EVENT_QUEUE_SIZE 8192 namespace fennec { static mutex lock; static dynarray> listeners; static mpscq queue(FENNEC_EVENT_QUEUE_SIZE); void event::handle_events() { lock_guard guard(lock); // if we don't copy into a temporary buffer, we could deadlock ourselves // if someone unwittingly calls dispatch() in an event listener. dynarray> dispatch(FENNEC_EVENT_QUEUE_SIZE); // we query the size instead of empty just in case someone decides to run their own thread // and send events from that thread. size_t n = queue.size(); for (size_t i = 0; i < n; ++i) { dispatch[i] = queue.pop(); } for (size_t i = 0; i < n; ++i) { _handle_event(dispatch[i]); } } void event::_add_listener(event_listener* listener, uint64_t type) { lock_guard guard(lock); if (listeners.size() <= type) { listeners.resize(type + 1); } listeners[type].insert(listener); } void event::remove_listener(event_listener* listener) { lock_guard guard(lock); for (auto& it : listeners) { it.erase(listener); } } void event::_handle_event(unique_ptr& event) { auto type = event->get_type().id(); for (auto& listener : listeners[type]) { listener->handle_event(event.get()); } } void event::_dispatch(unique_ptr&& event) { // queue is taking ownership of the resource queue.emplace(event.release()); } void event::_dispatch_immediate(unique_ptr&& event) { lock_guard guard(lock); _handle_event(event); } }