-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.c
1455 lines (1155 loc) · 35.9 KB
/
main.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
//#define _POSIX_C_SOURCE 199309L
//#define _BSD_SOURCE
#include "fontmap_static.h"
#include "widthmap_static.h"
#include "nunifont.h"
#include <string.h>
#include <SDL.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include "vterm.h"
#include <locale.h>
#include <limits.h>
#include "nsdl.h"
#include <time.h>
#include "regis.h"
#include <stdbool.h>
#include "ssh.h"
#include "local.h"
#include "inlinedata.h"
#include "ngui.h"
#include "utf8proc.h"
#ifdef IOS_BUILD
#include "iphone_pasteboard.h"
#include "virtual_buttons.h"
#endif
#ifdef OSX_BUILD
#include "osx_pasteboard.h"
#endif
#define CONNECTION_LOCAL 1
#define CONNECTION_SSH 2
void redraw_required();
static VTerm *vt;
static VTermScreen *vts;
bool any_blinking = false;
bool new_screen_size = false;
int new_screen_size_x;
int new_screen_size_y;
static int cols;
static int rows;
int display_width;
int display_height;
int display_width_last_kb=0;
int display_height_last_kb=0;
int display_width_abs;
int display_height_abs;
struct SDL_Window *screen=1;
struct SDL_Renderer *renderer=1;
bool draw_selection = false;
int draw_fade_selection=0;
int select_start_x=0;
int select_start_y=0;
int select_end_x =0;
int select_end_y =0;
int select_start_scroll_offset;
int select_end_scroll_offset;
int connection_type=0;
bool hterm_quit = false;
int scroll_offset=0;
size_t scroll_buffer_initial_size = 10000;
size_t scroll_buffer_size = 0;
size_t scroll_buffer_start =0;
size_t scroll_buffer_end =0;
VTermScreenCell **scroll_buffer = 0;
uint32_t *scroll_buffer_lens=0;
VTermState *vs;
char open_arg1[100] = "";
char open_arg2[100];
char open_arg3[100];
char open_arg4[100];
char open_arg5[100];
char open_arg6[100];
int select_text_start_x=-1;
int select_text_start_y=-1;
int select_text_end_x=-1;
int select_text_end_y=-1;
// Funtions used to communicate with host
int (*c_open)(char *hostname,char *username, char *password,char *fingerprintstr,char *key1,char *key2) = 0;
int (*c_close)() = 0;
int (*c_write)(char *bytes,int len) = 0;
int (*c_read)(char *bytes,int len) = 0;
int (*c_resize)(int rows,int cols) = 0;
void scroll_buffer_get(size_t line_number,VTermScreenCell **line,int *len);
bool hterm_next_key_ctrl=false;
bool hterm_next_key_alt =false;
bool hterm_ctrl_pressed=false;
void regis_render() {
if(!regis_cleared()) {
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, regis_layer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_DestroyTexture(texture);
}
}
void inline_data_render() {
if(inline_data_layer != 0) {
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, inline_data_layer);
int res = SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_DestroyTexture(texture);
}
}
VTermScreenCell *grab_row(int trow,bool *dont_free,int *len) {
VTermScreenCell *rowdata = 0;
if(trow >= 0) {
// a screen row
rowdata = malloc(cols*sizeof(VTermScreenCell));
VTermPos vp;
for(int n=0;n<cols;n++) {
vp.row = trow;
vp.col = n;
vterm_screen_get_cell(vts,vp,&(rowdata[n]));
}
*len = cols;
*dont_free =false;
} else {
// a scrollback row
if((0-trow) > scroll_buffer_size) { rowdata = 0; }
else {
scroll_buffer_get(0-trow,&rowdata,len);
*dont_free=true;
}
}
return rowdata;
}
VTermScreenCell c_screen_data[1000][1000];
bool cdf =true;
bool cellcompare(VTermScreenCell a,VTermScreenCell b) {
if(a.chars[0] != b.chars[0]) return false;
if(a.chars[1] != b.chars[1]) return false;
if(a.attrs.bold != b.attrs.bold) return false;
if(a.attrs.underline != b.attrs.underline) return false;
if(a.attrs.italic != b.attrs.italic) return false;
if(a.attrs.blink != b.attrs.blink) return false;
if(a.attrs.reverse != b.attrs.reverse) return false;
if(a.attrs.strike != b.attrs.strike) return false;
if(a.attrs.font != b.attrs.font) return false;
return true;
}
void draw_row(VTermScreenCell *row,int crow,int ypos,int glen) {
/*
if(cdf==true) {
for(int n=0;n<1000;n++){
for(int i=0;i<1000;i++) {
c_screen_data[n][i].chars[0]=0;
}
}
}
cdf=false;
*/
int xpos=0;
for(int n=0;n<cols;n++) {
if(n >= glen) break;
uint16_t rtext[1000];
rtext[0] = row[n].chars[0];
if(rtext[0]==0) rtext[0]=' ';
rtext[1]=0;
VTermColor fg = row[n].fg;
VTermColor bg = row[n].bg;
//if(cellcompare(c_screen_data[crow][n],row[n]) == false) {
if(row[n].attrs.blink == 1) any_blinking = true;
draw_unitext_fancy_renderer(renderer,xpos,ypos,rtext,(bg.red << 24) + (bg.green << 16) + (bg.blue << 8) + 0xff,
(fg.red << 24) + (fg.green << 16) + (fg.blue << 8) + 0xff,
row[n].attrs.bold,
row[n].attrs.underline,
row[n].attrs.italic,
row[n].attrs.blink,
row[n].attrs.reverse,
row[n].attrs.strike,
row[n].attrs.font);
//}
//c_screen_data[crow][n] = row[n];
xpos+=(nunifont_width+nunifont_space);
if(row[n].width == 2) {xpos +=(nunifont_width+nunifont_space);n++;}
}
}
void scroll_buffer_init() {
scroll_buffer = malloc(sizeof(VTermScreenCell *)*scroll_buffer_initial_size);
scroll_buffer_lens = malloc(sizeof(int32_t)*scroll_buffer_initial_size);
for(int n=0;n<scroll_buffer_initial_size;n++) {
scroll_buffer[n] = 0;
scroll_buffer_lens[n]=0;
}
scroll_buffer_size = scroll_buffer_initial_size;
scroll_buffer_start=0;
scroll_buffer_end =0;
}
void scroll_buffer_push(VTermScreenCell *scroll_line,size_t len) {
if(scroll_buffer == 0) scroll_buffer_init();
if(scroll_buffer_end >= scroll_buffer_size) scroll_buffer_end = 0;
if(scroll_buffer[scroll_buffer_end] != 0) {
// if infini buffer, do resize
// scroll_buffer_resize(scroll_buffer_size+10000);
// else
free(scroll_buffer[scroll_buffer_end]);
scroll_buffer[scroll_buffer_end]=0;
}
scroll_buffer[scroll_buffer_end] = malloc(sizeof(VTermScreenCell)*len);
scroll_buffer_lens[scroll_buffer_end] = len;
for(size_t n=0;n<len;n++) {
scroll_buffer[scroll_buffer_end][n] = scroll_line[n];
}
scroll_buffer_end++;
}
void scroll_buffer_get(size_t line_number,VTermScreenCell **line,int *len) {
int idx = scroll_buffer_end-line_number-1;
if(idx < 0) idx = scroll_buffer_size+idx;
if(idx < 0) *line = 0;
*line = scroll_buffer[idx];
*len = scroll_buffer_lens[idx];
}
void scroll_buffer_dump() {
}
static int screen_prescroll(VTermRect rect, void *user)
{
if(rect.start_row != 0 || rect.start_col != 0 || rect.end_col != cols)
return 0;
for(int row=rect.start_row;row<rect.end_row;row++) {
VTermScreenCell scrolloff[1000];
size_t len=0;
for(int n=0;n<cols;n++) {
VTermPos vp;
vp.row=row;
vp.col=n;
VTermScreenCell c;
int i = vterm_screen_get_cell(vts,vp,&c);
scrolloff[n] = c;
len++;
}
scroll_buffer_push(scrolloff,cols);
}
redraw_required();
return 1;
}
static int screen_resize(int new_rows, int new_cols, void *user)
{
rows = new_rows;
cols = new_cols;
return 1;
}
static int parser_resize(int new_rows, int new_cols, void *user)
{
return 1;
}
static int screen_bell(void* d) {
}
int state_erase(VTermRect r,void *user) {
redraw_required();
return 0;
}
VTermScreenCallbacks cb_screen = {
.prescroll = &screen_prescroll,
.resize = &screen_resize,
.bell = &screen_bell
};
VTermStateCallbacks cb_state = {
.putglyph = 0,
.movecursor = 0,
.scrollrect = 0,
.moverect = 0,
.erase = &state_erase,
.initpen = 0,
.setpenattr = 0,
.settermprop = 0,
.setmousefunc = 0,
.bell = 0,
.resize = 0
};
int csi_handler(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user) {
if(command == 'J') {
if(!regis_recent()) regis_clear();
inline_data_clear();
redraw_required();
}
// This is an attempt to capture clears in tmux
if(command == 'K') {
inline_data_clear();
redraw_required();
}
return 0;
}
int dcs_handler(const char *command,size_t cmdlen,void *user) {
if(cmdlen < 3) return 0;
regis_processor(command+2,cmdlen);
}
int osc_handler(const char *command,size_t cmdlen,void *user) {
}
int text_handler(const char *bytes, size_t len, void *user) {
inline_data_receive(bytes,len);
}
int esc_handler(const char *bytes, size_t len, void *user) {
}
VTermParserCallbacks cb_parser = {
.text = text_handler,
.control = 0,
.escape = esc_handler,
.csi = csi_handler,
.osc = osc_handler,
.dcs = dcs_handler,
.resize = 0 //&parser_resize,
// int (*text)(const char *bytes, size_t len, void *user);
// int (*control)(unsigned char control, void *user);
// int (*escape)(const char *bytes, size_t len, void *user);
// int (*csi)(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user);
// int (*osc)(const char *command, size_t cmdlen, void *user);
// int (*dcs)(const char *command, size_t cmdlen, void *user);
// int (*resize)(int rows, int cols, void *user);
};
void terminal_resize() {
rows = display_height/nunifont_height;
cols = display_width/nunifont_width;
if(c_resize != NULL) (*c_resize)(cols,rows);
if(vt != 0) vterm_set_size(vt,rows,cols);
}
void cursor_position(int *cursorx,int *cursory) {
VTermPos cursorpos;
vterm_state_get_cursorpos(vs,&cursorpos);
*cursorx = cursorpos.col;
*cursory = cursorpos.row;
}
void redraw_text() {
for(int row = 0; row < rows; row++) {
int trow = row-scroll_offset;
bool dont_free=false;
int glen=0;
VTermScreenCell *rowdata=grab_row(trow,&dont_free,&glen);
if(rowdata != 0) draw_row(rowdata,trow,row*(nunifont_height+nunifont_space),glen);
int cursorx=0;
int cursory=0;
cursor_position(&cursorx,&cursory);
if(cursory == trow) {
int width=nunifont_width+nunifont_space;
if((cursorx < cols) && (cursory < rows) && (rowdata != 0)) {
if(rowdata[cursorx].width == 2) width+=(nunifont_width+nunifont_space);
SDL_SetRenderDrawColor(renderer,0xEF,0xEF,0xEF,0xA0);
SDL_Rect r;
r.x = cursorx*(nunifont_width+nunifont_space);
r.y = row*(nunifont_height+nunifont_space);
r.w = width;
r.h = nunifont_height+nunifont_space;
SDL_RenderFillRect(renderer,&r);
}
}
if((rowdata != 0) && (dont_free==false)){free(rowdata); rowdata=0;}
}
}
void mouse_to_select_box(int sx,int sy,int so,
int ex,int ey,int eo,
int *stx,int *sty,int *etx,int *ety) {
*stx=floor(((float)sx/(nunifont_width +nunifont_space)));
*etx=ceil( ((float)ex/(nunifont_width +nunifont_space)));
*sty=floor(((float)sy/(nunifont_height+nunifont_space)))-so;
*ety=ceil( ((float)ey/(nunifont_height+nunifont_space)))-eo;
if(sy > ey) {*ety=*ety-1; *etx=*etx-1;} else
if(sy < ey) {*ety=*ety-1; }
if(*sty == *ety){
if(*etx < *stx) *etx = *etx - 1;
if(*etx > *stx) *etx = *etx + 1;
}
}
void get_text_region(int text_start_x,int text_start_y,int text_end_x,int text_end_y,uint16_t **itext,int *ilen) {
if(text_start_y > text_end_y) {
int t = text_start_y;
text_start_y = text_end_y;
text_end_y = t;
}
int len=0;
uint16_t *text = malloc(10240);
for(int y=text_start_y;y<=text_end_y;y++) {
bool dont_free=false;
int glen=0;
VTermScreenCell *row_data = grab_row(y,&dont_free,&glen);
if(row_data == 0) { text[0]=0; }
else {
//cliping for first and last lines.
int start_x;
if(y == text_start_y) {
start_x = text_start_x;
} else {
start_x = 0;
}
int end_x;
if(y==text_end_y) {
end_x = text_end_x;
} else {
end_x = cols;
}
//find last non-whitespace, clip to here too.
for(int n=cols;n>=0;n--) {
if(row_data[n].chars[0] != ' ') {
if(n < end_x) end_x = n;
break;
}
}
for(int x=start_x;x<end_x;x++) {
if(text_end_x >= glen) {text_end_x=glen-1;}
text[len] = row_data[x].chars[0];
if(text[len]!=0 && (text[len]!=65535)) len++;
}
}
if(!dont_free) free(row_data);
if(y!=text_end_y) {
text[len] = '\n';
len++;
}
}
text[len]=0;
text[len+1]=0;
text[len+2]=0;
text[len+3]=0;
text[len+4]=0;
text[len+5]=0;
text[len+6]=0;
text[len+7]=0;
*itext = text;
*ilen = len;
}
void redraw_selection() {
if(draw_selection || (draw_fade_selection>0)) {
int color=255;
if(draw_fade_selection>0) {
color=draw_fade_selection*5;
draw_fade_selection--;
redraw_required();
} else {
color=255;
}
select_end_scroll_offset = scroll_offset;
int sselect_text_end_y = select_text_end_y + scroll_offset;
int sselect_text_start_y = select_text_start_y + scroll_offset;
int sx=select_text_start_x*(nunifont_width+nunifont_space);
int sy=sselect_text_start_y*(nunifont_height+nunifont_space);
int ex=select_text_end_x*(nunifont_width+nunifont_space);
int ey=sselect_text_end_y*(nunifont_height+nunifont_space)-1;
SDL_SetRenderDrawColor(renderer, color, color, color,color);
// start line
if(select_text_start_y!=select_text_end_y) { // only draw central block bound for non-central lines.
SDL_RenderDrawLine(renderer,0,sy+(nunifont_height+nunifont_space),sx,sy+(nunifont_height+nunifont_space));
SDL_RenderDrawLine(renderer,sx,sy,display_width-1,sy);
} else {
SDL_RenderDrawLine(renderer,sx,sy,ex,sy);
}
SDL_RenderDrawLine(renderer,sx,sy,sx,sy+(nunifont_height+nunifont_space));
// end line
if(select_text_start_y!=select_text_end_y) { // only draw central block bound for non-central lines.
SDL_RenderDrawLine(renderer,ex,ey,display_width-1,ey);
SDL_RenderDrawLine(renderer,ex,ey+(nunifont_height+nunifont_space),0,ey+(nunifont_height+nunifont_space));
} else {
SDL_RenderDrawLine(renderer,sx,ey+(nunifont_height+nunifont_space),ex,ey+(nunifont_height+nunifont_space));
}
SDL_RenderDrawLine(renderer,ex,ey+(nunifont_height+nunifont_space),ex,ey);
// only draw block for non-single lines.
if(select_text_start_y!=select_text_end_y) {
// central block
SDL_RenderDrawLine(renderer,0 ,sy+(nunifont_height+nunifont_space),0,ey+(nunifont_height+nunifont_space));
SDL_RenderDrawLine(renderer,display_width-1,sy,display_width-1,ey);
}
}
}
void redraw_screen() {
SDL_SetRenderDrawColor(renderer,0x00,0x00,0x00,0xff);
SDL_RenderClear(renderer);
any_blinking = false;
redraw_text();
redraw_selection();
regis_render();
inline_data_render();
ngui_render();
SDL_RenderPresent(renderer);
}
#ifdef IOS_BUILD
void ios_connect() {
display_serverselect(screen);
for(;;) {
display_serverselect_run();
int c = display_serverselect_get(open_arg1,open_arg2,open_arg3,open_arg4,open_arg5,open_arg6);
// key transfer
if(c == 1) {
int open_ret = ssh_open_preshell(open_arg1,open_arg2,open_arg3,open_arg4,open_arg5,open_arg6);
int result = ssh_getkeys(open_arg5,open_arg6);
if(result == 0) display_serverselect_keyxfer_ok();
if(result == 1) display_serverselect_keyxfer_fail();
display_serverselect_complete();
c_close();
break;
}
// normal connection
if(c == 2) {
display_serverselect_complete();
break;
}
// should not happen.
if(c == -2) {
break;
}
}
}
#endif
void do_sdl_init() {
if(SDL_Init(SDL_INIT_VIDEO)<0) {
return;
}
#if defined(OSX_BUILD) || defined(IOS_BUILD)
SDL_GL_SetAttribute(SDL_GL_RED_SIZE , 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE , 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32);
#ifdef OSX_BUILD
// SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 0 );
#endif
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
#endif
#ifdef IOS_BUILD
screen=SDL_CreateWindow(NULL, 0, 0, 0, 0,SDL_WINDOW_FULLSCREEN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS);
#endif
#if defined(OSX_BUILD) || defined(LINUX_BUILD)
screen=SDL_CreateWindow("hterm", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1324, 750,SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
#endif
#ifdef IOS_BUILD
ios_connect();
#endif
#ifdef IOS_BUILD
SDL_GetWindowSize(screen,&display_width,&display_height);
display_width_abs = display_width;
display_height_abs = display_height;
#endif
if (screen == 0) {
printf("Could not initialize Window\n");
printf("Error: %s\n",SDL_GetError());
}
renderer = SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED);
//SDL_BITSPERPIXEL(format);
SDL_SetRenderDrawBlendMode(renderer,
SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer,0x00,0x00,0xff,0xff);
SDL_RenderClear(renderer);
set_system_bg(255);//alpha always 255
}
void sdl_read_thread();
void console_read_init() {
int open_ret = c_open(open_arg1,open_arg2,open_arg3,open_arg4,open_arg5,open_arg6);
#ifdef IOS_BUILD
display_server_select_setactive(true);
if(open_ret == -5) {
display_serverselect_keyfailure();
}
if(connection_type == CONNECTION_SSH) {
if(open_ret == 0) {
char *fingerprintstr = ssh_fingerprintstr();
if(open_arg4[0]==0) {
display_serverselect_firstkey(fingerprintstr);
}
write_connection(open_arg1,open_arg2,open_arg3,fingerprintstr);
}
}
#endif
terminal_resize();
}
void console_poll() {
// sending bytes from pts to vterm
int len;
char buffer[10241];
len = c_read(buffer, sizeof(buffer)-1);
if(len > 0) {
if((buffer != 0) && (len != 0)) {
vterm_push_bytes(vt, buffer, len);
}
redraw_required();
}
if(len < 0) {
hterm_quit = true;
return;
}
}
bool redraw_req=true;
int forced_recreaterenderer=0;
int last_kb_shown=-2;
void sdl_render_thread() {
SDL_Event event;
SDL_StartTextInput();
for(;;) {
if(redraw_req) {
redraw_req=false; // should go first as draw can itself trigger a redraw.
redraw_screen();
} else {
SDL_Delay(10);
}
SDL_Event event;
int ret = 1;
for(;ret==1;) {
ret = SDL_PollEvent(&event);
if(ret != 0) {
sdl_read_thread(&event);
}
}
console_poll();
if(hterm_quit == true) return;
#ifdef IOS_BUILD
if((SDL_IsScreenKeyboardShown(screen) != last_kb_shown) &&
(last_kb_shown != -3)) {
SDL_GetWindowSize(screen,&display_width,&display_height);
display_width_abs = display_width;
display_height_abs = display_height;
#ifdef IOS_BUILD
if(SDL_IsScreenKeyboardShown(screen)) {
display_width = display_width_last_kb;
display_height = display_height_last_kb;
virtual_buttons_reposition();
} else {
virtual_buttons_reposition();
#ifndef ITUNES_BUILD
virtual_buttons_disable();
#endif
}
#endif
redraw_required();
}
last_kb_shown = SDL_IsScreenKeyboardShown(screen);
#endif
}
}
void redraw_required() {
redraw_req=true;
}
uint8_t *paste_text() {
#ifdef IOS_BUILD
return iphone_paste();
#endif
#ifdef OSX_BUILD
return osx_paste();
#endif
#ifdef LINUX_BUILD
uint8_t *paste_data = malloc(sizeof(uint8_t)*10240);
FILE *r1 = popen("xclip -o","r");
if(r1!=NULL) {
for(size_t n=0;feof(r1) == false;n++) {
int c = fgetc(r1);
if(!feof(r1)) {
paste_data[n] = c;
paste_data[n+1] = 0;
}
}
pclose(r1);
}
return paste_data;
#endif
}
void copy_text(uint16_t *itext,int len) {
// TODO: This needs to be updated to generate UTF8 text
size_t pos=0;
char text[20000];
for(int n=0;n<len;n++) {
size_t s = utf8proc_encode_char(itext[n],text+pos);
pos+=s;
text[pos ]=0;
text[pos+1]=0;
text[pos+2]=0;
text[pos+3]=0;
text[pos+4]=0;
}
#ifdef IOS_BUILD
iphone_copy(text);
#endif
#ifdef OSX_BUILD
osx_copy(text);
#endif
#ifdef LINUX_BUILD
FILE *w1 = popen("xclip -selection c","w");
if(w1!=NULL) {
fprintf(w1,"%s",text);
pclose(w1);
}
FILE *w2 = popen("xclip -i","w");
if(w2==NULL) return;
fprintf(w2,"%s",text);
pclose(w2);
#endif
// execute these two commands on Linux/XWindows by default
//echo "test" | xclip -selection c
//echo "test" | xclip -i
}
int delta_sum=0;
bool select_disable=false;
int last_text_point_x = -1;
int last_text_point_y = -1;
void process_mouse_event(SDL_Event *event) {
#ifdef IOS_BUILD
if(event->type == SDL_FINGERMOTION) {
SDL_Touch *t = SDL_GetTouch(event->tfinger.touchId);
if(t->num_fingers != 0) {
}
if(t->num_fingers == 2) {
select_disable=true;
int delta = event->tfinger.dy;
delta_sum += delta;
if(delta_sum > 500) {
scroll_offset++;
redraw_required();
delta_sum-=500;
}
if(delta_sum < -500) {
scroll_offset--;
if(scroll_offset < 0) scroll_offset = 0;
redraw_required();
delta_sum+=500;
}
return; // prevent select code from running.
} else {
select_disable=false;
}
}
if(event->type == SDL_FINGERUP) {
}
if(select_disable) {
draw_selection = false;
return;
}
#endif
if((event->type != SDL_MOUSEMOTION) && (event->type != SDL_MOUSEBUTTONUP) && (event->type != SDL_MOUSEBUTTONDOWN) && (event->type != SDL_MOUSEWHEEL)) return;
int mouse_x = event->motion.x;
int mouse_y = event->motion.y;
#if defined(OSX_BUILD) || defined(LINUX_BUILD)
if(event->type == SDL_MOUSEWHEEL) {
if(event->wheel.y > 0) {
scroll_offset++;
redraw_required();
} else {
scroll_offset--;
if(scroll_offset < 0) scroll_offset = 0;
redraw_required();
}
} else
#endif
if(event->type == SDL_MOUSEMOTION ) {
if(draw_selection == true) {
//if(event->button.y <= 0 ) scroll_offset++;
//if(event->button.y >= (screen->h-1)) {if(scroll_offset != 0) scroll_offset--;}
select_end_scroll_offset = scroll_offset;
mouse_to_select_box(select_start_x,select_start_y,select_start_scroll_offset,
select_end_x ,select_end_y,select_end_scroll_offset,
&select_text_start_x, &select_text_start_y, &select_text_end_x, &select_text_end_y);
select_end_x = event->button.x;
select_end_y = event->button.y;
redraw_required();
}
} else
if(event->type == SDL_MOUSEBUTTONUP ) {
draw_selection = false;
int xdelta = select_start_x-select_end_x;
if(xdelta < 0) xdelta = 0-xdelta;
int ydelta = select_start_y-select_end_y;
if(ydelta < 0) ydelta = 0-ydelta;
if((xdelta < 15) && (ydelta < 15)) {
// this was a selection less than a single character in size
// in these cases we don't actually process a selection.
// we note the time, if there was another selection recently then this is a word selection
// from this point we flood out left and right and select this text.
// we also need to briefly display that this was selected.
int text_start_x;
int text_start_y;
int text_end_x;
int text_end_y;
select_end_scroll_offset = scroll_offset;
mouse_to_select_box(select_start_x,select_start_y,select_start_scroll_offset,
select_end_x, select_end_y ,select_end_scroll_offset,
&text_start_x, &text_start_y, &text_end_x, &text_end_y);
if((last_text_point_x == text_start_x) &&
(last_text_point_y == text_start_y)) {
int word_start_x=-1;
int word_end_x =-1;
// grab text for this line
uint16_t *text=0;
int len=0;
get_text_region(0,text_start_y,cols,text_start_y,&text,&len);
// find left bound
for(int n=text_start_x;n>=0;n--) {
if((text[n] == ' ') || (text[n] == '\n')) {