mirror of
https://github.com/AppleWin/AppleWin.git
synced 2025-09-25 11:25:36 +00:00
Refactor ParallelPrinter.cpp/h as a C++ class (PR #1067)
Add command line: -s1 parallel. NB. Only a single Parallel Printer card is supported, and currently it's restricted to slot 1.
This commit is contained in:
@@ -38,113 +38,99 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
#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)
|
||||
void ParallelPrinterCard::InitializeIO(LPBYTE pCxRomPeripheral)
|
||||
{
|
||||
const DWORD PRINTDRVR_SIZE = APPLE_SLOT_SIZE;
|
||||
BYTE* pData = GetFrame().GetResource(IDR_PRINTDRVR_FW, "FIRMWARE", PRINTDRVR_SIZE);
|
||||
if(pData == NULL)
|
||||
return;
|
||||
|
||||
memcpy(pCxRomPeripheral + uSlot*256, pData, PRINTDRVR_SIZE);
|
||||
memcpy(pCxRomPeripheral + m_slot*APPLE_SLOT_SIZE, pData, PRINTDRVR_SIZE);
|
||||
|
||||
//
|
||||
|
||||
RegisterIoHandler(uSlot, PrintStatus, PrintTransmit, NULL, NULL, NULL, NULL);
|
||||
RegisterIoHandler(m_slot, IORead, IOWrite, NULL, NULL, this, NULL);
|
||||
}
|
||||
|
||||
//===========================================================================
|
||||
static BOOL CheckPrint()
|
||||
bool ParallelPrinterCard::CheckPrint(void)
|
||||
{
|
||||
inactivity = 0;
|
||||
if (file == NULL)
|
||||
{
|
||||
m_inactivity = 0;
|
||||
if (m_file == NULL)
|
||||
{
|
||||
//TCHAR filepath[MAX_PATH * 2];
|
||||
//_tcsncpy(filepath, g_sProgramDir, MAX_PATH);
|
||||
//_tcsncat(filepath, _T("Printer.txt"), MAX_PATH);
|
||||
//_tcsncat(filepath, _T("Printer.txt"), MAX_PATH);
|
||||
//file = fopen(filepath, "wb");
|
||||
if (g_bPrinterAppend )
|
||||
file = fopen(Printer_GetFilename().c_str(), "ab");
|
||||
if (m_bPrinterAppend )
|
||||
m_file = fopen(ParallelPrinterCard::GetFilename().c_str(), "ab");
|
||||
else
|
||||
file = fopen(Printer_GetFilename().c_str(), "wb");
|
||||
}
|
||||
return (file != NULL);
|
||||
m_file = fopen(ParallelPrinterCard::GetFilename().c_str(), "wb");
|
||||
}
|
||||
return (m_file != NULL);
|
||||
}
|
||||
|
||||
//===========================================================================
|
||||
static void ClosePrint()
|
||||
void ParallelPrinterCard::ClosePrint(void)
|
||||
{
|
||||
if (file != NULL)
|
||||
{
|
||||
fclose(file);
|
||||
file = NULL;
|
||||
if (m_file != NULL)
|
||||
{
|
||||
fclose(m_file);
|
||||
m_file = NULL;
|
||||
std::string ExtendedFileName = "copy \"";
|
||||
ExtendedFileName.append (Printer_GetFilename());
|
||||
ExtendedFileName.append (ParallelPrinterCard::GetFilename());
|
||||
ExtendedFileName.append ("\" prn");
|
||||
//if (g_bDumpToPrinter) ShellExecute(NULL, "print", Printer_GetFilename(), NULL, NULL, 0); //Print through Notepad
|
||||
if (g_bDumpToPrinter)
|
||||
if (m_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;
|
||||
}
|
||||
m_inactivity = 0;
|
||||
}
|
||||
|
||||
//===========================================================================
|
||||
void PrintDestroy()
|
||||
void ParallelPrinterCard::Destroy(void)
|
||||
{
|
||||
ClosePrint();
|
||||
ClosePrint();
|
||||
}
|
||||
|
||||
//===========================================================================
|
||||
void PrintUpdate(DWORD totalcycles)
|
||||
void ParallelPrinterCard::Update(const ULONG nExecutedCycles)
|
||||
{
|
||||
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();
|
||||
}
|
||||
if (m_file == NULL)
|
||||
return;
|
||||
|
||||
// if ((inactivity += totalcycles) > (Printer_GetIdleLimit () * 1000 * 1000)) //This line seems to give a very big deviation
|
||||
if ((m_inactivity += nExecutedCycles) > (ParallelPrinterCard::GetIdleLimit () * 710000))
|
||||
{
|
||||
// inactive, so close the file (next print will overwrite or append to it, according to the settings made)
|
||||
ClosePrint();
|
||||
}
|
||||
}
|
||||
|
||||
//===========================================================================
|
||||
void PrintReset()
|
||||
void ParallelPrinterCard::Reset(const bool powerCycle)
|
||||
{
|
||||
ClosePrint();
|
||||
ClosePrint();
|
||||
}
|
||||
|
||||
//===========================================================================
|
||||
static BYTE __stdcall PrintStatus(WORD, WORD, BYTE, BYTE, ULONG)
|
||||
BYTE __stdcall ParallelPrinterCard::IORead(WORD, WORD address, BYTE, BYTE, ULONG)
|
||||
{
|
||||
CheckPrint();
|
||||
return 0xFF; // status - TODO?
|
||||
UINT slot = ((address & 0xff) >> 4) - 8;
|
||||
ParallelPrinterCard* card = (ParallelPrinterCard*)MemGetSlotParameters(slot);
|
||||
|
||||
card->CheckPrint();
|
||||
return 0xFF; // status - TODO?
|
||||
}
|
||||
|
||||
//===========================================================================
|
||||
static BYTE __stdcall PrintTransmit(WORD, WORD address, BYTE, BYTE value, ULONG)
|
||||
BYTE __stdcall ParallelPrinterCard::IOWrite(WORD, WORD address, BYTE, BYTE value, ULONG)
|
||||
{
|
||||
if (!CheckPrint())
|
||||
UINT slot = ((address & 0xff) >> 4) - 8;
|
||||
ParallelPrinterCard* card = (ParallelPrinterCard*)MemGetSlotParameters(slot);
|
||||
|
||||
if (!card->CheckPrint())
|
||||
return 0;
|
||||
|
||||
// only allow writes to the load output port (i.e., $C090)
|
||||
@@ -155,44 +141,85 @@ static BYTE __stdcall PrintTransmit(WORD, WORD address, BYTE, BYTE value, ULONG)
|
||||
|
||||
if (IsPravets(GetApple2Type()))
|
||||
{
|
||||
if (g_bConvertEncoding)
|
||||
if (card->m_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);
|
||||
if ((card->m_bFilterUnprintable == false) || (c>31) || (c==13) || (c==10) || (c>0x7F)) //c>0x7F is needed for cyrillic characters
|
||||
fwrite(&c, 1, 1, card->m_file);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//===========================================================================
|
||||
|
||||
const std::string & Printer_GetFilename()
|
||||
const std::string& ParallelPrinterCard::GetFilename(void)
|
||||
{
|
||||
return g_szPrintFilename;
|
||||
return m_szPrintFilename;
|
||||
}
|
||||
|
||||
void Printer_SetFilename(const std::string & prtFilename)
|
||||
#define DEFAULT_PRINT_FILENAME "Printer.txt"
|
||||
|
||||
void ParallelPrinterCard::SetFilename(const std::string& prtFilename)
|
||||
{
|
||||
if (!prtFilename.empty())
|
||||
{
|
||||
g_szPrintFilename = prtFilename;
|
||||
m_szPrintFilename = prtFilename;
|
||||
}
|
||||
else //No registry entry is available
|
||||
{
|
||||
g_szPrintFilename = g_sProgramDir + DEFAULT_PRINT_FILENAME;
|
||||
RegSaveString(REG_CONFIG, REGVALUE_PRINTER_FILENAME, 1, g_szPrintFilename);
|
||||
m_szPrintFilename = g_sProgramDir + DEFAULT_PRINT_FILENAME;
|
||||
RegSaveString(REG_CONFIG, REGVALUE_PRINTER_FILENAME, 1, m_szPrintFilename);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int Printer_GetIdleLimit()
|
||||
UINT ParallelPrinterCard::GetIdleLimit(void)
|
||||
{
|
||||
return g_PrinterIdleLimit;
|
||||
return m_printerIdleLimit;
|
||||
}
|
||||
|
||||
void Printer_SetIdleLimit(unsigned int Duration)
|
||||
void ParallelPrinterCard::SetIdleLimit(UINT Duration)
|
||||
{
|
||||
g_PrinterIdleLimit = Duration;
|
||||
m_printerIdleLimit = Duration;
|
||||
}
|
||||
|
||||
//===========================================================================
|
||||
|
||||
void ParallelPrinterCard::GetRegistryConfig(void)
|
||||
{
|
||||
std::string regSection = RegGetConfigSlotSection(m_slot);
|
||||
|
||||
DWORD dwTmp;
|
||||
char szFilename[MAX_PATH];
|
||||
|
||||
if (RegLoadValue(regSection.c_str(), REGVALUE_DUMP_TO_PRINTER, TRUE, &dwTmp))
|
||||
SetDumpToPrinter(dwTmp ? true : false);
|
||||
|
||||
if (RegLoadValue(regSection.c_str(), REGVALUE_CONVERT_ENCODING, TRUE, &dwTmp))
|
||||
SetConvertEncoding(dwTmp ? true : false);
|
||||
|
||||
if (RegLoadValue(regSection.c_str(), REGVALUE_FILTER_UNPRINTABLE, TRUE, &dwTmp))
|
||||
SetFilterUnprintable(dwTmp ? true : false);
|
||||
|
||||
if (RegLoadValue(regSection.c_str(), REGVALUE_PRINTER_APPEND, TRUE, &dwTmp))
|
||||
SetPrinterAppend(dwTmp ? true : false);
|
||||
|
||||
if (RegLoadString(regSection.c_str(), REGVALUE_PRINTER_FILENAME, 1, szFilename, MAX_PATH, TEXT("")))
|
||||
SetFilename(szFilename);
|
||||
|
||||
if (RegLoadValue(regSection.c_str(), REGVALUE_PRINTER_IDLE_LIMIT, TRUE, &dwTmp))
|
||||
SetIdleLimit(dwTmp);
|
||||
}
|
||||
|
||||
void ParallelPrinterCard::SetRegistryConfig(void)
|
||||
{
|
||||
std::string regSection = RegGetConfigSlotSection(m_slot);
|
||||
RegSaveValue(regSection.c_str(), REGVALUE_DUMP_TO_PRINTER, TRUE, GetDumpToPrinter() ? 1 : 0);
|
||||
RegSaveValue(regSection.c_str(), REGVALUE_CONVERT_ENCODING, TRUE, GetConvertEncoding() ? 1 : 0);
|
||||
RegSaveValue(regSection.c_str(), REGVALUE_FILTER_UNPRINTABLE, TRUE, GetFilterUnprintable() ? 1 : 0);
|
||||
RegSaveValue(regSection.c_str(), REGVALUE_PRINTER_APPEND, TRUE, GetPrinterAppend() ? 1 : 0);
|
||||
RegSaveString(regSection.c_str(), REGVALUE_PRINTER_FILENAME, TRUE, GetFilename());
|
||||
RegSaveValue(regSection.c_str(), REGVALUE_PRINTER_IDLE_LIMIT, TRUE, GetIdleLimit());
|
||||
}
|
||||
|
||||
//===========================================================================
|
||||
@@ -209,57 +236,57 @@ void Printer_SetIdleLimit(unsigned int Duration)
|
||||
#define SS_YAML_KEY_APPEND "Printer Append"
|
||||
#define SS_YAML_KEY_DUMPTOREALPRINTER "Enable Dump To Real Printer"
|
||||
|
||||
const std::string& Printer_GetSnapshotCardName(void)
|
||||
const std::string& ParallelPrinterCard::GetSnapshotCardName(void)
|
||||
{
|
||||
static const std::string name(SS_YAML_VALUE_CARD_PRINTER);
|
||||
return name;
|
||||
}
|
||||
|
||||
void Printer_SaveSnapshot(class YamlSaveHelper& yamlSaveHelper, const UINT uSlot)
|
||||
void ParallelPrinterCard::SaveSnapshot(class YamlSaveHelper& yamlSaveHelper)
|
||||
{
|
||||
YamlSaveHelper::Slot slot(yamlSaveHelper, Printer_GetSnapshotCardName(), uSlot, 1);
|
||||
YamlSaveHelper::Slot slot(yamlSaveHelper, ParallelPrinterCard::GetSnapshotCardName(), m_slot, 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);
|
||||
yamlSaveHelper.SaveUint(SS_YAML_KEY_INACTIVITY, m_inactivity);
|
||||
yamlSaveHelper.SaveUint(SS_YAML_KEY_IDLELIMIT, m_printerIdleLimit);
|
||||
yamlSaveHelper.SaveString(SS_YAML_KEY_FILENAME, m_szPrintFilename);
|
||||
yamlSaveHelper.SaveBool(SS_YAML_KEY_FILEOPEN, (m_file != NULL) ? true : false);
|
||||
yamlSaveHelper.SaveBool(SS_YAML_KEY_DUMPTOPRINTER, m_bDumpToPrinter);
|
||||
yamlSaveHelper.SaveBool(SS_YAML_KEY_CONVERTENCODING, m_bConvertEncoding);
|
||||
yamlSaveHelper.SaveBool(SS_YAML_KEY_FILTERUNPRINTABLE, m_bFilterUnprintable);
|
||||
yamlSaveHelper.SaveBool(SS_YAML_KEY_APPEND, m_bPrinterAppend);
|
||||
yamlSaveHelper.SaveBool(SS_YAML_KEY_DUMPTOREALPRINTER, m_bEnableDumpToRealPrinter);
|
||||
}
|
||||
|
||||
bool Printer_LoadSnapshot(class YamlLoadHelper& yamlLoadHelper, UINT slot, UINT version)
|
||||
bool ParallelPrinterCard::LoadSnapshot(class YamlLoadHelper& yamlLoadHelper, UINT version)
|
||||
{
|
||||
if (slot != 1) // fixme
|
||||
Card::ThrowErrorInvalidSlot(CT_GenericPrinter, slot);
|
||||
if (m_slot != SLOT1) // fixme
|
||||
Card::ThrowErrorInvalidSlot(CT_GenericPrinter, m_slot);
|
||||
|
||||
if (version != 1)
|
||||
Card::ThrowErrorInvalidVersion(CT_GenericPrinter, version);
|
||||
|
||||
inactivity = yamlLoadHelper.LoadUint(SS_YAML_KEY_INACTIVITY);
|
||||
g_PrinterIdleLimit = yamlLoadHelper.LoadUint(SS_YAML_KEY_IDLELIMIT);
|
||||
g_szPrintFilename = yamlLoadHelper.LoadString(SS_YAML_KEY_FILENAME);
|
||||
m_inactivity = yamlLoadHelper.LoadUint(SS_YAML_KEY_INACTIVITY);
|
||||
m_printerIdleLimit = yamlLoadHelper.LoadUint(SS_YAML_KEY_IDLELIMIT);
|
||||
m_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
|
||||
m_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);
|
||||
m_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);
|
||||
m_bDumpToPrinter = yamlLoadHelper.LoadBool(SS_YAML_KEY_DUMPTOPRINTER);
|
||||
m_bConvertEncoding = yamlLoadHelper.LoadBool(SS_YAML_KEY_CONVERTENCODING);
|
||||
m_bFilterUnprintable = yamlLoadHelper.LoadBool(SS_YAML_KEY_FILTERUNPRINTABLE);
|
||||
m_bEnableDumpToRealPrinter = yamlLoadHelper.LoadBool(SS_YAML_KEY_DUMPTOREALPRINTER);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user