Skip to content

Commit

Permalink
Yaft: change keyboard hold behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
timower committed Jan 25, 2021
1 parent ec66601 commit bee9637
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 24 deletions.
4 changes: 4 additions & 0 deletions apps/yaft/ctrlseq/csi.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ set_mode(struct terminal_t* term, struct parm_t* parm) {
term->mode |= MODE_VWBS;
} else if (mode == 1000) {
term->mode |= MODE_MOUSE;
} else if (mode == 1002) {
term->mode |= MODE_MOUSE_MOVE;
} else {
printf("UNKNOWN MODE: %d\n", mode);
}
Expand Down Expand Up @@ -461,6 +463,8 @@ reset_mode(struct terminal_t* term, struct parm_t* parm) {
term->mode &= ~MODE_VWBS;
} else if (mode == 1000) {
term->mode &= ~MODE_MOUSE;
} else if (mode == 1002) {
term->mode &= ~MODE_MOUSE_MOVE;
}
}
}
Expand Down
48 changes: 31 additions & 17 deletions apps/yaft/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ Keyboard::init(rmlib::fb::FrameBuffer& fb, terminal_t& term) {
int y = startHeight;
int rowNum = 0;
for (const auto& row : layout) {
int x = 0;
int x = (fb.canvas.width - row_size * baseKeyWidth) / 2;
for (const auto& key : row) {
const auto keyWidth = baseKeyWidth * key.width;

Expand Down Expand Up @@ -337,10 +337,13 @@ Keyboard::drawKey(const Key& key) const {
if (key.isDown()) {
auto a = key.keyRect.topLeft + Point{ 2, 2 };
auto b = key.keyRect.bottomRight - Point{ 1, 1 };
fb->canvas.drawLine(a, { b.x, a.y }, 0x0);
fb->canvas.drawLine(a, { a.x, b.y }, 0x0);
fb->canvas.drawLine(b, { b.x, a.y }, 0x0);
fb->canvas.drawLine(b, { a.x, b.y }, 0x0);

if (key.held) {
fb->canvas.drawLine(a, { b.x, a.y }, 0x0);
fb->canvas.drawLine(b, { a.x, b.y }, 0x0);
}
}
}

Expand Down Expand Up @@ -419,15 +422,18 @@ struct event_traits<PenEvent> {
template<typename Event>
void
handleScreenEvent(Keyboard& kb, const Event& ev) {
if ((kb.term->mode & MODE_MOUSE) == 0) {
if ((kb.term->mode & ALL_MOUSE_MODES) == 0) {
return;
}

const auto slot = event_traits<Event>::getSlot(ev);

const auto loc = ev.location - kb.screenRect.topLeft;
char cx = 33 + (loc.x / CELL_WIDTH);
char cy = 33 + (loc.y / CELL_HEIGHT);
auto loc = ev.location - kb.screenRect.topLeft;
loc.x /= CELL_WIDTH;
loc.y /= CELL_HEIGHT;

char cx = 33 + loc.x;
char cy = 33 + loc.y;
char buf[] = { esc_char, '[', 'M', 32, cx, cy };

if (ev.type == event_traits<Event>::down_type) {
Expand All @@ -451,8 +457,13 @@ handleScreenEvent(Keyboard& kb, const Event& ev) {
// Send mouse up code
buf[3] += 3; // mouse release
write(kb.term->fd, buf, 6);
} else if (kb.mouseSlot != -1 && kb.lastMousePos != loc &&
(kb.term->mode & MODE_MOUSE_MOVE) != 0) {
buf[3] += 32; // mouse move
write(kb.term->fd, buf, 6);
}
}
kb.lastMousePos = loc;
} // namespace

template<typename Event>
void
Expand All @@ -469,32 +480,31 @@ handleKeyEvent(Keyboard& kb, const Event& ev) {
key->slot = event_traits<Event>::getSlot(ev);
key->nextRepeat = time_source::now() + repeat_delay;

kb.drawKey(*key);
kb.fb->doUpdate(
key->keyRect, rmlib::fb::Waveform::DU, rmlib::fb::UpdateFlags::None);

kb.sendKeyDown(*key);

// Clear sticky keys.
if (!isModifier(key->info.code)) {
for (auto* key : { kb.shiftKey, kb.altKey, kb.ctrlKey }) {
key->nextRepeat = time_source::now() + repeat_delay;
if (key->stuck) {
key->stuck = false;
key->nextRepeat = time_source::now() + repeat_delay;
kb.drawKey(*key);
kb.fb->doUpdate(key->keyRect,
rmlib::fb::Waveform::DU,
rmlib::fb::UpdateFlags::None);
}
}
} else {
if constexpr (std::is_same_v<Event, PenEvent>) {
if (!key->held) {
key->stuck = !key->stuck;
} else {
key->stuck = false;
}
key->held = false;
}

kb.drawKey(*key);
kb.fb->doUpdate(
key->keyRect, rmlib::fb::Waveform::DU, rmlib::fb::UpdateFlags::None);

} else if (ev.type == event_traits<Event>::up_type) {
const auto slot = event_traits<Event>::getSlot(ev);
auto keyIt =
Expand Down Expand Up @@ -543,7 +553,11 @@ Keyboard::updateRepeat() {
if (time > key.nextRepeat) {
// If a modifier is long pressed, stick it.
if (isModifier(key.info.code)) {
key.stuck = true;
key.held = true;

drawKey(key);
fb->doUpdate(
key.keyRect, rmlib::fb::Waveform::DU, rmlib::fb::UpdateFlags::None);
}

sendKeyDown(key);
Expand Down
7 changes: 5 additions & 2 deletions apps/yaft/keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ struct Keyboard {
rmlib::Rect keyRect;

int slot = -1;
bool stuck = false;

bool isDown() const { return slot != -1 || stuck; }
bool stuck = false; // Used for mod keys, get stuck after tap.
bool held = false; // Used for mod keys, held down after long press.

bool isDown() const { return slot != -1 || stuck || held; }

time_source::time_point nextRepeat;
};
Expand Down Expand Up @@ -67,6 +69,7 @@ struct Keyboard {
terminal_t* term;

int mouseSlot = -1;
rmlib::Point lastMousePos;

// Pointers for tracking modifier state.
Key* shiftKey = nullptr;
Expand Down
15 changes: 10 additions & 5 deletions apps/yaft/yaft.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,37 +126,42 @@ enum term_mode {
MODE_VWBS = 0x08, /* variable-width backspace */
MODE_APP_CURSOR = 0x10, /* app cursor mode */
MODE_MOUSE = 0x20, /* enable xterm mouse */
MODE_MOUSE_MOVE = 0x40, /* enable xterm mouse */
};

#ifdef __cplusplus
inline term_mode&
constexpr term_mode&
operator|=(term_mode& m, term_mode o) {
m = (term_mode)(m | o);
return m;
}

inline term_mode
constexpr term_mode
operator|(term_mode m, term_mode o) {
return (term_mode)((int)m | (int)o);
}

inline term_mode&
constexpr term_mode&
operator&=(term_mode& m, term_mode o) {
m = (term_mode)(m & o);
return m;
}

inline term_mode
constexpr term_mode
operator&(term_mode m, term_mode o) {
return (term_mode)((int)m & (int)o);
}

inline term_mode
constexpr term_mode
operator~(term_mode m) {
return (term_mode)(~(int)m);
}
#endif

enum {
ALL_MOUSE_MODES = MODE_MOUSE_MOVE | MODE_MOUSE,
};

enum esc_state {
STATE_RESET = 0x00,
STATE_ESC = 0x01, /* 0x1B, \033, ESC */
Expand Down

0 comments on commit bee9637

Please sign in to comment.