--- nono/vm/keyboard.cpp 2026/04/29 17:05:11 1.1.1.10 +++ nono/vm/keyboard.cpp 2026/04/29 17:05:25 1.1.1.12 @@ -88,7 +88,7 @@ // MakeKey()/BreakKey() : // | // | KEY_INPUT Message : -// | - - - - - - - - - - - -> KeyInputCallback() +// | - - - - - - - - - - - -> KeyInputMessage() // o | // : MD::KeyInput() // | @@ -101,15 +101,13 @@ #include "keyboard.h" #include "config.h" #include "scheduler.h" -#include "sync.h" +#include "syncer.h" #include "uimessage.h" - -// グローバル参照用 -Keyboard *gKeyboard; +#include // コンストラクタ Keyboard::Keyboard() - : inherited("Keyboard") + : inherited(OBJ_KEYBOARD) { pressed.resize(KC_max); } @@ -117,15 +115,20 @@ Keyboard::Keyboard() // デストラクタ Keyboard::~Keyboard() { - gKeyboard = NULL; } // 初期化 bool Keyboard::Init() { - gScheduler->ConnectMessage(MessageID::KEY_INPUT, this, - ToMessageCallback(&Keyboard::KeyInputCallback)); + if (inherited::Init() == false) { + return false; + } + + syncer = GetSyncer(); + + scheduler->ConnectMessage(MessageID::KEY_INPUT, this, + ToMessageCallback(&Keyboard::KeyInputMessage)); return true; } @@ -143,7 +146,7 @@ Keyboard::ResetHard(bool poweron) } // キー通知 - gSync->RequestKeyPressed(active != 0); + syncer->RequestKeyPressed(Any()); } // 内部状態をリセット @@ -159,10 +162,9 @@ Keyboard::PowerOff() // キーボードから手を離したことにする std::fill(pressed.begin(), pressed.end(), false); - active = 0; // キーが全部解放されたので通知 - gSync->RequestKeyPressed(false); + syncer->RequestKeyPressed(false); DoDisconnect(); } @@ -174,7 +176,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 +186,7 @@ Keyboard::MakeKey(uint keycode) keycode |= KC_FLAG_MAKE; // ここは UI スレッドからも呼ばれるのでメッセージを送る - gScheduler->SendMessage(MessageID::KEY_INPUT, keycode); + scheduler->SendMessage(MessageID::KEY_INPUT, keycode); } // キーを離した (外部から、共通キーコードで呼ばれる) @@ -194,7 +196,7 @@ Keyboard::BreakKey(uint keycode) keycode |= KC_FLAG_BREAK; // ここは UI スレッドからも呼ばれるのでメッセージを送る - gScheduler->SendMessage(MessageID::KEY_INPUT, keycode); + scheduler->SendMessage(MessageID::KEY_INPUT, keycode); } // 文字による入力 @@ -234,7 +236,7 @@ Keyboard::CharInput(uint charcode) // キー入力メッセージコールバック void -Keyboard::KeyInputCallback(MessageID msgid, uint32 arg) +Keyboard::KeyInputMessage(MessageID msgid, uint32 arg) { uint keystat = arg; switch (keystat) { @@ -289,7 +291,7 @@ Keyboard::DoDisconnect() // キー入力を1つ処理する。 // keystat が押下・開放されたキーの物理的な状態。 -// キーを破棄したら false を返す。 +// キー入力を受け取らずに破棄した場合は false を返す。 bool Keyboard::KeyInputCommon(uint keystat) { @@ -338,13 +340,19 @@ Keyboard::KeyInputCommon(uint keystat) } } - // キーが押されてる間は VM の高速実行を抑制する。 + // キーがどれか1つでも押されてる間は VM の高速実行を抑制する。 // すべてのキーが離されたら高速モードを許可する。 - pressed[keycode] = ismake; if (ismake) { - Acquire(); + bool prev = Any(); + pressed[keycode] = true; + if (prev == false) { + syncer->RequestKeyPressed(true); + } } else { - Release(); + pressed[keycode] = false; + if (Any() == false) { + syncer->RequestKeyPressed(false); + } } // キーの状態が変わったので UI に通知。 @@ -384,24 +392,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 +464,7 @@ Keyboard::keyname { "minus", // 2c "circum", // 2d "backslash", // 2e - NULL, // 2f + "", // 2f "OPT1", // 30 "OPT2", // 31