Skip to content

Instantly share code, notes, and snippets.

@matias-eduardo
Last active May 23, 2021 07:30
Show Gist options
  • Save matias-eduardo/7804347beae42a62393db1c38f46c70d to your computer and use it in GitHub Desktop.
Save matias-eduardo/7804347beae42a62393db1c38f46c70d to your computer and use it in GitHub Desktop.
wm_char and sending unicode codepoint event
// window.event.character_input -- // note(matias): not always triggered by keydown
case WM_CHAR: {
State& state = *(State*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
uint32_t ch = (uint32_t)wParam;
// note(matias): searching for utf-16 in wikipedia helps a lot here
// ch.is_printable?
if ((ch >= 32 && ch != 127) || ch == '\t' || ch == '\n' || ch == '\r') {
// ch.in_basic_multilingual_plane? -> single 16-bit code unit
if (ch < 0xd800) {
// note(matias): [enter] comes as either '\r' or '\n' so we normalize to '\n'
if (ch == (uint32_t)'\r') {
ch = (uint32_t)'\n';
}
// todo(matias): reset state.high_surrogate = 0?
// ch.in_high_surrogate_plane? -> save first code unit
} else if (ch >= 0xd800 && ch <= 0xdbff) {
state.high_surrogate = ch;
return 0;
// ch.in_low_surrogate_plane? -> combine both code units
} else if (ch >= 0xdc00 && ch <= 0xdfff) {
// todo(matias): what happens if state.high_surrogate is 0?
uint32_t low_surrogate = ch;
ch = 0x10000;
ch += (state.high_surrogate & 0x03ff) << 10;
ch += low_surrogate & 0x03ff;
// todo(matias): reset state.high_surrogate = 0?
} else [[unlikely]] {
// note(matias): probably an error, so let's just ignore it
return 0;
}
// event.push(character_input)
Event& event = state.events[state.events_count];
event.kind = EventKind_CharacterInput;
event.codepoint = ch;
state.events_count++;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment