changeset 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 5ee81fece0fa
children 557d35fcf15a
files source/main.c
diffstat 1 files changed, 59 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/source/main.c
+++ b/source/main.c
@@ -22,19 +22,25 @@
 #include <string.h>
 #include <sys/socket.h>
 
+struct state_t {
+	u32 keys;
+	circlePosition pad, cpad;
+	touchPosition touch;
+};
+
 static u32 *SOC_buffer = NULL;
 
 int create_socket(int domain, int type, struct sockaddr *addr)
 {
 	int fd = socket(domain, type, 0);
 	if (fd < 0) {
-		printf("socket() failed.\n");
+		puts("socket() failed.");
 		return -1;
 	}
 
 	if (addr) {
 		if (connect(fd, addr, sizeof(*addr)) < 0) {
-			printf("connect() failed.\n");
+			puts("connect() failed.");
 			return -1;
 		}
 	}
@@ -46,13 +52,13 @@ struct sockaddr* get_sockaddr_from_hostn
 {
 	struct hostent *server = gethostbyname(hostname);
 	if (!server) {
-		printf("gethostbyname() failed.\n");
+		puts("gethostbyname() failed.");
 		return NULL;
 	}
 
 	struct sockaddr_in *addr = calloc(1, sizeof(struct sockaddr_in));
 	if (!addr) {
-		printf("calloc() failed.\n");
+		puts("calloc() failed.");
 		return NULL;
 	}
 
@@ -68,7 +74,7 @@ struct sockaddr* get_sockaddr_from_ip(co
 {
 	struct sockaddr_in *addr = calloc(1, sizeof(struct sockaddr_in));
 	if (!addr) {
-		printf("calloc() failed.\n");
+		puts("calloc() failed.");
 		return NULL;
 	}
 
@@ -94,20 +100,22 @@ struct network_t *networkInit(const char
 
 	SOC_buffer = (u32*)memalign(SOC_ALIGN, SOC_BUFFERSIZE);
 	if (!SOC_buffer) {
-		printf("calloc() failed.\n");
+		puts("memalign() failed.");
 		return NULL;
 	}
 
 	unsigned initialized = SOC_Initialize(SOC_buffer, SOC_BUFFERSIZE);
 	if (initialized) {
-		printf("SOC_Initialize() failed.\n");
+		puts("SOC_Initialize() failed.");
 		return NULL;
 	}
 
 	//struct sockaddr *addr = get_sockaddr_from_hostname(hostname, port);
 	struct sockaddr *addr = get_sockaddr_from_ip(hostname, port);
-	if (!addr)
+	if (!addr) {
+		puts("get_sockaddr_from_ip() failed.");
 		return NULL;
+	}
 
 	// TODO: IPv6
 	int domain = AF_INET;
@@ -120,14 +128,25 @@ struct network_t *networkInit(const char
 	return n;
 }
 
-int sendKeys(struct network_t *n, u32 keys)
+int sendState(struct network_t *n, struct state_t *state)
 {
-	const char *msg = (const char*) &keys;
-	int msglen = sizeof(keys);
+	const char *msg = (const char*) state;
+	const int msglen = sizeof(struct state_t);
 	return sendto(n->udp_fd, msg, msglen, 0, n->addr, n->addr_size);
 }
 
-int main(int argc, char **argv)
+void exitWithMessage(const char *message)
+{
+	puts(message);
+	fflush(stdout);
+	gfxFlushBuffers();
+	gfxSwapBuffers();
+	for (int i=0; i < 60; ++i)
+		gspWaitForVBlank();
+	gfxExit();
+}
+
+int main(void)
 {
 	//Matrix containing the name of each key. Useful for printing when a key is pressed
 	char keysNames[32][32] = {
@@ -147,70 +166,64 @@ int main(int argc, char **argv)
 	//Initialize console on top screen. Using NULL as the second argument tells the console library to use the internal console structure as current one
 	consoleInit(GFX_TOP, NULL);
 
-	u32 old_keys = 0; //In these variables there will be information about keys detected in the previous frame
+	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
 
 	// Initialize the network
 	struct network_t *n = networkInit("192.168.0.13", 16150);
 	if (!n) {
-		printf("HORROR!");
-		fflush(stdout);
-		gfxFlushBuffers();
-		gfxSwapBuffers();
-		for(;;);
+		exitWithMessage("networkInit() failed.");
+		return 1;
 	}
 
-	printf("\x1b[0;0HPress Start to exit.");
-	printf("\x1b[1;0HCirclePad position:");
-
 	// Main loop
 	while (aptMainLoop())
 	{
+		static struct state_t state;
+
 		//Scan all the inputs. This should be done once for each frame
 		hidScanInput();
 
 		//hidKeysHeld returns information about which buttons have are held down in this frame
-		u32 keys = hidKeysHeld();
+		state.keys = hidKeysHeld();
 
-		//if (keys & KEY_START) break; // break in order to return to hbmenu
+		//Read both pads position
+		hidCircleRead(&state.pad);
+		hidCstickRead(&state.cpad);
+
+		// FIXME: use HOME instead
+		if (state.keys & KEY_START) break; // break in order to return to hbmenu
 
 		//Do the keys printing only if keys have changed
-		if (keys != old_keys)
+		if (memcmp(&state, &old_state, sizeof(struct state_t)))
 		{
 			//Clear console
 			consoleClear();
 
-			//These two lines must be rewritten because we cleared the whole console
-			printf("\x1b[0;0HPress Start to exit.");
-			printf("\x1b[1;0HCirclePad position:");
+			//Every line must be rewritten because we cleared the whole console
+			printf("\x1b[0;0HPress Start to exit.\n");
 
-			printf("\x1b[3;0H"); //Move the cursor to the fourth row because on the third one we'll write the circle pad position
+			//Print both pads position
+			printf("CirclePad position: %04d; %04d\n", state.pad.dx, state.pad.dy);
+			printf("CPad position:      %04d; %04d\n", state.cpad.dx, state.cpad.dy);
+
+			//Move the cursor to the fourth row because on the third one we'll write the circle pad position
+			printf("\x1b[3;0H");
 
 			//Check if some of the keys are down, held or up
 			for (int i = 0; i < 32; i++)
 			{
-				if (keys & BIT(i)) printf("%s\n", keysNames[i]);
+				if (state.keys & BIT(i))
+					printf("%s\n", keysNames[i]);
 			}
 
-			if (sendKeys(n, keys) < 0) {
-				perror("sendKeys");
-				printf("HORROR! sendKeys()!");
-				fflush(stdout);
-				gfxFlushBuffers();
-				gfxSwapBuffers();
-				for(;;);
+			if (sendState(n, &state) < 0) {
+				exitWithMessage("sendState() failed.");
+				return 1;
 			}
 		}
 
 		//Set keys old values for the next frame
-		old_keys = keys;
-
-		circlePosition pos;
-
-		//Read the CirclePad position
-		hidCircleRead(&pos);
-
-		//Print the CirclePad position
-		printf("\x1b[2;0H%04d; %04d", pos.dx, pos.dy);
+		memcpy(&old_state, &state, sizeof(struct state_t));
 
 		// Flush and swap framebuffers
 		gfxFlushBuffers();
@@ -221,6 +234,6 @@ int main(int argc, char **argv)
 	}
 
 	// Exit services
-	gfxExit();
+	exitWithMessage("Exiting...");
 	return 0;
 }