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