2 * "$Id: colorman.c 11173 2013-07-23 12:31:34Z msweet $"
4 * Color management routines for the CUPS scheduler.
6 * Copyright 2007-2013 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 * cupsdRegisterColor() - Register vendor color profiles in a PPD
45 * cupsdStartColor() - Initialize color management.
46 * cupsdStopColor() - Shutdown color management.
47 * cupsdUnregisterColor() - Unregister vendor color profiles in a PPD
49 * apple_init_profile() - Initialize a color profile.
50 * apple_register_profiles() - Register color profiles for a printer.
51 * apple_unregister_profiles() - Remove color profiles for the specified
53 * colord_create_device() - Create a device and register profiles.
54 * colord_create_profile() - Create a color profile for a printer.
55 * colord_delete_device() - Delete a device
56 * colord_device_add_profile() - Assign a profile to a device.
57 * colord_dict_add_strings() - Add two strings to a dictionary.
58 * colord_find_device() - Finds a device
59 * colord_get_qualifier_format() - Get the qualifier format.
60 * colord_register_printer() - Register profiles for a printer.
61 * colord_unregister_printer() - Unregister profiles for a printer.
65 * Include necessary headers...
69 #include <cups/ppd-private.h>
72 # include <ApplicationServices/ApplicationServices.h>
73 extern CFUUIDRef ColorSyncCreateUUIDFromUInt32(unsigned id);
74 # include <CoreFoundation/CoreFoundation.h>
75 #elif defined(HAVE_DBUS)
76 # include <dbus/dbus.h>
79 * Defines used by colord. See the reference docs for further details:
81 * http://colord.hughsie.com/api/ref-dbus.html
84 # define COLORD_SCOPE_NORMAL "normal"
86 # define COLORD_SCOPE_TEMP "temp" /* Process scope */
87 # define COLORD_SCOPE_DISK "disk" /* Lives forever, as stored in DB */
89 # define COLORD_RELATION_SOFT "soft" /* Mapping is not default */
90 # define COLORD_RELATION_HARD "hard" /* Explicitly mapped profile */
92 # define COLORD_SPACE_RGB "rgb" /* RGB colorspace */
93 # define COLORD_SPACE_CMYK "cmyk" /* CMYK colorspace */
94 # define COLORD_SPACE_GRAY "gray" /* Gray colorspace */
95 # define COLORD_SPACE_UNKNOWN "unknown"
96 /* Unknown colorspace */
98 # define COLORD_MODE_PHYSICAL "physical"
100 # define COLORD_MODE_VIRTUAL "virtual"
101 /* Virtual device with no hardware */
103 # define COLORD_KIND_PRINTER "printer"
104 /* printing output device */
106 # define COLORD_DBUS_SERVICE "org.freedesktop.ColorManager"
107 # define COLORD_DBUS_INTERFACE "org.freedesktop.ColorManager"
108 # define COLORD_DBUS_INTERFACE_DEVICE "org.freedesktop.ColorManager.Device"
109 # define COLORD_DBUS_PATH "/org/freedesktop/ColorManager"
110 /* Path for color management system */
111 # define COLORD_DBUS_TIMEOUT 5000 /* Timeout for connecting to colord in ms */
112 #endif /* __APPLE__ */
119 #if !defined(__APPLE__) && defined(HAVE_DBUS)
120 static DBusConnection *colord_con = NULL;
121 /* DBUS connection for colord */
122 #endif /* !__APPLE__ && HAVE_DBUS */
130 static void apple_init_profile(ppd_file_t *ppd, cups_array_t *languages,
131 CFMutableDictionaryRef profile,
132 unsigned id, const char *name,
133 const char *text, const char *iccfile);
134 static void apple_register_profiles(cupsd_printer_t *p);
135 static void apple_unregister_profiles(cupsd_printer_t *p);
137 #elif defined(HAVE_DBUS)
138 static void colord_create_device(cupsd_printer_t *p, ppd_file_t *ppd,
139 cups_array_t *profiles,
140 const char *colorspace, char **format,
141 const char *relation, const char *scope);
142 static void colord_create_profile(cups_array_t *profiles,
143 const char *printer_name,
144 const char *qualifier,
145 const char *colorspace,
146 char **format, const char *iccfile,
148 static void colord_delete_device(const char *device_id);
149 static void colord_device_add_profile(const char *device_path,
150 const char *profile_path,
151 const char *relation);
152 static void colord_dict_add_strings(DBusMessageIter *dict,
153 const char *key, const char *value);
154 static char *colord_find_device(const char *device_id);
155 static void colord_get_qualifier_format(ppd_file_t *ppd, char *format[3]);
156 static void colord_register_printer(cupsd_printer_t *p);
157 static void colord_unregister_printer(cupsd_printer_t *p);
158 #endif /* __APPLE__ */
162 * 'cupsdRegisterColor()' - Register vendor color profiles in a PPD file.
166 cupsdRegisterColor(cupsd_printer_t *p) /* I - Printer */
171 apple_unregister_profiles(p);
172 apple_register_profiles(p);
175 #elif defined(HAVE_DBUS)
176 colord_unregister_printer(p);
177 colord_register_printer(p);
178 #endif /* __APPLE__ */
183 * 'cupsdStartColor()' - Initialize color management.
187 cupsdStartColor(void)
189 #if !defined(__APPLE__) && defined(HAVE_DBUS)
190 cupsd_printer_t *p; /* Current printer */
193 colord_con = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
195 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
197 p = (cupsd_printer_t *)cupsArrayNext(Printers))
198 cupsdRegisterColor(p);
199 #endif /* !__APPLE__ && HAVE_DBUS */
204 * 'cupsdStopColor()' - Shutdown color management.
210 #if !defined(__APPLE__) && defined(HAVE_DBUS)
211 dbus_connection_unref(colord_con);
213 #endif /* !__APPLE__ && HAVE_DBUS */
218 * 'cupsdUnregisterColor()' - Unregister vendor color profiles in a PPD file.
222 cupsdUnregisterColor(cupsd_printer_t *p)/* I - Printer */
226 apple_unregister_profiles(p);
228 #elif defined(HAVE_DBUS)
229 colord_unregister_printer(p);
230 #endif /* __APPLE__ */
236 * 'apple_init_profile()' - Initialize a color profile.
241 ppd_file_t *ppd, /* I - PPD file */
242 cups_array_t *languages, /* I - Languages in the PPD file */
243 CFMutableDictionaryRef profile, /* I - Profile dictionary */
244 unsigned id, /* I - Profile ID */
245 const char *name, /* I - Profile name */
246 const char *text, /* I - Profile UI text */
247 const char *iccfile) /* I - ICC filename */
249 CFURLRef url; /* URL for profile filename */
250 CFMutableDictionaryRef dict; /* Dictionary for name */
251 char *language; /* Current language */
252 ppd_attr_t *attr; /* Profile attribute */
253 CFStringRef cflang, /* Language string */
254 cftext; /* Localized text */
260 * Build the profile name dictionary...
263 dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
264 &kCFTypeDictionaryKeyCallBacks,
265 &kCFTypeDictionaryValueCallBacks);
268 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to initialize profile \"%s\".",
273 cftext = CFStringCreateWithCString(kCFAllocatorDefault, text,
274 kCFStringEncodingUTF8);
278 CFDictionarySetValue(dict, CFSTR("en_US"), cftext);
285 * Find localized names for the color profiles...
288 cupsArraySave(ppd->sorted_attrs);
290 for (language = (char *)cupsArrayFirst(languages);
292 language = (char *)cupsArrayNext(languages))
296 if ((attr = _ppdLocalizedAttr(ppd, "cupsICCProfile", name,
298 attr = _ppdLocalizedAttr(ppd, "APTiogaProfile", name, language);
301 attr = _ppdLocalizedAttr(ppd, "ColorModel", name, language);
303 if (attr && attr->text[0])
305 cflang = CFStringCreateWithCString(kCFAllocatorDefault, language,
306 kCFStringEncodingUTF8);
307 cftext = CFStringCreateWithCString(kCFAllocatorDefault, attr->text,
308 kCFStringEncodingUTF8);
310 if (cflang && cftext)
311 CFDictionarySetValue(dict, cflang, cftext);
321 cupsArrayRestore(ppd->sorted_attrs);
325 * Fill in the profile data...
328 if (iccfile && *iccfile)
330 url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault,
331 (const UInt8 *)iccfile,
332 strlen(iccfile), false);
336 CFDictionarySetValue(profile, kColorSyncDeviceProfileURL, url);
341 CFDictionarySetValue(profile, kColorSyncDeviceModeDescriptions, dict);
347 * 'apple_register_profiles()' - Register color profiles for a printer.
351 apple_register_profiles(
352 cupsd_printer_t *p) /* I - Printer */
354 int i; /* Looping var */
355 char ppdfile[1024], /* PPD filename */
356 iccfile[1024], /* ICC filename */
357 selector[PPD_MAX_NAME];
358 /* Profile selection string */
359 ppd_file_t *ppd; /* PPD file */
360 ppd_attr_t *attr, /* Profile attributes */
361 *profileid_attr,/* cupsProfileID attribute */
362 *q1_attr, /* ColorModel (or other) qualifier */
363 *q2_attr, /* MediaType (or other) qualifier */
364 *q3_attr; /* Resolution (or other) qualifier */
365 char q_keyword[PPD_MAX_NAME];
366 /* Qualifier keyword */
367 const char *q1_choice, /* ColorModel (or other) choice */
368 *q2_choice, /* MediaType (or other) choice */
369 *q3_choice; /* Resolution (or other) choice */
370 ppd_option_t *cm_option; /* Color model option */
371 ppd_choice_t *cm_choice; /* Color model choice */
372 int num_profiles; /* Number of profiles */
373 OSStatus error = 0; /* Last error */
374 unsigned device_id, /* Printer device ID */
375 profile_id = 0, /* Profile ID */
376 default_profile_id = 0;
377 /* Default profile ID */
378 CFMutableDictionaryRef device_name; /* Printer device name dictionary */
379 CFStringRef printer_name; /* Printer name string */
380 cups_array_t *languages; /* Languages array */
381 CFMutableDictionaryRef profiles, /* Dictionary of profiles */
382 profile; /* Current profile info dictionary */
383 CFStringRef dict_key; /* Key in factory profile dictionary */
387 * Make sure ColorSync is available...
390 if (ColorSyncRegisterDevice == NULL)
394 * Try opening the PPD file for this printer...
397 snprintf(ppdfile, sizeof(ppdfile), "%s/ppd/%s.ppd", ServerRoot, p->name);
398 if ((ppd = _ppdOpenFile(ppdfile, _PPD_LOCALIZATION_ICC_PROFILES)) == NULL)
402 * See if we have any profiles...
405 for (num_profiles = 0, attr = ppdFindAttr(ppd, "cupsICCProfile", NULL);
407 attr = ppdFindNextAttr(ppd, "cupsICCProfile", NULL))
408 if (attr->spec[0] && attr->value && attr->value[0])
410 if (attr->value[0] != '/')
411 snprintf(iccfile, sizeof(iccfile), "%s/profiles/%s", DataDir,
414 strlcpy(iccfile, attr->value, sizeof(iccfile));
416 if (access(iccfile, 0))
418 cupsdLogMessage(CUPSD_LOG_ERROR,
419 "%s: ICC Profile \"%s\" does not exist.", p->name,
421 cupsdSetPrinterReasons(p, "+cups-missing-filter-warning");
429 * Create a dictionary for the factory profiles...
432 profiles = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
433 &kCFTypeDictionaryKeyCallBacks,
434 &kCFTypeDictionaryValueCallBacks);
437 cupsdLogMessage(CUPSD_LOG_ERROR,
438 "Unable to allocate memory for factory profiles.");
444 * If we have profiles, add them...
447 if (num_profiles > 0)
450 * For CUPS PPDs, figure out the default profile selector values...
453 if ((attr = ppdFindAttr(ppd, "cupsICCQualifier1", NULL)) != NULL &&
454 attr->value && attr->value[0])
456 snprintf(q_keyword, sizeof(q_keyword), "Default%s", attr->value);
457 q1_attr = ppdFindAttr(ppd, q_keyword, NULL);
459 else if ((q1_attr = ppdFindAttr(ppd, "DefaultColorModel", NULL)) == NULL)
460 q1_attr = ppdFindAttr(ppd, "DefaultColorSpace", NULL);
462 if (q1_attr && q1_attr->value && q1_attr->value[0])
463 q1_choice = q1_attr->value;
467 if ((attr = ppdFindAttr(ppd, "cupsICCQualifier2", NULL)) != NULL &&
468 attr->value && attr->value[0])
470 snprintf(q_keyword, sizeof(q_keyword), "Default%s", attr->value);
471 q2_attr = ppdFindAttr(ppd, q_keyword, NULL);
474 q2_attr = ppdFindAttr(ppd, "DefaultMediaType", NULL);
476 if (q2_attr && q2_attr->value && q2_attr->value[0])
477 q2_choice = q2_attr->value;
481 if ((attr = ppdFindAttr(ppd, "cupsICCQualifier3", NULL)) != NULL &&
482 attr->value && attr->value[0])
484 snprintf(q_keyword, sizeof(q_keyword), "Default%s", attr->value);
485 q3_attr = ppdFindAttr(ppd, q_keyword, NULL);
488 q3_attr = ppdFindAttr(ppd, "DefaultResolution", NULL);
490 if (q3_attr && q3_attr->value && q3_attr->value[0])
491 q3_choice = q3_attr->value;
496 * Loop through the profiles listed in the PPD...
499 languages = _ppdGetLanguages(ppd);
501 for (attr = ppdFindAttr(ppd, "cupsICCProfile", NULL);
503 attr = ppdFindNextAttr(ppd, "cupsICCProfile", NULL))
504 if (attr->spec[0] && attr->value && attr->value[0])
507 * Add this profile...
510 if (attr->value[0] != '/')
511 snprintf(iccfile, sizeof(iccfile), "%s/profiles/%s", DataDir,
514 strlcpy(iccfile, attr->value, sizeof(iccfile));
516 if (_cupsFileCheck(iccfile, _CUPS_FILE_CHECK_FILE, !RunUser,
517 cupsdLogFCMessage, p))
520 cupsArraySave(ppd->sorted_attrs);
522 if ((profileid_attr = ppdFindAttr(ppd, "cupsProfileID",
523 attr->spec)) != NULL &&
524 profileid_attr->value && isdigit(profileid_attr->value[0] & 255))
525 profile_id = (unsigned)strtoul(profileid_attr->value, NULL, 10);
527 profile_id = _ppdHashName(attr->spec);
529 cupsArrayRestore(ppd->sorted_attrs);
531 profile = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
532 &kCFTypeDictionaryKeyCallBacks,
533 &kCFTypeDictionaryValueCallBacks);
536 cupsdLogMessage(CUPSD_LOG_ERROR,
537 "Unable to allocate memory for color profile.");
543 apple_init_profile(ppd, languages, profile, profile_id, attr->spec,
544 attr->text[0] ? attr->text : attr->spec, iccfile);
546 dict_key = CFStringCreateWithFormat(kCFAllocatorDefault, NULL,
547 CFSTR("%u"), profile_id);
550 CFDictionarySetValue(profiles, dict_key, profile);
557 * See if this is the default profile...
560 if (!default_profile_id && q1_choice && q2_choice && q3_choice)
562 snprintf(selector, sizeof(selector), "%s.%s.%s", q1_choice, q2_choice,
564 if (!strcmp(selector, attr->spec))
565 default_profile_id = profile_id;
568 if (!default_profile_id && q1_choice && q2_choice)
570 snprintf(selector, sizeof(selector), "%s.%s.", q1_choice, q2_choice);
571 if (!strcmp(selector, attr->spec))
572 default_profile_id = profile_id;
575 if (!default_profile_id && q1_choice && q3_choice)
577 snprintf(selector, sizeof(selector), "%s..%s", q1_choice, q3_choice);
578 if (!strcmp(selector, attr->spec))
579 default_profile_id = profile_id;
582 if (!default_profile_id && q1_choice)
584 snprintf(selector, sizeof(selector), "%s..", q1_choice);
585 if (!strcmp(selector, attr->spec))
586 default_profile_id = profile_id;
589 if (!default_profile_id && q2_choice && q3_choice)
591 snprintf(selector, sizeof(selector), ".%s.%s", q2_choice, q3_choice);
592 if (!strcmp(selector, attr->spec))
593 default_profile_id = profile_id;
596 if (!default_profile_id && q2_choice)
598 snprintf(selector, sizeof(selector), ".%s.", q2_choice);
599 if (!strcmp(selector, attr->spec))
600 default_profile_id = profile_id;
603 if (!default_profile_id && q3_choice)
605 snprintf(selector, sizeof(selector), "..%s", q3_choice);
606 if (!strcmp(selector, attr->spec))
607 default_profile_id = profile_id;
611 _ppdFreeLanguages(languages);
613 else if ((cm_option = ppdFindOption(ppd, "ColorModel")) != NULL)
616 * Extract profiles from ColorModel option...
619 const char *profile_name; /* Name of generic profile */
622 num_profiles = cm_option->num_choices;
624 for (i = cm_option->num_choices, cm_choice = cm_option->choices;
628 if (!strcmp(cm_choice->choice, "Gray") ||
629 !strcmp(cm_choice->choice, "Black"))
630 profile_name = "Gray";
631 else if (!strcmp(cm_choice->choice, "RGB") ||
632 !strcmp(cm_choice->choice, "CMY"))
633 profile_name = "RGB";
634 else if (!strcmp(cm_choice->choice, "CMYK") ||
635 !strcmp(cm_choice->choice, "KCMY"))
636 profile_name = "CMYK";
638 profile_name = "DeviceN";
640 snprintf(selector, sizeof(selector), "%s..", profile_name);
641 profile_id = _ppdHashName(selector);
643 profile = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
644 &kCFTypeDictionaryKeyCallBacks,
645 &kCFTypeDictionaryValueCallBacks);
648 cupsdLogMessage(CUPSD_LOG_ERROR,
649 "Unable to allocate memory for color profile.");
655 apple_init_profile(ppd, NULL, profile, profile_id, cm_choice->choice,
656 cm_choice->text, NULL);
658 dict_key = CFStringCreateWithFormat(kCFAllocatorDefault, NULL,
659 CFSTR("%u"), profile_id);
662 CFDictionarySetValue(profiles, dict_key, profile);
668 if (cm_choice->marked)
669 default_profile_id = profile_id;
675 * Use the default colorspace...
678 attr = ppdFindAttr(ppd, "DefaultColorSpace", NULL);
680 num_profiles = (attr && ppd->colorspace == PPD_CS_GRAY) ? 1 : 2;
683 * Add the grayscale profile first. We always have a grayscale profile.
686 profile = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
687 &kCFTypeDictionaryKeyCallBacks,
688 &kCFTypeDictionaryValueCallBacks);
692 cupsdLogMessage(CUPSD_LOG_ERROR,
693 "Unable to allocate memory for color profile.");
699 profile_id = _ppdHashName("Gray..");
700 apple_init_profile(ppd, NULL, profile, profile_id, "Gray", "Gray", NULL);
702 dict_key = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%u"),
706 CFDictionarySetValue(profiles, dict_key, profile);
713 * Then add the RGB/CMYK/DeviceN color profile...
716 profile = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
717 &kCFTypeDictionaryKeyCallBacks,
718 &kCFTypeDictionaryValueCallBacks);
722 cupsdLogMessage(CUPSD_LOG_ERROR,
723 "Unable to allocate memory for color profile.");
729 switch (ppd->colorspace)
734 profile_id = _ppdHashName("RGB..");
735 apple_init_profile(ppd, NULL, profile, profile_id, "RGB", "RGB",
741 profile_id = _ppdHashName("CMYK..");
742 apple_init_profile(ppd, NULL, profile, profile_id, "CMYK", "CMYK",
751 profile_id = _ppdHashName("DeviceN..");
752 apple_init_profile(ppd, NULL, profile, profile_id, "DeviceN",
757 if (CFDictionaryGetCount(profile) > 0)
759 dict_key = CFStringCreateWithFormat(kCFAllocatorDefault, NULL,
760 CFSTR("%u"), profile_id);
763 CFDictionarySetValue(profiles, dict_key, profile);
771 if (num_profiles > 0)
774 * Make sure we have a default profile ID...
777 if (!default_profile_id)
778 default_profile_id = profile_id; /* Last profile */
780 dict_key = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%u"),
784 CFDictionarySetValue(profiles, kColorSyncDeviceDefaultProfileID,
790 * Get the device ID hash and pathelogical name dictionary.
793 cupsdLogMessage(CUPSD_LOG_INFO, "Registering ICC color profiles for \"%s\"",
796 device_id = _ppdHashName(p->name);
797 device_name = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
798 &kCFTypeDictionaryKeyCallBacks,
799 &kCFTypeDictionaryValueCallBacks);
800 printer_name = CFStringCreateWithCString(kCFAllocatorDefault,
801 p->name, kCFStringEncodingUTF8);
803 if (device_name && printer_name)
806 * Register the device with ColorSync...
809 CFTypeRef deviceDictKeys[] =
811 kColorSyncDeviceDescriptions,
812 kColorSyncFactoryProfiles,
813 kColorSyncDeviceUserScope,
814 kColorSyncDeviceHostScope
816 CFTypeRef deviceDictVals[] =
817 { /* Device values */
820 kCFPreferencesAnyUser,
821 kCFPreferencesCurrentHost
823 CFDictionaryRef deviceDict; /* Device dictionary */
824 CFUUIDRef deviceUUID; /* Device UUID */
826 CFDictionarySetValue(device_name, CFSTR("en_US"), printer_name);
828 deviceDict = CFDictionaryCreate(kCFAllocatorDefault,
829 (const void **)deviceDictKeys,
830 (const void **)deviceDictVals,
831 sizeof(deviceDictKeys) /
832 sizeof(deviceDictKeys[0]),
833 &kCFTypeDictionaryKeyCallBacks,
834 &kCFTypeDictionaryValueCallBacks);
835 deviceUUID = ColorSyncCreateUUIDFromUInt32(device_id);
837 if (!deviceDict || !deviceUUID ||
838 !ColorSyncRegisterDevice(kColorSyncPrinterDeviceClass, deviceUUID,
843 CFRelease(deviceUUID);
846 CFRelease(deviceDict);
856 cupsdLogMessage(CUPSD_LOG_ERROR,
857 "Unable to register ICC color profiles for \"%s\": %d",
858 p->name, (int)error);
861 CFRelease(printer_name);
864 CFRelease(device_name);
868 * Free any memory we used...
878 * 'apple_unregister_profiles()' - Remove color profiles for the specified
883 apple_unregister_profiles(
884 cupsd_printer_t *p) /* I - Printer */
887 * Make sure ColorSync is available...
890 if (ColorSyncUnregisterDevice != NULL)
892 CFUUIDRef deviceUUID; /* Device UUID */
894 deviceUUID = ColorSyncCreateUUIDFromUInt32(_ppdHashName(p->name));
897 ColorSyncUnregisterDevice(kColorSyncPrinterDeviceClass, deviceUUID);
898 CFRelease(deviceUUID);
904 #elif defined(HAVE_DBUS)
906 * 'colord_create_device()' - Create a device and register profiles.
910 colord_create_device(
911 cupsd_printer_t *p, /* I - Printer */
912 ppd_file_t *ppd, /* I - PPD file */
913 cups_array_t *profiles, /* I - Profiles array */
914 const char *colorspace, /* I - Device colorspace, e.g. 'rgb' */
915 char **format, /* I - Device qualifier format */
916 const char *relation, /* I - Profile relation, either 'soft'
918 const char *scope) /* I - The scope of the device, e.g.
919 'normal', 'temp' or 'disk' */
921 DBusMessage *message = NULL; /* D-Bus request */
922 DBusMessage *reply = NULL; /* D-Bus reply */
923 DBusMessageIter args; /* D-Bus method arguments */
924 DBusMessageIter dict; /* D-Bus method arguments */
925 DBusError error; /* D-Bus error */
926 const char *device_path; /* Device object path */
927 const char *profile_path; /* Profile path */
928 char *default_profile_path = NULL;
929 /* Default profile path */
930 char device_id[1024]; /* Device ID as understood by colord */
931 char format_str[1024]; /* Qualifier format as a string */
935 * Create the device...
938 snprintf(device_id, sizeof(device_id), "cups-%s", p->name);
939 device_path = device_id;
941 message = dbus_message_new_method_call(COLORD_DBUS_SERVICE,
943 COLORD_DBUS_INTERFACE,
946 dbus_message_iter_init_append(message, &args);
947 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &device_path);
948 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &scope);
950 snprintf(format_str, sizeof(format_str), "%s.%s.%s", format[0], format[1],
953 dbus_message_iter_open_container(&args, DBUS_TYPE_ARRAY, "{ss}", &dict);
954 colord_dict_add_strings(&dict, "Colorspace", colorspace);
955 colord_dict_add_strings(&dict, "Mode", COLORD_MODE_PHYSICAL);
956 if (ppd->manufacturer)
957 colord_dict_add_strings(&dict, "Vendor", ppd->manufacturer);
959 colord_dict_add_strings(&dict, "Model", ppd->modelname);
960 if (p->sanitized_device_uri)
961 colord_dict_add_strings(&dict, "Serial", p->sanitized_device_uri);
962 colord_dict_add_strings(&dict, "Format", format_str);
963 colord_dict_add_strings(&dict, "Kind", COLORD_KIND_PRINTER);
964 dbus_message_iter_close_container(&args, &dict);
967 * Send the CreateDevice request synchronously...
970 dbus_error_init(&error);
971 cupsdLogMessage(CUPSD_LOG_DEBUG, "Calling CreateDevice(%s,%s)", device_id,
973 reply = dbus_connection_send_with_reply_and_block(colord_con, message,
978 cupsdLogMessage(CUPSD_LOG_WARN, "CreateDevice failed: %s:%s", error.name,
980 dbus_error_free(&error);
988 dbus_message_iter_init(reply, &args);
989 if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_OBJECT_PATH)
991 cupsdLogMessage(CUPSD_LOG_WARN,
992 "CreateDevice failed: Incorrect reply type.");
996 dbus_message_iter_get_basic(&args, &device_path);
997 cupsdLogMessage(CUPSD_LOG_DEBUG, "Created device \"%s\".", device_path);
1003 for (profile_path = cupsArrayFirst(profiles);
1005 profile_path = cupsArrayNext(profiles))
1007 colord_device_add_profile(device_path, profile_path, relation);
1012 if (default_profile_path)
1013 free(default_profile_path);
1016 dbus_message_unref(message);
1019 dbus_message_unref(reply);
1024 * 'colord_create_profile()' - Create a color profile for a printer.
1028 colord_create_profile(
1029 cups_array_t *profiles, /* I - Profiles array */
1030 const char *printer_name, /* I - Printer name */
1031 const char *qualifier, /* I - Profile qualifier */
1032 const char *colorspace, /* I - Profile colorspace */
1033 char **format, /* I - Profile qualifier format */
1034 const char *iccfile, /* I - ICC filename */
1035 const char *scope) /* I - The scope of the profile, e.g.
1036 'normal', 'temp' or 'disk' */
1038 DBusMessage *message = NULL; /* D-Bus request */
1039 DBusMessage *reply = NULL; /* D-Bus reply */
1040 DBusMessageIter args; /* D-Bus method arguments */
1041 DBusMessageIter dict; /* D-Bus method arguments */
1042 DBusError error; /* D-Bus error */
1043 char *idstr; /* Profile ID string */
1044 size_t idstrlen; /* Profile ID allocated length */
1045 const char *profile_path; /* Device object path */
1046 char format_str[1024]; /* Qualifier format as a string */
1050 * Create the profile...
1053 message = dbus_message_new_method_call(COLORD_DBUS_SERVICE,
1055 COLORD_DBUS_INTERFACE,
1058 idstrlen = strlen(printer_name) + 1 + strlen(qualifier) + 1;
1059 if ((idstr = malloc(idstrlen)) == NULL)
1061 snprintf(idstr, idstrlen, "%s-%s", printer_name, qualifier);
1062 cupsdLogMessage(CUPSD_LOG_DEBUG, "Using profile ID \"%s\".", idstr);
1064 dbus_message_iter_init_append(message, &args);
1065 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &idstr);
1066 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &scope);
1068 snprintf(format_str, sizeof(format_str), "%s.%s.%s", format[0], format[1],
1071 dbus_message_iter_open_container(&args, DBUS_TYPE_ARRAY, "{ss}", &dict);
1072 colord_dict_add_strings(&dict, "Qualifier", qualifier);
1073 colord_dict_add_strings(&dict, "Format", format_str);
1074 colord_dict_add_strings(&dict, "Colorspace", colorspace);
1076 colord_dict_add_strings(&dict, "Filename", iccfile);
1077 dbus_message_iter_close_container(&args, &dict);
1080 * Send the CreateProfile request synchronously...
1083 dbus_error_init(&error);
1084 cupsdLogMessage(CUPSD_LOG_DEBUG, "Calling CreateProfile(%s,%s)", idstr,
1086 reply = dbus_connection_send_with_reply_and_block(colord_con, message,
1087 COLORD_DBUS_TIMEOUT,
1091 cupsdLogMessage(CUPSD_LOG_WARN, "CreateProfile failed: %s:%s", error.name,
1093 dbus_error_free(&error);
1101 dbus_message_iter_init(reply, &args);
1102 if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_OBJECT_PATH)
1104 cupsdLogMessage(CUPSD_LOG_WARN,
1105 "CreateProfile failed: Incorrect reply type.");
1109 dbus_message_iter_get_basic(&args, &profile_path);
1110 cupsdLogMessage(CUPSD_LOG_DEBUG, "Created profile \"%s\".", profile_path);
1111 cupsArrayAdd(profiles, strdup(profile_path));
1116 dbus_message_unref(message);
1119 dbus_message_unref(reply);
1127 * 'colord_delete_device()' - Delete a device
1131 colord_delete_device(
1132 const char *device_id) /* I - Device ID string */
1134 DBusMessage *message = NULL; /* D-Bus request */
1135 DBusMessage *reply = NULL; /* D-Bus reply */
1136 DBusMessageIter args; /* D-Bus method arguments */
1137 DBusError error; /* D-Bus error */
1138 char *device_path; /* Device object path */
1142 * Find the device...
1145 if ((device_path = colord_find_device(device_id)) == NULL)
1149 * Delete the device...
1152 message = dbus_message_new_method_call(COLORD_DBUS_SERVICE,
1154 COLORD_DBUS_INTERFACE,
1157 dbus_message_iter_init_append(message, &args);
1158 dbus_message_iter_append_basic(&args, DBUS_TYPE_OBJECT_PATH, &device_path);
1161 * Send the DeleteDevice request synchronously...
1164 dbus_error_init(&error);
1165 cupsdLogMessage(CUPSD_LOG_DEBUG, "Calling DeleteDevice(%s)", device_path);
1166 reply = dbus_connection_send_with_reply_and_block(colord_con, message,
1167 COLORD_DBUS_TIMEOUT,
1171 cupsdLogMessage(CUPSD_LOG_DEBUG, "DeleteDevice failed: %s:%s", error.name,
1173 dbus_error_free(&error);
1183 dbus_message_unref(message);
1186 dbus_message_unref(reply);
1191 * 'colord_device_add_profile()' - Assign a profile to a device.
1195 colord_device_add_profile(
1196 const char *device_path, /* I - Device object path */
1197 const char *profile_path, /* I - Profile object path */
1198 const char *relation) /* I - Device relation, either
1201 DBusMessage *message = NULL; /* D-Bus request */
1202 DBusMessage *reply = NULL; /* D-Bus reply */
1203 DBusMessageIter args; /* D-Bus method arguments */
1204 DBusError error; /* D-Bus error */
1207 message = dbus_message_new_method_call(COLORD_DBUS_SERVICE,
1209 COLORD_DBUS_INTERFACE_DEVICE,
1212 dbus_message_iter_init_append(message, &args);
1213 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &relation);
1214 dbus_message_iter_append_basic(&args, DBUS_TYPE_OBJECT_PATH, &profile_path);
1215 cupsdLogMessage(CUPSD_LOG_DEBUG, "Calling %s:AddProfile(%s) [%s]",
1216 device_path, profile_path, relation);
1219 * Send the AddProfile request synchronously...
1222 dbus_error_init(&error);
1223 reply = dbus_connection_send_with_reply_and_block(colord_con, message,
1224 COLORD_DBUS_TIMEOUT,
1228 cupsdLogMessage(CUPSD_LOG_WARN, "AddProfile failed: %s:%s", error.name,
1230 dbus_error_free(&error);
1237 dbus_message_unref(message);
1240 dbus_message_unref(reply);
1245 * 'colord_dict_add_strings()' - Add two strings to a dictionary.
1249 colord_dict_add_strings(
1250 DBusMessageIter *dict, /* I - Dictionary */
1251 const char *key, /* I - Key string */
1252 const char *value) /* I - Value string */
1254 DBusMessageIter entry; /* Entry to add */
1257 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, NULL, &entry);
1258 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1259 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &value);
1260 dbus_message_iter_close_container(dict, &entry);
1265 * 'colord_find_device()' - Finds a device
1268 static char * /* O - Device path or NULL */
1270 const char *device_id) /* I - Device ID string */
1272 DBusMessage *message = NULL; /* D-Bus request */
1273 DBusMessage *reply = NULL; /* D-Bus reply */
1274 DBusMessageIter args; /* D-Bus method arguments */
1275 DBusError error; /* D-Bus error */
1276 const char *device_path_tmp; /* Device object path */
1277 char *device_path = NULL; /* Device object path */
1280 message = dbus_message_new_method_call(COLORD_DBUS_SERVICE,
1282 COLORD_DBUS_INTERFACE,
1285 dbus_message_iter_init_append(message, &args);
1286 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &device_id);
1289 * Send the FindDeviceById request synchronously...
1292 dbus_error_init(&error);
1293 cupsdLogMessage(CUPSD_LOG_DEBUG, "Calling FindDeviceById(%s)", device_id);
1294 reply = dbus_connection_send_with_reply_and_block(colord_con, message,
1295 COLORD_DBUS_TIMEOUT,
1299 cupsdLogMessage(CUPSD_LOG_DEBUG, "FindDeviceById failed: %s:%s",
1300 error.name, error.message);
1301 dbus_error_free(&error);
1309 dbus_message_iter_init(reply, &args);
1310 if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_OBJECT_PATH)
1312 cupsdLogMessage(CUPSD_LOG_WARN,
1313 "FindDeviceById failed: Incorrect reply type.");
1317 dbus_message_iter_get_basic(&args, &device_path_tmp);
1318 if (device_path_tmp)
1319 device_path = strdup(device_path_tmp);
1324 dbus_message_unref(message);
1327 dbus_message_unref(reply);
1329 return (device_path);
1334 * 'colord_get_qualifier_format()' - Get the qualifier format.
1336 * Note: Returns a value of "ColorSpace.MediaType.Resolution" by default.
1340 colord_get_qualifier_format(
1341 ppd_file_t *ppd, /* I - PPD file data */
1342 char *format[3]) /* I - Format tuple */
1344 const char *tmp; /* Temporary string */
1345 ppd_attr_t *attr; /* Profile attributes */
1349 * Get 1st section...
1352 if ((attr = ppdFindAttr(ppd, "cupsICCQualifier1", NULL)) != NULL)
1354 else if (ppdFindAttr(ppd, "DefaultColorModel", NULL))
1356 else if (ppdFindAttr(ppd, "DefaultColorSpace", NULL))
1361 format[0] = strdup(tmp);
1364 * Get 2nd section...
1367 if ((attr = ppdFindAttr(ppd, "cupsICCQualifier2", NULL)) != NULL)
1372 format[1] = strdup(tmp);
1375 * Get 3rd section...
1378 if ((attr = ppdFindAttr(ppd, "cupsICCQualifier3", NULL)) != NULL)
1383 format[2] = strdup(tmp);
1388 * 'colord_register_printer()' - Register profiles for a printer.
1392 colord_register_printer(
1393 cupsd_printer_t *p) /* I - printer */
1395 char ppdfile[1024], /* PPD filename */
1396 iccfile[1024]; /* ICC filename */
1397 ppd_file_t *ppd; /* PPD file */
1398 cups_array_t *profiles; /* Profile paths array */
1399 ppd_attr_t *attr; /* Profile attributes */
1400 const char *device_colorspace; /* Device colorspace */
1401 char *format[3]; /* Qualifier format tuple */
1405 * Ensure we have a D-Bus connection...
1412 * Try opening the PPD file for this printer...
1415 snprintf(ppdfile, sizeof(ppdfile), "%s/ppd/%s.ppd", ServerRoot, p->name);
1416 if ((ppd = _ppdOpenFile(ppdfile, _PPD_LOCALIZATION_ICC_PROFILES)) == NULL)
1420 * Find out the qualifier format
1423 colord_get_qualifier_format(ppd, format);
1426 * See if we have any embedded profiles...
1429 profiles = cupsArrayNew3(NULL, NULL, NULL, 0, (cups_acopy_func_t)strdup,
1430 (cups_afree_func_t)free);
1431 for (attr = ppdFindAttr(ppd, "cupsICCProfile", NULL);
1433 attr = ppdFindNextAttr(ppd, "cupsICCProfile", NULL))
1434 if (attr->spec[0] && attr->value && attr->value[0])
1436 if (attr->value[0] != '/')
1437 snprintf(iccfile, sizeof(iccfile), "%s/profiles/%s", DataDir,
1440 strlcpy(iccfile, attr->value, sizeof(iccfile));
1442 if (_cupsFileCheck(iccfile, _CUPS_FILE_CHECK_FILE, !RunUser,
1443 cupsdLogFCMessage, p))
1446 colord_create_profile(profiles, p->name, attr->spec, COLORD_SPACE_UNKNOWN,
1447 format, iccfile, COLORD_SCOPE_TEMP);
1451 * Add the grayscale profile first. We always have a grayscale profile.
1454 colord_create_profile(profiles, p->name, "Gray..", COLORD_SPACE_GRAY,
1455 format, NULL, COLORD_SCOPE_TEMP);
1458 * Then add the RGB/CMYK/DeviceN color profile...
1461 device_colorspace = "unknown";
1462 switch (ppd->colorspace)
1466 device_colorspace = COLORD_SPACE_RGB;
1467 colord_create_profile(profiles, p->name, "RGB..", COLORD_SPACE_RGB,
1468 format, NULL, COLORD_SCOPE_TEMP);
1473 device_colorspace = COLORD_SPACE_CMYK;
1474 colord_create_profile(profiles, p->name, "CMYK..", COLORD_SPACE_CMYK,
1475 format, NULL, COLORD_SCOPE_TEMP);
1479 device_colorspace = COLORD_SPACE_GRAY;
1483 colord_create_profile(profiles, p->name, "DeviceN..",
1484 COLORD_SPACE_UNKNOWN, format, NULL,
1490 * Register the device with colord.
1493 cupsdLogMessage(CUPSD_LOG_INFO, "Registering ICC color profiles for \"%s\".",
1495 colord_create_device(p, ppd, profiles, device_colorspace, format,
1496 COLORD_RELATION_SOFT, COLORD_SCOPE_TEMP);
1499 * Free any memory we used...
1502 cupsArrayDelete(profiles);
1513 * 'colord_unregister_printer()' - Unregister profiles for a printer.
1517 colord_unregister_printer(
1518 cupsd_printer_t *p) /* I - printer */
1520 char device_id[1024]; /* Device ID as understood by colord */
1524 * Ensure we have a D-Bus connection...
1531 * Just delete the device itself, and leave the profiles registered
1534 snprintf(device_id, sizeof(device_id), "cups-%s", p->name);
1535 colord_delete_device(device_id);
1537 #endif /* __APPLE__ */
1541 * End of "$Id: colorman.c 11173 2013-07-23 12:31:34Z msweet $".