--- nono/lib/mainapp.h 2026/04/29 17:04:28 1.1 +++ nono/lib/mainapp.h 2026/04/29 17:05:08 1.1.1.11 @@ -1,93 +1,159 @@ // // nono -// Copyright (C) 2018 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt +// + +// +// CLI/GUI 共通のメイン部分 // #pragma once -#include "configfile.h" -#include "debugger.h" -#include "logger.h" -#include "scheduler.h" #include "vm.h" -extern bool console_log; -extern bool fast_mode; -extern char logopt[256]; -extern const char *human68k_file; -extern char human68k_arg[256]; - -extern std::string vmdir; - -extern bool parse_logopt(const char *arg, u_int vmtype); - -// 起動時の処理 は CLI と GUI でよく似ているので共通化する。 -// ただし地味に異なるところがあるのでテンプレートでコンパイル時処理。 -// log_to_stdout はログをコンソールに(も)出力するなら true。 -template bool -main_init(bool log_to_stdout) +class Config; +class Logger; +class MonitorManager; + +class MainApp { - // ログ機構は引数処理後なるはや - logger_init(); - gLogger->UseStdout(log_to_stdout); - - // 引数で指定されたディレクトリの設定ファイルを読む - gConfig = new Config(vmdir); - if (!gConfig->Load()) { - return false; - } - - // VM 種別を決定 - u_int vm_type; - if (is_cli && human68k_file) { - vm_type = VM::TYPE_RXZ; - } else { - vm_type = gConfig->ReadInt("vm_type"); - } - - // VM 作成 - if (vm_type == VM::TYPE_X68030) { - gVM = new VM_X68030(); - - } else if (vm_type == VM::TYPE_LUNA) { - gVM = new VM_LUNA(); - - } else if (vm_type == VM::TYPE_RXZ && is_cli) { - gVM = new VM_RXZ(human68k_file, human68k_arg); - - } else { - warnx("%s: invalid vm_type %d", - gConfig->GetFilename().c_str(), vm_type); - return false; - } - - // 動的なコンストラクション - if (!gVM->Create()) { - return false; - } - - // ログレベルを設定。コンストラクト後すぐに行う。 - // -L help もここで処理。 - if (!parse_logopt(logopt, vm_type)) { - return false; - } - - // VM 初期化 - // (デバッガは VM オブジェクトリストに入っていない) - if (!gVM->Init()) { - return false; - } - debugger_init(); - - // 起動時設定の適用 - if (!gVM->Apply()) { - return false; - } - - // ここで引数を VM に適用? - // XXX これは設定ファイルのオーバーライド方向にするべき - gScheduler->SetFullSpeed(fast_mode); + public: + // Init() の戻り値 + static const int PASS = -1; // 実行を継続 + + public: + MainApp(); + ~MainApp(); + + // 初期化 (ステージ1) + int Init1(bool is_cli_, int ac, char *av[]); + + // 初期化 (ステージ2) + bool Init2(); + + std::string SearchFile(const std::string& name) const; + + // vmtype (VMTYPE_*) を返す。 + // (Config が持っているのは文字列) + vmtype_t GetVMType() const { return vmtype; } + + // 現在の VM が指定の feature を持っているか? + bool HasVMFeature(vmf_t flag) const; + + // VM ディレクトリを返す + const std::string& GetVMDir() const { return vmdir; } + + // ログレベルを設定する。 + static bool SetLogopt(const std::vector&, std::string *errmsg); + + // ログ名の一覧を表示する + static std::vector GetLogNames(); + + // オブジェクトを追加/削除 (Object が呼ぶ) + void AddObject(Object *); + void DeleteObject(Object *); + + // オブジェクトのリストを返す + const std::vector& GetObjects() const { return objects; } + + // Logger を返す + Logger *GetLogger() const { return logger.get(); } + + public: + // -B: ベンチマークモード + int benchmark_mode {}; + + // -b: ブレークポイント "[,]" + std::vector debug_breakaddr {}; + + // -d: 起動時にデバッガで停止 + bool debug_on_start {}; + + // -D: デバッガを stdio で使う + bool opt_D {}; + + // -f: 高速モード (XXX 設定ファイルと含めて見直すこと) + bool fast_mode {}; + + // --fontsize: フォントサイズ指定値 (値は fontsize_t ではない) + int fontsize {}; + + // -H: 内蔵 Human68k 風を組み込む。-X による実行ファイル名が必要。 + bool human_mode {}; + + // -M: モニタ名 + std::string monitor_opt {}; + + // -s,--scale: 画面スケール + double screen_scale {}; + + // -X: ホスト実行ファイル名とその引数 + const char *exec_file {}; + std::string exec_arg {}; + bool load_and_exec {}; + + private: + // ヘルプメッセージを表示する + void ShowHelp(bool all) const; + + // バージョンを表示する + void ShowVersion() const; + + // 引数を処理する + bool ParseOpt(int ac, char *av[]); + + // opt を logopt に追加する (-L オプション用) + void AddLogopt(const char *opt); + + // -L オプションを処理する + bool ParseLogopt(); + + // ログレベル1つを設定する。 + static bool SetLogopt1(const std::string&, std::string *errmsg); + + // コンパイルされている host netdriver の一覧を表示 + void ShowHostnet() const; + + // CLI/GUI 区別 + bool IsCLI() const { return is_cli; } + bool IsGUI() const { return !is_cli; } + bool is_cli {}; + + // ログ + std::unique_ptr logger /*{}*/; + + // 全オブジェクトへのポインタのリスト (所有はしていない) + std::vector objects {}; + + // VM + std::unique_ptr pVM {}; + + // 設定項目 + std::unique_ptr pConfig /*{}*/; + + // VM 種別 + vmtype_t vmtype {}; + + // モニタマネージャ + std::unique_ptr pMonitorManager /*{}*/; + + // -c: VM ディレクトリ or 設定ファイル + std::string vmdir {}; + std::string vmfile {}; + + // -C: ログをコンソールに出力 + bool log_to_console {}; + + // -L: ログ指定文字列 + std::string logopt {}; + + // --show-config: 設定内容を表示 (1なら通常、2ならall) + int show_config {}; - return true; -} + // -V: パラメータ指定 + std::vector config_options {}; +}; +// グローバルインスタンス +extern MainApp gMainApp;