|
|
1.1 root 1: /*
1.1.1.5 root 2: Hatari - printer.c
1.1 root 3:
1.1.1.15 root 4: This file is distributed under the GNU General Public License, version 2
5: or at your option any later version. Read the file gpl.txt for details.
1.1.1.5 root 6:
1.1.1.6 root 7: Printer communication. When bytes are sent from the ST they are sent to these
8: functions via 'Printer_TransferByteTo()'. This will then open a file and
9: direct the output to this. These bytes are buffered up (to improve speed) and
10: this also allow us to detect when the stream goes into idle - at which point
11: we close the file/printer.
1.1.1.5 root 12: */
1.1.1.11 root 13: const char Printer_fileid[] = "Hatari printer.c : " __DATE__ " " __TIME__;
1.1.1.4 root 14:
1.1 root 15: #include "main.h"
1.1.1.9 root 16: #include "configuration.h"
1.1 root 17: #include "file.h"
1.1.1.9 root 18: #include "paths.h"
1.1 root 19: #include "printer.h"
1.1.1.14 root 20: #include "log.h"
1.1.1.3 root 21:
1.1.1.14 root 22: #define PRINTER_DEBUG 0
23: #if PRINTER_DEBUG
24: #define Dprintf(a) printf a
25: #else
26: #define Dprintf(a)
27: #endif
1.1 root 28:
1.1.1.14 root 29: /* After ~4 seconds (4*50 VBLs), flush & close printer */
30: #define PRINTER_IDLE_CLOSE (4*50)
1.1 root 31:
1.1.1.5 root 32: static int nIdleCount;
1.1.1.14 root 33: static int bUnflushed;
1.1 root 34:
1.1.1.9 root 35: static FILE *pPrinterHandle;
36:
37:
1.1.1.6 root 38: /*-----------------------------------------------------------------------*/
1.1.1.9 root 39: /**
40: * Initialise Printer
41: */
1.1 root 42: void Printer_Init(void)
43: {
1.1.1.14 root 44: char *separator;
45: Dprintf((stderr, "Printer_Init()\n"));
1.1.1.9 root 46:
1.1.1.14 root 47: /* disabled from config/command line? */
48: if (!ConfigureParams.Printer.szPrintToFileName[0])
49: return;
50:
51: /* printer name without path? */
52: separator = strrchr(ConfigureParams.Printer.szPrintToFileName, PATHSEP);
53: if (!separator)
54: return;
55:
56: *separator = '\0';
57: if (!File_DirExists(ConfigureParams.Printer.szPrintToFileName)) {
58: Log_AlertDlg(LOG_ERROR, "Printer output file directory inaccessible. Printing disabled.");
59: ConfigureParams.Printer.bEnablePrinting = false;
1.1.1.5 root 60: }
1.1.1.14 root 61: *separator = PATHSEP;
1.1.1.5 root 62:
1.1.1.14 root 63: Dprintf((stderr, "Filename for printing: %s \n", ConfigureParams.Printer.szPrintToFileName));
1.1 root 64: }
65:
1.1.1.2 root 66:
67: /*-----------------------------------------------------------------------*/
1.1.1.9 root 68: /**
69: * Uninitialise Printer
70: */
1.1 root 71: void Printer_UnInit(void)
72: {
1.1.1.14 root 73: Dprintf((stderr, "Printer_UnInit()\n"));
1.1 root 74:
1.1.1.6 root 75: /* Close any open files */
1.1.1.9 root 76: pPrinterHandle = File_Close(pPrinterHandle);
1.1.1.14 root 77: bUnflushed = false;
78: nIdleCount = 0;
1.1 root 79: }
80:
1.1.1.2 root 81:
1.1.1.6 root 82: /*-----------------------------------------------------------------------*/
1.1.1.9 root 83: /**
84: * Pass byte from emulator to printer. Opens the printer file appending
1.1.1.12 root 85: * if it isn't already open. Returns false if connection to "printer"
1.1.1.9 root 86: * failed
87: */
1.1.1.10 root 88: bool Printer_TransferByteTo(Uint8 Byte)
1.1 root 89: {
1.1.1.6 root 90: /* Do we want to output to a printer/file? */
91: if (!ConfigureParams.Printer.bEnablePrinting)
1.1.1.12 root 92: return false; /* Failed if printing disabled */
1.1.1.6 root 93:
94: /* Have we made a connection to our printer/file? */
1.1.1.14 root 95: if (!pPrinterHandle)
1.1.1.6 root 96: {
1.1.1.9 root 97: /* open printer file... */
1.1.1.16 root 98: pPrinterHandle = File_Open(ConfigureParams.Printer.szPrintToFileName, "a+b");
1.1.1.14 root 99: if (!pPrinterHandle)
100: {
101: Log_AlertDlg(LOG_ERROR, "Printer output file open failed. Printing disabled.");
102: ConfigureParams.Printer.bEnablePrinting = false;
103: return false;
104: }
1.1.1.6 root 105: }
1.1.1.14 root 106: if (fputc(Byte, pPrinterHandle) != Byte)
1.1.1.6 root 107: {
1.1.1.17! root 108: Log_Printf(LOG_ERROR, "Printer_TransferByteTo() writing failed!\n");
1.1.1.14 root 109: return false;
1.1.1.6 root 110: }
1.1.1.14 root 111: bUnflushed = true;
112: return true;
1.1 root 113: }
114:
1.1.1.2 root 115:
1.1.1.6 root 116: /*-----------------------------------------------------------------------*/
1.1.1.9 root 117: /**
118: * Empty printer buffer, and if remains idle for set time close connection
119: * (ie close file, stop printer)
120: */
1.1 root 121: void Printer_CheckIdleStatus(void)
122: {
1.1.1.6 root 123: /* Is anything waiting for printer? */
1.1.1.14 root 124: if (bUnflushed)
1.1.1.6 root 125: {
1.1.1.14 root 126: fflush(pPrinterHandle);
127: bUnflushed = false;
1.1.1.6 root 128: nIdleCount = 0;
129: }
130: else
131: {
132: nIdleCount++;
133: /* Has printer been idle? */
134: if (nIdleCount >= PRINTER_IDLE_CLOSE)
135: {
136: /* Close printer output */
1.1.1.14 root 137: Printer_UnInit();
1.1.1.6 root 138: }
139: }
1.1 root 140: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.