--- nono/vm/keyboard.cpp 2026/04/29 17:05:11 1.1.1.10 +++ nono/vm/keyboard.cpp 2026/04/29 17:06:01 1.1.1.19 @@ -88,7 +88,7 @@ // MakeKey()/BreakKey() : // | // | KEY_INPUT Message : -// | - - - - - - - - - - - -> KeyInputCallback() +// | - - - - - - - - - - - -> KeyInputMessage() // o | // : MD::KeyInput() // | @@ -100,16 +100,35 @@ #include "keyboard.h" #include "config.h" +#include "mainapp.h" #include "scheduler.h" -#include "sync.h" +#include "syncer.h" #include "uimessage.h" +#include -// グローバル参照用 -Keyboard *gKeyboard; +// +// 文字入力しか持たないキーボードの下位クラス +// + +// コンストラクタ +DumbKeyboard::DumbKeyboard() + : inherited(OBJ_KEYBOARD) +{ +} + +// デストラクタ +DumbKeyboard::~DumbKeyboard() +{ +} + + +// +// 通常のキーボード +// // コンストラクタ Keyboard::Keyboard() - : inherited("Keyboard") + : inherited() { pressed.resize(KC_max); } @@ -117,15 +136,20 @@ Keyboard::Keyboard() // デストラクタ Keyboard::~Keyboard() { - gKeyboard = NULL; } // 初期化 bool Keyboard::Init() { - gScheduler->ConnectMessage(MessageID::KEY_INPUT, this, - ToMessageCallback(&Keyboard::KeyInputCallback)); + // LUNA はキーリピートをソフトウェアで処理するため。 + // X68030 も Human68k を高速モードで使う際に必要。 + syncer = GetSyncer(); + + uimessage = gMainApp.GetUIMessage(); + + scheduler->ConnectMessage(MessageID::KEY_INPUT, this, + ToMessageCallback(&Keyboard::KeyInputMessage)); return true; } @@ -134,7 +158,7 @@ void Keyboard::ResetHard(bool poweron) { if (poweron) { - is_connected = gConfig->Find("keyboard-connect").AsInt(); + is_connected = gConfig->Find("keyboard-connect").AsBool(); if (is_connected) { DoConnect(); @@ -143,7 +167,7 @@ Keyboard::ResetHard(bool poweron) } // キー通知 - gSync->RequestKeyPressed(active != 0); + syncer->NotifyKeyPressed(Any()); } // 内部状態をリセット @@ -159,10 +183,9 @@ Keyboard::PowerOff() // キーボードから手を離したことにする std::fill(pressed.begin(), pressed.end(), false); - active = 0; // キーが全部解放されたので通知 - gSync->RequestKeyPressed(false); + syncer->NotifyKeyPressed(false); DoDisconnect(); } @@ -174,7 +197,7 @@ Keyboard::Connect(bool connect) uint code = connect ? CC_connect : CC_disconnect; // ここは UI スレッドからも呼ばれるのでメッセージを送る - gScheduler->SendMessage(MessageID::KEY_INPUT, code); + scheduler->SendMessage(MessageID::KEY_INPUT, code); } // キーを押した (外部から、共通キーコードで呼ばれる) @@ -184,7 +207,7 @@ Keyboard::MakeKey(uint keycode) keycode |= KC_FLAG_MAKE; // ここは UI スレッドからも呼ばれるのでメッセージを送る - gScheduler->SendMessage(MessageID::KEY_INPUT, keycode); + scheduler->SendMessage(MessageID::KEY_INPUT, keycode); } // キーを離した (外部から、共通キーコードで呼ばれる) @@ -194,7 +217,7 @@ Keyboard::BreakKey(uint keycode) keycode |= KC_FLAG_BREAK; // ここは UI スレッドからも呼ばれるのでメッセージを送る - gScheduler->SendMessage(MessageID::KEY_INPUT, keycode); + scheduler->SendMessage(MessageID::KEY_INPUT, keycode); } // 文字による入力 @@ -234,7 +257,7 @@ Keyboard::CharInput(uint charcode) // キー入力メッセージコールバック void -Keyboard::KeyInputCallback(MessageID msgid, uint32 arg) +Keyboard::KeyInputMessage(MessageID msgid, uint32 arg) { uint keystat = arg; switch (keystat) { @@ -270,7 +293,7 @@ Keyboard::DoConnect() } // UI に通知する - UIMessage::Post(UIMessage::KEYBOARD); + uimessage->Post(UIMessage::KEYBOARD); } void @@ -284,12 +307,33 @@ Keyboard::DoDisconnect() MDDisconnect(); // UI に通知する - UIMessage::Post(UIMessage::KEYBOARD); + uimessage->Post(UIMessage::KEYBOARD); +} + +// キーの押し下げ状態を設定する。 +// 初期化時以外で pressed を変更するときはこの関数を使用すること。 +void +Keyboard::SetPressed(uint keycode, bool ismake) +{ + // キーがどれか1つでも押されてる間は VM の高速実行を抑制する。 + // すべてのキーが離されたら高速モードを許可する。 + if (ismake) { + bool prev = Any(); + pressed[keycode] = true; + if (prev == false) { + syncer->NotifyKeyPressed(true); + } + } else { + pressed[keycode] = false; + if (Any() == false) { + syncer->NotifyKeyPressed(false); + } + } } // キー入力を1つ処理する。 // keystat が押下・開放されたキーの物理的な状態。 -// キーを破棄したら false を返す。 +// キー入力を受け取らずに破棄した場合は false を返す。 bool Keyboard::KeyInputCommon(uint keystat) { @@ -313,7 +357,7 @@ Keyboard::KeyInputCommon(uint keystat) if (loglevel >= 1) { std::string msg; - msg += string_format("%d(", keycode); + msg += string_format("%u(", keycode); msg += GetKeyName(keycode); msg += ")"; @@ -338,14 +382,7 @@ Keyboard::KeyInputCommon(uint keystat) } } - // キーが押されてる間は VM の高速実行を抑制する。 - // すべてのキーが離されたら高速モードを許可する。 - pressed[keycode] = ismake; - if (ismake) { - Acquire(); - } else { - Release(); - } + SetPressed(keycode, ismake); // キーの状態が変わったので UI に通知。 // KC_FLAG_POST が立っていればキーコードも送信 (char 入力モード用)。 @@ -358,7 +395,7 @@ Keyboard::KeyInputCommon(uint keystat) } else { arg = 0; } - UIMessage::Post(UIMessage::KEYBOARD, arg); + uimessage->Post(UIMessage::KEYBOARD, arg); // LUNA の LED キー消灯はキー開放を送信 if (ledflag == KC_FLAG_LED_BREAK) { @@ -384,24 +421,12 @@ Keyboard::KeyInputCommon(uint keystat) return true; } -// アクティブカウンタを 1 増やす。 -// 0 -> 1 の遷移でスケジューラに通知する。 -void -Keyboard::Acquire() -{ - if (active++ == 0) { - gSync->RequestKeyPressed(true); - } -} - -// アクティブカウンタを 1 減らす。 -// 1 -> 0 の遷移でスケジューラに通知する。 -void -Keyboard::Release() +// キーが1つでも押されていれば true を返す。 +bool +Keyboard::Any() const { - if (--active == 0) { - gSync->RequestKeyPressed(false); - } + auto it = std::find(pressed.begin(), pressed.end(), true); + return (it != pressed.end()); } // キー名を返す (主にデバッグ用) @@ -421,7 +446,7 @@ Keyboard::GetKeyName(uint keycode) Keyboard::keyname { "(none)", // 00 "BREAK", // 01 - "COPY", // 02 + NULL, // 02 "romaji", // 03 "code", // 04 "kigou", // 05 @@ -439,7 +464,7 @@ Keyboard::keyname { "ESC", // 10 "BS", // 11 "enter", // 12 - "INS", // 13 + NULL, // 13 "space", // 14 "DEL", // 15 "XFER", // 16 @@ -453,8 +478,8 @@ Keyboard::keyname { "right", // 1e "down", // 1f - "ROLLUP", // 20 - "ROLLDOWN", // 21 + "INS", // 20 + "COPY", // 21 "1", // 22 "2", // 23 "3", // 24 @@ -468,10 +493,10 @@ Keyboard::keyname { "minus", // 2c "circum", // 2d "backslash", // 2e - NULL, // 2f + "", // 2f - "OPT1", // 30 - "OPT2", // 31 + "CUT", // 30 + "PASTE", // 31 "Q", // 32 "W", // 33 "E", // 34 @@ -487,7 +512,7 @@ Keyboard::keyname { "hira", // 3e "zenkaku", // 3f - "HOME", // 40 + NULL, // 40 "UNDO", // 41 "A", // 42 "S", // 43 @@ -501,8 +526,8 @@ Keyboard::keyname { "semicolon", // 4b "colon", // 4c "bracketright", // 4d - NULL, // 4e - NULL, // 4f + "OPT1", // 4e + "OPT2", // 4f "XF1", // 50 "XF2", // 51 @@ -521,7 +546,7 @@ Keyboard::keyname { "XF4", // 5e "XF5", // 5f - NULL, // 60 + "HOME", // 60 "PAD_plus", // 61 "PAD_minus", // 62 "PAD_7", // 63 @@ -538,8 +563,8 @@ Keyboard::keyname { "PAD_enter", // 6e "PAD_CLR", // 6f - NULL, // 70 - NULL, // 71 + "ROLLUP", // 70 + "ROLLDOWN", // 71 "F1", // 72 "F2", // 73 "F3", // 74