From de296b77ea49da2c65a16c64981a5b19f3c064be Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Thu, 10 Oct 2019 21:34:50 +0200 Subject: [PATCH] Add windows support to unicode matches --- native/libwinbridge/bridge.cpp | 2 +- native/libwinbridge/bridge.h | 2 +- src/bridge/windows.rs | 2 +- src/context/windows.rs | 29 ++++++++++++++++++++--------- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/native/libwinbridge/bridge.cpp b/native/libwinbridge/bridge.cpp index 3d977f6..dfe6108 100644 --- a/native/libwinbridge/bridge.cpp +++ b/native/libwinbridge/bridge.cpp @@ -250,7 +250,7 @@ LRESULT CALLBACK window_procedure(HWND window, unsigned int msg, WPARAM wp, LPAR // We need to call the callback in two different ways based on the type of key // The only modifier we use that has a result > 0 is the BACKSPACE, so we have to consider it. if (result >= 1 && raw->data.keyboard.VKey != VK_BACK) { - keypress_callback(manager_instance, reinterpret_cast(buffer.data()), buffer.size(), 0, raw->data.keyboard.VKey, is_key_down); + keypress_callback(manager_instance, reinterpret_cast(buffer.data()), buffer.size(), 0, raw->data.keyboard.VKey, is_key_down); }else{ keypress_callback(manager_instance, nullptr, 0, 1, raw->data.keyboard.VKey, is_key_down); } diff --git a/native/libwinbridge/bridge.h b/native/libwinbridge/bridge.h index e9ddbf8..fc0a80c 100644 --- a/native/libwinbridge/bridge.h +++ b/native/libwinbridge/bridge.h @@ -39,7 +39,7 @@ extern "C" int32_t initialize(void * self, wchar_t * ico_path, wchar_t * bmp_pat * Called when a new keypress is made, the first argument is an int array, * while the second is the size of the array. */ -typedef void (*KeypressCallback)(void * self, int32_t *buffer, int32_t len, int32_t is_modifier, int32_t key_code, int32_t is_key_down); +typedef void (*KeypressCallback)(void * self, uint16_t *buffer, int32_t len, int32_t is_modifier, int32_t key_code, int32_t is_key_down); extern KeypressCallback keypress_callback; /* diff --git a/src/bridge/windows.rs b/src/bridge/windows.rs index 6866cbe..349230c 100644 --- a/src/bridge/windows.rs +++ b/src/bridge/windows.rs @@ -49,7 +49,7 @@ extern { pub fn set_clipboard(payload: *const u16) -> i32; // KEYBOARD - pub fn register_keypress_callback(cb: extern fn(_self: *mut c_void, *const i32, + pub fn register_keypress_callback(cb: extern fn(_self: *mut c_void, *const u16, i32, i32, i32, i32)); pub fn eventloop(); diff --git a/src/context/windows.rs b/src/context/windows.rs index 39d2a14..c6ecf57 100644 --- a/src/context/windows.rs +++ b/src/context/windows.rs @@ -23,8 +23,8 @@ use crate::event::{Event, KeyEvent, KeyModifier, ActionType}; use crate::event::KeyModifier::*; use std::ffi::c_void; use std::{fs}; -use widestring::U16CString; -use log::{info}; +use widestring::{U16CString, U16CStr}; +use log::{info, error}; const BMP_BINARY : &[u8] = include_bytes!("../res/win/espanso.bmp"); const ICO_BINARY : &[u8] = include_bytes!("../res/win/espanso.ico"); @@ -102,20 +102,31 @@ impl super::Context for WindowsContext { // Native bridge code -extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const i32, len: i32, +extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const u16, len: i32, is_modifier: i32, key_code: i32, is_key_down: i32) { unsafe { let _self = _self as *mut WindowsContext; if is_key_down != 0 { // KEY DOWN EVENT if is_modifier == 0 { // Char event - // Convert the received buffer to a character + // Convert the received buffer to a string let buffer = std::slice::from_raw_parts(raw_buffer, len as usize); - let r = std::char::from_u32(buffer[0] as u32); + let c_string = U16CStr::from_slice_with_nul(buffer); - // Send the char through the channel - if let Some(c) = r { - let event = Event::Key(KeyEvent::Char(c)); - (*_self).send_channel.send(event).unwrap(); + if let Ok(c_string) = c_string { + let string = c_string.to_string(); + + // Send the char through the channel + match string { + Ok(string) => { + let event = Event::Key(KeyEvent::Char(string)); + (*_self).send_channel.send(event).unwrap(); + }, + Err(e) => { + error!("Unable to receive char: {}",e); + }, + } + }else{ + error!("unable to decode widechar"); } } }else{ // KEY UP event