/* Visible target collection. Saves raw pairs; never changes kernel calibration. */ #define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "n810-touch-confirm.h" static volatile sig_atomic_t stopped; static void stop(int sig) { (void)sig; stopped=1; } static long elapsed(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC,&t); return t.tv_sec; } static int input_open(void) { for (int i=0;i<32;i++) { char path[64], name[128]={0}; snprintf(path,sizeof(path),"/dev/input/event%d",i); int fd=open(path,O_RDONLY|O_CLOEXEC|O_NONBLOCK); if (fd<0) continue; if (ioctl(fd,EVIOCGNAME(sizeof(name)),name)>=0 && !strcmp(name,"TSC2005 touchscreen")) return fd; close(fd); } errno=ENODEV; return -1; } static int refresh(int fd) { struct omapfb_update_window w={.width=800,.height=480,.out_width=800,.out_height=480}; return ioctl(fd,OMAPFB_UPDATE_WINDOW,&w); } static void pixel(unsigned char *mem,const struct fb_fix_screeninfo *f, const struct fb_var_screeninfo *v,int x,int y,uint16_t value) { if(x>=0 && x<800 && y>=0 && y<480) memcpy(mem+(size_t)(y+v->yoffset)*f->line_length+(size_t)(x+v->xoffset)*2,&value,2); } /* The shipped console font supplies the same legible glyphs as Pocket. */ static unsigned char font[256][64]; static unsigned font_width, font_height, font_stride; static int load_font(const char *path) { FILE *file=fopen(path,"rb"); unsigned char header[32]; if(!file) return -1; if(fread(header,1,32,file)!=32) {fclose(file);return -1;} unsigned offset, size; if(header[0]==0x36 && header[1]==0x04) { offset=4; font_width=8; font_height=header[3]; size=font_height; } else { uint32_t h[8]; memcpy(h,header,32); if(h[0]!=0x864ab572 || h[4]<256) {fclose(file);return -1;} offset=h[2]; size=h[5]; font_height=h[6]; font_width=h[7]; } font_stride=(font_width+7)/8; if(!font_width || font_width>16 || !font_height || font_height>32 || size>64 || size4096) {fclose(file);return -1;} if(fseek(file,offset,SEEK_SET)) {fclose(file);return -1;} for(int i=0;i<256;i++) if(fread(font[i],1,size,file)!=size) {fclose(file);return -1;} fclose(file); return 0; } static void text(unsigned char *mem,const struct fb_fix_screeninfo *f, const struct fb_var_screeninfo *v,int x,int y,const char *label,uint16_t color) { for(const unsigned char *c=(const unsigned char *)label;*c;c++,x+=(int)font_width*2) for(unsigned row=0;row>(col%8))) for(int dy=0;dy<2;dy++) for(int dx=0;dx<2;dx++) pixel(mem,f,v,x+col*2+dx,y+row*2+dy,color); } static int target_screen(int fb,unsigned char *mem,const struct fb_fix_screeninfo *f, const struct fb_var_screeninfo *v,int tx,int ty,int index,int pending) { for(int y=0;y<480;y++) for(int x=0;x<800;x++) pixel(mem,f,v,x,y,0x0841); uint16_t color=pending>0?0xffe0:0xffff; for(int k=-18;k<=18;k++) for(int d=-1;d<=1;d++) { pixel(mem,f,v,tx+k,ty+d,color); pixel(mem,f,v,tx+d,ty+k,color); } char heading[40];snprintf(heading,sizeof(heading),"TOUCH SETUP - POINT %d OF 5",index+1); text(mem,f,v,40,140,heading,0xffff); text(mem,f,v,40,185,pending<0?"TAPS DID NOT MATCH. TRY AGAIN.":pending?"TAP THE SAME CROSS AGAIN":"TAP THE WHITE CROSS",color); text(mem,f,v,40,225,pending>0?"TO CONFIRM. LIFT BETWEEN TAPS.":"TAP THE CROSS TWICE, LIFTING EACH TIME.",0xffff); text(mem,f,v,40,285,"MISSED? TAP THE CROSS TWICE.",0xbdf7); text(mem,f,v,40,325,"WAIT FOR THE CROSS TO MOVE.",0xbdf7); return refresh(fb); } static int pointer_test(int fb,int in,unsigned char *mem, const struct fb_fix_screeninfo *f,const struct fb_var_screeninfo *v, const double c[6]) { for(int y=0;y<480;y++) for(int x=0;x<800;x++) pixel(mem,f,v,x,y,0x0841); const int targets[5][2]={{80,48},{720,48},{720,432},{80,432},{400,240}}; for(int i=0;i<5;i++) for(int k=-12;k<=12;k++) { pixel(mem,f,v,targets[i][0]+k,targets[i][1],0x7bef); pixel(mem,f,v,targets[i][0],targets[i][1]+k,0x7bef); } unsigned char *background=malloc(f->smem_len); if(!background) return 1; memcpy(background,mem,f->smem_len); if(refresh(fb)<0) {free(background);return 1;} int x=-1,y=-1,down=0,dirty=0,frames=0,oldx=-1,oldy=-1; long deadline=elapsed()+120; struct timespec last={0}; while(!stopped && elapsed()=0 && y>=0 && delta>=0.033) { double px=c[0]*x+c[1]*y+c[2],py=c[3]*x+c[4]*y+c[5]; int tx=(int)(fmax(0,fmin(799,px))+0.5),ty=(int)(fmax(0,fmin(479,py))+0.5); if(oldx>=0) { for(int row=oldy-9;row<=oldy+9;row++) { if(row<0 || row>=480) continue; int left=oldx-9<0?0:oldx-9,right=oldx+10>800?800:oldx+10; size_t offset=(size_t)(row+v->yoffset)*f->line_length+(size_t)(left+v->xoffset)*2; memcpy(mem+offset,background+offset,(size_t)(right-left)*2); } } for(int k=-9;k<=9;k++) { pixel(mem,f,v,tx+k,ty,down?0x07ff:0xffff); pixel(mem,f,v,tx,ty+k,down?0x07ff:0xffff); } if(refresh(fb)<0) {free(background);return 1;} oldx=tx;oldy=ty; last=now;dirty=0;frames++; } } fprintf(stderr,"Pointer test finished: %d rendered frames\n",frames); free(background);return stopped?1:0; } int main(int argc,char **argv) { int fb=-1,in=-1,tty=-1,oldmode=KD_TEXT,result=1,graphics=0; unsigned char *mem=MAP_FAILED,*saved=NULL; struct fb_fix_screeninfo f; struct fb_var_screeninfo v; FILE *out=NULL; int shared=argc==3 && !strcmp(argv[1],"--shared-framebuffer"); int pointer=argc==3 && !strcmp(argv[1],"--pointer"); double coefficients[6]={0}; const int targets[5][2]={{80,48},{720,48},{720,432},{80,432},{400,240}}; if(argc!=2 && !pointer && !shared) { fprintf(stderr,"Usage: n810-touch-calibrate OUTPUT.csv (run on a temporary VT)\n"); return 2; } if(pointer) { FILE *config=fopen(argv[2],"r"); if(!config) {perror("mapping");return 1;} int n=fscanf(config,"%lf %lf %lf %lf %lf %lf",&coefficients[0],&coefficients[1],&coefficients[2],&coefficients[3],&coefficients[4],&coefficients[5]); int extra;do {extra=fgetc(config);} while(extra==' '||extra=='\n'||extra=='\t'||extra=='\r'); fclose(config); if(n!=6 || extra!=EOF) {fprintf(stderr,"Mapping requires six coefficients\n");return 1;} for(int i=0;i<6;i++) if(!isfinite(coefficients[i]) || fabs(coefficients[i])>10000) return 1; } fb=open("/dev/fb0",O_RDWR|O_CLOEXEC);in=input_open();if(!shared) tty=open("/dev/tty",O_RDWR|O_CLOEXEC); if(fb<0 || in<0 || (!shared && tty<0) || ioctl(fb,FBIOGET_FSCREENINFO,&f)<0 || ioctl(fb,FBIOGET_VSCREENINFO,&v)<0 || (!shared && ioctl(tty,KDGETMODE,&oldmode)<0)) { perror("device");goto done; } if(v.xres!=800 || v.yres!=480 || v.bits_per_pixel!=16 || f.visual!=FB_VISUAL_TRUECOLOR || v.red.offset!=11 || v.red.length!=5 || v.green.offset!=5 || v.green.length!=6 || v.blue.offset!=0 || v.blue.length!=5 || (uint64_t)(v.xoffset+800)*2>f.line_length || (uint64_t)(v.yoffset+480)*f.line_length>f.smem_len) { fprintf(stderr,"Requires the N810 800x480 RGB565 framebuffer\n");goto done; } saved=malloc(f.smem_len); if(!saved) goto done; mem=mmap(NULL,f.smem_len,PROT_READ|PROT_WRITE,MAP_SHARED,fb,0); if(mem==MAP_FAILED) {perror("mmap");goto done;} memcpy(saved,mem,f.smem_len); if(!pointer) {out=fopen(argv[shared?2:1],"wx"); if(!out) {perror("output (choose a new file)");goto done;}} signal(SIGINT,stop);signal(SIGTERM,stop);signal(SIGHUP,stop); if(!shared && ioctl(tty,KDSETMODE,KD_GRAPHICS)<0) {perror("graphics");goto done;} graphics=1; if(pointer) {result=pointer_test(fb,in,mem,&f,&v,coefficients);goto done;} fprintf(out,"screen_x,screen_y,raw_x,raw_y,samples\n");fflush(out); const char *font_path=getenv("N810_CALIBRATION_FONT"); if(load_font(font_path?font_path:"/usr/local/share/pocket/build/graphics/console-font.psf")<0) { fprintf(stderr,"Cannot load calibration instruction font\n");goto done; } struct input_absinfo ax,ay; if(ioctl(in,EVIOCGABS(ABS_X),&ax)<0 || ioctl(in,EVIOCGABS(ABS_Y),&ay)<0) goto done; double tolerance_x=fmax(8,(ax.maximum-ax.minimum)*0.03); double tolerance_y=fmax(8,(ay.maximum-ay.minimum)*0.03); long deadline=elapsed()+300; for(int target=0;target<5 && !stopped;target++) { int tx=targets[target][0],ty=targets[target][1]; struct touch_confirmation confirmation={0}; int confirmed=0, retry=0; while(!confirmed && !stopped && elapsed()=0 && y>=0) {sx+=x;sy+=y;n++;} } if(!released) {fprintf(stderr,"Cancelled or timed out; calibration incomplete\n");goto done;} int confirmation_result=confirm_touch(&confirmation,(double)sx/n,(double)sy/n,tolerance_x,tolerance_y); confirmed=confirmation_result==1; retry=confirmation_result<0; if(confirmed) { fprintf(out,"%d,%d,%.3f,%.3f,%d\n",tx,ty,confirmation.x,confirmation.y,n);fflush(out); } } if(!confirmed) goto done; } if(!stopped) {result=0;fprintf(stderr,"Five targets collected. Calibration is not yet applied.\n");} done: if(graphics) {memcpy(mem,saved,f.smem_len);refresh(fb);if(!shared) ioctl(tty,KDSETMODE,oldmode);} if(out) fclose(out); if(mem!=MAP_FAILED) munmap(mem,f.smem_len); free(saved);if(tty>=0)close(tty);if(in>=0)close(in);if(fb>=0)close(fb); return result; }