libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
sdlsubsystem.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <promeki/namespace.h>
11#include <promeki/application.h>
12#include <promeki/eventloop.h>
13#include <promeki/selfpipe.h>
15
16PROMEKI_NAMESPACE_BEGIN
17
18class Widget;
19
56 public:
67
70
71 SdlSubsystem(const SdlSubsystem &) = delete;
72 SdlSubsystem &operator=(const SdlSubsystem &) = delete;
73
78 static SdlSubsystem *instance() { return _instance; }
79
86 SDLEventPump &eventPump() { return _eventPump; }
87
97 EventLoop *eventLoop() { return _eventLoop; }
98
103 Widget *focusedWidget() const { return _focusedWidget; }
104
121 void setFocusedWidget(Widget *widget) { _focusedWidget = widget; }
122
123 private:
124 static SdlSubsystem *_instance;
125
126 EventLoop *_eventLoop = nullptr;
127 SDLEventPump _eventPump;
128 Widget *_focusedWidget = nullptr;
129
130 // Self-pipe used as the SDL → EventLoop bridge.
131 // SDL_AddEventWatch writes one byte to the write end
132 // from whatever thread pushed the event; the read
133 // end is registered as an EventLoop IoSource whose
134 // callback drains the pipe and pumps SDL's queue.
135 SelfPipe _sdlPipe;
136 int _sdlSourceHandle = -1;
137
138 // Periodic kick that calls @c SDL_PumpEvents so OS
139 // input (keyboard, window close, mouse) gets pulled
140 // into SDL's queue even when nothing on the promeki
141 // side is pushing events. Without this, a paused
142 // playback pipeline (no frames, no wakeMainThread
143 // user events) leaves OS events stranded: the
144 // @c sdlEventWatch callback only fires when
145 // @c SDL_PushEvent or a pump pulls a new event
146 // in, so the watch → pipe → IoSource → pump chain
147 // deadlocks without an external driver.
148 int _pumpTimerId = -1;
149
150 static bool sdlEventWatch(void *userdata, SDL_Event *event);
151};
152
153PROMEKI_NAMESPACE_END
Pumps SDL events into the promeki event system.
Definition sdleventpump.h:37
SDL subsystem installed alongside an Application.
Definition sdlsubsystem.h:55
SdlSubsystem()
Initialises SDL and installs the event-loop bridge.
void setFocusedWidget(Widget *widget)
Sets the widget that receives keyboard input.
Definition sdlsubsystem.h:121
SDLEventPump & eventPump()
Returns the subsystem's SDLEventPump.
Definition sdlsubsystem.h:86
Widget * focusedWidget() const
Returns the widget that currently receives keyboard input, or nullptr if none is set.
Definition sdlsubsystem.h:103
~SdlSubsystem()
Destructor. Removes I/O source, closes pipe, calls SDL_Quit.
EventLoop * eventLoop()
Returns the main EventLoop the subsystem is bound to.
Definition sdlsubsystem.h:97
static SdlSubsystem * instance()
Returns the active SdlSubsystem instance.
Definition sdlsubsystem.h:78