Merge branch 'content_type' into 'master'

Finish up imservice state

See merge request Librem5/squeekboard!93
This commit is contained in:
Dorota Czaplejewicz
2019-07-30 11:44:19 +00:00
4 changed files with 1558 additions and 32 deletions

1354
src/bitflags.rs Normal file

File diff suppressed because it is too large Load Diff

View File

@ -5,19 +5,6 @@
#include "eekboard/eekboard-context-service.h" #include "eekboard/eekboard-context-service.h"
void imservice_handle_text_change_cause(void *data, struct zwp_input_method_v2 *input_method, uint32_t cause) {}
void imservice_handle_content_type(void *data, struct zwp_input_method_v2 *input_method, uint32_t hint, uint32_t purpose)
{
struct imservice *ims = (struct imservice*)data;
EekboardContextService *context = EEKBOARD_CONTEXT_SERVICE(ims->ui_manager);
eekboard_context_service_set_hint_purpose(context, hint, purpose);
}
void imservice_handle_unavailable(void *data, struct zwp_input_method_v2 *input_method) {}
static const struct zwp_input_method_v2_listener input_method_listener = { static const struct zwp_input_method_v2_listener input_method_listener = {
.activate = imservice_handle_input_method_activate, .activate = imservice_handle_input_method_activate,
.deactivate = imservice_handle_input_method_deactivate, .deactivate = imservice_handle_input_method_deactivate,
@ -41,14 +28,16 @@ struct imservice* get_imservice(EekboardContextService *context,
return imservice; return imservice;
} }
void imservice_make_visible(EekboardContextService *context, void imservice_make_visible(EekboardContextService *context) {
struct zwp_input_method_v2 *im) {
(void)im;
eekboard_context_service_show_keyboard (context); eekboard_context_service_show_keyboard (context);
} }
void imservice_try_hide(EekboardContextService *context, void imservice_try_hide(EekboardContextService *context) {
struct zwp_input_method_v2 *im) {
(void)im;
eekboard_context_service_hide_keyboard (context); eekboard_context_service_hide_keyboard (context);
} }
/// Declared explicitly because _destroy is inline,
/// making it unavailable in Rust
void imservice_destroy_im(struct zwp_input_method_v2 *im) {
zwp_input_method_v2_destroy(im);
}

View File

@ -4,11 +4,7 @@
#include "input-method-unstable-v2-client-protocol.h" #include "input-method-unstable-v2-client-protocol.h"
#include "eek/eek-types.h" #include "eek/eek-types.h"
struct imservice struct imservice;
{
struct zwp_input_method_v2 *im;
EekboardContextService *ui_manager;
};
struct imservice* get_imservice(EekboardContextService *context, struct imservice* get_imservice(EekboardContextService *context,
struct zwp_input_method_manager_v2 *manager, struct zwp_input_method_manager_v2 *manager,
@ -22,5 +18,8 @@ void imservice_handle_input_method_deactivate(void *data, struct zwp_input_metho
void imservice_handle_surrounding_text(void *data, struct zwp_input_method_v2 *input_method, void imservice_handle_surrounding_text(void *data, struct zwp_input_method_v2 *input_method,
const char *text, uint32_t cursor, uint32_t anchor); const char *text, uint32_t cursor, uint32_t anchor);
void imservice_handle_commit_state(void *data, struct zwp_input_method_v2 *input_method); void imservice_handle_commit_state(void *data, struct zwp_input_method_v2 *input_method);
void imservice_handle_content_type(void *data, struct zwp_input_method_v2 *input_method, uint32_t hint, uint32_t purpose);
void imservice_handle_text_change_cause(void *data, struct zwp_input_method_v2 *input_method, uint32_t cause);
void imservice_handle_unavailable(void *data, struct zwp_input_method_v2 *input_method);
#endif #endif

View File

@ -1,9 +1,11 @@
#[macro_use]
mod bitflags;
use std::boxed::Box; use std::boxed::Box;
use std::ffi::CString; use std::ffi::CString;
use std::num::Wrapping; use std::num::Wrapping;
use std::string::String; use std::string::String;
/// Gathers stuff defined in C or called by C /// Gathers stuff defined in C or called by C
pub mod c { pub mod c {
use super::*; use super::*;
@ -18,7 +20,7 @@ pub mod c {
} }
// The following defined in C // The following defined in C
/// struct zwp_input_method_v2* /// struct zwp_input_method_v2*
#[repr(transparent)] #[repr(transparent)]
pub struct InputMethod(*const c_void); pub struct InputMethod(*const c_void);
@ -29,8 +31,10 @@ pub mod c {
#[no_mangle] #[no_mangle]
extern "C" { extern "C" {
fn imservice_make_visible(imservice: *const UIManager); fn imservice_destroy_im(im: *mut c::InputMethod);
fn imservice_try_hide(imservice: *const UIManager); fn eekboard_context_service_set_hint_purpose(imservice: *const UIManager, hint: u32, purpose: u32);
fn eekboard_context_service_show_keyboard(imservice: *const UIManager);
fn eekboard_context_service_hide_keyboard(imservice: *const UIManager);
} }
// 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
@ -86,7 +90,49 @@ pub mod c {
imservice.pending = IMProtocolState { imservice.pending = IMProtocolState {
surrounding_text: into_cstring(text).expect("Received invalid string"), surrounding_text: into_cstring(text).expect("Received invalid string"),
surrounding_cursor: cursor, surrounding_cursor: cursor,
..imservice.pending ..imservice.pending.clone()
};
}
#[no_mangle]
pub unsafe extern "C"
fn imservice_handle_content_type(imservice: *mut IMService,
_im: *const InputMethod,
hint: u32, purpose: u32)
{
let imservice = &mut *imservice;
imservice.pending = IMProtocolState {
content_hint: {
ContentHint::from_bits(hint).unwrap_or_else(|| {
eprintln!("Warning: received invalid hint flags");
ContentHint::NONE
})
},
content_purpose: {
ContentPurpose::from_num(purpose).unwrap_or_else(|| {
eprintln!("Warning: Received invalid purpose flags");
ContentPurpose::Normal
})
},
..imservice.pending.clone()
};
}
#[no_mangle]
pub unsafe extern "C"
fn imservice_handle_text_change_cause(imservice: *mut IMService,
_im: *const InputMethod,
cause: u32)
{
let imservice = &mut *imservice;
imservice.pending = IMProtocolState {
text_change_cause: {
ChangeCause::from_num(cause).unwrap_or_else(|| {
eprintln!("Warning: received invalid cause flags");
ChangeCause::InputMethod
})
},
..imservice.pending.clone()
}; };
} }
@ -106,24 +152,162 @@ pub mod c {
}; };
if active_changed { if active_changed {
if imservice.current.active { if imservice.current.active {
imservice_make_visible(imservice.ui_manager); eekboard_context_service_show_keyboard(imservice.ui_manager);
eekboard_context_service_set_hint_purpose(
imservice.ui_manager,
imservice.current.content_hint.bits(),
imservice.current.content_purpose.as_num());
} else { } else {
imservice_try_hide(imservice.ui_manager); eekboard_context_service_hide_keyboard(imservice.ui_manager);
} }
} }
} }
#[no_mangle]
pub unsafe extern "C"
fn imservice_handle_unavailable(imservice: *mut IMService,
im: *mut InputMethod)
{
imservice_destroy_im(im);
let imservice = &mut *imservice;
// no need to care about proper double-buffering,
// the keyboard is already decommissioned
imservice.current.active = false;
eekboard_context_service_hide_keyboard(imservice.ui_manager);
}
// FIXME: destroy and deallocate // FIXME: destroy and deallocate
} }
bitflags!{
/// Map to `text_input_unstable_v3.content_hint` values
pub struct ContentHint: u32 {
const NONE = 0x0;
const COMPLETION = 0x1;
const SPELLCHECK = 0x2;
const AUTO_CAPITALIZATION = 0x4;
const LOWERCASE = 0x8;
const UPPERCASE = 0x10;
const TITLECASE = 0x20;
const HIDDEN_TEXT = 0x40;
const SENSITIVE_DATA = 0x80;
const LATIN = 0x100;
const MULTILINE = 0x200;
}
}
/// Map to `text_input_unstable_v3.content_purpose` values
#[derive(Debug, Clone)]
pub enum ContentPurpose {
Normal,
Alpha,
Digits,
Number,
Phone,
Url,
Email,
Name,
Password,
Pin,
Date,
Time,
Datetime,
Terminal,
}
impl ContentPurpose {
fn from_num(num: u32) -> Option<ContentPurpose> {
use ContentPurpose::*;
match num {
0 => Some(Normal),
1 => Some(Alpha),
2 => Some(Digits),
3 => Some(Number),
4 => Some(Phone),
5 => Some(Url),
6 => Some(Email),
7 => Some(Name),
8 => Some(Password),
9 => Some(Pin),
10 => Some(Date),
11 => Some(Time),
12 => Some(Datetime),
13 => Some(Terminal),
_ => None,
}
}
fn as_num(self: &ContentPurpose) -> u32 {
use ContentPurpose::*;
match self {
Normal => 0,
Alpha => 1,
Digits => 2,
Number => 3,
Phone => 4,
Url => 5,
Email => 6,
Name => 7,
Password => 8,
Pin => 9,
Date => 10,
Time => 11,
Datetime => 12,
Terminal => 13,
}
}
}
/// Map to `text_input_unstable_v3.change_cause` values
#[derive(Debug, Clone)]
pub enum ChangeCause {
InputMethod,
Other,
}
impl ChangeCause {
pub fn from_num(num: u32) -> Option<ChangeCause> {
match num {
0 => Some(ChangeCause::InputMethod),
1 => Some(ChangeCause::Other),
_ => None
}
}
pub fn as_num(&self) -> u32 {
match &self {
ChangeCause::InputMethod => 0,
ChangeCause::Other => 1,
}
}
}
/// Describes the desired state of the input method as requested by the server /// Describes the desired state of the input method as requested by the server
#[derive(Default, Clone)] #[derive(Clone)]
struct IMProtocolState { struct IMProtocolState {
surrounding_text: CString, surrounding_text: CString,
surrounding_cursor: u32, surrounding_cursor: u32,
content_purpose: ContentPurpose,
content_hint: ContentHint,
text_change_cause: ChangeCause,
active: bool, active: bool,
} }
impl Default for IMProtocolState {
fn default() -> IMProtocolState {
IMProtocolState {
surrounding_text: CString::default(),
surrounding_cursor: 0, // TODO: mark that there's no cursor
content_hint: ContentHint::NONE,
content_purpose: ContentPurpose::Normal,
text_change_cause: ChangeCause::InputMethod,
active: false,
}
}
}
pub struct IMService { pub struct IMService {
/// Owned reference (still created and destroyed in C) /// Owned reference (still created and destroyed in C)
im: *const c::InputMethod, im: *const c::InputMethod,