1 /*
2  * PROJECT:     ReactOS Spooler Router
3  * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE:     Functions related to Print Monitors
5  * COPYRIGHT:   Copyright 2015-2017 Colin Finck (colin@reactos.org)
6  */
7 
8 #include "precomp.h"
9 
10 BOOL WINAPI
11 EnumMonitorsW(PWSTR pName, DWORD Level, PBYTE pMonitors, DWORD cbBuf, PDWORD pcbNeeded, PDWORD pcReturned)
12 {
13     BOOL bReturnValue = TRUE;
14     DWORD cbCallBuffer;
15     DWORD cbNeeded;
16     DWORD dwReturned;
17     PBYTE pCallBuffer;
18     PSPOOLSS_PRINT_PROVIDER pPrintProvider;
19     PLIST_ENTRY pEntry;
20 
21     // Sanity checks.
22     if (cbBuf && !pMonitors)
23     {
24         SetLastError(ERROR_INVALID_USER_BUFFER);
25         return FALSE;
26     }
27 
28     // Begin counting.
29     *pcbNeeded = 0;
30     *pcReturned = 0;
31 
32     // At the beginning, we have the full buffer available.
33     cbCallBuffer = cbBuf;
34     pCallBuffer = pMonitors;
35 
36     // Loop through all Print Provider.
37     for (pEntry = PrintProviderList.Flink; pEntry != &PrintProviderList; pEntry = pEntry->Flink)
38     {
39         pPrintProvider = CONTAINING_RECORD(pEntry, SPOOLSS_PRINT_PROVIDER, Entry);
40 
41         // Check if this Print Provider provides an EnumMonitors function.
42         if (!pPrintProvider->PrintProvider.fpEnumMonitors)
43             continue;
44 
45         // Call the EnumMonitors function of this Print Provider.
46         cbNeeded = 0;
47         dwReturned = 0;
48         bReturnValue = pPrintProvider->PrintProvider.fpEnumMonitors(pName, Level, pCallBuffer, cbCallBuffer, &cbNeeded, &dwReturned);
49 
50         // Add the returned counts to the total values.
51         *pcbNeeded += cbNeeded;
52         *pcReturned += dwReturned;
53 
54         // Reduce the available buffer size for the next call without risking an underflow.
55         if (cbNeeded < cbCallBuffer)
56             cbCallBuffer -= cbNeeded;
57         else
58             cbCallBuffer = 0;
59 
60         // Advance the buffer if the caller provided it.
61         if (pCallBuffer)
62             pCallBuffer += cbNeeded;
63 
64         // Check if we shall not ask other Print Providers.
65         if (bReturnValue == ROUTER_STOP_ROUTING)
66             break;
67     }
68 
69     return bReturnValue;
70 }
71