| 1 | /* |
| 2 | * |
| 3 | * Dump the per-printer options for Gutenprint |
| 4 | * |
| 5 | * Copyright 2000-2018 Robert Krawitz ([email protected]) |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the Free |
| 9 | * Software Foundation; either version 2 of the License, or (at your option) |
| 10 | * any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 14 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | * for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #ifdef HAVE_CONFIG_H |
| 22 | #include <config.h> |
| 23 | #endif |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <gutenprint/gutenprint.h> |
| 27 | #include <gutenprint/gutenprint-intl.h> |
| 28 | |
| 29 | int |
| 30 | main(int argc, char **argv) |
| 31 | { |
| 32 | int i; |
| 33 | size_t j, k; |
| 34 | int first_arg = 1; |
| 35 | stp_string_list_t *printer_list = NULL; |
| 36 | stp_parameter_level_t max_level = STP_PARAMETER_LEVEL_ADVANCED4; |
| 37 | if (argc > 1 && !strcmp(argv[1], "-s")) |
| 38 | { |
| 39 | max_level = STP_PARAMETER_LEVEL_BASIC; |
| 40 | first_arg++; |
| 41 | } |
| 42 | |
| 43 | stp_init(); |
| 44 | |
| 45 | if (argc > first_arg) |
| 46 | { |
| 47 | printer_list = stp_string_list_create(); |
| 48 | for (i = 1; i < argc; i++) |
| 49 | stp_string_list_add_string(printer_list, argv[i], argv[i]); |
| 50 | } |
| 51 | for (i = 0; i < stp_printer_model_count(); i++) |
| 52 | { |
| 53 | stp_parameter_list_t params; |
| 54 | unsigned int nparams; |
| 55 | stp_parameter_t desc; |
| 56 | const stp_printer_t *printer = stp_get_printer_by_index(i); |
| 57 | const char *driver = stp_printer_get_driver(printer); |
| 58 | const char *family = stp_printer_get_family(printer); |
| 59 | stp_vars_t *pv; |
| 60 | int tcount = 0; |
| 61 | size_t count; |
| 62 | int printer_is_color = 0; |
| 63 | if (strcmp(family, "ps") == 0 || strcmp(family, "raw") == 0) |
| 64 | continue; |
| 65 | if (printer_list && !stp_string_list_is_present(printer_list, driver)) |
| 66 | continue; |
| 67 | |
| 68 | pv = stp_vars_create_copy(stp_printer_get_defaults(printer)); |
| 69 | /* Set Job Mode to "Job" as this enables the Duplex option */ |
| 70 | stp_set_string_parameter(pv, "JobMode", "Job"); |
| 71 | |
| 72 | stp_describe_parameter(pv, "PrintingMode", &desc); |
| 73 | if (stp_string_list_is_present(desc.bounds.str, "Color")) |
| 74 | printer_is_color = 1; |
| 75 | stp_parameter_description_destroy(&desc); |
| 76 | if (printer_is_color) |
| 77 | stp_set_string_parameter(pv, "PrintingMode", "Color"); |
| 78 | else |
| 79 | stp_set_string_parameter(pv, "PrintingMode", "BW"); |
| 80 | stp_set_string_parameter(pv, "ChannelBitDepth", "8"); |
| 81 | |
| 82 | printf("# Printer model %s, long name `%s'\n", driver, |
| 83 | stp_printer_get_long_name(printer)); |
| 84 | |
| 85 | printf("$families{'%s'} = '%s';\n", driver, family); |
| 86 | printf("$models{'%s'} = '%d';\n", driver, stp_get_model_id(pv)); |
| 87 | |
| 88 | params = stp_get_parameter_list(pv); |
| 89 | nparams = stp_parameter_list_count(params); |
| 90 | |
| 91 | for (k = 0; k < nparams; k++) |
| 92 | { |
| 93 | const stp_parameter_t *p = stp_parameter_list_param(params, k); |
| 94 | if (p->read_only || |
| 95 | (p->p_level > max_level && strcmp(p->name, "Resolution") != 0) || |
| 96 | (p->p_class != STP_PARAMETER_CLASS_OUTPUT && |
| 97 | p->p_class != STP_PARAMETER_CLASS_CORE && |
| 98 | p->p_class != STP_PARAMETER_CLASS_FEATURE)) |
| 99 | continue; |
| 100 | count = 0; |
| 101 | stp_describe_parameter(pv, p->name, &desc); |
| 102 | if (desc.is_active) |
| 103 | { |
| 104 | if (desc.p_type == STP_PARAMETER_TYPE_STRING_LIST) |
| 105 | { |
| 106 | count = stp_string_list_count(desc.bounds.str); |
| 107 | if (count > 0) |
| 108 | { |
| 109 | if (strcmp(desc.name, "Resolution") == 0) |
| 110 | { |
| 111 | for (j = 0; j < count; j++) |
| 112 | { |
| 113 | const stp_param_string_t *param = |
| 114 | stp_string_list_param(desc.bounds.str, j); |
| 115 | stp_resolution_t x, y; |
| 116 | stp_set_string_parameter(pv, "Resolution", |
| 117 | param->name); |
| 118 | stp_describe_resolution(pv, &x, &y); |
| 119 | if (x > 0 && y > 0) |
| 120 | printf("$resolutions{'%s'}{'%s'} = [%d, %d];\n", |
| 121 | driver, param->name, (int) x, (int) y); |
| 122 | } |
| 123 | stp_clear_string_parameter(pv, "Resolution"); |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | if (strcmp(desc.name, "PageSize") == 0) |
| 128 | { |
| 129 | stp_dimension_t min_area = 7200 * 7200; |
| 130 | stp_dimension_t max_area = 0; |
| 131 | const char *min_size_name = NULL; |
| 132 | const char *max_size_name = NULL; |
| 133 | for (j = 0; j < count; j++) |
| 134 | { |
| 135 | const stp_param_string_t *param = |
| 136 | stp_string_list_param(desc.bounds.str, j); |
| 137 | const stp_papersize_t *ps = |
| 138 | stp_describe_papersize(pv, param->name); |
| 139 | if (ps->width > 0 && ps->height > 0 && |
| 140 | (ps->width * ps->height) < min_area) |
| 141 | { |
| 142 | min_area = ps->width * ps->height; |
| 143 | min_size_name = param->name; |
| 144 | } |
| 145 | if (ps->width > 0 && ps->height > 0 && |
| 146 | (ps->width * ps->height) > max_area) |
| 147 | { |
| 148 | max_area = ps->width * ps->height; |
| 149 | max_size_name = param->name; |
| 150 | } |
| 151 | } |
| 152 | if (min_size_name) |
| 153 | { |
| 154 | printf("$min_page_size{'%s'} = '%s';\n", |
| 155 | driver, min_size_name); |
| 156 | printf("$max_page_size{'%s'} = '%s';\n", |
| 157 | driver, max_size_name); |
| 158 | } |
| 159 | } |
| 160 | printf("$stpdata{'%s'}{'%s'} = [qw(", driver, desc.name); |
| 161 | if (!desc.is_mandatory && ! |
| 162 | stp_string_list_is_present(desc.bounds.str, "None")) |
| 163 | fputs("+None ", stdout); |
| 164 | for (j = 0; j < count; j++) |
| 165 | { |
| 166 | const stp_param_string_t *param = |
| 167 | stp_string_list_param(desc.bounds.str, j); |
| 168 | printf("%s%s ", |
| 169 | (strcmp(desc.deflt.str, param->name)) ? "" : "+", |
| 170 | param->name); |
| 171 | } |
| 172 | fputs(")];\n", stdout); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | else if (desc.p_type == STP_PARAMETER_TYPE_BOOLEAN) |
| 177 | { |
| 178 | if (desc.is_mandatory) |
| 179 | printf("$stp_bools{'%s'}{'%s'} = %d;\n", |
| 180 | driver, desc.name, desc.deflt.boolean); |
| 181 | else |
| 182 | printf("$stp_bools{'%s'}{'%s'} = %d;\n", |
| 183 | driver, desc.name, -1); |
| 184 | } |
| 185 | else if (desc.p_type == STP_PARAMETER_TYPE_DOUBLE) |
| 186 | { |
| 187 | if (desc.bounds.dbl.lower <= desc.deflt.dbl && |
| 188 | desc.bounds.dbl.upper >= desc.deflt.dbl) |
| 189 | printf("$stp_float_values{'%s'}{'%s'} = [%d, %.3f, %.3f, %.3f];\n", |
| 190 | driver, desc.name, desc.is_mandatory, |
| 191 | desc.deflt.dbl, desc.bounds.dbl.lower, |
| 192 | desc.bounds.dbl.upper); |
| 193 | } |
| 194 | else if (desc.p_type == STP_PARAMETER_TYPE_INT) |
| 195 | { |
| 196 | if (desc.bounds.integer.lower <= desc.deflt.integer && |
| 197 | desc.bounds.integer.upper >= desc.deflt.integer) |
| 198 | printf("$stp_int_values{'%s'}{'%s'} = [%d, %d, %d, %d];\n", |
| 199 | driver, desc.name, desc.is_mandatory, |
| 200 | desc.deflt.integer, desc.bounds.integer.lower, |
| 201 | desc.bounds.integer.upper); |
| 202 | } |
| 203 | else if (desc.p_type == STP_PARAMETER_TYPE_DIMENSION) |
| 204 | { |
| 205 | if (desc.bounds.dimension.lower <= desc.deflt.dimension && |
| 206 | desc.bounds.dimension.upper >= desc.deflt.dimension) |
| 207 | printf("$stp_dimension_values{'%s'}{'%s'} = [%d, %.3f, %.3f, %.3f];\n", |
| 208 | driver, desc.name, desc.is_mandatory, |
| 209 | desc.deflt.dimension, desc.bounds.dimension.lower, |
| 210 | desc.bounds.dimension.upper); |
| 211 | } |
| 212 | tcount += count; |
| 213 | } |
| 214 | stp_parameter_description_destroy(&desc); |
| 215 | } |
| 216 | stp_parameter_list_destroy(params); |
| 217 | if (tcount > 0) |
| 218 | { |
| 219 | printf("$stpdata{'%s'}{'Color'} = [qw(", driver); |
| 220 | if (printer_is_color) |
| 221 | fputs("Color RawCMYK ", stdout); |
| 222 | fputs("Grayscale BlackAndWhite)];\n", stdout); |
| 223 | } |
| 224 | stp_vars_destroy(pv); |
| 225 | } |
| 226 | if (printer_list) |
| 227 | stp_string_list_destroy(printer_list); |
| 228 | return 0; |
| 229 | } |