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