From fb921ab4319f170d6855e22432f75c4bf3769ec5 Mon Sep 17 00:00:00 2001 From: SnowNF Date: Wed, 20 Mar 2024 11:31:42 +0800 Subject: [PATCH] Optimize the code based on the suggestions --- include/config/libinput.h | 4 ++-- src/config/libinput.c | 2 +- src/config/rcxml.c | 21 +++++++++++++++++---- src/seat.c | 3 ++- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/include/config/libinput.h b/include/config/libinput.h index 59fbf76a..0c211613 100644 --- a/include/config/libinput.h +++ b/include/config/libinput.h @@ -30,8 +30,8 @@ struct libinput_category { int dwt; /* -1 or libinput_config_dwt_state */ int click_method; /* -1 or libinput_config_click_method */ int send_events_mode; /* -1 or libinput_config_send_events_mode */ - bool no_calibration_matrix; /* false if have calibration matrix */ - float calibration_matrix[6]; /* calibration matrix */ + bool have_calibration_matrix; + float calibration_matrix[6]; }; enum lab_libinput_device_type get_device_type(const char *s); diff --git a/src/config/libinput.c b/src/config/libinput.c index 65ee42c8..af4f1bd0 100644 --- a/src/config/libinput.c +++ b/src/config/libinput.c @@ -25,7 +25,7 @@ libinput_category_init(struct libinput_category *l) l->dwt = -1; l->click_method = -1; l->send_events_mode = -1; - l->no_calibration_matrix = true; + l->have_calibration_matrix = false; } enum lab_libinput_device_type diff --git a/src/config/rcxml.c b/src/config/rcxml.c index 83adc4f0..d96daf8a 100644 --- a/src/config/rcxml.c +++ b/src/config/rcxml.c @@ -2,6 +2,7 @@ #define _POSIX_C_SOURCE 200809L #include #include +#include #include #include #include @@ -612,11 +613,23 @@ fill_libinput_category(char *nodename, char *content) current_libinput_category->send_events_mode = get_send_events_mode(content); } else if (!strcasecmp(nodename, "calibrationMatrix")) { - float *m = current_libinput_category->calibration_matrix; - int r = sscanf(content, "%f%f%f%f%f%f", &m[0], &m[1], &m[2], &m[3], &m[4], &m[5]); - if (r == 6) { - current_libinput_category->no_calibration_matrix = false; + errno = 0; + current_libinput_category->have_calibration_matrix = true; + float *mat = current_libinput_category->calibration_matrix; + gchar **elements = g_strsplit(content, " ", -1); + guint length = g_strv_length(elements); + for (guint i = 0; i < length; ++i) { + char *end_str = NULL; + mat[i] = strtof(elements[i], &end_str); + if (i == 6 || errno == ERANGE || !end_str) { + wlr_log(WLR_ERROR, + "bad calibration matrix value, expect six floats"); + current_libinput_category->have_calibration_matrix = false; + errno = 0; + break; + } } + g_strfreev(elements); } } diff --git a/src/seat.c b/src/seat.c index 89750886..db9b6775 100644 --- a/src/seat.c +++ b/src/seat.c @@ -238,8 +238,9 @@ configure_libinput(struct wlr_input_device *wlr_input_device) libinput_device_config_send_events_set_mode(libinput_dev, dc->send_events_mode); } + /* Non-zero if the device can be calibrated, zero otherwise. */ if (libinput_device_config_calibration_has_matrix(libinput_dev) == 0 - || dc->no_calibration_matrix) { + || !dc->have_calibration_matrix) { wlr_log(WLR_INFO, "calibration matrix not configured"); } else { wlr_log(WLR_INFO, "calibration matrix configured");