Keep track of per-column information inside the listview.
[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         DPRINTF("  Flags : ");
69         if (dwFlags & DDENUM_ATTACHEDSECONDARYDEVICES)
70             DPRINTF("DDENUM_ATTACHEDSECONDARYDEVICES ");
71         if (dwFlags & DDENUM_DETACHEDSECONDARYDEVICES)
72             DPRINTF("DDENUM_DETACHEDSECONDARYDEVICES ");
73         if (dwFlags & DDENUM_NONDISPLAYDEVICES)
74             DPRINTF("DDENUM_NONDISPLAYDEVICES ");
75         DPRINTF("\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     if (DDRAW_num_drivers == 0)
245     {
246         WARN("no DirectDraw drivers registered\n");
247         return DDERR_INVALIDDIRECTDRAWGUID;
248     }
249
250     if (lpGUID == (LPGUID)DDCREATE_EMULATIONONLY
251         || lpGUID == (LPGUID)DDCREATE_HARDWAREONLY)
252         lpGUID = NULL;
253
254     TRACE("(%s,%p,%p)\n",debugstr_guid(lpGUID),lplpDD,pUnkOuter);
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   return DDRAW_Create(lpGUID,(LPVOID*)lplpDD,pUnkOuter,&IID_IDirectDraw,FALSE);
281 }
282
283 /***********************************************************************
284  *              DirectDrawCreateEx (DDRAW.@)
285  *
286  * Only creates new IDirectDraw7 interfaces.
287  * Supposed to fail if legacy interfaces are requested.
288  * In theory.
289  */
290 HRESULT WINAPI DirectDrawCreateEx(
291         LPGUID lpGUID, LPVOID* lplpDD, REFIID iid, LPUNKNOWN pUnkOuter
292 ) {
293     if (!IsEqualGUID(iid, &IID_IDirectDraw7))
294         return DDERR_INVALIDPARAMS;
295
296   return DDRAW_Create(lpGUID, lplpDD, pUnkOuter, iid, TRUE);
297 }
298
299 extern HRESULT Uninit_DirectDraw_Create(const GUID*, LPDIRECTDRAW7*,
300                                         LPUNKNOWN, BOOL);
301
302 /* This is for the class factory. */
303 static HRESULT DDRAW_CreateDirectDraw(IUnknown* pUnkOuter, REFIID iid,
304                                       LPVOID* ppObj)
305 {
306     LPDIRECTDRAW7 pDD;
307     HRESULT hr;
308
309     hr = Uninit_DirectDraw_Create(NULL, &pDD, pUnkOuter, TRUE); /* ex? */
310     if (FAILED(hr)) return hr;
311
312     hr = IDirectDraw7_QueryInterface(pDD, iid, ppObj);
313     IDirectDraw_Release(pDD);
314     return hr;
315 }
316
317 /******************************************************************************
318  * DirectDraw ClassFactory
319  */
320 typedef struct {
321     ICOM_VFIELD_MULTI(IClassFactory);
322
323     DWORD ref;
324     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID iid,
325                                  LPVOID *ppObj);
326 } IClassFactoryImpl;
327
328 struct object_creation_info
329 {
330     const CLSID *clsid;
331     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID riid,
332                                  LPVOID *ppObj);
333 };
334
335 /* There should be more, but these are the only ones listed in the header
336  * file. */
337 extern HRESULT DDRAW_CreateDirectDrawClipper(IUnknown *pUnkOuter, REFIID riid,
338                                              LPVOID *ppObj);
339
340 static const struct object_creation_info object_creation[] =
341 {
342     { &CLSID_DirectDraw,        DDRAW_CreateDirectDraw },
343     { &CLSID_DirectDraw7,       DDRAW_CreateDirectDraw },
344     { &CLSID_DirectDrawClipper, DDRAW_CreateDirectDrawClipper }
345 };
346
347 static HRESULT WINAPI
348 DDCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
349 {
350     ICOM_THIS(IClassFactoryImpl,iface);
351
352     if (IsEqualGUID(riid, &IID_IUnknown)
353         || IsEqualGUID(riid, &IID_IClassFactory))
354     {
355         IClassFactory_AddRef(iface);
356         *ppobj = This;
357         return S_OK;
358     }
359
360     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
361     return E_NOINTERFACE;
362 }
363
364 static ULONG WINAPI DDCF_AddRef(LPCLASSFACTORY iface) {
365     ICOM_THIS(IClassFactoryImpl,iface);
366     return ++(This->ref);
367 }
368
369 static ULONG WINAPI DDCF_Release(LPCLASSFACTORY iface) {
370     ICOM_THIS(IClassFactoryImpl,iface);
371
372     ULONG ref = --This->ref;
373
374     if (ref == 0)
375         HeapFree(GetProcessHeap(), 0, This);
376
377     return ref;
378 }
379
380
381 static HRESULT WINAPI DDCF_CreateInstance(
382         LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
383 ) {
384     ICOM_THIS(IClassFactoryImpl,iface);
385
386     TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
387
388     return This->pfnCreateInstance(pOuter, riid, ppobj);
389 }
390
391 static HRESULT WINAPI DDCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
392     ICOM_THIS(IClassFactoryImpl,iface);
393     FIXME("(%p)->(%d),stub!\n",This,dolock);
394     return S_OK;
395 }
396
397 static ICOM_VTABLE(IClassFactory) DDCF_Vtbl =
398 {
399     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
400     DDCF_QueryInterface,
401     DDCF_AddRef,
402     DDCF_Release,
403     DDCF_CreateInstance,
404     DDCF_LockServer
405 };
406
407 /*******************************************************************************
408  * DllGetClassObject [DDRAW.@]
409  * Retrieves class object from a DLL object
410  *
411  * NOTES
412  *    Docs say returns STDAPI
413  *
414  * PARAMS
415  *    rclsid [I] CLSID for the class object
416  *    riid   [I] Reference to identifier of interface for class object
417  *    ppv    [O] Address of variable to receive interface pointer for riid
418  *
419  * RETURNS
420  *    Success: S_OK
421  *    Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
422  *             E_UNEXPECTED
423  */
424 DWORD WINAPI DDRAW_DllGetClassObject(REFCLSID rclsid,REFIID riid,LPVOID *ppv)
425 {
426     unsigned int i;
427     IClassFactoryImpl *factory;
428
429     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
430
431     if ( !IsEqualGUID( &IID_IClassFactory, riid )
432          && ! IsEqualGUID( &IID_IUnknown, riid) )
433         return E_NOINTERFACE;
434
435     for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
436     {
437         if (IsEqualGUID(object_creation[i].clsid, rclsid))
438             break;
439     }
440
441     if (i == sizeof(object_creation)/sizeof(object_creation[0]))
442     {
443         FIXME("%s: no class found.\n", debugstr_guid(rclsid));
444         return CLASS_E_CLASSNOTAVAILABLE;
445     }
446
447     factory = HeapAlloc(GetProcessHeap(), 0, sizeof(*factory));
448     if (factory == NULL) return E_OUTOFMEMORY;
449
450     ICOM_INIT_INTERFACE(factory, IClassFactory, DDCF_Vtbl);
451     factory->ref = 1;
452
453     factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
454
455     *ppv = ICOM_INTERFACE(factory, IClassFactory);
456     return S_OK;
457 }
458
459
460 /*******************************************************************************
461  * DllCanUnloadNow [DDRAW.@]  Determines whether the DLL is in use.
462  *
463  * RETURNS
464  *    Success: S_OK
465  *    Failure: S_FALSE
466  */
467 DWORD WINAPI DDRAW_DllCanUnloadNow(void) {
468     FIXME("(void): stub\n");
469     return S_FALSE;
470 }
471
472 /******************************************************************************
473  * Initialisation
474  */
475
476 /* Choose which driver is considered the primary display driver. It will
477  * be created when we get a NULL guid for the DirectDrawCreate(Ex). */
478 static int DDRAW_ChooseDefaultDriver(void)
479 {
480     int i;
481     int best = 0;
482     int best_score = 0;
483
484     assert(DDRAW_num_drivers > 0);
485
486     /* This algorithm is really stupid. */
487     for (i=0; i < DDRAW_num_drivers; i++)
488     {
489         if (DDRAW_drivers[i]->preference > best_score)
490         {
491             best_score = DDRAW_drivers[i]->preference;
492             best = i;
493         }
494     }
495
496     assert(best_score > 0);
497
498     return best;
499 }
500
501 BOOL WINAPI DDRAW_DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
502 {
503     /* If we were sufficiently cool, DDraw drivers would just be COM
504      * objects, registered with a particular component category. */
505
506     DDRAW_HAL_Init(hInstDLL, fdwReason, lpv);
507     DDRAW_User_Init(hInstDLL, fdwReason, lpv);
508
509     if (fdwReason == DLL_PROCESS_ATTACH)
510     {
511         HMODULE mod = GetModuleHandleA( "x11drv.dll" );
512         if (mod)
513         {
514             wine_tsx11_lock_ptr   = (void *)GetProcAddress( mod, "wine_tsx11_lock" );
515             wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_unlock" );
516         }
517     }
518
519     if (DDRAW_num_drivers > 0)
520         DDRAW_default_driver = DDRAW_ChooseDefaultDriver();
521
522     return TRUE;
523 }
524
525 /* Register a direct draw driver. This should be called from your init
526  * function. (That's why there is no locking: your init func is called from
527  * our DllInit, which is serialised.) */
528 void DDRAW_register_driver(const ddraw_driver *driver)
529 {
530     int i;
531
532     for (i = 0; i < DDRAW_num_drivers; i++)
533     {
534         if (DDRAW_drivers[i] == driver)
535         {
536             ERR("Driver reregistering %p\n", driver);
537             return;
538         }
539     }
540
541     if (DDRAW_num_drivers == sizeof(DDRAW_drivers)/sizeof(DDRAW_drivers[0]))
542     {
543         ERR("too many DDRAW drivers\n");
544         return;
545     }
546
547     DDRAW_drivers[DDRAW_num_drivers++] = driver;
548 }
549
550 /* This totally doesn't belong here. */
551 LONG DDRAW_width_bpp_to_pitch(DWORD width, DWORD bpp)
552 {
553     LONG pitch;
554
555     assert(bpp != 0); /* keeps happening... */
556
557     if (bpp == 15) bpp = 16;
558     pitch = width * (bpp / 8);
559     return pitch + (8 - (pitch % 8)) % 8;
560 }