--- 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:49 1.1.1.14 @@ -4,13 +4,16 @@ // Licensed under nono-license.txt // -#include "header.h" -#include "mainapp.h" -#include "mpu680x0.h" -#include "pluto.h" -#include "vm.h" +// +// CLI アプリケーションのエントリポイント +// -void cli_halt_callback(void *); +#include "nono.h" +#include "cuimessage.h" +#include "kevent.h" +#include "mainapp.h" +#include "monitor.h" +#include "power.h" // wxApp と相似にするためのクラス class CLIApp @@ -18,10 +21,14 @@ class CLIApp public: int OnInit(); bool Parse(); - [[noreturn]] void OnCreate(); + int OnCreate(); + void OnHalt(); - int ac; - char **av; + int ac {}; + char **av {}; + + autofd kq {}; + bool exit_request {}; }; int @@ -42,41 +49,86 @@ 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; } - 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() { + gMainApp.GetUIMessage()->StartUIMessage(&CUIMessage::Process); + // 電源投入 - gVM->PowerButton(); + auto power = GetPowerDevice(); + power->PushPowerButton(); - // ? - for (;;) { - sleep(1); + // メッセージを待つ + 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; + } } + + gMainApp.GetUIMessage()->StopUIMessage(); + + return 0; } // m680x0 ダブルバスフォールト時に呼ばれるコールバック。 void -cli_halt_callback(void *dummy) +CLIApp::OnHalt() { // どうする? warnx("Double bus fault has occurred in VM");