-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvt100.js
3126 lines (2946 loc) · 117 KB
/
vt100.js
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
// VT100.js -- JavaScript based terminal emulator
// Copyright (C) 2008-2009 Markus Gutschke <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// In addition to these license terms, the author grants the following
// additional rights:
//
// If you modify this program, or any covered work, by linking or
// combining it with the OpenSSL project's OpenSSL library (or a
// modified version of that library), containing parts covered by the
// terms of the OpenSSL or SSLeay licenses, the author
// grants you additional permission to convey the resulting work.
// Corresponding Source for a non-source form of such a combination
// shall include the source code for the parts of OpenSSL used as well
// as that of the covered work.
//
// You may at your option choose to remove this additional permission from
// the work, or from any part of it.
//
// It is possible to build this program in a way that it loads OpenSSL
// libraries at run-time. If doing so, the following notices are required
// by the OpenSSL and SSLeay licenses:
//
// This product includes software developed by the OpenSSL Project
// for use in the OpenSSL Toolkit. (http://www.openssl.org/)
//
// This product includes cryptographic software written by Eric Young
// ([email protected])
//
//
// The most up-to-date version of this program is always available from
// http://shellinabox.com
//
//
// Notes:
//
// The author believes that for the purposes of this license, you meet the
// requirements for publishing the source code, if your web server publishes
// the source in unmodified form (i.e. with licensing information, comments,
// formatting, and identifier names intact). If there are technical reasons
// that require you to make changes to the source code when serving the
// JavaScript (e.g to remove pre-processor directives from the source), these
// changes should be done in a reversible fashion.
//
// The author does not consider websites that reference this script in
// unmodified form, and web servers that serve this script in unmodified form
// to be derived works. As such, they are believed to be outside of the
// scope of this license and not subject to the rights or restrictions of the
// GNU General Public License.
//
// If in doubt, consult a legal professional familiar with the laws that
// apply in your country.
// #define ESnormal 0
// #define ESesc 1
// #define ESsquare 2
// #define ESgetpars 3
// #define ESgotpars 4
// #define ESdeviceattr 5
// #define ESfunckey 6
// #define EShash 7
// #define ESsetG0 8
// #define ESsetG1 9
// #define ESsetG2 10
// #define ESsetG3 11
// #define ESbang 12
// #define ESpercent 13
// #define ESignore 14
// #define ESnonstd 15
// #define ESpalette 16
// #define ESstatus 17
// #define ESss2 18
// #define ESss3 19
// #define ATTR_DEFAULT 0x00F0
// #define ATTR_REVERSE 0x0100
// #define ATTR_UNDERLINE 0x0200
// #define ATTR_DIM 0x0400
// #define ATTR_BRIGHT 0x0800
// #define ATTR_BLINK 0x1000
// #define MOUSE_DOWN 0
// #define MOUSE_UP 1
// #define MOUSE_CLICK 2
function VT100(container) {
this.initializeElements(container);
this.initializeAnsiColors();
this.maxScrollbackLines = 500;
this.npar = 0;
this.par = [ ];
this.isQuestionMark = false;
this.savedX = [ ];
this.savedY = [ ];
this.savedAttr = [ ];
this.savedUseGMap = 0;
this.savedGMap = [ this.Latin1Map, this.VT100GraphicsMap,
this.CodePage437Map, this.DirectToFontMap ];
this.savedValid = [ ];
this.respondString = '';
this.statusString = '';
this.internalClipboard = undefined;
this.reset(true);
}
VT100.prototype.reset = function(clearHistory) {
this.isEsc = 0 /* ESnormal */;
this.needWrap = false;
this.autoWrapMode = true;
this.dispCtrl = false;
this.toggleMeta = false;
this.insertMode = false;
this.applKeyMode = false;
this.cursorKeyMode = false;
this.crLfMode = false;
this.offsetMode = false;
this.mouseReporting = false;
this.utfEnabled = true;
this.visualBell = typeof suppressAllAudio !=
'undefined' &&
suppressAllAudio;
this.utfCount = 0;
this.utfChar = 0;
this.style = '';
this.attr = 0x00F0 /* ATTR_DEFAULT */;
this.useGMap = 0;
this.GMap = [ this.Latin1Map,
this.VT100GraphicsMap,
this.CodePage437Map,
this.DirectToFontMap ];
this.translate = this.GMap[this.useGMap];
this.top = 0;
this.bottom = this.terminalHeight;
this.lastCharacter = ' ';
this.userTabStop = [ ];
if (clearHistory) {
for (var i = 0; i < 2; i++) {
while (this.console[i].firstChild) {
this.console[i].removeChild(this.console[i].firstChild);
}
}
}
this.enableAlternateScreen(false);
this.gotoXY(0, 0);
this.showCursor();
this.isInverted = false;
this.refreshInvertedState();
this.clearRegion(0, 0, this.terminalWidth, this.terminalHeight, this.style);
};
VT100.prototype.initializeAnsiColors = function() {
var elem = document.createElement('pre');
this.container.appendChild(elem);
this.setTextContent(elem, ' ');
this.ansi = [ ];
for (var i = 0; i < 16; i++) {
elem.id = 'ansi' + i;
this.ansi[i] = this.getCurrentComputedStyle(elem, 'backgroundColor');
}
this.container.removeChild(elem);
};
VT100.prototype.addListener = function(elem, event, listener) {
if (elem.addEventListener) {
elem.addEventListener(event, listener, false);
} else {
elem.attachEvent('on' + event, listener);
}
};
VT100.prototype.initializeElements = function(container) {
// If the necessary objects have not already been defined in the HTML
// page, create them now.
if (container) {
this.container = container;
} else if (!(this.container = document.getElementById('vt100'))) {
this.container = document.createElement('div');
this.container.id = 'vt100';
document.body.appendChild(this.container);
}
if (!this.getChildById(this.container, 'reconnect') ||
!this.getChildById(this.container, 'menu') ||
!this.getChildById(this.container, 'scrollable') ||
!this.getChildById(this.container, 'console') ||
!this.getChildById(this.container, 'alt_console') ||
!this.getChildById(this.container, 'ieprobe') ||
!this.getChildById(this.container, 'padding') ||
!this.getChildById(this.container, 'cursor') ||
!this.getChildById(this.container, 'lineheight') ||
!this.getChildById(this.container, 'space') ||
!this.getChildById(this.container, 'input') ||
!this.getChildById(this.container, 'cliphelper') ||
!this.getChildById(this.container, 'attrib')) {
// Only enable the "embed" object, if we have a suitable plugin. Otherwise,
// we might get a pointless warning that a suitable plugin is not yet
// installed. If in doubt, we'd rather just stay silent.
var embed = '';
try {
if (typeof navigator.mimeTypes["audio/x-wav"].enabledPlugin.name !=
'undefined') {
embed = typeof suppressAllAudio != 'undefined' &&
suppressAllAudio ? "" :
'<embed classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" ' +
'id="beep_embed" ' +
'src="beep.wav" ' +
'autostart="false" ' +
'volume="100" ' +
'enablejavascript="true" ' +
'type="audio/x-wav" ' +
'height="16" ' +
'width="200" ' +
'style="position:absolute;left:-1000px;top:-1000px" />';
}
} catch (e) {
}
this.container.innerHTML =
'<div id="reconnect" style="visibility: hidden">' +
'<input type="button" value="Connect" ' +
'onsubmit="return false" />' +
'</div>' +
'<div id="menu"></div>' +
'<div id="scrollable">' +
'<pre id="lineheight"> </pre>' +
'<pre id="console">' +
'<pre></pre>' +
'<div id="ieprobe"><span> </span></div>' +
'</pre>' +
'<pre id="alt_console" style="display: none"></pre>' +
'<div id="padding"></div>' +
'<pre id="cursor"> </pre>' +
'</div>' +
'<div class="hidden">' +
'<pre><div><span id="space"></span></div></pre>' +
'<input type="textfield" id="input" />' +
'<input type="textfield" id="cliphelper" />' +
'<span id="attrib"> </span>' +
(typeof suppressAllAudio != 'undefined' &&
suppressAllAudio ? "" :
embed + '<bgsound id="beep_bgsound" loop=1 />') +
'</div>';
}
// Find the object used for playing the "beep" sound, if any.
if (typeof suppressAllAudio != 'undefined' && suppressAllAudio) {
this.beeper = undefined;
} else {
this.beeper = this.getChildById(this.container,
'beep_embed');
if (!this.beeper || !this.beeper.Play) {
this.beeper = this.getChildById(this.container,
'beep_bgsound');
if (!this.beeper || typeof this.beeper.src == 'undefined') {
this.beeper = undefined;
}
}
}
// Initialize the variables for finding the text console and the
// cursor.
this.reconnectBtn = this.getChildById(this.container,'reconnect');
this.menu = this.getChildById(this.container, 'menu');
this.scrollable = this.getChildById(this.container,
'scrollable');
this.lineheight = this.getChildById(this.container,
'lineheight');
this.console =
[ this.getChildById(this.container, 'console'),
this.getChildById(this.container, 'alt_console') ];
var ieProbe = this.getChildById(this.container, 'ieprobe');
this.padding = this.getChildById(this.container, 'padding');
this.cursor = this.getChildById(this.container, 'cursor');
this.space = this.getChildById(this.container, 'space');
this.input = this.getChildById(this.container, 'input');
this.cliphelper = this.getChildById(this.container,
'cliphelper');
this.attributeHelper = this.getChildById(this.container, 'attrib');
// Remember the dimensions of a standard character glyph. We would
// expect that we could just check cursor.clientWidth/Height at any time,
// but it turns out that browsers sometimes invalidate these values
// (e.g. while displaying a print preview screen).
this.cursorWidth = this.cursor.clientWidth;
this.cursorHeight = this.lineheight.clientHeight;
// IE has a slightly different boxing model, that we need to compensate for
this.isIE = ieProbe.offsetTop > 1;
ieProbe = undefined;
this.console.innerHTML = '';
// Determine if the terminal window is positioned at the beginning of the
// page, or if it is embedded somewhere else in the page. For full-screen
// terminals, automatically resize whenever the browser window changes.
var marginTop = parseInt(this.getCurrentComputedStyle(
document.body, 'marginTop'));
var marginLeft = parseInt(this.getCurrentComputedStyle(
document.body, 'marginLeft'));
var marginRight = parseInt(this.getCurrentComputedStyle(
document.body, 'marginRight'));
var x = this.container.offsetLeft;
var y = this.container.offsetTop;
for (var parent = this.container; parent = parent.offsetParent; ) {
x += parent.offsetLeft;
y += parent.offsetTop;
}
this.isEmbedded = marginTop != y ||
marginLeft != x ||
(window.innerWidth ||
document.documentElement.clientWidth ||
document.body.clientWidth) -
marginRight != x + this.container.offsetWidth;
if (!this.isEmbedded) {
this.addListener(window, 'resize',
function(vt100) {
return function() {
vt100.hideContextMenu();
vt100.resizer();
}
}(this));
// Hide extra scrollbars attached to window
document.body.style.margin = '0px';
try { document.body.style.overflow ='hidden'; } catch (e) { }
try { document.body.oncontextmenu = function() {return false;};} catch(e){}
}
// Hide context menu
this.hideContextMenu();
// Add listener to reconnect button
this.addListener(this.reconnectBtn.firstChild, 'click',
function(vt100) {
return function() {
var rc = vt100.reconnect();
vt100.input.focus();
return rc;
}
}(this));
// Add input listeners
this.addListener(this.input, 'blur',
function(vt100) {
return function() { vt100.blurCursor(); } }(this));
this.addListener(this.input, 'focus',
function(vt100) {
return function() { vt100.focusCursor(); } }(this));
this.addListener(this.input, 'keydown',
function(vt100) {
return function(e) {
if (!e) e = window.event;
return vt100.keyDown(e); } }(this));
this.addListener(this.input, 'keypress',
function(vt100) {
return function(e) {
if (!e) e = window.event;
return vt100.keyPressed(e); } }(this));
this.addListener(this.input, 'keyup',
function(vt100) {
return function(e) {
if (!e) e = window.event;
return vt100.keyUp(e); } }(this));
// Attach listeners that move the focus to the <input> field. This way we
// can make sure that we can receive keyboard input.
var mouseEvent = function(vt100, type) {
return function(e) {
if (!e) e = window.event;
return vt100.mouseEvent(e, type);
};
};
this.addListener(this.scrollable,'mousedown',mouseEvent(this, 0 /* MOUSE_DOWN */));
this.addListener(this.scrollable,'mouseup', mouseEvent(this, 1 /* MOUSE_UP */));
this.addListener(this.scrollable,'click', mouseEvent(this, 2 /* MOUSE_CLICK */));
// Initialize the blank terminal window.
this.currentScreen = 0;
this.cursorX = 0;
this.cursorY = 0;
this.numScrollbackLines = 0;
this.top = 0;
this.bottom = 0x7FFFFFFF;
this.resizer();
this.focusCursor();
this.input.focus();
};
VT100.prototype.getChildById = function(parent, id) {
var nodeList = parent.all || parent.getElementsByTagName('*');
if (typeof nodeList.namedItem == 'undefined') {
for (var i = 0; i < nodeList.length; i++) {
if (nodeList[i].id == id) {
return nodeList[i];
}
}
return null;
} else {
var elem = (parent.all || parent.getElementsByTagName('*')).namedItem(id);
return elem ? elem[0] || elem : null;
}
};
VT100.prototype.getCurrentComputedStyle = function(elem, style) {
if (typeof elem.currentStyle != 'undefined') {
return elem.currentStyle[style];
} else {
return document.defaultView.getComputedStyle(elem, null)[style];
}
};
VT100.prototype.reconnect = function() {
return false;
};
VT100.prototype.showReconnect = function(state) {
if (state) {
this.reconnectBtn.style.visibility = '';
} else {
this.reconnectBtn.style.visibility = 'hidden';
}
};
VT100.prototype.repairElements = function(console) {
for (var line = console.firstChild; line; line = line.nextSibling) {
if (!line.clientHeight) {
var newLine = document.createElement(line.tagName);
newLine.style.cssText = line.style.cssText;
newLine.className = line.className;
if (line.tagName == 'DIV') {
for (var span = line.firstChild; span; span = span.nextSibling) {
var newSpan = document.createElement(span.tagName);
newSpan.style.cssText = span.style.cssText;
this.setTextContent(newSpan, this.getTextContent(span));
newLine.appendChild(newSpan);
}
} else {
this.setTextContent(newLine, this.getTextContent(line));
}
line.parentNode.replaceChild(newLine, line);
line = newLine;
}
}
};
VT100.prototype.resized = function(w, h) {
};
VT100.prototype.resizer = function() {
// The cursor can get corrupted if the print-preview is displayed in Firefox.
// Recreating it, will repair it.
var newCursor = document.createElement('pre');
this.setTextContent(newCursor, ' ');
newCursor.id = 'cursor';
newCursor.style.cssText = this.cursor.style.cssText;
this.cursor.parentNode.insertBefore(newCursor, this.cursor);
if (!newCursor.clientHeight) {
// Things are broken right now. This is probably because we are
// displaying the print-preview. Just don't change any of our settings
// until the print dialog is closed again.
newCursor.parentNode.removeChild(newCursor);
return;
} else {
// Swap the old broken cursor for the newly created one.
this.cursor.parentNode.removeChild(this.cursor);
this.cursor = newCursor;
}
// Really horrible things happen if the contents of the terminal changes
// while the print-preview is showing. We get HTML elements that show up
// in the DOM, but that do not take up any space. Find these elements and
// try to fix them.
this.repairElements(this.console[0]);
this.repairElements(this.console[1]);
// Lock the cursor size to the size of a normal character. This helps with
// characters that are taller/shorter than normal. Unfortunately, we will
// still get confused if somebody enters a character that is wider/narrower
// than normal. This can happen if the browser tries to substitute a
// characters from a different font.
this.cursor.style.width = this.cursorWidth + 'px';
this.cursor.style.height = this.cursorHeight + 'px';
// Adjust height for one pixel padding of the #vt100 element.
// The latter is necessary to properly display the inactive cursor.
var console = this.console[this.currentScreen];
var height = (this.isEmbedded ? this.container.clientHeight
: (window.innerHeight ||
document.documentElement.clientHeight ||
document.body.clientHeight))-1;
var partial = height % this.cursorHeight;
this.scrollable.style.height = (height > 0 ? height : 0) + 'px';
this.padding.style.height = (partial > 0 ? partial : 0) + 'px';
var oldTerminalHeight = this.terminalHeight;
this.updateWidth();
this.updateHeight();
// Clip the cursor to the visible screen.
var cx = this.cursorX;
var cy = this.cursorY + this.numScrollbackLines;
// The alternate screen never keeps a scroll back buffer.
this.updateNumScrollbackLines();
while (this.currentScreen && this.numScrollbackLines > 0) {
console.removeChild(console.firstChild);
this.numScrollbackLines--;
}
cy -= this.numScrollbackLines;
if (cx < 0) {
cx = 0;
} else if (cx > this.terminalWidth) {
cx = this.terminalWidth - 1;
if (cx < 0) {
cx = 0;
}
}
if (cy < 0) {
cy = 0;
} else if (cy > this.terminalHeight) {
cy = this.terminalHeight - 1;
if (cy < 0) {
cy = 0;
}
}
// Clip the scroll region to the visible screen.
if (this.bottom > this.terminalHeight ||
this.bottom == oldTerminalHeight) {
this.bottom = this.terminalHeight;
}
if (this.top >= this.bottom) {
this.top = this.bottom-1;
if (this.top < 0) {
this.top = 0;
}
}
// Truncate lines, if necessary. Explicitly reposition cursor (this is
// particularly important after changing the screen number), and reset
// the scroll region to the default.
this.truncateLines(this.terminalWidth);
this.putString(cx, cy, '', undefined);
this.scrollable.scrollTop = this.numScrollbackLines *
this.cursorHeight + 1;
// Update classNames for lines in the scrollback buffer
var line = console.firstChild;
for (var i = 0; i < this.numScrollbackLines; i++) {
line.className = 'scrollback';
line = line.nextSibling;
}
while (line) {
line.className = '';
line = line.nextSibling;
}
// Reposition the reconnect button
this.reconnectBtn.style.left = (this.terminalWidth*this.cursorWidth -
this.reconnectBtn.clientWidth)/2 + 'px';
this.reconnectBtn.style.top = (this.terminalHeight*this.cursorHeight-
this.reconnectBtn.clientHeight)/2 + 'px';
// Send notification that the window size has been changed
this.resized(this.terminalWidth, this.terminalHeight);
};
VT100.prototype.selection = function() {
try {
return '' + (window.getSelection && window.getSelection() ||
document.selection && document.selection.type == 'Text' &&
document.selection.createRange().text || '');
} catch (e) {
}
return '';
};
VT100.prototype.cancelEvent = function(event) {
try {
// For non-IE browsers
event.stopPropagation();
event.preventDefault();
} catch (e) {
}
try {
// For IE
event.cancelBubble = true;
event.returnValue = false;
event.button = 0;
event.keyCode = 0;
} catch (e) {
}
return false;
};
VT100.prototype.mouseEvent = function(event, type) {
// If any text is currently selected, do not move the focus as that would
// invalidate the selection.
var selection = this.selection();
if ((type == 1 /* MOUSE_UP */ || type == 2 /* MOUSE_CLICK */) && !selection.length) {
this.input.focus();
}
// Compute mouse position in characters.
var offsetX = this.container.offsetLeft;
var offsetY = this.container.offsetTop;
for (var e = this.container; e = e.offsetParent; ) {
offsetX += e.offsetLeft;
offsetY += e.offsetTop;
}
var x = (event.clientX - offsetX) / this.cursorWidth;
var y = ((event.clientY - offsetY) + this.scrollable.offsetTop) /
this.cursorHeight - this.numScrollbackLines;
var inside = true;
if (x >= this.terminalWidth) {
x = this.terminalWidth - 1;
inside = false;
}
if (x < 0) {
x = 0;
inside = false;
}
if (y >= this.terminalHeight) {
y = this.terminalHeight - 1;
inside = false;
}
if (y < 0) {
y = 0;
inside = false;
}
// Compute button number and modifier keys.
var button = type != 0 /* MOUSE_DOWN */ ? 3 :
typeof event.pageX != 'undefined' ? event.button :
[ undefined, 0, 2, 0, 1, 0, 1, 0 ][event.button];
if (button != undefined) {
if (event.shiftKey) {
button |= 0x04;
}
if (event.altKey || event.metaKey) {
button |= 0x08;
}
if (event.ctrlKey) {
button |= 0x10;
}
}
// Report mouse events if they happen inside of the current screen and
// with the SHIFT key unpressed. Both of these restrictions do not apply
// for button releases, as we always want to report those.
if (this.mouseReporting && !selection.length &&
(type != 0 /* MOUSE_DOWN */ || !event.shiftKey)) {
if (inside || type != 0 /* MOUSE_DOWN */) {
if (button != undefined) {
var report = '\u001B[M' + String.fromCharCode(button + 32) +
String.fromCharCode(x + 33) +
String.fromCharCode(y + 33);
if (type != 2 /* MOUSE_CLICK */) {
this.keysPressed(report);
}
// If we reported the event, stop propagating it (not sure, if this
// actually works on most browsers; blocking the global "oncontextmenu"
// even is still necessary).
return this.cancelEvent(event);
}
}
}
// Bring up context menu.
if (button == 2 && !event.shiftKey) {
if (type == 0 /* MOUSE_DOWN */) {
this.showContextMenu(event.clientX - offsetX, event.clientY - offsetY);
}
return this.cancelEvent(event);
}
if (this.mouseReporting) {
try {
event.shiftKey = false;
} catch (e) {
}
}
return true;
};
VT100.prototype.getTextContent = function(elem) {
return elem.textContent ||
(typeof elem.textContent == 'undefined' ? elem.innerText : '');
};
VT100.prototype.setTextContent = function(elem, s) {
// Updating the content of an element is an expensive operation. It actually
// pays off to first check whether the element is still unchanged.
if (typeof elem.textContent == 'undefined') {
if (elem.innerText != s) {
elem.innerText = s;
}
} else {
if (elem.textContent != s) {
elem.textContent = s;
}
}
};
VT100.prototype.insertBlankLine = function(y, style) {
// Insert a blank line a position y. This method ignores the scrollback
// buffer. The caller has to add the length of the scrollback buffer to
// the position, if necessary.
// If the position is larger than the number of current lines, this
// method just adds a new line right after the last existing one. It does
// not add any missing lines in between. It is the caller's responsibility
// to do so.
if (style == undefined) {
style = '';
}
var line;
if (!style) {
line = document.createElement('pre');
this.setTextContent(line, '\n');
} else {
line = document.createElement('div');
var span = document.createElement('span');
span.style.cssText = style;
this.setTextContent(span, this.spaces(this.terminalWidth));
line.appendChild(span);
}
line.style.height = this.cursorHeight + 'px';
var console = this.console[this.currentScreen];
if (console.childNodes.length > y) {
console.insertBefore(line, console.childNodes[y]);
} else {
console.appendChild(line);
}
};
VT100.prototype.updateWidth = function() {
this.terminalWidth = Math.floor(this.console[this.currentScreen].offsetWidth/
this.cursorWidth);
return this.terminalWidth;
};
VT100.prototype.updateHeight = function() {
// We want to be able to display either a terminal window that fills the
// entire browser window, or a terminal window that is contained in a
// <div> which is embededded somewhere in the web page.
if (this.isEmbedded) {
// Embedded terminal. Use size of the containing <div> (id="vt100").
this.terminalHeight = Math.floor((this.container.clientHeight-1) /
this.cursorHeight);
} else {
// Use the full browser window.
this.terminalHeight = Math.floor(((window.innerHeight ||
document.documentElement.clientHeight ||
document.body.clientHeight)-1)/
this.cursorHeight);
}
return this.terminalHeight;
};
VT100.prototype.updateNumScrollbackLines = function() {
var scrollback = Math.floor(
this.console[this.currentScreen].offsetHeight /
this.cursorHeight) -
this.terminalHeight;
this.numScrollbackLines = scrollback < 0 ? 0 : scrollback;
return this.numScrollbackLines;
};
VT100.prototype.truncateLines = function(width) {
if (width < 0) {
width = 0;
}
for (var line = this.console[this.currentScreen].firstChild; line;
line = line.nextSibling) {
if (line.tagName == 'DIV') {
var x = 0;
// Traverse current line and truncate if once we saw "width" characters
for (var span = line.firstChild; span;
span = span.nextSibling) {
var s = this.getTextContent(span);
var l = s.length;
if (x + l > width) {
this.setTextContent(span, s.substr(0, width - x));
while (span.nextSibling) {
line.removeChild(line.lastChild);
}
break;
}
x += l;
}
// Prune white space from the end of the current line
var span = line.lastChild;
while (span && !span.style.cssText.length) {
// Scan backwards looking for first non-space character
var s = this.getTextContent(span);
for (var i = s.length; i--; ) {
if (s.charAt(i) != ' ') {
if (i+1 != s.length) {
this.setTextContent(s.substr(0, i+1));
}
span = null;
break;
}
}
if (span) {
var sibling = span;
span = span.previousSibling;
if (span) {
// Remove blank <span>'s from end of line
line.removeChild(sibling);
} else {
// Remove entire line (i.e. <div>), if empty
var blank = document.createElement('pre');
blank.style.height = this.cursorHeight + 'px';
this.setTextContent(blank, '\n');
line.parentNode.replaceChild(blank, line);
}
}
}
}
}
};
VT100.prototype.putString = function(x, y, text, style) {
if (!style) {
style = '';
}
var yIdx = y + this.numScrollbackLines;
var line;
var sibling;
var s;
var span;
var xPos = 0;
var console = this.console[this.currentScreen];
if (!text.length && (yIdx >= console.childNodes.length ||
console.childNodes[yIdx].tagName != 'DIV')) {
// Positioning cursor to a blank location
span = null;
} else {
// Create missing blank lines at end of page
while (console.childNodes.length <= yIdx) {
// In order to simplify lookups, we want to make sure that each line
// is represented by exactly one element (and possibly a whole bunch of
// children).
// For non-blank lines, we can create a <div> containing one or more
// <span>s. For blank lines, this fails as browsers tend to optimize them
// away. But fortunately, a <pre> tag containing a newline character
// appears to work for all browsers (a would also work, but then
// copying from the browser window would insert superfluous spaces into
// the clipboard).
this.insertBlankLine(yIdx);
}
line = console.childNodes[yIdx];
// If necessary, promote blank '\n' line to a <div> tag
if (line.tagName != 'DIV') {
var div = document.createElement('div');
div.style.height = this.cursorHeight + 'px';
div.innerHTML = '<span></span>';
console.replaceChild(div, line);
line = div;
}
// Scan through list of <span>'s until we find the one where our text
// starts
span = line.firstChild;
var len;
while (span.nextSibling && xPos < x) {
len = this.getTextContent(span).length;
if (xPos + len > x) {
break;
}
xPos += len;
span = span.nextSibling;
}
if (text.length) {
// If current <span> is not long enough, pad with spaces or add new
// span
s = this.getTextContent(span);
var oldStyle = span.style.cssText;
if (xPos + s.length < x) {
if (oldStyle != '') {
span = document.createElement('span');
line.appendChild(span);
span.style.cssText = '';
oldStyle = '';
xPos += s.length;
s = '';
}
do {
s += ' ';
} while (xPos + s.length < x);
}
// If styles do not match, create a new <span>
var del = text.length - s.length + x - xPos;
if (oldStyle != style && (oldStyle || style)) {
if (xPos == x) {
// Replacing text at beginning of existing <span>
if (text.length >= s.length) {
// New text is equal or longer than existing text
s = text;
} else {
// Insert new <span> before the current one, then remove leading
// part of existing <span>, adjust style of new <span>, and finally
// set its contents
sibling = document.createElement('span');
line.insertBefore(sibling, span);
this.setTextContent(span, s.substr(text.length));
span = sibling;
s = text;
}
} else {
// Replacing text some way into the existing <span>
var remainder = s.substr(x + text.length - xPos);
this.setTextContent(span, s.substr(0, x - xPos));
xPos = x;
sibling = document.createElement('span');
if (span.nextSibling) {
line.insertBefore(sibling, span.nextSibling);
span = sibling;
if (remainder.length) {
sibling = document.createElement('span');
sibling.style.cssText = oldStyle;
this.setTextContent(sibling, remainder);
line.insertBefore(sibling, span.nextSibling);
}
} else {
line.appendChild(sibling);
span = sibling;
if (remainder.length) {
sibling = document.createElement('span');
sibling.style.cssText = oldStyle;
this.setTextContent(sibling, remainder);
line.appendChild(sibling);
}
}
s = text;
}
span.style.cssText = style;
} else {
// Overwrite (partial) <span> with new text
s = s.substr(0, x - xPos) +
text +
s.substr(x + text.length - xPos);
}
this.setTextContent(span, s);
// Delete all subsequent <span>'s that have just been overwritten
sibling = span.nextSibling;
while (del > 0 && sibling) {
s = this.getTextContent(sibling);
len = s.length;
if (len <= del) {
line.removeChild(sibling);
del -= len;
sibling = span.nextSibling;
} else {
this.setTextContent(sibling, s.substr(del));
break;
}
}
// Merge <span> with next sibling, if styles are identical
if (sibling && span.style.cssText == sibling.style.cssText) {
this.setTextContent(span,
this.getTextContent(span) +
this.getTextContent(sibling));
line.removeChild(sibling);
}
}
}
// Position cursor
this.cursorX = x + text.length;
if (this.cursorX >= this.terminalWidth) {
this.cursorX = this.terminalWidth - 1;
if (this.cursorX < 0) {
this.cursorX = 0;
}
}
var pixelX = -1;
var pixelY = -1;
if (!this.cursor.style.visibility) {
var idx = this.cursorX - xPos;