]>
Commit | Line | Data |
---|---|---|
1 | /* | |
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 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
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 | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | * | |
20 | * See the COPYING file for full license information. | |
21 | */ | |
22 | ||
23 | #include "src/lpr/Printer.hh" | |
24 | #include "src/lpr/Win32Utils.hh" | |
25 | using namespace std; | |
26 | ||
27 | inline LPTSTR tstr(const string & str) | |
28 | { | |
29 | return const_cast<char*>(str.c_str()); | |
30 | } | |
31 | ||
32 | Printer::Printer(const string &name, bool debugFlag) throw(PrinterException) | |
33 | : m_devName(name), | |
34 | m_debugFlag(debugFlag), | |
35 | m_devHandle(INVALID_HANDLE_VALUE), | |
36 | m_devMode(0), | |
37 | m_buffer(0), | |
38 | m_bufferSize(4096), | |
39 | m_bufferIndex(0), | |
40 | m_rawFlag(false) | |
41 | { | |
42 | m_buffer = new unsigned char[m_bufferSize]; | |
43 | mapPortName(); | |
44 | ||
45 | open(); | |
46 | ||
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]); | |
53 | ||
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) | |
78 | { | |
79 | ClosePrinter(m_devHandle); | |
80 | m_devHandle = INVALID_HANDLE_VALUE; | |
81 | } | |
82 | } | |
83 | ||
84 | void Printer::endDoc() throw(PrinterException) | |
85 | { | |
86 | if (!EndDocPrinter(m_devHandle)) | |
87 | throw PrinterException("EndDocPrinter error: " + | |
88 | Win32Utils::getErrorMessage()); | |
89 | } | |
90 | ||
91 | void Printer::endPage() throw(PrinterException) | |
92 | { | |
93 | cout << "Printer::endPage()" << endl; | |
94 | if (!EndPagePrinter(m_devHandle)) | |
95 | throw PrinterException("EndPagePrinter error: " + | |
96 | Win32Utils::getErrorMessage()); | |
97 | } | |
98 | ||
99 | void Printer::flush(void) throw(PrinterException) | |
100 | { | |
101 | if (m_bufferIndex > 0) | |
102 | { | |
103 | write(m_buffer, m_bufferIndex); | |
104 | m_bufferIndex = 0; | |
105 | } | |
106 | } | |
107 | ||
108 | bool Printer::getRawFlag() const | |
109 | { | |
110 | return m_rawFlag; | |
111 | } | |
112 | ||
113 | void Printer::print(istream & in, const string & docName) throw(PrinterException) | |
114 | { | |
115 | startDoc(docName); | |
116 | ||
117 | char ch; | |
118 | char lastCh = '\0'; | |
119 | while (in.get(ch)) | |
120 | { | |
121 | if (!m_rawFlag && ch == '\n' && lastCh != '\r') | |
122 | put('\r'); | |
123 | put(ch); | |
124 | lastCh = ch; | |
125 | } | |
126 | flush(); | |
127 | endDoc(); | |
128 | } | |
129 | ||
130 | void Printer::put(unsigned char ch) throw(PrinterException) | |
131 | { | |
132 | if (m_bufferIndex == m_bufferSize) | |
133 | flush(); | |
134 | m_buffer[m_bufferIndex++] = ch; | |
135 | } | |
136 | ||
137 | void Printer::setRawFlag(bool flag) | |
138 | { | |
139 | m_rawFlag = flag; | |
140 | } | |
141 | ||
142 | void Printer::startDoc(const string & docName) throw(PrinterException) | |
143 | { | |
144 | DOC_INFO_1 di1; | |
145 | CHAR raw[] = "RAW"; | |
146 | open(); | |
147 | ||
148 | di1.pDocName = tstr(docName); | |
149 | di1.pOutputFile = 0; | |
150 | di1.pDatatype = raw; | |
151 | ||
152 | if (StartDocPrinter(m_devHandle, 1, (LPBYTE) &di1) == 0) | |
153 | throw PrinterException("StartDocPrinter error: " + | |
154 | Win32Utils::getErrorMessage()); | |
155 | } | |
156 | ||
157 | void Printer::startPage() throw(PrinterException) | |
158 | { | |
159 | cout << "Printer::startPage()" << endl; | |
160 | if (!StartPagePrinter(m_devHandle)) | |
161 | throw PrinterException("StartPagePrinter error: " + | |
162 | Win32Utils::getErrorMessage()); | |
163 | } | |
164 | ||
165 | void Printer::write(unsigned char *buf, unsigned int size) throw(PrinterException) | |
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 | ||
179 | Printer::PrinterList Printer::enumPrinters(DWORD flags, LPTSTR name) throw(PrinterException) | |
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) | |
194 | { | |
195 | if (m_debugFlag) | |
196 | cerr << "Printer name: '" << pi->pPrinterName << "', " | |
197 | << "Port name: '" << pi->pPortName << "'" << endl; | |
198 | list.push_back(*pi++); | |
199 | } | |
200 | ||
201 | return list; | |
202 | } | |
203 | ||
204 | void Printer::mapPortName() throw(PrinterException) | |
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) | |
223 | { | |
224 | TCHAR empty[] = ""; | |
225 | PrinterList list = enumPrinters(PRINTER_ENUM_NAME, empty); | |
226 | for (PrinterList::iterator it = list.begin(); it != list.end(); ++it) | |
227 | { | |
228 | if (strcasecmp(m_devName.c_str(), it->pPortName) == 0) | |
229 | { | |
230 | if (m_debugFlag) | |
231 | cout << "Mapped '" << m_devName << "' to '" << it->pPrinterName | |
232 | << "'" << endl; | |
233 | m_devName = it->pPrinterName; | |
234 | return; | |
235 | } | |
236 | } | |
237 | } | |
238 | ||
239 | else if (isWindows2K_NT4) | |
240 | { | |
241 | PrinterList list = enumPrinters(PRINTER_ENUM_LOCAL, 0); | |
242 | for (PrinterList::iterator it = list.begin(); it != list.end(); ++it) | |
243 | { | |
244 | if (strcasecmp(m_devName.c_str(), it->pPortName) == 0) | |
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 | { | |
257 | if (strcasecmp(m_devName.c_str(), it->pPortName) == 0) | |
258 | { | |
259 | if (m_debugFlag) | |
260 | cout << "Mapped '" << m_devName << "' to '" << it->pPrinterName | |
261 | << "'" << endl; | |
262 | m_devName = it->pPrinterName; | |
263 | return; | |
264 | } | |
265 | } | |
266 | } | |
267 | } | |
268 | ||
269 | void Printer::open() throw(PrinterException) | |
270 | { | |
271 | PRINTER_DEFAULTS prDef; | |
272 | CHAR raw[] = "RAW"; | |
273 | ||
274 | prDef.pDatatype = raw; | |
275 | prDef.pDevMode = m_devMode; | |
276 | prDef.DesiredAccess = PRINTER_ACCESS_USE; | |
277 | ||
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 | } |