-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprinter_other.go
More file actions
292 lines (259 loc) · 8.94 KB
/
printer_other.go
File metadata and controls
292 lines (259 loc) · 8.94 KB
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
//go:build !windows && !crossbuild
package main
/*
#cgo CFLAGS: -I/opt/homebrew/include/libusb-1.0 -I/usr/local/include/libusb-1.0
#cgo LDFLAGS: -L/opt/homebrew/lib -L/usr/local/lib -lusb-1.0
#include <libusb.h>
#include <stdlib.h>
#include <stdio.h>
// Check if a USB device exists (returns 0 if found, -1 if not)
int usb_device_exists(int vendor_id, int product_id) {
libusb_context *ctx = NULL;
if (libusb_init(&ctx) < 0) return -1;
libusb_device_handle *handle = libusb_open_device_with_vid_pid(ctx, vendor_id, product_id);
if (handle == NULL) {
libusb_exit(ctx);
return -1;
}
libusb_close(handle);
libusb_exit(ctx);
return 0;
}
// Find the bulk OUT endpoint for a given interface
unsigned char find_bulk_out_endpoint(libusb_device *dev, int interface_num) {
struct libusb_config_descriptor *config;
if (libusb_get_active_config_descriptor(dev, &config) != 0) return 0;
unsigned char ep = 0;
if (interface_num < config->bNumInterfaces) {
const struct libusb_interface *iface = &config->interface[interface_num];
if (iface->num_altsetting > 0) {
const struct libusb_interface_descriptor *desc = &iface->altsetting[0];
for (int i = 0; i < desc->bNumEndpoints; i++) {
const struct libusb_endpoint_descriptor *epd = &desc->endpoint[i];
// Bulk OUT: transfer type = bulk (0x02), direction = OUT (0x00)
if ((epd->bmAttributes & 0x03) == 0x02 &&
(epd->bEndpointAddress & 0x80) == 0x00) {
ep = epd->bEndpointAddress;
fprintf(stderr, "[usb] Found bulk OUT endpoint: 0x%02x\n", ep);
break;
}
}
}
}
libusb_free_config_descriptor(config);
return ep;
}
// Send raw data to a USB printer by vendor/product ID
int usb_raw_print(int vendor_id, int product_id, const unsigned char *data, int length) {
libusb_context *ctx = NULL;
libusb_device_handle *handle = NULL;
int ret;
ret = libusb_init(&ctx);
if (ret < 0) return -1;
handle = libusb_open_device_with_vid_pid(ctx, vendor_id, product_id);
if (handle == NULL) {
libusb_exit(ctx);
return -2; // device not found
}
// Detach kernel driver if active (macOS CUPS grabs the device)
if (libusb_kernel_driver_active(handle, 0) == 1) {
libusb_detach_kernel_driver(handle, 0);
}
ret = libusb_claim_interface(handle, 0);
if (ret < 0) {
libusb_close(handle);
libusb_exit(ctx);
return -3; // cannot claim interface
}
// Auto-detect the bulk OUT endpoint
libusb_device *dev = libusb_get_device(handle);
unsigned char endpoint = find_bulk_out_endpoint(dev, 0);
if (endpoint == 0) {
fprintf(stderr, "[usb] No bulk OUT endpoint found, trying 0x01 and 0x02\n");
// Try common endpoints as fallback
int transferred = 0;
ret = libusb_bulk_transfer(handle, 0x02, (unsigned char *)data, length, &transferred, 5000);
if (ret < 0) {
ret = libusb_bulk_transfer(handle, 0x01, (unsigned char *)data, length, &transferred, 5000);
}
libusb_release_interface(handle, 0);
libusb_close(handle);
libusb_exit(ctx);
if (ret < 0) return -4;
return transferred;
}
int transferred = 0;
ret = libusb_bulk_transfer(handle, endpoint, (unsigned char *)data, length, &transferred, 5000);
libusb_release_interface(handle, 0);
libusb_close(handle);
libusb_exit(ctx);
if (ret < 0) return -4; // transfer failed
return transferred;
}
*/
import "C"
import (
"fmt"
"os/exec"
"strings"
)
const (
tscVendorID = 0x1203
tscProductID = 0x0133 // TDP-244 Plus
)
// C_usb_device_exists checks if the known TSC USB device is connected.
// Exported as a Go function so other files (driver_darwin.go) can use it without importing C.
func C_usb_device_exists() bool {
return C.usb_device_exists(C.int(tscVendorID), C.int(tscProductID)) == 0
}
// listLocalPrinters lists ALL printers: USB (direct) + all CUPS printers with status.
func listLocalPrinters() ([]PrinterInfo, error) {
var printers []PrinterInfo
// Check if TSC USB device is physically connected via libusb
usbConnected := C.usb_device_exists(C.int(tscVendorID), C.int(tscProductID)) == 0
// Parse CUPS printer status: "lpstat -p" gives status of each printer
statusMap := make(map[string]string) // name -> "idle"|"disabled"|"printing"
if out, err := exec.Command("lpstat", "-p").Output(); err == nil {
for _, line := range strings.Split(string(out), "\n") {
line = strings.TrimSpace(line)
if !strings.HasPrefix(line, "la impresora ") && !strings.HasPrefix(line, "impresora ") && !strings.HasPrefix(line, "printer ") {
continue
}
// Parse: "la impresora NAME está inactiva" or "printer NAME is idle"
// or: "impresora NAME desactivada"
fields := strings.Fields(line)
if len(fields) < 3 {
continue
}
// Name is the 3rd field (after "la impresora" or "printer")
var name string
if strings.HasPrefix(line, "la impresora ") || strings.HasPrefix(line, "impresora ") {
// Spanish: "la impresora X está inactiva" or "impresora X desactivada"
for _, f := range fields {
if f != "la" && f != "impresora" {
name = f
break
}
}
} else {
// English: "printer X is idle"
name = fields[1]
}
if name == "" {
continue
}
lower := strings.ToLower(line)
if strings.Contains(lower, "desactivad") || strings.Contains(lower, "disabled") {
statusMap[name] = "disabled"
} else if strings.Contains(lower, "imprimiendo") || strings.Contains(lower, "printing") {
statusMap[name] = "printing"
} else {
statusMap[name] = "idle"
}
}
}
// List ALL CUPS printers (not just TSC)
out, err := exec.Command("lpstat", "-a").Output()
if err == nil {
for _, line := range strings.Split(string(out), "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
name := strings.Fields(line)[0]
lower := strings.ToLower(name)
status := statusMap[name]
online := status != "disabled"
// Determine type — TSC printers may have USB direct access
pType := "cups"
isTSC := false
for _, kw := range []string{"tsc", "tdp", "ttp", "te2", "te3"} {
if strings.Contains(lower, kw) {
isTSC = true
break
}
}
if isTSC && usbConnected {
// TSC printer with USB device present — mark as USB and online
printers = append(printers, PrinterInfo{
Name: name,
Type: "usb",
Model: "TDP-244 Plus",
Online: true,
Status: status,
})
} else if isTSC && !usbConnected {
// TSC printer in CUPS but USB not connected
printers = append(printers, PrinterInfo{
Name: name,
Type: "cups",
Online: false,
Status: "disconnected",
})
} else {
printers = append(printers, PrinterInfo{
Name: name,
Type: pType,
Online: online,
Status: status,
})
}
}
}
return printers, nil
}
// rawPrint sends data directly to the printer. Tries USB first, falls back to CUPS.
func rawPrint(printerName string, data []byte) error {
if strings.HasPrefix(printerName, "(simulated)") {
fmt.Printf("[simulate] Would print %d bytes to %s\n", len(data), printerName)
fmt.Printf("[simulate] Commands:\n%s\n", string(data))
return nil
}
// Try direct USB first for TSC printers
isTSC := strings.Contains(printerName, "USB") || strings.Contains(strings.ToLower(printerName), "tsc")
if isTSC {
cData := C.CBytes(data)
defer C.free(cData)
ret := C.usb_raw_print(
C.int(tscVendorID),
C.int(tscProductID),
(*C.uchar)(cData),
C.int(len(data)),
)
if ret > 0 {
fmt.Printf("[print-usb] Sent %d/%d bytes directly via USB to %s\n", int(ret), len(data), printerName)
return nil
}
// USB failed — fall through to CUPS
fmt.Printf("[print-usb] USB not available (ret=%d), falling back to CUPS for %s\n", int(ret), printerName)
}
// CUPS fallback: resolve the actual CUPS printer name
cupsName := printerName
// Strip "-USB" suffix if present — CUPS doesn't use it
cupsName = strings.TrimSuffix(cupsName, "-USB")
// If we still can't find the printer, search CUPS for any TSC printer
if isTSC {
if out, err := exec.Command("lpstat", "-a").Output(); err == nil {
for _, line := range strings.Split(string(out), "\n") {
fields := strings.Fields(strings.TrimSpace(line))
if len(fields) == 0 {
continue
}
name := fields[0]
lower := strings.ToLower(name)
if strings.Contains(lower, "tsc") || strings.Contains(lower, "tdp") || strings.Contains(lower, "ttp") {
cupsName = name
break
}
}
}
}
cmd := exec.Command("lp", "-d", cupsName, "-o", "raw")
cmd.Stdin = strings.NewReader(string(data))
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("lp -d %s failed: %v — %s", cupsName, err, string(output))
}
fmt.Printf("[print-cups] Sent %d bytes via lp to %s\n", len(data), cupsName)
return nil
}