summaryrefslogtreecommitdiff
path: root/backend/as6e.c
blob: 76557771c3f74f4e64285de97c2cd8bee8562b7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
/* sane - Scanner Access Now Easy.
   Artec AS6E backend.
   Copyright (C) 2000 Eugene S. Weiss
   This file is part of the SANE package.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.

   As a special exception, the authors of SANE give permission for
   additional uses of the libraries contained in this release of SANE.

   The exception is that, if you link a SANE library with other files
   to produce an executable, this does not by itself cause the
   resulting executable to be covered by the GNU General Public
   License.  Your use of that executable is in no way restricted on
   account of linking the SANE library code into it.

   This exception does not, however, invalidate any other reasons why
   the executable file might be covered by the GNU General Public
   License.

   If you submit changes to SANE to the maintainers to be included in
   a subsequent release, you agree by submitting the changes that
   those changes may be distributed with this exception intact.

   If you write modifications of your own for SANE, it is your choice
   whether to permit this exception to apply to your modifications.
   If you do not wish that, delete this exception notice.

   This file implements a backend for the Artec AS6E by making a bridge
   to the as6edriver program.  The as6edriver program can be found at
   http://as6edriver.sourceforge.net .  */




#include "../include/sane/config.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <limits.h>
#include <stdarg.h>
#include <string.h>
#include <signal.h>
#include <sys/stat.h>

#include "../include/sane/sane.h"
#include "../include/sane/saneopts.h"

#define BACKENDNAME as6e
#include "../include/sane/sanei_backend.h"
#include "../include/sane/sanei_config.h"

#include "as6e.h"

static int num_devices;
static AS6E_Device *first_dev;
static AS6E_Scan *first_handle;
static const SANE_Device **devlist = 0;

static SANE_Status attach (const char *devname, AS6E_Device ** devp);
/* static SANE_Status attach_one (const char *dev);  */

static const SANE_String_Const mode_list[] = {
  SANE_VALUE_SCAN_MODE_LINEART,
  SANE_VALUE_SCAN_MODE_GRAY,
  SANE_VALUE_SCAN_MODE_COLOR,
  0
};

static const SANE_Word resolution_list[] = {
  4, 300, 200, 100, 50
};

static const SANE_Range x_range = {
  SANE_FIX (0),
  SANE_FIX (215.91),
  SANE_FIX (0)
};

static const SANE_Range y_range = {
  SANE_FIX (0),
  SANE_FIX (297.19),
  SANE_FIX (0)
};


static const SANE_Range brightness_range = {
  -100,
  100,
  1
};

static const SANE_Range contrast_range = {
  -100,
  100,
  1
};

/*--------------------------------------------------------------------------*/
static SANE_Int
as6e_unit_convert (SANE_Fixed value)
{

  double precise;
  SANE_Int return_value;

  precise = SANE_UNFIX (value);
  precise = (precise * 300) / MM_PER_INCH;
  return_value = precise;
  return return_value;
}

/*--------------------------------------------------------------------------*/

SANE_Status
sane_read (SANE_Handle handle, SANE_Byte * buf, SANE_Int max_len,
	   SANE_Int * len)
{
  AS6E_Scan *s = handle;
  SANE_Word buffer_offset = 0;
  int written = 0, bytes_read = 0, maxbytes;
  SANE_Word bytecounter, linebufcounter, ctlbytes;
  SANE_Byte *linebuffer;

  DBG (3, "reading %d bytes, %d bytes in carryover buffer\n", max_len,
       s->scan_buffer_count);

  if ((unsigned int) s->image_counter >= s->bytes_to_read)
    {
      *len = 0;
      if (s->scanning)
	{
	  read (s->as6e_params.ctlinpipe, &written, sizeof (written));
	  if (written != -1)
	    DBG (3, "pipe error\n");
	  DBG (3, "trying  to read -1 ...written = %d\n", written);
	}
      s->scanning = SANE_FALSE;
      DBG (1, "image data complete, sending EOF...\n");
      return SANE_STATUS_EOF;
    }				/*image complete */

  linebuffer = s->line_buffer;
  if (s->scan_buffer_count > 0)
    {				/*there are leftover bytes from the last call */
      if (s->scan_buffer_count <= max_len)
	{
	  for (*len = 0; *len < s->scan_buffer_count; (*len)++)
	    {
	      buf[*len] = s->scan_buffer[*len];
	      buffer_offset++;
	    }
	  s->scan_buffer_count = 0;
	  if (s->scan_buffer_count == max_len)
	    {
	      s->scan_buffer_count = 0;
	      s->image_counter += max_len;
	      DBG (3, "returning %d bytes from the carryover buffer\n", *len);
	      return SANE_STATUS_GOOD;
	    }
	}
      else
	{
	  for (*len = 0; *len < max_len; (*len)++)
	    buf[*len] = s->scan_buffer[*len];

	  for (bytecounter = max_len;
	       bytecounter < s->scan_buffer_count; bytecounter++)
	    s->scan_buffer[bytecounter - max_len]
	      = s->scan_buffer[bytecounter];

	  s->scan_buffer_count -= max_len;
	  s->image_counter += max_len;
	  DBG (3, "returning %d bytes from the carryover buffer\n", *len);
	  return SANE_STATUS_GOOD;
	}
    }
  else
    {
      *len = 0;			/*no bytes in the buffer */
      if (!s->scanning)
	{
	  DBG (1, "scan over returning %d\n", *len);
	  if (s->scan_buffer_count)
	    return SANE_STATUS_GOOD;
	  else
	    return SANE_STATUS_EOF;
	}
    }
  while (*len < max_len)
    {
      DBG (3, "trying to read number of bytes...\n");
      ctlbytes = read (s->as6e_params.ctlinpipe, &written, sizeof (written));
      DBG (3, "bytes written = %d, ctlbytes =%d\n", written, ctlbytes);
      fflush (stdout);
      if ((s->cancelled) && (written == 0))
	{			/*first clear -1 from pipe */
	  DBG (1, "sending SANE_STATUS_CANCELLED\n");
	  read (s->as6e_params.ctlinpipe, &written, sizeof (written));
	  s->scanning = SANE_FALSE;
	  return SANE_STATUS_CANCELLED;
	}
      if (written == -1)
	{
	  DBG (1, "-1READ Scanner through. returning %d bytes\n", *len);
	  s->image_counter += *len;
	  s->scanning = SANE_FALSE;
	  return SANE_STATUS_GOOD;
	}
      linebufcounter = 0;
      DBG (3,
	   "linebufctr reset, len =%d written =%d bytes_read =%d, max = %d\n",
	   *len, written, bytes_read, max_len);
      maxbytes = written;
      while (linebufcounter < written)
	{
	  DBG (4, "trying to read data pipe\n");
	  bytes_read =
	    read (s->as6e_params.datapipe, linebuffer + linebufcounter,
		  maxbytes);
	  linebufcounter += bytes_read;
	  maxbytes -= bytes_read;
	  DBG (3, "bytes_read = %d linebufcounter = %d\n", bytes_read,
	       linebufcounter);
	}
      DBG (3, "written =%d max_len =%d  len =%d\n", written, max_len, *len);
      if (written <= (max_len - *len))
	{
	  for (bytecounter = 0; bytecounter < written; bytecounter++)
	    {
	      buf[bytecounter + buffer_offset] = linebuffer[bytecounter];
	      (*len)++;
	    }
	  buffer_offset += written;
	  DBG (3, "buffer offset = %d\n", buffer_offset);
	}
      else if (max_len > *len)
	{			/*there's still room to send data */
	  for (bytecounter = 0; bytecounter < (max_len - *len); bytecounter++)
	    buf[bytecounter + buffer_offset] = linebuffer[bytecounter];
	  DBG (3, "topping off buffer\n");
	  for (bytecounter = (max_len - *len); bytecounter < written;
	       bytecounter++)
	    {

	      s->scan_buffer[s->scan_buffer_count + bytecounter -
			     (max_len - *len)] = linebuffer[bytecounter];
	    }
	  s->scan_buffer_count += (written - (max_len - *len));
	  *len = max_len;
	}
      else
	{			/*everything goes into the carryover buffer */
	  for (bytecounter = 0; bytecounter < written; bytecounter++)
	    s->scan_buffer[s->scan_buffer_count + bytecounter]
	      = linebuffer[bytecounter];
	  s->scan_buffer_count += written;
	}
    }				/*while there's space in the buffer */
  s->image_counter += *len;
  DBG (3, "image ctr = %d bytes_to_read = %lu returning %d\n",
       s->image_counter, (u_long) s->bytes_to_read, *len);

  return SANE_STATUS_GOOD;
}

/*--------------------------------------------------------------------------*/
void
sane_cancel (SANE_Handle h)
{
  AS6E_Scan *s = h;
  SANE_Word test;
  DBG (2, "trying to cancel...\n");
  if (s->scanning)
    {
      test = kill (s->child_pid, SIGUSR1);
      if (test == 0)
	s->cancelled = SANE_TRUE;
    }
}

/*--------------------------------------------------------------------------*/

SANE_Status
sane_start (SANE_Handle handle)
{
  AS6E_Scan *s = handle;
  SANE_Status status;
  int repeat = 1;
  SANE_Word numbytes;
  int scan_params[8];
  /* First make sure we have a current parameter set.  Some of the
   * parameters will be overwritten below, but that's OK.  */
  DBG (2, "sane_start\n");
  status = sane_get_parameters (s, 0);
  if (status != SANE_STATUS_GOOD)
    return status;
  DBG (1, "Got params again...\n");
  numbytes = write (s->as6e_params.ctloutpipe, &repeat, sizeof (repeat));
  if (numbytes != sizeof (repeat))
    return (SANE_STATUS_IO_ERROR);
  DBG (1, "sending start_scan signal\n");
  scan_params[0] = s->as6e_params.resolution;
  if (strcmp (s->value[OPT_MODE].s, SANE_VALUE_SCAN_MODE_COLOR) == 0)
    scan_params[1] = 0;
  else if (strcmp (s->value[OPT_MODE].s, SANE_VALUE_SCAN_MODE_GRAY) == 0)
    scan_params[1] = 1;
  else if (strcmp (s->value[OPT_MODE].s, SANE_VALUE_SCAN_MODE_LINEART) == 0)
    scan_params[1] = 2;
  else
    return (SANE_STATUS_JAMMED);	/*this should never happen */
  scan_params[2] = s->as6e_params.startpos;
  scan_params[3] = s->as6e_params.stoppos;
  scan_params[4] = s->as6e_params.startline;
  scan_params[5] = s->as6e_params.stopline;
  scan_params[6] = s->value[OPT_BRIGHTNESS].w;
  scan_params[7] = s->value[OPT_CONTRAST].w;
  DBG (1, "scan params = %d %d %d %d %d %d %d %d\n", scan_params[0],
       scan_params[1], scan_params[2], scan_params[3],
       scan_params[4], scan_params[5], scan_params[6], scan_params[7]);
  numbytes =
    write (s->as6e_params.ctloutpipe, scan_params, sizeof (scan_params));
  if (numbytes != sizeof (scan_params))
    return (SANE_STATUS_IO_ERROR);
  s->scanning = SANE_TRUE;
  s->scan_buffer_count = 0;
  s->image_counter = 0;
  s->cancelled = 0;
  return (SANE_STATUS_GOOD);
}

/*--------------------------------------------------------------------------*/

SANE_Status
sane_get_parameters (SANE_Handle handle, SANE_Parameters * params)
{
  AS6E_Scan *s = handle;
  SANE_String mode;
  SANE_Word divisor = 1;
  DBG (2, "sane_get_parameters\n");
  if (!s->scanning)
    {
      memset (&s->sane_params, 0, sizeof (s->sane_params));
      s->as6e_params.resolution = s->value[OPT_RESOLUTION].w;
      s->as6e_params.startpos = as6e_unit_convert (s->value[OPT_TL_X].w);
      s->as6e_params.stoppos = as6e_unit_convert (s->value[OPT_BR_X].w);
      s->as6e_params.startline = as6e_unit_convert (s->value[OPT_TL_Y].w);
      s->as6e_params.stopline = as6e_unit_convert (s->value[OPT_BR_Y].w);
      if ((s->as6e_params.resolution == 200)
	  || (s->as6e_params.resolution == 100))
	divisor = 3;
      else if (s->as6e_params.resolution == 50)
	divisor = 6;		/*get legal values for 200 dpi */
      s->as6e_params.startpos = (s->as6e_params.startpos / divisor) * divisor;
      s->as6e_params.stoppos = (s->as6e_params.stoppos / divisor) * divisor;
      s->as6e_params.startline =
	(s->as6e_params.startline / divisor) * divisor;
      s->as6e_params.stopline = (s->as6e_params.stopline / divisor) * divisor;
      s->sane_params.pixels_per_line =
	(s->as6e_params.stoppos -
	 s->as6e_params.startpos) * s->as6e_params.resolution / 300;
      s->sane_params.lines =
	(s->as6e_params.stopline -
	 s->as6e_params.startline) * s->as6e_params.resolution / 300;
      mode = s->value[OPT_MODE].s;
/*      if ((strcmp (s->mode, SANE_VALUE_SCAN_MODE_LINEART) == 0) ||
	  (strcmp (s->mode, SANE_VALUE_SCAN_MODE_HALFTONE) == 0))
	{
	  s->sane_params.format = SANE_FRAME_GRAY;
	  s->sane_params.bytes_per_line = (s->sane_params.pixels_per_line + 7) / 8;
	  s->sane_params.depth = 1;
	}  */
/*else*/ if ((strcmp (mode, SANE_VALUE_SCAN_MODE_GRAY) == 0)
	     || (strcmp (mode, SANE_VALUE_SCAN_MODE_LINEART) == 0))
	{
	  s->sane_params.format = SANE_FRAME_GRAY;
	  s->sane_params.bytes_per_line = s->sane_params.pixels_per_line;
	  s->sane_params.depth = 8;
	}			/*grey frame */
      else
	{
	  s->sane_params.format = SANE_FRAME_RGB;
	  s->sane_params.bytes_per_line = 3 * s->sane_params.pixels_per_line;
	  s->sane_params.depth = 8;
	}			/*color frame */
      s->bytes_to_read = s->sane_params.lines * s->sane_params.bytes_per_line;
      s->sane_params.last_frame = SANE_TRUE;
    }				/*!scanning */

  if (params)
    *params = s->sane_params;
  return (SANE_STATUS_GOOD);
}

/*--------------------------------------------------------------------------*/
SANE_Status
sane_control_option (SANE_Handle handle, SANE_Int option,
		     SANE_Action action, void *val, SANE_Int * info)
{
  AS6E_Scan *s = handle;
  SANE_Status status = 0;
  SANE_Word cap;
  DBG (2, "sane_control_option\n");
  if (info)
    *info = 0;
  if (s->scanning)
    return SANE_STATUS_DEVICE_BUSY;
  if (option >= NUM_OPTIONS)
    return SANE_STATUS_INVAL;
  cap = s->options_list[option].cap;
  if (!SANE_OPTION_IS_ACTIVE (cap))
    return SANE_STATUS_INVAL;
  if (action == SANE_ACTION_GET_VALUE)
    {
      DBG (1, "sane_control_option %d, get value\n", option);
      switch (option)
	{
	  /* word options: */
	case OPT_RESOLUTION:
	case OPT_TL_X:
	case OPT_TL_Y:
	case OPT_BR_X:
	case OPT_BR_Y:
	case OPT_NUM_OPTS:
	case OPT_CONTRAST:
	case OPT_BRIGHTNESS:
	  *(SANE_Word *) val = s->value[option].w;
	  return (SANE_STATUS_GOOD);
	  /* string options: */
	case OPT_MODE:
	  strcpy (val, s->value[option].s);
	  return (SANE_STATUS_GOOD);
	}
    }
  else if (action == SANE_ACTION_SET_VALUE)
    {
      DBG (1, "sane_control_option %d, set value\n", option);
      if (!SANE_OPTION_IS_SETTABLE (cap))
	return (SANE_STATUS_INVAL);
/*      status = sanei_constrain_value (s->options_list[option], val, info);*/
      if (status != SANE_STATUS_GOOD)
	return (status);
      switch (option)
	{
	  /* (mostly) side-effect-free word options: */
	case OPT_RESOLUTION:
	case OPT_BR_X:
	case OPT_BR_Y:
	case OPT_TL_X:
	case OPT_TL_Y:
	  if (info && s->value[option].w != *(SANE_Word *) val)
	    *info |= SANE_INFO_RELOAD_PARAMS;
	  /* fall through */
	case OPT_NUM_OPTS:
	case OPT_CONTRAST:
	case OPT_BRIGHTNESS:
	  s->value[option].w = *(SANE_Word *) val;
	  DBG (1, "set brightness to\n");
	  return (SANE_STATUS_GOOD);
	case OPT_MODE:
	  if (s->value[option].s)
	    free (s->value[option].s);
	  s->value[option].s = strdup (val);
	  return (SANE_STATUS_GOOD);
	}
    }
  return (SANE_STATUS_INVAL);
}

/*--------------------------------------------------------------------------*/
const SANE_Option_Descriptor *
sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
{
  AS6E_Scan *s = handle;
  DBG (2, "sane_get_option_descriptor\n");
  if ((unsigned) option >= NUM_OPTIONS)
    return (0);
  return (&s->options_list[option]);
}

/*--------------------------------------------------------------------------*/

void
sane_close (SANE_Handle handle)
{
  AS6E_Scan *prev, *s;
  SANE_Word repeat = 0;
  DBG (2, "sane_close\n");
  /* remove handle from list of open handles: */
  prev = 0;
  for (s = first_handle; s; s = s->next)
    {
      if (s == handle)
	break;
      prev = s;
    }
  if (!s)
    {
      DBG (1, "close: invalid handle %p\n", handle);
      return;			/* oops, not a handle we know about */
    }

  if (s->scanning)
    sane_cancel (handle);
  write (s->as6e_params.ctloutpipe, &repeat, sizeof (repeat));
  close (s->as6e_params.ctloutpipe);
  free (s->scan_buffer);
  free (s->line_buffer);
  if (prev)
    prev->next = s->next;
  else
    first_handle = s;
  free (handle);
}

/*--------------------------------------------------------------------------*/
void
sane_exit (void)
{
  AS6E_Device *next;
  DBG (2, "sane_exit\n");
  while (first_dev != NULL)
    {
      next = first_dev->next;
      free (first_dev);
      first_dev = next;
    }
  if (devlist)
    free (devlist);
}

/*--------------------------------------------------------------------------*/
static SANE_Status
as6e_open (AS6E_Scan * s)
{

  int data_processed, exec_result, as6e_status;
  int ctloutpipe[2], ctlinpipe[2], datapipe[2];
  char inpipe_desc[32], outpipe_desc[32], datapipe_desc[32];
  pid_t fork_result;
  DBG (1, "as6e_open\n");
  memset (inpipe_desc, '\0', sizeof (inpipe_desc));
  memset (outpipe_desc, '\0', sizeof (outpipe_desc));
  memset (datapipe_desc, '\0', sizeof (datapipe_desc));
  if ((pipe (ctloutpipe) == 0) && (pipe (ctlinpipe) == 0)
      && (pipe (datapipe) == 0))
    {
      fork_result = fork ();
      if (fork_result == (pid_t) - 1)
	{
	  DBG (1, "Fork failure");
	  return (SANE_STATUS_IO_ERROR);
	}

      if (fork_result == 0)
	{			/*in child */
	  sprintf (inpipe_desc, "%d", ctlinpipe[WRITEPIPE]);
	  sprintf (outpipe_desc, "%d", ctloutpipe[READPIPE]);
	  sprintf (datapipe_desc, "%d", datapipe[WRITEPIPE]);
	  exec_result =
	    execlp ("as6edriver", "as6edriver", "-s", inpipe_desc,
		    outpipe_desc, datapipe_desc, (char *) 0);
	  DBG (1, "The SANE backend was unable to start \"as6edriver\".\n");
	  DBG (1, "This must be installed in a directory in your PATH.\n");
	  DBG (1, "To acquire the as6edriver program,\n");
	  DBG (1, "go to http://as6edriver.sourceforge.net.\n");
	  write (ctlinpipe[WRITEPIPE], &exec_result, sizeof (exec_result));
	  exit (-1);
	}
      else
	{			/*parent process */
	  data_processed =
	    read (ctlinpipe[READPIPE], &as6e_status, sizeof (as6e_status));
	  DBG (1, "%d - read %d status = %d\n", getpid (), data_processed,
	       as6e_status);
	  if (as6e_status == -2)
	    {
	      DBG (1, "Port access denied.\n");
	      return (SANE_STATUS_IO_ERROR);
	    }
	  if (as6e_status == -1)
	    {
	      DBG (1, "Could not contact scanner.\n");
	      return (SANE_STATUS_IO_ERROR);
	    }

	  if (as6e_status == 1)
	    DBG (1, "Using nibble mode.\n");
	  if (as6e_status == 2)
	    DBG (1, "Using byte mode.\n");
	  if (as6e_status == 3)
	    DBG (1, "Using EPP mode.\n");
	  s->as6e_params.ctlinpipe = ctlinpipe[READPIPE];
	  s->as6e_params.ctloutpipe = ctloutpipe[WRITEPIPE];
	  s->as6e_params.datapipe = datapipe[READPIPE];
	  s->child_pid = fork_result;
	  return (SANE_STATUS_GOOD);
	}			/*else */
    }
  else
    return (SANE_STATUS_IO_ERROR);
}


/*--------------------------------------------------------------------------*/
SANE_Status
sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize)
{
  char dev_name[PATH_MAX];
  size_t len;
  FILE *fp = NULL;

  DBG_INIT ();
  DBG (2, "sane_init (authorize %s null)\n", (authorize) ? "!=" : "==");
  if (version_code)
    *version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, SANE_CURRENT_MINOR, 0);
/*  fp = sanei_config_open (AS6E_CONFIG_FILE);*/
  if (!fp)
    {
      return (attach ("as6edriver", 0));
    }

  while (fgets (dev_name, sizeof (dev_name), fp))
    {
      if (dev_name[0] == '#')	/* ignore line comments */
	continue;
      len = strlen (dev_name);
      if (dev_name[len - 1] == '\n')
	dev_name[--len] = '\0';
      if (!len)
	continue;		/* ignore empty lines */
/*      sanei_config_attach_matching_devices (dev_name, attach_one);*/
    }
  fclose (fp);
  return (SANE_STATUS_GOOD);
}

/*--------------------------------------------------------------------------*/
/*
static SANE_Status
attach_one (const char *dev)
{
  DBG (2, "attach_one\n");
  attach (dev, 0);
  return (SANE_STATUS_GOOD);
}
  */
/*--------------------------------------------------------------------------*/
SANE_Status
sane_get_devices (const SANE_Device *** device_list, SANE_Bool local_only)
{
  static const SANE_Device **devlist = 0;
  AS6E_Device *dev;
  int i;
  DBG (3, "sane_get_devices (local_only = %d)\n", local_only);
  if (devlist)
    free (devlist);
  devlist = malloc ((num_devices + 1) * sizeof (devlist[0]));
  if (!devlist)
    return SANE_STATUS_NO_MEM;
  i = 0;
  for (dev = first_dev; i < num_devices; dev = dev->next)
    devlist[i++] = &dev->sane;
  devlist[i++] = 0;
  *device_list = devlist;
  return (SANE_STATUS_GOOD);
}


/*--------------------------------------------------------------------------*/

static size_t
max_string_size (const SANE_String_Const strings[])
{
  size_t size, max_size = 0;
  int i;
  for (i = 0; strings[i]; ++i)
    {
      size = strlen (strings[i]) + 1;
      if (size > max_size)
	max_size = size;
    }

  return (max_size);
}

/*--------------------------------------------------------------------------*/

static void
initialize_options_list (AS6E_Scan * s)
{

  SANE_Int option;
  DBG (2, "initialize_options_list\n");
  for (option = 0; option < NUM_OPTIONS; ++option)
    {
      s->options_list[option].size = sizeof (SANE_Word);
      s->options_list[option].cap =
	SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
    }

  s->options_list[OPT_NUM_OPTS].name = SANE_NAME_NUM_OPTIONS;
  s->options_list[OPT_NUM_OPTS].title = SANE_TITLE_NUM_OPTIONS;
  s->options_list[OPT_NUM_OPTS].desc = SANE_DESC_NUM_OPTIONS;
  s->options_list[OPT_NUM_OPTS].type = SANE_TYPE_INT;
  s->options_list[OPT_NUM_OPTS].unit = SANE_UNIT_NONE;
  s->options_list[OPT_NUM_OPTS].size = sizeof (SANE_Word);
  s->options_list[OPT_NUM_OPTS].cap = SANE_CAP_SOFT_DETECT;
  s->options_list[OPT_NUM_OPTS].constraint_type = SANE_CONSTRAINT_NONE;
  s->value[OPT_NUM_OPTS].w = NUM_OPTIONS;
  s->options_list[OPT_MODE].name = SANE_NAME_SCAN_MODE;
  s->options_list[OPT_MODE].title = SANE_TITLE_SCAN_MODE;
  s->options_list[OPT_MODE].desc = SANE_DESC_SCAN_MODE;
  s->options_list[OPT_MODE].type = SANE_TYPE_STRING;
  s->options_list[OPT_MODE].size = max_string_size (mode_list);
  s->options_list[OPT_MODE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
  s->options_list[OPT_MODE].constraint.string_list = mode_list;
  s->value[OPT_MODE].s = strdup (mode_list[2]);
  s->options_list[OPT_RESOLUTION].name = SANE_NAME_SCAN_RESOLUTION;
  s->options_list[OPT_RESOLUTION].title = SANE_TITLE_SCAN_RESOLUTION;
  s->options_list[OPT_RESOLUTION].desc = SANE_DESC_SCAN_RESOLUTION;
  s->options_list[OPT_RESOLUTION].type = SANE_TYPE_INT;
  s->options_list[OPT_RESOLUTION].unit = SANE_UNIT_DPI;
  s->options_list[OPT_RESOLUTION].constraint_type = SANE_CONSTRAINT_WORD_LIST;
  s->options_list[OPT_RESOLUTION].constraint.word_list = resolution_list;
  s->value[OPT_RESOLUTION].w = 200;
  s->options_list[OPT_TL_X].name = SANE_NAME_SCAN_TL_X;
  s->options_list[OPT_TL_X].title = SANE_TITLE_SCAN_TL_X;
  s->options_list[OPT_TL_X].desc = SANE_DESC_SCAN_TL_X;
  s->options_list[OPT_TL_X].type = SANE_TYPE_FIXED;
  s->options_list[OPT_TL_X].unit = SANE_UNIT_MM;
  s->options_list[OPT_TL_X].constraint_type = SANE_CONSTRAINT_RANGE;
  s->options_list[OPT_TL_X].constraint.range = &x_range;
  s->value[OPT_TL_X].w = s->options_list[OPT_TL_X].constraint.range->min;
  s->options_list[OPT_TL_Y].name = SANE_NAME_SCAN_TL_Y;
  s->options_list[OPT_TL_Y].title = SANE_TITLE_SCAN_TL_Y;
  s->options_list[OPT_TL_Y].desc = SANE_DESC_SCAN_TL_Y;
  s->options_list[OPT_TL_Y].type = SANE_TYPE_FIXED;
  s->options_list[OPT_TL_Y].unit = SANE_UNIT_MM;
  s->options_list[OPT_TL_Y].constraint_type = SANE_CONSTRAINT_RANGE;
  s->options_list[OPT_TL_Y].constraint.range = &y_range;
  s->value[OPT_TL_Y].w = s->options_list[OPT_TL_Y].constraint.range->min;
  s->options_list[OPT_BR_X].name = SANE_NAME_SCAN_BR_X;
  s->options_list[OPT_BR_X].title = SANE_TITLE_SCAN_BR_X;
  s->options_list[OPT_BR_X].desc = SANE_DESC_SCAN_BR_X;
  s->options_list[OPT_BR_X].type = SANE_TYPE_FIXED;
  s->options_list[OPT_BR_X].unit = SANE_UNIT_MM;
  s->options_list[OPT_BR_X].constraint_type = SANE_CONSTRAINT_RANGE;
  s->options_list[OPT_BR_X].constraint.range = &x_range;
  s->value[OPT_BR_X].w = s->options_list[OPT_BR_X].constraint.range->max;
  s->options_list[OPT_BR_Y].name = SANE_NAME_SCAN_BR_Y;
  s->options_list[OPT_BR_Y].title = SANE_TITLE_SCAN_BR_Y;
  s->options_list[OPT_BR_Y].desc = SANE_DESC_SCAN_BR_Y;
  s->options_list[OPT_BR_Y].type = SANE_TYPE_FIXED;
  s->options_list[OPT_BR_Y].unit = SANE_UNIT_MM;
  s->options_list[OPT_BR_Y].constraint_type = SANE_CONSTRAINT_RANGE;
  s->options_list[OPT_BR_Y].constraint.range = &y_range;
  s->value[OPT_BR_Y].w = s->options_list[OPT_BR_Y].constraint.range->max;
  s->options_list[OPT_CONTRAST].name = SANE_NAME_CONTRAST;
  s->options_list[OPT_CONTRAST].title = SANE_TITLE_CONTRAST;
  s->options_list[OPT_CONTRAST].desc = SANE_DESC_CONTRAST;
  s->options_list[OPT_CONTRAST].type = SANE_TYPE_INT;
  s->options_list[OPT_CONTRAST].unit = SANE_UNIT_NONE;
  s->options_list[OPT_CONTRAST].constraint_type = SANE_CONSTRAINT_RANGE;
  s->options_list[OPT_CONTRAST].constraint.range = &brightness_range;
  s->value[OPT_BRIGHTNESS].w = 10;
  s->options_list[OPT_BRIGHTNESS].name = SANE_NAME_BRIGHTNESS;
  s->options_list[OPT_BRIGHTNESS].title = SANE_TITLE_BRIGHTNESS;
  s->options_list[OPT_BRIGHTNESS].desc = SANE_DESC_BRIGHTNESS;
  s->options_list[OPT_BRIGHTNESS].type = SANE_TYPE_INT;
  s->options_list[OPT_BRIGHTNESS].unit = SANE_UNIT_NONE;
  s->options_list[OPT_BRIGHTNESS].constraint_type = SANE_CONSTRAINT_RANGE;
  s->options_list[OPT_BRIGHTNESS].constraint.range = &contrast_range;
  s->value[OPT_CONTRAST].w = -32;
}

/*--------------------------------------------------------------------------*/
static int
check_for_driver (const char *devname)
{
#define NAMESIZE 128
  struct stat statbuf;
  mode_t modes;
  char *path;
  char dir[NAMESIZE];
  int count = 0, offset = 0, valid;

  path = getenv ("PATH");
  if (!path)
    return 0;
  while (path[count] != '\0')
    {
      memset (dir, '\0', sizeof (dir));
      valid = 1;
      while ((path[count] != ':') && (path[count] != '\0'))
	{
	  /* prevent writing data, which are out of bounds */
	  if ((unsigned int)(count - offset) < sizeof (dir))
	    dir[count - offset] = path[count];
	  else
	    valid = 0;
	  count++;
	}
	if (valid == 1)
          {
            char fullname[NAMESIZE];
            int len = snprintf(fullname, sizeof(fullname), "%s/%s", dir, devname);
            if ((len > 0) && (len <= (int)sizeof(fullname)))
              {
                if (!stat (fullname, &statbuf))
                  {
                    modes = statbuf.st_mode;
                    if (S_ISREG (modes))
                      return (1);		/* found as6edriver */
                  }
              }
          }
      if (path[count] == '\0')
	return (0);		/* end of path --no driver found */
      count++;
      offset = count;
    }
  return (0);
}


/*--------------------------------------------------------------------------*/
static SANE_Status
attach (const char *devname, AS6E_Device ** devp)
{

  AS6E_Device *dev;

/*  SANE_Status status;  */
  DBG (2, "attach\n");
  for (dev = first_dev; dev; dev = dev->next)
    {
      if (strcmp (dev->sane.name, devname) == 0)
	{
	  if (devp)
	    *devp = dev;
	  return (SANE_STATUS_GOOD);
	}
    }
  dev = malloc (sizeof (*dev));
  if (!dev)
    return (SANE_STATUS_NO_MEM);
  memset (dev, 0, sizeof (*dev));
  dev->sane.name = strdup (devname);
  if (!check_for_driver (devname))
    {
      free (dev);
      return (SANE_STATUS_INVAL);
    }

  dev->sane.model = "AS6E";
  dev->sane.vendor = "Artec";
  dev->sane.type = "flatbed scanner";
  ++num_devices;
  dev->next = first_dev;
  first_dev = dev;
  if (devp)
    *devp = dev;
  return (SANE_STATUS_GOOD);
}


/*--------------------------------------------------------------------------*/
SANE_Status
sane_open (SANE_String_Const devicename, SANE_Handle * handle)
{
  SANE_Status status;
  AS6E_Device *dev;
  AS6E_Scan *s;
  DBG (2, "sane_open\n");
  if (devicename[0])
    {
      for (dev = first_dev; dev; dev = dev->next)
	if (strcmp (dev->sane.name, devicename) == 0)
	  break;
      if (!dev)
	{
	  status = attach (devicename, &dev);
	  if (status != SANE_STATUS_GOOD)
	    return (status);
	}
    }
  else
    {
      /* empty devicname -> use first device */
      dev = first_dev;
    }
  if (!dev)
    return SANE_STATUS_INVAL;
  s = malloc (sizeof (*s));
  if (!s)
    return SANE_STATUS_NO_MEM;
  memset (s, 0, sizeof (*s));
  s->scan_buffer = malloc (SCAN_BUF_SIZE);
  if (!s->scan_buffer)
    return SANE_STATUS_NO_MEM;
  memset (s->scan_buffer, 0, SCAN_BUF_SIZE);
  s->line_buffer = malloc (SCAN_BUF_SIZE);
  if (!s->line_buffer)
    return SANE_STATUS_NO_MEM;
  memset (s->line_buffer, 0, SCAN_BUF_SIZE);
  status = as6e_open (s);
  if (status != SANE_STATUS_GOOD)
    return status;
  initialize_options_list (s);
  s->scanning = 0;
  /* insert newly opened handle into list of open handles: */
  s->next = first_handle;
  first_handle = s;
  *handle = s;
  return (status);
}

/*--------------------------------------------------------------------------*/
SANE_Status
sane_set_io_mode (SANE_Handle handle, SANE_Bool non_blocking)
{
  DBG (2, "sane_set_io_mode( %p, %d )\n", handle, non_blocking);
  return (SANE_STATUS_UNSUPPORTED);
}

/*---------------------------------------------------------------------------*/
SANE_Status
sane_get_select_fd (SANE_Handle handle, SANE_Int * fd)
{
  DBG (2, "sane_get_select_fd( %p, %p )\n",(void *)  handle, (void *) fd);
  return SANE_STATUS_UNSUPPORTED;
}