blob: 65517d74636434a954596898a68cfc032cb2fd6d [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
24INT = Intrinsic("INT", "%u")
25UINT = Intrinsic("UINT", "%u")
26LONG = Intrinsic("LONG", "%li")
27ULONG = Intrinsic("ULONG", "%lu")
28
José Fonseca0dcc85c2008-07-08 07:34:54 +090029BYTE = Intrinsic("BYTE", "0x%02lx")
30WORD = Intrinsic("WORD", "0x%04lx")
31DWORD = Intrinsic("DWORD", "0x%08lx")
José Fonsecad626cf42008-07-07 07:43:16 +090032
33BOOL = Intrinsic("BOOL", "%i")
34
José Fonseca4a9c40c2008-07-07 18:04:53 +090035LARGE_INTEGER = Intrinsic("LARGE_INTEGER", "0x%llx")
José Fonsecad626cf42008-07-07 07:43:16 +090036
37HRESULT = Alias("HRESULT", Int)
38
39PVOID = Intrinsic("PVOID", "%p")
40HWND = Intrinsic("HWND", "%p")
41HMONITOR = Intrinsic("HMONITOR", "%p")
42
43REFIID = Alias("REFIID", PVOID)
44GUID = Alias("GUID", PVOID)
45
46POINT = Struct("POINT", (
47 (LONG, "x"),
48 (LONG, "y"),
49))
50
51RECT = Struct("RECT", (
52 (LONG, "left"),
53 (LONG, "top"),
54 (LONG, "right"),
55 (LONG, "bottom"),
56))
57
58PALETTEENTRY = Struct("PALETTEENTRY", (
59 (BYTE, "peRed"),
60 (BYTE, "peGreen"),
61 (BYTE, "peBlue"),
62 (BYTE, "peFlags"),
63))
64
65RGNDATA = Struct("RGNDATA", ())
66REFGUID = Alias("REFGUID", PVOID)
67
68
69IUnknown = Interface("IUnknown")
70
71IUnknown.methods = (
72 Method(HRESULT, "QueryInterface", ((REFIID, "riid"), (Pointer(Pointer(Void)), "ppvObj"))),
73 Method(ULONG, "AddRef", ()),
74 Method(ULONG, "Release", ()),
75)
76
77
78class Dll:
79
80 def __init__(self, name):
81 self.name = name
82 self.functions = []
83 if self not in towrap:
84 towrap.append(self)
85
86 def wrap_name(self):
87 return "Wrap" + self.name
88
89 def wrap_pre_decl(self):
90 pass
91
92 def wrap_decl(self):
José Fonsecad626cf42008-07-07 07:43:16 +090093 print 'static HINSTANCE g_hDll = NULL;'
José Fonsecaa83fb242008-07-07 16:55:52 +090094 print 'Log * g_pLog = NULL;'
José Fonsecad7605892008-07-07 13:09:31 +090095 print 'static TCHAR g_szDll[MAX_PATH] = {0};'
José Fonsecad626cf42008-07-07 07:43:16 +090096 print
97 print 'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);'
98 print
99
100 def wrap_impl(self):
101 print r'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {'
José Fonsecad7605892008-07-07 13:09:31 +0900102 #print r' Log * pLog;'
José Fonsecad626cf42008-07-07 07:43:16 +0900103 print r' switch(fdwReason) {'
104 print r' case DLL_PROCESS_ATTACH:'
José Fonsecad7605892008-07-07 13:09:31 +0900105 print r' if(!GetSystemDirectory(g_szDll, MAX_PATH))'
José Fonsecad626cf42008-07-07 07:43:16 +0900106 print r' return FALSE;'
José Fonsecad7605892008-07-07 13:09:31 +0900107 print r' _tcscat(g_szDll, TEXT("\\%s.dll"));' % self.name
108 #print r' if ((dwTlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES)'
109 #print r' return FALSE;'
110 #print r' if(!g_pLog)'
111 #print r' g_pLog = new Log(TEXT("%s"));' % self.name
José Fonsecad626cf42008-07-07 07:43:16 +0900112 print r' case DLL_THREAD_ATTACH:'
José Fonsecad7605892008-07-07 13:09:31 +0900113 #print r' pLog = new Log(TEXT("%s"));' % self.name
114 #print r' TlsSetValue(dwTlsIndex, pLog);'
José Fonsecad626cf42008-07-07 07:43:16 +0900115 print r' return TRUE;'
116 print r' case DLL_THREAD_DETACH:'
José Fonsecad7605892008-07-07 13:09:31 +0900117 #print r' pLog = (Log *)TlsGetValue(dwTlsIndex);'
118 #print r' if (pLog != NULL)'
119 #print r' delete pLog;'
120 print r' return TRUE;'
121 print r' case DLL_PROCESS_DETACH:'
122 #print r' pLog = (Log *)TlsGetValue(dwTlsIndex);'
123 #print r' if (pLog != NULL)'
124 #print r' delete pLog;'
125 #print r' TlsFree(dwTlsIndex);'
126 print r' if(g_pLog) {'
127 print r' delete g_pLog;'
128 print r' g_pLog = NULL;'
129 print r' }'
130 print r' if(g_hDll) {'
131 print r' FreeLibrary(g_hDll);'
132 print r' g_hDll = NULL;'
133 print r' }'
José Fonsecad626cf42008-07-07 07:43:16 +0900134 print r' return TRUE;'
135 print r' }'
José Fonsecad7605892008-07-07 13:09:31 +0900136 print r' (void)hinstDLL;'
José Fonsecad626cf42008-07-07 07:43:16 +0900137 print r' (void)lpvReserved;'
138 print r' return TRUE;'
139 print r'}'
140 print
141 for function in self.functions:
142 type = 'P' + function.name
143 print function.prototype() + ' {'
José Fonsecad7605892008-07-07 13:09:31 +0900144 if 1:
145 print ' if(g_pLog)'
146 print ' delete g_pLog;'
147 print ' g_pLog = new Log(TEXT("%s"));' % self.name
148 #print ' g_pLog->ReOpen();'
José Fonsecad626cf42008-07-07 07:43:16 +0900149 print ' typedef ' + function.prototype('* %s' % type) + ';'
150 print ' %s pFunction;' % type
151 if function.type is Void:
152 result = ''
153 else:
154 print ' %s result;' % function.type
155 result = 'result = '
José Fonsecaa83fb242008-07-07 16:55:52 +0900156 print ' if(!g_hDll) {'
157 print ' g_hDll = LoadLibrary(g_szDll);'
158 print ' if(!g_hDll)'
159 print ' ExitProcess(0);'
160 print ' }'
José Fonsecad626cf42008-07-07 07:43:16 +0900161 print ' pFunction = (%s)GetProcAddress( g_hDll, "%s");' % (type, function.name)
162 print ' if(!pFunction)'
163 print ' ExitProcess(0);'
164 print ' g_pLog->BeginCall("%s");' % (function.name)
165 print ' %spFunction(%s);' % (result, ', '.join([str(name) for type, name in function.args]))
José Fonsecad7605892008-07-07 13:09:31 +0900166 print ' g_pLog->EndCall();'
José Fonsecad626cf42008-07-07 07:43:16 +0900167 for type, name in function.args:
168 if type.isoutput():
169 type.wrap_instance(name)
170 if function.type is not Void:
171 function.type.wrap_instance('result')
José Fonsecad626cf42008-07-07 07:43:16 +0900172 if function.type is not Void:
173 print ' return result;'
174 print '}'
175 print
176 print
177