]> git.cworth.org Git - apitrace/blob - windows.py
Trace opengl32.dll.
[apitrace] / windows.py
1 #############################################################################
2 #
3 # Copyright 2008 Tungsten Graphics, Inc.
4 #
5 # This program is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published
7 # by the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 #############################################################################
19
20 """windows.h"""
21
22 from base import *
23
24 SHORT = Intrinsic("SHORT", "%i")
25 USHORT = Intrinsic("USHORT", "%u")
26 INT = Intrinsic("INT", "%i")
27 UINT = Intrinsic("UINT", "%u")
28 LONG = Intrinsic("LONG", "%li")
29 ULONG = Intrinsic("ULONG", "%lu")
30 FLOAT = Intrinsic("FLOAT", "%f")
31
32 INT32 = Intrinsic("INT32", "%i")
33 UINT32 = Intrinsic("UINT32", "%i")
34
35 BYTE = Intrinsic("BYTE", "0x%02lx")
36 WORD = Intrinsic("WORD", "0x%04lx")
37 DWORD = Intrinsic("DWORD", "0x%08lx")
38
39 BOOL = Intrinsic("BOOL", "%i")
40
41 LPLONG = Pointer(LONG)
42 LPWORD = Pointer(WORD)
43 LPDWORD = Pointer(DWORD)
44 LPBOOL = Pointer(BOOL)
45 LPSIZE = LPDWORD
46
47 LPSTR = String
48 LPCSTR = Const(String)
49 LPWSTR = String
50
51 LARGE_INTEGER = Intrinsic("LARGE_INTEGER", "0x%llx")
52
53 HRESULT = Alias("HRESULT", Int)
54
55 PVOID = Intrinsic("PVOID", "%p")
56 LPVOID = PVOID
57 HANDLE = Intrinsic("HANDLE", "%p")
58 HWND = Intrinsic("HWND", "%p")
59 HDC = Intrinsic("HDC", "%p")
60 HMONITOR = Intrinsic("HMONITOR", "%p")
61
62 GUID = Struct("GUID", [
63     (DWORD, "Data1"),
64     (WORD, "Data2"),
65     (WORD, "Data3"),
66     (BYTE, "Data4[0]"),
67     (BYTE, "Data4[1]"),
68     (BYTE, "Data4[2]"),
69     (BYTE, "Data4[3]"),
70     (BYTE, "Data4[4]"),
71     (BYTE, "Data4[5]"),
72     (BYTE, "Data4[6]"),
73     (BYTE, "Data4[7]"),
74 ])
75 LPGUID = Pointer(GUID)
76
77 #REFGUID = Alias("REFGUID", Pointer(GUID))
78 REFGUID = Alias("REFGUID", GUID)
79
80 IID = Alias("IID", GUID)
81 #REFIID = Alias("REFIID", Pointer(IID))
82 REFIID = Alias("REFIID", IID)
83
84 CLSID = Alias("CLSID", GUID)
85 #REFCLSID = Alias("REFCLSID", Pointer(CLSID))
86 REFCLSID = Alias("REFCLSID", CLSID)
87
88 LUID = Struct("LUID", [
89     (DWORD, "LowPart"),
90     (LONG, "HighPart"),
91 ])
92
93 POINT = Struct("POINT", (
94   (LONG, "x"),
95   (LONG, "y"),
96 )) 
97 LPPOINT = Pointer(POINT)
98
99 RECT = Struct("RECT", (
100   (LONG, "left"),
101   (LONG, "top"),
102   (LONG, "right"), 
103   (LONG, "bottom"), 
104 )) 
105 LPRECT = Pointer(RECT)
106
107 PALETTEENTRY = Struct("PALETTEENTRY", (
108   (BYTE, "peRed"),
109   (BYTE, "peGreen"),
110   (BYTE, "peBlue"), 
111   (BYTE, "peFlags"), 
112 )) 
113 LPPALETTEENTRY = Pointer(PALETTEENTRY)
114
115
116 RGNDATAHEADER = Struct("RGNDATAHEADER", [
117     (DWORD, "dwSize"),
118     (DWORD, "iType"),
119     (DWORD, "nCount"),
120     (DWORD, "nRgnSize"),
121     (RECT, "rcBound"),
122 ])
123
124 RGNDATA = Struct("RGNDATA", [
125     (RGNDATAHEADER, "rdh"),
126     #(Char, "Buffer[1]"),
127 ])
128 LPRGNDATA = Pointer(RGNDATA)
129
130
131 IUnknown = Interface("IUnknown")
132
133 IUnknown.methods = (
134         Method(HRESULT, "QueryInterface", ((REFIID, "riid"), (Pointer(Pointer(Void)), "ppvObj"))),
135         Method(ULONG, "AddRef", ()),
136         Method(ULONG, "Release", ()),
137 )
138
139
140 class Dll:
141
142     def __init__(self, name):
143         self.name = name
144         self.functions = []
145         if self not in towrap:
146             towrap.append(self)
147
148     def wrap_name(self):
149         return "Wrap" + self.name
150
151     def wrap_pre_decl(self):
152         pass
153
154     def wrap_decl(self):
155         print 'static HINSTANCE g_hDll = NULL;'
156         print 'static TCHAR g_szDll[MAX_PATH] = {0};'
157         print
158         print 'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);'
159         print
160
161     def wrap_impl(self):
162         print r'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {'
163         print r'    switch(fdwReason) {'
164         print r'    case DLL_PROCESS_ATTACH:'
165         print r'        if(!GetSystemDirectory(g_szDll, MAX_PATH))'
166         print r'            return FALSE;'
167         print r'        _tcscat(g_szDll, TEXT("\\%s.dll"));' % self.name
168         print r'        Log::Open("%s");' % self.name
169         print r'    case DLL_THREAD_ATTACH:'
170         print r'        return TRUE;'
171         print r'    case DLL_THREAD_DETACH:'
172         print r'        return TRUE;'
173         print r'    case DLL_PROCESS_DETACH:'
174         print r'        Log::Close();'
175         print r'        if(g_hDll) {'
176         print r'            FreeLibrary(g_hDll);'
177         print r'            g_hDll = NULL;'
178         print r'        }'
179         print r'        return TRUE;'
180         print r'    }'
181         print r'    (void)hinstDLL;'
182         print r'    (void)lpvReserved;'
183         print r'    return TRUE;'
184         print r'}'
185         print
186         for function in self.functions:
187             type = 'P' + function.name
188             print function.prototype() + ' {'
189             if 0:
190                 print '    Log::Close();'
191                 print '    Log::Open("%s");' % self.name
192             #print '    Log::ReOpen();'
193             print '    typedef ' + function.prototype('* %s' % type) + ';'
194             print '    %s pFunction;' % type
195             if function.type is Void:
196                 result = ''
197             else:
198                 print '    %s result;' % function.type
199                 result = 'result = '
200             print '    if(!g_hDll) {'
201             print '        g_hDll = LoadLibrary(g_szDll);'
202             print '        if(!g_hDll)'
203             print '            ExitProcess(0);'
204             print '    }'
205             print '    pFunction = (%s)GetProcAddress( g_hDll, "%s");' % (type, function.name)
206             print '    if(!pFunction)'
207             print '        ExitProcess(0);'
208             print '    Log::BeginCall("%s");' % (function.name)
209             for type, name in function.args:
210                 if not type.isoutput():
211                     type.unwrap_instance(name)
212                     print '    Log::BeginArg("%s", "%s");' % (type, name)
213                     type.dump(name)
214                     print '    Log::EndArg();'
215             print '    %spFunction(%s);' % (result, ', '.join([str(name) for type, name in function.args]))
216             for type, name in function.args:
217                 if type.isoutput():
218                     print '    Log::BeginArg("%s", "%s");' % (type, name)
219                     type.dump(name)
220                     print '    Log::EndArg();'
221                     type.wrap_instance(name)
222             if function.type is not Void:
223                 print '    Log::BeginReturn("%s");' % function.type
224                 function.type.dump("result")
225                 print '    Log::EndReturn();'
226                 function.type.wrap_instance('result')
227             print '    Log::EndCall();'
228             if function.type is not Void:
229                 print '    return result;'
230             print '}'
231             print
232         print
233