Skip to content

Commit

Permalink
Merge pull request #5 from jstkdng/sixel-gif
Browse files Browse the repository at this point in the history
Sixel gif
  • Loading branch information
jstkdng authored Feb 14, 2023
2 parents ab00bf2 + 9e14b89 commit f016a6f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ auto Application::execute(const std::string& cmd) -> void
if (j["action"] == "add") {
int max_width = (static_cast<int>(j["max_width"]) - 1) * terminal.font_width;
int max_height = (static_cast<int>(j["max_height"]) - 1) * terminal.font_height;
int x = static_cast<int>(j["x"]) * terminal.font_width;
int y = static_cast<int>(j["y"]) * terminal.font_height;
int x = static_cast<int>(j["x"]);
int y = static_cast<int>(j["y"]);
canvas->create(x, y, max_width, max_height);
image = Image::load(j["path"], max_width, max_height);
if (!image) {
Expand Down
2 changes: 1 addition & 1 deletion src/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ auto Canvas::init(const Terminal& terminal) -> std::unique_ptr<Canvas>
return std::make_unique<SixelCanvas>();
}
logger << "=== Using X11 output" << std::endl;
return std::make_unique<X11Canvas>();
return std::make_unique<X11Canvas>(terminal);
}
19 changes: 13 additions & 6 deletions src/canvas/sixel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ auto SixelCanvas::is_supported(const Terminal& terminal) -> bool
SixelCanvas::SixelCanvas()
{
sixel_encoder_new(&encoder, nullptr);
sixel_encoder_setopt(encoder, SIXEL_OPTFLAG_8BIT_MODE, nullptr);
}

SixelCanvas::~SixelCanvas()
Expand All @@ -41,13 +40,14 @@ SixelCanvas::~SixelCanvas()
}

auto SixelCanvas::create(int x, int y, int max_width, int max_height) -> void
{}
{
this->x = x + 1;
this->y = y + 1;
}

auto SixelCanvas::draw(Image& image) -> void
{
draw_frame(image);
// TODO: how to do this with sixel?
/*if (image.framerate() == -1) {
if (image.framerate() == -1) {
draw_frame(image);
return;
}
Expand All @@ -58,7 +58,7 @@ auto SixelCanvas::draw(Image& image) -> void
unsigned long duration = (1.0 / image.framerate()) * 1000;
std::this_thread::sleep_for(std::chrono::milliseconds(duration));
}
});*/
});
}

auto SixelCanvas::clear() -> void
Expand All @@ -68,6 +68,7 @@ auto SixelCanvas::clear() -> void

auto SixelCanvas::draw_frame(const Image& image) -> void
{
move_cursor(y, x);
sixel_encoder_encode_bytes(encoder,
const_cast<unsigned char*>(image.data()),
image.width(),
Expand All @@ -76,3 +77,9 @@ auto SixelCanvas::draw_frame(const Image& image) -> void
nullptr,
(-1));
}

auto SixelCanvas::move_cursor(int row, int col) -> void
{
printf("\033[%d;%df", row, col);
fflush(stdout);
}
3 changes: 3 additions & 0 deletions src/canvas/sixel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ class SixelCanvas : public Canvas
private:
sixel_encoder_t *encoder;
std::unique_ptr<std::jthread> draw_thread;
int x;
int y;

auto draw_frame(const Image& image) -> void;
auto move_cursor(int row, int col) -> void;
};


Expand Down
9 changes: 6 additions & 3 deletions src/canvas/x11/x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ struct free_delete
void operator()(void* x) { free(x); }
};

X11Canvas::X11Canvas()
X11Canvas::X11Canvas(const Terminal& terminal):
terminal(terminal)
{
connection = xcb_connect(nullptr, nullptr);
if (xcb_connection_has_error(connection)) {
Expand Down Expand Up @@ -63,7 +64,8 @@ auto X11Canvas::create(int x, int y, int max_width, int max_height) -> void
// if WID exists prevent doing any calculations
auto proc = client_pids.front();
windows.push_back(std::make_unique<Window>(connection, screen,
std::stoi(wid.value()), x, y, max_width, max_height));
std::stoi(wid.value()), x * terminal.font_width,
y * terminal.font_height, max_width, max_height));
return;
}

Expand All @@ -73,7 +75,8 @@ auto X11Canvas::create(int x, int y, int max_width, int max_height) -> void
for (const auto& ppid: ppids) {
if (!pid_window_map.contains(ppid.pid)) continue;
windows.push_back(std::make_unique<Window>(connection, screen,
pid_window_map[ppid.pid], x, y, max_width, max_height));
pid_window_map[ppid.pid], x * terminal.font_width,
y * terminal.font_height, max_width, max_height));
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/canvas/x11/x11.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "canvas.hpp"
#include "image.hpp"
#include "window.hpp"
#include "terminal.hpp"

#include <xcb/xproto.h>
#include <memory>
Expand All @@ -29,7 +30,7 @@
class X11Canvas : public Canvas
{
public:
X11Canvas();
X11Canvas(const Terminal& terminal);
~X11Canvas();

auto create(int x, int y, int max_width, int max_height) -> void override;
Expand All @@ -39,6 +40,7 @@ class X11Canvas : public Canvas
private:
xcb_connection_t *connection;
xcb_screen_t *screen;
const Terminal& terminal;

std::vector<std::unique_ptr<Window>> windows;

Expand Down

0 comments on commit f016a6f

Please sign in to comment.