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 6, 2024
1 parent e151cf5 commit 20e88b8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions metadata/input-device.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
<option name="output" type="string">
<default></default>
</option>
<option name="calibration" type="string">
<default></default>
</option>
</object>
</wayfire>
40 changes: 40 additions & 0 deletions src/core/seat/input-manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,40 @@ void wf::input_manager_t::handle_new_input(wlr_input_device *dev)
configure_input_devices();
}

void wf::input_manager_t::calibrate_touch_device(wlr_input_device *dev, std::string const & cal)
{
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);
}
}

void wf::input_manager_t::configure_input_device(wlr_input_device *dev)
{
auto cursor = wf::get_core().get_wlr_cursor();
Expand All @@ -78,6 +112,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())
{
calibrate_touch_device(dev, cal);
}

auto wo = wf::get_core().output_layout->find_output(mapped_output);
if (wo)
{
Expand Down
6 changes: 6 additions & 0 deletions src/core/seat/input-manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class input_manager_t
*/
void configure_input_devices();

/**
* 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(wlr_input_device *dev, std::string const & cal);

input_manager_t();
~input_manager_t() = default;

Expand Down

0 comments on commit 20e88b8

Please sign in to comment.