use crate::{ffi, AsRaw, FromRaw};
#[cfg(feature = "libinput_1_26")]
use crate::{DeviceConfigError, DeviceConfigResult};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum TabletToolType {
Pen,
Eraser,
Brush,
Pencil,
Airbrush,
Mouse,
Lens,
#[cfg(feature = "libinput_1_14")]
Totem,
}
ffi_ref_struct! {
struct TabletTool, ffi::libinput_tablet_tool, ffi::libinput_tablet_tool_ref, ffi::libinput_tablet_tool_unref
}
impl TabletTool {
ffi_func!(
pub fn serial, ffi::libinput_tablet_tool_get_serial, u64);
ffi_func!(
pub fn tool_id, ffi::libinput_tablet_tool_get_tool_id, u64);
pub fn tool_type(&self) -> Option<TabletToolType> {
match unsafe { ffi::libinput_tablet_tool_get_type(self.as_raw_mut()) } {
ffi::libinput_tablet_tool_type_LIBINPUT_TABLET_TOOL_TYPE_PEN => {
Some(TabletToolType::Pen)
}
ffi::libinput_tablet_tool_type_LIBINPUT_TABLET_TOOL_TYPE_ERASER => {
Some(TabletToolType::Eraser)
}
ffi::libinput_tablet_tool_type_LIBINPUT_TABLET_TOOL_TYPE_BRUSH => {
Some(TabletToolType::Brush)
}
ffi::libinput_tablet_tool_type_LIBINPUT_TABLET_TOOL_TYPE_PENCIL => {
Some(TabletToolType::Pencil)
}
ffi::libinput_tablet_tool_type_LIBINPUT_TABLET_TOOL_TYPE_AIRBRUSH => {
Some(TabletToolType::Airbrush)
}
ffi::libinput_tablet_tool_type_LIBINPUT_TABLET_TOOL_TYPE_MOUSE => {
Some(TabletToolType::Mouse)
}
ffi::libinput_tablet_tool_type_LIBINPUT_TABLET_TOOL_TYPE_LENS => {
Some(TabletToolType::Lens)
}
#[cfg(feature = "libinput_1_14")]
ffi::libinput_tablet_tool_type_LIBINPUT_TABLET_TOOL_TYPE_TOTEM => {
Some(TabletToolType::Totem)
}
_x => {
#[cfg(feature = "log")]
log::warn!("Unknown `TabletToolType` returned by libinput: {}", _x);
None
}
}
}
pub fn has_button(&self, button: u32) -> bool {
unsafe { ffi::libinput_tablet_tool_has_button(self.as_raw_mut(), button) != 0 }
}
ffi_func!(
pub fn has_distance, ffi::libinput_tablet_tool_has_distance, bool);
ffi_func!(
pub fn has_pressure, ffi::libinput_tablet_tool_has_pressure, bool);
ffi_func!(
pub fn has_rotation, ffi::libinput_tablet_tool_has_rotation, bool);
ffi_func!(
pub fn has_slider, ffi::libinput_tablet_tool_has_slider, bool);
ffi_func!(
pub fn has_tilt, ffi::libinput_tablet_tool_has_tilt, bool);
ffi_func!(
pub fn has_wheel, ffi::libinput_tablet_tool_has_wheel, bool);
ffi_func!(
pub fn is_unique, ffi::libinput_tablet_tool_is_unique, bool);
#[cfg(feature = "libinput_1_14")]
ffi_func!(
pub fn tablet_tool_has_size, ffi::libinput_tablet_tool_has_size, bool);
#[cfg(feature = "libinput_1_26")]
pub fn config_pressure_range_set(&self, minimum: f64, maximum: f64) -> DeviceConfigResult {
match unsafe {
ffi::libinput_tablet_tool_config_pressure_range_set(self.as_raw_mut(), minimum, maximum)
} {
ffi::libinput_config_status_LIBINPUT_CONFIG_STATUS_SUCCESS => Ok(()),
ffi::libinput_config_status_LIBINPUT_CONFIG_STATUS_UNSUPPORTED => {
Err(DeviceConfigError::Unsupported)
}
ffi::libinput_config_status_LIBINPUT_CONFIG_STATUS_INVALID => {
Err(DeviceConfigError::Invalid)
}
_ => panic!("libinput returned invalid 'libinput_config_status'"),
}
}
#[cfg(feature = "libinput_1_26")]
ffi_func!(
pub fn config_pressure_range_is_available, ffi::libinput_tablet_tool_config_pressure_range_is_available, bool);
#[cfg(feature = "libinput_1_26")]
ffi_func!(
pub fn config_pressure_range_get_minimum, ffi::libinput_tablet_tool_config_pressure_range_get_minimum, f64);
#[cfg(feature = "libinput_1_26")]
ffi_func!(
pub fn config_pressure_range_get_maximum, ffi::libinput_tablet_tool_config_pressure_range_get_maximum, f64);
#[cfg(feature = "libinput_1_26")]
ffi_func!(
pub fn config_pressure_range_get_default_minimum, ffi::libinput_tablet_tool_config_pressure_range_get_default_minimum, f64);
#[cfg(feature = "libinput_1_26")]
ffi_func!(
pub fn config_pressure_range_get_default_maximum, ffi::libinput_tablet_tool_config_pressure_range_get_default_maximum, f64);
}
#[cfg(feature = "libinput_1_29")]
#[doc(alias = "libinput_config_eraser_button_mode")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum EraserButtonMode {
Button,
Default,
}
#[cfg(feature = "libinput_1_29")]
impl EraserButtonMode {
fn from_ffi(v: ffi::libinput_config_eraser_button_mode) -> Self {
match v {
ffi::libinput_config_eraser_button_mode_LIBINPUT_CONFIG_ERASER_BUTTON_BUTTON => {
Self::Button
}
ffi::libinput_config_eraser_button_mode_LIBINPUT_CONFIG_ERASER_BUTTON_DEFAULT => {
Self::Default
}
_ => unreachable!(),
}
}
fn as_ffi(&self) -> ffi::libinput_config_eraser_button_mode {
match self {
EraserButtonMode::Button => {
ffi::libinput_config_eraser_button_mode_LIBINPUT_CONFIG_ERASER_BUTTON_BUTTON
}
EraserButtonMode::Default => {
ffi::libinput_config_eraser_button_mode_LIBINPUT_CONFIG_ERASER_BUTTON_DEFAULT
}
}
}
}
#[cfg(feature = "libinput_1_29")]
impl TabletTool {
#[doc(alias = "libinput_tablet_tool_config_eraser_button_get_button")]
pub fn config_eraser_button_get_button(&self) -> Option<u32> {
let btn = unsafe {
ffi::libinput_tablet_tool_config_eraser_button_get_button(self.as_raw_mut()) as u32
};
(btn != 0).then_some(btn)
}
#[doc(alias = "libinput_tablet_tool_config_eraser_button_get_default_button")]
pub fn config_eraser_button_get_default_button(&self) -> Option<u32> {
let btn = unsafe {
ffi::libinput_tablet_tool_config_eraser_button_get_default_button(self.as_raw_mut())
as u32
};
(btn != 0).then_some(btn)
}
#[doc(alias = "libinput_tablet_tool_config_eraser_button_get_mode")]
pub fn config_eraser_button_get_mode(&self) -> EraserButtonMode {
EraserButtonMode::from_ffi(unsafe {
ffi::libinput_tablet_tool_config_eraser_button_get_mode(self.as_raw_mut())
})
}
#[doc(alias = "libinput_tablet_tool_config_eraser_button_get_default_mode")]
pub fn config_eraser_button_get_default_mode(&self) -> EraserButtonMode {
EraserButtonMode::from_ffi(unsafe {
ffi::libinput_tablet_tool_config_eraser_button_get_default_mode(self.as_raw_mut())
})
}
ffi_func!(
#[doc(alias = "libinput_tablet_tool_config_eraser_button_get_modes")]
pub fn config_eraser_button_get_modes, ffi::libinput_tablet_tool_config_eraser_button_get_modes, u32);
#[doc(alias = "libinput_tablet_tool_config_eraser_button_set_button")]
pub fn config_eraser_button_set_button(&self, button: u32) -> DeviceConfigResult {
DeviceConfigError::from_ffi(unsafe {
ffi::libinput_tablet_tool_config_eraser_button_set_button(self.as_raw_mut(), button)
})
}
#[doc(alias = "libinput_tablet_tool_config_eraser_button_set_mode")]
pub fn config_eraser_button_set_mode(&self, mode: EraserButtonMode) -> DeviceConfigResult {
DeviceConfigError::from_ffi(unsafe {
ffi::libinput_tablet_tool_config_eraser_button_set_mode(
self.as_raw_mut(),
mode.as_ffi(),
)
})
}
}