Release 960506
[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 NAMING CONVENTIONS FOR API FUNCTIONS AND TYPES
47 ==============================================
48
49 In order to support both Win16 and Win32 APIs within the same source
50 code, as well as share the include files between the emulator and the
51 library, the following convention must be used in naming all API
52 functions and types. If the Windows API uses the name 'xxx', the Wine
53 code must use:
54
55  - 'xxx16' for the 16-bit version,
56  - 'xxx32' for the 32-bit version when no ASCII/Unicode strings are
57    involved,
58  - 'xxx32A' for the 32-bit version with ASCII strings,
59  - 'xxx32W' for the 32-bit version with Unicode strings.
60
61 You should then use the macros WINELIB_NAME[_AW](xxx) or
62 DECL_WINELIB_TYPE[_AW](xxx) (defined in include/wintypes.h) to define
63 the correct 'xxx' function or type for Winelib. When compiling the
64 emulator, 'xxx' is _not_ defined, meaning that you must always specify
65 explicitly whether you want the 16-bit or 32-bit version.
66
67 Note: if 'xxx' is the same in Win16 and Win32, you can simply use the
68 same name as Windows.
69
70 Examples:
71
72 typedef short INT16;
73 typedef int INT32;
74 DECL_WINELIB_TYPE(INT);
75
76 typedef struct { /* Win32 ASCII data structure */ } WNDCLASS32A;
77 typedef struct { /* Win32 Unicode data structure */ } WNDCLASS32W;
78 typedef struct { /* Win16 data structure */ } WNDCLASS16;
79 DECL_WINELIB_TYPE_AW(WNDCLASS);
80
81 ATOM RegisterClass16( WNDCLASS16 * );
82 ATOM RegisterClass32A( WNDCLASS32A * );
83 ATOM RegisterClass32W( WNDCLASS32W * );
84 #define RegisterClass WINELIB_NAME_AW(RegisterClass)
85
86 The Winelib user can then say:
87
88     INT i;
89     WNDCLASS wc = { ... };
90     RegisterClass( &wc );
91
92 and this will use the correct declaration depending on the definition
93 of the symbols WINELIB16, WINELIB32 and UNICODE.
94
95
96 API ENTRY POINTS
97 ================
98
99 Because Win16 programs use a 16-bit stack and because they can only
100 call 16:16 addressed functions, all API entry points must be at low
101 address offsets and must have the arguments translated and moved to
102 Wines 32-bit stack.  This task is handled by the code in the "if1632"
103 directory.  To define a new API entry point handler you must place a
104 new entry in the appropriate API specification file.  These files are
105 named *.spec.  For example, the API specification file for the USER DLL
106 is contained in the file user.spec.  These entries are processed by
107 the "build" program to create dll_*.s and dll_tab_*.c.  The dll_*.s
108 files contain the entry point code for each API call, and the dll_tab_*.s
109 files contain tables used by relay.c to translate arguments and transfer
110 control to the proper handler.  The format of the *.spec files is
111 documented in the file "tools/build-spec.txt".
112
113
114 DEBUG MESSAGES
115 ==============
116
117 To display a message only during debugging, you normally write something
118 like this:
119
120 #ifdef DEBUG_WIN
121         printf("abc...");
122 #endif
123
124 You can write this shorter (and better) in this way:
125
126         dprintf_win(stddeb,"abc...");
127
128 All symbols of the form dprintf_xxxx are macros defined in include/debug.h .
129 The macro-definitions are generated by the shell-script tools/make_debug. It
130 scans the source code for symbols of this forms and puts the necessary
131 macro definitions in include/debug.h and include/stddebug.h . These macros
132 test for the symbol DEBUG_XXXX (e.g. dprintf_win refers to DEBUG_WIN) being 
133 defined and thus decided whether to actually display the text. If you want
134 to enable specific types of messages, simply put the corresponding
135 #define DEBUG_XXXX in include/stddebug.h . If you want to enable or disable
136 a specific type of message in just one c-source-file, put the corresponding 
137 #define DEBUG_XXXX or #undefine DEBUG_XXXX between #include<stddebug.h> and
138 #include <debug.h> in that specific file. In addition you can change the 
139 types of displayed messages by supplying the "-debugmsg" option to Wine. 
140 If your debugging code is more complex than just printf, you can use the
141 symbols debugging_XXX as well. These are true when XXX is enabled, either
142 permanent or in the command line. So instead of writing
143
144 #ifdef DEBUG_WIN
145         DumpSomeStructure(&str);
146 #endif
147
148 write
149         if(debugging_win)DumpSomeStructure(&str);
150 Don't worry about the inefficiency of the test. If it is permanently 
151 disabled (thus debugging_win is 0 at compile time), the compiler will 
152 eliminate the dead code.
153
154 The file handle "stddeb" is intended for displaying standard informational
155 messages, whereas "stdnimp" is intended for displaying messages concerning
156 not yet implemented functions.
157
158 You have to start tools/make_debug only if you introduced a new macro,
159 e.g.  dprintf_win32s - not if you just changed one of the #define
160 DEBUG_XXX's in include/stddebug.h or in a specific file.