/* Serial Mouse Tester Copyright (C) 2000-2007 Dmitry Brant http://dmitrybrant.com This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ //--------------------------------------------------------------------------- #include #pragma hdrstop #include "main.h" #include //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TMainForm *MainForm; //--------------------------------------------------------------------------- __fastcall TMainForm::TMainForm(TComponent* Owner) : TForm(Owner), comHandle(INVALID_HANDLE_VALUE) { } //--------------------------------------------------------------------------- void __fastcall TMainForm::MsgHandler(TMessage &message){ DWORD dw = (DWORD)message.WParam; unsigned char cm[3]; cm[0] = ((unsigned char*)&message.WParam)[0]; cm[1] = ((unsigned char*)&message.WParam)[1]; cm[2] = ((unsigned char*)&message.WParam)[2]; short dy=0, dx=0; static bool rightdown=false, leftdown=false; dx += ((cm[0] & 0x3) << 6); dx += (cm[1] & 0x3F); dy += ((cm[0] & 0xC) << 4); dy += (cm[2] & 0x3F); if(dx > 127) dx-=256; if(dy > 127) dy-=256; if(chkLog->Checked){ String temp1=""; temp1 = IntToHex((int)cm[0], 2) + " "; temp1 += IntToHex((int)cm[1], 2) + " "; temp1 += IntToHex((int)cm[2], 2); lblActivity->Caption = temp1; } if(chkPatch->Checked){ TPoint pt; pt = Mouse->CursorPos; if(dx) pt.x += dx; if(dy) pt.y += dy; if(pt.x >= Screen->Width) pt.x = Screen->Width - 1; if(pt.y >= Screen->Height) pt.y = Screen->Height - 1; Mouse->CursorPos = pt; pt.x = pt.x * (65535 / Screen->Width); pt.y = pt.y * (65535 / Screen->Height); if(cm[0] & 0x20){ if(!leftdown){ mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN, pt.x, pt.y, NULL, NULL); leftdown=true; } }else{ if(leftdown){ mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP, pt.x, pt.y, NULL, NULL); leftdown=false; } } if(cm[0] & 0x10){ if(!rightdown){ mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTDOWN, pt.x, pt.y, NULL, NULL); rightdown=true; } }else{ if(rightdown){ mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTUP, pt.x, pt.y, NULL, NULL); rightdown=false; } } }else{ if(dx) Shape1->Left += dx; if(dy) Shape1->Top += dy; if((cm[0] & 0x30) == 0x30) Shape1->Brush->Color = clAqua; else if(cm[0] & 0x20) Shape1->Brush->Color = clRed; else if(cm[0] & 0x10) Shape1->Brush->Color = clLime; else Shape1->Brush->Color = clBlue; } } //--------------------------------------------------------------------------- void __fastcall TMainForm::actConnectExecute(TObject *Sender){ DCB dcb; HANDLE handle; COMMTIMEOUTS ctmo; // address of communications time-out structure DWORD fdwAction; // action to perform if(comHandle != INVALID_HANDLE_VALUE){ CloseHandle(comHandle); comHandle = INVALID_HANDLE_VALUE; } handle = CreateFile(((TMenuItem*)Sender) == Connect1 ? "COM1" : "COM2", // LPCTSTR lpFileName, GENERIC_READ | GENERIC_WRITE, // DWORD dwDesiredAccess, 0, // DWORD dwShareMode, NULL, // LPSECURITY_ATTRIBUTES lpSecurityAttributes, OPEN_EXISTING, // DWORD dwCreationDistribution, NULL, // DWORD dwFlagsAndAttributes, NULL // HANDLE hTemplateFile ); if (handle == INVALID_HANDLE_VALUE){ Application->MessageBox("Could not open COM port.", "Error", MB_ICONSTOP); return; } if(!GetCommState(handle, &dcb)){ CloseHandle(handle); Application->MessageBox("GetCommState() failed.", "Error", MB_ICONSTOP); return; } dcb.BaudRate = 1200; dcb.ByteSize = 7; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; //dcb.fOutxCtsFlow = 0; //dcb.fOutxDsrFlow = 0; dcb.fDtrControl = DTR_CONTROL_ENABLE; //dcb.fDsrSensitivity = 0; //dcb.fOutX = 0; //dcb.fInX = 0; //dcb.fNull = 0; dcb.fRtsControl = RTS_CONTROL_ENABLE; //dcb.XonLim = 4; //dcb.XoffLim = 1; if (!SetCommState(handle, &dcb)){ CloseHandle(handle); Application->MessageBox("SetCommState() failed.", "Error", MB_ICONSTOP); return; } fdwAction = PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR; if (!PurgeComm(handle, fdwAction)){ CloseHandle(handle); Application->MessageBox("PurgeComm() failed.", "Error", MB_ICONSTOP); return; } comHandle = handle; // file scope variable theThread = new ComThread(); //start the thread Connect1->Enabled = false; Connect2->Enabled = false; Disconnect1->Enabled = true; } //--------------------------------------------------------------------------- void __fastcall TMainForm::actDisconnectExecute(TObject *Sender){ if(comHandle == INVALID_HANDLE_VALUE) return; theThread->Terminate(); theThread->WaitFor(); delete theThread; theThread = NULL; CloseHandle(comHandle); comHandle = INVALID_HANDLE_VALUE; Connect1->Enabled = true; Connect2->Enabled = true; Disconnect1->Enabled = false; } //--------------------------------------------------------------------------- void __fastcall TMainForm::Exit1Click(TObject *Sender){ Close(); } //--------------------------------------------------------------------------- void __fastcall TMainForm::FormMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y){ Shape1->Left = X; Shape1->Top = Y; } //--------------------------------------------------------------------------- void __fastcall TMainForm::chkPatchClick(TObject *Sender){ chkPatch->Checked = !chkPatch->Checked; } //--------------------------------------------------------------------------- void __fastcall TMainForm::mnuAboutClick(TObject *Sender){ Application->MessageBox("Serial Mouse Tester. Copyright 2000-2007 Dmitry Brant, All Rights Reserved.\nhttp://dmitrybrant.com", "About...", MB_ICONINFORMATION); } //--------------------------------------------------------------------------- void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action){ if(theThread){ theThread->Terminate(); delete theThread; } if(comHandle != INVALID_HANDLE_VALUE) CloseHandle(comHandle); } //--------------------------------------------------------------------------- void __fastcall TMainForm::chkLogClick(TObject *Sender){ chkLog->Checked = !chkLog->Checked; } //---------------------------------------------------------------------------