--- nono/cli/cli_app.cpp 2026/04/29 17:04:51 1.1.1.6 +++ nono/cli/cli_app.cpp 2026/04/29 17:05:23 1.1.1.12 @@ -4,13 +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 "vm.h" - -void cli_halt_callback(void *); +#include "monitor.h" +#include "power.h" // wxApp と相似にするためのクラス class CLIApp @@ -18,10 +21,15 @@ class CLIApp public: int OnInit(); bool Parse(); - [[noreturn]] void OnCreate(); + int OnCreate(); + void OnHalt(); + void OnAppExit(); + + int ac {}; + char **av {}; - int ac; - char **av; + autofd kq {}; + bool exit_request {}; }; int @@ -46,38 +54,85 @@ CLIApp::OnInit() if (rv != MainApp::PASS) { return rv; } - if (!gMainApp.Init2()) { + // UIMessage の初期化はなんとなくスレッド開始前のほうがよかろう + kq = CUIMessage::Init(); + if (kq < 0) { return EXIT_FAILURE; } - // VM にコールバックを登録 - if (gMPU680x0) { - gMPU680x0->SetHaltCallback(cli_halt_callback, NULL); + // モニタ登録締切。CLI ではあまり関係ないけど揃えておく。 + gMonitorManager->Fix(); + + // スレッド作成を伴う初期化。 + if (!gMainApp.Init2()) { + return EXIT_FAILURE; } // メインウィンドウに相当するところ OnCreate(); + // 戻ってきたので、VM を解放。 + gMainApp.Dispose(); + return EXIT_SUCCESS; } // メインウィンドウ作成後のイベントに相当するところ -void +int CLIApp::OnCreate() { + CUIMessage::Connect(UIMessage::APPEXIT, + [&](const UIMessage&) { OnAppExit(); }); + if (gMainApp.Has(VMCap::M68K)) { + CUIMessage::Connect(UIMessage::HALT, + [&](const UIMessage&) { OnHalt(); }); + } + // UIMessage の処理を開始する。 + UIMessage::Attach(&CUIMessage::Process); + // 電源投入 - gVM->PowerButton(); + auto power = GetPowerDevice(); + power->PushPowerButton(); + + // メッセージを待つ + 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; + } - // ? - for (;;) { - sleep(1); + // ディスパッチ + CUIMessage::Dispatch(m); } + + return 0; } // m680x0 ダブルバスフォールト時に呼ばれるコールバック。 void -cli_halt_callback(void *dummy) +CLIApp::OnHalt() { // どうする? warnx("Double bus fault has occurred in VM"); } + +// アプリケーションの終了要求で呼ばれるコールバック。 +void +CLIApp::OnAppExit() +{ + exit_request = true; +}