2026-10-28 12:41:00 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
// ReMEdy J2ME (MIDP 2.0 / CLDC 1.1) Emulator for Bananarchy Linux
|
|
|
|
|
// Part 3: CLDC 1.1 Standard Library, RMS, GCF Sockets/HTTP, and ALSA MMAPI Audio
|
|
|
|
|
|
|
|
|
|
#ifndef BANANARCHY_REMEDY_CLDC_NET_MEDIA_HPP
|
|
|
|
|
#define BANANARCHY_REMEDY_CLDC_NET_MEDIA_HPP
|
|
|
|
|
|
|
|
|
|
#include "remedy_jvm.hpp"
|
|
|
|
|
|
|
|
|
|
#include <alsa/asoundlib.h>
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <atomic>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
#include <netdb.h>
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
|
#include <poll.h>
|
2026-10-28 15:25:07 +02:00
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <sched.h>
|
|
|
|
|
#include <sys/resource.h>
|
2026-10-28 12:41:00 +02:00
|
|
|
#include <random>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
|
|
namespace remedy {
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
2026-10-28 15:25:07 +02:00
|
|
|
// ALSA Low-Latency Tone, Standard MIDI (Format 0/1) & WAV Audio Engine
|
2026-10-28 12:41:00 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
class AlsaSoundEngine {
|
|
|
|
|
public:
|
2026-10-28 15:25:07 +02:00
|
|
|
static constexpr unsigned int kSampleRate = 48000;
|
|
|
|
|
// ReMEdy's own audio on/off toggle (pause menu "Audio" item, remedy.cfg).
|
2026-10-28 12:41:00 +02:00
|
|
|
bool enabled = true;
|
2026-10-28 15:25:07 +02:00
|
|
|
// Mirror of the system-wide speaker volume (0..100), as set by the xdrawer
|
|
|
|
|
// "Speaker" slider and stored in $XDG_CONFIG_HOME/sxmo/speaker_vol.
|
|
|
|
|
std::atomic<int> volumePct{100};
|
2026-10-28 12:41:00 +02:00
|
|
|
|
|
|
|
|
~AlsaSoundEngine() { shutdown(); }
|
|
|
|
|
|
|
|
|
|
void init() {
|
2026-10-28 15:25:07 +02:00
|
|
|
refreshSystemVolume(true);
|
2026-10-28 12:41:00 +02:00
|
|
|
if (running_.exchange(true)) return;
|
|
|
|
|
worker_ = std::thread([this]() { audioThreadLoop(); });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void shutdown() {
|
|
|
|
|
if (!running_.exchange(false)) return;
|
|
|
|
|
if (worker_.joinable()) worker_.join();
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-28 15:25:07 +02:00
|
|
|
void setPaused(bool p) { paused_.store(p, std::memory_order_relaxed); }
|
|
|
|
|
|
|
|
|
|
bool isSystemMuted() const {
|
|
|
|
|
return sysMuted_.load(std::memory_order_relaxed);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-29 20:42:26 +02:00
|
|
|
static int readSimpleInt(const char* path, int fallback) {
|
|
|
|
|
FILE* f = std::fopen(path, "r");
|
|
|
|
|
if (!f) return fallback;
|
|
|
|
|
int v = fallback;
|
|
|
|
|
(void)std::fscanf(f, "%d", &v);
|
|
|
|
|
std::fclose(f);
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-28 15:25:07 +02:00
|
|
|
static std::string sxmoConfigDir() {
|
|
|
|
|
const char* xdg = std::getenv("XDG_CONFIG_HOME");
|
|
|
|
|
if (xdg && *xdg) return std::string(xdg) + "/sxmo";
|
|
|
|
|
const char* home = std::getenv("HOME");
|
|
|
|
|
return std::string(home && *home ? home : "/home/user") + "/.config/sxmo";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Kept for callers that re-sync after returning from xdrawer.
|
|
|
|
|
void loadSavedSpeakerVol() { refreshSystemVolume(true); }
|
|
|
|
|
|
|
|
|
|
// Re-read the system speaker volume. It follows the same rules as xdrawer:
|
|
|
|
|
// the percentage comes from speaker_vol (falling back to the WCD9302
|
|
|
|
|
// "RX3 Digital Volume" control when the file does not exist yet), and the
|
|
|
|
|
// speaker is muted when "SPK DAC Switch" is off or the volume is 0.
|
|
|
|
|
// Cheap enough to call from the mixer thread every 500 ms: it stats the
|
|
|
|
|
// file and only re-reads it when its mtime changes, and samples the two
|
|
|
|
|
// ALSA controls directly.
|
|
|
|
|
void refreshSystemVolume(bool force) {
|
|
|
|
|
std::lock_guard<std::mutex> lk(volMu_);
|
|
|
|
|
std::string path = sxmoConfigDir() + "/speaker_vol";
|
|
|
|
|
struct stat st {};
|
|
|
|
|
bool haveFile = (::stat(path.c_str(), &st) == 0);
|
|
|
|
|
int64_t mtimeNs = haveFile ? (static_cast<int64_t>(st.st_mtim.tv_sec) *
|
|
|
|
|
1000000000LL +
|
|
|
|
|
st.st_mtim.tv_nsec)
|
|
|
|
|
: -1;
|
|
|
|
|
if (force || mtimeNs != lastVolMtimeNs_) {
|
|
|
|
|
lastVolMtimeNs_ = mtimeNs;
|
|
|
|
|
filePct_ = -1;
|
|
|
|
|
if (haveFile) {
|
|
|
|
|
FILE* f = std::fopen(path.c_str(), "r");
|
|
|
|
|
if (f) {
|
|
|
|
|
int v = -1;
|
|
|
|
|
if (std::fscanf(f, "%d", &v) == 1 && v >= 0 && v <= 100) {
|
|
|
|
|
filePct_ = v;
|
|
|
|
|
}
|
|
|
|
|
std::fclose(f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool spkOn = true;
|
|
|
|
|
long rx3 = 92;
|
|
|
|
|
snd_ctl_t* ctl = nullptr;
|
|
|
|
|
if (snd_ctl_open(&ctl, "hw:0", 0) == 0 && ctl) {
|
|
|
|
|
spkOn = readCtlBool(ctl, "SPK DAC Switch", true);
|
|
|
|
|
rx3 = readCtlInt(ctl, "RX3 Digital Volume", 92);
|
|
|
|
|
snd_ctl_close(ctl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pct = filePct_;
|
|
|
|
|
if (pct < 0) {
|
|
|
|
|
// Same RX3 -> percentage mapping as xdrawer (60..92 => 0..100%)
|
|
|
|
|
if (rx3 <= 0) {
|
|
|
|
|
pct = 0;
|
|
|
|
|
} else if (rx3 >= 92) {
|
|
|
|
|
pct = 100;
|
|
|
|
|
} else {
|
|
|
|
|
pct = std::clamp(static_cast<int>(std::lround(((rx3 - 60) * 100.0) /
|
|
|
|
|
32.0)),
|
|
|
|
|
5, 100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
volumePct.store(pct, std::memory_order_relaxed);
|
2026-10-29 20:42:26 +02:00
|
|
|
bool muted = (readSimpleInt("/tmp/sxmo_out_muted", 0) == 1) || pct <= 0;
|
2026-10-28 15:25:07 +02:00
|
|
|
sysMuted_.store(muted, std::memory_order_relaxed);
|
|
|
|
|
float lin = pct / 100.0f;
|
2026-10-29 20:42:26 +02:00
|
|
|
sysGain_.store(muted ? 0.0f : lin, std::memory_order_relaxed);
|
2026-10-28 15:25:07 +02:00
|
|
|
}
|
|
|
|
|
|
2026-10-29 20:42:26 +02:00
|
|
|
// Change the system-wide output volume linearly via sxmo_audio.sh.
|
2026-10-28 15:25:07 +02:00
|
|
|
void setSystemVolume(int pct) {
|
|
|
|
|
pct = std::clamp(pct, 0, 100);
|
|
|
|
|
std::string dir = sxmoConfigDir();
|
|
|
|
|
::mkdir(dir.c_str(), 0755);
|
|
|
|
|
{
|
|
|
|
|
FILE* f = std::fopen((dir + "/speaker_vol").c_str(), "w");
|
|
|
|
|
if (f) {
|
|
|
|
|
std::fprintf(f, "%d\n", pct);
|
|
|
|
|
std::fclose(f);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-29 20:42:26 +02:00
|
|
|
char cmd[128];
|
2026-10-28 15:25:07 +02:00
|
|
|
std::snprintf(cmd, sizeof(cmd),
|
2026-10-29 20:42:26 +02:00
|
|
|
"sxmo_audio.sh vol set %d >/dev/null 2>&1 &", pct);
|
2026-10-28 15:25:07 +02:00
|
|
|
std::thread([c = std::string(cmd)]() { (void)std::system(c.c_str()); })
|
|
|
|
|
.detach();
|
|
|
|
|
refreshSystemVolume(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Backwards-compatible name used by the pause menu / volume keys.
|
|
|
|
|
void setHardwareVolume(int pct) { setSystemVolume(pct); }
|
|
|
|
|
|
|
|
|
|
static float perceptualGain(int levelPct) {
|
|
|
|
|
if (levelPct <= 0) return 0.0f;
|
|
|
|
|
if (levelPct >= 100) return 1.0f;
|
2026-10-29 20:42:26 +02:00
|
|
|
return std::clamp(levelPct, 0, 100) / 100.0f;
|
2026-10-28 15:25:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4096-entry sine table shared by the synth voices (avoids libm sin()
|
|
|
|
|
// calls in the per-sample hot loop on Cortex-A7).
|
|
|
|
|
static float fastSin01(float phase01) {
|
|
|
|
|
static const std::vector<float> kTable = []() {
|
|
|
|
|
std::vector<float> t(4097);
|
|
|
|
|
for (int i = 0; i <= 4096; ++i) {
|
|
|
|
|
t[i] = static_cast<float>(std::sin(i * 6.283185307179586 / 4096.0));
|
|
|
|
|
}
|
|
|
|
|
return t;
|
|
|
|
|
}();
|
|
|
|
|
float pos = phase01 * 4096.0f;
|
|
|
|
|
int idx = static_cast<int>(pos);
|
|
|
|
|
if (idx < 0) idx = 0;
|
|
|
|
|
if (idx > 4095) idx = 4095;
|
|
|
|
|
float frac = pos - static_cast<float>(idx);
|
|
|
|
|
return kTable[idx] + (kTable[idx + 1] - kTable[idx]) * frac;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Play a standalone MIDI note (0..127, 69 = A4 = 440Hz) for durationMs
|
2026-10-28 12:41:00 +02:00
|
|
|
void playTone(int note, int durationMs, int vol) {
|
|
|
|
|
if (!enabled || durationMs <= 0 || vol <= 0) return;
|
|
|
|
|
float freq = 440.0f * std::pow(2.0f, (note - 69) / 12.0f);
|
2026-10-28 15:25:07 +02:00
|
|
|
freq = std::clamp(freq, 45.0f, 8000.0f);
|
|
|
|
|
int samples =
|
|
|
|
|
std::clamp(static_cast<int>((durationMs * int64_t(kSampleRate)) / 1000),
|
|
|
|
|
240, static_cast<int>(kSampleRate * 10));
|
|
|
|
|
float amp = perceptualGain(vol) * 0.28f;
|
2026-10-28 12:41:00 +02:00
|
|
|
std::lock_guard<std::mutex> lk(mu_);
|
|
|
|
|
ActiveVoice v;
|
|
|
|
|
v.freq = freq;
|
|
|
|
|
v.phase = 0.0f;
|
|
|
|
|
v.remainingSamples = samples;
|
|
|
|
|
v.totalSamples = samples;
|
|
|
|
|
v.amp = amp;
|
2026-10-28 15:25:07 +02:00
|
|
|
if (voices_.size() < 12) {
|
2026-10-28 12:41:00 +02:00
|
|
|
voices_.push_back(v);
|
|
|
|
|
} else {
|
|
|
|
|
voices_[0] = v;
|
|
|
|
|
}
|
2026-10-28 15:25:07 +02:00
|
|
|
lastActiveMs_ = nowMs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Play or start a player buffer (WAV, Standard MIDI Format 0/1, or Tone sequence)
|
|
|
|
|
void startPlayerBytes(int playerId, const std::vector<uint8_t>& rawData,
|
|
|
|
|
bool loop, int playerVolPct = 80) {
|
|
|
|
|
if (rawData.empty()) {
|
|
|
|
|
playTone(69, 90, playerVolPct);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Check for Standard MIDI ("MThd")
|
|
|
|
|
if (rawData.size() >= 14 && std::memcmp(rawData.data(), "MThd", 4) == 0) {
|
|
|
|
|
startMidiBytes(playerId, rawData, loop, playerVolPct);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Check for RIFF WAVE ("RIFF" ... "WAVE")
|
|
|
|
|
if (rawData.size() >= 44 && std::memcmp(rawData.data(), "RIFF", 4) == 0 &&
|
|
|
|
|
std::memcmp(rawData.data() + 8, "WAVE", 4) == 0) {
|
|
|
|
|
startWavBytes(playerId, rawData, loop, playerVolPct);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Check for JSR-135 ToneControl sequence or Nokia Smart Messaging / OTA tone
|
|
|
|
|
if (startToneSequenceBytes(playerId, rawData, loop, playerVolPct)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Fallback brief pleasant chime
|
|
|
|
|
if (playerVolPct > 0) {
|
|
|
|
|
playTone(64, 90, playerVolPct);
|
|
|
|
|
playTone(71, 110, playerVolPct);
|
|
|
|
|
}
|
2026-10-28 12:41:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void playWavBytes(const std::vector<uint8_t>& wavData, bool loop = false) {
|
2026-10-28 15:25:07 +02:00
|
|
|
startPlayerBytes(0, wavData, loop, 85);
|
|
|
|
|
}
|
2026-10-28 12:41:00 +02:00
|
|
|
|
2026-10-28 15:25:07 +02:00
|
|
|
void setPlayerVolume(int playerId, int volPct) {
|
|
|
|
|
float g = perceptualGain(volPct);
|
2026-10-28 12:41:00 +02:00
|
|
|
std::lock_guard<std::mutex> lk(mu_);
|
2026-10-28 15:25:07 +02:00
|
|
|
playerGains_[playerId] = g;
|
|
|
|
|
if (midiSeq_.active && midiSeq_.playerId == playerId) {
|
|
|
|
|
midiSeq_.gain = g;
|
|
|
|
|
}
|
|
|
|
|
for (auto& s : pcmStreams_) {
|
|
|
|
|
if (s.playerId == playerId) {
|
|
|
|
|
s.gain = g;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void stopPlayer(int playerId) {
|
|
|
|
|
std::lock_guard<std::mutex> lk(mu_);
|
|
|
|
|
if (midiSeq_.active && midiSeq_.playerId == playerId) {
|
|
|
|
|
midiSeq_.active = false;
|
|
|
|
|
for (auto& mv : midiVoices_) {
|
|
|
|
|
if (mv.active && !mv.releasing) {
|
|
|
|
|
mv.releasing = true;
|
|
|
|
|
mv.releaseRemaining = 480;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (auto it = pcmStreams_.begin(); it != pcmStreams_.end();) {
|
|
|
|
|
if (it->playerId == playerId) {
|
|
|
|
|
it = pcmStreams_.erase(it);
|
|
|
|
|
} else {
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void rewindPlayer(int playerId) {
|
|
|
|
|
std::lock_guard<std::mutex> lk(mu_);
|
|
|
|
|
if (midiSeq_.playerId == playerId) {
|
|
|
|
|
midiSeq_.currentFrame = 0;
|
|
|
|
|
midiSeq_.eventIdx = 0;
|
|
|
|
|
}
|
|
|
|
|
for (auto& s : pcmStreams_) {
|
|
|
|
|
if (s.playerId == playerId) {
|
|
|
|
|
s.pos = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isPlayerActive(int playerId) {
|
|
|
|
|
std::lock_guard<std::mutex> lk(mu_);
|
|
|
|
|
if (midiSeq_.active && midiSeq_.playerId == playerId) return true;
|
|
|
|
|
for (const auto& s : pcmStreams_) {
|
|
|
|
|
if (s.playerId == playerId && s.pos < s.samples.size()) return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2026-10-28 12:41:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void stopAll() {
|
|
|
|
|
std::lock_guard<std::mutex> lk(mu_);
|
|
|
|
|
voices_.clear();
|
2026-10-28 15:25:07 +02:00
|
|
|
pcmStreams_.clear();
|
|
|
|
|
midiSeq_.active = false;
|
|
|
|
|
midiVoices_.clear();
|
2026-10-28 12:41:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
struct ActiveVoice {
|
|
|
|
|
float freq = 440.0f;
|
|
|
|
|
float phase = 0.0f;
|
|
|
|
|
int remainingSamples = 0;
|
|
|
|
|
int totalSamples = 1;
|
|
|
|
|
float amp = 0.2f;
|
|
|
|
|
};
|
|
|
|
|
|
2026-10-28 15:25:07 +02:00
|
|
|
struct ActivePcmStream {
|
|
|
|
|
int playerId = 0;
|
|
|
|
|
std::vector<int16_t> samples;
|
|
|
|
|
size_t pos = 0;
|
|
|
|
|
bool loop = false;
|
|
|
|
|
float gain = 0.8f;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct MidiEvent {
|
|
|
|
|
uint64_t frame = 0;
|
|
|
|
|
uint8_t type = 0; // 0x80 NoteOff, 0x90 NoteOn, 0xB0 CC, 0xC0 Prog, 0xE0 Bend
|
|
|
|
|
uint8_t channel = 0;
|
|
|
|
|
uint8_t data1 = 0;
|
|
|
|
|
uint8_t data2 = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct MidiSequence {
|
|
|
|
|
int playerId = -1;
|
|
|
|
|
bool active = false;
|
|
|
|
|
bool loop = false;
|
|
|
|
|
float gain = 0.8f;
|
|
|
|
|
std::vector<MidiEvent> events;
|
|
|
|
|
size_t eventIdx = 0;
|
|
|
|
|
uint64_t currentFrame = 0;
|
|
|
|
|
uint64_t totalFrames = 0;
|
|
|
|
|
uint8_t chProgram[16] = {0};
|
|
|
|
|
uint8_t chVolume[16] = {100, 100, 100, 100, 100, 100, 100, 100,
|
|
|
|
|
100, 100, 100, 100, 100, 100, 100, 100};
|
|
|
|
|
uint8_t chExpr[16] = {127, 127, 127, 127, 127, 127, 127, 127,
|
|
|
|
|
127, 127, 127, 127, 127, 127, 127, 127};
|
|
|
|
|
float chPitchBend[16] = {0.0f};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct MidiVoice {
|
|
|
|
|
bool active = false;
|
|
|
|
|
bool isDrum = false;
|
|
|
|
|
uint8_t channel = 0;
|
|
|
|
|
uint8_t note = 60;
|
|
|
|
|
uint8_t program = 0;
|
|
|
|
|
float baseFreq = 440.0f;
|
|
|
|
|
float phase = 0.0f;
|
|
|
|
|
float subPhase = 0.0f;
|
|
|
|
|
float velocityGain = 0.5f;
|
|
|
|
|
float env = 0.0f;
|
|
|
|
|
float sustainLevel = 0.45f;
|
|
|
|
|
float decayRate = 0.00004f;
|
|
|
|
|
bool releasing = false;
|
|
|
|
|
int releaseRemaining = 0;
|
|
|
|
|
int ageSamples = 0;
|
|
|
|
|
int maxSamples = 48000 * 6;
|
|
|
|
|
};
|
|
|
|
|
|
2026-10-28 12:41:00 +02:00
|
|
|
std::atomic<bool> running_{false};
|
2026-10-28 15:25:07 +02:00
|
|
|
std::atomic<bool> paused_{false};
|
2026-10-28 12:41:00 +02:00
|
|
|
std::thread worker_;
|
|
|
|
|
std::mutex mu_;
|
2026-10-28 15:25:07 +02:00
|
|
|
|
|
|
|
|
// System speaker volume tracking (see refreshSystemVolume)
|
|
|
|
|
std::mutex volMu_;
|
|
|
|
|
int64_t lastVolMtimeNs_ = -2;
|
|
|
|
|
int filePct_ = -1;
|
|
|
|
|
std::atomic<bool> sysMuted_{false};
|
|
|
|
|
std::atomic<float> sysGain_{1.0f};
|
|
|
|
|
int64_t lastVolPollMs_ = 0;
|
|
|
|
|
|
|
|
|
|
static snd_ctl_elem_value_t* prepCtlValue(snd_ctl_elem_value_t* val,
|
|
|
|
|
snd_ctl_elem_id_t* id,
|
|
|
|
|
const char* name) {
|
|
|
|
|
snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
|
|
|
|
|
snd_ctl_elem_id_set_name(id, name);
|
|
|
|
|
snd_ctl_elem_value_set_id(val, id);
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static long readCtlInt(snd_ctl_t* ctl, const char* name, long fallback) {
|
|
|
|
|
snd_ctl_elem_id_t* id = nullptr;
|
|
|
|
|
snd_ctl_elem_value_t* val = nullptr;
|
|
|
|
|
snd_ctl_elem_id_alloca(&id);
|
|
|
|
|
snd_ctl_elem_value_alloca(&val);
|
|
|
|
|
prepCtlValue(val, id, name);
|
|
|
|
|
if (snd_ctl_elem_read(ctl, val) < 0) return fallback;
|
|
|
|
|
return snd_ctl_elem_value_get_integer(val, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool readCtlBool(snd_ctl_t* ctl, const char* name, bool fallback) {
|
|
|
|
|
snd_ctl_elem_id_t* id = nullptr;
|
|
|
|
|
snd_ctl_elem_value_t* val = nullptr;
|
|
|
|
|
snd_ctl_elem_id_alloca(&id);
|
|
|
|
|
snd_ctl_elem_value_alloca(&val);
|
|
|
|
|
prepCtlValue(val, id, name);
|
|
|
|
|
if (snd_ctl_elem_read(ctl, val) < 0) return fallback;
|
|
|
|
|
return snd_ctl_elem_value_get_boolean(val, 0) != 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void writeCtlInt(snd_ctl_t* ctl, const char* name, long v) {
|
|
|
|
|
snd_ctl_elem_id_t* id = nullptr;
|
|
|
|
|
snd_ctl_elem_value_t* val = nullptr;
|
|
|
|
|
snd_ctl_elem_id_alloca(&id);
|
|
|
|
|
snd_ctl_elem_value_alloca(&val);
|
|
|
|
|
prepCtlValue(val, id, name);
|
|
|
|
|
snd_ctl_elem_value_set_integer(val, 0, v);
|
|
|
|
|
(void)snd_ctl_elem_write(ctl, val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void writeCtlBool(snd_ctl_t* ctl, const char* name, bool v) {
|
|
|
|
|
snd_ctl_elem_id_t* id = nullptr;
|
|
|
|
|
snd_ctl_elem_value_t* val = nullptr;
|
|
|
|
|
snd_ctl_elem_id_alloca(&id);
|
|
|
|
|
snd_ctl_elem_value_alloca(&val);
|
|
|
|
|
prepCtlValue(val, id, name);
|
|
|
|
|
snd_ctl_elem_value_set_boolean(val, 0, v ? 1 : 0);
|
|
|
|
|
(void)snd_ctl_elem_write(ctl, val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void writeCtlEnum(snd_ctl_t* ctl, const char* name, unsigned int v) {
|
|
|
|
|
snd_ctl_elem_id_t* id = nullptr;
|
|
|
|
|
snd_ctl_elem_value_t* val = nullptr;
|
|
|
|
|
snd_ctl_elem_id_alloca(&id);
|
|
|
|
|
snd_ctl_elem_value_alloca(&val);
|
|
|
|
|
prepCtlValue(val, id, name);
|
|
|
|
|
snd_ctl_elem_value_set_enumerated(val, 0, v);
|
|
|
|
|
(void)snd_ctl_elem_write(ctl, val);
|
|
|
|
|
}
|
|
|
|
|
std::unordered_map<int, float> playerGains_;
|
2026-10-28 12:41:00 +02:00
|
|
|
std::vector<ActiveVoice> voices_;
|
2026-10-28 15:25:07 +02:00
|
|
|
std::vector<ActivePcmStream> pcmStreams_;
|
|
|
|
|
MidiSequence midiSeq_;
|
|
|
|
|
std::vector<MidiVoice> midiVoices_;
|
|
|
|
|
uint32_t noiseLfsr_ = 0xACE1u;
|
|
|
|
|
int64_t lastActiveMs_ = 0;
|
|
|
|
|
float lpStage1L_ = 0.0f;
|
|
|
|
|
float lpStage2L_ = 0.0f;
|
|
|
|
|
int16_t lastSample_ = 0;
|
|
|
|
|
|
|
|
|
|
static uint16_t readBe16(const uint8_t* p) {
|
|
|
|
|
return static_cast<uint16_t>((static_cast<uint16_t>(p[0]) << 8) | p[1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint32_t readBe32(const uint8_t* p) {
|
|
|
|
|
return (static_cast<uint32_t>(p[0]) << 24) |
|
|
|
|
|
(static_cast<uint32_t>(p[1]) << 16) |
|
|
|
|
|
(static_cast<uint32_t>(p[2]) << 8) |
|
|
|
|
|
static_cast<uint32_t>(p[3]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint32_t readMidiVarLen(const uint8_t* data, size_t len, size_t& pos) {
|
|
|
|
|
uint32_t val = 0;
|
|
|
|
|
for (int i = 0; i < 4 && pos < len; ++i) {
|
|
|
|
|
uint8_t b = data[pos++];
|
|
|
|
|
val = (val << 7) | (b & 0x7Fu);
|
|
|
|
|
if ((b & 0x80u) == 0) break;
|
|
|
|
|
}
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void startWavBytes(int playerId, const std::vector<uint8_t>& wavData,
|
|
|
|
|
bool loop, int playerVolPct) {
|
|
|
|
|
uint16_t audioFmt = readLe16(wavData.data() + 20);
|
|
|
|
|
uint16_t channels = readLe16(wavData.data() + 22);
|
|
|
|
|
uint32_t sampleRate = readLe32(wavData.data() + 24);
|
|
|
|
|
uint16_t blockAlign = readLe16(wavData.data() + 32);
|
|
|
|
|
uint16_t bitsPerSample = readLe16(wavData.data() + 34);
|
|
|
|
|
if (channels == 0 || sampleRate == 0) return;
|
|
|
|
|
|
|
|
|
|
size_t pos = 12;
|
|
|
|
|
const uint8_t* pcmPtr = nullptr;
|
|
|
|
|
size_t pcmBytes = 0;
|
|
|
|
|
while (pos + 8 <= wavData.size()) {
|
|
|
|
|
uint32_t chunkSz = readLe32(wavData.data() + pos + 4);
|
|
|
|
|
if (std::memcmp(wavData.data() + pos, "fmt ", 4) == 0 &&
|
|
|
|
|
pos + 8 + 16 <= wavData.size()) {
|
|
|
|
|
audioFmt = readLe16(wavData.data() + pos + 8);
|
|
|
|
|
channels = readLe16(wavData.data() + pos + 10);
|
|
|
|
|
sampleRate = readLe32(wavData.data() + pos + 12);
|
|
|
|
|
blockAlign = readLe16(wavData.data() + pos + 20);
|
|
|
|
|
bitsPerSample = readLe16(wavData.data() + pos + 22);
|
|
|
|
|
} else if (std::memcmp(wavData.data() + pos, "data", 4) == 0) {
|
|
|
|
|
pcmPtr = wavData.data() + pos + 8;
|
|
|
|
|
pcmBytes = std::min<size_t>(chunkSz, wavData.size() - (pos + 8));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
pos += 8 + chunkSz + (chunkSz & 1u);
|
|
|
|
|
}
|
|
|
|
|
if (!pcmPtr || pcmBytes == 0 || channels == 0 || sampleRate == 0) return;
|
|
|
|
|
|
|
|
|
|
std::vector<int16_t> srcMono;
|
|
|
|
|
if (audioFmt == 1 && (bitsPerSample == 8 || bitsPerSample == 16)) {
|
|
|
|
|
size_t frameSize = channels * (bitsPerSample / 8);
|
|
|
|
|
if (frameSize == 0) return;
|
|
|
|
|
size_t totalFrames = pcmBytes / frameSize;
|
|
|
|
|
srcMono.resize(totalFrames);
|
|
|
|
|
for (size_t i = 0; i < totalFrames; ++i) {
|
|
|
|
|
const uint8_t* fptr = pcmPtr + i * frameSize;
|
|
|
|
|
int32_t sum = 0;
|
|
|
|
|
for (uint16_t ch = 0; ch < channels; ++ch) {
|
|
|
|
|
if (bitsPerSample == 16) {
|
|
|
|
|
sum += static_cast<int16_t>(readLe16(fptr + ch * 2));
|
|
|
|
|
} else {
|
|
|
|
|
sum += (static_cast<int>(fptr[ch]) - 128) << 8;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
srcMono[i] = static_cast<int16_t>(sum / channels);
|
|
|
|
|
}
|
|
|
|
|
} else if (audioFmt == 17 && blockAlign > 0) {
|
|
|
|
|
// IMA ADPCM mono/stereo fallback decoding
|
|
|
|
|
static const int kIndexTable[16] = {-1, -1, -1, -1, 2, 4, 6, 8,
|
|
|
|
|
-1, -1, -1, -1, 2, 4, 6, 8};
|
|
|
|
|
static const int kStepTable[89] = {
|
|
|
|
|
7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
|
|
|
|
|
19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
|
|
|
|
|
50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
|
|
|
|
|
130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
|
|
|
|
|
337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
|
|
|
|
|
876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
|
|
|
|
|
2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
|
|
|
|
|
5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
|
|
|
|
|
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767};
|
|
|
|
|
size_t offset = 0;
|
|
|
|
|
while (offset + 4 <= pcmBytes) {
|
|
|
|
|
size_t blkLen = std::min<size_t>(blockAlign, pcmBytes - offset);
|
|
|
|
|
const uint8_t* blk = pcmPtr + offset;
|
|
|
|
|
int predictor = static_cast<int16_t>(readLe16(blk));
|
|
|
|
|
int stepIdx = std::clamp<int>(blk[2], 0, 88);
|
|
|
|
|
srcMono.push_back(static_cast<int16_t>(predictor));
|
|
|
|
|
for (size_t b = 4 * channels; b < blkLen; ++b) {
|
|
|
|
|
uint8_t byteVal = blk[b];
|
|
|
|
|
for (int nibShift = 0; nibShift <= 4; nibShift += 4) {
|
|
|
|
|
int nib = (byteVal >> nibShift) & 0x0F;
|
|
|
|
|
int step = kStepTable[stepIdx];
|
|
|
|
|
int diff = step >> 3;
|
|
|
|
|
if (nib & 4) diff += step;
|
|
|
|
|
if (nib & 2) diff += step >> 1;
|
|
|
|
|
if (nib & 1) diff += step >> 2;
|
|
|
|
|
if (nib & 8)
|
|
|
|
|
predictor -= diff;
|
|
|
|
|
else
|
|
|
|
|
predictor += diff;
|
|
|
|
|
predictor = std::clamp(predictor, -32768, 32767);
|
|
|
|
|
stepIdx = std::clamp(stepIdx + kIndexTable[nib], 0, 88);
|
|
|
|
|
srcMono.push_back(static_cast<int16_t>(predictor));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
offset += blkLen;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (srcMono.empty()) return;
|
|
|
|
|
|
|
|
|
|
// Gentle pre-smoothing on 8-bit low-rate WAVs to remove quantization harshness
|
|
|
|
|
if (bitsPerSample == 8 && sampleRate <= 16000 && srcMono.size() >= 4) {
|
|
|
|
|
int32_t prev = srcMono[0];
|
|
|
|
|
for (size_t i = 0; i < srcMono.size(); ++i) {
|
|
|
|
|
int32_t cur = srcMono[i];
|
|
|
|
|
prev = (prev * 3 + cur * 5) >> 3;
|
|
|
|
|
srcMono[i] = static_cast<int16_t>(prev);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t totalFrames = srcMono.size();
|
|
|
|
|
size_t outFrames =
|
|
|
|
|
(totalFrames * static_cast<uint64_t>(kSampleRate)) / sampleRate;
|
|
|
|
|
outFrames = std::clamp<size_t>(outFrames, 1, kSampleRate * 30);
|
|
|
|
|
std::vector<int16_t> resampled(outFrames);
|
|
|
|
|
double step = static_cast<double>(sampleRate) / static_cast<double>(kSampleRate);
|
|
|
|
|
for (size_t i = 0; i < outFrames; ++i) {
|
|
|
|
|
double srcPos = i * step;
|
|
|
|
|
size_t idx0 = static_cast<size_t>(srcPos);
|
|
|
|
|
if (idx0 >= totalFrames) idx0 = totalFrames - 1;
|
|
|
|
|
size_t idx1 = std::min(idx0 + 1, totalFrames - 1);
|
|
|
|
|
double frac = srcPos - static_cast<double>(idx0);
|
|
|
|
|
double interp =
|
|
|
|
|
srcMono[idx0] * (1.0 - frac) + srcMono[idx1] * frac;
|
|
|
|
|
// Short fade-in/fade-out (2 ms = 96 samples) to prevent edge clicks
|
|
|
|
|
double env = 1.0;
|
|
|
|
|
if (i < 96) env = static_cast<double>(i) / 96.0;
|
|
|
|
|
if (outFrames - i < 96) env = std::min(env, static_cast<double>(outFrames - i) / 96.0);
|
|
|
|
|
resampled[i] = static_cast<int16_t>(
|
|
|
|
|
std::clamp(interp * env, -32768.0, 32767.0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lk(mu_);
|
|
|
|
|
float g = perceptualGain(playerVolPct);
|
|
|
|
|
auto it = playerGains_.find(playerId);
|
|
|
|
|
if (it != playerGains_.end()) g = it->second;
|
|
|
|
|
|
|
|
|
|
for (auto& s : pcmStreams_) {
|
|
|
|
|
if (s.playerId == playerId) {
|
|
|
|
|
s.samples = std::move(resampled);
|
|
|
|
|
s.pos = 0;
|
|
|
|
|
s.loop = loop;
|
|
|
|
|
s.gain = g;
|
|
|
|
|
lastActiveMs_ = nowMs();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ActivePcmStream stream;
|
|
|
|
|
stream.playerId = playerId;
|
|
|
|
|
stream.samples = std::move(resampled);
|
|
|
|
|
stream.pos = 0;
|
|
|
|
|
stream.loop = loop;
|
|
|
|
|
stream.gain = g;
|
|
|
|
|
if (pcmStreams_.size() >= 6) {
|
|
|
|
|
pcmStreams_.erase(pcmStreams_.begin());
|
|
|
|
|
}
|
|
|
|
|
pcmStreams_.push_back(std::move(stream));
|
|
|
|
|
lastActiveMs_ = nowMs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void startMidiBytes(int playerId, const std::vector<uint8_t>& mid, bool loop,
|
|
|
|
|
int playerVolPct) {
|
|
|
|
|
if (mid.size() < 14 || std::memcmp(mid.data(), "MThd", 4) != 0) return;
|
|
|
|
|
uint32_t hdrLen = readBe32(mid.data() + 4);
|
|
|
|
|
uint16_t numTracks = readBe16(mid.data() + 10);
|
|
|
|
|
uint16_t division = readBe16(mid.data() + 12);
|
|
|
|
|
if (division == 0) division = 480;
|
|
|
|
|
|
|
|
|
|
struct RawTickEvent {
|
|
|
|
|
uint64_t tick = 0;
|
|
|
|
|
uint32_t seqOrder = 0;
|
|
|
|
|
uint8_t type = 0; // 0xFF = tempo (tempoUs), else MIDI status high nibble
|
|
|
|
|
uint8_t channel = 0;
|
|
|
|
|
uint8_t data1 = 0;
|
|
|
|
|
uint8_t data2 = 0;
|
|
|
|
|
uint32_t tempoUs = 500000;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::vector<RawTickEvent> rawEvents;
|
|
|
|
|
rawEvents.reserve(2048);
|
|
|
|
|
uint32_t orderCounter = 0;
|
|
|
|
|
|
|
|
|
|
size_t pos = 8 + hdrLen;
|
|
|
|
|
for (uint16_t trk = 0; trk < numTracks && pos + 8 <= mid.size(); ++trk) {
|
|
|
|
|
if (std::memcmp(mid.data() + pos, "MTrk", 4) != 0) {
|
|
|
|
|
uint32_t skipSz = readBe32(mid.data() + pos + 4);
|
|
|
|
|
pos += 8 + skipSz;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
uint32_t trkLen = readBe32(mid.data() + pos + 4);
|
|
|
|
|
size_t trkEnd = std::min(mid.size(), pos + 8 + trkLen);
|
|
|
|
|
size_t tpos = pos + 8;
|
|
|
|
|
uint64_t absTick = 0;
|
|
|
|
|
uint8_t runningStatus = 0;
|
|
|
|
|
|
|
|
|
|
while (tpos < trkEnd) {
|
|
|
|
|
uint32_t delta = readMidiVarLen(mid.data(), trkEnd, tpos);
|
|
|
|
|
absTick += delta;
|
|
|
|
|
if (tpos >= trkEnd) break;
|
|
|
|
|
|
|
|
|
|
uint8_t status = mid[tpos];
|
|
|
|
|
if (status < 0x80) {
|
|
|
|
|
status = runningStatus;
|
|
|
|
|
} else {
|
|
|
|
|
tpos++;
|
|
|
|
|
if (status < 0xF0) runningStatus = status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status == 0xFF) {
|
|
|
|
|
if (tpos >= trkEnd) break;
|
|
|
|
|
uint8_t metaType = mid[tpos++];
|
|
|
|
|
uint32_t metaLen = readMidiVarLen(mid.data(), trkEnd, tpos);
|
|
|
|
|
if (metaType == 0x51 && metaLen == 3 && tpos + 3 <= trkEnd) {
|
|
|
|
|
uint32_t usPerQn = (static_cast<uint32_t>(mid[tpos]) << 16) |
|
|
|
|
|
(static_cast<uint32_t>(mid[tpos + 1]) << 8) |
|
|
|
|
|
static_cast<uint32_t>(mid[tpos + 2]);
|
|
|
|
|
if (usPerQn > 0) {
|
|
|
|
|
RawTickEvent ev;
|
|
|
|
|
ev.tick = absTick;
|
|
|
|
|
ev.seqOrder = orderCounter++;
|
|
|
|
|
ev.type = 0xFF;
|
|
|
|
|
ev.tempoUs = usPerQn;
|
|
|
|
|
rawEvents.push_back(ev);
|
|
|
|
|
}
|
|
|
|
|
} else if (metaType == 0x2F) {
|
|
|
|
|
tpos += metaLen;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
tpos = std::min(trkEnd, tpos + metaLen);
|
|
|
|
|
} else if (status == 0xF0 || status == 0xF7) {
|
|
|
|
|
uint32_t sysexLen = readMidiVarLen(mid.data(), trkEnd, tpos);
|
|
|
|
|
tpos = std::min(trkEnd, tpos + sysexLen);
|
|
|
|
|
} else {
|
|
|
|
|
uint8_t hi = status & 0xF0u;
|
|
|
|
|
uint8_t ch = status & 0x0Fu;
|
|
|
|
|
if (hi == 0x80 || hi == 0x90 || hi == 0xA0 || hi == 0xB0 ||
|
|
|
|
|
hi == 0xE0) {
|
|
|
|
|
if (tpos + 2 > trkEnd) break;
|
|
|
|
|
uint8_t d1 = mid[tpos++] & 0x7Fu;
|
|
|
|
|
uint8_t d2 = mid[tpos++] & 0x7Fu;
|
|
|
|
|
RawTickEvent ev;
|
|
|
|
|
ev.tick = absTick;
|
|
|
|
|
ev.seqOrder = orderCounter++;
|
|
|
|
|
ev.type = (hi == 0x90 && d2 == 0) ? 0x80 : hi;
|
|
|
|
|
ev.channel = ch;
|
|
|
|
|
ev.data1 = d1;
|
|
|
|
|
ev.data2 = d2;
|
|
|
|
|
rawEvents.push_back(ev);
|
|
|
|
|
} else if (hi == 0xC0 || hi == 0xD0) {
|
|
|
|
|
if (tpos + 1 > trkEnd) break;
|
|
|
|
|
uint8_t d1 = mid[tpos++] & 0x7Fu;
|
|
|
|
|
RawTickEvent ev;
|
|
|
|
|
ev.tick = absTick;
|
|
|
|
|
ev.seqOrder = orderCounter++;
|
|
|
|
|
ev.type = hi;
|
|
|
|
|
ev.channel = ch;
|
|
|
|
|
ev.data1 = d1;
|
|
|
|
|
ev.data2 = 0;
|
|
|
|
|
rawEvents.push_back(ev);
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pos = trkEnd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rawEvents.empty()) return;
|
|
|
|
|
|
|
|
|
|
std::sort(rawEvents.begin(), rawEvents.end(),
|
|
|
|
|
[](const RawTickEvent& a, const RawTickEvent& b) {
|
|
|
|
|
if (a.tick != b.tick) return a.tick < b.tick;
|
|
|
|
|
return a.seqOrder < b.seqOrder;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Convert ticks to 48000 Hz audio frames using tempo map
|
|
|
|
|
std::vector<MidiEvent> frameEvents;
|
|
|
|
|
frameEvents.reserve(rawEvents.size());
|
|
|
|
|
double currentFrameD = 0.0;
|
|
|
|
|
uint64_t prevTick = 0;
|
|
|
|
|
double usPerTick = 500000.0 / static_cast<double>(division);
|
|
|
|
|
if (division & 0x8000u) {
|
|
|
|
|
int fps = -static_cast<int8_t>((division >> 8) & 0xFF);
|
|
|
|
|
int tpf = division & 0xFF;
|
|
|
|
|
if (fps > 0 && tpf > 0) usPerTick = 1000000.0 / (fps * tpf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const auto& rev : rawEvents) {
|
|
|
|
|
uint64_t dt = rev.tick - prevTick;
|
|
|
|
|
currentFrameD +=
|
|
|
|
|
(static_cast<double>(dt) * usPerTick * kSampleRate) / 1000000.0;
|
|
|
|
|
prevTick = rev.tick;
|
|
|
|
|
|
|
|
|
|
if (rev.type == 0xFF) {
|
|
|
|
|
if ((division & 0x8000u) == 0) {
|
|
|
|
|
usPerTick =
|
|
|
|
|
static_cast<double>(rev.tempoUs) / static_cast<double>(division);
|
|
|
|
|
}
|
|
|
|
|
} else if (rev.type == 0x80 || rev.type == 0x90 || rev.type == 0xB0 ||
|
|
|
|
|
rev.type == 0xC0 || rev.type == 0xE0) {
|
|
|
|
|
MidiEvent mev;
|
|
|
|
|
mev.frame = static_cast<uint64_t>(currentFrameD);
|
|
|
|
|
mev.type = rev.type;
|
|
|
|
|
mev.channel = rev.channel;
|
|
|
|
|
mev.data1 = rev.data1;
|
|
|
|
|
mev.data2 = rev.data2;
|
|
|
|
|
frameEvents.push_back(mev);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (frameEvents.empty()) return;
|
|
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lk(mu_);
|
|
|
|
|
float g = perceptualGain(playerVolPct);
|
|
|
|
|
auto it = playerGains_.find(playerId);
|
|
|
|
|
if (it != playerGains_.end()) g = it->second;
|
|
|
|
|
|
|
|
|
|
midiSeq_ = MidiSequence{};
|
|
|
|
|
midiSeq_.playerId = playerId;
|
|
|
|
|
midiSeq_.active = true;
|
|
|
|
|
midiSeq_.loop = loop;
|
|
|
|
|
midiSeq_.gain = g;
|
|
|
|
|
midiSeq_.events = std::move(frameEvents);
|
|
|
|
|
midiSeq_.totalFrames =
|
|
|
|
|
midiSeq_.events.back().frame + (kSampleRate / 5); // +200ms tail
|
|
|
|
|
midiVoices_.clear();
|
|
|
|
|
lastActiveMs_ = nowMs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool startToneSequenceBytes(int playerId, const std::vector<uint8_t>& seq,
|
|
|
|
|
bool loop, int playerVolPct) {
|
|
|
|
|
if (seq.size() < 4) return false;
|
|
|
|
|
// JSR-135 ToneControl sequence starts with VERSION (-2 = 0xFE, 1)
|
|
|
|
|
std::vector<MidiEvent> evs;
|
|
|
|
|
uint64_t curFrame = 0;
|
|
|
|
|
int tempoBpm = 120;
|
|
|
|
|
int resolution = 64;
|
|
|
|
|
|
|
|
|
|
if (static_cast<int8_t>(seq[0]) == -2) {
|
|
|
|
|
size_t i = 2;
|
|
|
|
|
while (i + 1 < seq.size()) {
|
|
|
|
|
int8_t cmd = static_cast<int8_t>(seq[i]);
|
|
|
|
|
uint8_t param = seq[i + 1];
|
|
|
|
|
i += 2;
|
|
|
|
|
if (cmd == -3) { // TEMPO (BPM = param * 4)
|
|
|
|
|
tempoBpm = std::max<int>(20, param * 4);
|
|
|
|
|
} else if (cmd == -4) { // RESOLUTION
|
|
|
|
|
resolution = std::max<int>(1, param);
|
|
|
|
|
} else if (cmd >= -1 && param > 0) {
|
|
|
|
|
uint64_t durFrames =
|
|
|
|
|
(static_cast<uint64_t>(param) * 240ULL * kSampleRate) /
|
|
|
|
|
(static_cast<uint64_t>(tempoBpm) * resolution);
|
|
|
|
|
durFrames = std::clamp<uint64_t>(durFrames, 480, kSampleRate * 4);
|
|
|
|
|
if (cmd >= 0) {
|
|
|
|
|
MidiEvent on{curFrame, 0x90, 0, static_cast<uint8_t>(cmd), 100};
|
|
|
|
|
MidiEvent off{curFrame + (durFrames * 9) / 10, 0x80, 0,
|
|
|
|
|
static_cast<uint8_t>(cmd), 0};
|
|
|
|
|
evs.push_back(on);
|
|
|
|
|
evs.push_back(off);
|
|
|
|
|
}
|
|
|
|
|
curFrame += durFrames;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (evs.empty()) return false;
|
|
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lk(mu_);
|
|
|
|
|
float g = perceptualGain(playerVolPct);
|
|
|
|
|
auto it = playerGains_.find(playerId);
|
|
|
|
|
if (it != playerGains_.end()) g = it->second;
|
|
|
|
|
|
|
|
|
|
midiSeq_ = MidiSequence{};
|
|
|
|
|
midiSeq_.playerId = playerId;
|
|
|
|
|
midiSeq_.active = true;
|
|
|
|
|
midiSeq_.loop = loop;
|
|
|
|
|
midiSeq_.gain = g;
|
|
|
|
|
midiSeq_.events = std::move(evs);
|
|
|
|
|
midiSeq_.totalFrames = curFrame + (kSampleRate / 10);
|
|
|
|
|
midiVoices_.clear();
|
|
|
|
|
lastActiveMs_ = nowMs();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void triggerMidiEventLocked(const MidiEvent& ev) {
|
|
|
|
|
uint8_t ch = ev.channel & 0x0Fu;
|
|
|
|
|
if (ev.type == 0x80) {
|
|
|
|
|
for (auto& mv : midiVoices_) {
|
|
|
|
|
if (mv.active && mv.channel == ch && mv.note == ev.data1 &&
|
|
|
|
|
!mv.releasing) {
|
|
|
|
|
mv.releasing = true;
|
|
|
|
|
mv.releaseRemaining = 960; // 20ms smooth release
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (ev.type == 0x90) {
|
|
|
|
|
// Retrigger existing voice on same channel + note if present
|
|
|
|
|
for (auto& mv : midiVoices_) {
|
|
|
|
|
if (mv.active && mv.channel == ch && mv.note == ev.data1) {
|
|
|
|
|
initMidiVoice(mv, ch, ev.data1, ev.data2);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (auto& mv : midiVoices_) {
|
|
|
|
|
if (!mv.active) {
|
|
|
|
|
initMidiVoice(mv, ch, ev.data1, ev.data2);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (midiVoices_.size() < 16) {
|
|
|
|
|
MidiVoice mv;
|
|
|
|
|
initMidiVoice(mv, ch, ev.data1, ev.data2);
|
|
|
|
|
midiVoices_.push_back(mv);
|
|
|
|
|
} else {
|
|
|
|
|
// Steal oldest releasing or oldest voice
|
|
|
|
|
size_t bestIdx = 0;
|
|
|
|
|
int bestScore = -1;
|
|
|
|
|
for (size_t i = 0; i < midiVoices_.size(); ++i) {
|
|
|
|
|
int score = midiVoices_[i].ageSamples +
|
|
|
|
|
(midiVoices_[i].releasing ? 200000 : 0);
|
|
|
|
|
if (score > bestScore) {
|
|
|
|
|
bestScore = score;
|
|
|
|
|
bestIdx = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
initMidiVoice(midiVoices_[bestIdx], ch, ev.data1, ev.data2);
|
|
|
|
|
}
|
|
|
|
|
} else if (ev.type == 0xB0) {
|
|
|
|
|
if (ev.data1 == 7) {
|
|
|
|
|
midiSeq_.chVolume[ch] = ev.data2;
|
|
|
|
|
} else if (ev.data1 == 11) {
|
|
|
|
|
midiSeq_.chExpr[ch] = ev.data2;
|
|
|
|
|
} else if (ev.data1 == 120 || ev.data1 == 123) {
|
|
|
|
|
for (auto& mv : midiVoices_) {
|
|
|
|
|
if (mv.active && mv.channel == ch) {
|
|
|
|
|
mv.releasing = true;
|
|
|
|
|
mv.releaseRemaining = 480;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (ev.type == 0xC0) {
|
|
|
|
|
midiSeq_.chProgram[ch] = ev.data1;
|
|
|
|
|
} else if (ev.type == 0xE0) {
|
|
|
|
|
int bend = (static_cast<int>(ev.data2) << 7) | ev.data1;
|
|
|
|
|
midiSeq_.chPitchBend[ch] = (bend - 8192) / 4096.0f; // +/- 2 semitones
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void initMidiVoice(MidiVoice& mv, uint8_t ch, uint8_t note, uint8_t vel) {
|
|
|
|
|
mv = MidiVoice{};
|
|
|
|
|
mv.active = true;
|
|
|
|
|
mv.channel = ch;
|
|
|
|
|
mv.note = note;
|
|
|
|
|
mv.program = midiSeq_.chProgram[ch];
|
|
|
|
|
mv.isDrum = (ch == 9);
|
|
|
|
|
mv.baseFreq =
|
|
|
|
|
440.0f *
|
|
|
|
|
std::pow(2.0f, (note - 69 + midiSeq_.chPitchBend[ch]) / 12.0f);
|
|
|
|
|
mv.baseFreq = std::clamp(mv.baseFreq, 32.0f, 9000.0f);
|
|
|
|
|
mv.velocityGain = std::pow(std::clamp<int>(vel, 1, 127) / 127.0f, 0.7f);
|
|
|
|
|
mv.env = 0.05f;
|
|
|
|
|
mv.releasing = false;
|
|
|
|
|
mv.releaseRemaining = 960;
|
|
|
|
|
mv.ageSamples = 0;
|
|
|
|
|
|
|
|
|
|
if (mv.isDrum) {
|
|
|
|
|
mv.sustainLevel = 0.0f;
|
|
|
|
|
if (note == 35 || note == 36) {
|
|
|
|
|
mv.baseFreq = 135.0f; // Kick drum
|
|
|
|
|
mv.decayRate = 0.00032f;
|
|
|
|
|
mv.maxSamples = kSampleRate / 3;
|
|
|
|
|
} else if (note == 38 || note == 40) {
|
|
|
|
|
mv.baseFreq = 185.0f; // Snare drum
|
|
|
|
|
mv.decayRate = 0.00028f;
|
|
|
|
|
mv.maxSamples = kSampleRate / 3;
|
|
|
|
|
} else if (note == 42 || note == 44) {
|
|
|
|
|
mv.baseFreq = 820.0f; // Closed hi-hat
|
|
|
|
|
mv.decayRate = 0.00075f;
|
|
|
|
|
mv.maxSamples = kSampleRate / 8;
|
|
|
|
|
} else {
|
|
|
|
|
mv.baseFreq = 420.0f; // Open hat / cymbal / tom
|
|
|
|
|
mv.decayRate = 0.00018f;
|
|
|
|
|
mv.maxSamples = kSampleRate / 2;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// GM instrument family envelope shaping
|
|
|
|
|
uint8_t family = mv.program / 8;
|
|
|
|
|
if (family == 0 || family == 1 || family == 3 || family == 4 ||
|
|
|
|
|
family == 5 || family == 13) {
|
|
|
|
|
// Piano, Chromatic Percussion, Guitar, Bass, Plucked
|
|
|
|
|
mv.sustainLevel = 0.25f;
|
|
|
|
|
mv.decayRate = 0.000035f;
|
|
|
|
|
mv.maxSamples = kSampleRate * 4;
|
|
|
|
|
} else {
|
|
|
|
|
// Organ, Strings, Ensemble, Brass, Reed, Pipe, Synth Lead/Pad
|
|
|
|
|
mv.sustainLevel = 0.65f;
|
|
|
|
|
mv.decayRate = 0.000012f;
|
|
|
|
|
mv.maxSamples = kSampleRate * 8;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float sampleMidiVoiceLocked(MidiVoice& mv) {
|
|
|
|
|
mv.ageSamples++;
|
|
|
|
|
if (mv.ageSamples >= mv.maxSamples && !mv.releasing) {
|
|
|
|
|
mv.releasing = true;
|
|
|
|
|
mv.releaseRemaining = 960;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Attack / Decay / Sustain / Release envelope
|
|
|
|
|
if (mv.releasing) {
|
|
|
|
|
if (mv.releaseRemaining <= 0) {
|
|
|
|
|
mv.active = false;
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
|
|
|
|
mv.env *= 0.9965f;
|
|
|
|
|
mv.releaseRemaining--;
|
|
|
|
|
if (mv.env < 0.001f) {
|
|
|
|
|
mv.active = false;
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
|
|
|
|
} else if (mv.ageSamples < 144) {
|
|
|
|
|
// 3ms linear attack
|
|
|
|
|
mv.env = static_cast<float>(mv.ageSamples) / 144.0f;
|
|
|
|
|
} else {
|
|
|
|
|
mv.env += (mv.sustainLevel - mv.env) * mv.decayRate * 8.0f;
|
|
|
|
|
if (mv.isDrum && mv.env < 0.002f) {
|
|
|
|
|
mv.active = false;
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Advance LFSR noise generator
|
|
|
|
|
uint32_t bit = ((noiseLfsr_ >> 0) ^ (noiseLfsr_ >> 2) ^ (noiseLfsr_ >> 3) ^
|
|
|
|
|
(noiseLfsr_ >> 5)) &
|
|
|
|
|
1u;
|
|
|
|
|
noiseLfsr_ = (noiseLfsr_ >> 1) | (bit << 15);
|
|
|
|
|
float noise = ((static_cast<int>(noiseLfsr_ & 0xFFu) - 128) / 128.0f);
|
|
|
|
|
|
|
|
|
|
float sample = 0.0f;
|
|
|
|
|
if (mv.isDrum) {
|
|
|
|
|
if (mv.note == 35 || mv.note == 36) {
|
|
|
|
|
// Kick: pitch drops rapidly toward 44Hz
|
|
|
|
|
mv.baseFreq = 44.0f + (mv.baseFreq - 44.0f) * 0.9982f;
|
|
|
|
|
mv.phase += mv.baseFreq / static_cast<float>(kSampleRate);
|
|
|
|
|
if (mv.phase >= 1.0f) mv.phase -= 1.0f;
|
|
|
|
|
sample = fastSin01(mv.phase) * 0.85f +
|
|
|
|
|
(mv.ageSamples < 240 ? noise * 0.25f : 0.0f);
|
|
|
|
|
} else if (mv.note == 38 || mv.note == 40) {
|
|
|
|
|
// Snare: body sine + snappy noise
|
|
|
|
|
mv.phase += mv.baseFreq / static_cast<float>(kSampleRate);
|
|
|
|
|
if (mv.phase >= 1.0f) mv.phase -= 1.0f;
|
|
|
|
|
sample = fastSin01(mv.phase) * 0.35f + noise * 0.65f;
|
|
|
|
|
} else {
|
|
|
|
|
// Hi-hat / cymbal / percussion
|
|
|
|
|
mv.phase += mv.baseFreq / static_cast<float>(kSampleRate);
|
|
|
|
|
if (mv.phase >= 1.0f) mv.phase -= 1.0f;
|
|
|
|
|
float metallic = (mv.phase < 0.5f ? 0.3f : -0.3f);
|
|
|
|
|
sample = noise * 0.55f + metallic * 0.25f;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
float freq = mv.baseFreq;
|
|
|
|
|
mv.phase += freq / static_cast<float>(kSampleRate);
|
|
|
|
|
if (mv.phase >= 1.0f) mv.phase -= 1.0f;
|
|
|
|
|
mv.subPhase += (freq * 0.5f) / static_cast<float>(kSampleRate);
|
|
|
|
|
if (mv.subPhase >= 1.0f) mv.subPhase -= 1.0f;
|
|
|
|
|
|
|
|
|
|
uint8_t family = mv.program / 8;
|
|
|
|
|
float duty = (family == 3 || family == 4) ? 0.25f : 0.5f;
|
|
|
|
|
float pulse = (mv.phase < duty) ? 0.42f : -0.42f;
|
|
|
|
|
float fundamental = fastSin01(mv.phase) * 0.45f;
|
|
|
|
|
float subOct = fastSin01(mv.subPhase) * 0.18f;
|
|
|
|
|
sample = pulse + fundamental + subOct;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float chGain = (midiSeq_.chVolume[mv.channel] / 127.0f) *
|
|
|
|
|
(midiSeq_.chExpr[mv.channel] / 127.0f);
|
|
|
|
|
return sample * mv.env * mv.velocityGain * chGain * 0.2f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool configureLowLatency(snd_pcm_t* pcm, unsigned int rate,
|
|
|
|
|
snd_pcm_uframes_t periodSize = 256,
|
|
|
|
|
snd_pcm_uframes_t bufferSize = 1024) {
|
|
|
|
|
snd_pcm_hw_params_t* hw = nullptr;
|
|
|
|
|
snd_pcm_hw_params_alloca(&hw);
|
|
|
|
|
if (snd_pcm_hw_params_any(pcm, hw) < 0) return false;
|
|
|
|
|
if (snd_pcm_hw_params_set_access(pcm, hw, SND_PCM_ACCESS_RW_INTERLEAVED) <
|
|
|
|
|
0)
|
|
|
|
|
return false;
|
|
|
|
|
if (snd_pcm_hw_params_set_format(pcm, hw, SND_PCM_FORMAT_S16_LE) < 0)
|
|
|
|
|
return false;
|
|
|
|
|
if (snd_pcm_hw_params_set_channels(pcm, hw, 2) < 0) return false;
|
|
|
|
|
if (snd_pcm_hw_params_set_rate_near(pcm, hw, &rate, nullptr) < 0)
|
|
|
|
|
return false;
|
|
|
|
|
if (snd_pcm_hw_params_set_period_size_near(pcm, hw, &periodSize, nullptr) <
|
|
|
|
|
0)
|
|
|
|
|
return false;
|
|
|
|
|
if (snd_pcm_hw_params_set_buffer_size_near(pcm, hw, &bufferSize) < 0)
|
|
|
|
|
return false;
|
|
|
|
|
if (snd_pcm_hw_params(pcm, hw) < 0) return false;
|
|
|
|
|
|
|
|
|
|
snd_pcm_sw_params_t* sw = nullptr;
|
|
|
|
|
snd_pcm_sw_params_alloca(&sw);
|
|
|
|
|
if (snd_pcm_sw_params_current(pcm, sw) == 0) {
|
|
|
|
|
snd_pcm_sw_params_set_start_threshold(pcm, sw, periodSize);
|
|
|
|
|
snd_pcm_sw_params_set_avail_min(pcm, sw, periodSize);
|
|
|
|
|
snd_pcm_sw_params(pcm, sw);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2026-10-28 12:41:00 +02:00
|
|
|
|
|
|
|
|
void audioThreadLoop() {
|
|
|
|
|
snd_pcm_t* pcm = nullptr;
|
2026-10-28 15:25:07 +02:00
|
|
|
// 512-frame periods in a 3072-frame ring (~64 ms at 48 kHz): small enough
|
|
|
|
|
// for responsive SFX, large enough that the synth thread survives being
|
|
|
|
|
// descheduled while the bytecode interpreter saturates a core.
|
2026-10-28 12:41:00 +02:00
|
|
|
constexpr snd_pcm_uframes_t kPeriod = 512;
|
2026-10-28 15:25:07 +02:00
|
|
|
constexpr snd_pcm_uframes_t kBuffer = 3072;
|
2026-10-28 12:41:00 +02:00
|
|
|
int16_t stereoBuf[kPeriod * 2];
|
|
|
|
|
|
2026-10-28 15:25:07 +02:00
|
|
|
// Best effort: ask for real-time or raised priority for the mixer thread
|
|
|
|
|
// (silently ignored when the user lacks CAP_SYS_NICE / RLIMIT_RTPRIO).
|
|
|
|
|
{
|
|
|
|
|
sched_param sp{};
|
|
|
|
|
sp.sched_priority = 10;
|
|
|
|
|
if (pthread_setschedparam(pthread_self(), SCHED_FIFO, &sp) != 0) {
|
|
|
|
|
(void)setpriority(PRIO_PROCESS, static_cast<id_t>(::gettid()), -8);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (running_.load(std::memory_order_relaxed)) {
|
|
|
|
|
bool isPaused = paused_.load(std::memory_order_relaxed);
|
|
|
|
|
bool hasActiveAudio = false;
|
|
|
|
|
bool withinGraceWindow = false;
|
|
|
|
|
|
|
|
|
|
// Follow live changes to the system speaker volume / mute (xdrawer,
|
|
|
|
|
// sxmo hooks, other apps) without restarting the MIDlet.
|
|
|
|
|
{
|
|
|
|
|
int64_t nowV = nowMs();
|
|
|
|
|
if (nowV - lastVolPollMs_ >= 500) {
|
|
|
|
|
lastVolPollMs_ = nowV;
|
|
|
|
|
refreshSystemVolume(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
bool sysMuted = sysMuted_.load(std::memory_order_relaxed);
|
2026-10-28 12:41:00 +02:00
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lk(mu_);
|
2026-10-28 15:25:07 +02:00
|
|
|
hasActiveAudio =
|
|
|
|
|
!voices_.empty() || !pcmStreams_.empty() || midiSeq_.active;
|
|
|
|
|
if (!hasActiveAudio) {
|
|
|
|
|
for (const auto& mv : midiVoices_) {
|
|
|
|
|
if (mv.active) {
|
|
|
|
|
hasActiveAudio = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int64_t now = nowMs();
|
|
|
|
|
if (hasActiveAudio) {
|
|
|
|
|
lastActiveMs_ = now;
|
|
|
|
|
withinGraceWindow = true;
|
|
|
|
|
} else if (lastActiveMs_ > 0 && (now - lastActiveMs_) < 2500) {
|
|
|
|
|
// Keep ALSA open for 2.5s after sound completes so DMA finishes
|
|
|
|
|
// playing without truncation and back-to-back SFX have zero open lag
|
|
|
|
|
withinGraceWindow = true;
|
|
|
|
|
}
|
2026-10-28 12:41:00 +02:00
|
|
|
}
|
2026-10-28 15:25:07 +02:00
|
|
|
|
|
|
|
|
if (!enabled || sysMuted || isPaused || !withinGraceWindow) {
|
2026-10-28 12:41:00 +02:00
|
|
|
if (pcm) {
|
|
|
|
|
snd_pcm_drop(pcm);
|
|
|
|
|
snd_pcm_close(pcm);
|
|
|
|
|
pcm = nullptr;
|
|
|
|
|
}
|
2026-10-28 15:25:07 +02:00
|
|
|
::usleep(10000);
|
2026-10-28 12:41:00 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!pcm) {
|
2026-10-28 15:25:07 +02:00
|
|
|
for (const char* dev : {"hw:0,0", "default", "plughw:0,0"}) {
|
|
|
|
|
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,
|
|
|
|
|
SND_PCM_ACCESS_RW_INTERLEAVED, 2, kSampleRate,
|
|
|
|
|
1, 64000);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2026-10-28 12:41:00 +02:00
|
|
|
}
|
2026-10-28 15:25:07 +02:00
|
|
|
pcm = nullptr;
|
|
|
|
|
}
|
|
|
|
|
if (!pcm) {
|
|
|
|
|
::usleep(40000);
|
|
|
|
|
continue;
|
2026-10-28 12:41:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lk(mu_);
|
2026-10-28 15:25:07 +02:00
|
|
|
float masterGain = sysGain_.load(std::memory_order_relaxed);
|
|
|
|
|
|
2026-10-28 12:41:00 +02:00
|
|
|
for (size_t i = 0; i < kPeriod; ++i) {
|
|
|
|
|
float mix = 0.0f;
|
2026-10-28 15:25:07 +02:00
|
|
|
|
|
|
|
|
// 1. Standalone playTone voices
|
2026-10-28 12:41:00 +02:00
|
|
|
for (auto it = voices_.begin(); it != voices_.end();) {
|
|
|
|
|
if (it->remainingSamples <= 0) {
|
|
|
|
|
it = voices_.erase(it);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
float env = 1.0f;
|
2026-10-28 15:25:07 +02:00
|
|
|
if (it->remainingSamples < 480) {
|
|
|
|
|
env = it->remainingSamples / 480.0f;
|
2026-10-28 12:41:00 +02:00
|
|
|
}
|
2026-10-28 15:25:07 +02:00
|
|
|
float s = (it->phase < 0.5f ? 0.55f : -0.55f) +
|
|
|
|
|
0.45f * fastSin01(it->phase);
|
|
|
|
|
mix += s * it->amp * env * masterGain;
|
|
|
|
|
it->phase += it->freq / static_cast<float>(kSampleRate);
|
2026-10-28 12:41:00 +02:00
|
|
|
if (it->phase >= 1.0f) it->phase -= 1.0f;
|
|
|
|
|
it->remainingSamples--;
|
|
|
|
|
++it;
|
|
|
|
|
}
|
2026-10-28 15:25:07 +02:00
|
|
|
|
|
|
|
|
// 2. Standard MIDI sequencer & polyphonic synth
|
|
|
|
|
if (midiSeq_.active) {
|
|
|
|
|
while (midiSeq_.eventIdx < midiSeq_.events.size() &&
|
|
|
|
|
midiSeq_.events[midiSeq_.eventIdx].frame <=
|
|
|
|
|
midiSeq_.currentFrame) {
|
|
|
|
|
triggerMidiEventLocked(midiSeq_.events[midiSeq_.eventIdx]);
|
|
|
|
|
midiSeq_.eventIdx++;
|
|
|
|
|
}
|
|
|
|
|
midiSeq_.currentFrame++;
|
|
|
|
|
if (midiSeq_.currentFrame >= midiSeq_.totalFrames) {
|
|
|
|
|
if (midiSeq_.loop) {
|
|
|
|
|
midiSeq_.currentFrame = 0;
|
|
|
|
|
midiSeq_.eventIdx = 0;
|
|
|
|
|
} else {
|
|
|
|
|
midiSeq_.active = false;
|
|
|
|
|
}
|
2026-10-28 12:41:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
2026-10-28 15:25:07 +02:00
|
|
|
if (!midiVoices_.empty()) {
|
|
|
|
|
float midiMix = 0.0f;
|
|
|
|
|
for (auto& mv : midiVoices_) {
|
|
|
|
|
if (mv.active) {
|
|
|
|
|
midiMix += sampleMidiVoiceLocked(mv);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
mix += midiMix * midiSeq_.gain * masterGain;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. Multi-stream resampled WAV SFX / audio
|
|
|
|
|
for (auto it = pcmStreams_.begin(); it != pcmStreams_.end();) {
|
|
|
|
|
if (it->pos < it->samples.size()) {
|
|
|
|
|
mix += (it->samples[it->pos++] / 32768.0f) * it->gain *
|
|
|
|
|
masterGain * 0.85f;
|
|
|
|
|
if (it->pos >= it->samples.size()) {
|
|
|
|
|
if (it->loop) {
|
|
|
|
|
it->pos = 0;
|
|
|
|
|
++it;
|
|
|
|
|
} else {
|
|
|
|
|
it = pcmStreams_.erase(it);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
it = pcmStreams_.erase(it);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2-stage low-pass filter + soft knee limiter matching gambatte_phone
|
|
|
|
|
float inSample = mix * 32767.0f;
|
|
|
|
|
lpStage1L_ += 0.58f * (inSample - lpStage1L_);
|
|
|
|
|
lpStage2L_ += 0.58f * (lpStage1L_ - lpStage2L_);
|
|
|
|
|
int32_t filtered = static_cast<int32_t>(lpStage2L_);
|
|
|
|
|
if (filtered > 24000) filtered = 24000 + ((filtered - 24000) >> 2);
|
|
|
|
|
if (filtered < -24000) filtered = -24000 + ((filtered + 24000) >> 2);
|
|
|
|
|
lastSample_ = static_cast<int16_t>(
|
|
|
|
|
std::clamp<int32_t>(filtered, -32768, 32767));
|
|
|
|
|
stereoBuf[i * 2] = lastSample_;
|
|
|
|
|
stereoBuf[i * 2 + 1] = lastSample_;
|
2026-10-28 12:41:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
snd_pcm_sframes_t w = snd_pcm_writei(pcm, stereoBuf, kPeriod);
|
|
|
|
|
if (w < 0) {
|
2026-10-28 15:25:07 +02:00
|
|
|
if (snd_pcm_recover(pcm, static_cast<int>(w), 1) < 0) {
|
|
|
|
|
snd_pcm_prepare(pcm);
|
|
|
|
|
}
|
2026-10-28 12:41:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pcm) {
|
|
|
|
|
snd_pcm_drop(pcm);
|
|
|
|
|
snd_pcm_close(pcm);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Native Stream, Collection, Timer, and Connection Payloads
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
struct NativeInputStream {
|
|
|
|
|
std::vector<uint8_t> data;
|
|
|
|
|
size_t pos = 0;
|
|
|
|
|
size_t markPos = 0;
|
|
|
|
|
int sockFd = -1;
|
|
|
|
|
|
|
|
|
|
~NativeInputStream() {
|
|
|
|
|
// Note: sockFd is owned by NativeConnection if shared, only close if standalone
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int readByte() {
|
|
|
|
|
if (sockFd >= 0 && pos >= data.size()) {
|
|
|
|
|
uint8_t b = 0;
|
|
|
|
|
struct pollfd pfd {sockFd, POLLIN, 0};
|
|
|
|
|
if (::poll(&pfd, 1, 2000) > 0) {
|
|
|
|
|
ssize_t n = ::recv(sockFd, &b, 1, 0);
|
|
|
|
|
if (n == 1) return b;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (pos >= data.size()) return -1;
|
|
|
|
|
return data[pos++];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int readBytes(uint8_t* dst, int len) {
|
|
|
|
|
if (len <= 0) return 0;
|
|
|
|
|
if (sockFd >= 0 && pos >= data.size()) {
|
|
|
|
|
struct pollfd pfd {sockFd, POLLIN, 0};
|
|
|
|
|
if (::poll(&pfd, 1, 2500) > 0) {
|
|
|
|
|
ssize_t n = ::recv(sockFd, dst, len, 0);
|
|
|
|
|
return (n > 0) ? static_cast<int>(n) : -1;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (pos >= data.size()) return -1;
|
|
|
|
|
int avail = static_cast<int>(data.size() - pos);
|
|
|
|
|
int n = std::min(len, avail);
|
|
|
|
|
std::memcpy(dst, data.data() + pos, n);
|
|
|
|
|
pos += n;
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int available() const {
|
|
|
|
|
if (sockFd >= 0) {
|
|
|
|
|
struct pollfd pfd {sockFd, POLLIN, 0};
|
|
|
|
|
if (::poll(&pfd, 1, 0) > 0) return 1;
|
|
|
|
|
}
|
|
|
|
|
return static_cast<int>(data.size() > pos ? (data.size() - pos) : 0);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct NativeOutputStream {
|
|
|
|
|
std::vector<uint8_t> data;
|
|
|
|
|
int sockFd = -1;
|
|
|
|
|
|
|
|
|
|
void writeByte(uint8_t b) {
|
|
|
|
|
if (sockFd >= 0) {
|
|
|
|
|
(void)::send(sockFd, &b, 1, MSG_NOSIGNAL);
|
|
|
|
|
} else {
|
|
|
|
|
data.push_back(b);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void writeBytes(const uint8_t* src, int len) {
|
|
|
|
|
if (len <= 0) return;
|
|
|
|
|
if (sockFd >= 0) {
|
|
|
|
|
(void)::send(sockFd, src, len, MSG_NOSIGNAL);
|
|
|
|
|
} else {
|
|
|
|
|
data.insert(data.end(), src, src + len);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct NativeConnection {
|
|
|
|
|
enum Kind { Socket, Http, File, Datagram, ServerSocket } kind = Socket;
|
|
|
|
|
std::string url;
|
|
|
|
|
std::string host;
|
|
|
|
|
int port = 80;
|
|
|
|
|
int sockFd = -1;
|
|
|
|
|
std::string method = "GET";
|
|
|
|
|
std::map<std::string, std::string> reqHeaders;
|
|
|
|
|
std::map<std::string, std::string> respHeaders;
|
|
|
|
|
std::vector<std::pair<std::string, std::string>> respHeaderList;
|
|
|
|
|
int responseCode = 200;
|
|
|
|
|
std::string responseMessage = "OK";
|
|
|
|
|
bool executed = false;
|
|
|
|
|
std::shared_ptr<NativeInputStream> inStream;
|
|
|
|
|
std::shared_ptr<NativeOutputStream> outStream;
|
|
|
|
|
|
|
|
|
|
~NativeConnection() { close(); }
|
|
|
|
|
|
|
|
|
|
void close() {
|
|
|
|
|
if (sockFd >= 0) {
|
|
|
|
|
::close(sockFd);
|
|
|
|
|
sockFd = -1;
|
|
|
|
|
}
|
|
|
|
|
if (inStream) inStream->sockFd = -1;
|
|
|
|
|
if (outStream) outStream->sockFd = -1;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct ScheduledTimerTask {
|
|
|
|
|
uint32_t taskObjRef = 0;
|
|
|
|
|
int64_t nextRunMs = 0;
|
|
|
|
|
int64_t periodMs = 0;
|
|
|
|
|
bool cancelled = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// CLDC / RMS / GCF / MMAPI Registration & State
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
class CldcNetMediaRuntime {
|
|
|
|
|
public:
|
|
|
|
|
bool networkEnabled = true;
|
|
|
|
|
AlsaSoundEngine sound;
|
|
|
|
|
std::vector<ScheduledTimerTask> timerTasks;
|
|
|
|
|
std::vector<uint32_t> serialCallbacks;
|
|
|
|
|
|
|
|
|
|
void init(Jvm& vm) {
|
|
|
|
|
sound.init();
|
|
|
|
|
registerJavaLang(vm);
|
|
|
|
|
registerJavaUtil(vm);
|
|
|
|
|
registerJavaIo(vm);
|
|
|
|
|
registerRms(vm);
|
|
|
|
|
registerGcfNetwork(vm);
|
|
|
|
|
registerMmapi(vm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void shutdown() {
|
|
|
|
|
sound.shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tickTimersAndSerialCallbacks(Jvm& vm) {
|
|
|
|
|
int64_t now = nowMs();
|
|
|
|
|
for (size_t i = 0; i < timerTasks.size();) {
|
|
|
|
|
auto& t = timerTasks[i];
|
|
|
|
|
const JObject* obj = vm.getObj(t.taskObjRef);
|
|
|
|
|
if (t.cancelled || !obj || obj->getField("cancelled").i != 0) {
|
|
|
|
|
timerTasks.erase(timerTasks.begin() + i);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (now >= t.nextRunMs) {
|
|
|
|
|
vm.invokeMethodSync(t.taskObjRef, "run", "()V", {}, nullptr, 100000);
|
|
|
|
|
if (t.periodMs > 0 && !t.cancelled) {
|
|
|
|
|
t.nextRunMs = now + t.periodMs;
|
|
|
|
|
++i;
|
|
|
|
|
} else {
|
|
|
|
|
timerTasks.erase(timerTasks.begin() + i);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!serialCallbacks.empty()) {
|
|
|
|
|
std::vector<uint32_t> batch = std::move(serialCallbacks);
|
|
|
|
|
serialCallbacks.clear();
|
|
|
|
|
for (uint32_t rRef : batch) {
|
|
|
|
|
if (rRef != 0) {
|
|
|
|
|
vm.invokeMethodSync(rRef, "run", "()V", {}, nullptr, 150000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
static std::shared_ptr<NativeInputStream> getOrCreateInputStream(
|
|
|
|
|
Jvm& vm, uint32_t ref) {
|
|
|
|
|
JObject* o = vm.getObj(ref);
|
|
|
|
|
if (!o) return nullptr;
|
|
|
|
|
if (!o->nativeData) {
|
|
|
|
|
o->nativeData = std::make_shared<NativeInputStream>();
|
|
|
|
|
}
|
|
|
|
|
return std::static_pointer_cast<NativeInputStream>(o->nativeData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static std::shared_ptr<NativeOutputStream> getOrCreateOutputStream(
|
|
|
|
|
Jvm& vm, uint32_t ref) {
|
|
|
|
|
JObject* o = vm.getObj(ref);
|
|
|
|
|
if (!o) return nullptr;
|
|
|
|
|
if (!o->nativeData) {
|
|
|
|
|
o->nativeData = std::make_shared<NativeOutputStream>();
|
|
|
|
|
}
|
|
|
|
|
return std::static_pointer_cast<NativeOutputStream>(o->nativeData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void registerJavaLang(Jvm& vm) {
|
|
|
|
|
// Initialize System.out and System.err static fields
|
|
|
|
|
uint32_t outStreamRef = vm.allocObject("java/io/PrintStream");
|
|
|
|
|
JClass* sysCls = vm.loadClass("java/lang/System");
|
|
|
|
|
if (!sysCls) {
|
|
|
|
|
// Create synthetic System static storage via setStaticField
|
|
|
|
|
}
|
|
|
|
|
(void)outStreamRef;
|
|
|
|
|
|
|
|
|
|
// java/lang/Object
|
|
|
|
|
vm.registerNative("java/lang/Object", "<init>", "()V",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Object", "getClass", "()Ljava/lang/Class;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
const JObject* o = v.getObj(a[0].ref);
|
|
|
|
|
uint32_t cref = v.allocObject("java/lang/Class");
|
|
|
|
|
if (o) v.getObj(cref)->strUtf8 = o->className;
|
|
|
|
|
*r = JVal::MakeRef(cref);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Object", "hashCode", "()I",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeInt(static_cast<int32_t>(a[0].ref));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Object", "equals", "(Ljava/lang/Object;)Z",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeInt(a[0].ref == a[1].ref ? 1 : 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Object", "toString", "()Ljava/lang/String;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
const JObject* o = v.getObj(a[0].ref);
|
|
|
|
|
if (!o) {
|
|
|
|
|
*r = JVal::MakeRef(v.allocString("null"));
|
|
|
|
|
} else if (o->isString ||
|
|
|
|
|
o->className == "java/lang/StringBuffer" ||
|
|
|
|
|
o->className == "java/lang/StringBuilder") {
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(o->strUtf8));
|
|
|
|
|
} else {
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(
|
|
|
|
|
o->className + "@" + std::to_string(a[0].ref)));
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Object", "notify", "()V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
v.notifyWaiters(a[0].ref, false);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Object", "notifyAll", "()V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
v.notifyWaiters(a[0].ref, true);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Object", "wait", "*",
|
|
|
|
|
[](Jvm&, JThread& th, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
th.state = ThreadState::Waiting;
|
|
|
|
|
th.waitingOnRef = a[0].ref;
|
|
|
|
|
int64_t timeout = (a.size() >= 2) ? a[1].l : 0;
|
|
|
|
|
th.wakeTimeMs = (timeout > 0) ? (nowMs() + timeout)
|
|
|
|
|
: (nowMs() + 50);
|
|
|
|
|
th.yieldSlice = true;
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// java/lang/Class
|
|
|
|
|
vm.registerNative("java/lang/Class", "forName",
|
|
|
|
|
"(Ljava/lang/String;)Ljava/lang/Class;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
std::string cname = v.getStringUtf8(a[0].ref);
|
|
|
|
|
for (char& c : cname) {
|
|
|
|
|
if (c == '.') c = '/';
|
|
|
|
|
}
|
|
|
|
|
v.loadClass(cname);
|
|
|
|
|
uint32_t cref = v.allocObject("java/lang/Class");
|
|
|
|
|
v.getObj(cref)->strUtf8 = cname;
|
|
|
|
|
*r = JVal::MakeRef(cref);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Class", "newInstance", "()Ljava/lang/Object;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
const JObject* co = v.getObj(a[0].ref);
|
|
|
|
|
std::string cname = co ? co->strUtf8 : "java/lang/Object";
|
|
|
|
|
uint32_t ref = v.allocObject(cname);
|
|
|
|
|
v.invokeMethodSync(ref, "<init>", "()V");
|
|
|
|
|
*r = JVal::MakeRef(ref);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Class", "getName", "()Ljava/lang/String;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
const JObject* co = v.getObj(a[0].ref);
|
|
|
|
|
std::string cname = co ? co->strUtf8 : "java.lang.Object";
|
|
|
|
|
for (char& c : cname) {
|
|
|
|
|
if (c == '/') c = '.';
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(cname));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"java/lang/Class", "getResourceAsStream",
|
|
|
|
|
"(Ljava/lang/String;)Ljava/io/InputStream;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
if (a.size() < 2 || a[1].ref == 0 || !v.jar) {
|
|
|
|
|
*r = JVal::Null();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
std::string resPath = v.getStringUtf8(a[1].ref);
|
|
|
|
|
const JObject* clsObj = v.getObj(a[0].ref);
|
|
|
|
|
if (!resPath.empty() && resPath[0] != '/' && clsObj &&
|
|
|
|
|
!clsObj->strUtf8.empty()) {
|
|
|
|
|
size_t sl = clsObj->strUtf8.rfind('/');
|
|
|
|
|
if (sl != std::string::npos) {
|
|
|
|
|
resPath = clsObj->strUtf8.substr(0, sl + 1) + resPath;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
std::vector<uint8_t> bytes;
|
|
|
|
|
if (!v.jar->extractEntry(resPath, &bytes)) {
|
|
|
|
|
*r = JVal::Null();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
uint32_t isRef = v.allocObject("java/io/ByteArrayInputStream");
|
|
|
|
|
auto nis = getOrCreateInputStream(v, isRef);
|
|
|
|
|
nis->data = std::move(bytes);
|
|
|
|
|
*r = JVal::MakeRef(isRef);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// java/lang/System & Runtime
|
|
|
|
|
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*) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/System", "exit", "(I)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
v.exitRequested = true;
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/System", "identityHashCode",
|
|
|
|
|
"(Ljava/lang/Object;)I",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeInt(static_cast<int32_t>(a[0].ref));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"java/lang/System", "arraycopy",
|
|
|
|
|
"(Ljava/lang/Object;ILjava/lang/Object;II)V",
|
|
|
|
|
[](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* src = v.getObj(a[0].ref);
|
|
|
|
|
int32_t srcPos = a[1].i;
|
|
|
|
|
JObject* dst = v.getObj(a[2].ref);
|
|
|
|
|
int32_t dstPos = a[3].i;
|
|
|
|
|
int32_t len = a[4].i;
|
|
|
|
|
if (!src || !dst) {
|
|
|
|
|
v.throwException(th, "java/lang/NullPointerException");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (len <= 0) return true;
|
|
|
|
|
if (srcPos < 0 || dstPos < 0 || srcPos + len > src->arrayLength() ||
|
|
|
|
|
dstPos + len > dst->arrayLength()) {
|
|
|
|
|
v.throwException(th, "java/lang/ArrayIndexOutOfBoundsException");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
auto copyVec = [&](auto& sv, auto& dv) {
|
|
|
|
|
if (&sv == &dv && dstPos > srcPos) {
|
|
|
|
|
for (int32_t i = len - 1; i >= 0; --i) {
|
|
|
|
|
dv[dstPos + i] = sv[srcPos + i];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for (int32_t i = 0; i < len; ++i) {
|
|
|
|
|
dv[dstPos + i] = sv[srcPos + i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
switch (src->arrayKind) {
|
|
|
|
|
case ArrayKind::ByteOrBool: copyVec(src->byteArr, dst->byteArr); break;
|
|
|
|
|
case ArrayKind::Char: copyVec(src->charArr, dst->charArr); break;
|
|
|
|
|
case ArrayKind::Short: copyVec(src->shortArr, dst->shortArr); break;
|
|
|
|
|
case ArrayKind::Int: copyVec(src->intArr, dst->intArr); break;
|
|
|
|
|
case ArrayKind::Long: copyVec(src->longArr, dst->longArr); break;
|
|
|
|
|
case ArrayKind::Float: copyVec(src->floatArr, dst->floatArr); break;
|
|
|
|
|
case ArrayKind::Double: copyVec(src->doubleArr, dst->doubleArr); break;
|
|
|
|
|
case ArrayKind::Ref: copyVec(src->refArr, dst->refArr); break;
|
|
|
|
|
default: break;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"java/lang/System", "getProperty",
|
|
|
|
|
"(Ljava/lang/String;)Ljava/lang/String;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
std::string key = v.getStringUtf8(a[0].ref);
|
|
|
|
|
std::string val;
|
|
|
|
|
if (key == "microedition.profiles") val = "MIDP-2.0";
|
|
|
|
|
else if (key == "microedition.configuration") val = "CLDC-1.1";
|
|
|
|
|
else if (key == "microedition.platform") val = "Nokia8110-4G/ReMEdy-1.0";
|
|
|
|
|
else if (key == "microedition.encoding") val = "UTF-8";
|
|
|
|
|
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 == "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/";
|
|
|
|
|
else if (key == "fileconn.dir.private") val = "file:///home/user/.local/share/sxmo/remedy/files/";
|
|
|
|
|
else if (key == "com.nokia.mid.batterylevel") val = "90";
|
|
|
|
|
else if (v.suiteInfo) val = v.suiteInfo->getProperty(key);
|
|
|
|
|
if (val.empty()) {
|
|
|
|
|
*r = JVal::Null();
|
|
|
|
|
} else {
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(val));
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
vm.registerNative("java/lang/Runtime", "getRuntime",
|
|
|
|
|
"()Ljava/lang/Runtime;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
|
|
|
|
*r = JVal::MakeRef(v.allocObject("java/lang/Runtime"));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Runtime", "freeMemory", "()J",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
|
|
|
|
*r = JVal::MakeLong(16 * 1024 * 1024LL);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Runtime", "totalMemory", "()J",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
|
|
|
|
*r = JVal::MakeLong(32 * 1024 * 1024LL);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Runtime", "gc", "()V",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// java/lang/String
|
|
|
|
|
vm.registerNative("java/lang/String", "<init>", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (!self) return true;
|
|
|
|
|
self->isString = true;
|
|
|
|
|
if (a.size() == 1) {
|
|
|
|
|
self->strUtf8.clear();
|
|
|
|
|
} else if (a.size() >= 2) {
|
|
|
|
|
const JObject* arg1 = v.getObj(a[1].ref);
|
|
|
|
|
if (!arg1) {
|
|
|
|
|
self->strUtf8.clear();
|
|
|
|
|
} else if (arg1->isString ||
|
|
|
|
|
arg1->className == "java/lang/StringBuffer" ||
|
|
|
|
|
arg1->className == "java/lang/StringBuilder") {
|
|
|
|
|
self->strUtf8 = arg1->strUtf8;
|
|
|
|
|
} else if (arg1->arrayKind == ArrayKind::Char) {
|
|
|
|
|
int off = (a.size() >= 4) ? a[2].i : 0;
|
|
|
|
|
int len = (a.size() >= 4)
|
|
|
|
|
? a[3].i
|
|
|
|
|
: static_cast<int>(arg1->charArr.size());
|
|
|
|
|
off = std::clamp(off, 0,
|
|
|
|
|
static_cast<int>(arg1->charArr.size()));
|
|
|
|
|
len = std::clamp(
|
|
|
|
|
len, 0,
|
|
|
|
|
static_cast<int>(arg1->charArr.size()) - off);
|
|
|
|
|
self->strUtf8 =
|
|
|
|
|
Jvm::u16ToUtf8(arg1->charArr.data() + off, len);
|
|
|
|
|
} else if (arg1->arrayKind == ArrayKind::ByteOrBool) {
|
|
|
|
|
int off = (a.size() >= 4 && a[2].type == JType::Int)
|
|
|
|
|
? a[2].i
|
|
|
|
|
: 0;
|
|
|
|
|
int len = (a.size() >= 4 && a[3].type == JType::Int)
|
|
|
|
|
? a[3].i
|
|
|
|
|
: static_cast<int>(arg1->byteArr.size());
|
|
|
|
|
off = std::clamp(off, 0,
|
|
|
|
|
static_cast<int>(arg1->byteArr.size()));
|
|
|
|
|
len = std::clamp(
|
|
|
|
|
len, 0,
|
|
|
|
|
static_cast<int>(arg1->byteArr.size()) - off);
|
|
|
|
|
self->strUtf8.assign(
|
|
|
|
|
reinterpret_cast<const char*>(
|
|
|
|
|
arg1->byteArr.data() + off),
|
|
|
|
|
len);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/String", "length", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto u16 = Jvm::utf8ToU16(v.getStringUtf8(a[0].ref));
|
|
|
|
|
*r = JVal::MakeInt(static_cast<int32_t>(u16.size()));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/String", "charAt", "(I)C",
|
|
|
|
|
[](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto u16 = Jvm::utf8ToU16(v.getStringUtf8(a[0].ref));
|
|
|
|
|
int32_t idx = a[1].i;
|
|
|
|
|
if (idx < 0 || idx >= static_cast<int32_t>(u16.size())) {
|
|
|
|
|
v.throwException(
|
|
|
|
|
th, "java/lang/IndexOutOfBoundsException");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeInt(u16[idx]);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/String", "substring", "(I)Ljava/lang/String;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto u16 = Jvm::utf8ToU16(v.getStringUtf8(a[0].ref));
|
|
|
|
|
int b = std::clamp<int>(a[1].i, 0, u16.size());
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(
|
|
|
|
|
Jvm::u16ToUtf8(u16.data() + b, u16.size() - b)));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/String", "substring", "(II)Ljava/lang/String;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto u16 = Jvm::utf8ToU16(v.getStringUtf8(a[0].ref));
|
|
|
|
|
int b = std::clamp<int>(a[1].i, 0, u16.size());
|
|
|
|
|
int e = std::clamp<int>(a[2].i, b, u16.size());
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(
|
|
|
|
|
Jvm::u16ToUtf8(u16.data() + b, e - b)));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/String", "equals", "(Ljava/lang/Object;)Z",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
if (a[1].ref == 0) {
|
|
|
|
|
*r = JVal::MakeInt(0);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
const JObject* o2 = v.getObj(a[1].ref);
|
|
|
|
|
if (!o2 || (!o2->isString &&
|
|
|
|
|
o2->className != "java/lang/String")) {
|
|
|
|
|
*r = JVal::MakeInt(0);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeInt(v.getStringUtf8(a[0].ref) ==
|
|
|
|
|
v.getStringUtf8(a[1].ref)
|
|
|
|
|
? 1
|
|
|
|
|
: 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/String", "equalsIgnoreCase",
|
|
|
|
|
"(Ljava/lang/String;)Z",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
if (a[1].ref == 0) {
|
|
|
|
|
*r = JVal::MakeInt(0);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeInt(
|
|
|
|
|
toLowerStr(v.getStringUtf8(a[0].ref)) ==
|
|
|
|
|
toLowerStr(v.getStringUtf8(a[1].ref))
|
|
|
|
|
? 1
|
|
|
|
|
: 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/String", "compareTo",
|
|
|
|
|
"(Ljava/lang/String;)I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
std::string s1 = v.getStringUtf8(a[0].ref);
|
|
|
|
|
std::string s2 = v.getStringUtf8(a[1].ref);
|
|
|
|
|
*r = JVal::MakeInt(s1.compare(s2));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/String", "startsWith", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
std::string s = v.getStringUtf8(a[0].ref);
|
|
|
|
|
std::string p = v.getStringUtf8(a[1].ref);
|
|
|
|
|
int off = (a.size() >= 3) ? std::max(0, a[2].i) : 0;
|
|
|
|
|
if (static_cast<size_t>(off) > s.size()) {
|
|
|
|
|
*r = JVal::MakeInt(0);
|
|
|
|
|
} else {
|
|
|
|
|
*r = JVal::MakeInt(s.rfind(p, off) ==
|
|
|
|
|
static_cast<size_t>(off)
|
|
|
|
|
? 1
|
|
|
|
|
: 0);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/String", "endsWith",
|
|
|
|
|
"(Ljava/lang/String;)Z",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
std::string s = v.getStringUtf8(a[0].ref);
|
|
|
|
|
std::string p = v.getStringUtf8(a[1].ref);
|
|
|
|
|
bool ok = s.size() >= p.size() &&
|
|
|
|
|
s.compare(s.size() - p.size(), p.size(), p) == 0;
|
|
|
|
|
*r = JVal::MakeInt(ok ? 1 : 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2026-10-28 15:25:07 +02:00
|
|
|
vm.registerNative(
|
|
|
|
|
"java/lang/String", "regionMatches", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
bool ignoreCase = false;
|
|
|
|
|
int toffset = 0;
|
|
|
|
|
uint32_t otherRef = 0;
|
|
|
|
|
int ooffset = 0;
|
|
|
|
|
int len = 0;
|
|
|
|
|
if (a.size() >= 6) {
|
|
|
|
|
ignoreCase = (a[1].i != 0);
|
|
|
|
|
toffset = a[2].i;
|
|
|
|
|
otherRef = a[3].ref;
|
|
|
|
|
ooffset = a[4].i;
|
|
|
|
|
len = a[5].i;
|
|
|
|
|
} else if (a.size() >= 5) {
|
|
|
|
|
toffset = a[1].i;
|
|
|
|
|
otherRef = a[2].ref;
|
|
|
|
|
ooffset = a[3].i;
|
|
|
|
|
len = a[4].i;
|
|
|
|
|
}
|
|
|
|
|
if (otherRef == 0 || toffset < 0 || ooffset < 0 || len < 0) {
|
|
|
|
|
*r = JVal::MakeInt(0);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
auto s1 = Jvm::utf8ToU16(v.getStringUtf8(a[0].ref));
|
|
|
|
|
auto s2 = Jvm::utf8ToU16(v.getStringUtf8(otherRef));
|
|
|
|
|
if (static_cast<size_t>(toffset + len) > s1.size() ||
|
|
|
|
|
static_cast<size_t>(ooffset + len) > s2.size()) {
|
|
|
|
|
*r = JVal::MakeInt(0);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
bool match = true;
|
|
|
|
|
for (int i = 0; i < len; ++i) {
|
|
|
|
|
uint16_t c1 = s1[toffset + i];
|
|
|
|
|
uint16_t c2 = s2[ooffset + i];
|
|
|
|
|
if (c1 == c2) continue;
|
|
|
|
|
if (ignoreCase && c1 < 128 && c2 < 128 &&
|
|
|
|
|
std::tolower(c1) == std::tolower(c2)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
match = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeInt(match ? 1 : 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2026-10-28 12:41:00 +02:00
|
|
|
vm.registerNative("java/lang/String", "indexOf", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
std::string s = v.getStringUtf8(a[0].ref);
|
|
|
|
|
size_t from = (a.size() >= 3)
|
|
|
|
|
? static_cast<size_t>(std::max(0, a[2].i))
|
|
|
|
|
: 0;
|
|
|
|
|
size_t pos = std::string::npos;
|
|
|
|
|
if (a[1].type == JType::Ref) {
|
|
|
|
|
pos = s.find(v.getStringUtf8(a[1].ref), from);
|
|
|
|
|
} else {
|
|
|
|
|
pos = s.find(static_cast<char>(a[1].i), from);
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeInt(pos == std::string::npos
|
|
|
|
|
? -1
|
|
|
|
|
: static_cast<int32_t>(pos));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/String", "lastIndexOf", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
std::string s = v.getStringUtf8(a[0].ref);
|
|
|
|
|
size_t from = (a.size() >= 3)
|
|
|
|
|
? static_cast<size_t>(std::max(0, a[2].i))
|
|
|
|
|
: std::string::npos;
|
|
|
|
|
size_t pos = std::string::npos;
|
|
|
|
|
if (a[1].type == JType::Ref) {
|
|
|
|
|
pos = s.rfind(v.getStringUtf8(a[1].ref), from);
|
|
|
|
|
} else {
|
|
|
|
|
pos = s.rfind(static_cast<char>(a[1].i), from);
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeInt(pos == std::string::npos
|
|
|
|
|
? -1
|
|
|
|
|
: static_cast<int32_t>(pos));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/String", "trim", "()Ljava/lang/String;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeRef(
|
|
|
|
|
v.allocString(trimStr(v.getStringUtf8(a[0].ref))));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/String", "toLowerCase",
|
|
|
|
|
"()Ljava/lang/String;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeRef(
|
|
|
|
|
v.allocString(toLowerStr(v.getStringUtf8(a[0].ref))));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/String", "toUpperCase",
|
|
|
|
|
"()Ljava/lang/String;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
std::string s = v.getStringUtf8(a[0].ref);
|
|
|
|
|
for (char& c : s) {
|
|
|
|
|
c = static_cast<char>(
|
|
|
|
|
std::toupper(static_cast<unsigned char>(c)));
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(s));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/String", "replace", "(CC)Ljava/lang/String;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto u16 = Jvm::utf8ToU16(v.getStringUtf8(a[0].ref));
|
|
|
|
|
uint16_t oldC = static_cast<uint16_t>(a[1].i);
|
|
|
|
|
uint16_t newC = static_cast<uint16_t>(a[2].i);
|
|
|
|
|
for (uint16_t& c : u16) {
|
|
|
|
|
if (c == oldC) c = newC;
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(
|
|
|
|
|
Jvm::u16ToUtf8(u16.data(), u16.size())));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/String", "concat",
|
|
|
|
|
"(Ljava/lang/String;)Ljava/lang/String;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(
|
|
|
|
|
v.getStringUtf8(a[0].ref) +
|
|
|
|
|
v.getStringUtf8(a[1].ref)));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/String", "getBytes", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
std::string s = v.getStringUtf8(a[0].ref);
|
|
|
|
|
uint32_t arr = v.allocPrimitiveArray(
|
|
|
|
|
8, static_cast<int32_t>(s.size()));
|
|
|
|
|
JObject* ao = v.getObj(arr);
|
|
|
|
|
for (size_t i = 0; i < s.size(); ++i) {
|
|
|
|
|
ao->byteArr[i] = static_cast<int8_t>(s[i]);
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeRef(arr);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/String", "toCharArray", "()[C",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto u16 = Jvm::utf8ToU16(v.getStringUtf8(a[0].ref));
|
|
|
|
|
uint32_t arr = v.allocPrimitiveArray(
|
|
|
|
|
5, static_cast<int32_t>(u16.size()));
|
|
|
|
|
v.getObj(arr)->charArr = std::move(u16);
|
|
|
|
|
*r = JVal::MakeRef(arr);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/String", "hashCode", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto u16 = Jvm::utf8ToU16(v.getStringUtf8(a[0].ref));
|
|
|
|
|
uint32_t h = 0;
|
|
|
|
|
for (uint16_t c : u16) h = 31u * h + c;
|
|
|
|
|
*r = JVal::MakeInt(static_cast<int32_t>(h));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/String", "intern", "()Ljava/lang/String;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeRef(
|
|
|
|
|
v.internString(v.getStringUtf8(a[0].ref)));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/String", "valueOf", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
if (a.empty()) {
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(""));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
const JVal& x = a[0];
|
|
|
|
|
if (x.type == JType::Ref) {
|
|
|
|
|
if (x.ref == 0) {
|
|
|
|
|
*r = JVal::MakeRef(v.allocString("null"));
|
|
|
|
|
} else {
|
|
|
|
|
const JObject* o = v.getObj(x.ref);
|
|
|
|
|
if (o && o->arrayKind == ArrayKind::Char) {
|
|
|
|
|
int off = (a.size() >= 3) ? a[1].i : 0;
|
|
|
|
|
int len = (a.size() >= 3)
|
|
|
|
|
? a[2].i
|
|
|
|
|
: static_cast<int>(o->charArr.size());
|
|
|
|
|
off = std::clamp(
|
|
|
|
|
off, 0, static_cast<int>(o->charArr.size()));
|
|
|
|
|
len = std::clamp(
|
|
|
|
|
len, 0,
|
|
|
|
|
static_cast<int>(o->charArr.size()) - off);
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(
|
|
|
|
|
Jvm::u16ToUtf8(o->charArr.data() + off, len)));
|
|
|
|
|
} else {
|
|
|
|
|
*r = JVal::MakeRef(
|
|
|
|
|
v.allocString(v.getStringUtf8(x.ref)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (x.type == JType::Long) {
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(std::to_string(x.l)));
|
|
|
|
|
} else if (x.type == JType::Float) {
|
|
|
|
|
char buf[64];
|
|
|
|
|
std::snprintf(buf, sizeof(buf), "%g", x.f);
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(buf));
|
|
|
|
|
} else if (x.type == JType::Double) {
|
|
|
|
|
char buf[64];
|
|
|
|
|
std::snprintf(buf, sizeof(buf), "%g", x.d);
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(buf));
|
|
|
|
|
} else {
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(std::to_string(x.i)));
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// java/lang/StringBuffer & StringBuilder
|
|
|
|
|
for (const char* sbCls :
|
|
|
|
|
{"java/lang/StringBuffer", "java/lang/StringBuilder"}) {
|
|
|
|
|
vm.registerNative(sbCls, "<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 && a[1].ref != 0) {
|
|
|
|
|
self->strUtf8 = v.getStringUtf8(a[1].ref);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
sbCls, "append", "(C)Ljava/lang/StringBuffer;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
uint16_t ch = static_cast<uint16_t>(a[1].i);
|
|
|
|
|
if (self) self->strUtf8 += Jvm::u16ToUtf8(&ch, 1);
|
|
|
|
|
*r = a[0];
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
sbCls, "append", "(C)Ljava/lang/StringBuilder;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
uint16_t ch = static_cast<uint16_t>(a[1].i);
|
|
|
|
|
if (self) self->strUtf8 += Jvm::u16ToUtf8(&ch, 1);
|
|
|
|
|
*r = a[0];
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
sbCls, "append", "(Z)Ljava/lang/StringBuffer;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self) self->strUtf8 += (a[1].i != 0 ? "true" : "false");
|
|
|
|
|
*r = a[0];
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
sbCls, "append", "(Z)Ljava/lang/StringBuilder;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self) self->strUtf8 += (a[1].i != 0 ? "true" : "false");
|
|
|
|
|
*r = a[0];
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
sbCls, "append", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self && a.size() >= 2) {
|
|
|
|
|
const JVal& x = a[1];
|
|
|
|
|
if (x.type == JType::Ref) {
|
|
|
|
|
if (x.ref == 0) {
|
|
|
|
|
self->strUtf8 += "null";
|
|
|
|
|
} else {
|
|
|
|
|
const JObject* o = v.getObj(x.ref);
|
|
|
|
|
if (o && o->arrayKind == ArrayKind::Char) {
|
|
|
|
|
int off = (a.size() >= 4) ? a[2].i : 0;
|
|
|
|
|
int len = (a.size() >= 4)
|
|
|
|
|
? a[3].i
|
|
|
|
|
: static_cast<int>(o->charArr.size());
|
|
|
|
|
off = std::clamp(off, 0,
|
|
|
|
|
static_cast<int>(o->charArr.size()));
|
|
|
|
|
len = std::clamp(
|
|
|
|
|
len, 0, static_cast<int>(o->charArr.size()) - off);
|
|
|
|
|
self->strUtf8 +=
|
|
|
|
|
Jvm::u16ToUtf8(o->charArr.data() + off, len);
|
|
|
|
|
} else {
|
|
|
|
|
self->strUtf8 += v.getStringUtf8(x.ref);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (x.type == JType::Long) {
|
|
|
|
|
self->strUtf8 += std::to_string(x.l);
|
|
|
|
|
} else if (x.type == JType::Float) {
|
|
|
|
|
char buf[64];
|
|
|
|
|
std::snprintf(buf, sizeof(buf), "%g", x.f);
|
|
|
|
|
self->strUtf8 += buf;
|
|
|
|
|
} else if (x.type == JType::Double) {
|
|
|
|
|
char buf[64];
|
|
|
|
|
std::snprintf(buf, sizeof(buf), "%g", x.d);
|
|
|
|
|
self->strUtf8 += buf;
|
|
|
|
|
} else {
|
|
|
|
|
self->strUtf8 += std::to_string(x.i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*r = a[0];
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(sbCls, "length", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
auto u16 = Jvm::utf8ToU16(self ? self->strUtf8 : "");
|
|
|
|
|
*r = JVal::MakeInt(static_cast<int32_t>(u16.size()));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(sbCls, "setLength", "(I)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (!self) return true;
|
|
|
|
|
auto u16 = Jvm::utf8ToU16(self->strUtf8);
|
|
|
|
|
int n = std::max(0, a[1].i);
|
|
|
|
|
u16.resize(n, 0);
|
|
|
|
|
self->strUtf8 = Jvm::u16ToUtf8(u16.data(), u16.size());
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(sbCls, "charAt", "(I)C",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
auto u16 = Jvm::utf8ToU16(self ? self->strUtf8 : "");
|
|
|
|
|
int idx = a[1].i;
|
|
|
|
|
*r = JVal::MakeInt(
|
|
|
|
|
(idx >= 0 && idx < static_cast<int>(u16.size()))
|
|
|
|
|
? u16[idx]
|
|
|
|
|
: 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2026-10-28 15:25:07 +02:00
|
|
|
vm.registerNative(sbCls, "setCharAt", "(IC)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (!self) return true;
|
|
|
|
|
auto u16 = Jvm::utf8ToU16(self->strUtf8);
|
|
|
|
|
int idx = a[1].i;
|
|
|
|
|
if (idx >= 0 && idx < static_cast<int>(u16.size())) {
|
|
|
|
|
u16[idx] = static_cast<uint16_t>(a[2].i);
|
|
|
|
|
self->strUtf8 =
|
|
|
|
|
Jvm::u16ToUtf8(u16.data(), u16.size());
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(sbCls, "ensureCapacity", "(I)V",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(sbCls, "capacity", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
*r = JVal::MakeInt(
|
|
|
|
|
static_cast<int32_t>(
|
|
|
|
|
(self ? self->strUtf8.size() : 0) + 16));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
sbCls, "insert", "(IC)Ljava/lang/StringBuffer;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self) {
|
|
|
|
|
auto u16 = Jvm::utf8ToU16(self->strUtf8);
|
|
|
|
|
int idx = std::clamp<int>(a[1].i, 0, u16.size());
|
|
|
|
|
u16.insert(u16.begin() + idx, static_cast<uint16_t>(a[2].i));
|
|
|
|
|
self->strUtf8 = Jvm::u16ToUtf8(u16.data(), u16.size());
|
|
|
|
|
}
|
|
|
|
|
*r = a[0];
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
sbCls, "insert", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self && a.size() >= 3) {
|
|
|
|
|
auto u16 = Jvm::utf8ToU16(self->strUtf8);
|
|
|
|
|
int idx = std::clamp<int>(a[1].i, 0, u16.size());
|
|
|
|
|
std::string ins;
|
|
|
|
|
const JVal& x = a[2];
|
|
|
|
|
if (x.type == JType::Ref) {
|
|
|
|
|
ins = (x.ref == 0) ? "null" : v.getStringUtf8(x.ref);
|
|
|
|
|
} else if (x.type == JType::Long) {
|
|
|
|
|
ins = std::to_string(x.l);
|
|
|
|
|
} else {
|
|
|
|
|
ins = std::to_string(x.i);
|
|
|
|
|
}
|
|
|
|
|
auto insU16 = Jvm::utf8ToU16(ins);
|
|
|
|
|
u16.insert(u16.begin() + idx, insU16.begin(), insU16.end());
|
|
|
|
|
self->strUtf8 = Jvm::u16ToUtf8(u16.data(), u16.size());
|
|
|
|
|
}
|
|
|
|
|
*r = a[0];
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(sbCls, "deleteCharAt", "(I)Ljava/lang/StringBuffer;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self) {
|
|
|
|
|
auto u16 = Jvm::utf8ToU16(self->strUtf8);
|
|
|
|
|
if (a[1].i >= 0 &&
|
|
|
|
|
a[1].i < static_cast<int>(u16.size())) {
|
|
|
|
|
u16.erase(u16.begin() + a[1].i);
|
|
|
|
|
self->strUtf8 =
|
|
|
|
|
Jvm::u16ToUtf8(u16.data(), u16.size());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*r = a[0];
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2026-10-28 12:41:00 +02:00
|
|
|
vm.registerNative(sbCls, "delete", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self) {
|
|
|
|
|
auto u16 = Jvm::utf8ToU16(self->strUtf8);
|
|
|
|
|
int s = std::clamp<int>(a[1].i, 0, u16.size());
|
|
|
|
|
int e = std::clamp<int>(a[2].i, s, u16.size());
|
|
|
|
|
u16.erase(u16.begin() + s, u16.begin() + e);
|
|
|
|
|
self->strUtf8 =
|
|
|
|
|
Jvm::u16ToUtf8(u16.data(), u16.size());
|
|
|
|
|
}
|
|
|
|
|
*r = a[0];
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(sbCls, "toString", "()Ljava/lang/String;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
*r = JVal::MakeRef(
|
|
|
|
|
v.allocString(self ? self->strUtf8 : ""));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Boxed primitive classes (Integer, Long, Short, Byte, Boolean, Character, Float, Double)
|
|
|
|
|
for (const char* bCls :
|
|
|
|
|
{"java/lang/Integer", "java/lang/Short", "java/lang/Byte",
|
|
|
|
|
"java/lang/Boolean", "java/lang/Character"}) {
|
|
|
|
|
vm.registerNative(bCls, "<init>", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self && a.size() >= 2) {
|
|
|
|
|
if (a[1].type == JType::Ref) {
|
|
|
|
|
self->setField(
|
|
|
|
|
"value",
|
|
|
|
|
JVal::MakeInt(std::atoi(
|
|
|
|
|
v.getStringUtf8(a[1].ref).c_str())));
|
|
|
|
|
} else {
|
|
|
|
|
self->setField("value", JVal::MakeInt(a[1].i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
for (const char* mName :
|
|
|
|
|
{"intValue", "shortValue", "byteValue", "booleanValue", "charValue"}) {
|
|
|
|
|
vm.registerNative(bCls, mName, "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a,
|
|
|
|
|
JVal* r) {
|
|
|
|
|
const JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
*r = JVal::MakeInt(self ? self->getField("value").i
|
|
|
|
|
: 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
vm.registerNative(bCls, "toString", "()Ljava/lang/String;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
const JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
int32_t val = self ? self->getField("value").i : 0;
|
|
|
|
|
if (self && self->className == "java/lang/Boolean") {
|
|
|
|
|
*r = JVal::MakeRef(
|
|
|
|
|
v.allocString(val != 0 ? "true" : "false"));
|
|
|
|
|
} else {
|
|
|
|
|
*r = JVal::MakeRef(
|
|
|
|
|
v.allocString(std::to_string(val)));
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
vm.registerNative("java/lang/Integer", "toString", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
if (a[0].type == JType::Ref) {
|
|
|
|
|
const JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(
|
|
|
|
|
std::to_string(self ? self->getField("value").i : 0)));
|
|
|
|
|
} else {
|
|
|
|
|
int radix = (a.size() >= 2) ? a[1].i : 10;
|
|
|
|
|
if (radix == 16) {
|
|
|
|
|
char buf[32];
|
|
|
|
|
std::snprintf(buf, sizeof(buf), "%x", a[0].i);
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(buf));
|
|
|
|
|
} else {
|
|
|
|
|
*r = JVal::MakeRef(
|
|
|
|
|
v.allocString(std::to_string(a[0].i)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Integer", "toHexString",
|
|
|
|
|
"(I)Ljava/lang/String;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
char buf[32];
|
|
|
|
|
std::snprintf(buf, sizeof(buf), "%x",
|
|
|
|
|
static_cast<uint32_t>(a[0].i));
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(buf));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Integer", "parseInt", "*",
|
|
|
|
|
[](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
std::string s = trimStr(v.getStringUtf8(a[0].ref));
|
|
|
|
|
int radix = (a.size() >= 2) ? a[1].i : 10;
|
|
|
|
|
if (s.empty()) {
|
|
|
|
|
v.throwException(th, "java/lang/NumberFormatException");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
char* endp = nullptr;
|
|
|
|
|
long val = std::strtol(s.c_str(), &endp, radix);
|
|
|
|
|
*r = JVal::MakeInt(static_cast<int32_t>(val));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Integer", "valueOf", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
int32_t val = (a[0].type == JType::Ref)
|
|
|
|
|
? std::atoi(
|
|
|
|
|
v.getStringUtf8(a[0].ref).c_str())
|
|
|
|
|
: a[0].i;
|
|
|
|
|
uint32_t ref = v.allocObject("java/lang/Integer");
|
|
|
|
|
v.getObj(ref)->setField("value", JVal::MakeInt(val));
|
|
|
|
|
*r = JVal::MakeRef(ref);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Long", "<init>", "(J)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self) self->setField("value", JVal::MakeLong(a[1].l));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Long", "longValue", "()J",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
const JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
*r = JVal::MakeLong(self ? self->getField("value", 'J').l
|
|
|
|
|
: 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Long", "parseLong", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
std::string s = trimStr(v.getStringUtf8(a[0].ref));
|
|
|
|
|
int radix = (a.size() >= 2) ? a[1].i : 10;
|
|
|
|
|
*r = JVal::MakeLong(std::strtoll(s.c_str(), nullptr, radix));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Long", "toString", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
int64_t val = (a[0].type == JType::Ref)
|
|
|
|
|
? v.getObj(a[0].ref)->getField("value", 'J').l
|
|
|
|
|
: a[0].l;
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(std::to_string(val)));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// java/lang/Math
|
|
|
|
|
vm.registerNative("java/lang/Math", "abs", "(I)I",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeInt(std::abs(a[0].i));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Math", "abs", "(J)J",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeLong(std::llabs(a[0].l));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Math", "abs", "(F)F",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeFloat(std::fabs(a[0].f));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Math", "abs", "(D)D",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeDouble(std::fabs(a[0].d));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Math", "min", "(II)I",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeInt(std::min(a[0].i, a[1].i));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Math", "min", "(JJ)J",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeLong(std::min(a[0].l, a[1].l));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Math", "min", "(FF)F",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeFloat(std::min(a[0].f, a[1].f));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Math", "min", "(DD)D",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeDouble(std::min(a[0].d, a[1].d));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Math", "max", "(II)I",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeInt(std::max(a[0].i, a[1].i));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Math", "max", "(JJ)J",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeLong(std::max(a[0].l, a[1].l));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Math", "max", "(FF)F",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeFloat(std::max(a[0].f, a[1].f));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Math", "max", "(DD)D",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeDouble(std::max(a[0].d, a[1].d));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Math", "sin", "(D)D",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeDouble(std::sin(a[0].d));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Math", "cos", "(D)D",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeDouble(std::cos(a[0].d));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Math", "tan", "(D)D",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeDouble(std::tan(a[0].d));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Math", "sqrt", "(D)D",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeDouble(std::sqrt(a[0].d));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Math", "ceil", "(D)D",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeDouble(std::ceil(a[0].d));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Math", "floor", "(D)D",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeDouble(std::floor(a[0].d));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Math", "toRadians", "(D)D",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeDouble(a[0].d * 0.017453292519943295);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Math", "toDegrees", "(D)D",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
*r = JVal::MakeDouble(a[0].d * 57.29577951308232);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// java/lang/Thread
|
|
|
|
|
vm.registerNative("java/lang/Thread", "<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) {
|
|
|
|
|
const JObject* arg1 = v.getObj(a[1].ref);
|
|
|
|
|
if (arg1 && !arg1->isString) {
|
|
|
|
|
self->setField("target", a[1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Thread", "start", "()V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (!self) return true;
|
|
|
|
|
uint32_t runRef = self->getField("target", 'L').ref;
|
|
|
|
|
if (runRef == 0) runRef = a[0].ref;
|
|
|
|
|
JObject* runObj = v.getObj(runRef);
|
|
|
|
|
if (!runObj) return true;
|
|
|
|
|
JClass* cls = nullptr;
|
|
|
|
|
const JMethodInfo* m = nullptr;
|
|
|
|
|
if (v.resolveBytecodeMethod(runObj->className, "run",
|
|
|
|
|
"()V", &cls, &m)) {
|
|
|
|
|
JThread* nth = v.createThread(a[0].ref);
|
|
|
|
|
v.pushBytecodeFrame(*nth, cls, m,
|
|
|
|
|
{JVal::MakeRef(runRef)});
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Thread", "sleep", "(J)V",
|
|
|
|
|
[](Jvm&, JThread& th, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
int64_t ms = std::max<int64_t>(1, a[0].l);
|
|
|
|
|
th.state = ThreadState::Sleeping;
|
|
|
|
|
th.wakeTimeMs = nowMs() + ms;
|
|
|
|
|
th.yieldSlice = true;
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Thread", "yield", "()V",
|
|
|
|
|
[](Jvm&, JThread& th, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
th.yieldSlice = true;
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Thread", "currentThread",
|
|
|
|
|
"()Ljava/lang/Thread;",
|
|
|
|
|
[](Jvm& v, JThread& th, const std::vector<JVal>&, JVal* r) {
|
|
|
|
|
if (th.threadObjRef == 0) {
|
|
|
|
|
th.threadObjRef = v.allocObject("java/lang/Thread");
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeRef(th.threadObjRef);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Thread", "isAlive", "()Z",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
for (const auto& t : v.threads()) {
|
|
|
|
|
if (t->threadObjRef == a[0].ref &&
|
|
|
|
|
t->state != ThreadState::Finished) {
|
|
|
|
|
*r = JVal::MakeInt(1);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeInt(0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2026-10-28 15:25:07 +02:00
|
|
|
vm.registerNative("java/lang/Thread", "setPriority", "(I)V",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Thread", "getPriority", "()I",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
|
|
|
|
*r = JVal::MakeInt(5);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Thread", "activeCount", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
|
|
|
|
int cnt = 0;
|
|
|
|
|
for (const auto& t : v.threads()) {
|
|
|
|
|
if (t->state != ThreadState::Finished) cnt++;
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeInt(std::max(1, cnt));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Thread", "interrupt", "()V",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Thread", "join", "*",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2026-10-28 12:41:00 +02:00
|
|
|
|
|
|
|
|
// java/lang/Throwable
|
|
|
|
|
vm.registerNative("java/lang/Throwable", "<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->setField("detailMessage", a[1]);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Throwable", "getMessage",
|
|
|
|
|
"()Ljava/lang/String;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
const JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
*r = self ? self->getField("detailMessage", 'L')
|
|
|
|
|
: JVal::Null();
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/lang/Throwable", "printStackTrace", "()V",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void registerJavaUtil(Jvm& vm) {
|
|
|
|
|
// java/util/Random
|
|
|
|
|
vm.registerNative("java/util/Random", "<init>", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (!self) return true;
|
|
|
|
|
uint64_t seed = (a.size() >= 2)
|
|
|
|
|
? static_cast<uint64_t>(a[1].l)
|
|
|
|
|
: static_cast<uint64_t>(wallTimeMs());
|
|
|
|
|
self->nativeData = std::make_shared<std::mt19937_64>(seed);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/util/Random", "setSeed", "(J)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self) {
|
|
|
|
|
self->nativeData = std::make_shared<std::mt19937_64>(
|
|
|
|
|
static_cast<uint64_t>(a[1].l));
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/util/Random", "nextInt", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self && !self->nativeData) {
|
|
|
|
|
self->nativeData = std::make_shared<std::mt19937_64>(
|
|
|
|
|
static_cast<uint64_t>(wallTimeMs()));
|
|
|
|
|
}
|
|
|
|
|
auto* rng = self ? static_cast<std::mt19937_64*>(
|
|
|
|
|
self->nativeData.get())
|
|
|
|
|
: nullptr;
|
|
|
|
|
*r = JVal::MakeInt(
|
|
|
|
|
rng ? static_cast<int32_t>((*rng)()) : std::rand());
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/util/Random", "nextInt", "(I)I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
int32_t bound = a[1].i;
|
|
|
|
|
if (bound <= 0) {
|
|
|
|
|
*r = JVal::MakeInt(0);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self && !self->nativeData) {
|
|
|
|
|
self->nativeData = std::make_shared<std::mt19937_64>(
|
|
|
|
|
static_cast<uint64_t>(wallTimeMs()));
|
|
|
|
|
}
|
|
|
|
|
auto* rng = self ? static_cast<std::mt19937_64*>(
|
|
|
|
|
self->nativeData.get())
|
|
|
|
|
: nullptr;
|
|
|
|
|
uint32_t raw = rng ? static_cast<uint32_t>((*rng)())
|
|
|
|
|
: static_cast<uint32_t>(std::rand());
|
|
|
|
|
*r = JVal::MakeInt(
|
|
|
|
|
static_cast<int32_t>(raw % static_cast<uint32_t>(bound)));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/util/Random", "nextLong", "()J",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self && !self->nativeData) {
|
|
|
|
|
self->nativeData = std::make_shared<std::mt19937_64>(
|
|
|
|
|
static_cast<uint64_t>(wallTimeMs()));
|
|
|
|
|
}
|
|
|
|
|
auto* rng = self ? static_cast<std::mt19937_64*>(
|
|
|
|
|
self->nativeData.get())
|
|
|
|
|
: nullptr;
|
|
|
|
|
*r = JVal::MakeLong(
|
|
|
|
|
rng ? static_cast<int64_t>((*rng)()) : std::rand());
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// java/util/Vector & Stack (stored in JObject::refArr)
|
|
|
|
|
for (const char* vCls : {"java/util/Vector", "java/util/Stack"}) {
|
|
|
|
|
vm.registerNative(vCls, "<init>", "*",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "addElement", "(Ljava/lang/Object;)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self) self->refArr.push_back(a[1].ref);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "insertElementAt", "(Ljava/lang/Object;I)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self) {
|
|
|
|
|
int idx = std::clamp<int>(a[2].i, 0,
|
|
|
|
|
self->refArr.size());
|
|
|
|
|
self->refArr.insert(self->refArr.begin() + idx,
|
|
|
|
|
a[1].ref);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "setElementAt", "(Ljava/lang/Object;I)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self && a[2].i >= 0 &&
|
|
|
|
|
a[2].i < static_cast<int>(self->refArr.size())) {
|
|
|
|
|
self->refArr[a[2].i] = a[1].ref;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "elementAt", "(I)Ljava/lang/Object;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self && a[1].i >= 0 &&
|
|
|
|
|
a[1].i < static_cast<int>(self->refArr.size())) {
|
|
|
|
|
*r = JVal::MakeRef(self->refArr[a[1].i]);
|
|
|
|
|
} else {
|
|
|
|
|
*r = JVal::Null();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "firstElement", "()Ljava/lang/Object;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
*r = (self && !self->refArr.empty())
|
|
|
|
|
? JVal::MakeRef(self->refArr.front())
|
|
|
|
|
: JVal::Null();
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "lastElement", "()Ljava/lang/Object;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
*r = (self && !self->refArr.empty())
|
|
|
|
|
? JVal::MakeRef(self->refArr.back())
|
|
|
|
|
: JVal::Null();
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "removeElementAt", "(I)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self && a[1].i >= 0 &&
|
|
|
|
|
a[1].i < static_cast<int>(self->refArr.size())) {
|
|
|
|
|
self->refArr.erase(self->refArr.begin() + a[1].i);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "removeElement", "(Ljava/lang/Object;)Z",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self) {
|
|
|
|
|
auto it = std::find(self->refArr.begin(),
|
|
|
|
|
self->refArr.end(), a[1].ref);
|
|
|
|
|
if (it != self->refArr.end()) {
|
|
|
|
|
self->refArr.erase(it);
|
|
|
|
|
*r = JVal::MakeInt(1);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeInt(0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "removeAllElements", "()V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self) self->refArr.clear();
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "size", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
*r = JVal::MakeInt(
|
|
|
|
|
self ? static_cast<int32_t>(self->refArr.size())
|
|
|
|
|
: 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2026-10-28 15:25:07 +02:00
|
|
|
vm.registerNative(vCls, "setSize", "(I)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self) {
|
|
|
|
|
int n = std::max(0, a[1].i);
|
|
|
|
|
self->refArr.resize(static_cast<size_t>(n), 0);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "capacity", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
*r = JVal::MakeInt(
|
|
|
|
|
self ? static_cast<int32_t>(
|
|
|
|
|
std::max<size_t>(10, self->refArr.capacity()))
|
|
|
|
|
: 10);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "ensureCapacity", "(I)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self && a[1].i > 0) {
|
|
|
|
|
self->refArr.reserve(static_cast<size_t>(a[1].i));
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "trimToSize", "()V",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "indexOf", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
int startIdx = (a.size() >= 3) ? std::max(0, a[2].i) : 0;
|
|
|
|
|
if (self) {
|
|
|
|
|
for (int i = startIdx;
|
|
|
|
|
i < static_cast<int>(self->refArr.size()); ++i) {
|
|
|
|
|
if (self->refArr[i] == a[1].ref) {
|
|
|
|
|
*r = JVal::MakeInt(i);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeInt(-1);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "lastIndexOf", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self && !self->refArr.empty()) {
|
|
|
|
|
int startIdx =
|
|
|
|
|
(a.size() >= 3)
|
|
|
|
|
? std::min<int>(a[2].i,
|
|
|
|
|
self->refArr.size() - 1)
|
|
|
|
|
: static_cast<int>(self->refArr.size()) - 1;
|
|
|
|
|
for (int i = startIdx; i >= 0; --i) {
|
|
|
|
|
if (self->refArr[i] == a[1].ref) {
|
|
|
|
|
*r = JVal::MakeInt(i);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeInt(-1);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2026-10-28 12:41:00 +02:00
|
|
|
vm.registerNative(vCls, "isEmpty", "()Z",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
*r = JVal::MakeInt(
|
|
|
|
|
(!self || self->refArr.empty()) ? 1 : 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "contains", "(Ljava/lang/Object;)Z",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
bool found =
|
|
|
|
|
self && std::find(self->refArr.begin(),
|
|
|
|
|
self->refArr.end(),
|
|
|
|
|
a[1].ref) != self->refArr.end();
|
|
|
|
|
*r = JVal::MakeInt(found ? 1 : 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "copyInto", "([Ljava/lang/Object;)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
JObject* dst = v.getObj(a[1].ref);
|
|
|
|
|
if (self && dst && dst->arrayKind == ArrayKind::Ref) {
|
|
|
|
|
size_t n = std::min(self->refArr.size(),
|
|
|
|
|
dst->refArr.size());
|
|
|
|
|
for (size_t i = 0; i < n; ++i) {
|
|
|
|
|
dst->refArr[i] = self->refArr[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "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->refArr = self->refArr;
|
|
|
|
|
eo->nativeTag = 0;
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeRef(en);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "push", "(Ljava/lang/Object;)Ljava/lang/Object;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self) self->refArr.push_back(a[1].ref);
|
|
|
|
|
*r = a[1];
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "pop", "()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()) {
|
|
|
|
|
*r = JVal::MakeRef(self->refArr.back());
|
|
|
|
|
self->refArr.pop_back();
|
|
|
|
|
} else {
|
|
|
|
|
*r = JVal::Null();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "peek", "()Ljava/lang/Object;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
*r = (self && !self->refArr.empty())
|
|
|
|
|
? JVal::MakeRef(self->refArr.back())
|
|
|
|
|
: JVal::Null();
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(vCls, "empty", "()Z",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
*r = JVal::MakeInt(
|
|
|
|
|
(!self || self->refArr.empty()) ? 1 : 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// java/util/Enumeration
|
|
|
|
|
vm.registerNative("java/util/Enumeration", "hasMoreElements", "()Z",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
*r = JVal::MakeInt(
|
|
|
|
|
(self && self->nativeTag <
|
|
|
|
|
static_cast<int32_t>(
|
|
|
|
|
self->refArr.size()))
|
|
|
|
|
? 1
|
|
|
|
|
: 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/util/Enumeration", "nextElement",
|
|
|
|
|
"()Ljava/lang/Object;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self && self->nativeTag <
|
|
|
|
|
static_cast<int32_t>(
|
|
|
|
|
self->refArr.size())) {
|
|
|
|
|
*r = JVal::MakeRef(self->refArr[self->nativeTag++]);
|
|
|
|
|
} else {
|
|
|
|
|
*r = JVal::Null();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// java/util/Hashtable
|
|
|
|
|
vm.registerNative("java/util/Hashtable", "<init>", "*",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"java/util/Hashtable", "put",
|
|
|
|
|
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
std::string k = v.getStringUtf8(a[1].ref);
|
|
|
|
|
if (k.empty()) k = "#" + std::to_string(a[1].ref);
|
|
|
|
|
*r = self ? self->getField(k, 'L') : JVal::Null();
|
|
|
|
|
if (self) self->setField(k, a[2]);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"java/util/Hashtable", "get",
|
|
|
|
|
"(Ljava/lang/Object;)Ljava/lang/Object;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
std::string k = v.getStringUtf8(a[1].ref);
|
|
|
|
|
if (k.empty()) k = "#" + std::to_string(a[1].ref);
|
|
|
|
|
*r = self ? self->getField(k, 'L') : JVal::Null();
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"java/util/Hashtable", "containsKey", "(Ljava/lang/Object;)Z",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
std::string k = v.getStringUtf8(a[1].ref);
|
|
|
|
|
if (k.empty()) k = "#" + std::to_string(a[1].ref);
|
|
|
|
|
*r = JVal::MakeInt(
|
|
|
|
|
(self && self->fields.find(k) != self->fields.end()) ? 1 : 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/util/Hashtable", "remove",
|
|
|
|
|
"(Ljava/lang/Object;)Ljava/lang/Object;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
std::string k = v.getStringUtf8(a[1].ref);
|
|
|
|
|
if (k.empty()) k = "#" + std::to_string(a[1].ref);
|
|
|
|
|
*r = self ? self->getField(k, 'L') : JVal::Null();
|
|
|
|
|
if (self) self->fields.erase(k);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
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();
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/util/Hashtable", "size", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
*r = JVal::MakeInt(
|
|
|
|
|
self ? static_cast<int32_t>(self->fields.size())
|
|
|
|
|
: 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// java/util/Timer & TimerTask
|
|
|
|
|
vm.registerNative("java/util/Timer", "<init>", "*",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/util/Timer", "schedule", "*",
|
|
|
|
|
[this](Jvm&, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
if (a.size() >= 3) {
|
|
|
|
|
ScheduledTimerTask st;
|
|
|
|
|
st.taskObjRef = a[1].ref;
|
|
|
|
|
st.nextRunMs = nowMs() + std::max<int64_t>(0, a[2].l);
|
|
|
|
|
st.periodMs = (a.size() >= 4) ? a[3].l : 0;
|
|
|
|
|
timerTasks.push_back(st);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/util/Timer", "scheduleAtFixedRate", "*",
|
|
|
|
|
[this](Jvm&, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
if (a.size() >= 4) {
|
|
|
|
|
ScheduledTimerTask st;
|
|
|
|
|
st.taskObjRef = a[1].ref;
|
|
|
|
|
st.nextRunMs = nowMs() + std::max<int64_t>(0, a[2].l);
|
|
|
|
|
st.periodMs = std::max<int64_t>(1, a[3].l);
|
|
|
|
|
timerTasks.push_back(st);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/util/Timer", "cancel", "()V",
|
|
|
|
|
[this](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
timerTasks.clear();
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/util/TimerTask", "<init>", "()V",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative("java/util/TimerTask", "cancel", "()Z",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self) self->setField("cancelled", JVal::MakeInt(1));
|
|
|
|
|
*r = JVal::MakeInt(1);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void registerJavaIo(Jvm& vm) {
|
|
|
|
|
// ByteArrayInputStream & DataInputStream & InputStream
|
|
|
|
|
for (const char* isCls :
|
|
|
|
|
{"java/io/InputStream", "java/io/ByteArrayInputStream",
|
|
|
|
|
"java/io/DataInputStream"}) {
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
isCls, "<init>", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
if (a.size() >= 2 && a[1].type == JType::Ref) {
|
|
|
|
|
JObject* src = v.getObj(a[1].ref);
|
|
|
|
|
if (src && src->arrayKind == ArrayKind::ByteOrBool) {
|
|
|
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
|
|
|
|
int off = (a.size() >= 4) ? a[2].i : 0;
|
|
|
|
|
int len = (a.size() >= 4)
|
|
|
|
|
? a[3].i
|
|
|
|
|
: static_cast<int>(src->byteArr.size());
|
|
|
|
|
off = std::clamp(off, 0, static_cast<int>(src->byteArr.size()));
|
|
|
|
|
len = std::clamp(len, 0,
|
|
|
|
|
static_cast<int>(src->byteArr.size()) - off);
|
|
|
|
|
nis->data.assign(
|
|
|
|
|
reinterpret_cast<const uint8_t*>(src->byteArr.data() + off),
|
|
|
|
|
reinterpret_cast<const uint8_t*>(src->byteArr.data() + off +
|
|
|
|
|
len));
|
|
|
|
|
} else if (src && src->nativeData) {
|
|
|
|
|
v.getObj(a[0].ref)->nativeData = src->nativeData;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(isCls, "read", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
|
|
|
|
*r = JVal::MakeInt(nis ? nis->readByte() : -1);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
isCls, "read", "([B)I",
|
|
|
|
|
[](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::ByteOrBool) {
|
|
|
|
|
*r = JVal::MakeInt(-1);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
int n = nis->readBytes(
|
|
|
|
|
reinterpret_cast<uint8_t*>(arr->byteArr.data()),
|
|
|
|
|
static_cast<int>(arr->byteArr.size()));
|
|
|
|
|
*r = JVal::MakeInt(n);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
isCls, "read", "([BII)I",
|
|
|
|
|
[](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::ByteOrBool ||
|
|
|
|
|
off < 0 || len < 0 ||
|
|
|
|
|
off + len > static_cast<int>(arr->byteArr.size())) {
|
|
|
|
|
*r = JVal::MakeInt(-1);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
int n = nis->readBytes(
|
|
|
|
|
reinterpret_cast<uint8_t*>(arr->byteArr.data() + off), len);
|
|
|
|
|
*r = JVal::MakeInt(n);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
isCls, "readFully", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
|
|
|
|
JObject* arr = v.getObj(a[1].ref);
|
|
|
|
|
if (!nis || !arr || arr->arrayKind != ArrayKind::ByteOrBool) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
int off = (a.size() >= 4) ? a[2].i : 0;
|
|
|
|
|
int len = (a.size() >= 4) ? a[3].i
|
|
|
|
|
: static_cast<int>(arr->byteArr.size());
|
|
|
|
|
off = std::clamp(off, 0, static_cast<int>(arr->byteArr.size()));
|
|
|
|
|
len = std::clamp(len, 0,
|
|
|
|
|
static_cast<int>(arr->byteArr.size()) - off);
|
|
|
|
|
nis->readBytes(
|
|
|
|
|
reinterpret_cast<uint8_t*>(arr->byteArr.data() + off), len);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(isCls, "available", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
|
|
|
|
*r = JVal::MakeInt(nis ? nis->available() : 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(isCls, "skip", "(J)J",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
|
|
|
|
if (!nis) {
|
|
|
|
|
*r = JVal::MakeLong(0);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
size_t avail = (nis->data.size() > nis->pos)
|
|
|
|
|
? (nis->data.size() - nis->pos)
|
|
|
|
|
: 0;
|
|
|
|
|
size_t sk = std::min<size_t>(
|
|
|
|
|
avail, static_cast<size_t>(std::max<int64_t>(0, a[1].l)));
|
|
|
|
|
nis->pos += sk;
|
|
|
|
|
*r = JVal::MakeLong(static_cast<int64_t>(sk));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(isCls, "skipBytes", "(I)I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
|
|
|
|
if (!nis) {
|
|
|
|
|
*r = JVal::MakeInt(0);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
size_t avail = (nis->data.size() > nis->pos)
|
|
|
|
|
? (nis->data.size() - nis->pos)
|
|
|
|
|
: 0;
|
|
|
|
|
size_t sk = std::min<size_t>(
|
|
|
|
|
avail, static_cast<size_t>(std::max(0, a[1].i)));
|
|
|
|
|
nis->pos += sk;
|
|
|
|
|
*r = JVal::MakeInt(static_cast<int32_t>(sk));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(isCls, "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(isCls, "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(isCls, "close", "()V",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(isCls, "readBoolean", "()Z",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
|
|
|
|
*r = JVal::MakeInt((nis && nis->readByte() > 0) ? 1 : 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(isCls, "readByte", "()B",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
|
|
|
|
*r = JVal::MakeInt(
|
|
|
|
|
static_cast<int8_t>(nis ? nis->readByte() : 0));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(isCls, "readUnsignedByte", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
|
|
|
|
int b = nis ? nis->readByte() : 0;
|
|
|
|
|
*r = JVal::MakeInt(b < 0 ? 0 : (b & 0xFF));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(isCls, "readShort", "()S",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
|
|
|
|
int b1 = nis ? (nis->readByte() & 0xFF) : 0;
|
|
|
|
|
int b2 = nis ? (nis->readByte() & 0xFF) : 0;
|
|
|
|
|
*r = JVal::MakeInt(
|
|
|
|
|
static_cast<int16_t>((b1 << 8) | b2));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(isCls, "readUnsignedShort", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
|
|
|
|
int b1 = nis ? (nis->readByte() & 0xFF) : 0;
|
|
|
|
|
int b2 = nis ? (nis->readByte() & 0xFF) : 0;
|
|
|
|
|
*r = JVal::MakeInt((b1 << 8) | b2);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(isCls, "readChar", "()C",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
|
|
|
|
int b1 = nis ? (nis->readByte() & 0xFF) : 0;
|
|
|
|
|
int b2 = nis ? (nis->readByte() & 0xFF) : 0;
|
|
|
|
|
*r = JVal::MakeInt((b1 << 8) | b2);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(isCls, "readInt", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
|
|
|
|
uint32_t x = 0;
|
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
|
x = (x << 8) |
|
|
|
|
|
static_cast<uint32_t>(
|
|
|
|
|
nis ? (nis->readByte() & 0xFF) : 0);
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeInt(static_cast<int32_t>(x));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(isCls, "readLong", "()J",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
|
|
|
|
uint64_t x = 0;
|
|
|
|
|
for (int i = 0; i < 8; ++i) {
|
|
|
|
|
x = (x << 8) |
|
|
|
|
|
static_cast<uint64_t>(
|
|
|
|
|
nis ? (nis->readByte() & 0xFF) : 0);
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeLong(static_cast<int64_t>(x));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(isCls, "readFloat", "()F",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
|
|
|
|
uint32_t x = 0;
|
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
|
x = (x << 8) |
|
|
|
|
|
static_cast<uint32_t>(
|
|
|
|
|
nis ? (nis->readByte() & 0xFF) : 0);
|
|
|
|
|
}
|
|
|
|
|
float fval = 0.0f;
|
|
|
|
|
std::memcpy(&fval, &x, sizeof(float));
|
|
|
|
|
*r = JVal::MakeFloat(fval);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
isCls, "readUTF", "()Ljava/lang/String;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto nis = getOrCreateInputStream(v, a[0].ref);
|
|
|
|
|
int b1 = nis ? (nis->readByte() & 0xFF) : 0;
|
|
|
|
|
int b2 = nis ? (nis->readByte() & 0xFF) : 0;
|
|
|
|
|
int len = (b1 << 8) | b2;
|
|
|
|
|
std::string s(len, '\0');
|
|
|
|
|
if (nis && len > 0) {
|
|
|
|
|
nis->readBytes(reinterpret_cast<uint8_t*>(s.data()), len);
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(s));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OutputStream, ByteArrayOutputStream, DataOutputStream, PrintStream
|
|
|
|
|
for (const char* osCls :
|
|
|
|
|
{"java/io/OutputStream", "java/io/ByteArrayOutputStream",
|
|
|
|
|
"java/io/DataOutputStream", "java/io/PrintStream"}) {
|
|
|
|
|
vm.registerNative(osCls, "<init>", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
if (a.size() >= 2 && a[1].type == JType::Ref) {
|
|
|
|
|
JObject* sub = v.getObj(a[1].ref);
|
|
|
|
|
if (sub && sub->nativeData) {
|
|
|
|
|
v.getObj(a[0].ref)->nativeData = sub->nativeData;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(osCls, "write", "(I)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
|
|
|
|
if (nos) nos->writeByte(static_cast<uint8_t>(a[1].i));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
osCls, "write", "([B)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
|
|
|
|
JObject* arr = v.getObj(a[1].ref);
|
|
|
|
|
if (nos && arr && arr->arrayKind == ArrayKind::ByteOrBool) {
|
|
|
|
|
nos->writeBytes(
|
|
|
|
|
reinterpret_cast<const uint8_t*>(arr->byteArr.data()),
|
|
|
|
|
static_cast<int>(arr->byteArr.size()));
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
osCls, "write", "([BII)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
|
|
|
|
JObject* arr = v.getObj(a[1].ref);
|
|
|
|
|
int off = a[2].i;
|
|
|
|
|
int len = a[3].i;
|
|
|
|
|
if (nos && arr && arr->arrayKind == ArrayKind::ByteOrBool &&
|
|
|
|
|
off >= 0 && len > 0 &&
|
|
|
|
|
off + len <= static_cast<int>(arr->byteArr.size())) {
|
|
|
|
|
nos->writeBytes(
|
|
|
|
|
reinterpret_cast<const uint8_t*>(arr->byteArr.data() + off),
|
|
|
|
|
len);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
osCls, "toByteArray", "()[B",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
|
|
|
|
int32_t sz = nos ? static_cast<int32_t>(nos->data.size()) : 0;
|
|
|
|
|
uint32_t arr = v.allocPrimitiveArray(8, sz);
|
|
|
|
|
if (nos && sz > 0) {
|
|
|
|
|
std::memcpy(v.getObj(arr)->byteArr.data(), nos->data.data(), sz);
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeRef(arr);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(osCls, "size", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
|
|
|
|
*r = JVal::MakeInt(
|
|
|
|
|
nos ? static_cast<int32_t>(nos->data.size()) : 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(osCls, "reset", "()V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
|
|
|
|
if (nos) nos->data.clear();
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(osCls, "flush", "()V",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(osCls, "close", "()V",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(osCls, "print", "*",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(osCls, "println", "*",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(osCls, "writeBoolean", "(Z)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
|
|
|
|
if (nos) nos->writeByte(a[1].i != 0 ? 1 : 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(osCls, "writeByte", "(I)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
|
|
|
|
if (nos) nos->writeByte(static_cast<uint8_t>(a[1].i));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(osCls, "writeShort", "(I)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
|
|
|
|
if (nos) {
|
|
|
|
|
nos->writeByte(static_cast<uint8_t>(a[1].i >> 8));
|
|
|
|
|
nos->writeByte(static_cast<uint8_t>(a[1].i));
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(osCls, "writeChar", "(I)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
|
|
|
|
if (nos) {
|
|
|
|
|
nos->writeByte(static_cast<uint8_t>(a[1].i >> 8));
|
|
|
|
|
nos->writeByte(static_cast<uint8_t>(a[1].i));
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(osCls, "writeInt", "(I)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
|
|
|
|
if (nos) {
|
|
|
|
|
uint32_t x = static_cast<uint32_t>(a[1].i);
|
|
|
|
|
nos->writeByte(static_cast<uint8_t>(x >> 24));
|
|
|
|
|
nos->writeByte(static_cast<uint8_t>(x >> 16));
|
|
|
|
|
nos->writeByte(static_cast<uint8_t>(x >> 8));
|
|
|
|
|
nos->writeByte(static_cast<uint8_t>(x));
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(osCls, "writeLong", "(J)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
|
|
|
|
if (nos) {
|
|
|
|
|
uint64_t x = static_cast<uint64_t>(a[1].l);
|
|
|
|
|
for (int i = 7; i >= 0; --i) {
|
|
|
|
|
nos->writeByte(
|
|
|
|
|
static_cast<uint8_t>((x >> (i * 8)) & 0xFF));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
osCls, "writeUTF", "(Ljava/lang/String;)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
|
|
|
|
std::string s = v.getStringUtf8(a[1].ref);
|
|
|
|
|
if (nos) {
|
|
|
|
|
uint16_t len = static_cast<uint16_t>(
|
|
|
|
|
std::min<size_t>(s.size(), 65535));
|
|
|
|
|
nos->writeByte(static_cast<uint8_t>(len >> 8));
|
|
|
|
|
nos->writeByte(static_cast<uint8_t>(len));
|
|
|
|
|
nos->writeBytes(reinterpret_cast<const uint8_t*>(s.data()), len);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void registerRms(Jvm& vm) {
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/rms/RecordStore", "openRecordStore", "*",
|
|
|
|
|
[](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
std::string name = v.getStringUtf8(a[0].ref);
|
|
|
|
|
bool create = (a.size() >= 2) ? (a[1].i != 0) : true;
|
|
|
|
|
if (!v.rms) {
|
|
|
|
|
v.throwException(th, "javax/microedition/rms/RecordStoreException");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
RmsStoreData* sd = v.rms->openStore(name, create);
|
|
|
|
|
if (!sd) {
|
|
|
|
|
v.throwException(
|
|
|
|
|
th, "javax/microedition/rms/RecordStoreNotFoundException",
|
|
|
|
|
name);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
uint32_t ref = v.allocObject("javax/microedition/rms/RecordStore");
|
|
|
|
|
v.getObj(ref)->strUtf8 = name;
|
|
|
|
|
*r = JVal::MakeRef(ref);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/rms/RecordStore", "closeRecordStore", "()V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (self && v.rms) v.rms->closeStore(self->strUtf8);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/rms/RecordStore", "deleteRecordStore",
|
|
|
|
|
"(Ljava/lang/String;)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
if (v.rms) v.rms->deleteStore(v.getStringUtf8(a[0].ref));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/rms/RecordStore", "listRecordStores",
|
|
|
|
|
"()[Ljava/lang/String;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
|
|
|
|
if (!v.rms) {
|
|
|
|
|
*r = JVal::Null();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
auto names = v.rms->listStores();
|
|
|
|
|
uint32_t arr = v.allocRefArray(
|
|
|
|
|
"java/lang/String", static_cast<int32_t>(names.size()));
|
|
|
|
|
for (size_t i = 0; i < names.size(); ++i) {
|
|
|
|
|
v.getObj(arr)->refArr[i] = v.allocString(names[i]);
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeRef(arr);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/rms/RecordStore", "getNumRecords", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
RmsStoreData* sd =
|
|
|
|
|
(self && v.rms) ? v.rms->openStore(self->strUtf8, true) : nullptr;
|
|
|
|
|
if (sd) v.rms->closeStore(self->strUtf8);
|
|
|
|
|
*r = JVal::MakeInt(
|
|
|
|
|
sd ? static_cast<int32_t>(sd->records.size()) : 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/rms/RecordStore", "getNextRecordID", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
RmsStoreData* sd =
|
|
|
|
|
(self && v.rms) ? v.rms->openStore(self->strUtf8, true) : nullptr;
|
|
|
|
|
if (sd) v.rms->closeStore(self->strUtf8);
|
|
|
|
|
*r = JVal::MakeInt(sd ? sd->nextRecordId : 1);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/rms/RecordStore", "getSize", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
RmsStoreData* sd =
|
|
|
|
|
(self && v.rms) ? v.rms->openStore(self->strUtf8, true) : nullptr;
|
|
|
|
|
if (sd) v.rms->closeStore(self->strUtf8);
|
|
|
|
|
*r = JVal::MakeInt((sd && v.rms) ? v.rms->getSize(sd) : 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/rms/RecordStore", "getSizeAvailable", "()I",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
|
|
|
|
*r = JVal::MakeInt(4 * 1024 * 1024);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/rms/RecordStore", "addRecord", "([BII)I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
RmsStoreData* sd =
|
|
|
|
|
(self && v.rms) ? v.rms->openStore(self->strUtf8, true) : nullptr;
|
|
|
|
|
const uint8_t* ptr = nullptr;
|
|
|
|
|
int len = 0;
|
|
|
|
|
if (a[1].ref != 0) {
|
|
|
|
|
JObject* arr = v.getObj(a[1].ref);
|
|
|
|
|
if (arr && arr->arrayKind == ArrayKind::ByteOrBool) {
|
|
|
|
|
int off = std::clamp(
|
|
|
|
|
a[2].i, 0, static_cast<int>(arr->byteArr.size()));
|
|
|
|
|
len = std::clamp(
|
|
|
|
|
a[3].i, 0, static_cast<int>(arr->byteArr.size()) - off);
|
|
|
|
|
ptr = reinterpret_cast<const uint8_t*>(arr->byteArr.data() + off);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int32_t id = (sd && v.rms) ? v.rms->addRecord(sd, ptr, len) : 1;
|
|
|
|
|
if (sd) v.rms->closeStore(self->strUtf8);
|
|
|
|
|
*r = JVal::MakeInt(id);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/rms/RecordStore", "setRecord", "(I[BII)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
RmsStoreData* sd =
|
|
|
|
|
(self && v.rms) ? v.rms->openStore(self->strUtf8, true) : nullptr;
|
|
|
|
|
const uint8_t* ptr = nullptr;
|
|
|
|
|
int len = 0;
|
|
|
|
|
if (a[2].ref != 0) {
|
|
|
|
|
JObject* arr = v.getObj(a[2].ref);
|
|
|
|
|
if (arr && arr->arrayKind == ArrayKind::ByteOrBool) {
|
|
|
|
|
int off = std::clamp(
|
|
|
|
|
a[3].i, 0, static_cast<int>(arr->byteArr.size()));
|
|
|
|
|
len = std::clamp(
|
|
|
|
|
a[4].i, 0, static_cast<int>(arr->byteArr.size()) - off);
|
|
|
|
|
ptr = reinterpret_cast<const uint8_t*>(arr->byteArr.data() + off);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (sd && v.rms) {
|
|
|
|
|
v.rms->setRecord(sd, a[1].i, ptr, len);
|
|
|
|
|
v.rms->closeStore(self->strUtf8);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/rms/RecordStore", "getRecord", "(I)[B",
|
|
|
|
|
[](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
RmsStoreData* sd =
|
|
|
|
|
(self && v.rms) ? v.rms->openStore(self->strUtf8, true) : nullptr;
|
|
|
|
|
std::vector<uint8_t> rec;
|
|
|
|
|
bool ok = sd && v.rms && v.rms->getRecord(sd, a[1].i, &rec);
|
|
|
|
|
if (sd) v.rms->closeStore(self->strUtf8);
|
|
|
|
|
if (!ok) {
|
|
|
|
|
v.throwException(
|
|
|
|
|
th, "javax/microedition/rms/InvalidRecordIDException");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
uint32_t arr = v.allocPrimitiveArray(
|
|
|
|
|
8, static_cast<int32_t>(rec.size()));
|
|
|
|
|
if (!rec.empty()) {
|
|
|
|
|
std::memcpy(v.getObj(arr)->byteArr.data(), rec.data(), rec.size());
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeRef(arr);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/rms/RecordStore", "getRecord", "(I[BI)I",
|
|
|
|
|
[](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
RmsStoreData* sd =
|
|
|
|
|
(self && v.rms) ? v.rms->openStore(self->strUtf8, true) : nullptr;
|
|
|
|
|
std::vector<uint8_t> rec;
|
|
|
|
|
bool ok = sd && v.rms && v.rms->getRecord(sd, a[1].i, &rec);
|
|
|
|
|
if (sd) v.rms->closeStore(self->strUtf8);
|
|
|
|
|
if (!ok) {
|
|
|
|
|
v.throwException(
|
|
|
|
|
th, "javax/microedition/rms/InvalidRecordIDException");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
JObject* dst = v.getObj(a[2].ref);
|
|
|
|
|
int off = a[3].i;
|
|
|
|
|
if (dst && dst->arrayKind == ArrayKind::ByteOrBool && off >= 0 &&
|
|
|
|
|
off + rec.size() <= dst->byteArr.size()) {
|
|
|
|
|
std::memcpy(dst->byteArr.data() + off, rec.data(), rec.size());
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeInt(static_cast<int32_t>(rec.size()));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/rms/RecordStore", "getRecordSize", "(I)I",
|
|
|
|
|
[](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
RmsStoreData* sd =
|
|
|
|
|
(self && v.rms) ? v.rms->openStore(self->strUtf8, true) : nullptr;
|
|
|
|
|
int32_t sz = (sd && v.rms) ? v.rms->getRecordSize(sd, a[1].i) : -1;
|
|
|
|
|
if (sd) v.rms->closeStore(self->strUtf8);
|
|
|
|
|
if (sz < 0) {
|
|
|
|
|
v.throwException(
|
|
|
|
|
th, "javax/microedition/rms/InvalidRecordIDException");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeInt(sz);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/rms/RecordStore", "deleteRecord", "(I)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
RmsStoreData* sd =
|
|
|
|
|
(self && v.rms) ? v.rms->openStore(self->strUtf8, true) : nullptr;
|
|
|
|
|
if (sd && v.rms) {
|
|
|
|
|
v.rms->deleteRecord(sd, a[1].i);
|
|
|
|
|
v.rms->closeStore(self->strUtf8);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/rms/RecordStore", "enumerateRecords", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
RmsStoreData* sd =
|
|
|
|
|
(self && v.rms) ? v.rms->openStore(self->strUtf8, true) : nullptr;
|
|
|
|
|
uint32_t en = v.allocObject(
|
|
|
|
|
"javax/microedition/rms/RecordEnumeration");
|
|
|
|
|
JObject* eo = v.getObj(en);
|
|
|
|
|
if (sd && eo) {
|
|
|
|
|
eo->strUtf8 = self->strUtf8;
|
|
|
|
|
eo->nativeTag = 0;
|
|
|
|
|
for (const auto& kv : sd->records) {
|
|
|
|
|
eo->intArr.push_back(kv.first);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (sd) v.rms->closeStore(self->strUtf8);
|
|
|
|
|
*r = JVal::MakeRef(en);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/rms/RecordEnumeration", "numRecords", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
*r = JVal::MakeInt(
|
|
|
|
|
self ? static_cast<int32_t>(self->intArr.size()) : 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/rms/RecordEnumeration", "hasNextElement", "()Z",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
*r = JVal::MakeInt(
|
|
|
|
|
(self && self->nativeTag <
|
|
|
|
|
static_cast<int32_t>(self->intArr.size()))
|
|
|
|
|
? 1
|
|
|
|
|
: 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/rms/RecordEnumeration", "nextRecordId", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
int32_t id =
|
|
|
|
|
(self && self->nativeTag <
|
|
|
|
|
static_cast<int32_t>(self->intArr.size()))
|
|
|
|
|
? self->intArr[self->nativeTag++]
|
|
|
|
|
: 1;
|
|
|
|
|
*r = JVal::MakeInt(id);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/rms/RecordEnumeration", "nextRecord", "()[B",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
if (!self ||
|
|
|
|
|
self->nativeTag >= static_cast<int32_t>(self->intArr.size()) ||
|
|
|
|
|
!v.rms) {
|
|
|
|
|
*r = JVal::Null();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
int32_t id = self->intArr[self->nativeTag++];
|
|
|
|
|
RmsStoreData* sd = v.rms->openStore(self->strUtf8, true);
|
|
|
|
|
std::vector<uint8_t> rec;
|
|
|
|
|
if (sd) {
|
|
|
|
|
v.rms->getRecord(sd, id, &rec);
|
|
|
|
|
v.rms->closeStore(self->strUtf8);
|
|
|
|
|
}
|
|
|
|
|
uint32_t arr = v.allocPrimitiveArray(
|
|
|
|
|
8, static_cast<int32_t>(rec.size()));
|
|
|
|
|
if (!rec.empty()) {
|
|
|
|
|
std::memcpy(v.getObj(arr)->byteArr.data(), rec.data(), rec.size());
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeRef(arr);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/rms/RecordEnumeration", "destroy", "()V",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void executeHttpIfNeeded(NativeConnection* nc) {
|
|
|
|
|
if (!nc || nc->kind != NativeConnection::Http || nc->executed) return;
|
|
|
|
|
nc->executed = true;
|
|
|
|
|
nc->inStream = std::make_shared<NativeInputStream>();
|
|
|
|
|
|
|
|
|
|
CURL* curl = curl_easy_init();
|
|
|
|
|
if (!curl) {
|
|
|
|
|
nc->responseCode = 500;
|
|
|
|
|
nc->responseMessage = "CURL Error";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, nc->url.c_str());
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15L);
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8L);
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
|
|
|
|
curl_easy_setopt(
|
|
|
|
|
curl, CURLOPT_USERAGENT,
|
|
|
|
|
"Nokia8110-4G/2.0 (Bananarchy; ReMEdy/1.0) Profile/MIDP-2.0 Configuration/CLDC-1.1");
|
|
|
|
|
|
|
|
|
|
struct curl_slist* headers = nullptr;
|
|
|
|
|
for (const auto& kv : nc->reqHeaders) {
|
|
|
|
|
std::string h = kv.first + ": " + kv.second;
|
|
|
|
|
headers = curl_slist_append(headers, h.c_str());
|
|
|
|
|
}
|
|
|
|
|
if (headers) curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
|
|
|
|
|
|
|
|
|
if (nc->method == "POST") {
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
|
|
|
|
if (nc->outStream && !nc->outStream->data.empty()) {
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,
|
|
|
|
|
nc->outStream->data.data());
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,
|
|
|
|
|
static_cast<long>(nc->outStream->data.size()));
|
|
|
|
|
} else {
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0L);
|
|
|
|
|
}
|
|
|
|
|
} else if (nc->method == "HEAD") {
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteVector);
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &nc->inStream->data);
|
|
|
|
|
|
|
|
|
|
CURLcode res = curl_easy_perform(curl);
|
|
|
|
|
long code = 200;
|
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
|
|
|
|
|
if (headers) curl_slist_free_all(headers);
|
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
|
|
|
|
if (res != CURLE_OK) {
|
|
|
|
|
nc->responseCode = 503;
|
|
|
|
|
nc->responseMessage = "Network Unreachable";
|
|
|
|
|
} else {
|
|
|
|
|
nc->responseCode = static_cast<int>(code > 0 ? code : 200);
|
|
|
|
|
nc->responseMessage = (nc->responseCode == 200) ? "OK" : "HTTP Response";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void registerGcfNetwork(Jvm& vm) {
|
|
|
|
|
auto openConn = [this](Jvm& v, JThread& th, const std::string& url) -> uint32_t {
|
|
|
|
|
if (!networkEnabled && url.rfind("file://", 0) != 0) {
|
|
|
|
|
v.throwException(th, "java/io/IOException", "Network disabled in ReMEdy");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
auto nc = std::make_shared<NativeConnection>();
|
|
|
|
|
nc->url = url;
|
|
|
|
|
nc->inStream = std::make_shared<NativeInputStream>();
|
|
|
|
|
nc->outStream = std::make_shared<NativeOutputStream>();
|
|
|
|
|
|
|
|
|
|
if (url.rfind("socket://", 0) == 0 || url.rfind("ssl://", 0) == 0 ||
|
|
|
|
|
url.rfind("tls://", 0) == 0) {
|
|
|
|
|
nc->kind = NativeConnection::Socket;
|
|
|
|
|
size_t schemeEnd = url.find("://") + 3;
|
|
|
|
|
std::string hostPort = url.substr(schemeEnd);
|
|
|
|
|
size_t colon = hostPort.rfind(':');
|
|
|
|
|
nc->host = (colon == std::string::npos) ? hostPort
|
|
|
|
|
: hostPort.substr(0, colon);
|
|
|
|
|
nc->port = (colon == std::string::npos)
|
|
|
|
|
? 80
|
|
|
|
|
: std::atoi(hostPort.substr(colon + 1).c_str());
|
|
|
|
|
if (nc->host.empty()) nc->host = "127.0.0.1";
|
|
|
|
|
|
|
|
|
|
struct addrinfo hints {}, *res = nullptr;
|
|
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
std::string portStr = std::to_string(nc->port);
|
|
|
|
|
if (::getaddrinfo(nc->host.c_str(), portStr.c_str(), &hints, &res) == 0 &&
|
|
|
|
|
res) {
|
|
|
|
|
int fd = ::socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
|
|
|
|
if (fd >= 0) {
|
|
|
|
|
struct timeval tv {4, 0};
|
|
|
|
|
::setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
|
|
|
|
::setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
|
|
|
|
|
if (::connect(fd, res->ai_addr, res->ai_addrlen) == 0) {
|
|
|
|
|
nc->sockFd = fd;
|
|
|
|
|
nc->inStream->sockFd = fd;
|
|
|
|
|
nc->outStream->sockFd = fd;
|
|
|
|
|
} else {
|
|
|
|
|
::close(fd);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
::freeaddrinfo(res);
|
|
|
|
|
}
|
|
|
|
|
uint32_t ref = v.allocObject("javax/microedition/io/SocketConnection");
|
|
|
|
|
v.getObj(ref)->nativeData = nc;
|
|
|
|
|
return ref;
|
|
|
|
|
} else if (url.rfind("http://", 0) == 0 ||
|
|
|
|
|
url.rfind("https://", 0) == 0) {
|
|
|
|
|
nc->kind = NativeConnection::Http;
|
|
|
|
|
uint32_t ref = v.allocObject("javax/microedition/io/HttpConnection");
|
|
|
|
|
v.getObj(ref)->nativeData = nc;
|
|
|
|
|
return ref;
|
|
|
|
|
} else {
|
|
|
|
|
nc->kind = NativeConnection::File;
|
|
|
|
|
uint32_t ref = v.allocObject(
|
|
|
|
|
"javax/microedition/io/file/FileConnection");
|
|
|
|
|
v.getObj(ref)->nativeData = nc;
|
|
|
|
|
return ref;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/io/Connector", "open", "*",
|
|
|
|
|
[openConn](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
std::string url = v.getStringUtf8(a[0].ref);
|
|
|
|
|
uint32_t cref = openConn(v, th, url);
|
|
|
|
|
if (cref == 0) return false;
|
|
|
|
|
*r = JVal::MakeRef(cref);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/io/Connector", "openInputStream",
|
|
|
|
|
"(Ljava/lang/String;)Ljava/io/InputStream;",
|
|
|
|
|
[openConn](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
uint32_t cref = openConn(v, th, v.getStringUtf8(a[0].ref));
|
|
|
|
|
if (cref == 0) return false;
|
|
|
|
|
auto* nc = static_cast<NativeConnection*>(
|
|
|
|
|
v.getObj(cref)->nativeData.get());
|
|
|
|
|
executeHttpIfNeeded(nc);
|
|
|
|
|
uint32_t isRef = v.allocObject("java/io/DataInputStream");
|
|
|
|
|
v.getObj(isRef)->nativeData = nc->inStream;
|
|
|
|
|
*r = JVal::MakeRef(isRef);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/io/Connector", "openDataInputStream",
|
|
|
|
|
"(Ljava/lang/String;)Ljava/io/DataInputStream;",
|
|
|
|
|
[openConn](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
uint32_t cref = openConn(v, th, v.getStringUtf8(a[0].ref));
|
|
|
|
|
if (cref == 0) return false;
|
|
|
|
|
auto* nc = static_cast<NativeConnection*>(
|
|
|
|
|
v.getObj(cref)->nativeData.get());
|
|
|
|
|
executeHttpIfNeeded(nc);
|
|
|
|
|
uint32_t isRef = v.allocObject("java/io/DataInputStream");
|
|
|
|
|
v.getObj(isRef)->nativeData = nc->inStream;
|
|
|
|
|
*r = JVal::MakeRef(isRef);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/io/Connector", "openOutputStream",
|
|
|
|
|
"(Ljava/lang/String;)Ljava/io/OutputStream;",
|
|
|
|
|
[openConn](Jvm& v, JThread& th, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
uint32_t cref = openConn(v, th, v.getStringUtf8(a[0].ref));
|
|
|
|
|
if (cref == 0) return false;
|
|
|
|
|
auto* nc = static_cast<NativeConnection*>(
|
|
|
|
|
v.getObj(cref)->nativeData.get());
|
|
|
|
|
uint32_t osRef = v.allocObject("java/io/DataOutputStream");
|
|
|
|
|
v.getObj(osRef)->nativeData = nc->outStream;
|
|
|
|
|
*r = JVal::MakeRef(osRef);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (const char* connCls :
|
|
|
|
|
{"javax/microedition/io/Connection",
|
|
|
|
|
"javax/microedition/io/InputConnection",
|
|
|
|
|
"javax/microedition/io/OutputConnection",
|
|
|
|
|
"javax/microedition/io/StreamConnection",
|
|
|
|
|
"javax/microedition/io/ContentConnection",
|
|
|
|
|
"javax/microedition/io/SocketConnection",
|
|
|
|
|
"javax/microedition/io/HttpConnection",
|
|
|
|
|
"javax/microedition/io/HttpsConnection",
|
|
|
|
|
"javax/microedition/io/file/FileConnection"}) {
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
connCls, "openInputStream", "()Ljava/io/InputStream;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
|
|
|
|
self->nativeData.get())
|
|
|
|
|
: nullptr;
|
|
|
|
|
executeHttpIfNeeded(nc);
|
|
|
|
|
uint32_t isRef = v.allocObject("java/io/DataInputStream");
|
|
|
|
|
if (nc) v.getObj(isRef)->nativeData = nc->inStream;
|
|
|
|
|
*r = JVal::MakeRef(isRef);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
connCls, "openDataInputStream", "()Ljava/io/DataInputStream;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
|
|
|
|
self->nativeData.get())
|
|
|
|
|
: nullptr;
|
|
|
|
|
executeHttpIfNeeded(nc);
|
|
|
|
|
uint32_t isRef = v.allocObject("java/io/DataInputStream");
|
|
|
|
|
if (nc) v.getObj(isRef)->nativeData = nc->inStream;
|
|
|
|
|
*r = JVal::MakeRef(isRef);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
connCls, "openOutputStream", "()Ljava/io/OutputStream;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
|
|
|
|
self->nativeData.get())
|
|
|
|
|
: nullptr;
|
|
|
|
|
uint32_t osRef = v.allocObject("java/io/DataOutputStream");
|
|
|
|
|
if (nc) v.getObj(osRef)->nativeData = nc->outStream;
|
|
|
|
|
*r = JVal::MakeRef(osRef);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
connCls, "openDataOutputStream", "()Ljava/io/DataOutputStream;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
|
|
|
|
self->nativeData.get())
|
|
|
|
|
: nullptr;
|
|
|
|
|
uint32_t osRef = v.allocObject("java/io/DataOutputStream");
|
|
|
|
|
if (nc) v.getObj(osRef)->nativeData = nc->outStream;
|
|
|
|
|
*r = JVal::MakeRef(osRef);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
connCls, "setRequestMethod", "(Ljava/lang/String;)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
|
|
|
|
self->nativeData.get())
|
|
|
|
|
: nullptr;
|
|
|
|
|
if (nc) nc->method = v.getStringUtf8(a[1].ref);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
connCls, "setRequestProperty",
|
|
|
|
|
"(Ljava/lang/String;Ljava/lang/String;)V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
|
|
|
|
self->nativeData.get())
|
|
|
|
|
: nullptr;
|
|
|
|
|
if (nc) {
|
|
|
|
|
nc->reqHeaders[v.getStringUtf8(a[1].ref)] =
|
|
|
|
|
v.getStringUtf8(a[2].ref);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
connCls, "getResponseCode", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
|
|
|
|
self->nativeData.get())
|
|
|
|
|
: nullptr;
|
|
|
|
|
executeHttpIfNeeded(nc);
|
|
|
|
|
*r = JVal::MakeInt(nc ? nc->responseCode : 200);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
connCls, "getResponseMessage", "()Ljava/lang/String;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
|
|
|
|
self->nativeData.get())
|
|
|
|
|
: nullptr;
|
|
|
|
|
executeHttpIfNeeded(nc);
|
|
|
|
|
*r = JVal::MakeRef(
|
|
|
|
|
v.allocString(nc ? nc->responseMessage : "OK"));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
connCls, "getLength", "()J",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
|
|
|
|
self->nativeData.get())
|
|
|
|
|
: nullptr;
|
|
|
|
|
executeHttpIfNeeded(nc);
|
|
|
|
|
int64_t len = (nc && nc->inStream)
|
|
|
|
|
? static_cast<int64_t>(nc->inStream->data.size())
|
|
|
|
|
: -1;
|
|
|
|
|
*r = JVal::MakeLong(len);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
connCls, "getAddress", "()Ljava/lang/String;",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
|
|
|
|
self->nativeData.get())
|
|
|
|
|
: nullptr;
|
|
|
|
|
*r = JVal::MakeRef(v.allocString(nc ? nc->host : "127.0.0.1"));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
connCls, "getPort", "()I",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
|
|
|
|
self->nativeData.get())
|
|
|
|
|
: nullptr;
|
|
|
|
|
*r = JVal::MakeInt(nc ? nc->port : 80);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
connCls, "setSocketOption", "(BI)V",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
connCls, "close", "()V",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
auto* nc = self ? static_cast<NativeConnection*>(
|
|
|
|
|
self->nativeData.get())
|
|
|
|
|
: nullptr;
|
|
|
|
|
if (nc) nc->close();
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-28 15:25:07 +02:00
|
|
|
struct NativePlayer {
|
|
|
|
|
int playerId = 0;
|
|
|
|
|
int state = 200; // CLOSED=0, UNREALIZED=100, REALIZED=200, PREFETCHED=300, STARTED=400
|
|
|
|
|
int loopCount = 1;
|
|
|
|
|
int volumeLevel = 80;
|
|
|
|
|
bool muted = false;
|
|
|
|
|
int64_t mediaTimeUs = 0;
|
|
|
|
|
std::string contentType = "audio/midi";
|
|
|
|
|
std::vector<uint8_t> data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int nextPlayerId_ = 1;
|
|
|
|
|
|
2026-10-28 12:41:00 +02:00
|
|
|
void registerMmapi(Jvm& vm) {
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/media/Manager", "playTone", "(III)V",
|
|
|
|
|
[this](Jvm&, JThread&, const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
sound.playTone(a[0].i, a[1].i, a[2].i);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2026-10-28 15:25:07 +02:00
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/media/Manager", "getSupportedContentTypes", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
|
|
|
|
uint32_t arr = v.allocRefArray("java/lang/String", 4);
|
|
|
|
|
JObject* ao = v.getObj(arr);
|
|
|
|
|
ao->refArr[0] = v.allocString("audio/midi");
|
|
|
|
|
ao->refArr[1] = v.allocString("audio/x-wav");
|
|
|
|
|
ao->refArr[2] = v.allocString("audio/wav");
|
|
|
|
|
ao->refArr[3] = v.allocString("audio/x-tone-seq");
|
|
|
|
|
*r = JVal::MakeRef(arr);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/media/Manager", "getSupportedProtocols", "*",
|
|
|
|
|
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
|
|
|
|
uint32_t arr = v.allocRefArray("java/lang/String", 2);
|
|
|
|
|
JObject* ao = v.getObj(arr);
|
|
|
|
|
ao->refArr[0] = v.allocString("device");
|
|
|
|
|
ao->refArr[1] = v.allocString("http");
|
|
|
|
|
*r = JVal::MakeRef(arr);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2026-10-28 12:41:00 +02:00
|
|
|
vm.registerNative(
|
|
|
|
|
"javax/microedition/media/Manager", "createPlayer", "*",
|
2026-10-28 15:25:07 +02:00
|
|
|
[this](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
2026-10-28 12:41:00 +02:00
|
|
|
uint32_t pref = v.allocObject("javax/microedition/media/Player");
|
|
|
|
|
JObject* po = v.getObj(pref);
|
2026-10-28 15:25:07 +02:00
|
|
|
auto np = std::make_shared<NativePlayer>();
|
|
|
|
|
np->playerId = nextPlayerId_++;
|
|
|
|
|
np->state = 200; // REALIZED
|
|
|
|
|
if (a.size() >= 2 && a[1].type == JType::Ref && a[1].ref != 0) {
|
|
|
|
|
np->contentType = v.getStringUtf8(a[1].ref);
|
|
|
|
|
}
|
|
|
|
|
if (!a.empty() && a[0].type == JType::Ref && a[0].ref != 0) {
|
2026-10-28 12:41:00 +02:00
|
|
|
JObject* src = v.getObj(a[0].ref);
|
2026-10-28 15:25:07 +02:00
|
|
|
if (src) {
|
|
|
|
|
if (src->isString) {
|
|
|
|
|
std::string loc = v.getStringUtf8(a[0].ref);
|
|
|
|
|
if (loc.rfind("resource:", 0) == 0) {
|
|
|
|
|
loc = loc.substr(9);
|
|
|
|
|
}
|
|
|
|
|
if (v.jar) v.jar->extractEntry(loc, &np->data);
|
|
|
|
|
} else if (src->nativeData) {
|
|
|
|
|
auto* nis = static_cast<NativeInputStream*>(
|
|
|
|
|
src->nativeData.get());
|
|
|
|
|
if (nis) {
|
|
|
|
|
if (nis->pos < nis->data.size()) {
|
|
|
|
|
np->data.assign(nis->data.begin() + nis->pos,
|
|
|
|
|
nis->data.end());
|
|
|
|
|
nis->pos = nis->data.size();
|
|
|
|
|
} else {
|
|
|
|
|
np->data = nis->data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-28 12:41:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
2026-10-28 15:25:07 +02:00
|
|
|
if (po) po->nativeData = np;
|
2026-10-28 12:41:00 +02:00
|
|
|
*r = JVal::MakeRef(pref);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-28 15:25:07 +02:00
|
|
|
auto ensureNativePlayer = [this](JObject* self) -> NativePlayer* {
|
|
|
|
|
if (!self) return nullptr;
|
|
|
|
|
if (!self->nativeData) {
|
|
|
|
|
auto np = std::make_shared<NativePlayer>();
|
|
|
|
|
np->playerId = nextPlayerId_++;
|
|
|
|
|
self->nativeData = np;
|
|
|
|
|
}
|
|
|
|
|
return static_cast<NativePlayer*>(self->nativeData.get());
|
|
|
|
|
};
|
|
|
|
|
|
2026-10-28 12:41:00 +02:00
|
|
|
for (const char* pCls :
|
|
|
|
|
{"javax/microedition/media/Player",
|
|
|
|
|
"javax/microedition/media/Controllable",
|
2026-10-28 15:25:07 +02:00
|
|
|
"javax/microedition/media/Control",
|
2026-10-28 12:41:00 +02:00
|
|
|
"javax/microedition/media/control/VolumeControl",
|
2026-10-28 15:25:07 +02:00
|
|
|
"javax/microedition/media/control/ToneControl",
|
2026-10-28 12:41:00 +02:00
|
|
|
"com/nokia/mid/sound/Sound"}) {
|
2026-10-28 15:25:07 +02:00
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "<init>", "*",
|
|
|
|
|
[this, ensureNativePlayer](Jvm& v, JThread&,
|
|
|
|
|
const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
if (a.empty()) return true;
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(self);
|
|
|
|
|
if (!np) return true;
|
|
|
|
|
// com.nokia.mid.sound.Sound(byte[] data, int type) or Sound(int freq, long dur)
|
|
|
|
|
if (a.size() >= 3 && a[1].type == JType::Ref && a[1].ref != 0) {
|
|
|
|
|
const JObject* bo = v.getObj(a[1].ref);
|
|
|
|
|
if (bo && bo->arrayKind == ArrayKind::Byte) {
|
|
|
|
|
np->data.resize(bo->byteArr.size());
|
|
|
|
|
for (size_t i = 0; i < bo->byteArr.size(); ++i) {
|
|
|
|
|
np->data[i] = static_cast<uint8_t>(bo->byteArr[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (a.size() >= 3 && a[1].type == JType::Int) {
|
|
|
|
|
// Sound(int freq, long duration) -> store synthetic tone header
|
|
|
|
|
int freq = std::clamp(a[1].i, 50, 8000);
|
|
|
|
|
int midiNote = static_cast<int>(
|
|
|
|
|
std::round(69.0 + 12.0 * std::log2(freq / 440.0)));
|
|
|
|
|
midiNote = std::clamp(midiNote, 0, 127);
|
|
|
|
|
np->loopCount = 1;
|
|
|
|
|
if (self) self->nativeTag = (midiNote << 16) |
|
|
|
|
|
std::clamp<int>(
|
|
|
|
|
a[2].type == JType::Long
|
|
|
|
|
? static_cast<int>(a[2].l)
|
|
|
|
|
: a[2].i,
|
|
|
|
|
20, 5000);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "init", "*",
|
|
|
|
|
[ensureNativePlayer](Jvm& v, JThread&, const std::vector<JVal>& a,
|
|
|
|
|
JVal*) {
|
|
|
|
|
if (a.size() >= 3 && a[1].type == JType::Ref && a[1].ref != 0) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(self);
|
|
|
|
|
const JObject* bo = v.getObj(a[1].ref);
|
|
|
|
|
if (np && bo && bo->arrayKind == ArrayKind::Byte) {
|
|
|
|
|
np->data.resize(bo->byteArr.size());
|
|
|
|
|
for (size_t i = 0; i < bo->byteArr.size(); ++i) {
|
|
|
|
|
np->data[i] = static_cast<uint8_t>(bo->byteArr[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "realize", "()V",
|
|
|
|
|
[ensureNativePlayer](Jvm& v, JThread&, const std::vector<JVal>& a,
|
|
|
|
|
JVal*) {
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(v.getObj(a[0].ref));
|
|
|
|
|
if (np && np->state != 0 && np->state < 200) np->state = 200;
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "prefetch", "()V",
|
|
|
|
|
[ensureNativePlayer](Jvm& v, JThread&, const std::vector<JVal>& a,
|
|
|
|
|
JVal*) {
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(v.getObj(a[0].ref));
|
|
|
|
|
if (np && np->state != 0 && np->state < 300) np->state = 300;
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2026-10-28 12:41:00 +02:00
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "start", "()V",
|
2026-10-28 15:25:07 +02:00
|
|
|
[this, ensureNativePlayer](Jvm& v, JThread&,
|
|
|
|
|
const std::vector<JVal>& a, JVal*) {
|
2026-10-28 12:41:00 +02:00
|
|
|
JObject* self = v.getObj(a[0].ref);
|
2026-10-28 15:25:07 +02:00
|
|
|
NativePlayer* np = ensureNativePlayer(self);
|
|
|
|
|
if (!np || np->state == 0) return true;
|
|
|
|
|
np->state = 400; // STARTED
|
|
|
|
|
int effVol = np->muted ? 0 : np->volumeLevel;
|
|
|
|
|
bool loop = (np->loopCount < 0 || np->loopCount > 1);
|
|
|
|
|
sound.setPlayerVolume(np->playerId, effVol);
|
|
|
|
|
if (!np->data.empty()) {
|
|
|
|
|
sound.startPlayerBytes(np->playerId, np->data, loop, effVol);
|
|
|
|
|
} else if (self && self->nativeTag > 0) {
|
|
|
|
|
int note = (self->nativeTag >> 16) & 0x7F;
|
|
|
|
|
int dur = self->nativeTag & 0xFFFF;
|
|
|
|
|
sound.playTone(note, dur > 0 ? dur : 90, effVol);
|
2026-10-28 12:41:00 +02:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "play", "(I)V",
|
2026-10-28 15:25:07 +02:00
|
|
|
[this, ensureNativePlayer](Jvm& v, JThread&,
|
|
|
|
|
const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(self);
|
|
|
|
|
if (!np) return true;
|
|
|
|
|
np->loopCount = (a.size() >= 2 && a[1].i == 0) ? -1 : a[1].i;
|
|
|
|
|
np->state = 400;
|
|
|
|
|
int effVol = np->muted ? 0 : np->volumeLevel;
|
|
|
|
|
bool loop = (np->loopCount < 0 || np->loopCount > 1);
|
|
|
|
|
if (!np->data.empty()) {
|
|
|
|
|
sound.startPlayerBytes(np->playerId, np->data, loop, effVol);
|
|
|
|
|
} else if (self && self->nativeTag > 0) {
|
|
|
|
|
int note = (self->nativeTag >> 16) & 0x7F;
|
|
|
|
|
int dur = self->nativeTag & 0xFFFF;
|
|
|
|
|
sound.playTone(note, dur > 0 ? dur : 90, effVol);
|
|
|
|
|
} else {
|
|
|
|
|
sound.playTone(72, 80, effVol);
|
|
|
|
|
}
|
2026-10-28 12:41:00 +02:00
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "stop", "()V",
|
2026-10-28 15:25:07 +02:00
|
|
|
[this, ensureNativePlayer](Jvm& v, JThread&,
|
|
|
|
|
const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(v.getObj(a[0].ref));
|
|
|
|
|
if (np) {
|
|
|
|
|
sound.stopPlayer(np->playerId);
|
|
|
|
|
if (np->state == 400) np->state = 300; // PREFETCHED
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "deallocate", "()V",
|
|
|
|
|
[this, ensureNativePlayer](Jvm& v, JThread&,
|
|
|
|
|
const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(v.getObj(a[0].ref));
|
|
|
|
|
if (np) {
|
|
|
|
|
sound.stopPlayer(np->playerId);
|
|
|
|
|
if (np->state > 200) np->state = 200; // REALIZED
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "close", "()V",
|
|
|
|
|
[this, ensureNativePlayer](Jvm& v, JThread&,
|
|
|
|
|
const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(v.getObj(a[0].ref));
|
|
|
|
|
if (np) {
|
|
|
|
|
sound.stopPlayer(np->playerId);
|
|
|
|
|
np->state = 0; // CLOSED
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "release", "()V",
|
|
|
|
|
[this, ensureNativePlayer](Jvm& v, JThread&,
|
|
|
|
|
const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(v.getObj(a[0].ref));
|
|
|
|
|
if (np) {
|
|
|
|
|
sound.stopPlayer(np->playerId);
|
|
|
|
|
np->state = 0;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "getState", "()I",
|
|
|
|
|
[this, ensureNativePlayer](Jvm& v, JThread&,
|
|
|
|
|
const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(self);
|
|
|
|
|
if (!np) {
|
|
|
|
|
*r = JVal::MakeInt(0);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (self && self->className == "com/nokia/mid/sound/Sound") {
|
|
|
|
|
// Nokia Sound constants: SOUND_PLAYING=0, SOUND_STOPPED=1, SOUND_UNINITIALIZED=3
|
|
|
|
|
bool active = sound.isPlayerActive(np->playerId);
|
|
|
|
|
*r = JVal::MakeInt(active ? 0 : 1);
|
|
|
|
|
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.
|
|
|
|
|
np->state = 300;
|
|
|
|
|
} else if (!sound.isPlayerActive(np->playerId)) {
|
|
|
|
|
np->state = 300; // Transition back to PREFETCHED when finished
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeInt(np->state);
|
2026-10-28 12:41:00 +02:00
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "setLoopCount", "(I)V",
|
2026-10-28 15:25:07 +02:00
|
|
|
[ensureNativePlayer](Jvm& v, JThread&, const std::vector<JVal>& a,
|
|
|
|
|
JVal*) {
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(v.getObj(a[0].ref));
|
|
|
|
|
if (np) np->loopCount = a[1].i;
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "setMediaTime", "(J)J",
|
|
|
|
|
[this, ensureNativePlayer](Jvm& v, JThread&,
|
|
|
|
|
const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(v.getObj(a[0].ref));
|
|
|
|
|
int64_t t = (a.size() >= 2) ? std::max<int64_t>(0, a[1].l) : 0;
|
|
|
|
|
if (np) {
|
|
|
|
|
np->mediaTimeUs = t;
|
|
|
|
|
if (t == 0) sound.rewindPlayer(np->playerId);
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeLong(t);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "getMediaTime", "()J",
|
|
|
|
|
[ensureNativePlayer](Jvm& v, JThread&, const std::vector<JVal>& a,
|
|
|
|
|
JVal* r) {
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(v.getObj(a[0].ref));
|
|
|
|
|
*r = JVal::MakeLong(np ? np->mediaTimeUs : 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "getDuration", "()J",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
|
|
|
|
*r = JVal::MakeLong(1000000LL);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "getContentType", "()Ljava/lang/String;",
|
|
|
|
|
[ensureNativePlayer](Jvm& v, JThread&, const std::vector<JVal>& a,
|
|
|
|
|
JVal* r) {
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(v.getObj(a[0].ref));
|
|
|
|
|
*r = JVal::MakeRef(
|
|
|
|
|
v.allocString(np ? np->contentType : "audio/midi"));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "addPlayerListener",
|
|
|
|
|
"(Ljavax/microedition/media/PlayerListener;)V",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "removePlayerListener",
|
|
|
|
|
"(Ljavax/microedition/media/PlayerListener;)V",
|
|
|
|
|
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
2026-10-28 12:41:00 +02:00
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "getControl",
|
|
|
|
|
"(Ljava/lang/String;)Ljavax/microedition/media/Control;",
|
2026-10-28 15:25:07 +02:00
|
|
|
[ensureNativePlayer](Jvm& v, JThread&, const std::vector<JVal>& a,
|
|
|
|
|
JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
ensureNativePlayer(self);
|
|
|
|
|
std::string ctrlType =
|
|
|
|
|
(a.size() >= 2 && a[1].type == JType::Ref && a[1].ref != 0)
|
|
|
|
|
? 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";
|
|
|
|
|
uint32_t cref = v.allocObject(clsName);
|
|
|
|
|
JObject* co = v.getObj(cref);
|
|
|
|
|
if (co && self) {
|
|
|
|
|
// Share NativePlayer between Player and its Control!
|
|
|
|
|
co->nativeData = self->nativeData;
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeRef(cref);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "getControls", "()[Ljavax/microedition/media/Control;",
|
|
|
|
|
[ensureNativePlayer](Jvm& v, JThread&, const std::vector<JVal>& a,
|
|
|
|
|
JVal* r) {
|
|
|
|
|
JObject* self = v.getObj(a[0].ref);
|
|
|
|
|
ensureNativePlayer(self);
|
|
|
|
|
uint32_t arr =
|
|
|
|
|
v.allocRefArray("javax/microedition/media/Control", 1);
|
|
|
|
|
uint32_t cref =
|
|
|
|
|
v.allocObject("javax/microedition/media/control/VolumeControl");
|
|
|
|
|
if (self && v.getObj(cref)) {
|
|
|
|
|
v.getObj(cref)->nativeData = self->nativeData;
|
|
|
|
|
}
|
|
|
|
|
v.getObj(arr)->refArr[0] = cref;
|
|
|
|
|
*r = JVal::MakeRef(arr);
|
2026-10-28 12:41:00 +02:00
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "setLevel", "(I)I",
|
2026-10-28 15:25:07 +02:00
|
|
|
[this, ensureNativePlayer](Jvm& v, JThread&,
|
|
|
|
|
const std::vector<JVal>& a, JVal* r) {
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(v.getObj(a[0].ref));
|
|
|
|
|
int lvl = std::clamp(a[1].i, 0, 100);
|
|
|
|
|
if (np) {
|
|
|
|
|
np->volumeLevel = lvl;
|
|
|
|
|
sound.setPlayerVolume(np->playerId, np->muted ? 0 : lvl);
|
|
|
|
|
}
|
|
|
|
|
*r = JVal::MakeInt(lvl);
|
2026-10-28 12:41:00 +02:00
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "getLevel", "()I",
|
2026-10-28 15:25:07 +02:00
|
|
|
[ensureNativePlayer](Jvm& v, JThread&, const std::vector<JVal>& a,
|
|
|
|
|
JVal* r) {
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(v.getObj(a[0].ref));
|
|
|
|
|
*r = JVal::MakeInt(np ? np->volumeLevel : 80);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "setMute", "(Z)V",
|
|
|
|
|
[this, ensureNativePlayer](Jvm& v, JThread&,
|
|
|
|
|
const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(v.getObj(a[0].ref));
|
|
|
|
|
if (np) {
|
|
|
|
|
np->muted = (a[1].i != 0);
|
|
|
|
|
sound.setPlayerVolume(np->playerId,
|
|
|
|
|
np->muted ? 0 : np->volumeLevel);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "isMuted", "()Z",
|
|
|
|
|
[ensureNativePlayer](Jvm& v, JThread&, const std::vector<JVal>& a,
|
|
|
|
|
JVal* r) {
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(v.getObj(a[0].ref));
|
|
|
|
|
*r = JVal::MakeInt((np && np->muted) ? 1 : 0);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "setGain", "(I)V",
|
|
|
|
|
[this, ensureNativePlayer](Jvm& v, JThread&,
|
|
|
|
|
const std::vector<JVal>& a, JVal*) {
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(v.getObj(a[0].ref));
|
|
|
|
|
if (np) {
|
|
|
|
|
np->volumeLevel = std::clamp((a[1].i * 100) / 255, 0, 100);
|
|
|
|
|
sound.setPlayerVolume(np->playerId, np->volumeLevel);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "getGain", "()I",
|
|
|
|
|
[ensureNativePlayer](Jvm& v, JThread&, const std::vector<JVal>& a,
|
|
|
|
|
JVal* r) {
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(v.getObj(a[0].ref));
|
|
|
|
|
*r = JVal::MakeInt(np ? (np->volumeLevel * 255) / 100 : 200);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
vm.registerNative(
|
|
|
|
|
pCls, "setSequence", "([B)V",
|
|
|
|
|
[ensureNativePlayer](Jvm& v, JThread&, const std::vector<JVal>& a,
|
|
|
|
|
JVal*) {
|
|
|
|
|
NativePlayer* np = ensureNativePlayer(v.getObj(a[0].ref));
|
|
|
|
|
if (np && a.size() >= 2 && a[1].type == JType::Ref && a[1].ref != 0) {
|
|
|
|
|
const JObject* bo = v.getObj(a[1].ref);
|
|
|
|
|
if (bo && bo->arrayKind == ArrayKind::Byte) {
|
|
|
|
|
np->data.resize(bo->byteArr.size());
|
|
|
|
|
for (size_t i = 0; i < bo->byteArr.size(); ++i) {
|
|
|
|
|
np->data[i] = static_cast<uint8_t>(bo->byteArr[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-28 12:41:00 +02:00
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace remedy
|
|
|
|
|
|
|
|
|
|
#endif // BANANARCHY_REMEDY_CLDC_NET_MEDIA_HPP
|