From b9c34ace3ac9778c1d6d9c725ce8e9da0833fb58 Mon Sep 17 00:00:00 2001 From: Gabriel Ozouf Date: Fri, 11 Sep 2020 16:21:55 +0200 Subject: [PATCH] [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 --- escher/src/clipboard.cpp | 6 ++++++ ion/include/ion.h | 1 + ion/include/ion/clipboard.h | 18 ++++++++++++++++ ion/src/device/shared/drivers/Makefile | 1 + ion/src/device/shared/drivers/clipboard.cpp | 11 ++++++++++ ion/src/simulator/Makefile | 1 + ion/src/simulator/shared/clipboard.cpp | 24 +++++++++++++++++++++ 7 files changed, 62 insertions(+) create mode 100644 ion/include/ion/clipboard.h create mode 100644 ion/src/device/shared/drivers/clipboard.cpp create mode 100644 ion/src/simulator/shared/clipboard.cpp diff --git a/escher/src/clipboard.cpp b/escher/src/clipboard.cpp index c69c07210..dc38d2d70 100644 --- a/escher/src/clipboard.cpp +++ b/escher/src/clipboard.cpp @@ -1,5 +1,6 @@ #include #include +#include #include 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) { diff --git a/ion/include/ion.h b/ion/include/ion.h index e01bc03c1..54e3671ce 100644 --- a/ion/include/ion.h +++ b/ion/include/ion.h @@ -3,6 +3,7 @@ #include #include +#include #include #include #include diff --git a/ion/include/ion/clipboard.h b/ion/include/ion/clipboard.h new file mode 100644 index 000000000..92bbe0414 --- /dev/null +++ b/ion/include/ion/clipboard.h @@ -0,0 +1,18 @@ +#ifndef ION_CLIPBOARD_H +#define ION_CLIPBOARD_H + +#include + +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 diff --git a/ion/src/device/shared/drivers/Makefile b/ion/src/device/shared/drivers/Makefile index 548632a8e..a73470f0e 100644 --- a/ion/src/device/shared/drivers/Makefile +++ b/ion/src/device/shared/drivers/Makefile @@ -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 \ diff --git a/ion/src/device/shared/drivers/clipboard.cpp b/ion/src/device/shared/drivers/clipboard.cpp new file mode 100644 index 000000000..88e321f9f --- /dev/null +++ b/ion/src/device/shared/drivers/clipboard.cpp @@ -0,0 +1,11 @@ +#include + +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) {} + +} diff --git a/ion/src/simulator/Makefile b/ion/src/simulator/Makefile index 10f10fb1e..473c0c7b3 100644 --- a/ion/src/simulator/Makefile +++ b/ion/src/simulator/Makefile @@ -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 \ diff --git a/ion/src/simulator/shared/clipboard.cpp b/ion/src/simulator/shared/clipboard.cpp new file mode 100644 index 000000000..e18546833 --- /dev/null +++ b/ion/src/simulator/shared/clipboard.cpp @@ -0,0 +1,24 @@ +#include +#include +#include + +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); + } +} + +}