25 lines
5.6 KiB
C
25 lines
5.6 KiB
C
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
||
/* Portable protocol reference with native X11 drawing and optional aplay audio. */
|
||
#include "../common/rbp.h"
|
||
#include <X11/Xlib.h>
|
||
#include <X11/Xutil.h>
|
||
#include <X11/keysym.h>
|
||
#include <sys/socket.h>
|
||
#include <arpa/inet.h>
|
||
#include <unistd.h>
|
||
#include <poll.h>
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include <string.h>
|
||
#include <signal.h>
|
||
#include <fcntl.h>
|
||
#include <errno.h>
|
||
#include <sys/wait.h>
|
||
static int rd(void *c,void *b,unsigned int n){return (int)recv(*(int*)c,b,n,0);}static int wr(void *c,const void *b,unsigned int n){return (int)send(*(int*)c,b,n,0);}
|
||
static unsigned long color(unsigned v,unsigned long mask){unsigned shift=0;unsigned long m=mask;if(!m)return 0;while(!(m&1)){shift++;m>>=1;}return ((v*m/255)<<shift)&mask;}
|
||
static void draw(Display*d,Window win,GC gc,rb_u8*p,rb_u32 n){unsigned w,h,x,y;rb_u16 *pix;XImage*im;Visual*v=DefaultVisual(d,DefaultScreen(d));if(n<6)return;w=rb_get16(p);h=rb_get16(p+2);if(w<1||h<1||w>1920||h>1080||(p[4]!=2&&p[4]!=3))return;pix=malloc((size_t)w*h*2);if(!pix)return;if(rb_rgb565(p+6,n-6,pix,(rb_u32)w*h,p[4]==3)){free(pix);return;}im=XCreateImage(d,v,DefaultDepth(d,DefaultScreen(d)),ZPixmap,0,NULL,w,h,32,0);if(!im){free(pix);return;}im->data=calloc(h,im->bytes_per_line);if(!im->data){XDestroyImage(im);free(pix);return;}for(y=0;y<h;y++)for(x=0;x<w;x++){unsigned z=pix[y*w+x];XPutPixel(im,x,y,color(((z>>11)&31)*255/31,v->red_mask)|color(((z>>5)&63)*255/63,v->green_mask)|color((z&31)*255/31,v->blue_mask));}XPutImage(d,win,gc,im,0,0,0,0,w,h);XFlush(d);XDestroyImage(im);free(pix);}
|
||
static int audiofd=-1;static pid_t audiopid=-1;static unsigned rate0,ch0;
|
||
static void stop_audio(void){if(audiofd>=0)close(audiofd);audiofd=-1;if(audiopid>0){kill(audiopid,SIGTERM);waitpid(audiopid,NULL,0);}audiopid=-1;}
|
||
static void audio(rb_u8*p,rb_u32 n){short pcm[4410];int k;unsigned rate,ch;int f[2];char r[16],c[8];if(n<8)return;rate=rb_get32(p);ch=p[4];if((rate!=11025&&rate!=22050&&rate!=44100)||(ch!=1&&ch!=2))return;k=rb_audio(p,n,pcm,4410);if(k<=0)return;if(audiofd<0||rate!=rate0||ch!=ch0){stop_audio();if(pipe(f))return;snprintf(r,sizeof r,"%u",rate);snprintf(c,sizeof c,"%u",ch);audiopid=fork();if(!audiopid){dup2(f[0],0);close(f[0]);close(f[1]);execlp("aplay","aplay","-q","-t","raw","-f","S16_LE","-r",r,"-c",c,(char*)0);_exit(127);}close(f[0]);if(audiopid<0){close(f[1]);return;}audiofd=f[1];fcntl(audiofd,F_SETFL,O_NONBLOCK);rate0=rate;ch0=ch;}if(write(audiofd,pcm,k*2)<0&&errno==EPIPE)stop_audio();}
|
||
int main(int argc,char**argv){int fd,ready=0,run=1;struct sockaddr_in addr;rb_io io;Display*d;Window win;GC gc;Atom wm;struct pollfd f[2];rb_header h;rb_u8 *p;const char*token=getenv("RBP_TOKEN");if(argc!=4||!token||strlen(token)!=64){fprintf(stderr,"RBP_TOKEN=... %s IPv4 port URL\n",argv[0]);return 1;}signal(SIGPIPE,SIG_IGN);fd=socket(AF_INET,SOCK_STREAM,0);memset(&addr,0,sizeof addr);addr.sin_family=AF_INET;addr.sin_port=htons(atoi(argv[2]));if(fd<0||inet_pton(AF_INET,argv[1],&addr.sin_addr)!=1||connect(fd,(void*)&addr,sizeof addr)){perror("connect");return 1;}fcntl(fd,F_SETFD,FD_CLOEXEC);io.ctx=&fd;io.read=rd;io.write=wr;d=XOpenDisplay(NULL);if(!d)return 1;win=XCreateSimpleWindow(d,DefaultRootWindow(d),0,0,800,600,0,0,0xffffff);gc=XCreateGC(d,win,0,NULL);XStoreName(d,win,"MARS RetroBrowser – F5 reload / Alt+Left back");XSelectInput(d,win,ExposureMask|KeyPressMask|KeyReleaseMask|ButtonPressMask|ButtonReleaseMask|PointerMotionMask);wm=XInternAtom(d,"WM_DELETE_WINDOW",False);XSetWMProtocols(d,win,&wm,1);XMapWindow(d,win);rb_send(&io,RB_AUTH,token,64);f[0].fd=fd;f[0].events=POLLIN;f[1].fd=ConnectionNumber(d);f[1].events=POLLIN;while(run){int pr=poll(f,2,5000);if(pr<0)break;if(!pr){if(ready)rb_send(&io,RB_PING,NULL,0);continue;}if(f[0].revents&(POLLERR|POLLHUP|POLLNVAL))break;if(f[0].revents&POLLIN){if(rb_header_read(&io,&h))break;p=malloc(h.length+1);if(!p||rb_read(&io,p,h.length)){free(p);break;}p[h.length]=0;if(h.type==RB_HELLO)rb_hello(&io,800,600,6,1);else if(h.type==RB_READY){ready=1;rb_send(&io,RB_NAV,argv[3],strlen(argv[3]));}else if(h.type==RB_FRAME)draw(d,win,gc,p,h.length);else if(h.type==RB_AUDIO)audio(p,h.length);else if(h.type==RB_ERROR)fprintf(stderr,"Server: %s\n",p);free(p);}while(XPending(d)){XEvent e;XNextEvent(d,&e);if(e.type==ClientMessage){run=0;break;}if(!ready)continue;if(e.type==MotionNotify||e.type==ButtonPress||e.type==ButtonRelease){rb_u8 b[8]={0};int x=e.type==MotionNotify?e.xmotion.x:e.xbutton.x,y=e.type==MotionNotify?e.xmotion.y:e.xbutton.y;b[0]=e.type==MotionNotify?0:e.type==ButtonPress?1:2;if(b[0]){if(e.xbutton.button==4||e.xbutton.button==5){if(e.type==ButtonRelease)continue;b[0]=3;rb_put16(b+6,(rb_u16)(e.xbutton.button==4?-100:100));}else b[1]=e.xbutton.button<=3?e.xbutton.button:0;}rb_put16(b+2,(rb_u16)(x<0?0:x));rb_put16(b+4,(rb_u16)(y<0?0:y));rb_send(&io,RB_MOUSE,b,8);}else if(e.type==KeyPress||e.type==KeyRelease){char text[64],b[65];KeySym key;int n=XLookupString(&e.xkey,text,sizeof text,&key,NULL);const char*name=NULL;if(key==XK_F5&&e.type==KeyPress){rb_send(&io,RB_RELOAD,NULL,0);continue;}if((e.xkey.state&Mod1Mask)&&key==XK_Left&&e.type==KeyPress){rb_send(&io,RB_BACK,NULL,0);continue;}switch(key){case XK_Return:name="Enter";break;case XK_BackSpace:name="Backspace";break;case XK_Tab:name="Tab";break;case XK_Escape:name="Escape";break;case XK_Left:name="ArrowLeft";break;case XK_Right:name="ArrowRight";break;case XK_Up:name="ArrowUp";break;case XK_Down:name="ArrowDown";break;}if(name){b[0]=e.type==KeyPress;strcpy(b+1,name);rb_send(&io,RB_KEY,b,strlen(name)+1);}else if(e.type==KeyPress&&n>0&&((unsigned char)text[0])>=32)rb_send(&io,RB_TEXT,text,n);}}}stop_audio();close(fd);XCloseDisplay(d);return 0;}
|