--- nono/vm/lunakbd.cpp 2026/04/29 17:04:52 1.1.1.9 +++ nono/vm/lunakbd.cpp 2026/04/29 17:04:59 1.1.1.11 @@ -20,11 +20,8 @@ // コンストラクタ LunaKeyboard::LunaKeyboard() { - monitor_size = nnSize(30, 1); - led.resize(2); - key_event.dev = this; key_event.func = (DeviceCallback_t)&LunaKeyboard::KeyboardCallback; key_event.time = 0; key_event.SetName("Keyboard Slot"); @@ -36,12 +33,16 @@ LunaKeyboard::LunaKeyboard() mouse_event[0].time = 20_msec; mouse_event[0].SetName("Mouse Sampling"); - for (int i = 1; i < 3; i++) { + for (int i = 1; i < mouse_event.size(); i++) { mouse_event[i].dev = this; mouse_event[i].func = (DeviceCallback_t)&LunaKeyboard::MouseCallback; mouse_event[i].time = 2_msec * i; mouse_event[i].SetName(string_format("Mouse Slot%d", i)); } + + monitor.func = (MonitorCallback_t)&LunaKeyboard::MonitorUpdate; + monitor.SetSize(30, 1); + monitor.Regist(ID_MONITOR_KEYBOARD); } // デストラクタ @@ -75,8 +76,8 @@ LunaKeyboard::ResetHard() // 動作中のタイマーは一旦とめる key_event.Stop(); - for (int i = 0; i < countof(mouse_event); i++) { - mouse_event[i].Stop(); + for (auto& ev : mouse_event) { + ev.Stop(); } // で、マウスサンプリングは電源オン時から有効らしい @@ -85,10 +86,10 @@ LunaKeyboard::ResetHard() // モニター void -LunaKeyboard::MonitorUpdate(TextScreen& monitor) +LunaKeyboard::MonitorUpdate(Monitor *, TextScreen& screen) { int x, y; - monitor.Clear(); + screen.Clear(); x = mouse_x; y = mouse_y; @@ -100,7 +101,7 @@ LunaKeyboard::MonitorUpdate(TextScreen& y = -999; if (y > 999) y = 999; - monitor.Print(0, 0, "Mouse: x=%-4d y=%-4d %c %c %c", + screen.Print(0, 0, "Mouse: x=%-4d y=%-4d %c %c %c", x, y, (mouse_l ? 'L' : '-'), (mouse_m ? 'M' : '-'), @@ -126,20 +127,31 @@ LunaKeyboard::IsShift() const return pressed[KC_SHIFT_L] || pressed[KC_SHIFT_R]; } -// キー処理 (MD固有部分) -bool -LunaKeyboard::ProcessKey(uint keystat) +// キー入力を1つ処理する +void +LunaKeyboard::KeyInput(uint keystat) { - if (KC_IS_MAKE(keystat)) { + uint keycode = KC_CODE(keystat); + bool ismake = KC_IS_MAKE(keystat); + + assert(keycode < KC_max); + + // 共通処理へ + if (KeyInputCommon(keystat) == false) { + return; + } + + // LED キーの処理 + if (ismake) { // LED はトグル? - uint keycode = KC_CODE(keystat); int n = Keycode2LED(keycode); if (n >= 0) { led[n] = !led[n]; } } - return true; + // LUNA のキーボードはキーボード側でキーリピートしないので + // ここでは何もしなくてよい } // 共通キーコードから LED 番号を返す。LED キーでなければ -1 を返す。 @@ -257,74 +269,64 @@ LunaKeyboard::KeyboardCallback(Event& ev void LunaKeyboard::MouseSamplingCallback(Event& ev) { - int reset_x = 0; - int reset_y = 0; - - // 移動量±1は送信しないが次回に向けた積算は続けるそうだ。 - if (mouse_x == -1 || mouse_x == 1) { - reset_x = mouse_x; - mouse_x = 0; - } - if (mouse_y == -1 || mouse_y == 1) { - reset_y = mouse_y; - mouse_y = 0; - } - // 移動量が±1 を超えるかボタンがどれかでも変化すれば送信。 - if (mouse_x != 0 || - mouse_y != 0 || + if (mouse_x < -1 || mouse_x > 1 || + mouse_y < -1 || mouse_y > 1 || mouse_r != prev_r || mouse_l != prev_l || mouse_m != prev_m) { - putlog(2, "Mouse Sampling x=%d y=%d %c%c%c", - mouse_x, mouse_y, - (mouse_r ? 'L' : '-'), - (mouse_m ? 'M' : '-'), - (mouse_l ? 'R' : '-')); - - // 1バイト目、ボタン状態。 - // mouse_[rlm] 変数は押し下げが true、LUNA のマウスは押し下げが %0 - uint8 b = 0x80 | - (mouse_l ? 0 : 0x04) | - (mouse_m ? 0 : 0x02) | - (mouse_r ? 0 : 0x01); - - // 2バイト目が X、3バイト目が Y。 - // 移動量は絶対値 0x7f でサチる (signed char ではない)。 - if (mouse_x < -127) - mouse_x = -127; - if (mouse_x > 127) - mouse_x = 127; - - if (mouse_y < -127) - mouse_y = -127; - if (mouse_y > 127) - mouse_y = 127; - - // 1バイト目はここで送信 - gSIO->Rx(1, b); - - // 2バイト目は 2msec 秒後に送信 - mouse_event[1].code = (uint8)mouse_x; - mouse_event[1].Start(); - - // 3バイト目は 4msec 秒後に送信 - mouse_event[2].code = (uint8)mouse_y; - mouse_event[2].Start(); - } else { - putlog(3, "Mouse Sampling inactive"); + MouseSendStart(); } + // 20msec 後に再び自分を呼び出す + ev.Start(); +} + +void +LunaKeyboard::MouseSendStart() +{ + putlog(2, "Mouse Sampling x=%d y=%d %c%c%c", + mouse_x, mouse_y, + (mouse_r ? 'L' : '-'), + (mouse_m ? 'M' : '-'), + (mouse_l ? 'R' : '-')); + + // 1バイト目、ボタン状態。 + // mouse_[rlm] 変数は押し下げが true、LUNA のマウスは押し下げが %0 + uint8 b = 0x80 | + (mouse_l ? 0 : 0x04) | + (mouse_m ? 0 : 0x02) | + (mouse_r ? 0 : 0x01); + + // 2バイト目が X、3バイト目が Y。 + // 移動量は絶対値 0x7f でサチる (signed char ではない)。 + if (mouse_x < -127) + mouse_x = -127; + if (mouse_x > 127) + mouse_x = 127; + + if (mouse_y < -127) + mouse_y = -127; + if (mouse_y > 127) + mouse_y = 127; + + // 1バイト目はここで送信 + gSIO->Rx(1, b); + + // 2バイト目は 2msec 秒後に送信 + mouse_event[1].code = (uint8)mouse_x; + mouse_event[1].Start(); + + // 3バイト目は 4msec 秒後に送信 + mouse_event[2].code = (uint8)mouse_y; + mouse_event[2].Start(); // 積算リセット - mouse_x = reset_x; - mouse_y = reset_y; + mouse_x = 0; + mouse_y = 0; prev_r = mouse_r; prev_l = mouse_l; prev_m = mouse_m; - - // 20msec 後に再び自分を呼び出す - ev.Start(); } // マウスの後続バイトを送出するイベントコールバック