Skip to content

Commit

Permalink
input-device: Add calibration option for touch input devices
Browse files Browse the repository at this point in the history
This adds a calibration option so that touch input devices can be calibrated
via wayfire configuration.
  • Loading branch information
soreau committed Nov 22, 2024
1 parent 464c5e6 commit be42cbb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/api/wayfire/input-device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ class input_device_t
*/
bool set_enabled(bool enabled = true);

/**
* Calibrate a touch device with a matrix. This function does nothing
* if called with a device that is not a touch device.
*/
void calibrate_touch_device(std::string const & cal);

/**
* @return true if the compositor should receive events from the device
*/
Expand Down
11 changes: 9 additions & 2 deletions src/core/seat/input-manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ void wf::input_manager_t::handle_new_input(wlr_input_device *dev)
configure_input_devices();
}

void wf::input_manager_t::configure_input_device(wlr_input_device *dev)
void wf::input_manager_t::configure_input_device(std::unique_ptr<wf::input_device_impl_t> & device)
{
auto dev = device->get_wlr_handle();
auto cursor = wf::get_core().get_wlr_cursor();
auto section =
wf::get_core().config_backend->get_input_device_section(dev);
Expand All @@ -78,6 +79,12 @@ void wf::input_manager_t::configure_input_device(wlr_input_device *dev)
}
}

auto cal = section->get_option("calibration")->get_value_str();
if (!cal.empty())
{
device->calibrate_touch_device(cal);
}

auto wo = wf::get_core().output_layout->find_output(mapped_output);
if (wo)
{
Expand All @@ -101,7 +108,7 @@ void wf::input_manager_t::configure_input_devices()

for (auto& device : this->input_devices)
{
configure_input_device(device->get_wlr_handle());
configure_input_device(device);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/seat/input-manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class input_manager_t
* Map a single input device to output as specified in the
* config file or by hints in the wlroots backend.
*/
void configure_input_device(wlr_input_device *dev);
void configure_input_device(std::unique_ptr<wf::input_device_impl_t> & device);

/**
* Go through all input devices and map them to outputs as specified in the
Expand Down
35 changes: 35 additions & 0 deletions src/core/seat/seat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,41 @@ bool input_device_t::is_enabled()
return mode == LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
}

void input_device_t::calibrate_touch_device(std::string const & cal)
{
wlr_input_device *dev = handle;
if (!wlr_input_device_is_libinput(dev) || (dev->type != WLR_INPUT_DEVICE_TOUCH))
{
return;
}

float m[6];
auto libinput_dev = wlr_libinput_get_device_handle(dev);
if (sscanf(cal.c_str(), "%f %f %f %f %f %f",
&m[0], &m[1], &m[2], &m[3], &m[4], &m[5]) == 6)
{
enum libinput_config_status status;

status = libinput_device_config_calibration_set_matrix(libinput_dev, m);
if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
{
LOGE("Failed to apply calibration for ", nonull(dev->name));
LOGE(" ", m[0], " ", m[1], " ", m[2], " ", m[3], " ", m[4], " ", m[5]);
} else
{
LOGI("Calibrated input device successfully: ", nonull(dev->name));
LOGI(" ", m[0], " ", m[1], " ", m[2], " ", m[3], " ", m[4], " ", m[5]);
}
} else
{
LOGE("Incorrect calibration configuration for ", nonull(dev->name));
LOGI("Setting default matrix calibration: ");
libinput_device_config_calibration_get_default_matrix(libinput_dev, m);
LOGI(" ", m[0], " ", m[1], " ", m[2], " ", m[3], " ", m[4], " ", m[5]);
libinput_device_config_calibration_set_matrix(libinput_dev, m);
}
}

input_device_t::input_device_t(wlr_input_device *handle)
{
this->handle = handle;
Expand Down

0 comments on commit be42cbb

Please sign in to comment.