Merge pull request #4324 from FernetMenta/wasapi
[vuplus_xbmc] / lib / libhdhomerun / hdhomerun_os_windows.h
1 /*
2  * hdhomerun_os_windows.h
3  *
4  * Copyright © 2006-2008 Silicondust Engineering Ltd. <www.silicondust.com>.
5  *
6  * This library is free software; you can redistribute it and/or 
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 3 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
18  * 
19  * As a special exception to the GNU Lesser General Public License,
20  * you may link, statically or dynamically, an application with a
21  * publicly distributed version of the Library to produce an
22  * executable file containing portions of the Library, and
23  * distribute that executable file under terms of your choice,
24  * without any of the additional requirements listed in clause 4 of
25  * the GNU Lesser General Public License.
26  * 
27  * By "a publicly distributed version of the Library", we mean
28  * either the unmodified Library as distributed by Silicondust, or a
29  * modified version of the Library that is distributed under the
30  * conditions defined in the GNU Lesser General Public License.
31  */
32
33 #define _WINSOCKAPI_
34 #include <windows.h>
35 #include <winsock2.h>
36 #include <ws2tcpip.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <stdarg.h>
40 #include <string.h>
41 #include <signal.h>
42 #include <time.h>
43 #include <sys/types.h>
44 #include <sys/timeb.h>
45
46 #ifdef __MINGW__
47 extern 
48 int WSAAPI getaddrinfo(
49   const char *nodename,
50   const char *servname,
51   const struct addrinfo *hints,
52   struct addrinfo **res
53 );
54
55 extern
56 void WSAAPI freeaddrinfo(
57   struct addrinfo *ai
58 );
59 #else
60 #include <wspiapi.h>
61 #endif
62
63
64 #if defined(DLL_EXPORT)
65 #define LIBTYPE __declspec( dllexport )
66 #elif  defined(DLL_IMPORT)
67 #define LIBTYPE __declspec( dllimport )
68 #else
69 #define LIBTYPE
70 #endif
71
72 typedef int bool_t;
73 typedef signed __int8 int8_t;
74 typedef signed __int16 int16_t;
75 typedef signed __int32 int32_t;
76 typedef signed __int64 int64_t;
77 typedef unsigned __int8 uint8_t;
78 typedef unsigned __int16 uint16_t;
79 typedef unsigned __int32 uint32_t;
80 typedef unsigned __int64 uint64_t;
81 typedef HANDLE pthread_t;
82 typedef HANDLE pthread_mutex_t;
83
84 #define socklen_t int
85 #define close closesocket
86 #define sock_getlasterror WSAGetLastError()
87 #define sock_getlasterror_socktimeout (WSAGetLastError() == WSAETIMEDOUT)
88 #ifndef va_copy
89 #define va_copy(x, y) x = y
90 #endif
91 #define atoll _atoi64
92 #define strdup _strdup
93 #ifndef strcasecmp
94 #define strcasecmp _stricmp
95 #endif
96 #define snprintf _snprintf
97 #define fseeko _fseeki64
98 #define ftello _ftelli64
99 #define THREAD_FUNC_PREFIX DWORD WINAPI
100 #define SIGPIPE SIGABRT
101
102 static inline int msleep(unsigned int ms)
103 {
104         Sleep(ms);
105         return 0;
106 }
107
108 static inline int sleep(unsigned int sec)
109 {
110         Sleep(sec * 1000);
111         return 0;
112 }
113
114 static inline uint64_t getcurrenttime(void)
115 {
116         struct timeb tb;
117         ftime(&tb);
118         return ((uint64_t)tb.time * 1000) + tb.millitm;
119 }
120
121 static inline int setsocktimeout(int s, int level, int optname, uint64_t timeout)
122 {
123         int t = (int)timeout;
124         return setsockopt(s, level, optname, (char *)&t, sizeof(t));
125 }
126
127 static inline int pthread_create(pthread_t *tid, void *attr, LPTHREAD_START_ROUTINE start, void *arg)
128 {
129         *tid = CreateThread(NULL, 0, start, arg, 0, NULL);
130         if (!*tid) {
131                 return (int)GetLastError();
132         }
133         return 0;
134 }
135
136 static inline int pthread_join(pthread_t tid, void **value_ptr)
137 {
138         while (1) {
139                 DWORD ExitCode = 0;
140                 if (!GetExitCodeThread(tid, &ExitCode)) {
141                         return (int)GetLastError();
142                 }
143                 if (ExitCode != STILL_ACTIVE) {
144                         return 0;
145                 }
146         }
147 }
148
149 static inline void pthread_mutex_init(pthread_mutex_t *mutex, void *attr)
150 {
151         *mutex = CreateMutex(NULL, FALSE, NULL);
152 }
153
154 static inline void pthread_mutex_lock(pthread_mutex_t *mutex)
155 {
156         WaitForSingleObject(*mutex, INFINITE);
157 }
158
159 static inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
160 {
161         ReleaseMutex(*mutex);
162 }
163
164 /*
165  * The console output format should be set to UTF-8, however in XP and Vista this breaks batch file processing.
166  * Attempting to restore on exit fails to restore if the program is terminated by the user.
167  * Solution - set the output format each printf.
168  */
169 static inline void console_vprintf(const char *fmt, va_list ap)
170 {
171         UINT cp = GetConsoleOutputCP();
172         SetConsoleOutputCP(CP_UTF8);
173         vprintf(fmt, ap);
174         SetConsoleOutputCP(cp);
175 }
176
177 static inline void console_printf(const char *fmt, ...)
178 {
179         va_list ap;
180         va_start(ap, fmt);
181         console_vprintf(fmt, ap);
182         va_end(ap);
183 }