forked from cy384/ssheven
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssheven.c
1369 lines (1140 loc) · 35.2 KB
/
ssheven.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
/*
* ssheven
*
* Copyright (c) 2020 by cy384 <[email protected]>
* See LICENSE file for details
*/
#include "ssheven.h"
#include "ssheven-console.h"
#include "ssheven-net.h"
#include "ssheven-debug.h"
#include <Threads.h>
#include <MacMemory.h>
#include <Quickdraw.h>
#include <Fonts.h>
#include <Windows.h>
#include <Sound.h>
#include <Gestalt.h>
#include <Devices.h>
#include <Scrap.h>
#include <Controls.h>
#include <ControlDefinitions.h>
#include <stdio.h>
// sinful globals
struct ssheven_console con = { NULL, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, CLICK_SELECT, NULL, NULL };
struct ssheven_ssh_connection ssh_con = { NULL, NULL, kOTInvalidEndpointRef, NULL, NULL };
struct preferences prefs;
enum THREAD_COMMAND read_thread_command = WAIT;
enum THREAD_STATE read_thread_state = UNINITIALIZED;
const uint8_t ascii_to_control_code[256] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 27, 28, 29, 30, 31, 0, 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, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};
// maps virtual keycodes to (hopefully) ascii characters
// this will probably be weird for non-letter keypresses
uint8_t keycode_to_ascii[256] = {0};
void generate_key_mapping(void)
{
// TODO this sucks
// gets the currently loaded keymap resource
// passes in the virtual keycode but without any previous state or modifiers
// ignores the second byte if we're currently using a multibyte lang
void* kchr = (void*)GetScriptManagerVariable(smKCHRCache);
for (uint16_t i = 0; i < 256; i++)
{
keycode_to_ascii[i] = KeyTranslate(kchr, i, 0) & 0xff;
}
}
void set_window_title(WindowPtr w, const char* c_name, size_t length)
{
Str255 pascal_name;
strncpy((char *) &pascal_name[1], c_name, 254);
pascal_name[0] = length < 254 ? length : 254;
SetWTitle(w, pascal_name);
}
void set_terminal_string(void)
{
/*
* terminal type to send over ssh, determines features etc. some good options:
* "vanilla" supports basically nothing
* "vt100" just the basics
* "xterm" everything
* "xterm-mono" everything except color
* "xterm-16color" classic 16 ANSI colors only
*/
switch (prefs.display_mode)
{
case FASTEST:
prefs.terminal_string = "xterm-mono";
break;
case COLOR:
prefs.terminal_string = "xterm-16color";
break;
default:
prefs.terminal_string = SSHEVEN_DEFAULT_TERM_STRING;
break;
}
}
int save_prefs(void)
{
int ok = 1;
short foundVRefNum = 0;
long foundDirID = 0;
FSSpec pref_file;
short prefRefNum = 0;
OSType pref_type = 'SH7p';
OSType creator_type = 'SSH7';
// find the preferences folder on the system disk, create folder if needed
OSErr e = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder, &foundVRefNum, &foundDirID);
if (e != noErr) ok = 0;
// make an FSSpec for the new file we want to make
if (ok)
{
e = FSMakeFSSpec(foundVRefNum, foundDirID, PREFERENCES_FILENAME, &pref_file);
// if the file exists, delete it
if (e == noErr) FSpDelete(&pref_file);
// and then make it
e = FSpCreate(&pref_file, creator_type, pref_type, smSystemScript);
if (e != noErr) ok = 0;
}
// open the file
if (ok)
{
e = FSpOpenDF(&pref_file, fsRdWrPerm, &prefRefNum);
if (e != noErr) ok = 0;
}
// write prefs to the file
if (ok)
{
// TODO: choose buffer size more effectively
size_t write_length = 8192;
char* output_buffer = malloc(write_length);
memset(output_buffer, 0, write_length);
long int i = snprintf(output_buffer, write_length, "%d\n%d\n", prefs.major_version, prefs.minor_version);
i += snprintf(output_buffer+i, write_length-i, "%d\n%d\n%d\n%d\n%d\n", (int)prefs.auth_type, (int)prefs.display_mode, (int)prefs.fg_color, (int)prefs.bg_color, (int)prefs.font_size);
snprintf(output_buffer+i, prefs.hostname[0]+1, "%s", prefs.hostname+1); i += prefs.hostname[0];
i += snprintf(output_buffer+i, write_length-i, "\n");
snprintf(output_buffer+i, prefs.username[0]+1, "%s", prefs.username+1); i += prefs.username[0];
i += snprintf(output_buffer+i, write_length-i, "\n");
snprintf(output_buffer+i, prefs.port[0]+1, "%s", prefs.port+1); i += prefs.port[0];
i += snprintf(output_buffer+i, write_length-i, "\n");
if (prefs.privkey_path && prefs.privkey_path[0] != '\0')
{
i += snprintf(output_buffer+i, write_length-i, "%s\n%s\n", prefs.privkey_path, prefs.pubkey_path);
}
else
{
i += snprintf(output_buffer+i, write_length-i, "\n\n");
}
// tell it to write all bytes
long int bytes = i;
e = FSWrite(prefRefNum, &bytes, output_buffer);
// FSWrite sets bytes to the actual number of bytes written
if (e != noErr || (bytes != i)) ok = 0;
}
// close the file
if (prefRefNum != 0)
{
e = FSClose(prefRefNum);
if (e != noErr) ok = 0;
}
return ok;
}
// check if the main device is black and white
int detect_color_screen(void)
{
return TestDeviceAttribute(GetMainDevice(), gdDevType);
}
void init_prefs(void)
{
// initialize everything to a safe default
prefs.major_version = SSHEVEN_VERSION_MAJOR;
prefs.minor_version = SSHEVEN_VERSION_MINOR;
memset(&(prefs.hostname), 0, 512);
memset(&(prefs.username), 0, 256);
memset(&(prefs.password), 0, 256);
memset(&(prefs.port), 0, 256);
// default port: 22
prefs.port[0] = 2;
prefs.port[1] = '2';
prefs.port[2] = '2';
prefs.pubkey_path = "";
prefs.privkey_path = "";
prefs.terminal_string = SSHEVEN_DEFAULT_TERM_STRING;
prefs.auth_type = USE_PASSWORD;
prefs.display_mode = detect_color_screen() ? COLOR : FASTEST;
prefs.fg_color = blackColor;
prefs.bg_color = whiteColor;
prefs.font_size = 9;
prefs.loaded_from_file = 0;
}
void load_prefs(void)
{
// now try to load preferences from the file
short foundVRefNum = 0;
long foundDirID = 0;
FSSpec pref_file;
short prefRefNum = 0;
// find the preferences folder on the system disk
OSErr e = FindFolder(kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder, &foundVRefNum, &foundDirID);
if (e != noErr) return;
// make an FSSpec for the preferences file location and check if it exists
// TODO: if I just put PREFERENCES_FILENAME it doesn't work, wtf
e = FSMakeFSSpec(foundVRefNum, foundDirID, "\pssheven Preferences", &pref_file);
if (e == fnfErr) // file not found, nothing to load
{
return;
}
else if (e != noErr) return;
e = FSpOpenDF(&pref_file, fsCurPerm, &prefRefNum);
if (e != noErr) return;
// actually read and parse the file
long int buffer_size = 8192;
char* buffer = NULL;
buffer = malloc(buffer_size);
prefs.privkey_path = malloc(2048);
prefs.pubkey_path = malloc(2048);
prefs.pubkey_path[0] = '\0';
prefs.privkey_path[0] = '\0';
prefs.hostname[0] = 0;
prefs.username[0] = 0;
prefs.port[0] = 0;
e = FSRead(prefRefNum, &buffer_size, buffer);
e = FSClose(prefRefNum);
// check the version (first two numbers)
int items_got = sscanf(buffer, "%d\n%d", &prefs.major_version, &prefs.minor_version);
if (items_got != 2) return;
// only load a prefs file if the saved version number matches ours
if ((prefs.major_version == SSHEVEN_VERSION_MAJOR) && (prefs.minor_version == SSHEVEN_VERSION_MINOR))
{
prefs.loaded_from_file = 1;
items_got = sscanf(buffer, "%d\n%d\n%d\n%d\n%d\n%d\n%d\n%255[^\n]\n%255[^\n]\n%255[^\n]\n%[^\n]\n%[^\n]", &prefs.major_version, &prefs.minor_version, (int*)&prefs.auth_type, (int*)&prefs.display_mode, &prefs.fg_color, &prefs.bg_color, &prefs.font_size, prefs.hostname+1, prefs.username+1, prefs.port+1, prefs.privkey_path, prefs.pubkey_path);
// add the size for the pascal strings
prefs.hostname[0] = (unsigned char)strlen(prefs.hostname+1);
prefs.username[0] = (unsigned char)strlen(prefs.username+1);
prefs.port[0] = (unsigned char)strlen(prefs.port+1);
set_terminal_string();
}
else
{
prefs.major_version = SSHEVEN_VERSION_MAJOR;
prefs.minor_version = SSHEVEN_VERSION_MINOR;
}
if (buffer) free(buffer);
}
// borrowed from Retro68 sample code
// draws the "default" indicator around a button
pascal void ButtonFrameProc(DialogRef dlg, DialogItemIndex itemNo)
{
DialogItemType type;
Handle itemH;
Rect box;
GetDialogItem(dlg, 1, &type, &itemH, &box);
InsetRect(&box, -4, -4);
PenSize(3, 3);
FrameRoundRect(&box, 16, 16);
}
void display_about_box(void)
{
DialogRef about = GetNewDialog(DLOG_ABOUT, 0, (WindowPtr)-1);
UpdateDialog(about, about->visRgn);
while (!Button()) YieldToAnyThread();
while (Button()) YieldToAnyThread();
FlushEvents(everyEvent, 0);
DisposeWindow(about);
}
void ssh_paste(void)
{
// GetScrap requires a handle, not a raw buffer
// it will increase the size of the handle if needed
Handle buf = NewHandle(256);
int r = GetScrap(buf, 'TEXT', 0);
if (r > 0)
{
ssh_write(*buf, r);
}
DisposeHandle(buf);
}
void ssh_copy(void)
{
char* selection = NULL;
size_t len = get_selection(&selection);
if (selection == NULL || len == 0) return;
OSErr e = ZeroScrap();
if (e != noErr) printf_i("Failed to ZeroScrap!");
e = PutScrap(len, 'TEXT', selection);
if (e != noErr) printf_i("Failed to PutScrap!");
}
int qd_color_to_menu_item(int qd_color)
{
switch (qd_color)
{
case blackColor: return 1;
case redColor: return 2;
case greenColor: return 3;
case yellowColor: return 4;
case blueColor: return 5;
case magentaColor: return 6;
case cyanColor: return 7;
case whiteColor: return 8;
default: return 1;
}
}
int menu_item_to_qd_color(int menu_item)
{
switch (menu_item)
{
case 1: return blackColor;
case 2: return redColor;
case 3: return greenColor;
case 4: return yellowColor;
case 5: return blueColor;
case 6: return magentaColor;
case 7: return cyanColor;
case 8: return whiteColor;
default: return 1;
}
}
int font_size_to_menu_item(int font_size)
{
switch (font_size)
{
case 9: return 1;
case 10: return 2;
case 12: return 3;
case 14: return 4;
case 18: return 5;
case 24: return 6;
case 36: return 7;
default: return 1;
}
}
int menu_item_to_font_size(int menu_item)
{
switch (menu_item)
{
case 1: return 9;
case 2: return 10;
case 3: return 12;
case 4: return 14;
case 5: return 18;
case 6: return 24;
case 7: return 36;
default: return 9;
}
}
void preferences_window(void)
{
// modal dialog setup
TEInit();
InitDialogs(NULL);
DialogPtr dlg = GetNewDialog(DLOG_PREFERENCES, 0, (WindowPtr)-1);
InitCursor();
// select all text in dialog item 4 (the hostname one)
//SelectDialogItemText(dlg, 4, 0, 32767);
DialogItemType type;
Handle itemH;
Rect box;
// draw default button indicator around the connect button
GetDialogItem(dlg, 2, &type, &itemH, &box);
SetDialogItem(dlg, 2, type, (Handle)NewUserItemUPP(&ButtonFrameProc), &box);
// get the handles for each menu, set to current prefs value
ControlHandle term_type_menu;
GetDialogItem(dlg, 6, &type, &itemH, &box);
term_type_menu = (ControlHandle)itemH;
SetControlValue(term_type_menu, prefs.display_mode + 1);
ControlHandle bg_color_menu;
GetDialogItem(dlg, 7, &type, &itemH, &box);
bg_color_menu = (ControlHandle)itemH;
SetControlValue(bg_color_menu, qd_color_to_menu_item(prefs.bg_color));
ControlHandle fg_color_menu;
GetDialogItem(dlg, 8, &type, &itemH, &box);
fg_color_menu = (ControlHandle)itemH;
SetControlValue(fg_color_menu, qd_color_to_menu_item(prefs.fg_color));
ControlHandle font_size_menu;
GetDialogItem(dlg, 10, &type, &itemH, &box);
font_size_menu = (ControlHandle)itemH;
SetControlValue(font_size_menu, font_size_to_menu_item(prefs.font_size));
// let the modalmanager do everything
// stop on ok or cancel
short item;
do {
ModalDialog(NULL, &item);
} while(item != 1 && item != 11);
// save if OK'd
if (item == 1)
{
// read menu values into prefs
prefs.display_mode = GetControlValue(term_type_menu) - 1;
// TODO: don't save colors, make it take effect immediately
int save_bg = prefs.bg_color;
int save_fg = prefs.fg_color;
prefs.bg_color = menu_item_to_qd_color(GetControlValue(bg_color_menu));
prefs.fg_color = menu_item_to_qd_color(GetControlValue(fg_color_menu));
int new_font_size = menu_item_to_font_size(GetControlValue(font_size_menu));
// resize window if font size changed
if (new_font_size != prefs.font_size)
{
prefs.font_size = new_font_size;
font_size_change();
}
save_prefs();
prefs.bg_color = save_bg;
prefs.fg_color = save_fg;
// TODO: make this actually fix all colors in vterm
update_console_colors();
}
// clean it up
DisposeDialog(dlg);
FlushEvents(everyEvent, -1);
}
// returns 1 if quit selected, else 0
int process_menu_select(int32_t result)
{
int exit = 0;
int16_t menu = (result & 0xFFFF0000) >> 16;
int16_t item = (result & 0x0000FFFF);
Str255 name;
switch (menu)
{
case MENU_APPLE:
if (item == 1)
{
display_about_box();
}
else
{
GetMenuItemText(GetMenuHandle(menu), item, name);
OpenDeskAcc(name);
}
break;
case MENU_FILE:
if (item == 1)
{
if (connect() == 0) disconnect();
}
if (item == 2) disconnect();
if (item == 4) preferences_window();
if (item == 6) exit = 1;
break;
case MENU_EDIT:
if (item == 4) ssh_copy();
if (item == 5) ssh_paste();
break;
default:
break;
}
HiliteMenu(0);
return exit;
}
void resize_con_window(WindowPtr eventWin, EventRecord event)
{
clear_selection();
// TODO: put this somewhere else
// limits on window size
// top = min vertical
// bottom = max vertical
// left = min horizontal
// right = max horizontal
Rect window_limits = { .top = con.cell_height*2 + 2,
.bottom = con.cell_height*100 + 2,
.left = con.cell_width*10 + 4,
.right = con.cell_width*200 + 4 };
long growResult = GrowWindow(eventWin, event.where, &window_limits);
if (growResult != 0)
{
int height = growResult >> 16;
int width = growResult & 0xFFFF;
// 'snap' to a size that won't have extra pixels not in a cell
int next_height = height - ((height - 4) % con.cell_height);
int next_width = width - ((width - 4) % con.cell_width);
SizeWindow(eventWin, next_width, next_height, true);
EraseRect(&(con.win->portRect));
InvalRect(&(con.win->portRect));
con.size_x = (next_width - 4)/con.cell_width;
con.size_y = (next_height - 2)/con.cell_height;
vterm_set_size(con.vterm, con.size_y, con.size_x);
libssh2_channel_request_pty_size(ssh_con.channel, con.size_x, con.size_y);
}
}
int handle_keypress(EventRecord* event)
{
unsigned char c = event->message & charCodeMask;
// if we have a key and command, and it's not autorepeating
if (c && event->what != autoKey && event->modifiers & cmdKey)
{
switch (c)
{
case 'k':
if (read_thread_state == UNINITIALIZED || read_thread_state == DONE)
{
if (connect() == 0) disconnect();
}
break;
case 'd':
if (read_thread_state == OPEN) disconnect();
break;
case 'q':
return 1;
break;
case 'v':
ssh_paste();
break;
case 'c':
ssh_copy();
break;
default:
break;
}
}
else if (c)
{
// get the unmodified version of the keypress
uint8_t unmodified_key = keycode_to_ascii[(event->message & keyCodeMask)>>8];
// if we have a control code for this key
if (event->modifiers & controlKey && ascii_to_control_code[unmodified_key] != 255)
{
ssh_con.send_buffer[0] = ascii_to_control_code[unmodified_key];
ssh_write(ssh_con.send_buffer, 1);
}
else
{
// if we have alt and the character would be printable ascii, use it
if (event->modifiers & optionKey && c >= 32 && c <= 126)
{
ssh_con.send_buffer[0] = c;
ssh_write(ssh_con.send_buffer, 1);
}
else
{
// otherwise manually send an escape if we have alt held
if (event->modifiers & optionKey)
{
ssh_con.send_buffer[0] = '\e';
ssh_write(ssh_con.send_buffer, 1);
}
if (key_to_vterm[c] != VTERM_KEY_NONE)
{
// doesn't seem like vterm does modifiers properly, so don't bother
vterm_keyboard_key(con.vterm, key_to_vterm[c], VTERM_MOD_NONE);
}
else
{
ssh_con.send_buffer[0] = event->modifiers & optionKey ? unmodified_key : c;
ssh_write(ssh_con.send_buffer, 1);
}
}
}
}
return 0;
}
void event_loop(void)
{
int exit_event_loop = 0;
EventRecord event;
WindowPtr eventWin;
Point local_mouse_position;
// maximum length of time to sleep (in ticks)
// GetCaretTime gets the number of ticks between system caret on/off time
long int sleep_time = GetCaretTime() / 4;
do
{
// wait to get a GUI event
while (!WaitNextEvent(everyEvent, &event, sleep_time, NULL))
{
YieldToAnyThread();
check_cursor();
BeginUpdate(con.win);
draw_screen(&(con.win->portRect));
EndUpdate(con.win);
}
// might need to toggle our cursor even if we got an event
check_cursor();
// handle any GUI events
switch (event.what)
{
case updateEvt:
eventWin = (WindowPtr)event.message;
BeginUpdate(eventWin);
draw_screen(&(eventWin->portRect));
EndUpdate(eventWin);
break;
case keyDown:
case autoKey: // autokey means we're repeating a held down key event
exit_event_loop = handle_keypress(&event);
break;
case mouseDown:
switch(FindWindow(event.where, &eventWin))
{
case inDrag:
// allow the window to be dragged anywhere on any monitor
// hmmm... which of these is better???
DragWindow(eventWin, event.where, &(*(GetGrayRgn()))->rgnBBox);
// DragWindow(eventWin, event.where, &(*qd.thePort->visRgn)->rgnBBox);
break;
case inGrow:
resize_con_window(eventWin, event);
break;
case inGoAway:
{
if (TrackGoAway(eventWin, event.where))
exit_event_loop = 1;
}
break;
case inMenuBar:
exit_event_loop = process_menu_select(MenuSelect(event.where));
break;
case inSysWindow:
// is this system6 relevant only???
SystemClick(&event, eventWin);
break;
case inContent:
GetMouse(&local_mouse_position);
mouse_click(local_mouse_position, true);
break;
}
break;
case mouseUp:
// only tell the console to lift the mouse if we clicked through it
if (con.mouse_state)
{
GetMouse(&local_mouse_position);
mouse_click(local_mouse_position, false);
}
break;
}
YieldToAnyThread();
} while (!exit_event_loop);
}
// from the ATS password sample code
pascal Boolean TwoItemFilter(DialogPtr dlog, EventRecord *event, short *itemHit)
{
DialogPtr evtDlog;
short selStart, selEnd;
long unsigned ticks;
Handle itemH;
DialogItemType type;
Rect box;
// TODO: this should be declared somewhere? include it?
int kVisualDelay = 8;
if (event->what == keyDown || event->what == autoKey)
{
char c = event->message & charCodeMask;
switch (c)
{
case kEnterCharCode: // select the ok button
case kReturnCharCode: // we have to manually blink it...
case kLineFeedCharCode:
GetDialogItem(dlog, 1, &type, &itemH, &box);
HiliteControl((ControlHandle)(itemH), kControlButtonPart);
Delay(kVisualDelay, &ticks);
HiliteControl((ControlHandle)(itemH), 1);
*itemHit = 1;
return true;
case kTabCharCode: // cancel out tab events
event->what = nullEvent;
return false;
case kEscapeCharCode: // hit cancel on esc or cmd-period
case '.':
if ((event->modifiers & cmdKey) || c == kEscapeCharCode)
{
GetDialogItem(dlog, 6, &type, &itemH, &box);
HiliteControl((ControlHandle)(itemH), kControlButtonPart);
Delay(kVisualDelay, &ticks);
HiliteControl((ControlHandle)(itemH), 6);
*itemHit = 6;
return true;
}
__attribute__ ((fallthrough)); // fall through in case of a plain '.'
default: // TODO: this is dumb and assumes everything else is a valid text character
selStart = (**((DialogPeek)dlog)->textH).selStart; // Get the selection in the visible item
selEnd = (**((DialogPeek)dlog)->textH).selEnd;
SelectDialogItemText(dlog, 5, selStart, selEnd); // Select text in invisible item
DialogSelect(event, &evtDlog, itemHit); // Input key
SelectDialogItemText(dlog, 4, selStart, selEnd); // Select same area in visible item
if ((event->message & charCodeMask) != kBackspaceCharCode) // If it's not a backspace (backspace is the only key that can affect both the text and the selection- thus we need to process it in both fields, but not change it for the hidden field.
event->message = 0xa5; // Replace with character to use (the bullet)
DialogSelect(event, &evtDlog, itemHit); // Put in fake character
return true;
case kLeftArrowCharCode:
case kRightArrowCharCode:
case kUpArrowCharCode:
case kDownArrowCharCode:
case kHomeCharCode:
case kEndCharCode:
return false; // don't handle them
}
}
return false; // pass on other (non-keypress) events
}
// from the ATS password sample code
// 1 for ok, 0 for cancel
int password_dialog(int dialog_resource)
{
int ret = 1;
DialogPtr dlog;
Handle itemH;
short item;
Rect box;
DialogItemType type;
dlog = GetNewDialog(dialog_resource, 0, (WindowPtr) - 1);
// draw default button indicator around the connect button
GetDialogItem(dlog, 2, &type, &itemH, &box);
SetDialogItem(dlog, 2, type, (Handle)NewUserItemUPP(&ButtonFrameProc), &box);
do {
ModalDialog(NewModalFilterUPP(TwoItemFilter), &item);
} while (item != 1 && item != 6); // until OK or cancel
if (6 == item) ret = 0;
// read out of the hidden text box
GetDialogItem(dlog, 5, &type, &itemH, &box);
GetDialogItemText(itemH, (unsigned char*)prefs.password);
prefs.auth_type = USE_PASSWORD;
DisposeDialog(dlog);
return ret;
}
// derived from More Files sample code
OSErr
FSpPathFromLocation(
FSSpec *spec, /* The location we want a path for. */
int *length, /* Length of the resulting path. */
Handle *fullPath) /* Handle to path. */
{
OSErr err;
FSSpec tempSpec;
CInfoPBRec pb;
*fullPath = NULL;
/*
* Make a copy of the input FSSpec that can be modified.
*/
BlockMoveData(spec, &tempSpec, sizeof(FSSpec));
if (tempSpec.parID == fsRtParID) {
/*
* The object is a volume. Add a colon to make it a full
* pathname. Allocate a handle for it and we are done.
*/
tempSpec.name[0] += 2;
tempSpec.name[tempSpec.name[0] - 1] = ':';
tempSpec.name[tempSpec.name[0]] = '\0';
err = PtrToHand(&tempSpec.name[1], fullPath, tempSpec.name[0]);
} else {
/*
* The object isn't a volume. Is the object a file or a directory?
*/
pb.dirInfo.ioNamePtr = tempSpec.name;
pb.dirInfo.ioVRefNum = tempSpec.vRefNum;
pb.dirInfo.ioDrDirID = tempSpec.parID;
pb.dirInfo.ioFDirIndex = 0;
err = PBGetCatInfoSync(&pb);
if ((err == noErr) || (err == fnfErr)) {
/*
* If the file doesn't currently exist we start over. If the
* directory exists everything will work just fine. Otherwise we
* will just fail later. If the object is a directory, append a
* colon so full pathname ends with colon.
*/
if (err == fnfErr) {
BlockMoveData(spec, &tempSpec, sizeof(FSSpec));
} else if ( (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0 ) {
tempSpec.name[0] += 1;
tempSpec.name[tempSpec.name[0]] = ':';
}
/*
* Create a new Handle for the object - make it a C string
*/
tempSpec.name[0] += 1;
tempSpec.name[tempSpec.name[0]] = '\0';
err = PtrToHand(&tempSpec.name[1], fullPath, tempSpec.name[0]);
if (err == noErr) {
/*
* Get the ancestor directory names - loop until we have an
* error or find the root directory.
*/
pb.dirInfo.ioNamePtr = tempSpec.name;
pb.dirInfo.ioVRefNum = tempSpec.vRefNum;
pb.dirInfo.ioDrParID = tempSpec.parID;
do {
pb.dirInfo.ioFDirIndex = -1;
pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
err = PBGetCatInfoSync(&pb);
if (err == noErr) {
/*
* Append colon to directory name and add
* directory name to beginning of fullPath
*/
++tempSpec.name[0];
tempSpec.name[tempSpec.name[0]] = ':';
(void) Munger(*fullPath, 0, NULL, 0, &tempSpec.name[1],
tempSpec.name[0]);
err = MemError();
}
} while ( (err == noErr) && (pb.dirInfo.ioDrDirID != fsRtDirID) );
}
}
}
/*
* On error Dispose the handle, set it to NULL & return the err.
* Otherwise, set the length & return.
*/
if (err == noErr) {
*length = GetHandleSize(*fullPath) - 1;
} else {
if ( *fullPath != NULL ) {
DisposeHandle(*fullPath);
}
*fullPath = NULL;
*length = 0;
}
return err;
}
int key_dialog(void)
{
Handle full_path = NULL;
int path_length = 0;
// if we don't have a saved pubkey path, ask for one
if (prefs.pubkey_path == NULL || prefs.pubkey_path[0] == '\0')
{
NoteAlert(ALRT_PUBKEY, nil);
StandardFileReply pubkey;
StandardGetFile(NULL, 0, NULL, &pubkey);
FSpPathFromLocation(&pubkey.sfFile, &path_length, &full_path);
prefs.pubkey_path = malloc(path_length+1);
strncpy(prefs.pubkey_path, (char*)(*full_path), path_length+1);
DisposeHandle(full_path);
// if the user hit cancel, 0
if (!pubkey.sfGood) return 0;
}
path_length = 0;
full_path = NULL;
// if we don't have a saved privkey path, ask for one
if (prefs.privkey_path == NULL || prefs.privkey_path[0] == '\0')
{
NoteAlert(ALRT_PRIVKEY, nil);
StandardFileReply privkey;
StandardGetFile(NULL, 0, NULL, &privkey);
FSpPathFromLocation(&privkey.sfFile, &path_length, &full_path);
prefs.privkey_path = malloc(path_length+1);
strncpy(prefs.privkey_path, (char*)(*full_path), path_length+1);
DisposeHandle(full_path);
// if the user hit cancel, 0
if (!privkey.sfGood) return 0;
}
// get the key decryption password
if (!password_dialog(DLOG_KEY_PASSWORD)) return 0;
prefs.auth_type = USE_KEY;
return 1;
}
int intro_dialog(void)
{
// modal dialog setup
TEInit();
InitDialogs(NULL);
DialogPtr dlg = GetNewDialog(DLOG_CONNECT, 0, (WindowPtr)-1);
InitCursor();
DialogItemType type;
Handle itemH;
Rect box;
// draw default button indicator around the connect button
GetDialogItem(dlg, 2, &type, &itemH, &box);
SetDialogItem(dlg, 2, type, (Handle)NewUserItemUPP(&ButtonFrameProc), &box);
// get the handles for each of the text boxes, and load preference data in
ControlHandle address_text_box;
GetDialogItem(dlg, 4, &type, &itemH, &box);
address_text_box = (ControlHandle)itemH;
SetDialogItemText((Handle)address_text_box, (ConstStr255Param)prefs.hostname);
// select all text in hostname dialog item
SelectDialogItemText(dlg, 4, 0, 32767);
ControlHandle port_text_box;