forked from audetto/AppleWin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParallelPrinter.cpp
261 lines (214 loc) · 8.26 KB
/
ParallelPrinter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
AppleWin : An Apple //e emulator for Windows
Copyright (C) 1994-1996, Michael O'Brien
Copyright (C) 1999-2001, Oliver Schmidt
Copyright (C) 2002-2005, Tom Charlesworth
Copyright (C) 2006-2007, Tom Charlesworth, Michael Pohoreski, Nick Westgate
AppleWin is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
AppleWin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with AppleWin; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* Description: Parallel Printer Interface Card emulation
*
* Author: Nick Westgate, Stannev
*/
#include "StdAfx.h"
#include "ParallelPrinter.h"
#include "Core.h"
#include "Memory.h"
#include "Pravets.h"
#include "Registry.h"
#include "YamlHelper.h"
#include "Interface.h"
#include "../resource/resource.h"
static DWORD inactivity = 0;
static unsigned int g_PrinterIdleLimit = 10;
static FILE* file = NULL;
DWORD const PRINTDRVR_SIZE = APPLE_SLOT_SIZE;
#define DEFAULT_PRINT_FILENAME "Printer.txt"
static std::string g_szPrintFilename;
bool g_bDumpToPrinter = false;
bool g_bConvertEncoding = true;
bool g_bFilterUnprintable = true;
bool g_bPrinterAppend = false;
bool g_bEnableDumpToRealPrinter = false;
//===========================================================================
static BYTE __stdcall PrintStatus(WORD, WORD, BYTE, BYTE, ULONG);
static BYTE __stdcall PrintTransmit(WORD, WORD, BYTE, BYTE value, ULONG);
VOID PrintLoadRom(LPBYTE pCxRomPeripheral, const UINT uSlot)
{
BYTE* pData = GetFrame().GetResource(IDR_PRINTDRVR_FW, "FIRMWARE", PRINTDRVR_SIZE);
if(pData == NULL)
return;
memcpy(pCxRomPeripheral + uSlot*256, pData, PRINTDRVR_SIZE);
//
RegisterIoHandler(uSlot, PrintStatus, PrintTransmit, NULL, NULL, NULL, NULL);
}
//===========================================================================
static BOOL CheckPrint()
{
inactivity = 0;
if (file == NULL)
{
//TCHAR filepath[MAX_PATH * 2];
//_tcsncpy(filepath, g_sProgramDir, MAX_PATH);
//_tcsncat(filepath, _T("Printer.txt"), MAX_PATH);
//file = fopen(filepath, "wb");
if (g_bPrinterAppend )
file = fopen(Printer_GetFilename().c_str(), "ab");
else
file = fopen(Printer_GetFilename().c_str(), "wb");
}
return (file != NULL);
}
//===========================================================================
static void ClosePrint()
{
if (file != NULL)
{
fclose(file);
file = NULL;
std::string ExtendedFileName = "copy \"";
ExtendedFileName.append (Printer_GetFilename());
ExtendedFileName.append ("\" prn");
//if (g_bDumpToPrinter) ShellExecute(NULL, "print", Printer_GetFilename(), NULL, NULL, 0); //Print through Notepad
if (g_bDumpToPrinter)
system (ExtendedFileName.c_str ()); //Print through console. This is supposed to be the better way, because it shall print images (with older printers only).
}
inactivity = 0;
}
//===========================================================================
void PrintDestroy()
{
ClosePrint();
}
//===========================================================================
void PrintUpdate(DWORD totalcycles)
{
if (file == NULL)
{
return;
}
// if ((inactivity += totalcycles) > (Printer_GetIdleLimit () * 1000 * 1000)) //This line seems to give a very big deviation
if ((inactivity += totalcycles) > (Printer_GetIdleLimit () * 710000))
{
// inactive, so close the file (next print will overwrite or append to it, according to the settings made)
ClosePrint();
}
}
//===========================================================================
void PrintReset()
{
ClosePrint();
}
//===========================================================================
static BYTE __stdcall PrintStatus(WORD, WORD, BYTE, BYTE, ULONG)
{
CheckPrint();
return 0xFF; // status - TODO?
}
//===========================================================================
static BYTE __stdcall PrintTransmit(WORD, WORD, BYTE, BYTE value, ULONG)
{
if (!CheckPrint())
return 0;
BYTE c = value & 0x7F;
if (IsPravets(GetApple2Type()))
{
if (g_bConvertEncoding)
c = GetPravets().ConvertToPrinterChar(value);
}
if ((g_bFilterUnprintable == false) || (c>31) || (c==13) || (c==10) || (c>0x7F)) //c>0x7F is needed for cyrillic characters
fwrite(&c, 1, 1, file);
return 0;
}
//===========================================================================
const std::string & Printer_GetFilename()
{
return g_szPrintFilename;
}
void Printer_SetFilename(const std::string & prtFilename)
{
if (!prtFilename.empty())
{
g_szPrintFilename = prtFilename;
}
else //No registry entry is available
{
g_szPrintFilename = g_sProgramDir + DEFAULT_PRINT_FILENAME;
RegSaveString(REG_CONFIG, REGVALUE_PRINTER_FILENAME, 1, g_szPrintFilename);
}
}
unsigned int Printer_GetIdleLimit()
{
return g_PrinterIdleLimit;
}
void Printer_SetIdleLimit(unsigned int Duration)
{
g_PrinterIdleLimit = Duration;
}
//===========================================================================
#define SS_YAML_VALUE_CARD_PRINTER "Generic Printer"
#define SS_YAML_KEY_INACTIVITY "Inactivity"
#define SS_YAML_KEY_IDLELIMIT "Printer Idle Limit"
#define SS_YAML_KEY_FILENAME "Print Filename"
#define SS_YAML_KEY_FILEOPEN "Is File Open"
#define SS_YAML_KEY_DUMPTOPRINTER "Dump To Printer"
#define SS_YAML_KEY_CONVERTENCODING "Convert Encoding"
#define SS_YAML_KEY_FILTERUNPRINTABLE "Filter Unprintable"
#define SS_YAML_KEY_APPEND "Printer Append"
#define SS_YAML_KEY_DUMPTOREALPRINTER "Enable Dump To Real Printer"
std::string Printer_GetSnapshotCardName(void)
{
static const std::string name(SS_YAML_VALUE_CARD_PRINTER);
return name;
}
void Printer_SaveSnapshot(class YamlSaveHelper& yamlSaveHelper, const UINT uSlot)
{
YamlSaveHelper::Slot slot(yamlSaveHelper, Printer_GetSnapshotCardName(), uSlot, 1);
YamlSaveHelper::Label state(yamlSaveHelper, "%s:\n", SS_YAML_KEY_STATE);
yamlSaveHelper.SaveUint(SS_YAML_KEY_INACTIVITY, inactivity);
yamlSaveHelper.SaveUint(SS_YAML_KEY_IDLELIMIT, g_PrinterIdleLimit);
yamlSaveHelper.SaveString(SS_YAML_KEY_FILENAME, g_szPrintFilename);
yamlSaveHelper.SaveBool(SS_YAML_KEY_FILEOPEN, (file != NULL) ? true : false);
yamlSaveHelper.SaveBool(SS_YAML_KEY_DUMPTOPRINTER, g_bDumpToPrinter);
yamlSaveHelper.SaveBool(SS_YAML_KEY_CONVERTENCODING, g_bConvertEncoding);
yamlSaveHelper.SaveBool(SS_YAML_KEY_FILTERUNPRINTABLE, g_bFilterUnprintable);
yamlSaveHelper.SaveBool(SS_YAML_KEY_APPEND, g_bPrinterAppend);
yamlSaveHelper.SaveBool(SS_YAML_KEY_DUMPTOREALPRINTER, g_bEnableDumpToRealPrinter);
}
bool Printer_LoadSnapshot(class YamlLoadHelper& yamlLoadHelper, UINT slot, UINT version)
{
if (slot != 1) // fixme
throw std::runtime_error("Card: wrong slot");
if (version != 1)
throw std::runtime_error("Card: wrong version");
inactivity = yamlLoadHelper.LoadUint(SS_YAML_KEY_INACTIVITY);
g_PrinterIdleLimit = yamlLoadHelper.LoadUint(SS_YAML_KEY_IDLELIMIT);
g_szPrintFilename = yamlLoadHelper.LoadString(SS_YAML_KEY_FILENAME);
if (yamlLoadHelper.LoadBool(SS_YAML_KEY_FILEOPEN))
{
yamlLoadHelper.LoadBool(SS_YAML_KEY_APPEND); // Consume
g_bPrinterAppend = true; // Re-open print-file in append mode
BOOL bRes = CheckPrint();
if (!bRes)
throw std::runtime_error("Printer Card: Unable to resume printing to file");
}
else
{
g_bPrinterAppend = yamlLoadHelper.LoadBool(SS_YAML_KEY_APPEND);
}
g_bDumpToPrinter = yamlLoadHelper.LoadBool(SS_YAML_KEY_DUMPTOPRINTER);
g_bConvertEncoding = yamlLoadHelper.LoadBool(SS_YAML_KEY_CONVERTENCODING);
g_bFilterUnprintable = yamlLoadHelper.LoadBool(SS_YAML_KEY_FILTERUNPRINTABLE);
g_bEnableDumpToRealPrinter = yamlLoadHelper.LoadBool(SS_YAML_KEY_DUMPTOREALPRINTER);
return true;
}