Re: [hatari-devel] Problem with printer output?

[ Thread Index | Date Index | More lists.tuxfamily.org/hatari-devel Archives ]


Hi,

On lauantai 04 helmikuu 2012, Matthias Arndt wrote:
> The printer.c file is probably still of Winston heritage. I didn'T touch
> it back then, only replaced some few lines to make it Unix and libc
> compatible. Feel free to change anything to make it more compatible and
> sane.

Either there shouldn't be buffering at all, like with midi.c,
or if it should be buffered, I would prefer relying on C-lib
and removing the manual buffered.  See the attached (non-tested)
patch.

I'm not sure what's the point in closing the printer file if
there hasn't been any printing for 4s.  Does anybody know why
it does that?


> > In TOS v2, running MINIMAL.PRG didn't result in any
> > Printer_TransferByteTo() calls, but "Print screen" did.
> > 
> > On the listed working TOS versions, MINIMAL.PRG running results
> > in Printer_TransferByteTo() calls though.
> > 
> > When I printed in psg.c, the PORTA value, during the printing...
> >
> >[...]
> >
> > I.e. the bits 0x4 and 0x2 are being toggled, although the code checks
> > for bit 0x20 being toggled:
....
> > [1] TOS Print screen gives same PORTA output as I get for Fwrites
> > to PRN: in the working TOS versions.
> > 
> > Any idea what's wrong?
> 
> Not really. Back then I simply asume as soon as the STROBE is set, a
> byte needs to be send to the printer. If that is wrong, ofocurse the
> whole thing was whacky from the first commit. I never claimed it was a
> perfect 100% solution, just a hack, that worked for the time being.

Does somebody have a real Atari machine with TOS v1.02 - v2.06
*and* a printer, and if yes, could you try whether booting with
the attached floppy disk contents[1] produces any printer output?

[1] which just has minimal.prg + small test text in auto/ folder.


	- Eero

Attachment: bootauto.st.gz
Description: GNU Zip compressed data

diff -r e9fa6df86f6a src/change.c
--- a/src/change.c	Sun Feb 05 20:36:10 2012 +0100
+++ b/src/change.c	Wed Feb 08 23:54:57 2012 +0200
@@ -152,6 +152,7 @@
 	bool bReInitIoMem = false;
 	bool bScreenModeChange = false;
 	bool bReInitMidi = false;
+	bool bReInitPrinter = false;
 	bool bFloppyInsert[MAX_FLOPPYDRIVES];
 	int i;
 
@@ -182,7 +183,8 @@
 	    || strcmp(changed->Printer.szPrintToFileName,current->Printer.szPrintToFileName))
 	{
 		Dprintf("- printer>\n");
-		Printer_CloseAllConnections();
+		Printer_UnInit();
+		bReInitPrinter = true;
 	}
 
 	/* Did set new RS232 parameters? */
@@ -373,6 +375,13 @@
 		IoMem_Init();
 	}
 
+	/* Re-init Printer emulation? */
+	if (bReInitPrinter)
+	{
+		Dprintf("- printer<\n");
+		Printer_Init();
+	}
+
 	/* Re-init MIDI emulation? */
 	if (bReInitMidi)
 	{
diff -r e9fa6df86f6a src/includes/printer.h
--- a/src/includes/printer.h	Sun Feb 05 20:36:10 2012 +0100
+++ b/src/includes/printer.h	Wed Feb 08 23:54:57 2012 +0200
@@ -6,7 +6,6 @@
 
 extern void Printer_Init(void);
 extern void Printer_UnInit(void);
-extern void Printer_CloseAllConnections(void);
 extern bool Printer_TransferByteTo(Uint8 Byte);
 extern void Printer_CheckIdleStatus(void);
 
diff -r e9fa6df86f6a src/printer.c
--- a/src/printer.c	Sun Feb 05 20:36:10 2012 +0100
+++ b/src/printer.c	Wed Feb 08 23:54:57 2012 +0200
@@ -17,62 +17,50 @@
 #include "file.h"
 #include "paths.h"
 #include "printer.h"
+#include "log.h"
 
-/* #define PRINTER_DEBUG */
+#define PRINTER_DEBUG 0
+#if PRINTER_DEBUG
+#define Dprintf(a) printf a
+#else
+#define Dprintf(a)
+#endif
 
-#define PRINTER_FILENAME "hatari.prn"
+/* After ~4 seconds (4*50 VBLs), flush & close printer */
+#define PRINTER_IDLE_CLOSE   (4*50)
 
-#define PRINTER_IDLE_CLOSE   (4*50)     /* After 4 seconds, close printer */
-
-#define PRINTER_BUFFER_SIZE  2048       /* 2k buffer which when full will be written to printer/file */
-
-static Uint8 PrinterBuffer[PRINTER_BUFFER_SIZE];   /* Buffer to store character before output */
-static size_t nPrinterBufferChars;      /* # characters in above buffer */
-static bool bConnectedPrinter;
 static int nIdleCount;
+static int bUnflushed;
 
 static FILE *pPrinterHandle;
 
 
-/* internal functions */
-static void Printer_ResetInternalBuffer(void);
-static bool Printer_EmptyInternalBuffer(void);
-static void Printer_AddByteToInternalBuffer(Uint8 Byte);
-
-
 /*-----------------------------------------------------------------------*/
 /**
  * Initialise Printer
  */
 void Printer_Init(void)
 {
-#ifdef PRINTER_DEBUG
-	fprintf(stderr,"Printer_Init()\n");
-#endif
+	char *separator;
+	Dprintf((stderr, "Printer_Init()\n"));
 
-	/* A valid file name for printing is already set up in configuration.c.
-	 * But we check it again since the user might have entered an invalid
-	 * file name in the hatari.cfg file... */
-	if (strlen(ConfigureParams.Printer.szPrintToFileName) <= 1)
-	{
-		const char *psHomeDir;
-		psHomeDir = Paths_GetHatariHome();
+	/* disabled from config/command line? */
+	if (!ConfigureParams.Printer.szPrintToFileName[0])
+		return;
 
-		/* construct filename for printing.... */
-		if (strlen(psHomeDir)+1+strlen(PRINTER_FILENAME) < sizeof(ConfigureParams.Printer.szPrintToFileName))
-		{
-			sprintf(ConfigureParams.Printer.szPrintToFileName, "%s%c%s",
-			        psHomeDir, PATHSEP, PRINTER_FILENAME);
-		}
-		else
-		{
-			strcpy(ConfigureParams.Printer.szPrintToFileName, PRINTER_FILENAME);
-		}
+	/* printer name without path? */
+	separator = strrchr(ConfigureParams.Printer.szPrintToFileName, PATHSEP);
+	if (!separator)
+		return;
+	
+	*separator = '\0';
+	if (!File_DirExists(ConfigureParams.Printer.szPrintToFileName)) {
+		Log_AlertDlg(LOG_ERROR, "Printer output file directory inaccessible. Printing disabled.");
+		ConfigureParams.Printer.bEnablePrinting = false;
 	}
+	*separator = PATHSEP;
 
-#ifdef PRINTER_DEBUG
-	fprintf(stderr,"Filename for printing: %s \n", ConfigureParams.Printer.szPrintToFileName);
-#endif
+	Dprintf((stderr, "Filename for printing: %s \n", ConfigureParams.Printer.szPrintToFileName));
 }
 
 
@@ -82,86 +70,12 @@
  */
 void Printer_UnInit(void)
 {
-	/* Close any open files */
-	Printer_CloseAllConnections();
-
-#ifdef PRINTER_DEBUG
-	fprintf(stderr,"Printer_UnInit()\n");
-#endif
-}
-
-
-/*-----------------------------------------------------------------------*/
-/**
- * Close all open output file, empty buffers etc.
- */
-void Printer_CloseAllConnections(void)
-{
-	/* Empty buffer */
-	Printer_EmptyInternalBuffer();
+	Dprintf((stderr, "Printer_UnInit()\n"));
 
 	/* Close any open files */
 	pPrinterHandle = File_Close(pPrinterHandle);
-
-	/* Signal finished with printing */
-	bConnectedPrinter = false;
-}
-
-
-/*-----------------------------------------------------------------------*/
-/**
- * Reset Printer Buffer
- */
-static void Printer_ResetInternalBuffer(void)
-{
-	nPrinterBufferChars = 0;
-}
-
-
-/*-----------------------------------------------------------------------*/
-/**
- * Empty Printer Buffer
- */
-static bool Printer_EmptyInternalBuffer(void)
-{
-	/* Write bytes to file */
-	if (nPrinterBufferChars > 0)
-	{
-		if (pPrinterHandle)
-		{
-			size_t n;
-			/* Write bytes out */
-			n = fwrite((Uint8 *)PrinterBuffer, sizeof(Uint8),
-			           nPrinterBufferChars, pPrinterHandle);
-			if (n < nPrinterBufferChars)
-			{
-				/* we wrote less then expected! */
-				fprintf(stderr, "Printer_EmptyInternalBuffer():"
-					"ERROR not all chars were written\n");
-			}
-		}
-
-		/* Reset */
-		Printer_ResetInternalBuffer();
-
-		return true;
-	}
-	/* Nothing to do */
-	return false;
-}
-
-
-/*-----------------------------------------------------------------------*/
-/**
- * Add byte to our internal buffer, and when full write out - needed to speed
- */
-static void Printer_AddByteToInternalBuffer(Uint8 Byte)
-{
-	/* Is buffer full? If so empty */
-	if (nPrinterBufferChars == PRINTER_BUFFER_SIZE)
-		Printer_EmptyInternalBuffer();
-	/* Add character */
-	PrinterBuffer[nPrinterBufferChars++] = Byte;
+	bUnflushed = false;
+	nIdleCount = 0;
 }
 
 
@@ -178,25 +92,24 @@
 		return false;   /* Failed if printing disabled */
 
 	/* Have we made a connection to our printer/file? */
-	if (!bConnectedPrinter)
+	if (!pPrinterHandle)
 	{
 		/* open printer file... */
 		pPrinterHandle = File_Open(ConfigureParams.Printer.szPrintToFileName, "a+");
-		bConnectedPrinter = (pPrinterHandle != NULL);
-
-		/* Reset the printer */
-		Printer_ResetInternalBuffer();
+		if (!pPrinterHandle)
+		{
+			Log_AlertDlg(LOG_ERROR, "Printer output file open failed. Printing disabled.");
+			ConfigureParams.Printer.bEnablePrinting = false;
+			return false;
+		}
 	}
-
-	/* Is all OK? */
-	if (bConnectedPrinter)
+	if (fputc(Byte, pPrinterHandle) != Byte)
 	{
-		/* Add byte to our buffer. */
-		Printer_AddByteToInternalBuffer(Byte);
-		return true;    /* OK */
+		fprintf(stderr, "ERROR: Printer_TransferByteTo() writing failed!\n");
+		return false;
 	}
-	else
-		return false;   /* Failed */
+	bUnflushed = true;
+	return true;
 }
 
 
@@ -208,8 +121,10 @@
 void Printer_CheckIdleStatus(void)
 {
 	/* Is anything waiting for printer? */
-	if (Printer_EmptyInternalBuffer())
+	if (bUnflushed)
 	{
+		fflush(pPrinterHandle);
+		bUnflushed = false;
 		nIdleCount = 0;
 	}
 	else
@@ -219,8 +134,7 @@
 		if (nIdleCount >= PRINTER_IDLE_CLOSE)
 		{
 			/* Close printer output */
-			Printer_CloseAllConnections();
-			nIdleCount = 0;
+			Printer_UnInit();
 		}
 	}
 }


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/