forked from fantaisie-software/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutomationLibrary.pb
More file actions
632 lines (515 loc) · 17.9 KB
/
AutomationLibrary.pb
File metadata and controls
632 lines (515 loc) · 17.9 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
;--------------------------------------------------------------------------------------------
; 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.
;--------------------------------------------------------------------------------------------
;
; Implements the client side of the IDE automation
; This is compiled into a dll so tools can use it
;
XIncludeFile "CompilerFlags.pb"
XIncludeFile "Macro.pb"
XIncludeFile "FileSystem.pb"
XIncludeFile "RemoteProcedureCall.pb"
Prototype EventCallback(*Call)
Global LastError$ = "" ; initialize, so the pointer is not NULL
Global EventCallback.EventCallback = 0
CompilerIf #CompileWindows = 0
;
;- Unix specific communication stuff
; The Unix domain socket stuff is implemented in C for simplicity
;
#SOCKET_ERROR = -1
#INVALID_SOCKET = -1
CompilerIf #PB_Compiler_Debugger
#DEBUG = 1
#BUILD_DIRECTORY = "/Users/freak/PureBuild/v4.60_X86/ide/"
CompilerEndIf
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
Procedure SendRequest(*Call.RPC_Call)
Success = 0
CompilerIf #DEBUG
RPC_DebugCall(*Call, "Sending request")
CompilerEndIf
If AutomationSocket <> #SOCKET_ERROR And RPC_Encode(*Call)
If DomainSocket_Send(AutomationSocket, *Call\Encoded)
; wait for response
Timeout.q = ElapsedMilliseconds()
Repeat
*Buffer = DomainSocket_Receive(AutomationSocket)
If *Buffer = 0
Delay(10)
EndIf
Until *Buffer <> 0 Or ElapsedMilliseconds()-Timeout > 5000
If *Buffer = -1
; connection lost
AutomationSocket = #SOCKET_ERROR
RPC_InitResponse(*Call, 1, #True)
RPC_SetString(*Call, 0, "Connection lost")
ElseIf *Buffer
Size = PeekL(*Buffer)
If RPC_Decode(*Call, *Buffer, Size, #False) ; do not copy. the buffer now belongs to the call
Success = 1
Else
FreeMemory(*Buffer) ; in case of error
RPC_InitResponse(*Call, 1, #True)
RPC_SetString(*Call, 0, "Out of resources")
EndIf
Else
RPC_InitResponse(*Call, 1, #True)
RPC_SetString(*Call, 0, "Communication timeout")
EndIf
Else
RPC_InitResponse(*Call, 1, #True)
RPC_SetString(*Call, 0, "Connection lost")
EndIf
Else
RPC_InitResponse(*Call, 1, #True)
RPC_SetString(*Call, 0, "Out of resources")
EndIf
CompilerIf #DEBUG
RPC_DebugCall(*Call, "Received response")
CompilerEndIf
If *Call\ErrorFlag
LastError$ = RPC_GetString(*Call, 0)
Success = 0
EndIf
ProcedureReturn Success
EndProcedure
Procedure WaitEvents(Timeout)
EndProcedure
Procedure Connect(WindowID, ProcessID, Executable$)
Protected Call.RPC_Call
Success = 0
If Executable$
; make path absolute
Executable$ = ResolveRelativePath(GetCurrentDirectory(), Executable$)
EndIf
; enumerate all domain sockets from the IDE naming scheme and query them
; to see if they match our criteria
;
If ExamineDirectory(0, "/tmp/", ".pb-automation-*")
While NextDirectoryEntry(0)
If DirectoryEntryType(0) = #PB_DirectoryEntry_File
Path$ = "/tmp/" + DirectoryEntryName(0)
AutomationSocket = DomainSocket_Connect(Path$)
If AutomationSocket <> #SOCKET_ERROR
; send the identify command to this IDE
RPC_InitCall(@Call.RPC_Call, "Identify", 0)
If SendRequest(@Call)
If RPC_GetLong(@Call, 0) = AsciiConst('A','U','T','1') ; check protocol version first
If WindowID
If RPC_GetQuad(@Call, 3) = WindowID
Success = 1
EndIf
ElseIf ProcessID
If RPC_GetLong(@Call, 2) = ProcessID
Success = 1
EndIf
ElseIf Executable$
If IsEqualFile(Executable$, RPC_GetString(@Call, 1))
Success = 1
EndIf
Else
; connect to any, so we accept this connection
Success = 1
EndIf
EndIf
EndIf
RPC_ClearCall(@Call)
If Success
Break
Else
; not the right IDE
DomainSocket_Close(AutomationSocket)
AutomationSocket = #SOCKET_ERROR
EndIf
EndIf
EndIf
Wend
FinishDirectory(0)
EndIf
If Success = 0
LastError$ = "Connection could not be established."
EndIf
ProcedureReturn Success
EndProcedure
Procedure Disconnect()
DomainSocket_Close(AutomationSocket)
AutomationSocket = #SOCKET_ERROR
EndProcedure
CompilerElse
;
;- Windows specific communication stuff (using WM_COPYDATA)
;
Global CommunicationWindow
Global CommunicationMessage
Global TargetWindow
Global *CurrentCall.RPC_Call, CallComplete
Procedure CommunicationCallback(Window, Message, wParam, lParam)
Protected EventCall.RPC_Call
Protected *StoredCall.RPC_Call
If Window = CommunicationWindow And Message = #WM_COPYDATA And wParam = TargetWindow And *CurrentCall
*copy.COPYDATASTRUCT = lParam
If *copy And *copy\dwData = AsciiConst('A','U','T','1') And *copy\cbData > 20 And *copy\lpData
; check the actual size with the encoded data
If PeekL(*copy\lpData) <= *copy\cbData
; check if this is an event call or a response
;
If PeekL(*copy\lpData+4) = 0
; event call
If EventCallback And RPC_Decode(@EventCall, *copy\lpData, *copy\cbData)
; store the *CurrentCall pointer, so we can have nested calls from the event handler
*StoredCall = *CurrentCall
EventCallback(@EventCall)
; restore the wait for the response
*CurrentCall = *StoredCall
CallComplete = #False
EndIf
Else
; response to call
; check for a match in the responseID
If PeekL(*copy\lpData+8) = *CurrentCall\ResponseID
; match, try to decode
If RPC_Decode(*CurrentCall, *copy\lpData, *copy\cbData) = 0
RPC_InitResponse(*CurrentCall, 1, #True)
RPC_SetString(*CurrentCall, 0, "Invalid response")
EndIf
CallComplete = #True
EndIf
EndIf
EndIf
EndIf
ProcedureReturn #True
EndIf
ProcedureReturn DefWindowProc_(Window, Message, wParam, lParam)
EndProcedure
Procedure SendRequest(*Call.RPC_Call)
Success = 0
CompilerIf #DEBUG
RPC_DebugCall(*Call, "Sending request")
CompilerEndIf
If CommunicationWindow And TargetWindow And RPC_Encode(*Call)
; prepare for receiving the response, as it can happen even while
; we are in the SendMessage_() below (makes sense, as the IDE sends the response right then)
CallComplete = #False
*CurrentCall = *Call
; Send the data
;
request.COPYDATASTRUCT\dwData = AsciiConst('A','U','T','1')
request\cbData = *Call\EncodedSize
request\lpData = *Call\Encoded
SendMessage_(TargetWindow, #WM_COPYDATA, CommunicationWindow, @request)
; The SendMessage_() does not return until the IDE has processed the call, and any responses
; are already sent. So we need no message loop here to wait for a response
If CallComplete
; *Call is already decoded in the callback, so all is fine
Success = 1
Else
RPC_InitResponse(*Call, 1, #True)
RPC_SetString(*Call, 0, "Communication timeout")
EndIf
Else
RPC_InitResponse(*Call, 1, #True)
RPC_SetString(*Call, 0, "Out of resources")
EndIf
CompilerIf #DEBUG
RPC_DebugCall(*Call, "Received response")
CompilerEndIf
If *Call\ErrorFlag
LastError$ = RPC_GetString(*Call, 0)
Success = 0
EndIf
ProcedureReturn Success
EndProcedure
Procedure WaitEvents(Timeout)
StartTime.q = ElapsedMilliseconds()
; This message loop will not interfere with PB WaitWindowEvent(), as events are now also
; stored by the PB event lib on Windows (so we do not miss any events here)
; also we only get events for our own communication window anyway
;
If CommunicationWindow
If Timeout <= 0
If PeekMessage_(@msg.MSG, CommunicationWindow, 0, 0, #PM_REMOVE)
TranslateMessage_(@msg)
DispatchMessage_(@msg)
EndIf
Else
While ElapsedMilliseconds()-StartTime < Timeout
If PeekMessage_(@msg.MSG, CommunicationWindow, 0, 0, #PM_REMOVE)
TranslateMessage_(@msg)
DispatchMessage_(@msg)
Else
Delay(50)
EndIf
Wend
EndIf
EndIf
EndProcedure
Procedure Connect(WindowID, ProcessID, Executable$)
Success = 0
TargetWindow = 0
If CommunicationWindow
;
; Discover all IDE instances that support Automation
;
If WindowID
; post to this window only
PostMessage_(WindowID, CommunicationMessage, AsciiConst('A','U','T','O'), CommunicationWindow)
Else
PostMessage_(#HWND_BROADCAST, CommunicationMessage, AsciiConst('A','U','T','O'), CommunicationWindow)
EndIf
If Executable$
; make path absolute
Executable$ = ResolveRelativePath(GetCurrentDirectory(), Executable$)
EndIf
Timeout.q = ElapsedMilliseconds()
Repeat
If PeekMessage_(@msg.MSG, CommunicationWindow, 0, 0, #PM_REMOVE)
TranslateMessage_(@msg)
DispatchMessage_(@msg)
; The connection communication is done (at least the response) with PostMessage,
; so we do not need code in the callback and can process it here
;
If msg\hwnd = CommunicationWindow And msg\message = CommunicationMessage
CompilerIf #DEBUG
Debug "[RunOnceMessage received] Code = '" + PeekS(@msg\wParam, 4, #PB_Ascii) + "', Window = " + Hex(msg\lParam)
CompilerEndIf
If msg\wParam = AsciiConst('A','U','T','1')
; response from an IDE that supports this
If WindowID
; if a WindowID was given, we are done at this point
If WindowID = msg\lParam
TargetWindow = msg\lParam
Success = 1
EndIf
ElseIf ProcessID
; find out the ProcessID of this window
If GetWindowThreadProcessId_(msg\lParam, @dwProcessID.l) And dwProcessID = ProcessID
TargetWindow = msg\lParam
Success = 1
EndIf
ElseIf Executable$
; need to ask the IDE if the executable matches
; response is then an 'AOK' message
copy.COPYDATASTRUCT\cbData = StringByteLength(Executable$, #PB_UTF8) + 1
copy\lpData = AllocateMemory(copy\cbData)
If copy\lpData
PokeS(copy\lpData, Executable$, -1, #PB_UTF8)
copy\dwData = AsciiConst('A','E','X','E')
SendMessage_(msg\lParam, #WM_COPYDATA, CommunicationWindow, @copy)
FreeMemory(copy\lpData)
EndIf
Else
; ConnectAny, accept the first response
TargetWindow = msg\lParam
Success = 1
EndIf
ElseIf msg\wParam = AsciiConst(0, 'A','O','K')
; response from an IDE with the correct Executable$
TargetWindow = msg\lParam
Success = 1
EndIf
EndIf
Else
Delay(50)
EndIf
Until Success Or ElapsedMilliseconds()-Timeout > 5000
EndIf
If Success = 0
LastError$ = "Connection could not be established."
EndIf
ProcedureReturn Success
EndProcedure
Procedure Disconnect()
; nothing to do here currently on Windows
TargetWindow = 0
EndProcedure
ProcedureDLL AttachProcess(Instance)
;
; Create our communication window and class
;
Class.WNDCLASS
Class\hbrBackground = #COLOR_BTNFACE+1
Class\hInstance = Instance
Class\lpszClassName = @"PB_AutomationClient"
Class\lpfnWndProc = @CommunicationCallback()
RegisterClass_(@Class)
CommunicationMessage = RegisterWindowMessage_(#ProductName$+"_RunOnce") ; use the IDE runonce message for this too
If OSVersion() >= #PB_OS_Windows_2000
; create a message only window
ParentWindow = -3 ; HWND_MESSAGE
Else
ParentWindow = #Null
EndIf
CommunicationWindow = CreateWindowEx_(0, @"PB_AutomationClient", @"", 0, 0, 0, 0, 0, ParentWindow, 0, Instance, #Null)
EndProcedure
ProcedureDLL DetachProcess(Instance)
;
; Clean up
;
CloseWindow_(CommunicationWindow)
UnregisterClass_(@"PB_AutomationClient", Instance)
EndProcedure
CompilerEndIf
;- Connecting/Disconnecting
;
ProcedureDLL AUTO_ConnectToWindow(WindowID)
ProcedureReturn Connect(WindowID, 0, "")
EndProcedure
ProcedureDLL AUTO_ConnectToProcess(ProcessID)
ProcedureReturn Connect(0, ProcessID, "")
EndProcedure
ProcedureDLL AUTO_ConnectToProgram(Executable$)
ProcedureReturn Connect(0, 0, Executable$)
EndProcedure
ProcedureDLL AUTO_ConnectToAny()
ProcedureReturn Connect(0, 0, "")
EndProcedure
ProcedureDLL AUTO_ConnectFromTool()
MainWindow$ = GetEnvironmentVariable("PB_TOOL_MainWindow")
If MainWindow$
ProcedureReturn Connect(Val(MainWindow$), 0, "")
Else
LastError$ = "IDE Tool information not found"
ProcedureReturn 0
EndIf
EndProcedure
ProcedureDLL AUTO_Disconnect()
Disconnect()
EndProcedure
;- Error handling
;
ProcedureDLL AUTO_ClearError()
LastError$ = ""
EndProcedure
ProcedureDLL AUTO_LastErrorPtr()
ProcedureReturn @LastError$
EndProcedure
;- Event handling
;
ProcedureDLL AUTO_RPC_SetCallback(Callback.EventCallback)
EventCallback = Callback
EndProcedure
ProcedureDLL AUTO_RPC_ProcessEvents(Timeout)
WaitEvents(Timeout)
EndProcedure
;- RPC wrapper
;
; We add a wrapper around this, because of the DLL boundary (need to handle strings right),
; and also so the caller process does not know the contents of the *Call structure
; for future compatibility
;
ProcedureDLL AUTO_RPC_NewCall(Function$, NbParameters)
*Call.RPC_Call = AllocateMemory(SizeOf(RPC_Call))
If *Call
InitializeStructure(*Call, RPC_Call)
RPC_InitCall(*Call, Function$, NbParameters)
EndIf
ProcedureReturn *Call
EndProcedure
ProcedureDLL AUTO_RPC_FreeCall(*Call.RPC_Call)
If *Call
RPC_ClearCall(*Call)
FreeMemory(*Call)
EndIf
EndProcedure
ProcedureDLL AUTO_RPC_CallResponse(*Call.RPC_Call, NbParameters)
If *Call
RPC_InitResponse(*Call, NbParameters, #False)
EndIf
EndProcedure
ProcedureDLL AUTO_RPC_CallError(*Call.RPC_Call, Message$)
If *Call
RPC_InitResponse(*Call, 1, #True)
RPC_SetString(*Call, 0, Message$)
EndIf
EndProcedure
ProcedureDLL AUTO_RPC_SendCall(*Call.RPC_Call)
If *Call
ProcedureReturn SendRequest(*Call)
Else
LastError$ = "Invalid call."
ProcedureReturn 0
EndIf
EndProcedure
;- RPC Get/Set functions
;
ProcedureDLL AUTO_RPC_SetLong(*Call.RPC_Call, Index, Value.l)
If *Call
RPC_SetLong(*Call, Index, Value)
EndIf
EndProcedure
ProcedureDLL AUTO_RPC_SetQuad(*Call.RPC_Call, Index, Value.q)
If *Call
RPC_SetQuad(*Call, Index, Value)
EndIf
EndProcedure
ProcedureDLL AUTO_RPC_SetString(*Call.RPC_Call, Index, Value$)
If *Call
RPC_SetString(*Call, Index, Value$)
EndIf
EndProcedure
ProcedureDLL AUTO_RPC_SetMemory(*Call.RPC_Call, Index, *Buffer, Size)
If *Call
RPC_SetMemory(*Call, Index, *Buffer, Size)
EndIf
EndProcedure
ProcedureDLL.l AUTO_RPC_GetLong(*Call.RPC_Call, Index)
If *Call
ProcedureReturn RPC_GetLong(*Call, Index)
Else
ProcedureReturn 0
EndIf
EndProcedure
ProcedureDLL.q AUTO_RPC_GetQuad(*Call.RPC_Call, Index)
If *Call
ProcedureReturn RPC_GetQuad(*Call, Index)
Else
ProcedureReturn 0
EndIf
EndProcedure
ProcedureDLL AUTO_RPC_GetStringPtr(*Call.RPC_Call, Index)
If *Call And Index < *Call\NbParameters And *Call\Parameter(Index)\Type = #PB_String
; return only the pointer here, so the caller can PeekS it
ProcedureReturn @*Call\Parameter(Index)\String$
Else
ProcedureReturn @""
EndIf
EndProcedure
ProcedureDLL AUTO_RPC_GetMemorySize(*Call.RPC_Call, Index)
If *Call
ProcedureReturn RPC_GetMemorySize(*Call, Index)
Else
ProcedureReturn 0
EndIf
EndProcedure
ProcedureDLL AUTO_RPC_GetMemory(*Call.RPC_Call, Index)
If *Call
ProcedureReturn RPC_GetMemory(*Call, Index)
Else
ProcedureReturn 0
EndIf
EndProcedure
ProcedureDLL AUTO_RPC_CountParameters(*Call.RPC_Call)
If *Call
ProcedureReturn *Call\NbParameters
Else
ProcedureReturn 0
EndIf
EndProcedure
ProcedureDLL AUTO_RPC_GetFunctionPtr(*Call.RPC_Call)
If *Call
ProcedureReturn @*Call\Function$
Else
ProcedureReturn @""
EndIf
EndProcedure