This commit is contained in:
@@ -45,7 +45,15 @@ std::vector<std::shared_ptr<IPrinterDriver>> PrinterService::detectPrinters() {
|
|||||||
for (auto& usbDev : usbDevs) {
|
for (auto& usbDev : usbDevs) {
|
||||||
auto driver = driverFactory->create(usbDev->getUsbId());
|
auto driver = driverFactory->create(usbDev->getUsbId());
|
||||||
if (driver != nullptr) {
|
if (driver != nullptr) {
|
||||||
mDetectedPrinters.push_back(driver);
|
// Attach the USB device to the printer driver
|
||||||
|
// Convert unique_ptr to shared_ptr for attachment
|
||||||
|
std::shared_ptr<libusbwrap::IUsbDevice> sharedUsbDev = std::move(usbDev);
|
||||||
|
if (driver->attachUsbDevice(sharedUsbDev)) {
|
||||||
|
mDetectedPrinters.push_back(driver);
|
||||||
|
spdlog::debug("Successfully attached USB device to printer driver: {}", driver->getName());
|
||||||
|
} else {
|
||||||
|
spdlog::warn("Failed to attach USB device to printer driver: {}", driver->getName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,6 +67,9 @@ std::shared_ptr<IPrinterDriver> PrinterService::selectPrinter(const std::string&
|
|||||||
auto driverFactory = std::make_unique<PrinterDriverFactory>();
|
auto driverFactory = std::make_unique<PrinterDriverFactory>();
|
||||||
auto printer = driverFactory->createByName(printerName);
|
auto printer = driverFactory->createByName(printerName);
|
||||||
if (printer) {
|
if (printer) {
|
||||||
|
// For virtual/fake printers, call attachUsbDevice with nullptr to initialize
|
||||||
|
// For real printers selected explicitly, they would need actual USB device
|
||||||
|
printer->attachUsbDevice(nullptr);
|
||||||
mCurrentPrinter = printer;
|
mCurrentPrinter = printer;
|
||||||
spdlog::info("Using explicitly selected printer: {}", printerName);
|
spdlog::info("Using explicitly selected printer: {}", printerName);
|
||||||
return mCurrentPrinter;
|
return mCurrentPrinter;
|
||||||
|
|||||||
@@ -33,12 +33,6 @@ class CairoWrapper : public ICairoWrapper {
|
|||||||
public:
|
public:
|
||||||
~CairoWrapper() override = default;
|
~CairoWrapper() override = default;
|
||||||
|
|
||||||
// No copy, no move
|
|
||||||
CairoWrapper(const CairoWrapper&) = delete;
|
|
||||||
CairoWrapper& operator=(const CairoWrapper&) = delete;
|
|
||||||
CairoWrapper(CairoWrapper&&) = delete;
|
|
||||||
CairoWrapper& operator=(CairoWrapper&&) = delete;
|
|
||||||
|
|
||||||
// Cairo image surface functions
|
// Cairo image surface functions
|
||||||
cairo_surface_t* cairo_image_surface_create(cairo_format_t format, int width, int height) override {
|
cairo_surface_t* cairo_image_surface_create(cairo_format_t format, int width, int height) override {
|
||||||
return ::cairo_image_surface_create(format, width, height);
|
return ::cairo_image_surface_create(format, width, height);
|
||||||
|
|||||||
@@ -39,27 +39,30 @@ const PrinterInfo FakePrinter::mInfo = {.driverName = "FakePrinter",
|
|||||||
.usbId{0x0000, 0x0000}, // No USB ID - virtual printer created explicitly
|
.usbId{0x0000, 0x0000}, // No USB ID - virtual printer created explicitly
|
||||||
.pixelLines = 128};
|
.pixelLines = 128};
|
||||||
|
|
||||||
const std::string_view FakePrinter::getDriverName() {
|
std::string_view FakePrinter::getDriverName() {
|
||||||
return mInfo.driverName;
|
return mInfo.driverName;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string_view FakePrinter::getName() {
|
std::string_view FakePrinter::getName() {
|
||||||
return mInfo.name;
|
return mInfo.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string_view FakePrinter::getVersion() {
|
std::string_view FakePrinter::getVersion() {
|
||||||
return mInfo.version;
|
return mInfo.version;
|
||||||
}
|
}
|
||||||
|
|
||||||
const PrinterInfo FakePrinter::getPrinterInfo() {
|
PrinterInfo FakePrinter::getPrinterInfo() {
|
||||||
return mInfo;
|
return mInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
const PrinterStatus FakePrinter::getPrinterStatus() {
|
PrinterStatus FakePrinter::getPrinterStatus() {
|
||||||
|
if (!mHasAttachedDevice) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
return mStatus;
|
return mStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
const libusbwrap::usbId FakePrinter::getUsbId() {
|
libusbwrap::usbId FakePrinter::getUsbId() {
|
||||||
return mInfo.usbId;
|
return mInfo.usbId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,6 +89,9 @@ bool FakePrinter::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap)
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool FakePrinter::printMonochromeData(const graphics::MonochromeData& data) {
|
bool FakePrinter::printMonochromeData(const graphics::MonochromeData& data) {
|
||||||
|
if (!mHasAttachedDevice) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
spdlog::debug("FakePrinter: Simulating printing of {}x{} bitmap", data.width, data.height);
|
spdlog::debug("FakePrinter: Simulating printing of {}x{} bitmap", data.width, data.height);
|
||||||
|
|
||||||
// Simulate the printing process by reconstructing the bitmap
|
// Simulate the printing process by reconstructing the bitmap
|
||||||
@@ -125,6 +131,9 @@ bool FakePrinter::printLabel(const std::unique_ptr<graphics::ILabel> label) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool FakePrinter::print() {
|
bool FakePrinter::print() {
|
||||||
|
if (!mHasAttachedDevice) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
spdlog::debug("FakePrinter: Print command (no-op for virtual printer)");
|
spdlog::debug("FakePrinter: Print command (no-op for virtual printer)");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,12 +52,12 @@ class FakePrinter : public ::ptprnt::IPrinterDriver {
|
|||||||
static const PrinterInfo mInfo;
|
static const PrinterInfo mInfo;
|
||||||
|
|
||||||
// IPrinterDriver interface
|
// IPrinterDriver interface
|
||||||
[[nodiscard]] const std::string_view getDriverName() override;
|
[[nodiscard]] std::string_view getDriverName() override;
|
||||||
[[nodiscard]] const std::string_view getName() override;
|
[[nodiscard]] std::string_view getName() override;
|
||||||
[[nodiscard]] const libusbwrap::usbId getUsbId() override;
|
[[nodiscard]] libusbwrap::usbId getUsbId() override;
|
||||||
[[nodiscard]] const std::string_view getVersion() override;
|
[[nodiscard]] std::string_view getVersion() override;
|
||||||
[[nodiscard]] const PrinterInfo getPrinterInfo() override;
|
[[nodiscard]] PrinterInfo getPrinterInfo() override;
|
||||||
[[nodiscard]] const PrinterStatus getPrinterStatus() override;
|
[[nodiscard]] PrinterStatus getPrinterStatus() override;
|
||||||
bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) override;
|
bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) override;
|
||||||
bool detachUsbDevice() override;
|
bool detachUsbDevice() override;
|
||||||
bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) override;
|
bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) override;
|
||||||
|
|||||||
@@ -48,24 +48,30 @@ P700Printer::~P700Printer() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string_view P700Printer::getDriverName() {
|
std::string_view P700Printer::getDriverName() {
|
||||||
return mInfo.driverName;
|
return mInfo.driverName;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string_view P700Printer::getName() {
|
std::string_view P700Printer::getName() {
|
||||||
return mInfo.name;
|
return mInfo.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string_view P700Printer::getVersion() {
|
std::string_view P700Printer::getVersion() {
|
||||||
return mInfo.version;
|
return mInfo.version;
|
||||||
}
|
}
|
||||||
|
|
||||||
const PrinterInfo P700Printer::getPrinterInfo() {
|
PrinterInfo P700Printer::getPrinterInfo() {
|
||||||
return mInfo;
|
return mInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
const PrinterStatus P700Printer::getPrinterStatus() {
|
PrinterStatus P700Printer::getPrinterStatus() {
|
||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
|
if (!mUsbHndl) {
|
||||||
|
spdlog::error("USB Handle is invalid!");
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
send(p700::commands::GET_STATUS);
|
send(p700::commands::GET_STATUS);
|
||||||
|
|
||||||
int tx = 0;
|
int tx = 0;
|
||||||
@@ -79,7 +85,7 @@ const PrinterStatus P700Printer::getPrinterStatus() {
|
|||||||
return PrinterStatus{.tapeWidthMm = recvBuf[10]};
|
return PrinterStatus{.tapeWidthMm = recvBuf[10]};
|
||||||
}
|
}
|
||||||
|
|
||||||
const libusbwrap::usbId P700Printer::getUsbId() {
|
libusbwrap::usbId P700Printer::getUsbId() {
|
||||||
return mInfo.usbId;
|
return mInfo.usbId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -66,12 +66,12 @@ class P700Printer : public ::ptprnt::IPrinterDriver {
|
|||||||
static const PrinterInfo mInfo;
|
static const PrinterInfo mInfo;
|
||||||
|
|
||||||
// IPrinterDriver
|
// IPrinterDriver
|
||||||
[[nodiscard]] const std::string_view getDriverName() override;
|
[[nodiscard]] std::string_view getDriverName() override;
|
||||||
[[nodiscard]] const std::string_view getName() override;
|
[[nodiscard]] std::string_view getName() override;
|
||||||
[[nodiscard]] const libusbwrap::usbId getUsbId() override;
|
[[nodiscard]] libusbwrap::usbId getUsbId() override;
|
||||||
[[nodiscard]] const std::string_view getVersion() override;
|
[[nodiscard]] std::string_view getVersion() override;
|
||||||
[[nodiscard]] const PrinterInfo getPrinterInfo() override;
|
[[nodiscard]] PrinterInfo getPrinterInfo() override;
|
||||||
[[nodiscard]] const PrinterStatus getPrinterStatus() override;
|
[[nodiscard]] PrinterStatus getPrinterStatus() override;
|
||||||
bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) override;
|
bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) override;
|
||||||
bool detachUsbDevice() override;
|
bool detachUsbDevice() override;
|
||||||
bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) override;
|
bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) override;
|
||||||
|
|||||||
@@ -32,12 +32,12 @@ namespace ptprnt {
|
|||||||
class IPrinterDriver {
|
class IPrinterDriver {
|
||||||
public:
|
public:
|
||||||
virtual ~IPrinterDriver() = default;
|
virtual ~IPrinterDriver() = default;
|
||||||
[[nodiscard]] virtual const std::string_view getDriverName() = 0;
|
[[nodiscard]] virtual std::string_view getDriverName() = 0;
|
||||||
[[nodiscard]] virtual const std::string_view getName() = 0;
|
[[nodiscard]] virtual std::string_view getName() = 0;
|
||||||
[[nodiscard]] virtual const std::string_view getVersion() = 0;
|
[[nodiscard]] virtual std::string_view getVersion() = 0;
|
||||||
[[nodiscard]] virtual const libusbwrap::usbId getUsbId() = 0;
|
[[nodiscard]] virtual libusbwrap::usbId getUsbId() = 0;
|
||||||
[[nodiscard]] virtual const PrinterInfo getPrinterInfo() = 0;
|
[[nodiscard]] virtual PrinterInfo getPrinterInfo() = 0;
|
||||||
[[nodiscard]] virtual const PrinterStatus getPrinterStatus() = 0;
|
[[nodiscard]] virtual PrinterStatus getPrinterStatus() = 0;
|
||||||
virtual bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) = 0;
|
virtual bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) = 0;
|
||||||
virtual bool detachUsbDevice() = 0;
|
virtual bool detachUsbDevice() = 0;
|
||||||
virtual bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) = 0;
|
virtual bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user