From cec6695ba5615076f326ce0cd4e5f21fe6341db1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Sloth=20T=C3=B8nnesen?= Date: Mon, 10 Jul 2023 21:07:23 +0000 Subject: ql: refactor full_write to take ctx Access to ctx is needed for the next patch --- src/ql.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/ql.c b/src/ql.c index 167f7a8..1023dbe 100644 --- a/src/ql.c +++ b/src/ql.c @@ -22,10 +22,11 @@ struct ql_ctx { #define STATUS_READ_TIMEOUT 5 /* seconds */ -#define full_write(fd, buf) (retry_write(fd, buf, sizeof(buf)) == (ssize_t)sizeof(buf)) +#define full_write(ctx, buf) (retry_write(ctx, buf, sizeof(buf)) == (ssize_t)sizeof(buf)) -ssize_t retry_write(int fd, const char *buf, size_t len) +ssize_t retry_write(ql_ctx_t ctx, const char *buf, size_t len) { + int fd = ctx->fd; size_t written = 0; int retval; @@ -77,7 +78,7 @@ ql_ctx_t ql_open(const char *printer) ctx->fd = fd; const char clear[200] = { 0, }; - (void)full_write(fd, clear); // recommended to clear old/errored jobs + (void)full_write(ctx, clear); // recommended to clear old/errored jobs return ctx; } @@ -92,13 +93,13 @@ void ql_close(ql_ctx_t ctx) bool ql_init(ql_ctx_t ctx) { const char init[] = { ESC, '@' }; - return full_write(ctx->fd, init); + return full_write(ctx, init); } bool ql_request_status(ql_ctx_t ctx) { const char status_req[] = { ESC, 'i', 'S' }; - return full_write(ctx->fd, status_req); + return full_write(ctx, status_req); } bool ql_read_status(ql_ctx_t ctx, ql_status_t * status) @@ -152,25 +153,25 @@ bool ql_read_status(ql_ctx_t ctx, ql_status_t * status) bool ql_set_mode(ql_ctx_t ctx, unsigned mode) { char cmd[] = { ESC, 'i', 'M', mode }; - return full_write(ctx->fd, cmd); + return full_write(ctx, cmd); } bool ql_set_expanded_mode(ql_ctx_t ctx, unsigned mode) { char cmd[] = { ESC, 'i', 'K', mode }; - return full_write(ctx->fd, cmd); + return full_write(ctx, cmd); } bool ql_set_autocut_every_n(ql_ctx_t ctx, uint8_t n) { char cmd[] = { ESC, 'i', 'A', n }; - return full_write(ctx->fd, cmd); + return full_write(ctx, cmd); } bool ql_set_margin(ql_ctx_t ctx, uint16_t dots) { char cmd[] = { ESC, 'i', 'd', dots & 0xff, dots >> 8 }; - return full_write(ctx->fd, cmd); + return full_write(ctx, cmd); } bool ql_needs_mode_switch(const ql_status_t * status) @@ -193,7 +194,7 @@ bool ql_switch_to_raster_mode(ql_ctx_t ctx) #define MODE_RASTER 1 #define MODE_P_TOUCH_TEMPLATE 3 char cmd[] = { ESC, 'i', 'a', MODE_RASTER }; - return full_write(ctx->fd, cmd); + return full_write(ctx, cmd); } static void pack_column(uint8_t * out, uint16_t bytes, uint16_t colno, @@ -230,7 +231,7 @@ bool ql_print_raster_image(ql_ctx_t ctx, const ql_status_t * status, img->width & 0xff, img->width >> 8, 0, 0, cfg->first_page ? 0 : 1, 0 }; - if (!full_write(ctx->fd, print_info)) + if (!full_write(ctx, print_info)) return false; for (unsigned w = 0; w < img->width; ++w) { @@ -239,12 +240,12 @@ 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->fd, block)) + if (!full_write(ctx, block)) return false; } char done[] = { 0x1a }; // print with feeding - return full_write(ctx->fd, done); + return full_write(ctx, done); } const char *ql_decode_model(const ql_status_t * status) -- cgit v1.2.1