]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/dest-options.c
Merge changes from CUPS 1.7svn-r10578.
[thirdparty/cups.git] / cups / dest-options.c
1 /*
2 * "$Id$"
3 *
4 * Destination option/media support for CUPS.
5 *
6 * Copyright 2012 by Apple Inc.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
13 *
14 * This file is subject to the Apple OS-Developed Software exception.
15 *
16 * Contents:
17 *
18 * cupsCheckDestSupported() - Check that the option and value are supported
19 * by the destination.
20 * cupsCopyDestConflicts() - Get conflicts and resolutions for a new
21 * option/value pair.
22 * cupsCopyDestInfo() - Get the supported values/capabilities for the
23 * destination.
24 * cupsFreeDestInfo() - Free destination information obtained using
25 * @link cupsCopyDestInfo@.
26 * cupsGetDestMediaByName() - Get media names, dimensions, and margins.
27 * cupsGetDestMediaBySize() - Get media names, dimensions, and margins.
28 * cups_add_dconstres() - Add a constraint or resolver to an array.
29 * cups_compare_dconstres() - Compare to resolver entries.
30 * cups_compare_media_db() - Compare two media entries.
31 * cups_copy_media_db() - Copy a media entry.
32 * cups_create_constraints() - Create the constraints and resolvers arrays.
33 * cups_create_defaults() - Create the -default option array.
34 * cups_create_media_db() - Create the media database.
35 * cups_free_media_cb() - Free a media entry.
36 * cups_get_media_db() - Lookup the media entry for a given size.
37 * cups_is_close_media_db() - Compare two media entries to see if they are
38 * close to the same size.
39 * cups_test_constraints() - Test constraints.
40 */
41
42 /*
43 * Include necessary headers...
44 */
45
46 #include "cups-private.h"
47
48
49 /*
50 * Local functions...
51 */
52
53 static void cups_add_dconstres(cups_array_t *a, ipp_t *collection);
54 static int cups_compare_dconstres(_cups_dconstres_t *a,
55 _cups_dconstres_t *b);
56 static int cups_compare_media_db(_cups_media_db_t *a,
57 _cups_media_db_t *b);
58 static _cups_media_db_t *cups_copy_media_db(_cups_media_db_t *mdb);
59 static void cups_create_constraints(cups_dinfo_t *dinfo);
60 static void cups_create_defaults(cups_dinfo_t *dinfo);
61 static void cups_create_media_db(cups_dinfo_t *dinfo);
62 static void cups_free_media_db(_cups_media_db_t *mdb);
63 static int cups_get_media_db(cups_dinfo_t *dinfo,
64 _pwg_media_t *pwg, unsigned flags,
65 cups_size_t *size);
66 static int cups_is_close_media_db(_cups_media_db_t *a,
67 _cups_media_db_t *b);
68 static cups_array_t *cups_test_constraints(cups_dinfo_t *dinfo,
69 const char *new_option,
70 const char *new_value,
71 int num_options,
72 cups_option_t *options,
73 int *num_conflicts,
74 cups_option_t **conflicts);
75
76
77 /*
78 * 'cupsCheckDestSupported()' - Check that the option and value are supported
79 * by the destination.
80 *
81 * Returns 1 if supported, 0 otherwise.
82 *
83 * @since CUPS 1.6/OS X 10.8@
84 */
85
86 int /* O - 1 if supported, 0 otherwise */
87 cupsCheckDestSupported(
88 http_t *http, /* I - Connection to destination */
89 cups_dest_t *dest, /* I - Destination */
90 cups_dinfo_t *dinfo, /* I - Destination information */
91 const char *option, /* I - Option */
92 const char *value) /* I - Value */
93 {
94 int i; /* Looping var */
95 char temp[1024]; /* Temporary string */
96 int int_value; /* Integer value */
97 int xres_value, /* Horizontal resolution */
98 yres_value; /* Vertical resolution */
99 ipp_res_t units_value; /* Resolution units */
100 ipp_attribute_t *attr; /* Attribute */
101 _ipp_value_t *attrval; /* Current attribute value */
102
103
104 /*
105 * Range check input...
106 */
107
108 if (!http || !dest || !dinfo || !option || !value)
109 return (0);
110
111 /*
112 * Lookup the attribute...
113 */
114
115 if (strstr(option, "-supported"))
116 attr = ippFindAttribute(dinfo->attrs, option, IPP_TAG_ZERO);
117 else
118 {
119 snprintf(temp, sizeof(temp), "%s-supported", option);
120 attr = ippFindAttribute(dinfo->attrs, temp, IPP_TAG_ZERO);
121 }
122
123 if (!attr)
124 return (0);
125
126 /*
127 * Compare values...
128 */
129
130 if (!strcmp(option, "media") && !strncmp(value, "custom_", 7))
131 {
132 /*
133 * Check range of custom media sizes...
134 */
135
136 _pwg_media_t *pwg; /* Current PWG media size info */
137 int min_width, /* Minimum width */
138 min_length, /* Minimum length */
139 max_width, /* Maximum width */
140 max_length; /* Maximum length */
141
142 /*
143 * Get the minimum and maximum size...
144 */
145
146 min_width = min_length = INT_MAX;
147 max_width = max_length = 0;
148
149 for (i = attr->num_values, attrval = attr->values;
150 i > 0;
151 i --, attrval ++)
152 {
153 if (!strncmp(attrval->string.text, "custom_min_", 11) &&
154 (pwg = _pwgMediaForPWG(attrval->string.text)) != NULL)
155 {
156 min_width = pwg->width;
157 min_length = pwg->length;
158 }
159 else if (!strncmp(attrval->string.text, "custom_max_", 11) &&
160 (pwg = _pwgMediaForPWG(attrval->string.text)) != NULL)
161 {
162 max_width = pwg->width;
163 max_length = pwg->length;
164 }
165 }
166
167 /*
168 * Check the range...
169 */
170
171 if (min_width < INT_MAX && max_width > 0 &&
172 (pwg = _pwgMediaForPWG(value)) != NULL &&
173 pwg->width >= min_width && pwg->width <= max_width &&
174 pwg->length >= min_length && pwg->length <= max_length)
175 return (1);
176 }
177 else
178 {
179 /*
180 * Check literal values...
181 */
182
183 switch (attr->value_tag)
184 {
185 case IPP_TAG_INTEGER :
186 case IPP_TAG_ENUM :
187 int_value = atoi(value);
188
189 for (i = 0; i < attr->num_values; i ++)
190 if (attr->values[i].integer == int_value)
191 return (1);
192 break;
193
194 case IPP_TAG_BOOLEAN :
195 return (attr->values[0].boolean);
196
197 case IPP_TAG_RANGE :
198 int_value = atoi(value);
199
200 for (i = 0; i < attr->num_values; i ++)
201 if (int_value >= attr->values[i].range.lower &&
202 int_value <= attr->values[i].range.upper)
203 return (1);
204 break;
205
206 case IPP_TAG_RESOLUTION :
207 if (sscanf(value, "%dx%d%15s", &xres_value, &yres_value, temp) != 3)
208 {
209 if (sscanf(value, "%d%15s", &xres_value, temp) != 2)
210 return (0);
211
212 yres_value = xres_value;
213 }
214
215 if (!strcmp(temp, "dpi"))
216 units_value = IPP_RES_PER_INCH;
217 else if (!strcmp(temp, "dpc") || !strcmp(temp, "dpcm"))
218 units_value = IPP_RES_PER_CM;
219 else
220 return (0);
221
222 for (i = attr->num_values, attrval = attr->values;
223 i > 0;
224 i --, attrval ++)
225 {
226 if (attrval->resolution.xres == xres_value &&
227 attrval->resolution.yres == yres_value &&
228 attrval->resolution.units == units_value)
229 return (1);
230 }
231 break;
232
233 case IPP_TAG_TEXT :
234 case IPP_TAG_NAME :
235 case IPP_TAG_KEYWORD :
236 case IPP_TAG_CHARSET :
237 case IPP_TAG_URI :
238 case IPP_TAG_URISCHEME :
239 case IPP_TAG_MIMETYPE :
240 case IPP_TAG_LANGUAGE :
241 case IPP_TAG_TEXTLANG :
242 case IPP_TAG_NAMELANG :
243 for (i = 0; i < attr->num_values; i ++)
244 if (!strcmp(attr->values[i].string.text, value))
245 return (1);
246 break;
247
248 default :
249 break;
250 }
251 }
252
253 /*
254 * If we get there the option+value is not supported...
255 */
256
257 return (0);
258 }
259
260
261 /*
262 * 'cupsCopyDestConflicts()' - Get conflicts and resolutions for a new
263 * option/value pair.
264 *
265 * "num_options" and "options" represent the currently selected options by the
266 * user. "new_option" and "new_value" are the setting the user has just
267 * changed.
268 *
269 * Returns 1 if there is a conflict, 0 if there are no conflicts, and -1 if
270 * there was an unrecoverable error such as a resolver loop.
271 *
272 * If "num_conflicts" and "conflicts" are not NULL, they are set to contain the
273 * list of conflicting option/value pairs. Similarly, if "num_resolved" and
274 * "resolved" are not NULL they will be set to the list of changes needed to
275 * resolve the conflict.
276 *
277 * If cupsCopyDestConflicts returns 1 but "num_resolved" and "resolved" are set
278 * to 0 and NULL, respectively, then the conflict cannot be resolved.
279 *
280 * @since CUPS 1.6/OS X 10.8@
281 */
282
283 int /* O - 1 if there is a conflict, 0 if none, -1 on error */
284 cupsCopyDestConflicts(
285 http_t *http, /* I - Connection to destination */
286 cups_dest_t *dest, /* I - Destination */
287 cups_dinfo_t *dinfo, /* I - Destination information */
288 int num_options, /* I - Number of current options */
289 cups_option_t *options, /* I - Current options */
290 const char *new_option, /* I - New option */
291 const char *new_value, /* I - New value */
292 int *num_conflicts, /* O - Number of conflicting options */
293 cups_option_t **conflicts, /* O - Conflicting options */
294 int *num_resolved, /* O - Number of options to resolve */
295 cups_option_t **resolved) /* O - Resolved options */
296 {
297 int i, /* Looping var */
298 have_conflicts = 0, /* Do we have conflicts? */
299 changed, /* Did we change something? */
300 tries, /* Number of tries for resolution */
301 num_myconf = 0, /* My number of conflicting options */
302 num_myres = 0; /* My number of resolved options */
303 cups_option_t *myconf = NULL, /* My conflicting options */
304 *myres = NULL, /* My resolved options */
305 *myoption, /* My current option */
306 *option; /* Current option */
307 cups_array_t *active, /* Active conflicts */
308 *pass = NULL, /* Resolvers for this pass */
309 *resolvers = NULL, /* Resolvers we have used */
310 *test; /* Test array for conflicts */
311 _cups_dconstres_t *c, /* Current constraint */
312 *r; /* Current resolver */
313 ipp_attribute_t *attr; /* Current attribute */
314 char value[2048]; /* Current attribute value as string */
315 const char *myvalue; /* Current value of an option */
316
317
318 /*
319 * Clear returned values...
320 */
321
322 if (num_conflicts)
323 *num_conflicts = 0;
324
325 if (conflicts)
326 *conflicts = NULL;
327
328 if (num_resolved)
329 *num_resolved = 0;
330
331 if (resolved)
332 *resolved = NULL;
333
334 /*
335 * Range check input...
336 */
337
338 if (!http || !dest || !dinfo ||
339 (num_conflicts != NULL) != (conflicts != NULL) ||
340 (num_resolved != NULL) != (resolved != NULL))
341 return (0);
342
343 /*
344 * Load constraints as needed...
345 */
346
347 if (!dinfo->constraints)
348 cups_create_constraints(dinfo);
349
350 if (cupsArrayCount(dinfo->constraints) == 0)
351 return (0);
352
353 if (!dinfo->num_defaults)
354 cups_create_defaults(dinfo);
355
356 /*
357 * If we are resolving, create a shadow array...
358 */
359
360 if (num_resolved)
361 {
362 for (i = num_options, option = options; i > 0; i --, option ++)
363 num_myres = cupsAddOption(option->name, option->value, num_myres, &myres);
364
365 if (new_option && new_value)
366 num_myres = cupsAddOption(new_option, new_value, num_myres, &myres);
367 }
368 else
369 {
370 num_myres = num_options;
371 myres = options;
372 }
373
374 /*
375 * Check for any conflicts...
376 */
377
378 if (num_resolved)
379 pass = cupsArrayNew((cups_array_func_t)cups_compare_dconstres, NULL);
380
381 for (tries = 0; tries < 100; tries ++)
382 {
383 /*
384 * Check for any conflicts...
385 */
386
387 if (num_conflicts || num_resolved)
388 {
389 cupsFreeOptions(num_myconf, myconf);
390
391 num_myconf = 0;
392 myconf = NULL;
393 active = cups_test_constraints(dinfo, new_option, new_value,
394 num_myres, myres, &num_myconf,
395 &myconf);
396 }
397 else
398 active = cups_test_constraints(dinfo, new_option, new_value, num_myres,
399 myres, NULL, NULL);
400
401 have_conflicts = (active != NULL);
402
403 if (!active || !num_resolved)
404 break; /* All done */
405
406 /*
407 * Scan the constraints that were triggered to apply resolvers...
408 */
409
410 if (!resolvers)
411 resolvers = cupsArrayNew((cups_array_func_t)cups_compare_dconstres, NULL);
412
413 for (c = (_cups_dconstres_t *)cupsArrayFirst(active), changed = 0;
414 c;
415 c = (_cups_dconstres_t *)cupsArrayNext(active))
416 {
417 if (cupsArrayFind(pass, c))
418 continue; /* Already applied this resolver... */
419
420 if (cupsArrayFind(resolvers, c))
421 {
422 DEBUG_printf(("1cupsCopyDestConflicts: Resolver loop with %s.",
423 c->name));
424 have_conflicts = -1;
425 goto cleanup;
426 }
427
428 if ((r = cupsArrayFind(dinfo->resolvers, c)) == NULL)
429 {
430 DEBUG_printf(("1cupsCopyDestConflicts: Resolver %s not found.",
431 c->name));
432 have_conflicts = -1;
433 goto cleanup;
434 }
435
436 /*
437 * Add the options from the resolver...
438 */
439
440 cupsArrayAdd(pass, r);
441 cupsArrayAdd(resolvers, r);
442
443 for (attr = ippFirstAttribute(r->collection);
444 attr;
445 attr = ippNextAttribute(r->collection))
446 {
447 if (new_option && !strcmp(attr->name, new_option))
448 continue; /* Ignore this if we just changed it */
449
450 if (ippAttributeString(attr, value, sizeof(value)) >= sizeof(value))
451 continue; /* Ignore if the value is too long */
452
453 if ((test = cups_test_constraints(dinfo, attr->name, value, num_myres,
454 myres, NULL, NULL)) == NULL)
455 {
456 /*
457 * That worked, flag it...
458 */
459
460 changed = 1;
461 }
462 else
463 cupsArrayDelete(test);
464
465 /*
466 * Add the option/value from the resolver regardless of whether it
467 * worked; this makes sure that we can cascade several changes to
468 * make things resolve...
469 */
470
471 num_myres = cupsAddOption(attr->name, value, num_myres, &myres);
472 }
473 }
474
475 if (!changed)
476 {
477 DEBUG_puts("1cupsCopyDestConflicts: Unable to resolve constraints.");
478 have_conflicts = -1;
479 goto cleanup;
480 }
481
482 cupsArrayClear(pass);
483
484 cupsArrayDelete(active);
485 active = NULL;
486 }
487
488 if (tries >= 0)
489 {
490 DEBUG_puts("1cupsCopyDestConflicts: Unable to resolve after 100 tries.");
491 have_conflicts = -1;
492 goto cleanup;
493 }
494
495 /*
496 * Copy resolved options as needed...
497 */
498
499 if (num_resolved)
500 {
501 for (i = num_myres, myoption = myres; i > 0; i --, myoption ++)
502 {
503 if ((myvalue = cupsGetOption(myoption->name, num_options,
504 options)) == NULL ||
505 strcmp(myvalue, myoption->value))
506 {
507 if (new_option && !strcmp(new_option, myoption->name) &&
508 new_value && !strcmp(new_value, myoption->value))
509 continue;
510
511 *num_resolved = cupsAddOption(myoption->name, myoption->value,
512 *num_resolved, resolved);
513 }
514 }
515 }
516
517 /*
518 * Clean up...
519 */
520
521 cleanup:
522
523 cupsArrayDelete(active);
524 cupsArrayDelete(pass);
525 cupsArrayDelete(resolvers);
526
527 if (num_resolved)
528 {
529 /*
530 * Free shadow copy of options...
531 */
532
533 cupsFreeOptions(num_myres, myres);
534 }
535
536 if (num_conflicts)
537 {
538 /*
539 * Return conflicting options to caller...
540 */
541
542 *num_conflicts = num_myconf;
543 *conflicts = myconf;
544 }
545 else
546 {
547 /*
548 * Free conflicting options...
549 */
550
551 cupsFreeOptions(num_myconf, myconf);
552 }
553
554 return (have_conflicts);
555 }
556
557
558 /*
559 * 'cupsCopyDestInfo()' - Get the supported values/capabilities for the
560 * destination.
561 *
562 * The caller is responsible for calling @link cupsFreeDestInfo@ on the return
563 * value. @code NULL@ is returned on error.
564 *
565 * @since CUPS 1.6/OS X 10.8@
566 */
567
568 cups_dinfo_t * /* O - Destination information */
569 cupsCopyDestInfo(
570 http_t *http, /* I - Connection to destination */
571 cups_dest_t *dest) /* I - Destination */
572 {
573 cups_dinfo_t *dinfo; /* Destination information */
574 ipp_t *request, /* Get-Printer-Attributes request */
575 *response; /* Supported attributes */
576 int tries, /* Number of tries so far */
577 delay, /* Current retry delay */
578 prev_delay; /* Next retry delay */
579 const char *uri; /* Printer URI */
580 char resource[1024]; /* Resource path */
581 int version; /* IPP version */
582 ipp_status_t status; /* Status of request */
583 static const char * const requested_attrs[] =
584 { /* Requested attributes */
585 "job-template",
586 "media-col-database",
587 "printer-description"
588 };
589
590
591 DEBUG_printf(("cupsCopyDestSupported(http=%p, dest=%p(%s))", http, dest,
592 dest ? dest->name : ""));
593
594 /*
595 * Range check input...
596 */
597
598 if (!http || !dest)
599 return (NULL);
600
601 /*
602 * Get the printer URI and resource path...
603 */
604
605 if ((uri = _cupsGetDestResource(dest, resource, sizeof(resource))) == NULL)
606 return (NULL);
607
608 /*
609 * Get the supported attributes...
610 */
611
612 delay = 1;
613 prev_delay = 1;
614 tries = 0;
615 version = 20;
616
617 do
618 {
619 /*
620 * Send a Get-Printer-Attributes request...
621 */
622
623 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
624 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
625 uri);
626 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
627 NULL, cupsUser());
628 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
629 "requested-attributes",
630 (int)(sizeof(requested_attrs) / sizeof(requested_attrs[0])),
631 NULL, requested_attrs);
632 response = cupsDoRequest(http, request, resource);
633 status = cupsLastError();
634
635 if (status > IPP_OK_SUBST)
636 {
637 DEBUG_printf(("cupsCopyDestSupported: Get-Printer-Attributes for '%s' "
638 "returned %s (%s)", dest->name, ippErrorString(status),
639 cupsLastErrorString()));
640
641 ippDelete(response);
642 response = NULL;
643
644 if (status == IPP_VERSION_NOT_SUPPORTED && version > 11)
645 version = 11;
646 else if (status == IPP_PRINTER_BUSY)
647 {
648 sleep(delay);
649
650 delay = _cupsNextDelay(delay, &prev_delay);
651 }
652 else
653 return (NULL);
654 }
655
656 tries ++;
657 }
658 while (!response && tries < 10);
659
660 if (!response)
661 return (NULL);
662
663 /*
664 * Allocate a cups_dinfo_t structure and return it...
665 */
666
667 if ((dinfo = calloc(1, sizeof(cups_dinfo_t))) == NULL)
668 {
669 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
670 ippDelete(response);
671 return (NULL);
672 }
673
674 dinfo->uri = uri;
675 dinfo->resource = _cupsStrAlloc(resource);
676 dinfo->attrs = response;
677
678 return (dinfo);
679 }
680
681
682 /*
683 * 'cupsFreeDestInfo()' - Free destination information obtained using
684 * @link cupsCopyDestInfo@.
685 */
686
687 void
688 cupsFreeDestInfo(cups_dinfo_t *dinfo) /* I - Destination information */
689 {
690 /*
691 * Range check input...
692 */
693
694 if (!dinfo)
695 return;
696
697 /*
698 * Free memory and return...
699 */
700
701 _cupsStrFree(dinfo->resource);
702
703 cupsArrayDelete(dinfo->constraints);
704 cupsArrayDelete(dinfo->resolvers);
705
706 cupsArrayDelete(dinfo->localizations);
707
708 cupsArrayDelete(dinfo->media_db);
709
710 ippDelete(dinfo->attrs);
711
712 free(dinfo);
713 }
714
715
716 /*
717 * 'cupsGetDestMediaByName()' - Get media names, dimensions, and margins.
718 *
719 * The "media" string is a PWG media name, while "width" and "length" are the
720 * dimensions in hundredths of millimeters. "flags" provides some matching
721 * guidance (multiple flags can be combined):
722 *
723 * CUPS_MEDIA_FLAGS_DEFAULT = find the closest size supported by the printer
724 * CUPS_MEDIA_FLAGS_BORDERLESS = find a borderless size
725 * CUPS_MEDIA_FLAGS_DUPLEX = find a size compatible with 2-sided printing
726 * CUPS_MEDIA_FLAGS_EXACT = find an exact match for the size
727 * CUPS_MEDIA_FLAGS_READY = if the printer supports media sensing, find the
728 * size amongst the "ready" media.
729 *
730 * The matching result (if any) is returned in the "cups_size_t" structure.
731 *
732 * Returns 1 when there is a match and 0 if there is not a match.
733 *
734 * @since CUPS 1.6/OS X 10.8@
735 */
736
737 int /* O - 1 on match, 0 on failure */
738 cupsGetDestMediaByName(
739 http_t *http, /* I - Connection to destination */
740 cups_dest_t *dest, /* I - Destination */
741 cups_dinfo_t *dinfo, /* I - Destination information */
742 const char *media, /* I - Media name */
743 unsigned flags, /* I - Media matching flags */
744 cups_size_t *size) /* O - Media size information */
745 {
746 _pwg_media_t *pwg; /* PWG media info */
747
748
749 /*
750 * Range check input...
751 */
752
753 if (size)
754 memset(size, 0, sizeof(cups_size_t));
755
756 if (!http || !dest || !dinfo || !media || !size)
757 {
758 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
759 return (0);
760 }
761
762 /*
763 * Lookup the media size name...
764 */
765
766 if ((pwg = _pwgMediaForPWG(media)) == NULL)
767 if ((pwg = _pwgMediaForLegacy(media)) == NULL)
768 {
769 DEBUG_printf(("1cupsGetDestMediaByName: Unknown size '%s'.", media));
770 _cupsSetError(IPP_INTERNAL_ERROR, _("Unknown media size name."), 1);
771 return (0);
772 }
773
774 /*
775 * Lookup the size...
776 */
777
778 return (cups_get_media_db(dinfo, pwg, flags, size));
779 }
780
781
782 /*
783 * 'cupsGetDestMediaBySize()' - Get media names, dimensions, and margins.
784 *
785 * The "media" string is a PWG media name, while "width" and "length" are the
786 * dimensions in hundredths of millimeters. "flags" provides some matching
787 * guidance (multiple flags can be combined):
788 *
789 * CUPS_MEDIA_FLAGS_DEFAULT = find the closest size supported by the printer
790 * CUPS_MEDIA_FLAGS_BORDERLESS = find a borderless size
791 * CUPS_MEDIA_FLAGS_DUPLEX = find a size compatible with 2-sided printing
792 * CUPS_MEDIA_FLAGS_EXACT = find an exact match for the size
793 * CUPS_MEDIA_FLAGS_READY = if the printer supports media sensing, find the
794 * size amongst the "ready" media.
795 *
796 * The matching result (if any) is returned in the "cups_size_t" structure.
797 *
798 * Returns 1 when there is a match and 0 if there is not a match.
799 *
800 * @since CUPS 1.6/OS X 10.8@
801 */
802
803 int /* O - 1 on match, 0 on failure */
804 cupsGetDestMediaBySize(
805 http_t *http, /* I - Connection to destination */
806 cups_dest_t *dest, /* I - Destination */
807 cups_dinfo_t *dinfo, /* I - Destination information */
808 int width, /* I - Media width in hundredths of
809 * of millimeters */
810 int length, /* I - Media length in hundredths of
811 * of millimeters */
812 unsigned flags, /* I - Media matching flags */
813 cups_size_t *size) /* O - Media size information */
814 {
815 _pwg_media_t *pwg; /* PWG media info */
816
817
818 /*
819 * Range check input...
820 */
821
822 if (size)
823 memset(size, 0, sizeof(cups_size_t));
824
825 if (!http || !dest || !dinfo || width <= 0 || length <= 0 || !size)
826 {
827 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
828 return (0);
829 }
830
831 /*
832 * Lookup the media size name...
833 */
834
835 if ((pwg = _pwgMediaForSize(width, length)) == NULL)
836 {
837 DEBUG_printf(("1cupsGetDestMediaBySize: Invalid size %dx%d.", width,
838 length));
839 _cupsSetError(IPP_INTERNAL_ERROR, _("Invalid media size."), 1);
840 return (0);
841 }
842
843 /*
844 * Lookup the size...
845 */
846
847 return (cups_get_media_db(dinfo, pwg, flags, size));
848 }
849
850
851 /*
852 * 'cups_add_dconstres()' - Add a constraint or resolver to an array.
853 */
854
855 static void
856 cups_add_dconstres(
857 cups_array_t *a, /* I - Array */
858 ipp_t *collection) /* I - Collection value */
859 {
860 ipp_attribute_t *attr; /* Attribute */
861 _cups_dconstres_t *temp; /* Current constraint/resolver */
862
863
864 if ((attr = ippFindAttribute(collection, "resolver-name",
865 IPP_TAG_NAME)) == NULL)
866 return;
867
868 if ((temp = calloc(1, sizeof(_cups_dconstres_t))) == NULL)
869 return;
870
871 temp->name = attr->values[0].string.text;
872 temp->collection = collection;
873
874 cupsArrayAdd(a, temp);
875 }
876
877
878 /*
879 * 'cups_compare_dconstres()' - Compare to resolver entries.
880 */
881
882 static int /* O - Result of comparison */
883 cups_compare_dconstres(
884 _cups_dconstres_t *a, /* I - First resolver */
885 _cups_dconstres_t *b) /* I - Second resolver */
886 {
887 return (strcmp(a->name, b->name));
888 }
889
890
891 /*
892 * 'cups_compare_media_db()' - Compare two media entries.
893 */
894
895 static int /* O - Result of comparison */
896 cups_compare_media_db(
897 _cups_media_db_t *a, /* I - First media entries */
898 _cups_media_db_t *b) /* I - Second media entries */
899 {
900 int result; /* Result of comparison */
901
902
903 if ((result = a->width - b->width) == 0)
904 result = a->length - b->length;
905
906 return (result);
907 }
908
909
910 /*
911 * 'cups_copy_media_db()' - Copy a media entry.
912 */
913
914 static _cups_media_db_t * /* O - New media entry */
915 cups_copy_media_db(
916 _cups_media_db_t *mdb) /* I - Media entry to copy */
917 {
918 _cups_media_db_t *temp; /* New media entry */
919
920
921 if ((temp = calloc(1, sizeof(_cups_media_db_t))) == NULL)
922 return (NULL);
923
924 if (mdb->color)
925 temp->color = _cupsStrAlloc(mdb->color);
926 if (mdb->key)
927 temp->key = _cupsStrAlloc(mdb->key);
928 if (mdb->info)
929 temp->info = _cupsStrAlloc(mdb->info);
930 if (mdb->size_name)
931 temp->size_name = _cupsStrAlloc(mdb->size_name);
932 if (mdb->source)
933 temp->source = _cupsStrAlloc(mdb->source);
934 if (mdb->type)
935 temp->type = _cupsStrAlloc(mdb->type);
936
937 temp->width = mdb->width;
938 temp->length = mdb->length;
939 temp->bottom = mdb->bottom;
940 temp->left = mdb->left;
941 temp->right = mdb->right;
942 temp->top = mdb->top;
943
944 return (temp);
945 }
946
947
948 /*
949 * 'cups_create_constraints()' - Create the constraints and resolvers arrays.
950 */
951
952 static void
953 cups_create_constraints(
954 cups_dinfo_t *dinfo) /* I - Destination information */
955 {
956 int i; /* Looping var */
957 ipp_attribute_t *attr; /* Attribute */
958 _ipp_value_t *val; /* Current value */
959
960
961 dinfo->constraints = cupsArrayNew3(NULL, NULL, NULL, 0, NULL,
962 (cups_afree_func_t)free);
963 dinfo->resolvers = cupsArrayNew3((cups_array_func_t)cups_compare_dconstres,
964 NULL, NULL, 0, NULL,
965 (cups_afree_func_t)free);
966
967 if ((attr = ippFindAttribute(dinfo->attrs, "job-constraints-supported",
968 IPP_TAG_BEGIN_COLLECTION)) != NULL)
969 {
970 for (i = attr->num_values, val = attr->values; i > 0; i --, val ++)
971 cups_add_dconstres(dinfo->constraints, val->collection);
972 }
973
974 if ((attr = ippFindAttribute(dinfo->attrs, "job-resolvers-supported",
975 IPP_TAG_BEGIN_COLLECTION)) != NULL)
976 {
977 for (i = attr->num_values, val = attr->values; i > 0; i --, val ++)
978 cups_add_dconstres(dinfo->resolvers, val->collection);
979 }
980 }
981
982
983 /*
984 * 'cups_create_defaults()' - Create the -default option array.
985 *
986 * TODO: Need to support collection defaults...
987 */
988
989 static void
990 cups_create_defaults(
991 cups_dinfo_t *dinfo) /* I - Destination information */
992 {
993 ipp_attribute_t *attr; /* Current attribute */
994 char name[IPP_MAX_NAME + 1],
995 /* Current name */
996 *nameptr, /* Pointer into current name */
997 value[2048]; /* Current value */
998
999
1000 /*
1001 * Iterate through the printer attributes looking for xxx-default and adding
1002 * xxx=value to the defaults option array.
1003 */
1004
1005 for (attr = ippFirstAttribute(dinfo->attrs);
1006 attr;
1007 attr = ippNextAttribute(dinfo->attrs))
1008 {
1009 if (!attr->name || attr->group_tag != IPP_TAG_PRINTER)
1010 continue;
1011
1012 if (attr->value_tag == IPP_TAG_BEGIN_COLLECTION)
1013 continue; /* TODO: STR #4096 */
1014
1015 if ((nameptr = attr->name + strlen(attr->name) - 8) <= attr->name ||
1016 strcmp(nameptr, "-default"))
1017 continue;
1018
1019 strlcpy(name, attr->name, sizeof(name));
1020 if ((nameptr = name + strlen(name) - 8) <= name ||
1021 strcmp(nameptr, "-default"))
1022 continue;
1023
1024 *nameptr = '\0';
1025
1026 if (ippAttributeString(attr, value, sizeof(value)) >= sizeof(value))
1027 continue;
1028
1029 dinfo->num_defaults = cupsAddOption(name, value, dinfo->num_defaults,
1030 &dinfo->defaults);
1031 }
1032 }
1033
1034
1035 /*
1036 * 'cups_create_media_db()' - Create the media database.
1037 */
1038
1039 static void
1040 cups_create_media_db(
1041 cups_dinfo_t *dinfo) /* I - Destination information */
1042 {
1043 int i; /* Looping var */
1044 _ipp_value_t *val; /* Current value */
1045 ipp_attribute_t *media_col_db, /* media-col-database */
1046 *media_attr, /* media-xxx */
1047 *x_dimension, /* x-dimension */
1048 *y_dimension; /* y-dimension */
1049 _pwg_media_t *pwg; /* PWG media info */
1050 _cups_media_db_t mdb; /* Media entry */
1051
1052
1053 dinfo->media_db = cupsArrayNew3((cups_array_func_t)cups_compare_media_db,
1054 NULL, NULL, 0,
1055 (cups_acopy_func_t)cups_copy_media_db,
1056 (cups_afree_func_t)cups_free_media_db);
1057 dinfo->min_size.width = INT_MAX;
1058 dinfo->min_size.length = INT_MAX;
1059 dinfo->max_size.width = 0;
1060 dinfo->max_size.length = 0;
1061
1062 if ((media_col_db = ippFindAttribute(dinfo->attrs, "media-col-database",
1063 IPP_TAG_BEGIN_COLLECTION)) != NULL)
1064 {
1065 _ipp_value_t *custom = NULL; /* Custom size range value */
1066
1067 for (i = media_col_db->num_values, val = media_col_db->values;
1068 i > 0;
1069 i --, val ++)
1070 {
1071 memset(&mdb, 0, sizeof(mdb));
1072
1073 if ((media_attr = ippFindAttribute(val->collection, "media-size",
1074 IPP_TAG_BEGIN_COLLECTION)) != NULL)
1075 {
1076 ipp_t *media_size = media_attr->values[0].collection;
1077 /* media-size collection value */
1078
1079 if ((x_dimension = ippFindAttribute(media_size, "x-dimension",
1080 IPP_TAG_INTEGER)) != NULL &&
1081 (y_dimension = ippFindAttribute(media_size, "y-dimension",
1082 IPP_TAG_INTEGER)) != NULL)
1083 {
1084 mdb.width = x_dimension->values[0].integer;
1085 mdb.length = y_dimension->values[0].integer;
1086 }
1087 else if ((x_dimension = ippFindAttribute(media_size, "x-dimension",
1088 IPP_TAG_RANGE)) != NULL &&
1089 (y_dimension = ippFindAttribute(media_size, "y-dimension",
1090 IPP_TAG_RANGE)) != NULL)
1091 {
1092 /*
1093 * Custom size range; save this as the custom size value with default
1094 * margins, then continue; we'll capture the real margins below...
1095 */
1096
1097 custom = val;
1098
1099 dinfo->min_size.width = x_dimension->values[0].range.lower;
1100 dinfo->min_size.length = y_dimension->values[0].range.lower;
1101 dinfo->min_size.left =
1102 dinfo->min_size.right = 635; /* Default 1/4" side margins */
1103 dinfo->min_size.top =
1104 dinfo->min_size.bottom = 1270; /* Default 1/2" top/bottom margins */
1105
1106 dinfo->max_size.width = x_dimension->values[0].range.upper;
1107 dinfo->max_size.length = y_dimension->values[0].range.upper;
1108 dinfo->max_size.left =
1109 dinfo->max_size.right = 635; /* Default 1/4" side margins */
1110 dinfo->max_size.top =
1111 dinfo->max_size.bottom = 1270; /* Default 1/2" top/bottom margins */
1112
1113 continue;
1114 }
1115 }
1116
1117 if ((media_attr = ippFindAttribute(val->collection, "media-color",
1118 IPP_TAG_ZERO)) != NULL &&
1119 (media_attr->value_tag == IPP_TAG_NAME ||
1120 media_attr->value_tag == IPP_TAG_NAMELANG ||
1121 media_attr->value_tag == IPP_TAG_KEYWORD))
1122 mdb.color = media_attr->values[0].string.text;
1123
1124 if ((media_attr = ippFindAttribute(val->collection, "media-info",
1125 IPP_TAG_TEXT)) != NULL)
1126 mdb.info = media_attr->values[0].string.text;
1127
1128 if ((media_attr = ippFindAttribute(val->collection, "media-key",
1129 IPP_TAG_ZERO)) != NULL &&
1130 (media_attr->value_tag == IPP_TAG_NAME ||
1131 media_attr->value_tag == IPP_TAG_NAMELANG ||
1132 media_attr->value_tag == IPP_TAG_KEYWORD))
1133 mdb.key = media_attr->values[0].string.text;
1134
1135 if ((media_attr = ippFindAttribute(val->collection, "media-size-name",
1136 IPP_TAG_ZERO)) != NULL &&
1137 (media_attr->value_tag == IPP_TAG_NAME ||
1138 media_attr->value_tag == IPP_TAG_NAMELANG ||
1139 media_attr->value_tag == IPP_TAG_KEYWORD))
1140 mdb.size_name = media_attr->values[0].string.text;
1141
1142 if ((media_attr = ippFindAttribute(val->collection, "media-source",
1143 IPP_TAG_ZERO)) != NULL &&
1144 (media_attr->value_tag == IPP_TAG_NAME ||
1145 media_attr->value_tag == IPP_TAG_NAMELANG ||
1146 media_attr->value_tag == IPP_TAG_KEYWORD))
1147 mdb.source = media_attr->values[0].string.text;
1148
1149 if ((media_attr = ippFindAttribute(val->collection, "media-type",
1150 IPP_TAG_ZERO)) != NULL &&
1151 (media_attr->value_tag == IPP_TAG_NAME ||
1152 media_attr->value_tag == IPP_TAG_NAMELANG ||
1153 media_attr->value_tag == IPP_TAG_KEYWORD))
1154 mdb.type = media_attr->values[0].string.text;
1155
1156 if ((media_attr = ippFindAttribute(val->collection, "media-bottom-margin",
1157 IPP_TAG_INTEGER)) != NULL)
1158 mdb.bottom = media_attr->values[0].integer;
1159
1160 if ((media_attr = ippFindAttribute(val->collection, "media-left-margin",
1161 IPP_TAG_INTEGER)) != NULL)
1162 mdb.left = media_attr->values[0].integer;
1163
1164 if ((media_attr = ippFindAttribute(val->collection, "media-right-margin",
1165 IPP_TAG_INTEGER)) != NULL)
1166 mdb.right = media_attr->values[0].integer;
1167
1168 if ((media_attr = ippFindAttribute(val->collection, "media-top-margin",
1169 IPP_TAG_INTEGER)) != NULL)
1170 mdb.top = media_attr->values[0].integer;
1171
1172 cupsArrayAdd(dinfo->media_db, &mdb);
1173 }
1174
1175 if (custom)
1176 {
1177 if ((media_attr = ippFindAttribute(custom->collection,
1178 "media-bottom-margin",
1179 IPP_TAG_INTEGER)) != NULL)
1180 {
1181 dinfo->min_size.top =
1182 dinfo->max_size.top = media_attr->values[0].integer;
1183 }
1184
1185 if ((media_attr = ippFindAttribute(custom->collection,
1186 "media-left-margin",
1187 IPP_TAG_INTEGER)) != NULL)
1188 {
1189 dinfo->min_size.left =
1190 dinfo->max_size.left = media_attr->values[0].integer;
1191 }
1192
1193 if ((media_attr = ippFindAttribute(custom->collection,
1194 "media-right-margin",
1195 IPP_TAG_INTEGER)) != NULL)
1196 {
1197 dinfo->min_size.right =
1198 dinfo->max_size.right = media_attr->values[0].integer;
1199 }
1200
1201 if ((media_attr = ippFindAttribute(custom->collection,
1202 "media-top-margin",
1203 IPP_TAG_INTEGER)) != NULL)
1204 {
1205 dinfo->min_size.top =
1206 dinfo->max_size.top = media_attr->values[0].integer;
1207 }
1208 }
1209 }
1210 else if ((media_attr = ippFindAttribute(dinfo->attrs, "media-supported",
1211 IPP_TAG_ZERO)) != NULL &&
1212 (media_attr->value_tag == IPP_TAG_NAME ||
1213 media_attr->value_tag == IPP_TAG_NAMELANG ||
1214 media_attr->value_tag == IPP_TAG_KEYWORD))
1215 {
1216 memset(&mdb, 0, sizeof(mdb));
1217
1218 mdb.left =
1219 mdb.right = 635; /* Default 1/4" side margins */
1220 mdb.top =
1221 mdb.bottom = 1270; /* Default 1/2" top/bottom margins */
1222
1223 for (i = media_attr->num_values, val = media_attr->values;
1224 i > 0;
1225 i --, val ++)
1226 {
1227 if ((pwg = _pwgMediaForPWG(val->string.text)) == NULL)
1228 if ((pwg = _pwgMediaForLegacy(val->string.text)) == NULL)
1229 {
1230 DEBUG_printf(("3cups_create_media_db: Ignoring unknown size '%s'.",
1231 val->string.text));
1232 continue;
1233 }
1234
1235 mdb.width = pwg->width;
1236 mdb.length = pwg->length;
1237
1238 if (!strncmp(val->string.text, "custom_min_", 11))
1239 {
1240 mdb.size_name = NULL;
1241 dinfo->min_size = mdb;
1242 }
1243 else if (!strncmp(val->string.text, "custom_max_", 11))
1244 {
1245 mdb.size_name = NULL;
1246 dinfo->max_size = mdb;
1247 }
1248 else
1249 {
1250 mdb.size_name = val->string.text;
1251
1252 cupsArrayAdd(dinfo->media_db, &mdb);
1253 }
1254 }
1255 }
1256 }
1257
1258
1259 /*
1260 * 'cups_free_media_cb()' - Free a media entry.
1261 */
1262
1263 static void
1264 cups_free_media_db(
1265 _cups_media_db_t *mdb) /* I - Media entry to free */
1266 {
1267 if (mdb->color)
1268 _cupsStrFree(mdb->color);
1269 if (mdb->key)
1270 _cupsStrFree(mdb->key);
1271 if (mdb->info)
1272 _cupsStrFree(mdb->info);
1273 if (mdb->size_name)
1274 _cupsStrFree(mdb->size_name);
1275 if (mdb->source)
1276 _cupsStrFree(mdb->source);
1277 if (mdb->type)
1278 _cupsStrFree(mdb->type);
1279
1280 free(mdb);
1281 }
1282
1283
1284 /*
1285 * 'cups_get_media_db()' - Lookup the media entry for a given size.
1286 */
1287
1288 static int /* O - 1 on match, 0 on failure */
1289 cups_get_media_db(cups_dinfo_t *dinfo, /* I - Destination information */
1290 _pwg_media_t *pwg, /* I - PWG media info */
1291 unsigned flags, /* I - Media matching flags */
1292 cups_size_t *size) /* O - Media size/margin/name info */
1293 {
1294 _cups_media_db_t *mdb, /* Current media database entry */
1295 *best = NULL, /* Best matching entry */
1296 key; /* Search key */
1297
1298
1299 /*
1300 * Create the media database as needed...
1301 */
1302
1303 if (!dinfo->media_db)
1304 cups_create_media_db(dinfo);
1305
1306 /*
1307 * Find a match...
1308 */
1309
1310 memset(&key, 0, sizeof(key));
1311 key.width = pwg->width;
1312 key.length = pwg->length;
1313
1314 if ((mdb = cupsArrayFind(dinfo->media_db, &key)) != NULL)
1315 {
1316 /*
1317 * Found an exact match, let's figure out the best margins for the flags
1318 * supplied...
1319 */
1320
1321 best = mdb;
1322
1323 if (flags & CUPS_MEDIA_FLAGS_BORDERLESS)
1324 {
1325 /*
1326 * Look for the smallest margins...
1327 */
1328
1329 if (best->left != 0 || best->right != 0 || best->top != 0 ||
1330 best->bottom != 0)
1331 {
1332 for (mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db);
1333 mdb && !cups_compare_media_db(mdb, &key);
1334 mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db))
1335 {
1336 if (mdb->left <= best->left && mdb->right <= best->right &&
1337 mdb->top <= best->top && mdb->bottom <= best->bottom)
1338 {
1339 best = mdb;
1340 if (mdb->left == 0 && mdb->right == 0 && mdb->bottom == 0 &&
1341 mdb->top == 0)
1342 break;
1343 }
1344 }
1345 }
1346
1347 /*
1348 * If we need an exact match, return no-match if the size is not
1349 * borderless.
1350 */
1351
1352 if ((flags & CUPS_MEDIA_FLAGS_EXACT) &&
1353 (best->left || best->right || best->top || best->bottom))
1354 return (0);
1355 }
1356 else if (flags & CUPS_MEDIA_FLAGS_DUPLEX)
1357 {
1358 /*
1359 * Look for the largest margins...
1360 */
1361
1362 for (mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db);
1363 mdb && !cups_compare_media_db(mdb, &key);
1364 mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db))
1365 {
1366 if (mdb->left >= best->left && mdb->right >= best->right &&
1367 mdb->top >= best->top && mdb->bottom >= best->bottom)
1368 best = mdb;
1369 }
1370 }
1371 else
1372 {
1373 /*
1374 * Look for the smallest non-zero margins...
1375 */
1376
1377 for (mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db);
1378 mdb && !cups_compare_media_db(mdb, &key);
1379 mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db))
1380 {
1381 if (((mdb->left > 0 && mdb->left <= best->left) || best->left == 0) &&
1382 ((mdb->right > 0 && mdb->right <= best->right) ||
1383 best->right == 0) &&
1384 ((mdb->top > 0 && mdb->top <= best->top) || best->top == 0) &&
1385 ((mdb->bottom > 0 && mdb->bottom <= best->bottom) ||
1386 best->bottom == 0))
1387 best = mdb;
1388 }
1389 }
1390 }
1391 else if (flags & CUPS_MEDIA_FLAGS_EXACT)
1392 {
1393 /*
1394 * See if we can do this as a custom size...
1395 */
1396
1397 if (pwg->width < dinfo->min_size.width ||
1398 pwg->width > dinfo->max_size.width ||
1399 pwg->length < dinfo->min_size.length ||
1400 pwg->length > dinfo->max_size.length)
1401 return (0); /* Out of range */
1402
1403 if ((flags & CUPS_MEDIA_FLAGS_BORDERLESS) &&
1404 (dinfo->min_size.left > 0 || dinfo->min_size.right > 0 ||
1405 dinfo->min_size.top > 0 || dinfo->min_size.bottom > 0))
1406 return (0); /* Not borderless */
1407
1408 key.size_name = (char *)pwg->pwg;
1409 key.bottom = dinfo->min_size.bottom;
1410 key.left = dinfo->min_size.left;
1411 key.right = dinfo->min_size.right;
1412 key.top = dinfo->min_size.top;
1413
1414 best = &key;
1415 }
1416 else if (pwg->width >= dinfo->min_size.width &&
1417 pwg->width <= dinfo->max_size.width &&
1418 pwg->length >= dinfo->min_size.length &&
1419 pwg->length <= dinfo->max_size.length)
1420 {
1421 /*
1422 * Map to custom size...
1423 */
1424
1425 key.size_name = (char *)pwg->pwg;
1426 key.bottom = dinfo->min_size.bottom;
1427 key.left = dinfo->min_size.left;
1428 key.right = dinfo->min_size.right;
1429 key.top = dinfo->min_size.top;
1430
1431 best = &key;
1432 }
1433 else
1434 {
1435 /*
1436 * Find a close size...
1437 */
1438
1439 for (mdb = (_cups_media_db_t *)cupsArrayFirst(dinfo->media_db);
1440 mdb;
1441 mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db))
1442 if (cups_is_close_media_db(mdb, &key))
1443 break;
1444
1445 if (!mdb)
1446 return (0);
1447
1448 best = mdb;
1449
1450 if (flags & CUPS_MEDIA_FLAGS_BORDERLESS)
1451 {
1452 /*
1453 * Look for the smallest margins...
1454 */
1455
1456 if (best->left != 0 || best->right != 0 || best->top != 0 ||
1457 best->bottom != 0)
1458 {
1459 for (mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db);
1460 mdb && cups_is_close_media_db(mdb, &key);
1461 mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db))
1462 {
1463 if (mdb->left <= best->left && mdb->right <= best->right &&
1464 mdb->top <= best->top && mdb->bottom <= best->bottom)
1465 {
1466 best = mdb;
1467 if (mdb->left == 0 && mdb->right == 0 && mdb->bottom == 0 &&
1468 mdb->top == 0)
1469 break;
1470 }
1471 }
1472 }
1473 }
1474 else if (flags & CUPS_MEDIA_FLAGS_DUPLEX)
1475 {
1476 /*
1477 * Look for the largest margins...
1478 */
1479
1480 for (mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db);
1481 mdb && cups_is_close_media_db(mdb, &key);
1482 mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db))
1483 {
1484 if (mdb->left >= best->left && mdb->right >= best->right &&
1485 mdb->top >= best->top && mdb->bottom >= best->bottom)
1486 best = mdb;
1487 }
1488 }
1489 else
1490 {
1491 /*
1492 * Look for the smallest non-zero margins...
1493 */
1494
1495 for (mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db);
1496 mdb && cups_is_close_media_db(mdb, &key);
1497 mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db))
1498 {
1499 if (((mdb->left > 0 && mdb->left <= best->left) || best->left == 0) &&
1500 ((mdb->right > 0 && mdb->right <= best->right) ||
1501 best->right == 0) &&
1502 ((mdb->top > 0 && mdb->top <= best->top) || best->top == 0) &&
1503 ((mdb->bottom > 0 && mdb->bottom <= best->bottom) ||
1504 best->bottom == 0))
1505 best = mdb;
1506 }
1507 }
1508 }
1509
1510 if (best)
1511 {
1512 /*
1513 * Return the matching size...
1514 */
1515
1516 if (best->size_name)
1517 strlcpy(size->media, best->size_name, sizeof(size->media));
1518 else if (best->key)
1519 strlcpy(size->media, best->key, sizeof(size->media));
1520 else
1521 strlcpy(size->media, pwg->pwg, sizeof(size->media));
1522
1523 size->width = best->width;
1524 size->length = best->length;
1525 size->bottom = best->bottom;
1526 size->left = best->left;
1527 size->right = best->right;
1528 size->top = best->top;
1529
1530 return (1);
1531 }
1532
1533 return (0);
1534 }
1535
1536
1537 /*
1538 * 'cups_is_close_media_db()' - Compare two media entries to see if they are
1539 * close to the same size.
1540 *
1541 * Currently we use 5 points (from PostScript) as the matching range...
1542 */
1543
1544 static int /* O - 1 if the sizes are close */
1545 cups_is_close_media_db(
1546 _cups_media_db_t *a, /* I - First media entries */
1547 _cups_media_db_t *b) /* I - Second media entries */
1548 {
1549 int dwidth, /* Difference in width */
1550 dlength; /* Difference in length */
1551
1552
1553 dwidth = a->width - b->width;
1554 dlength = a->length - b->length;
1555
1556 return (dwidth >= -176 && dwidth <= 176 &&
1557 dlength >= -176 && dlength <= 176);
1558 }
1559
1560
1561 /*
1562 * 'cups_test_constraints()' - Test constraints.
1563 *
1564 * TODO: STR #4096 - Need to properly support media-col contraints...
1565 */
1566
1567 static cups_array_t * /* O - Active constraints */
1568 cups_test_constraints(
1569 cups_dinfo_t *dinfo, /* I - Destination information */
1570 const char *new_option, /* I - Newly selected option */
1571 const char *new_value, /* I - Newly selected value */
1572 int num_options, /* I - Number of options */
1573 cups_option_t *options, /* I - Options */
1574 int *num_conflicts, /* O - Number of conflicting options */
1575 cups_option_t **conflicts) /* O - Conflicting options */
1576 {
1577 int i, /* Looping var */
1578 match; /* Value matches? */
1579 int num_matching; /* Number of matching options */
1580 cups_option_t *matching; /* Matching options */
1581 _cups_dconstres_t *c; /* Current constraint */
1582 cups_array_t *active = NULL; /* Active constraints */
1583 ipp_attribute_t *attr; /* Current attribute */
1584 _ipp_value_t *attrval; /* Current attribute value */
1585 const char *value; /* Current value */
1586 char temp[1024]; /* Temporary string */
1587 int int_value; /* Integer value */
1588 int xres_value, /* Horizontal resolution */
1589 yres_value; /* Vertical resolution */
1590 ipp_res_t units_value; /* Resolution units */
1591
1592
1593 for (c = (_cups_dconstres_t *)cupsArrayFirst(dinfo->constraints);
1594 c;
1595 c = (_cups_dconstres_t *)cupsArrayNext(dinfo->constraints))
1596 {
1597 num_matching = 0;
1598 matching = NULL;
1599
1600 for (attr = ippFirstAttribute(c->collection);
1601 attr;
1602 attr = ippNextAttribute(c->collection))
1603 {
1604 if (attr->value_tag == IPP_TAG_BEGIN_COLLECTION)
1605 break; /* TODO: STR #4096 */
1606
1607 /*
1608 * Get the value for the current attribute in the constraint...
1609 */
1610
1611 if (new_option && new_value && !strcmp(attr->name, new_option))
1612 value = new_value;
1613 else if ((value = cupsGetOption(attr->name, num_options,
1614 options)) == NULL)
1615 value = cupsGetOption(attr->name, dinfo->num_defaults, dinfo->defaults);
1616
1617 if (!value)
1618 {
1619 /*
1620 * Not set so this constraint does not apply...
1621 */
1622
1623 break;
1624 }
1625
1626 match = 0;
1627
1628 switch (attr->value_tag)
1629 {
1630 case IPP_TAG_INTEGER :
1631 case IPP_TAG_ENUM :
1632 int_value = atoi(value);
1633
1634 for (i = attr->num_values, attrval = attr->values;
1635 i > 0;
1636 i --, attrval ++)
1637 {
1638 if (attrval->integer == int_value)
1639 {
1640 match = 1;
1641 break;
1642 }
1643 }
1644 break;
1645
1646 case IPP_TAG_BOOLEAN :
1647 int_value = !strcmp(value, "true");
1648
1649 for (i = attr->num_values, attrval = attr->values;
1650 i > 0;
1651 i --, attrval ++)
1652 {
1653 if (attrval->boolean == int_value)
1654 {
1655 match = 1;
1656 break;
1657 }
1658 }
1659 break;
1660
1661 case IPP_TAG_RANGE :
1662 int_value = atoi(value);
1663
1664 for (i = attr->num_values, attrval = attr->values;
1665 i > 0;
1666 i --, attrval ++)
1667 {
1668 if (int_value >= attrval->range.lower &&
1669 int_value <= attrval->range.upper)
1670 {
1671 match = 1;
1672 break;
1673 }
1674 }
1675 break;
1676
1677 case IPP_TAG_RESOLUTION :
1678 if (sscanf(value, "%dx%d%15s", &xres_value, &yres_value, temp) != 3)
1679 {
1680 if (sscanf(value, "%d%15s", &xres_value, temp) != 2)
1681 break;
1682
1683 yres_value = xres_value;
1684 }
1685
1686 if (!strcmp(temp, "dpi"))
1687 units_value = IPP_RES_PER_INCH;
1688 else if (!strcmp(temp, "dpc") || !strcmp(temp, "dpcm"))
1689 units_value = IPP_RES_PER_CM;
1690 else
1691 break;
1692
1693 for (i = attr->num_values, attrval = attr->values;
1694 i > 0;
1695 i --, attrval ++)
1696 {
1697 if (attrval->resolution.xres == xres_value &&
1698 attrval->resolution.yres == yres_value &&
1699 attrval->resolution.units == units_value)
1700 {
1701 match = 1;
1702 break;
1703 }
1704 }
1705 break;
1706
1707 case IPP_TAG_TEXT :
1708 case IPP_TAG_NAME :
1709 case IPP_TAG_KEYWORD :
1710 case IPP_TAG_CHARSET :
1711 case IPP_TAG_URI :
1712 case IPP_TAG_URISCHEME :
1713 case IPP_TAG_MIMETYPE :
1714 case IPP_TAG_LANGUAGE :
1715 case IPP_TAG_TEXTLANG :
1716 case IPP_TAG_NAMELANG :
1717 for (i = attr->num_values, attrval = attr->values;
1718 i > 0;
1719 i --, attrval ++)
1720 {
1721 if (!strcmp(attrval->string.text, value))
1722 {
1723 match = 1;
1724 break;
1725 }
1726 }
1727 break;
1728
1729 default :
1730 break;
1731 }
1732
1733 if (!match)
1734 break;
1735
1736 num_matching = cupsAddOption(attr->name, value, num_matching, &matching);
1737 }
1738
1739 if (!attr)
1740 {
1741 if (!active)
1742 active = cupsArrayNew(NULL, NULL);
1743
1744 cupsArrayAdd(active, c);
1745
1746 if (num_conflicts && conflicts)
1747 {
1748 cups_option_t *moption; /* Matching option */
1749
1750 for (i = num_matching, moption = matching; i > 0; i --, moption ++)
1751 *num_conflicts = cupsAddOption(moption->name, moption->value,
1752 *num_conflicts, conflicts);
1753 }
1754 }
1755
1756 cupsFreeOptions(num_matching, matching);
1757 }
1758
1759 return (active);
1760 }
1761
1762
1763 /*
1764 * End of "$Id$".
1765 */