Path: blob/master/thirdparty/sdl/hidapi/windows/hidapi_descriptor_reconstruct.h
9917 views
/*******************************************************1HIDAPI - Multi-Platform library for2communication with HID devices.34libusb/hidapi Team56Copyright 2022, All Rights Reserved.78At the discretion of the user of this library,9this software may be licensed under the terms of the10GNU General Public License v3, a BSD-Style license, or the11original HIDAPI license as outlined in the LICENSE.txt,12LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt13files located at the root of the source distribution.14These files may also be found in the public source15code repository located at:16https://github.com/libusb/hidapi .17********************************************************/18#include "SDL_internal.h"1920#ifndef HIDAPI_DESCRIPTOR_RECONSTRUCT_H__21#define HIDAPI_DESCRIPTOR_RECONSTRUCT_H__2223#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)24/* Do not warn about wcsncpy usage.25https://docs.microsoft.com/cpp/c-runtime-library/security-features-in-the-crt */26#define _CRT_SECURE_NO_WARNINGS27#endif2829#include "hidapi_winapi.h"3031#ifdef _MSC_VER32#pragma warning(push)33#pragma warning(disable: 4200)34#pragma warning(disable: 4201)35#pragma warning(disable: 4214)36#endif3738#include <windows.h>3940#include "hidapi_hidsdi.h"41/*#include <assert.h>*/4243#define NUM_OF_HIDP_REPORT_TYPES 34445typedef enum rd_items_ {46rd_main_input = 0x80, /* 1000 00 nn */47rd_main_output = 0x90, /* 1001 00 nn */48rd_main_feature = 0xB0, /* 1011 00 nn */49rd_main_collection = 0xA0, /* 1010 00 nn */50rd_main_collection_end = 0xC0, /* 1100 00 nn */51rd_global_usage_page = 0x04, /* 0000 01 nn */52rd_global_logical_minimum = 0x14, /* 0001 01 nn */53rd_global_logical_maximum = 0x24, /* 0010 01 nn */54rd_global_physical_minimum = 0x34, /* 0011 01 nn */55rd_global_physical_maximum = 0x44, /* 0100 01 nn */56rd_global_unit_exponent = 0x54, /* 0101 01 nn */57rd_global_unit = 0x64, /* 0110 01 nn */58rd_global_report_size = 0x74, /* 0111 01 nn */59rd_global_report_id = 0x84, /* 1000 01 nn */60rd_global_report_count = 0x94, /* 1001 01 nn */61rd_global_push = 0xA4, /* 1010 01 nn */62rd_global_pop = 0xB4, /* 1011 01 nn */63rd_local_usage = 0x08, /* 0000 10 nn */64rd_local_usage_minimum = 0x18, /* 0001 10 nn */65rd_local_usage_maximum = 0x28, /* 0010 10 nn */66rd_local_designator_index = 0x38, /* 0011 10 nn */67rd_local_designator_minimum = 0x48, /* 0100 10 nn */68rd_local_designator_maximum = 0x58, /* 0101 10 nn */69rd_local_string = 0x78, /* 0111 10 nn */70rd_local_string_minimum = 0x88, /* 1000 10 nn */71rd_local_string_maximum = 0x98, /* 1001 10 nn */72rd_local_delimiter = 0xA8 /* 1010 10 nn */73} rd_items;7475typedef enum rd_main_items_ {76rd_input = HidP_Input,77rd_output = HidP_Output,78rd_feature = HidP_Feature,79rd_collection,80rd_collection_end,81rd_delimiter_open,82rd_delimiter_usage,83rd_delimiter_close,84} rd_main_items;8586typedef struct rd_bit_range_ {87int FirstBit;88int LastBit;89} rd_bit_range;9091typedef enum rd_item_node_type_ {92rd_item_node_cap,93rd_item_node_padding,94rd_item_node_collection,95} rd_node_type;9697struct rd_main_item_node {98int FirstBit; /* Position of first bit in report (counting from 0) */99int LastBit; /* Position of last bit in report (counting from 0) */100rd_node_type TypeOfNode; /* Information if caps index refers to the array of button caps, value caps,101or if the node is just a padding element to fill unused bit positions.102The node can also be a collection node without any bits in the report. */103int CapsIndex; /* Index in the array of caps */104int CollectionIndex; /* Index in the array of link collections */105rd_main_items MainItemType; /* Input, Output, Feature, Collection or Collection End */106unsigned char ReportID;107struct rd_main_item_node* next;108};109110typedef struct hid_pp_caps_info_ {111USHORT FirstCap;112USHORT NumberOfCaps; // Includes empty caps after LastCap113USHORT LastCap;114USHORT ReportByteLength;115} hid_pp_caps_info, *phid_pp_caps_info;116117typedef struct hid_pp_link_collection_node_ {118USAGE LinkUsage;119USAGE LinkUsagePage;120USHORT Parent;121USHORT NumberOfChildren;122USHORT NextSibling;123USHORT FirstChild;124ULONG CollectionType : 8;125ULONG IsAlias : 1;126ULONG Reserved : 23;127// Same as the public API structure HIDP_LINK_COLLECTION_NODE, but without PVOID UserContext at the end128} hid_pp_link_collection_node, *phid_pp_link_collection_node;129130// Note: This is risk-reduction-measure for this specific struct, as it has ULONG bit-field.131// Although very unlikely, it might still be possible that the compiler creates a memory layout that is132// not binary compatile.133// Other structs are not checked at the time of writing.134//static_assert(sizeof(struct hid_pp_link_collection_node_) == 16,135// "Size of struct hid_pp_link_collection_node_ not as expected. This might break binary compatibility");136SDL_COMPILE_TIME_ASSERT(hid_pp_link_collection_node_, sizeof(struct hid_pp_link_collection_node_) == 16);137138typedef struct hidp_unknown_token_ {139UCHAR Token; /* Specifies the one-byte prefix of a global item. */140UCHAR Reserved[3];141ULONG BitField; /* Specifies the data part of the global item. */142} hidp_unknown_token, * phidp_unknown_token;143144typedef struct hid_pp_cap_ {145USAGE UsagePage;146UCHAR ReportID;147UCHAR BitPosition;148USHORT ReportSize; // WIN32 term for this is BitSize149USHORT ReportCount;150USHORT BytePosition;151USHORT BitCount;152ULONG BitField;153USHORT NextBytePosition;154USHORT LinkCollection;155USAGE LinkUsagePage;156USAGE LinkUsage;157158// Start of 8 Flags in one byte159BOOLEAN IsMultipleItemsForArray:1;160161BOOLEAN IsPadding:1;162BOOLEAN IsButtonCap:1;163BOOLEAN IsAbsolute:1;164BOOLEAN IsRange:1;165BOOLEAN IsAlias:1; // IsAlias is set to TRUE in the first n-1 capability structures added to the capability array. IsAlias set to FALSE in the nth capability structure.166BOOLEAN IsStringRange:1;167BOOLEAN IsDesignatorRange:1;168// End of 8 Flags in one byte169BOOLEAN Reserved1[3];170171hidp_unknown_token UnknownTokens[4]; // 4 x 8 Byte172173union {174struct {175USAGE UsageMin;176USAGE UsageMax;177USHORT StringMin;178USHORT StringMax;179USHORT DesignatorMin;180USHORT DesignatorMax;181USHORT DataIndexMin;182USHORT DataIndexMax;183} Range;184struct {185USAGE Usage;186USAGE Reserved1;187USHORT StringIndex;188USHORT Reserved2;189USHORT DesignatorIndex;190USHORT Reserved3;191USHORT DataIndex;192USHORT Reserved4;193} NotRange;194};195union {196struct {197LONG LogicalMin;198LONG LogicalMax;199} Button;200struct {201BOOLEAN HasNull;202UCHAR Reserved4[3];203LONG LogicalMin;204LONG LogicalMax;205LONG PhysicalMin;206LONG PhysicalMax;207} NotButton;208};209ULONG Units;210ULONG UnitsExp;211212} hid_pp_cap, *phid_pp_cap;213214typedef struct hidp_preparsed_data_ {215UCHAR MagicKey[8];216USAGE Usage;217USAGE UsagePage;218USHORT Reserved[2];219220// CAPS structure for Input, Output and Feature221hid_pp_caps_info caps_info[3];222223USHORT FirstByteOfLinkCollectionArray;224USHORT NumberLinkCollectionNodes;225226#ifndef _MSC_VER227// MINGW fails with: Flexible array member in union not supported228// Solution: https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html229union {230#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA231#pragma GCC diagnostic push232#pragma GCC diagnostic ignored "-Wpedantic"233#endif234hid_pp_cap caps[0];235hid_pp_link_collection_node LinkCollectionArray[0];236#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA237#pragma GCC diagnostic pop238#endif239};240#else241union {242hid_pp_cap caps[];243hid_pp_link_collection_node LinkCollectionArray[];244};245#endif246247} hidp_preparsed_data;248249#ifdef _MSC_VER250#pragma warning(pop)251#endif252253#endif254255256