Uriel Web Browser for TempleOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

180 lines
5.2 KiB

#define URIEL_BINARY_EOF "*[U_EOF]"
#define URIEL_BINARY_SOF "*[U_SOF]"
#define URIEL_FCMD_GET "*[U_GET]"
#define URIEL_FCMD_PUT "*[U_PUT]"
#define URIEL_TMP_IMG "/Tmp/uriel_img.bmp"
#define URIEL_PORT 2
#define URIEL_RAW_PORT 0x02F8
#define URIEL_TIMEOUT 1
#include "::/Doc/Comm"
U8 UrielPageBuf[2097152];
U8 UrielFileBuf[10485760];
I64 UrielFileBufSize=0;
I64 UrielPageBufSize=0;
I64 UrielGetFileSuccess=0;
CComm *c=CommInit8n1(URIEL_PORT,115200);
U0 Get(U8 *remote_file, U8 *local_file="file") {
UrielGetFileSuccess=0;
Bool load=FALSE;
I64 b=0;
I64 PrevFileBufSize=0;
I64 StartTimer=0;
I64 TickTimer=0;
UrielFileBufSize=0;
CommPutS(URIEL_PORT, URIEL_FCMD_GET);
CommPutS(URIEL_PORT, remote_file);
CommPutS(URIEL_PORT, "|");
CommPutS(URIEL_PORT, Define("DD_OS_NAME_VERSION"));
CommPutS(URIEL_PORT, "^");
progress1_max=100;
progress1=0;
StrCpy(progress1_desc, "Receiving file");
while (!load) {
if ((TickTimer-StartTimer)>URIEL_TIMEOUT) { UrielFileBufSize=0; break; };
if (InU8(0x60)==0x01) { UrielFileBufSize=0; break; };
PrevFileBufSize=UrielFileBufSize;
Sleep(0);
while (FifoU8Rem(c->RX_fifo,&b)) {
if (b != 0x00) {
UrielFileBuf[UrielFileBufSize] = b;
UrielFileBufSize++;
progress1++;
if (progress1>progress1_max) { progress1=0; };
}
else {
if (StrCmp(UrielFileBuf+(UrielFileBufSize-8), URIEL_BINARY_EOF)==0) {
UrielFileBufSize -= 8;
load=TRUE;
UrielGetFileSuccess=1;
break;
}
else {
UrielFileBuf[UrielFileBufSize] = b;
UrielFileBufSize++;
progress1++;
if (progress1>progress1_max) { progress1=0; };
}
}
}
if (PrevFileBufSize==UrielFileBufSize) {
if (StartTimer==0) { StartTimer=SysTimerRead/1000000; };
TickTimer=SysTimerRead/1000000;
}
}
FileWrite(local_file, UrielFileBuf, UrielFileBufSize);
ProgressBarsRst;
}
U0 Send(U8 *local_file, U8 *remote_file="") {
CDirEntry *d_file = FilesFind(local_file);
U8 *s_file = FileRead(d_file->full_name, d_file->size);
I64 f_pos=0;
CommPutS(URIEL_PORT, URIEL_FCMD_PUT);
CommPutS(URIEL_PORT, local_file);
CommPutS(URIEL_PORT, "|");
CommPutS(URIEL_PORT, remote_file);
CommPutS(URIEL_PORT, URIEL_BINARY_SOF);
progress1_max=d_file->size;
progress1=0;
StrCpy(progress1_desc, "Sending file");
for (f_pos=0;f_pos<d_file->size;f_pos++) {
if (InU8(0x60)==0x01) { UrielFileBufSize=0; break; };
Sleep(0);
progress1=f_pos;
OutU8(URIEL_RAW_PORT, s_file[f_pos]);
}
CommPutS(URIEL_PORT, URIEL_BINARY_EOF);
ProgressBarsRst;
Free(s_file);
Free(d_file);
}
U0 InsertImg(U8 *img_anchor, U8 *img_sid, U8 *img_eid, U8 *img_url) {
UrielGetFileSuccess=0;
DocAnchorFind(DocPut, img_anchor);
Get(img_url, URIEL_TMP_IMG);
while (UrielGetFileSuccess==0) {
DocAnchorFind(DocPut, img_anchor);
Get("retry:send", URIEL_TMP_IMG);
}
DocAnchorFind(DocPut, img_anchor);
DocBMP(,URIEL_TMP_IMG);
if (StrCmp(img_sid, "")!=0) {
DocAnchorFind(DocPut, img_sid);
DocPrintPartial(DocPut, "$ID,14$");
}
if (StrCmp(img_eid, "")!=0) {
DocAnchorFind(DocPut, img_eid);
DocPrintPartial(DocPut, "$ID,-14$");
}
DocAnchorFind(DocPut, img_anchor);
UrielGetFileSuccess=0;
}
U0 CloseBrowser() {
Bool close=FALSE;
close = PopUpCancelOk("Close Browser?");
if (close) { In("x\n"); };
}
U0 Browser(U8 *url)
{
WinMax;
U8 *Cmd;
UrielPageBufSize=0;
I64 UrielPageBufPos=0;
CommPutS(URIEL_PORT, url);
CommPutS(URIEL_PORT, "|");
CommPutS(URIEL_PORT, Define("DD_OS_NAME_VERSION"));
CommPutS(URIEL_PORT, "^");
CDoc *UrielPage = DocNew();
I64 b=0;
Bool load=FALSE;
Bool proc=FALSE;
Bool pad=TRUE;
progress1_max=100;
progress1=0;
StrCpy(progress1_desc, "Request sent, waiting for response");
while (!load) {
if (InU8(0x60)==0x01) { UrielPageBufSize=0; break; };
Sleep(0);
while (FifoU8Rem(c->RX_fifo,&b)) {
if (b == 0xFF && !pad) { load=TRUE; break; };
if (b != 0xFF && pad) { pad=FALSE; };
if (b != 0xFF && !pad) {
if (!proc) { proc=TRUE; StrCpy(progress1_desc, "Processing DolDoc"); };
UrielPageBuf[UrielPageBufSize] = b;
UrielPageBufSize++;
progress1++;
if (progress1>progress1_max) { progress1=0; };
}
}
}
while (UrielPageBufPos<UrielPageBufSize) {
DocPrintPartial(UrielPage, "%c", UrielPageBuf[UrielPageBufPos]);
UrielPageBufPos++;
}
ProgressBarsRst;
DocClear;
DocInsDoc(DocPut, UrielPage);
DocTop;
DocDel(UrielPage);
while (TRUE) {
Cmd = GetStr;
if(StrCmp(Cmd, "x")==0) { break; };
}
Free(Cmd);
DocBottom;
}
U0 Navigate(U8 *anchor, U8 *url) {
DocAnchorFind(DocPut, anchor);
Browser(url);
}