Moved memory/environ.c, memory/virtual.c and misc/cpu.c to
[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
5 SOURCE TREE STRUCTURE
6 =====================
7
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.  Note that several of the libraries
12 listed here are "stubbed out", meaning they still need to be implemented.
13
14 DLLs:
15 -----
16         dlls/                   - All the DLLs implemented by Wine
17
18                 advapi32/       - Crypto, systeminfo, security, eventlogging
19                 avicap32/       - AVI capture window class
20                 avifil32/       - COM object to play AVI files
21                 cabinet/        - Cabinet file interface
22                 comcat/         - Component category manager
23                 comctl32/       - Common controls
24                 commdlg/        - Common dialog boxes (both 16 & 32 bit)
25                 crtdll/         - Old C runtime library
26                 crypt32/        - Cryptography
27                 ctl3d/          - 3D Effects for Common GUI Components
28                 d3d8/           - Direct3D (3D graphics)
29                 d3d9/           - Direct3D (3D graphics)
30                 d3dim/          - Direct3D Immediate Mode
31                 d3dx8/          - Direct3D (3D graphics)
32                 dciman32/       - DCI Manager (graphics)
33                 ddraw/          - DirectDraw (graphics)
34                 devenum/        - Device enumeration (part of DirectShow)
35                 dinput/         - DirectInput (device input)
36                 dinput8/        - DirectInput (device input)
37                 dmband/         - DirectMusic
38                 dmcompos/       - DirectMusic
39                 dmime/          - DirectMusic
40                 dmloader/       - DirectMusic
41                 dmscript/       - DirectMusic
42                 dmstyle/        - DirectMusic
43                 dmsynth/        - DirectMusic
44                 dmusic/         - DirectMusic
45                 dmusic32/       - DirectMusic
46                 dplay/          - DirectPlay (networking)
47                 dplayx/         - DirectPlay (networking)
48                 dpnhpast/       - DirectPlay NAT Helper
49                 dsound/         - DirectSound (audio)
50                 gdi/            - GDI (graphics)
51                 gdi/enhmfdrv/   - Enhanced metafile driver
52                 gdi/mfdrv/      - Metafile driver
53                 glu32/          - OpenGL Utility library (graphics)
54                 icmp/           - ICMP protocol (networking)
55                 imagehlp/       - PE (Portable Executable) Image Helper lib
56                 imm32/          - Input Method Manager
57                 iphlpapi/       - IP Helper API
58                 kernel/         - The Windows kernel
59                 lzexpand/       - Lempel-Ziv compression/decompression
60                 mapi32/         - Mail interface
61                 mpr/            - Multi-Protocol Router (networking)
62                 msacm/          - Audio Compression Manager (multimedia)
63                 msdmo/          - DirectX Media Objects
64                 msimg32/        - Gradient and transparency (graphics)
65                 msisys/         - System information
66                 msi/            - Microsoft Installer
67                 msnet32/        - Network interface
68                 msvcrt/         - C runtime library 
69                 msvcrt20/       - C runtime library version 2.0
70                 msvcrtd/        - C runtime library debugging
71                 msvideo/        - 16 bit video manager
72                 mswsock/        - Misc networking
73                 netapi32/       - Network interface
74                 ntdll/          - NT implementation of kernel calls
75                 odbc32/         - Open DataBase Connectivity driver manager
76                 ole32/          - 32 bit OLE 2.0 libraries
77                 oleaut32/       - 32 bit OLE 2.0 automation
78                 olecli/         - 16 bit OLE client
79                 oledlg/         - OLE 2.0 user interface support
80                 olepro32/       - 32 bit OLE 2.0 automation
81                 olesvr/         - 16 bit OLE server
82                 opengl32/       - OpenGL implementation (graphics)
83                 psapi/          - Process Status interface
84                 qcap/           - DirectShow runtime
85                 quartz/         - DirectShow runtime
86                 rasapi32/       - Remote Access Server interface
87                 richedit/       - Rich text editing control
88                 rpcrt4/         - Remote Procedure Call runtime
89                 serialui/       - Serial port property pages
90                 setupapi/       - Setup interface
91                 shdocvw/        - Shell document object and control
92                 shfolder/       - Shell folder service
93                 shell32/        - COM object implementing shell views
94                 shlwapi/        - Shell Light-Weight interface
95                 snmpapi/        - SNMP protocol interface (networking)
96                 sti/            - Still Image service
97                 tapi32/         - Telephone interface
98                 ttydrv/         - TTY display driver (Wine specific)
99                 twain/          - TWAIN Imaging device communications
100                 url/            - Internet shortcut shell extension
101                 urlmon/         - URL Moniker allows binding to a URL (like KIO/gnome-vfs)
102                 user/           - Window management, standard controls, etc.
103                 version/        - File installation library
104                 win32s/         - 32-bit function access for 16-bit systems
105                 winaspi/        - 16 bit Advanced SCSI Peripheral Interface
106                 winedos/        - DOS features and BIOS calls (interrupts) (wine specific)
107                 wineps/         - Postscript driver (Wine specific)
108                 wininet/        - Internet extensions
109                 winmm/          - Multimedia (16 & 32 bit)
110                 winmm/mcianim/  - MCI animation driver
111                 winmm/mciavi/   - MCI video driver
112                 winmm/mcicda/   - MCI audio CD driver
113                 winmm/mciseq/   - MCI MIDI driver
114                 winmm/mciwave/  - MCI wave driver
115                 winmm/midimap/  - MIDI mapper
116                 winmm/wavemap/  - Audio mapper
117                 winmm/winealsa/ - ALSA audio driver
118                 winmm/winearts/ - aRts audio driver
119                 winmm/winejack/ - JACK audio server driver
120                 winmm/wineoss/  - OSS audio driver
121                 winnls/         - National Language Support
122                 winsock/        - Sockets 2.0 (networking)
123                 wsock32/        - Sockets 1.1 (networking)
124                 wintab32/       - Tablet device interface
125                 winspool/       - Printing & Print Spooler
126                 wintrust/       - Trust verification interface
127                 wow32/          - WOW subsystem
128                 x11drv/         - X11 display driver (Wine specific)
129
130 Winelib programs:
131 -----------------
132
133         programs/               - All the Winelib programs
134
135                 avitools/       - AVI information viewer and player
136                 clock/          - Graphical clock
137                 cmdlgtst/       - Common dialog tests
138                 control/        - Control panel
139                 expand/         - Decompress Lempel-Ziv compressed archive
140                 notepad/        - Notepad with RichEdit functionality
141                 osversioncheck/ - Check version of Windows being indicated
142                 progman/        - Program manager
143                 regapi/         - Command line Registry implementation
144                 regedit/        - Registry editor
145                 regsvr32/       - Register COM server
146                 regtest/        - Registry testing program
147                 rpcss/          - RPC services
148                 rundll32/       - Execute DLL functions directly
149                 uninstaller/    - Remove installed programs
150                 view/           - Metafile viewer
151                 wcmd/           - Command line interface
152                 wineconsole/    - Console
153                 winedbg/        - Debugger
154                 winefile/       - File manager
155                 winemine/       - Mine game
156                 winepath/       - Translate between Wine and Unix paths
157                 winhelp/        - Help viewer
158                 winver/         - Windows Version Program
159
160
161 Support programs, libraries, etc:
162 ---------------------------------
163
164         documentation/          - some documentation
165         include/                - Windows standard includes
166         libs/                   - the Wine libraries
167         libs/port/              - portability library
168         libs/unicode/           - Unicode support shared
169         libs/uuid/              - Windows-compatible UUID numbers
170         libs/wine/              - Wine bootstrap library
171         libs/wpp/               - C preprocessor
172         loader/                 - the main Wine loader
173         server/                 - the Wine server
174         tools/                  - relay code builder, resource compiler, etc.
175
176
177 Miscellaneous:
178 --------------
179
180 Note: these directories will ultimately get moved into their
181 respective dlls.
182
183         files/                  - KERNEL file I/O
184         misc/                   - KERNEL shell, registry, winsock, etc.
185         msdos/                  - KERNEL DOS support
186
187         graphics/               - GDI graphics drivers
188         objects/                - GDI logical objects
189
190         controls/               - USER built-in widgets
191         windows/                - USER window management
192
193
194
195 IMPLEMENTING NEW API CALLS
196 ==========================
197
198 This is the simple version, and covers only Win32. Win16 is slightly
199 uglier, because of the Pascal heritage and the segmented memory model.
200
201 All of the Win32 APIs known to Wine are listed in the .spec file of
202 their corresponding dll. An unimplemented call will look like (from
203 gdi32.spec)
204   269 stub PolyBezierTo
205 To implement this call, you need to do the following four things.
206
207 1. Find the appropriate parameters for the call, and add a prototype to
208 the correct header file. In this case, that means [include/wingdi.h],
209 and it might look like
210   BOOL WINAPI PolyBezierTo(HDC, LPCVOID, DWORD);
211 If the function has both an ASCII and a Unicode version, you need to
212 define both and add a #define WINELIB_NAME_AW declaration. See below
213 for discussion of function naming conventions.
214   
215 2. Modify the .spec file to tell Wine that the function has an
216 implementation, what the parameters look like and what Wine function
217 to use for the implementation. In Win32, things are simple--everything
218 is 32-bits. However, the relay code handles pointers and pointers to
219 strings slightly differently, so you should use 'str' and 'wstr' for
220 strings, 'ptr' for other pointer types, and 'long' for everything else.
221   269 stdcall PolyBezierTo(long ptr long) PolyBezierTo
222 The 'PolyBezierTo' at the end of the line is which Wine function to use
223 for the implementation.
224
225 3. Implement the function as a stub. Once you add the function to the .spec
226 file, you must add the function to the Wine source before it will link.
227 Add a function called 'PolyBezierTo' somewhere. Good things to put
228 into a stub:
229   o a correct prototype, including the WINAPI
230   o header comments, including full documentation for the function and
231     arguments (see documentation/README.documentation)
232   o A FIXME message and an appropriate return value are good things to
233     put in a stub.
234
235   /************************************************************
236    *                    PolyBezierTo   (GDI32.269)  
237    *  
238    * Draw many Bezier curves.
239    *
240    * RETURNS
241    *   Success: Non-zero.
242    *   Failure: FALSE. Use GetLastError() to find the error cause.
243    *
244    * BUGS
245    *   Unimplemented
246    */
247    BOOL WINAPI PolyBezierTo(HDC hdc,     /* [In] Device context to draw to */
248                             LPCVOID p,   /* [In] Array of POINT structs */
249                             DWORD count  /* [In] Number of points in p */
250    ) 
251    {
252       /* tell the user they've got a substandard implementation */
253       FIXME(gdi, ":(%x,%p,%d): stub\n", hdc, p, count);
254
255       /* some programs may be able to compensate, 
256        * if they know what happened 
257        */
258       SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  
259       return FALSE;    /* error value */
260    }
261
262 4. Implement and test the rest of the function.
263
264
265 IMPLEMENTING A NEW DLL
266 ======================
267
268 Generic directions
269 ------------------
270
271 Apart from writing the set of needed .c files, you also need to do the 
272 following:
273
274 1.  Create a directory <MyDll> where to store the implementation of
275     the DLL. This directory has to be put under the dlls/ directory.
276     If the DLL exists under Windows as both 16 and 32 bit DLL, you
277     should have a single directory with both implementations.
278
279 2.  Create the Makefile.in in the ./dlls/<MyDll>/ directory. You can
280     copy an existing Makefile.in from another ./dlls/ subdirectory.
281     You need at least to change the MODULE and C_SRCS macros. 
282
283 3.  Add the directory in ./configure.ac (in AC_OUTPUT macro at the end
284     of the file to trigger the Makefile generation)
285
286 4.  Run ./make_dlls in the dlls directory to update Makefile.in in
287     that directory.
288
289 5.  You can now regenerate ./configure file (with 'make configure')
290     and the various Makefiles (with 'configure; make depend') (run
291     from the top of Wine's tree).
292     You should now have a Makefile file in ./dlls/<MyDll>/
293
294 6.  Create the .spec file for the DLL exported functions in your
295     directory. Refer to 'Implementation of new API calls' earlier in
296     this document for more information on this part.
297
298 7.  You can now start adding .c files. For the .h files, if they are
299     standard Windows one, put them in include/. If they are linked to
300     *your* implementation of the dll, put them in your newly created
301     directory.
302
303 Debug channels
304 --------------
305
306 If you need to create a new debug channel, just add the
307 WINE_DEFAULT_DEBUG_CHANNEL to your .c file(s), and use them. 
308 All the housekeeping will happen automatically.
309
310 Resources
311 ---------
312
313 If you also need to add resources to your DLL, then create the .rc
314 file. Add to your ./dlls/<MyDll>/Makefile.in, in the RC_SRCS macro,
315 the list of .rc files to add to the DLL. See dlls/comctl32/ for an
316 example of this.
317
318 Thunking
319 --------
320
321 If you're building a 16 & 32 bit DLLs pair, then from the 32 bit code
322 you might need to call 16 bit routine. The way to do it to add in the
323 code, fragments like:
324 /* ### Start build ### */
325 extern WORD CALLBACK <PREFIX>_CallTo16_word_wwlll(FARPROC16,WORD,WORD,LONG,LONG,LONG);
326 /* ### stop build ### */
327 Where <PREFIX>_ is an internal prefix for your module. The first
328 parameter is always of type FARPROC16. Then, you can get the regular
329 list of parameters. The _word_wwlll indicates the type of return (long
330 or word) and the size of the parameters (here l=>long, w=>word; which
331 maps to WORD,WORD,LONG,LONG,LONG.
332 You can put several functions between the Start/Stop build pair.
333
334 You can also read the winebuild manpage for more details on this.
335
336 Then, add to ./dlls/<MyDll>/Makefile.in a line like:
337
338 EXTRA_OBJS = $(MODULE).glue.o
339
340 See dlls/winmm/ for an example of this.
341
342 MEMORY AND SEGMENTS
343 ===================
344
345 NE (Win16) executables consist of multiple segments.  The Wine loader
346 loads each segment into a unique location in the Wine processes memory
347 and assigns a selector to that segment.  Because of this, it's not
348 possible to exchange addresses freely between 16-bit and 32-bit code.
349 Addresses used by 16-bit code are segmented addresses (16:16), formed
350 by a 16-bit selector and a 16-bit offset.  Those used by the Wine code
351 are regular 32-bit linear addresses.
352
353 There are four ways to obtain a segmented pointer:
354   - Using the MapLS function (recommended).
355   - Allocate a block of memory from the global heap and use
356     WIN16_GlobalLock to get its segmented address.
357   - Declare the argument as 'segptr' instead of 'ptr' in the spec file
358     for a given API function.
359
360 Once you have a segmented pointer, it must be converted to a linear
361 pointer before you can use it from 32-bit code.  This can be done with
362 the MapSL function.  The linear pointer can then be used freely with
363 standard Unix functions like memcpy() etc. without worrying about 64k
364 boundaries.  Note: there's no easy way to convert back from a linear
365 to a segmented address.
366
367 In most cases, you don't need to worry about segmented address, as the
368 conversion is made automatically by the callback code and the API
369 functions only see linear addresses. However, in some cases it is
370 necessary to manipulate segmented addresses; the most frequent cases
371 are:
372   - API functions that return a pointer
373   - lParam of Windows messages that point to a structure
374   - Pointers contained inside structures accessed by 16-bit code.
375
376 It is usually a good practice to used the type 'SEGPTR' for segmented
377 pointers, instead of something like 'LPSTR' or 'char *'.  As SEGPTR is
378 defined as a DWORD, you'll get a compilation warning if you mistakenly
379 use it as a regular 32-bit pointer.
380
381
382 STRUCTURE PACKING
383 =================
384
385 Under Windows, data structures are tightly packed, i.e. there is no
386 padding between structure members. On the other hand, by default gcc
387 aligns structure members (e.g. WORDs are on a WORD boundary, etc.).
388 This means that a structure like
389
390 struct { BYTE x; WORD y; };
391
392 will take 3 bytes under Windows, but 4 with gcc, because gcc will add a
393 dummy byte between x and y. To have the correct layout for structures
394 used by Windows code, you need to embed the struct within two special
395 #include's which will take care of the packing for you:
396
397 #include "pshpack1.h"
398 struct { BYTE x; WORD y; };
399 #include "poppack1.h"
400
401 For alignment on a 2-byte boundary, there is a "pshpack2.h", etc.
402
403 The use of the WINE_PACKED attribute is obsolete. Please remove these 
404 in favour of the above solution. 
405 Using WINE_PACKED, you would declare the above structure like this:
406
407 struct { BYTE x; WORD y WINE_PACKED; };
408
409 You had to do this every time a structure member is not aligned
410 correctly under Windows (i.e. a WORD not on an even address, or a
411 DWORD on a address that was not a multiple of 4).
412
413
414 NAMING CONVENTIONS FOR API FUNCTIONS AND TYPES
415 ==============================================
416
417 In order to support both Win16 and Win32 APIs within the same source
418 code, the following convention must be used in naming all API
419 functions and types. If the Windows API uses the name 'xxx', the Wine
420 code must use:
421
422  - 'xxx16' for the Win16 version,
423  - 'xxx'   for the Win32 version when no ASCII/Unicode strings are
424    involved,
425  - 'xxxA'  for the Win32 version with ASCII strings,
426  - 'xxxW'  for the Win32 version with Unicode strings.
427
428 If the function has both ASCII and Unicode version, you should then
429 use the macros WINELIB_NAME_AW(xxx) or DECL_WINELIB_TYPE_AW(xxx)
430 (defined in include/windef.h) to define the correct 'xxx' function
431 or type for Winelib. When compiling Wine itself, 'xxx' is _not_
432 defined, meaning that code inside of Wine must always specify
433 explicitly the ASCII or Unicode version.
434
435 If 'xxx' is the same in Win16 and Win32, you can simply use the same
436 name as Windows, i.e. just 'xxx'.  If 'xxx' is Win16 only, you could
437 use the name as is, but it's preferable to use 'xxx16' to make it
438 clear it is a Win16 function.
439
440 Examples:
441
442 typedef struct { /* Win32 ASCII data structure */ } WNDCLASSA;
443 typedef struct { /* Win32 Unicode data structure */ } WNDCLASSW;
444 typedef struct { /* Win16 data structure */ } WNDCLASS16;
445 DECL_WINELIB_TYPE_AW(WNDCLASS);
446
447 ATOM RegisterClass16( WNDCLASS16 * );
448 ATOM RegisterClassA( WNDCLASSA * );
449 ATOM RegisterClassW( WNDCLASSW * );
450 #define RegisterClass WINELIB_NAME_AW(RegisterClass)
451
452 The Winelib user can then say:
453
454     WNDCLASS wc = { ... };
455     RegisterClass( &wc );
456
457 and this will use the correct declaration depending on the definition
458 of the UNICODE symbol.
459
460
461 NAMING CONVENTIONS FOR NON-API FUNCTIONS AND TYPES
462 ==================================================
463
464 Functions and data which are internal to your code (or at least shouldn't be
465 visible to any Winelib or Windows program) should be preceded by
466 an identifier to the module:
467
468 Examples:
469
470 ENUMPRINTERS_GetDWORDFromRegistryA()    (in dlls/winspool/info.c)
471 IAVIFile_fnRelease()                    (in dlls/avifil32/avifile.c)
472 X11DRV_CreateDC()                       (in graphics/x11drv/init.c)
473
474 if you need prototypes for these, there are a few possibilities:
475 - within same source file only:
476   put the prototypes at the top of your file and mark them as prototypes.
477 - within the same module:
478   create a header file within the subdirectory where that module resides,
479   e.g.  graphics/ddraw_private.h
480 - from a totally different module, or for use in winelib:
481   you should never do that. Only exported APIs can be called across
482   module boundaries.
483
484
485 DEBUG MESSAGES
486 ==============
487
488 To display a message only during debugging, you normally write something
489 like this:
490
491         TRACE("abc...");  or
492         FIXME("abc...");  or
493         WARN("abc...");   or
494         ERR("abc...");
495
496 depending on the seriousness of the problem. (documentation/debugging.sgml
497 explains when it is appropriate to use each of them). You need to declare
498 the debug channel name at the top of the file (after the includes) using
499 the WINE_DEFAULT_DEBUG_CHANNEL macro, like so:
500
501         WINE_DEFAULT_DEBUG_CHANNEL(win);
502
503 If your debugging code is more complex than just printf, you can use 
504 the macros:
505
506         TRACE_ON(xxx), WARN_ON(xxx), ERR_ON(xxx) and FIXME_ON(xxx) 
507
508 to test if the given channel is enabled. Thus, you can write:
509
510         if (TRACE_ON(win)) DumpSomeStructure(&str);
511
512 Don't worry about the inefficiency of the test. If it is permanently 
513 disabled (that is TRACE_ON(win) is 0 at compile time), the compiler will 
514 eliminate the dead code.
515
516 For more info about debugging messages, read:
517
518 http://www.winehq.org/site/docs/wine-devel/debugging
519
520
521 MORE INFO
522 =========
523
524 1. There is a FREE online version of the MSDN library (including
525    documentation for the Win32 API) on http://msdn.microsoft.com/
526    or http://www.msdn.com/
527
528 2. Windows apilist:  http://www.mentalis.org/apilist/apilist.php
529
530 3. http://www.sonic.net/~undoc/bookstore.html
531
532 4. In 1993 Dr. Dobbs Journal published a column called "Undocumented Corner".
533
534 5. www.geocities.com/SiliconValley/4942/