Mercurial > hg > old > magoroku_racing.bad
comparison sjoy.cc @ 99:c534f339ee8b
change c++
author | e085768 |
---|---|
date | Thu, 02 Jun 2011 17:51:41 +0900 |
parents | sjoy.c@0b65ca27f113 |
children | e5f2eb98b575 |
comparison
equal
deleted
inserted
replaced
98:1033fece71ce | 99:c534f339ee8b |
---|---|
1 // Copyright(C) 2001 Sony Computer Entertainment Inc. All Rights Reserved. | |
2 // | |
3 // "sjoy.c" | |
4 // | |
5 // | |
6 #include <stdio.h> | |
7 #include <unistd.h> | |
8 #include <fcntl.h> | |
9 #include <assert.h> | |
10 | |
11 | |
12 #include "sjoy.h" | |
13 | |
14 #define N_JOY 2 | |
15 | |
16 static char *g_devName[N_JOY] = { | |
17 "/dev/js0", | |
18 "/dev/js1", | |
19 }; | |
20 | |
21 static int g_fd[N_JOY] = { | |
22 -1, -1, | |
23 }; | |
24 | |
25 //static __u32 g_button[N_JOY]; // max 32 buttons per joystick | |
26 //static __s16 g_axis[N_JOY][2]; // max 2 axis per joystick | |
27 | |
28 //---------------------------------------------------------------------- | |
29 int sjoy_open(void) | |
30 { | |
31 int joy; | |
32 | |
33 sjoy_close(); | |
34 | |
35 for (joy = 0; joy < N_JOY; joy++) { | |
36 assert(g_fd[joy] == -1); | |
37 g_fd[joy] = open(g_devName[joy], O_RDONLY | O_NONBLOCK); | |
38 if (g_fd[joy] < 0) { | |
39 fprintf(stderr, "can't open %s\n", g_devName[joy]); | |
40 fprintf(stderr, | |
41 "You don't have permission, or should load module for joysticks.\n" | |
42 "How to load joystick module:\n" | |
43 " # modprobe ps2pad\n"); | |
44 return -1; | |
45 } | |
46 } | |
47 | |
48 return 0; | |
49 } | |
50 | |
51 //---------------------------------------------------------------------- | |
52 int sjoy_close(void) | |
53 { | |
54 int joy; | |
55 int fail = 0; | |
56 | |
57 for (joy = 0; joy < N_JOY; joy++) { | |
58 if (g_fd[joy] >= 0) { | |
59 fail |= close(g_fd[joy]); | |
60 } | |
61 g_fd[joy] = -1; | |
62 } | |
63 | |
64 return fail ? -1 : 0; | |
65 } | |
66 | |
67 /* 適当に自作:miya */ | |
68 struct js_event | |
69 { | |
70 int type; | |
71 int number; | |
72 int value; | |
73 }; | |
74 | |
75 int g_button[10]; | |
76 int g_axis[10][10]; | |
77 | |
78 #define JS_EVENT_INIT 1 | |
79 #define JS_EVENT_BUTTON 2 | |
80 #define JS_EVENT_AXIS 3 | |
81 /* ここまで適当に自作:miya */ | |
82 | |
83 | |
84 //---------------------------------------------------------------------- | |
85 void sjoy_poll(void) | |
86 { | |
87 int joy; | |
88 | |
89 for (joy = 0; joy < N_JOY; joy++) { | |
90 if (g_fd[joy] < 0) { | |
91 continue; | |
92 } | |
93 for (; ;) { | |
94 struct js_event e; | |
95 int n = read(g_fd[joy], &e, sizeof(e)); | |
96 if (n != sizeof(e)) { | |
97 break; | |
98 } | |
99 switch (e.type & ~JS_EVENT_INIT) { | |
100 case JS_EVENT_BUTTON: | |
101 g_button[joy] &= ~(1 << e.number); | |
102 g_button[joy] |= (e.value << e.number); | |
103 break; | |
104 | |
105 case JS_EVENT_AXIS: | |
106 g_axis[joy][e.number] = e.value; | |
107 break; | |
108 | |
109 default: | |
110 assert(0); | |
111 break; | |
112 } | |
113 } | |
114 } | |
115 | |
116 } | |
117 | |
118 //---------------------------------------------------------------------- | |
119 int sjoy_get_button(int joy) | |
120 { | |
121 return g_button[joy]; | |
122 } | |
123 | |
124 //---------------------------------------------------------------------- | |
125 int sjoy_get_axis(int joy, int axis) | |
126 { | |
127 return g_axis[joy][axis]; | |
128 } | |
129 | |
130 //---------------------------------------------------------------------- | |
131 int sjoy_get_ps2_button(int joy) | |
132 { | |
133 int w = g_button[joy]; | |
134 int a0 = g_axis[joy][0]; | |
135 int a1 = g_axis[joy][1]; | |
136 int th = 0x4000; | |
137 | |
138 w |= (a0 < -th) ? SJOY_PS2_L_LEFT : 0; | |
139 w |= (a1 > th) ? SJOY_PS2_L_DOWN : 0; | |
140 w |= (a0 > th) ? SJOY_PS2_L_RIGHT : 0; | |
141 w |= (a1 < -th) ? SJOY_PS2_L_UP : 0; | |
142 | |
143 return w; | |
144 } |