]> git.cworth.org Git - apitrace/blob - windows.py
Basic Linux/GLX tracing support.
[apitrace] / windows.py
1 ##########################################################################
2 #
3 # Copyright 2008-2009 VMware, Inc.
4 # All Rights Reserved.
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24 ##########################################################################/
25
26 """windows.h"""
27
28 from base import *
29
30 SHORT = Intrinsic("SHORT", "%i")
31 USHORT = Intrinsic("USHORT", "%u")
32 INT = Intrinsic("INT", "%i")
33 UINT = Intrinsic("UINT", "%u")
34 LONG = Intrinsic("LONG", "%li")
35 ULONG = Intrinsic("ULONG", "%lu")
36 FLOAT = Intrinsic("FLOAT", "%f")
37
38 INT32 = Intrinsic("INT32", "%i")
39 UINT32 = Intrinsic("UINT32", "%i")
40
41 BYTE = Intrinsic("BYTE", "0x%02lx")
42 WORD = Intrinsic("WORD", "0x%04lx")
43 DWORD = Intrinsic("DWORD", "0x%08lx")
44
45 BOOL = Intrinsic("BOOL", "%i")
46
47 LPLONG = Pointer(LONG)
48 LPWORD = Pointer(WORD)
49 LPDWORD = Pointer(DWORD)
50 LPBOOL = Pointer(BOOL)
51 LPSIZE = LPDWORD
52
53 LPSTR = String
54 LPCSTR = Const(String)
55 LPWSTR = WString
56 LPCWSTR = Const(WString)
57
58 LARGE_INTEGER = Intrinsic("LARGE_INTEGER", "0x%llx")
59 SIZE_T = Alias("SIZE_T", SizeT)
60
61 HRESULT = Alias("HRESULT", Int)
62
63 PVOID = Intrinsic("PVOID", "%p")
64 LPVOID = PVOID
65 HANDLE = Intrinsic("HANDLE", "%p")
66 HWND = Intrinsic("HWND", "%p")
67 HDC = Intrinsic("HDC", "%p")
68 HMONITOR = Intrinsic("HMONITOR", "%p")
69
70 GUID = Struct("GUID", [
71     (DWORD, "Data1"),
72     (WORD, "Data2"),
73     (WORD, "Data3"),
74     (BYTE, "Data4[0]"),
75     (BYTE, "Data4[1]"),
76     (BYTE, "Data4[2]"),
77     (BYTE, "Data4[3]"),
78     (BYTE, "Data4[4]"),
79     (BYTE, "Data4[5]"),
80     (BYTE, "Data4[6]"),
81     (BYTE, "Data4[7]"),
82 ])
83 LPGUID = Pointer(GUID)
84
85 #REFGUID = Alias("REFGUID", Pointer(GUID))
86 REFGUID = Alias("REFGUID", GUID)
87
88 IID = Alias("IID", GUID)
89 #REFIID = Alias("REFIID", Pointer(IID))
90 REFIID = Alias("REFIID", IID)
91
92 CLSID = Alias("CLSID", GUID)
93 #REFCLSID = Alias("REFCLSID", Pointer(CLSID))
94 REFCLSID = Alias("REFCLSID", CLSID)
95
96 LUID = Struct("LUID", [
97     (DWORD, "LowPart"),
98     (LONG, "HighPart"),
99 ])
100
101 POINT = Struct("POINT", (
102   (LONG, "x"),
103   (LONG, "y"),
104 )) 
105 LPPOINT = Pointer(POINT)
106
107 RECT = Struct("RECT", (
108   (LONG, "left"),
109   (LONG, "top"),
110   (LONG, "right"), 
111   (LONG, "bottom"), 
112 )) 
113 LPRECT = Pointer(RECT)
114
115 PALETTEENTRY = Struct("PALETTEENTRY", (
116   (BYTE, "peRed"),
117   (BYTE, "peGreen"),
118   (BYTE, "peBlue"), 
119   (BYTE, "peFlags"), 
120 )) 
121 LPPALETTEENTRY = Pointer(PALETTEENTRY)
122
123
124 RGNDATAHEADER = Struct("RGNDATAHEADER", [
125     (DWORD, "dwSize"),
126     (DWORD, "iType"),
127     (DWORD, "nCount"),
128     (DWORD, "nRgnSize"),
129     (RECT, "rcBound"),
130 ])
131
132 RGNDATA = Struct("RGNDATA", [
133     (RGNDATAHEADER, "rdh"),
134     #(Char, "Buffer[1]"),
135 ])
136 LPRGNDATA = Pointer(RGNDATA)
137
138 HMODULE = Alias("HMODULE", LPVOID)
139
140 IUnknown = Interface("IUnknown")
141
142 IUnknown.methods = (
143         Method(HRESULT, "QueryInterface", ((REFIID, "riid"), (Pointer(Pointer(Void)), "ppvObj"))),
144         Method(ULONG, "AddRef", ()),
145         Method(ULONG, "Release", ()),
146 )
147
148
149 class DllFunction(Function):
150
151     def get_true_pointer(self):
152         ptype = self.pointer_type()
153         pvalue = self.pointer_value()
154         print '    if(!g_hDll) {'
155         print '        g_hDll = LoadLibrary(g_szDll);'
156         print '        if(!g_hDll)'
157         self.fail_impl()
158         print '    }'
159         print '    if(!%s) {' % (pvalue,)
160         print '        %s = (%s)GetProcAddress(g_hDll, "%s");' % (pvalue, ptype, self.name)
161         print '        if(!%s)' % (pvalue,)
162         self.fail_impl()
163         print '    }'
164
165
166 class Dll:
167
168     def __init__(self, name):
169         self.name = name
170         self.functions = []
171         if self not in towrap:
172             towrap.append(self)
173
174     def wrap_name(self):
175         return "Wrap" + self.name
176
177     def wrap_pre_decl(self):
178         pass
179
180     def wrap_decl(self):
181         print 'static HINSTANCE g_hDll = NULL;'
182         print 'static TCHAR g_szDll[MAX_PATH] = {0};'
183         print
184         print 'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);'
185         print
186         for function in self.functions:
187             function.wrap_decl()
188         print
189
190     def wrap_impl(self):
191         print r'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {'
192         print r'    switch(fdwReason) {'
193         print r'    case DLL_PROCESS_ATTACH:'
194         print r'        if(!GetSystemDirectory(g_szDll, MAX_PATH))'
195         print r'            return FALSE;'
196         print r'        _tcscat(g_szDll, TEXT("\\%s.dll"));' % self.name
197         print r'        Log::Open("%s");' % self.name
198         print r'    case DLL_THREAD_ATTACH:'
199         print r'        return TRUE;'
200         print r'    case DLL_THREAD_DETACH:'
201         print r'        return TRUE;'
202         print r'    case DLL_PROCESS_DETACH:'
203         print r'        Log::Close();'
204         print r'        if(g_hDll) {'
205         print r'            FreeLibrary(g_hDll);'
206         print r'            g_hDll = NULL;'
207         print r'        }'
208         print r'        return TRUE;'
209         print r'    }'
210         print r'    (void)hinstDLL;'
211         print r'    (void)lpvReserved;'
212         print r'    return TRUE;'
213         print r'}'
214         print
215         for function in self.functions:
216             function.wrap_impl()
217         print
218