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