Debian Codegraph @

src/selection.c

raw symbols SCIP
1 /* Copyright © 2016 Bill Allombert
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 Check the License for details. You should have received a copy of it, along
14 with the package; see the file 'COPYING'. If not, write to the Free Software
15 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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]; /*Force struct alignment*/
77 struct tiocl_selection sel;
78 } s;
79 s.argp[0] = 0; /* unused */
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 /* The kernel return EINVAL for TIOCL_SELMOUSEREPORT when
92 TIOCL_GETMOUSEREPORTING reports 0. Unfortunately this cannot be
93 checked without race conditions, so it is simpler to ignore the
94 error.
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, /* control chars */
175 0x03FFE000, /* digits and "-./" */
176 0x87FFFFFE, /* uppercase and '_' */
177 0x07FFFFFE, /* lowercase */
178 0x00000000,
179 0x00000000,
180 0xFF7FFFFF, /* latin-1 accented letters, not multiplication sign */
181 0xFF7FFFFF /* latin-1 accented letters, not division sign */
182 }
183 }; /* all of Unicode above U+00FF is considered "word" chars, even
184 frames and the likes */
185
186 /* we allow changing only U+0020..U+7E */
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 }