Release 980329
[wine/multimedia.git] / DEVELOPERS-HINTS
blob40ccb6ac08453e515603c2f598ac81136e64a20c
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
64 MEMORY AND SEGMENTS
65 ===================
67 NE (Win16) executables consist of multiple segments.  The Wine loader
68 loads each segment into a unique location in the Wine processes memory
69 and assigns a selector to that segment.  Because of this, it's not
70 possible to exchange addresses freely between 16-bit and 32-bit code.
71 Addresses used by 16-bit code are segmented addresses (16:16), formed
72 by a 16-bit selector and a 16-bit offset.  Those used by the Wine code
73 are regular 32-bit linear addresses.
75 There are four ways to obtain a segmented pointer:
76   - Use the SEGPTR_* macros in include/heap.h (recommended).
77   - Allocate a block of memory from the global heap and use
78     WIN16_GlobalLock to get its segmented address.
79   - Allocate a block of memory from a local heap, and build the
80     segmented address from the local heap selector (see the
81     USER_HEAP_* macros for an example of this).
82   - Declare the argument as 'segptr' instead of 'ptr' in the spec file
83     for a given API function.
85 Once you have a segmented pointer, it must be converted to a linear
86 pointer before you can use it from 32-bit code.  This can be done with
87 the PTR_SEG_TO_LIN() and PTR_SEG_OFF_TO_LIN() macros.  The linear
88 pointer can then be used freely with standard Unix functions like
89 memcpy() etc. without worrying about 64k boundaries.  Note: there's no
90 easy way to convert back from a linear to a segmented address.
92 In most cases, you don't need to worry about segmented address, as the
93 conversion is made automatically by the callback code and the API
94 functions only see linear addresses. However, in some cases it is
95 necessary to manipulate segmented addresses; the most frequent cases
96 are:
97   - API functions that return a pointer
98   - lParam of Windows messages that point to a structure
99   - Pointers contained inside structures accessed by 16-bit code.
101 It is usually a good practice to used the type 'SEGPTR' for segmented
102 pointers, instead of something like 'LPSTR' or 'char *'.  As SEGPTR is
103 defined as a DWORD, you'll get a compilation warning if you mistakenly
104 use it as a regular 32-bit pointer.
107 STRUCTURE PACKING
108 =================
110 Under Windows, data structures are tightly packed, i.e. there is no
111 padding between structure members. On the other hand, by default gcc
112 aligns structure members (e.g. WORDs are on a WORD boundary, etc.).
113 This means that a structure like
115 struct { BYTE x; WORD y; };
117 will take 3 bytes under Windows, but 4 with gcc, because gcc will add a
118 dummy byte between x and y. To have the correct layout for structures
119 used by Windows code, you need to use the WINE_PACKED attribute; so you
120 would declare the above structure like this:
122 struct { BYTE x; WORD y WINE_PACKED; };
124 You have to do this every time a structure member is not aligned
125 correctly under Windows (i.e. a WORD not on an even address, or a
126 DWORD on a address that is not a multiple of 4).
129 NAMING CONVENTIONS FOR API FUNCTIONS AND TYPES
130 ==============================================
132 In order to support both Win16 and Win32 APIs within the same source
133 code, as well as share the include files between the emulator and the
134 library, the following convention must be used in naming all API
135 functions and types. If the Windows API uses the name 'xxx', the Wine
136 code must use:
138  - 'xxx16' for the 16-bit version,
139  - 'xxx32' for the 32-bit version when no ASCII/Unicode strings are
140    involved,
141  - 'xxx32A' for the 32-bit version with ASCII strings,
142  - 'xxx32W' for the 32-bit version with Unicode strings.
144 You should then use the macros WINELIB_NAME[_AW](xxx) or
145 DECL_WINELIB_TYPE[_AW](xxx) (defined in include/wintypes.h) to define
146 the correct 'xxx' function or type for Winelib. When compiling the
147 emulator, 'xxx' is _not_ defined, meaning that you must always specify
148 explicitly whether you want the 16-bit or 32-bit version.
150 Note: if 'xxx' is the same in Win16 and Win32, you can simply use the
151 same name as Windows.
153 Examples:
155 typedef short INT16;
156 typedef int INT32;
157 DECL_WINELIB_TYPE(INT);
159 typedef struct { /* Win32 ASCII data structure */ } WNDCLASS32A;
160 typedef struct { /* Win32 Unicode data structure */ } WNDCLASS32W;
161 typedef struct { /* Win16 data structure */ } WNDCLASS16;
162 DECL_WINELIB_TYPE_AW(WNDCLASS);
164 ATOM RegisterClass16( WNDCLASS16 * );
165 ATOM RegisterClass32A( WNDCLASS32A * );
166 ATOM RegisterClass32W( WNDCLASS32W * );
167 #define RegisterClass WINELIB_NAME_AW(RegisterClass)
169 The Winelib user can then say:
171     INT i;
172     WNDCLASS wc = { ... };
173     RegisterClass( &wc );
175 and this will use the correct declaration depending on the definition
176 of the symbols WINELIB16, WINELIB32 and UNICODE.
179 API ENTRY POINTS
180 ================
182 Because Win16 programs use a 16-bit stack and because they can only
183 call 16:16 addressed functions, all API entry points must be at low
184 address offsets and must have the arguments translated and moved to
185 Wines 32-bit stack.  This task is handled by the code in the "if1632"
186 directory.  To define a new API entry point handler you must place a
187 new entry in the appropriate API specification file.  These files are
188 named *.spec.  For example, the API specification file for the USER
189 DLL is contained in the file user.spec.  These entries are processed
190 by the "build" program to create an assembly file containing the entry
191 point code for each API call.  The format of the *.spec files is
192 documented in the file "tools/build-spec.txt".
195 DEBUG MESSAGES
196 ==============
198 To display a message only during debugging, you normally write something
199 like this:
201         TRACE(win,"abc...");  or
202         FIXME(win,"abc...");  or
203         WARN(win,"abc...");   or
204         ERR(win,"abc...");
206 depending on the seriousness of the problem. (documentation/degug-msgs
207 explains when it is appropriate to use each of them)
209 These macros are defined in include/debug.h. The macro-definitions are
210 generated by the shell-script tools/make_debug. It scans the source
211 code for symbols of this forms and puts the necessary macro
212 definitions in include/debug.h and include/debugdefs.h. These macros
213 test whether the debugging "channel" associated with the first
214 argument of these macros (win in the above example) is enabled and
215 thus decide whether to actually display the text.  In addition you can
216 change the types of displayed messages by supplying the "-debugmsg"
217 option to Wine.  If your debugging code is more complex than just
218 printf, you can use the symbols TRACE_ON(xxx), WARN_ON(xxx),
219 ERR_ON(xxx) and FIXME_ON(xxx) as well. These are true when channel xxx
220 is enabled, either permanent or in the command line. Thus, you can
221 write:
223         if(TRACE_ON(win))DumpSomeStructure(&str);
225 Don't worry about the inefficiency of the test. If it is permanently 
226 disabled (that is TRACE_ON(win) is 0 at compile time), the compiler will 
227 eliminate the dead code.
229 You have to start tools/make_debug only if you introduced a new macro,
230 e.g.  TRACE(win32).
232 For more info about debugging messages, read:
234 documentation/debug-msgs
237 MORE INFO
238 =========
240 1. There is a FREE online version of the MSDN library (including
241    documentation for the Win32 API) on http://www.microsoft.com/msdn/
243 2. http://www.sonic.net/~undoc/bookstore.html
245 3. In 1993 Dr. Dobbs Journal published a column called "Undocumented Corner".
247 4. You might want to check out BYTE from December 1983 as well :-)