--- nono/vm/keyboard.cpp 2026/04/29 17:05:06 1.1.1.9 +++ nono/vm/keyboard.cpp 2026/04/29 17:05:38 1.1.1.15 @@ -4,6 +4,10 @@ // Licensed under nono-license.txt // +// +// キーボード (基本クラス) +// + // 物理キーボードとソフトウェアキーボードについて // // 物理キーボード入力 ---------->+ @@ -83,9 +87,8 @@ // v // MakeKey()/BreakKey() : // | -// | inputqueue + : -// | keyinput_event -// | - - - - - - - - - - - -> KeyInputCallback() +// | KEY_INPUT Message : +// | - - - - - - - - - - - -> KeyInputMessage() // o | // : MD::KeyInput() // | @@ -96,20 +99,37 @@ // 機種ごとに接続先に送信 #include "keyboard.h" +#include "config.h" #include "scheduler.h" +#include "syncer.h" #include "uimessage.h" +#include + +// +// 文字入力しか持たないキーボードの下位クラス +// + +// コンストラクタ +DumbKeyboard::DumbKeyboard() + : inherited(OBJ_KEYBOARD) +{ +} -std::unique_ptr gKeyboard; +// デストラクタ +DumbKeyboard::~DumbKeyboard() +{ +} + + +// +// 通常のキーボード +// // コンストラクタ Keyboard::Keyboard() - : inherited("Keyboard") + : inherited() { pressed.resize(KC_max); - - keyinput_event.func = (DeviceCallback_t)&Keyboard::KeyInputCallback; - keyinput_event.time = 0; - keyinput_event.SetName("Keyboard Input"); } // デストラクタ @@ -117,42 +137,82 @@ Keyboard::~Keyboard() { } -// リセット +// 初期化 +bool +Keyboard::Init() +{ + syncer = GetSyncer(); + + scheduler->ConnectMessage(MessageID::KEY_INPUT, this, + ToMessageCallback(&Keyboard::KeyInputMessage)); + return true; +} + +// 電源オン/リセット void -Keyboard::ResetHard() +Keyboard::ResetHard(bool poweron) { - // 内部状態を全部リセット + if (poweron) { + is_connected = gConfig->Find("keyboard-connect").AsInt(); + + if (is_connected) { + DoConnect(); + } else { + DoDisconnect(); + } - inputqueue.Clear(); + // キー通知 + syncer->RequestKeyPressed(Any()); + } + + // 内部状態をリセット sendqueue.Clear(); +} + +// 電源オフ +void +Keyboard::PowerOff() +{ + // LED は全部消灯 + std::fill(led.begin(), led.end(), false); + + // キーボードから手を離したことにする std::fill(pressed.begin(), pressed.end(), false); - active = 0; + // キーが全部解放されたので通知 - gScheduler->RequestKeyPressed(false); + syncer->RequestKeyPressed(false); + + DoDisconnect(); +} + +// キーボードを接続する (外部から呼ばれる) +void +Keyboard::Connect(bool connect) +{ + uint code = connect ? CC_connect : CC_disconnect; + + // ここは UI スレッドからも呼ばれるのでメッセージを送る + scheduler->SendMessage(MessageID::KEY_INPUT, code); } // キーを押した (外部から、共通キーコードで呼ばれる) void Keyboard::MakeKey(uint keycode) { - // ここは UI スレッドからも呼ばれるのでキューに突っ込むだけ - { - std::unique_lock lock(mtx); - inputqueue.Enqueue(keycode | KC_FLAG_MAKE); - } - keyinput_event.Start(); + keycode |= KC_FLAG_MAKE; + + // ここは UI スレッドからも呼ばれるのでメッセージを送る + scheduler->SendMessage(MessageID::KEY_INPUT, keycode); } // キーを離した (外部から、共通キーコードで呼ばれる) void Keyboard::BreakKey(uint keycode) { - // ここは UI スレッドからも呼ばれるのでキューに突っ込むだけ - { - std::unique_lock lock(mtx); - inputqueue.Enqueue(keycode | KC_FLAG_BREAK); - } - keyinput_event.Start(); + keycode |= KC_FLAG_BREAK; + + // ここは UI スレッドからも呼ばれるのでメッセージを送る + scheduler->SendMessage(MessageID::KEY_INPUT, keycode); } // 文字による入力 @@ -190,26 +250,64 @@ Keyboard::CharInput(uint charcode) } } -// キー入力コールバック +// キー入力メッセージコールバック void -Keyboard::KeyInputCallback(Event& ev) +Keyboard::KeyInputMessage(MessageID msgid, uint32 arg) { - for (;;) { - uint keystat; - { - std::unique_lock lock(mtx); - if (inputqueue.Empty()) { - return; - } - keystat = inputqueue.Dequeue(); - } + uint keystat = arg; + switch (keystat) { + case CC_connect: + DoConnect(); + break; + case CC_disconnect: + DoDisconnect(); + break; + default: KeyInput(keystat); + break; } } +// キーボードの接続処理の実体。 +void +Keyboard::DoConnect() +{ + is_connected = true; + + MDConnect(); + + // LED は全部消灯で初期化 + std::fill(led.begin(), led.end(), false); + + // 接続されたので押しっぱなしになっているキーをスキャンして送る + for (int i = 0; i < pressed.size(); i++) { + if (pressed[i]) { + KeyInput(i | KC_FLAG_BREAK); + KeyInput(i); + } + } + + // UI に通知する + UIMessage::Post(UIMessage::KEYBOARD); +} + +void +Keyboard::DoDisconnect() +{ + is_connected = false; + + // 電源が切れたので LED は全部消す + std::fill(led.begin(), led.end(), false); + + MDDisconnect(); + + // UI に通知する + UIMessage::Post(UIMessage::KEYBOARD); +} + // キー入力を1つ処理する。 // keystat が押下・開放されたキーの物理的な状態。 -// キーを破棄したら false を返す。 +// キー入力を受け取らずに破棄した場合は false を返す。 bool Keyboard::KeyInputCommon(uint keystat) { @@ -233,7 +331,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 += ")"; @@ -258,13 +356,19 @@ Keyboard::KeyInputCommon(uint keystat) } } - // キーが押されてる間は VM の高速実行を抑制する - // すべてのキーが離されたら高速モードを許可する - pressed[keycode] = ismake; + // キーがどれか1つでも押されてる間は VM の高速実行を抑制する。 + // すべてのキーが離されたら高速モードを許可する。 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 に通知。 @@ -285,6 +389,11 @@ Keyboard::KeyInputCommon(uint keystat) keystat |= KC_FLAG_BREAK; } + // キーボードが接続されていなければここまで処理して false を返す + if (is_connected == false) { + return false; + } + if (ledflag != KC_FLAG_LED_DROP) { // 送信キューに登録 @@ -299,24 +408,12 @@ Keyboard::KeyInputCommon(uint keystat) return true; } -// アクティブカウンタを 1 増やす。 -// 0 -> 1 の遷移でスケジューラに通知する。 -void -Keyboard::Acquire() -{ - if (active++ == 0) { - gScheduler->RequestKeyPressed(true); - } -} - -// アクティブカウンタを 1 減らす。 -// 1 -> 0 の遷移でスケジューラに通知する。 -void -Keyboard::Release() +// キーが1つでも押されていれば true を返す。 +bool +Keyboard::Any() const { - if (--active == 0) { - gScheduler->RequestKeyPressed(false); - } + auto it = std::find(pressed.begin(), pressed.end(), true); + return (it != pressed.end()); } // キー名を返す (主にデバッグ用) @@ -383,7 +480,7 @@ Keyboard::keyname { "minus", // 2c "circum", // 2d "backslash", // 2e - NULL, // 2f + "", // 2f "OPT1", // 30 "OPT2", // 31