Generate labels with pangocairo (#8)
All checks were successful
Build ptprnt / build (push) Successful in 3m41s
All checks were successful
Build ptprnt / build (push) Successful in 3m41s
Goal of this PR is to have some basic labels generated with pangocairo - size of the canvas should be matching the input text and grow/shrink accordingly - basic formatting options like fontsize and align should be working Reviewed-on: moritz/ptouch-prnt#8
This commit was merged in pull request #8.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 2023 Moritz Martinius
|
||||
Copyright (C) 2023-2024 Moritz Martinius
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 2023 Moritz Martinius
|
||||
Copyright (C) 2023-2024 Moritz Martinius
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -27,12 +27,12 @@
|
||||
#include "libusbwrap/interface/IUsbDevice.hpp"
|
||||
|
||||
namespace libusbwrap {
|
||||
UsbDevice::UsbDevice(libusb_context* ctx, libusb_device* dev) : mLibusbCtx(ctx), mLibusbDev(dev) {
|
||||
UsbDevice::UsbDevice(libusb_context* ctx, usbDevice_ptr dev) : mLibusbCtx(ctx), mLibusbDev(std::move(dev)) {
|
||||
if (mLibusbCtx == nullptr || mLibusbDev == nullptr) {
|
||||
throw std::invalid_argument("ctx or device are nullptr");
|
||||
}
|
||||
|
||||
libusb_get_device_descriptor(dev, &mLibusbDevDesc);
|
||||
libusb_get_device_descriptor(mLibusbDev.get(), &mLibusbDevDesc);
|
||||
}
|
||||
|
||||
UsbDevice::~UsbDevice() {
|
||||
@@ -44,7 +44,7 @@ UsbDevice::~UsbDevice() {
|
||||
}
|
||||
|
||||
bool UsbDevice::open() {
|
||||
int openStatus = libusb_open(mLibusbDev, &mLibusbDevHandle);
|
||||
int openStatus = libusb_open(mLibusbDev.get(), &mLibusbDevHandle);
|
||||
if (openStatus != 0) {
|
||||
mLastError = static_cast<Error>(openStatus);
|
||||
return false;
|
||||
@@ -90,13 +90,12 @@ bool UsbDevice::releaseInterface(int interfaceNo) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UsbDevice::bulkTransfer(uint8_t endpoint, std::vector<uint8_t>& data, int* tx,
|
||||
unsigned int timeout) {
|
||||
bool UsbDevice::bulkTransfer(uint8_t endpoint, const std::vector<uint8_t>& data, int* tx, unsigned int timeout) {
|
||||
// TODO: implement error handling for incomplete transactions (tx length != data length)
|
||||
int bulkTransferStatus = 0;
|
||||
|
||||
bulkTransferStatus =
|
||||
libusb_bulk_transfer(mLibusbDevHandle, endpoint, data.data(), data.size(), tx, timeout);
|
||||
bulkTransferStatus = libusb_bulk_transfer(mLibusbDevHandle, endpoint, const_cast<unsigned char*>(data.data()),
|
||||
data.size(), tx, timeout);
|
||||
if (bulkTransferStatus != 0) {
|
||||
mLastError = static_cast<Error>(bulkTransferStatus);
|
||||
return false;
|
||||
@@ -109,15 +108,15 @@ const usbId UsbDevice::getUsbId() {
|
||||
}
|
||||
|
||||
const device::Speed UsbDevice::getSpeed() {
|
||||
return static_cast<device::Speed>(libusb_get_device_speed(mLibusbDev));
|
||||
return static_cast<device::Speed>(libusb_get_device_speed(mLibusbDev.get()));
|
||||
}
|
||||
|
||||
const uint8_t UsbDevice::getBusNumber() {
|
||||
return libusb_get_bus_number(mLibusbDev);
|
||||
return libusb_get_bus_number(mLibusbDev.get());
|
||||
}
|
||||
|
||||
const uint8_t UsbDevice::getPortNumber() {
|
||||
return libusb_get_port_number(mLibusbDev);
|
||||
return libusb_get_port_number(mLibusbDev.get());
|
||||
}
|
||||
|
||||
const Error UsbDevice::getLastError() {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 2023 Moritz Martinius
|
||||
Copyright (C) 2023-2024 Moritz Martinius
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -20,21 +20,34 @@
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
|
||||
#include "libusb.h"
|
||||
#include "libusbwrap/LibUsbTypes.hpp"
|
||||
#include "libusbwrap/interface/IUsbDevice.hpp"
|
||||
|
||||
namespace libusbwrap {
|
||||
|
||||
struct usbDevice_deleter {
|
||||
void operator()(libusb_device* dev_ptr) const {
|
||||
if (nullptr != dev_ptr) {
|
||||
libusb_unref_device(dev_ptr);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
using usbDevice_ptr = std::unique_ptr<libusb_device, usbDevice_deleter>;
|
||||
|
||||
class UsbDevice : public IUsbDevice {
|
||||
public:
|
||||
explicit UsbDevice(libusb_context* ctx, libusb_device* dev);
|
||||
explicit UsbDevice(libusb_context* ctx, usbDevice_ptr dev);
|
||||
~UsbDevice() override;
|
||||
|
||||
// delete copy ctor and assignment
|
||||
UsbDevice(const UsbDevice&) = delete;
|
||||
UsbDevice& operator=(UsbDevice&) = delete;
|
||||
UsbDevice(const UsbDevice&) = delete;
|
||||
UsbDevice& operator=(const UsbDevice&) = delete;
|
||||
UsbDevice(UsbDevice&&) = delete;
|
||||
UsbDevice& operator=(UsbDevice&&) = delete;
|
||||
|
||||
bool open() override;
|
||||
void close() override;
|
||||
@@ -43,8 +56,7 @@ class UsbDevice : public IUsbDevice {
|
||||
bool detachKernelDriver(int interfaceNo) override;
|
||||
bool claimInterface(int interfaceNo) override;
|
||||
bool releaseInterface(int interfaceNo) override;
|
||||
bool bulkTransfer(uint8_t endpoint, std::vector<uint8_t>& data, int* tx,
|
||||
unsigned int timeout) override;
|
||||
bool bulkTransfer(uint8_t endpoint, const std::vector<uint8_t>& data, int* tx, unsigned int timeout) override;
|
||||
|
||||
// getters
|
||||
const usbId getUsbId() override;
|
||||
@@ -58,9 +70,9 @@ class UsbDevice : public IUsbDevice {
|
||||
|
||||
private:
|
||||
libusb_context* mLibusbCtx{nullptr};
|
||||
libusb_device* mLibusbDev{nullptr};
|
||||
usbDevice_ptr mLibusbDev{nullptr};
|
||||
libusb_device_handle* mLibusbDevHandle{nullptr};
|
||||
libusb_device_descriptor mLibusbDevDesc;
|
||||
libusb_device_descriptor mLibusbDevDesc{0};
|
||||
std::atomic<bool> mIsOpen = false;
|
||||
Error mLastError = Error::SUCCESS;
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 2023 Moritz Martinius
|
||||
Copyright (C) 2023-2025 Moritz Martinius
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -20,9 +20,11 @@
|
||||
#include "libusbwrap/UsbDeviceFactory.hpp"
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#include "libusb.h"
|
||||
#include "libusbwrap/UsbDevice.hpp"
|
||||
@@ -31,49 +33,54 @@
|
||||
namespace libusbwrap {
|
||||
|
||||
UsbDeviceFactory::~UsbDeviceFactory() {
|
||||
if (mDeviceListInitialized) {
|
||||
libusb_free_device_list(mLibusbDeviceList, 1);
|
||||
if (mLibusbCtx) {
|
||||
libusb_exit(mLibusbCtx);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::findAllDevices() {
|
||||
std::vector<std::unique_ptr<IUsbDevice>> UsbDeviceFactory::findAllDevices() {
|
||||
refreshDeviceList();
|
||||
return buildMaskedDeviceVector(0x0, 0x0, 0x0, 0x0);
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::findDevices(uint16_t vid, uint16_t pid) {
|
||||
std::vector<std::unique_ptr<IUsbDevice>> UsbDeviceFactory::findDevices(uint16_t vid, uint16_t pid) {
|
||||
refreshDeviceList();
|
||||
return buildMaskedDeviceVector(0xffff, 0xffff, vid, pid);
|
||||
return buildMaskedDeviceVector(LIBUSB_BITMASK_ALL, LIBUSB_BITMASK_ALL, vid, pid);
|
||||
}
|
||||
|
||||
int UsbDeviceFactory::refreshDeviceList() {
|
||||
int ret = libusb_get_device_list(mLibusbCtx, &mLibusbDeviceList);
|
||||
ssize_t UsbDeviceFactory::refreshDeviceList() {
|
||||
libusb_device** list{nullptr};
|
||||
|
||||
ssize_t ret = libusb_get_device_list(mLibusbCtx, &list);
|
||||
mLibusbDeviceList.clear();
|
||||
if (ret < 0) {
|
||||
spdlog::error("Error enumarating USB devices");
|
||||
} else if (ret == 0) {
|
||||
spdlog::warn("No USB devices found");
|
||||
}
|
||||
mDeviceListInitialized = true;
|
||||
|
||||
for (ssize_t i = 0; i < ret; i++) {
|
||||
mLibusbDeviceList.emplace_back(list[i]);
|
||||
}
|
||||
|
||||
libusb_free_device_list(list, false);
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::buildMaskedDeviceVector(uint16_t vidMask,
|
||||
uint16_t pidMask,
|
||||
uint16_t vid,
|
||||
uint16_t pid) {
|
||||
std::vector<std::shared_ptr<IUsbDevice>> matchedDevices;
|
||||
std::vector<std::unique_ptr<IUsbDevice>> UsbDeviceFactory::buildMaskedDeviceVector(uint16_t vidMask, uint16_t pidMask,
|
||||
uint16_t vid, uint16_t pid) {
|
||||
std::vector<std::unique_ptr<IUsbDevice>> matchedDevices;
|
||||
// see libusb/examples/listdevs.c
|
||||
int i = 0;
|
||||
libusb_device* currDev = nullptr;
|
||||
while ((currDev = mLibusbDeviceList[i++]) != nullptr) {
|
||||
struct libusb_device_descriptor currDevDesc;
|
||||
int ret = libusb_get_device_descriptor(currDev, &currDevDesc);
|
||||
for (auto& currDev : mLibusbDeviceList) {
|
||||
struct libusb_device_descriptor currDevDesc{};
|
||||
|
||||
int ret = libusb_get_device_descriptor(currDev.get(), &currDevDesc);
|
||||
spdlog::trace("Detected Device {:04x}:{:04x} ", currDevDesc.idVendor, currDevDesc.idProduct);
|
||||
if (ret < 0) {
|
||||
continue;
|
||||
}
|
||||
if (((currDevDesc.idVendor & vidMask) == vid) &&
|
||||
((currDevDesc.idProduct & pidMask) == pid)) {
|
||||
matchedDevices.push_back(std::make_shared<UsbDevice>(mLibusbCtx, currDev));
|
||||
if (((currDevDesc.idVendor & vidMask) == vid) && ((currDevDesc.idProduct & pidMask) == pid)) {
|
||||
matchedDevices.push_back(std::make_unique<UsbDevice>(mLibusbCtx, std::move(currDev)));
|
||||
}
|
||||
}
|
||||
return matchedDevices;
|
||||
|
||||
@@ -19,9 +19,16 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "libusb.h"
|
||||
#include "libusbwrap/UsbDevice.hpp"
|
||||
#include "libusbwrap/interface/IUsbDeviceFactory.hpp"
|
||||
|
||||
namespace libusbwrap {
|
||||
|
||||
constexpr const uint16_t LIBUSB_BITMASK_ALL = 0xffff;
|
||||
|
||||
class UsbDeviceFactory : public IUsbDeviceFactory {
|
||||
public:
|
||||
UsbDeviceFactory() = default;
|
||||
@@ -39,7 +46,7 @@ class UsbDeviceFactory : public IUsbDeviceFactory {
|
||||
*
|
||||
* @return std::vector<std::shared_ptr<IUsbDevice>> Vector of all detected USB devices
|
||||
*/
|
||||
std::vector<std::shared_ptr<IUsbDevice>> findAllDevices() override;
|
||||
std::vector<std::unique_ptr<IUsbDevice>> findAllDevices() override;
|
||||
/**
|
||||
* @brief Gets all devices with certain vid/pid combination.
|
||||
* If only one device of certain type is connected, vector is usually only one element
|
||||
@@ -48,17 +55,17 @@ class UsbDeviceFactory : public IUsbDeviceFactory {
|
||||
* @param pid ProductId of the devices to find
|
||||
* @return std::vector<std::shared_ptr<IUsbDevice>> Vector of detected USB devices based on vid/pid
|
||||
*/
|
||||
std::vector<std::shared_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) override;
|
||||
std::vector<std::unique_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) override;
|
||||
|
||||
private:
|
||||
// methods
|
||||
int refreshDeviceList();
|
||||
std::vector<std::shared_ptr<IUsbDevice>> buildMaskedDeviceVector(uint16_t vidMask,
|
||||
ssize_t refreshDeviceList();
|
||||
std::vector<std::unique_ptr<IUsbDevice>> buildMaskedDeviceVector(uint16_t vidMask,
|
||||
uint16_t pidMask, uint16_t vid,
|
||||
uint16_t pid);
|
||||
// members
|
||||
libusb_context* mLibusbCtx{nullptr};
|
||||
libusb_device** mLibusbDeviceList{};
|
||||
std::vector<usbDevice_ptr> mLibusbDeviceList{};
|
||||
bool mDeviceListInitialized = false;
|
||||
};
|
||||
} // namespace libusbwrap
|
||||
@@ -1,3 +1,22 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 2023-2024 Moritz Martinius
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <sys/types.h>
|
||||
@@ -29,11 +48,10 @@ class IUsbDevice {
|
||||
virtual void close() = 0;
|
||||
|
||||
// libusb wrappers
|
||||
virtual bool detachKernelDriver(int interfaceNo) = 0;
|
||||
virtual bool claimInterface(int interfaceNo) = 0;
|
||||
virtual bool releaseInterface(int interfaceNo) = 0;
|
||||
virtual bool bulkTransfer(uint8_t endpoint, std::vector<uint8_t>& data, int* tx,
|
||||
unsigned int timeout) = 0;
|
||||
virtual bool detachKernelDriver(int interfaceNo) = 0;
|
||||
virtual bool claimInterface(int interfaceNo) = 0;
|
||||
virtual bool releaseInterface(int interfaceNo) = 0;
|
||||
virtual bool bulkTransfer(uint8_t endpoint, const std::vector<uint8_t>& data, int* tx, unsigned int timeout) = 0;
|
||||
|
||||
// getters
|
||||
virtual const usbId getUsbId() = 0;
|
||||
|
||||
@@ -1,3 +1,22 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 2023 Moritz Martinius
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
@@ -9,8 +28,8 @@
|
||||
namespace libusbwrap {
|
||||
class IUsbDeviceFactory {
|
||||
public:
|
||||
virtual ~IUsbDeviceFactory() = default;
|
||||
virtual std::vector<std::shared_ptr<IUsbDevice>> findAllDevices() = 0;
|
||||
virtual std::vector<std::shared_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) = 0;
|
||||
virtual ~IUsbDeviceFactory() = default;
|
||||
virtual std::vector<std::unique_ptr<IUsbDevice>> findAllDevices() = 0;
|
||||
virtual std::vector<std::unique_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) = 0;
|
||||
};
|
||||
} // namespace libusbwrap
|
||||
Reference in New Issue
Block a user