Example #1
0
File: main.c Project: Jeflux/3DSC
int main(int argc, char **argv) {
	gfxInitDefault();
	socInit((u32 *)memalign(0x1000, 0x100000), 0x100000);

	// Check wifi status
	u32 wifiStatus = 0;
	ACU_GetWifiStatus(&wifiStatus);
	if (!wifiStatus) {
		printf("\x1b[1;1HNo WiFi! Is your wireless slider on?");
	}

	// Use printf on top screen
	consoleInit(GFX_TOP, NULL);

	// Stuff for network magic
	int recvlen;
	unsigned char buf[BUFSIZE];
	unsigned char IDBuf[BUFSIZE];

	// Try create socket
	int err = 0;
	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
		err = 1;
	}

	// Setup socket addresses
	out.sin_family = in.sin_family = AF_INET;
	out.sin_port = in.sin_port = htons(PORT); // Set port
	in.sin_addr.s_addr = INADDR_ANY;

	// Try to bind socket to port
	if (err == 0 && bind(sock, (struct sockaddr *)&in, sizeof(in)) < 0)
		err = 2;

	// If any errors
	if (err != 0)
		printf("\x1b[4;1HError opening connection: %i", err);

	// Set socket receive to non-blocking to be able to exit while listening for broadcast
	fcntl(sock, F_SETFL, O_NONBLOCK);


	bool connected = false;

	// Main loop
	while (aptMainLoop()) {

		if (err == 0) {
			// If not connected, listen for broadcast
			if (!connected) {
				printf("\x1b[1;1HListening for broadcast on port %d", PORT);

				// Listen for packets. Returns packet size
				recvlen = recvfrom(sock, buf, BUFSIZE, 0, (struct sockaddr *)&out, &addrlen);

				// Check if correct packet. recvlen < 0 -> Error
				if (recvlen > 0) {
					printf("\x1b[2;1Hr"); // Debug print. Writes r for any packet received
					buf[recvlen] = 0; // Don't remember what this is for. Oops

									  // Check if message is connection port (Broadcast sends out port number as broadcast)
					int i = atoi(buf);
					if (i == PORT) {
						// If broadcast message, assign send address to received address
						in.sin_addr.s_addr = out.sin_addr.s_addr;

						// Prepare program; Setting flag, turning of backlight, etc
						connected = true;
						consoleClear();
						printf("\x1b[1;1HConnected");
						if (backlightOff == false)
							disableBacklight();
						backlightOff = true;
					}
				}
			}
		}

		printf("\x1b[28;1HPlayer ID: %i", playerID);
		printf("\x1b[29;1HPress START and SELECT to exit");

		// Scan input
		hidScanInput();
		// Save keystate
		u32 kDown = hidKeysHeld();

		if ((kDown & KEY_START) && (kDown & KEY_SELECT)) break; // break in order to return to hbmenu

		// If no errors and connected. Contruct input message
		if (err == 0 && connected) {
			// Get circle pad state
			circlePosition pos;
			hidCircleRead(&pos);

			// Construct message
			message.ID = playerID;
			message.pdx = pos.dx;
			message.pdy = pos.dy;
			message.btn = kDown;

			// Query for touchscreen information
			touchPosition touch;
			hidTouchRead(&touch);

			message.touch_px = touch.px;
			message.touch_py = touch.py;

			// Send packet to address broadcast came from
			sendto(sock, &message, sizeof(message), 0, (struct sockaddr *)&in, sizeof(in));

			int recv = 0;
			int count = 0;
			do {
				count++;
				// Send packet to address broadcast came from
				sendto(sock, &message, sizeof(message), 0, (struct sockaddr *)&in, sizeof(in));
				// Listen for packet to get player ID and check if server is alive
				//int recv = read(sock, IDBuf, sizeof(IDBuf));
				recv = recvfrom(sock, IDBuf, BUFSIZE, 0, (struct sockaddr *)&out, &addrlen);
			} while (atoi(IDBuf) == PORT && count < 5);

			if (count < 5 && recv > 0)
				playerID = IDBuf[0] | IDBuf[1] << 1;
			else {
				connected = false;
				playerID = 0;
				consoleClear();
				printf("\x1b[2;1HDisconnected");

				if (backlightOff == true) {
					enableBacklight();
					backlightOff = false;
				}
			}
		}

		u64 sleepDuration = 16000000ULL;
		svcSleepThread(sleepDuration);

		// Draw stuff
		gfxFlushBuffers();
		gfxSwapBuffers();
		gspWaitForVBlank();
	}

	// On exit
	if (backlightOff == true)
		enableBacklight();
	socExit();
	gfxExit();
	return 0;
}
Example #2
0
int main(void) {
	acInit();
	gfxInitDefault();
	
	gfxSetDoubleBuffering(GFX_TOP, false);
	gfxSetDoubleBuffering(GFX_BOTTOM, false);
	
	if(setjmp(exitJmp)) goto exit;
	
	preRenderKeyboard();
	
	clearScreen();
	drawString(10, 10, "Initing FS...");
	gfxFlushBuffers();
	gfxSwapBuffers();
	
	fsInit();
	
	clearScreen();
	drawString(10, 10, "Initing SOC...");
	gfxFlushBuffers();
	gfxSwapBuffers();
	
	SOC_Initialize((u32 *)memalign(0x1000, 0x100000), 0x100000);
	
	u32 wifiStatus = 0;
	ACU_GetWifiStatus(NULL, &wifiStatus);
	if(!wifiStatus) {
		hang("No WiFi! Is your wireless slider on?");
	}
	
	clearScreen();
	drawString(10, 10, "Reading settings...");
	gfxFlushBuffers();
	gfxSwapBuffers();
	
	if(!readSettings()) {
		hang("Could not read 3DSController.ini!");
	}
	
	clearScreen();
	drawString(10, 10, "Connecting to %s on port %d...", settings.IPString, settings.port);
	gfxFlushBuffers();
	gfxSwapBuffers();
	
	openSocket(settings.port);
	sendConnectionRequest();
	
	clearScreen();
	gfxFlushBuffers();
	gfxSwapBuffers();
	
	disableBacklight();
	
	while(aptMainLoop()) {
		hidScanInput();
		irrstScanInput();
		
		u32 kHeld = hidKeysHeld();
		circlePosition circlePad;
		circlePosition cStick;
		hidCstickRead(&cStick);
		hidCircleRead(&circlePad);
		touchPosition touch;
		touchRead(&touch);
		
		clearScreen();
		
		if((kHeld & KEY_L) && (kHeld & KEY_R) && (kHeld & KEY_X)) {
			if(keyboardToggle) {
				keyboardActive = !keyboardActive;
				keyboardToggle = false;
				
				if(keyboardActive) enableBacklight();
			}
		}
		else keyboardToggle = true;
		
		if(keyboardActive) {
			drawKeyboard();
			
			if(touch.px >= 1 && touch.px <= 312 && touch.py >= 78 && touch.py <= 208) {
				int x = (int)((float)touch.px * 12.0f / 320.0f);
				int y = (int)((float)(touch.py - 78) * 12.0f / 320.0f);
				int width = 24;
				int height = 24;
				
				if(keyboardChars[x + y * 12] == ' ') {
					while(keyboardChars[(x - 1) + y * 12] == ' ') x--;
					
					width = (int)(5.0f * 320.0f / 12.0f) - 1;
				}
				
				else if(keyboardChars[x + y * 12] == '\13') {
					while(keyboardChars[(x - 1) + y * 12] == '\13') x--;
					while(keyboardChars[x + (y - 1) * 12] == '\13') y--;
					
					width = (int)(2.0f * 320.0f / 12.0f) - 1;
					height = (int)(3.0f * 320.0f / 12.0f) - 1;
				}
				
				if(keyboardChars[x + y * 12]) drawBox((int)((float)x * 320.0f / 12.0f) + 1, (int)(78.0f + (float)y * 320.0f / 12.0f) + 1, width, height, 31, 31, 0);
			}
		}
		
		sendKeys(kHeld, circlePad, touch, cStick);
		
		//receiveBuffer(sizeof(struct packet));
		
		if((kHeld & KEY_START) && (kHeld & KEY_SELECT)) longjmp(exitJmp, 1);
		
		gfxFlushBuffers();
		gspWaitForVBlank();
		gfxSwapBuffers();
	}
	
	exit:
	
	enableBacklight();
	
	SOC_Shutdown();
	
	svcCloseHandle(fileHandle);
	fsExit();
	
	gfxExit();
	acExit();
	
	return 0;
}