2 * Destination option/media support for CUPS.
4 * Copyright 2012-2017 by Apple Inc.
6 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
10 * Include necessary headers...
13 #include "cups-private.h"
20 #define _CUPS_MEDIA_READY_TTL 30 /* Life of xxx-ready values */
27 static void cups_add_dconstres(cups_array_t
*a
, ipp_t
*collection
);
28 static int cups_collection_contains(ipp_t
*test
, ipp_t
*match
);
29 static size_t cups_collection_string(ipp_attribute_t
*attr
, char *buffer
, size_t bufsize
);
30 static int cups_compare_dconstres(_cups_dconstres_t
*a
,
31 _cups_dconstres_t
*b
);
32 static int cups_compare_media_db(_cups_media_db_t
*a
,
34 static _cups_media_db_t
*cups_copy_media_db(_cups_media_db_t
*mdb
);
35 static void cups_create_cached(http_t
*http
, cups_dinfo_t
*dinfo
,
37 static void cups_create_constraints(cups_dinfo_t
*dinfo
);
38 static void cups_create_defaults(cups_dinfo_t
*dinfo
);
39 static void cups_create_media_db(cups_dinfo_t
*dinfo
,
41 static void cups_free_media_db(_cups_media_db_t
*mdb
);
42 static int cups_get_media_db(http_t
*http
, cups_dinfo_t
*dinfo
,
43 pwg_media_t
*pwg
, unsigned flags
,
45 static int cups_is_close_media_db(_cups_media_db_t
*a
,
47 static cups_array_t
*cups_test_constraints(cups_dinfo_t
*dinfo
,
48 const char *new_option
,
49 const char *new_value
,
51 cups_option_t
*options
,
53 cups_option_t
**conflicts
);
54 static void cups_update_ready(http_t
*http
, cups_dinfo_t
*dinfo
);
58 * 'cupsAddDestMediaOptions()' - Add the option corresponding to the specified media size.
63 int /* O - New number of options */
64 cupsAddDestMediaOptions(
65 http_t
*http
, /* I - Connection to destination */
66 cups_dest_t
*dest
, /* I - Destination */
67 cups_dinfo_t
*dinfo
, /* I - Destination information */
68 unsigned flags
, /* I - Media matching flags */
69 cups_size_t
*size
, /* I - Media size */
70 int num_options
, /* I - Current number of options */
71 cups_option_t
**options
) /* IO - Options */
73 cups_array_t
*db
; /* Media database */
74 _cups_media_db_t
*mdb
; /* Media database entry */
75 char value
[2048]; /* Option value */
79 * Range check input...
82 if (!http
|| !dest
|| !dinfo
|| !size
|| !options
)
84 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
89 * Find the matching media size...
92 if (flags
& CUPS_MEDIA_FLAGS_READY
)
97 DEBUG_printf(("1cupsAddDestMediaOptions: size->media=\"%s\"", size
->media
));
99 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
); mdb
; mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
101 if (mdb
->key
&& !strcmp(mdb
->key
, size
->media
))
103 else if (mdb
->size_name
&& !strcmp(mdb
->size_name
, size
->media
))
109 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
); mdb
; mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
111 if (mdb
->width
== size
->width
&& mdb
->length
== size
->length
&& mdb
->bottom
== size
->bottom
&& mdb
->left
== size
->left
&& mdb
->right
== size
->right
&& mdb
->top
== size
->top
)
118 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
); mdb
; mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
120 if (mdb
->width
== size
->width
&& mdb
->length
== size
->length
)
127 DEBUG_puts("1cupsAddDestMediaOptions: Unable to find matching size.");
128 return (num_options
);
131 DEBUG_printf(("1cupsAddDestMediaOptions: MATCH mdb%p [key=\"%s\" size_name=\"%s\" source=\"%s\" type=\"%s\" width=%d length=%d B%d L%d R%d T%d]", (void *)mdb
, mdb
->key
, mdb
->size_name
, mdb
->source
, mdb
->type
, mdb
->width
, mdb
->length
, mdb
->bottom
, mdb
->left
, mdb
->right
, mdb
->top
));
136 snprintf(value
, sizeof(value
), "{media-size={x-dimension=%d y-dimension=%d} media-bottom-margin=%d media-left-margin=%d media-right-margin=%d media-top-margin=%d media-source=\"%s\" media-type=\"%s\"}", mdb
->width
, mdb
->length
, mdb
->bottom
, mdb
->left
, mdb
->right
, mdb
->top
, mdb
->source
, mdb
->type
);
138 snprintf(value
, sizeof(value
), "{media-size={x-dimension=%d y-dimension=%d} media-bottom-margin=%d media-left-margin=%d media-right-margin=%d media-top-margin=%d media-source=\"%s\"}", mdb
->width
, mdb
->length
, mdb
->bottom
, mdb
->left
, mdb
->right
, mdb
->top
, mdb
->source
);
142 snprintf(value
, sizeof(value
), "{media-size={x-dimension=%d y-dimension=%d} media-bottom-margin=%d media-left-margin=%d media-right-margin=%d media-top-margin=%d media-type=\"%s\"}", mdb
->width
, mdb
->length
, mdb
->bottom
, mdb
->left
, mdb
->right
, mdb
->top
, mdb
->type
);
146 snprintf(value
, sizeof(value
), "{media-size={x-dimension=%d y-dimension=%d} media-bottom-margin=%d media-left-margin=%d media-right-margin=%d media-top-margin=%d}", mdb
->width
, mdb
->length
, mdb
->bottom
, mdb
->left
, mdb
->right
, mdb
->top
);
149 num_options
= cupsAddOption("media-col", value
, num_options
, options
);
151 return (num_options
);
156 * 'cupsCheckDestSupported()' - Check that the option and value are supported
157 * by the destination.
159 * Returns 1 if supported, 0 otherwise.
161 * @since CUPS 1.6/macOS 10.8@
164 int /* O - 1 if supported, 0 otherwise */
165 cupsCheckDestSupported(
166 http_t
*http
, /* I - Connection to destination */
167 cups_dest_t
*dest
, /* I - Destination */
168 cups_dinfo_t
*dinfo
, /* I - Destination information */
169 const char *option
, /* I - Option */
170 const char *value
) /* I - Value or @code NULL@ */
172 int i
; /* Looping var */
173 char temp
[1024]; /* Temporary string */
174 int int_value
; /* Integer value */
175 int xres_value
, /* Horizontal resolution */
176 yres_value
; /* Vertical resolution */
177 ipp_res_t units_value
; /* Resolution units */
178 ipp_attribute_t
*attr
; /* Attribute */
179 _ipp_value_t
*attrval
; /* Current attribute value */
183 * Get the default connection as needed...
187 http
= _cupsConnect();
190 * Range check input...
193 if (!http
|| !dest
|| !dinfo
|| !option
)
197 * Lookup the attribute...
200 if (strstr(option
, "-supported"))
201 attr
= ippFindAttribute(dinfo
->attrs
, option
, IPP_TAG_ZERO
);
204 snprintf(temp
, sizeof(temp
), "%s-supported", option
);
205 attr
= ippFindAttribute(dinfo
->attrs
, temp
, IPP_TAG_ZERO
);
218 if (!strcmp(option
, "media") && !strncmp(value
, "custom_", 7))
221 * Check range of custom media sizes...
224 pwg_media_t
*pwg
; /* Current PWG media size info */
225 int min_width
, /* Minimum width */
226 min_length
, /* Minimum length */
227 max_width
, /* Maximum width */
228 max_length
; /* Maximum length */
231 * Get the minimum and maximum size...
234 min_width
= min_length
= INT_MAX
;
235 max_width
= max_length
= 0;
237 for (i
= attr
->num_values
, attrval
= attr
->values
;
241 if (!strncmp(attrval
->string
.text
, "custom_min_", 11) &&
242 (pwg
= pwgMediaForPWG(attrval
->string
.text
)) != NULL
)
244 min_width
= pwg
->width
;
245 min_length
= pwg
->length
;
247 else if (!strncmp(attrval
->string
.text
, "custom_max_", 11) &&
248 (pwg
= pwgMediaForPWG(attrval
->string
.text
)) != NULL
)
250 max_width
= pwg
->width
;
251 max_length
= pwg
->length
;
259 if (min_width
< INT_MAX
&& max_width
> 0 &&
260 (pwg
= pwgMediaForPWG(value
)) != NULL
&&
261 pwg
->width
>= min_width
&& pwg
->width
<= max_width
&&
262 pwg
->length
>= min_length
&& pwg
->length
<= max_length
)
268 * Check literal values...
271 switch (attr
->value_tag
)
273 case IPP_TAG_INTEGER
:
275 int_value
= atoi(value
);
277 for (i
= 0; i
< attr
->num_values
; i
++)
278 if (attr
->values
[i
].integer
== int_value
)
282 case IPP_TAG_BOOLEAN
:
283 return (attr
->values
[0].boolean
);
286 int_value
= atoi(value
);
288 for (i
= 0; i
< attr
->num_values
; i
++)
289 if (int_value
>= attr
->values
[i
].range
.lower
&&
290 int_value
<= attr
->values
[i
].range
.upper
)
294 case IPP_TAG_RESOLUTION
:
295 if (sscanf(value
, "%dx%d%15s", &xres_value
, &yres_value
, temp
) != 3)
297 if (sscanf(value
, "%d%15s", &xres_value
, temp
) != 2)
300 yres_value
= xres_value
;
303 if (!strcmp(temp
, "dpi"))
304 units_value
= IPP_RES_PER_INCH
;
305 else if (!strcmp(temp
, "dpc") || !strcmp(temp
, "dpcm"))
306 units_value
= IPP_RES_PER_CM
;
310 for (i
= attr
->num_values
, attrval
= attr
->values
;
314 if (attrval
->resolution
.xres
== xres_value
&&
315 attrval
->resolution
.yres
== yres_value
&&
316 attrval
->resolution
.units
== units_value
)
323 case IPP_TAG_KEYWORD
:
324 case IPP_TAG_CHARSET
:
326 case IPP_TAG_URISCHEME
:
327 case IPP_TAG_MIMETYPE
:
328 case IPP_TAG_LANGUAGE
:
329 case IPP_TAG_TEXTLANG
:
330 case IPP_TAG_NAMELANG
:
331 for (i
= 0; i
< attr
->num_values
; i
++)
332 if (!strcmp(attr
->values
[i
].string
.text
, value
))
342 * If we get there the option+value is not supported...
350 * 'cupsCopyDestConflicts()' - Get conflicts and resolutions for a new
353 * "num_options" and "options" represent the currently selected options by the
354 * user. "new_option" and "new_value" are the setting the user has just
357 * Returns 1 if there is a conflict, 0 if there are no conflicts, and -1 if
358 * there was an unrecoverable error such as a resolver loop.
360 * If "num_conflicts" and "conflicts" are not @code NULL@, they are set to
361 * contain the list of conflicting option/value pairs. Similarly, if
362 * "num_resolved" and "resolved" are not @code NULL@ they will be set to the
363 * list of changes needed to resolve the conflict.
365 * If cupsCopyDestConflicts returns 1 but "num_resolved" and "resolved" are set
366 * to 0 and @code NULL@, respectively, then the conflict cannot be resolved.
368 * @since CUPS 1.6/macOS 10.8@
371 int /* O - 1 if there is a conflict, 0 if none, -1 on error */
372 cupsCopyDestConflicts(
373 http_t
*http
, /* I - Connection to destination */
374 cups_dest_t
*dest
, /* I - Destination */
375 cups_dinfo_t
*dinfo
, /* I - Destination information */
376 int num_options
, /* I - Number of current options */
377 cups_option_t
*options
, /* I - Current options */
378 const char *new_option
, /* I - New option */
379 const char *new_value
, /* I - New value */
380 int *num_conflicts
, /* O - Number of conflicting options */
381 cups_option_t
**conflicts
, /* O - Conflicting options */
382 int *num_resolved
, /* O - Number of options to resolve */
383 cups_option_t
**resolved
) /* O - Resolved options */
385 int i
, /* Looping var */
386 have_conflicts
= 0, /* Do we have conflicts? */
387 changed
, /* Did we change something? */
388 tries
, /* Number of tries for resolution */
389 num_myconf
= 0, /* My number of conflicting options */
390 num_myres
= 0; /* My number of resolved options */
391 cups_option_t
*myconf
= NULL
, /* My conflicting options */
392 *myres
= NULL
, /* My resolved options */
393 *myoption
, /* My current option */
394 *option
; /* Current option */
395 cups_array_t
*active
= NULL
, /* Active conflicts */
396 *pass
= NULL
, /* Resolvers for this pass */
397 *resolvers
= NULL
, /* Resolvers we have used */
398 *test
; /* Test array for conflicts */
399 _cups_dconstres_t
*c
, /* Current constraint */
400 *r
; /* Current resolver */
401 ipp_attribute_t
*attr
; /* Current attribute */
402 char value
[2048]; /* Current attribute value as string */
403 const char *myvalue
; /* Current value of an option */
407 * Clear returned values...
423 * Get the default connection as needed...
427 http
= _cupsConnect();
430 * Range check input...
433 if (!http
|| !dest
|| !dinfo
||
434 (num_conflicts
!= NULL
) != (conflicts
!= NULL
) ||
435 (num_resolved
!= NULL
) != (resolved
!= NULL
))
439 * Load constraints as needed...
442 if (!dinfo
->constraints
)
443 cups_create_constraints(dinfo
);
445 if (cupsArrayCount(dinfo
->constraints
) == 0)
448 if (!dinfo
->num_defaults
)
449 cups_create_defaults(dinfo
);
452 * If we are resolving, create a shadow array...
457 for (i
= num_options
, option
= options
; i
> 0; i
--, option
++)
458 num_myres
= cupsAddOption(option
->name
, option
->value
, num_myres
, &myres
);
460 if (new_option
&& new_value
)
461 num_myres
= cupsAddOption(new_option
, new_value
, num_myres
, &myres
);
465 num_myres
= num_options
;
470 * Check for any conflicts...
474 pass
= cupsArrayNew((cups_array_func_t
)cups_compare_dconstres
, NULL
);
476 for (tries
= 0; tries
< 100; tries
++)
479 * Check for any conflicts...
482 if (num_conflicts
|| num_resolved
)
484 cupsFreeOptions(num_myconf
, myconf
);
488 active
= cups_test_constraints(dinfo
, new_option
, new_value
,
489 num_myres
, myres
, &num_myconf
,
493 active
= cups_test_constraints(dinfo
, new_option
, new_value
, num_myres
,
496 have_conflicts
= (active
!= NULL
);
498 if (!active
|| !num_resolved
)
499 break; /* All done */
502 * Scan the constraints that were triggered to apply resolvers...
506 resolvers
= cupsArrayNew((cups_array_func_t
)cups_compare_dconstres
, NULL
);
508 for (c
= (_cups_dconstres_t
*)cupsArrayFirst(active
), changed
= 0;
510 c
= (_cups_dconstres_t
*)cupsArrayNext(active
))
512 if (cupsArrayFind(pass
, c
))
513 continue; /* Already applied this resolver... */
515 if (cupsArrayFind(resolvers
, c
))
517 DEBUG_printf(("1cupsCopyDestConflicts: Resolver loop with %s.",
523 if ((r
= cupsArrayFind(dinfo
->resolvers
, c
)) == NULL
)
525 DEBUG_printf(("1cupsCopyDestConflicts: Resolver %s not found.",
532 * Add the options from the resolver...
535 cupsArrayAdd(pass
, r
);
536 cupsArrayAdd(resolvers
, r
);
538 for (attr
= ippFirstAttribute(r
->collection
);
540 attr
= ippNextAttribute(r
->collection
))
542 if (new_option
&& !strcmp(attr
->name
, new_option
))
543 continue; /* Ignore this if we just changed it */
545 if (ippAttributeString(attr
, value
, sizeof(value
)) >= sizeof(value
))
546 continue; /* Ignore if the value is too long */
548 if ((test
= cups_test_constraints(dinfo
, attr
->name
, value
, num_myres
,
549 myres
, NULL
, NULL
)) == NULL
)
552 * That worked, flag it...
558 cupsArrayDelete(test
);
561 * Add the option/value from the resolver regardless of whether it
562 * worked; this makes sure that we can cascade several changes to
563 * make things resolve...
566 num_myres
= cupsAddOption(attr
->name
, value
, num_myres
, &myres
);
572 DEBUG_puts("1cupsCopyDestConflicts: Unable to resolve constraints.");
577 cupsArrayClear(pass
);
579 cupsArrayDelete(active
);
585 DEBUG_puts("1cupsCopyDestConflicts: Unable to resolve after 100 tries.");
591 * Copy resolved options as needed...
596 for (i
= num_myres
, myoption
= myres
; i
> 0; i
--, myoption
++)
598 if ((myvalue
= cupsGetOption(myoption
->name
, num_options
,
600 strcmp(myvalue
, myoption
->value
))
602 if (new_option
&& !strcmp(new_option
, myoption
->name
) &&
603 new_value
&& !strcmp(new_value
, myoption
->value
))
606 *num_resolved
= cupsAddOption(myoption
->name
, myoption
->value
,
607 *num_resolved
, resolved
);
618 cupsArrayDelete(active
);
619 cupsArrayDelete(pass
);
620 cupsArrayDelete(resolvers
);
625 * Free shadow copy of options...
628 cupsFreeOptions(num_myres
, myres
);
634 * Return conflicting options to caller...
637 *num_conflicts
= num_myconf
;
643 * Free conflicting options...
646 cupsFreeOptions(num_myconf
, myconf
);
649 return (have_conflicts
);
654 * 'cupsCopyDestInfo()' - Get the supported values/capabilities for the
657 * The caller is responsible for calling @link cupsFreeDestInfo@ on the return
658 * value. @code NULL@ is returned on error.
660 * @since CUPS 1.6/macOS 10.8@
663 cups_dinfo_t
* /* O - Destination information */
665 http_t
*http
, /* I - Connection to destination */
666 cups_dest_t
*dest
) /* I - Destination */
668 cups_dinfo_t
*dinfo
; /* Destination information */
669 ipp_t
*request
, /* Get-Printer-Attributes request */
670 *response
; /* Supported attributes */
671 int tries
, /* Number of tries so far */
672 delay
, /* Current retry delay */
673 prev_delay
; /* Next retry delay */
674 const char *uri
; /* Printer URI */
675 char resource
[1024]; /* Resource path */
676 int version
; /* IPP version */
677 ipp_status_t status
; /* Status of request */
678 static const char * const requested_attrs
[] =
679 { /* Requested attributes */
681 "media-col-database",
682 "printer-description"
686 DEBUG_printf(("cupsCopyDestSupported(http=%p, dest=%p(%s))", (void *)http
, (void *)dest
, dest
? dest
->name
: ""));
689 * Get the default connection as needed...
693 http
= _cupsConnect();
696 * Range check input...
703 * Get the printer URI and resource path...
706 if ((uri
= _cupsGetDestResource(dest
, resource
, sizeof(resource
))) == NULL
)
710 * Get the supported attributes...
721 * Send a Get-Printer-Attributes request...
724 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
725 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
727 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
728 "requesting-user-name", NULL
, cupsUser());
729 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
730 "requested-attributes",
731 (int)(sizeof(requested_attrs
) / sizeof(requested_attrs
[0])),
732 NULL
, requested_attrs
);
733 response
= cupsDoRequest(http
, request
, resource
);
734 status
= cupsLastError();
736 if (status
> IPP_STATUS_OK_IGNORED_OR_SUBSTITUTED
)
738 DEBUG_printf(("cupsCopyDestSupported: Get-Printer-Attributes for '%s' "
739 "returned %s (%s)", dest
->name
, ippErrorString(status
),
740 cupsLastErrorString()));
745 if (status
== IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
&& version
> 11)
747 else if (status
== IPP_STATUS_ERROR_BUSY
)
749 sleep((unsigned)delay
);
751 delay
= _cupsNextDelay(delay
, &prev_delay
);
759 while (!response
&& tries
< 10);
765 * Allocate a cups_dinfo_t structure and return it...
768 if ((dinfo
= calloc(1, sizeof(cups_dinfo_t
))) == NULL
)
770 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
775 dinfo
->version
= version
;
777 dinfo
->resource
= _cupsStrAlloc(resource
);
778 dinfo
->attrs
= response
;
785 * 'cupsFindDestDefault()' - Find the default value(s) for the given option.
787 * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
788 * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
789 * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
790 * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
791 * functions to inspect the default value(s) as needed.
793 * @since CUPS 1.7/macOS 10.9@
796 ipp_attribute_t
* /* O - Default attribute or @code NULL@ for none */
798 http_t
*http
, /* I - Connection to destination */
799 cups_dest_t
*dest
, /* I - Destination */
800 cups_dinfo_t
*dinfo
, /* I - Destination information */
801 const char *option
) /* I - Option/attribute name */
803 char name
[IPP_MAX_NAME
]; /* Attribute name */
807 * Get the default connection as needed...
811 http
= _cupsConnect();
814 * Range check input...
817 if (!http
|| !dest
|| !dinfo
|| !option
)
819 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
824 * Find and return the attribute...
827 snprintf(name
, sizeof(name
), "%s-default", option
);
828 return (ippFindAttribute(dinfo
->attrs
, name
, IPP_TAG_ZERO
));
833 * 'cupsFindDestReady()' - Find the default value(s) for the given option.
835 * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
836 * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
837 * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
838 * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
839 * functions to inspect the default value(s) as needed.
841 * @since CUPS 1.7/macOS 10.9@
844 ipp_attribute_t
* /* O - Default attribute or @code NULL@ for none */
846 http_t
*http
, /* I - Connection to destination */
847 cups_dest_t
*dest
, /* I - Destination */
848 cups_dinfo_t
*dinfo
, /* I - Destination information */
849 const char *option
) /* I - Option/attribute name */
851 char name
[IPP_MAX_NAME
]; /* Attribute name */
855 * Get the default connection as needed...
859 http
= _cupsConnect();
862 * Range check input...
865 if (!http
|| !dest
|| !dinfo
|| !option
)
867 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
872 * Find and return the attribute...
875 cups_update_ready(http
, dinfo
);
877 snprintf(name
, sizeof(name
), "%s-ready", option
);
878 return (ippFindAttribute(dinfo
->ready_attrs
, name
, IPP_TAG_ZERO
));
883 * 'cupsFindDestSupported()' - Find the default value(s) for the given option.
885 * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
886 * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
887 * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
888 * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
889 * functions to inspect the default value(s) as needed.
891 * @since CUPS 1.7/macOS 10.9@
894 ipp_attribute_t
* /* O - Default attribute or @code NULL@ for none */
895 cupsFindDestSupported(
896 http_t
*http
, /* I - Connection to destination */
897 cups_dest_t
*dest
, /* I - Destination */
898 cups_dinfo_t
*dinfo
, /* I - Destination information */
899 const char *option
) /* I - Option/attribute name */
901 char name
[IPP_MAX_NAME
]; /* Attribute name */
905 * Get the default connection as needed...
909 http
= _cupsConnect();
912 * Range check input...
915 if (!http
|| !dest
|| !dinfo
|| !option
)
917 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
922 * Find and return the attribute...
925 snprintf(name
, sizeof(name
), "%s-supported", option
);
926 return (ippFindAttribute(dinfo
->attrs
, name
, IPP_TAG_ZERO
));
931 * 'cupsFreeDestInfo()' - Free destination information obtained using
932 * @link cupsCopyDestInfo@.
934 * @since CUPS 1.6/macOS 10.8@
938 cupsFreeDestInfo(cups_dinfo_t
*dinfo
) /* I - Destination information */
941 * Range check input...
948 * Free memory and return...
951 _cupsStrFree(dinfo
->resource
);
953 cupsArrayDelete(dinfo
->constraints
);
954 cupsArrayDelete(dinfo
->resolvers
);
956 cupsArrayDelete(dinfo
->localizations
);
958 cupsArrayDelete(dinfo
->media_db
);
960 cupsArrayDelete(dinfo
->cached_db
);
962 ippDelete(dinfo
->ready_attrs
);
963 cupsArrayDelete(dinfo
->ready_db
);
965 ippDelete(dinfo
->attrs
);
972 * 'cupsGetDestMediaByIndex()' - Get a media name, dimension, and margins for a
975 * The @code flags@ parameter determines which set of media are indexed. For
976 * example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will get the Nth
977 * borderless size supported by the printer.
979 * @since CUPS 1.7/macOS 10.9@
982 int /* O - 1 on success, 0 on failure */
983 cupsGetDestMediaByIndex(
984 http_t
*http
, /* I - Connection to destination */
985 cups_dest_t
*dest
, /* I - Destination */
986 cups_dinfo_t
*dinfo
, /* I - Destination information */
987 int n
, /* I - Media size number (0-based) */
988 unsigned flags
, /* I - Media flags */
989 cups_size_t
*size
) /* O - Media size information */
991 _cups_media_db_t
*nsize
; /* Size for N */
992 pwg_media_t
*pwg
; /* PWG media name for size */
996 * Get the default connection as needed...
1000 http
= _cupsConnect();
1003 * Range check input...
1007 memset(size
, 0, sizeof(cups_size_t
));
1009 if (!http
|| !dest
|| !dinfo
|| n
< 0 || !size
)
1011 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1016 * Load media list as needed...
1019 if (flags
& CUPS_MEDIA_FLAGS_READY
)
1020 cups_update_ready(http
, dinfo
);
1022 if (!dinfo
->cached_db
|| dinfo
->cached_flags
!= flags
)
1023 cups_create_cached(http
, dinfo
, flags
);
1026 * Copy the size over and return...
1029 if ((nsize
= (_cups_media_db_t
*)cupsArrayIndex(dinfo
->cached_db
, n
)) == NULL
)
1031 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1036 strlcpy(size
->media
, nsize
->key
, sizeof(size
->media
));
1037 else if (nsize
->size_name
)
1038 strlcpy(size
->media
, nsize
->size_name
, sizeof(size
->media
));
1039 else if ((pwg
= pwgMediaForSize(nsize
->width
, nsize
->length
)) != NULL
)
1040 strlcpy(size
->media
, pwg
->pwg
, sizeof(size
->media
));
1043 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1047 size
->width
= nsize
->width
;
1048 size
->length
= nsize
->length
;
1049 size
->bottom
= nsize
->bottom
;
1050 size
->left
= nsize
->left
;
1051 size
->right
= nsize
->right
;
1052 size
->top
= nsize
->top
;
1059 * 'cupsGetDestMediaByName()' - Get media names, dimensions, and margins.
1061 * The "media" string is a PWG media name. "Flags" provides some matching
1062 * guidance (multiple flags can be combined):
1064 * CUPS_MEDIA_FLAGS_DEFAULT = find the closest size supported by the printer,
1065 * CUPS_MEDIA_FLAGS_BORDERLESS = find a borderless size,
1066 * CUPS_MEDIA_FLAGS_DUPLEX = find a size compatible with 2-sided printing,
1067 * CUPS_MEDIA_FLAGS_EXACT = find an exact match for the size, and
1068 * CUPS_MEDIA_FLAGS_READY = if the printer supports media sensing, find the
1069 * size amongst the "ready" media.
1071 * The matching result (if any) is returned in the "cups_size_t" structure.
1073 * Returns 1 when there is a match and 0 if there is not a match.
1075 * @since CUPS 1.6/macOS 10.8@
1078 int /* O - 1 on match, 0 on failure */
1079 cupsGetDestMediaByName(
1080 http_t
*http
, /* I - Connection to destination */
1081 cups_dest_t
*dest
, /* I - Destination */
1082 cups_dinfo_t
*dinfo
, /* I - Destination information */
1083 const char *media
, /* I - Media name */
1084 unsigned flags
, /* I - Media matching flags */
1085 cups_size_t
*size
) /* O - Media size information */
1087 pwg_media_t
*pwg
; /* PWG media info */
1091 * Get the default connection as needed...
1095 http
= _cupsConnect();
1098 * Range check input...
1102 memset(size
, 0, sizeof(cups_size_t
));
1104 if (!http
|| !dest
|| !dinfo
|| !media
|| !size
)
1106 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1111 * Lookup the media size name...
1114 if ((pwg
= pwgMediaForPWG(media
)) == NULL
)
1115 if ((pwg
= pwgMediaForLegacy(media
)) == NULL
)
1117 DEBUG_printf(("1cupsGetDestMediaByName: Unknown size '%s'.", media
));
1118 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown media size name."), 1);
1123 * Lookup the size...
1126 return (cups_get_media_db(http
, dinfo
, pwg
, flags
, size
));
1131 * 'cupsGetDestMediaBySize()' - Get media names, dimensions, and margins.
1133 * "Width" and "length" are the dimensions in hundredths of millimeters.
1134 * "Flags" provides some matching guidance (multiple flags can be combined):
1136 * CUPS_MEDIA_FLAGS_DEFAULT = find the closest size supported by the printer,
1137 * CUPS_MEDIA_FLAGS_BORDERLESS = find a borderless size,
1138 * CUPS_MEDIA_FLAGS_DUPLEX = find a size compatible with 2-sided printing,
1139 * CUPS_MEDIA_FLAGS_EXACT = find an exact match for the size, and
1140 * CUPS_MEDIA_FLAGS_READY = if the printer supports media sensing, find the
1141 * size amongst the "ready" media.
1143 * The matching result (if any) is returned in the "cups_size_t" structure.
1145 * Returns 1 when there is a match and 0 if there is not a match.
1147 * @since CUPS 1.6/macOS 10.8@
1150 int /* O - 1 on match, 0 on failure */
1151 cupsGetDestMediaBySize(
1152 http_t
*http
, /* I - Connection to destination */
1153 cups_dest_t
*dest
, /* I - Destination */
1154 cups_dinfo_t
*dinfo
, /* I - Destination information */
1155 int width
, /* I - Media width in hundredths of
1157 int length
, /* I - Media length in hundredths of
1159 unsigned flags
, /* I - Media matching flags */
1160 cups_size_t
*size
) /* O - Media size information */
1162 pwg_media_t
*pwg
; /* PWG media info */
1166 * Get the default connection as needed...
1170 http
= _cupsConnect();
1173 * Range check input...
1177 memset(size
, 0, sizeof(cups_size_t
));
1179 if (!http
|| !dest
|| !dinfo
|| width
<= 0 || length
<= 0 || !size
)
1181 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1186 * Lookup the media size name...
1189 if ((pwg
= pwgMediaForSize(width
, length
)) == NULL
)
1191 DEBUG_printf(("1cupsGetDestMediaBySize: Invalid size %dx%d.", width
,
1193 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Invalid media size."), 1);
1198 * Lookup the size...
1201 return (cups_get_media_db(http
, dinfo
, pwg
, flags
, size
));
1206 * 'cupsGetDestMediaCount()' - Get the number of sizes supported by a
1209 * The @code flags@ parameter determines the set of media sizes that are
1210 * counted. For example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will return
1211 * the number of borderless sizes.
1213 * @since CUPS 1.7/macOS 10.9@
1216 int /* O - Number of sizes */
1217 cupsGetDestMediaCount(
1218 http_t
*http
, /* I - Connection to destination */
1219 cups_dest_t
*dest
, /* I - Destination */
1220 cups_dinfo_t
*dinfo
, /* I - Destination information */
1221 unsigned flags
) /* I - Media flags */
1224 * Get the default connection as needed...
1228 http
= _cupsConnect();
1231 * Range check input...
1234 if (!http
|| !dest
|| !dinfo
)
1236 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1241 * Load media list as needed...
1244 if (flags
& CUPS_MEDIA_FLAGS_READY
)
1245 cups_update_ready(http
, dinfo
);
1247 if (!dinfo
->cached_db
|| dinfo
->cached_flags
!= flags
)
1248 cups_create_cached(http
, dinfo
, flags
);
1250 return (cupsArrayCount(dinfo
->cached_db
));
1255 * 'cupsGetDestMediaDefault()' - Get the default size for a destination.
1257 * The @code flags@ parameter determines which default size is returned. For
1258 * example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will return the default
1259 * borderless size, typically US Letter or A4, but sometimes 4x6 photo media.
1261 * @since CUPS 1.7/macOS 10.9@
1264 int /* O - 1 on success, 0 on failure */
1265 cupsGetDestMediaDefault(
1266 http_t
*http
, /* I - Connection to destination */
1267 cups_dest_t
*dest
, /* I - Destination */
1268 cups_dinfo_t
*dinfo
, /* I - Destination information */
1269 unsigned flags
, /* I - Media flags */
1270 cups_size_t
*size
) /* O - Media size information */
1272 const char *media
; /* Default media size */
1276 * Get the default connection as needed...
1280 http
= _cupsConnect();
1283 * Range check input...
1287 memset(size
, 0, sizeof(cups_size_t
));
1289 if (!http
|| !dest
|| !dinfo
|| !size
)
1291 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1296 * Get the default media size, if any...
1299 if ((media
= cupsGetOption("media", dest
->num_options
, dest
->options
)) == NULL
)
1300 media
= "na_letter_8.5x11in";
1302 if (cupsGetDestMediaByName(http
, dest
, dinfo
, media
, flags
, size
))
1305 if (strcmp(media
, "na_letter_8.5x11in") && cupsGetDestMediaByName(http
, dest
, dinfo
, "iso_a4_210x297mm", flags
, size
))
1308 if (strcmp(media
, "iso_a4_210x297mm") && cupsGetDestMediaByName(http
, dest
, dinfo
, "na_letter_8.5x11in", flags
, size
))
1311 if ((flags
& CUPS_MEDIA_FLAGS_BORDERLESS
) && cupsGetDestMediaByName(http
, dest
, dinfo
, "na_index_4x6in", flags
, size
))
1315 * Fall back to the first matching media size...
1318 return (cupsGetDestMediaByIndex(http
, dest
, dinfo
, 0, flags
, size
));
1323 * 'cups_add_dconstres()' - Add a constraint or resolver to an array.
1328 cups_array_t
*a
, /* I - Array */
1329 ipp_t
*collection
) /* I - Collection value */
1331 ipp_attribute_t
*attr
; /* Attribute */
1332 _cups_dconstres_t
*temp
; /* Current constraint/resolver */
1335 if ((attr
= ippFindAttribute(collection
, "resolver-name",
1336 IPP_TAG_NAME
)) == NULL
)
1339 if ((temp
= calloc(1, sizeof(_cups_dconstres_t
))) == NULL
)
1342 temp
->name
= attr
->values
[0].string
.text
;
1343 temp
->collection
= collection
;
1345 cupsArrayAdd(a
, temp
);
1350 * 'cups_collection_contains()' - Check whether test collection is contained in the matching collection.
1353 static int /* O - 1 on a match, 0 on a non-match */
1354 cups_collection_contains(ipp_t
*test
, /* I - Collection to test */
1355 ipp_t
*match
) /* I - Matching values */
1357 int i
, j
, /* Looping vars */
1358 mcount
, /* Number of match values */
1359 tcount
; /* Number of test values */
1360 ipp_attribute_t
*tattr
, /* Testing attribute */
1361 *mattr
; /* Matching attribute */
1362 const char *tval
; /* Testing string value */
1365 for (mattr
= ippFirstAttribute(match
); mattr
; mattr
= ippNextAttribute(match
))
1367 if ((tattr
= ippFindAttribute(test
, ippGetName(mattr
), IPP_TAG_ZERO
)) == NULL
)
1370 tcount
= ippGetCount(tattr
);
1372 switch (ippGetValueTag(mattr
))
1374 case IPP_TAG_INTEGER
:
1376 if (ippGetValueTag(tattr
) != ippGetValueTag(mattr
))
1379 for (i
= 0; i
< tcount
; i
++)
1381 if (!ippContainsInteger(mattr
, ippGetInteger(tattr
, i
)))
1386 case IPP_TAG_RANGE
:
1387 if (ippGetValueTag(tattr
) != IPP_TAG_INTEGER
)
1390 for (i
= 0; i
< tcount
; i
++)
1392 if (!ippContainsInteger(mattr
, ippGetInteger(tattr
, i
)))
1397 case IPP_TAG_BOOLEAN
:
1398 if (ippGetValueTag(tattr
) != IPP_TAG_BOOLEAN
|| ippGetBoolean(tattr
, 0) != ippGetBoolean(mattr
, 0))
1402 case IPP_TAG_TEXTLANG
:
1403 case IPP_TAG_NAMELANG
:
1406 case IPP_TAG_KEYWORD
:
1408 case IPP_TAG_URISCHEME
:
1409 case IPP_TAG_CHARSET
:
1410 case IPP_TAG_LANGUAGE
:
1411 case IPP_TAG_MIMETYPE
:
1412 for (i
= 0; i
< tcount
; i
++)
1414 if ((tval
= ippGetString(tattr
, i
, NULL
)) == NULL
|| !ippContainsString(mattr
, tval
))
1419 case IPP_TAG_BEGIN_COLLECTION
:
1420 for (i
= 0; i
< tcount
; i
++)
1422 ipp_t
*tcol
= ippGetCollection(tattr
, i
);
1423 /* Testing collection */
1425 for (j
= 0, mcount
= ippGetCount(mattr
); j
< mcount
; j
++)
1426 if (!cups_collection_contains(tcol
, ippGetCollection(mattr
, j
)))
1441 * 'cups_collection_string()' - Convert an IPP collection to an option string.
1444 static size_t /* O - Number of bytes needed */
1445 cups_collection_string(
1446 ipp_attribute_t
*attr
, /* I - Collection attribute */
1447 char *buffer
, /* I - String buffer */
1448 size_t bufsize
) /* I - Size of buffer */
1450 int i
, j
, /* Looping vars */
1451 count
, /* Number of collection values */
1452 mcount
; /* Number of member values */
1453 ipp_t
*col
; /* Collection */
1454 ipp_attribute_t
*first
, /* First member attribute */
1455 *member
; /* Member attribute */
1456 char *bufptr
, /* Pointer into buffer */
1457 *bufend
, /* End of buffer */
1458 temp
[100]; /* Temporary string */
1459 const char *mptr
; /* Pointer into member value */
1460 int mlen
; /* Length of octetString */
1464 bufend
= buffer
+ bufsize
- 1;
1466 for (i
= 0, count
= ippGetCount(attr
); i
< count
; i
++)
1468 col
= ippGetCollection(attr
, i
);
1472 if (bufptr
< bufend
)
1478 if (bufptr
< bufend
)
1483 for (member
= first
= ippFirstAttribute(col
); member
; member
= ippNextAttribute(col
))
1485 const char *mname
= ippGetName(member
);
1487 if (member
!= first
)
1489 if (bufptr
< bufend
)
1495 if (ippGetValueTag(member
) == IPP_TAG_BOOLEAN
)
1497 if (!ippGetBoolean(member
, 0))
1499 if (bufptr
< bufend
)
1500 strlcpy(bufptr
, "no", bufend
- bufptr
+ 1);
1504 if (bufptr
< bufend
)
1505 strlcpy(bufptr
, mname
, bufend
- bufptr
+ 1);
1506 bufptr
+= strlen(mname
);
1510 if (bufptr
< bufend
)
1511 strlcpy(bufptr
, mname
, bufend
- bufptr
+ 1);
1512 bufptr
+= strlen(mname
);
1514 if (bufptr
< bufend
)
1519 if (ippGetValueTag(member
) == IPP_TAG_BEGIN_COLLECTION
)
1522 * Convert sub-collection...
1525 bufptr
+= cups_collection_string(member
, bufptr
, bufptr
< bufend
? (size_t)(bufend
- bufptr
+ 1) : 0);
1530 * Convert simple type...
1533 for (j
= 0, mcount
= ippGetCount(member
); j
< mcount
; j
++)
1537 if (bufptr
< bufend
)
1543 switch (ippGetValueTag(member
))
1545 case IPP_TAG_INTEGER
:
1547 bufptr
+= snprintf(bufptr
, bufptr
< bufend
? (bufend
- bufptr
+ 1) : 0, "%d", ippGetInteger(member
, j
));
1550 case IPP_TAG_STRING
:
1551 if (bufptr
< bufend
)
1556 for (mptr
= (const char *)ippGetOctetString(member
, j
, &mlen
); mlen
> 0; mlen
--, mptr
++)
1558 if (*mptr
== '\"' || *mptr
== '\\')
1560 if (bufptr
< bufend
)
1566 if (bufptr
< bufend
)
1572 if (bufptr
< bufend
)
1580 unsigned year
; /* Year */
1581 const ipp_uchar_t
*date
= ippGetDate(member
, j
);
1584 year
= ((unsigned)date
[0] << 8) + (unsigned)date
[1];
1586 if (date
[9] == 0 && date
[10] == 0)
1587 snprintf(temp
, sizeof(temp
), "%04u-%02u-%02uT%02u:%02u:%02uZ", year
, date
[2], date
[3], date
[4], date
[5], date
[6]);
1589 snprintf(temp
, sizeof(temp
), "%04u-%02u-%02uT%02u:%02u:%02u%c%02u%02u", year
, date
[2], date
[3], date
[4], date
[5], date
[6], date
[8], date
[9], date
[10]);
1591 if (buffer
&& bufptr
< bufend
)
1592 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
+ 1));
1594 bufptr
+= strlen(temp
);
1598 case IPP_TAG_RESOLUTION
:
1600 int xres
, /* Horizontal resolution */
1601 yres
; /* Vertical resolution */
1602 ipp_res_t units
; /* Resolution units */
1604 xres
= ippGetResolution(member
, j
, &yres
, &units
);
1607 snprintf(temp
, sizeof(temp
), "%d%s", xres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
1609 snprintf(temp
, sizeof(temp
), "%dx%d%s", xres
, yres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
1611 if (buffer
&& bufptr
< bufend
)
1612 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
+ 1));
1614 bufptr
+= strlen(temp
);
1618 case IPP_TAG_RANGE
:
1620 int lower
, /* Lower bound */
1621 upper
; /* Upper bound */
1623 lower
= ippGetRange(member
, j
, &upper
);
1625 snprintf(temp
, sizeof(temp
), "%d-%d", lower
, upper
);
1627 if (buffer
&& bufptr
< bufend
)
1628 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
+ 1));
1630 bufptr
+= strlen(temp
);
1634 case IPP_TAG_TEXTLANG
:
1635 case IPP_TAG_NAMELANG
:
1638 case IPP_TAG_KEYWORD
:
1640 case IPP_TAG_URISCHEME
:
1641 case IPP_TAG_CHARSET
:
1642 case IPP_TAG_LANGUAGE
:
1643 case IPP_TAG_MIMETYPE
:
1644 if (bufptr
< bufend
)
1649 for (mptr
= ippGetString(member
, j
, NULL
); *mptr
; mptr
++)
1651 if (*mptr
== '\"' || *mptr
== '\\')
1653 if (bufptr
< bufend
)
1659 if (bufptr
< bufend
)
1665 if (bufptr
< bufend
)
1678 if (bufptr
< bufend
)
1685 return ((size_t)(bufptr
- buffer
+ 1));
1690 * 'cups_compare_dconstres()' - Compare to resolver entries.
1693 static int /* O - Result of comparison */
1694 cups_compare_dconstres(
1695 _cups_dconstres_t
*a
, /* I - First resolver */
1696 _cups_dconstres_t
*b
) /* I - Second resolver */
1698 return (strcmp(a
->name
, b
->name
));
1703 * 'cups_compare_media_db()' - Compare two media entries.
1706 static int /* O - Result of comparison */
1707 cups_compare_media_db(
1708 _cups_media_db_t
*a
, /* I - First media entries */
1709 _cups_media_db_t
*b
) /* I - Second media entries */
1711 int result
; /* Result of comparison */
1714 if ((result
= a
->width
- b
->width
) == 0)
1715 result
= a
->length
- b
->length
;
1722 * 'cups_copy_media_db()' - Copy a media entry.
1725 static _cups_media_db_t
* /* O - New media entry */
1727 _cups_media_db_t
*mdb
) /* I - Media entry to copy */
1729 _cups_media_db_t
*temp
; /* New media entry */
1732 if ((temp
= calloc(1, sizeof(_cups_media_db_t
))) == NULL
)
1736 temp
->color
= _cupsStrAlloc(mdb
->color
);
1738 temp
->key
= _cupsStrAlloc(mdb
->key
);
1740 temp
->info
= _cupsStrAlloc(mdb
->info
);
1742 temp
->size_name
= _cupsStrAlloc(mdb
->size_name
);
1744 temp
->source
= _cupsStrAlloc(mdb
->source
);
1746 temp
->type
= _cupsStrAlloc(mdb
->type
);
1748 temp
->width
= mdb
->width
;
1749 temp
->length
= mdb
->length
;
1750 temp
->bottom
= mdb
->bottom
;
1751 temp
->left
= mdb
->left
;
1752 temp
->right
= mdb
->right
;
1753 temp
->top
= mdb
->top
;
1760 * 'cups_create_cached()' - Create the media selection cache.
1764 cups_create_cached(http_t
*http
, /* I - Connection to destination */
1765 cups_dinfo_t
*dinfo
, /* I - Destination information */
1766 unsigned flags
) /* I - Media selection flags */
1768 cups_array_t
*db
; /* Media database array to use */
1769 _cups_media_db_t
*mdb
, /* Media database entry */
1770 *first
; /* First entry this size */
1773 DEBUG_printf(("3cups_create_cached(http=%p, dinfo=%p, flags=%u)", (void *)http
, (void *)dinfo
, flags
));
1775 if (dinfo
->cached_db
)
1776 cupsArrayDelete(dinfo
->cached_db
);
1778 dinfo
->cached_db
= cupsArrayNew(NULL
, NULL
);
1779 dinfo
->cached_flags
= flags
;
1781 if (flags
& CUPS_MEDIA_FLAGS_READY
)
1783 DEBUG_puts("4cups_create_cached: ready media");
1785 cups_update_ready(http
, dinfo
);
1786 db
= dinfo
->ready_db
;
1790 DEBUG_puts("4cups_create_cached: supported media");
1792 if (!dinfo
->media_db
)
1793 cups_create_media_db(dinfo
, CUPS_MEDIA_FLAGS_DEFAULT
);
1795 db
= dinfo
->media_db
;
1798 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
), first
= mdb
;
1800 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1802 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
));
1804 if (flags
& CUPS_MEDIA_FLAGS_BORDERLESS
)
1806 if (!mdb
->left
&& !mdb
->right
&& !mdb
->top
&& !mdb
->bottom
)
1808 DEBUG_printf(("4cups_create_cached: add %p", (void *)mdb
));
1809 cupsArrayAdd(dinfo
->cached_db
, mdb
);
1812 else if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
1814 if (first
->width
!= mdb
->width
|| first
->length
!= mdb
->length
)
1816 DEBUG_printf(("4cups_create_cached: add %p", (void *)first
));
1817 cupsArrayAdd(dinfo
->cached_db
, first
);
1820 else if (mdb
->left
>= first
->left
&& mdb
->right
>= first
->right
&& mdb
->top
>= first
->top
&& mdb
->bottom
>= first
->bottom
&&
1821 (mdb
->left
!= first
->left
|| mdb
->right
!= first
->right
|| mdb
->top
!= first
->top
|| mdb
->bottom
!= first
->bottom
))
1826 DEBUG_printf(("4cups_create_cached: add %p", (void *)mdb
));
1827 cupsArrayAdd(dinfo
->cached_db
, mdb
);
1831 if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
1833 DEBUG_printf(("4cups_create_cached: add %p", (void *)first
));
1834 cupsArrayAdd(dinfo
->cached_db
, first
);
1840 * 'cups_create_constraints()' - Create the constraints and resolvers arrays.
1844 cups_create_constraints(
1845 cups_dinfo_t
*dinfo
) /* I - Destination information */
1847 int i
; /* Looping var */
1848 ipp_attribute_t
*attr
; /* Attribute */
1849 _ipp_value_t
*val
; /* Current value */
1852 dinfo
->constraints
= cupsArrayNew3(NULL
, NULL
, NULL
, 0, NULL
,
1853 (cups_afree_func_t
)free
);
1854 dinfo
->resolvers
= cupsArrayNew3((cups_array_func_t
)cups_compare_dconstres
,
1855 NULL
, NULL
, 0, NULL
,
1856 (cups_afree_func_t
)free
);
1858 if ((attr
= ippFindAttribute(dinfo
->attrs
, "job-constraints-supported",
1859 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1861 for (i
= attr
->num_values
, val
= attr
->values
; i
> 0; i
--, val
++)
1862 cups_add_dconstres(dinfo
->constraints
, val
->collection
);
1865 if ((attr
= ippFindAttribute(dinfo
->attrs
, "job-resolvers-supported",
1866 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1868 for (i
= attr
->num_values
, val
= attr
->values
; i
> 0; i
--, val
++)
1869 cups_add_dconstres(dinfo
->resolvers
, val
->collection
);
1875 * 'cups_create_defaults()' - Create the -default option array.
1879 cups_create_defaults(
1880 cups_dinfo_t
*dinfo
) /* I - Destination information */
1882 ipp_attribute_t
*attr
; /* Current attribute */
1883 char name
[IPP_MAX_NAME
+ 1],
1885 *nameptr
, /* Pointer into current name */
1886 value
[2048]; /* Current value */
1890 * Iterate through the printer attributes looking for xxx-default and adding
1891 * xxx=value to the defaults option array.
1894 for (attr
= ippFirstAttribute(dinfo
->attrs
); attr
; attr
= ippNextAttribute(dinfo
->attrs
))
1896 if (!ippGetName(attr
) || ippGetGroupTag(attr
) != IPP_TAG_PRINTER
)
1899 strlcpy(name
, ippGetName(attr
), sizeof(name
));
1900 if ((nameptr
= name
+ strlen(name
) - 8) <= name
|| strcmp(nameptr
, "-default"))
1905 if (ippGetValueTag(attr
) == IPP_TAG_BEGIN_COLLECTION
)
1907 if (cups_collection_string(attr
, value
, sizeof(value
)) >= sizeof(value
))
1910 else if (ippAttributeString(attr
, value
, sizeof(value
)) >= sizeof(value
))
1913 dinfo
->num_defaults
= cupsAddOption(name
, value
, dinfo
->num_defaults
, &dinfo
->defaults
);
1919 * 'cups_create_media_db()' - Create the media database.
1923 cups_create_media_db(
1924 cups_dinfo_t
*dinfo
, /* I - Destination information */
1925 unsigned flags
) /* I - Media flags */
1927 int i
; /* Looping var */
1928 _ipp_value_t
*val
; /* Current value */
1929 ipp_attribute_t
*media_col_db
, /* media-col-database */
1930 *media_attr
, /* media-xxx */
1931 *x_dimension
, /* x-dimension */
1932 *y_dimension
; /* y-dimension */
1933 pwg_media_t
*pwg
; /* PWG media info */
1934 cups_array_t
*db
; /* New media database array */
1935 _cups_media_db_t mdb
; /* Media entry */
1936 char media_key
[256]; /* Synthesized media-key value */
1939 db
= cupsArrayNew3((cups_array_func_t
)cups_compare_media_db
,
1941 (cups_acopy_func_t
)cups_copy_media_db
,
1942 (cups_afree_func_t
)cups_free_media_db
);
1944 if (flags
== CUPS_MEDIA_FLAGS_READY
)
1946 dinfo
->ready_db
= db
;
1948 media_col_db
= ippFindAttribute(dinfo
->ready_attrs
, "media-col-ready",
1949 IPP_TAG_BEGIN_COLLECTION
);
1950 media_attr
= ippFindAttribute(dinfo
->ready_attrs
, "media-ready",
1955 dinfo
->media_db
= db
;
1956 dinfo
->min_size
.width
= INT_MAX
;
1957 dinfo
->min_size
.length
= INT_MAX
;
1958 dinfo
->max_size
.width
= 0;
1959 dinfo
->max_size
.length
= 0;
1961 media_col_db
= ippFindAttribute(dinfo
->attrs
, "media-col-database",
1962 IPP_TAG_BEGIN_COLLECTION
);
1963 media_attr
= ippFindAttribute(dinfo
->attrs
, "media-supported",
1969 _ipp_value_t
*custom
= NULL
; /* Custom size range value */
1971 for (i
= media_col_db
->num_values
, val
= media_col_db
->values
;
1975 memset(&mdb
, 0, sizeof(mdb
));
1977 if ((media_attr
= ippFindAttribute(val
->collection
, "media-size",
1978 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1980 ipp_t
*media_size
= media_attr
->values
[0].collection
;
1981 /* media-size collection value */
1983 if ((x_dimension
= ippFindAttribute(media_size
, "x-dimension",
1984 IPP_TAG_INTEGER
)) != NULL
&&
1985 (y_dimension
= ippFindAttribute(media_size
, "y-dimension",
1986 IPP_TAG_INTEGER
)) != NULL
)
1992 mdb
.width
= x_dimension
->values
[0].integer
;
1993 mdb
.length
= y_dimension
->values
[0].integer
;
1995 else if ((x_dimension
= ippFindAttribute(media_size
, "x-dimension",
1996 IPP_TAG_INTEGER
)) != NULL
&&
1997 (y_dimension
= ippFindAttribute(media_size
, "y-dimension",
1998 IPP_TAG_RANGE
)) != NULL
)
2004 mdb
.width
= x_dimension
->values
[0].integer
;
2005 mdb
.length
= y_dimension
->values
[0].range
.upper
;
2007 else if (flags
!= CUPS_MEDIA_FLAGS_READY
&&
2008 (x_dimension
= ippFindAttribute(media_size
, "x-dimension",
2009 IPP_TAG_RANGE
)) != NULL
&&
2010 (y_dimension
= ippFindAttribute(media_size
, "y-dimension",
2011 IPP_TAG_RANGE
)) != NULL
)
2014 * Custom size range; save this as the custom size value with default
2015 * margins, then continue; we'll capture the real margins below...
2020 dinfo
->min_size
.width
= x_dimension
->values
[0].range
.lower
;
2021 dinfo
->min_size
.length
= y_dimension
->values
[0].range
.lower
;
2022 dinfo
->min_size
.left
=
2023 dinfo
->min_size
.right
= 635; /* Default 1/4" side margins */
2024 dinfo
->min_size
.top
=
2025 dinfo
->min_size
.bottom
= 1270; /* Default 1/2" top/bottom margins */
2027 dinfo
->max_size
.width
= x_dimension
->values
[0].range
.upper
;
2028 dinfo
->max_size
.length
= y_dimension
->values
[0].range
.upper
;
2029 dinfo
->max_size
.left
=
2030 dinfo
->max_size
.right
= 635; /* Default 1/4" side margins */
2031 dinfo
->max_size
.top
=
2032 dinfo
->max_size
.bottom
= 1270; /* Default 1/2" top/bottom margins */
2037 if ((media_attr
= ippFindAttribute(val
->collection
, "media-color", IPP_TAG_ZERO
)) != NULL
&& (media_attr
->value_tag
== IPP_TAG_NAME
|| media_attr
->value_tag
== IPP_TAG_NAMELANG
|| media_attr
->value_tag
== IPP_TAG_KEYWORD
))
2038 mdb
.color
= media_attr
->values
[0].string
.text
;
2040 if ((media_attr
= ippFindAttribute(val
->collection
, "media-info", IPP_TAG_TEXT
)) != NULL
)
2041 mdb
.info
= media_attr
->values
[0].string
.text
;
2043 if ((media_attr
= ippFindAttribute(val
->collection
, "media-key", IPP_TAG_ZERO
)) != NULL
&& (media_attr
->value_tag
== IPP_TAG_NAME
|| media_attr
->value_tag
== IPP_TAG_NAMELANG
|| media_attr
->value_tag
== IPP_TAG_KEYWORD
))
2044 mdb
.key
= media_attr
->values
[0].string
.text
;
2046 if ((media_attr
= ippFindAttribute(val
->collection
, "media-size-name", IPP_TAG_ZERO
)) != NULL
&& (media_attr
->value_tag
== IPP_TAG_NAME
|| media_attr
->value_tag
== IPP_TAG_NAMELANG
|| media_attr
->value_tag
== IPP_TAG_KEYWORD
))
2047 mdb
.size_name
= media_attr
->values
[0].string
.text
;
2049 if ((media_attr
= ippFindAttribute(val
->collection
, "media-source", IPP_TAG_ZERO
)) != NULL
&& (media_attr
->value_tag
== IPP_TAG_NAME
|| media_attr
->value_tag
== IPP_TAG_NAMELANG
|| media_attr
->value_tag
== IPP_TAG_KEYWORD
))
2050 mdb
.source
= media_attr
->values
[0].string
.text
;
2052 if ((media_attr
= ippFindAttribute(val
->collection
, "media-type", IPP_TAG_ZERO
)) != NULL
&& (media_attr
->value_tag
== IPP_TAG_NAME
|| media_attr
->value_tag
== IPP_TAG_NAMELANG
|| media_attr
->value_tag
== IPP_TAG_KEYWORD
))
2053 mdb
.type
= media_attr
->values
[0].string
.text
;
2055 if ((media_attr
= ippFindAttribute(val
->collection
, "media-bottom-margin", IPP_TAG_INTEGER
)) != NULL
)
2056 mdb
.bottom
= media_attr
->values
[0].integer
;
2058 if ((media_attr
= ippFindAttribute(val
->collection
, "media-left-margin", IPP_TAG_INTEGER
)) != NULL
)
2059 mdb
.left
= media_attr
->values
[0].integer
;
2061 if ((media_attr
= ippFindAttribute(val
->collection
, "media-right-margin", IPP_TAG_INTEGER
)) != NULL
)
2062 mdb
.right
= media_attr
->values
[0].integer
;
2064 if ((media_attr
= ippFindAttribute(val
->collection
, "media-top-margin", IPP_TAG_INTEGER
)) != NULL
)
2065 mdb
.top
= media_attr
->values
[0].integer
;
2069 if (!mdb
.size_name
&& (pwg
= pwgMediaForSize(mdb
.width
, mdb
.length
)) != NULL
)
2070 mdb
.size_name
= (char *)pwg
->pwg
;
2075 * Use a CUPS-specific identifier if we don't have a size name...
2078 if (flags
& CUPS_MEDIA_FLAGS_READY
)
2079 snprintf(media_key
, sizeof(media_key
), "cups-media-ready-%d", i
+ 1);
2081 snprintf(media_key
, sizeof(media_key
), "cups-media-%d", i
+ 1);
2083 else if (mdb
.source
)
2086 * Generate key using size name, source, and type (if set)...
2090 snprintf(media_key
, sizeof(media_key
), "%s_%s_%s", mdb
.size_name
, mdb
.source
, mdb
.type
);
2092 snprintf(media_key
, sizeof(media_key
), "%s_%s", mdb
.size_name
, mdb
.source
);
2097 * Generate key using size name and type...
2100 snprintf(media_key
, sizeof(media_key
), "%s_%s", mdb
.size_name
, mdb
.type
);
2105 * Key is just the size name...
2108 strlcpy(media_key
, mdb
.size_name
, sizeof(media_key
));
2112 * Append "_borderless" for borderless media...
2115 if (!mdb
.bottom
&& !mdb
.left
&& !mdb
.right
&& !mdb
.top
)
2116 strlcat(media_key
, "_borderless", sizeof(media_key
));
2118 mdb
.key
= media_key
;
2121 DEBUG_printf(("1cups_create_media_db: Adding media: key=\"%s\", width=%d, length=%d, source=\"%s\", type=\"%s\".", mdb
.key
, mdb
.width
, mdb
.length
, mdb
.source
, mdb
.type
));
2123 cupsArrayAdd(db
, &mdb
);
2128 if ((media_attr
= ippFindAttribute(custom
->collection
,
2129 "media-bottom-margin",
2130 IPP_TAG_INTEGER
)) != NULL
)
2132 dinfo
->min_size
.top
=
2133 dinfo
->max_size
.top
= media_attr
->values
[0].integer
;
2136 if ((media_attr
= ippFindAttribute(custom
->collection
,
2137 "media-left-margin",
2138 IPP_TAG_INTEGER
)) != NULL
)
2140 dinfo
->min_size
.left
=
2141 dinfo
->max_size
.left
= media_attr
->values
[0].integer
;
2144 if ((media_attr
= ippFindAttribute(custom
->collection
,
2145 "media-right-margin",
2146 IPP_TAG_INTEGER
)) != NULL
)
2148 dinfo
->min_size
.right
=
2149 dinfo
->max_size
.right
= media_attr
->values
[0].integer
;
2152 if ((media_attr
= ippFindAttribute(custom
->collection
,
2154 IPP_TAG_INTEGER
)) != NULL
)
2156 dinfo
->min_size
.top
=
2157 dinfo
->max_size
.top
= media_attr
->values
[0].integer
;
2161 else if (media_attr
&&
2162 (media_attr
->value_tag
== IPP_TAG_NAME
||
2163 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
2164 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
2166 memset(&mdb
, 0, sizeof(mdb
));
2169 mdb
.right
= 635; /* Default 1/4" side margins */
2171 mdb
.bottom
= 1270; /* Default 1/2" top/bottom margins */
2173 for (i
= media_attr
->num_values
, val
= media_attr
->values
;
2177 if ((pwg
= pwgMediaForPWG(val
->string
.text
)) == NULL
)
2178 if ((pwg
= pwgMediaForLegacy(val
->string
.text
)) == NULL
)
2180 DEBUG_printf(("3cups_create_media_db: Ignoring unknown size '%s'.",
2185 mdb
.width
= pwg
->width
;
2186 mdb
.length
= pwg
->length
;
2188 if (flags
!= CUPS_MEDIA_FLAGS_READY
&&
2189 !strncmp(val
->string
.text
, "custom_min_", 11))
2191 mdb
.size_name
= NULL
;
2192 dinfo
->min_size
= mdb
;
2194 else if (flags
!= CUPS_MEDIA_FLAGS_READY
&&
2195 !strncmp(val
->string
.text
, "custom_max_", 11))
2197 mdb
.size_name
= NULL
;
2198 dinfo
->max_size
= mdb
;
2202 mdb
.size_name
= val
->string
.text
;
2204 cupsArrayAdd(db
, &mdb
);
2212 * 'cups_free_media_cb()' - Free a media entry.
2217 _cups_media_db_t
*mdb
) /* I - Media entry to free */
2220 _cupsStrFree(mdb
->color
);
2222 _cupsStrFree(mdb
->key
);
2224 _cupsStrFree(mdb
->info
);
2226 _cupsStrFree(mdb
->size_name
);
2228 _cupsStrFree(mdb
->source
);
2230 _cupsStrFree(mdb
->type
);
2237 * 'cups_get_media_db()' - Lookup the media entry for a given size.
2240 static int /* O - 1 on match, 0 on failure */
2241 cups_get_media_db(http_t
*http
, /* I - Connection to destination */
2242 cups_dinfo_t
*dinfo
, /* I - Destination information */
2243 pwg_media_t
*pwg
, /* I - PWG media info */
2244 unsigned flags
, /* I - Media matching flags */
2245 cups_size_t
*size
) /* O - Media size/margin/name info */
2247 cups_array_t
*db
; /* Which media database to query */
2248 _cups_media_db_t
*mdb
, /* Current media database entry */
2249 *best
= NULL
, /* Best matching entry */
2250 key
; /* Search key */
2254 * Create the media database as needed...
2257 if (flags
& CUPS_MEDIA_FLAGS_READY
)
2259 cups_update_ready(http
, dinfo
);
2260 db
= dinfo
->ready_db
;
2264 if (!dinfo
->media_db
)
2265 cups_create_media_db(dinfo
, CUPS_MEDIA_FLAGS_DEFAULT
);
2267 db
= dinfo
->media_db
;
2274 memset(&key
, 0, sizeof(key
));
2275 key
.width
= pwg
->width
;
2276 key
.length
= pwg
->length
;
2278 if ((mdb
= cupsArrayFind(db
, &key
)) != NULL
)
2281 * Found an exact match, let's figure out the best margins for the flags
2287 if (flags
& CUPS_MEDIA_FLAGS_BORDERLESS
)
2290 * Look for the smallest margins...
2293 if (best
->left
!= 0 || best
->right
!= 0 || best
->top
!= 0 || best
->bottom
!= 0)
2295 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
2296 mdb
&& !cups_compare_media_db(mdb
, &key
);
2297 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2299 if (mdb
->left
<= best
->left
&& mdb
->right
<= best
->right
&&
2300 mdb
->top
<= best
->top
&& mdb
->bottom
<= best
->bottom
)
2303 if (mdb
->left
== 0 && mdb
->right
== 0 && mdb
->bottom
== 0 &&
2311 * If we need an exact match, return no-match if the size is not
2315 if ((flags
& CUPS_MEDIA_FLAGS_EXACT
) &&
2316 (best
->left
|| best
->right
|| best
->top
|| best
->bottom
))
2319 else if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
2322 * Look for the largest margins...
2325 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
2326 mdb
&& !cups_compare_media_db(mdb
, &key
);
2327 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2329 if (mdb
->left
>= best
->left
&& mdb
->right
>= best
->right
&&
2330 mdb
->top
>= best
->top
&& mdb
->bottom
>= best
->bottom
&&
2331 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
2338 * Look for the smallest non-zero margins...
2341 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
2342 mdb
&& !cups_compare_media_db(mdb
, &key
);
2343 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2345 if (((mdb
->left
> 0 && mdb
->left
<= best
->left
) || best
->left
== 0) &&
2346 ((mdb
->right
> 0 && mdb
->right
<= best
->right
) || best
->right
== 0) &&
2347 ((mdb
->top
> 0 && mdb
->top
<= best
->top
) || best
->top
== 0) &&
2348 ((mdb
->bottom
> 0 && mdb
->bottom
<= best
->bottom
) || best
->bottom
== 0) &&
2349 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
2354 else if (flags
& CUPS_MEDIA_FLAGS_EXACT
)
2357 * See if we can do this as a custom size...
2360 if (pwg
->width
< dinfo
->min_size
.width
||
2361 pwg
->width
> dinfo
->max_size
.width
||
2362 pwg
->length
< dinfo
->min_size
.length
||
2363 pwg
->length
> dinfo
->max_size
.length
)
2364 return (0); /* Out of range */
2366 if ((flags
& CUPS_MEDIA_FLAGS_BORDERLESS
) &&
2367 (dinfo
->min_size
.left
> 0 || dinfo
->min_size
.right
> 0 ||
2368 dinfo
->min_size
.top
> 0 || dinfo
->min_size
.bottom
> 0))
2369 return (0); /* Not borderless */
2371 key
.size_name
= (char *)pwg
->pwg
;
2372 key
.bottom
= dinfo
->min_size
.bottom
;
2373 key
.left
= dinfo
->min_size
.left
;
2374 key
.right
= dinfo
->min_size
.right
;
2375 key
.top
= dinfo
->min_size
.top
;
2379 else if (pwg
->width
>= dinfo
->min_size
.width
&&
2380 pwg
->width
<= dinfo
->max_size
.width
&&
2381 pwg
->length
>= dinfo
->min_size
.length
&&
2382 pwg
->length
<= dinfo
->max_size
.length
)
2385 * Map to custom size...
2388 key
.size_name
= (char *)pwg
->pwg
;
2389 key
.bottom
= dinfo
->min_size
.bottom
;
2390 key
.left
= dinfo
->min_size
.left
;
2391 key
.right
= dinfo
->min_size
.right
;
2392 key
.top
= dinfo
->min_size
.top
;
2399 * Find a close size...
2402 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
);
2404 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2405 if (cups_is_close_media_db(mdb
, &key
))
2413 if (flags
& CUPS_MEDIA_FLAGS_BORDERLESS
)
2416 * Look for the smallest margins...
2419 if (best
->left
!= 0 || best
->right
!= 0 || best
->top
!= 0 ||
2422 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
2423 mdb
&& cups_is_close_media_db(mdb
, &key
);
2424 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2426 if (mdb
->left
<= best
->left
&& mdb
->right
<= best
->right
&&
2427 mdb
->top
<= best
->top
&& mdb
->bottom
<= best
->bottom
&&
2428 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
2431 if (mdb
->left
== 0 && mdb
->right
== 0 && mdb
->bottom
== 0 &&
2438 else if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
2441 * Look for the largest margins...
2444 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
2445 mdb
&& cups_is_close_media_db(mdb
, &key
);
2446 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2448 if (mdb
->left
>= best
->left
&& mdb
->right
>= best
->right
&&
2449 mdb
->top
>= best
->top
&& mdb
->bottom
>= best
->bottom
&&
2450 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
2457 * Look for the smallest non-zero margins...
2460 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
2461 mdb
&& cups_is_close_media_db(mdb
, &key
);
2462 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2464 if (((mdb
->left
> 0 && mdb
->left
<= best
->left
) || best
->left
== 0) &&
2465 ((mdb
->right
> 0 && mdb
->right
<= best
->right
) ||
2466 best
->right
== 0) &&
2467 ((mdb
->top
> 0 && mdb
->top
<= best
->top
) || best
->top
== 0) &&
2468 ((mdb
->bottom
> 0 && mdb
->bottom
<= best
->bottom
) ||
2469 best
->bottom
== 0) &&
2470 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
2479 * Return the matching size...
2483 strlcpy(size
->media
, best
->key
, sizeof(size
->media
));
2484 else if (best
->size_name
)
2485 strlcpy(size
->media
, best
->size_name
, sizeof(size
->media
));
2487 strlcpy(size
->media
, pwg
->pwg
, sizeof(size
->media
));
2489 size
->width
= best
->width
;
2490 size
->length
= best
->length
;
2491 size
->bottom
= best
->bottom
;
2492 size
->left
= best
->left
;
2493 size
->right
= best
->right
;
2494 size
->top
= best
->top
;
2504 * 'cups_is_close_media_db()' - Compare two media entries to see if they are
2505 * close to the same size.
2507 * Currently we use 5 points (from PostScript) as the matching range...
2510 static int /* O - 1 if the sizes are close */
2511 cups_is_close_media_db(
2512 _cups_media_db_t
*a
, /* I - First media entries */
2513 _cups_media_db_t
*b
) /* I - Second media entries */
2515 int dwidth
, /* Difference in width */
2516 dlength
; /* Difference in length */
2519 dwidth
= a
->width
- b
->width
;
2520 dlength
= a
->length
- b
->length
;
2522 return (dwidth
>= -176 && dwidth
<= 176 &&
2523 dlength
>= -176 && dlength
<= 176);
2528 * 'cups_test_constraints()' - Test constraints.
2531 static cups_array_t
* /* O - Active constraints */
2532 cups_test_constraints(
2533 cups_dinfo_t
*dinfo
, /* I - Destination information */
2534 const char *new_option
, /* I - Newly selected option */
2535 const char *new_value
, /* I - Newly selected value */
2536 int num_options
, /* I - Number of options */
2537 cups_option_t
*options
, /* I - Options */
2538 int *num_conflicts
, /* O - Number of conflicting options */
2539 cups_option_t
**conflicts
) /* O - Conflicting options */
2541 int i
, /* Looping var */
2542 count
, /* Number of values */
2543 match
; /* Value matches? */
2544 int num_matching
; /* Number of matching options */
2545 cups_option_t
*matching
; /* Matching options */
2546 _cups_dconstres_t
*c
; /* Current constraint */
2547 cups_array_t
*active
= NULL
; /* Active constraints */
2548 ipp_t
*col
; /* Collection value */
2549 ipp_attribute_t
*attr
; /* Current attribute */
2550 _ipp_value_t
*attrval
; /* Current attribute value */
2551 const char *value
; /* Current value */
2552 char temp
[1024]; /* Temporary string */
2553 int int_value
; /* Integer value */
2554 int xres_value
, /* Horizontal resolution */
2555 yres_value
; /* Vertical resolution */
2556 ipp_res_t units_value
; /* Resolution units */
2559 for (c
= (_cups_dconstres_t
*)cupsArrayFirst(dinfo
->constraints
);
2561 c
= (_cups_dconstres_t
*)cupsArrayNext(dinfo
->constraints
))
2566 for (attr
= ippFirstAttribute(c
->collection
);
2568 attr
= ippNextAttribute(c
->collection
))
2571 * Get the value for the current attribute in the constraint...
2574 if (new_option
&& new_value
&& !strcmp(attr
->name
, new_option
))
2576 else if ((value
= cupsGetOption(attr
->name
, num_options
, options
)) == NULL
)
2577 value
= cupsGetOption(attr
->name
, dinfo
->num_defaults
, dinfo
->defaults
);
2582 * Not set so this constraint does not apply...
2590 switch (attr
->value_tag
)
2592 case IPP_TAG_INTEGER
:
2594 int_value
= atoi(value
);
2596 for (i
= attr
->num_values
, attrval
= attr
->values
;
2600 if (attrval
->integer
== int_value
)
2608 case IPP_TAG_BOOLEAN
:
2609 int_value
= !strcmp(value
, "true");
2611 for (i
= attr
->num_values
, attrval
= attr
->values
;
2615 if (attrval
->boolean
== int_value
)
2623 case IPP_TAG_RANGE
:
2624 int_value
= atoi(value
);
2626 for (i
= attr
->num_values
, attrval
= attr
->values
;
2630 if (int_value
>= attrval
->range
.lower
&&
2631 int_value
<= attrval
->range
.upper
)
2639 case IPP_TAG_RESOLUTION
:
2640 if (sscanf(value
, "%dx%d%15s", &xres_value
, &yres_value
, temp
) != 3)
2642 if (sscanf(value
, "%d%15s", &xres_value
, temp
) != 2)
2645 yres_value
= xres_value
;
2648 if (!strcmp(temp
, "dpi"))
2649 units_value
= IPP_RES_PER_INCH
;
2650 else if (!strcmp(temp
, "dpc") || !strcmp(temp
, "dpcm"))
2651 units_value
= IPP_RES_PER_CM
;
2655 for (i
= attr
->num_values
, attrval
= attr
->values
;
2659 if (attrval
->resolution
.xres
== xres_value
&&
2660 attrval
->resolution
.yres
== yres_value
&&
2661 attrval
->resolution
.units
== units_value
)
2671 case IPP_TAG_KEYWORD
:
2672 case IPP_TAG_CHARSET
:
2674 case IPP_TAG_URISCHEME
:
2675 case IPP_TAG_MIMETYPE
:
2676 case IPP_TAG_LANGUAGE
:
2677 case IPP_TAG_TEXTLANG
:
2678 case IPP_TAG_NAMELANG
:
2679 for (i
= attr
->num_values
, attrval
= attr
->values
;
2683 if (!strcmp(attrval
->string
.text
, value
))
2691 case IPP_TAG_BEGIN_COLLECTION
:
2693 _cupsEncodeOption(col
, IPP_TAG_ZERO
, NULL
, ippGetName(attr
), value
);
2695 for (i
= 0, count
= ippGetCount(attr
); i
< count
; i
++)
2697 if (cups_collection_contains(col
, ippGetCollection(attr
, i
)))
2714 num_matching
= cupsAddOption(attr
->name
, value
, num_matching
, &matching
);
2720 active
= cupsArrayNew(NULL
, NULL
);
2722 cupsArrayAdd(active
, c
);
2724 if (num_conflicts
&& conflicts
)
2726 cups_option_t
*moption
; /* Matching option */
2728 for (i
= num_matching
, moption
= matching
; i
> 0; i
--, moption
++)
2729 *num_conflicts
= cupsAddOption(moption
->name
, moption
->value
, *num_conflicts
, conflicts
);
2733 cupsFreeOptions(num_matching
, matching
);
2741 * 'cups_update_ready()' - Update xxx-ready attributes for the printer.
2745 cups_update_ready(http_t
*http
, /* I - Connection to destination */
2746 cups_dinfo_t
*dinfo
) /* I - Destination information */
2748 ipp_t
*request
; /* Get-Printer-Attributes request */
2749 static const char * const pattrs
[] = /* Printer attributes we want */
2751 "finishings-col-ready",
2753 "job-finishings-col-ready",
2754 "job-finishings-ready",
2761 * Don't update more than once every 30 seconds...
2764 if ((time(NULL
) - dinfo
->ready_time
) < _CUPS_MEDIA_READY_TTL
)
2768 * Free any previous results...
2771 if (dinfo
->cached_flags
& CUPS_MEDIA_FLAGS_READY
)
2773 cupsArrayDelete(dinfo
->cached_db
);
2774 dinfo
->cached_db
= NULL
;
2775 dinfo
->cached_flags
= CUPS_MEDIA_FLAGS_DEFAULT
;
2778 ippDelete(dinfo
->ready_attrs
);
2779 dinfo
->ready_attrs
= NULL
;
2781 cupsArrayDelete(dinfo
->ready_db
);
2782 dinfo
->ready_db
= NULL
;
2785 * Query the xxx-ready values...
2788 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
2789 ippSetVersion(request
, dinfo
->version
/ 10, dinfo
->version
% 10);
2791 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
2793 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
2795 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "requested-attributes", (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
2797 dinfo
->ready_attrs
= cupsDoRequest(http
, request
, dinfo
->resource
);
2800 * Update the ready media database...
2803 cups_create_media_db(dinfo
, CUPS_MEDIA_FLAGS_READY
);
2806 * Update last lookup time and return...
2809 dinfo
->ready_time
= time(NULL
);