blob: 79b835ef679f5961631162a49c5d6de6b17cc4d2 [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
51REFIID = Alias("REFIID", PVOID)
52GUID = Alias("GUID", PVOID)
José Fonseca73f33cc2008-07-09 02:17:51 +090053LUID = Alias("LUID", PVOID)
José Fonsecad626cf42008-07-07 07:43:16 +090054
55POINT = Struct("POINT", (
56 (LONG, "x"),
57 (LONG, "y"),
58))
59
60RECT = Struct("RECT", (
61 (LONG, "left"),
62 (LONG, "top"),
63 (LONG, "right"),
64 (LONG, "bottom"),
65))
66
67PALETTEENTRY = Struct("PALETTEENTRY", (
68 (BYTE, "peRed"),
69 (BYTE, "peGreen"),
70 (BYTE, "peBlue"),
71 (BYTE, "peFlags"),
72))
73
74RGNDATA = Struct("RGNDATA", ())
75REFGUID = Alias("REFGUID", PVOID)
76
77
78IUnknown = Interface("IUnknown")
79
80IUnknown.methods = (
81 Method(HRESULT, "QueryInterface", ((REFIID, "riid"), (Pointer(Pointer(Void)), "ppvObj"))),
82 Method(ULONG, "AddRef", ()),
83 Method(ULONG, "Release", ()),
84)
85
86
87class Dll:
88
89 def __init__(self, name):
90 self.name = name
91 self.functions = []
92 if self not in towrap:
93 towrap.append(self)
94
95 def wrap_name(self):
96 return "Wrap" + self.name
97
98 def wrap_pre_decl(self):
99 pass
100
101 def wrap_decl(self):
José Fonsecad626cf42008-07-07 07:43:16 +0900102 print 'static HINSTANCE g_hDll = NULL;'
José Fonsecad7605892008-07-07 13:09:31 +0900103 print 'static TCHAR g_szDll[MAX_PATH] = {0};'
José Fonsecad626cf42008-07-07 07:43:16 +0900104 print
105 print 'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);'
106 print
107
108 def wrap_impl(self):
109 print r'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {'
José Fonsecad7605892008-07-07 13:09:31 +0900110 #print r' Log * pLog;'
José Fonsecad626cf42008-07-07 07:43:16 +0900111 print r' switch(fdwReason) {'
112 print r' case DLL_PROCESS_ATTACH:'
José Fonsecad7605892008-07-07 13:09:31 +0900113 print r' if(!GetSystemDirectory(g_szDll, MAX_PATH))'
José Fonsecad626cf42008-07-07 07:43:16 +0900114 print r' return FALSE;'
José Fonsecad7605892008-07-07 13:09:31 +0900115 print r' _tcscat(g_szDll, TEXT("\\%s.dll"));' % self.name
116 #print r' if ((dwTlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES)'
117 #print r' return FALSE;'
118 #print r' if(!g_pLog)'
119 #print r' g_pLog = new Log(TEXT("%s"));' % self.name
José Fonsecad626cf42008-07-07 07:43:16 +0900120 print r' case DLL_THREAD_ATTACH:'
José Fonsecad7605892008-07-07 13:09:31 +0900121 #print r' pLog = new Log(TEXT("%s"));' % self.name
122 #print r' TlsSetValue(dwTlsIndex, pLog);'
José Fonsecad626cf42008-07-07 07:43:16 +0900123 print r' return TRUE;'
124 print r' case DLL_THREAD_DETACH:'
José Fonsecad7605892008-07-07 13:09:31 +0900125 #print r' pLog = (Log *)TlsGetValue(dwTlsIndex);'
126 #print r' if (pLog != NULL)'
127 #print r' delete pLog;'
128 print r' return TRUE;'
129 print r' case DLL_PROCESS_DETACH:'
130 #print r' pLog = (Log *)TlsGetValue(dwTlsIndex);'
131 #print r' if (pLog != NULL)'
132 #print r' delete pLog;'
133 #print r' TlsFree(dwTlsIndex);'
134 print r' if(g_pLog) {'
135 print r' delete g_pLog;'
136 print r' g_pLog = NULL;'
137 print r' }'
138 print r' if(g_hDll) {'
139 print r' FreeLibrary(g_hDll);'
140 print r' g_hDll = NULL;'
141 print r' }'
José Fonsecad626cf42008-07-07 07:43:16 +0900142 print r' return TRUE;'
143 print r' }'
José Fonsecad7605892008-07-07 13:09:31 +0900144 print r' (void)hinstDLL;'
José Fonsecad626cf42008-07-07 07:43:16 +0900145 print r' (void)lpvReserved;'
146 print r' return TRUE;'
147 print r'}'
148 print
149 for function in self.functions:
150 type = 'P' + function.name
151 print function.prototype() + ' {'
José Fonsecad7605892008-07-07 13:09:31 +0900152 if 1:
153 print ' if(g_pLog)'
154 print ' delete g_pLog;'
155 print ' g_pLog = new Log(TEXT("%s"));' % self.name
156 #print ' g_pLog->ReOpen();'
José Fonsecad626cf42008-07-07 07:43:16 +0900157 print ' typedef ' + function.prototype('* %s' % type) + ';'
158 print ' %s pFunction;' % type
159 if function.type is Void:
160 result = ''
161 else:
162 print ' %s result;' % function.type
163 result = 'result = '
José Fonsecaa83fb242008-07-07 16:55:52 +0900164 print ' if(!g_hDll) {'
165 print ' g_hDll = LoadLibrary(g_szDll);'
166 print ' if(!g_hDll)'
167 print ' ExitProcess(0);'
168 print ' }'
José Fonsecad626cf42008-07-07 07:43:16 +0900169 print ' pFunction = (%s)GetProcAddress( g_hDll, "%s");' % (type, function.name)
170 print ' if(!pFunction)'
171 print ' ExitProcess(0);'
172 print ' g_pLog->BeginCall("%s");' % (function.name)
173 print ' %spFunction(%s);' % (result, ', '.join([str(name) for type, name in function.args]))
José Fonsecad7605892008-07-07 13:09:31 +0900174 print ' g_pLog->EndCall();'
José Fonsecad626cf42008-07-07 07:43:16 +0900175 for type, name in function.args:
176 if type.isoutput():
177 type.wrap_instance(name)
178 if function.type is not Void:
179 function.type.wrap_instance('result')
José Fonsecad626cf42008-07-07 07:43:16 +0900180 if function.type is not Void:
181 print ' return result;'
182 print '}'
183 print
184 print
185