Skip to content

Commit

Permalink
add support for non-SDL devices
Browse files Browse the repository at this point in the history
  • Loading branch information
y5nw committed Aug 18, 2024
1 parent 7703253 commit ecb126b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
17 changes: 16 additions & 1 deletion irr/include/IrrlichtDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,13 @@ class IrrlichtDevice : public virtual IReferenceCounted
//! Get the scancode of the corresponding keycode.
virtual u32 getScancodeFromKey(const KeyCode &key) const
{
return key.index() == 0 ? std::get<EKEY_CODE>(key) : KEY_KEY_CODES_COUNT + std::get<wchar_t>(key);
if (key.index() == 0) {
const auto keycode = std::get<EKEY_CODE>(key);
// treat KEY_UNKNOWN and KEY_KEY_CODES_COUNT as the same and return 0.
return KeyCode::isValid(keycode) ? keycode : 0;
}
const auto keychar = std::get<wchar_t>(key);
return keychar == 0 ? 0 : KEY_KEY_CODES_COUNT + keychar;
}

//! Get the keycode of the corresponding scancode.
Expand All @@ -352,6 +358,15 @@ class IrrlichtDevice : public virtual IReferenceCounted
key.emplace<wchar_t>(scancode - KEY_KEY_CODES_COUNT);
return key;
}

protected:

// TODO: This is just some boilerplate for non-SDL devices. Remove this once we fully switch to SDL.
void fillScancode(SEvent &irrevent)
{
auto &keyinput = irrevent.KeyInput;
keyinput.SystemKeyCode = getScancodeFromKey(KeyCode(keyinput.Key, keyinput.Char));
}
};

} // end namespace irr
12 changes: 11 additions & 1 deletion irr/src/CIrrDeviceLinux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -881,10 +881,19 @@ bool CIrrDeviceLinux::run()

irrevent.EventType = irr::EET_KEY_INPUT_EVENT;
irrevent.KeyInput.PressedDown = false;
irrevent.KeyInput.Char = 0; // on release that's undefined
irrevent.KeyInput.Control = (event.xkey.state & ControlMask) != 0;
irrevent.KeyInput.Shift = (event.xkey.state & ShiftMask) != 0;
irrevent.KeyInput.Key = getKeyCode(event);
{ // Pass a char on release as well since this is needed for the scancode stub.
union
{
char buf[8];
wchar_t wbuf[2];
} tmp = {{0}};
XLookupString(&event.xkey, tmp.buf, sizeof(tmp.buf), NULL, NULL);
irrevent.KeyInput.Char = tmp.wbuf[0];
}
fillScancode(irrevent);

postEventFromUser(irrevent);
break;
Expand Down Expand Up @@ -941,6 +950,7 @@ bool CIrrDeviceLinux::run()
irrevent.KeyInput.Control = (event.xkey.state & ControlMask) != 0;
irrevent.KeyInput.Shift = (event.xkey.state & ShiftMask) != 0;
irrevent.KeyInput.Key = getKeyCode(event);
fillScancode(irrevent);
postEventFromUser(irrevent);
} break;

Expand Down
3 changes: 3 additions & 0 deletions irr/src/CIrrDeviceOSX.mm
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ - (BOOL)isQuit

IsShiftDown = ievent.KeyInput.Shift;

fillScancode(ievent);
postEventFromUser(ievent);
}

Expand All @@ -780,6 +781,7 @@ - (BOOL)isQuit

IsControlDown = ievent.KeyInput.Control;

fillScancode(ievent);
postEventFromUser(ievent);
}

Expand Down Expand Up @@ -981,6 +983,7 @@ - (BOOL)isQuit
ievent.KeyInput.Shift = ([(NSEvent *)event modifierFlags] & NSShiftKeyMask) != 0;
ievent.KeyInput.Control = ([(NSEvent *)event modifierFlags] & NSControlKeyMask) != 0;
ievent.KeyInput.Char = mchar;
fillScancode(ievent);

if (skipCommand)
ievent.KeyInput.Control = true;
Expand Down
2 changes: 2 additions & 0 deletions irr/src/CIrrDeviceWin32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
if ((allKeys[VK_MENU] & 0x80) != 0)
event.KeyInput.Control = 0;

fillScancode(event);

dev = getDeviceFromHWnd(hWnd);
if (dev)
dev->postEventFromUser(event);
Expand Down

0 comments on commit ecb126b

Please sign in to comment.