-
Notifications
You must be signed in to change notification settings - Fork 0
/
JudithKernel.pas
185 lines (159 loc) · 4.01 KB
/
JudithKernel.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
unit JudithKernel;
interface
uses
windows, messages, jsruntime, System.Classes, msgserver, System.SysUtils,
XSuperObject, keyhook;
const
chInterval = 100;
type
TJudith = class
constructor create;
destructor free;
private
procedure handlemsg(var Msg: TMessage);
procedure handledata(var Msg: Twmcopydata);
procedure DeviceArrival(Sender: TObject);
procedure DeviceRemove(Sender: TObject);
procedure ProcHotkey(var key: word);
procedure ProcHook(Sender: TObject; Action: TKeyHookAction; KeyName: string; keycode: integer);
public
active: Boolean;
window: Tmsgserver;
engine: Tjsruntime;
keyhook: Tkeyhook;
procedure terminate;
private
lastmsg: Int64;
lastch: dword;
end;
type
PJudith = ^TJudith;
implementation
uses
System.strutils, JSNatives, entrypoint, mod_keyhook;
function IsFileInUse(filename: TFileName): Boolean;
var
HFileRes: HFILE;
begin
Result := False;
if not FileExists(filename) then
Exit;
HFileRes := CreateFile(PChar(filename), GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
Result := (HFileRes = INVALID_HANDLE_VALUE);
if not Result then
CloseHandle(HFileRes);
end;
constructor TJudith.create;
begin
window := Tmsgserver.create;
window.OnMessage := self.handlemsg;
window.OnData := self.handledata;
WM_JUDITH := RegisterWindowMessage('JudithOrigin');
window.OnDeviceArrival := DeviceArrival;
window.OnDeviceRemove := DeviceRemove;
window.OnHotkey := ProcHotkey;
keyhook := Tkeyhook.create(nil);
keyhook.OnHook := ProcHook;
keyhook.Open(window.handle);
ahandle := window.handle;
JSNatives.winh := window.handle;
engine := Tjsruntime.create;
active := true;
end;
destructor TJudith.free;
begin
active := False;
__shutdown := true;
keyhook.Close;
keyhook.Destroy;
engine.free;
window.free;
end;
procedure TJudith.terminate;
begin
// cannot close application properly for now
TerminateProcess(GetCurrentProcess, 0);
self.active := False;
end;
function b2s(b: Boolean): string;
begin
if b then
Result := '1'
else
Result := '0';
end;
procedure TJudith.ProcHook(Sender: TObject; Action: TKeyHookAction; KeyName: string; keycode: integer);
begin
if Action = khaDown then
begin
engine.processEvent(5, '', keycode);
engine.processEvent(6, b2s((caps)), keycode);
end;
end;
procedure TJudith.handledata(var Msg: Twmcopydata);
var
cmd, param: string;
data: string;
begin
data := trim(PChar(Msg.CopyDataStruct.lpData));
if data = '' then
Exit;
outputdebugstring(pchar(data));
if data[1] = '~' then
begin
cmd := Copy(data, 2, Pos('=', data) - 2);
param := Copy(data, Pos('=', data) + 1, Length(data));
OutputDebugStringW(pwidechar('command: ' + cmd + ' param: ' + param));
case IndexStr(cmd, ['eval']) of
0:
self.engine.Eval(param);
end;
Exit;
end;
engine.processEvent(9, data);
end;
procedure TJudith.ProcHotkey(var key: word);
begin
engine.processEvent(4, '', key);
end;
procedure TJudith.DeviceRemove(Sender: TObject);
begin
engine.processEvent(2);
end;
procedure TJudith.DeviceArrival(Sender: TObject);
begin
engine.processEvent(1);
end;
procedure TJudith.handlemsg(var Msg: TMessage);
var
wm: Twinmsg;
begin
wm.Msg := 0;
case Msg.Msg of
WM_COMMAND:
wm.Msg := 1;
WM_TIMECHANGE:
wm.Msg := 2;
WM_POWERBROADCAST:
wm.Msg := 3;
WM_DISPLAYCHANGE:
wm.Msg := 4;
WM_ENDSESSION:
wm.Msg := 5;
end;
if wm.Msg > 0 then
begin
if (wm.Msg = 1) and (Msg.WParam = 3535) then
begin
engine.processEvent(8, '', Msg.LParam);
end
else if ((gettickcount - lastmsg) > 250) then
begin
lastmsg := gettickcount;
engine.processEvent(10, '', wm.Msg);
end
else
OutputDebugString(PChar('duplicate window message detected'));
end;
end;
end.