summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAsbjørn Sloth Tønnesen <ast@2e8.dk>2023-07-10 21:07:23 +0000
committerAsbjørn Sloth Tønnesen <ast@2e8.dk>2023-07-10 21:19:16 +0000
commitcec6695ba5615076f326ce0cd4e5f21fe6341db1 (patch)
treedd682b99285dcb0f105b8b74597cba93b615c68b /src
parent5f79d6da760e025b6af757242f70f4910901e053 (diff)
downloadqlprint-cec6695ba5615076f326ce0cd4e5f21fe6341db1.tar.gz
qlprint-cec6695ba5615076f326ce0cd4e5f21fe6341db1.tar.xz
qlprint-cec6695ba5615076f326ce0cd4e5f21fe6341db1.zip
ql: refactor full_write to take ctx
Access to ctx is needed for the next patch
Diffstat (limited to 'src')
-rw-r--r--src/ql.c27
1 files 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)