[21305] | 1 |
|
---|
| 2 | /*
|
---|
| 3 | * Winspool support code
|
---|
| 4 | *
|
---|
| 5 | * Copyright 2000 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
| 6 | *
|
---|
| 7 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
| 8 | *
|
---|
| 9 | */
|
---|
| 10 | #define INCL_SPL
|
---|
| 11 | #define INCL_SPLERRORS
|
---|
| 12 | #define INCL_SPLDOSPRINT
|
---|
| 13 | #define INCL_DOSERRORS
|
---|
| 14 | #define INCL_GPI
|
---|
| 15 | #define INCL_WIN
|
---|
| 16 | #define INCL_DEV
|
---|
| 17 |
|
---|
| 18 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
---|
| 19 | #include <stdlib.h>
|
---|
| 20 | #include <pmspl.h>
|
---|
| 21 | #include <string.h>
|
---|
| 22 | #include <win32type.h>
|
---|
| 23 | #include <winuser32.h>
|
---|
| 24 | #include <misc.h>
|
---|
| 25 | #include <wprocess.h>
|
---|
| 26 | #include "oslibspl.h"
|
---|
| 27 |
|
---|
| 28 | /**
|
---|
| 29 | * Enumerate printers.
|
---|
| 30 | * @returns Pointer to requested data.
|
---|
| 31 | * @returns NULL on failure.
|
---|
| 32 | * @param pszComputer Computer name.
|
---|
| 33 | * @param flType Flags.
|
---|
| 34 | * @param pcReturned Number of structures returned.
|
---|
| 35 | * @param pcTotal Total number of structures available.
|
---|
| 36 | */
|
---|
| 37 | void * OSLibSplEnumPrinter(LPSTR pszComputer, DWORD flType, PDWORD pcReturned, PDWORD pcTotal)
|
---|
| 38 | {
|
---|
| 39 | ULONG cbNeeded = 0;
|
---|
| 40 | SPLERR rc = SplEnumPrinter(NULL, 0, flType, NULL, 0, pcReturned, pcTotal, &cbNeeded, NULL);
|
---|
| 41 | if (!rc || rc == ERROR_MORE_DATA || rc == NERR_BufTooSmall)
|
---|
| 42 | {
|
---|
| 43 | void *pv = malloc(cbNeeded+1);
|
---|
| 44 | rc = SplEnumPrinter(pszComputer, 0, flType, pv, cbNeeded, pcReturned, pcTotal, &cbNeeded, NULL);
|
---|
| 45 | if (!rc || rc == ERROR_MORE_DATA)
|
---|
| 46 | return pv;
|
---|
| 47 | free(pv);
|
---|
| 48 | }
|
---|
| 49 | return NULL;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | //******************************************************************************
|
---|
| 53 | //******************************************************************************
|
---|
| 54 | ULONG OSLibShowPrinterDialog(HWND hWnd, LPSTR printerName)
|
---|
| 55 | {
|
---|
| 56 | HAB hab;
|
---|
| 57 | ULONG cReturned, cTotal, cbNeeded, flType,cbBuf, ulrc;
|
---|
| 58 | int devNum = -1;
|
---|
| 59 | OSLIB_PRQINFO3 *queueInfo;
|
---|
| 60 | char *pszTmp, *pszDeviceName;
|
---|
| 61 | SPLERR rc;
|
---|
| 62 |
|
---|
| 63 | hab = WinQueryAnchorBlock(Win32ToOS2Handle(hWnd));
|
---|
| 64 |
|
---|
| 65 | rc = SplEnumQueue(NULL, 3, NULL, 0, &cReturned, &cTotal, &cbNeeded, NULL);
|
---|
| 66 | if (rc && rc != ERROR_MORE_DATA && rc != NERR_BufTooSmall)
|
---|
| 67 | {
|
---|
| 68 | dprintf(("OSLibSplEnumQueue failed"));
|
---|
| 69 | return -1;
|
---|
| 70 | }
|
---|
| 71 | if (cTotal == 0)
|
---|
| 72 | {//no printers installed
|
---|
| 73 | return -1;
|
---|
| 74 | }
|
---|
| 75 | queueInfo = (OSLIB_PRQINFO3*)malloc(cbNeeded);
|
---|
| 76 |
|
---|
| 77 | rc = SplEnumQueue(NULL, 3, queueInfo, cbNeeded, &cReturned, &cTotal, &cbNeeded, NULL);
|
---|
| 78 | if (rc)
|
---|
| 79 | {
|
---|
| 80 | free(queueInfo);
|
---|
| 81 | dprintf(("OSLibSplEnumQueue failed"));
|
---|
| 82 | return -1;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | for (int i = 0; i < cReturned; i++)
|
---|
| 86 | if (!strcmp(queueInfo[i].pszName, printerName))
|
---|
| 87 | {
|
---|
| 88 | devNum = i;
|
---|
| 89 | break;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | if (devNum == -1)
|
---|
| 93 | {
|
---|
| 94 | free(queueInfo);
|
---|
| 95 | return -1;
|
---|
| 96 | }
|
---|
| 97 | /* Use the first device name in the PRQINFO3 structure */
|
---|
| 98 | pszTmp = strchr(queueInfo[devNum].pszPrinters, ',');
|
---|
| 99 | if (pszTmp)
|
---|
| 100 | *pszTmp = '\0';
|
---|
| 101 |
|
---|
| 102 | /* Use just the driver name from the driver.device string */
|
---|
| 103 | pszDeviceName = strchr(queueInfo[devNum].pszDriverName, '.');
|
---|
| 104 | if (pszDeviceName)
|
---|
| 105 | {
|
---|
| 106 | *pszDeviceName = '\0';
|
---|
| 107 | pszDeviceName++;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /* Check size of buffer required for job properties */
|
---|
| 111 | cbBuf = DevPostDeviceModes( hab,
|
---|
| 112 | (PDRIVDATA)NULL,
|
---|
| 113 | queueInfo[devNum].pszDriverName,
|
---|
| 114 | pszDeviceName,
|
---|
| 115 | queueInfo[devNum].pszPrinters,
|
---|
| 116 | DPDM_POSTJOBPROP
|
---|
| 117 | );
|
---|
| 118 |
|
---|
| 119 | /* Return error to caller */
|
---|
| 120 | if (cbBuf<=0)
|
---|
| 121 | {
|
---|
| 122 | free(queueInfo);
|
---|
| 123 | return(-1);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /* Display job properties dialog & get updated job properties from driver */
|
---|
| 127 | ulrc = DevPostDeviceModes( hab,
|
---|
| 128 | (PDRIVDATA)queueInfo[devNum].pDriverData,
|
---|
| 129 | queueInfo[devNum].pszDriverName,
|
---|
| 130 | pszDeviceName,
|
---|
| 131 | queueInfo[devNum].pszPrinters,
|
---|
| 132 | DPDM_POSTJOBPROP);
|
---|
| 133 |
|
---|
| 134 | if (pszDeviceName)
|
---|
| 135 | *(--pszDeviceName) = '.';
|
---|
| 136 |
|
---|
| 137 | ulrc = SplSetQueue(NULL, printerName, 3, (PVOID)&queueInfo[devNum], cbBuf, 0);
|
---|
| 138 | if (ulrc)
|
---|
| 139 | dprintf(("Spooler Set Queue error %d",ulrc));
|
---|
| 140 |
|
---|
| 141 | free(queueInfo);
|
---|
| 142 | return(1);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | /**
|
---|
| 146 | * Enumerate queues.
|
---|
| 147 | * @returns Pointer to requested data.
|
---|
| 148 | * @returns NULL on failure.
|
---|
| 149 | * @param pszComputer Computer name.
|
---|
| 150 | * @param ulLevel Information level.
|
---|
| 151 | * @param pcReturned Number of structures returned.
|
---|
| 152 | * @param pcTotal Total number of structures available.
|
---|
| 153 | */
|
---|
| 154 | void * OSLibSplEnumQueue(LPSTR pszComputer, DWORD ulLevel, DWORD *pcReturned, DWORD *pcTotal)
|
---|
| 155 | {
|
---|
| 156 | ULONG cbNeeded = 0;
|
---|
| 157 | SPLERR rc = SplEnumQueue(pszComputer, ulLevel, NULL, 0, pcReturned, pcTotal, &cbNeeded, NULL);
|
---|
| 158 | if (!rc || rc == ERROR_MORE_DATA || rc == NERR_BufTooSmall)
|
---|
| 159 | {
|
---|
| 160 | void *pv = malloc(cbNeeded+1);
|
---|
| 161 | SPLERR rc = SplEnumQueue(pszComputer, ulLevel, pv, cbNeeded, pcReturned, pcTotal, &cbNeeded, NULL);
|
---|
| 162 | if (!rc || rc == ERROR_MORE_DATA)
|
---|
| 163 | return pv;
|
---|
| 164 | free(pv);
|
---|
| 165 | }
|
---|
| 166 | return NULL;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /**
|
---|
| 170 | * Enumerate devices.
|
---|
| 171 | * @returns Pointer to requested data.
|
---|
| 172 | * @returns NULL on failure.
|
---|
| 173 | * @param pszComputer Computer name.
|
---|
| 174 | * @param ulLevel Information level.
|
---|
| 175 | * @param pcReturned Number of structures returned.
|
---|
| 176 | * @param pcTotal Total number of structures available.
|
---|
| 177 | */
|
---|
| 178 | void * OSLibSplEnumDevice(LPSTR pszComputer, DWORD ulLevel, DWORD *pcReturned, DWORD *pcTotal)
|
---|
| 179 | {
|
---|
| 180 | ULONG cbNeeded = 0;
|
---|
| 181 | SPLERR rc = SplEnumDevice(pszComputer, ulLevel, NULL, 0, pcReturned, pcTotal, &cbNeeded, NULL);
|
---|
| 182 | if (!rc || rc == ERROR_MORE_DATA || rc == NERR_BufTooSmall)
|
---|
| 183 | {
|
---|
| 184 | void *pv = malloc(cbNeeded+1);
|
---|
| 185 | SPLERR rc = SplEnumDevice(pszComputer, ulLevel, pv, cbNeeded, pcReturned, pcTotal, &cbNeeded, NULL);
|
---|
| 186 | if (!rc || rc == ERROR_MORE_DATA)
|
---|
| 187 | return pv;
|
---|
| 188 | free(pv);
|
---|
| 189 | }
|
---|
| 190 | return NULL;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | //******************************************************************************
|
---|
| 194 | //******************************************************************************
|
---|
| 195 | /**
|
---|
| 196 | * @remark bird: This a nasty thing. Any chance we could use the app default flag for the queue?
|
---|
| 197 | */
|
---|
| 198 | BOOL OSLibSplFindDefaultPrinter(LPSTR lpszPortName, LPSTR lpszDriverName, LPSTR lpszQueueName)
|
---|
| 199 | {
|
---|
| 200 | /*
|
---|
| 201 | * OS/2 stores the default printer as a combination of printerdevice and queue.
|
---|
| 202 | *
|
---|
| 203 | * Printer Device is related to one port. It may have multiple printer
|
---|
| 204 | * drivers because the port may serve multiple queues with different drivers.
|
---|
| 205 | * The Ports are related to multiple queues.
|
---|
| 206 | *
|
---|
| 207 | * So we take the default default printer+queue combination and finds the
|
---|
| 208 | */
|
---|
| 209 | char szDefPrnDev[20];
|
---|
| 210 | char szDefPrnQue[20];
|
---|
| 211 | if (PrfQueryProfileString(HINI_PROFILE, "PM_SPOOLER", "PRINTER", "",
|
---|
| 212 | szDefPrnDev, sizeof(szDefPrnDev)) > 1
|
---|
| 213 | && PrfQueryProfileString(HINI_PROFILE, "PM_SPOOLER", "QUEUE", "",
|
---|
| 214 | szDefPrnQue, sizeof(szDefPrnQue)) > 1
|
---|
| 215 | && szDefPrnDev[0]
|
---|
| 216 | && szDefPrnQue[0]
|
---|
| 217 | )
|
---|
| 218 | {
|
---|
| 219 | char *psz;
|
---|
| 220 | /* remove everything beyond the first ';' */
|
---|
| 221 | if ((psz = strchr(szDefPrnDev, ';')) != NULL)
|
---|
| 222 | *psz = '\0';
|
---|
| 223 | if ((psz = strchr(szDefPrnQue, ';')) != NULL)
|
---|
| 224 | *psz = '\0';
|
---|
| 225 |
|
---|
| 226 | /*
|
---|
| 227 | * Now we must lookup the port name from the device settings.
|
---|
| 228 | * This is a string of this form:
|
---|
| 229 | * <port>;<driver1>[,<driver2>;<queue1>[,<queue2>];?;?;
|
---|
| 230 | */
|
---|
| 231 | ULONG cb = 0;
|
---|
| 232 | if (PrfQueryProfileSize(HINI_SYSTEMPROFILE, "PM_SPOOLER_PRINTER", szDefPrnDev, &cb)
|
---|
| 233 | && cb > 0)
|
---|
| 234 | {
|
---|
| 235 | char *pszBufD = (char*)malloc(cb + 1);
|
---|
| 236 | if (pszBufD
|
---|
| 237 | && PrfQueryProfileString(HINI_SYSTEMPROFILE, "PM_SPOOLER_PRINTER", szDefPrnDev,
|
---|
| 238 | NULL, pszBufD, cb + 1)
|
---|
| 239 | > 1
|
---|
| 240 | )
|
---|
| 241 | {
|
---|
| 242 | /*
|
---|
| 243 | * Now get the Default printer driver for the queue.
|
---|
| 244 | * This is stored as a ';' separated list of drivers, the first one is the default.
|
---|
| 245 | */
|
---|
| 246 | if (PrfQueryProfileSize(HINI_SYSTEMPROFILE, "PM_SPOOLER_QUEUE_DD", szDefPrnQue, &cb)
|
---|
| 247 | && cb > 0)
|
---|
| 248 | {
|
---|
| 249 | char *pszBufQ = (char*)malloc(cb + 1);
|
---|
| 250 | if (pszBufQ
|
---|
| 251 | && PrfQueryProfileString(HINI_SYSTEMPROFILE, "PM_SPOOLER_QUEUE_DD", szDefPrnQue,
|
---|
| 252 | NULL, pszBufQ, cb + 1)
|
---|
| 253 | > 1
|
---|
| 254 | )
|
---|
| 255 | {
|
---|
| 256 | /*
|
---|
| 257 | * We got everything now. just find the parts we need.
|
---|
| 258 | * First printer driver from QUEUE_DD
|
---|
| 259 | * Port name of the device.
|
---|
| 260 | */
|
---|
| 261 | if ((psz = strchr(pszBufQ, ';')) != NULL)
|
---|
| 262 | *psz = '\0';
|
---|
| 263 | if ((psz = strchr(pszBufQ, ',')) != NULL) //paranoia! in case comman separated list of some kind.
|
---|
| 264 | *psz = '\0';
|
---|
| 265 | if ((psz = strchr(pszBufD, ';')) != NULL)
|
---|
| 266 | *psz = '\0';
|
---|
| 267 | if ((psz = strchr(pszBufD, ',')) != NULL) //paranoia in case comman separated list of some kind.
|
---|
| 268 | *psz = '\0';
|
---|
| 269 |
|
---|
| 270 | /*
|
---|
| 271 | * Now make default printer string for the win.ini.
|
---|
| 272 | */
|
---|
| 273 | strcpy(lpszPortName, pszBufD);
|
---|
| 274 | strcat(lpszQueueName, szDefPrnQue);
|
---|
| 275 | strcat(lpszDriverName, pszBufQ);
|
---|
| 276 | dprintf(("OSLibSplFindDefaultPrinter: Successfully found default printer.'%s-%s-%s'", szDefPrnQue, lpszDriverName, pszBufQ));
|
---|
| 277 | free(pszBufQ);
|
---|
| 278 | return TRUE;
|
---|
| 279 | }
|
---|
| 280 | }
|
---|
| 281 | else
|
---|
| 282 | {
|
---|
| 283 | /* OS/2 the device may exist though the default queue is destroyed.
|
---|
| 284 | * it may still exist even if there are no queues on the system at all!
|
---|
| 285 | */
|
---|
| 286 | dprintf(("OSLibSplFindDefaultPrinter: no queue driver entry for '%s'.", szDefPrnQue));
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | free(pszBufD);
|
---|
| 290 | }
|
---|
| 291 | }
|
---|
| 292 | else
|
---|
| 293 | {
|
---|
| 294 | /* OS/2 doesn't remove the default settings if the default queue/printer is deleted. */
|
---|
| 295 | dprintf(("OSLibSplFindDefaultPrinter: can't find device settings for '%s'.", szDefPrnDev));
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
| 298 | else
|
---|
| 299 | {
|
---|
| 300 | dprintf(("OSLibSplFindDefaultPrinter: no default printer? szDefPrnDev='%s' szDefPrnQue='%s'.", szDefPrnDev, szDefPrnQue));
|
---|
| 301 | }
|
---|
| 302 | return FALSE;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 |
|
---|
| 306 | /**
|
---|
| 307 | * Does SplQueryQueue allocating a sufficent buffer of the default heap.
|
---|
| 308 | *
|
---|
| 309 | * @returns pointer to queue info.
|
---|
| 310 | * @returns NULL on failure.
|
---|
| 311 | * @param pszComputerName See SplQueryQueue.
|
---|
| 312 | * @param pszQueueName See SplQueryQueue.
|
---|
| 313 | * @param ulLevel See SplQueryQueue.
|
---|
| 314 | */
|
---|
| 315 | void * OSLibSplQueryQueue(LPSTR pszComputerName, LPSTR pszQueueName, ULONG ulLevel)
|
---|
| 316 | {
|
---|
| 317 | ULONG cbNeeded = 0;
|
---|
| 318 | SPLERR rc = SplQueryQueue(pszComputerName, pszQueueName, ulLevel, NULL, 0, &cbNeeded);
|
---|
| 319 | if (rc && rc != ERROR_MORE_DATA && rc != NERR_BufTooSmall)
|
---|
| 320 | return NULL;
|
---|
| 321 |
|
---|
| 322 | void *pv = malloc(cbNeeded);
|
---|
| 323 | rc = SplQueryQueue(pszComputerName, pszQueueName, ulLevel, pv, cbNeeded, &cbNeeded);
|
---|
| 324 | if (rc)
|
---|
| 325 | {
|
---|
| 326 | free(pv);
|
---|
| 327 | return NULL;
|
---|
| 328 | }
|
---|
| 329 | return pv;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 |
|
---|
| 333 | /**
|
---|
| 334 | * see pmref.
|
---|
| 335 | * @returns see pmref.
|
---|
| 336 | * @param pszToken see pmref.
|
---|
| 337 | * @param lCount see pmref.
|
---|
| 338 | * @param pqmdopData see pmref.
|
---|
| 339 | */
|
---|
| 340 | HSPL OSLibSplQmOpen(LPSTR pszToken, LONG lCount, LPSTR* pqmdopData)
|
---|
| 341 | {
|
---|
| 342 | return SplQmOpen(pszToken, lCount, pqmdopData);
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 |
|
---|
| 346 | /**
|
---|
| 347 | * see pmref.
|
---|
| 348 | * @returns see pmref.
|
---|
| 349 | * @param hspl see pmref.
|
---|
| 350 | */
|
---|
| 351 | BOOL OSLibSplQmClose(HANDLE hspl)
|
---|
| 352 | {
|
---|
| 353 | return SplQmClose(hspl);
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 |
|
---|
| 357 | /**
|
---|
| 358 | * see pmref.
|
---|
| 359 | * @returns see pmref.
|
---|
| 360 | * @param hspl see pmref.
|
---|
| 361 | * @param pszDocName see pmref.
|
---|
| 362 | */
|
---|
| 363 | BOOL OSLibSplQmStartDoc(HANDLE hspl, LPSTR pszDocName)
|
---|
| 364 | {
|
---|
| 365 | return SplQmStartDoc(hspl, pszDocName);
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 |
|
---|
| 369 | /**
|
---|
| 370 | * see pmref.
|
---|
| 371 | * @returns see pmref.
|
---|
| 372 | * @param hspl see pmref.
|
---|
| 373 | */
|
---|
| 374 | BOOL OSLibSplQmEndDoc(HANDLE hspl)
|
---|
| 375 | {
|
---|
| 376 | return SplQmEndDoc(hspl);
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 |
|
---|
| 380 | /**
|
---|
| 381 | * see pmref
|
---|
| 382 | * @returns see pmref
|
---|
| 383 | * @param hspl see pmref
|
---|
| 384 | * @param cbData see pmref
|
---|
| 385 | * @param pvData see pmref
|
---|
| 386 | */
|
---|
| 387 | BOOL OSLibSplQmWrite(HANDLE hspl, LONG cbData, PVOID pvData)
|
---|
| 388 | {
|
---|
| 389 | return SplQmWrite(hspl, cbData, pvData);
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 |
|
---|
| 393 | /**
|
---|
| 394 | * see pmref.
|
---|
| 395 | * @returns see pmref.
|
---|
| 396 | * @param hspl see pmref.
|
---|
| 397 | * @param ulPageNo see pmref.
|
---|
| 398 | */
|
---|
| 399 | BOOL OSLibSplQmNewPage(HANDLE hspl, ULONG ulPageNo)
|
---|
| 400 | {
|
---|
| 401 | return SplQmNewPage(hspl, ulPageNo);
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 |
|
---|
| 405 | /**
|
---|
| 406 | * Some spooler apis sets the last error.
|
---|
| 407 | *
|
---|
| 408 | * @returns last error.
|
---|
| 409 | */
|
---|
| 410 | ULONG OSLibSplWinGetLastError(void)
|
---|
| 411 | {
|
---|
| 412 | TEB *teb = GetThreadTEB();
|
---|
| 413 | return WinGetLastError(teb ? teb->o.odin.hab : 0) & 0xFFFF;
|
---|
| 414 | }
|
---|
| 415 |
|
---|