From 94843fa7979ca0f892ea92b279f0ae4ec48697a7 Mon Sep 17 00:00:00 2001 From: Steven Tracey Date: Thu, 21 Nov 2024 16:28:39 -0500 Subject: [PATCH] Home Page --- c/libs/go_p2fa.h | 22 +++++++++++++++++++++- c/src/p2fa.c | 24 ++++++++++++++++++++++-- go/ctils.go | 24 ++++++++++++++++++++++++ go/totp.go | 18 ++++++++++++++---- 4 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 go/ctils.go diff --git a/c/libs/go_p2fa.h b/c/libs/go_p2fa.h index c8e2c45..9ea0391 100644 --- a/c/libs/go_p2fa.h +++ b/c/libs/go_p2fa.h @@ -19,6 +19,12 @@ typedef struct { const char *p; ptrdiff_t n; } _GoString_; /* Start of preamble from import "C" comments. */ +#line 3 "ctils.go" + +#include + +#line 1 "cgo-generated-wrapper" + /* End of preamble from import "C" comments. */ @@ -74,8 +80,22 @@ typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; extern "C" { #endif + +/* Return type for prepareCStringArray */ +struct prepareCStringArray_return { + char** r0; + GoInt r1; +}; +extern __declspec(dllexport) struct prepareCStringArray_return prepareCStringArray(GoSlice goStrings); +extern __declspec(dllexport) void freeCStringArray(char** cStrings, GoInt length); extern __declspec(dllexport) void loadConfigs(); -extern __declspec(dllexport) GoSlice getConfigNames(); + +/* Return type for getConfigNames */ +struct getConfigNames_return { + char** r0; + int r1; +}; +extern __declspec(dllexport) struct getConfigNames_return getConfigNames(); extern __declspec(dllexport) char* getCode(GoString configName); extern __declspec(dllexport) GoUint64 getTimeRemaining(GoInt n); extern __declspec(dllexport) GoUint64 getTimeRemainingMS(GoInt n); diff --git a/c/src/p2fa.c b/c/src/p2fa.c index cc79c01..af0fdc2 100644 --- a/c/src/p2fa.c +++ b/c/src/p2fa.c @@ -15,6 +15,9 @@ #include "../libs/GUI/GUI_BMPfile.h" #include "../libs/Config/Debug.h" +extern char** getConfigNames(int* length); +extern void freeCStringArray(char** cStrings, int length); + void Handler(int sigNum) { DebugLine(28, 0, "Caught signal %d\n", sigNum); EPD_2in13_V4_Init(); @@ -31,19 +34,35 @@ void Handler(int sigNum) { } void home(UWORD *blackImage) { + int length; + char** configNames = getConfigNames(&length); + + if (configNames == NULL || length == 0) { + DebugLine(10, 0, "No configs found\n"); + return; + } + Paint_NewImage(blackImage, EPD_2in13_V4_WIDTH, EPD_2in13_V4_HEIGHT, 0, WHITE); EPD_2in13_V4_Init(); + Paint_SelectImage(blackImage); + Paint_Clear(WHITE); + for (int i = 0; i < length; ++i) { + Paint_DrawString(5, 5 + (i * 20), configNames[i], &Font12, WHITE, BLACK); + } + EPD_2in13_V4_Display_Base(blackImage); + freeCStringArray(configNames, length); } void code(UWORD *blackImage) { int ch; + codeLoop: while(1) { ch = getch(); if (ch != ERR) { if (ch == 'q') break; - DebugLine(2, 0, "Key Pressed: %c\n", ch); + DebugLine(2, 0, "Key Pressed: %c\n", ch); } Paint_NewImage(blackImage, EPD_2in13_V4_WIDTH, EPD_2in13_V4_HEIGHT, 90, WHITE); @@ -72,7 +91,7 @@ void code(UWORD *blackImage) { for (int i = getTimeRemainingMS(30);; i = getTimeRemainingMS(30)) { ch = getch(); if (ch != ERR) { - if (ch == 'q') break; + if (ch == 'q') return; } if (i > lastI) break; startTime = clock(); @@ -117,6 +136,7 @@ int drawLoop() { return -1; } + home(blackImage); code(blackImage); return 0; diff --git a/go/ctils.go b/go/ctils.go new file mode 100644 index 0000000..92ad350 --- /dev/null +++ b/go/ctils.go @@ -0,0 +1,24 @@ +package main + +/* +#include +*/ +import "C" +import "unsafe" + +//export prepareCStringArray +func prepareCStringArray(goStrings []string) (**C.char, int) { + cStrings := make([]*C.char, len(goStrings)) + for i, s := range goStrings { + cStrings[i] = C.CString(s) + } + return (**C.char)(unsafe.Pointer(&cStrings[0])), len(goStrings) +} + +//export freeCStringArray +func freeCStringArray(cStrings **C.char, length int) { + array := (*[1 << 30]*C.char)(unsafe.Pointer(cStrings))[:length:length] + for _, s := range array { + C.free(unsafe.Pointer(s)) + } +} diff --git a/go/totp.go b/go/totp.go index 5236c9f..637cde9 100644 --- a/go/totp.go +++ b/go/totp.go @@ -10,10 +10,12 @@ import ( "hash" "log" "os" + "path/filepath" "runtime" "strconv" "strings" "time" + "unsafe" ) var token string @@ -119,17 +121,25 @@ func loadConfigs() { Format: format, } - configs[fileEntry.Name()] = config + fileName := fileEntry.Name() + ext := filepath.Ext(fileName) + + configs[strings.TrimSuffix(fileName, ext)] = config } } //export getConfigNames -func getConfigNames() []string { +func getConfigNames() (**C.char, C.int) { names := make([]string, 0, len(configs)) for name := range configs { names = append(names, name) } - return names + cStrings := make([]*C.char, len(names)) + for i, name := range names { + cStrings[i] = C.CString(name) + } + + return (**C.char)(unsafe.Pointer(&cStrings[0])), C.int(len(names)) } //export getCode @@ -150,4 +160,4 @@ func getTimeRemainingMS(n int) uint64 { return (uint64(n) * 1000) - (uint64(time.Now().UnixNano()/1e6) % (uint64(n) * 1000)) } -func main() {} +//func main() {}