Add callback to windows implementation

This commit is contained in:
Federico Terzi 2019-08-30 18:32:10 +02:00
parent b25858ad05
commit 197bd5be34
4 changed files with 47 additions and 11 deletions

View File

@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 3.0)
project(libwinbridge)
set (CMAKE_CXX_STANDARD 14)
add_library(winbridge STATIC bridge.cpp bridge.h)
install(TARGETS winbridge DESTINATION .)

View File

@ -17,10 +17,16 @@ HWND window;
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
*/
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)
{
@ -46,7 +52,7 @@ LRESULT CALLBACK workerWindowProcedure(HWND window, unsigned int msg, WPARAM wp,
// Request the Raw input data
if (GetRawInputData((HRAWINPUT)lp, RID_INPUT, lpb.data(), &dwSize,
sizeof(RAWINPUTHEADER)) != dwSize) {
std::cerr << "GetRawInputData does not return correct size!" << std::endl;
return 0;
}
// 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 (result >= 1) {
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;
//std::cout << buffer[0] << " " << buffer[1] << " res=" << result << " vk=" << raw->data.keyboard.VKey << " rsc=" << raw->data.keyboard.MakeCode << 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
currentKeyboardLayout = GetKeyboardLayout(0);
@ -109,7 +115,7 @@ void initialize() {
WNDCLASSEX wndclass = {
sizeof(WNDCLASSEX), // cbSize: Size of this structure
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, // 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.
@ -148,12 +154,14 @@ void initialize() {
Rid[0].hwndTarget = window;
if (RegisterRawInputDevices(Rid, 1, sizeof(Rid[0])) == FALSE) { // Something went wrong, error.
// TODO: error callback
return -1;
}
}else{
// Something went wrong, error.
// TODO: error callback
return -1;
}
return 1;
}
void eventloop() {
@ -165,8 +173,8 @@ void eventloop() {
// Enter the Event loop
MSG 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.
}

View File

@ -2,11 +2,26 @@
#define ESPANSO_BRIDGE_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
* return: 1 if OK, -1 otherwise.
*/
extern "C" void initialize();
extern "C" int32_t initialize();
/*
* Start the event loop indefinitely. Blocking call.

View File

@ -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")]
extern {
fn initialize();
fn eventloop();
fn register_keypress_callback(cb: extern fn(*const i32, i32));
}
fn main() {
@ -10,6 +18,9 @@ fn main() {
// calling the function from foo library
unsafe {
initialize();
register_keypress_callback(keypress_callback);
eventloop();
};
}