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"
21 #define _CUPS_MEDIA_READY_TTL 30 /* Life of xxx-ready values */
28 static void cups_add_dconstres(cups_array_t
*a
, ipp_t
*collection
);
29 static int cups_collection_contains(ipp_t
*test
, ipp_t
*match
);
30 static size_t cups_collection_string(ipp_attribute_t
*attr
, char *buffer
, size_t bufsize
);
31 static int cups_compare_dconstres(_cups_dconstres_t
*a
,
32 _cups_dconstres_t
*b
);
33 static int cups_compare_media_db(_cups_media_db_t
*a
,
35 static _cups_media_db_t
*cups_copy_media_db(_cups_media_db_t
*mdb
);
36 static void cups_create_cached(http_t
*http
, cups_dinfo_t
*dinfo
,
38 static void cups_create_constraints(cups_dinfo_t
*dinfo
);
39 static void cups_create_defaults(cups_dinfo_t
*dinfo
);
40 static void cups_create_media_db(cups_dinfo_t
*dinfo
,
42 static void cups_free_media_db(_cups_media_db_t
*mdb
);
43 static int cups_get_media_db(http_t
*http
, cups_dinfo_t
*dinfo
,
44 pwg_media_t
*pwg
, unsigned flags
,
46 static int cups_is_close_media_db(_cups_media_db_t
*a
,
48 static cups_array_t
*cups_test_constraints(cups_dinfo_t
*dinfo
,
49 const char *new_option
,
50 const char *new_value
,
52 cups_option_t
*options
,
54 cups_option_t
**conflicts
);
55 static void cups_update_ready(http_t
*http
, cups_dinfo_t
*dinfo
);
59 * 'cupsAddDestMediaOptions()' - Add the option corresponding to the specified media size.
64 int /* O - New number of options */
65 cupsAddDestMediaOptions(
66 http_t
*http
, /* I - Connection to destination */
67 cups_dest_t
*dest
, /* I - Destination */
68 cups_dinfo_t
*dinfo
, /* I - Destination information */
69 unsigned flags
, /* I - Media matching flags */
70 cups_size_t
*size
, /* I - Media size */
71 int num_options
, /* I - Current number of options */
72 cups_option_t
**options
) /* IO - Options */
74 cups_array_t
*db
; /* Media database */
75 _cups_media_db_t
*mdb
; /* Media database entry */
76 char value
[2048]; /* Option value */
80 * Range check input...
83 if (!http
|| !dest
|| !dinfo
|| !size
|| !options
)
85 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
90 * Find the matching media size...
93 if (flags
& CUPS_MEDIA_FLAGS_READY
)
98 DEBUG_printf(("1cupsAddDestMediaOptions: size->media=\"%s\"", size
->media
));
100 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
); mdb
; mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
102 if (mdb
->key
&& !strcmp(mdb
->key
, size
->media
))
104 else if (mdb
->size_name
&& !strcmp(mdb
->size_name
, size
->media
))
110 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
); mdb
; mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
112 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
)
119 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
); mdb
; mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
121 if (mdb
->width
== size
->width
&& mdb
->length
== size
->length
)
128 DEBUG_puts("1cupsAddDestMediaOptions: Unable to find matching size.");
129 return (num_options
);
132 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
));
137 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
);
139 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
);
143 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
);
147 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
);
150 num_options
= cupsAddOption("media-col", value
, num_options
, options
);
152 return (num_options
);
157 * 'cupsCheckDestSupported()' - Check that the option and value are supported
158 * by the destination.
160 * Returns 1 if supported, 0 otherwise.
162 * @since CUPS 1.6/macOS 10.8@
165 int /* O - 1 if supported, 0 otherwise */
166 cupsCheckDestSupported(
167 http_t
*http
, /* I - Connection to destination */
168 cups_dest_t
*dest
, /* I - Destination */
169 cups_dinfo_t
*dinfo
, /* I - Destination information */
170 const char *option
, /* I - Option */
171 const char *value
) /* I - Value or @code NULL@ */
173 int i
; /* Looping var */
174 char temp
[1024]; /* Temporary string */
175 int int_value
; /* Integer value */
176 int xres_value
, /* Horizontal resolution */
177 yres_value
; /* Vertical resolution */
178 ipp_res_t units_value
; /* Resolution units */
179 ipp_attribute_t
*attr
; /* Attribute */
180 _ipp_value_t
*attrval
; /* Current attribute value */
184 * Get the default connection as needed...
188 http
= _cupsConnect();
191 * Range check input...
194 if (!http
|| !dest
|| !dinfo
|| !option
)
198 * Lookup the attribute...
201 if (strstr(option
, "-supported"))
202 attr
= ippFindAttribute(dinfo
->attrs
, option
, IPP_TAG_ZERO
);
205 snprintf(temp
, sizeof(temp
), "%s-supported", option
);
206 attr
= ippFindAttribute(dinfo
->attrs
, temp
, IPP_TAG_ZERO
);
219 if (!strcmp(option
, "media") && !strncmp(value
, "custom_", 7))
222 * Check range of custom media sizes...
225 pwg_media_t
*pwg
; /* Current PWG media size info */
226 int min_width
, /* Minimum width */
227 min_length
, /* Minimum length */
228 max_width
, /* Maximum width */
229 max_length
; /* Maximum length */
232 * Get the minimum and maximum size...
235 min_width
= min_length
= INT_MAX
;
236 max_width
= max_length
= 0;
238 for (i
= attr
->num_values
, attrval
= attr
->values
;
242 if (!strncmp(attrval
->string
.text
, "custom_min_", 11) &&
243 (pwg
= pwgMediaForPWG(attrval
->string
.text
)) != NULL
)
245 min_width
= pwg
->width
;
246 min_length
= pwg
->length
;
248 else if (!strncmp(attrval
->string
.text
, "custom_max_", 11) &&
249 (pwg
= pwgMediaForPWG(attrval
->string
.text
)) != NULL
)
251 max_width
= pwg
->width
;
252 max_length
= pwg
->length
;
260 if (min_width
< INT_MAX
&& max_width
> 0 &&
261 (pwg
= pwgMediaForPWG(value
)) != NULL
&&
262 pwg
->width
>= min_width
&& pwg
->width
<= max_width
&&
263 pwg
->length
>= min_length
&& pwg
->length
<= max_length
)
269 * Check literal values...
272 switch (attr
->value_tag
)
274 case IPP_TAG_INTEGER
:
276 int_value
= atoi(value
);
278 for (i
= 0; i
< attr
->num_values
; i
++)
279 if (attr
->values
[i
].integer
== int_value
)
283 case IPP_TAG_BOOLEAN
:
284 return (attr
->values
[0].boolean
);
287 int_value
= atoi(value
);
289 for (i
= 0; i
< attr
->num_values
; i
++)
290 if (int_value
>= attr
->values
[i
].range
.lower
&&
291 int_value
<= attr
->values
[i
].range
.upper
)
295 case IPP_TAG_RESOLUTION
:
296 if (sscanf(value
, "%dx%d%15s", &xres_value
, &yres_value
, temp
) != 3)
298 if (sscanf(value
, "%d%15s", &xres_value
, temp
) != 2)
301 yres_value
= xres_value
;
304 if (!strcmp(temp
, "dpi"))
305 units_value
= IPP_RES_PER_INCH
;
306 else if (!strcmp(temp
, "dpc") || !strcmp(temp
, "dpcm"))
307 units_value
= IPP_RES_PER_CM
;
311 for (i
= attr
->num_values
, attrval
= attr
->values
;
315 if (attrval
->resolution
.xres
== xres_value
&&
316 attrval
->resolution
.yres
== yres_value
&&
317 attrval
->resolution
.units
== units_value
)
324 case IPP_TAG_KEYWORD
:
325 case IPP_TAG_CHARSET
:
327 case IPP_TAG_URISCHEME
:
328 case IPP_TAG_MIMETYPE
:
329 case IPP_TAG_LANGUAGE
:
330 case IPP_TAG_TEXTLANG
:
331 case IPP_TAG_NAMELANG
:
332 for (i
= 0; i
< attr
->num_values
; i
++)
333 if (!strcmp(attr
->values
[i
].string
.text
, value
))
343 * If we get there the option+value is not supported...
351 * 'cupsCopyDestConflicts()' - Get conflicts and resolutions for a new
354 * "num_options" and "options" represent the currently selected options by the
355 * user. "new_option" and "new_value" are the setting the user has just
358 * Returns 1 if there is a conflict, 0 if there are no conflicts, and -1 if
359 * there was an unrecoverable error such as a resolver loop.
361 * If "num_conflicts" and "conflicts" are not @code NULL@, they are set to
362 * contain the list of conflicting option/value pairs. Similarly, if
363 * "num_resolved" and "resolved" are not @code NULL@ they will be set to the
364 * list of changes needed to resolve the conflict.
366 * If cupsCopyDestConflicts returns 1 but "num_resolved" and "resolved" are set
367 * to 0 and @code NULL@, respectively, then the conflict cannot be resolved.
369 * @since CUPS 1.6/macOS 10.8@
372 int /* O - 1 if there is a conflict, 0 if none, -1 on error */
373 cupsCopyDestConflicts(
374 http_t
*http
, /* I - Connection to destination */
375 cups_dest_t
*dest
, /* I - Destination */
376 cups_dinfo_t
*dinfo
, /* I - Destination information */
377 int num_options
, /* I - Number of current options */
378 cups_option_t
*options
, /* I - Current options */
379 const char *new_option
, /* I - New option */
380 const char *new_value
, /* I - New value */
381 int *num_conflicts
, /* O - Number of conflicting options */
382 cups_option_t
**conflicts
, /* O - Conflicting options */
383 int *num_resolved
, /* O - Number of options to resolve */
384 cups_option_t
**resolved
) /* O - Resolved options */
386 int i
, /* Looping var */
387 have_conflicts
= 0, /* Do we have conflicts? */
388 changed
, /* Did we change something? */
389 tries
, /* Number of tries for resolution */
390 num_myconf
= 0, /* My number of conflicting options */
391 num_myres
= 0; /* My number of resolved options */
392 cups_option_t
*myconf
= NULL
, /* My conflicting options */
393 *myres
= NULL
, /* My resolved options */
394 *myoption
, /* My current option */
395 *option
; /* Current option */
396 cups_array_t
*active
= NULL
, /* Active conflicts */
397 *pass
= NULL
, /* Resolvers for this pass */
398 *resolvers
= NULL
, /* Resolvers we have used */
399 *test
; /* Test array for conflicts */
400 _cups_dconstres_t
*c
, /* Current constraint */
401 *r
; /* Current resolver */
402 ipp_attribute_t
*attr
; /* Current attribute */
403 char value
[2048]; /* Current attribute value as string */
404 const char *myvalue
; /* Current value of an option */
408 * Clear returned values...
424 * Get the default connection as needed...
428 http
= _cupsConnect();
431 * Range check input...
434 if (!http
|| !dest
|| !dinfo
||
435 (num_conflicts
!= NULL
) != (conflicts
!= NULL
) ||
436 (num_resolved
!= NULL
) != (resolved
!= NULL
))
440 * Load constraints as needed...
443 if (!dinfo
->constraints
)
444 cups_create_constraints(dinfo
);
446 if (cupsArrayCount(dinfo
->constraints
) == 0)
449 if (!dinfo
->num_defaults
)
450 cups_create_defaults(dinfo
);
453 * If we are resolving, create a shadow array...
458 for (i
= num_options
, option
= options
; i
> 0; i
--, option
++)
459 num_myres
= cupsAddOption(option
->name
, option
->value
, num_myres
, &myres
);
461 if (new_option
&& new_value
)
462 num_myres
= cupsAddOption(new_option
, new_value
, num_myres
, &myres
);
466 num_myres
= num_options
;
471 * Check for any conflicts...
475 pass
= cupsArrayNew((cups_array_func_t
)cups_compare_dconstres
, NULL
);
477 for (tries
= 0; tries
< 100; tries
++)
480 * Check for any conflicts...
483 if (num_conflicts
|| num_resolved
)
485 cupsFreeOptions(num_myconf
, myconf
);
489 active
= cups_test_constraints(dinfo
, new_option
, new_value
,
490 num_myres
, myres
, &num_myconf
,
494 active
= cups_test_constraints(dinfo
, new_option
, new_value
, num_myres
,
497 have_conflicts
= (active
!= NULL
);
499 if (!active
|| !num_resolved
)
500 break; /* All done */
503 * Scan the constraints that were triggered to apply resolvers...
507 resolvers
= cupsArrayNew((cups_array_func_t
)cups_compare_dconstres
, NULL
);
509 for (c
= (_cups_dconstres_t
*)cupsArrayFirst(active
), changed
= 0;
511 c
= (_cups_dconstres_t
*)cupsArrayNext(active
))
513 if (cupsArrayFind(pass
, c
))
514 continue; /* Already applied this resolver... */
516 if (cupsArrayFind(resolvers
, c
))
518 DEBUG_printf(("1cupsCopyDestConflicts: Resolver loop with %s.",
524 if ((r
= cupsArrayFind(dinfo
->resolvers
, c
)) == NULL
)
526 DEBUG_printf(("1cupsCopyDestConflicts: Resolver %s not found.",
533 * Add the options from the resolver...
536 cupsArrayAdd(pass
, r
);
537 cupsArrayAdd(resolvers
, r
);
539 for (attr
= ippFirstAttribute(r
->collection
);
541 attr
= ippNextAttribute(r
->collection
))
543 if (new_option
&& !strcmp(attr
->name
, new_option
))
544 continue; /* Ignore this if we just changed it */
546 if (ippAttributeString(attr
, value
, sizeof(value
)) >= sizeof(value
))
547 continue; /* Ignore if the value is too long */
549 if ((test
= cups_test_constraints(dinfo
, attr
->name
, value
, num_myres
,
550 myres
, NULL
, NULL
)) == NULL
)
553 * That worked, flag it...
559 cupsArrayDelete(test
);
562 * Add the option/value from the resolver regardless of whether it
563 * worked; this makes sure that we can cascade several changes to
564 * make things resolve...
567 num_myres
= cupsAddOption(attr
->name
, value
, num_myres
, &myres
);
573 DEBUG_puts("1cupsCopyDestConflicts: Unable to resolve constraints.");
578 cupsArrayClear(pass
);
580 cupsArrayDelete(active
);
586 DEBUG_puts("1cupsCopyDestConflicts: Unable to resolve after 100 tries.");
592 * Copy resolved options as needed...
597 for (i
= num_myres
, myoption
= myres
; i
> 0; i
--, myoption
++)
599 if ((myvalue
= cupsGetOption(myoption
->name
, num_options
,
601 strcmp(myvalue
, myoption
->value
))
603 if (new_option
&& !strcmp(new_option
, myoption
->name
) &&
604 new_value
&& !strcmp(new_value
, myoption
->value
))
607 *num_resolved
= cupsAddOption(myoption
->name
, myoption
->value
,
608 *num_resolved
, resolved
);
619 cupsArrayDelete(active
);
620 cupsArrayDelete(pass
);
621 cupsArrayDelete(resolvers
);
626 * Free shadow copy of options...
629 cupsFreeOptions(num_myres
, myres
);
635 * Return conflicting options to caller...
638 *num_conflicts
= num_myconf
;
644 * Free conflicting options...
647 cupsFreeOptions(num_myconf
, myconf
);
650 return (have_conflicts
);
655 * 'cupsCopyDestInfo()' - Get the supported values/capabilities for the
658 * The caller is responsible for calling @link cupsFreeDestInfo@ on the return
659 * value. @code NULL@ is returned on error.
661 * @since CUPS 1.6/macOS 10.8@
664 cups_dinfo_t
* /* O - Destination information */
666 http_t
*http
, /* I - Connection to destination */
667 cups_dest_t
*dest
) /* I - Destination */
669 cups_dinfo_t
*dinfo
; /* Destination information */
670 unsigned dflags
; /* Destination flags */
671 ipp_t
*request
, /* Get-Printer-Attributes request */
672 *response
; /* Supported attributes */
673 int tries
, /* Number of tries so far */
674 delay
, /* Current retry delay */
675 prev_delay
; /* Next retry delay */
676 const char *uri
; /* Printer URI */
677 char resource
[1024]; /* Resource path */
678 int version
; /* IPP version */
679 ipp_status_t status
; /* Status of request */
680 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
681 static const char * const requested_attrs
[] =
682 { /* Requested attributes */
684 "media-col-database",
685 "printer-description"
689 DEBUG_printf(("cupsCopyDestInfo(http=%p, dest=%p(%s))", (void *)http
, (void *)dest
, dest
? dest
->name
: ""));
692 * Get the default connection as needed...
697 DEBUG_puts("1cupsCopyDestInfo: Default server connection.");
698 http
= _cupsConnect();
699 dflags
= CUPS_DEST_FLAGS_NONE
;
702 else if (httpAddrFamily(http
->hostaddr
) == AF_LOCAL
)
704 DEBUG_puts("1cupsCopyDestInfo: Connection to server (domain socket).");
705 dflags
= CUPS_DEST_FLAGS_NONE
;
707 #endif /* AF_LOCAL */
708 else if ((strcmp(http
->hostname
, cg
->server
) && cg
->server
[0] != '/') || cg
->ipp_port
!= httpAddrPort(http
->hostaddr
))
710 DEBUG_printf(("1cupsCopyDestInfo: Connection to device (%s).", http
->hostname
));
711 dflags
= CUPS_DEST_FLAGS_DEVICE
;
715 DEBUG_printf(("1cupsCopyDestInfo: Connection to server (%s).", http
->hostname
));
716 dflags
= CUPS_DEST_FLAGS_NONE
;
720 * Range check input...
727 * Get the printer URI and resource path...
730 if ((uri
= _cupsGetDestResource(dest
, dflags
, resource
, sizeof(resource
))) == NULL
)
732 DEBUG_puts("1cupsCopyDestInfo: Unable to get resource.");
737 * Get the supported attributes...
748 * Send a Get-Printer-Attributes request...
751 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
753 ippSetVersion(request
, version
/ 10, version
% 10);
754 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
755 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
756 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", (int)(sizeof(requested_attrs
) / sizeof(requested_attrs
[0])), NULL
, requested_attrs
);
757 response
= cupsDoRequest(http
, request
, resource
);
758 status
= cupsLastError();
760 if (status
> IPP_STATUS_OK_IGNORED_OR_SUBSTITUTED
)
762 DEBUG_printf(("1cupsCopyDestInfo: Get-Printer-Attributes for '%s' returned %s (%s)", dest
->name
, ippErrorString(status
), cupsLastErrorString()));
767 if ((status
== IPP_STATUS_ERROR_BAD_REQUEST
|| status
== IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
) && version
> 11)
771 else if (status
== IPP_STATUS_ERROR_BUSY
)
773 sleep((unsigned)delay
);
775 delay
= _cupsNextDelay(delay
, &prev_delay
);
783 while (!response
&& tries
< 10);
787 DEBUG_puts("1cupsCopyDestInfo: Unable to get printer attributes.");
792 * Allocate a cups_dinfo_t structure and return it...
795 if ((dinfo
= calloc(1, sizeof(cups_dinfo_t
))) == NULL
)
797 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
802 DEBUG_printf(("1cupsCopyDestInfo: version=%d, uri=\"%s\", resource=\"%s\".", version
, uri
, resource
));
804 dinfo
->version
= version
;
806 dinfo
->resource
= _cupsStrAlloc(resource
);
807 dinfo
->attrs
= response
;
814 * 'cupsFindDestDefault()' - Find the default value(s) for the given option.
816 * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
817 * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
818 * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
819 * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
820 * functions to inspect the default value(s) as needed.
822 * @since CUPS 1.7/macOS 10.9@
825 ipp_attribute_t
* /* O - Default attribute or @code NULL@ for none */
827 http_t
*http
, /* I - Connection to destination */
828 cups_dest_t
*dest
, /* I - Destination */
829 cups_dinfo_t
*dinfo
, /* I - Destination information */
830 const char *option
) /* I - Option/attribute name */
832 char name
[IPP_MAX_NAME
]; /* Attribute name */
836 * Get the default connection as needed...
840 http
= _cupsConnect();
843 * Range check input...
846 if (!http
|| !dest
|| !dinfo
|| !option
)
848 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
853 * Find and return the attribute...
856 snprintf(name
, sizeof(name
), "%s-default", option
);
857 return (ippFindAttribute(dinfo
->attrs
, name
, IPP_TAG_ZERO
));
862 * 'cupsFindDestReady()' - Find the default value(s) for the given option.
864 * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
865 * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
866 * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
867 * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
868 * functions to inspect the default value(s) as needed.
870 * @since CUPS 1.7/macOS 10.9@
873 ipp_attribute_t
* /* O - Default attribute or @code NULL@ for none */
875 http_t
*http
, /* I - Connection to destination */
876 cups_dest_t
*dest
, /* I - Destination */
877 cups_dinfo_t
*dinfo
, /* I - Destination information */
878 const char *option
) /* I - Option/attribute name */
880 char name
[IPP_MAX_NAME
]; /* Attribute name */
884 * Get the default connection as needed...
888 http
= _cupsConnect();
891 * Range check input...
894 if (!http
|| !dest
|| !dinfo
|| !option
)
896 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
901 * Find and return the attribute...
904 cups_update_ready(http
, dinfo
);
906 snprintf(name
, sizeof(name
), "%s-ready", option
);
907 return (ippFindAttribute(dinfo
->ready_attrs
, name
, IPP_TAG_ZERO
));
912 * 'cupsFindDestSupported()' - Find the default value(s) for the given option.
914 * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
915 * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
916 * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
917 * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
918 * functions to inspect the default value(s) as needed.
920 * @since CUPS 1.7/macOS 10.9@
923 ipp_attribute_t
* /* O - Default attribute or @code NULL@ for none */
924 cupsFindDestSupported(
925 http_t
*http
, /* I - Connection to destination */
926 cups_dest_t
*dest
, /* I - Destination */
927 cups_dinfo_t
*dinfo
, /* I - Destination information */
928 const char *option
) /* I - Option/attribute name */
930 char name
[IPP_MAX_NAME
]; /* Attribute name */
934 * Get the default connection as needed...
938 http
= _cupsConnect();
941 * Range check input...
944 if (!http
|| !dest
|| !dinfo
|| !option
)
946 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
951 * Find and return the attribute...
954 snprintf(name
, sizeof(name
), "%s-supported", option
);
955 return (ippFindAttribute(dinfo
->attrs
, name
, IPP_TAG_ZERO
));
960 * 'cupsFreeDestInfo()' - Free destination information obtained using
961 * @link cupsCopyDestInfo@.
963 * @since CUPS 1.6/macOS 10.8@
967 cupsFreeDestInfo(cups_dinfo_t
*dinfo
) /* I - Destination information */
970 * Range check input...
977 * Free memory and return...
980 _cupsStrFree(dinfo
->resource
);
982 cupsArrayDelete(dinfo
->constraints
);
983 cupsArrayDelete(dinfo
->resolvers
);
985 cupsArrayDelete(dinfo
->localizations
);
987 cupsArrayDelete(dinfo
->media_db
);
989 cupsArrayDelete(dinfo
->cached_db
);
991 ippDelete(dinfo
->ready_attrs
);
992 cupsArrayDelete(dinfo
->ready_db
);
994 ippDelete(dinfo
->attrs
);
1001 * 'cupsGetDestMediaByIndex()' - Get a media name, dimension, and margins for a
1004 * The @code flags@ parameter determines which set of media are indexed. For
1005 * example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will get the Nth
1006 * borderless size supported by the printer.
1008 * @since CUPS 1.7/macOS 10.9@
1011 int /* O - 1 on success, 0 on failure */
1012 cupsGetDestMediaByIndex(
1013 http_t
*http
, /* I - Connection to destination */
1014 cups_dest_t
*dest
, /* I - Destination */
1015 cups_dinfo_t
*dinfo
, /* I - Destination information */
1016 int n
, /* I - Media size number (0-based) */
1017 unsigned flags
, /* I - Media flags */
1018 cups_size_t
*size
) /* O - Media size information */
1020 _cups_media_db_t
*nsize
; /* Size for N */
1021 pwg_media_t
*pwg
; /* PWG media name for size */
1025 * Get the default connection as needed...
1029 http
= _cupsConnect();
1032 * Range check input...
1036 memset(size
, 0, sizeof(cups_size_t
));
1038 if (!http
|| !dest
|| !dinfo
|| n
< 0 || !size
)
1040 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1045 * Load media list as needed...
1048 if (flags
& CUPS_MEDIA_FLAGS_READY
)
1049 cups_update_ready(http
, dinfo
);
1051 if (!dinfo
->cached_db
|| dinfo
->cached_flags
!= flags
)
1052 cups_create_cached(http
, dinfo
, flags
);
1055 * Copy the size over and return...
1058 if ((nsize
= (_cups_media_db_t
*)cupsArrayIndex(dinfo
->cached_db
, n
)) == NULL
)
1060 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1065 strlcpy(size
->media
, nsize
->key
, sizeof(size
->media
));
1066 else if (nsize
->size_name
)
1067 strlcpy(size
->media
, nsize
->size_name
, sizeof(size
->media
));
1068 else if ((pwg
= pwgMediaForSize(nsize
->width
, nsize
->length
)) != NULL
)
1069 strlcpy(size
->media
, pwg
->pwg
, sizeof(size
->media
));
1072 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1076 size
->width
= nsize
->width
;
1077 size
->length
= nsize
->length
;
1078 size
->bottom
= nsize
->bottom
;
1079 size
->left
= nsize
->left
;
1080 size
->right
= nsize
->right
;
1081 size
->top
= nsize
->top
;
1088 * 'cupsGetDestMediaByName()' - Get media names, dimensions, and margins.
1090 * The "media" string is a PWG media name. "Flags" provides some matching
1091 * guidance (multiple flags can be combined):
1093 * CUPS_MEDIA_FLAGS_DEFAULT = find the closest size supported by the printer,
1094 * CUPS_MEDIA_FLAGS_BORDERLESS = find a borderless size,
1095 * CUPS_MEDIA_FLAGS_DUPLEX = find a size compatible with 2-sided printing,
1096 * CUPS_MEDIA_FLAGS_EXACT = find an exact match for the size, and
1097 * CUPS_MEDIA_FLAGS_READY = if the printer supports media sensing, find the
1098 * size amongst the "ready" media.
1100 * The matching result (if any) is returned in the "cups_size_t" structure.
1102 * Returns 1 when there is a match and 0 if there is not a match.
1104 * @since CUPS 1.6/macOS 10.8@
1107 int /* O - 1 on match, 0 on failure */
1108 cupsGetDestMediaByName(
1109 http_t
*http
, /* I - Connection to destination */
1110 cups_dest_t
*dest
, /* I - Destination */
1111 cups_dinfo_t
*dinfo
, /* I - Destination information */
1112 const char *media
, /* I - Media name */
1113 unsigned flags
, /* I - Media matching flags */
1114 cups_size_t
*size
) /* O - Media size information */
1116 pwg_media_t
*pwg
; /* PWG media info */
1120 * Get the default connection as needed...
1124 http
= _cupsConnect();
1127 * Range check input...
1131 memset(size
, 0, sizeof(cups_size_t
));
1133 if (!http
|| !dest
|| !dinfo
|| !media
|| !size
)
1135 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1140 * Lookup the media size name...
1143 if ((pwg
= pwgMediaForPWG(media
)) == NULL
)
1144 if ((pwg
= pwgMediaForLegacy(media
)) == NULL
)
1146 DEBUG_printf(("1cupsGetDestMediaByName: Unknown size '%s'.", media
));
1147 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unknown media size name."), 1);
1152 * Lookup the size...
1155 return (cups_get_media_db(http
, dinfo
, pwg
, flags
, size
));
1160 * 'cupsGetDestMediaBySize()' - Get media names, dimensions, and margins.
1162 * "Width" and "length" are the dimensions in hundredths of millimeters.
1163 * "Flags" provides some matching guidance (multiple flags can be combined):
1165 * CUPS_MEDIA_FLAGS_DEFAULT = find the closest size supported by the printer,
1166 * CUPS_MEDIA_FLAGS_BORDERLESS = find a borderless size,
1167 * CUPS_MEDIA_FLAGS_DUPLEX = find a size compatible with 2-sided printing,
1168 * CUPS_MEDIA_FLAGS_EXACT = find an exact match for the size, and
1169 * CUPS_MEDIA_FLAGS_READY = if the printer supports media sensing, find the
1170 * size amongst the "ready" media.
1172 * The matching result (if any) is returned in the "cups_size_t" structure.
1174 * Returns 1 when there is a match and 0 if there is not a match.
1176 * @since CUPS 1.6/macOS 10.8@
1179 int /* O - 1 on match, 0 on failure */
1180 cupsGetDestMediaBySize(
1181 http_t
*http
, /* I - Connection to destination */
1182 cups_dest_t
*dest
, /* I - Destination */
1183 cups_dinfo_t
*dinfo
, /* I - Destination information */
1184 int width
, /* I - Media width in hundredths of
1186 int length
, /* I - Media length in hundredths of
1188 unsigned flags
, /* I - Media matching flags */
1189 cups_size_t
*size
) /* O - Media size information */
1191 pwg_media_t
*pwg
; /* PWG media info */
1195 * Get the default connection as needed...
1199 http
= _cupsConnect();
1202 * Range check input...
1206 memset(size
, 0, sizeof(cups_size_t
));
1208 if (!http
|| !dest
|| !dinfo
|| width
<= 0 || length
<= 0 || !size
)
1210 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1215 * Lookup the media size name...
1218 if ((pwg
= pwgMediaForSize(width
, length
)) == NULL
)
1220 DEBUG_printf(("1cupsGetDestMediaBySize: Invalid size %dx%d.", width
,
1222 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Invalid media size."), 1);
1227 * Lookup the size...
1230 return (cups_get_media_db(http
, dinfo
, pwg
, flags
, size
));
1235 * 'cupsGetDestMediaCount()' - Get the number of sizes supported by a
1238 * The @code flags@ parameter determines the set of media sizes that are
1239 * counted. For example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will return
1240 * the number of borderless sizes.
1242 * @since CUPS 1.7/macOS 10.9@
1245 int /* O - Number of sizes */
1246 cupsGetDestMediaCount(
1247 http_t
*http
, /* I - Connection to destination */
1248 cups_dest_t
*dest
, /* I - Destination */
1249 cups_dinfo_t
*dinfo
, /* I - Destination information */
1250 unsigned flags
) /* I - Media flags */
1253 * Get the default connection as needed...
1257 http
= _cupsConnect();
1260 * Range check input...
1263 if (!http
|| !dest
|| !dinfo
)
1265 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1270 * Load media list as needed...
1273 if (flags
& CUPS_MEDIA_FLAGS_READY
)
1274 cups_update_ready(http
, dinfo
);
1276 if (!dinfo
->cached_db
|| dinfo
->cached_flags
!= flags
)
1277 cups_create_cached(http
, dinfo
, flags
);
1279 return (cupsArrayCount(dinfo
->cached_db
));
1284 * 'cupsGetDestMediaDefault()' - Get the default size for a destination.
1286 * The @code flags@ parameter determines which default size is returned. For
1287 * example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will return the default
1288 * borderless size, typically US Letter or A4, but sometimes 4x6 photo media.
1290 * @since CUPS 1.7/macOS 10.9@
1293 int /* O - 1 on success, 0 on failure */
1294 cupsGetDestMediaDefault(
1295 http_t
*http
, /* I - Connection to destination */
1296 cups_dest_t
*dest
, /* I - Destination */
1297 cups_dinfo_t
*dinfo
, /* I - Destination information */
1298 unsigned flags
, /* I - Media flags */
1299 cups_size_t
*size
) /* O - Media size information */
1301 const char *media
; /* Default media size */
1305 * Get the default connection as needed...
1309 http
= _cupsConnect();
1312 * Range check input...
1316 memset(size
, 0, sizeof(cups_size_t
));
1318 if (!http
|| !dest
|| !dinfo
|| !size
)
1320 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1325 * Get the default media size, if any...
1328 if ((media
= cupsGetOption("media", dest
->num_options
, dest
->options
)) == NULL
)
1329 media
= "na_letter_8.5x11in";
1331 if (cupsGetDestMediaByName(http
, dest
, dinfo
, media
, flags
, size
))
1334 if (strcmp(media
, "na_letter_8.5x11in") && cupsGetDestMediaByName(http
, dest
, dinfo
, "iso_a4_210x297mm", flags
, size
))
1337 if (strcmp(media
, "iso_a4_210x297mm") && cupsGetDestMediaByName(http
, dest
, dinfo
, "na_letter_8.5x11in", flags
, size
))
1340 if ((flags
& CUPS_MEDIA_FLAGS_BORDERLESS
) && cupsGetDestMediaByName(http
, dest
, dinfo
, "na_index_4x6in", flags
, size
))
1344 * Fall back to the first matching media size...
1347 return (cupsGetDestMediaByIndex(http
, dest
, dinfo
, 0, flags
, size
));
1352 * 'cups_add_dconstres()' - Add a constraint or resolver to an array.
1357 cups_array_t
*a
, /* I - Array */
1358 ipp_t
*collection
) /* I - Collection value */
1360 ipp_attribute_t
*attr
; /* Attribute */
1361 _cups_dconstres_t
*temp
; /* Current constraint/resolver */
1364 if ((attr
= ippFindAttribute(collection
, "resolver-name",
1365 IPP_TAG_NAME
)) == NULL
)
1368 if ((temp
= calloc(1, sizeof(_cups_dconstres_t
))) == NULL
)
1371 temp
->name
= attr
->values
[0].string
.text
;
1372 temp
->collection
= collection
;
1374 cupsArrayAdd(a
, temp
);
1379 * 'cups_collection_contains()' - Check whether test collection is contained in the matching collection.
1382 static int /* O - 1 on a match, 0 on a non-match */
1383 cups_collection_contains(ipp_t
*test
, /* I - Collection to test */
1384 ipp_t
*match
) /* I - Matching values */
1386 int i
, j
, /* Looping vars */
1387 mcount
, /* Number of match values */
1388 tcount
; /* Number of test values */
1389 ipp_attribute_t
*tattr
, /* Testing attribute */
1390 *mattr
; /* Matching attribute */
1391 const char *tval
; /* Testing string value */
1394 for (mattr
= ippFirstAttribute(match
); mattr
; mattr
= ippNextAttribute(match
))
1396 if ((tattr
= ippFindAttribute(test
, ippGetName(mattr
), IPP_TAG_ZERO
)) == NULL
)
1399 tcount
= ippGetCount(tattr
);
1401 switch (ippGetValueTag(mattr
))
1403 case IPP_TAG_INTEGER
:
1405 if (ippGetValueTag(tattr
) != ippGetValueTag(mattr
))
1408 for (i
= 0; i
< tcount
; i
++)
1410 if (!ippContainsInteger(mattr
, ippGetInteger(tattr
, i
)))
1415 case IPP_TAG_RANGE
:
1416 if (ippGetValueTag(tattr
) != IPP_TAG_INTEGER
)
1419 for (i
= 0; i
< tcount
; i
++)
1421 if (!ippContainsInteger(mattr
, ippGetInteger(tattr
, i
)))
1426 case IPP_TAG_BOOLEAN
:
1427 if (ippGetValueTag(tattr
) != IPP_TAG_BOOLEAN
|| ippGetBoolean(tattr
, 0) != ippGetBoolean(mattr
, 0))
1431 case IPP_TAG_TEXTLANG
:
1432 case IPP_TAG_NAMELANG
:
1435 case IPP_TAG_KEYWORD
:
1437 case IPP_TAG_URISCHEME
:
1438 case IPP_TAG_CHARSET
:
1439 case IPP_TAG_LANGUAGE
:
1440 case IPP_TAG_MIMETYPE
:
1441 for (i
= 0; i
< tcount
; i
++)
1443 if ((tval
= ippGetString(tattr
, i
, NULL
)) == NULL
|| !ippContainsString(mattr
, tval
))
1448 case IPP_TAG_BEGIN_COLLECTION
:
1449 for (i
= 0; i
< tcount
; i
++)
1451 ipp_t
*tcol
= ippGetCollection(tattr
, i
);
1452 /* Testing collection */
1454 for (j
= 0, mcount
= ippGetCount(mattr
); j
< mcount
; j
++)
1455 if (!cups_collection_contains(tcol
, ippGetCollection(mattr
, j
)))
1470 * 'cups_collection_string()' - Convert an IPP collection to an option string.
1473 static size_t /* O - Number of bytes needed */
1474 cups_collection_string(
1475 ipp_attribute_t
*attr
, /* I - Collection attribute */
1476 char *buffer
, /* I - String buffer */
1477 size_t bufsize
) /* I - Size of buffer */
1479 int i
, j
, /* Looping vars */
1480 count
, /* Number of collection values */
1481 mcount
; /* Number of member values */
1482 ipp_t
*col
; /* Collection */
1483 ipp_attribute_t
*first
, /* First member attribute */
1484 *member
; /* Member attribute */
1485 char *bufptr
, /* Pointer into buffer */
1486 *bufend
, /* End of buffer */
1487 temp
[100]; /* Temporary string */
1488 const char *mptr
; /* Pointer into member value */
1489 int mlen
; /* Length of octetString */
1493 bufend
= buffer
+ bufsize
- 1;
1495 for (i
= 0, count
= ippGetCount(attr
); i
< count
; i
++)
1497 col
= ippGetCollection(attr
, i
);
1501 if (bufptr
< bufend
)
1507 if (bufptr
< bufend
)
1512 for (member
= first
= ippFirstAttribute(col
); member
; member
= ippNextAttribute(col
))
1514 const char *mname
= ippGetName(member
);
1516 if (member
!= first
)
1518 if (bufptr
< bufend
)
1524 if (ippGetValueTag(member
) == IPP_TAG_BOOLEAN
)
1526 if (!ippGetBoolean(member
, 0))
1528 if (bufptr
< bufend
)
1529 strlcpy(bufptr
, "no", (size_t)(bufend
- bufptr
+ 1));
1533 if (bufptr
< bufend
)
1534 strlcpy(bufptr
, mname
, (size_t)(bufend
- bufptr
+ 1));
1535 bufptr
+= strlen(mname
);
1539 if (bufptr
< bufend
)
1540 strlcpy(bufptr
, mname
, (size_t)(bufend
- bufptr
+ 1));
1541 bufptr
+= strlen(mname
);
1543 if (bufptr
< bufend
)
1548 if (ippGetValueTag(member
) == IPP_TAG_BEGIN_COLLECTION
)
1551 * Convert sub-collection...
1554 bufptr
+= cups_collection_string(member
, bufptr
, bufptr
< bufend
? (size_t)(bufend
- bufptr
+ 1) : 0);
1559 * Convert simple type...
1562 for (j
= 0, mcount
= ippGetCount(member
); j
< mcount
; j
++)
1566 if (bufptr
< bufend
)
1572 switch (ippGetValueTag(member
))
1574 case IPP_TAG_INTEGER
:
1576 bufptr
+= snprintf(bufptr
, bufptr
< bufend
? (size_t)(bufend
- bufptr
+ 1) : 0, "%d", ippGetInteger(member
, j
));
1579 case IPP_TAG_STRING
:
1580 if (bufptr
< bufend
)
1585 for (mptr
= (const char *)ippGetOctetString(member
, j
, &mlen
); mlen
> 0; mlen
--, mptr
++)
1587 if (*mptr
== '\"' || *mptr
== '\\')
1589 if (bufptr
< bufend
)
1595 if (bufptr
< bufend
)
1601 if (bufptr
< bufend
)
1609 unsigned year
; /* Year */
1610 const ipp_uchar_t
*date
= ippGetDate(member
, j
);
1613 year
= ((unsigned)date
[0] << 8) + (unsigned)date
[1];
1615 if (date
[9] == 0 && date
[10] == 0)
1616 snprintf(temp
, sizeof(temp
), "%04u-%02u-%02uT%02u:%02u:%02uZ", year
, date
[2], date
[3], date
[4], date
[5], date
[6]);
1618 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]);
1620 if (buffer
&& bufptr
< bufend
)
1621 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
+ 1));
1623 bufptr
+= strlen(temp
);
1627 case IPP_TAG_RESOLUTION
:
1629 int xres
, /* Horizontal resolution */
1630 yres
; /* Vertical resolution */
1631 ipp_res_t units
; /* Resolution units */
1633 xres
= ippGetResolution(member
, j
, &yres
, &units
);
1636 snprintf(temp
, sizeof(temp
), "%d%s", xres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
1638 snprintf(temp
, sizeof(temp
), "%dx%d%s", xres
, yres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
1640 if (buffer
&& bufptr
< bufend
)
1641 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
+ 1));
1643 bufptr
+= strlen(temp
);
1647 case IPP_TAG_RANGE
:
1649 int lower
, /* Lower bound */
1650 upper
; /* Upper bound */
1652 lower
= ippGetRange(member
, j
, &upper
);
1654 snprintf(temp
, sizeof(temp
), "%d-%d", lower
, upper
);
1656 if (buffer
&& bufptr
< bufend
)
1657 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
+ 1));
1659 bufptr
+= strlen(temp
);
1663 case IPP_TAG_TEXTLANG
:
1664 case IPP_TAG_NAMELANG
:
1667 case IPP_TAG_KEYWORD
:
1669 case IPP_TAG_URISCHEME
:
1670 case IPP_TAG_CHARSET
:
1671 case IPP_TAG_LANGUAGE
:
1672 case IPP_TAG_MIMETYPE
:
1673 if (bufptr
< bufend
)
1678 for (mptr
= ippGetString(member
, j
, NULL
); *mptr
; mptr
++)
1680 if (*mptr
== '\"' || *mptr
== '\\')
1682 if (bufptr
< bufend
)
1688 if (bufptr
< bufend
)
1694 if (bufptr
< bufend
)
1707 if (bufptr
< bufend
)
1714 return ((size_t)(bufptr
- buffer
+ 1));
1719 * 'cups_compare_dconstres()' - Compare to resolver entries.
1722 static int /* O - Result of comparison */
1723 cups_compare_dconstres(
1724 _cups_dconstres_t
*a
, /* I - First resolver */
1725 _cups_dconstres_t
*b
) /* I - Second resolver */
1727 return (strcmp(a
->name
, b
->name
));
1732 * 'cups_compare_media_db()' - Compare two media entries.
1735 static int /* O - Result of comparison */
1736 cups_compare_media_db(
1737 _cups_media_db_t
*a
, /* I - First media entries */
1738 _cups_media_db_t
*b
) /* I - Second media entries */
1740 int result
; /* Result of comparison */
1743 if ((result
= a
->width
- b
->width
) == 0)
1744 result
= a
->length
- b
->length
;
1751 * 'cups_copy_media_db()' - Copy a media entry.
1754 static _cups_media_db_t
* /* O - New media entry */
1756 _cups_media_db_t
*mdb
) /* I - Media entry to copy */
1758 _cups_media_db_t
*temp
; /* New media entry */
1761 if ((temp
= calloc(1, sizeof(_cups_media_db_t
))) == NULL
)
1765 temp
->color
= _cupsStrAlloc(mdb
->color
);
1767 temp
->key
= _cupsStrAlloc(mdb
->key
);
1769 temp
->info
= _cupsStrAlloc(mdb
->info
);
1771 temp
->size_name
= _cupsStrAlloc(mdb
->size_name
);
1773 temp
->source
= _cupsStrAlloc(mdb
->source
);
1775 temp
->type
= _cupsStrAlloc(mdb
->type
);
1777 temp
->width
= mdb
->width
;
1778 temp
->length
= mdb
->length
;
1779 temp
->bottom
= mdb
->bottom
;
1780 temp
->left
= mdb
->left
;
1781 temp
->right
= mdb
->right
;
1782 temp
->top
= mdb
->top
;
1789 * 'cups_create_cached()' - Create the media selection cache.
1793 cups_create_cached(http_t
*http
, /* I - Connection to destination */
1794 cups_dinfo_t
*dinfo
, /* I - Destination information */
1795 unsigned flags
) /* I - Media selection flags */
1797 cups_array_t
*db
; /* Media database array to use */
1798 _cups_media_db_t
*mdb
, /* Media database entry */
1799 *first
; /* First entry this size */
1802 DEBUG_printf(("3cups_create_cached(http=%p, dinfo=%p, flags=%u)", (void *)http
, (void *)dinfo
, flags
));
1804 if (dinfo
->cached_db
)
1805 cupsArrayDelete(dinfo
->cached_db
);
1807 dinfo
->cached_db
= cupsArrayNew(NULL
, NULL
);
1808 dinfo
->cached_flags
= flags
;
1810 if (flags
& CUPS_MEDIA_FLAGS_READY
)
1812 DEBUG_puts("4cups_create_cached: ready media");
1814 cups_update_ready(http
, dinfo
);
1815 db
= dinfo
->ready_db
;
1819 DEBUG_puts("4cups_create_cached: supported media");
1821 if (!dinfo
->media_db
)
1822 cups_create_media_db(dinfo
, CUPS_MEDIA_FLAGS_DEFAULT
);
1824 db
= dinfo
->media_db
;
1827 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
), first
= mdb
;
1829 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
1831 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
));
1833 if (flags
& CUPS_MEDIA_FLAGS_BORDERLESS
)
1835 if (!mdb
->left
&& !mdb
->right
&& !mdb
->top
&& !mdb
->bottom
)
1837 DEBUG_printf(("4cups_create_cached: add %p", (void *)mdb
));
1838 cupsArrayAdd(dinfo
->cached_db
, mdb
);
1841 else if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
1843 if (first
->width
!= mdb
->width
|| first
->length
!= mdb
->length
)
1845 DEBUG_printf(("4cups_create_cached: add %p", (void *)first
));
1846 cupsArrayAdd(dinfo
->cached_db
, first
);
1849 else if (mdb
->left
>= first
->left
&& mdb
->right
>= first
->right
&& mdb
->top
>= first
->top
&& mdb
->bottom
>= first
->bottom
&&
1850 (mdb
->left
!= first
->left
|| mdb
->right
!= first
->right
|| mdb
->top
!= first
->top
|| mdb
->bottom
!= first
->bottom
))
1855 DEBUG_printf(("4cups_create_cached: add %p", (void *)mdb
));
1856 cupsArrayAdd(dinfo
->cached_db
, mdb
);
1860 if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
1862 DEBUG_printf(("4cups_create_cached: add %p", (void *)first
));
1863 cupsArrayAdd(dinfo
->cached_db
, first
);
1869 * 'cups_create_constraints()' - Create the constraints and resolvers arrays.
1873 cups_create_constraints(
1874 cups_dinfo_t
*dinfo
) /* I - Destination information */
1876 int i
; /* Looping var */
1877 ipp_attribute_t
*attr
; /* Attribute */
1878 _ipp_value_t
*val
; /* Current value */
1881 dinfo
->constraints
= cupsArrayNew3(NULL
, NULL
, NULL
, 0, NULL
,
1882 (cups_afree_func_t
)free
);
1883 dinfo
->resolvers
= cupsArrayNew3((cups_array_func_t
)cups_compare_dconstres
,
1884 NULL
, NULL
, 0, NULL
,
1885 (cups_afree_func_t
)free
);
1887 if ((attr
= ippFindAttribute(dinfo
->attrs
, "job-constraints-supported",
1888 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1890 for (i
= attr
->num_values
, val
= attr
->values
; i
> 0; i
--, val
++)
1891 cups_add_dconstres(dinfo
->constraints
, val
->collection
);
1894 if ((attr
= ippFindAttribute(dinfo
->attrs
, "job-resolvers-supported",
1895 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1897 for (i
= attr
->num_values
, val
= attr
->values
; i
> 0; i
--, val
++)
1898 cups_add_dconstres(dinfo
->resolvers
, val
->collection
);
1904 * 'cups_create_defaults()' - Create the -default option array.
1908 cups_create_defaults(
1909 cups_dinfo_t
*dinfo
) /* I - Destination information */
1911 ipp_attribute_t
*attr
; /* Current attribute */
1912 char name
[IPP_MAX_NAME
+ 1],
1914 *nameptr
, /* Pointer into current name */
1915 value
[2048]; /* Current value */
1919 * Iterate through the printer attributes looking for xxx-default and adding
1920 * xxx=value to the defaults option array.
1923 for (attr
= ippFirstAttribute(dinfo
->attrs
); attr
; attr
= ippNextAttribute(dinfo
->attrs
))
1925 if (!ippGetName(attr
) || ippGetGroupTag(attr
) != IPP_TAG_PRINTER
)
1928 strlcpy(name
, ippGetName(attr
), sizeof(name
));
1929 if ((nameptr
= name
+ strlen(name
) - 8) <= name
|| strcmp(nameptr
, "-default"))
1934 if (ippGetValueTag(attr
) == IPP_TAG_BEGIN_COLLECTION
)
1936 if (cups_collection_string(attr
, value
, sizeof(value
)) >= sizeof(value
))
1939 else if (ippAttributeString(attr
, value
, sizeof(value
)) >= sizeof(value
))
1942 dinfo
->num_defaults
= cupsAddOption(name
, value
, dinfo
->num_defaults
, &dinfo
->defaults
);
1948 * 'cups_create_media_db()' - Create the media database.
1952 cups_create_media_db(
1953 cups_dinfo_t
*dinfo
, /* I - Destination information */
1954 unsigned flags
) /* I - Media flags */
1956 int i
; /* Looping var */
1957 _ipp_value_t
*val
; /* Current value */
1958 ipp_attribute_t
*media_col_db
, /* media-col-database */
1959 *media_attr
, /* media-xxx */
1960 *x_dimension
, /* x-dimension */
1961 *y_dimension
; /* y-dimension */
1962 pwg_media_t
*pwg
; /* PWG media info */
1963 cups_array_t
*db
; /* New media database array */
1964 _cups_media_db_t mdb
; /* Media entry */
1965 char media_key
[256]; /* Synthesized media-key value */
1968 db
= cupsArrayNew3((cups_array_func_t
)cups_compare_media_db
,
1970 (cups_acopy_func_t
)cups_copy_media_db
,
1971 (cups_afree_func_t
)cups_free_media_db
);
1973 if (flags
== CUPS_MEDIA_FLAGS_READY
)
1975 dinfo
->ready_db
= db
;
1977 media_col_db
= ippFindAttribute(dinfo
->ready_attrs
, "media-col-ready",
1978 IPP_TAG_BEGIN_COLLECTION
);
1979 media_attr
= ippFindAttribute(dinfo
->ready_attrs
, "media-ready",
1984 dinfo
->media_db
= db
;
1985 dinfo
->min_size
.width
= INT_MAX
;
1986 dinfo
->min_size
.length
= INT_MAX
;
1987 dinfo
->max_size
.width
= 0;
1988 dinfo
->max_size
.length
= 0;
1990 media_col_db
= ippFindAttribute(dinfo
->attrs
, "media-col-database",
1991 IPP_TAG_BEGIN_COLLECTION
);
1992 media_attr
= ippFindAttribute(dinfo
->attrs
, "media-supported",
1998 _ipp_value_t
*custom
= NULL
; /* Custom size range value */
2000 for (i
= media_col_db
->num_values
, val
= media_col_db
->values
;
2004 memset(&mdb
, 0, sizeof(mdb
));
2006 if ((media_attr
= ippFindAttribute(val
->collection
, "media-size",
2007 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
2009 ipp_t
*media_size
= media_attr
->values
[0].collection
;
2010 /* media-size collection value */
2012 if ((x_dimension
= ippFindAttribute(media_size
, "x-dimension",
2013 IPP_TAG_INTEGER
)) != NULL
&&
2014 (y_dimension
= ippFindAttribute(media_size
, "y-dimension",
2015 IPP_TAG_INTEGER
)) != NULL
)
2021 mdb
.width
= x_dimension
->values
[0].integer
;
2022 mdb
.length
= y_dimension
->values
[0].integer
;
2024 else if ((x_dimension
= ippFindAttribute(media_size
, "x-dimension",
2025 IPP_TAG_INTEGER
)) != NULL
&&
2026 (y_dimension
= ippFindAttribute(media_size
, "y-dimension",
2027 IPP_TAG_RANGE
)) != NULL
)
2033 mdb
.width
= x_dimension
->values
[0].integer
;
2034 mdb
.length
= y_dimension
->values
[0].range
.upper
;
2036 else if (flags
!= CUPS_MEDIA_FLAGS_READY
&&
2037 (x_dimension
= ippFindAttribute(media_size
, "x-dimension",
2038 IPP_TAG_RANGE
)) != NULL
&&
2039 (y_dimension
= ippFindAttribute(media_size
, "y-dimension",
2040 IPP_TAG_RANGE
)) != NULL
)
2043 * Custom size range; save this as the custom size value with default
2044 * margins, then continue; we'll capture the real margins below...
2049 dinfo
->min_size
.width
= x_dimension
->values
[0].range
.lower
;
2050 dinfo
->min_size
.length
= y_dimension
->values
[0].range
.lower
;
2051 dinfo
->min_size
.left
=
2052 dinfo
->min_size
.right
= 635; /* Default 1/4" side margins */
2053 dinfo
->min_size
.top
=
2054 dinfo
->min_size
.bottom
= 1270; /* Default 1/2" top/bottom margins */
2056 dinfo
->max_size
.width
= x_dimension
->values
[0].range
.upper
;
2057 dinfo
->max_size
.length
= y_dimension
->values
[0].range
.upper
;
2058 dinfo
->max_size
.left
=
2059 dinfo
->max_size
.right
= 635; /* Default 1/4" side margins */
2060 dinfo
->max_size
.top
=
2061 dinfo
->max_size
.bottom
= 1270; /* Default 1/2" top/bottom margins */
2066 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
))
2067 mdb
.color
= media_attr
->values
[0].string
.text
;
2069 if ((media_attr
= ippFindAttribute(val
->collection
, "media-info", IPP_TAG_TEXT
)) != NULL
)
2070 mdb
.info
= media_attr
->values
[0].string
.text
;
2072 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
))
2073 mdb
.key
= media_attr
->values
[0].string
.text
;
2075 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
))
2076 mdb
.size_name
= media_attr
->values
[0].string
.text
;
2078 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
))
2079 mdb
.source
= media_attr
->values
[0].string
.text
;
2081 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
))
2082 mdb
.type
= media_attr
->values
[0].string
.text
;
2084 if ((media_attr
= ippFindAttribute(val
->collection
, "media-bottom-margin", IPP_TAG_INTEGER
)) != NULL
)
2085 mdb
.bottom
= media_attr
->values
[0].integer
;
2087 if ((media_attr
= ippFindAttribute(val
->collection
, "media-left-margin", IPP_TAG_INTEGER
)) != NULL
)
2088 mdb
.left
= media_attr
->values
[0].integer
;
2090 if ((media_attr
= ippFindAttribute(val
->collection
, "media-right-margin", IPP_TAG_INTEGER
)) != NULL
)
2091 mdb
.right
= media_attr
->values
[0].integer
;
2093 if ((media_attr
= ippFindAttribute(val
->collection
, "media-top-margin", IPP_TAG_INTEGER
)) != NULL
)
2094 mdb
.top
= media_attr
->values
[0].integer
;
2098 if (!mdb
.size_name
&& (pwg
= pwgMediaForSize(mdb
.width
, mdb
.length
)) != NULL
)
2099 mdb
.size_name
= (char *)pwg
->pwg
;
2104 * Use a CUPS-specific identifier if we don't have a size name...
2107 if (flags
& CUPS_MEDIA_FLAGS_READY
)
2108 snprintf(media_key
, sizeof(media_key
), "cups-media-ready-%d", i
+ 1);
2110 snprintf(media_key
, sizeof(media_key
), "cups-media-%d", i
+ 1);
2112 else if (mdb
.source
)
2115 * Generate key using size name, source, and type (if set)...
2119 snprintf(media_key
, sizeof(media_key
), "%s_%s_%s", mdb
.size_name
, mdb
.source
, mdb
.type
);
2121 snprintf(media_key
, sizeof(media_key
), "%s_%s", mdb
.size_name
, mdb
.source
);
2126 * Generate key using size name and type...
2129 snprintf(media_key
, sizeof(media_key
), "%s_%s", mdb
.size_name
, mdb
.type
);
2134 * Key is just the size name...
2137 strlcpy(media_key
, mdb
.size_name
, sizeof(media_key
));
2141 * Append "_borderless" for borderless media...
2144 if (!mdb
.bottom
&& !mdb
.left
&& !mdb
.right
&& !mdb
.top
)
2145 strlcat(media_key
, "_borderless", sizeof(media_key
));
2147 mdb
.key
= media_key
;
2150 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
));
2152 cupsArrayAdd(db
, &mdb
);
2157 if ((media_attr
= ippFindAttribute(custom
->collection
,
2158 "media-bottom-margin",
2159 IPP_TAG_INTEGER
)) != NULL
)
2161 dinfo
->min_size
.top
=
2162 dinfo
->max_size
.top
= media_attr
->values
[0].integer
;
2165 if ((media_attr
= ippFindAttribute(custom
->collection
,
2166 "media-left-margin",
2167 IPP_TAG_INTEGER
)) != NULL
)
2169 dinfo
->min_size
.left
=
2170 dinfo
->max_size
.left
= media_attr
->values
[0].integer
;
2173 if ((media_attr
= ippFindAttribute(custom
->collection
,
2174 "media-right-margin",
2175 IPP_TAG_INTEGER
)) != NULL
)
2177 dinfo
->min_size
.right
=
2178 dinfo
->max_size
.right
= media_attr
->values
[0].integer
;
2181 if ((media_attr
= ippFindAttribute(custom
->collection
,
2183 IPP_TAG_INTEGER
)) != NULL
)
2185 dinfo
->min_size
.top
=
2186 dinfo
->max_size
.top
= media_attr
->values
[0].integer
;
2190 else if (media_attr
&&
2191 (media_attr
->value_tag
== IPP_TAG_NAME
||
2192 media_attr
->value_tag
== IPP_TAG_NAMELANG
||
2193 media_attr
->value_tag
== IPP_TAG_KEYWORD
))
2195 memset(&mdb
, 0, sizeof(mdb
));
2198 mdb
.right
= 635; /* Default 1/4" side margins */
2200 mdb
.bottom
= 1270; /* Default 1/2" top/bottom margins */
2202 for (i
= media_attr
->num_values
, val
= media_attr
->values
;
2206 if ((pwg
= pwgMediaForPWG(val
->string
.text
)) == NULL
)
2207 if ((pwg
= pwgMediaForLegacy(val
->string
.text
)) == NULL
)
2209 DEBUG_printf(("3cups_create_media_db: Ignoring unknown size '%s'.",
2214 mdb
.width
= pwg
->width
;
2215 mdb
.length
= pwg
->length
;
2217 if (flags
!= CUPS_MEDIA_FLAGS_READY
&&
2218 !strncmp(val
->string
.text
, "custom_min_", 11))
2220 mdb
.size_name
= NULL
;
2221 dinfo
->min_size
= mdb
;
2223 else if (flags
!= CUPS_MEDIA_FLAGS_READY
&&
2224 !strncmp(val
->string
.text
, "custom_max_", 11))
2226 mdb
.size_name
= NULL
;
2227 dinfo
->max_size
= mdb
;
2231 mdb
.size_name
= val
->string
.text
;
2233 cupsArrayAdd(db
, &mdb
);
2241 * 'cups_free_media_cb()' - Free a media entry.
2246 _cups_media_db_t
*mdb
) /* I - Media entry to free */
2249 _cupsStrFree(mdb
->color
);
2251 _cupsStrFree(mdb
->key
);
2253 _cupsStrFree(mdb
->info
);
2255 _cupsStrFree(mdb
->size_name
);
2257 _cupsStrFree(mdb
->source
);
2259 _cupsStrFree(mdb
->type
);
2266 * 'cups_get_media_db()' - Lookup the media entry for a given size.
2269 static int /* O - 1 on match, 0 on failure */
2270 cups_get_media_db(http_t
*http
, /* I - Connection to destination */
2271 cups_dinfo_t
*dinfo
, /* I - Destination information */
2272 pwg_media_t
*pwg
, /* I - PWG media info */
2273 unsigned flags
, /* I - Media matching flags */
2274 cups_size_t
*size
) /* O - Media size/margin/name info */
2276 cups_array_t
*db
; /* Which media database to query */
2277 _cups_media_db_t
*mdb
, /* Current media database entry */
2278 *best
= NULL
, /* Best matching entry */
2279 key
; /* Search key */
2283 * Create the media database as needed...
2286 if (flags
& CUPS_MEDIA_FLAGS_READY
)
2288 cups_update_ready(http
, dinfo
);
2289 db
= dinfo
->ready_db
;
2293 if (!dinfo
->media_db
)
2294 cups_create_media_db(dinfo
, CUPS_MEDIA_FLAGS_DEFAULT
);
2296 db
= dinfo
->media_db
;
2303 memset(&key
, 0, sizeof(key
));
2304 key
.width
= pwg
->width
;
2305 key
.length
= pwg
->length
;
2307 if ((mdb
= cupsArrayFind(db
, &key
)) != NULL
)
2310 * Found an exact match, let's figure out the best margins for the flags
2316 if (flags
& CUPS_MEDIA_FLAGS_BORDERLESS
)
2319 * Look for the smallest margins...
2322 if (best
->left
!= 0 || best
->right
!= 0 || best
->top
!= 0 || best
->bottom
!= 0)
2324 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
2325 mdb
&& !cups_compare_media_db(mdb
, &key
);
2326 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2328 if (mdb
->left
<= best
->left
&& mdb
->right
<= best
->right
&&
2329 mdb
->top
<= best
->top
&& mdb
->bottom
<= best
->bottom
)
2332 if (mdb
->left
== 0 && mdb
->right
== 0 && mdb
->bottom
== 0 &&
2340 * If we need an exact match, return no-match if the size is not
2344 if ((flags
& CUPS_MEDIA_FLAGS_EXACT
) &&
2345 (best
->left
|| best
->right
|| best
->top
|| best
->bottom
))
2348 else if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
2351 * Look for the largest margins...
2354 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
2355 mdb
&& !cups_compare_media_db(mdb
, &key
);
2356 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2358 if (mdb
->left
>= best
->left
&& mdb
->right
>= best
->right
&&
2359 mdb
->top
>= best
->top
&& mdb
->bottom
>= best
->bottom
&&
2360 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
2367 * Look for the smallest non-zero margins...
2370 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
2371 mdb
&& !cups_compare_media_db(mdb
, &key
);
2372 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2374 if (((mdb
->left
> 0 && mdb
->left
<= best
->left
) || best
->left
== 0) &&
2375 ((mdb
->right
> 0 && mdb
->right
<= best
->right
) || best
->right
== 0) &&
2376 ((mdb
->top
> 0 && mdb
->top
<= best
->top
) || best
->top
== 0) &&
2377 ((mdb
->bottom
> 0 && mdb
->bottom
<= best
->bottom
) || best
->bottom
== 0) &&
2378 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
2383 else if (flags
& CUPS_MEDIA_FLAGS_EXACT
)
2386 * See if we can do this as a custom size...
2389 if (pwg
->width
< dinfo
->min_size
.width
||
2390 pwg
->width
> dinfo
->max_size
.width
||
2391 pwg
->length
< dinfo
->min_size
.length
||
2392 pwg
->length
> dinfo
->max_size
.length
)
2393 return (0); /* Out of range */
2395 if ((flags
& CUPS_MEDIA_FLAGS_BORDERLESS
) &&
2396 (dinfo
->min_size
.left
> 0 || dinfo
->min_size
.right
> 0 ||
2397 dinfo
->min_size
.top
> 0 || dinfo
->min_size
.bottom
> 0))
2398 return (0); /* Not borderless */
2400 key
.size_name
= (char *)pwg
->pwg
;
2401 key
.bottom
= dinfo
->min_size
.bottom
;
2402 key
.left
= dinfo
->min_size
.left
;
2403 key
.right
= dinfo
->min_size
.right
;
2404 key
.top
= dinfo
->min_size
.top
;
2408 else if (pwg
->width
>= dinfo
->min_size
.width
&&
2409 pwg
->width
<= dinfo
->max_size
.width
&&
2410 pwg
->length
>= dinfo
->min_size
.length
&&
2411 pwg
->length
<= dinfo
->max_size
.length
)
2414 * Map to custom size...
2417 key
.size_name
= (char *)pwg
->pwg
;
2418 key
.bottom
= dinfo
->min_size
.bottom
;
2419 key
.left
= dinfo
->min_size
.left
;
2420 key
.right
= dinfo
->min_size
.right
;
2421 key
.top
= dinfo
->min_size
.top
;
2428 * Find a close size...
2431 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
);
2433 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2434 if (cups_is_close_media_db(mdb
, &key
))
2442 if (flags
& CUPS_MEDIA_FLAGS_BORDERLESS
)
2445 * Look for the smallest margins...
2448 if (best
->left
!= 0 || best
->right
!= 0 || best
->top
!= 0 ||
2451 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
2452 mdb
&& cups_is_close_media_db(mdb
, &key
);
2453 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2455 if (mdb
->left
<= best
->left
&& mdb
->right
<= best
->right
&&
2456 mdb
->top
<= best
->top
&& mdb
->bottom
<= best
->bottom
&&
2457 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
2460 if (mdb
->left
== 0 && mdb
->right
== 0 && mdb
->bottom
== 0 &&
2467 else if (flags
& CUPS_MEDIA_FLAGS_DUPLEX
)
2470 * Look for the largest margins...
2473 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
2474 mdb
&& cups_is_close_media_db(mdb
, &key
);
2475 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2477 if (mdb
->left
>= best
->left
&& mdb
->right
>= best
->right
&&
2478 mdb
->top
>= best
->top
&& mdb
->bottom
>= best
->bottom
&&
2479 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
2486 * Look for the smallest non-zero margins...
2489 for (mdb
= (_cups_media_db_t
*)cupsArrayNext(db
);
2490 mdb
&& cups_is_close_media_db(mdb
, &key
);
2491 mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
2493 if (((mdb
->left
> 0 && mdb
->left
<= best
->left
) || best
->left
== 0) &&
2494 ((mdb
->right
> 0 && mdb
->right
<= best
->right
) ||
2495 best
->right
== 0) &&
2496 ((mdb
->top
> 0 && mdb
->top
<= best
->top
) || best
->top
== 0) &&
2497 ((mdb
->bottom
> 0 && mdb
->bottom
<= best
->bottom
) ||
2498 best
->bottom
== 0) &&
2499 (mdb
->bottom
!= best
->bottom
|| mdb
->left
!= best
->left
|| mdb
->right
!= best
->right
|| mdb
->top
!= best
->top
))
2508 * Return the matching size...
2512 strlcpy(size
->media
, best
->key
, sizeof(size
->media
));
2513 else if (best
->size_name
)
2514 strlcpy(size
->media
, best
->size_name
, sizeof(size
->media
));
2516 strlcpy(size
->media
, pwg
->pwg
, sizeof(size
->media
));
2518 size
->width
= best
->width
;
2519 size
->length
= best
->length
;
2520 size
->bottom
= best
->bottom
;
2521 size
->left
= best
->left
;
2522 size
->right
= best
->right
;
2523 size
->top
= best
->top
;
2533 * 'cups_is_close_media_db()' - Compare two media entries to see if they are
2534 * close to the same size.
2536 * Currently we use 5 points (from PostScript) as the matching range...
2539 static int /* O - 1 if the sizes are close */
2540 cups_is_close_media_db(
2541 _cups_media_db_t
*a
, /* I - First media entries */
2542 _cups_media_db_t
*b
) /* I - Second media entries */
2544 int dwidth
, /* Difference in width */
2545 dlength
; /* Difference in length */
2548 dwidth
= a
->width
- b
->width
;
2549 dlength
= a
->length
- b
->length
;
2551 return (dwidth
>= -176 && dwidth
<= 176 &&
2552 dlength
>= -176 && dlength
<= 176);
2557 * 'cups_test_constraints()' - Test constraints.
2560 static cups_array_t
* /* O - Active constraints */
2561 cups_test_constraints(
2562 cups_dinfo_t
*dinfo
, /* I - Destination information */
2563 const char *new_option
, /* I - Newly selected option */
2564 const char *new_value
, /* I - Newly selected value */
2565 int num_options
, /* I - Number of options */
2566 cups_option_t
*options
, /* I - Options */
2567 int *num_conflicts
, /* O - Number of conflicting options */
2568 cups_option_t
**conflicts
) /* O - Conflicting options */
2570 int i
, /* Looping var */
2571 count
, /* Number of values */
2572 match
; /* Value matches? */
2573 int num_matching
; /* Number of matching options */
2574 cups_option_t
*matching
; /* Matching options */
2575 _cups_dconstres_t
*c
; /* Current constraint */
2576 cups_array_t
*active
= NULL
; /* Active constraints */
2577 ipp_t
*col
; /* Collection value */
2578 ipp_attribute_t
*attr
; /* Current attribute */
2579 _ipp_value_t
*attrval
; /* Current attribute value */
2580 const char *value
; /* Current value */
2581 char temp
[1024]; /* Temporary string */
2582 int int_value
; /* Integer value */
2583 int xres_value
, /* Horizontal resolution */
2584 yres_value
; /* Vertical resolution */
2585 ipp_res_t units_value
; /* Resolution units */
2588 for (c
= (_cups_dconstres_t
*)cupsArrayFirst(dinfo
->constraints
);
2590 c
= (_cups_dconstres_t
*)cupsArrayNext(dinfo
->constraints
))
2595 for (attr
= ippFirstAttribute(c
->collection
);
2597 attr
= ippNextAttribute(c
->collection
))
2600 * Get the value for the current attribute in the constraint...
2603 if (new_option
&& new_value
&& !strcmp(attr
->name
, new_option
))
2605 else if ((value
= cupsGetOption(attr
->name
, num_options
, options
)) == NULL
)
2606 value
= cupsGetOption(attr
->name
, dinfo
->num_defaults
, dinfo
->defaults
);
2611 * Not set so this constraint does not apply...
2619 switch (attr
->value_tag
)
2621 case IPP_TAG_INTEGER
:
2623 int_value
= atoi(value
);
2625 for (i
= attr
->num_values
, attrval
= attr
->values
;
2629 if (attrval
->integer
== int_value
)
2637 case IPP_TAG_BOOLEAN
:
2638 int_value
= !strcmp(value
, "true");
2640 for (i
= attr
->num_values
, attrval
= attr
->values
;
2644 if (attrval
->boolean
== int_value
)
2652 case IPP_TAG_RANGE
:
2653 int_value
= atoi(value
);
2655 for (i
= attr
->num_values
, attrval
= attr
->values
;
2659 if (int_value
>= attrval
->range
.lower
&&
2660 int_value
<= attrval
->range
.upper
)
2668 case IPP_TAG_RESOLUTION
:
2669 if (sscanf(value
, "%dx%d%15s", &xres_value
, &yres_value
, temp
) != 3)
2671 if (sscanf(value
, "%d%15s", &xres_value
, temp
) != 2)
2674 yres_value
= xres_value
;
2677 if (!strcmp(temp
, "dpi"))
2678 units_value
= IPP_RES_PER_INCH
;
2679 else if (!strcmp(temp
, "dpc") || !strcmp(temp
, "dpcm"))
2680 units_value
= IPP_RES_PER_CM
;
2684 for (i
= attr
->num_values
, attrval
= attr
->values
;
2688 if (attrval
->resolution
.xres
== xres_value
&&
2689 attrval
->resolution
.yres
== yres_value
&&
2690 attrval
->resolution
.units
== units_value
)
2700 case IPP_TAG_KEYWORD
:
2701 case IPP_TAG_CHARSET
:
2703 case IPP_TAG_URISCHEME
:
2704 case IPP_TAG_MIMETYPE
:
2705 case IPP_TAG_LANGUAGE
:
2706 case IPP_TAG_TEXTLANG
:
2707 case IPP_TAG_NAMELANG
:
2708 for (i
= attr
->num_values
, attrval
= attr
->values
;
2712 if (!strcmp(attrval
->string
.text
, value
))
2720 case IPP_TAG_BEGIN_COLLECTION
:
2722 _cupsEncodeOption(col
, IPP_TAG_ZERO
, NULL
, ippGetName(attr
), value
);
2724 for (i
= 0, count
= ippGetCount(attr
); i
< count
; i
++)
2726 if (cups_collection_contains(col
, ippGetCollection(attr
, i
)))
2743 num_matching
= cupsAddOption(attr
->name
, value
, num_matching
, &matching
);
2749 active
= cupsArrayNew(NULL
, NULL
);
2751 cupsArrayAdd(active
, c
);
2753 if (num_conflicts
&& conflicts
)
2755 cups_option_t
*moption
; /* Matching option */
2757 for (i
= num_matching
, moption
= matching
; i
> 0; i
--, moption
++)
2758 *num_conflicts
= cupsAddOption(moption
->name
, moption
->value
, *num_conflicts
, conflicts
);
2762 cupsFreeOptions(num_matching
, matching
);
2770 * 'cups_update_ready()' - Update xxx-ready attributes for the printer.
2774 cups_update_ready(http_t
*http
, /* I - Connection to destination */
2775 cups_dinfo_t
*dinfo
) /* I - Destination information */
2777 ipp_t
*request
; /* Get-Printer-Attributes request */
2778 static const char * const pattrs
[] = /* Printer attributes we want */
2780 "finishings-col-ready",
2782 "job-finishings-col-ready",
2783 "job-finishings-ready",
2790 * Don't update more than once every 30 seconds...
2793 if ((time(NULL
) - dinfo
->ready_time
) < _CUPS_MEDIA_READY_TTL
)
2797 * Free any previous results...
2800 if (dinfo
->cached_flags
& CUPS_MEDIA_FLAGS_READY
)
2802 cupsArrayDelete(dinfo
->cached_db
);
2803 dinfo
->cached_db
= NULL
;
2804 dinfo
->cached_flags
= CUPS_MEDIA_FLAGS_DEFAULT
;
2807 ippDelete(dinfo
->ready_attrs
);
2808 dinfo
->ready_attrs
= NULL
;
2810 cupsArrayDelete(dinfo
->ready_db
);
2811 dinfo
->ready_db
= NULL
;
2814 * Query the xxx-ready values...
2817 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
2818 ippSetVersion(request
, dinfo
->version
/ 10, dinfo
->version
% 10);
2820 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
2822 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
2824 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "requested-attributes", (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
2826 dinfo
->ready_attrs
= cupsDoRequest(http
, request
, dinfo
->resource
);
2829 * Update the ready media database...
2832 cups_create_media_db(dinfo
, CUPS_MEDIA_FLAGS_READY
);
2835 * Update last lookup time and return...
2838 dinfo
->ready_time
= time(NULL
);