Implement A->W call for GetNamedSecurityInfo.
[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 #define GLPRIVATE_NO_REDEFINE
26
27 #include "config.h"
28 #include "wine/port.h"
29
30 #include <assert.h>
31 #include <stdarg.h>
32 #include <string.h>
33 #include <stdlib.h>
34
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winnls.h"
38 #include "winerror.h"
39 #include "wingdi.h"
40
41 #include "ddraw.h"
42 #include "d3d.h"
43
44 /* This for all the enumeration and creation of D3D-related objects */
45 #include "ddraw_private.h"
46 #include "wine/debug.h"
47 #include "wine/library.h"
48
49 #include "gl_private.h"
50
51 #undef GLPRIVATE_NO_REDEFINE
52
53 #define MAX_DDRAW_DRIVERS 3
54 static const ddraw_driver* DDRAW_drivers[MAX_DDRAW_DRIVERS];
55 static int DDRAW_num_drivers; /* = 0 */
56 static int DDRAW_default_driver;
57
58 void (*wine_tsx11_lock_ptr)(void) = NULL;
59 void (*wine_tsx11_unlock_ptr)(void) = NULL;
60
61 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
62
63 /**********************************************************************/
64
65 typedef struct {
66     LPVOID lpCallback;
67     LPVOID lpContext;
68 } DirectDrawEnumerateProcData;
69
70 BOOL opengl_initialized = 0;
71
72 #ifdef HAVE_OPENGL
73
74 #include "mesa_private.h"
75
76 static void *gl_handle = NULL;
77
78 #define GL_API_FUNCTION(f) typeof(f) * p##f;
79 #include "gl_api.h"
80 #undef GL_API_FUNCTION
81
82 #ifndef SONAME_LIBGL
83 #define SONAME_LIBGL "libGL.so"
84 #endif
85
86 static BOOL DDRAW_bind_to_opengl( void )
87 {
88     const char *glname = SONAME_LIBGL;
89
90     gl_handle = wine_dlopen(glname, RTLD_NOW, NULL, 0);
91     if (!gl_handle) {
92         WARN("Wine cannot find the OpenGL graphics library (%s).\n",glname);
93         return FALSE;
94     }
95
96 #define GL_API_FUNCTION(f)  \
97     if((p##f = wine_dlsym(gl_handle, #f, NULL, 0)) == NULL) \
98     { \
99         WARN("Can't find symbol %s\n", #f); \
100         goto sym_not_found; \
101     }
102 #include "gl_api.h"
103 #undef GL_API_FUNCTION
104
105     /* And now calls the function to initialize the various fields for the rendering devices */
106     return d3ddevice_init_at_startup(gl_handle);
107     
108 sym_not_found:
109     WARN("Wine cannot find certain functions that it needs inside the OpenGL\n"
110          "graphics library.  To enable Wine to use OpenGL please upgrade\n"
111          "your OpenGL libraries\n");
112     wine_dlclose(gl_handle, NULL, 0);
113     gl_handle = NULL;
114     return FALSE;
115 }
116
117 #endif /* HAVE_OPENGL */
118
119 BOOL s3tc_initialized = 0;
120
121 static void *s3tc_handle = NULL;
122
123 FUNC_FETCH_2D_TEXEL_RGBA_DXT1 fetch_2d_texel_rgba_dxt1;
124 FUNC_FETCH_2D_TEXEL_RGBA_DXT3 fetch_2d_texel_rgba_dxt3;
125 FUNC_FETCH_2D_TEXEL_RGBA_DXT5 fetch_2d_texel_rgba_dxt5;
126
127 #ifndef SONAME_LIBTXC_DXTN
128 #define SONAME_LIBTXC_DXTN "libtxc_dxtn.so"
129 #endif
130
131 static BOOL DDRAW_bind_to_s3tc( void )
132 {
133     const char * const s3tcname = SONAME_LIBTXC_DXTN;
134
135     s3tc_handle = wine_dlopen(s3tcname, RTLD_NOW, NULL, 0);
136     if (!s3tc_handle) {
137         TRACE("No S3TC software decompression library seems to be present (%s).\n",s3tcname);
138         return FALSE;
139     }
140     TRACE("Found S3TC software decompression library (%s).\n",s3tcname);
141
142 #define API_FUNCTION(f)  \
143     if((f = wine_dlsym(s3tc_handle, #f, NULL, 0)) == NULL) \
144     { \
145         WARN("Can't find symbol %s\n", #f); \
146         goto sym_not_found; \
147     }
148     API_FUNCTION(fetch_2d_texel_rgba_dxt1);
149     API_FUNCTION(fetch_2d_texel_rgba_dxt3);
150     API_FUNCTION(fetch_2d_texel_rgba_dxt5);
151 #undef API_FUNCTION
152
153     return TRUE;
154     
155 sym_not_found:
156     WARN("Wine cannot find functions that are necessary for S3TC software decompression\n");
157     wine_dlclose(s3tc_handle, NULL, 0);
158     s3tc_handle = NULL;
159     return FALSE;
160 }
161
162 /***********************************************************************
163  *              DirectDrawEnumerateExA (DDRAW.@)
164  */
165 HRESULT WINAPI DirectDrawEnumerateExA(
166     LPDDENUMCALLBACKEXA lpCallback, LPVOID lpContext, DWORD dwFlags)
167 {
168     int i;
169     TRACE("(%p,%p, %08lx)\n", lpCallback, lpContext, dwFlags);
170
171     if (TRACE_ON(ddraw)) {
172         TRACE("  Flags : ");
173         if (dwFlags & DDENUM_ATTACHEDSECONDARYDEVICES)
174             TRACE("DDENUM_ATTACHEDSECONDARYDEVICES ");
175         if (dwFlags & DDENUM_DETACHEDSECONDARYDEVICES)
176             TRACE("DDENUM_DETACHEDSECONDARYDEVICES ");
177         if (dwFlags & DDENUM_NONDISPLAYDEVICES)
178             TRACE("DDENUM_NONDISPLAYDEVICES ");
179         TRACE("\n");
180     }
181
182     for (i=0; i<DDRAW_num_drivers; i++)
183     {
184         TRACE("Enumerating %s/%s interface\n",
185               DDRAW_drivers[i]->info->szDriver,
186               DDRAW_drivers[i]->info->szDescription);
187
188         /* We have to pass NULL from the primary display device.
189          * RoadRage chapter 6's enumeration routine expects it. */
190         if (!lpCallback((DDRAW_default_driver == i) ? NULL
191                         :(LPGUID)&DDRAW_drivers[i]->info->guidDeviceIdentifier,
192                         (LPSTR)DDRAW_drivers[i]->info->szDescription,
193                         (LPSTR)DDRAW_drivers[i]->info->szDriver,
194                         lpContext, 0))
195             return DD_OK;
196     }
197
198     /* Unsupported flags */
199     if (dwFlags & DDENUM_NONDISPLAYDEVICES) {
200         FIXME("no non-display devices supported.\n");
201     }
202     if (dwFlags & DDENUM_DETACHEDSECONDARYDEVICES) {
203         FIXME("no detached secondary devices supported.\n");
204     }
205
206     return DD_OK;
207 }
208
209 /***********************************************************************
210  *              DirectDrawEnumerateExW (DDRAW.@)
211  */
212
213 static BOOL CALLBACK DirectDrawEnumerateExProcW(
214     GUID *lpGUID, LPSTR lpDriverDescription, LPSTR lpDriverName,
215     LPVOID lpContext, HMONITOR hm)
216 {
217     INT len;
218     BOOL bResult;
219     LPWSTR lpDriverDescriptionW, lpDriverNameW;
220     DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
221
222     len = MultiByteToWideChar( CP_ACP, 0, lpDriverDescription, -1, NULL, 0 );
223     lpDriverDescriptionW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
224     MultiByteToWideChar( CP_ACP, 0, lpDriverDescription, -1, lpDriverDescriptionW, len );
225
226     len = MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, NULL, 0 );
227     lpDriverNameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
228     MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, lpDriverNameW, len );
229
230     bResult = (*(LPDDENUMCALLBACKEXW *) pEPD->lpCallback)(lpGUID, lpDriverDescriptionW,
231                                                           lpDriverNameW, pEPD->lpContext, hm);
232
233     HeapFree(GetProcessHeap(), 0, lpDriverDescriptionW);
234     HeapFree(GetProcessHeap(), 0, lpDriverNameW);
235     return bResult;
236 }
237
238 HRESULT WINAPI DirectDrawEnumerateExW(
239   LPDDENUMCALLBACKEXW lpCallback, LPVOID lpContext, DWORD dwFlags)
240 {
241     DirectDrawEnumerateProcData epd;
242     epd.lpCallback = (LPVOID) lpCallback;
243     epd.lpContext = lpContext;
244
245     return DirectDrawEnumerateExA(DirectDrawEnumerateExProcW, (LPVOID) &epd, 0);
246 }
247
248 /***********************************************************************
249  *              DirectDrawEnumerateA (DDRAW.@)
250  */
251
252 static BOOL CALLBACK DirectDrawEnumerateProcA(
253         GUID *lpGUID, LPSTR lpDriverDescription, LPSTR lpDriverName,
254         LPVOID lpContext, HMONITOR hm)
255 {
256     DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
257
258     return ((LPDDENUMCALLBACKA) pEPD->lpCallback)(
259         lpGUID, lpDriverDescription, lpDriverName, pEPD->lpContext);
260 }
261
262 HRESULT WINAPI DirectDrawEnumerateA(
263   LPDDENUMCALLBACKA lpCallback, LPVOID lpContext)
264 {
265     DirectDrawEnumerateProcData epd;
266     epd.lpCallback = (LPVOID) lpCallback;
267     epd.lpContext = lpContext;
268
269     return DirectDrawEnumerateExA(DirectDrawEnumerateProcA, (LPVOID) &epd, 0);
270 }
271
272 /***********************************************************************
273  *              DirectDrawEnumerateW (DDRAW.@)
274  */
275
276 static BOOL WINAPI DirectDrawEnumerateProcW(
277   GUID *lpGUID, LPWSTR lpDriverDescription, LPWSTR lpDriverName,
278   LPVOID lpContext, HMONITOR hm)
279 {
280     DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
281
282     return ((LPDDENUMCALLBACKW) pEPD->lpCallback)(
283         lpGUID, lpDriverDescription, lpDriverName, pEPD->lpContext);
284 }
285
286 HRESULT WINAPI DirectDrawEnumerateW(
287   LPDDENUMCALLBACKW lpCallback, LPVOID lpContext)
288 {
289     DirectDrawEnumerateProcData epd;
290     epd.lpCallback = (LPVOID) lpCallback;
291     epd.lpContext = lpContext;
292
293     return DirectDrawEnumerateExW(DirectDrawEnumerateProcW, (LPVOID) &epd, 0);
294 }
295
296 /***********************************************************************
297  *              DirectDrawCreate (DDRAW.@)
298  */
299
300 const ddraw_driver* DDRAW_FindDriver(const GUID* pGUID)
301 {
302     static const GUID zeroGUID; /* gets zero-inited */
303
304     TRACE("(%s)\n", pGUID ? debugstr_guid(pGUID) : "(null)");
305
306     if (DDRAW_num_drivers == 0) return NULL;
307
308     if (pGUID == (LPGUID)DDCREATE_EMULATIONONLY
309         || pGUID == (LPGUID)DDCREATE_HARDWAREONLY)
310         pGUID = NULL;
311
312     if (pGUID == NULL || memcmp(pGUID, &zeroGUID, sizeof(GUID)) == 0)
313     {
314         /* Use the default driver. */
315         return DDRAW_drivers[DDRAW_default_driver];
316     }
317     else
318     {
319         /* Look for a matching GUID. */
320
321         int i;
322         for (i=0; i < DDRAW_num_drivers; i++)
323         {
324             if (IsEqualGUID(pGUID,
325                             &DDRAW_drivers[i]->info->guidDeviceIdentifier))
326                 break;
327         }
328
329         if (i < DDRAW_num_drivers)
330         {
331             return DDRAW_drivers[i];
332         }
333         else
334         {
335             ERR("(%s): did not recognize requested GUID.\n",debugstr_guid(pGUID));
336             return NULL;
337         }
338     }
339 }
340
341 static HRESULT DDRAW_Create(
342         LPGUID lpGUID, LPVOID *lplpDD, LPUNKNOWN pUnkOuter, REFIID iid, BOOL ex
343 ) {
344     const ddraw_driver* driver;
345     LPDIRECTDRAW7 pDD;
346     HRESULT hr;
347
348     TRACE("(%s,%p,%p,%d)\n", debugstr_guid(lpGUID), lplpDD, pUnkOuter, ex);
349
350     if (DDRAW_num_drivers == 0)
351     {
352         WARN("no DirectDraw drivers registered\n");
353         return DDERR_INVALIDDIRECTDRAWGUID;
354     }
355
356     if (lpGUID == (LPGUID)DDCREATE_EMULATIONONLY
357         || lpGUID == (LPGUID)DDCREATE_HARDWAREONLY)
358         lpGUID = NULL;
359
360     if (pUnkOuter != NULL)
361         return DDERR_INVALIDPARAMS; /* CLASS_E_NOAGGREGATION? */
362
363     driver = DDRAW_FindDriver(lpGUID);
364     if (driver == NULL) return DDERR_INVALIDDIRECTDRAWGUID;
365
366     hr = driver->create(lpGUID, &pDD, pUnkOuter, ex);
367     if (FAILED(hr)) return hr;
368
369     hr = IDirectDraw7_QueryInterface(pDD, iid, lplpDD);
370     IDirectDraw7_Release(pDD);
371     return hr;
372 }
373
374 /***********************************************************************
375  *              DirectDrawCreate (DDRAW.@)
376  *
377  * Only creates legacy IDirectDraw interfaces.
378  * Cannot create IDirectDraw7 interfaces.
379  * In theory.
380  */
381 HRESULT WINAPI DirectDrawCreate(
382         LPGUID lpGUID, LPDIRECTDRAW* lplpDD, LPUNKNOWN pUnkOuter
383 ) {
384     TRACE("(%s,%p,%p)\n", debugstr_guid(lpGUID), lplpDD, pUnkOuter);
385     return DDRAW_Create(lpGUID, (LPVOID*) lplpDD, pUnkOuter, &IID_IDirectDraw, FALSE);
386 }
387
388 /***********************************************************************
389  *              DirectDrawCreateEx (DDRAW.@)
390  *
391  * Only creates new IDirectDraw7 interfaces.
392  * Supposed to fail if legacy interfaces are requested.
393  * In theory.
394  */
395 HRESULT WINAPI DirectDrawCreateEx(
396         LPGUID lpGUID, LPVOID* lplpDD, REFIID iid, LPUNKNOWN pUnkOuter
397 ) {
398     TRACE("(%s,%p,%s,%p)\n", debugstr_guid(lpGUID), lplpDD, debugstr_guid(iid), pUnkOuter);
399
400     if (!IsEqualGUID(iid, &IID_IDirectDraw7))
401         return DDERR_INVALIDPARAMS;
402
403     return DDRAW_Create(lpGUID, lplpDD, pUnkOuter, iid, TRUE);
404 }
405
406 extern HRESULT Uninit_DirectDraw_Create(const GUID*, LPDIRECTDRAW7*,
407                                         LPUNKNOWN, BOOL);
408
409 /* This is for the class factory. */
410 static HRESULT DDRAW_CreateDirectDraw(IUnknown* pUnkOuter, REFIID iid,
411                                       LPVOID* ppObj)
412 {
413     LPDIRECTDRAW7 pDD;
414     HRESULT hr;
415     BOOL ex;
416
417     TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppObj);
418     
419     /* This is a mighty hack :-) */
420     if (IsEqualGUID(iid, &IID_IDirectDraw7))
421         ex = TRUE;
422     else
423         ex = FALSE;
424     
425     hr = Uninit_DirectDraw_Create(NULL, &pDD, pUnkOuter, ex);
426     if (FAILED(hr)) return hr;
427
428     hr = IDirectDraw7_QueryInterface(pDD, iid, ppObj);
429     IDirectDraw_Release(pDD);
430     return hr;
431 }
432
433 /******************************************************************************
434  * DirectDraw ClassFactory
435  */
436 typedef struct {
437     ICOM_VFIELD_MULTI(IClassFactory);
438
439     DWORD ref;
440     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID iid,
441                                  LPVOID *ppObj);
442 } IClassFactoryImpl;
443
444 struct object_creation_info
445 {
446     const CLSID *clsid;
447     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID riid,
448                                  LPVOID *ppObj);
449 };
450
451 /* There should be more, but these are the only ones listed in the header
452  * file. */
453 extern HRESULT DDRAW_CreateDirectDrawClipper(IUnknown *pUnkOuter, REFIID riid,
454                                              LPVOID *ppObj);
455
456 static const struct object_creation_info object_creation[] =
457 {
458     { &CLSID_DirectDraw,        DDRAW_CreateDirectDraw },
459     { &CLSID_DirectDraw7,       DDRAW_CreateDirectDraw },
460     { &CLSID_DirectDrawClipper, DDRAW_CreateDirectDrawClipper }
461 };
462
463 static HRESULT WINAPI
464 DDCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
465 {
466     ICOM_THIS(IClassFactoryImpl,iface);
467
468     TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj);
469     
470     if (IsEqualGUID(riid, &IID_IUnknown)
471         || IsEqualGUID(riid, &IID_IClassFactory))
472     {
473         IClassFactory_AddRef(iface);
474         *ppobj = This;
475         return S_OK;
476     }
477
478     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
479     return E_NOINTERFACE;
480 }
481
482 static ULONG WINAPI DDCF_AddRef(LPCLASSFACTORY iface) {
483     ICOM_THIS(IClassFactoryImpl,iface);
484
485     TRACE("(%p)->() incrementing from %ld.\n", This, This->ref);
486     
487     return ++(This->ref);
488 }
489
490 static ULONG WINAPI DDCF_Release(LPCLASSFACTORY iface) {
491     ICOM_THIS(IClassFactoryImpl,iface);
492
493     TRACE("(%p)->() decrementing from %ld.\n", This, This->ref);
494
495     if (--This->ref == 0)
496         HeapFree(GetProcessHeap(), 0, This);
497
498     return This->ref;
499 }
500
501
502 static HRESULT WINAPI DDCF_CreateInstance(
503         LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
504 ) {
505     ICOM_THIS(IClassFactoryImpl,iface);
506
507     TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
508
509     return This->pfnCreateInstance(pOuter, riid, ppobj);
510 }
511
512 static HRESULT WINAPI DDCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
513     ICOM_THIS(IClassFactoryImpl,iface);
514     FIXME("(%p)->(%d),stub!\n",This,dolock);
515     return S_OK;
516 }
517
518 static ICOM_VTABLE(IClassFactory) DDCF_Vtbl =
519 {
520     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
521     DDCF_QueryInterface,
522     DDCF_AddRef,
523     DDCF_Release,
524     DDCF_CreateInstance,
525     DDCF_LockServer
526 };
527
528 /*******************************************************************************
529  * DllGetClassObject [DDRAW.@]
530  * Retrieves class object from a DLL object
531  *
532  * NOTES
533  *    Docs say returns STDAPI
534  *
535  * PARAMS
536  *    rclsid [I] CLSID for the class object
537  *    riid   [I] Reference to identifier of interface for class object
538  *    ppv    [O] Address of variable to receive interface pointer for riid
539  *
540  * RETURNS
541  *    Success: S_OK
542  *    Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
543  *             E_UNEXPECTED
544  */
545 DWORD WINAPI DDRAW_DllGetClassObject(REFCLSID rclsid,REFIID riid,LPVOID *ppv)
546 {
547     unsigned int i;
548     IClassFactoryImpl *factory;
549
550     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
551
552     if ( !IsEqualGUID( &IID_IClassFactory, riid )
553          && ! IsEqualGUID( &IID_IUnknown, riid) )
554         return E_NOINTERFACE;
555
556     for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
557     {
558         if (IsEqualGUID(object_creation[i].clsid, rclsid))
559             break;
560     }
561
562     if (i == sizeof(object_creation)/sizeof(object_creation[0]))
563     {
564         FIXME("%s: no class found.\n", debugstr_guid(rclsid));
565         return CLASS_E_CLASSNOTAVAILABLE;
566     }
567
568     factory = HeapAlloc(GetProcessHeap(), 0, sizeof(*factory));
569     if (factory == NULL) return E_OUTOFMEMORY;
570
571     ICOM_INIT_INTERFACE(factory, IClassFactory, DDCF_Vtbl);
572     factory->ref = 1;
573
574     factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
575
576     *ppv = ICOM_INTERFACE(factory, IClassFactory);
577     return S_OK;
578 }
579
580
581 /*******************************************************************************
582  * DllCanUnloadNow [DDRAW.@]  Determines whether the DLL is in use.
583  *
584  * RETURNS
585  *    Success: S_OK
586  *    Failure: S_FALSE
587  */
588 DWORD WINAPI DDRAW_DllCanUnloadNow(void) {
589     FIXME("(void): stub\n");
590     return S_FALSE;
591 }
592
593 /******************************************************************************
594  * Initialisation
595  */
596
597 /* Choose which driver is considered the primary display driver. It will
598  * be created when we get a NULL guid for the DirectDrawCreate(Ex). */
599 static int DDRAW_ChooseDefaultDriver(void)
600 {
601     int i;
602     int best = 0;
603     int best_score = 0;
604
605     assert(DDRAW_num_drivers > 0);
606
607     /* This algorithm is really stupid. */
608     for (i=0; i < DDRAW_num_drivers; i++)
609     {
610         if (DDRAW_drivers[i]->preference > best_score)
611         {
612             best_score = DDRAW_drivers[i]->preference;
613             best = i;
614         }
615     }
616
617     assert(best_score > 0);
618
619     return best;
620 }
621
622 /***********************************************************************
623  *              DllMain (DDRAW.0)
624  */
625 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
626 {
627     /* If we were sufficiently cool, DDraw drivers would just be COM
628      * objects, registered with a particular component category. */
629
630     DDRAW_HAL_Init(hInstDLL, fdwReason, lpv);
631     DDRAW_User_Init(hInstDLL, fdwReason, lpv);
632
633     if (fdwReason == DLL_PROCESS_ATTACH)
634     {
635         HMODULE mod;
636
637         DisableThreadLibraryCalls(hInstDLL);
638
639         mod = GetModuleHandleA( "x11drv.dll" );
640         if (mod)
641         {
642             wine_tsx11_lock_ptr   = (void *)GetProcAddress( mod, "wine_tsx11_lock" );
643             wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_unlock" );
644         }
645 #ifdef HAVE_OPENGL
646         opengl_initialized = DDRAW_bind_to_opengl();
647 #endif /* HAVE_OPENGL */
648         s3tc_initialized = DDRAW_bind_to_s3tc();
649     }
650
651     if (DDRAW_num_drivers > 0)
652         DDRAW_default_driver = DDRAW_ChooseDefaultDriver();
653
654     return TRUE;
655 }
656
657 /* Register a direct draw driver. This should be called from your init
658  * function. (That's why there is no locking: your init func is called from
659  * our DllInit, which is serialised.) */
660 void DDRAW_register_driver(const ddraw_driver *driver)
661 {
662     int i;
663
664     for (i = 0; i < DDRAW_num_drivers; i++)
665     {
666         if (DDRAW_drivers[i] == driver)
667         {
668             ERR("Driver reregistering %p\n", driver);
669             return;
670         }
671     }
672
673     if (DDRAW_num_drivers == sizeof(DDRAW_drivers)/sizeof(DDRAW_drivers[0]))
674     {
675         ERR("too many DDRAW drivers\n");
676         return;
677     }
678
679     DDRAW_drivers[DDRAW_num_drivers++] = driver;
680 }
681
682 /* This totally doesn't belong here. */
683 LONG DDRAW_width_bpp_to_pitch(DWORD width, DWORD bpp)
684 {
685     LONG pitch;
686
687     assert(bpp != 0); /* keeps happening... */
688
689     if (bpp == 15) bpp = 16;
690     pitch = width * (bpp / 8);
691     return pitch + (8 - (pitch % 8)) % 8;
692 }