#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: selection.c,v 1.12 2023/02/07 20:37:48 mlelstv Exp $");
#endif
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/tty.h>
#include <dev/wscons/wsconsio.h>
#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "pathnames.h"
#include "wsmoused.h"
int selection_startup(struct mouse *m);
int selection_cleanup(void);
void selection_wsmouse_event(struct wscons_event);
void selection_wscons_event(struct wscons_event, int);
void selection_poll_timeout(void);
struct mode_bootstrap Selection_Mode = {
"selection",
selection_startup,
selection_cleanup,
selection_wsmouse_event,
selection_wscons_event,
selection_poll_timeout
};
struct selarea {
size_t sa_x1;
size_t sa_y1;
size_t sa_x2;
size_t sa_y2;
size_t sa_startoff;
size_t sa_endoff;
size_t sa_buflen;
char *sa_buf;
};
struct selmouse {
struct mouse *sm_mouse;
int sm_ttyfd;
size_t sm_x;
size_t sm_y;
size_t sm_max_x;
size_t sm_max_y;
size_t sm_slowdown_x;
size_t sm_slowdown_y;
size_t sm_count_x;
size_t sm_count_y;
int sm_visible;
int sm_selecting;
int sm_but_select;
int sm_but_paste;
};
static struct selmouse Selmouse;
static struct selarea Selarea;
static int Initialized = 0;
static void cursor_hide(void);
static void cursor_show(void);
static void open_tty(int);
static void char_invert(size_t, size_t);
static void *alloc_sel(size_t);
static char *fill_buf(char *, size_t, size_t, size_t);
static size_t row_length(size_t);
static void selarea_copy_text(void);
static void selarea_start(void);
static void selarea_end(void);
static void selarea_calculate(void);
static void selarea_getrowcol(size_t, size_t *, size_t *);
static void selarea_hide(void);
static void selarea_show(void);
static void selarea_paste(void);
int
selection_startup(struct mouse *m)
{
int i;
struct wsdisplay_char ch;
struct block *conf;
if (Initialized) {
log_warnx("selection mode already initialized");
return 1;
}
(void)memset(&Selmouse, 0, sizeof(struct selmouse));
Selmouse.sm_mouse = m;
conf = config_get_mode("selection");
Selmouse.sm_slowdown_x = block_get_propval_int(conf, "slowdown_x", 0);
Selmouse.sm_slowdown_y = block_get_propval_int(conf, "slowdown_y", 3);
if (block_get_propval_int(conf, "lefthanded", 0)) {
Selmouse.sm_but_select = 2;
Selmouse.sm_but_paste = 0;
} else {
Selmouse.sm_but_select = 0;
Selmouse.sm_but_paste = 2;
}
(void)ioctl(Selmouse.sm_mouse->m_statfd, WSDISPLAYIO_GETACTIVESCREEN,
&i);
Selmouse.sm_ttyfd = -1;
open_tty(i);
ch.row = ch.col = 0;
if (ioctl(Selmouse.sm_ttyfd, WSDISPLAYIO_GETWSCHAR, &ch) < 0) {
(void)close(Selmouse.sm_ttyfd);
log_warn("ioctl(WSDISPLAYIO_GETWSCHAR) failed");
return 0;
}
assert(Selmouse.sm_max_y != 0);
assert(Selmouse.sm_max_x != 0);
Selmouse.sm_y = Selmouse.sm_max_y / 2;
Selmouse.sm_x = Selmouse.sm_max_x / 2;
Selmouse.sm_count_y = 0;
Selmouse.sm_count_x = 0;
Selmouse.sm_visible = 0;
Selmouse.sm_selecting = 0;
Initialized = 1;
return 1;
}
int
selection_cleanup(void)
{
cursor_hide();
if (Selmouse.sm_ttyfd >= 0)
(void)close(Selmouse.sm_ttyfd);
return 1;
}
void
selection_wsmouse_event(struct wscons_event evt)
{
const struct wsmouse_calibcoords *abs = &Selmouse.sm_mouse->m_calib;
if (IS_MOTION_EVENT(evt.type)) {
if (Selmouse.sm_selecting)
selarea_hide();
cursor_hide();
switch (evt.type) {
case WSCONS_EVENT_MOUSE_DELTA_X:
if (Selmouse.sm_count_x >= Selmouse.sm_slowdown_x) {
Selmouse.sm_count_x = 0;
if (evt.value > 0)
Selmouse.sm_x++;
else if (Selmouse.sm_x != 0)
Selmouse.sm_x--;
if (Selmouse.sm_x > Selmouse.sm_max_x)
Selmouse.sm_x = Selmouse.sm_max_x;
} else
Selmouse.sm_count_x++;
break;
case WSCONS_EVENT_MOUSE_DELTA_Y:
if (Selmouse.sm_count_y >= Selmouse.sm_slowdown_y) {
Selmouse.sm_count_y = 0;
if (evt.value < 0)
Selmouse.sm_y++;
else if (Selmouse.sm_y != 0)
Selmouse.sm_y--;
if (Selmouse.sm_y > Selmouse.sm_max_y)
Selmouse.sm_y = Selmouse.sm_max_y;
} else
Selmouse.sm_count_y++;
break;
case WSCONS_EVENT_MOUSE_DELTA_Z:
case WSCONS_EVENT_MOUSE_DELTA_W:
break;
case WSCONS_EVENT_MOUSE_ABSOLUTE_X:
if (!Selmouse.sm_mouse->m_doabs)
break;
Selmouse.sm_x
= ((evt.value - abs->minx) * (Selmouse.sm_max_x + 1))
/ (abs->maxx - abs->minx + 1);
break;
case WSCONS_EVENT_MOUSE_ABSOLUTE_Y:
if (!Selmouse.sm_mouse->m_doabs)
break;
Selmouse.sm_y
= ((evt.value - abs->miny) * (Selmouse.sm_max_y + 1))
/ (abs->maxy - abs->miny + 1);
break;
case WSCONS_EVENT_MOUSE_ABSOLUTE_Z:
case WSCONS_EVENT_MOUSE_ABSOLUTE_W:
break;
default:
log_warnx("unknown event");
}
if (Selmouse.sm_selecting)
selarea_show();
cursor_show();
} else if (IS_BUTTON_EVENT(evt.type)) {
switch (evt.type) {
case WSCONS_EVENT_MOUSE_UP:
if (evt.value == Selmouse.sm_but_select) {
selarea_end();
selarea_hide();
}
break;
case WSCONS_EVENT_MOUSE_DOWN:
if (evt.value == Selmouse.sm_but_select) {
selarea_start();
cursor_hide();
selarea_show();
} else if (evt.value == Selmouse.sm_but_paste) {
selarea_paste();
break;
}
break;
default:
log_warnx("unknown button event");
}
}
}
void
selection_wscons_event(struct wscons_event evt, int preclose)
{
switch (evt.type) {
case WSCONS_EVENT_SCREEN_SWITCH:
if (preclose) {
if (Selmouse.sm_selecting)
selarea_hide();
cursor_hide();
} else {
if (!Selmouse.sm_mouse->m_disabled)
open_tty(evt.value);
cursor_show();
if (Selmouse.sm_selecting)
selarea_show();
}
break;
}
}
void
selection_poll_timeout(void)
{
if (!Selmouse.sm_selecting)
cursor_hide();
}
static void
cursor_hide(void)
{
if (Selmouse.sm_visible) {
char_invert(Selmouse.sm_y, Selmouse.sm_x);
Selmouse.sm_visible = 0;
}
}
static void
cursor_show(void)
{
if (!Selmouse.sm_visible) {
char_invert(Selmouse.sm_y, Selmouse.sm_x);
Selmouse.sm_visible = 1;
}
}
static void
open_tty(int ttyno)
{
char buf[20];
struct winsize ws;
if (Selmouse.sm_ttyfd >= 0)
(void)close(Selmouse.sm_ttyfd);
(void)snprintf(buf, sizeof(buf), _PATH_TTYPREFIX "%d", ttyno);
Selmouse.sm_ttyfd = open(buf, O_RDONLY | O_NONBLOCK);
if (Selmouse.sm_ttyfd < 0)
log_warnx("cannot open %s", buf);
if (ioctl(Selmouse.sm_ttyfd, TIOCGWINSZ, &ws) < 0) {
log_warn("cannot get terminal size");
Selmouse.sm_max_y = 24;
Selmouse.sm_max_x = 79;
} else {
Selmouse.sm_max_y = ws.ws_row - 1;
Selmouse.sm_max_x = ws.ws_col - 1;
}
if (Selmouse.sm_x > Selmouse.sm_max_x)
Selmouse.sm_x = Selmouse.sm_max_x;
if (Selmouse.sm_y > Selmouse.sm_max_y)
Selmouse.sm_y = Selmouse.sm_max_y;
}
static void
char_invert(size_t row, size_t col)
{
int t;
struct wsdisplay_char ch;
ch.row = row;
ch.col = col;
if (ioctl(Selmouse.sm_ttyfd, WSDISPLAYIO_GETWSCHAR, &ch) == -1) {
log_warn("ioctl(WSDISPLAYIO_GETWSCHAR) failed");
return;
}
t = ch.foreground;
ch.foreground = ch.background;
ch.background = t;
if (ioctl(Selmouse.sm_ttyfd, WSDISPLAYIO_PUTWSCHAR, &ch) == -1)
log_warn("ioctl(WSDISPLAYIO_PUTWSCHAR) failed");
}
static void *
alloc_sel(size_t len)
{
void *ptr;
ptr = malloc(len);
if (ptr == NULL)
log_warn("cannot allocate memory for selection (%lu bytes)",
(unsigned long)len);
return ptr;
}
static char *
fill_buf(char *ptr, size_t row, size_t col, size_t end)
{
struct wsdisplay_char ch;
ch.row = row;
for (ch.col = col; ch.col < end; ch.col++) {
if (ioctl(Selmouse.sm_ttyfd, WSDISPLAYIO_GETWSCHAR,
&ch) == -1) {
log_warn("ioctl(WSDISPLAYIO_GETWSCHAR) failed");
*ptr++ = ' ';
} else {
*ptr++ = ch.letter;
}
}
return ptr;
}
static size_t
row_length(size_t row)
{
struct wsdisplay_char ch;
ch.col = Selmouse.sm_max_x;
ch.row = row;
do {
if (ioctl(Selmouse.sm_ttyfd, WSDISPLAYIO_GETWSCHAR, &ch) == -1)
log_warn("ioctl(WSDISPLAYIO_GETWSCHAR) failed");
ch.col--;
} while (isspace((unsigned char)ch.letter) && ch.col >= 0);
return ch.col + 2;
}
static void
selarea_copy_text(void)
{
char *ptr, *str;
size_t l, r;
ptr = NULL;
if (Selarea.sa_y1 == Selarea.sa_y2) {
l = row_length(Selarea.sa_y1);
if (Selarea.sa_x1 > l)
str = NULL;
else {
if (Selarea.sa_x2 > l)
Selarea.sa_x2 = l;
ptr = str =
alloc_sel(Selarea.sa_x2 - Selarea.sa_x1 + 1);
if (ptr == NULL)
return;
ptr = fill_buf(ptr, Selarea.sa_y1, Selarea.sa_x1,
Selarea.sa_x2);
*ptr = '\0';
}
} else {
ptr = str =
alloc_sel(Selarea.sa_endoff - Selarea.sa_startoff + 1);
if (ptr == NULL)
return;
l = row_length(Selarea.sa_y1);
if (Selarea.sa_x1 < l) {
ptr = fill_buf(ptr, Selarea.sa_y1, Selarea.sa_x1, l);
*ptr++ = '\r';
}
if ((Selarea.sa_y2 - Selarea.sa_y1) > 1) {
for (r = Selarea.sa_y1 + 1; r <= Selarea.sa_y2 - 1;
r++) {
ptr = fill_buf(ptr, r, 0, row_length(r));
*ptr++ = '\r';
}
}
l = row_length(Selarea.sa_y2);
if (Selarea.sa_x2 < l)
l = Selarea.sa_x2;
ptr = fill_buf(ptr, Selarea.sa_y2, 0, l);
*ptr = '\0';
}
if (Selarea.sa_buf != NULL) {
free(Selarea.sa_buf);
Selarea.sa_buf = NULL;
}
if (str != NULL) {
Selarea.sa_buf = str;
Selarea.sa_buflen = ptr - str;
}
}
static void
selarea_start(void)
{
if (Selarea.sa_buf != NULL) {
free(Selarea.sa_buf);
Selarea.sa_buf = NULL;
}
Selarea.sa_y1 = Selmouse.sm_y;
Selarea.sa_x1 = Selmouse.sm_x;
selarea_calculate();
Selmouse.sm_selecting = 1;
}
static void
selarea_end(void)
{
size_t i;
selarea_calculate();
if (Selarea.sa_x1 > Selarea.sa_x2) {
i = Selarea.sa_x2;
Selarea.sa_x2 = Selarea.sa_x1;
Selarea.sa_x1 = i;
}
if (Selarea.sa_y1 > Selarea.sa_y2) {
i = Selarea.sa_y2;
Selarea.sa_y2 = Selarea.sa_y1;
Selarea.sa_y1 = i;
}
selarea_copy_text();
Selmouse.sm_selecting = 0;
}
static void
selarea_calculate(void)
{
size_t i;
i = Selmouse.sm_max_x + 1;
Selarea.sa_y2 = Selmouse.sm_y;
Selarea.sa_x2 = Selmouse.sm_x;
Selarea.sa_startoff = Selarea.sa_y1 * i + Selarea.sa_x1;
Selarea.sa_endoff = Selarea.sa_y2 * i + Selarea.sa_x2;
if (Selarea.sa_startoff > Selarea.sa_endoff) {
i = Selarea.sa_endoff;
Selarea.sa_endoff = Selarea.sa_startoff;
Selarea.sa_startoff = i;
}
}
static void
selarea_getrowcol(size_t pos, size_t* row, size_t* col)
{
size_t xres = Selmouse.sm_max_x + 1;
*row = pos / xres;
*col = pos - (*row * xres);
}
static void
selarea_hide(void)
{
size_t i, row, col;
for (i = Selarea.sa_startoff; i <= Selarea.sa_endoff; i++) {
selarea_getrowcol(i, &row, &col);
char_invert(row, col);
}
}
static void
selarea_show(void)
{
size_t i, row, col;
selarea_calculate();
for (i = Selarea.sa_startoff; i <= Selarea.sa_endoff; i++) {
selarea_getrowcol(i, &row, &col);
char_invert(row, col);
}
}
static void
selarea_paste(void)
{
size_t i;
if (Selarea.sa_buf == NULL)
return;
for (i = 0; i < Selarea.sa_buflen; i++)
if (ioctl(Selmouse.sm_ttyfd, TIOCSTI,
&Selarea.sa_buf[i]) == -1)
log_warn("ioctl(TIOCSTI)");
}