--- nono/cli/cli_app.cpp 2026/04/29 17:04:56 1.1.1.8 +++ nono/cli/cli_app.cpp 2026/04/29 17:05:09 1.1.1.9 @@ -4,12 +4,16 @@ // Licensed under nono-license.txt // +// +// CLI アプリケーションのエントリポイント +// + #include "header.h" #include "cuimessage.h" +#include "kevent.h" #include "mainapp.h" #include "mpu680x0.h" #include "pluto.h" -#include "scheduler.h" #include "vm.h" // wxApp と相似にするためのクラス @@ -20,9 +24,13 @@ class CLIApp bool Parse(); int OnCreate(); void OnHalt(); + void OnAppExit(); - int ac; - char **av; + int ac {}; + char **av {}; + + autofd kq {}; + bool exit_request {}; }; int @@ -47,6 +55,11 @@ CLIApp::OnInit() if (rv != MainApp::PASS) { return rv; } + // UIMessage の初期化はなんとなくスレッド開始前のほうがよかろう + kq = CUIMessage::Init(); + if (kq < 0) { + return EXIT_FAILURE; + } if (!gMainApp.Init2()) { return EXIT_FAILURE; } @@ -61,6 +74,8 @@ CLIApp::OnInit() int CLIApp::OnCreate() { + CUIMessage::Connect(UIMessage::APPEXIT, + [&](const UIMessage&) { OnAppExit(); }); if (gMPU680x0) { CUIMessage::Connect(UIMessage::HALT, [&](const UIMessage&) { OnHalt(); }); @@ -71,9 +86,33 @@ CLIApp::OnCreate() // 電源投入 gVM->PowerButton(); - // スケジューラの終了を待つ + // メッセージを待つ + while (exit_request == false) { + struct kevent kev; + + if (kevent_poll(kq, &kev, 1, NULL) < 0) { + if (errno == EINTR) { + continue; + } + warn("CLIApp::OnCreate: kevent_poll"); + // XXX どうする? + continue; + } + + // パイプから1メッセージ分読み出す + UIMessage m; + if (read((int)kev.ident, &m, sizeof(m)) < 0) { + warn("CLIApp::OnCreate: read"); + // XXX どうする? + continue; + } - gScheduler->Wait(); + // ディスパッチ + CUIMessage::Dispatch(m); + } + + // VM を解放 + gVM->Dispose(); return 0; } @@ -85,3 +124,10 @@ CLIApp::OnHalt() // どうする? warnx("Double bus fault has occurred in VM"); } + +// アプリケーションの終了要求で呼ばれるコールバック。 +void +CLIApp::OnAppExit() +{ + exit_request = true; +}