Avoid referencing potentially freed token.
[wine/multimedia.git] / DEVELOPERS-HINTS
blob9d0dae5de7af6fb4627749cc7a3e508de9d0650b
1 This document should help new developers get started. Like all of Wine, it
2 is a work in progress.
5 SOURCE TREE STRUCTURE
6 =====================
8 The Wine source tree is loosely based on the original Windows modules. 
9 Most of the source is concerned with implementing the Wine API, although
10 there are also various tools, documentation, sample Winelib code, and
11 code specific to the binary loader.
13 DLLs:
14 -----
15         dlls/                   - All the DLLs implemented by Wine
17                 advapi32/       - crypto, systeminfo, security, eventlogging
18                 avicap32/
19                 avifil32/       - COM object to play AVI files
20                 comctl32/       - common controls
21                 commdlg/        - common dialog boxes (both 16 & 32 bit)
22                 crtdll/         - Old C runtime library
23                 crypt32/
24                 dciman32/
25                 ddraw/          - DirectX ddraw
26                 dinput/         - DirectX dinput 
27                 dplay/          - DirectX dplay
28                 dplayx/         - DirectX dplayx
29                 dsound/         - DirectX dsound
30                 gdi/            - GDI (graphics calls)
31                         enhmetafiledrv/ - enhanced metafile driver
32                         metafiledrv/    - metafile driver
33                         win16drv/       - support for Win16 printer drivers
34                 glu32/
35                 icmp/
36                 imagehlp/       - PE (Portable Executable) Image Helper lib
37                 imm32/
38                 kernel/         - The Windows kernel
39                 lzexpand/       - Liv-Zempel compression/decompression
40                 mpr/            - Multi-Protocol Router (interface to various 
41                                   network transport protocols)
42                 msacm/          - audio compression manager (multimedia) (16 bit)
43                 msacm32/        - audio compression manager (multimedia) (32 bit)
44                 msdmo/
45                 msimg32/
46                 msisys/
47                 msnet/
48                 msrle32
49                 msvcrt/         - 16 bit C runtime library 
50                 msvcrt20/       - 32 bit C runtime library
51                 msvideo/        - 16 bit video manager
52                 netapi32/
53                 ntdll/          - NT implementation of kernel calls
54                 odbc32/
55                 ole32/          - 32 bit OLE 2.0 libraries
56                 oleaut32/       - 32 bit OLE 2.0 automation
57                 olecli/         - 16 bit OLE client
58                 oledlg/         - OLE 2.0 user interface support
59                 olepro32/       - 32 bit OLE 2.0 automation
60                 olesvr/         - 16 bit OLE server
61                 opengl32/       - OpenGL implementation
62                 psapi/          - process status API
63                 qcap/
64                 quartz/
65                 rasapi32/       - remote access server API
66                 richedit/
67                 rpcrt4/
68                 serialui/
69                 setupapi/
70                 shdocvw/
71                 shfolder/
72                 shell32/        - COM object implementing shell views
73                 shlwapi/
74                 sti/
75                 tapi32/         - telephone API
76                 ttydrv/         - TTY display driver (Wine specific)
77                 url
78                 urlmon
79                 user/           - Window management, standard controls, etc.
80                 ver/            - File Installation Library (16 bit)
81                 version/        - File Installation Library (32 bit)
82                 win32s/
83                 win87em/        - 80387 math-emulation
84                 winaspi/        - 16 bit Advanced SCSI Peripheral Interface
85                 winedos/        - DOS features and BIOS calls (interrupts)
86                 wineps/         - Postscript driver (Wine specific)
87                 winmm/          - multimedia (16 & 32 bit)
88                         mciXXX/ - various MCI drivers
89                         midimap/- midi mapper
90                         wavemap/- audio mapper
91                         winearts/ - ARTS audio driver
92                         winejack/ - jack audio server driver
93                         wineoss/- MM driver for OSS systems
94                 winnls/         - National Language Support
95                 winsock/        
96                 wsock32/
97                 winspool/       - Printing & Print Spooler
98                 wintrust/
99                 wnaspi32/       - 32 bit ASPI
100                 x11drv/         - X11 display driver (Wine specific)
102 Winelib programs:
103 -----------------
105         programs/               - All the Winelib programs
106                 avitools/
107                 clock/
108                 cmdlgtst/
109                 control/
110                 expand/
111                 notepad/
112                 osversioncheck/
113                 progman/
114                 regapi/
115                 regedit/
116                 regsvr32/
117                 regtest/
118                 uninstaller/
119                 view/
120                 wcmd/
121                 wineconsole/
122                 winedbg/
123                 winefile/
124                 winemine/
125                 winepath/
126                 winetest/
127                 winhelp/
128                 winver/
131 Support programs, libraries, etc:
132 ---------------------------------
134         documentation/          - some documentation
135         include/                - Windows standard includes
136         library/                - the Wine portability library
137         miscemu/                - the main Wine program
138         ole/                    - global UUIDs static library
139         server/                 - the Wine server
140         tools/                  - relay code builder, new rc, bugreport
141                                   generator, wineconfigurator, etc.
142         unicode/                - Unicode support shared
145 Miscellaneous:
146 --------------
148 Note: these directories will ultimately get moved into their
149 respective dlls.
151         files/                  - KERNEL file I/O
152         if1632/                 - KERNEL relay code
153         loader/                 - KERNEL loader code
154         memory/                 - KERNEL memory management
155         misc/                   - KERNEL shell, registry, winsock, etc.
156         msdos/                  - KERNEL DOS support
157         relay32/                - KERNEL 32-bit relay code
158         scheduler/              - KERNEL process and thread management
159         win32/                  - KERNEL misc Win32 functions
161         graphics/               - GDI graphics drivers
162         objects/                - GDI logical objects
164         controls/               - USER built-in widgets
165         windows/                - USER window management
167         tsx11/                  - thread-safe X11 wrappers (auto generated)
171 IMPLEMENTING NEW API CALLS
172 ==========================
174 This is the simple version, and covers only Win32. Win16 is slightly
175 uglier, because of the Pascal heritage and the segmented memory model.
177 All of the Win32 APIs known to Wine are listed in the .spec file of
178 their corresponding dll. An unimplemented call will look like (from
179 gdi32.spec)
180   269 stub PolyBezierTo
181 To implement this call, you need to do the following four things.
183 1. Find the appropriate parameters for the call, and add a prototype to
184 the correct header file. In this case, that means [include/wingdi.h],
185 and it might look like
186   BOOL WINAPI PolyBezierTo(HDC, LPCVOID, DWORD);
187 If the function has both an ASCII and a Unicode version, you need to
188 define both and add a #define WINELIB_NAME_AW declaration. See below
189 for discussion of function naming conventions.
190   
191 2. Modify the .spec file to tell Wine that the function has an
192 implementation, what the parameters look like and what Wine function
193 to use for the implementation. In Win32, things are simple--everything
194 is 32-bits. However, the relay code handles pointers and pointers to
195 strings slightly differently, so you should use 'str' and 'wstr' for
196 strings, 'ptr' for other pointer types, and 'long' for everything else.
197   269 stdcall PolyBezierTo(long ptr long) PolyBezierTo
198 The 'PolyBezierTo' at the end of the line is which Wine function to use
199 for the implementation.
201 3. Implement the function as a stub. Once you add the function to the .spec
202 file, you must add the function to the Wine source before it will link.
203 Add a function called 'PolyBezierTo' somewhere. Good things to put
204 into a stub:
205   o a correct prototype, including the WINAPI
206   o header comments, including full documentation for the function and
207     arguments (see documentation/README.documentation)
208   o A FIXME message and an appropriate return value are good things to
209     put in a stub.
211   /************************************************************
212    *                    PolyBezierTo   (GDI32.269)  
213    *  
214    * Draw many Bezier curves
215    *
216    * RETURNS
217    *   nonzero on success or zero on faillure
218    *
219    * BUGS
220    *   Unimplemented
221    */
222    BOOL WINAPI PolyBezierTo(HDC hdc,     /* handle to device context */
223                             LPCVOID p,   /* ptr to array of Point structs */
224                             DWORD count  /* nr of points in array */
225    ) 
226    {
227       /* tell the user they've got a substandard implementation */
228       FIXME(gdi, ":(%x,%p,%d): stub\n", hdc, p, count);
230       /* some programs may be able to compensate, 
231        * if they know what happened 
232        */
233       SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  
234       return FALSE;    /* error value */
235    }
237 4. Implement and test the rest of the function.
240 IMPLEMENTING A NEW DLL
241 ======================
243 Generic directions
244 ------------------
246 Apart from writing the set of needed .c files, you also need to do the 
247 following:
249 1.  Create a directory <MyDll> where to store the implementation of
250     the DLL. This directory has to be put under the dlls/ directory.
251     If the DLL exists under Windows as both 16 and 32 bit DLL, you
252     should have a single directory with both implementations.
254 2.  Create the Makefile.in in the ./dlls/<MyDll>/ directory. You can
255     copy an existing Makefile.in from another ./dlls/ subdirectory.
256     You need at least to change the MODULE and C_SRCS macros. 
258 3.  Add the directory in ./configure.ac (in AC_OUTPUT macro at the end
259     of the file to trigger the Makefile generation)
261 4.  Run ./make_dlls in the dlls directory to update Makefile.in in
262     that directory.
264 5.  You can now regenerate ./configure file (with 'make configure')
265     and the various Makefiles (with 'configure; make depend') (run
266     from the top of Wine's tree).
267     You should now have a Makefile file in ./dlls/<MyDll>/
269 6.  Create the .spec file for the DLL exported functions in your
270     directory. Refer to 'Implementation of new API calls' earlier in
271     this document for more information on this part.
273 7.  You can now start adding .c files. For the .h files, if they are
274     standard Windows one, put them in include/. If they are linked to
275     *your* implementation of the dll, put them in your newly created
276     directory.
278 Debug channels
279 --------------
281 If you need to create a new debug channel, just add the
282 WINE_DEFAULT_DEBUG_CHANNEL to your .c file(s), and use them. 
283 All the housekeeping will happen automatically.
285 Resources
286 ---------
288 If you also need to add resources to your DLL, the create the .rc
289 file. Add to your ./dlls/<MyDll>/Makefile.in, in the RC_SRCS macro,
290 the list of .rc files to add to the DLL. See dlls/comctl32/ for an
291 example of this.
293 Thunking
294 --------
296 If you're building a 16 & 32 bit DLLs pair, then from the 32 bit code
297 you might need to call 16 bit routine. The way to do it to add in the
298 code, fragments like:
299 /* ### Start build ### */
300 extern WORD CALLBACK <PREFIX>_CallTo16_word_wwlll(FARPROC16,WORD,WORD,LONG,LONG,LONG);
301 /* ### stop build ### */
302 Where <PREFIX>_ is an internal prefix for your module. The first
303 parameter is always of type FARPROC16. Then, you can get the regular
304 list of parameters. The _word_wwlll indicates the type of return (long
305 or word) and the size of the parameters (here l=>long, w=>word; which
306 maps to WORD,WORD,LONG,LONG,LONG.
307 You can put several functions between the Start/Stop build pair.
309 You can also read the winebuild manpage for more details on this.
311 Then, add to ./dlls/<MyDll>/Makefile.in a line like:
313 EXTRA_OBJS = $(MODULE).glue.o
315 See dlls/winmm/ for an example of this.
317 MEMORY AND SEGMENTS
318 ===================
320 NE (Win16) executables consist of multiple segments.  The Wine loader
321 loads each segment into a unique location in the Wine processes memory
322 and assigns a selector to that segment.  Because of this, it's not
323 possible to exchange addresses freely between 16-bit and 32-bit code.
324 Addresses used by 16-bit code are segmented addresses (16:16), formed
325 by a 16-bit selector and a 16-bit offset.  Those used by the Wine code
326 are regular 32-bit linear addresses.
328 There are four ways to obtain a segmented pointer:
329   - Using the MapLS function (recommended).
330   - Allocate a block of memory from the global heap and use
331     WIN16_GlobalLock to get its segmented address.
332   - Declare the argument as 'segptr' instead of 'ptr' in the spec file
333     for a given API function.
335 Once you have a segmented pointer, it must be converted to a linear
336 pointer before you can use it from 32-bit code.  This can be done with
337 the MapSL function.  The linear pointer can then be used freely with
338 standard Unix functions like memcpy() etc. without worrying about 64k
339 boundaries.  Note: there's no easy way to convert back from a linear
340 to a segmented address.
342 In most cases, you don't need to worry about segmented address, as the
343 conversion is made automatically by the callback code and the API
344 functions only see linear addresses. However, in some cases it is
345 necessary to manipulate segmented addresses; the most frequent cases
346 are:
347   - API functions that return a pointer
348   - lParam of Windows messages that point to a structure
349   - Pointers contained inside structures accessed by 16-bit code.
351 It is usually a good practice to used the type 'SEGPTR' for segmented
352 pointers, instead of something like 'LPSTR' or 'char *'.  As SEGPTR is
353 defined as a DWORD, you'll get a compilation warning if you mistakenly
354 use it as a regular 32-bit pointer.
357 STRUCTURE PACKING
358 =================
360 Under Windows, data structures are tightly packed, i.e. there is no
361 padding between structure members. On the other hand, by default gcc
362 aligns structure members (e.g. WORDs are on a WORD boundary, etc.).
363 This means that a structure like
365 struct { BYTE x; WORD y; };
367 will take 3 bytes under Windows, but 4 with gcc, because gcc will add a
368 dummy byte between x and y. To have the correct layout for structures
369 used by Windows code, you need to embed the struct within two special
370 #include's which will take care of the packing for you:
372 #include "pshpack1.h"
373 struct { BYTE x; WORD y; };
374 #include "poppack1.h"
376 For alignment on a 2-byte boundary, there is a "pshpack2.h", etc.
378 The use of the WINE_PACKED attribute is obsolete. Please remove these 
379 in favour of the above solution. 
380 Using WINE_PACKED, you would declare the above structure like this:
382 struct { BYTE x; WORD y WINE_PACKED; };
384 You had to do this every time a structure member is not aligned
385 correctly under Windows (i.e. a WORD not on an even address, or a
386 DWORD on a address that was not a multiple of 4).
389 NAMING CONVENTIONS FOR API FUNCTIONS AND TYPES
390 ==============================================
392 In order to support both Win16 and Win32 APIs within the same source
393 code, the following convention must be used in naming all API
394 functions and types. If the Windows API uses the name 'xxx', the Wine
395 code must use:
397  - 'xxx16' for the Win16 version,
398  - 'xxx'   for the Win32 version when no ASCII/Unicode strings are
399    involved,
400  - 'xxxA'  for the Win32 version with ASCII strings,
401  - 'xxxW'  for the Win32 version with Unicode strings.
403 If the function has both ASCII and Unicode version, you should then
404 use the macros WINELIB_NAME_AW(xxx) or DECL_WINELIB_TYPE_AW(xxx)
405 (defined in include/windef.h) to define the correct 'xxx' function
406 or type for Winelib. When compiling Wine itself, 'xxx' is _not_
407 defined, meaning that code inside of Wine must always specify
408 explicitly the ASCII or Unicode version.
410 If 'xxx' is the same in Win16 and Win32, you can simply use the same
411 name as Windows, i.e. just 'xxx'.  If 'xxx' is Win16 only, you could
412 use the name as is, but it's preferable to use 'xxx16' to make it
413 clear it is a Win16 function.
415 Examples:
417 typedef struct { /* Win32 ASCII data structure */ } WNDCLASSA;
418 typedef struct { /* Win32 Unicode data structure */ } WNDCLASSW;
419 typedef struct { /* Win16 data structure */ } WNDCLASS16;
420 DECL_WINELIB_TYPE_AW(WNDCLASS);
422 ATOM RegisterClass16( WNDCLASS16 * );
423 ATOM RegisterClassA( WNDCLASSA * );
424 ATOM RegisterClassW( WNDCLASSW * );
425 #define RegisterClass WINELIB_NAME_AW(RegisterClass)
427 The Winelib user can then say:
429     WNDCLASS wc = { ... };
430     RegisterClass( &wc );
432 and this will use the correct declaration depending on the definition
433 of the UNICODE symbol.
436 NAMING CONVENTIONS FOR NON-API FUNCTIONS AND TYPES
437 ==================================================
439 Functions and data which are internal to your code (or at least shouldn't be
440 visible to any Winelib or Windows program) should be preceded by
441 an identifier to the module:
443 Examples:
445 ENUMPRINTERS_GetDWORDFromRegistryA()    (in dlls/winspool/info.c)
446 IAVIFile_fnRelease()                    (in dlls/avifil32/avifile.c)
447 X11DRV_CreateDC()                       (in graphics/x11drv/init.c)
449 if you need prototypes for these, there are a few possibilities:
450 - within same source file only:
451   put the prototypes at the top of your file and mark them as prototypes.
452 - within the same module:
453   create a header file within the subdirectory where that module resides,
454   e.g.  graphics/ddraw_private.h
455 - from a totally different module, or for use in winelib:
456   you should never do that. Only exported APIs can be called across
457   module boundaries.
460 DEBUG MESSAGES
461 ==============
463 To display a message only during debugging, you normally write something
464 like this:
466         TRACE("abc...");  or
467         FIXME("abc...");  or
468         WARN("abc...");   or
469         ERR("abc...");
471 depending on the seriousness of the problem. (documentation/degug-msgs
472 explains when it is appropriate to use each of them). You need to declare
473 the debug channel name at the top of the file (after the includes) using
474 the WINE_DEFAULT_DEBUG_CHANNEL macro, like so:
476         WINE_DEFAULT_DEBUG_CHANNEL(win);
478 If your debugging code is more complex than just printf, you can use 
479 the macros:
481         TRACE_ON(xxx), WARN_ON(xxx), ERR_ON(xxx) and FIXME_ON(xxx) 
483 to test if the given channel is enabled. Thus, you can write:
485         if (TRACE_ON(win)) DumpSomeStructure(&str);
487 Don't worry about the inefficiency of the test. If it is permanently 
488 disabled (that is TRACE_ON(win) is 0 at compile time), the compiler will 
489 eliminate the dead code.
491 For more info about debugging messages, read:
493 documentation/debug-msgs
496 MORE INFO
497 =========
499 1. There is a FREE online version of the MSDN library (including
500    documentation for the Win32 API) on http://www.microsoft.com/msdn/
502 2. http://www.sonic.net/~undoc/bookstore.html
504 3. In 1993 Dr. Dobbs Journal published a column called "Undocumented Corner".
506 4. You might want to check out BYTE from December 1983 as well :-)