]>
Commit | Line | Data |
---|---|---|
fe3a7d70 | 1 | /* |
21006e63 CW |
2 | * lpr for cygwin/windows |
3 | * | |
4 | * Copyright (C) 2000-2003 Rick Rankin | |
5 | * http://www.cygwin.com/ml/cygwin/2000-07/msg00320.html | |
6 | * | |
d76073f1 | 7 | * This program is free software: you can redistribute it and/or modify |
21006e63 | 8 | * it under the terms of the GNU General Public License as published by |
d76073f1 CW |
9 | * the Free Software Foundation, either version 3 of the License, or |
10 | * (at your option) any later version. | |
21006e63 CW |
11 | * |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
d76073f1 CW |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | * | |
20 | * See the COPYING file for full license information. | |
21006e63 CW |
21 | */ |
22 | ||
23 | #include "src/lpr/Printer.hh" | |
24 | #include "src/lpr/Win32Utils.hh" | |
fe3a7d70 | 25 | using namespace std; |
21006e63 CW |
26 | |
27 | inline LPTSTR tstr(const string & str) | |
28 | { | |
29 | return const_cast<char*>(str.c_str()); | |
30 | } | |
31 | ||
fe3a7d70 CW |
32 | Printer::Printer(const string &name, bool debugFlag) throw(PrinterException) |
33 | : m_devName(name), | |
21006e63 CW |
34 | m_debugFlag(debugFlag), |
35 | m_devHandle(INVALID_HANDLE_VALUE), | |
36 | m_devMode(0), | |
37 | m_buffer(0), | |
38 | m_bufferSize(4096), | |
a18d2869 CW |
39 | m_bufferIndex(0), |
40 | m_rawFlag(false) | |
21006e63 CW |
41 | { |
42 | m_buffer = new unsigned char[m_bufferSize]; | |
43 | mapPortName(); | |
44 | ||
45 | open(); | |
fe3a7d70 | 46 | |
21006e63 CW |
47 | // Initialize the DEVMODE structure |
48 | // Get the amount of space needed for the buffer. | |
49 | DWORD needed = DocumentProperties(NULL, m_devHandle, tstr(m_devName), | |
50 | NULL, NULL, 0); | |
51 | ||
52 | m_devMode = reinterpret_cast<DEVMODE *>(new unsigned char[needed]); | |
fe3a7d70 | 53 | |
21006e63 CW |
54 | // Get the default DEVMODE for the printer. |
55 | DWORD ret = DocumentProperties(NULL, m_devHandle, tstr(m_devName), | |
56 | m_devMode, NULL, DM_OUT_BUFFER); | |
57 | if (ret != IDOK) | |
58 | throw PrinterException("error getting the default device mode: " + | |
59 | Win32Utils::getErrorMessage()); | |
60 | ||
61 | // Close the printer, because we'll need to reopen it later, passing it | |
62 | // the DEVMODE structure with any optional changes by the user. | |
63 | close(); | |
64 | } | |
65 | ||
66 | Printer::~Printer() | |
67 | { | |
68 | close(); | |
69 | if (m_buffer != 0) | |
70 | delete [] m_buffer; | |
71 | if (m_devMode != 0) | |
72 | delete m_devMode; | |
73 | } | |
74 | ||
75 | void Printer::close() | |
76 | { | |
77 | if (m_devHandle != INVALID_HANDLE_VALUE) | |
fe3a7d70 CW |
78 | { |
79 | ClosePrinter(m_devHandle); | |
80 | m_devHandle = INVALID_HANDLE_VALUE; | |
81 | } | |
21006e63 CW |
82 | } |
83 | ||
fe3a7d70 | 84 | void Printer::endDoc() throw(PrinterException) |
21006e63 CW |
85 | { |
86 | if (!EndDocPrinter(m_devHandle)) | |
87 | throw PrinterException("EndDocPrinter error: " + | |
88 | Win32Utils::getErrorMessage()); | |
89 | } | |
90 | ||
fe3a7d70 | 91 | void Printer::endPage() throw(PrinterException) |
21006e63 CW |
92 | { |
93 | cout << "Printer::endPage()" << endl; | |
94 | if (!EndPagePrinter(m_devHandle)) | |
95 | throw PrinterException("EndPagePrinter error: " + | |
96 | Win32Utils::getErrorMessage()); | |
97 | } | |
98 | ||
fe3a7d70 | 99 | void Printer::flush(void) throw(PrinterException) |
21006e63 CW |
100 | { |
101 | if (m_bufferIndex > 0) | |
fe3a7d70 CW |
102 | { |
103 | write(m_buffer, m_bufferIndex); | |
104 | m_bufferIndex = 0; | |
105 | } | |
21006e63 CW |
106 | } |
107 | ||
a18d2869 CW |
108 | bool Printer::getRawFlag() const |
109 | { | |
110 | return m_rawFlag; | |
111 | } | |
112 | ||
fe3a7d70 | 113 | void Printer::print(istream & in, const string & docName) throw(PrinterException) |
21006e63 CW |
114 | { |
115 | startDoc(docName); | |
116 | ||
117 | char ch; | |
118 | char lastCh = '\0'; | |
807fb049 | 119 | while (in.get(ch)) |
fe3a7d70 CW |
120 | { |
121 | if (!m_rawFlag && ch == '\n' && lastCh != '\r') | |
122 | put('\r'); | |
123 | put(ch); | |
124 | lastCh = ch; | |
125 | } | |
21006e63 CW |
126 | flush(); |
127 | endDoc(); | |
128 | } | |
129 | ||
fe3a7d70 | 130 | void Printer::put(unsigned char ch) throw(PrinterException) |
21006e63 CW |
131 | { |
132 | if (m_bufferIndex == m_bufferSize) | |
133 | flush(); | |
134 | m_buffer[m_bufferIndex++] = ch; | |
135 | } | |
a18d2869 CW |
136 | |
137 | void Printer::setRawFlag(bool flag) | |
138 | { | |
139 | m_rawFlag = flag; | |
140 | } | |
141 | ||
fe3a7d70 | 142 | void Printer::startDoc(const string & docName) throw(PrinterException) |
21006e63 CW |
143 | { |
144 | DOC_INFO_1 di1; | |
7854241f | 145 | CHAR raw[] = "RAW"; |
21006e63 | 146 | open(); |
fe3a7d70 | 147 | |
21006e63 CW |
148 | di1.pDocName = tstr(docName); |
149 | di1.pOutputFile = 0; | |
7854241f | 150 | di1.pDatatype = raw; |
fe3a7d70 | 151 | |
21006e63 CW |
152 | if (StartDocPrinter(m_devHandle, 1, (LPBYTE) &di1) == 0) |
153 | throw PrinterException("StartDocPrinter error: " + | |
154 | Win32Utils::getErrorMessage()); | |
155 | } | |
156 | ||
fe3a7d70 | 157 | void Printer::startPage() throw(PrinterException) |
21006e63 CW |
158 | { |
159 | cout << "Printer::startPage()" << endl; | |
160 | if (!StartPagePrinter(m_devHandle)) | |
161 | throw PrinterException("StartPagePrinter error: " + | |
162 | Win32Utils::getErrorMessage()); | |
163 | } | |
164 | ||
fe3a7d70 | 165 | void Printer::write(unsigned char *buf, unsigned int size) throw(PrinterException) |
21006e63 CW |
166 | { |
167 | DWORD written; | |
168 | if (!WritePrinter(m_devHandle, buf, size, &written)) | |
169 | throw PrinterException("error writing to device '" + m_devName + "': " + | |
170 | Win32Utils::getErrorMessage()); | |
171 | } | |
172 | ||
173 | //*************************************************************************** | |
174 | // | |
175 | // Private operations | |
176 | // | |
177 | //*************************************************************************** | |
178 | ||
fe3a7d70 | 179 | Printer::PrinterList Printer::enumPrinters(DWORD flags, LPTSTR name) throw(PrinterException) |
21006e63 CW |
180 | { |
181 | DWORD numPrinters; | |
182 | DWORD bytesNeeded; | |
183 | EnumPrinters(flags, name, 5, 0, 0, &bytesNeeded, &numPrinters); | |
184 | auto_ptr<BYTE> enumBuffer(new BYTE[bytesNeeded]); | |
185 | if (!EnumPrinters(flags, name, 5, enumBuffer.get(), bytesNeeded, | |
186 | &bytesNeeded, &numPrinters)) | |
187 | throw PrinterException("unable to enumerate printers: " + | |
188 | Win32Utils::getErrorMessage()); | |
189 | ||
190 | PRINTER_INFO_5 *pi = reinterpret_cast<PRINTER_INFO_5 *>(enumBuffer.get()); | |
191 | ||
192 | PrinterList list; | |
193 | for (unsigned int ii = 0; ii < numPrinters; ++ii) | |
fe3a7d70 CW |
194 | { |
195 | if (m_debugFlag) | |
196 | cerr << "Printer name: '" << pi->pPrinterName << "', " | |
197 | << "Port name: '" << pi->pPortName << "'" << endl; | |
198 | list.push_back(*pi++); | |
199 | } | |
21006e63 CW |
200 | |
201 | return list; | |
202 | } | |
203 | ||
fe3a7d70 | 204 | void Printer::mapPortName() throw(PrinterException) |
21006e63 CW |
205 | { |
206 | ||
207 | OSVERSIONINFO osvi; | |
208 | osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
209 | GetVersionEx(&osvi); | |
210 | bool isWindows9x = (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS && | |
211 | (osvi.dwMajorVersion > 4 || | |
212 | (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion > 0))); | |
213 | ||
214 | bool isWindows2K_NT4 = (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT && | |
215 | osvi.dwMajorVersion >= 4); | |
216 | ||
217 | if (m_debugFlag) | |
218 | cout << "isWindows9x = " << isWindows9x << ", " | |
219 | << "isWindows2K_NT4 = " << isWindows2K_NT4 << endl; | |
220 | ||
221 | ||
222 | if (isWindows9x) | |
21006e63 | 223 | { |
7854241f CW |
224 | TCHAR empty[] = ""; |
225 | PrinterList list = enumPrinters(PRINTER_ENUM_NAME, empty); | |
fe3a7d70 CW |
226 | for (PrinterList::iterator it = list.begin(); it != list.end(); ++it) |
227 | { | |
c3bb28d5 | 228 | if (strcasecmp(m_devName.c_str(), it->pPortName) == 0) |
fe3a7d70 CW |
229 | { |
230 | if (m_debugFlag) | |
231 | cout << "Mapped '" << m_devName << "' to '" << it->pPrinterName | |
232 | << "'" << endl; | |
233 | m_devName = it->pPrinterName; | |
234 | return; | |
235 | } | |
236 | } | |
21006e63 | 237 | } |
21006e63 CW |
238 | |
239 | else if (isWindows2K_NT4) | |
21006e63 | 240 | { |
fe3a7d70 CW |
241 | PrinterList list = enumPrinters(PRINTER_ENUM_LOCAL, 0); |
242 | for (PrinterList::iterator it = list.begin(); it != list.end(); ++it) | |
243 | { | |
c3bb28d5 | 244 | if (strcasecmp(m_devName.c_str(), it->pPortName) == 0) |
fe3a7d70 CW |
245 | { |
246 | if (m_debugFlag) | |
247 | cout << "Mapped '" << m_devName << "' to '" << it->pPrinterName | |
248 | << "'" << endl; | |
249 | m_devName = it->pPrinterName; | |
250 | return; | |
251 | } | |
252 | } | |
253 | ||
254 | list = enumPrinters(PRINTER_ENUM_CONNECTIONS, 0); | |
255 | for (PrinterList::iterator it = list.begin(); it != list.end(); ++it) | |
256 | { | |
c3bb28d5 | 257 | if (strcasecmp(m_devName.c_str(), it->pPortName) == 0) |
fe3a7d70 CW |
258 | { |
259 | if (m_debugFlag) | |
260 | cout << "Mapped '" << m_devName << "' to '" << it->pPrinterName | |
261 | << "'" << endl; | |
262 | m_devName = it->pPrinterName; | |
263 | return; | |
264 | } | |
265 | } | |
21006e63 | 266 | } |
21006e63 CW |
267 | } |
268 | ||
fe3a7d70 | 269 | void Printer::open() throw(PrinterException) |
21006e63 CW |
270 | { |
271 | PRINTER_DEFAULTS prDef; | |
7854241f | 272 | CHAR raw[] = "RAW"; |
fe3a7d70 | 273 | |
7854241f | 274 | prDef.pDatatype = raw; |
21006e63 | 275 | prDef.pDevMode = m_devMode; |
669b09e4 | 276 | prDef.DesiredAccess = PRINTER_ACCESS_USE; |
fe3a7d70 | 277 | |
21006e63 CW |
278 | if (m_devHandle == INVALID_HANDLE_VALUE && |
279 | !OpenPrinter(tstr(m_devName), &m_devHandle, &prDef)) | |
280 | throw PrinterException("can't open '" + m_devName + "' for writing: " + | |
281 | Win32Utils::getErrorMessage()); | |
282 | } |