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

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.8   root        7: #include "wxdumpmonitor.h"
1.1       root        8: #include "wxmainframe.h"
                      9: #include "wxmainview.h"
                     10: #include "wxtextpanel.h"
1.1.1.4   root       11: #include "wxversion.h"
1.1       root       12: #include "mainapp.h"
1.1.1.3   root       13: #include "mpu680x0.h"
                     14: #include "mpu88xx0.h"
1.1.1.7   root       15: #include "scheduler.h"
1.1.1.8   root       16: #include <algorithm>
1.1.1.3   root       17: #include <wx/intl.h>
1.1       root       18: 
1.1.1.8   root       19: class WXApp : public wxApp
1.1       root       20: {
1.1.1.6   root       21:        using inherited = wxApp;
1.1       root       22:  public:
                     23:        bool OnInit();
1.1.1.6   root       24:        int OnRun() override;
1.1       root       25:  private:
                     26:        bool ParseMonitors(const wxString& str);
1.1.1.8   root       27:        void ShowMonitorsName();
1.1       root       28:        int SearchMonitorName(const char *name);
1.1.1.3   root       29: 
                     30:        wxLocale locale;
1.1       root       31: };
                     32: 
1.1.1.3   root       33: // IMPLEMENT_APP() は闇マクロすぎて clang のちからには耐えられない
                     34: #if defined(__clang__)
                     35: #pragma clang diagnostic push
                     36: #pragma clang diagnostic ignored "-Weverything"
                     37: #endif
1.1       root       38: IMPLEMENT_APP(WXApp)
1.1.1.3   root       39: #if defined(__clang__)
                     40: #pragma clang diagnostic pop
                     41: #endif
1.1       root       42: 
                     43: bool
                     44: WXApp::OnInit()
                     45: {
1.1.1.6   root       46:        return true;
                     47: }
                     48: 
                     49: int
                     50: WXApp::OnRun()
                     51: {
1.1.1.5   root       52:        int rv;
                     53: 
1.1.1.7   root       54:        rv = gMainApp.Init1(false, argc, argv);
1.1.1.5   root       55:        if (rv != MainApp::PASS) {
1.1.1.6   root       56:                return rv;
1.1       root       57:        }
                     58: 
1.1.1.3   root       59:        // 環境変数 $NONO_BUILD_HOME が定義されていればカタログ検索パスに
                     60:        // $NONO_BUILD_HOME/po を追加する (システムパスより前に挿入される)。
                     61:        // カタログ開発時にワークツリーにあるカタログを見るため。
                     62:        locale.Init(wxLANGUAGE_DEFAULT);
                     63:        const char *build_home = getenv("NONO_BUILD_HOME");
                     64:        if (build_home) {
                     65:                wxString path(build_home);
                     66:                path += "/po";
                     67:                wxLocale::AddCatalogLookupPathPrefix(path);
                     68:        }
                     69:        locale.AddCatalog("nono");
                     70: 
1.1.1.2   root       71:        // --fontsize テキスト系コントロールのフォントサイズ
                     72:        // 紛らわしいが ::global_fontsize は 12, 16 とかいう値。
1.1.1.4   root       73:        // WXTextPanel::global_fontsize (FontId) は enum 値。
                     74:        FontId fontid;
1.1.1.2   root       75:        switch (gMainApp.fontsize) {
                     76:         case 12:
1.1.1.4   root       77:                fontid = FontId::_6x12;
1.1.1.2   root       78:                break;
                     79:         case 16:
1.1.1.4   root       80:                fontid = FontId::_8x16;
1.1.1.2   root       81:                break;
                     82:         default:
                     83:                warnx("invalid fontsize: %d", gMainApp.fontsize);
1.1.1.7   root       84:                return EXIT_FAILURE;
1.1       root       85:        }
1.1.1.4   root       86:        WXTextPanel::global_fontid = fontid;
                     87: 
                     88:        // グローバルオブジェクト (どこでやるのがいいか)
                     89:        gHostInfo.reset(new HostInfoMonitor());
1.1       root       90: 
1.1.1.8   root       91:        // これ以降モニタの登録をしてはいけない。
                     92:        gMonitorManager.Fix();
                     93: 
1.1       root       94:        // -M 起動時に表示するモニタ
1.1.1.2   root       95:        if (!gMainApp.monitor_opt.empty()) {
                     96:                if (!ParseMonitors(gMainApp.monitor_opt)) {
1.1.1.7   root       97:                        return EXIT_FAILURE;
1.1       root       98:                }
                     99:        }
                    100: 
1.1.1.2   root      101:        // -s screen scale
                    102:        WXMainView::screen_scale = gMainApp.screen_scale;
1.1       root      103: 
1.1.1.7   root      104:        // 引数処理が終わったので、スレッド作成を伴う初期化。
                    105:        if (!gMainApp.Init2()) {
                    106:                return EXIT_FAILURE;
                    107:        }
                    108: 
1.1.1.2   root      109:        // メインウィンドウ
                    110:        gMainFrame = new WXMainFrame(NULL);
                    111:        gMainFrame->Show(true);
                    112:        SetTopWindow(gMainFrame);
1.1.1.6   root      113: 
                    114:        return inherited::OnRun();
1.1       root      115: }
                    116: 
                    117: // -M オプションの解析。書式は
1.1.1.8   root      118: //  -M <arg>[,<arg>[,...]]
                    119: // <arg> のどれかが "help" なら有効な名前を列挙して終了。
                    120: // <arg> は通常モニタ名(サブウィンドウ名)のみだが
                    121: // memdump* の場合のみ <name>=<hexaddr> 形式を受け付ける。
                    122: // 継続する場合は true、しない場合は false を返す。
1.1       root      123: bool
                    124: WXApp::ParseMonitors(const wxString& str)
                    125: {
1.1.1.8   root      126:        std::vector<std::string> args = string_split(str, ',');
1.1       root      127: 
1.1.1.8   root      128:        // "help" があればヘルプを表示して終了
                    129:        for (const auto& arg : args) {
                    130:                if (arg == "help") {
                    131:                        ShowMonitorsName();
                    132:                        return false;
1.1       root      133:                }
                    134:        }
                    135: 
1.1.1.8   root      136:        // 一致するモニタ(ウィンドウ)の開始フラグを立てる
                    137:        for (const auto& arg : args) {
                    138:                std::string name;
                    139:                std::string value;
                    140: 
                    141:                auto pos = arg.find('=');
                    142:                if (pos != std::string::npos) {
                    143:                        name = arg.substr(0, pos);
                    144:                        value = arg.substr(++pos);
                    145:                } else {
                    146:                        name = arg;
1.1.1.3   root      147:                }
1.1.1.8   root      148: 
                    149:                // この名前のウィンドウを探して start フラグを立てる
                    150:                int id = SearchMonitorName(name.c_str());
                    151:                if (id < 0) {
1.1       root      152:                        return false;
                    153:                }
1.1.1.8   root      154:                WXMainFrame::subwindow_start[id] = true;
                    155: 
                    156:                // 値付き memdump の処理
                    157:                if (IS_MONITOR_MEMDUMP(id) && value.empty() == false) {
                    158:                        char *end;
                    159:                        errno = 0;
                    160:                        unsigned long u = strtoul(value.c_str(), &end, 16);
                    161:                        if (end == optarg || end[0] != '\0') {
                    162:                                warnx("%s: syntax error on monitor address", value.c_str());
                    163:                                return -1;
                    164:                        }
                    165:                        if (errno == ERANGE || u > 0xffffffff) {
                    166:                                errno = ERANGE;
                    167:                                warn("%s", value.c_str());
                    168:                                return -1;
                    169:                        }
                    170:                        WXMemdumpWindow::SetStickyAddr(id, (uint32)u);
                    171:                }
1.1       root      172:        }
                    173:        return true;
                    174: }
                    175: 
1.1.1.8   root      176: // モニタ名の一覧を表示する
                    177: void
                    178: WXApp::ShowMonitorsName()
                    179: {
                    180:        std::vector<int> list;
                    181: 
                    182:        // モニターは登録されていれば列挙
                    183:        // (登録されているものを全部列挙ではないことに注意)
                    184:        for (int id = ID_MONITOR_START; id <= ID_MONITOR_END; id++) {
                    185:                if (gMonitorManager.Find(id)) {
                    186:                        list.push_back(id);
                    187:                }
                    188:        }
                    189:        // サブウィンドウは機種情報から判断
                    190:        for (int id = ID_SUBWIN_START; id <= ID_SUBWIN_END; id++) {
                    191:                vmf_t feature = gMonitorManager.GetFeature(id);
                    192:                if (gMainApp.HasVMFeature(feature)) {
                    193:                        list.push_back(id);
                    194:                }
                    195:        }
                    196: 
                    197:        // 一覧を表示
                    198:        std::string msg = MonitorManager::MakeListString(list);
                    199:        printf("%s", msg.c_str());
                    200: }
                    201: 
                    202: // モニタ名 name を検索し、一つに確定すればその id を返す。
                    203: // 確定しない、あるいは一致しない場合はエラーメッセージを表示し -1 を返す。
                    204: //
                    205: // "memdump" (数字なし) は空いてる memdump<N> を順に使用する。
                    206: // ただし今の所この処理は1パスで前から順に処理しているだけなので、
                    207: // "memdump0,memdump" は指定可能だが(2つ目が memdump1 になる)、
                    208: // "memdump,memdump0" は memdump0 を2回指定したことになる。
1.1       root      209: int
                    210: WXApp::SearchMonitorName(const char *name)
                    211: {
1.1.1.8   root      212:        std::vector<int> candidates;
                    213:        std::vector<std::string> cand_names;
                    214: 
                    215:        for (int id = ID_MONITOR_START; id <= ID_SUBWIN_END; id++) {
                    216:                if (id < ID_SUBWIN_START) {
                    217:                        // モニターは登録されていなければ除外する
                    218:                        if (gMonitorManager.Find(id) == NULL) {
                    219:                                continue;
                    220:                        }
                    221:                } else {
                    222:                        // サブウィンドウは該当機種でなければ除外する
                    223:                        vmf_t feature = gMonitorManager.GetFeature(id);
                    224:                        if (gMainApp.HasVMFeature(feature) == false) {
                    225:                                continue;
                    226:                        }
                    227:                }
                    228: 
                    229:                const auto& aliases = gMonitorManager.GetAliases(id);
                    230: 
                    231:                bool matched = false;
                    232:                for (const auto& alias : aliases) {
                    233:                        // 全文一致したら確定
                    234:                        // ("lunafb" と "lunafb0" みたいなのがあるため)
                    235:                        if (alias == name) {
                    236:                                return id;
                    237:                        }
                    238: 
                    239:                        // 部分一致したら名前はすべて覚えておく
                    240:                        if (starts_with_ignorecase(alias, name)) {
                    241:                                cand_names.push_back(alias);
                    242:                                matched = true;
                    243:                        }
                    244:                }
                    245:                // 同じオブジェクトで別名が複数回部分一致しても、1回として数える
                    246:                if (matched) {
                    247:                        candidates.push_back(id);
                    248:                }
                    249:        }
                    250: 
                    251:        if (candidates.empty()) {
                    252:                // 一致しない
                    253:                warnx("Unknown window name \"%s\"", name);
                    254:                return -1;
                    255:        } else if (candidates.size() == 1) {
                    256:                // 部分一致したのが1つだけなら確定
                    257:                return candidates[0];
                    258:        } else {
                    259:                // 候補が複数あった
                    260: 
                    261:                // 候補が memdumpN のみなら無記名 memdump とする。
                    262:                bool is_memdump = std::all_of(candidates.begin(), candidates.end(),
                    263:                        [](int x) { return IS_MONITOR_MEMDUMP(x); }
                    264:                );
                    265:                if (is_memdump) {
                    266:                        // この時点で空いてるものを返す
                    267:                        for (int i = 0; i < MAX_MEMDUMP_MONITOR; i++) {
                    268:                                int id = ID_MONITOR_MEMDUMP0 + i;
                    269:                                if (WXMainFrame::subwindow_start[id] == false) {
                    270:                                        return id;
                    271:                                }
                    272:                        }
                    273:                        // 空きがなければエラー
                    274:                        warnx("No more memdump windows available");
                    275:                        return -1;
                    276:                }
                    277: 
                    278:                // そうでなければ、候補を表示
                    279:                std::string candstr;
                    280:                for (const auto& cname : cand_names) {
                    281:                        candstr += ' ';
                    282:                        candstr += cname;
1.1       root      283:                }
1.1.1.8   root      284:                warnx("Ambiguous window name \"%s\": candidates are%s",
                    285:                        name, candstr.c_str());
                    286:                return -1;
1.1       root      287:        }
                    288: }

unix.superglobalmegacorp.com

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