2002-03-09 22:06:58 +03:00
|
|
|
/*
|
|
|
|
* Printing interface for PuTTY.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "putty.h"
|
2004-01-20 23:35:27 +03:00
|
|
|
#include <winspool.h>
|
2002-03-12 21:27:10 +03:00
|
|
|
|
2002-03-09 22:06:58 +03:00
|
|
|
struct printer_enum_tag {
|
|
|
|
int nprinters;
|
2003-08-21 23:48:45 +04:00
|
|
|
DWORD enum_level;
|
|
|
|
union {
|
|
|
|
LPPRINTER_INFO_4 i4;
|
|
|
|
LPPRINTER_INFO_5 i5;
|
|
|
|
} info;
|
2002-03-09 22:06:58 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct printer_job_tag {
|
|
|
|
HANDLE hprinter;
|
|
|
|
};
|
|
|
|
|
2003-08-21 23:48:45 +04:00
|
|
|
static char *printer_add_enum(int param, DWORD level, char *buffer,
|
2002-03-12 21:27:10 +03:00
|
|
|
int offset, int *nprinters_ptr)
|
|
|
|
{
|
|
|
|
DWORD needed, nprinters;
|
|
|
|
|
2003-03-29 19:14:26 +03:00
|
|
|
buffer = sresize(buffer, offset+512, char);
|
2002-03-12 21:27:10 +03:00
|
|
|
|
2002-09-02 17:47:50 +04:00
|
|
|
/*
|
|
|
|
* Exploratory call to EnumPrinters to determine how much space
|
|
|
|
* we'll need for the output. Discard the return value since it
|
|
|
|
* will almost certainly be a failure due to lack of space.
|
|
|
|
*/
|
2003-08-21 23:48:45 +04:00
|
|
|
EnumPrinters(param, NULL, level, buffer+offset, 512,
|
2002-09-02 17:47:50 +04:00
|
|
|
&needed, &nprinters);
|
2002-03-12 21:27:10 +03:00
|
|
|
|
|
|
|
if (needed < 512)
|
|
|
|
needed = 512;
|
|
|
|
|
2003-03-29 19:14:26 +03:00
|
|
|
buffer = sresize(buffer, offset+needed, char);
|
2002-03-12 21:27:10 +03:00
|
|
|
|
2003-08-21 23:48:45 +04:00
|
|
|
if (EnumPrinters(param, NULL, level, buffer+offset,
|
2002-03-12 21:27:10 +03:00
|
|
|
needed, &needed, &nprinters) == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
*nprinters_ptr += nprinters;
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2002-03-09 22:06:58 +03:00
|
|
|
printer_enum *printer_start_enum(int *nprinters_ptr)
|
|
|
|
{
|
2003-03-29 19:14:26 +03:00
|
|
|
printer_enum *ret = snew(printer_enum);
|
2002-03-12 21:27:10 +03:00
|
|
|
char *buffer = NULL, *retval;
|
2002-03-09 22:06:58 +03:00
|
|
|
|
|
|
|
*nprinters_ptr = 0; /* default return value */
|
2003-03-29 19:14:26 +03:00
|
|
|
buffer = snewn(512, char);
|
2002-03-12 21:27:10 +03:00
|
|
|
|
2003-08-21 23:48:45 +04:00
|
|
|
/*
|
|
|
|
* Determine what enumeration level to use.
|
|
|
|
* When enumerating printers, we need to use PRINTER_INFO_4 on
|
|
|
|
* NT-class systems to avoid Windows looking too hard for them and
|
|
|
|
* slowing things down; and we need to avoid PRINTER_INFO_5 as
|
|
|
|
* we've seen network printers not show up.
|
|
|
|
* On 9x-class systems, PRINTER_INFO_4 isn't available and
|
|
|
|
* PRINTER_INFO_5 is recommended.
|
|
|
|
* Bletch.
|
|
|
|
*/
|
|
|
|
if (osVersion.dwPlatformId != VER_PLATFORM_WIN32_NT) {
|
|
|
|
ret->enum_level = 5;
|
|
|
|
} else {
|
|
|
|
ret->enum_level = 4;
|
|
|
|
}
|
|
|
|
|
2002-08-11 16:17:25 +04:00
|
|
|
retval = printer_add_enum(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS,
|
2003-08-21 23:48:45 +04:00
|
|
|
ret->enum_level, buffer, 0, nprinters_ptr);
|
2002-03-12 21:27:10 +03:00
|
|
|
if (!retval)
|
|
|
|
goto error;
|
|
|
|
else
|
|
|
|
buffer = retval;
|
|
|
|
|
2003-08-21 23:48:45 +04:00
|
|
|
switch (ret->enum_level) {
|
|
|
|
case 4:
|
|
|
|
ret->info.i4 = (LPPRINTER_INFO_4)buffer;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
ret->info.i5 = (LPPRINTER_INFO_5)buffer;
|
|
|
|
break;
|
|
|
|
}
|
2002-03-12 21:27:10 +03:00
|
|
|
ret->nprinters = *nprinters_ptr;
|
2002-03-09 22:06:58 +03:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
error:
|
|
|
|
sfree(buffer);
|
|
|
|
sfree(ret);
|
2002-03-12 21:27:10 +03:00
|
|
|
*nprinters_ptr = 0;
|
2002-03-09 22:06:58 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *printer_get_name(printer_enum *pe, int i)
|
|
|
|
{
|
|
|
|
if (!pe)
|
|
|
|
return NULL;
|
|
|
|
if (i < 0 || i >= pe->nprinters)
|
|
|
|
return NULL;
|
2003-08-21 23:48:45 +04:00
|
|
|
switch (pe->enum_level) {
|
|
|
|
case 4:
|
|
|
|
return pe->info.i4[i].pPrinterName;
|
|
|
|
case 5:
|
|
|
|
return pe->info.i5[i].pPrinterName;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-03-09 22:06:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void printer_finish_enum(printer_enum *pe)
|
|
|
|
{
|
|
|
|
if (!pe)
|
|
|
|
return;
|
2003-08-21 23:48:45 +04:00
|
|
|
switch (pe->enum_level) {
|
|
|
|
case 4:
|
|
|
|
sfree(pe->info.i4);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
sfree(pe->info.i5);
|
|
|
|
break;
|
|
|
|
}
|
2002-03-09 22:06:58 +03:00
|
|
|
sfree(pe);
|
|
|
|
}
|
|
|
|
|
|
|
|
printer_job *printer_start_job(char *printer)
|
|
|
|
{
|
2003-03-29 19:14:26 +03:00
|
|
|
printer_job *ret = snew(printer_job);
|
2002-03-09 22:06:58 +03:00
|
|
|
DOC_INFO_1 docinfo;
|
|
|
|
int jobstarted = 0, pagestarted = 0;
|
|
|
|
|
|
|
|
ret->hprinter = NULL;
|
|
|
|
if (!OpenPrinter(printer, &ret->hprinter, NULL))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
docinfo.pDocName = "PuTTY remote printer output";
|
|
|
|
docinfo.pOutputFile = NULL;
|
|
|
|
docinfo.pDatatype = "RAW";
|
|
|
|
|
|
|
|
if (!StartDocPrinter(ret->hprinter, 1, (LPSTR)&docinfo))
|
|
|
|
goto error;
|
|
|
|
jobstarted = 1;
|
|
|
|
|
|
|
|
if (!StartPagePrinter(ret->hprinter))
|
|
|
|
goto error;
|
|
|
|
pagestarted = 1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (pagestarted)
|
|
|
|
EndPagePrinter(ret->hprinter);
|
|
|
|
if (jobstarted)
|
|
|
|
EndDocPrinter(ret->hprinter);
|
|
|
|
if (ret->hprinter)
|
|
|
|
ClosePrinter(ret->hprinter);
|
|
|
|
sfree(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void printer_job_data(printer_job *pj, void *data, int len)
|
|
|
|
{
|
|
|
|
DWORD written;
|
|
|
|
|
|
|
|
if (!pj)
|
|
|
|
return;
|
|
|
|
|
|
|
|
WritePrinter(pj->hprinter, data, len, &written);
|
|
|
|
}
|
|
|
|
|
|
|
|
void printer_finish_job(printer_job *pj)
|
|
|
|
{
|
|
|
|
if (!pj)
|
|
|
|
return;
|
|
|
|
|
|
|
|
EndPagePrinter(pj->hprinter);
|
|
|
|
EndDocPrinter(pj->hprinter);
|
|
|
|
ClosePrinter(pj->hprinter);
|
|
|
|
sfree(pj);
|
|
|
|
}
|