Files
86Box/src/printer/prt_ps.c

541 lines
13 KiB
C
Raw Normal View History

2019-12-01 01:26:52 +01:00
/*
* 86Box A hypervisor and IBM PC system emulator that specializes in
* running old operating systems and software designed for IBM
* PC systems and compatibles from 1981 through fairly recent
* system designs based on the PCI bus.
2019-12-01 01:26:52 +01:00
*
* This file is part of the 86Box distribution.
2019-12-01 01:26:52 +01:00
*
2024-06-21 15:36:28 +06:00
* Implementation of a generic PostScript printer and a
* generic PCL 5e printer.
2019-12-01 01:26:52 +01:00
*
2020-03-25 00:46:02 +02:00
*
2019-12-01 01:26:52 +01:00
*
* Authors: David Hrdlička, <hrdlickadavid@outlook.com>
2024-06-21 15:36:28 +06:00
* Cacodemon345
2019-12-01 01:26:52 +01:00
*
* Copyright 2019 David Hrdlička.
2024-06-21 15:36:28 +06:00
* Copyright 2024 Cacodemon345.
2019-12-01 01:26:52 +01:00
*/
#include <inttypes.h>
#include <memory.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <86box/86box.h>
#include <86box/timer.h>
#include <86box/lpt.h>
#include <86box/pit.h>
2022-04-19 23:06:39 +02:00
#include <86box/path.h>
#include <86box/plat.h>
#include <86box/plat_dynld.h>
#include <86box/ui.h>
#include <86box/prt_devs.h>
2019-12-01 01:26:52 +01:00
#ifdef _WIN32
2022-09-18 17:17:25 -04:00
# define GSDLLAPI __stdcall
#else
2022-09-18 17:17:25 -04:00
# define GSDLLAPI
2019-12-01 23:05:43 +01:00
#endif
2022-09-18 17:17:25 -04:00
#define GS_ARG_ENCODING_UTF8 1
#define gs_error_Quit -101
2019-12-01 23:05:43 +01:00
2021-08-26 14:27:52 +06:00
#ifdef _WIN32
2022-09-18 17:17:25 -04:00
# if (!(defined __amd64__ || defined _M_X64 || defined __aarch64__ || defined _M_ARM64))
# define PATH_GHOSTSCRIPT_DLL "gsdll32.dll"
2024-06-21 15:36:28 +06:00
# define PATH_GHOSTPCL_DLL "gpcl6dll32.dll"
2022-09-18 17:17:25 -04:00
# else
# define PATH_GHOSTSCRIPT_DLL "gsdll64.dll"
2024-06-21 15:36:28 +06:00
# define PATH_GHOSTPCL_DLL "gpcl6dll64.dll"
2022-09-18 17:17:25 -04:00
# endif
2021-08-26 14:27:52 +06:00
#elif defined __APPLE__
2022-09-18 17:17:25 -04:00
# define PATH_GHOSTSCRIPT_DLL "libgs.dylib"
# define PATH_GHOSTPCL_DLL "libgpcl6.9.54.dylib"
2021-08-26 14:27:52 +06:00
#else
# define PATH_GHOSTSCRIPT_DLL "libgs.so.9"
# define PATH_GHOSTSCRIPT_DLL_ALT1 "libgs.so.10"
# define PATH_GHOSTSCRIPT_DLL_ALT2 "libgs.so"
# define PATH_GHOSTPCL_DLL "libgpcl6.so.9"
# define PATH_GHOSTPCL_DLL_ALT1 "libgpcl6.so.10"
# define PATH_GHOSTPCL_DLL_ALT2 "libgpcl6.so"
2021-08-26 14:27:52 +06:00
#endif
2019-12-05 23:35:53 +01:00
2022-09-18 17:17:25 -04:00
#define POSTSCRIPT_BUFFER_LENGTH 65536
2019-12-01 23:05:43 +01:00
2023-06-09 23:46:54 -04:00
typedef struct ps_t {
2022-09-18 17:17:25 -04:00
const char *name;
2019-12-01 01:26:52 +01:00
2022-09-18 17:17:25 -04:00
void *lpt;
2019-12-01 01:26:52 +01:00
2022-09-18 17:17:25 -04:00
pc_timer_t pulse_timer;
pc_timer_t timeout_timer;
2019-12-01 01:26:52 +01:00
2022-09-18 17:17:25 -04:00
char data;
bool ack;
bool select;
bool busy;
bool int_pending;
bool error;
bool autofeed;
2024-06-21 15:36:28 +06:00
bool pcl;
bool pcl_escape;
2022-09-18 17:17:25 -04:00
uint8_t ctrl;
2019-12-01 01:26:52 +01:00
2022-09-18 17:17:25 -04:00
char printer_path[260];
2019-12-01 01:26:52 +01:00
2022-09-18 17:17:25 -04:00
char filename[260];
2019-12-01 01:26:52 +01:00
2022-09-18 17:17:25 -04:00
char buffer[POSTSCRIPT_BUFFER_LENGTH];
size_t buffer_pos;
2019-12-01 01:26:52 +01:00
} ps_t;
2020-08-13 02:53:16 +02:00
typedef struct gsapi_revision_s {
const char *product;
const char *copyright;
2022-09-18 17:17:25 -04:00
long revision;
long revisiondate;
2020-08-13 02:53:16 +02:00
} gsapi_revision_t;
2022-09-18 17:17:25 -04:00
static int(GSDLLAPI *gsapi_revision)(gsapi_revision_t *pr, int len);
static int(GSDLLAPI *gsapi_new_instance)(void **pinstance, void *caller_handle);
static void(GSDLLAPI *gsapi_delete_instance)(void *instance);
static int(GSDLLAPI *gsapi_set_arg_encoding)(void *instance, int encoding);
static int(GSDLLAPI *gsapi_init_with_args)(void *instance, int argc, char **argv);
static int(GSDLLAPI *gsapi_exit)(void *instance);
2020-08-13 02:53:16 +02:00
static dllimp_t ghostscript_imports[] = {
2022-09-18 17:17:25 -04:00
// clang-format off
2022-02-26 23:31:28 -05:00
{ "gsapi_revision", &gsapi_revision },
{ "gsapi_new_instance", &gsapi_new_instance },
{ "gsapi_delete_instance", &gsapi_delete_instance },
{ "gsapi_set_arg_encoding", &gsapi_set_arg_encoding },
{ "gsapi_init_with_args", &gsapi_init_with_args },
{ "gsapi_exit", &gsapi_exit },
{ NULL, NULL }
2022-09-18 17:17:25 -04:00
// clang-format on
2020-08-13 02:53:16 +02:00
};
2022-09-18 17:17:25 -04:00
static void *ghostscript_handle = NULL;
2020-08-13 02:53:16 +02:00
2019-12-01 01:26:52 +01:00
static void
reset_ps(ps_t *dev)
{
2020-08-13 02:53:16 +02:00
if (dev == NULL)
2022-09-18 17:17:25 -04:00
return;
2019-12-01 01:26:52 +01:00
dev->ack = false;
2022-09-18 17:17:25 -04:00
dev->buffer[0] = 0;
2019-12-01 01:26:52 +01:00
dev->buffer_pos = 0;
timer_disable(&dev->pulse_timer);
timer_disable(&dev->timeout_timer);
}
static void
pulse_timer(void *priv)
{
ps_t *dev = (ps_t *) priv;
if (dev->ack) {
2022-09-18 17:17:25 -04:00
dev->ack = 0;
lpt_irq(dev->lpt, 1);
2019-12-01 01:26:52 +01:00
}
timer_disable(&dev->pulse_timer);
}
2019-12-01 23:05:43 +01:00
static int
convert_to_pdf(ps_t *dev)
{
2024-06-21 15:36:28 +06:00
volatile int code, arg = 0;
2022-09-18 17:17:25 -04:00
void *instance = NULL;
2023-05-16 15:43:20 -04:00
char input_fn[1024];
char output_fn[1024];
2024-06-21 15:36:28 +06:00
char *gsargv[11];
strcpy(input_fn, dev->printer_path);
2022-04-19 23:06:39 +02:00
path_slash(input_fn);
strcat(input_fn, dev->filename);
strcpy(output_fn, input_fn);
2024-06-21 15:36:28 +06:00
strcpy(output_fn + strlen(output_fn) - (dev->pcl ? 4 : 3), ".pdf");
gsargv[arg++] = "";
gsargv[arg++] = "-dNOPAUSE";
gsargv[arg++] = "-dBATCH";
gsargv[arg++] = "-dSAFER";
gsargv[arg++] = "-sDEVICE=pdfwrite";
if (dev->pcl) {
gsargv[arg++] = "-LPCL";
gsargv[arg++] = "-lPCL5E";
}
gsargv[arg++] = "-q";
gsargv[arg++] = "-o";
gsargv[arg++] = output_fn;
gsargv[arg++] = input_fn;
2019-12-01 23:05:43 +01:00
code = gsapi_new_instance(&instance, dev);
2020-08-13 02:53:16 +02:00
if (code < 0)
2022-09-18 17:17:25 -04:00
return code;
2019-12-01 23:05:43 +01:00
code = gsapi_set_arg_encoding(instance, GS_ARG_ENCODING_UTF8);
2019-12-01 23:05:43 +01:00
2020-08-13 02:53:16 +02:00
if (code == 0)
2024-06-21 15:36:28 +06:00
code = gsapi_init_with_args(instance, arg, gsargv);
2019-12-01 23:05:43 +01:00
2020-08-13 02:53:16 +02:00
if (code == 0 || code == gs_error_Quit)
2022-09-18 17:17:25 -04:00
code = gsapi_exit(instance);
2020-08-13 02:53:16 +02:00
else
2022-09-18 17:17:25 -04:00
gsapi_exit(instance);
2019-12-01 23:05:43 +01:00
gsapi_delete_instance(instance);
2019-12-01 23:05:43 +01:00
2020-08-13 02:53:16 +02:00
if (code == 0)
2022-09-18 17:17:25 -04:00
plat_remove(input_fn);
2020-08-13 02:53:16 +02:00
else
2022-09-18 17:17:25 -04:00
plat_remove(output_fn);
2019-12-01 23:05:43 +01:00
return code;
}
2019-12-01 01:26:52 +01:00
static void
2020-10-11 21:04:29 -03:00
write_buffer(ps_t *dev, bool finish)
2019-12-01 01:26:52 +01:00
{
2022-09-18 17:17:25 -04:00
char path[1024];
2019-12-01 01:26:52 +01:00
FILE *fp;
2020-08-13 02:53:16 +02:00
if (dev->buffer[0] == 0)
2022-09-18 17:17:25 -04:00
return;
2019-12-05 23:35:53 +01:00
2020-08-13 02:53:16 +02:00
if (dev->filename[0] == 0)
2024-06-21 15:36:28 +06:00
plat_tempfile(dev->filename, NULL, dev->pcl ? ".pcl" : ".ps");
2019-12-01 01:26:52 +01:00
strcpy(path, dev->printer_path);
2022-04-19 23:06:39 +02:00
path_slash(path);
strcat(path, dev->filename);
2019-12-01 01:26:52 +01:00
2024-06-21 15:36:28 +06:00
fp = plat_fopen(path, dev->pcl ? "ab" : "a");
2020-08-13 02:53:16 +02:00
if (fp == NULL)
2022-09-18 17:17:25 -04:00
return;
2019-12-01 01:26:52 +01:00
fseek(fp, 0, SEEK_END);
2024-06-21 15:36:28 +06:00
if (dev->pcl)
fwrite(dev->buffer, 1, dev->buffer_pos, fp);
else
fprintf(fp, "%.*s", POSTSCRIPT_BUFFER_LENGTH, dev->buffer);
2019-12-01 01:26:52 +01:00
fclose(fp);
2019-12-04 19:52:49 +01:00
2022-09-18 17:17:25 -04:00
dev->buffer[0] = 0;
2019-12-04 19:52:49 +01:00
dev->buffer_pos = 0;
2020-08-13 02:53:16 +02:00
2020-10-11 21:04:29 -03:00
if (finish) {
2022-09-18 17:17:25 -04:00
if (ghostscript_handle != NULL)
convert_to_pdf(dev);
2020-08-13 02:53:16 +02:00
2022-09-18 17:17:25 -04:00
dev->filename[0] = 0;
2020-10-11 21:04:29 -03:00
}
2019-12-01 01:26:52 +01:00
}
static void
timeout_timer(void *priv)
{
ps_t *dev = (ps_t *) priv;
2020-10-11 21:04:29 -03:00
write_buffer(dev, true);
2019-12-01 01:26:52 +01:00
timer_disable(&dev->timeout_timer);
}
static void
2023-06-28 13:46:28 -04:00
ps_write_data(uint8_t val, void *priv)
2019-12-01 01:26:52 +01:00
{
2023-06-28 13:46:28 -04:00
ps_t *dev = (ps_t *) priv;
2019-12-01 01:26:52 +01:00
2020-08-13 02:53:16 +02:00
if (dev == NULL)
2022-09-18 17:17:25 -04:00
return;
2019-12-01 01:26:52 +01:00
dev->data = (char) val;
}
static void
process_data(ps_t *dev)
2019-12-07 14:49:05 +01:00
{
2024-06-21 15:36:28 +06:00
/* On PCL, check for escape sequences. */
if (dev->pcl) {
if (dev->data == 0x1B)
dev->pcl_escape = true;
else if (dev->pcl_escape) {
dev->pcl_escape = false;
if (dev->data == 0xE) {
dev->buffer[dev->buffer_pos++] = dev->data;
dev->buffer[dev->buffer_pos] = 0;
if (dev->buffer_pos > 2)
write_buffer(dev, true);
return;
}
}
}
/* On PostScript, check for non-printable characters. */
else if ((dev->data < 0x20) || (dev->data == 0x7f)) {
2022-09-18 17:17:25 -04:00
switch (dev->data) {
/* The following characters are considered white-space
by the PostScript specification */
case '\t':
case '\n':
case '\f':
case '\r':
break;
/* Same with NUL, except we better change it to a space first */
case '\0':
dev->data = ' ';
break;
/* Ctrl+D (0x04) marks the end of the document */
case '\4':
write_buffer(dev, true);
return;
/* Don't bother with the others */
default:
return;
}
2019-12-07 14:49:05 +01:00
}
/* Flush the buffer if we have run to its end */
2020-08-13 02:53:16 +02:00
if (dev->buffer_pos == POSTSCRIPT_BUFFER_LENGTH - 1)
2022-09-18 17:17:25 -04:00
write_buffer(dev, false);
2019-12-07 14:49:05 +01:00
dev->buffer[dev->buffer_pos++] = dev->data;
2022-09-18 17:17:25 -04:00
dev->buffer[dev->buffer_pos] = 0;
2019-12-07 14:49:05 +01:00
}
static void
ps_autofeed(uint8_t val, void *priv)
{
ps_t *dev = (ps_t *) priv;
if (dev == NULL)
return;
dev->autofeed = val & 0x02 ? true : false;
}
static void
ps_strobe(uint8_t old, uint8_t val, void *priv)
{
ps_t *dev = (ps_t *) priv;
if (dev == NULL)
return;
if (!(val & 0x01) && (old & 0x01)) {
process_data(dev);
dev->ack = true;
timer_set_delay_u64(&dev->pulse_timer, ISACONST);
timer_set_delay_u64(&dev->timeout_timer, 5000000 * TIMER_USEC);
}
}
2019-12-01 01:26:52 +01:00
static void
2023-06-28 13:46:28 -04:00
ps_write_ctrl(uint8_t val, void *priv)
2019-12-01 01:26:52 +01:00
{
2023-06-28 13:46:28 -04:00
ps_t *dev = (ps_t *) priv;
2019-12-01 01:26:52 +01:00
2020-08-13 02:53:16 +02:00
if (dev == NULL)
2022-09-18 17:17:25 -04:00
return;
2019-12-01 01:26:52 +01:00
dev->autofeed = val & 0x02 ? true : false;
2020-08-13 02:53:16 +02:00
if (val & 0x08)
2022-09-18 17:17:25 -04:00
dev->select = true;
2019-12-01 01:26:52 +01:00
2019-12-07 14:49:05 +01:00
if ((val & 0x04) && !(dev->ctrl & 0x04)) {
2022-09-18 17:17:25 -04:00
/* Reset printer */
dev->select = false;
2019-12-01 01:26:52 +01:00
2022-09-18 17:17:25 -04:00
reset_ps(dev);
2019-12-01 01:26:52 +01:00
}
2019-12-07 14:49:05 +01:00
if (!(val & 0x01) && (dev->ctrl & 0x01)) {
2022-09-18 17:17:25 -04:00
process_data(dev);
2019-12-01 01:26:52 +01:00
2022-09-18 17:17:25 -04:00
dev->ack = true;
2019-12-01 01:26:52 +01:00
2022-09-18 17:17:25 -04:00
timer_set_delay_u64(&dev->pulse_timer, ISACONST);
timer_set_delay_u64(&dev->timeout_timer, 5000000 * TIMER_USEC);
2019-12-01 01:26:52 +01:00
}
dev->ctrl = val;
}
static uint8_t
2023-06-28 13:46:28 -04:00
ps_read_status(void *priv)
2019-12-01 01:26:52 +01:00
{
2023-07-20 18:58:26 -04:00
const ps_t *dev = (ps_t *) priv;
uint8_t ret = 0x9f;
2019-12-01 01:26:52 +01:00
2020-08-13 02:53:16 +02:00
if (!dev->ack)
2022-09-18 17:17:25 -04:00
ret |= 0x40;
2019-12-01 01:26:52 +01:00
2023-05-16 15:43:20 -04:00
return ret;
2019-12-01 01:26:52 +01:00
}
static void *
ps_init(void *lpt)
{
2025-01-07 00:42:06 -05:00
ps_t *dev = (ps_t *) calloc(1, sizeof(ps_t));
gsapi_revision_t rev;
2019-12-01 01:26:52 +01:00
dev->ctrl = 0x04;
2022-09-18 17:17:25 -04:00
dev->lpt = lpt;
2025-01-07 00:42:06 -05:00
dev->pcl = false;
2019-12-01 01:26:52 +01:00
/* Try loading the DLL. */
ghostscript_handle = dynld_module(PATH_GHOSTSCRIPT_DLL, ghostscript_imports);
#ifdef PATH_GHOSTSCRIPT_DLL_ALT1
if (ghostscript_handle == NULL) {
ghostscript_handle = dynld_module(PATH_GHOSTSCRIPT_DLL_ALT1, ghostscript_imports);
# ifdef PATH_GHOSTSCRIPT_DLL_ALT2
if (ghostscript_handle == NULL)
ghostscript_handle = dynld_module(PATH_GHOSTSCRIPT_DLL_ALT2, ghostscript_imports);
# endif
}
#endif
if (ghostscript_handle == NULL) {
ui_msgbox_header(MBX_ERROR, plat_get_string(STRING_GHOSTSCRIPT_ERROR_TITLE), plat_get_string(STRING_GHOSTSCRIPT_ERROR_DESC));
} else {
if (gsapi_revision(&rev, sizeof(rev)) == 0) {
2022-09-18 17:17:25 -04:00
pclog("Loaded %s, rev %ld (%ld)\n", rev.product, rev.revision, rev.revisiondate);
} else {
2022-09-18 17:17:25 -04:00
dynld_close(ghostscript_handle);
ghostscript_handle = NULL;
}
2019-12-07 14:49:05 +01:00
}
2019-12-01 23:05:43 +01:00
2022-11-19 08:49:04 -05:00
/* Cache print folder path. */
2019-12-01 01:26:52 +01:00
memset(dev->printer_path, 0x00, sizeof(dev->printer_path));
2022-04-19 23:06:39 +02:00
path_append_filename(dev->printer_path, usr_path, "printer");
2020-08-13 02:53:16 +02:00
if (!plat_dir_check(dev->printer_path))
2022-09-18 17:17:25 -04:00
plat_dir_create(dev->printer_path);
2022-04-19 23:06:39 +02:00
path_slash(dev->printer_path);
2019-12-01 01:26:52 +01:00
timer_add(&dev->pulse_timer, pulse_timer, dev, 0);
timer_add(&dev->timeout_timer, timeout_timer, dev, 0);
reset_ps(dev);
2023-05-16 15:43:20 -04:00
return dev;
2019-12-01 01:26:52 +01:00
}
#ifdef USE_PCL
2024-06-21 15:36:28 +06:00
static void *
pcl_init(void *lpt)
{
2025-01-07 00:42:06 -05:00
ps_t *dev = (ps_t *) calloc(1, sizeof(ps_t));
2024-06-21 15:36:28 +06:00
gsapi_revision_t rev;
dev->ctrl = 0x04;
dev->lpt = lpt;
dev->pcl = true;
/* Try loading the DLL. */
ghostscript_handle = dynld_module(PATH_GHOSTPCL_DLL, ghostscript_imports);
#ifdef PATH_GHOSTPCL_DLL_ALT1
2024-06-21 15:36:28 +06:00
if (ghostscript_handle == NULL) {
ghostscript_handle = dynld_module(PATH_GHOSTPCL_DLL_ALT1, ghostscript_imports);
# ifdef PATH_GHOSTPCL_DLL_ALT2
if (ghostscript_handle == NULL)
ghostscript_handle = dynld_module(PATH_GHOSTPCL_DLL_ALT2, ghostscript_imports);
# endif
2024-06-21 15:36:28 +06:00
}
#endif
if (ghostscript_handle == NULL) {
ui_msgbox_header(MBX_ERROR, plat_get_string(STRING_GHOSTPCL_ERROR_TITLE), plat_get_string(STRING_GHOSTPCL_ERROR_DESC));
} else {
if (gsapi_revision(&rev, sizeof(rev)) == 0) {
pclog("Loaded %s, rev %ld (%ld)\n", rev.product, rev.revision, rev.revisiondate);
} else {
dynld_close(ghostscript_handle);
ghostscript_handle = NULL;
}
}
/* Cache print folder path. */
memset(dev->printer_path, 0x00, sizeof(dev->printer_path));
path_append_filename(dev->printer_path, usr_path, "printer");
if (!plat_dir_check(dev->printer_path))
plat_dir_create(dev->printer_path);
path_slash(dev->printer_path);
timer_add(&dev->pulse_timer, pulse_timer, dev, 0);
timer_add(&dev->timeout_timer, timeout_timer, dev, 0);
reset_ps(dev);
return dev;
}
#endif
2024-06-21 15:36:28 +06:00
2019-12-01 01:26:52 +01:00
static void
2023-06-28 13:46:28 -04:00
ps_close(void *priv)
2019-12-01 01:26:52 +01:00
{
2023-06-28 13:46:28 -04:00
ps_t *dev = (ps_t *) priv;
2019-12-01 01:26:52 +01:00
2020-08-13 02:53:16 +02:00
if (dev == NULL)
2022-09-18 17:17:25 -04:00
return;
2019-12-01 01:26:52 +01:00
2020-08-13 02:53:16 +02:00
if (dev->buffer[0] != 0)
2022-09-18 17:17:25 -04:00
write_buffer(dev, true);
2019-12-01 01:26:52 +01:00
if (ghostscript_handle != NULL) {
2022-09-18 17:17:25 -04:00
dynld_close(ghostscript_handle);
ghostscript_handle = NULL;
}
2019-12-01 01:26:52 +01:00
free(dev);
}
const lpt_device_t lpt_prt_ps_device = {
.name = "Generic PostScript Printer",
.internal_name = "postscript",
.init = ps_init,
.close = ps_close,
.write_data = ps_write_data,
.write_ctrl = ps_write_ctrl,
.autofeed = ps_autofeed,
.strobe = ps_strobe,
.read_status = ps_read_status,
.read_ctrl = NULL,
.epp_write_data = NULL,
.epp_request_read = NULL
2020-08-13 02:53:16 +02:00
};
2024-06-21 15:36:28 +06:00
#ifdef USE_PCL
2024-06-21 15:36:28 +06:00
const lpt_device_t lpt_prt_pcl_device = {
.name = "Generic PCL5e Printer",
.internal_name = "pcl",
.init = pcl_init,
.close = ps_close,
.write_data = ps_write_data,
.write_ctrl = ps_write_ctrl,
.autofeed = ps_autofeed,
.strobe = ps_strobe,
.read_status = ps_read_status,
.read_ctrl = NULL,
.epp_write_data = NULL,
.epp_request_read = NULL
2024-06-21 15:36:28 +06:00
};
#endif