Release 960521
[wine] / DEVELOPERS-HINTS
1 This is intended to be a document to help new developers get started.
2 Existing developers should feel free to add their comments.
3
4 MEMORY AND SEGMENTS
5 ===================
6
7 NE (Win16) executables consist of multiple segments.  The Wine loader
8 loads each segment into a unique location in the Wine processes memory
9 and assigns a selector to that segment.  Because of this, it's not
10 possible to exchange addresses freely between 16-bit and 32-bit code.
11 Addresses used by 16-bit code are segmented addresses (16:16), formed
12 by a 16-bit selector and a 16-bit offset.  Those used by the Wine code
13 are regular 32-bit linear addresses.
14
15 There's three ways to obtain a segmented pointer:
16   - Allocate a block of memory from the global heap and use
17     WIN16_GlobalLock to get its segmented address.
18   - Allocate a block of memory from a local heap, and build the
19     segmented address from the local heap selector (see the
20     USER_HEAP_* macros for an example of this).
21   - Declare the argument as 'segptr' instead of 'ptr' in the spec file
22     for a given API function.
23
24 Once you have a segmented pointer, it must be converted to a linear
25 pointer before you can use it from 32-bit code.  This can be done with
26 the PTR_SEG_TO_LIN() and PTR_SEG_OFF_TO_LIN() macros.  The linear
27 pointer can then be used freely with standard Unix functions like
28 memcpy() etc. without worrying about 64k boundaries.  Note: there's no
29 easy way to convert back from a linear to a segmented address.
30
31 In most cases, you don't need to worry about segmented address, as the
32 conversion is made automatically by the callback code and the API
33 functions only see linear addresses. However, in some cases it is
34 necessary to manipulate segmented addresses; the most frequent cases
35 are:
36   - API functions that return a pointer
37   - lParam of Windows messages that point to a structure
38   - Pointers contained inside structures accessed by 16-bit code.
39
40 It is usually a good practice to used the type 'SEGPTR' for segmented
41 pointers, instead of something like 'LPSTR' or 'char *'.  As SEGPTR is
42 defined as a DWORD, you'll get a compilation warning if you mistakenly
43 use it as a regular 32-bit pointer.
44
45
46 STRUCTURE PACKING
47 =================
48
49 Under Windows, data structures are tightly packed, i.e. there is no
50 padding between structure members. On the other hand, by default gcc
51 aligns structure members (e.g. WORDs are on a WORD boundary, etc.).
52 This means that a structure like
53
54 struct { BYTE x; WORD y; };
55
56 will take 3 bytes under Windows, but 4 with gcc, because gcc will add a
57 dummy byte between x and y. To have the correct layout for structures
58 used by Windows code, you need to use the WINE_PACKED attribute; so you
59 would declare the above structure like this:
60
61 struct { BYTE x; WORD y WINE_PACKED; };
62
63 You have to do this every time a structure member is not aligned
64 correctly under Windows (i.e. a WORD not on an even address, or a
65 DWORD on a address that is not a multiple of 4).
66
67
68 NAMING CONVENTIONS FOR API FUNCTIONS AND TYPES
69 ==============================================
70
71 In order to support both Win16 and Win32 APIs within the same source
72 code, as well as share the include files between the emulator and the
73 library, the following convention must be used in naming all API
74 functions and types. If the Windows API uses the name 'xxx', the Wine
75 code must use:
76
77  - 'xxx16' for the 16-bit version,
78  - 'xxx32' for the 32-bit version when no ASCII/Unicode strings are
79    involved,
80  - 'xxx32A' for the 32-bit version with ASCII strings,
81  - 'xxx32W' for the 32-bit version with Unicode strings.
82
83 You should then use the macros WINELIB_NAME[_AW](xxx) or
84 DECL_WINELIB_TYPE[_AW](xxx) (defined in include/wintypes.h) to define
85 the correct 'xxx' function or type for Winelib. When compiling the
86 emulator, 'xxx' is _not_ defined, meaning that you must always specify
87 explicitly whether you want the 16-bit or 32-bit version.
88
89 Note: if 'xxx' is the same in Win16 and Win32, you can simply use the
90 same name as Windows.
91
92 Examples:
93
94 typedef short INT16;
95 typedef int INT32;
96 DECL_WINELIB_TYPE(INT);
97
98 typedef struct { /* Win32 ASCII data structure */ } WNDCLASS32A;
99 typedef struct { /* Win32 Unicode data structure */ } WNDCLASS32W;
100 typedef struct { /* Win16 data structure */ } WNDCLASS16;
101 DECL_WINELIB_TYPE_AW(WNDCLASS);
102
103 ATOM RegisterClass16( WNDCLASS16 * );
104 ATOM RegisterClass32A( WNDCLASS32A * );
105 ATOM RegisterClass32W( WNDCLASS32W * );
106 #define RegisterClass WINELIB_NAME_AW(RegisterClass)
107
108 The Winelib user can then say:
109
110     INT i;
111     WNDCLASS wc = { ... };
112     RegisterClass( &wc );
113
114 and this will use the correct declaration depending on the definition
115 of the symbols WINELIB16, WINELIB32 and UNICODE.
116
117
118 API ENTRY POINTS
119 ================
120
121 Because Win16 programs use a 16-bit stack and because they can only
122 call 16:16 addressed functions, all API entry points must be at low
123 address offsets and must have the arguments translated and moved to
124 Wines 32-bit stack.  This task is handled by the code in the "if1632"
125 directory.  To define a new API entry point handler you must place a
126 new entry in the appropriate API specification file.  These files are
127 named *.spec.  For example, the API specification file for the USER DLL
128 is contained in the file user.spec.  These entries are processed by
129 the "build" program to create dll_*.s and dll_tab_*.c.  The dll_*.s
130 files contain the entry point code for each API call, and the dll_tab_*.s
131 files contain tables used by relay.c to translate arguments and transfer
132 control to the proper handler.  The format of the *.spec files is
133 documented in the file "tools/build-spec.txt".
134
135
136 DEBUG MESSAGES
137 ==============
138
139 To display a message only during debugging, you normally write something
140 like this:
141
142 #ifdef DEBUG_WIN
143         printf("abc...");
144 #endif
145
146 You can write this shorter (and better) in this way:
147
148         dprintf_win(stddeb,"abc...");
149
150 All symbols of the form dprintf_xxxx are macros defined in include/debug.h .
151 The macro-definitions are generated by the shell-script tools/make_debug. It
152 scans the source code for symbols of this forms and puts the necessary
153 macro definitions in include/debug.h and include/stddebug.h . These macros
154 test for the symbol DEBUG_XXXX (e.g. dprintf_win refers to DEBUG_WIN) being 
155 defined and thus decided whether to actually display the text. If you want
156 to enable specific types of messages, simply put the corresponding
157 #define DEBUG_XXXX in include/stddebug.h . If you want to enable or disable
158 a specific type of message in just one c-source-file, put the corresponding 
159 #define DEBUG_XXXX or #undefine DEBUG_XXXX between #include<stddebug.h> and
160 #include <debug.h> in that specific file. In addition you can change the 
161 types of displayed messages by supplying the "-debugmsg" option to Wine. 
162 If your debugging code is more complex than just printf, you can use the
163 symbols debugging_XXX as well. These are true when XXX is enabled, either
164 permanent or in the command line. So instead of writing
165
166 #ifdef DEBUG_WIN
167         DumpSomeStructure(&str);
168 #endif
169
170 write
171         if(debugging_win)DumpSomeStructure(&str);
172 Don't worry about the inefficiency of the test. If it is permanently 
173 disabled (thus debugging_win is 0 at compile time), the compiler will 
174 eliminate the dead code.
175
176 The file handle "stddeb" is intended for displaying standard informational
177 messages, whereas "stdnimp" is intended for displaying messages concerning
178 not yet implemented functions.
179
180 You have to start tools/make_debug only if you introduced a new macro,
181 e.g.  dprintf_win32s - not if you just changed one of the #define
182 DEBUG_XXX's in include/stddebug.h or in a specific file.