]> git.mdlowis.com Git - proto/labwc.git/commitdiff
Optimize the code based on the suggestions
authorSnowNF <SnowNF@outlook.com>
Wed, 20 Mar 2024 03:31:42 +0000 (11:31 +0800)
committerJohan Malm <johanmalm@users.noreply.github.com>
Wed, 20 Mar 2024 22:52:19 +0000 (22:52 +0000)
include/config/libinput.h
src/config/libinput.c
src/config/rcxml.c
src/seat.c

index 59fbf76aec54dddbddc172d23d375ad554cf0673..0c211613a5a479d3a81a89f473bc44e796f645b9 100644 (file)
@@ -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);
index 65ee42c8bd4f416d872014cf3fe4b2589658786a..af4f1bd086e3c1f38ee40bc437cc24778b081c82 100644 (file)
@@ -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
index 83adc4f0400f8239bb9e8fb7f8404ef6f2cbee9b..d96daf8abb093477c372b9178873da56907e6e68 100644 (file)
@@ -2,6 +2,7 @@
 #define _POSIX_C_SOURCE 200809L
 #include <assert.h>
 #include <fcntl.h>
+#include <glib.h>
 #include <libxml/parser.h>
 #include <libxml/tree.h>
 #include <stdbool.h>
@@ -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);
        }
 }
 
index 8975088600f6cb8bb57ea532495459251741c338..db9b67757e0bcbdd30dddecb2ffe3870c41a5171 100644 (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);
        }
 
+       /* 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");