Add callback to windows implementation
This commit is contained in:
parent
b25858ad05
commit
197bd5be34
|
@ -1,6 +1,8 @@
|
||||||
cmake_minimum_required(VERSION 3.0)
|
cmake_minimum_required(VERSION 3.0)
|
||||||
project(libwinbridge)
|
project(libwinbridge)
|
||||||
|
|
||||||
|
set (CMAKE_CXX_STANDARD 14)
|
||||||
|
|
||||||
add_library(winbridge STATIC bridge.cpp bridge.h)
|
add_library(winbridge STATIC bridge.cpp bridge.h)
|
||||||
|
|
||||||
install(TARGETS winbridge DESTINATION .)
|
install(TARGETS winbridge DESTINATION .)
|
|
@ -17,10 +17,16 @@ HWND window;
|
||||||
|
|
||||||
const wchar_t* const winclass = L"Espanso";
|
const wchar_t* const winclass = L"Espanso";
|
||||||
|
|
||||||
|
keypress_callback keypressCallback;
|
||||||
|
|
||||||
|
void register_keypress_callback(keypress_callback callback) {
|
||||||
|
keypressCallback = callback;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Message handler procedure for the Worker window
|
* Message handler procedure for the Worker window
|
||||||
*/
|
*/
|
||||||
LRESULT CALLBACK workerWindowProcedure(HWND window, unsigned int msg, WPARAM wp, LPARAM lp)
|
LRESULT CALLBACK window_worker_procedure(HWND window, unsigned int msg, WPARAM wp, LPARAM lp)
|
||||||
{
|
{
|
||||||
switch (msg)
|
switch (msg)
|
||||||
{
|
{
|
||||||
|
@ -46,7 +52,7 @@ LRESULT CALLBACK workerWindowProcedure(HWND window, unsigned int msg, WPARAM wp,
|
||||||
// Request the Raw input data
|
// Request the Raw input data
|
||||||
if (GetRawInputData((HRAWINPUT)lp, RID_INPUT, lpb.data(), &dwSize,
|
if (GetRawInputData((HRAWINPUT)lp, RID_INPUT, lpb.data(), &dwSize,
|
||||||
sizeof(RAWINPUTHEADER)) != dwSize) {
|
sizeof(RAWINPUTHEADER)) != dwSize) {
|
||||||
std::cerr << "GetRawInputData does not return correct size!" << std::endl;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert the input data
|
// Convert the input data
|
||||||
|
@ -86,8 +92,8 @@ LRESULT CALLBACK workerWindowProcedure(HWND window, unsigned int msg, WPARAM wp,
|
||||||
|
|
||||||
// If a result is available, invoke the callback
|
// If a result is available, invoke the callback
|
||||||
if (result >= 1) {
|
if (result >= 1) {
|
||||||
std::cout << buffer[0] << " " << buffer[1] << " res=" << result << " vk=" << raw->data.keyboard.VKey << " rsc=" << raw->data.keyboard.MakeCode << std::endl;
|
//std::cout << buffer[0] << " " << buffer[1] << " res=" << result << " vk=" << raw->data.keyboard.VKey << " rsc=" << raw->data.keyboard.MakeCode << std::endl;
|
||||||
std::cout << static_cast<char>(buffer[0]) << std::endl;
|
keypressCallback(reinterpret_cast<int32_t*>(buffer.data()), buffer.size());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -99,7 +105,7 @@ LRESULT CALLBACK workerWindowProcedure(HWND window, unsigned int msg, WPARAM wp,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void initialize() {
|
int32_t initialize() {
|
||||||
// Initialize the default keyboard layout
|
// Initialize the default keyboard layout
|
||||||
currentKeyboardLayout = GetKeyboardLayout(0);
|
currentKeyboardLayout = GetKeyboardLayout(0);
|
||||||
|
|
||||||
|
@ -109,7 +115,7 @@ void initialize() {
|
||||||
WNDCLASSEX wndclass = {
|
WNDCLASSEX wndclass = {
|
||||||
sizeof(WNDCLASSEX), // cbSize: Size of this structure
|
sizeof(WNDCLASSEX), // cbSize: Size of this structure
|
||||||
0, // style: Class styles
|
0, // style: Class styles
|
||||||
workerWindowProcedure, // lpfnWndProc: Pointer to the window procedure
|
window_worker_procedure, // lpfnWndProc: Pointer to the window procedure
|
||||||
0, // cbClsExtra: Number of extra bytes to allocate following the window-class structure
|
0, // cbClsExtra: Number of extra bytes to allocate following the window-class structure
|
||||||
0, // cbWndExtra: The number of extra bytes to allocate following the window instance.
|
0, // cbWndExtra: The number of extra bytes to allocate following the window instance.
|
||||||
GetModuleHandle(0), // hInstance: A handle to the instance that contains the window procedure for the class.
|
GetModuleHandle(0), // hInstance: A handle to the instance that contains the window procedure for the class.
|
||||||
|
@ -148,12 +154,14 @@ void initialize() {
|
||||||
Rid[0].hwndTarget = window;
|
Rid[0].hwndTarget = window;
|
||||||
|
|
||||||
if (RegisterRawInputDevices(Rid, 1, sizeof(Rid[0])) == FALSE) { // Something went wrong, error.
|
if (RegisterRawInputDevices(Rid, 1, sizeof(Rid[0])) == FALSE) { // Something went wrong, error.
|
||||||
// TODO: error callback
|
return -1;
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
// Something went wrong, error.
|
// Something went wrong, error.
|
||||||
// TODO: error callback
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void eventloop() {
|
void eventloop() {
|
||||||
|
@ -165,8 +173,8 @@ void eventloop() {
|
||||||
// Enter the Event loop
|
// Enter the Event loop
|
||||||
MSG msg;
|
MSG msg;
|
||||||
while (GetMessage(&msg, 0, 0, 0)) DispatchMessage(&msg);
|
while (GetMessage(&msg, 0, 0, 0)) DispatchMessage(&msg);
|
||||||
}else{ // Something went wrong, error
|
|
||||||
// TODO: error callback
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Something went wrong, this should have been an infinite loop.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,11 +2,26 @@
|
||||||
#define ESPANSO_BRIDGE_H
|
#define ESPANSO_BRIDGE_H
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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 (*keypress_callback)(int32_t *buffer, int32_t len);
|
||||||
|
|
||||||
|
extern keypress_callback keypressCallback;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Register the callback that will be called when a keypress was made
|
||||||
|
*/
|
||||||
|
extern "C" void register_keypress_callback(keypress_callback callback);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Initialize the Windows worker's parameters
|
* Initialize the Windows worker's parameters
|
||||||
|
* return: 1 if OK, -1 otherwise.
|
||||||
*/
|
*/
|
||||||
extern "C" void initialize();
|
extern "C" int32_t initialize();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Start the event loop indefinitely. Blocking call.
|
* Start the event loop indefinitely. Blocking call.
|
||||||
|
|
11
src/main.rs
11
src/main.rs
|
@ -1,7 +1,15 @@
|
||||||
|
extern fn keypress_callback(raw_buffer: *const i32, len: i32) {
|
||||||
|
unsafe {
|
||||||
|
let buffer = std::slice::from_raw_parts(raw_buffer, len as usize);
|
||||||
|
println!("{}", std::char::from_u32(buffer[0] as u32).unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[link(name="winbridge", kind="static")]
|
#[link(name="winbridge", kind="static")]
|
||||||
extern {
|
extern {
|
||||||
fn initialize();
|
fn initialize();
|
||||||
fn eventloop();
|
fn eventloop();
|
||||||
|
fn register_keypress_callback(cb: extern fn(*const i32, i32));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -10,6 +18,9 @@ fn main() {
|
||||||
// calling the function from foo library
|
// calling the function from foo library
|
||||||
unsafe {
|
unsafe {
|
||||||
initialize();
|
initialize();
|
||||||
|
|
||||||
|
register_keypress_callback(keypress_callback);
|
||||||
|
|
||||||
eventloop();
|
eventloop();
|
||||||
};
|
};
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user