自己修改minigui库后的文件,可以直接用于800x600显示器的5点校准
程序1(修改后dummy.c文件)
/*
**
Id:dummy.c73352007−08−1603:38:27Zxgwang
**
** dummy.c: The dummy IAL engine.
**
** Copyright (C) 2003 ~ 2007 Feynman Software.
** Copyright (C) 2001 ~ 2002 Wei Yongming.
**
** Created by Wei Yongming, 2001/09/13
*/
include
include
include
include “common.h”
ifdef _DUMMY_IAL
include
include
include
include
include
include
include
include
include
include “ial.h”
include “dummy.h”
static struct tsdev *ts = NULL;
static int mousex = 0;
static int mousey = 0;
static int button = 0;
static int kbfd = 0;
static char keystate[128] = { 0 };
static void getxy(int focus);
/************** Low Level Input Operations ************/
static int keyboard_update(void)
{
struct input_event input;
//printf(“keyboard update\n”);
if (kbfd > 0)
{
if (read(kbfd, &input, sizeof(input)) == sizeof(input))
{
memset(keystate, 0, sizeof(keystate));
if(input.type == EV_KEY)
{
if ((input.code >= 0) && (input.code < sizeof(keystate)/sizeof(keystate[0])))
{
if (input.value > 0)
{
keystate[input.code] = 1;
//printf(“keyboard_update, key down, input.code = %d\n”, input.code);
}
else
{
//printf(“keyboard_update, key up, input.code = %d\n”, input.code);
}
}
return 128;
}
}
}
return 0;
}
static char* keyboard_getstate()
{
return keystate;
}
/*
* Mouse operations – Event
*/
static int mouse_update(void)
{
if(!calibrate_flag)
{
struct ts_sample samp;
int ret = ts_read(ts, &samp, 1);
if (ret == 1) {
if (samp.pressure > 0) {
mousex = samp.x;
mousey = samp.y;
}
button = (samp.pressure > 0) ? IAL_MOUSE_LEFTBUTTON : 0;
// printf(“tslibial: button = %d, mousex = %d, mousey = %d\n”, button, mousex, mousey);
return 1;
}else{
return 0;
}
}
else
{
if(button)
{
button = 0;
}
else
{
if(cal_focus < 5)
{
printf(“cal_focus = %d\n”, cal_focus);
getxy(cal_focus);
cal_focus ++;
}
}
printf(“tslibial: button = %d, mousex = %d, mousey = %d\n”, button, mousex, mousey);
return 1;
}
}
static void mouse_getxy (int* x, int* y)
{
*x = mousex;
*y = mousey;
}
static int mouse_getbutton(void)
{
return button;
}
static int wait_event (int which, fd_set *in, fd_set *out, fd_set *except,
struct timeval *timeout)
{
struct timeval to = { 0, 10000 };
if (mouse_update() > 0)
{
return IAL_MOUSEEVENT;
}
fd_set input_set;
FD_ZERO(&input_set);
FD_SET(kbfd, &input_set);
if(select(kbfd + 1, &input_set, NULL, NULL, &to) > 0)
{
if(FD_ISSET(kbfd, &input_set))
{
return IAL_KEYEVENT;
}
}
return -1;
}
BOOL InitDummyInput (INPUT* input, const char* mdev, const char* mtype)
{
const char* tsdevice;
printf(“InitTsLibInput\n”);
if ((tsdevice = getenv (“TSLIB_TSDEVICE”)) == NULL) {
tsdevice = mdev;
}
printf("tsdevice = %s\n", tsdevice);
if (tsdevice == NULL) {
_MG_PRINTF ("IAL>TSLib: Please specify the ts device\n");
return FALSE;
}
ts = ts_open (tsdevice, 0);
if (!ts) {
_MG_PRINTF ("IAL>TSLib: can not open ts device\n");
return FALSE;
}
if (ts_config (ts)) {
_MG_PRINTF ("IAL>TSLib: can not config ts device\n");
return FALSE;
}
input->update_mouse = mouse_update;
input->get_mouse_xy = mouse_getxy;
input->set_mouse_xy = NULL;
input->get_mouse_button = mouse_getbutton;
input->set_mouse_range = NULL;
input->wait_event = wait_event;
kbfd = open("/dev/input/event0", O_RDONLY | O_NONBLOCK);
printf("kbfd = %d\n", kbfd);
input->update_keyboard = keyboard_update;
input->get_keyboard_state = keyboard_getstate;
input->suspend_keyboard = NULL;
input->resume_keyboard = NULL;
input->set_leds = NULL;
return TRUE;
}
void TermDummyInput (void)
{
if (ts) {
ts_close(ts);
ts = NULL;
}
if (kbfd > 0)
{
close(kbfd);
}
}
static int sort_by_x(const void* a, const void *b)
{
return (((struct ts_sample )a)->x - ((struct ts_sample )b)->x);
}
static int sort_by_y(const void* a, const void *b)
{
return (((struct ts_sample )a)->y - ((struct ts_sample )b)->y);
}
static void getxy(int focus)
{
static char tmp = -1;
if (tmp != focus)
{
mousex = cal.xfb[focus];
mousey = cal.yfb[focus];
tmp = focus;
button = IAL_MOUSE_LEFTBUTTON;
}
define MAX_SAMPLES 128
struct ts_sample samp[MAX_SAMPLES];
int index, middle;
do
{
while (ts_read_raw(ts, &samp[0], 1) < 0)
{
continue;
}
}
while (samp[0].pressure == 0);
/* Now collect up to MAX_SAMPLES touches into the samp array. */
index = 0;
do
{
if (index < MAX_SAMPLES-1)
index++;
while (ts_read_raw(ts, &samp[index], 1) <= 0)
{
continue;
}
}
while (samp[index].pressure > 0);
printf("Took %d samples...\n",index);
/*
* At this point, we have samples in indices zero to (index-1)
* which means that we have (index) number of samples. We want
* to calculate the median of the samples so that wild outliers
* don't skew the result. First off, let's assume that arrays
* are one-based instead of zero-based. If this were the case
* and index was odd, we would need sample number ((index+1)/2)
* of a sorted array; if index was even, we would need the
* average of sample number (index/2) and sample number
* ((index/2)+1). To turn this into something useful for the
* real world, we just need to subtract one off of the sample
* numbers. So for when index is odd, we need sample number
* (((index+1)/2)-1). Due to integer division truncation, we
* can simplify this to just (index/2). When index is even, we
* need the average of sample number ((index/2)-1) and sample
* number (index/2). Calculate (index/2) now and we'll handle
* the even odd stuff after we sort.
*/
middle = index/2;
if (&cal.x[focus])
{
qsort(samp, index, sizeof(struct ts_sample), sort_by_x);
if (index & 1)
cal.x[focus] = samp[middle].x;
else
cal.x[focus] = (samp[middle-1].x + samp[middle].x) / 2;
}
if (&cal.y[focus])
{
qsort(samp, index, sizeof(struct ts_sample), sort_by_y);
if (index & 1)
cal.y[focus] = samp[middle].y;
else
cal.y[focus] = (samp[middle-1].y + samp[middle].y) / 2;
}
}
static int calibration_perform(CALIBRATE *cal)
{
int j;
float n, x, y, x2, y2, xy, z, zx, zy;
float det, a, b, c, e, f, i;
float scaling = 65536.0;
// Get sums for matrix
n = x = y = x2 = y2 = xy = 0;
for(j=0; j<5; j++)
{
n += 1.0;
x += (float)cal->x[j];
y += (float)cal->y[j];
x2 += (float)(cal->x[j]*cal->x[j]);
y2 += (float)(cal->y[j]*cal->y[j]);
xy += (float)(cal->x[j]*cal->y[j]);
}
// Get determinant of matrix – check if determinant is too small
det = n*(x2*y2 - xy*xy) + x*(xy*y - x*y2) + y*(x*xy - y*x2);
if(det < 0.1 && det > -0.1)
{
printf(“ts_calibrate: determinant is too small – %f\n”,det);
return 0;
}
// Get elements of inverse matrix
a = (x2*y2 - xy*xy)/det;
b = (xy*y - x*y2)/det;
c = (x*xy - y*x2)/det;
e = (n*y2 - y*y)/det;
f = (x*y - n*xy)/det;
i = (n*x2 - x*x)/det;
// Get sums for x calibration
z = zx = zy = 0;
for(j=0; j<5; j++)
{
z += (float)cal->xfb[j];
zx += (float)(cal->xfb[j]*cal->x[j]);
zy += (float)(cal->xfb[j]*cal->y[j]);
}
// Now multiply out to get the calibration for framebuffer x coord
cal->a[0] = (int)((a*z + b*zx + c*zy)*(scaling));
cal->a[1] = (int)((b*z + e*zx + f*zy)*(scaling));
cal->a[2] = (int)((c*z + f*zx + i*zy)*(scaling));
printf("%f %f %f\n",(a*z + b*zx + c*zy),
(b*z + e*zx + f*zy),
(c*z + f*zx + i*zy));
// Get sums for y calibration
z = zx = zy = 0;
for(j=0; j<5; j++)
{
z += (float)cal->yfb[j];
zx += (float)(cal->yfb[j]*cal->x[j]);
zy += (float)(cal->yfb[j]*cal->y[j]);
}
// Now multiply out to get the calibration for framebuffer y coord
cal->a[3] = (int)((a*z + b*zx + c*zy)*(scaling));
cal->a[4] = (int)((b*z + e*zx + f*zy)*(scaling));
cal->a[5] = (int)((c*z + f*zx + i*zy)*(scaling));
printf("%f %f %f\n",(a*z + b*zx + c*zy),
(b*z + e*zx + f*zy),
(c*z + f*zx + i*zy));
// If we got here, we’re OK, so assign scaling to a[6] and return
cal->a[6] = (int)scaling;
return 1;
}
/*
* save pointercal file
* 2013/11/06
*/
int calibrate_save ()
{
char *calfile = NULL;
int cal_fd;
char cal_buffer[256];
int i;
calibration_perform(&cal);
printf (“Calibration constants: “);
for (i = 0; i < 7; i++) printf(“%d “, cal.a[i]);
printf(“\n”);
if ((calfile = getenv(“TSLIB_CALIBFILE”)) != NULL)
{
cal_fd = open (calfile, O_CREAT | O_RDWR);
}
else
{
cal_fd = open (“/etc/pointercal”, O_CREAT | O_RDWR);
}
sprintf (cal_buffer,"%d %d %d %d %d %d %d",
cal.a[1], cal.a[2], cal.a[0],
cal.a[4], cal.a[5], cal.a[3], cal.a[6]);
write (cal_fd, cal_buffer, strlen (cal_buffer) + 1);
sync ();
close (cal_fd);
ts_end ();
ts_init (1);
return TRUE;
}
void ts_end(void)
{
if (ts) {
ts_close(ts);
ts = NULL;
}
}
void ts_create(void)
{
char pcalbuf[200];
int index;
char *tokptr;
char *calfile = getenv(“TSLIB_CALIBFILE”);
if (calfile == NULL)
{
calfile = “/etc/pointercal”;
}
int pcal_fd = open(calfile, O_RDONLY);
if(pcal_fd < 0)
{
printf(“Error: open calibrate file\n”);
}
else
{
read(pcal_fd, pcalbuf, 200);
cal.a[0] = atoi(strtok(pcalbuf, ” “));
index = 1;
while(index < 7)
{
tokptr = strtok(NULL,” “);
if(*tokptr!=’\0’)
{
cal.a[index] = atoi(tokptr);
index++;
}
}
close(pcal_fd);
}
cal.xfb[0] = 50;
cal.xfb[1] = 750;
cal.xfb[2] = 750;
cal.xfb[3] = 50;
cal.xfb[4] = 400;
cal.yfb[0] = 50;
cal.yfb[1] = 50;
cal.yfb[2] = 550;
cal.yfb[3] = 550;
cal.yfb[4] = 300;
}
int ts_init(int nonblock)
{
const char* tsdevice;
printf(“InitTsLibInput\n”);
if ((tsdevice = getenv (“TSLIB_TSDEVICE”)) == NULL) {
tsdevice = “/dev/touchscreen-1wire”;
}
printf("tsdevice = %s\n", tsdevice);
if (tsdevice == NULL) {
_MG_PRINTF ("IAL>TSLib: Please specify the ts device\n");
return FALSE;
}
ts = ts_open (tsdevice, nonblock);
if (!ts) {
_MG_PRINTF ("IAL>TSLib: can not open ts device\n");
return FALSE;
}
if (ts_config (ts)) {
_MG_PRINTF ("IAL>TSLib: can not config ts device\n");
return FALSE;
}
if(nonblock)
{
__mg_cur_input->update_mouse = mouse_update;
__mg_cur_input->get_mouse_xy = mouse_getxy;
__mg_cur_input->set_mouse_xy = NULL;
__mg_cur_input->get_mouse_button = mouse_getbutton;
__mg_cur_input->set_mouse_range = NULL;
__mg_cur_input->wait_event = wait_event;
}
cal_focus = 0;
}
void SetCalFlag(char flag)
{
calibrate_flag = flag;
}
endif /* _DUMMY_IAL */
程序2(界面程序):
include
include
include
include
include
include
include
include
include “resource.h”
include “func.h”
include “lang.h”
include “com.h”
include “tscal.h”
extern void ts_end(void);
extern void ts_create(void);
extern int ts_init(int nonblock);
extern int calibrate_save ();
extern void SetCalFlag(char flag);
/*
*
*/
static int TscalWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
int fb[5][2] = {
{50, 50},
{750, 50},
{750, 550},
{50, 550},
{400, 300}
};
switch(message)
{
case MSG_CREATE:
{
ts_create();
ts_end();
ts_init(0);
SetCalFlag(1);
}
break;
case MSG_PAINT:
{
HDC hdc = GetClientDC(hWnd);
RECT rect;
GetClientRect(hWnd, &rect);
SetBkMode(hdc, BM_TRANSPARENT);
SetTextColor(hdc, COLOR_HQ_YELLOW(hdc));
rect.left += 300;
rect.right -= 300;
rect.top = 200;
rect.bottom = rect.top + 30;
DrawText(hdc, GetWindowCaption(hWnd), -1, &rect, DT_CENTER
| DT_VCENTER | DT_SINGLELINE);
WND_EXTRA_DATA data;
data.value = GetWindowAdditionalData(hWnd);
rect.left = fb[data.pwnd_tscal->index][0] - 10;
rect.right = rect.left + 20;
rect.top = fb[data.pwnd_tscal->index][1] - 10;
rect.bottom = rect.top + 20;
DrawCross(hdc, &rect);
ReleaseDC(hdc);
}
break;
case MSG_CLOSE:
{
DestroyMainWindow(hWnd);
MainWindowCleanup(hWnd);
ShowCursor(SW_SHOW);
}
return 0;
case MSG_DESTROY:
{
ShowWindow(hWnd, SW_HIDE);
DestroyAllControls (hWnd);
}
break;
case MSG_ERASEBKGND:
{
HDC hdc = GetClientDC(hWnd);
RECT rect;
if(lParam)
{
memcpy(&rect, (RECT*) lParam, sizeof(RECT));
}
else
{
GetClientRect(hWnd, &rect);
}
int width = GET_RECT_WIDTH(rect);
int height = GET_RECT_HEIGHT(rect);
SetBrushColor(hdc, COLOR_HQ_BLACK(hdc));
FillBox(hdc, rect.left, rect.top, width, height);
ReleaseDC(hdc);
}
return 0;
case MSG_LBUTTONDOWN:
{
WND_EXTRA_DATA data;
data.value = GetWindowAdditionalData(hWnd);
int cal_focus = data.pwnd_tscal->index;
printf("focus = %d\n", cal_focus);
if(data.pwnd_tscal->index > 3)
{
SetCalFlag(0);
calibrate_save();
PostMessage(hWnd, MSG_CLOSE, 0, 0);
}
else
{
RECT rect;
rect.left = fb[data.pwnd_tscal->index][0] - 10;
rect.right = rect.left + 20;
rect.top = fb[data.pwnd_tscal->index][1] - 10;
rect.bottom = rect.top + 20;
InvalidateRect(hWnd, &rect, TRUE);
++data.pwnd_tscal->index;
rect.left = fb[data.pwnd_tscal->index][0] - 10;
rect.right = rect.left + 20;
rect.top = fb[data.pwnd_tscal->index][1] - 10;
rect.bottom = rect.top + 20;
InvalidateRect(hWnd, &rect, TRUE);
}
}
break;
}
return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
*
*/
HWND CreateTscalWindow(HWND hHosting)
{
MAINWINCREATE CreateInfo;
CreateInfo.dwStyle = WS_VISIBLE;
CreateInfo.dwExStyle = WS_EX_NONE;
CreateInfo.spCaption = GetString(480);
CreateInfo.hMenu = 0;
CreateInfo.hCursor = GetSystemCursor(0);
CreateInfo.hIcon = 0;
CreateInfo.MainWindowProc = TscalWinProc;
CreateInfo.lx = 0;
CreateInfo.ty = 0;
CreateInfo.rx = CX_SCREEN;
CreateInfo.by = CY_SCREEN;
CreateInfo.iBkColor = COLOR_lightwhite;
CreateInfo.dwAddData = (int)calloc(1, sizeof(WND_TSCAL));
CreateInfo.hHosting = hHosting;
HWND hWnd = CreateMainWindow(&CreateInfo);
if(hWnd != HWND_INVALID)
{
ShowCursor(SW_HIDE);
ShowWindow(hWnd, SW_SHOWNORMAL);
}
return hWnd;
}