Mercurial > remote-gamepad
annotate source/main.c @ 3:4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 18 Aug 2015 00:25:10 +0100 |
parents | 5ba54fc65608 |
children | 557d35fcf15a |
rev | line source |
---|---|
0 | 1 /* |
2 Circle Pad example made by Aurelio Mannara for ctrulib | |
3 Please refer to https://github.com/smealum/ctrulib/blob/master/libctru/include/3ds/services/hid.h for more information | |
4 This code was modified for the last time on: 12/13/2014 2:20 UTC+1 | |
5 | |
6 This wouldn't be possible without the amazing work done by: | |
7 -Smealum | |
8 -fincs | |
9 -WinterMute | |
10 -yellows8 | |
11 -plutoo | |
12 -mtheall | |
13 -Many others who worked on 3DS and I'm surely forgetting about | |
14 */ | |
15 | |
16 #include <3ds.h> | |
17 #include <arpa/inet.h> | |
18 #include <assert.h> | |
19 #include <malloc.h> | |
20 #include <netdb.h> | |
21 #include <stdio.h> | |
22 #include <string.h> | |
23 #include <sys/socket.h> | |
24 | |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
25 struct state_t { |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
26 u32 keys; |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
27 circlePosition pad, cpad; |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
28 touchPosition touch; |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
29 }; |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
30 |
0 | 31 static u32 *SOC_buffer = NULL; |
32 | |
33 int create_socket(int domain, int type, struct sockaddr *addr) | |
34 { | |
35 int fd = socket(domain, type, 0); | |
36 if (fd < 0) { | |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
37 puts("socket() failed."); |
0 | 38 return -1; |
39 } | |
40 | |
41 if (addr) { | |
42 if (connect(fd, addr, sizeof(*addr)) < 0) { | |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
43 puts("connect() failed."); |
0 | 44 return -1; |
45 } | |
46 } | |
47 | |
48 return fd; | |
49 } | |
50 | |
51 struct sockaddr* get_sockaddr_from_hostname(const char *hostname, int port) | |
52 { | |
53 struct hostent *server = gethostbyname(hostname); | |
54 if (!server) { | |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
55 puts("gethostbyname() failed."); |
0 | 56 return NULL; |
57 } | |
58 | |
59 struct sockaddr_in *addr = calloc(1, sizeof(struct sockaddr_in)); | |
60 if (!addr) { | |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
61 puts("calloc() failed."); |
0 | 62 return NULL; |
63 } | |
64 | |
65 //memset(&addr, 0, sizeof(struct sockaddr_in)); | |
66 addr->sin_family = AF_INET; | |
67 addr->sin_port = htons(port); | |
68 memcpy(&addr->sin_addr.s_addr, server->h_addr_list[0], server->h_length); | |
69 | |
70 return (struct sockaddr*) addr; | |
71 } | |
72 | |
73 struct sockaddr* get_sockaddr_from_ip(const char *hostname, int port) | |
74 { | |
75 struct sockaddr_in *addr = calloc(1, sizeof(struct sockaddr_in)); | |
76 if (!addr) { | |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
77 puts("calloc() failed."); |
0 | 78 return NULL; |
79 } | |
80 | |
81 //memset(&addr, 0, sizeof(struct sockaddr_in)); | |
82 addr->sin_family = AF_INET; | |
83 addr->sin_port = htons(port); | |
84 addr->sin_addr.s_addr = inet_addr(hostname); | |
85 | |
86 return (struct sockaddr*) addr; | |
87 } | |
88 | |
89 struct network_t { | |
90 int tcp_fd; | |
91 int udp_fd; | |
92 struct sockaddr *addr; | |
93 int addr_size; | |
94 }; | |
95 | |
96 struct network_t *networkInit(const char *hostname, int port) | |
97 { | |
98 const unsigned SOC_ALIGN = 0x1000; | |
99 const unsigned SOC_BUFFERSIZE = 0x100000; | |
100 | |
1
5ba54fc65608
Revert a stupid s/memalign/calloc/ in soc:U initialisation.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
101 SOC_buffer = (u32*)memalign(SOC_ALIGN, SOC_BUFFERSIZE); |
0 | 102 if (!SOC_buffer) { |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
103 puts("memalign() failed."); |
0 | 104 return NULL; |
105 } | |
106 | |
107 unsigned initialized = SOC_Initialize(SOC_buffer, SOC_BUFFERSIZE); | |
108 if (initialized) { | |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
109 puts("SOC_Initialize() failed."); |
0 | 110 return NULL; |
111 } | |
112 | |
113 //struct sockaddr *addr = get_sockaddr_from_hostname(hostname, port); | |
114 struct sockaddr *addr = get_sockaddr_from_ip(hostname, port); | |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
115 if (!addr) { |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
116 puts("get_sockaddr_from_ip() failed."); |
0 | 117 return NULL; |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
118 } |
0 | 119 |
120 // TODO: IPv6 | |
121 int domain = AF_INET; | |
122 | |
123 struct network_t *n = malloc(sizeof(struct network_t)); | |
124 //n->tcp_fd = create_socket(domain, SOCK_STREAM, addr); | |
125 n->udp_fd = create_socket(domain, SOCK_DGRAM, NULL); | |
126 n->addr = addr; | |
127 n->addr_size = sizeof(struct sockaddr_in); | |
128 return n; | |
129 } | |
130 | |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
131 int sendState(struct network_t *n, struct state_t *state) |
0 | 132 { |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
133 const char *msg = (const char*) state; |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
134 const int msglen = sizeof(struct state_t); |
0 | 135 return sendto(n->udp_fd, msg, msglen, 0, n->addr, n->addr_size); |
136 } | |
137 | |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
138 void exitWithMessage(const char *message) |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
139 { |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
140 puts(message); |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
141 fflush(stdout); |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
142 gfxFlushBuffers(); |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
143 gfxSwapBuffers(); |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
144 for (int i=0; i < 60; ++i) |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
145 gspWaitForVBlank(); |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
146 gfxExit(); |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
147 } |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
148 |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
149 int main(void) |
0 | 150 { |
151 //Matrix containing the name of each key. Useful for printing when a key is pressed | |
152 char keysNames[32][32] = { | |
153 "KEY_A", "KEY_B", "KEY_SELECT", "KEY_START", | |
154 "KEY_DRIGHT", "KEY_DLEFT", "KEY_DUP", "KEY_DDOWN", | |
155 "KEY_R", "KEY_L", "KEY_X", "KEY_Y", | |
156 "", "", "KEY_ZL", "KEY_ZR", | |
157 "", "", "", "", | |
158 "KEY_TOUCH", "", "", "", | |
159 "KEY_CSTICK_RIGHT", "KEY_CSTICK_LEFT", "KEY_CSTICK_UP", "KEY_CSTICK_DOWN", | |
160 "KEY_CPAD_RIGHT", "KEY_CPAD_LEFT", "KEY_CPAD_UP", "KEY_CPAD_DOWN" | |
161 }; | |
162 | |
163 // Initialize services | |
164 gfxInitDefault(); | |
165 | |
166 //Initialize console on top screen. Using NULL as the second argument tells the console library to use the internal console structure as current one | |
167 consoleInit(GFX_TOP, NULL); | |
168 | |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
169 struct state_t old_state = {0xffffffff, {0xffff, 0xffff}, {0xffff, 0xffff}, {0xffff, 0xffff}}; //In these variables there will be information about keys detected in the previous frame |
0 | 170 |
171 // Initialize the network | |
172 struct network_t *n = networkInit("192.168.0.13", 16150); | |
173 if (!n) { | |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
174 exitWithMessage("networkInit() failed."); |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
175 return 1; |
0 | 176 } |
177 | |
178 // Main loop | |
179 while (aptMainLoop()) | |
180 { | |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
181 static struct state_t state; |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
182 |
0 | 183 //Scan all the inputs. This should be done once for each frame |
184 hidScanInput(); | |
185 | |
186 //hidKeysHeld returns information about which buttons have are held down in this frame | |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
187 state.keys = hidKeysHeld(); |
0 | 188 |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
189 //Read both pads position |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
190 hidCircleRead(&state.pad); |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
191 hidCstickRead(&state.cpad); |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
192 |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
193 // FIXME: use HOME instead |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
194 if (state.keys & KEY_START) break; // break in order to return to hbmenu |
0 | 195 |
196 //Do the keys printing only if keys have changed | |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
197 if (memcmp(&state, &old_state, sizeof(struct state_t))) |
0 | 198 { |
199 //Clear console | |
200 consoleClear(); | |
201 | |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
202 //Every line must be rewritten because we cleared the whole console |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
203 printf("\x1b[0;0HPress Start to exit.\n"); |
0 | 204 |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
205 //Print both pads position |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
206 printf("CirclePad position: %04d; %04d\n", state.pad.dx, state.pad.dy); |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
207 printf("CPad position: %04d; %04d\n", state.cpad.dx, state.cpad.dy); |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
208 |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
209 //Move the cursor to the fourth row because on the third one we'll write the circle pad position |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
210 printf("\x1b[3;0H"); |
0 | 211 |
212 //Check if some of the keys are down, held or up | |
213 for (int i = 0; i < 32; i++) | |
214 { | |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
215 if (state.keys & BIT(i)) |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
216 printf("%s\n", keysNames[i]); |
0 | 217 } |
218 | |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
219 if (sendState(n, &state) < 0) { |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
220 exitWithMessage("sendState() failed."); |
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
221 return 1; |
0 | 222 } |
223 } | |
224 | |
225 //Set keys old values for the next frame | |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
226 memcpy(&old_state, &state, sizeof(struct state_t)); |
0 | 227 |
228 // Flush and swap framebuffers | |
229 gfxFlushBuffers(); | |
230 gfxSwapBuffers(); | |
231 | |
232 //Wait for VBlank | |
233 gspWaitForVBlank(); | |
234 } | |
235 | |
236 // Exit services | |
3
4c4be527a8d5
Send the entire state (including both pads and touch screen), and other fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
237 exitWithMessage("Exiting..."); |
0 | 238 return 0; |
239 } |