@@ -80,16 +80,16 @@ static int32_t mapButtonId(int32_t code)
8080 // buttons[3] Top button in right cluster
8181 // buttons[4] Top left front button
8282 // buttons[5] Top right front button
83- // buttons[6] Bottom left front button
84- // buttons[7] Bottom right front button
83+ // buttons[6] Bottom left front button (trigger)
84+ // buttons[7] Bottom right front button (trigger)
8585 // buttons[8] Left button in center cluster
8686 // buttons[9] Right button in center cluster
8787 // buttons[10] Left stick pressed button
8888 // buttons[11] Right stick pressed button
8989 // buttons[12] Top button in left cluster
9090 // buttons[13] Bottom button in left cluster
91- // buttons[14] Right button in left cluster
92- // buttons[15] Left button in left cluster
91+ // buttons[14] Right button in left cluster
92+ // buttons[15] Left button in left cluster
9393
9494 struct MapEntry {
9595 const int32_t code;
@@ -107,18 +107,28 @@ static int32_t mapButtonId(int32_t code)
107107 { BTN_TR2, 7 },
108108 { BTN_SELECT, 8 },
109109 { BTN_START, 9 },
110- { BTN_THUMBL, 10 },
111- { BTN_THUMBR, 11 },
110+ { BTN_THUMBL, 10 }, // stick pressed button
111+ { BTN_THUMBR, 11 }, // stick pressed button
112112 { ABS_HAT0Y, 12 },
113- { ABS_HAT0X, 14 }
113+ { ABS_HAT0X, 14 },
114+
115+ // TODO: Add configurable re-mapping for any controller
116+ //
117+ // Append: XBOX Wireless Controller
118+ //
119+ { ABS_BRAKE, 6 }, // trigger
120+ { ABS_GAS, 7 }, // trigger
121+ { KEY_BACK, 8 } // Left center "menu/start" button
122+
114123 };
115124
116125 static const size_t mapSize = sizeof (map)/sizeof (map[0 ]);
117126
118127 for (int i = 0 ; i < mapSize; ++i)
128+ {
119129 if (code == map[i].code )
120130 return map[i].id ;
121-
131+ }
122132 return -1 ;
123133}
124134
@@ -137,7 +147,7 @@ static int mapAxisId(int32_t code)
137147 { ABS_X, 0 },
138148 { ABS_Y, 1 },
139149 { ABS_Z, 2 },
140- { ABS_RZ, 3 }
150+ { ABS_RZ, 3 },
141151 };
142152
143153 static const size_t mapSize = sizeof (map)/sizeof (map[0 ]);
@@ -164,8 +174,10 @@ static double mapAxisValue(int32_t val, int32_t min, int32_t max)
164174 double offset = (max + min) / (kMappedMax - kMappedMin ) * scale * kMappedMin ;
165175 double mappedVal = val * scale + offset;
166176 mappedVal = clamp (mappedVal, kMappedMin , kMappedMax );
177+
167178 if (mappedVal < 0.009 && mappedVal > -0.009 )
168- return 0.0 ;
179+ mappedVal = 0.0 ;
180+
169181 return mappedVal;
170182}
171183
@@ -304,7 +316,7 @@ struct GamepadProvider
304316 if (fd < 0 )
305317 return false ;
306318
307- unsigned long ev_bits[NBITS (EV_CNT)] = {0 };
319+ unsigned long ev_bits[NBITS ( EV_CNT)] = {0 };
308320 unsigned long key_bits[NBITS (KEY_CNT)] = {0 };
309321 unsigned long abs_bits[NBITS (ABS_CNT)] = {0 };
310322
@@ -369,6 +381,7 @@ struct GamepadProvider
369381 {
370382 SourceData *sourcedata = static_cast <SourceData*>(data);
371383 GamepadProvider& self = *(sourcedata->first );
384+
372385 int32_t gamepad_id = sourcedata->second ;
373386 if (condition & (G_IO_HUP | G_IO_ERR)) {
374387 self.closeGamepad (gamepad_id);
@@ -572,8 +585,9 @@ void GamepadProvider::updateGamepad(GamepadInfo& info, std::vector<input_event>
572585 const auto updateButtonValue = [&info, &buttonValuesChanged] (int32_t code, int32_t value)
573586 {
574587 const int32_t button_id = mapButtonId (code);
588+
575589 if (button_id < 0 || button_id >= info.buttonValues .size ()) {
576- g_warning (" unmapped button code: %d \n " , code);
590+ g_warning (" unmapped button code: %d 0x%04X \n " , code , code);
577591 return ;
578592 }
579593 info.buttonValues [button_id] = value == 0 ? 0.0 : 1.0 ;
@@ -584,12 +598,12 @@ void GamepadProvider::updateGamepad(GamepadInfo& info, std::vector<input_event>
584598 {
585599 // todo: figure out a better to handle dpad
586600 if (code != ABS_HAT0X && code != ABS_HAT0Y) {
587- g_warning (" not supported code: %d \n " , code);
601+ g_warning (" not supported code: %d 0x%04X \n " , code , code);
588602 return ;
589603 }
590604 const int32_t idx_base = mapButtonId (code);
591605 if ((idx_base < 0 ) || (idx_base + 1 >= info.buttonValues .size ())) {
592- g_warning (" unmapped button code: %d \n " , code);
606+ g_warning (" unmapped button code: %d 0x%04X \n " , code , code);
593607 return ;
594608 }
595609 info.buttonValues [idx_base] = 0.0 ;
@@ -599,6 +613,32 @@ void GamepadProvider::updateGamepad(GamepadInfo& info, std::vector<input_event>
599613 buttonValuesChanged = true ;
600614 };
601615
616+ const auto updateTriggerValue = [&info, &buttonValuesChanged] (int32_t code, int32_t value)
617+ {
618+ const int32_t idx_base = mapButtonId (code);
619+ if ((idx_base < 0 ) || (idx_base + 1 >= info.buttonValues .size ())) {
620+ g_warning (" unmapped trigger code: %d 0x%04X\n " , code, code);
621+ return ;
622+ }
623+
624+ auto info_iter = info.axisInfo .find (code);
625+ if (info_iter == info.axisInfo .end ()) {
626+ g_warning (" no trigger info: %d \n " , code);
627+ return ;
628+ }
629+
630+ const input_absinfo& axisInfo = info_iter->second ;
631+ double mapped = mapAxisValue (value, axisInfo.minimum , axisInfo.maximum );
632+
633+ // Need to Normalize values in [ -1 <-> 1 ] to [ 0 <-> 1 ]
634+ //
635+ // First [ -1 <-> 1 ] / 2 = [ -0.5 <-> 0.5 ]
636+ // Then [ -0.5 <-> 0.5] + 0.5 = [ 0.0 <-> 1.0 ]
637+ //
638+ info.buttonValues [idx_base] = (mapped/2 ) + 0.5 ;
639+ buttonValuesChanged = true ;
640+ };
641+
602642 const auto updateAxisValue = [&info, &axisValuesChanged] (int32_t code, int32_t value)
603643 {
604644 auto info_iter = info.axisInfo .find (code);
@@ -611,8 +651,10 @@ void GamepadProvider::updateGamepad(GamepadInfo& info, std::vector<input_event>
611651 g_warning (" unmapped axis code: %d \n " , code);
612652 return ;
613653 }
654+
614655 const input_absinfo& axisInfo = info_iter->second ;
615656 double mapped = mapAxisValue (value, axisInfo.minimum , axisInfo.maximum );
657+
616658 info.axisValues [axis_id] = mapped;
617659 axisValuesChanged = true ;
618660 };
@@ -628,6 +670,9 @@ void GamepadProvider::updateGamepad(GamepadInfo& info, std::vector<input_event>
628670 }
629671 case EV_ABS:
630672 {
673+ if (event.code == ABS_GAS || event.code == ABS_BRAKE) // L/R triggers
674+ updateTriggerValue (event.code , event.value );
675+ else
631676 if (event.code == ABS_HAT0X || event.code == ABS_HAT0Y)
632677 updateDpadButtonValue (event.code , event.value );
633678 else
0 commit comments