improve cef keyboard handling

this is a rough approximation of the handling of various keycodes/syms
within chromium. it needs work, but for my system it works okay
This commit is contained in:
Mike Blumenkrantz 2015-05-21 15:52:16 -04:00
parent efc75ef40e
commit 28aed52a4b

View file

@ -16,6 +16,7 @@ use libc::{c_double, c_int};
use msg::constellation_msg::{self, KeyModifiers, KeyState};
use script_traits::MouseButton;
use std::cell::{Cell, RefCell};
use std::mem::transmute;
pub struct ServoCefBrowserHost {
/// A reference to the browser.
@ -26,6 +27,337 @@ pub struct ServoCefBrowserHost {
pub composite_ok: Cell<bool>,
}
// From blink ui/events/keycodes/keyboard_codes_posix.h.
#[allow(dead_code)]
enum KeyboardCode {
VKEY_BACK = 0x08,
VKEY_TAB = 0x09,
VKEY_BACKTAB = 0x0A,
VKEY_CLEAR = 0x0C,
VKEY_RETURN = 0x0D,
VKEY_SHIFT = 0x10,
VKEY_CONTROL = 0x11,
VKEY_MENU = 0x12,
VKEY_PAUSE = 0x13,
VKEY_CAPITAL = 0x14,
VKEY_KANA = 0x15,
//VKEY_HANGUL = 0x15,
VKEY_JUNJA = 0x17,
VKEY_FINAL = 0x18,
VKEY_HANJA = 0x19,
//VKEY_KANJI = 0x19,
VKEY_ESCAPE = 0x1B,
VKEY_CONVERT = 0x1C,
VKEY_NONCONVERT = 0x1D,
VKEY_ACCEPT = 0x1E,
VKEY_MODECHANGE = 0x1F,
VKEY_SPACE = 0x20,
VKEY_PRIOR = 0x21,
VKEY_NEXT = 0x22,
VKEY_END = 0x23,
VKEY_HOME = 0x24,
VKEY_LEFT = 0x25,
VKEY_UP = 0x26,
VKEY_RIGHT = 0x27,
VKEY_DOWN = 0x28,
VKEY_SELECT = 0x29,
VKEY_PRINT = 0x2A,
VKEY_EXECUTE = 0x2B,
VKEY_SNAPSHOT = 0x2C,
VKEY_INSERT = 0x2D,
VKEY_DELETE = 0x2E,
VKEY_HELP = 0x2F,
VKEY_0 = 0x30,
VKEY_1 = 0x31,
VKEY_2 = 0x32,
VKEY_3 = 0x33,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Y_OEM_103 = 0xE3, // GTV KEYCODE_MEDIA_REWIND
VKEY_OEM_104 = 0xE4, // GTV KEYCODE_MEDIA_FAST_FORWARD
VKEY_PROCESSKEY = 0xE5,
VKEY_PACKET = 0xE7,
VKEY_DBE_SBCSCHAR = 0xF3,
VKEY_DBE_DBCSCHAR = 0xF4,
VKEY_ATTN = 0xF6,
VKEY_CRSEL = 0xF7,
VKEY_EXSEL = 0xF8,
VKEY_EREOF = 0xF9,
VKEY_PLAY = 0xFA,
VKEY_ZOOM = 0xFB,
VKEY_NONAME = 0xFC,
VKEY_PA1 = 0xFD,
VKEY_OEM_CLEAR = 0xFE,
VKEY_UNKNOWN = 0,
// POSIX specific VKEYs. Note that as of Windows SDK 7.1, 0x97-9F, 0xD8-DA,
// and 0xE8 are unassigned.
VKEY_WLAN = 0x97,
VKEY_POWER = 0x98,
VKEY_BRIGHTNESS_DOWN = 0xD8,
VKEY_BRIGHTNESS_UP = 0xD9,
VKEY_KBD_BRIGHTNESS_DOWN = 0xDA,
VKEY_KBD_BRIGHTNESS_UP = 0xE8,
// Windows does not have a specific key code for AltGr. We use the unused 0xE1
// (VK_OEM_AX) code to represent AltGr, matching the behaviour of Firefox on
// Linux.
VKEY_ALTGR = 0xE1,
// Windows does not have a specific key code for Compose. We use the unused
// 0xE6 (VK_ICO_CLEAR) code to represent Compose.
VKEY_COMPOSE = 0xE6,
}
// this is way too much work to do 100% correctly right now.
// see xkb_keyboard_layout_engine.cc -> XkbKeyboardLayoutEngine::Lookup in chromium for details
fn get_key_msg(keycode: c_int, character: u16) -> Option<constellation_msg::Key> {
let code: KeyboardCode = unsafe { transmute(keycode as u8) };
match code {
KeyboardCode::VKEY_BACK => Some(constellation_msg::Key::Backspace),
KeyboardCode::VKEY_RIGHT => Some(constellation_msg::Key::Right),
KeyboardCode::VKEY_LEFT => Some(constellation_msg::Key::Left),
KeyboardCode::VKEY_UP => Some(constellation_msg::Key::Up),
KeyboardCode::VKEY_DOWN => Some(constellation_msg::Key::Down),
KeyboardCode::VKEY_RSHIFT => Some(constellation_msg::Key::RightShift),
KeyboardCode::VKEY_SHIFT | KeyboardCode::VKEY_LSHIFT => Some(constellation_msg::Key::LeftShift),
KeyboardCode::VKEY_RCONTROL => Some(constellation_msg::Key::RightControl),
KeyboardCode::VKEY_CONTROL | KeyboardCode::VKEY_LCONTROL => Some(constellation_msg::Key::LeftControl),
KeyboardCode::VKEY_LWIN => Some(constellation_msg::Key::LeftSuper),
KeyboardCode::VKEY_RWIN => Some(constellation_msg::Key::RightSuper),
KeyboardCode::VKEY_MENU => Some(constellation_msg::Key::LeftAlt),
KeyboardCode::VKEY_APPS => Some(constellation_msg::Key::Menu),
KeyboardCode::VKEY_ALTGR => Some(constellation_msg::Key::RightAlt), //not sure if correct...
KeyboardCode::VKEY_ESCAPE => Some(constellation_msg::Key::Escape),
KeyboardCode::VKEY_INSERT => Some(constellation_msg::Key::Insert),
KeyboardCode::VKEY_DELETE => Some(constellation_msg::Key::Delete),
KeyboardCode::VKEY_NEXT => Some(constellation_msg::Key::PageUp),
KeyboardCode::VKEY_PRIOR => Some(constellation_msg::Key::PageDown),
KeyboardCode::VKEY_HOME => Some(constellation_msg::Key::Home),
KeyboardCode::VKEY_END => Some(constellation_msg::Key::End),
KeyboardCode::VKEY_CAPITAL => Some(constellation_msg::Key::CapsLock),
KeyboardCode::VKEY_F1 => Some(constellation_msg::Key::F1),
KeyboardCode::VKEY_F2 => Some(constellation_msg::Key::F2),
KeyboardCode::VKEY_F3 => Some(constellation_msg::Key::F3),
KeyboardCode::VKEY_F4 => Some(constellation_msg::Key::F4),
KeyboardCode::VKEY_F5 => Some(constellation_msg::Key::F5),
KeyboardCode::VKEY_F6 => Some(constellation_msg::Key::F6),
KeyboardCode::VKEY_F7 => Some(constellation_msg::Key::F7),
KeyboardCode::VKEY_F8 => Some(constellation_msg::Key::F8),
KeyboardCode::VKEY_F9 => Some(constellation_msg::Key::F9),
KeyboardCode::VKEY_F10 => Some(constellation_msg::Key::F10),
KeyboardCode::VKEY_F11 => Some(constellation_msg::Key::F11),
KeyboardCode::VKEY_F12 => Some(constellation_msg::Key::F12),
KeyboardCode::VKEY_F13 => Some(constellation_msg::Key::F13),
KeyboardCode::VKEY_F14 => Some(constellation_msg::Key::F14),
KeyboardCode::VKEY_F15 => Some(constellation_msg::Key::F15),
KeyboardCode::VKEY_F16 => Some(constellation_msg::Key::F16),
KeyboardCode::VKEY_F17 => Some(constellation_msg::Key::F17),
KeyboardCode::VKEY_F18 => Some(constellation_msg::Key::F18),
KeyboardCode::VKEY_F19 => Some(constellation_msg::Key::F19),
KeyboardCode::VKEY_F20 => Some(constellation_msg::Key::F20),
KeyboardCode::VKEY_F21 => Some(constellation_msg::Key::F21),
KeyboardCode::VKEY_F22 => Some(constellation_msg::Key::F22),
KeyboardCode::VKEY_F23 => Some(constellation_msg::Key::F23),
KeyboardCode::VKEY_F24 => Some(constellation_msg::Key::F24),
KeyboardCode::VKEY_NUMPAD0 => Some(constellation_msg::Key::Kp0),
KeyboardCode::VKEY_NUMPAD1 => Some(constellation_msg::Key::Kp1),
KeyboardCode::VKEY_NUMPAD2 => Some(constellation_msg::Key::Kp2),
KeyboardCode::VKEY_NUMPAD3 => Some(constellation_msg::Key::Kp3),
KeyboardCode::VKEY_NUMPAD4 => Some(constellation_msg::Key::Kp4),
KeyboardCode::VKEY_NUMPAD5 => Some(constellation_msg::Key::Kp5),
KeyboardCode::VKEY_NUMPAD6 => Some(constellation_msg::Key::Kp6),
KeyboardCode::VKEY_NUMPAD7 => Some(constellation_msg::Key::Kp7),
KeyboardCode::VKEY_NUMPAD8 => Some(constellation_msg::Key::Kp8),
KeyboardCode::VKEY_NUMPAD9 => Some(constellation_msg::Key::Kp9),
KeyboardCode::VKEY_DECIMAL => Some(constellation_msg::Key::KpDecimal),
KeyboardCode::VKEY_DIVIDE => Some(constellation_msg::Key::KpDivide),
KeyboardCode::VKEY_MULTIPLY => Some(constellation_msg::Key::KpMultiply),
KeyboardCode::VKEY_SUBTRACT => Some(constellation_msg::Key::KpSubtract),
KeyboardCode::VKEY_ADD => Some(constellation_msg::Key::KpAdd),
KeyboardCode::VKEY_NUMLOCK => Some(constellation_msg::Key::NumLock),
KeyboardCode::VKEY_PRINT => Some(constellation_msg::Key::PrintScreen),
KeyboardCode::VKEY_PAUSE => Some(constellation_msg::Key::Pause),
//VKEY_BACK
_ => { match character as u8 {
b'[' => Some(constellation_msg::Key::LeftBracket),
b']' => Some(constellation_msg::Key::RightBracket),
b'=' => Some(constellation_msg::Key::Equal),
b';' => Some(constellation_msg::Key::Semicolon),
b'/' => Some(constellation_msg::Key::Slash),
b'.' => Some(constellation_msg::Key::Period),
b'-' => Some(constellation_msg::Key::Minus),
b',' => Some(constellation_msg::Key::Comma),
b'\'' => Some(constellation_msg::Key::Apostrophe),
b'\\' => Some(constellation_msg::Key::Backslash),
b'`' => Some(constellation_msg::Key::GraveAccent),
b'\t' => Some(constellation_msg::Key::Tab),
b'a' | b'A' => Some(constellation_msg::Key::A),
b'b' | b'B' => Some(constellation_msg::Key::B),
b'c' | b'C' => Some(constellation_msg::Key::C),
b'd' | b'D' => Some(constellation_msg::Key::D),
b'e' | b'E' => Some(constellation_msg::Key::E),
b'f' | b'F' => Some(constellation_msg::Key::F),
b'g' | b'G' => Some(constellation_msg::Key::G),
b'h' | b'H' => Some(constellation_msg::Key::H),
b'i' | b'I' => Some(constellation_msg::Key::I),
b'j' | b'J' => Some(constellation_msg::Key::J),
b'k' | b'K' => Some(constellation_msg::Key::K),
b'l' | b'L' => Some(constellation_msg::Key::L),
b'm' | b'M' => Some(constellation_msg::Key::M),
b'n' | b'N' => Some(constellation_msg::Key::N),
b'o' | b'O' => Some(constellation_msg::Key::O),
b'p' | b'P' => Some(constellation_msg::Key::P),
b'q' | b'Q' => Some(constellation_msg::Key::Q),
b'r' | b'R' => Some(constellation_msg::Key::R),
b's' | b'S' => Some(constellation_msg::Key::S),
b't' | b'T' => Some(constellation_msg::Key::T),
b'u' | b'U' => Some(constellation_msg::Key::U),
b'v' | b'V' => Some(constellation_msg::Key::V),
b'w' | b'W' => Some(constellation_msg::Key::W),
b'x' | b'X' => Some(constellation_msg::Key::X),
b'y' | b'Y' => Some(constellation_msg::Key::Y),
b'z' | b'Z' => Some(constellation_msg::Key::Z),
b'0' => Some(constellation_msg::Key::Num0),
b'1' => Some(constellation_msg::Key::Num1),
b'2' => Some(constellation_msg::Key::Num2),
b'3' => Some(constellation_msg::Key::Num3),
b'4' => Some(constellation_msg::Key::Num4),
b'5' => Some(constellation_msg::Key::Num5),
b'6' => Some(constellation_msg::Key::Num6),
b'7' => Some(constellation_msg::Key::Num7),
b'8' => Some(constellation_msg::Key::Num8),
b'9' => Some(constellation_msg::Key::Num9),
b'\n' | b'\r' => Some(constellation_msg::Key::Enter),
b' ' => Some(constellation_msg::Key::Space),
_ => None
}
}
}
}
// unhandled
//pub enum Key {
//World1,
//World2,
//ScrollLock,
//KpEnter,
//KpEqual,
//RightAlt,
//}
full_cef_class_impl! {
ServoCefBrowserHost : CefBrowserHost, cef_browser_host_t {
fn get_client(&this,) -> *mut cef_client_t {{
@ -67,48 +399,13 @@ full_cef_class_impl! {
}}
fn send_key_event(&this, event: *const cef_key_event [&cef_key_event],) -> () {{
// FIXME(pcwalton): So awful. But it's nearly midnight here and I have to get
// Google working.
let event: &cef_key_event = event;
let key = match (*event).character as u8 {
b'a' | b'A' => constellation_msg::Key::A,
b'b' | b'B' => constellation_msg::Key::B,
b'c' | b'C' => constellation_msg::Key::C,
b'd' | b'D' => constellation_msg::Key::D,
b'e' | b'E' => constellation_msg::Key::E,
b'f' | b'F' => constellation_msg::Key::F,
b'g' | b'G' => constellation_msg::Key::G,
b'h' | b'H' => constellation_msg::Key::H,
b'i' | b'I' => constellation_msg::Key::I,
b'j' | b'J' => constellation_msg::Key::J,
b'k' | b'K' => constellation_msg::Key::K,
b'l' | b'L' => constellation_msg::Key::L,
b'm' | b'M' => constellation_msg::Key::M,
b'n' | b'N' => constellation_msg::Key::N,
b'o' | b'O' => constellation_msg::Key::O,
b'p' | b'P' => constellation_msg::Key::P,
b'q' | b'Q' => constellation_msg::Key::Q,
b'r' | b'R' => constellation_msg::Key::R,
b's' | b'S' => constellation_msg::Key::S,
b't' | b'T' => constellation_msg::Key::T,
b'u' | b'U' => constellation_msg::Key::U,
b'v' | b'V' => constellation_msg::Key::V,
b'w' | b'W' => constellation_msg::Key::W,
b'x' | b'X' => constellation_msg::Key::X,
b'y' | b'Y' => constellation_msg::Key::Y,
b'z' | b'Z' => constellation_msg::Key::Z,
b'0' => constellation_msg::Key::Num0,
b'1' => constellation_msg::Key::Num1,
b'2' => constellation_msg::Key::Num2,
b'3' => constellation_msg::Key::Num3,
b'4' => constellation_msg::Key::Num4,
b'5' => constellation_msg::Key::Num5,
b'6' => constellation_msg::Key::Num6,
b'7' => constellation_msg::Key::Num7,
b'8' => constellation_msg::Key::Num8,
b'9' => constellation_msg::Key::Num9,
b'\n' | b'\r' => constellation_msg::Key::Enter,
_ => constellation_msg::Key::Space,
let key = match get_key_msg((*event).windows_key_code, (*event).character) {
Some(keycode) => keycode,
None => {
error!("Unhandled keycode({}) passed!", (*event).windows_key_code);
return;
}
};
let key_state = match (*event).t {
// in tests with cef-real, this event had no effect