hschumann2/TempleOS-Source-Code
0838
1 2U0 GridInit()3{//Init mouse grid struct. See ::/Demo/Graphics/Grid.HC.4 ms_grid.x=ms_grid.y=ms_grid.z=8;5 ms_grid.x_offset=ms_grid.y_offset=ms_grid.z_offset=0;6 ms_grid.x_speed =ms_grid.y_speed =ms_grid.z_speed =1;7 ms_grid.show=ms_grid.snap=ms_grid.coord=FALSE;8}9 10U0 MsUpdate(I64 x,I64 y,I64 z,Bool l,Bool r)11{12 ms.presnap.x=ToI64(ms.scale.x*x)+ms.offset.x;13 ms.presnap.y=ToI64(ms.scale.y*y)+ms.offset.y;14 ms.presnap.z=ToI64(ms.scale.z*z)+ms.offset.z;15 if (ms_grid.snap) {16 ms.pos.x=Trunc(ms.presnap.x/ms_grid.x)*ms_grid.x+ms_grid.x_offset;17 ms.pos.y=Trunc(ms.presnap.y/ms_grid.y)*ms_grid.y+ms_grid.y_offset;18 ms.pos.z=Trunc(ms.presnap.z/ms_grid.z)*ms_grid.z+ms_grid.z_offset;19 } else {20 ms.pos.x=ms.presnap.x;21 ms.pos.y=ms.presnap.y;22 ms.pos.z=ms.presnap.z;23 }24 25 ms.pos.x=ClampI64(ms.pos.x,0,GR_WIDTH-1);26 ms.pos.y=ClampI64(ms.pos.y,0,GR_HEIGHT-1);27 ms.pos_text.x=ms.pos.x/FONT_WIDTH;28 if (ms.pos_text.x>=text.cols) {29 ms.pos_text.x=text.cols-1;30 ms.pos.x=text.cols*FONT_WIDTH-1;31 }32 ms.pos_text.y=ms.pos.y/FONT_HEIGHT;33 if (ms.pos_text.y>=text.rows) {34 ms.pos_text.y=text.rows-1;35 ms.pos.y=text.rows*FONT_HEIGHT-1;36 }37 ms.lb=l;38 ms.rb=r;39 LBEqu(&kbd.scan_code,SCf_MS_L_DOWN,ms.lb);40 LBEqu(&kbd.scan_code,SCf_MS_R_DOWN,ms.rb);41}42 43U0 MsSet(I64 x=I64_MAX,I64 y=I64_MAX,I64 z=I64_MAX,I64 l=I64_MAX,I64 r=I64_MAX)44{//Note: Generates a message. See MsSet().45 if (!(0<=x<GR_WIDTH))46 x=ms.pos.x;47 if (!(0<=y<GR_HEIGHT))48 y=ms.pos.y;49 if (z==I64_MAX)50 z=ms.pos.z;51 52 if (!(FALSE<=l<=TRUE))53 l=ms.lb;54 if (!(FALSE<=r<=TRUE))55 r=ms.rb;56 57 x=(x-ms.offset.x)/ms.scale.x;58 y=(y-ms.offset.y)/ms.scale.y;59 z=(z-ms.offset.z)/ms.scale.z;60 MsUpdate(x,y,z,l,r);61 MsHardSet(x,y,z,l,r);62}63 64U0 MsInit()65{66 MemSet(&ms,0,sizeof(CMsStateGlbls));67 MemSet(&ms_last,0,sizeof(CMsStateGlbls));68 ms.offset.x=ms.offset.y=ms.offset.z=0;69 ms.scale.x=ms.scale.y=ms.scale.z=1.0;70 ms.pos_text.x=ms.pos_text.y=ms.pos_text.z=0;71 ms.has_wheel=FALSE;72 ms.show=TRUE;73 ms.speed=0;74 ms.timestamp=GetTSC;75 ms.dbl_time=0.350;76 GridInit;77}78 79U0 MsHardPktRead()80{81 U8 j;82 if (GetTSC>ms_hard.timestamp+cnts.time_stamp_freq>>3)83 FifoU8Flush(ms_hard.fifo);84 ms_hard.timestamp=GetTSC;85 FifoU8Ins(ms_hard.fifo,InU8(KBD_PORT));86 if (FifoU8Cnt(ms_hard.fifo)==ms_hard.pkt_size)87 while (FifoU8Rem(ms_hard.fifo,&j))88 FifoU8Ins(ms_hard.fifo2,j);89}90 91interrupt U0 IRQMsHard()92{93 CLD94 OutU8(0xA0,0x20);95 OutU8(0x20,0x20);96 ms_hard.irqs_working=TRUE;97 if (ms_hard.install_in_progress || !ms_hard.installed) {98 kbd.rst=TRUE;99 return;100 }101 MsHardPktRead;102}103 104U0 MsHardGetType()105{106 I64 b;107 KbdMsCmdAck(0xF2);108 b=KbdCmdRead;109 if (b==3)110 ms_hard.has_wheel=TRUE;111 else if (b==4)112 ms_hard.has_ext_bttns=TRUE;113}114 115Bool MsHardRst()116{117 U8 b,*_b;118 F64 timeout;119 Bool res=FALSE;120 121 ms_hard.has_wheel=FALSE;122 ms_hard.has_ext_bttns=FALSE;123 124 if (*0x40E(U16 *)==0x9FC0) {125 _b=0x9FC00+0x30;126 *_b=1; //This enables my mouse. It might be for one machine.127//USB DMA packets, set-up by BIOS to make legacy PS/2?128 }129 130 try {131 KbdCmdFlush;132 KbdCmdSend(KBD_CTRL,0xAD); //Disable Kbd133 KbdCmdSend(KBD_CTRL,0xA8); //Enable Mouse134 135 KbdMsCmdAck(0xFF); //Rst136 137 timeout=tS+10.0;138 do139 try {140 KbdCmdRead;141 timeout=0; //force exit142 } catch143 Fs->catch_except=TRUE;144 while (tS<timeout);145 146 try147 KbdCmdRead;148 catch149 Fs->catch_except=TRUE;150 151 KbdMsCmdAck(0xF3,200,0xF3,100,0xF3,80);152 MsHardGetType;153 KbdMsCmdAck(0xF3,10);154 MsHardGetType;155 KbdMsCmdAck(0xE8,0x03,0xE6,0xF3,100,0xF4);156 res=TRUE;157 158 //Enable IRQ 12159 KbdCmdSend(KBD_CTRL,0x20);160 b=KbdCmdRead;161 KbdCmdSend(KBD_CTRL,0x60);162 KbdCmdSend(KBD_PORT,(b|2)&~0x20);163 164 } catch165 Fs->catch_except=TRUE;166 167 //This is been added to override failure168 //because the mouse sometimes still works.169 res=TRUE;170 171 try172 KbdCmdSend(KBD_CTRL,0xAE); //Enable Keyboard173 catch174 Fs->catch_except=TRUE;175 if (ms_hard.has_wheel || ms_hard.has_ext_bttns)176 ms_hard.pkt_size=4;177 else178 ms_hard.pkt_size=3;179 if (!res)180 try181 KbdCmdSend(KBD_CTRL,0xA7); //Disable Mouse182 catch183 Fs->catch_except=TRUE;184 return res;185}186 187U0 MsHardSpeedSet()188{189 I64 dd,tmp;190 if ((dd=SqrI64(ms_hard_last.pos.x-ms_hard.pos.x)191 +SqrI64(ms_hard_last.pos.y-ms_hard.pos.y)) &&192 (tmp=ms_hard.timestamp-ms_hard_last.timestamp))193 ms_hard.speed=Sqrt(dd)*cnts.time_stamp_freq/tmp;194 ms_hard_last.timestamp=ms_hard.timestamp;195}196 197U0 MsHardSetPre()198{199 I64 old_timestamp=ms_hard_last.timestamp;200 MemCpy(&ms_hard_last,&ms_hard,sizeof(CMsHardStateGlbls));201 ms_hard_last.timestamp=old_timestamp;202}203 204U0 MsHardSetPost()205{206 I64 i;207 ms_hard.pos.x=ms_hard.prescale.x*ms_hard.scale.x*ms_grid.x_speed;208 ms_hard.pos.y=ms_hard.prescale.y*ms_hard.scale.y*ms_grid.y_speed;209 ms_hard.pos.z=ms_hard.prescale.z*ms_hard.scale.z*ms_grid.z_speed;210 211 i=Trunc(ms.scale.x*ms_hard.pos.x/ms_grid.x)*ms_grid.x+ms.offset.x;212//TODO ms_grid.x_offset?213 if (i<0)214 ms.offset.x-=i;215 else if (i>=GR_WIDTH)216 ms.offset.x+=GR_WIDTH-1-i;217 218 i=Trunc(ms.scale.y*ms_hard.pos.y/ms_grid.y)*ms_grid.y+ms.offset.y;219 if (i<0)220 ms.offset.y-=i;221 else if (i>=GR_HEIGHT)222 ms.offset.y+=GR_HEIGHT-1-i;223 224 if (ms_hard.pos.x!=ms_hard_last.pos.x || ms_hard.pos.y!=ms_hard_last.pos.y ||225 ms_hard.pos.z!=ms_hard_last.pos.z) {226 ms_hard.evt=TRUE;227 MsHardSpeedSet;228 } else229 for (i=0;i<5;i++)230 if (ms_hard.bttns[i]!=ms_hard_last.bttns[i]) {231 ms_hard.evt=TRUE;232 break;233 }234}235 236U0 MsHardHndlr()237{238 I64 i,dx,dy,dz;239 U8 ms_buf[4];240 241 MsHardSetPre;242 for (i=0;i<4;i++)243 ms_buf[i]=0;244 for (i=0;i<ms_hard.pkt_size;i++)245 if (!FifoU8Rem(ms_hard.fifo2,&ms_buf[i]))246 ms_buf[i]=0;247 248 ms_hard.bttns[0] = ms_buf[0] & 1;249 ms_hard.bttns[1] = (ms_buf[0] & 2) >> 1;250 ms_hard.bttns[2] = (ms_buf[0] & 4) >> 2;251 ms_hard.bttns[3] = (ms_buf[3] & 0x10) >> 4;252 ms_hard.bttns[4] = (ms_buf[3] & 0x20) >> 5;253 if (ms_buf[0] & 0x10)254 dx=ms_buf[1]-256;255 else256 dx=ms_buf[1];257 if (ms_buf[0] & 0x20)258 dy=256-ms_buf[2];259 else260 dy=-ms_buf[2];261 if (ms_buf[3] & 0x08)262 dz=ms_buf[3]&7-8;263 else264 dz=ms_buf[3]&7;265 266 ms_hard.prescale.x+=dx;267 ms_hard.prescale.y+=dy;268 ms_hard.prescale.z+=dz;269 270 MsHardSetPost;271}272 273U0 MsHardSet(I64 x,I64 y,I64 z,I64 l,I64 r)274{275 ms_hard.timestamp=GetTSC;276 MsHardSetPre;277 ms_hard.prescale.x=x/ms_hard.scale.x/ms_grid.x_speed;278 ms_hard.prescale.y=y/ms_hard.scale.y/ms_grid.y_speed;279 ms_hard.prescale.z=z/ms_hard.scale.z/ms_grid.z_speed;280 ms_hard.bttns[0]=l;281 ms_hard.bttns[1]=r;282 MsHardSetPost;283}284 285U0 KbdMsRst()286{287 KbdCmdFlush;288 FifoU8Flush(kbd.fifo2);289 FifoU8Flush(ms_hard.fifo2);290 FifoI64Flush(kbd.scan_code_fifo);291 kbd.scan_code=0;292 kbd.rst=FALSE;293}294 295Bool MsHardDrvrInstall(I64 dummy=0) //can be spawned296{297 no_warn dummy;298 I64 i;299 ms_hard.install_in_progress=TRUE;300 OutU8(0xA1,InU8(0xA1)|0x10);301 ms_hard.installed=ms_hard.irqs_working=FALSE;302 IntEntrySet(0x2C,&IRQMsHard);303 for(i=0;i<5;i++)304 ms_hard.bttns[i]=0;305 if (i=MsHardRst)306 OutU8(0xA1,InU8(0xA1)&~0x10);307 KbdMsRst;308 ms_hard.install_attempts++;309 ms_hard.installed=ms_hard.evt=i;310 ms_hard.install_in_progress=FALSE;311 return ms_hard.installed;312}313 314U0 KbdMsHndlr(Bool poll_kbd,Bool poll_ms)315{316 if (ms_hard.install_in_progress) {317 Yield;318 return;319 }320 if (kbd.rst)321 KbdMsRst;322 else {323 if (poll_ms && ms_hard.installed && !ms_hard.irqs_working) {324 PUSHFD325 CLI326 while (InU8(KBD_CTRL)&1)327 MsHardPktRead;328 POPFD329 }330 331 if (poll_kbd)332 while (InU8(KBD_CTRL)&1)333 KbdPktRead;334 335 if (kbd.rst)336 KbdMsRst;337 else {338 while (FifoU8Cnt(kbd.fifo2))339 KbdHndlr;340 while (FifoU8Cnt(ms_hard.fifo2))341 if (ms_hard.installed)342 MsHardHndlr;343 else344 KbdMsRst;345 }346 }347}348 349U0 KbdMsInit()350{351 MemSet(&kbd,0,sizeof(CKbdStateGlbls));352 kbd.fifo=FifoU8New(8);353 kbd.fifo2=FifoU8New(0x1000);354 kbd.scan_code_fifo=FifoI64New(0x1000);355 kbd.irqs_working=FALSE;356 MemSet(&ms_hard,0,sizeof(CMsHardStateGlbls));357 ms_hard.fifo=FifoU8New(8);358 ms_hard.fifo2=FifoU8New(0x1000);359 ms_hard.scale.x=0.5;360 ms_hard.scale.y=0.5;361 ms_hard.scale.z=1.0;362 ms_hard.prescale.x=GR_WIDTH/ms_hard.scale.x/2.0;363 ms_hard.prescale.y=GR_HEIGHT/ms_hard.scale.y/2.0;364 ms_hard.prescale.z=0/ms_hard.scale.z;365 ms_hard.pos.x=GR_WIDTH>>1;366 ms_hard.pos.y=GR_HEIGHT>>1;367 MemCpy(&ms_hard_last,&ms_hard,sizeof(CMsHardStateGlbls));368}369 