Files

70 lines
9.8 KiB
C

/* SPDX-License-Identifier: GPL-2.0-or-later */
/* AmigaOS 2.04+ / OCS / ECS, IPv4 bsdsocket.library V4. */
#include <exec/types.h>
#include <exec/libraries.h>
#include <intuition/intuition.h>
#include <intuition/screens.h>
#include <graphics/gfx.h>
#include <utility/tagitem.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/dos.h>
#include <proto/bsdsocket.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "planar.h"
#include "../../common/settings.h"
static rb_settings cfg,draft;
static int reopen,field,dropdown,dialog_result;
static const char *profiles[]={"A500 Auto","A500 RLE","A500 RAW"};
static const char *cfgpath="PROGDIR:RetroBrowser.cfg";
struct Library *SocketBase;
struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
#define MAX_FRAME 64006UL
static struct Screen *scr;
static struct Window *win;
static LONG fd=-1;
static rb_io io;
static rb_u8 *payload,*planes,lut[256],header[16];
static rb_u16 *pixels;
static rb_u32 used,need;
static rb_u16 kind;
static int body,ready,editing,running=1,haveframe;
static char url[512]="https://example.com";
static int netread(void *c,void *p,unsigned int n){(void)c;return (int)recv(fd,p,n,0);}
static int netwrite(void *c,const void *p,unsigned int n){(void)c;return (int)send(fd,(void*)p,n,0);}
static void tx(rb_u16 type,const void *p,rb_u32 n){if(rb_send(&io,type,p,n))running=0;}
static void banner(const char *s){struct RastPort *r=win->RPort;unsigned n=(unsigned)strlen(s);if(n>39)n=39;SetAPen(r,0);RectFill(r,0,0,319,10);SetAPen(r,4);Move(r,2,8);Text(r,(STRPTR)s,n);}
static void toolbar(void){banner("F1 URL F2 Reload [ Einstellungen ]");}
static void present(void){unsigned p,y;struct BitMap *b=scr->RastPort.BitMap;WaitBlit();for(p=0;p<4;p++)for(y=0;y<200;y++)memcpy((rb_u8*)b->Planes[p]+(rb_u32)y*b->BytesPerRow,planes+(rb_u32)p*A500_PLANE_BYTES+(rb_u32)y*A500_STRIDE,A500_STRIDE);toolbar();}
static void edit_banner(void){unsigned n=(unsigned)strlen(url);banner(url+(n>39?n-39:0));}
static void process(void){if(kind==RB_HELLO){if(rb_hello(&io,A500_W,A500_H,cfg.profile==1?4:cfg.profile==2?2:6,0))running=0;}else if(kind==RB_READY){ready=1;toolbar();tx(RB_NAV,url,strlen(url));}else if(kind==RB_FRAME){if(need<6||rb_get16(payload)!=A500_W||rb_get16(payload+2)!=A500_H||(payload[4]!=2&&payload[4]!=3))return;if(rb_rgb565(payload+6,need-6,pixels,(rb_u32)A500_W*A500_H,payload[4]==3))return;a500_planar(pixels,planes,lut);haveframe=1;present();if(editing)edit_banner();}else if(kind==RB_ERROR){banner("Serverfehler - Einstellungen");reopen=1;}}
static int pump(void){unsigned i;for(i=0;i<8;i++){fd_set rf;struct timeval tv;rb_u32 total;rb_u8 *dst;unsigned ask;int n;FD_ZERO(&rf);FD_SET(fd,&rf);tv.tv_sec=0;tv.tv_usec=0;n=WaitSelect(fd+1,&rf,NULL,NULL,&tv,NULL);if(n<0)return -1;if(!n)return 0;total=body?need:16;dst=body?payload:header;ask=(unsigned)((total-used)>4096?4096:total-used);n=netread(NULL,dst+used,ask);if(n<=0)return -1;used+=(rb_u32)n;if(used==total){used=0;if(!body){if(memcmp(header,"RBP1",4)||rb_get16(header+4)!=1)return -1;kind=rb_get16(header+6);need=rb_get32(header+8);if(need>MAX_FRAME)return -1;if(!need){process();continue;}body=1;}else{process();body=0;}}}return 0;}
static void special(const char *s){rb_u8 b[32];unsigned n=(unsigned)strlen(s);if(!ready||n>=sizeof b)return;b[0]=1;memcpy(b+1,s,n);tx(RB_KEY,b,n+1);b[0]=0;tx(RB_KEY,b,n+1);}
static void rawkey(unsigned code){if(code&128)return;switch(code){case 0x50:editing=!editing;if(editing)edit_banner();else if(haveframe)present();break;case 0x51:if(ready)tx(RB_RELOAD,NULL,0);break;case 0x52:if(ready)tx(RB_BACK,NULL,0);break;case 0x53:if(ready)tx(RB_FORWARD,NULL,0);break;case 0x55:reopen=2;break;case 0x59:running=0;break;case 0x4c:if(!editing)special("ArrowUp");break;case 0x4d:if(!editing)special("ArrowDown");break;case 0x4e:if(!editing)special("ArrowRight");break;case 0x4f:if(!editing)special("ArrowLeft");break;}}
static void textkey(unsigned c){unsigned n=(unsigned)strlen(url);if(editing){if(c==13||c==10){editing=0;if(haveframe)present();if(ready)tx(RB_NAV,url,n);}else if(c==27){editing=0;if(haveframe)present();}else if(c==8&&n){url[n-1]=0;edit_banner();}else if(c==127){url[0]=0;edit_banner();}else if(c>=32&&c<127&&n<sizeof url-1){url[n]=(char)c;url[n+1]=0;edit_banner();}return;}if(c==13)special("Enter");else if(c==8)special("Backspace");else if(c==9)special("Tab");else if(c==27)special("Escape");else if(c==127)special("Delete");else if(ready&&c>=32&&c<127){rb_u8 ch=(rb_u8)c;tx(RB_TEXT,&ch,1);} }
static void mouse(unsigned code,int x,int y){rb_u8 b[8]={0};if(y<12){if(code==SELECTDOWN&&x>=135)reopen=2;return;}if(!ready||editing||x<0||x>=320||y<0||y>=200)return;if(code==SELECTDOWN||code==SELECTUP){b[0]=(code==SELECTDOWN)?1:2;b[1]=1;}else if(code==MENUDOWN||code==MENUUP){b[0]=(code==MENUDOWN)?1:2;b[1]=3;}else if(code!=0)return;rb_put16(b+2,(rb_u16)(x/2));rb_put16(b+4,(rb_u16)(y/2));tx(RB_MOUSE,b,8);}
/* VANILLAKEY handles printable keys; function/navigation keys arrive as RAWKEY. */
static void events(void){struct IntuiMessage *m;while((m=(struct IntuiMessage*)GetMsg(win->UserPort))){ULONG cls=m->Class;unsigned code=m->Code;int x=m->MouseX,y=m->MouseY;ReplyMsg((struct Message*)m);if(cls==IDCMP_VANILLAKEY)textkey(code);else if(cls==IDCMP_RAWKEY)rawkey(code);else if(cls==IDCMP_MOUSEBUTTONS)mouse(code,x,y);else if(cls==IDCMP_MOUSEMOVE)mouse(0,x,y);else if(cls==IDCMP_CLOSEWINDOW)running=0;}}
static void line(int y,const char *t){SetAPen(win->RPort,4);Move(win->RPort,8,y);Text(win->RPort,(STRPTR)t,(ULONG)strlen(t));}
static void draw_dialog(const char *message){char b[40];unsigned n;struct RastPort*r=win->RPort;SetAPen(r,0);RectFill(r,0,0,319,199);line(12,"MARS RetroBrowser - Einstellungen");
n=strlen(draft.host);sprintf(b,"%c Server: %.28s",field==0?'>':' ',draft.host+(n>28?n-28:0));line(35,b);
sprintf(b,"%c Port: %s",field==1?'>':' ',draft.port);line(52,b);
sprintf(b,"%c Token: %u/64 Zeichen",field==2?'>':' ',(unsigned)strlen(draft.token));line(69,b);
sprintf(b,"%c Profil: %s [v]",field==3?'>':' ',profiles[draft.profile]);line(86,b);
if(dropdown){line(103," A500 Auto");line(116," A500 RLE");line(129," A500 RAW");}else line(109,"Token: verdeckt; Backspace loescht");
line(153,"[ Speichern & verbinden ] [ Abbruch ]");line(170,message);line(189,"Tab: Feld F10: Ende");}
static int settings_dialog(void){struct IntuiMessage*m;draft=cfg;field=0;dropdown=0;dialog_result=0;draw_dialog("");while(!dialog_result){while((m=(struct IntuiMessage*)GetMsg(win->UserPort))){ULONG cls=m->Class;unsigned c=m->Code;int x=m->MouseX,y=m->MouseY;ReplyMsg((struct Message*)m);if(cls==IDCMP_RAWKEY&&c==0x59){dialog_result=-1;break;}if(cls==IDCMP_MOUSEBUTTONS&&c==SELECTDOWN){if(dropdown&&y>=93&&y<134){draft.profile=(y-93)/13;if(draft.profile>2)draft.profile=2;dropdown=0;}else if(y>=22&&y<78){field=(y-22)/17;if(field>2)field=2;dropdown=0;}else if(y>=78&&y<93){field=3;dropdown=!dropdown;}else if(y>=138&&y<160){if(x>=200){dialog_result=-1;break;}if(inet_addr(draft.host)!=INADDR_NONE&&settings_save(cfgpath,&draft)){cfg=draft;dialog_result=1;break;}draw_dialog("IPv4 / Token / Speicher pruefen");continue;}draw_dialog("");}else if(cls==IDCMP_VANILLAKEY){if(c==9){field=(field+1)%4;dropdown=0;}else if(c==27){dialog_result=-1;break;}else if(c==13&&field==3){dropdown=!dropdown;}else if(field==3&&c>='1'&&c<='3'){draft.profile=c-'1';dropdown=0;}else settings_key(&draft,field,c);draw_dialog("");}}Delay(1);}return dialog_result>0;}
static void disconnect(void){if(fd>=0)CloseSocket(fd);fd=-1;ready=body=haveframe=editing=0;used=need=0;}
static int connect_cfg(void){struct sockaddr_in addr;memset(&addr,0,sizeof addr);addr.sin_family=AF_INET;addr.sin_port=htons((unsigned short)atoi(cfg.port));addr.sin_addr.s_addr=inet_addr(cfg.host);if(addr.sin_addr.s_addr==INADDR_NONE)return 0;fd=socket(AF_INET,SOCK_STREAM,0);if(fd<0||connect(fd,(struct sockaddr*)&addr,sizeof addr)<0){disconnect();return 0;}io.ctx=NULL;io.read=netread;io.write=netwrite;return rb_send(&io,RB_AUTH,cfg.token,64)==0;}
int main(int argc,char **argv){unsigned i;unsigned long ticks=0;int rc=1;(void)argc;(void)argv;settings_load(cfgpath,&cfg);IntuitionBase=(struct IntuitionBase*)OpenLibrary("intuition.library",36);GfxBase=(struct GfxBase*)OpenLibrary("graphics.library",36);SocketBase=OpenLibrary("bsdsocket.library",4);if(!IntuitionBase||!GfxBase||!SocketBase){fprintf(stderr,"AmigaOS 2.04+ and active TCP/IP stack required\n");goto done;}payload=malloc(MAX_FRAME);pixels=malloc((size_t)A500_W*A500_H*2);planes=malloc(4UL*A500_PLANE_BYTES);if(!payload||!pixels||!planes){fprintf(stderr,"Insufficient RAM\n");goto done;}a500_make_lut(lut);scr=OpenScreenTags(NULL,SA_Width,320,SA_Height,200,SA_Depth,4,SA_DisplayID,0,SA_Type,CUSTOMSCREEN,SA_Title,(ULONG)"MARS RetroBrowser",TAG_DONE);if(!scr)goto done;for(i=0;i<16;i++)SetRGB4(&scr->ViewPort,i,a500_palette[i]>>8,(a500_palette[i]>>4)&15,a500_palette[i]&15);ShowTitle(scr,FALSE);win=OpenWindowTags(NULL,WA_CustomScreen,(ULONG)scr,WA_Left,0,WA_Top,0,WA_Width,320,WA_Height,200,WA_Borderless,TRUE,WA_Backdrop,TRUE,WA_Activate,TRUE,WA_RMBTrap,TRUE,WA_ReportMouse,TRUE,WA_IDCMP,IDCMP_VANILLAKEY|IDCMP_RAWKEY|IDCMP_MOUSEBUTTONS|IDCMP_MOUSEMOVE,TAG_DONE);if(!win)goto done;reopen=!settings_valid(&cfg);while(running){if(reopen){disconnect();if(!settings_dialog()&&(reopen!=2||!settings_valid(&cfg)))break;reopen=0;}banner("Connecting ...");if(!connect_cfg()){reopen=1;continue;}while(running&&!reopen){events();if(!running||reopen)break;if(pump()<0){reopen=1;break;}if(++ticks>=250){ticks=0;if(ready)tx(RB_PING,NULL,0);}Delay(1);}disconnect();}rc=0;
done:if(fd>=0)CloseSocket(fd);if(win)CloseWindow(win);if(scr)CloseScreen(scr);free(planes);free(pixels);free(payload);if(SocketBase)CloseLibrary(SocketBase);if(GfxBase)CloseLibrary((struct Library*)GfxBase);if(IntuitionBase)CloseLibrary((struct Library*)IntuitionBase);return rc;}