ffi: Remove unnecessary pointers to InputMethod

InputMethod is already a pointer.
This commit is contained in:
Dorota Czaplejewicz
2022-01-23 18:06:26 +00:00
parent f4f44a49ae
commit 236f7d4daf
2 changed files with 28 additions and 17 deletions

View File

@ -26,21 +26,32 @@ pub mod c {
use super::*; use super::*;
use std::os::raw::{c_char, c_void}; use std::os::raw::{c_char, c_void};
use std::ptr;
// The following defined in C // The following defined in C
/// struct zwp_input_method_v2* /// struct zwp_input_method_v2*
#[repr(transparent)] #[repr(transparent)]
#[derive(PartialEq, Clone, Copy)]
pub struct InputMethod(*const c_void); pub struct InputMethod(*const c_void);
impl InputMethod {
pub fn is_null(&self) -> bool {
self.0.is_null()
}
pub fn null() -> Self {
Self(ptr::null())
}
}
extern "C" { extern "C" {
fn imservice_destroy_im(im: *mut c::InputMethod); fn imservice_destroy_im(im: InputMethod);
#[allow(improper_ctypes)] // IMService will never be dereferenced in C #[allow(improper_ctypes)] // IMService will never be dereferenced in C
pub fn imservice_connect_listeners(im: *mut InputMethod, imservice: *const IMService); pub fn imservice_connect_listeners(im: InputMethod, imservice: *const IMService);
pub fn eek_input_method_commit_string(im: *mut InputMethod, text: *const c_char); pub fn eek_input_method_commit_string(im: InputMethod, text: *const c_char);
pub fn eek_input_method_delete_surrounding_text(im: *mut InputMethod, before: u32, after: u32); pub fn eek_input_method_delete_surrounding_text(im: InputMethod, before: u32, after: u32);
pub fn eek_input_method_commit(im: *mut InputMethod, serial: u32); pub fn eek_input_method_commit(im: InputMethod, serial: u32);
} }
// The following defined in Rust. TODO: wrap naked pointers to Rust data inside RefCells to prevent multiple writers // The following defined in Rust. TODO: wrap naked pointers to Rust data inside RefCells to prevent multiple writers
@ -49,7 +60,7 @@ pub mod c {
#[no_mangle] #[no_mangle]
pub extern "C" pub extern "C"
fn imservice_handle_input_method_activate(imservice: *mut IMService, fn imservice_handle_input_method_activate(imservice: *mut IMService,
im: *const InputMethod) im: InputMethod)
{ {
let imservice = check_imservice(imservice, im).unwrap(); let imservice = check_imservice(imservice, im).unwrap();
imservice.preedit_string = String::new(); imservice.preedit_string = String::new();
@ -62,7 +73,7 @@ pub mod c {
#[no_mangle] #[no_mangle]
pub extern "C" pub extern "C"
fn imservice_handle_input_method_deactivate(imservice: *mut IMService, fn imservice_handle_input_method_deactivate(imservice: *mut IMService,
im: *const InputMethod) im: InputMethod)
{ {
let imservice = check_imservice(imservice, im).unwrap(); let imservice = check_imservice(imservice, im).unwrap();
imservice.pending = IMProtocolState { imservice.pending = IMProtocolState {
@ -74,7 +85,7 @@ pub mod c {
#[no_mangle] #[no_mangle]
pub extern "C" pub extern "C"
fn imservice_handle_surrounding_text(imservice: *mut IMService, fn imservice_handle_surrounding_text(imservice: *mut IMService,
im: *const InputMethod, im: InputMethod,
text: *const c_char, cursor: u32, _anchor: u32) text: *const c_char, cursor: u32, _anchor: u32)
{ {
let imservice = check_imservice(imservice, im).unwrap(); let imservice = check_imservice(imservice, im).unwrap();
@ -90,7 +101,7 @@ pub mod c {
#[no_mangle] #[no_mangle]
pub extern "C" pub extern "C"
fn imservice_handle_content_type(imservice: *mut IMService, fn imservice_handle_content_type(imservice: *mut IMService,
im: *const InputMethod, im: InputMethod,
hint: u32, purpose: u32) hint: u32, purpose: u32)
{ {
let imservice = check_imservice(imservice, im).unwrap(); let imservice = check_imservice(imservice, im).unwrap();
@ -118,7 +129,7 @@ pub mod c {
#[no_mangle] #[no_mangle]
pub extern "C" pub extern "C"
fn imservice_handle_text_change_cause(imservice: *mut IMService, fn imservice_handle_text_change_cause(imservice: *mut IMService,
im: *const InputMethod, im: InputMethod,
cause: u32) cause: u32)
{ {
let imservice = check_imservice(imservice, im).unwrap(); let imservice = check_imservice(imservice, im).unwrap();
@ -138,7 +149,7 @@ pub mod c {
#[no_mangle] #[no_mangle]
pub extern "C" pub extern "C"
fn imservice_handle_done(imservice: *mut IMService, fn imservice_handle_done(imservice: *mut IMService,
im: *const InputMethod) im: InputMethod)
{ {
let imservice = check_imservice(imservice, im).unwrap(); let imservice = check_imservice(imservice, im).unwrap();
@ -156,7 +167,7 @@ pub mod c {
#[no_mangle] #[no_mangle]
pub extern "C" pub extern "C"
fn imservice_handle_unavailable(imservice: *mut IMService, fn imservice_handle_unavailable(imservice: *mut IMService,
im: *mut InputMethod) im: InputMethod)
{ {
let imservice = check_imservice(imservice, im).unwrap(); let imservice = check_imservice(imservice, im).unwrap();
unsafe { imservice_destroy_im(im); } unsafe { imservice_destroy_im(im); }
@ -181,7 +192,7 @@ pub mod c {
/// Care must be take /// Care must be take
/// not to exceed the lifetime of the pointer with the reference, /// not to exceed the lifetime of the pointer with the reference,
/// especially not to store it. /// especially not to store it.
fn check_imservice(imservice: *mut IMService, im: *const InputMethod) fn check_imservice(imservice: *mut IMService, im: InputMethod)
-> Result<&'static mut IMService, &'static str> -> Result<&'static mut IMService, &'static str>
{ {
if imservice.is_null() { if imservice.is_null() {
@ -315,7 +326,7 @@ impl Default for IMProtocolState {
pub struct IMService { pub struct IMService {
/// Owned reference (still created and destroyed in C) /// Owned reference (still created and destroyed in C)
pub im: *mut c::InputMethod, pub im: c::InputMethod,
sender: driver::Threaded, sender: driver::Threaded,
pending: IMProtocolState, pending: IMProtocolState,
@ -331,7 +342,7 @@ pub enum SubmitError {
impl IMService { impl IMService {
pub fn new( pub fn new(
im: *mut c::InputMethod, im: c::InputMethod,
sender: driver::Threaded, sender: driver::Threaded,
) -> Box<IMService> { ) -> Box<IMService> {
// IMService will be referenced to by C, // IMService will be referenced to by C,

View File

@ -53,7 +53,7 @@ mod c {
input_method_manager: *const c_void, input_method_manager: *const c_void,
outputs: Wrapped<Outputs>, outputs: Wrapped<Outputs>,
seat: *const c_void, seat: *const c_void,
input_method: *mut InputMethod, input_method: InputMethod,
virtual_keyboard: ZwpVirtualKeyboardV1, virtual_keyboard: ZwpVirtualKeyboardV1,
} }
@ -65,7 +65,7 @@ mod c {
input_method_manager: ptr::null(), input_method_manager: ptr::null(),
outputs: Wrapped::new(outputs_manager), outputs: Wrapped::new(outputs_manager),
seat: ptr::null(), seat: ptr::null(),
input_method: ptr::null_mut(), input_method: InputMethod::null(),
virtual_keyboard: ZwpVirtualKeyboardV1::null(), virtual_keyboard: ZwpVirtualKeyboardV1::null(),
} }
} }