--- nono/cli/cli_app.cpp 2026/04/29 17:04:53 1.1.1.7 +++ nono/cli/cli_app.cpp 2026/04/29 17:05:09 1.1.1.9 @@ -4,15 +4,18 @@ // 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" -void cli_halt_callback(void *); - // wxApp と相似にするためのクラス class CLIApp { @@ -20,9 +23,14 @@ class CLIApp int OnInit(); bool Parse(); int OnCreate(); + void OnHalt(); + void OnAppExit(); + + int ac {}; + char **av {}; - int ac; - char **av; + autofd kq {}; + bool exit_request {}; }; int @@ -47,13 +55,13 @@ 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); + if (!gMainApp.Init2()) { + return EXIT_FAILURE; } // メインウィンドウに相当するところ @@ -66,20 +74,60 @@ CLIApp::OnInit() int CLIApp::OnCreate() { + CUIMessage::Connect(UIMessage::APPEXIT, + [&](const UIMessage&) { OnAppExit(); }); + if (gMPU680x0) { + CUIMessage::Connect(UIMessage::HALT, + [&](const UIMessage&) { OnHalt(); }); + } + // UIMessage の処理を開始する。 + UIMessage::Attach(&CUIMessage::Process); + // 電源投入 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; + } + + // ディスパッチ + CUIMessage::Dispatch(m); + } - gScheduler->Wait(); + // VM を解放 + gVM->Dispose(); 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; +}