]> git.cworth.org Git - apitrace/blob - windows.py
Cleanup generated log code.
[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 = Alias("SHORT", Short)
31 USHORT = Alias("USHORT", UShort)
32 INT = Alias("INT", Int)
33 UINT = Alias("UINT", UInt)
34 LONG = Alias("LONG", Long)
35 ULONG = Alias("ULONG", ULong)
36 LONGLONG = Alias("LONGLONG", LongLong)
37 FLOAT = Alias("FLOAT", Float)
38
39 INT32 = Literal("INT32", "UInt")
40 UINT32 = Literal("UINT32", "UInt")
41
42 BYTE = Literal("BYTE", "UInt", base=16)
43 WORD = Literal("WORD", "UInt", base=16)
44 DWORD = Literal("DWORD", "UInt", base=16)
45
46 BOOL = Alias("BOOL", Bool)
47
48 LPLONG = Pointer(LONG)
49 LPWORD = Pointer(WORD)
50 LPDWORD = Pointer(DWORD)
51 LPBOOL = Pointer(BOOL)
52 LPSIZE = LPDWORD
53
54 LPSTR = String
55 LPCSTR = Const(String)
56 LPWSTR = WString
57 LPCWSTR = Const(WString)
58
59 LARGE_INTEGER = Struct("LARGE_INTEGER", [
60     (LONGLONG, 'QuadPart'),
61 ])
62
63 SIZE_T = Alias("SIZE_T", SizeT)
64
65 HRESULT = Alias("HRESULT", Int)
66
67 PVOID = Opaque("PVOID")
68 LPVOID = PVOID
69 HANDLE = Opaque("HANDLE")
70 HWND = Opaque("HWND")
71 HDC = Opaque("HDC")
72 HMONITOR = Opaque("HMONITOR")
73
74 GUID = Struct("GUID", [
75     (DWORD, "Data1"),
76     (WORD, "Data2"),
77     (WORD, "Data3"),
78     (BYTE, "Data4[0]"),
79     (BYTE, "Data4[1]"),
80     (BYTE, "Data4[2]"),
81     (BYTE, "Data4[3]"),
82     (BYTE, "Data4[4]"),
83     (BYTE, "Data4[5]"),
84     (BYTE, "Data4[6]"),
85     (BYTE, "Data4[7]"),
86 ])
87 LPGUID = Pointer(GUID)
88
89 #REFGUID = Alias("REFGUID", Pointer(GUID))
90 REFGUID = Alias("REFGUID", GUID)
91
92 IID = Alias("IID", GUID)
93 #REFIID = Alias("REFIID", Pointer(IID))
94 REFIID = Alias("REFIID", IID)
95
96 CLSID = Alias("CLSID", GUID)
97 #REFCLSID = Alias("REFCLSID", Pointer(CLSID))
98 REFCLSID = Alias("REFCLSID", CLSID)
99
100 LUID = Struct("LUID", [
101     (DWORD, "LowPart"),
102     (LONG, "HighPart"),
103 ])
104
105 POINT = Struct("POINT", (
106   (LONG, "x"),
107   (LONG, "y"),
108 )) 
109 LPPOINT = Pointer(POINT)
110
111 RECT = Struct("RECT", (
112   (LONG, "left"),
113   (LONG, "top"),
114   (LONG, "right"), 
115   (LONG, "bottom"), 
116 )) 
117 LPRECT = Pointer(RECT)
118
119 PALETTEENTRY = Struct("PALETTEENTRY", (
120   (BYTE, "peRed"),
121   (BYTE, "peGreen"),
122   (BYTE, "peBlue"), 
123   (BYTE, "peFlags"), 
124 )) 
125 LPPALETTEENTRY = Pointer(PALETTEENTRY)
126
127
128 RGNDATAHEADER = Struct("RGNDATAHEADER", [
129     (DWORD, "dwSize"),
130     (DWORD, "iType"),
131     (DWORD, "nCount"),
132     (DWORD, "nRgnSize"),
133     (RECT, "rcBound"),
134 ])
135
136 RGNDATA = Struct("RGNDATA", [
137     (RGNDATAHEADER, "rdh"),
138     #(Char, "Buffer[1]"),
139 ])
140 LPRGNDATA = Pointer(RGNDATA)
141
142 HMODULE = Opaque("HMODULE")
143
144 IUnknown = Interface("IUnknown")
145
146 IUnknown.methods = (
147         Method(HRESULT, "QueryInterface", ((REFIID, "riid"), (Pointer(OpaquePointer(Void)), "ppvObj"))),
148         Method(ULONG, "AddRef", ()),
149         Method(ULONG, "Release", ()),
150 )
151
152
153 class DllFunction(Function):
154
155     def get_true_pointer(self):
156         ptype = self.pointer_type()
157         pvalue = self.pointer_value()
158         print '    if(!g_hDll) {'
159         print '        g_hDll = LoadLibrary(g_szDll);'
160         print '        if(!g_hDll)'
161         self.fail_impl()
162         print '    }'
163         print '    if(!%s) {' % (pvalue,)
164         print '        %s = (%s)GetProcAddress(g_hDll, "%s");' % (pvalue, ptype, self.name)
165         print '        if(!%s)' % (pvalue,)
166         self.fail_impl()
167         print '    }'
168
169
170 class Dll:
171
172     def __init__(self, name):
173         self.name = name
174         self.functions = []
175         if self not in towrap:
176             towrap.append(self)
177
178     def wrap_name(self):
179         return "Wrap" + self.name
180
181     def wrap_pre_decl(self):
182         pass
183
184     def wrap_decl(self):
185         print 'static HINSTANCE g_hDll = NULL;'
186         print 'static TCHAR g_szDll[MAX_PATH] = {0};'
187         print
188         print 'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);'
189         print
190         for function in self.functions:
191             function.wrap_decl()
192         print
193
194     def wrap_impl(self):
195         print r'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {'
196         print r'    switch(fdwReason) {'
197         print r'    case DLL_PROCESS_ATTACH:'
198         print r'        if(!GetSystemDirectory(g_szDll, MAX_PATH))'
199         print r'            return FALSE;'
200         print r'        _tcscat(g_szDll, TEXT("\\%s.dll"));' % self.name
201         print r'        Log::Open("%s");' % self.name
202         print r'    case DLL_THREAD_ATTACH:'
203         print r'        return TRUE;'
204         print r'    case DLL_THREAD_DETACH:'
205         print r'        return TRUE;'
206         print r'    case DLL_PROCESS_DETACH:'
207         print r'        Log::Close();'
208         print r'        if(g_hDll) {'
209         print r'            FreeLibrary(g_hDll);'
210         print r'            g_hDll = NULL;'
211         print r'        }'
212         print r'        return TRUE;'
213         print r'    }'
214         print r'    (void)hinstDLL;'
215         print r'    (void)lpvReserved;'
216         print r'    return TRUE;'
217         print r'}'
218         print
219         for function in self.functions:
220             function.wrap_impl()
221         print
222