blob: 48a3f3f3a5fe3abec7ec6f3442334c84c18551c5 [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é Fonsecae54e4112009-06-25 13:56:18 +010049LPWSTR = WString
José Fonseca44eaac92009-06-30 19:48:50 +010050LPCWSTR = Const(WString)
José Fonsecac569f872009-01-04 16:45:17 +000051
José Fonseca4a9c40c2008-07-07 18:04:53 +090052LARGE_INTEGER = Intrinsic("LARGE_INTEGER", "0x%llx")
José Fonseca0b8fea12009-09-28 11:33:36 +010053SIZE_T = Alias("SIZE_T", SizeT)
José Fonsecad626cf42008-07-07 07:43:16 +090054
55HRESULT = Alias("HRESULT", Int)
56
57PVOID = Intrinsic("PVOID", "%p")
José Fonsecac569f872009-01-04 16:45:17 +000058LPVOID = PVOID
José Fonseca73f33cc2008-07-09 02:17:51 +090059HANDLE = Intrinsic("HANDLE", "%p")
José Fonsecad626cf42008-07-07 07:43:16 +090060HWND = Intrinsic("HWND", "%p")
José Fonseca73f33cc2008-07-09 02:17:51 +090061HDC = Intrinsic("HDC", "%p")
José Fonsecad626cf42008-07-07 07:43:16 +090062HMONITOR = Intrinsic("HMONITOR", "%p")
63
José Fonseca8a56d142008-07-09 12:18:08 +090064GUID = Struct("GUID", [
65 (DWORD, "Data1"),
66 (WORD, "Data2"),
67 (WORD, "Data3"),
68 (BYTE, "Data4[0]"),
69 (BYTE, "Data4[1]"),
70 (BYTE, "Data4[2]"),
71 (BYTE, "Data4[3]"),
72 (BYTE, "Data4[4]"),
73 (BYTE, "Data4[5]"),
74 (BYTE, "Data4[6]"),
75 (BYTE, "Data4[7]"),
76])
José Fonsecac569f872009-01-04 16:45:17 +000077LPGUID = Pointer(GUID)
José Fonseca8a56d142008-07-09 12:18:08 +090078
79#REFGUID = Alias("REFGUID", Pointer(GUID))
80REFGUID = Alias("REFGUID", GUID)
81
82IID = Alias("IID", GUID)
83#REFIID = Alias("REFIID", Pointer(IID))
84REFIID = Alias("REFIID", IID)
85
José Fonsecac569f872009-01-04 16:45:17 +000086CLSID = Alias("CLSID", GUID)
87#REFCLSID = Alias("REFCLSID", Pointer(CLSID))
88REFCLSID = Alias("REFCLSID", CLSID)
89
José Fonseca8a56d142008-07-09 12:18:08 +090090LUID = Struct("LUID", [
91 (DWORD, "LowPart"),
92 (LONG, "HighPart"),
93])
José Fonsecad626cf42008-07-07 07:43:16 +090094
95POINT = Struct("POINT", (
96 (LONG, "x"),
97 (LONG, "y"),
98))
José Fonsecac569f872009-01-04 16:45:17 +000099LPPOINT = Pointer(POINT)
José Fonsecad626cf42008-07-07 07:43:16 +0900100
101RECT = Struct("RECT", (
102 (LONG, "left"),
103 (LONG, "top"),
104 (LONG, "right"),
105 (LONG, "bottom"),
106))
José Fonsecac569f872009-01-04 16:45:17 +0000107LPRECT = Pointer(RECT)
José Fonsecad626cf42008-07-07 07:43:16 +0900108
109PALETTEENTRY = Struct("PALETTEENTRY", (
110 (BYTE, "peRed"),
111 (BYTE, "peGreen"),
112 (BYTE, "peBlue"),
113 (BYTE, "peFlags"),
114))
José Fonsecac569f872009-01-04 16:45:17 +0000115LPPALETTEENTRY = Pointer(PALETTEENTRY)
José Fonsecad626cf42008-07-07 07:43:16 +0900116
José Fonseca8a56d142008-07-09 12:18:08 +0900117
118RGNDATAHEADER = Struct("RGNDATAHEADER", [
119 (DWORD, "dwSize"),
120 (DWORD, "iType"),
121 (DWORD, "nCount"),
122 (DWORD, "nRgnSize"),
123 (RECT, "rcBound"),
124])
125
126RGNDATA = Struct("RGNDATA", [
127 (RGNDATAHEADER, "rdh"),
128 #(Char, "Buffer[1]"),
129])
José Fonsecac569f872009-01-04 16:45:17 +0000130LPRGNDATA = Pointer(RGNDATA)
José Fonsecad626cf42008-07-07 07:43:16 +0900131
José Fonseca0b8fea12009-09-28 11:33:36 +0100132HMODULE = Alias("HMODULE", LPVOID)
José Fonsecad626cf42008-07-07 07:43:16 +0900133
134IUnknown = Interface("IUnknown")
135
136IUnknown.methods = (
137 Method(HRESULT, "QueryInterface", ((REFIID, "riid"), (Pointer(Pointer(Void)), "ppvObj"))),
138 Method(ULONG, "AddRef", ()),
139 Method(ULONG, "Release", ()),
140)
141
142
José Fonseca3c2c9292009-05-04 12:16:30 +0100143class DllFunction(Function):
144
145 def get_true_pointer(self):
146 ptype = self.pointer_type()
147 pvalue = self.pointer_value()
148 print ' if(!g_hDll) {'
149 print ' g_hDll = LoadLibrary(g_szDll);'
150 print ' if(!g_hDll)'
151 self.fail_impl()
152 print ' }'
153 print ' if(!%s) {' % (pvalue,)
José Fonsecac77023d2009-05-04 12:53:50 +0100154 print ' %s = (%s)GetProcAddress(g_hDll, "%s");' % (pvalue, ptype, self.name)
José Fonseca3c2c9292009-05-04 12:16:30 +0100155 print ' if(!%s)' % (pvalue,)
156 self.fail_impl()
157 print ' }'
158
159
José Fonsecad626cf42008-07-07 07:43:16 +0900160class Dll:
161
162 def __init__(self, name):
163 self.name = name
164 self.functions = []
165 if self not in towrap:
166 towrap.append(self)
167
168 def wrap_name(self):
169 return "Wrap" + self.name
170
171 def wrap_pre_decl(self):
172 pass
173
174 def wrap_decl(self):
José Fonsecad626cf42008-07-07 07:43:16 +0900175 print 'static HINSTANCE g_hDll = NULL;'
José Fonsecad7605892008-07-07 13:09:31 +0900176 print 'static TCHAR g_szDll[MAX_PATH] = {0};'
José Fonsecad626cf42008-07-07 07:43:16 +0900177 print
178 print 'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);'
179 print
José Fonseca3c2c9292009-05-04 12:16:30 +0100180 for function in self.functions:
181 function.wrap_decl()
182 print
José Fonsecad626cf42008-07-07 07:43:16 +0900183
184 def wrap_impl(self):
185 print r'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {'
José Fonsecad626cf42008-07-07 07:43:16 +0900186 print r' switch(fdwReason) {'
187 print r' case DLL_PROCESS_ATTACH:'
José Fonsecad7605892008-07-07 13:09:31 +0900188 print r' if(!GetSystemDirectory(g_szDll, MAX_PATH))'
José Fonsecad626cf42008-07-07 07:43:16 +0900189 print r' return FALSE;'
José Fonsecad7605892008-07-07 13:09:31 +0900190 print r' _tcscat(g_szDll, TEXT("\\%s.dll"));' % self.name
José Fonsecadf824982009-04-13 13:49:51 +0100191 print r' Log::Open("%s");' % self.name
José Fonsecad626cf42008-07-07 07:43:16 +0900192 print r' case DLL_THREAD_ATTACH:'
193 print r' return TRUE;'
194 print r' case DLL_THREAD_DETACH:'
José Fonsecad7605892008-07-07 13:09:31 +0900195 print r' return TRUE;'
196 print r' case DLL_PROCESS_DETACH:'
José Fonseca22aec832008-07-09 09:38:45 +0900197 print r' Log::Close();'
José Fonsecad7605892008-07-07 13:09:31 +0900198 print r' if(g_hDll) {'
199 print r' FreeLibrary(g_hDll);'
200 print r' g_hDll = NULL;'
201 print r' }'
José Fonsecad626cf42008-07-07 07:43:16 +0900202 print r' return TRUE;'
203 print r' }'
José Fonsecad7605892008-07-07 13:09:31 +0900204 print r' (void)hinstDLL;'
José Fonsecad626cf42008-07-07 07:43:16 +0900205 print r' (void)lpvReserved;'
206 print r' return TRUE;'
207 print r'}'
208 print
209 for function in self.functions:
José Fonseca3c2c9292009-05-04 12:16:30 +0100210 function.wrap_impl()
José Fonsecad626cf42008-07-07 07:43:16 +0900211 print
212