Added fields from NetBSD struct sigcontext on i386.
[wine] / dlls / ddraw / main.c
1 /*              DirectDraw Base Functions
2  *
3  * Copyright 1997-1999 Marcus Meissner
4  * Copyright 1998 Lionel Ulmer (most of Direct3D stuff)
5  * Copyright 2000-2001 TransGaming Technologies Inc.
6  *
7  * This file contains the (internal) driver registration functions,
8  * driver enumeration APIs and DirectDraw creation functions.
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26
27 #include <assert.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #include "winnls.h"
32 #include "winerror.h"
33
34 #include "ddraw.h"
35 #include "d3d.h"
36
37 /* This for all the enumeration and creation of D3D-related objects */
38 #include "ddraw_private.h"
39 #include "wine/debug.h"
40
41 #define MAX_DDRAW_DRIVERS 3
42 static const ddraw_driver* DDRAW_drivers[MAX_DDRAW_DRIVERS];
43 static int DDRAW_num_drivers; /* = 0 */
44 static int DDRAW_default_driver;
45
46 void (*wine_tsx11_lock_ptr)(void) = NULL;
47 void (*wine_tsx11_unlock_ptr)(void) = NULL;
48
49 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
50
51 /**********************************************************************/
52
53 typedef struct {
54     LPVOID lpCallback;
55     LPVOID lpContext;
56 } DirectDrawEnumerateProcData;
57
58 /***********************************************************************
59  *              DirectDrawEnumerateExA (DDRAW.@)
60  */
61 HRESULT WINAPI DirectDrawEnumerateExA(
62     LPDDENUMCALLBACKEXA lpCallback, LPVOID lpContext, DWORD dwFlags)
63 {
64     int i;
65     TRACE("(%p,%p, %08lx)\n", lpCallback, lpContext, dwFlags);
66
67     if (TRACE_ON(ddraw)) {
68         TRACE("  Flags : ");
69         if (dwFlags & DDENUM_ATTACHEDSECONDARYDEVICES)
70             TRACE("DDENUM_ATTACHEDSECONDARYDEVICES ");
71         if (dwFlags & DDENUM_DETACHEDSECONDARYDEVICES)
72             TRACE("DDENUM_DETACHEDSECONDARYDEVICES ");
73         if (dwFlags & DDENUM_NONDISPLAYDEVICES)
74             TRACE("DDENUM_NONDISPLAYDEVICES ");
75         TRACE("\n");
76     }
77
78     for (i=0; i<DDRAW_num_drivers; i++)
79     {
80         TRACE("Enumerating %s/%s interface\n",
81               DDRAW_drivers[i]->info->szDriver,
82               DDRAW_drivers[i]->info->szDescription);
83
84         /* We have to pass NULL from the primary display device.
85          * RoadRage chapter 6's enumeration routine expects it. */
86         if (!lpCallback((DDRAW_default_driver == i) ? NULL
87                         :(LPGUID)&DDRAW_drivers[i]->info->guidDeviceIdentifier,
88                         (LPSTR)DDRAW_drivers[i]->info->szDescription,
89                         (LPSTR)DDRAW_drivers[i]->info->szDriver,
90                         lpContext, 0))
91             return DD_OK;
92     }
93
94     /* Unsupported flags */
95     if (dwFlags & DDENUM_NONDISPLAYDEVICES) {
96         FIXME("no non-display devices supported.\n");
97     }
98     if (dwFlags & DDENUM_DETACHEDSECONDARYDEVICES) {
99         FIXME("no detached secondary devices supported.\n");
100     }
101
102     return DD_OK;
103 }
104
105 /***********************************************************************
106  *              DirectDrawEnumerateExW (DDRAW.@)
107  */
108
109 static BOOL CALLBACK DirectDrawEnumerateExProcW(
110     GUID *lpGUID, LPSTR lpDriverDescription, LPSTR lpDriverName,
111     LPVOID lpContext, HMONITOR hm)
112 {
113     INT len;
114     BOOL bResult;
115     LPWSTR lpDriverDescriptionW, lpDriverNameW;
116     DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
117
118     len = MultiByteToWideChar( CP_ACP, 0, lpDriverDescription, -1, NULL, 0 );
119     lpDriverDescriptionW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
120     MultiByteToWideChar( CP_ACP, 0, lpDriverDescription, -1, lpDriverDescriptionW, len );
121
122     len = MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, NULL, 0 );
123     lpDriverNameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
124     MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, lpDriverNameW, len );
125
126     bResult = (*(LPDDENUMCALLBACKEXW *) pEPD->lpCallback)(lpGUID, lpDriverDescriptionW,
127                                                           lpDriverNameW, pEPD->lpContext, hm);
128
129     HeapFree(GetProcessHeap(), 0, lpDriverDescriptionW);
130     HeapFree(GetProcessHeap(), 0, lpDriverNameW);
131     return bResult;
132 }
133
134 HRESULT WINAPI DirectDrawEnumerateExW(
135   LPDDENUMCALLBACKEXW lpCallback, LPVOID lpContext, DWORD dwFlags)
136 {
137     DirectDrawEnumerateProcData epd;
138     epd.lpCallback = (LPVOID) lpCallback;
139     epd.lpContext = lpContext;
140
141     return DirectDrawEnumerateExA(DirectDrawEnumerateExProcW, (LPVOID) &epd, 0);
142 }
143
144 /***********************************************************************
145  *              DirectDrawEnumerateA (DDRAW.@)
146  */
147
148 static BOOL CALLBACK DirectDrawEnumerateProcA(
149         GUID *lpGUID, LPSTR lpDriverDescription, LPSTR lpDriverName,
150         LPVOID lpContext, HMONITOR hm)
151 {
152     DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
153
154     return ((LPDDENUMCALLBACKA) pEPD->lpCallback)(
155         lpGUID, lpDriverDescription, lpDriverName, pEPD->lpContext);
156 }
157
158 HRESULT WINAPI DirectDrawEnumerateA(
159   LPDDENUMCALLBACKA lpCallback, LPVOID lpContext)
160 {
161     DirectDrawEnumerateProcData epd;
162     epd.lpCallback = (LPVOID) lpCallback;
163     epd.lpContext = lpContext;
164
165     return DirectDrawEnumerateExA(DirectDrawEnumerateProcA, (LPVOID) &epd, 0);
166 }
167
168 /***********************************************************************
169  *              DirectDrawEnumerateW (DDRAW.@)
170  */
171
172 static BOOL WINAPI DirectDrawEnumerateProcW(
173   GUID *lpGUID, LPWSTR lpDriverDescription, LPWSTR lpDriverName,
174   LPVOID lpContext, HMONITOR hm)
175 {
176     DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
177
178     return ((LPDDENUMCALLBACKW) pEPD->lpCallback)(
179         lpGUID, lpDriverDescription, lpDriverName, pEPD->lpContext);
180 }
181
182 HRESULT WINAPI DirectDrawEnumerateW(
183   LPDDENUMCALLBACKW lpCallback, LPVOID lpContext)
184 {
185     DirectDrawEnumerateProcData epd;
186     epd.lpCallback = (LPVOID) lpCallback;
187     epd.lpContext = lpContext;
188
189     return DirectDrawEnumerateExW(DirectDrawEnumerateProcW, (LPVOID) &epd, 0);
190 }
191
192 /***********************************************************************
193  *              DirectDrawCreate (DDRAW.@)
194  */
195
196 const ddraw_driver* DDRAW_FindDriver(const GUID* pGUID)
197 {
198     static const GUID zeroGUID; /* gets zero-inited */
199
200     TRACE("(%s)\n", pGUID ? debugstr_guid(pGUID) : "(null)");
201
202     if (DDRAW_num_drivers == 0) return NULL;
203
204     if (pGUID == (LPGUID)DDCREATE_EMULATIONONLY
205         || pGUID == (LPGUID)DDCREATE_HARDWAREONLY)
206         pGUID = NULL;
207
208     if (pGUID == NULL || memcmp(pGUID, &zeroGUID, sizeof(GUID)) == 0)
209     {
210         /* Use the default driver. */
211         return DDRAW_drivers[DDRAW_default_driver];
212     }
213     else
214     {
215         /* Look for a matching GUID. */
216
217         int i;
218         for (i=0; i < DDRAW_num_drivers; i++)
219         {
220             if (IsEqualGUID(pGUID,
221                             &DDRAW_drivers[i]->info->guidDeviceIdentifier))
222                 break;
223         }
224
225         if (i < DDRAW_num_drivers)
226         {
227             return DDRAW_drivers[i];
228         }
229         else
230         {
231             ERR("(%s): did not recognize requested GUID.\n",debugstr_guid(pGUID));
232             return NULL;
233         }
234     }
235 }
236
237 static HRESULT DDRAW_Create(
238         LPGUID lpGUID, LPVOID *lplpDD, LPUNKNOWN pUnkOuter, REFIID iid, BOOL ex
239 ) {
240     const ddraw_driver* driver;
241     LPDIRECTDRAW7 pDD;
242     HRESULT hr;
243
244     TRACE("(%s,%p,%p,%d)\n", debugstr_guid(lpGUID), lplpDD, pUnkOuter, ex);
245
246     if (DDRAW_num_drivers == 0)
247     {
248         WARN("no DirectDraw drivers registered\n");
249         return DDERR_INVALIDDIRECTDRAWGUID;
250     }
251
252     if (lpGUID == (LPGUID)DDCREATE_EMULATIONONLY
253         || lpGUID == (LPGUID)DDCREATE_HARDWAREONLY)
254         lpGUID = NULL;
255
256     if (pUnkOuter != NULL)
257         return DDERR_INVALIDPARAMS; /* CLASS_E_NOAGGREGATION? */
258
259     driver = DDRAW_FindDriver(lpGUID);
260     if (driver == NULL) return DDERR_INVALIDDIRECTDRAWGUID;
261
262     hr = driver->create(lpGUID, &pDD, pUnkOuter, ex);
263     if (FAILED(hr)) return hr;
264
265     hr = IDirectDraw7_QueryInterface(pDD, iid, lplpDD);
266     IDirectDraw7_Release(pDD);
267     return hr;
268 }
269
270 /***********************************************************************
271  *              DirectDrawCreate (DDRAW.@)
272  *
273  * Only creates legacy IDirectDraw interfaces.
274  * Cannot create IDirectDraw7 interfaces.
275  * In theory.
276  */
277 HRESULT WINAPI DirectDrawCreate(
278         LPGUID lpGUID, LPDIRECTDRAW* lplpDD, LPUNKNOWN pUnkOuter
279 ) {
280     TRACE("(%s,%p,%p)\n", debugstr_guid(lpGUID), lplpDD, pUnkOuter);
281     return DDRAW_Create(lpGUID, (LPVOID*) lplpDD, pUnkOuter, &IID_IDirectDraw, FALSE);
282 }
283
284 /***********************************************************************
285  *              DirectDrawCreateEx (DDRAW.@)
286  *
287  * Only creates new IDirectDraw7 interfaces.
288  * Supposed to fail if legacy interfaces are requested.
289  * In theory.
290  */
291 HRESULT WINAPI DirectDrawCreateEx(
292         LPGUID lpGUID, LPVOID* lplpDD, REFIID iid, LPUNKNOWN pUnkOuter
293 ) {
294     TRACE("(%s,%p,%s,%p)\n", debugstr_guid(lpGUID), lplpDD, debugstr_guid(iid), pUnkOuter);
295
296     if (!IsEqualGUID(iid, &IID_IDirectDraw7))
297         return DDERR_INVALIDPARAMS;
298
299     return DDRAW_Create(lpGUID, lplpDD, pUnkOuter, iid, TRUE);
300 }
301
302 extern HRESULT Uninit_DirectDraw_Create(const GUID*, LPDIRECTDRAW7*,
303                                         LPUNKNOWN, BOOL);
304
305 /* This is for the class factory. */
306 static HRESULT DDRAW_CreateDirectDraw(IUnknown* pUnkOuter, REFIID iid,
307                                       LPVOID* ppObj)
308 {
309     LPDIRECTDRAW7 pDD;
310     HRESULT hr;
311     BOOL ex;
312
313     TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppObj);
314     
315     /* This is a mighty hack :-) */
316     if (IsEqualGUID(iid, &IID_IDirectDraw7))
317         ex = TRUE;
318     else
319         ex = FALSE;
320     
321     hr = Uninit_DirectDraw_Create(NULL, &pDD, pUnkOuter, ex);
322     if (FAILED(hr)) return hr;
323
324     hr = IDirectDraw7_QueryInterface(pDD, iid, ppObj);
325     IDirectDraw_Release(pDD);
326     return hr;
327 }
328
329 /******************************************************************************
330  * DirectDraw ClassFactory
331  */
332 typedef struct {
333     ICOM_VFIELD_MULTI(IClassFactory);
334
335     DWORD ref;
336     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID iid,
337                                  LPVOID *ppObj);
338 } IClassFactoryImpl;
339
340 struct object_creation_info
341 {
342     const CLSID *clsid;
343     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID riid,
344                                  LPVOID *ppObj);
345 };
346
347 /* There should be more, but these are the only ones listed in the header
348  * file. */
349 extern HRESULT DDRAW_CreateDirectDrawClipper(IUnknown *pUnkOuter, REFIID riid,
350                                              LPVOID *ppObj);
351
352 static const struct object_creation_info object_creation[] =
353 {
354     { &CLSID_DirectDraw,        DDRAW_CreateDirectDraw },
355     { &CLSID_DirectDraw7,       DDRAW_CreateDirectDraw },
356     { &CLSID_DirectDrawClipper, DDRAW_CreateDirectDrawClipper }
357 };
358
359 static HRESULT WINAPI
360 DDCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
361 {
362     ICOM_THIS(IClassFactoryImpl,iface);
363
364     TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj);
365     
366     if (IsEqualGUID(riid, &IID_IUnknown)
367         || IsEqualGUID(riid, &IID_IClassFactory))
368     {
369         IClassFactory_AddRef(iface);
370         *ppobj = This;
371         return S_OK;
372     }
373
374     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
375     return E_NOINTERFACE;
376 }
377
378 static ULONG WINAPI DDCF_AddRef(LPCLASSFACTORY iface) {
379     ICOM_THIS(IClassFactoryImpl,iface);
380
381     TRACE("(%p)->() incrementing from %ld.\n", This, This->ref);
382     
383     return ++(This->ref);
384 }
385
386 static ULONG WINAPI DDCF_Release(LPCLASSFACTORY iface) {
387     ICOM_THIS(IClassFactoryImpl,iface);
388
389     TRACE("(%p)->() decrementing from %ld.\n", This, This->ref);
390
391     if (--This->ref == 0)
392         HeapFree(GetProcessHeap(), 0, This);
393
394     return This->ref;
395 }
396
397
398 static HRESULT WINAPI DDCF_CreateInstance(
399         LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
400 ) {
401     ICOM_THIS(IClassFactoryImpl,iface);
402
403     TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
404
405     return This->pfnCreateInstance(pOuter, riid, ppobj);
406 }
407
408 static HRESULT WINAPI DDCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
409     ICOM_THIS(IClassFactoryImpl,iface);
410     FIXME("(%p)->(%d),stub!\n",This,dolock);
411     return S_OK;
412 }
413
414 static ICOM_VTABLE(IClassFactory) DDCF_Vtbl =
415 {
416     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
417     DDCF_QueryInterface,
418     DDCF_AddRef,
419     DDCF_Release,
420     DDCF_CreateInstance,
421     DDCF_LockServer
422 };
423
424 /*******************************************************************************
425  * DllGetClassObject [DDRAW.@]
426  * Retrieves class object from a DLL object
427  *
428  * NOTES
429  *    Docs say returns STDAPI
430  *
431  * PARAMS
432  *    rclsid [I] CLSID for the class object
433  *    riid   [I] Reference to identifier of interface for class object
434  *    ppv    [O] Address of variable to receive interface pointer for riid
435  *
436  * RETURNS
437  *    Success: S_OK
438  *    Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
439  *             E_UNEXPECTED
440  */
441 DWORD WINAPI DDRAW_DllGetClassObject(REFCLSID rclsid,REFIID riid,LPVOID *ppv)
442 {
443     unsigned int i;
444     IClassFactoryImpl *factory;
445
446     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
447
448     if ( !IsEqualGUID( &IID_IClassFactory, riid )
449          && ! IsEqualGUID( &IID_IUnknown, riid) )
450         return E_NOINTERFACE;
451
452     for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
453     {
454         if (IsEqualGUID(object_creation[i].clsid, rclsid))
455             break;
456     }
457
458     if (i == sizeof(object_creation)/sizeof(object_creation[0]))
459     {
460         FIXME("%s: no class found.\n", debugstr_guid(rclsid));
461         return CLASS_E_CLASSNOTAVAILABLE;
462     }
463
464     factory = HeapAlloc(GetProcessHeap(), 0, sizeof(*factory));
465     if (factory == NULL) return E_OUTOFMEMORY;
466
467     ICOM_INIT_INTERFACE(factory, IClassFactory, DDCF_Vtbl);
468     factory->ref = 1;
469
470     factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
471
472     *ppv = ICOM_INTERFACE(factory, IClassFactory);
473     return S_OK;
474 }
475
476
477 /*******************************************************************************
478  * DllCanUnloadNow [DDRAW.@]  Determines whether the DLL is in use.
479  *
480  * RETURNS
481  *    Success: S_OK
482  *    Failure: S_FALSE
483  */
484 DWORD WINAPI DDRAW_DllCanUnloadNow(void) {
485     FIXME("(void): stub\n");
486     return S_FALSE;
487 }
488
489 /******************************************************************************
490  * Initialisation
491  */
492
493 /* Choose which driver is considered the primary display driver. It will
494  * be created when we get a NULL guid for the DirectDrawCreate(Ex). */
495 static int DDRAW_ChooseDefaultDriver(void)
496 {
497     int i;
498     int best = 0;
499     int best_score = 0;
500
501     assert(DDRAW_num_drivers > 0);
502
503     /* This algorithm is really stupid. */
504     for (i=0; i < DDRAW_num_drivers; i++)
505     {
506         if (DDRAW_drivers[i]->preference > best_score)
507         {
508             best_score = DDRAW_drivers[i]->preference;
509             best = i;
510         }
511     }
512
513     assert(best_score > 0);
514
515     return best;
516 }
517
518 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
519 {
520     /* If we were sufficiently cool, DDraw drivers would just be COM
521      * objects, registered with a particular component category. */
522
523     DDRAW_HAL_Init(hInstDLL, fdwReason, lpv);
524     DDRAW_User_Init(hInstDLL, fdwReason, lpv);
525
526     if (fdwReason == DLL_PROCESS_ATTACH)
527     {
528         HMODULE mod = GetModuleHandleA( "x11drv.dll" );
529         if (mod)
530         {
531             wine_tsx11_lock_ptr   = (void *)GetProcAddress( mod, "wine_tsx11_lock" );
532             wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_unlock" );
533         }
534     }
535
536     if (DDRAW_num_drivers > 0)
537         DDRAW_default_driver = DDRAW_ChooseDefaultDriver();
538
539     return TRUE;
540 }
541
542 /* Register a direct draw driver. This should be called from your init
543  * function. (That's why there is no locking: your init func is called from
544  * our DllInit, which is serialised.) */
545 void DDRAW_register_driver(const ddraw_driver *driver)
546 {
547     int i;
548
549     for (i = 0; i < DDRAW_num_drivers; i++)
550     {
551         if (DDRAW_drivers[i] == driver)
552         {
553             ERR("Driver reregistering %p\n", driver);
554             return;
555         }
556     }
557
558     if (DDRAW_num_drivers == sizeof(DDRAW_drivers)/sizeof(DDRAW_drivers[0]))
559     {
560         ERR("too many DDRAW drivers\n");
561         return;
562     }
563
564     DDRAW_drivers[DDRAW_num_drivers++] = driver;
565 }
566
567 /* This totally doesn't belong here. */
568 LONG DDRAW_width_bpp_to_pitch(DWORD width, DWORD bpp)
569 {
570     LONG pitch;
571
572     assert(bpp != 0); /* keeps happening... */
573
574     if (bpp == 15) bpp = 16;
575     pitch = width * (bpp / 8);
576     return pitch + (8 - (pitch % 8)) % 8;
577 }