Skip to content

Commit db0f04f

Browse files
authored
Merge branch 'smallbasic:master' into master
2 parents 1d640ef + 00db337 commit db0f04f

10 files changed

Lines changed: 77 additions & 31 deletions

File tree

src/common/plugins.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ typedef int (*sblib_count_fn) (void);
4242
typedef int (*sblib_init_fn) (const char *);
4343
typedef int (*sblib_has_window_ui_fn) (void);
4444
typedef int (*sblib_free_fn) (int, int);
45+
typedef int (*sblib_refresh_id_fn) (int, int);
4546
typedef void (*sblib_close_fn) (void);
4647

4748
typedef struct {
@@ -51,6 +52,7 @@ typedef struct {
5152
sblib_exec_fn _sblib_proc_exec;
5253
sblib_exec_fn _sblib_func_exec;
5354
sblib_free_fn _sblib_free;
55+
sblib_refresh_id_fn _sblib_refresh_id;
5456
ext_func_node_t *_func_list;
5557
ext_proc_node_t *_proc_list;
5658
uint32_t _id;
@@ -327,6 +329,7 @@ static void slib_import_routines(slib_t *lib, int comp) {
327329
lib->_sblib_func_exec = slib_getoptptr(lib, "sblib_func_exec");
328330
lib->_sblib_proc_exec = slib_getoptptr(lib, "sblib_proc_exec");
329331
lib->_sblib_free = slib_getoptptr(lib, "sblib_free");
332+
lib->_sblib_refresh_id = slib_getoptptr(lib, "sblib_refresh_id");
330333
sblib_count_fn fcount = slib_getoptptr(lib, "sblib_proc_count");
331334
sblib_getname_fn fgetname = slib_getoptptr(lib, "sblib_proc_getname");
332335

@@ -565,6 +568,17 @@ void plugin_free(int lib_id, int cls_id, int id) {
565568
}
566569
}
567570

571+
int plugin_refresh_id(int lib_id, int cls_id, int id) {
572+
int result = id;
573+
if (lib_id != -1 && cls_id != -1 && id != -1) {
574+
slib_t *lib = get_lib(lib_id);
575+
if (lib && lib->_sblib_refresh_id) {
576+
result = lib->_sblib_refresh_id(cls_id, id);
577+
}
578+
}
579+
return result;
580+
}
581+
568582
void plugin_close() {
569583
for (int i = 0; i < MAX_SLIBS; i++) {
570584
if (plugins[i]) {
@@ -594,6 +608,7 @@ void *plugin_get_func(const char *name) { return 0; }
594608
int plugin_procexec(int lib_id, int index) { return -1; }
595609
int plugin_funcexec(int lib_id, int index, var_t *ret) { return -1; }
596610
void plugin_free(int lib_id, int cls_id, int id) {}
611+
int plugin_refresh_id(int lib_id, int cls_id, int id) {return id;}
597612
void plugin_close() {}
598613
#endif
599614

src/common/plugins.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ int plugin_funcexec(int lib_id, int index, var_t *ret);
7171
//
7272
void plugin_free(int lib_id, int cls_id, int id);
7373

74+
//
75+
// in v_move and plugin_free later called on source, dest remains registered
76+
//
77+
int plugin_refresh_id(int lib_id, int cls_id, int id);
78+
7479
//
7580
// closes the plugin system
7681
//

src/common/var.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "common/sys.h"
1111
#include "common/sberr.h"
12+
#include "common/plugins.h"
1213

1314
#define INT_STR_LEN 64
1415

@@ -571,9 +572,9 @@ void v_move(var_t *dest, const var_t *src) {
571572
dest->v.m.map = src->v.m.map;
572573
dest->v.m.count = src->v.m.count;
573574
dest->v.m.size = src->v.m.size;
574-
dest->v.m.id = src->v.m.id;
575575
dest->v.m.lib_id = src->v.m.lib_id;
576576
dest->v.m.cls_id = src->v.m.cls_id;
577+
dest->v.m.id = plugin_refresh_id(src->v.m.lib_id, src->v.m.cls_id, src->v.m.id);
577578
break;
578579
case V_REF:
579580
dest->v.ref = src->v.ref;

src/common/var_map.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,9 @@ void map_set(var_p_t dest, const var_p_t src) {
317317
hashmap_create(dest, src->v.m.count);
318318
hashmap_foreach(src, map_set_cb, &cb);
319319
dest->v.m.count = src->v.m.count;
320-
dest->v.m.id = src->v.m.id;
321-
dest->v.m.lib_id = -1;
322-
dest->v.m.cls_id = -1;
320+
dest->v.m.lib_id = src->v.m.lib_id;
321+
dest->v.m.cls_id = src->v.m.cls_id;
322+
dest->v.m.id = plugin_refresh_id(src->v.m.lib_id, src->v.m.cls_id, src->v.m.id);
323323
}
324324
}
325325

src/include/module.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,23 @@ int sblib_func_exec(int index, int param_count, slib_par_t *params, var_t *retva
120120
/**
121121
* @ingroup modlib
122122
*
123-
* executes a function
123+
* free resources associated with the variable
124124
*
125125
* @param cls_id the variable class identifier
126126
* @param id the variable instance identifier
127127
*/
128128
int sblib_free(int cls_id, int id);
129129

130+
/**
131+
* @ingroup modlib
132+
*
133+
* registers a fresh id to replace the given id
134+
*
135+
* @param cls_id the variable class identifier
136+
* @param id the variable instance identifier
137+
*/
138+
int sblib_refresh_id(int cls_id, int id);
139+
130140
/**
131141
* @ingroup modlib
132142
*

src/platform/android/app/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ android {
77
// can override some attributes in main/AndroidManifest.xml
88
defaultConfig {
99
applicationId 'net.sourceforge.smallbasic'
10-
minSdkVersion 21
10+
minSdkVersion 23
1111
targetSdkVersion 36
12-
versionCode 89
12+
versionCode 91
1313
versionName '12.33'
1414
resourceConfigurations += ['en']
1515
}
@@ -53,6 +53,6 @@ android {
5353
}
5454

5555
dependencies {
56-
implementation 'androidx.core:core:1.17.0'
56+
implementation 'androidx.core:core:1.18.0'
5757
testImplementation 'junit:junit:4.13.2'
5858
}

src/platform/android/app/src/main/java/net/sourceforge/smallbasic/MainActivity.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,8 +808,15 @@ protected void onCreate(Bundle savedInstanceState) {
808808

809809
@Override
810810
protected void onPause() {
811-
super.onPause();
812811
onActivityPaused(true);
812+
// wait for Graphics::redraw() to complete
813+
try {
814+
Thread.sleep(10);
815+
}
816+
catch (Exception e) {
817+
// ignored
818+
}
819+
super.onPause();
813820
}
814821

815822
@Override

src/platform/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
mavenCentral()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:9.0.0'
8+
classpath 'com.android.tools.build:gradle:9.1.1'
99
}
1010
}
1111

src/platform/android/jni/runtime.cpp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ Runtime *runtime = nullptr;
3333
// Pipe file descriptors: g_backPipe[0] is read-end, g_backPipe[1] is write-end
3434
static int g_backPipe[2] = {-1, -1};
3535

36-
// whether native back key handling is active
37-
static bool g_predictiveBack = false;
38-
3936
// the logical top of the screen
4037
static int g_top = 0;
4138

@@ -115,23 +112,18 @@ void handleCommand(android_app *app, int32_t cmd) {
115112
}
116113
}
117114

118-
static void pushBackEvent() {
119-
auto *maEvent = new MAEvent();
120-
maEvent->nativeKey = AKEYCODE_BACK;
121-
maEvent->type = EVENT_TYPE_KEY_PRESSED;
122-
runtime->pushEvent(maEvent);
123-
}
124-
125115
//
126116
// Callback registered with ALooper that is triggered when the pipe receives data.
127117
// This is what wakes the blocked ALooper_pollOnce() and lets us run pushBackEvent().
128118
//
129119
static int pipeCallback(int fd, int events, void *data) {
130120
// clear the byte that woke the pipe, then return 1 to stay registered
131-
logEntered();
132-
char buf[1];
133-
read(fd, buf, 1);
134-
pushBackEvent();
121+
if (runtime != nullptr && runtime->isActive()) {
122+
logEntered();
123+
char buf[1];
124+
read(fd, buf, 1);
125+
runtime->onBack();
126+
}
135127
return 1;
136128
}
137129

@@ -165,8 +157,9 @@ static void process_input(android_app *app, android_poll_source *source) {
165157
AKeyEvent_getKeyCode(event) == AKEYCODE_BACK) {
166158
// prevent AInputQueue_preDispatchEvent from attempting to close
167159
// the keypad here to avoid a crash in android 4.2 + 4.3.
168-
if (AKeyEvent_getAction(event) == AKEY_EVENT_ACTION_DOWN && runtime->isActive() && !g_predictiveBack) {
169-
pushBackEvent();
160+
if (AKeyEvent_getAction(event) == AKEY_EVENT_ACTION_DOWN && runtime != nullptr && runtime->isActive()) {
161+
trace("AKEYCODE_BACK event");
162+
runtime->onBack();
170163
}
171164
AInputQueue_finishEvent(app->inputQueue, event, true);
172165
} else if (!AInputQueue_preDispatchEvent(app->inputQueue, event)) {
@@ -297,8 +290,7 @@ Runtime::Runtime(android_app *app) :
297290
_looper = ALooper_forThread();
298291
_sensorManager = ASensorManager_getInstance();
299292
memset(&_sensors, 0, sizeof(_sensors));
300-
g_predictiveBack = getBoolean("isPredictiveBack");
301-
if (g_predictiveBack) {
293+
if (getBoolean("isPredictiveBack")) {
302294
setupBackWakePipe(_looper);
303295
}
304296
}
@@ -512,7 +504,7 @@ char *Runtime::loadResource(const char *fileName) {
512504

513505
MAEvent *Runtime::popEvent() {
514506
pthread_mutex_lock(&_mutex);
515-
MAEvent *result = _eventQueue->pop();
507+
auto *result = _eventQueue->pop();
516508
pthread_mutex_unlock(&_mutex);
517509
return result;
518510
}
@@ -1003,6 +995,21 @@ void Runtime::showKeypad(bool show) {
1003995
_app->activity->vm->DetachCurrentThread();
1004996
}
1005997

998+
void Runtime::onBack() {
999+
pthread_mutex_lock(&_mutex);
1000+
auto *current = _eventQueue->peek();
1001+
if (current == nullptr || current->nativeKey != AKEYCODE_BACK) {
1002+
trace("pushing back event to the queue");
1003+
auto *event = new MAEvent();
1004+
event->nativeKey = AKEYCODE_BACK;
1005+
event->type = EVENT_TYPE_KEY_PRESSED;
1006+
_eventQueue->push(event);
1007+
} else {
1008+
trace("skipping duplicate back event");
1009+
}
1010+
pthread_mutex_unlock(&_mutex);
1011+
}
1012+
10061013
void Runtime::onResize(int width, int height, int imeState) {
10071014
logEntered();
10081015
if (_graphics != nullptr) {
@@ -1051,7 +1058,7 @@ void Runtime::onUnicodeChar(int ch) {
10511058

10521059
char *Runtime::getClipboardText() {
10531060
char *result;
1054-
String text = getStringBytes("getClipboardText");
1061+
auto text = getStringBytes("getClipboardText");
10551062
if (!text.empty()) {
10561063
result = strdup(text.c_str());
10571064
} else {
@@ -1096,7 +1103,7 @@ void System::completeKeyword(int index) const {
10961103
int maGetEvent(MAEvent *event) {
10971104
int result;
10981105
if (runtime->hasEvent()) {
1099-
MAEvent *nextEvent = runtime->popEvent();
1106+
auto *nextEvent = runtime->popEvent();
11001107
event->point = nextEvent->point;
11011108
event->type = nextEvent->type;
11021109
delete nextEvent;

src/platform/android/jni/runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ struct Runtime : public System {
7272
void share(const char *path) override { setString("share", path); }
7373
void showCursor(CursorType cursorType) override {}
7474
void showKeypad(bool show);
75+
void onBack();
7576
void onPaused(bool paused) { if (_graphics != nullptr) {_graphics->onPaused(paused);} }
7677
void onResize(int w, int h, int imeState);
7778
void onRunCompleted() override;

0 commit comments

Comments
 (0)