Header | cups/backend.h cups/sidechannel.h |
---|---|
Library | -lcups |
See Also | Programming: Introduction to CUPS Programming Programming: CUPS API Programming: PPD API Programming: Raster API |
Filters, printer drivers, port monitors, and backends use a common interface for processing print jobs and communicating status information to the scheduler. Each filter is run with a standard set of command-line arguments:
The scheduler runs one or more of these programs to print any given job. The first filter reads from the print file and writes to the standard output, while the remaining filters read from the standard input and write to the standard output. The backend is the last filter in the chain and writes to the device.
Filters must exit with status 0 when they successfully generate print data
or 1 when they encounter an error. Backends can return any of the
cups_backend_t
constants.
The following environment variables are defined by the printing system:
Filters and backends communicate wih the scheduler by writing messages to the standard error file. For example, the following code sets the current printer state message to "Printing page 5":
int page = 5; fprintf(stderr, "INFO: Printing page %d\n", page);
Each message is a single line of text starting with one of the following prefix strings:
marker-colors
, marker-levels
,
marker-names
, marker-types
,
printer-alert
, and printer-alert-description
printer attributes.Messages without one of these prefixes are treated as if they began with the "DEBUG:" prefix string.
Filters can communicate with the backend via the
cupsBackChannelRead
and
cupsSideChannelDoRequest
functions. The
cupsBackChannelRead
function
reads data that has been sent back from the device and is typically used to
obtain status and configuration information. For example, the following code
polls the backend for back-channel data:
#include <cups/cups.h> char buffer[8192]; ssize_t bytes; /* Use a timeout of 0.0 seconds to poll for back-channel data */ bytes = cupsBackChannelRead(buffer, sizeof(buffer), 0.0);The
cupsSideChannelDoRequest
function allows you to get out-of-band status information and do synchronization
with the device. For example, the following code gets the current IEEE-1284
device ID string from the backend:
#include <cups/sidechannel.h> char data[2049]; int datalen; cups_sc_status_t status; /* Tell cupsSideChannelDoRequest() how big our buffer is, less 1 byte for nul-termination... */ datalen = sizeof(data) - 1; /* Get the IEEE-1284 device ID, waiting for up to 1 second */ status = cupsSideChannelDoRequest(CUPS_SC_CMD_GET_DEVICE_ID, data, &datalen, 1.0); /* Use the returned value if OK was returned and the length is non-zero */ if (status == CUPS_SC_STATUS_OK && datalen > 0) data[datalen] = '\0'; else data[0] = '\0';
Backends communicate with filters using the reciprocal functions
cupsBackChannelWrite
,
cupsSideChannelRead
, and
cupsSideChannelWrite
. We
recommend writing back-channel data using a timeout of 1.0 seconds:
#include <cups/cups.h> char buffer[8192]; ssize_t bytes; /* Use a timeout of 1.0 seconds to give filters a chance to read */ cupsBackChannelWrite(buffer, bytes, 1.0);
The cupsSideChannelRead
function reads a side-channel command from a filter, driver, or port monitor.
Backends can either poll for commands using a timeout
of 0.0, wait
indefinitely for commands using a timeout
of -1.0 (probably in a
separate thread for that purpose), or use select
or
poll
on the CUPS_SC_FD
file descriptor (4) to handle
input and output on several file descriptors at the same time. Backends can pass
NULL
for the data
and datalen
parameters
since none of the commands sent by upstream filters contain any data at this
time.
Once a command is processed, the backend uses the
cupsSideChannelWrite
function
to send its response. For example, the following code shows how to poll for a
side-channel command and respond to it:
#include <cups/sidechannel.h> cups_sc_command_t command; cups_sc_status_t status; /* Poll for a command... */ if (!cupsSideChannelRead(&command, &status, NULL, NULL, 0.0)) { char data[2048]; int datalen; switch (command) { /* handle supported commands, file data/datalen/status with values as needed */ default : status = CUPS_SC_STATUS_NOT_IMPLEMENTED; datalen = 0; break; } /* Send a response... */ cupsSideChannelWrite(command, status, data, datalen, 1.0); }
Read data from the backchannel.
ssize_t cupsBackChannelRead (
char *buffer,
size_t bytes,
double timeout
);
Bytes read or -1 on error
Reads up to "bytes" bytes from the backchannel. The "timeout" parameter controls how many seconds to wait for the data - use 0.0 to return immediately if there is no data, -1.0 to wait for data indefinitely.
Write data to the backchannel.
ssize_t cupsBackChannelWrite (
const char *buffer,
size_t bytes,
double timeout
);
Bytes written or -1 on error
Writes "bytes" bytes to the backchannel. The "timeout" parameter controls how many seconds to wait for the data to be written - use 0.0 to return immediately if the data cannot be written, -1.0 to wait indefinitely.
Send a side-channel command to a backend and wait for a response.
cups_sc_status_t cupsSideChannelDoRequest (
cups_sc_command_t command,
char *data,
int *datalen,
double timeout
);
Status of command
This function is normally only called by filters, drivers, or port
monitors in order to communicate with the backend used by the current
printer. Programs must be prepared to handle timeout or "not
implemented" status codes, which indicate that the backend or device
do not support the specified side-channel command.
The "datalen" parameter must be initialized to the size of the buffer
pointed to by the "data" parameter. cupsSideChannelDoRequest() will
update the value to contain the number of data bytes in the buffer.
Read a side-channel message.
int cupsSideChannelRead (
cups_sc_command_t *command,
cups_sc_status_t *status,
char *data,
int *datalen,
double timeout
);
0 on success, -1 on error
This function is normally only called by backend programs to read
commands from a filter, driver, or port monitor program. The
caller must be prepared to handle incomplete or invalid messages
and return the corresponding status codes.
The "datalen" parameter must be initialized to the size of the buffer
pointed to by the "data" parameter. cupsSideChannelDoRequest() will
update the value to contain the number of data bytes in the buffer.
Write a side-channel message.
int cupsSideChannelWrite (
cups_sc_command_t command,
cups_sc_status_t status,
const char *data,
int datalen,
double timeout
);
0 on success, -1 on error
This function is normally only called by backend programs to send responses to a filter, driver, or port monitor program.
Backend exit codes
typedef enum cups_backend_e cups_backend_t;
Bidirectional capabilities
typedef enum cups_sc_bidi_e cups_sc_bidi_t;
Request command codes
typedef enum cups_sc_command_e cups_sc_command_t;
Printer state bits
typedef enum cups_sc_state_e cups_sc_state_t;
Response status codes
typedef enum cups_sc_status_e cups_sc_status_t;
Backend exit codes
Bidirectional capabilities
Request command codes
Printer state bits
Response status codes