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 | |
| 24 | INT = Intrinsic("INT", "%u") |
| 25 | UINT = Intrinsic("UINT", "%u") |
| 26 | LONG = Intrinsic("LONG", "%li") |
| 27 | ULONG = Intrinsic("ULONG", "%lu") |
| 28 | |
José Fonseca | 0dcc85c | 2008-07-08 07:34:54 +0900 | [diff] [blame] | 29 | BYTE = Intrinsic("BYTE", "0x%02lx") |
| 30 | WORD = Intrinsic("WORD", "0x%04lx") |
| 31 | DWORD = Intrinsic("DWORD", "0x%08lx") |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 32 | |
| 33 | BOOL = Intrinsic("BOOL", "%i") |
| 34 | |
José Fonseca | 4a9c40c | 2008-07-07 18:04:53 +0900 | [diff] [blame] | 35 | LARGE_INTEGER = Intrinsic("LARGE_INTEGER", "0x%llx") |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 36 | |
| 37 | HRESULT = Alias("HRESULT", Int) |
| 38 | |
| 39 | PVOID = Intrinsic("PVOID", "%p") |
| 40 | HWND = Intrinsic("HWND", "%p") |
| 41 | HMONITOR = Intrinsic("HMONITOR", "%p") |
| 42 | |
| 43 | REFIID = Alias("REFIID", PVOID) |
| 44 | GUID = Alias("GUID", PVOID) |
| 45 | |
| 46 | POINT = Struct("POINT", ( |
| 47 | (LONG, "x"), |
| 48 | (LONG, "y"), |
| 49 | )) |
| 50 | |
| 51 | RECT = Struct("RECT", ( |
| 52 | (LONG, "left"), |
| 53 | (LONG, "top"), |
| 54 | (LONG, "right"), |
| 55 | (LONG, "bottom"), |
| 56 | )) |
| 57 | |
| 58 | PALETTEENTRY = Struct("PALETTEENTRY", ( |
| 59 | (BYTE, "peRed"), |
| 60 | (BYTE, "peGreen"), |
| 61 | (BYTE, "peBlue"), |
| 62 | (BYTE, "peFlags"), |
| 63 | )) |
| 64 | |
| 65 | RGNDATA = Struct("RGNDATA", ()) |
| 66 | REFGUID = Alias("REFGUID", PVOID) |
| 67 | |
| 68 | |
| 69 | IUnknown = Interface("IUnknown") |
| 70 | |
| 71 | IUnknown.methods = ( |
| 72 | Method(HRESULT, "QueryInterface", ((REFIID, "riid"), (Pointer(Pointer(Void)), "ppvObj"))), |
| 73 | Method(ULONG, "AddRef", ()), |
| 74 | Method(ULONG, "Release", ()), |
| 75 | ) |
| 76 | |
| 77 | |
| 78 | class 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é Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 93 | print 'static HINSTANCE g_hDll = NULL;' |
José Fonseca | a83fb24 | 2008-07-07 16:55:52 +0900 | [diff] [blame] | 94 | print 'Log * g_pLog = NULL;' |
José Fonseca | d760589 | 2008-07-07 13:09:31 +0900 | [diff] [blame] | 95 | print 'static TCHAR g_szDll[MAX_PATH] = {0};' |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 96 | 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é Fonseca | d760589 | 2008-07-07 13:09:31 +0900 | [diff] [blame] | 102 | #print r' Log * pLog;' |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 103 | print r' switch(fdwReason) {' |
| 104 | print r' case DLL_PROCESS_ATTACH:' |
José Fonseca | d760589 | 2008-07-07 13:09:31 +0900 | [diff] [blame] | 105 | print r' if(!GetSystemDirectory(g_szDll, MAX_PATH))' |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 106 | print r' return FALSE;' |
José Fonseca | d760589 | 2008-07-07 13:09:31 +0900 | [diff] [blame] | 107 | 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é Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 112 | print r' case DLL_THREAD_ATTACH:' |
José Fonseca | d760589 | 2008-07-07 13:09:31 +0900 | [diff] [blame] | 113 | #print r' pLog = new Log(TEXT("%s"));' % self.name |
| 114 | #print r' TlsSetValue(dwTlsIndex, pLog);' |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 115 | print r' return TRUE;' |
| 116 | print r' case DLL_THREAD_DETACH:' |
José Fonseca | d760589 | 2008-07-07 13:09:31 +0900 | [diff] [blame] | 117 | #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é Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 134 | print r' return TRUE;' |
| 135 | print r' }' |
José Fonseca | d760589 | 2008-07-07 13:09:31 +0900 | [diff] [blame] | 136 | print r' (void)hinstDLL;' |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 137 | 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é Fonseca | d760589 | 2008-07-07 13:09:31 +0900 | [diff] [blame] | 144 | 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é Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 149 | 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é Fonseca | a83fb24 | 2008-07-07 16:55:52 +0900 | [diff] [blame] | 156 | print ' if(!g_hDll) {' |
| 157 | print ' g_hDll = LoadLibrary(g_szDll);' |
| 158 | print ' if(!g_hDll)' |
| 159 | print ' ExitProcess(0);' |
| 160 | print ' }' |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 161 | 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é Fonseca | d760589 | 2008-07-07 13:09:31 +0900 | [diff] [blame] | 166 | print ' g_pLog->EndCall();' |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 167 | 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é Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 172 | if function.type is not Void: |
| 173 | print ' return result;' |
| 174 | print '}' |
| 175 | print |
| 176 | print |
| 177 | |