forked from gphilippot/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Automation.pb
395 lines (323 loc) · 11.9 KB
/
Automation.pb
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
;--------------------------------------------------------------------------------------------
; Copyright (c) Fantaisie Software. All rights reserved.
; Dual licensed under the GPL and Fantaisie Software licenses.
; See LICENSE and LICENSE-FANTAISIE in the project root for license information.
;--------------------------------------------------------------------------------------------
;
; This file exposes IDE functionality to external programs.
; The functionality will be accessible through a dll compiled from AutomationLibrary.pb
; The actual communication protocol is private
;
; This way to make the IDE extensible has some advantages:
; - no plugin code is running in the IDE process (so no plugin can easily crash the IDE)
; - we control what can be controlled from a plugin, so we can be sure the stuff
; does not leave the IDE in an invalid state
; - the dll and private communication makes using this simple, and we can change things as we want
;
; This file implements the IDE side of the communication
; The Unix domain socket stuff is implemented in C for simplicity
;
CompilerIf #CompileWindows = 0
#SOCKET_ERROR = -1
#INVALID_SOCKET = -1
ImportC #BUILD_DIRECTORY + "AutomationDomainSocket.o"
DomainSocket_Create(path.p-ascii)
DomainSocket_Accept(socket.l)
DomainSocket_Connect(path.p-ascii)
DomainSocket_Close(socket.l)
DomainSocket_Send(socket.l, *call)
DomainSocket_Receive(socket.l)
EndImport
Global AutomationSocket = #SOCKET_ERROR
Global AutomationSocketFile$
Global NewList AutomationClient()
CompilerEndIf
Structure AutomationEvent
List Client.i()
EndStructure
Global NewMap AutomationEvent.AutomationEvent()
; Test if an event client is connection
;
Procedure IsAutomationEventClient(Event$)
If FindMapElement(AutomationEvent(), Event$) And ListSize(AutomationEvent()\Client()) > 0
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
; Fill *Call with an error response
;
Procedure AutomationError(*Call.RPC_Call, Message$)
RPC_InitResponse(*Call, 1, #True)
RPC_SetString(*Call, 0, Message$)
EndProcedure
; Fill *Call with an empty response message (no error)
;
Procedure AutomationVoid(*Call.RPC_Call)
RPC_InitResponse(*Call, 0, #False)
EndProcedure
; Received an (already decoded) RPC_Call and must fill it with response data
;
Procedure AutomationRequest(*Call.RPC_Call, ClientID)
CompilerIf #DEBUG
RPC_DebugCall(*Call, "Automation Request")
CompilerEndIf
Select *Call\Function$ ; name is case sensitive
; On Linux/OSX, the client just connects to all available IDE instances
; and uses the following command to identify the correct one
;
CompilerIf #CompileWindows = 0
Case "Identify"
RPC_InitResponse(*Call, 4, #False)
RPC_SetLong(*Call, 0, AsciiConst('A', 'U', 'T', '1')) ; identify "protocol" version
RPC_SetString(*Call, 1, ProgramFilename())
RPC_SetLong(*Call, 2, getpid_())
RPC_SetQuad(*Call, 3, WindowID(#WINDOW_Main)) ; for x64 compatibility
CompilerEndIf
Case "RegisterEvent"
Event$ = RPC_GetString(*Call, 0)
AddElement(AutomationEvent(Event$)\Client())
AutomationEvent(Event$)\Client() = ClientID
AutomationVoid(*Call)
Case "UnregisterEvent"
Event$ = RPC_GetString(*Call, 0)
If FindMapElement(AutomationEvent(), Event$)
ForEach AutomationEvent()\Client()
If AutomationEvent()\Client() = ClientID
DeleteElement(AutomationEvent()\Client())
Break
EndIf
Next AutomationEvent()\Client()
EndIf
AutomationVoid(*Call)
;
Case "MenuCommand"
Command$ = UCase(RPC_GetString(*Call, 0))
MenuItem = -1
Restore ShortcutItems ;
For i = 0 To #MENU_LastShortcutItem
Read.s MenuTitle$
Read.s MenuItem$
Read.l DefaultShortcut
If Command$ = UCase(MenuItem$)
MenuItem = i
Break
EndIf
Next i
CompilerIf #CompileMac
; handle the 3 special cases
If MenuItem = #MENU_AboutNotUsed
MenuItem = #MENU_About
ElseIf MenuItem = #MENU_PreferenceNotUsed
MenuItem = #MENU_Preference
ElseIf MenuItem = #MENU_ExitNotUsed
MenuItem = #MENU_Exit
EndIf
CompilerEndIf
If MenuItem = -1
AutomationError(*Call, "Menu Command not found")
Else
MainMenuEvent(MenuItem)
AutomationVoid(*Call)
EndIf
; this is for simple debugging only
; Case "SayHello"
; Name$ = RPC_GetString(*Call, 0)
; RPC_InitResponse(*Call, 1)
; RPC_SetString(*Call, 0, "Hello '"+Name$+"' !")
Default
AutomationError(*Call, "Invalid request.")
EndSelect
CompilerIf #DEBUG
RPC_DebugCall(*Call, "Automation Response")
CompilerEndIf
EndProcedure
; Currently nothing to do here on Windows
Procedure InitAutomation()
; CompilerIf #CompileWindows = 0
; ; Unix domain sockets operate on a pseudofile, and since we can have multiple
; ; IDE instances running, try multiple times with different names
; ;
; For i = 0 To 20
; AutomationSocketFile$ = "/tmp/.pb-automation-" + Str(Random(10000))
; AutomationSocket = DomainSocket_Create(AutomationSocketFile$)
; If AutomationSocket <> #SOCKET_ERROR
; ; success
; Break
; EndIf
; Next i
;
; CompilerEndIf
EndProcedure
Procedure ShutdownAutomation()
; CompilerIf #CompileWindows = 0
; If AutomationSocket <> #SOCKET_ERROR
; ForEach AutomationClient()
; DomainSocket_Close(AutomationClient())
; Next AutomationClient()
; ClearList(AutomationClient())
;
; DomainSocket_Close(AutomationSocket)
; DeleteFile(AutomationSocketFile$)
; AutomationSocket = #SOCKET_ERROR
; EndIf
; CompilerEndIf
EndProcedure
CompilerIf #CompileWindows = 0
; Communication via unix domain sockets
;
Procedure ProcessAutomationRequest()
Protected Call.RPC_Call
Protected Processed = 0
; not used yet
ProcedureReturn 0
If AutomationSocket <> #SOCKET_ERROR
; Check for new connections
;
Repeat
socket = DomainSocket_Accept(AutomationSocket)
If socket <> #SOCKET_ERROR
AddElement(AutomationClient())
AutomationClient() = socket
Debug "[Automation] Client connected: " + Str(socket)
EndIf
Until socket = #SOCKET_ERROR
; Check for data on existing connections
ForEach AutomationClient()
Repeat
*Buffer = DomainSocket_Receive(AutomationClient())
If *Buffer = -1
; special value indicating that the connection was closed
Debug "[Automation] Client disconnect: " + Str(AutomationClient())
DomainSocket_Close(AutomationClient())
DeleteElement(AutomationClient())
Break
ElseIf *Buffer
; a command was received
Processed + 1
Size = PeekL(*Buffer)
If RPC_Decode(@Call, *Buffer, Size, #False) ; do not copy, Call takes ownership of the buffer
; process the decoded request
AutomationRequest(@Call, AutomationClient())
; encode the response
If RPC_Encode(@Call)
; send result
DomainSocket_Send(AutomationClient(), Call\Encoded)
EndIf
RPC_ClearCall(@Call)
Else
; need to free the buffer
FreeMemory(*Buffer)
EndIf
EndIf
Until *Buffer = 0 Or Processed > 50
If Processed > 50 ; do not handle too many requests at once
Break
EndIf
Next AutomationClient()
EndIf
EndProcedure
CompilerElse
Global *CurrentAutomationCall.RPC_Call
Global AutomationCallComplete
; The communication is done via WM_COPYDATA messages on Windows, which
; is quite simple on the IDE side
;
Procedure ProcessAutomationRequest(WindowID, *copy.COPYDATASTRUCT)
; Protected Call.RPC_Call
; Protected *WaitingCall.RPC_Call
;
; If *copy\dwData = 'AUT1' And RPC_Decode(@Call, *copy\lpData, *copy\cbData)
;
; If Call\IsResponse
; ; response to an event
; ;
; CopyStructure(*CurrentAutomationCall, @Call, RPC_Call)
; AutomationCallComplete = #True
;
; Else
; ; request from client
; *WaitingCall = *CurrentAutomationCall ; store this locally, to allow nested events/calls
;
; ; process the decoded request
; AutomationRequest(@Call, WindowID)
;
; ; encode the response
; If RPC_Encode(@Call)
; ; send result
; response.COPYDATASTRUCT\dwData = 'AUT1'
; response\cbData = Call\EncodedSize
; response\lpData = Call\Encoded
;
; ; Use a timeout to he protected from hung client programs
; ;
; If SendMessageTimeout_(WindowID, #WM_COPYDATA, WindowID(#WINDOW_Main), @response, #SMTO_ABORTIFHUNG|#SMTO_NORMAL, 5000, @MessageResult) = 0
; MessageRequester(#ProductName$,Language("Misc","AutomationTimeout"), #FLAG_Error)
; EndIf
; EndIf
; RPC_ClearCall(@Call)
;
; *CurrentAutomationCall = *WaitingCall
; AutomationCallComplete = #False
;
; EndIf
;
; EndIf
EndProcedure
Procedure SendAutomationEvent(*Call.RPC_Call, ChainClients = #False)
Success = 0
If FindMapElement(AutomationEvent(), *Call\Function$)
CompilerIf #DEBUG
RPC_DebugCall(*Call, "Automation Event")
CompilerEndIf
ForEach AutomationEvent()\Client()
If RPC_Encode(*Call)
request.COPYDATASTRUCT\dwData = AsciiConst('A', 'U', 'T', '1')
request\cbData = *Call\EncodedSize
request\lpData = *Call\Encoded
*CurrentAutomationCall = *Call
AutomationCallComplete = #False
If SendMessageTimeout_(AutomationEvent()\Client(), #WM_COPYDATA, WindowID(#WINDOW_Main), @request, #SMTO_ABORTIFHUNG|#SMTO_NORMAL, 5000, @MessageResult) = 0
MessageRequester(#ProductName$,Language("Misc","AutomationTimeout"), #FLAG_Error)
Success = #False
Break
ElseIf *Call\ErrorFlag <> 0
Success = #False
Break
Else
Success = #True
; If ChainClients is set, the response from the first client is sent to further clients
; (so Call & Response must have the same signature
If ChainClients = #False
Break
EndIf
EndIf
EndIf
Next AutomationEvent()\Client()
CompilerIf #DEBUG
RPC_DebugCall(*Call, "Automation Event (Response)")
CompilerEndIf
EndIf
ProcedureReturn Success
EndProcedure
CompilerEndIf
;- Automation events
;
Procedure AutomationEvent_AutoComplete(List Elements.s())
; Protected Call.RPC_Call
; If IsAutomationEventClient("AutoComplete")
; RPC_InitCall(@Call, "AutoComplete", ListSize(Elements()))
; ForEach Elements()
; RPC_SetString(@Call, ListIndex(Elements()), Elements())
; Next Elements()
;
; If SendAutomationEvent(@Call, #True)
; ClearList(Elements())
; For i = 0 To Call\NbParameters-1
; AddElement(Elements())
; Elements() = RPC_GetString(@Call, i)
; Next i
; EndIf
; RPC_ClearCall(@Call)
; EndIf
EndProcedure