Release 970629
[wine/multimedia.git] / DEVELOPERS-HINTS
blob7f68ba4bd56cf19cbe659c94ba6c74e43b1fe754
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 #ifdef DEBUG_WIN
202         printf("abc...");
203 #endif
205 You can write this shorter (and better) in this way:
207         dprintf_win(stddeb,"abc...");
209 All symbols of the form dprintf_xxxx are macros defined in include/debug.h .
210 The macro-definitions are generated by the shell-script tools/make_debug. It
211 scans the source code for symbols of this forms and puts the necessary
212 macro definitions in include/debug.h and include/stddebug.h . These macros
213 test for the symbol DEBUG_XXXX (e.g. dprintf_win refers to DEBUG_WIN) being 
214 defined and thus decided whether to actually display the text. If you want
215 to enable specific types of messages, simply put the corresponding
216 #define DEBUG_XXXX in include/stddebug.h . If you want to enable or disable
217 a specific type of message in just one c-source-file, put the corresponding 
218 #define DEBUG_XXXX or #undefine DEBUG_XXXX between #include<stddebug.h> and
219 #include <debug.h> in that specific file. In addition you can change the 
220 types of displayed messages by supplying the "-debugmsg" option to Wine. 
221 If your debugging code is more complex than just printf, you can use the
222 symbols debugging_XXX as well. These are true when XXX is enabled, either
223 permanent or in the command line. So instead of writing
225 #ifdef DEBUG_WIN
226         DumpSomeStructure(&str);
227 #endif
229 write
230         if(debugging_win)DumpSomeStructure(&str);
231 Don't worry about the inefficiency of the test. If it is permanently 
232 disabled (thus debugging_win is 0 at compile time), the compiler will 
233 eliminate the dead code.
235 The file handle "stddeb" is intended for displaying standard informational
236 messages, whereas "stdnimp" is intended for displaying messages concerning
237 not yet implemented functions.
239 You have to start tools/make_debug only if you introduced a new macro,
240 e.g.  dprintf_win32s - not if you just changed one of the #define
241 DEBUG_XXX's in include/stddebug.h or in a specific file.
243 MORE INFO
244 =========
246 1. There is a FREE online version of the MSDN library (including
247    documentation for the Win32 API) on http://www.microsoft.com/msdn/
249 2. http://www.sonic.net/~undoc/bookstore.html
251 3. In 1993 Dr. Dobbs Journal published a column called "Undocumented Corner".
253 4. You might want to check out BYTE from December 1983 as well :-)