Initial host event manager.

This commit is contained in:
Maxim Poliakovski 2023-04-01 17:20:53 +02:00
parent 49f47c5f3f
commit f1c898b17e
5 changed files with 177 additions and 7 deletions

View File

@ -5,4 +5,4 @@ include_directories("${PROJECT_SOURCE_DIR}"
file(GLOB SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
add_library(core OBJECT ${SOURCES})
target_link_libraries(core)
target_link_libraries(core PRIVATE SDL2::SDL2)

65
core/hostevents.cpp Normal file
View File

@ -0,0 +1,65 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-23 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
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/>.
*/
#include <core/hostevents.h>
#include <cpu/ppc/ppcemu.h>
#include <loguru.hpp>
#include <SDL.h>
EventManager* EventManager::event_manager;
void EventManager::poll_events()
{
SDL_Event event;
while (SDL_PollEvent(&event)) {
events_captured++;
switch (event.type) {
case SDL_QUIT:
power_on = false;
break;
case SDL_WINDOWEVENT: {
WindowEvent we;
we.sub_type = event.window.event;
we.window_id = event.window.windowID;
this->post_event(we);
}
break;
case SDL_KEYDOWN:
key_downs++;
break;
case SDL_KEYUP:
key_ups++;
break;
case SDL_MOUSEMOTION:
mouse_motions++;
break;
default:
unhandled_events++;
}
}
}

94
core/hostevents.h Normal file
View File

@ -0,0 +1,94 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-23 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
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/>.
*/
#ifndef EVENT_MANAGER_H
#define EVENT_MANAGER_H
#include <cinttypes>
#include <functional>
#include <list>
#include <map>
#include <utility>
enum EventType : uint16_t {
EVENT_UNKNOWN = 0,
EVENT_WINDOW = 100,
EVENT_KEYBOARD = 200,
EVENT_MOUSE = 300,
};
class BaseEvent {
public:
BaseEvent(EventType type) { this->type = type; };
EventType type;
};
class WindowEvent : public BaseEvent {
public:
WindowEvent() : BaseEvent(EventType::EVENT_WINDOW) {};
uint16_t sub_type;
uint32_t window_id;
};
class EventManager {
public:
static EventManager* get_instance() {
if (!event_manager) {
event_manager = new EventManager();
}
return event_manager;
};
using EventHandler = std::function<void(const BaseEvent&)>;
void poll_events();
void register_handler(const EventType event_type, EventHandler&& eh) {
this->_handlers[event_type].emplace_back(std::move(eh));
};
void post_event(const BaseEvent& event) {
auto type = event.type;
if( _handlers.find(type) == _handlers.end() )
return;
auto&& handlers = _handlers.at(type);
for(auto&& handler : handlers)
handler(event);
}
private:
static EventManager* event_manager;
EventManager() {}; // private constructor to implement a singleton
std::map<EventType, std::list<EventHandler>> _handlers;
uint64_t events_captured = 0;
uint64_t unhandled_events = 0;
uint64_t key_downs = 0;
uint64_t key_ups = 0;
uint64_t mouse_motions = 0;
};
#endif // EVENT_MANAGER_H

View File

@ -36,9 +36,11 @@ using namespace std;
#define NS_PER_SEC 1E9
#define USEC_PER_SEC 1E6
#define NS_PER_USEC 1000UL
#define NS_PER_MSEC 1E6
#define ONE_BILLION_NS 0x3B9ACA00UL
#define USECS_TO_NSECS(us) (us) * NS_PER_USEC
#define MSECS_TO_NSECS(ms) (ms) * NS_PER_MSEC
typedef function<void()> timer_cb;

View File

@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
Copyright (C) 2018-23 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@ -22,11 +22,14 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
// The main runfile - main.cpp
// This is where the magic begins
#include "debugger/debugger.h"
#include "machines/machinefactory.h"
#include "machines/machineproperties.h"
#include "utils/profiler.h"
#include "ppcemu.h"
#include <core/hostevents.h>
#include <core/timermanager.h>
#include <cpu/ppc/ppcemu.h>
#include <debugger/debugger.h>
#include <machines/machinefactory.h>
#include <machines/machineproperties.h>
#include <utils/profiler.h>
#include <cinttypes>
#include <csignal>
#include <cstring>
@ -196,6 +199,12 @@ int main(int argc, char** argv) {
// redirect SIGABRT to our own handler
signal(SIGABRT, sigabrt_handler);
// set up system wide event polling using
// default Macintosh polling rate of 11 ms
TimerManager::get_instance()->add_cyclic_timer(MSECS_TO_NSECS(11), [] {
EventManager::get_instance()->poll_events();
});
switch (execution_mode) {
case 0:
for (;;) {