Bug Summary

File:Source/Core/Core/Src/HW/WiimoteEmu/WiimoteEmu.cpp
Location:line 556, column 32
Description:The left operand of '<' is a garbage value

Annotated Source Code

1// Copyright 2013 Dolphin Emulator Project
2// Licensed under GPLv2
3// Refer to the license.txt file included.
4
5#include <cmath>
6
7#include "Attachment/Classic.h"
8#include "Attachment/Nunchuk.h"
9#include "Attachment/Guitar.h"
10#include "Attachment/Drums.h"
11#include "Attachment/Turntable.h"
12
13#include "WiimoteEmu.h"
14#include "WiimoteHid.h"
15
16#include "../WiimoteReal/WiimoteReal.h"
17
18#include "Timer.h"
19#include "Common.h"
20#include "../../Host.h"
21#include "../../ConfigManager.h"
22
23#include "UDPTLayer.h"
24
25inline double round(double x) { return (x-floor(x))>0.5 ? ceil(x) : floor(x); } //because damn MSVSC doesen't comply to C99
26
27#include "MatrixMath.h"
28
29#include "../../Movie.h"
30#include "NetPlayClient.h"
31
32namespace
33{
34// :)
35auto const TAU = 6.28318530717958647692;
36auto const PI = TAU / 2.0;
37}
38
39namespace WiimoteEmu
40{
41
42/* An example of a factory default first bytes of the Eeprom memory. There are differences between
43 different Wiimotes, my Wiimote had different neutral values for the accelerometer. */
44static const u8 eeprom_data_0[] = {
45 // IR, maybe more
46 // assuming last 2 bytes are checksum
47 0xA1, 0xAA, 0x8B, 0x99, 0xAE, 0x9E, 0x78, 0x30, 0xA7, /*0x74, 0xD3,*/ 0x00, 0x00, // messing up the checksum on purpose
48 0xA1, 0xAA, 0x8B, 0x99, 0xAE, 0x9E, 0x78, 0x30, 0xA7, /*0x74, 0xD3,*/ 0x00, 0x00,
49 // Accelerometer
50 // 0g x,y,z, 1g x,y,z, idk, last byte is a checksum
51 0x80, 0x80, 0x80, 0x00, 0x9A, 0x9A, 0x9A, 0x00, 0x40, 0xE3,
52 0x80, 0x80, 0x80, 0x00, 0x9A, 0x9A, 0x9A, 0x00, 0x40, 0xE3,
53};
54
55static const u8 motion_plus_id[] = { 0x00, 0x00, 0xA6, 0x20, 0x00, 0x05 };
56
57static const u8 eeprom_data_16D0[] = {
58 0x00, 0x00, 0x00, 0xFF, 0x11, 0xEE, 0x00, 0x00,
59 0x33, 0xCC, 0x44, 0xBB, 0x00, 0x00, 0x66, 0x99,
60 0x77, 0x88, 0x00, 0x00, 0x2B, 0x01, 0xE8, 0x13
61};
62
63const ReportFeatures reporting_mode_features[] =
64{
65 //0x30: Core Buttons
66 { 2, 0, 0, 0, 4 },
67 //0x31: Core Buttons and Accelerometer
68 { 2, 4, 0, 0, 7 },
69 //0x32: Core Buttons with 8 Extension bytes
70 { 2, 0, 0, 4, 12 },
71 //0x33: Core Buttons and Accelerometer with 12 IR bytes
72 { 2, 4, 7, 0, 19 },
73 //0x34: Core Buttons with 19 Extension bytes
74 { 2, 0, 0, 4, 23 },
75 //0x35: Core Buttons and Accelerometer with 16 Extension Bytes
76 { 2, 4, 0, 7, 23 },
77 //0x36: Core Buttons with 10 IR bytes and 9 Extension Bytes
78 { 2, 0, 4, 14, 23 },
79 //0x37: Core Buttons and Accelerometer with 10 IR bytes and 6 Extension Bytes
80 { 2, 4, 7, 17, 23 },
81
82 // UNSUPPORTED:
83 //0x3d: 21 Extension Bytes
84 { 0, 0, 0, 2, 23 },
85 //0x3e / 0x3f: Interleaved Core Buttons and Accelerometer with 36 IR bytes
86 { 0, 0, 0, 0, 23 },
87};
88
89void EmulateShake(AccelData* const accel
90 , accel_cal* const calib
91 , ControllerEmu::Buttons* const buttons_group
92 , u8* const shake_step )
93{
94 // frame count of one up/down shake
95 // < 9 no shake detection in "Wario Land: Shake It"
96 auto const shake_step_max = 15;
97
98 // peak G-force
99 double shake_intensity;
100
101 // shake is a bitfield of X,Y,Z shake button states
102 static const unsigned int btns[] = { 0x01, 0x02, 0x04 };
103 unsigned int shake = 0;
104 buttons_group->GetState( &shake, btns );
105
106 for (int i = 0; i != 3; ++i)
107 {
108 if (shake & (1 << i))
109 {
110 double zero = double((&(calib->zero_g.x))[i]);
111 double one = double((&(calib->one_g.x))[i]);
112 shake_intensity = max(zero / (one - zero), (255.f - zero) / (one - zero));
113 (&(accel->x))[i] = std::sin(TAU * shake_step[i] / shake_step_max) * shake_intensity;
114 shake_step[i] = (shake_step[i] + 1) % shake_step_max;
115 }
116 else
117 shake_step[i] = 0;
118 }
119}
120
121void EmulateTilt(AccelData* const accel
122 , ControllerEmu::Tilt* const tilt_group
123 , const bool focus, const bool sideways, const bool upright)
124{
125 float roll, pitch;
126 // 180 degrees
127 tilt_group->GetState(&roll, &pitch, 0, focus ? PI : 0);
128
129 unsigned int ud = 0, lr = 0, fb = 0;
130
131 // some notes that no one will understand but me :p
132 // left, forward, up
133 // lr/ left == negative for all orientations
134 // ud/ up == negative for upright longways
135 // fb/ forward == positive for (sideways flat)
136
137 // determine which axis is which direction
138 ud = upright ? (sideways ? 0 : 1) : 2;
139 lr = sideways;
140 fb = upright ? 2 : (sideways ? 0 : 1);
141
142 int sgn[3]={-1,1,1}; //sign fix
143
144 if (sideways && !upright)
145 sgn[fb] *= -1;
146 if (!sideways && upright)
147 sgn[ud] *= -1;
148
149 (&accel->x)[ud] = (sin((PI / 2) - std::max(fabsf(roll), fabsf(pitch))))*sgn[ud];
150 (&accel->x)[lr] = -sin(roll)*sgn[lr];
151 (&accel->x)[fb] = sin(pitch)*sgn[fb];
152}
153
154#define SWING_INTENSITY2.5f 2.5f//-uncalibrated(aprox) 0x40-calibrated
155
156void EmulateSwing(AccelData* const accel
157 , ControllerEmu::Force* const swing_group
158 , const bool sideways, const bool upright)
159{
160 float swing[3];
161 swing_group->GetState(swing, 0, SWING_INTENSITY2.5f);
162
163 s8 g_dir[3] = {-1, -1, -1};
164 u8 axis_map[3];
165
166 // determine which axis is which direction
167 axis_map[0] = upright ? (sideways ? 0 : 1) : 2; // up/down
168 axis_map[1] = sideways; // left|right
169 axis_map[2] = upright ? 2 : (sideways ? 0 : 1); // forward/backward
170
171 // some orientations have up as positive, some as negative
172 // same with forward
173 if (sideways && !upright)
174 g_dir[axis_map[2]] *= -1;
175 if (!sideways && upright)
176 g_dir[axis_map[0]] *= -1;
177
178 for (unsigned int i=0; i<3; ++i)
179 (&accel->x)[axis_map[i]] += swing[i] * g_dir[i];
180}
181
182const u16 button_bitmasks[] =
183{
184 Wiimote::BUTTON_A,
185 Wiimote::BUTTON_B,
186 Wiimote::BUTTON_ONE,
187 Wiimote::BUTTON_TWO,
188 Wiimote::BUTTON_MINUS,
189 Wiimote::BUTTON_PLUS,
190 Wiimote::BUTTON_HOME
191};
192
193const u16 dpad_bitmasks[] =
194{
195 Wiimote::PAD_UP, Wiimote::PAD_DOWN, Wiimote::PAD_LEFT, Wiimote::PAD_RIGHT
196};
197const u16 dpad_sideways_bitmasks[] =
198{
199 Wiimote::PAD_RIGHT, Wiimote::PAD_LEFT, Wiimote::PAD_UP, Wiimote::PAD_DOWN
200};
201
202const char* const named_buttons[] =
203{
204 "A", "B", "1", "2", "-", "+", "Home",
205};
206
207void Wiimote::Reset()
208{
209 m_reporting_mode = WM_REPORT_CORE0x30;
210 // i think these two are good
211 m_reporting_channel = 0;
212 m_reporting_auto = false;
213
214 m_rumble_on = false;
215 m_speaker_mute = false;
216 m_motion_plus_present = false;
217 m_motion_plus_active = false;
218
219 // will make the first Update() call send a status request
220 // the first call to RequestStatus() will then set up the status struct extension bit
221 m_extension->active_extension = -1;
222
223 // eeprom
224 memset(m_eeprom, 0, sizeof(m_eeprom));
225 // calibration data
226 memcpy(m_eeprom, eeprom_data_0, sizeof(eeprom_data_0));
227 // dunno what this is for, copied from old plugin
228 memcpy(m_eeprom + 0x16D0, eeprom_data_16D0, sizeof(eeprom_data_16D0));
229
230 // set up the register
231 memset(&m_reg_speaker, 0, sizeof(m_reg_speaker));
232 memset(&m_reg_ir, 0, sizeof(m_reg_ir));
233 memset(&m_reg_ext, 0, sizeof(m_reg_ext));
234 memset(&m_reg_motion_plus, 0, sizeof(m_reg_motion_plus));
235
236 memcpy(&m_reg_motion_plus.ext_identifier, motion_plus_id, sizeof(motion_plus_id));
237
238 // status
239 memset(&m_status, 0, sizeof(m_status));
240 // Battery levels in voltage
241 // 0x00 - 0x32: level 1
242 // 0x33 - 0x43: level 2
243 // 0x33 - 0x54: level 3
244 // 0x55 - 0xff: level 4
245 m_status.battery = 0x5f;
246
247 memset(m_shake_step, 0, sizeof(m_shake_step));
248
249 // clear read request queue
250 while (m_read_requests.size())
251 {
252 delete[] m_read_requests.front().data;
253 m_read_requests.pop();
254 }
255}
256
257Wiimote::Wiimote( const unsigned int index )
258 : m_index(index)
259 , ir_sin(0)
260 , ir_cos(1)
261// , m_sound_stream( NULL )
262{
263 // ---- set up all the controls ----
264
265 // buttons
266 groups.push_back(m_buttons = new Buttons("Buttons"));
267 for (unsigned int i=0; i < sizeof(named_buttons)/sizeof(*named_buttons); ++i)
268 m_buttons->controls.push_back(new ControlGroup::Input( named_buttons[i]));
269
270 // udp
271 groups.push_back(m_udp = new UDPWrapper(m_index, _trans("UDP Wiimote")"UDP Wiimote"));
272
273 // ir
274 groups.push_back(m_ir = new Cursor(_trans("IR")"IR"));
275
276 // swing
277 groups.push_back(m_swing = new Force(_trans("Swing")"Swing"));
278
279 // tilt
280 groups.push_back(m_tilt = new Tilt(_trans("Tilt")"Tilt"));
281
282 // shake
283 groups.push_back(m_shake = new Buttons(_trans("Shake")"Shake"));
284 m_shake->controls.push_back(new ControlGroup::Input("X"));
285 m_shake->controls.push_back(new ControlGroup::Input("Y"));
286 m_shake->controls.push_back(new ControlGroup::Input("Z"));
287
288 // extension
289 groups.push_back(m_extension = new Extension(_trans("Extension")"Extension"));
290 m_extension->attachments.push_back(new WiimoteEmu::None(m_reg_ext));
291 m_extension->attachments.push_back(new WiimoteEmu::Nunchuk(m_udp, m_reg_ext));
292 m_extension->attachments.push_back(new WiimoteEmu::Classic(m_reg_ext));
293 m_extension->attachments.push_back(new WiimoteEmu::Guitar(m_reg_ext));
294 m_extension->attachments.push_back(new WiimoteEmu::Drums(m_reg_ext));
295 m_extension->attachments.push_back(new WiimoteEmu::Turntable(m_reg_ext));
296
297 m_extension->settings.push_back(new ControlGroup::Setting(_trans("Motion Plus")"Motion Plus", 0, 0, 1));
298
299 // rumble
300 groups.push_back(m_rumble = new ControlGroup(_trans("Rumble")"Rumble"));
301 m_rumble->controls.push_back(new ControlGroup::Output(_trans("Motor")"Motor"));
302
303 // dpad
304 groups.push_back(m_dpad = new Buttons("D-Pad"));
305 for (unsigned int i=0; i < 4; ++i)
306 m_dpad->controls.push_back(new ControlGroup::Input(named_directions[i]));
307
308 // options
309 groups.push_back( m_options = new ControlGroup(_trans("Options")"Options"));
310 m_options->settings.push_back(new ControlGroup::Setting(_trans("Background Input")"Background Input", false));
311 m_options->settings.push_back(new ControlGroup::Setting(_trans("Sideways Wiimote")"Sideways Wiimote", false));
312 m_options->settings.push_back(new ControlGroup::Setting(_trans("Upright Wiimote")"Upright Wiimote", false));
313
314 // TODO: This value should probably be re-read if SYSCONF gets changed
315 m_sensor_bar_on_top = SConfig::GetInstance().m_SYSCONF->GetData<u8>("BT.BAR") != 0;
316
317 // --- reset eeprom/register/values to default ---
318 Reset();
319}
320
321std::string Wiimote::GetName() const
322{
323 return std::string("Wiimote") + char('1'+m_index);
324}
325
326// if windows is focused or background input is enabled
327#define HAS_FOCUS(Host_RendererHasFocus() || (m_options->settings[0]->value
!= 0))
(Host_RendererHasFocus() || (m_options->settings[0]->value != 0))
328
329bool Wiimote::Step()
330{
331 const bool has_focus = HAS_FOCUS(Host_RendererHasFocus() || (m_options->settings[0]->value
!= 0))
;
332
333 // TODO: change this a bit
334 m_motion_plus_present = m_extension->settings[0]->value != 0;
335
336 // no rumble if no focus
337 if (false == has_focus)
338 m_rumble_on = false;
339
340 m_rumble->controls[0]->control_ref->State(m_rumble_on);
341
342 // when a movie is active, this button status update is disabled (moved), because movies only record data reports.
343 if(!(Movie::IsPlayingInput() || Movie::IsRecordingInput()) || NetPlay::IsNetPlayRunning())
344 {
345 UpdateButtonsStatus(has_focus);
346 }
347
348 // check if there is a read data request
349 if (m_read_requests.size())
350 {
351 ReadRequest& rr = m_read_requests.front();
352 // send up to 16 bytes to the wii
353 SendReadDataReply(rr);
354 //SendReadDataReply(rr.channel, rr);
355
356 // if there is no more data, remove from queue
357 if (0 == rr.size)
358 {
359 delete[] rr.data;
360 m_read_requests.pop();
361 }
362
363 // dont send any other reports
364 return true;
365 }
366
367 // check if a status report needs to be sent
368 // this happens on wiimote sync and when extensions are switched
369 if (m_extension->active_extension != m_extension->switch_extension)
370 {
371 RequestStatus();
372
373 // Wiibrew: Following a connection or disconnection event on the Extension Port,
374 // data reporting is disabled and the Data Reporting Mode must be reset before new data can arrive.
375 // after a game receives an unrequested status report,
376 // it expects data reports to stop until it sets the reporting mode again
377 m_reporting_auto = false;
378
379 return true;
380 }
381
382 return false;
383}
384
385void Wiimote::UpdateButtonsStatus(bool has_focus)
386{
387 // update buttons in status struct
388 m_status.buttons = 0;
389 if (has_focus)
390 {
391 const bool is_sideways = m_options->settings[1]->value != 0;
392 m_buttons->GetState(&m_status.buttons, button_bitmasks);
393 m_dpad->GetState(&m_status.buttons, is_sideways ? dpad_sideways_bitmasks : dpad_bitmasks);
394 UDPTLayer::GetButtons(m_udp, &m_status.buttons);
395 }
396}
397
398void Wiimote::GetCoreData(u8* const data)
399{
400 // when a movie is active, the button update happens here instead of Wiimote::Step, to avoid potential desync issues.
401 if(Movie::IsPlayingInput() || Movie::IsRecordingInput() || NetPlay::IsNetPlayRunning())
402 {
403 UpdateButtonsStatus(HAS_FOCUS(Host_RendererHasFocus() || (m_options->settings[0]->value
!= 0))
);
404 }
405
406 *(wm_core*)data |= m_status.buttons;
407}
408
409void Wiimote::GetAccelData(u8* const data, u8* const buttons)
410{
411 const bool has_focus = HAS_FOCUS(Host_RendererHasFocus() || (m_options->settings[0]->value
!= 0))
;
412 const bool is_sideways = m_options->settings[1]->value != 0;
413 const bool is_upright = m_options->settings[2]->value != 0;
414 accel_cal* calib = (accel_cal*)&m_eeprom[0x16];
415
416 // ----TILT----
417 EmulateTilt(&m_accel, m_tilt, has_focus, is_sideways, is_upright);
418
419 // ----SWING----
420 // ----SHAKE----
421 if (has_focus)
422 {
423 EmulateSwing(&m_accel, m_swing, is_sideways, is_upright);
424 EmulateShake(&m_accel, calib, m_shake, m_shake_step);
425 UDPTLayer::GetAcceleration(m_udp, &m_accel);
426 }
427 wm_accel* dt = (wm_accel*)data;
428 double cx,cy,cz;
429 cx=trim(m_accel.x*(calib->one_g.x-calib->zero_g.x)+calib->zero_g.x);
430 cy=trim(m_accel.y*(calib->one_g.y-calib->zero_g.y)+calib->zero_g.y);
431 cz=trim(m_accel.z*(calib->one_g.z-calib->zero_g.z)+calib->zero_g.z);
432 dt->x=u8(cx);
433 dt->y=u8(cy);
434 dt->z=u8(cz);
435 if (buttons)
436 {
437 buttons[0]|=(u8(cx*4)&3)<<5;
438 buttons[1]|=((u8(cy*2)&1)<<5)|((u8(cz*2)&1)<<6);
439 }
440}
441#define kCutoffFreq5.0f 5.0f
442inline void LowPassFilter(double & var, double newval, double period)
443{
444 double RC=1.0/kCutoffFreq5.0f;
445 double alpha=period/(period+RC);
446 var = newval * alpha + var * (1.0 - alpha);
447}
448
449void Wiimote::GetIRData(u8* const data, bool use_accel)
450{
451 const bool has_focus = HAS_FOCUS(Host_RendererHasFocus() || (m_options->settings[0]->value
!= 0))
;
452
453 u16 x[4], y[4];
454 memset(x, 0xFF, sizeof(x));
455
456 if (has_focus)
1
Assuming 'has_focus' is 0
2
Taking false branch
457 {
458 float xx = 10000, yy = 0, zz = 0;
459 double nsin,ncos;
460
461 if (use_accel)
462 {
463 double ax,az,len;
464 ax=m_accel.x;
465 az=m_accel.z;
466 len=sqrt(ax*ax+az*az);
467 if (len)
468 {
469 ax/=len;
470 az/=len; //normalizing the vector
471 nsin=ax;
472 ncos=az;
473 }
474 else
475 {
476 nsin=0;
477 ncos=1;
478 }
479 // PanicAlert("%d %d %d\nx:%f\nz:%f\nsin:%f\ncos:%f",accel->x,accel->y,accel->z,ax,az,sin,cos);
480 //PanicAlert("%d %d %d\n%d %d %d\n%d %d %d",accel->x,accel->y,accel->z,calib->zero_g.x,calib->zero_g.y,calib->zero_g.z,
481 // calib->one_g.x,calib->one_g.y,calib->one_g.z);
482 }
483 else
484 {
485 nsin=0; //m_tilt stuff here (can't figure it out yet....)
486 ncos=1;
487 }
488
489 LowPassFilter(ir_sin,nsin,1.0f/60);
490 LowPassFilter(ir_cos,ncos,1.0f/60);
491
492 m_ir->GetState(&xx, &yy, &zz, true);
493 UDPTLayer::GetIR(m_udp, &xx, &yy, &zz);
494
495 Vertex v[4];
496
497 static const int camWidth=1024;
498 static const int camHeight=768;
499 static const double bndup=-0.315447;
500 static const double bnddown=0.85;
501 static const double bndleft=0.443364;
502 static const double bndright=-0.443364;
503 static const double dist1=100.f/camWidth; //this seems the optimal distance for zelda
504 static const double dist2=1.2f*dist1;
505
506 for (int i=0; i<4; i++)
507 {
508 v[i].x=xx*(bndright-bndleft)/2+(bndleft+bndright)/2;
509 if (m_sensor_bar_on_top) v[i].y=yy*(bndup-bnddown)/2+(bndup+bnddown)/2;
510 else v[i].y=yy*(bndup-bnddown)/2-(bndup+bnddown)/2;
511 v[i].z=0;
512 }
513
514 v[0].x-=(zz*0.5+1)*dist1;
515 v[1].x+=(zz*0.5+1)*dist1;
516 v[2].x-=(zz*0.5+1)*dist2;
517 v[3].x+=(zz*0.5+1)*dist2;
518
519#define printmatrix(m)MsgAlert(false, WARNING, "%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n"
,m[0][0],m[0][1],m[0][2],m[0][3],m[1][0],m[1][1],m[1][2],m[1]
[3],m[2][0],m[2][1],m[2][2],m[2][3],m[3][0],m[3][1],m[3][2],m
[3][3])
PanicAlert("%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n",m[0][0],m[0][1],m[0][2],m[0][3],m[1][0],m[1][1],m[1][2],m[1][3],m[2][0],m[2][1],m[2][2],m[2][3],m[3][0],m[3][1],m[3][2],m[3][3])MsgAlert(false, WARNING, "%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n"
,m[0][0],m[0][1],m[0][2],m[0][3],m[1][0],m[1][1],m[1][2],m[1]
[3],m[2][0],m[2][1],m[2][2],m[2][3],m[3][0],m[3][1],m[3][2],m
[3][3])
520 Matrix rot,tot;
521 static Matrix scale;
522 static bool isscale=false;
523 if (!isscale)
524 {
525 MatrixScale(scale,1,camWidth/camHeight,1);
526 //MatrixIdentity(scale);
527 }
528 MatrixRotationByZ(rot,ir_sin,ir_cos);
529 //MatrixIdentity(rot);
530 MatrixMultiply(tot,scale,rot);
531
532 for (int i=0; i<4; i++)
533 {
534 MatrixTransformVertex(tot,v[i]);
535 if ((v[i].x<-1)||(v[i].x>1)||(v[i].y<-1)||(v[i].y>1))
536 continue;
537 x[i]=(u16)round((v[i].x+1)/2*(camWidth-1));
538 y[i]=(u16)round((v[i].y+1)/2*(camHeight-1));
539 }
540 // PanicAlert("%f %f\n%f %f\n%f %f\n%f %f\n%d %d\n%d %d\n%d %d\n%d %d",
541 // v[0].x,v[0].y,v[1].x,v[1].y,v[2].x,v[2].y,v[3].x,v[3].y,
542 // x[0],y[0],x[1],y[1],x[2],y[2],x[3],y[38]);
543 }
544 // Fill report with valid data when full handshake was done
545 if (m_reg_ir.data[0x30])
3
Taking true branch
546 // ir mode
547 switch (m_reg_ir.mode)
4
Control jumps to 'case 1:' at line 550
548 {
549 // basic
550 case 1 :
551 {
552 memset(data, 0xFF, 10);
553 wm_ir_basic* const irdata = (wm_ir_basic*)data;
554 for (unsigned int i=0; i<2; ++i)
5
Loop condition is true. Entering loop body
555 {
556 if (x[i*2] < 1024 && y[i*2] < 768)
6
The left operand of '<' is a garbage value
557 {
558 irdata[i].x1 = u8(x[i*2]);
559 irdata[i].x1hi = x[i*2] >> 8;
560
561 irdata[i].y1 = u8(y[i*2]);
562 irdata[i].y1hi = y[i*2] >> 8;
563 }
564 if (x[i*2+1] < 1024 && y[i*2+1] < 768)
565 {
566 irdata[i].x2 = u8(x[i*2+1]);
567 irdata[i].x2hi = x[i*2+1] >> 8;
568
569 irdata[i].y2 = u8(y[i*2+1]);
570 irdata[i].y2hi = y[i*2+1] >> 8;
571 }
572 }
573 }
574 break;
575 // extended
576 case 3 :
577 {
578 memset(data, 0xFF, 12);
579 wm_ir_extended* const irdata = (wm_ir_extended*)data;
580 for (unsigned int i=0; i<4; ++i)
581 if (x[i] < 1024 && y[i] < 768)
582 {
583 irdata[i].x = u8(x[i]);
584 irdata[i].xhi = x[i] >> 8;
585
586 irdata[i].y = u8(y[i]);
587 irdata[i].yhi = y[i] >> 8;
588
589 irdata[i].size = 10;
590 }
591 }
592 break;
593 // full
594 case 5 :
595 PanicAlert("Full IR report")MsgAlert(false, WARNING, "Full IR report");
596 // UNSUPPORTED
597 break;
598 }
599}
600
601void Wiimote::GetExtData(u8* const data)
602{
603 m_extension->GetState(data, HAS_FOCUS(Host_RendererHasFocus() || (m_options->settings[0]->value
!= 0))
);
604
605 // i dont think anything accesses the extension data like this, but ill support it. Indeed, commercial games don't do this.
606 // i think it should be unencrpyted in the register, encrypted when read.
607 memcpy(m_reg_ext.controller_data, data, sizeof(wm_extension));
608
609 // motionplus pass-through modes
610 if (m_motion_plus_active)
611 {
612 switch (m_reg_motion_plus.ext_identifier[0x4])
613 {
614 // nunchuck pass-through mode
615 // Bit 7 of byte 5 is moved to bit 6 of byte 5, overwriting it
616 // Bit 0 of byte 4 is moved to bit 7 of byte 5
617 // Bit 3 of byte 5 is moved to bit 4 of byte 5, overwriting it
618 // Bit 1 of byte 5 is moved to bit 3 of byte 5
619 // Bit 0 of byte 5 is moved to bit 2 of byte 5, overwriting it
620 case 0x5:
621 //data[5] & (1 << 7)
622 //data[4] & (1 << 0)
623 //data[5] & (1 << 3)
624 //data[5] & (1 << 1)
625 //data[5] & (1 << 0)
626 break;
627
628 // classic controller/musical instrument pass-through mode
629 // Bit 0 of Byte 4 is overwritten
630 // Bits 0 and 1 of Byte 5 are moved to bit 0 of Bytes 0 and 1, overwriting
631 case 0x7:
632 //data[4] & (1 << 0)
633 //data[5] & (1 << 0)
634 //data[5] & (1 << 1)
635 break;
636
637 // unknown pass-through mode
638 default:
639 break;
640 }
641
642 ((wm_motionplus_data*)data)->is_mp_data = 0;
643 ((wm_motionplus_data*)data)->extension_connected = m_extension->active_extension;
644 }
645
646 if (0xAA == m_reg_ext.encryption)
647 wiimote_encrypt(&m_ext_key, data, 0x00, sizeof(wm_extension));
648}
649
650void Wiimote::Update()
651{
652 // no channel == not connected i guess
653 if (0 == m_reporting_channel)
654 return;
655
656 // returns true if a report was sent
657 if (Step())
658 return;
659
660 u8 data[MAX_PAYLOAD23];
661 memset(data, 0, sizeof(data));
662
663 // figure out what data we need
664 s8 rptf_size = MAX_PAYLOAD23;
665
666 Movie::SetPolledDevice();
667
668 const ReportFeatures& rptf = reporting_mode_features[m_reporting_mode - WM_REPORT_CORE0x30];
669 rptf_size = rptf.size;
670 if (Movie::IsPlayingInput() && Movie::PlayWiimote(m_index, data, rptf, m_reg_ir.mode))
671 {
672 if (rptf.core)
673 m_status.buttons = *(wm_core*)(data + rptf.core);
674 }
675 else
676 {
677 data[0] = 0xA1;
678 data[1] = m_reporting_mode;
679
680 // core buttons
681 if (rptf.core)
682 GetCoreData(data + rptf.core);
683
684 // acceleration
685 if (rptf.accel)
686 GetAccelData(data + rptf.accel, rptf.core?(data+rptf.core):NULL__null);
687
688 // IR
689 if (rptf.ir)
690 GetIRData(data + rptf.ir, (rptf.accel != 0));
691
692 // extension
693 if (rptf.ext)
694 GetExtData(data + rptf.ext);
695
696 // hybrid wiimote stuff (for now, it's not supported while recording)
697 if (WIIMOTE_SRC_HYBRID == g_wiimote_sources[m_index] && !Movie::IsRecordingInput())
698 {
699 using namespace WiimoteReal;
700
701 std::lock_guard<std::recursive_mutex> lk(g_refresh_lock);
702 if (g_wiimotes[m_index])
703 {
704 const Report& rpt = g_wiimotes[m_index]->ProcessReadQueue();
705 if (!rpt.empty())
706 {
707 const u8 *real_data = rpt.data();
708 switch (real_data[1])
709 {
710 // use data reports
711 default:
712 if (real_data[1] >= WM_REPORT_CORE0x30)
713 {
714 const ReportFeatures& real_rptf = reporting_mode_features[real_data[1] - WM_REPORT_CORE0x30];
715
716 // force same report type from real-wiimote
717 if (&real_rptf != &rptf)
718 rptf_size = 0;
719
720 // core
721 // mix real-buttons with emu-buttons in the status struct, and in the report
722 if (real_rptf.core && rptf.core)
723 {
724 m_status.buttons |= *(wm_core*)(real_data + real_rptf.core);
725 *(wm_core*)(data + rptf.core) = m_status.buttons;
726 }
727
728 // accel
729 // use real-accel data always i guess
730 if (real_rptf.accel && rptf.accel)
731 memcpy(data + rptf.accel, real_data + real_rptf.accel, sizeof(wm_accel));
732
733 // ir
734 // TODO
735
736 // ext
737 // use real-ext data if an emu-extention isn't chosen
738 if (real_rptf.ext && rptf.ext && (0 == m_extension->switch_extension))
739 memcpy(data + rptf.ext, real_data + real_rptf.ext, sizeof(wm_extension));
740 }
741 else if (WM_ACK_DATA0x22 != real_data[1] || m_extension->active_extension > 0)
742 rptf_size = 0;
743 else
744 // use real-acks if an emu-extension isn't chosen
745 rptf_size = -1;
746 break;
747
748 // use all status reports, after modification of the extension bit
749 case WM_STATUS_REPORT0x20 :
750 //if (m_extension->switch_extension)
751 //((wm_status_report*)(real_data + 2))->extension = (m_extension->active_extension > 0);
752 if (m_extension->active_extension)
753 ((wm_status_report*)(real_data + 2))->extension = 1;
754 rptf_size = -1;
755 break;
756
757 // use all read-data replies
758 case WM_READ_DATA_REPLY0x21:
759 rptf_size = -1;
760 break;
761
762 }
763
764 // copy over report from real-wiimote
765 if (-1 == rptf_size)
766 {
767 std::copy(rpt.begin(), rpt.end(), data);
768 rptf_size = rpt.size();
769 }
770 }
771 }
772 }
773 }
774 if (NetPlay::IsNetPlayRunning())
775 {
776 NetPlay_GetWiimoteData(m_index, data, rptf.size);
777 if (rptf.core)
778 m_status.buttons = *(wm_core*)(data + rptf.core);
779 }
780 if (!Movie::IsPlayingInput())
781 {
782 Movie::CheckWiimoteStatus(m_index, data, rptf, m_reg_ir.mode);
783 }
784
785 // don't send a data report if auto reporting is off
786 if (false == m_reporting_auto && data[2] >= WM_REPORT_CORE0x30)
787 return;
788
789 // send data report
790 if (rptf_size)
791 {
792 WiimoteEmu::Spy(this, data, rptf_size);
793 Core::Callback_WiimoteInterruptChannel(m_index, m_reporting_channel, data, rptf_size);
794 }
795}
796
797void Wiimote::ControlChannel(const u16 _channelID, const void* _pData, u32 _Size)
798{
799 // Check for custom communication
800 if (99 == _channelID)
801 {
802 // wiimote disconnected
803 //PanicAlert( "Wiimote Disconnected" );
804
805 // reset eeprom/register/reporting mode
806 Reset();
807 return;
808 }
809
810 // this all good?
811 m_reporting_channel = _channelID;
812
813 const hid_packet* const hidp = (hid_packet*)_pData;
814
815 INFO_LOG(WIIMOTE, "Emu ControlChannel (page: %i, type: 0x%02x, param: 0x%02x)", m_index, hidp->type, hidp->param)do { { if (LogTypes::LINFO <= 3) GenericLog(LogTypes::LINFO
, LogTypes::WIIMOTE, "/home/anal/dolphin-emu/Source/Core/Core/Src/HW/WiimoteEmu/WiimoteEmu.cpp"
, 815, "Emu ControlChannel (page: %i, type: 0x%02x, param: 0x%02x)"
, m_index, hidp->type, hidp->param); } } while (0)
;
816
817 switch (hidp->type)
818 {
819 case HID_TYPE_HANDSHAKE0 :
820 PanicAlert("HID_TYPE_HANDSHAKE - %s", (hidp->param == HID_PARAM_INPUT) ? "INPUT" : "OUPUT")MsgAlert(false, WARNING, "HID_TYPE_HANDSHAKE - %s", (hidp->
param == 1) ? "INPUT" : "OUPUT")
;
821 break;
822
823 case HID_TYPE_SET_REPORT5 :
824 if (HID_PARAM_INPUT1 == hidp->param)
825 {
826 PanicAlert("HID_TYPE_SET_REPORT - INPUT")MsgAlert(false, WARNING, "HID_TYPE_SET_REPORT - INPUT");
827 }
828 else
829 {
830 // AyuanX: My experiment shows Control Channel is never used
831 // shuffle2: but lwbt uses this, so we'll do what we must :)
832 HidOutputReport((wm_report*)hidp->data);
833
834 u8 handshake = HID_HANDSHAKE_SUCCESS0;
835 Core::Callback_WiimoteInterruptChannel(m_index, _channelID, &handshake, 1);
836 }
837 break;
838
839 case HID_TYPE_DATA0xA :
840 PanicAlert("HID_TYPE_DATA - %s", (hidp->param == HID_PARAM_INPUT) ? "INPUT" : "OUTPUT")MsgAlert(false, WARNING, "HID_TYPE_DATA - %s", (hidp->param
== 1) ? "INPUT" : "OUTPUT")
;
841 break;
842
843 default :
844 PanicAlert("HidControlChannel: Unknown type %x and param %x", hidp->type, hidp->param)MsgAlert(false, WARNING, "HidControlChannel: Unknown type %x and param %x"
, hidp->type, hidp->param)
;
845 break;
846 }
847
848}
849
850void Wiimote::InterruptChannel(const u16 _channelID, const void* _pData, u32 _Size)
851{
852 // this all good?
853 m_reporting_channel = _channelID;
854
855 const hid_packet* const hidp = (hid_packet*)_pData;
856
857 switch (hidp->type)
858 {
859 case HID_TYPE_DATA0xA:
860 switch (hidp->param)
861 {
862 case HID_PARAM_OUTPUT2 :
863 {
864 const wm_report* const sr = (wm_report*)hidp->data;
865
866 if (WIIMOTE_SRC_REAL & g_wiimote_sources[m_index])
867 {
868 switch (sr->wm)
869 {
870 // these two types are handled in RequestStatus() & ReadData()
871 case WM_REQUEST_STATUS0x15 :
872 case WM_READ_DATA0x17 :
873 if (WIIMOTE_SRC_REAL == g_wiimote_sources[m_index])
874 WiimoteReal::InterruptChannel(m_index, _channelID, _pData, _Size);
875 break;
876
877 default :
878 WiimoteReal::InterruptChannel(m_index, _channelID, _pData, _Size);
879 break;
880 }
881
882 HidOutputReport(sr, m_extension->switch_extension > 0);
883 }
884 else
885 HidOutputReport(sr);
886 }
887 break;
888
889 default :
890 PanicAlert("HidInput: HID_TYPE_DATA - param 0x%02x", hidp->param)MsgAlert(false, WARNING, "HidInput: HID_TYPE_DATA - param 0x%02x"
, hidp->param)
;
891 break;
892 }
893 break;
894
895 default:
896 PanicAlert("HidInput: Unknown type 0x%02x and param 0x%02x", hidp->type, hidp->param)MsgAlert(false, WARNING, "HidInput: Unknown type 0x%02x and param 0x%02x"
, hidp->type, hidp->param)
;
897 break;
898 }
899}
900
901void Wiimote::LoadDefaults(const ControllerInterface& ciface)
902{
903 ControllerEmu::LoadDefaults(ciface);
904
905 #define set_control(group, num, str)(group)->controls[num]->control_ref->expression = (str
)
(group)->controls[num]->control_ref->expression = (str)
906
907 // Buttons
908#if defined HAVE_X111 && HAVE_X111
909 set_control(m_buttons, 0, "Click 1")(m_buttons)->controls[0]->control_ref->expression = (
"Click 1")
; // A
910 set_control(m_buttons, 1, "Click 3")(m_buttons)->controls[1]->control_ref->expression = (
"Click 3")
; // B
911#else
912 set_control(m_buttons, 0, "Click 0")(m_buttons)->controls[0]->control_ref->expression = (
"Click 0")
; // A
913 set_control(m_buttons, 1, "Click 1")(m_buttons)->controls[1]->control_ref->expression = (
"Click 1")
; // B
914#endif
915 set_control(m_buttons, 2, "1")(m_buttons)->controls[2]->control_ref->expression = (
"1")
; // 1
916 set_control(m_buttons, 3, "2")(m_buttons)->controls[3]->control_ref->expression = (
"2")
; // 2
917 set_control(m_buttons, 4, "Q")(m_buttons)->controls[4]->control_ref->expression = (
"Q")
; // -
918 set_control(m_buttons, 5, "E")(m_buttons)->controls[5]->control_ref->expression = (
"E")
; // +
919
920#ifdef _WIN32
921 set_control(m_buttons, 6, "RETURN")(m_buttons)->controls[6]->control_ref->expression = (
"RETURN")
; // Home
922#else
923 set_control(m_buttons, 6, "Return")(m_buttons)->controls[6]->control_ref->expression = (
"Return")
; // Home
924#endif
925
926 // Shake
927 for (size_t i = 0; i != 3; ++i)
928 set_control(m_shake, i, "Click 2")(m_shake)->controls[i]->control_ref->expression = ("Click 2"
)
;
929
930 // IR
931 set_control(m_ir, 0, "Cursor Y-")(m_ir)->controls[0]->control_ref->expression = ("Cursor Y-"
)
;
932 set_control(m_ir, 1, "Cursor Y+")(m_ir)->controls[1]->control_ref->expression = ("Cursor Y+"
)
;
933 set_control(m_ir, 2, "Cursor X-")(m_ir)->controls[2]->control_ref->expression = ("Cursor X-"
)
;
934 set_control(m_ir, 3, "Cursor X+")(m_ir)->controls[3]->control_ref->expression = ("Cursor X+"
)
;
935
936 // DPad
937#ifdef _WIN32
938 set_control(m_dpad, 0, "UP")(m_dpad)->controls[0]->control_ref->expression = ("UP"
)
; // Up
939 set_control(m_dpad, 1, "DOWN")(m_dpad)->controls[1]->control_ref->expression = ("DOWN"
)
; // Down
940 set_control(m_dpad, 2, "LEFT")(m_dpad)->controls[2]->control_ref->expression = ("LEFT"
)
; // Left
941 set_control(m_dpad, 3, "RIGHT")(m_dpad)->controls[3]->control_ref->expression = ("RIGHT"
)
; // Right
942#elif __APPLE__
943 set_control(m_dpad, 0, "Up Arrow")(m_dpad)->controls[0]->control_ref->expression = ("Up Arrow"
)
; // Up
944 set_control(m_dpad, 1, "Down Arrow")(m_dpad)->controls[1]->control_ref->expression = ("Down Arrow"
)
; // Down
945 set_control(m_dpad, 2, "Left Arrow")(m_dpad)->controls[2]->control_ref->expression = ("Left Arrow"
)
; // Left
946 set_control(m_dpad, 3, "Right Arrow")(m_dpad)->controls[3]->control_ref->expression = ("Right Arrow"
)
; // Right
947#else
948 set_control(m_dpad, 0, "Up")(m_dpad)->controls[0]->control_ref->expression = ("Up"
)
; // Up
949 set_control(m_dpad, 1, "Down")(m_dpad)->controls[1]->control_ref->expression = ("Down"
)
; // Down
950 set_control(m_dpad, 2, "Left")(m_dpad)->controls[2]->control_ref->expression = ("Left"
)
; // Left
951 set_control(m_dpad, 3, "Right")(m_dpad)->controls[3]->control_ref->expression = ("Right"
)
; // Right
952#endif
953
954 // ugly stuff
955 // enable nunchuk
956 m_extension->switch_extension = 1;
957
958 // set nunchuk defaults
959 m_extension->attachments[1]->LoadDefaults(ciface);
960}
961
962}