From aa94d80145461cb66eb151068d48b8e1b457e265 Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Sat, 7 Dec 2019 22:05:31 -0700 Subject: [PATCH] ready to try executing actual Python within UPIWIN --- scripts/tmp_main.py | 19 +++++++++++++++ src/config.c | 48 ++++++++++++++++++++++++++++++++++++++ src/config.h | 3 +++ src/ep_init.c | 57 +++++++++++++++++++++++++++++++++++++++++++-- src/ep_init.h | 1 + src/main.c | 19 +++------------ src/scode.h | 3 ++- src/wintype.h | 2 ++ 8 files changed, 133 insertions(+), 19 deletions(-) create mode 100644 scripts/tmp_main.py diff --git a/scripts/tmp_main.py b/scripts/tmp_main.py new file mode 100644 index 0000000..12a6781 --- /dev/null +++ b/scripts/tmp_main.py @@ -0,0 +1,19 @@ +# Initial test script +FBPRIMCLR_BLACK = 0x0000 +FBPRIMCLR_RED = 0xF800 +FBPRIMCLR_GREEN = 0x07E0 +FBPRIMCLR_BLUE = 0x001F +FBPRIMCLR_YELLOW = 0xFFE0 +FBPRIMCLR_CYAN = 0x07FF +FBPRIMCLR_MAGENTA = 0xF81F +FBPRIMCLR_WHITE = 0xFFFF + +upiwin_tmp.filled_rectangle(10, 10, 50, 50, FBPRIMCLR_RED, False) +upiwin_tmp.filled_rectangle(60, 10, 100, 50, FBPRIMCLR_GREEN, False) +upiwin_tmp.filled_rectangle(110, 10, 150, 50, FBPRIMCLR_BLUE, False) +upiwin_tmp.filled_rectangle(10, 60, 50, 100, FBPRIMCLR_CYAN, False) +upiwin_tmp.filled_rectangle(60, 60, 100, 100, FBPRIMCLR_MAGENTA, False) +upiwin_tmp.filled_rectangle(110, 60, 150, 100, FBPRIMCLR_YELLOW, False) +upiwin_tmp.rectangle(10, 110, 150, 150, FBPRIMCLR_WHITE, False) +upiwin_tmp.line(10, 110, 150, 150, FBPRIMCLR_WHITE, False) +upiwin_tmp.line(10, 150, 150, 110, FBPRIMCLR_WHITE, False) diff --git a/src/config.c b/src/config.c index d9aa37a..ecf476c 100644 --- a/src/config.c +++ b/src/config.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -75,6 +76,7 @@ static HRESULT parse_cmdline(int argc, char *argv[], GLOBAL_CONFIG *parsed) { int c; PSTR pstr; + PPCSTR pargs; BOOL help = FALSE; memset(parsed, 0, sizeof(GLOBAL_CONFIG)); @@ -123,6 +125,48 @@ static HRESULT parse_cmdline(int argc, char *argv[], GLOBAL_CONFIG *parsed) fputs(helptext, stdout); return S_FALSE; } + + if (optind < argc) + { + pstr = realpath(argv[optind], NULL); /* implicit strdup */ + if (!pstr) + { + Log(LERROR, "Out of memory in parse_cmdline"); + return E_OUTOFMEMORY; + } + if (access(pstr, R_OK)) + { + fprintf(stderr, "%s: script %s not found\n", argv[0], pstr); + return UPIWIN_E_INVALIDSCRIPT; + } + parsed->script_name = pstr; + if (++optind < argc) + { + parsed->script_arg_count = argc - optind; + pargs = (PPCSTR)malloc(sizeof(PCSTR) * parsed->script_arg_count); + if (!pargs) + { + Log(LERROR, "Out of memory in parse_cmdline"); + return E_OUTOFMEMORY; + } + for (c = 0; c < parsed->script_arg_count; c++) + { + pargs[c] = strdup(argv[optind++]); + if (!(pargs[c])) + { + Log(LERROR, "Out of memory in parse_cmdline"); + return E_OUTOFMEMORY; + } + } + parsed->script_args = pargs; + } + } + else + { + fprintf(stderr, "%s: no script specified\n", argv[0], c); + return UPIWIN_E_NOSCRIPT; + } + return S_OK; } @@ -132,6 +176,10 @@ static void overlay_config(GLOBAL_CONFIG *p) Gconfig.framebuffer_device = p->framebuffer_device; if (p->touchscreen_device) Gconfig.touchscreen_device = p->touchscreen_device; + /* always overlay the script name and arguments */ + Gconfig.script_name = p->script_name; + Gconfig.script_arg_count = p->script_arg_count; + Gconfig.script_args = p->script_args; } HRESULT Config_setup(int argc, char *argv[]) diff --git a/src/config.h b/src/config.h index 1bb94e4..0a514a7 100644 --- a/src/config.h +++ b/src/config.h @@ -12,6 +12,9 @@ typedef struct tagGLOBAL_CONFIG { PCSTR python_loc; /* location of the Python3 executable */ UINT32 button_debounce; /* minimum time between button up and next button down (ms) */ UINT32 sys_mq_length; /* length of system message queue */ + PCSTR script_name; /* script name to be run */ + INT32 script_arg_count; /* number of arguments to pass to the script */ + PPCSTR script_args; /* arguments to pass to the script */ } GLOBAL_CONFIG; extern GLOBAL_CONFIG Gconfig; /* one global configuration to rule them all */ diff --git a/src/ep_init.c b/src/ep_init.c index 83f3de3..3eba18b 100644 --- a/src/ep_init.c +++ b/src/ep_init.c @@ -1,3 +1,6 @@ +#include +#include +#include #define PY_SSIZE_T_CLEAN #include #include "scode.h" @@ -24,8 +27,7 @@ static void epython_cleanup(void) UPIWIN_tmp_module = NULL; Py_DECREF(UPIWIN_module); UPIWIN_module = NULL; - if (!Py_FinalizeEx()) - Log(LWARN, "errors encountered when Python uninitialized itself"); + Py_FinalizeEx(); PyMem_RawFree(python_name); python_name = NULL; } @@ -91,3 +93,54 @@ error_0: python_name = NULL; return hr; } + +HRESULT Epython_run(void) +{ + HRESULT hr = S_OK; + int i; + FILE *fp; + wchar_t **args; + + Log(LINFO, "Ready to execute %s", Gconfig.script_name); + fp = fopen(Gconfig.script_name, "rb"); + if (fp) + { + args = (wchar_t **)alloca((Gconfig.script_arg_count + 1) * sizeof(wchar_t *)); + memset(args, 0, (Gconfig.script_arg_count + 1) * sizeof(wchar_t *)); + args[0] = Py_DecodeLocale(Gconfig.script_name, NULL); + if (args[0]) + { + for (i=0; i