| 1 |
t |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
#include <unistd.h> |
| 20 |
#include <fcntl.h> |
| 21 |
#include <sys/ioctl.h> |
| 22 |
#include <linux/tiocl.h> |
| 23 |
#include <stdint.h> |
| 24 |
#include <linux/kd.h> |
| 25 |
#include <errno.h> |
| 26 |
|
| 27 |
#include "consolation.h" |
| 28 |
|
| 29 |
static int |
| 30 |
check_mode(int fd) |
| 31 |
{ |
| 32 |
int mode; |
| 33 |
ioctl(fd, KDGETMODE, &mode); |
| 34 |
return mode==KD_TEXT; |
| 35 |
} |
| 36 |
|
| 37 |
void |
| 38 |
set_screen_size_and_mouse_reporting(void) |
| 39 |
{ |
| 40 |
struct winsize s; |
| 41 |
unsigned char request = MOUSE_REPORTING_OFF; |
| 42 |
int fd = open("/dev/tty0",O_RDONLY); |
| 43 |
if (fd == -1) |
| 44 |
{ |
| 45 |
perror("open /dev/tty0"); |
| 46 |
return; |
| 47 |
} |
| 48 |
if (ioctl(fd, TIOCGWINSZ, &s)) |
| 49 |
perror("TIOCGWINSZ"); |
| 50 |
else |
| 51 |
{ |
| 52 |
screen_width = s.ws_col; |
| 53 |
screen_height = s.ws_row; |
| 54 |
} |
| 55 |
if (do_mouse_reporting) |
| 56 |
{ |
| 57 |
request = TIOCL_GETMOUSEREPORTING; |
| 58 |
if (ioctl(fd, TIOCLINUX, &request)) |
| 59 |
{ |
| 60 |
perror("TIOCLINUX, TIOCL_GETMOUSEREPORTING"); |
| 61 |
request = MOUSE_REPORTING_OFF; |
| 62 |
} |
| 63 |
} |
| 64 |
close(fd); |
| 65 |
if (request >= MOUSE_REPORTING_MODE_COUNT) |
| 66 |
request = MOUSE_REPORTING_OFF; |
| 67 |
mouse_reporting = request; |
| 68 |
close(fd); |
| 69 |
} |
| 70 |
|
| 71 |
static void |
| 72 |
linux_selection(int xs, int ys, int xe, int ye, int sel_mode) |
| 73 |
{ |
| 74 |
int fd; |
| 75 |
struct { |
| 76 |
char argp[2]; |
| 77 |
struct tiocl_selection sel; |
| 78 |
} s; |
| 79 |
s.argp[0] = 0; |
| 80 |
s.argp[1] = TIOCL_SETSEL; |
| 81 |
s.sel.xs = xs; |
| 82 |
s.sel.ys = ys; |
| 83 |
s.sel.xe = xe<0 ? xs: xe; |
| 84 |
s.sel.ye = ye<0 ? ys: ye; |
| 85 |
s.sel.sel_mode = sel_mode; |
| 86 |
fd = open("/dev/tty0",O_RDONLY); |
| 87 |
if (check_mode(fd)) |
| 88 |
{ |
| 89 |
int err = ioctl(fd, TIOCLINUX, ((char*)&s)+1); |
| 90 |
if (err<0 && !(errno==EINVAL && (sel_mode&TIOCL_SELMOUSEREPORT))) |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
perror("selection: TIOCLINUX"); |
| 97 |
} |
| 98 |
close(fd); |
| 99 |
} |
| 100 |
|
| 101 |
void |
| 102 |
report_pointer(int x, int y, enum current_button button) |
| 103 |
{ |
| 104 |
linux_selection(x, y, x, y, TIOCL_SELCLEAR); |
| 105 |
linux_selection(x, y, x, y, TIOCL_SELMOUSEREPORT + button ); |
| 106 |
} |
| 107 |
|
| 108 |
void |
| 109 |
draw_pointer(int x, int y) |
| 110 |
{ |
| 111 |
linux_selection(x, y, x, y, TIOCL_SELPOINTER); |
| 112 |
} |
| 113 |
|
| 114 |
void |
| 115 |
select_region(int x, int y, int x2, int y2) |
| 116 |
{ |
| 117 |
linux_selection(x, y, x2, y2, TIOCL_SELCHAR); |
| 118 |
} |
| 119 |
|
| 120 |
void |
| 121 |
select_words(int x, int y, int x2, int y2) |
| 122 |
{ |
| 123 |
linux_selection(x, y, x2, y2, TIOCL_SELWORD); |
| 124 |
} |
| 125 |
|
| 126 |
void |
| 127 |
select_lines(int x, int y, int x2, int y2) |
| 128 |
{ |
| 129 |
linux_selection(x, y, x2, y2, TIOCL_SELLINE); |
| 130 |
} |
| 131 |
|
| 132 |
void paste(void) |
| 133 |
{ |
| 134 |
char subcode = TIOCL_PASTESEL; |
| 135 |
int fd = open("/dev/tty0",O_RDWR); |
| 136 |
if (check_mode(fd)) |
| 137 |
if (ioctl(fd, TIOCLINUX, &subcode)<0) |
| 138 |
perror("paste: TIOCLINUX"); |
| 139 |
close(fd); |
| 140 |
} |
| 141 |
|
| 142 |
void scroll(int sc) |
| 143 |
{ |
| 144 |
int fd; |
| 145 |
struct { |
| 146 |
char subcode[4]; |
| 147 |
int sc; |
| 148 |
} scr; |
| 149 |
scr.subcode[0] = TIOCL_SCROLLCONSOLE; |
| 150 |
scr.subcode[1] = 0; |
| 151 |
scr.subcode[2] = 0; |
| 152 |
scr.subcode[3] = 0; |
| 153 |
scr.sc = sc; |
| 154 |
fd = open("/dev/tty0",O_RDONLY); |
| 155 |
if (check_mode(fd)) |
| 156 |
if (ioctl(fd, TIOCLINUX, &scr)<0) |
| 157 |
perror("scroll: TIOCLINUX"); |
| 158 |
close(fd); |
| 159 |
} |
| 160 |
|
| 161 |
static int goodchar(unsigned char x) |
| 162 |
{ |
| 163 |
return x >= 0x20 && x < 0x7f; |
| 164 |
} |
| 165 |
|
| 166 |
void set_lut(const char *def) |
| 167 |
{ |
| 168 |
int fd; |
| 169 |
struct { |
| 170 |
char subcode; |
| 171 |
char padding[3]; |
| 172 |
uint32_t lut[8]; |
| 173 |
} l = { TIOCL_SELLOADLUT, { 0, 0, 0 }, { |
| 174 |
0x00000000, |
| 175 |
0x03FFE000, |
| 176 |
0x87FFFFFE, |
| 177 |
0x07FFFFFE, |
| 178 |
0x00000000, |
| 179 |
0x00000000, |
| 180 |
0xFF7FFFFF, |
| 181 |
0xFF7FFFFF |
| 182 |
} |
| 183 |
}; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
if (def) |
| 188 |
{ |
| 189 |
l.lut[1] = l.lut[2] = l.lut[3] = 0; |
| 190 |
while (*def) { |
| 191 |
char c = *def++; |
| 192 |
if (!goodchar(c)) |
| 193 |
continue; |
| 194 |
if (*def == '-' && goodchar(def[1])) { |
| 195 |
++def; |
| 196 |
for (; c <= *def; ++c) |
| 197 |
l.lut[c >> 5] |= 1 << (uint32_t)(c & 31); |
| 198 |
++def; |
| 199 |
} |
| 200 |
else |
| 201 |
l.lut[c >> 5] |= 1 << (uint32_t)(c & 31); |
| 202 |
} |
| 203 |
} |
| 204 |
fd = open("/dev/tty0",O_RDWR); |
| 205 |
if (check_mode(fd)) |
| 206 |
if (ioctl(fd, TIOCLINUX, &l)<0) |
| 207 |
perror("set_lut: TIOCLINUX"); |
| 208 |
close(fd); |
| 209 |
} |