Fix the case of product and company names.
[wine] / documentation / architecture.sgml
1   <chapter id="architecture">
2     <title>Overview</title>
3     <para>Brief overview of Wine's architecture...</para>
4
5     <sect1 id="basic-overview">
6       <title>Basic Overview</title>
7
8       <para>
9         Written by &name-ove-kaaven; <email>&email-ove-kaaven;</email>
10       </para>
11
12       <para>
13         With the fundamental architecture of Wine stabilizing, and
14         people starting to think that we might soon be ready to
15         actually release this thing, it may be time to take a look at
16         how Wine actually works and operates.
17       </para>
18
19       <sect2>
20         <title>Wine Overview</title>
21         <para>
22           Wine is often used as a recursive acronym, standing for
23           "Wine Is Not an Emulator". Sometimes it is also known to be
24           used for "Windows Emulator". In a way, both meanings are
25           correct, only seen from different perspectives. The first
26           meaning says that Wine is not a virtual machine, it does not
27           emulate a CPU, and you are not supposed to install 
28           Windows nor any Windows device drivers on top of it; rather,
29           Wine is an implementation of the Windows API, and can be
30           used as a library to port Windows applications to Unix. The
31           second meaning, obviously, is that to Windows binaries
32           (<filename>.exe</filename> files), Wine does look like
33           Windows, and emulates its behaviour and quirks rather
34           closely.
35         </para>
36         <note>
37           <title>Note</title>
38           <para>
39             The "Emulator" perspective should not be thought of as if
40             Wine is a typical inefficient emulation layer that means
41             Wine can't be anything but slow - the faithfulness to the
42             badly designed Windows API may of course impose a minor
43             overhead in some cases, but this is both balanced out by
44             the higher efficiency of the Unix platforms Wine runs on,
45             and that other possible abstraction libraries (like Motif,
46             GTK+, CORBA, etc) has a runtime overhead typically
47             comparable to Wine's.
48           </para>
49         </note>
50       </sect2>
51
52       <sect2>
53         <title>Win16 and Win32</title>
54         <para>
55           Win16 and Win32 applications have different requirements;
56           for example, Win16 apps expect cooperative multitasking
57           among themselves, and to exist in the same address space,
58           while Win32 apps expect the complete opposite, i.e.
59           preemptive multitasking, and separate address spaces.
60         </para>
61         <para>
62           Wine now deals with this issue by launching a separate Wine
63           process for each Win32 process, but not for Win16 tasks.
64           Win16 tasks are now run as different intersynchronized
65           threads in the same Wine process; this Wine process is
66           commonly known as a <firstterm>WOW</firstterm> process,
67           referring to a similar mechanism used by Windows NT.
68           Synchronization between the Win16 tasks running in the WOW
69           process is normally done through the Win16 mutex - whenever
70           one of them is running, it holds the Win16 mutex, keeping
71           the others from running. When the task wishes to let the
72           other tasks run, the thread releases the Win16 mutex, and
73           one of the waiting threads will then acquire it and let its
74           task run.
75         </para>
76       </sect2>
77
78       <sect2>
79         <title>The Wine server</title>
80         <para>
81           The Wine server is among the most confusing concepts in Wine.
82           What is its function in Wine? Well, to be brief, it provides
83           Inter-Process Communication (IPC), synchronization, and
84           process/thread management. When the wineserver launches, it
85           creates a Unix socket for the current host based on (see below)
86           your home directory's <filename>.wine</filename> subdirectory (or
87           wherever the <constant>WINEPREFIX</constant> environment
88           variable points) - all Wine processes launched later
89           connects to the wineserver using this socket. (If a
90           wineserver was not already running, the first Wine process
91           will start up the wineserver in auto-terminate mode (i.e.
92           the wineserver will then terminate itself once the last Wine
93           process has terminated).)
94         </para>
95         <para>
96           In earlier versions of Wine the master socket mentioned
97           above was actually created in the configuration directory;
98           either your home directory's <filename>/wine</filename> 
99           subdirectory or wherever the <constant>WINEPREFIX</constant>
100           environment variable points>.  Since that might not be possible
101           the socket is actually created within the <filename>/tmp</filename>
102           directory with a name that reflects the configuration directory.
103           This means that there can actually be several separate copies of
104           the wineserver running; one per combination of user and
105           configuration directory.  Note that you should not have several
106           users using the same configuration directory at the same time;
107           they will have different copies of the wineserver running and
108           this could well lead to problems with the registry information
109           that they are sharing.
110         </para>
111         <para>
112           Every thread in each Wine process has its own request
113           buffer, which is shared with the wineserver. When a thread
114           needs to synchronize or communicate with any other thread or
115           process, it fills out its request buffer, then writes a
116           command code through the socket. The wineserver handles the
117           command as appropriate, while the client thread waits for a
118           reply. In some cases, like with the various
119           <function>WaitFor</function> synchronization primitives, the
120           server handles it by marking the client thread as waiting
121           and does not send it a reply before the wait condition has
122           been satisfied.
123         </para>
124         <para>
125           The wineserver itself is a single and separate process and
126           does not have its own threading - instead, it is built on
127           top of a large <function>poll()</function> loop that alerts
128           the wineserver whenever anything happens, such as a client
129           having sent a command, or a wait condition having been satisfied.
130           There is thus no danger of race conditions inside the
131           wineserver itself - it is often called upon to do operations
132           that look completely atomic to its clients.
133         </para>
134         <para>
135           Because the wineserver needs to manage processes, threads,
136           shared handles, synchronization, and any related issues, all
137           the clients' Win32 objects are also managed by the
138           wineserver, and the clients must send requests to the
139           wineserver whenever they need to know any Win32 object
140           handle's associated Unix file descriptor (in which case the
141           wineserver duplicates the file descriptor, transmits it to
142           the client, and leaves it to the client to close the duplicate
143           when the client has finished with it).
144         </para>
145       </sect2>
146
147       <sect2>
148         <title>The Service Thread</title>
149         <para>
150           The Wine server cannot do everything that needs to be done
151           behind the application's back, considering that it's not
152           threaded (so cannot do anything that would block or take any
153           significant amount of time), nor does it share the address
154           space of its client threads. Thus, a special event loop also
155           exists in each Win32 process' own address space, but handled
156           like one of the process' own threads. This special thread is
157           called the <firstterm>service thread</firstterm>, and does
158           things that it wouldn't be appropriate for the wineserver to
159           do. For example, it can call the application's asynchronous
160           system timer callbacks every time a timer event is signalled
161           (the wineserver handles the signalling, of course).
162         </para>
163         <para>
164           One important function of the service thread is to support
165           the X11 driver's event loop. Whenever an event arrives from
166           the X server, the service thread wakes up and sees the
167           event, processes it, and posts messages into the
168           application's message queues as appropriate. But this
169           function is not unique - any number of Wine core components
170           can install their own handlers into the service thread as
171           necessary, whenever they need to do something independent of
172           the application's own event loop. (At the moment, this
173           includes, but is not limited to, multimedia timers, serial
174           comms, and winsock async selects.)
175         </para>
176         <para>
177           The implementation of the service thread is in
178           <filename>scheduler/services.c</filename>.
179         </para>
180       </sect2>
181
182       <sect2>
183         <title>Relays, Thunks, and DLL descriptors</title>
184         <para>
185           Loading a Windows binary into memory isn't that hard by
186           itself, the hard part is all those various DLLs and entry
187           points it imports and expects to be there and function as
188           expected; this is, obviously, what the entire Wine
189           implementation is all about. Wine contains a range of DLL
190           implementations. Each of the implemented (or
191           half-implemented) DLLs (which can be found in the
192           <filename>dlls/</filename> directory) need to make
193           themselves known to the Wine core through a DLL descriptor.
194           These descriptors point to such things as the DLL's
195           resources and the entry point table.
196         </para>
197         <para>
198           The DLL descriptor and entry point table is generated by the
199           <command>winebuild</command> tool (previously just named
200           <command>build</command>), taking DLL specification files
201           with the extension <filename>.spec</filename> as input. The
202           output file contains a global constructor that automatically
203           registers the DLL's descriptor with the Wine core at
204           runtime.
205         </para>
206         <para>
207           Once an application module wants to import a DLL, Wine will
208           look through its list of registered DLLs (if it's not
209           registered, it will look for it on disk). (Failing that, it
210           will look for a real Windows <filename>.DLL</filename> file
211           to use, and look through its imports, etc.) To resolve the
212           module's imports, Wine looks through the entry point table
213           and finds if it's defined there. (If not, it'll emit the
214           error "No handler for ...", which, if the application called
215           the entry point, is a fatal error.)
216         </para>
217         <para>
218           Since Wine is 32-bit code itself, and if the compiler
219           supports Windows' calling convention, <type>stdcall</type>
220           (<command>gcc</command> does), Wine can resolve imports into
221           Win32 code by substituting the addresses of the Wine
222           handlers directly without any thunking layer in between.
223           This eliminates the overhead most people associate with
224           "emulation", and is what the applications expect anyway.
225         </para>
226         <para>
227           However, if the user specified <parameter>--debugmsg
228             +relay</parameter>, a thunk layer is inserted between the
229           application imports and the Wine handlers; this layer is
230           known as "relay" because all it does is print out the
231           arguments/return values (by using the argument lists in the
232           DLL descriptor's entry point table), then pass the call on,
233           but it's invaluable for debugging misbehaving calls into
234           Wine code. A similar mechanism also exists between Windows
235           DLLs - Wine can optionally insert thunk layers between them,
236           by using <parameter>--debugmsg +snoop</parameter>, but since
237           no DLL descriptor information exists for non-Wine DLLs, this
238           is less reliable and may lead to crashes.
239         </para>
240         <para>
241           For Win16 code, there is no way around thunking - Wine needs
242           to relay between 16-bit and 32-bit code. These thunks switch
243           between the app's 16-bit stack and Wine's 32-bit stack,
244           copies and converts arguments as appropriate, and handles
245           the Win16 mutex. Suffice to say that the kind of intricate
246           stack content juggling this results in, is not exactly
247           suitable study material for beginners.
248         </para>
249       </sect2>
250
251       <sect2>
252         <title>Core and non-core DLLs</title>
253
254 <!-- FIXME: Should do this without the .jpg (AJ)
255         <para>
256           This slide (by Marcus Meissner of Caldera Systems, shown at
257           the Comdex 99) shows how Wine is meant to fit into the
258           Windows DLL model.
259           <mediaobject>
260             <imageobject>
261               <imagedata fileref="arch-layout.jpg" format="jpg">
262             </imageobject>
263           </mediaobject>
264         </para>
265 FIXME -->
266
267         <para>
268           Wine must at least completely replace the "Big Three" DLLs
269           (KERNEL/KERNEL32, GDI/GDI32, and USER/USER32), which all
270           other DLLs are layered on top of. But since Wine is (for
271           various reasons) leaning towards the NT way of implementing
272           things, the NTDLL is another core DLL to be implemented in
273           Wine, and many KERNEL32 and ADVAPI32 features will be
274           implemented through the NTDLL. The wineserver and the
275           service thread provide the backbone for the implementation
276           of these core DLLs, and integration with the X11 driver
277           (which provides GDI/GDI32 and USER/USER32 functionality
278           along with the Windows standard controls). All non-core
279           DLLs, on the other hand, are expected to only use routines
280           exported by other DLLs (and none of these backbone services
281           directly), to keep the code base as tidy as possible. An
282           example of this is COMCTL32 (Common Controls), which should
283           only use standard GDI32- and USER32-exported routines.
284         </para>
285       </sect2>
286     </sect1>
287
288     <sect1 id="module-overview">
289       <title>Module Overview</title>
290
291       <para>
292         written by (???)
293       </para>
294       <para>
295         (Extracted from <filename>wine/documentation/internals</filename>)
296       </para>
297
298       <sect2>
299         <title>KERNEL Module</title>
300
301         <para>Needs some content...</para>
302       </sect2>
303
304       <sect2>
305         <title>GDI Module</title>
306
307         <sect3>
308           <title>X Windows System interface</title>
309
310           <para>
311             The X libraries used to implement X clients (such as Wine)
312             do not work properly if multiple threads access the same
313             display concurrently. It is possible to compile the X
314             libraries to perform their own synchronization (initiated
315             by calling <function>XInitThreads()</function>). However,
316             Wine does not use this approach. Instead Wine performs its
317             own synchronization by putting a wrapper around every X
318             call that is used. This wrapper protects library access
319             with a critical section, and also arranges things so that
320             X libraries compiled without <option>-D_REENTRANT</option>
321             (eg. with global <varname>errno</varname> variable) will
322             work with Wine.
323           </para>
324           <para>
325             To make this scheme work, all calls to X must use the
326             proper wrapper functions (or do their own synchronization
327             that is compatible with the wrappers). The wrapper for a
328             function <function>X...()</function> is calles
329             <function>TSX...()</function> (for "Thread Safe X ...").
330             So for example, instead of calling
331             <function>XOpenDisplay()</function> in the code,
332             <function>TSXOpenDisplay()</function> must be used.
333             Likewise, X header files that contain function prototypes
334             are wrapped, so that eg. <filename>"ts_xutil.h"</filename>
335             must be included rather than
336             <filename>&lt;X11/Xutil.h&gt;</filename>. It is important
337             that this scheme is used everywhere to avoid the
338             introduction of nondeterministic and hard-to-find errors
339             in Wine.
340           </para>
341           <para>
342             The code for the thread safe X wrappers is contained in
343             the <filename>tsx11/</filename> directory and in
344             <filename>include/ts*.h</filename>. To use a new (ie. not
345             previously used) X function in Wine, a new wrapper must be
346             created. The wrappers are generated (semi-)automatically
347             from the X11R6 includes using the
348             <filename>tools/make_X11wrappers</filename> perl script.
349             In simple cases it should be enough to add the name of the
350             new function to the list in
351             <filename>tsx11/X11_calls</filename>; if this does not
352             work the wrapper must be added manually to the
353             <filename>make_X11wrappers</filename> script. See comments
354             in <filename>tsx11/X11_calls</filename> and
355             <filename>tools/make_X11wrappers</filename> for further
356             details.
357           </para>
358         </sect3>
359       </sect2>
360
361       <sect2>
362         <title>USER Module</title>
363
364         <para>
365           USER implements windowing and messaging subsystems. It also
366           contains code for common controls and for other
367           miscellaneous  stuff (rectangles, clipboard, WNet, etc).
368           Wine USER code is  located in <filename>windows/</filename>,
369           <filename>controls/</filename>, and
370           <filename>misc/</filename> directories.
371         </para>
372
373         <sect3>
374           <title>Windowing subsystem</title>
375
376           <para><filename>windows/win.c</filename></para>
377           <para><filename>windows/winpos.c</filename></para>
378           <para>
379             Windows are arranged into parent/child hierarchy with one
380             common ancestor for all windows (desktop window). Each
381             window structure contains a pointer to the immediate
382             ancestor (parent window if <constant>WS_CHILD</constant>
383             style bit is set), a pointer to the sibling (returned by
384             <function>GetWindow(..., GW_NEXT)</function>), a pointer
385             to the owner  window (set only for popup window if it was
386             created with valid  <varname>hwndParent</varname>
387             parameter), and a pointer to the first child window
388             (<function>GetWindow(.., GW_CHILD)</function>). All popup
389             and non-child windows are therefore placed in the first
390             level of this hierarchy and their ancestor link
391             (<varname>wnd-&gt;parent</varname>) points to the desktop
392             window.
393           </para>
394           <screen>
395    Desktop window                       - root window
396     |     \      `-.
397     |      \        `-.
398    popup -&gt; wnd1  -&gt;  wnd2                - top level windows    
399     |       \   `-.      `-.
400     |        \     `-.      `-.
401    child1  child2 -&gt; child3  child4     - child windows
402           </screen>
403           <para>
404             Horizontal arrows denote sibling relationship, vertical
405             lines - ancestor/child. To summarize, all windows with the
406             same immediate ancestor are sibling windows, all windows
407             which do not have desktop as their immediate ancestor are
408             child windows. Popup windows behave as topmost top-level
409             windows unless they are owned. In this case the only
410             requirement is that they must precede their owners in the
411             top-level sibling list (they are not topmost). Child
412             windows are confined to the client area of their parent
413             windows (client area is where window gets to do its own
414             drawing, non-client area consists of caption, menu,
415             borders, intrinsic scrollbars, and
416             minimize/maximize/close/help buttons). 
417           </para>
418           <para>
419             Another fairly important concept is
420             <firstterm>z-order</firstterm>. It is derived from the
421             ancestor/child hierarchy and is used to determine
422             "above/below" relationship. For instance, in the example
423             above, z-order is
424           </para>
425           <screen>
426 child1-&gt;popup-&gt;child2-&gt;child3-&gt;wnd1-&gt;child4-&gt;wnd2-&gt;desktop.
427           </screen>
428           <para>
429             Current  active window ("foreground window" in Win32) is
430             moved to the front of z-order unless its top-level
431             ancestor owns popup windows.
432           </para>
433           <para>
434             All these issues are dealt with (or supposed to be) in
435             <filename>windows/winpos.c</filename> with
436             <function>SetWindowPos()</function> being the primary
437             interface to the window manager.
438           </para>
439           <para>
440             Wine specifics: in default and managed mode each top-level
441             window gets its own X counterpart with desktop window
442             being basically a fake stub. In desktop mode, however,
443             only desktop window has an X window associated with it.
444             Also, <function>SetWindowPos()</function> should
445             eventually be implemented via
446             <function>Begin/End/DeferWindowPos()</function> calls and
447             not the other way around.
448           </para>
449
450           <sect4>
451             <title>Visible region, clipping region and update region</title>
452
453             <para><filename>windows/dce.c</filename></para>
454             <para><filename>windows/winpos.c</filename></para>
455             <para><filename>windows/painting.c</filename></para>
456
457             <screen>
458     ________________________
459    |_________               |  A and B are child windows of C
460    |    A    |______        | 
461    |         |      |       |
462    |---------'      |       |
463    |   |      B     |       |
464    |   |            |       |
465    |   `------------'       |
466    |                   C    |
467    `------------------------'
468             </screen>
469             <para>
470               Visible region determines which part of the window is
471               not obscured by other windows. If a window has the
472               <constant>WS_CLIPCHILDREN</constant> style then all
473               areas below its children are considered invisible.
474               Similarly, if the <constant>WS_CLIPSIBLINGS</constant>
475               bit is in effect then all areas obscured by its siblings
476               are invisible. Child windows are always clipped by the
477               boundaries of their parent windows.
478             </para>
479             <para>
480               B has a <constant>WS_CLIPSIBLINGS</constant> style:
481             </para>
482             <screen>
483    .          ______ 
484    :         |      |
485    |   ,-----'      |
486    |   |      B     | - visible region of B
487    |   |            |
488    :   `------------'
489             </screen>
490             <para>
491               When the program requests a <firstterm>display
492                 context</firstterm> (DC) for a window it  can specify
493               an optional clipping region that further restricts the
494               area where the graphics output can appear. This area is
495               calculated as an intersection of the visible region and
496               a clipping region. 
497             </para>
498             <para>
499               Program asked for a DC with a clipping region:
500             </para>
501             <screen>
502           ______
503       ,--|--.   |     .    ,--. 
504    ,--+--'  |   |     :   _:  |
505    |  |   B |   |  =&gt; |  |    | - DC region where the painting will
506    |  |     |   |     |  |    |   be visible
507    `--|-----|---'     :  `----'
508       `-----'
509             </screen>
510             <para>
511               When the window manager detects that some part of the window
512               became visible it adds this area to the update region of this
513               window and then generates <constant>WM_ERASEBKGND</constant> and
514               <constant>WM_PAINT</constant> messages.  In addition,
515               <constant>WM_NCPAINT</constant> message is sent when the
516               uncovered area  intersects a nonclient part of the window.
517               Application must reply to the <constant>WM_PAINT</constant>
518               message by calling the
519               <function>BeginPaint()</function>/<function>EndPaint()</function>
520               pair of functions. <function>BeginPaint()</function> returns a DC
521               that uses accumulated update region as a clipping region. This
522               operation cleans up invalidated area and the window will not
523               receive another <constant>WM_PAINT</constant> until the window
524               manager creates a new update region.
525             </para>
526             <para>
527               A was moved to the left:
528             </para>
529             <screen>
530     ________________________       ...          / C update region
531    |______                  |     :      .___ /
532    | A    |_________        |  =&gt; |   ...|___|..
533    |      |         |       |     |   :  |   |
534    |------'         |       |     |   :  '---' 
535    |   |      B     |       |     |   :      \
536    |   |            |       |     :            \
537    |   `------------'       |                    B update region
538    |                   C    |
539    `------------------------'
540             </screen>
541             <para>
542               Windows maintains a display context cache consisting of
543               entries that include the DC itself, the window to which
544               it belongs, and an optional clipping region (visible
545               region is stored in the DC itself). When an API call
546               changes the state of the window tree, window manager has
547               to go through the DC cache to recalculate visible
548               regions for entries whose windows were involved in the
549               operation. DC entries (DCE) can be either private to the
550               window, or private to the window class, or shared
551               between all windows (Windows 3.1 limits the number of
552               shared DCEs to 5).
553             </para>
554           </sect4>
555         </sect3>
556
557         <sect3>
558           <title>Messaging subsystem</title>
559
560           <para><filename>windows/queue.c</filename></para>
561           <para><filename>windows/message.c</filename></para>
562
563           <para>
564             Each Windows task/thread has its own message queue - this
565             is where it gets messages from. Messages can be:
566             <orderedlist>
567               <listitem>
568                 <para>
569                   generated on the fly (<constant>WM_PAINT</constant>,
570                   <constant>WM_NCPAINT</constant>,
571                   <constant>WM_TIMER</constant>)
572                 </para>
573               </listitem>
574               <listitem>
575                 <para>
576                   created by the system (hardware messages)
577                 </para>
578               </listitem>
579               <listitem>
580                 <para>
581                   posted by other tasks/threads (<function>PostMessage</function>)
582                 </para>
583               </listitem>
584               <listitem>
585                 <para>
586                   sent by other tasks/threads (<function>SendMessage</function>)
587                 </para>
588               </listitem>
589             </orderedlist>
590           </para>
591           <para>
592             Message priority:
593           </para>
594           <para>
595             First the system looks for sent messages, then for posted
596             messages, then for hardware messages, then it checks if
597             the queue has the "dirty window" bit set, and, finally, it
598             checks for expired timers. See
599             <filename>windows/message.c</filename>.
600           </para>
601           <para>
602             From all these different types of messages, only posted
603             messages go directly into the private message queue.
604             System messages (even in Win95) are first collected in the
605             system message queue and then they either sit there until
606             <function>Get/PeekMessage</function> gets to process them
607             or, as in Win95, if system queue is getting clobbered, a
608             special thread ("raw input thread") assigns them to the
609             private queues. Sent messages are queued separately and
610             the sender sleeps until it gets a reply. Special messages
611             are generated on the fly depending on the window/queue
612             state. If the window update region is not empty, the
613             system sets the <constant>QS_PAINT</constant> bit in the
614             owning queue and eventually this window receives a
615             <constant>WM_PAINT</constant> message
616             (<constant>WM_NCPAINT</constant> too if the update region
617             intersects with the non-client area). A timer event is
618             raised when one of the queue timers expire. Depending on
619             the timer parameters <function>DispatchMessage</function>
620             either calls the callback function or the window
621             procedure. If there are no messages pending the
622             task/thread sleeps until messages appear.
623           </para>
624           <para>
625             There are several tricky moments (open for discussion) - 
626           </para>
627
628           <itemizedlist>
629             <listitem>
630               <para>
631                 System message order has to be honored and messages
632                 should be processed within correct task/thread
633                 context. Therefore when <function>Get/PeekMessage</function> encounters
634                 unassigned system message and this message appears not
635                 to be for the current task/thread it should either
636                 skip it (or get rid of it by moving it into the
637                 private message queue of the target task/thread -
638                 Win95, AFAIK) and look further or roll back and then
639                 yield until this message gets processed when system
640                 switches to the correct context (Win16). In the first
641                 case we lose correct message ordering, in the second
642                 case we have the infamous synchronous system message
643                 queue. Here is a post to one of the OS/2 newsgroup I
644                 found to be relevant:
645               </para>
646               <blockquote>
647                 <attribution>by David Charlap</attribution>
648                 <para>
649                   " Here's the problem in a nutshell, and there is no
650                   good solution. Every possible solution creates a
651                   different problem.
652                 </para>
653                 <para>
654                   With a windowing system, events can go to many
655                   different windows. Most are sent by applications or
656                   by the OS when things relating to that window happen
657                   (like repainting, timers, etc.)
658                 </para>
659                 <para>
660                   Mouse input events go to the window you click on
661                   (unless some window captures the mouse).
662                 </para>
663                 <para>
664                   So far, no problem.  Whenever an event happens, you
665                   put a message on the target window's message queue.
666                   Every process has a message queue.  If the process
667                   queue fills up, the messages back up onto the system
668                   queue.
669                 </para>
670                 <para>
671                   This is the first cause of apps hanging the GUI.  If
672                   an app doesn't handle messages and they back up into
673                   the system queue, other apps can't get any more
674                   messages.  The reason is that the next message in
675                   line can't go anywhere, and the system won't skip
676                   over it.
677                 </para>
678                 <para>
679                   This can be fixed by making apps have bigger private
680                   message queues. The SIQ fix does this.  PMQSIZE does
681                   this for systems without the SIQ fix.  Applications
682                   can also request large queues on their own.
683                 </para>
684                 <para>
685                   Another source of the problem, however, happens when
686                   you include keyboard events.  When you press a key,
687                   there's no easy way to know what window the
688                   keystroke message should be delivered to.
689                 </para>
690                 <para>
691                   Most windowing systems use a concept known as
692                   "focus".  The window with focus gets all incoming
693                   keyboard messages.  Focus can be changed from window
694                   to window by apps or by users clicking on windows.
695                 </para>
696                 <para>
697                   This is the second source of the problem.  Suppose
698                   window A has focus. You click on window B and start
699                   typing before the window gets focus. Where should
700                   the keystrokes go?  On the one hand, they should go
701                   to A until the focus actually changes to B.  On the
702                   other hand, you probably want the keystrokes to go
703                   to B, since you clicked there first.
704                 </para>
705                 <para>
706                   OS/2's solution is that when a focus-changing event
707                   happens (like clicking on a window), OS/2 holds all
708                   messages in the system queue until the focus change
709                   actually happens.  This way, subsequent keystrokes
710                   go to the window you clicked on, even if it takes a
711                   while for that window to get focus.
712                 </para>
713                 <para>
714                   The downside is that if the window takes a real long
715                   time to get focus (maybe it's not handling events,
716                   or maybe the window losing focus isn't handling
717                   events), everything backs up in the system queue and
718                   the system appears hung.
719                 </para>
720                 <para>
721                   There are a few solutions to this problem.
722                 </para>
723                 <para>
724                   One is to make focus policy asynchronous.  That is,
725                   focus changing has absolutely nothing to do with the
726                   keyboard.  If you click on a window and start typing
727                   before the focus actually changes, the keystrokes go
728                   to the first window until focus changes, then they
729                   go to the second. This is what X-windows does.
730                 </para>
731                 <para>
732                   Another is what NT does.  When focus changes,
733                   keyboard events are held in the system message
734                   queue, but other events are allowed through. This is
735                   "asynchronous" because the messages in the system
736                   queue are delivered to the application queues in a
737                   different order from that with which they were
738                   posted.  If a bad app won't handle the "lose focus"
739                   message, it's of no consequence - the app receiving
740                   focus will get its "gain focus" message, and the
741                   keystrokes will go to it.
742                 </para>
743                 <para>
744                   The NT solution also takes care of the application
745                   queue filling up problem.  Since the system delivers
746                   messages asynchronously, messages waiting in the
747                   system queue will just sit there and the rest of the
748                   messages will be delivered to their apps.
749                 </para>
750                 <para>
751                   The OS/2 SIQ solution is this:  When a
752                   focus-changing event happens, in addition to
753                   blocking further messages from the application
754                   queues, a timer is started.  When the timer goes
755                   off, if the focus change has not yet happened, the
756                   bad app has its focus taken away and all messages
757                   targeted at that window are skipped.  When the bad
758                   app finally handles the focus change message, OS/2
759                   will detect this and stop skipping its messages.
760                 </para>
761
762                 <para>
763                   As for the pros and cons:
764                 </para>
765                 <para>
766                   The X-windows solution is probably the easiest.  The
767                   problem is that users generally don't like having to
768                   wait for the focus to change before they start
769                   typing.  On many occasions, you can type and the
770                   characters end up in the wrong window because
771                   something (usually heavy system load) is preventing
772                   the focus change from happening in a timely manner.
773                 </para>
774                 <para>
775                   The NT solution seems pretty nice, but making the
776                   system message queue asynchronous can cause similar
777                   problems to the X-windows problem. Since messages
778                   can be delivered out of order, programs must not
779                   assume that two messages posted in a particular
780                   order will be delivered in that same order.  This
781                   can break legacy apps, but since Win32 always had an
782                   asynchronous queue, it is fair to simply tell app
783                   designers "don't do that".  It's harder to tell app
784                   designers something like that on OS/2 - they'll
785                   complain "you changed the rules and our apps are
786                   breaking."
787                 </para>
788                 <para>
789                   The OS/2 solution's problem is that nothing happens
790                   until you try to change window focus, and then wait
791                   for the timeout.  Until then, the bad app is not
792                   detected and nothing is done."
793                 </para>
794               </blockquote>
795             </listitem>
796
797             <listitem>
798               <para>
799                 Intertask/interthread
800                 <function>SendMessage</function>. The system has to
801                 inform the target queue about the forthcoming message,
802                 then it has to carry out the context switch and wait
803                 until the result is available.  Win16 stores necessary
804                 parameters in the queue structure and then calls
805                 <function>DirectedYield()</function> function.
806                 However, in Win32 there could be  several messages
807                 pending sent by preemptively executing threads, and in
808                 this case <function>SendMessage</function> has to
809                 build some sort of message queue for sent messages.
810                 Another issue is what to do with messages sent to the
811                 sender when it is blocked inside its own
812                 <function>SendMessage</function>. 
813               </para>
814             </listitem>
815           </itemizedlist>
816         </sect3>
817       </sect2>
818     </sect1>
819
820     <sect1 id="arch-dlls">
821       <title>Wine/Windows DLLs</title>
822
823       <para>
824         Based upon various messages on wine-devel especially by Ulrich
825         Weigand. Adapted by Michele Petrovski and Klaas van Gend.
826       </para>
827
828       <para>
829         (Extracted from <filename>wine/documentation/dlls</filename>)
830       </para>
831
832       <para>
833         This document mainly deals with the status of current DLL
834         support by Wine.  The Wine ini file currently supports
835         settings to change the load order of DLLs.  The load order
836         depends on several issues, which results in different settings
837         for various DLLs.
838       </para>
839
840       <sect2>
841         <title>Pros of Native DLLs</title>
842
843         <para>
844           Native DLLs of course guarantee 100% compatibility for
845           routines they implement. For example, using the native USER
846           DLL would maintain a virtually perfect and Windows 95-like
847           look for window borders, dialog controls, and so on. Using
848           the built-in Wine version of this library, on the other
849           hand, would produce a display that does not precisely mimic
850           that of Windows 95.  Such subtle differences can be
851           engendered in other important DLLs, such as the common
852           controls library COMMCTRL or the common dialogs library
853           COMMDLG, when built-in Wine DLLs outrank other types in load
854           order.
855         </para>
856         <para>
857           More significant, less aesthetically-oriented problems can
858           result if the built-in Wine version of the SHELL DLL is
859           loaded before the native version of this library. SHELL
860           contains routines such as those used by installer utilities
861           to create desktop shortcuts. Some installers might fail when
862           using Wine's built-in SHELL.
863         </para>
864       </sect2>
865
866       <sect2>
867         <title>Cons of Native DLLs</title>
868
869         <para>
870           Not every application performs better under native DLLs. If
871           a library tries to access features of the rest of the system
872           that are not fully implemented in Wine, the native DLL might
873           work much worse than the corresponding built-in one, if at
874           all. For example, the native Windows GDI library must be
875           paired with a Windows display driver, which of course is not
876           present under Intel Unix and Wine.
877         </para>
878         <para>
879           Finally, occasionally built-in Wine DLLs implement more
880           features than the corresponding native Windows DLLs.
881           Probably the most important example of such behavior is the
882           integration of Wine with X provided by Wine's built-in USER
883           DLL. Should the native Windows USER library take load-order
884           precedence, such features as the ability to use the
885           clipboard or drag-and-drop between Wine windows and X
886           windows will be lost.
887         </para>
888       </sect2>
889
890       <sect2>
891         <title>Deciding Between Native and Built-In DLLs</title>
892
893         <para>
894           Clearly, there is no one rule-of-thumb regarding which
895           load-order to use. So, you must become familiar with:
896         </para>
897
898         <itemizedlist>
899           <listitem>
900             <para>
901               what specific DLLs do
902             </para>
903           </listitem>
904           <listitem>
905             <para>
906               which other DLLs or features a given library interacts with
907             </para>
908           </listitem>
909         </itemizedlist>
910         <para>
911           and use this information to make a case-by-case decision.
912         </para>
913       </sect2>
914
915       <sect2>
916         <title>Load Order for DLLs</title>
917
918         <para>
919           Using the DLL sections from the wine configuration file, the
920           load order can be tweaked to a high degree. In general it is
921           advised not to change the settings of the configuration
922           file. The default configuration specifies the right load
923           order for the most important DLLs.
924         </para>
925         <para>
926           The default load order follows this algorithm: for all DLLs
927           which have a fully-functional Wine implementation, or where
928           the native DLL is known not to work, the built-in library
929           will be loaded first. In all other cases, the native DLL
930           takes load-order precedence.
931         </para>
932         <para>
933           The <varname>DefaultLoadOrder</varname> from the
934           [DllDefaults] section specifies for all DLLs which version
935           to try first. See manpage for explanation of the arguments.
936         </para>
937         <para>
938           The [DllOverrides] section deals with DLLs, which need a
939           different-from-default treatment. 
940         </para>
941         <para>
942           The [DllPairs] section is for DLLs, which must be loaded in
943           pairs. In general, these are DLLs for either 16-bit or
944           32-bit applications. In most cases in Windows, the 32-bit
945           version cannot be used without its 16-bit counterpart. For
946           Wine, it is customary that the 16-bit implementations rely
947           on the 32-bit implementations and cast the results back to
948           16-bit arguments. Changing anything in this section is bound
949           to result in errors.
950         </para>
951         <para>
952           For the future, the Wine implementation of Windows DLL seems
953           to head towards unifying the 16 and 32 bit DLLs wherever
954           possible, resulting in larger DLLs.  They are stored in the
955           <filename>dlls/</filename> subdirectory using the 16-bit
956           name.  For large DLLs, a split might be discussed.
957         </para>
958       </sect2>
959
960       <sect2>
961         <title>Understanding What DLLs Do</title>
962
963         <para>
964           The following list briefly describes each of the DLLs
965           commonly found in Windows whose load order may be modified
966           during the configuration and compilation of Wine.
967         </para>
968         <para>
969           (See also <filename>./DEVELOPER-HINTS</filename> or the
970           <filename>dlls/</filename> subdirectory to see which DLLs
971           are currently being rewritten for Wine)
972         </para>
973
974 <!-- FIXME: Should convert this table into a VariableList element -->
975         <screen>
976 ADVAPI32.DLL:      32-bit application advanced programming interfaces
977                    like crypto, systeminfo, security and event logging
978 AVIFILE.DLL:       32-bit application programming interfaces for the
979                    Audio Video Interleave (AVI) Windows-specific
980                    Microsoft audio-video standard
981 COMMCTRL.DLL:      16-bit common controls
982 COMCTL32.DLL:      32-bit common controls
983 COMDLG32.DLL:      32-bit common dialogs
984 COMMDLG.DLL:       16-bit common dialogs
985 COMPOBJ.DLL:       OLE 16- and 32-bit compatibility libraries
986 CRTDLL.DLL:        Microsoft C runtime
987 DCIMAN.DLL:        16-bit
988 DCIMAN32.DLL:      32-bit display controls
989 DDEML.DLL:         DDE messaging
990 D3D*.DLL           DirectX/Direct3D drawing libraries
991 DDRAW.DLL:         DirectX drawing libraries
992 DINPUT.DLL:        DirectX input libraries
993 DISPLAY.DLL:       Display libraries
994 DPLAY.DLL, DPLAYX.DLL:  DirectX playback libraries
995 DSOUND.DLL:        DirectX audio libraries
996 GDI.DLL:           16-bit graphics driver interface
997 GDI32.DLL:         32-bit graphics driver interface
998 IMAGEHLP.DLL:      32-bit IMM API helper libraries (for PE-executables)
999 IMM32.DLL:         32-bit IMM API
1000 IMGUTIL.DLL:       
1001 KERNEL32.DLL       32-bit kernel DLL
1002 KEYBOARD.DLL:      Keyboard drivers
1003 LZ32.DLL:          32-bit Lempel-Ziv or LZ file compression
1004                    used by the installshield installers (???).
1005 LZEXPAND.DLL:      LZ file expansion; needed for Windows Setup
1006 MMSYSTEM.DLL:      Core of the Windows multimedia system
1007 MOUSE.DLL:         Mouse drivers
1008 MPR.DLL:           32-bit Windows network interface
1009 MSACM.DLL:         Core of the Addressed Call Mode or ACM system
1010 MSACM32.DLL:       Core of the 32-bit ACM system
1011                    Audio Compression Manager ???
1012 MSNET32.DLL        32-bit network APIs
1013 MSVFW32.DLL:       32-bit Windows video system
1014 MSVIDEO.DLL:       16-bit Windows video system
1015 OLE2.DLL:          OLE 2.0 libraries
1016 OLE32.DLL:         32-bit OLE 2.0 components
1017 OLE2CONV.DLL:      Import filter for graphics files
1018 OLE2DISP.DLL, OLE2NLS.DLL: OLE 2.1 16- and 32-bit interoperability
1019 OLE2PROX.DLL:      Proxy server for OLE 2.0
1020 OLE2THK.DLL:       Thunking for OLE 2.0
1021 OLEAUT32.DLL       32-bit OLE 2.0 automation
1022 OLECLI.DLL:        16-bit OLE client
1023 OLECLI32.DLL:      32-bit OLE client
1024 OLEDLG.DLL:        OLE 2.0 user interface support
1025 OLESVR.DLL:        16-bit OLE server libraries
1026 OLESVR32.DLL:      32-bit OLE server libraries
1027 PSAPI.DLL:         Proces Status API libraries
1028 RASAPI16.DLL:      16-bit Remote Access Services libraries
1029 RASAPI32.DLL:      32-bit Remote Access Services libraries
1030 SHELL.DLL:         16-bit Windows shell used by Setup
1031 SHELL32.DLL:       32-bit Windows shell (COM object?)
1032 TAPI/TAPI32/TAPIADDR:  Telephone API (for Modems)
1033 W32SKRNL:          Win32s Kernel ? (not in use for Win95 and up!)
1034 WIN32S16.DLL:      Application compatibility for Win32s
1035 WIN87EM.DLL:       80387 math-emulation libraries
1036 WINASPI.DLL:       Advanced SCSI Peripheral Interface or ASPI libraries
1037 WINDEBUG.DLL       Windows debugger
1038 WINMM.DLL:         Libraries for multimedia thunking
1039 WING.DLL:          Libraries required to "draw" graphics
1040 WINSOCK.DLL:       Sockets APIs
1041 WINSPOOL.DLL:      Print spooler libraries
1042 WNASPI32.DLL:      32-bit ASPI libraries
1043 WSOCK32.DLL:       32-bit sockets APIs
1044         </screen>
1045       </sect2>
1046     </sect1>
1047   </chapter>
1048
1049 <!-- Keep this comment at the end of the file
1050 Local variables:
1051 mode: sgml
1052 sgml-parent-document:("wine-devel.sgml" "set" "book" "part" "chapter" "")
1053 End:
1054 -->