Bug Summary

File:Source/Core/InputCommon/Src/ControllerInterface/ControllerInterface.cpp
Location:line 258, column 31
Description:Called C++ object pointer is null

Annotated Source Code

1#include "ControllerInterface.h"
2
3#ifdef CIFACE_USE_XINPUT
4 #include "XInput/XInput.h"
5#endif
6#ifdef CIFACE_USE_DINPUT
7 #include "DInput/DInput.h"
8#endif
9#ifdef CIFACE_USE_XLIB
10 #include "Xlib/Xlib.h"
11#endif
12#ifdef CIFACE_USE_OSX
13 #include "OSX/OSX.h"
14#endif
15#ifdef CIFACE_USE_SDL
16 #include "SDL/SDL.h"
17#endif
18#ifdef CIFACE_USE_ANDROID
19 #include "Android/Android.h"
20#endif
21
22#include "Thread.h"
23
24namespace
25{
26const float INPUT_DETECT_THRESHOLD = 0.55f;
27}
28
29ControllerInterface g_controller_interface;
30
31//
32// Init
33//
34// detect devices and inputs outputs / will make refresh function later
35//
36void ControllerInterface::Initialize()
37{
38 if (m_is_init)
39 return;
40
41#ifdef CIFACE_USE_DINPUT
42 ciface::DInput::Init(m_devices, (HWND)m_hwnd);
43#endif
44#ifdef CIFACE_USE_XINPUT
45 ciface::XInput::Init(m_devices);
46#endif
47#ifdef CIFACE_USE_XLIB
48 ciface::Xlib::Init(m_devices, m_hwnd);
49#endif
50#ifdef CIFACE_USE_OSX
51 ciface::OSX::Init(m_devices, m_hwnd);
52#endif
53#ifdef CIFACE_USE_SDL
54 ciface::SDL::Init(m_devices);
55#endif
56#ifdef CIFACE_USE_ANDROID
57 ciface::Android::Init(m_devices);
58#endif
59
60 m_is_init = true;
61}
62
63//
64// DeInit
65//
66// remove all devices/ call library cleanup functions
67//
68void ControllerInterface::Shutdown()
69{
70 if (false == m_is_init)
71 return;
72
73 std::vector<Device*>::const_iterator
74 d = m_devices.begin(),
75 de = m_devices.end();
76 for ( ;d != de; ++d )
77 {
78 std::vector<Device::Output*>::const_iterator
79 o = (*d)->Outputs().begin(),
80 oe = (*d)->Outputs().end();
81 // set outputs to ZERO before destroying device
82 for ( ;o!=oe; ++o)
83 (*o)->SetState(0);
84 // update output
85 (*d)->UpdateOutput();
86
87 //delete device
88 delete *d;
89 }
90
91 m_devices.clear();
92
93#ifdef CIFACE_USE_XINPUT
94 // nothing needed
95#endif
96#ifdef CIFACE_USE_DINPUT
97 // nothing needed
98#endif
99#ifdef CIFACE_USE_XLIB
100 // nothing needed
101#endif
102#ifdef CIFACE_USE_OSX
103 ciface::OSX::DeInit();
104#endif
105#ifdef CIFACE_USE_SDL
106 // TODO: there seems to be some sort of memory leak with SDL, quit isn't freeing everything up
107 SDL_Quit();
108#endif
109#ifdef CIFACE_USE_ANDROID
110 // nothing needed
111#endif
112
113 m_is_init = false;
114}
115
116//
117// SetHwnd
118//
119// sets the hwnd used for some crap when initializing, use before calling Init
120//
121void ControllerInterface::SetHwnd( void* const hwnd )
122{
123 m_hwnd = hwnd;
124}
125
126//
127// UpdateInput
128//
129// update input for all devices, return true if all devices returned successful
130//
131bool ControllerInterface::UpdateInput(const bool force)
132{
133 std::unique_lock<std::recursive_mutex> lk(update_lock, std::defer_lock);
134
135 if (force)
136 lk.lock();
137 else if (!lk.try_lock())
138 return false;
139
140 size_t ok_count = 0;
141
142 std::vector<Device*>::const_iterator
143 d = m_devices.begin(),
144 e = m_devices.end();
145 for ( ;d != e; ++d )
146 {
147 if ((*d)->UpdateInput())
148 ++ok_count;
149 //else
150 // disabled. it might be causing problems
151 //(*d)->ClearInputState();
152 }
153
154 return (m_devices.size() == ok_count);
155}
156
157//
158// UpdateOutput
159//
160// update output for all devices, return true if all devices returned successful
161//
162bool ControllerInterface::UpdateOutput(const bool force)
163{
164 std::unique_lock<std::recursive_mutex> lk(update_lock, std::defer_lock);
165
166 if (force)
167 lk.lock();
168 else if (!lk.try_lock())
169 return false;
170
171 size_t ok_count = 0;
172
173 std::vector<Device*>::const_iterator
174 d = m_devices.begin(),
175 e = m_devices.end();
176 for (;d != e; ++d)
177 (*d)->UpdateOutput();
178
179 return (m_devices.size() == ok_count);
180}
181
182//
183// Device :: ~Device
184//
185// Destructor, delete all inputs/outputs on device destruction
186//
187ControllerInterface::Device::~Device()
188{
189 {
190 // delete inputs
191 std::vector<Device::Input*>::iterator
192 i = m_inputs.begin(),
193 e = m_inputs.end();
194 for ( ;i!=e; ++i)
195 delete *i;
196 }
197
198 {
199 // delete outputs
200 std::vector<Device::Output*>::iterator
201 o = m_outputs.begin(),
202 e = m_outputs.end();
203 for ( ;o!=e; ++o)
204 delete *o;
205 }
206}
207
208void ControllerInterface::Device::AddInput(Input* const i)
209{
210 m_inputs.push_back(i);
211}
212
213void ControllerInterface::Device::AddOutput(Output* const o)
214{
215 m_outputs.push_back(o);
216}
217
218//
219// Device :: ClearInputState
220//
221// Device classes should override this function
222// ControllerInterface will call this when the device returns failure during UpdateInput
223// used to try to set all buttons and axes to their default state when user unplugs a gamepad during play
224// buttons/axes that were held down at the time of unplugging should be seen as not pressed after unplugging
225//
226void ControllerInterface::Device::ClearInputState()
227{
228 // this is going to be called for every UpdateInput call that fails
229 // kinda slow but, w/e, should only happen when user unplugs a device while playing
230}
231
232//
233// InputReference :: State
234//
235// get the state of an input reference
236// override function for ControlReference::State ...
237//
238ControlState ControllerInterface::InputReference::State( const ControlState ignore )
239{
240 //if (NULL == device)
241 //return 0;
242
243 ControlState state = 0;
244
245 std::vector<DeviceControl>::const_iterator
246 ci = m_controls.begin(),
247 ce = m_controls.end();
248
249 // bit of hax for "NOT" to work at start of expression
250 if (ci != ce)
1
Taking false branch
251 {
252 if (ci->mode == 2)
253 state = 1;
254 }
255
256 for (; ci!=ce; ++ci)
2
Loop condition is true. Entering loop body
4
Loop condition is true. Entering loop body
7
Loop condition is true. Entering loop body
9
Loop condition is true. Entering loop body
257 {
258 const ControlState istate = ci->control->ToInput()->GetState();
10
Called C++ object pointer is null
259
260 switch (ci->mode)
3
'Default' branch taken. Execution continues on line 256
5
Control jumps to 'case 1:' at line 267
8
'Default' branch taken. Execution continues on line 256
261 {
262 // OR
263 case 0 :
264 state = std::max(state, istate);
265 break;
266 // AND
267 case 1 :
268 state = std::min(state, istate);
269 break;
6
Execution continues on line 256
270 // NOT
271 case 2 :
272 state = std::max(std::min(state, 1.0f - istate), 0.0f);
273 break;
274 // ADD
275 case 3 :
276 state += istate;
277 break;
278 }
279 }
280
281 return std::min(1.0f, state * range);
282}
283
284//
285// OutputReference :: State
286//
287// set the state of all binded outputs
288// overrides ControlReference::State .. combined them so i could make the gui simple / inputs == same as outputs one list
289// i was lazy and it works so watever
290//
291ControlState ControllerInterface::OutputReference::State(const ControlState state)
292{
293 const ControlState tmp_state = std::min(1.0f, state * range);
294
295 // output ref just ignores the modes ( |&!... )
296
297 std::vector<DeviceControl>::iterator
298 ci = m_controls.begin(),
299 ce = m_controls.end();
300 for (; ci != ce; ++ci)
301 ci->control->ToOutput()->SetState(tmp_state);
302
303 return state; // just return the output, watever
304}
305
306//
307// DeviceQualifier :: ToString
308//
309// get string from a device qualifier / serialize
310//
311std::string ControllerInterface::DeviceQualifier::ToString() const
312{
313 if (source.empty() && (cid < 0) && name.empty())
314 return "";
315 std::ostringstream ss;
316 ss << source << '/';
317 if ( cid > -1 )
318 ss << cid;
319 ss << '/' << name;
320 return ss.str();
321}
322
323//
324// DeviceQualifier :: FromString
325//
326// set a device qualifier from a string / unserialize
327//
328void ControllerInterface::DeviceQualifier::FromString(const std::string& str)
329{
330 std::istringstream ss(str);
331
332 std::getline(ss, source = "", '/');
333
334 // silly
335 std::getline(ss, name, '/');
336 std::istringstream(name) >> (cid = -1);
337
338 std::getline(ss, name = "");
339}
340
341//
342// DeviceQualifier :: FromDevice
343//
344// set a device qualifier from a device
345//
346void ControllerInterface::DeviceQualifier::FromDevice(const ControllerInterface::Device* const dev)
347{
348 name = dev->GetName();
349 cid = dev->GetId();
350 source= dev->GetSource();
351}
352
353bool ControllerInterface::DeviceQualifier::operator==(const ControllerInterface::Device* const dev) const
354{
355 if (dev->GetId() == cid)
356 if (dev->GetName() == name)
357 if (dev->GetSource() == source)
358 return true;
359 return false;
360}
361
362bool ControllerInterface::DeviceQualifier::operator==(const ControllerInterface::DeviceQualifier& devq) const
363{
364 if (cid == devq.cid)
365 if (name == devq.name)
366 if (source == devq.source)
367 return true;
368
369 return false;
370}
371
372//
373// UpdateReference
374//
375// updates a controlreference's binded devices/controls
376// need to call this to re-parse a control reference's expression after changing it
377//
378void ControllerInterface::UpdateReference(ControllerInterface::ControlReference* ref
379 , const ControllerInterface::DeviceQualifier& default_device) const
380{
381 ref->m_controls.clear();
382
383 // adding | to parse the last item, silly
384 std::istringstream ss(ref->expression + '|');
385
386 const std::string mode_chars("|&!^");
387
388 ControlReference::DeviceControl devc;
389
390 std::string dev_str;
391 std::string ctrl_str;
392
393 char c = 0;
394 while (ss.read(&c, 1))
395 {
396 const size_t f = mode_chars.find(c);
397
398 if (mode_chars.npos != f)
399 {
400 // add ctrl
401 if (ctrl_str.size())
402 {
403 DeviceQualifier devq;
404
405 // using default device or alterate device inside `backticks`
406 if (dev_str.empty())
407 devq = default_device;
408 else
409 devq.FromString(dev_str);
410
411 // find device
412 Device* const def_device = FindDevice(devq);
413
414 if (def_device)
415 {
416 if (ref->is_input)
417 devc.control = FindInput(ctrl_str, def_device);
418 else
419 devc.control = FindOutput(ctrl_str, def_device);
420
421 if (devc.control)
422 ref->m_controls.push_back(devc);
423 }
424 }
425 // reset stuff for next ctrl
426 devc.mode = (int)f;
427 ctrl_str.clear();
428 }
429 else if ('`' == c)
430 {
431 // different device
432 if (std::getline(ss, dev_str, '`').eof())
433 break; // no terminating '`' character
434 }
435 else
436 {
437 ctrl_str += c;
438 }
439 }
440}
441
442ControllerInterface::Device* ControllerInterface::FindDevice(const ControllerInterface::DeviceQualifier& devq) const
443{
444 std::vector<ControllerInterface::Device*>::const_iterator
445 di = m_devices.begin(),
446 de = m_devices.end();
447 for (; di!=de; ++di)
448 if (devq == *di)
449 return *di;
450
451 return NULL__null;
452}
453
454//
455// InputReference :: Detect
456//
457// wait for input on all binded devices
458// supports not detecting inputs that were held down at the time of Detect start,
459// which is useful for those crazy flightsticks that have certain buttons that are always held down
460// or some crazy axes or something
461// upon input, return pointer to detected Control
462// else return NULL
463//
464ControllerInterface::Device::Control* ControllerInterface::InputReference::Detect(const unsigned int ms, Device* const device)
465{
466 unsigned int time = 0;
467 std::vector<bool> states(device->Inputs().size());
468
469 if (device->Inputs().size() == 0)
470 return NULL__null;
471
472 // get starting state of all inputs,
473 // so we can ignore those that were activated at time of Detect start
474 std::vector<Device::Input*>::const_iterator
475 i = device->Inputs().begin(),
476 e = device->Inputs().end();
477 for (std::vector<bool>::iterator state = states.begin(); i != e; ++i)
478 *state++ = ((*i)->GetState() > (1 - INPUT_DETECT_THRESHOLD));
479
480 while (time < ms)
481 {
482 device->UpdateInput();
483 i = device->Inputs().begin();
484 for (std::vector<bool>::iterator state = states.begin(); i != e; ++i,++state)
485 {
486 // detected an input
487 if ((*i)->IsDetectable() && (*i)->GetState() > INPUT_DETECT_THRESHOLD)
488 {
489 // input was released at some point during Detect call
490 // return the detected input
491 if (false == *state)
492 return *i;
493 }
494 else if ((*i)->GetState() < (1 - INPUT_DETECT_THRESHOLD))
495 {
496 *state = false;
497 }
498 }
499 Common::SleepCurrentThread(10); time += 10;
500 }
501
502 // no input was detected
503 return NULL__null;
504}
505
506//
507// OutputReference :: Detect
508//
509// Totally different from the inputReference detect / I have them combined so it was simpler to make the GUI.
510// The GUI doesn't know the difference between an input and an output / it's odd but I was lazy and it was easy
511//
512// set all binded outputs to <range> power for x milliseconds return false
513//
514ControllerInterface::Device::Control* ControllerInterface::OutputReference::Detect(const unsigned int ms, Device* const device)
515{
516 // ignore device
517
518 // don't hang if we don't even have any controls mapped
519 if (m_controls.size())
520 {
521 State(1);
522 unsigned int slept = 0;
523
524 // this loop is to make stuff like flashing keyboard LEDs work
525 while (ms > (slept += 10))
526 {
527 // TODO: improve this to update more than just the default device's output
528 device->UpdateOutput();
529 Common::SleepCurrentThread(10);
530 }
531
532 State(0);
533 device->UpdateOutput();
534 }
535 return NULL__null;
536}
537
538ControllerInterface::Device::Input* ControllerInterface::Device::FindInput(const std::string &name) const
539{
540 std::vector<Input*>::const_iterator
541 it = m_inputs.begin(),
542 itend = m_inputs.end();
543 for (; it != itend; ++it)
544 if ((*it)->GetName() == name)
545 return *it;
546
547 return NULL__null;
548}
549
550ControllerInterface::Device::Output* ControllerInterface::Device::FindOutput(const std::string &name) const
551{
552 std::vector<Output*>::const_iterator
553 it = m_outputs.begin(),
554 itend = m_outputs.end();
555 for (; it != itend; ++it)
556 if ((*it)->GetName() == name)
557 return *it;
558
559 return NULL__null;
560}
561
562ControllerInterface::Device::Input* ControllerInterface::FindInput(const std::string& name, const Device* def_dev) const
563{
564 if (def_dev)
565 {
566 Device::Input* const inp = def_dev->FindInput(name);
567 if (inp)
568 return inp;
569 }
570
571 std::vector<Device*>::const_iterator
572 di = m_devices.begin(),
573 de = m_devices.end();
574 for (; di != de; ++di)
575 {
576 Device::Input* const i = (*di)->FindInput(name);
577
578 if (i)
579 return i;
580 }
581
582 return NULL__null;
583}
584
585ControllerInterface::Device::Output* ControllerInterface::FindOutput(const std::string& name, const Device* def_dev) const
586{
587 return def_dev->FindOutput(name);
588}