1 /*
2  * COPYRIGHT:            See COPYING in the top level directory
3  * PROJECT:              ReactOS DirectX
4  * FILE:                 dll/directx/ddraw/Ddraw/ddraw_setcooperativelevel.c
5  * PURPOSE:              IDirectDraw7::SetCooperativeLevel Implementation
6  * PROGRAMMER:           Magnus Olsen
7  *
8  */
9 
10 #include "rosdraw.h"
11 
12 HRESULT WINAPI
Main_DirectDraw_SetCooperativeLevel(LPDDRAWI_DIRECTDRAW_INT This,HWND hwnd,DWORD cooplevel)13 Main_DirectDraw_SetCooperativeLevel (LPDDRAWI_DIRECTDRAW_INT This, HWND hwnd, DWORD cooplevel)
14 {
15     HRESULT retVal = DD_OK;
16 
17 
18     DX_WINDBG_trace();
19 
20     _SEH2_TRY
21     {
22 
23         if (hwnd && !IsWindow(hwnd))
24         {
25             retVal = DDERR_INVALIDPARAMS;
26              _SEH2_LEAVE;
27         }
28 
29         // FIXME test if 0x20 exists as a flag and what thuse it do
30         if ( cooplevel & (~(DDSCL_FPUPRESERVE | DDSCL_FPUSETUP | DDSCL_MULTITHREADED | DDSCL_CREATEDEVICEWINDOW |
31                             DDSCL_SETDEVICEWINDOW | DDSCL_SETFOCUSWINDOW | DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE |
32                             DDSCL_NORMAL | DDSCL_NOWINDOWCHANGES | DDSCL_ALLOWREBOOT | DDSCL_FULLSCREEN)))
33         {
34 
35               retVal = DDERR_INVALIDPARAMS;
36              _SEH2_LEAVE;
37         }
38 
39         if (!( cooplevel & (DDSCL_NORMAL | DDSCL_EXCLUSIVE | DDSCL_SETFOCUSWINDOW)))
40         {
41               retVal = DDERR_INVALIDPARAMS;
42              _SEH2_LEAVE;
43         }
44 
45         if ((cooplevel & DDSCL_FPUSETUP) && (cooplevel & DDSCL_FPUPRESERVE))
46         {
47               retVal = DDERR_INVALIDPARAMS;
48              _SEH2_LEAVE;
49         }
50 
51         if ((cooplevel & DDSCL_EXCLUSIVE) && (!(cooplevel & DDSCL_FULLSCREEN)))
52         {
53               retVal = DDERR_INVALIDPARAMS;
54              _SEH2_LEAVE;
55         }
56 
57         if ((cooplevel & DDSCL_ALLOWMODEX) &&  (!(cooplevel & DDSCL_FULLSCREEN)))
58         {
59               retVal = DDERR_INVALIDPARAMS;
60              _SEH2_LEAVE;
61         }
62 
63         if ((cooplevel & (DDSCL_CREATEDEVICEWINDOW | DDSCL_SETFOCUSWINDOW)))
64         {
65               retVal = DDERR_INVALIDPARAMS;
66              _SEH2_LEAVE;
67         }
68 
69         if (!cooplevel)
70         {
71               retVal = DDERR_INVALIDPARAMS;
72              _SEH2_LEAVE;
73         }
74 
75 
76         /* NORMAL MODE */
77         if(!(cooplevel & (~DDSCL_NORMAL)))
78         {
79             /* FIXME in setup.c  set DDRAWI_UMODELOADED | DDRAWI_DISPLAYDRV | DDRAWI_EMULATIONINITIALIZED | DDRAWI_GDIDRV  | DDRAWI_ATTACHEDTODESKTOP */
80             /* FIXME in setup.c This->lpLcl->lpGbl->dwFlags =  */
81 
82             This->lpLcl->dwLocalFlags = DDRAWILCL_SETCOOPCALLED | DDRAWILCL_DIRECTDRAW7 ;
83             This->lpLcl->hWnd = (ULONG_PTR) hwnd;
84             This->lpLcl->hFocusWnd = (ULONG_PTR) hwnd;
85             This->lpLcl->lpGbl->lpExclusiveOwner=NULL;
86 
87             retVal = DD_OK;
88             _SEH2_LEAVE;
89         }
90 
91         /* FULLSCREEN */
92         if ((!(cooplevel & (~(DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN)))) ||
93            (!(cooplevel & (~(DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX)))))
94 
95         {
96             /* FIXME in setup.c This->lpLcl->lpGbl->dwFlags =  */
97 
98             if (hwnd == NULL)
99             {
100                 retVal = DDERR_INVALIDPARAMS;
101                 _SEH2_LEAVE;
102             }
103 
104             if( (GetWindowLongPtr(hwnd, GWL_STYLE) & WS_CHILD) )
105             {
106                 retVal = DDERR_INVALIDPARAMS;
107                 _SEH2_LEAVE;
108             }
109 
110             if( (This->lpLcl->lpGbl->lpExclusiveOwner != NULL) &&
111                 (This->lpLcl->lpGbl->lpExclusiveOwner != This->lpLcl) )
112             {
113                 retVal = DDERR_INVALIDPARAMS;
114                 _SEH2_LEAVE;
115             }
116 
117             This->lpLcl->lpGbl->lpExclusiveOwner = This-> lpLcl;
118 
119             This->lpLcl->dwLocalFlags = DDRAWILCL_SETCOOPCALLED     | DDRAWILCL_DIRECTDRAW7  | DDRAWILCL_HOOKEDHWND |
120                                         DDRAWILCL_HASEXCLUSIVEMODE  | DDRAWILCL_ISFULLSCREEN | DDRAWILCL_ACTIVEYES |
121                                         DDRAWILCL_CURSORCLIPPED;
122 
123             if (cooplevel & DDSCL_ALLOWMODEX)
124             {
125                 This->lpLcl->dwLocalFlags = This->lpLcl->dwLocalFlags | DDRAWILCL_ALLOWMODEX;
126             }
127 
128             This->lpLcl->hWnd = (ULONG_PTR) hwnd;
129             This->lpLcl->hFocusWnd = (ULONG_PTR) hwnd;
130 
131 
132             /* FIXME fullscreen are not finuish */
133 
134             retVal = DD_OK;
135             _SEH2_LEAVE;
136         }
137 
138     /*
139  * Code from wine, this functions have been cut and paste from wine 0.9.35
140  * and have been modify allot and are still in devloping so it match with
141  * msdn document struct and flags
142  */
143 
144 
145 
146         ///* Handle those levels first which set various hwnds */
147         //if(cooplevel & DDSCL_SETFOCUSWINDOW)
148         //{
149         //
150 
151         //    if(This->lpLcl->dwLocalFlags & DDRAWILCL_SETCOOPCALLED)
152         //    {
153         //        retVal = DDERR_HWNDALREADYSET;
154         //         _SEH2_LEAVE;
155         //    }
156         //    else if( (This->lpLcl->dwLocalFlags & DDRAWILCL_ISFULLSCREEN) && window)
157         //    {
158         //        retVal = DDERR_HWNDALREADYSET;
159         //        _SEH2_LEAVE;
160         //    }
161 
162         //    This->lpLcl->hFocusWnd = (ULONG_PTR) hwnd;
163 
164 
165         //    /* Won't use the hwnd param for anything else */
166         //    hwnd = NULL;
167 
168         //    /* Use the focus window for drawing too */
169         //    This->lpLcl->hWnd = This->lpLcl->hFocusWnd;
170 
171         //}
172 
173         ///* DDSCL_NORMAL or DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE */
174         //if(cooplevel & DDSCL_NORMAL)
175         //{
176         //    /* Can't coexist with fullscreen or exclusive */
177         //    if(cooplevel & (DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE) )
178         //    {
179         //        retVal = DDERR_INVALIDPARAMS;
180         //         _SEH2_LEAVE;
181         //    }
182 
183         //    /* Switching from fullscreen? */
184         //    if(This->lpLcl->dwLocalFlags & DDRAWILCL_ISFULLSCREEN)
185         //    {
186         //        /* Restore the display mode */
187         //        Main_DirectDraw_RestoreDisplayMode(iface);
188 
189         //        This->lpLcl->dwLocalFlags &= ~DDRAWILCL_ISFULLSCREEN;
190         //        This->lpLcl->dwLocalFlags &= ~DDRAWILCL_HASEXCLUSIVEMODE;
191         //        This->lpLcl->dwLocalFlags &= ~DDRAWILCL_ALLOWMODEX;
192         //    }
193 
194         //    /* Don't override focus windows or private device windows */
195         //    if( hwnd &&
196         //        !(This->lpLcl->hFocusWnd) &&
197         //        !(This->lpLcl->dwObsolete1) &&
198         //        (hwnd != window) )
199         //    {
200         //        This->lpLcl->hWnd = (ULONG_PTR)hwnd;
201         //    }
202 
203         //    /* FIXME GL
204         //        IWineD3DDevice_SetFullscreen(This->wineD3DDevice,
205         //                             FALSE);
206         //    */
207         //    }
208         //    else if(cooplevel & DDSCL_FULLSCREEN)
209         //    {
210         //        /* Needs DDSCL_EXCLUSIVE */
211         //        if(!(cooplevel & DDSCL_EXCLUSIVE) )
212         //        {
213         //            retVal = DDERR_INVALIDPARAMS;
214         //            _SEH2_LEAVE;
215         //        }
216 
217         //        /* Switch from normal to full screen mode? */
218         //        if (!(This->lpLcl->dwLocalFlags & DDRAWILCL_HASEXCLUSIVEMODE))
219         //        {
220         //            /* FIXME GL
221         //            IWineD3DDevice_SetFullscreen(This->wineD3DDevice,
222         //                                 TRUE);
223         //            */
224         //        }
225 
226         //        /* Don't override focus windows or private device windows */
227         //        if( hwnd &&
228         //            !(This->lpLcl->hFocusWnd) &&
229         //            !(This->lpLcl->dwObsolete1) &&
230         //            (hwnd != window) )
231         //        {
232         //            This->lpLcl->hWnd =  (ULONG_PTR) hwnd;
233         //        }
234         //    }
235         //    else if(cooplevel & DDSCL_EXCLUSIVE)
236         //    {
237         //        retVal = DDERR_INVALIDPARAMS;
238         //        _SEH2_LEAVE;
239         //    }
240 
241         //    if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
242         //    {
243         //        /* Don't create a device window if a focus window is set */
244         //        if( !This->lpLcl->hFocusWnd)
245         //        {
246         //            HWND devicewindow = CreateWindowExW(0, classname, L"DDraw device window",
247         //                                        WS_POPUP, 0, 0,
248         //                                        GetSystemMetrics(SM_CXSCREEN),
249         //                                        GetSystemMetrics(SM_CYSCREEN),
250         //                                        NULL, NULL, GetModuleHandleW(0), NULL);
251 
252         //            ShowWindow(devicewindow, SW_SHOW);   /* Just to be sure */
253 
254         //            This->lpLcl->dwObsolete1 = (DWORD)devicewindow;
255         //        }
256         //    }
257 
258         //    if(cooplevel & DDSCL_MULTITHREADED && !(This->lpLcl->dwLocalFlags & DDRAWILCL_MULTITHREADED))
259         //    {
260         //        /* FIXME GL
261         //         * IWineD3DDevice_SetMultithreaded(This->wineD3DDevice);
262         //         */
263         //    }
264 
265 
266 
267         //    /* Store the cooperative_level */
268 
269         //    /* FIXME GL
270         //     * This->cooperative_level |= cooplevel;
271         //     */
272     }
273     _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
274     {
275     }
276     _SEH2_END;
277 
278 
279     return retVal;
280 }
281