ICOMization of remaining interfaces.
[wine] / DEVELOPERS-HINTS
1 This document should help new developers get started. Like all of Wine, it
2 is a work in progress.
3
4 SOURCE TREE STRUCTURE
5 =====================
6
7 The Wine source tree is loosely based on the original Windows modules. 
8 Most of the source is concerned with implementing the Wine API, although
9 there are also various tools, documentation, sample Winelib code, and
10 code specific to the binary loader.
11
12 Wine API directories:
13 ---------------------
14
15 KERNEL:
16
17         files/                  - file I/O
18         loader/                 - Win16-, Win32-binary loader
19         memory/                 - memory management
20         msdos/                  - DOS features and BIOS calls (interrupts)
21         scheduler/              - process and thread management
22
23 GDI:
24
25         graphics/               - graphics drivers
26         graphics/x11drv/        - X11 display driver
27         graphics/metafiledrv/   - metafile driver
28         objects/                - logical objects
29
30 USER:
31
32         controls/               - built-in widgets
33         resources/              - built-in dialog resources
34         windows/                - window management
35
36 Other DLLs:
37
38         dlls/*/                 - Other system DLLs implemented by Wine
39
40 Miscellaneous:
41
42         misc/                   - shell, registry, winsock, etc.
43         multimedia/             - multimedia driver
44         ipc/                    - SysV IPC based interprocess communication
45         win32/                  - misc Win32 functions
46
47 Tools:
48 ------
49
50         rc/                     - old resource compiler
51         tools/                  - relay code builder, new rc, etc.
52         documentation/          - some documentation
53
54
55 Binary loader specific directories:
56 -----------------------------------
57
58         debugger/               - built-in debugger
59         if1632/                 - relay code
60         miscemu/                - hardware instruction emulation
61         graphics/win16drv/      - Win16 printer driver
62
63 Winelib specific directories:
64 -----------------------------
65
66         library/                - Required code for programs using Winelib
67         libtest/                - Small samples and tests
68         programs/               - Extended samples / system utilities
69
70 IMPLEMENTING NEW API CALLS
71 ==========================
72
73 This is the simple version, and covers only Win32. Win16 is slightly uglier,
74 because of the Pascal heritage and the segmented memory model.
75
76 All of the Win32 APIs known to Wine are listed in [relay32/*.spec]. An
77 unimplemented call will look like (from gdi32.spec)
78   269 stub PolyBezierTo
79 To implement this call, you need to do the following four things.
80
81 1. Find the appropriate parameters for the call, and add a prototype to
82 [include/windows.h]. In this case, it might look like
83   BOOL WINAPI PolyBezierTo(HDC, LPCVOID, DWORD);
84 If the function has both an ASCII and a Unicode version, you need to
85 define both and add a #define WINELIB_NAME_AW declaration. See below
86 for discussion of function naming conventions.
87   
88 2. Modify the .spec file to tell Wine that the function has an
89 implementation, what the parameters look like and what Wine function
90 to use for the implementation. In Win32, things are simple--everything
91 is 32-bits. However, the relay code handles pointers and pointers to
92 strings slightly differently, so you should use 'str' and 'wstr' for
93 strings, 'ptr' for other pointer types, and 'long' for everything else.
94   269 stdcall PolyBezierTo(long ptr long) PolyBezierTo
95 The 'PolyBezierTo' at the end of the line is which Wine function to use
96 for the implementation.
97
98 3. Implement the function as a stub. Once you add the function to the .spec
99 file, you must add the function to the Wine source before it will link.
100 Add a function called 'PolyBezierTo' somewhere. Good things to put
101 into a stub:
102   o a correct prototype, including the WINAPI
103   o header comments, including full documentation for the function and
104     arguments
105   o A FIXME message and an appropriate return value are good things to
106     put in a stub.
107
108   /************************************************************
109    *  PolyBezierTo   (GDI32.269)  Draw many Bezier curves
110    *
111    * BUGS
112    *   Unimplemented
113    */
114    BOOL WINAPI PolyBezierTo(HDC hdc, LPCVOID p, DWORD count) {
115         /* tell the user they've got a substandard implementation */
116       FIXME(gdi, ":(%x,%p,%d): stub\n", hdc, p, count);
117         /* some programs may be able to compensate, 
118            if they know what happened */
119       SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  
120       return FALSE;    /* error value */
121    }
122
123 4. Implement and test the function.
124
125 MEMORY AND SEGMENTS
126 ===================
127
128 NE (Win16) executables consist of multiple segments.  The Wine loader
129 loads each segment into a unique location in the Wine processes memory
130 and assigns a selector to that segment.  Because of this, it's not
131 possible to exchange addresses freely between 16-bit and 32-bit code.
132 Addresses used by 16-bit code are segmented addresses (16:16), formed
133 by a 16-bit selector and a 16-bit offset.  Those used by the Wine code
134 are regular 32-bit linear addresses.
135
136 There are four ways to obtain a segmented pointer:
137   - Use the SEGPTR_* macros in include/heap.h (recommended).
138   - Allocate a block of memory from the global heap and use
139     WIN16_GlobalLock to get its segmented address.
140   - Allocate a block of memory from a local heap, and build the
141     segmented address from the local heap selector (see the
142     USER_HEAP_* macros for an example of this).
143   - Declare the argument as 'segptr' instead of 'ptr' in the spec file
144     for a given API function.
145
146 Once you have a segmented pointer, it must be converted to a linear
147 pointer before you can use it from 32-bit code.  This can be done with
148 the PTR_SEG_TO_LIN() and PTR_SEG_OFF_TO_LIN() macros.  The linear
149 pointer can then be used freely with standard Unix functions like
150 memcpy() etc. without worrying about 64k boundaries.  Note: there's no
151 easy way to convert back from a linear to a segmented address.
152
153 In most cases, you don't need to worry about segmented address, as the
154 conversion is made automatically by the callback code and the API
155 functions only see linear addresses. However, in some cases it is
156 necessary to manipulate segmented addresses; the most frequent cases
157 are:
158   - API functions that return a pointer
159   - lParam of Windows messages that point to a structure
160   - Pointers contained inside structures accessed by 16-bit code.
161
162 It is usually a good practice to used the type 'SEGPTR' for segmented
163 pointers, instead of something like 'LPSTR' or 'char *'.  As SEGPTR is
164 defined as a DWORD, you'll get a compilation warning if you mistakenly
165 use it as a regular 32-bit pointer.
166
167
168 STRUCTURE PACKING
169 =================
170
171 Under Windows, data structures are tightly packed, i.e. there is no
172 padding between structure members. On the other hand, by default gcc
173 aligns structure members (e.g. WORDs are on a WORD boundary, etc.).
174 This means that a structure like
175
176 struct { BYTE x; WORD y; };
177
178 will take 3 bytes under Windows, but 4 with gcc, because gcc will add a
179 dummy byte between x and y. To have the correct layout for structures
180 used by Windows code, you need to use the WINE_PACKED attribute; so you
181 would declare the above structure like this:
182
183 struct { BYTE x; WORD y WINE_PACKED; };
184
185 You have to do this every time a structure member is not aligned
186 correctly under Windows (i.e. a WORD not on an even address, or a
187 DWORD on a address that is not a multiple of 4).
188
189
190 NAMING CONVENTIONS FOR API FUNCTIONS AND TYPES
191 ==============================================
192
193 In order to support both Win16 and Win32 APIs within the same source
194 code, the following convention must be used in naming all API
195 functions and types. If the Windows API uses the name 'xxx', the Wine
196 code must use:
197
198  - 'xxx16' for the Win16 version,
199  - 'xxx'   for the Win32 version when no ASCII/Unicode strings are
200    involved,
201  - 'xxxA'  for the Win32 version with ASCII strings,
202  - 'xxxW'  for the Win32 version with Unicode strings.
203
204 If the function has both ASCII and Unicode version, you should then
205 use the macros WINELIB_NAME_AW(xxx) or DECL_WINELIB_TYPE_AW(xxx)
206 (defined in include/wintypes.h) to define the correct 'xxx' function
207 or type for Winelib. When compiling Wine itself, 'xxx' is _not_
208 defined, meaning that code inside of Wine must always specify
209 explicitly the ASCII or Unicode version.
210
211 If 'xxx' is the same in Win16 and Win32, you can simply use the same
212 name as Windows, i.e. just 'xxx'.  If 'xxx' is Win16 only, you could
213 use the name as is, but it's preferable to use 'xxx16' to make it
214 clear it is a Win16 function.
215
216 Examples:
217
218 typedef struct { /* Win32 ASCII data structure */ } WNDCLASSA;
219 typedef struct { /* Win32 Unicode data structure */ } WNDCLASSW;
220 typedef struct { /* Win16 data structure */ } WNDCLASS16;
221 DECL_WINELIB_TYPE_AW(WNDCLASS);
222
223 ATOM RegisterClass16( WNDCLASS16 * );
224 ATOM RegisterClassA( WNDCLASSA * );
225 ATOM RegisterClassW( WNDCLASSW * );
226 #define RegisterClass WINELIB_NAME_AW(RegisterClass)
227
228 The Winelib user can then say:
229
230     WNDCLASS wc = { ... };
231     RegisterClass( &wc );
232
233 and this will use the correct declaration depending on the definition
234 of the UNICODE symbol.
235
236
237 API ENTRY POINTS
238 ================
239
240 Because Win16 programs use a 16-bit stack and because they can only
241 call 16:16 addressed functions, all API entry points must be at low
242 address offsets and must have the arguments translated and moved to
243 Wines 32-bit stack.  This task is handled by the code in the "if1632"
244 directory.  To define a new API entry point handler you must place a
245 new entry in the appropriate API specification file.  These files are
246 named *.spec.  For example, the API specification file for the USER
247 DLL is contained in the file user.spec.  These entries are processed
248 by the "build" program to create an assembly file containing the entry
249 point code for each API call.  The format of the *.spec files is
250 documented in the file "tools/build-spec.txt".
251
252
253 DEBUG MESSAGES
254 ==============
255
256 To display a message only during debugging, you normally write something
257 like this:
258
259         TRACE(win,"abc...");  or
260         FIXME(win,"abc...");  or
261         WARN(win,"abc...");   or
262         ERR(win,"abc...");
263
264 depending on the seriousness of the problem. (documentation/degug-msgs
265 explains when it is appropriate to use each of them)
266
267 These macros are defined in include/debug.h. The macro-definitions are
268 generated by the shell-script tools/make_debug. It scans the source
269 code for symbols of this forms and puts the necessary macro
270 definitions in include/debug.h and include/debugdefs.h. These macros
271 test whether the debugging "channel" associated with the first
272 argument of these macros (win in the above example) is enabled and
273 thus decide whether to actually display the text.  In addition you can
274 change the types of displayed messages by supplying the "-debugmsg"
275 option to Wine.  If your debugging code is more complex than just
276 printf, you can use the symbols TRACE_ON(xxx), WARN_ON(xxx),
277 ERR_ON(xxx) and FIXME_ON(xxx) as well. These are true when channel xxx
278 is enabled, either permanent or in the command line. Thus, you can
279 write:
280
281         if(TRACE_ON(win))DumpSomeStructure(&str);
282
283 Don't worry about the inefficiency of the test. If it is permanently 
284 disabled (that is TRACE_ON(win) is 0 at compile time), the compiler will 
285 eliminate the dead code.
286
287 You have to start tools/make_debug only if you introduced a new macro,
288 e.g.  TRACE(win32).
289
290 For more info about debugging messages, read:
291
292 documentation/debug-msgs
293
294
295 MORE INFO
296 =========
297
298 1. There is a FREE online version of the MSDN library (including
299    documentation for the Win32 API) on http://www.microsoft.com/msdn/
300
301 2. http://www.sonic.net/~undoc/bookstore.html
302
303 3. In 1993 Dr. Dobbs Journal published a column called "Undocumented Corner".
304
305 4. You might want to check out BYTE from December 1983 as well :-)
306