2020-07-29 21:02:21 +03:00
|
|
|
/* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 2; -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef nsPrinterCUPS_h___
|
|
|
|
#define nsPrinterCUPS_h___
|
|
|
|
|
2020-08-04 15:06:37 +03:00
|
|
|
#include "nsPrinterBase.h"
|
2020-08-20 09:11:08 +03:00
|
|
|
#include "nsPrintSettingsImpl.h"
|
2020-07-29 21:02:21 +03:00
|
|
|
#include "nsCUPSShim.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
|
2020-09-10 02:51:59 +03:00
|
|
|
#include "mozilla/DataMutex.h"
|
2020-11-11 22:00:00 +03:00
|
|
|
#include "mozilla/FunctionRef.h"
|
2020-09-10 02:51:59 +03:00
|
|
|
#include "mozilla/RecursiveMutex.h"
|
|
|
|
|
2020-07-29 21:02:21 +03:00
|
|
|
/**
|
|
|
|
* @brief Interface to help implementing nsIPrinter using a CUPS printer.
|
|
|
|
*/
|
2020-08-04 15:06:37 +03:00
|
|
|
class nsPrinterCUPS final : public nsPrinterBase {
|
2020-07-29 21:02:21 +03:00
|
|
|
public:
|
2020-08-04 15:06:37 +03:00
|
|
|
NS_IMETHOD GetName(nsAString& aName) override;
|
2020-09-16 00:23:27 +03:00
|
|
|
NS_IMETHOD GetSystemName(nsAString& aName) override;
|
2020-08-04 15:06:37 +03:00
|
|
|
bool SupportsDuplex() const final;
|
|
|
|
bool SupportsColor() const final;
|
2020-09-07 21:58:48 +03:00
|
|
|
bool SupportsMonochrome() const final;
|
2020-08-14 23:41:59 +03:00
|
|
|
bool SupportsCollation() const final;
|
2020-11-03 23:35:38 +03:00
|
|
|
PrinterInfo CreatePrinterInfo() const final;
|
2020-09-17 23:05:08 +03:00
|
|
|
MarginDouble GetMarginsForPaper(nsString aPaperId) const final {
|
2020-08-05 14:26:13 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE(
|
|
|
|
"The CUPS API requires us to always get the margin when fetching the "
|
|
|
|
"paper list so there should be no need to query it separately");
|
|
|
|
return {};
|
|
|
|
}
|
2020-08-04 15:06:37 +03:00
|
|
|
|
2020-07-29 21:02:21 +03:00
|
|
|
nsPrinterCUPS() = delete;
|
|
|
|
|
2020-10-14 00:52:04 +03:00
|
|
|
nsPrinterCUPS(const mozilla::CommonPaperInfoArray* aArray,
|
|
|
|
const nsCUPSShim& aShim, nsString aDisplayName,
|
2020-09-10 02:56:13 +03:00
|
|
|
cups_dest_t* aPrinter)
|
2020-10-14 00:52:04 +03:00
|
|
|
: nsPrinterBase(aArray),
|
|
|
|
mShim(aShim),
|
2020-08-10 01:05:57 +03:00
|
|
|
mDisplayName(std::move(aDisplayName)),
|
2023-04-11 00:11:28 +03:00
|
|
|
mPrinterInfoMutex(CUPSPrinterInfo{aPrinter},
|
|
|
|
"nsPrinterCUPS::mPrinterInfoMutex") {}
|
2020-08-06 04:42:13 +03:00
|
|
|
|
2020-11-11 22:00:00 +03:00
|
|
|
static void ForEachExtraMonochromeSetting(
|
|
|
|
mozilla::FunctionRef<void(const nsACString&, const nsACString&)>);
|
|
|
|
|
2020-08-04 15:06:37 +03:00
|
|
|
private:
|
2020-09-10 02:56:13 +03:00
|
|
|
struct CUPSPrinterInfo {
|
2023-04-11 00:11:28 +03:00
|
|
|
explicit constexpr CUPSPrinterInfo(cups_dest_t* aPrinter)
|
|
|
|
: mPrinter(aPrinter) {}
|
|
|
|
cups_dest_t* mPrinter;
|
2020-09-10 02:56:13 +03:00
|
|
|
cups_dinfo_t* mPrinterInfo = nullptr;
|
|
|
|
uint64_t mCUPSMajor = 0;
|
|
|
|
uint64_t mCUPSMinor = 0;
|
|
|
|
uint64_t mCUPSPatch = 0;
|
2020-09-11 03:36:17 +03:00
|
|
|
|
2020-09-25 01:01:43 +03:00
|
|
|
// Whether we have attempted to fetch mPrinterInfo with CUPS_HTTP_DEFAULT.
|
|
|
|
bool mTriedInitWithDefault = false;
|
|
|
|
// Whether we have attempted to fetch mPrinterInfo with a connection.
|
|
|
|
bool mTriedInitWithConnection = false;
|
2023-04-11 00:11:28 +03:00
|
|
|
CUPSPrinterInfo() = delete;
|
2020-09-10 02:56:13 +03:00
|
|
|
CUPSPrinterInfo(const CUPSPrinterInfo&) = delete;
|
2023-04-11 00:11:28 +03:00
|
|
|
CUPSPrinterInfo(CUPSPrinterInfo&& aOther)
|
|
|
|
: mPrinter(aOther.mPrinter),
|
|
|
|
mPrinterInfo(aOther.mPrinterInfo),
|
|
|
|
mCUPSMajor(aOther.mCUPSMajor),
|
|
|
|
mCUPSMinor(aOther.mCUPSMinor),
|
|
|
|
mCUPSPatch(aOther.mCUPSPatch) {
|
|
|
|
aOther.mPrinter = nullptr;
|
|
|
|
aOther.mPrinterInfo = nullptr;
|
|
|
|
}
|
2020-09-10 02:56:13 +03:00
|
|
|
};
|
|
|
|
|
2020-09-10 02:51:59 +03:00
|
|
|
using PrinterInfoMutex =
|
2020-09-10 02:56:13 +03:00
|
|
|
mozilla::DataMutexBase<CUPSPrinterInfo, mozilla::RecursiveMutex>;
|
2020-09-10 02:51:59 +03:00
|
|
|
|
2020-11-13 22:58:25 +03:00
|
|
|
using PrinterInfoLock = PrinterInfoMutex::AutoLock;
|
|
|
|
|
2020-07-29 21:02:21 +03:00
|
|
|
~nsPrinterCUPS();
|
|
|
|
|
2020-08-20 09:11:08 +03:00
|
|
|
/**
|
|
|
|
* Retrieves the localized name for a given media (paper).
|
2020-08-22 04:43:33 +03:00
|
|
|
* Returns nullptr if the name cannot be localized.
|
2020-08-20 09:11:08 +03:00
|
|
|
*/
|
|
|
|
const char* LocalizeMediaName(http_t& aConnection, cups_size_t& aMedia) const;
|
|
|
|
|
|
|
|
void GetPrinterName(nsAString& aName) const;
|
|
|
|
|
2020-07-29 21:02:21 +03:00
|
|
|
// Little util for getting support flags using the direct CUPS names.
|
2020-09-03 02:49:08 +03:00
|
|
|
bool Supports(const char* aOption, const char* aValue) const;
|
|
|
|
|
|
|
|
// Returns support value if CUPS meets the minimum version, otherwise returns
|
|
|
|
// |aDefault|
|
|
|
|
bool IsCUPSVersionAtLeast(uint64_t aCUPSMajor, uint64_t aCUPSMinor,
|
|
|
|
uint64_t aCUPSPatch) const;
|
2020-07-29 21:02:21 +03:00
|
|
|
|
2023-04-11 00:11:28 +03:00
|
|
|
const char* FindCUPSOption(PrinterInfoLock& aLock, const char* name) const {
|
|
|
|
const cups_dest_t* const printer = aLock->mPrinter;
|
|
|
|
return mShim.cupsGetOption(name, printer->num_options, printer->options);
|
|
|
|
}
|
|
|
|
|
2020-11-03 23:35:38 +03:00
|
|
|
class Connection {
|
|
|
|
public:
|
|
|
|
http_t* GetConnection(cups_dest_t* aDest);
|
|
|
|
|
|
|
|
inline explicit Connection(const nsCUPSShim& aShim) : mShim(aShim) {}
|
|
|
|
Connection() = delete;
|
|
|
|
~Connection();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const nsCUPSShim& mShim;
|
|
|
|
http_t* mConnection = CUPS_HTTP_DEFAULT;
|
|
|
|
bool mWasInited = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
PrintSettingsInitializer DefaultSettings(Connection& aConnection) const;
|
|
|
|
nsTArray<mozilla::PaperInfo> PaperList(Connection& aConnection) const;
|
2020-09-25 01:01:43 +03:00
|
|
|
/**
|
|
|
|
* Attempts to populate the CUPSPrinterInfo object.
|
|
|
|
* This usually works with the CUPS default connection,
|
|
|
|
* but has been known to require an established connection
|
|
|
|
* on older versions of Ubuntu (18 and below).
|
|
|
|
*/
|
2020-11-13 22:58:25 +03:00
|
|
|
PrinterInfoLock TryEnsurePrinterInfo(
|
2023-04-11 00:11:28 +03:00
|
|
|
http_t* const aConnection = CUPS_HTTP_DEFAULT) const {
|
|
|
|
PrinterInfoLock lock = mPrinterInfoMutex.Lock();
|
|
|
|
TryEnsurePrinterInfo(lock, aConnection);
|
|
|
|
return lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TryEnsurePrinterInfo that uses a caller-provided PrinterInfoLock.
|
|
|
|
*
|
|
|
|
* This can be used to avoid unnecessarily redundant locking of
|
|
|
|
* mPrinterInfoLock when getting a connection through
|
|
|
|
* Connection::GetConnection and then passing that into TryEnsurePrinterInfo.
|
|
|
|
*/
|
|
|
|
void TryEnsurePrinterInfo(PrinterInfoLock& aLock,
|
|
|
|
http_t* const aConnection) const;
|
2020-09-10 02:56:13 +03:00
|
|
|
|
2020-07-29 21:02:21 +03:00
|
|
|
const nsCUPSShim& mShim;
|
2020-08-10 01:05:57 +03:00
|
|
|
nsString mDisplayName;
|
2020-09-10 02:51:59 +03:00
|
|
|
mutable PrinterInfoMutex mPrinterInfoMutex;
|
2020-07-29 21:02:21 +03:00
|
|
|
};
|
|
|
|
|
2020-10-27 19:23:26 +03:00
|
|
|
// There's no standard setting in Core Printing for monochrome. Or rather,
|
|
|
|
// there is (PMSetColorMode) but it does nothing. Similarly, the relevant gtk
|
|
|
|
// setting only works on Windows, yay.
|
|
|
|
//
|
|
|
|
// So on CUPS the right setting to use depends on the print driver. So we set /
|
|
|
|
// look for a variety of driver-specific keys that are known to work across
|
|
|
|
// printers.
|
|
|
|
//
|
|
|
|
// We set all the known settings, because the alternative to that is parsing ppd
|
|
|
|
// files from the printer and find the relevant known choices that can apply,
|
|
|
|
// and that is a lot more complex, similarly sketchy (requires the same amount
|
|
|
|
// of driver-specific knowledge), and requires using deprecated CUPS APIs.
|
2021-04-19 22:53:02 +03:00
|
|
|
#define CUPS_EACH_MONOCHROME_PRINTER_SETTING(macro_) \
|
|
|
|
macro_("ColorModel", "Gray") /* Generic */ \
|
|
|
|
macro_("BRMonoColor", "Mono") /* Brother */ \
|
|
|
|
macro_("BRPrintQuality", "Black") /* Brother */ \
|
|
|
|
macro_("CNIJGrayScale", "1") /* Canon */ \
|
2022-03-19 19:48:25 +03:00
|
|
|
macro_("CNGrayscale", "True") /* Canon */ \
|
2021-04-19 22:53:02 +03:00
|
|
|
macro_("INK", "MONO") /* Epson */ \
|
|
|
|
macro_("HPColorMode", "GrayscalePrint") /* HP */ \
|
|
|
|
macro_("ColorMode", "Mono") /* Samsung */ \
|
|
|
|
macro_("PrintoutMode", "Normal.Gray") /* Foomatic */ \
|
|
|
|
macro_("ProcessColorModel", "Mono") /* Samsung */ \
|
|
|
|
macro_("ARCMode", "CMBW") /* Sharp */ \
|
|
|
|
macro_("XRXColor", "BW") /* Xerox */ \
|
|
|
|
macro_("XROutputColor", "PrintAsGrayscale") /* Xerox, bug 1676191#c32 */ \
|
|
|
|
macro_("SelectColor", "Grayscale") /* Konica Minolta */ \
|
|
|
|
macro_("OKControl", "Gray") /* Oki */ \
|
|
|
|
macro_("BLW", "TrueM") /* Lexmark */ \
|
|
|
|
macro_("EPRendering", "None") /* Epson */
|
2020-10-27 19:23:26 +03:00
|
|
|
|
2020-07-29 21:02:21 +03:00
|
|
|
#endif /* nsPrinterCUPS_h___ */
|