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