Files
86Box/src/printer/prt_ps.c

414 lines
8.0 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.
*
* This file is part of the 86Box distribution.
*
* Implementation of a generic PostScript printer.
*
2019-12-07 20:29:51 +01:00
* Version: @(#)prt_ps.c 1.0.2 2019/12/08
2019-12-01 01:26:52 +01:00
*
* Authors: David Hrdlička, <hrdlickadavid@outlook.com>
*
* Copyright 2019 David Hrdlička.
*/
#include <inttypes.h>
#include <memory.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "../86box.h"
#include "../lang/language.h"
2019-12-01 01:26:52 +01:00
#include "../lpt.h"
#include "../timer.h"
#include "../pit.h"
#include "../plat.h"
2019-12-01 23:05:43 +01:00
#include "../plat_dynld.h"
#include "../ui.h"
2019-12-01 01:26:52 +01:00
#include "prt_devs.h"
2019-12-01 23:05:43 +01:00
#if defined(_WIN32) && !defined(__WINDOWS__)
#define __WINDOWS__
#endif
#include <ghostscript/iapi.h>
#include <ghostscript/ierrors.h>
2019-12-05 23:35:53 +01:00
#define PATH_GHOSTSCRIPT_DLL "gsdll32.dll"
#define PATH_GHOSTSCRIPT_SO "libgs.so"
#define POSTSCRIPT_BUFFER_LENGTH 65536
2019-12-01 23:05:43 +01:00
static GSDLLAPI int (*ghostscript_revision)(gsapi_revision_t *pr, int len);
static GSDLLAPI int (*ghostscript_new_instance)(void **pinstance, void *caller_handle);
static GSDLLAPI void (*ghostscript_delete_instance)(void *instance);
static GSDLLAPI int (*ghostscript_set_arg_encoding)(void *instance, int encoding);
static GSDLLAPI int (*ghostscript_init_with_args)(void *instance, int argc, char **argv);
static GSDLLAPI int (*ghostscript_exit)(void *instance);
static dllimp_t ghostscript_imports[] = {
{ "gsapi_revision", &ghostscript_revision },
{ "gsapi_new_instance", &ghostscript_new_instance },
{ "gsapi_delete_instance", &ghostscript_delete_instance },
{ "gsapi_set_arg_encoding", &ghostscript_set_arg_encoding },
{ "gsapi_init_with_args", &ghostscript_init_with_args },
{ "gsapi_exit", &ghostscript_exit },
{ NULL, NULL }
};
static void *ghostscript_handle = NULL;
static bool ghostscript_initialized = false;
2019-12-01 01:26:52 +01:00
typedef struct
{
const char *name;
void *lpt;
pc_timer_t pulse_timer;
pc_timer_t timeout_timer;
char data;
bool ack;
bool select;
bool busy;
bool int_pending;
bool error;
bool autofeed;
uint8_t ctrl;
wchar_t printer_path[260];
wchar_t filename[260];
2019-12-05 23:35:53 +01:00
char buffer[POSTSCRIPT_BUFFER_LENGTH];
2019-12-01 01:26:52 +01:00
uint16_t buffer_pos;
} ps_t;
static void
reset_ps(ps_t *dev)
{
2019-12-07 14:49:05 +01:00
if (dev == NULL) {
return;
}
2019-12-01 01:26:52 +01:00
dev->ack = false;
2019-12-01 23:05:43 +01: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) {
dev->ack = 0;
lpt_irq(dev->lpt, 1);
}
timer_disable(&dev->pulse_timer);
}
2019-12-01 23:05:43 +01:00
static int
convert_to_pdf(ps_t *dev)
{
volatile int code;
void *instance = NULL;
wchar_t input_fn[1024], output_fn[1024], *gsargv[9];
input_fn[0] = 0;
wcscat(input_fn, dev->printer_path);
wcscat(input_fn, dev->filename);
output_fn[0] = 0;
wcscat(output_fn, input_fn);
wcscpy(output_fn + wcslen(output_fn) - 3, L".pdf");
gsargv[0] = L"";
gsargv[1] = L"-dNOPAUSE";
gsargv[2] = L"-dBATCH";
gsargv[3] = L"-dSAFER";
gsargv[4] = L"-sDEVICE=pdfwrite";
gsargv[5] = L"-q";
gsargv[6] = L"-o";
gsargv[7] = output_fn;
gsargv[8] = input_fn;
code = ghostscript_new_instance(&instance, dev);
2019-12-07 14:49:05 +01:00
if (code < 0) {
2019-12-01 23:05:43 +01:00
return code;
2019-12-07 14:49:05 +01:00
}
2019-12-01 23:05:43 +01:00
code = ghostscript_set_arg_encoding(instance, GS_ARG_ENCODING_UTF16LE);
2019-12-07 14:49:05 +01:00
if (code == 0) {
code = ghostscript_init_with_args(instance, 9, (char **) gsargv);
2019-12-07 14:49:05 +01:00
}
2019-12-01 23:05:43 +01:00
2019-12-07 14:49:05 +01:00
if (code == 0 || code == gs_error_Quit) {
2019-12-01 23:05:43 +01:00
code = ghostscript_exit(instance);
2019-12-07 14:49:05 +01:00
} else {
2019-12-01 23:05:43 +01:00
ghostscript_exit(instance);
2019-12-07 14:49:05 +01:00
}
2019-12-01 23:05:43 +01:00
ghostscript_delete_instance(instance);
2019-12-07 14:49:05 +01:00
if (code == 0) {
2019-12-01 23:05:43 +01:00
plat_remove(input_fn);
2019-12-07 14:49:05 +01:00
} else {
2019-12-01 23:05:43 +01:00
plat_remove(output_fn);
2019-12-07 14:49:05 +01:00
}
2019-12-01 23:05:43 +01:00
return code;
}
2019-12-01 01:26:52 +01:00
static void
finish_document(ps_t *dev)
{
2019-12-07 14:49:05 +01:00
if (ghostscript_handle != NULL) {
2019-12-01 23:05:43 +01:00
convert_to_pdf(dev);
2019-12-07 14:49:05 +01:00
}
2019-12-01 01:26:52 +01:00
dev->filename[0] = 0;
}
static void
2019-12-07 14:49:05 +01:00
write_buffer(ps_t *dev, bool newline)
2019-12-01 01:26:52 +01:00
{
wchar_t path[1024];
FILE *fp;
2019-12-07 14:49:05 +01:00
if (dev->buffer[0] == 0) {
2019-12-05 23:35:53 +01:00
return;
2019-12-07 14:49:05 +01:00
}
2019-12-05 23:35:53 +01:00
2019-12-07 14:49:05 +01:00
if (dev->filename[0] == 0) {
2019-12-01 01:26:52 +01:00
plat_tempfile(dev->filename, NULL, L".ps");
2019-12-07 14:49:05 +01:00
}
2019-12-01 01:26:52 +01:00
path[0] = 0;
wcscat(path, dev->printer_path);
wcscat(path, dev->filename);
fp = plat_fopen(path, L"a");
2019-12-07 14:49:05 +01:00
if (fp == NULL) {
2019-12-01 01:26:52 +01:00
return;
2019-12-07 14:49:05 +01:00
}
2019-12-01 01:26:52 +01:00
fseek(fp, 0, SEEK_END);
2019-12-07 14:49:05 +01:00
fprintf(fp, "%.*s%s", POSTSCRIPT_BUFFER_LENGTH, dev->buffer, newline ? "\n" : "");
2019-12-01 01:26:52 +01:00
fclose(fp);
2019-12-04 19:52:49 +01:00
dev->buffer[0] = 0;
dev->buffer_pos = 0;
2019-12-01 01:26:52 +01:00
}
static void
timeout_timer(void *priv)
{
ps_t *dev = (ps_t *) priv;
2019-12-07 14:49:05 +01:00
write_buffer(dev, false);
2019-12-01 01:26:52 +01:00
finish_document(dev);
timer_disable(&dev->timeout_timer);
}
static void
ps_write_data(uint8_t val, void *p)
{
ps_t *dev = (ps_t *) p;
2019-12-07 14:49:05 +01:00
if (dev == NULL) {
return;
}
2019-12-01 01:26:52 +01:00
dev->data = (char) val;
}
2019-12-07 14:49:05 +01:00
static bool
process_nonprintable(ps_t *dev)
{
switch (dev->data) {
case '\b':
dev->buffer_pos--;
break;
case '\r':
dev->buffer_pos = 0;
if (dev->autofeed)
write_buffer(dev, true);
break;
2019-12-07 19:47:32 +01:00
case '\v':
case '\f':
2019-12-07 14:49:05 +01:00
case '\n':
write_buffer(dev, true);
break;
case 0x04: // Ctrl+D
write_buffer(dev, false);
finish_document(dev);
break;
/* Characters that should be written to the buffer as-is */
case '\t':
return false;
}
return true;
}
static void
process_data(ps_t *dev)
{
if (dev->data < 0x20 || dev->data == 0x7F) {
if (process_nonprintable(dev)) {
return;
}
}
if (dev->buffer_pos == POSTSCRIPT_BUFFER_LENGTH) {
write_buffer(dev, false);
}
dev->buffer[dev->buffer_pos++] = dev->data;
dev->buffer[dev->buffer_pos] = 0;
}
2019-12-01 01:26:52 +01:00
static void
ps_write_ctrl(uint8_t val, void *p)
{
ps_t *dev = (ps_t *) p;
2019-12-07 14:49:05 +01:00
if (dev == NULL) {
return;
}
2019-12-01 01:26:52 +01:00
dev->autofeed = val & 0x02 ? true : false;
2019-12-05 23:35:53 +01:00
if (val & 0x08) {
2019-12-01 01:26:52 +01:00
dev->select = true;
}
2019-12-07 14:49:05 +01:00
if ((val & 0x04) && !(dev->ctrl & 0x04)) {
2019-12-01 01:26:52 +01:00
// reset printer
dev->select = false;
reset_ps(dev);
}
2019-12-07 14:49:05 +01:00
if (!(val & 0x01) && (dev->ctrl & 0x01)) {
process_data(dev);
2019-12-01 01:26:52 +01:00
dev->ack = true;
timer_set_delay_u64(&dev->pulse_timer, ISACONST);
timer_set_delay_u64(&dev->timeout_timer, 500000 * TIMER_USEC);
}
dev->ctrl = val;
}
static uint8_t
ps_read_status(void *p)
{
ps_t *dev = (ps_t *) p;
uint8_t ret = 0x1f;
ret |= 0x80;
2019-12-07 14:49:05 +01:00
if (!dev->ack) {
2019-12-01 01:26:52 +01:00
ret |= 0x40;
2019-12-07 14:49:05 +01:00
}
2019-12-01 01:26:52 +01:00
return(ret);
}
2019-12-01 23:05:43 +01:00
static void
ghostscript_init()
{
gsapi_revision_t rev;
ghostscript_initialized = true;
/* Try loading the DLL. */
ghostscript_handle = dynld_module(PATH_GHOSTSCRIPT_DLL, ghostscript_imports);
if (ghostscript_handle == NULL) {
ui_msgbox(MBX_ERROR, (wchar_t *) IDS_2123);
2019-12-01 23:05:43 +01:00
return;
}
2019-12-05 23:35:53 +01:00
if (ghostscript_revision(&rev, sizeof(rev)) == 0) {
2019-12-01 23:05:43 +01:00
pclog("Loaded %s, rev %ld (%ld)\n", rev.product, rev.revision, rev.revisiondate);
2019-12-05 23:35:53 +01:00
} else {
2019-12-01 23:05:43 +01:00
ghostscript_handle = NULL;
}
}
2019-12-01 01:26:52 +01:00
static void *
ps_init(void *lpt)
{
ps_t *dev;
dev = (ps_t *) malloc(sizeof(ps_t));
memset(dev, 0x00, sizeof(ps_t));
dev->ctrl = 0x04;
dev->lpt = lpt;
reset_ps(dev);
2019-12-07 14:49:05 +01:00
if (!ghostscript_initialized) {
2019-12-01 23:05:43 +01:00
ghostscript_init();
2019-12-07 14:49:05 +01:00
}
2019-12-01 23:05:43 +01:00
2019-12-01 01:26:52 +01:00
// Cache print folder path
memset(dev->printer_path, 0x00, sizeof(dev->printer_path));
plat_append_filename(dev->printer_path, usr_path, L"printer");
2019-12-07 14:49:05 +01:00
if (!plat_dir_check(dev->printer_path)) {
2019-12-01 01:26:52 +01:00
plat_dir_create(dev->printer_path);
2019-12-07 14:49:05 +01:00
}
2019-12-01 01:26:52 +01:00
plat_path_slash(dev->printer_path);
timer_add(&dev->pulse_timer, pulse_timer, dev, 0);
timer_add(&dev->timeout_timer, timeout_timer, dev, 0);
return(dev);
}
static void
ps_close(void *p)
{
ps_t *dev = (ps_t *) p;
2019-12-07 14:49:05 +01:00
if (dev == NULL) {
return;
}
2019-12-01 01:26:52 +01:00
2019-12-05 23:35:53 +01:00
if (dev->buffer[0] != 0) {
2019-12-07 14:49:05 +01:00
write_buffer(dev, false);
2019-12-01 23:05:43 +01:00
finish_document(dev);
}
2019-12-01 01:26:52 +01:00
free(dev);
}
const lpt_device_t lpt_prt_ps_device = {
name: "Generic PostScript printer",
init: ps_init,
close: ps_close,
write_data: ps_write_data,
write_ctrl: ps_write_ctrl,
read_data: NULL,
read_status: ps_read_status,
read_ctrl: NULL
};