Skip to content

Commit d4c3955

Browse files
committed
Wire up kitty tmux workaround to terminal query result.
When we see tmux, we need to do things differently, so let's bubble that up from when we query the terminal anyway. Issues: #95
1 parent af963fe commit d4c3955

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

src/kitty-canvas.cc

+6-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,12 @@ static char *AppendEscaped(char *pos, char c, bool wrap_tmux) {
7171

7272
KittyGraphicsCanvas::KittyGraphicsCanvas(BufferedWriteSequencer *ws,
7373
ThreadPool *thread_pool,
74+
bool tmux_workaround_needed,
7475
const DisplayOptions &opts)
75-
: TerminalCanvas(ws), options_(opts), executor_(thread_pool) {}
76+
: TerminalCanvas(ws),
77+
options_(opts),
78+
tmux_workaround_needed_(tmux_workaround_needed),
79+
executor_(thread_pool) {}
7680

7781
void KittyGraphicsCanvas::Send(int x, int dy, const Framebuffer &fb_orig,
7882
SeqType seq_type, Duration end_of_frame) {
@@ -123,7 +127,7 @@ void KittyGraphicsCanvas::Send(int x, int dy, const Framebuffer &fb_orig,
123127
const int cols = fb->width() / opts.cell_x_px;
124128
const int rows = -cell_height_for_pixels(-fb->height());
125129
const int indent = x / opts.cell_x_px;
126-
bool wrap_tmux = false;
130+
bool wrap_tmux = tmux_workaround_needed_;
127131
std::function<OutBuffer()> encode_fun = [opts, fb, id, buffer, offset, rows,
128132
cols, indent, wrap_tmux]() {
129133
std::unique_ptr<const Framebuffer> auto_delete(fb);

src/kitty-canvas.h

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace timg {
2525
class KittyGraphicsCanvas final : public TerminalCanvas {
2626
public:
2727
KittyGraphicsCanvas(BufferedWriteSequencer *ws, ThreadPool *thread_pool,
28+
bool tmux_workaround_needed,
2829
const DisplayOptions &opts);
2930

3031
int cell_height_for_pixels(int pixels) const final;
@@ -34,6 +35,7 @@ class KittyGraphicsCanvas final : public TerminalCanvas {
3435

3536
private:
3637
const DisplayOptions &options_;
38+
const bool tmux_workaround_needed_;
3739
ThreadPool *const executor_;
3840

3941
char *RequestBuffer(int width, int height);

src/term-query.cc

+4
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ TermGraphicsInfo QuerySupportedGraphicsProtocol() {
186186
TermGraphicsInfo result;
187187
result.preferred_graphics = GraphicsProtocol::kNone;
188188
result.known_broken_sixel_cursor_placement = false;
189+
result.in_tmux = false;
189190

190191
const char *const term = getenv("TERM");
191192
if (term && strcmp(term, "xterm-kitty") == 0) {
@@ -229,6 +230,9 @@ TermGraphicsInfo QuerySupportedGraphicsProtocol() {
229230
result.preferred_graphics = GraphicsProtocol::kSixel;
230231
result.known_broken_sixel_cursor_placement = true;
231232
}
233+
if (contains(data, len, "tmux")) {
234+
result.in_tmux = true;
235+
}
232236
// We finish once we found the response to DSR5
233237
return (const char *)memmem(data, len, "\033[0n", 3);
234238
});

src/term-query.h

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ enum class GraphicsProtocol {
3030
struct TermGraphicsInfo {
3131
GraphicsProtocol preferred_graphics;
3232
bool known_broken_sixel_cursor_placement;
33+
bool in_tmux;
3334
};
3435

3536
// Query the terminal if and what graphics protocol it supports.

src/timg.cc

+20-1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ struct PresentationOptions {
123123
// Rendering
124124
Pixelation pixelation = Pixelation::kNotChosen;
125125
bool sixel_cursor_workaround = false;
126+
bool tmux_workaround = false;
126127
bool terminal_use_upper_block = false;
127128
bool use_256_color = false; // For terminals that don't do 24 bit color
128129

@@ -306,6 +307,7 @@ static void PresentImages(LoadedImageSources *loaded_sources,
306307
case Pixelation::kKittyGraphics:
307308
compression_pool.reset(new ThreadPool(sequencer->max_queue_len() + 1));
308309
canvas.reset(new KittyGraphicsCanvas(sequencer, compression_pool.get(),
310+
present.tmux_workaround,
309311
display_opts));
310312
break;
311313
case Pixelation::kiTerm2Graphics:
@@ -728,7 +730,8 @@ int main(int argc, char *argv[]) {
728730
present.pixelation = Pixelation::kQuarterBlock; // Good default.
729731
#ifdef WITH_TIMG_TERMINAL_QUERY
730732
if (term.font_width_px > 0 && term.font_height_px > 0) {
731-
auto graphics_info = timg::QuerySupportedGraphicsProtocol();
733+
auto graphics_info = timg::QuerySupportedGraphicsProtocol();
734+
present.tmux_workaround = graphics_info.in_tmux;
732735
switch (graphics_info.preferred_graphics) {
733736
case timg::GraphicsProtocol::kIterm2:
734737
present.pixelation = Pixelation::kiTerm2Graphics;
@@ -750,6 +753,11 @@ int main(int argc, char *argv[]) {
750753
}
751754
#endif
752755
}
756+
else if (present.pixelation == Pixelation::kKittyGraphics) {
757+
// If the user manually chooses kitty, we still need to know if in tmux
758+
auto graphics_info = timg::QuerySupportedGraphicsProtocol();
759+
present.tmux_workaround = graphics_info.in_tmux;
760+
}
753761
#if defined(WITH_TIMG_SIXEL) && defined(WITH_TIMG_TERMINAL_QUERY)
754762
// If the user manually choose sixel, we still can't avoid a terminal
755763
// query, as we have to figure out if it has a broken cursor implementation.
@@ -760,6 +768,17 @@ int main(int argc, char *argv[]) {
760768
}
761769
#endif
762770

771+
// If we are in tmux and use the kitty protocol, we need to tell it to
772+
// allow pass-through.
773+
// Somewhat broken abstraction here, should move to some
774+
// TerminalCanvas::Prepare().
775+
if (present.pixelation == Pixelation::kKittyGraphics &&
776+
present.tmux_workaround) {
777+
if (system("tmux set -p allow-passthrough on") != 0) {
778+
fprintf(stderr, "Could not set tmux passthrough for graphics\n");
779+
}
780+
}
781+
763782
// If 'none' is chosen for background color, then using the
764783
// PNG compression with alpha channels gives us compositing on client side
765784
if (is_pixel_direct_p(present.pixelation) &&

0 commit comments

Comments
 (0)