| 1 | /* |
| 2 | * |
| 3 | * Printer maintenance utility for EPSON Stylus (R) printers |
| 4 | * |
| 5 | * Copyright 2000-2003 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 <gutenprint/gutenprint.h> |
| 25 | #include <stdio.h> |
| 26 | #include <unistd.h> |
| 27 | #include <string.h> |
| 28 | #include <strings.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <errno.h> |
| 31 | #include <signal.h> |
| 32 | #include <sys/wait.h> |
| 33 | #include <ctype.h> |
| 34 | #include <time.h> |
| 35 | #if defined(HAVE_VARARGS_H) && !defined(HAVE_STDARG_H) |
| 36 | #include <varargs.h> |
| 37 | #else |
| 38 | #include <stdarg.h> |
| 39 | #endif |
| 40 | #ifdef HAVE_FCNTL_H |
| 41 | #include <fcntl.h> |
| 42 | #endif |
| 43 | #ifdef HAVE_POLL |
| 44 | #include <sys/poll.h> |
| 45 | #endif |
| 46 | #ifdef HAVE_GETOPT_H |
| 47 | #include <getopt.h> |
| 48 | #endif |
| 49 | #ifdef HAVE_READLINE_READLINE_H |
| 50 | #include <readline/readline.h> |
| 51 | #endif |
| 52 | #ifdef HAVE_READLINE_HISTORY_H |
| 53 | #include <readline/history.h> |
| 54 | #endif |
| 55 | #include <gutenprint/gutenprint-intl-internal.h> |
| 56 | #include "d4lib.h" |
| 57 | |
| 58 | void do_align(void); |
| 59 | char *do_get_input (const char *prompt); |
| 60 | void do_head_clean(void); |
| 61 | void do_help(int code); |
| 62 | void do_identify(void); |
| 63 | void do_ink_level(void); |
| 64 | void do_extended_ink_info(void); |
| 65 | void do_nozzle_check(void); |
| 66 | void do_status(void); |
| 67 | void do_reset_ink_levels(void); |
| 68 | int do_print_cmd(void); |
| 69 | |
| 70 | #pragma GCC diagnostic ignored "-Woverlength-strings" |
| 71 | #pragma GCC diagnostic ignored "-Wformat-nonliteral" |
| 72 | |
| 73 | const char *banner = N_("\ |
| 74 | Escputil version " VERSION ", Copyright (C) 2000-2006 Robert Krawitz\n\ |
| 75 | Escputil comes with ABSOLUTELY NO WARRANTY; for details type 'escputil -l'\n\ |
| 76 | This is free software, and you are welcome to redistribute it\n\ |
| 77 | under certain conditions; type 'escputil -l' for details.\n"); |
| 78 | |
| 79 | const char *license = N_("\ |
| 80 | Copyright 2000-2006 Robert Krawitz ([email protected])\n\ |
| 81 | \n\ |
| 82 | This program is free software; you can redistribute it and/or modify it\n\ |
| 83 | under the terms of the GNU General Public License as published by the Free\n\ |
| 84 | Software Foundation; either version 2 of the License, or (at your option)\n\ |
| 85 | any later version.\n\ |
| 86 | \n\ |
| 87 | This program is distributed in the hope that it will be useful, but\n\ |
| 88 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\n\ |
| 89 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n\ |
| 90 | for more details.\n\ |
| 91 | \n\ |
| 92 | You should have received a copy of the GNU General Public License\n\ |
| 93 | along with this program. If not, see <https://www.gnu.org/licenses/>.\n"); |
| 94 | |
| 95 | |
| 96 | #if defined(HAVE_GETOPT_H) && defined(HAVE_GETOPT_LONG) |
| 97 | |
| 98 | struct option optlist[] = |
| 99 | { |
| 100 | { "printer-name", 1, NULL, (int) 'P' }, |
| 101 | { "raw-device", 1, NULL, (int) 'r' }, |
| 102 | { "ink-level", 0, NULL, (int) 'i' }, |
| 103 | { "extended-ink-info", 0, NULL, (int) 'e' }, |
| 104 | { "clean-head", 0, NULL, (int) 'c' }, |
| 105 | { "nozzle-check", 0, NULL, (int) 'n' }, |
| 106 | { "align-head", 0, NULL, (int) 'a' }, |
| 107 | { "status", 0, NULL, (int) 's' }, |
| 108 | { "reset-ink-levels", 0, NULL, (int) 'X' }, |
| 109 | { "new", 0, NULL, (int) 'u' }, |
| 110 | { "help", 0, NULL, (int) 'h' }, |
| 111 | { "identify", 0, NULL, (int) 'd' }, |
| 112 | { "model", 1, NULL, (int) 'm' }, |
| 113 | { "quiet", 0, NULL, (int) 'q' }, |
| 114 | { "license", 0, NULL, (int) 'l' }, |
| 115 | { "list-models", 0, NULL, (int) 'M' }, |
| 116 | { "short-name", 0, NULL, (int) 'S' }, |
| 117 | { "choices", 0, NULL, (int) 'C' }, |
| 118 | { "patterns", 0, NULL, (int) 'p' }, |
| 119 | { NULL, 0, NULL, 0 } |
| 120 | }; |
| 121 | |
| 122 | const char *help_msg = N_("\ |
| 123 | Usage: escputil [-c | -n | -a | -i | -e | -s | -d | -l | -M | -X]\n\ |
| 124 | [-P printer | -r device] [-u] [-q] [-m model] [ -S ]\n\ |
| 125 | [-C choices] [-p patterns]\n\ |
| 126 | Perform maintenance on EPSON Stylus (R) printers.\n\ |
| 127 | Examples: escputil --ink-level --raw-device /dev/usb/lp0\n\ |
| 128 | escputil --clean-head --new --printer-name MyQueue\n\ |
| 129 | \n\ |
| 130 | Commands:\n\ |
| 131 | -c|--clean-head Clean the print head.\n\ |
| 132 | -n|--nozzle-check Print a nozzle test pattern.\n\ |
| 133 | Dirty or clogged nozzles will show as gaps in the\n\ |
| 134 | pattern. If you see any gaps, you should clean\n\ |
| 135 | the print head.\n\ |
| 136 | -a|--align-head Align the print head. CAUTION: Misuse of this\n\ |
| 137 | utility may result in poor print quality and/or\n\ |
| 138 | damage to the printer.\n\ |
| 139 | -s|--status Retrieve printer status.\n\ |
| 140 | -i|--ink-level Obtain the ink level from the printer. This requires\n\ |
| 141 | read/write access to the raw printer device.\n\ |
| 142 | -e|--extended-ink-info Obtain the extended ink information from the\n\ |
| 143 | printer. This requires read/write access to the raw\n\ |
| 144 | printer device.\n\ |
| 145 | -d|--identify Query the printer for make and model information.\n\ |
| 146 | This requires read/write access to the raw printer\n\ |
| 147 | device.\n\ |
| 148 | -X|--reset-ink Reset ink levels.\n\ |
| 149 | This requires read/write access to the raw printer\n\ |
| 150 | device.\n\ |
| 151 | -l|--license Display the license/warranty terms of this program.\n\ |
| 152 | -M|--list-models List the available printer models.\n\ |
| 153 | -h|--help Print this help message.\n\ |
| 154 | Options:\n\ |
| 155 | -P|--printer-name Specify the name of the printer queue to operate on.\n\ |
| 156 | Default is the default system printer.\n\ |
| 157 | -r|--raw-device Specify the name of the device to write to directly\n\ |
| 158 | rather than going through a printer queue.\n\ |
| 159 | -m|--model Specify the printer model.\n\ |
| 160 | -u|--new The printer is a new printer (Stylus Color 740 or\n\ |
| 161 | newer). Only needed when not using a raw device or\n\ |
| 162 | when the model is not specified.\n\ |
| 163 | -q|--quiet Suppress the banner.\n\ |
| 164 | -S|--short-name Print the short name of the printer with --identify.\n\ |
| 165 | -C|--choices Specify the number of pattern choices for alignment\n\ |
| 166 | -p|--patterns Specify the number of sets of patterns for alignment\n"); |
| 167 | #else |
| 168 | const char *help_msg = N_("\ |
| 169 | Usage: escputil [OPTIONS] [COMMAND]\n\ |
| 170 | Usage: escputil [-c | -n | -a | -i | -e | -s | -d | -l | -M | -X]\n\ |
| 171 | [-P printer | -r device] [-u] [-q] [-m model] [ -S ]\n\ |
| 172 | [-C choices] [-p patterns]\n\ |
| 173 | Perform maintenance on EPSON Stylus (R) printers.\n\ |
| 174 | Examples: escputil -i -r /dev/usb/lp0\n\ |
| 175 | escputil -c -u -P MyQueue\n\ |
| 176 | \n\ |
| 177 | Commands:\n\ |
| 178 | -c Clean the print head.\n\ |
| 179 | -n Print a nozzle test pattern.\n\ |
| 180 | Dirty or clogged nozzles will show as gaps in the\n\ |
| 181 | pattern. If you see any gaps, you should clean\n\ |
| 182 | the print head.\n\ |
| 183 | -a Align the print head. CAUTION: Misuse of this\n\ |
| 184 | utility may result in poor print quality and/or\n\ |
| 185 | damage to the printer.\n\ |
| 186 | -s Retrieve printer status.\n\ |
| 187 | -i Obtain the ink level from the printer. This requires\n\ |
| 188 | read/write access to the raw printer device.\n\ |
| 189 | -e Obtain the extended ink information from the printer.\n\ |
| 190 | Only for R800 printer and friends. This requires\n\ |
| 191 | read/write access to the raw printer device.\n\ |
| 192 | -d Query the printer for make and model information. This\n\ |
| 193 | requires read/write access to the raw printer device.\n\ |
| 194 | -X Reset ink levels. This requires read/write access to the raw\n\ |
| 195 | printer device.\n\ |
| 196 | -l Display the license/warranty terms of this program.\n\ |
| 197 | -M List the available printer models.\n\ |
| 198 | -h Print this help message.\n\ |
| 199 | Options:\n\ |
| 200 | -P Specify the name of the printer queue to operate on.\n\ |
| 201 | Default is the default system printer.\n\ |
| 202 | -r Specify the name of the device to write to directly\n\ |
| 203 | rather than going through a printer queue.\n\ |
| 204 | -u The printer is a new printer (Stylus Color 740 or newer).\n\ |
| 205 | Only needed when not using a raw device.\n\ |
| 206 | -q Suppress the banner.\n\ |
| 207 | -S Print the short name of the printer with -d.\n\ |
| 208 | -m Specify the precise printer model for head alignment.\n\ |
| 209 | -C Specify the number of pattern choices for alignment\n\ |
| 210 | -p Specify the number of sets of patterns for alignment\n"); |
| 211 | #endif |
| 212 | |
| 213 | char *the_printer = NULL; |
| 214 | char *raw_device = NULL; |
| 215 | char *printer_model = NULL; |
| 216 | char printer_cmd[1025]; |
| 217 | int bufpos = 0; |
| 218 | int isnew = 0; |
| 219 | int interchangeable_inks = 0; |
| 220 | int found_unknown_old_printer = 0; |
| 221 | int print_short_name = 0; |
| 222 | const stp_printer_t *the_printer_t = NULL; |
| 223 | int printer_was_in_packet_mode = 0; |
| 224 | int alignment_passes = 0; |
| 225 | int alignment_choices = 0; |
| 226 | |
| 227 | static int stp_debug = 0; |
| 228 | #define STP_DEBUG(x) do { if (stp_debug || getenv("STP_DEBUG")) x; } while (0) |
| 229 | static int send_size = 0x0200; |
| 230 | static int receive_size = 0x0200; |
| 231 | int socket_id = -1; |
| 232 | |
| 233 | typedef enum |
| 234 | { |
| 235 | CMD_INK_LEVEL, |
| 236 | CMD_STATUS |
| 237 | } status_cmd_t; |
| 238 | |
| 239 | static char * |
| 240 | c_strdup(const char *s) |
| 241 | { |
| 242 | char *ret = stp_malloc(strlen(s) + 1); |
| 243 | strcpy(ret, s); |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | static void |
| 248 | print_models(void) |
| 249 | { |
| 250 | int printer_count = stp_printer_model_count(); |
| 251 | int i; |
| 252 | for (i = 0; i < printer_count; i++) |
| 253 | { |
| 254 | const stp_printer_t *printer = stp_get_printer_by_index(i); |
| 255 | if (strcmp(stp_printer_get_family(printer), "escp2") == 0) |
| 256 | { |
| 257 | printf("%-15s %s\n", stp_printer_get_driver(printer), |
| 258 | gettext(stp_printer_get_long_name(printer))); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | void |
| 264 | do_help(int code) |
| 265 | { |
| 266 | printf("%s", gettext(help_msg)); |
| 267 | exit(code); |
| 268 | } |
| 269 | |
| 270 | static void |
| 271 | exit_packet_mode_old(int do_init) |
| 272 | { |
| 273 | static char hdr[] = "\000\000\000\033\001@EJL 1284.4\n@EJL \n\033@"; |
| 274 | memcpy(printer_cmd + bufpos, hdr, sizeof(hdr) - 1); /* DON'T include null! */ |
| 275 | STP_DEBUG(printf("***Exit packet mode (%d)\n", do_init)); |
| 276 | bufpos += sizeof(hdr) - 1; |
| 277 | if (!do_init) |
| 278 | bufpos -= 2; |
| 279 | } |
| 280 | |
| 281 | static void |
| 282 | initialize_print_cmd(int do_init) |
| 283 | { |
| 284 | bufpos = 0; |
| 285 | STP_DEBUG(printf("***Initialize print command\n")); |
| 286 | if (isnew) |
| 287 | exit_packet_mode_old(do_init); |
| 288 | } |
| 289 | |
| 290 | static void |
| 291 | initialize_print_cmd_new(int do_init) |
| 292 | { |
| 293 | bufpos = 0; |
| 294 | STP_DEBUG(printf("***Initialize print command (force new)\n")); |
| 295 | exit_packet_mode_old(do_init); |
| 296 | } |
| 297 | |
| 298 | int |
| 299 | main(int argc, char **argv) |
| 300 | { |
| 301 | int quiet = 0; |
| 302 | int operation = 0; |
| 303 | int c; |
| 304 | |
| 305 | /* Set up gettext */ |
| 306 | #ifdef HAVE_LOCALE_H |
| 307 | (void) setlocale (LC_ALL, ""); |
| 308 | #endif |
| 309 | #ifdef ENABLE_NLS |
| 310 | bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR); |
| 311 | #endif |
| 312 | |
| 313 | stp_init(); |
| 314 | if (getenv("STP_DEBUG")) |
| 315 | stp_debug = 1; |
| 316 | else |
| 317 | stp_debug = 0; |
| 318 | setDebug(stp_debug); |
| 319 | while (1) |
| 320 | { |
| 321 | #if defined(HAVE_GETOPT_H) && defined(HAVE_GETOPT_LONG) |
| 322 | int option_index = 0; |
| 323 | c = getopt_long(argc, argv, "P:r:iecnasXduqm:hlMSC:p:", optlist, &option_index); |
| 324 | #else |
| 325 | c = getopt(argc, argv, "P:r:iecnasXduqm:hlMSC:p:"); |
| 326 | #endif |
| 327 | if (c == -1) |
| 328 | break; |
| 329 | switch (c) |
| 330 | { |
| 331 | case 'q': |
| 332 | quiet = 1; |
| 333 | break; |
| 334 | case 'c': |
| 335 | case 'i': |
| 336 | case 'e': |
| 337 | case 'n': |
| 338 | case 'a': |
| 339 | case 'd': |
| 340 | case 's': |
| 341 | case 'o': |
| 342 | case 'X': |
| 343 | if (operation) |
| 344 | do_help(1); |
| 345 | operation = c; |
| 346 | break; |
| 347 | case 'P': |
| 348 | if (the_printer || raw_device) |
| 349 | { |
| 350 | printf(_("You may only specify one printer or raw device.")); |
| 351 | do_help(1); |
| 352 | } |
| 353 | the_printer = c_strdup(optarg); |
| 354 | break; |
| 355 | case 'r': |
| 356 | if (the_printer || raw_device) |
| 357 | { |
| 358 | printf(_("You may only specify one printer or raw device.")); |
| 359 | do_help(1); |
| 360 | } |
| 361 | raw_device = c_strdup(optarg); |
| 362 | break; |
| 363 | case 'm': |
| 364 | if (printer_model) |
| 365 | { |
| 366 | printf(_("You may only specify one printer model.")); |
| 367 | do_help(1); |
| 368 | } |
| 369 | printer_model = c_strdup(optarg); |
| 370 | break; |
| 371 | case 'u': |
| 372 | isnew = 1; |
| 373 | break; |
| 374 | case 'h': |
| 375 | do_help(0); |
| 376 | break; |
| 377 | case 'l': |
| 378 | printf("%s\n", gettext(license)); |
| 379 | exit(0); |
| 380 | case 'M': |
| 381 | print_models(); |
| 382 | exit(0); |
| 383 | case 'S': |
| 384 | print_short_name = 1; |
| 385 | break; |
| 386 | case 'C': |
| 387 | alignment_choices = atoi(optarg); |
| 388 | if (alignment_choices < 1) |
| 389 | { |
| 390 | printf(_("Alignment choices must be at least 1.")); |
| 391 | do_help(1); |
| 392 | } |
| 393 | break; |
| 394 | case 'p': |
| 395 | alignment_passes = atoi(optarg); |
| 396 | if (alignment_passes < 1) |
| 397 | { |
| 398 | printf(_("Alignment passes must be at least 1.")); |
| 399 | do_help(1); |
| 400 | } |
| 401 | break; |
| 402 | default: |
| 403 | printf("%s\n", gettext(banner)); |
| 404 | fprintf(stderr, _("Unknown option %c\n"), c); |
| 405 | do_help(1); |
| 406 | } |
| 407 | } |
| 408 | if (!quiet) |
| 409 | printf("%s\n", banner); |
| 410 | if (operation == 0) |
| 411 | { |
| 412 | fprintf(stderr, _("Usage: %s [OPTIONS] command\n"), argv[0]); |
| 413 | #ifdef __GNU_LIBRARY__ |
| 414 | fprintf(stderr, _("Type `%s --help' for more information.\n"), argv[0]); |
| 415 | #else |
| 416 | fprintf(stderr, _("Type `%s -h' for more information.\n"), argv[0]); |
| 417 | #endif |
| 418 | exit(1); |
| 419 | } |
| 420 | initialize_print_cmd(1); |
| 421 | switch(operation) |
| 422 | { |
| 423 | case 'c': |
| 424 | do_head_clean(); |
| 425 | break; |
| 426 | case 'n': |
| 427 | do_nozzle_check(); |
| 428 | break; |
| 429 | case 'i': |
| 430 | do_ink_level(); |
| 431 | break; |
| 432 | case 'e': |
| 433 | do_extended_ink_info(); |
| 434 | break; |
| 435 | case 'a': |
| 436 | do_align(); |
| 437 | break; |
| 438 | case 'd': |
| 439 | do_identify(); |
| 440 | break; |
| 441 | case 's': |
| 442 | do_status(); |
| 443 | break; |
| 444 | case 'X': |
| 445 | do_reset_ink_levels(); |
| 446 | break; |
| 447 | default: |
| 448 | do_help(1); |
| 449 | } |
| 450 | exit(0); |
| 451 | } |
| 452 | |
| 453 | static void |
| 454 | print_debug_data(const char *buf, size_t count) |
| 455 | { |
| 456 | size_t i; |
| 457 | for (i = 0; i < count; i++) |
| 458 | { |
| 459 | if (i % 16 == 0) |
| 460 | printf("\n***%4zu: ", i); |
| 461 | else if (i % 4 == 0) |
| 462 | printf(" "); |
| 463 | if (isgraph(buf[i])) |
| 464 | printf(" %c", (unsigned) ((unsigned char) buf[i])); |
| 465 | else |
| 466 | printf(" %02x", (unsigned) ((unsigned char) buf[i])); |
| 467 | } |
| 468 | printf("\n"); |
| 469 | } |
| 470 | |
| 471 | int |
| 472 | do_print_cmd(void) |
| 473 | { |
| 474 | FILE *pfile; |
| 475 | int bytes = 0; |
| 476 | int retries = 0; |
| 477 | char command[1024]; |
| 478 | memcpy(printer_cmd + bufpos, "\f\033\000\033\000", 5); |
| 479 | bufpos += 5; |
| 480 | if (raw_device) |
| 481 | { |
| 482 | pfile = fopen(raw_device, "wb"); |
| 483 | if (!pfile) |
| 484 | { |
| 485 | fprintf(stderr, _("Cannot open device %s: %s\n"), raw_device, |
| 486 | strerror(errno)); |
| 487 | return 1; |
| 488 | } |
| 489 | } |
| 490 | else |
| 491 | { |
| 492 | if (!access("/bin/lpr", X_OK) || |
| 493 | !access("/usr/bin/lpr", X_OK) || |
| 494 | !access("/usr/bsd/lpr", X_OK)) |
| 495 | { |
| 496 | if (the_printer == NULL) |
| 497 | strcpy(command, "lpr -l"); |
| 498 | else |
| 499 | snprintf(command, 1023, "lpr -P%s -l", the_printer); |
| 500 | } |
| 501 | else if (the_printer == NULL) |
| 502 | strcpy(command, "lp -s -oraw"); |
| 503 | else |
| 504 | snprintf(command, 1023, "lp -s -oraw -d%s", the_printer); |
| 505 | |
| 506 | if ((pfile = popen(command, "w")) == NULL) |
| 507 | { |
| 508 | fprintf(stderr, _("Cannot print to printer %s with %s\n"), |
| 509 | the_printer, command); |
| 510 | return 1; |
| 511 | } |
| 512 | } |
| 513 | STP_DEBUG(printf("***Sending print command to %s:", |
| 514 | raw_device ? raw_device : command)); |
| 515 | STP_DEBUG(print_debug_data(printer_cmd, bufpos)); |
| 516 | while (bytes < bufpos) |
| 517 | { |
| 518 | int status = fwrite(printer_cmd + bytes, 1, bufpos - bytes, pfile); |
| 519 | if (status == 0) |
| 520 | { |
| 521 | retries++; |
| 522 | if (retries > 2) |
| 523 | { |
| 524 | fprintf(stderr, _("Unable to send command to printer\n")); |
| 525 | if (raw_device) |
| 526 | fclose(pfile); |
| 527 | else |
| 528 | pclose(pfile); |
| 529 | return 1; |
| 530 | } |
| 531 | } |
| 532 | else if (status == -1) |
| 533 | { |
| 534 | fprintf(stderr, _("Unable to send command to printer\n")); |
| 535 | if (raw_device) |
| 536 | fclose(pfile); |
| 537 | else |
| 538 | pclose(pfile); |
| 539 | return 1; |
| 540 | } |
| 541 | else |
| 542 | { |
| 543 | bytes += status; |
| 544 | retries = 0; |
| 545 | } |
| 546 | } |
| 547 | if (raw_device) |
| 548 | fclose(pfile); |
| 549 | else |
| 550 | pclose(pfile); |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | static int |
| 555 | read_from_printer(int fd, char *buf, int bufsize, int quiet) |
| 556 | { |
| 557 | #ifdef HAVE_POLL |
| 558 | struct pollfd ufds; |
| 559 | #endif |
| 560 | int status; |
| 561 | int retry = 100; |
| 562 | #ifdef HAVE_FCNTL_H |
| 563 | fcntl(fd, F_SETFL, |
| 564 | O_NONBLOCK | (fcntl(fd, F_GETFL) & ~(O_RDONLY|O_WRONLY|O_RDWR))); |
| 565 | #endif |
| 566 | memset(buf, 0, bufsize); |
| 567 | |
| 568 | do |
| 569 | { |
| 570 | #ifdef HAVE_POLL |
| 571 | ufds.fd = fd; |
| 572 | ufds.events = POLLIN; |
| 573 | ufds.revents = 0; |
| 574 | if ((status = poll(&ufds, 1, 1000)) < 0) |
| 575 | break; |
| 576 | #endif |
| 577 | status = read(fd, buf, bufsize - 1); |
| 578 | if (status == 0 || (status < 0 && errno == EAGAIN)) |
| 579 | { |
| 580 | struct timespec tm = {0, 2000000}; |
| 581 | nanosleep(&tm, &tm); |
| 582 | status = 0; /* not an error (read would have blocked) */ |
| 583 | } |
| 584 | } |
| 585 | while ((status == 0) && (--retry != 0)); |
| 586 | |
| 587 | if (status > 0) |
| 588 | { |
| 589 | STP_DEBUG(printf("***read_from_printer returns %d\n", status)); |
| 590 | STP_DEBUG(print_debug_data(buf, status)); |
| 591 | } |
| 592 | else if (status == 0 && retry == 0) |
| 593 | { |
| 594 | if (!quiet) |
| 595 | fprintf(stderr, _("Read from printer timed out\n")); |
| 596 | } |
| 597 | else if (status < 0) |
| 598 | { |
| 599 | if (!quiet) |
| 600 | fprintf(stderr, _("Cannot read from %s: %s\n"), raw_device, strerror(errno)); |
| 601 | } |
| 602 | |
| 603 | return status; |
| 604 | } |
| 605 | |
| 606 | static void |
| 607 | start_remote_sequence(void) |
| 608 | { |
| 609 | static char remote_hdr[] = "\033@\033(R\010\000\000REMOTE1"; |
| 610 | memcpy(printer_cmd + bufpos, remote_hdr, sizeof(remote_hdr) - 1); |
| 611 | bufpos += sizeof(remote_hdr) - 1; |
| 612 | STP_DEBUG(printf("***Start remote sequence\n")); |
| 613 | } |
| 614 | |
| 615 | static void |
| 616 | end_remote_sequence(void) |
| 617 | { |
| 618 | static char remote_trailer[] = "\033\000\000\000\033\000"; |
| 619 | memcpy(printer_cmd + bufpos, remote_trailer, sizeof(remote_trailer) - 1); |
| 620 | bufpos += sizeof(remote_trailer) - 1; |
| 621 | STP_DEBUG(printf("***End remote sequence\n")); |
| 622 | } |
| 623 | |
| 624 | static void |
| 625 | do_remote_cmd(const char *cmd, int nargs, ...) |
| 626 | { |
| 627 | int i; |
| 628 | va_list args; |
| 629 | va_start(args, nargs); |
| 630 | |
| 631 | start_remote_sequence(); |
| 632 | memcpy(printer_cmd + bufpos, cmd, 2); |
| 633 | STP_DEBUG(printf("***Remote command: %s", cmd)); |
| 634 | bufpos += 2; |
| 635 | printer_cmd[bufpos] = nargs % 256; |
| 636 | printer_cmd[bufpos + 1] = (nargs >> 8) % 256; |
| 637 | STP_DEBUG(printf(" %02x %02x", |
| 638 | (unsigned) printer_cmd[bufpos], |
| 639 | (unsigned) printer_cmd[bufpos + 1])); |
| 640 | if (nargs > 0) |
| 641 | for (i = 0; i < nargs; i++) |
| 642 | { |
| 643 | printer_cmd[bufpos + 2 + i] = va_arg(args, int); |
| 644 | STP_DEBUG(printf(" %02x", |
| 645 | (unsigned) printer_cmd[bufpos + 2 + i])); |
| 646 | } |
| 647 | STP_DEBUG(printf("\n")); |
| 648 | bufpos += 2 + nargs; |
| 649 | end_remote_sequence(); |
| 650 | va_end(args); |
| 651 | } |
| 652 | |
| 653 | static void |
| 654 | do_remote_cmd_only(const char *cmd, int nargs, ...) |
| 655 | { |
| 656 | int i; |
| 657 | va_list args; |
| 658 | va_start(args, nargs); |
| 659 | |
| 660 | memcpy(printer_cmd + bufpos, cmd, 2); |
| 661 | STP_DEBUG(printf("***Remote command: %s", cmd)); |
| 662 | bufpos += 2; |
| 663 | printer_cmd[bufpos] = nargs % 256; |
| 664 | printer_cmd[bufpos + 1] = (nargs >> 8) % 256; |
| 665 | STP_DEBUG(printf(" %02x %02x", |
| 666 | (unsigned) printer_cmd[bufpos], |
| 667 | (unsigned) printer_cmd[bufpos + 1])); |
| 668 | if (nargs > 0) |
| 669 | for (i = 0; i < nargs; i++) |
| 670 | { |
| 671 | printer_cmd[bufpos + 2 + i] = va_arg(args, int); |
| 672 | STP_DEBUG(printf(" %02x", |
| 673 | (unsigned) printer_cmd[bufpos + 2 + i])); |
| 674 | } |
| 675 | STP_DEBUG(printf("\n")); |
| 676 | bufpos += 2 + nargs; |
| 677 | va_end(args); |
| 678 | } |
| 679 | |
| 680 | static void |
| 681 | add_resets(int count) |
| 682 | { |
| 683 | int i; |
| 684 | STP_DEBUG(printf("***Add %d resets\n", count)); |
| 685 | for (i = 0; i < count; i++) |
| 686 | { |
| 687 | printer_cmd[bufpos++] = '\033'; |
| 688 | printer_cmd[bufpos++] = '\000'; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | static void |
| 693 | send_nulls(int fd) |
| 694 | { |
| 695 | char buf[16384]; |
| 696 | (void) memset(buf, 0, sizeof(buf)); |
| 697 | (void) write(fd, buf, sizeof(buf)); |
| 698 | } |
| 699 | |
| 700 | static int |
| 701 | init_packet(int fd, int force) |
| 702 | { |
| 703 | int tries = 0; |
| 704 | STP_DEBUG(printf("***Init packet mode %d\n", force)); |
| 705 | |
| 706 | Loop: |
| 707 | if (!force) |
| 708 | { |
| 709 | STP_DEBUG(printf("***Flushing early data...\n")); |
| 710 | flushData(fd, (unsigned char) -1); |
| 711 | } |
| 712 | |
| 713 | STP_DEBUG(printf("***EnterIEEE...\n")); |
| 714 | if (!EnterIEEE(fd)) |
| 715 | { |
| 716 | STP_DEBUG(printf("***EnterIEEE failed!\n")); |
| 717 | if (tries++ < 5) |
| 718 | { |
| 719 | STP_DEBUG(printf("***Retrying\n")); |
| 720 | send_nulls(fd); |
| 721 | flushData(fd, (unsigned char) -1); |
| 722 | goto Loop; |
| 723 | } |
| 724 | return 1; |
| 725 | } |
| 726 | STP_DEBUG(printf("***Init...\n")); |
| 727 | if (!Init(fd)) |
| 728 | { |
| 729 | STP_DEBUG(printf("***Init failed!\n")); |
| 730 | if (tries++ < 5) |
| 731 | { |
| 732 | STP_DEBUG(printf("***Retrying\n")); |
| 733 | send_nulls(fd); |
| 734 | flushData(fd, (unsigned char) -1); |
| 735 | goto Loop; |
| 736 | } |
| 737 | return 1; |
| 738 | } |
| 739 | |
| 740 | STP_DEBUG(printf("***GetSocket...\n")); |
| 741 | socket_id = GetSocketID(fd, "EPSON-CTRL"); |
| 742 | if (!socket_id) |
| 743 | { |
| 744 | STP_DEBUG(printf("***GetSocket failed!\n")); |
| 745 | return 1; |
| 746 | } |
| 747 | STP_DEBUG(printf("***OpenChannel...\n")); |
| 748 | switch ( OpenChannel(fd, socket_id, &send_size, &receive_size) ) |
| 749 | { |
| 750 | case -1: |
| 751 | STP_DEBUG(printf("***Fatal Error return 1\n")); |
| 752 | return 1; /* unrecoverable error */ |
| 753 | break; |
| 754 | case 0: |
| 755 | STP_DEBUG(printf("***Error\n")); /* recoverable error ? */ |
| 756 | return 1; |
| 757 | break; |
| 758 | } |
| 759 | |
| 760 | STP_DEBUG(printf("***Flushing data...\n")); |
| 761 | flushData(fd, socket_id); |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | static volatile int alarm_interrupt; |
| 766 | |
| 767 | static void |
| 768 | alarm_handler(int sig) |
| 769 | { |
| 770 | (void)sig; |
| 771 | alarm_interrupt = 1; |
| 772 | } |
| 773 | |
| 774 | static int |
| 775 | open_raw_device(void) |
| 776 | { |
| 777 | int fd; |
| 778 | |
| 779 | if (!raw_device) |
| 780 | { |
| 781 | fprintf(stderr, _("Please specify a raw device\n")); |
| 782 | exit(1); |
| 783 | } |
| 784 | |
| 785 | fd = open(raw_device, O_RDWR, 0666); |
| 786 | if (fd == -1) |
| 787 | { |
| 788 | fprintf(stderr, _("Cannot open %s read/write: %s\n"), raw_device, |
| 789 | strerror(errno)); |
| 790 | exit(1); |
| 791 | } |
| 792 | |
| 793 | return fd; |
| 794 | } |
| 795 | |
| 796 | static void |
| 797 | set_printer_model(void) |
| 798 | { |
| 799 | int i = 0; |
| 800 | int printer_count = stp_printer_model_count(); |
| 801 | for (i = 0; i < printer_count; i++) |
| 802 | { |
| 803 | the_printer_t = stp_get_printer_by_index(i); |
| 804 | |
| 805 | if (strcmp(stp_printer_get_family(the_printer_t), "escp2") == 0) |
| 806 | { |
| 807 | const char *short_name = stp_printer_get_driver(the_printer_t); |
| 808 | const char *long_name = stp_printer_get_long_name(the_printer_t); |
| 809 | if (!strcasecmp(printer_model, short_name) || |
| 810 | !strcasecmp(printer_model, long_name) || |
| 811 | (!strncmp(short_name, "escp2-", strlen("escp2-")) && |
| 812 | !strcasecmp(printer_model, short_name + strlen("escp2-"))) || |
| 813 | (!strncasecmp(long_name, "Epson ", strlen("Epson ")) && |
| 814 | !strcasecmp(printer_model, long_name + strlen("Epson "))) || (!strncasecmp(long_name, "Fujifilm ", strlen("Fujifilm ")) && |
| 815 | !strcasecmp(printer_model, long_name + strlen("Fujifilm ")))) |
| 816 | { |
| 817 | const stp_vars_t *printvars; |
| 818 | stp_parameter_t desc; |
| 819 | |
| 820 | printvars = stp_printer_get_defaults(the_printer_t); |
| 821 | stp_describe_parameter(printvars, "SupportsPacketMode", |
| 822 | &desc); |
| 823 | if (desc.p_type == STP_PARAMETER_TYPE_BOOLEAN) |
| 824 | isnew = desc.deflt.boolean; |
| 825 | stp_parameter_description_destroy(&desc); |
| 826 | stp_describe_parameter(printvars, "InterchangeableInk", |
| 827 | &desc); |
| 828 | if (desc.p_type == STP_PARAMETER_TYPE_BOOLEAN) |
| 829 | interchangeable_inks = desc.deflt.boolean; |
| 830 | stp_parameter_description_destroy(&desc); |
| 831 | STP_DEBUG(printf("***Found it! %s\n", printer_model)); |
| 832 | return; |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | fprintf(stderr, _("Unknown printer %s!\n"), printer_model); |
| 837 | the_printer_t = NULL; |
| 838 | } |
| 839 | |
| 840 | static int |
| 841 | test_for_di(const unsigned char *buf) |
| 842 | { |
| 843 | return !(strncmp("di", (const char *)buf, 2) && |
| 844 | strncmp("@EJL ID", (const char *)buf, 7)); |
| 845 | } |
| 846 | |
| 847 | static int |
| 848 | test_for_st(const unsigned char *buf) |
| 849 | { |
| 850 | return !(strncmp("st", (const char *)buf, 2) && |
| 851 | strncmp("@BDC ST", (const char *)buf, 7)); |
| 852 | } |
| 853 | |
| 854 | static int |
| 855 | test_for_ii(const unsigned char *buf) |
| 856 | { |
| 857 | return !(strncmp("ii", (const char *)buf, 2) && |
| 858 | strncmp("@BDC PS", (const char *)buf, 7)); |
| 859 | } |
| 860 | |
| 861 | static int |
| 862 | test_for_ri(const unsigned char *buf) |
| 863 | { |
| 864 | return !(strncmp("ri:00:OK;", (const char *)buf, 9)); |
| 865 | } |
| 866 | |
| 867 | static const stp_printer_t * |
| 868 | initialize_printer(int quiet, int fail_if_not_found) |
| 869 | { |
| 870 | int packet_initialized = 0; |
| 871 | int fd; |
| 872 | int tries = 0; |
| 873 | int status; |
| 874 | int forced_packet_mode = 0; |
| 875 | char* pos; |
| 876 | char* spos; |
| 877 | unsigned char buf[1024]; |
| 878 | const char init_str[] = "\033\1@EJL ID\r\n"; |
| 879 | |
| 880 | quiet = 0; |
| 881 | |
| 882 | fd = open_raw_device(); |
| 883 | |
| 884 | if (!printer_model) |
| 885 | { |
| 886 | do |
| 887 | { |
| 888 | alarm_interrupt = 0; |
| 889 | signal(SIGALRM, alarm_handler); |
| 890 | alarm(5); |
| 891 | status = SafeWrite(fd, init_str, sizeof(init_str) - 1); |
| 892 | alarm(0); |
| 893 | signal(SIGALRM, SIG_DFL); |
| 894 | STP_DEBUG(printf("***status %d alarm %d\n", status, alarm_interrupt)); |
| 895 | if (status != sizeof(init_str) - 1 && (status != -1 || !alarm_interrupt)) |
| 896 | { |
| 897 | fprintf(stderr, _("Cannot write to %s: %s\n"), raw_device, |
| 898 | strerror(errno)); |
| 899 | exit(1); |
| 900 | } |
| 901 | STP_DEBUG(printf("***Try old command %d alarm %d\n", |
| 902 | tries, alarm_interrupt)); |
| 903 | status = read_from_printer(fd, (char*)buf, 1024, 1); |
| 904 | if (status <= 0 && tries > 0) |
| 905 | { |
| 906 | forced_packet_mode = !init_packet(fd, 1); |
| 907 | status = 1; |
| 908 | } |
| 909 | if (!forced_packet_mode && |
| 910 | status > 0 && !strstr((char *) buf, "@EJL ID") && tries < 3) |
| 911 | { |
| 912 | STP_DEBUG(printf("***Found bad data: %s\n", buf)); |
| 913 | /* |
| 914 | * We know the printer's not dead. Try to turn off status |
| 915 | * and try again. |
| 916 | */ |
| 917 | initialize_print_cmd(1); |
| 918 | do_remote_cmd("ST", 2, 0, 0); |
| 919 | add_resets(2); |
| 920 | (void) SafeWrite(fd, printer_cmd, bufpos); |
| 921 | status = 0; |
| 922 | } |
| 923 | tries++; |
| 924 | } while (status <= 0); |
| 925 | |
| 926 | if (forced_packet_mode || ((buf[3] == status) && (buf[6] == 0x7f))) |
| 927 | { |
| 928 | /* |
| 929 | * NX100 looks like it's in D4 mode, but it doesn't really |
| 930 | * respond correctly. So we force it out of D4 mode and |
| 931 | * then back in to ensure that it's right. Trying to force |
| 932 | * it into D4 mode alone isn't good enough. |
| 933 | */ |
| 934 | initialize_print_cmd_new(0); |
| 935 | (void) SafeWrite(fd, printer_cmd, bufpos); |
| 936 | flushData(fd, (unsigned char) -1); |
| 937 | forced_packet_mode = !init_packet(fd, 1); |
| 938 | STP_DEBUG(printf("***Printer in packet mode....\n")); |
| 939 | packet_initialized = 1; |
| 940 | isnew = 1; |
| 941 | /* request status command */ |
| 942 | status = |
| 943 | writeAndReadData(fd, socket_id, (const unsigned char*)"di\1\0\1", |
| 944 | 5, 1, (unsigned char *) buf, 1023, |
| 945 | &send_size, &receive_size, &test_for_di); |
| 946 | if (status <= 0) |
| 947 | { |
| 948 | fprintf(stderr, _("\nCannot write to %s: %s\n"), |
| 949 | raw_device, strerror(errno)); |
| 950 | close(fd); |
| 951 | return NULL; |
| 952 | } |
| 953 | } |
| 954 | STP_DEBUG(printf("***status: %i\n", status)); |
| 955 | STP_DEBUG(printf("***Buf: %s\n", buf)); |
| 956 | if (status > 0) |
| 957 | { |
| 958 | char *xpos; |
| 959 | pos = strstr((char*)buf, "@EJL ID"); |
| 960 | STP_DEBUG(printf("***pos: %s\n", pos ? pos : "(null)")); |
| 961 | if (pos) |
| 962 | { |
| 963 | if (((xpos = strstr((char *)pos, "MDL:")) != NULL) || |
| 964 | ((xpos = strstr((char *)pos, "DES:")) != NULL)) |
| 965 | { |
| 966 | STP_DEBUG(printf("***xpos: %s\n", xpos ? xpos : "(null)")); |
| 967 | pos = xpos + 4; |
| 968 | STP_DEBUG(printf("***pos: %s\n", pos ? pos : "(null)")); |
| 969 | } |
| 970 | else |
| 971 | { |
| 972 | if (pos) |
| 973 | pos = strchr(pos, (int) ';'); |
| 974 | STP_DEBUG(printf("***pos: %s\n", pos ? pos : "(null)")); |
| 975 | if (pos) |
| 976 | pos = strchr(pos + 1, (int) ';'); |
| 977 | STP_DEBUG(printf("***pos: %s\n", pos ? pos : "(null)")); |
| 978 | if (pos) |
| 979 | pos = strchr(pos, (int) ':'); |
| 980 | STP_DEBUG(printf("***pos: %s\n", pos ? pos : "(null)")); |
| 981 | if (pos) |
| 982 | pos++; |
| 983 | } |
| 984 | if (pos) |
| 985 | spos = strchr(pos, (int) ';'); |
| 986 | } |
| 987 | if (!pos) |
| 988 | { |
| 989 | if (!quiet && fail_if_not_found) |
| 990 | { |
| 991 | fprintf(stderr, _("\nCannot detect printer type.\n" |
| 992 | "Please use -m to specify your printer model.\n")); |
| 993 | do_help(1); |
| 994 | } |
| 995 | /* |
| 996 | * Some printers seem to return status, but don't respond |
| 997 | * usefully to @EJL ID. The Stylus Pro 7500 seems to be |
| 998 | * one of these. |
| 999 | */ |
| 1000 | if (status > 0 && !fail_if_not_found && |
| 1001 | strstr((char *) buf, "@BDC ST")) |
| 1002 | { |
| 1003 | found_unknown_old_printer = 1; |
| 1004 | /* |
| 1005 | * Set the printer model to something rational so that |
| 1006 | * attempts to describe parameters will succeed. |
| 1007 | * However, make it clear that this is a dummy, |
| 1008 | * so we don't actually try to print it out. |
| 1009 | */ |
| 1010 | STP_DEBUG(printf("***Can't find printer name, assuming Stylus Photo\n")); |
| 1011 | printer_model = c_strdup("escp2-photo"); |
| 1012 | } |
| 1013 | else |
| 1014 | { |
| 1015 | STP_DEBUG(printf("***Can't get response to @EJL ID\n")); |
| 1016 | close(fd); |
| 1017 | return NULL; |
| 1018 | } |
| 1019 | } |
| 1020 | else |
| 1021 | { |
| 1022 | if (spos) |
| 1023 | *spos = '\000'; |
| 1024 | printer_model = pos; |
| 1025 | STP_DEBUG(printf("***printer model: %s\n", printer_model)); |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | set_printer_model(); |
| 1031 | |
| 1032 | if (isnew && !packet_initialized) |
| 1033 | { |
| 1034 | isnew = !init_packet(fd, 0); |
| 1035 | } |
| 1036 | |
| 1037 | close(fd); |
| 1038 | STP_DEBUG(printf("***new? %s\n", isnew ? "yes" : "no")); |
| 1039 | return the_printer_t; |
| 1040 | } |
| 1041 | |
| 1042 | static const stp_printer_t * |
| 1043 | get_printer(int quiet, int fail_if_not_found) |
| 1044 | { |
| 1045 | if (the_printer_t) |
| 1046 | return the_printer_t; |
| 1047 | else |
| 1048 | { |
| 1049 | const stp_printer_t *printer = |
| 1050 | initialize_printer(quiet, fail_if_not_found); |
| 1051 | STP_DEBUG(printf("***init done, printer found? %s...\n", |
| 1052 | printer ? "yes" : "no")); |
| 1053 | return printer; |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | static const char *colors_new[] = |
| 1058 | { |
| 1059 | N_("Black"), /* 00 */ |
| 1060 | N_("Photo Black"), /* 01 */ |
| 1061 | N_("Unknown"), /* 02 */ |
| 1062 | N_("Cyan"), /* 03 */ |
| 1063 | N_("Magenta"), /* 04 */ |
| 1064 | N_("Yellow"), /* 05 */ |
| 1065 | N_("Light Cyan"), /* 06 */ |
| 1066 | N_("Light Magenta"), /* 07 */ |
| 1067 | N_("Unknown"), /* 08 */ |
| 1068 | N_("Unknown"), /* 09 */ |
| 1069 | N_("Light Black"), /* 0a */ |
| 1070 | N_("Matte Black"), /* 0b */ |
| 1071 | N_("Red"), /* 0c */ |
| 1072 | N_("Blue"), /* 0d */ |
| 1073 | N_("Gloss Optimizer"), /* 0e */ |
| 1074 | N_("Light Light Black"), /* 0f */ |
| 1075 | N_("Orange"), /* 10 */ |
| 1076 | }; |
| 1077 | static int color_count = sizeof(colors_new) / sizeof(const char *); |
| 1078 | |
| 1079 | static const char *aux_colors[] = |
| 1080 | { |
| 1081 | N_("Black"), /* 0 */ |
| 1082 | N_("Cyan"), /* 1 */ |
| 1083 | N_("Magenta"), /* 2 */ |
| 1084 | N_("Yellow"), /* 3 */ |
| 1085 | N_("Light Cyan"), /* 4 */ |
| 1086 | N_("Light Magenta"), /* 5 */ |
| 1087 | NULL, /* 6 */ |
| 1088 | NULL, /* 7 */ |
| 1089 | NULL, /* 8 */ |
| 1090 | N_("Red"), /* 9 */ |
| 1091 | N_("Blue"), /* a */ |
| 1092 | NULL, /* b */ |
| 1093 | NULL, /* c */ |
| 1094 | N_("Orange"), /* d */ |
| 1095 | NULL, /* e */ |
| 1096 | NULL, /* f */ |
| 1097 | NULL, /* 10 */ |
| 1098 | NULL, /* 11 */ |
| 1099 | NULL, /* 12 */ |
| 1100 | N_("Red"), /* 13 */ |
| 1101 | NULL, /* 14 */ |
| 1102 | NULL, /* 15 */ |
| 1103 | N_("Red"), /* 16 */ |
| 1104 | }; |
| 1105 | static int aux_color_count = sizeof(aux_colors) / sizeof(const char *); |
| 1106 | |
| 1107 | static void |
| 1108 | print_status(int param) |
| 1109 | { |
| 1110 | switch (param) |
| 1111 | { |
| 1112 | case 0: |
| 1113 | printf(_("Status: Error\n")); |
| 1114 | break; |
| 1115 | case 1: |
| 1116 | printf(_("Status: Self-Printing\n")); |
| 1117 | break; |
| 1118 | case 2: |
| 1119 | printf(_("Status: Busy\n")); |
| 1120 | break; |
| 1121 | case 3: |
| 1122 | printf(_("Status: Waiting\n")); |
| 1123 | break; |
| 1124 | case 4: |
| 1125 | printf(_("Status: Idle\n")); |
| 1126 | break; |
| 1127 | case 5: |
| 1128 | printf(_("Status: Paused\n")); |
| 1129 | break; |
| 1130 | case 7: |
| 1131 | printf(_("Status: Cleaning\n")); |
| 1132 | break; |
| 1133 | case 8: |
| 1134 | printf(_("Status: Factory shipment\n")); |
| 1135 | break; |
| 1136 | case 9: |
| 1137 | printf(_("Status: Sleeping\n")); /* Lower power mode */ |
| 1138 | break; |
| 1139 | case 0xa: |
| 1140 | printf(_("Status: Shutting down\n")); |
| 1141 | break; |
| 1142 | case 0xc: |
| 1143 | printf(_("Status: Initializing paper\n")); |
| 1144 | break; |
| 1145 | case 0xd: |
| 1146 | printf(_("Status: Black ink switching\n")); |
| 1147 | break; |
| 1148 | case 0xf: |
| 1149 | printf(_("Status: Initializing ink\n")); |
| 1150 | break; |
| 1151 | default: |
| 1152 | printf(_("Status: Unknown (%d)\n"), param); |
| 1153 | break; |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | static void |
| 1158 | print_error(int param) |
| 1159 | { |
| 1160 | switch (param) |
| 1161 | { |
| 1162 | case 0: |
| 1163 | printf(_("Error: Fatal Error\n")); |
| 1164 | break; |
| 1165 | case 1: |
| 1166 | printf(_("Error: Other interface is selected\n")); |
| 1167 | break; |
| 1168 | case 2: |
| 1169 | printf(_("Error: Cover Open\n")); |
| 1170 | break; |
| 1171 | case 4: |
| 1172 | printf(_("Error: Paper jam\n")); |
| 1173 | break; |
| 1174 | case 5: |
| 1175 | printf(_("Error: Ink out\n")); |
| 1176 | break; |
| 1177 | case 6: |
| 1178 | printf(_("Error: Paper out\n")); |
| 1179 | break; |
| 1180 | case 0xb: |
| 1181 | printf(_("Error: Double feed\n")); |
| 1182 | break; |
| 1183 | case 0xc: |
| 1184 | printf(_("Error: Miscellaneous paper error\n")); |
| 1185 | break; |
| 1186 | case 0x10: |
| 1187 | printf(_("Error: Maintenance cartridge overflow\n")); |
| 1188 | break; |
| 1189 | case 0x11: |
| 1190 | printf(_("Error: Wait return from the tear-off position\n")); |
| 1191 | break; |
| 1192 | case 0x12: |
| 1193 | printf(_("Error: Double feed error\n")); |
| 1194 | break; |
| 1195 | case 0x16: |
| 1196 | printf(_("Error: Cleaning impossible\n")); |
| 1197 | break; |
| 1198 | case 0x1a: |
| 1199 | printf(_("Error: Ink cartridge lever released\n\n")); |
| 1200 | break; |
| 1201 | case 0x1c: |
| 1202 | printf(_("Error: Unrecoverable cutter error\n")); |
| 1203 | break; |
| 1204 | case 0x1d: |
| 1205 | printf(_("Error: Recoverable cutter jam\n")); |
| 1206 | break; |
| 1207 | case 0x22: |
| 1208 | printf(_("Error: No maintenance cartridge present\n")); |
| 1209 | break; |
| 1210 | case 0x25: |
| 1211 | printf(_("Error: Rear cover open\n")); |
| 1212 | break; |
| 1213 | case 0x29: |
| 1214 | printf(_("Error: CD Tray Out\n")); |
| 1215 | break; |
| 1216 | case 0x2a: |
| 1217 | printf(_("Error: Card loading error\n")); |
| 1218 | break; |
| 1219 | case 0x2b: |
| 1220 | printf(_("Error: Tray cover open\n")); |
| 1221 | break; |
| 1222 | case 0x2d: |
| 1223 | printf(_("Error: No maintenance box\n")); |
| 1224 | break; |
| 1225 | case 0x2e: |
| 1226 | printf(_("Error: Maintenance box overflow\n")); |
| 1227 | break; |
| 1228 | case 0x36: |
| 1229 | printf(_("Error: Maintenance cartridge cover open\n")); |
| 1230 | break; |
| 1231 | case 0x37: |
| 1232 | printf(_("Error: Front cover open\n")); |
| 1233 | break; |
| 1234 | case 0x40: |
| 1235 | printf(_("Error: Strip bin missing\n")); |
| 1236 | break; |
| 1237 | case 0x41: |
| 1238 | printf(_("Error: Maintenance request\n")); |
| 1239 | break; |
| 1240 | default: |
| 1241 | printf(_("Error: Unknown (%d)\n"), param); |
| 1242 | break; |
| 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | static void |
| 1247 | print_warning(unsigned long param, const stp_string_list_t *color_list) |
| 1248 | { |
| 1249 | if (param >= 0x10 && param < 0x20) |
| 1250 | { |
| 1251 | param &= 0xf; |
| 1252 | if (color_list && param < stp_string_list_count(color_list)) |
| 1253 | printf(_("Warning: %s Ink Low\n"), |
| 1254 | gettext(stp_string_list_param(color_list, param)->text)); |
| 1255 | else |
| 1256 | printf(_("Warning: Channel %lu Ink Low\n"), param); |
| 1257 | } |
| 1258 | else if (param >= 0x50 && param < 0x60) |
| 1259 | { |
| 1260 | param &= 0xf; |
| 1261 | if (color_list && param < stp_string_list_count(color_list)) |
| 1262 | printf(_("Warning: %s Cleaning Disabled\n"), |
| 1263 | gettext(stp_string_list_param(color_list, param)->text)); |
| 1264 | else |
| 1265 | printf(_("Warning: Channel %lu Cleaning \n"), param); |
| 1266 | } |
| 1267 | else |
| 1268 | { |
| 1269 | switch (param) |
| 1270 | { |
| 1271 | case 0x20: |
| 1272 | printf(_("Warning: Maintenance cartridge near full\n")); |
| 1273 | break; |
| 1274 | case 0x21: |
| 1275 | printf(_("Warning: Maintenance request pending\n")); |
| 1276 | break; |
| 1277 | default: |
| 1278 | printf(_("Warning: Unknown (%lu)\n"), param); |
| 1279 | break; |
| 1280 | } |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | static void |
| 1285 | print_self_printing_state(int param) |
| 1286 | { |
| 1287 | switch (param) |
| 1288 | { |
| 1289 | case 0: |
| 1290 | printf(_("Printing nozzle self-test\n")); |
| 1291 | break; |
| 1292 | default: |
| 1293 | break; |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | static const char * |
| 1298 | looking_at_command(const char *buf, const char *cmd) |
| 1299 | { |
| 1300 | if (!strncmp(buf, cmd, strlen(cmd))) |
| 1301 | { |
| 1302 | const char *answer = buf + strlen(cmd); |
| 1303 | if (answer[0] == ':') |
| 1304 | return (answer + 1); |
| 1305 | } |
| 1306 | return NULL; |
| 1307 | } |
| 1308 | |
| 1309 | static const char * |
| 1310 | find_group(const char *buf) |
| 1311 | { |
| 1312 | while (buf[0] == ';') |
| 1313 | buf++; |
| 1314 | buf = strchr(buf, ';'); |
| 1315 | if (buf && buf[1]) |
| 1316 | return buf + 1; |
| 1317 | else |
| 1318 | return NULL; |
| 1319 | } |
| 1320 | |
| 1321 | static int |
| 1322 | get_digit(char digit) |
| 1323 | { |
| 1324 | if (digit >= '0' && digit <= '9') |
| 1325 | return digit - '0'; |
| 1326 | else if (digit >= 'A' && digit <= 'F') |
| 1327 | return digit - 'A' + 10; |
| 1328 | else if (digit >= 'a' && digit <= 'f') |
| 1329 | return digit - 'a' + 10; |
| 1330 | else |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
| 1334 | static void |
| 1335 | print_old_ink_levels(const char *ind, stp_string_list_t *color_list) |
| 1336 | { |
| 1337 | size_t i; |
| 1338 | for (i = 0; i < stp_string_list_count(color_list); i++) |
| 1339 | { |
| 1340 | int val; |
| 1341 | if (!ind[0] || ind[0] == ';') |
| 1342 | return; |
| 1343 | val = (get_digit(ind[0]) << 4) + get_digit(ind[1]); |
| 1344 | printf("%20s %20d\n", |
| 1345 | gettext(stp_string_list_param(color_list, i)->text), val); |
| 1346 | ind += 2; |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | static void |
| 1351 | do_old_status(status_cmd_t cmd, const char *buf, const stp_printer_t *printer) |
| 1352 | { |
| 1353 | if (cmd == CMD_STATUS) |
| 1354 | printf(_("Printer Name: %s\n"), |
| 1355 | printer ? stp_printer_get_long_name(printer) : _("Unknown")); |
| 1356 | do |
| 1357 | { |
| 1358 | const char *ind; |
| 1359 | if (cmd == CMD_STATUS && (ind = looking_at_command(buf, "ST")) != 0) |
| 1360 | print_status(atoi(ind)); |
| 1361 | else if (cmd == CMD_STATUS && |
| 1362 | (ind = looking_at_command(buf, "ER")) != 0) |
| 1363 | print_error(atoi(ind)); |
| 1364 | else if ((ind = looking_at_command(buf, "IQ")) != 0) |
| 1365 | { |
| 1366 | stp_string_list_t *color_list = NULL; |
| 1367 | if (printer) |
| 1368 | { |
| 1369 | stp_parameter_t desc; |
| 1370 | const stp_vars_t *printvars = stp_printer_get_defaults(printer); |
| 1371 | stp_describe_parameter(printvars, "ChannelNames", &desc); |
| 1372 | if (desc.p_type == STP_PARAMETER_TYPE_STRING_LIST) |
| 1373 | { |
| 1374 | color_list = stp_string_list_create_copy(desc.bounds.str); |
| 1375 | STP_DEBUG(printf("***Using color list from driver (%ld %ld)\n", |
| 1376 | (long)stp_string_list_count(desc.bounds.str), |
| 1377 | (long)stp_string_list_count(color_list))); |
| 1378 | stp_parameter_description_destroy(&desc); |
| 1379 | } |
| 1380 | } |
| 1381 | if (!color_list)/* use defaults */ |
| 1382 | { |
| 1383 | stp_string_list_add_string(color_list, "Black", _("Black")); |
| 1384 | stp_string_list_add_string(color_list, "Cyan", _("Cyan")); |
| 1385 | stp_string_list_add_string(color_list, "Magenta", _("Magenta")); |
| 1386 | stp_string_list_add_string(color_list, "Yellow", _("Yellow")); |
| 1387 | stp_string_list_add_string(color_list, "LightCyan", _("Light Cyan")); |
| 1388 | stp_string_list_add_string(color_list, "LightMagenta", _("Light Magenta")); |
| 1389 | } |
| 1390 | |
| 1391 | if (cmd == CMD_STATUS) |
| 1392 | printf(_("Ink Levels:\n")); |
| 1393 | printf("%20s %20s\n", _("Ink color"), _("Percent remaining")); |
| 1394 | print_old_ink_levels(ind, color_list); |
| 1395 | stp_string_list_destroy(color_list); |
| 1396 | if (cmd == CMD_STATUS) |
| 1397 | printf("\n"); |
| 1398 | } |
| 1399 | STP_DEBUG(printf("***looking at %s\n", buf)); |
| 1400 | } while ((buf = find_group(buf)) != NULL); |
| 1401 | } |
| 1402 | |
| 1403 | static void |
| 1404 | do_new_status(status_cmd_t cmd, char *buf, int bytes, |
| 1405 | const stp_printer_t *printer) |
| 1406 | { |
| 1407 | int i = 0; |
| 1408 | size_t j; |
| 1409 | const char *ind; |
| 1410 | const stp_string_list_t *color_list = NULL; |
| 1411 | stp_parameter_t desc; |
| 1412 | unsigned maint_level = 0xff; |
| 1413 | |
| 1414 | const stp_vars_t *printvars = NULL; |
| 1415 | if (printer) |
| 1416 | { |
| 1417 | printvars = stp_printer_get_defaults(printer); |
| 1418 | stp_describe_parameter(printvars, "ChannelNames", &desc); |
| 1419 | if (desc.p_type == STP_PARAMETER_TYPE_STRING_LIST) |
| 1420 | color_list = desc.bounds.str; |
| 1421 | } |
| 1422 | STP_DEBUG(printf("***New format bytes: %d bytes\n", bytes)); |
| 1423 | if (cmd == CMD_STATUS) |
| 1424 | printf(_("Printer Name: %s\n"), |
| 1425 | printer ? stp_printer_get_long_name(printer) : _("Unknown")); |
| 1426 | while (i < bytes) |
| 1427 | { |
| 1428 | unsigned hdr = buf[i]; |
| 1429 | unsigned total_param_count = buf[i + 1]; |
| 1430 | unsigned param = buf[i + 2]; |
| 1431 | STP_DEBUG(printf("***Header: %x param count: %d\n", hdr, total_param_count)); |
| 1432 | if (hdr == 0x0f) /* Always report ink */ |
| 1433 | { |
| 1434 | size_t count = (total_param_count - 1) / param; |
| 1435 | ind = buf + i + 3; |
| 1436 | if (cmd == CMD_STATUS) |
| 1437 | printf(_("Ink Levels:\n")); |
| 1438 | printf("%25s %20s\n", _("Ink color"), _("Percent remaining")); |
| 1439 | for (j = 0; j < count; j++) |
| 1440 | { |
| 1441 | STP_DEBUG(printf("*** Ink %zu: ind[0] %d ind[1] %d ind[2] %d interchangeable %d param %d count %d aux %d\n", |
| 1442 | j, ind[0], ind[1], ind[2], interchangeable_inks, |
| 1443 | param, color_count, aux_color_count)); |
| 1444 | if (ind[0] < color_count && param == 3 /* && |
| 1445 | (interchangeable_inks || ind[1] >= aux_color_count || |
| 1446 | ! aux_colors[(int) ind[1]]) */) |
| 1447 | { |
| 1448 | STP_DEBUG(printf("***Case 0\n")); |
| 1449 | printf("%25s %20d\n", |
| 1450 | gettext(colors_new[(int) ind[0]]), ind[2]); |
| 1451 | } |
| 1452 | else if (ind[1] < aux_color_count && aux_colors[(int) ind[1]]) |
| 1453 | { |
| 1454 | STP_DEBUG(printf("***Case 1\n")); |
| 1455 | printf("%25s %20d\n", |
| 1456 | gettext(aux_colors[(int) ind[1]]), ind[2]); |
| 1457 | } |
| 1458 | else |
| 1459 | { |
| 1460 | STP_DEBUG(printf("***Case 2\n")); |
| 1461 | printf("%8s 0x%02x 0x%02x %20d\n", |
| 1462 | _("Unknown"), (unsigned char) ind[0], |
| 1463 | (unsigned char) ind[1], ind[2]); |
| 1464 | } |
| 1465 | ind += param; |
| 1466 | } |
| 1467 | if (maint_level != 0xff) |
| 1468 | printf("\n%25s %20d\n", _("Maintenance Cartridge"), maint_level); |
| 1469 | if (cmd == CMD_STATUS) |
| 1470 | printf("\n"); |
| 1471 | } |
| 1472 | else if (hdr == 0x0d) /* Always report Maintenance cartridge level */ |
| 1473 | { |
| 1474 | maint_level = buf[i + 2]; /* Stash and print out as part of ink level */ |
| 1475 | } |
| 1476 | else if (cmd == CMD_STATUS) |
| 1477 | { |
| 1478 | switch (hdr) |
| 1479 | { |
| 1480 | case 0x1: /* Status */ |
| 1481 | print_status(param); |
| 1482 | break; |
| 1483 | case 0x2: /* Error */ |
| 1484 | print_error(param); |
| 1485 | break; |
| 1486 | case 0x3: /* Self-printing */ |
| 1487 | print_self_printing_state(param); |
| 1488 | break; |
| 1489 | case 0x4: /* Warning */ |
| 1490 | for (j = 0; j < total_param_count; j++) |
| 1491 | { |
| 1492 | param = (unsigned) buf[i + j + 2]; |
| 1493 | print_warning(param, color_list); |
| 1494 | } |
| 1495 | break; |
| 1496 | case 0x19: /* Job name */ |
| 1497 | if (total_param_count > 5) |
| 1498 | { |
| 1499 | printf(_("Job Name: ")); |
| 1500 | for (j = 5; j < total_param_count; j++) |
| 1501 | putchar(buf[i + j + 2]); |
| 1502 | putchar('\n'); |
| 1503 | } |
| 1504 | break; |
| 1505 | case 0x1f: /* Serial number */ |
| 1506 | printf(_("Serial Number: ")); |
| 1507 | for (j = 0; j < total_param_count; j++) |
| 1508 | putchar(buf[i + j + 2]); |
| 1509 | putchar('\n'); |
| 1510 | break; |
| 1511 | case 0x21: /* Paper remaining, in centimeters */ |
| 1512 | param = buf[i + 3] + (buf[i + 4] << 8); |
| 1513 | printf(_("Paper Remaining:")); |
| 1514 | printf(" %d feet / %d cm", (int)(((double)param+15.24) / 30.48f), param); |
| 1515 | putchar('\n'); |
| 1516 | break; |
| 1517 | case 0x2c: /* Paper width, in units of 360 dpi */ |
| 1518 | param = buf[i + 2] + (buf[i + 3] << 8); |
| 1519 | printf(_("Paper Width: ")); |
| 1520 | if (param > 2880) |
| 1521 | printf("A4 / 210mm"); |
| 1522 | else if (param > 2160) |
| 1523 | printf("8 inch / 203 mm"); |
| 1524 | else if (param > 1800) |
| 1525 | printf("6 inch / 152 mm"); |
| 1526 | else if (param > 1440) |
| 1527 | printf("5 inch / 127 mm"); |
| 1528 | else |
| 1529 | printf("4 inch / 102 mm"); |
| 1530 | putchar('\n'); |
| 1531 | break; |
| 1532 | default: |
| 1533 | /* Ignore other commands */ |
| 1534 | break; |
| 1535 | } |
| 1536 | } |
| 1537 | i += total_param_count + 2; |
| 1538 | } |
| 1539 | if (printer) |
| 1540 | stp_parameter_description_destroy(&desc); |
| 1541 | exit(0); |
| 1542 | } |
| 1543 | |
| 1544 | static void |
| 1545 | do_status_command_internal(status_cmd_t cmd) |
| 1546 | { |
| 1547 | int fd; |
| 1548 | int status; |
| 1549 | int retry = 4; |
| 1550 | char buf[1024]; |
| 1551 | const stp_printer_t *printer; |
| 1552 | const char *cmd_name = NULL; |
| 1553 | switch (cmd) |
| 1554 | { |
| 1555 | case CMD_INK_LEVEL: |
| 1556 | cmd_name = _("ink levels"); |
| 1557 | break; |
| 1558 | case CMD_STATUS: |
| 1559 | cmd_name = _("status"); |
| 1560 | break; |
| 1561 | } |
| 1562 | if (!raw_device) |
| 1563 | { |
| 1564 | fprintf(stderr,_("Obtaining %s requires using a raw device.\n"), |
| 1565 | gettext(cmd_name)); |
| 1566 | exit(1); |
| 1567 | } |
| 1568 | |
| 1569 | STP_DEBUG(printf("***%s...\n", cmd_name)); |
| 1570 | printer = get_printer(1, 0); |
| 1571 | if (!found_unknown_old_printer) |
| 1572 | STP_DEBUG(printf("***%s found %s%s\n", gettext(cmd_name), |
| 1573 | printer ? stp_printer_get_long_name(printer) : |
| 1574 | printer_model ? printer_model : "(null)", |
| 1575 | printer ? "" : "(Unknown model)")); |
| 1576 | |
| 1577 | fd = open_raw_device(); |
| 1578 | |
| 1579 | if (isnew) |
| 1580 | { |
| 1581 | /* request status command */ |
| 1582 | status = |
| 1583 | writeAndReadData(fd, socket_id, (const unsigned char*)"st\1\0\1", |
| 1584 | 5, 1, (unsigned char *) buf, 1023, |
| 1585 | &send_size, &receive_size, &test_for_st); |
| 1586 | if (status <= 0) |
| 1587 | { |
| 1588 | fprintf(stderr, _("\nCannot write to %s: %s\n"), raw_device, strerror(errno)); |
| 1589 | CloseChannel(fd, socket_id); |
| 1590 | exit(1); |
| 1591 | } |
| 1592 | buf[status] = '\0'; |
| 1593 | if (buf[7] == '2') |
| 1594 | do_new_status(cmd, buf + 12, status - 12, printer); |
| 1595 | else |
| 1596 | do_old_status(cmd, buf + 9, printer); |
| 1597 | CloseChannel(fd, socket_id); |
| 1598 | } |
| 1599 | else |
| 1600 | { |
| 1601 | do |
| 1602 | { |
| 1603 | add_resets(2); |
| 1604 | initialize_print_cmd(1); |
| 1605 | do_remote_cmd("ST", 2, 0, 1); |
| 1606 | add_resets(2); |
| 1607 | if (SafeWrite(fd, printer_cmd, bufpos) < bufpos) |
| 1608 | { |
| 1609 | fprintf(stderr, _("Cannot write to %s: %s\n"), raw_device, |
| 1610 | strerror(errno)); |
| 1611 | exit(1); |
| 1612 | } |
| 1613 | status = read_from_printer(fd, buf, 1024, 1); |
| 1614 | if (status < 0) |
| 1615 | { |
| 1616 | exit(1); |
| 1617 | } |
| 1618 | } while (--retry != 0 && !status); |
| 1619 | buf[status] = '\0'; |
| 1620 | if (status > 9) |
| 1621 | do_old_status(cmd, buf + 9, printer); |
| 1622 | } |
| 1623 | (void) close(fd); |
| 1624 | exit(0); |
| 1625 | } |
| 1626 | |
| 1627 | void |
| 1628 | do_ink_level(void) |
| 1629 | { |
| 1630 | do_status_command_internal(CMD_INK_LEVEL); |
| 1631 | } |
| 1632 | |
| 1633 | static stp_string_list_t * |
| 1634 | get_ink_channel_list(const stp_printer_t *printer, int fd) |
| 1635 | { |
| 1636 | stp_parameter_t desc; |
| 1637 | char buf[1024]; |
| 1638 | int i; |
| 1639 | char *ind; |
| 1640 | stp_string_list_t *color_list = stp_string_list_create(); |
| 1641 | |
| 1642 | if (printer) |
| 1643 | { |
| 1644 | const stp_vars_t *printvars = stp_printer_get_defaults(printer); |
| 1645 | stp_describe_parameter(printvars, "ChannelNames", &desc); |
| 1646 | } |
| 1647 | |
| 1648 | if (printer && desc.p_type == STP_PARAMETER_TYPE_STRING_LIST) |
| 1649 | { |
| 1650 | STP_DEBUG(printf("***Using color list from driver (%ld %ld)\n", |
| 1651 | (long)stp_string_list_count(desc.bounds.str), |
| 1652 | (long)stp_string_list_count(color_list))); |
| 1653 | color_list = stp_string_list_create_copy(desc.bounds.str); |
| 1654 | stp_parameter_description_destroy(&desc); |
| 1655 | } |
| 1656 | else |
| 1657 | { |
| 1658 | /* |
| 1659 | * If we're using the "new" ink status format and we don't know |
| 1660 | * about the printer, take the colors from the ink status |
| 1661 | * message rather than from the ink list. This gives us a |
| 1662 | * last chance to determine the inks |
| 1663 | */ |
| 1664 | /* request status command */ |
| 1665 | int status = |
| 1666 | writeAndReadData(fd, socket_id, (const unsigned char*)"st\1\0\1", |
| 1667 | 5, 1, (unsigned char *) buf, 1023, |
| 1668 | &send_size, &receive_size, &test_for_st); |
| 1669 | if (status <= 0) |
| 1670 | { |
| 1671 | stp_parameter_description_destroy(&desc); |
| 1672 | fprintf(stderr, _("\nCannot write to %s: %s\n"), |
| 1673 | raw_device, strerror(errno)); |
| 1674 | exit(1); |
| 1675 | } |
| 1676 | buf[status] = '\0'; |
| 1677 | if ( buf[7] == '2' ) |
| 1678 | { |
| 1679 | int param; |
| 1680 | STP_DEBUG(printf("***New format ink!\n")); |
| 1681 | /* new binary format ! */ |
| 1682 | i = 10; |
| 1683 | while (buf[i] != 0x0f && i < status) |
| 1684 | i += buf[i + 1] + 2; |
| 1685 | ind = buf + i; |
| 1686 | param = buf[i + 2]; |
| 1687 | i = 3; |
| 1688 | while (i < ind[1]) |
| 1689 | { |
| 1690 | if (ind[i] < color_count) |
| 1691 | { |
| 1692 | STP_DEBUG(printf("*** Case 0: Ink %d %d (%s)\n", |
| 1693 | i, ind[i], colors_new[(int) ind[i]])); |
| 1694 | stp_string_list_add_string_unsafe(color_list, |
| 1695 | colors_new[(int) ind[i]], |
| 1696 | colors_new[(int) ind[i]]); |
| 1697 | } |
| 1698 | else if (ind[i] == 0x40 && ind[i + 1] < aux_color_count) |
| 1699 | { |
| 1700 | STP_DEBUG(printf("*** Case 1: Ink %d %d (%s)\n", |
| 1701 | i, ind[i+1], aux_colors[(int) ind[i+1]])); |
| 1702 | stp_string_list_add_string_unsafe(color_list, |
| 1703 | aux_colors[(int) ind[i + 1]], |
| 1704 | aux_colors[(int) ind[i + 1]]); |
| 1705 | } |
| 1706 | else |
| 1707 | { |
| 1708 | STP_DEBUG(printf("*** Case 2: Unknown\n")); |
| 1709 | stp_string_list_add_string_unsafe(color_list, "Unknown", |
| 1710 | "Unknown"); |
| 1711 | } |
| 1712 | i+=param; |
| 1713 | } |
| 1714 | } |
| 1715 | STP_DEBUG(printf("***Using color list from status message\n")); |
| 1716 | } |
| 1717 | return color_list; |
| 1718 | } |
| 1719 | |
| 1720 | static void |
| 1721 | do_extended_ink_info_1(const stp_printer_t *printer) |
| 1722 | { |
| 1723 | int status; |
| 1724 | char buf[1024]; |
| 1725 | unsigned val, id, id2, year, year2, month, month2; |
| 1726 | unsigned iv[6]; |
| 1727 | |
| 1728 | char *ind; |
| 1729 | size_t i; |
| 1730 | int fd = open_raw_device(); |
| 1731 | |
| 1732 | stp_string_list_t *color_list = get_ink_channel_list(printer, fd); |
| 1733 | |
| 1734 | for (i = 0; i < stp_string_list_count(color_list); i++) |
| 1735 | { |
| 1736 | char req[] = "ii\2\0\1\1"; |
| 1737 | req[5] = i + 1; |
| 1738 | status = |
| 1739 | writeAndReadData(fd, socket_id, (const unsigned char*)req, |
| 1740 | 6, 1, (unsigned char *) buf, 1023, |
| 1741 | &send_size, &receive_size, &test_for_ii); |
| 1742 | if (status <= 0) |
| 1743 | { |
| 1744 | stp_string_list_destroy(color_list); |
| 1745 | CloseChannel(fd, socket_id); |
| 1746 | exit(1); |
| 1747 | } |
| 1748 | ind = strchr(buf, 'i'); |
| 1749 | if (!ind) |
| 1750 | ind = strchr(buf, 'I'); |
| 1751 | if (!ind) |
| 1752 | { |
| 1753 | STP_DEBUG(printf("***Case 0: failure %zu (%s)\n", i, buf)); |
| 1754 | printf("Cannot identify cartridge in slot %zu\n", i); |
| 1755 | } |
| 1756 | else if (sscanf(ind, |
| 1757 | "II:01;IQT:%x;TSH:%*4s;PDY:%x;PDM:%x;IC1:%x;IC2:%*x;IK1:%*x;IK2:%*x;TOV:%*x;TVU:%*x;LOG:EPSON;IQT:%x,%x,%x,%x,%x;TSH:%*4s;PDY:%x;PDM:%x;IC1:%x;IC2:%*xIK1:%*x;IK2;%*x;TOV:%*x;TVU:%*x;LOG:EPSON;", |
| 1758 | &iv[0], &year, &month, &id, |
| 1759 | &iv[1], &iv[2], &iv[3], &iv[4], &iv[5], |
| 1760 | &year2, &month2, &id2) == 12 || |
| 1761 | sscanf(ind, |
| 1762 | "II:01;IQT:%x;TSH:%*4s;PDY:%x;PDM:%x;IC1:%x;IC2:%*x;IK1:%*x;IK2:%*x;TOV:%*x;TVU:%*x;LOG:INKbyEPSON;IQT:%x,%x,%x,%x,%x;TSH:%*4s;PDY:%x;PDM:%x;IC1:%x;IC2:%*xIK1:%*x;IK2;%*x;TOV:%*x;TVU:%*x;LOG:INKbyEPSON;", |
| 1763 | &iv[0], &year, &month, &id, |
| 1764 | &iv[1], &iv[2], &iv[3], &iv[4], &iv[5], |
| 1765 | &year2, &month2, &id2) == 12) |
| 1766 | { |
| 1767 | int j; |
| 1768 | STP_DEBUG(printf("***Case 1: i %zu iv %ud %ud %ud %ud %ud %ud year %ud %ud mo %ud %ud id %ud %ud\n", |
| 1769 | i, iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], |
| 1770 | year, year2, month, month2, id, id2)); |
| 1771 | printf("%20s %20s %12s %7s\n", |
| 1772 | _("Ink cartridge"), _("Percent remaining"), _("Part number"), |
| 1773 | _("Date")); |
| 1774 | printf("%20s %20d T0%03d %2d%02d-%02d\n", |
| 1775 | gettext(stp_string_list_param(color_list, 0)->text), |
| 1776 | iv[0], id, (year > 80 ? 19 : 20), year, month); |
| 1777 | for (j = 1; j < 6; j++) |
| 1778 | printf("%20s %20d T0%03d %2d%02d-%02d\n", |
| 1779 | gettext(stp_string_list_param(color_list, j)->text), |
| 1780 | iv[j], id2, (year2 > 80 ? 19 : 20), year2, month2); |
| 1781 | break; |
| 1782 | } |
| 1783 | else if (sscanf(ind, |
| 1784 | "II:01;IQT:%x;TSH:%*4s;PDY:%x;PDM:%x;IC1:%x;IC2:%*x;IK1:%*x;IK2:%*x;TOV:%*x;TVU:%*x;LOG:EPSON;IQT:%x,%x,%x;TSH:%*4s;PDY:%x;PDM:%x;IC1:%x;IC2:%*xIK1:%*x;IK2;%*x;TOV:%*x;TVU:%*x;LOG:EPSON;", |
| 1785 | &iv[0], &year, &month, &id, |
| 1786 | &iv[1], &iv[2], &iv[3], |
| 1787 | &year2, &month2, &id2) == 10 || |
| 1788 | sscanf(ind, |
| 1789 | "II:01;IQT:%x;TSH:%*4s;PDY:%x;PDM:%x;IC1:%x;IC2:%*x;IK1:%*x;IK2:%*x;TOV:%*x;TVU:%*x;LOG:INKbyEPSON;IQT:%x,%x,%x;TSH:%*4s;PDY:%x;PDM:%x;IC1:%x;IC2:%*xIK1:%*x;IK2;%*x;TOV:%*x;TVU:%*x;LOG:INKbyEPSON;", |
| 1790 | &iv[0], &year, &month, &id, |
| 1791 | &iv[1], &iv[2], &iv[3], |
| 1792 | &year2, &month2, &id2) == 10) |
| 1793 | { |
| 1794 | int j; |
| 1795 | STP_DEBUG(printf("***Case 2: i %zu iv %ud %ud %ud %ud year %ud %ud mo %ud %ud id %ud %ud\n", |
| 1796 | i, iv[0], iv[1], iv[2], iv[3], |
| 1797 | year, year2, month, month2, id, id2)); |
| 1798 | printf("%20s %20s %12s %7s\n", |
| 1799 | _("Ink cartridge"), _("Percent remaining"), _("Part number"), |
| 1800 | _("Date")); |
| 1801 | printf("%20s %20d T0%03d %2d%02d-%02d\n", |
| 1802 | gettext(stp_string_list_param(color_list, 0)->text), |
| 1803 | iv[0], id, (year > 80 ? 19 : 20), year, month); |
| 1804 | for (j = 1; j < 4; j++) |
| 1805 | printf("%20s %20d T0%03d %2d%02d-%02d\n", |
| 1806 | gettext(stp_string_list_param(color_list, j)->text), |
| 1807 | iv[j], id2, (year2 > 80 ? 19 : 20), year2, month2); |
| 1808 | break; |
| 1809 | } |
| 1810 | else if (sscanf(ind, |
| 1811 | "II:01;IQT:%x;TSH:%*4s;PDY:%x;PDM:%x;IC1:%x;IC2:%*x;IK1:%*x;IK2:%*x;TOV:%*x;TVU:%*x;LOG:EPSON;", |
| 1812 | &val, &year, &month, &id ) == 4 || |
| 1813 | sscanf(ind, |
| 1814 | "II:01;IQT:%x;TSH:%*4s;PDY:%x;PDM:%x;IC1:%x;IC2:%*x;IK1:%*x;IK2:%*x;TOV:%*x;TVU:%*x;LOG:INKbyEPSON;", |
| 1815 | &val, &year, &month, &id ) == 4) |
| 1816 | { |
| 1817 | STP_DEBUG(printf("***Case 3: i %zu val %ud year %ud mo %ud id %ud\n", |
| 1818 | i, val, year, month, id)); |
| 1819 | if (i == 0) |
| 1820 | printf("%20s %20s %12s %7s\n", |
| 1821 | _("Ink cartridge"), _("Percent remaining"), _("Part number"), |
| 1822 | _("Date")); |
| 1823 | printf("%20s %20d T0%03d %2d%02d-%02d\n", |
| 1824 | gettext(stp_string_list_param(color_list, i)->text), |
| 1825 | val, id, (year > 80 ? 19 : 20), year, month); |
| 1826 | } |
| 1827 | else if (sscanf(ind, |
| 1828 | "IQT:%x;TSH:%*4s;PDY:%x;PDM:%x;IC1:%x;IC2:%*x;IK1:%*x;IK2:%*x;TOV:%*x;TVU:%*x;LOG:EPSON;", |
| 1829 | &val, &year, &month, &id ) == 4 || |
| 1830 | sscanf(ind, |
| 1831 | "IQT:%x;TSH:%*4s;PDY:%x;PDM:%x;IC1:%x;IC2:%*x;IK1:%*x;IK2:%*x;TOV:%*x;TVU:%*x;LOG:INKbyEPSON;", |
| 1832 | &val, &year, &month, &id ) == 4) |
| 1833 | { |
| 1834 | STP_DEBUG(printf("***Case 4: i %zu val %ud year %ud mo %ud id %ud\n", |
| 1835 | i, val, year, month, id)); |
| 1836 | if (i == 0) |
| 1837 | printf("%20s %20s %12s %7s\n", |
| 1838 | _("Ink cartridge"), _("Percent remaining"), _("Part number"), |
| 1839 | _("Date")); |
| 1840 | printf("%20s %20d T0%03d %2d%02d-%02d\n", |
| 1841 | gettext(stp_string_list_param(color_list, i)->text), |
| 1842 | val, id, (year > 80 ? 19 : 20), year, month); |
| 1843 | } |
| 1844 | else if (sscanf(ind, |
| 1845 | "IQT:%x;TSH:%*4s;PDY:%x;PDM:%x;IC1:%x;IC2:%*x;IK1:%*x;IK2:%*x;TOV:%*x;TVU:%*x;LOG:EPSON;", |
| 1846 | &val, &year, &month, &id ) == 4 || |
| 1847 | sscanf(ind, |
| 1848 | "IQT:%x;TSH:%*4s;PDY:%x;PDM:%x;IC1:%x;IC2:%*x;IK1:%*x;IK2:%*x;TOV:%*x;TVU:%*x;LOG:INKbyEPSON;", |
| 1849 | &val, &year, &month, &id ) == 4) |
| 1850 | { |
| 1851 | STP_DEBUG(printf("***Case 4: i %zu val %ud year %ud mo %ud id %ud\n", |
| 1852 | i, val, year, month, id)); |
| 1853 | if (i == 0) |
| 1854 | printf("%20s %20s %12s %7s\n", |
| 1855 | _("Ink cartridge"), _("Percent remaining"), _("Part number"), |
| 1856 | _("Date")); |
| 1857 | printf("%20s %20d T0%03d %2d%02d-%02d\n", |
| 1858 | gettext(stp_string_list_param(color_list, i)->text), |
| 1859 | val, id, (year > 80 ? 19 : 20), year, month); |
| 1860 | } |
| 1861 | else if (sscanf(ind, |
| 1862 | "II:%*2s;IQT:%x;PDY:%x;PDM:%x;STY:%*2s;STM:%*2s;STD:%*2s;EDY:%*2s;EDM:%*2s;EDD:%*2s;IC1:%x;IC2:%*4s;IK:%*4s;TOV:%*x;TVU:%*x;VIQ:%*4x;UIQ:%*4x;ERC:%*x;SID:%*x;LOG:EPSON ;", |
| 1863 | &val, &year, &month, &id ) == 4 || |
| 1864 | sscanf(ind, |
| 1865 | "ii:%*2s;IQT:%x;PDY:%x;PDM:%x;STY:%*2s;STM:%*2s;STD:%*2s;EDY:%*2s;EDM:%*2s;EDD:%*2s;IC1:%x;IC2:%*4s;IK:%*4s;TOV:%*x;TOU:%*x;TVU:%*x;VIQ:%*4x;UIQ:%*4x;ERC:%*x;SID:%*x;LOG:EPSON ;", |
| 1866 | &val, &year, &month, &id ) == 4) |
| 1867 | { |
| 1868 | STP_DEBUG(printf("***Case 5: i %zu val %ud year %ud mo %ud id %ud\n", |
| 1869 | i, val, year, month, id)); |
| 1870 | if (i == 0) |
| 1871 | printf("%20s %20s %12s %7s\n", |
| 1872 | _("Ink cartridge"), _("Percent remaining"), _("Part number"), |
| 1873 | _("Date")); |
| 1874 | printf("%20s %20d T0%03d %2d%02d-%02d\n", |
| 1875 | gettext(stp_string_list_param(color_list, i)->text), |
| 1876 | val, id, (year > 80 ? 19 : 20), year, month); |
| 1877 | } |
| 1878 | else |
| 1879 | { |
| 1880 | STP_DEBUG(printf("***Case 6: failure %zu (%s)\n", i, ind)); |
| 1881 | printf("Cannot identify cartridge in slot %zu\n", i); |
| 1882 | } |
| 1883 | } |
| 1884 | stp_string_list_destroy(color_list); |
| 1885 | CloseChannel(fd, socket_id); |
| 1886 | } |
| 1887 | |
| 1888 | void |
| 1889 | do_extended_ink_info(void) |
| 1890 | { |
| 1891 | const stp_printer_t *printer; |
| 1892 | |
| 1893 | if (!raw_device) |
| 1894 | { |
| 1895 | fprintf(stderr,_("Obtaining extended ink information requires using a raw device.\n")); |
| 1896 | exit(1); |
| 1897 | } |
| 1898 | |
| 1899 | printer = get_printer(1, 0); |
| 1900 | if (!printer) |
| 1901 | fprintf(stderr, |
| 1902 | "Warning! Printer %s is not known; information may be incomplete or incorrect\n", |
| 1903 | printer_model ? printer_model : "(unknown printer)"); |
| 1904 | |
| 1905 | if (isnew) |
| 1906 | do_extended_ink_info_1(printer); |
| 1907 | else |
| 1908 | do_ink_level(); |
| 1909 | } |
| 1910 | |
| 1911 | void |
| 1912 | do_reset_ink_levels(void) |
| 1913 | { |
| 1914 | const stp_printer_t *printer; |
| 1915 | int fd; |
| 1916 | size_t i; |
| 1917 | |
| 1918 | stp_string_list_t *color_list; |
| 1919 | |
| 1920 | if (!raw_device) |
| 1921 | { |
| 1922 | fprintf(stderr,_("Resetting ink levels requires using a raw device.\n")); |
| 1923 | exit(1); |
| 1924 | } |
| 1925 | |
| 1926 | printer = get_printer(1, 0); |
| 1927 | if (!printer) |
| 1928 | { |
| 1929 | fprintf(stderr, |
| 1930 | "Error! Printer %s is not known; cannot reset ink\n", |
| 1931 | printer_model ? printer_model : "(unknown printer)"); |
| 1932 | exit(1); |
| 1933 | } |
| 1934 | |
| 1935 | if (!isnew) |
| 1936 | { |
| 1937 | fprintf(stderr, "Cannot reset ink for printer %s\n", printer_model); |
| 1938 | } |
| 1939 | fd = open_raw_device(); |
| 1940 | color_list = get_ink_channel_list(printer, fd); |
| 1941 | for (i = 0; i < stp_string_list_count(color_list); i++) |
| 1942 | { |
| 1943 | int status; |
| 1944 | char buf[1024]; |
| 1945 | char req[] = "ri\2\0\0\0"; |
| 1946 | req[5] = i; |
| 1947 | STP_DEBUG(printf("***Attempt to reset ink for channel %zu\n", i)); |
| 1948 | status = |
| 1949 | writeAndReadData(fd, socket_id, (const unsigned char*)req, |
| 1950 | 6, 1, (unsigned char *) buf, 1023, |
| 1951 | &send_size, &receive_size, &test_for_ri); |
| 1952 | if (status <= 0) |
| 1953 | { |
| 1954 | stp_string_list_destroy(color_list); |
| 1955 | CloseChannel(fd, socket_id); |
| 1956 | exit(1); |
| 1957 | } |
| 1958 | } |
| 1959 | stp_string_list_destroy(color_list); |
| 1960 | CloseChannel(fd, socket_id); |
| 1961 | } |
| 1962 | |
| 1963 | void |
| 1964 | do_identify(void) |
| 1965 | { |
| 1966 | const stp_printer_t *printer; |
| 1967 | if (!raw_device) |
| 1968 | { |
| 1969 | fprintf(stderr, |
| 1970 | _("Printer identification requires using a raw device.\n")); |
| 1971 | exit(1); |
| 1972 | } |
| 1973 | if (printer_model) |
| 1974 | printer_model = NULL; |
| 1975 | printer = get_printer(1, 1); |
| 1976 | if (printer && !found_unknown_old_printer) |
| 1977 | { |
| 1978 | if (print_short_name) |
| 1979 | printf("%s\n", stp_printer_get_driver(printer)); |
| 1980 | else |
| 1981 | printf("%s\n", gettext(stp_printer_get_long_name(printer))); |
| 1982 | exit(0); |
| 1983 | } |
| 1984 | else if (printer_model) |
| 1985 | printf("EPSON %s\n", printer_model); |
| 1986 | else |
| 1987 | { |
| 1988 | fprintf(stderr, _("Cannot identify printer model.\n")); |
| 1989 | exit(1); |
| 1990 | } |
| 1991 | } |
| 1992 | |
| 1993 | void |
| 1994 | do_status(void) |
| 1995 | { |
| 1996 | do_status_command_internal(CMD_STATUS); |
| 1997 | exit(1); |
| 1998 | } |
| 1999 | |
| 2000 | |
| 2001 | void |
| 2002 | do_head_clean(void) |
| 2003 | { |
| 2004 | if (printer_model) |
| 2005 | set_printer_model(); |
| 2006 | else if (raw_device) |
| 2007 | (void) get_printer(1, 0); |
| 2008 | initialize_print_cmd(1); |
| 2009 | do_remote_cmd("CH", 2, 0, 0); |
| 2010 | printf(_("Cleaning heads...\n")); |
| 2011 | exit(do_print_cmd()); |
| 2012 | } |
| 2013 | |
| 2014 | void |
| 2015 | do_nozzle_check(void) |
| 2016 | { |
| 2017 | if (printer_model) |
| 2018 | set_printer_model(); |
| 2019 | else if (raw_device) |
| 2020 | (void) get_printer(1, 0); |
| 2021 | initialize_print_cmd(1); |
| 2022 | start_remote_sequence(); |
| 2023 | do_remote_cmd_only("VI", 2, 0, 0); |
| 2024 | do_remote_cmd_only("NC", 2, 0, 0x10); |
| 2025 | do_remote_cmd_only("NC", 2, 0, 0); |
| 2026 | end_remote_sequence(); |
| 2027 | printf(_("Running nozzle check, please ensure paper is in the printer.\n")); |
| 2028 | exit(do_print_cmd()); |
| 2029 | } |
| 2030 | |
| 2031 | const char *new_align_help = N_("\ |
| 2032 | Please read these instructions very carefully before proceeding.\n\ |
| 2033 | \n\ |
| 2034 | This utility lets you align the print head of your Epson Stylus inkjet\n\ |
| 2035 | printer. Misuse of this utility may cause your print quality to degrade\n\ |
| 2036 | and possibly damage your printer. This utility has not been reviewed by\n\ |
| 2037 | Seiko Epson for correctness, and is offered with no warranty at all. The\n\ |
| 2038 | entire risk of using this utility lies with you.\n\ |
| 2039 | \n\ |
| 2040 | This utility prints %d test patterns. Each pattern looks very similar.\n\ |
| 2041 | The patterns consist of a series of pairs of vertical lines that overlap.\n\ |
| 2042 | Below each pair of lines is a number between %d and %d.\n\ |
| 2043 | \n\ |
| 2044 | When you inspect the pairs of lines, you should find the pair of lines that\n\ |
| 2045 | is best in alignment, that is, that best forms a single vertical line.\n\ |
| 2046 | Inspect the pairs very carefully to find the best match. Using a loupe\n\ |
| 2047 | or magnifying glass is recommended for the most critical inspection.\n\ |
| 2048 | It is also suggested that you use a good quality paper for the test,\n\ |
| 2049 | so that the lines are well-formed and do not spread through the paper.\n\ |
| 2050 | After picking the number matching the best pair, place the paper back in\n\ |
| 2051 | the paper input tray before typing it in.\n\ |
| 2052 | \n\ |
| 2053 | Each pattern is similar, but later patterns use finer dots for more\n\ |
| 2054 | critical alignment. You must run all of the passes to correctly align your\n\ |
| 2055 | printer. After running all the alignment passes, the alignment\n\ |
| 2056 | patterns will be printed once more. You should find that the middle-most\n\ |
| 2057 | pair (#%d out of the %d) is the best for all patterns.\n\ |
| 2058 | \n\ |
| 2059 | After the passes are printed once more, you will be offered the\n\ |
| 2060 | choices of (s)aving the result in the printer, (r)epeating the process,\n\ |
| 2061 | or (q)uitting without saving. Quitting will not restore the previous\n\ |
| 2062 | settings, but powering the printer off and back on will. If you quit,\n\ |
| 2063 | you must repeat the entire process if you wish to later save the results.\n\ |
| 2064 | It is essential that you not turn your printer off during this procedure.\n\n"); |
| 2065 | |
| 2066 | const char *old_align_help = N_("\ |
| 2067 | Please read these instructions very carefully before proceeding.\n\ |
| 2068 | \n\ |
| 2069 | This utility lets you align the print head of your Epson Stylus inkjet\n\ |
| 2070 | printer. Misuse of this utility may cause your print quality to degrade\n\ |
| 2071 | and possibly damage your printer. This utility has not been reviewed by\n\ |
| 2072 | Seiko Epson for correctness, and is offered with no warranty at all. The\n\ |
| 2073 | entire risk of using this utility lies with you.\n\ |
| 2074 | \n\ |
| 2075 | This utility prints a test pattern that consist of a series of pairs of\n\ |
| 2076 | vertical lines that overlap. Below each pair of lines is a number between\n\ |
| 2077 | %d and %d.\n\ |
| 2078 | \n\ |
| 2079 | When you inspect the pairs of lines, you should find the pair of lines that\n\ |
| 2080 | is best in alignment, that is, that best forms a single vertical align.\n\ |
| 2081 | Inspect the pairs very carefully to find the best match. Using a loupe\n\ |
| 2082 | or magnifying glass is recommended for the most critical inspection.\n\ |
| 2083 | It is also suggested that you use a good quality paper for the test,\n\ |
| 2084 | so that the lines are well-formed and do not spread through the paper.\n\ |
| 2085 | After picking the number matching the best pair, place the paper back in\n\ |
| 2086 | the paper input tray before typing it in.\n\ |
| 2087 | \n\ |
| 2088 | After running the alignment pattern, it will be printed once more. You\n\ |
| 2089 | should find that the middle-most pair (#%d out of the %d) is the best.\n\ |
| 2090 | You will then be offered the choices of (s)aving the result in the printer,\n\ |
| 2091 | (r)epeating the process, or (q)uitting without saving. Quitting will not\n\ |
| 2092 | restore the previous settings, but powering the printer off and back on will.\n\ |
| 2093 | If you quit, you must repeat the entire process if you wish to later save\n\ |
| 2094 | the results. It is essential that you not turn off your printer during\n\ |
| 2095 | this procedure.\n\n"); |
| 2096 | |
| 2097 | static void |
| 2098 | do_align_help(int passes, int choices) |
| 2099 | { |
| 2100 | if (passes > 1) |
| 2101 | printf(gettext(new_align_help), passes, 1, choices, (choices + 1) / 2, choices); |
| 2102 | else |
| 2103 | printf(gettext(old_align_help), 1, choices, (choices + 1) / 2, choices); |
| 2104 | fflush(stdout); |
| 2105 | } |
| 2106 | |
| 2107 | static void |
| 2108 | printer_error(void) |
| 2109 | { |
| 2110 | printf(_("Unable to send command to the printer, exiting.\n")); |
| 2111 | exit(1); |
| 2112 | } |
| 2113 | |
| 2114 | static int |
| 2115 | do_final_alignment(void) |
| 2116 | { |
| 2117 | int retry_count = 0; |
| 2118 | while (1) |
| 2119 | { |
| 2120 | char *inbuf; |
| 2121 | retry_count++; |
| 2122 | if (retry_count > 10) |
| 2123 | { |
| 2124 | printf(_("Exiting\n")); |
| 2125 | exit(1); |
| 2126 | } |
| 2127 | printf(_("Please inspect the final output very carefully to ensure that your\n" |
| 2128 | "printer is in proper alignment. You may now:\n" |
| 2129 | " (s)ave the results in the printer,\n" |
| 2130 | " (q)uit without saving the results, or\n" |
| 2131 | " (r)epeat the entire process from the beginning.\n" |
| 2132 | "You will then be asked to confirm your choice.\n" |
| 2133 | "What do you want to do (s, q, r)?\n")); |
| 2134 | fflush(stdout); |
| 2135 | inbuf = do_get_input(_("> ")); |
| 2136 | if (!inbuf) |
| 2137 | continue; |
| 2138 | switch (inbuf[0]) |
| 2139 | { |
| 2140 | case 'q': |
| 2141 | case 'Q': |
| 2142 | printf(_("Please confirm by typing 'q' again that you wish to quit without saving:\n")); |
| 2143 | fflush(stdout); |
| 2144 | inbuf = do_get_input (_("> ")); |
| 2145 | if (!inbuf) |
| 2146 | continue; |
| 2147 | if (inbuf[0] == 'q' || inbuf[0] == 'Q') |
| 2148 | { |
| 2149 | printf(_("OK, your printer is aligned, but the alignment has not been saved.\n" |
| 2150 | "If you wish to save the alignment, you must repeat this process.\n")); |
| 2151 | return 1; |
| 2152 | } |
| 2153 | break; |
| 2154 | case 'r': |
| 2155 | case 'R': |
| 2156 | printf(_("Please confirm by typing 'r' again that you wish to repeat the\n" |
| 2157 | "alignment process:\n")); |
| 2158 | fflush(stdout); |
| 2159 | inbuf = do_get_input(_("> ")); |
| 2160 | if (!inbuf) |
| 2161 | continue; |
| 2162 | if (inbuf[0] == 'r' || inbuf[0] == 'R') |
| 2163 | { |
| 2164 | printf(_("Repeating the alignment process.\n")); |
| 2165 | return 0; |
| 2166 | } |
| 2167 | break; |
| 2168 | case 's': |
| 2169 | case 'S': |
| 2170 | printf(_("This will permanently alter the configuration of your printer.\n" |
| 2171 | "WARNING: this procedure has not been approved by Seiko Epson, and\n" |
| 2172 | "it may damage your printer. Proceed?\n" |
| 2173 | "Please confirm by typing 's' again that you wish to save the settings\n" |
| 2174 | "to your printer:\n")); |
| 2175 | |
| 2176 | fflush(stdout); |
| 2177 | inbuf = do_get_input(_("> ")); |
| 2178 | if (!inbuf) |
| 2179 | continue; |
| 2180 | if (inbuf[0] == 's' || inbuf[0] == 'S') |
| 2181 | { |
| 2182 | printf(_("About to save settings...")); |
| 2183 | fflush(stdout); |
| 2184 | initialize_print_cmd(1); |
| 2185 | do_remote_cmd("SV", 0); |
| 2186 | if (do_print_cmd()) |
| 2187 | { |
| 2188 | printf(_("failed!\n")); |
| 2189 | printf(_("Your settings were not saved successfully. You must repeat the\n" |
| 2190 | "alignment procedure.\n")); |
| 2191 | exit(1); |
| 2192 | } |
| 2193 | printf(_("succeeded!\n")); |
| 2194 | printf(_("Your alignment settings have been saved to the printer.\n")); |
| 2195 | return 1; |
| 2196 | } |
| 2197 | break; |
| 2198 | default: |
| 2199 | printf(_("Unrecognized command.\n")); |
| 2200 | continue; |
| 2201 | } |
| 2202 | printf(_("Final command was not confirmed.\n")); |
| 2203 | } |
| 2204 | } |
| 2205 | |
| 2206 | const char *printer_msg = |
| 2207 | N_("This procedure assumes that your printer is an %s.\n" |
| 2208 | "If this is not your printer model, please type control-C now and\n" |
| 2209 | "choose your actual printer model.\n\n" |
| 2210 | "Please place a sheet of paper in your printer to begin the head\n" |
| 2211 | "alignment procedure.\n"); |
| 2212 | |
| 2213 | /* |
| 2214 | * This is the thorny one. |
| 2215 | */ |
| 2216 | void |
| 2217 | do_align(void) |
| 2218 | { |
| 2219 | char *inbuf; |
| 2220 | long answer; |
| 2221 | char *endptr; |
| 2222 | int curpass; |
| 2223 | stp_parameter_t desc; |
| 2224 | const char *printer_name; |
| 2225 | stp_vars_t *v = stp_vars_create(); |
| 2226 | |
| 2227 | if (printer_model) |
| 2228 | set_printer_model(); |
| 2229 | else if (raw_device) |
| 2230 | (void) get_printer(0, 0); |
| 2231 | |
| 2232 | if (!the_printer_t || found_unknown_old_printer) |
| 2233 | return; |
| 2234 | |
| 2235 | printer_name = stp_printer_get_long_name(the_printer_t); |
| 2236 | stp_set_driver(v, stp_printer_get_driver(the_printer_t)); |
| 2237 | |
| 2238 | if (alignment_passes == 0) |
| 2239 | { |
| 2240 | stp_describe_parameter(v, "AlignmentPasses", &desc); |
| 2241 | if (desc.p_type != STP_PARAMETER_TYPE_INT) |
| 2242 | { |
| 2243 | fprintf(stderr, |
| 2244 | "Unable to retrieve number of alignment passes for printer %s\n", |
| 2245 | printer_name); |
| 2246 | return; |
| 2247 | } |
| 2248 | alignment_passes = desc.deflt.integer; |
| 2249 | stp_parameter_description_destroy(&desc); |
| 2250 | } |
| 2251 | |
| 2252 | if (alignment_choices == 0) |
| 2253 | { |
| 2254 | stp_describe_parameter(v, "AlignmentChoices", &desc); |
| 2255 | if (desc.p_type != STP_PARAMETER_TYPE_INT) |
| 2256 | { |
| 2257 | fprintf(stderr, |
| 2258 | "Unable to retrieve number of alignment choices for printer %s\n", |
| 2259 | printer_name); |
| 2260 | return; |
| 2261 | } |
| 2262 | alignment_choices = desc.deflt.integer; |
| 2263 | stp_parameter_description_destroy(&desc); |
| 2264 | } |
| 2265 | |
| 2266 | if (alignment_passes <= 0 || alignment_choices <= 0) |
| 2267 | { |
| 2268 | printf("No alignment required for printer %s\n", printer_name); |
| 2269 | return; |
| 2270 | } |
| 2271 | |
| 2272 | do |
| 2273 | { |
| 2274 | do_align_help(alignment_passes, alignment_choices); |
| 2275 | printf(gettext(printer_msg), gettext(printer_name)); |
| 2276 | inbuf = do_get_input(_("Press enter to continue > ")); |
| 2277 | top: |
| 2278 | initialize_print_cmd(1); |
| 2279 | for (curpass = 0; curpass < alignment_passes; curpass++) |
| 2280 | do_remote_cmd("DT", 3, 0, curpass, 0); |
| 2281 | if (do_print_cmd()) |
| 2282 | printer_error(); |
| 2283 | printf(_("Please inspect the print, and choose the best pair of lines in each pattern.\n" |
| 2284 | "Type a pair number, '?' for help, or 'r' to repeat the procedure.\n")); |
| 2285 | initialize_print_cmd(1); |
| 2286 | for (curpass = 1; curpass <= alignment_passes; curpass ++) |
| 2287 | { |
| 2288 | int retry_count = 0; |
| 2289 | reread: |
| 2290 | retry_count++; |
| 2291 | if (retry_count > 10) |
| 2292 | { |
| 2293 | printf(_("Exiting\n")); |
| 2294 | return; |
| 2295 | } |
| 2296 | printf(_("Pass #%d"), curpass); |
| 2297 | inbuf = do_get_input(_("> ")); |
| 2298 | if (!inbuf) |
| 2299 | goto reread; |
| 2300 | switch (inbuf[0]) |
| 2301 | { |
| 2302 | case 'r': |
| 2303 | case 'R': |
| 2304 | printf(_("Please insert a fresh sheet of paper.\n")); |
| 2305 | fflush(stdout); |
| 2306 | initialize_print_cmd(1); |
| 2307 | (void) do_get_input(_("Press enter to continue > ")); |
| 2308 | /* Ick. Surely there's a cleaner way? */ |
| 2309 | goto top; |
| 2310 | case 'h': |
| 2311 | case '?': |
| 2312 | do_align_help(alignment_passes, alignment_choices); |
| 2313 | fflush(stdout); |
| 2314 | case '\n': |
| 2315 | case '\000': |
| 2316 | goto reread; |
| 2317 | default: |
| 2318 | break; |
| 2319 | } |
| 2320 | answer = strtol(inbuf, &endptr, 10); |
| 2321 | if (errno == ERANGE) |
| 2322 | { |
| 2323 | printf(_("Number out of range!\n")); |
| 2324 | goto reread; |
| 2325 | } |
| 2326 | if (endptr == inbuf) |
| 2327 | { |
| 2328 | printf(_("I cannot understand what you typed!\n")); |
| 2329 | fflush(stdout); |
| 2330 | goto reread; |
| 2331 | } |
| 2332 | if (answer < 1 || answer > alignment_choices) |
| 2333 | { |
| 2334 | printf(_("The best pair of lines should be numbered between 1 and %d.\n"), |
| 2335 | alignment_choices); |
| 2336 | fflush(stdout); |
| 2337 | goto reread; |
| 2338 | } |
| 2339 | do_remote_cmd("DA", 4, 0, curpass - 1, 0, answer); |
| 2340 | } |
| 2341 | printf(_("Attempting to set alignment...")); |
| 2342 | if (do_print_cmd()) |
| 2343 | printer_error(); |
| 2344 | printf(_("succeeded.\n")); |
| 2345 | printf(_("Please verify that the alignment is correct. After the alignment pattern\n" |
| 2346 | "is printed again, please ensure that the best pattern for each line is\n" |
| 2347 | "pattern %d. If it is not, you should repeat the process to get the best\n" |
| 2348 | "quality printing.\n"), (alignment_choices + 1) / 2); |
| 2349 | printf(_("Please insert a fresh sheet of paper.\n")); |
| 2350 | (void) do_get_input(_("Press enter to continue > ")); |
| 2351 | initialize_print_cmd(1); |
| 2352 | for (curpass = 0; curpass < alignment_passes; curpass++) |
| 2353 | do_remote_cmd("DT", 3, 0, curpass, 0); |
| 2354 | if (do_print_cmd()) |
| 2355 | printer_error(); |
| 2356 | } while (!do_final_alignment()); |
| 2357 | exit(0); |
| 2358 | } |
| 2359 | |
| 2360 | char * |
| 2361 | do_get_input (const char *prompt) |
| 2362 | { |
| 2363 | static char *input = NULL; |
| 2364 | #if (HAVE_LIBREADLINE == 0 || !defined HAVE_READLINE_READLINE_H) |
| 2365 | char *fgets_status; |
| 2366 | #endif |
| 2367 | /* free only if previously allocated */ |
| 2368 | if (input) |
| 2369 | { |
| 2370 | stp_free (input); |
| 2371 | input = NULL; |
| 2372 | } |
| 2373 | #if (HAVE_LIBREADLINE > 0 && defined HAVE_READLINE_READLINE_H) |
| 2374 | /* get input with libreadline, if present */ |
| 2375 | input = readline (prompt); |
| 2376 | /* if input, add to history list */ |
| 2377 | #ifdef HAVE_READLINE_HISTORY_H |
| 2378 | if (input && *input) |
| 2379 | { |
| 2380 | add_history (input); |
| 2381 | } |
| 2382 | #endif |
| 2383 | #else |
| 2384 | /* no libreadline; use fgets instead */ |
| 2385 | input = stp_malloc (sizeof (char) * BUFSIZ); |
| 2386 | memset(input, 0, BUFSIZ); |
| 2387 | printf ("%s", prompt); |
| 2388 | fgets_status = fgets (input, BUFSIZ, stdin); |
| 2389 | if (fgets_status == NULL) |
| 2390 | { |
| 2391 | fprintf (stdout, _("Error in input\n")); |
| 2392 | return (NULL); |
| 2393 | } |
| 2394 | else if (strlen (input) == 1 && input[0] == '\n') |
| 2395 | { |
| 2396 | /* user just hit enter: empty input buffer */ |
| 2397 | /* remove line feed */ |
| 2398 | input[0] = '\0'; |
| 2399 | } |
| 2400 | else |
| 2401 | { |
| 2402 | /* remove line feed */ |
| 2403 | input[strlen (input) - 1] = '\0'; |
| 2404 | } |
| 2405 | #endif |
| 2406 | return (input); |
| 2407 | } |