@@ -29,7 +29,7 @@ static HidVibrationValue vibration_values[DEFAULT_MAX_PADS][2];
29
29
static HidVibrationValue vibration_stop ;
30
30
static int previous_handheld = -1 ;
31
31
/* 1 = handheld, 0 = docked, -1 = first use */
32
- static uint previous_split_joycon_setting [ MAX_USERS ] = { 0 };
32
+ static uint previous_split_joycon_setting = { -1 };
33
33
#endif
34
34
35
35
static const char * switch_joypad_name (unsigned pad )
@@ -166,8 +166,7 @@ static void switch_joypad_destroy(void)
166
166
167
167
previous_handheld = -1 ;
168
168
169
- for (i = 0 ; i < MAX_USERS ; i ++ )
170
- previous_split_joycon_setting [i ] = 0 ;
169
+ previous_split_joycon_setting = 0 ;
171
170
172
171
for (i = 0 ; i < DEFAULT_MAX_PADS ; i ++ )
173
172
{
@@ -185,113 +184,96 @@ static void switch_joypad_destroy(void)
185
184
186
185
#ifdef HAVE_LIBNX
187
186
188
- static void switch_joypad_poll (void )
187
+ static void switch_split_joycons (void )
189
188
{
190
- int i , handheld ;
191
- settings_t * settings = config_get_ptr ();
192
-
193
- hidScanInput ();
189
+ unsigned i ;
190
+ for (i = 0 ; i < MAX_USERS ; i += 2 )
191
+ {
192
+ hidSetNpadJoyAssignmentModeSingleByDefault (i );
193
+ hidSetNpadJoyAssignmentModeSingleByDefault (i + 1 );
194
+ hidSetNpadJoyHoldType (HidJoyHoldType_Horizontal );
195
+ }
196
+ }
194
197
195
- handheld = hidGetHandheldMode ();
198
+ static void switch_join_joycons (void )
199
+ {
200
+ /* find all left/right single JoyCon pairs and join them together */
201
+ int id , id_0 , id_1 ;
202
+ int last_right_id = MAX_USERS ;
203
+ for (id = 0 ; id < MAX_USERS ; id ++ )
204
+ hidSetNpadJoyAssignmentModeDual (id );
196
205
197
- if ( previous_handheld == -1 )
206
+ for ( id_0 = 0 ; id_0 < MAX_USERS ; id_0 ++ )
198
207
{
199
- /* First call of this function, apply joycon settings
200
- * according to preferences, init variables */
201
- if (!handheld )
208
+ if (hidGetControllerType (id_0 ) & TYPE_JOYCON_LEFT )
202
209
{
203
- for (i = 0 ; i < MAX_USERS ; i += 2 )
210
+ for (id_1 = last_right_id - 1 ; id_1 >= 0 ; id_1 -- )
204
211
{
205
- if (settings -> uints .input_split_joycon [i ])
206
- {
207
- hidSetNpadJoyAssignmentModeSingleByDefault (i );
208
- hidSetNpadJoyAssignmentModeSingleByDefault (i + 1 );
209
- hidSetNpadJoyHoldType (HidJoyHoldType_Horizontal );
210
- }
211
- else if (!settings -> uints .input_split_joycon [i ])
212
+ if (hidGetControllerType (id_1 ) & TYPE_JOYCON_RIGHT )
212
213
{
213
- hidSetNpadJoyAssignmentModeDual (i );
214
- hidSetNpadJoyAssignmentModeDual (i + 1 );
215
- hidMergeSingleJoyAsDualJoy (i , i + 1 );
214
+ /* prevent missing player numbers */
215
+ last_right_id = id_1 ;
216
+ if (id_0 < id_1 )
217
+ hidMergeSingleJoyAsDualJoy (id_0 , id_1 );
218
+ else if (id_0 > id_1 )
219
+ hidMergeSingleJoyAsDualJoy (id_1 , id_0 );
220
+ break ;
216
221
}
217
222
}
218
223
}
219
- previous_handheld = handheld ;
220
- for (i = 0 ; i < MAX_USERS ; i += 2 )
221
- previous_split_joycon_setting [i ] = settings -> uints .input_split_joycon [i ];
222
224
}
225
+ }
226
+
227
+ static void switch_joypad_poll (void )
228
+ {
229
+ int i , handheld ;
230
+ settings_t * settings = config_get_ptr ();
231
+
232
+ hidScanInput ();
223
233
224
- if (!handheld && previous_handheld )
234
+ /* handheld means the Switch is neither docked nor in tabletop mode */
235
+ /* e.g. it is used held in hands with both joycons attached */
236
+ handheld = hidGetHandheldMode ();
237
+
238
+ if (previous_handheld == -1 )
225
239
{
226
- /* switching out of handheld, so make sure
227
- * joycons are correctly split. */
228
- for (i = 0 ; i < MAX_USERS ; i += 2 )
229
- {
230
- /* CONTROLLER_PLAYER_X, X == i++ */
231
- if (settings -> uints .input_split_joycon [i ])
232
- {
233
- hidSetNpadJoyAssignmentModeSingleByDefault (i );
234
- hidSetNpadJoyAssignmentModeSingleByDefault (i + 1 );
235
- hidSetNpadJoyHoldType (HidJoyHoldType_Horizontal );
236
- }
237
- }
240
+ /* first call of this function, apply joycon settings
241
+ * according to preferences */
242
+ if (!handheld && settings -> uints .input_split_joycon )
243
+ switch_split_joycons ();
244
+ else
245
+ switch_join_joycons ();
238
246
}
239
- else if ( handheld && ! previous_handheld )
247
+ else
240
248
{
241
- /* switching into handheld, so make sure all split joycons are joined */
242
- for (i = 0 ; i < MAX_USERS ; i += 2 )
249
+ if (!handheld && previous_handheld )
243
250
{
244
- /* find all left/right single JoyCon pairs and join them together */
245
- int id , id_0 , id_1 ;
246
- int last_right_id = MAX_USERS ;
247
- for (id = 0 ; id < MAX_USERS ; id ++ )
248
- hidSetNpadJoyAssignmentModeDual (id );
249
-
250
- for (id_0 = 0 ; id_0 < MAX_USERS ; id_0 ++ )
251
- {
252
- if (hidGetControllerType (id_0 ) & TYPE_JOYCON_LEFT )
253
- {
254
- for (id_1 = last_right_id - 1 ; id_1 >= 0 ; id_1 -- )
255
- {
256
- if (hidGetControllerType (id_1 ) & TYPE_JOYCON_RIGHT )
257
- {
258
- /* prevent missing player numbers */
259
- last_right_id = id_1 ;
260
- if (id_0 < id_1 )
261
- hidMergeSingleJoyAsDualJoy (id_0 , id_1 );
262
- else if (id_0 > id_1 )
263
- hidMergeSingleJoyAsDualJoy (id_1 , id_0 );
264
- break ;
265
- }
266
- }
267
- }
268
- }
251
+ /* switching out of handheld, so make sure
252
+ * joycons are correctly split. */
253
+ if (settings -> uints .input_split_joycon )
254
+ switch_split_joycons ();
269
255
}
270
- }
271
- else if (!handheld )
272
- {
273
- /* split or join joycons every time the user changes a setting */
274
- for (i = 0 ; i < MAX_USERS ; i += 2 )
256
+ else if (handheld && !previous_handheld )
275
257
{
276
- if ( settings -> uints .input_split_joycon [i ]
277
- && !previous_split_joycon_setting [i ])
258
+ /* switching into handheld, so make sure all split joycons are joined */
259
+ switch_join_joycons ();
260
+ }
261
+ else if (!handheld )
262
+ {
263
+ /* the user might have changed the split joycon setting, so respond */
264
+ if (settings -> uints .input_split_joycon && !previous_split_joycon_setting )
278
265
{
279
- hidSetNpadJoyAssignmentModeSingleByDefault (i );
280
- hidSetNpadJoyAssignmentModeSingleByDefault (i + 1 );
281
- hidSetNpadJoyHoldType (HidJoyHoldType_Horizontal );
282
- }
283
- else if (!settings -> uints .input_split_joycon [i ]
284
- && previous_split_joycon_setting [i ])
266
+ /* setting changed from unsplit to split, so split them all */
267
+ switch_split_joycons ();
268
+ }
269
+ else if (!settings -> uints .input_split_joycon && previous_split_joycon_setting )
285
270
{
286
- hidSetNpadJoyAssignmentModeDual (i );
287
- hidSetNpadJoyAssignmentModeDual (i + 1 );
288
- hidMergeSingleJoyAsDualJoy (i , i + 1 );
271
+ /* setting changed from split to unsplit, so join them all */
272
+ switch_join_joycons ();
289
273
}
290
274
}
291
275
}
292
-
293
- for (i = 0 ; i < MAX_USERS ; i += 2 )
294
- previous_split_joycon_setting [i ] = settings -> uints .input_split_joycon [i ];
276
+ previous_split_joycon_setting = settings -> uints .input_split_joycon ;
295
277
previous_handheld = handheld ;
296
278
297
279
for (i = 0 ; i < DEFAULT_MAX_PADS ; i ++ )
0 commit comments