summaryrefslogtreecommitdiff
path: root/usr.sbin/ssio/get_scanner.c
blob: 0e565837723c606db2b70ab66ee4d3b8b3d5bdd2 (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
static char *rcs_id = "$Id: get_scanner.c,v 1.5 2002/06/01 20:27:15 deraadt Exp $";
/*
 * Copyright (c) 1995 Kenneth Stailey
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed for the NetBSD Project
 *	by Kenneth Stailey
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * get_scanner.c: retrieve the a scanner's parameters
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#if defined (__NetBSD__) || defined(__OpenBSD__)
#include <sys/ioctl.h>
#endif
#include <sys/scanio.h>

int print_type(int type) {
  static char *scanners[] = {
    "kibo",
    "ricoh_is410",
    "fujitsu_m3069g",
    "hp_scanjet_IIc",
    "ricoh_fs1",
    "sharp_jx600",
    "ricoh_is50",
    "ibm_2456",
    "umax_uc630",
    "umax_ug630",
    "mustek_06000cx",
    "mustek_12000cx",
  };

  if (type < 0 || type > MUSTEK_12000CX)
    return 1;

  puts(scanners[type]);
  return 0;
}

void
usage(char *prog_name)
{
  fprintf(stderr, "usage: %s [-p][-q][-l <logical name>]\n", prog_name);
  exit(1);
}


main(int argc, char *argv[])
{
  int s_fd;
  struct scan_io s_io;

  int quiet = FALSE;
  int do_pixels = FALSE;
  int do_type = FALSE;

  char *logical_name = "scan0";
  char device_special[255];

  int c;
  extern int optind;
  extern char *optarg;

  while ((c = getopt(argc, argv, "tpql:")) != -1) {
    switch (c) {
    case 'q':
      quiet = TRUE;
      break;

    case 'l':
      logical_name = optarg;
      break;

    case 'p':
      do_pixels = TRUE;
      break;

    case 't':
      do_type = TRUE;
      break;

    case '?':
      usage(argv[0]);
      break;
    }
  }

  snprintf(device_special, sizeof device_special, "/dev/%s", logical_name);
  if ((s_fd = open(device_special, O_RDONLY)) < 0) {
    perror("open of scanner");
    exit(1);
  }

  if (ioctl(s_fd, SCIOCGET, &s_io) < 0) {
    perror("ioctl on scanner");
    exit(1);
  }

  if (do_pixels) {
    printf("%d %d\n", s_io.scan_pixels_per_line, s_io.scan_lines);
    exit(0);
  }

  if (do_type)
    return (print_type(s_io.scan_scanner_type));

  if (quiet) {
    printf("-w %g -h %g -x %g -y %g -b %d -c %d -r %d",
	   (double)s_io.scan_width / 1200.0,
	   (double)s_io.scan_height / 1200.0,
	   (double)s_io.scan_x_origin / 1200.0,
	   (double)s_io.scan_y_origin / 1200.0,
	   s_io.scan_brightness,
	   s_io.scan_contrast,
	   s_io.scan_x_resolution);
    switch (s_io.scan_image_mode) {
    case SIM_BINARY_MONOCHROME:
      printf(" -i m\n");
      break;
    case SIM_DITHERED_MONOCHROME:
      printf(" -i d\n");
      break;
    case SIM_GRAYSCALE:
      printf(" -i g\n");
      break;
    case SIM_COLOR:
      printf(" -i c\n");
      break;
    case SIM_RED:
      printf(" -i R\n");
      break;
    case SIM_GREEN:
      printf(" -i G\n");
      break;
    case SIM_BLUE:
      printf(" -i B\n");
      break;
    }
  } else {
    printf("width = %g inches\n", (double)s_io.scan_width / 1200.0);
    printf("height = %g inches\n", (double)s_io.scan_height / 1200.0);
    printf("x_origin = %g inches\n", (double)s_io.scan_x_origin / 1200.0);
    printf("y_origin = %g inches\n", (double)s_io.scan_y_origin / 1200.0);
    printf("brightness = %d\n", s_io.scan_brightness);
    printf("contrast = %d\n", s_io.scan_contrast);
    printf("resolution = %d dpi\n", s_io.scan_x_resolution);
    printf("image_mode = ");
    switch (s_io.scan_image_mode) {
    case SIM_BINARY_MONOCHROME:
      printf("binary_monochrome\n");
      break;
    case SIM_DITHERED_MONOCHROME:
      printf("dithered_monochrome\n");
      break;
    case SIM_GRAYSCALE:
      printf("grayscale\n");
      break;
    case SIM_COLOR:
      printf("color\n");
      break;
    case SIM_RED:
      printf("red\n");
      break;
    case SIM_GREEN:
      printf("green\n");
      break;      
    case SIM_BLUE:
      printf("blue\n");
      break;      
    }
  }

  close(s_fd);
}