Skip to content

Commit

Permalink
更新依赖项并优化代码结构。
Browse files Browse the repository at this point in the history
  • Loading branch information
mzdk100 committed Dec 5, 2024
1 parent e64085d commit 7c43a7e
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 71 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ authors = [
description = "RigelA screen reader (rsr)."
license = "Apache-2.0"
keywords = ["screen-reader", "accessibility"]
version = "0.2.0"
version = "0.2.1"
edition = "2021"
readme = "README.md"
repository = "https://gitcode.net/mzdk100/rigela.git"
Expand All @@ -28,10 +28,10 @@ version = "0.12.3"
version = "0.4.22"

[workspace.dependencies.tokio]
version = "1.41.1"
version = "1.42.0"

[workspace.dependencies.serde]
version = "1.0.214"
version = "1.0.215"

[workspace.dependencies.win-wrap]
version = "0.2.9"
Expand Down
2 changes: 1 addition & 1 deletion a11y/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ optional = true

[dependencies.rigela-utils]
path = "../utils"
version = "0.2.0"
version = "0.2.1"
optional = true

[dependencies.scintilla-sys]
Expand Down
4 changes: 2 additions & 2 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ repository.workspace = true
proc-macro = true

[dependencies]
proc-macro2 = "1.0.86"
proc-macro2 = "1.0.92"
quote = "1.0.37"
heck = "0.5.0"

[dependencies.syn]
version = "2.0.79"
version = "2.0.90"
features = ["full"]
4 changes: 2 additions & 2 deletions main/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ rust-i18n = "3.1.2"
select = "0.6.0"

# 压缩解压
zip = "2.2.0"
zip = "2.2.1"

# 跨线程去锁传输数据
arc-swap = "1.7.1"
Expand All @@ -106,7 +106,7 @@ workspace = true

[dependencies.reqwest]
# 网络请求
version = "0.12.8"
version = "0.12.9"
features = ["json"]

[dependencies.native-windows-derive]
Expand Down
20 changes: 10 additions & 10 deletions main/src/talent/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ async fn current_date(context: Weak<Context>) {
context.get_performer().speak(&msg).await;
}

impl Speakable for PdhCounter {
fn get_sentence(&self) -> String {
t!(
"program.current_cpu_usage",
value = self.get_value().1.round()
)
.to_string()
}
}

#[talent(doc = t ! ("program.current_cpu_usage_doc").to_string(), key = combo_key ! ("RigelA", VkQ))]
async fn current_cpu_usage(context: Weak<Context>) {
static CPU_QUERY: LazyLock<(PdhCounter, PdhQuery)> = LazyLock::new(|| {
Expand All @@ -66,16 +76,6 @@ async fn current_cpu_usage(context: Weak<Context>) {
});
CPU_QUERY.1.collect_data();

impl Speakable for PdhCounter {
fn get_sentence(&self) -> String {
t!(
"program.current_cpu_usage",
value = self.get_value().1.round()
)
.to_string()
}
}

context.get_performer().speak(&CPU_QUERY.0).await;
}

Expand Down
8 changes: 6 additions & 2 deletions utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ clip = ["clipboard"]
color = []
common = []
fs = ["win-wrap/shell", "tokio/fs", "tokio/io-util"]
ibmeci = ["common", "encoding_rs/fast-gb-hanzi-encode", "tokio/sync"]
ibmeci = ["common", "encoding_rs/fast-gb-hanzi-encode", "tokio/sync", "flume"]
killer = ["pipe", "win-wrap/threading"]
library = ["fs"]
logger = ["log4rs"]
Expand All @@ -40,7 +40,7 @@ optional = true

[dependencies.encoding_rs]
# 字符串编解码
version = "0.8.34"
version = "0.8.35"
optional = true

[dependencies.log4rs]
Expand Down Expand Up @@ -68,3 +68,7 @@ optional = true
[dependencies.png]
version = "0.17.14"
optional = true

[dependencies.flume]
optional = true
version = "0.11.1"
100 changes: 49 additions & 51 deletions utils/src/ibmeci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,26 @@
* See the License for the specific language governing permissions and limitations under the License.
*/

use crate::{call_proc, common::SafeModuleHandle, library::setup_library};
use crate::{
call_proc,
common::SafeModuleHandle,
library::{get_rigela_library_path, setup_library},
};
use encoding_rs::GBK;
use log::info;
use flume::{bounded, Sender};
use log::{error, info};
use std::{
alloc::{alloc_zeroed, dealloc, Layout},
borrow::Cow,
ffi::{c_char, CString},
sync::OnceLock,
thread,
};
use tokio::sync::oneshot::{self, channel, Sender};
use tokio::sync::oneshot;
use win_wrap::{
common::{free_library, get_proc_address, load_library, FARPROC, LPARAM, WPARAM},
message::{message_loop, post_thread_message, register_window_message},
message::{message_loop, post_thread_message},
threading::get_current_thread_id,
wm,
};
use crate::library::get_rigela_library_path;

macro_rules! eci {
($module:expr,new) => {
Expand Down Expand Up @@ -161,8 +163,7 @@ pub const VP_SPEED: u32 = 6;
pub const VP_VOLUME: u32 = 7;

//noinspection SpellCheckingInspection
static mut IBMECI: OnceLock<Ibmeci> = OnceLock::new();
static SYNTH_TASK: OnceLock<u32> = OnceLock::new();
static IBMECI: OnceLock<Ibmeci> = OnceLock::new();

extern "system" fn _callback_internal(
#[allow(unused_variables)] h_eci: u32,
Expand All @@ -174,7 +175,7 @@ extern "system" fn _callback_internal(
return RETURN_DATA_PROCESSED;
}
unsafe {
let eci = IBMECI.get_mut();
let eci = IBMECI.get();
if eci.is_none() {
return RETURN_DATA_PROCESSED;
}
Expand All @@ -184,17 +185,19 @@ extern "system" fn _callback_internal(
for i in 0..(param * 2) {
vec.push(*eci.buffer_ptr.wrapping_add(i as usize));
}
eci.data.extend(vec);
let _ = eci.data_tx.send(vec);
}
RETURN_DATA_PROCESSED
}

const BUFFER_LAYOUT: Layout = Layout::new::<[u8; 8192]>();

//noinspection SpellCheckingInspection
#[derive(Debug)]
pub struct Ibmeci {
buffer_layout: Layout,
buffer_ptr: *mut u8,
data: Vec<u8>,
request_tx: Sender<(Vec<u8>, Sender<Vec<u8>>)>,
data_tx: Sender<Vec<u8>>,
h_module: SafeModuleHandle,
h_eci: i32,
thread: u32,
Expand All @@ -205,12 +208,10 @@ impl Ibmeci {
/**
获取一个实例。
*/
pub async fn get<'a>() -> Result<&'a Self, String> {
unsafe {
// 单例模式
if let Some(self_) = IBMECI.get() {
return Ok(self_);
}
pub async fn get() -> Result<&'static Self, String> {
// 单例模式
if let Some(self_) = IBMECI.get() {
return Ok(self_);
}
const LIB_NAME: &str = "ibmeci.dll";
let eci_path = get_rigela_library_path().join(LIB_NAME);
Expand All @@ -230,13 +231,14 @@ impl Ibmeci {
let (tx, rx) = oneshot::channel();
thread::spawn(move || {
let h_eci = eci!(*h_module, new).unwrap_or(0);
let buffer_layout = Layout::new::<[u8; 8192]>();
let buffer_ptr = unsafe { alloc_zeroed(buffer_layout) };
let buffer_ptr = unsafe { alloc_zeroed(BUFFER_LAYOUT) };

let (tx_data, rx_data) = bounded(BUFFER_LAYOUT.size());
let (tx_request, rx_request) = bounded(2);
let self_ = Self {
buffer_layout,
buffer_ptr,
data: vec![],
request_tx: tx_request,
data_tx: tx_data,
h_module: h_module.clone(),
h_eci,
thread: get_current_thread_id(),
Expand All @@ -247,22 +249,20 @@ impl Ibmeci {
*h_module,
set_output_buffer,
h_eci,
(buffer_layout.size() / 2) as u32,
(BUFFER_LAYOUT.size() / 2) as u32,
buffer_ptr
);
info!("Module handle: {:?}, eci handle: {}", h_module.0, h_eci);
unsafe {
IBMECI.set(self_).unwrap();
tx.send(IBMECI.get().unwrap()).unwrap();
}
message_loop(|m| {
if wm!(SYNTH_TASK) == m.message {
let b = unsafe { Box::from_raw(m.wParam.0 as *mut Cow<[u8]>) };
eci!(*h_module, add_text, h_eci, *b);
IBMECI.set(self_).unwrap();
tx.send(IBMECI.get().unwrap()).unwrap();
message_loop(|_| {
if let Ok((text, tx_status)) = rx_request.try_recv() {
eci!(*h_module, add_text, h_eci, text);
eci!(*h_module, synthesize, h_eci);
eci!(*h_module, synchronize, h_eci);
let b = unsafe { Box::from_raw(m.lParam.0 as *mut Sender<()>) };
b.send(()).unwrap_or(());
while let Ok(data) = rx_data.try_recv() {
let _ = tx_status.send(data);
}
}
});
});
Expand Down Expand Up @@ -312,25 +312,23 @@ impl Ibmeci {
}
last_char = i;
}
Cow::from(v)
v.into()
} else {
text
};
if let Some(eci) = unsafe { IBMECI.get_mut() } {
eci.data.clear();
let (tx, rx) = channel();
let tx = Box::new(tx);
post_thread_message(
eci.thread,
wm!(SYNTH_TASK),
WPARAM(Box::into_raw(Box::new(text)) as usize),
LPARAM(Box::into_raw(tx) as isize),
);
rx.await.unwrap_or(());
eci.data.clone()
} else {
vec![]
let Some(eci) = IBMECI.get() else {
return vec![];
};
let (tx, rx) = bounded(2);
if let Err(e) = eci.request_tx.send_async((text.to_vec(), tx)).await {
error!("Can't synth the data. ({})", e);
}
post_thread_message(eci.thread, 0, WPARAM::default(), LPARAM::default());
let mut buf = vec![];
while let Ok(data) = rx.recv_async().await {
buf.extend(data);
}
buf
}

/**
Expand Down Expand Up @@ -385,7 +383,7 @@ impl Drop for Ibmeci {
free_library(*self.h_module);
}
unsafe {
dealloc(self.buffer_ptr, self.buffer_layout);
dealloc(self.buffer_ptr, BUFFER_LAYOUT);
}
}
}
Expand All @@ -396,8 +394,8 @@ unsafe impl Send for Ibmeci {}

#[cfg(all(test, target_arch = "x86"))]
mod test_eci {
use super::Ibmeci;
use super::super::logger::init_logger;
use super::Ibmeci;

#[tokio::test]
async fn main() {
Expand Down

0 comments on commit 7c43a7e

Please sign in to comment.