Send message for WSAAsyncSelect sockets directly from the server,
[wine] / win32 / newfns.c
1 /*
2  * Win32 miscellaneous functions
3  *
4  * Copyright 1995 Thomas Sandford (tdgsandf@prds-grn.demon.co.uk)
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 2.1 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, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 /* Misc. new functions - they should be moved into appropriate files
22 at a later date. */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/time.h>
27 #include <unistd.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winnls.h"
31 #include "winerror.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(win32);
35 WINE_DECLARE_DEBUG_CHANNEL(debug);
36
37
38 static BOOL     QUERYPERF_Initialized           = 0;
39 #if defined(__i386__) && defined(__GNUC__)
40 static BOOL     QUERYPERF_RDTSC_Use             = 0;
41 static LONGLONG QUERYPERF_RDTSC_Frequency       = 0;
42 #endif
43
44 static void QUERYPERF_Init(void)
45 {
46 #if defined(__i386__) && defined(__GNUC__)
47     /* We are running on i386 and compiling on GCC.
48      * Do a runtime check to see if we have the rdtsc instruction available
49      */
50     FILE                *fp;
51     char                line[256], *s, *value;
52     double              cpuMHz;
53
54     TRACE("()\n");
55     
56     if (IsProcessorFeaturePresent( PF_RDTSC_INSTRUCTION_AVAILABLE ))
57     {
58         /* rdtsc is available.  However, in order to use it
59          * we also need to be able to get the processor's
60          * speed.  Currently we do this by reading /proc/cpuinfo
61          * which makes it Linux-specific.
62          */
63
64         TRACE("rdtsc available\n");
65
66         fp = fopen( "/proc/cpuinfo", "r" );
67         if (fp)
68         {
69             while(fgets( line, sizeof(line), fp ))
70             {
71                 /* NOTE: the ':' is the only character we can rely on */
72                 if (!(value = strchr( line, ':' )))
73                     continue;
74
75                 /* terminate the valuename */
76                 *value++ = '\0';
77                 /* skip any leading spaces */
78                 while (*value == ' ') value++;
79                 if ((s = strchr( value, '\n' )))
80                     *s = '\0';
81
82                 if (!strncasecmp( line, "cpu MHz", strlen( "cpu MHz" ) ))
83                 {
84                     if (sscanf( value, "%lf", &cpuMHz ) == 1)
85                     {
86                         QUERYPERF_RDTSC_Frequency = (LONGLONG)(cpuMHz * 1000000.0);
87                         QUERYPERF_RDTSC_Use = TRUE;
88                         TRACE("using frequency: %lldHz\n", QUERYPERF_RDTSC_Frequency);
89                         break;
90                     }
91                 }
92             }
93             fclose(fp);
94         }
95     }
96 #endif
97     QUERYPERF_Initialized = TRUE;
98 }
99
100                     
101 /****************************************************************************
102  *              QueryPerformanceCounter (KERNEL32.@)
103  */
104 BOOL WINAPI QueryPerformanceCounter(PLARGE_INTEGER counter)
105 {
106     struct timeval tv;
107
108     if (!QUERYPERF_Initialized)
109         QUERYPERF_Init();
110
111 #if defined(__i386__) && defined(__GNUC__)
112     if (QUERYPERF_RDTSC_Use)
113     {
114         /* i586 optimized version */
115         __asm__ __volatile__ ( "rdtsc"
116                                : "=a" (counter->s.LowPart), "=d" (counter->s.HighPart) );
117         return TRUE;
118     }
119     /* fall back to generic routine (ie, for i386, i486) */
120 #endif
121
122     /* generic routine */
123     gettimeofday( &tv, NULL );
124     counter->QuadPart = (LONGLONG)tv.tv_usec + (LONGLONG)tv.tv_sec * 1000000LL;
125     return TRUE;
126 }
127
128 /****************************************************************************
129  *              QueryPerformanceFrequency (KERNEL32.@)
130  */
131 BOOL WINAPI QueryPerformanceFrequency(PLARGE_INTEGER frequency)
132 {
133     if (!QUERYPERF_Initialized)
134         QUERYPERF_Init();
135
136 #if defined(__i386__) && defined(__GNUC__)
137     if (QUERYPERF_RDTSC_Use)
138     {
139         frequency->QuadPart = QUERYPERF_RDTSC_Frequency;
140         return TRUE;
141     }
142 #endif
143     
144     frequency->s.LowPart        = 1000000;
145     frequency->s.HighPart       = 0;
146     return TRUE;
147 }
148
149 /****************************************************************************
150  *              FlushInstructionCache (KERNEL32.@)
151  */
152 BOOL WINAPI FlushInstructionCache(DWORD x,DWORD y,DWORD z) {
153         if (GetVersion() & 0x80000000) return TRUE; /* not NT, always TRUE */
154         FIXME_(debug)("(0x%08lx,0x%08lx,0x%08lx): stub\n",x,y,z);
155         return TRUE;
156 }
157
158 /***********************************************************************
159  *           GetSystemPowerStatus      (KERNEL32.@)
160  */
161 BOOL WINAPI GetSystemPowerStatus(LPSYSTEM_POWER_STATUS sps_ptr)
162 {
163     return FALSE;   /* no power management support */
164 }
165
166
167 /***********************************************************************
168  *           SetSystemPowerState      (KERNEL32.@)
169  */
170 BOOL WINAPI SetSystemPowerState(BOOL suspend_or_hibernate,
171                                   BOOL force_flag)
172 {
173     /* suspend_or_hibernate flag: w95 does not support
174        this feature anyway */
175
176     for ( ;0; )
177     {
178         if ( force_flag )
179         {
180         }
181         else
182         {
183         }
184     }
185     return TRUE;
186 }
187
188
189 /******************************************************************************
190  * CreateMailslotA [KERNEL32.@]
191  */
192 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
193                                    DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa)
194 {
195     FIXME("(%s,%ld,%ld,%p): stub\n", debugstr_a(lpName),
196           nMaxMessageSize, lReadTimeout, sa);
197     return 1;
198 }
199
200
201 /******************************************************************************
202  * CreateMailslotW [KERNEL32.@]  Creates a mailslot with specified name
203  * 
204  * PARAMS
205  *    lpName          [I] Pointer to string for mailslot name
206  *    nMaxMessageSize [I] Maximum message size
207  *    lReadTimeout    [I] Milliseconds before read time-out
208  *    sa              [I] Pointer to security structure
209  *
210  * RETURNS
211  *    Success: Handle to mailslot
212  *    Failure: INVALID_HANDLE_VALUE
213  */
214 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
215                                    DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
216 {
217     FIXME("(%s,%ld,%ld,%p): stub\n", debugstr_w(lpName), 
218           nMaxMessageSize, lReadTimeout, sa);
219     return 1;
220 }
221
222
223 /******************************************************************************
224  * GetMailslotInfo [KERNEL32.@]  Retrieves info about specified mailslot
225  *
226  * PARAMS
227  *    hMailslot        [I] Mailslot handle
228  *    lpMaxMessageSize [O] Address of maximum message size
229  *    lpNextSize       [O] Address of size of next message
230  *    lpMessageCount   [O] Address of number of messages
231  *    lpReadTimeout    [O] Address of read time-out
232  * 
233  * RETURNS
234  *    Success: TRUE
235  *    Failure: FALSE
236  */
237 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
238                                LPDWORD lpNextSize, LPDWORD lpMessageCount,
239                                LPDWORD lpReadTimeout )
240 {
241     FIXME("(%04x): stub\n",hMailslot);
242     if (lpMaxMessageSize) *lpMaxMessageSize = (DWORD)NULL;
243     if (lpNextSize) *lpNextSize = (DWORD)NULL;
244     if (lpMessageCount) *lpMessageCount = (DWORD)NULL;
245     if (lpReadTimeout) *lpReadTimeout = (DWORD)NULL;
246     return TRUE;
247 }
248
249
250 /******************************************************************************
251  * GetCompressedFileSizeA [KERNEL32.@]
252  *
253  * NOTES
254  *    This should call the W function below
255  */
256 DWORD WINAPI GetCompressedFileSizeA(
257     LPCSTR lpFileName,
258     LPDWORD lpFileSizeHigh)
259 {
260     FIXME("(...): stub\n");
261     return 0xffffffff;
262 }
263
264
265 /******************************************************************************
266  * GetCompressedFileSizeW [KERNEL32.@]  
267  * 
268  * RETURNS
269  *    Success: Low-order doubleword of number of bytes
270  *    Failure: 0xffffffff
271  */
272 DWORD WINAPI GetCompressedFileSizeW(
273     LPCWSTR lpFileName,     /* [in]  Pointer to name of file */
274     LPDWORD lpFileSizeHigh) /* [out] Receives high-order doubleword of size */
275 {
276     FIXME("(%s,%p): stub\n",debugstr_w(lpFileName),lpFileSizeHigh);
277     return 0xffffffff;
278 }
279
280
281 /******************************************************************************
282  * SetComputerNameA [KERNEL32.@]  
283  */
284 BOOL WINAPI SetComputerNameA( LPCSTR lpComputerName )
285 {
286     BOOL ret;
287     DWORD len = MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, NULL, 0 );
288     LPWSTR nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
289
290     MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, nameW, len );
291     ret = SetComputerNameW( nameW );
292     HeapFree( GetProcessHeap(), 0, nameW );
293     return ret;
294 }
295
296
297 /******************************************************************************
298  * SetComputerNameW [KERNEL32.@]
299  *
300  * PARAMS
301  *    lpComputerName [I] Address of new computer name
302  * 
303  * RETURNS STD
304  */
305 BOOL WINAPI SetComputerNameW( LPCWSTR lpComputerName )
306 {
307     FIXME("(%s): stub\n", debugstr_w(lpComputerName));
308     return TRUE;
309 }
310
311 /******************************************************************************
312  *              CreateIoCompletionPort (KERNEL32.@)
313  */
314 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle,
315 HANDLE hExistingCompletionPort, DWORD dwCompletionKey,
316 DWORD dwNumberOfConcurrentThreads)
317 {
318     FIXME("(%04x, %04x, %08lx, %08lx): stub.\n", hFileHandle, hExistingCompletionPort, dwCompletionKey, dwNumberOfConcurrentThreads);
319     return (HANDLE)NULL;
320 }
321
322 /******************************************************************************
323  *              GetQueuedCompletionStatus (KERNEL32.@)
324  */
325 BOOL WINAPI GetQueuedCompletionStatus(
326     HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
327     LPDWORD lpCompletionKey, LPOVERLAPPED *lpOverlapped, DWORD dwMilliseconds
328 ) {
329     FIXME("(%x,%p,%p,%p,%ld), stub!\n",CompletionPort,lpNumberOfBytesTransferred,lpCompletionKey,lpOverlapped,dwMilliseconds);
330     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
331     return FALSE;
332 }