Fix a typo.
[reactos.git] / reactos / subsys / win32k / objects / dc.c
index e436def..e44b5df 100644 (file)
@@ -139,7 +139,7 @@ NtGdiCancelDC(HDC  hDC)
 }
 
 HDC STDCALL
-NtGdiCreateCompatableDC(HDC hDC)
+NtGdiCreateCompatibleDC(HDC hDC)
 {
   PDC  NewDC, OrigDC;
   HBITMAP  hBitmap;
@@ -661,18 +661,79 @@ IntPrepareDriverIfNeeded()
    return (PrimarySurface.PreparedDriver ? TRUE : IntPrepareDriver());
 }
 
+static BOOL FASTCALL
+PrepareVideoPrt()
+{
+   PIRP Irp;
+   NTSTATUS Status;
+   IO_STATUS_BLOCK Iosb;
+   BOOL Prepare = TRUE;
+   ULONG Length = sizeof(BOOL);
+   PIO_STACK_LOCATION StackPtr;
+   LARGE_INTEGER StartOffset;
+   PFILE_OBJECT FileObject = PrimarySurface.VideoFileObject;
+   PDEVICE_OBJECT DeviceObject = FileObject->DeviceObject;
+
+   DPRINT("PrepareVideoPrt() called\n");
+
+   KeClearEvent(&PrimarySurface.VideoFileObject->Event);
+
+   ObReferenceObjectByPointer(FileObject, 0, IoFileObjectType, KernelMode);
+
+   StartOffset.QuadPart = 0;
+   Irp = IoBuildSynchronousFsdRequest(IRP_MJ_WRITE,
+                                      DeviceObject,
+                                      (PVOID) &Prepare,
+                                      Length,
+                                      &StartOffset,
+                                      NULL,
+                                      &Iosb);
+   if (NULL == Irp)
+   {
+      return FALSE;
+   }
+
+   /* Set up IRP Data */
+   Irp->Tail.Overlay.OriginalFileObject = FileObject;
+   Irp->RequestorMode = KernelMode;
+   Irp->Overlay.AsynchronousParameters.UserApcRoutine = NULL;
+   Irp->Overlay.AsynchronousParameters.UserApcContext = NULL;
+   Irp->Flags |= IRP_WRITE_OPERATION;
+
+   /* Setup Stack Data */
+   StackPtr = IoGetNextIrpStackLocation(Irp);
+   StackPtr->FileObject = PrimarySurface.VideoFileObject;
+   StackPtr->Parameters.Write.Key = 0;
+
+   Status = IoCallDriver(DeviceObject, Irp);
+
+   if (STATUS_PENDING == Status)
+   {
+      KeWaitForSingleObject(&FileObject->Event, Executive, KernelMode, TRUE, 0);
+      Status = Iosb.Status;
+   }
+
+   return NT_SUCCESS(Status);
+}
+
 BOOL FASTCALL
 IntCreatePrimarySurface()
 {
    SIZEL SurfSize;
    RECTL SurfaceRect;
    SURFOBJ *SurfObj;
-
+   BOOL calledFromUser;
+   
    if (! IntPrepareDriverIfNeeded())
    {
       return FALSE;
    }
 
+   if (! PrepareVideoPrt())
+   {
+      return FALSE;
+   }
+
    DPRINT("calling EnableSurface\n");
    /* Enable the drawing surface */
    PrimarySurface.Handle =
@@ -688,6 +749,11 @@ IntCreatePrimarySurface()
 
    PrimarySurface.DriverFunctions.AssertMode(PrimarySurface.PDev, TRUE);
 
+   calledFromUser = UserIsEntered(); //fixme: possibly upgrade a shared lock
+   if (!calledFromUser){
+      UserEnterExclusive();
+   }
+
    /* attach monitor */
    IntAttachMonitor(&PrimarySurface, PrimarySurface.DisplayNumber);
 
@@ -705,7 +771,11 @@ IntCreatePrimarySurface()
    GDIDEV(SurfObj)->Pointer.Pos.y = (SurfaceRect.bottom - SurfaceRect.top) / 2;
 
    EngUnlockSurface(SurfObj);
-   IntShowDesktop(IntGetActiveDesktop(), SurfSize.cx, SurfSize.cy);
+   co_IntShowDesktop(IntGetActiveDesktop(), SurfSize.cx, SurfSize.cy);
+
+   if (!calledFromUser){
+      UserLeave();
+   }
 
    return TRUE;
 }
@@ -713,11 +783,22 @@ IntCreatePrimarySurface()
 VOID FASTCALL
 IntDestroyPrimarySurface()
   {
+    BOOL calledFromUser; 
+     
     DRIVER_UnreferenceDriver(L"DISPLAY");
 
+    calledFromUser = UserIsEntered();
+    if (!calledFromUser){
+       UserEnterExclusive();
+    }
+
     /* detach monitor */
     IntDetachMonitor(&PrimarySurface);
 
+    if (!calledFromUser){
+       UserLeave();
+    }
+
     /*
      * FIXME: Hide a mouse pointer there. Also because we have to prevent
      * memory leaks with the Eng* mouse routines.
@@ -746,7 +827,8 @@ IntGdiCreateDC(PUNICODE_STRING Driver,
   HDC      hDC = NULL;
   HRGN     hVisRgn;
   UNICODE_STRING StdDriver;
-
+  BOOL calledFromUser;
+  
   RtlInitUnicodeString(&StdDriver, L"DISPLAY");
 
   if (NULL == Driver || 0 == RtlCompareUnicodeString(Driver, &StdDriver, TRUE))
@@ -759,10 +841,26 @@ IntGdiCreateDC(PUNICODE_STRING Driver,
               return NULL;
             }
         }
-      else if (! IntGraphicsCheck(TRUE))
+      else
         {
-          DPRINT1("Unable to initialize graphics, returning NULL dc\n");
-          return NULL;
+          calledFromUser = UserIsEntered();
+          if (!calledFromUser){
+             UserEnterExclusive();
+          }
+          
+          if (! co_IntGraphicsCheck(TRUE))
+          {
+            if (!calledFromUser){
+               UserLeave();
+            } 
+            DPRINT1("Unable to initialize graphics, returning NULL dc\n");
+            return NULL;
+          }
+          
+          if (!calledFromUser){
+            UserLeave();
+          } 
+          
         }
     }
 
@@ -770,7 +868,7 @@ IntGdiCreateDC(PUNICODE_STRING Driver,
   if ((hNewDC = DC_FindOpenDC(Driver)) != NULL)
   {
     hDC = hNewDC;
-    return  NtGdiCreateCompatableDC(hDC);
+    return  NtGdiCreateCompatibleDC(hDC);
   }
 
   if (Driver != NULL && Driver->Buffer != NULL)
@@ -889,7 +987,9 @@ NtGdiCreateDC(PUNICODE_STRING Driver,
     }
   }
 
-  Ret = IntGdiCreateDC(&SafeDriver, &SafeDevice, NULL, &SafeInitData, FALSE);
+  Ret = IntGdiCreateDC(NULL == Driver ? NULL : &SafeDriver,
+                       NULL == Device ? NULL : &SafeDevice, NULL,
+                       NULL == InitData ? NULL : &SafeInitData, FALSE);
 
   return Ret;
 }
@@ -962,10 +1062,17 @@ NtGdiDeleteDC(HDC  DCHandle)
 {
   PDC  DCToDelete;
 
+  if (!GDIOBJ_OwnedByCurrentProcess(DCHandle))
+    {
+      SetLastWin32Error(ERROR_INVALID_HANDLE);
+      return FALSE;
+    }
+  
   DCToDelete = DC_LockDc(DCHandle);
   if (DCToDelete == NULL)
     {
-      return  FALSE;
+      SetLastWin32Error(ERROR_INVALID_HANDLE);
+      return FALSE;
     }
 
   /*  First delete all saved DCs  */
@@ -1396,7 +1503,7 @@ NtGdiSetDCState ( HDC hDC, HDC hDCSave )
 INT FASTCALL
 IntGdiGetDeviceCaps(PDC dc, INT Index)
 {
-  INT ret;
+  INT ret = 0;
   POINT  pt;
 
   /* Retrieve capability */