]> git.sur5r.net Git - cc65/blob - libsrc/cbm/penadjust.c
Fixed bugs; and, improved the efficiency of some pce library functions.
[cc65] / libsrc / cbm / penadjust.c
1 /*
2 ** Main lightpen driver calibration functions.
3 **
4 ** 2013-07-25, Greg King
5 */
6
7
8 #include <stddef.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <pen.h>
12
13
14 static const char *name;
15
16
17 /* Get a lightpen calibration value from a file if it exists.  Otherwise, call
18 ** pen_calibrate() to create a value; then, write it into a file, so that it
19 ** will be available at the next time that the lightpen is used.
20 ** Might change the screen.
21 */
22 static void __fastcall__ adjuster (unsigned char *XOffset)
23 {
24     int fd = open (name, O_RDONLY);
25
26     if (fd < 0) {
27         pen_calibrate (XOffset);
28         fd = open (name, O_WRONLY | O_CREAT | O_EXCL);
29         if (fd >= 0) {
30             (void) write (fd, XOffset, 1);
31             close (fd);
32         }
33     } else {
34         (void) read (fd, XOffset, 1);
35         close (fd);
36     }
37 }
38
39
40 /* pen_adjust() is optional; if you want to use its feature,
41 ** then it must be called before a driver is installed.
42 ** Note:  This function merely saves the file-name pointer, and sets
43 ** the pen_adjuster pointer.  The file will be read only when a driver
44 ** is installed, and only if that driver wants to be calibrated.
45 */
46 void __fastcall__ pen_adjust (const char *filename)
47 {
48     if (filename != NULL && filename[0] != '\0') {
49         name = filename;
50         pen_adjuster = adjuster;
51     } else {
52         pen_adjuster = pen_calibrate;
53     }
54 }