MCPcopy Create free account
hub / github.com/SFML/SFML / isConnected

Method isConnected

src/SFML/Window/macOS/JoystickImpl.cpp:103–175  ·  view source on GitHub ↗

///////////////////////////////////////////////////////

Source from the content-addressed store, hash-verified

101
102////////////////////////////////////////////////////////////
103bool JoystickImpl::isConnected(unsigned int index)
104{
105 const AutoreleasePool pool;
106 bool state = false; // Is the index-th joystick connected?
107
108 // First, let's check if the device was previously detected:
109 if (m_locationIDs[index] != 0)
110 {
111 state = true;
112 }
113 else
114 {
115 // Otherwise, let's check if it is now connected
116 // i.e., m_locationIDs[index] == 0
117
118 // if there is more connected joystick to the HID manager than
119 // opened joystick devices then we find the new one.
120
121 unsigned int openedCount = 0;
122 for (const auto& locationID : m_locationIDs)
123 {
124 if (locationID != 0)
125 ++openedCount;
126 }
127
128
129 const unsigned int connectedCount = HIDJoystickManager::getInstance().getJoystickCount();
130
131 if (connectedCount > openedCount)
132 {
133 // Get all devices
134 CFSetRef devices = HIDJoystickManager::getInstance().copyJoysticks();
135
136 if (devices != nullptr)
137 {
138 const CFIndex size = CFSetGetCount(devices);
139 if (size > 0)
140 {
141 std::vector<CFTypeRef> array(static_cast<std::size_t>(size)); // array of IOHIDDeviceRef
142 CFSetGetValues(devices, array.data());
143
144 // If there exists a device d s.t. there is no j s.t.
145 // m_locationIDs[j] == d's location then we have a new device.
146
147 for (CFIndex didx(0); !state && didx < size; ++didx)
148 {
149 auto* d = static_cast<IOHIDDeviceRef>(const_cast<void*>(array[static_cast<std::size_t>(didx)]));
150 const Location dloc = HIDInputManager::getLocationID(d);
151
152 bool foundJ = false;
153 for (unsigned int j(0); !foundJ && j < Joystick::Count; ++j)
154 {
155 if (m_locationIDs[j] == dloc)
156 foundJ = true;
157 }
158
159 if (!foundJ)
160 {

Callers

nothing calls this directly

Calls 2

getJoystickCountMethod · 0.80
copyJoysticksMethod · 0.80

Tested by

no test coverage detected