Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement OSC533 screenshot #1648

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/vtbackend/Functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ constexpr inline auto SETICON = FunctionDocumentation { .mnemonic = "SETICON", .
constexpr inline auto SETTITLE = FunctionDocumentation { .mnemonic = "SETTITLE", .comment = "Change Window & Icon Title" };
constexpr inline auto SETWINTITLE = FunctionDocumentation { .mnemonic = "SETWINTITLE", .comment = "Change Window Title" };
constexpr inline auto SETXPROP = FunctionDocumentation { .mnemonic = "SETXPROP", .comment = "Set X11 property" };
constexpr inline auto SCREENSHOT = FunctionDocumentation { .mnemonic = "SCREENSHOT", .comment = "Screenshot of the screen" };

} // namespace documentation

Expand Down Expand Up @@ -621,6 +622,7 @@ constexpr inline auto SETICON = detail::OSC(1, VTExtension::XTerm, doc
constexpr inline auto SETTITLE = detail::OSC(0, VTExtension::XTerm, documentation::SETTITLE);
constexpr inline auto SETWINTITLE = detail::OSC(2, VTExtension::XTerm, documentation::SETWINTITLE);
constexpr inline auto SETXPROP = detail::OSC(3, VTExtension::XTerm, documentation::SETXPROP);
constexpr inline auto SCREENSHOT = detail::OSC(533, VTExtension::Contour, documentation::SCREENSHOT);

constexpr inline auto CaptureBufferCode = 314;

Expand Down Expand Up @@ -754,6 +756,7 @@ constexpr static auto allFunctionsArray() noexcept
SETTITLE,
SETWINTITLE,
SETXPROP,
SCREENSHOT,
SETCOLPAL,
SETCWD,
HYPERLINK,
Expand Down
32 changes: 32 additions & 0 deletions src/vtbackend/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include <type_traits>
#include <variant>

#include "vtbackend/primitives.h"

#if defined(_WIN32)
#include <Windows.h>
#endif
Expand Down Expand Up @@ -3703,6 +3705,35 @@ ApplyResult Screen<Cell>::apply(Function const& function, Sequence const& seq)
case SETICON: return ApplyResult::Ok; // NB: Silently ignore!
case SETWINTITLE: _terminal->setWindowTitle(seq.intermediateCharacters()); break;
case SETXPROP: return ApplyResult::Unsupported;
case SCREENSHOT: {
auto coords = seq.intermediateCharacters();
// format: top;left;bottom;right
auto splitted = crispy::split(coords, ';');
auto top = std::stoi(std::string { splitted[0] });
auto left = std::stoi(std::string { splitted[1] });
auto bottom = std::stoi(std::string { splitted[2] });
auto right = std::stoi(std::string { splitted[3] });

// OSC 533 ; top ; left ; lines ; columns ; format ; Pt ST
std::string answer;
auto out = std::back_inserter(answer);
std::format_to(
out, "{};{};{};{};{};Pt", top, left, bottom - top, right - left, "plain text");

for (auto indexRow = top; indexRow <= bottom; ++indexRow)
{
for (auto indexColumn = left; indexColumn <= right; ++indexColumn)
{
auto& cell = at(LineOffset { indexRow }, ColumnOffset { indexColumn });
std::format_to(out, "{}", cell.toUtf8());
}
std::format_to(out, "\n");
}
std::cout << std::format("HERE: {} '{}'\n", answer.size(), answer);
answer = std::format("\033]533;{}",answer);

return ApplyResult::Unsupported;
}
case SETCOLPAL: return impl::SETCOLPAL(seq, *_terminal);
case RCOLPAL: return impl::RCOLPAL(seq, *_terminal);
case SETCWD: return impl::SETCWD(seq, *this);
Expand Down Expand Up @@ -3761,6 +3792,7 @@ unique_ptr<ParserExtension> Screen<Cell>::hookSixel(Sequence const& seq)
case 4:
case 3: return 3;
case 2: return 5;

case 1:
case 0: return 2;
default: return 1;
Expand Down
Loading