External application launcher app

This commit is contained in:
Damien Nicolet
2019-12-13 01:02:14 +01:00
parent f763563834
commit 2700098c08
20 changed files with 660 additions and 1 deletions

30
apps/external/Makefile vendored Normal file
View File

@@ -0,0 +1,30 @@
apps += External::App
app_headers += apps/external/app.h
app_external_src = $(addprefix apps/external/,\
app.cpp \
extapp_api.cpp \
archive.cpp \
main_controller.cpp \
pointer_text_table_cell.cpp \
)
SFLAGS += -Iapps/external/
ifeq ($(PLATFORM),device)
SFLAGS += -DDEVICE
else
include apps/external/app/sources.mak
endif
app_src += $(app_external_src)
i18n_files += $(addprefix apps/external/,\
base.de.i18n\
base.en.i18n\
base.es.i18n\
base.fr.i18n\
base.pt.i18n\
)
$(eval $(call depends_on_image,apps/external/app.cpp,apps/external/external_icon.png))

44
apps/external/app.cpp vendored Normal file
View File

@@ -0,0 +1,44 @@
#include "app.h"
#include "external_icon.h"
#include <apps/i18n.h>
namespace External {
I18n::Message App::Descriptor::name() {
return I18n::Message::ExternalApp;
}
I18n::Message App::Descriptor::upperName() {
return I18n::Message::ExternalAppCapital;
}
const Image * App::Descriptor::icon() {
return ImageStore::ExternalIcon;
}
App * App::Snapshot::unpack(Container * container) {
return new (container->currentAppBuffer()) App(this);
}
App::Descriptor * App::Snapshot::descriptor() {
static Descriptor descriptor;
return &descriptor;
}
void App::didBecomeActive(Window * window) {
::App::didBecomeActive(window);
m_window = window;
}
void App::redraw() {
m_window->redraw(true);
}
App::App(Snapshot * snapshot) :
::App(snapshot, &m_stackViewController),
m_mainController(&m_stackViewController, this),
m_stackViewController(&m_modalViewController, &m_mainController)
{
}
}

37
apps/external/app.h vendored Normal file
View File

@@ -0,0 +1,37 @@
#ifndef EXTERNAL_APP_H
#define EXTERNAL_APP_H
#include <escher.h>
#include "main_controller.h"
namespace External {
class App : public ::App {
public:
class Descriptor : public ::App::Descriptor {
public:
I18n::Message name() override;
I18n::Message upperName() override;
const Image * icon() override;
};
class Snapshot : public ::App::Snapshot {
public:
App * unpack(Container * container) override;
Descriptor * descriptor() override;
};
void redraw();
virtual void didBecomeActive(Window * window);
int heapSize() { return k_externalHeapSize; }
char * heap() { return m_externalHeap; }
private:
App(Snapshot * snapshot);
MainController m_mainController;
StackViewController m_stackViewController;
Window * m_window;
static constexpr int k_externalHeapSize = 131072;
char m_externalHeap[k_externalHeapSize];
};
}
#endif

6
apps/external/app/sample.c vendored Normal file
View File

@@ -0,0 +1,6 @@
#include <extapp_api.h>
void extapp_main() {
extapp_pushRectUniform(10, 10, LCD_WIDTH-20, LCD_HEIGHT-20, 0);
extapp_msleep(1000);
}

3
apps/external/app/sources.mak vendored Normal file
View File

@@ -0,0 +1,3 @@
app_external_src += $(addprefix apps/external/app/,\
sample.c \
)

141
apps/external/archive.cpp vendored Normal file
View File

@@ -0,0 +1,141 @@
#include "archive.h"
#include "extapp_api.h"
#include <string.h>
#include <stdlib.h>
namespace External {
namespace Archive {
#ifdef DEVICE
struct TarHeader
{ /* byte offset */
char name[100]; /* 0 */
char mode[8]; /* 100 */
char uid[8]; /* 108 */
char gid[8]; /* 116 */
char size[12]; /* 124 */
char mtime[12]; /* 136 */
char chksum[8]; /* 148 */
char typeflag; /* 156 */
char linkname[100]; /* 157 */
char magic[8]; /* 257 */
char uname[32]; /* 265 */
char gname[32]; /* 297 */
char devmajor[8]; /* 329 */
char devminor[8]; /* 337 */
char padding[167]; /* 345 */
} __attribute__((packed));
static_assert(sizeof(TarHeader) == 512);
bool isSane(const TarHeader* tar) {
return !memcmp(tar->magic, "ustar ", 8) && tar->name[0] != '\x00' && tar->name[0] != '\xFF';
}
bool fileAtIndex(size_t index, File &entry) {
const TarHeader* tar = reinterpret_cast<const TarHeader*>(0x90200000);
unsigned size = 0;
// Sanity check.
if (!isSane(tar)) {
return false;
}
/**
* TAR files are comprised of a set of records aligned to 512 bytes boundary
* followed by data.
*/
while (index-- > 0) {
size = 0;
for (int i = 0; i < 11; i++)
size = size * 8 + (tar->size[i] - '0');
// Move to the next TAR header.
unsigned stride = (sizeof(TarHeader) + size + 511);
stride = (stride >> 9) << 9;
tar = reinterpret_cast<const TarHeader*>(reinterpret_cast<const char*>(tar) + stride);
// Sanity check.
if (!isSane(tar)) {
return false;
}
}
// File entry found, copy data out.
entry.name = tar->name;
entry.data = reinterpret_cast<const uint8_t*>(tar) + sizeof(TarHeader);
entry.dataLength = size;
entry.isExecutable = (tar->mode[4] & 0x01) == 1;
return true;
}
extern "C" void (* const apiPointers[])(void);
typedef uint32_t (*entrypoint)(const uint32_t, const void *, void *, const uint32_t);
uint32_t executeFile(const char *name, void * heap, const uint32_t heapSize) {
File entry;
if(fileAtIndex(indexFromName(name), entry)) {
if(!entry.isExecutable) {
return 0;
}
uint32_t ep = *reinterpret_cast<const uint32_t*>(entry.data);
if(ep >= 0x90200000 && ep < 0x90800000) {
return ((entrypoint)ep)(API_VERSION, apiPointers, heap, heapSize);
}
}
return -1;
}
int indexFromName(const char *name) {
File entry;
for (int i = 0; fileAtIndex(i, entry); i++) {
if (strcmp(name, entry.name) == 0) {
return i;
}
}
return -1;
}
size_t numberOfFiles() {
File dummy;
size_t count;
for (count = 0; fileAtIndex(count, dummy); count++);
return count;
}
#else
bool fileAtIndex(size_t index, File &entry) {
entry.name = "App";
entry.data = NULL;
entry.dataLength = 0;
entry.isExecutable = true;
return true;
}
extern "C" void extapp_main(void);
uint32_t executeFile(const char *name, void * heap, const uint32_t heapSize) {
extapp_main();
return 0;
}
int indexFromName(const char *name) {
return 0;
}
size_t numberOfFiles() {
return 1;
}
#endif
}
}

27
apps/external/archive.h vendored Normal file
View File

@@ -0,0 +1,27 @@
#ifndef EXTERNAL_ARCHIVE_H
#define EXTERNAL_ARCHIVE_H
#include <stddef.h>
#include <stdint.h>
namespace External {
namespace Archive {
constexpr int MaxNameLength = 40;
struct File {
const char *name;
const uint8_t *data;
size_t dataLength;
bool isExecutable;
};
bool fileAtIndex(size_t index, File &entry);
int indexFromName(const char *name);
size_t numberOfFiles();
uint32_t executeFile(const char *name, void * heap, const uint32_t heapSize);
}
}
#endif

4
apps/external/base.de.i18n vendored Normal file
View File

@@ -0,0 +1,4 @@
ExternalApp = "External"
ExternalAppCapital = "EXTERNAL"
ExternalAppApiMismatch = "API mismatch"
ExternalAppExecError = "Cannot execute file"

4
apps/external/base.en.i18n vendored Normal file
View File

@@ -0,0 +1,4 @@
ExternalApp = "External"
ExternalAppCapital = "EXTERNAL"
ExternalAppApiMismatch = "API mismatch"
ExternalAppExecError = "Cannot execute file"

4
apps/external/base.es.i18n vendored Normal file
View File

@@ -0,0 +1,4 @@
ExternalApp = "External"
ExternalAppCapital = "EXTERNAL"
ExternalAppApiMismatch = "API mismatch"
ExternalAppExecError = "Cannot execute file"

4
apps/external/base.fr.i18n vendored Normal file
View File

@@ -0,0 +1,4 @@
ExternalApp = "External"
ExternalAppCapital = "EXTERNAL"
ExternalAppApiMismatch = "API mismatch"
ExternalAppExecError = "Cannot execute file"

4
apps/external/base.pt.i18n vendored Normal file
View File

@@ -0,0 +1,4 @@
ExternalApp = "External"
ExternalAppCapital = "EXTERNAL"
ExternalAppApiMismatch = "API mismatch"
ExternalAppExecError = "Cannot execute file"

85
apps/external/extapp_api.cpp vendored Normal file
View File

@@ -0,0 +1,85 @@
#include <ion.h>
#include <kandinsky.h>
#include <escher.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include "extapp_api.h"
#include <python/port/port.h>
extern "C" {
#include <python/port/mphalport.h>
}
uint64_t extapp_millis() {
return Ion::Timing::millis();
}
void extapp_msleep(uint32_t ms) {
Ion::Timing::msleep(ms);
}
uint64_t extapp_scanKeyboard() {
return Ion::Keyboard::scan();
}
void extapp_pushRect(int16_t x, int16_t y, uint16_t w, uint16_t h, const uint16_t * pixels) {
KDRect rect(x, y, w, h);
Ion::Display::pushRect(rect, reinterpret_cast<const KDColor*>(pixels));
}
void extapp_pushRectUniform(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t color) {
KDRect rect(x, y, w, h);
Ion::Display::pushRectUniform(rect, KDColor::RGB16(color));
}
int16_t extapp_drawTextLarge(const char *text, int16_t x, int16_t y, uint16_t fg, uint16_t bg) {
KDPoint point(x, y);
auto ctx = KDIonContext::sharedContext();
ctx->setClippingRect(KDRect(0,0,320,240));
ctx->setOrigin(KDPoint(0,0));
ctx->drawString(text, point, KDFont::LargeFont, KDColor::RGB16(fg), KDColor::RGB16(bg));
return point.x();
}
int16_t extapp_drawTextSmall(const char *text, int16_t x, int16_t y, uint16_t fg, uint16_t bg) {
KDPoint point(x, y);
auto ctx = KDIonContext::sharedContext();
ctx->setClippingRect(KDRect(0,0,320,240));
ctx->setOrigin(KDPoint(0,0));
ctx->drawString(text, point, KDFont::SmallFont, KDColor::RGB16(fg), KDColor::RGB16(bg));
return point.x();
}
void extapp_waitForVBlank() {
Ion::Display::waitForVBlank();
}
void extapp_clipboardStore(const char *text) {
Clipboard::sharedClipboard()->store(text);
}
const char * extapp_clipboardText() {
return Clipboard::sharedClipboard()->storedText();
}
extern "C" void (* const apiPointers[])(void) = {
(void (*)(void)) extapp_millis,
(void (*)(void)) extapp_msleep,
(void (*)(void)) extapp_scanKeyboard,
(void (*)(void)) extapp_pushRect,
(void (*)(void)) extapp_pushRectUniform,
(void (*)(void)) extapp_drawTextLarge,
(void (*)(void)) extapp_drawTextSmall,
(void (*)(void)) extapp_waitForVBlank,
(void (*)(void)) extapp_clipboardStore,
(void (*)(void)) extapp_clipboardText,
(void (*)(void)) nullptr
};

76
apps/external/extapp_api.h vendored Normal file
View File

@@ -0,0 +1,76 @@
#ifndef EXTERNAL_API_H
#define EXTERNAL_API_H
#include <stdint.h>
#define API_VERSION 1
#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif
#define LCD_WIDTH 320
#define LCD_HEIGHT 240
#define KEY_Left ((uint64_t)1 << 0)
#define KEY_Up ((uint64_t)1 << 1)
#define KEY_Down ((uint64_t)1 << 2)
#define KEY_Right ((uint64_t)1 << 3)
#define KEY_OK ((uint64_t)1 << 4)
#define KEY_Back ((uint64_t)1 << 5)
#define KEY_Home ((uint64_t)1 << 6)
#define KEY_OnOff (((uint64_t)1 << 7) || ((uint64_t)1 << 8))
#define KEY_Shift ((uint64_t)1 << 12)
#define KEY_Alpha ((uint64_t)1 << 13)
#define KEY_XNT ((uint64_t)1 << 14)
#define KEY_Var ((uint64_t)1 << 15)
#define KEY_Toolbox ((uint64_t)1 << 16)
#define KEY_Backspace ((uint64_t)1 << 17)
#define KEY_Exp ((uint64_t)1 << 18)
#define KEY_Ln ((uint64_t)1 << 19)
#define KEY_Log ((uint64_t)1 << 20)
#define KEY_Imaginary ((uint64_t)1 << 21)
#define KEY_Comma ((uint64_t)1 << 22)
#define KEY_Power ((uint64_t)1 << 23)
#define KEY_Sine ((uint64_t)1 << 24)
#define KEY_Cosine ((uint64_t)1 << 25)
#define KEY_Tangent ((uint64_t)1 << 26)
#define KEY_Pi ((uint64_t)1 << 27)
#define KEY_Sqrt ((uint64_t)1 << 28)
#define KEY_Square ((uint64_t)1 << 29)
#define KEY_Seven ((uint64_t)1 << 30)
#define KEY_Eight ((uint64_t)1 << 31)
#define KEY_Nine ((uint64_t)1 << 32)
#define KEY_LeftParenthesis ((uint64_t)1 << 33)
#define KEY_RightParenthesis ((uint64_t)1 << 34)
#define KEY_Four ((uint64_t)1 << 36)
#define KEY_Five ((uint64_t)1 << 37)
#define KEY_Six ((uint64_t)1 << 38)
#define KEY_Multiplication ((uint64_t)1 << 39)
#define KEY_Division ((uint64_t)1 << 40)
#define KEY_One ((uint64_t)1 << 42)
#define KEY_Two ((uint64_t)1 << 43)
#define KEY_Three ((uint64_t)1 << 44)
#define KEY_Plus ((uint64_t)1 << 45)
#define KEY_Minus ((uint64_t)1 << 46)
#define KEY_Zero ((uint64_t)1 << 48)
#define KEY_Dot ((uint64_t)1 << 49)
#define KEY_EE ((uint64_t)1 << 50)
#define KEY_Ans ((uint64_t)1 << 51)
#define KEY_EXE ((uint64_t)1 << 52)
#define KEY_None ((uint64_t)1 << 54)
EXTERNC uint64_t extapp_millis();
EXTERNC void extapp_msleep(uint32_t ms);
EXTERNC uint64_t extapp_scanKeyboard();
EXTERNC void extapp_pushRect(int16_t x, int16_t y, uint16_t w, uint16_t h, const uint16_t * pixels);
EXTERNC void extapp_pushRectUniform(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t color);
EXTERNC int16_t extapp_drawTextLarge(const char *text, int16_t x, int16_t y, uint16_t fg, uint16_t bg);
EXTERNC int16_t extapp_drawTextSmall(const char *text, int16_t x, int16_t y, uint16_t fg, uint16_t bg);
EXTERNC void extapp_waitForVBlank();
EXTERNC void extapp_clipboardStore(const char *text);
EXTERNC const char * extapp_clipboardText();
#endif

BIN
apps/external/external_icon.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

98
apps/external/main_controller.cpp vendored Normal file
View File

@@ -0,0 +1,98 @@
#include "main_controller.h"
#include <apps/i18n.h>
#include <apps/apps_container.h>
#include <assert.h>
#include "archive.h"
#include "app.h"
using namespace Poincare;
namespace External {
using namespace Archive;
MainController::MainController(Responder * parentResponder, ::App * app) :
ViewController(parentResponder),
m_selectableTableView(this)
{
m_app = app;
}
View * MainController::view() {
return &m_selectableTableView;
}
void MainController::didBecomeFirstResponder() {
if (selectedRow() < 0) {
selectCellAtLocation(0, 0);
}
Container::activeApp()->setFirstResponder(&m_selectableTableView);
}
bool MainController::handleEvent(Ion::Events::Event event) {
if (numberOfRows() > 0 && (event == Ion::Events::OK || event == Ion::Events::EXE)) {
uint32_t res = executeFile(m_cells[selectedRow()].text(), ((App *)m_app)->heap(), ((App *)m_app)->heapSize());
((App*)m_app)->redraw();
switch(res) {
case 0:
break;
case 1:
Container::activeApp()->displayWarning(I18n::Message::ExternalAppApiMismatch);
break;
case 2:
Container::activeApp()->displayWarning(I18n::Message::StorageMemoryFull1);
break;
default:
Container::activeApp()->displayWarning(I18n::Message::ExternalAppExecError);
break;
}
return true;
}
return false;
}
int MainController::numberOfRows() const {
return k_numberOfCells;
};
KDCoordinate MainController::rowHeight(int j) {
return Metric::ParameterCellHeight;
}
KDCoordinate MainController::cumulatedHeightFromIndex(int j) {
return j*rowHeight(0);
}
int MainController::indexFromCumulatedHeight(KDCoordinate offsetY) {
return offsetY/rowHeight(0);
}
HighlightCell * MainController::reusableCell(int index, int type) {
assert(index < k_numberOfCells);
return &m_cells[index];
}
int MainController::reusableCellCount(int type) {
return k_numberOfCells;
}
int MainController::typeAtLocation(int i, int j) {
return 0;
}
void MainController::willDisplayCellForIndex(HighlightCell * cell, int index) {
PointerTextTableCell * myTextCell = (PointerTextTableCell *)cell;
struct File f;
if(fileAtIndex(index, f)) {
myTextCell->setText(f.name);
myTextCell->setTextColor(f.isExecutable ? KDColorBlack : Palette::GreyDark);
}
}
void MainController::viewWillAppear() {
int count = numberOfFiles();
k_numberOfCells = count <= k_maxNumberOfCells ? count : k_maxNumberOfCells;
m_selectableTableView.reloadData();
}
}

34
apps/external/main_controller.h vendored Normal file
View File

@@ -0,0 +1,34 @@
#ifndef EXTERNAL_MAIN_CONTROLLER_H
#define EXTERNAL_MAIN_CONTROLLER_H
#include <escher.h>
#include "pointer_text_table_cell.h"
namespace External {
class MainController : public ViewController, public ListViewDataSource, public SelectableTableViewDataSource {
public:
MainController(Responder * parentResponder, App * app);
View * view() override;
bool handleEvent(Ion::Events::Event event) override;
void didBecomeFirstResponder() override;
int numberOfRows() const override;
KDCoordinate rowHeight(int j) override;
KDCoordinate cumulatedHeightFromIndex(int j) override;
int indexFromCumulatedHeight(KDCoordinate offsetY) override;
HighlightCell * reusableCell(int index, int type) override;
int reusableCellCount(int type) override;
int typeAtLocation(int i, int j) override;
void willDisplayCellForIndex(HighlightCell * cell, int index) override;
void viewWillAppear() override;
private:
App * m_app;
SelectableTableView m_selectableTableView;
int k_numberOfCells = 1;
constexpr static int k_maxNumberOfCells = 16;
PointerTextTableCell m_cells[k_maxNumberOfCells];
};
}
#endif

View File

@@ -0,0 +1,38 @@
#include "pointer_text_table_cell.h"
#include <escher/buffer_text_view.h>
#include <escher/palette.h>
#include <assert.h>
PointerTextTableCell::PointerTextTableCell(const char * text, const KDFont * font, Layout layout) :
TableCell(layout),
m_pointerTextView(font, text, 0, 0.5, KDColorBlack, KDColorWhite)
{
}
View * PointerTextTableCell::labelView() const {
return (View *)&m_pointerTextView;
}
const char * PointerTextTableCell::text() const {
return m_pointerTextView.text();
}
void PointerTextTableCell::setHighlighted(bool highlight) {
HighlightCell::setHighlighted(highlight);
KDColor backgroundColor = highlight? Palette::ListCellBackgroundSelected : Palette::ListCellBackground;
m_pointerTextView.setBackgroundColor(backgroundColor);
}
void PointerTextTableCell::setText(const char * text) {
m_pointerTextView.setText(text);
layoutSubviews();
}
void PointerTextTableCell::setTextColor(KDColor color) {
m_pointerTextView.setTextColor(color);
}
void PointerTextTableCell::setTextFont(const KDFont * font) {
m_pointerTextView.setFont(font);
layoutSubviews();
}

20
apps/external/pointer_text_table_cell.h vendored Normal file
View File

@@ -0,0 +1,20 @@
#ifndef ESCHER_POINTER_TEXT_TABLE_CELL_H
#define ESCHER_POINTER_TEXT_TABLE_CELL_H
#include <escher/pointer_text_view.h>
#include <escher/table_cell.h>
class PointerTextTableCell : public TableCell {
public:
PointerTextTableCell(const char * text = "", const KDFont * font = KDFont::SmallFont, Layout layout = Layout::Horizontal);
View * labelView() const override;
const char * text() const override;
virtual void setHighlighted(bool highlight) override;
void setText(const char * text);
virtual void setTextColor(KDColor color);
void setTextFont(const KDFont * font);
private:
PointerTextView m_pointerTextView;
};
#endif

View File

@@ -7,7 +7,7 @@ EPSILON_VERSION ?= 12.0.0
EPSILON_CUSTOM_VERSION ?= 1.18.0-0
# USERNAME ?= N/A
# Valid values are "none", "update", "beta"
EPSILON_APPS ?= calculation rpn graph code statistics probability solver atom sequence regression settings
EPSILON_APPS ?= calculation rpn graph code statistics probability solver atom sequence regression settings external
EPSILON_I18N ?= en fr es de pt
EPSILON_GETOPT ?= 0
ESCHER_LOG_EVENTS_BINARY ?= 0