/*-
* Copyright (c) 2002 TAKEMRUA Shin
* 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. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``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 FOUNDATION OR CONTRIBUTORS
* 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.
*/
/* parse command line */
while ((ch = getopt(argc, argv, "d:D:f:hnuv")) != -1) {
switch (ch) {
case 'D':
dispdev_name = optarg;
break;
case 'd':
dev_name = optarg;
break;
case 'f':
data_file = optarg;
break;
case 'h':
usage();
/* NOTREACHED */
case 'n':
opt_noupdate = 1;
break;
case 'u':
opt_forceupdate = 1;
break;
case 'v':
opt_verbose = 1;
break;
default:
usage();
/* NOTREACHED */
}
}
if (argv[optind] != NULL) {
usage();
/* NOTREACHED */
}
/* load calibration parameters from specified file */
load_data(data_file, &data);
/* open touch panel device and initialize touch panel routines */
if ((tpfd = open(dev_name, O_RDWR)) < 0)
errx(EXIT_FAILURE, "can't open touch panel");
if (tp_init(&tp, tpfd) < 0)
errx(EXIT_FAILURE, "can't initialize touch panel");
/* find out saved parameters for the touch panel */
pref = search_data(&data, tp.id);
if (opt_forceupdate || pref == NULL) {
/* if the parameters wasn't found or '-f' options was
specified, do 'calibration' */
struct wsmouse_calibcoords coords;
/* draw cursors and collect samples */
if (do_calibration(dispdev_name, &tp, &coords) < 0) {
/* ESC key was pressed to abort */
exit(EXIT_FAILURE);
}
/* update parameters with new one */
replace_data(&data, tp.id, &coords);
pref = search_data(&data, tp.id);
} else {
/* nothing is updated,
so you don't have to write back the data */
opt_noupdate = 1;
}
if (opt_verbose)
write_coords(stdout, tp.id, pref);
/* set calibration parameters into touch panel device */
if (tp_setcalibcoords(&tp, pref) < 0)
errx(EXIT_FAILURE, "can't set samples");
/* save calibration parameters from specified file */
if (!opt_noupdate)
save_data(data_file, &data);
/* dispose data */
free_data(&data);
exit(EXIT_SUCCESS);
/* NOTREACHED */
}
/*
* load calibration parameters from specified file
*
* return: none (it won't return if some error occurs)
*/
void
load_data(const char *data_file, struct tpctl_data *data)
{
int error;
init_data(data);
error = read_data(data_file, data);
switch (error) {
case ERR_NONE:
break;
case ERR_NOFILE:
fprintf(stderr, "%s: can't open %s\n", getprogname(),
data_file);
/* it might be OK... */
break;
case ERR_IO:
fprintf(stderr, "%s: I/O error on %s\n", getprogname(),
data_file);
exit(EXIT_FAILURE);
break;
case ERR_SYNTAX:
fprintf(stderr, "%s: format error at %s, line %d\n",
getprogname(), data_file, data->lineno);
exit(EXIT_FAILURE);
break;
case ERR_DUPNAME:
fprintf(stderr, "%s: duplicate entry at %s, line %d\n",
getprogname(), data_file, data->lineno);
exit(EXIT_FAILURE);
break;
default:
fprintf(stderr, "%s: internal error\n", getprogname());
exit(EXIT_FAILURE);
break;
}
}
/*
* save calibration parameters to specified file
*
* return: none (it won't return if some error occurs)
*/
void
save_data(const char *data_file, struct tpctl_data *data)
{
int error;
error = write_data(data_file, data);
switch (error) {
case ERR_NONE:
break;
case ERR_NOFILE:
fprintf(stderr, "%s: can't open %s\n", getprogname(),
data_file);
exit(EXIT_FAILURE);
break;
case ERR_IO:
fprintf(stderr, "%s: I/O error on %s\n", getprogname(),
data_file);
exit(EXIT_FAILURE);
break;
default:
fprintf(stderr, "%s: internal error\n", getprogname());
exit(EXIT_FAILURE);
break;
}
}
/*
* draw cursors on frame buffer and collect samples in
* wamouse_calibcoords structure.
*
* return: 0 succeeded
* -1 aborted by user (ESC key was pressed)
* (it won't return if some error occurs)
*/
int
do_calibration(const char *dev, struct tp *tp,
struct wsmouse_calibcoords *coords)
{
int fbfd;
struct fb fb;
int i, x, y, xm, ym, cursize, error, res;
/* open frame buffer device and initialize frame buffer routine */
if ((fbfd = open(dev, O_RDWR)) < 0)
errx(EXIT_FAILURE, "can't open frame buffer");
if (fb_init(&fb, fbfd) < 0)
errx(EXIT_FAILURE, "can't map frame buffer");
if (error) {
errno = error;
errx(EXIT_FAILURE, "can't get samples");
}
return (0);
}
/*
* draw cross cursor on frame buffer
*
* return: none
*/
void
drawcross(struct fb *fb, int x, int y, int size, fb_pixel_t pixel)
{
size /= 2;
fb_drawline(fb, x, y - size + 1, x, y - 1, pixel);
fb_drawline(fb, x + 1, y - size + 1, x + 1, y - 1, pixel);
fb_drawline(fb, x, y + 2, x, y + size, pixel);
fb_drawline(fb, x + 1, y + 2, x + 1, y + size, pixel);
fb_drawline(fb, x - size + 1, y, x - 1, y, pixel);
fb_drawline(fb, x - size + 1, y + 1, x - 1, y + 1, pixel);
fb_drawline(fb, x + 2, y, x + size, y, pixel);
fb_drawline(fb, x + 2, y + 1, x + size, y + 1, pixel);
}
/*
* check ESC key
*
* date: input file descriptor
*
* return: 0 nothing has occurred
* 1 ESC key was pressed
* -1 error
*/
int
check_esc(void *data)
{
int fd = (int)(intptr_t)data;
int flg, n, error;
char buf[1];
struct termios tm, raw;
if (tcgetattr(fd, &tm) < 0)
return (-1);
raw = tm;
cfmakeraw(&raw);
if (tcsetattr(fd, TCSANOW, &raw) < 0)
return (-1);
if ((flg = fcntl(fd, F_GETFL)) == -1)
return (-1);
if (fcntl(fd, F_SETFL, flg | O_NONBLOCK) == -1)
return (-1);
n = read(fd, buf, 1);
error = errno;
fcntl(fd, F_SETFL, flg);
tcsetattr(fd, TCSANOW, &tm);
if (n < 0)
return (error == EWOULDBLOCK ? 0 : -1);
if (n == 0)
return (0); /* EOF */
if (*buf == 0x1b)
return (1); /* ESC */