-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
lprint-brother.c
568 lines (455 loc) · 17.8 KB
/
lprint-brother.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//
// Experimental Brother driver for LPrint, a Label Printer Application
//
// Copyright © 2023-2024 by Michael R Sweet.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//
#include "lprint.h"
#ifdef LPRINT_EXPERIMENTAL
//
// Local types...
//
typedef struct lprint_brother_s // Brother driver data
{
bool is_pt_series; // Is this a PT-series printer?
bool is_ql_800; // Is this the QL-800 printer?
lprint_dither_t dither; // Dither buffer
int count; // Output count for print info
size_t alloc_bytes, // Allocated bytes for output buffer
num_bytes; // Number of bytes in output buffer
unsigned char *buffer; // Output buffer
} lprint_brother_t;
//
// Local globals...
//
static const char * const lprint_brother_ql_media[] =
{ // Supported QL-* media sizes
"oe_dk1219-round_0.47x0.47in",
"oe_dk1204-multi-purpose_0.66x2.1in",
"oe_dk1203-file-folder_0.66x3.4in",
"oe_dk1209-small-address_1.1x2.4in",
"oe_dk1201-address_1.1x3.5in",
"oe_dk1208-large-address_1.4x3.5in",
"oe_dk1240-large-multi-purpose_1.9x4in",
"oe_dk1207-cd-dvd_2.2x2.2in",
"oe_dk1202-shipping_2.4x3.9in",
"na_index-4x6_4x6in", // DK1241/1247
"roll_dk2113-continuous-film_2.4x600in", // Black/Clear
"roll_dk2205-continuous_2.4x1200in", // Black on White
"roll_dk2210-continuous_1.1x1200in",
"roll_dk2211-continuous-film_1.1x600in",
"roll_dk2212-continuous-film_2.4x600in",
"roll_dk2214-continuous_0.47x1200in",
"roll_dk2243-continuous_4x1200in", // Black on White
"roll_dk2246-continuous_4.07x1200in", // Black on White
"roll_dk2251-continuous_2.4x600in", // Black/Red on White
"roll_dk2606-continuous-film_2.4x600in", // Black/Yellow
"roll_dk4205-continuous-removable_2.4x1200in",// Black on White
"roll_dk4605-continuous-removable_2.4x1200in",// Black/Yellow on White
"roll_max_2.5x3600in",
"roll_min_0.25x1in"
};
static const char * const lprint_brother_pt_media[] =
{ // Supported PT-* media sizes
"oe_thin-1in-tape_0.25x1in",
"oe_thinner-1in-tape_0.375x1in",
"oe_medium-1in-tape_0.5x1in",
"oe_wider-1in-tape_0.75x1in",
"oe_wide-1in-tape_1x1in",
"oe_thin-2in-tape_0.25x2in",
"oe_thinner-2in-tape_0.375x2in",
"oe_medium-2in-tape_0.5x2in",
"oe_wider-2in-tape_0.75x2in",
"oe_wide-2in-tape_1x2in",
"oe_thin-3in-tape_0.25x3in",
"oe_thinner-3in-tape_0.375x3in",
"oe_medium-3in-tape_0.5x3in",
"oe_wider-3in-tape_0.75x3in",
"oe_wide-3in-tape_1x3in",
"roll_max_1x3600in",
"roll_min_0.25x1in"
};
//
// Local functions...
//
static bool lprint_brother_get_status(pappl_printer_t *printer, pappl_device_t *device);
static bool lprint_brother_printfile(pappl_job_t *job, pappl_pr_options_t *options, pappl_device_t *device);
static bool lprint_brother_rendjob(pappl_job_t *job, pappl_pr_options_t *options, pappl_device_t *device);
static bool lprint_brother_rendpage(pappl_job_t *job, pappl_pr_options_t *options, pappl_device_t *device, unsigned page);
static bool lprint_brother_rstartjob(pappl_job_t *job, pappl_pr_options_t *options, pappl_device_t *device);
static bool lprint_brother_rstartpage(pappl_job_t *job, pappl_pr_options_t *options, pappl_device_t *device, unsigned page);
static bool lprint_brother_rwriteline(pappl_job_t *job, pappl_pr_options_t *options, pappl_device_t *device, unsigned y, const unsigned char *line);
static bool lprint_brother_status(pappl_printer_t *printer);
//
// 'lprintBrother()' - Initialize the Brother driver.
//
bool // O - `true` on success, `false` on error
lprintBrother(
pappl_system_t *system, // I - System
const char *driver_name,// I - Driver name
const char *device_uri, // I - Device URI
const char *device_id, // I - 1284 device ID
pappl_pr_driver_data_t *data, // I - Pointer to driver data
ipp_t **attrs, // O - Pointer to driver attributes
void *cbdata) // I - Callback data (not used)
{
// Print callbacks...
data->printfile_cb = lprint_brother_printfile;
data->rendjob_cb = lprint_brother_rendjob;
data->rendpage_cb = lprint_brother_rendpage;
data->rstartjob_cb = lprint_brother_rstartjob;
data->rstartpage_cb = lprint_brother_rstartpage;
data->rwriteline_cb = lprint_brother_rwriteline;
data->status_cb = lprint_brother_status;
// Vendor-specific format...
data->format = LPRINT_BROTHER_PT_CBP_MIMETYPE;
if (!strncmp(driver_name, "brother_ql-", 11))
{
// QL-series...
// Set resolution...
// TODO: Add support for 300x600dpi mode for QL-570/580N/700/8xx
data->num_resolution = 1;
data->x_resolution[0] = data->y_resolution[0] = 300;
data->x_default = data->y_default = data->x_resolution[0];
// Basically borderless...
data->left_right = 1;
data->bottom_top = 1;
// Supported media...
data->num_media = (int)(sizeof(lprint_brother_ql_media) / sizeof(lprint_brother_ql_media[0]));
memcpy(data->media, lprint_brother_ql_media, sizeof(lprint_brother_ql_media));
papplCopyString(data->media_ready[0].size_name, "roll_dk2205-continuous_2.4x3.9in", sizeof(data->media_ready[0].size_name));
papplCopyString(data->media_ready[0].type, "continuous", sizeof(data->media_ready[0].type));
data->num_type = 2;
data->type[0] = "labels";
data->type[1] = "continuous";
}
else
{
// PT-series...
// Set resolution...
data->num_resolution = 1;
data->x_resolution[0] = data->y_resolution[0] = 180;
data->x_default = data->y_default = data->x_resolution[0];
// Basically borderless...
data->left_right = 1;
data->bottom_top = 1;
// Supported media...
data->num_media = (int)(sizeof(lprint_brother_pt_media) / sizeof(lprint_brother_pt_media[0]));
memcpy(data->media, lprint_brother_pt_media, sizeof(lprint_brother_pt_media));
data->num_source = 1;
data->source[0] = "main-roll";
papplCopyString(data->media_ready[0].size_name, "oe_wide-2in-tape_1x2in", sizeof(data->media_ready[0].size_name));
papplCopyString(data->media_ready[0].type, "labels", sizeof(data->media_ready[0].type));
data->num_type = 2;
data->type[0] = "continuous";
data->type[1] = "continuous-film";
data->type[2] = "continuous-removable";
}
data->num_source = 1;
data->source[0] = "main-roll";
// 5 darkness/density settings
data->darkness_configured = 50;
data->darkness_supported = 5;
return (true);
}
//
// 'lprint_brother_get_status()' - Query the printer status information...
//
static bool // O - `true` on success, `false` on failure
lprint_brother_get_status(
pappl_printer_t *printer, // I - Printer
pappl_device_t *device) // I - Device
{
unsigned char buffer[32]; // Status buffer
pappl_preason_t preasons; // "printer-state-reasons" values
const char *media; // "media-ready" value
// Request status...
if (!papplDevicePuts(device, "\033iS"))
return (false);
// Read status buffer...
if (papplDeviceRead(device, buffer, sizeof(buffer)) < (ssize_t)sizeof(buffer))
return (false);
LPRINT_DEBUG("lprint_brother_get_status: Print Head Mark = %02x\n", buffer[0]);
LPRINT_DEBUG("lprint_brother_get_status: Size = %02x\n", buffer[1]);
LPRINT_DEBUG("lprint_brother_get_status: Reserved = %02x\n", buffer[2]);
LPRINT_DEBUG("lprint_brother_get_status: Series Code = %02x\n", buffer[3]);
LPRINT_DEBUG("lprint_brother_get_status: Model Code = %02x %02x\n", buffer[4], buffer[5]);
LPRINT_DEBUG("lprint_brother_get_status: Reserved = %02x\n", buffer[6]);
LPRINT_DEBUG("lprint_brother_get_status: Reserved = %02x\n", buffer[7]);
LPRINT_DEBUG("lprint_brother_get_status: Error Info 1 = %02x\n", buffer[8]);
LPRINT_DEBUG("lprint_brother_get_status: Error Info 2 = %02x\n", buffer[9]);
LPRINT_DEBUG("lprint_brother_get_status: Media Width = %02x\n", buffer[10]);
LPRINT_DEBUG("lprint_brother_get_status: Media Type = %02x\n", buffer[11]);
LPRINT_DEBUG("lprint_brother_get_status: Reserved = %02x\n", buffer[12]);
LPRINT_DEBUG("lprint_brother_get_status: Reserved = %02x\n", buffer[13]);
LPRINT_DEBUG("lprint_brother_get_status: Reserved = %02x\n", buffer[14]);
LPRINT_DEBUG("lprint_brother_get_status: Mode = %02x\n", buffer[15]);
LPRINT_DEBUG("lprint_brother_get_status: Reserved = %02x\n", buffer[16]);
LPRINT_DEBUG("lprint_brother_get_status: Media Length = %02x\n", buffer[17]);
LPRINT_DEBUG("lprint_brother_get_status: Status Type = %02x\n", buffer[18]);
LPRINT_DEBUG("lprint_brother_get_status: Phase Type = %02x\n", buffer[19]);
LPRINT_DEBUG("lprint_brother_get_status: Phase Number = %02x %02x\n", buffer[20], buffer[21]);
LPRINT_DEBUG("lprint_brother_get_status: Notification # = %02x\n", buffer[22]);
LPRINT_DEBUG("lprint_brother_get_status: Reserved = %02x\n", buffer[23]);
LPRINT_DEBUG("lprint_brother_get_status: Tape Color = %02x\n", buffer[24]);
LPRINT_DEBUG("lprint_brother_get_status: Text Color = %02x\n", buffer[25]);
LPRINT_DEBUG("lprint_brother_get_status: Hardware Info = %02x %02x %02x %02x\n", buffer[26], buffer[27], buffer[28], buffer[29]);
LPRINT_DEBUG("lprint_brother_get_status: Reserved = %02x %02x\n", buffer[30], buffer[31]);
// Match ready media...
if ((media = lprintMediaMatch(printer, 0, 100 * buffer[10], 100 * buffer[17])) != NULL)
papplLogPrinter(printer, PAPPL_LOGLEVEL_DEBUG, "Detected media is '%s'.", media);
// Convert error info to "printer-state-reasons" bits...
preasons = PAPPL_PREASON_NONE;
if (buffer[8] & 0x03)
preasons |= PAPPL_PREASON_MEDIA_EMPTY;
if (buffer[8] & 0xfc)
preasons |= PAPPL_PREASON_OTHER;
if (buffer[9] & 0x01)
preasons |= PAPPL_PREASON_MEDIA_NEEDED;
if (buffer[9] & 0x10)
preasons |= PAPPL_PREASON_COVER_OPEN;
if (buffer[9] & 0x40)
preasons |= PAPPL_PREASON_MEDIA_JAM;
if (buffer[9] & 0xae)
preasons |= PAPPL_PREASON_OTHER;
papplPrinterSetReasons(printer, preasons, ~preasons);
return (true);
}
//
// 'lprint_brother_printfile()' - Print a file.
//
static bool // O - `true` on success, `false` on failure
lprint_brother_printfile(
pappl_job_t *job, // I - Job
pappl_pr_options_t *options, // I - Job options
pappl_device_t *device) // I - Output device
{
int fd; // Input file
ssize_t bytes; // Bytes read/written
char buffer[65536]; // Read/write buffer
// lprint_brother_t brother; // Driver data
// Reset the printer...
lprint_brother_rstartjob(job, options, device);
// Copy the raw file...
papplJobSetImpressions(job, 1);
if ((fd = open(papplJobGetFilename(job), O_RDONLY)) < 0)
{
papplLogJob(job, PAPPL_LOGLEVEL_ERROR, "Unable to open print file '%s': %s", papplJobGetFilename(job), strerror(errno));
return (false);
}
while ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
{
if (papplDeviceWrite(device, buffer, (size_t)bytes) < 0)
{
papplLogJob(job, PAPPL_LOGLEVEL_ERROR, "Unable to send %d bytes to printer.", (int)bytes);
close(fd);
return (false);
}
}
close(fd);
lprint_brother_rstartjob(job, options, device);
papplJobSetImpressionsCompleted(job, 1);
return (true);
}
//
// 'lprint_brother_rend()' - End a job.
//
static bool // O - `true` on success, `false` on failure
lprint_brother_rendjob(
pappl_job_t *job, // I - Job
pappl_pr_options_t *options, // I - Job options
pappl_device_t *device) // I - Output device
{
lprint_brother_t *brother = (lprint_brother_t *)papplJobGetData(job);
// Brother driver data
(void)options;
free(brother->buffer);
free(brother);
papplJobSetData(job, NULL);
return (true);
}
//
// 'lprint_brother_rendpage()' - End a page.
//
static bool // O - `true` on success, `false` on failure
lprint_brother_rendpage(
pappl_job_t *job, // I - Job
pappl_pr_options_t *options, // I - Job options
pappl_device_t *device, // I - Output device
unsigned page) // I - Page number
{
lprint_brother_t *brother = (lprint_brother_t *)papplJobGetData(job);
// Brother driver data
unsigned char buffer[13]; // Print Information command buffer
// Write last line
lprint_brother_rwriteline(job, options, device, options->header.cupsHeight, NULL);
// Send print information...
buffer[ 0] = 0x1b;
buffer[ 1] = 'i';
buffer[ 2] = 'z';
buffer[ 3] = !strcmp(options->media.type, "continuous") ? 0x04 : 0x0c;
buffer[ 4] = 0;
buffer[ 5] = options->media.size_width / 100;
buffer[ 6] = options->media.size_length / 100;
#if 1
buffer[ 7] = options->header.cupsHeight & 255;
buffer[ 8] = (options->header.cupsHeight >> 8) & 255;
buffer[ 9] = (options->header.cupsHeight >> 16) & 255;
buffer[10] = (options->header.cupsHeight >> 24) & 255;
#else
buffer[ 7] = brother->count & 255;
buffer[ 8] = (brother->count >> 8) & 255;
buffer[ 9] = (brother->count >> 16) & 255;
buffer[10] = (brother->count >> 24) & 255;
#endif // 1
buffer[11] = page == 0 ? 0 : 1;
buffer[12] = 0;
if (!papplDeviceWrite(device, buffer, sizeof(buffer)))
return (false);
// Send label data...
if (brother->num_bytes > 0 && !papplDeviceWrite(device, brother->buffer, brother->num_bytes))
return (false);
// Eject/cut
papplDevicePrintf(device, "\033iM%c\014", !strcmp(options->media.type, "continuous") ? 64 : 0);
papplDeviceFlush(device);
// Free memory and return...
lprintDitherFree(&brother->dither);
return (true);
}
//
// 'lprint_brother_rstartjob()' - Start a job.
//
static bool // O - `true` on success, `false` on failure
lprint_brother_rstartjob(
pappl_job_t *job, // I - Job
pappl_pr_options_t *options, // I - Job options
pappl_device_t *device) // I - Output device
{
lprint_brother_t *brother = (lprint_brother_t *)calloc(1, sizeof(lprint_brother_t));
// Brother driver data
const char *driver_name = papplPrinterGetDriverName(papplJobGetPrinter(job));
// Driver name
char buffer[400]; // Reset buffer
int darkness; // Combined darkness
(void)options;
// Save driver data...
papplJobSetData(job, brother);
// Reset the printer...
memset(buffer, 0, sizeof(buffer));
if (driver_name && !strncmp(driver_name, "brother_pt-", 11))
{
// Send short reset sequence for PT-series tape printers
papplDeviceWrite(device, buffer, 100);
brother->is_pt_series = true;
}
else
{
// Send long reset sequence for QL-series label printers
papplDeviceWrite(device, buffer, sizeof(buffer));
brother->is_ql_800 = driver_name && !strcmp(driver_name, "brother_ql-800");
}
// Get status information...
lprint_brother_get_status(papplJobGetPrinter(job), device);
// if (!lprint_brother_get_status(papplJobGetPrinter(job), device))
// return (false);
// Reset and set raster mode...
if (!papplDevicePuts(device, "\033@\033ia\001"))
return (false);
// print-darkness / printer-darkness-configured
if ((darkness = options->print_darkness + options->darkness_configured) < 0)
darkness = 0;
else if (darkness > 100)
darkness = 100;
return (papplDevicePrintf(device, "\033iD%c", 4 * darkness / 100 + 1));
}
//
// 'lprint_brother_rstartpage()' - Start a page.
//
static bool // O - `true` on success, `false` on failure
lprint_brother_rstartpage(
pappl_job_t *job, // I - Job
pappl_pr_options_t *options, // I - Job options
pappl_device_t *device, // I - Output device
unsigned page) // I - Page number
{
lprint_brother_t *brother = (lprint_brother_t *)papplJobGetData(job);
// Brother driver data
(void)page;
if (!lprintDitherAlloc(&brother->dither, job, options, CUPS_CSPACE_K, options->header.HWResolution[0] == 300 ? 1.2 : 1.0))
return (false);
brother->count = 0;
brother->num_bytes = 0;
return (true);
}
//
// 'lprint_brother_rwriteline()' - Write a raster line.
//
static bool // O - `true` on success, `false` on failure
lprint_brother_rwriteline(
pappl_job_t *job, // I - Job
pappl_pr_options_t *options, // I - Job options
pappl_device_t *device, // I - Output device
unsigned y, // I - Line number
const unsigned char *line) // I - Line
{
lprint_brother_t *brother = (lprint_brother_t *)papplJobGetData(job);
// Brother driver data
unsigned char *bufptr; // Pointer into page buffer
if (!lprintDitherLine(&brother->dither, y, line))
return (true);
if ((brother->alloc_bytes - brother->num_bytes) < (3 + brother->dither.out_width))
{
size_t temp_alloc = brother->alloc_bytes + brother->dither.out_width + 4096;
// New allocated size
unsigned char *temp = realloc(brother->buffer, temp_alloc);
// New buffer
if (!temp)
{
papplLogJob(job, PAPPL_LOGLEVEL_ERROR, "Unable to allocate %lu bytes of memory memory.", (unsigned long)temp_alloc);
return (false);
}
brother->alloc_bytes = temp_alloc;
brother->buffer = temp;
}
bufptr = brother->buffer + brother->num_bytes;
if (brother->is_ql_800 || brother->dither.output[0] || memcmp(brother->dither.output, brother->dither.output + 1, brother->dither.out_width - 1))
{
// Non-blank line...
// TODO: Add PackBits compression support
brother->count += 3 + brother->dither.out_width;
*bufptr++ = 'g';
if (brother->is_pt_series)
{
*bufptr++ = brother->dither.out_width & 255;
*bufptr++ = (brother->dither.out_width >> 8) & 255;
}
else
{
*bufptr++ = 0;
*bufptr++ = brother->dither.out_width;
}
memcpy(bufptr, brother->dither.output, brother->dither.out_width);
brother->num_bytes += 3 + brother->dither.out_width;
}
else
{
// Blank line
brother->count ++;
*bufptr = 'Z';
brother->num_bytes ++;
}
return (true);
}
//
// 'lprint_brother_status()' - Get current printer status.
//
static bool // O - `true` on success, `false` on failure
lprint_brother_status(
pappl_printer_t *printer) // I - Printer
{
(void)printer;
return (true);
}
#endif // LPRINT_EXPERIMENTAL