José Fonseca | 9544244 | 2008-07-08 10:32:53 +0900 | [diff] [blame] | 1 | ############################################################################# |
| 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é Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 20 | """windows.h""" |
| 21 | |
| 22 | from base import * |
| 23 | |
José Fonseca | 73f33cc | 2008-07-09 02:17:51 +0900 | [diff] [blame] | 24 | SHORT = Intrinsic("SHORT", "%i") |
| 25 | USHORT = Intrinsic("USHORT", "%u") |
| 26 | INT = Intrinsic("INT", "%i") |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 27 | UINT = Intrinsic("UINT", "%u") |
| 28 | LONG = Intrinsic("LONG", "%li") |
| 29 | ULONG = Intrinsic("ULONG", "%lu") |
José Fonseca | 73f33cc | 2008-07-09 02:17:51 +0900 | [diff] [blame] | 30 | FLOAT = Intrinsic("FLOAT", "%f") |
| 31 | |
| 32 | INT32 = Intrinsic("INT32", "%i") |
| 33 | UINT32 = Intrinsic("UINT32", "%i") |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 34 | |
José Fonseca | 0dcc85c | 2008-07-08 07:34:54 +0900 | [diff] [blame] | 35 | BYTE = Intrinsic("BYTE", "0x%02lx") |
| 36 | WORD = Intrinsic("WORD", "0x%04lx") |
| 37 | DWORD = Intrinsic("DWORD", "0x%08lx") |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 38 | |
| 39 | BOOL = Intrinsic("BOOL", "%i") |
| 40 | |
José Fonseca | 4a9c40c | 2008-07-07 18:04:53 +0900 | [diff] [blame] | 41 | LARGE_INTEGER = Intrinsic("LARGE_INTEGER", "0x%llx") |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 42 | |
| 43 | HRESULT = Alias("HRESULT", Int) |
| 44 | |
| 45 | PVOID = Intrinsic("PVOID", "%p") |
José Fonseca | 73f33cc | 2008-07-09 02:17:51 +0900 | [diff] [blame] | 46 | HANDLE = Intrinsic("HANDLE", "%p") |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 47 | HWND = Intrinsic("HWND", "%p") |
José Fonseca | 73f33cc | 2008-07-09 02:17:51 +0900 | [diff] [blame] | 48 | HDC = Intrinsic("HDC", "%p") |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 49 | HMONITOR = Intrinsic("HMONITOR", "%p") |
| 50 | |
José Fonseca | 8a56d14 | 2008-07-09 12:18:08 +0900 | [diff] [blame^] | 51 | GUID = 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)) |
| 66 | REFGUID = Alias("REFGUID", GUID) |
| 67 | |
| 68 | IID = Alias("IID", GUID) |
| 69 | #REFIID = Alias("REFIID", Pointer(IID)) |
| 70 | REFIID = Alias("REFIID", IID) |
| 71 | |
| 72 | LUID = Struct("LUID", [ |
| 73 | (DWORD, "LowPart"), |
| 74 | (LONG, "HighPart"), |
| 75 | ]) |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 76 | |
| 77 | POINT = Struct("POINT", ( |
| 78 | (LONG, "x"), |
| 79 | (LONG, "y"), |
| 80 | )) |
| 81 | |
| 82 | RECT = Struct("RECT", ( |
| 83 | (LONG, "left"), |
| 84 | (LONG, "top"), |
| 85 | (LONG, "right"), |
| 86 | (LONG, "bottom"), |
| 87 | )) |
| 88 | |
| 89 | PALETTEENTRY = Struct("PALETTEENTRY", ( |
| 90 | (BYTE, "peRed"), |
| 91 | (BYTE, "peGreen"), |
| 92 | (BYTE, "peBlue"), |
| 93 | (BYTE, "peFlags"), |
| 94 | )) |
| 95 | |
José Fonseca | 8a56d14 | 2008-07-09 12:18:08 +0900 | [diff] [blame^] | 96 | |
| 97 | RGNDATAHEADER = Struct("RGNDATAHEADER", [ |
| 98 | (DWORD, "dwSize"), |
| 99 | (DWORD, "iType"), |
| 100 | (DWORD, "nCount"), |
| 101 | (DWORD, "nRgnSize"), |
| 102 | (RECT, "rcBound"), |
| 103 | ]) |
| 104 | |
| 105 | RGNDATA = Struct("RGNDATA", [ |
| 106 | (RGNDATAHEADER, "rdh"), |
| 107 | #(Char, "Buffer[1]"), |
| 108 | ]) |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 109 | |
| 110 | |
| 111 | IUnknown = Interface("IUnknown") |
| 112 | |
| 113 | IUnknown.methods = ( |
| 114 | Method(HRESULT, "QueryInterface", ((REFIID, "riid"), (Pointer(Pointer(Void)), "ppvObj"))), |
| 115 | Method(ULONG, "AddRef", ()), |
| 116 | Method(ULONG, "Release", ()), |
| 117 | ) |
| 118 | |
| 119 | |
| 120 | class 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é Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 135 | print 'static HINSTANCE g_hDll = NULL;' |
José Fonseca | d760589 | 2008-07-07 13:09:31 +0900 | [diff] [blame] | 136 | print 'static TCHAR g_szDll[MAX_PATH] = {0};' |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 137 | 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é Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 143 | print r' switch(fdwReason) {' |
| 144 | print r' case DLL_PROCESS_ATTACH:' |
José Fonseca | d760589 | 2008-07-07 13:09:31 +0900 | [diff] [blame] | 145 | print r' if(!GetSystemDirectory(g_szDll, MAX_PATH))' |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 146 | print r' return FALSE;' |
José Fonseca | d760589 | 2008-07-07 13:09:31 +0900 | [diff] [blame] | 147 | print r' _tcscat(g_szDll, TEXT("\\%s.dll"));' % self.name |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 148 | print r' case DLL_THREAD_ATTACH:' |
| 149 | print r' return TRUE;' |
| 150 | print r' case DLL_THREAD_DETACH:' |
José Fonseca | d760589 | 2008-07-07 13:09:31 +0900 | [diff] [blame] | 151 | print r' return TRUE;' |
| 152 | print r' case DLL_PROCESS_DETACH:' |
José Fonseca | 22aec83 | 2008-07-09 09:38:45 +0900 | [diff] [blame] | 153 | print r' Log::Close();' |
José Fonseca | d760589 | 2008-07-07 13:09:31 +0900 | [diff] [blame] | 154 | print r' if(g_hDll) {' |
| 155 | print r' FreeLibrary(g_hDll);' |
| 156 | print r' g_hDll = NULL;' |
| 157 | print r' }' |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 158 | print r' return TRUE;' |
| 159 | print r' }' |
José Fonseca | d760589 | 2008-07-07 13:09:31 +0900 | [diff] [blame] | 160 | print r' (void)hinstDLL;' |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 161 | 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é Fonseca | d760589 | 2008-07-07 13:09:31 +0900 | [diff] [blame] | 168 | if 1: |
José Fonseca | 22aec83 | 2008-07-09 09:38:45 +0900 | [diff] [blame] | 169 | print ' Log::Close();' |
| 170 | print ' Log::Open(TEXT("%s"));' % self.name |
| 171 | #print ' Log::ReOpen();' |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 172 | 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é Fonseca | a83fb24 | 2008-07-07 16:55:52 +0900 | [diff] [blame] | 179 | print ' if(!g_hDll) {' |
| 180 | print ' g_hDll = LoadLibrary(g_szDll);' |
| 181 | print ' if(!g_hDll)' |
| 182 | print ' ExitProcess(0);' |
| 183 | print ' }' |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 184 | print ' pFunction = (%s)GetProcAddress( g_hDll, "%s");' % (type, function.name) |
| 185 | print ' if(!pFunction)' |
| 186 | print ' ExitProcess(0);' |
José Fonseca | 22aec83 | 2008-07-09 09:38:45 +0900 | [diff] [blame] | 187 | print ' Log::BeginCall("%s");' % (function.name) |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 188 | print ' %spFunction(%s);' % (result, ', '.join([str(name) for type, name in function.args])) |
José Fonseca | 22aec83 | 2008-07-09 09:38:45 +0900 | [diff] [blame] | 189 | print ' Log::EndCall();' |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 190 | 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é Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 195 | if function.type is not Void: |
| 196 | print ' return result;' |
| 197 | print '}' |
| 198 | print |
| 199 | print |
| 200 | |