--- 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:16 1.1.1.11 @@ -4,12 +4,15 @@ // 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 "power.h" #include "vm.h" // wxApp と相似にするためのクラス @@ -20,9 +23,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 +54,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,7 +73,9 @@ CLIApp::OnInit() int CLIApp::OnCreate() { - if (gMPU680x0) { + CUIMessage::Connect(UIMessage::APPEXIT, + [&](const UIMessage&) { OnAppExit(); }); + if (gMainApp.Has(VMCap::M68K)) { CUIMessage::Connect(UIMessage::HALT, [&](const UIMessage&) { OnHalt(); }); } @@ -69,11 +83,36 @@ CLIApp::OnCreate() UIMessage::Attach(&CUIMessage::Process); // 電源投入 - gVM->PowerButton(); + auto power = GetPowerDevice(); + power->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; +}