Update debug channel usage in DEVELOPER-HINTS.
[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.
12
13 Wine API directories:
14 ---------------------
15
16 KERNEL:
17
18         files/                  - file I/O
19         memory/                 - memory management
20         scheduler/              - process and thread management
21         winnls/                 - National Language Support configuration files
22
23 GDI:
24
25         graphics/               - graphics drivers
26                 x11drv/         - X11 display driver
27                 win16drv/       -> see below 
28                 ttydrv/         - tty display driver
29                 psdrv/          - PostScript graphics driver
30                 metafiledrv/    - metafile driver
31                 enhmetafiledrv/ - enhanced metafile driver
32                 wing/           - WinG (for games) internface
33         objects/                - logical objects
34
35 USER:
36
37         controls/               - built-in widgets
38         resources/              - built-in menu and message box resources
39         windows/                - window management
40
41 Other DLLs:
42
43         dlls/                   - Other system DLLs implemented by Wine
44                 advapi32/       - crypto, systeminfo, security, eventlogging
45                 avicap32/
46                 avifil32/       - COM object to play AVI files
47                 comctl32/       - common controls
48                 commdlg/        - common dialog boxes (both 16 & 32 bit)
49                 crtdll/         - Old C runtime library
50                 crypt32/
51                 dciman32/
52                 ddraw/          - DirectX ddraw
53                 dinput/         - DirectX dinput 
54                 dplay/          - DirectX dplay
55                 dplayx/         - DirectX dplayx
56                 dsound/         - DirectX dsound
57                 glu32/
58                 icmp/
59                 imagehlp/       - PE (Portable Executable) Image Helper lib
60                 imm32/
61                 lzexpand/       - Liv-Zempel compression/decompression
62                 mpr/            - Multi-Protocol Router (interface to various 
63                                   network transport protocols)
64                 msacm/          - audio compression manager (multimedia) (16 bit)
65                 msacm32/        - audio compression manager (multimedia) (32 bit)
66                 msdmo/
67                 msimg32/
68                 msisys/
69                 msnet/
70                 msrle32
71                 msvcrt/         - 16 bit C runtime library 
72                 msvcrt20/       - 32 bit C runtime library
73                 msvideo/        - 16 bit video manager
74                 netapi32/
75                 ntdll/          - NT implementation of kernel calls
76                 odbc32/
77                 ole32/          - 32 bit OLE 2.0 libraries
78                 oleaut32/       - 32 bit OLE 2.0 automation
79                 olecli/         - 16 bit OLE client
80                 oledlg/         - OLE 2.0 user interface support
81                 olepro32/       - 32 bit OLE 2.0 automation
82                 olesvr/         - 16 bit OLE server
83                 opengl32/       - OpenGL implementation
84                 psapi/          - process status API
85                 qcap/
86                 quartz/
87                 rasapi32/       - remote access server API
88                 richedit/
89                 rpcrt4/
90                 serialui/
91                 setupapi/
92                 shdocvw/
93                 shfolder/
94                 shell32/        - COM object implementing shell views
95                 shlwapi/
96                 sti/
97                 tapi32/         - telephone API
98                 url
99                 urlmon
100                 ver/            - File Installation Library (16 bit)
101                 version/        - File Installation Library (32 bit)
102                 win32s/
103                 win87em/        - 80387 math-emulation
104                 winaspi/        - 16 bit Advanced SCSI Peripheral Interface
105                 winedos/        - DOS features and BIOS calls (interrupts)
106                 winmm/          - multimedia (16 & 32 bit)
107                         mciXXX/ - various MCI drivers
108                         midimap/- midi mapper
109                         wavemap/- audio mapper
110                         winearts/ - ARTS audio driver
111                         wineoss/- MM driver for OSS systems
112                 winnls/         - National Language Support
113                 winsock/        
114                 wsock32/
115                 winspool/       - Printing & Print Spooler
116                 wintrust/
117                 wnaspi32/       - 32 bit ASPI
118
119 Miscellaneous:
120
121         misc/                   - shell, registry, winsock, etc.
122         ipc/                    - SysV IPC based interprocess communication
123         win32/                  - misc Win32 functions
124
125 Tools:
126 ------
127
128         tools/                  - relay code builder, new rc, bugreport
129                                   generator, wineconfigurator, etc.
130         documentation/          - some documentation
131
132
133 Binary loader specific directories:
134 -----------------------------------
135
136         if1632/                 - relay code
137         miscemu/                - hardware instruction emulation
138         graphics/win16drv/      - Win16 printer driver
139         server/                 - the main, controlling thread of wine
140         tsx11/                  - thread-safe X11 wrappers (auto generated)
141
142 Winelib specific directories:
143 -----------------------------
144
145         library/                - Required code for programs using Winelib
146         programs/               - Extended samples / system utilities
147
148
149 IMPLEMENTING NEW API CALLS
150 ==========================
151
152 This is the simple version, and covers only Win32. Win16 is slightly uglier,
153 because of the Pascal heritage and the segmented memory model.
154
155 All of the Win32 APIs known to Wine are listed in [relay32/*.spec]. An
156 unimplemented call will look like (from gdi32.spec)
157   269 stub PolyBezierTo
158 To implement this call, you need to do the following four things.
159
160 1. Find the appropriate parameters for the call, and add a prototype to
161 the correct header file. In this case, that means [include/wingdi.h],
162 and it might look like
163   BOOL WINAPI PolyBezierTo(HDC, LPCVOID, DWORD);
164 If the function has both an ASCII and a Unicode version, you need to
165 define both and add a #define WINELIB_NAME_AW declaration. See below
166 for discussion of function naming conventions.
167   
168 2. Modify the .spec file to tell Wine that the function has an
169 implementation, what the parameters look like and what Wine function
170 to use for the implementation. In Win32, things are simple--everything
171 is 32-bits. However, the relay code handles pointers and pointers to
172 strings slightly differently, so you should use 'str' and 'wstr' for
173 strings, 'ptr' for other pointer types, and 'long' for everything else.
174   269 stdcall PolyBezierTo(long ptr long) PolyBezierTo
175 The 'PolyBezierTo' at the end of the line is which Wine function to use
176 for the implementation.
177
178 3. Implement the function as a stub. Once you add the function to the .spec
179 file, you must add the function to the Wine source before it will link.
180 Add a function called 'PolyBezierTo' somewhere. Good things to put
181 into a stub:
182   o a correct prototype, including the WINAPI
183   o header comments, including full documentation for the function and
184     arguments (see documentation/README.documentation)
185   o A FIXME message and an appropriate return value are good things to
186     put in a stub.
187
188   /************************************************************
189    *                    PolyBezierTo   (GDI32.269)  
190    *  
191    * Draw many Bezier curves
192    *
193    * RETURNS
194    *   nonzero on success or zero on faillure
195    *
196    * BUGS
197    *   Unimplemented
198    */
199    BOOL WINAPI PolyBezierTo(HDC hdc,     /* handle to device context */
200                             LPCVOID p,   /* ptr to array of Point structs */
201                             DWORD count  /* nr of points in array */
202    ) 
203    {
204       /* tell the user they've got a substandard implementation */
205       FIXME(gdi, ":(%x,%p,%d): stub\n", hdc, p, count);
206
207       /* some programs may be able to compensate, 
208        * if they know what happened 
209        */
210       SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  
211       return FALSE;    /* error value */
212    }
213
214 4. Implement and test the rest of the function.
215
216
217 IMPLEMENTING A NEW DLL
218 ======================
219
220 Generic directions
221 ------------------
222
223 Apart from writing the set of needed .c files, you also need to do the 
224 following:
225
226 1.  Create a directory <MyDll> where to store the implementation of
227     the DLL. 
228
229     If the DLL exists under Windows as both 16 and 32 bit DLL, you can
230     either create one directory for each, or have a single directory
231     with both implementations. 
232
233     This (those) directory(ies) have to be put under the dlls/
234     directory in Wine tree structure.
235
236 2.  Create the Makefile.in in the ./dlls/<MyDll>/ directory. You can
237     copy an existing Makefile.in from another ./dlls/ subdirectory.
238
239     You need at least to change the MODULE, SPEC_SRCS, and C_SRCS
240     macros. 
241
242 3.  Add the directory (and the generated .o file for the module) in: 
243     + ./configure.in (in AC_OUTPUT macro at the end of the file to
244       trigger the Makefile generation),
245     + ./Makefile.in (in LIBSUBDIRS and LIBOBJS macros)
246     + ./dlls/Makefile.in (in SUBDIRS macro)
247
248 4.  You can now regenerate ./configure file (with 'make configure')
249     and the various Makefiles (with 'configure; make depend') (run
250     from the top of Wine's tree).
251
252     You shall now have a Makefile file in ./dlls/<MyDll>/
253
254 5.  You now need to declare the DLL in the module lists. This is done
255     by adding the corresponding descriptor in ./if1632/builtin.c if
256     your DLL is 16 bit (resp. ./relay32/builtin.c for a 32 bit DLL)
257     (or both if your directory contains the dual 16/32
258     implementations). 
259
260     Note: the name of the descriptor is based on the module name, not
261     on the file name (they are the same in most of the case, but for
262     some DLLs it's not the case).
263
264 6.  You also need to define the loadorder for the created DLL
265     (./wine.ini and ./loader/loadorder.c). Usually, "native,builtin"
266     is ok. If you have written a paired 16/32 bit implementation, don't
267     forget to define it also in those files. 
268
269 7.  Create the .spec file for the DLL export points in your
270     directory. Refer to 'Implementation of new API calls' earlier in
271     this document for more information on this part.
272
273 8.  Don't forget the .cvsignore file. The .cvsignore contain (on a per
274     directory basis) all the files generated by the compilation
275     process, why cvs shall ignore when processing the dir. 
276     *.o is in there by default, but in Wine case you will find:
277     - Makefile (generated from Makefile.in)
278     - *.spec.c: those c files are generated by tools/build from the
279       .spec file
280     - when thunking down to 16 bit DLLs, you'll get some others (.glue.c)
281     - result of .y => .c translation (by yacc or bison)
282     - result of .rc compilation
283     - ...
284     For a simple DLL, listing in .cvsignore Makefile and
285     <MyDll>.spec.c will do. 
286
287 9.  You can now start adding .c files.
288
289 10. For the .h files, if they are standard Windows one, put them in
290     include/. If they are linked to *your* implementation of the DLL,
291     put them in your newly created directory.
292
293 Debug channels
294 --------------
295
296 If you need to create a new debug channel, just add the
297 WINE_DEFAULT_DEBUG_CHANNEL to your .c file(s), and use them. 
298 All the housekeeping will happen automatically.
299
300 Resources
301 ---------
302
303 If you also need to add resources to your DLL, the create the .rc
304 file. Since, the .rc file will be translated into a .s file, and then
305 compiled as a .o file, its basename must be different from the
306 basename of any .c file.
307 Add to your ./dlls/<MyDll>/Makefile.in, in the RC_SRCS macro, the list 
308 of .rc files to add to the DLL. You may also have to add the following
309 directives
310 1/ to tell gnumake to translate .rc into .s files,
311         $(RC_SRCS:.rc=.s): $(WRC)
312 2/ to give some parameters to wrc for helping the translation.
313         WRCEXTRA  = -s -p$(MODULE)
314
315 See dlls/comctl32/ for an example of this.
316
317 Thunking
318 --------
319
320 If you're building a 16 & 32 bit DLLs pair, then from the 32 bit code
321 you might need to call 16 bit routine. The way to do it to add in the
322 code, fragments like:
323 /* ### Start build ### */
324 extern WORD CALLBACK <PREFIX>_CallTo16_word_wwlll(FARPROC16,WORD,WORD,LONG,LONG,LONG);
325 /* ### stop build ### */
326 Where <PREFIX>_ is an internal prefix for your module. The first
327 parameter is always of type FARPROC16. Then, you can get the regular
328 list of parameters. The _word_wwlll indicates the type of return (long
329 or word) and the size of the parameters (here l=>long, w=>word; which
330 maps to WORD,WORD,LONG,LONG,LONG.
331 You can put several functions between the Start/Stop build pair.
332
333 You can also read tools/build.txt for more details on this.
334
335 Then, add to ./dlls/<MyDll>/Makefile.in to the macro GLUE the list of
336 .c files containing the /* ### Start build ### */ directives.
337
338 See dlls/winmm/ for an example of this.
339
340 MEMORY AND SEGMENTS
341 ===================
342
343 NE (Win16) executables consist of multiple segments.  The Wine loader
344 loads each segment into a unique location in the Wine processes memory
345 and assigns a selector to that segment.  Because of this, it's not
346 possible to exchange addresses freely between 16-bit and 32-bit code.
347 Addresses used by 16-bit code are segmented addresses (16:16), formed
348 by a 16-bit selector and a 16-bit offset.  Those used by the Wine code
349 are regular 32-bit linear addresses.
350
351 There are four ways to obtain a segmented pointer:
352   - Use the SEGPTR_* macros in include/heap.h (recommended).
353   - Allocate a block of memory from the global heap and use
354     WIN16_GlobalLock to get its segmented address.
355   - Allocate a block of memory from a local heap, and build the
356     segmented address from the local heap selector (see the
357     USER_HEAP_* macros for an example of this).
358   - Declare the argument as 'segptr' instead of 'ptr' in the spec file
359     for a given API function.
360
361 Once you have a segmented pointer, it must be converted to a linear
362 pointer before you can use it from 32-bit code.  This can be done with
363 the PTR_SEG_TO_LIN() and PTR_SEG_OFF_TO_LIN() macros.  The linear
364 pointer can then be used freely with standard Unix functions like
365 memcpy() etc. without worrying about 64k boundaries.  Note: there's no
366 easy way to convert back from a linear to a segmented address.
367
368 In most cases, you don't need to worry about segmented address, as the
369 conversion is made automatically by the callback code and the API
370 functions only see linear addresses. However, in some cases it is
371 necessary to manipulate segmented addresses; the most frequent cases
372 are:
373   - API functions that return a pointer
374   - lParam of Windows messages that point to a structure
375   - Pointers contained inside structures accessed by 16-bit code.
376
377 It is usually a good practice to used the type 'SEGPTR' for segmented
378 pointers, instead of something like 'LPSTR' or 'char *'.  As SEGPTR is
379 defined as a DWORD, you'll get a compilation warning if you mistakenly
380 use it as a regular 32-bit pointer.
381
382
383 STRUCTURE PACKING
384 =================
385
386 Under Windows, data structures are tightly packed, i.e. there is no
387 padding between structure members. On the other hand, by default gcc
388 aligns structure members (e.g. WORDs are on a WORD boundary, etc.).
389 This means that a structure like
390
391 struct { BYTE x; WORD y; };
392
393 will take 3 bytes under Windows, but 4 with gcc, because gcc will add a
394 dummy byte between x and y. To have the correct layout for structures
395 used by Windows code, you need to embed the struct within two special
396 #include's which will take care of the packing for you:
397
398 #include "pshpack1.h"
399 struct { BYTE x; WORD y; };
400 #include "poppack1.h"
401
402 For alignment on a 2-byte boundary, there is a "pshpack2.h", etc.
403
404 The use of the WINE_PACKED attribute is obsolete. Please remove these 
405 in favour of the above solution. 
406 Using WINE_PACKED, you would declare the above structure like this:
407
408 struct { BYTE x; WORD y WINE_PACKED; };
409
410 You had to do this every time a structure member is not aligned
411 correctly under Windows (i.e. a WORD not on an even address, or a
412 DWORD on a address that was not a multiple of 4).
413
414
415 NAMING CONVENTIONS FOR API FUNCTIONS AND TYPES
416 ==============================================
417
418 In order to support both Win16 and Win32 APIs within the same source
419 code, the following convention must be used in naming all API
420 functions and types. If the Windows API uses the name 'xxx', the Wine
421 code must use:
422
423  - 'xxx16' for the Win16 version,
424  - 'xxx'   for the Win32 version when no ASCII/Unicode strings are
425    involved,
426  - 'xxxA'  for the Win32 version with ASCII strings,
427  - 'xxxW'  for the Win32 version with Unicode strings.
428
429 If the function has both ASCII and Unicode version, you should then
430 use the macros WINELIB_NAME_AW(xxx) or DECL_WINELIB_TYPE_AW(xxx)
431 (defined in include/windef.h) to define the correct 'xxx' function
432 or type for Winelib. When compiling Wine itself, 'xxx' is _not_
433 defined, meaning that code inside of Wine must always specify
434 explicitly the ASCII or Unicode version.
435
436 If 'xxx' is the same in Win16 and Win32, you can simply use the same
437 name as Windows, i.e. just 'xxx'.  If 'xxx' is Win16 only, you could
438 use the name as is, but it's preferable to use 'xxx16' to make it
439 clear it is a Win16 function.
440
441 Examples:
442
443 typedef struct { /* Win32 ASCII data structure */ } WNDCLASSA;
444 typedef struct { /* Win32 Unicode data structure */ } WNDCLASSW;
445 typedef struct { /* Win16 data structure */ } WNDCLASS16;
446 DECL_WINELIB_TYPE_AW(WNDCLASS);
447
448 ATOM RegisterClass16( WNDCLASS16 * );
449 ATOM RegisterClassA( WNDCLASSA * );
450 ATOM RegisterClassW( WNDCLASSW * );
451 #define RegisterClass WINELIB_NAME_AW(RegisterClass)
452
453 The Winelib user can then say:
454
455     WNDCLASS wc = { ... };
456     RegisterClass( &wc );
457
458 and this will use the correct declaration depending on the definition
459 of the UNICODE symbol.
460
461
462 NAMING CONVENTIONS FOR NON-API FUNCTIONS AND TYPES
463 ==================================================
464
465 Functions and data which are internal to your code (or at least shouldn't be
466 visible to any Winelib or Windows program) should be preceded by
467 an identifier to the module:
468
469 Examples:
470
471 ENUMPRINTERS_GetDWORDFromRegistryA()    (in dlls/winspool/info.c)
472 IAVIFile_fnRelease()                    (in dlls/avifil32/avifile.c)
473 X11DRV_CreateDC()                       (in graphics/x11drv/init.c)
474 TIMER_Init()                            (implemented in windows/timer.c,
475                                          used in loader/main.c )
476
477 if you need prototypes for these, there are a few possibilities:
478 - within same source file only:
479   put the prototypes at the top of your file and mark them as prototypes.
480 - within the same module:
481   create a header file within the subdirectory where that module resides,
482   e.g.  graphics/ddraw_private.h
483 - from a totally different module, or for use in winelib:
484   put your header file entry in /include/wine/
485   but be careful not to clutter this directory!
486 under no circumstances, you should add non-api calls to the standard
487 windoze include files. Unfortunately, this is often the case, e.g.
488 the above example of TIMER_Init is defined in include/message.h
489
490
491 API ENTRY POINTS
492 ================
493
494 Because Win16 programs use a 16-bit stack and because they can only
495 call 16:16 addressed functions, all API entry points must be at low
496 address offsets and must have the arguments translated and moved to
497 Wines 32-bit stack.  This task is handled by the code in the "if1632"
498 directory.  To define a new API entry point handler you must place a
499 new entry in the appropriate API specification file.  These files are
500 named *.spec.  For example, the API specification file for the USER
501 DLL is contained in the file user.spec.  These entries are processed
502 by the "build" program to create an assembly file containing the entry
503 point code for each API call.  The format of the *.spec files is
504 documented in the file "tools/build-spec.txt".
505
506
507 DEBUG MESSAGES
508 ==============
509
510 To display a message only during debugging, you normally write something
511 like this:
512
513         TRACE("abc...");  or
514         FIXME("abc...");  or
515         WARN("abc...");   or
516         ERR("abc...");
517
518 depending on the seriousness of the problem. (documentation/degug-msgs
519 explains when it is appropriate to use each of them). You need to declare
520 the debug channel name at the top of the file (after the includes) using
521 the WINE_DEFAULT_DEBUG_CHANNEL macro, like so:
522
523         WINE_DEFAULT_DEBUG_CHANNEL(win);
524
525 If your debugging code is more complex than just printf, you can use 
526 the macros:
527
528         TRACE_ON(xxx), WARN_ON(xxx), ERR_ON(xxx) and FIXME_ON(xxx) 
529
530 to test if the given channel is enabled. Thus, you can write:
531
532         if (TRACE_ON(win)) DumpSomeStructure(&str);
533
534 Don't worry about the inefficiency of the test. If it is permanently 
535 disabled (that is TRACE_ON(win) is 0 at compile time), the compiler will 
536 eliminate the dead code.
537
538 For more info about debugging messages, read:
539
540 documentation/debug-msgs
541
542
543 MORE INFO
544 =========
545
546 1. There is a FREE online version of the MSDN library (including
547    documentation for the Win32 API) on http://www.microsoft.com/msdn/
548
549 2. http://www.sonic.net/~undoc/bookstore.html
550
551 3. In 1993 Dr. Dobbs Journal published a column called "Undocumented Corner".
552
553 4. You might want to check out BYTE from December 1983 as well :-)