Added mappings for a few messages.
[wine] / dlls / ddraw / dsurface / user.c
1 /*      User-based primary surface driver
2  *
3  * Copyright 2000-2001 TransGaming Technologies Inc.
4  */
5
6 #include "config.h"
7
8 #include <assert.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "winerror.h"
13 #include "debugtools.h"
14 #include "ddraw_private.h"
15 #include "dsurface/main.h"
16 #include "dsurface/dib.h"
17 #include "dsurface/user.h"
18 #include "dsurface/wndproc.h"
19
20 DEFAULT_DEBUG_CHANNEL(ddraw);
21
22 /* if you use OWN_WINDOW, don't use SYNC_UPDATE, or you may get trouble */
23 /* #define SYNC_UPDATE */
24 #define OWN_WINDOW
25
26 #ifdef OWN_WINDOW
27 static void User_create_own_window(IDirectDrawSurfaceImpl* This);
28 static void User_destroy_own_window(IDirectDrawSurfaceImpl* This);
29 #endif
30 #ifndef SYNC_UPDATE
31 static DWORD CALLBACK User_update_thread(LPVOID);
32 #endif
33 static void User_copy_to_screen(IDirectDrawSurfaceImpl* This, LPCRECT rc);
34 static void User_copy_from_screen(IDirectDrawSurfaceImpl* This, LPCRECT rc);
35
36 static HWND get_display_window(IDirectDrawSurfaceImpl* This, LPPOINT pt);
37
38 static ICOM_VTABLE(IDirectDrawSurface7) User_IDirectDrawSurface7_VTable;
39
40 HRESULT
41 User_DirectDrawSurface_Construct(IDirectDrawSurfaceImpl* This,
42                                  IDirectDrawImpl* pDD,
43                                  const DDSURFACEDESC2* pDDSD)
44 {
45     USER_PRIV_VAR(priv, This);
46     HRESULT hr;
47
48     TRACE("(%p,%p,%p)\n",This,pDD,pDDSD);
49     hr = DIB_DirectDrawSurface_Construct(This, pDD, pDDSD);
50     if (FAILED(hr)) return hr;
51
52     ICOM_INIT_INTERFACE(This, IDirectDrawSurface7,
53                         User_IDirectDrawSurface7_VTable);
54
55     This->final_release = User_DirectDrawSurface_final_release;
56     This->duplicate_surface = User_DirectDrawSurface_duplicate_surface;
57
58     This->lock_update   = User_DirectDrawSurface_lock_update;
59     This->unlock_update = User_DirectDrawSurface_unlock_update;
60
61     This->flip_data   = User_DirectDrawSurface_flip_data;
62     This->flip_update = User_DirectDrawSurface_flip_update;
63
64     This->get_dc     = User_DirectDrawSurface_get_dc;
65     This->release_dc = User_DirectDrawSurface_release_dc;
66
67     This->set_palette    = User_DirectDrawSurface_set_palette;
68     This->update_palette = User_DirectDrawSurface_update_palette;
69
70     This->get_gamma_ramp = User_DirectDrawSurface_get_gamma_ramp;
71     This->set_gamma_ramp = User_DirectDrawSurface_set_gamma_ramp;
72
73     This->get_display_window = User_DirectDrawSurface_get_display_window;
74
75     if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
76     {
77 #ifdef OWN_WINDOW
78         DirectDrawSurface_RegisterClass();
79 #endif
80 #ifndef SYNC_UPDATE
81         priv->user.update_event = CreateEventA(NULL, FALSE, FALSE, NULL);
82         priv->user.update_thread = CreateThread(NULL, 0, User_update_thread, This, 0, NULL);
83 #ifdef OWN_WINDOW
84         if (This->ddraw_owner->cooperative_level & DDSCL_FULLSCREEN) {
85             /* wait for window creation (or update thread destruction) */
86             while (WaitForMultipleObjects(1, &priv->user.update_thread, FALSE, 10) == WAIT_TIMEOUT)
87                 if (This->more.lpDDRAWReserved) break;
88             if (!This->more.lpDDRAWReserved) {
89                 ERR("window creation failed\n");
90             }
91         }
92 #endif
93 #else
94 #ifdef OWN_WINDOW
95         User_create_own_window(This);
96 #endif
97 #endif
98         if (!This->more.lpDDRAWReserved)
99             This->more.lpDDRAWReserved = (LPVOID)pDD->window;
100     }
101
102     return DIB_DirectDrawSurface_alloc_dc(This, &priv->user.cached_dc);
103 }
104
105 HRESULT
106 User_DirectDrawSurface_Create(IDirectDrawImpl *pDD,
107                               const DDSURFACEDESC2 *pDDSD,
108                               LPDIRECTDRAWSURFACE7 *ppSurf,
109                               IUnknown *pUnkOuter)
110 {
111     IDirectDrawSurfaceImpl* This;
112     HRESULT hr;
113     assert(pUnkOuter == NULL);
114
115     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
116                      sizeof(*This) + sizeof(User_DirectDrawSurfaceImpl));
117     if (This == NULL) return E_OUTOFMEMORY;
118
119     This->private = (User_DirectDrawSurfaceImpl*)(This+1);
120
121     hr = User_DirectDrawSurface_Construct(This, pDD, pDDSD);
122     if (FAILED(hr))
123         HeapFree(GetProcessHeap(), 0, This);
124     else
125         *ppSurf = ICOM_INTERFACE(This, IDirectDrawSurface7);
126
127     return hr;
128 }
129
130 void User_DirectDrawSurface_final_release(IDirectDrawSurfaceImpl* This)
131 {
132     USER_PRIV_VAR(priv, This);
133
134     if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
135     {
136 #ifndef SYNC_UPDATE
137         HANDLE event = priv->user.update_event;
138         priv->user.update_event = 0;
139         SetEvent(event);
140         TRACE("waiting for update thread to terminate...\n");
141 #ifdef OWN_WINDOW
142         /* dispatch any waiting sendmessages */
143         {
144             MSG msg;
145             PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
146         }
147         /* to avoid deadlocks, allow SendMessages from update thread
148          * through while we wait for it */
149         while (MsgWaitForMultipleObjects(1, &priv->user.update_thread, FALSE,
150                                          INFINITE, QS_SENDMESSAGE) == WAIT_OBJECT_0+1)
151         {
152             MSG msg;
153             PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
154         }
155 #else
156         WaitForSingleObject(priv->user.update_thread,INFINITE);
157 #endif
158         TRACE("update thread terminated\n");
159         CloseHandle(priv->user.update_thread);
160 #else
161 #ifdef OWN_WINDOW
162         User_destroy_own_window(This);
163 #endif
164 #endif
165         This->more.lpDDRAWReserved = 0;
166 #ifdef OWN_WINDOW
167         DirectDrawSurface_UnregisterClass();
168 #endif
169     }
170     DIB_DirectDrawSurface_free_dc(This, priv->user.cached_dc);
171     DIB_DirectDrawSurface_final_release(This);
172 }
173
174 void User_DirectDrawSurface_lock_update(IDirectDrawSurfaceImpl* This,
175                                         LPCRECT pRect)
176 {
177     User_copy_from_screen(This, pRect);
178 }
179
180 void User_DirectDrawSurface_unlock_update(IDirectDrawSurfaceImpl* This,
181                                           LPCRECT pRect)
182 {
183 #ifdef SYNC_UPDATE
184     User_copy_to_screen(This, pRect);
185 #else
186     USER_PRIV_VAR(priv, This);
187     SetEvent(priv->user.update_event);
188 #endif
189 }
190
191 void User_DirectDrawSurface_set_palette(IDirectDrawSurfaceImpl* This,
192                                         IDirectDrawPaletteImpl* pal) 
193 {
194     USER_PRIV_VAR(priv, This);
195
196     if (!pal) {
197         FIXME("selecting null palette\n");
198         /* I don't think selecting GDI object 0 will work, we should select
199          * the original palette, returned by the first SelectPalette */
200         SelectPalette(priv->user.cached_dc, 0, FALSE);
201         return;
202     }
203
204     SelectPalette(priv->user.cached_dc, pal->hpal, FALSE);
205
206     DIB_DirectDrawSurface_set_palette(This, pal);
207 }
208
209 void User_DirectDrawSurface_update_palette(IDirectDrawSurfaceImpl* This,
210                                            IDirectDrawPaletteImpl* pal,
211                                            DWORD dwStart, DWORD dwCount,
212                                            LPPALETTEENTRY palent)
213 {
214     USER_PRIV_VAR(priv, This);
215
216     DIB_DirectDrawSurface_update_palette(This, pal, dwStart, dwCount, palent);
217     /* FIXME: realize palette on display window */
218
219 #ifndef SYNC_UPDATE
220     /* with async updates, it's probably cool to force an update */
221     SetEvent(priv->user.update_event);
222 #endif
223 }
224
225 HRESULT User_DirectDrawSurface_duplicate_surface(IDirectDrawSurfaceImpl* This,
226                                                  LPDIRECTDRAWSURFACE7* ppDup)
227 {
228     return User_DirectDrawSurface_Create(This->ddraw_owner,
229                                          &This->surface_desc, ppDup, NULL);
230 }
231
232 BOOL User_DirectDrawSurface_flip_data(IDirectDrawSurfaceImpl* front,
233                                       IDirectDrawSurfaceImpl* back,
234                                       DWORD dwFlags)
235 {
236     USER_PRIV_VAR(front_priv, front);
237     USER_PRIV_VAR(back_priv, back);
238
239     {
240         HDC tmp;
241         tmp = front_priv->user.cached_dc;
242         front_priv->user.cached_dc = back_priv->user.cached_dc;
243         back_priv->user.cached_dc = tmp;
244     }
245
246     return DIB_DirectDrawSurface_flip_data(front, back, dwFlags);
247 }
248
249 void User_DirectDrawSurface_flip_update(IDirectDrawSurfaceImpl* This, DWORD dwFlags)
250 {
251 #ifdef SYNC_UPDATE
252     assert(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE);
253     User_copy_to_screen(This,NULL);
254 #else
255     USER_PRIV_VAR(priv, This);
256     assert(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE);
257     SetEvent(priv->user.update_event);
258 #endif
259 }
260
261 HRESULT User_DirectDrawSurface_get_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
262 {
263     USER_PRIV_VAR(priv, This);
264
265     *phDC = priv->user.cached_dc;
266     return S_OK;
267 }
268
269 HRESULT User_DirectDrawSurface_release_dc(IDirectDrawSurfaceImpl* This,
270                                           HDC hDC)
271 {
272     return S_OK;
273 }
274
275 HRESULT User_DirectDrawSurface_get_gamma_ramp(IDirectDrawSurfaceImpl* This,
276                                               DWORD dwFlags,
277                                               LPDDGAMMARAMP lpGammaRamp)
278 {
279     if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
280     {
281         POINT offset;
282         HWND hDisplayWnd;
283         HDC hDisplayDC;
284         HRESULT hr;
285         hDisplayWnd = get_display_window(This, &offset);
286         hDisplayDC = GetDCEx(hDisplayWnd, 0, DCX_CLIPSIBLINGS|DCX_CACHE);
287         hr = GetDeviceGammaRamp(hDisplayDC, lpGammaRamp) ? DD_OK : DDERR_UNSUPPORTED;
288         ReleaseDC(hDisplayWnd, hDisplayDC);
289         return hr;
290     }
291     return Main_DirectDrawSurface_get_gamma_ramp(This, dwFlags, lpGammaRamp);
292 }
293
294 HRESULT User_DirectDrawSurface_set_gamma_ramp(IDirectDrawSurfaceImpl* This,
295                                               DWORD dwFlags,
296                                               LPDDGAMMARAMP lpGammaRamp)
297 {
298     if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
299     {
300         POINT offset;
301         HWND hDisplayWnd;
302         HDC hDisplayDC;
303         HRESULT hr;
304         hDisplayWnd = get_display_window(This, &offset);
305         hDisplayDC = GetDCEx(hDisplayWnd, 0, DCX_CLIPSIBLINGS|DCX_CACHE);
306         hr = SetDeviceGammaRamp(hDisplayDC, lpGammaRamp) ? DD_OK : DDERR_UNSUPPORTED;
307         ReleaseDC(hDisplayWnd, hDisplayDC);
308         return hr;
309     }
310     return Main_DirectDrawSurface_set_gamma_ramp(This, dwFlags, lpGammaRamp);
311 }
312
313 /* Returns the window that hosts the display.
314  * *pt is set to the upper left position of the window relative to the
315  * upper left corner of the surface. */
316 static HWND get_display_window(IDirectDrawSurfaceImpl* This, LPPOINT pt)
317 {
318     memset(pt, 0, sizeof(*pt));
319
320     if (This->ddraw_owner->cooperative_level & DDSCL_FULLSCREEN)
321     {
322 #ifdef OWN_WINDOW
323         USER_PRIV_VAR(priv, This);
324 #if 1
325         SetWindowPos(priv->user.window, HWND_TOP, 0, 0, 0, 0,
326                      SWP_DEFERERASE|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|
327                      SWP_NOREDRAW|SWP_NOSENDCHANGING|SWP_NOSIZE);
328 #endif
329         return priv->user.window;
330 #else
331         return This->ddraw_owner->window;
332 #endif
333     }
334     else
335     {
336         if (This->clipper != NULL)
337         {
338             /* looks silly, but since we don't have the clipper locked */
339             HWND hWnd = This->clipper->hWnd;
340
341             if (hWnd != 0)
342             {
343                 ClientToScreen(hWnd, pt);
344                 return hWnd;
345             }
346             else
347             {
348                 static BOOL warn = 0;
349                 if (!warn++) FIXME("clipper clip lists not supported\n");
350
351                 return GetDesktopWindow();
352             }
353         }
354         else
355         {
356             static BOOL warn = 0;
357             if (!warn++) WARN("hosting on root\n");
358
359             return GetDesktopWindow();
360         }
361     }
362 }
363
364 HWND User_DirectDrawSurface_get_display_window(IDirectDrawSurfaceImpl* This)
365 {
366     POINT offset;
367     return get_display_window(This, &offset);
368 }
369
370 #ifdef OWN_WINDOW
371 static void User_create_own_window(IDirectDrawSurfaceImpl* This)
372 {
373     USER_PRIV_VAR(priv, This);
374
375     if (This->ddraw_owner->cooperative_level & DDSCL_FULLSCREEN)
376     {
377         priv->user.window = CreateWindowExA(WS_EX_TOPMOST |
378                                             WS_EX_LAYERED |
379                                             WS_EX_TRANSPARENT,
380                                             "WINE_DDRAW", "DirectDraw",
381                                             WS_POPUP,
382                                             0, 0,
383                                             This->surface_desc.dwWidth,
384                                             This->surface_desc.dwHeight,
385                                             GetDesktopWindow(),
386                                             0, 0, This);
387         This->more.lpDDRAWReserved = (LPVOID)priv->user.window;
388         SetWindowPos(priv->user.window, HWND_TOP, 0, 0, 0, 0,
389                      SWP_DEFERERASE|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|
390                      SWP_NOREDRAW|SWP_NOSENDCHANGING|SWP_NOSIZE|SWP_SHOWWINDOW);
391         UpdateWindow(priv->user.window);
392     }
393 }
394
395 static void User_destroy_own_window(IDirectDrawSurfaceImpl* This)
396 {
397     USER_PRIV_VAR(priv, This);
398
399     if (priv->user.window)
400     {
401         SetWindowPos(priv->user.window, 0, 0, 0, 0, 0,
402                      SWP_DEFERERASE|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOZORDER|
403                      SWP_NOREDRAW|SWP_NOSENDCHANGING|SWP_NOSIZE|SWP_HIDEWINDOW);
404         This->more.lpDDRAWReserved = NULL;
405         DestroyWindow(priv->user.window);
406         priv->user.window = 0;
407     }
408 }
409 #endif
410
411 #ifndef SYNC_UPDATE
412 static DWORD CALLBACK User_update_thread(LPVOID arg)
413 {
414     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)arg;
415     USER_PRIV_VAR(priv, This);
416     volatile DWORD *pActive = (volatile DWORD *)&priv->user.update_event;
417     HANDLE event = *pActive;
418
419 #ifdef OWN_WINDOW
420     User_create_own_window(This);
421 #endif
422
423     /* the point of this is that many games lock the primary surface
424      * multiple times per frame; this thread will then simply copy as
425      * often as it can without keeping the main thread waiting for
426      * each unlock, thus keeping the frame rate high */
427     do {
428 #ifdef OWN_WINDOW
429         DWORD ret = MsgWaitForMultipleObjects(1, &event, FALSE, INFINITE, QS_ALLINPUT);
430         MSG msg;
431
432         while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
433         {
434             switch (msg.message) {
435             case WM_PAINT:
436                 DispatchMessageA(&msg);
437                 break;
438             default:
439                 /* since we risk getting keyboard messages posted to us,
440                  * repost posted messages to cooperative window */
441                 PostMessageA(This->ddraw_owner->window, msg.message, msg.wParam, msg.lParam);
442             }
443         }
444 #else
445         DWORD ret = WaitForSingleObject(event, INFINITE);
446 #endif
447         if (ret == WAIT_OBJECT_0)
448         {
449             if (*pActive)
450                 User_copy_to_screen(This, NULL);
451             else
452                 break;
453         }
454         else if (ret != WAIT_OBJECT_0+1) break;
455     } while (TRUE);
456
457 #ifdef OWN_WINDOW
458     User_destroy_own_window(This);
459 #endif
460
461     return 0;
462 }
463 #endif
464
465 static void User_copy_to_screen(IDirectDrawSurfaceImpl* This, LPCRECT rc)
466 {
467     /* rc is unused. We copy the whole thing. */
468
469     if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
470     {
471         POINT offset;
472         HWND hDisplayWnd;
473         HDC hDisplayDC;
474         HDC hSurfaceDC;
475
476         if (FAILED(This->get_dc(This, &hSurfaceDC)))
477             return;
478
479         hDisplayWnd = get_display_window(This, &offset);
480         hDisplayDC = GetDCEx(hDisplayWnd, 0, DCX_CLIPSIBLINGS|DCX_CACHE);
481 #if 0
482         /* FIXME: this doesn't work... if users really want to run
483          * X in 8bpp, then we need to call directly into display.drv
484          * (or Wine's equivalent), and force a private colormap
485          * without default entries. */
486         if (This->palette) {
487             SelectPalette(hDisplayDC, This->palette->hpal, FALSE);
488             RealizePalette(hDisplayDC); /* sends messages => deadlocks */
489         }
490 #endif
491
492         BitBlt(hDisplayDC, 0, 0, This->surface_desc.dwWidth,
493                This->surface_desc.dwHeight, hSurfaceDC, offset.x, offset.y,
494                SRCCOPY);
495
496         ReleaseDC(hDisplayWnd, hDisplayDC);
497     }
498 }
499
500 static void User_copy_from_screen(IDirectDrawSurfaceImpl* This, LPCRECT rc)
501 {
502     /* rc is unused. We copy the whole thing. */
503
504     if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
505     {
506         POINT offset;
507         HWND hDisplayWnd = get_display_window(This, &offset);
508         HDC hDisplayDC = GetDC(hDisplayWnd);
509
510         BitBlt(This->hDC, offset.x, offset.y, This->surface_desc.dwWidth,
511                This->surface_desc.dwHeight, hDisplayDC, 0, 0, SRCCOPY);
512
513         ReleaseDC(hDisplayWnd, hDisplayDC);
514     }
515 }
516
517 static ICOM_VTABLE(IDirectDrawSurface7) User_IDirectDrawSurface7_VTable =
518 {
519     Main_DirectDrawSurface_QueryInterface,
520     Main_DirectDrawSurface_AddRef,
521     Main_DirectDrawSurface_Release,
522     Main_DirectDrawSurface_AddAttachedSurface,
523     Main_DirectDrawSurface_AddOverlayDirtyRect,
524     DIB_DirectDrawSurface_Blt,
525     Main_DirectDrawSurface_BltBatch,
526     DIB_DirectDrawSurface_BltFast,
527     Main_DirectDrawSurface_DeleteAttachedSurface,
528     Main_DirectDrawSurface_EnumAttachedSurfaces,
529     Main_DirectDrawSurface_EnumOverlayZOrders,
530     Main_DirectDrawSurface_Flip,
531     Main_DirectDrawSurface_GetAttachedSurface,
532     Main_DirectDrawSurface_GetBltStatus,
533     Main_DirectDrawSurface_GetCaps,
534     Main_DirectDrawSurface_GetClipper,
535     Main_DirectDrawSurface_GetColorKey,
536     Main_DirectDrawSurface_GetDC,
537     Main_DirectDrawSurface_GetFlipStatus,
538     Main_DirectDrawSurface_GetOverlayPosition,
539     Main_DirectDrawSurface_GetPalette,
540     Main_DirectDrawSurface_GetPixelFormat,
541     Main_DirectDrawSurface_GetSurfaceDesc,
542     Main_DirectDrawSurface_Initialize,
543     Main_DirectDrawSurface_IsLost,
544     Main_DirectDrawSurface_Lock,
545     Main_DirectDrawSurface_ReleaseDC,
546     DIB_DirectDrawSurface_Restore,
547     Main_DirectDrawSurface_SetClipper,
548     Main_DirectDrawSurface_SetColorKey,
549     Main_DirectDrawSurface_SetOverlayPosition,
550     Main_DirectDrawSurface_SetPalette,
551     Main_DirectDrawSurface_Unlock,
552     Main_DirectDrawSurface_UpdateOverlay,
553     Main_DirectDrawSurface_UpdateOverlayDisplay,
554     Main_DirectDrawSurface_UpdateOverlayZOrder,
555     Main_DirectDrawSurface_GetDDInterface,
556     Main_DirectDrawSurface_PageLock,
557     Main_DirectDrawSurface_PageUnlock,
558     DIB_DirectDrawSurface_SetSurfaceDesc,
559     Main_DirectDrawSurface_SetPrivateData,
560     Main_DirectDrawSurface_GetPrivateData,
561     Main_DirectDrawSurface_FreePrivateData,
562     Main_DirectDrawSurface_GetUniquenessValue,
563     Main_DirectDrawSurface_ChangeUniquenessValue,
564     Main_DirectDrawSurface_SetPriority,
565     Main_DirectDrawSurface_GetPriority,
566     Main_DirectDrawSurface_SetLOD,
567     Main_DirectDrawSurface_GetLOD
568 };