TSLib 增加校准后无需重启图形系统自动应用功能

直接上补丁喽

diff --git a/plugins/linear.c b/plugins/linear.c
index a24916a..90fc985 100644
--- a/plugins/linear.c
+++ b/plugins/linear.c
@@ -16,12 +16,15 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <time.h>
 
 #include <stdio.h>
 
 #include "tslib.h"
 #include "tslib-filter.h"
 
+#define TSLIB_LINEAR_TIMEOUT	5
+
 struct tslib_linear {
 	struct tslib_module_info module;
 	int	swap_xy;
@@ -33,14 +36,74 @@ struct tslib_linear {
 
 // Linear scaling and offset parameters for x,y (can include rotation)
 	int	a[7];
+
+// Timespec
+	struct timespec last;
 };
 
+static void
+linear_read_pointercal(struct tslib_linear *lin)
+{
+	struct stat sbuf;
+	int pcal_fd;
+	char pcalbuf[200];
+	int index;
+	char *tokptr;
+	char *calfile=NULL;
+	char *defaultcalfile = "/etc/pointercal";
+
+// Use default values that leave ts numbers unchanged after transform
+	lin->a[0] = 1;
+	lin->a[1] = 0;
+	lin->a[2] = 0;
+	lin->a[3] = 0;
+	lin->a[4] = 1;
+	lin->a[5] = 0;
+	lin->a[6] = 1;
+	lin->p_offset = 0;
+	lin->p_mult   = 1;
+	lin->p_div    = 1;
+	lin->swap_xy  = 0;
+
+	/*
+	 * Check calibration file
+	 */
+	if( (calfile = getenv("TSLIB_CALIBFILE")) == NULL) calfile = defaultcalfile;
+	if(stat(calfile,&sbuf)==0) {
+		pcal_fd = open(calfile,O_RDONLY);
+		read(pcal_fd,pcalbuf,200);
+		lin->a[0] = atoi(strtok(pcalbuf," "));
+		index=1;
+		while(index<7) {
+			tokptr = strtok(NULL," ");
+			if(*tokptr!='\0') {
+				lin->a[index] = atoi(tokptr);
+				index++;
+			}
+		}
+#ifdef DEBUG
+		printf("Linear calibration constants: ");
+		for(index=0;index<7;index++) printf("%d ",lin->a[index]);
+		printf("\n");
+#endif /*DEBUG*/
+		close(pcal_fd);
+	}
+}
+
 static int
 linear_read(struct tslib_module_info *info, struct ts_sample *samp, int nr)
 {
 	struct tslib_linear *lin = (struct tslib_linear *)info;
 	int ret;
 	int xtemp,ytemp;
+	struct timespec now;
+
+	clock_gettime(CLOCK_MONOTONIC, &now);
+	if(TSLIB_LINEAR_TIMEOUT < (now.tv_sec-lin->last.tv_sec))
+	{
+		linear_read_pointercal(lin);
+		memmove(&(lin->last), &now, sizeof(struct timespec));
+	}
 
 	ret = info->next->ops->read(info->next, samp, nr);
 	if (ret >= 0) {
@@ -102,13 +165,6 @@ TSAPI struct tslib_module_info *mod_init(struct tsdev *dev, const char *params)
 {
 
 	struct tslib_linear *lin;
-	struct stat sbuf;
-	int pcal_fd;
-	char pcalbuf[200];
-	int index;
-	char *tokptr;
-	char *calfile=NULL;
-	char *defaultcalfile = "/etc/pointercal";
 
 	lin = malloc(sizeof(struct tslib_linear));
 	if (lin == NULL)
@@ -116,45 +172,10 @@ TSAPI struct tslib_module_info *mod_init(struct tsdev *dev, const char *params)
 
 	lin->module.ops = &linear_ops;
 
-// Use default values that leave ts numbers unchanged after transform
-	lin->a[0] = 1;
-	lin->a[1] = 0;
-	lin->a[2] = 0;
-	lin->a[3] = 0;
-	lin->a[4] = 1;
-	lin->a[5] = 0;
-	lin->a[6] = 1;
-	lin->p_offset = 0;
-	lin->p_mult   = 1;
-	lin->p_div    = 1;
-	lin->swap_xy  = 0;
+	clock_gettime(CLOCK_MONOTONIC, &(lin->last));
+	linear_read_pointercal(lin);
 
 	/*
-	 * Check calibration file
-	 */
-	if( (calfile = getenv("TSLIB_CALIBFILE")) == NULL) calfile = defaultcalfile;
-	if(stat(calfile,&sbuf)==0) {
-		pcal_fd = open(calfile,O_RDONLY);
-		read(pcal_fd,pcalbuf,200);
-		lin->a[0] = atoi(strtok(pcalbuf," "));
-		index=1;
-		while(index<7) {
-			tokptr = strtok(NULL," ");
-			if(*tokptr!='\0') {
-				lin->a[index] = atoi(tokptr);
-				index++;
-			}
-		}
-#ifdef DEBUG
-		printf("Linear calibration constants: ");
-		for(index=0;index<7;index++) printf("%d ",lin->a[index]);
-		printf("\n");
-#endif /*DEBUG*/
-		close(pcal_fd);
-	}
-		
-		
-	/*
 	 * Parse the parameters.
 	 */
 	if (tslib_parse_vars(&lin->module, linear_vars, NR_VARS, params)) {

Over!

Leave a Reply

Your email address will not be published. Required fields are marked *