All pastes #1429317 Raw Edit

sobkas

public text v1 · immutable
#1429317 ·published 2009-05-20 22:14 UTC
rendered paste body
diff -rup ~joystick-20051019/utils/jscal.c joystick-20051019/utils/jscal.c
--- ~joystick-20051019/utils/jscal.c	2009-05-19 12:07:27.000000000 +0200
+++ joystick-20051019/utils/jscal.c	2009-05-21 00:13:17.000000000 +0200
@@ -61,7 +61,9 @@ struct correction_data {
 
 int fd;
 struct js_corr corr[MAX_AXES];
+struct js_corr corr2[MAX_AXES];
 __u8 axmap[ABS_MAX + 1];
+__u8 axmap2[ABS_MAX + 1];
 __u16 buttonmap[(KEY_MAX - BTN_MISC + 1)];
 char axes, buttons, fuzz;
 int version;
@@ -354,8 +356,7 @@ void print_mappings(char *devicename)
 		exit(1);
 	}
 	if (ioctl(fd, JSIOCGBTNMAP, &buttonmap)) {
-		perror("jscal: error getting button map");
-		exit(1);
+	        buttons=0;
 	}
 
 	printf("jscal -u %d", axes);
@@ -373,6 +374,59 @@ void print_mappings(char *devicename)
 	printf(" %s\n",devicename);
 }
 
+
+void get_axmap2(void)
+{
+        if (ioctl(fd, JSIOCGAXMAP, &axmap2)) {
+		perror("jscal: error getting axis map");
+		exit(1);
+	}
+}
+
+int* compare_axes(void)
+{
+        int axmes[256];
+	int i;
+	int *ax=malloc(axes*sizeof(int));
+	for(i=0;i<axes;++i){
+	        axmes[(axmap2[i])]=i;
+	}
+	for(i=0;i<axes;++i){
+	        ax[i]=axmes[(axmap[i])];
+	}
+	return ax;
+}
+
+void correct_axes(int *p)
+{
+        int i;
+
+	if (ioctl(fd, JSIOCGAXES, &axes)) {
+		perror("jscal: error getting axes");
+		exit(1);
+	}
+	if (ioctl(fd, JSIOCGCORR, &corr2)) {
+		perror("jscal: error getting correction");
+		exit(1);
+	}
+
+	for (i = 0; i < axes; i++) {
+	        corr[i]=corr2[(p[i])];
+	}
+	if (ioctl(fd, JSIOCSCORR, &corr)) {
+		perror("jscal: error setting correction");
+		exit(1);
+	}
+
+}
+
+void correct_corr(void)
+{
+        int *x;
+        x=compare_axes();
+        correct_axes(x);
+}
+
 void print_settings(char *devicename)
 {
 	int i,j;
@@ -425,7 +479,7 @@ void set_mappings(char *p)
 		exit(1);
 	}
 
-  //axes
+   //axes
 	sscanf(p, "%d", &axes_on_cl);
 	p = strstr(p, ",");
 
@@ -457,15 +511,15 @@ void set_mappings(char *p)
 	sscanf(++p, "%d", &btns_on_cl);
 	p = strstr(p, ",");
 
-	if (btns_on_cl != buttons) {
+	if ((btns_on_cl != buttons)&&(btns_on_cl!=0)) {
 		fprintf(stderr, "jscal: joystick has %d buttons and not %d as specified on command line\n", 
 			buttons, btns_on_cl);
 		exit(1);
 	}
 
 
-	for (i = 0; i < buttons; i++)
-  {
+	for (i = 0; i < btns_on_cl; i++)
+	  {
 		if (!p) {
 			fprintf(stderr, "jscal: missing mapping for button %d\n", i);
 			exit(1);
@@ -483,21 +537,27 @@ void set_mappings(char *p)
 			exit(1);
 		}
 		buttonmap[i] = btn_mapping;
-	}
+	  }
 
 	if (p) {
 		fprintf(stderr, "jscal: too many values\n");
 		exit(1);
 	}
+	get_axmap2();
 	
 	if (ioctl(fd, JSIOCSAXMAP, &axmap)) {
 		perror("jscal: error setting axis map");
 		exit(1);
 	}
-	if (ioctl(fd, JSIOCSBTNMAP, &buttonmap)) {
-		perror("jscal: error setting button map");
-		exit(1);
-	}
+	
+	correct_corr();
+	
+	
+	if ((ioctl(fd, JSIOCSBTNMAP, &buttonmap))&&(btns_on_cl!=0)) {
+	        perror("jscal: error setting button map");
+	        exit(1);
+       }
+       
 }
 
 void set_correction(char *p)