-
Notifications
You must be signed in to change notification settings - Fork 1
/
TerminalView.m
2409 lines (2022 loc) · 47.1 KB
/
TerminalView.m
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
/*
copyright 2002-2003 Alexander Malmberg <[email protected]>
2005-2016 Riccardo Mottola <[email protected]>
This file is a part of Terminal.app. Terminal.app is free software; you
can redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation; version 2
of the License. See COPYING or main.m for more information.
*/
/*
TODO: Move pty and child process handling to another class. Make this a
stupid but fast character cell display view.
*/
/* define this if you need the forkpty replacement and it is not automatically
activated */
#undef USE_FORKPTY_REPLACEMENT
/* check for solaris */
#if defined (__SVR4) && defined (__sun)
#define __SOLARIS__ 1
#define USE_FORKPTY_REPLACEMENT 1
#endif
#include <math.h>
#include <unistd.h>
#ifdef __NetBSD__
# include <sys/types.h>
# include <sys/ioctl.h>
# include <termios.h>
# include <pcap.h>
# include <util.h>
#define TCSETS TIOCSETA
#elif defined(__FreeBSD__)
# include <sys/types.h>
# include <sys/ioctl.h>
# include <termios.h>
# include <libutil.h>
# include <pcap.h>
#elif defined(__OpenBSD__)
# include <termios.h>
# include <util.h>
# include <sys/ioctl.h>
#elif defined (__GNU__)
#else
# include <termio.h>
#endif
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#ifndef __FreeBSD__
#if !(defined (__NetBSD__)) && !(defined (__SOLARIS__)) && !(defined(__OpenBSD__))
# include <pty.h>
#endif
#endif
#import <Foundation/NSBundle.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSRunLoop.h>
#import <Foundation/NSUserDefaults.h>
#import <Foundation/NSCharacterSet.h>
#import <Foundation/NSArchiver.h>
#import <GNUstepBase/Unicode.h>
#import <AppKit/NSApplication.h>
#import <AppKit/NSPasteboard.h>
#import <AppKit/NSDragging.h>
#import <AppKit/NSEvent.h>
#import <AppKit/NSGraphics.h>
#import <AppKit/NSScroller.h>
#import <AppKit/DPSOperators.h>
#import "TerminalView.h"
#import "TerminalViewPrefs.h"
#import "TerminalParser_Linux.h"
/* forkpty replacement */
#ifdef USE_FORKPTY_REPLACEMENT
#include <stdio.h> /* for stderr and perror*/
#include <errno.h> /* for int errno */
#include <fcntl.h>
#include <sys/termios.h>
#include <stropts.h>
#include <stdlib.h>
#include <string.h>
#define PATH_TTY "/dev/tty"
int ptyMakeControllingTty(int *slaveFd, const char *slaveName)
{
pid_t pgid;
int fd;
if (!slaveFd || *slaveFd < 0)
{
perror("slaveFd invalid");
return -1;
}
/* disconnect from the old controlling tty */
#ifdef TIOCNOTTY
if ((fd = open(PATH_TTY, O_RDWR | O_NOCTTY)) >= 0 )
{
ioctl(fd, TIOCNOTTY, NULL);
close(fd);
}
#endif
pgid = setsid(); /* create session and set process ID */
if (pgid == -1)
{
if (errno == EPERM)
perror("EPERM error on setsid");
}
/* Make it our controlling tty */
#ifdef TIOCSCTTY
if (ioctl(*slaveFd, TIOCSCTTY, NULL) == -1)
return -1;
#else
#warning TIOCSCTTY replacement
{
/* first terminal we open after setsid() is the controlling one */
char *controllingTty;
int ctr_fdes;
controllingTty = ttyname(*slaveFd);
ctr_fdes = open(controllingTty, O_RDWR);
close(ctr_fdes);
}
#endif /* TIOCSCTTY */
#if defined (TIOCSPGRP)
ioctl (0, TIOCSPGRP, &pgid);
#else
#warning no TIOCSPGRP
tcsetpgrp (0, pgid);
#endif
if ((fd = open(slaveName, O_RDWR)) >= 0)
{
close(*slaveFd);
*slaveFd = fd;
printf("Got new filedescriptor...\n");
}
if ((fd = open(PATH_TTY, O_RDWR)) == -1)
return -1;
close(fd);
return 0;
}
int openpty(int *amaster, int *aslave, char *name, const struct termios *termp, const struct winsize *winp)
{
int fdm, fds;
char *slaveName;
fdm = open("/dev/ptmx", O_RDWR); /* open master */
if (fdm == -1)
{
perror("openpty:open(master)");
return -1;
}
if(grantpt(fdm)) /* grant access to the slave */
{
perror("openpty:grantpt(master)");
close(fdm);
return -1;
}
if(unlockpt(fdm)) /* unlock the slave terminal */
{
perror("openpty:unlockpt(master)");
close(fdm);
return -1;
}
slaveName = ptsname(fdm); /* get name of the slave */
if (slaveName == NULL)
{
perror("openpty:ptsname(master)");
close(fdm);
return -1;
}
if (name) /* of name ptr not null, copy it name back */
strcpy(name, slaveName);
fds = open(slaveName, O_RDWR | O_NOCTTY); /* open slave */
if (fds == -1)
{
perror("openpty:open(slave)");
close (fdm);
return -1;
}
/* ldterm and ttcompat are automatically pushed on the stack on some systems*/
#ifdef __SOLARIS__
if (ioctl(fds, I_PUSH, "ptem") == -1) /* pseudo terminal module */
{
perror("openpty:ioctl(I_PUSH, ptem");
close(fdm);
close(fds);
return -1;
}
if (ioctl(fds, I_PUSH, "ldterm") == -1) /* ldterm must stay atop ptem */
{
perror("forkpty:ioctl(I_PUSH, ldterm");
close(fdm);
close(fds);
return -1;
}
#endif
/* set terminal parameters if present */
if (termp)
ioctl(fds, TCSETS, termp);
if (winp)
ioctl(fds, TIOCSWINSZ, winp);
*amaster = fdm;
*aslave = fds;
return 0;
}
int forkpty (int *amaster, char *slaveName, const struct termios *termp, const struct winsize *winp)
{
int fdm, fds; /* master and slave file descriptors */
pid_t pid;
if (openpty(&fdm, &fds, slaveName, termp, winp) == -1)
{
perror("forkpty:openpty()");
return -1;
}
pid = fork();
if (pid == -1)
{
/* error */
perror("forkpty:fork()");
close(fdm);
close(fds);
return -1;
} else if (pid == 0)
{
/* child */
ptyMakeControllingTty(&fds, slaveName);
if (fds != STDIN_FILENO && dup2(fds, STDIN_FILENO) == -1)
perror("error duplicationg stdin");
if (fds != STDOUT_FILENO && dup2(fds, STDOUT_FILENO) == -1)
perror("error duplicationg stdout");
if (fds != STDERR_FILENO && dup2(fds, STDERR_FILENO) == -1)
perror("error duplicationg stderr");
if (fds != STDIN_FILENO && fds != STDOUT_FILENO && fds != STDERR_FILENO)
close(fds);
close (fdm);
} else
{
/* father */
close (fds);
*amaster = fdm;
}
return pid;
}
#endif /* forpkty replacement */
/* TODO */
@interface NSView (unlockfocus)
-(void) unlockFocusNeedsFlush: (BOOL)flush;
@end
NSString
*TerminalViewBecameIdleNotification=@"TerminalViewBecameIdle",
*TerminalViewBecameNonIdleNotification=@"TerminalViewBecameNonIdle",
*TerminalViewTitleDidChangeNotification=@"TerminalViewTitleDidChange";
@interface TerminalView (scrolling)
-(void) _updateScroller;
-(void) _scrollTo: (int)new_scroll update: (BOOL)update;
-(void) setScroller: (NSScroller *)sc;
@end
@interface TerminalView (selection)
-(void) _clearSelection;
@end
@interface TerminalView (input) <RunLoopEvents>
-(void) closeProgram;
-(void) runShell;
-(void) runProgram: (NSString *)path
withArguments: (NSArray *)args
initialInput: (NSString *)d;
@end
/**
TerminalScreen protocol implementation and rendering methods
**/
@implementation TerminalView (display)
#define ADD_DIRTY(ax0,ay0,asx,asy) do { \
if (dirty.x0==-1) \
{ \
dirty.x0=(ax0); \
dirty.y0=(ay0); \
dirty.x1=(ax0)+(asx); \
dirty.y1=(ay0)+(asy); \
} \
else \
{ \
if (dirty.x0>(ax0)) dirty.x0=(ax0); \
if (dirty.y0>(ay0)) dirty.y0=(ay0); \
if (dirty.x1<(ax0)+(asx)) dirty.x1=(ax0)+(asx); \
if (dirty.y1<(ay0)+(asy)) dirty.y1=(ay0)+(asy); \
} \
} while (0)
#define SCREEN(x,y) (screen[(y)*sx+(x)])
/* handle accumulated pending scrolls with a single composite */
-(void) _handlePendingScroll: (BOOL)lockFocus
{
float x0,y0,w,h,dx,dy;
if (!pending_scroll)
return;
if (pending_scroll>=sy || pending_scroll<=-sy)
{
pending_scroll=0;
return;
}
NSDebugLLog(@"draw",@"_handlePendingScroll %i %i",pending_scroll,lockFocus);
dx=x0=0;
w=fx*sx;
if (pending_scroll>0)
{
y0=0;
h=(sy-pending_scroll)*fy;
dy=pending_scroll*fy;
y0=sy*fy-y0-h;
dy=sy*fy-dy-h;
}
else
{
pending_scroll=-pending_scroll;
y0=pending_scroll*fy;
h=(sy-pending_scroll)*fy;
dy=0;
y0=sy*fy-y0-h;
dy=sy*fy-dy-h;
}
if (lockFocus)
[self lockFocus];
DPScomposite(GSCurrentContext(),border_x+x0,border_y+y0,w,h,
[self gState],border_x+dx,border_y+dy,NSCompositeCopy);
if (lockFocus)
[self unlockFocusNeedsFlush: NO];
num_scrolls++;
pending_scroll=0;
}
static int total_draw=0;
static const float col_h[8]={ 0,240,120,180, 0,300, 60, 0};
static const float col_s[8]={0.0,1.0,1.0,1.0,1.0,1.0,1.0,0.0};
static void set_background(NSGraphicsContext *gc,
unsigned char color,unsigned char in)
{
float bh,bs,bb;
int bg=color>>4;
if (bg==0)
bb=0.0;
else if (bg>=8)
bg-=8,bb=1.0;
else
bb=0.6;
bs=col_s[bg];
bh=col_h[bg]/360.0;
DPSsethsbcolor(gc,bh,bs,bb);
}
static void set_foreground(NSGraphicsContext *gc,
unsigned char color,unsigned char in, BOOL blackOnWhite)
{
int fg=color;
float h,s,b;
if (blackOnWhite)
{
if (color == 0) { fg = 7; in = 2; } // Black becomes white
else if (color == 7) { fg = 0; in = 0; } // White becomes black
//else in = 3; // Other colors are saturated
}
if (fg>=8)
{
in++;
fg-=8;
}
if (fg==0)
{
if (in==2)
b=0.4;
else
b=0.0;
}
else if (in==0)
b=0.6;
else if (in==1)
b=0.8;
else
b=1.0;
h=col_h[fg]/360.0;
s=col_s[fg];
if (in==2)
s*=0.75;
DPSsethsbcolor(gc,h,s,b);
}
-(void) drawRect: (NSRect)r
{
int ix,iy;
char buf[8];
NSGraphicsContext *cur=GSCurrentContext();
int x0,y0,x1,y1;
NSFont *f,*current_font=nil;
int encoding;
NSDebugLLog(@"draw",@"drawRect: (%g %g)+(%g %g) %i\n",
r.origin.x,r.origin.y,r.size.width,r.size.height,
draw_all);
if (pending_scroll)
[self _handlePendingScroll: NO];
/* draw the border around the view if needed */
{
float a,b;
if (blackOnWhite)
DPSsetgray(cur,1.0);
else
DPSsetgray(cur,0.0);
if (r.origin.x<border_x)
DPSrectfill(cur,r.origin.x,r.origin.y,border_x-r.origin.x,r.size.height);
if (r.origin.y<border_y)
DPSrectfill(cur,r.origin.x,r.origin.y,r.size.width,border_y-r.origin.y);
a=border_x+sx*fx;
b=r.origin.x+r.size.width;
if (b>a)
DPSrectfill(cur,a,r.origin.y,b-a,r.size.height);
a=border_y+sy*fy;
b=r.origin.y+r.size.height;
if (b>a)
DPSrectfill(cur,r.origin.x,a,r.size.width,b-a);
}
/* figure out what character cells might need redrawing */
r.origin.x-=border_x;
r.origin.y-=border_y;
x0=floor(r.origin.x/fx);
x1=ceil((r.origin.x+r.size.width)/fx);
if (x0<0) x0=0;
if (x1>=sx) x1=sx;
y1=floor(r.origin.y/fy);
y0=ceil((r.origin.y+r.size.height)/fy);
y0=sy-y0;
y1=sy-y1;
if (y0<0) y0=0;
if (y1>=sy) y1=sy;
NSDebugLLog(@"draw",@"dirty (%i %i)-(%i %i)\n",x0,y0,x1,y1);
draw_cursor=draw_cursor || draw_all ||
(SCREEN(cursor_x,cursor_y).attr&0x80)!=0;
{
int ry;
screen_char_t *ch;
float scr_y,scr_x,start_x;
/* setting the color is slow, so we try to avoid it */
unsigned char l_color,l_attr,color;
/* Fill the background of dirty cells. Since the background doesn't
change that often, runs of dirty cells with the same background color
are combined and drawn with a single rectfill. */
l_color=0;
l_attr=0;
set_foreground(cur,l_color,l_attr,blackOnWhite);
for (iy=y0;iy<y1;iy++)
{
ry=iy+current_scroll;
if (ry>=0)
ch=&SCREEN(x0,ry);
else
ch=&sbuf[x0+(max_scrollback+ry)*sx];
scr_y=(sy-1-iy)*fy+border_y;
/*
#define R(scr_x,scr_y,fx,fy) \
DPSgsave(cur); \
DPSsetgray(cur,0.0); \
DPSrectfill(cur,scr_x,scr_y,fx,fy); \
DPSgrestore(cur); \
DPSrectstroke(cur,scr_x,scr_y,fx,fy); \
*/
/* ~400 cycles/cell on average */
#define R(scr_x,scr_y,fx,fy) DPSrectfill(cur,scr_x,scr_y,fx,fy)
start_x=-1;
for (ix=x0;ix<x1;ix++,ch++)
{
if (!draw_all && !(ch->attr&0x80))
{
if (start_x!=-1)
{
scr_x=ix*fx+border_x;
R(start_x,scr_y,scr_x-start_x,fy);
start_x=-1;
}
continue;
}
scr_x=ix*fx+border_x;
if (ch->attr&0x8)
{
color=ch->color&0xf;
if (ch->attr&0x40) color^=0xf;
if (color!=l_color || (ch->attr&0x03)!=l_attr)
{
if (start_x!=-1)
{
R(start_x,scr_y,scr_x-start_x,fy);
start_x=scr_x;
}
l_color=color;
l_attr=ch->attr&0x03;
set_foreground(cur,l_color,l_attr,blackOnWhite);
}
}
else
{
color=ch->color&0xf0;
if (ch->attr&0x40) color^=0xf0;
if (color!=l_color)
{
if (start_x!=-1)
{
R(start_x,scr_y,scr_x-start_x,fy);
start_x=scr_x;
}
l_color=color;
l_attr=ch->attr&0x03;
set_background(cur,l_color,l_attr);
}
}
if (start_x==-1)
start_x=scr_x;
}
if (start_x!=-1)
{
scr_x=ix*fx+border_x;
R(start_x,scr_y,scr_x-start_x,fy);
}
}
/* now draw any dirty characters */
for (iy=y0;iy<y1;iy++)
{
ry=iy+current_scroll;
if (ry>=0)
ch=&SCREEN(x0,ry);
else
ch=&sbuf[x0+(max_scrollback+ry)*sx];
scr_y=(sy-1-iy)*fy+border_y;
for (ix=x0;ix<x1;ix++,ch++)
{
if (!draw_all && !(ch->attr&0x80))
continue;
ch->attr&=0x7f;
scr_x=ix*fx+border_x;
/* ~1700 cycles/change */
if (ch->attr&0x02 || (ch->ch!=0 && ch->ch!=32))
{
if (!(ch->attr&0x8))
{
color=ch->color&0xf;
if (ch->attr&0x40) color^=0xf;
if (color!=l_color || (ch->attr&0x03)!=l_attr)
{
l_color=color;
l_attr=ch->attr&0x03;
set_foreground(cur,l_color,l_attr,blackOnWhite);
}
}
else
{
color=ch->color&0xf0;
if (ch->attr&0x40) color^=0xf0;
if (color!=l_color)
{
l_color=color;
l_attr=ch->attr&0x03;
set_background(cur,l_color,l_attr);
}
}
}
if (ch->ch!=0 && ch->ch!=32 && ch->ch!=MULTI_CELL_GLYPH)
{
total_draw++;
if ((ch->attr&3)==2)
{
encoding=boldFont_encoding;
f=boldFont;
}
else
{
encoding=font_encoding;
f=font;
}
if (f!=current_font)
{
/* ~190 cycles/change */
[f set];
current_font=f;
}
/* we short-circuit utf8 for performance with back-art */
/* TODO: short-circuit latin1 too? */
if (encoding==NSUTF8StringEncoding)
{
unichar uch=ch->ch;
if (uch>=0x800)
{
buf[2]=(uch&0x3f)|0x80;
uch>>=6;
buf[1]=(uch&0x3f)|0x80;
uch>>=6;
buf[0]=(uch&0x0f)|0xe0;
buf[3]=0;
}
else if (uch>=0x80)
{
buf[1]=(uch&0x3f)|0x80;
uch>>=6;
buf[0]=(uch&0x1f)|0xc0;
buf[2]=0;
}
else
{
buf[0]=uch;
buf[1]=0;
}
}
else
{
unichar uch=ch->ch;
if (uch<=0x80)
{
buf[0]=uch;
buf[1]=0;
}
else
{
unsigned char *pbuf=(unsigned char *)buf;
unsigned int dlen=sizeof(buf)-1;
GSFromUnicode(&pbuf,&dlen,&uch,1,encoding,NULL,GSUniTerminate);
}
}
/* ~580 cycles */
DPSmoveto(cur,scr_x+fx0,scr_y+fy0);
/* baseline here for mc-case 0.65 */
/* ~3800 cycles */
DPSshow(cur,buf);
/* ~95 cycles to ARTGState -DPSshow:... */
/* ~343 cycles to isEmpty */
/* ~593 cycles to currentpoint */
/* ~688 cycles to transform */
/* ~1152 cycles to FTFont -drawString:... */
/* ~1375 cycles to -drawString:... setup */
/* ~1968 cycles cmap lookup */
/* ~2718 cycles sbit lookup */
/* ~~2750 cycles blit setup */
/* ~3140 cycles blit loop, empty call */
/* ~3140 cycles blit loop, setup */
/* ~3325 cycles blit loop, no write */
/* ~3800 cycles total */
}
/* underline */
if (ch->attr&0x4)
DPSrectfill(cur,scr_x,scr_y,fx,1);
}
}
}
if (draw_cursor)
{
float x,y;
[[TerminalViewDisplayPrefs cursorColor] set];
x=cursor_x*fx+border_x;
y=(sy-1-cursor_y+current_scroll)*fy+border_y;
switch ([TerminalViewDisplayPrefs cursorStyle])
{
case CURSOR_LINE:
DPSrectfill(cur,x,y,fx,fy*0.1);
break;
case CURSOR_BLOCK_STROKE:
DPSrectstroke(cur,x+0.5,y+0.5,fx-1.0,fy-1.0);
break;
case CURSOR_BLOCK_FILL:
DPSrectfill(cur,x,y,fx,fy);
break;
case CURSOR_BLOCK_INVERT:
DPScompositerect(cur,x,y,fx,fy,
NSCompositeHighlight);
break;
}
draw_cursor=NO;
}
NSDebugLLog(@"draw",@"total_draw=%i",total_draw);
draw_all=1;
}
-(BOOL) isOpaque
{
return YES;
}
-(void) setNeedsDisplayInRect: (NSRect)r
{
draw_all=2;
[super setNeedsDisplayInRect: r];
}
-(void) setNeedsLazyDisplayInRect: (NSRect)r
{
if (draw_all==1)
draw_all=0;
[super setNeedsDisplayInRect: r];
}
-(void) benchmark: (id)sender
{
int i;
double t1,t2;
NSRect r=[self frame];
t1=[NSDate timeIntervalSinceReferenceDate];
total_draw=0;
for (i=0;i<100;i++)
{
draw_all=2;
[self lockFocus];
[self drawRect: r];
[self unlockFocusNeedsFlush: NO];
}
t2=[NSDate timeIntervalSinceReferenceDate];
t2-=t1;
fprintf(stderr,"%8.4f %8.5f/redraw total_draw=%i\n",t2,t2/i,total_draw);
}
-(void) ts_setTitle: (NSString *)new_title type: (int)title_type
{
NSDebugLLog(@"ts",@"setTitle: %@ type: %i",new_title,title_type);
if (title_type==1 || title_type==0)
ASSIGN(title_miniwindow,new_title);
if (title_type==2 || title_type==0)
ASSIGN(title_window,new_title);
if (title_type==3)
ASSIGN(title_filename,new_title);
[[NSNotificationCenter defaultCenter]
postNotificationName: TerminalViewTitleDidChangeNotification
object: self];
}
-(void) ts_goto: (int)x :(int)y
{
NSDebugLLog(@"ts",@"goto: %i:%i",x,y);
cursor_x=x;
cursor_y=y;
if (cursor_x>=sx) cursor_x=sx-1;
if (cursor_x<0) cursor_x=0;
if (cursor_y>=sy) cursor_y=sy-1;
if (cursor_y<0) cursor_y=0;
}
-(void) ts_putChar: (screen_char_t)ch count: (int)c at: (int)x :(int)y
{
int i;
screen_char_t *s;
NSDebugLLog(@"ts",@"putChar: '%c' %02x %02x count: %i at: %i:%i",
ch.ch,ch.color,ch.attr,c,x,y);
if (y<0 || y>=sy) return;
if (x+c>sx)
c=sx-x;
if (x<0)
{
c-=x;
x=0;
}
s=&SCREEN(x,y);
ch.attr|=0x80;
for (i=0;i<c;i++)
*s++=ch;
ADD_DIRTY(x,y,c,1);
}
-(void) ts_putChar: (screen_char_t)ch count: (int)c offset: (int)ofs
{
int i;
screen_char_t *s;
NSDebugLLog(@"ts",@"putChar: '%c' %02x %02x count: %i offset: %i",
ch.ch,ch.color,ch.attr,c,ofs);
if (ofs+c>sx*sy)
c=sx*sy-ofs;
if (ofs<0)
{
c-=ofs;
ofs=0;
}
s=&SCREEN(ofs,0);
ch.attr|=0x80;
for (i=0;i<c;i++)
*s++=ch;
ADD_DIRTY(0,0,sx,sy); /* TODO */
}
-(void) ts_scrollUp: (int)t :(int)b rows: (int)nr save: (BOOL)save
{
screen_char_t *d, *s;
NSDebugLLog(@"ts",@"scrollUp: %i:%i rows: %i save: %i",
t,b,nr,save);
if (save && t==0 && b==sy) /* TODO? */
{
int num;
if (nr<max_scrollback)
{
memmove(sbuf,&sbuf[sx*nr],sizeof(screen_char_t)*sx*(max_scrollback-nr));
num=nr;
}
else
num=max_scrollback;
if (num<sy)
{
memmove(&sbuf[sx*(max_scrollback-num)],screen,num*sx*sizeof(screen_char_t));
}
else
{
memmove(&sbuf[sx*(max_scrollback-num)],screen,sy*sx*sizeof(screen_char_t));
/* TODO: should this use video_erase_char? */
memset(&sbuf[sx*(max_scrollback-num+sy)],0,sx*(num-sy)*sizeof(screen_char_t));
}
sb_length+=num;
if (sb_length>max_scrollback)
sb_length=max_scrollback;
}
if (t+nr >= b)
nr = b - t - 1;
if (b > sy || t >= b || nr < 1)
return;
d = &SCREEN(0,t);
s = &SCREEN(0,t+nr);
if (current_y>=t && current_y<=b)
{
SCREEN(current_x,current_y).attr|=0x80;
draw_cursor=YES;
/*
TODO: does this properly handle the case when the cursor is in
an area that gets scrolled 'over'?
now it does, but not in an optimal way. handling of this could be
optimized in all scrolling methods, but it probably won't make
much difference
*/
}
memmove(d, s, (b-t-nr) * sx * sizeof(screen_char_t));
if (!current_scroll)
{
if (t==0 && b==sy)
{
pending_scroll-=nr;
}
else
{
float x0,y0,w,h,dx,dy;
if (pending_scroll)
[self _handlePendingScroll: YES];
x0=0;
w=fx*sx;
y0=(t+nr)*fy;
h=(b-t-nr)*fy;
dx=0;
dy=t*fy;
y0=sy*fy-y0-h;
dy=sy*fy-dy-h;
[self lockFocus];
DPScomposite(GSCurrentContext(),border_x+x0,border_y+y0,w,h,
[self gState],border_x+dx,border_y+dy,NSCompositeCopy);
[self unlockFocusNeedsFlush: NO];
num_scrolls++;
}
}
ADD_DIRTY(0,t,sx,b-t);
}
-(void) ts_scrollDown: (int)t :(int)b rows: (int)nr
{
screen_char_t *s;
unsigned int step;
NSDebugLLog(@"ts",@"scrollDown: %i:%i rows: %i",
t,b,nr);
if (t+nr >= b)
nr = b - t - 1;
if (b > sy || t >= b || nr < 1)
return;
s = &SCREEN(0,t);
step = sx * nr;
if (current_y>=t && current_y<=b)
{
SCREEN(current_x,current_y).attr|=0x80;
draw_cursor=YES;
}
memmove(s + step, s, (b-t-nr)*sx*sizeof(screen_char_t));
if (!current_scroll)