-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpu_main.pas
1289 lines (1214 loc) · 37.8 KB
/
pu_main.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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit pu_main;
{$mode objfpc}{$H+}
{
Copyright (C) 2015 Patrick Chevalley
http://www.ap-i.net
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
}
interface
uses pu_devlist, pu_setup, u_utils, pu_indigui, XMLConf, process,
indibaseclient, indibasedevice, indiapi, indicom, UniqueInstance, UScaleDPI,
Classes, SysUtils, LazFileUtils, Forms, Controls, Graphics, Dialogs,
ComCtrls, StdCtrls, Grids, ExtCtrls, ActnList, Menus, LCLVersion, InterfaceBase;
type
{ Tf_main }
Tf_main = class(TForm)
BtnAdd: TButton;
BtnSetup: TButton;
BtnStartStop: TButton;
cbAdvanced: TCheckBox;
ClientBtn: TButton;
ConfigLabel: TLabel;
led: TImage;
ImageList1: TImageList;
LabelStatus: TLabel;
MainMenu1: TMainMenu;
MenuFile: TMenuItem;
MenuItem1: TMenuItem;
MenuHelp: TMenuItem;
MenuAbout: TMenuItem;
MenuEditName: TMenuItem;
MenuHelpOnline: TMenuItem;
MenuHelpPdf: TMenuItem;
MenuEdit: TMenuItem;
MenuSetup: TMenuItem;
MenuRestartDevice: TMenuItem;
MenuStopDevice: TMenuItem;
MenuDeleteDevice: TMenuItem;
MenuRestartServer: TMenuItem;
MenuStopServer: TMenuItem;
MenuQuit: TMenuItem;
Panel1: TPanel;
PopupMenu1: TPopupMenu;
StringGrid1: TStringGrid;
StatusTimer: TTimer;
UniqueInstance1: TUniqueInstance;
procedure BtnAddClick(Sender: TObject);
procedure BtnStartStopClick(Sender: TObject);
procedure cbAdvancedClick(Sender: TObject);
procedure ClientBtnClick(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure MenuAboutClick(Sender: TObject);
procedure MenuDeleteDeviceClick(Sender: TObject);
procedure MenuEditNameClick(Sender: TObject);
procedure MenuHelpOnlineClick(Sender: TObject);
procedure MenuHelpPdfClick(Sender: TObject);
procedure MenuSetupClick(Sender: TObject);
procedure MenuQuitClick(Sender: TObject);
procedure MenuRestartDeviceClick(Sender: TObject);
procedure MenuRestartServerClick(Sender: TObject);
procedure MenuStopDeviceClick(Sender: TObject);
procedure MenuStopServerClick(Sender: TObject);
procedure PopupMenu1Popup(Sender: TObject);
procedure StringGrid1CheckboxToggled(sender: TObject; aCol, aRow: Integer; aState: TCheckboxState);
procedure StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
aRect: TRect; aState: TGridDrawState);
procedure StringGrid1EditingDone(Sender: TObject);
procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure StatusTimerTimer(Sender: TObject);
procedure StringGrid1SelectEditor(Sender: TObject; aCol, aRow: Integer; var Editor: TWinControl);
procedure UniqueInstance1OtherInstance(Sender: TObject; ParamCount: Integer; const Parameters: array of String);
private
{ private declarations }
f_indigui: Tf_indigui;
indiclient: TIndiBaseClient;
ActiveDevLst,ActiveExecLst: TStringList;
TunnelProcess: TProcess;
rc,config: TXMLConfig;
ConfigDir,configfile,devlist,serveroptions,serverlog: string;
RemoteHost,RemoteSshPort,RemoteUser,LocalPort,RemotePort,sshopt: string;
Initialized,autostart,moreoptions,stayontop,remote,ServerStarted: boolean;
GUIready: boolean;
ServerFifo: string;
CurrentCol, CurrentRow: integer;
AutoConnectList: string;
Procedure GetAppDir;
procedure LoadConfig(cname:string);
procedure ClearGrid;
procedure SetTitle;
procedure SaveConfig;
function WriteCmd(cmd:string): boolean;
procedure ShowErr(msg:string; str:TStringList);
procedure CheckDuplicateDevice(dev: Tdevicenode);
procedure EditDeviceName(r: integer);
procedure StartDevice(r:integer);
procedure StopDevice(r:integer);
procedure StartServer;
procedure StopServer;
procedure StartTunnel;
procedure StopTunnel;
function ServerPid: string;
procedure Status;
function GetServerPort: string;
procedure GUIdestroy(Sender: TObject);
procedure SetupConfigChange(Sender: TObject);
procedure GetIndiDevices;
procedure IndiNewProperty(indiProp: IndiProperty);
procedure IndiDeleteDevice(dp: Basedevice);
function CheckDevList(fn:string):boolean;
procedure SetAutoConnect;
public
{ public declarations }
end;
var
f_main: Tf_main;
compile_time, compile_version, compile_system, lclver: string;
const
colactive=0; colgroup=1; colname=2; colconnect=3; coldriver=4;
implementation
{$if (lcl_fullversion >= 1070000)}
uses lclplatformdef;
{$endif}
{$i revision.inc}
{$R *.lfm}
{$ifdef unix}
procedure RecvSignal(sig: longint); cdecl;
begin
case sig of
1: begin
f_main.WindowState:=wsNormal;
end;
15: begin
f_main.StopServer;
f_main.Close;
end;
end;
end;
{$endif}
{ Tf_main }
procedure Tf_main.FormCreate(Sender: TObject);
begin
DefaultFormatSettings.DecimalSeparator:='.';
DefaultFormatSettings.TimeSeparator:=':';
Initialized:=false;
ServerStarted:=false;
lclver:=lcl_version;
compile_time:={$I %DATE%}+' '+{$I %TIME%};
compile_version:='Lazarus '+lcl_version+' Free Pascal '+{$I %FPCVERSION%}+' '+{$I %FPCTARGETOS%}+'-'+{$I %FPCTARGETCPU%}+'-'+LCLPlatformDirNames[WidgetSet.LCLPlatform];
compile_system:={$I %FPCTARGETOS%};
ActiveDevLst:=TStringList.Create;
ActiveExecLst:=TStringList.Create;
sshopt:=' -oBatchMode=yes -oConnectTimeout=10 ';
ServerFifo:=slash(GetTempDir(true))+'IndiStarter.fifo';
ClientBtn.Enabled:=false;
autostart:=false;
moreoptions:=false;
serveroptions:='';
serverlog:='';
remote:=false;
RemoteHost:='';
RemoteSshPort:='22';
RemoteUser:='';
LocalPort:='7624';
RemotePort:='7624';
stayontop:=false;
ClearGrid;
GSCdir:='';
Getappdir;
ConfigExtension:= '.conf';
rc:=TXMLConfig.Create(self);
config:=TXMLConfig.Create(self);
ConfigDir:=GetAppConfigDirUTF8(false,true);
configfile:='default.conf';
devlist:=slash(ConfigDir)+'default.devices';
rc.Filename:=slash(ConfigDir)+'indistarter.rc';
configfile:=rc.GetValue('/Current/Config',configfile);
if Application.HasOption('c', 'config') then begin
configfile:=Application.GetOptionValue('c', 'config')+'.conf';
end;
UScaleDPI.UseScaling:=true;
UScaleDPI.SetScale(Canvas);
ScaleDPI(Self);
Width:=DoScaleX(380);
{$ifdef lclcocoa}
StringGrid1.FixedColor:=clBackground;
{$endif}
{$ifdef unix}
CdcSigAction(@RecvSignal);
{$endif}
end;
procedure Tf_main.FormShow(Sender: TObject);
var configautostart: boolean;
begin
if not Initialized then begin
Initialized:=true;
LoadConfig(configfile);
configautostart:=autostart;
if Application.HasOption('s', 'start') then begin
autostart:=true;
end;
if autostart then begin
StartServer;
end
else begin
try
ServerStarted:=true;
Status;
finally
ServerStarted:=false;
end;
end;
autostart:=configautostart;
end;
end;
Procedure Tf_main.GetAppDir;
var buf:string;
{$ifdef darwin}
i: integer;
{$endif}
begin
{$ifdef darwin}
Appdir:=ExtractFilePath(ParamStr(0));
i := pos('MacOS/',Appdir);
if i>0 then
Appdir:=ExtractFilePath(copy(appdir,1,i))
else
Appdir:='/Applications/IndiStarter.app/Contents';
if not FileExists(slash(Appdir)+slash('Resources')+'indistarter.pdf') then
Appdir:='/Applications/IndiStarter.app/Contents';
AppBaseDir:=StringReplace(Appdir,'/Contents','',[]);
bindir:=slash(appdir)+slash('MacOS');
Docdir:=slash(Appdir)+slash('Resources');
{$else}
Appdir:=getcurrentdir;
if not DirectoryExists(slash(Appdir)+slash('..')+slash('share')+slash('doc')+'indistarter') then begin
Appdir:=ExtractFilePath(ParamStr(0));
end;
Appdir:=expandfilename(Appdir);
// Be sur the doc directory exists
if (not directoryexists(slash(Appdir)+slash('..')+slash('share')+slash('doc')+'indistarter')) then begin
// try under the current directory
buf:=GetCurrentDir;
if (directoryexists(slash(buf)+slash('..')+slash('share')+slash('doc')+'indistarter')) then
appdir:=buf
else begin
// try under the program directory
buf:=ExtractFilePath(ParamStr(0));
if (directoryexists(slash(buf)+slash(buf)+slash('..')+slash('share')+slash('doc')+'indistarter')) then
appdir:=buf
else begin
// try in /usr
buf:=ExpandFileName(slash('/usr/bin'));
if (directoryexists(slash(buf)+slash('..')+slash('share')+slash('doc')+'indistarter')) then
appdir:=buf
else begin
// try /usr/local
buf:=ExpandFileName(slash('/usr/local/bin'));
if (directoryexists(slash(buf)+slash('..')+slash('share')+slash('doc')+'indistarter')) then
appdir:=buf
else begin
Showmessage('Error: Can''t locate the doc directory !!'+crlf+'Please try to reinstall the software');
end;
end;
end;
end;
end;
bindir:='';
Appdir:=expandfilename(Appdir);
Docdir:=slash(Appdir)+slash('..')+slash('share')+slash('doc')+'indistarter';
Docdir:=expandfilename(Docdir);
AppBaseDir:='';
{$endif}
end;
procedure Tf_main.SetAutoConnect;
var i:integer;
begin
AutoConnectList:='';
for i:=1 to StringGrid1.RowCount-1 do begin
if StringGrid1.Cells[colconnect,i]='1' then begin
AutoConnectList:=AutoConnectList+'|'+StringGrid1.Cells[colname,i]+'|';
end;
end;
if AutoConnectList<>'' then moreoptions:=true;
cbAdvanced.Checked:=moreoptions;
StringGrid1.Columns[colconnect-1].Visible:=moreoptions;
StringGrid1.Columns[coldriver-1].Visible:=moreoptions;
end;
function Tf_main.CheckDevList(fn:string): boolean;
var i,j: integer;
rows,fields: TStringList;
buf: string;
begin
result:=true;
rows:=TStringList.Create;
fields:=TStringList.Create;
try
rows.LoadFromFile(fn);
for i:=0 to rows.Count-1 do begin
SplitRec(rows[i],',',fields);
if fields.Count>4 then exit;
if fields.Count>5 then begin
result:=false;
exit;
end;
if i=0 then
fields.Insert(3,'Auto-Connect')
else
fields.Insert(3,'0');
buf:='';
for j:=0 to fields.Count-1 do
buf:=buf+fields[j]+',';
delete(buf,Length(buf),1);
rows[i]:=buf;
end;
rows.SaveToFile(fn);
finally
rows.Free;
fields.Free;
end;
end;
procedure Tf_main.LoadConfig(cname:string);
var i: integer;
begin
ClearGrid;
ConfigLabel.Caption:=ExtractFileNameOnly(cname);
configfile:=cname;
config.Filename:=slash(ConfigDir)+configfile;
devlist:=ChangeFileExt(config.Filename,'.devices');
devlist:=config.GetValue('/Devices/List',devlist);
Bindir:=config.GetValue('/Server/Bindir',Bindir);
GSCdir:=config.GetValue('/Server/GSCdir',GSCdir);
autostart:=config.GetValue('/Server/Autostart',autostart);
serveroptions:=config.GetValue('/Server/Options',serveroptions);
serverlog:=config.GetValue('/Server/Log','');
remote:=config.GetValue('/Server/Remote',remote);
moreoptions:=config.GetValue('/Server/MoreOptions',false);
cbAdvanced.Checked:=moreoptions;
RemoteHost:=config.GetValue('/RemoteServer/Host',RemoteHost);
RemoteSshPort:=config.GetValue('/RemoteServer/SshPort','22');
RemoteUser:=config.GetValue('/RemoteServer/User',RemoteUser);
LocalPort:=config.GetValue('/RemoteServer/LocalPort',LocalPort);
RemotePort:=config.GetValue('/RemoteServer/RemotePort',RemotePort);
stayontop:=config.GetValue('/Window/StayOnTop',stayontop);
Width:=config.GetValue('/Window/Width',Width);
Height:=config.GetValue('/Window/Height',Height);
FormPos(self,Left,Top);
for i:=0 to StringGrid1.Columns.Count-1 do begin
StringGrid1.Columns[i].Width:=config.GetValue('/Grid/Column'+inttostr(i)+'/Width',StringGrid1.Columns[i].Width);
end;
if FileExistsUTF8(devlist) then begin
if CheckDevList(devlist) then begin
StringGrid1.LoadFromCSVFile(devlist);
SetTitle;
SetAutoConnect;
end
else begin
ShowMessage('Invalid device list!');
ClearGrid;
end;
end
else ClearGrid;
ActiveDevLst.Clear;
ActiveExecLst.Clear;
for i:=1 to StringGrid1.RowCount-1 do begin
StringGrid1.Cells[colactive,i]:='';
end;
if stayontop then FormStyle:=fsStayOnTop else FormStyle:=fsNormal;
StatusTimer.Interval:=5000;
end;
procedure Tf_main.SaveConfig;
var i: integer;
begin
devlist:=ChangeFileExt(config.Filename,'.devices');
StringGrid1.SaveToCSVFile(devlist);
config.DeleteValue('/Devices/List');
config.SetValue('/Server/Bindir',Bindir);
config.SetValue('/Server/GSCdir',GSCdir);
config.SetValue('/Server/Autostart',autostart);
config.SetValue('/Server/Options',serveroptions);
config.SetValue('/Server/Log',serverlog);
config.SetValue('/Server/Remote',remote);
config.SetValue('/Server/MoreOptions',moreoptions);
config.SetValue('/RemoteServer/Host',RemoteHost);
config.SetValue('/RemoteServer/SshPort',RemoteSshPort);
config.SetValue('/RemoteServer/User',RemoteUser);
config.SetValue('/RemoteServer/LocalPort',LocalPort);
config.SetValue('/RemoteServer/RemotePort',RemotePort);
config.SetValue('/Window/StayOnTop',stayontop);
config.SetValue('/Window/Width',Width);
config.SetValue('/Window/Height',Height);
for i:=0 to StringGrid1.Columns.Count-1 do begin
config.SetValue('/Grid/Column'+inttostr(i)+'/Width',StringGrid1.Columns[i].Width);
end;
config.Flush;
rc.SetValue('/Current/Config',configfile);
rc.Flush;
end;
procedure Tf_main.UniqueInstance1OtherInstance(Sender: TObject; ParamCount: Integer; const Parameters: array of String);
var i: integer;
buf: string;
astart: boolean;
begin
Hide;
Show;
if (not ServerStarted) and (ParamCount>0) then begin
astart:=false;
i:=0;
while i<ParamCount do begin
if Parameters[i]='-c' then begin
inc(i);
configfile:=Parameters[i]+'.conf';
LoadConfig(configfile);
end;
if copy(Parameters[i],1,9)='--config=' then begin
buf:=Parameters[i];
delete(buf,1,9);
configfile:=buf+'.conf';
LoadConfig(configfile);
end;
if (Parameters[i]='-s')or(Parameters[i]='--start') then begin
astart:=true;
end;
inc(i);
end;
if astart then StartServer;
end;
end;
procedure Tf_main.FormDestroy(Sender: TObject);
begin
if ActiveDevLst<>nil then begin
if ServerPid<>'' then StopServer;
ActiveDevLst.Free;
ActiveExecLst.Free;
end
else
writeln('Exiting because other instance of indistarter is running.');
end;
procedure Tf_main.MenuAboutClick(Sender: TObject);
var aboutmsg,cdate: string;
begin
cdate:={$I %DATE%};
cdate:=copy(cdate,1,4);
aboutmsg:='INDI Starter '+crlf;
aboutmsg:=aboutmsg+starter_version+'-'+RevisionStr+' '+compile_time+crlf;
aboutmsg:=aboutmsg+'Compiled with:'+crlf;
aboutmsg:=aboutmsg+compile_version+crlf+crlf;
aboutmsg:=aboutmsg+'A simple program to run a INDI server'+crlf;
aboutmsg:=aboutmsg+'http://www.indilib.org'+crlf+crlf;
aboutmsg:=aboutmsg+'Copyright (C) '+cdate+' Patrick Chevalley'+crlf;
aboutmsg:=aboutmsg+'http://www.ap-i.net'+crlf+crlf;
aboutmsg:=aboutmsg+'This program is free software; you can redistribute it and/or'+crlf;
aboutmsg:=aboutmsg+'modify it under the terms of the GNU General Public License'+crlf;
aboutmsg:=aboutmsg+'as published by the Free Software Foundation; either version 3'+crlf;
aboutmsg:=aboutmsg+'of the License, or (at your option) any later version.'+crlf;
ShowMessage(aboutmsg);
end;
procedure Tf_main.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
if ServerPid='' then begin
SaveConfig;
CloseAction:=caFree;
end else begin
CloseAction:=caMinimize;
end;
end;
procedure Tf_main.ClearGrid;
begin
StringGrid1.RowCount:=1;
StringGrid1.ColWidths[0]:=23;
StringGrid1.Columns[colgroup-1].Width:=110;
StringGrid1.Columns[colname-1].Width:=156;
StringGrid1.Columns[colconnect-1].Width:=100;
StringGrid1.Columns[coldriver-1].Width:=250;
StringGrid1.Columns[colconnect-1].Visible:=moreoptions;
StringGrid1.Columns[coldriver-1].Visible:=moreoptions;
AutoConnectList:='';
SetTitle;
end;
procedure Tf_main.SetTitle;
begin
StringGrid1.Columns[colgroup-1].Title.Caption:='Group';
StringGrid1.Columns[colname-1].Title.Caption:='Driver name';
StringGrid1.Columns[colconnect-1].Title.Caption:='Auto-Connect';
StringGrid1.Columns[coldriver-1].Title.Caption:='Driver';
end;
procedure Tf_main.MenuSetupClick(Sender: TObject);
var saveconfigname: string;
savestayontop:boolean;
begin
if ServerPid='' then begin
if f_setup.Visible then exit;
SaveConfig;
saveconfigname:=configfile;
savestayontop:=stayontop;
f_setup.onConfigChange:=@SetupConfigChange;
f_setup.ConfigDir:=ConfigDir;
f_setup.config:=ExtractFileNameOnly(configfile);
f_setup.indipath.Directory:=Bindir;
f_setup.gscpath.Directory:=GSCdir;
f_setup.serveroptions.Text:=serveroptions;
f_setup.LogFileName.FileName:=serverlog;
f_setup.autostart.Checked:=autostart;
f_setup.stayontop.Checked:=stayontop;
f_setup.remote.Checked:=remote;
f_setup.remotehost.Text:=RemoteHost;
f_setup.sshport.Text:=RemoteSshPort;
f_setup.remoteuser.Text:=RemoteUser;
f_setup.localport.Text:=LocalPort;
f_setup.remoteport.Text:=RemotePort;
f_setup.PanelRemote.Visible:=remote;
FormPos(f_setup,Mouse.CursorPos.X,Mouse.CursorPos.Y);
f_setup.ShowModal;
if f_setup.ModalResult=mrOK then begin
autostart := f_setup.autostart.Checked;
serveroptions := f_setup.serveroptions.Text;
if f_setup.LogFileName.Visible then
serverlog := trim(f_setup.LogFileName.FileName)
else
serverlog := '';
Bindir := f_setup.indipath.Directory;
GSCdir := f_setup.gscpath.Directory;
remote := f_setup.remote.Checked;
RemoteHost := f_setup.remotehost.Text;
RemoteSshPort := f_setup.sshport.Text;
RemoteUser := f_setup.remoteuser.Text;
LocalPort := f_setup.localport.Text;
RemotePort := f_setup.remoteport.Text;
stayontop := f_setup.stayontop.Checked;
if (stayontop<>savestayontop) then begin
if stayontop then FormStyle:=fsStayOnTop else FormStyle:=fsNormal;
end;
SaveConfig;
end
else begin
LoadConfig(saveconfigname);
end;
end
else
ShowMessage('Stop the server before to change the setup.');
end;
procedure Tf_main.SetupConfigChange(Sender: TObject);
begin
LoadConfig(f_setup.config+'.conf');
f_setup.serveroptions.Text:=serveroptions;
f_setup.LogFileName.FileName:=serverlog;
f_setup.autostart.Checked:=autostart;
f_setup.stayontop.Checked:=stayontop;
f_setup.remote.Checked:=remote;
f_setup.remotehost.Text:=RemoteHost;
f_setup.sshport.Text:=RemoteSshPort;
f_setup.remoteuser.Text:=RemoteUser;
f_setup.localport.Text:=LocalPort;
f_setup.remoteport.Text:=RemotePort;
f_setup.PanelRemote.Visible:=remote;
end;
procedure Tf_main.MenuQuitClick(Sender: TObject);
begin
Close;
end;
procedure Tf_main.BtnStartStopClick(Sender: TObject);
begin
if BtnStartStop.Caption='Start' then begin
StartServer;
end else begin
if MessageDlg('Stop INDI server','Do you want to stop the INDI server now?',mtConfirmation,mbYesNo,0)=mrYes then
StopServer;
end;
end;
procedure Tf_main.cbAdvancedClick(Sender: TObject);
begin
moreoptions:=cbAdvanced.Checked;
SetAutoConnect;
end;
procedure Tf_main.MenuRestartServerClick(Sender: TObject);
begin
try
Screen.Cursor:=crHourGlass;
StopServer;
Wait(2);
StartServer;
finally
Screen.Cursor:=crDefault;
end;
end;
procedure Tf_main.MenuStopServerClick(Sender: TObject);
begin
StopServer;
end;
procedure Tf_main.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
aRect: TRect; aState: TGridDrawState);
begin
with Sender as TStringGrid do begin
if (aCol=colactive)and(aRow>0) then begin
Canvas.Brush.style := bssolid;
if (cells[aCol,aRow]='1')then begin
Canvas.Brush.Color := clWindow;
Canvas.FillRect(aRect);
ImageList1.Draw(Canvas,aRect.left+2,aRect.top+2,1);
end else begin
Canvas.Brush.Color := clWindow;
Canvas.FillRect(aRect);
ImageList1.Draw(Canvas,aRect.left+2,aRect.top+2,0);
end;
end;
end;
end;
procedure Tf_main.StringGrid1EditingDone(Sender: TObject);
begin
SetAutoConnect;
end;
procedure Tf_main.StringGrid1CheckboxToggled(sender: TObject; aCol, aRow: Integer; aState: TCheckboxState);
begin
SetAutoConnect;
end;
procedure Tf_main.StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var grsel:TGridRect;
begin
StringGrid1.MouseToCell(X, Y, CurrentCol, CurrentRow);
grsel.Top:=CurrentRow;
grsel.Bottom:=CurrentRow;
grsel.Left:=CurrentCol;
grsel.Right:=CurrentCol;
StringGrid1.Selection:=grsel;
end;
procedure Tf_main.PopupMenu1Popup(Sender: TObject);
begin
if ServerPid='' then begin
MenuRestartDevice.Caption:='Start server';
end
else begin
if (CurrentRow>0)and(CurrentRow<StringGrid1.RowCount)and(StringGrid1.Cells[colactive,CurrentRow]='1') then
MenuRestartDevice.Caption:='Restart device'
else
MenuRestartDevice.Caption:='Start device';
end;
end;
procedure Tf_main.StatusTimerTimer(Sender: TObject);
begin
StatusTimer.Enabled:=false;
Status;
if remote then StatusTimer.Interval:=15000 else StatusTimer.Interval:=2000;
StatusTimer.Enabled:=true;
end;
procedure Tf_main.StringGrid1SelectEditor(Sender: TObject; aCol, aRow: Integer; var Editor: TWinControl);
begin
if aCol=colconnect then
Editor:=StringGrid1.EditorByStyle(cbsCheckboxColumn)
else
Editor:=StringGrid1.EditorByStyle(cbsNone);
end;
procedure Tf_main.MenuRestartDeviceClick(Sender: TObject);
begin
try
Screen.Cursor:=crHourGlass;
if ServerPid='' then
StartServer
else
StopDevice(CurrentRow);
wait(2);
StartDevice(CurrentRow);
finally
Screen.Cursor:=crDefault;
end;
end;
procedure Tf_main.MenuStopDeviceClick(Sender: TObject);
begin
try
Screen.Cursor:=crHourGlass;
StopDevice(CurrentRow);
finally
Screen.Cursor:=crDefault;
end;
end;
procedure Tf_main.BtnAddClick(Sender: TObject);
var node: TTreeNode;
dev: Tdevicenode;
r:integer;
cdrv,chost,buf: string;
begin
f_devlist.CustomCheckBox.Checked:=false;
FormPos(f_devlist,Mouse.CursorPos.X,Mouse.CursorPos.Y);
f_devlist.ShowModal;
if (f_devlist.ModalResult=mrOK) then begin
if f_devlist.CustomCheckBox.Checked then begin
cdrv:=trim(f_devlist.CustomDriver.Text);
if cdrv<>'' then begin
chost:=trim(f_devlist.CustomHost.Text);
if chost='' then
buf:=cdrv
else
buf:='"'+cdrv+'"@'+chost;
StringGrid1.RowCount:=StringGrid1.RowCount+1;
r:=StringGrid1.RowCount-1;
StringGrid1.Cells[colactive,r]:='';
StringGrid1.Cells[colgroup,r]:='Custom';
StringGrid1.Cells[colname,r]:=cdrv;
StringGrid1.Cells[colconnect,r]:='0';
StringGrid1.Cells[coldriver,r]:=buf;
if ServerPid<>'' then StartDevice(r);
end;
end else begin
node:=f_devlist.TreeView1.Selected;
if node<>nil then begin
dev:=Tdevicenode(node.Data);
if dev<>nil then begin
CheckDuplicateDevice(dev);
StringGrid1.RowCount:=StringGrid1.RowCount+1;
r:=StringGrid1.RowCount-1;
StringGrid1.Cells[colactive,r]:='';
StringGrid1.Cells[colgroup,r]:=dev.GroupName;
StringGrid1.Cells[colname,r]:=dev.DevLbl;
StringGrid1.Cells[colconnect,r]:='0';
StringGrid1.Cells[coldriver,r]:=dev.Drv;
if ServerPid<>'' then StartDevice(r);
end;
end;
end;
StringGrid1.SaveToCSVFile(devlist);
end;
end;
procedure Tf_main.MenuDeleteDeviceClick(Sender: TObject);
var i:integer;
begin
if (StringGrid1.RowCount>1)and(CurrentRow>=1)and(CurrentRow<StringGrid1.RowCount) then begin
try
Screen.Cursor:=crHourGlass;
StopDevice(CurrentRow);
for i:=CurrentRow to StringGrid1.RowCount-2 do begin
StringGrid1.Cells[colactive,i]:=StringGrid1.Cells[colactive,i+1];
StringGrid1.Cells[colgroup,i]:=StringGrid1.Cells[colgroup,i+1];
StringGrid1.Cells[colname,i]:=StringGrid1.Cells[colname,i+1];
StringGrid1.Cells[colconnect,i]:=StringGrid1.Cells[colconnect,i+1];
StringGrid1.Cells[coldriver,i]:=StringGrid1.Cells[coldriver,i+1];
end;
StringGrid1.RowCount:=StringGrid1.RowCount-1;
StringGrid1.SaveToCSVFile(devlist);
finally
Screen.Cursor:=crDefault;
end;
end;
end;
procedure Tf_main.MenuEditNameClick(Sender: TObject);
begin
if StringGrid1.Cells[colactive,CurrentRow]<>'1' then
EditDeviceName(CurrentRow)
else
ShowMessage('The device must be stopped.');
end;
procedure Tf_main.MenuHelpOnlineClick(Sender: TObject);
begin
ExecuteFile('https://github.com/pchev/indistarter/wiki');
end;
procedure Tf_main.MenuHelpPdfClick(Sender: TObject);
var pdffn: string;
begin
pdffn:=ExpandFileNameUTF8(slash(Docdir)+'indistarter.pdf');
ExecuteFile(pdffn);
end;
procedure Tf_main.CheckDuplicateDevice(dev: Tdevicenode);
var drvname,devname: string;
i: integer;
begin
drvname:=trim(dev.Drv);
devname:=trim(dev.DevLbl);
for i:=1 to StringGrid1.RowCount-1 do begin
if (trim(StringGrid1.Cells[coldriver,i])=drvname)and(trim(StringGrid1.Cells[colname,i])=devname) then begin
while trim(StringGrid1.Cells[colname,i])=devname do
devname:=FormEntry(self,'Duplicate, enter new name',devname);
dev.DevLbl:=devname;
break;
end;
end;
end;
procedure Tf_main.EditDeviceName(r: integer);
var drvname,devname: string;
i: integer;
begin
if (r>0)and(r<StringGrid1.RowCount) then begin
drvname:=trim(StringGrid1.Cells[coldriver,r]);
devname:=trim(StringGrid1.Cells[colname,r]);
devname:=FormEntry(self,'Enter new name',devname);
for i:=1 to StringGrid1.RowCount-1 do begin
if (i<>r)and(trim(StringGrid1.Cells[coldriver,i])=drvname)and(trim(StringGrid1.Cells[colname,i])=devname) then begin
while trim(StringGrid1.Cells[colname,i])=devname do
devname:=FormEntry(self,'Duplicate, enter new name',devname);
break;
end;
end;
StringGrid1.Cells[colname,r]:=devname;
end;
end;
procedure Tf_main.ShowErr(msg:string; str:TStringList);
var buf: string;
i:integer;
begin
buf:=msg+crlf;
for i:=0 to str.Count-1 do buf:=buf+str[i]+crlf;
ShowMessage(buf);
end;
procedure Tf_main.StartDevice(r:integer);
var group,drv,devname,buf: string;
begin
if (r>0)and(r<StringGrid1.RowCount) then begin
group:=StringGrid1.Cells[colgroup,r];
drv:=StringGrid1.Cells[coldriver,r];
devname:=StringGrid1.Cells[colname,r];
if group='Custom' then begin
if remote then
buf:='start '+drv
else
buf:='start '+drv;
end else begin
if remote then
buf:='start '+drv+' -n \"'+devname+'\"'
else
buf:='start '+drv+' -n "'+devname+'"';
end;
WriteCmd(buf);
end;
end;
procedure Tf_main.StopDevice(r:integer);
var group,drv,devname,buf: string;
begin
if (r>0)and(r<StringGrid1.RowCount) then begin
group:=StringGrid1.Cells[colgroup,r];
drv:=StringGrid1.Cells[coldriver,r];
devname:=StringGrid1.Cells[colname,r];
if group='Custom' then begin
if remote then
buf:='stop '+drv
else
buf:='stop '+drv;
end else begin
if remote then
buf:='stop '+drv+' -n \"'+devname+'\"'
else
buf:='stop '+drv+' -n "'+devname+'"';
end;
WriteCmd(buf);
end;
end;
function Tf_main.WriteCmd(cmd:string): boolean;
var f: textfile;
str:TStringList;
begin
if ServerPid<>'' then begin
if remote then begin
str:=TStringList.Create;
ExecProcess('ssh '+sshopt+' -p '+RemoteSshPort+' '+RemoteUser+'@'+RemoteHost+' echo '+cmd+'>'+ServerFifo,str);
result:=true;
str.Free;
end
else begin
AssignFile(f,ServerFifo);
Rewrite(f);
Writeln(f,cmd);
CloseFile(f);
result:=true;
end;
end
else
result:=false;
end;
procedure Tf_main.StartServer;
var str:TStringList;
cmd:string;
i:integer;
begin
try
if StringGrid1.RowCount<2 then begin
LabelStatus.Caption:='No driver configured!';
exit;
end;
StatusTimer.Enabled:=false;
if ServerPid='' then begin
str:=TStringList.Create;
if remote then begin
ServerFifo:='/tmp/IndiStarter.fifo';
ExecProcess('ssh '+sshopt+' -p '+RemoteSshPort+' '+RemoteUser+'@'+RemoteHost+' rm '+ServerFifo,str);
if ExecProcess('ssh '+sshopt+' -p '+RemoteSshPort+' '+RemoteUser+'@'+RemoteHost+' mkfifo '+ServerFifo,str)<>0 then begin ShowErr(RemoteUser+'@'+RemoteHost+' mkfifo '+ServerFifo,str);exit;end;
if ExecProcess('ssh '+sshopt+' -p '+RemoteSshPort+' '+RemoteUser+'@'+RemoteHost+' "sh -c ''nohup indiserver '+serveroptions+' -f '+ServerFifo+' >/dev/null 2>&1 &''"',str)<>0 then begin ShowErr(RemoteUser+'@'+RemoteHost+' indiserver',str);exit;end;
Wait(5);
ServerStarted:=true;
if StringGrid1.RowCount>1 then begin
for i:=1 to StringGrid1.RowCount-1 do begin
StartDevice(i);
end;
end;
StartTunnel;
end
else begin
ServerFifo:=slash(GetTempDir(true))+'IndiStarter.fifo';
DeleteFile(ServerFifo);
if (ExecProcess('mkfifo '+ServerFifo,str)=0) then begin
{$ifdef darwin}
cmd:='PATH="'+slash(appdir)+'Resources/DriverSupport:'+slash(appdir)+'MacOS:$PATH"';
cmd:=cmd+' INDIPREFIX="'+AppBaseDir+'"';
cmd:=cmd+' DYLD_LIBRARY_PATH="'+slash(appdir)+'Frameworks"';
cmd:=cmd+' IOLIBS="'+slash(appdir)+'Resources/DriverSupport/gphoto/IOLIBS"';
cmd:=cmd+' CAMLIBS="'+slash(appdir)+'Resources/DriverSupport/gphoto/CAMLIBS"';
if GSCdir<>'' then cmd:=cmd+' GSCDAT="'+GSCdir+'"';
cmd:=cmd+' indiserver '+serveroptions+' -f '+ServerFifo;
ExecBG(cmd,serverlog);
{$else}
if bindir<>'' then
cmd:='export PATH='+bindir+':$PATH && '
else
cmd:='';
cmd:=cmd+'indiserver '+serveroptions+' -f '+ServerFifo;
ExecBG(cmd,serverlog);
{$endif}
Wait(1);
ServerStarted:=true;
if StringGrid1.RowCount>1 then begin
for i:=1 to StringGrid1.RowCount-1 do begin
StartDevice(i);
end;
end;
end else begin
ShowErr('Cannot create FIFO!',str);
exit;
end;
end;
str.free;