[WIN32K] Tag more pool allocations
[reactos.git] / reactos / subsystems / win32 / win32k / ntuser / monitor.c
index 1d89da5..d13c438 100644 (file)
@@ -12,9 +12,9 @@
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  *  COPYRIGHT:        See COPYING in the top level directory
  *  PROJECT:          ReactOS kernel
@@ -27,7 +27,7 @@
 
 /* INCLUDES ******************************************************************/
 
-#include <w32k.h>
+#include <win32k.h>
 
 /* FIXME: find include file for these */
 #define MONITORINFOF_PRIMARY      1
@@ -41,7 +41,7 @@
 /* GLOBALS *******************************************************************/
 
 /* list of monitors */
-static PMONITOR_OBJECT gMonitorList = NULL;
+static PMONITOR gMonitorList = NULL;
 
 /* INITALIZATION FUNCTIONS ****************************************************/
 
@@ -76,26 +76,25 @@ CleanupMonitorImpl()
 
 /* IntCreateMonitorObject
  *
- * Creates a MONITOR_OBJECT
+ * Creates a MONITOR
  *
  * Return value
- *   If the function succeeds a pointer to a MONITOR_OBJECT is returned. On failure
+ *   If the function succeeds a pointer to a MONITOR is returned. On failure
  *   NULL is returned.
  */
 static
-PMONITOR_OBJECT
+PMONITOR
 IntCreateMonitorObject()
 {
    HANDLE Handle;
-   PMONITOR_OBJECT Monitor;
+   PMONITOR Monitor;
 
-   Monitor = ObmCreateObject(gHandleTable, &Handle, otMonitor, sizeof (MONITOR_OBJECT));
+   Monitor = UserCreateObject(gHandleTable, NULL, &Handle, otMonitor, sizeof (MONITOR));
    if (Monitor == NULL)
    {
       return NULL;
    }
 
-   Monitor->Handle = Handle;
    ExInitializeFastMutex(&Monitor->Lock);
 
    return Monitor;
@@ -103,44 +102,43 @@ IntCreateMonitorObject()
 
 /* IntDestroyMonitorObject
  *
- * Destroys a MONITOR_OBJECT
+ * Destroys a MONITOR
  * You have to be the owner of the monitors lock to safely destroy it.
  *
  * Arguments
  *
  *   pMonitor
- *      Pointer to the MONITOR_OBJECT which shall be deleted
+ *      Pointer to the MONITOR which shall be deleted
  */
 static
 void
-IntDestroyMonitorObject(IN PMONITOR_OBJECT pMonitor)
+IntDestroyMonitorObject(IN PMONITOR pMonitor)
 {
    RtlFreeUnicodeString(&pMonitor->DeviceName);
-   ObmDereferenceObject(pMonitor);
+   UserDereferenceObject(pMonitor);
 }
 
 
-static
-PMONITOR_OBJECT FASTCALL
+PMONITOR FASTCALL
 UserGetMonitorObject(IN HMONITOR hMonitor)
 {
-   PMONITOR_OBJECT Monitor;
-   
+   PMONITOR Monitor;
+
    if (!hMonitor)
    {
       SetLastWin32Error(ERROR_INVALID_MONITOR_HANDLE);
       return NULL;
    }
-   
-   
-   Monitor = (PMONITOR_OBJECT)UserGetObject(gHandleTable, hMonitor, otMonitor);
+
+
+   Monitor = (PMONITOR)UserGetObject(gHandleTable, hMonitor, otMonitor);
    if (!Monitor)
    {
       SetLastWin32Error(ERROR_INVALID_MONITOR_HANDLE);
       return NULL;
    }
 
-   ASSERT(USER_BODY_TO_HEADER(Monitor)->RefCount >= 0);
+   ASSERT(Monitor->head.cLockObj >= 0);
 
    return Monitor;
 }
@@ -148,21 +146,21 @@ UserGetMonitorObject(IN HMONITOR hMonitor)
 
 /* IntAttachMonitor
  *
- * Creates a new MONITOR_OBJECT and appends it to the list of monitors.
+ * Creates a new MONITOR and appends it to the list of monitors.
  *
  * Arguments
  *
- *   pGdiDevice     Pointer to the GDIDEVICE onto which the monitor was attached
+ *   pGdiDevice     Pointer to the PDEVOBJ onto which the monitor was attached
  *   DisplayNumber  Display Number (starting with 0)
  *
  * Return value
  *   Returns a NTSTATUS
  */
 NTSTATUS
-IntAttachMonitor(IN GDIDEVICE *pGdiDevice,
+IntAttachMonitor(IN PDEVOBJ *pGdiDevice,
                  IN ULONG DisplayNumber)
 {
-   PMONITOR_OBJECT Monitor;
+   PMONITOR Monitor;
    WCHAR Buffer[CCHDEVICENAME];
 
    DPRINT("Attaching monitor...\n");
@@ -175,14 +173,27 @@ IntAttachMonitor(IN GDIDEVICE *pGdiDevice,
       return STATUS_INSUFFICIENT_RESOURCES;
    }
 
-   _snwprintf(Buffer, CCHDEVICENAME, L"\\??\\DISPLAY%d", DisplayNumber + 1);
+   _snwprintf(Buffer, CCHDEVICENAME, L"\\\\.\\DISPLAY%d", DisplayNumber + 1);
    if (!RtlCreateUnicodeString(&Monitor->DeviceName, Buffer))
    {
       DPRINT("Couldn't duplicate monitor name!\n");
+      UserDereferenceObject(Monitor);
+      UserDeleteObject(UserHMGetHandle(Monitor), otMonitor);
       return STATUS_INSUFFICIENT_RESOURCES;
    }
 
    Monitor->GdiDevice = pGdiDevice;
+   Monitor->rcMonitor.left  = 0;
+   Monitor->rcMonitor.top   = 0;   
+   Monitor->rcMonitor.right  = Monitor->rcMonitor.left + pGdiDevice->gdiinfo.ulHorzRes; 
+   Monitor->rcMonitor.bottom = Monitor->rcMonitor.top + pGdiDevice->gdiinfo.ulVertRes;
+   Monitor->rcWork = Monitor->rcMonitor;
+   Monitor->cWndStack = 0;
+
+   Monitor->hrgnMonitor = IntSysCreateRectRgnIndirect( &Monitor->rcMonitor );
+
+   IntGdiSetRegionOwner(Monitor->hrgnMonitor, GDI_OBJ_HMGR_PUBLIC);
+
    if (gMonitorList == NULL)
    {
       DPRINT("Primary monitor is beeing attached\n");
@@ -191,10 +202,9 @@ IntAttachMonitor(IN GDIDEVICE *pGdiDevice,
    }
    else
    {
-      PMONITOR_OBJECT p;
+      PMONITOR p;
       DPRINT("Additional monitor is beeing attached\n");
       for (p = gMonitorList; p->Next != NULL; p = p->Next)
-         ;
       {
          p->Next = Monitor;
       }
@@ -206,19 +216,19 @@ IntAttachMonitor(IN GDIDEVICE *pGdiDevice,
 
 /* IntDetachMonitor
  *
- * Deletes a MONITOR_OBJECT and removes it from the list of monitors.
+ * Deletes a MONITOR and removes it from the list of monitors.
  *
  * Arguments
  *
- *   pGdiDevice  Pointer to the GDIDEVICE from which the monitor was detached
+ *   pGdiDevice  Pointer to the PDEVOBJ from which the monitor was detached
  *
  * Return value
  *   Returns a NTSTATUS
  */
 NTSTATUS
-IntDetachMonitor(IN GDIDEVICE *pGdiDevice)
+IntDetachMonitor(IN PDEVOBJ *pGdiDevice)
 {
-   PMONITOR_OBJECT Monitor;
+   PMONITOR Monitor;
 
    for (Monitor = gMonitorList; Monitor != NULL; Monitor = Monitor->Next)
    {
@@ -234,7 +244,7 @@ IntDetachMonitor(IN GDIDEVICE *pGdiDevice)
 
    if (Monitor->IsPrimary && (Monitor->Next != NULL || Monitor->Prev != NULL))
    {
-      PMONITOR_OBJECT NewPrimaryMonitor = (Monitor->Prev != NULL) ? (Monitor->Prev) : (Monitor->Next);
+      PMONITOR NewPrimaryMonitor = (Monitor->Prev != NULL) ? (Monitor->Prev) : (Monitor->Next);
 
       ExEnterCriticalRegionAndAcquireFastMutexUnsafe(&NewPrimaryMonitor->Lock);
       NewPrimaryMonitor->IsPrimary = TRUE;
@@ -254,6 +264,9 @@ IntDetachMonitor(IN GDIDEVICE *pGdiDevice)
          Monitor->Next->Prev = Monitor->Prev;
    }
 
+   if (Monitor->hrgnMonitor)
+      REGION_FreeRgnByHandle(Monitor->hrgnMonitor);
+
    IntDestroyMonitorObject(Monitor);
 
    return STATUS_SUCCESS;
@@ -261,16 +274,16 @@ IntDetachMonitor(IN GDIDEVICE *pGdiDevice)
 
 /* IntGetPrimaryMonitor
  *
- * Returns a PMONITOR_OBJECT for the primary monitor
+ * Returns a PMONITOR for the primary monitor
  *
  * Return value
- *   PMONITOR_OBJECT
+ *   PMONITOR
  */
-static
-PMONITOR_OBJECT
+PMONITOR
+FASTCALL
 IntGetPrimaryMonitor()
 {
-   PMONITOR_OBJECT Monitor;
+   PMONITOR Monitor;
 
    for (Monitor = gMonitorList; Monitor != NULL; Monitor = Monitor->Next)
    {
@@ -315,31 +328,33 @@ IntGetPrimaryMonitor()
  */
 static
 UINT
-IntGetMonitorsFromRect(OPTIONAL IN LPCRECT pRect,
+IntGetMonitorsFromRect(OPTIONAL IN LPCRECTL pRect,
                        OPTIONAL OUT HMONITOR *hMonitorList,
-                       OPTIONAL OUT LPRECT monitorRectList,
+                       OPTIONAL OUT PRECTL monitorRectList,
                        OPTIONAL IN DWORD listSize,
                        OPTIONAL IN DWORD flags)
 {
-   PMONITOR_OBJECT Monitor, NearestMonitor = NULL;
+   PMONITOR Monitor, NearestMonitor = NULL, PrimaryMonitor = NULL;
    UINT iCount = 0;
    LONG iNearestDistanceX = 0x7fffffff, iNearestDistanceY = 0x7fffffff;
 
    /* find monitors which intersect the rectangle */
    for (Monitor = gMonitorList; Monitor != NULL; Monitor = Monitor->Next)
    {
-      RECT MonitorRect, IntersectionRect;
+      RECTL MonitorRect, IntersectionRect;
 
       ExEnterCriticalRegionAndAcquireFastMutexUnsafe(&Monitor->Lock);
-      MonitorRect.left = 0; /* FIXME: get origin */
-      MonitorRect.top = 0; /* FIXME: get origin */
-      MonitorRect.right = MonitorRect.left + Monitor->GdiDevice->GDIInfo.ulHorzRes;
-      MonitorRect.bottom = MonitorRect.top + Monitor->GdiDevice->GDIInfo.ulVertRes;
+      MonitorRect = Monitor->rcMonitor;
       ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(&Monitor->Lock);
 
       DPRINT("MonitorRect: left = %d, top = %d, right = %d, bottom = %d\n",
              MonitorRect.left, MonitorRect.top, MonitorRect.right, MonitorRect.bottom);
 
+      if (flags == MONITOR_DEFAULTTOPRIMARY && Monitor->IsPrimary)
+      {
+         PrimaryMonitor = Monitor;
+      }
+
       if (pRect != NULL)
       {
          BOOL intersects = TRUE;
@@ -386,7 +401,7 @@ IntGetMonitorsFromRect(OPTIONAL IN LPCRECT pRect,
       if (iCount < listSize)
       {
          if (hMonitorList != NULL)
-            hMonitorList[iCount] = Monitor->Handle;
+            hMonitorList[iCount] = UserHMGetHandle(Monitor);
          if (monitorRectList != NULL)
             monitorRectList[iCount] = IntersectionRect;
       }
@@ -398,11 +413,19 @@ IntGetMonitorsFromRect(OPTIONAL IN LPCRECT pRect,
       if (iCount < listSize)
       {
          if (hMonitorList != NULL)
-            hMonitorList[iCount] = NearestMonitor->Handle;
+            hMonitorList[iCount] = UserHMGetHandle(NearestMonitor);
+      }
+      iCount++;
+   }
+   else if (iCount == 0 && flags == MONITOR_DEFAULTTOPRIMARY)
+   {
+      if (iCount < listSize)
+      {
+         if (hMonitorList != NULL)
+            hMonitorList[iCount] = UserHMGetHandle(PrimaryMonitor);
       }
       iCount++;
    }
-
    return iCount;
 }
 
@@ -439,19 +462,19 @@ IntGetMonitorsFromRect(OPTIONAL IN LPCRECT pRect,
  *   The number of monitors which intersect the specified region or -1 on failure.
  */
 INT
-STDCALL
+APIENTRY
 NtUserEnumDisplayMonitors(
    OPTIONAL IN HDC hDC,
-   OPTIONAL IN LPCRECT pRect,
+   OPTIONAL IN LPCRECTL pRect,
    OPTIONAL OUT HMONITOR *hMonitorList,
-   OPTIONAL OUT LPRECT monitorRectList,
+   OPTIONAL OUT PRECTL monitorRectList,
    OPTIONAL IN DWORD listSize)
 {
    INT numMonitors, i;
    HMONITOR *safeHMonitorList = NULL;
-   LPRECT safeRectList = NULL;
-   RECT rect, *myRect;
-   RECT dcRect;
+   PRECTL safeRectList = NULL;
+   RECTL rect, *myRect;
+   RECTL dcRect;
    NTSTATUS status;
 
    /* get rect */
@@ -480,7 +503,7 @@ NtUserEnumDisplayMonitors(
          /* FIXME: setlasterror? */
          return -1;
       }
-      dcVisRgn = dc->w.hVisRgn;
+      dcVisRgn = dc->rosdc.hVisRgn;
       DC_UnlockDc(dc);
 
       regionType = NtGdiGetRgnBox(dcVisRgn, &dcRect);
@@ -537,7 +560,7 @@ NtUserEnumDisplayMonitors(
    }
    if (monitorRectList != NULL && listSize != 0)
    {
-      safeRectList = ExAllocatePool(PagedPool, sizeof (RECT) * listSize);
+      safeRectList = ExAllocatePoolWithTag(PagedPool, sizeof (RECT) * listSize, USERTAG_MONITORRECTS);
       if (safeRectList == NULL)
       {
          ExFreePool(safeHMonitorList);
@@ -610,19 +633,19 @@ NtUserEnumDisplayMonitors(
  *
  */
 BOOL
-STDCALL
+APIENTRY
 NtUserGetMonitorInfo(
    IN HMONITOR hMonitor,
    OUT LPMONITORINFO pMonitorInfo)
 {
-   PMONITOR_OBJECT Monitor;
+   PMONITOR Monitor;
    MONITORINFOEXW MonitorInfo;
    NTSTATUS Status;
    DECLARE_RETURN(BOOL);
-   
+
    DPRINT("Enter NtUserGetMonitorInfo\n");
    UserEnterShared();
-   
+
    /* get monitor object */
    if (!(Monitor = UserGetMonitorObject(hMonitor)))
    {
@@ -651,11 +674,8 @@ NtUserGetMonitorInfo(
    }
 
    /* fill monitor info */
-   MonitorInfo.rcMonitor.left = 0; /* FIXME: get origin */
-   MonitorInfo.rcMonitor.top = 0; /* FIXME: get origin */
-   MonitorInfo.rcMonitor.right = MonitorInfo.rcMonitor.left + Monitor->GdiDevice->GDIInfo.ulHorzRes;
-   MonitorInfo.rcMonitor.bottom = MonitorInfo.rcMonitor.top + Monitor->GdiDevice->GDIInfo.ulVertRes;
-   MonitorInfo.rcWork = MonitorInfo.rcMonitor; /* FIXME: use DEVMODE panning to calculate work area? */
+   MonitorInfo.rcMonitor = Monitor->rcMonitor;
+   MonitorInfo.rcWork = Monitor->rcWork;
    MonitorInfo.dwFlags = 0;
 
    if (Monitor->IsPrimary)
@@ -685,7 +705,7 @@ NtUserGetMonitorInfo(
    DPRINT("GetMonitorInfo: success\n");
 
    RETURN(TRUE);
-   
+
 CLEANUP:
    DPRINT("Leave NtUserGetMonitorInfo, ret=%i\n",_ret_);
    UserLeave();
@@ -709,13 +729,13 @@ CLEANUP:
  *   return value depends on dwFlags
  */
 HMONITOR
-STDCALL
+APIENTRY
 NtUserMonitorFromPoint(
    IN POINT point,
    IN DWORD dwFlags)
 {
    INT NumMonitors;
-   RECT InRect;
+   RECTL InRect;
    HMONITOR hMonitor = NULL;
 
    /* fill inRect */
@@ -733,9 +753,9 @@ NtUserMonitorFromPoint(
    {
       if (dwFlags == MONITOR_DEFAULTTOPRIMARY)
       {
-         PMONITOR_OBJECT MonitorObj = IntGetPrimaryMonitor();
+         PMONITOR MonitorObj = IntGetPrimaryMonitor();
          if (MonitorObj)
-            hMonitor = MonitorObj->Handle;
+            hMonitor = UserHMGetHandle(MonitorObj);
       }
       else if (dwFlags == MONITOR_DEFAULTTONEAREST)
       {
@@ -767,18 +787,18 @@ NtUserMonitorFromPoint(
  *   return value depends on dwFlags
  */
 HMONITOR
-STDCALL
+APIENTRY
 NtUserMonitorFromRect(
-   IN LPCRECT pRect,
+   IN LPCRECTL pRect,
    IN DWORD dwFlags)
 {
    INT numMonitors, iLargestArea = -1, i;
-   LPRECT rectList;
+   PRECTL rectList;
    HMONITOR *hMonitorList;
    HMONITOR hMonitor = NULL;
-   RECT rect;
+   RECTL rect;
    NTSTATUS status;
-   
+
    /* get rect */
    status = MmCopyFromCaller(&rect, pRect, sizeof (RECT));
    if (!NT_SUCCESS(status))
@@ -798,9 +818,9 @@ NtUserMonitorFromRect(
    {
       if (dwFlags == MONITOR_DEFAULTTOPRIMARY)
       {
-         PMONITOR_OBJECT monitorObj = IntGetPrimaryMonitor();
+         PMONITOR monitorObj = IntGetPrimaryMonitor();
          if (monitorObj)
-            return monitorObj->Handle;
+            return UserHMGetHandle(monitorObj);
       }
       else if (dwFlags == MONITOR_DEFAULTTONEAREST)
       {
@@ -825,7 +845,7 @@ NtUserMonitorFromRect(
       /* FIXME: SetLastWin32Error? */
       return (HMONITOR)NULL;
    }
-   rectList = ExAllocatePool(PagedPool, sizeof (RECT) * numMonitors);
+   rectList = ExAllocatePoolWithTag(PagedPool, sizeof (RECT) * numMonitors, USERTAG_MONITORRECTS);
    if (rectList == NULL)
    {
       ExFreePool(hMonitorList);
@@ -862,33 +882,41 @@ NtUserMonitorFromRect(
 
 
 HMONITOR
-STDCALL
+APIENTRY
 NtUserMonitorFromWindow(
    IN HWND hWnd,
    IN DWORD dwFlags)
 {
    PWINDOW_OBJECT Window;
    HMONITOR hMonitor = NULL;
-   RECT Rect;
+   RECTL Rect;
    DECLARE_RETURN(HMONITOR);
-   
+
    DPRINT("Enter NtUserMonitorFromWindow\n");
    UserEnterShared();
-   
+
    if (!(Window = UserGetWindowObject(hWnd)))
    {
-      RETURN(NULL);
+      if (dwFlags == MONITOR_DEFAULTTONULL)
+      {
+         RETURN(hMonitor);
+      }
+      IntGetMonitorsFromRect(NULL, &hMonitor, NULL, 1, dwFlags);
+      RETURN(hMonitor);
    }
 
-   Rect.left = Rect.right = Window->WindowRect.left;
-   Rect.top = Rect.bottom = Window->WindowRect.bottom;
+   if (!Window->Wnd)
+      RETURN(hMonitor);
+
+   Rect.left = Rect.right = Window->Wnd->rcWindow.left;
+   Rect.top = Rect.bottom = Window->Wnd->rcWindow.bottom;
 
    IntGetMonitorsFromRect(&Rect, &hMonitor, NULL, 1, dwFlags);
 
    RETURN(hMonitor);
-   
+
 CLEANUP:
    DPRINT("Leave NtUserMonitorFromWindow, ret=%i\n",_ret_);
    UserLeave();
-   END_CLEANUP;   
+   END_CLEANUP;
 }