*/}}
Browse Source

Controller btn/axis continuous mapping

YimingWu 3 months ago
parent
commit
6bc298dcbd
4 changed files with 72 additions and 47 deletions
  1. 53 30
      la_controllers.c
  2. 2 0
      la_interface.h
  3. 13 13
      la_util.c
  4. 4 4
      la_util.h

+ 53 - 30
la_controllers.c

@@ -77,6 +77,27 @@ void la_ReadControllerAxisLimit(laController* c, int i, int Min, int Max){
     c->AxisLimitMins[i] = -32768*0.95;
     c->AxisLimitMaxes[i] = 32767*0.95;
 }
+
+int la_ControllerButtonToMap(int btn){
+    if(btn<BTN_MISC) return -1;
+    if(btn<=BTN_GEAR_UP) return btn-BTN_MISC;
+    if(btn<BTN_DPAD_UP) return -1;
+    if(btn<=BTN_DPAD_RIGHT) return BTN_GEAR_UP-BTN_MISC+1+btn-BTN_DPAD_UP;
+    if(btn<BTN_TRIGGER_HAPPY) return -1;
+    if(btn<=BTN_TRIGGER_HAPPY40) return BTN_GEAR_UP-BTN_MISC+1+BTN_DPAD_RIGHT-BTN_DPAD_UP+1+btn-BTN_TRIGGER_HAPPY;
+    return -1;
+}
+int la_ControllerButtonToIndex(laController* c,int btn){
+    int map=la_ControllerButtonToMap(btn); if(map<0) return -1; return c->ButtonsMap[map];
+}
+int la_ControllerAxisToMap(int abs){
+    if(abs<ABS_X) return -1; if(abs>ABS_MAX) return -1;
+    return abs;
+}
+int la_ControllerAxisToIndex(laController* c,int abs){
+    int map=la_ControllerAxisToMap(abs); if(map<0) return -1; return c->AxisMap[map];
+}
+
 void la_InitControllers(){
 #ifdef __linux__
     char path[32]="/dev/input/js";
@@ -90,7 +111,7 @@ void la_InitControllers(){
 		if (file != -1){
 			// Get name
 			ioctl(file, EVIOCGNAME(128), name);
-            printf("%s ",name);
+            printf("%s\n",name);
 
             barray_t *abs_barray = barray_init(ABS_CNT);
             ioctl(file, EVIOCGBIT(EV_ABS, ABS_CNT), barray_data(abs_barray));
@@ -104,9 +125,19 @@ void la_InitControllers(){
 
             laController* c=la_NewController(name, fileName, file, abs_count, key_count);
 
-			for (unsigned int j=0; j<abs_count; j++){ struct input_absinfo axisInfo;
-				if (ioctl(file, EVIOCGABS(j), &axisInfo) != -1){
-                    la_ReadControllerAxisLimit(c,j,axisInfo.minimum,axisInfo.maximum);
+            int nextid=0;
+            for (unsigned int j=0; j<KEY_CNT; j++){
+                if(barray_is_set(key_barray,j)){
+                    int mapid=la_ControllerButtonToMap(j); if(mapid<0){ continue; }
+                    c->ButtonsMap[mapid]=nextid; nextid++;
+                }
+			}
+            nextid=0;
+			for (unsigned int j=0; j<ABS_CNT; j++){ struct input_absinfo axisInfo;
+				if (barray_is_set(abs_barray,j) && (ioctl(file, EVIOCGABS(j), &axisInfo) != -1)){
+                    int mapid=la_ControllerAxisToMap(j); if(mapid<0){ continue; }
+                    la_ReadControllerAxisLimit(c,nextid,axisInfo.minimum,axisInfo.maximum);
+                    c->AxisMap[mapid]=nextid; nextid++;
 				}
 			}
 
@@ -123,22 +154,14 @@ void la_UpdateControllerStatus(){
     for(laController* c=MAIN.Controllers.pFirst;c;c=c->Item.pNext){ if(c->Error) continue;
         int bytes; while((bytes=read(c->fd, &event, sizeof(struct input_event)))>0){
             if(event.type == EV_KEY){
-                printf("b %d %d", event.code, event.value);
-                if(event.code>=BTN_JOYSTICK && event.code<=BTN_THUMBR){
-                    event.code -= BTN_JOYSTICK;
-                }elif(event.code>=BTN_TRIGGER_HAPPY && event.code<=BTN_TRIGGER_HAPPY40){
-                    event.code=event.code-BTN_TRIGGER_HAPPY+BTN_THUMBR-BTN_JOYSTICK+1;
-                }
-                printf(" %d \n", event.code, event.value);
-                //if(event.code>=c->NumButtons+BTN_THUMBR) continue;
-                c->ButtonValues[event.code]=event.value; HasEvent=1;
+                int idx = la_ControllerButtonToIndex(c,event.code); if(idx<0) continue;
+                c->ButtonValues[idx]=event.value; HasEvent=1;
+                //printf("b %d\n", idx);
             }
             else if(event.type == EV_ABS){
-                if(event.code>=c->NumAxes+ABS_X) continue;
-                int axis=event.code-ABS_X;
-                c->AxisValues[axis]=rint(tnsLinearItp(-32768.0f,32767.0f,((real)event.value/(c->AxisMaxes[axis]-c->AxisMins[axis]))));
-                HasEvent=1;
-                //printf("a %d %d\n", event.code, event.value);
+                int idx = la_ControllerAxisToIndex(c,event.code); if(idx<0) continue;
+                c->AxisValues[idx]=rint(tnsLinearItp(-32768.0f,32767.0f,((real)event.value/(c->AxisMaxes[idx]-c->AxisMins[idx]))));
+                HasEvent=1; //printf("a %d %d\n", idx, event.value);
             }
         }
         if(bytes<=0){ struct stat buffer; if(stat(c->Path->Ptr,&buffer)<0){ c->Error=1; HasEvent=1; } }
@@ -251,18 +274,18 @@ void la_RegisterControllerProps(){
     la_AddButtonProp(pc,"t2_up","T2+","Toggle 2+", 13,0,0);
     la_AddButtonProp(pc,"t2_dn","T2-","Toggle 2-", 14,0,0);
     la_AddButtonProp(pc,"t3_up","T3+","Toggle 3+", 15,0,0);
-    la_AddButtonProp(pc,"t3_dn","T3-","Toggle 3-", 31,0,0);
-    la_AddButtonProp(pc,"t4_up","T4+","Toggle 4+", 32,0,0);
-    la_AddButtonProp(pc,"t4_dn","T4-","Toggle 4-", 33,0,0);
-    la_AddButtonProp(pc,"h3","H3","Hat 3 (Upper round hat)", 34,4,"N,E,S,W");
-    la_AddButtonProp(pc,"h4","H4","Hat 4 (lower jagged hat)", 38,4,"N,E,S,W");
-    la_AddButtonProp(pc,"pinky_up","P+","Pinky up", 42,0,0);
-    la_AddButtonProp(pc,"pinky_dn","P-","Pinky down", 43,0,0);
-    la_AddButtonProp(pc,"dial_fwd","D+","Dial forward", 44,0,0);
-    la_AddButtonProp(pc,"dial_back","D-","Dial backward", 45,0,0);
-    la_AddButtonProp(pc,"bball","BP","Ball push", 46,0,0);
-    la_AddButtonProp(pc,"slider","SLD","Slider", 47,0,0);
-    la_AddButtonProp(pc,"mode","Mode","Mode switch", 48,3,"M1,M2,S1");
+    la_AddButtonProp(pc,"t3_dn","T3-","Toggle 3-", 16,0,0);
+    la_AddButtonProp(pc,"t4_up","T4+","Toggle 4+", 17,0,0);
+    la_AddButtonProp(pc,"t4_dn","T4-","Toggle 4-", 18,0,0);
+    la_AddButtonProp(pc,"h3","H3","Hat 3 (Upper round hat)", 19,4,"N,E,S,W");
+    la_AddButtonProp(pc,"h4","H4","Hat 4 (lower jagged hat)", 23,4,"N,E,S,W");
+    la_AddButtonProp(pc,"pinky_up","P+","Pinky up", 27,0,0);
+    la_AddButtonProp(pc,"pinky_dn","P-","Pinky down", 28,0,0);
+    la_AddButtonProp(pc,"dial_fwd","D+","Dial forward", 29,0,0);
+    la_AddButtonProp(pc,"dial_back","D-","Dial backward", 30,0,0);
+    la_AddButtonProp(pc,"bball","BP","Ball push", 31,0,0);
+    la_AddButtonProp(pc,"slider","SLD","Slider", 32,0,0);
+    la_AddButtonProp(pc,"mode","Mode","Mode switch", 33,3,"M1,M2,S1");
 
 
     pc=laAddPropertyContainer("la_controller_x56_stick", "X56 Stick", "X56 Stick", 0,laui_X56Stick,sizeof(laController),0,0,1);

+ 2 - 0
la_interface.h

@@ -1475,6 +1475,8 @@ STRUCTURE(laController){
     int AxisCenterMaxes[LA_JS_MAX_AXES]; // calibrated center;
     int AxisCenterMins[LA_JS_MAX_AXES];
     char ButtonValues[LA_JS_MAX_BUTTONS];
+    u8bit ButtonsMap[128];
+    u8bit AxisMap[64];
 };
 
 void la_InitControllers();

+ 13 - 13
la_util.c

@@ -154,7 +154,7 @@ int nutSameAddress(void *l, void *r){
 barray_t *barray_init(size_t num_bits)
 {
     size_t num_longs = BITS_TO_LONGS(num_bits);
-    barray_t *barray = malloc(sizeof(unsigned long) * num_longs + sizeof(barray_t));
+    barray_t *barray = calloc(1,sizeof(u64bit) * num_longs + sizeof(barray_t));
     barray->num_bits = num_bits;
     barray->num_longs = num_longs;
     return barray;
@@ -163,7 +163,7 @@ void barray_free(barray_t *barray)
 {
     free(barray);
 }
-unsigned long *barray_data(barray_t *barray)
+u64bit *barray_data(barray_t *barray)
 {
     return barray->data;
 }
@@ -171,7 +171,7 @@ size_t barray_count_set(barray_t *barray)
 {
     size_t count = 0;
     for (int i = 0; i < barray->num_longs; i++)
-        count += __builtin_popcountl(barray->data[i]);
+        count += __builtin_popcountll(barray->data[i]);
     return count;
 }
 void barray_set(barray_t *barray, bit_t bit)
@@ -182,7 +182,7 @@ void barray_set(barray_t *barray, bit_t bit)
     int index = bit / BITS_PER_LONG;
     int shift = bit % BITS_PER_LONG;
 
-    barray->data[index] |= (1 << shift);
+    barray->data[index] |= ((u64bit)1 << shift);
 }
 void barray_clear(barray_t *barray, bit_t bit)
 {
@@ -192,7 +192,7 @@ void barray_clear(barray_t *barray, bit_t bit)
     int index = bit / BITS_PER_LONG;
     int shift = bit % BITS_PER_LONG;
 
-    barray->data[index] &= ~(1 << shift);
+    barray->data[index] &= ~((u64bit)1 << shift);
 }
 bool barray_is_set(barray_t *barray, bit_t bit)
 {
@@ -201,13 +201,13 @@ bool barray_is_set(barray_t *barray, bit_t bit)
 
     int index = bit / BITS_PER_LONG;
     int shift = bit % BITS_PER_LONG;
-    return (barray->data[index] & (1 << shift)) != 0;
+    return (barray->data[index] & ((u64bit)1 << shift)) != 0;
 }
 void barray_foreach_set(barray_t *barray, barray_callback_t callback, void *arg)
 {
     for (int i = 0; i < barray->num_longs; i++)
     {
-        unsigned long bits = barray->data[i];
+        u64bit bits = barray->data[i];
         while (bits != 0)
         {
             callback(i * BITS_PER_LONG + __builtin_ctzl(bits), arg);
@@ -1020,7 +1020,7 @@ void hshFree(laHash65536** h){
     if(!h || !*h) return; free(*h); *h=0;
 }
 
-laListHandle* hsh65536DoHashLongPtr(laHash65536* hash, unsigned long long buckle) {
+laListHandle* hsh65536DoHashLongPtr(laHash65536* hash, u64bit buckle) {
 	return &hash->Entries[(unsigned short)(buckle*13)];
 }
 laListHandle* hsh65536DoHashNUID(laHash65536* hash, char * NUID) {
@@ -2223,8 +2223,8 @@ void laOpenInternetLink(char *url){
     //HKEY hkRoot, hSubKey;
     //char ValueName[256]={0};
     //char DataValue[256]={0};
-    //unsigned long cbValueName = 256;
-    //unsigned long cbDataValue = 256;
+    //u64bit cbValueName = 256;
+    //u64bit cbDataValue = 256;
     //char ShellChar[512]={0};
     //DWORD dwType;
     //
@@ -2244,12 +2244,12 @@ void usleep(unsigned int usec){
         init = 1;
         const HINSTANCE ntdll = LoadLibrary("ntdll.dll");
         if (ntdll != NULL){
-            typedef long(NTAPI* pNtQueryTimerResolution)(unsigned long* MinimumResolution, unsigned long* MaximumResolution, unsigned long* CurrentResolution);
-            typedef long(NTAPI* pNtSetTimerResolution)(unsigned long RequestedResolution, char SetResolution, unsigned long* ActualResolution);
+            typedef long(NTAPI* pNtQueryTimerResolution)(u64bit* MinimumResolution, u64bit* MaximumResolution, u64bit* CurrentResolution);
+            typedef long(NTAPI* pNtSetTimerResolution)(u64bit RequestedResolution, char SetResolution, u64bit* ActualResolution);
             pNtQueryTimerResolution NtQueryTimerResolution = (pNtQueryTimerResolution)GetProcAddress(ntdll, "NtQueryTimerResolution");
             pNtSetTimerResolution   NtSetTimerResolution = (pNtSetTimerResolution)GetProcAddress(ntdll, "NtSetTimerResolution");
             if (NtQueryTimerResolution != NULL && NtSetTimerResolution != NULL){
-                unsigned long minimum, maximum, current;
+                u64bit minimum, maximum, current;
                 NtQueryTimerResolution(&minimum, &maximum, &current);
                 NtSetTimerResolution(maximum, (char)1, &current);
             }

+ 4 - 4
la_util.h

@@ -619,7 +619,7 @@ void hsh65536InsertItem(laHash65536* hash, laListItem* li, long buckle);
 
 void hsh65536Init(laHash65536** h);
 void hshFree(laHash65536** h);
-laListHandle* hsh65536DoHashLongPtr(laHash65536* hash, unsigned long long buckle);
+laListHandle* hsh65536DoHashLongPtr(laHash65536* hash, u64bit buckle);
 laListHandle* hsh65536DoHashNUID(laHash65536* hash, char * NUID);
 
 laListItem* hsh256FindItemSTR(laHash256* hash, laCompareFunc func, char * buckle);
@@ -747,7 +747,7 @@ typedef unsigned bit_t;
 
 barray_t *barray_init(size_t num_bits);
 void barray_free(barray_t *barray);
-unsigned long *barray_data(barray_t *barray);
+u64bit *barray_data(barray_t *barray);
 size_t barray_count_set(barray_t *barray);
 void barray_set(barray_t *barray, bit_t bit);
 void barray_clear(barray_t *barray, bit_t bit);
@@ -755,14 +755,14 @@ bool barray_is_set(barray_t *barray, bit_t bit);
 typedef void (*barray_callback_t)(bit_t bit, void *arg);
 void barray_foreach_set(barray_t *barray, barray_callback_t callback, void *arg);
 
-#define BITS_PER_LONG        (sizeof(unsigned long) * 8)
+#define BITS_PER_LONG        (sizeof(u64bit) * 8)
 #define BITS_TO_LONGS(n)     (((n) + BITS_PER_LONG - 1) / BITS_PER_LONG)
 
 struct barray
 {
     size_t num_bits;
     size_t num_longs;
-    unsigned long data[0];
+    u64bit data[0];
 };
 
 void laOpenInternetLink(char* url);