]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/dest-options.c
The `cupsCopyDestConflicts` function now handles collection attribute
[thirdparty/cups.git] / cups / dest-options.c
1 /*
2 * Destination option/media support for CUPS.
3 *
4 * Copyright 2012-2017 by Apple Inc.
5 *
6 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
7 */
8
9 /*
10 * Include necessary headers...
11 */
12
13 #include "cups-private.h"
14
15
16 /*
17 * Local constants...
18 */
19
20 #define _CUPS_MEDIA_READY_TTL 30 /* Life of xxx-ready values */
21
22
23 /*
24 * Local functions...
25 */
26
27 static void cups_add_dconstres(cups_array_t *a, ipp_t *collection);
28 static int cups_collection_contains(ipp_t *test, ipp_t *match);
29 static size_t cups_collection_string(ipp_attribute_t *attr, char *buffer, size_t bufsize);
30 static int cups_compare_dconstres(_cups_dconstres_t *a,
31 _cups_dconstres_t *b);
32 static int cups_compare_media_db(_cups_media_db_t *a,
33 _cups_media_db_t *b);
34 static _cups_media_db_t *cups_copy_media_db(_cups_media_db_t *mdb);
35 static void cups_create_cached(http_t *http, cups_dinfo_t *dinfo,
36 unsigned flags);
37 static void cups_create_constraints(cups_dinfo_t *dinfo);
38 static void cups_create_defaults(cups_dinfo_t *dinfo);
39 static void cups_create_media_db(cups_dinfo_t *dinfo,
40 unsigned flags);
41 static void cups_free_media_db(_cups_media_db_t *mdb);
42 static int cups_get_media_db(http_t *http, cups_dinfo_t *dinfo,
43 pwg_media_t *pwg, unsigned flags,
44 cups_size_t *size);
45 static int cups_is_close_media_db(_cups_media_db_t *a,
46 _cups_media_db_t *b);
47 static cups_array_t *cups_test_constraints(cups_dinfo_t *dinfo,
48 const char *new_option,
49 const char *new_value,
50 int num_options,
51 cups_option_t *options,
52 int *num_conflicts,
53 cups_option_t **conflicts);
54 static void cups_update_ready(http_t *http, cups_dinfo_t *dinfo);
55
56
57 /*
58 * 'cupsAddDestMediaOptions()' - Add the option corresponding to the specified media size.
59 *
60 * @since CUPS 2.3@
61 */
62
63 int /* O - New number of options */
64 cupsAddDestMediaOptions(
65 http_t *http, /* I - Connection to destination */
66 cups_dest_t *dest, /* I - Destination */
67 cups_dinfo_t *dinfo, /* I - Destination information */
68 unsigned flags, /* I - Media matching flags */
69 cups_size_t *size, /* I - Media size */
70 int num_options, /* I - Current number of options */
71 cups_option_t **options) /* IO - Options */
72 {
73 cups_array_t *db; /* Media database */
74 _cups_media_db_t *mdb; /* Media database entry */
75 char value[2048]; /* Option value */
76
77
78 /*
79 * Range check input...
80 */
81
82 if (!http || !dest || !dinfo || !size || !options)
83 {
84 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
85 return (num_options);
86 }
87
88 /*
89 * Find the matching media size...
90 */
91
92 if (flags & CUPS_MEDIA_FLAGS_READY)
93 db = dinfo->ready_db;
94 else
95 db = dinfo->media_db;
96
97 DEBUG_printf(("1cupsAddDestMediaOptions: size->media=\"%s\"", size->media));
98
99 for (mdb = (_cups_media_db_t *)cupsArrayFirst(db); mdb; mdb = (_cups_media_db_t *)cupsArrayNext(db))
100 {
101 if (mdb->key && !strcmp(mdb->key, size->media))
102 break;
103 else if (mdb->size_name && !strcmp(mdb->size_name, size->media))
104 break;
105 }
106
107 if (!mdb)
108 {
109 for (mdb = (_cups_media_db_t *)cupsArrayFirst(db); mdb; mdb = (_cups_media_db_t *)cupsArrayNext(db))
110 {
111 if (mdb->width == size->width && mdb->length == size->length && mdb->bottom == size->bottom && mdb->left == size->left && mdb->right == size->right && mdb->top == size->top)
112 break;
113 }
114 }
115
116 if (!mdb)
117 {
118 for (mdb = (_cups_media_db_t *)cupsArrayFirst(db); mdb; mdb = (_cups_media_db_t *)cupsArrayNext(db))
119 {
120 if (mdb->width == size->width && mdb->length == size->length)
121 break;
122 }
123 }
124
125 if (!mdb)
126 {
127 DEBUG_puts("1cupsAddDestMediaOptions: Unable to find matching size.");
128 return (num_options);
129 }
130
131 DEBUG_printf(("1cupsAddDestMediaOptions: MATCH mdb%p [key=\"%s\" size_name=\"%s\" source=\"%s\" type=\"%s\" width=%d length=%d B%d L%d R%d T%d]", (void *)mdb, mdb->key, mdb->size_name, mdb->source, mdb->type, mdb->width, mdb->length, mdb->bottom, mdb->left, mdb->right, mdb->top));
132
133 if (mdb->source)
134 {
135 if (mdb->type)
136 snprintf(value, sizeof(value), "{media-size={x-dimension=%d y-dimension=%d} media-bottom-margin=%d media-left-margin=%d media-right-margin=%d media-top-margin=%d media-source=\"%s\" media-type=\"%s\"}", mdb->width, mdb->length, mdb->bottom, mdb->left, mdb->right, mdb->top, mdb->source, mdb->type);
137 else
138 snprintf(value, sizeof(value), "{media-size={x-dimension=%d y-dimension=%d} media-bottom-margin=%d media-left-margin=%d media-right-margin=%d media-top-margin=%d media-source=\"%s\"}", mdb->width, mdb->length, mdb->bottom, mdb->left, mdb->right, mdb->top, mdb->source);
139 }
140 else if (mdb->type)
141 {
142 snprintf(value, sizeof(value), "{media-size={x-dimension=%d y-dimension=%d} media-bottom-margin=%d media-left-margin=%d media-right-margin=%d media-top-margin=%d media-type=\"%s\"}", mdb->width, mdb->length, mdb->bottom, mdb->left, mdb->right, mdb->top, mdb->type);
143 }
144 else
145 {
146 snprintf(value, sizeof(value), "{media-size={x-dimension=%d y-dimension=%d} media-bottom-margin=%d media-left-margin=%d media-right-margin=%d media-top-margin=%d}", mdb->width, mdb->length, mdb->bottom, mdb->left, mdb->right, mdb->top);
147 }
148
149 num_options = cupsAddOption("media-col", value, num_options, options);
150
151 return (num_options);
152 }
153
154
155 /*
156 * 'cupsCheckDestSupported()' - Check that the option and value are supported
157 * by the destination.
158 *
159 * Returns 1 if supported, 0 otherwise.
160 *
161 * @since CUPS 1.6/macOS 10.8@
162 */
163
164 int /* O - 1 if supported, 0 otherwise */
165 cupsCheckDestSupported(
166 http_t *http, /* I - Connection to destination */
167 cups_dest_t *dest, /* I - Destination */
168 cups_dinfo_t *dinfo, /* I - Destination information */
169 const char *option, /* I - Option */
170 const char *value) /* I - Value or @code NULL@ */
171 {
172 int i; /* Looping var */
173 char temp[1024]; /* Temporary string */
174 int int_value; /* Integer value */
175 int xres_value, /* Horizontal resolution */
176 yres_value; /* Vertical resolution */
177 ipp_res_t units_value; /* Resolution units */
178 ipp_attribute_t *attr; /* Attribute */
179 _ipp_value_t *attrval; /* Current attribute value */
180
181
182 /*
183 * Get the default connection as needed...
184 */
185
186 if (!http)
187 http = _cupsConnect();
188
189 /*
190 * Range check input...
191 */
192
193 if (!http || !dest || !dinfo || !option)
194 return (0);
195
196 /*
197 * Lookup the attribute...
198 */
199
200 if (strstr(option, "-supported"))
201 attr = ippFindAttribute(dinfo->attrs, option, IPP_TAG_ZERO);
202 else
203 {
204 snprintf(temp, sizeof(temp), "%s-supported", option);
205 attr = ippFindAttribute(dinfo->attrs, temp, IPP_TAG_ZERO);
206 }
207
208 if (!attr)
209 return (0);
210
211 if (!value)
212 return (1);
213
214 /*
215 * Compare values...
216 */
217
218 if (!strcmp(option, "media") && !strncmp(value, "custom_", 7))
219 {
220 /*
221 * Check range of custom media sizes...
222 */
223
224 pwg_media_t *pwg; /* Current PWG media size info */
225 int min_width, /* Minimum width */
226 min_length, /* Minimum length */
227 max_width, /* Maximum width */
228 max_length; /* Maximum length */
229
230 /*
231 * Get the minimum and maximum size...
232 */
233
234 min_width = min_length = INT_MAX;
235 max_width = max_length = 0;
236
237 for (i = attr->num_values, attrval = attr->values;
238 i > 0;
239 i --, attrval ++)
240 {
241 if (!strncmp(attrval->string.text, "custom_min_", 11) &&
242 (pwg = pwgMediaForPWG(attrval->string.text)) != NULL)
243 {
244 min_width = pwg->width;
245 min_length = pwg->length;
246 }
247 else if (!strncmp(attrval->string.text, "custom_max_", 11) &&
248 (pwg = pwgMediaForPWG(attrval->string.text)) != NULL)
249 {
250 max_width = pwg->width;
251 max_length = pwg->length;
252 }
253 }
254
255 /*
256 * Check the range...
257 */
258
259 if (min_width < INT_MAX && max_width > 0 &&
260 (pwg = pwgMediaForPWG(value)) != NULL &&
261 pwg->width >= min_width && pwg->width <= max_width &&
262 pwg->length >= min_length && pwg->length <= max_length)
263 return (1);
264 }
265 else
266 {
267 /*
268 * Check literal values...
269 */
270
271 switch (attr->value_tag)
272 {
273 case IPP_TAG_INTEGER :
274 case IPP_TAG_ENUM :
275 int_value = atoi(value);
276
277 for (i = 0; i < attr->num_values; i ++)
278 if (attr->values[i].integer == int_value)
279 return (1);
280 break;
281
282 case IPP_TAG_BOOLEAN :
283 return (attr->values[0].boolean);
284
285 case IPP_TAG_RANGE :
286 int_value = atoi(value);
287
288 for (i = 0; i < attr->num_values; i ++)
289 if (int_value >= attr->values[i].range.lower &&
290 int_value <= attr->values[i].range.upper)
291 return (1);
292 break;
293
294 case IPP_TAG_RESOLUTION :
295 if (sscanf(value, "%dx%d%15s", &xres_value, &yres_value, temp) != 3)
296 {
297 if (sscanf(value, "%d%15s", &xres_value, temp) != 2)
298 return (0);
299
300 yres_value = xres_value;
301 }
302
303 if (!strcmp(temp, "dpi"))
304 units_value = IPP_RES_PER_INCH;
305 else if (!strcmp(temp, "dpc") || !strcmp(temp, "dpcm"))
306 units_value = IPP_RES_PER_CM;
307 else
308 return (0);
309
310 for (i = attr->num_values, attrval = attr->values;
311 i > 0;
312 i --, attrval ++)
313 {
314 if (attrval->resolution.xres == xres_value &&
315 attrval->resolution.yres == yres_value &&
316 attrval->resolution.units == units_value)
317 return (1);
318 }
319 break;
320
321 case IPP_TAG_TEXT :
322 case IPP_TAG_NAME :
323 case IPP_TAG_KEYWORD :
324 case IPP_TAG_CHARSET :
325 case IPP_TAG_URI :
326 case IPP_TAG_URISCHEME :
327 case IPP_TAG_MIMETYPE :
328 case IPP_TAG_LANGUAGE :
329 case IPP_TAG_TEXTLANG :
330 case IPP_TAG_NAMELANG :
331 for (i = 0; i < attr->num_values; i ++)
332 if (!strcmp(attr->values[i].string.text, value))
333 return (1);
334 break;
335
336 default :
337 break;
338 }
339 }
340
341 /*
342 * If we get there the option+value is not supported...
343 */
344
345 return (0);
346 }
347
348
349 /*
350 * 'cupsCopyDestConflicts()' - Get conflicts and resolutions for a new
351 * option/value pair.
352 *
353 * "num_options" and "options" represent the currently selected options by the
354 * user. "new_option" and "new_value" are the setting the user has just
355 * changed.
356 *
357 * Returns 1 if there is a conflict, 0 if there are no conflicts, and -1 if
358 * there was an unrecoverable error such as a resolver loop.
359 *
360 * If "num_conflicts" and "conflicts" are not @code NULL@, they are set to
361 * contain the list of conflicting option/value pairs. Similarly, if
362 * "num_resolved" and "resolved" are not @code NULL@ they will be set to the
363 * list of changes needed to resolve the conflict.
364 *
365 * If cupsCopyDestConflicts returns 1 but "num_resolved" and "resolved" are set
366 * to 0 and @code NULL@, respectively, then the conflict cannot be resolved.
367 *
368 * @since CUPS 1.6/macOS 10.8@
369 */
370
371 int /* O - 1 if there is a conflict, 0 if none, -1 on error */
372 cupsCopyDestConflicts(
373 http_t *http, /* I - Connection to destination */
374 cups_dest_t *dest, /* I - Destination */
375 cups_dinfo_t *dinfo, /* I - Destination information */
376 int num_options, /* I - Number of current options */
377 cups_option_t *options, /* I - Current options */
378 const char *new_option, /* I - New option */
379 const char *new_value, /* I - New value */
380 int *num_conflicts, /* O - Number of conflicting options */
381 cups_option_t **conflicts, /* O - Conflicting options */
382 int *num_resolved, /* O - Number of options to resolve */
383 cups_option_t **resolved) /* O - Resolved options */
384 {
385 int i, /* Looping var */
386 have_conflicts = 0, /* Do we have conflicts? */
387 changed, /* Did we change something? */
388 tries, /* Number of tries for resolution */
389 num_myconf = 0, /* My number of conflicting options */
390 num_myres = 0; /* My number of resolved options */
391 cups_option_t *myconf = NULL, /* My conflicting options */
392 *myres = NULL, /* My resolved options */
393 *myoption, /* My current option */
394 *option; /* Current option */
395 cups_array_t *active = NULL, /* Active conflicts */
396 *pass = NULL, /* Resolvers for this pass */
397 *resolvers = NULL, /* Resolvers we have used */
398 *test; /* Test array for conflicts */
399 _cups_dconstres_t *c, /* Current constraint */
400 *r; /* Current resolver */
401 ipp_attribute_t *attr; /* Current attribute */
402 char value[2048]; /* Current attribute value as string */
403 const char *myvalue; /* Current value of an option */
404
405
406 /*
407 * Clear returned values...
408 */
409
410 if (num_conflicts)
411 *num_conflicts = 0;
412
413 if (conflicts)
414 *conflicts = NULL;
415
416 if (num_resolved)
417 *num_resolved = 0;
418
419 if (resolved)
420 *resolved = NULL;
421
422 /*
423 * Get the default connection as needed...
424 */
425
426 if (!http)
427 http = _cupsConnect();
428
429 /*
430 * Range check input...
431 */
432
433 if (!http || !dest || !dinfo ||
434 (num_conflicts != NULL) != (conflicts != NULL) ||
435 (num_resolved != NULL) != (resolved != NULL))
436 return (0);
437
438 /*
439 * Load constraints as needed...
440 */
441
442 if (!dinfo->constraints)
443 cups_create_constraints(dinfo);
444
445 if (cupsArrayCount(dinfo->constraints) == 0)
446 return (0);
447
448 if (!dinfo->num_defaults)
449 cups_create_defaults(dinfo);
450
451 /*
452 * If we are resolving, create a shadow array...
453 */
454
455 if (num_resolved)
456 {
457 for (i = num_options, option = options; i > 0; i --, option ++)
458 num_myres = cupsAddOption(option->name, option->value, num_myres, &myres);
459
460 if (new_option && new_value)
461 num_myres = cupsAddOption(new_option, new_value, num_myres, &myres);
462 }
463 else
464 {
465 num_myres = num_options;
466 myres = options;
467 }
468
469 /*
470 * Check for any conflicts...
471 */
472
473 if (num_resolved)
474 pass = cupsArrayNew((cups_array_func_t)cups_compare_dconstres, NULL);
475
476 for (tries = 0; tries < 100; tries ++)
477 {
478 /*
479 * Check for any conflicts...
480 */
481
482 if (num_conflicts || num_resolved)
483 {
484 cupsFreeOptions(num_myconf, myconf);
485
486 num_myconf = 0;
487 myconf = NULL;
488 active = cups_test_constraints(dinfo, new_option, new_value,
489 num_myres, myres, &num_myconf,
490 &myconf);
491 }
492 else
493 active = cups_test_constraints(dinfo, new_option, new_value, num_myres,
494 myres, NULL, NULL);
495
496 have_conflicts = (active != NULL);
497
498 if (!active || !num_resolved)
499 break; /* All done */
500
501 /*
502 * Scan the constraints that were triggered to apply resolvers...
503 */
504
505 if (!resolvers)
506 resolvers = cupsArrayNew((cups_array_func_t)cups_compare_dconstres, NULL);
507
508 for (c = (_cups_dconstres_t *)cupsArrayFirst(active), changed = 0;
509 c;
510 c = (_cups_dconstres_t *)cupsArrayNext(active))
511 {
512 if (cupsArrayFind(pass, c))
513 continue; /* Already applied this resolver... */
514
515 if (cupsArrayFind(resolvers, c))
516 {
517 DEBUG_printf(("1cupsCopyDestConflicts: Resolver loop with %s.",
518 c->name));
519 have_conflicts = -1;
520 goto cleanup;
521 }
522
523 if ((r = cupsArrayFind(dinfo->resolvers, c)) == NULL)
524 {
525 DEBUG_printf(("1cupsCopyDestConflicts: Resolver %s not found.",
526 c->name));
527 have_conflicts = -1;
528 goto cleanup;
529 }
530
531 /*
532 * Add the options from the resolver...
533 */
534
535 cupsArrayAdd(pass, r);
536 cupsArrayAdd(resolvers, r);
537
538 for (attr = ippFirstAttribute(r->collection);
539 attr;
540 attr = ippNextAttribute(r->collection))
541 {
542 if (new_option && !strcmp(attr->name, new_option))
543 continue; /* Ignore this if we just changed it */
544
545 if (ippAttributeString(attr, value, sizeof(value)) >= sizeof(value))
546 continue; /* Ignore if the value is too long */
547
548 if ((test = cups_test_constraints(dinfo, attr->name, value, num_myres,
549 myres, NULL, NULL)) == NULL)
550 {
551 /*
552 * That worked, flag it...
553 */
554
555 changed = 1;
556 }
557 else
558 cupsArrayDelete(test);
559
560 /*
561 * Add the option/value from the resolver regardless of whether it
562 * worked; this makes sure that we can cascade several changes to
563 * make things resolve...
564 */
565
566 num_myres = cupsAddOption(attr->name, value, num_myres, &myres);
567 }
568 }
569
570 if (!changed)
571 {
572 DEBUG_puts("1cupsCopyDestConflicts: Unable to resolve constraints.");
573 have_conflicts = -1;
574 goto cleanup;
575 }
576
577 cupsArrayClear(pass);
578
579 cupsArrayDelete(active);
580 active = NULL;
581 }
582
583 if (tries >= 100)
584 {
585 DEBUG_puts("1cupsCopyDestConflicts: Unable to resolve after 100 tries.");
586 have_conflicts = -1;
587 goto cleanup;
588 }
589
590 /*
591 * Copy resolved options as needed...
592 */
593
594 if (num_resolved)
595 {
596 for (i = num_myres, myoption = myres; i > 0; i --, myoption ++)
597 {
598 if ((myvalue = cupsGetOption(myoption->name, num_options,
599 options)) == NULL ||
600 strcmp(myvalue, myoption->value))
601 {
602 if (new_option && !strcmp(new_option, myoption->name) &&
603 new_value && !strcmp(new_value, myoption->value))
604 continue;
605
606 *num_resolved = cupsAddOption(myoption->name, myoption->value,
607 *num_resolved, resolved);
608 }
609 }
610 }
611
612 /*
613 * Clean up...
614 */
615
616 cleanup:
617
618 cupsArrayDelete(active);
619 cupsArrayDelete(pass);
620 cupsArrayDelete(resolvers);
621
622 if (num_resolved)
623 {
624 /*
625 * Free shadow copy of options...
626 */
627
628 cupsFreeOptions(num_myres, myres);
629 }
630
631 if (num_conflicts)
632 {
633 /*
634 * Return conflicting options to caller...
635 */
636
637 *num_conflicts = num_myconf;
638 *conflicts = myconf;
639 }
640 else
641 {
642 /*
643 * Free conflicting options...
644 */
645
646 cupsFreeOptions(num_myconf, myconf);
647 }
648
649 return (have_conflicts);
650 }
651
652
653 /*
654 * 'cupsCopyDestInfo()' - Get the supported values/capabilities for the
655 * destination.
656 *
657 * The caller is responsible for calling @link cupsFreeDestInfo@ on the return
658 * value. @code NULL@ is returned on error.
659 *
660 * @since CUPS 1.6/macOS 10.8@
661 */
662
663 cups_dinfo_t * /* O - Destination information */
664 cupsCopyDestInfo(
665 http_t *http, /* I - Connection to destination */
666 cups_dest_t *dest) /* I - Destination */
667 {
668 cups_dinfo_t *dinfo; /* Destination information */
669 ipp_t *request, /* Get-Printer-Attributes request */
670 *response; /* Supported attributes */
671 int tries, /* Number of tries so far */
672 delay, /* Current retry delay */
673 prev_delay; /* Next retry delay */
674 const char *uri; /* Printer URI */
675 char resource[1024]; /* Resource path */
676 int version; /* IPP version */
677 ipp_status_t status; /* Status of request */
678 static const char * const requested_attrs[] =
679 { /* Requested attributes */
680 "job-template",
681 "media-col-database",
682 "printer-description"
683 };
684
685
686 DEBUG_printf(("cupsCopyDestSupported(http=%p, dest=%p(%s))", (void *)http, (void *)dest, dest ? dest->name : ""));
687
688 /*
689 * Get the default connection as needed...
690 */
691
692 if (!http)
693 http = _cupsConnect();
694
695 /*
696 * Range check input...
697 */
698
699 if (!http || !dest)
700 return (NULL);
701
702 /*
703 * Get the printer URI and resource path...
704 */
705
706 if ((uri = _cupsGetDestResource(dest, resource, sizeof(resource))) == NULL)
707 return (NULL);
708
709 /*
710 * Get the supported attributes...
711 */
712
713 delay = 1;
714 prev_delay = 1;
715 tries = 0;
716 version = 20;
717
718 do
719 {
720 /*
721 * Send a Get-Printer-Attributes request...
722 */
723
724 request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
725 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
726 uri);
727 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
728 "requesting-user-name", NULL, cupsUser());
729 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
730 "requested-attributes",
731 (int)(sizeof(requested_attrs) / sizeof(requested_attrs[0])),
732 NULL, requested_attrs);
733 response = cupsDoRequest(http, request, resource);
734 status = cupsLastError();
735
736 if (status > IPP_STATUS_OK_IGNORED_OR_SUBSTITUTED)
737 {
738 DEBUG_printf(("cupsCopyDestSupported: Get-Printer-Attributes for '%s' "
739 "returned %s (%s)", dest->name, ippErrorString(status),
740 cupsLastErrorString()));
741
742 ippDelete(response);
743 response = NULL;
744
745 if (status == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED && version > 11)
746 version = 11;
747 else if (status == IPP_STATUS_ERROR_BUSY)
748 {
749 sleep((unsigned)delay);
750
751 delay = _cupsNextDelay(delay, &prev_delay);
752 }
753 else
754 return (NULL);
755 }
756
757 tries ++;
758 }
759 while (!response && tries < 10);
760
761 if (!response)
762 return (NULL);
763
764 /*
765 * Allocate a cups_dinfo_t structure and return it...
766 */
767
768 if ((dinfo = calloc(1, sizeof(cups_dinfo_t))) == NULL)
769 {
770 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
771 ippDelete(response);
772 return (NULL);
773 }
774
775 dinfo->version = version;
776 dinfo->uri = uri;
777 dinfo->resource = _cupsStrAlloc(resource);
778 dinfo->attrs = response;
779
780 return (dinfo);
781 }
782
783
784 /*
785 * 'cupsFindDestDefault()' - Find the default value(s) for the given option.
786 *
787 * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
788 * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
789 * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
790 * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
791 * functions to inspect the default value(s) as needed.
792 *
793 * @since CUPS 1.7/macOS 10.9@
794 */
795
796 ipp_attribute_t * /* O - Default attribute or @code NULL@ for none */
797 cupsFindDestDefault(
798 http_t *http, /* I - Connection to destination */
799 cups_dest_t *dest, /* I - Destination */
800 cups_dinfo_t *dinfo, /* I - Destination information */
801 const char *option) /* I - Option/attribute name */
802 {
803 char name[IPP_MAX_NAME]; /* Attribute name */
804
805
806 /*
807 * Get the default connection as needed...
808 */
809
810 if (!http)
811 http = _cupsConnect();
812
813 /*
814 * Range check input...
815 */
816
817 if (!http || !dest || !dinfo || !option)
818 {
819 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
820 return (NULL);
821 }
822
823 /*
824 * Find and return the attribute...
825 */
826
827 snprintf(name, sizeof(name), "%s-default", option);
828 return (ippFindAttribute(dinfo->attrs, name, IPP_TAG_ZERO));
829 }
830
831
832 /*
833 * 'cupsFindDestReady()' - Find the default value(s) for the given option.
834 *
835 * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
836 * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
837 * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
838 * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
839 * functions to inspect the default value(s) as needed.
840 *
841 * @since CUPS 1.7/macOS 10.9@
842 */
843
844 ipp_attribute_t * /* O - Default attribute or @code NULL@ for none */
845 cupsFindDestReady(
846 http_t *http, /* I - Connection to destination */
847 cups_dest_t *dest, /* I - Destination */
848 cups_dinfo_t *dinfo, /* I - Destination information */
849 const char *option) /* I - Option/attribute name */
850 {
851 char name[IPP_MAX_NAME]; /* Attribute name */
852
853
854 /*
855 * Get the default connection as needed...
856 */
857
858 if (!http)
859 http = _cupsConnect();
860
861 /*
862 * Range check input...
863 */
864
865 if (!http || !dest || !dinfo || !option)
866 {
867 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
868 return (NULL);
869 }
870
871 /*
872 * Find and return the attribute...
873 */
874
875 cups_update_ready(http, dinfo);
876
877 snprintf(name, sizeof(name), "%s-ready", option);
878 return (ippFindAttribute(dinfo->ready_attrs, name, IPP_TAG_ZERO));
879 }
880
881
882 /*
883 * 'cupsFindDestSupported()' - Find the default value(s) for the given option.
884 *
885 * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
886 * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
887 * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
888 * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
889 * functions to inspect the default value(s) as needed.
890 *
891 * @since CUPS 1.7/macOS 10.9@
892 */
893
894 ipp_attribute_t * /* O - Default attribute or @code NULL@ for none */
895 cupsFindDestSupported(
896 http_t *http, /* I - Connection to destination */
897 cups_dest_t *dest, /* I - Destination */
898 cups_dinfo_t *dinfo, /* I - Destination information */
899 const char *option) /* I - Option/attribute name */
900 {
901 char name[IPP_MAX_NAME]; /* Attribute name */
902
903
904 /*
905 * Get the default connection as needed...
906 */
907
908 if (!http)
909 http = _cupsConnect();
910
911 /*
912 * Range check input...
913 */
914
915 if (!http || !dest || !dinfo || !option)
916 {
917 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
918 return (NULL);
919 }
920
921 /*
922 * Find and return the attribute...
923 */
924
925 snprintf(name, sizeof(name), "%s-supported", option);
926 return (ippFindAttribute(dinfo->attrs, name, IPP_TAG_ZERO));
927 }
928
929
930 /*
931 * 'cupsFreeDestInfo()' - Free destination information obtained using
932 * @link cupsCopyDestInfo@.
933 *
934 * @since CUPS 1.6/macOS 10.8@
935 */
936
937 void
938 cupsFreeDestInfo(cups_dinfo_t *dinfo) /* I - Destination information */
939 {
940 /*
941 * Range check input...
942 */
943
944 if (!dinfo)
945 return;
946
947 /*
948 * Free memory and return...
949 */
950
951 _cupsStrFree(dinfo->resource);
952
953 cupsArrayDelete(dinfo->constraints);
954 cupsArrayDelete(dinfo->resolvers);
955
956 cupsArrayDelete(dinfo->localizations);
957
958 cupsArrayDelete(dinfo->media_db);
959
960 cupsArrayDelete(dinfo->cached_db);
961
962 ippDelete(dinfo->ready_attrs);
963 cupsArrayDelete(dinfo->ready_db);
964
965 ippDelete(dinfo->attrs);
966
967 free(dinfo);
968 }
969
970
971 /*
972 * 'cupsGetDestMediaByIndex()' - Get a media name, dimension, and margins for a
973 * specific size.
974 *
975 * The @code flags@ parameter determines which set of media are indexed. For
976 * example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will get the Nth
977 * borderless size supported by the printer.
978 *
979 * @since CUPS 1.7/macOS 10.9@
980 */
981
982 int /* O - 1 on success, 0 on failure */
983 cupsGetDestMediaByIndex(
984 http_t *http, /* I - Connection to destination */
985 cups_dest_t *dest, /* I - Destination */
986 cups_dinfo_t *dinfo, /* I - Destination information */
987 int n, /* I - Media size number (0-based) */
988 unsigned flags, /* I - Media flags */
989 cups_size_t *size) /* O - Media size information */
990 {
991 _cups_media_db_t *nsize; /* Size for N */
992 pwg_media_t *pwg; /* PWG media name for size */
993
994
995 /*
996 * Get the default connection as needed...
997 */
998
999 if (!http)
1000 http = _cupsConnect();
1001
1002 /*
1003 * Range check input...
1004 */
1005
1006 if (size)
1007 memset(size, 0, sizeof(cups_size_t));
1008
1009 if (!http || !dest || !dinfo || n < 0 || !size)
1010 {
1011 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
1012 return (0);
1013 }
1014
1015 /*
1016 * Load media list as needed...
1017 */
1018
1019 if (flags & CUPS_MEDIA_FLAGS_READY)
1020 cups_update_ready(http, dinfo);
1021
1022 if (!dinfo->cached_db || dinfo->cached_flags != flags)
1023 cups_create_cached(http, dinfo, flags);
1024
1025 /*
1026 * Copy the size over and return...
1027 */
1028
1029 if ((nsize = (_cups_media_db_t *)cupsArrayIndex(dinfo->cached_db, n)) == NULL)
1030 {
1031 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
1032 return (0);
1033 }
1034
1035 if (nsize->key)
1036 strlcpy(size->media, nsize->key, sizeof(size->media));
1037 else if (nsize->size_name)
1038 strlcpy(size->media, nsize->size_name, sizeof(size->media));
1039 else if ((pwg = pwgMediaForSize(nsize->width, nsize->length)) != NULL)
1040 strlcpy(size->media, pwg->pwg, sizeof(size->media));
1041 else
1042 {
1043 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
1044 return (0);
1045 }
1046
1047 size->width = nsize->width;
1048 size->length = nsize->length;
1049 size->bottom = nsize->bottom;
1050 size->left = nsize->left;
1051 size->right = nsize->right;
1052 size->top = nsize->top;
1053
1054 return (1);
1055 }
1056
1057
1058 /*
1059 * 'cupsGetDestMediaByName()' - Get media names, dimensions, and margins.
1060 *
1061 * The "media" string is a PWG media name. "Flags" provides some matching
1062 * guidance (multiple flags can be combined):
1063 *
1064 * CUPS_MEDIA_FLAGS_DEFAULT = find the closest size supported by the printer,
1065 * CUPS_MEDIA_FLAGS_BORDERLESS = find a borderless size,
1066 * CUPS_MEDIA_FLAGS_DUPLEX = find a size compatible with 2-sided printing,
1067 * CUPS_MEDIA_FLAGS_EXACT = find an exact match for the size, and
1068 * CUPS_MEDIA_FLAGS_READY = if the printer supports media sensing, find the
1069 * size amongst the "ready" media.
1070 *
1071 * The matching result (if any) is returned in the "cups_size_t" structure.
1072 *
1073 * Returns 1 when there is a match and 0 if there is not a match.
1074 *
1075 * @since CUPS 1.6/macOS 10.8@
1076 */
1077
1078 int /* O - 1 on match, 0 on failure */
1079 cupsGetDestMediaByName(
1080 http_t *http, /* I - Connection to destination */
1081 cups_dest_t *dest, /* I - Destination */
1082 cups_dinfo_t *dinfo, /* I - Destination information */
1083 const char *media, /* I - Media name */
1084 unsigned flags, /* I - Media matching flags */
1085 cups_size_t *size) /* O - Media size information */
1086 {
1087 pwg_media_t *pwg; /* PWG media info */
1088
1089
1090 /*
1091 * Get the default connection as needed...
1092 */
1093
1094 if (!http)
1095 http = _cupsConnect();
1096
1097 /*
1098 * Range check input...
1099 */
1100
1101 if (size)
1102 memset(size, 0, sizeof(cups_size_t));
1103
1104 if (!http || !dest || !dinfo || !media || !size)
1105 {
1106 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
1107 return (0);
1108 }
1109
1110 /*
1111 * Lookup the media size name...
1112 */
1113
1114 if ((pwg = pwgMediaForPWG(media)) == NULL)
1115 if ((pwg = pwgMediaForLegacy(media)) == NULL)
1116 {
1117 DEBUG_printf(("1cupsGetDestMediaByName: Unknown size '%s'.", media));
1118 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unknown media size name."), 1);
1119 return (0);
1120 }
1121
1122 /*
1123 * Lookup the size...
1124 */
1125
1126 return (cups_get_media_db(http, dinfo, pwg, flags, size));
1127 }
1128
1129
1130 /*
1131 * 'cupsGetDestMediaBySize()' - Get media names, dimensions, and margins.
1132 *
1133 * "Width" and "length" are the dimensions in hundredths of millimeters.
1134 * "Flags" provides some matching guidance (multiple flags can be combined):
1135 *
1136 * CUPS_MEDIA_FLAGS_DEFAULT = find the closest size supported by the printer,
1137 * CUPS_MEDIA_FLAGS_BORDERLESS = find a borderless size,
1138 * CUPS_MEDIA_FLAGS_DUPLEX = find a size compatible with 2-sided printing,
1139 * CUPS_MEDIA_FLAGS_EXACT = find an exact match for the size, and
1140 * CUPS_MEDIA_FLAGS_READY = if the printer supports media sensing, find the
1141 * size amongst the "ready" media.
1142 *
1143 * The matching result (if any) is returned in the "cups_size_t" structure.
1144 *
1145 * Returns 1 when there is a match and 0 if there is not a match.
1146 *
1147 * @since CUPS 1.6/macOS 10.8@
1148 */
1149
1150 int /* O - 1 on match, 0 on failure */
1151 cupsGetDestMediaBySize(
1152 http_t *http, /* I - Connection to destination */
1153 cups_dest_t *dest, /* I - Destination */
1154 cups_dinfo_t *dinfo, /* I - Destination information */
1155 int width, /* I - Media width in hundredths of
1156 * of millimeters */
1157 int length, /* I - Media length in hundredths of
1158 * of millimeters */
1159 unsigned flags, /* I - Media matching flags */
1160 cups_size_t *size) /* O - Media size information */
1161 {
1162 pwg_media_t *pwg; /* PWG media info */
1163
1164
1165 /*
1166 * Get the default connection as needed...
1167 */
1168
1169 if (!http)
1170 http = _cupsConnect();
1171
1172 /*
1173 * Range check input...
1174 */
1175
1176 if (size)
1177 memset(size, 0, sizeof(cups_size_t));
1178
1179 if (!http || !dest || !dinfo || width <= 0 || length <= 0 || !size)
1180 {
1181 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
1182 return (0);
1183 }
1184
1185 /*
1186 * Lookup the media size name...
1187 */
1188
1189 if ((pwg = pwgMediaForSize(width, length)) == NULL)
1190 {
1191 DEBUG_printf(("1cupsGetDestMediaBySize: Invalid size %dx%d.", width,
1192 length));
1193 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Invalid media size."), 1);
1194 return (0);
1195 }
1196
1197 /*
1198 * Lookup the size...
1199 */
1200
1201 return (cups_get_media_db(http, dinfo, pwg, flags, size));
1202 }
1203
1204
1205 /*
1206 * 'cupsGetDestMediaCount()' - Get the number of sizes supported by a
1207 * destination.
1208 *
1209 * The @code flags@ parameter determines the set of media sizes that are
1210 * counted. For example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will return
1211 * the number of borderless sizes.
1212 *
1213 * @since CUPS 1.7/macOS 10.9@
1214 */
1215
1216 int /* O - Number of sizes */
1217 cupsGetDestMediaCount(
1218 http_t *http, /* I - Connection to destination */
1219 cups_dest_t *dest, /* I - Destination */
1220 cups_dinfo_t *dinfo, /* I - Destination information */
1221 unsigned flags) /* I - Media flags */
1222 {
1223 /*
1224 * Get the default connection as needed...
1225 */
1226
1227 if (!http)
1228 http = _cupsConnect();
1229
1230 /*
1231 * Range check input...
1232 */
1233
1234 if (!http || !dest || !dinfo)
1235 {
1236 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
1237 return (0);
1238 }
1239
1240 /*
1241 * Load media list as needed...
1242 */
1243
1244 if (flags & CUPS_MEDIA_FLAGS_READY)
1245 cups_update_ready(http, dinfo);
1246
1247 if (!dinfo->cached_db || dinfo->cached_flags != flags)
1248 cups_create_cached(http, dinfo, flags);
1249
1250 return (cupsArrayCount(dinfo->cached_db));
1251 }
1252
1253
1254 /*
1255 * 'cupsGetDestMediaDefault()' - Get the default size for a destination.
1256 *
1257 * The @code flags@ parameter determines which default size is returned. For
1258 * example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will return the default
1259 * borderless size, typically US Letter or A4, but sometimes 4x6 photo media.
1260 *
1261 * @since CUPS 1.7/macOS 10.9@
1262 */
1263
1264 int /* O - 1 on success, 0 on failure */
1265 cupsGetDestMediaDefault(
1266 http_t *http, /* I - Connection to destination */
1267 cups_dest_t *dest, /* I - Destination */
1268 cups_dinfo_t *dinfo, /* I - Destination information */
1269 unsigned flags, /* I - Media flags */
1270 cups_size_t *size) /* O - Media size information */
1271 {
1272 const char *media; /* Default media size */
1273
1274
1275 /*
1276 * Get the default connection as needed...
1277 */
1278
1279 if (!http)
1280 http = _cupsConnect();
1281
1282 /*
1283 * Range check input...
1284 */
1285
1286 if (size)
1287 memset(size, 0, sizeof(cups_size_t));
1288
1289 if (!http || !dest || !dinfo || !size)
1290 {
1291 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
1292 return (0);
1293 }
1294
1295 /*
1296 * Get the default media size, if any...
1297 */
1298
1299 if ((media = cupsGetOption("media", dest->num_options, dest->options)) == NULL)
1300 media = "na_letter_8.5x11in";
1301
1302 if (cupsGetDestMediaByName(http, dest, dinfo, media, flags, size))
1303 return (1);
1304
1305 if (strcmp(media, "na_letter_8.5x11in") && cupsGetDestMediaByName(http, dest, dinfo, "iso_a4_210x297mm", flags, size))
1306 return (1);
1307
1308 if (strcmp(media, "iso_a4_210x297mm") && cupsGetDestMediaByName(http, dest, dinfo, "na_letter_8.5x11in", flags, size))
1309 return (1);
1310
1311 if ((flags & CUPS_MEDIA_FLAGS_BORDERLESS) && cupsGetDestMediaByName(http, dest, dinfo, "na_index_4x6in", flags, size))
1312 return (1);
1313
1314 /*
1315 * Fall back to the first matching media size...
1316 */
1317
1318 return (cupsGetDestMediaByIndex(http, dest, dinfo, 0, flags, size));
1319 }
1320
1321
1322 /*
1323 * 'cups_add_dconstres()' - Add a constraint or resolver to an array.
1324 */
1325
1326 static void
1327 cups_add_dconstres(
1328 cups_array_t *a, /* I - Array */
1329 ipp_t *collection) /* I - Collection value */
1330 {
1331 ipp_attribute_t *attr; /* Attribute */
1332 _cups_dconstres_t *temp; /* Current constraint/resolver */
1333
1334
1335 if ((attr = ippFindAttribute(collection, "resolver-name",
1336 IPP_TAG_NAME)) == NULL)
1337 return;
1338
1339 if ((temp = calloc(1, sizeof(_cups_dconstres_t))) == NULL)
1340 return;
1341
1342 temp->name = attr->values[0].string.text;
1343 temp->collection = collection;
1344
1345 cupsArrayAdd(a, temp);
1346 }
1347
1348
1349 /*
1350 * 'cups_collection_contains()' - Check whether test collection is contained in the matching collection.
1351 */
1352
1353 static int /* O - 1 on a match, 0 on a non-match */
1354 cups_collection_contains(ipp_t *test, /* I - Collection to test */
1355 ipp_t *match) /* I - Matching values */
1356 {
1357 int i, /* Looping var */
1358 count; /* Number of test values */
1359 ipp_attribute_t *tattr, /* Testing attribute */
1360 *mattr; /* Matching attribute */
1361 const char *tval; /* Testing string value */
1362
1363
1364 for (mattr = ippFirstAttribute(match); mattr; mattr = ippNextAttribute(match))
1365 {
1366 if ((tattr = ippFindAttribute(test, ippGetName(mattr), IPP_TAG_ZERO)) == NULL)
1367 return (0);
1368
1369 count = ippGetCount(tattr);
1370
1371 switch (ippGetValueTag(mattr))
1372 {
1373 case IPP_TAG_INTEGER :
1374 case IPP_TAG_ENUM :
1375 if (ippGetValueTag(tattr) != ippGetValueTag(mattr))
1376 return (0);
1377
1378 for (i = 0; i < count; i ++)
1379 {
1380 if (!ippContainsInteger(mattr, ippGetInteger(tattr, i)))
1381 return (0);
1382 }
1383 break;
1384
1385 case IPP_TAG_RANGE :
1386 if (ippGetValueTag(tattr) != IPP_TAG_INTEGER)
1387 return (0);
1388
1389 for (i = 0; i < count; i ++)
1390 {
1391 if (!ippContainsInteger(mattr, ippGetInteger(tattr, i)))
1392 return (0);
1393 }
1394 break;
1395
1396 case IPP_TAG_BOOLEAN :
1397 if (ippGetValueTag(tattr) != IPP_TAG_BOOLEAN || ippGetBoolean(tattr, 0) != ippGetBoolean(mattr, 0))
1398 return (0);
1399 break;
1400
1401 case IPP_TAG_TEXTLANG :
1402 case IPP_TAG_NAMELANG :
1403 case IPP_TAG_TEXT :
1404 case IPP_TAG_NAME :
1405 case IPP_TAG_KEYWORD :
1406 case IPP_TAG_URI :
1407 case IPP_TAG_URISCHEME :
1408 case IPP_TAG_CHARSET :
1409 case IPP_TAG_LANGUAGE :
1410 case IPP_TAG_MIMETYPE :
1411 for (i = 0; i < count; i ++)
1412 {
1413 if ((tval = ippGetString(tattr, i, NULL)) == NULL || !ippContainsString(mattr, tval))
1414 return (0);
1415 }
1416 break;
1417
1418 default :
1419 return (0);
1420 }
1421 }
1422
1423 return (1);
1424 }
1425
1426
1427 /*
1428 * 'cups_collection_string()' - Convert an IPP collection to an option string.
1429 */
1430
1431 static size_t /* O - Number of bytes needed */
1432 cups_collection_string(
1433 ipp_attribute_t *attr, /* I - Collection attribute */
1434 char *buffer, /* I - String buffer */
1435 size_t bufsize) /* I - Size of buffer */
1436 {
1437 int i, j, /* Looping vars */
1438 count, /* Number of collection values */
1439 mcount; /* Number of member values */
1440 ipp_t *col; /* Collection */
1441 ipp_attribute_t *first, /* First member attribute */
1442 *member; /* Member attribute */
1443 char *bufptr, /* Pointer into buffer */
1444 *bufend, /* End of buffer */
1445 temp[100]; /* Temporary string */
1446 const char *mptr; /* Pointer into member value */
1447 int mlen; /* Length of octetString */
1448
1449
1450 bufptr = buffer;
1451 bufend = buffer + bufsize - 1;
1452
1453 for (i = 0, count = ippGetCount(attr); i < count; i ++)
1454 {
1455 col = ippGetCollection(attr, i);
1456
1457 if (i)
1458 {
1459 if (bufptr < bufend)
1460 *bufptr++ = ',';
1461 else
1462 bufptr ++;
1463 }
1464
1465 if (bufptr < bufend)
1466 *bufptr++ = '{';
1467 else
1468 bufptr ++;
1469
1470 for (member = first = ippFirstAttribute(col); member; member = ippNextAttribute(col))
1471 {
1472 const char *mname = ippGetName(member);
1473
1474 if (member != first)
1475 {
1476 if (bufptr < bufend)
1477 *bufptr++ = ' ';
1478 else
1479 bufptr ++;
1480 }
1481
1482 if (ippGetValueTag(member) == IPP_TAG_BOOLEAN)
1483 {
1484 if (!ippGetBoolean(member, 0))
1485 {
1486 if (bufptr < bufend)
1487 strlcpy(bufptr, "no", bufend - bufptr + 1);
1488 bufptr += 2;
1489 }
1490
1491 if (bufptr < bufend)
1492 strlcpy(bufptr, mname, bufend - bufptr + 1);
1493 bufptr += strlen(mname);
1494 continue;
1495 }
1496
1497 if (bufptr < bufend)
1498 strlcpy(bufptr, mname, bufend - bufptr + 1);
1499 bufptr += strlen(mname);
1500
1501 if (bufptr < bufend)
1502 *bufptr++ = '=';
1503 else
1504 bufptr ++;
1505
1506 if (ippGetValueTag(member) == IPP_TAG_BEGIN_COLLECTION)
1507 {
1508 /*
1509 * Convert sub-collection...
1510 */
1511
1512 bufptr += cups_collection_string(member, bufptr, bufptr < bufend ? (size_t)(bufend - bufptr + 1) : 0);
1513 }
1514 else
1515 {
1516 /*
1517 * Convert simple type...
1518 */
1519
1520 for (j = 0, mcount = ippGetCount(member); j < mcount; j ++)
1521 {
1522 if (j)
1523 {
1524 if (bufptr < bufend)
1525 *bufptr++ = ',';
1526 else
1527 bufptr ++;
1528 }
1529
1530 switch (ippGetValueTag(member))
1531 {
1532 case IPP_TAG_INTEGER :
1533 case IPP_TAG_ENUM :
1534 bufptr += snprintf(bufptr, bufptr < bufend ? (bufend - bufptr + 1) : 0, "%d", ippGetInteger(member, j));
1535 break;
1536
1537 case IPP_TAG_STRING :
1538 if (bufptr < bufend)
1539 *bufptr++ = '\"';
1540 else
1541 bufptr ++;
1542
1543 for (mptr = (const char *)ippGetOctetString(member, j, &mlen); mlen > 0; mlen --, mptr ++)
1544 {
1545 if (*mptr == '\"' || *mptr == '\\')
1546 {
1547 if (bufptr < bufend)
1548 *bufptr++ = '\\';
1549 else
1550 bufptr ++;
1551 }
1552
1553 if (bufptr < bufend)
1554 *bufptr++ = *mptr;
1555 else
1556 bufptr ++;
1557 }
1558
1559 if (bufptr < bufend)
1560 *bufptr++ = '\"';
1561 else
1562 bufptr ++;
1563 break;
1564
1565 case IPP_TAG_DATE :
1566 {
1567 unsigned year; /* Year */
1568 const ipp_uchar_t *date = ippGetDate(member, j);
1569 /* Date value */
1570
1571 year = ((unsigned)date[0] << 8) + (unsigned)date[1];
1572
1573 if (date[9] == 0 && date[10] == 0)
1574 snprintf(temp, sizeof(temp), "%04u-%02u-%02uT%02u:%02u:%02uZ", year, date[2], date[3], date[4], date[5], date[6]);
1575 else
1576 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]);
1577
1578 if (buffer && bufptr < bufend)
1579 strlcpy(bufptr, temp, (size_t)(bufend - bufptr + 1));
1580
1581 bufptr += strlen(temp);
1582 }
1583 break;
1584
1585 case IPP_TAG_RESOLUTION :
1586 {
1587 int xres, /* Horizontal resolution */
1588 yres; /* Vertical resolution */
1589 ipp_res_t units; /* Resolution units */
1590
1591 xres = ippGetResolution(member, j, &yres, &units);
1592
1593 if (xres == yres)
1594 snprintf(temp, sizeof(temp), "%d%s", xres, units == IPP_RES_PER_INCH ? "dpi" : "dpcm");
1595 else
1596 snprintf(temp, sizeof(temp), "%dx%d%s", xres, yres, units == IPP_RES_PER_INCH ? "dpi" : "dpcm");
1597
1598 if (buffer && bufptr < bufend)
1599 strlcpy(bufptr, temp, (size_t)(bufend - bufptr + 1));
1600
1601 bufptr += strlen(temp);
1602 }
1603 break;
1604
1605 case IPP_TAG_RANGE :
1606 {
1607 int lower, /* Lower bound */
1608 upper; /* Upper bound */
1609
1610 lower = ippGetRange(member, j, &upper);
1611
1612 snprintf(temp, sizeof(temp), "%d-%d", lower, upper);
1613
1614 if (buffer && bufptr < bufend)
1615 strlcpy(bufptr, temp, (size_t)(bufend - bufptr + 1));
1616
1617 bufptr += strlen(temp);
1618 }
1619 break;
1620
1621 case IPP_TAG_TEXTLANG :
1622 case IPP_TAG_NAMELANG :
1623 case IPP_TAG_TEXT :
1624 case IPP_TAG_NAME :
1625 case IPP_TAG_KEYWORD :
1626 case IPP_TAG_URI :
1627 case IPP_TAG_URISCHEME :
1628 case IPP_TAG_CHARSET :
1629 case IPP_TAG_LANGUAGE :
1630 case IPP_TAG_MIMETYPE :
1631 if (bufptr < bufend)
1632 *bufptr++ = '\"';
1633 else
1634 bufptr ++;
1635
1636 for (mptr = ippGetString(member, j, NULL); *mptr; mptr ++)
1637 {
1638 if (*mptr == '\"' || *mptr == '\\')
1639 {
1640 if (bufptr < bufend)
1641 *bufptr++ = '\\';
1642 else
1643 bufptr ++;
1644 }
1645
1646 if (bufptr < bufend)
1647 *bufptr++ = *mptr;
1648 else
1649 bufptr ++;
1650 }
1651
1652 if (bufptr < bufend)
1653 *bufptr++ = '\"';
1654 else
1655 bufptr ++;
1656 break;
1657
1658 default :
1659 break;
1660 }
1661 }
1662 }
1663 }
1664
1665 if (bufptr < bufend)
1666 *bufptr++ = '}';
1667 else
1668 bufptr ++;
1669 }
1670
1671 *bufptr = '\0';
1672 return ((size_t)(bufptr - buffer + 1));
1673 }
1674
1675
1676 /*
1677 * 'cups_compare_dconstres()' - Compare to resolver entries.
1678 */
1679
1680 static int /* O - Result of comparison */
1681 cups_compare_dconstres(
1682 _cups_dconstres_t *a, /* I - First resolver */
1683 _cups_dconstres_t *b) /* I - Second resolver */
1684 {
1685 return (strcmp(a->name, b->name));
1686 }
1687
1688
1689 /*
1690 * 'cups_compare_media_db()' - Compare two media entries.
1691 */
1692
1693 static int /* O - Result of comparison */
1694 cups_compare_media_db(
1695 _cups_media_db_t *a, /* I - First media entries */
1696 _cups_media_db_t *b) /* I - Second media entries */
1697 {
1698 int result; /* Result of comparison */
1699
1700
1701 if ((result = a->width - b->width) == 0)
1702 result = a->length - b->length;
1703
1704 return (result);
1705 }
1706
1707
1708 /*
1709 * 'cups_copy_media_db()' - Copy a media entry.
1710 */
1711
1712 static _cups_media_db_t * /* O - New media entry */
1713 cups_copy_media_db(
1714 _cups_media_db_t *mdb) /* I - Media entry to copy */
1715 {
1716 _cups_media_db_t *temp; /* New media entry */
1717
1718
1719 if ((temp = calloc(1, sizeof(_cups_media_db_t))) == NULL)
1720 return (NULL);
1721
1722 if (mdb->color)
1723 temp->color = _cupsStrAlloc(mdb->color);
1724 if (mdb->key)
1725 temp->key = _cupsStrAlloc(mdb->key);
1726 if (mdb->info)
1727 temp->info = _cupsStrAlloc(mdb->info);
1728 if (mdb->size_name)
1729 temp->size_name = _cupsStrAlloc(mdb->size_name);
1730 if (mdb->source)
1731 temp->source = _cupsStrAlloc(mdb->source);
1732 if (mdb->type)
1733 temp->type = _cupsStrAlloc(mdb->type);
1734
1735 temp->width = mdb->width;
1736 temp->length = mdb->length;
1737 temp->bottom = mdb->bottom;
1738 temp->left = mdb->left;
1739 temp->right = mdb->right;
1740 temp->top = mdb->top;
1741
1742 return (temp);
1743 }
1744
1745
1746 /*
1747 * 'cups_create_cached()' - Create the media selection cache.
1748 */
1749
1750 static void
1751 cups_create_cached(http_t *http, /* I - Connection to destination */
1752 cups_dinfo_t *dinfo, /* I - Destination information */
1753 unsigned flags) /* I - Media selection flags */
1754 {
1755 cups_array_t *db; /* Media database array to use */
1756 _cups_media_db_t *mdb, /* Media database entry */
1757 *first; /* First entry this size */
1758
1759
1760 DEBUG_printf(("3cups_create_cached(http=%p, dinfo=%p, flags=%u)", (void *)http, (void *)dinfo, flags));
1761
1762 if (dinfo->cached_db)
1763 cupsArrayDelete(dinfo->cached_db);
1764
1765 dinfo->cached_db = cupsArrayNew(NULL, NULL);
1766 dinfo->cached_flags = flags;
1767
1768 if (flags & CUPS_MEDIA_FLAGS_READY)
1769 {
1770 DEBUG_puts("4cups_create_cached: ready media");
1771
1772 cups_update_ready(http, dinfo);
1773 db = dinfo->ready_db;
1774 }
1775 else
1776 {
1777 DEBUG_puts("4cups_create_cached: supported media");
1778
1779 if (!dinfo->media_db)
1780 cups_create_media_db(dinfo, CUPS_MEDIA_FLAGS_DEFAULT);
1781
1782 db = dinfo->media_db;
1783 }
1784
1785 for (mdb = (_cups_media_db_t *)cupsArrayFirst(db), first = mdb;
1786 mdb;
1787 mdb = (_cups_media_db_t *)cupsArrayNext(db))
1788 {
1789 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));
1790
1791 if (flags & CUPS_MEDIA_FLAGS_BORDERLESS)
1792 {
1793 if (!mdb->left && !mdb->right && !mdb->top && !mdb->bottom)
1794 {
1795 DEBUG_printf(("4cups_create_cached: add %p", (void *)mdb));
1796 cupsArrayAdd(dinfo->cached_db, mdb);
1797 }
1798 }
1799 else if (flags & CUPS_MEDIA_FLAGS_DUPLEX)
1800 {
1801 if (first->width != mdb->width || first->length != mdb->length)
1802 {
1803 DEBUG_printf(("4cups_create_cached: add %p", (void *)first));
1804 cupsArrayAdd(dinfo->cached_db, first);
1805 first = mdb;
1806 }
1807 else if (mdb->left >= first->left && mdb->right >= first->right && mdb->top >= first->top && mdb->bottom >= first->bottom &&
1808 (mdb->left != first->left || mdb->right != first->right || mdb->top != first->top || mdb->bottom != first->bottom))
1809 first = mdb;
1810 }
1811 else
1812 {
1813 DEBUG_printf(("4cups_create_cached: add %p", (void *)mdb));
1814 cupsArrayAdd(dinfo->cached_db, mdb);
1815 }
1816 }
1817
1818 if (flags & CUPS_MEDIA_FLAGS_DUPLEX)
1819 {
1820 DEBUG_printf(("4cups_create_cached: add %p", (void *)first));
1821 cupsArrayAdd(dinfo->cached_db, first);
1822 }
1823 }
1824
1825
1826 /*
1827 * 'cups_create_constraints()' - Create the constraints and resolvers arrays.
1828 */
1829
1830 static void
1831 cups_create_constraints(
1832 cups_dinfo_t *dinfo) /* I - Destination information */
1833 {
1834 int i; /* Looping var */
1835 ipp_attribute_t *attr; /* Attribute */
1836 _ipp_value_t *val; /* Current value */
1837
1838
1839 dinfo->constraints = cupsArrayNew3(NULL, NULL, NULL, 0, NULL,
1840 (cups_afree_func_t)free);
1841 dinfo->resolvers = cupsArrayNew3((cups_array_func_t)cups_compare_dconstres,
1842 NULL, NULL, 0, NULL,
1843 (cups_afree_func_t)free);
1844
1845 if ((attr = ippFindAttribute(dinfo->attrs, "job-constraints-supported",
1846 IPP_TAG_BEGIN_COLLECTION)) != NULL)
1847 {
1848 for (i = attr->num_values, val = attr->values; i > 0; i --, val ++)
1849 cups_add_dconstres(dinfo->constraints, val->collection);
1850 }
1851
1852 if ((attr = ippFindAttribute(dinfo->attrs, "job-resolvers-supported",
1853 IPP_TAG_BEGIN_COLLECTION)) != NULL)
1854 {
1855 for (i = attr->num_values, val = attr->values; i > 0; i --, val ++)
1856 cups_add_dconstres(dinfo->resolvers, val->collection);
1857 }
1858 }
1859
1860
1861 /*
1862 * 'cups_create_defaults()' - Create the -default option array.
1863 */
1864
1865 static void
1866 cups_create_defaults(
1867 cups_dinfo_t *dinfo) /* I - Destination information */
1868 {
1869 ipp_attribute_t *attr; /* Current attribute */
1870 char name[IPP_MAX_NAME + 1],
1871 /* Current name */
1872 *nameptr, /* Pointer into current name */
1873 value[2048]; /* Current value */
1874
1875
1876 /*
1877 * Iterate through the printer attributes looking for xxx-default and adding
1878 * xxx=value to the defaults option array.
1879 */
1880
1881 for (attr = ippFirstAttribute(dinfo->attrs); attr; attr = ippNextAttribute(dinfo->attrs))
1882 {
1883 if (!ippGetName(attr) || ippGetGroupTag(attr) != IPP_TAG_PRINTER)
1884 continue;
1885
1886 strlcpy(name, ippGetName(attr), sizeof(name));
1887 if ((nameptr = name + strlen(name) - 8) <= name || strcmp(nameptr, "-default"))
1888 continue;
1889
1890 *nameptr = '\0';
1891
1892 if (ippGetValueTag(attr) == IPP_TAG_BEGIN_COLLECTION)
1893 {
1894 if (cups_collection_string(attr, value, sizeof(value)) >= sizeof(value))
1895 continue;
1896 }
1897 else if (ippAttributeString(attr, value, sizeof(value)) >= sizeof(value))
1898 continue;
1899
1900 dinfo->num_defaults = cupsAddOption(name, value, dinfo->num_defaults, &dinfo->defaults);
1901 }
1902 }
1903
1904
1905 /*
1906 * 'cups_create_media_db()' - Create the media database.
1907 */
1908
1909 static void
1910 cups_create_media_db(
1911 cups_dinfo_t *dinfo, /* I - Destination information */
1912 unsigned flags) /* I - Media flags */
1913 {
1914 int i; /* Looping var */
1915 _ipp_value_t *val; /* Current value */
1916 ipp_attribute_t *media_col_db, /* media-col-database */
1917 *media_attr, /* media-xxx */
1918 *x_dimension, /* x-dimension */
1919 *y_dimension; /* y-dimension */
1920 pwg_media_t *pwg; /* PWG media info */
1921 cups_array_t *db; /* New media database array */
1922 _cups_media_db_t mdb; /* Media entry */
1923 char media_key[256]; /* Synthesized media-key value */
1924
1925
1926 db = cupsArrayNew3((cups_array_func_t)cups_compare_media_db,
1927 NULL, NULL, 0,
1928 (cups_acopy_func_t)cups_copy_media_db,
1929 (cups_afree_func_t)cups_free_media_db);
1930
1931 if (flags == CUPS_MEDIA_FLAGS_READY)
1932 {
1933 dinfo->ready_db = db;
1934
1935 media_col_db = ippFindAttribute(dinfo->ready_attrs, "media-col-ready",
1936 IPP_TAG_BEGIN_COLLECTION);
1937 media_attr = ippFindAttribute(dinfo->ready_attrs, "media-ready",
1938 IPP_TAG_ZERO);
1939 }
1940 else
1941 {
1942 dinfo->media_db = db;
1943 dinfo->min_size.width = INT_MAX;
1944 dinfo->min_size.length = INT_MAX;
1945 dinfo->max_size.width = 0;
1946 dinfo->max_size.length = 0;
1947
1948 media_col_db = ippFindAttribute(dinfo->attrs, "media-col-database",
1949 IPP_TAG_BEGIN_COLLECTION);
1950 media_attr = ippFindAttribute(dinfo->attrs, "media-supported",
1951 IPP_TAG_ZERO);
1952 }
1953
1954 if (media_col_db)
1955 {
1956 _ipp_value_t *custom = NULL; /* Custom size range value */
1957
1958 for (i = media_col_db->num_values, val = media_col_db->values;
1959 i > 0;
1960 i --, val ++)
1961 {
1962 memset(&mdb, 0, sizeof(mdb));
1963
1964 if ((media_attr = ippFindAttribute(val->collection, "media-size",
1965 IPP_TAG_BEGIN_COLLECTION)) != NULL)
1966 {
1967 ipp_t *media_size = media_attr->values[0].collection;
1968 /* media-size collection value */
1969
1970 if ((x_dimension = ippFindAttribute(media_size, "x-dimension",
1971 IPP_TAG_INTEGER)) != NULL &&
1972 (y_dimension = ippFindAttribute(media_size, "y-dimension",
1973 IPP_TAG_INTEGER)) != NULL)
1974 {
1975 /*
1976 * Fixed size...
1977 */
1978
1979 mdb.width = x_dimension->values[0].integer;
1980 mdb.length = y_dimension->values[0].integer;
1981 }
1982 else if ((x_dimension = ippFindAttribute(media_size, "x-dimension",
1983 IPP_TAG_INTEGER)) != NULL &&
1984 (y_dimension = ippFindAttribute(media_size, "y-dimension",
1985 IPP_TAG_RANGE)) != NULL)
1986 {
1987 /*
1988 * Roll limits...
1989 */
1990
1991 mdb.width = x_dimension->values[0].integer;
1992 mdb.length = y_dimension->values[0].range.upper;
1993 }
1994 else if (flags != CUPS_MEDIA_FLAGS_READY &&
1995 (x_dimension = ippFindAttribute(media_size, "x-dimension",
1996 IPP_TAG_RANGE)) != NULL &&
1997 (y_dimension = ippFindAttribute(media_size, "y-dimension",
1998 IPP_TAG_RANGE)) != NULL)
1999 {
2000 /*
2001 * Custom size range; save this as the custom size value with default
2002 * margins, then continue; we'll capture the real margins below...
2003 */
2004
2005 custom = val;
2006
2007 dinfo->min_size.width = x_dimension->values[0].range.lower;
2008 dinfo->min_size.length = y_dimension->values[0].range.lower;
2009 dinfo->min_size.left =
2010 dinfo->min_size.right = 635; /* Default 1/4" side margins */
2011 dinfo->min_size.top =
2012 dinfo->min_size.bottom = 1270; /* Default 1/2" top/bottom margins */
2013
2014 dinfo->max_size.width = x_dimension->values[0].range.upper;
2015 dinfo->max_size.length = y_dimension->values[0].range.upper;
2016 dinfo->max_size.left =
2017 dinfo->max_size.right = 635; /* Default 1/4" side margins */
2018 dinfo->max_size.top =
2019 dinfo->max_size.bottom = 1270; /* Default 1/2" top/bottom margins */
2020 continue;
2021 }
2022 }
2023
2024 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))
2025 mdb.color = media_attr->values[0].string.text;
2026
2027 if ((media_attr = ippFindAttribute(val->collection, "media-info", IPP_TAG_TEXT)) != NULL)
2028 mdb.info = media_attr->values[0].string.text;
2029
2030 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))
2031 mdb.key = media_attr->values[0].string.text;
2032
2033 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))
2034 mdb.size_name = media_attr->values[0].string.text;
2035
2036 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))
2037 mdb.source = media_attr->values[0].string.text;
2038
2039 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))
2040 mdb.type = media_attr->values[0].string.text;
2041
2042 if ((media_attr = ippFindAttribute(val->collection, "media-bottom-margin", IPP_TAG_INTEGER)) != NULL)
2043 mdb.bottom = media_attr->values[0].integer;
2044
2045 if ((media_attr = ippFindAttribute(val->collection, "media-left-margin", IPP_TAG_INTEGER)) != NULL)
2046 mdb.left = media_attr->values[0].integer;
2047
2048 if ((media_attr = ippFindAttribute(val->collection, "media-right-margin", IPP_TAG_INTEGER)) != NULL)
2049 mdb.right = media_attr->values[0].integer;
2050
2051 if ((media_attr = ippFindAttribute(val->collection, "media-top-margin", IPP_TAG_INTEGER)) != NULL)
2052 mdb.top = media_attr->values[0].integer;
2053
2054 if (!mdb.key)
2055 {
2056 if (!mdb.size_name && (pwg = pwgMediaForSize(mdb.width, mdb.length)) != NULL)
2057 mdb.size_name = (char *)pwg->pwg;
2058
2059 if (!mdb.size_name)
2060 {
2061 /*
2062 * Use a CUPS-specific identifier if we don't have a size name...
2063 */
2064
2065 if (flags & CUPS_MEDIA_FLAGS_READY)
2066 snprintf(media_key, sizeof(media_key), "cups-media-ready-%d", i + 1);
2067 else
2068 snprintf(media_key, sizeof(media_key), "cups-media-%d", i + 1);
2069 }
2070 else if (mdb.source)
2071 {
2072 /*
2073 * Generate key using size name, source, and type (if set)...
2074 */
2075
2076 if (mdb.type)
2077 snprintf(media_key, sizeof(media_key), "%s_%s_%s", mdb.size_name, mdb.source, mdb.type);
2078 else
2079 snprintf(media_key, sizeof(media_key), "%s_%s", mdb.size_name, mdb.source);
2080 }
2081 else if (mdb.type)
2082 {
2083 /*
2084 * Generate key using size name and type...
2085 */
2086
2087 snprintf(media_key, sizeof(media_key), "%s_%s", mdb.size_name, mdb.type);
2088 }
2089 else
2090 {
2091 /*
2092 * Key is just the size name...
2093 */
2094
2095 strlcpy(media_key, mdb.size_name, sizeof(media_key));
2096 }
2097
2098 /*
2099 * Append "_borderless" for borderless media...
2100 */
2101
2102 if (!mdb.bottom && !mdb.left && !mdb.right && !mdb.top)
2103 strlcat(media_key, "_borderless", sizeof(media_key));
2104
2105 mdb.key = media_key;
2106 }
2107
2108 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));
2109
2110 cupsArrayAdd(db, &mdb);
2111 }
2112
2113 if (custom)
2114 {
2115 if ((media_attr = ippFindAttribute(custom->collection,
2116 "media-bottom-margin",
2117 IPP_TAG_INTEGER)) != NULL)
2118 {
2119 dinfo->min_size.top =
2120 dinfo->max_size.top = media_attr->values[0].integer;
2121 }
2122
2123 if ((media_attr = ippFindAttribute(custom->collection,
2124 "media-left-margin",
2125 IPP_TAG_INTEGER)) != NULL)
2126 {
2127 dinfo->min_size.left =
2128 dinfo->max_size.left = media_attr->values[0].integer;
2129 }
2130
2131 if ((media_attr = ippFindAttribute(custom->collection,
2132 "media-right-margin",
2133 IPP_TAG_INTEGER)) != NULL)
2134 {
2135 dinfo->min_size.right =
2136 dinfo->max_size.right = media_attr->values[0].integer;
2137 }
2138
2139 if ((media_attr = ippFindAttribute(custom->collection,
2140 "media-top-margin",
2141 IPP_TAG_INTEGER)) != NULL)
2142 {
2143 dinfo->min_size.top =
2144 dinfo->max_size.top = media_attr->values[0].integer;
2145 }
2146 }
2147 }
2148 else if (media_attr &&
2149 (media_attr->value_tag == IPP_TAG_NAME ||
2150 media_attr->value_tag == IPP_TAG_NAMELANG ||
2151 media_attr->value_tag == IPP_TAG_KEYWORD))
2152 {
2153 memset(&mdb, 0, sizeof(mdb));
2154
2155 mdb.left =
2156 mdb.right = 635; /* Default 1/4" side margins */
2157 mdb.top =
2158 mdb.bottom = 1270; /* Default 1/2" top/bottom margins */
2159
2160 for (i = media_attr->num_values, val = media_attr->values;
2161 i > 0;
2162 i --, val ++)
2163 {
2164 if ((pwg = pwgMediaForPWG(val->string.text)) == NULL)
2165 if ((pwg = pwgMediaForLegacy(val->string.text)) == NULL)
2166 {
2167 DEBUG_printf(("3cups_create_media_db: Ignoring unknown size '%s'.",
2168 val->string.text));
2169 continue;
2170 }
2171
2172 mdb.width = pwg->width;
2173 mdb.length = pwg->length;
2174
2175 if (flags != CUPS_MEDIA_FLAGS_READY &&
2176 !strncmp(val->string.text, "custom_min_", 11))
2177 {
2178 mdb.size_name = NULL;
2179 dinfo->min_size = mdb;
2180 }
2181 else if (flags != CUPS_MEDIA_FLAGS_READY &&
2182 !strncmp(val->string.text, "custom_max_", 11))
2183 {
2184 mdb.size_name = NULL;
2185 dinfo->max_size = mdb;
2186 }
2187 else
2188 {
2189 mdb.size_name = val->string.text;
2190
2191 cupsArrayAdd(db, &mdb);
2192 }
2193 }
2194 }
2195 }
2196
2197
2198 /*
2199 * 'cups_free_media_cb()' - Free a media entry.
2200 */
2201
2202 static void
2203 cups_free_media_db(
2204 _cups_media_db_t *mdb) /* I - Media entry to free */
2205 {
2206 if (mdb->color)
2207 _cupsStrFree(mdb->color);
2208 if (mdb->key)
2209 _cupsStrFree(mdb->key);
2210 if (mdb->info)
2211 _cupsStrFree(mdb->info);
2212 if (mdb->size_name)
2213 _cupsStrFree(mdb->size_name);
2214 if (mdb->source)
2215 _cupsStrFree(mdb->source);
2216 if (mdb->type)
2217 _cupsStrFree(mdb->type);
2218
2219 free(mdb);
2220 }
2221
2222
2223 /*
2224 * 'cups_get_media_db()' - Lookup the media entry for a given size.
2225 */
2226
2227 static int /* O - 1 on match, 0 on failure */
2228 cups_get_media_db(http_t *http, /* I - Connection to destination */
2229 cups_dinfo_t *dinfo, /* I - Destination information */
2230 pwg_media_t *pwg, /* I - PWG media info */
2231 unsigned flags, /* I - Media matching flags */
2232 cups_size_t *size) /* O - Media size/margin/name info */
2233 {
2234 cups_array_t *db; /* Which media database to query */
2235 _cups_media_db_t *mdb, /* Current media database entry */
2236 *best = NULL, /* Best matching entry */
2237 key; /* Search key */
2238
2239
2240 /*
2241 * Create the media database as needed...
2242 */
2243
2244 if (flags & CUPS_MEDIA_FLAGS_READY)
2245 {
2246 cups_update_ready(http, dinfo);
2247 db = dinfo->ready_db;
2248 }
2249 else
2250 {
2251 if (!dinfo->media_db)
2252 cups_create_media_db(dinfo, CUPS_MEDIA_FLAGS_DEFAULT);
2253
2254 db = dinfo->media_db;
2255 }
2256
2257 /*
2258 * Find a match...
2259 */
2260
2261 memset(&key, 0, sizeof(key));
2262 key.width = pwg->width;
2263 key.length = pwg->length;
2264
2265 if ((mdb = cupsArrayFind(db, &key)) != NULL)
2266 {
2267 /*
2268 * Found an exact match, let's figure out the best margins for the flags
2269 * supplied...
2270 */
2271
2272 best = mdb;
2273
2274 if (flags & CUPS_MEDIA_FLAGS_BORDERLESS)
2275 {
2276 /*
2277 * Look for the smallest margins...
2278 */
2279
2280 if (best->left != 0 || best->right != 0 || best->top != 0 || best->bottom != 0)
2281 {
2282 for (mdb = (_cups_media_db_t *)cupsArrayNext(db);
2283 mdb && !cups_compare_media_db(mdb, &key);
2284 mdb = (_cups_media_db_t *)cupsArrayNext(db))
2285 {
2286 if (mdb->left <= best->left && mdb->right <= best->right &&
2287 mdb->top <= best->top && mdb->bottom <= best->bottom)
2288 {
2289 best = mdb;
2290 if (mdb->left == 0 && mdb->right == 0 && mdb->bottom == 0 &&
2291 mdb->top == 0)
2292 break;
2293 }
2294 }
2295 }
2296
2297 /*
2298 * If we need an exact match, return no-match if the size is not
2299 * borderless.
2300 */
2301
2302 if ((flags & CUPS_MEDIA_FLAGS_EXACT) &&
2303 (best->left || best->right || best->top || best->bottom))
2304 return (0);
2305 }
2306 else if (flags & CUPS_MEDIA_FLAGS_DUPLEX)
2307 {
2308 /*
2309 * Look for the largest margins...
2310 */
2311
2312 for (mdb = (_cups_media_db_t *)cupsArrayNext(db);
2313 mdb && !cups_compare_media_db(mdb, &key);
2314 mdb = (_cups_media_db_t *)cupsArrayNext(db))
2315 {
2316 if (mdb->left >= best->left && mdb->right >= best->right &&
2317 mdb->top >= best->top && mdb->bottom >= best->bottom &&
2318 (mdb->bottom != best->bottom || mdb->left != best->left || mdb->right != best->right || mdb->top != best->top))
2319 best = mdb;
2320 }
2321 }
2322 else
2323 {
2324 /*
2325 * Look for the smallest non-zero margins...
2326 */
2327
2328 for (mdb = (_cups_media_db_t *)cupsArrayNext(db);
2329 mdb && !cups_compare_media_db(mdb, &key);
2330 mdb = (_cups_media_db_t *)cupsArrayNext(db))
2331 {
2332 if (((mdb->left > 0 && mdb->left <= best->left) || best->left == 0) &&
2333 ((mdb->right > 0 && mdb->right <= best->right) || best->right == 0) &&
2334 ((mdb->top > 0 && mdb->top <= best->top) || best->top == 0) &&
2335 ((mdb->bottom > 0 && mdb->bottom <= best->bottom) || best->bottom == 0) &&
2336 (mdb->bottom != best->bottom || mdb->left != best->left || mdb->right != best->right || mdb->top != best->top))
2337 best = mdb;
2338 }
2339 }
2340 }
2341 else if (flags & CUPS_MEDIA_FLAGS_EXACT)
2342 {
2343 /*
2344 * See if we can do this as a custom size...
2345 */
2346
2347 if (pwg->width < dinfo->min_size.width ||
2348 pwg->width > dinfo->max_size.width ||
2349 pwg->length < dinfo->min_size.length ||
2350 pwg->length > dinfo->max_size.length)
2351 return (0); /* Out of range */
2352
2353 if ((flags & CUPS_MEDIA_FLAGS_BORDERLESS) &&
2354 (dinfo->min_size.left > 0 || dinfo->min_size.right > 0 ||
2355 dinfo->min_size.top > 0 || dinfo->min_size.bottom > 0))
2356 return (0); /* Not borderless */
2357
2358 key.size_name = (char *)pwg->pwg;
2359 key.bottom = dinfo->min_size.bottom;
2360 key.left = dinfo->min_size.left;
2361 key.right = dinfo->min_size.right;
2362 key.top = dinfo->min_size.top;
2363
2364 best = &key;
2365 }
2366 else if (pwg->width >= dinfo->min_size.width &&
2367 pwg->width <= dinfo->max_size.width &&
2368 pwg->length >= dinfo->min_size.length &&
2369 pwg->length <= dinfo->max_size.length)
2370 {
2371 /*
2372 * Map to custom size...
2373 */
2374
2375 key.size_name = (char *)pwg->pwg;
2376 key.bottom = dinfo->min_size.bottom;
2377 key.left = dinfo->min_size.left;
2378 key.right = dinfo->min_size.right;
2379 key.top = dinfo->min_size.top;
2380
2381 best = &key;
2382 }
2383 else
2384 {
2385 /*
2386 * Find a close size...
2387 */
2388
2389 for (mdb = (_cups_media_db_t *)cupsArrayFirst(db);
2390 mdb;
2391 mdb = (_cups_media_db_t *)cupsArrayNext(db))
2392 if (cups_is_close_media_db(mdb, &key))
2393 break;
2394
2395 if (!mdb)
2396 return (0);
2397
2398 best = mdb;
2399
2400 if (flags & CUPS_MEDIA_FLAGS_BORDERLESS)
2401 {
2402 /*
2403 * Look for the smallest margins...
2404 */
2405
2406 if (best->left != 0 || best->right != 0 || best->top != 0 ||
2407 best->bottom != 0)
2408 {
2409 for (mdb = (_cups_media_db_t *)cupsArrayNext(db);
2410 mdb && cups_is_close_media_db(mdb, &key);
2411 mdb = (_cups_media_db_t *)cupsArrayNext(db))
2412 {
2413 if (mdb->left <= best->left && mdb->right <= best->right &&
2414 mdb->top <= best->top && mdb->bottom <= best->bottom &&
2415 (mdb->bottom != best->bottom || mdb->left != best->left || mdb->right != best->right || mdb->top != best->top))
2416 {
2417 best = mdb;
2418 if (mdb->left == 0 && mdb->right == 0 && mdb->bottom == 0 &&
2419 mdb->top == 0)
2420 break;
2421 }
2422 }
2423 }
2424 }
2425 else if (flags & CUPS_MEDIA_FLAGS_DUPLEX)
2426 {
2427 /*
2428 * Look for the largest margins...
2429 */
2430
2431 for (mdb = (_cups_media_db_t *)cupsArrayNext(db);
2432 mdb && cups_is_close_media_db(mdb, &key);
2433 mdb = (_cups_media_db_t *)cupsArrayNext(db))
2434 {
2435 if (mdb->left >= best->left && mdb->right >= best->right &&
2436 mdb->top >= best->top && mdb->bottom >= best->bottom &&
2437 (mdb->bottom != best->bottom || mdb->left != best->left || mdb->right != best->right || mdb->top != best->top))
2438 best = mdb;
2439 }
2440 }
2441 else
2442 {
2443 /*
2444 * Look for the smallest non-zero margins...
2445 */
2446
2447 for (mdb = (_cups_media_db_t *)cupsArrayNext(db);
2448 mdb && cups_is_close_media_db(mdb, &key);
2449 mdb = (_cups_media_db_t *)cupsArrayNext(db))
2450 {
2451 if (((mdb->left > 0 && mdb->left <= best->left) || best->left == 0) &&
2452 ((mdb->right > 0 && mdb->right <= best->right) ||
2453 best->right == 0) &&
2454 ((mdb->top > 0 && mdb->top <= best->top) || best->top == 0) &&
2455 ((mdb->bottom > 0 && mdb->bottom <= best->bottom) ||
2456 best->bottom == 0) &&
2457 (mdb->bottom != best->bottom || mdb->left != best->left || mdb->right != best->right || mdb->top != best->top))
2458 best = mdb;
2459 }
2460 }
2461 }
2462
2463 if (best)
2464 {
2465 /*
2466 * Return the matching size...
2467 */
2468
2469 if (best->key)
2470 strlcpy(size->media, best->key, sizeof(size->media));
2471 else if (best->size_name)
2472 strlcpy(size->media, best->size_name, sizeof(size->media));
2473 else
2474 strlcpy(size->media, pwg->pwg, sizeof(size->media));
2475
2476 size->width = best->width;
2477 size->length = best->length;
2478 size->bottom = best->bottom;
2479 size->left = best->left;
2480 size->right = best->right;
2481 size->top = best->top;
2482
2483 return (1);
2484 }
2485
2486 return (0);
2487 }
2488
2489
2490 /*
2491 * 'cups_is_close_media_db()' - Compare two media entries to see if they are
2492 * close to the same size.
2493 *
2494 * Currently we use 5 points (from PostScript) as the matching range...
2495 */
2496
2497 static int /* O - 1 if the sizes are close */
2498 cups_is_close_media_db(
2499 _cups_media_db_t *a, /* I - First media entries */
2500 _cups_media_db_t *b) /* I - Second media entries */
2501 {
2502 int dwidth, /* Difference in width */
2503 dlength; /* Difference in length */
2504
2505
2506 dwidth = a->width - b->width;
2507 dlength = a->length - b->length;
2508
2509 return (dwidth >= -176 && dwidth <= 176 &&
2510 dlength >= -176 && dlength <= 176);
2511 }
2512
2513
2514 /*
2515 * 'cups_test_constraints()' - Test constraints.
2516 */
2517
2518 static cups_array_t * /* O - Active constraints */
2519 cups_test_constraints(
2520 cups_dinfo_t *dinfo, /* I - Destination information */
2521 const char *new_option, /* I - Newly selected option */
2522 const char *new_value, /* I - Newly selected value */
2523 int num_options, /* I - Number of options */
2524 cups_option_t *options, /* I - Options */
2525 int *num_conflicts, /* O - Number of conflicting options */
2526 cups_option_t **conflicts) /* O - Conflicting options */
2527 {
2528 int i, /* Looping var */
2529 count, /* Number of values */
2530 match; /* Value matches? */
2531 int num_matching; /* Number of matching options */
2532 cups_option_t *matching; /* Matching options */
2533 _cups_dconstres_t *c; /* Current constraint */
2534 cups_array_t *active = NULL; /* Active constraints */
2535 ipp_t *col; /* Collection value */
2536 ipp_attribute_t *attr; /* Current attribute */
2537 _ipp_value_t *attrval; /* Current attribute value */
2538 const char *value; /* Current value */
2539 char temp[1024]; /* Temporary string */
2540 int int_value; /* Integer value */
2541 int xres_value, /* Horizontal resolution */
2542 yres_value; /* Vertical resolution */
2543 ipp_res_t units_value; /* Resolution units */
2544
2545
2546 for (c = (_cups_dconstres_t *)cupsArrayFirst(dinfo->constraints);
2547 c;
2548 c = (_cups_dconstres_t *)cupsArrayNext(dinfo->constraints))
2549 {
2550 num_matching = 0;
2551 matching = NULL;
2552
2553 for (attr = ippFirstAttribute(c->collection);
2554 attr;
2555 attr = ippNextAttribute(c->collection))
2556 {
2557 /*
2558 * Get the value for the current attribute in the constraint...
2559 */
2560
2561 if (new_option && new_value && !strcmp(attr->name, new_option))
2562 value = new_value;
2563 else if ((value = cupsGetOption(attr->name, num_options, options)) == NULL)
2564 value = cupsGetOption(attr->name, dinfo->num_defaults, dinfo->defaults);
2565
2566 if (!value)
2567 {
2568 /*
2569 * Not set so this constraint does not apply...
2570 */
2571
2572 break;
2573 }
2574
2575 match = 0;
2576
2577 switch (attr->value_tag)
2578 {
2579 case IPP_TAG_INTEGER :
2580 case IPP_TAG_ENUM :
2581 int_value = atoi(value);
2582
2583 for (i = attr->num_values, attrval = attr->values;
2584 i > 0;
2585 i --, attrval ++)
2586 {
2587 if (attrval->integer == int_value)
2588 {
2589 match = 1;
2590 break;
2591 }
2592 }
2593 break;
2594
2595 case IPP_TAG_BOOLEAN :
2596 int_value = !strcmp(value, "true");
2597
2598 for (i = attr->num_values, attrval = attr->values;
2599 i > 0;
2600 i --, attrval ++)
2601 {
2602 if (attrval->boolean == int_value)
2603 {
2604 match = 1;
2605 break;
2606 }
2607 }
2608 break;
2609
2610 case IPP_TAG_RANGE :
2611 int_value = atoi(value);
2612
2613 for (i = attr->num_values, attrval = attr->values;
2614 i > 0;
2615 i --, attrval ++)
2616 {
2617 if (int_value >= attrval->range.lower &&
2618 int_value <= attrval->range.upper)
2619 {
2620 match = 1;
2621 break;
2622 }
2623 }
2624 break;
2625
2626 case IPP_TAG_RESOLUTION :
2627 if (sscanf(value, "%dx%d%15s", &xres_value, &yres_value, temp) != 3)
2628 {
2629 if (sscanf(value, "%d%15s", &xres_value, temp) != 2)
2630 break;
2631
2632 yres_value = xres_value;
2633 }
2634
2635 if (!strcmp(temp, "dpi"))
2636 units_value = IPP_RES_PER_INCH;
2637 else if (!strcmp(temp, "dpc") || !strcmp(temp, "dpcm"))
2638 units_value = IPP_RES_PER_CM;
2639 else
2640 break;
2641
2642 for (i = attr->num_values, attrval = attr->values;
2643 i > 0;
2644 i --, attrval ++)
2645 {
2646 if (attrval->resolution.xres == xres_value &&
2647 attrval->resolution.yres == yres_value &&
2648 attrval->resolution.units == units_value)
2649 {
2650 match = 1;
2651 break;
2652 }
2653 }
2654 break;
2655
2656 case IPP_TAG_TEXT :
2657 case IPP_TAG_NAME :
2658 case IPP_TAG_KEYWORD :
2659 case IPP_TAG_CHARSET :
2660 case IPP_TAG_URI :
2661 case IPP_TAG_URISCHEME :
2662 case IPP_TAG_MIMETYPE :
2663 case IPP_TAG_LANGUAGE :
2664 case IPP_TAG_TEXTLANG :
2665 case IPP_TAG_NAMELANG :
2666 for (i = attr->num_values, attrval = attr->values;
2667 i > 0;
2668 i --, attrval ++)
2669 {
2670 if (!strcmp(attrval->string.text, value))
2671 {
2672 match = 1;
2673 break;
2674 }
2675 }
2676 break;
2677
2678 case IPP_TAG_BEGIN_COLLECTION :
2679 col = ippNew();
2680 _cupsEncodeOption(col, IPP_TAG_ZERO, NULL, ippGetName(attr), value);
2681
2682 for (i = 0, count = ippGetCount(attr); i < count; i ++)
2683 {
2684 if (cups_collection_contains(col, ippGetCollection(attr, i)))
2685 {
2686 match = 1;
2687 break;
2688 }
2689 }
2690
2691 ippDelete(col);
2692 break;
2693
2694 default :
2695 break;
2696 }
2697
2698 if (!match)
2699 break;
2700
2701 num_matching = cupsAddOption(attr->name, value, num_matching, &matching);
2702 }
2703
2704 if (!attr)
2705 {
2706 if (!active)
2707 active = cupsArrayNew(NULL, NULL);
2708
2709 cupsArrayAdd(active, c);
2710
2711 if (num_conflicts && conflicts)
2712 {
2713 cups_option_t *moption; /* Matching option */
2714
2715 for (i = num_matching, moption = matching; i > 0; i --, moption ++)
2716 *num_conflicts = cupsAddOption(moption->name, moption->value, *num_conflicts, conflicts);
2717 }
2718 }
2719
2720 cupsFreeOptions(num_matching, matching);
2721 }
2722
2723 return (active);
2724 }
2725
2726
2727 /*
2728 * 'cups_update_ready()' - Update xxx-ready attributes for the printer.
2729 */
2730
2731 static void
2732 cups_update_ready(http_t *http, /* I - Connection to destination */
2733 cups_dinfo_t *dinfo) /* I - Destination information */
2734 {
2735 ipp_t *request; /* Get-Printer-Attributes request */
2736 static const char * const pattrs[] = /* Printer attributes we want */
2737 {
2738 "finishings-col-ready",
2739 "finishings-ready",
2740 "job-finishings-col-ready",
2741 "job-finishings-ready",
2742 "media-col-ready",
2743 "media-ready"
2744 };
2745
2746
2747 /*
2748 * Don't update more than once every 30 seconds...
2749 */
2750
2751 if ((time(NULL) - dinfo->ready_time) < _CUPS_MEDIA_READY_TTL)
2752 return;
2753
2754 /*
2755 * Free any previous results...
2756 */
2757
2758 if (dinfo->cached_flags & CUPS_MEDIA_FLAGS_READY)
2759 {
2760 cupsArrayDelete(dinfo->cached_db);
2761 dinfo->cached_db = NULL;
2762 dinfo->cached_flags = CUPS_MEDIA_FLAGS_DEFAULT;
2763 }
2764
2765 ippDelete(dinfo->ready_attrs);
2766 dinfo->ready_attrs = NULL;
2767
2768 cupsArrayDelete(dinfo->ready_db);
2769 dinfo->ready_db = NULL;
2770
2771 /*
2772 * Query the xxx-ready values...
2773 */
2774
2775 request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
2776 ippSetVersion(request, dinfo->version / 10, dinfo->version % 10);
2777
2778 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
2779 dinfo->uri);
2780 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
2781 NULL, cupsUser());
2782 ippAddStrings(request, IPP_TAG_OPERATION, IPP_CONST_TAG(IPP_TAG_KEYWORD), "requested-attributes", (int)(sizeof(pattrs) / sizeof(pattrs[0])), NULL, pattrs);
2783
2784 dinfo->ready_attrs = cupsDoRequest(http, request, dinfo->resource);
2785
2786 /*
2787 * Update the ready media database...
2788 */
2789
2790 cups_create_media_db(dinfo, CUPS_MEDIA_FLAGS_READY);
2791
2792 /*
2793 * Update last lookup time and return...
2794 */
2795
2796 dinfo->ready_time = time(NULL);
2797 }