1 /* SANE - Scanner Access Now Easy.
2
3 Copyright (C) 2011-2020 Rolf Bensch <rolf at bensch hyphen online dot de>
4 Copyright (C) 2007-2008 Nicolas Martin, <nicols-guest at alioth dot debian dot org>
5 Copyright (C) 2006-2007 Wittawat Yamwong <wittawat@web.de>
6
7 This file is part of the SANE package.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <https://www.gnu.org/licenses/>.
21
22 As a special exception, the authors of SANE give permission for
23 additional uses of the libraries contained in this release of SANE.
24
25 The exception is that, if you link a SANE library with other files
26 to produce an executable, this does not by itself cause the
27 resulting executable to be covered by the GNU General Public
28 License. Your use of that executable is in no way restricted on
29 account of linking the SANE library code into it.
30
31 This exception does not, however, invalidate any other reasons why
32 the executable file might be covered by the GNU General Public
33 License.
34
35 If you submit changes to SANE to the maintainers to be included in
36 a subsequent release, you agree by submitting the changes that
37 those changes may be distributed with this exception intact.
38
39 If you write modifications of your own for SANE, it is your choice
40 whether to permit this exception to apply to your modifications.
41 If you do not wish that, delete this exception notice.
42 */
43 #include "../include/sane/config.h"
44
45 #include <errno.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #ifdef USE_PTHREAD
49 # include <pthread.h>
50 #endif
51 #include <signal.h> /* sigaction(POSIX) */
52 #include <unistd.h> /* POSIX: write read close pipe */
53 #ifdef HAVE_FCNTL_H
54 # include <fcntl.h>
55 #endif
56
57 #include "pixma_rename.h"
58 #include "pixma.h"
59
60 # define DEBUG_NOT_STATIC
61 # include "../include/sane/sane.h"
62 # include "../include/sane/sanei.h"
63 # include "../include/sane/saneopts.h"
64 # include "../include/sane/sanei_thread.h"
65 # include "../include/sane/sanei_backend.h"
66 # include "../include/sane/sanei_config.h"
67 # include "../include/sane/sanei_jpeg.h"
68 # include "../include/sane/sanei_usb.h"
69
70 #ifdef NDEBUG
71 # define PDBG(x)
72 #else
73 # define PDBG(x) IF_DBG(x)
74 #endif /* NDEBUG */
75
76 #ifdef __GNUC__
77 # define UNUSED(v) (void) v
78 #else
79 # define UNUSED(v)
80 #endif
81
82 #define DECL_CTX pixma_sane_t *ss = check_handle(h)
83 #define OPT_IN_CTX ss->opt
84 #define SOD(opt) OPT_IN_CTX[opt].sod
85 #define OVAL(opt) OPT_IN_CTX[opt].val
86 #define AUTO_GAMMA 2.2
87
88 /* pixma_sane_options.h generated by
89 * scripts/pixma_gen_options.py h < pixma.c > pixma_sane_options.h
90 */
91 #include "pixma_sane_options.h"
92
93 #define BUTTON_GROUP_SIZE ( opt_adf_orientation - opt_button_1 + 1 )
94 #define BUTTON_GROUP_INDEX(x) ( x - opt_button_1 )
95
96 typedef struct pixma_sane_t
97 {
98 struct pixma_sane_t *next;
99 pixma_t *s;
100 pixma_scan_param_t sp;
101 SANE_Bool cancel;
102
103 /* valid states: idle, !idle && scanning, !idle && !scanning */
104 SANE_Bool idle;
105 SANE_Bool scanning;
106 SANE_Status last_read_status; /* valid if !idle && !scanning */
107
108 option_descriptor_t opt[opt_last];
109 char button_option_is_cached[BUTTON_GROUP_SIZE];
110 SANE_Range xrange, yrange;
111 SANE_Word dpi_list[9]; /* up to 9600 dpi */
112 SANE_String_Const mode_list[6];
113 pixma_scan_mode_t mode_map[6];
114 uint8_t gamma_table[4096];
115 SANE_String_Const source_list[4];
116 pixma_paper_source_t source_map[4];
117 SANE_String_Const calibrate_list[PIXMA_CALIBRATE_NUM_OPTS + 1];
118 pixma_calibrate_option_t calibrate_map[PIXMA_CALIBRATE_NUM_OPTS + 1];
119
120 unsigned byte_pos_in_line, output_line_size;
121 uint64_t image_bytes_read;
122 unsigned page_count; /* valid for ADF */
123
124 SANE_Pid reader_taskid;
125 int wpipe, rpipe;
126 SANE_Bool reader_stop;
127
128 /* Valid for JPEG source */
129 djpeg_dest_ptr jdst;
130 struct jpeg_decompress_struct jpeg_cinfo;
131 struct jpeg_error_mgr jpeg_err;
132 SANE_Bool jpeg_header_seen;
133 } pixma_sane_t;
134
135 typedef struct
136 {
137 struct jpeg_source_mgr jpeg;
138
139 pixma_sane_t *s;
140 JOCTET *buffer;
141
142 SANE_Byte *linebuffer;
143 SANE_Int linebuffer_size;
144 SANE_Int linebuffer_index;
145 } pixma_jpeg_src_mgr;
146
147
148 static const char vendor_str[] = "CANON";
149 static const char type_str[] = "multi-function peripheral";
150
151 static pixma_sane_t *first_scanner = NULL;
152 static const SANE_Device **dev_list = NULL;
153 static const char* conf_devices[MAX_CONF_DEVICES];
154
mark_all_button_options_cached(struct pixma_sane_t * ss)155 static void mark_all_button_options_cached ( struct pixma_sane_t * ss )
156 {
157 int i;
158 for (i = 0; i < (opt__group_5 - opt_button_1); i++ )
159 ss -> button_option_is_cached[i] = 1;
160 }
161
config_attach_pixma(SANEI_Config __sane_unused__ * config,const char * devname,void __sane_unused__ * data)162 static SANE_Status config_attach_pixma(SANEI_Config __sane_unused__ * config,
163 const char *devname,
164 void __sane_unused__ *data)
165 {
166 int i;
167 for (i=0; i < (MAX_CONF_DEVICES -1); i++)
168 {
169 if(conf_devices[i] == NULL)
170 {
171 conf_devices[i] = strdup(devname);
172 return SANE_STATUS_GOOD;
173 }
174 }
175 return SANE_STATUS_INVAL;
176 }
177
178 static SANE_Status
map_error(int error)179 map_error (int error)
180 {
181 if (error >= 0)
182 return SANE_STATUS_GOOD;
183
184 switch (error)
185 {
186 case PIXMA_ENOMEM:
187 return SANE_STATUS_NO_MEM;
188 case PIXMA_ECANCELED:
189 return SANE_STATUS_CANCELLED;
190 case PIXMA_EBUSY:
191 return SANE_STATUS_DEVICE_BUSY;
192 case PIXMA_EINVAL:
193 return SANE_STATUS_INVAL;
194 case PIXMA_EACCES:
195 return SANE_STATUS_ACCESS_DENIED;
196 case PIXMA_EPAPER_JAMMED:
197 return SANE_STATUS_JAMMED;
198 case PIXMA_ENO_PAPER:
199 return SANE_STATUS_NO_DOCS;
200 case PIXMA_ECOVER_OPEN:
201 return SANE_STATUS_COVER_OPEN;
202 case PIXMA_ENOTSUP:
203 return SANE_STATUS_UNSUPPORTED;
204 case PIXMA_EPROTO:
205 case PIXMA_ENODEV:
206 case PIXMA_EIO:
207 case PIXMA_ETIMEDOUT:
208 return SANE_STATUS_IO_ERROR;
209 }
210 PDBG (pixma_dbg (1, "BUG: unmapped error %d\n", error));
211 return SANE_STATUS_IO_ERROR;
212 }
213
214 static int
getenv_atoi(const char * name,int def)215 getenv_atoi (const char *name, int def)
216 {
217 const char *str = getenv (name);
218 return (str) ? atoi (str) : def;
219 }
220
221 #define CONST_CAST(t,x) (t)(x)
222
223 static void
free_block(const void * ptr)224 free_block (const void * ptr)
225 {
226 free (CONST_CAST (void *, ptr));
227 }
228
229 static void
cleanup_device_list(void)230 cleanup_device_list (void)
231 {
232 if (dev_list)
233 {
234 int i;
235 for (i = 0; dev_list[i]; i++)
236 {
237 free_block ((const void *) dev_list[i]->name);
238 free_block ((const void *) dev_list[i]->model);
239 free_block ((const void *) dev_list[i]);
240 }
241 }
242 free (dev_list);
243 dev_list = NULL;
244 }
245
246 static void
find_scanners(SANE_Bool local_only)247 find_scanners (SANE_Bool local_only)
248 {
249 unsigned i, nscanners;
250
251 cleanup_device_list ();
252 nscanners = pixma_find_scanners (conf_devices, local_only);
253 PDBG (pixma_dbg (3, "pixma_find_scanners() found %u devices\n", nscanners));
254 dev_list =
255 (const SANE_Device **) calloc (nscanners + 1, sizeof (*dev_list));
256 if (!dev_list)
257 return;
258 for (i = 0; i != nscanners; i++)
259 {
260 SANE_Device *sdev = (SANE_Device *) calloc (1, sizeof (*sdev));
261 char *name, *model;
262 if (!sdev)
263 goto nomem;
264 name = strdup (pixma_get_device_id (i));
265 model = strdup (pixma_get_device_model (i));
266 if (!name || !model)
267 {
268 free (name);
269 free (model);
270 free (sdev);
271 goto nomem;
272 }
273 sdev->name = name;
274 sdev->model = model;
275 sdev->vendor = vendor_str;
276 sdev->type = type_str;
277 dev_list[i] = sdev;
278 }
279 /* dev_list is already NULL terminated by calloc(). */
280 return;
281
282 nomem:
283 PDBG (pixma_dbg (1, "WARNING:not enough memory for device list\n"));
284 return;
285 }
286
287 static pixma_sane_t *
check_handle(SANE_Handle h)288 check_handle (SANE_Handle h)
289 {
290 pixma_sane_t *p;
291
292 for (p = first_scanner; p && (SANE_Handle) p != h; p = p->next)
293 {
294 }
295 return p;
296 }
297
298 static void
update_button_state(pixma_sane_t * ss,SANE_Int * info)299 update_button_state (pixma_sane_t * ss, SANE_Int * info)
300 {
301 SANE_Int b1 = OVAL (opt_button_1).w;
302 SANE_Int b2 = OVAL (opt_button_2).w;
303 uint32_t ev = pixma_wait_event (ss->s, 300);
304 switch (ev & ~PIXMA_EV_ACTION_MASK)
305 {
306 case PIXMA_EV_BUTTON1:
307 b1 = 1;
308 break;
309 case PIXMA_EV_BUTTON2:
310 b2 = 1;
311 break;
312 }
313
314 if (b1 != OVAL (opt_button_1).w || b2 != OVAL (opt_button_2).w)
315 {
316 *info |= SANE_INFO_RELOAD_OPTIONS;
317 OVAL (opt_button_1).w = b1;
318 OVAL (opt_button_2).w = b2;
319 OVAL (opt_original).w = GET_EV_ORIGINAL(ev);
320 OVAL (opt_target).w = GET_EV_TARGET(ev);
321 OVAL (opt_scan_resolution).w = GET_EV_DPI(ev);
322 OVAL (opt_document_type).w = GET_EV_DOC(ev);
323 OVAL (opt_adf_status).w = GET_EV_STAT(ev);
324 OVAL (opt_adf_orientation).w = GET_EV_ORIENT(ev);
325 }
326 mark_all_button_options_cached(ss);
327 }
328
329 static SANE_Bool
enable_option(pixma_sane_t * ss,SANE_Int o,SANE_Bool enable)330 enable_option (pixma_sane_t * ss, SANE_Int o, SANE_Bool enable)
331 {
332 SANE_Word save = SOD (o).cap;
333 if (enable)
334 SOD (o).cap &= ~SANE_CAP_INACTIVE;
335 else
336 SOD (o).cap |= SANE_CAP_INACTIVE;
337 return (save != SOD (o).cap);
338 }
339
340 static void
clamp_value(pixma_sane_t * ss,SANE_Int n,void * v,SANE_Int * info)341 clamp_value (pixma_sane_t * ss, SANE_Int n, void *v, SANE_Int * info)
342 {
343 SANE_Option_Descriptor *sod = &SOD (n);
344 SANE_Word *va = (SANE_Word *) v;
345 const SANE_Range *range = sod->constraint.range;
346 int i, nmemb;
347
348 nmemb = sod->size / sizeof (SANE_Word);
349 for (i = 0; i < nmemb; i++)
350 {
351 SANE_Word value = va[i];
352 if (value < range->min)
353 {
354 value = range->min;
355 }
356 else if (value > range->max)
357 {
358 value = range->max;
359 }
360 if (range->quant != 0)
361 {
362 value = (value - range->min + range->quant / 2) /
363 range->quant * range->quant;
364 }
365 if (value != va[i])
366 {
367 va[i] = value;
368 *info |= SANE_INFO_INEXACT;
369 }
370 }
371 }
372
373 /* create dynamic mode_list
374 * ss: scanner device
375 * tpu = 0: flatbed or ADF mode
376 * 1 bit lineart, 8 bit grayscale and 24 bit color scans
377 * tpu = 1: TPU mode
378 * 16 bit grayscale and 48 bit color scans */
379 static void
create_mode_list(pixma_sane_t * ss)380 create_mode_list (pixma_sane_t * ss)
381 {
382 SANE_Bool tpu;
383 const pixma_config_t *cfg;
384 int i;
385
386 cfg = pixma_get_config (ss->s);
387 tpu = (ss->source_map[OVAL (opt_source).w] == PIXMA_SOURCE_TPU);
388
389 /* setup available mode */
390 i = 0;
391 ss->mode_list[i] = SANE_VALUE_SCAN_MODE_COLOR;
392 ss->mode_map[i] = PIXMA_SCAN_MODE_COLOR;
393 i++;
394 if (cfg->cap & PIXMA_CAP_GRAY)
395 {
396 ss->mode_list[i] = SANE_VALUE_SCAN_MODE_GRAY;
397 ss->mode_map[i] = PIXMA_SCAN_MODE_GRAY;
398 i++;
399 }
400 if (tpu && (cfg->cap & PIXMA_CAP_NEGATIVE))
401 {
402 ss->mode_list[i] = SANE_I18N ("Negative color");
403 ss->mode_map[i] = PIXMA_SCAN_MODE_NEGATIVE_COLOR;
404 i++;
405 if (cfg->cap & PIXMA_CAP_GRAY)
406 {
407 ss->mode_list[i] = SANE_I18N ("Negative gray");
408 ss->mode_map[i] = PIXMA_SCAN_MODE_NEGATIVE_GRAY;
409 i++;
410 }
411 }
412 if (tpu && (cfg->cap & PIXMA_CAP_TPUIR) == PIXMA_CAP_TPUIR)
413 {
414 ss->mode_list[i] = SANE_I18N ("Infrared");
415 ss->mode_map[i] = PIXMA_SCAN_MODE_TPUIR;
416 i++;
417 }
418 if (!tpu && (cfg->cap & PIXMA_CAP_48BIT))
419 {
420 ss->mode_list[i] = SANE_I18N ("48 bits color");
421 ss->mode_map[i] = PIXMA_SCAN_MODE_COLOR_48;
422 i++;
423 if (cfg->cap & PIXMA_CAP_GRAY)
424 {
425 ss->mode_list[i] = SANE_I18N ("16 bits gray");
426 ss->mode_map[i] = PIXMA_SCAN_MODE_GRAY_16;
427 i++;
428 }
429 }
430 if (!tpu && (cfg->cap & PIXMA_CAP_LINEART))
431 {
432 ss->mode_list[i] = SANE_VALUE_SCAN_MODE_LINEART;
433 ss->mode_map[i] = PIXMA_SCAN_MODE_LINEART;
434 i++;
435 }
436 /* terminate mode_list and mode_map */
437 ss->mode_list[i] = 0;
438 ss->mode_map[i] = 0;
439 }
440
441 /* create dynamic dpi_list
442 * ss: scanner device */
443 static void
create_dpi_list(pixma_sane_t * ss)444 create_dpi_list (pixma_sane_t * ss)
445 {
446 const pixma_config_t *cfg;
447 int i, j;
448 int min;
449 unsigned min_dpi;
450 unsigned max_dpi;
451
452 cfg = pixma_get_config (ss->s);
453
454 /* get min/max dpi */
455 max_dpi = cfg->xdpi;
456 min_dpi = 75;
457 if (ss->source_map[OVAL (opt_source).w] == PIXMA_SOURCE_TPU
458 && ss->mode_map[OVAL (opt_mode).w] == PIXMA_SCAN_MODE_TPUIR)
459 { /* IR mode */
460 /*PDBG (pixma_dbg (4, "*create_dpi_list***** TPUIR mode\n"));*/
461 min_dpi = (cfg->tpuir_min_dpi) ? cfg->tpuir_min_dpi : 75;
462 max_dpi = (cfg->tpuir_max_dpi) ? cfg->tpuir_max_dpi : cfg->xdpi;
463 }
464 else if (ss->source_map[OVAL (opt_source).w] == PIXMA_SOURCE_TPU
465 || ss->source_map[OVAL (opt_source).w] == PIXMA_SOURCE_ADF
466 || ss->source_map[OVAL (opt_source).w] == PIXMA_SOURCE_ADFDUP)
467 { /* ADF / TPU mode */
468 /*PDBG (pixma_dbg (4, "*create_dpi_list***** ADF/TPU mode\n"));*/
469 min_dpi = (cfg->adftpu_min_dpi) ? cfg->adftpu_min_dpi : 75;
470 max_dpi = (cfg->adftpu_max_dpi) ? cfg->adftpu_max_dpi : cfg->xdpi;
471 }
472 else if (ss->source_map[OVAL (opt_source).w] == PIXMA_SOURCE_FLATBED
473 && (ss->mode_map[OVAL (opt_mode).w] == PIXMA_SCAN_MODE_COLOR_48
474 || ss->mode_map[OVAL (opt_mode).w] == PIXMA_SCAN_MODE_GRAY_16))
475 { /* 48 bits flatbed */
476 /*PDBG (pixma_dbg (4, "*create_dpi_list***** 48 bits flatbed mode\n"));*/
477 min_dpi = (cfg->min_xdpi_16) ? cfg->min_xdpi_16 : 75;
478 }
479
480 /* set j for min. dpi
481 * 75 dpi: j = 0
482 * 150 dpi: j = 1 \
483 * 300 dpi: j = 2 |--> from cfg->adftpu_min_dpi or cfg->tpuir_min_dpi
484 * 600 dpi: j = 3 /
485 * */
486 j = -1;
487 min = min_dpi / 75;
488 do
489 {
490 j++;
491 min >>= 1;
492 }
493 while (min > 0);
494
495 /* create dpi_list
496 * use j for min. dpi */
497 i = 0;
498 do
499 {
500 i++; j++;
501 ss->dpi_list[i] = 75 * (1 << (j - 1)); /* 75 x 2^(j-1) */
502 }
503 while ((unsigned) ss->dpi_list[i] < max_dpi);
504 ss->dpi_list[0] = i;
505 /*PDBG (pixma_dbg (4, "*create_dpi_list***** min_dpi = %d, max_dpi = %d\n", min_dpi, max_dpi));*/
506 }
507
508
509 static void
create_calibrate_list(pixma_sane_t * ss)510 create_calibrate_list (pixma_sane_t * ss)
511 {
512 int i = 0;
513 ss->calibrate_list[i] = SANE_I18N ("Once");
514 ss->calibrate_map[i] = PIXMA_CALIBRATE_ONCE;
515 i++;
516 ss->calibrate_list[i] = SANE_I18N ("Always");
517 ss->calibrate_map[i] = PIXMA_CALIBRATE_ALWAYS;
518 i++;
519 ss->calibrate_list[i] = SANE_I18N ("Never");
520 ss->calibrate_map[i] = PIXMA_CALIBRATE_NEVER;
521 i++;
522 }
523
524 static void
select_value_from_list(pixma_sane_t * ss,SANE_Int n,void * v,SANE_Int * info)525 select_value_from_list (pixma_sane_t * ss, SANE_Int n, void *v,
526 SANE_Int * info)
527 {
528 SANE_Option_Descriptor *sod = &SOD (n);
529 SANE_Word *va = (SANE_Word *) v;
530 const SANE_Word *list = sod->constraint.word_list;
531 int i, j, nmemb;
532
533 nmemb = sod->size / sizeof (SANE_Word);
534 for (i = 0; i < nmemb; i++)
535 {
536 SANE_Word value = va[i];
537 SANE_Word mindelta = abs (value - list[1]);
538 SANE_Word nearest = list[1];
539 for (j = 2; j <= list[0]; j++)
540 {
541 SANE_Word delta = abs (value - list[j]);
542 if (delta < mindelta)
543 {
544 mindelta = delta;
545 nearest = list[j];
546 }
547 if (mindelta == 0)
548 break;
549 }
550 if (va[i] != nearest)
551 {
552 va[i] = nearest;
553 *info |= SANE_INFO_INEXACT;
554 }
555 }
556 }
557
558 static SANE_Status
control_scalar_option(pixma_sane_t * ss,SANE_Int n,SANE_Action a,void * v,SANE_Int * info)559 control_scalar_option (pixma_sane_t * ss, SANE_Int n, SANE_Action a, void *v,
560 SANE_Int * info)
561 {
562 option_descriptor_t *opt = &(OPT_IN_CTX[n]);
563 SANE_Word val;
564
565 /* PDBG (pixma_dbg (4, "*control_scalar_option***** n = %u, a = %u\n", n, a)); */
566
567 switch (a)
568 {
569 case SANE_ACTION_GET_VALUE:
570 switch (opt->sod.type)
571 {
572 case SANE_TYPE_BOOL:
573 case SANE_TYPE_INT:
574 case SANE_TYPE_FIXED:
575 *(SANE_Word *) v = opt->val.w;
576 break;
577 default:
578 return SANE_STATUS_UNSUPPORTED;
579 }
580 return SANE_STATUS_GOOD;
581
582 case SANE_ACTION_SET_VALUE:
583 switch (opt->sod.type)
584 {
585 case SANE_TYPE_BOOL:
586 val = *(SANE_Word *) v;
587 if (val != SANE_TRUE && val != SANE_FALSE)
588 return SANE_STATUS_INVAL;
589 opt->val.w = val;
590 break;
591 case SANE_TYPE_INT:
592 case SANE_TYPE_FIXED:
593 if (opt->sod.constraint_type == SANE_CONSTRAINT_RANGE)
594 clamp_value (ss, n, v, info);
595 else if (opt->sod.constraint_type == SANE_CONSTRAINT_WORD_LIST)
596 select_value_from_list (ss, n, v, info);
597 opt->val.w = *(SANE_Word *) v;
598 break;
599 default:
600 return SANE_STATUS_UNSUPPORTED;
601 }
602 *info |= opt->info;
603 return SANE_STATUS_GOOD;
604
605 case SANE_ACTION_SET_AUTO:
606 switch (opt->sod.type)
607 {
608 case SANE_TYPE_BOOL:
609 case SANE_TYPE_INT:
610 case SANE_TYPE_FIXED:
611 opt->val.w = opt->def.w;
612 break;
613 default:
614 return SANE_STATUS_UNSUPPORTED;
615 }
616 *info |= opt->info;
617 return SANE_STATUS_GOOD;
618 }
619 return SANE_STATUS_UNSUPPORTED;
620 }
621
622 static SANE_Status
control_string_option(pixma_sane_t * ss,SANE_Int n,SANE_Action a,void * v,SANE_Int * info)623 control_string_option (pixma_sane_t * ss, SANE_Int n, SANE_Action a, void *v,
624 SANE_Int * info)
625 {
626 option_descriptor_t *opt = &(OPT_IN_CTX[n]);
627 const SANE_String_Const *slist = opt->sod.constraint.string_list;
628 SANE_String str = (SANE_String) v;
629
630 /* PDBG (pixma_dbg (4, "*control_string_option***** n = %u, a = %u\n", n, a)); */
631
632 if (opt->sod.constraint_type == SANE_CONSTRAINT_NONE)
633 {
634 switch (a)
635 {
636 case SANE_ACTION_GET_VALUE:
637 strcpy (str, opt->val.s);
638 break;
639 case SANE_ACTION_SET_AUTO:
640 str = opt->def.s;
641 /* fall through */
642 case SANE_ACTION_SET_VALUE:
643 strncpy (opt->val.s, str, opt->sod.size - 1);
644 *info |= opt->info;
645 break;
646 }
647 return SANE_STATUS_GOOD;
648 }
649 else
650 {
651 int i;
652
653 switch (a)
654 {
655 case SANE_ACTION_GET_VALUE:
656 strcpy (str, slist[opt->val.w]);
657 break;
658 case SANE_ACTION_SET_AUTO:
659 str = opt->def.ptr;
660 /* fall through */
661 case SANE_ACTION_SET_VALUE:
662 i = 0;
663 while (slist[i] && strcasecmp (str, slist[i]) != 0)
664 i++;
665 if (!slist[i])
666 return SANE_STATUS_INVAL;
667 if (strcmp (slist[i], str) != 0)
668 {
669 strcpy (str, slist[i]);
670 *info |= SANE_INFO_INEXACT;
671 }
672 opt->val.w = i;
673 *info |= opt->info;
674 break;
675 }
676 return SANE_STATUS_GOOD;
677 }
678 }
679
680 static SANE_Status
control_option(pixma_sane_t * ss,SANE_Int n,SANE_Action a,void * v,SANE_Int * info)681 control_option (pixma_sane_t * ss, SANE_Int n,
682 SANE_Action a, void *v, SANE_Int * info)
683 {
684 SANE_Option_Descriptor *sod = &SOD (n);
685 int result, i;
686 const pixma_config_t *cfg;
687 SANE_Int dummy;
688
689 /* info may be null, better to set a dummy here then test everywhere */
690 if (info == NULL)
691 info = &dummy;
692
693 cfg = pixma_get_config (ss->s);
694
695 /* PDBG (pixma_dbg (4, "*control_option***** n = %u, a = %u\n", n, a)); */
696
697 /* first deal with options that require special treatment */
698 result = SANE_STATUS_UNSUPPORTED;
699 switch (n)
700 {
701 case opt_gamma_table:
702 {
703 int table_size = sod->size / sizeof (SANE_Word);
704 int byte_cnt = table_size == 1024 ? 2 : 1;
705
706 switch (a)
707 {
708 case SANE_ACTION_SET_VALUE:
709 PDBG (pixma_dbg (4, "*control_option***** opt_gamma_table: SANE_ACTION_SET_VALUE with %d values ***** \n", table_size));
710 clamp_value (ss, n, v, info);
711 if (byte_cnt == 1)
712 {
713 for (i = 0; i < table_size; i++)
714 ss->gamma_table[i] = *((SANE_Int *) v + i);
715 }
716 else
717 {
718 for (i = 0; i < table_size; i++)
719 {
720 ss->gamma_table[i * 2] = *((SANE_Int *) v + i);
721 ss->gamma_table[i * 2 + 1] = *((uint8_t *)((SANE_Int *) v + i) + 1);
722 }
723 }
724 /* PDBG (pixma_hexdump (4, (uint8_t *)v, table_size * 4)); */
725 /* PDBG (pixma_hexdump (4, ss->gamma_table, table_size * byte_cnt)); */
726 break;
727 case SANE_ACTION_GET_VALUE:
728 PDBG (pixma_dbg (4, "*control_option***** opt_gamma_table: SANE_ACTION_GET_VALUE ***** \n"));
729 if (byte_cnt == 1)
730 {
731 for (i = 0; i < table_size; i++)
732 *((SANE_Int *) v + i) = ss->gamma_table[i];
733 }
734 else
735 {
736 for (i = 0; i < table_size; i++)
737 {
738 *((SANE_Int *) v + i) = ss->gamma_table[i * 2];
739 *((uint8_t *)((SANE_Int *) v + i) + 1) = ss->gamma_table[i * 2 + 1];
740 }
741 }
742 break;
743 case SANE_ACTION_SET_AUTO:
744 PDBG (pixma_dbg (4, "*control_option***** opt_gamma_table: SANE_ACTION_SET_AUTO with gamma=%f ***** \n",
745 SANE_UNFIX (OVAL (opt_gamma).w)));
746 pixma_fill_gamma_table (SANE_UNFIX (OVAL (opt_gamma).w),
747 ss->gamma_table, table_size);
748 /* PDBG (pixma_hexdump (4, ss->gamma_table, table_size * byte_cnt)); */
749 break;
750 default:
751 return SANE_STATUS_UNSUPPORTED;
752 }
753 return SANE_STATUS_GOOD;
754 }
755
756 case opt_button_update:
757 if (a == SANE_ACTION_SET_VALUE)
758 {
759 update_button_state (ss, info);
760 return SANE_STATUS_GOOD;
761 }
762 else
763 {
764 return SANE_STATUS_INVAL;
765 }
766 break;
767 case opt_button_1:
768 case opt_button_2:
769 case opt_original:
770 case opt_target:
771 case opt_scan_resolution:
772 case opt_document_type:
773 case opt_adf_status:
774 case opt_adf_orientation:
775 /* poll scanner if option is not cached */
776 if (! ss->button_option_is_cached[ BUTTON_GROUP_INDEX(n) ] )
777 update_button_state (ss, info);
778 /* mark this option as read */
779 ss->button_option_is_cached[ BUTTON_GROUP_INDEX(n) ] = 0;
780 }
781
782 /* now deal with getting and setting of options */
783 switch (SOD (n).type)
784 {
785 case SANE_TYPE_BOOL:
786 case SANE_TYPE_INT:
787 case SANE_TYPE_FIXED:
788 result = control_scalar_option (ss, n, a, v, info);
789 break;
790 case SANE_TYPE_STRING:
791 result = control_string_option (ss, n, a, v, info);
792 break;
793 case SANE_TYPE_BUTTON:
794 case SANE_TYPE_GROUP:
795 PDBG (pixma_dbg (1, "BUG:control_option():Unhandled option\n"));
796 result = SANE_STATUS_INVAL;
797 break;
798 }
799 if (result != SANE_STATUS_GOOD)
800 return result;
801
802 /* deal with dependencies between options */
803 switch (n)
804 {
805 case opt_custom_gamma:
806 if (a == SANE_ACTION_SET_VALUE || a == SANE_ACTION_SET_AUTO)
807 {
808 if (enable_option (ss, opt_gamma_table, OVAL (opt_custom_gamma).b))
809 *info |= SANE_INFO_RELOAD_OPTIONS;
810 if (OVAL (opt_custom_gamma).b)
811 sane_control_option (ss, opt_gamma_table, SANE_ACTION_SET_AUTO,
812 NULL, NULL);
813
814 }
815 break;
816 case opt_gamma:
817 if (a == SANE_ACTION_SET_VALUE || a == SANE_ACTION_SET_AUTO)
818 {
819 int table_size = SOD (opt_gamma_table).size / sizeof(SANE_Word);
820 PDBG (pixma_dbg (4, "*control_option***** gamma = %f *\n",
821 SANE_UNFIX (OVAL (opt_gamma).w)));
822 PDBG (pixma_dbg (4, "*control_option***** table size = %d *\n",
823 (int)(SOD (opt_gamma_table).size / sizeof (SANE_Word))));
824 pixma_fill_gamma_table (SANE_UNFIX (OVAL (opt_gamma).w),
825 ss->gamma_table, table_size);
826 /* PDBG (pixma_hexdump (4, ss->gamma_table,
827 table_size == 1024 ? 2048 : table_size)); */
828 }
829 break;
830 case opt_mode:
831 if (cfg->cap & (PIXMA_CAP_48BIT|PIXMA_CAP_LINEART|PIXMA_CAP_TPUIR)
832 && (a == SANE_ACTION_SET_VALUE || a == SANE_ACTION_SET_AUTO))
833 { /* new mode selected: Color, Gray, ... */
834 /* PDBG (pixma_dbg (4, "*control_option***** mode = %u *\n",
835 ss->mode_map[OVAL (opt_mode).w])); */
836 /* recreate dynamic lists */
837 create_dpi_list (ss);
838 if (ss->mode_map[OVAL (opt_mode).w] == PIXMA_SCAN_MODE_LINEART)
839 { /* lineart */
840 enable_option (ss, opt_threshold, SANE_TRUE);
841 enable_option (ss, opt_threshold_curve, SANE_TRUE);
842 }
843 else
844 { /* all other modes */
845 enable_option (ss, opt_threshold, SANE_FALSE);
846 enable_option (ss, opt_threshold_curve, SANE_FALSE);
847 }
848 *info |= SANE_INFO_RELOAD_OPTIONS;
849 }
850 break;
851 case opt_source:
852 if ((cfg->cap & (PIXMA_CAP_ADF|PIXMA_CAP_ADFDUP|PIXMA_CAP_TPU))
853 && (a == SANE_ACTION_SET_VALUE || a == SANE_ACTION_SET_AUTO))
854 { /* new source selected: flatbed, ADF, TPU, ... */
855 /* to avoid fatal errors,
856 * select first entry of dynamic mode_list
857 * identifiers are unknown here */
858 OVAL (opt_mode).w = ss->mode_map[0];
859 /* recreate dynamic lists */
860 create_mode_list (ss);
861 create_dpi_list (ss);
862 /* to avoid fatal errors,
863 * select first entry of dynamic dpi_list
864 * identifiers are unknown here */
865 OVAL (opt_resolution).w = ss->dpi_list[1];
866 if (ss->mode_map[OVAL (opt_mode).w] == PIXMA_SCAN_MODE_LINEART)
867 { /* lineart */
868 enable_option (ss, opt_threshold, SANE_TRUE);
869 enable_option (ss, opt_threshold_curve, SANE_TRUE);
870 }
871 else
872 { /* all other modes */
873 enable_option (ss, opt_threshold, SANE_FALSE);
874 enable_option (ss, opt_threshold_curve, SANE_FALSE);
875 }
876 if (cfg->cap & (PIXMA_CAP_ADF_WAIT))
877 { /* adf-wait */
878 enable_option (ss, opt_adf_wait, SANE_TRUE);
879 }
880 else
881 { /* disable adf-wait */
882 enable_option (ss, opt_adf_wait, SANE_FALSE);
883 }
884 *info |= SANE_INFO_RELOAD_OPTIONS;
885 }
886 break;
887 }
888
889 return result;
890 }
891
892 #ifndef NDEBUG
893 static void
print_scan_param(int level,const pixma_scan_param_t * sp)894 print_scan_param (int level, const pixma_scan_param_t * sp)
895 {
896 pixma_dbg (level, "Scan parameters\n");
897 pixma_dbg (level, " line_size=%"PRIu64" image_size=%"PRIu64" channels=%u depth=%u\n",
898 sp->line_size, sp->image_size, sp->channels, sp->depth);
899 pixma_dbg (level, " dpi=%ux%u offset=(%u,%u) dimension=%ux%u\n",
900 sp->xdpi, sp->ydpi, sp->x, sp->y, sp->w, sp->h);
901 pixma_dbg (level, " gamma=%f gamma_table=%p source=%d\n", sp->gamma,
902 (void *) sp->gamma_table, sp->source);
903 pixma_dbg (level, " adf-wait=%d\n", sp->adf_wait);
904 }
905 #endif
906
907 static int
calc_scan_param(pixma_sane_t * ss,pixma_scan_param_t * sp)908 calc_scan_param (pixma_sane_t * ss, pixma_scan_param_t * sp)
909 {
910 int x1, y1, x2, y2;
911 int error;
912
913 memset (sp, 0, sizeof (*sp));
914
915 sp->channels = (OVAL (opt_mode).w == 0) ? 3 : 1;
916 sp->depth = (OVAL (opt_mode).w == 2) ? 1 : 8;
917 sp->xdpi = sp->ydpi = OVAL (opt_resolution).w;
918
919 #define PIXEL(x,dpi) (int)((SANE_UNFIX(x) / 25.4 * (dpi)) + 0.5)
920 x1 = PIXEL (OVAL (opt_tl_x).w, sp->xdpi);
921 x2 = PIXEL (OVAL (opt_br_x).w, sp->xdpi);
922 if (x2 < x1)
923 {
924 int temp = x1;
925 x1 = x2;
926 x2 = temp;
927 }
928 y1 = PIXEL (OVAL (opt_tl_y).w, sp->ydpi);
929 y2 = PIXEL (OVAL (opt_br_y).w, sp->ydpi);
930 if (y2 < y1)
931 {
932 int temp = y1;
933 y1 = y2;
934 y2 = temp;
935 }
936 #undef PIXEL
937 sp->x = x1;
938 sp->y = y1;
939 sp->w = x2 - x1;
940 sp->h = y2 - y1;
941 if (sp->w == 0)
942 sp->w = 1;
943 if (sp->h == 0)
944 sp->h = 1;
945 sp->tpu_offset_added = 0;
946
947 sp->gamma = SANE_UNFIX (OVAL (opt_gamma).w);
948 sp->gamma_table = ss->gamma_table;
949 sp->source = ss->source_map[OVAL (opt_source).w];
950 sp->mode = ss->mode_map[OVAL (opt_mode).w];
951 sp->adf_pageid = ss->page_count;
952 sp->threshold = 2.55 * OVAL (opt_threshold).w;
953 sp->threshold_curve = OVAL (opt_threshold_curve).w;
954 sp->adf_wait = OVAL (opt_adf_wait).w;
955 sp->calibrate = ss->calibrate_map[OVAL (opt_calibrate).w];
956
957 error = pixma_check_scan_param (ss->s, sp);
958 if (error < 0)
959 {
960 PDBG (pixma_dbg (1, "BUG:calc_scan_param() failed %d\n", error));
961 PDBG (print_scan_param (1, sp));
962 }
963 return error;
964 }
965
966 static void
init_option_descriptors(pixma_sane_t * ss)967 init_option_descriptors (pixma_sane_t * ss)
968 {
969 const pixma_config_t *cfg;
970 int i;
971
972 cfg = pixma_get_config (ss->s);
973
974 /* PDBG (pixma_dbg (4, "*init_option_descriptors*****\n")); */
975
976 /* setup range for the scan area. */
977 ss->xrange.min = SANE_FIX (0);
978 ss->xrange.max = SANE_FIX (cfg->width / 75.0 * 25.4);
979 ss->xrange.quant = SANE_FIX (0);
980
981 ss->yrange.min = SANE_FIX (0);
982 ss->yrange.max = SANE_FIX (cfg->height / 75.0 * 25.4);
983 ss->yrange.quant = SANE_FIX (0);
984
985 /* mode_list and source_list were already NULL-terminated,
986 * because the whole pixma_sane_t was cleared during allocation. */
987
988 /* setup available mode. */
989 create_mode_list (ss);
990
991 /* setup dpi up to the value supported by the scanner. */
992 create_dpi_list (ss);
993
994 /* setup paper source */
995 i = 0;
996 ss->source_list[i] = SANE_I18N ("Flatbed");
997 ss->source_map[i] = PIXMA_SOURCE_FLATBED;
998 i++;
999 if (cfg->cap & PIXMA_CAP_ADF)
1000 {
1001 ss->source_list[i] = SANE_I18N ("Automatic Document Feeder");
1002 ss->source_map[i] = PIXMA_SOURCE_ADF;
1003 i++;
1004 }
1005 if ((cfg->cap & PIXMA_CAP_ADFDUP) == PIXMA_CAP_ADFDUP)
1006 {
1007 ss->source_list[i] = SANE_I18N ("ADF Duplex");
1008 ss->source_map[i] = PIXMA_SOURCE_ADFDUP;
1009 i++;
1010 }
1011 if (cfg->cap & PIXMA_CAP_TPU)
1012 {
1013 ss->source_list[i] = SANE_I18N ("Transparency Unit");
1014 ss->source_map[i] = PIXMA_SOURCE_TPU;
1015 i++;
1016 }
1017
1018 create_calibrate_list (ss);
1019
1020 build_option_descriptors (ss);
1021
1022 /* Enable options that are available only in some scanners. */
1023 if (cfg->cap & PIXMA_CAP_GAMMA_TABLE)
1024 {
1025 SANE_Option_Descriptor *sod = &SOD (opt_gamma_table);
1026
1027 /* some scanners have a large gamma table with 4096 entries */
1028 if (cfg->cap & PIXMA_CAP_GT_4096)
1029 {
1030 static const SANE_Range constraint_gamma_table_4096 = { 0,0xff,0 };
1031 sod->desc = SANE_I18N("Gamma-correction table with 4096 entries. In color mode this option equally affects the red, green, and blue channels simultaneously (i.e., it is an intensity gamma table).");
1032 sod->size = 4096 * sizeof(SANE_Word);
1033 sod->constraint.range = &constraint_gamma_table_4096;
1034 }
1035
1036 /* PDBG (pixma_dbg (4, "*%s***** PIXMA_CAP_GAMMA_TABLE ***** \n",
1037 __func__)); */
1038 /* PDBG (pixma_dbg (4, "%s: gamma_table_contraint.max = %d\n",
1039 __func__, sod->constraint.range->max)); */
1040 /* PDBG (pixma_dbg (4, "%s: gamma_table_size = %d\n",
1041 __func__, sod->size / sizeof(SANE_Word))); */
1042
1043 /* activate option gamma */
1044 enable_option (ss, opt_gamma, SANE_TRUE);
1045 sane_control_option (ss, opt_gamma, SANE_ACTION_SET_AUTO,
1046 NULL, NULL);
1047 /* activate option custom gamma table */
1048 enable_option (ss, opt_custom_gamma, SANE_TRUE);
1049 sane_control_option (ss, opt_custom_gamma, SANE_ACTION_SET_AUTO,
1050 NULL, NULL);
1051 }
1052 enable_option (ss, opt_button_controlled,
1053 ((cfg->cap & PIXMA_CAP_EVENTS) != 0));
1054 }
1055
1056 /* Writing to reader_ss outside reader_process() is a BUG! */
1057 static pixma_sane_t *reader_ss = NULL;
1058
1059 static void
reader_signal_handler(int sig)1060 reader_signal_handler (int sig)
1061 {
1062 if (reader_ss)
1063 {
1064 reader_ss->reader_stop = SANE_TRUE;
1065 /* reader process is ended by SIGTERM, so no cancel in this case */
1066 if (sig != SIGTERM)
1067 pixma_cancel (reader_ss->s);
1068 }
1069 }
1070
1071 static int
write_all(pixma_sane_t * ss,void * buf_,size_t size)1072 write_all (pixma_sane_t * ss, void *buf_, size_t size)
1073 {
1074 uint8_t *buf = (uint8_t *) buf_;
1075 int count;
1076
1077 while (size != 0 && !ss->reader_stop)
1078 {
1079 count = write (ss->wpipe, buf, size);
1080 if (count == -1 && errno != EINTR)
1081 break;
1082 if (count == -1 && errno == EINTR)
1083 continue;
1084 buf += count;
1085 size -= count;
1086 }
1087 return buf - (uint8_t *) buf_;
1088 }
1089
1090 /* NOTE: reader_loop() runs either in a separate thread or process. */
1091 static SANE_Status
reader_loop(pixma_sane_t * ss)1092 reader_loop (pixma_sane_t * ss)
1093 {
1094 void *buf;
1095 unsigned bufsize;
1096 int count = 0;
1097
1098 PDBG (pixma_dbg (3, "Reader task started\n"));
1099 /*bufsize = ss->sp.line_size + 1;*/ /* XXX: "odd" bufsize for testing pixma_read_image() */
1100 bufsize = ss->sp.line_size; /* bufsize EVEN needed by Xsane for 48 bits depth */
1101 buf = malloc (bufsize);
1102 if (!buf)
1103 {
1104 count = PIXMA_ENOMEM;
1105 goto done;
1106 }
1107
1108 count = pixma_activate_connection (ss->s);
1109 if (count < 0)
1110 goto done;
1111
1112 pixma_enable_background (ss->s, 1);
1113 if (OVAL (opt_button_controlled).b && ss->page_count == 0)
1114 {
1115 int start = 0;
1116 #ifndef NDEBUG
1117 pixma_dbg (1, "==== Button-controlled scan mode is enabled.\n");
1118 pixma_dbg (1, "==== To proceed, press 'SCAN' or 'COLOR' button. "
1119 "To cancel, press 'GRAY' or 'END' button.\n");
1120 #endif
1121 while (pixma_wait_event (ss->s, 10) != 0)
1122 {
1123 }
1124 while (!start)
1125 {
1126 uint32_t events;
1127 if (ss->reader_stop)
1128 {
1129 count = PIXMA_ECANCELED;
1130 goto done;
1131 }
1132 events = pixma_wait_event (ss->s, 1000);
1133 switch (events & ~PIXMA_EV_ACTION_MASK)
1134 {
1135 case PIXMA_EV_BUTTON1:
1136 start = 1;
1137 break;
1138 case PIXMA_EV_BUTTON2:
1139 count = PIXMA_ECANCELED;
1140 goto done;
1141 }
1142 }
1143 }
1144 count = pixma_scan (ss->s, &ss->sp);
1145 if (count >= 0)
1146 {
1147 while ((count = pixma_read_image (ss->s, buf, bufsize)) > 0)
1148 {
1149 if (write_all (ss, buf, count) != count)
1150 pixma_cancel (ss->s);
1151 }
1152 }
1153
1154 done:
1155 pixma_enable_background (ss->s, 0);
1156 pixma_deactivate_connection (ss->s);
1157 free (buf);
1158 close (ss->wpipe);
1159 ss->wpipe = -1;
1160 if (count >= 0)
1161 {
1162 PDBG (pixma_dbg (3, "Reader task terminated\n"));
1163 }
1164 else
1165 {
1166 PDBG (pixma_dbg
1167 (2, "Reader task terminated: %s\n", pixma_strerror (count)));
1168 }
1169 return map_error (count);
1170 }
1171
1172 static int
reader_process(void * arg)1173 reader_process (void *arg)
1174 {
1175 pixma_sane_t *ss = (pixma_sane_t *) arg;
1176 struct SIGACTION sa;
1177
1178 reader_ss = ss;
1179 memset (&sa, 0, sizeof (sa));
1180 sigemptyset (&sa.sa_mask);
1181 sa.sa_handler = reader_signal_handler;
1182 /* FIXME: which signal else? */
1183 sigaction (SIGHUP, &sa, NULL);
1184 sigaction (SIGINT, &sa, NULL);
1185 sigaction (SIGPIPE, &sa, NULL);
1186 sigaction (SIGTERM, &sa, NULL);
1187 close (ss->rpipe);
1188 ss->rpipe = -1;
1189 return reader_loop (ss);
1190 }
1191
1192 static int
reader_thread(void * arg)1193 reader_thread (void *arg)
1194 {
1195 pixma_sane_t *ss = (pixma_sane_t *) arg;
1196 #ifdef USE_PTHREAD
1197 /* Block SIGPIPE. We will handle this in reader_loop() by checking
1198 ss->reader_stop and the return value from write(). */
1199 sigset_t sigs;
1200 sigemptyset (&sigs);
1201 sigaddset (&sigs, SIGPIPE);
1202 pthread_sigmask (SIG_BLOCK, &sigs, NULL);
1203 #endif /* USE_PTHREAD */
1204 return reader_loop (ss);
1205 }
1206
1207 static SANE_Pid
terminate_reader_task(pixma_sane_t * ss,int * exit_code)1208 terminate_reader_task (pixma_sane_t * ss, int *exit_code)
1209 {
1210 SANE_Pid result, pid;
1211 int status = 0;
1212
1213 pid = ss->reader_taskid;
1214 if (!sanei_thread_is_valid (pid))
1215 return pid;
1216 if (sanei_thread_is_forked ())
1217 {
1218 sanei_thread_kill (pid);
1219 }
1220 else
1221 {
1222 ss->reader_stop = SANE_TRUE;
1223 /* pixma_cancel (ss->s); What is this for ? Makes end-of-scan buggy => removing */
1224 }
1225 result = sanei_thread_waitpid (pid, &status);
1226 sanei_thread_invalidate (ss->reader_taskid);
1227
1228 if (ss->sp.source != PIXMA_SOURCE_ADF && ss->sp.source != PIXMA_SOURCE_ADFDUP)
1229 ss->idle = SANE_TRUE;
1230
1231 if (result == pid)
1232 {
1233 if (exit_code)
1234 *exit_code = status;
1235 return pid;
1236 }
1237 else
1238 {
1239 PDBG (pixma_dbg (1, "WARNING:waitpid() failed %s\n", strerror (errno)));
1240 sanei_thread_invalidate (pid);
1241 return pid;
1242 }
1243 }
1244
1245 static int
start_reader_task(pixma_sane_t * ss)1246 start_reader_task (pixma_sane_t * ss)
1247 {
1248 int fds[2];
1249 SANE_Pid pid;
1250 int is_forked;
1251
1252 if (ss->rpipe != -1 || ss->wpipe != -1)
1253 {
1254 PDBG (pixma_dbg
1255 (1, "BUG:rpipe = %d, wpipe = %d\n", ss->rpipe, ss->wpipe));
1256 close (ss->rpipe);
1257 close (ss->wpipe);
1258 ss->rpipe = -1;
1259 ss->wpipe = -1;
1260 }
1261 if (sanei_thread_is_valid (ss->reader_taskid))
1262 {
1263 PDBG (pixma_dbg
1264 (1, "BUG:reader_taskid(%ld) != -1\n", (long) ss->reader_taskid));
1265 terminate_reader_task (ss, NULL);
1266 }
1267 if (pipe (fds) == -1)
1268 {
1269 PDBG (pixma_dbg (1, "ERROR:start_reader_task():pipe() failed %s\n",
1270 strerror (errno)));
1271 return PIXMA_ENOMEM;
1272 }
1273 ss->rpipe = fds[0];
1274 ss->wpipe = fds[1];
1275 ss->reader_stop = SANE_FALSE;
1276
1277 is_forked = sanei_thread_is_forked ();
1278 if (is_forked)
1279 {
1280 pid = sanei_thread_begin (reader_process, ss);
1281 if (sanei_thread_is_valid (pid))
1282 {
1283 close (ss->wpipe);
1284 ss->wpipe = -1;
1285 }
1286 }
1287 else
1288 {
1289 pid = sanei_thread_begin (reader_thread, ss);
1290 }
1291 if (!sanei_thread_is_valid (pid))
1292 {
1293 close (ss->wpipe);
1294 close (ss->rpipe);
1295 ss->wpipe = -1;
1296 ss->rpipe = -1;
1297 PDBG (pixma_dbg (1, "ERROR:unable to start reader task\n"));
1298 return PIXMA_ENOMEM;
1299 }
1300 PDBG (pixma_dbg (3, "Reader task id=%ld (%s)\n", (long) pid,
1301 (is_forked) ? "forked" : "threaded"));
1302 ss->reader_taskid = pid;
1303 return 0;
1304 }
1305
1306 /* libJPEG API callbacks */
1307 static void
jpeg_init_source(j_decompress_ptr __sane_unused__ cinfo)1308 jpeg_init_source(j_decompress_ptr __sane_unused__ cinfo)
1309 {
1310 /* No-op */
1311 }
1312
1313 static void
jpeg_term_source(j_decompress_ptr __sane_unused__ cinfo)1314 jpeg_term_source(j_decompress_ptr __sane_unused__ cinfo)
1315 {
1316 /* No-op */
1317 }
1318
1319 static boolean
jpeg_fill_input_buffer(j_decompress_ptr cinfo)1320 jpeg_fill_input_buffer(j_decompress_ptr cinfo)
1321 {
1322 pixma_jpeg_src_mgr *mgr = (pixma_jpeg_src_mgr *)cinfo->src;
1323 int size;
1324 int retry;
1325
1326 for (retry = 0; retry < 30; retry ++ )
1327 {
1328 size = read (mgr->s->rpipe, mgr->buffer, 1024);
1329 if (size == 0)
1330 {
1331 return FALSE;
1332 }
1333 else if (size < 0)
1334 {
1335 sleep (1);
1336 }
1337 else
1338 {
1339 mgr->jpeg.next_input_byte = mgr->buffer;
1340 mgr->jpeg.bytes_in_buffer = size;
1341 return TRUE;
1342 }
1343 }
1344
1345 return FALSE;
1346 }
1347
1348 static void
jpeg_skip_input_data(j_decompress_ptr cinfo,long num_bytes)1349 jpeg_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
1350 {
1351 pixma_jpeg_src_mgr *mgr = (pixma_jpeg_src_mgr *)cinfo->src;
1352
1353 if (num_bytes > 0)
1354 {
1355 /* Read and throw away extra */
1356 while (num_bytes > (long)mgr->jpeg.bytes_in_buffer)
1357 {
1358 num_bytes -= (long)mgr->jpeg.bytes_in_buffer;
1359 jpeg_fill_input_buffer(cinfo);
1360 }
1361
1362 /* Update jpeg info structure with leftover */
1363 mgr->jpeg.next_input_byte += (size_t) num_bytes;
1364 mgr->jpeg.bytes_in_buffer -= (size_t) num_bytes;
1365 }
1366 }
1367
1368 /* Pixma JPEG reader helpers */
1369 static SANE_Status
pixma_jpeg_start(pixma_sane_t * s)1370 pixma_jpeg_start(pixma_sane_t *s)
1371 {
1372 pixma_jpeg_src_mgr *mgr;
1373
1374 s->jpeg_cinfo.err = jpeg_std_error(&s->jpeg_err);
1375
1376 jpeg_create_decompress(&s->jpeg_cinfo);
1377
1378 s->jpeg_cinfo.src = (struct jpeg_source_mgr *)(*s->jpeg_cinfo.mem->alloc_small)((j_common_ptr)&s->jpeg_cinfo,
1379 JPOOL_PERMANENT, sizeof(pixma_jpeg_src_mgr));
1380
1381 memset(s->jpeg_cinfo.src, 0, sizeof(pixma_jpeg_src_mgr));
1382
1383 mgr = (pixma_jpeg_src_mgr *)s->jpeg_cinfo.src;
1384 mgr->s = s;
1385
1386 mgr->buffer = (JOCTET *)(*s->jpeg_cinfo.mem->alloc_small)((j_common_ptr)&s->jpeg_cinfo,
1387 JPOOL_PERMANENT,
1388 1024 * sizeof(JOCTET));
1389
1390 mgr->jpeg.init_source = jpeg_init_source;
1391 mgr->jpeg.fill_input_buffer = jpeg_fill_input_buffer;
1392 mgr->jpeg.skip_input_data = jpeg_skip_input_data;
1393 mgr->jpeg.resync_to_restart = jpeg_resync_to_restart;
1394 mgr->jpeg.term_source = jpeg_term_source;
1395 mgr->jpeg.bytes_in_buffer = 0;
1396 mgr->jpeg.next_input_byte = NULL;
1397
1398 s->jpeg_header_seen = 0;
1399
1400 return SANE_STATUS_GOOD;
1401 }
1402
1403 static SANE_Status
pixma_jpeg_read_header(pixma_sane_t * s)1404 pixma_jpeg_read_header(pixma_sane_t *s)
1405 {
1406 pixma_jpeg_src_mgr *src = (pixma_jpeg_src_mgr *)s->jpeg_cinfo.src;
1407
1408 if (jpeg_read_header(&s->jpeg_cinfo, TRUE))
1409 {
1410 s->jdst = sanei_jpeg_jinit_write_ppm(&s->jpeg_cinfo);
1411
1412 if (jpeg_start_decompress(&s->jpeg_cinfo))
1413 {
1414 int size;
1415
1416 DBG(3, "%s: w: %d, h: %d, components: %d\n",
1417 __func__,
1418 s->jpeg_cinfo.output_width, s->jpeg_cinfo.output_height,
1419 s->jpeg_cinfo.output_components);
1420
1421 size = s->jpeg_cinfo.output_width * s->jpeg_cinfo.output_components * 1;
1422
1423 src->linebuffer = (*s->jpeg_cinfo.mem->alloc_large)((j_common_ptr)&s->jpeg_cinfo,
1424 JPOOL_PERMANENT, size);
1425
1426 src->linebuffer_size = 0;
1427 src->linebuffer_index = 0;
1428
1429 s->jpeg_header_seen = 1;
1430
1431 return SANE_STATUS_GOOD;
1432 }
1433 else
1434 {
1435 DBG(0, "%s: decompression failed\n", __func__);
1436 return SANE_STATUS_IO_ERROR;
1437 }
1438 }
1439 else
1440 {
1441 DBG(0, "%s: cannot read JPEG header\n", __func__);
1442 return SANE_STATUS_IO_ERROR;
1443 }
1444 }
1445
1446 static void
pixma_jpeg_finish(pixma_sane_t * ss)1447 pixma_jpeg_finish(pixma_sane_t *ss)
1448 {
1449 jpeg_destroy_decompress(&ss->jpeg_cinfo);
1450 }
1451
1452 static void
pixma_jpeg_read(pixma_sane_t * ss,SANE_Byte * data,SANE_Int max_length,SANE_Int * length)1453 pixma_jpeg_read(pixma_sane_t *ss, SANE_Byte *data,
1454 SANE_Int max_length, SANE_Int *length)
1455 {
1456 struct jpeg_decompress_struct *cinfo = &ss->jpeg_cinfo;
1457 pixma_jpeg_src_mgr *src = (pixma_jpeg_src_mgr *)cinfo->src;
1458
1459 int l;
1460
1461 *length = 0;
1462
1463 /* copy from line buffer if available */
1464 if (src->linebuffer_size && src->linebuffer_index < src->linebuffer_size)
1465 {
1466 *length = src->linebuffer_size - src->linebuffer_index;
1467
1468 if (*length > max_length)
1469 *length = max_length;
1470
1471 memcpy(data, src->linebuffer + src->linebuffer_index, *length);
1472 src->linebuffer_index += *length;
1473
1474 return;
1475 }
1476
1477 if (cinfo->output_scanline >= cinfo->output_height)
1478 {
1479 *length = 0;
1480 return;
1481 }
1482
1483 /* scanlines of decompressed data will be in ss->jdst->buffer
1484 * only one line at time is supported
1485 */
1486
1487 l = jpeg_read_scanlines(cinfo, ss->jdst->buffer, 1);
1488 if (l == 0)
1489 return;
1490
1491 /* from ss->jdst->buffer to linebuffer
1492 * linebuffer holds width * bytesperpixel
1493 */
1494
1495 (*ss->jdst->put_pixel_rows)(cinfo, ss->jdst, 1, (char *)src->linebuffer);
1496
1497 *length = ss->sp.w * ss->sp.channels;
1498 /* Convert RGB into grayscale */
1499 if (ss->sp.channels == 1)
1500 {
1501 unsigned int i;
1502 unsigned char *d = (unsigned char *)src->linebuffer;
1503 unsigned char *s = (unsigned char *)src->linebuffer;
1504 for (i = 0; i < ss->sp.w; i++)
1505 {
1506 /* Using BT.709 luma formula, fixed-point */
1507 int sum = ( s[0]*2126 + s[1]*7152 + s[2]*722 );
1508 *d = sum / 10000;
1509 d ++;
1510 s += 3;
1511 }
1512 }
1513
1514 /* Maybe pack into lineary binary image */
1515 if (ss->sp.depth == 1)
1516 {
1517 *length /= 8;
1518 unsigned int i;
1519 unsigned char *d = (unsigned char *)src->linebuffer;
1520 unsigned char *s = (unsigned char *)src->linebuffer;
1521 unsigned char b = 0;
1522 for (i = 1; i < ss->sp.w + 1; i++)
1523 {
1524 if (*(s++) > 127)
1525 b = (b << 1) | 0;
1526 else
1527 b = (b << 1) | 1;
1528 if ((i % 8) == 0)
1529 *(d++) = b;
1530 }
1531 }
1532
1533 src->linebuffer_size = *length;
1534 src->linebuffer_index = 0;
1535
1536 if (*length > max_length)
1537 *length = max_length;
1538
1539 memcpy(data, src->linebuffer + src->linebuffer_index, *length);
1540 src->linebuffer_index += *length;
1541 }
1542
1543
1544
1545 static SANE_Status
read_image(pixma_sane_t * ss,void * buf,unsigned size,int * readlen)1546 read_image (pixma_sane_t * ss, void *buf, unsigned size, int *readlen)
1547 {
1548 int count, status;
1549
1550 if (readlen)
1551 *readlen = 0;
1552 if (ss->image_bytes_read >= ss->sp.image_size)
1553 return SANE_STATUS_EOF;
1554
1555 do
1556 {
1557 if (ss->cancel)
1558 /* ss->rpipe has already been closed by sane_cancel(). */
1559 return SANE_STATUS_CANCELLED;
1560 if (ss->sp.mode_jpeg && !ss->jpeg_header_seen)
1561 {
1562 status = pixma_jpeg_read_header(ss);
1563 if (status != SANE_STATUS_GOOD)
1564 {
1565 close (ss->rpipe);
1566 pixma_jpeg_finish(ss);
1567 ss->rpipe = -1;
1568 if (sanei_thread_is_valid (terminate_reader_task (ss, &status))
1569 && status != SANE_STATUS_GOOD)
1570 {
1571 return status;
1572 }
1573 else
1574 {
1575 /* either terminate_reader_task failed or
1576 rpipe was closed but we expect more data */
1577 return SANE_STATUS_IO_ERROR;
1578 }
1579 }
1580 }
1581
1582 if (ss->sp.mode_jpeg)
1583 {
1584 count = -1;
1585 pixma_jpeg_read(ss, buf, size, &count);
1586 }
1587 else
1588 count = read (ss->rpipe, buf, size);
1589 }
1590 while (count == -1 && errno == EINTR);
1591
1592 if (count == -1)
1593 {
1594 if (errno == EAGAIN)
1595 return SANE_STATUS_GOOD;
1596 if (!ss->cancel)
1597 {
1598 PDBG (pixma_dbg (1, "WARNING:read_image():read() failed %s\n",
1599 strerror (errno)));
1600 }
1601 close (ss->rpipe);
1602 ss->rpipe = -1;
1603 terminate_reader_task (ss, NULL);
1604 if (ss->sp.mode_jpeg)
1605 pixma_jpeg_finish(ss);
1606 return SANE_STATUS_IO_ERROR;
1607 }
1608
1609 /* here count >= 0 */
1610 ss->image_bytes_read += count;
1611 if (ss->image_bytes_read > ss->sp.image_size)
1612 {
1613 PDBG (pixma_dbg (1, "BUG:ss->image_bytes_read > ss->sp.image_size\n"));
1614 }
1615 if (ss->image_bytes_read >= ss->sp.image_size)
1616 {
1617 close (ss->rpipe);
1618 ss->rpipe = -1;
1619 terminate_reader_task (ss, NULL);
1620 if (ss->sp.mode_jpeg)
1621 pixma_jpeg_finish(ss);
1622 }
1623 else if (count == 0)
1624 {
1625 PDBG (pixma_dbg (3, "read_image():reader task closed the pipe:%"
1626 PRIu64" bytes received, %"PRIu64" bytes expected\n",
1627 ss->image_bytes_read, ss->sp.image_size));
1628 close (ss->rpipe);
1629 if (ss->sp.mode_jpeg)
1630 pixma_jpeg_finish(ss);
1631 ss->rpipe = -1;
1632 if (sanei_thread_is_valid (terminate_reader_task (ss, &status))
1633 && status != SANE_STATUS_GOOD)
1634 {
1635 return status;
1636 }
1637 else
1638 {
1639 /* either terminate_reader_task failed or
1640 rpipe was closed but we expect more data */
1641 return SANE_STATUS_IO_ERROR;
1642 }
1643 }
1644 if (readlen)
1645 *readlen = count;
1646 return SANE_STATUS_GOOD;
1647 }
1648
1649
1650 /*******************************************************************
1651 ** SANE API
1652 *******************************************************************/
1653 SANE_Status
sane_init(SANE_Int * version_code,SANE_Auth_Callback authorize)1654 sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize)
1655 {
1656 int status, myversion, i;
1657 SANEI_Config config;
1658
1659 UNUSED (authorize);
1660
1661 if (!version_code)
1662 return SANE_STATUS_INVAL;
1663 myversion = 100 * PIXMA_VERSION_MAJOR + PIXMA_VERSION_MINOR;
1664 *version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, SANE_CURRENT_MINOR, myversion);
1665 DBG_INIT ();
1666 sanei_thread_init ();
1667 pixma_set_debug_level (DBG_LEVEL);
1668
1669 PDBG(pixma_dbg(2, "pixma is compiled %s pthread support.\n",
1670 (sanei_thread_is_forked () ? "without" : "with")));
1671
1672 for (i = 0; i < MAX_CONF_DEVICES; i++)
1673 conf_devices[i] = NULL;
1674
1675 config.count = 0;
1676 config.descriptors = NULL;
1677 config.values = NULL;
1678
1679 if (sanei_configure_attach(PIXMA_CONFIG_FILE, &config,
1680 config_attach_pixma, NULL) != SANE_STATUS_GOOD)
1681 PDBG(pixma_dbg(2, "Could not read pixma configuration file: %s\n",
1682 PIXMA_CONFIG_FILE));
1683
1684 status = pixma_init ();
1685 if (status < 0)
1686 {
1687 PDBG (pixma_dbg (2, "pixma_init() failed %s\n", pixma_strerror (status)));
1688 }
1689 return map_error (status);
1690 }
1691
1692 void
sane_exit(void)1693 sane_exit (void)
1694 {
1695 while (first_scanner)
1696 sane_close (first_scanner);
1697 cleanup_device_list ();
1698 pixma_cleanup ();
1699 sanei_usb_exit ();
1700 }
1701
1702 SANE_Status
sane_get_devices(const SANE_Device *** device_list,SANE_Bool local_only)1703 sane_get_devices (const SANE_Device *** device_list, SANE_Bool local_only)
1704 {
1705 if (!device_list)
1706 return SANE_STATUS_INVAL;
1707 find_scanners (local_only);
1708 *device_list = dev_list;
1709 return (dev_list) ? SANE_STATUS_GOOD : SANE_STATUS_NO_MEM;
1710 }
1711
1712 SANE_Status
sane_open(SANE_String_Const name,SANE_Handle * h)1713 sane_open (SANE_String_Const name, SANE_Handle * h)
1714 {
1715 unsigned i, j, nscanners;
1716 int error = 0;
1717 pixma_sane_t *ss = NULL;
1718 const pixma_config_t *cfg;
1719
1720 if (!name || !h)
1721 return SANE_STATUS_INVAL;
1722
1723 *h = NULL;
1724 nscanners = pixma_find_scanners (conf_devices, SANE_FALSE);
1725 if (nscanners == 0)
1726 return SANE_STATUS_INVAL;
1727
1728 /* also get device id if we replay a xml file
1729 * otherwise name contains the xml filename
1730 * and further replay will fail */
1731 if (name[0] == '\0' || strstr (name, ".xml"))
1732 name = pixma_get_device_id (0);
1733
1734 /* Have we already opened the scanner? */
1735 for (ss = first_scanner; ss; ss = ss->next)
1736 {
1737 if (strcmp (pixma_get_string (ss->s, PIXMA_STRING_ID), name) == 0)
1738 {
1739 /* We have already opened it! */
1740 return SANE_STATUS_DEVICE_BUSY;
1741 }
1742 }
1743
1744 i = 0;
1745 while (strcmp (pixma_get_device_id (i), name) != 0)
1746 {
1747 if (++i >= nscanners)
1748 return SANE_STATUS_INVAL;
1749 }
1750 cfg = pixma_get_device_config (i);
1751 if ((cfg->cap & PIXMA_CAP_EXPERIMENT) != 0)
1752 {
1753 #ifndef NDEBUG
1754 pixma_dbg (1, "WARNING:"
1755 "Experimental backend CAN DAMAGE your hardware!\n");
1756 if (getenv_atoi ("PIXMA_EXPERIMENT", 0) == 0)
1757 {
1758 pixma_dbg (1, "Experimental SANE backend for %s is disabled "
1759 "by default.\n", pixma_get_device_model (i));
1760 pixma_dbg (1, "To enable it, set the environment variable "
1761 "PIXMA_EXPERIMENT to non-zero.\n");
1762 return SANE_STATUS_UNSUPPORTED;
1763 }
1764 #else
1765 return SANE_STATUS_UNSUPPORTED;
1766 #endif
1767 }
1768
1769 ss = (pixma_sane_t *) calloc (1, sizeof (*ss));
1770 if (!ss)
1771 return SANE_STATUS_NO_MEM;
1772 ss->next = first_scanner;
1773 first_scanner = ss;
1774 sanei_thread_initialize (ss->reader_taskid);
1775 ss->wpipe = -1;
1776 ss->rpipe = -1;
1777 ss->idle = SANE_TRUE;
1778 ss->scanning = SANE_FALSE;
1779 ss->sp.frontend_cancel = SANE_FALSE;
1780 for (j=0; j < BUTTON_GROUP_SIZE; j++)
1781 ss->button_option_is_cached[j] = 0;
1782 error = pixma_open (i, &ss->s);
1783 if (error < 0)
1784 {
1785 sane_close (ss);
1786 return map_error (error);
1787 }
1788 pixma_enable_background (ss->s, 0);
1789 init_option_descriptors (ss);
1790 *h = ss;
1791 return SANE_STATUS_GOOD;
1792 }
1793
1794 void
sane_close(SANE_Handle h)1795 sane_close (SANE_Handle h)
1796 {
1797 pixma_sane_t **p, *ss;
1798
1799 for (p = &first_scanner; *p && *p != (pixma_sane_t *) h; p = &((*p)->next))
1800 {
1801 }
1802 if (!(*p))
1803 return;
1804 ss = *p;
1805 sane_cancel (ss);
1806 pixma_close (ss->s);
1807 *p = ss->next;
1808 free (ss);
1809 }
1810
1811 const SANE_Option_Descriptor *
sane_get_option_descriptor(SANE_Handle h,SANE_Int n)1812 sane_get_option_descriptor (SANE_Handle h, SANE_Int n)
1813 {
1814 DECL_CTX;
1815
1816 if (ss && 0 <= n && n < opt_last)
1817 return &SOD (n);
1818 return NULL;
1819 }
1820
1821 SANE_Status
sane_control_option(SANE_Handle h,SANE_Int n,SANE_Action a,void * v,SANE_Int * i)1822 sane_control_option (SANE_Handle h, SANE_Int n,
1823 SANE_Action a, void *v, SANE_Int * i)
1824 {
1825 DECL_CTX;
1826 SANE_Int info = 0;
1827 int error;
1828 option_descriptor_t *opt;
1829
1830 if (i)
1831 *i = 0;
1832 if (!ss)
1833 return SANE_STATUS_INVAL;
1834 if (n < 0 || n >= opt_last)
1835 return SANE_STATUS_UNSUPPORTED;
1836 if (!ss->idle && a != SANE_ACTION_GET_VALUE)
1837 {
1838 PDBG (pixma_dbg (3, "Warning: !idle && !SANE_ACTION_GET_VALUE\n"));
1839 if (ss->sp.source != PIXMA_SOURCE_ADF && ss->sp.source != PIXMA_SOURCE_ADFDUP)
1840 return SANE_STATUS_INVAL;
1841 }
1842
1843 opt = &(OPT_IN_CTX[n]);
1844 if (!SANE_OPTION_IS_ACTIVE (opt->sod.cap))
1845 return SANE_STATUS_INVAL;
1846 switch (a)
1847 {
1848 case SANE_ACTION_SET_VALUE:
1849 if ((opt->sod.type != SANE_TYPE_BUTTON && !v) ||
1850 !SANE_OPTION_IS_SETTABLE (opt->sod.cap))
1851 return SANE_STATUS_INVAL; /* or _UNSUPPORTED? */
1852 break;
1853 case SANE_ACTION_SET_AUTO:
1854 if (!(opt->sod.cap & SANE_CAP_AUTOMATIC) ||
1855 !SANE_OPTION_IS_SETTABLE (opt->sod.cap))
1856 return SANE_STATUS_INVAL; /* or _UNSUPPORTED? */
1857 break;
1858 case SANE_ACTION_GET_VALUE:
1859 if (!v || !(opt->sod.cap & SANE_CAP_SOFT_DETECT))
1860 return SANE_STATUS_INVAL; /* or _UNSUPPORTED? */
1861 break;
1862 default:
1863 return SANE_STATUS_UNSUPPORTED;
1864 }
1865
1866 error = control_option (ss, n, a, v, &info);
1867 if (error == SANE_STATUS_GOOD && i)
1868 *i = info;
1869 return error;
1870 }
1871
1872 SANE_Status
sane_get_parameters(SANE_Handle h,SANE_Parameters * p)1873 sane_get_parameters (SANE_Handle h, SANE_Parameters * p)
1874 {
1875 DECL_CTX;
1876 pixma_scan_param_t temp, *sp;
1877
1878 if (!ss || !p)
1879 return SANE_STATUS_INVAL;
1880
1881 if (!ss->idle)
1882 {
1883 sp = &ss->sp; /* sp is calculated in sane_start() */
1884 }
1885 else
1886 {
1887 calc_scan_param (ss, &temp);
1888 sp = &temp;
1889 }
1890 p->format = (sp->channels == 3) ? SANE_FRAME_RGB : SANE_FRAME_GRAY;
1891 p->last_frame = SANE_TRUE;
1892 p->lines = sp->h;
1893 p->depth = sp->depth;
1894 p->pixels_per_line = sp->w;
1895 /* p->bytes_per_line = sp->line_size; NOTE: It should work this way, but it doesn't. No SANE frontend can cope with this. */
1896 p->bytes_per_line = (sp->w * sp->channels * sp->depth) / 8;
1897 return SANE_STATUS_GOOD;
1898 }
1899
1900 SANE_Status
sane_start(SANE_Handle h)1901 sane_start (SANE_Handle h)
1902 {
1903 DECL_CTX;
1904 int error = 0;
1905
1906 if (!ss)
1907 return SANE_STATUS_INVAL;
1908 if (!ss->idle && ss->scanning)
1909 {
1910 PDBG (pixma_dbg (3, "Warning in Sane_start: !idle && scanning. idle=%d, ss->scanning=%d\n",
1911 ss->idle, ss->scanning));
1912 if (ss->sp.source != PIXMA_SOURCE_ADF && ss->sp.source != PIXMA_SOURCE_ADFDUP)
1913 return SANE_STATUS_INVAL;
1914 }
1915
1916 ss->cancel = SANE_FALSE;
1917 if (ss->idle ||
1918 ss->source_map[OVAL (opt_source).w] == PIXMA_SOURCE_FLATBED ||
1919 ss->source_map[OVAL (opt_source).w] == PIXMA_SOURCE_TPU)
1920 ss->page_count = 0; /* start from idle state or scan from flatbed or TPU */
1921 else
1922 ss->page_count++;
1923 if (calc_scan_param (ss, &ss->sp) < 0)
1924 return SANE_STATUS_INVAL;
1925
1926 /* Prepare the JPEG decompressor, if needed */
1927 if (ss->sp.mode_jpeg)
1928 {
1929 SANE_Status status;
1930 status = pixma_jpeg_start(ss);
1931 if (status != SANE_STATUS_GOOD)
1932 {
1933 PDBG (pixma_dbg(1, "%s: pixma_jpeg_start: %s\n", __func__, sane_strstatus(status)) );
1934 return status;
1935 }
1936 }
1937
1938 ss->image_bytes_read = 0;
1939 /* TODO: Check paper here in sane_start(). A function like
1940 pixma_get_status() is needed. */
1941 error = start_reader_task (ss);
1942 if (error >= 0)
1943 {
1944 ss->output_line_size = (ss->sp.w * ss->sp.channels * ss->sp.depth) / 8;
1945 ss->byte_pos_in_line = 0;
1946 ss->last_read_status = SANE_STATUS_GOOD;
1947 ss->scanning = SANE_TRUE;
1948 ss->idle = SANE_FALSE;
1949 if (ss->sp.mode_jpeg && !ss->jpeg_header_seen)
1950 {
1951 SANE_Status status;
1952 status = pixma_jpeg_read_header(ss);
1953 if (status != SANE_STATUS_GOOD)
1954 {
1955 close (ss->rpipe);
1956 pixma_jpeg_finish(ss);
1957 ss->rpipe = -1;
1958 if (sanei_thread_is_valid (terminate_reader_task (ss, &error))
1959 && error != SANE_STATUS_GOOD)
1960 {
1961 return error;
1962 }
1963 }
1964 }
1965 }
1966 return map_error (error);
1967 }
1968
1969 SANE_Status
sane_read(SANE_Handle h,SANE_Byte * buf,SANE_Int maxlen,SANE_Int * len)1970 sane_read (SANE_Handle h, SANE_Byte * buf, SANE_Int maxlen, SANE_Int * len)
1971 {
1972 DECL_CTX;
1973 int sum, n;
1974 /* Due to 32 pixels alignment, sizeof(temp) is to be greater than:
1975 * max(nchannels) * max (sp.line_size - output_line_size)
1976 * so currently: 3 * 32 = 96 for better end line cropping efficiency */
1977 SANE_Byte temp[100];
1978 SANE_Status status;
1979
1980 if (len)
1981 *len = 0;
1982 if (!ss || !buf || !len)
1983 return SANE_STATUS_INVAL;
1984 if (ss->cancel)
1985 return SANE_STATUS_CANCELLED;
1986 if ((ss->idle)
1987 && (ss->sp.source == PIXMA_SOURCE_ADF || ss->sp.source == PIXMA_SOURCE_ADFDUP))
1988 return SANE_STATUS_INVAL;
1989 if (!ss->scanning)
1990 return ss->last_read_status;
1991
1992 status = SANE_STATUS_GOOD;
1993 /* CCD scanners use software lineart
1994 * the scanner must scan 24 bit color or 8 bit grayscale for one bit lineart */
1995 if ((ss->sp.line_size - ((ss->sp.software_lineart == 1) ? (ss->output_line_size * 8) : ss->output_line_size)) == 0)
1996 {
1997 status = read_image (ss, buf, maxlen, &sum);
1998 }
1999 else
2000 {
2001 /* FIXME: Because there is no frontend that can cope with padding at
2002 the end of line, we've to remove it here in the backend! */
2003 PDBG (pixma_dbg (1, "*sane_read***** Warning: padding may cause incomplete scan results\n"));
2004 sum = 0;
2005 while (sum < maxlen)
2006 {
2007 if (ss->byte_pos_in_line < ss->output_line_size)
2008 {
2009 n = ss->output_line_size - ss->byte_pos_in_line;
2010 if ((maxlen - sum) < n)
2011 n = maxlen - sum;
2012 status = read_image (ss, buf, n, &n);
2013 if (n == 0)
2014 break;
2015 sum += n;
2016 buf += n;
2017 ss->byte_pos_in_line += n;
2018 }
2019 else
2020 {
2021 /* skip padding */
2022 n = ss->sp.line_size - ss->byte_pos_in_line;
2023 if (n > (int) sizeof (temp))
2024 {
2025 PDBG (pixma_dbg (3, "Inefficient skip buffer. Should be %d\n", n));
2026 n = sizeof (temp);
2027 }
2028 status = read_image (ss, temp, n, &n);
2029 if (n == 0)
2030 break;
2031 ss->byte_pos_in_line += n;
2032 if (ss->byte_pos_in_line == ss->sp.line_size)
2033 ss->byte_pos_in_line = 0;
2034 }
2035 }
2036 }
2037 if (ss->cancel)
2038 status = SANE_STATUS_CANCELLED;
2039 else if ((status == SANE_STATUS_GOOD || status == SANE_STATUS_EOF) &&
2040 sum > 0)
2041 {
2042 *len = sum;
2043 status = SANE_STATUS_GOOD;
2044 }
2045 ss->scanning = (status == SANE_STATUS_GOOD);
2046 ss->last_read_status = status;
2047 return status;
2048 }
2049
2050 void
sane_cancel(SANE_Handle h)2051 sane_cancel (SANE_Handle h)
2052 {
2053 DECL_CTX;
2054
2055 if (!ss)
2056 return;
2057 ss->cancel = SANE_TRUE;
2058 ss->sp.frontend_cancel = SANE_TRUE;
2059 if (ss->idle)
2060 return;
2061 close (ss->rpipe);
2062 if (ss->sp.mode_jpeg)
2063 pixma_jpeg_finish(ss);
2064 ss->rpipe = -1;
2065 terminate_reader_task (ss, NULL);
2066 ss->idle = SANE_TRUE;
2067 }
2068
2069 SANE_Status
sane_set_io_mode(SANE_Handle h,SANE_Bool m)2070 sane_set_io_mode (SANE_Handle h, SANE_Bool m)
2071 {
2072 DECL_CTX;
2073
2074 if (!ss || ss->idle || ss->rpipe == -1)
2075 return SANE_STATUS_INVAL;
2076 #ifdef HAVE_FCNTL_H
2077 PDBG (pixma_dbg (2, "Setting %sblocking mode\n", (m) ? "non-" : ""));
2078 if (fcntl (ss->rpipe, F_SETFL, (m) ? O_NONBLOCK : 0) == -1)
2079 {
2080 PDBG (pixma_dbg
2081 (1, "WARNING:fcntl(F_SETFL) failed %s\n", strerror (errno)));
2082 return SANE_STATUS_UNSUPPORTED;
2083 }
2084 return SANE_STATUS_GOOD;
2085 #else
2086 return (m) ? SANE_STATUS_UNSUPPORTED : SANE_STATUS_GOOD;
2087 #endif
2088 }
2089
2090 SANE_Status
sane_get_select_fd(SANE_Handle h,SANE_Int * fd)2091 sane_get_select_fd (SANE_Handle h, SANE_Int * fd)
2092 {
2093 DECL_CTX;
2094
2095 *fd = -1;
2096 if (!ss || !fd || ss->idle || ss->rpipe == -1)
2097 return SANE_STATUS_INVAL;
2098 *fd = ss->rpipe;
2099 return SANE_STATUS_GOOD;
2100 }
2101
2102 /* CAUTION!
2103 * Remove generated files pixma_sane_options.[ch] after editing SANE option
2104 * descriptors below OR do a 'make clean' OR manually generate them as described
2105 * below.
2106 * However, make drops the circular dependency and the files won't be generated
2107 * again (see merge request sane-project/backends!491).
2108
2109 BEGIN SANE_Option_Descriptor
2110
2111 rem -------------------------------------------
2112 type group
2113 title Scan mode
2114
2115 type int resolution
2116 unit dpi
2117 constraint @word_list = ss->dpi_list
2118 default 75
2119 title @SANE_TITLE_SCAN_RESOLUTION
2120 desc @SANE_DESC_SCAN_RESOLUTION
2121 cap soft_select soft_detect automatic
2122 info reload_params
2123
2124 type string mode[30]
2125 constraint @string_list = ss->mode_list
2126 default @s = SANE_VALUE_SCAN_MODE_COLOR
2127 title @SANE_TITLE_SCAN_MODE
2128 desc @SANE_DESC_SCAN_MODE
2129 cap soft_select soft_detect automatic
2130 info reload_params
2131
2132 type string source[30]
2133 constraint @string_list = ss->source_list
2134 title @SANE_TITLE_SCAN_SOURCE
2135 desc Selects the scan source (such as a document-feeder). Set source before mode and resolution. Resets mode and resolution to auto values.
2136 default Flatbed
2137 cap soft_select soft_detect
2138
2139 type bool button-controlled
2140 title Button-controlled scan
2141 desc When enabled, scan process will not start immediately. To proceed, press \"SCAN\" button (for MP150) or \"COLOR\" button (for other models). To cancel, press \"GRAY\" button.
2142 default SANE_FALSE
2143 cap soft_select soft_detect inactive
2144
2145 rem -------------------------------------------
2146 type group
2147 title Gamma
2148
2149 type bool custom-gamma
2150 default SANE_FALSE
2151 title @SANE_TITLE_CUSTOM_GAMMA
2152 desc @SANE_DESC_CUSTOM_GAMMA
2153 cap soft_select soft_detect automatic inactive
2154
2155 type int gamma-table[1024]
2156 constraint (0,0xffff,0)
2157 title @SANE_TITLE_GAMMA_VECTOR
2158 desc Gamma-correction table with 1024 entries. In color mode this option equally affects the red, green, and blue channels simultaneously (i.e., it is an intensity gamma table).
2159 cap soft_select soft_detect automatic inactive
2160
2161 type fixed gamma
2162 default AUTO_GAMMA
2163 constraint (0.3,5,0)
2164 title Gamma function exponent
2165 desc Changes intensity of midtones
2166 cap soft_select soft_detect automatic inactive
2167
2168 rem -------------------------------------------
2169 type group
2170 title Geometry
2171
2172 type fixed tl-x
2173 unit mm
2174 default 0
2175 constraint @range = &ss->xrange
2176 title @SANE_TITLE_SCAN_TL_X
2177 desc @SANE_DESC_SCAN_TL_X
2178 cap soft_select soft_detect automatic
2179 info reload_params
2180
2181 type fixed tl-y
2182 unit mm
2183 default 0
2184 constraint @range = &ss->yrange
2185 title @SANE_TITLE_SCAN_TL_Y
2186 desc @SANE_DESC_SCAN_TL_Y
2187 cap soft_select soft_detect automatic
2188 info reload_params
2189
2190 type fixed br-x
2191 unit mm
2192 default _MAX
2193 constraint @range = &ss->xrange
2194 title @SANE_TITLE_SCAN_BR_X
2195 desc @SANE_DESC_SCAN_BR_X
2196 cap soft_select soft_detect automatic
2197 info reload_params
2198
2199 type fixed br-y
2200 unit mm
2201 default _MAX
2202 constraint @range = &ss->yrange
2203 title @SANE_TITLE_SCAN_BR_Y
2204 desc @SANE_DESC_SCAN_BR_Y
2205 cap soft_select soft_detect automatic
2206 info reload_params
2207
2208 rem -------------------------------------------
2209 type group
2210 title Buttons
2211
2212 type button button-update
2213 title Update button state
2214 cap soft_select soft_detect advanced
2215
2216 type int button-1
2217 default 0
2218 title Button 1
2219 cap soft_detect advanced
2220
2221 type int button-2
2222 default 0
2223 title Button 2
2224 cap soft_detect advanced
2225
2226 type int original
2227 default 0
2228 title Type of original to scan
2229 cap soft_detect advanced
2230
2231 type int target
2232 default 0
2233 title Target operation type
2234 cap soft_detect advanced
2235
2236 type int scan-resolution
2237 default 0
2238 title Scan resolution
2239 cap soft_detect advanced
2240
2241 type int document-type
2242 default 0
2243 title Document type
2244 cap soft_detect advanced
2245
2246 type int adf-status
2247 default 0
2248 title ADF status
2249 cap soft_detect advanced
2250
2251 type int adf-orientation
2252 default 0
2253 title ADF orientation
2254 cap soft_detect advanced
2255
2256 rem -------------------------------------------
2257 type group
2258 title Extras
2259
2260 type int threshold
2261 unit PERCENT
2262 default 50
2263 constraint (0,100,1)
2264 title @SANE_TITLE_THRESHOLD
2265 desc @SANE_DESC_THRESHOLD
2266 cap soft_select soft_detect automatic inactive
2267
2268 type int threshold-curve
2269 constraint (0,127,1)
2270 title Threshold curve
2271 desc Dynamic threshold curve, from light to dark, normally 50-65
2272 cap soft_select soft_detect automatic inactive
2273
2274 type int adf-wait
2275 default 0
2276 constraint (0,3600,1)
2277 title ADF Waiting Time
2278 desc When set, the scanner waits up to the specified time in seconds for a new document inserted into the automatic document feeder.
2279 cap soft_select soft_detect automatic inactive
2280
2281 type string calibrate[30]
2282 constraint @string_list = ss->calibrate_list
2283 title Calibration
2284 desc When to perform scanner calibration. If you choose \"Once\" it will be performed a single time per driver init for single page scans, and for the first page for each ADF scan.
2285 default Once
2286 cap soft_select soft_detect automatic
2287
2288 rem -------------------------------------------
2289 END SANE_Option_Descriptor
2290 */
2291
2292 /* pixma_sane_options.c generated by
2293 * scripts/pixma_gen_options.py < pixma.c > pixma_sane_options.c
2294 *
2295 * pixma_sane_options.h generated by
2296 * scripts/pixma_gen_options.py h < pixma.c > pixma_sane_options.h
2297 */
2298 #include "pixma_sane_options.c"
2299