2 * Destination option/media support for CUPS.
4 * Copyright © 2012-2018 by Apple Inc.
6 * Licensed under Apache License v2.0. See the file "LICENSE" for more
11 * Include necessary headers...
14 #include "cups-private.h"
15 #include "debug-internal.h"
22 #define _CUPS_MEDIA_READY_TTL 30 /* Life of xxx-ready values */
29 static void cups_add_dconstres(cups_array_t
*a
, ipp_t
*collection
);
30 static int cups_collection_contains(ipp_t
*test
, ipp_t
*match
);
31 static size_t cups_collection_string(ipp_attribute_t
*attr
, char *buffer
, size_t bufsize
);
32 static int cups_compare_dconstres(_cups_dconstres_t
*a
,
33 _cups_dconstres_t
*b
);
34 static int cups_compare_media_db(_cups_media_db_t
*a
,
36 static _cups_media_db_t
*cups_copy_media_db(_cups_media_db_t
*mdb
);
37 static void cups_create_cached(http_t
*http
, cups_dinfo_t
*dinfo
,
39 static void cups_create_constraints(cups_dinfo_t
*dinfo
);
40 static void cups_create_defaults(cups_dinfo_t
*dinfo
);
41 static void cups_create_media_db(cups_dinfo_t
*dinfo
,
43 static void cups_free_media_db(_cups_media_db_t
*mdb
);
44 static int cups_get_media_db(http_t
*http
, cups_dinfo_t
*dinfo
,
45 pwg_media_t
*pwg
, unsigned flags
,
47 static int cups_is_close_media_db(_cups_media_db_t
*a
,
49 static cups_array_t
*cups_test_constraints(cups_dinfo_t
*dinfo
,
50 const char *new_option
,
51 const char *new_value
,
53 cups_option_t
*options
,
55 cups_option_t
**conflicts
);
56 static void cups_update_ready(http_t
*http
, cups_dinfo_t
*dinfo
);
60 * 'cupsAddDestMediaOptions()' - Add the option corresponding to the specified media size.
65 int /* O - New number of options */
66 cupsAddDestMediaOptions(
67 http_t
*http
, /* I - Connection to destination */
68 cups_dest_t
*dest
, /* I - Destination */
69 cups_dinfo_t
*dinfo
, /* I - Destination information */
70 unsigned flags
, /* I - Media matching flags */
71 cups_size_t
*size
, /* I - Media size */
72 int num_options
, /* I - Current number of options */
73 cups_option_t
**options
) /* IO - Options */
75 cups_array_t
*db
; /* Media database */
76 _cups_media_db_t
*mdb
; /* Media database entry */
77 char value
[2048]; /* Option value */
81 * Range check input...
84 if (!http
|| !dest
|| !dinfo
|| !size
|| !options
)
86 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
91 * Find the matching media size...
94 if (flags
& CUPS_MEDIA_FLAGS_READY
)
99 DEBUG_printf(("1cupsAddDestMediaOptions: size->media=\"%s\"", size
->media
));
101 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
); mdb
; mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
103 if (mdb
->key
&& !strcmp(mdb
->key
, size
->media
))
105 else if (mdb
->size_name
&& !strcmp(mdb
->size_name
, size
->media
))
111 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
); mdb
; mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
113 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
)
120 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
); mdb
; mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
122 if (mdb
->width
== size
->width
&& mdb
->length
== size
->length
)
129 DEBUG_puts("1cupsAddDestMediaOptions: Unable to find matching size.");
130 return (num_options
);
133 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
));
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\" media-type=\"%s\"}", mdb
->width
, mdb
->length
, mdb
->bottom
, mdb
->left
, mdb
->right
, mdb
->top
, mdb
->source
, mdb
->type
);
140 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
);
144 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
);
148 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
);
151 num_options
= cupsAddOption("media-col", value
, num_options
, options
);
153 return (num_options
);
158 * 'cupsCheckDestSupported()' - Check that the option and value are supported
159 * by the destination.
161 * Returns 1 if supported, 0 otherwise.
163 * @since CUPS 1.6/macOS 10.8@
166 int /* O - 1 if supported, 0 otherwise */
167 cupsCheckDestSupported(
168 http_t
*http
, /* I - Connection to destination */
169 cups_dest_t
*dest
, /* I - Destination */
170 cups_dinfo_t
*dinfo
, /* I - Destination information */
171 const char *option
, /* I - Option */
172 const char *value
) /* I - Value or @code NULL@ */
174 int i
; /* Looping var */
175 char temp
[1024]; /* Temporary string */
176 int int_value
; /* Integer value */
177 int xres_value
, /* Horizontal resolution */
178 yres_value
; /* Vertical resolution */
179 ipp_res_t units_value
; /* Resolution units */
180 ipp_attribute_t
*attr
; /* Attribute */
181 _ipp_value_t
*attrval
; /* Current attribute value */
185 * Get the default connection as needed...
189 http
= _cupsConnect();
192 * Range check input...
195 if (!http
|| !dest
|| !dinfo
|| !option
)
199 * Lookup the attribute...
202 if (strstr(option
, "-supported"))
203 attr
= ippFindAttribute(dinfo
->attrs
, option
, IPP_TAG_ZERO
);
206 snprintf(temp
, sizeof(temp
), "%s-supported", option
);
207 attr
= ippFindAttribute(dinfo
->attrs
, temp
, IPP_TAG_ZERO
);
220 if (!strcmp(option
, "media") && !strncmp(value
, "custom_", 7))
223 * Check range of custom media sizes...
226 pwg_media_t
*pwg
; /* Current PWG media size info */
227 int min_width
, /* Minimum width */
228 min_length
, /* Minimum length */
229 max_width
, /* Maximum width */
230 max_length
; /* Maximum length */
233 * Get the minimum and maximum size...
236 min_width
= min_length
= INT_MAX
;
237 max_width
= max_length
= 0;
239 for (i
= attr
->num_values
, attrval
= attr
->values
;
243 if (!strncmp(attrval
->string
.text
, "custom_min_", 11) &&
244 (pwg
= pwgMediaForPWG(attrval
->string
.text
)) != NULL
)
246 min_width
= pwg
->width
;
247 min_length
= pwg
->length
;
249 else if (!strncmp(attrval
->string
.text
, "custom_max_", 11) &&
250 (pwg
= pwgMediaForPWG(attrval
->string
.text
)) != NULL
)
252 max_width
= pwg
->width
;
253 max_length
= pwg
->length
;
261 if (min_width
< INT_MAX
&& max_width
> 0 &&
262 (pwg
= pwgMediaForPWG(value
)) != NULL
&&
263 pwg
->width
>= min_width
&& pwg
->width
<= max_width
&&
264 pwg
->length
>= min_length
&& pwg
->length
<= max_length
)
270 * Check literal values...
273 switch (attr
->value_tag
)
275 case IPP_TAG_INTEGER
:
277 int_value
= atoi(value
);
279 for (i
= 0; i
< attr
->num_values
; i
++)
280 if (attr
->values
[i
].integer
== int_value
)
284 case IPP_TAG_BOOLEAN
:
285 return (attr
->values
[0].boolean
);
288 int_value
= atoi(value
);
290 for (i
= 0; i
< attr
->num_values
; i
++)
291 if (int_value
>= attr
->values
[i
].range
.lower
&&
292 int_value
<= attr
->values
[i
].range
.upper
)
296 case IPP_TAG_RESOLUTION
:
297 if (sscanf(value
, "%dx%d%15s", &xres_value
, &yres_value
, temp
) != 3)
299 if (sscanf(value
, "%d%15s", &xres_value
, temp
) != 2)
302 yres_value
= xres_value
;
305 if (!strcmp(temp
, "dpi"))
306 units_value
= IPP_RES_PER_INCH
;
307 else if (!strcmp(temp
, "dpc") || !strcmp(temp
, "dpcm"))
308 units_value
= IPP_RES_PER_CM
;
312 for (i
= attr
->num_values
, attrval
= attr
->values
;
316 if (attrval
->resolution
.xres
== xres_value
&&
317 attrval
->resolution
.yres
== yres_value
&&
318 attrval
->resolution
.units
== units_value
)
325 case IPP_TAG_KEYWORD
:
326 case IPP_TAG_CHARSET
:
328 case IPP_TAG_URISCHEME
:
329 case IPP_TAG_MIMETYPE
:
330 case IPP_TAG_LANGUAGE
:
331 case IPP_TAG_TEXTLANG
:
332 case IPP_TAG_NAMELANG
:
333 for (i
= 0; i
< attr
->num_values
; i
++)
334 if (!strcmp(attr
->values
[i
].string
.text
, value
))
344 * If we get there the option+value is not supported...
352 * 'cupsCopyDestConflicts()' - Get conflicts and resolutions for a new
355 * "num_options" and "options" represent the currently selected options by the
356 * user. "new_option" and "new_value" are the setting the user has just
359 * Returns 1 if there is a conflict, 0 if there are no conflicts, and -1 if
360 * there was an unrecoverable error such as a resolver loop.
362 * If "num_conflicts" and "conflicts" are not @code NULL@, they are set to
363 * contain the list of conflicting option/value pairs. Similarly, if
364 * "num_resolved" and "resolved" are not @code NULL@ they will be set to the
365 * list of changes needed to resolve the conflict.
367 * If cupsCopyDestConflicts returns 1 but "num_resolved" and "resolved" are set
368 * to 0 and @code NULL@, respectively, then the conflict cannot be resolved.
370 * @since CUPS 1.6/macOS 10.8@
373 int /* O - 1 if there is a conflict, 0 if none, -1 on error */
374 cupsCopyDestConflicts(
375 http_t
*http
, /* I - Connection to destination */
376 cups_dest_t
*dest
, /* I - Destination */
377 cups_dinfo_t
*dinfo
, /* I - Destination information */
378 int num_options
, /* I - Number of current options */
379 cups_option_t
*options
, /* I - Current options */
380 const char *new_option
, /* I - New option */
381 const char *new_value
, /* I - New value */
382 int *num_conflicts
, /* O - Number of conflicting options */
383 cups_option_t
**conflicts
, /* O - Conflicting options */
384 int *num_resolved
, /* O - Number of options to resolve */
385 cups_option_t
**resolved
) /* O - Resolved options */
387 int i
, /* Looping var */
388 have_conflicts
= 0, /* Do we have conflicts? */
389 changed
, /* Did we change something? */
390 tries
, /* Number of tries for resolution */
391 num_myconf
= 0, /* My number of conflicting options */
392 num_myres
= 0; /* My number of resolved options */
393 cups_option_t
*myconf
= NULL
, /* My conflicting options */
394 *myres
= NULL
, /* My resolved options */
395 *myoption
, /* My current option */
396 *option
; /* Current option */
397 cups_array_t
*active
= NULL
, /* Active conflicts */
398 *pass
= NULL
, /* Resolvers for this pass */
399 *resolvers
= NULL
, /* Resolvers we have used */
400 *test
; /* Test array for conflicts */
401 _cups_dconstres_t
*c
, /* Current constraint */
402 *r
; /* Current resolver */
403 ipp_attribute_t
*attr
; /* Current attribute */
404 char value
[2048]; /* Current attribute value as string */
405 const char *myvalue
; /* Current value of an option */
409 * Clear returned values...
425 * Get the default connection as needed...
429 http
= _cupsConnect();
432 * Range check input...
435 if (!http
|| !dest
|| !dinfo
||
436 (num_conflicts
!= NULL
) != (conflicts
!= NULL
) ||
437 (num_resolved
!= NULL
) != (resolved
!= NULL
))
441 * Load constraints as needed...
444 if (!dinfo
->constraints
)
445 cups_create_constraints(dinfo
);
447 if (cupsArrayCount(dinfo
->constraints
) == 0)
450 if (!dinfo
->num_defaults
)
451 cups_create_defaults(dinfo
);
454 * If we are resolving, create a shadow array...
459 for (i
= num_options
, option
= options
; i
> 0; i
--, option
++)
460 num_myres
= cupsAddOption(option
->name
, option
->value
, num_myres
, &myres
);
462 if (new_option
&& new_value
)
463 num_myres
= cupsAddOption(new_option
, new_value
, num_myres
, &myres
);
467 num_myres
= num_options
;
472 * Check for any conflicts...
476 pass
= cupsArrayNew((cups_array_func_t
)cups_compare_dconstres
, NULL
);
478 for (tries
= 0; tries
< 100; tries
++)
481 * Check for any conflicts...
484 if (num_conflicts
|| num_resolved
)
486 cupsFreeOptions(num_myconf
, myconf
);
490 active
= cups_test_constraints(dinfo
, new_option
, new_value
,
491 num_myres
, myres
, &num_myconf
,
495 active
= cups_test_constraints(dinfo
, new_option
, new_value
, num_myres
,
498 have_conflicts
= (active
!= NULL
);
500 if (!active
|| !num_resolved
)
501 break; /* All done */
504 * Scan the constraints that were triggered to apply resolvers...
508 resolvers
= cupsArrayNew((cups_array_func_t
)cups_compare_dconstres
, NULL
);
510 for (c
= (_cups_dconstres_t
*)cupsArrayFirst(active
), changed
= 0;
512 c
= (_cups_dconstres_t
*)cupsArrayNext(active
))
514 if (cupsArrayFind(pass
, c
))
515 continue; /* Already applied this resolver... */
517 if (cupsArrayFind(resolvers
, c
))
519 DEBUG_printf(("1cupsCopyDestConflicts: Resolver loop with %s.",
525 if ((r
= cupsArrayFind(dinfo
->resolvers
, c
)) == NULL
)
527 DEBUG_printf(("1cupsCopyDestConflicts: Resolver %s not found.",
534 * Add the options from the resolver...
537 cupsArrayAdd(pass
, r
);
538 cupsArrayAdd(resolvers
, r
);
540 for (attr
= ippFirstAttribute(r
->collection
);
542 attr
= ippNextAttribute(r
->collection
))
544 if (new_option
&& !strcmp(attr
->name
, new_option
))
545 continue; /* Ignore this if we just changed it */
547 if (ippAttributeString(attr
, value
, sizeof(value
)) >= sizeof(value
))
548 continue; /* Ignore if the value is too long */
550 if ((test
= cups_test_constraints(dinfo
, attr
->name
, value
, num_myres
,
551 myres
, NULL
, NULL
)) == NULL
)
554 * That worked, flag it...
560 cupsArrayDelete(test
);
563 * Add the option/value from the resolver regardless of whether it
564 * worked; this makes sure that we can cascade several changes to
565 * make things resolve...
568 num_myres
= cupsAddOption(attr
->name
, value
, num_myres
, &myres
);
574 DEBUG_puts("1cupsCopyDestConflicts: Unable to resolve constraints.");
579 cupsArrayClear(pass
);
581 cupsArrayDelete(active
);
587 DEBUG_puts("1cupsCopyDestConflicts: Unable to resolve after 100 tries.");
593 * Copy resolved options as needed...
598 for (i
= num_myres
, myoption
= myres
; i
> 0; i
--, myoption
++)
600 if ((myvalue
= cupsGetOption(myoption
->name
, num_options
,
602 strcmp(myvalue
, myoption
->value
))
604 if (new_option
&& !strcmp(new_option
, myoption
->name
) &&
605 new_value
&& !strcmp(new_value
, myoption
->value
))
608 *num_resolved
= cupsAddOption(myoption
->name
, myoption
->value
,
609 *num_resolved
, resolved
);
620 cupsArrayDelete(active
);
621 cupsArrayDelete(pass
);
622 cupsArrayDelete(resolvers
);
627 * Free shadow copy of options...
630 cupsFreeOptions(num_myres
, myres
);
636 * Return conflicting options to caller...
639 *num_conflicts
= num_myconf
;
645 * Free conflicting options...
648 cupsFreeOptions(num_myconf
, myconf
);
651 return (have_conflicts
);
656 * 'cupsCopyDestInfo()' - Get the supported values/capabilities for the
659 * The caller is responsible for calling @link cupsFreeDestInfo@ on the return
660 * value. @code NULL@ is returned on error.
662 * @since CUPS 1.6/macOS 10.8@
665 cups_dinfo_t
* /* O - Destination information */
667 http_t
*http
, /* I - Connection to destination */
668 cups_dest_t
*dest
) /* I - Destination */
670 cups_dinfo_t
*dinfo
; /* Destination information */
671 unsigned dflags
; /* Destination flags */
672 ipp_t
*request
, /* Get-Printer-Attributes request */
673 *response
; /* Supported attributes */
674 int tries
, /* Number of tries so far */
675 delay
, /* Current retry delay */
676 prev_delay
; /* Next retry delay */
677 const char *uri
; /* Printer URI */
678 char resource
[1024]; /* Resource path */
679 int version
; /* IPP version */
680 ipp_status_t status
; /* Status of request */
681 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
682 static const char * const requested_attrs
[] =
683 { /* Requested attributes */
685 "media-col-database",
686 "printer-description"
690 DEBUG_printf(("cupsCopyDestInfo(http=%p, dest=%p(%s))", (void *)http
, (void *)dest
, dest
? dest
->name
: ""));
693 * Get the default connection as needed...
698 DEBUG_puts("1cupsCopyDestInfo: Default server connection.");
699 http
= _cupsConnect();
700 dflags
= CUPS_DEST_FLAGS_NONE
;
703 else if (httpAddrFamily(http
->hostaddr
) == AF_LOCAL
)
705 DEBUG_puts("1cupsCopyDestInfo: Connection to server (domain socket).");
706 dflags
= CUPS_DEST_FLAGS_NONE
;
708 #endif /* AF_LOCAL */
709 else if ((strcmp(http
->hostname
, cg
->server
) && cg
->server
[0] != '/') || cg
->ipp_port
!= httpAddrPort(http
->hostaddr
))
711 DEBUG_printf(("1cupsCopyDestInfo: Connection to device (%s).", http
->hostname
));
712 dflags
= CUPS_DEST_FLAGS_DEVICE
;
716 DEBUG_printf(("1cupsCopyDestInfo: Connection to server (%s).", http
->hostname
));
717 dflags
= CUPS_DEST_FLAGS_NONE
;
721 * Range check input...
728 * Get the printer URI and resource path...
731 if ((uri
= _cupsGetDestResource(dest
, dflags
, resource
, sizeof(resource
))) == NULL
)
733 DEBUG_puts("1cupsCopyDestInfo: Unable to get resource.");
738 * Get the supported attributes...
749 * Send a Get-Printer-Attributes request...
752 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
754 ippSetVersion(request
, version
/ 10, version
% 10);
755 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
756 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
757 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", (int)(sizeof(requested_attrs
) / sizeof(requested_attrs
[0])), NULL
, requested_attrs
);
758 response
= cupsDoRequest(http
, request
, resource
);
759 status
= cupsLastError();
761 if (status
> IPP_STATUS_OK_IGNORED_OR_SUBSTITUTED
)
763 DEBUG_printf(("1cupsCopyDestInfo: Get-Printer-Attributes for '%s' returned %s (%s)", dest
->name
, ippErrorString(status
), cupsLastErrorString()));
768 if ((status
== IPP_STATUS_ERROR_BAD_REQUEST
|| status
== IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
) && version
> 11)
772 else if (status
== IPP_STATUS_ERROR_BUSY
)
774 sleep((unsigned)delay
);
776 delay
= _cupsNextDelay(delay
, &prev_delay
);
784 while (!response
&& tries
< 10);
788 DEBUG_puts("1cupsCopyDestInfo: Unable to get printer attributes.");
793 * Allocate a cups_dinfo_t structure and return it...
796 if ((dinfo
= calloc(1, sizeof(cups_dinfo_t
))) == NULL
)
798 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
803 DEBUG_printf(("1cupsCopyDestInfo: version=%d, uri=\"%s\", resource=\"%s\".", version
, uri
, resource
));
805 dinfo
->version
= version
;
807 dinfo
->resource
= _cupsStrAlloc(resource
);
808 dinfo
->attrs
= response
;
815 * 'cupsFindDestDefault()' - Find the default value(s) for the given option.
817 * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
818 * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
819 * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
820 * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
821 * functions to inspect the default value(s) as needed.
823 * @since CUPS 1.7/macOS 10.9@
826 ipp_attribute_t
* /* O - Default attribute or @code NULL@ for none */
828 http_t
*http
, /* I - Connection to destination */
829 cups_dest_t
*dest
, /* I - Destination */
830 cups_dinfo_t
*dinfo
, /* I - Destination information */
831 const char *option
) /* I - Option/attribute name */
833 char name
[IPP_MAX_NAME
]; /* Attribute name */
837 * Get the default connection as needed...
841 http
= _cupsConnect();
844 * Range check input...
847 if (!http
|| !dest
|| !dinfo
|| !option
)
849 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
854 * Find and return the attribute...
857 snprintf(name
, sizeof(name
), "%s-default", option
);
858 return (ippFindAttribute(dinfo
->attrs
, name
, IPP_TAG_ZERO
));
863 * 'cupsFindDestReady()' - Find the default value(s) for the given option.
865 * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
866 * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
867 * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
868 * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
869 * functions to inspect the default value(s) as needed.
871 * @since CUPS 1.7/macOS 10.9@
874 ipp_attribute_t
* /* O - Default attribute or @code NULL@ for none */
876 http_t
*http
, /* I - Connection to destination */
877 cups_dest_t
*dest
, /* I - Destination */
878 cups_dinfo_t
*dinfo
, /* I - Destination information */
879 const char *option
) /* I - Option/attribute name */
881 char name
[IPP_MAX_NAME
]; /* Attribute name */
885 * Get the default connection as needed...
889 http
= _cupsConnect();
892 * Range check input...
895 if (!http
|| !dest
|| !dinfo
|| !option
)
897 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
902 * Find and return the attribute...
905 cups_update_ready(http
, dinfo
);
907 snprintf(name
, sizeof(name
), "%s-ready", option
);
908 return (ippFindAttribute(dinfo
->ready_attrs
, name
, IPP_TAG_ZERO
));
913 * 'cupsFindDestSupported()' - Find the default value(s) for the given option.
915 * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
916 * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
917 * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
918 * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
919 * functions to inspect the default value(s) as needed.
921 * @since CUPS 1.7/macOS 10.9@
924 ipp_attribute_t
* /* O - Default attribute or @code NULL@ for none */
925 cupsFindDestSupported(
926 http_t
*http
, /* I - Connection to destination */
927 cups_dest_t
*dest
, /* I - Destination */
928 cups_dinfo_t
*dinfo
, /* I - Destination information */
929 const char *option
) /* I - Option/attribute name */
931 char name
[IPP_MAX_NAME
]; /* Attribute name */
935 * Get the default connection as needed...
939 http
= _cupsConnect();
942 * Range check input...
945 if (!http
|| !dest
|| !dinfo
|| !option
)
947 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
952 * Find and return the attribute...
955 snprintf(name
, sizeof(name
), "%s-supported", option
);
956 return (ippFindAttribute(dinfo
->attrs
, name
, IPP_TAG_ZERO
));
961 * 'cupsFreeDestInfo()' - Free destination information obtained using
962 * @link cupsCopyDestInfo@.
964 * @since CUPS 1.6/macOS 10.8@
968 cupsFreeDestInfo(cups_dinfo_t
*dinfo
) /* I - Destination information */
971 * Range check input...
978 * Free memory and return...
981 _cupsStrFree(dinfo
->resource
);
983 cupsArrayDelete(dinfo
->constraints
);
984 cupsArrayDelete(dinfo
->resolvers
);
986 cupsArrayDelete(dinfo
->localizations
);
988 cupsArrayDelete(dinfo
->media_db
);
990 cupsArrayDelete(dinfo
->cached_db
);
992 ippDelete(dinfo
->ready_attrs
);
993 cupsArrayDelete(dinfo
->ready_db
);
995 ippDelete(dinfo
->attrs
);
1002 * 'cupsGetDestMediaByIndex()' - Get a media name, dimension, and margins for a
1005 * The @code flags@ parameter determines which set of media are indexed. For
1006 * example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will get the Nth
1007 * borderless size supported by the printer.
1009 * @since CUPS 1.7/macOS 10.9@
1012 int /* O - 1 on success, 0 on failure */
1013 cupsGetDestMediaByIndex(
1014 http_t
*http
, /* I - Connection to destination */
1015 cups_dest_t
*dest
, /* I - Destination */
1016 cups_dinfo_t
*dinfo
, /* I - Destination information */
1017 int n
, /* I - Media size number (0-based) */
1018 unsigned flags
, /* I - Media flags */
1019 cups_size_t
*size
) /* O - Media size information */
1021 _cups_media_db_t
*nsize
; /* Size for N */
1022 pwg_media_t
*pwg
; /* PWG media name for size */
1026 * Get the default connection as needed...
1030 http
= _cupsConnect();
1033 * Range check input...
1037 memset(size
, 0, sizeof(cups_size_t
));
1039 if (!http
|| !dest
|| !dinfo
|| n
< 0 || !size
)
1041 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1046 * Load media list as needed...
1049 if (flags
& CUPS_MEDIA_FLAGS_READY
)
1050 cups_update_ready(http
, dinfo
);
1052 if (!dinfo
->cached_db
|| dinfo
->cached_flags
!= flags
)
1053 cups_create_cached(http
, dinfo
, flags
);
1056 * Copy the size over and return...
1059 if ((nsize
= (_cups_media_db_t
*)cupsArrayIndex(dinfo
->cached_db
, n
)) == NULL
)
1061 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1066 strlcpy(size
->media
, nsize
->key
, sizeof(size
->media
));
1067 else if (nsize
->size_name
)
1068 strlcpy(size
->media
, nsize
->size_name
, sizeof(size
->media
));
1069 else if ((pwg
= pwgMediaForSize(nsize
->width
, nsize
->length
)) != NULL
)
1070 strlcpy(size
->media
, pwg
->pwg
, sizeof(size
->media
));
1073 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1077 size
->width
= nsize
->width
;
1078 size
->length
= nsize
->length
;
1079 size
->bottom
= nsize
->bottom
;
1080 size
->left
= nsize
->left
;
1081 size
->right
= nsize
->right
;
1082 size
->top
= nsize
->top
;
1089 * 'cupsGetDestMediaByName()' - Get media names, dimensions, and margins.
1091 * The "media" string is a PWG media name. "Flags" provides some matching
1092 * guidance (multiple flags can be combined):
1094 * CUPS_MEDIA_FLAGS_DEFAULT = find the closest size supported by the printer,
1095 * CUPS_MEDIA_FLAGS_BORDERLESS = find a borderless size,
1096 * CUPS_MEDIA_FLAGS_DUPLEX = find a size compatible with 2-sided printing,
1097 * CUPS_MEDIA_FLAGS_EXACT = find an exact match for the size, and
1098 * CUPS_MEDIA_FLAGS_READY = if the printer supports media sensing, find the
1099 * size amongst the "ready" media.
1101 * The matching result (if any) is returned in the "cups_size_t" structure.
1103 * Returns 1 when there is a match and 0 if there is not a match.
1105 * @since CUPS 1.6/macOS 10.8@
1108 int /* O - 1 on match, 0 on failure */
1109 cupsGetDestMediaByName(
1110 http_t
*http
, /* I - Connection to destination */
1111 cups_dest_t
*dest
, /* I - Destination */
1112 cups_dinfo_t
*dinfo
, /* I - Destination information */
1113 const char *media
, /* I - Media name */
1114 unsigned flags
, /* I - Media matching flags */
1115 cups_size_t
*size
) /* O - Media size information */
1117 pwg_media_t
*pwg
; /* PWG media info */
1121 * Get the default connection as needed...
1125 http
= _cupsConnect();
1128 * Range check input...
1132 memset(size
, 0, sizeof(cups_size_t
));
1134 if (!http
|| !dest
|| !dinfo
|| !media
|| !size
)
1136 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1141 * Lookup the media size name...
1144 if ((pwg
= pwgMediaForPWG(media
)) == NULL
)
1145 if ((pwg
= pwgMediaForLegacy(media
)) == NULL
)
1147 DEBUG_printf(("1cupsGetDestMediaByName: Unknown size '%s'.", media
));
1148 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown media size name."), 1);
1153 * Lookup the size...
1156 return (cups_get_media_db(http
, dinfo
, pwg
, flags
, size
));
1161 * 'cupsGetDestMediaBySize()' - Get media names, dimensions, and margins.
1163 * "Width" and "length" are the dimensions in hundredths of millimeters.
1164 * "Flags" provides some matching guidance (multiple flags can be combined):
1166 * CUPS_MEDIA_FLAGS_DEFAULT = find the closest size supported by the printer,
1167 * CUPS_MEDIA_FLAGS_BORDERLESS = find a borderless size,
1168 * CUPS_MEDIA_FLAGS_DUPLEX = find a size compatible with 2-sided printing,
1169 * CUPS_MEDIA_FLAGS_EXACT = find an exact match for the size, and
1170 * CUPS_MEDIA_FLAGS_READY = if the printer supports media sensing, find the
1171 * size amongst the "ready" media.
1173 * The matching result (if any) is returned in the "cups_size_t" structure.
1175 * Returns 1 when there is a match and 0 if there is not a match.
1177 * @since CUPS 1.6/macOS 10.8@
1180 int /* O - 1 on match, 0 on failure */
1181 cupsGetDestMediaBySize(
1182 http_t
*http
, /* I - Connection to destination */
1183 cups_dest_t
*dest
, /* I - Destination */
1184 cups_dinfo_t
*dinfo
, /* I - Destination information */
1185 int width
, /* I - Media width in hundredths of
1187 int length
, /* I - Media length in hundredths of
1189 unsigned flags
, /* I - Media matching flags */
1190 cups_size_t
*size
) /* O - Media size information */
1192 pwg_media_t
*pwg
; /* PWG media info */
1196 * Get the default connection as needed...
1200 http
= _cupsConnect();
1203 * Range check input...
1207 memset(size
, 0, sizeof(cups_size_t
));
1209 if (!http
|| !dest
|| !dinfo
|| width
<= 0 || length
<= 0 || !size
)
1211 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1216 * Lookup the media size name...
1219 if ((pwg
= pwgMediaForSize(width
, length
)) == NULL
)
1221 DEBUG_printf(("1cupsGetDestMediaBySize: Invalid size %dx%d.", width
,
1223 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Invalid media size."), 1);
1228 * Lookup the size...
1231 return (cups_get_media_db(http
, dinfo
, pwg
, flags
, size
));
1236 * 'cupsGetDestMediaCount()' - Get the number of sizes supported by a
1239 * The @code flags@ parameter determines the set of media sizes that are
1240 * counted. For example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will return
1241 * the number of borderless sizes.
1243 * @since CUPS 1.7/macOS 10.9@
1246 int /* O - Number of sizes */
1247 cupsGetDestMediaCount(
1248 http_t
*http
, /* I - Connection to destination */
1249 cups_dest_t
*dest
, /* I - Destination */
1250 cups_dinfo_t
*dinfo
, /* I - Destination information */
1251 unsigned flags
) /* I - Media flags */
1254 * Get the default connection as needed...
1258 http
= _cupsConnect();
1261 * Range check input...
1264 if (!http
|| !dest
|| !dinfo
)
1266 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1271 * Load media list as needed...
1274 if (flags
& CUPS_MEDIA_FLAGS_READY
)
1275 cups_update_ready(http
, dinfo
);
1277 if (!dinfo
->cached_db
|| dinfo
->cached_flags
!= flags
)
1278 cups_create_cached(http
, dinfo
, flags
);
1280 return (cupsArrayCount(dinfo
->cached_db
));
1285 * 'cupsGetDestMediaDefault()' - Get the default size for a destination.
1287 * The @code flags@ parameter determines which default size is returned. For
1288 * example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will return the default
1289 * borderless size, typically US Letter or A4, but sometimes 4x6 photo media.
1291 * @since CUPS 1.7/macOS 10.9@
1294 int /* O - 1 on success, 0 on failure */
1295 cupsGetDestMediaDefault(
1296 http_t
*http
, /* I - Connection to destination */
1297 cups_dest_t
*dest
, /* I - Destination */
1298 cups_dinfo_t
*dinfo
, /* I - Destination information */
1299 unsigned flags
, /* I - Media flags */
1300 cups_size_t
*size
) /* O - Media size information */
1302 const char *media
; /* Default media size */
1306 * Get the default connection as needed...
1310 http
= _cupsConnect();
1313 * Range check input...
1317 memset(size
, 0, sizeof(cups_size_t
));
1319 if (!http
|| !dest
|| !dinfo
|| !size
)
1321 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1326 * Get the default media size, if any...
1329 if ((media
= cupsGetOption("media", dest
->num_options
, dest
->options
)) == NULL
)
1330 media
= "na_letter_8.5x11in";
1332 if (cupsGetDestMediaByName(http
, dest
, dinfo
, media
, flags
, size
))
1335 if (strcmp(media
, "na_letter_8.5x11in") && cupsGetDestMediaByName(http
, dest
, dinfo
, "iso_a4_210x297mm", flags
, size
))
1338 if (strcmp(media
, "iso_a4_210x297mm") && cupsGetDestMediaByName(http
, dest
, dinfo
, "na_letter_8.5x11in", flags
, size
))
1341 if ((flags
& CUPS_MEDIA_FLAGS_BORDERLESS
) && cupsGetDestMediaByName(http
, dest
, dinfo
, "na_index_4x6in", flags
, size
))
1345 * Fall back to the first matching media size...
1348 return (cupsGetDestMediaByIndex(http
, dest
, dinfo
, 0, flags
, size
));
1353 * 'cups_add_dconstres()' - Add a constraint or resolver to an array.
1358 cups_array_t
*a
, /* I - Array */
1359 ipp_t
*collection
) /* I - Collection value */
1361 ipp_attribute_t
*attr
; /* Attribute */
1362 _cups_dconstres_t
*temp
; /* Current constraint/resolver */
1365 if ((attr
= ippFindAttribute(collection
, "resolver-name",
1366 IPP_TAG_NAME
)) == NULL
)
1369 if ((temp
= calloc(1, sizeof(_cups_dconstres_t
))) == NULL
)
1372 temp
->name
= attr
->values
[0].string
.text
;
1373 temp
->collection
= collection
;
1375 cupsArrayAdd(a
, temp
);
1380 * 'cups_collection_contains()' - Check whether test collection is contained in the matching collection.
1383 static int /* O - 1 on a match, 0 on a non-match */
1384 cups_collection_contains(ipp_t
*test
, /* I - Collection to test */
1385 ipp_t
*match
) /* I - Matching values */
1387 int i
, j
, /* Looping vars */
1388 mcount
, /* Number of match values */
1389 tcount
; /* Number of test values */
1390 ipp_attribute_t
*tattr
, /* Testing attribute */
1391 *mattr
; /* Matching attribute */
1392 const char *tval
; /* Testing string value */
1395 for (mattr
= ippFirstAttribute(match
); mattr
; mattr
= ippNextAttribute(match
))
1397 if ((tattr
= ippFindAttribute(test
, ippGetName(mattr
), IPP_TAG_ZERO
)) == NULL
)
1400 tcount
= ippGetCount(tattr
);
1402 switch (ippGetValueTag(mattr
))
1404 case IPP_TAG_INTEGER
:
1406 if (ippGetValueTag(tattr
) != ippGetValueTag(mattr
))
1409 for (i
= 0; i
< tcount
; i
++)
1411 if (!ippContainsInteger(mattr
, ippGetInteger(tattr
, i
)))
1416 case IPP_TAG_RANGE
:
1417 if (ippGetValueTag(tattr
) != IPP_TAG_INTEGER
)
1420 for (i
= 0; i
< tcount
; i
++)
1422 if (!ippContainsInteger(mattr
, ippGetInteger(tattr
, i
)))
1427 case IPP_TAG_BOOLEAN
:
1428 if (ippGetValueTag(tattr
) != IPP_TAG_BOOLEAN
|| ippGetBoolean(tattr
, 0) != ippGetBoolean(mattr
, 0))
1432 case IPP_TAG_TEXTLANG
:
1433 case IPP_TAG_NAMELANG
:
1436 case IPP_TAG_KEYWORD
:
1438 case IPP_TAG_URISCHEME
:
1439 case IPP_TAG_CHARSET
:
1440 case IPP_TAG_LANGUAGE
:
1441 case IPP_TAG_MIMETYPE
:
1442 for (i
= 0; i
< tcount
; i
++)
1444 if ((tval
= ippGetString(tattr
, i
, NULL
)) == NULL
|| !ippContainsString(mattr
, tval
))
1449 case IPP_TAG_BEGIN_COLLECTION
:
1450 for (i
= 0; i
< tcount
; i
++)
1452 ipp_t
*tcol
= ippGetCollection(tattr
, i
);
1453 /* Testing collection */
1455 for (j
= 0, mcount
= ippGetCount(mattr
); j
< mcount
; j
++)
1456 if (!cups_collection_contains(tcol
, ippGetCollection(mattr
, j
)))
1471 * 'cups_collection_string()' - Convert an IPP collection to an option string.
1474 static size_t /* O - Number of bytes needed */
1475 cups_collection_string(
1476 ipp_attribute_t
*attr
, /* I - Collection attribute */
1477 char *buffer
, /* I - String buffer */
1478 size_t bufsize
) /* I - Size of buffer */
1480 int i
, j
, /* Looping vars */
1481 count
, /* Number of collection values */
1482 mcount
; /* Number of member values */
1483 ipp_t
*col
; /* Collection */
1484 ipp_attribute_t
*first
, /* First member attribute */
1485 *member
; /* Member attribute */
1486 char *bufptr
, /* Pointer into buffer */
1487 *bufend
, /* End of buffer */
1488 temp
[100]; /* Temporary string */
1489 const char *mptr
; /* Pointer into member value */
1490 int mlen
; /* Length of octetString */
1494 bufend
= buffer
+ bufsize
- 1;
1496 for (i
= 0, count
= ippGetCount(attr
); i
< count
; i
++)
1498 col
= ippGetCollection(attr
, i
);
1502 if (bufptr
< bufend
)
1508 if (bufptr
< bufend
)
1513 for (member
= first
= ippFirstAttribute(col
); member
; member
= ippNextAttribute(col
))
1515 const char *mname
= ippGetName(member
);
1517 if (member
!= first
)
1519 if (bufptr
< bufend
)
1525 if (ippGetValueTag(member
) == IPP_TAG_BOOLEAN
)
1527 if (!ippGetBoolean(member
, 0))
1529 if (bufptr
< bufend
)
1530 strlcpy(bufptr
, "no", (size_t)(bufend
- bufptr
+ 1));
1534 if (bufptr
< bufend
)
1535 strlcpy(bufptr
, mname
, (size_t)(bufend
- bufptr
+ 1));
1536 bufptr
+= strlen(mname
);
1540 if (bufptr
< bufend
)
1541 strlcpy(bufptr
, mname
, (size_t)(bufend
- bufptr
+ 1));
1542 bufptr
+= strlen(mname
);
1544 if (bufptr
< bufend
)
1549 if (ippGetValueTag(member
) == IPP_TAG_BEGIN_COLLECTION
)
1552 * Convert sub-collection...
1555 bufptr
+= cups_collection_string(member
, bufptr
, bufptr
< bufend
? (size_t)(bufend
- bufptr
+ 1) : 0);
1560 * Convert simple type...
1563 for (j
= 0, mcount
= ippGetCount(member
); j
< mcount
; j
++)
1567 if (bufptr
< bufend
)
1573 switch (ippGetValueTag(member
))
1575 case IPP_TAG_INTEGER
:
1577 bufptr
+= snprintf(bufptr
, bufptr
< bufend
? (size_t)(bufend
- bufptr
+ 1) : 0, "%d", ippGetInteger(member
, j
));
1580 case IPP_TAG_STRING
:
1581 if (bufptr
< bufend
)
1586 for (mptr
= (const char *)ippGetOctetString(member
, j
, &mlen
); mlen
> 0; mlen
--, mptr
++)
1588 if (*mptr
== '\"' || *mptr
== '\\')
1590 if (bufptr
< bufend
)
1596 if (bufptr
< bufend
)
1602 if (bufptr
< bufend
)
1610 unsigned year
; /* Year */
1611 const ipp_uchar_t
*date
= ippGetDate(member
, j
);
1614 year
= ((unsigned)date
[0] << 8) + (unsigned)date
[1];
1616 if (date
[9] == 0 && date
[10] == 0)
1617 snprintf(temp
, sizeof(temp
), "%04u-%02u-%02uT%02u:%02u:%02uZ", year
, date
[2], date
[3], date
[4], date
[5], date
[6]);
1619 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]);
1621 if (buffer
&& bufptr
< bufend
)
1622 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
+ 1));
1624 bufptr
+= strlen(temp
);
1628 case IPP_TAG_RESOLUTION
:
1630 int xres
, /* Horizontal resolution */
1631 yres
; /* Vertical resolution */
1632 ipp_res_t units
; /* Resolution units */
1634 xres
= ippGetResolution(member
, j
, &yres
, &units
);
1637 snprintf(temp
, sizeof(temp
), "%d%s", xres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
1639 snprintf(temp
, sizeof(temp
), "%dx%d%s", xres
, yres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
1641 if (buffer
&& bufptr
< bufend
)
1642 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
+ 1));
1644 bufptr
+= strlen(temp
);
1648 case IPP_TAG_RANGE
:
1650 int lower
, /* Lower bound */
1651 upper
; /* Upper bound */
1653 lower
= ippGetRange(member
, j
, &upper
);
1655 snprintf(temp
, sizeof(temp
), "%d-%d", lower
, upper
);
1657 if (buffer
&& bufptr
< bufend
)
1658 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
+ 1));
1660 bufptr
+= strlen(temp
);
1664 case IPP_TAG_TEXTLANG
:
1665 case IPP_TAG_NAMELANG
:
1668 case IPP_TAG_KEYWORD
:
1670 case IPP_TAG_URISCHEME
:
1671 case IPP_TAG_CHARSET
:
1672 case IPP_TAG_LANGUAGE
:
1673 case IPP_TAG_MIMETYPE
:
1674 if (bufptr
< bufend
)
1679 for (mptr
= ippGetString(member
, j
, NULL
); *mptr
; mptr
++)
1681 if (*mptr
== '\"' || *mptr
== '\\')
1683 if (bufptr
< bufend
)
1689 if (bufptr
< bufend
)
1695 if (bufptr
< bufend
)
1708 if (bufptr
< bufend
)
1715 return ((size_t)(bufptr
- buffer
+ 1));
1720 * 'cups_compare_dconstres()' - Compare to resolver entries.
1723 static int /* O - Result of comparison */
1724 cups_compare_dconstres(
1725 _cups_dconstres_t
*a
, /* I - First resolver */
1726 _cups_dconstres_t
*b
) /* I - Second resolver */
1728 return (strcmp(a
->name
, b
->name
));
1733 * 'cups_compare_media_db()' - Compare two media entries.
1736 static int /* O - Result of comparison */
1737 cups_compare_media_db(
1738 _cups_media_db_t
*a
, /* I - First media entries */
1739 _cups_media_db_t
*b
) /* I - Second media entries */
1741 int result
; /* Result of comparison */
1744 if ((result
= a
->width
- b
->width
) == 0)
1745 result
= a
->length
- b
->length
;
1752 * 'cups_copy_media_db()' - Copy a media entry.
1755 static _cups_media_db_t
* /* O - New media entry */
1757 _cups_media_db_t
*mdb
) /* I - Media entry to copy */
1759 _cups_media_db_t
*temp
; /* New media entry */
1762 if ((temp
= calloc(1, sizeof(_cups_media_db_t
))) == NULL
)
1766 temp
->color
= _cupsStrAlloc(mdb
->color
);
1768 temp
->key
= _cupsStrAlloc(mdb
->key
);
1770 temp
->info
= _cupsStrAlloc(mdb
->info
);
1772 temp
->size_name
= _cupsStrAlloc(mdb
->size_name
);
1774 temp
->source
= _cupsStrAlloc(mdb
->source
);
1776 temp
->type
= _cupsStrAlloc(mdb
->type
);
1778 temp
->width
= mdb
->width
;
1779 temp
->length
= mdb
->length
;
1780 temp
->bottom
= mdb
->bottom
;
1781 temp
->left
= mdb
->left
;
1782 temp
->right
= mdb
->right
;
1783 temp
->top
= mdb
->top
;
1790 * 'cups_create_cached()' - Create the media selection cache.
1794 cups_create_cached(http_t
*http
, /* I - Connection to destination */
1795 cups_dinfo_t
*dinfo
, /* I - Destination information */
1796 unsigned flags
) /* I - Media selection flags */
1798 cups_array_t
*db
; /* Media database array to use */
1799 _cups_media_db_t
*mdb
, /* Media database entry */
1800 *first
; /* First entry this size */
1803 DEBUG_printf(("3cups_create_cached(http=%p, dinfo=%p, flags=%u)", (void *)http
, (void *)dinfo
, flags
));
1805 if (dinfo
->cached_db
)
1806 cupsArrayDelete(dinfo
->cached_db
);
1808 dinfo
->cached_db
= cupsArrayNew(NULL
, NULL
);
1809 dinfo
->cached_flags
= flags
;
1811 if (flags
& CUPS_MEDIA_FLAGS_READY
)
1813 DEBUG_puts("4cups_create_cached: ready media");
1815 cups_update_ready(http
, dinfo
);
1816 db
= dinfo
->ready_db
;
1820 DEBUG_puts("4cups_create_cached: supported media");
1822 if (!dinfo
->media_db
)
1823 cups_create_media_db(dinfo
, CUPS_MEDIA_FLAGS_DEFAULT
);
1825 db
= dinfo
->media_db
;
1828 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
), first
= mdb
;
1830 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1832 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
));
1834 if (flags
& CUPS_MEDIA_FLAGS_BORDERLESS
)
1836 if (!mdb
->left
&& !mdb
->right
&& !mdb
->top
&& !mdb
->bottom
)
1838 DEBUG_printf(("4cups_create_cached: add %p", (void *)mdb
));
1839 cupsArrayAdd(dinfo
->cached_db
, mdb
);
1842 else if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
1844 if (first
->width
!= mdb
->width
|| first
->length
!= mdb
->length
)
1846 DEBUG_printf(("4cups_create_cached: add %p", (void *)first
));
1847 cupsArrayAdd(dinfo
->cached_db
, first
);
1850 else if (mdb
->left
>= first
->left
&& mdb
->right
>= first
->right
&& mdb
->top
>= first
->top
&& mdb
->bottom
>= first
->bottom
&&
1851 (mdb
->left
!= first
->left
|| mdb
->right
!= first
->right
|| mdb
->top
!= first
->top
|| mdb
->bottom
!= first
->bottom
))
1856 DEBUG_printf(("4cups_create_cached: add %p", (void *)mdb
));
1857 cupsArrayAdd(dinfo
->cached_db
, mdb
);
1861 if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
1863 DEBUG_printf(("4cups_create_cached: add %p", (void *)first
));
1864 cupsArrayAdd(dinfo
->cached_db
, first
);
1870 * 'cups_create_constraints()' - Create the constraints and resolvers arrays.
1874 cups_create_constraints(
1875 cups_dinfo_t
*dinfo
) /* I - Destination information */
1877 int i
; /* Looping var */
1878 ipp_attribute_t
*attr
; /* Attribute */
1879 _ipp_value_t
*val
; /* Current value */
1882 dinfo
->constraints
= cupsArrayNew3(NULL
, NULL
, NULL
, 0, NULL
,
1883 (cups_afree_func_t
)free
);
1884 dinfo
->resolvers
= cupsArrayNew3((cups_array_func_t
)cups_compare_dconstres
,
1885 NULL
, NULL
, 0, NULL
,
1886 (cups_afree_func_t
)free
);
1888 if ((attr
= ippFindAttribute(dinfo
->attrs
, "job-constraints-supported",
1889 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1891 for (i
= attr
->num_values
, val
= attr
->values
; i
> 0; i
--, val
++)
1892 cups_add_dconstres(dinfo
->constraints
, val
->collection
);
1895 if ((attr
= ippFindAttribute(dinfo
->attrs
, "job-resolvers-supported",
1896 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1898 for (i
= attr
->num_values
, val
= attr
->values
; i
> 0; i
--, val
++)
1899 cups_add_dconstres(dinfo
->resolvers
, val
->collection
);
1905 * 'cups_create_defaults()' - Create the -default option array.
1909 cups_create_defaults(
1910 cups_dinfo_t
*dinfo
) /* I - Destination information */
1912 ipp_attribute_t
*attr
; /* Current attribute */
1913 char name
[IPP_MAX_NAME
+ 1],
1915 *nameptr
, /* Pointer into current name */
1916 value
[2048]; /* Current value */
1920 * Iterate through the printer attributes looking for xxx-default and adding
1921 * xxx=value to the defaults option array.
1924 for (attr
= ippFirstAttribute(dinfo
->attrs
); attr
; attr
= ippNextAttribute(dinfo
->attrs
))
1926 if (!ippGetName(attr
) || ippGetGroupTag(attr
) != IPP_TAG_PRINTER
)
1929 strlcpy(name
, ippGetName(attr
), sizeof(name
));
1930 if ((nameptr
= name
+ strlen(name
) - 8) <= name
|| strcmp(nameptr
, "-default"))
1935 if (ippGetValueTag(attr
) == IPP_TAG_BEGIN_COLLECTION
)
1937 if (cups_collection_string(attr
, value
, sizeof(value
)) >= sizeof(value
))
1940 else if (ippAttributeString(attr
, value
, sizeof(value
)) >= sizeof(value
))
1943 dinfo
->num_defaults
= cupsAddOption(name
, value
, dinfo
->num_defaults
, &dinfo
->defaults
);
1949 * 'cups_create_media_db()' - Create the media database.
1953 cups_create_media_db(
1954 cups_dinfo_t
*dinfo
, /* I - Destination information */
1955 unsigned flags
) /* I - Media flags */
1957 int i
; /* Looping var */
1958 _ipp_value_t
*val
; /* Current value */
1959 ipp_attribute_t
*media_col_db
, /* media-col-database */
1960 *media_attr
, /* media-xxx */
1961 *x_dimension
, /* x-dimension */
1962 *y_dimension
; /* y-dimension */
1963 pwg_media_t
*pwg
; /* PWG media info */
1964 cups_array_t
*db
; /* New media database array */
1965 _cups_media_db_t mdb
; /* Media entry */
1966 char media_key
[256]; /* Synthesized media-key value */
1969 db
= cupsArrayNew3((cups_array_func_t
)cups_compare_media_db
,
1971 (cups_acopy_func_t
)cups_copy_media_db
,
1972 (cups_afree_func_t
)cups_free_media_db
);
1974 if (flags
== CUPS_MEDIA_FLAGS_READY
)
1976 dinfo
->ready_db
= db
;
1978 media_col_db
= ippFindAttribute(dinfo
->ready_attrs
, "media-col-ready",
1979 IPP_TAG_BEGIN_COLLECTION
);
1980 media_attr
= ippFindAttribute(dinfo
->ready_attrs
, "media-ready",
1985 dinfo
->media_db
= db
;
1986 dinfo
->min_size
.width
= INT_MAX
;
1987 dinfo
->min_size
.length
= INT_MAX
;
1988 dinfo
->max_size
.width
= 0;
1989 dinfo
->max_size
.length
= 0;
1991 media_col_db
= ippFindAttribute(dinfo
->attrs
, "media-col-database",
1992 IPP_TAG_BEGIN_COLLECTION
);
1993 media_attr
= ippFindAttribute(dinfo
->attrs
, "media-supported",
1999 _ipp_value_t
*custom
= NULL
; /* Custom size range value */
2001 for (i
= media_col_db
->num_values
, val
= media_col_db
->values
;
2005 memset(&mdb
, 0, sizeof(mdb
));
2007 if ((media_attr
= ippFindAttribute(val
->collection
, "media-size",
2008 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
2010 ipp_t
*media_size
= media_attr
->values
[0].collection
;
2011 /* media-size collection value */
2013 if ((x_dimension
= ippFindAttribute(media_size
, "x-dimension",
2014 IPP_TAG_INTEGER
)) != NULL
&&
2015 (y_dimension
= ippFindAttribute(media_size
, "y-dimension",
2016 IPP_TAG_INTEGER
)) != NULL
)
2022 mdb
.width
= x_dimension
->values
[0].integer
;
2023 mdb
.length
= y_dimension
->values
[0].integer
;
2025 else if ((x_dimension
= ippFindAttribute(media_size
, "x-dimension",
2026 IPP_TAG_INTEGER
)) != NULL
&&
2027 (y_dimension
= ippFindAttribute(media_size
, "y-dimension",
2028 IPP_TAG_RANGE
)) != NULL
)
2034 mdb
.width
= x_dimension
->values
[0].integer
;
2035 mdb
.length
= y_dimension
->values
[0].range
.upper
;
2037 else if (flags
!= CUPS_MEDIA_FLAGS_READY
&&
2038 (x_dimension
= ippFindAttribute(media_size
, "x-dimension",
2039 IPP_TAG_RANGE
)) != NULL
&&
2040 (y_dimension
= ippFindAttribute(media_size
, "y-dimension",
2041 IPP_TAG_RANGE
)) != NULL
)
2044 * Custom size range; save this as the custom size value with default
2045 * margins, then continue; we'll capture the real margins below...
2050 dinfo
->min_size
.width
= x_dimension
->values
[0].range
.lower
;
2051 dinfo
->min_size
.length
= y_dimension
->values
[0].range
.lower
;
2052 dinfo
->min_size
.left
=
2053 dinfo
->min_size
.right
= 635; /* Default 1/4" side margins */
2054 dinfo
->min_size
.top
=
2055 dinfo
->min_size
.bottom
= 1270; /* Default 1/2" top/bottom margins */
2057 dinfo
->max_size
.width
= x_dimension
->values
[0].range
.upper
;
2058 dinfo
->max_size
.length
= y_dimension
->values
[0].range
.upper
;
2059 dinfo
->max_size
.left
=
2060 dinfo
->max_size
.right
= 635; /* Default 1/4" side margins */
2061 dinfo
->max_size
.top
=
2062 dinfo
->max_size
.bottom
= 1270; /* Default 1/2" top/bottom margins */
2067 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
))
2068 mdb
.color
= media_attr
->values
[0].string
.text
;
2070 if ((media_attr
= ippFindAttribute(val
->collection
, "media-info", IPP_TAG_TEXT
)) != NULL
)
2071 mdb
.info
= media_attr
->values
[0].string
.text
;
2073 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
))
2074 mdb
.key
= media_attr
->values
[0].string
.text
;
2076 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
))
2077 mdb
.size_name
= media_attr
->values
[0].string
.text
;
2079 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
))
2080 mdb
.source
= media_attr
->values
[0].string
.text
;
2082 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
))
2083 mdb
.type
= media_attr
->values
[0].string
.text
;
2085 if ((media_attr
= ippFindAttribute(val
->collection
, "media-bottom-margin", IPP_TAG_INTEGER
)) != NULL
)
2086 mdb
.bottom
= media_attr
->values
[0].integer
;
2088 if ((media_attr
= ippFindAttribute(val
->collection
, "media-left-margin", IPP_TAG_INTEGER
)) != NULL
)
2089 mdb
.left
= media_attr
->values
[0].integer
;
2091 if ((media_attr
= ippFindAttribute(val
->collection
, "media-right-margin", IPP_TAG_INTEGER
)) != NULL
)
2092 mdb
.right
= media_attr
->values
[0].integer
;
2094 if ((media_attr
= ippFindAttribute(val
->collection
, "media-top-margin", IPP_TAG_INTEGER
)) != NULL
)
2095 mdb
.top
= media_attr
->values
[0].integer
;
2099 if (!mdb
.size_name
&& (pwg
= pwgMediaForSize(mdb
.width
, mdb
.length
)) != NULL
)
2100 mdb
.size_name
= (char *)pwg
->pwg
;
2105 * Use a CUPS-specific identifier if we don't have a size name...
2108 if (flags
& CUPS_MEDIA_FLAGS_READY
)
2109 snprintf(media_key
, sizeof(media_key
), "cups-media-ready-%d", i
+ 1);
2111 snprintf(media_key
, sizeof(media_key
), "cups-media-%d", i
+ 1);
2113 else if (mdb
.source
)
2116 * Generate key using size name, source, and type (if set)...
2120 snprintf(media_key
, sizeof(media_key
), "%s_%s_%s", mdb
.size_name
, mdb
.source
, mdb
.type
);
2122 snprintf(media_key
, sizeof(media_key
), "%s_%s", mdb
.size_name
, mdb
.source
);
2127 * Generate key using size name and type...
2130 snprintf(media_key
, sizeof(media_key
), "%s_%s", mdb
.size_name
, mdb
.type
);
2135 * Key is just the size name...
2138 strlcpy(media_key
, mdb
.size_name
, sizeof(media_key
));
2142 * Append "_borderless" for borderless media...
2145 if (!mdb
.bottom
&& !mdb
.left
&& !mdb
.right
&& !mdb
.top
)
2146 strlcat(media_key
, "_borderless", sizeof(media_key
));
2148 mdb
.key
= media_key
;
2151 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
));
2153 cupsArrayAdd(db
, &mdb
);
2158 if ((media_attr
= ippFindAttribute(custom
->collection
,
2159 "media-bottom-margin",
2160 IPP_TAG_INTEGER
)) != NULL
)
2162 dinfo
->min_size
.top
=
2163 dinfo
->max_size
.top
= media_attr
->values
[0].integer
;
2166 if ((media_attr
= ippFindAttribute(custom
->collection
,
2167 "media-left-margin",
2168 IPP_TAG_INTEGER
)) != NULL
)
2170 dinfo
->min_size
.left
=
2171 dinfo
->max_size
.left
= media_attr
->values
[0].integer
;
2174 if ((media_attr
= ippFindAttribute(custom
->collection
,
2175 "media-right-margin",
2176 IPP_TAG_INTEGER
)) != NULL
)
2178 dinfo
->min_size
.right
=
2179 dinfo
->max_size
.right
= media_attr
->values
[0].integer
;
2182 if ((media_attr
= ippFindAttribute(custom
->collection
,
2184 IPP_TAG_INTEGER
)) != NULL
)
2186 dinfo
->min_size
.top
=
2187 dinfo
->max_size
.top
= media_attr
->values
[0].integer
;
2191 else if (media_attr
&&
2192 (media_attr
->value_tag
== IPP_TAG_NAME
||
2193 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
2194 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
2196 memset(&mdb
, 0, sizeof(mdb
));
2199 mdb
.right
= 635; /* Default 1/4" side margins */
2201 mdb
.bottom
= 1270; /* Default 1/2" top/bottom margins */
2203 for (i
= media_attr
->num_values
, val
= media_attr
->values
;
2207 if ((pwg
= pwgMediaForPWG(val
->string
.text
)) == NULL
)
2208 if ((pwg
= pwgMediaForLegacy(val
->string
.text
)) == NULL
)
2210 DEBUG_printf(("3cups_create_media_db: Ignoring unknown size '%s'.",
2215 mdb
.width
= pwg
->width
;
2216 mdb
.length
= pwg
->length
;
2218 if (flags
!= CUPS_MEDIA_FLAGS_READY
&&
2219 !strncmp(val
->string
.text
, "custom_min_", 11))
2221 mdb
.size_name
= NULL
;
2222 dinfo
->min_size
= mdb
;
2224 else if (flags
!= CUPS_MEDIA_FLAGS_READY
&&
2225 !strncmp(val
->string
.text
, "custom_max_", 11))
2227 mdb
.size_name
= NULL
;
2228 dinfo
->max_size
= mdb
;
2232 mdb
.size_name
= val
->string
.text
;
2234 cupsArrayAdd(db
, &mdb
);
2242 * 'cups_free_media_cb()' - Free a media entry.
2247 _cups_media_db_t
*mdb
) /* I - Media entry to free */
2250 _cupsStrFree(mdb
->color
);
2252 _cupsStrFree(mdb
->key
);
2254 _cupsStrFree(mdb
->info
);
2256 _cupsStrFree(mdb
->size_name
);
2258 _cupsStrFree(mdb
->source
);
2260 _cupsStrFree(mdb
->type
);
2267 * 'cups_get_media_db()' - Lookup the media entry for a given size.
2270 static int /* O - 1 on match, 0 on failure */
2271 cups_get_media_db(http_t
*http
, /* I - Connection to destination */
2272 cups_dinfo_t
*dinfo
, /* I - Destination information */
2273 pwg_media_t
*pwg
, /* I - PWG media info */
2274 unsigned flags
, /* I - Media matching flags */
2275 cups_size_t
*size
) /* O - Media size/margin/name info */
2277 cups_array_t
*db
; /* Which media database to query */
2278 _cups_media_db_t
*mdb
, /* Current media database entry */
2279 *best
= NULL
, /* Best matching entry */
2280 key
; /* Search key */
2284 * Create the media database as needed...
2287 if (flags
& CUPS_MEDIA_FLAGS_READY
)
2289 cups_update_ready(http
, dinfo
);
2290 db
= dinfo
->ready_db
;
2294 if (!dinfo
->media_db
)
2295 cups_create_media_db(dinfo
, CUPS_MEDIA_FLAGS_DEFAULT
);
2297 db
= dinfo
->media_db
;
2304 memset(&key
, 0, sizeof(key
));
2305 key
.width
= pwg
->width
;
2306 key
.length
= pwg
->length
;
2308 if ((mdb
= cupsArrayFind(db
, &key
)) != NULL
)
2311 * Found an exact match, let's figure out the best margins for the flags
2317 if (flags
& CUPS_MEDIA_FLAGS_BORDERLESS
)
2320 * Look for the smallest margins...
2323 if (best
->left
!= 0 || best
->right
!= 0 || best
->top
!= 0 || best
->bottom
!= 0)
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
)
2333 if (mdb
->left
== 0 && mdb
->right
== 0 && mdb
->bottom
== 0 &&
2341 * If we need an exact match, return no-match if the size is not
2345 if ((flags
& CUPS_MEDIA_FLAGS_EXACT
) &&
2346 (best
->left
|| best
->right
|| best
->top
|| best
->bottom
))
2349 else if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
2352 * Look for the largest margins...
2355 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
2356 mdb
&& !cups_compare_media_db(mdb
, &key
);
2357 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2359 if (mdb
->left
>= best
->left
&& mdb
->right
>= best
->right
&&
2360 mdb
->top
>= best
->top
&& mdb
->bottom
>= best
->bottom
&&
2361 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
2368 * Look for the smallest non-zero margins...
2371 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
2372 mdb
&& !cups_compare_media_db(mdb
, &key
);
2373 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2375 if (((mdb
->left
> 0 && mdb
->left
<= best
->left
) || best
->left
== 0) &&
2376 ((mdb
->right
> 0 && mdb
->right
<= best
->right
) || best
->right
== 0) &&
2377 ((mdb
->top
> 0 && mdb
->top
<= best
->top
) || best
->top
== 0) &&
2378 ((mdb
->bottom
> 0 && mdb
->bottom
<= best
->bottom
) || best
->bottom
== 0) &&
2379 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
2384 else if (flags
& CUPS_MEDIA_FLAGS_EXACT
)
2387 * See if we can do this as a custom size...
2390 if (pwg
->width
< dinfo
->min_size
.width
||
2391 pwg
->width
> dinfo
->max_size
.width
||
2392 pwg
->length
< dinfo
->min_size
.length
||
2393 pwg
->length
> dinfo
->max_size
.length
)
2394 return (0); /* Out of range */
2396 if ((flags
& CUPS_MEDIA_FLAGS_BORDERLESS
) &&
2397 (dinfo
->min_size
.left
> 0 || dinfo
->min_size
.right
> 0 ||
2398 dinfo
->min_size
.top
> 0 || dinfo
->min_size
.bottom
> 0))
2399 return (0); /* Not borderless */
2401 key
.size_name
= (char *)pwg
->pwg
;
2402 key
.bottom
= dinfo
->min_size
.bottom
;
2403 key
.left
= dinfo
->min_size
.left
;
2404 key
.right
= dinfo
->min_size
.right
;
2405 key
.top
= dinfo
->min_size
.top
;
2409 else if (pwg
->width
>= dinfo
->min_size
.width
&&
2410 pwg
->width
<= dinfo
->max_size
.width
&&
2411 pwg
->length
>= dinfo
->min_size
.length
&&
2412 pwg
->length
<= dinfo
->max_size
.length
)
2415 * Map to custom size...
2418 key
.size_name
= (char *)pwg
->pwg
;
2419 key
.bottom
= dinfo
->min_size
.bottom
;
2420 key
.left
= dinfo
->min_size
.left
;
2421 key
.right
= dinfo
->min_size
.right
;
2422 key
.top
= dinfo
->min_size
.top
;
2429 * Find a close size...
2432 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
);
2434 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2435 if (cups_is_close_media_db(mdb
, &key
))
2443 if (flags
& CUPS_MEDIA_FLAGS_BORDERLESS
)
2446 * Look for the smallest margins...
2449 if (best
->left
!= 0 || best
->right
!= 0 || best
->top
!= 0 ||
2452 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
2453 mdb
&& cups_is_close_media_db(mdb
, &key
);
2454 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2456 if (mdb
->left
<= best
->left
&& mdb
->right
<= best
->right
&&
2457 mdb
->top
<= best
->top
&& mdb
->bottom
<= best
->bottom
&&
2458 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
2461 if (mdb
->left
== 0 && mdb
->right
== 0 && mdb
->bottom
== 0 &&
2468 else if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
2471 * Look for the largest margins...
2474 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
2475 mdb
&& cups_is_close_media_db(mdb
, &key
);
2476 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2478 if (mdb
->left
>= best
->left
&& mdb
->right
>= best
->right
&&
2479 mdb
->top
>= best
->top
&& mdb
->bottom
>= best
->bottom
&&
2480 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
2487 * Look for the smallest non-zero margins...
2490 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
2491 mdb
&& cups_is_close_media_db(mdb
, &key
);
2492 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2494 if (((mdb
->left
> 0 && mdb
->left
<= best
->left
) || best
->left
== 0) &&
2495 ((mdb
->right
> 0 && mdb
->right
<= best
->right
) ||
2496 best
->right
== 0) &&
2497 ((mdb
->top
> 0 && mdb
->top
<= best
->top
) || best
->top
== 0) &&
2498 ((mdb
->bottom
> 0 && mdb
->bottom
<= best
->bottom
) ||
2499 best
->bottom
== 0) &&
2500 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
2509 * Return the matching size...
2513 strlcpy(size
->media
, best
->key
, sizeof(size
->media
));
2514 else if (best
->size_name
)
2515 strlcpy(size
->media
, best
->size_name
, sizeof(size
->media
));
2517 strlcpy(size
->media
, pwg
->pwg
, sizeof(size
->media
));
2519 size
->width
= best
->width
;
2520 size
->length
= best
->length
;
2521 size
->bottom
= best
->bottom
;
2522 size
->left
= best
->left
;
2523 size
->right
= best
->right
;
2524 size
->top
= best
->top
;
2534 * 'cups_is_close_media_db()' - Compare two media entries to see if they are
2535 * close to the same size.
2537 * Currently we use 5 points (from PostScript) as the matching range...
2540 static int /* O - 1 if the sizes are close */
2541 cups_is_close_media_db(
2542 _cups_media_db_t
*a
, /* I - First media entries */
2543 _cups_media_db_t
*b
) /* I - Second media entries */
2545 int dwidth
, /* Difference in width */
2546 dlength
; /* Difference in length */
2549 dwidth
= a
->width
- b
->width
;
2550 dlength
= a
->length
- b
->length
;
2552 return (dwidth
>= -176 && dwidth
<= 176 &&
2553 dlength
>= -176 && dlength
<= 176);
2558 * 'cups_test_constraints()' - Test constraints.
2561 static cups_array_t
* /* O - Active constraints */
2562 cups_test_constraints(
2563 cups_dinfo_t
*dinfo
, /* I - Destination information */
2564 const char *new_option
, /* I - Newly selected option */
2565 const char *new_value
, /* I - Newly selected value */
2566 int num_options
, /* I - Number of options */
2567 cups_option_t
*options
, /* I - Options */
2568 int *num_conflicts
, /* O - Number of conflicting options */
2569 cups_option_t
**conflicts
) /* O - Conflicting options */
2571 int i
, /* Looping var */
2572 count
, /* Number of values */
2573 match
; /* Value matches? */
2574 int num_matching
; /* Number of matching options */
2575 cups_option_t
*matching
; /* Matching options */
2576 _cups_dconstres_t
*c
; /* Current constraint */
2577 cups_array_t
*active
= NULL
; /* Active constraints */
2578 ipp_t
*col
; /* Collection value */
2579 ipp_attribute_t
*attr
; /* Current attribute */
2580 _ipp_value_t
*attrval
; /* Current attribute value */
2581 const char *value
; /* Current value */
2582 char temp
[1024]; /* Temporary string */
2583 int int_value
; /* Integer value */
2584 int xres_value
, /* Horizontal resolution */
2585 yres_value
; /* Vertical resolution */
2586 ipp_res_t units_value
; /* Resolution units */
2589 for (c
= (_cups_dconstres_t
*)cupsArrayFirst(dinfo
->constraints
);
2591 c
= (_cups_dconstres_t
*)cupsArrayNext(dinfo
->constraints
))
2596 for (attr
= ippFirstAttribute(c
->collection
);
2598 attr
= ippNextAttribute(c
->collection
))
2601 * Get the value for the current attribute in the constraint...
2604 if (new_option
&& new_value
&& !strcmp(attr
->name
, new_option
))
2606 else if ((value
= cupsGetOption(attr
->name
, num_options
, options
)) == NULL
)
2607 value
= cupsGetOption(attr
->name
, dinfo
->num_defaults
, dinfo
->defaults
);
2612 * Not set so this constraint does not apply...
2620 switch (attr
->value_tag
)
2622 case IPP_TAG_INTEGER
:
2624 int_value
= atoi(value
);
2626 for (i
= attr
->num_values
, attrval
= attr
->values
;
2630 if (attrval
->integer
== int_value
)
2638 case IPP_TAG_BOOLEAN
:
2639 int_value
= !strcmp(value
, "true");
2641 for (i
= attr
->num_values
, attrval
= attr
->values
;
2645 if (attrval
->boolean
== int_value
)
2653 case IPP_TAG_RANGE
:
2654 int_value
= atoi(value
);
2656 for (i
= attr
->num_values
, attrval
= attr
->values
;
2660 if (int_value
>= attrval
->range
.lower
&&
2661 int_value
<= attrval
->range
.upper
)
2669 case IPP_TAG_RESOLUTION
:
2670 if (sscanf(value
, "%dx%d%15s", &xres_value
, &yres_value
, temp
) != 3)
2672 if (sscanf(value
, "%d%15s", &xres_value
, temp
) != 2)
2675 yres_value
= xres_value
;
2678 if (!strcmp(temp
, "dpi"))
2679 units_value
= IPP_RES_PER_INCH
;
2680 else if (!strcmp(temp
, "dpc") || !strcmp(temp
, "dpcm"))
2681 units_value
= IPP_RES_PER_CM
;
2685 for (i
= attr
->num_values
, attrval
= attr
->values
;
2689 if (attrval
->resolution
.xres
== xres_value
&&
2690 attrval
->resolution
.yres
== yres_value
&&
2691 attrval
->resolution
.units
== units_value
)
2701 case IPP_TAG_KEYWORD
:
2702 case IPP_TAG_CHARSET
:
2704 case IPP_TAG_URISCHEME
:
2705 case IPP_TAG_MIMETYPE
:
2706 case IPP_TAG_LANGUAGE
:
2707 case IPP_TAG_TEXTLANG
:
2708 case IPP_TAG_NAMELANG
:
2709 for (i
= attr
->num_values
, attrval
= attr
->values
;
2713 if (!strcmp(attrval
->string
.text
, value
))
2721 case IPP_TAG_BEGIN_COLLECTION
:
2723 _cupsEncodeOption(col
, IPP_TAG_ZERO
, NULL
, ippGetName(attr
), value
);
2725 for (i
= 0, count
= ippGetCount(attr
); i
< count
; i
++)
2727 if (cups_collection_contains(col
, ippGetCollection(attr
, i
)))
2744 num_matching
= cupsAddOption(attr
->name
, value
, num_matching
, &matching
);
2750 active
= cupsArrayNew(NULL
, NULL
);
2752 cupsArrayAdd(active
, c
);
2754 if (num_conflicts
&& conflicts
)
2756 cups_option_t
*moption
; /* Matching option */
2758 for (i
= num_matching
, moption
= matching
; i
> 0; i
--, moption
++)
2759 *num_conflicts
= cupsAddOption(moption
->name
, moption
->value
, *num_conflicts
, conflicts
);
2763 cupsFreeOptions(num_matching
, matching
);
2771 * 'cups_update_ready()' - Update xxx-ready attributes for the printer.
2775 cups_update_ready(http_t
*http
, /* I - Connection to destination */
2776 cups_dinfo_t
*dinfo
) /* I - Destination information */
2778 ipp_t
*request
; /* Get-Printer-Attributes request */
2779 static const char * const pattrs
[] = /* Printer attributes we want */
2781 "finishings-col-ready",
2783 "job-finishings-col-ready",
2784 "job-finishings-ready",
2791 * Don't update more than once every 30 seconds...
2794 if ((time(NULL
) - dinfo
->ready_time
) < _CUPS_MEDIA_READY_TTL
)
2798 * Free any previous results...
2801 if (dinfo
->cached_flags
& CUPS_MEDIA_FLAGS_READY
)
2803 cupsArrayDelete(dinfo
->cached_db
);
2804 dinfo
->cached_db
= NULL
;
2805 dinfo
->cached_flags
= CUPS_MEDIA_FLAGS_DEFAULT
;
2808 ippDelete(dinfo
->ready_attrs
);
2809 dinfo
->ready_attrs
= NULL
;
2811 cupsArrayDelete(dinfo
->ready_db
);
2812 dinfo
->ready_db
= NULL
;
2815 * Query the xxx-ready values...
2818 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
2819 ippSetVersion(request
, dinfo
->version
/ 10, dinfo
->version
% 10);
2821 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
2823 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
2825 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "requested-attributes", (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
2827 dinfo
->ready_attrs
= cupsDoRequest(http
, request
, dinfo
->resource
);
2830 * Update the ready media database...
2833 cups_create_media_db(dinfo
, CUPS_MEDIA_FLAGS_READY
);
2836 * Update last lookup time and return...
2839 dinfo
->ready_time
= time(NULL
);