Compare commits

...

2 Commits

Author SHA1 Message Date
ecf1cfc29c - fleshing out event system and window manager
- added tests back in for window management
2025-12-27 07:19:53 -05:00
184bc7fcdf - events tested and implemented multithreading support through mpscq 2025-12-23 12:24:23 -05:00
20 changed files with 396 additions and 82 deletions

View File

@@ -137,6 +137,11 @@ public:
return *_table[i];
}
constexpr bool operator()(size_t i) const {
assert(i < capacity(), "Index out of Bounds!");
return _table[i];
}
/// @}
@@ -205,7 +210,7 @@ public:
/// \brief C++ Iterator Specification `begin()`
/// \returns an iterator at the start of the object pool
const_iterator begin() const {
return iterator(this, 0);
return const_iterator(this, 0);
}
///
@@ -218,7 +223,7 @@ public:
/// \brief C++ Iterator Specification `end()`
/// \returns an iterator at the start of the end of the object pool
const_iterator end() const {
return iterator(this, _size);
return const_iterator(this, _size);
}
///

View File

@@ -33,12 +33,17 @@ struct event;
/// \brief Class outlining the interface for an object that listens for events
class event_listener {
public:
virtual ~event_listener() = default;
virtual ~event_listener();
///
/// \brief event handler callback
/// \param event the event to handle
virtual void handle_event(event* event) = 0;
#ifndef FENNEC_DOXYGEN
FENNEC_RTTI_CLASS_ENABLE() {
}
#endif
};
///
@@ -46,36 +51,38 @@ public:
struct event {
virtual ~event() = default;
static void handle_events();
///
/// \brief Registers a listener for the event type
/// \tparam EventT the event type
/// \param listener the listener to register
template<typename EventT>
static void add_listener(event_listener* listener) {
event::add_listener(listener, typeuuid<EventT, event>());
event::_add_listener(listener, type::get<EventT>().id());
}
///
/// \brief Add a listener for an event type of \f$type\f$
/// \param listener the listener to add
/// \param type the event type to listen for
static void add_listener(event_listener* listener, uint64_t type);
///
/// \brief removes a listener from the event system
/// \param listener the listener to remove
static void remove_listener(event_listener* listener);
template<typename EventT, typename...ArgsT>
static void dispatch_immediate(ArgsT&&...args) {
static void dispatch(ArgsT&&...args) {
event::_dispatch(fennec::make_unique<EventT>(fennec::forward<ArgsT>(args)...));
}
template<typename EventT, typename...ArgsT>
static void dispatch_next_tick(ArgsT&&...args) {
static void dispatch_immediate(ArgsT&&...args) {
event::_dispatch_immediate(fennec::make_unique<EventT>(fennec::forward<ArgsT>(args)...));
}
private:
static void _add_listener(event_listener* listener, uint64_t type);
static void _handle_event(unique_ptr<event>& event);
static void _dispatch(unique_ptr<event>&& event);
static void _dispatch_immediate(unique_ptr<event>&& event);
#ifndef FENNEC_DOXYGEN
FENNEC_RTTI_CLASS_ENABLE() {
}

View File

@@ -31,6 +31,8 @@
#ifndef FENNEC_LANG_ASSERT_H
#define FENNEC_LANG_ASSERT_H
#include <fennec/lang/types.h>
///
/// \page fennec_lang_assert Assertions
///
@@ -69,7 +71,19 @@
#endif
#ifndef FENNEC_DOXYGEN
void _assert_impl(const char* expression, const char* file, int line, const char* function, const char* desc, bool halt);
void _assert_impl(const char* expr, size_t expr_l,
const char* file, size_t file_l, int line,
const char* func, size_t func_l,
const char* desc, bool halt);
template<size_t ExprL, size_t FileL, size_t FuncL>
void _assert(const char (&expr)[ExprL],
const char (&file)[FileL], int line,
const char (&func)[FuncL],
const char* desc,
bool halt) {
::_assert_impl(expr, ExprL, file, FileL, line, func, FuncL, desc, halt);
}
#endif
///
@@ -78,7 +92,7 @@ void _assert_impl(const char* expression, const char* file, int line, const char
/// \param description the description of the assertion
#define assert(expression, description) \
if(not(expression)) [[unlikely]] { \
_assert_impl(#expression, __FILE__, __LINE__, __PRETTY_FUNCTION__, description, not FENNEC_RELEASE); \
_assert(#expression, __FILE__, __LINE__, __PRETTY_FUNCTION__, description, not FENNEC_RELEASE); \
}
///
@@ -87,7 +101,7 @@ void _assert_impl(const char* expression, const char* file, int line, const char
/// \param description the description of the assertion
#define assertf(expression, description) \
if(not(expression)) [[unlikely]] { \
_assert_impl(#expression, __FILE__, __LINE__, __PRETTY_FUNCTION__, description, true); \
_assert(#expression, __FILE__, __LINE__, __PRETTY_FUNCTION__, description, true); \
}
///

View File

@@ -117,13 +117,20 @@ public:
}
///
/// \brief Move Constructor, transfers ownership from \f$other\f$
/// \param other The unique_ptr to take ownership from
constexpr unique_ptr(unique_ptr&& other)
: _handle(other._handle) {
other._handle = nullptr;
}
///
/// \brief Move Constructor, transfers ownership from \f$other\f$
/// \param other The unique_ptr to take ownership from
template<typename DerivedT> requires(is_base_of_v<TypeT, DerivedT>)
constexpr unique_ptr(unique_ptr<DerivedT>&& other)
: _handle(other.release()) {
}
// Delete copy constructor
constexpr unique_ptr(const unique_ptr&) = delete;
@@ -205,6 +212,9 @@ public:
return _handle != nullptr;
}
///
/// \brief dereference operator
/// \returns a reference to the held value
element_t& operator*() const {
return *_handle;
}
@@ -225,6 +235,11 @@ unique_ptr<TypeT> make_unique(ArgsT&&...args) {
return unique_ptr<TypeT>(new TypeT(fennec::forward<ArgsT>(args)...));
}
template<typename TypeT>
unique_ptr<TypeT> make_unique(TypeT* ptr) {
return unique_ptr<TypeT>(ptr);
}
}
#endif // FENNEC_MEMORY_POINTERS_H

View File

@@ -87,18 +87,20 @@ public:
///
/// \brief shared object release
/// \param obj the shared object to release
virtual void unload_object(shared_object* obj) = 0;
virtual void unload_object(shared_object* obj) = 0;
///
/// \brief shared object symbol locator
/// \param obj the shared object to search
/// \param name the name of the symbol to search for
/// \returns a reference to the symbol
virtual symbol find_symbol(shared_object* obj, const cstring& name) = 0;
virtual symbol find_symbol(shared_object* obj, const cstring& name) = 0;
virtual void initialize(); //!< Initialize Drivers and Contexts
virtual void shutdown(); //!< Close Drivers and Contexts
virtual window_manager& get_window_manager() const { return *wmanager; }
protected:
unique_ptr<window_manager> wmanager; //!< the window manager

View File

@@ -336,9 +336,9 @@ public:
protected:
display_server* const server; //!< the display server the window belongs to
window* const parent; //!< the parent window
window* root; //!< the nearest top-level window in the hierarchy
config cfg; //!< the current configuration
state state; //!< the current state
window* root; //!< the nearest top-level window in the hierarchy
unique_ptr<gfxsurface> gfx_surface; //!< the corresponding graphics surface
};

View File

@@ -38,28 +38,23 @@
#include <fennec/containers/object_pool.h>
#include <fennec/platform/interface/window.h>
#include <fennec/threading/lock_guard.h>
#include <fennec/threading/mpscq.h>
#include <fennec/threading/mutex.h>
#include <fennec/threading/thread.h>
namespace fennec
{
using window_id = size_t; //!< type representing an id for a window
///
/// \brief class for handling display servers and windows
class window_manager {
// Definitions =========================================================================================================
public:
using window_id = size_t; //!< type representing an id for a window
static constexpr window_id nullid = -1; //!< constant representing a null window
private:
using server_t = unique_ptr<display_server>;
using window_t = unique_ptr<window>;
using window_pool_t = object_pool<window_t>;
using config = window::config;
enum command_ : uint8_t {
command_set_flag = 0,
command_resize
@@ -71,6 +66,14 @@ private:
void* data;
};
static constexpr size_t max_commands = 512;
using server_t = unique_ptr<display_server>;
using window_t = unique_ptr<window>;
using window_pool_t = object_pool<window_t>;
using command_queue_t = mpscq<command>;
using config = window::config;
// Constructors & Destructor ===========================================================================================
public:
@@ -96,6 +99,7 @@ public:
window_id create_window(const config& config, window_id parent = nullid);
void begin(window_id window);
void end(window_id window);
void close(window_id window);
// Thread-Safe Functions ===============================================================================================
@@ -111,11 +115,11 @@ public:
ivec2 get_size(window_id window) const {
lock_guard guard(_lock);
return _size(window);
return _get_size(window);
}
ivec2 get_position(window_id window) const {
lock_guard guard(_lock);
return _position(window);
return _get_position(window);
}
bool is_visible(window_id window) const { lock_guard guard(_lock); return _check_state(window, window::state_visible); }
@@ -151,18 +155,20 @@ public:
private:
mutable mutex _lock;
thread::id _thread;
platform* _platform;
server_t _display;
window_pool_t _windows;
mutable mutex _lock;
thread::id _thread;
platform* _platform;
server_t _display;
window_pool_t _windows;
command_queue_t _commands;
window_id _parent(window_id id) const;
window_id _root(window_id id) const;
ivec2 _size(window_id id) const;
ivec2 _position(window_id id) const;
ivec2 _get_size(window_id id) const;
ivec2 _get_position(window_id id) const;
bool _check_state(window_id id, uint8_t state) const;

View File

@@ -81,9 +81,10 @@ public:
}
///
/// \brief Buffer Constructor, wraps the provided C-Style string
/// \param str the buffer to wrap
/// \param n the number of characters in the buffer plus the null terminator
///
/// \note If used with `::strlen`, the result should be incremented by 1 to include the null terminator
constexpr cstring(char* str, size_t n)
: _str(str)
, _size(n - 1)
@@ -94,7 +95,6 @@ public:
}
///
/// \brief Buffer Constructor, wraps the provided C-Style string
/// \param str the buffer to wrap
/// \tparam n the number of characters in the buffer plus the null terminator
template<size_t n>
@@ -108,9 +108,10 @@ public:
}
///
/// \brief Const Buffer Constructor, wraps the provided C-Style string
/// \param str the buffer to wrap
/// \param n the number of characters in the buffer plus the null terminator
///
/// \note If used with `::strlen`, the result should be incremented by 1 to include the null terminator
constexpr cstring(const char* str, size_t n)
: _cstr(str)
, _size(n - 1)

View File

@@ -19,14 +19,45 @@
#include <fennec/containers/dynarray.h>
#include <fennec/containers/set.h>
#include <fennec/core/event.h>
#include <fennec/threading/lock_guard.h>
#include <fennec/threading/mpscq.h>
#include <fennec/threading/mutex.h>
#define FENNEC_EVENT_QUEUE_SIZE 8192
namespace fennec
{
static mutex lock;
static dynarray<set<event_listener*>> listeners;
static mpscq<event> queue(FENNEC_EVENT_QUEUE_SIZE);
event_listener::~event_listener() {
event::remove_listener(this);
}
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<unique_ptr<event>> 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);
void event::add_listener(event_listener* listener, uint64_t type) {
if (listeners.size() <= type) {
listeners.resize(type + 1);
}
@@ -34,9 +65,29 @@ void event::add_listener(event_listener* listener, uint64_t type) {
}
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>& event) {
auto type = event->get_type().id();
for (auto& listener : listeners[type]) {
listener->handle_event(event.get());
}
}
void event::_dispatch(unique_ptr<event>&& event) {
// queue is taking ownership of the resource
queue.emplace(event.release());
}
void event::_dispatch_immediate(unique_ptr<event>&& event) {
lock_guard guard(lock);
_handle_event(event);
}
}

View File

@@ -16,17 +16,24 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =====================================================================================================================
#include <stdio.h>
#include <fennec/core/logger.h>
#include <cpptrace/cpptrace.hpp>
void _assert_callback(const char* expression, const char* file, int line, const char* function, const char* description)
void _assert_callback(const fennec::cstring& expr, const fennec::cstring& file, int line, const fennec::cstring& func, const fennec::cstring& desc)
{
// Skip
// __assert_callback
// __assert_impl
printf("Assert failed: \"%s\" \n"
fennec::logger::log(fennec::format(""
"Assert failed: \"{}\" \n"
"At {}:{} in {} \n"
"Description: {} \n"
"Stacktrace: \n",
expr, file, line, func, desc
));
/*printf("Assert failed: \"%s\" \n"
"At %s:%d in %s \n"
"Description: %s \n",
expression, file, line, function, description);
expression, file, line, function, description);*/
cpptrace::generate_trace(2).print();
}

View File

@@ -17,12 +17,21 @@
// =====================================================================================================================
#include <fennec/lang/detail/_stdlib.h>
#include <fennec/string/cstring.h>
void _assert_callback(const char* expression, const char* file, int line, const char* function, const char* description);
void _assert_callback(const fennec::cstring& expr, const fennec::cstring& file, int line, const fennec::cstring& func, const fennec::cstring& desc);
void _assert_impl(const char* expression, const char* file, int line, const char* function, const char* description, bool halt)
void _assert_impl(const char* expr, size_t expr_l,
const char* file, size_t file_l, int line,
const char* func, size_t func_l,
const char* desc, bool halt)
{
_assert_callback(expression, file, line, function, description);
_assert_callback(
fennec::cstring(expr, expr_l),
fennec::cstring(file, file_l), line,
fennec::cstring(func, func_l),
fennec::cstring(desc, ::strlen(desc) + 1)
);
if (halt) {
::abort();

View File

@@ -24,9 +24,14 @@ namespace fennec
{
window::window(display_server* server, const config& conf, window* parent)
: server(server), parent(parent)
, cfg(conf), state(), root(nullptr) {
: server(server), parent(parent), root(parent ? parent : this)
, cfg(conf), state() {
state.mode = conf.mode;
while (root != nullptr and root->is_popup()) {
root = root->get_parent();
}
assertf(root != nullptr, "Failed to find appropriate top-level window.");
}
window::~window() {

View File

@@ -77,14 +77,6 @@ void wayland_window::initialize() {
return;
}
root = parent;
while (root != nullptr and root->is_popup()) {
root = static_cast<wayland_window*>(root->get_parent());
}
assertf(root != nullptr, "Failed to find appropriate top-level window.");
wayland_server* wl_server = static_cast<wayland_server*>(server);
surface = wl_compositor_create_surface(wl_server->compositor);

View File

@@ -28,8 +28,8 @@
namespace fennec {
window_manager::window_manager(platform* platform)
: _platform(platform) {
}
: _platform(platform)
, _commands(max_commands) {}
window_manager::~window_manager() {
shutdown();
@@ -70,6 +70,7 @@ void window_manager::shutdown() {
}
assertf(_thread == thread::current(), "Attempted to shutdown Window Manager on a different thread!");
lock_guard guard(_lock);
// Cleanup Windows
for (auto& window : _windows) {
@@ -84,31 +85,97 @@ void window_manager::shutdown() {
}
void window_manager::dispatch() {
assertf(_thread == thread::current(), "Attempted to dispatch Window Manager on a different thread!");
if (not _display) {
return;
}
assertf(_thread == thread::current(), "Attempted to dispatch Window Manager on a different thread!");
lock_guard guard(_lock);
_display->dispatch();
}
window_manager::window_id window_manager::create_window(const window::config& config, window_id parent) {
assertf(_thread == thread::current(), "Attempted to create a window on a different thread!");
window_id window_manager::create_window(const window::config& config, window_id parent) {
if (not _display) {
return nullid;
}
assertf(_thread == thread::current(), "Attempted to create a window on a different thread!");
lock_guard guard(_lock);
window* p = parent == nullid ? nullptr : _windows[parent].get();
return _windows.emplace(_display->create_window(config, p));
window_id id = _windows.emplace(_display->create_window(config, p));
_windows[id]->initialize();
return id;
}
void window_manager::begin(window_id window) {
assertf(_thread == thread::current(), "Attempted to set window context on a different thread!");
if (not _display) {
return;
}
assertf(_thread == thread::current(), "Attempted to set window context on a different thread!");
lock_guard guard(_lock);
_windows[window]->begin_frame();
}
void window_manager::end(window_id window) {
assertf(_thread == thread::current(), "Attempted to set window context on a different thread!");
if (not _display) {
return;
}
assertf(_thread == thread::current(), "Attempted to set window context on a different thread!");
lock_guard guard(_lock);
_windows[window]->end_frame();
}
void window_manager::close(window_id window) {
if (not _display) {
return;
}
assertf(_thread == thread::current(), "Attempted to set window context on a different thread!");
lock_guard guard(_lock);
_windows[window]->shutdown();
_windows[window] = nullptr;
_windows.erase(window);
}
window_id window_manager::_parent(window_id) const {
return nullid;
}
window_id window_manager::_root(window_id) const {
return nullid;
}
ivec2 window_manager::_get_size(window_id id) const {
return _windows[id]->get_size();
}
ivec2 window_manager::_get_position(window_id id) const {
return _windows[id]->get_position();
}
bool window_manager::_check_state(window_id id, uint8_t state) const {
switch (state) {
case window::state_running: return _windows[id]->is_running();
case window::state_child: return _windows[id]->is_child();
case window::state_suspended: return _windows[id]->is_suspended();
case window::state_visible: return _windows[id]->is_visible();
default: return false;
}
}
bool window_manager::_get_flag(window_id id, uint8_t flag) const {
return _windows[id]->get_flag(flag);
}
bool window_manager::_set_flag(window_id, uint8_t, bool) {
return false;
}
} // fennec

View File

@@ -9,6 +9,8 @@ add_executable(fennec-test
tests/lang/test_metaprogramming.h
tests/lang/test_function.h
tests/test_threading.h
tests/test_core.h
tests/core/test_event.h
)
target_compile_definitions(fennec-test PUBLIC FENNEC_TEST_CWD="${CMAKE_SOURCE_DIR}/bin/${FENNEC_BUILD_NAME}"

View File

@@ -22,6 +22,7 @@
#include "test.h"
#include "tests/test_containers.h"
#include "tests/test_core.h"
#include "tests/test_string.h"
#include "tests/test_format.h"
#include "tests/test_filesystem.h"
@@ -77,6 +78,11 @@ int main(int, char **)
fennec::test::fennec_test_rtti();
fennec_test_spacer(3);
fennec_test_header("core library");
fennec_test_spacer(2);
fennec::test::fennec_test_core();
fennec_test_spacer(3);
fennec_test_header("platform library");
fennec_test_spacer(2);
fennec::test::fennec_test_platform();

View File

@@ -79,7 +79,7 @@ inline std::ostream& operator<<(std::ostream& os, const path& str) {
// Helper for printing types
inline std::ostream& operator<<(std::ostream& os, const type& t) {
return os << t.name();
return os << t.name().cstr();
}
}

View File

@@ -0,0 +1,76 @@
// =====================================================================================================================
// 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 <https://www.gnu.org/licenses/>.
// =====================================================================================================================
///
/// \file test_event.h
/// \brief
///
///
/// \details
/// \author Medusa Slockbower
///
/// \copyright Copyright © 2025 Medusa Slockbower ([GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html))
///
///
#ifndef FENNEC_TEST_CORE_EVENT_H
#define FENNEC_TEST_CORE_EVENT_H
#include "../../test.h"
#include <fennec/core/event.h>
namespace fennec::test
{
struct test_event : public event {
FENNEC_RTTI_CLASS_ENABLE(event) {
}
};
struct test_listener : public event_listener {
bool handled = { false };
void handle_event(event* event) override {
handled = true;
fennec_test_run(event->get_type(), type::get<test_event>());
}
FENNEC_RTTI_CLASS_ENABLE(event_listener) {
}
};
inline void fennec_test_core_event() {
test_listener listener;
event::add_listener<test_event>(&listener);
event::dispatch<test_event>();
event::handle_events();
fennec_test_run(listener.handled, true);
listener.handled = false;
event::dispatch_immediate<test_event>();
fennec_test_run(listener.handled, true);
}
}
#endif // FENNEC_TEST_CORE_EVENT_H

52
test/tests/test_core.h Normal file
View File

@@ -0,0 +1,52 @@
// =====================================================================================================================
// 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 <https://www.gnu.org/licenses/>.
// =====================================================================================================================
///
/// \file test_core.h
/// \brief
///
///
/// \details
/// \author Medusa Slockbower
///
/// \copyright Copyright © 2025 Medusa Slockbower ([GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html))
///
///
#ifndef FENNEC_TEST_CORE_H
#define FENNEC_TEST_CORE_H
#include "../test.h"
#include "core/test_event.h"
namespace fennec::test
{
inline void fennec_test_core() {
fennec_test_subheader("events");
fennec_test_spacer(2);
fennec_test_core_event();
fennec_test_spacer(3);
}
}
#endif // FENNEC_TEST_CORE_H

View File

@@ -22,7 +22,7 @@
#include "../test.h"
#include <fennec/platform/interface/platform.h>
#include <fennec/platform/window_manager.h>
namespace fennec::test
{
@@ -32,14 +32,12 @@ inline void fennec_test_platform() {
platform* platform = platform::instance();
platform->initialize();
/*
display_server* display = platform->get_display_server();
window_manager& wm = platform->get_window_manager();
window* window = display->create_window(window::config {
window_id window = wm.create_window(window::config {
.title = string("fennec-test"),
.flags = { },
.mode = window::mode_windowed,
.parent = window::nullid,
.rect = {
.position = { 0, 0 },
.size = { 720, 480 },
@@ -48,19 +46,18 @@ inline void fennec_test_platform() {
.accessibility = { string("test window"), string("test window description") }
});
assertf(window != nullptr, "Failed to create test window.");
assertf(window != window_manager::nullid, "Failed to create test window.");
window->initialize();
while (window->is_running()) {
window->begin_frame();
window->end_frame();
display->dispatch();
while (wm.is_running(window)) {
wm.begin(window);
wm.end(window);
wm.dispatch();
}
window->shutdown();
*/
wm.close(window);
platform::instance()->shutdown();