1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:57:45 +00:00

Merge EventLoopSDL into EventLoop.

This commit is contained in:
Andreas Kling 2019-01-10 05:28:37 +01:00
parent 3e908abfca
commit f603c17136
7 changed files with 122 additions and 147 deletions

View file

@ -135,7 +135,7 @@ public:
String text() const { return m_text; } String text() const { return m_text; }
private: private:
friend class EventLoopSDL; friend class EventLoop;
int m_key { 0 }; int m_key { 0 };
bool m_ctrl { false }; bool m_ctrl { false };
bool m_alt { false }; bool m_alt { false };

View file

@ -1,6 +1,7 @@
#include "EventLoop.h" #include "EventLoop.h"
#include "Event.h" #include "Event.h"
#include "Object.h" #include "Object.h"
#include "WindowManager.h"
static EventLoop* s_mainEventLoop; static EventLoop* s_mainEventLoop;
@ -50,3 +51,108 @@ void EventLoop::postEvent(Object* receiver, OwnPtr<Event>&& event)
m_queuedEvents.append({ receiver, std::move(event) }); m_queuedEvents.append({ receiver, std::move(event) });
} }
#ifdef USE_SDL
static inline MouseButton toMouseButton(byte sdlButton)
{
if (sdlButton == 1)
return MouseButton::Left;
if (sdlButton == 2)
return MouseButton::Middle;
if (sdlButton == 3)
return MouseButton::Right;
ASSERT_NOT_REACHED();
return MouseButton::None;
}
void EventLoop::handleKeyEvent(Event::Type type, const SDL_KeyboardEvent& sdlKey)
{
auto keyEvent = make<KeyEvent>(type, 0);
int key = 0;
switch (sdlKey.keysym.sym) {
case SDLK_LEFT: key = KeyboardKey::LeftArrow; break;
case SDLK_RIGHT: key = KeyboardKey::RightArrow; break;
case SDLK_UP: key = KeyboardKey::UpArrow; break;
case SDLK_DOWN: key = KeyboardKey::DownArrow; break;
case SDLK_BACKSPACE: key = KeyboardKey::Backspace; break;
case SDLK_RETURN: key = KeyboardKey::Return; break;
}
keyEvent->m_key = key;
if (sdlKey.keysym.sym > SDLK_UNKNOWN && sdlKey.keysym.sym <= 'z') {
char buf[] = { 0, 0 };
char& ch = buf[0];
ch = (char)sdlKey.keysym.sym;
if (sdlKey.keysym.mod & KMOD_SHIFT) {
if (ch >= 'a' && ch <= 'z') {
ch &= ~0x20;
} else {
switch (ch) {
case '1': ch = '!'; break;
case '2': ch = '@'; break;
case '3': ch = '#'; break;
case '4': ch = '$'; break;
case '5': ch = '%'; break;
case '6': ch = '^'; break;
case '7': ch = '&'; break;
case '8': ch = '*'; break;
case '9': ch = '('; break;
case '0': ch = ')'; break;
case '-': ch = '_'; break;
case '=': ch = '+'; break;
case '`': ch = '~'; break;
case ',': ch = '<'; break;
case '.': ch = '>'; break;
case '/': ch = '?'; break;
case '[': ch = '{'; break;
case ']': ch = '}'; break;
case '\\': ch = '|'; break;
case '\'': ch = '"'; break;
case ';': ch = ':'; break;
}
}
}
keyEvent->m_text = buf;
}
keyEvent->m_shift = sdlKey.keysym.mod & KMOD_SHIFT;
keyEvent->m_ctrl = sdlKey.keysym.mod & KMOD_CTRL;
keyEvent->m_alt = sdlKey.keysym.mod & KMOD_ALT;
postEvent(&WindowManager::the(), std::move(keyEvent));
}
void EventLoop::waitForEvent()
{
SDL_Event sdlEvent;
while (SDL_PollEvent(&sdlEvent) != 0) {
switch (sdlEvent.type) {
case SDL_QUIT:
postEvent(nullptr, make<QuitEvent>());
return;
case SDL_WINDOWEVENT:
if (sdlEvent.window.event == SDL_WINDOWEVENT_EXPOSED) {
// Spam paint events whenever we get exposed.
// This is obviously not ideal, but the SDL backend here is just a prototype anyway.
postEvent(&WindowManager::the(), make<PaintEvent>());
}
return;
case SDL_MOUSEMOTION:
postEvent(&WindowManager::the(), make<MouseEvent>(Event::MouseMove, sdlEvent.motion.x, sdlEvent.motion.y));
return;
case SDL_MOUSEBUTTONDOWN:
postEvent(&WindowManager::the(), make<MouseEvent>(Event::MouseDown, sdlEvent.button.x, sdlEvent.button.y, toMouseButton(sdlEvent.button.button)));
return;
case SDL_MOUSEBUTTONUP:
postEvent(&WindowManager::the(), make<MouseEvent>(Event::MouseUp, sdlEvent.button.x, sdlEvent.button.y, toMouseButton(sdlEvent.button.button)));
return;
case SDL_KEYDOWN:
handleKeyEvent(Event::KeyDown, sdlEvent.key);
return;
case SDL_KEYUP:
handleKeyEvent(Event::KeyUp, sdlEvent.key);
return;
}
}
}
#endif

View file

@ -1,27 +1,33 @@
#pragma once #pragma once
#include "Event.h"
#include <AK/OwnPtr.h> #include <AK/OwnPtr.h>
#include <AK/Vector.h> #include <AK/Vector.h>
class Event; #ifdef USE_SDL
#include <SDL.h>
#endif
class Object; class Object;
class EventLoop { class EventLoop {
public: public:
virtual ~EventLoop(); EventLoop();
~EventLoop();
int exec(); int exec();
virtual void waitForEvent() = 0;
void postEvent(Object* receiver, OwnPtr<Event>&&); void postEvent(Object* receiver, OwnPtr<Event>&&);
static EventLoop& main(); static EventLoop& main();
protected:
EventLoop();
private: private:
void waitForEvent();
#ifdef USE_SDL
void handleKeyEvent(Event::Type, const SDL_KeyboardEvent&);
#endif
struct QueuedEvent { struct QueuedEvent {
Object* receiver { nullptr }; Object* receiver { nullptr };
OwnPtr<Event> event; OwnPtr<Event> event;

View file

@ -1,119 +0,0 @@
#include "EventLoopSDL.h"
#include "Event.h"
#include <SDL.h>
#include "Widget.h"
#include "WindowManager.h"
#include <unistd.h>
EventLoopSDL::EventLoopSDL()
{
}
EventLoopSDL::~EventLoopSDL()
{
}
static inline MouseButton toMouseButton(byte sdlButton)
{
if (sdlButton == 1)
return MouseButton::Left;
if (sdlButton == 2)
return MouseButton::Middle;
if (sdlButton == 3)
return MouseButton::Right;
ASSERT_NOT_REACHED();
return MouseButton::None;
}
void EventLoopSDL::handleKeyEvent(Event::Type type, const SDL_KeyboardEvent& sdlKey)
{
auto keyEvent = make<KeyEvent>(type, 0);
int key = 0;
switch (sdlKey.keysym.sym) {
case SDLK_LEFT: key = KeyboardKey::LeftArrow; break;
case SDLK_RIGHT: key = KeyboardKey::RightArrow; break;
case SDLK_UP: key = KeyboardKey::UpArrow; break;
case SDLK_DOWN: key = KeyboardKey::DownArrow; break;
case SDLK_BACKSPACE: key = KeyboardKey::Backspace; break;
case SDLK_RETURN: key = KeyboardKey::Return; break;
}
keyEvent->m_key = key;
if (sdlKey.keysym.sym > SDLK_UNKNOWN && sdlKey.keysym.sym <= 'z') {
char buf[] = { 0, 0 };
char& ch = buf[0];
ch = (char)sdlKey.keysym.sym;
if (sdlKey.keysym.mod & KMOD_SHIFT) {
if (ch >= 'a' && ch <= 'z') {
ch &= ~0x20;
} else {
switch (ch) {
case '1': ch = '!'; break;
case '2': ch = '@'; break;
case '3': ch = '#'; break;
case '4': ch = '$'; break;
case '5': ch = '%'; break;
case '6': ch = '^'; break;
case '7': ch = '&'; break;
case '8': ch = '*'; break;
case '9': ch = '('; break;
case '0': ch = ')'; break;
case '-': ch = '_'; break;
case '=': ch = '+'; break;
case '`': ch = '~'; break;
case ',': ch = '<'; break;
case '.': ch = '>'; break;
case '/': ch = '?'; break;
case '[': ch = '{'; break;
case ']': ch = '}'; break;
case '\\': ch = '|'; break;
case '\'': ch = '"'; break;
case ';': ch = ':'; break;
}
}
}
keyEvent->m_text = buf;
}
keyEvent->m_shift = sdlKey.keysym.mod & KMOD_SHIFT;
keyEvent->m_ctrl = sdlKey.keysym.mod & KMOD_CTRL;
keyEvent->m_alt = sdlKey.keysym.mod & KMOD_ALT;
postEvent(&WindowManager::the(), std::move(keyEvent));
}
void EventLoopSDL::waitForEvent()
{
SDL_Event sdlEvent;
while (SDL_PollEvent(&sdlEvent) != 0) {
switch (sdlEvent.type) {
case SDL_QUIT:
postEvent(nullptr, make<QuitEvent>());
return;
case SDL_WINDOWEVENT:
if (sdlEvent.window.event == SDL_WINDOWEVENT_EXPOSED) {
// Spam paint events whenever we get exposed.
// This is obviously not ideal, but the SDL backend here is just a prototype anyway.
postEvent(&WindowManager::the(), make<PaintEvent>());
}
return;
case SDL_MOUSEMOTION:
postEvent(&WindowManager::the(), make<MouseEvent>(Event::MouseMove, sdlEvent.motion.x, sdlEvent.motion.y));
return;
case SDL_MOUSEBUTTONDOWN:
postEvent(&WindowManager::the(), make<MouseEvent>(Event::MouseDown, sdlEvent.button.x, sdlEvent.button.y, toMouseButton(sdlEvent.button.button)));
return;
case SDL_MOUSEBUTTONUP:
postEvent(&WindowManager::the(), make<MouseEvent>(Event::MouseUp, sdlEvent.button.x, sdlEvent.button.y, toMouseButton(sdlEvent.button.button)));
return;
case SDL_KEYDOWN:
handleKeyEvent(Event::KeyDown, sdlEvent.key);
return;
case SDL_KEYUP:
handleKeyEvent(Event::KeyUp, sdlEvent.key);
return;
}
}
}

View file

@ -1,17 +0,0 @@
#pragma once
#include "Event.h"
#include "EventLoop.h"
#include <SDL.h>
class EventLoopSDL final : public EventLoop {
public:
EventLoopSDL();
virtual ~EventLoopSDL() override;
private:
virtual void waitForEvent() override;
void handleKeyEvent(Event::Type, const SDL_KeyboardEvent&);
};

View file

@ -10,7 +10,6 @@ VFS_OBJS = \
AbstractScreen.o \ AbstractScreen.o \
FrameBuffer.o \ FrameBuffer.o \
EventLoop.o \ EventLoop.o \
EventLoopSDL.o \
Rect.o \ Rect.o \
Object.o \ Object.o \
Widget.o \ Widget.o \

View file

@ -1,5 +1,5 @@
#include "FrameBuffer.h" #include "FrameBuffer.h"
#include "EventLoopSDL.h" #include "EventLoop.h"
#include "RootWidget.h" #include "RootWidget.h"
#include "Label.h" #include "Label.h"
#include "Button.h" #include "Button.h"
@ -17,7 +17,7 @@ int main(int argc, char** argv)
FrameBuffer fb(800, 600); FrameBuffer fb(800, 600);
fb.show(); fb.show();
EventLoopSDL loop; EventLoop loop;
RootWidget w; RootWidget w;
WindowManager::the().setRootWidget(&w); WindowManager::the().setRootWidget(&w);