libreshop_client/source/main.c

146 lines
3.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <gccore.h>
#include <network.h>
#include <wiiuse/wpad.h>
#include <stdbool.h>
#include "sandia/sandia.h"
#include <jansson.h>
// Uncomment if this is a development build
//#include "debug.h"
static void *xfb = NULL;
static GXRModeObj *rmode = NULL;
static char version[] = { "0.0dev\0" };
void printheader() {
printf("\x1b[44m");
printf(" \n");
printf(" LibreShop v%s \n", version);
printf(" \n");
printf("\x1b[40m\n");
}
void logprint(int type, char *message) {
switch(type) {
case 1:
printf("\x1b[34m[\x1b[32mOK\x1b[34m]");
break;
case 2:
printf("\x1b[34m[\x1b[31m--\x1b[34m]");
break;
case 3:
printf("\x1b[31m!!\x1b[34m]");
break;
default:
printf(" ");
}
printf(" \x1b[37m%s", message);
}
int main(int argc, char **argv) {
s32 ret;
char localip[16] = {0};
char gateway[16] = {0};
char netmask[16] = {0};
VIDEO_Init();
WPAD_Init();
rmode = VIDEO_GetPreferredMode(NULL);
xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
VIDEO_Configure(rmode);
VIDEO_SetNextFramebuffer(xfb);
VIDEO_SetBlack(FALSE);
VIDEO_Flush();
VIDEO_WaitVSync();
if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
printf("\x1b[1;0H");
printheader();
logprint(0, "Welcome to LibreShop!\n");
#ifdef DEBUG_H
logprint(2, "Warning! You are running a DEBUG build.\n");
#endif
logprint(0, "Initializing network.\n");
ret = if_config(localip, netmask, gateway, TRUE, 20);
if (ret>=0) {
logprint(1, "Initialized successfully!\n");
logprint(0, "Your Wii's IP address is ");
printf("%s.\n", localip);
sandia req = sandia_create("libreshop.donut.eu.org", 80);
sandia_response res = sandia_get_request(&req, "/");
bool lastWasNewline = false;
int resBodyLength = strlen(res.body);
for (int i = 0; i < resBodyLength; i++) {
if (res.body[i] == '\r') continue;
if (res.body[i] == '\n') {
if (lastWasNewline) {
int rBL = resBodyLength - i;
char* body = malloc(rBL);
for (int j = 0; j < rBL; j++) {
body[j] = res.body[j + i + 1];
}
json_error_t error;
json_t *root = json_loads(body, 0, &error);
if (!root || !json_is_array(root)) {
printf("failed decoding\n");
}
else {
printf("%s\n", body);
for (int j = 0; j < json_array_size(root); j++) {
json_t *data = json_array_get(root, j);
printf("%s\n", json_string_value(data));
}
}
break;
}
else lastWasNewline = true;
}
else lastWasNewline = false;
}
printf("\n");
sandia_close(&req);
} else {
logprint(0, "Network configuration failed :(\n");
}
while(1) {
WPAD_ScanPads();
u32 pressed = WPAD_ButtonsDown(0);
if ( pressed & WPAD_BUTTON_HOME ) {
logprint(0, "HOME pressed, quitting!");
exit(0);
}
VIDEO_WaitVSync();
}
return 0;
}