@@ -80,16 +80,16 @@ static int32_t mapButtonId(int32_t code)
80
80
// buttons[3] Top button in right cluster
81
81
// buttons[4] Top left front button
82
82
// 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)
85
85
// buttons[8] Left button in center cluster
86
86
// buttons[9] Right button in center cluster
87
87
// buttons[10] Left stick pressed button
88
88
// buttons[11] Right stick pressed button
89
89
// buttons[12] Top button in left cluster
90
90
// 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
93
93
94
94
struct MapEntry {
95
95
const int32_t code;
@@ -107,18 +107,28 @@ static int32_t mapButtonId(int32_t code)
107
107
{ BTN_TR2, 7 },
108
108
{ BTN_SELECT, 8 },
109
109
{ 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
112
112
{ 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
+
114
123
};
115
124
116
125
static const size_t mapSize = sizeof (map)/sizeof (map[0 ]);
117
126
118
127
for (int i = 0 ; i < mapSize; ++i)
128
+ {
119
129
if (code == map[i].code )
120
130
return map[i].id ;
121
-
131
+ }
122
132
return -1 ;
123
133
}
124
134
@@ -137,7 +147,7 @@ static int mapAxisId(int32_t code)
137
147
{ ABS_X, 0 },
138
148
{ ABS_Y, 1 },
139
149
{ ABS_Z, 2 },
140
- { ABS_RZ, 3 }
150
+ { ABS_RZ, 3 },
141
151
};
142
152
143
153
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)
164
174
double offset = (max + min) / (kMappedMax - kMappedMin ) * scale * kMappedMin ;
165
175
double mappedVal = val * scale + offset;
166
176
mappedVal = clamp (mappedVal, kMappedMin , kMappedMax );
177
+
167
178
if (mappedVal < 0.009 && mappedVal > -0.009 )
168
- return 0.0 ;
179
+ mappedVal = 0.0 ;
180
+
169
181
return mappedVal;
170
182
}
171
183
@@ -304,7 +316,7 @@ struct GamepadProvider
304
316
if (fd < 0 )
305
317
return false ;
306
318
307
- unsigned long ev_bits[NBITS (EV_CNT)] = {0 };
319
+ unsigned long ev_bits[NBITS ( EV_CNT)] = {0 };
308
320
unsigned long key_bits[NBITS (KEY_CNT)] = {0 };
309
321
unsigned long abs_bits[NBITS (ABS_CNT)] = {0 };
310
322
@@ -369,6 +381,7 @@ struct GamepadProvider
369
381
{
370
382
SourceData *sourcedata = static_cast <SourceData*>(data);
371
383
GamepadProvider& self = *(sourcedata->first );
384
+
372
385
int32_t gamepad_id = sourcedata->second ;
373
386
if (condition & (G_IO_HUP | G_IO_ERR)) {
374
387
self.closeGamepad (gamepad_id);
@@ -572,8 +585,9 @@ void GamepadProvider::updateGamepad(GamepadInfo& info, std::vector<input_event>
572
585
const auto updateButtonValue = [&info, &buttonValuesChanged] (int32_t code, int32_t value)
573
586
{
574
587
const int32_t button_id = mapButtonId (code);
588
+
575
589
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);
577
591
return ;
578
592
}
579
593
info.buttonValues [button_id] = value == 0 ? 0.0 : 1.0 ;
@@ -584,12 +598,12 @@ void GamepadProvider::updateGamepad(GamepadInfo& info, std::vector<input_event>
584
598
{
585
599
// todo: figure out a better to handle dpad
586
600
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);
588
602
return ;
589
603
}
590
604
const int32_t idx_base = mapButtonId (code);
591
605
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);
593
607
return ;
594
608
}
595
609
info.buttonValues [idx_base] = 0.0 ;
@@ -599,6 +613,32 @@ void GamepadProvider::updateGamepad(GamepadInfo& info, std::vector<input_event>
599
613
buttonValuesChanged = true ;
600
614
};
601
615
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
+
602
642
const auto updateAxisValue = [&info, &axisValuesChanged] (int32_t code, int32_t value)
603
643
{
604
644
auto info_iter = info.axisInfo .find (code);
@@ -611,8 +651,10 @@ void GamepadProvider::updateGamepad(GamepadInfo& info, std::vector<input_event>
611
651
g_warning (" unmapped axis code: %d \n " , code);
612
652
return ;
613
653
}
654
+
614
655
const input_absinfo& axisInfo = info_iter->second ;
615
656
double mapped = mapAxisValue (value, axisInfo.minimum , axisInfo.maximum );
657
+
616
658
info.axisValues [axis_id] = mapped;
617
659
axisValuesChanged = true ;
618
660
};
@@ -628,6 +670,9 @@ void GamepadProvider::updateGamepad(GamepadInfo& info, std::vector<input_event>
628
670
}
629
671
case EV_ABS:
630
672
{
673
+ if (event.code == ABS_GAS || event.code == ABS_BRAKE) // L/R triggers
674
+ updateTriggerValue (event.code , event.value );
675
+ else
631
676
if (event.code == ABS_HAT0X || event.code == ABS_HAT0Y)
632
677
updateDpadButtonValue (event.code , event.value );
633
678
else
0 commit comments