[Qt] Add proper message queue for thread communication

This commit is contained in:
wheremyfoodat 2023-10-30 20:20:47 +02:00
parent 5984c27960
commit 8d5485fbeb
2 changed files with 60 additions and 22 deletions

View file

@ -9,9 +9,11 @@
#include <filesystem>
#include <mutex>
#include <thread>
#include <vector>
#include "emulator.hpp"
#include "panda_qt/screen.hpp"
#include "services/hid.hpp"
class MainWindow : public QMainWindow {
Q_OBJECT
@ -23,15 +25,34 @@ class MainWindow : public QMainWindow {
Dark = 2,
};
// Types of messages we might send from the GUI thread to the emulator thread
enum class MessageType {
LoadROM, Reset, Pause, Resume, TogglePause, DumpRomFS, PressKey, ReleaseKey
};
// Tagged union representing our message queue messages
struct EmulatorMessage {
MessageType type;
union {
struct {
std::filesystem::path* p;
} path;
struct {
u32 key;
} key;
};
};
// This would normally be an std::unique_ptr but it's shared between threads so definitely not
Emulator* emu = nullptr;
std::thread emuThread;
std::atomic<bool> appRunning = true; // Is the application itself running?
std::mutex messageQueueMutex; // Used for synchronizing messages between the emulator and UI
std::filesystem::path romToLoad = "";
bool needToLoadROM = false;
// Used for synchronizing messages between the emulator and UI
std::mutex messageQueueMutex;
std::vector<EmulatorMessage> messageQueue;
ScreenWidget screen;
QComboBox* themeSelect = nullptr;
@ -43,6 +64,8 @@ class MainWindow : public QMainWindow {
void emuThreadMainLoop();
void selectROM();
void dumpRomFS();
void sendMessage(const EmulatorMessage& message);
void dispatchMessage(const EmulatorMessage& message);
// Tracks whether we are using an OpenGL-backed renderer or a Vulkan-backed renderer
bool usingGL = false;