4 * Color management routines for the CUPS scheduler.
6 * Copyright 2007-2012 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * Original DBUS/colord code is Copyright 2011 Red Hat, Inc.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
21 * Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
24 * Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
46 * Include necessary headers...
50 #include <cups/ppd-private.h>
53 # include <ApplicationServices/ApplicationServices.h>
54 extern CFUUIDRef
ColorSyncCreateUUIDFromUInt32(unsigned id
);
55 # include <CoreFoundation/CoreFoundation.h>
56 #elif defined(HAVE_DBUS)
57 # include <dbus/dbus.h>
60 * Defines used by colord. See the reference docs for further details:
62 * http://colord.hughsie.com/api/ref-dbus.html
65 # define COLORD_SCOPE_NORMAL "normal"
67 # define COLORD_SCOPE_TEMP "temp" /* Process scope */
68 # define COLORD_SCOPE_DISK "disk" /* Lives forever, as stored in DB */
70 # define COLORD_RELATION_SOFT "soft" /* Mapping is not default */
71 # define COLORD_RELATION_HARD "hard" /* Explicitly mapped profile */
73 # define COLORD_SPACE_RGB "rgb" /* RGB colorspace */
74 # define COLORD_SPACE_CMYK "cmyk" /* CMYK colorspace */
75 # define COLORD_SPACE_GRAY "gray" /* Gray colorspace */
76 # define COLORD_SPACE_UNKNOWN "unknown"
77 /* Unknown colorspace */
79 # define COLORD_MODE_PHYSICAL "physical"
81 # define COLORD_MODE_VIRTUAL "virtual"
82 /* Virtual device with no hardware */
84 # define COLORD_KIND_PRINTER "printer"
85 /* printing output device */
87 # define COLORD_DBUS_MSG(p,m) dbus_message_new_method_call(\
88 "org.freedesktop.ColorManager", (p),\
89 "org.freedesktop.ColorManager", (m))
90 /* Macro to make new colord messages */
91 # define COLORD_DBUS_PATH "/org/freedesktop/ColorManager"
92 /* Path for color management system */
93 # define COLORD_DBUS_TIMEOUT 5000 /* Timeout for connecting to colord in ms */
94 #endif /* __APPLE__ */
101 #if !defined(__APPLE__) && defined(HAVE_DBUS)
102 static DBusConnection
*colord_con
= NULL
;
103 /* DBUS connection for colord */
104 #endif /* !__APPLE__ && HAVE_DBUS */
112 static void apple_init_profile(ppd_file_t
*ppd
, cups_array_t
*languages
,
113 CFMutableDictionaryRef profile
,
114 unsigned id
, const char *name
,
115 const char *text
, const char *iccfile
);
116 static void apple_register_profiles(cupsd_printer_t
*p
);
117 static void apple_unregister_profiles(cupsd_printer_t
*p
);
119 #elif defined(HAVE_DBUS)
120 static void colord_create_device(cupsd_printer_t
*p
, ppd_file_t
*ppd
,
121 cups_array_t
*profiles
,
122 const char *colorspace
, char **format
,
123 const char *relation
, const char *scope
);
124 static void colord_create_profile(cups_array_t
*profiles
,
125 const char *printer_name
,
126 const char *qualifier
,
127 const char *colorspace
,
128 char **format
, const char *iccfile
,
130 static void colord_delete_device(const char *device_id
);
131 static void colord_device_add_profile(const char *device_path
,
132 const char *profile_path
,
133 const char *relation
);
134 static void colord_dict_add_strings(DBusMessageIter
*dict
,
135 const char *key
, const char *value
);
136 static char *colord_find_device(const char *device_id
);
137 static void colord_get_qualifier_format(ppd_file_t
*ppd
, char *format
[3]);
138 static void colord_register_printer(cupsd_printer_t
*p
);
139 static void colord_unregister_printer(cupsd_printer_t
*p
);
140 #endif /* __APPLE__ */
144 * 'cupsdRegisterColor()' - Register vendor color profiles in a PPD file.
148 cupsdRegisterColor(cupsd_printer_t
*p
) /* I - Printer */
151 apple_unregister_profiles(p
);
152 apple_register_profiles(p
);
154 #elif defined(HAVE_DBUS)
155 colord_unregister_printer(p
);
156 colord_register_printer(p
);
157 #endif /* __APPLE__ */
162 * 'cupsdStartColor()' - Initialize color management.
166 cupsdStartColor(void)
168 #if !defined(__APPLE__) && defined(HAVE_DBUS)
169 cupsd_printer_t
*p
; /* Current printer */
172 colord_con
= dbus_bus_get(DBUS_BUS_SYSTEM
, NULL
);
174 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
176 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
177 cupsdRegisterColor(p
);
178 #endif /* !__APPLE__ && HAVE_DBUS */
183 * 'cupsdStopColor()' - Shutdown color management.
189 #if !defined(__APPLE__) && defined(HAVE_DBUS)
190 dbus_connection_unref(colord_con
);
192 #endif /* !__APPLE__ && HAVE_DBUS */
197 * 'cupsdUnregisterColor()' - Unregister vendor color profiles in a PPD file.
201 cupsdUnregisterColor(cupsd_printer_t
*p
)/* I - Printer */
204 apple_unregister_profiles(p
);
206 #elif defined(HAVE_DBUS)
207 colord_unregister_printer(p
);
208 #endif /* __APPLE__ */
214 * 'apple_init_profile()' - Initialize a color profile.
219 ppd_file_t
*ppd
, /* I - PPD file */
220 cups_array_t
*languages
, /* I - Languages in the PPD file */
221 CFMutableDictionaryRef profile
, /* I - Profile dictionary */
222 unsigned id
, /* I - Profile ID */
223 const char *name
, /* I - Profile name */
224 const char *text
, /* I - Profile UI text */
225 const char *iccfile
) /* I - ICC filename */
227 CFURLRef url
; /* URL for profile filename */
228 CFMutableDictionaryRef dict
; /* Dictionary for name */
229 char *language
; /* Current language */
230 ppd_attr_t
*attr
; /* Profile attribute */
231 CFStringRef cflang
, /* Language string */
232 cftext
; /* Localized text */
238 * Build the profile name dictionary...
241 dict
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
242 &kCFTypeDictionaryKeyCallBacks
,
243 &kCFTypeDictionaryValueCallBacks
);
246 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to initialize profile \"%s\".",
251 cftext
= CFStringCreateWithCString(kCFAllocatorDefault
, text
,
252 kCFStringEncodingUTF8
);
256 CFDictionarySetValue(dict
, CFSTR("en_US"), cftext
);
263 * Find localized names for the color profiles...
266 cupsArraySave(ppd
->sorted_attrs
);
268 for (language
= (char *)cupsArrayFirst(languages
);
270 language
= (char *)cupsArrayNext(languages
))
274 if ((attr
= _ppdLocalizedAttr(ppd
, "cupsICCProfile", name
,
276 attr
= _ppdLocalizedAttr(ppd
, "APTiogaProfile", name
, language
);
279 attr
= _ppdLocalizedAttr(ppd
, "ColorModel", name
, language
);
281 if (attr
&& attr
->text
[0])
283 cflang
= CFStringCreateWithCString(kCFAllocatorDefault
, language
,
284 kCFStringEncodingUTF8
);
285 cftext
= CFStringCreateWithCString(kCFAllocatorDefault
, attr
->text
,
286 kCFStringEncodingUTF8
);
288 if (cflang
&& cftext
)
289 CFDictionarySetValue(dict
, cflang
, cftext
);
299 cupsArrayRestore(ppd
->sorted_attrs
);
303 * Fill in the profile data...
308 url
= CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault
,
309 (const UInt8
*)iccfile
,
310 strlen(iccfile
), false);
314 CFDictionarySetValue(profile
, kColorSyncDeviceProfileURL
, url
);
319 CFDictionarySetValue(profile
, kColorSyncDeviceModeDescriptions
, dict
);
325 * 'apple_register_profiles()' - Register color profiles for a printer.
329 apple_register_profiles(
330 cupsd_printer_t
*p
) /* I - Printer */
332 int i
; /* Looping var */
333 char ppdfile
[1024], /* PPD filename */
334 iccfile
[1024], /* ICC filename */
335 selector
[PPD_MAX_NAME
];
336 /* Profile selection string */
337 ppd_file_t
*ppd
; /* PPD file */
338 ppd_attr_t
*attr
, /* Profile attributes */
339 *profileid_attr
,/* cupsProfileID attribute */
340 *q1_attr
, /* ColorModel (or other) qualifier */
341 *q2_attr
, /* MediaType (or other) qualifier */
342 *q3_attr
; /* Resolution (or other) qualifier */
343 char q_keyword
[PPD_MAX_NAME
];
344 /* Qualifier keyword */
345 const char *q1_choice
, /* ColorModel (or other) choice */
346 *q2_choice
, /* MediaType (or other) choice */
347 *q3_choice
; /* Resolution (or other) choice */
348 ppd_option_t
*cm_option
; /* Color model option */
349 ppd_choice_t
*cm_choice
; /* Color model choice */
350 int num_profiles
; /* Number of profiles */
351 OSStatus error
= 0; /* Last error */
352 unsigned device_id
, /* Printer device ID */
353 profile_id
= 0, /* Profile ID */
354 default_profile_id
= 0;
355 /* Default profile ID */
356 CFMutableDictionaryRef device_name
; /* Printer device name dictionary */
357 CFStringRef printer_name
; /* Printer name string */
358 cups_array_t
*languages
; /* Languages array */
359 CFMutableDictionaryRef profiles
, /* Dictionary of profiles */
360 profile
; /* Current profile info dictionary */
361 CFStringRef dict_key
; /* Key in factory profile dictionary */
365 * Make sure ColorSync is available...
368 if (ColorSyncRegisterDevice
== NULL
)
372 * Try opening the PPD file for this printer...
375 snprintf(ppdfile
, sizeof(ppdfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
376 if ((ppd
= _ppdOpenFile(ppdfile
, _PPD_LOCALIZATION_ICC_PROFILES
)) == NULL
)
380 * See if we have any profiles...
383 for (num_profiles
= 0, attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
385 attr
= ppdFindNextAttr(ppd
, "cupsICCProfile", NULL
))
386 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
388 if (attr
->value
[0] != '/')
389 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
392 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
394 if (access(iccfile
, 0))
396 cupsdLogMessage(CUPSD_LOG_ERROR
,
397 "%s: ICC Profile \"%s\" does not exist.", p
->name
,
399 cupsdSetPrinterReasons(p
, "+cups-missing-filter-warning");
407 * Create a dictionary for the factory profiles...
410 profiles
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
411 &kCFTypeDictionaryKeyCallBacks
,
412 &kCFTypeDictionaryValueCallBacks
);
415 cupsdLogMessage(CUPSD_LOG_ERROR
,
416 "Unable to allocate memory for factory profiles.");
422 * If we have profiles, add them...
425 if (num_profiles
> 0)
428 * For CUPS PPDs, figure out the default profile selector values...
431 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier1", NULL
)) != NULL
&&
432 attr
->value
&& attr
->value
[0])
434 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
435 q1_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
437 else if ((q1_attr
= ppdFindAttr(ppd
, "DefaultColorModel", NULL
)) == NULL
)
438 q1_attr
= ppdFindAttr(ppd
, "DefaultColorSpace", NULL
);
440 if (q1_attr
&& q1_attr
->value
&& q1_attr
->value
[0])
441 q1_choice
= q1_attr
->value
;
445 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier2", NULL
)) != NULL
&&
446 attr
->value
&& attr
->value
[0])
448 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
449 q2_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
452 q2_attr
= ppdFindAttr(ppd
, "DefaultMediaType", NULL
);
454 if (q2_attr
&& q2_attr
->value
&& q2_attr
->value
[0])
455 q2_choice
= q2_attr
->value
;
459 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier3", NULL
)) != NULL
&&
460 attr
->value
&& attr
->value
[0])
462 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
463 q3_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
466 q3_attr
= ppdFindAttr(ppd
, "DefaultResolution", NULL
);
468 if (q3_attr
&& q3_attr
->value
&& q3_attr
->value
[0])
469 q3_choice
= q3_attr
->value
;
474 * Loop through the profiles listed in the PPD...
477 languages
= _ppdGetLanguages(ppd
);
479 for (attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
481 attr
= ppdFindNextAttr(ppd
, "cupsICCProfile", NULL
))
482 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
485 * Add this profile...
488 if (attr
->value
[0] != '/')
489 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
492 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
494 if (_cupsFileCheck(iccfile
, _CUPS_FILE_CHECK_FILE
, !RunUser
,
495 cupsdLogFCMessage
, p
))
498 cupsArraySave(ppd
->sorted_attrs
);
500 if ((profileid_attr
= ppdFindAttr(ppd
, "cupsProfileID",
501 attr
->spec
)) != NULL
&&
502 profileid_attr
->value
&& isdigit(profileid_attr
->value
[0] & 255))
503 profile_id
= (unsigned)strtoul(profileid_attr
->value
, NULL
, 10);
505 profile_id
= _ppdHashName(attr
->spec
);
507 cupsArrayRestore(ppd
->sorted_attrs
);
509 profile
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
510 &kCFTypeDictionaryKeyCallBacks
,
511 &kCFTypeDictionaryValueCallBacks
);
514 cupsdLogMessage(CUPSD_LOG_ERROR
,
515 "Unable to allocate memory for color profile.");
521 apple_init_profile(ppd
, languages
, profile
, profile_id
, attr
->spec
,
522 attr
->text
[0] ? attr
->text
: attr
->spec
, iccfile
);
524 dict_key
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
,
525 CFSTR("%u"), profile_id
);
528 CFDictionarySetValue(profiles
, dict_key
, profile
);
535 * See if this is the default profile...
538 if (!default_profile_id
&& q1_choice
&& q2_choice
&& q3_choice
)
540 snprintf(selector
, sizeof(selector
), "%s.%s.%s", q1_choice
, q2_choice
,
542 if (!strcmp(selector
, attr
->spec
))
543 default_profile_id
= profile_id
;
546 if (!default_profile_id
&& q1_choice
&& q2_choice
)
548 snprintf(selector
, sizeof(selector
), "%s.%s.", q1_choice
, q2_choice
);
549 if (!strcmp(selector
, attr
->spec
))
550 default_profile_id
= profile_id
;
553 if (!default_profile_id
&& q1_choice
&& q3_choice
)
555 snprintf(selector
, sizeof(selector
), "%s..%s", q1_choice
, q3_choice
);
556 if (!strcmp(selector
, attr
->spec
))
557 default_profile_id
= profile_id
;
560 if (!default_profile_id
&& q1_choice
)
562 snprintf(selector
, sizeof(selector
), "%s..", q1_choice
);
563 if (!strcmp(selector
, attr
->spec
))
564 default_profile_id
= profile_id
;
567 if (!default_profile_id
&& q2_choice
&& q3_choice
)
569 snprintf(selector
, sizeof(selector
), ".%s.%s", q2_choice
, q3_choice
);
570 if (!strcmp(selector
, attr
->spec
))
571 default_profile_id
= profile_id
;
574 if (!default_profile_id
&& q2_choice
)
576 snprintf(selector
, sizeof(selector
), ".%s.", q2_choice
);
577 if (!strcmp(selector
, attr
->spec
))
578 default_profile_id
= profile_id
;
581 if (!default_profile_id
&& q3_choice
)
583 snprintf(selector
, sizeof(selector
), "..%s", q3_choice
);
584 if (!strcmp(selector
, attr
->spec
))
585 default_profile_id
= profile_id
;
589 _ppdFreeLanguages(languages
);
591 else if ((cm_option
= ppdFindOption(ppd
, "ColorModel")) != NULL
)
594 * Extract profiles from ColorModel option...
597 const char *profile_name
; /* Name of generic profile */
600 num_profiles
= cm_option
->num_choices
;
602 for (i
= cm_option
->num_choices
, cm_choice
= cm_option
->choices
;
606 if (!strcmp(cm_choice
->choice
, "Gray") ||
607 !strcmp(cm_choice
->choice
, "Black"))
608 profile_name
= "Gray";
609 else if (!strcmp(cm_choice
->choice
, "RGB") ||
610 !strcmp(cm_choice
->choice
, "CMY"))
611 profile_name
= "RGB";
612 else if (!strcmp(cm_choice
->choice
, "CMYK") ||
613 !strcmp(cm_choice
->choice
, "KCMY"))
614 profile_name
= "CMYK";
616 profile_name
= "DeviceN";
618 snprintf(selector
, sizeof(selector
), "%s..", profile_name
);
619 profile_id
= _ppdHashName(selector
);
621 profile
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
622 &kCFTypeDictionaryKeyCallBacks
,
623 &kCFTypeDictionaryValueCallBacks
);
626 cupsdLogMessage(CUPSD_LOG_ERROR
,
627 "Unable to allocate memory for color profile.");
633 apple_init_profile(ppd
, NULL
, profile
, profile_id
, cm_choice
->choice
,
634 cm_choice
->text
, NULL
);
636 dict_key
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
,
637 CFSTR("%u"), profile_id
);
640 CFDictionarySetValue(profiles
, dict_key
, profile
);
646 if (cm_choice
->marked
)
647 default_profile_id
= profile_id
;
653 * Use the default colorspace...
656 attr
= ppdFindAttr(ppd
, "DefaultColorSpace", NULL
);
658 num_profiles
= (attr
&& ppd
->colorspace
== PPD_CS_GRAY
) ? 1 : 2;
661 * Add the grayscale profile first. We always have a grayscale profile.
664 profile
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
665 &kCFTypeDictionaryKeyCallBacks
,
666 &kCFTypeDictionaryValueCallBacks
);
670 cupsdLogMessage(CUPSD_LOG_ERROR
,
671 "Unable to allocate memory for color profile.");
677 profile_id
= _ppdHashName("Gray..");
678 apple_init_profile(ppd
, NULL
, profile
, profile_id
, "Gray", "Gray", NULL
);
680 dict_key
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("%u"),
684 CFDictionarySetValue(profiles
, dict_key
, profile
);
691 * Then add the RGB/CMYK/DeviceN color profile...
694 profile
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
695 &kCFTypeDictionaryKeyCallBacks
,
696 &kCFTypeDictionaryValueCallBacks
);
700 cupsdLogMessage(CUPSD_LOG_ERROR
,
701 "Unable to allocate memory for color profile.");
707 switch (ppd
->colorspace
)
712 profile_id
= _ppdHashName("RGB..");
713 apple_init_profile(ppd
, NULL
, profile
, profile_id
, "RGB", "RGB",
719 profile_id
= _ppdHashName("CMYK..");
720 apple_init_profile(ppd
, NULL
, profile
, profile_id
, "CMYK", "CMYK",
729 profile_id
= _ppdHashName("DeviceN..");
730 apple_init_profile(ppd
, NULL
, profile
, profile_id
, "DeviceN",
735 if (CFDictionaryGetCount(profile
) > 0)
737 dict_key
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
,
738 CFSTR("%u"), profile_id
);
741 CFDictionarySetValue(profiles
, dict_key
, profile
);
749 if (num_profiles
> 0)
752 * Make sure we have a default profile ID...
755 if (!default_profile_id
)
756 default_profile_id
= profile_id
; /* Last profile */
758 dict_key
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("%u"),
762 CFDictionarySetValue(profiles
, kColorSyncDeviceDefaultProfileID
,
768 * Get the device ID hash and pathelogical name dictionary.
771 cupsdLogMessage(CUPSD_LOG_INFO
, "Registering ICC color profiles for \"%s\"",
774 device_id
= _ppdHashName(p
->name
);
775 device_name
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
776 &kCFTypeDictionaryKeyCallBacks
,
777 &kCFTypeDictionaryValueCallBacks
);
778 printer_name
= CFStringCreateWithCString(kCFAllocatorDefault
,
779 p
->name
, kCFStringEncodingUTF8
);
781 if (device_name
&& printer_name
)
784 * Register the device with ColorSync...
787 CFTypeRef deviceDictKeys
[] =
789 kColorSyncDeviceDescriptions
,
790 kColorSyncFactoryProfiles
,
791 kColorSyncDeviceUserScope
,
792 kColorSyncDeviceHostScope
794 CFTypeRef deviceDictVals
[] =
795 { /* Device values */
798 kCFPreferencesAnyUser
,
799 kCFPreferencesCurrentHost
801 CFDictionaryRef deviceDict
; /* Device dictionary */
802 CFUUIDRef deviceUUID
; /* Device UUID */
804 CFDictionarySetValue(device_name
, CFSTR("en_US"), printer_name
);
806 deviceDict
= CFDictionaryCreate(kCFAllocatorDefault
,
807 (const void **)deviceDictKeys
,
808 (const void **)deviceDictVals
,
809 sizeof(deviceDictKeys
) /
810 sizeof(deviceDictKeys
[0]),
811 &kCFTypeDictionaryKeyCallBacks
,
812 &kCFTypeDictionaryValueCallBacks
);
813 deviceUUID
= ColorSyncCreateUUIDFromUInt32(device_id
);
815 if (!deviceDict
|| !deviceUUID
||
816 !ColorSyncRegisterDevice(kColorSyncPrinterDeviceClass
, deviceUUID
,
821 CFRelease(deviceUUID
);
824 CFRelease(deviceDict
);
834 cupsdLogMessage(CUPSD_LOG_ERROR
,
835 "Unable to register ICC color profiles for \"%s\": %d",
836 p
->name
, (int)error
);
839 CFRelease(printer_name
);
842 CFRelease(device_name
);
846 * Free any memory we used...
856 * 'apple_unregister_profiles()' - Remove color profiles for the specified
861 apple_unregister_profiles(
862 cupsd_printer_t
*p
) /* I - Printer */
865 * Make sure ColorSync is available...
868 if (ColorSyncUnregisterDevice
!= NULL
)
870 CFUUIDRef deviceUUID
; /* Device UUID */
872 deviceUUID
= ColorSyncCreateUUIDFromUInt32(_ppdHashName(p
->name
));
875 ColorSyncUnregisterDevice(kColorSyncPrinterDeviceClass
, deviceUUID
);
876 CFRelease(deviceUUID
);
882 #elif defined(HAVE_DBUS)
884 * 'colord_create_device()' - Create a device and register profiles.
888 colord_create_device(
889 cupsd_printer_t
*p
, /* I - Printer */
890 ppd_file_t
*ppd
, /* I - PPD file */
891 cups_array_t
*profiles
, /* I - Profiles array */
892 const char *colorspace
, /* I - Device colorspace, e.g. 'rgb' */
893 char **format
, /* I - Device qualifier format */
894 const char *relation
, /* I - Profile relation, either 'soft'
896 const char *scope
) /* I - The scope of the device, e.g.
897 'normal', 'temp' or 'disk' */
899 DBusMessage
*message
= NULL
; /* D-Bus request */
900 DBusMessage
*reply
= NULL
; /* D-Bus reply */
901 DBusMessageIter args
; /* D-Bus method arguments */
902 DBusMessageIter dict
; /* D-Bus method arguments */
903 DBusError error
; /* D-Bus error */
904 const char *device_path
; /* Device object path */
905 const char *profile_path
; /* Profile path */
906 char *default_profile_path
= NULL
;
907 /* Default profile path */
908 char device_id
[1024]; /* Device ID as understood by colord */
909 char format_str
[1024]; /* Qualifier format as a string */
913 * Create the device...
916 snprintf(device_id
, sizeof(device_id
), "cups-%s", p
->name
);
917 device_path
= device_id
;
919 message
= COLORD_DBUS_MSG(COLORD_DBUS_PATH
, "CreateDevice");
921 dbus_message_iter_init_append(message
, &args
);
922 dbus_message_iter_append_basic(&args
, DBUS_TYPE_STRING
, &device_path
);
923 dbus_message_iter_append_basic(&args
, DBUS_TYPE_STRING
, &scope
);
925 snprintf(format_str
, sizeof(format_str
), "%s.%s.%s", format
[0], format
[1],
928 dbus_message_iter_open_container(&args
, DBUS_TYPE_ARRAY
, "{ss}", &dict
);
929 colord_dict_add_strings(&dict
, "Colorspace", colorspace
);
930 colord_dict_add_strings(&dict
, "Mode", COLORD_MODE_PHYSICAL
);
931 if (ppd
->manufacturer
)
932 colord_dict_add_strings(&dict
, "Vendor", ppd
->manufacturer
);
934 colord_dict_add_strings(&dict
, "Model", ppd
->modelname
);
935 if (p
->sanitized_device_uri
)
936 colord_dict_add_strings(&dict
, "Serial", p
->sanitized_device_uri
);
937 colord_dict_add_strings(&dict
, "Format", format_str
);
938 colord_dict_add_strings(&dict
, "Kind", COLORD_KIND_PRINTER
);
939 dbus_message_iter_close_container(&args
, &dict
);
942 * Send the CreateDevice request synchronously...
945 dbus_error_init(&error
);
946 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Calling CreateDevice(%s,%s)", device_id
,
948 reply
= dbus_connection_send_with_reply_and_block(colord_con
, message
,
953 cupsdLogMessage(CUPSD_LOG_WARN
, "CreateDevice failed: %s:%s", error
.name
,
955 dbus_error_free(&error
);
963 dbus_message_iter_init(reply
, &args
);
964 if (dbus_message_iter_get_arg_type(&args
) != DBUS_TYPE_OBJECT_PATH
)
966 cupsdLogMessage(CUPSD_LOG_WARN
,
967 "CreateDevice failed: Incorrect reply type.");
971 dbus_message_iter_get_basic(&args
, &device_path
);
972 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Created device \"%s\".", device_path
);
978 for (profile_path
= cupsArrayFirst(profiles
);
980 profile_path
= cupsArrayNext(profiles
))
982 colord_device_add_profile(device_path
, profile_path
, relation
);
987 if (default_profile_path
)
988 free(default_profile_path
);
991 dbus_message_unref(message
);
994 dbus_message_unref(reply
);
999 * 'colord_create_profile()' - Create a color profile for a printer.
1003 colord_create_profile(
1004 cups_array_t
*profiles
, /* I - Profiles array */
1005 const char *printer_name
, /* I - Printer name */
1006 const char *qualifier
, /* I - Profile qualifier */
1007 const char *colorspace
, /* I - Profile colorspace */
1008 char **format
, /* I - Profile qualifier format */
1009 const char *iccfile
, /* I - ICC filename */
1010 const char *scope
) /* I - The scope of the profile, e.g.
1011 'normal', 'temp' or 'disk' */
1013 DBusMessage
*message
= NULL
; /* D-Bus request */
1014 DBusMessage
*reply
= NULL
; /* D-Bus reply */
1015 DBusMessageIter args
; /* D-Bus method arguments */
1016 DBusMessageIter dict
; /* D-Bus method arguments */
1017 DBusError error
; /* D-Bus error */
1018 char *idstr
; /* Profile ID string */
1019 size_t idstrlen
; /* Profile ID allocated length */
1020 const char *profile_path
; /* Device object path */
1021 char format_str
[1024]; /* Qualifier format as a string */
1025 * Create the profile...
1028 message
= COLORD_DBUS_MSG(COLORD_DBUS_PATH
, "CreateProfile");
1030 idstrlen
= strlen(printer_name
) + 1 + strlen(qualifier
) + 1;
1031 if ((idstr
= malloc(idstrlen
)) == NULL
)
1033 snprintf(idstr
, idstrlen
, "%s-%s", printer_name
, qualifier
);
1034 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Using profile ID \"%s\".", idstr
);
1036 dbus_message_iter_init_append(message
, &args
);
1037 dbus_message_iter_append_basic(&args
, DBUS_TYPE_STRING
, &idstr
);
1038 dbus_message_iter_append_basic(&args
, DBUS_TYPE_STRING
, &scope
);
1040 snprintf(format_str
, sizeof(format_str
), "%s.%s.%s", format
[0], format
[1],
1043 dbus_message_iter_open_container(&args
, DBUS_TYPE_ARRAY
, "{ss}", &dict
);
1044 colord_dict_add_strings(&dict
, "Qualifier", qualifier
);
1045 colord_dict_add_strings(&dict
, "Format", format_str
);
1046 colord_dict_add_strings(&dict
, "Colorspace", colorspace
);
1048 colord_dict_add_strings(&dict
, "Filename", iccfile
);
1049 dbus_message_iter_close_container(&args
, &dict
);
1052 * Send the CreateProfile request synchronously...
1055 dbus_error_init(&error
);
1056 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Calling CreateProfile(%s,%s)", idstr
,
1058 reply
= dbus_connection_send_with_reply_and_block(colord_con
, message
,
1059 COLORD_DBUS_TIMEOUT
,
1063 cupsdLogMessage(CUPSD_LOG_WARN
, "CreateProfile failed: %s:%s", error
.name
,
1065 dbus_error_free(&error
);
1073 dbus_message_iter_init(reply
, &args
);
1074 if (dbus_message_iter_get_arg_type(&args
) != DBUS_TYPE_OBJECT_PATH
)
1076 cupsdLogMessage(CUPSD_LOG_WARN
,
1077 "CreateProfile failed: Incorrect reply type.");
1081 dbus_message_iter_get_basic(&args
, &profile_path
);
1082 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Created profile \"%s\".", profile_path
);
1083 cupsArrayAdd(profiles
, strdup(profile_path
));
1088 dbus_message_unref(message
);
1091 dbus_message_unref(reply
);
1099 * 'colord_delete_device()' - Delete a device
1103 colord_delete_device(
1104 const char *device_id
) /* I - Device ID string */
1106 DBusMessage
*message
= NULL
; /* D-Bus request */
1107 DBusMessage
*reply
= NULL
; /* D-Bus reply */
1108 DBusMessageIter args
; /* D-Bus method arguments */
1109 DBusError error
; /* D-Bus error */
1110 char *device_path
; /* Device object path */
1114 * Find the device...
1117 if ((device_path
= colord_find_device(device_id
)) == NULL
)
1121 * Delete the device...
1124 message
= COLORD_DBUS_MSG(COLORD_DBUS_PATH
, "DeleteDevice");
1126 dbus_message_iter_init_append(message
, &args
);
1127 dbus_message_iter_append_basic(&args
, DBUS_TYPE_STRING
, &device_id
);
1130 * Send the DeleteDevice request synchronously...
1133 dbus_error_init(&error
);
1134 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Calling DeleteDevice(%s)", device_id
);
1135 reply
= dbus_connection_send_with_reply_and_block(colord_con
, message
,
1136 COLORD_DBUS_TIMEOUT
,
1140 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DeleteDevice failed: %s:%s", error
.name
,
1142 dbus_error_free(&error
);
1152 dbus_message_unref(message
);
1155 dbus_message_unref(reply
);
1160 * 'colord_device_add_profile()' - Assign a profile to a device.
1164 colord_device_add_profile(
1165 const char *device_path
, /* I - Device object path */
1166 const char *profile_path
, /* I - Profile object path */
1167 const char *relation
) /* I - Device relation, either
1170 DBusMessage
*message
= NULL
; /* D-Bus request */
1171 DBusMessage
*reply
= NULL
; /* D-Bus reply */
1172 DBusMessageIter args
; /* D-Bus method arguments */
1173 DBusError error
; /* D-Bus error */
1176 message
= COLORD_DBUS_MSG(device_path
, "AddProfile");
1178 dbus_message_iter_init_append(message
, &args
);
1179 dbus_message_iter_append_basic(&args
, DBUS_TYPE_STRING
, &relation
);
1180 dbus_message_iter_append_basic(&args
, DBUS_TYPE_OBJECT_PATH
, &profile_path
);
1181 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Calling %s:AddProfile(%s) [%s]",
1182 device_path
, profile_path
, relation
);
1185 * Send the AddProfile request synchronously...
1188 dbus_error_init(&error
);
1189 reply
= dbus_connection_send_with_reply_and_block(colord_con
, message
,
1190 COLORD_DBUS_TIMEOUT
,
1194 cupsdLogMessage(CUPSD_LOG_WARN
, "AddProfile failed: %s:%s", error
.name
,
1196 dbus_error_free(&error
);
1203 dbus_message_unref(message
);
1206 dbus_message_unref(reply
);
1211 * 'colord_dict_add_strings()' - Add two strings to a dictionary.
1215 colord_dict_add_strings(
1216 DBusMessageIter
*dict
, /* I - Dictionary */
1217 const char *key
, /* I - Key string */
1218 const char *value
) /* I - Value string */
1220 DBusMessageIter entry
; /* Entry to add */
1223 dbus_message_iter_open_container(dict
, DBUS_TYPE_DICT_ENTRY
, NULL
, &entry
);
1224 dbus_message_iter_append_basic(&entry
, DBUS_TYPE_STRING
, &key
);
1225 dbus_message_iter_append_basic(&entry
, DBUS_TYPE_STRING
, &value
);
1226 dbus_message_iter_close_container(dict
, &entry
);
1231 * 'colord_find_device()' - Finds a device
1234 static char * /* O - Device path or NULL */
1236 const char *device_id
) /* I - Device ID string */
1238 DBusMessage
*message
= NULL
; /* D-Bus request */
1239 DBusMessage
*reply
= NULL
; /* D-Bus reply */
1240 DBusMessageIter args
; /* D-Bus method arguments */
1241 DBusError error
; /* D-Bus error */
1242 const char *device_path_tmp
; /* Device object path */
1243 char *device_path
= NULL
; /* Device object path */
1246 message
= COLORD_DBUS_MSG(COLORD_DBUS_PATH
, "FindDeviceById");
1248 dbus_message_iter_init_append(message
, &args
);
1249 dbus_message_iter_append_basic(&args
, DBUS_TYPE_STRING
, &device_id
);
1252 * Send the FindDeviceById request synchronously...
1255 dbus_error_init(&error
);
1256 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Calling FindDeviceById(%s)", device_id
);
1257 reply
= dbus_connection_send_with_reply_and_block(colord_con
, message
,
1258 COLORD_DBUS_TIMEOUT
,
1262 cupsdLogMessage(CUPSD_LOG_DEBUG
, "FindDeviceById failed: %s:%s",
1263 error
.name
, error
.message
);
1264 dbus_error_free(&error
);
1272 dbus_message_iter_init(reply
, &args
);
1273 if (dbus_message_iter_get_arg_type(&args
) != DBUS_TYPE_OBJECT_PATH
)
1275 cupsdLogMessage(CUPSD_LOG_WARN
,
1276 "FindDeviceById failed: Incorrect reply type.");
1280 dbus_message_iter_get_basic(&args
, &device_path_tmp
);
1281 if (device_path_tmp
)
1282 device_path
= strdup(device_path_tmp
);
1287 dbus_message_unref(message
);
1290 dbus_message_unref(reply
);
1292 return (device_path
);
1297 * 'colord_get_qualifier_format()' - Get the qualifier format.
1299 * Note: Returns a value of "ColorSpace.MediaType.Resolution" by default.
1303 colord_get_qualifier_format(
1304 ppd_file_t
*ppd
, /* I - PPD file data */
1305 char *format
[3]) /* I - Format tuple */
1307 const char *tmp
; /* Temporary string */
1308 ppd_attr_t
*attr
; /* Profile attributes */
1312 * Get 1st section...
1315 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier1", NULL
)) != NULL
)
1317 else if (ppdFindAttr(ppd
, "DefaultColorModel", NULL
))
1319 else if (ppdFindAttr(ppd
, "DefaultColorSpace", NULL
))
1324 format
[0] = strdup(tmp
);
1327 * Get 2nd section...
1330 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier2", NULL
)) != NULL
)
1335 format
[1] = strdup(tmp
);
1338 * Get 3rd section...
1341 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier3", NULL
)) != NULL
)
1346 format
[2] = strdup(tmp
);
1351 * 'colord_register_printer()' - Register profiles for a printer.
1355 colord_register_printer(
1356 cupsd_printer_t
*p
) /* I - printer */
1358 char ppdfile
[1024], /* PPD filename */
1359 iccfile
[1024]; /* ICC filename */
1360 ppd_file_t
*ppd
; /* PPD file */
1361 cups_array_t
*profiles
; /* Profile paths array */
1362 ppd_attr_t
*attr
; /* Profile attributes */
1363 const char *device_colorspace
; /* Device colorspace */
1364 char *format
[3]; /* Qualifier format tuple */
1368 * Ensure we have a D-Bus connection...
1375 * Try opening the PPD file for this printer...
1378 snprintf(ppdfile
, sizeof(ppdfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
1379 if ((ppd
= _ppdOpenFile(ppdfile
, _PPD_LOCALIZATION_ICC_PROFILES
)) == NULL
)
1383 * Find out the qualifier format
1386 colord_get_qualifier_format(ppd
, format
);
1389 * See if we have any embedded profiles...
1392 profiles
= cupsArrayNew3(NULL
, NULL
, NULL
, 0, (cups_acopy_func_t
)strdup
,
1393 (cups_afree_func_t
)free
);
1394 for (attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
1396 attr
= ppdFindNextAttr(ppd
, "cupsICCProfile", NULL
))
1397 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
1399 if (attr
->value
[0] != '/')
1400 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
1403 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
1405 if (_cupsFileCheck(iccfile
, _CUPS_FILE_CHECK_FILE
, !RunUser
,
1406 cupsdLogFCMessage
, p
))
1409 colord_create_profile(profiles
, p
->name
, attr
->spec
, COLORD_SPACE_UNKNOWN
,
1410 format
, iccfile
, COLORD_SCOPE_TEMP
);
1414 * Add the grayscale profile first. We always have a grayscale profile.
1417 colord_create_profile(profiles
, p
->name
, "Gray..", COLORD_SPACE_GRAY
,
1418 format
, NULL
, COLORD_SCOPE_TEMP
);
1421 * Then add the RGB/CMYK/DeviceN color profile...
1424 device_colorspace
= "unknown";
1425 switch (ppd
->colorspace
)
1429 device_colorspace
= COLORD_SPACE_RGB
;
1430 colord_create_profile(profiles
, p
->name
, "RGB..", COLORD_SPACE_RGB
,
1431 format
, NULL
, COLORD_SCOPE_TEMP
);
1436 device_colorspace
= COLORD_SPACE_CMYK
;
1437 colord_create_profile(profiles
, p
->name
, "CMYK..", COLORD_SPACE_CMYK
,
1438 format
, NULL
, COLORD_SCOPE_TEMP
);
1442 device_colorspace
= COLORD_SPACE_GRAY
;
1446 colord_create_profile(profiles
, p
->name
, "DeviceN..",
1447 COLORD_SPACE_UNKNOWN
, format
, NULL
,
1453 * Register the device with colord.
1456 cupsdLogMessage(CUPSD_LOG_INFO
, "Registering ICC color profiles for \"%s\".",
1458 colord_create_device(p
, ppd
, profiles
, device_colorspace
, format
,
1459 COLORD_RELATION_SOFT
, COLORD_SCOPE_TEMP
);
1462 * Free any memory we used...
1465 cupsArrayDelete(profiles
);
1476 * 'colord_unregister_printer()' - Unregister profiles for a printer.
1480 colord_unregister_printer(
1481 cupsd_printer_t
*p
) /* I - printer */
1483 char device_id
[1024]; /* Device ID as understood by colord */
1487 * Ensure we have a D-Bus connection...
1494 * Just delete the device itself, and leave the profiles registered
1497 snprintf(device_id
, sizeof(device_id
), "cups-%s", p
->name
);
1498 colord_delete_device(device_id
);
1500 #endif /* __APPLE__ */