[ion/clipboard] Use system clipboard on simulator

Add methods to interface Epsilon's clipboard with the system's when
running a simulator.
Note that the clipboard still uses a buffer located in the
Escher::Clipboard class, as some features require a buffer to execute
computations on the clipboard's content.

Change-Id: I14c19615805d38735e64d481c617863db22db9bc
This commit is contained in:
Gabriel Ozouf
2020-09-11 16:21:55 +02:00
committed by Émilie Feral
parent 6ba88921b9
commit b9c34ace3a
7 changed files with 62 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
#include <escher/clipboard.h>
#include <escher/text_field.h>
#include <ion/clipboard.h>
#include <algorithm>
static Clipboard s_clipboard;
@@ -10,9 +11,12 @@ Clipboard * Clipboard::sharedClipboard() {
void Clipboard::store(const char * storedText, int length) {
strlcpy(m_textBuffer, storedText, length == -1 ? TextField::maxBufferSize() : std::min(TextField::maxBufferSize(), length + 1));
Ion::Clipboard::write(m_textBuffer);
}
const char * Clipboard::storedText() {
Ion::Clipboard::read(m_textBuffer, TextField::maxBufferSize());
/* In order to allow copy/paste of empty formulas, we need to add empty
* layouts between empty system parenthesis. This way, when the expression
* is parsed, it is recognized as a proper formula and appears with the correct
@@ -39,6 +43,8 @@ const char * Clipboard::storedText() {
void Clipboard::reset() {
strlcpy(m_textBuffer, "", 1);
/* As we do not want to empty the user's computer's clipboard when entering
* exam mode, we do not reset Ion::Clipboard. */
}
void Clipboard::replaceCharForPython(bool entersPythonApp) {

View File

@@ -3,6 +3,7 @@
#include <ion/backlight.h>
#include <ion/battery.h>
#include <ion/clipboard.h>
#include <ion/console.h>
#include <ion/display.h>
#include <ion/events.h>

View File

@@ -0,0 +1,18 @@
#ifndef ION_CLIPBOARD_H
#define ION_CLIPBOARD_H
#include <stddef.h>
namespace Ion {
namespace Clipboard {
/* Write the text to the system clipboard. */
void write(const char * text);
/* Fill the buffer with text from the system clipboard. */
void read(char * buffer, size_t bufferSize);
}
}
#endif

View File

@@ -3,6 +3,7 @@ ion_device_src += $(addprefix ion/src/device/shared/drivers/, \
battery.cpp \
base64.cpp \
board.cpp \
clipboard.cpp \
console_uart.cpp:+consoleuart \
console_dummy.cpp:-consoleuart \
crc32.cpp \

View File

@@ -0,0 +1,11 @@
#include <ion/clipboard.h>
namespace Ion {
/* Dummy implementation
* On the device, the clipboard is fully handled by Escher::Clipboard. */
void Clipboard::write(const char * text) {}
void Clipboard::read(char * buffer, size_t bufferSize) {}
}

View File

@@ -8,6 +8,7 @@ ion_src += $(addprefix ion/src/simulator/shared/, \
dummy/serial_number.cpp \
dummy/stack.cpp \
dummy/usb.cpp \
clipboard.cpp \
console_stdio.cpp:-consoledisplay \
crc32.cpp \
display.cpp:-headless \

View File

@@ -0,0 +1,24 @@
#include <ion/clipboard.h>
#include <SDL.h>
#include <string.h>
namespace Ion {
void Clipboard::write(const char * text) {
/* FIXME : Handle the error if need be. */
SDL_SetClipboardText(text);
}
void Clipboard::read(char * buffer, size_t bufferSize) {
if (!SDL_HasClipboardText()) {
buffer[0] = '\0';
return;
}
char * text = SDL_GetClipboardText();
if (text) {
strlcpy(buffer, text, bufferSize);
SDL_free(text);
}
}
}