]> git.cworth.org Git - apitrace/blob - windows.py
Have logging methods as regular functions.
[apitrace] / windows.py
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
20 """windows.h"""
21
22 from base import *
23
24 SHORT = Intrinsic("SHORT", "%i")
25 USHORT = Intrinsic("USHORT", "%u")
26 INT = Intrinsic("INT", "%i")
27 UINT = Intrinsic("UINT", "%u")
28 LONG = Intrinsic("LONG", "%li")
29 ULONG = Intrinsic("ULONG", "%lu")
30 FLOAT = Intrinsic("FLOAT", "%f")
31
32 INT32 = Intrinsic("INT32", "%i")
33 UINT32 = Intrinsic("UINT32", "%i")
34
35 BYTE = Intrinsic("BYTE", "0x%02lx")
36 WORD = Intrinsic("WORD", "0x%04lx")
37 DWORD = Intrinsic("DWORD", "0x%08lx")
38
39 BOOL = Intrinsic("BOOL", "%i")
40
41 LARGE_INTEGER = Intrinsic("LARGE_INTEGER", "0x%llx")
42
43 HRESULT = Alias("HRESULT", Int)
44
45 PVOID = Intrinsic("PVOID", "%p")
46 HANDLE = Intrinsic("HANDLE", "%p")
47 HWND = Intrinsic("HWND", "%p")
48 HDC = Intrinsic("HDC", "%p")
49 HMONITOR = Intrinsic("HMONITOR", "%p")
50
51 REFIID = Alias("REFIID", PVOID)
52 GUID = Alias("GUID", PVOID)
53 LUID = Alias("LUID", PVOID)
54
55 POINT = Struct("POINT", (
56   (LONG, "x"),
57   (LONG, "y"),
58 )) 
59
60 RECT = Struct("RECT", (
61   (LONG, "left"),
62   (LONG, "top"),
63   (LONG, "right"), 
64   (LONG, "bottom"), 
65 )) 
66
67 PALETTEENTRY = Struct("PALETTEENTRY", (
68   (BYTE, "peRed"),
69   (BYTE, "peGreen"),
70   (BYTE, "peBlue"), 
71   (BYTE, "peFlags"), 
72 )) 
73
74 RGNDATA = Struct("RGNDATA", ())
75 REFGUID = Alias("REFGUID", PVOID)
76
77
78 IUnknown = Interface("IUnknown")
79
80 IUnknown.methods = (
81         Method(HRESULT, "QueryInterface", ((REFIID, "riid"), (Pointer(Pointer(Void)), "ppvObj"))),
82         Method(ULONG, "AddRef", ()),
83         Method(ULONG, "Release", ()),
84 )
85
86
87 class 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):
102         print 'static HINSTANCE g_hDll = NULL;'
103         print 'static TCHAR g_szDll[MAX_PATH] = {0};'
104         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) {'
110         print r'    switch(fdwReason) {'
111         print r'    case DLL_PROCESS_ATTACH:'
112         print r'        if(!GetSystemDirectory(g_szDll, MAX_PATH))'
113         print r'            return FALSE;'
114         print r'        _tcscat(g_szDll, TEXT("\\%s.dll"));' % self.name
115         print r'    case DLL_THREAD_ATTACH:'
116         print r'        return TRUE;'
117         print r'    case DLL_THREAD_DETACH:'
118         print r'        return TRUE;'
119         print r'    case DLL_PROCESS_DETACH:'
120         print r'        Log::Close();'
121         print r'        if(g_hDll) {'
122         print r'            FreeLibrary(g_hDll);'
123         print r'            g_hDll = NULL;'
124         print r'        }'
125         print r'        return TRUE;'
126         print r'    }'
127         print r'    (void)hinstDLL;'
128         print r'    (void)lpvReserved;'
129         print r'    return TRUE;'
130         print r'}'
131         print
132         for function in self.functions:
133             type = 'P' + function.name
134             print function.prototype() + ' {'
135             if 1:
136                 print '    Log::Close();'
137                 print '    Log::Open(TEXT("%s"));' % self.name
138             #print '    Log::ReOpen();'
139             print '    typedef ' + function.prototype('* %s' % type) + ';'
140             print '    %s pFunction;' % type
141             if function.type is Void:
142                 result = ''
143             else:
144                 print '    %s result;' % function.type
145                 result = 'result = '
146             print '    if(!g_hDll) {'
147             print '        g_hDll = LoadLibrary(g_szDll);'
148             print '        if(!g_hDll)'
149             print '            ExitProcess(0);'
150             print '    }'
151             print '    pFunction = (%s)GetProcAddress( g_hDll, "%s");' % (type, function.name)
152             print '    if(!pFunction)'
153             print '        ExitProcess(0);'
154             print '    Log::BeginCall("%s");' % (function.name)
155             print '    %spFunction(%s);' % (result, ', '.join([str(name) for type, name in function.args]))
156             print '    Log::EndCall();'
157             for type, name in function.args:
158                 if type.isoutput():
159                     type.wrap_instance(name)
160             if function.type is not Void:
161                 function.type.wrap_instance('result')
162             if function.type is not Void:
163                 print '    return result;'
164             print '}'
165             print
166         print
167