ddraw: Y coords are inversed.
[wine] / dlls / pdh / pdh_main.c
1 /*
2  * Performance Data Helper (pdh.dll)
3  *
4  * Copyright 2007 Andrey Turkin
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
25 #include "windef.h"
26 #include "winbase.h"
27
28 #include "pdh.h"
29 #include "pdhmsg.h"
30 #include "winperf.h"
31
32 #include "wine/debug.h"
33 #include "wine/list.h"
34 #include "wine/unicode.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(pdh);
37
38 static inline void *pdh_alloc( SIZE_T size )
39 {
40     return HeapAlloc( GetProcessHeap(), 0, size );
41 }
42
43 static inline void *pdh_alloc_zero( SIZE_T size )
44 {
45     return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size );
46 }
47
48 static inline void pdh_free( LPVOID mem )
49 {
50     HeapFree( GetProcessHeap(), 0, mem );
51 }
52
53 static inline WCHAR *pdh_strdup( const WCHAR *src )
54 {
55     WCHAR *dst;
56
57     if (!src) return NULL;
58     if ((dst = pdh_alloc( (strlenW( src ) + 1) * sizeof(WCHAR) ))) strcpyW( dst, src );
59     return dst;
60 }
61
62 static inline WCHAR *pdh_strdup_aw( const char *src )
63 {
64     int len;
65     WCHAR *dst;
66
67     if (!src) return NULL;
68     len = MultiByteToWideChar( CP_ACP, 0, src, -1, NULL, 0 );
69     if ((dst = pdh_alloc( len * sizeof(WCHAR) ))) MultiByteToWideChar( CP_ACP, 0, src, -1, dst, len );
70     return dst;
71 }
72
73 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
74 {
75     TRACE("(0x%p, %d, %p)\n",hinstDLL,fdwReason,lpvReserved);
76
77     if (fdwReason == DLL_WINE_PREATTACH) return FALSE;    /* prefer native version */
78
79     if (fdwReason == DLL_PROCESS_ATTACH)
80     {
81         DisableThreadLibraryCalls( hinstDLL );
82     }
83
84     return TRUE;
85 }
86
87 struct counter
88 {
89     struct list     entry;
90     WCHAR          *path;                           /* identifier */
91     DWORD           type;                           /* counter type */
92     DWORD           status;                         /* update status */
93     LONG            scale;                          /* scale factor */
94     LONG            defaultscale;                   /* default scale factor */
95     DWORD_PTR       user;                           /* user data */
96     DWORD_PTR       queryuser;                      /* query user data */
97     LONGLONG        base;                           /* samples per second */
98     FILETIME        stamp;                          /* time stamp */
99     void (CALLBACK *collect)( struct counter * );   /* collect callback */
100     union
101     {
102         LONG     longvalue;
103         double   doublevalue;
104         LONGLONG largevalue;
105     } one;                                          /* first value */
106     union
107     {
108         LONG     longvalue;
109         double   doublevalue;
110         LONGLONG largevalue;
111     } two;                                          /* second value */
112 };
113
114 static struct counter *create_counter( void )
115 {
116     struct counter *counter;
117
118     if ((counter = pdh_alloc_zero( sizeof(struct counter) ))) return counter;
119     return NULL;
120 }
121
122 #define PDH_MAGIC_QUERY     0x50444830 /* 'PDH0' */
123
124 struct query
125 {
126     DWORD       magic;      /* signature */
127     DWORD_PTR   user;       /* user data */
128     struct list counters;   /* counter list */
129 };
130
131 static struct query *create_query( void )
132 {
133     struct query *query;
134
135     if ((query = pdh_alloc_zero( sizeof(struct query) )))
136     {
137         query->magic = PDH_MAGIC_QUERY;
138         list_init( &query->counters );
139         return query;
140     }
141     return NULL;
142 }
143
144 struct source
145 {
146     const WCHAR    *path;                           /* identifier */
147     void (CALLBACK *collect)( struct counter * );   /* collect callback */
148     DWORD           type;                           /* counter type */
149     LONG            scale;                          /* default scale factor */
150     LONGLONG        base;                           /* samples per second */
151 };
152
153 /* counter source registry */
154 static const struct source counter_sources[] =
155 {
156     { NULL, NULL, 0, 0, 0 }
157 };
158
159 /***********************************************************************
160  *              PdhAddCounterA   (PDH.@)
161  */
162 PDH_STATUS WINAPI PdhAddCounterA( PDH_HQUERY query, LPCSTR path,
163                                   DWORD_PTR userdata, PDH_HCOUNTER *counter )
164 {
165     PDH_STATUS ret;
166     WCHAR *pathW;
167
168     TRACE("%p %s %lx %p\n", query, debugstr_a(path), userdata, counter);
169
170     if (!path) return PDH_INVALID_ARGUMENT;
171
172     if (!(pathW = pdh_strdup_aw( path )))
173         return PDH_MEMORY_ALLOCATION_FAILURE;
174
175     ret = PdhAddCounterW( query, pathW, userdata, counter );
176
177     pdh_free( pathW );
178     return ret;
179 }
180
181 /***********************************************************************
182  *              PdhAddCounterW   (PDH.@)
183  */
184 PDH_STATUS WINAPI PdhAddCounterW( PDH_HQUERY hquery, LPCWSTR path,
185                                   DWORD_PTR userdata, PDH_HCOUNTER *hcounter )
186 {
187     struct query *query = hquery;
188     struct counter *counter;
189     unsigned int i;
190
191     TRACE("%p %s %lx %p\n", hquery, debugstr_w(path), userdata, hcounter);
192
193     if (!path  || !hcounter) return PDH_INVALID_ARGUMENT;
194     if (!query || (query->magic != PDH_MAGIC_QUERY)) return PDH_INVALID_HANDLE;
195
196     *hcounter = NULL;
197     for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
198     {
199         if (strstrW( path, counter_sources[i].path ))
200         {
201             if ((counter = create_counter()))
202             {
203                 counter->path         = pdh_strdup( counter_sources[i].path );
204                 counter->collect      = counter_sources[i].collect;
205                 counter->type         = counter_sources[i].type;
206                 counter->defaultscale = counter_sources[i].scale;
207                 counter->base         = counter_sources[i].base;
208                 counter->queryuser    = query->user;
209                 counter->user         = userdata;
210
211                 list_add_tail( &query->counters, &counter->entry );
212
213                 *hcounter = counter;
214                 return ERROR_SUCCESS;
215             }
216             return PDH_MEMORY_ALLOCATION_FAILURE;
217         }
218     }
219     return PDH_CSTATUS_NO_COUNTER;
220 }
221
222 /***********************************************************************
223  *              PdhCloseQuery   (PDH.@)
224  */
225 PDH_STATUS WINAPI PdhCloseQuery( PDH_HQUERY handle )
226 {
227     struct query *query = handle;
228     struct list *item;
229
230     TRACE("%p\n", handle);
231
232     if (!query || (query->magic != PDH_MAGIC_QUERY)) return PDH_INVALID_HANDLE;
233
234     LIST_FOR_EACH( item, &query->counters )
235     {
236         struct counter *counter = LIST_ENTRY( item, struct counter, entry );
237
238         list_remove( &counter->entry );
239
240         pdh_free( counter->path );
241         pdh_free( counter );
242     }
243
244     pdh_free( query );
245     return ERROR_SUCCESS;
246 }
247
248 /***********************************************************************
249  *              PdhCollectQueryData   (PDH.@)
250  */
251 PDH_STATUS WINAPI PdhCollectQueryData( PDH_HQUERY handle )
252 {
253     struct query *query = handle;
254     struct list *item;
255
256     TRACE("%p\n", handle);
257
258     if (!query || (query->magic != PDH_MAGIC_QUERY)) return PDH_INVALID_HANDLE;
259
260     LIST_FOR_EACH( item, &query->counters )
261     {
262         SYSTEMTIME time;
263         struct counter *counter = LIST_ENTRY( item, struct counter, entry );
264
265         counter->collect( counter );
266
267         GetLocalTime( &time );
268         SystemTimeToFileTime( &time, &counter->stamp );
269     }
270     return ERROR_SUCCESS;
271 }
272
273 /***********************************************************************
274  *              PdhOpenQueryA   (PDH.@)
275  */
276 PDH_STATUS WINAPI PdhOpenQueryA( LPCSTR source, DWORD_PTR userdata, PDH_HQUERY *query )
277 {
278     PDH_STATUS ret;
279     WCHAR *sourceW = NULL;
280
281     TRACE("%s %lx %p\n", debugstr_a(source), userdata, query);
282
283     if (source && !(sourceW = pdh_strdup_aw( source ))) return PDH_MEMORY_ALLOCATION_FAILURE;
284
285     ret = PdhOpenQueryW( sourceW, userdata, query );
286     pdh_free( sourceW );
287
288     return ret;
289 }
290
291 /***********************************************************************
292  *              PdhOpenQueryW   (PDH.@)
293  */
294 PDH_STATUS WINAPI PdhOpenQueryW( LPCWSTR source, DWORD_PTR userdata, PDH_HQUERY *handle )
295 {
296     struct query *query;
297
298     TRACE("%s %lx %p\n", debugstr_w(source), userdata, handle);
299
300     if (!handle) return PDH_INVALID_ARGUMENT;
301
302     if (source)
303     {
304         FIXME("log file data source not supported\n");
305         return PDH_INVALID_ARGUMENT;
306     }
307     if ((query = create_query()))
308     {
309         query->user = userdata;
310         *handle = query;
311
312         return ERROR_SUCCESS;
313     }
314     return PDH_MEMORY_ALLOCATION_FAILURE;
315 }
316
317 /***********************************************************************
318  *              PdhRemoveCounter   (PDH.@)
319  */
320 PDH_STATUS WINAPI PdhRemoveCounter( PDH_HCOUNTER handle )
321 {
322     struct counter *counter = handle;
323
324     TRACE("%p\n", handle);
325
326     if (!counter) return PDH_INVALID_HANDLE;
327
328     list_remove( &counter->entry );
329
330     pdh_free( counter->path );
331     pdh_free( counter );
332
333     return ERROR_SUCCESS;
334 }