--- nono/vm/keyboard.cpp 2026/04/29 17:05:11 1.1.1.10 +++ nono/vm/keyboard.cpp 2026/04/29 17:05:43 1.1.1.16 @@ -88,7 +88,7 @@ // MakeKey()/BreakKey() : // | // | KEY_INPUT Message : -// | - - - - - - - - - - - -> KeyInputCallback() +// | - - - - - - - - - - - -> KeyInputMessage() // o | // : MD::KeyInput() // | @@ -101,15 +101,33 @@ #include "keyboard.h" #include "config.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 +135,16 @@ Keyboard::Keyboard() // デストラクタ Keyboard::~Keyboard() { - gKeyboard = NULL; } // 初期化 bool Keyboard::Init() { - gScheduler->ConnectMessage(MessageID::KEY_INPUT, this, - ToMessageCallback(&Keyboard::KeyInputCallback)); + syncer = GetSyncer(); + + scheduler->ConnectMessage(MessageID::KEY_INPUT, this, + ToMessageCallback(&Keyboard::KeyInputMessage)); return true; } @@ -143,7 +162,7 @@ Keyboard::ResetHard(bool poweron) } // キー通知 - gSync->RequestKeyPressed(active != 0); + syncer->RequestKeyPressed(Any()); } // 内部状態をリセット @@ -159,10 +178,9 @@ Keyboard::PowerOff() // キーボードから手を離したことにする std::fill(pressed.begin(), pressed.end(), false); - active = 0; // キーが全部解放されたので通知 - gSync->RequestKeyPressed(false); + syncer->RequestKeyPressed(false); DoDisconnect(); } @@ -174,7 +192,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 +202,7 @@ Keyboard::MakeKey(uint keycode) keycode |= KC_FLAG_MAKE; // ここは UI スレッドからも呼ばれるのでメッセージを送る - gScheduler->SendMessage(MessageID::KEY_INPUT, keycode); + scheduler->SendMessage(MessageID::KEY_INPUT, keycode); } // キーを離した (外部から、共通キーコードで呼ばれる) @@ -194,7 +212,7 @@ Keyboard::BreakKey(uint keycode) keycode |= KC_FLAG_BREAK; // ここは UI スレッドからも呼ばれるのでメッセージを送る - gScheduler->SendMessage(MessageID::KEY_INPUT, keycode); + scheduler->SendMessage(MessageID::KEY_INPUT, keycode); } // 文字による入力 @@ -234,7 +252,7 @@ Keyboard::CharInput(uint charcode) // キー入力メッセージコールバック void -Keyboard::KeyInputCallback(MessageID msgid, uint32 arg) +Keyboard::KeyInputMessage(MessageID msgid, uint32 arg) { uint keystat = arg; switch (keystat) { @@ -287,9 +305,30 @@ Keyboard::DoDisconnect() 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->RequestKeyPressed(true); + } + } else { + pressed[keycode] = false; + if (Any() == false) { + syncer->RequestKeyPressed(false); + } + } +} + // キー入力を1つ処理する。 // keystat が押下・開放されたキーの物理的な状態。 -// キーを破棄したら false を返す。 +// キー入力を受け取らずに破棄した場合は false を返す。 bool Keyboard::KeyInputCommon(uint keystat) { @@ -313,7 +352,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 +377,7 @@ Keyboard::KeyInputCommon(uint keystat) } } - // キーが押されてる間は VM の高速実行を抑制する。 - // すべてのキーが離されたら高速モードを許可する。 - pressed[keycode] = ismake; - if (ismake) { - Acquire(); - } else { - Release(); - } + SetPressed(keycode, ismake); // キーの状態が変わったので UI に通知。 // KC_FLAG_POST が立っていればキーコードも送信 (char 入力モード用)。 @@ -384,24 +416,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()); } // キー名を返す (主にデバッグ用) @@ -468,7 +488,7 @@ Keyboard::keyname { "minus", // 2c "circum", // 2d "backslash", // 2e - NULL, // 2f + "", // 2f "OPT1", // 30 "OPT2", // 31