1 /* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 2; -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsPrinterCUPS_h___
7 #define nsPrinterCUPS_h___
9 #include "nsPrinterBase.h"
10 #include "nsPrintSettingsImpl.h"
11 #include "nsCUPSShim.h"
14 #include "mozilla/DataMutex.h"
15 #include "mozilla/FunctionRef.h"
16 #include "mozilla/RecursiveMutex.h"
19 * @brief Interface to help implementing nsIPrinter using a CUPS printer.
21 class nsPrinterCUPS final
: public nsPrinterBase
{
23 NS_IMETHOD
GetName(nsAString
& aName
) override
;
24 NS_IMETHOD
GetSystemName(nsAString
& aName
) override
;
25 bool SupportsDuplex() const final
;
26 bool SupportsColor() const final
;
27 bool SupportsMonochrome() const final
;
28 bool SupportsCollation() const final
;
29 PrinterInfo
CreatePrinterInfo() const final
;
30 MarginDouble
GetMarginsForPaper(nsString aPaperId
) const final
{
31 MOZ_ASSERT_UNREACHABLE(
32 "The CUPS API requires us to always get the margin when fetching the "
33 "paper list so there should be no need to query it separately");
37 nsPrinterCUPS() = delete;
39 nsPrinterCUPS(const mozilla::CommonPaperInfoArray
* aArray
,
40 const nsCUPSShim
& aShim
, nsString aDisplayName
,
41 cups_dest_t
* aPrinter
)
42 : nsPrinterBase(aArray
),
44 mDisplayName(std::move(aDisplayName
)),
46 mPrinterInfoMutex("nsPrinterCUPS::mPrinterInfoMutex") {}
48 static void ForEachExtraMonochromeSetting(
49 mozilla::FunctionRef
<void(const nsACString
&, const nsACString
&)>);
51 inline const char* FindCUPSOption(const char* name
) const {
52 return mShim
.cupsGetOption(name
, mPrinter
->num_options
, mPrinter
->options
);
56 struct CUPSPrinterInfo
{
57 cups_dinfo_t
* mPrinterInfo
= nullptr;
58 uint64_t mCUPSMajor
= 0;
59 uint64_t mCUPSMinor
= 0;
60 uint64_t mCUPSPatch
= 0;
62 // Whether we have attempted to fetch mPrinterInfo with CUPS_HTTP_DEFAULT.
63 bool mTriedInitWithDefault
= false;
64 // Whether we have attempted to fetch mPrinterInfo with a connection.
65 bool mTriedInitWithConnection
= false;
66 CUPSPrinterInfo() = default;
67 CUPSPrinterInfo(const CUPSPrinterInfo
&) = delete;
68 CUPSPrinterInfo(CUPSPrinterInfo
&&) = delete;
71 using PrinterInfoMutex
=
72 mozilla::DataMutexBase
<CUPSPrinterInfo
, mozilla::RecursiveMutex
>;
74 using PrinterInfoLock
= PrinterInfoMutex::AutoLock
;
79 * Retrieves the localized name for a given media (paper).
80 * Returns nullptr if the name cannot be localized.
82 const char* LocalizeMediaName(http_t
& aConnection
, cups_size_t
& aMedia
) const;
84 void GetPrinterName(nsAString
& aName
) const;
86 // Little util for getting support flags using the direct CUPS names.
87 bool Supports(const char* aOption
, const char* aValue
) const;
89 // Returns support value if CUPS meets the minimum version, otherwise returns
91 bool IsCUPSVersionAtLeast(uint64_t aCUPSMajor
, uint64_t aCUPSMinor
,
92 uint64_t aCUPSPatch
) const;
96 http_t
* GetConnection(cups_dest_t
* aDest
);
98 inline explicit Connection(const nsCUPSShim
& aShim
) : mShim(aShim
) {}
99 Connection() = delete;
103 const nsCUPSShim
& mShim
;
104 http_t
* mConnection
= CUPS_HTTP_DEFAULT
;
105 bool mWasInited
= false;
108 PrintSettingsInitializer
DefaultSettings(Connection
& aConnection
) const;
109 nsTArray
<mozilla::PaperInfo
> PaperList(Connection
& aConnection
) const;
111 * Attempts to populate the CUPSPrinterInfo object.
112 * This usually works with the CUPS default connection,
113 * but has been known to require an established connection
114 * on older versions of Ubuntu (18 and below).
116 PrinterInfoLock
TryEnsurePrinterInfo(
117 http_t
* const aConnection
= CUPS_HTTP_DEFAULT
) const;
119 const nsCUPSShim
& mShim
;
120 nsString mDisplayName
;
121 cups_dest_t
* mPrinter
;
122 mutable PrinterInfoMutex mPrinterInfoMutex
;
125 // There's no standard setting in Core Printing for monochrome. Or rather,
126 // there is (PMSetColorMode) but it does nothing. Similarly, the relevant gtk
127 // setting only works on Windows, yay.
129 // So on CUPS the right setting to use depends on the print driver. So we set /
130 // look for a variety of driver-specific keys that are known to work across
133 // We set all the known settings, because the alternative to that is parsing ppd
134 // files from the printer and find the relevant known choices that can apply,
135 // and that is a lot more complex, similarly sketchy (requires the same amount
136 // of driver-specific knowledge), and requires using deprecated CUPS APIs.
137 #define CUPS_EACH_MONOCHROME_PRINTER_SETTING(macro_) \
138 macro_("ColorModel", "Gray") /* Generic */ \
139 macro_("BRMonoColor", "Mono") /* Brother */ \
140 macro_("BRPrintQuality", "Black") /* Brother */ \
141 macro_("CNIJGrayScale", "1") /* Canon */ \
142 macro_("CNGrayscale", "True") /* Canon */ \
143 macro_("INK", "MONO") /* Epson */ \
144 macro_("HPColorMode", "GrayscalePrint") /* HP */ \
145 macro_("ColorMode", "Mono") /* Samsung */ \
146 macro_("PrintoutMode", "Normal.Gray") /* Foomatic */ \
147 macro_("ProcessColorModel", "Mono") /* Samsung */ \
148 macro_("ARCMode", "CMBW") /* Sharp */ \
149 macro_("XRXColor", "BW") /* Xerox */ \
150 macro_("XROutputColor", "PrintAsGrayscale") /* Xerox, bug 1676191#c32 */ \
151 macro_("SelectColor", "Grayscale") /* Konica Minolta */ \
152 macro_("OKControl", "Gray") /* Oki */ \
153 macro_("BLW", "TrueM") /* Lexmark */ \
154 macro_("EPRendering", "None") /* Epson */
156 #endif /* nsPrinterCUPS_h___ */