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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 /*
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 Circle Pad example made by Aurelio Mannara for ctrulib
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 Please refer to https://github.com/smealum/ctrulib/blob/master/libctru/include/3ds/services/hid.h for more information
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 This code was modified for the last time on: 12/13/2014 2:20 UTC+1
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 This wouldn't be possible without the amazing work done by:
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 -Smealum
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 -fincs
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 -WinterMute
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 -yellows8
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 -plutoo
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 -mtheall
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 -Many others who worked on 3DS and I'm surely forgetting about
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 */
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 #include <3ds.h>
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 #include <arpa/inet.h>
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 #include <assert.h>
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 #include <malloc.h>
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 #include <netdb.h>
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 #include <stdio.h>
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 #include <string.h>
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 #include <sys/socket.h>
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 static u32 *SOC_buffer = NULL;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 int create_socket(int domain, int type, struct sockaddr *addr)
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 {
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 int fd = socket(domain, type, 0);
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 return -1;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 }
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 if (addr) {
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 return -1;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 }
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 }
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 return fd;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 }
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 struct sockaddr* get_sockaddr_from_hostname(const char *hostname, int port)
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 {
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 struct hostent *server = gethostbyname(hostname);
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 return NULL;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 }
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 struct sockaddr_in *addr = calloc(1, sizeof(struct sockaddr_in));
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 return NULL;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 }
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 //memset(&addr, 0, sizeof(struct sockaddr_in));
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 addr->sin_family = AF_INET;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 addr->sin_port = htons(port);
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 memcpy(&addr->sin_addr.s_addr, server->h_addr_list[0], server->h_length);
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 return (struct sockaddr*) addr;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 }
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 struct sockaddr* get_sockaddr_from_ip(const char *hostname, int port)
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 {
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 struct sockaddr_in *addr = calloc(1, sizeof(struct sockaddr_in));
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 return NULL;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 }
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 //memset(&addr, 0, sizeof(struct sockaddr_in));
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 addr->sin_family = AF_INET;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 addr->sin_port = htons(port);
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 addr->sin_addr.s_addr = inet_addr(hostname);
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 return (struct sockaddr*) addr;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 }
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 struct network_t {
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 int tcp_fd;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 int udp_fd;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 struct sockaddr *addr;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 int addr_size;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94 };
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96 struct network_t *networkInit(const char *hostname, int port)
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 {
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 const unsigned SOC_ALIGN = 0x1000;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 const unsigned SOC_BUFFERSIZE = 0x100000;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 return NULL;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105 }
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 unsigned initialized = SOC_Initialize(SOC_buffer, SOC_BUFFERSIZE);
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110 return NULL;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111 }
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113 //struct sockaddr *addr = get_sockaddr_from_hostname(hostname, port);
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
120 // TODO: IPv6
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121 int domain = AF_INET;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
123 struct network_t *n = malloc(sizeof(struct network_t));
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
124 //n->tcp_fd = create_socket(domain, SOCK_STREAM, addr);
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
125 n->udp_fd = create_socket(domain, SOCK_DGRAM, NULL);
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
126 n->addr = addr;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127 n->addr_size = sizeof(struct sockaddr_in);
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 return n;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129 }
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135 return sendto(n->udp_fd, msg, msglen, 0, n->addr, n->addr_size);
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
136 }
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
150 {
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
151 //Matrix containing the name of each key. Useful for printing when a key is pressed
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
152 char keysNames[32][32] = {
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
153 "KEY_A", "KEY_B", "KEY_SELECT", "KEY_START",
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154 "KEY_DRIGHT", "KEY_DLEFT", "KEY_DUP", "KEY_DDOWN",
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 "KEY_R", "KEY_L", "KEY_X", "KEY_Y",
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
156 "", "", "KEY_ZL", "KEY_ZR",
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
157 "", "", "", "",
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
158 "KEY_TOUCH", "", "", "",
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
159 "KEY_CSTICK_RIGHT", "KEY_CSTICK_LEFT", "KEY_CSTICK_UP", "KEY_CSTICK_DOWN",
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
160 "KEY_CPAD_RIGHT", "KEY_CPAD_LEFT", "KEY_CPAD_UP", "KEY_CPAD_DOWN"
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
161 };
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
162
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
163 // Initialize services
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
164 gfxInitDefault();
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
165
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
167 consoleInit(GFX_TOP, NULL);
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
170
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
171 // Initialize the network
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172 struct network_t *n = networkInit("192.168.0.13", 16150);
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
176 }
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
177
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
178 // Main loop
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
179 while (aptMainLoop())
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
183 //Scan all the inputs. This should be done once for each frame
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
184 hidScanInput();
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
185
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
195
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
198 {
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
199 //Clear console
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
200 consoleClear();
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
211
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
212 //Check if some of the keys are down, held or up
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
213 for (int i = 0; i < 32; i++)
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
217 }
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
222 }
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
223 }
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
224
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
227
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
228 // Flush and swap framebuffers
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
229 gfxFlushBuffers();
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
230 gfxSwapBuffers();
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
231
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
232 //Wait for VBlank
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
233 gspWaitForVBlank();
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
234 }
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
235
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
238 return 0;
4e831ec746e4 Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
239 }