Optimize the code based on the suggestions

This commit is contained in:
SnowNF 2024-03-20 11:31:42 +08:00 committed by Johan Malm
parent bd4d92bad8
commit fb921ab431
4 changed files with 22 additions and 8 deletions

View file

@ -30,8 +30,8 @@ struct libinput_category {
int dwt; /* -1 or libinput_config_dwt_state */ int dwt; /* -1 or libinput_config_dwt_state */
int click_method; /* -1 or libinput_config_click_method */ int click_method; /* -1 or libinput_config_click_method */
int send_events_mode; /* -1 or libinput_config_send_events_mode */ int send_events_mode; /* -1 or libinput_config_send_events_mode */
bool no_calibration_matrix; /* false if have calibration matrix */ bool have_calibration_matrix;
float calibration_matrix[6]; /* calibration matrix */ float calibration_matrix[6];
}; };
enum lab_libinput_device_type get_device_type(const char *s); enum lab_libinput_device_type get_device_type(const char *s);

View file

@ -25,7 +25,7 @@ libinput_category_init(struct libinput_category *l)
l->dwt = -1; l->dwt = -1;
l->click_method = -1; l->click_method = -1;
l->send_events_mode = -1; l->send_events_mode = -1;
l->no_calibration_matrix = true; l->have_calibration_matrix = false;
} }
enum lab_libinput_device_type enum lab_libinput_device_type

View file

@ -2,6 +2,7 @@
#define _POSIX_C_SOURCE 200809L #define _POSIX_C_SOURCE 200809L
#include <assert.h> #include <assert.h>
#include <fcntl.h> #include <fcntl.h>
#include <glib.h>
#include <libxml/parser.h> #include <libxml/parser.h>
#include <libxml/tree.h> #include <libxml/tree.h>
#include <stdbool.h> #include <stdbool.h>
@ -612,12 +613,24 @@ fill_libinput_category(char *nodename, char *content)
current_libinput_category->send_events_mode = current_libinput_category->send_events_mode =
get_send_events_mode(content); get_send_events_mode(content);
} else if (!strcasecmp(nodename, "calibrationMatrix")) { } else if (!strcasecmp(nodename, "calibrationMatrix")) {
float *m = current_libinput_category->calibration_matrix; errno = 0;
int r = sscanf(content, "%f%f%f%f%f%f", &m[0], &m[1], &m[2], &m[3], &m[4], &m[5]); current_libinput_category->have_calibration_matrix = true;
if (r == 6) { float *mat = current_libinput_category->calibration_matrix;
current_libinput_category->no_calibration_matrix = false; 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);
}
} }
static void static void

View file

@ -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); 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 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"); wlr_log(WLR_INFO, "calibration matrix not configured");
} else { } else {
wlr_log(WLR_INFO, "calibration matrix configured"); wlr_log(WLR_INFO, "calibration matrix configured");