- Make the draw_text() function more generic.

- Display an error message on the screen when the calibration error
  is too high before re-starting.
This commit is contained in:
matthieu 2007-08-31 21:53:55 +00:00
parent 69e64ab313
commit 7c79bd957e

View File

@ -1,4 +1,4 @@
/* $OpenBSD: xtsscale.c,v 1.5 2007/08/31 21:17:33 matthieu Exp $ */ /* $OpenBSD: xtsscale.c,v 1.6 2007/08/31 21:53:55 matthieu Exp $ */
/* /*
* Copyright (c) 2007 Robert Nagy <robert@openbsd.org> * Copyright (c) 2007 Robert Nagy <robert@openbsd.org>
* *
@ -50,6 +50,7 @@
#define Background "white" #define Background "white"
#define TouchCross "black" #define TouchCross "black"
#define PromptText "black" #define PromptText "black"
#define Error "red"
/* where the calibration points are placed */ /* where the calibration points are placed */
#define SCREEN_DIVIDE 16 #define SCREEN_DIVIDE 16
@ -65,7 +66,7 @@ int screen;
Window root; Window root;
Window win; Window win;
XftFont *font; XftFont *font;
XftColor cross, promptColor, bg; XftColor cross, errorColor, promptColor, bg;
XftDraw *draw; XftDraw *draw;
unsigned int width, height; /* window size */ unsigned int width, height; /* window size */
char *progname; char *progname;
@ -74,6 +75,18 @@ int evfd;
int cx[5], cy[5]; int cx[5], cy[5];
int x[5], y[5]; int x[5], y[5];
static char *prompt_message[] = {
"TOUCH SCREEN CALIBRATION",
"Press on the cross hairs please...",
NULL
};
static char *error_message[] = {
"Not accurate enough.",
"Try again...",
NULL
};
void void
get_events(int i) get_events(int i)
{ {
@ -140,6 +153,11 @@ render_init(void)
fprintf(stderr, "cannot get bg color"); fprintf(stderr, "cannot get bg color");
exit(2); exit(2);
} }
if (!XftColorAllocName(display, XDefaultVisual(display, screen),
DefaultColormap(display, screen), Error, &errorColor)) {
fprintf(stderr, "cannot get color");
exit(2);
}
draw = XftDrawCreate(display, win, DefaultVisual(display, screen), draw = XftDrawCreate(display, win, DefaultVisual(display, screen),
DefaultColormap(display, screen)); DefaultColormap(display, screen));
} }
@ -179,55 +197,29 @@ draw_point(int x, int y, int width, int size, XftColor *color)
} }
void void
draw_text() draw_text(char **message, XftColor *color)
{ {
static char *prompt[] = { int len;
"TOUCH SCREEN CALIBRATION",
"Press on the cross hairs please..."
};
#define num (sizeof(prompt) / sizeof(prompt[0]))
static int init = 0;
static int p_len[num];
static int p_xpos[num];
static int p_height = 0;
static int p_maxwidth = 0;
int i, x, y; int i, x, y;
int line_height;
XGlyphInfo extents; XGlyphInfo extents;
if (!init) { i = 0;
for (i = 0; i < num; i++) { y = height / 3;
p_len[i] = strlen(prompt[i]); while (message[i] != NULL) {
XftTextExtents8(display, font, prompt[i], len = strlen(message[i]);
p_len[i], &extents); XftTextExtents8(display, font, message[i], len, &extents);
p_xpos[i] = (width - extents.width)/2; x = (width - extents.width)/2;
if (extents.width > p_maxwidth) XftDrawString8(draw, color, font, x, y, message[i], len);
p_maxwidth = extents.width; y += extents.height * 1.5;
if (extents.height > p_height) i++;
p_height = extents.height;
}
init = 1;
} }
line_height = p_height * 1.5;
y = height / 2 - 6 * line_height;
for (i = 0; i < num; i++) {
XftDrawString8(draw, &promptColor, font,
p_xpos[i], y+i*line_height, prompt[i], p_len[i]);
}
#undef num
} }
void void
draw_graphics(int i, int j, int n) draw_graphics(int i, int j, int n)
{ {
XftColor *color;
draw_text(); draw_text(prompt_message, &promptColor);
color = &cross;
if (n == 2) { if (n == 2) {
cx[n] = width / 2; cx[n] = width / 2;
@ -236,7 +228,7 @@ draw_graphics(int i, int j, int n)
cx[n] = (MARK_POINT[i] * width) / SCREEN_MAX; cx[n] = (MARK_POINT[i] * width) / SCREEN_MAX;
cy[n] = (MARK_POINT[j] * height) / SCREEN_MAX; cy[n] = (MARK_POINT[j] * height) / SCREEN_MAX;
} }
draw_point(cx[n], cy[n], width / 200, width / 64, color); draw_point(cx[n], cy[n], width / 200, width / 64, &cross);
} }
Cursor Cursor
@ -351,7 +343,7 @@ calib:
if (fabs(xerr) > fabs(a * width * .01)) { if (fabs(xerr) > fabs(a * width * .01)) {
fprintf(stderr, "X error (%.2f) too high, try again\n", fprintf(stderr, "X error (%.2f) too high, try again\n",
fabs(xerr)); fabs(xerr));
goto calib; goto err;
} }
wmcoords.minx = (int) (b + 0.5); wmcoords.minx = (int) (b + 0.5);
wmcoords.maxx = (int) (a * width + b + 0.5); wmcoords.maxx = (int) (a * width + b + 0.5);
@ -369,7 +361,7 @@ calib:
if (fabs(yerr) > fabs(a * height * 0.01)) { if (fabs(yerr) > fabs(a * height * 0.01)) {
fprintf(stderr, "Y error (%.2f) too high, try again\n", fprintf(stderr, "Y error (%.2f) too high, try again\n",
fabs(yerr)); fabs(yerr));
goto calib; goto err;
} }
wmcoords.miny = (int) (b + 0.5); wmcoords.miny = (int) (b + 0.5);
wmcoords.maxy = (int) (a * height + b + 0.5); wmcoords.maxy = (int) (a * height + b + 0.5);
@ -390,4 +382,9 @@ calib:
wmcoords.resx, wmcoords.resy); wmcoords.resx, wmcoords.resy);
return 0; return 0;
err:
draw_text(error_message, &errorColor);
XFlush(display);
sleep(2);
goto calib;
} }