[YAROTOWS] Reintegrate the branch. For a brighter future.
[reactos.git] / reactos / subsystems / win32 / win32k / ntuser / windc.c
index 38ac3b4..e553417 100644 (file)
@@ -9,7 +9,7 @@
 
 /* INCLUDES ******************************************************************/
 
-#include <w32k.h>
+#include <win32k.h>
 
 #define NDEBUG
 #include <debug.h>
@@ -41,6 +41,9 @@ DceCreateDisplayDC(VOID)
   UNICODE_STRING DriverName;
   RtlInitUnicodeString(&DriverName, L"DISPLAY");
   hDC = IntGdiCreateDC(&DriverName, NULL, NULL, NULL, FALSE);
+
+  co_IntGraphicsCheck(TRUE);
+
 //
 // If NULL, first time through! Build the default window dc!
 //
@@ -59,7 +62,7 @@ DceCreateDisplayDC(VOID)
 
 static
 HRGN FASTCALL
-DceGetVisRgn(PWINDOW_OBJECT Window, ULONG Flags, HWND hWndChild, ULONG CFlags)
+DceGetVisRgn(PWND Window, ULONG Flags, HWND hWndChild, ULONG CFlags)
 {
   HRGN VisRgn;
 
@@ -75,12 +78,9 @@ DceGetVisRgn(PWINDOW_OBJECT Window, ULONG Flags, HWND hWndChild, ULONG CFlags)
 }
 
 PDCE FASTCALL
-DceAllocDCE(PWINDOW_OBJECT Window OPTIONAL, DCE_TYPE Type)
+DceAllocDCE(PWND Window OPTIONAL, DCE_TYPE Type)
 {
   PDCE pDce;
-  PWND Wnd = NULL;
-
-  if (Window) Wnd = Window->Wnd;
 
   pDce = ExAllocatePoolWithTag(PagedPool, sizeof(DCE), TAG_PDCE);
   if(!pDce)
@@ -94,9 +94,9 @@ DceAllocDCE(PWINDOW_OBJECT Window OPTIONAL, DCE_TYPE Type)
   }
   DCECount++;
   DPRINT("Alloc DCE's! %d\n",DCECount);
-  pDce->hwndCurrent = (Window ? Window->hSelf : NULL);
-  pDce->pwndOrg  = Wnd;
-  pDce->pwndClip = Wnd;
+  pDce->hwndCurrent = (Window ? Window->head.h : NULL);
+  pDce->pwndOrg  = Window;
+  pDce->pwndClip = Window;
   pDce->hrgnClip = NULL;
   pDce->hrgnClipPublic = NULL;
   pDce->hrgnSavedVis = NULL;
@@ -124,12 +124,12 @@ DceAllocDCE(PWINDOW_OBJECT Window OPTIONAL, DCE_TYPE Type)
   else
   {
      pDce->DCXFlags = DCX_DCEBUSY;
-     if (Wnd)
+     if (Window)
      {
         if (Type == DCE_WINDOW_DC)
         {
-          if (Wnd->style & WS_CLIPCHILDREN) pDce->DCXFlags |= DCX_CLIPCHILDREN;
-          if (Wnd->style & WS_CLIPSIBLINGS) pDce->DCXFlags |= DCX_CLIPSIBLINGS;
+          if (Window->style & WS_CLIPCHILDREN) pDce->DCXFlags |= DCX_CLIPCHILDREN;
+          if (Window->style & WS_CLIPSIBLINGS) pDce->DCXFlags |= DCX_CLIPSIBLINGS;
         }
      }
   }
@@ -137,12 +137,11 @@ DceAllocDCE(PWINDOW_OBJECT Window OPTIONAL, DCE_TYPE Type)
 }
 
 static VOID APIENTRY
-DceSetDrawable( PWINDOW_OBJECT Window OPTIONAL,
+DceSetDrawable( PWND Window OPTIONAL,
                 HDC hDC,
                 ULONG Flags,
                 BOOL SetClipOrigin)
 {
-  PWND Wnd;
   DC *dc = DC_LockDc(hDC);
   if(!dc)
       return;
@@ -154,16 +153,15 @@ DceSetDrawable( PWINDOW_OBJECT Window OPTIONAL,
   }
   else
   {
-      Wnd = Window->Wnd;
       if (Flags & DCX_WINDOW)
       {
-         dc->ptlDCOrig.x = Wnd->rcWindow.left;
-         dc->ptlDCOrig.y = Wnd->rcWindow.top;
+         dc->ptlDCOrig.x = Window->rcWindow.left;
+         dc->ptlDCOrig.y = Window->rcWindow.top;
       }
       else
       {
-         dc->ptlDCOrig.x = Wnd->rcClient.left;
-         dc->ptlDCOrig.y = Wnd->rcClient.top;
+         dc->ptlDCOrig.x = Window->rcClient.left;
+         dc->ptlDCOrig.y = Window->rcClient.top;
       }
   }
   DC_UnlockDc(dc);
@@ -181,7 +179,7 @@ DceDeleteClipRgn(DCE* Dce)
    }
    else if (Dce->hrgnClip != NULL)
    {
-      REGION_FreeRgnByHandle(Dce->hrgnClip);
+      GDIOBJ_FreeObjByHandle(Dce->hrgnClip, GDI_OBJECT_TYPE_REGION|GDI_OBJECT_TYPE_SILENT);
    }
 
    Dce->hrgnClip = NULL;
@@ -193,7 +191,7 @@ DceDeleteClipRgn(DCE* Dce)
 static INT FASTCALL
 DceReleaseDC(DCE* dce, BOOL EndPaint)
 {
-   if (DCX_DCEBUSY != (dce->DCXFlags & (DCX_DCEEMPTY | DCX_DCEBUSY)))
+   if (DCX_DCEBUSY != (dce->DCXFlags & (DCX_INDESTROY | DCX_DCEEMPTY | DCX_DCEBUSY)))
    {
       return 0;
    }
@@ -254,16 +252,15 @@ DceReleaseDC(DCE* dce, BOOL EndPaint)
 }
 
 static VOID FASTCALL
-DceUpdateVisRgn(DCE *Dce, PWINDOW_OBJECT Window, ULONG Flags)
+DceUpdateVisRgn(DCE *Dce, PWND Window, ULONG Flags)
 {
    HANDLE hRgnVisible = NULL;
    ULONG DcxFlags;
-   PWINDOW_OBJECT DesktopWindow;
+   PWND DesktopWindow;
 
    if (Flags & DCX_PARENTCLIP)
    {
-      PWINDOW_OBJECT Parent;
-      PWND ParentWnd;
+      PWND Parent;
 
       Parent = Window->spwndParent;
       if(!Parent)
@@ -272,9 +269,7 @@ DceUpdateVisRgn(DCE *Dce, PWINDOW_OBJECT Window, ULONG Flags)
          goto noparent;
       }
 
-      ParentWnd = Parent->Wnd;
-
-      if (ParentWnd->style & WS_CLIPSIBLINGS)
+      if (Parent->style & WS_CLIPSIBLINGS)
       {
          DcxFlags = DCX_CLIPSIBLINGS |
                     (Flags & ~(DCX_CLIPCHILDREN | DCX_WINDOW));
@@ -283,14 +278,14 @@ DceUpdateVisRgn(DCE *Dce, PWINDOW_OBJECT Window, ULONG Flags)
       {
          DcxFlags = Flags & ~(DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW);
       }
-      hRgnVisible = DceGetVisRgn(Parent, DcxFlags, Window->hSelf, Flags);
+      hRgnVisible = DceGetVisRgn(Parent, DcxFlags, Window->head.h, Flags);
    }
    else if (Window == NULL)
    {
       DesktopWindow = UserGetWindowObject(IntGetDesktopWindow());
       if (NULL != DesktopWindow)
       {
-         hRgnVisible = IntSysCreateRectRgnIndirect(&DesktopWindow->Wnd->rcWindow);
+         hRgnVisible = IntSysCreateRectRgnIndirect(&DesktopWindow->rcWindow);
       }
       else
       {
@@ -338,24 +333,21 @@ noparent:
 }
 
 HDC FASTCALL
-UserGetDCEx(PWINDOW_OBJECT Window OPTIONAL, HANDLE ClipRegion, ULONG Flags)
+UserGetDCEx(PWND Wnd OPTIONAL, HANDLE ClipRegion, ULONG Flags)
 {
-   PWINDOW_OBJECT Parent;
+   PWND Parent;
    ULONG DcxFlags;
    DCE* Dce = NULL;
    BOOL UpdateClipOrigin = FALSE;
-   PWND Wnd = NULL;
    HDC hDC = NULL;
    PPROCESSINFO ppi;
    PLIST_ENTRY pLE;
 
-   if (NULL == Window)
+   if (NULL == Wnd)
    {
       Flags &= ~DCX_USESTYLE;
       Flags |= DCX_CACHE;
    }
-   else
-       Wnd = Window->Wnd;
 
    if (Flags & (DCX_WINDOW | DCX_PARENTCLIP)) Flags |= DCX_CACHE;
 
@@ -420,9 +412,9 @@ UserGetDCEx(PWINDOW_OBJECT Window OPTIONAL, HANDLE ClipRegion, ULONG Flags)
       Flags &= ~(DCX_PARENTCLIP | DCX_CLIPCHILDREN);
    }
 
-   Parent = (Window ? Window->spwndParent : NULL);
+   Parent = (Wnd ? Wnd->spwndParent : NULL);
 
-   if (NULL == Window || !(Wnd->style & WS_CHILD) || NULL == Parent)
+   if (NULL == Wnd || !(Wnd->style & WS_CHILD) || NULL == Parent)
    {
       Flags &= ~DCX_PARENTCLIP;
       Flags |= DCX_CLIPSIBLINGS;
@@ -434,10 +426,10 @@ UserGetDCEx(PWINDOW_OBJECT Window OPTIONAL, HANDLE ClipRegion, ULONG Flags)
    if (Flags & DCX_PARENTCLIP)
    {
       if ((Wnd->style & WS_VISIBLE) &&
-          (Parent->Wnd->style & WS_VISIBLE))
+          (Parent->style & WS_VISIBLE))
       {
          Flags &= ~DCX_CLIPCHILDREN;
-         if (Parent->Wnd->style & WS_CLIPSIBLINGS)
+         if (Parent->style & WS_CLIPSIBLINGS)
          {
             Flags |= DCX_CLIPSIBLINGS;
          }
@@ -478,7 +470,7 @@ UserGetDCEx(PWINDOW_OBJECT Window OPTIONAL, HANDLE ClipRegion, ULONG Flags)
             {
                DceEmpty = Dce;
             }
-            else if (Dce->hwndCurrent == (Window ? Window->hSelf : NULL) &&
+            else if (Dce->hwndCurrent == (Wnd ? Wnd->head.h : NULL) &&
                      ((Dce->DCXFlags & DCX_CACHECOMPAREMASK) == DcxFlags))
             {
                UpdateClipOrigin = TRUE;
@@ -499,7 +491,7 @@ UserGetDCEx(PWINDOW_OBJECT Window OPTIONAL, HANDLE ClipRegion, ULONG Flags)
       }
       if (!Dce) return NULL;
 
-      Dce->hwndCurrent = (Window ? Window->hSelf : NULL);
+      Dce->hwndCurrent = (Wnd ? Wnd->head.h : NULL);
    }
    else // If we are here, we are POWNED or having CLASS.
    {
@@ -508,7 +500,7 @@ UserGetDCEx(PWINDOW_OBJECT Window OPTIONAL, HANDLE ClipRegion, ULONG Flags)
       Dce = CONTAINING_RECORD(pLE, DCE, List);
       do
       {   // Check for Window handle than HDC match for CLASS.
-          if ((Dce->hwndCurrent == Window->hSelf) ||
+          if ((Dce->hwndCurrent == Wnd->head.h) ||
               (Dce->hDC == hDC))
              break;
           pLE = Dce->List.Flink;
@@ -556,29 +548,35 @@ UserGetDCEx(PWINDOW_OBJECT Window OPTIONAL, HANDLE ClipRegion, ULONG Flags)
    {
       Flags |= DCX_INTERSECTRGN | DCX_KEEPCLIPRGN;
       Dce->DCXFlags |= DCX_INTERSECTRGN | DCX_KEEPCLIPRGN;
-      ClipRegion = Window->hrgnUpdate;
+      ClipRegion = Wnd->hrgnUpdate;
    }
 
    if (ClipRegion == (HRGN) 1)
    {
       if (!(Flags & DCX_WINDOW))
       {
-         Dce->hrgnClip = IntSysCreateRectRgnIndirect(&Window->Wnd->rcClient);
+         Dce->hrgnClip = IntSysCreateRectRgnIndirect(&Wnd->rcClient);
       }
       else
       {
-         Dce->hrgnClip = IntSysCreateRectRgnIndirect(&Window->Wnd->rcWindow);
+         Dce->hrgnClip = IntSysCreateRectRgnIndirect(&Wnd->rcWindow);
       }
       Dce->DCXFlags &= ~DCX_KEEPCLIPRGN;
    }
    else if (ClipRegion != NULL)
    {
+      if (Dce->hrgnClip != NULL)
+      {
+         DPRINT1("Should not be called!!\n");
+         GDIOBJ_FreeObjByHandle(Dce->hrgnClip, GDI_OBJECT_TYPE_REGION|GDI_OBJECT_TYPE_SILENT);
+         Dce->hrgnClip = NULL;
+      }
       Dce->hrgnClip = ClipRegion;
    }
 
-   DceSetDrawable(Window, Dce->hDC, Flags, UpdateClipOrigin);
+   DceSetDrawable(Wnd, Dce->hDC, Flags, UpdateClipOrigin);
 
-   DceUpdateVisRgn(Dce, Window, Flags);
+   DceUpdateVisRgn(Dce, Wnd, Flags);
 
    if (Dce->DCXFlags & DCX_CACHE)
    {
@@ -621,6 +619,8 @@ DceFreeDCE(PDCE pdce, BOOLEAN Force)
   pLE = pdce->List.Flink;
   ret = CONTAINING_RECORD(pLE, DCE, List);
 
+  pdce->DCXFlags |= DCX_INDESTROY;
+
   if (Force && !GDIOBJ_OwnedByCurrentProcess(pdce->hDC))
   {
      DPRINT("Change ownership for DCE! -> %x\n" , pdce);
@@ -635,12 +635,17 @@ DceFreeDCE(PDCE pdce, BOOLEAN Force)
          Hit = TRUE;
      }
   }
+  else
+  {
+     if (!GreGetObjectOwner(pdce->hDC, GDIObjType_DC_TYPE))
+        DC_SetOwnership( pdce->hDC, PsGetCurrentProcess());
+  }
 
   if (!Hit) IntGdiDeleteDC(pdce->hDC, TRUE);
 
-  if (pdce->hrgnClip && ! (pdce->DCXFlags & DCX_KEEPCLIPRGN))
+  if (pdce->hrgnClip && !(pdce->DCXFlags & DCX_KEEPCLIPRGN))
   {
-      REGION_FreeRgnByHandle(pdce->hrgnClip);
+      GDIOBJ_FreeObjByHandle(pdce->hrgnClip, GDI_OBJECT_TYPE_REGION|GDI_OBJECT_TYPE_SILENT);
       pdce->hrgnClip = NULL;
   }
 
@@ -666,14 +671,14 @@ DceFreeDCE(PDCE pdce, BOOLEAN Force)
  * Remove owned DCE and reset unreleased cache DCEs.
  */
 void FASTCALL
-DceFreeWindowDCE(PWINDOW_OBJECT Window)
+DceFreeWindowDCE(PWND Window)
 {
   PDCE pDCE;
   PLIST_ENTRY pLE;
 
   if (DCECount <= 0)
   {
-     DPRINT1("No Entry!\n");
+     DPRINT1("FreeWindowDCE No Entry! %d\n",DCECount);
      return;
   }
 
@@ -681,14 +686,22 @@ DceFreeWindowDCE(PWINDOW_OBJECT Window)
   pDCE = CONTAINING_RECORD(pLE, DCE, List);
   do
   {
-     if (!pDCE) break;
-     if (IsListEmpty(&pDCE->List)) break;
-     if ( pDCE->hwndCurrent == Window->hSelf &&
+     if (!pDCE)
+     {
+        DPRINT1("FreeWindowDCE No DCE Pointer!\n");
+        break;
+     }
+     if (IsListEmpty(&pDCE->List))
+     {
+        DPRINT1("FreeWindowDCE List is Empty!!!!\n");
+        break;
+     }
+     if ( pDCE->hwndCurrent == Window->head.h &&
           !(pDCE->DCXFlags & DCX_DCEEMPTY) )
      {
         if (!(pDCE->DCXFlags & DCX_CACHE)) /* owned or Class DCE*/
         {
-           if (Window->Wnd->pcls->style & CS_CLASSDC) /* Test Class first */
+           if (Window->pcls->style & CS_CLASSDC) /* Test Class first */
            {
               if (pDCE->DCXFlags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) /* Class DCE*/
                  DceDeleteClipRgn(pDCE);
@@ -700,10 +713,13 @@ DceFreeWindowDCE(PWINDOW_OBJECT Window)
 
               DPRINT("POWNED DCE going Cheap!! DCX_CACHE!! hDC-> %x \n", pDCE->hDC);
               if (!IntGdiSetDCOwnerEx( pDCE->hDC, GDI_OBJ_HMGR_NONE, FALSE))
-                  return;
+              {
+                  DPRINT1("Fail Owner Switch hDC-> %x \n", pDCE->hDC);
+                  break;
+              }
               /* Do not change owner so thread can clean up! */
            }
-           else if (Window->Wnd->pcls->style & CS_OWNDC) /* owned DCE*/
+           else if (Window->pcls->style & CS_OWNDC) /* owned DCE*/
            {
               pDCE = DceFreeDCE(pDCE, FALSE);
               if (!pDCE) break;
@@ -711,7 +727,8 @@ DceFreeWindowDCE(PWINDOW_OBJECT Window)
            }
            else
            {
-              ASSERT(FALSE);
+              DPRINT1("Not POWNED or CLASSDC hwndCurrent -> %x \n", pDCE->hwndCurrent);
+              //ASSERT(FALSE);
            }
         }
         else
@@ -725,7 +742,7 @@ DceFreeWindowDCE(PWINDOW_OBJECT Window)
                * We should change this to DPRINT when ReactOS is more stable
                * (for 1.0?).
                */
-              DPRINT1("[%p] GetDC() without ReleaseDC()!\n", Window->hSelf);
+              DPRINT1("[%p] GetDC() without ReleaseDC()!\n", Window->head.h);
               DceReleaseDC(pDCE, FALSE);
            }
            pDCE->DCXFlags |= DCX_DCEEMPTY;
@@ -805,11 +822,11 @@ DceEmptyCache(VOID)
 }
 
 VOID FASTCALL
-DceResetActiveDCEs(PWINDOW_OBJECT Window)
+DceResetActiveDCEs(PWND Window)
 {
    DCE *pDCE;
    PDC dc;
-   PWINDOW_OBJECT CurrentWindow;
+   PWND CurrentWindow;
    INT DeltaX;
    INT DeltaY;
    PLIST_ENTRY pLE;
@@ -825,9 +842,9 @@ DceResetActiveDCEs(PWINDOW_OBJECT Window)
    {
       if(!pDCE) break;
       if(pLE == &LEDce) break;
-      if (0 == (pDCE->DCXFlags & DCX_DCEEMPTY))
+      if (0 == (pDCE->DCXFlags & (DCX_DCEEMPTY|DCX_INDESTROY)))
       {
-         if (Window->hSelf == pDCE->hwndCurrent)
+         if (Window->head.h == pDCE->hwndCurrent)
          {
             CurrentWindow = Window;
          }
@@ -853,17 +870,17 @@ DceResetActiveDCEs(PWINDOW_OBJECT Window)
          {
             if (pDCE->DCXFlags & DCX_WINDOW)
             {
-               DeltaX = CurrentWindow->Wnd->rcWindow.left - dc->ptlDCOrig.x;
-               DeltaY = CurrentWindow->Wnd->rcWindow.top - dc->ptlDCOrig.y;
-               dc->ptlDCOrig.x = CurrentWindow->Wnd->rcWindow.left;
-               dc->ptlDCOrig.y = CurrentWindow->Wnd->rcWindow.top;
+               DeltaX = CurrentWindow->rcWindow.left - dc->ptlDCOrig.x;
+               DeltaY = CurrentWindow->rcWindow.top - dc->ptlDCOrig.y;
+               dc->ptlDCOrig.x = CurrentWindow->rcWindow.left;
+               dc->ptlDCOrig.y = CurrentWindow->rcWindow.top;
             }
             else
             {
-               DeltaX = CurrentWindow->Wnd->rcClient.left - dc->ptlDCOrig.x;
-               DeltaY = CurrentWindow->Wnd->rcClient.top - dc->ptlDCOrig.y;
-               dc->ptlDCOrig.x = CurrentWindow->Wnd->rcClient.left;
-               dc->ptlDCOrig.y = CurrentWindow->Wnd->rcClient.top;
+               DeltaX = CurrentWindow->rcClient.left - dc->ptlDCOrig.x;
+               DeltaY = CurrentWindow->rcClient.top - dc->ptlDCOrig.y;
+               dc->ptlDCOrig.x = CurrentWindow->rcClient.left;
+               dc->ptlDCOrig.y = CurrentWindow->rcClient.top;
             }
             if (NULL != dc->rosdc.hClipRgn)
             {
@@ -879,7 +896,7 @@ DceResetActiveDCEs(PWINDOW_OBJECT Window)
 
          DceUpdateVisRgn(pDCE, CurrentWindow, pDCE->DCXFlags);
 
-         if (Window->hSelf != pDCE->hwndCurrent)
+         if (Window->head.h != pDCE->hwndCurrent)
          {
 //            IntEngWindowChanged(CurrentWindow, WOC_RGN_CLIENT);
 //            UserDerefObject(CurrentWindow);
@@ -902,9 +919,12 @@ IntWindowFromDC(HDC hDc)
   Dce = CONTAINING_RECORD(pLE, DCE, List);
   do
   {
-      if(Dce->hDC == hDc)
+      if (Dce->hDC == hDc)
       {
-         Ret = Dce->hwndCurrent;
+         if (Dce->DCXFlags & DCX_INDESTROY)
+            Ret = NULL;
+         else
+            Ret = Dce->hwndCurrent;
          break;
       }
       pLE = Dce->List.Flink;
@@ -915,7 +935,7 @@ IntWindowFromDC(HDC hDc)
 }
 
 INT FASTCALL
-UserReleaseDC(PWINDOW_OBJECT Window, HDC hDc, BOOL EndPaint)
+UserReleaseDC(PWND Window, HDC hDc, BOOL EndPaint)
 {
   PDCE dce;
   PLIST_ENTRY pLE;
@@ -947,7 +967,7 @@ UserReleaseDC(PWINDOW_OBJECT Window, HDC hDc, BOOL EndPaint)
 }
 
 HDC FASTCALL
-UserGetWindowDC(PWINDOW_OBJECT Wnd)
+UserGetWindowDC(PWND Wnd)
 {
   return UserGetDCEx(Wnd, 0, DCX_USESTYLE | DCX_WINDOW);
 }
@@ -956,7 +976,7 @@ HWND FASTCALL
 UserGethWnd( HDC hdc, PWNDOBJ *pwndo)
 {
   PWNDGDI pWndgdi;
-  PWINDOW_OBJECT Wnd;
+  PWND Wnd;
   HWND hWnd;
 
   hWnd = IntWindowFromDC(hdc);
@@ -976,7 +996,7 @@ UserGethWnd( HDC hdc, PWNDOBJ *pwndo)
 HDC APIENTRY
 NtUserGetDCEx(HWND hWnd OPTIONAL, HANDLE ClipRegion, ULONG Flags)
 {
-  PWINDOW_OBJECT Wnd=NULL;
+  PWND Wnd=NULL;
   DECLARE_RETURN(HDC);
 
   DPRINT("Enter NtUserGetDCEx\n");