forked from cyd01/KiTTY
-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.c
8994 lines (8208 loc) · 275 KB
/
window.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
/*
* window.c - the PuTTY(tel) main program, which runs a PuTTY terminal
* emulator and backend in a window.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <limits.h>
#include <assert.h>
#ifdef MOD_FAR2L
/* far2l extensions support - base64 encode/decode libs */
#include <../../far2l/cencode.h>
#include <../../far2l/cdecode.h>
#endif
#ifdef __WINE__
#define NO_MULTIMON /* winelib doesn't have this */
#endif
#ifndef NO_MULTIMON
#define COMPILE_MULTIMON_STUBS
#endif
#include "putty.h"
#include "terminal.h"
#include "storage.h"
#include "putty-rc.h"
#include "winsecur.h"
#include "winseat.h"
#include "tree234.h"
#ifndef NO_MULTIMON
#ifdef MOD_PERSO
#include <winuser.h>
#else
#include <multimon.h>
#endif
#endif
#include <imm.h>
#include <commctrl.h>
#include <richedit.h>
#include <mmsystem.h>
/* From MSDN: In the WM_SYSCOMMAND message, the four low-order bits of
* wParam are used by Windows, and should be masked off, so we shouldn't
* attempt to store information in them. Hence all these identifiers have
* the low 4 bits clear. Also, identifiers should < 0xF000. */
#define IDM_SHOWLOG 0x0010
#define IDM_NEWSESS 0x0020
#define IDM_DUPSESS 0x0030
#define IDM_RESTART 0x0040
#define IDM_RECONF 0x0050
#define IDM_CLRSB 0x0060
#define IDM_RESET 0x0070
#define IDM_HELP 0x0140
#define IDM_ABOUT 0x0150
#define IDM_SAVEDSESS 0x0160
#define IDM_COPYALL 0x0170
#define IDM_FULLSCREEN 0x0180
#define IDM_COPY 0x0190
#define IDM_PASTE 0x01A0
#define IDM_NEWDUPSESS 0x01B0
#define IDM_SPECIALSEP 0x0200
#define IDM_SPECIAL_MIN 0x0400
#define IDM_SPECIAL_MAX 0x0800
#define IDM_SAVED_MIN 0x1000
#define IDM_SAVED_MAX 0x5000
#define MENU_SAVED_STEP 16
/* Maximum number of sessions on saved-session submenu */
#define MENU_SAVED_MAX ((IDM_SAVED_MAX-IDM_SAVED_MIN) / MENU_SAVED_STEP)
#define WM_IGNORE_CLIP (WM_APP + 2)
#define WM_FULLSCR_ON_MAX (WM_APP + 3)
#define WM_GOT_CLIPDATA (WM_APP + 4)
/* Needed for Chinese support and apparently not always defined. */
#ifndef VK_PROCESSKEY
#define VK_PROCESSKEY 0xE5
#endif
/* Mouse wheel support. */
#ifndef WM_MOUSEWHEEL
#define WM_MOUSEWHEEL 0x020A /* not defined in earlier SDKs */
#endif
#ifndef WHEEL_DELTA
#define WHEEL_DELTA 120
#endif
/* DPI awareness support */
#ifndef WM_DPICHANGED
#define WM_DPICHANGED 0x02E0
#define WM_DPICHANGED_BEFOREPARENT 0x02E2
#define WM_DPICHANGED_AFTERPARENT 0x02E3
#define WM_GETDPISCALEDSIZE 0x02E4
#endif
/* VK_PACKET, used to send Unicode characters in WM_KEYDOWNs */
#ifndef VK_PACKET
#define VK_PACKET 0xE7
#endif
static Mouse_Button translate_button(Mouse_Button button);
static void show_mouseptr(bool show);
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam,
unsigned char *output);
static void init_palette(void);
static void init_fonts(int, int);
static void init_dpi_info(void);
static void another_font(int);
static void deinit_fonts(void);
static void set_input_locale(HKL);
static void update_savedsess_menu(void);
static void init_winfuncs(void);
static bool is_full_screen(void);
static void make_full_screen(void);
static void clear_full_screen(void);
static void flip_full_screen(void);
static void process_clipdata(HGLOBAL clipdata, bool unicode);
static void setup_clipboards(Terminal *, Conf *);
/* Window layout information */
static void reset_window(int);
static int extra_width, extra_height;
static int font_width, font_height;
static bool font_dualwidth, font_varpitch;
static int offset_width, offset_height;
static bool was_zoomed = false;
static int prev_rows, prev_cols;
static void flash_window(int mode);
static void sys_cursor_update(void);
static bool get_fullscreen_rect(RECT * ss);
static int caret_x = -1, caret_y = -1;
static int kbd_codepage;
static Ldisc *ldisc;
static Backend *backend;
static struct unicode_data ucsdata;
static bool session_closed;
static bool reconfiguring = false;
static const SessionSpecial *specials = NULL;
static HMENU specials_menu = NULL;
static int n_specials = 0;
#define TIMING_TIMER_ID 1234
static long timing_next_time;
static struct {
HMENU menu;
} popup_menus[2];
enum { SYSMENU, CTXMENU };
static HMENU savedsess_menu;
#ifdef MOD_PERSO
Conf *conf;
#else
static Conf *conf;
#endif
static LogContext *logctx;
static Terminal *term;
struct wm_netevent_params {
/* Used to pass data to wm_netevent_callback */
WPARAM wParam;
LPARAM lParam;
};
static void conf_cache_data(void);
static int cursor_type;
static int vtmode;
static struct sesslist sesslist; /* for saved-session menu */
#define FONT_NORMAL 0
#define FONT_BOLD 1
#define FONT_UNDERLINE 2
#define FONT_BOLDUND 3
#ifdef MOD_PERSO
#define FONT_ITALIC 4
#define FONT_WIDE 0x08
#define FONT_HIGH 0x10
#define FONT_NARROW 0x20
#define FONT_OEM 0x40
#define FONT_OEMBOLD 0x41
#define FONT_OEMUND 0x42
#define FONT_OEMBOLDUND 0x43
#define FONT_MAXNO 0x4F
#define FONT_SHIFT 6
static int use_italics;
#else
#define FONT_WIDE 0x04
#define FONT_HIGH 0x08
#define FONT_NARROW 0x10
#define FONT_OEM 0x20
#define FONT_OEMBOLD 0x21
#define FONT_OEMUND 0x22
#define FONT_OEMBOLDUND 0x23
#define FONT_MAXNO 0x40
#define FONT_SHIFT 5
#endif
static HFONT fonts[FONT_MAXNO];
static LOGFONT lfont;
static bool fontflag[FONT_MAXNO];
static enum {
BOLD_NONE, BOLD_SHADOW, BOLD_FONT
} bold_font_mode;
static bool bold_colours;
static enum {
UND_LINE, UND_FONT
} und_mode;
static int descent, font_strikethrough_y;
static COLORREF colours[OSC4_NCOLOURS];
static HPALETTE pal;
static LPLOGPALETTE logpal;
bool tried_pal = false;
COLORREF colorref_modifier = 0;
enum MONITOR_DPI_TYPE { MDT_EFFECTIVE_DPI, MDT_ANGULAR_DPI, MDT_RAW_DPI, MDT_DEFAULT };
DECL_WINDOWS_FUNCTION(static, HRESULT, GetDpiForMonitor, (HMONITOR hmonitor, enum MONITOR_DPI_TYPE dpiType, UINT *dpiX, UINT *dpiY));
DECL_WINDOWS_FUNCTION(static, HRESULT, GetSystemMetricsForDpi, (int nIndex, UINT dpi));
DECL_WINDOWS_FUNCTION(static, HRESULT, AdjustWindowRectExForDpi, (LPRECT lpRect, DWORD dwStyle, BOOL bMenu, DWORD dwExStyle, UINT dpi));
static struct _dpi_info {
POINT cur_dpi;
RECT new_wnd_rect;
} dpi_info;
static HBITMAP caretbm;
#ifdef MOD_HYPERLINK
#include "urlhack.h"
static int urlhack_cursor_is_hand = 0;
void urlhack_enable(void);
#endif
#ifdef MOD_SAVEDUMP
void SaveDump( void ) ;
#endif
#ifdef MOD_PERSO
#include <sys/types.h>
#include <process.h>
#include <math.h>
extern HWND MainHwnd ;
#include "../../kitty.h"
#include "../../kitty_commun.h"
#include "../../kitty_crypt.h"
#include "../../kitty_launcher.h"
#include "../../kitty_registry.h"
#include "../../kitty_ssh.h"
#include "../../kitty_tools.h"
#include "../../kitty_win.h"
extern int PuttyFlag ;
int GetCursorType() { return cursor_type ; }
void SetCursorType( const int ct ) { cursor_type = ct ; }
extern char BuildVersionTime[256] ;
void SendStrToTerminal( const char * str, const int len ) {
char c ;
int i ;
if( len <= 0 ) return ;
// if( term!=NULL ) term_seen_key_event(term) ;
for( i=0 ; i<len ; i++ ) {
c = (unsigned char)str[i] ;
if (ldisc)
//term_keyinputw(term, (const wchar_t *) &c, 1); //lpage_send(ldisc, CP_ACP, &c, 1, 1);
term_keyinput(term, -1, &c, 1) ;
}
}
// resize en convertissant en nombre de lignes et colonnes
void resize( int height, int width ) {
int w,h;
if( width!=-1 ) {
prev_rows = term->rows;
prev_cols = term->cols;
w = width / font_width ;
if (w < 1) w = 1;
h = height / font_height ;
if (h < 1) h = 1;
}
else {
h = prev_rows ;
w = prev_cols ;
}
term_size(term, h, w, conf_get_int(conf,CONF_savelines)) ;
reset_window(0);
w = (width-conf_get_int(conf,CONF_window_border)*2) / font_width ;
if (w < 1) w = 1;
h = (height-conf_get_int(conf,CONF_window_border)*2) / font_height ;
if (h < 1) h = 1;
conf_set_int(conf,CONF_height,h);
conf_set_int(conf,CONF_width,w);
}
int Convert1Reg( const char * filename ) ;
int get_param( const char * val ) ;
void SendKeyboardPlus( HWND hwnd, const char * st ) ;
//`colours'
int return_offset_height(void) { return offset_height ; }
int return_offset_width(void) { return offset_width ; }
int return_font_height(void) { return font_height ; }
int return_font_width(void) { return font_width ; }
COLORREF return_colours258(void) { return colours[258]; }
// prototypes de fonctions decrites dans winnet.c
struct netscheduler_tag* netscheduler_new(void) ;
void netscheduler_free(struct netscheduler_tag* netscheduler) ;
void AddDynamicSFTPConnect( Conf *conf ) ;
/* String pour charger automatiquement au démarrage un fichier dans un editeur connecté */
static char *LoadFile = NULL ;
/* Flag pour interdire l'ouverture de boite configuration */
int force_reconf = 1 ;
// Reinitialiser le fichier de logs
void logfile_reinit(void *handle) ;
void set_cmd_line( const char * st ) ;
/* Flag to manage first connection */
bool first_connect = true ;
/* Start a session with conf structure */
void RunSessionWithConfSettings( Conf *conf ) {
char b[2048];
char *cl=NULL;
const char *argprefix;
bool inherit_handles;
STARTUPINFO si;
PROCESS_INFORMATION pi;
HANDLE filemap = NULL;
if (restricted_acl)
argprefix = "&R";
else
argprefix = "";
/*
* Allocate a file-mapping memory chunk for the
* config structure.
*/
SECURITY_ATTRIBUTES sa;
strbuf *serbuf;
void *p;
int size;
serbuf = strbuf_new();
conf_serialise(BinarySink_UPCAST(serbuf), conf);
size = serbuf->len;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = true;
filemap = CreateFileMapping(INVALID_HANDLE_VALUE,&sa,PAGE_READWRITE,0, size, NULL);
if (filemap && filemap != INVALID_HANDLE_VALUE) {
p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, size);
if (p) {
memcpy(p, serbuf->s, size);
UnmapViewOfFile(p);
}
}
strbuf_free(serbuf);
inherit_handles = true;
cl = dupprintf("putty %s&%p:%u", argprefix,filemap, (unsigned)size);
GetModuleFileName(NULL, b, sizeof(b) - 1);
si.cb = sizeof(si);
si.lpReserved = NULL;
si.lpDesktop = NULL;
si.lpTitle = NULL;
si.dwFlags = 0;
si.cbReserved2 = 0;
si.lpReserved2 = NULL;
CreateProcess(b, cl, NULL, NULL, inherit_handles,NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
if (filemap)
CloseHandle(filemap);
sfree(cl);
}
/* Start new session with current settings */
void RunSessionWithCurrentSettings( HWND hwnd, Conf * oldconf, const char * host, const char * user, const char * pass, const int port, const char * remotepath ) {
Conf *newconf = conf_copy(oldconf) ;
if( host != NULL ) { conf_set_str(newconf,CONF_host,host) ; }
if( user != NULL ) { conf_set_str(newconf,CONF_username,user) ; }
if( pass != NULL ) { conf_set_str(newconf,CONF_password,pass) ; }
#ifndef MOD_NOPASSWORD
char pst[4096] ;
strcpy( pst, conf_get_str(newconf, CONF_password ) ) ;
MASKPASS(GetCryptSaltFlag(), pst);
if( pass != NULL ) { strcpy( pst, pass ) ; }
conf_set_str( newconf, CONF_password, pst ) ;
memset(pst,0,strlen(pst));
#else
conf_set_str( newconf, CONF_password, "" ) ;
#endif
if( remotepath != NULL ) {
char *buf=(char*)malloc(strlen(remotepath)+5);
sprintf(buf,"cd %s",remotepath);
conf_set_str(newconf,CONF_autocommand,buf) ;
} //else { conf_set_str(newconf,CONF_autocommand,"") ; }
//if( port != 0 ) { conf_set_int(newconf,CONF_port,port) ; } else { conf_set_int(newconf,CONF_port,22) ; }
if( conf_launchable(newconf) ) {
RunSessionWithConfSettings( newconf ) ;
} else {
save_settings("__STARTUP",newconf) ;
RunSession( hwnd, conf_get_str(conf,CONF_folder), "__STARTUP" ) ;
del_settings("__STARTUP");
}
conf_free( newconf ) ;
}
#ifdef MOD_INTEGRATED_KEYGEN
int WINAPI KeyGen_WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show);
#endif
#ifdef MOD_INTEGRATED_AGENT
int WINAPI Agent_WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show);
#endif
#endif
#ifdef MOD_WTS
typedef enum _WTS_VIRTUAL_CLASS { WTSVirtualClientData, WTSVirtualFileHandle } WTS_VIRTUAL_CLASS; // WTS_VIRTUAL_CLASS is not definned in file wtsapi32.h !!!
#include <wtsapi32.h>
#endif
#if (defined MOD_BACKGROUNDIMAGE) && (!defined FLJ)
#include "../../kitty_image.h"
#endif
#ifdef MOD_RECONNECT
static time_t last_reconnect = 0;
void SetConnBreakIcon( HWND hwnd ) ;
static void close_session(void *ignored_context);
#endif
#ifdef MOD_RUTTY
/* rutty: */
#define IDM_SCRIPT (0x5100)
#define IDM_SCRIPTSEND (0x5110)
#define IDM_SCRIPTHALT (0x5120)
#include "script.h"
ScriptData scriptdata;
#include "script_win.c"
#include "script_ahk.c"
#include "script.c"
/* rutty */
#endif
#ifdef MOD_PROXY
#include "kitty_proxy.h"
#endif
static int dbltime, lasttime, lastact;
static Mouse_Button lastbtn;
/* this allows xterm-style mouse handling. */
static bool send_raw_mouse = false;
static int wheel_accumulator = 0;
static bool pointer_indicates_raw_mouse = false;
static BusyStatus busy_status = BUSY_NOT;
static char *window_name, *icon_name;
static int compose_state = 0;
static UINT wm_mousewheel = WM_MOUSEWHEEL;
#define IS_HIGH_VARSEL(wch1, wch2) \
((wch1) == 0xDB40 && ((wch2) >= 0xDD00 && (wch2) <= 0xDDEF))
#define IS_LOW_VARSEL(wch) \
(((wch) >= 0x180B && (wch) <= 0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR */ \
((wch) >= 0xFE00 && (wch) <= 0xFE0F)) /* VARIATION SELECTOR 1-16 */
static bool wintw_setup_draw_ctx(TermWin *);
static void wintw_draw_text(TermWin *, int x, int y, wchar_t *text, int len,
unsigned long attrs, int lattrs, truecolour tc);
static void wintw_draw_cursor(TermWin *, int x, int y, wchar_t *text, int len,
unsigned long attrs, int lattrs, truecolour tc);
static void wintw_draw_trust_sigil(TermWin *, int x, int y);
static int wintw_char_width(TermWin *, int uc);
static void wintw_free_draw_ctx(TermWin *);
static void wintw_set_cursor_pos(TermWin *, int x, int y);
static void wintw_set_raw_mouse_mode(TermWin *, bool enable);
static void wintw_set_raw_mouse_mode_pointer(TermWin *, bool enable);
#ifdef MOD_PERSO
static void wintw_set_focus_reporting_mode(TermWin *, bool enable);
#endif
static void wintw_set_scrollbar(TermWin *, int total, int start, int page);
static void wintw_bell(TermWin *, int mode);
static void wintw_clip_write(
TermWin *, int clipboard, wchar_t *text, int *attrs,
truecolour *colours, int len, bool must_deselect);
static void wintw_clip_request_paste(TermWin *, int clipboard);
static void wintw_refresh(TermWin *);
static void wintw_request_resize(TermWin *, int w, int h);
static void wintw_set_title(TermWin *, const char *title);
static void wintw_set_icon_title(TermWin *, const char *icontitle);
static void wintw_set_minimised(TermWin *, bool minimised);
static void wintw_set_maximised(TermWin *, bool maximised);
static void wintw_move(TermWin *, int x, int y);
static void wintw_set_zorder(TermWin *, bool top);
static void wintw_palette_set(TermWin *, unsigned, unsigned, const rgb *);
static void wintw_palette_get_overrides(TermWin *, Terminal *);
static const TermWinVtable windows_termwin_vt = {
.setup_draw_ctx = wintw_setup_draw_ctx,
.draw_text = wintw_draw_text,
.draw_cursor = wintw_draw_cursor,
.draw_trust_sigil = wintw_draw_trust_sigil,
.char_width = wintw_char_width,
.free_draw_ctx = wintw_free_draw_ctx,
.set_cursor_pos = wintw_set_cursor_pos,
.set_raw_mouse_mode = wintw_set_raw_mouse_mode,
#ifdef MOD_PERSO
.set_focus_reporting_mode = wintw_set_focus_reporting_mode,
#endif
.set_raw_mouse_mode_pointer = wintw_set_raw_mouse_mode_pointer,
.set_scrollbar = wintw_set_scrollbar,
.bell = wintw_bell,
.clip_write = wintw_clip_write,
.clip_request_paste = wintw_clip_request_paste,
.refresh = wintw_refresh,
.request_resize = wintw_request_resize,
.set_title = wintw_set_title,
.set_icon_title = wintw_set_icon_title,
.set_minimised = wintw_set_minimised,
.set_maximised = wintw_set_maximised,
.move = wintw_move,
.set_zorder = wintw_set_zorder,
.palette_set = wintw_palette_set,
.palette_get_overrides = wintw_palette_get_overrides,
};
static TermWin wintw[1];
static HDC wintw_hdc;
static HICON trust_icon = INVALID_HANDLE_VALUE;
const bool share_can_be_downstream = true;
const bool share_can_be_upstream = true;
static bool is_utf8(void)
{
return ucsdata.line_codepage == CP_UTF8;
}
static bool win_seat_is_utf8(Seat *seat)
{
return is_utf8();
}
static char *win_seat_get_ttymode(Seat *seat, const char *mode)
{
return term_get_ttymode(term, mode);
}
static StripCtrlChars *win_seat_stripctrl_new(
Seat *seat, BinarySink *bs_out, SeatInteractionContext sic)
{
return stripctrl_new_term(bs_out, false, 0, term);
}
static size_t win_seat_output(
Seat *seat, bool is_stderr, const void *, size_t);
#ifdef MOD_RUTTY
/* rutty */
size_t win_seat_output_local(Seat *seat, bool is_stderr, const void *, size_t);
#endif
static bool win_seat_eof(Seat *seat);
static int win_seat_get_userpass_input(
Seat *seat, prompts_t *p, bufchain *input);
static void win_seat_notify_remote_exit(Seat *seat);
static void win_seat_connection_fatal(Seat *seat, const char *msg);
static void win_seat_update_specials_menu(Seat *seat);
static void win_seat_set_busy_status(Seat *seat, BusyStatus status);
static bool win_seat_set_trust_status(Seat *seat, bool trusted);
static bool win_seat_get_cursor_position(Seat *seat, int *x, int *y);
static bool win_seat_get_window_pixel_size(Seat *seat, int *x, int *y);
static const SeatVtable win_seat_vt = {
.output = win_seat_output,
#ifdef MOD_RUTTY
.output_local = win_seat_output_local, /* rutty */
#endif
.eof = win_seat_eof,
.get_userpass_input = win_seat_get_userpass_input,
.notify_remote_exit = win_seat_notify_remote_exit,
.connection_fatal = win_seat_connection_fatal,
.update_specials_menu = win_seat_update_specials_menu,
.get_ttymode = win_seat_get_ttymode,
.set_busy_status = win_seat_set_busy_status,
.verify_ssh_host_key = win_seat_verify_ssh_host_key,
.confirm_weak_crypto_primitive = win_seat_confirm_weak_crypto_primitive,
.confirm_weak_cached_hostkey = win_seat_confirm_weak_cached_hostkey,
.is_utf8 = win_seat_is_utf8,
.echoedit_update = nullseat_echoedit_update,
.get_x_display = nullseat_get_x_display,
.get_windowid = nullseat_get_windowid,
.get_window_pixel_size = win_seat_get_window_pixel_size,
.stripctrl_new = win_seat_stripctrl_new,
.set_trust_status = win_seat_set_trust_status,
.verbose = nullseat_verbose_yes,
.interactive = nullseat_interactive_yes,
.get_cursor_position = win_seat_get_cursor_position,
};
#ifdef MOD_FAR2L
// "static" removed by far2l extensions support patch
// we need to access wgs from terminal.c to open dialog boxes, etc
WinGuiSeat wgs = { .seat.vt = &win_seat_vt,
#else
static WinGuiSeat wgs = { .seat.vt = &win_seat_vt,
#endif
.logpolicy.vt = &win_gui_logpolicy_vt };
#ifdef MOD_PERSO
Terminal* GetTerminal() { return term ; }
void do_eventlog( const char * st ) { lp_eventlog(&wgs.logpolicy,st); }
#endif
static void start_backend(void)
{
const struct BackendVtable *vt;
char *error, *realhost;
int i;
/*
* Select protocol. This is farmed out into a table in a
* separate file to enable an ssh-free variant.
*/
vt = backend_vt_from_proto(conf_get_int(conf, CONF_protocol));
if (!vt) {
char *str = dupprintf("%s Internal Error", appname);
MessageBox(NULL, "Unsupported protocol number found",
str, MB_OK | MB_ICONEXCLAMATION);
sfree(str);
cleanup_exit(1);
}
seat_set_trust_status(&wgs.seat, true);
error = backend_init(vt, &wgs.seat, &backend, logctx, conf,
conf_get_str(conf, CONF_host),
conf_get_int(conf, CONF_port),
&realhost,
conf_get_bool(conf, CONF_tcp_nodelay),
conf_get_bool(conf, CONF_tcp_keepalives));
if (error) {
#ifdef MOD_RECONNECT
char *str = dupprintf("%s Error", appname);
char *msg = dupprintf("Unable to open connection to\n%s\n%s",
conf_dest(conf), error);
sfree(error);
if( GetAutoreconnectFlag() && conf_get_int(conf,CONF_failure_reconnect) && is_backend_first_connected ) {
lp_eventlog(&wgs.logpolicy, msg) ;
} else {
if( GetAutoreconnectFlag() && conf_get_int(conf,CONF_failure_reconnect) ) {
if( MessageBox(NULL, msg, str, MB_ICONERROR | MB_RETRYCANCEL | MB_DEFBUTTON2) == IDCANCEL ) {
sfree(str);
sfree(msg);
exit(0);
} else {
start_backend() ;
return ;
}
} else {
MessageBox(NULL, msg, str, MB_OK);
}
}
sfree(str);
sfree(msg);
if( GetAutoreconnectFlag() && conf_get_int(conf,CONF_failure_reconnect) && is_backend_first_connected ) {
SetConnBreakIcon(wgs.term_hwnd) ;
SetSSHConnected(0) ;
queue_toplevel_callback(close_session, NULL);
session_closed = true;
lp_eventlog(&wgs.logpolicy, "Unable to connect, trying to reconnect...") ;
SetTimer(wgs.term_hwnd, TIMER_RECONNECT, GetReconnectDelay()*1000, NULL) ;
return ;
}
else
#else
char *str = dupprintf("%s Error", appname);
char *msg = dupprintf("Unable to open connection to\n%s\n%s",
conf_dest(conf), error);
sfree(error);
MessageBox(NULL, msg, str, MB_ICONERROR | MB_OK);
sfree(str);
sfree(msg);
#endif
exit(0);
}
term_setup_window_titles(term, realhost);
sfree(realhost);
/*
* Connect the terminal to the backend for resize purposes.
*/
term_provide_backend(term, backend);
/*
* Set up a line discipline.
*/
ldisc = ldisc_create(conf, term, backend, &wgs.seat);
/*
* Destroy the Restart Session menu item. (This will return
* failure if it's already absent, as it will be the very first
* time we call this function. We ignore that, because as long
* as the menu item ends up not being there, we don't care
* whether it was us who removed it or not!)
*/
for (i = 0; i < lenof(popup_menus); i++) {
DeleteMenu(popup_menus[i].menu, IDM_RESTART, MF_BYCOMMAND);
}
session_closed = false;
#ifdef MOD_RUTTY
/* rutty: */
script_init(&scriptdata, conf);
if(conf_get_int(conf, CONF_script_mode) == SCRIPT_PLAY && !filename_is_null(conf_get_filename(conf, CONF_script_filename)))
script_sendfile(&scriptdata, conf_get_filename(conf, CONF_script_filename));
else if(conf_get_int(conf, CONF_script_mode) == SCRIPT_RECORD && !filename_is_null(conf_get_filename(conf, CONF_script_filename)))
script_record(&scriptdata, conf_get_filename(conf, CONF_script_filename));
/* rutty */
#endif
}
static void close_session(void *ignored_context)
{
char *newtitle;
int i;
session_closed = true;
newtitle = dupprintf("%s (inactive)", appname);
#ifdef MOD_PERSO
newtitle = dupprintf("%s (inactive)", conf_get_str(conf,CONF_wintitle));
#endif
win_set_icon_title(wintw, newtitle);
win_set_title(wintw, newtitle);
sfree(newtitle);
if (ldisc) {
ldisc_free(ldisc);
ldisc = NULL;
}
if (backend) {
backend_free(backend);
backend = NULL;
term_provide_backend(term, NULL);
seat_update_specials_menu(&wgs.seat);
}
/*
* Show the Restart Session menu item. Do a precautionary
* delete first to ensure we never end up with more than one.
*/
for (i = 0; i < lenof(popup_menus); i++) {
DeleteMenu(popup_menus[i].menu, IDM_RESTART, MF_BYCOMMAND);
InsertMenu(popup_menus[i].menu, IDM_DUPSESS, MF_BYCOMMAND | MF_ENABLED,
IDM_RESTART, "&Restart Session");
}
}
#ifdef MOD_RECONNECT
void RestartSession( void ) {
if( backend ) {
SetSSHConnected(0) ;
queue_toplevel_callback(close_session, NULL) ; backend = NULL ;
}
if( GetAutoreconnectFlag() ) {
lp_eventlog(&wgs.logpolicy, "User request session restart..." ) ;
} else {
win_seat_connection_fatal( &wgs.seat, "User request session restart..." ) ;
SetTimer(wgs.term_hwnd, TIMER_RECONNECT, 10, NULL) ;
}
#ifdef MOD_FAR2L
term->far2l_ext = 0;
#endif
PostMessage(wgs.term_hwnd,WM_KEYDOWN,VK_RETURN ,0) ;
PostMessage(wgs.term_hwnd,WM_KEYUP,VK_RETURN ,1) ;
}
#endif
const unsigned cmdline_tooltype =
TOOLTYPE_HOST_ARG |
TOOLTYPE_PORT_ARG |
TOOLTYPE_NO_VERBOSE_OPTION;
HINSTANCE hinst;
int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
{
MSG msg;
HRESULT hr;
int guess_width, guess_height;
dll_hijacking_protection();
#ifdef MOD_ZMODEM
struct netscheduler_tag *netsc = NULL ;
#endif
hinst = inst;
sk_init();
init_common_controls();
/* Set Explicit App User Model Id so that jump lists don't cause
PuTTY to hang on to removable media. */
set_explicit_app_user_model_id();
/* Ensure a Maximize setting in Explorer doesn't maximise the
* config box. */
defuse_showwindow();
init_winver();
/*
* If we're running a version of Windows that doesn't support
* WM_MOUSEWHEEL, find out what message number we should be
* using instead.
*/
if (osMajorVersion < 4 ||
(osMajorVersion == 4 && osPlatformId != VER_PLATFORM_WIN32_NT))
wm_mousewheel = RegisterWindowMessage("MSWHEEL_ROLLMSG");
init_help();
init_winfuncs();
conf = conf_new();
#ifdef MOD_HYPERLINK
urlhack_init();
#endif
#ifdef MOD_PERSO
// Initialisation specifique a KiTTY
set_cmd_line( cmdline ) ;
SethInstIcons( hinst ) ;
InitWinMain();
if( debug_flag ) {
if( IniFileFlag == SAVEMODE_REG ) { debug_logevent( "Save mode is set to registry (classic)" ) ; }
if( IniFileFlag == SAVEMODE_DIR ) { debug_logevent( "Save mode is set to directory (portable)" ) ; }
debug_logevent( "Command-line: %s", cmdline ) ;
debug_logevent( "Version: %s", BuildVersionTime ) ;
}
#endif
#ifdef MOD_NOTRANSPARENCY
SetTransparencyFlag(0);
#endif
/*
* Initialize COM.
*/
hr = CoInitialize(NULL);
if (hr != S_OK && hr != S_FALSE) {
char *str = dupprintf("%s Fatal Error", appname);
MessageBox(NULL, "Failed to initialize COM subsystem",
str, MB_OK | MB_ICONEXCLAMATION);
sfree(str);
return 1;
}
/*
* Process the command line.
*/
{
char *p;
bool special_launchable_argument = false;
settings_set_default_protocol(be_default_protocol);
/* Find the appropriate default port. */
{
const struct BackendVtable *vt =
backend_vt_from_proto(be_default_protocol);
settings_set_default_port(0); /* illegal */
if (vt)
settings_set_default_port(vt->default_port);
}
conf_set_int(conf, CONF_logtype, LGTYP_NONE);
do_defaults(NULL, conf);
p = cmdline;
/*
* Process a couple of command-line options which are more
* easily dealt with before the line is broken up into words.
* These are the old-fashioned but convenient @sessionname and
* the internal-use-only &sharedmemoryhandle, plus the &R
* prefix for -restrict-acl, all of which are used by PuTTYs
* auto-launching each other via System-menu options.
*/
while (*p && isspace(*p))
p++;
if (*p == '&' && p[1] == 'R' &&
(!p[2] || p[2] == '@' || p[2] == '&')) {
/* &R restrict-acl prefix */
restrict_process_acl();
p += 2;
}
if (*p == '@') {
/*
* An initial @ means that the whole of the rest of the
* command line should be treated as the name of a saved
* session, with _no quoting or escaping_. This makes it a
* very convenient means of automated saved-session
* launching, via IDM_SAVEDSESS or Windows 7 jump lists.
*/
int i = strlen(p);
while (i > 1 && isspace(p[i - 1]))
i--;
p[i] = '\0';
#ifdef MOD_PERSO
if( GetDirectoryBrowseFlag() ) {
char *pst = strstr( p+1, "/" ) ;
if( pst!=NULL ) {
pst[0]='\0';
char *name = (char*)malloc( strlen(p)+1 ) ;
strcpy( name, p+1 ) ;
conf_set_str(conf,CONF_folder,name) ;
SetSessPath( name ) ;
SetInitCurrentFolder( name ) ;
free(name) ;
pst[0]='@' ;
p=pst;
}
char * pfolder ;
if( (pfolder=strstr(p+1," -folder \"")) ) {
pfolder[0]='\0';
pfolder += 10 ;
pfolder[strlen(pfolder)-1]='\0';
SetSessPath( pfolder ) ;
SetInitCurrentFolder( pfolder ) ;
}
}
#endif
do_defaults(p + 1, conf);
if (!conf_launchable(conf) && !do_config(conf)) {
cleanup_exit(0);
}
special_launchable_argument = true;
} else if (*p == '&') {
/*
* An initial & means we've been given a command line
* containing the hex value of a HANDLE for a file
* mapping object, which we must then interpret as a
* serialised Conf.
*/
HANDLE filemap;
void *cp;
unsigned cpsize;
if (sscanf(p + 1, "%p:%u", &filemap, &cpsize) == 2 &&
(cp = MapViewOfFile(filemap, FILE_MAP_READ,
0, 0, cpsize)) != NULL) {
BinarySource src[1];
BinarySource_BARE_INIT(src, cp, cpsize);
if (!conf_deserialise(conf, src))
modalfatalbox("Serialised configuration data was invalid");
UnmapViewOfFile(cp);
CloseHandle(filemap);
} else if (!do_config(conf)) {
cleanup_exit(0);
}
special_launchable_argument = true;
} else if (!*p) {
/* Do-nothing case for an empty command line - or rather,
* for a command line that's empty _after_ we strip off
* the &R prefix. */
} else {
/*
* Otherwise, break up the command line and deal with
* it sensibly.
*/
int argc, i;
char **argv;