-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathwpad.c
More file actions
2055 lines (1738 loc) · 51 KB
/
Copy pathwpad.c
File metadata and controls
2055 lines (1738 loc) · 51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------
wpad.c -- Wiimote Application Programmers Interface
Copyright (C) 2008-2025
Michael Wiedenbauer (shagkur)
Dave Murphy (WinterMute)
Hector Martin (marcan)
Zarithya
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
-------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "asm.h"
#include "processor.h"
#include "bte.h"
#include "conf.h"
#include "ir.h"
#include "speaker.h"
#include "dynamics.h"
#include "guitar_hero_3.h"
#include "wiiboard.h"
#include "wiiuse_internal.h"
#include "wiiuse/wpad.h"
#include "ogcsys.h"
#define MAX_STREAMDATA_LEN 20
#define EVENTQUEUE_LENGTH 16
struct _wpad_thresh {
s32 btns;
s32 ir;
s32 js;
s32 acc;
s32 wb;
s32 mp;
};
struct _wpad_cb {
wiimote *wm;
s32 data_fmt;
s32 queue_head;
s32 queue_tail;
s32 queue_full;
u32 queue_length;
u32 dropped_events;
s32 idle_time;
s32 speaker_enabled;
struct _wpad_thresh thresh;
void *sound_data;
u32 sound_len;
u32 sound_off;
syswd_t sound_alarm;
WPADData lstate;
WPADData *queue_ext;
WPADData queue_int[EVENTQUEUE_LENGTH];
};
static syswd_t __wpad_timer;
static vu32 __wpads_inited = 0;
static vs32 __wpads_bonded = 0;
static u32 __wpad_idletimeout = 300;
static vu32 __wpads_active = 0;
static vu32 __wpads_used = 0;
wiimote **__wpads = NULL;
static wiimote_listen __wpads_listen[WPAD_MAX_DEVICES] = {0};
static WPADData wpaddata[WPAD_MAX_DEVICES] = {0};
static struct _wpad_cb __wpdcb[WPAD_MAX_DEVICES] = {0};
static conf_pads __wpad_devs = {0};
static conf_pad_guests __wpad_guests = {0};
static struct linkkey_info __wpad_keys[CONF_PAD_MAX_REGISTERED] = {0};
static struct linkkey_info __wpad_guest_keys[CONF_PAD_ACTIVE_AND_OTHER] = {0};
static sem_t __wpad_confsave_sem;
static bool __wpad_confsave_thread_running = false;
static void* __wpad_confsave_thread_func(void *);
static lwp_t __wpad_confsave_thread = LWP_THREAD_NULL;
static s32 __wpad_onreset(s32 final);
static s32 __wpad_disconnect(struct _wpad_cb *wpdcb);
static void __wpad_eventCB(struct wiimote_t *wm,s32 event);
static s32 GetGuestSlot(struct bd_addr *pad_addr);
static s32 GetRegisteredSlot(struct bd_addr *pad_addr);
static void __wpad_def_powcb(s32 chan);
static void __wpad_def_hostsynccb(u32 held);
static WPADShutdownCallback __wpad_batcb = NULL;
static WPADShutdownCallback __wpad_powcb = __wpad_def_powcb;
static WPADDisconnectCallback __wpad_disconncb = NULL;
static WPADHostSyncBtnCallback __wpad_hostsynccb = __wpad_def_hostsynccb;
static WPADStatusCallback __wpad_statuscb = NULL;
extern void __wiiuse_sensorbar_enable(int enable);
static sys_resetinfo __wpad_resetinfo = {
{},
__wpad_onreset,
127
};
static s32 __wpad_onreset(s32 final)
{
WIIUSE_DEBUG("__wpad_onreset(%d)",final);
if(final==FALSE) {
WPAD_Shutdown();
}
return 1;
}
u32 __WPADClearActiveList(void)
{
u32 level,ret;
_CPU_ISR_Disable(level);
if(CONF_GetPadDevices(&__wpad_devs) < 0) {
ret = 1;
goto err;
}
__wpads_active = 0;
__wpads_used = 0;
memset(__wpad_devs.active,0,sizeof(__wpad_devs.active));
memset(&__wpad_guests,0,sizeof(__wpad_guests));
memset(__wpad_guest_keys,0,sizeof(__wpad_guest_keys));
if(CONF_SetPadDevices(&__wpad_devs) < 0) {
ret = 1;
goto err;
}
if(CONF_SetPadGuestDevices(&__wpad_guests) < 0) {
ret = 1;
goto err;
}
ret = CONF_SaveChanges();
err:
_CPU_ISR_Restore(level);
return ret;
}
static void __wpad_def_powcb(s32 chan)
{
SYS_DoPowerCB();
}
static void __wpad_timeouthandler(syswd_t alarm,void *cbarg)
{
s32 i;
struct wiimote_t *wm = NULL;
struct _wpad_cb *wpdcb = NULL;
if(!__wpads_active) return;
for(i=0;i<WPAD_MAX_DEVICES;i++) {
wpdcb = &__wpdcb[i];
wm = wpdcb->wm;
if(wm && WIIMOTE_IS_SET(wm,WIIMOTE_STATE_CONNECTED)) {
wpdcb->idle_time++;
if(wpdcb->idle_time>=__wpad_idletimeout) {
wpdcb->idle_time = 0;
wiiuse_disconnect(wm);
}
}
}
}
static void __wpad_sounddata_alarmhandler(syswd_t alarm,void *cbarg)
{
u8 *snd_data;
u32 snd_off;
struct wiimote_t *wm;
struct _wpad_cb *wpdcb = (struct _wpad_cb*)cbarg;
if(!wpdcb) return;
if(wpdcb->sound_off>=wpdcb->sound_len) {
wpdcb->sound_data = NULL;
wpdcb->sound_len = 0;
wpdcb->sound_off = 0;
SYS_CancelAlarm(wpdcb->sound_alarm);
return;
}
wm = wpdcb->wm;
snd_data = wpdcb->sound_data;
snd_off = wpdcb->sound_off;
wpdcb->sound_off += MAX_STREAMDATA_LEN;
wiiuse_write_streamdata(wm,(snd_data+snd_off),MAX_STREAMDATA_LEN,NULL);
}
static void __wpad_setfmt(s32 chan)
{
switch(__wpdcb[chan].data_fmt) {
case WPAD_FMT_BTNS:
wiiuse_set_flags(__wpads[chan], 0, WIIUSE_CONTINUOUS);
wiiuse_motion_sensing(__wpads[chan],0);
if(chan != WPAD_BALANCE_BOARD) wiiuse_set_ir(__wpads[chan],0);
break;
case WPAD_FMT_BTNS_ACC:
wiiuse_set_flags(__wpads[chan], WIIUSE_CONTINUOUS, 0);
wiiuse_motion_sensing(__wpads[chan],1);
if(chan != WPAD_BALANCE_BOARD) wiiuse_set_ir(__wpads[chan],0);
break;
case WPAD_FMT_BTNS_ACC_IR:
wiiuse_set_flags(__wpads[chan], WIIUSE_CONTINUOUS, 0);
wiiuse_motion_sensing(__wpads[chan],1);
if(chan != WPAD_BALANCE_BOARD) wiiuse_set_ir(__wpads[chan],1);
break;
default:
break;
}
}
wiimote *__wpad_assign_slot(wiimote_listen *wml, u8 err)
{
u32 i, level;
_CPU_ISR_Disable(level);
for(i=0; i<WPAD_MAX_DEVICES; i++) {
if(wml == &__wpads_listen[i]) {
break;
}
}
if (i >= WPAD_MAX_DEVICES)
{
WIIUSE_ERROR("WPAD Slot %d Not Valid\n", i);
_CPU_ISR_Restore(level);
return NULL;
}
if (err) {
WIIUSE_ERROR("WPAD Connection Error %d\n", (s8)err);
__wpads_used &= ~(0x01<<i);
_CPU_ISR_Restore(level);
return NULL;
}
WIIUSE_DEBUG("WPAD Assigning Slot %d (used: 0x%02x)", i, __wpads_used);
if(i>=WPAD_BALANCE_BOARD) {
BYTES_FROM_BD_ADDR(__wpad_devs.balance_board.bdaddr, &wml->bdaddr);
memcpy(__wpad_devs.balance_board.name, wml->name, sizeof(__wpad_devs.balance_board.name));
} else {
BYTES_FROM_BD_ADDR(__wpad_devs.active[i].bdaddr, &wml->bdaddr);
}
LWP_SemPost(__wpad_confsave_sem);
_CPU_ISR_Restore(level);
return __wpads[i];
}
static void* __wpad_confsave_thread_func(void *)
{
__wpad_confsave_thread_running = true;
while (__wpad_confsave_thread_running)
{
CONF_SetPadDevices(&__wpad_devs);
CONF_SaveChanges();
LWP_SemWait(__wpad_confsave_sem);
}
return NULL;
}
static void __wpad_confsave_thread_stop(void)
{
if (__wpad_confsave_sem != LWP_SEM_NULL) {
if (__wpad_confsave_thread != LWP_THREAD_NULL) {
__wpad_confsave_thread_running = false;
LWP_SemPost(__wpad_confsave_sem);
LWP_JoinThread(__wpad_confsave_thread, NULL);
__wpad_confsave_thread = LWP_THREAD_NULL;
}
LWP_SemDestroy(__wpad_confsave_sem);
__wpad_confsave_sem = LWP_SEM_NULL;
}
}
wiimote *__wpad_register_new(wiimote_listen *wml, u8 err)
{
u32 i,j,level;
wiimote *wm;
int guestEntry = CONF_PAD_MAX_ACTIVE;
int registeredEntry = CONF_PAD_MAX_REGISTERED;
_CPU_ISR_Disable(level);
wm = __wpad_assign_slot(wml, err);
if (wm)
{
guestEntry = GetGuestSlot(&wml->bdaddr);
registeredEntry = GetRegisteredSlot(&wml->bdaddr);
if(BTE_GetPairMode() == PAIR_MODE_NORMAL) {
// Permanent pair, need to save bdaddr as known controller
if(registeredEntry >= __wpad_devs.num_registered) {
// If currently guest, we want to allow for upgrade to permanent pair
if(guestEntry < CONF_PAD_MAX_ACTIVE) {
// Wipe guest entry and shift later ones up
for(i = guestEntry; i < (__wpad_guests.num_guests - 1); i++) {
memcpy(&__wpad_guests.guests[i], &__wpad_guests.guests[i+1], sizeof(conf_pad_guest_device));
}
memset(&__wpad_guests.guests[--__wpad_guests.num_guests], 0, sizeof(conf_pad_guest_device));
}
WIIUSE_DEBUG("Registering Wiimote '%s' with system...", wml->name);
memmove(&__wpad_devs.registered[1], &__wpad_devs.registered[0], sizeof(conf_pad_device)*(CONF_PAD_MAX_REGISTERED-1));
BYTES_FROM_BD_ADDR(__wpad_devs.registered[0].bdaddr, &wml->bdaddr);
memcpy(__wpad_devs.registered[0].name, wml->name, sizeof(__wpad_devs.registered[0].name));
if (__wpad_devs.num_registered < CONF_PAD_MAX_REGISTERED)
__wpad_devs.num_registered++;
LWP_SemPost(__wpad_confsave_sem);
}
} else if(registeredEntry >= CONF_PAD_MAX_REGISTERED) {
// Not permanent pair, need to save bdaddr and link key as guest controller
if(guestEntry >= CONF_PAD_MAX_ACTIVE) {
WIIUSE_DEBUG("Saving Wiimote '%s' as guest...", wml->name);
memmove(&__wpad_guests.guests[1], &__wpad_guests.guests[0], sizeof(conf_pad_guest_device)*(CONF_PAD_MAX_ACTIVE-1));
BYTES_FROM_BD_ADDR(__wpad_guests.guests[0].bdaddr, &wml->bdaddr);
memcpy(__wpad_guests.guests[0].name, wml->name, sizeof(__wpad_guests.guests[0].name));
if (__wpad_guests.num_guests < CONF_PAD_MAX_ACTIVE)
__wpad_guests.num_guests++;
for(i=0; i<CONF_PAD_MAX_ACTIVE; i++) {
if(bd_addr_cmp(&wml->bdaddr,&__wpad_guest_keys[i].bdaddr)) {
u8 *src = __wpad_guest_keys[i].key;
u8 *dst = __wpad_guests.guests[0].link_key;
// Keys are stored in config backwards, like bdaddrs
for (j=0;j<BD_LINK_KEY_LEN;j++) {
dst[j] = src[BD_LINK_KEY_LEN - 1 - j];
}
break;
}
}
}
}
}
_CPU_ISR_Restore(level);
return wm;
}
static s32 __wpad_setevtfilter_finished(s32 result,void *usrdata)
{
u32 level;
WIIUSE_DEBUG("__wpad_setevtfilter_finished(%d)",result);
_CPU_ISR_Disable(level);
if((result==ERR_OK) && (__wpads_inited==WPAD_STATE_ENABLING)) {
__wpads_inited = WPAD_STATE_ENABLED;
}
_CPU_ISR_Restore(level);
return ERR_OK;
}
static s32 __wpad_init_finished(s32 result,void *usrdata)
{
u32 level;
WIIUSE_DEBUG("__wpad_init_finished(%d)",result);
_CPU_ISR_Disable(level);
if(__wpads_inited==WPAD_STATE_ENABLING) {
BTE_SetEvtFilter(0x01,0x00,NULL,__wpad_setevtfilter_finished);
}
_CPU_ISR_Restore(level);
return ERR_OK;
}
static s32 __wpad_patch_finished(s32 result,void *usrdata)
{
u32 level;
WIIUSE_DEBUG("__wpad_patch_finished(%d)",result);
_CPU_ISR_Disable(level);
if(__wpads_inited==WPAD_STATE_ENABLING) {
BTE_InitSub(__wpad_init_finished);
}
_CPU_ISR_Restore(level);
return ERR_OK;
}
static s32 __readlinkkey_finished(s32 result,void *usrdata)
{
u32 i, j, level;
WIIUSE_DEBUG("__readlinkkey_finished(%d)",result);
_CPU_ISR_Disable(level);
if(__wpads_inited==WPAD_STATE_ENABLING) {
__wpads_bonded = result;
for(i=0;i<__wpad_guests.num_guests;i++) {
BD_ADDR_FROM_BYTES(&__wpad_guest_keys[i].bdaddr, __wpad_guests.guests[i].bdaddr);
u8 *src = __wpad_guests.guests[i].link_key;
u8 *dst = __wpad_guest_keys[i].key;
// Keys are stored in config backwards, like bdaddrs
for (j=0;j<BD_LINK_KEY_LEN;j++) {
dst[j] = src[BD_LINK_KEY_LEN - 1 - j];
}
}
BTE_ApplyPatch(__wpad_patch_finished);
}
_CPU_ISR_Restore(level);
return ERR_OK;
}
static s32 __initcore_finished(s32 result,void *usrdata)
{
u32 level;
WIIUSE_DEBUG("__initcore_finished(%d)",result);
_CPU_ISR_Disable(level);
if((result==ERR_OK) && (__wpads_inited==WPAD_STATE_ENABLING)) {
BTE_ReadStoredLinkKey(__wpad_keys,CONF_PAD_MAX_REGISTERED,__readlinkkey_finished);
}
_CPU_ISR_Restore(level);
return ERR_OK;
}
static s32 __wpad_disconnect(struct _wpad_cb *wpdcb)
{
struct wiimote_t *wm;
WIIUSE_DEBUG("__wpad_disconnect");
if(wpdcb==NULL) return 0;
wm = wpdcb->wm;
if(wm && WIIMOTE_IS_SET(wm,WIIMOTE_STATE_CONNECTED)) {
wiiuse_disconnect(wm);
}
return 0;
}
static void __wpad_calc_data(WPADData *data,WPADData *lstate,struct accel_t *accel_calib,u32 smoothed)
{
if(data->err!=WPAD_ERR_NONE) return;
data->orient = lstate->orient;
data->ir.state = lstate->ir.state;
data->ir.sensorbar = lstate->ir.sensorbar;
data->ir.x = lstate->ir.x;
data->ir.y = lstate->ir.y;
data->ir.sx = lstate->ir.sx;
data->ir.sy = lstate->ir.sy;
data->ir.ax = lstate->ir.ax;
data->ir.ay = lstate->ir.ay;
data->ir.distance = lstate->ir.distance;
data->ir.z = lstate->ir.z;
data->ir.angle = lstate->ir.angle;
data->ir.error_cnt = lstate->ir.error_cnt;
data->ir.glitch_cnt = lstate->ir.glitch_cnt;
data->btns_l = lstate->btns_h;
if(data->data_present & WPAD_DATA_ACCEL) {
calculate_orientation(accel_calib, &data->accel, &data->orient, smoothed);
calculate_gforce(accel_calib, &data->accel, &data->gforce);
}
if(data->data_present & WPAD_DATA_IR) {
interpret_ir_data(&data->ir,&data->orient);
}
if(data->data_present & WPAD_DATA_EXPANSION) {
switch(data->exp.type) {
case EXP_NUNCHUK:
{
struct nunchuk_t *nc = &data->exp.nunchuk;
nc->orient = lstate->exp.nunchuk.orient;
calc_joystick_state(&nc->js,nc->js.pos.x,nc->js.pos.y);
calculate_orientation(&nc->accel_calib,&nc->accel,&nc->orient,smoothed);
calculate_gforce(&nc->accel_calib,&nc->accel,&nc->gforce);
data->btns_h |= (data->exp.nunchuk.btns<<16);
}
break;
case EXP_CLASSIC:
{
struct classic_ctrl_t *cc = &data->exp.classic;
cc->r_shoulder = ((f32)cc->rs_raw/0x1F);
cc->l_shoulder = ((f32)cc->ls_raw/0x1F);
calc_joystick_state(&cc->ljs, cc->ljs.pos.x, cc->ljs.pos.y);
calc_joystick_state(&cc->rjs, cc->rjs.pos.x, cc->rjs.pos.y);
// overwrite Wiimote buttons (unused) with extra Wii U Pro Controller stick buttons
if (data->exp.classic.type == CLASSIC_TYPE_WIIU)
data->btns_h = (data->exp.classic.btns & WII_U_PRO_CTRL_BUTTON_EXTRA) >> 16;
data->btns_h |= ((data->exp.classic.btns & CLASSIC_CTRL_BUTTON_ALL)<<16);
}
break;
case EXP_GUITAR_HERO_3:
{
struct guitar_hero_3_t *gh3 = &data->exp.gh3;
gh3->touch_bar = 0;
if (gh3->tb_raw > 0x1B)
gh3->touch_bar = GUITAR_HERO_3_TOUCH_ORANGE;
else if (gh3->tb_raw > 0x18)
gh3->touch_bar = GUITAR_HERO_3_TOUCH_ORANGE | GUITAR_HERO_3_TOUCH_BLUE;
else if (gh3->tb_raw > 0x15)
gh3->touch_bar = GUITAR_HERO_3_TOUCH_BLUE;
else if (gh3->tb_raw > 0x13)
gh3->touch_bar = GUITAR_HERO_3_TOUCH_BLUE | GUITAR_HERO_3_TOUCH_YELLOW;
else if (gh3->tb_raw > 0x10)
gh3->touch_bar = GUITAR_HERO_3_TOUCH_YELLOW;
else if (gh3->tb_raw > 0x0D)
gh3->touch_bar = GUITAR_HERO_3_TOUCH_AVAILABLE;
else if (gh3->tb_raw > 0x0B)
gh3->touch_bar = GUITAR_HERO_3_TOUCH_YELLOW | GUITAR_HERO_3_TOUCH_RED;
else if (gh3->tb_raw > 0x08)
gh3->touch_bar = GUITAR_HERO_3_TOUCH_RED;
else if (gh3->tb_raw > 0x05)
gh3->touch_bar = GUITAR_HERO_3_TOUCH_RED | GUITAR_HERO_3_TOUCH_GREEN;
else if (gh3->tb_raw > 0x02)
gh3->touch_bar = GUITAR_HERO_3_TOUCH_GREEN;
gh3->whammy_bar = (gh3->wb_raw - GUITAR_HERO_3_WHAMMY_BAR_MIN) / (float)(GUITAR_HERO_3_WHAMMY_BAR_MAX - GUITAR_HERO_3_WHAMMY_BAR_MIN);
calc_joystick_state(&gh3->js, gh3->js.pos.x, gh3->js.pos.y);
data->btns_h |= (data->exp.gh3.btns<<16);
}
break;
case EXP_WII_BOARD:
{
struct wii_board_t *wb = &data->exp.wb;
calc_balanceboard_state(wb);
}
break;
default:
break;
}
}
data->btns_d = data->btns_h & ~data->btns_l;
data->btns_u = ~data->btns_h & data->btns_l;
*lstate = *data;
}
static void __save_state(struct wiimote_t* wm) {
/* wiimote */
wm->lstate.btns = wm->btns;
wm->lstate.accel = wm->accel;
/* ir */
wm->lstate.ir = wm->ir;
/* expansion */
switch (wm->exp.type) {
case EXP_NUNCHUK:
wm->lstate.exp.nunchuk = wm->exp.nunchuk;
break;
case EXP_CLASSIC:
wm->lstate.exp.classic = wm->exp.classic;
break;
case EXP_GUITAR_HERO_3:
wm->lstate.exp.gh3 = wm->exp.gh3;
break;
case EXP_WII_BOARD:
wm->lstate.exp.wb = wm->exp.wb;
break;
case EXP_MOTION_PLUS:
wm->lstate.exp.mp = wm->exp.mp;
break;
}
}
#define ABS(x) ((s32)(x)>0?(s32)(x):-((s32)(x)))
#define STATE_CHECK(thresh, a, b) \
if(((thresh) > WPAD_THRESH_IGNORE) && (ABS((a)-(b)) > (thresh))) \
state_changed = 1;
#define STATE_CHECK_SIMPLE(thresh, a, b) \
if(((thresh) > WPAD_THRESH_IGNORE) && ((a) != (b))) \
state_changed = 1;
static u32 __wpad_read_expansion(struct wiimote_t *wm,WPADData *data, struct _wpad_thresh *thresh)
{
int state_changed = 0;
switch(data->exp.type) {
case EXP_NUNCHUK:
data->exp.nunchuk = wm->exp.nunchuk;
STATE_CHECK_SIMPLE(thresh->btns, wm->exp.nunchuk.btns, wm->lstate.exp.nunchuk.btns);
STATE_CHECK(thresh->acc, wm->exp.nunchuk.accel.x, wm->lstate.exp.nunchuk.accel.x);
STATE_CHECK(thresh->acc, wm->exp.nunchuk.accel.y, wm->lstate.exp.nunchuk.accel.y);
STATE_CHECK(thresh->acc, wm->exp.nunchuk.accel.z, wm->lstate.exp.nunchuk.accel.z);
STATE_CHECK(thresh->js, wm->exp.nunchuk.js.pos.x, wm->lstate.exp.nunchuk.js.pos.x);
STATE_CHECK(thresh->js, wm->exp.nunchuk.js.pos.y, wm->lstate.exp.nunchuk.js.pos.y);
break;
case EXP_CLASSIC:
data->exp.classic = wm->exp.classic;
STATE_CHECK_SIMPLE(thresh->btns, wm->exp.classic.btns, wm->lstate.exp.classic.btns);
STATE_CHECK(thresh->js, wm->exp.classic.rs_raw, wm->lstate.exp.classic.rs_raw);
STATE_CHECK(thresh->js, wm->exp.classic.ls_raw, wm->lstate.exp.classic.ls_raw);
STATE_CHECK(thresh->js, wm->exp.classic.ljs.pos.x, wm->lstate.exp.classic.ljs.pos.x);
STATE_CHECK(thresh->js, wm->exp.classic.ljs.pos.y, wm->lstate.exp.classic.ljs.pos.y);
STATE_CHECK(thresh->js, wm->exp.classic.rjs.pos.x, wm->lstate.exp.classic.rjs.pos.x);
STATE_CHECK(thresh->js, wm->exp.classic.rjs.pos.y, wm->lstate.exp.classic.rjs.pos.y);
break;
case EXP_GUITAR_HERO_3:
data->exp.gh3 = wm->exp.gh3;
STATE_CHECK_SIMPLE(thresh->btns, wm->exp.gh3.btns, wm->lstate.exp.gh3.btns);
STATE_CHECK(thresh->js, wm->exp.gh3.wb_raw, wm->lstate.exp.gh3.wb_raw);
STATE_CHECK(thresh->js, wm->exp.gh3.js.pos.x, wm->lstate.exp.gh3.js.pos.x);
STATE_CHECK(thresh->js, wm->exp.gh3.js.pos.y, wm->lstate.exp.gh3.js.pos.y);
break;
case EXP_WII_BOARD:
data->exp.wb = wm->exp.wb;
STATE_CHECK(thresh->wb,wm->exp.wb.rtl,wm->lstate.exp.wb.rtl);
STATE_CHECK(thresh->wb,wm->exp.wb.rtr,wm->lstate.exp.wb.rtr);
STATE_CHECK(thresh->wb,wm->exp.wb.rbl,wm->lstate.exp.wb.rbl);
STATE_CHECK(thresh->wb,wm->exp.wb.rbr,wm->lstate.exp.wb.rbr);
break;
case EXP_MOTION_PLUS:
data->exp.mp = wm->exp.mp;
STATE_CHECK(thresh->mp,wm->exp.mp.rx,wm->lstate.exp.mp.rx);
STATE_CHECK(thresh->mp,wm->exp.mp.ry,wm->lstate.exp.mp.ry);
STATE_CHECK(thresh->mp,wm->exp.mp.rz,wm->lstate.exp.mp.rz);
break;
}
return state_changed;
}
static void __wpad_read_wiimote(struct wiimote_t *wm, WPADData *data, s32 *idle_time, struct _wpad_thresh *thresh)
{
int i;
int state_changed = 0;
data->err = WPAD_ERR_TRANSFER;
data->data_present = 0;
data->battery_level = wm->battery_level;
data->exp.type = wm->exp.type;
if(wm && WIIMOTE_IS_SET(wm,WIIMOTE_STATE_CONNECTED)) {
if(WIIMOTE_IS_SET(wm,WIIMOTE_STATE_HANDSHAKE_COMPLETE)) {
switch(wm->event_buf[0]) {
case WM_RPT_BTN:
case WM_RPT_BTN_ACC:
case WM_RPT_BTN_ACC_IR:
case WM_RPT_BTN_EXP:
case WM_RPT_BTN_ACC_EXP:
case WM_RPT_BTN_IR_EXP:
case WM_RPT_BTN_ACC_IR_EXP:
data->btns_h = (wm->btns&0xffff);
data->data_present |= WPAD_DATA_BUTTONS;
STATE_CHECK_SIMPLE(thresh->btns, wm->btns, wm->lstate.btns);
}
switch(wm->event_buf[0]) {
case WM_RPT_BTN_ACC:
case WM_RPT_BTN_ACC_IR:
case WM_RPT_BTN_ACC_EXP:
case WM_RPT_BTN_ACC_IR_EXP:
data->accel = wm->accel;
data->data_present |= WPAD_DATA_ACCEL;
STATE_CHECK(thresh->acc, wm->accel.x, wm->lstate.accel.x);
STATE_CHECK(thresh->acc, wm->accel.y, wm->lstate.accel.y);
STATE_CHECK(thresh->acc, wm->accel.z, wm->lstate.accel.z);
}
switch(wm->event_buf[0]) {
//IR requires acceleration
//case WM_RPT_BTN_IR_EXP:
case WM_RPT_BTN_ACC_IR:
case WM_RPT_BTN_ACC_IR_EXP:
data->ir = wm->ir;
data->data_present |= WPAD_DATA_IR;
for(i=0; i<WPAD_MAX_IR_DOTS; i++) {
STATE_CHECK_SIMPLE(thresh->ir, wm->ir.dot[i].visible, wm->lstate.ir.dot[i].visible);
STATE_CHECK(thresh->ir, wm->ir.dot[i].rx, wm->lstate.ir.dot[i].rx);
STATE_CHECK(thresh->ir, wm->ir.dot[i].ry, wm->lstate.ir.dot[i].ry);
}
}
switch(wm->event_buf[0]) {
case WM_RPT_BTN_EXP:
case WM_RPT_BTN_ACC_EXP:
case WM_RPT_BTN_IR_EXP:
case WM_RPT_BTN_ACC_IR_EXP:
state_changed |= __wpad_read_expansion(wm,data,thresh);
data->data_present |= WPAD_DATA_EXPANSION;
}
data->err = WPAD_ERR_NONE;
if(state_changed) {
*idle_time = 0;
__save_state(wm);
}
} else
data->err = WPAD_ERR_NOT_READY;
} else
data->err = WPAD_ERR_NO_CONTROLLER;
}
static void __wpad_eventCB(struct wiimote_t *wm,s32 event)
{
s32 chan;
u32 maxbufs;
WPADData *wpadd = NULL;
struct _wpad_cb *wpdcb = NULL;
switch(event) {
case WIIUSE_EVENT:
chan = wm->unid;
wpdcb = &__wpdcb[chan];
if(wpdcb->queue_ext!=NULL) {
maxbufs = wpdcb->queue_length;
wpadd = &(wpdcb->queue_ext[wpdcb->queue_tail]);
} else {
maxbufs = EVENTQUEUE_LENGTH;
wpadd = &(wpdcb->queue_int[wpdcb->queue_tail]);
}
if(wpdcb->queue_full == maxbufs) {
wpdcb->queue_head++;
wpdcb->queue_head %= maxbufs;
wpdcb->dropped_events++;
} else {
wpdcb->queue_full++;
}
__wpad_read_wiimote(wm, wpadd, &wpdcb->idle_time, &wpdcb->thresh);
wpdcb->queue_tail++;
wpdcb->queue_tail %= maxbufs;
break;
case WIIUSE_STATUS:
break;
case WIIUSE_CONNECT:
chan = wm->unid;
wpdcb = &__wpdcb[chan];
wpdcb->wm = wm;
wpdcb->queue_head = 0;
wpdcb->queue_tail = 0;
wpdcb->queue_full = 0;
wpdcb->idle_time = 0;
memset(&wpdcb->lstate,0,sizeof(WPADData));
memset(&wpaddata[chan],0,sizeof(WPADData));
memset(wpdcb->queue_int,0,(sizeof(WPADData)*EVENTQUEUE_LENGTH));
wiiuse_set_ir_position(wm,(CONF_GetSensorBarPosition()^1));
wiiuse_set_ir_sensitivity(wm,CONF_GetIRSensitivity());
wiiuse_set_leds(wm,(WIIMOTE_LED_1<<(chan%WPAD_BALANCE_BOARD)),NULL);
wiiuse_set_speaker(wm,wpdcb->speaker_enabled);
__wpad_setfmt(chan);
__wpads_active |= (0x01<<chan);
break;
case WIIUSE_DISCONNECT:
chan = wm->unid;
wpdcb = &__wpdcb[chan];
wpdcb->wm = wm;
wpdcb->queue_head = 0;
wpdcb->queue_tail = 0;
wpdcb->queue_full = 0;
wpdcb->queue_length = 0;
wpdcb->queue_ext = NULL;
wpdcb->idle_time = -1;
memset(&wpdcb->lstate,0,sizeof(WPADData));
memset(&wpaddata[chan],0,sizeof(WPADData));
memset(wpdcb->queue_int,0,(sizeof(WPADData)*EVENTQUEUE_LENGTH));
__wpads_active &= ~(0x01<<chan);
break;
case WIIUSE_ACK:
break;
default:
WIIUSE_DEBUG("__wpad_eventCB(%02x)\n", event);
break;
}
}
void __wpad_disconnectCB(struct bd_addr *pad_addr, u8 reason)
{
int i;
u32 level;
WIIUSE_DEBUG("__wpad_disconnectCB(%d)", reason);
_CPU_ISR_Disable(level);
if(__wpads_inited == WPAD_STATE_ENABLED) {
for(i=0;i<WPAD_MAX_DEVICES;i++) {
if(bd_addr_cmp(pad_addr,&__wpads_listen[i].bdaddr)) {
if (__wpad_disconncb) {
__wpad_disconncb(i, reason);
} else {
// For backwards compatibility
switch (reason) {
case WPAD_DISCON_BATTERY_DIED:
if(__wpad_batcb) __wpad_batcb(i);
break;
case WPAD_DISCON_POWER_OFF:
if(__wpad_powcb) __wpad_powcb(i);
break;
}
}
__wpads_used &= ~(0x01<<i);
break;
}
}
}
_CPU_ISR_Restore(level);
}
static void __wpad_hostsyncbuttonCB(u32 held)
{
u32 level;
_CPU_ISR_Disable(level);
if((__wpads_inited == WPAD_STATE_ENABLED) && __wpad_hostsynccb) {
__wpad_hostsynccb(held);
}
_CPU_ISR_Restore(level);
}
static s32 GetActiveSlot(struct bd_addr *pad_addr)
{
int i;
int slot = CONF_PAD_MAX_ACTIVE;
struct bd_addr bdaddr;
for(i=0; i<CONF_PAD_MAX_ACTIVE; i++) {
BD_ADDR_FROM_BYTES(&bdaddr,__wpad_devs.active[i].bdaddr);
if(bd_addr_cmp(pad_addr,&bdaddr)) {
slot = i;
break;
}
}
return slot;
}
s32 __wpad_read_remote_name_finished(s32 result,void *userdata)
{
u32 level;
int i;
struct bd_addr bdaddr;
int slot = WPAD_MAX_DEVICES;
struct pad_name_info *info = (struct pad_name_info *)userdata;
_CPU_ISR_Disable(level);
if(result == ERR_OK && info != NULL) {
if (!strncmp("Nintendo RVL-", (char *)info->name, 13)) {
// Found Wii accessory, is it controller or something else?
if (!strncmp("Nintendo RVL-CNT-", (char *)info->name, 17)) {
slot = GetActiveSlot(&info->bdaddr);
if (slot < CONF_PAD_MAX_ACTIVE) {
WIIUSE_DEBUG("Wiimote already active in slot %d", slot);
} else {
// Not active, try to make active
slot = WPAD_MAX_DEVICES;
for(i=0; i<CONF_PAD_MAX_ACTIVE; i++) {
if(!(__wpads_used & (1<<i))) {
WIIUSE_DEBUG("Attempting to connect to Wiimote using slot %d", i);
slot = i;
break;
}
}
}
} else {
// Check for balance board
BD_ADDR_FROM_BYTES(&(bdaddr),__wpad_devs.balance_board.bdaddr);
if (bd_addr_cmp(&info->bdaddr,&bdaddr)) {
WIIUSE_DEBUG("Balance Board already registered");
slot = WPAD_BALANCE_BOARD;
}
// Not active, try to make active
if(slot != WPAD_BALANCE_BOARD) {
if(!(__wpads_used & (1<<WPAD_BALANCE_BOARD))) {
WIIUSE_DEBUG("Attempting to connect to Balance Board");
slot = WPAD_BALANCE_BOARD;
}
}
}
if(slot < WPAD_MAX_DEVICES) {
result = wiiuse_connect(&__wpads_listen[slot],&info->bdaddr,info->name,__wpad_register_new);
__wpads_used |= (0x01<<slot);
result = ERR_OK;
} else {
WIIUSE_WARNING("WPAD All Slots Used\n");
result = ERR_CONN;
}
} else {
result = ERR_ARG;
}
}
if (info) free(info);
_CPU_ISR_Restore(level);
return result;
}
static s32 GetRegisteredSlot(struct bd_addr *pad_addr)
{
int i;
int slot = CONF_PAD_MAX_REGISTERED;
struct bd_addr bdaddr;
for(i=0; i<__wpad_devs.num_registered; i++) {
BD_ADDR_FROM_BYTES(&bdaddr,__wpad_devs.registered[i].bdaddr);
if(bd_addr_cmp(pad_addr,&bdaddr)) {
slot = i;
break;
}
}
return slot;
}
static s32 GetGuestSlot(struct bd_addr *pad_addr)
{
int i;
int slot = CONF_PAD_MAX_ACTIVE;
struct bd_addr bdaddr;
for(i=0; i<__wpad_guests.num_guests; i++) {
BD_ADDR_FROM_BYTES(&bdaddr,__wpad_guests.guests[i].bdaddr);
if(bd_addr_cmp(pad_addr,&bdaddr)) {
slot = i;
break;
}
}
return slot;
}
static s8 __wpad_connreqCB(void *arg,struct bd_addr *pad_addr,u8 *cod,u8 link_type)
{
int i, j, level;
int slot = WPAD_MAX_DEVICES;
int confslot = CONF_PAD_MAX_ACTIVE;
struct bd_addr bdaddr;
const u8 *name = NULL;
WIIUSE_DEBUG("__wpad_connreqCB");
_CPU_ISR_Disable(level);
// Only accept connection requests (i.e. "press any button") if not doing guest pairing
if(BTE_GetPairMode() == PAIR_MODE_NORMAL) {
if(!bd_addr_cmp(pad_addr,BD_ADDR_ANY)) {
confslot = GetActiveSlot(pad_addr);
if (confslot < CONF_PAD_MAX_ACTIVE) {
BD_ADDR_FROM_BYTES(&bdaddr,__wpad_devs.active[confslot].bdaddr);
name = (const u8 *)__wpad_devs.active[confslot].name;
WIIUSE_DEBUG("Active pad '%s' found in slot %d", name, confslot);
if (!(__wpads_used & (1<<confslot)) || bd_addr_cmp(pad_addr,&bdaddr))
slot = confslot;
else
WIIUSE_DEBUG("Slot %d taken! Finding new slot", confslot);
}
if(slot >= WPAD_MAX_DEVICES) {
for(i=0; i<CONF_PAD_MAX_REGISTERED; i++) {
BD_ADDR_FROM_BYTES(&bdaddr,__wpad_devs.registered[i].bdaddr);
if(bd_addr_cmp(pad_addr,&bdaddr)) {
name = (const u8 *)__wpad_devs.registered[i].name;
WIIUSE_DEBUG("Registered pad '%s' found in slot %d", name, i);
// Not active, try to make active
if(!strncmp("Nintendo RVL-CNT-", __wpad_devs.registered[i].name, 17)) {
for(j=0; j<WPAD_BALANCE_BOARD; j++) {