1From 90a27f97bc58ff3d5cc8d603d06847194763f82d Mon Sep 17 00:00:00 2001
2From: Jeffy Chen <jeffy.chen@rock-chips.com>
3Date: Tue, 4 Aug 2020 14:30:33 +0800
4Subject: [PATCH 28/93] Support setting touch calibration through environment
5
6For example:
71/ Use weston-calibrator tool to get calibration data
8[root@rk3399:/]# weston-calibrator
9Final calibration values: -0.846961 0.122487 0.864176 -0.110204 -0.685913 0.958219
10
112/ Export calibration data through environment:
12export WESTON_TOUCH_CALIBRATION="-0.846961 0.122487 0.864176 -0.110204 -0.685913 0.958219"
13
14Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
15---
16 clients/calibrator.c        |  9 +++++++++
17 clients/touch-calibrator.c  | 12 ++++++++----
18 libweston/libinput-device.c | 13 +++++++++++++
19 3 files changed, 30 insertions(+), 4 deletions(-)
20
21diff --git a/clients/calibrator.c b/clients/calibrator.c
22index be65d9b..20fc1e7 100644
23--- a/clients/calibrator.c
24+++ b/clients/calibrator.c
25@@ -106,6 +106,7 @@ finish_calibration (struct calibrator *calibrator)
26 	struct weston_matrix m;
27 	struct weston_matrix inverse;
28 	struct weston_vector x_calib, y_calib;
29+	struct rectangle allocation;
30 	int i;
31
32
33@@ -141,6 +142,14 @@ finish_calibration (struct calibrator *calibrator)
34 		x_calib.f[0], x_calib.f[1], x_calib.f[2],
35 		y_calib.f[0], y_calib.f[1], y_calib.f[2]);
36
37+	widget_get_allocation(calibrator->widget, &allocation);
38+	x_calib.f[2] /= allocation.width;
39+	y_calib.f[2] /= allocation.height;
40+
41+	printf ("Final calibration values: %f %f %f %f %f %f\n",
42+		x_calib.f[0], x_calib.f[1], x_calib.f[2],
43+		y_calib.f[0], y_calib.f[1], y_calib.f[2]);
44+
45 	exit(0);
46 }
47
48diff --git a/clients/touch-calibrator.c b/clients/touch-calibrator.c
49index 49dd920..04a966b 100644
50--- a/clients/touch-calibrator.c
51+++ b/clients/touch-calibrator.c
52@@ -772,15 +772,19 @@ touch_device_handler(void *data, struct weston_touch_calibration *c,
53
54 	if (!cal->match_name) {
55 		printf("device \"%s\" - head \"%s\"\n", device, head);
56-		return;
57-	}
58
59-	if (cal->device_name)
60+		if (!cal->device_name)
61+			cal->device_name = strdup(device);
62 		return;
63+	}
64
65 	if (strcmp(cal->match_name, device) == 0 ||
66-	    strcmp(cal->match_name, head) == 0)
67+	    strcmp(cal->match_name, head) == 0) {
68+		if (cal->device_name)
69+			free(cal->device_name);
70+
71 		cal->device_name = strdup(device);
72+	}
73 }
74
75 struct weston_touch_calibration_listener touch_calibration_listener = {
76diff --git a/libweston/libinput-device.c b/libweston/libinput-device.c
77index 4ea89de..09bd459 100644
78--- a/libweston/libinput-device.c
79+++ b/libweston/libinput-device.c
80@@ -627,6 +627,19 @@ evdev_device_set_calibration(struct evdev_device *device)
81 		return;
82 	}
83
84+	calibration_values = getenv("WESTON_TOUCH_CALIBRATION");
85+	if (calibration_values && sscanf(calibration_values,
86+					 "%f %f %f %f %f %f",
87+					 &calibration.m[0],
88+					 &calibration.m[1],
89+					 &calibration.m[2],
90+					 &calibration.m[3],
91+					 &calibration.m[4],
92+					 &calibration.m[5]) == 6) {
93+		do_set_calibration(device, &calibration);
94+		return;
95+	}
96+
97 	width = device->output->width;
98 	height = device->output->height;
99 	if (width == 0 || height == 0)
100--
1012.20.1
102
103