4 * Color management routines for the CUPS scheduler.
6 * Copyright 2007-2014 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.
43 * Include necessary headers...
47 #include <cups/ppd-private.h>
50 # include <ApplicationServices/ApplicationServices.h>
51 extern CFUUIDRef
ColorSyncCreateUUIDFromUInt32(unsigned id
);
52 # include <CoreFoundation/CoreFoundation.h>
53 #elif defined(HAVE_DBUS)
54 # include <dbus/dbus.h>
57 * Defines used by colord. See the reference docs for further details:
59 * http://colord.hughsie.com/api/ref-dbus.html
62 # define COLORD_SCOPE_NORMAL "normal"
64 # define COLORD_SCOPE_TEMP "temp" /* Process scope */
65 # define COLORD_SCOPE_DISK "disk" /* Lives forever, as stored in DB */
67 # define COLORD_RELATION_SOFT "soft" /* Mapping is not default */
68 # define COLORD_RELATION_HARD "hard" /* Explicitly mapped profile */
70 # define COLORD_SPACE_RGB "rgb" /* RGB colorspace */
71 # define COLORD_SPACE_CMYK "cmyk" /* CMYK colorspace */
72 # define COLORD_SPACE_GRAY "gray" /* Gray colorspace */
73 # define COLORD_SPACE_UNKNOWN "unknown"
74 /* Unknown colorspace */
76 # define COLORD_MODE_PHYSICAL "physical"
78 # define COLORD_MODE_VIRTUAL "virtual"
79 /* Virtual device with no hardware */
81 # define COLORD_KIND_PRINTER "printer"
82 /* printing output device */
84 # define COLORD_DBUS_SERVICE "org.freedesktop.ColorManager"
85 # define COLORD_DBUS_INTERFACE "org.freedesktop.ColorManager"
86 # define COLORD_DBUS_INTERFACE_DEVICE "org.freedesktop.ColorManager.Device"
87 # define COLORD_DBUS_PATH "/org/freedesktop/ColorManager"
88 /* Path for color management system */
89 # define COLORD_DBUS_TIMEOUT 5000 /* Timeout for connecting to colord in ms */
90 #endif /* __APPLE__ */
97 #if !defined(__APPLE__) && defined(HAVE_DBUS)
98 static DBusConnection
*colord_con
= NULL
;
99 /* DBUS connection for colord */
100 #endif /* !__APPLE__ && HAVE_DBUS */
108 static void apple_init_profile(ppd_file_t
*ppd
, cups_array_t
*languages
,
109 CFMutableDictionaryRef profile
,
110 unsigned id
, const char *name
,
111 const char *text
, const char *iccfile
);
112 static void apple_register_profiles(cupsd_printer_t
*p
);
113 static void apple_unregister_profiles(cupsd_printer_t
*p
);
115 #elif defined(HAVE_DBUS)
116 static void colord_create_device(cupsd_printer_t
*p
, ppd_file_t
*ppd
,
117 cups_array_t
*profiles
,
118 const char *colorspace
, char **format
,
119 const char *relation
, const char *scope
);
120 static void colord_create_profile(cups_array_t
*profiles
,
121 const char *printer_name
,
122 const char *qualifier
,
123 const char *colorspace
,
124 char **format
, const char *iccfile
,
126 static void colord_delete_device(const char *device_id
);
127 static void colord_device_add_profile(const char *device_path
,
128 const char *profile_path
,
129 const char *relation
);
130 static void colord_dict_add_strings(DBusMessageIter
*dict
,
131 const char *key
, const char *value
);
132 static char *colord_find_device(const char *device_id
);
133 static void colord_get_qualifier_format(ppd_file_t
*ppd
, char *format
[3]);
134 static void colord_register_printer(cupsd_printer_t
*p
);
135 static void colord_unregister_printer(cupsd_printer_t
*p
);
136 #endif /* __APPLE__ */
140 * 'cupsdRegisterColor()' - Register vendor color profiles in a PPD file.
144 cupsdRegisterColor(cupsd_printer_t
*p
) /* I - Printer */
149 apple_unregister_profiles(p
);
150 apple_register_profiles(p
);
153 #elif defined(HAVE_DBUS)
156 colord_unregister_printer(p
);
157 colord_register_printer(p
);
159 #endif /* __APPLE__ */
164 * 'cupsdStartColor()' - Initialize color management.
168 cupsdStartColor(void)
170 #if !defined(__APPLE__) && defined(HAVE_DBUS)
171 cupsd_printer_t
*p
; /* Current printer */
174 colord_con
= dbus_bus_get(DBUS_BUS_SYSTEM
, NULL
);
176 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
178 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
179 cupsdRegisterColor(p
);
180 #endif /* !__APPLE__ && HAVE_DBUS */
185 * 'cupsdStopColor()' - Shutdown color management.
191 #if !defined(__APPLE__) && defined(HAVE_DBUS)
193 dbus_connection_unref(colord_con
);
195 #endif /* !__APPLE__ && HAVE_DBUS */
200 * 'cupsdUnregisterColor()' - Unregister vendor color profiles in a PPD file.
204 cupsdUnregisterColor(cupsd_printer_t
*p
)/* I - Printer */
208 apple_unregister_profiles(p
);
210 #elif defined(HAVE_DBUS)
212 colord_unregister_printer(p
);
213 #endif /* __APPLE__ */
219 * 'apple_init_profile()' - Initialize a color profile.
224 ppd_file_t
*ppd
, /* I - PPD file */
225 cups_array_t
*languages
, /* I - Languages in the PPD file */
226 CFMutableDictionaryRef profile
, /* I - Profile dictionary */
227 unsigned id
, /* I - Profile ID */
228 const char *name
, /* I - Profile name */
229 const char *text
, /* I - Profile UI text */
230 const char *iccfile
) /* I - ICC filename */
232 CFURLRef url
; /* URL for profile filename */
233 CFMutableDictionaryRef dict
; /* Dictionary for name */
234 char *language
; /* Current language */
235 ppd_attr_t
*attr
; /* Profile attribute */
236 CFStringRef cflang
, /* Language string */
237 cftext
; /* Localized text */
243 * Build the profile name dictionary...
246 dict
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
247 &kCFTypeDictionaryKeyCallBacks
,
248 &kCFTypeDictionaryValueCallBacks
);
251 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to initialize profile \"%s\".",
256 cftext
= CFStringCreateWithCString(kCFAllocatorDefault
, text
,
257 kCFStringEncodingUTF8
);
261 CFDictionarySetValue(dict
, CFSTR("en_US"), cftext
);
268 * Find localized names for the color profiles...
271 cupsArraySave(ppd
->sorted_attrs
);
273 for (language
= (char *)cupsArrayFirst(languages
);
275 language
= (char *)cupsArrayNext(languages
))
279 if ((attr
= _ppdLocalizedAttr(ppd
, "cupsICCProfile", name
,
281 attr
= _ppdLocalizedAttr(ppd
, "APTiogaProfile", name
, language
);
284 attr
= _ppdLocalizedAttr(ppd
, "ColorModel", name
, language
);
286 if (attr
&& attr
->text
[0])
288 cflang
= CFStringCreateWithCString(kCFAllocatorDefault
, language
,
289 kCFStringEncodingUTF8
);
290 cftext
= CFStringCreateWithCString(kCFAllocatorDefault
, attr
->text
,
291 kCFStringEncodingUTF8
);
293 if (cflang
&& cftext
)
294 CFDictionarySetValue(dict
, cflang
, cftext
);
304 cupsArrayRestore(ppd
->sorted_attrs
);
308 * Fill in the profile data...
311 if (iccfile
&& *iccfile
)
313 url
= CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault
, (const UInt8
*)iccfile
, (CFIndex
)strlen(iccfile
), false);
317 CFDictionarySetValue(profile
, kColorSyncDeviceProfileURL
, url
);
322 CFDictionarySetValue(profile
, kColorSyncDeviceModeDescriptions
, dict
);
328 * 'apple_register_profiles()' - Register color profiles for a printer.
332 apple_register_profiles(
333 cupsd_printer_t
*p
) /* I - Printer */
335 int i
; /* Looping var */
336 char ppdfile
[1024], /* PPD filename */
337 iccfile
[1024], /* ICC filename */
338 selector
[PPD_MAX_NAME
];
339 /* Profile selection string */
340 ppd_file_t
*ppd
; /* PPD file */
341 ppd_attr_t
*attr
, /* Profile attributes */
342 *profileid_attr
,/* cupsProfileID attribute */
343 *q1_attr
, /* ColorModel (or other) qualifier */
344 *q2_attr
, /* MediaType (or other) qualifier */
345 *q3_attr
; /* Resolution (or other) qualifier */
346 char q_keyword
[PPD_MAX_NAME
];
347 /* Qualifier keyword */
348 const char *q1_choice
, /* ColorModel (or other) choice */
349 *q2_choice
, /* MediaType (or other) choice */
350 *q3_choice
; /* Resolution (or other) choice */
351 ppd_option_t
*cm_option
; /* Color model option */
352 ppd_choice_t
*cm_choice
; /* Color model choice */
353 int num_profiles
; /* Number of profiles */
354 OSStatus error
= 0; /* Last error */
355 unsigned device_id
, /* Printer device ID */
356 profile_id
= 0, /* Profile ID */
357 default_profile_id
= 0;
358 /* Default profile ID */
359 CFMutableDictionaryRef device_name
; /* Printer device name dictionary */
360 CFStringRef printer_name
; /* Printer name string */
361 cups_array_t
*languages
; /* Languages array */
362 CFMutableDictionaryRef profiles
, /* Dictionary of profiles */
363 profile
; /* Current profile info dictionary */
364 CFStringRef dict_key
; /* Key in factory profile dictionary */
368 * Make sure ColorSync is available...
371 if (ColorSyncRegisterDevice
== NULL
)
375 * Try opening the PPD file for this printer...
378 snprintf(ppdfile
, sizeof(ppdfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
379 if ((ppd
= _ppdOpenFile(ppdfile
, _PPD_LOCALIZATION_ICC_PROFILES
)) == NULL
)
383 * See if we have any profiles...
386 for (num_profiles
= 0, attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
388 attr
= ppdFindNextAttr(ppd
, "cupsICCProfile", NULL
))
389 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
391 if (attr
->value
[0] != '/')
392 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
395 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
397 if (access(iccfile
, 0))
399 cupsdLogMessage(CUPSD_LOG_ERROR
,
400 "%s: ICC Profile \"%s\" does not exist.", p
->name
,
402 cupsdSetPrinterReasons(p
, "+cups-missing-filter-warning");
410 * Create a dictionary for the factory profiles...
413 profiles
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
414 &kCFTypeDictionaryKeyCallBacks
,
415 &kCFTypeDictionaryValueCallBacks
);
418 cupsdLogMessage(CUPSD_LOG_ERROR
,
419 "Unable to allocate memory for factory profiles.");
425 * If we have profiles, add them...
428 if (num_profiles
> 0)
431 * For CUPS PPDs, figure out the default profile selector values...
434 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier1", NULL
)) != NULL
&&
435 attr
->value
&& attr
->value
[0])
437 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
438 q1_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
440 else if ((q1_attr
= ppdFindAttr(ppd
, "DefaultColorModel", NULL
)) == NULL
)
441 q1_attr
= ppdFindAttr(ppd
, "DefaultColorSpace", NULL
);
443 if (q1_attr
&& q1_attr
->value
&& q1_attr
->value
[0])
444 q1_choice
= q1_attr
->value
;
448 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier2", NULL
)) != NULL
&&
449 attr
->value
&& attr
->value
[0])
451 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
452 q2_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
455 q2_attr
= ppdFindAttr(ppd
, "DefaultMediaType", NULL
);
457 if (q2_attr
&& q2_attr
->value
&& q2_attr
->value
[0])
458 q2_choice
= q2_attr
->value
;
462 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier3", NULL
)) != NULL
&&
463 attr
->value
&& attr
->value
[0])
465 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
466 q3_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
469 q3_attr
= ppdFindAttr(ppd
, "DefaultResolution", NULL
);
471 if (q3_attr
&& q3_attr
->value
&& q3_attr
->value
[0])
472 q3_choice
= q3_attr
->value
;
477 * Loop through the profiles listed in the PPD...
480 languages
= _ppdGetLanguages(ppd
);
482 for (attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
484 attr
= ppdFindNextAttr(ppd
, "cupsICCProfile", NULL
))
485 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
488 * Add this profile...
491 if (attr
->value
[0] != '/')
492 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
495 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
497 if (_cupsFileCheck(iccfile
, _CUPS_FILE_CHECK_FILE
, !RunUser
,
498 cupsdLogFCMessage
, p
))
501 cupsArraySave(ppd
->sorted_attrs
);
503 if ((profileid_attr
= ppdFindAttr(ppd
, "cupsProfileID",
504 attr
->spec
)) != NULL
&&
505 profileid_attr
->value
&& isdigit(profileid_attr
->value
[0] & 255))
506 profile_id
= (unsigned)strtoul(profileid_attr
->value
, NULL
, 10);
508 profile_id
= _ppdHashName(attr
->spec
);
510 cupsArrayRestore(ppd
->sorted_attrs
);
512 profile
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
513 &kCFTypeDictionaryKeyCallBacks
,
514 &kCFTypeDictionaryValueCallBacks
);
517 cupsdLogMessage(CUPSD_LOG_ERROR
,
518 "Unable to allocate memory for color profile.");
524 apple_init_profile(ppd
, languages
, profile
, profile_id
, attr
->spec
,
525 attr
->text
[0] ? attr
->text
: attr
->spec
, iccfile
);
527 dict_key
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
,
528 CFSTR("%u"), profile_id
);
531 CFDictionarySetValue(profiles
, dict_key
, profile
);
538 * See if this is the default profile...
541 if (!default_profile_id
&& q1_choice
&& q2_choice
&& q3_choice
)
543 snprintf(selector
, sizeof(selector
), "%s.%s.%s", q1_choice
, q2_choice
,
545 if (!strcmp(selector
, attr
->spec
))
546 default_profile_id
= profile_id
;
549 if (!default_profile_id
&& q1_choice
&& q2_choice
)
551 snprintf(selector
, sizeof(selector
), "%s.%s.", q1_choice
, q2_choice
);
552 if (!strcmp(selector
, attr
->spec
))
553 default_profile_id
= profile_id
;
556 if (!default_profile_id
&& q1_choice
&& q3_choice
)
558 snprintf(selector
, sizeof(selector
), "%s..%s", q1_choice
, q3_choice
);
559 if (!strcmp(selector
, attr
->spec
))
560 default_profile_id
= profile_id
;
563 if (!default_profile_id
&& q1_choice
)
565 snprintf(selector
, sizeof(selector
), "%s..", q1_choice
);
566 if (!strcmp(selector
, attr
->spec
))
567 default_profile_id
= profile_id
;
570 if (!default_profile_id
&& q2_choice
&& q3_choice
)
572 snprintf(selector
, sizeof(selector
), ".%s.%s", q2_choice
, q3_choice
);
573 if (!strcmp(selector
, attr
->spec
))
574 default_profile_id
= profile_id
;
577 if (!default_profile_id
&& q2_choice
)
579 snprintf(selector
, sizeof(selector
), ".%s.", q2_choice
);
580 if (!strcmp(selector
, attr
->spec
))
581 default_profile_id
= profile_id
;
584 if (!default_profile_id
&& q3_choice
)
586 snprintf(selector
, sizeof(selector
), "..%s", q3_choice
);
587 if (!strcmp(selector
, attr
->spec
))
588 default_profile_id
= profile_id
;
592 _ppdFreeLanguages(languages
);
594 else if ((cm_option
= ppdFindOption(ppd
, "ColorModel")) != NULL
)
597 * Extract profiles from ColorModel option...
600 const char *profile_name
; /* Name of generic profile */
603 num_profiles
= cm_option
->num_choices
;
605 for (i
= cm_option
->num_choices
, cm_choice
= cm_option
->choices
;
609 if (!strcmp(cm_choice
->choice
, "Gray") ||
610 !strcmp(cm_choice
->choice
, "Black"))
611 profile_name
= "Gray";
612 else if (!strcmp(cm_choice
->choice
, "RGB") ||
613 !strcmp(cm_choice
->choice
, "CMY"))
614 profile_name
= "RGB";
615 else if (!strcmp(cm_choice
->choice
, "CMYK") ||
616 !strcmp(cm_choice
->choice
, "KCMY"))
617 profile_name
= "CMYK";
619 profile_name
= "DeviceN";
621 snprintf(selector
, sizeof(selector
), "%s..", profile_name
);
622 profile_id
= _ppdHashName(selector
);
624 profile
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
625 &kCFTypeDictionaryKeyCallBacks
,
626 &kCFTypeDictionaryValueCallBacks
);
629 cupsdLogMessage(CUPSD_LOG_ERROR
,
630 "Unable to allocate memory for color profile.");
636 apple_init_profile(ppd
, NULL
, profile
, profile_id
, cm_choice
->choice
,
637 cm_choice
->text
, NULL
);
639 dict_key
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
,
640 CFSTR("%u"), profile_id
);
643 CFDictionarySetValue(profiles
, dict_key
, profile
);
649 if (cm_choice
->marked
)
650 default_profile_id
= profile_id
;
656 * Use the default colorspace...
659 attr
= ppdFindAttr(ppd
, "DefaultColorSpace", NULL
);
661 num_profiles
= (attr
&& ppd
->colorspace
== PPD_CS_GRAY
) ? 1 : 2;
664 * Add the grayscale profile first. We always have a grayscale profile.
667 profile
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
668 &kCFTypeDictionaryKeyCallBacks
,
669 &kCFTypeDictionaryValueCallBacks
);
673 cupsdLogMessage(CUPSD_LOG_ERROR
,
674 "Unable to allocate memory for color profile.");
680 profile_id
= _ppdHashName("Gray..");
681 apple_init_profile(ppd
, NULL
, profile
, profile_id
, "Gray", "Gray", NULL
);
683 dict_key
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("%u"),
687 CFDictionarySetValue(profiles
, dict_key
, profile
);
694 * Then add the RGB/CMYK/DeviceN color profile...
697 profile
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
698 &kCFTypeDictionaryKeyCallBacks
,
699 &kCFTypeDictionaryValueCallBacks
);
703 cupsdLogMessage(CUPSD_LOG_ERROR
,
704 "Unable to allocate memory for color profile.");
710 switch (ppd
->colorspace
)
715 profile_id
= _ppdHashName("RGB..");
716 apple_init_profile(ppd
, NULL
, profile
, profile_id
, "RGB", "RGB",
722 profile_id
= _ppdHashName("CMYK..");
723 apple_init_profile(ppd
, NULL
, profile
, profile_id
, "CMYK", "CMYK",
732 profile_id
= _ppdHashName("DeviceN..");
733 apple_init_profile(ppd
, NULL
, profile
, profile_id
, "DeviceN",
738 if (CFDictionaryGetCount(profile
) > 0)
740 dict_key
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
,
741 CFSTR("%u"), profile_id
);
744 CFDictionarySetValue(profiles
, dict_key
, profile
);
752 if (num_profiles
> 0)
755 * Make sure we have a default profile ID...
758 if (!default_profile_id
)
759 default_profile_id
= profile_id
; /* Last profile */
761 dict_key
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("%u"),
765 CFDictionarySetValue(profiles
, kColorSyncDeviceDefaultProfileID
,
771 * Get the device ID hash and pathelogical name dictionary.
774 cupsdLogMessage(CUPSD_LOG_INFO
, "Registering ICC color profiles for \"%s\"",
777 device_id
= _ppdHashName(p
->name
);
778 device_name
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
779 &kCFTypeDictionaryKeyCallBacks
,
780 &kCFTypeDictionaryValueCallBacks
);
781 printer_name
= CFStringCreateWithCString(kCFAllocatorDefault
,
782 p
->name
, kCFStringEncodingUTF8
);
784 if (device_name
&& printer_name
)
787 * Register the device with ColorSync...
790 CFTypeRef deviceDictKeys
[] =
792 kColorSyncDeviceDescriptions
,
793 kColorSyncFactoryProfiles
,
794 kColorSyncDeviceUserScope
,
795 kColorSyncDeviceHostScope
797 CFTypeRef deviceDictVals
[] =
798 { /* Device values */
801 kCFPreferencesAnyUser
,
802 kCFPreferencesCurrentHost
804 CFDictionaryRef deviceDict
; /* Device dictionary */
805 CFUUIDRef deviceUUID
; /* Device UUID */
807 CFDictionarySetValue(device_name
, CFSTR("en_US"), printer_name
);
809 deviceDict
= CFDictionaryCreate(kCFAllocatorDefault
,
810 (const void **)deviceDictKeys
,
811 (const void **)deviceDictVals
,
812 sizeof(deviceDictKeys
) /
813 sizeof(deviceDictKeys
[0]),
814 &kCFTypeDictionaryKeyCallBacks
,
815 &kCFTypeDictionaryValueCallBacks
);
816 deviceUUID
= ColorSyncCreateUUIDFromUInt32(device_id
);
818 if (!deviceDict
|| !deviceUUID
||
819 !ColorSyncRegisterDevice(kColorSyncPrinterDeviceClass
, deviceUUID
,
824 CFRelease(deviceUUID
);
827 CFRelease(deviceDict
);
837 cupsdLogMessage(CUPSD_LOG_ERROR
,
838 "Unable to register ICC color profiles for \"%s\": %d",
839 p
->name
, (int)error
);
842 CFRelease(printer_name
);
845 CFRelease(device_name
);
849 * Free any memory we used...
859 * 'apple_unregister_profiles()' - Remove color profiles for the specified
864 apple_unregister_profiles(
865 cupsd_printer_t
*p
) /* I - Printer */
868 * Make sure ColorSync is available...
871 if (ColorSyncUnregisterDevice
!= NULL
)
873 CFUUIDRef deviceUUID
; /* Device UUID */
875 deviceUUID
= ColorSyncCreateUUIDFromUInt32(_ppdHashName(p
->name
));
878 ColorSyncUnregisterDevice(kColorSyncPrinterDeviceClass
, deviceUUID
);
879 CFRelease(deviceUUID
);
885 #elif defined(HAVE_DBUS)
887 * 'colord_create_device()' - Create a device and register profiles.
891 colord_create_device(
892 cupsd_printer_t
*p
, /* I - Printer */
893 ppd_file_t
*ppd
, /* I - PPD file */
894 cups_array_t
*profiles
, /* I - Profiles array */
895 const char *colorspace
, /* I - Device colorspace, e.g. 'rgb' */
896 char **format
, /* I - Device qualifier format */
897 const char *relation
, /* I - Profile relation, either 'soft'
899 const char *scope
) /* I - The scope of the device, e.g.
900 'normal', 'temp' or 'disk' */
902 DBusMessage
*message
= NULL
; /* D-Bus request */
903 DBusMessage
*reply
= NULL
; /* D-Bus reply */
904 DBusMessageIter args
; /* D-Bus method arguments */
905 DBusMessageIter dict
; /* D-Bus method arguments */
906 DBusError error
; /* D-Bus error */
907 const char *device_path
; /* Device object path */
908 const char *profile_path
; /* Profile path */
909 char *default_profile_path
= NULL
;
910 /* Default profile path */
911 char device_id
[1024]; /* Device ID as understood by colord */
912 char format_str
[1024]; /* Qualifier format as a string */
916 * Create the device...
919 snprintf(device_id
, sizeof(device_id
), "cups-%s", p
->name
);
920 device_path
= device_id
;
922 message
= dbus_message_new_method_call(COLORD_DBUS_SERVICE
,
924 COLORD_DBUS_INTERFACE
,
927 dbus_message_iter_init_append(message
, &args
);
928 dbus_message_iter_append_basic(&args
, DBUS_TYPE_STRING
, &device_path
);
929 dbus_message_iter_append_basic(&args
, DBUS_TYPE_STRING
, &scope
);
931 snprintf(format_str
, sizeof(format_str
), "%s.%s.%s", format
[0], format
[1],
934 dbus_message_iter_open_container(&args
, DBUS_TYPE_ARRAY
, "{ss}", &dict
);
935 colord_dict_add_strings(&dict
, "Colorspace", colorspace
);
936 colord_dict_add_strings(&dict
, "Mode", COLORD_MODE_PHYSICAL
);
937 if (ppd
->manufacturer
)
938 colord_dict_add_strings(&dict
, "Vendor", ppd
->manufacturer
);
940 colord_dict_add_strings(&dict
, "Model", ppd
->modelname
);
941 if (p
->sanitized_device_uri
)
942 colord_dict_add_strings(&dict
, "Serial", p
->sanitized_device_uri
);
943 colord_dict_add_strings(&dict
, "Format", format_str
);
944 colord_dict_add_strings(&dict
, "Kind", COLORD_KIND_PRINTER
);
945 dbus_message_iter_close_container(&args
, &dict
);
948 * Send the CreateDevice request synchronously...
951 dbus_error_init(&error
);
952 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Calling CreateDevice(%s,%s)", device_id
,
954 reply
= dbus_connection_send_with_reply_and_block(colord_con
, message
,
959 cupsdLogMessage(CUPSD_LOG_WARN
, "CreateDevice failed: %s:%s", error
.name
,
961 dbus_error_free(&error
);
969 dbus_message_iter_init(reply
, &args
);
970 if (dbus_message_iter_get_arg_type(&args
) != DBUS_TYPE_OBJECT_PATH
)
972 cupsdLogMessage(CUPSD_LOG_WARN
,
973 "CreateDevice failed: Incorrect reply type.");
977 dbus_message_iter_get_basic(&args
, &device_path
);
978 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Created device \"%s\".", device_path
);
984 for (profile_path
= cupsArrayFirst(profiles
);
986 profile_path
= cupsArrayNext(profiles
))
988 colord_device_add_profile(device_path
, profile_path
, relation
);
993 if (default_profile_path
)
994 free(default_profile_path
);
997 dbus_message_unref(message
);
1000 dbus_message_unref(reply
);
1005 * 'colord_create_profile()' - Create a color profile for a printer.
1009 colord_create_profile(
1010 cups_array_t
*profiles
, /* I - Profiles array */
1011 const char *printer_name
, /* I - Printer name */
1012 const char *qualifier
, /* I - Profile qualifier */
1013 const char *colorspace
, /* I - Profile colorspace */
1014 char **format
, /* I - Profile qualifier format */
1015 const char *iccfile
, /* I - ICC filename */
1016 const char *scope
) /* I - The scope of the profile, e.g.
1017 'normal', 'temp' or 'disk' */
1019 DBusMessage
*message
= NULL
; /* D-Bus request */
1020 DBusMessage
*reply
= NULL
; /* D-Bus reply */
1021 DBusMessageIter args
; /* D-Bus method arguments */
1022 DBusMessageIter dict
; /* D-Bus method arguments */
1023 DBusError error
; /* D-Bus error */
1024 char *idstr
; /* Profile ID string */
1025 size_t idstrlen
; /* Profile ID allocated length */
1026 const char *profile_path
; /* Device object path */
1027 char format_str
[1024]; /* Qualifier format as a string */
1031 * Create the profile...
1034 message
= dbus_message_new_method_call(COLORD_DBUS_SERVICE
,
1036 COLORD_DBUS_INTERFACE
,
1039 idstrlen
= strlen(printer_name
) + 1 + strlen(qualifier
) + 1;
1040 if ((idstr
= malloc(idstrlen
)) == NULL
)
1042 snprintf(idstr
, idstrlen
, "%s-%s", printer_name
, qualifier
);
1043 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Using profile ID \"%s\".", idstr
);
1045 dbus_message_iter_init_append(message
, &args
);
1046 dbus_message_iter_append_basic(&args
, DBUS_TYPE_STRING
, &idstr
);
1047 dbus_message_iter_append_basic(&args
, DBUS_TYPE_STRING
, &scope
);
1049 snprintf(format_str
, sizeof(format_str
), "%s.%s.%s", format
[0], format
[1],
1052 dbus_message_iter_open_container(&args
, DBUS_TYPE_ARRAY
, "{ss}", &dict
);
1053 colord_dict_add_strings(&dict
, "Qualifier", qualifier
);
1054 colord_dict_add_strings(&dict
, "Format", format_str
);
1055 colord_dict_add_strings(&dict
, "Colorspace", colorspace
);
1057 colord_dict_add_strings(&dict
, "Filename", iccfile
);
1058 dbus_message_iter_close_container(&args
, &dict
);
1061 * Send the CreateProfile request synchronously...
1064 dbus_error_init(&error
);
1065 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Calling CreateProfile(%s,%s)", idstr
,
1067 reply
= dbus_connection_send_with_reply_and_block(colord_con
, message
,
1068 COLORD_DBUS_TIMEOUT
,
1072 cupsdLogMessage(CUPSD_LOG_WARN
, "CreateProfile failed: %s:%s", error
.name
,
1074 dbus_error_free(&error
);
1082 dbus_message_iter_init(reply
, &args
);
1083 if (dbus_message_iter_get_arg_type(&args
) != DBUS_TYPE_OBJECT_PATH
)
1085 cupsdLogMessage(CUPSD_LOG_WARN
,
1086 "CreateProfile failed: Incorrect reply type.");
1090 dbus_message_iter_get_basic(&args
, &profile_path
);
1091 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Created profile \"%s\".", profile_path
);
1092 cupsArrayAdd(profiles
, strdup(profile_path
));
1097 dbus_message_unref(message
);
1100 dbus_message_unref(reply
);
1108 * 'colord_delete_device()' - Delete a device
1112 colord_delete_device(
1113 const char *device_id
) /* I - Device ID string */
1115 DBusMessage
*message
= NULL
; /* D-Bus request */
1116 DBusMessage
*reply
= NULL
; /* D-Bus reply */
1117 DBusMessageIter args
; /* D-Bus method arguments */
1118 DBusError error
; /* D-Bus error */
1119 char *device_path
; /* Device object path */
1123 * Find the device...
1126 if ((device_path
= colord_find_device(device_id
)) == NULL
)
1130 * Delete the device...
1133 message
= dbus_message_new_method_call(COLORD_DBUS_SERVICE
,
1135 COLORD_DBUS_INTERFACE
,
1138 dbus_message_iter_init_append(message
, &args
);
1139 dbus_message_iter_append_basic(&args
, DBUS_TYPE_OBJECT_PATH
, &device_path
);
1142 * Send the DeleteDevice request synchronously...
1145 dbus_error_init(&error
);
1146 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Calling DeleteDevice(%s)", device_path
);
1147 reply
= dbus_connection_send_with_reply_and_block(colord_con
, message
,
1148 COLORD_DBUS_TIMEOUT
,
1152 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DeleteDevice failed: %s:%s", error
.name
,
1154 dbus_error_free(&error
);
1164 dbus_message_unref(message
);
1167 dbus_message_unref(reply
);
1172 * 'colord_device_add_profile()' - Assign a profile to a device.
1176 colord_device_add_profile(
1177 const char *device_path
, /* I - Device object path */
1178 const char *profile_path
, /* I - Profile object path */
1179 const char *relation
) /* I - Device relation, either
1182 DBusMessage
*message
= NULL
; /* D-Bus request */
1183 DBusMessage
*reply
= NULL
; /* D-Bus reply */
1184 DBusMessageIter args
; /* D-Bus method arguments */
1185 DBusError error
; /* D-Bus error */
1188 message
= dbus_message_new_method_call(COLORD_DBUS_SERVICE
,
1190 COLORD_DBUS_INTERFACE_DEVICE
,
1193 dbus_message_iter_init_append(message
, &args
);
1194 dbus_message_iter_append_basic(&args
, DBUS_TYPE_STRING
, &relation
);
1195 dbus_message_iter_append_basic(&args
, DBUS_TYPE_OBJECT_PATH
, &profile_path
);
1196 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Calling %s:AddProfile(%s) [%s]",
1197 device_path
, profile_path
, relation
);
1200 * Send the AddProfile request synchronously...
1203 dbus_error_init(&error
);
1204 reply
= dbus_connection_send_with_reply_and_block(colord_con
, message
,
1205 COLORD_DBUS_TIMEOUT
,
1209 cupsdLogMessage(CUPSD_LOG_WARN
, "AddProfile failed: %s:%s", error
.name
,
1211 dbus_error_free(&error
);
1218 dbus_message_unref(message
);
1221 dbus_message_unref(reply
);
1226 * 'colord_dict_add_strings()' - Add two strings to a dictionary.
1230 colord_dict_add_strings(
1231 DBusMessageIter
*dict
, /* I - Dictionary */
1232 const char *key
, /* I - Key string */
1233 const char *value
) /* I - Value string */
1235 DBusMessageIter entry
; /* Entry to add */
1238 dbus_message_iter_open_container(dict
, DBUS_TYPE_DICT_ENTRY
, NULL
, &entry
);
1239 dbus_message_iter_append_basic(&entry
, DBUS_TYPE_STRING
, &key
);
1240 dbus_message_iter_append_basic(&entry
, DBUS_TYPE_STRING
, &value
);
1241 dbus_message_iter_close_container(dict
, &entry
);
1246 * 'colord_find_device()' - Finds a device
1249 static char * /* O - Device path or NULL */
1251 const char *device_id
) /* I - Device ID string */
1253 DBusMessage
*message
= NULL
; /* D-Bus request */
1254 DBusMessage
*reply
= NULL
; /* D-Bus reply */
1255 DBusMessageIter args
; /* D-Bus method arguments */
1256 DBusError error
; /* D-Bus error */
1257 const char *device_path_tmp
; /* Device object path */
1258 char *device_path
= NULL
; /* Device object path */
1261 message
= dbus_message_new_method_call(COLORD_DBUS_SERVICE
,
1263 COLORD_DBUS_INTERFACE
,
1266 dbus_message_iter_init_append(message
, &args
);
1267 dbus_message_iter_append_basic(&args
, DBUS_TYPE_STRING
, &device_id
);
1270 * Send the FindDeviceById request synchronously...
1273 dbus_error_init(&error
);
1274 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Calling FindDeviceById(%s)", device_id
);
1275 reply
= dbus_connection_send_with_reply_and_block(colord_con
, message
,
1276 COLORD_DBUS_TIMEOUT
,
1280 cupsdLogMessage(CUPSD_LOG_DEBUG
, "FindDeviceById failed: %s:%s",
1281 error
.name
, error
.message
);
1282 dbus_error_free(&error
);
1290 dbus_message_iter_init(reply
, &args
);
1291 if (dbus_message_iter_get_arg_type(&args
) != DBUS_TYPE_OBJECT_PATH
)
1293 cupsdLogMessage(CUPSD_LOG_WARN
,
1294 "FindDeviceById failed: Incorrect reply type.");
1298 dbus_message_iter_get_basic(&args
, &device_path_tmp
);
1299 if (device_path_tmp
)
1300 device_path
= strdup(device_path_tmp
);
1305 dbus_message_unref(message
);
1308 dbus_message_unref(reply
);
1310 return (device_path
);
1315 * 'colord_get_qualifier_format()' - Get the qualifier format.
1317 * Note: Returns a value of "ColorSpace.MediaType.Resolution" by default.
1321 colord_get_qualifier_format(
1322 ppd_file_t
*ppd
, /* I - PPD file data */
1323 char *format
[3]) /* I - Format tuple */
1325 const char *tmp
; /* Temporary string */
1326 ppd_attr_t
*attr
; /* Profile attributes */
1330 * Get 1st section...
1333 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier1", NULL
)) != NULL
)
1335 else if (ppdFindAttr(ppd
, "DefaultColorModel", NULL
))
1337 else if (ppdFindAttr(ppd
, "DefaultColorSpace", NULL
))
1342 format
[0] = strdup(tmp
);
1345 * Get 2nd section...
1348 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier2", NULL
)) != NULL
)
1353 format
[1] = strdup(tmp
);
1356 * Get 3rd section...
1359 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier3", NULL
)) != NULL
)
1364 format
[2] = strdup(tmp
);
1369 * 'colord_register_printer()' - Register profiles for a printer.
1373 colord_register_printer(
1374 cupsd_printer_t
*p
) /* I - printer */
1376 char ppdfile
[1024], /* PPD filename */
1377 iccfile
[1024]; /* ICC filename */
1378 ppd_file_t
*ppd
; /* PPD file */
1379 cups_array_t
*profiles
; /* Profile paths array */
1380 ppd_attr_t
*attr
; /* Profile attributes */
1381 const char *device_colorspace
; /* Device colorspace */
1382 char *format
[3]; /* Qualifier format tuple */
1386 * Ensure we have a D-Bus connection...
1393 * Try opening the PPD file for this printer...
1396 snprintf(ppdfile
, sizeof(ppdfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
1397 if ((ppd
= _ppdOpenFile(ppdfile
, _PPD_LOCALIZATION_ICC_PROFILES
)) == NULL
)
1401 * Find out the qualifier format
1404 colord_get_qualifier_format(ppd
, format
);
1407 * See if we have any embedded profiles...
1410 profiles
= cupsArrayNew3(NULL
, NULL
, NULL
, 0, (cups_acopy_func_t
)strdup
,
1411 (cups_afree_func_t
)free
);
1412 for (attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
1414 attr
= ppdFindNextAttr(ppd
, "cupsICCProfile", NULL
))
1415 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
1417 if (attr
->value
[0] != '/')
1418 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
1421 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
1423 if (_cupsFileCheck(iccfile
, _CUPS_FILE_CHECK_FILE
, !RunUser
,
1424 cupsdLogFCMessage
, p
))
1427 colord_create_profile(profiles
, p
->name
, attr
->spec
, COLORD_SPACE_UNKNOWN
,
1428 format
, iccfile
, COLORD_SCOPE_TEMP
);
1432 * Add the grayscale profile first. We always have a grayscale profile.
1435 colord_create_profile(profiles
, p
->name
, "Gray..", COLORD_SPACE_GRAY
,
1436 format
, NULL
, COLORD_SCOPE_TEMP
);
1439 * Then add the RGB/CMYK/DeviceN color profile...
1442 device_colorspace
= "unknown";
1443 switch (ppd
->colorspace
)
1447 device_colorspace
= COLORD_SPACE_RGB
;
1448 colord_create_profile(profiles
, p
->name
, "RGB..", COLORD_SPACE_RGB
,
1449 format
, NULL
, COLORD_SCOPE_TEMP
);
1454 device_colorspace
= COLORD_SPACE_CMYK
;
1455 colord_create_profile(profiles
, p
->name
, "CMYK..", COLORD_SPACE_CMYK
,
1456 format
, NULL
, COLORD_SCOPE_TEMP
);
1460 device_colorspace
= COLORD_SPACE_GRAY
;
1464 colord_create_profile(profiles
, p
->name
, "DeviceN..",
1465 COLORD_SPACE_UNKNOWN
, format
, NULL
,
1471 * Register the device with colord.
1474 cupsdLogMessage(CUPSD_LOG_INFO
, "Registering ICC color profiles for \"%s\".",
1476 colord_create_device(p
, ppd
, profiles
, device_colorspace
, format
,
1477 COLORD_RELATION_SOFT
, COLORD_SCOPE_TEMP
);
1480 * Free any memory we used...
1483 cupsArrayDelete(profiles
);
1494 * 'colord_unregister_printer()' - Unregister profiles for a printer.
1498 colord_unregister_printer(
1499 cupsd_printer_t
*p
) /* I - printer */
1501 char device_id
[1024]; /* Device ID as understood by colord */
1505 * Ensure we have a D-Bus connection...
1512 * Just delete the device itself, and leave the profiles registered
1515 snprintf(device_id
, sizeof(device_id
), "cups-%s", p
->name
);
1516 colord_delete_device(device_id
);
1518 #endif /* __APPLE__ */