1 /* $NetBSD: main.c,v 1.4 2008/05/10 15:31:05 martin Exp $ */
4 * Copyright (c) 2002 TAKEMRUA Shin
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of The NetBSD Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/fcntl.h>
51 #include <sys/cdefs.h>
52 __RCSID("$NetBSD: main.c,v 1.4 2008/05/10 15:31:05 martin Exp $");
55 void load_data(const char *, struct tpctl_data
*);
56 void save_data(const char *, struct tpctl_data
*);
57 int do_calibration(const char *, struct tp
*, struct wsmouse_calibcoords
*);
58 void drawcross(struct fb
*, int, int, int, fb_pixel_t
);
59 int check_esc(void *);
69 fprintf(stderr
, "usage: %s [-D dispdev] [-d dev] [-f file] [-hnuv]\n",
76 main(int argc
, char *argv
[])
80 struct wsmouse_calibcoords
*pref
;
81 struct tpctl_data data
;
82 const char *data_file
;
84 const char *dispdev_name
;
86 /* set default values */
90 dev_name
= TPCTL_TP_DEVICE
;
91 dispdev_name
= TPCTL_FB_DEVICE
;
92 data_file
= TPCTL_DB_FILENAME
;
94 /* parse command line */
95 while ((ch
= getopt(argc
, argv
, "d:D:f:hnuv")) != -1) {
98 dispdev_name
= optarg
;
123 if (argv
[optind
] != NULL
) {
128 /* load calibrarion parameters from specified file */
129 load_data(data_file
, &data
);
131 /* open touch panel device and initialize touch panel routines */
132 if ((tpfd
= open(dev_name
, O_RDWR
)) < 0)
133 errx(EXIT_FAILURE
, "can't open touch panel");
134 if (tp_init(&tp
, tpfd
) < 0)
135 errx(EXIT_FAILURE
, "can't initialize touch panel");
137 /* find out saved parameters for the touch panel */
138 pref
= search_data(&data
, tp
.id
);
139 if (opt_forceupdate
|| pref
== NULL
) {
140 /* if the parameters wasn't found or '-f' options was
141 specified, do 'calibrarion' */
142 struct wsmouse_calibcoords coords
;
144 /* draw cursors and collect samples */
145 if (do_calibration(dispdev_name
, &tp
, &coords
) < 0) {
146 /* ESC key was pressed to abort */
149 /* update parameters with new one */
150 replace_data(&data
, tp
.id
, &coords
);
151 pref
= search_data(&data
, tp
.id
);
153 /* nothing is updated,
154 so you don't have to write back the data */
159 write_coords(stdout
, tp
.id
, pref
);
161 /* set calibration parameters into touch panel device */
162 if (tp_setcalibcoords(&tp
, pref
) < 0)
163 errx(EXIT_FAILURE
, "can't set samples");
165 /* save calibrarion parameters from specified file */
167 save_data(data_file
, &data
);
176 * load calibrarion parameters from specified file
178 * return: none (it won't return if some error occurs)
181 load_data(const char *data_file
, struct tpctl_data
*data
)
186 error
= read_data(data_file
, data
);
191 fprintf(stderr
, "%s: can't open %s\n", getprogname(),
193 /* it might be OK... */
196 fprintf(stderr
, "%s: I/O error on %s\n", getprogname(),
201 fprintf(stderr
, "%s: format error at %s, line %d\n",
202 getprogname(), data_file
, data
->lineno
);
206 fprintf(stderr
, "%s: duplicate entry at %s, line %d\n",
207 getprogname(), data_file
, data
->lineno
);
211 fprintf(stderr
, "%s: internal error\n", getprogname());
218 * save calibrarion parameters to specified file
220 * return: none (it won't return if some error occurs)
223 save_data(const char *data_file
, struct tpctl_data
*data
)
227 error
= write_data(data_file
, data
);
232 fprintf(stderr
, "%s: can't open %s\n", getprogname(),
237 fprintf(stderr
, "%s: I/O error on %s\n", getprogname(),
242 fprintf(stderr
, "%s: internal error\n", getprogname());
249 * draw cursors on frame buffer and collect samples in
250 * wamouse_calibcoords structure.
252 * return: 0 succeeded
253 * -1 aborted by user (ESC key was pressed)
254 * (it won't return if some error occurs)
257 do_calibration(const char *dev
, struct tp
*tp
,
258 struct wsmouse_calibcoords
*coords
)
262 int i
, x
, y
, xm
, ym
, cursize
, error
, res
;
264 /* open frame buffer device and initialize frame buffer routine */
265 if ((fbfd
= open(dev
, O_RDWR
)) < 0)
266 errx(EXIT_FAILURE
, "can't open frame buffer");
267 if (fb_init(&fb
, fbfd
) < 0)
268 errx(EXIT_FAILURE
, "can't map frame buffer");
270 memset(coords
, 0, sizeof(*coords
));
273 coords
->maxx
= fb
.conf
.hf_width
- 1;
274 coords
->maxy
= fb
.conf
.hf_height
- 1;
275 coords
->samplelen
= 5;
278 xm
= fb
.conf
.hf_width
/10;
279 ym
= fb
.conf
.hf_height
/10;
282 coords
->samples
[0].x
= fb
.conf
.hf_width
/2;
283 coords
->samples
[0].y
= fb
.conf
.hf_height
/2;
286 coords
->samples
[1].x
= xm
;
287 coords
->samples
[1].y
= ym
;
290 coords
->samples
[2].x
= xm
;
291 coords
->samples
[2].y
= fb
.conf
.hf_height
- ym
;
294 coords
->samples
[3].x
= fb
.conf
.hf_width
- xm
;
295 coords
->samples
[3].y
= fb
.conf
.hf_height
- ym
;
298 coords
->samples
[4].x
= fb
.conf
.hf_width
- xm
;
299 coords
->samples
[4].y
= ym
;
303 for (i
= 0; i
< coords
->samplelen
; i
++) {
305 coords
->samples
[i
].x
,
306 coords
->samples
[i
].y
,
310 res
= tp_get(tp
, &x
, &y
, check_esc
, 0 /* stdin */);
316 fb_dispmode(&fb
, WSDISPLAYIO_MODE_EMUL
);
317 return (-1); /* aborted by user */
319 coords
->samples
[i
].rawx
= x
;
320 coords
->samples
[i
].rawy
= y
;
322 coords
->samples
[i
].x
,
323 coords
->samples
[i
].y
,
326 tp_waitup(tp
, 200, check_esc
, 0 /* stdin */);
329 fb_dispmode(&fb
, WSDISPLAYIO_MODE_EMUL
);
333 printf("%s: %dx%d (%dbytes/line) %dbit offset=0x%lx\n",
337 fb
.conf
.hf_bytes_per_line
,
338 fb
.conf
.hf_pixel_width
,
344 errx(EXIT_FAILURE
, "can't get samples");
351 * draw cross cursor on frame buffer
356 drawcross(struct fb
*fb
, int x
, int y
, int size
, fb_pixel_t pixel
)
360 fb_drawline(fb
, x
, y
- size
+ 1, x
, y
- 1, pixel
);
361 fb_drawline(fb
, x
+ 1, y
- size
+ 1, x
+ 1, y
- 1, pixel
);
362 fb_drawline(fb
, x
, y
+ 2, x
, y
+ size
, pixel
);
363 fb_drawline(fb
, x
+ 1, y
+ 2, x
+ 1, y
+ size
, pixel
);
365 fb_drawline(fb
, x
- size
+ 1, y
, x
- 1, y
, pixel
);
366 fb_drawline(fb
, x
- size
+ 1, y
+ 1, x
- 1, y
+ 1, pixel
);
367 fb_drawline(fb
, x
+ 2, y
, x
+ size
, y
, pixel
);
368 fb_drawline(fb
, x
+ 2, y
+ 1, x
+ size
, y
+ 1, pixel
);
374 * date: input file descriptor
376 * return: 0 nothing has occurred
377 * 1 ESC key was pressed
381 check_esc(void *data
)
386 struct termios tm
, raw
;
388 if (tcgetattr(fd
, &tm
) < 0)
392 if (tcsetattr(fd
, TCSANOW
, &raw
) < 0)
394 if ((flg
= fcntl(fd
, F_GETFL
)) == -1)
396 if (fcntl(fd
, F_SETFL
, flg
| O_NONBLOCK
) == -1)
398 n
= read(fd
, buf
, 1);
400 fcntl(fd
, F_SETFL
, flg
);
401 tcsetattr(fd
, TCSANOW
, &tm
);
403 return (error
== EWOULDBLOCK
? 0 : -1);
405 return (0); /* EOF */
407 return (1); /* ESC */