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