added mdpcjoy
[megadrive.git] / mdpcjoy / mdpcjoy.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <linux/input.h>
4 #include <sys/ioctl.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <sys/io.h>
10
11 int main()
12 {
13         int i, ret, fd;
14         int dpad = 0;
15
16         for (i = 0;; i++)
17         {
18                 int support = 0;
19                 char name[64];
20
21                 snprintf(name, sizeof(name), "/dev/input/event%d", i);
22                 fd = open(name, O_RDONLY);
23                 if (fd == -1)
24                         break;
25
26                 /* check supported events */
27                 ret = ioctl(fd, EVIOCGBIT(0, sizeof(support)), &support);
28                 if (ret == -1) {
29                         printf("ioctl failed on %s\n", name);
30                         goto skip;
31                 }
32
33                 if (!(support & (1 << EV_KEY)))
34                         goto skip;
35                 if (!(support & (1 << EV_ABS)))
36                         goto skip;
37
38                 ioctl(fd, EVIOCGNAME(sizeof(name)), name);
39                 printf("found \"%s\" (type %08x)\n", name, support);
40                 break;
41
42 skip:
43                 close(fd);
44                 fd = -1;
45         }
46
47         if (fd == -1) {
48                 printf("no suitable devs\n");
49                 return 1;
50         }
51
52         ret = ioperm(888, 3, 1);
53         if (ret != 0) {
54                 perror("ioperm");
55                 return 1;
56         }
57
58         outb(0xc0, 890);        /* switch to output mode, TL and TR high */
59         outb(0x0f, 888);        /* Dx high */
60
61         while (1)
62         {
63                 struct input_event ev;
64                 int rd;
65
66                 rd = read(fd, &ev, sizeof(ev));
67                 if (rd < (int) sizeof(ev)) {
68                         perror("in_evdev: error reading");
69                         return 1;
70                 }
71
72                 if (ev.type == EV_KEY) {
73                         int val = 0xc0;
74                         if (ev.code & 1) {
75                                 if (ev.value)
76                                         val |= 2;
77                                 else
78                                         val &= ~2;
79                         } else {
80                                 if (ev.value)
81                                         val |= 8;
82                                 else
83                                         val &= ~8;
84                         }
85                         outb(val, 890);
86                 }
87                 else if (ev.type == EV_ABS)
88                 {
89                         if (ev.code == ABS_X) {
90                                 if (ev.value < 128)
91                                         dpad |= 4;
92                                 else if (ev.value > 128)
93                                         dpad |= 8;
94                                 else
95                                         dpad &= ~0x0c;
96                         }
97                         if (ev.code == ABS_Y) {
98                                 if (ev.value < 128)
99                                         dpad |= 1;
100                                 else if (ev.value > 128)
101                                         dpad |= 2;
102                                 else
103                                         dpad &= ~3;
104                         }
105                         outb(~dpad & 0x0f, 888);
106                 }
107         }
108
109         return 0;
110 }
111