MCPcopy Create free account
hub / github.com/defold/defold / SDLCreateJoystickGUIDReference

Function SDLCreateJoystickGUIDReference

engine/hid/src/test/test_hid.cpp:352–396  ·  view source on GitHub ↗

Reference implementation from SDL_joystick.c, using Defold types and helpers.

Source from the content-addressed store, hash-verified

350
351// Reference implementation from SDL_joystick.c, using Defold types and helpers.
352static dmHID::GamepadGuid SDLCreateJoystickGUIDReference(uint16_t bus, uint16_t vendor, uint16_t product, uint16_t version, const char* vendor_name, const char* product_name, uint8_t driver_signature, uint8_t driver_data)
353{
354 dmHID::GamepadGuid guid = {};
355 uint16_t crc = 0;
356
357 if (vendor_name && *vendor_name && product_name && *product_name)
358 {
359 crc = UpdateCRC16(crc, vendor_name);
360 crc = UpdateCRC16(crc, " ");
361 crc = UpdateCRC16(crc, product_name);
362 }
363 else if (product_name)
364 {
365 crc = UpdateCRC16(crc, product_name);
366 }
367
368 // SDL GUID words are stored little-endian, unlike Defold's network-order helpers.
369 guid.m_Bus = ByteSwapLE16(bus);
370 guid.m_CRC16 = ByteSwapLE16(crc);
371
372 if (vendor)
373 {
374 guid.m_Vendor = ByteSwapLE16(vendor);
375 guid.m_Product = ByteSwapLE16(product);
376 guid.m_Version = ByteSwapLE16(version);
377 guid.m_DriverSignature = driver_signature;
378 guid.m_DriverData = driver_data;
379 }
380 else
381 {
382 size_t available_space = sizeof(guid) - offsetof(dmHID::GamepadGuid, m_Vendor);
383
384 if (driver_signature)
385 {
386 available_space -= 2;
387 guid.m_DriverSignature = driver_signature;
388 guid.m_DriverData = driver_data;
389 }
390 if (product_name)
391 {
392 dmStrlCpy((char*) &guid.m_Vendor, product_name, available_space);
393 }
394 }
395 return guid;
396}
397
398static void AssertGamepadGuidEqual(const dmHID::GamepadGuid& expected, const dmHID::GamepadGuid& actual)
399{

Callers 1

TESTFunction · 0.85

Calls 3

ByteSwapLE16Function · 0.85
dmStrlCpyFunction · 0.85
UpdateCRC16Function · 0.70

Tested by

no test coverage detected