2 * Destination option/media support for CUPS.
4 * Copyright 2012-2017 by Apple Inc.
6 * These coded instructions, statements, and computer programs are the
7 * property of Apple Inc. and are protected by Federal copyright
8 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
9 * which should have been included with this file. If this file is
10 * missing or damaged, see the license at "http://www.cups.org/".
12 * This file is subject to the Apple OS-Developed Software exception.
16 * Include necessary headers...
19 #include "cups-private.h"
26 #define _CUPS_MEDIA_READY_TTL 30 /* Life of xxx-ready values */
33 static void cups_add_dconstres(cups_array_t
*a
, ipp_t
*collection
);
34 static int cups_compare_dconstres(_cups_dconstres_t
*a
,
35 _cups_dconstres_t
*b
);
36 static int cups_compare_media_db(_cups_media_db_t
*a
,
38 static _cups_media_db_t
*cups_copy_media_db(_cups_media_db_t
*mdb
);
39 static void cups_create_cached(http_t
*http
, cups_dinfo_t
*dinfo
,
41 static void cups_create_constraints(cups_dinfo_t
*dinfo
);
42 static void cups_create_defaults(cups_dinfo_t
*dinfo
);
43 static void cups_create_media_db(cups_dinfo_t
*dinfo
,
45 static void cups_free_media_db(_cups_media_db_t
*mdb
);
46 static int cups_get_media_db(http_t
*http
, cups_dinfo_t
*dinfo
,
47 pwg_media_t
*pwg
, unsigned flags
,
49 static int cups_is_close_media_db(_cups_media_db_t
*a
,
51 static cups_array_t
*cups_test_constraints(cups_dinfo_t
*dinfo
,
52 const char *new_option
,
53 const char *new_value
,
55 cups_option_t
*options
,
57 cups_option_t
**conflicts
);
58 static void cups_update_ready(http_t
*http
, cups_dinfo_t
*dinfo
);
62 * 'cupsCheckDestSupported()' - Check that the option and value are supported
65 * Returns 1 if supported, 0 otherwise.
67 * @since CUPS 1.6/macOS 10.8@
70 int /* O - 1 if supported, 0 otherwise */
71 cupsCheckDestSupported(
72 http_t
*http
, /* I - Connection to destination */
73 cups_dest_t
*dest
, /* I - Destination */
74 cups_dinfo_t
*dinfo
, /* I - Destination information */
75 const char *option
, /* I - Option */
76 const char *value
) /* I - Value */
78 int i
; /* Looping var */
79 char temp
[1024]; /* Temporary string */
80 int int_value
; /* Integer value */
81 int xres_value
, /* Horizontal resolution */
82 yres_value
; /* Vertical resolution */
83 ipp_res_t units_value
; /* Resolution units */
84 ipp_attribute_t
*attr
; /* Attribute */
85 _ipp_value_t
*attrval
; /* Current attribute value */
89 * Get the default connection as needed...
93 http
= _cupsConnect();
96 * Range check input...
99 if (!http
|| !dest
|| !dinfo
|| !option
|| !value
)
103 * Lookup the attribute...
106 if (strstr(option
, "-supported"))
107 attr
= ippFindAttribute(dinfo
->attrs
, option
, IPP_TAG_ZERO
);
110 snprintf(temp
, sizeof(temp
), "%s-supported", option
);
111 attr
= ippFindAttribute(dinfo
->attrs
, temp
, IPP_TAG_ZERO
);
121 if (!strcmp(option
, "media") && !strncmp(value
, "custom_", 7))
124 * Check range of custom media sizes...
127 pwg_media_t
*pwg
; /* Current PWG media size info */
128 int min_width
, /* Minimum width */
129 min_length
, /* Minimum length */
130 max_width
, /* Maximum width */
131 max_length
; /* Maximum length */
134 * Get the minimum and maximum size...
137 min_width
= min_length
= INT_MAX
;
138 max_width
= max_length
= 0;
140 for (i
= attr
->num_values
, attrval
= attr
->values
;
144 if (!strncmp(attrval
->string
.text
, "custom_min_", 11) &&
145 (pwg
= pwgMediaForPWG(attrval
->string
.text
)) != NULL
)
147 min_width
= pwg
->width
;
148 min_length
= pwg
->length
;
150 else if (!strncmp(attrval
->string
.text
, "custom_max_", 11) &&
151 (pwg
= pwgMediaForPWG(attrval
->string
.text
)) != NULL
)
153 max_width
= pwg
->width
;
154 max_length
= pwg
->length
;
162 if (min_width
< INT_MAX
&& max_width
> 0 &&
163 (pwg
= pwgMediaForPWG(value
)) != NULL
&&
164 pwg
->width
>= min_width
&& pwg
->width
<= max_width
&&
165 pwg
->length
>= min_length
&& pwg
->length
<= max_length
)
171 * Check literal values...
174 switch (attr
->value_tag
)
176 case IPP_TAG_INTEGER
:
178 int_value
= atoi(value
);
180 for (i
= 0; i
< attr
->num_values
; i
++)
181 if (attr
->values
[i
].integer
== int_value
)
185 case IPP_TAG_BOOLEAN
:
186 return (attr
->values
[0].boolean
);
189 int_value
= atoi(value
);
191 for (i
= 0; i
< attr
->num_values
; i
++)
192 if (int_value
>= attr
->values
[i
].range
.lower
&&
193 int_value
<= attr
->values
[i
].range
.upper
)
197 case IPP_TAG_RESOLUTION
:
198 if (sscanf(value
, "%dx%d%15s", &xres_value
, &yres_value
, temp
) != 3)
200 if (sscanf(value
, "%d%15s", &xres_value
, temp
) != 2)
203 yres_value
= xres_value
;
206 if (!strcmp(temp
, "dpi"))
207 units_value
= IPP_RES_PER_INCH
;
208 else if (!strcmp(temp
, "dpc") || !strcmp(temp
, "dpcm"))
209 units_value
= IPP_RES_PER_CM
;
213 for (i
= attr
->num_values
, attrval
= attr
->values
;
217 if (attrval
->resolution
.xres
== xres_value
&&
218 attrval
->resolution
.yres
== yres_value
&&
219 attrval
->resolution
.units
== units_value
)
226 case IPP_TAG_KEYWORD
:
227 case IPP_TAG_CHARSET
:
229 case IPP_TAG_URISCHEME
:
230 case IPP_TAG_MIMETYPE
:
231 case IPP_TAG_LANGUAGE
:
232 case IPP_TAG_TEXTLANG
:
233 case IPP_TAG_NAMELANG
:
234 for (i
= 0; i
< attr
->num_values
; i
++)
235 if (!strcmp(attr
->values
[i
].string
.text
, value
))
245 * If we get there the option+value is not supported...
253 * 'cupsCopyDestConflicts()' - Get conflicts and resolutions for a new
256 * "num_options" and "options" represent the currently selected options by the
257 * user. "new_option" and "new_value" are the setting the user has just
260 * Returns 1 if there is a conflict, 0 if there are no conflicts, and -1 if
261 * there was an unrecoverable error such as a resolver loop.
263 * If "num_conflicts" and "conflicts" are not @code NULL@, they are set to
264 * contain the list of conflicting option/value pairs. Similarly, if
265 * "num_resolved" and "resolved" are not @code NULL@ they will be set to the
266 * list of changes needed to resolve the conflict.
268 * If cupsCopyDestConflicts returns 1 but "num_resolved" and "resolved" are set
269 * to 0 and @code NULL@, respectively, then the conflict cannot be resolved.
271 * @since CUPS 1.6/macOS 10.8@
274 int /* O - 1 if there is a conflict, 0 if none, -1 on error */
275 cupsCopyDestConflicts(
276 http_t
*http
, /* I - Connection to destination */
277 cups_dest_t
*dest
, /* I - Destination */
278 cups_dinfo_t
*dinfo
, /* I - Destination information */
279 int num_options
, /* I - Number of current options */
280 cups_option_t
*options
, /* I - Current options */
281 const char *new_option
, /* I - New option */
282 const char *new_value
, /* I - New value */
283 int *num_conflicts
, /* O - Number of conflicting options */
284 cups_option_t
**conflicts
, /* O - Conflicting options */
285 int *num_resolved
, /* O - Number of options to resolve */
286 cups_option_t
**resolved
) /* O - Resolved options */
288 int i
, /* Looping var */
289 have_conflicts
= 0, /* Do we have conflicts? */
290 changed
, /* Did we change something? */
291 tries
, /* Number of tries for resolution */
292 num_myconf
= 0, /* My number of conflicting options */
293 num_myres
= 0; /* My number of resolved options */
294 cups_option_t
*myconf
= NULL
, /* My conflicting options */
295 *myres
= NULL
, /* My resolved options */
296 *myoption
, /* My current option */
297 *option
; /* Current option */
298 cups_array_t
*active
= NULL
, /* Active conflicts */
299 *pass
= NULL
, /* Resolvers for this pass */
300 *resolvers
= NULL
, /* Resolvers we have used */
301 *test
; /* Test array for conflicts */
302 _cups_dconstres_t
*c
, /* Current constraint */
303 *r
; /* Current resolver */
304 ipp_attribute_t
*attr
; /* Current attribute */
305 char value
[2048]; /* Current attribute value as string */
306 const char *myvalue
; /* Current value of an option */
310 * Clear returned values...
326 * Get the default connection as needed...
330 http
= _cupsConnect();
333 * Range check input...
336 if (!http
|| !dest
|| !dinfo
||
337 (num_conflicts
!= NULL
) != (conflicts
!= NULL
) ||
338 (num_resolved
!= NULL
) != (resolved
!= NULL
))
342 * Load constraints as needed...
345 if (!dinfo
->constraints
)
346 cups_create_constraints(dinfo
);
348 if (cupsArrayCount(dinfo
->constraints
) == 0)
351 if (!dinfo
->num_defaults
)
352 cups_create_defaults(dinfo
);
355 * If we are resolving, create a shadow array...
360 for (i
= num_options
, option
= options
; i
> 0; i
--, option
++)
361 num_myres
= cupsAddOption(option
->name
, option
->value
, num_myres
, &myres
);
363 if (new_option
&& new_value
)
364 num_myres
= cupsAddOption(new_option
, new_value
, num_myres
, &myres
);
368 num_myres
= num_options
;
373 * Check for any conflicts...
377 pass
= cupsArrayNew((cups_array_func_t
)cups_compare_dconstres
, NULL
);
379 for (tries
= 0; tries
< 100; tries
++)
382 * Check for any conflicts...
385 if (num_conflicts
|| num_resolved
)
387 cupsFreeOptions(num_myconf
, myconf
);
391 active
= cups_test_constraints(dinfo
, new_option
, new_value
,
392 num_myres
, myres
, &num_myconf
,
396 active
= cups_test_constraints(dinfo
, new_option
, new_value
, num_myres
,
399 have_conflicts
= (active
!= NULL
);
401 if (!active
|| !num_resolved
)
402 break; /* All done */
405 * Scan the constraints that were triggered to apply resolvers...
409 resolvers
= cupsArrayNew((cups_array_func_t
)cups_compare_dconstres
, NULL
);
411 for (c
= (_cups_dconstres_t
*)cupsArrayFirst(active
), changed
= 0;
413 c
= (_cups_dconstres_t
*)cupsArrayNext(active
))
415 if (cupsArrayFind(pass
, c
))
416 continue; /* Already applied this resolver... */
418 if (cupsArrayFind(resolvers
, c
))
420 DEBUG_printf(("1cupsCopyDestConflicts: Resolver loop with %s.",
426 if ((r
= cupsArrayFind(dinfo
->resolvers
, c
)) == NULL
)
428 DEBUG_printf(("1cupsCopyDestConflicts: Resolver %s not found.",
435 * Add the options from the resolver...
438 cupsArrayAdd(pass
, r
);
439 cupsArrayAdd(resolvers
, r
);
441 for (attr
= ippFirstAttribute(r
->collection
);
443 attr
= ippNextAttribute(r
->collection
))
445 if (new_option
&& !strcmp(attr
->name
, new_option
))
446 continue; /* Ignore this if we just changed it */
448 if (ippAttributeString(attr
, value
, sizeof(value
)) >= sizeof(value
))
449 continue; /* Ignore if the value is too long */
451 if ((test
= cups_test_constraints(dinfo
, attr
->name
, value
, num_myres
,
452 myres
, NULL
, NULL
)) == NULL
)
455 * That worked, flag it...
461 cupsArrayDelete(test
);
464 * Add the option/value from the resolver regardless of whether it
465 * worked; this makes sure that we can cascade several changes to
466 * make things resolve...
469 num_myres
= cupsAddOption(attr
->name
, value
, num_myres
, &myres
);
475 DEBUG_puts("1cupsCopyDestConflicts: Unable to resolve constraints.");
480 cupsArrayClear(pass
);
482 cupsArrayDelete(active
);
488 DEBUG_puts("1cupsCopyDestConflicts: Unable to resolve after 100 tries.");
494 * Copy resolved options as needed...
499 for (i
= num_myres
, myoption
= myres
; i
> 0; i
--, myoption
++)
501 if ((myvalue
= cupsGetOption(myoption
->name
, num_options
,
503 strcmp(myvalue
, myoption
->value
))
505 if (new_option
&& !strcmp(new_option
, myoption
->name
) &&
506 new_value
&& !strcmp(new_value
, myoption
->value
))
509 *num_resolved
= cupsAddOption(myoption
->name
, myoption
->value
,
510 *num_resolved
, resolved
);
521 cupsArrayDelete(active
);
522 cupsArrayDelete(pass
);
523 cupsArrayDelete(resolvers
);
528 * Free shadow copy of options...
531 cupsFreeOptions(num_myres
, myres
);
537 * Return conflicting options to caller...
540 *num_conflicts
= num_myconf
;
546 * Free conflicting options...
549 cupsFreeOptions(num_myconf
, myconf
);
552 return (have_conflicts
);
557 * 'cupsCopyDestInfo()' - Get the supported values/capabilities for the
560 * The caller is responsible for calling @link cupsFreeDestInfo@ on the return
561 * value. @code NULL@ is returned on error.
563 * @since CUPS 1.6/macOS 10.8@
566 cups_dinfo_t
* /* O - Destination information */
568 http_t
*http
, /* I - Connection to destination */
569 cups_dest_t
*dest
) /* I - Destination */
571 cups_dinfo_t
*dinfo
; /* Destination information */
572 ipp_t
*request
, /* Get-Printer-Attributes request */
573 *response
; /* Supported attributes */
574 int tries
, /* Number of tries so far */
575 delay
, /* Current retry delay */
576 prev_delay
; /* Next retry delay */
577 const char *uri
; /* Printer URI */
578 char resource
[1024]; /* Resource path */
579 int version
; /* IPP version */
580 ipp_status_t status
; /* Status of request */
581 static const char * const requested_attrs
[] =
582 { /* Requested attributes */
584 "media-col-database",
585 "printer-description"
589 DEBUG_printf(("cupsCopyDestSupported(http=%p, dest=%p(%s))", (void *)http
, (void *)dest
, dest
? dest
->name
: ""));
592 * Get the default connection as needed...
596 http
= _cupsConnect();
599 * Range check input...
606 * Get the printer URI and resource path...
609 if ((uri
= _cupsGetDestResource(dest
, resource
, sizeof(resource
))) == NULL
)
613 * Get the supported attributes...
624 * Send a Get-Printer-Attributes request...
627 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
628 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
630 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
631 "requesting-user-name", NULL
, cupsUser());
632 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
633 "requested-attributes",
634 (int)(sizeof(requested_attrs
) / sizeof(requested_attrs
[0])),
635 NULL
, requested_attrs
);
636 response
= cupsDoRequest(http
, request
, resource
);
637 status
= cupsLastError();
639 if (status
> IPP_STATUS_OK_IGNORED_OR_SUBSTITUTED
)
641 DEBUG_printf(("cupsCopyDestSupported: Get-Printer-Attributes for '%s' "
642 "returned %s (%s)", dest
->name
, ippErrorString(status
),
643 cupsLastErrorString()));
648 if (status
== IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
&& version
> 11)
650 else if (status
== IPP_STATUS_ERROR_BUSY
)
652 sleep((unsigned)delay
);
654 delay
= _cupsNextDelay(delay
, &prev_delay
);
662 while (!response
&& tries
< 10);
668 * Allocate a cups_dinfo_t structure and return it...
671 if ((dinfo
= calloc(1, sizeof(cups_dinfo_t
))) == NULL
)
673 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
678 dinfo
->version
= version
;
680 dinfo
->resource
= _cupsStrAlloc(resource
);
681 dinfo
->attrs
= response
;
688 * 'cupsFindDestDefault()' - Find the default value(s) for the given option.
690 * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
691 * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
692 * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
693 * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
694 * functions to inspect the default value(s) as needed.
696 * @since CUPS 1.7/macOS 10.9@
699 ipp_attribute_t
* /* O - Default attribute or @code NULL@ for none */
701 http_t
*http
, /* I - Connection to destination */
702 cups_dest_t
*dest
, /* I - Destination */
703 cups_dinfo_t
*dinfo
, /* I - Destination information */
704 const char *option
) /* I - Option/attribute name */
706 char name
[IPP_MAX_NAME
]; /* Attribute name */
710 * Get the default connection as needed...
714 http
= _cupsConnect();
717 * Range check input...
720 if (!http
|| !dest
|| !dinfo
|| !option
)
722 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
727 * Find and return the attribute...
730 snprintf(name
, sizeof(name
), "%s-default", option
);
731 return (ippFindAttribute(dinfo
->attrs
, name
, IPP_TAG_ZERO
));
736 * 'cupsFindDestReady()' - Find the default value(s) for the given option.
738 * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
739 * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
740 * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
741 * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
742 * functions to inspect the default value(s) as needed.
744 * @since CUPS 1.7/macOS 10.9@
747 ipp_attribute_t
* /* O - Default attribute or @code NULL@ for none */
749 http_t
*http
, /* I - Connection to destination */
750 cups_dest_t
*dest
, /* I - Destination */
751 cups_dinfo_t
*dinfo
, /* I - Destination information */
752 const char *option
) /* I - Option/attribute name */
754 char name
[IPP_MAX_NAME
]; /* Attribute name */
758 * Get the default connection as needed...
762 http
= _cupsConnect();
765 * Range check input...
768 if (!http
|| !dest
|| !dinfo
|| !option
)
770 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
775 * Find and return the attribute...
778 cups_update_ready(http
, dinfo
);
780 snprintf(name
, sizeof(name
), "%s-ready", option
);
781 return (ippFindAttribute(dinfo
->ready_attrs
, name
, IPP_TAG_ZERO
));
786 * 'cupsFindDestSupported()' - Find the default value(s) for the given option.
788 * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
789 * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
790 * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
791 * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
792 * functions to inspect the default value(s) as needed.
794 * @since CUPS 1.7/macOS 10.9@
797 ipp_attribute_t
* /* O - Default attribute or @code NULL@ for none */
798 cupsFindDestSupported(
799 http_t
*http
, /* I - Connection to destination */
800 cups_dest_t
*dest
, /* I - Destination */
801 cups_dinfo_t
*dinfo
, /* I - Destination information */
802 const char *option
) /* I - Option/attribute name */
804 char name
[IPP_MAX_NAME
]; /* Attribute name */
808 * Get the default connection as needed...
812 http
= _cupsConnect();
815 * Range check input...
818 if (!http
|| !dest
|| !dinfo
|| !option
)
820 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
825 * Find and return the attribute...
828 snprintf(name
, sizeof(name
), "%s-supported", option
);
829 return (ippFindAttribute(dinfo
->attrs
, name
, IPP_TAG_ZERO
));
834 * 'cupsFreeDestInfo()' - Free destination information obtained using
835 * @link cupsCopyDestInfo@.
839 cupsFreeDestInfo(cups_dinfo_t
*dinfo
) /* I - Destination information */
842 * Range check input...
849 * Free memory and return...
852 _cupsStrFree(dinfo
->resource
);
854 cupsArrayDelete(dinfo
->constraints
);
855 cupsArrayDelete(dinfo
->resolvers
);
857 cupsArrayDelete(dinfo
->localizations
);
859 cupsArrayDelete(dinfo
->media_db
);
861 cupsArrayDelete(dinfo
->cached_db
);
863 ippDelete(dinfo
->ready_attrs
);
864 cupsArrayDelete(dinfo
->ready_db
);
866 ippDelete(dinfo
->attrs
);
873 * 'cupsGetDestMediaByIndex()' - Get a media name, dimension, and margins for a
876 * The @code flags@ parameter determines which set of media are indexed. For
877 * example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will get the Nth
878 * borderless size supported by the printer.
880 * @since CUPS 1.7/macOS 10.9@
883 int /* O - 1 on success, 0 on failure */
884 cupsGetDestMediaByIndex(
885 http_t
*http
, /* I - Connection to destination */
886 cups_dest_t
*dest
, /* I - Destination */
887 cups_dinfo_t
*dinfo
, /* I - Destination information */
888 int n
, /* I - Media size number (0-based) */
889 unsigned flags
, /* I - Media flags */
890 cups_size_t
*size
) /* O - Media size information */
892 _cups_media_db_t
*nsize
; /* Size for N */
893 pwg_media_t
*pwg
; /* PWG media name for size */
897 * Get the default connection as needed...
901 http
= _cupsConnect();
904 * Range check input...
908 memset(size
, 0, sizeof(cups_size_t
));
910 if (!http
|| !dest
|| !dinfo
|| n
< 0 || !size
)
912 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
917 * Load media list as needed...
920 if (flags
& CUPS_MEDIA_FLAGS_READY
)
921 cups_update_ready(http
, dinfo
);
923 if (!dinfo
->cached_db
|| dinfo
->cached_flags
!= flags
)
924 cups_create_cached(http
, dinfo
, flags
);
927 * Copy the size over and return...
930 if ((nsize
= (_cups_media_db_t
*)cupsArrayIndex(dinfo
->cached_db
, n
)) == NULL
)
932 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
936 if (nsize
->size_name
)
937 strlcpy(size
->media
, nsize
->size_name
, sizeof(size
->media
));
939 strlcpy(size
->media
, nsize
->key
, sizeof(size
->media
));
940 else if ((pwg
= pwgMediaForSize(nsize
->width
, nsize
->length
)) != NULL
)
941 strlcpy(size
->media
, pwg
->pwg
, sizeof(size
->media
));
944 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
948 size
->width
= nsize
->width
;
949 size
->length
= nsize
->length
;
950 size
->bottom
= nsize
->bottom
;
951 size
->left
= nsize
->left
;
952 size
->right
= nsize
->right
;
953 size
->top
= nsize
->top
;
960 * 'cupsGetDestMediaByName()' - Get media names, dimensions, and margins.
962 * The "media" string is a PWG media name. "Flags" provides some matching
963 * guidance (multiple flags can be combined):
965 * CUPS_MEDIA_FLAGS_DEFAULT = find the closest size supported by the printer,
966 * CUPS_MEDIA_FLAGS_BORDERLESS = find a borderless size,
967 * CUPS_MEDIA_FLAGS_DUPLEX = find a size compatible with 2-sided printing,
968 * CUPS_MEDIA_FLAGS_EXACT = find an exact match for the size, and
969 * CUPS_MEDIA_FLAGS_READY = if the printer supports media sensing, find the
970 * size amongst the "ready" media.
972 * The matching result (if any) is returned in the "cups_size_t" structure.
974 * Returns 1 when there is a match and 0 if there is not a match.
976 * @since CUPS 1.6/macOS 10.8@
979 int /* O - 1 on match, 0 on failure */
980 cupsGetDestMediaByName(
981 http_t
*http
, /* I - Connection to destination */
982 cups_dest_t
*dest
, /* I - Destination */
983 cups_dinfo_t
*dinfo
, /* I - Destination information */
984 const char *media
, /* I - Media name */
985 unsigned flags
, /* I - Media matching flags */
986 cups_size_t
*size
) /* O - Media size information */
988 pwg_media_t
*pwg
; /* PWG media info */
992 * Get the default connection as needed...
996 http
= _cupsConnect();
999 * Range check input...
1003 memset(size
, 0, sizeof(cups_size_t
));
1005 if (!http
|| !dest
|| !dinfo
|| !media
|| !size
)
1007 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1012 * Lookup the media size name...
1015 if ((pwg
= pwgMediaForPWG(media
)) == NULL
)
1016 if ((pwg
= pwgMediaForLegacy(media
)) == NULL
)
1018 DEBUG_printf(("1cupsGetDestMediaByName: Unknown size '%s'.", media
));
1019 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown media size name."), 1);
1024 * Lookup the size...
1027 return (cups_get_media_db(http
, dinfo
, pwg
, flags
, size
));
1032 * 'cupsGetDestMediaBySize()' - Get media names, dimensions, and margins.
1034 * "Width" and "length" are the dimensions in hundredths of millimeters.
1035 * "Flags" provides some matching guidance (multiple flags can be combined):
1037 * CUPS_MEDIA_FLAGS_DEFAULT = find the closest size supported by the printer,
1038 * CUPS_MEDIA_FLAGS_BORDERLESS = find a borderless size,
1039 * CUPS_MEDIA_FLAGS_DUPLEX = find a size compatible with 2-sided printing,
1040 * CUPS_MEDIA_FLAGS_EXACT = find an exact match for the size, and
1041 * CUPS_MEDIA_FLAGS_READY = if the printer supports media sensing, find the
1042 * size amongst the "ready" media.
1044 * The matching result (if any) is returned in the "cups_size_t" structure.
1046 * Returns 1 when there is a match and 0 if there is not a match.
1048 * @since CUPS 1.6/macOS 10.8@
1051 int /* O - 1 on match, 0 on failure */
1052 cupsGetDestMediaBySize(
1053 http_t
*http
, /* I - Connection to destination */
1054 cups_dest_t
*dest
, /* I - Destination */
1055 cups_dinfo_t
*dinfo
, /* I - Destination information */
1056 int width
, /* I - Media width in hundredths of
1058 int length
, /* I - Media length in hundredths of
1060 unsigned flags
, /* I - Media matching flags */
1061 cups_size_t
*size
) /* O - Media size information */
1063 pwg_media_t
*pwg
; /* PWG media info */
1067 * Get the default connection as needed...
1071 http
= _cupsConnect();
1074 * Range check input...
1078 memset(size
, 0, sizeof(cups_size_t
));
1080 if (!http
|| !dest
|| !dinfo
|| width
<= 0 || length
<= 0 || !size
)
1082 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1087 * Lookup the media size name...
1090 if ((pwg
= pwgMediaForSize(width
, length
)) == NULL
)
1092 DEBUG_printf(("1cupsGetDestMediaBySize: Invalid size %dx%d.", width
,
1094 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Invalid media size."), 1);
1099 * Lookup the size...
1102 return (cups_get_media_db(http
, dinfo
, pwg
, flags
, size
));
1107 * 'cupsGetDestMediaCount()' - Get the number of sizes supported by a
1110 * The @code flags@ parameter determines the set of media sizes that are
1111 * counted. For example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will return
1112 * the number of borderless sizes.
1114 * @since CUPS 1.7/macOS 10.9@
1117 int /* O - Number of sizes */
1118 cupsGetDestMediaCount(
1119 http_t
*http
, /* I - Connection to destination */
1120 cups_dest_t
*dest
, /* I - Destination */
1121 cups_dinfo_t
*dinfo
, /* I - Destination information */
1122 unsigned flags
) /* I - Media flags */
1125 * Get the default connection as needed...
1129 http
= _cupsConnect();
1132 * Range check input...
1135 if (!http
|| !dest
|| !dinfo
)
1137 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1142 * Load media list as needed...
1145 if (flags
& CUPS_MEDIA_FLAGS_READY
)
1146 cups_update_ready(http
, dinfo
);
1148 if (!dinfo
->cached_db
|| dinfo
->cached_flags
!= flags
)
1149 cups_create_cached(http
, dinfo
, flags
);
1151 return (cupsArrayCount(dinfo
->cached_db
));
1156 * 'cupsGetDestMediaDefault()' - Get the default size for a destination.
1158 * The @code flags@ parameter determines which default size is returned. For
1159 * example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will return the default
1160 * borderless size, typically US Letter or A4, but sometimes 4x6 photo media.
1162 * @since CUPS 1.7/macOS 10.9@
1165 int /* O - 1 on success, 0 on failure */
1166 cupsGetDestMediaDefault(
1167 http_t
*http
, /* I - Connection to destination */
1168 cups_dest_t
*dest
, /* I - Destination */
1169 cups_dinfo_t
*dinfo
, /* I - Destination information */
1170 unsigned flags
, /* I - Media flags */
1171 cups_size_t
*size
) /* O - Media size information */
1173 const char *media
; /* Default media size */
1177 * Get the default connection as needed...
1181 http
= _cupsConnect();
1184 * Range check input...
1188 memset(size
, 0, sizeof(cups_size_t
));
1190 if (!http
|| !dest
|| !dinfo
|| !size
)
1192 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1197 * Get the default media size, if any...
1200 if ((media
= cupsGetOption("media", dest
->num_options
,
1201 dest
->options
)) == NULL
)
1202 media
= "na_letter_8.5x11in";
1204 if (cupsGetDestMediaByName(http
, dest
, dinfo
, media
, flags
, size
))
1207 if (strcmp(media
, "na_letter_8.5x11in") &&
1208 cupsGetDestMediaByName(http
, dest
, dinfo
, "iso_a4_210x297mm", flags
,
1212 if (strcmp(media
, "iso_a4_210x297mm") &&
1213 cupsGetDestMediaByName(http
, dest
, dinfo
, "na_letter_8.5x11in", flags
,
1217 if ((flags
& CUPS_MEDIA_FLAGS_BORDERLESS
) &&
1218 cupsGetDestMediaByName(http
, dest
, dinfo
, "na_index_4x6in", flags
, size
))
1222 * Fall back to the first matching media size...
1225 return (cupsGetDestMediaByIndex(http
, dest
, dinfo
, 0, flags
, size
));
1230 * 'cups_add_dconstres()' - Add a constraint or resolver to an array.
1235 cups_array_t
*a
, /* I - Array */
1236 ipp_t
*collection
) /* I - Collection value */
1238 ipp_attribute_t
*attr
; /* Attribute */
1239 _cups_dconstres_t
*temp
; /* Current constraint/resolver */
1242 if ((attr
= ippFindAttribute(collection
, "resolver-name",
1243 IPP_TAG_NAME
)) == NULL
)
1246 if ((temp
= calloc(1, sizeof(_cups_dconstres_t
))) == NULL
)
1249 temp
->name
= attr
->values
[0].string
.text
;
1250 temp
->collection
= collection
;
1252 cupsArrayAdd(a
, temp
);
1257 * 'cups_compare_dconstres()' - Compare to resolver entries.
1260 static int /* O - Result of comparison */
1261 cups_compare_dconstres(
1262 _cups_dconstres_t
*a
, /* I - First resolver */
1263 _cups_dconstres_t
*b
) /* I - Second resolver */
1265 return (strcmp(a
->name
, b
->name
));
1270 * 'cups_compare_media_db()' - Compare two media entries.
1273 static int /* O - Result of comparison */
1274 cups_compare_media_db(
1275 _cups_media_db_t
*a
, /* I - First media entries */
1276 _cups_media_db_t
*b
) /* I - Second media entries */
1278 int result
; /* Result of comparison */
1281 if ((result
= a
->width
- b
->width
) == 0)
1282 result
= a
->length
- b
->length
;
1289 * 'cups_copy_media_db()' - Copy a media entry.
1292 static _cups_media_db_t
* /* O - New media entry */
1294 _cups_media_db_t
*mdb
) /* I - Media entry to copy */
1296 _cups_media_db_t
*temp
; /* New media entry */
1299 if ((temp
= calloc(1, sizeof(_cups_media_db_t
))) == NULL
)
1303 temp
->color
= _cupsStrAlloc(mdb
->color
);
1305 temp
->key
= _cupsStrAlloc(mdb
->key
);
1307 temp
->info
= _cupsStrAlloc(mdb
->info
);
1309 temp
->size_name
= _cupsStrAlloc(mdb
->size_name
);
1311 temp
->source
= _cupsStrAlloc(mdb
->source
);
1313 temp
->type
= _cupsStrAlloc(mdb
->type
);
1315 temp
->width
= mdb
->width
;
1316 temp
->length
= mdb
->length
;
1317 temp
->bottom
= mdb
->bottom
;
1318 temp
->left
= mdb
->left
;
1319 temp
->right
= mdb
->right
;
1320 temp
->top
= mdb
->top
;
1327 * 'cups_create_cached()' - Create the media selection cache.
1331 cups_create_cached(http_t
*http
, /* I - Connection to destination */
1332 cups_dinfo_t
*dinfo
, /* I - Destination information */
1333 unsigned flags
) /* I - Media selection flags */
1335 cups_array_t
*db
; /* Media database array to use */
1336 _cups_media_db_t
*mdb
, /* Media database entry */
1337 *first
; /* First entry this size */
1340 DEBUG_printf(("3cups_create_cached(http=%p, dinfo=%p, flags=%u)", (void *)http
, (void *)dinfo
, flags
));
1342 if (dinfo
->cached_db
)
1343 cupsArrayDelete(dinfo
->cached_db
);
1345 dinfo
->cached_db
= cupsArrayNew(NULL
, NULL
);
1346 dinfo
->cached_flags
= flags
;
1348 if (flags
& CUPS_MEDIA_FLAGS_READY
)
1350 DEBUG_puts("4cups_create_cached: ready media");
1352 cups_update_ready(http
, dinfo
);
1353 db
= dinfo
->ready_db
;
1357 DEBUG_puts("4cups_create_cached: supported media");
1359 if (!dinfo
->media_db
)
1360 cups_create_media_db(dinfo
, CUPS_MEDIA_FLAGS_DEFAULT
);
1362 db
= dinfo
->media_db
;
1365 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
), first
= mdb
;
1367 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1369 DEBUG_printf(("4cups_create_cached: %p key=\"%s\", type=\"%s\", %dx%d, B%d L%d R%d T%d", (void *)mdb
, mdb
->key
, mdb
->type
, mdb
->width
, mdb
->length
, mdb
->bottom
, mdb
->left
, mdb
->right
, mdb
->top
));
1371 if (flags
& CUPS_MEDIA_FLAGS_BORDERLESS
)
1373 if (!mdb
->left
&& !mdb
->right
&& !mdb
->top
&& !mdb
->bottom
)
1375 DEBUG_printf(("4cups_create_cached: add %p", (void *)mdb
));
1376 cupsArrayAdd(dinfo
->cached_db
, mdb
);
1379 else if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
1381 if (first
->width
!= mdb
->width
|| first
->length
!= mdb
->length
)
1383 DEBUG_printf(("4cups_create_cached: add %p", (void *)first
));
1384 cupsArrayAdd(dinfo
->cached_db
, first
);
1387 else if (mdb
->left
>= first
->left
&& mdb
->right
>= first
->right
&& mdb
->top
>= first
->top
&& mdb
->bottom
>= first
->bottom
&&
1388 (mdb
->left
!= first
->left
|| mdb
->right
!= first
->right
|| mdb
->top
!= first
->top
|| mdb
->bottom
!= first
->bottom
))
1393 DEBUG_printf(("4cups_create_cached: add %p", (void *)mdb
));
1394 cupsArrayAdd(dinfo
->cached_db
, mdb
);
1398 if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
1400 DEBUG_printf(("4cups_create_cached: add %p", (void *)first
));
1401 cupsArrayAdd(dinfo
->cached_db
, first
);
1407 * 'cups_create_constraints()' - Create the constraints and resolvers arrays.
1411 cups_create_constraints(
1412 cups_dinfo_t
*dinfo
) /* I - Destination information */
1414 int i
; /* Looping var */
1415 ipp_attribute_t
*attr
; /* Attribute */
1416 _ipp_value_t
*val
; /* Current value */
1419 dinfo
->constraints
= cupsArrayNew3(NULL
, NULL
, NULL
, 0, NULL
,
1420 (cups_afree_func_t
)free
);
1421 dinfo
->resolvers
= cupsArrayNew3((cups_array_func_t
)cups_compare_dconstres
,
1422 NULL
, NULL
, 0, NULL
,
1423 (cups_afree_func_t
)free
);
1425 if ((attr
= ippFindAttribute(dinfo
->attrs
, "job-constraints-supported",
1426 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1428 for (i
= attr
->num_values
, val
= attr
->values
; i
> 0; i
--, val
++)
1429 cups_add_dconstres(dinfo
->constraints
, val
->collection
);
1432 if ((attr
= ippFindAttribute(dinfo
->attrs
, "job-resolvers-supported",
1433 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1435 for (i
= attr
->num_values
, val
= attr
->values
; i
> 0; i
--, val
++)
1436 cups_add_dconstres(dinfo
->resolvers
, val
->collection
);
1442 * 'cups_create_defaults()' - Create the -default option array.
1444 * TODO: Need to support collection defaults...
1448 cups_create_defaults(
1449 cups_dinfo_t
*dinfo
) /* I - Destination information */
1451 ipp_attribute_t
*attr
; /* Current attribute */
1452 char name
[IPP_MAX_NAME
+ 1],
1454 *nameptr
, /* Pointer into current name */
1455 value
[2048]; /* Current value */
1459 * Iterate through the printer attributes looking for xxx-default and adding
1460 * xxx=value to the defaults option array.
1463 for (attr
= ippFirstAttribute(dinfo
->attrs
);
1465 attr
= ippNextAttribute(dinfo
->attrs
))
1467 if (!attr
->name
|| attr
->group_tag
!= IPP_TAG_PRINTER
)
1470 if (attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
)
1471 continue; /* TODO: STR #4096 */
1473 if ((nameptr
= attr
->name
+ strlen(attr
->name
) - 8) <= attr
->name
||
1474 strcmp(nameptr
, "-default"))
1477 strlcpy(name
, attr
->name
, sizeof(name
));
1478 if ((nameptr
= name
+ strlen(name
) - 8) <= name
||
1479 strcmp(nameptr
, "-default"))
1484 if (ippAttributeString(attr
, value
, sizeof(value
)) >= sizeof(value
))
1487 dinfo
->num_defaults
= cupsAddOption(name
, value
, dinfo
->num_defaults
,
1494 * 'cups_create_media_db()' - Create the media database.
1498 cups_create_media_db(
1499 cups_dinfo_t
*dinfo
, /* I - Destination information */
1500 unsigned flags
) /* I - Media flags */
1502 int i
; /* Looping var */
1503 _ipp_value_t
*val
; /* Current value */
1504 ipp_attribute_t
*media_col_db
, /* media-col-database */
1505 *media_attr
, /* media-xxx */
1506 *x_dimension
, /* x-dimension */
1507 *y_dimension
; /* y-dimension */
1508 pwg_media_t
*pwg
; /* PWG media info */
1509 cups_array_t
*db
; /* New media database array */
1510 _cups_media_db_t mdb
; /* Media entry */
1513 db
= cupsArrayNew3((cups_array_func_t
)cups_compare_media_db
,
1515 (cups_acopy_func_t
)cups_copy_media_db
,
1516 (cups_afree_func_t
)cups_free_media_db
);
1518 if (flags
== CUPS_MEDIA_FLAGS_READY
)
1520 dinfo
->ready_db
= db
;
1522 media_col_db
= ippFindAttribute(dinfo
->ready_attrs
, "media-col-ready",
1523 IPP_TAG_BEGIN_COLLECTION
);
1524 media_attr
= ippFindAttribute(dinfo
->ready_attrs
, "media-ready",
1529 dinfo
->media_db
= db
;
1530 dinfo
->min_size
.width
= INT_MAX
;
1531 dinfo
->min_size
.length
= INT_MAX
;
1532 dinfo
->max_size
.width
= 0;
1533 dinfo
->max_size
.length
= 0;
1535 media_col_db
= ippFindAttribute(dinfo
->attrs
, "media-col-database",
1536 IPP_TAG_BEGIN_COLLECTION
);
1537 media_attr
= ippFindAttribute(dinfo
->attrs
, "media-supported",
1543 _ipp_value_t
*custom
= NULL
; /* Custom size range value */
1545 for (i
= media_col_db
->num_values
, val
= media_col_db
->values
;
1549 memset(&mdb
, 0, sizeof(mdb
));
1551 if ((media_attr
= ippFindAttribute(val
->collection
, "media-size",
1552 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1554 ipp_t
*media_size
= media_attr
->values
[0].collection
;
1555 /* media-size collection value */
1557 if ((x_dimension
= ippFindAttribute(media_size
, "x-dimension",
1558 IPP_TAG_INTEGER
)) != NULL
&&
1559 (y_dimension
= ippFindAttribute(media_size
, "y-dimension",
1560 IPP_TAG_INTEGER
)) != NULL
)
1566 mdb
.width
= x_dimension
->values
[0].integer
;
1567 mdb
.length
= y_dimension
->values
[0].integer
;
1569 else if ((x_dimension
= ippFindAttribute(media_size
, "x-dimension",
1570 IPP_TAG_INTEGER
)) != NULL
&&
1571 (y_dimension
= ippFindAttribute(media_size
, "y-dimension",
1572 IPP_TAG_RANGE
)) != NULL
)
1578 mdb
.width
= x_dimension
->values
[0].integer
;
1579 mdb
.length
= y_dimension
->values
[0].range
.upper
;
1581 else if (flags
!= CUPS_MEDIA_FLAGS_READY
&&
1582 (x_dimension
= ippFindAttribute(media_size
, "x-dimension",
1583 IPP_TAG_RANGE
)) != NULL
&&
1584 (y_dimension
= ippFindAttribute(media_size
, "y-dimension",
1585 IPP_TAG_RANGE
)) != NULL
)
1588 * Custom size range; save this as the custom size value with default
1589 * margins, then continue; we'll capture the real margins below...
1594 dinfo
->min_size
.width
= x_dimension
->values
[0].range
.lower
;
1595 dinfo
->min_size
.length
= y_dimension
->values
[0].range
.lower
;
1596 dinfo
->min_size
.left
=
1597 dinfo
->min_size
.right
= 635; /* Default 1/4" side margins */
1598 dinfo
->min_size
.top
=
1599 dinfo
->min_size
.bottom
= 1270; /* Default 1/2" top/bottom margins */
1601 dinfo
->max_size
.width
= x_dimension
->values
[0].range
.upper
;
1602 dinfo
->max_size
.length
= y_dimension
->values
[0].range
.upper
;
1603 dinfo
->max_size
.left
=
1604 dinfo
->max_size
.right
= 635; /* Default 1/4" side margins */
1605 dinfo
->max_size
.top
=
1606 dinfo
->max_size
.bottom
= 1270; /* Default 1/2" top/bottom margins */
1611 if ((media_attr
= ippFindAttribute(val
->collection
, "media-color",
1612 IPP_TAG_ZERO
)) != NULL
&&
1613 (media_attr
->value_tag
== IPP_TAG_NAME
||
1614 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
1615 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
1616 mdb
.color
= media_attr
->values
[0].string
.text
;
1618 if ((media_attr
= ippFindAttribute(val
->collection
, "media-info",
1619 IPP_TAG_TEXT
)) != NULL
)
1620 mdb
.info
= media_attr
->values
[0].string
.text
;
1622 if ((media_attr
= ippFindAttribute(val
->collection
, "media-key",
1623 IPP_TAG_ZERO
)) != NULL
&&
1624 (media_attr
->value_tag
== IPP_TAG_NAME
||
1625 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
1626 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
1627 mdb
.key
= media_attr
->values
[0].string
.text
;
1629 if ((media_attr
= ippFindAttribute(val
->collection
, "media-size-name",
1630 IPP_TAG_ZERO
)) != NULL
&&
1631 (media_attr
->value_tag
== IPP_TAG_NAME
||
1632 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
1633 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
1634 mdb
.size_name
= media_attr
->values
[0].string
.text
;
1636 if ((media_attr
= ippFindAttribute(val
->collection
, "media-source",
1637 IPP_TAG_ZERO
)) != NULL
&&
1638 (media_attr
->value_tag
== IPP_TAG_NAME
||
1639 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
1640 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
1641 mdb
.source
= media_attr
->values
[0].string
.text
;
1643 if ((media_attr
= ippFindAttribute(val
->collection
, "media-type",
1644 IPP_TAG_ZERO
)) != NULL
&&
1645 (media_attr
->value_tag
== IPP_TAG_NAME
||
1646 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
1647 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
1648 mdb
.type
= media_attr
->values
[0].string
.text
;
1650 if ((media_attr
= ippFindAttribute(val
->collection
, "media-bottom-margin",
1651 IPP_TAG_INTEGER
)) != NULL
)
1652 mdb
.bottom
= media_attr
->values
[0].integer
;
1654 if ((media_attr
= ippFindAttribute(val
->collection
, "media-left-margin",
1655 IPP_TAG_INTEGER
)) != NULL
)
1656 mdb
.left
= media_attr
->values
[0].integer
;
1658 if ((media_attr
= ippFindAttribute(val
->collection
, "media-right-margin",
1659 IPP_TAG_INTEGER
)) != NULL
)
1660 mdb
.right
= media_attr
->values
[0].integer
;
1662 if ((media_attr
= ippFindAttribute(val
->collection
, "media-top-margin",
1663 IPP_TAG_INTEGER
)) != NULL
)
1664 mdb
.top
= media_attr
->values
[0].integer
;
1666 cupsArrayAdd(db
, &mdb
);
1671 if ((media_attr
= ippFindAttribute(custom
->collection
,
1672 "media-bottom-margin",
1673 IPP_TAG_INTEGER
)) != NULL
)
1675 dinfo
->min_size
.top
=
1676 dinfo
->max_size
.top
= media_attr
->values
[0].integer
;
1679 if ((media_attr
= ippFindAttribute(custom
->collection
,
1680 "media-left-margin",
1681 IPP_TAG_INTEGER
)) != NULL
)
1683 dinfo
->min_size
.left
=
1684 dinfo
->max_size
.left
= media_attr
->values
[0].integer
;
1687 if ((media_attr
= ippFindAttribute(custom
->collection
,
1688 "media-right-margin",
1689 IPP_TAG_INTEGER
)) != NULL
)
1691 dinfo
->min_size
.right
=
1692 dinfo
->max_size
.right
= media_attr
->values
[0].integer
;
1695 if ((media_attr
= ippFindAttribute(custom
->collection
,
1697 IPP_TAG_INTEGER
)) != NULL
)
1699 dinfo
->min_size
.top
=
1700 dinfo
->max_size
.top
= media_attr
->values
[0].integer
;
1704 else if (media_attr
&&
1705 (media_attr
->value_tag
== IPP_TAG_NAME
||
1706 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
1707 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
1709 memset(&mdb
, 0, sizeof(mdb
));
1712 mdb
.right
= 635; /* Default 1/4" side margins */
1714 mdb
.bottom
= 1270; /* Default 1/2" top/bottom margins */
1716 for (i
= media_attr
->num_values
, val
= media_attr
->values
;
1720 if ((pwg
= pwgMediaForPWG(val
->string
.text
)) == NULL
)
1721 if ((pwg
= pwgMediaForLegacy(val
->string
.text
)) == NULL
)
1723 DEBUG_printf(("3cups_create_media_db: Ignoring unknown size '%s'.",
1728 mdb
.width
= pwg
->width
;
1729 mdb
.length
= pwg
->length
;
1731 if (flags
!= CUPS_MEDIA_FLAGS_READY
&&
1732 !strncmp(val
->string
.text
, "custom_min_", 11))
1734 mdb
.size_name
= NULL
;
1735 dinfo
->min_size
= mdb
;
1737 else if (flags
!= CUPS_MEDIA_FLAGS_READY
&&
1738 !strncmp(val
->string
.text
, "custom_max_", 11))
1740 mdb
.size_name
= NULL
;
1741 dinfo
->max_size
= mdb
;
1745 mdb
.size_name
= val
->string
.text
;
1747 cupsArrayAdd(db
, &mdb
);
1755 * 'cups_free_media_cb()' - Free a media entry.
1760 _cups_media_db_t
*mdb
) /* I - Media entry to free */
1763 _cupsStrFree(mdb
->color
);
1765 _cupsStrFree(mdb
->key
);
1767 _cupsStrFree(mdb
->info
);
1769 _cupsStrFree(mdb
->size_name
);
1771 _cupsStrFree(mdb
->source
);
1773 _cupsStrFree(mdb
->type
);
1780 * 'cups_get_media_db()' - Lookup the media entry for a given size.
1783 static int /* O - 1 on match, 0 on failure */
1784 cups_get_media_db(http_t
*http
, /* I - Connection to destination */
1785 cups_dinfo_t
*dinfo
, /* I - Destination information */
1786 pwg_media_t
*pwg
, /* I - PWG media info */
1787 unsigned flags
, /* I - Media matching flags */
1788 cups_size_t
*size
) /* O - Media size/margin/name info */
1790 cups_array_t
*db
; /* Which media database to query */
1791 _cups_media_db_t
*mdb
, /* Current media database entry */
1792 *best
= NULL
, /* Best matching entry */
1793 key
; /* Search key */
1797 * Create the media database as needed...
1800 if (flags
& CUPS_MEDIA_FLAGS_READY
)
1802 cups_update_ready(http
, dinfo
);
1803 db
= dinfo
->ready_db
;
1807 if (!dinfo
->media_db
)
1808 cups_create_media_db(dinfo
, CUPS_MEDIA_FLAGS_DEFAULT
);
1810 db
= dinfo
->media_db
;
1817 memset(&key
, 0, sizeof(key
));
1818 key
.width
= pwg
->width
;
1819 key
.length
= pwg
->length
;
1821 if ((mdb
= cupsArrayFind(db
, &key
)) != NULL
)
1824 * Found an exact match, let's figure out the best margins for the flags
1830 if (flags
& CUPS_MEDIA_FLAGS_BORDERLESS
)
1833 * Look for the smallest margins...
1836 if (best
->left
!= 0 || best
->right
!= 0 || best
->top
!= 0 || best
->bottom
!= 0)
1838 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
1839 mdb
&& !cups_compare_media_db(mdb
, &key
);
1840 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1842 if (mdb
->left
<= best
->left
&& mdb
->right
<= best
->right
&&
1843 mdb
->top
<= best
->top
&& mdb
->bottom
<= best
->bottom
)
1846 if (mdb
->left
== 0 && mdb
->right
== 0 && mdb
->bottom
== 0 &&
1854 * If we need an exact match, return no-match if the size is not
1858 if ((flags
& CUPS_MEDIA_FLAGS_EXACT
) &&
1859 (best
->left
|| best
->right
|| best
->top
|| best
->bottom
))
1862 else if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
1865 * Look for the largest margins...
1868 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
1869 mdb
&& !cups_compare_media_db(mdb
, &key
);
1870 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1872 if (mdb
->left
>= best
->left
&& mdb
->right
>= best
->right
&&
1873 mdb
->top
>= best
->top
&& mdb
->bottom
>= best
->bottom
&&
1874 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
1881 * Look for the smallest non-zero margins...
1884 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
1885 mdb
&& !cups_compare_media_db(mdb
, &key
);
1886 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1888 if (((mdb
->left
> 0 && mdb
->left
<= best
->left
) || best
->left
== 0) &&
1889 ((mdb
->right
> 0 && mdb
->right
<= best
->right
) || best
->right
== 0) &&
1890 ((mdb
->top
> 0 && mdb
->top
<= best
->top
) || best
->top
== 0) &&
1891 ((mdb
->bottom
> 0 && mdb
->bottom
<= best
->bottom
) || best
->bottom
== 0) &&
1892 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
1897 else if (flags
& CUPS_MEDIA_FLAGS_EXACT
)
1900 * See if we can do this as a custom size...
1903 if (pwg
->width
< dinfo
->min_size
.width
||
1904 pwg
->width
> dinfo
->max_size
.width
||
1905 pwg
->length
< dinfo
->min_size
.length
||
1906 pwg
->length
> dinfo
->max_size
.length
)
1907 return (0); /* Out of range */
1909 if ((flags
& CUPS_MEDIA_FLAGS_BORDERLESS
) &&
1910 (dinfo
->min_size
.left
> 0 || dinfo
->min_size
.right
> 0 ||
1911 dinfo
->min_size
.top
> 0 || dinfo
->min_size
.bottom
> 0))
1912 return (0); /* Not borderless */
1914 key
.size_name
= (char *)pwg
->pwg
;
1915 key
.bottom
= dinfo
->min_size
.bottom
;
1916 key
.left
= dinfo
->min_size
.left
;
1917 key
.right
= dinfo
->min_size
.right
;
1918 key
.top
= dinfo
->min_size
.top
;
1922 else if (pwg
->width
>= dinfo
->min_size
.width
&&
1923 pwg
->width
<= dinfo
->max_size
.width
&&
1924 pwg
->length
>= dinfo
->min_size
.length
&&
1925 pwg
->length
<= dinfo
->max_size
.length
)
1928 * Map to custom size...
1931 key
.size_name
= (char *)pwg
->pwg
;
1932 key
.bottom
= dinfo
->min_size
.bottom
;
1933 key
.left
= dinfo
->min_size
.left
;
1934 key
.right
= dinfo
->min_size
.right
;
1935 key
.top
= dinfo
->min_size
.top
;
1942 * Find a close size...
1945 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
);
1947 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1948 if (cups_is_close_media_db(mdb
, &key
))
1956 if (flags
& CUPS_MEDIA_FLAGS_BORDERLESS
)
1959 * Look for the smallest margins...
1962 if (best
->left
!= 0 || best
->right
!= 0 || best
->top
!= 0 ||
1965 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
1966 mdb
&& cups_is_close_media_db(mdb
, &key
);
1967 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1969 if (mdb
->left
<= best
->left
&& mdb
->right
<= best
->right
&&
1970 mdb
->top
<= best
->top
&& mdb
->bottom
<= best
->bottom
&&
1971 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
1974 if (mdb
->left
== 0 && mdb
->right
== 0 && mdb
->bottom
== 0 &&
1981 else if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
1984 * Look for the largest margins...
1987 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
1988 mdb
&& cups_is_close_media_db(mdb
, &key
);
1989 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1991 if (mdb
->left
>= best
->left
&& mdb
->right
>= best
->right
&&
1992 mdb
->top
>= best
->top
&& mdb
->bottom
>= best
->bottom
&&
1993 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
2000 * Look for the smallest non-zero margins...
2003 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
2004 mdb
&& cups_is_close_media_db(mdb
, &key
);
2005 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2007 if (((mdb
->left
> 0 && mdb
->left
<= best
->left
) || best
->left
== 0) &&
2008 ((mdb
->right
> 0 && mdb
->right
<= best
->right
) ||
2009 best
->right
== 0) &&
2010 ((mdb
->top
> 0 && mdb
->top
<= best
->top
) || best
->top
== 0) &&
2011 ((mdb
->bottom
> 0 && mdb
->bottom
<= best
->bottom
) ||
2012 best
->bottom
== 0) &&
2013 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
2022 * Return the matching size...
2025 if (best
->size_name
)
2026 strlcpy(size
->media
, best
->size_name
, sizeof(size
->media
));
2028 strlcpy(size
->media
, best
->key
, sizeof(size
->media
));
2030 strlcpy(size
->media
, pwg
->pwg
, sizeof(size
->media
));
2032 size
->width
= best
->width
;
2033 size
->length
= best
->length
;
2034 size
->bottom
= best
->bottom
;
2035 size
->left
= best
->left
;
2036 size
->right
= best
->right
;
2037 size
->top
= best
->top
;
2047 * 'cups_is_close_media_db()' - Compare two media entries to see if they are
2048 * close to the same size.
2050 * Currently we use 5 points (from PostScript) as the matching range...
2053 static int /* O - 1 if the sizes are close */
2054 cups_is_close_media_db(
2055 _cups_media_db_t
*a
, /* I - First media entries */
2056 _cups_media_db_t
*b
) /* I - Second media entries */
2058 int dwidth
, /* Difference in width */
2059 dlength
; /* Difference in length */
2062 dwidth
= a
->width
- b
->width
;
2063 dlength
= a
->length
- b
->length
;
2065 return (dwidth
>= -176 && dwidth
<= 176 &&
2066 dlength
>= -176 && dlength
<= 176);
2071 * 'cups_test_constraints()' - Test constraints.
2073 * TODO: STR #4096 - Need to properly support media-col contraints...
2076 static cups_array_t
* /* O - Active constraints */
2077 cups_test_constraints(
2078 cups_dinfo_t
*dinfo
, /* I - Destination information */
2079 const char *new_option
, /* I - Newly selected option */
2080 const char *new_value
, /* I - Newly selected value */
2081 int num_options
, /* I - Number of options */
2082 cups_option_t
*options
, /* I - Options */
2083 int *num_conflicts
, /* O - Number of conflicting options */
2084 cups_option_t
**conflicts
) /* O - Conflicting options */
2086 int i
, /* Looping var */
2087 match
; /* Value matches? */
2088 int num_matching
; /* Number of matching options */
2089 cups_option_t
*matching
; /* Matching options */
2090 _cups_dconstres_t
*c
; /* Current constraint */
2091 cups_array_t
*active
= NULL
; /* Active constraints */
2092 ipp_attribute_t
*attr
; /* Current attribute */
2093 _ipp_value_t
*attrval
; /* Current attribute value */
2094 const char *value
; /* Current value */
2095 char temp
[1024]; /* Temporary string */
2096 int int_value
; /* Integer value */
2097 int xres_value
, /* Horizontal resolution */
2098 yres_value
; /* Vertical resolution */
2099 ipp_res_t units_value
; /* Resolution units */
2102 for (c
= (_cups_dconstres_t
*)cupsArrayFirst(dinfo
->constraints
);
2104 c
= (_cups_dconstres_t
*)cupsArrayNext(dinfo
->constraints
))
2109 for (attr
= ippFirstAttribute(c
->collection
);
2111 attr
= ippNextAttribute(c
->collection
))
2113 if (attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
)
2114 break; /* TODO: STR #4096 */
2117 * Get the value for the current attribute in the constraint...
2120 if (new_option
&& new_value
&& !strcmp(attr
->name
, new_option
))
2122 else if ((value
= cupsGetOption(attr
->name
, num_options
,
2124 value
= cupsGetOption(attr
->name
, dinfo
->num_defaults
, dinfo
->defaults
);
2129 * Not set so this constraint does not apply...
2137 switch (attr
->value_tag
)
2139 case IPP_TAG_INTEGER
:
2141 int_value
= atoi(value
);
2143 for (i
= attr
->num_values
, attrval
= attr
->values
;
2147 if (attrval
->integer
== int_value
)
2155 case IPP_TAG_BOOLEAN
:
2156 int_value
= !strcmp(value
, "true");
2158 for (i
= attr
->num_values
, attrval
= attr
->values
;
2162 if (attrval
->boolean
== int_value
)
2170 case IPP_TAG_RANGE
:
2171 int_value
= atoi(value
);
2173 for (i
= attr
->num_values
, attrval
= attr
->values
;
2177 if (int_value
>= attrval
->range
.lower
&&
2178 int_value
<= attrval
->range
.upper
)
2186 case IPP_TAG_RESOLUTION
:
2187 if (sscanf(value
, "%dx%d%15s", &xres_value
, &yres_value
, temp
) != 3)
2189 if (sscanf(value
, "%d%15s", &xres_value
, temp
) != 2)
2192 yres_value
= xres_value
;
2195 if (!strcmp(temp
, "dpi"))
2196 units_value
= IPP_RES_PER_INCH
;
2197 else if (!strcmp(temp
, "dpc") || !strcmp(temp
, "dpcm"))
2198 units_value
= IPP_RES_PER_CM
;
2202 for (i
= attr
->num_values
, attrval
= attr
->values
;
2206 if (attrval
->resolution
.xres
== xres_value
&&
2207 attrval
->resolution
.yres
== yres_value
&&
2208 attrval
->resolution
.units
== units_value
)
2218 case IPP_TAG_KEYWORD
:
2219 case IPP_TAG_CHARSET
:
2221 case IPP_TAG_URISCHEME
:
2222 case IPP_TAG_MIMETYPE
:
2223 case IPP_TAG_LANGUAGE
:
2224 case IPP_TAG_TEXTLANG
:
2225 case IPP_TAG_NAMELANG
:
2226 for (i
= attr
->num_values
, attrval
= attr
->values
;
2230 if (!strcmp(attrval
->string
.text
, value
))
2245 num_matching
= cupsAddOption(attr
->name
, value
, num_matching
, &matching
);
2251 active
= cupsArrayNew(NULL
, NULL
);
2253 cupsArrayAdd(active
, c
);
2255 if (num_conflicts
&& conflicts
)
2257 cups_option_t
*moption
; /* Matching option */
2259 for (i
= num_matching
, moption
= matching
; i
> 0; i
--, moption
++)
2260 *num_conflicts
= cupsAddOption(moption
->name
, moption
->value
,
2261 *num_conflicts
, conflicts
);
2265 cupsFreeOptions(num_matching
, matching
);
2273 * 'cups_update_ready()' - Update xxx-ready attributes for the printer.
2277 cups_update_ready(http_t
*http
, /* I - Connection to destination */
2278 cups_dinfo_t
*dinfo
) /* I - Destination information */
2280 ipp_t
*request
; /* Get-Printer-Attributes request */
2281 static const char * const pattrs
[] = /* Printer attributes we want */
2283 "finishings-col-ready",
2285 "job-finishings-col-ready",
2286 "job-finishings-ready",
2293 * Don't update more than once every 30 seconds...
2296 if ((time(NULL
) - dinfo
->ready_time
) < _CUPS_MEDIA_READY_TTL
)
2300 * Free any previous results...
2303 if (dinfo
->cached_flags
& CUPS_MEDIA_FLAGS_READY
)
2305 cupsArrayDelete(dinfo
->cached_db
);
2306 dinfo
->cached_db
= NULL
;
2307 dinfo
->cached_flags
= CUPS_MEDIA_FLAGS_DEFAULT
;
2310 ippDelete(dinfo
->ready_attrs
);
2311 dinfo
->ready_attrs
= NULL
;
2313 cupsArrayDelete(dinfo
->ready_db
);
2314 dinfo
->ready_db
= NULL
;
2317 * Query the xxx-ready values...
2320 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
2321 ippSetVersion(request
, dinfo
->version
/ 10, dinfo
->version
% 10);
2323 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
2325 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
2327 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "requested-attributes", (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
2329 dinfo
->ready_attrs
= cupsDoRequest(http
, request
, dinfo
->resource
);
2332 * Update the ready media database...
2335 cups_create_media_db(dinfo
, CUPS_MEDIA_FLAGS_READY
);
2338 * Update last lookup time and return...
2341 dinfo
->ready_time
= time(NULL
);