diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ql.c | 32 |
1 files changed, 29 insertions, 3 deletions
@@ -13,21 +13,29 @@ #include <string.h> #include <time.h> +#define QL_XMIT_SIZE 4096 +#define QL_BUF_SIZE (QL_XMIT_SIZE * 2) + struct ql_ctx { char *printer; int fd; + ssize_t pos; + char buf[QL_BUF_SIZE]; }; #define ESC 0x1b #define STATUS_READ_TIMEOUT 5 /* seconds */ -#define full_write(ctx, buf) (retry_write(ctx, buf, sizeof(buf)) == (ssize_t)sizeof(buf)) +#define partial_write(ctx, buf) (do_write(ctx, buf, sizeof(buf), 0) == (ssize_t)sizeof(buf)) +#define full_write(ctx, buf) (do_write(ctx, buf, sizeof(buf), 1) == (ssize_t)sizeof(buf)) -ssize_t retry_write(ql_ctx_t ctx, const char *buf, size_t len) +ssize_t retry_write(ql_ctx_t ctx) { int fd = ctx->fd; size_t written = 0; + size_t len = ctx->pos; + char *buf = (char *)&ctx->buf; int retval; fd_set wfds; @@ -54,9 +62,27 @@ ssize_t retry_write(ql_ctx_t ctx, const char *buf, size_t len) written += n; } } + if (written == len) { + ctx->pos = 0; + } return written; } +static ssize_t do_write(ql_ctx_t ctx, const char *buf, ssize_t len, int flush_buffer) +{ + if (ctx->pos + len >= QL_BUF_SIZE) + return 0; + + memcpy(ctx->buf + ctx->pos, buf, len); + ctx->pos += len; + + ssize_t pos = ctx->pos; + if (flush_buffer || pos > QL_XMIT_SIZE) { + return retry_write(ctx) == pos ? len : 0; + } + return len; +} + static void get_mono_time(struct timespec *tp) { if (clock_gettime(CLOCK_MONOTONIC, tp) == -1) { @@ -240,7 +266,7 @@ bool ql_print_raster_image(ql_ctx_t ctx, const ql_status_t * status, block[1] = 0; block[2] = dn; pack_column((uint8_t *) block + 3, dn, w, img, cfg->threshold); - if (!full_write(ctx, block)) + if (!partial_write(ctx, block)) return false; } |