Annotation of nono/wx/wxapp.cpp, revision 1.1.1.16

1.1       root        1: //
                      2: // nono
1.1.1.3   root        3: // Copyright (C) 2020 nono project
                      4: // Licensed under nono-license.txt
1.1       root        5: //
                      6: 
1.1.1.11  root        7: //
                      8: // wx アプリケーションのエントリポイント
                      9: //
                     10: 
                     11: #include "fontmanager.h"
                     12: #include "wxlogsetting.h"
1.1       root       13: #include "wxmainframe.h"
                     14: #include "wxmainview.h"
1.1.1.11  root       15: #include "wxuimessage.h"
1.1.1.4   root       16: #include "wxversion.h"
1.1.1.13  root       17: #include "config.h"
1.1       root       18: #include "mainapp.h"
1.1.1.13  root       19: #include "memdump.h"
1.1.1.8   root       20: #include <algorithm>
1.1.1.3   root       21: #include <wx/intl.h>
1.1       root       22: 
1.1.1.8   root       23: class WXApp : public wxApp
1.1       root       24: {
1.1.1.6   root       25:        using inherited = wxApp;
1.1       root       26:  public:
1.1.1.11  root       27:        ~WXApp() override;
1.1.1.13  root       28:        bool OnInit() override;
1.1.1.6   root       29:        int OnRun() override;
1.1       root       30:  private:
                     31:        bool ParseMonitors(const wxString& str);
1.1.1.15  root       32:        bool ParseMemdumpValue(uint id, const std::string&, const std::string&);
1.1.1.8   root       33:        void ShowMonitorsName();
1.1       root       34:        int SearchMonitorName(const char *name);
1.1.1.15  root       35:        int FindAvailable(const std::vector<uint>& candidates,
1.1.1.13  root       36:                const std::string& name, int basenum, int count) const;
1.1.1.3   root       37: 
                     38:        wxLocale locale;
1.1.1.11  root       39: 
                     40:        std::unique_ptr<FontManager> pFontManager {};
                     41:        std::unique_ptr<HostInfoMonitor> pHostInfo {};
1.1       root       42: };
                     43: 
1.1.1.3   root       44: // IMPLEMENT_APP() は闇マクロすぎて clang のちからには耐えられない
1.1.1.11  root       45: PRAGMA_PUSH_WARNINGS
1.1       root       46: IMPLEMENT_APP(WXApp)
1.1.1.11  root       47: PRAGMA_POP_WARNINGS
                     48: 
                     49: WXApp::~WXApp()
                     50: {
                     51:        // 開発用
                     52:        WXUIMessage::AssertAllDisconnected();
                     53: }
1.1       root       54: 
                     55: bool
                     56: WXApp::OnInit()
                     57: {
1.1.1.6   root       58:        return true;
                     59: }
                     60: 
                     61: int
                     62: WXApp::OnRun()
                     63: {
1.1.1.5   root       64:        int rv;
                     65: 
1.1.1.7   root       66:        rv = gMainApp.Init1(false, argc, argv);
1.1.1.5   root       67:        if (rv != MainApp::PASS) {
1.1.1.6   root       68:                return rv;
1.1       root       69:        }
                     70: 
1.1.1.3   root       71:        // 環境変数 $NONO_BUILD_HOME が定義されていればカタログ検索パスに
                     72:        // $NONO_BUILD_HOME/po を追加する (システムパスより前に挿入される)。
                     73:        // カタログ開発時にワークツリーにあるカタログを見るため。
1.1.1.13  root       74:        locale.Init(wxLANGUAGE_DEFAULT, wxLOCALE_DONT_LOAD_DEFAULT);
1.1.1.3   root       75:        const char *build_home = getenv("NONO_BUILD_HOME");
                     76:        if (build_home) {
                     77:                wxString path(build_home);
                     78:                path += "/po";
                     79:                wxLocale::AddCatalogLookupPathPrefix(path);
                     80:        }
                     81:        locale.AddCatalog("nono");
                     82: 
1.1.1.13  root       83:        // テキスト系コントロールのフォントサイズ。
                     84:        // 紛らわしいが設定の monitor-fontsize は 12, 16 とかいう値。
1.1.1.11  root       85:        // FontManager::fontid は enum 値。
1.1.1.13  root       86:        const ConfigItem& item_fontsize = gConfig->Find("monitor-fontsize");
                     87:        int fontsize = item_fontsize.AsInt();
1.1.1.4   root       88:        FontId fontid;
1.1.1.13  root       89:        switch (fontsize) {
1.1.1.2   root       90:         case 12:
1.1.1.4   root       91:                fontid = FontId::_6x12;
1.1.1.2   root       92:                break;
                     93:         case 16:
1.1.1.4   root       94:                fontid = FontId::_8x16;
1.1.1.2   root       95:                break;
1.1.1.13  root       96:         case 24:
                     97:                fontid = FontId::_12x24;
                     98:                break;
1.1.1.2   root       99:         default:
1.1.1.13  root      100:                item_fontsize.Err("Must be either 12, 16 or 24");
1.1.1.7   root      101:                return EXIT_FAILURE;
1.1       root      102:        }
1.1.1.11  root      103:        pFontManager.reset(new FontManager());
                    104:        gFontManager = pFontManager.get();
                    105:        gFontManager->SetFont(fontid);
                    106: 
                    107:        // ホスト情報用のモニタ (どこでやるのがいいか)
                    108:        pHostInfo.reset(new HostInfoMonitor());
                    109:        gHostInfo = pHostInfo.get();
1.1       root      110: 
1.1.1.11  root      111:        // 手順 4. (vm/device.h)
1.1.1.8   root      112:        // これ以降モニタの登録をしてはいけない。
1.1.1.11  root      113:        gMonitorManager->Fix();
1.1.1.8   root      114: 
1.1       root      115:        // -M 起動時に表示するモニタ
1.1.1.2   root      116:        if (!gMainApp.monitor_opt.empty()) {
                    117:                if (!ParseMonitors(gMainApp.monitor_opt)) {
1.1.1.7   root      118:                        return EXIT_FAILURE;
1.1       root      119:                }
                    120:        }
                    121: 
1.1.1.13  root      122:        // メインスクリーン倍率
                    123:        const ConfigItem& item_scale = gConfig->Find("mainview-scale");
                    124:        double scale;
                    125:        if (item_scale.TryDouble(&scale) == false) {
                    126:                item_scale.Err();
                    127:                return EXIT_FAILURE;
                    128:        }
                    129:        // 制限は適当
                    130:        if (scale <= 0 || scale >= 10) {
                    131:                item_scale.Err();
                    132:                return EXIT_FAILURE;
                    133:        }
                    134:        WXMainView::screen_scale = scale;
                    135: 
                    136:        // モニター更新間隔
                    137:        const ConfigItem& item_rate = gConfig->Find("monitor-rate");
                    138:        int rate = item_rate.AsInt();
                    139:        if (rate < 1 || rate > 60) {
                    140:                item_rate.Err();
                    141:                return EXIT_FAILURE;
                    142:        }
                    143:        WXMainFrame::SetMonitorRate(rate);
1.1       root      144: 
1.1.1.7   root      145:        // 引数処理が終わったので、スレッド作成を伴う初期化。
                    146:        if (!gMainApp.Init2()) {
                    147:                return EXIT_FAILURE;
                    148:        }
                    149: 
1.1.1.2   root      150:        // メインウィンドウ
1.1.1.13  root      151:        auto mainframe = new WXMainFrame(NULL);
                    152:        mainframe->Show(true);
                    153:        SetTopWindow(mainframe);
1.1.1.6   root      154: 
1.1.1.10  root      155:        // 設定反映などの初期化
1.1.1.13  root      156:        if (!mainframe->Init()) {
1.1.1.10  root      157:                return EXIT_FAILURE;
                    158:        }
                    159: 
1.1.1.14  root      160:        // イベントループ。
                    161:        rv = inherited::OnRun();
                    162: 
                    163:        // 戻ってきたので VM を解放。
                    164:        gMainApp.Dispose();
                    165: 
                    166:        return rv;
1.1       root      167: }
                    168: 
                    169: // -M オプションの解析。書式は
1.1.1.8   root      170: //  -M <arg>[,<arg>[,...]]
                    171: // <arg> のどれかが "help" なら有効な名前を列挙して終了。
                    172: // <arg> は通常モニタ名(サブウィンドウ名)のみだが
                    173: // memdump* の場合のみ <name>=<hexaddr> 形式を受け付ける。
                    174: // 継続する場合は true、しない場合は false を返す。
1.1       root      175: bool
                    176: WXApp::ParseMonitors(const wxString& str)
                    177: {
1.1.1.8   root      178:        std::vector<std::string> args = string_split(str, ',');
1.1       root      179: 
1.1.1.8   root      180:        // "help" があればヘルプを表示して終了
                    181:        for (const auto& arg : args) {
                    182:                if (arg == "help") {
                    183:                        ShowMonitorsName();
                    184:                        return false;
1.1       root      185:                }
                    186:        }
                    187: 
1.1.1.8   root      188:        // 一致するモニタ(ウィンドウ)の開始フラグを立てる
                    189:        for (const auto& arg : args) {
                    190:                std::string name;
                    191:                std::string value;
                    192: 
                    193:                auto pos = arg.find('=');
                    194:                if (pos != std::string::npos) {
                    195:                        name = arg.substr(0, pos);
                    196:                        value = arg.substr(++pos);
                    197:                } else {
                    198:                        name = arg;
1.1.1.3   root      199:                }
1.1.1.8   root      200: 
                    201:                // この名前のウィンドウを探して start フラグを立てる
                    202:                int id = SearchMonitorName(name.c_str());
                    203:                if (id < 0) {
1.1       root      204:                        return false;
                    205:                }
1.1.1.8   root      206:                WXMainFrame::subwindow_start[id] = true;
                    207: 
                    208:                // 値付き memdump の処理
1.1.1.13  root      209:                if ((IS_MONITOR_MEMDUMP(id) || IS_MONITOR_XPMEMDUMP(id))
                    210:                                && value.empty() == false)
                    211:                {
                    212:                        if (ParseMemdumpValue(id, name, value) == false) {
                    213:                                return false;
                    214:                        }
                    215:                }
                    216:        }
                    217:        return true;
                    218: }
                    219: 
                    220: // -Mmemdump= の続きの文字列 value を解析してセットする。
                    221: // 書式は <hex-address>[.<fmt>]
                    222: // fmt は B/W/L と M(MMU)、I(dIsasm)、Z(XPdisasm)
                    223: bool
1.1.1.15  root      224: WXApp::ParseMemdumpValue(uint id,
1.1.1.13  root      225:        const std::string& name, const std::string& value)
                    226: {
                    227:        auto mon = gMonitorManager->Get(id);
1.1.1.16! root      228:        auto memdump = dynamic_cast<Memdump *>(mon->obj);
1.1.1.13  root      229:        assert(memdump);
                    230: 
                    231:        errno = 0;
                    232:        char *end;
                    233:        unsigned long u = strtoul(value.c_str(), &end, 16);
                    234:        // これが呼ばれた時点で value は空ではないはず
                    235:        if (errno == ERANGE || u > 0xffffffff) {
                    236:                errno = ERANGE;
                    237:                warn("-M %s=%s", name.c_str(), value.c_str());
                    238:                return false;
                    239:        }
                    240:        // アドレスを設定
                    241:        memdump->SetAddr((uint32)u);
                    242: 
                    243:        // さらに後ろがあれば表示形式指定。
                    244:        if (*end != '\0') {
                    245:                if (*end++ != '.') {
                    246:                        goto usage;
                    247:                }
                    248: 
                    249:                if (end[1] != '\0') {
                    250:                        goto usage;
                    251:                }
                    252: 
                    253:                Memdump::Format fmt;
                    254:                switch (std::toupper((int)*end)) {
                    255:                 case 'B':      fmt = Memdump::Byte;    break;
                    256:                 case '\0':
                    257:                 case 'W':      fmt = Memdump::Word;    break;
                    258:                 case 'L':      fmt = Memdump::Long;    break;
                    259:                 case 'M':
                    260:                        if (gMainApp.Has(VMCap::M88K)) {
                    261:                                fmt = Memdump::M88200Page;
                    262:                        } else {
1.1.1.16! root      263:                                // m68k はちょっと複雑すぎて放置。
        !           264:                                goto usage;
1.1.1.8   root      265:                        }
1.1.1.13  root      266:                        break;
                    267:                 case 'I':
                    268:                        if (gMainApp.Has(VMCap::M88K)) {
                    269:                                fmt = Memdump::M88100Disasm;
                    270:                        } else {
1.1.1.16! root      271:                                fmt = Memdump::M680x0Disasm;
1.1.1.8   root      272:                        }
1.1.1.13  root      273:                        break;
                    274:                 case 'Z':
                    275:                        fmt = Memdump::HD64180Disasm;
                    276:                        break;
                    277:                 default:
                    278:                 usage:
1.1.1.14  root      279:                        warnx("-M %s=%s: syntax error; syntax is \"<hex-addr>[.<BWLMIZ>]\"",
1.1.1.13  root      280:                                name.c_str(), value.c_str());
                    281:                        return false;
1.1.1.8   root      282:                }
1.1.1.13  root      283: 
                    284:                // 表示形式を設定
                    285:                memdump->SetFormat(fmt);
1.1       root      286:        }
1.1.1.13  root      287: 
1.1       root      288:        return true;
                    289: }
                    290: 
1.1.1.8   root      291: // モニタ名の一覧を表示する
                    292: void
                    293: WXApp::ShowMonitorsName()
                    294: {
1.1.1.15  root      295:        std::vector<uint> list;
1.1.1.8   root      296: 
                    297:        // モニターは登録されていれば列挙
                    298:        // (登録されているものを全部列挙ではないことに注意)
1.1.1.15  root      299:        for (uint id = ID_MONITOR_START; id <= ID_MONITOR_END; id++) {
1.1.1.11  root      300:                if (gMonitorManager->Find(id)) {
1.1.1.8   root      301:                        list.push_back(id);
                    302:                }
                    303:        }
                    304:        // サブウィンドウは機種情報から判断
1.1.1.15  root      305:        for (uint id = ID_SUBWIN_START; id <= ID_SUBWIN_END; id++) {
1.1.1.12  root      306:                VMCap vmcap = gMonitorManager->GetVMCap(id);
                    307:                if (gMainApp.Has(vmcap)) {
1.1.1.8   root      308:                        list.push_back(id);
                    309:                }
                    310:        }
                    311: 
                    312:        // 一覧を表示
                    313:        std::string msg = MonitorManager::MakeListString(list);
                    314:        printf("%s", msg.c_str());
                    315: }
                    316: 
                    317: // モニタ名 name を検索し、一つに確定すればその id を返す。
                    318: // 確定しない、あるいは一致しない場合はエラーメッセージを表示し -1 を返す。
                    319: //
                    320: // "memdump" (数字なし) は空いてる memdump<N> を順に使用する。
                    321: // ただし今の所この処理は1パスで前から順に処理しているだけなので、
                    322: // "memdump0,memdump" は指定可能だが(2つ目が memdump1 になる)、
                    323: // "memdump,memdump0" は memdump0 を2回指定したことになる。
1.1       root      324: int
                    325: WXApp::SearchMonitorName(const char *name)
                    326: {
1.1.1.15  root      327:        std::vector<uint> candidates;
1.1.1.8   root      328:        std::vector<std::string> cand_names;
                    329: 
1.1.1.15  root      330:        for (uint id = ID_MONITOR_START; id <= ID_SUBWIN_END; id++) {
1.1.1.8   root      331:                if (id < ID_SUBWIN_START) {
                    332:                        // モニターは登録されていなければ除外する
1.1.1.11  root      333:                        if (gMonitorManager->Find(id) == NULL) {
1.1.1.8   root      334:                                continue;
                    335:                        }
                    336:                } else {
                    337:                        // サブウィンドウは該当機種でなければ除外する
1.1.1.12  root      338:                        VMCap vmcap = gMonitorManager->GetVMCap(id);
                    339:                        if (gMainApp.Has(vmcap) == false) {
1.1.1.8   root      340:                                continue;
                    341:                        }
                    342:                }
                    343: 
1.1.1.11  root      344:                const auto& aliases = gMonitorManager->GetAliases(id);
1.1.1.8   root      345: 
                    346:                bool matched = false;
                    347:                for (const auto& alias : aliases) {
                    348:                        // 全文一致したら確定
                    349:                        // ("lunafb" と "lunafb0" みたいなのがあるため)
                    350:                        if (alias == name) {
                    351:                                return id;
                    352:                        }
                    353: 
                    354:                        // 部分一致したら名前はすべて覚えておく
                    355:                        if (starts_with_ignorecase(alias, name)) {
                    356:                                cand_names.push_back(alias);
                    357:                                matched = true;
                    358:                        }
                    359:                }
                    360:                // 同じオブジェクトで別名が複数回部分一致しても、1回として数える
                    361:                if (matched) {
                    362:                        candidates.push_back(id);
                    363:                }
                    364:        }
                    365: 
                    366:        if (candidates.empty()) {
                    367:                // 一致しない
                    368:                warnx("Unknown window name \"%s\"", name);
                    369:                return -1;
                    370:        } else if (candidates.size() == 1) {
                    371:                // 部分一致したのが1つだけなら確定
                    372:                return candidates[0];
                    373:        } else {
                    374:                // 候補が複数あった
1.1.1.15  root      375:                uint id;
1.1.1.8   root      376: 
1.1.1.13  root      377:                // "memdump" を空いてる "memdump*" に割り当てる
                    378:                id = FindAvailable(candidates, "memdump",
                    379:                        ID_MONITOR_MEMDUMP0, MAX_MEMDUMP_MONITOR);
                    380:                if (id != 0) {
                    381:                        return id;
                    382:                }
                    383: 
                    384:                // "xpmemdump" を空いてる "xpmemdump*" に割り当てる
                    385:                id = FindAvailable(candidates, "xpmemdump",
                    386:                        ID_MONITOR_XPMEMDUMP0, MAX_XPMEMDUMP_MONITOR);
                    387:                if (id != 0) {
                    388:                        return id;
1.1.1.8   root      389:                }
                    390: 
                    391:                // そうでなければ、候補を表示
                    392:                std::string candstr;
                    393:                for (const auto& cname : cand_names) {
                    394:                        candstr += ' ';
                    395:                        candstr += cname;
1.1       root      396:                }
1.1.1.8   root      397:                warnx("Ambiguous window name \"%s\": candidates are%s",
                    398:                        name, candstr.c_str());
                    399:                return -1;
1.1       root      400:        }
                    401: }
1.1.1.13  root      402: 
                    403: // candidates (候補の一覧) がすべて basenum .. count 内に入っていれば、
                    404: // その中の空きウィンドウ ID を返す。
                    405: // 空きがなければエラーメッセージを表示して - 1 を返す。
                    406: // candidates の中に basenum .. count 内に入っていないものがあれば 0 を返す。
                    407: // 今の所ここでターゲットにしている人たちに ID=0 はいないので。
                    408: // start は ID_MONITOR_*0、count は MAX_*_MONITOR のほうで、
                    409: // 最初と最後の ID ではないので注意。
                    410: int
1.1.1.15  root      411: WXApp::FindAvailable(const std::vector<uint>& candidates,
1.1.1.13  root      412:        const std::string& name, int start, int count) const
                    413: {
                    414:        // candidates がすべてこの start..end に入っているか
                    415:        bool all = std::all_of(candidates.begin(), candidates.end(),
                    416:                [&](int id) { return (start <= id && id < start + count); }
                    417:        );
                    418: 
                    419:        if (all) {
                    420:                // この時点で空いてるものを返す。
                    421:                for (int i = 0; i < count; i++) {
                    422:                        int id = start + i;
                    423:                        if (WXMainFrame::subwindow_start[id] == false) {
                    424:                                return id;
                    425:                        }
                    426:                }
                    427:                // 空きがなければエラー
                    428:                warnx("No more %s windows available", name.c_str());
                    429:                return -1;
                    430:        }
                    431: 
                    432:        // 候補が他にもあった
                    433:        return 0;
                    434: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.