2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.backend
;
31 import java
.awt
.image
.BufferedImage
;
32 import java
.io
.BufferedReader
;
33 import java
.io
.FileDescriptor
;
34 import java
.io
.FileInputStream
;
35 import java
.io
.InputStream
;
36 import java
.io
.InputStreamReader
;
37 import java
.io
.IOException
;
38 import java
.io
.OutputStream
;
39 import java
.io
.OutputStreamWriter
;
40 import java
.io
.PrintWriter
;
41 import java
.io
.Reader
;
42 import java
.io
.UnsupportedEncodingException
;
43 import java
.util
.ArrayList
;
44 import java
.util
.Collections
;
45 import java
.util
.HashMap
;
46 import java
.util
.List
;
49 import jexer
.bits
.Cell
;
50 import jexer
.bits
.CellAttributes
;
51 import jexer
.bits
.Color
;
52 import jexer
.event
.TCommandEvent
;
53 import jexer
.event
.TInputEvent
;
54 import jexer
.event
.TKeypressEvent
;
55 import jexer
.event
.TMouseEvent
;
56 import jexer
.event
.TResizeEvent
;
57 import static jexer
.TCommand
.*;
58 import static jexer
.TKeypress
.*;
61 * This class reads keystrokes and mouse events and emits output to ANSI
62 * X3.64 / ECMA-48 type terminals e.g. xterm, linux, vt100, ansi.sys, etc.
64 public class ECMA48Terminal
extends LogicalScreen
65 implements TerminalReader
, Runnable
{
67 // ------------------------------------------------------------------------
68 // Constants --------------------------------------------------------------
69 // ------------------------------------------------------------------------
72 * States in the input parser.
74 private enum ParseState
{
85 * Number of colors in the sixel palette. Xterm 335 defines the max as
88 private static final int MAX_COLOR_REGISTERS
= 1024;
89 // Black-and-white is possible too.
90 // private static final int MAX_COLOR_REGISTERS = 2;
92 // ------------------------------------------------------------------------
93 // Variables --------------------------------------------------------------
94 // ------------------------------------------------------------------------
97 * Emit debugging to stderr.
99 private boolean debugToStderr
= false;
102 * If true, emit T.416-style RGB colors for normal system colors. This
103 * is a) expensive in bandwidth, and b) potentially terrible looking for
106 private static boolean doRgbColor
= false;
109 * The session information.
111 private SessionInfo sessionInfo
;
114 * The event queue, filled up by a thread reading on input.
116 private List
<TInputEvent
> eventQueue
;
119 * If true, we want the reader thread to exit gracefully.
121 private boolean stopReaderThread
;
126 private Thread readerThread
;
129 * Parameters being collected. E.g. if the string is \033[1;3m, then
130 * params[0] will be 1 and params[1] will be 3.
132 private List
<String
> params
;
135 * Current parsing state.
137 private ParseState state
;
140 * The time we entered ESCAPE. If we get a bare escape without a code
141 * following it, this is used to return that bare escape.
143 private long escapeTime
;
146 * The time we last checked the window size. We try not to spawn stty
147 * more than once per second.
149 private long windowSizeTime
;
152 * true if mouse1 was down. Used to report mouse1 on the release event.
154 private boolean mouse1
;
157 * true if mouse2 was down. Used to report mouse2 on the release event.
159 private boolean mouse2
;
162 * true if mouse3 was down. Used to report mouse3 on the release event.
164 private boolean mouse3
;
167 * Cache the cursor visibility value so we only emit the sequence when we
170 private boolean cursorOn
= true;
173 * Cache the last window size to figure out if a TResizeEvent needs to be
176 private TResizeEvent windowResize
= null;
179 * Window width in pixels. Used for sixel support.
181 private int widthPixels
= 640;
184 * Window height in pixels. Used for sixel support.
186 private int heightPixels
= 400;
189 * If true, emit image data via sixel.
191 private boolean sixel
= true;
194 * The sixel palette handler.
196 private SixelPalette palette
= null;
199 * The sixel post-rendered string cache.
201 private SixelCache sixelCache
= null;
204 * If true, then we changed System.in and need to change it back.
206 private boolean setRawMode
;
209 * The terminal's input. If an InputStream is not specified in the
210 * constructor, then this InputStreamReader will be bound to System.in
211 * with UTF-8 encoding.
213 private Reader input
;
216 * The terminal's raw InputStream. If an InputStream is not specified in
217 * the constructor, then this InputReader will be bound to System.in.
218 * This is used by run() to see if bytes are available() before calling
219 * (Reader)input.read().
221 private InputStream inputStream
;
224 * The terminal's output. If an OutputStream is not specified in the
225 * constructor, then this PrintWriter will be bound to System.out with
228 private PrintWriter output
;
231 * The listening object that run() wakes up on new input.
233 private Object listener
;
236 * SixelPalette is used to manage the conversion of images between 24-bit
237 * RGB color and a palette of MAX_COLOR_REGISTERS colors.
239 private class SixelPalette
{
242 * Color palette for sixel output, sorted low to high.
244 private List
<Integer
> rgbColors
= new ArrayList
<Integer
>();
247 * Map of color palette index for sixel output, from the order it was
248 * generated by makePalette() to rgbColors.
250 private int [] rgbSortedIndex
= new int[MAX_COLOR_REGISTERS
];
253 * The color palette, organized by hue, saturation, and luminance.
254 * This is used for a fast color match.
256 private ArrayList
<ArrayList
<ArrayList
<ColorIdx
>>> hslColors
;
259 * Number of bits for hue.
261 private int hueBits
= -1;
264 * Number of bits for saturation.
266 private int satBits
= -1;
269 * Number of bits for luminance.
271 private int lumBits
= -1;
274 * Step size for hue bins.
276 private int hueStep
= -1;
279 * Step size for saturation bins.
281 private int satStep
= -1;
284 * Cached RGB to HSL result.
286 private int hsl
[] = new int[3];
289 * ColorIdx records a RGB color and its palette index.
291 private class ColorIdx
{
293 * The 24-bit RGB color.
298 * The palette index for this color.
303 * Public constructor.
305 * @param color the 24-bit RGB color
306 * @param index the palette index for this color
308 public ColorIdx(final int color
, final int index
) {
315 * Public constructor.
317 public SixelPalette() {
322 * Find the nearest match for a color in the palette.
324 * @param color the RGB color
325 * @return the index in rgbColors that is closest to color
327 public int matchColor(final int color
) {
332 * matchColor() is a critical performance bottleneck. To make it
333 * decent, we do the following:
335 * 1. Find the nearest two hues that bracket this color.
337 * 2. Find the nearest two saturations that bracket this color.
339 * 3. Iterate within these four bands of luminance values,
340 * returning the closest color by Euclidean distance.
342 * This strategy reduces the search space by about 97%.
344 int red
= (color
>>> 16) & 0xFF;
345 int green
= (color
>>> 8) & 0xFF;
346 int blue
= color
& 0xFF;
348 if (MAX_COLOR_REGISTERS
== 2) {
349 if (((red
* red
) + (green
* green
) + (blue
* blue
)) < 35568) {
358 rgbToHsl(red
, green
, blue
, hsl
);
362 // System.err.printf("%d %d %d\n", hue, sat, lum);
364 double diff
= Double
.MAX_VALUE
;
367 int hue1
= hue
/ (360/hueStep
);
369 if (hue1
>= hslColors
.size() - 1) {
370 // Bracket pure red from above.
371 hue1
= hslColors
.size() - 1;
373 } else if (hue1
== 0) {
374 // Bracket pure red from below.
375 hue2
= hslColors
.size() - 1;
378 for (int hI
= hue1
; hI
!= -1;) {
379 ArrayList
<ArrayList
<ColorIdx
>> sats
= hslColors
.get(hI
);
382 } else if (hI
== hue2
) {
386 int sMin
= (sat
/ satStep
) - 1;
391 } else if (sMin
== sats
.size() - 1) {
396 assert (sMax
- sMin
== 1);
399 // int sMax = sats.size() - 1;
401 for (int sI
= sMin
; sI
<= sMax
; sI
++) {
402 ArrayList
<ColorIdx
> lums
= sats
.get(sI
);
404 // True 3D colorspace match for the remaining values
405 for (ColorIdx c
: lums
) {
406 int rgbColor
= c
.color
;
408 int red2
= (rgbColor
>>> 16) & 0xFF;
409 int green2
= (rgbColor
>>> 8) & 0xFF;
410 int blue2
= rgbColor
& 0xFF;
411 newDiff
+= Math
.pow(red2
- red
, 2);
412 newDiff
+= Math
.pow(green2
- green
, 2);
413 newDiff
+= Math
.pow(blue2
- blue
, 2);
414 if (newDiff
< diff
) {
415 idx
= rgbSortedIndex
[c
.index
];
422 if (((red
* red
) + (green
* green
) + (blue
* blue
)) < diff
) {
423 // Black is a closer match.
425 } else if ((((255 - red
) * (255 - red
)) +
426 ((255 - green
) * (255 - green
)) +
427 ((255 - blue
) * (255 - blue
))) < diff
) {
429 // White is a closer match.
430 idx
= MAX_COLOR_REGISTERS
- 1;
437 * Clamp an int value to [0, 255].
439 * @param x the int value
440 * @return an int between 0 and 255.
442 private int clamp(final int x
) {
453 * Dither an image to a MAX_COLOR_REGISTERS palette. The dithered
454 * image cells will contain indexes into the palette.
456 * @param image the image to dither
457 * @return the dithered image. Every pixel is an index into the
460 public BufferedImage
ditherImage(final BufferedImage image
) {
462 BufferedImage ditheredImage
= new BufferedImage(image
.getWidth(),
463 image
.getHeight(), BufferedImage
.TYPE_INT_ARGB
);
465 int [] rgbArray
= image
.getRGB(0, 0, image
.getWidth(),
466 image
.getHeight(), null, 0, image
.getWidth());
467 ditheredImage
.setRGB(0, 0, image
.getWidth(), image
.getHeight(),
468 rgbArray
, 0, image
.getWidth());
470 for (int imageY
= 0; imageY
< image
.getHeight(); imageY
++) {
471 for (int imageX
= 0; imageX
< image
.getWidth(); imageX
++) {
472 int oldPixel
= ditheredImage
.getRGB(imageX
,
474 int colorIdx
= matchColor(oldPixel
);
475 assert (colorIdx
>= 0);
476 assert (colorIdx
< MAX_COLOR_REGISTERS
);
477 int newPixel
= rgbColors
.get(colorIdx
);
478 ditheredImage
.setRGB(imageX
, imageY
, colorIdx
);
480 int oldRed
= (oldPixel
>>> 16) & 0xFF;
481 int oldGreen
= (oldPixel
>>> 8) & 0xFF;
482 int oldBlue
= oldPixel
& 0xFF;
484 int newRed
= (newPixel
>>> 16) & 0xFF;
485 int newGreen
= (newPixel
>>> 8) & 0xFF;
486 int newBlue
= newPixel
& 0xFF;
488 int redError
= (oldRed
- newRed
) / 16;
489 int greenError
= (oldGreen
- newGreen
) / 16;
490 int blueError
= (oldBlue
- newBlue
) / 16;
492 int red
, green
, blue
;
493 if (imageX
< image
.getWidth() - 1) {
494 int pXpY
= ditheredImage
.getRGB(imageX
+ 1, imageY
);
495 red
= ((pXpY
>>> 16) & 0xFF) + (7 * redError
);
496 green
= ((pXpY
>>> 8) & 0xFF) + (7 * greenError
);
497 blue
= ( pXpY
& 0xFF) + (7 * blueError
);
499 green
= clamp(green
);
501 pXpY
= ((red
& 0xFF) << 16);
502 pXpY
|= ((green
& 0xFF) << 8) | (blue
& 0xFF);
503 ditheredImage
.setRGB(imageX
+ 1, imageY
, pXpY
);
505 if (imageY
< image
.getHeight() - 1) {
506 int pXpYp
= ditheredImage
.getRGB(imageX
+ 1,
508 red
= ((pXpYp
>>> 16) & 0xFF) + redError
;
509 green
= ((pXpYp
>>> 8) & 0xFF) + greenError
;
510 blue
= ( pXpYp
& 0xFF) + blueError
;
512 green
= clamp(green
);
514 pXpYp
= ((red
& 0xFF) << 16);
515 pXpYp
|= ((green
& 0xFF) << 8) | (blue
& 0xFF);
516 ditheredImage
.setRGB(imageX
+ 1, imageY
+ 1, pXpYp
);
518 } else if (imageY
< image
.getHeight() - 1) {
519 int pXmYp
= ditheredImage
.getRGB(imageX
- 1,
521 int pXYp
= ditheredImage
.getRGB(imageX
,
524 red
= ((pXmYp
>>> 16) & 0xFF) + (3 * redError
);
525 green
= ((pXmYp
>>> 8) & 0xFF) + (3 * greenError
);
526 blue
= ( pXmYp
& 0xFF) + (3 * blueError
);
528 green
= clamp(green
);
530 pXmYp
= ((red
& 0xFF) << 16);
531 pXmYp
|= ((green
& 0xFF) << 8) | (blue
& 0xFF);
532 ditheredImage
.setRGB(imageX
- 1, imageY
+ 1, pXmYp
);
534 red
= ((pXYp
>>> 16) & 0xFF) + (5 * redError
);
535 green
= ((pXYp
>>> 8) & 0xFF) + (5 * greenError
);
536 blue
= ( pXYp
& 0xFF) + (5 * blueError
);
538 green
= clamp(green
);
540 pXYp
= ((red
& 0xFF) << 16);
541 pXYp
|= ((green
& 0xFF) << 8) | (blue
& 0xFF);
542 ditheredImage
.setRGB(imageX
, imageY
+ 1, pXYp
);
544 } // for (int imageY = 0; imageY < image.getHeight(); imageY++)
545 } // for (int imageX = 0; imageX < image.getWidth(); imageX++)
547 return ditheredImage
;
551 * Convert an RGB color to HSL.
553 * @param red red color, between 0 and 255
554 * @param green green color, between 0 and 255
555 * @param blue blue color, between 0 and 255
556 * @param hsl the hsl color as [hue, saturation, luminance]
558 private void rgbToHsl(final int red
, final int green
,
559 final int blue
, final int [] hsl
) {
561 assert ((red
>= 0) && (red
<= 255));
562 assert ((green
>= 0) && (green
<= 255));
563 assert ((blue
>= 0) && (blue
<= 255));
565 double R
= red
/ 255.0;
566 double G
= green
/ 255.0;
567 double B
= blue
/ 255.0;
568 boolean Rmax
= false;
569 boolean Gmax
= false;
570 boolean Bmax
= false;
571 double min
= (R
< G ? R
: G
);
572 min
= (min
< B ? min
: B
);
574 if ((R
>= G
) && (R
>= B
)) {
577 } else if ((G
>= R
) && (G
>= B
)) {
580 } else if ((B
>= G
) && (B
>= R
)) {
585 double L
= (min
+ max
) / 2.0;
590 S
= (max
- min
) / (max
+ min
);
592 S
= (max
- min
) / (2.0 - max
- min
);
596 assert (Gmax
== false);
597 assert (Bmax
== false);
598 H
= (G
- B
) / (max
- min
);
600 assert (Rmax
== false);
601 assert (Bmax
== false);
602 H
= 2.0 + (B
- R
) / (max
- min
);
604 assert (Rmax
== false);
605 assert (Gmax
== false);
606 H
= 4.0 + (R
- G
) / (max
- min
);
611 hsl
[0] = (int) (H
* 60.0);
612 hsl
[1] = (int) (S
* 100.0);
613 hsl
[2] = (int) (L
* 100.0);
615 assert ((hsl
[0] >= 0) && (hsl
[0] <= 360));
616 assert ((hsl
[1] >= 0) && (hsl
[1] <= 100));
617 assert ((hsl
[2] >= 0) && (hsl
[2] <= 100));
621 * Convert a HSL color to RGB.
623 * @param hue hue, between 0 and 359
624 * @param sat saturation, between 0 and 100
625 * @param lum luminance, between 0 and 100
626 * @return the rgb color as 0x00RRGGBB
628 private int hslToRgb(final int hue
, final int sat
, final int lum
) {
629 assert ((hue
>= 0) && (hue
<= 360));
630 assert ((sat
>= 0) && (sat
<= 100));
631 assert ((lum
>= 0) && (lum
<= 100));
633 double S
= sat
/ 100.0;
634 double L
= lum
/ 100.0;
635 double C
= (1.0 - Math
.abs((2.0 * L
) - 1.0)) * S
;
636 double Hp
= hue
/ 60.0;
637 double X
= C
* (1.0 - Math
.abs((Hp
% 2) - 1.0));
644 } else if (Hp
<= 2.0) {
647 } else if (Hp
<= 3.0) {
650 } else if (Hp
<= 4.0) {
653 } else if (Hp
<= 5.0) {
656 } else if (Hp
<= 6.0) {
660 double m
= L
- (C
/ 2.0);
661 int red
= ((int) ((Rp
+ m
) * 255.0)) << 16;
662 int green
= ((int) ((Gp
+ m
) * 255.0)) << 8;
663 int blue
= (int) ((Bp
+ m
) * 255.0);
665 return (red
| green
| blue
);
669 * Create the sixel palette.
671 private void makePalette() {
672 // Generate the sixel palette. Because we have no idea at this
673 // layer which image(s) will be shown, we have to use a common
674 // palette with MAX_COLOR_REGISTERS colors for everything, and
675 // map the BufferedImage colors to their nearest neighbor in RGB
678 if (MAX_COLOR_REGISTERS
== 2) {
680 rgbColors
.add(0xFFFFFF);
681 rgbSortedIndex
[0] = 0;
682 rgbSortedIndex
[1] = 1;
686 // We build a palette using the Hue-Saturation-Luminence model,
687 // with 5+ bits for Hue, 2+ bits for Saturation, and 1+ bit for
688 // Luminance. We convert these colors to 24-bit RGB, sort them
689 // ascending, and steal the first index for pure black and the
690 // last for pure white. The 8-bit final palette favors bright
691 // colors, somewhere between pastel and classic television
692 // technicolor. 9- and 10-bit palettes are more uniform.
694 // Default at 256 colors.
699 assert (MAX_COLOR_REGISTERS
>= 256);
700 assert ((MAX_COLOR_REGISTERS
== 256)
701 || (MAX_COLOR_REGISTERS
== 512)
702 || (MAX_COLOR_REGISTERS
== 1024)
703 || (MAX_COLOR_REGISTERS
== 2048));
705 switch (MAX_COLOR_REGISTERS
) {
722 hueStep
= (int) (Math
.pow(2, hueBits
));
723 satStep
= (int) (100 / Math
.pow(2, satBits
));
724 // 1 bit for luminance: 40 and 70.
729 // 2 bits: 20, 40, 60, 80
734 // 3 bits: 8, 20, 32, 44, 56, 68, 80, 92
740 // System.err.printf("<html><body>\n");
741 // Hue is evenly spaced around the wheel.
742 hslColors
= new ArrayList
<ArrayList
<ArrayList
<ColorIdx
>>>();
744 final boolean DEBUG
= false;
745 ArrayList
<Integer
> rawRgbList
= new ArrayList
<Integer
>();
747 for (int hue
= 0; hue
< (360 - (360 % hueStep
));
748 hue
+= (360/hueStep
)) {
750 ArrayList
<ArrayList
<ColorIdx
>> satList
= null;
751 satList
= new ArrayList
<ArrayList
<ColorIdx
>>();
752 hslColors
.add(satList
);
754 // Saturation is linearly spaced between pastel and pure.
755 for (int sat
= satStep
; sat
<= 100; sat
+= satStep
) {
757 ArrayList
<ColorIdx
> lumList
= new ArrayList
<ColorIdx
>();
758 satList
.add(lumList
);
760 // Luminance brackets the pure color, but leaning toward
762 for (int lum
= lumBegin
; lum
< 100; lum
+= lumStep
) {
764 System.err.printf("<font style = \"color:");
765 System.err.printf("hsl(%d, %d%%, %d%%)",
767 System.err.printf(";\">=</font>\n");
769 int rgbColor
= hslToRgb(hue
, sat
, lum
);
770 rgbColors
.add(rgbColor
);
771 ColorIdx colorIdx
= new ColorIdx(rgbColor
,
772 rgbColors
.size() - 1);
773 lumList
.add(colorIdx
);
775 rawRgbList
.add(rgbColor
);
777 int red
= (rgbColor
>>> 16) & 0xFF;
778 int green
= (rgbColor
>>> 8) & 0xFF;
779 int blue
= rgbColor
& 0xFF;
780 int [] backToHsl
= new int[3];
781 rgbToHsl(red
, green
, blue
, backToHsl
);
782 System
.err
.printf("%d [%d] %d [%d] %d [%d]\n",
783 hue
, backToHsl
[0], sat
, backToHsl
[1],
789 // System.err.printf("\n</body></html>\n");
791 assert (rgbColors
.size() == MAX_COLOR_REGISTERS
);
794 * We need to sort rgbColors, so that toSixel() can know where
795 * BLACK and WHITE are in it. But we also need to be able to
796 * find the sorted values using the old unsorted indexes. So we
797 * will sort it, put all the indexes into a HashMap, and then
798 * build rgbSortedIndex[].
800 Collections
.sort(rgbColors
);
801 HashMap
<Integer
, Integer
> rgbColorIndices
= null;
802 rgbColorIndices
= new HashMap
<Integer
, Integer
>();
803 for (int i
= 0; i
< MAX_COLOR_REGISTERS
; i
++) {
804 rgbColorIndices
.put(rgbColors
.get(i
), i
);
806 for (int i
= 0; i
< MAX_COLOR_REGISTERS
; i
++) {
807 int rawColor
= rawRgbList
.get(i
);
808 rgbSortedIndex
[i
] = rgbColorIndices
.get(rawColor
);
811 for (int i
= 0; i
< MAX_COLOR_REGISTERS
; i
++) {
812 assert (rawRgbList
!= null);
813 int idx
= rgbSortedIndex
[i
];
814 int rgbColor
= rgbColors
.get(idx
);
815 if ((idx
!= 0) && (idx
!= MAX_COLOR_REGISTERS
- 1)) {
817 System.err.printf("%d %06x --> %d %06x\n",
818 i, rawRgbList.get(i), idx, rgbColors.get(idx));
820 assert (rgbColor
== rawRgbList
.get(i
));
825 // Set the dimmest color as true black, and the brightest as true
828 rgbColors
.set(MAX_COLOR_REGISTERS
- 1, 0xFFFFFF);
831 System.err.printf("<html><body>\n");
832 for (Integer rgb: rgbColors) {
833 System.err.printf("<font style = \"color:");
834 System.err.printf("#%06x", rgb);
835 System.err.printf(";\">=</font>\n");
837 System.err.printf("\n</body></html>\n");
843 * Emit the sixel palette.
845 * @param sb the StringBuilder to append to
846 * @param used array of booleans set to true for each color actually
847 * used in this cell, or null to emit the entire palette
848 * @return the string to emit to an ANSI / ECMA-style terminal
850 public String
emitPalette(final StringBuilder sb
,
851 final boolean [] used
) {
853 for (int i
= 0; i
< MAX_COLOR_REGISTERS
; i
++) {
854 if (((used
!= null) && (used
[i
] == true)) || (used
== null)) {
855 int rgbColor
= rgbColors
.get(i
);
856 sb
.append(String
.format("#%d;2;%d;%d;%d", i
,
857 ((rgbColor
>>> 16) & 0xFF) * 100 / 255,
858 ((rgbColor
>>> 8) & 0xFF) * 100 / 255,
859 ( rgbColor
& 0xFF) * 100 / 255));
862 return sb
.toString();
867 * SixelCache is a least-recently-used cache that hangs on to the
868 * post-rendered sixel string for a particular set of cells.
870 private class SixelCache
{
873 * Maximum size of the cache.
875 private int maxSize
= 100;
878 * The entries stored in the cache.
880 private HashMap
<String
, CacheEntry
> cache
= null;
883 * CacheEntry is one entry in the cache.
885 private class CacheEntry
{
897 * The last time this entry was used.
899 public long millis
= 0;
902 * Public constructor.
904 * @param key the cache entry key
905 * @param data the cache entry data
907 public CacheEntry(final String key
, final String data
) {
910 this.millis
= System
.currentTimeMillis();
915 * Public constructor.
917 * @param maxSize the maximum size of the cache
919 public SixelCache(final int maxSize
) {
920 this.maxSize
= maxSize
;
921 cache
= new HashMap
<String
, CacheEntry
>();
925 * Make a unique key for a list of cells.
927 * @param cells the cells
930 private String
makeKey(final ArrayList
<Cell
> cells
) {
931 StringBuilder sb
= new StringBuilder();
932 for (Cell cell
: cells
) {
933 sb
.append(cell
.hashCode());
935 return sb
.toString();
939 * Get an entry from the cache.
941 * @param cells the list of cells that are the cache key
942 * @return the sixel string representing these cells, or null if this
943 * list of cells is not in the cache
945 public String
get(final ArrayList
<Cell
> cells
) {
946 CacheEntry entry
= cache
.get(makeKey(cells
));
950 entry
.millis
= System
.currentTimeMillis();
955 * Put an entry into the cache.
957 * @param cells the list of cells that are the cache key
958 * @param data the sixel string representing these cells
960 public void put(final ArrayList
<Cell
> cells
, final String data
) {
961 String key
= makeKey(cells
);
963 // System.err.println("put() " + key + " size " + cache.size());
965 assert (!cache
.containsKey(key
));
967 assert (cache
.size() <= maxSize
);
968 if (cache
.size() == maxSize
) {
969 // Cache is at limit, evict oldest entry.
970 long oldestTime
= Long
.MAX_VALUE
;
971 String keyToRemove
= null;
972 for (CacheEntry entry
: cache
.values()) {
973 if ((entry
.millis
< oldestTime
) || (keyToRemove
== null)) {
974 keyToRemove
= entry
.key
;
975 oldestTime
= entry
.millis
;
979 System.err.println("put() remove key = " + keyToRemove +
980 " size " + cache.size());
982 assert (keyToRemove
!= null);
983 cache
.remove(keyToRemove
);
985 System.err.println("put() removed, size " + cache.size());
988 assert (cache
.size() <= maxSize
);
989 CacheEntry entry
= new CacheEntry(key
, data
);
990 assert (key
.equals(entry
.key
));
991 cache
.put(key
, entry
);
993 System.err.println("put() added key " + key + " " +
994 " size " + cache.size());
1000 // ------------------------------------------------------------------------
1001 // Constructors -----------------------------------------------------------
1002 // ------------------------------------------------------------------------
1005 * Constructor sets up state for getEvent(). If either windowWidth or
1006 * windowHeight are less than 1, the terminal is not resized.
1008 * @param listener the object this backend needs to wake up when new
1010 * @param input an InputStream connected to the remote user, or null for
1011 * System.in. If System.in is used, then on non-Windows systems it will
1012 * be put in raw mode; shutdown() will (blindly!) put System.in in cooked
1013 * mode. input is always converted to a Reader with UTF-8 encoding.
1014 * @param output an OutputStream connected to the remote user, or null
1015 * for System.out. output is always converted to a Writer with UTF-8
1017 * @param windowWidth the number of text columns to start with
1018 * @param windowHeight the number of text rows to start with
1019 * @throws UnsupportedEncodingException if an exception is thrown when
1020 * creating the InputStreamReader
1022 public ECMA48Terminal(final Object listener
, final InputStream input
,
1023 final OutputStream output
, final int windowWidth
,
1024 final int windowHeight
) throws UnsupportedEncodingException
{
1026 this(listener
, input
, output
);
1028 // Send dtterm/xterm sequences, which will probably not work because
1029 // allowWindowOps is defaulted to false.
1030 if ((windowWidth
> 0) && (windowHeight
> 0)) {
1031 String resizeString
= String
.format("\033[8;%d;%dt", windowHeight
,
1033 this.output
.write(resizeString
);
1034 this.output
.flush();
1039 * Constructor sets up state for getEvent().
1041 * @param listener the object this backend needs to wake up when new
1043 * @param input an InputStream connected to the remote user, or null for
1044 * System.in. If System.in is used, then on non-Windows systems it will
1045 * be put in raw mode; shutdown() will (blindly!) put System.in in cooked
1046 * mode. input is always converted to a Reader with UTF-8 encoding.
1047 * @param output an OutputStream connected to the remote user, or null
1048 * for System.out. output is always converted to a Writer with UTF-8
1050 * @throws UnsupportedEncodingException if an exception is thrown when
1051 * creating the InputStreamReader
1053 public ECMA48Terminal(final Object listener
, final InputStream input
,
1054 final OutputStream output
) throws UnsupportedEncodingException
{
1060 stopReaderThread
= false;
1061 this.listener
= listener
;
1063 if (input
== null) {
1064 // inputStream = System.in;
1065 inputStream
= new FileInputStream(FileDescriptor
.in
);
1069 inputStream
= input
;
1071 this.input
= new InputStreamReader(inputStream
, "UTF-8");
1073 if (input
instanceof SessionInfo
) {
1074 // This is a TelnetInputStream that exposes window size and
1075 // environment variables from the telnet layer.
1076 sessionInfo
= (SessionInfo
) input
;
1078 if (sessionInfo
== null) {
1079 if (input
== null) {
1080 // Reading right off the tty
1081 sessionInfo
= new TTYSessionInfo();
1083 sessionInfo
= new TSessionInfo();
1087 if (output
== null) {
1088 this.output
= new PrintWriter(new OutputStreamWriter(System
.out
,
1091 this.output
= new PrintWriter(new OutputStreamWriter(output
,
1095 // Request xterm report window dimensions in pixels
1096 this.output
.printf("%s", xtermReportWindowPixelDimensions());
1098 // Enable mouse reporting and metaSendsEscape
1099 this.output
.printf("%s%s", mouse(true), xtermMetaSendsEscape(true));
1100 this.output
.flush();
1102 // Query the screen size
1103 sessionInfo
.queryWindowSize();
1104 setDimensions(sessionInfo
.getWindowWidth(),
1105 sessionInfo
.getWindowHeight());
1107 // Hang onto the window size
1108 windowResize
= new TResizeEvent(TResizeEvent
.Type
.SCREEN
,
1109 sessionInfo
.getWindowWidth(), sessionInfo
.getWindowHeight());
1113 // Spin up the input reader
1114 eventQueue
= new ArrayList
<TInputEvent
>();
1115 readerThread
= new Thread(this);
1116 readerThread
.start();
1119 this.output
.write(clearAll());
1120 this.output
.flush();
1124 * Constructor sets up state for getEvent().
1126 * @param listener the object this backend needs to wake up when new
1128 * @param input the InputStream underlying 'reader'. Its available()
1129 * method is used to determine if reader.read() will block or not.
1130 * @param reader a Reader connected to the remote user.
1131 * @param writer a PrintWriter connected to the remote user.
1132 * @param setRawMode if true, set System.in into raw mode with stty.
1133 * This should in general not be used. It is here solely for Demo3,
1134 * which uses System.in.
1135 * @throws IllegalArgumentException if input, reader, or writer are null.
1137 public ECMA48Terminal(final Object listener
, final InputStream input
,
1138 final Reader reader
, final PrintWriter writer
,
1139 final boolean setRawMode
) {
1141 if (input
== null) {
1142 throw new IllegalArgumentException("InputStream must be specified");
1144 if (reader
== null) {
1145 throw new IllegalArgumentException("Reader must be specified");
1147 if (writer
== null) {
1148 throw new IllegalArgumentException("Writer must be specified");
1154 stopReaderThread
= false;
1155 this.listener
= listener
;
1157 inputStream
= input
;
1158 this.input
= reader
;
1160 if (setRawMode
== true) {
1163 this.setRawMode
= setRawMode
;
1165 if (input
instanceof SessionInfo
) {
1166 // This is a TelnetInputStream that exposes window size and
1167 // environment variables from the telnet layer.
1168 sessionInfo
= (SessionInfo
) input
;
1170 if (sessionInfo
== null) {
1171 if (setRawMode
== true) {
1172 // Reading right off the tty
1173 sessionInfo
= new TTYSessionInfo();
1175 sessionInfo
= new TSessionInfo();
1179 this.output
= writer
;
1181 // Request xterm report window dimensions in pixels
1182 this.output
.printf("%s", xtermReportWindowPixelDimensions());
1184 // Enable mouse reporting and metaSendsEscape
1185 this.output
.printf("%s%s", mouse(true), xtermMetaSendsEscape(true));
1186 this.output
.flush();
1188 // Query the screen size
1189 sessionInfo
.queryWindowSize();
1190 setDimensions(sessionInfo
.getWindowWidth(),
1191 sessionInfo
.getWindowHeight());
1193 // Hang onto the window size
1194 windowResize
= new TResizeEvent(TResizeEvent
.Type
.SCREEN
,
1195 sessionInfo
.getWindowWidth(), sessionInfo
.getWindowHeight());
1199 // Spin up the input reader
1200 eventQueue
= new ArrayList
<TInputEvent
>();
1201 readerThread
= new Thread(this);
1202 readerThread
.start();
1205 this.output
.write(clearAll());
1206 this.output
.flush();
1210 * Constructor sets up state for getEvent().
1212 * @param listener the object this backend needs to wake up when new
1214 * @param input the InputStream underlying 'reader'. Its available()
1215 * method is used to determine if reader.read() will block or not.
1216 * @param reader a Reader connected to the remote user.
1217 * @param writer a PrintWriter connected to the remote user.
1218 * @throws IllegalArgumentException if input, reader, or writer are null.
1220 public ECMA48Terminal(final Object listener
, final InputStream input
,
1221 final Reader reader
, final PrintWriter writer
) {
1223 this(listener
, input
, reader
, writer
, false);
1226 // ------------------------------------------------------------------------
1227 // LogicalScreen ----------------------------------------------------------
1228 // ------------------------------------------------------------------------
1231 * Set the window title.
1233 * @param title the new title
1236 public void setTitle(final String title
) {
1237 output
.write(getSetTitleString(title
));
1242 * Push the logical screen to the physical device.
1245 public void flushPhysical() {
1246 StringBuilder sb
= new StringBuilder();
1250 && (cursorY
<= height
- 1)
1251 && (cursorX
<= width
- 1)
1254 sb
.append(cursor(true));
1255 sb
.append(gotoXY(cursorX
, cursorY
));
1257 sb
.append(cursor(false));
1260 output
.write(sb
.toString());
1265 * Resize the physical screen to match the logical screen dimensions.
1268 public void resizeToScreen() {
1269 // Send dtterm/xterm sequences, which will probably not work because
1270 // allowWindowOps is defaulted to false.
1271 String resizeString
= String
.format("\033[8;%d;%dt", getHeight(),
1273 this.output
.write(resizeString
);
1274 this.output
.flush();
1277 // ------------------------------------------------------------------------
1278 // TerminalReader ---------------------------------------------------------
1279 // ------------------------------------------------------------------------
1282 * Check if there are events in the queue.
1284 * @return if true, getEvents() has something to return to the backend
1286 public boolean hasEvents() {
1287 synchronized (eventQueue
) {
1288 return (eventQueue
.size() > 0);
1293 * Return any events in the IO queue.
1295 * @param queue list to append new events to
1297 public void getEvents(final List
<TInputEvent
> queue
) {
1298 synchronized (eventQueue
) {
1299 if (eventQueue
.size() > 0) {
1300 synchronized (queue
) {
1301 queue
.addAll(eventQueue
);
1309 * Restore terminal to normal state.
1311 public void closeTerminal() {
1313 // System.err.println("=== shutdown() ==="); System.err.flush();
1315 // Tell the reader thread to stop looking at input
1316 stopReaderThread
= true;
1318 readerThread
.join();
1319 } catch (InterruptedException e
) {
1320 if (debugToStderr
) {
1321 e
.printStackTrace();
1325 // Disable mouse reporting and show cursor. Defensive null check
1326 // here in case closeTerminal() is called twice.
1327 if (output
!= null) {
1328 output
.printf("%s%s%s", mouse(false), cursor(true), normal());
1335 // We don't close System.in/out
1337 // Shut down the streams, this should wake up the reader thread
1338 // and make it exit.
1339 if (input
!= null) {
1342 } catch (IOException e
) {
1347 if (output
!= null) {
1355 * Set listener to a different Object.
1357 * @param listener the new listening object that run() wakes up on new
1360 public void setListener(final Object listener
) {
1361 this.listener
= listener
;
1365 * Reload options from System properties.
1367 public void reloadOptions() {
1368 // Permit RGB colors only if externally requested.
1369 if (System
.getProperty("jexer.ECMA48.rgbColor",
1370 "false").equals("true")
1377 // Pull the system properties for sixel output.
1378 if (System
.getProperty("jexer.ECMA48.sixel", "true").equals("true")) {
1385 // ------------------------------------------------------------------------
1386 // Runnable ---------------------------------------------------------------
1387 // ------------------------------------------------------------------------
1390 * Read function runs on a separate thread.
1393 boolean done
= false;
1394 // available() will often return > 1, so we need to read in chunks to
1396 char [] readBuffer
= new char[128];
1397 List
<TInputEvent
> events
= new ArrayList
<TInputEvent
>();
1399 while (!done
&& !stopReaderThread
) {
1401 // We assume that if inputStream has bytes available, then
1402 // input won't block on read().
1403 int n
= inputStream
.available();
1406 System.err.printf("inputStream.available(): %d\n", n);
1411 if (readBuffer
.length
< n
) {
1412 // The buffer wasn't big enough, make it huger
1413 readBuffer
= new char[readBuffer
.length
* 2];
1416 // System.err.printf("BEFORE read()\n"); System.err.flush();
1418 int rc
= input
.read(readBuffer
, 0, readBuffer
.length
);
1421 System.err.printf("AFTER read() %d\n", rc);
1429 for (int i
= 0; i
< rc
; i
++) {
1430 int ch
= readBuffer
[i
];
1431 processChar(events
, (char)ch
);
1433 getIdleEvents(events
);
1434 if (events
.size() > 0) {
1435 // Add to the queue for the backend thread to
1436 // be able to obtain.
1437 synchronized (eventQueue
) {
1438 eventQueue
.addAll(events
);
1440 if (listener
!= null) {
1441 synchronized (listener
) {
1442 listener
.notifyAll();
1449 getIdleEvents(events
);
1450 if (events
.size() > 0) {
1451 synchronized (eventQueue
) {
1452 eventQueue
.addAll(events
);
1454 if (listener
!= null) {
1455 synchronized (listener
) {
1456 listener
.notifyAll();
1462 if (output
.checkError()) {
1467 // Wait 20 millis for more data
1470 // System.err.println("end while loop"); System.err.flush();
1471 } catch (InterruptedException e
) {
1473 } catch (IOException e
) {
1474 e
.printStackTrace();
1477 } // while ((done == false) && (stopReaderThread == false))
1479 // Pass an event up to TApplication to tell it this Backend is done.
1480 synchronized (eventQueue
) {
1481 eventQueue
.add(new TCommandEvent(cmBackendDisconnect
));
1483 if (listener
!= null) {
1484 synchronized (listener
) {
1485 listener
.notifyAll();
1489 // System.err.println("*** run() exiting..."); System.err.flush();
1492 // ------------------------------------------------------------------------
1493 // ECMA48Terminal ---------------------------------------------------------
1494 // ------------------------------------------------------------------------
1497 * Get the width of a character cell in pixels.
1499 * @return the width in pixels of a character cell
1501 public int getTextWidth() {
1502 return (widthPixels
/ sessionInfo
.getWindowWidth());
1506 * Get the height of a character cell in pixels.
1508 * @return the height in pixels of a character cell
1510 public int getTextHeight() {
1511 return (heightPixels
/ sessionInfo
.getWindowHeight());
1515 * Getter for sessionInfo.
1517 * @return the SessionInfo
1519 public SessionInfo
getSessionInfo() {
1524 * Get the output writer.
1526 * @return the Writer
1528 public PrintWriter
getOutput() {
1533 * Call 'stty' to set cooked mode.
1535 * <p>Actually executes '/bin/sh -c stty sane cooked < /dev/tty'
1537 private void sttyCooked() {
1542 * Call 'stty' to set raw mode.
1544 * <p>Actually executes '/bin/sh -c stty -ignbrk -brkint -parmrk -istrip
1545 * -inlcr -igncr -icrnl -ixon -opost -echo -echonl -icanon -isig -iexten
1546 * -parenb cs8 min 1 < /dev/tty'
1548 private void sttyRaw() {
1553 * Call 'stty' to set raw or cooked mode.
1555 * @param mode if true, set raw mode, otherwise set cooked mode
1557 private void doStty(final boolean mode
) {
1558 String
[] cmdRaw
= {
1559 "/bin/sh", "-c", "stty -ignbrk -brkint -parmrk -istrip -inlcr -igncr -icrnl -ixon -opost -echo -echonl -icanon -isig -iexten -parenb cs8 min 1 < /dev/tty"
1561 String
[] cmdCooked
= {
1562 "/bin/sh", "-c", "stty sane cooked < /dev/tty"
1567 process
= Runtime
.getRuntime().exec(cmdRaw
);
1569 process
= Runtime
.getRuntime().exec(cmdCooked
);
1571 BufferedReader in
= new BufferedReader(new InputStreamReader(process
.getInputStream(), "UTF-8"));
1572 String line
= in
.readLine();
1573 if ((line
!= null) && (line
.length() > 0)) {
1574 System
.err
.println("WEIRD?! Normal output from stty: " + line
);
1577 BufferedReader err
= new BufferedReader(new InputStreamReader(process
.getErrorStream(), "UTF-8"));
1578 line
= err
.readLine();
1579 if ((line
!= null) && (line
.length() > 0)) {
1580 System
.err
.println("Error output from stty: " + line
);
1585 } catch (InterruptedException e
) {
1586 if (debugToStderr
) {
1587 e
.printStackTrace();
1591 int rc
= process
.exitValue();
1593 System
.err
.println("stty returned error code: " + rc
);
1595 } catch (IOException e
) {
1596 e
.printStackTrace();
1603 public void flush() {
1608 * Perform a somewhat-optimal rendering of a line.
1610 * @param y row coordinate. 0 is the top-most row.
1611 * @param sb StringBuilder to write escape sequences to
1612 * @param lastAttr cell attributes from the last call to flushLine
1614 private void flushLine(final int y
, final StringBuilder sb
,
1615 CellAttributes lastAttr
) {
1619 for (int x
= 0; x
< width
; x
++) {
1620 Cell lCell
= logical
[x
][y
];
1621 if (!lCell
.isBlank()) {
1625 // Push textEnd to first column beyond the text area
1629 // reallyCleared = true;
1631 boolean hasImage
= false;
1633 for (int x
= 0; x
< width
; x
++) {
1634 Cell lCell
= logical
[x
][y
];
1635 Cell pCell
= physical
[x
][y
];
1637 if (!lCell
.equals(pCell
) || reallyCleared
) {
1639 if (debugToStderr
) {
1640 System
.err
.printf("\n--\n");
1641 System
.err
.printf(" Y: %d X: %d\n", y
, x
);
1642 System
.err
.printf(" lCell: %s\n", lCell
);
1643 System
.err
.printf(" pCell: %s\n", pCell
);
1644 System
.err
.printf(" ==== \n");
1647 if (lastAttr
== null) {
1648 lastAttr
= new CellAttributes();
1649 sb
.append(normal());
1653 if ((lastX
!= (x
- 1)) || (lastX
== -1)) {
1654 // Advancing at least one cell, or the first gotoXY
1655 sb
.append(gotoXY(x
, y
));
1658 assert (lastAttr
!= null);
1660 if ((x
== textEnd
) && (textEnd
< width
- 1)) {
1661 assert (lCell
.isBlank());
1663 for (int i
= x
; i
< width
; i
++) {
1664 assert (logical
[i
][y
].isBlank());
1665 // Physical is always updated
1666 physical
[i
][y
].reset();
1669 // Clear remaining line
1670 sb
.append(clearRemainingLine());
1675 // Image cell: bypass the rest of the loop, it is not
1677 if (lCell
.isImage()) {
1680 // Save the last rendered cell
1683 // Physical is always updated
1684 physical
[x
][y
].setTo(lCell
);
1688 assert (!lCell
.isImage());
1691 sb
.append(gotoXY(x
, y
));
1694 // Now emit only the modified attributes
1695 if ((lCell
.getForeColor() != lastAttr
.getForeColor())
1696 && (lCell
.getBackColor() != lastAttr
.getBackColor())
1698 && (lCell
.isBold() == lastAttr
.isBold())
1699 && (lCell
.isReverse() == lastAttr
.isReverse())
1700 && (lCell
.isUnderline() == lastAttr
.isUnderline())
1701 && (lCell
.isBlink() == lastAttr
.isBlink())
1703 // Both colors changed, attributes the same
1704 sb
.append(color(lCell
.isBold(),
1705 lCell
.getForeColor(), lCell
.getBackColor()));
1707 if (debugToStderr
) {
1708 System
.err
.printf("1 Change only fore/back colors\n");
1711 } else if (lCell
.isRGB()
1712 && (lCell
.getForeColorRGB() != lastAttr
.getForeColorRGB())
1713 && (lCell
.getBackColorRGB() != lastAttr
.getBackColorRGB())
1714 && (lCell
.isBold() == lastAttr
.isBold())
1715 && (lCell
.isReverse() == lastAttr
.isReverse())
1716 && (lCell
.isUnderline() == lastAttr
.isUnderline())
1717 && (lCell
.isBlink() == lastAttr
.isBlink())
1719 // Both colors changed, attributes the same
1720 sb
.append(colorRGB(lCell
.getForeColorRGB(),
1721 lCell
.getBackColorRGB()));
1723 if (debugToStderr
) {
1724 System
.err
.printf("1 Change only fore/back colors (RGB)\n");
1726 } else if ((lCell
.getForeColor() != lastAttr
.getForeColor())
1727 && (lCell
.getBackColor() != lastAttr
.getBackColor())
1729 && (lCell
.isBold() != lastAttr
.isBold())
1730 && (lCell
.isReverse() != lastAttr
.isReverse())
1731 && (lCell
.isUnderline() != lastAttr
.isUnderline())
1732 && (lCell
.isBlink() != lastAttr
.isBlink())
1734 // Everything is different
1735 sb
.append(color(lCell
.getForeColor(),
1736 lCell
.getBackColor(),
1737 lCell
.isBold(), lCell
.isReverse(),
1739 lCell
.isUnderline()));
1741 if (debugToStderr
) {
1742 System
.err
.printf("2 Set all attributes\n");
1744 } else if ((lCell
.getForeColor() != lastAttr
.getForeColor())
1745 && (lCell
.getBackColor() == lastAttr
.getBackColor())
1747 && (lCell
.isBold() == lastAttr
.isBold())
1748 && (lCell
.isReverse() == lastAttr
.isReverse())
1749 && (lCell
.isUnderline() == lastAttr
.isUnderline())
1750 && (lCell
.isBlink() == lastAttr
.isBlink())
1753 // Attributes same, foreColor different
1754 sb
.append(color(lCell
.isBold(),
1755 lCell
.getForeColor(), true));
1757 if (debugToStderr
) {
1758 System
.err
.printf("3 Change foreColor\n");
1760 } else if (lCell
.isRGB()
1761 && (lCell
.getForeColorRGB() != lastAttr
.getForeColorRGB())
1762 && (lCell
.getBackColorRGB() == lastAttr
.getBackColorRGB())
1763 && (lCell
.getForeColorRGB() >= 0)
1764 && (lCell
.getBackColorRGB() >= 0)
1765 && (lCell
.isBold() == lastAttr
.isBold())
1766 && (lCell
.isReverse() == lastAttr
.isReverse())
1767 && (lCell
.isUnderline() == lastAttr
.isUnderline())
1768 && (lCell
.isBlink() == lastAttr
.isBlink())
1770 // Attributes same, foreColor different
1771 sb
.append(colorRGB(lCell
.getForeColorRGB(), true));
1773 if (debugToStderr
) {
1774 System
.err
.printf("3 Change foreColor (RGB)\n");
1776 } else if ((lCell
.getForeColor() == lastAttr
.getForeColor())
1777 && (lCell
.getBackColor() != lastAttr
.getBackColor())
1779 && (lCell
.isBold() == lastAttr
.isBold())
1780 && (lCell
.isReverse() == lastAttr
.isReverse())
1781 && (lCell
.isUnderline() == lastAttr
.isUnderline())
1782 && (lCell
.isBlink() == lastAttr
.isBlink())
1784 // Attributes same, backColor different
1785 sb
.append(color(lCell
.isBold(),
1786 lCell
.getBackColor(), false));
1788 if (debugToStderr
) {
1789 System
.err
.printf("4 Change backColor\n");
1791 } else if (lCell
.isRGB()
1792 && (lCell
.getForeColorRGB() == lastAttr
.getForeColorRGB())
1793 && (lCell
.getBackColorRGB() != lastAttr
.getBackColorRGB())
1794 && (lCell
.isBold() == lastAttr
.isBold())
1795 && (lCell
.isReverse() == lastAttr
.isReverse())
1796 && (lCell
.isUnderline() == lastAttr
.isUnderline())
1797 && (lCell
.isBlink() == lastAttr
.isBlink())
1799 // Attributes same, foreColor different
1800 sb
.append(colorRGB(lCell
.getBackColorRGB(), false));
1802 if (debugToStderr
) {
1803 System
.err
.printf("4 Change backColor (RGB)\n");
1805 } else if ((lCell
.getForeColor() == lastAttr
.getForeColor())
1806 && (lCell
.getBackColor() == lastAttr
.getBackColor())
1807 && (lCell
.getForeColorRGB() == lastAttr
.getForeColorRGB())
1808 && (lCell
.getBackColorRGB() == lastAttr
.getBackColorRGB())
1809 && (lCell
.isBold() == lastAttr
.isBold())
1810 && (lCell
.isReverse() == lastAttr
.isReverse())
1811 && (lCell
.isUnderline() == lastAttr
.isUnderline())
1812 && (lCell
.isBlink() == lastAttr
.isBlink())
1815 // All attributes the same, just print the char
1818 if (debugToStderr
) {
1819 System
.err
.printf("5 Only emit character\n");
1822 // Just reset everything again
1823 if (!lCell
.isRGB()) {
1824 sb
.append(color(lCell
.getForeColor(),
1825 lCell
.getBackColor(),
1829 lCell
.isUnderline()));
1831 if (debugToStderr
) {
1832 System
.err
.printf("6 Change all attributes\n");
1835 sb
.append(colorRGB(lCell
.getForeColorRGB(),
1836 lCell
.getBackColorRGB(),
1840 lCell
.isUnderline()));
1841 if (debugToStderr
) {
1842 System
.err
.printf("6 Change all attributes (RGB)\n");
1847 // Emit the character
1848 sb
.append(lCell
.getChar());
1850 // Save the last rendered cell
1852 lastAttr
.setTo(lCell
);
1854 // Physical is always updated
1855 physical
[x
][y
].setTo(lCell
);
1857 } // if (!lCell.equals(pCell) || (reallyCleared == true))
1859 } // for (int x = 0; x < width; x++)
1863 * Render the screen to a string that can be emitted to something that
1864 * knows how to process ECMA-48/ANSI X3.64 escape sequences.
1866 * @param sb StringBuilder to write escape sequences to
1867 * @return escape sequences string that provides the updates to the
1870 private String
flushString(final StringBuilder sb
) {
1871 CellAttributes attr
= null;
1873 if (reallyCleared
) {
1874 attr
= new CellAttributes();
1875 sb
.append(clearAll());
1879 * For sixel support, draw all of the sixel output first, and then
1880 * draw everything else afterwards. This works OK, but performance
1881 * is still a drag on larger pictures.
1883 for (int y
= 0; y
< height
; y
++) {
1884 for (int x
= 0; x
< width
; x
++) {
1885 // If physical had non-image data that is now image data, the
1886 // entire row must be redrawn.
1887 Cell lCell
= logical
[x
][y
];
1888 Cell pCell
= physical
[x
][y
];
1889 if (lCell
.isImage() && !pCell
.isImage()) {
1895 for (int y
= 0; y
< height
; y
++) {
1896 for (int x
= 0; x
< width
; x
++) {
1897 Cell lCell
= logical
[x
][y
];
1898 Cell pCell
= physical
[x
][y
];
1900 if (!lCell
.isImage()) {
1906 while ((right
< width
)
1907 && (logical
[right
][y
].isImage())
1908 && (!logical
[right
][y
].equals(physical
[right
][y
])
1913 ArrayList
<Cell
> cellsToDraw
= new ArrayList
<Cell
>();
1914 for (int i
= 0; i
< (right
- x
); i
++) {
1915 assert (logical
[x
+ i
][y
].isImage());
1916 cellsToDraw
.add(logical
[x
+ i
][y
]);
1918 // Physical is always updated.
1919 physical
[x
+ i
][y
].setTo(lCell
);
1921 if (cellsToDraw
.size() > 0) {
1922 sb
.append(toSixel(x
, y
, cellsToDraw
));
1929 // Draw the text part now.
1930 for (int y
= 0; y
< height
; y
++) {
1931 flushLine(y
, sb
, attr
);
1934 reallyCleared
= false;
1936 String result
= sb
.toString();
1937 if (debugToStderr
) {
1938 System
.err
.printf("flushString(): %s\n", result
);
1944 * Reset keyboard/mouse input parser.
1946 private void resetParser() {
1947 state
= ParseState
.GROUND
;
1948 params
= new ArrayList
<String
>();
1954 * Produce a control character or one of the special ones (ENTER, TAB,
1957 * @param ch Unicode code point
1958 * @param alt if true, set alt on the TKeypress
1959 * @return one TKeypress event, either a control character (e.g. isKey ==
1960 * false, ch == 'A', ctrl == true), or a special key (e.g. isKey == true,
1963 private TKeypressEvent
controlChar(final char ch
, final boolean alt
) {
1964 // System.err.printf("controlChar: %02x\n", ch);
1968 // Carriage return --> ENTER
1969 return new TKeypressEvent(kbEnter
, alt
, false, false);
1971 // Linefeed --> ENTER
1972 return new TKeypressEvent(kbEnter
, alt
, false, false);
1975 return new TKeypressEvent(kbEsc
, alt
, false, false);
1978 return new TKeypressEvent(kbTab
, alt
, false, false);
1980 // Make all other control characters come back as the alphabetic
1981 // character with the ctrl field set. So SOH would be 'A' +
1983 return new TKeypressEvent(false, 0, (char)(ch
+ 0x40),
1989 * Produce special key from CSI Pn ; Pm ; ... ~
1991 * @return one KEYPRESS event representing a special key
1993 private TInputEvent
csiFnKey() {
1995 if (params
.size() > 0) {
1996 key
= Integer
.parseInt(params
.get(0));
1998 boolean alt
= false;
1999 boolean ctrl
= false;
2000 boolean shift
= false;
2001 if (params
.size() > 1) {
2002 shift
= csiIsShift(params
.get(1));
2003 alt
= csiIsAlt(params
.get(1));
2004 ctrl
= csiIsCtrl(params
.get(1));
2009 return new TKeypressEvent(kbHome
, alt
, ctrl
, shift
);
2011 return new TKeypressEvent(kbIns
, alt
, ctrl
, shift
);
2013 return new TKeypressEvent(kbDel
, alt
, ctrl
, shift
);
2015 return new TKeypressEvent(kbEnd
, alt
, ctrl
, shift
);
2017 return new TKeypressEvent(kbPgUp
, alt
, ctrl
, shift
);
2019 return new TKeypressEvent(kbPgDn
, alt
, ctrl
, shift
);
2021 return new TKeypressEvent(kbF5
, alt
, ctrl
, shift
);
2023 return new TKeypressEvent(kbF6
, alt
, ctrl
, shift
);
2025 return new TKeypressEvent(kbF7
, alt
, ctrl
, shift
);
2027 return new TKeypressEvent(kbF8
, alt
, ctrl
, shift
);
2029 return new TKeypressEvent(kbF9
, alt
, ctrl
, shift
);
2031 return new TKeypressEvent(kbF10
, alt
, ctrl
, shift
);
2033 return new TKeypressEvent(kbF11
, alt
, ctrl
, shift
);
2035 return new TKeypressEvent(kbF12
, alt
, ctrl
, shift
);
2043 * Produce mouse events based on "Any event tracking" and UTF-8
2045 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
2047 * @return a MOUSE_MOTION, MOUSE_UP, or MOUSE_DOWN event
2049 private TInputEvent
parseMouse() {
2050 int buttons
= params
.get(0).charAt(0) - 32;
2051 int x
= params
.get(0).charAt(1) - 32 - 1;
2052 int y
= params
.get(0).charAt(2) - 32 - 1;
2054 // Clamp X and Y to the physical screen coordinates.
2055 if (x
>= windowResize
.getWidth()) {
2056 x
= windowResize
.getWidth() - 1;
2058 if (y
>= windowResize
.getHeight()) {
2059 y
= windowResize
.getHeight() - 1;
2062 TMouseEvent
.Type eventType
= TMouseEvent
.Type
.MOUSE_DOWN
;
2063 boolean eventMouse1
= false;
2064 boolean eventMouse2
= false;
2065 boolean eventMouse3
= false;
2066 boolean eventMouseWheelUp
= false;
2067 boolean eventMouseWheelDown
= false;
2069 // System.err.printf("buttons: %04x\r\n", buttons);
2086 if (!mouse1
&& !mouse2
&& !mouse3
) {
2087 eventType
= TMouseEvent
.Type
.MOUSE_MOTION
;
2089 eventType
= TMouseEvent
.Type
.MOUSE_UP
;
2106 // Dragging with mouse1 down
2109 eventType
= TMouseEvent
.Type
.MOUSE_MOTION
;
2113 // Dragging with mouse2 down
2116 eventType
= TMouseEvent
.Type
.MOUSE_MOTION
;
2120 // Dragging with mouse3 down
2123 eventType
= TMouseEvent
.Type
.MOUSE_MOTION
;
2127 // Dragging with mouse2 down after wheelUp
2130 eventType
= TMouseEvent
.Type
.MOUSE_MOTION
;
2134 // Dragging with mouse2 down after wheelDown
2137 eventType
= TMouseEvent
.Type
.MOUSE_MOTION
;
2141 eventMouseWheelUp
= true;
2145 eventMouseWheelDown
= true;
2149 // Unknown, just make it motion
2150 eventType
= TMouseEvent
.Type
.MOUSE_MOTION
;
2153 return new TMouseEvent(eventType
, x
, y
, x
, y
,
2154 eventMouse1
, eventMouse2
, eventMouse3
,
2155 eventMouseWheelUp
, eventMouseWheelDown
);
2159 * Produce mouse events based on "Any event tracking" and SGR
2161 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
2163 * @param release if true, this was a release ('m')
2164 * @return a MOUSE_MOTION, MOUSE_UP, or MOUSE_DOWN event
2166 private TInputEvent
parseMouseSGR(final boolean release
) {
2167 // SGR extended coordinates - mode 1006
2168 if (params
.size() < 3) {
2169 // Invalid position, bail out.
2172 int buttons
= Integer
.parseInt(params
.get(0));
2173 int x
= Integer
.parseInt(params
.get(1)) - 1;
2174 int y
= Integer
.parseInt(params
.get(2)) - 1;
2176 // Clamp X and Y to the physical screen coordinates.
2177 if (x
>= windowResize
.getWidth()) {
2178 x
= windowResize
.getWidth() - 1;
2180 if (y
>= windowResize
.getHeight()) {
2181 y
= windowResize
.getHeight() - 1;
2184 TMouseEvent
.Type eventType
= TMouseEvent
.Type
.MOUSE_DOWN
;
2185 boolean eventMouse1
= false;
2186 boolean eventMouse2
= false;
2187 boolean eventMouse3
= false;
2188 boolean eventMouseWheelUp
= false;
2189 boolean eventMouseWheelDown
= false;
2192 eventType
= TMouseEvent
.Type
.MOUSE_UP
;
2206 // Motion only, no buttons down
2207 eventType
= TMouseEvent
.Type
.MOUSE_MOTION
;
2211 // Dragging with mouse1 down
2213 eventType
= TMouseEvent
.Type
.MOUSE_MOTION
;
2217 // Dragging with mouse2 down
2219 eventType
= TMouseEvent
.Type
.MOUSE_MOTION
;
2223 // Dragging with mouse3 down
2225 eventType
= TMouseEvent
.Type
.MOUSE_MOTION
;
2229 // Dragging with mouse2 down after wheelUp
2231 eventType
= TMouseEvent
.Type
.MOUSE_MOTION
;
2235 // Dragging with mouse2 down after wheelDown
2237 eventType
= TMouseEvent
.Type
.MOUSE_MOTION
;
2241 eventMouseWheelUp
= true;
2245 eventMouseWheelDown
= true;
2249 // Unknown, bail out
2252 return new TMouseEvent(eventType
, x
, y
, x
, y
,
2253 eventMouse1
, eventMouse2
, eventMouse3
,
2254 eventMouseWheelUp
, eventMouseWheelDown
);
2258 * Return any events in the IO queue due to timeout.
2260 * @param queue list to append new events to
2262 private void getIdleEvents(final List
<TInputEvent
> queue
) {
2263 long nowTime
= System
.currentTimeMillis();
2265 // Check for new window size
2266 long windowSizeDelay
= nowTime
- windowSizeTime
;
2267 if (windowSizeDelay
> 1000) {
2268 int oldTextWidth
= getTextWidth();
2269 int oldTextHeight
= getTextHeight();
2271 sessionInfo
.queryWindowSize();
2272 int newWidth
= sessionInfo
.getWindowWidth();
2273 int newHeight
= sessionInfo
.getWindowHeight();
2275 if ((newWidth
!= windowResize
.getWidth())
2276 || (newHeight
!= windowResize
.getHeight())
2279 // Request xterm report window dimensions in pixels again.
2280 // Between now and then, ensure that the reported text cell
2281 // size is the same by setting widthPixels and heightPixels
2282 // to match the new dimensions.
2283 widthPixels
= oldTextWidth
* newWidth
;
2284 heightPixels
= oldTextHeight
* newHeight
;
2286 if (debugToStderr
) {
2287 System
.err
.println("Screen size changed, old size " +
2289 System
.err
.println(" new size " +
2290 newWidth
+ " x " + newHeight
);
2291 System
.err
.println(" old pixels " +
2292 oldTextWidth
+ " x " + oldTextHeight
);
2293 System
.err
.println(" new pixels " +
2294 getTextWidth() + " x " + getTextHeight());
2297 this.output
.printf("%s", xtermReportWindowPixelDimensions());
2298 this.output
.flush();
2300 TResizeEvent event
= new TResizeEvent(TResizeEvent
.Type
.SCREEN
,
2301 newWidth
, newHeight
);
2302 windowResize
= new TResizeEvent(TResizeEvent
.Type
.SCREEN
,
2303 newWidth
, newHeight
);
2306 windowSizeTime
= nowTime
;
2309 // ESCDELAY type timeout
2310 if (state
== ParseState
.ESCAPE
) {
2311 long escDelay
= nowTime
- escapeTime
;
2312 if (escDelay
> 100) {
2313 // After 0.1 seconds, assume a true escape character
2314 queue
.add(controlChar((char)0x1B, false));
2321 * Returns true if the CSI parameter for a keyboard command means that
2324 private boolean csiIsShift(final String x
) {
2336 * Returns true if the CSI parameter for a keyboard command means that
2339 private boolean csiIsAlt(final String x
) {
2351 * Returns true if the CSI parameter for a keyboard command means that
2354 private boolean csiIsCtrl(final String x
) {
2366 * Parses the next character of input to see if an InputEvent is
2369 * @param events list to append new events to
2370 * @param ch Unicode code point
2372 private void processChar(final List
<TInputEvent
> events
, final char ch
) {
2374 // ESCDELAY type timeout
2375 long nowTime
= System
.currentTimeMillis();
2376 if (state
== ParseState
.ESCAPE
) {
2377 long escDelay
= nowTime
- escapeTime
;
2378 if (escDelay
> 250) {
2379 // After 0.25 seconds, assume a true escape character
2380 events
.add(controlChar((char)0x1B, false));
2386 boolean ctrl
= false;
2387 boolean alt
= false;
2388 boolean shift
= false;
2390 // System.err.printf("state: %s ch %c\r\n", state, ch);
2396 state
= ParseState
.ESCAPE
;
2397 escapeTime
= nowTime
;
2402 // Control character
2403 events
.add(controlChar(ch
, false));
2410 events
.add(new TKeypressEvent(false, 0, ch
,
2411 false, false, false));
2420 // ALT-Control character
2421 events
.add(controlChar(ch
, true));
2427 // This will be one of the function keys
2428 state
= ParseState
.ESCAPE_INTERMEDIATE
;
2432 // '[' goes to CSI_ENTRY
2434 state
= ParseState
.CSI_ENTRY
;
2438 // Everything else is assumed to be Alt-keystroke
2439 if ((ch
>= 'A') && (ch
<= 'Z')) {
2443 events
.add(new TKeypressEvent(false, 0, ch
, alt
, ctrl
, shift
));
2447 case ESCAPE_INTERMEDIATE
:
2448 if ((ch
>= 'P') && (ch
<= 'S')) {
2452 events
.add(new TKeypressEvent(kbF1
));
2455 events
.add(new TKeypressEvent(kbF2
));
2458 events
.add(new TKeypressEvent(kbF3
));
2461 events
.add(new TKeypressEvent(kbF4
));
2470 // Unknown keystroke, ignore
2475 // Numbers - parameter values
2476 if ((ch
>= '0') && (ch
<= '9')) {
2477 params
.set(params
.size() - 1,
2478 params
.get(params
.size() - 1) + ch
);
2479 state
= ParseState
.CSI_PARAM
;
2482 // Parameter separator
2488 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
2492 events
.add(new TKeypressEvent(kbUp
, alt
, ctrl
, shift
));
2497 events
.add(new TKeypressEvent(kbDown
, alt
, ctrl
, shift
));
2502 events
.add(new TKeypressEvent(kbRight
, alt
, ctrl
, shift
));
2507 events
.add(new TKeypressEvent(kbLeft
, alt
, ctrl
, shift
));
2512 events
.add(new TKeypressEvent(kbHome
));
2517 events
.add(new TKeypressEvent(kbEnd
));
2521 // CBT - Cursor backward X tab stops (default 1)
2522 events
.add(new TKeypressEvent(kbBackTab
));
2527 state
= ParseState
.MOUSE
;
2530 // Mouse position, SGR (1006) coordinates
2531 state
= ParseState
.MOUSE_SGR
;
2538 // Unknown keystroke, ignore
2543 // Numbers - parameter values
2544 if ((ch
>= '0') && (ch
<= '9')) {
2545 params
.set(params
.size() - 1,
2546 params
.get(params
.size() - 1) + ch
);
2549 // Parameter separator
2557 // Generate a mouse press event
2558 TInputEvent event
= parseMouseSGR(false);
2559 if (event
!= null) {
2565 // Generate a mouse release event
2566 event
= parseMouseSGR(true);
2567 if (event
!= null) {
2576 // Unknown keystroke, ignore
2581 // Numbers - parameter values
2582 if ((ch
>= '0') && (ch
<= '9')) {
2583 params
.set(params
.size() - 1,
2584 params
.get(params
.size() - 1) + ch
);
2585 state
= ParseState
.CSI_PARAM
;
2588 // Parameter separator
2595 events
.add(csiFnKey());
2600 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
2604 if (params
.size() > 1) {
2605 shift
= csiIsShift(params
.get(1));
2606 alt
= csiIsAlt(params
.get(1));
2607 ctrl
= csiIsCtrl(params
.get(1));
2609 events
.add(new TKeypressEvent(kbUp
, alt
, ctrl
, shift
));
2614 if (params
.size() > 1) {
2615 shift
= csiIsShift(params
.get(1));
2616 alt
= csiIsAlt(params
.get(1));
2617 ctrl
= csiIsCtrl(params
.get(1));
2619 events
.add(new TKeypressEvent(kbDown
, alt
, ctrl
, shift
));
2624 if (params
.size() > 1) {
2625 shift
= csiIsShift(params
.get(1));
2626 alt
= csiIsAlt(params
.get(1));
2627 ctrl
= csiIsCtrl(params
.get(1));
2629 events
.add(new TKeypressEvent(kbRight
, alt
, ctrl
, shift
));
2634 if (params
.size() > 1) {
2635 shift
= csiIsShift(params
.get(1));
2636 alt
= csiIsAlt(params
.get(1));
2637 ctrl
= csiIsCtrl(params
.get(1));
2639 events
.add(new TKeypressEvent(kbLeft
, alt
, ctrl
, shift
));
2644 if (params
.size() > 1) {
2645 shift
= csiIsShift(params
.get(1));
2646 alt
= csiIsAlt(params
.get(1));
2647 ctrl
= csiIsCtrl(params
.get(1));
2649 events
.add(new TKeypressEvent(kbHome
, alt
, ctrl
, shift
));
2654 if (params
.size() > 1) {
2655 shift
= csiIsShift(params
.get(1));
2656 alt
= csiIsAlt(params
.get(1));
2657 ctrl
= csiIsCtrl(params
.get(1));
2659 events
.add(new TKeypressEvent(kbEnd
, alt
, ctrl
, shift
));
2664 if ((params
.size() > 2) && (params
.get(0).equals("4"))) {
2665 if (debugToStderr
) {
2666 System
.err
.printf("windowOp pixels: " +
2667 "height %s width %s\n",
2668 params
.get(1), params
.get(2));
2671 widthPixels
= Integer
.parseInt(params
.get(2));
2672 heightPixels
= Integer
.parseInt(params
.get(1));
2673 } catch (NumberFormatException e
) {
2674 if (debugToStderr
) {
2675 e
.printStackTrace();
2678 if (widthPixels
<= 0) {
2681 if (heightPixels
<= 0) {
2692 // Unknown keystroke, ignore
2697 params
.set(0, params
.get(params
.size() - 1) + ch
);
2698 if (params
.get(0).length() == 3) {
2699 // We have enough to generate a mouse event
2700 events
.add(parseMouse());
2709 // This "should" be impossible to reach
2714 * Request (u)xterm to report the current window size dimensions.
2716 * @return the string to emit to xterm
2718 private String
xtermReportWindowPixelDimensions() {
2723 * Tell (u)xterm that we want alt- keystrokes to send escape + character
2724 * rather than set the 8th bit. Anyone who wants UTF8 should want this
2727 * @param on if true, enable metaSendsEscape
2728 * @return the string to emit to xterm
2730 private String
xtermMetaSendsEscape(final boolean on
) {
2732 return "\033[?1036h\033[?1034l";
2734 return "\033[?1036l";
2738 * Create an xterm OSC sequence to change the window title.
2740 * @param title the new title
2741 * @return the string to emit to xterm
2743 private String
getSetTitleString(final String title
) {
2744 return "\033]2;" + title
+ "\007";
2747 // ------------------------------------------------------------------------
2748 // Sixel output support ---------------------------------------------------
2749 // ------------------------------------------------------------------------
2752 * Start a sixel string for display one row's worth of bitmap data.
2754 * @param x column coordinate. 0 is the left-most column.
2755 * @param y row coordinate. 0 is the top-most row.
2756 * @return the string to emit to an ANSI / ECMA-style terminal
2758 private String
startSixel(final int x
, final int y
) {
2759 StringBuilder sb
= new StringBuilder();
2761 assert (sixel
== true);
2764 sb
.append(gotoXY(x
, y
));
2767 sb
.append("\033Pq");
2769 if (palette
== null) {
2770 palette
= new SixelPalette();
2773 return sb
.toString();
2777 * End a sixel string for display one row's worth of bitmap data.
2779 * @return the string to emit to an ANSI / ECMA-style terminal
2781 private String
endSixel() {
2782 assert (sixel
== true);
2789 * Create a sixel string representing a row of several cells containing
2792 * @param x column coordinate. 0 is the left-most column.
2793 * @param y row coordinate. 0 is the top-most row.
2794 * @param cells the cells containing the bitmap data
2795 * @return the string to emit to an ANSI / ECMA-style terminal
2797 private String
toSixel(final int x
, final int y
,
2798 final ArrayList
<Cell
> cells
) {
2800 StringBuilder sb
= new StringBuilder();
2802 assert (cells
!= null);
2803 assert (cells
.size() > 0);
2804 assert (cells
.get(0).getImage() != null);
2806 if (sixel
== false) {
2807 sb
.append(normal());
2808 sb
.append(gotoXY(x
, y
));
2809 for (int i
= 0; i
< cells
.size(); i
++) {
2812 return sb
.toString();
2815 if (sixelCache
== null) {
2816 sixelCache
= new SixelCache(height
* 10);
2819 // Save and get rows to/from the cache that do NOT have inverted
2821 boolean saveInCache
= true;
2822 for (Cell cell
: cells
) {
2823 if (cell
.isInvertedImage()) {
2824 saveInCache
= false;
2828 String cachedResult
= sixelCache
.get(cells
);
2829 if (cachedResult
!= null) {
2830 // System.err.println("CACHE HIT");
2831 sb
.append(startSixel(x
, y
));
2832 sb
.append(cachedResult
);
2833 sb
.append(endSixel());
2834 return sb
.toString();
2836 // System.err.println("CACHE MISS");
2839 int imageWidth
= cells
.get(0).getImage().getWidth();
2840 int imageHeight
= cells
.get(0).getImage().getHeight();
2842 // cells.get(x).getImage() has a dithered bitmap containing indexes
2843 // into the color palette. Piece these together into one larger
2844 // image for final rendering.
2846 int fullWidth
= cells
.size() * getTextWidth();
2847 int fullHeight
= getTextHeight();
2848 for (int i
= 0; i
< cells
.size(); i
++) {
2849 totalWidth
+= cells
.get(i
).getImage().getWidth();
2852 BufferedImage image
= new BufferedImage(fullWidth
,
2853 fullHeight
, BufferedImage
.TYPE_INT_ARGB
);
2856 for (int i
= 0; i
< cells
.size() - 1; i
++) {
2857 int tileWidth
= Math
.min(cells
.get(i
).getImage().getWidth(),
2859 int tileHeight
= Math
.min(cells
.get(i
).getImage().getHeight(),
2861 if (false && cells
.get(i
).isInvertedImage()) {
2862 // I used to put an all-white cell over the cursor, don't do
2864 rgbArray
= new int[imageWidth
* imageHeight
];
2865 for (int j
= 0; j
< rgbArray
.length
; j
++) {
2866 rgbArray
[j
] = 0xFFFFFF;
2870 rgbArray
= cells
.get(i
).getImage().getRGB(0, 0,
2871 tileWidth
, tileHeight
, null, 0, tileWidth
);
2872 } catch (Exception e
) {
2873 throw new RuntimeException("image " + imageWidth
+ "x" +
2875 "tile " + tileWidth
+ "x" +
2877 " cells.get(i).getImage() " +
2878 cells
.get(i
).getImage() +
2880 " fullWidth " + fullWidth
+
2881 " fullHeight " + fullHeight
, e
);
2886 System.err.printf("calling image.setRGB(): %d %d %d %d %d\n",
2887 i * imageWidth, 0, imageWidth, imageHeight,
2889 System.err.printf(" fullWidth %d fullHeight %d cells.size() %d textWidth %d\n",
2890 fullWidth, fullHeight, cells.size(), getTextWidth());
2893 image
.setRGB(i
* imageWidth
, 0, tileWidth
, tileHeight
,
2894 rgbArray
, 0, tileWidth
);
2895 if (tileHeight
< fullHeight
) {
2896 int backgroundColor
= cells
.get(i
).getBackground().getRGB();
2897 for (int imageX
= 0; imageX
< image
.getWidth(); imageX
++) {
2898 for (int imageY
= imageHeight
; imageY
< fullHeight
;
2901 image
.setRGB(imageX
, imageY
, backgroundColor
);
2906 totalWidth
-= ((cells
.size() - 1) * imageWidth
);
2907 if (false && cells
.get(cells
.size() - 1).isInvertedImage()) {
2908 // I used to put an all-white cell over the cursor, don't do that
2910 rgbArray
= new int[totalWidth
* imageHeight
];
2911 for (int j
= 0; j
< rgbArray
.length
; j
++) {
2912 rgbArray
[j
] = 0xFFFFFF;
2916 rgbArray
= cells
.get(cells
.size() - 1).getImage().getRGB(0, 0,
2917 totalWidth
, imageHeight
, null, 0, totalWidth
);
2918 } catch (Exception e
) {
2919 throw new RuntimeException("image " + imageWidth
+ "x" +
2920 imageHeight
+ " cells.get(cells.size() - 1).getImage() " +
2921 cells
.get(cells
.size() - 1).getImage(), e
);
2924 image
.setRGB((cells
.size() - 1) * imageWidth
, 0, totalWidth
,
2925 imageHeight
, rgbArray
, 0, totalWidth
);
2927 if (totalWidth
< getTextWidth()) {
2928 int backgroundColor
= cells
.get(cells
.size() - 1).getBackground().getRGB();
2930 for (int imageX
= image
.getWidth() - totalWidth
;
2931 imageX
< image
.getWidth(); imageX
++) {
2933 for (int imageY
= 0; imageY
< fullHeight
; imageY
++) {
2934 image
.setRGB(imageX
, imageY
, backgroundColor
);
2939 // Dither the image. It is ok to lose the original here.
2940 if (palette
== null) {
2941 palette
= new SixelPalette();
2943 image
= palette
.ditherImage(image
);
2945 // Emit the palette, but only for the colors actually used by these
2947 boolean [] usedColors
= new boolean[MAX_COLOR_REGISTERS
];
2948 for (int imageX
= 0; imageX
< image
.getWidth(); imageX
++) {
2949 for (int imageY
= 0; imageY
< image
.getHeight(); imageY
++) {
2950 usedColors
[image
.getRGB(imageX
, imageY
)] = true;
2953 palette
.emitPalette(sb
, usedColors
);
2955 // Render the entire row of cells.
2956 for (int currentRow
= 0; currentRow
< fullHeight
; currentRow
+= 6) {
2957 int [][] sixels
= new int[image
.getWidth()][6];
2959 // See which colors are actually used in this band of sixels.
2960 for (int imageX
= 0; imageX
< image
.getWidth(); imageX
++) {
2961 for (int imageY
= 0;
2962 (imageY
< 6) && (imageY
+ currentRow
< fullHeight
);
2965 int colorIdx
= image
.getRGB(imageX
, imageY
+ currentRow
);
2966 assert (colorIdx
>= 0);
2967 assert (colorIdx
< MAX_COLOR_REGISTERS
);
2969 sixels
[imageX
][imageY
] = colorIdx
;
2973 for (int i
= 0; i
< MAX_COLOR_REGISTERS
; i
++) {
2974 boolean isUsed
= false;
2975 for (int imageX
= 0; imageX
< image
.getWidth(); imageX
++) {
2976 for (int j
= 0; j
< 6; j
++) {
2977 if (sixels
[imageX
][j
] == i
) {
2982 if (isUsed
== false) {
2986 // Set to the beginning of scan line for the next set of
2987 // colored pixels, and select the color.
2988 sb
.append(String
.format("$#%d", i
));
2991 int oldDataCount
= 0;
2992 for (int imageX
= 0; imageX
< image
.getWidth(); imageX
++) {
2994 // Add up all the pixels that match this color.
2997 (j
< 6) && (currentRow
+ j
< fullHeight
);
3000 if (sixels
[imageX
][j
] == i
) {
3027 if (data
== oldData
) {
3030 if (oldDataCount
== 1) {
3031 sb
.append((char) oldData
);
3032 } else if (oldDataCount
> 1) {
3033 sb
.append(String
.format("!%d", oldDataCount
));
3034 sb
.append((char) oldData
);
3040 } // for (int imageX = 0; imageX < image.getWidth(); imageX++)
3042 // Emit the last sequence.
3043 if (oldDataCount
== 1) {
3044 sb
.append((char) oldData
);
3045 } else if (oldDataCount
> 1) {
3046 sb
.append(String
.format("!%d", oldDataCount
));
3047 sb
.append((char) oldData
);
3050 } // for (int i = 0; i < MAX_COLOR_REGISTERS; i++)
3052 // Advance to the next scan line.
3055 } // for (int currentRow = 0; currentRow < imageHeight; currentRow += 6)
3057 // Kill the very last "-", because it is unnecessary.
3058 sb
.deleteCharAt(sb
.length() - 1);
3061 // This row is OK to save into the cache.
3062 sixelCache
.put(cells
, sb
.toString());
3065 return (startSixel(x
, y
) + sb
.toString() + endSixel());
3069 * Get the sixel support flag.
3071 * @return true if this terminal is emitting sixel
3073 public boolean hasSixel() {
3077 // ------------------------------------------------------------------------
3078 // End sixel output support -----------------------------------------------
3079 // ------------------------------------------------------------------------
3082 * Create a SGR parameter sequence for a single color change.
3084 * @param bold if true, set bold
3085 * @param color one of the Color.WHITE, Color.BLUE, etc. constants
3086 * @param foreground if true, this is a foreground color
3087 * @return the string to emit to an ANSI / ECMA-style terminal,
3090 private String
color(final boolean bold
, final Color color
,
3091 final boolean foreground
) {
3092 return color(color
, foreground
, true) +
3093 rgbColor(bold
, color
, foreground
);
3097 * Create a T.416 RGB parameter sequence for a single color change.
3099 * @param colorRGB a 24-bit RGB value for foreground color
3100 * @param foreground if true, this is a foreground color
3101 * @return the string to emit to an ANSI / ECMA-style terminal,
3104 private String
colorRGB(final int colorRGB
, final boolean foreground
) {
3106 int colorRed
= (colorRGB
>>> 16) & 0xFF;
3107 int colorGreen
= (colorRGB
>>> 8) & 0xFF;
3108 int colorBlue
= colorRGB
& 0xFF;
3110 StringBuilder sb
= new StringBuilder();
3112 sb
.append("\033[38;2;");
3114 sb
.append("\033[48;2;");
3116 sb
.append(String
.format("%d;%d;%dm", colorRed
, colorGreen
, colorBlue
));
3117 return sb
.toString();
3121 * Create a T.416 RGB parameter sequence for both foreground and
3122 * background color change.
3124 * @param foreColorRGB a 24-bit RGB value for foreground color
3125 * @param backColorRGB a 24-bit RGB value for foreground color
3126 * @return the string to emit to an ANSI / ECMA-style terminal,
3129 private String
colorRGB(final int foreColorRGB
, final int backColorRGB
) {
3130 int foreColorRed
= (foreColorRGB
>>> 16) & 0xFF;
3131 int foreColorGreen
= (foreColorRGB
>>> 8) & 0xFF;
3132 int foreColorBlue
= foreColorRGB
& 0xFF;
3133 int backColorRed
= (backColorRGB
>>> 16) & 0xFF;
3134 int backColorGreen
= (backColorRGB
>>> 8) & 0xFF;
3135 int backColorBlue
= backColorRGB
& 0xFF;
3137 StringBuilder sb
= new StringBuilder();
3138 sb
.append(String
.format("\033[38;2;%d;%d;%dm",
3139 foreColorRed
, foreColorGreen
, foreColorBlue
));
3140 sb
.append(String
.format("\033[48;2;%d;%d;%dm",
3141 backColorRed
, backColorGreen
, backColorBlue
));
3142 return sb
.toString();
3146 * Create a T.416 RGB parameter sequence for a single color change.
3148 * @param bold if true, set bold
3149 * @param color one of the Color.WHITE, Color.BLUE, etc. constants
3150 * @param foreground if true, this is a foreground color
3151 * @return the string to emit to an xterm terminal with RGB support,
3152 * e.g. "\033[38;2;RR;GG;BBm"
3154 private String
rgbColor(final boolean bold
, final Color color
,
3155 final boolean foreground
) {
3156 if (doRgbColor
== false) {
3159 StringBuilder sb
= new StringBuilder("\033[");
3161 // Bold implies foreground only
3163 if (color
.equals(Color
.BLACK
)) {
3164 sb
.append("84;84;84");
3165 } else if (color
.equals(Color
.RED
)) {
3166 sb
.append("252;84;84");
3167 } else if (color
.equals(Color
.GREEN
)) {
3168 sb
.append("84;252;84");
3169 } else if (color
.equals(Color
.YELLOW
)) {
3170 sb
.append("252;252;84");
3171 } else if (color
.equals(Color
.BLUE
)) {
3172 sb
.append("84;84;252");
3173 } else if (color
.equals(Color
.MAGENTA
)) {
3174 sb
.append("252;84;252");
3175 } else if (color
.equals(Color
.CYAN
)) {
3176 sb
.append("84;252;252");
3177 } else if (color
.equals(Color
.WHITE
)) {
3178 sb
.append("252;252;252");
3186 if (color
.equals(Color
.BLACK
)) {
3188 } else if (color
.equals(Color
.RED
)) {
3189 sb
.append("168;0;0");
3190 } else if (color
.equals(Color
.GREEN
)) {
3191 sb
.append("0;168;0");
3192 } else if (color
.equals(Color
.YELLOW
)) {
3193 sb
.append("168;84;0");
3194 } else if (color
.equals(Color
.BLUE
)) {
3195 sb
.append("0;0;168");
3196 } else if (color
.equals(Color
.MAGENTA
)) {
3197 sb
.append("168;0;168");
3198 } else if (color
.equals(Color
.CYAN
)) {
3199 sb
.append("0;168;168");
3200 } else if (color
.equals(Color
.WHITE
)) {
3201 sb
.append("168;168;168");
3205 return sb
.toString();
3209 * Create a T.416 RGB parameter sequence for both foreground and
3210 * background color change.
3212 * @param bold if true, set bold
3213 * @param foreColor one of the Color.WHITE, Color.BLUE, etc. constants
3214 * @param backColor one of the Color.WHITE, Color.BLUE, etc. constants
3215 * @return the string to emit to an xterm terminal with RGB support,
3216 * e.g. "\033[38;2;RR;GG;BB;48;2;RR;GG;BBm"
3218 private String
rgbColor(final boolean bold
, final Color foreColor
,
3219 final Color backColor
) {
3220 if (doRgbColor
== false) {
3224 return rgbColor(bold
, foreColor
, true) +
3225 rgbColor(false, backColor
, false);
3229 * Create a SGR parameter sequence for a single color change.
3231 * @param color one of the Color.WHITE, Color.BLUE, etc. constants
3232 * @param foreground if true, this is a foreground color
3233 * @param header if true, make the full header, otherwise just emit the
3234 * color parameter e.g. "42;"
3235 * @return the string to emit to an ANSI / ECMA-style terminal,
3238 private String
color(final Color color
, final boolean foreground
,
3239 final boolean header
) {
3241 int ecmaColor
= color
.getValue();
3243 // Convert Color.* values to SGR numerics
3251 return String
.format("\033[%dm", ecmaColor
);
3253 return String
.format("%d;", ecmaColor
);
3258 * Create a SGR parameter sequence for both foreground and background
3261 * @param bold if true, set bold
3262 * @param foreColor one of the Color.WHITE, Color.BLUE, etc. constants
3263 * @param backColor one of the Color.WHITE, Color.BLUE, etc. constants
3264 * @return the string to emit to an ANSI / ECMA-style terminal,
3265 * e.g. "\033[31;42m"
3267 private String
color(final boolean bold
, final Color foreColor
,
3268 final Color backColor
) {
3269 return color(foreColor
, backColor
, true) +
3270 rgbColor(bold
, foreColor
, backColor
);
3274 * Create a SGR parameter sequence for both foreground and
3275 * background color change.
3277 * @param foreColor one of the Color.WHITE, Color.BLUE, etc. constants
3278 * @param backColor one of the Color.WHITE, Color.BLUE, etc. constants
3279 * @param header if true, make the full header, otherwise just emit the
3280 * color parameter e.g. "31;42;"
3281 * @return the string to emit to an ANSI / ECMA-style terminal,
3282 * e.g. "\033[31;42m"
3284 private String
color(final Color foreColor
, final Color backColor
,
3285 final boolean header
) {
3287 int ecmaForeColor
= foreColor
.getValue();
3288 int ecmaBackColor
= backColor
.getValue();
3290 // Convert Color.* values to SGR numerics
3291 ecmaBackColor
+= 40;
3292 ecmaForeColor
+= 30;
3295 return String
.format("\033[%d;%dm", ecmaForeColor
, ecmaBackColor
);
3297 return String
.format("%d;%d;", ecmaForeColor
, ecmaBackColor
);
3302 * Create a SGR parameter sequence for foreground, background, and
3303 * several attributes. This sequence first resets all attributes to
3304 * default, then sets attributes as per the parameters.
3306 * @param foreColor one of the Color.WHITE, Color.BLUE, etc. constants
3307 * @param backColor one of the Color.WHITE, Color.BLUE, etc. constants
3308 * @param bold if true, set bold
3309 * @param reverse if true, set reverse
3310 * @param blink if true, set blink
3311 * @param underline if true, set underline
3312 * @return the string to emit to an ANSI / ECMA-style terminal,
3313 * e.g. "\033[0;1;31;42m"
3315 private String
color(final Color foreColor
, final Color backColor
,
3316 final boolean bold
, final boolean reverse
, final boolean blink
,
3317 final boolean underline
) {
3319 int ecmaForeColor
= foreColor
.getValue();
3320 int ecmaBackColor
= backColor
.getValue();
3322 // Convert Color.* values to SGR numerics
3323 ecmaBackColor
+= 40;
3324 ecmaForeColor
+= 30;
3326 StringBuilder sb
= new StringBuilder();
3327 if ( bold
&& reverse
&& blink
&& !underline
) {
3328 sb
.append("\033[0;1;7;5;");
3329 } else if ( bold
&& reverse
&& !blink
&& !underline
) {
3330 sb
.append("\033[0;1;7;");
3331 } else if ( !bold
&& reverse
&& blink
&& !underline
) {
3332 sb
.append("\033[0;7;5;");
3333 } else if ( bold
&& !reverse
&& blink
&& !underline
) {
3334 sb
.append("\033[0;1;5;");
3335 } else if ( bold
&& !reverse
&& !blink
&& !underline
) {
3336 sb
.append("\033[0;1;");
3337 } else if ( !bold
&& reverse
&& !blink
&& !underline
) {
3338 sb
.append("\033[0;7;");
3339 } else if ( !bold
&& !reverse
&& blink
&& !underline
) {
3340 sb
.append("\033[0;5;");
3341 } else if ( bold
&& reverse
&& blink
&& underline
) {
3342 sb
.append("\033[0;1;7;5;4;");
3343 } else if ( bold
&& reverse
&& !blink
&& underline
) {
3344 sb
.append("\033[0;1;7;4;");
3345 } else if ( !bold
&& reverse
&& blink
&& underline
) {
3346 sb
.append("\033[0;7;5;4;");
3347 } else if ( bold
&& !reverse
&& blink
&& underline
) {
3348 sb
.append("\033[0;1;5;4;");
3349 } else if ( bold
&& !reverse
&& !blink
&& underline
) {
3350 sb
.append("\033[0;1;4;");
3351 } else if ( !bold
&& reverse
&& !blink
&& underline
) {
3352 sb
.append("\033[0;7;4;");
3353 } else if ( !bold
&& !reverse
&& blink
&& underline
) {
3354 sb
.append("\033[0;5;4;");
3355 } else if ( !bold
&& !reverse
&& !blink
&& underline
) {
3356 sb
.append("\033[0;4;");
3358 assert (!bold
&& !reverse
&& !blink
&& !underline
);
3359 sb
.append("\033[0;");
3361 sb
.append(String
.format("%d;%dm", ecmaForeColor
, ecmaBackColor
));
3362 sb
.append(rgbColor(bold
, foreColor
, backColor
));
3363 return sb
.toString();
3367 * Create a SGR parameter sequence for foreground, background, and
3368 * several attributes. This sequence first resets all attributes to
3369 * default, then sets attributes as per the parameters.
3371 * @param foreColorRGB a 24-bit RGB value for foreground color
3372 * @param backColorRGB a 24-bit RGB value for foreground color
3373 * @param bold if true, set bold
3374 * @param reverse if true, set reverse
3375 * @param blink if true, set blink
3376 * @param underline if true, set underline
3377 * @return the string to emit to an ANSI / ECMA-style terminal,
3378 * e.g. "\033[0;1;31;42m"
3380 private String
colorRGB(final int foreColorRGB
, final int backColorRGB
,
3381 final boolean bold
, final boolean reverse
, final boolean blink
,
3382 final boolean underline
) {
3384 int foreColorRed
= (foreColorRGB
>>> 16) & 0xFF;
3385 int foreColorGreen
= (foreColorRGB
>>> 8) & 0xFF;
3386 int foreColorBlue
= foreColorRGB
& 0xFF;
3387 int backColorRed
= (backColorRGB
>>> 16) & 0xFF;
3388 int backColorGreen
= (backColorRGB
>>> 8) & 0xFF;
3389 int backColorBlue
= backColorRGB
& 0xFF;
3391 StringBuilder sb
= new StringBuilder();
3392 if ( bold
&& reverse
&& blink
&& !underline
) {
3393 sb
.append("\033[0;1;7;5;");
3394 } else if ( bold
&& reverse
&& !blink
&& !underline
) {
3395 sb
.append("\033[0;1;7;");
3396 } else if ( !bold
&& reverse
&& blink
&& !underline
) {
3397 sb
.append("\033[0;7;5;");
3398 } else if ( bold
&& !reverse
&& blink
&& !underline
) {
3399 sb
.append("\033[0;1;5;");
3400 } else if ( bold
&& !reverse
&& !blink
&& !underline
) {
3401 sb
.append("\033[0;1;");
3402 } else if ( !bold
&& reverse
&& !blink
&& !underline
) {
3403 sb
.append("\033[0;7;");
3404 } else if ( !bold
&& !reverse
&& blink
&& !underline
) {
3405 sb
.append("\033[0;5;");
3406 } else if ( bold
&& reverse
&& blink
&& underline
) {
3407 sb
.append("\033[0;1;7;5;4;");
3408 } else if ( bold
&& reverse
&& !blink
&& underline
) {
3409 sb
.append("\033[0;1;7;4;");
3410 } else if ( !bold
&& reverse
&& blink
&& underline
) {
3411 sb
.append("\033[0;7;5;4;");
3412 } else if ( bold
&& !reverse
&& blink
&& underline
) {
3413 sb
.append("\033[0;1;5;4;");
3414 } else if ( bold
&& !reverse
&& !blink
&& underline
) {
3415 sb
.append("\033[0;1;4;");
3416 } else if ( !bold
&& reverse
&& !blink
&& underline
) {
3417 sb
.append("\033[0;7;4;");
3418 } else if ( !bold
&& !reverse
&& blink
&& underline
) {
3419 sb
.append("\033[0;5;4;");
3420 } else if ( !bold
&& !reverse
&& !blink
&& underline
) {
3421 sb
.append("\033[0;4;");
3423 assert (!bold
&& !reverse
&& !blink
&& !underline
);
3424 sb
.append("\033[0;");
3427 sb
.append("m\033[38;2;");
3428 sb
.append(String
.format("%d;%d;%d", foreColorRed
, foreColorGreen
,
3430 sb
.append("m\033[48;2;");
3431 sb
.append(String
.format("%d;%d;%d", backColorRed
, backColorGreen
,
3434 return sb
.toString();
3438 * Create a SGR parameter sequence to reset to defaults.
3440 * @return the string to emit to an ANSI / ECMA-style terminal,
3443 private String
normal() {
3444 return normal(true) + rgbColor(false, Color
.WHITE
, Color
.BLACK
);
3448 * Create a SGR parameter sequence to reset to defaults.
3450 * @param header if true, make the full header, otherwise just emit the
3451 * bare parameter e.g. "0;"
3452 * @return the string to emit to an ANSI / ECMA-style terminal,
3455 private String
normal(final boolean header
) {
3457 return "\033[0;37;40m";
3463 * Create a SGR parameter sequence for enabling the visible cursor.
3465 * @param on if true, turn on cursor
3466 * @return the string to emit to an ANSI / ECMA-style terminal
3468 private String
cursor(final boolean on
) {
3469 if (on
&& !cursorOn
) {
3473 if (!on
&& cursorOn
) {
3481 * Clear the entire screen. Because some terminals use back-color-erase,
3482 * set the color to white-on-black beforehand.
3484 * @return the string to emit to an ANSI / ECMA-style terminal
3486 private String
clearAll() {
3487 return "\033[0;37;40m\033[2J";
3491 * Clear the line from the cursor (inclusive) to the end of the screen.
3492 * Because some terminals use back-color-erase, set the color to
3493 * white-on-black beforehand.
3495 * @return the string to emit to an ANSI / ECMA-style terminal
3497 private String
clearRemainingLine() {
3498 return "\033[0;37;40m\033[K";
3502 * Move the cursor to (x, y).
3504 * @param x column coordinate. 0 is the left-most column.
3505 * @param y row coordinate. 0 is the top-most row.
3506 * @return the string to emit to an ANSI / ECMA-style terminal
3508 private String
gotoXY(final int x
, final int y
) {
3509 return String
.format("\033[%d;%dH", y
+ 1, x
+ 1);
3513 * Tell (u)xterm that we want to receive mouse events based on "Any event
3514 * tracking", UTF-8 coordinates, and then SGR coordinates. Ideally we
3515 * will end up with SGR coordinates with UTF-8 coordinates as a fallback.
3517 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
3519 * Note that this also sets the alternate/primary screen buffer.
3521 * Finally, also emit a Privacy Message sequence that Jexer recognizes to
3522 * mean "hide the mouse pointer." We have to use our own sequence to do
3523 * this because there is no standard in xterm for unilaterally hiding the
3524 * pointer all the time (regardless of typing).
3526 * @param on If true, enable mouse report and use the alternate screen
3527 * buffer. If false disable mouse reporting and use the primary screen
3529 * @return the string to emit to xterm
3531 private String
mouse(final boolean on
) {
3533 return "\033[?1002;1003;1005;1006h\033[?1049h\033^hideMousePointer\033\\";
3535 return "\033[?1002;1003;1006;1005l\033[?1049l\033^showMousePointer\033\\";