Add Bluetooth audio/HID/discoverability/alias support, fix SD card hotplug & caller ID matching, and expand ReMEdy J2ME JSRs + 64MB GC
This commit is contained in:
1 parent
456d3fbc42
commit
471916424e
51 files changed
+2961
-305
No files matched your search
@@ -333,6 +333,7 @@ class AlsaSoundEngine {
|
||||
int playerId = -1;
|
||||
bool active = false;
|
||||
bool loop = false;
|
||||
size_t rawSize = 0;
|
||||
float gain = 0.8f;
|
||||
std::vector<MidiEvent> events;
|
||||
size_t eventIdx = 0;
|
||||
@@ -777,10 +778,20 @@ class AlsaSoundEngine {
|
||||
auto it = playerGains_.find(playerId);
|
||||
if (it != playerGains_.end()) g = it->second;
|
||||
|
||||
if (midiSeq_.active && midiSeq_.playerId == playerId &&
|
||||
midiSeq_.rawSize == mid.size() && midiSeq_.currentFrame > 0 &&
|
||||
midiSeq_.currentFrame < midiSeq_.totalFrames) {
|
||||
midiSeq_.loop = loop;
|
||||
midiSeq_.gain = g;
|
||||
lastActiveMs_ = nowMs();
|
||||
return;
|
||||
}
|
||||
|
||||
midiSeq_ = MidiSequence{};
|
||||
midiSeq_.playerId = playerId;
|
||||
midiSeq_.active = true;
|
||||
midiSeq_.loop = loop;
|
||||
midiSeq_.rawSize = mid.size();
|
||||
midiSeq_.gain = g;
|
||||
midiSeq_.events = std::move(frameEvents);
|
||||
midiSeq_.totalFrames =
|
||||
@@ -1132,12 +1143,32 @@ class AlsaSoundEngine {
|
||||
snd_pcm_close(pcm);
|
||||
pcm = nullptr;
|
||||
}
|
||||
if (!enabled || sysMuted) {
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
voices_.clear();
|
||||
for (auto it = pcmStreams_.begin(); it != pcmStreams_.end();) {
|
||||
if (!it->loop) {
|
||||
it = pcmStreams_.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
if (midiSeq_.active && !midiSeq_.loop) {
|
||||
midiSeq_.active = false;
|
||||
midiVoices_.clear();
|
||||
}
|
||||
}
|
||||
::usleep(10000);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!pcm) {
|
||||
for (const char* dev : {"hw:0,0", "default", "plughw:0,0"}) {
|
||||
bool preferDefault = (::access("/run/bluetoothd.pid", F_OK) == 0);
|
||||
const char* devsHwFirst[] = {"hw:0,0", "default", "plughw:0,0"};
|
||||
const char* devsDefFirst[] = {"default", "hw:0,0", "plughw:0,0"};
|
||||
const char** devs = preferDefault ? devsDefFirst : devsHwFirst;
|
||||
for (int di = 0; di < 3; ++di) {
|
||||
const char* dev = devs[di];
|
||||
if (snd_pcm_open(&pcm, dev, SND_PCM_STREAM_PLAYBACK, 0) == 0) {
|
||||
if (!configureLowLatency(pcm, kSampleRate, kPeriod, kBuffer)) {
|
||||
snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE,
|
||||
@@ -1577,13 +1608,29 @@ class CldcNetMediaRuntime {
|
||||
});
|
||||
|
||||
// java/lang/System & Runtime
|
||||
uint32_t sysOutRef = vm.allocObject("java/io/PrintStream");
|
||||
uint32_t sysErrRef = vm.allocObject("java/io/PrintStream");
|
||||
vm.setStaticFieldVal("java/lang/System", "out", "Ljava/io/PrintStream;",
|
||||
JVal::MakeRef(sysOutRef));
|
||||
vm.setStaticFieldVal("java/lang/System", "err", "Ljava/io/PrintStream;",
|
||||
JVal::MakeRef(sysErrRef));
|
||||
uint32_t boolTrueRef = vm.allocObject("java/lang/Boolean");
|
||||
vm.getObj(boolTrueRef)->setField("value", JVal::MakeInt(1));
|
||||
uint32_t boolFalseRef = vm.allocObject("java/lang/Boolean");
|
||||
vm.getObj(boolFalseRef)->setField("value", JVal::MakeInt(0));
|
||||
vm.setStaticFieldVal("java/lang/Boolean", "TRUE", "Ljava/lang/Boolean;",
|
||||
JVal::MakeRef(boolTrueRef));
|
||||
vm.setStaticFieldVal("java/lang/Boolean", "FALSE", "Ljava/lang/Boolean;",
|
||||
JVal::MakeRef(boolFalseRef));
|
||||
|
||||
vm.registerNative("java/lang/System", "currentTimeMillis", "()J",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeLong(wallTimeMs());
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/System", "gc", "()V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal*) {
|
||||
v.collectGarbage();
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/System", "exit", "(I)V",
|
||||
@@ -1653,6 +1700,7 @@ class CldcNetMediaRuntime {
|
||||
else if (key == "microedition.locale") val = "en-US";
|
||||
else if (key == "microedition.m3g.version") val = "1.1";
|
||||
else if (key == "microedition.io.file.FileConnection.version") val = "1.0";
|
||||
else if (key == "bluetooth.api.version") val = "1.1";
|
||||
else if (key == "file.separator") val = "/";
|
||||
else if (key == "fileconn.dir.photos") val = "file:///home/user/Pictures/";
|
||||
else if (key == "fileconn.dir.music") val = "file:///home/user/Music/";
|
||||
@@ -1674,17 +1722,18 @@ class CldcNetMediaRuntime {
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Runtime", "freeMemory", "()J",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeLong(16 * 1024 * 1024LL);
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeLong(v.freeHeapBytes());
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Runtime", "totalMemory", "()J",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeLong(32 * 1024 * 1024LL);
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeLong(v.totalHeapBytes());
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Runtime", "gc", "()V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal*) {
|
||||
v.collectGarbage();
|
||||
return true;
|
||||
});
|
||||
|
||||
@@ -2380,6 +2429,182 @@ class CldcNetMediaRuntime {
|
||||
*r = JVal::MakeRef(v.allocString(std::to_string(val)));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Integer", "toBinaryString",
|
||||
"(I)Ljava/lang/String;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
uint32_t u = static_cast<uint32_t>(a[0].i);
|
||||
if (u == 0) {
|
||||
*r = JVal::MakeRef(v.allocString("0"));
|
||||
return true;
|
||||
}
|
||||
std::string s;
|
||||
while (u > 0) {
|
||||
s.insert(s.begin(), (u & 1) ? '1' : '0');
|
||||
u >>= 1;
|
||||
}
|
||||
*r = JVal::MakeRef(v.allocString(s));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Integer", "toOctalString",
|
||||
"(I)Ljava/lang/String;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
char buf[32];
|
||||
std::snprintf(buf, sizeof(buf), "%o",
|
||||
static_cast<uint32_t>(a[0].i));
|
||||
*r = JVal::MakeRef(v.allocString(buf));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Character", "isDigit", "(C)Z",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
*r = JVal::MakeInt((a[0].i >= '0' && a[0].i <= '9') ? 1 : 0);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Character", "isLowerCase", "(C)Z",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
*r = JVal::MakeInt((a[0].i >= 'a' && a[0].i <= 'z') ? 1 : 0);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Character", "isUpperCase", "(C)Z",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
*r = JVal::MakeInt((a[0].i >= 'A' && a[0].i <= 'Z') ? 1 : 0);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Character", "toLowerCase", "(C)C",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
int c = a[0].i;
|
||||
if (c >= 'A' && c <= 'Z') c += ('a' - 'A');
|
||||
*r = JVal::MakeInt(c);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Character", "toUpperCase", "(C)C",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
int c = a[0].i;
|
||||
if (c >= 'a' && c <= 'z') c -= ('a' - 'A');
|
||||
*r = JVal::MakeInt(c);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Character", "digit", "(CI)I",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
int c = a[0].i;
|
||||
int radix = a[1].i;
|
||||
int d = -1;
|
||||
if (c >= '0' && c <= '9') d = c - '0';
|
||||
else if (c >= 'a' && c <= 'z') d = 10 + (c - 'a');
|
||||
else if (c >= 'A' && c <= 'Z') d = 10 + (c - 'A');
|
||||
if (d >= radix) d = -1;
|
||||
*r = JVal::MakeInt(d);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Float", "<init>", "*",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
if (!self || a.size() < 2) return true;
|
||||
float fv = (a[1].type == JType::Double)
|
||||
? static_cast<float>(a[1].d)
|
||||
: a[1].f;
|
||||
self->setField("value", JVal::MakeFloat(fv));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Float", "floatValue", "()F",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
const JObject* self = v.getObj(a[0].ref);
|
||||
*r = JVal::MakeFloat(self ? self->getField("value", 'F').f : 0.0f);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Float", "parseFloat", "(Ljava/lang/String;)F",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
*r = JVal::MakeFloat(
|
||||
std::strtof(v.getStringUtf8(a[0].ref).c_str(), nullptr));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Float", "valueOf", "(Ljava/lang/String;)Ljava/lang/Float;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
float fv = std::strtof(v.getStringUtf8(a[0].ref).c_str(), nullptr);
|
||||
uint32_t ref = v.allocObject("java/lang/Float");
|
||||
v.getObj(ref)->setField("value", JVal::MakeFloat(fv));
|
||||
*r = JVal::MakeRef(ref);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Float", "toString", "*",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
float fv = (a[0].type == JType::Ref)
|
||||
? (v.getObj(a[0].ref)
|
||||
? v.getObj(a[0].ref)->getField("value", 'F').f
|
||||
: 0.0f)
|
||||
: a[0].f;
|
||||
char buf[64];
|
||||
std::snprintf(buf, sizeof(buf), "%g", fv);
|
||||
*r = JVal::MakeRef(v.allocString(buf));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Float", "floatToIntBits", "(F)I",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
int32_t bits = 0;
|
||||
std::memcpy(&bits, &a[0].f, sizeof(int32_t));
|
||||
*r = JVal::MakeInt(bits);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Float", "intBitsToFloat", "(I)F",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
float fv = 0.0f;
|
||||
std::memcpy(&fv, &a[0].i, sizeof(float));
|
||||
*r = JVal::MakeFloat(fv);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Double", "<init>", "(D)V",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
if (self && a.size() >= 2) {
|
||||
self->setField("value", JVal::MakeDouble(a[1].d));
|
||||
}
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Double", "doubleValue", "()D",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
const JObject* self = v.getObj(a[0].ref);
|
||||
*r = JVal::MakeDouble(self ? self->getField("value", 'D').d : 0.0);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Double", "parseDouble", "(Ljava/lang/String;)D",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
*r = JVal::MakeDouble(
|
||||
std::strtod(v.getStringUtf8(a[0].ref).c_str(), nullptr));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Double", "valueOf", "(Ljava/lang/String;)Ljava/lang/Double;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
double dv = std::strtod(v.getStringUtf8(a[0].ref).c_str(), nullptr);
|
||||
uint32_t ref = v.allocObject("java/lang/Double");
|
||||
v.getObj(ref)->setField("value", JVal::MakeDouble(dv));
|
||||
*r = JVal::MakeRef(ref);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Double", "toString", "*",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
double dv = (a[0].type == JType::Ref)
|
||||
? (v.getObj(a[0].ref)
|
||||
? v.getObj(a[0].ref)->getField("value", 'D').d
|
||||
: 0.0)
|
||||
: a[0].d;
|
||||
char buf[64];
|
||||
std::snprintf(buf, sizeof(buf), "%g", dv);
|
||||
*r = JVal::MakeRef(v.allocString(buf));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Double", "doubleToLongBits", "(D)J",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
int64_t bits = 0;
|
||||
std::memcpy(&bits, &a[0].d, sizeof(int64_t));
|
||||
*r = JVal::MakeLong(bits);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Double", "longBitsToDouble", "(J)D",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
double dv = 0.0;
|
||||
std::memcpy(&dv, &a[0].l, sizeof(double));
|
||||
*r = JVal::MakeDouble(dv);
|
||||
return true;
|
||||
});
|
||||
|
||||
// java/lang/Math
|
||||
vm.registerNative("java/lang/Math", "abs", "(I)I",
|
||||
@@ -2982,7 +3207,10 @@ class CldcNetMediaRuntime {
|
||||
vm.registerNative("java/util/Hashtable", "clear", "()V",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
if (self) self->fields.clear();
|
||||
if (self) {
|
||||
self->fields.clear();
|
||||
self->refArr.clear();
|
||||
}
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/util/Hashtable", "size", "()I",
|
||||
@@ -2993,6 +3221,45 @@ class CldcNetMediaRuntime {
|
||||
: 0);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/util/Hashtable", "isEmpty", "()Z",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
*r = JVal::MakeInt(
|
||||
(!self || self->fields.empty()) ? 1 : 0);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/util/Hashtable", "keys",
|
||||
"()Ljava/util/Enumeration;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
uint32_t en = v.allocObject("java/util/Enumeration");
|
||||
JObject* eo = v.getObj(en);
|
||||
if (self && eo) {
|
||||
eo->nativeTag = 0;
|
||||
for (const auto& kv : self->fields) {
|
||||
eo->refArr.push_back(v.allocString(kv.first));
|
||||
}
|
||||
}
|
||||
*r = JVal::MakeRef(en);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/util/Hashtable", "elements",
|
||||
"()Ljava/util/Enumeration;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
uint32_t en = v.allocObject("java/util/Enumeration");
|
||||
JObject* eo = v.getObj(en);
|
||||
if (self && eo) {
|
||||
eo->nativeTag = 0;
|
||||
for (const auto& kv : self->fields) {
|
||||
if (kv.second.type == JType::Ref) {
|
||||
eo->refArr.push_back(kv.second.ref);
|
||||
}
|
||||
}
|
||||
}
|
||||
*r = JVal::MakeRef(en);
|
||||
return true;
|
||||
});
|
||||
|
||||
// java/util/Timer & TimerTask
|
||||
vm.registerNative("java/util/Timer", "<init>", "*",
|
||||
@@ -3436,6 +3703,199 @@ class CldcNetMediaRuntime {
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
// Reader & InputStreamReader (UTF-8 / ISO-8859-1 character stream decoder)
|
||||
auto readOneUtf8Char = [](NativeInputStream* nis) -> int {
|
||||
if (!nis) return -1;
|
||||
int b = nis->readByte();
|
||||
if (b < 0) return -1;
|
||||
if ((b & 0x80) == 0) return b;
|
||||
if ((b & 0xE0) == 0xC0) {
|
||||
int b2 = nis->readByte();
|
||||
if (b2 < 0) return b;
|
||||
return ((b & 0x1F) << 6) | (b2 & 0x3F);
|
||||
}
|
||||
if ((b & 0xF0) == 0xE0) {
|
||||
int b2 = nis->readByte();
|
||||
int b3 = nis->readByte();
|
||||
if (b2 < 0 || b3 < 0) return b;
|
||||
return ((b & 0x0F) << 12) | ((b2 & 0x3F) << 6) | (b3 & 0x3F);
|
||||
}
|
||||
return b;
|
||||
};
|
||||
for (const char* rdrCls : {"java/io/Reader", "java/io/InputStreamReader"}) {
|
||||
vm.registerNative(
|
||||
rdrCls, "<init>", "*",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
if (self && a.size() >= 2 && a[1].type == JType::Ref) {
|
||||
JObject* src = v.getObj(a[1].ref);
|
||||
if (src && src->nativeData) {
|
||||
self->nativeData = src->nativeData;
|
||||
}
|
||||
self->setField("in", a[1]);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
rdrCls, "read", "()I",
|
||||
[readOneUtf8Char](Jvm& v, JThread&, const std::vector<JVal>& a,
|
||||
JVal* r) {
|
||||
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||||
*r = JVal::MakeInt(readOneUtf8Char(nis.get()));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
rdrCls, "read", "([C)I",
|
||||
[readOneUtf8Char](Jvm& v, JThread&, const std::vector<JVal>& a,
|
||||
JVal* r) {
|
||||
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||||
JObject* arr = v.getObj(a[1].ref);
|
||||
if (!nis || !arr || arr->arrayKind != ArrayKind::Char) {
|
||||
*r = JVal::MakeInt(-1);
|
||||
return true;
|
||||
}
|
||||
int len = static_cast<int>(arr->charArr.size());
|
||||
if (len == 0) {
|
||||
*r = JVal::MakeInt(0);
|
||||
return true;
|
||||
}
|
||||
int count = 0;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
int c = readOneUtf8Char(nis.get());
|
||||
if (c < 0) break;
|
||||
arr->charArr[i] = static_cast<uint16_t>(c);
|
||||
++count;
|
||||
}
|
||||
*r = JVal::MakeInt(count == 0 ? -1 : count);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
rdrCls, "read", "([CII)I",
|
||||
[readOneUtf8Char](Jvm& v, JThread&, const std::vector<JVal>& a,
|
||||
JVal* r) {
|
||||
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||||
JObject* arr = v.getObj(a[1].ref);
|
||||
int off = a[2].i;
|
||||
int len = a[3].i;
|
||||
if (!nis || !arr || arr->arrayKind != ArrayKind::Char || off < 0 ||
|
||||
len < 0 || off + len > static_cast<int>(arr->charArr.size())) {
|
||||
*r = JVal::MakeInt(-1);
|
||||
return true;
|
||||
}
|
||||
if (len == 0) {
|
||||
*r = JVal::MakeInt(0);
|
||||
return true;
|
||||
}
|
||||
int count = 0;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
int c = readOneUtf8Char(nis.get());
|
||||
if (c < 0) break;
|
||||
arr->charArr[off + i] = static_cast<uint16_t>(c);
|
||||
++count;
|
||||
}
|
||||
*r = JVal::MakeInt(count == 0 ? -1 : count);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
rdrCls, "skip", "(J)J",
|
||||
[readOneUtf8Char](Jvm& v, JThread&, const std::vector<JVal>& a,
|
||||
JVal* r) {
|
||||
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||||
int64_t want = std::max<int64_t>(0, a[1].l);
|
||||
int64_t skipped = 0;
|
||||
while (skipped < want && readOneUtf8Char(nis.get()) >= 0) {
|
||||
++skipped;
|
||||
}
|
||||
*r = JVal::MakeLong(skipped);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
rdrCls, "ready", "()Z",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||||
*r = JVal::MakeInt((nis && nis->available() > 0) ? 1 : 0);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
rdrCls, "markSupported", "()Z",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeInt(1);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
rdrCls, "mark", "(I)V",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||||
if (nis) nis->markPos = nis->pos;
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
rdrCls, "reset", "()V",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||||
if (nis) nis->pos = nis->markPos;
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
rdrCls, "close", "()V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
}
|
||||
|
||||
// Writer & OutputStreamWriter
|
||||
for (const char* wrCls : {"java/io/Writer", "java/io/OutputStreamWriter"}) {
|
||||
vm.registerNative(
|
||||
wrCls, "<init>", "*",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
if (self && a.size() >= 2 && a[1].type == JType::Ref) {
|
||||
JObject* sub = v.getObj(a[1].ref);
|
||||
if (sub && sub->nativeData) self->nativeData = sub->nativeData;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
wrCls, "write", "(I)V",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
||||
if (nos) {
|
||||
uint16_t ch = static_cast<uint16_t>(a[1].i);
|
||||
std::string s = Jvm::u16ToUtf8(&ch, 1);
|
||||
nos->writeBytes(reinterpret_cast<const uint8_t*>(s.data()),
|
||||
static_cast<int>(s.size()));
|
||||
}
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
wrCls, "write", "*",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
||||
if (!nos || a.size() < 2 || a[1].type != JType::Ref) return true;
|
||||
JObject* arg = v.getObj(a[1].ref);
|
||||
if (!arg) return true;
|
||||
std::string s;
|
||||
if (arg->arrayKind == ArrayKind::Char) {
|
||||
int off = (a.size() >= 4) ? a[2].i : 0;
|
||||
int len = (a.size() >= 4) ? a[3].i
|
||||
: static_cast<int>(arg->charArr.size());
|
||||
off = std::clamp(off, 0, static_cast<int>(arg->charArr.size()));
|
||||
len = std::clamp(len, 0,
|
||||
static_cast<int>(arg->charArr.size()) - off);
|
||||
s = Jvm::u16ToUtf8(arg->charArr.data() + off, len);
|
||||
} else {
|
||||
s = v.getStringUtf8(a[1].ref);
|
||||
}
|
||||
nos->writeBytes(reinterpret_cast<const uint8_t*>(s.data()),
|
||||
static_cast<int>(s.size()));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
wrCls, "flush", "()V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
vm.registerNative(
|
||||
wrCls, "close", "()V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
}
|
||||
}
|
||||
|
||||
void registerRms(Jvm& vm) {
|
||||
@@ -4161,6 +4621,7 @@ class CldcNetMediaRuntime {
|
||||
"javax/microedition/media/Control",
|
||||
"javax/microedition/media/control/VolumeControl",
|
||||
"javax/microedition/media/control/ToneControl",
|
||||
"javax/microedition/media/control/MIDIControl",
|
||||
"com/nokia/mid/sound/Sound"}) {
|
||||
vm.registerNative(
|
||||
pCls, "<init>", "*",
|
||||
@@ -4331,13 +4792,12 @@ class CldcNetMediaRuntime {
|
||||
return true;
|
||||
}
|
||||
if (np->state == 400 && !np->data.empty()) {
|
||||
bool isMidi = (np->data.size() >= 4 &&
|
||||
std::memcmp(np->data.data(), "MThd", 4) == 0);
|
||||
bool isLooping = (np->loopCount < 0 || np->loopCount > 1);
|
||||
if (!isMidi && !isLooping) {
|
||||
// One-shot WAV SFX play asynchronously in the 6-channel mixer;
|
||||
// report PREFETCHED (300) so games that check getState() == 400
|
||||
// before starting music or rapid SFX never drop sounds.
|
||||
if (!isLooping) {
|
||||
// One-shot WAV/MIDI SFX and jingles play asynchronously in the
|
||||
// mixer; report PREFETCHED (300) so games that check
|
||||
// getState() == 400 before loading a level or playing SFX
|
||||
// never deadlock.
|
||||
np->state = 300;
|
||||
} else if (!sound.isPlayerActive(np->playerId)) {
|
||||
np->state = 300; // Transition back to PREFETCHED when finished
|
||||
@@ -4414,9 +4874,12 @@ class CldcNetMediaRuntime {
|
||||
? v.getStringUtf8(a[1].ref)
|
||||
: "VolumeControl";
|
||||
std::string clsName =
|
||||
(ctrlType.find("ToneControl") != std::string::npos)
|
||||
? "javax/microedition/media/control/ToneControl"
|
||||
: "javax/microedition/media/control/VolumeControl";
|
||||
"javax/microedition/media/control/VolumeControl";
|
||||
if (ctrlType.find("ToneControl") != std::string::npos) {
|
||||
clsName = "javax/microedition/media/control/ToneControl";
|
||||
} else if (ctrlType.find("MIDIControl") != std::string::npos) {
|
||||
clsName = "javax/microedition/media/control/MIDIControl";
|
||||
}
|
||||
uint32_t cref = v.allocObject(clsName);
|
||||
JObject* co = v.getObj(cref);
|
||||
if (co && self) {
|
||||
@@ -4519,7 +4982,296 @@ class CldcNetMediaRuntime {
|
||||
}
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
pCls, "shortMidiEvent", "(III)V",
|
||||
[this, ensureNativePlayer](Jvm& v, JThread&,
|
||||
const std::vector<JVal>& a, JVal*) {
|
||||
if (a.size() >= 4) {
|
||||
int status = a[1].i & 0xF0;
|
||||
int data1 = a[2].i & 0x7F;
|
||||
int data2 = a[3].i & 0x7F;
|
||||
if (status == 0x90 && data2 > 0) {
|
||||
NativePlayer* np = ensureNativePlayer(v.getObj(a[0].ref));
|
||||
int vol = np ? (np->muted ? 0 : np->volumeLevel) : 80;
|
||||
sound.playTone(data1, 90,
|
||||
std::clamp((data2 * vol) / 127, 0, 100));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
pCls, "longMidiEvent", "([BII)I",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
*r = JVal::MakeInt(a.size() >= 4 ? a[3].i : 0);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
pCls, "setChannelVolume", "(II)V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
vm.registerNative(
|
||||
pCls, "setProgram", "(III)V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
}
|
||||
|
||||
// StreamConnectionNotifier & ServerSocketConnection
|
||||
for (const char* scnCls :
|
||||
{"javax/microedition/io/StreamConnectionNotifier",
|
||||
"javax/microedition/io/ServerSocketConnection"}) {
|
||||
vm.registerNative(
|
||||
scnCls, "acceptAndOpen",
|
||||
"()Ljavax/microedition/io/StreamConnection;",
|
||||
[](Jvm& v, JThread& th, const std::vector<JVal>&, JVal*) {
|
||||
v.throwException(th, "java/io/IOException",
|
||||
"Connection closed");
|
||||
return false;
|
||||
});
|
||||
vm.registerNative(
|
||||
scnCls, "close", "()V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
}
|
||||
|
||||
// JSR-82 Bluetooth API (javax.bluetooth.*)
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/LocalDevice", "getLocalDevice",
|
||||
"()Ljavax/bluetooth/LocalDevice;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocObject("javax/bluetooth/LocalDevice"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/LocalDevice", "getProperty",
|
||||
"(Ljava/lang/String;)Ljava/lang/String;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocString("1.1"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/LocalDevice", "getDiscoveryAgent",
|
||||
"()Ljavax/bluetooth/DiscoveryAgent;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocObject("javax/bluetooth/DiscoveryAgent"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/LocalDevice", "getFriendlyName",
|
||||
"()Ljava/lang/String;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocString("Nokia 8110 4G"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/LocalDevice", "getBluetoothAddress",
|
||||
"()Ljava/lang/String;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocString("001122334455"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/LocalDevice", "setDiscoverable", "(I)Z",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeInt(1);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/LocalDevice", "getDiscoverable", "()I",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeInt(0x9E8B33);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/LocalDevice", "getRecord",
|
||||
"(Ljavax/microedition/io/Connection;)Ljavax/bluetooth/ServiceRecord;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocObject("javax/bluetooth/ServiceRecord"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/LocalDevice", "updateRecord",
|
||||
"(Ljavax/bluetooth/ServiceRecord;)V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/DiscoveryAgent", "startInquiry",
|
||||
"(ILjavax/bluetooth/DiscoveryListener;)Z",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
if (a.size() >= 3 && a[2].ref != 0) {
|
||||
v.invokeMethodSync(a[2].ref, "inquiryCompleted", "(I)V",
|
||||
{JVal::MakeInt(0)});
|
||||
}
|
||||
*r = JVal::MakeInt(1);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/DiscoveryAgent", "cancelInquiry",
|
||||
"(Ljavax/bluetooth/DiscoveryListener;)Z",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeInt(1);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/DiscoveryAgent", "searchServices",
|
||||
"([I[Ljavax/bluetooth/UUID;Ljavax/bluetooth/RemoteDevice;Ljavax/bluetooth/DiscoveryListener;)I",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
if (a.size() >= 5 && a[4].ref != 0) {
|
||||
v.invokeMethodSync(a[4].ref, "serviceSearchCompleted", "(II)V",
|
||||
{JVal::MakeInt(1), JVal::MakeInt(4)});
|
||||
}
|
||||
*r = JVal::MakeInt(1);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/DiscoveryAgent", "cancelServiceSearch", "(I)Z",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeInt(1);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/DiscoveryAgent", "retrieveDevices",
|
||||
"(I)[Ljavax/bluetooth/RemoteDevice;",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::Null();
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/RemoteDevice", "getFriendlyName",
|
||||
"(Z)Ljava/lang/String;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocString("BT Device"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/RemoteDevice", "getBluetoothAddress",
|
||||
"()Ljava/lang/String;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocString("001122334455"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/ServiceRecord", "getConnectionURL",
|
||||
"(IZ)Ljava/lang/String;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocString("btspp://001122334455:1"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/ServiceRecord", "getHostDevice",
|
||||
"()Ljavax/bluetooth/RemoteDevice;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocObject("javax/bluetooth/RemoteDevice"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/ServiceRecord", "getAttributeValue",
|
||||
"(I)Ljavax/bluetooth/DataElement;",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::Null();
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/ServiceRecord", "setAttributeValue",
|
||||
"(ILjavax/bluetooth/DataElement;)Z",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeInt(1);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/ServiceRecord", "setDeviceServiceClasses", "(I)V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/DataElement", "<init>", "*",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
if (self && a.size() >= 3) self->setField("value", a[2]);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/DataElement", "addElement",
|
||||
"(Ljavax/bluetooth/DataElement;)V",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
if (self && a.size() >= 2) self->refArr.push_back(a[1].ref);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/DataElement", "getValue", "()Ljava/lang/Object;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
if (self && !self->refArr.empty()) {
|
||||
uint32_t en = v.allocObject("java/util/Enumeration");
|
||||
JObject* eo = v.getObj(en);
|
||||
eo->refArr = self->refArr;
|
||||
eo->nativeTag = 0;
|
||||
*r = JVal::MakeRef(en);
|
||||
} else {
|
||||
*r = self ? self->getField("value", 'L') : JVal::Null();
|
||||
}
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/UUID", "<init>", "*",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
if (self && a.size() >= 2 && a[1].type == JType::Ref) {
|
||||
self->strUtf8 = v.getStringUtf8(a[1].ref);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
// JSR-75 FileSystemRegistry & FileConnection extras
|
||||
vm.registerNative(
|
||||
"javax/microedition/io/file/FileSystemRegistry", "listRoots",
|
||||
"()Ljava/util/Enumeration;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
uint32_t en = v.allocObject("java/util/Enumeration");
|
||||
JObject* eo = v.getObj(en);
|
||||
if (eo) {
|
||||
eo->nativeTag = 0;
|
||||
eo->refArr.push_back(v.allocString("root1/"));
|
||||
}
|
||||
*r = JVal::MakeRef(en);
|
||||
return true;
|
||||
});
|
||||
for (const char* fcMethod : {"exists", "isDirectory", "canRead", "canWrite"}) {
|
||||
vm.registerNative(
|
||||
"javax/microedition/io/file/FileConnection", fcMethod, "()Z",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeInt(0);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
vm.registerNative(
|
||||
"javax/microedition/io/file/FileConnection", "fileSize", "()J",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeLong(0);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/microedition/io/file/FileConnection", "list", "*",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocObject("java/util/Enumeration"));
|
||||
return true;
|
||||
});
|
||||
for (const char* fcVoid : {"create", "mkdir", "delete", "truncate"}) {
|
||||
vm.registerNative(
|
||||
"javax/microedition/io/file/FileConnection", fcVoid, "*",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
}
|
||||
|
||||
// JSR-120 Wireless Messaging API (javax.wireless.messaging.*)
|
||||
vm.registerNative(
|
||||
"javax/wireless/messaging/MessageConnection", "newMessage", "*",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(
|
||||
v.allocObject("javax/wireless/messaging/TextMessage"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/wireless/messaging/MessageConnection", "send",
|
||||
"(Ljavax/wireless/messaging/Message;)V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
vm.registerNative(
|
||||
"javax/wireless/messaging/TextMessage", "setPayloadText",
|
||||
"(Ljava/lang/String;)V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user