Skip to content

Commit

Permalink
Add landscape support
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwiz authored and timower committed Aug 2, 2023
1 parent 5640fd1 commit 66e0a78
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 27 deletions.
38 changes: 24 additions & 14 deletions apps/yaft/fb/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ brightness2gray(uint16_t brightness) {
}

inline void
draw_sixel(rmlib::fb::FrameBuffer& fb, int y_start, int col, uint8_t* pixmap) {
draw_sixel(rmlib::fb::FrameBuffer& fb, struct terminal_t* term, int y_start, int margin_left, int col, uint8_t* pixmap) {
int h, w, src_offset, dst_offset;
uint32_t pixel, color = 0;

Expand All @@ -43,8 +43,13 @@ draw_sixel(rmlib::fb::FrameBuffer& fb, int y_start, int col, uint8_t* pixmap) {
src_offset = BYTES_PER_PIXEL * (h * CELL_WIDTH + w);
memcpy(&color, pixmap + src_offset, BYTES_PER_PIXEL);

dst_offset = (y_start + h) * fb.canvas.lineSize() +
(col * CELL_WIDTH + w) * fb.canvas.components();
if (term->isLandscape) {
dst_offset = (margin_left + w) * fb.canvas.lineSize() +
(y_start - h) * fb.canvas.components();
} else {
dst_offset = (y_start + h) * fb.canvas.lineSize() +
(margin_left + w) * fb.canvas.components();
}
auto grayMode = brightness2gray(color2brightness(color));

switch (grayMode) {
Expand Down Expand Up @@ -74,7 +79,11 @@ draw_line(rmlib::fb::FrameBuffer& fb, struct terminal_t* term, int line) {
struct color_pair_t color_pair;
struct cell_t* cellp;

y_start = term->marginTop + line * CELL_HEIGHT;
if (term->isLandscape) {
y_start = term->height - (term->marginTop + line * CELL_HEIGHT);
} else {
y_start = term->marginTop + line * CELL_HEIGHT;
}

for (col = 0; col < term->cols; col++) {
margin_left =
Expand All @@ -85,7 +94,7 @@ draw_line(rmlib::fb::FrameBuffer& fb, struct terminal_t* term, int line) {

/* draw sixel pixmap */
if (cellp->has_pixmap) {
draw_sixel(fb, y_start, col, cellp->pixmap);
draw_sixel(fb, term, y_start, margin_left, col, cellp->pixmap);
continue;
}

Expand Down Expand Up @@ -141,8 +150,13 @@ draw_line(rmlib::fb::FrameBuffer& fb, struct terminal_t* term, int line) {
}

for (w = 0; w < CELL_WIDTH; w++) {
pos = (margin_left + w) * fb.canvas.components() +
(y_start + h) * fb.canvas.lineSize();
if (term->isLandscape) {
pos = (margin_left + w) * fb.canvas.lineSize() +
(y_start - h) * fb.canvas.components();
} else {
pos = (margin_left + w) * fb.canvas.components() +
(y_start + h) * fb.canvas.lineSize();
}

/* set fg or bg */
const auto* glyph = (cellp->attribute & ATTR_BOLD) != 0
Expand Down Expand Up @@ -175,17 +189,13 @@ draw_line(rmlib::fb::FrameBuffer& fb, struct terminal_t* term, int line) {
/* actual display update (bit blit) */
// TODO: group updates.
fb.doUpdate(
{ { 0, y_start }, { fb.canvas.width() - 1, y_start + CELL_HEIGHT - 1 } },
term->isLandscape ?
(rmlib::Rect){ { y_start, 0 }, { y_start - CELL_HEIGHT, term->width - 1 } } :
(rmlib::Rect){ { 0, y_start }, { fb.canvas.width() - 1, y_start + CELL_HEIGHT - 1 } },
rmlib::fb::Waveform::DU,
rmlib::fb::UpdateFlags::None);
update_count++;

/* TODO: page flip
if fb_fix_screeninfo.ypanstep > 0, we can use hardware panning.
set fb_fix_screeninfo.{yres_virtual,yoffset} and call
ioctl(FBIOPAN_DISPLAY) but drivers of recent hardware (inteldrmfb,
nouveaufb, radeonfb) don't support... (maybe we can use this by using
libdrm) */
/* TODO: vertical synchronizing */

term->line_dirty[line] =
Expand Down
26 changes: 19 additions & 7 deletions apps/yaft/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,12 +397,21 @@ Keyboard::init(rmlib::fb::FrameBuffer& fb, terminal_t& term) {

void
Keyboard::initKeyMap() {
// do physical keys:
for (const auto& keyInfo : keymap) {
physicalKeys.emplace(keyInfo.code, PhysicalKey{ keyInfo });
}

if (term->isLandscape) {
return;
}

baseKeyWidth = fb->canvas.width() / row_size;
startHeight =
fb->canvas.height() - (hidden ? hidden_keyboard_height : keyboard_height);

// Resize the terminal to make place for the keyboard.
term_resize(term, fb->canvas.width(), startHeight);
term_resize(term, fb->canvas.width(), startHeight, /* isLandscape */ false);

// Setup the keymap.
keys.clear();
Expand Down Expand Up @@ -441,11 +450,6 @@ Keyboard::initKeyMap() {
int marginLeft = term->width - term->cols * CELL_WIDTH;
this->screenRect = Rect{ { marginLeft, term->marginTop },
{ term->width - 1, term->height - 1 } };

// do physical keys:
for (const auto& keyInfo : keymap) {
physicalKeys.emplace(keyInfo.code, PhysicalKey{ keyInfo });
}
}

void
Expand Down Expand Up @@ -505,6 +509,10 @@ Keyboard::drawKey(const Key& key) const {

void
Keyboard::draw() const {
if (term->isLandscape) {
return;
}

Rect keyboardRect = { { 0, startHeight },
{ fb->canvas.width() - 1, fb->canvas.height() - 1 } };

Expand Down Expand Up @@ -802,7 +810,7 @@ Keyboard::handleEvents(const std::vector<rmlib::input::Event>& events) {
// If the event is not on the screen or it's a release event of a
// previously pressed key, handle it as a keyboard event. Otherwise
// handle it as a screen (mouse) event.
if (!screenRect.contains(ev.location) || isKeyRelease(*this, ev)) {
if ((!screenRect.contains(ev.location) || isKeyRelease(*this, ev)) && !term->isLandscape) {
handleKeyEvent(*this, ev);
} else {
handleScreenEvent(*this, ev);
Expand All @@ -817,6 +825,10 @@ Keyboard::handleEvents(const std::vector<rmlib::input::Event>& events) {

void
Keyboard::updateRepeat() {
if (term->isLandscape) {
return;
}

const auto time = time_source::now();

for (auto& key : keys) {
Expand Down
13 changes: 12 additions & 1 deletion apps/yaft/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ main(int argc, const char* argv[]) {
extern const char* shell_cmd; /* defined in conf.h */
const char* cmd;
const char** args;
int w, h;
bool isLandscape;
uint8_t buf[BUFSIZE];
ssize_t size;
struct terminal_t term;
Expand All @@ -214,7 +216,16 @@ main(int argc, const char* argv[]) {
goto fb_init_failed;
}

if (!term_init(&term, fb->canvas.width(), fb->canvas.height())) {
isLandscape = is_pogo_connected();
if (isLandscape) {
w = fb->canvas.height();
h = fb->canvas.width();
} else {
w = fb->canvas.width();
h = fb->canvas.height();
}

if (!term_init(&term, w, h, isLandscape)) {
logging(FATAL, "terminal initialize failed\n");
goto term_init_failed;
}
Expand Down
9 changes: 6 additions & 3 deletions apps/yaft/terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,14 @@ term_die(struct terminal_t* term) {
}

bool
term_init(struct terminal_t* term, int width, int height) {
term_init(struct terminal_t* term, int width, int height, bool isLandscape) {
extern const uint32_t color_list[COLORS]; /* global */

term->width = width;
term->height = height;
term->isLandscape = isLandscape;

/* 1 px margin on edges */
term->cols = (term->width - 2) / CELL_WIDTH;
term->lines = (term->height - 2) / CELL_HEIGHT;

Expand Down Expand Up @@ -448,12 +450,13 @@ term_init(struct terminal_t* term, int width, int height) {
}

void
term_resize(struct terminal_t* term, int width, int height) {
if (width == term->width && height == term->height)
term_resize(struct terminal_t* term, int width, int height, bool isLandscape) {
if (width == term->width && height == term->height && isLandscape == term->isLandscape)
return;

term->width = width;
term->height = height;
term->isLandscape = isLandscape;

term->cols = term->width / CELL_WIDTH;
term->lines = term->height / CELL_HEIGHT;
Expand Down
4 changes: 2 additions & 2 deletions apps/yaft/terminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ void
term_die(struct terminal_t* term);

bool
term_init(struct terminal_t* term, int width, int height);
term_init(struct terminal_t* term, int width, int height, bool isLandscape);

void
term_resize(struct terminal_t* term, int width, int height);
term_resize(struct terminal_t* term, int width, int height, bool isLandscape);

#ifdef __cplusplus
}
Expand Down
14 changes: 14 additions & 0 deletions apps/yaft/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,17 @@ sum(struct parm_t* parm) {

return sum;
}

static inline bool
is_pogo_connected() {
int fd = open("/sys/pogo/status/pogo_connected", O_RDWR);
if (fd == -1) {
return false;
}

char buf = '\0';
read(fd, &buf, 1);
close(fd);

return buf == '1';
}
1 change: 1 addition & 0 deletions apps/yaft/yaft.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ struct terminal_t {
int fd; /* master of pseudo terminal */
int width, height; /* terminal size (pixel) */
int cols, lines; /* terminal size (cell) */
bool isLandscape; /* screen orientation */
struct cell_t** cells; /* pointer to each cell: cells[y * lines + x] */
struct margin_t scroll; /* scroll margin */
struct point_t cursor; /* cursor pos (x, y) */
Expand Down

0 comments on commit 66e0a78

Please sign in to comment.