From 788e433d0a628fc8aa2aca3b23c1f64b16d5f4be Mon Sep 17 00:00:00 2001 From: Toby Gray Date: Tue, 15 Jan 2013 22:36:47 +0000 Subject: Windows: Add windows_common.h header * This file contains definition that will be shared between the Windows and Windows CE backends --- libusb/Makefile.am | 3 +- libusb/os/windows_common.h | 108 ++++++++++++++++++++++++++++++++ libusb/os/windows_usb.h | 65 +------------------ libusb/version_nano.h | 2 +- msvc/libusb_dll.dsp | 4 ++ msvc/libusb_dll_2005.vcproj | 53 ++++++++-------- msvc/libusb_dll_2010.vcxproj | 1 + msvc/libusb_dll_2010.vcxproj.filters | 3 + msvc/libusb_static.dsp | 4 ++ msvc/libusb_static_2005.vcproj | 4 ++ msvc/libusb_static_2010.vcxproj | 1 + msvc/libusb_static_2010.vcxproj.filters | 3 + msvc/libusb_static_2012.vcxproj | 1 + msvc/libusb_static_2012.vcxproj.filters | 3 + 14 files changed, 165 insertions(+), 90 deletions(-) create mode 100644 libusb/os/windows_common.h diff --git a/libusb/Makefile.am b/libusb/Makefile.am index 53fba6e..d248e47 100644 --- a/libusb/Makefile.am +++ b/libusb/Makefile.am @@ -50,7 +50,8 @@ libusb_1_0_la_LDFLAGS = $(LTLDFLAGS) libusb_1_0_la_SOURCES = libusbi.h core.c descriptor.c io.c sync.c $(OS_SRC) \ os/linux_usbfs.h os/darwin_usb.h os/windows_usb.h \ $(THREADS_SRC) \ - os/poll_posix.h os/poll_windows.h + os/poll_posix.h os/poll_windows.h \ + os/windows_common.h hdrdir = $(includedir)/libusb-1.0 hdr_HEADERS = libusb.h diff --git a/libusb/os/windows_common.h b/libusb/os/windows_common.h new file mode 100644 index 0000000..d12be6b --- /dev/null +++ b/libusb/os/windows_common.h @@ -0,0 +1,108 @@ +/* + * Windows backend common header for libusbx 1.0 + * + * This file brings together header code common between + * the desktop Windows and Windows CE backends. + * Copyright © 2009-2012 Pete Batard + * With contributions from Michael Plante, Orin Eman et al. + * Parts of this code adapted from libusb-win32-v1 by Stephan Meyer + * Major code testing contribution by Xiaofan Chen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#pragma once + +// Windows API default is uppercase - ugh! +#if !defined(bool) +#define bool BOOL +#endif +#if !defined(true) +#define true TRUE +#endif +#if !defined(false) +#define false FALSE +#endif + +#define safe_free(p) do {if (p != NULL) {free((void*)p); p = NULL;}} while(0) +#define safe_closehandle(h) do {if (h != INVALID_HANDLE_VALUE) {CloseHandle(h); h = INVALID_HANDLE_VALUE;}} while(0) +#define safe_min(a, b) min((size_t)(a), (size_t)(b)) +#define safe_strcp(dst, dst_max, src, count) do {memcpy(dst, src, safe_min(count, dst_max)); \ + ((char*)dst)[safe_min(count, dst_max)-1] = 0;} while(0) +#define safe_strcpy(dst, dst_max, src) safe_strcp(dst, dst_max, src, safe_strlen(src)+1) +#define safe_strncat(dst, dst_max, src, count) strncat(dst, src, safe_min(count, dst_max - safe_strlen(dst) - 1)) +#define safe_strcat(dst, dst_max, src) safe_strncat(dst, dst_max, src, safe_strlen(src)+1) +#define safe_strcmp(str1, str2) strcmp(((str1==NULL)?"":str1), ((str2==NULL)?"":str2)) +#define safe_stricmp(str1, str2) _stricmp(((str1==NULL)?"":str1), ((str2==NULL)?"":str2)) +#define safe_strncmp(str1, str2, count) strncmp(((str1==NULL)?"":str1), ((str2==NULL)?"":str2), count) +#define safe_strlen(str) ((str==NULL)?0:strlen(str)) +#define safe_sprintf _snprintf +#define safe_stprintf _sntprintf +#define safe_tcslen(str) ((str==NULL)?0:_tcslen(str)) +#define safe_unref_device(dev) do {if (dev != NULL) {libusb_unref_device(dev); dev = NULL;}} while(0) +#define wchar_to_utf8_ms(wstr, str, strlen) WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, strlen, NULL, NULL) +#ifndef ARRAYSIZE +#define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0])) +#endif + +#define ERR_BUFFER_SIZE 256 +#define TIMER_REQUEST_RETRY_MS 100 +#define MAX_TIMER_SEMAPHORES 128 + + +/* + * API macros - from libusb-win32 1.x + */ +#define DLL_DECLARE_PREFIXNAME(api, ret, prefixname, name, args) \ + typedef ret (api * __dll_##name##_t)args; \ + static __dll_##name##_t prefixname = NULL + +#ifndef _WIN32_WCE +#define DLL_STRINGIFY(dll) #dll +#define DLL_GET_MODULE_HANDLE(dll) GetModuleHandleA(DLL_STRINGIFY(dll)) +#define DLL_LOAD_LIBRARY(dll) LoadLibraryA(DLL_STRINGIFY(dll)) +#else +#define DLL_STRINGIFY(dll) L#dll +#define DLL_GET_MODULE_HANDLE(dll) GetModuleHandle(DLL_STRINGIFY(dll)) +#define DLL_LOAD_LIBRARY(dll) LoadLibrary(DLL_STRINGIFY(dll)) +#endif + +#define DLL_LOAD_PREFIXNAME(dll, prefixname, name, ret_on_failure) \ + do { \ + HMODULE h = DLL_GET_MODULE_HANDLE(dll); \ + if (!h) \ + h = DLL_LOAD_LIBRARY(dll); \ + if (!h) { \ + if (ret_on_failure) { return LIBUSB_ERROR_NOT_FOUND; } \ + else { break; } \ + } \ + prefixname = (__dll_##name##_t)GetProcAddress(h, \ + DLL_STRINGIFY(name)); \ + if (prefixname) break; \ + prefixname = (__dll_##name##_t)GetProcAddress(h, \ + DLL_STRINGIFY(name) DLL_STRINGIFY(A)); \ + if (prefixname) break; \ + prefixname = (__dll_##name##_t)GetProcAddress(h, \ + DLL_STRINGIFY(name) DLL_STRINGIFY(W)); \ + if (prefixname) break; \ + if(ret_on_failure) \ + return LIBUSB_ERROR_NOT_FOUND; \ + } while(0) + +#define DLL_DECLARE(api, ret, name, args) DLL_DECLARE_PREFIXNAME(api, ret, name, name, args) +#define DLL_LOAD(dll, name, ret_on_failure) DLL_LOAD_PREFIXNAME(dll, name, name, ret_on_failure) +#define DLL_DECLARE_PREFIXED(api, ret, prefix, name, args) DLL_DECLARE_PREFIXNAME(api, ret, prefix##name, name, args) +#define DLL_LOAD_PREFIXED(dll, prefix, name, ret_on_failure) DLL_LOAD_PREFIXNAME(dll, prefix##name, name, ret_on_failure) + diff --git a/libusb/os/windows_usb.h b/libusb/os/windows_usb.h index 8c5329b..5d67a56 100644 --- a/libusb/os/windows_usb.h +++ b/libusb/os/windows_usb.h @@ -22,6 +22,8 @@ #pragma once +#include "windows_common.h" + #if defined(_MSC_VER) // disable /W4 MSVC warnings that are benign #pragma warning(disable:4127) // conditional expression is constant @@ -30,17 +32,6 @@ #pragma warning(disable:4201) // nameless struct/union #endif -// Windows API default is uppercase - ugh! -#if !defined(bool) -#define bool BOOL -#endif -#if !defined(true) -#define true TRUE -#endif -#if !defined(false) -#define false FALSE -#endif - // Missing from MSVC6 setupapi.h #if !defined(SPDRP_ADDRESS) #define SPDRP_ADDRESS 28 @@ -57,24 +48,6 @@ extern char *_strdup(const char *strSource); // _beginthreadex is MSVCRT => unavailable for cygwin. Fallback to using CreateThread #define _beginthreadex(a, b, c, d, e, f) CreateThread(a, b, (LPTHREAD_START_ROUTINE)c, d, e, f) #endif -#define safe_free(p) do {if (p != NULL) {free((void*)p); p = NULL;}} while(0) -#define safe_closehandle(h) do {if (h != INVALID_HANDLE_VALUE) {CloseHandle(h); h = INVALID_HANDLE_VALUE;}} while(0) -#define safe_min(a, b) min((size_t)(a), (size_t)(b)) -#define safe_strcp(dst, dst_max, src, count) do {memcpy(dst, src, safe_min(count, dst_max)); \ - ((char*)dst)[safe_min(count, dst_max)-1] = 0;} while(0) -#define safe_strcpy(dst, dst_max, src) safe_strcp(dst, dst_max, src, safe_strlen(src)+1) -#define safe_strncat(dst, dst_max, src, count) strncat(dst, src, safe_min(count, dst_max - safe_strlen(dst) - 1)) -#define safe_strcat(dst, dst_max, src) safe_strncat(dst, dst_max, src, safe_strlen(src)+1) -#define safe_strcmp(str1, str2) strcmp(((str1==NULL)?"":str1), ((str2==NULL)?"":str2)) -#define safe_stricmp(str1, str2) _stricmp(((str1==NULL)?"":str1), ((str2==NULL)?"":str2)) -#define safe_strncmp(str1, str2, count) strncmp(((str1==NULL)?"":str1), ((str2==NULL)?"":str2), count) -#define safe_strlen(str) ((str==NULL)?0:strlen(str)) -#define safe_sprintf _snprintf -#define safe_unref_device(dev) do {if (dev != NULL) {libusb_unref_device(dev); dev = NULL;}} while(0) -#define wchar_to_utf8_ms(wstr, str, strlen) WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, strlen, NULL, NULL) -#ifndef ARRAYSIZE -#define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0])) -#endif #define MAX_CTRL_BUFFER_LENGTH 4096 #define MAX_USB_DEVICES 256 @@ -84,9 +57,6 @@ extern char *_strdup(const char *strSource); #define MAX_GUID_STRING_LENGTH 40 #define MAX_PATH_LENGTH 128 #define MAX_KEY_LENGTH 256 -#define MAX_TIMER_SEMAPHORES 128 -#define TIMER_REQUEST_RETRY_MS 100 -#define ERR_BUFFER_SIZE 256 #define LIST_SEPARATOR ';' #define HTAB_SIZE 1021 @@ -332,37 +302,6 @@ struct driver_lookup { const char* designation; // internal designation (for debug output) }; -/* - * API macros - from libusb-win32 1.x - */ -#define DLL_DECLARE_PREFIXNAME(api, ret, prefixname, name, args) \ - typedef ret (api * __dll_##name##_t)args; \ - static __dll_##name##_t prefixname = NULL - -#define DLL_LOAD_PREFIXNAME(dll, prefixname, name, ret_on_failure) \ - do { \ - HMODULE h = GetModuleHandleA(#dll); \ - if (!h) \ - h = LoadLibraryA(#dll); \ - if (!h) { \ - if (ret_on_failure) { return LIBUSB_ERROR_NOT_FOUND; } \ - else { break; } \ - } \ - prefixname = (__dll_##name##_t)GetProcAddress(h, #name); \ - if (prefixname) break; \ - prefixname = (__dll_##name##_t)GetProcAddress(h, #name "A"); \ - if (prefixname) break; \ - prefixname = (__dll_##name##_t)GetProcAddress(h, #name "W"); \ - if (prefixname) break; \ - if(ret_on_failure) \ - return LIBUSB_ERROR_NOT_FOUND; \ - } while(0) - -#define DLL_DECLARE(api, ret, name, args) DLL_DECLARE_PREFIXNAME(api, ret, name, name, args) -#define DLL_LOAD(dll, name, ret_on_failure) DLL_LOAD_PREFIXNAME(dll, name, name, ret_on_failure) -#define DLL_DECLARE_PREFIXED(api, ret, prefix, name, args) DLL_DECLARE_PREFIXNAME(api, ret, prefix##name, name, args) -#define DLL_LOAD_PREFIXED(dll, prefix, name, ret_on_failure) DLL_LOAD_PREFIXNAME(dll, prefix##name, name, ret_on_failure) - /* OLE32 dependency */ DLL_DECLARE_PREFIXED(WINAPI, HRESULT, p, CLSIDFromString, (LPCOLESTR, LPCLSID)); diff --git a/libusb/version_nano.h b/libusb/version_nano.h index fec778d..71078a1 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 10593 +#define LIBUSB_NANO 10594 diff --git a/msvc/libusb_dll.dsp b/msvc/libusb_dll.dsp index bad8d15..f2b15f0 100644 --- a/msvc/libusb_dll.dsp +++ b/msvc/libusb_dll.dsp @@ -181,6 +181,10 @@ SOURCE=..\libusb\os\threads_windows.h SOURCE=..\libusb\os\windows_usb.h # End Source File +# Begin Source File + +SOURCE=..\libusb\os\windows_common.h +# End Source File # End Group # Begin Group "Resource Files" diff --git a/msvc/libusb_dll_2005.vcproj b/msvc/libusb_dll_2005.vcproj index 0672695..976f664 100644 --- a/msvc/libusb_dll_2005.vcproj +++ b/msvc/libusb_dll_2005.vcproj @@ -97,12 +97,11 @@ /> - - - @@ -393,23 +388,31 @@ > + + + + diff --git a/msvc/libusb_dll_2010.vcxproj b/msvc/libusb_dll_2010.vcxproj index f1881fe..707f458 100644 --- a/msvc/libusb_dll_2010.vcxproj +++ b/msvc/libusb_dll_2010.vcxproj @@ -153,6 +153,7 @@ + diff --git a/msvc/libusb_dll_2010.vcxproj.filters b/msvc/libusb_dll_2010.vcxproj.filters index 3dacb2c..575bc4f 100644 --- a/msvc/libusb_dll_2010.vcxproj.filters +++ b/msvc/libusb_dll_2010.vcxproj.filters @@ -55,6 +55,9 @@ Header Files + + Header Files + diff --git a/msvc/libusb_static.dsp b/msvc/libusb_static.dsp index b636691..afdbbed 100644 --- a/msvc/libusb_static.dsp +++ b/msvc/libusb_static.dsp @@ -165,6 +165,10 @@ SOURCE=..\libusb\os\threads_windows.h SOURCE=..\libusb\os\windows_usb.h # End Source File +# Begin Source File + +SOURCE=..\libusb\os\windows_common.h +# End Source File # End Group # End Target # End Project diff --git a/msvc/libusb_static_2005.vcproj b/msvc/libusb_static_2005.vcproj index e9ba31d..1d41004 100644 --- a/msvc/libusb_static_2005.vcproj +++ b/msvc/libusb_static_2005.vcproj @@ -339,6 +339,10 @@ RelativePath="..\libusb\os\windows_usb.h" > + + diff --git a/msvc/libusb_static_2010.vcxproj b/msvc/libusb_static_2010.vcxproj index 57254be..0c445a4 100644 --- a/msvc/libusb_static_2010.vcxproj +++ b/msvc/libusb_static_2010.vcxproj @@ -143,6 +143,7 @@ + diff --git a/msvc/libusb_static_2010.vcxproj.filters b/msvc/libusb_static_2010.vcxproj.filters index 8585fcf..39dc64e 100644 --- a/msvc/libusb_static_2010.vcxproj.filters +++ b/msvc/libusb_static_2010.vcxproj.filters @@ -52,5 +52,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/msvc/libusb_static_2012.vcxproj b/msvc/libusb_static_2012.vcxproj index 4323d04..a8a6bdc 100644 --- a/msvc/libusb_static_2012.vcxproj +++ b/msvc/libusb_static_2012.vcxproj @@ -147,6 +147,7 @@ + diff --git a/msvc/libusb_static_2012.vcxproj.filters b/msvc/libusb_static_2012.vcxproj.filters index 8585fcf..39dc64e 100644 --- a/msvc/libusb_static_2012.vcxproj.filters +++ b/msvc/libusb_static_2012.vcxproj.filters @@ -52,5 +52,8 @@ Header Files + + Header Files + \ No newline at end of file -- cgit v1.2.1