2 * Destination option/media support for CUPS.
4 * Copyright 2012-2016 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 * file is 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/OS X 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 * Range check input...
92 if (!http
|| !dest
|| !dinfo
|| !option
|| !value
)
96 * Lookup the attribute...
99 if (strstr(option
, "-supported"))
100 attr
= ippFindAttribute(dinfo
->attrs
, option
, IPP_TAG_ZERO
);
103 snprintf(temp
, sizeof(temp
), "%s-supported", option
);
104 attr
= ippFindAttribute(dinfo
->attrs
, temp
, IPP_TAG_ZERO
);
114 if (!strcmp(option
, "media") && !strncmp(value
, "custom_", 7))
117 * Check range of custom media sizes...
120 pwg_media_t
*pwg
; /* Current PWG media size info */
121 int min_width
, /* Minimum width */
122 min_length
, /* Minimum length */
123 max_width
, /* Maximum width */
124 max_length
; /* Maximum length */
127 * Get the minimum and maximum size...
130 min_width
= min_length
= INT_MAX
;
131 max_width
= max_length
= 0;
133 for (i
= attr
->num_values
, attrval
= attr
->values
;
137 if (!strncmp(attrval
->string
.text
, "custom_min_", 11) &&
138 (pwg
= pwgMediaForPWG(attrval
->string
.text
)) != NULL
)
140 min_width
= pwg
->width
;
141 min_length
= pwg
->length
;
143 else if (!strncmp(attrval
->string
.text
, "custom_max_", 11) &&
144 (pwg
= pwgMediaForPWG(attrval
->string
.text
)) != NULL
)
146 max_width
= pwg
->width
;
147 max_length
= pwg
->length
;
155 if (min_width
< INT_MAX
&& max_width
> 0 &&
156 (pwg
= pwgMediaForPWG(value
)) != NULL
&&
157 pwg
->width
>= min_width
&& pwg
->width
<= max_width
&&
158 pwg
->length
>= min_length
&& pwg
->length
<= max_length
)
164 * Check literal values...
167 switch (attr
->value_tag
)
169 case IPP_TAG_INTEGER
:
171 int_value
= atoi(value
);
173 for (i
= 0; i
< attr
->num_values
; i
++)
174 if (attr
->values
[i
].integer
== int_value
)
178 case IPP_TAG_BOOLEAN
:
179 return (attr
->values
[0].boolean
);
182 int_value
= atoi(value
);
184 for (i
= 0; i
< attr
->num_values
; i
++)
185 if (int_value
>= attr
->values
[i
].range
.lower
&&
186 int_value
<= attr
->values
[i
].range
.upper
)
190 case IPP_TAG_RESOLUTION
:
191 if (sscanf(value
, "%dx%d%15s", &xres_value
, &yres_value
, temp
) != 3)
193 if (sscanf(value
, "%d%15s", &xres_value
, temp
) != 2)
196 yres_value
= xres_value
;
199 if (!strcmp(temp
, "dpi"))
200 units_value
= IPP_RES_PER_INCH
;
201 else if (!strcmp(temp
, "dpc") || !strcmp(temp
, "dpcm"))
202 units_value
= IPP_RES_PER_CM
;
206 for (i
= attr
->num_values
, attrval
= attr
->values
;
210 if (attrval
->resolution
.xres
== xres_value
&&
211 attrval
->resolution
.yres
== yres_value
&&
212 attrval
->resolution
.units
== units_value
)
219 case IPP_TAG_KEYWORD
:
220 case IPP_TAG_CHARSET
:
222 case IPP_TAG_URISCHEME
:
223 case IPP_TAG_MIMETYPE
:
224 case IPP_TAG_LANGUAGE
:
225 case IPP_TAG_TEXTLANG
:
226 case IPP_TAG_NAMELANG
:
227 for (i
= 0; i
< attr
->num_values
; i
++)
228 if (!strcmp(attr
->values
[i
].string
.text
, value
))
238 * If we get there the option+value is not supported...
246 * 'cupsCopyDestConflicts()' - Get conflicts and resolutions for a new
249 * "num_options" and "options" represent the currently selected options by the
250 * user. "new_option" and "new_value" are the setting the user has just
253 * Returns 1 if there is a conflict, 0 if there are no conflicts, and -1 if
254 * there was an unrecoverable error such as a resolver loop.
256 * If "num_conflicts" and "conflicts" are not @code NULL@, they are set to
257 * contain the list of conflicting option/value pairs. Similarly, if
258 * "num_resolved" and "resolved" are not @code NULL@ they will be set to the
259 * list of changes needed to resolve the conflict.
261 * If cupsCopyDestConflicts returns 1 but "num_resolved" and "resolved" are set
262 * to 0 and @code NULL@, respectively, then the conflict cannot be resolved.
264 * @since CUPS 1.6/OS X 10.8@
267 int /* O - 1 if there is a conflict, 0 if none, -1 on error */
268 cupsCopyDestConflicts(
269 http_t
*http
, /* I - Connection to destination */
270 cups_dest_t
*dest
, /* I - Destination */
271 cups_dinfo_t
*dinfo
, /* I - Destination information */
272 int num_options
, /* I - Number of current options */
273 cups_option_t
*options
, /* I - Current options */
274 const char *new_option
, /* I - New option */
275 const char *new_value
, /* I - New value */
276 int *num_conflicts
, /* O - Number of conflicting options */
277 cups_option_t
**conflicts
, /* O - Conflicting options */
278 int *num_resolved
, /* O - Number of options to resolve */
279 cups_option_t
**resolved
) /* O - Resolved options */
281 int i
, /* Looping var */
282 have_conflicts
= 0, /* Do we have conflicts? */
283 changed
, /* Did we change something? */
284 tries
, /* Number of tries for resolution */
285 num_myconf
= 0, /* My number of conflicting options */
286 num_myres
= 0; /* My number of resolved options */
287 cups_option_t
*myconf
= NULL
, /* My conflicting options */
288 *myres
= NULL
, /* My resolved options */
289 *myoption
, /* My current option */
290 *option
; /* Current option */
291 cups_array_t
*active
= NULL
, /* Active conflicts */
292 *pass
= NULL
, /* Resolvers for this pass */
293 *resolvers
= NULL
, /* Resolvers we have used */
294 *test
; /* Test array for conflicts */
295 _cups_dconstres_t
*c
, /* Current constraint */
296 *r
; /* Current resolver */
297 ipp_attribute_t
*attr
; /* Current attribute */
298 char value
[2048]; /* Current attribute value as string */
299 const char *myvalue
; /* Current value of an option */
303 * Clear returned values...
319 * Range check input...
322 if (!http
|| !dest
|| !dinfo
||
323 (num_conflicts
!= NULL
) != (conflicts
!= NULL
) ||
324 (num_resolved
!= NULL
) != (resolved
!= NULL
))
328 * Load constraints as needed...
331 if (!dinfo
->constraints
)
332 cups_create_constraints(dinfo
);
334 if (cupsArrayCount(dinfo
->constraints
) == 0)
337 if (!dinfo
->num_defaults
)
338 cups_create_defaults(dinfo
);
341 * If we are resolving, create a shadow array...
346 for (i
= num_options
, option
= options
; i
> 0; i
--, option
++)
347 num_myres
= cupsAddOption(option
->name
, option
->value
, num_myres
, &myres
);
349 if (new_option
&& new_value
)
350 num_myres
= cupsAddOption(new_option
, new_value
, num_myres
, &myres
);
354 num_myres
= num_options
;
359 * Check for any conflicts...
363 pass
= cupsArrayNew((cups_array_func_t
)cups_compare_dconstres
, NULL
);
365 for (tries
= 0; tries
< 100; tries
++)
368 * Check for any conflicts...
371 if (num_conflicts
|| num_resolved
)
373 cupsFreeOptions(num_myconf
, myconf
);
377 active
= cups_test_constraints(dinfo
, new_option
, new_value
,
378 num_myres
, myres
, &num_myconf
,
382 active
= cups_test_constraints(dinfo
, new_option
, new_value
, num_myres
,
385 have_conflicts
= (active
!= NULL
);
387 if (!active
|| !num_resolved
)
388 break; /* All done */
391 * Scan the constraints that were triggered to apply resolvers...
395 resolvers
= cupsArrayNew((cups_array_func_t
)cups_compare_dconstres
, NULL
);
397 for (c
= (_cups_dconstres_t
*)cupsArrayFirst(active
), changed
= 0;
399 c
= (_cups_dconstres_t
*)cupsArrayNext(active
))
401 if (cupsArrayFind(pass
, c
))
402 continue; /* Already applied this resolver... */
404 if (cupsArrayFind(resolvers
, c
))
406 DEBUG_printf(("1cupsCopyDestConflicts: Resolver loop with %s.",
412 if ((r
= cupsArrayFind(dinfo
->resolvers
, c
)) == NULL
)
414 DEBUG_printf(("1cupsCopyDestConflicts: Resolver %s not found.",
421 * Add the options from the resolver...
424 cupsArrayAdd(pass
, r
);
425 cupsArrayAdd(resolvers
, r
);
427 for (attr
= ippFirstAttribute(r
->collection
);
429 attr
= ippNextAttribute(r
->collection
))
431 if (new_option
&& !strcmp(attr
->name
, new_option
))
432 continue; /* Ignore this if we just changed it */
434 if (ippAttributeString(attr
, value
, sizeof(value
)) >= sizeof(value
))
435 continue; /* Ignore if the value is too long */
437 if ((test
= cups_test_constraints(dinfo
, attr
->name
, value
, num_myres
,
438 myres
, NULL
, NULL
)) == NULL
)
441 * That worked, flag it...
447 cupsArrayDelete(test
);
450 * Add the option/value from the resolver regardless of whether it
451 * worked; this makes sure that we can cascade several changes to
452 * make things resolve...
455 num_myres
= cupsAddOption(attr
->name
, value
, num_myres
, &myres
);
461 DEBUG_puts("1cupsCopyDestConflicts: Unable to resolve constraints.");
466 cupsArrayClear(pass
);
468 cupsArrayDelete(active
);
474 DEBUG_puts("1cupsCopyDestConflicts: Unable to resolve after 100 tries.");
480 * Copy resolved options as needed...
485 for (i
= num_myres
, myoption
= myres
; i
> 0; i
--, myoption
++)
487 if ((myvalue
= cupsGetOption(myoption
->name
, num_options
,
489 strcmp(myvalue
, myoption
->value
))
491 if (new_option
&& !strcmp(new_option
, myoption
->name
) &&
492 new_value
&& !strcmp(new_value
, myoption
->value
))
495 *num_resolved
= cupsAddOption(myoption
->name
, myoption
->value
,
496 *num_resolved
, resolved
);
507 cupsArrayDelete(active
);
508 cupsArrayDelete(pass
);
509 cupsArrayDelete(resolvers
);
514 * Free shadow copy of options...
517 cupsFreeOptions(num_myres
, myres
);
523 * Return conflicting options to caller...
526 *num_conflicts
= num_myconf
;
532 * Free conflicting options...
535 cupsFreeOptions(num_myconf
, myconf
);
538 return (have_conflicts
);
543 * 'cupsCopyDestInfo()' - Get the supported values/capabilities for the
546 * The caller is responsible for calling @link cupsFreeDestInfo@ on the return
547 * value. @code NULL@ is returned on error.
549 * @since CUPS 1.6/OS X 10.8@
552 cups_dinfo_t
* /* O - Destination information */
554 http_t
*http
, /* I - Connection to destination */
555 cups_dest_t
*dest
) /* I - Destination */
557 cups_dinfo_t
*dinfo
; /* Destination information */
558 ipp_t
*request
, /* Get-Printer-Attributes request */
559 *response
; /* Supported attributes */
560 int tries
, /* Number of tries so far */
561 delay
, /* Current retry delay */
562 prev_delay
; /* Next retry delay */
563 const char *uri
; /* Printer URI */
564 char resource
[1024]; /* Resource path */
565 int version
; /* IPP version */
566 ipp_status_t status
; /* Status of request */
567 static const char * const requested_attrs
[] =
568 { /* Requested attributes */
570 "media-col-database",
571 "printer-description"
575 DEBUG_printf(("cupsCopyDestSupported(http=%p, dest=%p(%s))", http
, dest
,
576 dest
? dest
->name
: ""));
579 * Range check input...
586 * Get the printer URI and resource path...
589 if ((uri
= _cupsGetDestResource(dest
, resource
, sizeof(resource
))) == NULL
)
593 * Get the supported attributes...
604 * Send a Get-Printer-Attributes request...
607 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
608 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
610 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
611 "requesting-user-name", NULL
, cupsUser());
612 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
613 "requested-attributes",
614 (int)(sizeof(requested_attrs
) / sizeof(requested_attrs
[0])),
615 NULL
, requested_attrs
);
616 response
= cupsDoRequest(http
, request
, resource
);
617 status
= cupsLastError();
619 if (status
> IPP_STATUS_OK_IGNORED_OR_SUBSTITUTED
)
621 DEBUG_printf(("cupsCopyDestSupported: Get-Printer-Attributes for '%s' "
622 "returned %s (%s)", dest
->name
, ippErrorString(status
),
623 cupsLastErrorString()));
628 if (status
== IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
&& version
> 11)
630 else if (status
== IPP_STATUS_ERROR_BUSY
)
632 sleep((unsigned)delay
);
634 delay
= _cupsNextDelay(delay
, &prev_delay
);
642 while (!response
&& tries
< 10);
648 * Allocate a cups_dinfo_t structure and return it...
651 if ((dinfo
= calloc(1, sizeof(cups_dinfo_t
))) == NULL
)
653 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
658 dinfo
->version
= version
;
660 dinfo
->resource
= _cupsStrAlloc(resource
);
661 dinfo
->attrs
= response
;
668 * 'cupsFindDestDefault()' - Find the default value(s) for the given option.
670 * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
671 * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
672 * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
673 * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
674 * functions to inspect the default value(s) as needed.
676 * @since CUPS 1.7/OS X 10.9@
679 ipp_attribute_t
* /* O - Default attribute or @code NULL@ for none */
681 http_t
*http
, /* I - Connection to destination */
682 cups_dest_t
*dest
, /* I - Destination */
683 cups_dinfo_t
*dinfo
, /* I - Destination information */
684 const char *option
) /* I - Option/attribute name */
686 char name
[IPP_MAX_NAME
]; /* Attribute name */
690 * Range check input...
693 if (!http
|| !dest
|| !dinfo
|| !option
)
695 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
700 * Find and return the attribute...
703 snprintf(name
, sizeof(name
), "%s-default", option
);
704 return (ippFindAttribute(dinfo
->attrs
, name
, IPP_TAG_ZERO
));
709 * 'cupsFindDestReady()' - Find the default value(s) for the given option.
711 * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
712 * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
713 * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
714 * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
715 * functions to inspect the default value(s) as needed.
717 * @since CUPS 1.7/OS X 10.9@
720 ipp_attribute_t
* /* O - Default attribute or @code NULL@ for none */
722 http_t
*http
, /* I - Connection to destination */
723 cups_dest_t
*dest
, /* I - Destination */
724 cups_dinfo_t
*dinfo
, /* I - Destination information */
725 const char *option
) /* I - Option/attribute name */
727 char name
[IPP_MAX_NAME
]; /* Attribute name */
731 * Range check input...
734 if (!http
|| !dest
|| !dinfo
|| !option
)
736 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
741 * Find and return the attribute...
744 cups_update_ready(http
, dinfo
);
746 snprintf(name
, sizeof(name
), "%s-ready", option
);
747 return (ippFindAttribute(dinfo
->ready_attrs
, name
, IPP_TAG_ZERO
));
752 * 'cupsFindDestSupported()' - Find the default value(s) for the given option.
754 * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
755 * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
756 * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
757 * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
758 * functions to inspect the default value(s) as needed.
760 * @since CUPS 1.7/OS X 10.9@
763 ipp_attribute_t
* /* O - Default attribute or @code NULL@ for none */
764 cupsFindDestSupported(
765 http_t
*http
, /* I - Connection to destination */
766 cups_dest_t
*dest
, /* I - Destination */
767 cups_dinfo_t
*dinfo
, /* I - Destination information */
768 const char *option
) /* I - Option/attribute name */
770 char name
[IPP_MAX_NAME
]; /* Attribute name */
774 * Range check input...
777 if (!http
|| !dest
|| !dinfo
|| !option
)
779 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
784 * Find and return the attribute...
787 snprintf(name
, sizeof(name
), "%s-supported", option
);
788 return (ippFindAttribute(dinfo
->attrs
, name
, IPP_TAG_ZERO
));
793 * 'cupsFreeDestInfo()' - Free destination information obtained using
794 * @link cupsCopyDestInfo@.
798 cupsFreeDestInfo(cups_dinfo_t
*dinfo
) /* I - Destination information */
801 * Range check input...
808 * Free memory and return...
811 _cupsStrFree(dinfo
->resource
);
813 cupsArrayDelete(dinfo
->constraints
);
814 cupsArrayDelete(dinfo
->resolvers
);
816 cupsArrayDelete(dinfo
->localizations
);
818 cupsArrayDelete(dinfo
->media_db
);
820 cupsArrayDelete(dinfo
->cached_db
);
822 ippDelete(dinfo
->ready_attrs
);
823 cupsArrayDelete(dinfo
->ready_db
);
825 ippDelete(dinfo
->attrs
);
832 * 'cupsGetDestMediaByIndex()' - Get a media name, dimension, and margins for a
835 * The @code flags@ parameter determines which set of media are indexed. For
836 * example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will get the Nth
837 * borderless size supported by the printer.
839 * @since CUPS 1.7/OS X 10.9@
842 int /* O - 1 on success, 0 on failure */
843 cupsGetDestMediaByIndex(
844 http_t
*http
, /* I - Connection to destination */
845 cups_dest_t
*dest
, /* I - Destination */
846 cups_dinfo_t
*dinfo
, /* I - Destination information */
847 int n
, /* I - Media size number (0-based) */
848 unsigned flags
, /* I - Media flags */
849 cups_size_t
*size
) /* O - Media size information */
851 _cups_media_db_t
*nsize
; /* Size for N */
852 pwg_media_t
*pwg
; /* PWG media name for size */
856 * Range check input...
860 memset(size
, 0, sizeof(cups_size_t
));
862 if (!http
|| !dest
|| !dinfo
|| n
< 0 || !size
)
864 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
869 * Load media list as needed...
872 if (flags
& CUPS_MEDIA_FLAGS_READY
)
873 cups_update_ready(http
, dinfo
);
875 if (!dinfo
->cached_db
|| dinfo
->cached_flags
!= flags
)
876 cups_create_cached(http
, dinfo
, flags
);
879 * Copy the size over and return...
882 if ((nsize
= (_cups_media_db_t
*)cupsArrayIndex(dinfo
->cached_db
, n
)) == NULL
)
884 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
888 if (nsize
->size_name
)
889 strlcpy(size
->media
, nsize
->size_name
, sizeof(size
->media
));
891 strlcpy(size
->media
, nsize
->key
, sizeof(size
->media
));
892 else if ((pwg
= pwgMediaForSize(nsize
->width
, nsize
->length
)) != NULL
)
893 strlcpy(size
->media
, pwg
->pwg
, sizeof(size
->media
));
896 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
900 size
->width
= nsize
->width
;
901 size
->length
= nsize
->length
;
902 size
->bottom
= nsize
->bottom
;
903 size
->left
= nsize
->left
;
904 size
->right
= nsize
->right
;
905 size
->top
= nsize
->top
;
912 * 'cupsGetDestMediaByName()' - Get media names, dimensions, and margins.
914 * The "media" string is a PWG media name. "Flags" provides some matching
915 * guidance (multiple flags can be combined):
917 * CUPS_MEDIA_FLAGS_DEFAULT = find the closest size supported by the printer,
918 * CUPS_MEDIA_FLAGS_BORDERLESS = find a borderless size,
919 * CUPS_MEDIA_FLAGS_DUPLEX = find a size compatible with 2-sided printing,
920 * CUPS_MEDIA_FLAGS_EXACT = find an exact match for the size, and
921 * CUPS_MEDIA_FLAGS_READY = if the printer supports media sensing, find the
922 * size amongst the "ready" media.
924 * The matching result (if any) is returned in the "cups_size_t" structure.
926 * Returns 1 when there is a match and 0 if there is not a match.
928 * @since CUPS 1.6/OS X 10.8@
931 int /* O - 1 on match, 0 on failure */
932 cupsGetDestMediaByName(
933 http_t
*http
, /* I - Connection to destination */
934 cups_dest_t
*dest
, /* I - Destination */
935 cups_dinfo_t
*dinfo
, /* I - Destination information */
936 const char *media
, /* I - Media name */
937 unsigned flags
, /* I - Media matching flags */
938 cups_size_t
*size
) /* O - Media size information */
940 pwg_media_t
*pwg
; /* PWG media info */
944 * Range check input...
948 memset(size
, 0, sizeof(cups_size_t
));
950 if (!http
|| !dest
|| !dinfo
|| !media
|| !size
)
952 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
957 * Lookup the media size name...
960 if ((pwg
= pwgMediaForPWG(media
)) == NULL
)
961 if ((pwg
= pwgMediaForLegacy(media
)) == NULL
)
963 DEBUG_printf(("1cupsGetDestMediaByName: Unknown size '%s'.", media
));
964 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown media size name."), 1);
972 return (cups_get_media_db(http
, dinfo
, pwg
, flags
, size
));
977 * 'cupsGetDestMediaBySize()' - Get media names, dimensions, and margins.
979 * "Width" and "length" are the dimensions in hundredths of millimeters.
980 * "Flags" provides some matching guidance (multiple flags can be combined):
982 * CUPS_MEDIA_FLAGS_DEFAULT = find the closest size supported by the printer,
983 * CUPS_MEDIA_FLAGS_BORDERLESS = find a borderless size,
984 * CUPS_MEDIA_FLAGS_DUPLEX = find a size compatible with 2-sided printing,
985 * CUPS_MEDIA_FLAGS_EXACT = find an exact match for the size, and
986 * CUPS_MEDIA_FLAGS_READY = if the printer supports media sensing, find the
987 * size amongst the "ready" media.
989 * The matching result (if any) is returned in the "cups_size_t" structure.
991 * Returns 1 when there is a match and 0 if there is not a match.
993 * @since CUPS 1.6/OS X 10.8@
996 int /* O - 1 on match, 0 on failure */
997 cupsGetDestMediaBySize(
998 http_t
*http
, /* I - Connection to destination */
999 cups_dest_t
*dest
, /* I - Destination */
1000 cups_dinfo_t
*dinfo
, /* I - Destination information */
1001 int width
, /* I - Media width in hundredths of
1003 int length
, /* I - Media length in hundredths of
1005 unsigned flags
, /* I - Media matching flags */
1006 cups_size_t
*size
) /* O - Media size information */
1008 pwg_media_t
*pwg
; /* PWG media info */
1012 * Range check input...
1016 memset(size
, 0, sizeof(cups_size_t
));
1018 if (!http
|| !dest
|| !dinfo
|| width
<= 0 || length
<= 0 || !size
)
1020 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1025 * Lookup the media size name...
1028 if ((pwg
= pwgMediaForSize(width
, length
)) == NULL
)
1030 DEBUG_printf(("1cupsGetDestMediaBySize: Invalid size %dx%d.", width
,
1032 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Invalid media size."), 1);
1037 * Lookup the size...
1040 return (cups_get_media_db(http
, dinfo
, pwg
, flags
, size
));
1045 * 'cupsGetDestMediaCount()' - Get the number of sizes supported by a
1048 * The @code flags@ parameter determines the set of media sizes that are
1049 * counted. For example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will return
1050 * the number of borderless sizes.
1052 * @since CUPS 1.7/OS X 10.9@
1055 int /* O - Number of sizes */
1056 cupsGetDestMediaCount(
1057 http_t
*http
, /* I - Connection to destination */
1058 cups_dest_t
*dest
, /* I - Destination */
1059 cups_dinfo_t
*dinfo
, /* I - Destination information */
1060 unsigned flags
) /* I - Media flags */
1063 * Range check input...
1066 if (!http
|| !dest
|| !dinfo
)
1068 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1073 * Load media list as needed...
1076 if (flags
& CUPS_MEDIA_FLAGS_READY
)
1077 cups_update_ready(http
, dinfo
);
1079 if (!dinfo
->cached_db
|| dinfo
->cached_flags
!= flags
)
1080 cups_create_cached(http
, dinfo
, flags
);
1082 return (cupsArrayCount(dinfo
->cached_db
));
1087 * 'cupsGetDestMediaDefault()' - Get the default size for a destination.
1089 * The @code flags@ parameter determines which default size is returned. For
1090 * example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will return the default
1091 * borderless size, typically US Letter or A4, but sometimes 4x6 photo media.
1093 * @since CUPS 1.7/OS X 10.9@
1096 int /* O - 1 on success, 0 on failure */
1097 cupsGetDestMediaDefault(
1098 http_t
*http
, /* I - Connection to destination */
1099 cups_dest_t
*dest
, /* I - Destination */
1100 cups_dinfo_t
*dinfo
, /* I - Destination information */
1101 unsigned flags
, /* I - Media flags */
1102 cups_size_t
*size
) /* O - Media size information */
1104 const char *media
; /* Default media size */
1108 * Range check input...
1112 memset(size
, 0, sizeof(cups_size_t
));
1114 if (!http
|| !dest
|| !dinfo
|| !size
)
1116 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1121 * Get the default media size, if any...
1124 if ((media
= cupsGetOption("media", dest
->num_options
,
1125 dest
->options
)) == NULL
)
1126 media
= "na_letter_8.5x11in";
1128 if (cupsGetDestMediaByName(http
, dest
, dinfo
, media
, flags
, size
))
1131 if (strcmp(media
, "na_letter_8.5x11in") &&
1132 cupsGetDestMediaByName(http
, dest
, dinfo
, "iso_a4_210x297mm", flags
,
1136 if (strcmp(media
, "iso_a4_210x297mm") &&
1137 cupsGetDestMediaByName(http
, dest
, dinfo
, "na_letter_8.5x11in", flags
,
1141 if ((flags
& CUPS_MEDIA_FLAGS_BORDERLESS
) &&
1142 cupsGetDestMediaByName(http
, dest
, dinfo
, "na_index_4x6in", flags
, size
))
1146 * Fall back to the first matching media size...
1149 return (cupsGetDestMediaByIndex(http
, dest
, dinfo
, 0, flags
, size
));
1154 * 'cups_add_dconstres()' - Add a constraint or resolver to an array.
1159 cups_array_t
*a
, /* I - Array */
1160 ipp_t
*collection
) /* I - Collection value */
1162 ipp_attribute_t
*attr
; /* Attribute */
1163 _cups_dconstres_t
*temp
; /* Current constraint/resolver */
1166 if ((attr
= ippFindAttribute(collection
, "resolver-name",
1167 IPP_TAG_NAME
)) == NULL
)
1170 if ((temp
= calloc(1, sizeof(_cups_dconstres_t
))) == NULL
)
1173 temp
->name
= attr
->values
[0].string
.text
;
1174 temp
->collection
= collection
;
1176 cupsArrayAdd(a
, temp
);
1181 * 'cups_compare_dconstres()' - Compare to resolver entries.
1184 static int /* O - Result of comparison */
1185 cups_compare_dconstres(
1186 _cups_dconstres_t
*a
, /* I - First resolver */
1187 _cups_dconstres_t
*b
) /* I - Second resolver */
1189 return (strcmp(a
->name
, b
->name
));
1194 * 'cups_compare_media_db()' - Compare two media entries.
1197 static int /* O - Result of comparison */
1198 cups_compare_media_db(
1199 _cups_media_db_t
*a
, /* I - First media entries */
1200 _cups_media_db_t
*b
) /* I - Second media entries */
1202 int result
; /* Result of comparison */
1205 if ((result
= a
->width
- b
->width
) == 0)
1206 result
= a
->length
- b
->length
;
1213 * 'cups_copy_media_db()' - Copy a media entry.
1216 static _cups_media_db_t
* /* O - New media entry */
1218 _cups_media_db_t
*mdb
) /* I - Media entry to copy */
1220 _cups_media_db_t
*temp
; /* New media entry */
1223 if ((temp
= calloc(1, sizeof(_cups_media_db_t
))) == NULL
)
1227 temp
->color
= _cupsStrAlloc(mdb
->color
);
1229 temp
->key
= _cupsStrAlloc(mdb
->key
);
1231 temp
->info
= _cupsStrAlloc(mdb
->info
);
1233 temp
->size_name
= _cupsStrAlloc(mdb
->size_name
);
1235 temp
->source
= _cupsStrAlloc(mdb
->source
);
1237 temp
->type
= _cupsStrAlloc(mdb
->type
);
1239 temp
->width
= mdb
->width
;
1240 temp
->length
= mdb
->length
;
1241 temp
->bottom
= mdb
->bottom
;
1242 temp
->left
= mdb
->left
;
1243 temp
->right
= mdb
->right
;
1244 temp
->top
= mdb
->top
;
1251 * 'cups_create_cached()' - Create the media selection cache.
1255 cups_create_cached(http_t
*http
, /* I - Connection to destination */
1256 cups_dinfo_t
*dinfo
, /* I - Destination information */
1257 unsigned flags
) /* I - Media selection flags */
1259 cups_array_t
*db
; /* Media database array to use */
1260 _cups_media_db_t
*mdb
, /* Media database entry */
1261 *first
; /* First entry this size */
1264 DEBUG_printf(("3cups_create_cached(http=%p, dinfo=%p, flags=%u)", http
, dinfo
, flags
));
1266 if (dinfo
->cached_db
)
1267 cupsArrayDelete(dinfo
->cached_db
);
1269 dinfo
->cached_db
= cupsArrayNew(NULL
, NULL
);
1270 dinfo
->cached_flags
= flags
;
1272 if (flags
& CUPS_MEDIA_FLAGS_READY
)
1274 DEBUG_puts("4cups_create_cached: ready media");
1276 cups_update_ready(http
, dinfo
);
1277 db
= dinfo
->ready_db
;
1281 DEBUG_puts("4cups_create_cached: supported media");
1283 if (!dinfo
->media_db
)
1284 cups_create_media_db(dinfo
, CUPS_MEDIA_FLAGS_DEFAULT
);
1286 db
= dinfo
->media_db
;
1289 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
), first
= mdb
;
1291 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1293 DEBUG_printf(("4cups_create_cached: %p key=\"%s\", type=\"%s\", %dx%d, B%d L%d R%d T%d", mdb
, mdb
->key
, mdb
->type
, mdb
->width
, mdb
->length
, mdb
->bottom
, mdb
->left
, mdb
->right
, mdb
->top
));
1295 if (flags
& CUPS_MEDIA_FLAGS_BORDERLESS
)
1297 if (!mdb
->left
&& !mdb
->right
&& !mdb
->top
&& !mdb
->bottom
)
1299 DEBUG_printf(("4cups_create_cached: add %p", mdb
));
1300 cupsArrayAdd(dinfo
->cached_db
, mdb
);
1303 else if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
1305 if (first
->width
!= mdb
->width
|| first
->length
!= mdb
->length
)
1307 DEBUG_printf(("4cups_create_cached: add %p", first
));
1308 cupsArrayAdd(dinfo
->cached_db
, first
);
1311 else if (mdb
->left
>= first
->left
&& mdb
->right
>= first
->right
&& mdb
->top
>= first
->top
&& mdb
->bottom
>= first
->bottom
&&
1312 (mdb
->left
!= first
->left
|| mdb
->right
!= first
->right
|| mdb
->top
!= first
->top
|| mdb
->bottom
!= first
->bottom
))
1317 DEBUG_printf(("4cups_create_cached: add %p", mdb
));
1318 cupsArrayAdd(dinfo
->cached_db
, mdb
);
1322 if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
1324 DEBUG_printf(("4cups_create_cached: add %p", first
));
1325 cupsArrayAdd(dinfo
->cached_db
, first
);
1331 * 'cups_create_constraints()' - Create the constraints and resolvers arrays.
1335 cups_create_constraints(
1336 cups_dinfo_t
*dinfo
) /* I - Destination information */
1338 int i
; /* Looping var */
1339 ipp_attribute_t
*attr
; /* Attribute */
1340 _ipp_value_t
*val
; /* Current value */
1343 dinfo
->constraints
= cupsArrayNew3(NULL
, NULL
, NULL
, 0, NULL
,
1344 (cups_afree_func_t
)free
);
1345 dinfo
->resolvers
= cupsArrayNew3((cups_array_func_t
)cups_compare_dconstres
,
1346 NULL
, NULL
, 0, NULL
,
1347 (cups_afree_func_t
)free
);
1349 if ((attr
= ippFindAttribute(dinfo
->attrs
, "job-constraints-supported",
1350 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1352 for (i
= attr
->num_values
, val
= attr
->values
; i
> 0; i
--, val
++)
1353 cups_add_dconstres(dinfo
->constraints
, val
->collection
);
1356 if ((attr
= ippFindAttribute(dinfo
->attrs
, "job-resolvers-supported",
1357 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1359 for (i
= attr
->num_values
, val
= attr
->values
; i
> 0; i
--, val
++)
1360 cups_add_dconstres(dinfo
->resolvers
, val
->collection
);
1366 * 'cups_create_defaults()' - Create the -default option array.
1368 * TODO: Need to support collection defaults...
1372 cups_create_defaults(
1373 cups_dinfo_t
*dinfo
) /* I - Destination information */
1375 ipp_attribute_t
*attr
; /* Current attribute */
1376 char name
[IPP_MAX_NAME
+ 1],
1378 *nameptr
, /* Pointer into current name */
1379 value
[2048]; /* Current value */
1383 * Iterate through the printer attributes looking for xxx-default and adding
1384 * xxx=value to the defaults option array.
1387 for (attr
= ippFirstAttribute(dinfo
->attrs
);
1389 attr
= ippNextAttribute(dinfo
->attrs
))
1391 if (!attr
->name
|| attr
->group_tag
!= IPP_TAG_PRINTER
)
1394 if (attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
)
1395 continue; /* TODO: STR #4096 */
1397 if ((nameptr
= attr
->name
+ strlen(attr
->name
) - 8) <= attr
->name
||
1398 strcmp(nameptr
, "-default"))
1401 strlcpy(name
, attr
->name
, sizeof(name
));
1402 if ((nameptr
= name
+ strlen(name
) - 8) <= name
||
1403 strcmp(nameptr
, "-default"))
1408 if (ippAttributeString(attr
, value
, sizeof(value
)) >= sizeof(value
))
1411 dinfo
->num_defaults
= cupsAddOption(name
, value
, dinfo
->num_defaults
,
1418 * 'cups_create_media_db()' - Create the media database.
1422 cups_create_media_db(
1423 cups_dinfo_t
*dinfo
, /* I - Destination information */
1424 unsigned flags
) /* I - Media flags */
1426 int i
; /* Looping var */
1427 _ipp_value_t
*val
; /* Current value */
1428 ipp_attribute_t
*media_col_db
, /* media-col-database */
1429 *media_attr
, /* media-xxx */
1430 *x_dimension
, /* x-dimension */
1431 *y_dimension
; /* y-dimension */
1432 pwg_media_t
*pwg
; /* PWG media info */
1433 cups_array_t
*db
; /* New media database array */
1434 _cups_media_db_t mdb
; /* Media entry */
1437 db
= cupsArrayNew3((cups_array_func_t
)cups_compare_media_db
,
1439 (cups_acopy_func_t
)cups_copy_media_db
,
1440 (cups_afree_func_t
)cups_free_media_db
);
1442 if (flags
== CUPS_MEDIA_FLAGS_READY
)
1444 dinfo
->ready_db
= db
;
1446 media_col_db
= ippFindAttribute(dinfo
->ready_attrs
, "media-col-ready",
1447 IPP_TAG_BEGIN_COLLECTION
);
1448 media_attr
= ippFindAttribute(dinfo
->ready_attrs
, "media-ready",
1453 dinfo
->media_db
= db
;
1454 dinfo
->min_size
.width
= INT_MAX
;
1455 dinfo
->min_size
.length
= INT_MAX
;
1456 dinfo
->max_size
.width
= 0;
1457 dinfo
->max_size
.length
= 0;
1459 media_col_db
= ippFindAttribute(dinfo
->attrs
, "media-col-database",
1460 IPP_TAG_BEGIN_COLLECTION
);
1461 media_attr
= ippFindAttribute(dinfo
->attrs
, "media-supported",
1467 _ipp_value_t
*custom
= NULL
; /* Custom size range value */
1469 for (i
= media_col_db
->num_values
, val
= media_col_db
->values
;
1473 memset(&mdb
, 0, sizeof(mdb
));
1475 if ((media_attr
= ippFindAttribute(val
->collection
, "media-size",
1476 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1478 ipp_t
*media_size
= media_attr
->values
[0].collection
;
1479 /* media-size collection value */
1481 if ((x_dimension
= ippFindAttribute(media_size
, "x-dimension",
1482 IPP_TAG_INTEGER
)) != NULL
&&
1483 (y_dimension
= ippFindAttribute(media_size
, "y-dimension",
1484 IPP_TAG_INTEGER
)) != NULL
)
1490 mdb
.width
= x_dimension
->values
[0].integer
;
1491 mdb
.length
= y_dimension
->values
[0].integer
;
1493 else if ((x_dimension
= ippFindAttribute(media_size
, "x-dimension",
1494 IPP_TAG_INTEGER
)) != NULL
&&
1495 (y_dimension
= ippFindAttribute(media_size
, "y-dimension",
1496 IPP_TAG_RANGE
)) != NULL
)
1502 mdb
.width
= x_dimension
->values
[0].integer
;
1503 mdb
.length
= y_dimension
->values
[0].range
.upper
;
1505 else if (flags
!= CUPS_MEDIA_FLAGS_READY
&&
1506 (x_dimension
= ippFindAttribute(media_size
, "x-dimension",
1507 IPP_TAG_RANGE
)) != NULL
&&
1508 (y_dimension
= ippFindAttribute(media_size
, "y-dimension",
1509 IPP_TAG_RANGE
)) != NULL
)
1512 * Custom size range; save this as the custom size value with default
1513 * margins, then continue; we'll capture the real margins below...
1518 dinfo
->min_size
.width
= x_dimension
->values
[0].range
.lower
;
1519 dinfo
->min_size
.length
= y_dimension
->values
[0].range
.lower
;
1520 dinfo
->min_size
.left
=
1521 dinfo
->min_size
.right
= 635; /* Default 1/4" side margins */
1522 dinfo
->min_size
.top
=
1523 dinfo
->min_size
.bottom
= 1270; /* Default 1/2" top/bottom margins */
1525 dinfo
->max_size
.width
= x_dimension
->values
[0].range
.upper
;
1526 dinfo
->max_size
.length
= y_dimension
->values
[0].range
.upper
;
1527 dinfo
->max_size
.left
=
1528 dinfo
->max_size
.right
= 635; /* Default 1/4" side margins */
1529 dinfo
->max_size
.top
=
1530 dinfo
->max_size
.bottom
= 1270; /* Default 1/2" top/bottom margins */
1535 if ((media_attr
= ippFindAttribute(val
->collection
, "media-color",
1536 IPP_TAG_ZERO
)) != NULL
&&
1537 (media_attr
->value_tag
== IPP_TAG_NAME
||
1538 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
1539 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
1540 mdb
.color
= media_attr
->values
[0].string
.text
;
1542 if ((media_attr
= ippFindAttribute(val
->collection
, "media-info",
1543 IPP_TAG_TEXT
)) != NULL
)
1544 mdb
.info
= media_attr
->values
[0].string
.text
;
1546 if ((media_attr
= ippFindAttribute(val
->collection
, "media-key",
1547 IPP_TAG_ZERO
)) != NULL
&&
1548 (media_attr
->value_tag
== IPP_TAG_NAME
||
1549 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
1550 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
1551 mdb
.key
= media_attr
->values
[0].string
.text
;
1553 if ((media_attr
= ippFindAttribute(val
->collection
, "media-size-name",
1554 IPP_TAG_ZERO
)) != NULL
&&
1555 (media_attr
->value_tag
== IPP_TAG_NAME
||
1556 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
1557 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
1558 mdb
.size_name
= media_attr
->values
[0].string
.text
;
1560 if ((media_attr
= ippFindAttribute(val
->collection
, "media-source",
1561 IPP_TAG_ZERO
)) != NULL
&&
1562 (media_attr
->value_tag
== IPP_TAG_NAME
||
1563 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
1564 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
1565 mdb
.source
= media_attr
->values
[0].string
.text
;
1567 if ((media_attr
= ippFindAttribute(val
->collection
, "media-type",
1568 IPP_TAG_ZERO
)) != NULL
&&
1569 (media_attr
->value_tag
== IPP_TAG_NAME
||
1570 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
1571 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
1572 mdb
.type
= media_attr
->values
[0].string
.text
;
1574 if ((media_attr
= ippFindAttribute(val
->collection
, "media-bottom-margin",
1575 IPP_TAG_INTEGER
)) != NULL
)
1576 mdb
.bottom
= media_attr
->values
[0].integer
;
1578 if ((media_attr
= ippFindAttribute(val
->collection
, "media-left-margin",
1579 IPP_TAG_INTEGER
)) != NULL
)
1580 mdb
.left
= media_attr
->values
[0].integer
;
1582 if ((media_attr
= ippFindAttribute(val
->collection
, "media-right-margin",
1583 IPP_TAG_INTEGER
)) != NULL
)
1584 mdb
.right
= media_attr
->values
[0].integer
;
1586 if ((media_attr
= ippFindAttribute(val
->collection
, "media-top-margin",
1587 IPP_TAG_INTEGER
)) != NULL
)
1588 mdb
.top
= media_attr
->values
[0].integer
;
1590 cupsArrayAdd(db
, &mdb
);
1595 if ((media_attr
= ippFindAttribute(custom
->collection
,
1596 "media-bottom-margin",
1597 IPP_TAG_INTEGER
)) != NULL
)
1599 dinfo
->min_size
.top
=
1600 dinfo
->max_size
.top
= media_attr
->values
[0].integer
;
1603 if ((media_attr
= ippFindAttribute(custom
->collection
,
1604 "media-left-margin",
1605 IPP_TAG_INTEGER
)) != NULL
)
1607 dinfo
->min_size
.left
=
1608 dinfo
->max_size
.left
= media_attr
->values
[0].integer
;
1611 if ((media_attr
= ippFindAttribute(custom
->collection
,
1612 "media-right-margin",
1613 IPP_TAG_INTEGER
)) != NULL
)
1615 dinfo
->min_size
.right
=
1616 dinfo
->max_size
.right
= media_attr
->values
[0].integer
;
1619 if ((media_attr
= ippFindAttribute(custom
->collection
,
1621 IPP_TAG_INTEGER
)) != NULL
)
1623 dinfo
->min_size
.top
=
1624 dinfo
->max_size
.top
= media_attr
->values
[0].integer
;
1628 else if (media_attr
&&
1629 (media_attr
->value_tag
== IPP_TAG_NAME
||
1630 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
1631 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
1633 memset(&mdb
, 0, sizeof(mdb
));
1636 mdb
.right
= 635; /* Default 1/4" side margins */
1638 mdb
.bottom
= 1270; /* Default 1/2" top/bottom margins */
1640 for (i
= media_attr
->num_values
, val
= media_attr
->values
;
1644 if ((pwg
= pwgMediaForPWG(val
->string
.text
)) == NULL
)
1645 if ((pwg
= pwgMediaForLegacy(val
->string
.text
)) == NULL
)
1647 DEBUG_printf(("3cups_create_media_db: Ignoring unknown size '%s'.",
1652 mdb
.width
= pwg
->width
;
1653 mdb
.length
= pwg
->length
;
1655 if (flags
!= CUPS_MEDIA_FLAGS_READY
&&
1656 !strncmp(val
->string
.text
, "custom_min_", 11))
1658 mdb
.size_name
= NULL
;
1659 dinfo
->min_size
= mdb
;
1661 else if (flags
!= CUPS_MEDIA_FLAGS_READY
&&
1662 !strncmp(val
->string
.text
, "custom_max_", 11))
1664 mdb
.size_name
= NULL
;
1665 dinfo
->max_size
= mdb
;
1669 mdb
.size_name
= val
->string
.text
;
1671 cupsArrayAdd(db
, &mdb
);
1679 * 'cups_free_media_cb()' - Free a media entry.
1684 _cups_media_db_t
*mdb
) /* I - Media entry to free */
1687 _cupsStrFree(mdb
->color
);
1689 _cupsStrFree(mdb
->key
);
1691 _cupsStrFree(mdb
->info
);
1693 _cupsStrFree(mdb
->size_name
);
1695 _cupsStrFree(mdb
->source
);
1697 _cupsStrFree(mdb
->type
);
1704 * 'cups_get_media_db()' - Lookup the media entry for a given size.
1707 static int /* O - 1 on match, 0 on failure */
1708 cups_get_media_db(http_t
*http
, /* I - Connection to destination */
1709 cups_dinfo_t
*dinfo
, /* I - Destination information */
1710 pwg_media_t
*pwg
, /* I - PWG media info */
1711 unsigned flags
, /* I - Media matching flags */
1712 cups_size_t
*size
) /* O - Media size/margin/name info */
1714 cups_array_t
*db
; /* Which media database to query */
1715 _cups_media_db_t
*mdb
, /* Current media database entry */
1716 *best
= NULL
, /* Best matching entry */
1717 key
; /* Search key */
1721 * Create the media database as needed...
1724 if (flags
& CUPS_MEDIA_FLAGS_READY
)
1726 cups_update_ready(http
, dinfo
);
1727 db
= dinfo
->ready_db
;
1731 if (!dinfo
->media_db
)
1732 cups_create_media_db(dinfo
, CUPS_MEDIA_FLAGS_DEFAULT
);
1734 db
= dinfo
->media_db
;
1741 memset(&key
, 0, sizeof(key
));
1742 key
.width
= pwg
->width
;
1743 key
.length
= pwg
->length
;
1745 if ((mdb
= cupsArrayFind(db
, &key
)) != NULL
)
1748 * Found an exact match, let's figure out the best margins for the flags
1754 if (flags
& CUPS_MEDIA_FLAGS_BORDERLESS
)
1757 * Look for the smallest margins...
1760 if (best
->left
!= 0 || best
->right
!= 0 || best
->top
!= 0 || best
->bottom
!= 0)
1762 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
1763 mdb
&& !cups_compare_media_db(mdb
, &key
);
1764 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1766 if (mdb
->left
<= best
->left
&& mdb
->right
<= best
->right
&&
1767 mdb
->top
<= best
->top
&& mdb
->bottom
<= best
->bottom
)
1770 if (mdb
->left
== 0 && mdb
->right
== 0 && mdb
->bottom
== 0 &&
1778 * If we need an exact match, return no-match if the size is not
1782 if ((flags
& CUPS_MEDIA_FLAGS_EXACT
) &&
1783 (best
->left
|| best
->right
|| best
->top
|| best
->bottom
))
1786 else if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
1789 * Look for the largest margins...
1792 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
1793 mdb
&& !cups_compare_media_db(mdb
, &key
);
1794 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1796 if (mdb
->left
>= best
->left
&& mdb
->right
>= best
->right
&&
1797 mdb
->top
>= best
->top
&& mdb
->bottom
>= best
->bottom
&&
1798 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
1805 * Look for the smallest non-zero margins...
1808 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
1809 mdb
&& !cups_compare_media_db(mdb
, &key
);
1810 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1812 if (((mdb
->left
> 0 && mdb
->left
<= best
->left
) || best
->left
== 0) &&
1813 ((mdb
->right
> 0 && mdb
->right
<= best
->right
) || best
->right
== 0) &&
1814 ((mdb
->top
> 0 && mdb
->top
<= best
->top
) || best
->top
== 0) &&
1815 ((mdb
->bottom
> 0 && mdb
->bottom
<= best
->bottom
) || best
->bottom
== 0) &&
1816 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
1821 else if (flags
& CUPS_MEDIA_FLAGS_EXACT
)
1824 * See if we can do this as a custom size...
1827 if (pwg
->width
< dinfo
->min_size
.width
||
1828 pwg
->width
> dinfo
->max_size
.width
||
1829 pwg
->length
< dinfo
->min_size
.length
||
1830 pwg
->length
> dinfo
->max_size
.length
)
1831 return (0); /* Out of range */
1833 if ((flags
& CUPS_MEDIA_FLAGS_BORDERLESS
) &&
1834 (dinfo
->min_size
.left
> 0 || dinfo
->min_size
.right
> 0 ||
1835 dinfo
->min_size
.top
> 0 || dinfo
->min_size
.bottom
> 0))
1836 return (0); /* Not borderless */
1838 key
.size_name
= (char *)pwg
->pwg
;
1839 key
.bottom
= dinfo
->min_size
.bottom
;
1840 key
.left
= dinfo
->min_size
.left
;
1841 key
.right
= dinfo
->min_size
.right
;
1842 key
.top
= dinfo
->min_size
.top
;
1846 else if (pwg
->width
>= dinfo
->min_size
.width
&&
1847 pwg
->width
<= dinfo
->max_size
.width
&&
1848 pwg
->length
>= dinfo
->min_size
.length
&&
1849 pwg
->length
<= dinfo
->max_size
.length
)
1852 * Map to custom size...
1855 key
.size_name
= (char *)pwg
->pwg
;
1856 key
.bottom
= dinfo
->min_size
.bottom
;
1857 key
.left
= dinfo
->min_size
.left
;
1858 key
.right
= dinfo
->min_size
.right
;
1859 key
.top
= dinfo
->min_size
.top
;
1866 * Find a close size...
1869 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
);
1871 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1872 if (cups_is_close_media_db(mdb
, &key
))
1880 if (flags
& CUPS_MEDIA_FLAGS_BORDERLESS
)
1883 * Look for the smallest margins...
1886 if (best
->left
!= 0 || best
->right
!= 0 || best
->top
!= 0 ||
1889 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
1890 mdb
&& cups_is_close_media_db(mdb
, &key
);
1891 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1893 if (mdb
->left
<= best
->left
&& mdb
->right
<= best
->right
&&
1894 mdb
->top
<= best
->top
&& mdb
->bottom
<= best
->bottom
&&
1895 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
1898 if (mdb
->left
== 0 && mdb
->right
== 0 && mdb
->bottom
== 0 &&
1905 else if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
1908 * Look for the largest margins...
1911 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
1912 mdb
&& cups_is_close_media_db(mdb
, &key
);
1913 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1915 if (mdb
->left
>= best
->left
&& mdb
->right
>= best
->right
&&
1916 mdb
->top
>= best
->top
&& mdb
->bottom
>= best
->bottom
&&
1917 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
1924 * Look for the smallest non-zero margins...
1927 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
1928 mdb
&& cups_is_close_media_db(mdb
, &key
);
1929 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1931 if (((mdb
->left
> 0 && mdb
->left
<= best
->left
) || best
->left
== 0) &&
1932 ((mdb
->right
> 0 && mdb
->right
<= best
->right
) ||
1933 best
->right
== 0) &&
1934 ((mdb
->top
> 0 && mdb
->top
<= best
->top
) || best
->top
== 0) &&
1935 ((mdb
->bottom
> 0 && mdb
->bottom
<= best
->bottom
) ||
1936 best
->bottom
== 0) &&
1937 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
1946 * Return the matching size...
1949 if (best
->size_name
)
1950 strlcpy(size
->media
, best
->size_name
, sizeof(size
->media
));
1952 strlcpy(size
->media
, best
->key
, sizeof(size
->media
));
1954 strlcpy(size
->media
, pwg
->pwg
, sizeof(size
->media
));
1956 size
->width
= best
->width
;
1957 size
->length
= best
->length
;
1958 size
->bottom
= best
->bottom
;
1959 size
->left
= best
->left
;
1960 size
->right
= best
->right
;
1961 size
->top
= best
->top
;
1971 * 'cups_is_close_media_db()' - Compare two media entries to see if they are
1972 * close to the same size.
1974 * Currently we use 5 points (from PostScript) as the matching range...
1977 static int /* O - 1 if the sizes are close */
1978 cups_is_close_media_db(
1979 _cups_media_db_t
*a
, /* I - First media entries */
1980 _cups_media_db_t
*b
) /* I - Second media entries */
1982 int dwidth
, /* Difference in width */
1983 dlength
; /* Difference in length */
1986 dwidth
= a
->width
- b
->width
;
1987 dlength
= a
->length
- b
->length
;
1989 return (dwidth
>= -176 && dwidth
<= 176 &&
1990 dlength
>= -176 && dlength
<= 176);
1995 * 'cups_test_constraints()' - Test constraints.
1997 * TODO: STR #4096 - Need to properly support media-col contraints...
2000 static cups_array_t
* /* O - Active constraints */
2001 cups_test_constraints(
2002 cups_dinfo_t
*dinfo
, /* I - Destination information */
2003 const char *new_option
, /* I - Newly selected option */
2004 const char *new_value
, /* I - Newly selected value */
2005 int num_options
, /* I - Number of options */
2006 cups_option_t
*options
, /* I - Options */
2007 int *num_conflicts
, /* O - Number of conflicting options */
2008 cups_option_t
**conflicts
) /* O - Conflicting options */
2010 int i
, /* Looping var */
2011 match
; /* Value matches? */
2012 int num_matching
; /* Number of matching options */
2013 cups_option_t
*matching
; /* Matching options */
2014 _cups_dconstres_t
*c
; /* Current constraint */
2015 cups_array_t
*active
= NULL
; /* Active constraints */
2016 ipp_attribute_t
*attr
; /* Current attribute */
2017 _ipp_value_t
*attrval
; /* Current attribute value */
2018 const char *value
; /* Current value */
2019 char temp
[1024]; /* Temporary string */
2020 int int_value
; /* Integer value */
2021 int xres_value
, /* Horizontal resolution */
2022 yres_value
; /* Vertical resolution */
2023 ipp_res_t units_value
; /* Resolution units */
2026 for (c
= (_cups_dconstres_t
*)cupsArrayFirst(dinfo
->constraints
);
2028 c
= (_cups_dconstres_t
*)cupsArrayNext(dinfo
->constraints
))
2033 for (attr
= ippFirstAttribute(c
->collection
);
2035 attr
= ippNextAttribute(c
->collection
))
2037 if (attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
)
2038 break; /* TODO: STR #4096 */
2041 * Get the value for the current attribute in the constraint...
2044 if (new_option
&& new_value
&& !strcmp(attr
->name
, new_option
))
2046 else if ((value
= cupsGetOption(attr
->name
, num_options
,
2048 value
= cupsGetOption(attr
->name
, dinfo
->num_defaults
, dinfo
->defaults
);
2053 * Not set so this constraint does not apply...
2061 switch (attr
->value_tag
)
2063 case IPP_TAG_INTEGER
:
2065 int_value
= atoi(value
);
2067 for (i
= attr
->num_values
, attrval
= attr
->values
;
2071 if (attrval
->integer
== int_value
)
2079 case IPP_TAG_BOOLEAN
:
2080 int_value
= !strcmp(value
, "true");
2082 for (i
= attr
->num_values
, attrval
= attr
->values
;
2086 if (attrval
->boolean
== int_value
)
2094 case IPP_TAG_RANGE
:
2095 int_value
= atoi(value
);
2097 for (i
= attr
->num_values
, attrval
= attr
->values
;
2101 if (int_value
>= attrval
->range
.lower
&&
2102 int_value
<= attrval
->range
.upper
)
2110 case IPP_TAG_RESOLUTION
:
2111 if (sscanf(value
, "%dx%d%15s", &xres_value
, &yres_value
, temp
) != 3)
2113 if (sscanf(value
, "%d%15s", &xres_value
, temp
) != 2)
2116 yres_value
= xres_value
;
2119 if (!strcmp(temp
, "dpi"))
2120 units_value
= IPP_RES_PER_INCH
;
2121 else if (!strcmp(temp
, "dpc") || !strcmp(temp
, "dpcm"))
2122 units_value
= IPP_RES_PER_CM
;
2126 for (i
= attr
->num_values
, attrval
= attr
->values
;
2130 if (attrval
->resolution
.xres
== xres_value
&&
2131 attrval
->resolution
.yres
== yres_value
&&
2132 attrval
->resolution
.units
== units_value
)
2142 case IPP_TAG_KEYWORD
:
2143 case IPP_TAG_CHARSET
:
2145 case IPP_TAG_URISCHEME
:
2146 case IPP_TAG_MIMETYPE
:
2147 case IPP_TAG_LANGUAGE
:
2148 case IPP_TAG_TEXTLANG
:
2149 case IPP_TAG_NAMELANG
:
2150 for (i
= attr
->num_values
, attrval
= attr
->values
;
2154 if (!strcmp(attrval
->string
.text
, value
))
2169 num_matching
= cupsAddOption(attr
->name
, value
, num_matching
, &matching
);
2175 active
= cupsArrayNew(NULL
, NULL
);
2177 cupsArrayAdd(active
, c
);
2179 if (num_conflicts
&& conflicts
)
2181 cups_option_t
*moption
; /* Matching option */
2183 for (i
= num_matching
, moption
= matching
; i
> 0; i
--, moption
++)
2184 *num_conflicts
= cupsAddOption(moption
->name
, moption
->value
,
2185 *num_conflicts
, conflicts
);
2189 cupsFreeOptions(num_matching
, matching
);
2197 * 'cups_update_ready()' - Update xxx-ready attributes for the printer.
2201 cups_update_ready(http_t
*http
, /* I - Connection to destination */
2202 cups_dinfo_t
*dinfo
) /* I - Destination information */
2204 ipp_t
*request
; /* Get-Printer-Attributes request */
2205 static const char * const pattrs
[] = /* Printer attributes we want */
2207 "finishings-col-ready",
2209 "job-finishings-col-ready",
2210 "job-finishings-ready",
2217 * Don't update more than once every 30 seconds...
2220 if ((time(NULL
) - dinfo
->ready_time
) < _CUPS_MEDIA_READY_TTL
)
2224 * Free any previous results...
2227 if (dinfo
->cached_flags
& CUPS_MEDIA_FLAGS_READY
)
2229 cupsArrayDelete(dinfo
->cached_db
);
2230 dinfo
->cached_db
= NULL
;
2231 dinfo
->cached_flags
= CUPS_MEDIA_FLAGS_DEFAULT
;
2234 ippDelete(dinfo
->ready_attrs
);
2235 dinfo
->ready_attrs
= NULL
;
2237 cupsArrayDelete(dinfo
->ready_db
);
2238 dinfo
->ready_db
= NULL
;
2241 * Query the xxx-ready values...
2244 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
2245 ippSetVersion(request
, dinfo
->version
/ 10, dinfo
->version
% 10);
2247 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
2249 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
2251 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "requested-attributes", (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
2253 dinfo
->ready_attrs
= cupsDoRequest(http
, request
, dinfo
->resource
);
2256 * Update the ready media database...
2259 cups_create_media_db(dinfo
, CUPS_MEDIA_FLAGS_READY
);
2262 * Update last lookup time and return...
2265 dinfo
->ready_time
= time(NULL
);