--- 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:49 1.1.1.14 @@ -4,13 +4,16 @@ // Licensed under nono-license.txt // -#include "header.h" +// +// CLI アプリケーションのエントリポイント +// + +#include "nono.h" #include "cuimessage.h" +#include "kevent.h" #include "mainapp.h" -#include "mpu680x0.h" -#include "pluto.h" -#include "scheduler.h" -#include "vm.h" +#include "monitor.h" +#include "power.h" // wxApp と相似にするためのクラス class CLIApp @@ -21,8 +24,11 @@ class CLIApp int OnCreate(); void OnHalt(); - int ac; - char **av; + int ac {}; + char **av {}; + + autofd kq {}; + bool exit_request {}; }; int @@ -43,10 +49,20 @@ CLIApp::OnInit() int rv; // CLI 側では関係ないが wx 側で必要なので初期化は2ステージに分かれている - rv = gMainApp.Init1(true, ac, av); + rv = gMainApp.Init1(ac, av, NULL); if (rv != MainApp::PASS) { return rv; } + // UIMessage の初期化はなんとなくスレッド開始前のほうがよかろう + kq = CUIMessage::Init(); + if (kq < 0) { + return EXIT_FAILURE; + } + + // モニタ登録締切。CLI ではあまり関係ないけど揃えておく。 + gMonitorManager->Fix(); + + // スレッド作成を伴う初期化。 if (!gMainApp.Init2()) { return EXIT_FAILURE; } @@ -54,6 +70,9 @@ CLIApp::OnInit() // メインウィンドウに相当するところ OnCreate(); + // 戻ってきたので、VM を解放。 + gMainApp.Dispose(); + return EXIT_SUCCESS; } @@ -61,19 +80,48 @@ CLIApp::OnInit() int CLIApp::OnCreate() { - if (gMPU680x0) { - CUIMessage::Connect(UIMessage::HALT, - [&](const UIMessage&) { OnHalt(); }); - } - // UIMessage の処理を開始する。 - UIMessage::Attach(&CUIMessage::Process); + gMainApp.GetUIMessage()->StartUIMessage(&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メッセージ分読み出す + union64 m; + if (read((int)kev.ident, &m, sizeof(m)) < 0) { + warn("CLIApp::OnCreate: read"); + // XXX どうする? + continue; + } + uint id = m.h; + + // 少ないのでこの場でディスパッチする。 + switch (id) { + case UIMessage::APPEXIT: + exit_request = true; + break; + case UIMessage::HALT: + OnHalt(); + break; + default: + break; + } + } - gScheduler->Wait(); + gMainApp.GetUIMessage()->StopUIMessage(); return 0; }