msdk_driver_adapter/
lib.rs

1use std::time::Duration;
2use anyhow::{Error, Result};
3use libloading::{Library, Symbol};
4
5const SUCCESS: i32 = 0; 
6
7pub struct MsdkAdapter {
8    port: u64,
9    handler: u64,
10    lib: Library
11}
12
13impl MsdkAdapter {
14    // port num, start from 1
15    pub fn new(port: u64) -> Result<MsdkAdapter> {
16        unsafe {
17            let lib = Library::new("msdk.dll")?;
18            let open_func: Symbol<extern "C" fn(u64) -> u64> = lib.get(b"M_Open")?;
19            let res = MsdkAdapter {
20                port,
21                handler: open_func(port),
22                lib
23            };
24            std::thread::sleep(Duration::from_secs(3));
25            Ok(res)
26        }
27    }
28}
29
30pub trait MsdkKeyBoardOperation {
31    fn open(&mut self, port_num: u64) -> Result<u64>;
32    fn close(&self) -> Result<i32>;
33    // windows keycode ascii
34    fn key_press(&self, key_code: i32, count: i32) -> Result<i32>;
35    fn key_down(&self, key_code: i32) -> Result<i32>;
36    fn key_up(&self, key_code: i32) -> Result<i32>;
37    fn all_key_up(&self) -> Result<i32>;
38}
39
40pub trait MsdkMouseOperation {
41    fn left_click(&self, count: i32) -> Result<i32>;
42    fn left_double_click(&self, count: i32) -> Result<i32>;
43}
44
45fn check_result(result: i32) -> Result<i32> {
46    match result {
47        SUCCESS => Ok(result),
48        _ => {
49            Err(Error::msg(format!("Check res failed, result: {}.", result)))
50        }
51    }
52}
53
54impl MsdkKeyBoardOperation for MsdkAdapter {
55    fn open(&mut self, port_num: u64) -> Result<u64> {
56        unsafe {
57            self.close().expect(&format!("Open port{} fail.", port_num));
58            let func: Symbol<extern "C" fn(u64) -> u64> = self.lib.get(b"M_Open")?;
59            let handler = func(self.port);
60            self.handler = handler;
61            self.port = port_num;
62            Ok(handler)
63        }
64    }
65
66    fn close(&self) -> Result<i32> {
67        unsafe {
68            let func: Symbol<extern "C" fn(u64) -> i32> = self.lib.get(b"M_Close")?;
69            let res = func(self.handler);
70            check_result(res)
71        }
72    }
73
74    fn key_press(&self, key_code: i32, count: i32) -> Result<i32> {
75        unsafe {
76            let func: Symbol<extern "C" fn(u64, i32, i32) -> i32> = self.lib.get(b"M_KeyPress2")?;
77            let res = func(self.handler, key_code, count);
78            check_result(res)
79        }
80    }
81
82    fn key_down(&self, key_code: i32) -> Result<i32> {
83        unsafe {
84            let func: Symbol<extern "C" fn(u64, i32) -> i32> = self.lib.get(b"M_KeyDown2")?;
85            let res = func(self.handler, key_code);
86            check_result(res)
87        }
88    }
89
90    fn key_up(&self, key_code: i32) -> Result<i32> {
91        unsafe {
92            let func: Symbol<extern "C" fn(u64, i32) -> i32> = self.lib.get(b"M_KeyUp2")?;
93            let res = func(self.handler, key_code);
94            check_result(res)
95        }
96    }
97
98    fn all_key_up(&self) -> Result<i32> {
99        unsafe {
100            let func: Symbol<extern "C" fn(u64) -> i32> = self.lib.get(b"M_ReleaseAllKey")?;
101            let res = func(self.handler);
102            check_result(res)
103        }
104    }
105}