-
Notifications
You must be signed in to change notification settings - Fork 0
/
DigIt_Bridge_Impl.pas
799 lines (624 loc) · 18.5 KB
/
DigIt_Bridge_Impl.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
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
(*******************************************************************************
** DigIt **
** **
** (s) 2024 Massimo Magnano **
** **
********************************************************************************
** Bridge Engine Implementation **
*******************************************************************************)
unit Digit_Bridge_Impl;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Digit_Bridge_Intf, MM_OpenArrayList;
type
TPluginInfo = record
Info: TDigIt_PluginInfo;
LibFile: String;
LibHandle: TLibHandle; //If =0 then InitProc and ReleaseProc refer to statically linked procedures
InfoProc: TDigIt_PluginInfoProc;
InitProc,
ReleaseProc: TDigIt_PluginInitProc;
end;
{ TDigIt_Plugins }
TDigIt_Plugins = class(TObject)
protected
rPluginsList: array of TPluginInfo;
function Find(const aName: String): Integer;
function GetPlugin(const aName: String): TPluginInfo; overload;
function GetPlugin(Index : Integer): TPluginInfo; overload;
function GetPluginName(Index: Integer): String;
function GetCount: Integer;
function FreeElement(Index: Integer): Boolean;
public
constructor Create;
destructor Destroy; override;
function Register(const aFileName: String): Boolean; overload;
function Register(const aPluginInfo: TPluginInfo): Boolean; overload;
function UnRegister(const aName: String): Boolean;
function RegisterInPath(aPath: String): Integer;
property Count: Integer read GetCount;
property PluginByName [const aName: String]: TPluginInfo read GetPlugin;
property Plugin [const aIndex: Integer]: TPluginInfo read GetPlugin;
property Name [const aIndex: Integer]: String read GetPluginName;
end;
{ TDigIt_Sources }
TSourceInfo = record
Flags: DWord;
Inst: IDigIt_Source;
end;
PSourceInfo = ^TSourceInfo;
TDigIt_Sources = class(specialize TOpenArrayList<TSourceInfo, String>, IDigIt_Sources)
function Register(const aName: PChar; const aClass: IDigIt_Source): Boolean; stdcall;
protected
rSelected: PSourceInfo;
rSelectedName: String;
rSelectedIndex: Integer;
rSelectedParams: IDigIt_Params;
function FreeElement(var aData: TSourceInfo): Boolean; override;
public
constructor Create;
function Select(SourceName: String; GetUserParams: Boolean=False): Boolean; overload;
function Select(SourceIndex: Integer; GetUserParams: Boolean=False): Boolean; overload;
function LoadSelectedParams(XMLFileName, XMLPath: String): Boolean;
property Selected: PSourceInfo read rSelected;
property SelectedIndex: Integer read rSelectedIndex;
property SelectedName: String read rSelectedName;
property SelectedParams: IDigIt_Params read rSelectedParams;
end;
{ #note -oMaxM : Not enabled for now until I figure out how to pass the image data and make the thumbnails }
(*
{ TDigIt_Destinations }
TDestinationInfo = record
Flags: DWord;
Inst: IDigIt_Destination;
end;
PDestinationInfo = ^TDestinationInfo;
TDigIt_Destinations = class(specialize TOpenArrayList<TDestinationInfo, String>, IDigIt_Destinations)
function Register(const aName: PChar; const aClass: IDigIt_Destination): Boolean; stdcall;
protected
rSelected: PDestinationInfo;
rSelectedName: String;
rSelectedIndex: Integer;
rSelectedParams: IDigIt_Params;
function FreeElement(var aData: TDestinationInfo): Boolean; override;
public
constructor Create;
function Select(DestinationName: String; GetUserParams: Boolean=False): Boolean; overload;
function Select(DestinationIndex: Integer; GetUserParams: Boolean=False): Boolean; overload;
function LoadSelectedParams(XMLFileName, XMLPath: String): Boolean;
property Selected: PDestinationInfo read rSelected;
property SelectedIndex: Integer read rSelectedIndex;
property SelectedName: String read rSelectedName;
property SelectedParams: IDigIt_Params read rSelectedParams;
end;
*)
{ TDigIt_Settings }
TDigIt_Settings = class(TNoRefCountObject, IDigIt_Settings)
public
constructor Create;
destructor Destroy; override;
//Path consts
function Path_Temp: PChar; stdcall;
function Path_Config: PChar; stdcall;
function Path_Application: PChar; stdcall;
end;
{ TDigIt_Bridge }
TDigIt_Bridge = class(TNoRefCountObject, IDigIt_Bridge)
protected
rSettings: TDigIt_Settings;
rSources: TDigIt_Sources;
//rDestinations: TDigIt_Destinations;
rPlugins: TDigIt_Plugins;
public
constructor Create;
destructor Destroy; override;
function Sources: IDigIt_Sources; stdcall;
//function Destinations: IDigIt_Destinations; stdcall;
function Settings: IDigIt_Settings; stdcall;
function Progress: IDigIt_Progress; stdcall;
//Internal Use only
function SourcesImpl: TDigIt_Sources;
//function DestinationsImpl: TDigIt_Destinations;
function SettingsImpl: TDigIt_Settings;
function Plugins: TDigIt_Plugins;
end;
var
theBridge: TDigIt_Bridge = nil;
implementation
uses dynlibs, Masks, DigIt_Types, DigIt_Form_Progress;
{ TDigIt_Plugins }
function TDigIt_Plugins.Find(const aName: String): Integer;
var
i : Integer;
begin
Result:= -1;
for i:=0 to Length(rPluginsList)-1 do
if (rPluginsList[i].Info.Name = aName) then
begin
Result:= i; break;
end;
end;
constructor TDigIt_Plugins.Create;
begin
inherited Create;
rPluginsList:= Nil;
end;
destructor TDigIt_Plugins.Destroy;
var
i:Integer;
res: Boolean;
begin
for i:=0 to Length(rPluginsList)-1 do
begin
try
res:= FreeElement(i);
except
end;
end;
try
rPluginsList:= Nil;
except
end;
inherited Destroy;
end;
function TDigIt_Plugins.Register(const aFileName: String): Boolean;
var
newPlugin: TPluginInfo;
res: Boolean;
begin
Result:= False;
try
with newPlugin do
begin
LibFile:= aFileName;
LibHandle:= LoadLibrary(aFileName);
if not(LibHandle = dynlibs.NilHandle) then
begin
InitProc:= TDigIt_PluginInitProc(GetProcedureAddress(LibHandle, DigIt_PluginInitProcName));
//Plugin MUST Have InitProc
if Assigned(InitProc) then
begin
ReleaseProc:= TDigIt_PluginInitProc(GetProcedureAddress(LibHandle, DigIt_PluginReleaseProcName));
InfoProc :=TDigIt_PluginInfoProc(GetProcedureAddress(LibHandle, DigIt_PluginInfoProcName));
res:= Assigned(InfoProc);
if res
then res:= InfoProc(Info);
if not(res)
then Info.Name:= ExtractFileName(aFileName);
Result:= Register(newPlugin);
end;
if not(Result)
then FreeLibrary(newPlugin.LibHandle);
end;
end;
except
if not(newPlugin.LibHandle = dynlibs.NilHandle)
then FreeLibrary(newPlugin.LibHandle);
Result:= False;
end;
end;
function TDigIt_Plugins.Register(const aPluginInfo: TPluginInfo): Boolean;
begin
Result:= False;
if (Find(aPluginInfo.Info.Name) = -1) then
begin
SetLength(rPluginsList, Length(rPluginsList)+1);
rPluginsList[Length(rPluginsList)-1]:= aPluginInfo;
if Assigned(aPluginInfo.InitProc)
then Result:= aPluginInfo.InitProc(theBridge);
end;
end;
function TDigIt_Plugins.UnRegister(const aName: String): Boolean;
var
r : Integer;
res: Boolean;
begin
Result:= False;
r:= Find(aName);
if (r > -1) then
begin
Result:= FreeElement(r);
Delete(rPluginsList, r, 1);
Result:= True;
end;
end;
function TDigIt_Plugins.RegisterInPath(aPath: String): Integer;
var
fileInfo: TSearchRec;
err: Integer;
theCaption,
theExt: String;
IsDir: Boolean;
begin
Result:= 0;
//if Last char is Separator, Delete it
if (aPath[Length(aPath)] in AllowDirectorySeparators)
then SetLength(aPath, Length(aPath)-1);
if DirectoryExists(aPath) then
try
err:= FindFirst(aPath+DirectorySeparator+'*', faAnyFile, fileInfo);
while (err = 0) do
begin
if (fileInfo.Name[1] <> '.') then //not [.] or [..]
begin
theCaption :=ExtractFileName(fileInfo.Name);
theExt :=ExtractFileExt(fileInfo.Name);
IsDir :=((fileInfo.Attr and faDirectory)<>0);
if not(IsDir) and MatchesMask(fileInfo.Name, '*.'+SharedSuffix)
then if Register(aPath+DirectorySeparator+fileInfo.Name)
then Inc(Result);
end;
err :=FindNext(fileInfo);
end;
finally
FindClose(fileInfo);
end;
end;
function TDigIt_Plugins.GetPlugin(const aName: String): TPluginInfo;
var
i: Integer;
begin
{ #note 2 -oMaxM : Return a Copy or a Pointer to List element? }
FillChar(Result, SizeOf(TPluginInfo), 0);
for i:=0 to Length(rPluginsList)-1 do
if (rPluginsList[i].Info.Name = aName) then
begin
Result:= rPluginsList[i]; break;
end;
end;
function TDigIt_Plugins.GetPlugin(Index: Integer): TPluginInfo;
begin
{ #note 2 -oMaxM : Return a Copy or a Pointer to List element? }
FillChar(Result, SizeOf(TPluginInfo), 0);
if (Index >= 0) and (Index < Length(rPluginsList))
then Result:= rPluginsList[Index];
end;
function TDigIt_Plugins.GetPluginName(Index: Integer): String;
begin
if (Index >= 0) and (Index < Length(rPluginsList))
then Result:= rPluginsList[Index].Info.Name
else Result:= '';
end;
function TDigIt_Plugins.GetCount: Integer;
begin
Result:= Length(rPluginsList);
end;
function TDigIt_Plugins.FreeElement(Index: Integer): Boolean;
begin
Result:= False;
if Assigned(rPluginsList[Index].ReleaseProc)
then Result:= rPluginsList[Index].ReleaseProc(theBridge);
if not(rPluginsList[Index].LibHandle = dynlibs.NilHandle)
then Result:= FreeLibrary(rPluginsList[Index].LibHandle);
end;
{ TDigIt_Sources }
function TDigIt_Sources.FreeElement(var aData: TSourceInfo): Boolean;
begin
Result:= False;
try
if (aData.Inst <> nil)
then aData.Inst.Release;
Result:= True;
finally
//MaxM: When the open array is freed the compiler frees the contents of the record using rtti,
// a very dangerous thing for us.
FillChar(aData, SizeOf(aData), 0);
end;
end;
constructor TDigIt_Sources.Create;
begin
inherited Create;
rSelected:= nil;
rSelectedName:= '';
rSelectedIndex:= -1;
rSelectedParams:= nil;
end;
function TDigIt_Sources.Select(SourceName: String; GetUserParams: Boolean): Boolean;
var
newSourceI: Integer;
newSource: PSourceInfo =nil;
begin
Result:= False;
if (SourceName <> '') then
try
newSourceI:= FindByKey(SourceName);
newSource:= Data[newSourceI];
if (newSource <> nil) then
begin
if GetUserParams and
not((newSource^.Inst.Params = nil) or newSource^.Inst.Params.GetFromUser)
then exit;
rSelected:= newSource;
rSelectedParams :=newSource^.Inst.Params;
rSelectedName:= SourceName;
rSelectedIndex:= newSourceI;
Result:= True;
end;
except
Result:= False;
end;
end;
function TDigIt_Sources.Select(SourceIndex: Integer; GetUserParams: Boolean): Boolean;
var
newSource: PSourceInfo =nil;
begin
Result:= False;
try
newSource:= Data[SourceIndex];
if (newSource <> nil) then
begin
if GetUserParams and
not((newSource^.Inst.Params = nil) or newSource^.Inst.Params.GetFromUser)
then exit;
rSelected:= newSource;
rSelectedParams :=newSource^.Inst.Params;
rSelectedName:= rList[SourceIndex].Key;
rSelectedIndex:= SourceIndex;
Result:= True;
end;
except
Result:= False;
end;
end;
function TDigIt_Sources.LoadSelectedParams(XMLFileName, XMLPath: String): Boolean;
begin
Result:= False;
if (rSelectedParams <> nil) and (XMLFileName <> '') and (XMLPath <> '') then
try
Result:= rSelectedParams.Load(PChar(XMLFileName), PChar(XMLPath));
if Result then Result:= rSelectedParams.OnSet;
except
Result:= False;
end;
end;
function TDigIt_Sources.Register(const aName: PChar; const aClass: IDigIt_Source): Boolean; stdcall;
var
newData: TSourceInfo;
begin
Result:= False;
if (aClass = nil) then exit;
//If the Class cannot Init don't register it and Release
if (aClass.Init)
then begin
newData.Inst:= aClass;
Result:= (Add(aName, newData) > -1);
end
else aClass.Release;
end;
(*
function TDigIt_Sources.UnRegister(const aName: String): Boolean;
var
r : Integer;
begin
Result:= False;
r:= Find(aName);
if (r > -1) then
begin
{ #todo 10 -oMaxM : Free the Instances? }
Result:= FreeElement(r);
Delete(rSourcesList, r, 1);
Result:= True;
end;
end;
function TDigIt_Sources.UnRegister(const aClass: IDigIt_Source): Boolean;
var
r : Integer;
begin
Result:= False;
r:= Find(aClass);
if (r > -1) then
begin
{ #todo 10 -oMaxM : Free the Instances? }
Result:= FreeElement(r);
Delete(rSourcesList, r, 1);
Result:= True;
end;
end;
*)
{ #note -oMaxM : Not enabled for now until I figure out how to pass the image data and make the thumbnails }
(*
{ TDigIt_Destinations }
function TDigIt_Destinations.FreeElement(var aData: TDestinationInfo): Boolean;
begin
Result:= False;
try
if (aData.Inst <> nil)
then aData.Inst.Release;
Result:= True;
finally
//MaxM: When the open array is freed the compiler frees the contents of the record using rtti,
// a very dangerous thing for us.
FillChar(aData, SizeOf(aData), 0);
end;
end;
constructor TDigIt_Destinations.Create;
begin
inherited Create;
rSelected:= nil;
rSelectedName:= '';
rSelectedIndex:= -1;
rSelectedParams:= nil;
end;
function TDigIt_Destinations.Select(DestinationName: String; GetUserParams: Boolean): Boolean;
var
newDestinationI: Integer;
newDestination: PDestinationInfo =nil;
begin
Result:= False;
if (DestinationName <> '') then
try
newDestinationI:= FindByKey(DestinationName);
newDestination:= Data[newDestinationI];
if (newDestination <> nil) then
begin
if GetUserParams and
not((newDestination^.Inst.Params = nil) or newDestination^.Inst.Params.GetFromUser)
then exit;
rSelected:= newDestination;
rSelectedParams :=newDestination^.Inst.Params;
rSelectedName:= DestinationName;
rSelectedIndex:= newDestinationI;
Result:= True;
end;
except
Result:= False;
end;
end;
function TDigIt_Destinations.Select(DestinationIndex: Integer; GetUserParams: Boolean): Boolean;
var
newDestination: PDestinationInfo =nil;
begin
Result:= False;
try
newDestination:= Data[DestinationIndex];
if (newDestination <> nil) then
begin
if GetUserParams and
not((newDestination^.Inst.Params = nil) or newDestination^.Inst.Params.GetFromUser)
then exit;
rSelected:= newDestination;
rSelectedParams :=newDestination^.Inst.Params;
rSelectedName:= rList[DestinationIndex].Key;
rSelectedIndex:= DestinationIndex;
Result:= True;
end;
except
Result:= False;
end;
end;
function TDigIt_Destinations.LoadSelectedParams(XMLFileName, XMLPath: String): Boolean;
begin
Result:= False;
if (rSelectedParams <> nil) and (XMLFileName <> '') and (XMLPath <> '') then
try
Result:= rSelectedParams.Load(PChar(XMLFileName), PChar(XMLPath));
if Result then Result:= rSelectedParams.OnSet;
except
Result:= False;
end;
end;
function TDigIt_Destinations.Register(const aName: PChar; const aClass: IDigIt_Destination): Boolean; stdcall;
var
newData: TDestinationInfo;
begin
Result:= False;
if (aClass = nil) then exit;
//If the Class cannot Init don't register it and Release
if (aClass.Init)
then begin
newData.Inst:= aClass;
Result:= (Add(aName, newData) > -1);
end
else aClass.Release;
end;
(*
function TDigIt_Destinations.UnRegister(const aName: String): Boolean;
var
r : Integer;
begin
Result:= False;
r:= Find(aName);
if (r > -1) then
begin
{ #todo 10 -oMaxM : Free the Instances? }
Result:= FreeElement(r);
Delete(rDestinationsList, r, 1);
Result:= True;
end;
end;
function TDigIt_Destinations.UnRegister(const aClass: IDigIt_Destination): Boolean;
var
r : Integer;
begin
Result:= False;
r:= Find(aClass);
if (r > -1) then
begin
{ #todo 10 -oMaxM : Free the Instances? }
Result:= FreeElement(r);
Delete(rDestinationsList, r, 1);
Result:= True;
end;
end;
*)
*)
{ TDigIt_Settings }
constructor TDigIt_Settings.Create;
begin
inherited Create;
end;
destructor TDigIt_Settings.Destroy;
begin
inherited Destroy;
end;
function TDigIt_Settings.Path_Temp: PChar; stdcall;
begin
Result:= PChar(DigIt_Types.Path_Temp); { #note 10 -oMaxM : Test internal plugin and LIBRARY }
end;
function TDigIt_Settings.Path_Config: PChar; stdcall;
begin
Result:= PChar(DigIt_Types.Path_Config); { #note 10 -oMaxM : Test internal plugin and LIBRARY }
end;
function TDigIt_Settings.Path_Application: PChar; stdcall;
begin
Result:= PChar(DigIt_Types.Path_Application); { #note 10 -oMaxM : Test internal plugin and LIBRARY }
end;
{ TDigIt_Bridge }
constructor TDigIt_Bridge.Create;
begin
inherited Create;
rPlugins:= TDigIt_Plugins.Create;
rSettings:= TDigIt_Settings.Create;
rSources:= TDigIt_Sources.Create;
//rDestinations:= TDigIt_Destinations.Create;
end;
destructor TDigIt_Bridge.Destroy;
begin
rSources.Free;
rSettings.Free;
rPlugins.Free;
//rDestinations.free;
inherited Destroy;
end;
function TDigIt_Bridge.Sources: IDigIt_Sources; stdcall;
begin
Result:= rSources as IDigIt_Sources;
end;
(*
function TDigIt_Bridge.Destinations: IDigIt_Destinations; stdcall;
begin
Result:= rDestinations as IDigIt_Destinations;
end;
*)
function TDigIt_Bridge.Settings: IDigIt_Settings; stdcall;
begin
Result:= rSettings as IDigIt_Settings;
end;
function TDigIt_Bridge.Progress: IDigIt_Progress; stdcall;
begin
Result:= DigIt_Progress as IDigIt_Progress;
end;
function TDigIt_Bridge.SourcesImpl: TDigIt_Sources;
begin
Result:= rSources;
end;
(*
function TDigIt_Bridge.DestinationsImpl: TDigIt_Destinations;
begin
Result:= rDestinations;
end;
*)
function TDigIt_Bridge.SettingsImpl: TDigIt_Settings;
begin
Result:= rSettings;
end;
function TDigIt_Bridge.Plugins: TDigIt_Plugins;
begin
Result:= rPlugins;
end;
initialization
theBridge:= TDigIt_Bridge.Create;
finalization
//theBridge.Free; { #note -oMaxM : MainForm Free the Bridge otherwise the TwainSource does not send the message STOP to 32 bit Comm }
end.