SDL_hid.c (3392B)
1 /* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20 */ 21 #include "../../SDL_internal.h" 22 23 #ifndef __WINRT__ 24 25 #include "SDL_hid.h" 26 27 28 HidD_GetString_t SDL_HidD_GetManufacturerString; 29 HidD_GetString_t SDL_HidD_GetProductString; 30 HidD_GetPreparsedData_t SDL_HidD_GetPreparsedData; 31 HidD_FreePreparsedData_t SDL_HidD_FreePreparsedData; 32 HidP_GetCaps_t SDL_HidP_GetCaps; 33 HidP_GetButtonCaps_t SDL_HidP_GetButtonCaps; 34 HidP_GetValueCaps_t SDL_HidP_GetValueCaps; 35 HidP_MaxDataListLength_t SDL_HidP_MaxDataListLength; 36 HidP_GetData_t SDL_HidP_GetData; 37 38 static HMODULE s_pHIDDLL = 0; 39 static int s_HIDDLLRefCount = 0; 40 41 42 int 43 WIN_LoadHIDDLL(void) 44 { 45 if (s_pHIDDLL) { 46 SDL_assert(s_HIDDLLRefCount > 0); 47 s_HIDDLLRefCount++; 48 return 0; /* already loaded */ 49 } 50 51 s_pHIDDLL = LoadLibrary(L"hid.dll"); 52 if (!s_pHIDDLL) { 53 return -1; 54 } 55 56 SDL_assert(s_HIDDLLRefCount == 0); 57 s_HIDDLLRefCount = 1; 58 59 SDL_HidD_GetManufacturerString = (HidD_GetString_t)GetProcAddress(s_pHIDDLL, "HidD_GetManufacturerString"); 60 SDL_HidD_GetProductString = (HidD_GetString_t)GetProcAddress(s_pHIDDLL, "HidD_GetProductString"); 61 SDL_HidD_GetPreparsedData = (HidD_GetPreparsedData_t)GetProcAddress(s_pHIDDLL, "HidD_GetPreparsedData"); 62 SDL_HidD_FreePreparsedData = (HidD_FreePreparsedData_t)GetProcAddress(s_pHIDDLL, "HidD_FreePreparsedData"); 63 SDL_HidP_GetCaps = (HidP_GetCaps_t)GetProcAddress(s_pHIDDLL, "HidP_GetCaps"); 64 SDL_HidP_GetButtonCaps = (HidP_GetButtonCaps_t)GetProcAddress(s_pHIDDLL, "HidP_GetButtonCaps"); 65 SDL_HidP_GetValueCaps = (HidP_GetValueCaps_t)GetProcAddress(s_pHIDDLL, "HidP_GetValueCaps"); 66 SDL_HidP_MaxDataListLength = (HidP_MaxDataListLength_t)GetProcAddress(s_pHIDDLL, "HidP_MaxDataListLength"); 67 SDL_HidP_GetData = (HidP_GetData_t)GetProcAddress(s_pHIDDLL, "HidP_GetData"); 68 if (!SDL_HidD_GetManufacturerString || !SDL_HidD_GetProductString || !SDL_HidD_GetPreparsedData || 69 !SDL_HidD_FreePreparsedData || !SDL_HidP_GetCaps || !SDL_HidP_GetButtonCaps || 70 !SDL_HidP_GetValueCaps || !SDL_HidP_MaxDataListLength || !SDL_HidP_GetData) { 71 WIN_UnloadHIDDLL(); 72 return -1; 73 } 74 75 return 0; 76 } 77 78 void 79 WIN_UnloadHIDDLL(void) 80 { 81 if (s_pHIDDLL) { 82 SDL_assert(s_HIDDLLRefCount > 0); 83 if (--s_HIDDLLRefCount == 0) { 84 FreeLibrary(s_pHIDDLL); 85 s_pHIDDLL = NULL; 86 } 87 } else { 88 SDL_assert(s_HIDDLLRefCount == 0); 89 } 90 } 91 92 #endif /* !__WINRT__ */ 93 94 /* vi: set ts=4 sw=4 expandtab: */