[ion/simulator] Clean the Keyboard implementation

This commit is contained in:
Romain Goyet
2020-09-03 22:57:12 -04:00
committed by Léa Saviot
parent 90b25ecf51
commit baf8b8cbf0
11 changed files with 110 additions and 49 deletions

View File

@@ -0,0 +1,11 @@
#include <ion/keyboard.h>
namespace Ion {
namespace Keyboard {
State scan() {
return State();
}
}
}

View File

@@ -4,6 +4,7 @@ ion_src += $(addprefix ion/src/shared/dummy/, \
display.cpp \
exam_mode.cpp \
fcc_id.cpp \
keyboard.cpp:+headless \
led.cpp \
power.cpp \
serial_number.cpp \
@@ -13,6 +14,7 @@ ion_src += $(addprefix ion/src/shared/dummy/, \
)
ion_src += $(addprefix ion/src/simulator/shared/, \
dummy/keyboard.cpp:+headless \
clipboard.cpp \
console_stdio.cpp:-consoledisplay \
crc32.cpp \
@@ -22,8 +24,7 @@ ion_src += $(addprefix ion/src/simulator/shared/, \
events_stdin.cpp:+headless \
framebuffer_base.cpp \
framebuffer_png.cpp:+headless \
keyboard_dummy.cpp:+headless \
keyboard_sdl.cpp:-headless \
keyboard.cpp:-headless \
layout.cpp:-headless \
main.cpp \
random.cpp \

View File

@@ -0,0 +1,19 @@
#include "../keyboard.h"
namespace Ion {
namespace Simulator {
namespace Keyboard {
void keyDown(Ion::Keyboard::Key k) {
}
void keyUp(Ion::Keyboard::Key k) {
}
bool scanHandlesSDLKey(SDL_Scancode key) {
return false;
}
}
}
}

View File

@@ -1,4 +1,4 @@
#include "platform.h"
#include "keyboard.h"
#include "layout.h"
#include "events.h"
#include "window.h"
@@ -191,7 +191,7 @@ Event getPlatformEvent() {
break;
}
if (event.type == SDL_KEYDOWN) {
if (IonSimulatorSDLKeyDetectedByScan(event.key.keysym.scancode)) {
if (Simulator::Keyboard::scanHandlesSDLKey(event.key.keysym.scancode)) {
continue;
}
result = eventFromSDLKeyboardEvent(event.key);

View File

@@ -5,23 +5,8 @@
#include <ion/keyboard.h>
#include <SDL.h>
#if EPSILON_SDL_SCREEN_ONLY
static Ion::Keyboard::State sKeyboardState;
void IonSimulatorKeyboardKeyDown(int keyNumber) {
Ion::Keyboard::Key key = static_cast<Ion::Keyboard::Key>(keyNumber);
sKeyboardState.setKey(key);
}
void IonSimulatorKeyboardKeyUp(int keyNumber) {
Ion::Keyboard::Key key = static_cast<Ion::Keyboard::Key>(keyNumber);
sKeyboardState.clearKey(key);
}
#endif
namespace Ion {
namespace Keyboard {
class KeySDLKeyPair {
public:
constexpr KeySDLKeyPair(Ion::Keyboard::Key key, SDL_Scancode SDLKey) :
@@ -50,6 +35,9 @@ constexpr static KeySDLKeyPair sKeyPairs[] = {
constexpr int sNumberOfKeyPairs = sizeof(sKeyPairs)/sizeof(KeySDLKeyPair);
namespace Ion {
namespace Keyboard {
State scan() {
// We need to tell SDL to get new state from the host OS
SDL_PumpEvents();
@@ -60,12 +48,8 @@ State scan() {
// Grab this opportunity to refresh the display if needed
Simulator::Window::refresh();
// Start with a "clean" state
State state;
#if EPSILON_SDL_SCREEN_ONLY
// In this case, keyboard states are sent over another channel.
state = sKeyboardState;
#else
State state = sKeyboardState;
#if !EPSILON_SDL_SCREEN_ONLY
// Register a key for the mouse, if any
Key k = Simulator::Layout::getHighlightedKey();
if (SDL_GetMouseState(nullptr, nullptr) && SDL_BUTTON(SDL_BUTTON_LEFT)) {
@@ -87,11 +71,27 @@ State scan() {
}
}
bool IonSimulatorSDLKeyDetectedByScan(SDL_Scancode key) {
for (int i = 0; i < Ion::Keyboard::sNumberOfKeyPairs; i++) {
if (key == Ion::Keyboard::sKeyPairs[i].SDLKey()) {
namespace Ion {
namespace Simulator {
namespace Keyboard {
void keyDown(Ion::Keyboard::Key k) {
sKeyboardState.setKey(k);
}
void keyUp(Ion::Keyboard::Key k) {
sKeyboardState.clearKey(k);
}
bool scanHandlesSDLKey(SDL_Scancode key) {
for (int i = 0; i < sNumberOfKeyPairs; i++) {
if (key == sKeyPairs[i].SDLKey()) {
return true;
}
}
return false;
}
}
}
}

View File

@@ -0,0 +1,19 @@
#ifndef ION_SIMULATOR_KEYBOARD_H
#define ION_SIMULATOR_KEYBOARD_H
#include <ion/keyboard.h>
#include <SDL.h>
namespace Ion {
namespace Simulator {
namespace Keyboard {
void keyDown(Ion::Keyboard::Key k);
void keyUp(Ion::Keyboard::Key k);
bool scanHandlesSDLKey(SDL_Scancode key);
}
}
}
#endif

View File

@@ -1,18 +0,0 @@
#include <ion/keyboard.h>
#include "platform.h"
void IonSimulatorKeyboardKeyDown(int keyNumber) {
}
void IonSimulatorKeyboardKeyUp(int keyNumber) {
}
namespace Ion {
namespace Keyboard {
State scan() {
return 0;
}
}
}

View File

@@ -15,12 +15,9 @@ SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi
char * IonSimulatorGetLanguageCode();
#if EPSILON_SDL_SCREEN_ONLY
void IonSimulatorKeyboardKeyDown(int keyNumber);
void IonSimulatorKeyboardKeyUp(int keyNumber);
void IonSimulatorEventsPushEvent(int eventNumber);
#endif
bool IonSimulatorSDLKeyDetectedByScan(SDL_Scancode key);
void IonSimulatorCallbackDidRefresh();
void IonSimulatorCallbackDidScanKeyboard();

View File

@@ -16,6 +16,7 @@ LDFLAGS += --pre-js ion/src/simulator/web/preamble_env.js
ion_src += $(addprefix ion/src/simulator/web/, \
callback.cpp \
clipboard_helper.cpp \
exports.cpp \
helpers.cpp \
)

View File

@@ -0,0 +1,15 @@
#include "exports.h"
#include "../shared/keyboard.h"
void IonSimulatorKeyboardKeyDown(int keyNumber) {
Ion::Keyboard::Key key = static_cast<Ion::Keyboard::Key>(keyNumber);
Ion::Simulator::Keyboard::keyDown(key);
}
void IonSimulatorKeyboardKeyUp(int keyNumber) {
Ion::Keyboard::Key key = static_cast<Ion::Keyboard::Key>(keyNumber);
Ion::Simulator::Keyboard::keyUp(key);
}
void IonSimulatorEventsPushEvent(int eventNumber) {
}

View File

@@ -0,0 +1,16 @@
#ifndef ION_SIMULATOR_WEB_EXPORTS_H
#define ION_SIMULATOR_WEB_EXPORTS_H
#ifdef __cplusplus
extern "C" {
#endif
void IonSimulatorKeyboardKeyDown(int keyNumber);
void IonSimulatorKeyboardKeyUp(int keyNumber);
void IonSimulatorEventsPushEvent(int eventNumber);
#ifdef __cplusplus
}
#endif
#endif