| 1 | unit uInit;
 | 
|---|
| 2 | 
 | 
|---|
| 3 | interface
 | 
|---|
| 4 | 
 | 
|---|
| 5 | uses
 | 
|---|
| 6 |   Forms, Windows, Messages, SysUtils, ExtCtrls, ORSystem;
 | 
|---|
| 7 | 
 | 
|---|
| 8 | type
 | 
|---|
| 9 | {$IFDEF GroupEncounter}
 | 
|---|
| 10 |   TCPRSTimeoutTimerCondition = function: boolean;
 | 
|---|
| 11 |   TCPRSTimeoutTimerAction = procedure;
 | 
|---|
| 12 | {$ELSE}
 | 
|---|
| 13 |   TCPRSTimeoutTimerCondition = function: boolean of object;
 | 
|---|
| 14 |   TCPRSTimeoutTimerAction = procedure of object;
 | 
|---|
| 15 | {$ENDIF}
 | 
|---|
| 16 | 
 | 
|---|
| 17 | procedure AutoUpdateCheck;
 | 
|---|
| 18 | 
 | 
|---|
| 19 | procedure InitTimeOut(AUserCondition: TCPRSTimeoutTimerCondition;
 | 
|---|
| 20 |                       AUserAction: TCPRSTimeoutTimerAction);
 | 
|---|
| 21 | procedure UpdateTimeOutInterval(NewTime: Cardinal);
 | 
|---|
| 22 | function TimedOut: boolean;
 | 
|---|
| 23 | procedure ShutDownTimeOut;
 | 
|---|
| 24 | 
 | 
|---|
| 25 | implementation
 | 
|---|
| 26 | 
 | 
|---|
| 27 | uses
 | 
|---|
| 28 |   fTimeout;
 | 
|---|
| 29 |   
 | 
|---|
| 30 | type
 | 
|---|
| 31 |   TCPRSTimeoutTimer = class(TTimer)
 | 
|---|
| 32 |   private
 | 
|---|
| 33 |     FHooked: boolean;
 | 
|---|
| 34 |     FUserCondition: TCPRSTimeoutTimerCondition;
 | 
|---|
| 35 |     FUserAction: TCPRSTimeoutTimerAction;
 | 
|---|
| 36 |     uTimeoutInterval: Cardinal;
 | 
|---|
| 37 |     uTimeoutKeyHandle, uTimeoutMouseHandle: HHOOK;
 | 
|---|
| 38 |   protected
 | 
|---|
| 39 |     procedure ResetTimeout;
 | 
|---|
| 40 |     procedure timTimeoutTimer(Sender: TObject);
 | 
|---|
| 41 |   end;
 | 
|---|
| 42 | 
 | 
|---|
| 43 | var
 | 
|---|
| 44 |   timTimeout: TCPRSTimeoutTimer = nil;
 | 
|---|
| 45 |   FTimedOut: boolean = FALSE;
 | 
|---|
| 46 | 
 | 
|---|
| 47 | function TimeoutKeyHook(Code: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; StdCall; forward;
 | 
|---|
| 48 | function TimeoutMouseHook(Code: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; StdCall; forward;
 | 
|---|
| 49 |   
 | 
|---|
| 50 | procedure AutoUpdateCheck;
 | 
|---|
| 51 | const
 | 
|---|
| 52 | {$IFDEF GroupEncounter}
 | 
|---|
| 53 |   AppHelpFile = 'CPRSGE';
 | 
|---|
| 54 | {$ELSE}
 | 
|---|
| 55 |   AppHelpFile = 'CPRS';
 | 
|---|
| 56 |   WhatsThisHelpFile = 'CPRSWT';
 | 
|---|
| 57 | {$ENDIF}
 | 
|---|
| 58 | var
 | 
|---|
| 59 |   x, CPRSUpdate :string;
 | 
|---|
| 60 | 
 | 
|---|
| 61 | begin
 | 
|---|
| 62 |   CPRSUpdate := RegReadStr(CPRS_REG_GOLD) + 'CPRSUpdate.exe';
 | 
|---|
| 63 |   if not FileExists(CPRSUpdate) then CPRSUpdate := 'CPRSUpdate.exe';
 | 
|---|
| 64 |   x := FullToPathPart(Application.ExeName) + AppHelpFile + '.HLP';
 | 
|---|
| 65 |   if AppOutOfDate(x) and FileExists(CPRSUpdate) then RunProgram(CPRSUpdate + ' XFER="' + x + '"');
 | 
|---|
| 66 |   x := FullToPathPart(Application.ExeName) + AppHelpFile + '.CNT';
 | 
|---|
| 67 |   if AppOutOfDate(x) and FileExists(CPRSUpdate) then RunProgram(CPRSUpdate + ' XFER="' + x + '"');
 | 
|---|
| 68 |   x := FullToPathPart(Application.ExeName) + WhatsThisHelpFile + '.HLP';
 | 
|---|
| 69 |   if AppOutOfDate(x) and FileExists(CPRSUpdate) then RunProgram(CPRSUpdate + ' XFER="' + x + '"');
 | 
|---|
| 70 |   x := FullToPathPart(Application.ExeName) + WhatsThisHelpFile + '.CNT';
 | 
|---|
| 71 |   if AppOutOfDate(x) and FileExists(CPRSUpdate) then RunProgram(CPRSUpdate + ' XFER="' + x + '"');
 | 
|---|
| 72 |   // Moved to CPRSUpdate.EXE in early test version of v27.  This code removed for CPRS v27.27.
 | 
|---|
| 73 |   //x := FullToPathPart(Application.ExeName) + 'BORLNDMM.DLL';
 | 
|---|
| 74 |   //if AppOutOfDate(x) and FileExists(CPRSUpdate) then RunProgram(CPRSUpdate + ' XFER="' + x + '"');
 | 
|---|
| 75 | end;
 | 
|---|
| 76 | 
 | 
|---|
| 77 | {** Timeout Functions **}
 | 
|---|
| 78 | 
 | 
|---|
| 79 | function TimeoutKeyHook(Code: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT;
 | 
|---|
| 80 | { this is called for every keyboard event that occurs while running CPRS }
 | 
|---|
| 81 | begin
 | 
|---|
| 82 |   if lParam shr 31 = 1 then timTimeout.ResetTimeout;                          // on KeyUp only
 | 
|---|
| 83 |   Result := CallNextHookEx(timTimeout.uTimeoutKeyHandle, Code, wParam, lParam);
 | 
|---|
| 84 | end;
 | 
|---|
| 85 | 
 | 
|---|
| 86 | function TimeoutMouseHook(Code: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT;
 | 
|---|
| 87 | { this is called for every mouse event that occurs while running CPRS }
 | 
|---|
| 88 | begin
 | 
|---|
| 89 |   if (Code >= 0) and (wParam > WM_MOUSEFIRST) and (wParam <= WM_MOUSELAST)
 | 
|---|
| 90 |     then timTimeout.ResetTimeout;                                             // all click events
 | 
|---|
| 91 |   Result := CallNextHookEx(timTimeout.uTimeoutMouseHandle, Code, wParam, lParam);
 | 
|---|
| 92 | end;
 | 
|---|
| 93 | 
 | 
|---|
| 94 | procedure InitTimeOut(AUserCondition: TCPRSTimeoutTimerCondition;
 | 
|---|
| 95 |                       AUserAction: TCPRSTimeoutTimerAction);
 | 
|---|
| 96 | begin
 | 
|---|
| 97 |   if(not assigned(timTimeout)) then
 | 
|---|
| 98 |   begin
 | 
|---|
| 99 |     timTimeOut := TCPRSTimeoutTimer.Create(Application);
 | 
|---|
| 100 |     with timTimeOut do
 | 
|---|
| 101 |     begin
 | 
|---|
| 102 |       OnTimer := timTimeoutTimer;
 | 
|---|
| 103 |       FUserCondition := AUserCondition;
 | 
|---|
| 104 |       FUserAction := AUserAction;
 | 
|---|
| 105 |       uTimeoutInterval    := 120000;  // initially 2 minutes, will get DTIME after signon
 | 
|---|
| 106 |       uTimeoutKeyHandle   := SetWindowsHookEx(WH_KEYBOARD, TimeoutKeyHook,   0, GetCurrentThreadID);
 | 
|---|
| 107 |       uTimeoutMouseHandle := SetWindowsHookEx(WH_MOUSE,    TimeoutMouseHook, 0, GetCurrentThreadID);
 | 
|---|
| 108 |       FHooked := TRUE;
 | 
|---|
| 109 |       Interval := uTimeoutInterval;
 | 
|---|
| 110 |       Enabled  := True;
 | 
|---|
| 111 |     end;
 | 
|---|
| 112 |   end;
 | 
|---|
| 113 | end;
 | 
|---|
| 114 | 
 | 
|---|
| 115 | procedure UpdateTimeOutInterval(NewTime: Cardinal);
 | 
|---|
| 116 | begin
 | 
|---|
| 117 |   if(assigned(timTimeout)) then
 | 
|---|
| 118 |   begin
 | 
|---|
| 119 |     with timTimeout do
 | 
|---|
| 120 |     begin
 | 
|---|
| 121 |       uTimeoutInterval := NewTime;
 | 
|---|
| 122 |       Interval := uTimeoutInterval;
 | 
|---|
| 123 |       Enabled  := True;
 | 
|---|
| 124 |     end;
 | 
|---|
| 125 |   end;
 | 
|---|
| 126 | end;
 | 
|---|
| 127 | 
 | 
|---|
| 128 | function TimedOut: boolean;
 | 
|---|
| 129 | begin
 | 
|---|
| 130 |   Result := FTimedOut;
 | 
|---|
| 131 | end;
 | 
|---|
| 132 | 
 | 
|---|
| 133 | procedure ShutDownTimeOut;
 | 
|---|
| 134 | begin
 | 
|---|
| 135 |   if(assigned(timTimeout)) then
 | 
|---|
| 136 |   begin
 | 
|---|
| 137 |     with timTimeout do
 | 
|---|
| 138 |     begin
 | 
|---|
| 139 |       Enabled := False;
 | 
|---|
| 140 |       if(FHooked) then
 | 
|---|
| 141 |       begin
 | 
|---|
| 142 |         UnhookWindowsHookEx(uTimeoutKeyHandle);
 | 
|---|
| 143 |         UnhookWindowsHookEx(uTimeoutMouseHandle);
 | 
|---|
| 144 |         FHooked := FALSE;
 | 
|---|
| 145 |       end;
 | 
|---|
| 146 |     end;
 | 
|---|
| 147 |     timTimeout.Free;
 | 
|---|
| 148 |     timTimeout := nil;
 | 
|---|
| 149 |   end;
 | 
|---|
| 150 | end;
 | 
|---|
| 151 | 
 | 
|---|
| 152 | 
 | 
|---|
| 153 | { TCPRSTimeoutTime }
 | 
|---|
| 154 | 
 | 
|---|
| 155 | procedure TCPRSTimeoutTimer.ResetTimeout;
 | 
|---|
| 156 | { this restarts the timer whenever there is a keyboard or mouse event }
 | 
|---|
| 157 | begin
 | 
|---|
| 158 |   Enabled  := False;
 | 
|---|
| 159 |   Interval := uTimeoutInterval;
 | 
|---|
| 160 |   Enabled  := True;
 | 
|---|
| 161 | end;
 | 
|---|
| 162 | 
 | 
|---|
| 163 | procedure TCPRSTimeoutTimer.timTimeoutTimer(Sender: TObject);
 | 
|---|
| 164 | { when the timer expires, the application is closed after warning the user }
 | 
|---|
| 165 | begin
 | 
|---|
| 166 |   Enabled := False;
 | 
|---|
| 167 |   if(assigned(FUserCondition)) then
 | 
|---|
| 168 |     FTimedOut := FUserCondition or AllowTimeout
 | 
|---|
| 169 |   else
 | 
|---|
| 170 |     FTimedOut := AllowTimeout;
 | 
|---|
| 171 |   if FTimedOut then
 | 
|---|
| 172 |   begin
 | 
|---|
| 173 |     if(assigned(FUserAction)) then FUserAction;
 | 
|---|
| 174 |   end
 | 
|---|
| 175 |   else
 | 
|---|
| 176 |     Enabled := True;
 | 
|---|
| 177 | end;
 | 
|---|
| 178 | 
 | 
|---|
| 179 | initialization
 | 
|---|
| 180 | 
 | 
|---|
| 181 | finalization
 | 
|---|
| 182 |   ShutDownTimeOut;
 | 
|---|
| 183 | 
 | 
|---|
| 184 | end.
 | 
|---|