blob: 4db8616618f31178a42d635f77a76bed44e592b6 [file] [log] [blame]
José Fonseca95442442008-07-08 10:32:53 +09001#############################################################################
2#
José Fonsecaf6c86992008-07-10 22:44:57 +09003# Copyright 2008 Tungsten Graphics, Inc.
José Fonseca95442442008-07-08 10:32:53 +09004#
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
José Fonsecad626cf42008-07-07 07:43:16 +090020"""windows.h"""
21
22from base import *
23
José Fonseca73f33cc2008-07-09 02:17:51 +090024SHORT = Intrinsic("SHORT", "%i")
25USHORT = Intrinsic("USHORT", "%u")
26INT = Intrinsic("INT", "%i")
José Fonsecad626cf42008-07-07 07:43:16 +090027UINT = Intrinsic("UINT", "%u")
28LONG = Intrinsic("LONG", "%li")
29ULONG = Intrinsic("ULONG", "%lu")
José Fonseca73f33cc2008-07-09 02:17:51 +090030FLOAT = Intrinsic("FLOAT", "%f")
31
32INT32 = Intrinsic("INT32", "%i")
33UINT32 = Intrinsic("UINT32", "%i")
José Fonsecad626cf42008-07-07 07:43:16 +090034
José Fonseca0dcc85c2008-07-08 07:34:54 +090035BYTE = Intrinsic("BYTE", "0x%02lx")
36WORD = Intrinsic("WORD", "0x%04lx")
37DWORD = Intrinsic("DWORD", "0x%08lx")
José Fonsecad626cf42008-07-07 07:43:16 +090038
39BOOL = Intrinsic("BOOL", "%i")
40
José Fonsecac569f872009-01-04 16:45:17 +000041LPLONG = Pointer(LONG)
42LPWORD = Pointer(WORD)
43LPDWORD = Pointer(DWORD)
44LPBOOL = Pointer(BOOL)
45LPSIZE = LPDWORD
46
47LPSTR = String
José Fonseca36e25aa2009-04-13 14:08:08 +010048LPCSTR = Const(String)
José Fonsecac569f872009-01-04 16:45:17 +000049LPWSTR = String
50
José Fonseca4a9c40c2008-07-07 18:04:53 +090051LARGE_INTEGER = Intrinsic("LARGE_INTEGER", "0x%llx")
José Fonsecad626cf42008-07-07 07:43:16 +090052
53HRESULT = Alias("HRESULT", Int)
54
55PVOID = Intrinsic("PVOID", "%p")
José Fonsecac569f872009-01-04 16:45:17 +000056LPVOID = PVOID
José Fonseca73f33cc2008-07-09 02:17:51 +090057HANDLE = Intrinsic("HANDLE", "%p")
José Fonsecad626cf42008-07-07 07:43:16 +090058HWND = Intrinsic("HWND", "%p")
José Fonseca73f33cc2008-07-09 02:17:51 +090059HDC = Intrinsic("HDC", "%p")
José Fonsecad626cf42008-07-07 07:43:16 +090060HMONITOR = Intrinsic("HMONITOR", "%p")
61
José Fonseca8a56d142008-07-09 12:18:08 +090062GUID = 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])
José Fonsecac569f872009-01-04 16:45:17 +000075LPGUID = Pointer(GUID)
José Fonseca8a56d142008-07-09 12:18:08 +090076
77#REFGUID = Alias("REFGUID", Pointer(GUID))
78REFGUID = Alias("REFGUID", GUID)
79
80IID = Alias("IID", GUID)
81#REFIID = Alias("REFIID", Pointer(IID))
82REFIID = Alias("REFIID", IID)
83
José Fonsecac569f872009-01-04 16:45:17 +000084CLSID = Alias("CLSID", GUID)
85#REFCLSID = Alias("REFCLSID", Pointer(CLSID))
86REFCLSID = Alias("REFCLSID", CLSID)
87
José Fonseca8a56d142008-07-09 12:18:08 +090088LUID = Struct("LUID", [
89 (DWORD, "LowPart"),
90 (LONG, "HighPart"),
91])
José Fonsecad626cf42008-07-07 07:43:16 +090092
93POINT = Struct("POINT", (
94 (LONG, "x"),
95 (LONG, "y"),
96))
José Fonsecac569f872009-01-04 16:45:17 +000097LPPOINT = Pointer(POINT)
José Fonsecad626cf42008-07-07 07:43:16 +090098
99RECT = Struct("RECT", (
100 (LONG, "left"),
101 (LONG, "top"),
102 (LONG, "right"),
103 (LONG, "bottom"),
104))
José Fonsecac569f872009-01-04 16:45:17 +0000105LPRECT = Pointer(RECT)
José Fonsecad626cf42008-07-07 07:43:16 +0900106
107PALETTEENTRY = Struct("PALETTEENTRY", (
108 (BYTE, "peRed"),
109 (BYTE, "peGreen"),
110 (BYTE, "peBlue"),
111 (BYTE, "peFlags"),
112))
José Fonsecac569f872009-01-04 16:45:17 +0000113LPPALETTEENTRY = Pointer(PALETTEENTRY)
José Fonsecad626cf42008-07-07 07:43:16 +0900114
José Fonseca8a56d142008-07-09 12:18:08 +0900115
116RGNDATAHEADER = Struct("RGNDATAHEADER", [
117 (DWORD, "dwSize"),
118 (DWORD, "iType"),
119 (DWORD, "nCount"),
120 (DWORD, "nRgnSize"),
121 (RECT, "rcBound"),
122])
123
124RGNDATA = Struct("RGNDATA", [
125 (RGNDATAHEADER, "rdh"),
126 #(Char, "Buffer[1]"),
127])
José Fonsecac569f872009-01-04 16:45:17 +0000128LPRGNDATA = Pointer(RGNDATA)
José Fonsecad626cf42008-07-07 07:43:16 +0900129
130
131IUnknown = Interface("IUnknown")
132
133IUnknown.methods = (
134 Method(HRESULT, "QueryInterface", ((REFIID, "riid"), (Pointer(Pointer(Void)), "ppvObj"))),
135 Method(ULONG, "AddRef", ()),
136 Method(ULONG, "Release", ()),
137)
138
139
José Fonseca3c2c9292009-05-04 12:16:30 +0100140class DllFunction(Function):
141
142 def get_true_pointer(self):
143 ptype = self.pointer_type()
144 pvalue = self.pointer_value()
145 print ' if(!g_hDll) {'
146 print ' g_hDll = LoadLibrary(g_szDll);'
147 print ' if(!g_hDll)'
148 self.fail_impl()
149 print ' }'
150 print ' if(!%s) {' % (pvalue,)
151 print ' %s = (%s)GetProcAddress( g_hDll, "%s");' % (pvalue, ptype, self.name)
152 print ' if(!%s)' % (pvalue,)
153 self.fail_impl()
154 print ' }'
155
156
José Fonsecad626cf42008-07-07 07:43:16 +0900157class Dll:
158
159 def __init__(self, name):
160 self.name = name
161 self.functions = []
162 if self not in towrap:
163 towrap.append(self)
164
165 def wrap_name(self):
166 return "Wrap" + self.name
167
168 def wrap_pre_decl(self):
169 pass
170
171 def wrap_decl(self):
José Fonsecad626cf42008-07-07 07:43:16 +0900172 print 'static HINSTANCE g_hDll = NULL;'
José Fonsecad7605892008-07-07 13:09:31 +0900173 print 'static TCHAR g_szDll[MAX_PATH] = {0};'
José Fonsecad626cf42008-07-07 07:43:16 +0900174 print
175 print 'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);'
176 print
José Fonseca3c2c9292009-05-04 12:16:30 +0100177 for function in self.functions:
178 function.wrap_decl()
179 print
José Fonsecad626cf42008-07-07 07:43:16 +0900180
181 def wrap_impl(self):
182 print r'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {'
José Fonsecad626cf42008-07-07 07:43:16 +0900183 print r' switch(fdwReason) {'
184 print r' case DLL_PROCESS_ATTACH:'
José Fonsecad7605892008-07-07 13:09:31 +0900185 print r' if(!GetSystemDirectory(g_szDll, MAX_PATH))'
José Fonsecad626cf42008-07-07 07:43:16 +0900186 print r' return FALSE;'
José Fonsecad7605892008-07-07 13:09:31 +0900187 print r' _tcscat(g_szDll, TEXT("\\%s.dll"));' % self.name
José Fonsecadf824982009-04-13 13:49:51 +0100188 print r' Log::Open("%s");' % self.name
José Fonsecad626cf42008-07-07 07:43:16 +0900189 print r' case DLL_THREAD_ATTACH:'
190 print r' return TRUE;'
191 print r' case DLL_THREAD_DETACH:'
José Fonsecad7605892008-07-07 13:09:31 +0900192 print r' return TRUE;'
193 print r' case DLL_PROCESS_DETACH:'
José Fonseca22aec832008-07-09 09:38:45 +0900194 print r' Log::Close();'
José Fonsecad7605892008-07-07 13:09:31 +0900195 print r' if(g_hDll) {'
196 print r' FreeLibrary(g_hDll);'
197 print r' g_hDll = NULL;'
198 print r' }'
José Fonsecad626cf42008-07-07 07:43:16 +0900199 print r' return TRUE;'
200 print r' }'
José Fonsecad7605892008-07-07 13:09:31 +0900201 print r' (void)hinstDLL;'
José Fonsecad626cf42008-07-07 07:43:16 +0900202 print r' (void)lpvReserved;'
203 print r' return TRUE;'
204 print r'}'
205 print
206 for function in self.functions:
José Fonseca3c2c9292009-05-04 12:16:30 +0100207 function.wrap_impl()
José Fonsecad626cf42008-07-07 07:43:16 +0900208 print
209