forked from yongchaofan/tinyTerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiny.c
1333 lines (1294 loc) · 36.8 KB
/
tiny.c
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
//
// "$Id: tiny.c 37717 2021-09-01 21:35:10 $"
//
// tinyTerm -- A minimal serail/telnet/ssh/sftp terminal emulator
//
// tiny.c is the GUI implementation using WIN32 API.
//
// Copyright 2018-2021 by Yongchao Fan.
//
// This library is free software distributed under GNU GPL 3.0,
// see the license at:
//
// https://github.com/yongchaofan/tinyTerm/blob/master/LICENSE
//
// Please report all bugs and problems on the following page:
//
// https://github.com/yongchaofan/tinyTerm/issues/new
//
#include "res/resource.h"
#include "tiny.h"
#include <windows.h>
#include <windowsx.h>
#include <shlobj.h>
#include <direct.h>
#define ID_SCRIPT0 1000
#define ID_CONNECT0 2000
//#define WM_DPICHANGED 0x02E0
//#define SM_CXPADDEDBORDER 92
int dpi = 96;
int titleHeight;
int fontSize = 16;
WCHAR fontFace[32] = L"Consolas";
WCHAR wndTitle[256] = L" Term Script Options ";
const char TINYTERM[]="\r\033[32mtinyTerm> \033[37m";
const char WELCOME[]="\r\n\n\
\ttinyTerm is a simple, small and scriptable terminal emulator,\r\n\n\
\ta serial/telnet/ssh/sftp/netconf client with unique features:\r\n\n\n\
\t * small portable exe less than 250KB\r\n\n\
\t * command history and autocompletion\r\n\n\
\t * text based batch command automation\r\n\n\
\t * drag and drop to send files via scp or xmodem\r\n\n\
\t * scripting interface at xmlhttp://127.0.0.1:%d\r\n\n\n\
\thttps://yongchaofan.github.io/tinyTerm\r\n\n\
\tVerision 1.9.9 ©2018-2021 Yongchao Fan\r\n\n";
const COLORREF COLORS[16] = {
RGB(0,0,0), RGB(192,0,0), RGB(0,192,0), RGB(192,192,0),
RGB(32,96,240), RGB(192,0,192), RGB(0,192,192), RGB(192,192,192),
RGB(0,0,0), RGB(240,0,0), RGB(0,240,0), RGB(240,240,0),
RGB(32,96,240), RGB(240,0,240), RGB(0,240,240), RGB(240,240,240)
};
static HINSTANCE hInst;
static HBRUSH dwBkBrush;
static HWND hwndTerm, hwndCmd;
static HWND hwndScriptDlg=NULL; //script control dialog window
static RECT termRect, wndRect;
static HFONT hTermFont;
static HMENU hMainMenu, hMenu[4];
const int TTERM=0, SCRIPT=1, OPTION=2, CONTEX=3;
int menuX[4];
TERM *pt;
HOST *ph;
static int iFontHeight, iFontWidth;
static int iTransparency = 255;
static int iConnectCount=0, iScriptCount=0, httport;
static BOOL bFocus=TRUE, bLocalEdit=FALSE, bScrollbar=FALSE;
static BOOL bScriptRun=FALSE, bScriptPause=FALSE;
static BOOL bFTPd=FALSE, bTFTPd=FALSE;
void LoadDict();
void SaveDict();
void OpenScript(WCHAR *wfn);
void DropScript(char *tl1s);
void DropFiles(HDROP hDrop);
void DropXmodem(HDROP hDrop);
int wchar_to_utf8(WCHAR *wbuf, int wcnt, char *buf, int cnt)
{
return WideCharToMultiByte(CP_UTF8, 0, wbuf, wcnt, buf, cnt, NULL, NULL);
}
int utf8_to_wchar(const char *buf, int cnt, WCHAR *wbuf, int wcnt)
{
return MultiByteToWideChar(CP_UTF8, 0, buf, cnt, wbuf, wcnt);
}
FILE * fopen_utf8(const char *fn, const char *mode)
{
WCHAR wfn[MAX_PATH], wmode[4];
utf8_to_wchar(fn, strlen(fn)+1, wfn, MAX_PATH);
utf8_to_wchar(mode, strlen(mode)+1, wmode, 4);
return _wfopen(wfn, wmode);
}
int stat_utf8(const char *fn, struct _stat *buffer)
{
WCHAR wfn[MAX_PATH];
utf8_to_wchar(fn, strlen(fn)+1, wfn, MAX_PATH);
return _wstat(wfn, buffer);
}
WCHAR *fileDialog( WCHAR *szFilter, DWORD dwFlags )
{
static WCHAR wname[MAX_PATH];
BOOL ret = FALSE;
OPENFILENAME ofn;
wname[0]=0;
memset(&ofn, 0, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwndTerm;
ofn.lpstrFile = wname;
ofn.nMaxFile = MAX_PATH-1;
ofn.lpstrFilter = szFilter;
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = dwFlags | OFN_NOCHANGEDIR;
if ( dwFlags&OFN_OVERWRITEPROMPT )
ret = GetSaveFileName(&ofn);
else
ret = GetOpenFileName(&ofn);
return ret ? wname : NULL;
}
char *getFolderName(WCHAR *wtitle)
{
static BROWSEINFO bi;
static char szFolder[MAX_PATH];
static WCHAR wfolder[MAX_PATH];
WCHAR szDispName[MAX_PATH];
LPITEMIDLIST pidl;
memset(&bi, 0, sizeof(BROWSEINFO));
bi.hwndOwner = 0;
bi.pidlRoot = NULL;
bi.pszDisplayName = szDispName;
bi.lpszTitle = wtitle;
bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0;
pidl = SHBrowseForFolder(&bi);
if ( pidl != NULL )
if ( SHGetPathFromIDList(pidl, wfolder) )
{
wchar_to_utf8(wfolder, -1, szFolder, MAX_PATH);
return szFolder;
}
return NULL;
}
BOOL fontDialog()
{
LOGFONT lf;
CHOOSEFONT cf;
ZeroMemory(&cf, sizeof(cf));
cf.lStructSize = sizeof (cf);
cf.hwndOwner = hwndTerm;
cf.lpLogFont = &lf;
cf.Flags = CF_SCREENFONTS|CF_FIXEDPITCHONLY|CF_INITTOLOGFONTSTRUCT;
if ( GetObject(hTermFont, sizeof(lf), &lf)==0 ) ZeroMemory(&lf, sizeof(lf));
if ( ChooseFont(&cf) )
{
DeleteObject(hTermFont);
hTermFont = CreateFontIndirect(&lf);
SendMessage( hwndCmd, WM_SETFONT, (WPARAM)hTermFont, TRUE );
fontSize = lf.lfHeight;
if ( fontSize<0 ) fontSize = -fontSize;
wcscpy(fontFace, lf.lfFaceName);
return TRUE;
}
return FALSE;
}
void menu_Add(WCHAR *wcmd)
{
if ( wcsncmp(wcmd, L"com", 3)==0 ||
wcsncmp(wcmd, L"ssh", 3)==0 ||
wcsncmp(wcmd, L"sftp", 4)==0 ||
wcsncmp(wcmd, L"telnet",6)==0 ||
wcsncmp(wcmd, L"netconf",7)==0 )
AppendMenu( hMenu[TTERM], 0, ID_CONNECT0+iConnectCount++, wcmd);
if ( wcsncmp(wcmd, L"script ", 7)==0 )
AppendMenu( hMenu[SCRIPT], 0, ID_SCRIPT0+iScriptCount++, wcmd+7);
}
void menu_Del(HMENU menu, int pos)
{
int id = GetMenuItemID(menu, pos);
if ( id>=ID_SCRIPT0 ) {
WCHAR wcmd[264] = L"!script ";
GetMenuString(menu, pos, wcmd+(id<ID_CONNECT0?8:1), 256, MF_BYPOSITION);
DeleteMenu(menu, pos, MF_BYPOSITION);
autocomplete_Del(wcmd);
}
}
void menu_Size()
{
titleHeight = GetSystemMetrics(SM_CYFRAME)
+ GetSystemMetrics(SM_CYCAPTION)
+ GetSystemMetrics(SM_CXPADDEDBORDER);
HFONT oldFont = 0;
RECT menuRect = { 0, 0, 0, 0};
HDC wndDC = GetWindowDC(hwndTerm);
oldFont = (HFONT)SelectObject(wndDC, GetStockObject(SYSTEM_FONT));
menuX[0] = 32;
DrawText(wndDC, L" Term ", 7, &menuRect, DT_CALCRECT);
menuX[1] = menuX[0]+(menuRect.right-menuRect.left)*dpi/96;
menuRect.left = menuRect.right = 0;
DrawText(wndDC, L"Script", 6, &menuRect, DT_CALCRECT);
menuX[2] = menuX[1]+(menuRect.right-menuRect.left)*dpi/96;
menuRect.left = menuRect.right = 0;
DrawText(wndDC, L"Options", 7, &menuRect, DT_CALCRECT);
menuX[3] = menuX[2]+(menuRect.right-menuRect.left)*dpi/96;
if ( oldFont ) SelectObject( wndDC, oldFont );
ReleaseDC(hwndTerm, wndDC);
}
void menu_Check(DWORD id, BOOL op)
{
CheckMenuItem(hMainMenu, id, MF_BYCOMMAND|(op?MF_CHECKED:MF_UNCHECKED));
}
void menu_Enable(DWORD id, BOOL op)
{
EnableMenuItem(hMainMenu, id, MF_BYCOMMAND|(op?MF_ENABLED:MF_GRAYED));
}
void menu_Popup(int i)
{
TrackPopupMenu(hMenu[i], TPM_LEFTBUTTON, wndRect.left+menuX[i]-24,
wndRect.top+titleHeight, 0, hwndTerm, NULL );
}
void cmd_Disp(WCHAR *wbuf)
{
SetWindowText(hwndCmd, wbuf);
PostMessage(hwndCmd, EM_SETSEL, 0, -1);
}
void cmd_Enter(WCHAR *wcmd)
{
char cmd[256];
int cnt = wchar_to_utf8(wcmd, -1, cmd, 256);
int added = autocomplete_Add(wcmd);
cmd_Disp(L"");
if ( *cmd=='!' ) {
if ( added ) menu_Add(wcmd+1);
if ( strncmp(cmd+1,"scp ",4)==0 || strncmp(cmd+1,"tun",3)==0 )
DropScript(strdup(cmd));
else
term_Cmd(pt, cmd, NULL);
}
else {
if ( ph->status!=IDLE ) {
cmd[cnt-1] = '\r';
term_Send(pt, cmd, cnt);
}
else {
if ( *cmd ) {
term_Print(pt, "\033[33m%s\r\n", cmd);
host_Open(ph, cmd);
}
else
host_Open(ph, NULL);
}
}
}
WNDPROC wpOrigCmdProc;
LRESULT APIENTRY CmdEditProc(HWND hwnd, UINT uMsg,
WPARAM wParam, LPARAM lParam)
{
WCHAR wcmd[256];
if ( uMsg==WM_KEYDOWN )
{
if ( GetKeyState(VK_CONTROL) & 0x8000 ) //CTRL+
{
char cmd = 0;
if ( wParam==54 ) cmd = 30; //^
if ( wParam>64 && wParam<91 ) cmd = wParam-64; //A-Z
if ( wParam>218&&wParam<222 ) cmd = wParam-192; //[\]
if ( cmd ) {
term_Send(pt, &cmd, 1);
return 1;
}
}
else
{
switch ( wParam ) {
case VK_UP:
cmd_Disp(autocomplete_Prev()); break;
case VK_DOWN:
cmd_Disp(autocomplete_Next()); break;
case VK_BACK:
if ( GetWindowText(hwndCmd, wcmd, 256)==0 ) {
term_Send(pt, "\b", 1);
return 1;
}
break;
case VK_RETURN:
if ( GetWindowText(hwndCmd, wcmd, 256)>=0 )
cmd_Enter(wcmd);
break;
}
}
}
return CallWindowProc(wpOrigCmdProc, hwnd, uMsg, wParam, lParam);
}
char *tiny_Gets(char *prompt, BOOL bEcho)
{
return ssh2_Gets(ph, prompt, bEcho);
}
static BOOL redraw_pending=FALSE;
void tiny_Redraw()
{
redraw_pending = TRUE;
}
void tiny_Title(char *buf)
{
utf8_to_wchar(buf, -1, wndTitle+50, 200);
SetWindowText(hwndTerm, wndTitle);
MENUITEMINFOA menuitem = { sizeof(MENUITEMINFOA) };
GetMenuItemInfoA(hMenu[0], ID_CONNECT, FALSE, &menuitem);
if ( ph->status==IDLE )
{
if ( bLocalEdit ) term_Disp(pt, TINYTERM);
menuitem.dwTypeData = "Connect...";
}
else {
menuitem.dwTypeData = "Disonnect";
}
menuitem.fMask = MIIM_TYPE | MIIM_DATA;
SetMenuItemInfoA(hMenu[0], ID_CONNECT, FALSE, &menuitem);
menu_Check(ID_ECHO, pt->bEcho);
}
BOOL tiny_Scroll(BOOL bShowScroll, int cy, int sy)
{
BOOL rt = FALSE;
if ( bShowScroll ) {
SetScrollRange(hwndTerm, SB_VERT, 0, cy, TRUE);
SetScrollPos(hwndTerm, SB_VERT, sy, TRUE);
if ( !bScrollbar ) {
bScrollbar = TRUE;
ShowScrollBar(hwndTerm, SB_VERT, TRUE);
rt = TRUE; //first pageup fix
}
}
else {
if ( bScrollbar ) {
bScrollbar = FALSE;
ShowScrollBar(hwndTerm, SB_VERT, FALSE);
}
}
tiny_Redraw();
return rt;
}
void tiny_Beep()
{
PlaySound(L"Default Beep", NULL, SND_ALIAS|SND_ASYNC);
}
//wnd_Size: adjust window size when fontface/fontsize or size_x/size_y changed
void wnd_Size()
{
HDC hdc;
TEXTMETRIC tm;
hdc = GetDC(hwndTerm);
SelectObject(hdc, hTermFont);
GetTextMetrics(hdc, &tm);
ReleaseDC(hwndTerm, hdc);
iFontHeight = tm.tmHeight;
iFontWidth = tm.tmAveCharWidth;
GetWindowRect( hwndTerm, &wndRect );
int x = wndRect.left;
int y = wndRect.top;
wndRect.right = x + iFontWidth*pt->size_x;
wndRect.bottom = y + iFontHeight*pt->size_y;
AdjustWindowRect(&wndRect, WS_TILEDWINDOW, FALSE);
MoveWindow( hwndTerm, x, y, wndRect.right-wndRect.left,
wndRect.bottom-wndRect.top, TRUE );
}
void font_Size()
{
DeleteObject(hTermFont);
hTermFont = CreateFont(fontSize*dpi/96,0,0,0,FW_MEDIUM,FALSE,FALSE,FALSE,
DEFAULT_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, FIXED_PITCH, fontFace);
SendMessage( hwndCmd, WM_SETFONT, (WPARAM)hTermFont, TRUE );
wnd_Size();
}
void tiny_Paint(HDC hDC, RECT rcPaint)
{
WCHAR wbuf[1024];
RECT text_rect = {0, 0, 0, 0};
SelectObject(hDC, hTermFont);
int y = pt->screen_y;
int sel_min = min(pt->sel_left, pt->sel_right);
int sel_max = max(pt->sel_left, pt->sel_right);
int dx, dy=rcPaint.top;
redraw_pending = FALSE;
for ( int l=dy/iFontHeight; l<pt->size_y; l++ ) {
dx = 0;
int i = pt->line[y+l];
while ( i<pt->line[y+l+1] ) {
BOOL utf8 = FALSE;
int j = i;
while ( pt->attr[j]==pt->attr[i] ) {
if ( (pt->buff[j]&0xc0)==0xc0 ) utf8 = TRUE;
if ( ++j==pt->line[y+l+1] ) break;
if ( j==sel_min || j==sel_max ) break;
}
if ( i>=sel_min&&i<sel_max ) {
SetTextColor(hDC, COLORS[0]);
SetBkColor(hDC, COLORS[7]);
}
else {
SetTextColor(hDC, COLORS[pt->attr[i]&0x0f]);
SetBkColor(hDC, COLORS[(pt->attr[i]>>4)&0x0f]);
}
int len = j-i;
if ( pt->buff[j-1]==0x0a ) len--; //remove unprintable 0x0a for XP
if ( utf8 ) {
int cnt = utf8_to_wchar(pt->buff+i, len, wbuf, 1024);
TextOutW(hDC, dx, dy, wbuf, cnt);
DrawText(hDC, wbuf, cnt, &text_rect, DT_CALCRECT|DT_NOPREFIX);
dx += text_rect.right;
}
else {
TextOutA(hDC, dx, dy, pt->buff+i, len);
dx += iFontWidth*len;
}
i=j;
}
if ( dx < termRect.right ) {
RECT fillRect;
fillRect.top = dy;
fillRect.bottom = dy+iFontHeight;
fillRect.left = dx;
fillRect.right = termRect.right;
FillRect(hDC, &fillRect, dwBkBrush);
}
dy += iFontHeight;
}
int cnt = utf8_to_wchar(pt->buff+pt->line[pt->cursor_y],
pt->cursor_x-pt->line[pt->cursor_y], wbuf, 1024);
if ( cnt>0 )
DrawText(hDC, wbuf, cnt, &text_rect, DT_CALCRECT|DT_NOPREFIX);
else
text_rect.right = 0;//DrawText won't work when wbuf is zero length
if ( bLocalEdit && !pt->bAlterScreen && host_Status(ph)!=AUTHENTICATING ) {
MoveWindow(hwndCmd, text_rect.right,
(pt->cursor_y-pt->screen_y)*iFontHeight,
termRect.right-text_rect.right, iFontHeight, TRUE);
SetFocus(hwndCmd);
}
else {
MoveWindow(hwndCmd, 0, 0, 1, 1, TRUE);
SetFocus(hwndTerm);
SetCaretPos(text_rect.right+1,
(pt->cursor_y-pt->screen_y)*iFontHeight+iFontHeight*3/4);
if ( pt->bCursor && bFocus)
ShowCaret(hwndTerm);
else
HideCaret(hwndTerm);
}
}
const WCHAR *PROTOCOLS[]={L"Serial ", L"telnet ", L"ssh ", L"sftp ", L"netconf "};
const WCHAR *PORTS[] ={L"2024", L"23", L"22", L"22", L"830"};
const WCHAR *SETTINGS[] ={L"9600,n,8,1", L"19200,n,8,1", L"38400,n,8,1",
L"57600,n,8,1", L"115200,n,8,1"};
void get_serial_ports(HWND hwndPort)
{
for ( int i=1; i<32; i++ ) {
WCHAR port[32];
wsprintf(port, L"\\\\.\\COM%d", i);
HANDLE hPort = CreateFile(port, GENERIC_READ, 0, NULL,
OPEN_EXISTING, 0, NULL);
if ( hPort != INVALID_HANDLE_VALUE ) {
ComboBox_AddString(hwndPort,port+4);
CloseHandle( hPort );
}
}
}
static WCHAR last_host[128] = L"192.168.1.1";
void get_hosts(HWND hwndHost)
{
ComboBox_AddString(hwndHost, last_host);
for ( int id=ID_CONNECT0; id<ID_CONNECT0+iConnectCount; id++ ) {
WCHAR wcmd[256], *p;
GetMenuString(hMainMenu, id, wcmd, 256, MF_BYCOMMAND);
p = wcschr(wcmd, L' ');
if ( p!=NULL ) ComboBox_AddString(hwndHost,p+1);
}
}
BOOL CALLBACK ConnectProc(HWND hwndDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{
static HWND hwndProto, hwndPort, hwndHost, hwndStatic, hwndTip;
static int proto = 2;
switch ( message ) {
case WM_INITDIALOG:
hwndStatic = GetDlgItem(hwndDlg, IDSTATIC);
hwndProto = GetDlgItem(hwndDlg, IDPROTO);
hwndPort = GetDlgItem(hwndDlg, IDPORT);
hwndHost = GetDlgItem(hwndDlg, IDHOST);
for ( int i=0; i<5; i++ ) ComboBox_AddString(hwndProto,PROTOCOLS[i]);
if ( proto==0 ) {
ComboBox_SetCurSel(hwndProto, 0);
proto = 2;
}
else {
ComboBox_SetCurSel(hwndProto, proto);
proto = 0;
}
PostMessage(hwndDlg, WM_COMMAND, CBN_SELCHANGE<<16, (LPARAM)hwndProto);
//setup tooltip
COMBOBOXINFO cbi;
cbi.cbSize = sizeof(COMBOBOXINFO);
hwndTip = CreateWindowEx(0, TOOLTIPS_CLASS, NULL,
WS_POPUP |TTS_ALWAYSTIP | TTS_BALLOON,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
hwndDlg, NULL, hInst, NULL);
if ( GetComboBoxInfo(hwndHost, &cbi) && hwndTip) {
TOOLINFO toolInfo = { 0 };
toolInfo.cbSize = sizeof(toolInfo);
toolInfo.hwnd = hwndDlg;
toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
toolInfo.uId = (UINT_PTR)cbi.hwndItem;
toolInfo.lpszText = L"Hostname or IPv4/IPv6 address";
SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo);
}
SetFocus(hwndHost);
break;
case WM_COMMAND:
if ( HIWORD(wParam)==CBN_SELCHANGE ) {
if ( (HWND)lParam==hwndProto ) {
int new_proto = ComboBox_GetCurSel(hwndProto);
if ( proto!=0 && new_proto==0 ) {
ComboBox_ResetContent(hwndPort);
get_serial_ports(hwndPort);
ComboBox_ResetContent(hwndHost);
for ( int i=0; i<5; i++ )
ComboBox_AddString(hwndHost,SETTINGS[i]);
ComboBox_SetCurSel(hwndHost, 0);
Static_SetText(hwndStatic, L"Settings:");
SendMessage(hwndTip, TTM_ACTIVATE, FALSE, 0);
}
if ( proto==0 && new_proto!=0 ) {
ComboBox_ResetContent(hwndHost);
get_hosts(hwndHost);
ComboBox_SetCurSel(hwndHost, 0);
ComboBox_ResetContent(hwndPort);
for ( int i=0; i<5; i++ )
ComboBox_AddString(hwndPort,PORTS[i]);
Static_SetText(hwndStatic, L"Host:");
SendMessage(hwndTip, TTM_ACTIVATE, TRUE, 0);
}
proto = new_proto;
ComboBox_SetCurSel(hwndPort, proto);
}
}
else {
int proto;
WCHAR wcmd[256], *conn = wcmd+1;
wcmd[0] = L'!';
switch ( LOWORD(wParam) ) {
case IDCONNECT:
proto = ComboBox_GetCurSel(hwndProto);
if ( proto==0 ) {
ComboBox_GetText(hwndPort, conn, 127);
wcscat(conn, L":");
ComboBox_GetText(hwndHost, conn+wcslen(conn), 127);
}
else {
ComboBox_GetText(hwndProto, conn, 127);
int len = wcslen(conn);
ComboBox_GetText(hwndHost, conn+len, 127);
ComboBox_GetText(hwndHost, last_host, 127);
SendMessage(hwndHost,CB_FINDSTRING,1,
(LPARAM)(conn+len));
if ( ComboBox_GetCurSel(hwndHost)==CB_ERR )
ComboBox_AddString(hwndHost, conn+len);
wcscat(conn, L":");
len = wcslen(conn);
ComboBox_GetText(hwndPort, conn+len, 128);
if ( wcscmp(conn+len, PORTS[proto])==0 ) conn[len-1]=0;
}
cmd_Enter(wcmd);
case IDCANCEL:
EndDialog(hwndDlg, wParam);
return TRUE;
}
}
}
return FALSE;
}
BOOL CALLBACK ScriptDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND hwndBtn;
switch (msg)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDPAUSE:
hwndBtn = GetDlgItem(hwndDlg, IDPAUSE);
bScriptPause = !bScriptPause;
SetWindowText(hwndBtn, bScriptPause? L"Resume":L"Pause");
return TRUE;
case IDQUIT:
bScriptRun = bScriptPause = FALSE;
DestroyWindow(hwndScriptDlg);
hwndScriptDlg = NULL;
return TRUE;
}
}
return FALSE;
}
void show_script_dialog()
{
if (bScriptRun && !IsWindow(hwndScriptDlg)) {
hwndScriptDlg = CreateDialog( hInst,
MAKEINTRESOURCE(IDD_SCRIPT),
hwndTerm,
(DLGPROC)ScriptDlgProc);
ShowWindow(hwndScriptDlg, SW_SHOW);
}
}
void hide_script_dialog()
{
if ( IsWindow(hwndScriptDlg) ) {
DestroyWindow(hwndScriptDlg);
hwndScriptDlg = NULL;
}
}
BOOL menu_Command( WPARAM wParam, LPARAM lParam )
{
switch ( LOWORD(wParam) ) {
case ID_ABOUT:{
char welcome[1024];
sprintf(welcome, WELCOME, httport);
term_Disp(pt, welcome);
}
break;
case ID_CONNECT:
if ( ph->status==IDLE )
DialogBox(hInst, MAKEINTRESOURCE(IDD_CONNECT),
hwndTerm, (DLGPROC)ConnectProc);
else
host_Close(ph);
break;
case ID_LOGG:
if ( !pt->bLogging ) {
WCHAR *wfn = fileDialog(L"logfile\0*.log\0All\0*.*\0\0",
OFN_PATHMUSTEXIST|OFN_NOREADONLYRETURN|OFN_OVERWRITEPROMPT);
if ( wfn!=NULL ) {
char fn[MAX_PATH];
wchar_to_utf8(wfn, wcslen(wfn)+1, fn, MAX_PATH);
term_Logg(pt, fn);
}
}
else
term_Logg( pt, NULL );
menu_Check( ID_LOGG, pt->bLogging );
break;
case ID_SELALL:
pt->sel_left = 0;
pt->sel_right = pt->cursor_x;
tiny_Redraw();
break;
case ID_COPY:
if ( OpenClipboard(hwndTerm) ) {
EmptyClipboard();
char *ptr;
int len = term_Copy(pt, &ptr);
HANDLE hglbCopy = GlobalAlloc(GMEM_MOVEABLE, (len+1)*2);
if ( hglbCopy!=NULL && len>0) {
WCHAR *wbuf = GlobalLock(hglbCopy);
len = utf8_to_wchar(ptr, len, wbuf, len);
wbuf[len] = 0;
GlobalUnlock(hglbCopy);
SetClipboardData(CF_UNICODETEXT, hglbCopy);
}
CloseClipboard();
}
break;
case ID_PASTE:
if ( OpenClipboard(hwndTerm) ) {
HANDLE hglb = GetClipboardData(CF_UNICODETEXT);
WCHAR *ptr = (WCHAR *)GlobalLock(hglb);
if (ptr != NULL) {
int len = wchar_to_utf8(ptr, -1, NULL, 0);
char *p = (char *)malloc(len);
if ( p!=NULL ) {
wchar_to_utf8(ptr, -1, p, len);
term_Paste(pt, p, len);
free(p);
}
GlobalUnlock(hglb);
}
CloseClipboard();
}
break;
case ID_MIDDLE:
term_Mouse(pt, MIDDLEUP, 0, 0);
break;
case ID_DELETE: {
WCHAR wcmd[256];
if ( GetWindowText(hwndCmd, wcmd, 256)>0 ) {
autocomplete_Del(wcmd);
cmd_Disp(autocomplete_Next());
}
break;
}
case ID_TAB:
if ( bLocalEdit ) {
char cmd[256];
WCHAR wcmd[256];
Edit_ReplaceSel(hwndCmd, L"");
GetWindowText(hwndCmd, wcmd, 256);
SetWindowText(hwndCmd, L"");
int cnt = wchar_to_utf8(wcmd, -1, cmd, 256);
if ( cnt>0 ) term_Send(pt, cmd, cnt-1);
}
term_Send(pt, "\t", 1);
break;
case ID_PRIOR: term_Scroll(pt, pt->size_y-1); break;
case ID_NEXT: term_Scroll(pt, 1-pt->size_y); break;
case ID_ECHO: menu_Check(ID_ECHO, term_Echo(pt)); break;
case ID_EDIT:
bLocalEdit = !bLocalEdit;
menu_Check(ID_EDIT, bLocalEdit);
if ( bLocalEdit && ph->status==IDLE ) term_Disp(pt, TINYTERM);
tiny_Redraw();
break;
case ID_TRANSP:
if ( lParam>0 && lParam<256 )
iTransparency = lParam;
else
iTransparency = (iTransparency==255) ? 224 : 255;
SetLayeredWindowAttributes(hwndTerm, 0, iTransparency, LWA_ALPHA);
menu_Check( ID_TRANSP, iTransparency!=255 );
break;
case ID_FONT:
if ( fontDialog() ) wnd_Size();
break;
case ID_FTPD:
bFTPd = ftp_Svr(bFTPd?NULL:getFolderName(L"Choose root directory"));
menu_Check( ID_FTPD, bFTPd );
break;
case ID_TFTPD:
bTFTPd = tftp_Svr(bTFTPd?NULL:getFolderName(L"Choose root directory"));
menu_Check( ID_TFTPD, bTFTPd );
break;
case ID_RUN: {
WCHAR *wfn=fileDialog(L"Script\0*.html;*.js;*.vbs;*.txt\0All\0*.*\0\0",
OFN_FILEMUSTEXIST);
if ( wfn!=NULL ) {
WCHAR wcwd[MAX_PATH];
_wgetcwd(wcwd, MAX_PATH);
int len = wcslen(wcwd);
if ( wcsncmp(wcwd, wfn, len)==0 ) wfn+=len+1;
OpenScript(wfn);
}
break;
}
case ID_PAUSE:
show_script_dialog();
break;
case ID_QUIT:
hide_script_dialog();
break;
case ID_TERM:
menu_Popup(TTERM);
break;
case ID_SCRIPT:
menu_Popup(SCRIPT);
break;
case ID_OPTIONS:
menu_Popup(OPTION);
break;
default:
if ( wParam>=ID_SCRIPT0 && wParam<ID_SCRIPT0+iScriptCount )
{
WCHAR wfn[256];
GetMenuString(hMenu[SCRIPT], wParam, wfn, 256, 0);
OpenScript(wfn);
}
else if ( wParam>=ID_CONNECT0 && wParam<ID_CONNECT0+iConnectCount )
{
WCHAR wcmd[256];
GetMenuString(hMenu[TTERM], wParam, wcmd, 256, 0);
cmd_Enter(wcmd);
}
else
return FALSE;
}
return TRUE;
}
void ftpd_quit()
{
menu_Check(ID_FTPD, bFTPd=FALSE);
}
void tftpd_quit()
{
menu_Check(ID_TFTPD, bTFTPd=FALSE);
}
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
static WCHAR wm_chars[2]={0,0}; //for unicode character input
switch (msg) {
case WM_CREATE:
hwndTerm = hwnd;
drop_Init(hwnd, DropScript);
DragAcceptFiles(hwnd, TRUE);
hwndCmd = CreateWindow(L"EDIT", NULL, WS_CHILD|WS_VISIBLE
|ES_AUTOHSCROLL|ES_NOHIDESEL, 0, 0, 1, 1,
hwnd, (HMENU)0, hInst, NULL);
wpOrigCmdProc = (WNDPROC)SetWindowLongPtr(hwndCmd,
GWLP_WNDPROC, (LONG_PTR)CmdEditProc);
SendMessage(hwndCmd, WM_SETFONT, (WPARAM)hTermFont, TRUE);
SendMessage(hwndCmd, EM_SETLIMITTEXT, 255, 0);
autocomplete_Init(hwndCmd);
LoadDict();
menu_Size();
font_Size();
wnd_Size();
SetLayeredWindowAttributes(hwnd,0,iTransparency,LWA_ALPHA);
ShowWindow(hwnd, SW_SHOW);
SetTimer(hwnd, 1, 20, (TIMERPROC)NULL); //redraw at 50Hz
if ( bLocalEdit )
term_Disp(pt, TINYTERM);
else
PostMessage(hwndTerm, WM_COMMAND, ID_CONNECT, 0);
break;
case WM_SIZE:
if ( IsWindowVisible(hwnd) ) { //change term size only when visible
GetClientRect(hwnd, &termRect);
term_Size(pt, termRect.right/iFontWidth,
termRect.bottom/iFontHeight);
tiny_Redraw();
}
case WM_MOVE:
GetWindowRect(hwnd, &wndRect);
menu_Size();
break;
case WM_DPICHANGED:
dpi = LOWORD(wParam);
font_Size();
menu_Size();
break;
case WM_PAINT: {
PAINTSTRUCT ps;
if ( BeginPaint(hwnd, &ps)!=NULL )
tiny_Paint(ps.hdc, ps.rcPaint);
EndPaint(hwnd, &ps);
}
break;
case WM_TIMER:
if ( redraw_pending ) InvalidateRect(hwndTerm, &termRect, TRUE);
break;
case WM_SETFOCUS:
CreateCaret(hwnd, NULL, iFontWidth, iFontHeight/4);
bFocus = TRUE;
break;
case WM_KILLFOCUS:
DestroyCaret();
bFocus = FALSE;
break;
case WM_IME_STARTCOMPOSITION:
//moves the composition window to cursor pos on Win10
break;
case WM_CHAR:
if ( ph->status==IDLE ) {//press Enter to reconnect
if ( (wParam&0xff)==0x0d ) host_Open(ph, NULL);
}
else {
if ( (wParam>>8)==0 ) {
char key = wParam&0xff;
term_Send(pt, &key, 1);
}
else {
char utf8[6], ho = wParam>>8;
if ( (ho&0xF8)!=0xD8 ) {
int c = wchar_to_utf8((WCHAR *)&wParam, 1, utf8, 6);
if ( c>0 ) term_Send(pt, utf8, c);
}
else {
if ( (ho&0xDC)==0xD8 )
wm_chars[0] = wParam; //high surrogate word
else
wm_chars[1] = wParam; //low surrogate word
if ( wm_chars[1]!=0 && wm_chars[0]!=0 ) {
int c = wchar_to_utf8(wm_chars, 2, utf8, 6);
if ( c>0 ) term_Send(pt, utf8, c);
wm_chars[0] = 0;
wm_chars[1] = 0;
}
}
}
}
break;
case WM_KEYDOWN:
switch( wParam ) {
case VK_DELETE:term_Send(pt, "\177",1); break;
case VK_UP: term_Send(pt, pt->bAppCursor?"\033OA":"\033[A",3); break;
case VK_DOWN: term_Send(pt, pt->bAppCursor?"\033OB":"\033[B",3); break;
case VK_RIGHT: term_Send(pt, pt->bAppCursor?"\033OC":"\033[C",3); break;
case VK_LEFT: term_Send(pt, pt->bAppCursor?"\033OD":"\033[D",3); break;
case VK_HOME: term_Send(pt, pt->bAppCursor?"\033OH":"\033[H",3); break;
case VK_END: term_Send(pt, pt->bAppCursor?"\033OF":"\033[F",3); break;
}
if ( bScrollbar )
term_Scroll(pt, pt->screen_y-(pt->cursor_y-pt->size_y+1));
break;
case WM_VSCROLL:
switch ( LOWORD (wParam) )
{
case SB_LINEUP: term_Scroll(pt, 1); break;
case SB_LINEDOWN: term_Scroll(pt, -1); break;
case SB_PAGEUP: term_Scroll(pt, pt->size_y-1); break;
case SB_PAGEDOWN: term_Scroll(pt, 1-pt->size_y); break;
case SB_THUMBTRACK: {
SCROLLINFO si;
si.cbSize = sizeof (si);
si.fMask = SIF_ALL;
GetScrollInfo (hwnd, SB_VERT, &si);
term_Scroll(pt, si.nPos-si.nTrackPos);
}
}
break;
case WM_MOUSEWHEEL:
term_Scroll(pt, GET_WHEEL_DELTA_WPARAM(wParam)/40);
break;
case WM_LBUTTONDBLCLK:
term_Mouse(pt, DOUBLECLK, GET_X_LPARAM(lParam)/iFontWidth,
(GET_Y_LPARAM(lParam)+2)/iFontHeight);
break;
case WM_LBUTTONDOWN:
term_Mouse(pt, LEFTDOWN, GET_X_LPARAM(lParam)/iFontWidth,
(GET_Y_LPARAM(lParam)+2)/iFontHeight);
SetCapture(hwnd);
break;
case WM_MOUSEMOVE:
if ( MK_LBUTTON&wParam ) {
term_Mouse(pt, LEFTDRAG, GET_X_LPARAM(lParam)/iFontWidth,
(GET_Y_LPARAM(lParam)+2)/iFontHeight);
}
break;
case WM_LBUTTONUP:
term_Mouse(pt, LEFTUP, GET_X_LPARAM(lParam)/iFontWidth,
(GET_Y_LPARAM(lParam)+2)/iFontHeight);
ReleaseCapture();
break;
case WM_MBUTTONUP:
term_Mouse(pt, MIDDLEUP, GET_X_LPARAM(lParam)/iFontWidth,
(GET_Y_LPARAM(lParam)+2)/iFontHeight);
break;
case WM_CONTEXTMENU:
TrackPopupMenu(hMenu[CONTEX], TPM_LEFTBUTTON, GET_X_LPARAM(lParam),
GET_Y_LPARAM(lParam), 0, hwndTerm, NULL);
break;
case WM_NCLBUTTONDOWN: {
int y = GET_Y_LPARAM(lParam)-wndRect.top;
if ( y>0 && y<titleHeight ) {
int x = GET_X_LPARAM(lParam)-wndRect.left;
if ( x>menuX[0] && x<menuX[3] ) return 0;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
case WM_NCLBUTTONUP: {
int y = GET_Y_LPARAM(lParam)-wndRect.top;
if ( y>0 && y<titleHeight ) {
int x = GET_X_LPARAM(lParam)-wndRect.left;
for ( int i=0; i<3; i++ ) {
if ( x>menuX[i] && x<menuX[i+1] ) {
menu_Popup(i);
return 0;
}
}
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
case WM_MENURBUTTONUP:
menu_Del((HMENU)lParam, wParam);
break;
case WM_DROPFILES:
if ( ph->type==SSH || ph->type==SFTP )
DropFiles((HDROP)wParam);
if ( ph->type==SERIAL )
DropXmodem((HDROP)wParam);
break;
case WM_CTLCOLOREDIT:
SetTextColor((HDC)wParam, COLORS[3]);