[ion/simulator] Rename platform APIs

This commit is contained in:
Romain Goyet
2019-09-25 15:04:03 +02:00
committed by EmilieNumworks
parent 8f4b45f50f
commit 0066dcd522
23 changed files with 175 additions and 68 deletions

View File

@@ -1,9 +1,12 @@
ion_src += $(addprefix ion/src/simulator/android/src/cpp/, \
images.cpp \
language.cpp \
telemetry.cpp \
)
ion_src += $(addprefix ion/src/simulator/shared/, \
language_dummy.cpp \
)
$(call object_for,ion/src/simulator/shared/main.cpp) : SFLAGS += -DEPSILON_SDL_FULLSCREEN=1
LDFLAGS += -ljnigraphics -llog

View File

@@ -3,7 +3,7 @@
#include <jni.h>
#include <android/bitmap.h>
SDL_Texture * IonSDLPlatformLoadImage(SDL_Renderer * renderer, const char * identifier) {
SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) {
JNIEnv * env = static_cast<JNIEnv *>(SDL_AndroidGetJNIEnv());
jobject activity = static_cast<jobject>(SDL_AndroidGetActivity());

View File

@@ -1,7 +0,0 @@
#include "../../../shared/platform.h"
char * IonSDLPlatformGetLanguageCode() {
// The Android NDK cannot retrieve the current locale
// This will be handled by the Java side
return nullptr;
}

View File

@@ -2,7 +2,7 @@
#include <jni.h>
#include <SDL.h>
void IonSDLPlatformTelemetryInit() {
void IonSimulatorTelemetryInit() {
JNIEnv * env = static_cast<JNIEnv *>(SDL_AndroidGetJNIEnv());
jobject activity = static_cast<jobject>(SDL_AndroidGetActivity());
@@ -12,7 +12,7 @@ void IonSDLPlatformTelemetryInit() {
env->CallVoidMethod(activity, j_methodId);
}
void IonSDLPlatformTelemetryEvent(const char * eventName) {
void IonSimulatorTelemetryEvent(const char * eventName) {
JNIEnv * env = static_cast<JNIEnv *>(SDL_AndroidGetJNIEnv());
jobject activity = static_cast<jobject>(SDL_AndroidGetActivity());
@@ -24,5 +24,5 @@ void IonSDLPlatformTelemetryEvent(const char * eventName) {
env->CallVoidMethod(activity, j_methodId, j_eventName);
}
void IonSDLPlatformTelemetryDeinit() {
void IonSimulatorTelemetryDeinit() {
}

View File

@@ -3,7 +3,7 @@
#include <SDL.h>
#include <UIKit/UIKit.h>
SDL_Texture * IonSDLPlatformLoadImage(SDL_Renderer * renderer, const char * identifier) {
SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) {
CGImageRef cgImage = [[UIImage imageNamed:[NSString stringWithUTF8String:identifier]] CGImage];
if (cgImage == NULL) {
return NULL;

View File

@@ -4,15 +4,15 @@
#import <GAIDictionaryBuilder.h>
#import <GAIFields.h>
void IonSDLPlatformTelemetryInit() {
void IonSimulatorTelemetryInit() {
[[GAI sharedInstance] trackerWithTrackingId:@"UA-93775823-3"];
}
void IonSDLPlatformTelemetryEvent(const char * eventName) {
void IonSimulatorTelemetryEvent(const char * eventName) {
id<GAITracker> tracker = [GAI sharedInstance].defaultTracker;
[tracker set:kGAIScreenName value:[NSString stringWithUTF8String:eventName]];
[tracker send:[[GAIDictionaryBuilder createScreenView] build]];
}
void IonSDLPlatformTelemetryDeinit() {
void IonSimulatorTelemetryDeinit() {
}

View File

@@ -4,7 +4,7 @@ ion_src += $(addprefix ion/src/simulator/macos/, \
ion_src += $(addprefix ion/src/simulator/shared/, \
apple/language.m \
telemetry_dummy.cpp \
dummy/telemetry.cpp \
)
# App resources

View File

@@ -3,7 +3,7 @@
#include <SDL.h>
#include <AppKit/AppKit.h>
SDL_Texture * IonSDLPlatformLoadImage(SDL_Renderer * renderer, const char * identifier) {
SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) {
NSImage * nsImage = [NSImage imageNamed:[NSString stringWithUTF8String:identifier]];
CGImageRef cgImage = [nsImage CGImageForProposedRect:NULL
context:NULL

View File

@@ -2,7 +2,7 @@
#include <Foundation/Foundation.h>
char * IonSDLPlatformGetLanguageCode() {
char * IonSimulatorGetLanguageCode() {
static char buffer[4] = {0};
if (buffer[0] == 0) {
NSString * preferredLanguage = [[NSLocale preferredLanguages] firstObject];

View File

@@ -0,0 +1,7 @@
#include "../platform.h"
void IonSimulatorCallbackDidRefresh() {
}
void IonSimulatorCallbackDidScanKeyboard() {
}

View File

@@ -0,0 +1,5 @@
#include "../platform.h"
char * IonSimulatorGetLanguageCode() {
return nullptr;
}

View File

@@ -0,0 +1,10 @@
#include "../platform.h"
void IonSimulatorTelemetryInit() {
}
void IonSimulatorTelemetryEvent(const char * eventName) {
}
void IonSimulatorTelemetryDeinit() {
}

View File

@@ -6,6 +6,60 @@
#include <SDL.h>
#include <string.h>
#if EPSILON_SDL_SCREEN_ONLY
template<typename T, int N>
class Queue {
public:
Queue() : m_first(&m_elements[0]), m_last(&m_elements[0]) {}
int size() {
if (m_last >= m_first) {
return m_last - m_first;
} else {
return m_last - (m_first - N);
}
}
void enqueue(T element) {
if (size() > N) {
// Queue is full
return;
}
*m_last = element;
m_last = next(m_last);
}
T dequeue() {
if (size() <= 0) {
// Dequeueing an empty queue
return T();
}
T e = *m_first;
m_first = next(m_first);
return e;
}
private:
T * next(T * p) {
if (p >= m_elements + N) {
return m_elements;
} else {
return p + 1;
}
}
T * m_first;
T * m_last;
T m_elements[N];
};
static Queue<Ion::Events::Event, 1024> sEventQueue;
void IonSimulatorEventsPushEvent(int eventNumber) {
sEventQueue.enqueue(Ion::Events::Event(eventNumber));
}
#endif
namespace Ion {
namespace Events {
@@ -120,6 +174,12 @@ static Event eventFromSDLTextInputEvent(SDL_TextInputEvent event) {
}
Event getPlatformEvent() {
#if EPSILON_SDL_SCREEN_ONLY
if (sEventQueue.size() > 0) {
Event event = sEventQueue.dequeue();
return event;
}
#endif
SDL_Event event;
while (SDL_PollEvent(&event)) {
// The while is important: it'll do a fast-pass over all useless SDL events
@@ -138,7 +198,7 @@ Event getPlatformEvent() {
return eventFromSDLTextInputEvent(event.text);
}
if (event.type == SDL_APP_WILLENTERFOREGROUND) {
IonSDLPlatformTelemetryEvent("Calculator");
IonSimulatorTelemetryEvent("Calculator");
return None;
}
}

View File

@@ -1,9 +1,24 @@
#include "main.h"
#include "platform.h"
#include "layout.h"
#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 {
@@ -11,9 +26,16 @@ State scan() {
// We need to tell SDL to get new state from the host OS
SDL_PumpEvents();
// Notify callbacks in case we need to do something
IonSimulatorCallbackDidScanKeyboard();
// Grab this opportunity to refresh the display if needed
SDL::Main::refresh();
#if EPSILON_SDL_SCREEN_ONLY
// In this case, keyboard states will be sent over another channel
return sKeyboardState;
#else
// Start with a "clean" state
State state;
@@ -25,24 +47,8 @@ State scan() {
state.setKey(k);
}
#if 0
// SDL exposes the first finger as a mouse!
// Register a key for each finger, if any
int numberOfTouchDevices = SDL_GetNumTouchDevices();
for (int i=0; i<numberOfTouchDevices; i++) {
SDL_TouchID touchDevice = SDL_GetTouchDevice(touchDevice);
int numberOfFingers = SDL_GetNumTouchFingers(i);
for (int j=0; j<numberOfFingers; j++) {
SDL_Finger * finger = SDL_GetTouchFinger(touchDevice, j);
// Todo: Convert the finger's coordinate into pixel coordinates
//SDL_Point p =
//Key k= SDL::Layout::keyAt(&p);
//state.setKey(k);
}
}
#endif
return state;
#endif
}
}

View File

@@ -1,5 +0,0 @@
#include "../shared/platform.h"
char * IonSDLPlatformGetLanguageCode() {
return nullptr;
}

View File

@@ -18,18 +18,18 @@ void Ion::Timing::msleep(uint32_t ms) {
int main(int argc, char * argv[]) {
std::vector<char *> arguments(argv, argv + argc);
char * language = IonSDLPlatformGetLanguageCode();
char * language = IonSimulatorGetLanguageCode();
if (language != nullptr) {
arguments.push_back("--language");
arguments.push_back(language);
}
IonSDLPlatformTelemetryInit();
IonSimulatorTelemetryInit();
Ion::SDL::Main::init();
IonSDLPlatformTelemetryEvent("Calculator");
IonSimulatorTelemetryEvent("Calculator");
ion_main(arguments.size(), &arguments[0]);
Ion::SDL::Main::quit();
IonSDLPlatformTelemetryDeinit();
IonSimulatorTelemetryDeinit();
return 0;
}
@@ -82,7 +82,7 @@ void init() {
Display::init(sRenderer);
#if !EPSILON_SDL_SCREEN_ONLY
sBackgroundTexture = IonSDLPlatformLoadImage(sRenderer, "background.jpg");
sBackgroundTexture = IonSimulatorLoadImage(sRenderer, "background.jpg");
#endif
relayout();
@@ -133,6 +133,8 @@ void refresh() {
Display::draw(sRenderer, &screenRect);
#endif
SDL_RenderPresent(sRenderer);
IonSimulatorCallbackDidRefresh();
}
void quit() {

View File

@@ -10,12 +10,24 @@ extern "C" {
/* Those functions should be implemented per-platform.
* They are defined as C function for easier interop. */
SDL_Texture * IonSDLPlatformLoadImage(SDL_Renderer * renderer, const char * identifier);
char * IonSDLPlatformGetLanguageCode();
SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier);
char * IonSimulatorGetLanguageCode();
void IonSDLPlatformTelemetryInit();
void IonSDLPlatformTelemetryEvent(const char * eventName);
void IonSDLPlatformTelemetryDeinit();
void IonSimulatorTelemetryInit();
void IonSimulatorTelemetryEvent(const char * eventName);
void IonSimulatorTelemetryDeinit();
#if EPSILON_SDL_SCREEN_ONLY
void IonSimulatorKeyboardKeyDown(int keyNumber);
void IonSimulatorKeyboardKeyUp(int keyNumber);
void IonSimulatorEventsPushEvent(int eventNumber);
#endif
void IonSimulatorCallbackDidRefresh();
void IonSimulatorCallbackDidScanKeyboard();
#ifdef __cplusplus
}

View File

@@ -1,10 +0,0 @@
#include "../shared/platform.h"
void IonSDLPlatformTelemetryInit() {
}
void IonSDLPlatformTelemetryEvent(const char * eventName) {
}
void IonSDLPlatformTelemetryDeinit() {
}

View File

@@ -8,9 +8,14 @@ SFLAGS += -DUSING_GENERATED_CONFIG_H
SFLAGS += -Iion/src/simulator/web/include
# Only render the screen, not the whole calculator which will be drawn in HTML
$(call object_for,ion/src/simulator/shared/main.cpp) : SFLAGS += -DEPSILON_SDL_SCREEN_ONLY=1
SFLAGS += -DEPSILON_SDL_SCREEN_ONLY=1
ion_src += $(addprefix ion/src/simulator/web/, \
callback.cpp \
helpers.cpp \
)
ion_src += $(addprefix ion/src/simulator/shared/, \
language_dummy.cpp \
telemetry_dummy.cpp \
dummy/language.cpp \
dummy/telemetry.cpp \
)

View File

@@ -0,0 +1,19 @@
#include "../shared/platform.h"
#include <emscripten.h>
void IonSimulatorCallbackDidRefresh() {
/* Notify JS that the display has been refreshed.
* This gives us a chance to mirror the display in fullscreen mode. */
EM_ASM(if (typeof Module.onDisplayRefresh === "function") { Module.onDisplayRefresh(); });
}
void IonSimulatorCallbackDidScanKeyboard() {
/* The following call to emscripten_sleep gives the JS VM a chance to do a run
* loop iteration. This in turns gives the browser an opportunity to call the
* IonEventsEmscriptenPushKey function, therefore modifying the sKeyboardState
* global variable before it is returned by this Ion::Keyboard::scan.
* On Emterpreter-async, emscripten_sleep is actually a wrapper around the JS
* function setTimeout, which can be called with a value of zero. Doing so
* puts the callback at the end of the queue of callbacks to be processed. */
emscripten_sleep(0);
}

View File

@@ -4,7 +4,7 @@ ion_src += $(addprefix ion/src/simulator/windows/, \
resources.rc \
)
ion_src += $(addprefix ion/src/simulator/shared/, \
telemetry_dummy.cpp \
dummy/telemetry.cpp \
)
LDFLAGS += -lgdiplus

View File

@@ -34,7 +34,7 @@ HRESULT CreateStreamOnResource(const char * name, LPSTREAM * stream) {
return hr;
}
SDL_Texture * IonSDLPlatformLoadImage(SDL_Renderer * renderer, const char * identifier) {
SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) {
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr);

View File

@@ -2,7 +2,7 @@
#include <winnls.h>
char * IonSDLPlatformGetLanguageCode() {
char * IonSimulatorGetLanguageCode() {
/* Per documentation, the maximum number of characters allowed for the
* language string is nine, including a terminating null character. */
static char buffer[9] = {0};