blob: d5041a944e30cf351fc0eb13de0f3d18e536b681 [file] [log] [blame]
José Fonseca95442442008-07-08 10:32:53 +09001#############################################################################
2#
3# Copyright 2008 Jose Fonseca
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
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é Fonseca4a9c40c2008-07-07 18:04:53 +090041LARGE_INTEGER = Intrinsic("LARGE_INTEGER", "0x%llx")
José Fonsecad626cf42008-07-07 07:43:16 +090042
43HRESULT = Alias("HRESULT", Int)
44
45PVOID = Intrinsic("PVOID", "%p")
José Fonseca73f33cc2008-07-09 02:17:51 +090046HANDLE = Intrinsic("HANDLE", "%p")
José Fonsecad626cf42008-07-07 07:43:16 +090047HWND = Intrinsic("HWND", "%p")
José Fonseca73f33cc2008-07-09 02:17:51 +090048HDC = Intrinsic("HDC", "%p")
José Fonsecad626cf42008-07-07 07:43:16 +090049HMONITOR = Intrinsic("HMONITOR", "%p")
50
José Fonseca8a56d142008-07-09 12:18:08 +090051GUID = Struct("GUID", [
52 (DWORD, "Data1"),
53 (WORD, "Data2"),
54 (WORD, "Data3"),
55 (BYTE, "Data4[0]"),
56 (BYTE, "Data4[1]"),
57 (BYTE, "Data4[2]"),
58 (BYTE, "Data4[3]"),
59 (BYTE, "Data4[4]"),
60 (BYTE, "Data4[5]"),
61 (BYTE, "Data4[6]"),
62 (BYTE, "Data4[7]"),
63])
64
65#REFGUID = Alias("REFGUID", Pointer(GUID))
66REFGUID = Alias("REFGUID", GUID)
67
68IID = Alias("IID", GUID)
69#REFIID = Alias("REFIID", Pointer(IID))
70REFIID = Alias("REFIID", IID)
71
72LUID = Struct("LUID", [
73 (DWORD, "LowPart"),
74 (LONG, "HighPart"),
75])
José Fonsecad626cf42008-07-07 07:43:16 +090076
77POINT = Struct("POINT", (
78 (LONG, "x"),
79 (LONG, "y"),
80))
81
82RECT = Struct("RECT", (
83 (LONG, "left"),
84 (LONG, "top"),
85 (LONG, "right"),
86 (LONG, "bottom"),
87))
88
89PALETTEENTRY = Struct("PALETTEENTRY", (
90 (BYTE, "peRed"),
91 (BYTE, "peGreen"),
92 (BYTE, "peBlue"),
93 (BYTE, "peFlags"),
94))
95
José Fonseca8a56d142008-07-09 12:18:08 +090096
97RGNDATAHEADER = Struct("RGNDATAHEADER", [
98 (DWORD, "dwSize"),
99 (DWORD, "iType"),
100 (DWORD, "nCount"),
101 (DWORD, "nRgnSize"),
102 (RECT, "rcBound"),
103])
104
105RGNDATA = Struct("RGNDATA", [
106 (RGNDATAHEADER, "rdh"),
107 #(Char, "Buffer[1]"),
108])
José Fonsecad626cf42008-07-07 07:43:16 +0900109
110
111IUnknown = Interface("IUnknown")
112
113IUnknown.methods = (
114 Method(HRESULT, "QueryInterface", ((REFIID, "riid"), (Pointer(Pointer(Void)), "ppvObj"))),
115 Method(ULONG, "AddRef", ()),
116 Method(ULONG, "Release", ()),
117)
118
119
120class Dll:
121
122 def __init__(self, name):
123 self.name = name
124 self.functions = []
125 if self not in towrap:
126 towrap.append(self)
127
128 def wrap_name(self):
129 return "Wrap" + self.name
130
131 def wrap_pre_decl(self):
132 pass
133
134 def wrap_decl(self):
José Fonsecad626cf42008-07-07 07:43:16 +0900135 print 'static HINSTANCE g_hDll = NULL;'
José Fonsecad7605892008-07-07 13:09:31 +0900136 print 'static TCHAR g_szDll[MAX_PATH] = {0};'
José Fonsecad626cf42008-07-07 07:43:16 +0900137 print
138 print 'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);'
139 print
140
141 def wrap_impl(self):
142 print r'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {'
José Fonsecad626cf42008-07-07 07:43:16 +0900143 print r' switch(fdwReason) {'
144 print r' case DLL_PROCESS_ATTACH:'
José Fonsecad7605892008-07-07 13:09:31 +0900145 print r' if(!GetSystemDirectory(g_szDll, MAX_PATH))'
José Fonsecad626cf42008-07-07 07:43:16 +0900146 print r' return FALSE;'
José Fonsecad7605892008-07-07 13:09:31 +0900147 print r' _tcscat(g_szDll, TEXT("\\%s.dll"));' % self.name
José Fonsecad626cf42008-07-07 07:43:16 +0900148 print r' case DLL_THREAD_ATTACH:'
149 print r' return TRUE;'
150 print r' case DLL_THREAD_DETACH:'
José Fonsecad7605892008-07-07 13:09:31 +0900151 print r' return TRUE;'
152 print r' case DLL_PROCESS_DETACH:'
José Fonseca22aec832008-07-09 09:38:45 +0900153 print r' Log::Close();'
José Fonsecad7605892008-07-07 13:09:31 +0900154 print r' if(g_hDll) {'
155 print r' FreeLibrary(g_hDll);'
156 print r' g_hDll = NULL;'
157 print r' }'
José Fonsecad626cf42008-07-07 07:43:16 +0900158 print r' return TRUE;'
159 print r' }'
José Fonsecad7605892008-07-07 13:09:31 +0900160 print r' (void)hinstDLL;'
José Fonsecad626cf42008-07-07 07:43:16 +0900161 print r' (void)lpvReserved;'
162 print r' return TRUE;'
163 print r'}'
164 print
165 for function in self.functions:
166 type = 'P' + function.name
167 print function.prototype() + ' {'
José Fonsecad7605892008-07-07 13:09:31 +0900168 if 1:
José Fonseca22aec832008-07-09 09:38:45 +0900169 print ' Log::Close();'
170 print ' Log::Open(TEXT("%s"));' % self.name
171 #print ' Log::ReOpen();'
José Fonsecad626cf42008-07-07 07:43:16 +0900172 print ' typedef ' + function.prototype('* %s' % type) + ';'
173 print ' %s pFunction;' % type
174 if function.type is Void:
175 result = ''
176 else:
177 print ' %s result;' % function.type
178 result = 'result = '
José Fonsecaa83fb242008-07-07 16:55:52 +0900179 print ' if(!g_hDll) {'
180 print ' g_hDll = LoadLibrary(g_szDll);'
181 print ' if(!g_hDll)'
182 print ' ExitProcess(0);'
183 print ' }'
José Fonsecad626cf42008-07-07 07:43:16 +0900184 print ' pFunction = (%s)GetProcAddress( g_hDll, "%s");' % (type, function.name)
185 print ' if(!pFunction)'
186 print ' ExitProcess(0);'
José Fonseca22aec832008-07-09 09:38:45 +0900187 print ' Log::BeginCall("%s");' % (function.name)
José Fonsecad626cf42008-07-07 07:43:16 +0900188 print ' %spFunction(%s);' % (result, ', '.join([str(name) for type, name in function.args]))
José Fonseca22aec832008-07-09 09:38:45 +0900189 print ' Log::EndCall();'
José Fonsecad626cf42008-07-07 07:43:16 +0900190 for type, name in function.args:
191 if type.isoutput():
192 type.wrap_instance(name)
193 if function.type is not Void:
194 function.type.wrap_instance('result')
José Fonsecad626cf42008-07-07 07:43:16 +0900195 if function.type is not Void:
196 print ' return result;'
197 print '}'
198 print
199 print
200