Skip to content

Commit

Permalink
Add 'not widely supported' SGR to support rate.cx (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
yne authored and devnoname120 committed Dec 12, 2017
1 parent 723ca18 commit 65c9680
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 23 deletions.
10 changes: 7 additions & 3 deletions common/debugScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ static size_t psvDebugScreenEscape(const unsigned char *str) {
if(!arg[0]) {arg[0] = 39;arg[1] = 49;argc = 1;}//no/0 args == reset BG + FG
for(unsigned c = 0; c <= argc; c++) {
uint32_t unit = arg[c] % 10, mode = arg[c] / 10, *color = mode&1 ? &colorFg : &colorBg;
if (arg[c]==1)colorFg|=0x808080;
if (arg[c]==2)colorFg&=0x7F7F7F;
if (mode!=3 && mode!=4 && mode!=9 && mode!=10)continue;//skip unsported modes
if (unit == 9){ // reset FG or BG
*color = mode&1 ? defaultFg : defaultBg;
} else if ((unit==8) && (arg[c+1]==5)) { // 8bit : [0-15][16-231][232-256] color map
Expand All @@ -87,16 +90,17 @@ static size_t psvDebugScreenEscape(const unsigned char *str) {
}
return 0;
}
/* avoid linking non-initializer (prx) with sceDisplay/sceMemory */
#ifndef NO_psvDebugScreenInit
int psvDebugScreenInit() {
#ifdef NO_psvDebugScreenInit
return 0;/* avoid linking non-initializer (prx) with sceDisplay/sceMemory */
#else
mutex = sceKernelCreateMutex("log_mutex", 0, 0, NULL);
SceUID displayblock = sceKernelAllocMemBlock("display", SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW, SCREEN_FB_SIZE, NULL);
sceKernelGetMemBlockBase(displayblock, (void**)&base);
SceDisplayFrameBuf frame = { sizeof(frame), base, SCREEN_FB_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
return sceDisplaySetFrameBuf(&frame, SCE_DISPLAY_SETBUF_NEXTFRAME);
}
#endif
}

int psvDebugScreenPuts(const char * _text) {
const unsigned char*text = (const unsigned char*)_text;
Expand Down
89 changes: 69 additions & 20 deletions net_http_bsd/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
#include <psp2/display.h>
#include <psp2/ctrl.h>
#include <psp2/net/net.h>
#include "debugScreen.h"
#define printf psvDebugScreenPrintf
#else
#define NO_psvDebugScreenInit
#endif

#include "debugScreen.h"

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
Expand All @@ -29,14 +33,16 @@ unsigned char readUTF(unsigned char c, int fd){
case 8213:return 0x17;
case 8216:return 0x60;
case 8217:return 0x27;
case 8218:return ',';
case 8230:return 0x2E;
case 8592:return '<';
case 8593:return '^';
case 8593:return 0xCE;
case 8594:return '>';
case 8595:return 'v';
case 8595:return 0xCD;
case 8598:return '\\';
case 8599:return '/';
case 8601:return '\\';
case 8600:return '\\';
case 8602:return '/';
case 9472:return 0x17;
case 9474:return 0x16;
Expand All @@ -49,38 +55,81 @@ unsigned char readUTF(unsigned char c, int fd){
case 9516:return 0x12;
case 9524:return 0x11;
case 9532:return 0x15;
case 9600:return 0xDF;
case 9604:return 0xDC;
case 9600:return 0xC0;
case 9601:return 0xC0;
case 9602:return 0xDC;
case 9603:return 0xDC;
case 9604:return 0xC2;
case 9605:return 0xC2;
case 9606:return 0xDB;
case 9607:return 0xDB;
case 9608:return 0xDB;
}
return '?';
}

int main (int argc, char *argv[]){
static char net_mem[1*1024*1024];
int main (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[]){
char* wttr[] = {"wttr.in","/London"};
char* rate[] = {"rate.sx","/"};
char**url = NULL;// will point to wttr or rate

#ifdef __vita__
psvDebugScreenInit();
psvDebugScreenFont.size_w-=1;//narrow character printing
printf("Press [L]=%s%s [R]=%s%s\n", wttr[0], wttr[1], rate[0], rate[1]);
#ifdef __vita__
static char net_mem[1*1024*1024];
sceSysmoduleLoadModule(SCE_SYSMODULE_NET);
sceNetInit(&(SceNetInitParam){net_mem, sizeof(net_mem)});
#endif

char* host = "wttr.in", *path="/Paris";
int pos=0, fd = socket(PF_INET, SOCK_STREAM, 0);
connect(fd, (const struct sockaddr *)&((struct sockaddr_in){.sin_family = AF_INET, .sin_port = htons(80), .sin_addr.s_addr = *(long*)(gethostbyname(host)->h_addr)}), sizeof(struct sockaddr_in));
#ifdef __vita__
for(SceCtrlData ctrl={}; !url; sceCtrlReadBufferPositive(0,&ctrl,1)){
if(ctrl.buttons == SCE_CTRL_LTRIGGER)
url=wttr;
if(ctrl.buttons == SCE_CTRL_RTRIGGER)
url=rate;
}
#else
while(!url){
int c = getchar();
if(c == 'L' || c == 'l')
url = wttr;
if(c == 'R' || c == 'r')
url = rate;
}
#endif

printf("fetching %s%s...\n", url[0], url[1]);
int fd = socket(PF_INET, SOCK_STREAM, 0);
connect(fd, (const struct sockaddr *)&((struct sockaddr_in){.sin_family = AF_INET, .sin_port = htons(80), .sin_addr.s_addr = *(long*)(gethostbyname(url[0])->h_addr)}), sizeof(struct sockaddr_in));

char*header[] = {"GET ",path," HTTP/1.1\r\n", "User-Agent: curl/7.52.1\r\n", "Host: ",host,"\r\n", "\r\n", 0};
for(int i=0;header[i];i++)write(fd, header[i], strlen(header[i]));
char*header[] = {"GET ",url[1]," HTTP/1.1\r\n", "User-Agent: curl/PSVita\r\n", "Host: ",url[0],"\r\n", "\r\n", 0};
for(int i = 0; header[i]; i++)//send all request header to the server
write(fd, header[i], strlen(header[i]));

for(unsigned char c, line[4096];read(fd,&c,sizeof(c))>0 && pos<sizeof(line);pos = (c=='\n')? 0 : (pos + 1)){
#ifdef __vita__
if (c>>6==3)c = readUTF(c,fd);
#endif
if (c == '\n') printf("%.*s\n",pos,line);
else line[pos] = c;
unsigned pos = 0;
unsigned char c, line[4096];
while(read(fd,&c,sizeof(c)) > 0 && pos < sizeof(line)) {
if (c>>6==3) // if fetched char is a UTF8
c = readUTF(c,fd); // convert it back to ASCII
if (c == '\n') { // end of line reached
psvDebugScreenPrintf("%.*s\n", pos, line); // printf the received line into the screen
pos = 0; // reset the buffer pointer back to 0
} else {
line[pos] = c;
pos++;
}
}

close(fd);

#ifndef __vita__ // generate a RGB screen dump (if built on PC)
//convert the dump into PNG with: convert -depth 8 -size 960x544+0 RGB:screen.data out.png;
int fdump = open("screen.data", O_WRONLY | O_CREAT | O_TRUNC, 0777);
for (unsigned i = 0; i < sizeof(base); i += 4)// for every RGBA bytes pointed by the screen "base" adress
write(fdump, base+i, 3);//write the RGB part (3 bytes) into "screen.data"
close(fdump);
#endif

#ifdef __vita__
sceNetTerm();
sceSysmoduleUnloadModule(SCE_SYSMODULE_NET);
Expand Down

11 comments on commit 65c9680

@CelesteBlue-dev
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add this sample in the Sample's README in the Samples list.

@yne
Copy link
Contributor Author

@yne yne commented on 65c9680 Jan 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not simply remove that nobody-ever-updated-list ?

@CelesteBlue-dev
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yne because it explains what each sample do.
For exemple, what's the difference between http-net and http-net-bsd has not be explained yet.

@yne
Copy link
Contributor Author

@yne yne commented on 65c9680 Jan 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to create a README.md in each sample folder.
This way we could have more organised/detailed informations (and screenshot !)
Also, clicking on a folder from github will display the sample README (which is good)
Finally, I could also create a jekyll file, so the sample repo could be browsed from vitasdk.github.io/samples

What do you think ?

@devnoname120
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather have an overview at the root so complexity O(1) rather that needing to open each sample folder to figure out what it does which is O(n).

@CelesteBlue-dev
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think same as @devnoname120 .
By the way noname120, where did you learned to talk about complexity this way ?

@yne
Copy link
Contributor Author

@yne yne commented on 65c9680 Jan 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@devnoname120 Need an overview of every sample ? here you go :

for md in */README.md ; do cat $md; done

BTW current one-liner sample description are laughable:

"A minimal $folder sample."

So yeah, a dedicated README.md would be more useful IMO.

@devnoname120
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Github doesn't have a shell interpreter.

@yne
Copy link
Contributor Author

@yne yne commented on 65c9680 Jan 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay so how about this one:
Mixing every sample description in one README also increase complexity because if you are looking for a particular one (which is the most common scenario) you need to filter-out the other samples description.

And for elder people, filtering information is very painful so I estimate the complexity to log2(n) :)

@cmsj
Copy link
Contributor

@cmsj cmsj commented on 65c9680 Jan 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about #42 :)

@devnoname120
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing the issue instead of discussing about it for hours, thanks! 👍

Please sign in to comment.