Skip to content

Commit

Permalink
Fully implement MIDlet class to the spec
Browse files Browse the repository at this point in the history
  • Loading branch information
usernameak committed Sep 14, 2024
1 parent 2ff004a commit a8d7e97
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
15 changes: 12 additions & 3 deletions src/main/java/javax/microedition/midlet/MIDlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ public final String getAppProperty(String key) {
}

public final void notifyDestroyed() {
throw new FeatureNotImplementedError("MIDlet::notifyDestroyed");
appInstance.onShutdown();
}

public final void notifyPaused() {
throw new FeatureNotImplementedError("MIDlet::notifyPaused");
pauseApp();
}

public final void resumeRequest() {
throw new FeatureNotImplementedError("MIDlet::resumeRequest");
try {
startApp();
} catch (MIDletStateChangeException e) {
// i guess...?
e.printStackTrace();
}
}

public static AppInstance getAppModel(MIDlet midlet) {
Expand All @@ -39,4 +44,8 @@ public static AppInstance getAppModel(MIDlet midlet) {
public static void startMidlet(MIDlet midlet) throws MIDletStateChangeException {
midlet.startApp();
}

public static void destroyMidlet(MIDlet midlet) throws MIDletStateChangeException {
midlet.destroyApp(false);
}
}
17 changes: 16 additions & 1 deletion src/main/java/net/sktemu/ams/AppInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class AppInstance implements AutoCloseable {

private long lastPresentTime = 0;

private MIDlet midlet;

static {
AmsSysPropManager.init();
}
Expand Down Expand Up @@ -101,7 +103,7 @@ public void initAppInstance() throws AmsException {
try {
Object midletObj = midletClass.getConstructor().newInstance();
if (midletObj instanceof MIDlet) {
MIDlet midlet = (MIDlet) midletObj;
midlet = (MIDlet) midletObj;
System.out.println("midlet load");

MIDlet.startMidlet(midlet);
Expand Down Expand Up @@ -214,4 +216,17 @@ public void blitGraphics() {

runOnUiThread(emuCanvas::repaint);
}

public boolean shutdown() {
try {
MIDlet.destroyMidlet(midlet);
} catch (MIDletStateChangeException e) {
return false;
}
return true;
}

public void onShutdown() {
System.exit(0);
}
}
19 changes: 17 additions & 2 deletions src/main/java/net/sktemu/ui/EmuUIFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class EmuUIFrame extends JFrame {
private final EmuCanvas canvas;
Expand All @@ -23,10 +25,23 @@ public EmuUIFrame(AppModel appModel) {
);
setContentPane(canvas);

setDefaultCloseOperation(EXIT_ON_CLOSE);

pack();

addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
try {
if (appInstance != null && !appInstance.shutdown()) return;
} catch (Throwable ex) {
ex.printStackTrace();
System.exit(1);
return;
}

System.exit(0);
}
});

addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
Expand Down

0 comments on commit a8d7e97

Please sign in to comment.