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@.
837 * @since CUPS 1.6/macOS 10.8@
841 cupsFreeDestInfo(cups_dinfo_t
*dinfo
) /* I - Destination information */
844 * Range check input...
851 * Free memory and return...
854 _cupsStrFree(dinfo
->resource
);
856 cupsArrayDelete(dinfo
->constraints
);
857 cupsArrayDelete(dinfo
->resolvers
);
859 cupsArrayDelete(dinfo
->localizations
);
861 cupsArrayDelete(dinfo
->media_db
);
863 cupsArrayDelete(dinfo
->cached_db
);
865 ippDelete(dinfo
->ready_attrs
);
866 cupsArrayDelete(dinfo
->ready_db
);
868 ippDelete(dinfo
->attrs
);
875 * 'cupsGetDestMediaByIndex()' - Get a media name, dimension, and margins for a
878 * The @code flags@ parameter determines which set of media are indexed. For
879 * example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will get the Nth
880 * borderless size supported by the printer.
882 * @since CUPS 1.7/macOS 10.9@
885 int /* O - 1 on success, 0 on failure */
886 cupsGetDestMediaByIndex(
887 http_t
*http
, /* I - Connection to destination */
888 cups_dest_t
*dest
, /* I - Destination */
889 cups_dinfo_t
*dinfo
, /* I - Destination information */
890 int n
, /* I - Media size number (0-based) */
891 unsigned flags
, /* I - Media flags */
892 cups_size_t
*size
) /* O - Media size information */
894 _cups_media_db_t
*nsize
; /* Size for N */
895 pwg_media_t
*pwg
; /* PWG media name for size */
899 * Get the default connection as needed...
903 http
= _cupsConnect();
906 * Range check input...
910 memset(size
, 0, sizeof(cups_size_t
));
912 if (!http
|| !dest
|| !dinfo
|| n
< 0 || !size
)
914 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
919 * Load media list as needed...
922 if (flags
& CUPS_MEDIA_FLAGS_READY
)
923 cups_update_ready(http
, dinfo
);
925 if (!dinfo
->cached_db
|| dinfo
->cached_flags
!= flags
)
926 cups_create_cached(http
, dinfo
, flags
);
929 * Copy the size over and return...
932 if ((nsize
= (_cups_media_db_t
*)cupsArrayIndex(dinfo
->cached_db
, n
)) == NULL
)
934 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
938 if (nsize
->size_name
)
939 strlcpy(size
->media
, nsize
->size_name
, sizeof(size
->media
));
941 strlcpy(size
->media
, nsize
->key
, sizeof(size
->media
));
942 else if ((pwg
= pwgMediaForSize(nsize
->width
, nsize
->length
)) != NULL
)
943 strlcpy(size
->media
, pwg
->pwg
, sizeof(size
->media
));
946 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
950 size
->width
= nsize
->width
;
951 size
->length
= nsize
->length
;
952 size
->bottom
= nsize
->bottom
;
953 size
->left
= nsize
->left
;
954 size
->right
= nsize
->right
;
955 size
->top
= nsize
->top
;
962 * 'cupsGetDestMediaByName()' - Get media names, dimensions, and margins.
964 * The "media" string is a PWG media name. "Flags" provides some matching
965 * guidance (multiple flags can be combined):
967 * CUPS_MEDIA_FLAGS_DEFAULT = find the closest size supported by the printer,
968 * CUPS_MEDIA_FLAGS_BORDERLESS = find a borderless size,
969 * CUPS_MEDIA_FLAGS_DUPLEX = find a size compatible with 2-sided printing,
970 * CUPS_MEDIA_FLAGS_EXACT = find an exact match for the size, and
971 * CUPS_MEDIA_FLAGS_READY = if the printer supports media sensing, find the
972 * size amongst the "ready" media.
974 * The matching result (if any) is returned in the "cups_size_t" structure.
976 * Returns 1 when there is a match and 0 if there is not a match.
978 * @since CUPS 1.6/macOS 10.8@
981 int /* O - 1 on match, 0 on failure */
982 cupsGetDestMediaByName(
983 http_t
*http
, /* I - Connection to destination */
984 cups_dest_t
*dest
, /* I - Destination */
985 cups_dinfo_t
*dinfo
, /* I - Destination information */
986 const char *media
, /* I - Media name */
987 unsigned flags
, /* I - Media matching flags */
988 cups_size_t
*size
) /* O - Media size information */
990 pwg_media_t
*pwg
; /* PWG media info */
994 * Get the default connection as needed...
998 http
= _cupsConnect();
1001 * Range check input...
1005 memset(size
, 0, sizeof(cups_size_t
));
1007 if (!http
|| !dest
|| !dinfo
|| !media
|| !size
)
1009 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1014 * Lookup the media size name...
1017 if ((pwg
= pwgMediaForPWG(media
)) == NULL
)
1018 if ((pwg
= pwgMediaForLegacy(media
)) == NULL
)
1020 DEBUG_printf(("1cupsGetDestMediaByName: Unknown size '%s'.", media
));
1021 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown media size name."), 1);
1026 * Lookup the size...
1029 return (cups_get_media_db(http
, dinfo
, pwg
, flags
, size
));
1034 * 'cupsGetDestMediaBySize()' - Get media names, dimensions, and margins.
1036 * "Width" and "length" are the dimensions in hundredths of millimeters.
1037 * "Flags" provides some matching guidance (multiple flags can be combined):
1039 * CUPS_MEDIA_FLAGS_DEFAULT = find the closest size supported by the printer,
1040 * CUPS_MEDIA_FLAGS_BORDERLESS = find a borderless size,
1041 * CUPS_MEDIA_FLAGS_DUPLEX = find a size compatible with 2-sided printing,
1042 * CUPS_MEDIA_FLAGS_EXACT = find an exact match for the size, and
1043 * CUPS_MEDIA_FLAGS_READY = if the printer supports media sensing, find the
1044 * size amongst the "ready" media.
1046 * The matching result (if any) is returned in the "cups_size_t" structure.
1048 * Returns 1 when there is a match and 0 if there is not a match.
1050 * @since CUPS 1.6/macOS 10.8@
1053 int /* O - 1 on match, 0 on failure */
1054 cupsGetDestMediaBySize(
1055 http_t
*http
, /* I - Connection to destination */
1056 cups_dest_t
*dest
, /* I - Destination */
1057 cups_dinfo_t
*dinfo
, /* I - Destination information */
1058 int width
, /* I - Media width in hundredths of
1060 int length
, /* I - Media length in hundredths of
1062 unsigned flags
, /* I - Media matching flags */
1063 cups_size_t
*size
) /* O - Media size information */
1065 pwg_media_t
*pwg
; /* PWG media info */
1069 * Get the default connection as needed...
1073 http
= _cupsConnect();
1076 * Range check input...
1080 memset(size
, 0, sizeof(cups_size_t
));
1082 if (!http
|| !dest
|| !dinfo
|| width
<= 0 || length
<= 0 || !size
)
1084 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1089 * Lookup the media size name...
1092 if ((pwg
= pwgMediaForSize(width
, length
)) == NULL
)
1094 DEBUG_printf(("1cupsGetDestMediaBySize: Invalid size %dx%d.", width
,
1096 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Invalid media size."), 1);
1101 * Lookup the size...
1104 return (cups_get_media_db(http
, dinfo
, pwg
, flags
, size
));
1109 * 'cupsGetDestMediaCount()' - Get the number of sizes supported by a
1112 * The @code flags@ parameter determines the set of media sizes that are
1113 * counted. For example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will return
1114 * the number of borderless sizes.
1116 * @since CUPS 1.7/macOS 10.9@
1119 int /* O - Number of sizes */
1120 cupsGetDestMediaCount(
1121 http_t
*http
, /* I - Connection to destination */
1122 cups_dest_t
*dest
, /* I - Destination */
1123 cups_dinfo_t
*dinfo
, /* I - Destination information */
1124 unsigned flags
) /* I - Media flags */
1127 * Get the default connection as needed...
1131 http
= _cupsConnect();
1134 * Range check input...
1137 if (!http
|| !dest
|| !dinfo
)
1139 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1144 * Load media list as needed...
1147 if (flags
& CUPS_MEDIA_FLAGS_READY
)
1148 cups_update_ready(http
, dinfo
);
1150 if (!dinfo
->cached_db
|| dinfo
->cached_flags
!= flags
)
1151 cups_create_cached(http
, dinfo
, flags
);
1153 return (cupsArrayCount(dinfo
->cached_db
));
1158 * 'cupsGetDestMediaDefault()' - Get the default size for a destination.
1160 * The @code flags@ parameter determines which default size is returned. For
1161 * example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will return the default
1162 * borderless size, typically US Letter or A4, but sometimes 4x6 photo media.
1164 * @since CUPS 1.7/macOS 10.9@
1167 int /* O - 1 on success, 0 on failure */
1168 cupsGetDestMediaDefault(
1169 http_t
*http
, /* I - Connection to destination */
1170 cups_dest_t
*dest
, /* I - Destination */
1171 cups_dinfo_t
*dinfo
, /* I - Destination information */
1172 unsigned flags
, /* I - Media flags */
1173 cups_size_t
*size
) /* O - Media size information */
1175 const char *media
; /* Default media size */
1179 * Get the default connection as needed...
1183 http
= _cupsConnect();
1186 * Range check input...
1190 memset(size
, 0, sizeof(cups_size_t
));
1192 if (!http
|| !dest
|| !dinfo
|| !size
)
1194 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1199 * Get the default media size, if any...
1202 if ((media
= cupsGetOption("media", dest
->num_options
,
1203 dest
->options
)) == NULL
)
1204 media
= "na_letter_8.5x11in";
1206 if (cupsGetDestMediaByName(http
, dest
, dinfo
, media
, flags
, size
))
1209 if (strcmp(media
, "na_letter_8.5x11in") &&
1210 cupsGetDestMediaByName(http
, dest
, dinfo
, "iso_a4_210x297mm", flags
,
1214 if (strcmp(media
, "iso_a4_210x297mm") &&
1215 cupsGetDestMediaByName(http
, dest
, dinfo
, "na_letter_8.5x11in", flags
,
1219 if ((flags
& CUPS_MEDIA_FLAGS_BORDERLESS
) &&
1220 cupsGetDestMediaByName(http
, dest
, dinfo
, "na_index_4x6in", flags
, size
))
1224 * Fall back to the first matching media size...
1227 return (cupsGetDestMediaByIndex(http
, dest
, dinfo
, 0, flags
, size
));
1232 * 'cups_add_dconstres()' - Add a constraint or resolver to an array.
1237 cups_array_t
*a
, /* I - Array */
1238 ipp_t
*collection
) /* I - Collection value */
1240 ipp_attribute_t
*attr
; /* Attribute */
1241 _cups_dconstres_t
*temp
; /* Current constraint/resolver */
1244 if ((attr
= ippFindAttribute(collection
, "resolver-name",
1245 IPP_TAG_NAME
)) == NULL
)
1248 if ((temp
= calloc(1, sizeof(_cups_dconstres_t
))) == NULL
)
1251 temp
->name
= attr
->values
[0].string
.text
;
1252 temp
->collection
= collection
;
1254 cupsArrayAdd(a
, temp
);
1259 * 'cups_compare_dconstres()' - Compare to resolver entries.
1262 static int /* O - Result of comparison */
1263 cups_compare_dconstres(
1264 _cups_dconstres_t
*a
, /* I - First resolver */
1265 _cups_dconstres_t
*b
) /* I - Second resolver */
1267 return (strcmp(a
->name
, b
->name
));
1272 * 'cups_compare_media_db()' - Compare two media entries.
1275 static int /* O - Result of comparison */
1276 cups_compare_media_db(
1277 _cups_media_db_t
*a
, /* I - First media entries */
1278 _cups_media_db_t
*b
) /* I - Second media entries */
1280 int result
; /* Result of comparison */
1283 if ((result
= a
->width
- b
->width
) == 0)
1284 result
= a
->length
- b
->length
;
1291 * 'cups_copy_media_db()' - Copy a media entry.
1294 static _cups_media_db_t
* /* O - New media entry */
1296 _cups_media_db_t
*mdb
) /* I - Media entry to copy */
1298 _cups_media_db_t
*temp
; /* New media entry */
1301 if ((temp
= calloc(1, sizeof(_cups_media_db_t
))) == NULL
)
1305 temp
->color
= _cupsStrAlloc(mdb
->color
);
1307 temp
->key
= _cupsStrAlloc(mdb
->key
);
1309 temp
->info
= _cupsStrAlloc(mdb
->info
);
1311 temp
->size_name
= _cupsStrAlloc(mdb
->size_name
);
1313 temp
->source
= _cupsStrAlloc(mdb
->source
);
1315 temp
->type
= _cupsStrAlloc(mdb
->type
);
1317 temp
->width
= mdb
->width
;
1318 temp
->length
= mdb
->length
;
1319 temp
->bottom
= mdb
->bottom
;
1320 temp
->left
= mdb
->left
;
1321 temp
->right
= mdb
->right
;
1322 temp
->top
= mdb
->top
;
1329 * 'cups_create_cached()' - Create the media selection cache.
1333 cups_create_cached(http_t
*http
, /* I - Connection to destination */
1334 cups_dinfo_t
*dinfo
, /* I - Destination information */
1335 unsigned flags
) /* I - Media selection flags */
1337 cups_array_t
*db
; /* Media database array to use */
1338 _cups_media_db_t
*mdb
, /* Media database entry */
1339 *first
; /* First entry this size */
1342 DEBUG_printf(("3cups_create_cached(http=%p, dinfo=%p, flags=%u)", (void *)http
, (void *)dinfo
, flags
));
1344 if (dinfo
->cached_db
)
1345 cupsArrayDelete(dinfo
->cached_db
);
1347 dinfo
->cached_db
= cupsArrayNew(NULL
, NULL
);
1348 dinfo
->cached_flags
= flags
;
1350 if (flags
& CUPS_MEDIA_FLAGS_READY
)
1352 DEBUG_puts("4cups_create_cached: ready media");
1354 cups_update_ready(http
, dinfo
);
1355 db
= dinfo
->ready_db
;
1359 DEBUG_puts("4cups_create_cached: supported media");
1361 if (!dinfo
->media_db
)
1362 cups_create_media_db(dinfo
, CUPS_MEDIA_FLAGS_DEFAULT
);
1364 db
= dinfo
->media_db
;
1367 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
), first
= mdb
;
1369 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1371 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
));
1373 if (flags
& CUPS_MEDIA_FLAGS_BORDERLESS
)
1375 if (!mdb
->left
&& !mdb
->right
&& !mdb
->top
&& !mdb
->bottom
)
1377 DEBUG_printf(("4cups_create_cached: add %p", (void *)mdb
));
1378 cupsArrayAdd(dinfo
->cached_db
, mdb
);
1381 else if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
1383 if (first
->width
!= mdb
->width
|| first
->length
!= mdb
->length
)
1385 DEBUG_printf(("4cups_create_cached: add %p", (void *)first
));
1386 cupsArrayAdd(dinfo
->cached_db
, first
);
1389 else if (mdb
->left
>= first
->left
&& mdb
->right
>= first
->right
&& mdb
->top
>= first
->top
&& mdb
->bottom
>= first
->bottom
&&
1390 (mdb
->left
!= first
->left
|| mdb
->right
!= first
->right
|| mdb
->top
!= first
->top
|| mdb
->bottom
!= first
->bottom
))
1395 DEBUG_printf(("4cups_create_cached: add %p", (void *)mdb
));
1396 cupsArrayAdd(dinfo
->cached_db
, mdb
);
1400 if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
1402 DEBUG_printf(("4cups_create_cached: add %p", (void *)first
));
1403 cupsArrayAdd(dinfo
->cached_db
, first
);
1409 * 'cups_create_constraints()' - Create the constraints and resolvers arrays.
1413 cups_create_constraints(
1414 cups_dinfo_t
*dinfo
) /* I - Destination information */
1416 int i
; /* Looping var */
1417 ipp_attribute_t
*attr
; /* Attribute */
1418 _ipp_value_t
*val
; /* Current value */
1421 dinfo
->constraints
= cupsArrayNew3(NULL
, NULL
, NULL
, 0, NULL
,
1422 (cups_afree_func_t
)free
);
1423 dinfo
->resolvers
= cupsArrayNew3((cups_array_func_t
)cups_compare_dconstres
,
1424 NULL
, NULL
, 0, NULL
,
1425 (cups_afree_func_t
)free
);
1427 if ((attr
= ippFindAttribute(dinfo
->attrs
, "job-constraints-supported",
1428 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1430 for (i
= attr
->num_values
, val
= attr
->values
; i
> 0; i
--, val
++)
1431 cups_add_dconstres(dinfo
->constraints
, val
->collection
);
1434 if ((attr
= ippFindAttribute(dinfo
->attrs
, "job-resolvers-supported",
1435 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1437 for (i
= attr
->num_values
, val
= attr
->values
; i
> 0; i
--, val
++)
1438 cups_add_dconstres(dinfo
->resolvers
, val
->collection
);
1444 * 'cups_create_defaults()' - Create the -default option array.
1446 * TODO: Need to support collection defaults...
1450 cups_create_defaults(
1451 cups_dinfo_t
*dinfo
) /* I - Destination information */
1453 ipp_attribute_t
*attr
; /* Current attribute */
1454 char name
[IPP_MAX_NAME
+ 1],
1456 *nameptr
, /* Pointer into current name */
1457 value
[2048]; /* Current value */
1461 * Iterate through the printer attributes looking for xxx-default and adding
1462 * xxx=value to the defaults option array.
1465 for (attr
= ippFirstAttribute(dinfo
->attrs
);
1467 attr
= ippNextAttribute(dinfo
->attrs
))
1469 if (!attr
->name
|| attr
->group_tag
!= IPP_TAG_PRINTER
)
1472 if (attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
)
1473 continue; /* TODO: STR #4096 */
1475 if ((nameptr
= attr
->name
+ strlen(attr
->name
) - 8) <= attr
->name
||
1476 strcmp(nameptr
, "-default"))
1479 strlcpy(name
, attr
->name
, sizeof(name
));
1480 if ((nameptr
= name
+ strlen(name
) - 8) <= name
||
1481 strcmp(nameptr
, "-default"))
1486 if (ippAttributeString(attr
, value
, sizeof(value
)) >= sizeof(value
))
1489 dinfo
->num_defaults
= cupsAddOption(name
, value
, dinfo
->num_defaults
,
1496 * 'cups_create_media_db()' - Create the media database.
1500 cups_create_media_db(
1501 cups_dinfo_t
*dinfo
, /* I - Destination information */
1502 unsigned flags
) /* I - Media flags */
1504 int i
; /* Looping var */
1505 _ipp_value_t
*val
; /* Current value */
1506 ipp_attribute_t
*media_col_db
, /* media-col-database */
1507 *media_attr
, /* media-xxx */
1508 *x_dimension
, /* x-dimension */
1509 *y_dimension
; /* y-dimension */
1510 pwg_media_t
*pwg
; /* PWG media info */
1511 cups_array_t
*db
; /* New media database array */
1512 _cups_media_db_t mdb
; /* Media entry */
1515 db
= cupsArrayNew3((cups_array_func_t
)cups_compare_media_db
,
1517 (cups_acopy_func_t
)cups_copy_media_db
,
1518 (cups_afree_func_t
)cups_free_media_db
);
1520 if (flags
== CUPS_MEDIA_FLAGS_READY
)
1522 dinfo
->ready_db
= db
;
1524 media_col_db
= ippFindAttribute(dinfo
->ready_attrs
, "media-col-ready",
1525 IPP_TAG_BEGIN_COLLECTION
);
1526 media_attr
= ippFindAttribute(dinfo
->ready_attrs
, "media-ready",
1531 dinfo
->media_db
= db
;
1532 dinfo
->min_size
.width
= INT_MAX
;
1533 dinfo
->min_size
.length
= INT_MAX
;
1534 dinfo
->max_size
.width
= 0;
1535 dinfo
->max_size
.length
= 0;
1537 media_col_db
= ippFindAttribute(dinfo
->attrs
, "media-col-database",
1538 IPP_TAG_BEGIN_COLLECTION
);
1539 media_attr
= ippFindAttribute(dinfo
->attrs
, "media-supported",
1545 _ipp_value_t
*custom
= NULL
; /* Custom size range value */
1547 for (i
= media_col_db
->num_values
, val
= media_col_db
->values
;
1551 memset(&mdb
, 0, sizeof(mdb
));
1553 if ((media_attr
= ippFindAttribute(val
->collection
, "media-size",
1554 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1556 ipp_t
*media_size
= media_attr
->values
[0].collection
;
1557 /* media-size collection value */
1559 if ((x_dimension
= ippFindAttribute(media_size
, "x-dimension",
1560 IPP_TAG_INTEGER
)) != NULL
&&
1561 (y_dimension
= ippFindAttribute(media_size
, "y-dimension",
1562 IPP_TAG_INTEGER
)) != NULL
)
1568 mdb
.width
= x_dimension
->values
[0].integer
;
1569 mdb
.length
= y_dimension
->values
[0].integer
;
1571 else if ((x_dimension
= ippFindAttribute(media_size
, "x-dimension",
1572 IPP_TAG_INTEGER
)) != NULL
&&
1573 (y_dimension
= ippFindAttribute(media_size
, "y-dimension",
1574 IPP_TAG_RANGE
)) != NULL
)
1580 mdb
.width
= x_dimension
->values
[0].integer
;
1581 mdb
.length
= y_dimension
->values
[0].range
.upper
;
1583 else if (flags
!= CUPS_MEDIA_FLAGS_READY
&&
1584 (x_dimension
= ippFindAttribute(media_size
, "x-dimension",
1585 IPP_TAG_RANGE
)) != NULL
&&
1586 (y_dimension
= ippFindAttribute(media_size
, "y-dimension",
1587 IPP_TAG_RANGE
)) != NULL
)
1590 * Custom size range; save this as the custom size value with default
1591 * margins, then continue; we'll capture the real margins below...
1596 dinfo
->min_size
.width
= x_dimension
->values
[0].range
.lower
;
1597 dinfo
->min_size
.length
= y_dimension
->values
[0].range
.lower
;
1598 dinfo
->min_size
.left
=
1599 dinfo
->min_size
.right
= 635; /* Default 1/4" side margins */
1600 dinfo
->min_size
.top
=
1601 dinfo
->min_size
.bottom
= 1270; /* Default 1/2" top/bottom margins */
1603 dinfo
->max_size
.width
= x_dimension
->values
[0].range
.upper
;
1604 dinfo
->max_size
.length
= y_dimension
->values
[0].range
.upper
;
1605 dinfo
->max_size
.left
=
1606 dinfo
->max_size
.right
= 635; /* Default 1/4" side margins */
1607 dinfo
->max_size
.top
=
1608 dinfo
->max_size
.bottom
= 1270; /* Default 1/2" top/bottom margins */
1613 if ((media_attr
= ippFindAttribute(val
->collection
, "media-color",
1614 IPP_TAG_ZERO
)) != NULL
&&
1615 (media_attr
->value_tag
== IPP_TAG_NAME
||
1616 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
1617 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
1618 mdb
.color
= media_attr
->values
[0].string
.text
;
1620 if ((media_attr
= ippFindAttribute(val
->collection
, "media-info",
1621 IPP_TAG_TEXT
)) != NULL
)
1622 mdb
.info
= media_attr
->values
[0].string
.text
;
1624 if ((media_attr
= ippFindAttribute(val
->collection
, "media-key",
1625 IPP_TAG_ZERO
)) != NULL
&&
1626 (media_attr
->value_tag
== IPP_TAG_NAME
||
1627 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
1628 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
1629 mdb
.key
= media_attr
->values
[0].string
.text
;
1631 if ((media_attr
= ippFindAttribute(val
->collection
, "media-size-name",
1632 IPP_TAG_ZERO
)) != NULL
&&
1633 (media_attr
->value_tag
== IPP_TAG_NAME
||
1634 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
1635 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
1636 mdb
.size_name
= media_attr
->values
[0].string
.text
;
1638 if ((media_attr
= ippFindAttribute(val
->collection
, "media-source",
1639 IPP_TAG_ZERO
)) != NULL
&&
1640 (media_attr
->value_tag
== IPP_TAG_NAME
||
1641 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
1642 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
1643 mdb
.source
= media_attr
->values
[0].string
.text
;
1645 if ((media_attr
= ippFindAttribute(val
->collection
, "media-type",
1646 IPP_TAG_ZERO
)) != NULL
&&
1647 (media_attr
->value_tag
== IPP_TAG_NAME
||
1648 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
1649 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
1650 mdb
.type
= media_attr
->values
[0].string
.text
;
1652 if ((media_attr
= ippFindAttribute(val
->collection
, "media-bottom-margin",
1653 IPP_TAG_INTEGER
)) != NULL
)
1654 mdb
.bottom
= media_attr
->values
[0].integer
;
1656 if ((media_attr
= ippFindAttribute(val
->collection
, "media-left-margin",
1657 IPP_TAG_INTEGER
)) != NULL
)
1658 mdb
.left
= media_attr
->values
[0].integer
;
1660 if ((media_attr
= ippFindAttribute(val
->collection
, "media-right-margin",
1661 IPP_TAG_INTEGER
)) != NULL
)
1662 mdb
.right
= media_attr
->values
[0].integer
;
1664 if ((media_attr
= ippFindAttribute(val
->collection
, "media-top-margin",
1665 IPP_TAG_INTEGER
)) != NULL
)
1666 mdb
.top
= media_attr
->values
[0].integer
;
1668 cupsArrayAdd(db
, &mdb
);
1673 if ((media_attr
= ippFindAttribute(custom
->collection
,
1674 "media-bottom-margin",
1675 IPP_TAG_INTEGER
)) != NULL
)
1677 dinfo
->min_size
.top
=
1678 dinfo
->max_size
.top
= media_attr
->values
[0].integer
;
1681 if ((media_attr
= ippFindAttribute(custom
->collection
,
1682 "media-left-margin",
1683 IPP_TAG_INTEGER
)) != NULL
)
1685 dinfo
->min_size
.left
=
1686 dinfo
->max_size
.left
= media_attr
->values
[0].integer
;
1689 if ((media_attr
= ippFindAttribute(custom
->collection
,
1690 "media-right-margin",
1691 IPP_TAG_INTEGER
)) != NULL
)
1693 dinfo
->min_size
.right
=
1694 dinfo
->max_size
.right
= media_attr
->values
[0].integer
;
1697 if ((media_attr
= ippFindAttribute(custom
->collection
,
1699 IPP_TAG_INTEGER
)) != NULL
)
1701 dinfo
->min_size
.top
=
1702 dinfo
->max_size
.top
= media_attr
->values
[0].integer
;
1706 else if (media_attr
&&
1707 (media_attr
->value_tag
== IPP_TAG_NAME
||
1708 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
1709 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
1711 memset(&mdb
, 0, sizeof(mdb
));
1714 mdb
.right
= 635; /* Default 1/4" side margins */
1716 mdb
.bottom
= 1270; /* Default 1/2" top/bottom margins */
1718 for (i
= media_attr
->num_values
, val
= media_attr
->values
;
1722 if ((pwg
= pwgMediaForPWG(val
->string
.text
)) == NULL
)
1723 if ((pwg
= pwgMediaForLegacy(val
->string
.text
)) == NULL
)
1725 DEBUG_printf(("3cups_create_media_db: Ignoring unknown size '%s'.",
1730 mdb
.width
= pwg
->width
;
1731 mdb
.length
= pwg
->length
;
1733 if (flags
!= CUPS_MEDIA_FLAGS_READY
&&
1734 !strncmp(val
->string
.text
, "custom_min_", 11))
1736 mdb
.size_name
= NULL
;
1737 dinfo
->min_size
= mdb
;
1739 else if (flags
!= CUPS_MEDIA_FLAGS_READY
&&
1740 !strncmp(val
->string
.text
, "custom_max_", 11))
1742 mdb
.size_name
= NULL
;
1743 dinfo
->max_size
= mdb
;
1747 mdb
.size_name
= val
->string
.text
;
1749 cupsArrayAdd(db
, &mdb
);
1757 * 'cups_free_media_cb()' - Free a media entry.
1762 _cups_media_db_t
*mdb
) /* I - Media entry to free */
1765 _cupsStrFree(mdb
->color
);
1767 _cupsStrFree(mdb
->key
);
1769 _cupsStrFree(mdb
->info
);
1771 _cupsStrFree(mdb
->size_name
);
1773 _cupsStrFree(mdb
->source
);
1775 _cupsStrFree(mdb
->type
);
1782 * 'cups_get_media_db()' - Lookup the media entry for a given size.
1785 static int /* O - 1 on match, 0 on failure */
1786 cups_get_media_db(http_t
*http
, /* I - Connection to destination */
1787 cups_dinfo_t
*dinfo
, /* I - Destination information */
1788 pwg_media_t
*pwg
, /* I - PWG media info */
1789 unsigned flags
, /* I - Media matching flags */
1790 cups_size_t
*size
) /* O - Media size/margin/name info */
1792 cups_array_t
*db
; /* Which media database to query */
1793 _cups_media_db_t
*mdb
, /* Current media database entry */
1794 *best
= NULL
, /* Best matching entry */
1795 key
; /* Search key */
1799 * Create the media database as needed...
1802 if (flags
& CUPS_MEDIA_FLAGS_READY
)
1804 cups_update_ready(http
, dinfo
);
1805 db
= dinfo
->ready_db
;
1809 if (!dinfo
->media_db
)
1810 cups_create_media_db(dinfo
, CUPS_MEDIA_FLAGS_DEFAULT
);
1812 db
= dinfo
->media_db
;
1819 memset(&key
, 0, sizeof(key
));
1820 key
.width
= pwg
->width
;
1821 key
.length
= pwg
->length
;
1823 if ((mdb
= cupsArrayFind(db
, &key
)) != NULL
)
1826 * Found an exact match, let's figure out the best margins for the flags
1832 if (flags
& CUPS_MEDIA_FLAGS_BORDERLESS
)
1835 * Look for the smallest margins...
1838 if (best
->left
!= 0 || best
->right
!= 0 || best
->top
!= 0 || best
->bottom
!= 0)
1840 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
1841 mdb
&& !cups_compare_media_db(mdb
, &key
);
1842 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1844 if (mdb
->left
<= best
->left
&& mdb
->right
<= best
->right
&&
1845 mdb
->top
<= best
->top
&& mdb
->bottom
<= best
->bottom
)
1848 if (mdb
->left
== 0 && mdb
->right
== 0 && mdb
->bottom
== 0 &&
1856 * If we need an exact match, return no-match if the size is not
1860 if ((flags
& CUPS_MEDIA_FLAGS_EXACT
) &&
1861 (best
->left
|| best
->right
|| best
->top
|| best
->bottom
))
1864 else if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
1867 * Look for the largest margins...
1870 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
1871 mdb
&& !cups_compare_media_db(mdb
, &key
);
1872 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1874 if (mdb
->left
>= best
->left
&& mdb
->right
>= best
->right
&&
1875 mdb
->top
>= best
->top
&& mdb
->bottom
>= best
->bottom
&&
1876 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
1883 * Look for the smallest non-zero margins...
1886 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
1887 mdb
&& !cups_compare_media_db(mdb
, &key
);
1888 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1890 if (((mdb
->left
> 0 && mdb
->left
<= best
->left
) || best
->left
== 0) &&
1891 ((mdb
->right
> 0 && mdb
->right
<= best
->right
) || best
->right
== 0) &&
1892 ((mdb
->top
> 0 && mdb
->top
<= best
->top
) || best
->top
== 0) &&
1893 ((mdb
->bottom
> 0 && mdb
->bottom
<= best
->bottom
) || best
->bottom
== 0) &&
1894 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
1899 else if (flags
& CUPS_MEDIA_FLAGS_EXACT
)
1902 * See if we can do this as a custom size...
1905 if (pwg
->width
< dinfo
->min_size
.width
||
1906 pwg
->width
> dinfo
->max_size
.width
||
1907 pwg
->length
< dinfo
->min_size
.length
||
1908 pwg
->length
> dinfo
->max_size
.length
)
1909 return (0); /* Out of range */
1911 if ((flags
& CUPS_MEDIA_FLAGS_BORDERLESS
) &&
1912 (dinfo
->min_size
.left
> 0 || dinfo
->min_size
.right
> 0 ||
1913 dinfo
->min_size
.top
> 0 || dinfo
->min_size
.bottom
> 0))
1914 return (0); /* Not borderless */
1916 key
.size_name
= (char *)pwg
->pwg
;
1917 key
.bottom
= dinfo
->min_size
.bottom
;
1918 key
.left
= dinfo
->min_size
.left
;
1919 key
.right
= dinfo
->min_size
.right
;
1920 key
.top
= dinfo
->min_size
.top
;
1924 else if (pwg
->width
>= dinfo
->min_size
.width
&&
1925 pwg
->width
<= dinfo
->max_size
.width
&&
1926 pwg
->length
>= dinfo
->min_size
.length
&&
1927 pwg
->length
<= dinfo
->max_size
.length
)
1930 * Map to custom size...
1933 key
.size_name
= (char *)pwg
->pwg
;
1934 key
.bottom
= dinfo
->min_size
.bottom
;
1935 key
.left
= dinfo
->min_size
.left
;
1936 key
.right
= dinfo
->min_size
.right
;
1937 key
.top
= dinfo
->min_size
.top
;
1944 * Find a close size...
1947 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
);
1949 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1950 if (cups_is_close_media_db(mdb
, &key
))
1958 if (flags
& CUPS_MEDIA_FLAGS_BORDERLESS
)
1961 * Look for the smallest margins...
1964 if (best
->left
!= 0 || best
->right
!= 0 || best
->top
!= 0 ||
1967 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
1968 mdb
&& cups_is_close_media_db(mdb
, &key
);
1969 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1971 if (mdb
->left
<= best
->left
&& mdb
->right
<= best
->right
&&
1972 mdb
->top
<= best
->top
&& mdb
->bottom
<= best
->bottom
&&
1973 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
1976 if (mdb
->left
== 0 && mdb
->right
== 0 && mdb
->bottom
== 0 &&
1983 else if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
1986 * Look for the largest margins...
1989 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
1990 mdb
&& cups_is_close_media_db(mdb
, &key
);
1991 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1993 if (mdb
->left
>= best
->left
&& mdb
->right
>= best
->right
&&
1994 mdb
->top
>= best
->top
&& mdb
->bottom
>= best
->bottom
&&
1995 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
2002 * Look for the smallest non-zero margins...
2005 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
2006 mdb
&& cups_is_close_media_db(mdb
, &key
);
2007 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2009 if (((mdb
->left
> 0 && mdb
->left
<= best
->left
) || best
->left
== 0) &&
2010 ((mdb
->right
> 0 && mdb
->right
<= best
->right
) ||
2011 best
->right
== 0) &&
2012 ((mdb
->top
> 0 && mdb
->top
<= best
->top
) || best
->top
== 0) &&
2013 ((mdb
->bottom
> 0 && mdb
->bottom
<= best
->bottom
) ||
2014 best
->bottom
== 0) &&
2015 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
2024 * Return the matching size...
2027 if (best
->size_name
)
2028 strlcpy(size
->media
, best
->size_name
, sizeof(size
->media
));
2030 strlcpy(size
->media
, best
->key
, sizeof(size
->media
));
2032 strlcpy(size
->media
, pwg
->pwg
, sizeof(size
->media
));
2034 size
->width
= best
->width
;
2035 size
->length
= best
->length
;
2036 size
->bottom
= best
->bottom
;
2037 size
->left
= best
->left
;
2038 size
->right
= best
->right
;
2039 size
->top
= best
->top
;
2049 * 'cups_is_close_media_db()' - Compare two media entries to see if they are
2050 * close to the same size.
2052 * Currently we use 5 points (from PostScript) as the matching range...
2055 static int /* O - 1 if the sizes are close */
2056 cups_is_close_media_db(
2057 _cups_media_db_t
*a
, /* I - First media entries */
2058 _cups_media_db_t
*b
) /* I - Second media entries */
2060 int dwidth
, /* Difference in width */
2061 dlength
; /* Difference in length */
2064 dwidth
= a
->width
- b
->width
;
2065 dlength
= a
->length
- b
->length
;
2067 return (dwidth
>= -176 && dwidth
<= 176 &&
2068 dlength
>= -176 && dlength
<= 176);
2073 * 'cups_test_constraints()' - Test constraints.
2075 * TODO: STR #4096 - Need to properly support media-col contraints...
2078 static cups_array_t
* /* O - Active constraints */
2079 cups_test_constraints(
2080 cups_dinfo_t
*dinfo
, /* I - Destination information */
2081 const char *new_option
, /* I - Newly selected option */
2082 const char *new_value
, /* I - Newly selected value */
2083 int num_options
, /* I - Number of options */
2084 cups_option_t
*options
, /* I - Options */
2085 int *num_conflicts
, /* O - Number of conflicting options */
2086 cups_option_t
**conflicts
) /* O - Conflicting options */
2088 int i
, /* Looping var */
2089 match
; /* Value matches? */
2090 int num_matching
; /* Number of matching options */
2091 cups_option_t
*matching
; /* Matching options */
2092 _cups_dconstres_t
*c
; /* Current constraint */
2093 cups_array_t
*active
= NULL
; /* Active constraints */
2094 ipp_attribute_t
*attr
; /* Current attribute */
2095 _ipp_value_t
*attrval
; /* Current attribute value */
2096 const char *value
; /* Current value */
2097 char temp
[1024]; /* Temporary string */
2098 int int_value
; /* Integer value */
2099 int xres_value
, /* Horizontal resolution */
2100 yres_value
; /* Vertical resolution */
2101 ipp_res_t units_value
; /* Resolution units */
2104 for (c
= (_cups_dconstres_t
*)cupsArrayFirst(dinfo
->constraints
);
2106 c
= (_cups_dconstres_t
*)cupsArrayNext(dinfo
->constraints
))
2111 for (attr
= ippFirstAttribute(c
->collection
);
2113 attr
= ippNextAttribute(c
->collection
))
2115 if (attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
)
2116 break; /* TODO: STR #4096 */
2119 * Get the value for the current attribute in the constraint...
2122 if (new_option
&& new_value
&& !strcmp(attr
->name
, new_option
))
2124 else if ((value
= cupsGetOption(attr
->name
, num_options
,
2126 value
= cupsGetOption(attr
->name
, dinfo
->num_defaults
, dinfo
->defaults
);
2131 * Not set so this constraint does not apply...
2139 switch (attr
->value_tag
)
2141 case IPP_TAG_INTEGER
:
2143 int_value
= atoi(value
);
2145 for (i
= attr
->num_values
, attrval
= attr
->values
;
2149 if (attrval
->integer
== int_value
)
2157 case IPP_TAG_BOOLEAN
:
2158 int_value
= !strcmp(value
, "true");
2160 for (i
= attr
->num_values
, attrval
= attr
->values
;
2164 if (attrval
->boolean
== int_value
)
2172 case IPP_TAG_RANGE
:
2173 int_value
= atoi(value
);
2175 for (i
= attr
->num_values
, attrval
= attr
->values
;
2179 if (int_value
>= attrval
->range
.lower
&&
2180 int_value
<= attrval
->range
.upper
)
2188 case IPP_TAG_RESOLUTION
:
2189 if (sscanf(value
, "%dx%d%15s", &xres_value
, &yres_value
, temp
) != 3)
2191 if (sscanf(value
, "%d%15s", &xres_value
, temp
) != 2)
2194 yres_value
= xres_value
;
2197 if (!strcmp(temp
, "dpi"))
2198 units_value
= IPP_RES_PER_INCH
;
2199 else if (!strcmp(temp
, "dpc") || !strcmp(temp
, "dpcm"))
2200 units_value
= IPP_RES_PER_CM
;
2204 for (i
= attr
->num_values
, attrval
= attr
->values
;
2208 if (attrval
->resolution
.xres
== xres_value
&&
2209 attrval
->resolution
.yres
== yres_value
&&
2210 attrval
->resolution
.units
== units_value
)
2220 case IPP_TAG_KEYWORD
:
2221 case IPP_TAG_CHARSET
:
2223 case IPP_TAG_URISCHEME
:
2224 case IPP_TAG_MIMETYPE
:
2225 case IPP_TAG_LANGUAGE
:
2226 case IPP_TAG_TEXTLANG
:
2227 case IPP_TAG_NAMELANG
:
2228 for (i
= attr
->num_values
, attrval
= attr
->values
;
2232 if (!strcmp(attrval
->string
.text
, value
))
2247 num_matching
= cupsAddOption(attr
->name
, value
, num_matching
, &matching
);
2253 active
= cupsArrayNew(NULL
, NULL
);
2255 cupsArrayAdd(active
, c
);
2257 if (num_conflicts
&& conflicts
)
2259 cups_option_t
*moption
; /* Matching option */
2261 for (i
= num_matching
, moption
= matching
; i
> 0; i
--, moption
++)
2262 *num_conflicts
= cupsAddOption(moption
->name
, moption
->value
,
2263 *num_conflicts
, conflicts
);
2267 cupsFreeOptions(num_matching
, matching
);
2275 * 'cups_update_ready()' - Update xxx-ready attributes for the printer.
2279 cups_update_ready(http_t
*http
, /* I - Connection to destination */
2280 cups_dinfo_t
*dinfo
) /* I - Destination information */
2282 ipp_t
*request
; /* Get-Printer-Attributes request */
2283 static const char * const pattrs
[] = /* Printer attributes we want */
2285 "finishings-col-ready",
2287 "job-finishings-col-ready",
2288 "job-finishings-ready",
2295 * Don't update more than once every 30 seconds...
2298 if ((time(NULL
) - dinfo
->ready_time
) < _CUPS_MEDIA_READY_TTL
)
2302 * Free any previous results...
2305 if (dinfo
->cached_flags
& CUPS_MEDIA_FLAGS_READY
)
2307 cupsArrayDelete(dinfo
->cached_db
);
2308 dinfo
->cached_db
= NULL
;
2309 dinfo
->cached_flags
= CUPS_MEDIA_FLAGS_DEFAULT
;
2312 ippDelete(dinfo
->ready_attrs
);
2313 dinfo
->ready_attrs
= NULL
;
2315 cupsArrayDelete(dinfo
->ready_db
);
2316 dinfo
->ready_db
= NULL
;
2319 * Query the xxx-ready values...
2322 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
2323 ippSetVersion(request
, dinfo
->version
/ 10, dinfo
->version
% 10);
2325 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
2327 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
2329 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "requested-attributes", (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
2331 dinfo
->ready_attrs
= cupsDoRequest(http
, request
, dinfo
->resource
);
2334 * Update the ready media database...
2337 cups_create_media_db(dinfo
, CUPS_MEDIA_FLAGS_READY
);
2340 * Update last lookup time and return...
2343 dinfo
->ready_time
= time(NULL
);