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