blob: 6e79340d732f8e184de93cb918853ab4950ef452 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/char/selection.c
3 *
4 * This module exports the functions:
5 *
6 * 'int set_selection(struct tiocl_selection __user *, struct tty_struct *)'
7 * 'void clear_selection(void)'
8 * 'int paste_selection(struct tty_struct *)'
9 * 'int sel_loadlut(char __user *)'
10 *
11 * Now that /dev/vcs exists, most of this can disappear again.
12 */
13
14#include <linux/module.h>
15#include <linux/tty.h>
16#include <linux/sched.h>
17#include <linux/mm.h>
18#include <linux/slab.h>
19#include <linux/types.h>
20
21#include <asm/uaccess.h>
22
Jan Engelhardt759448f2007-07-15 23:40:40 -070023#include <linux/kbd_kern.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/vt_kern.h>
25#include <linux/consolemap.h>
26#include <linux/selection.h>
27#include <linux/tiocl.h>
28#include <linux/console.h>
Alan Coxe33ac1c2010-06-01 22:52:54 +020029#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
32#define isspace(c) ((c) == ' ')
33
34extern void poke_blanked_console(void);
35
36/* Variables for selection control. */
37/* Use a dynamic buffer, instead of static (Dec 1994) */
Alan Coxca9bda02006-09-29 02:00:03 -070038struct vc_data *sel_cons; /* must not be deallocated */
Jan Engelhardt759448f2007-07-15 23:40:40 -070039static int use_unicode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static volatile int sel_start = -1; /* cleared by clear_selection */
41static int sel_end;
42static int sel_buffer_lth;
43static char *sel_buffer;
44
45/* clear_selection, highlight and highlight_pointer can be called
46 from interrupt (via scrollback/front) */
47
48/* set reverse video on characters s-e of console with selection. */
49static inline void highlight(const int s, const int e)
50{
51 invert_screen(sel_cons, s, e-s+2, 1);
52}
53
54/* use complementary color to show the pointer */
55static inline void highlight_pointer(const int where)
56{
57 complement_pos(sel_cons, where);
58}
59
Jan Engelhardt759448f2007-07-15 23:40:40 -070060static u16
Linus Torvalds1da177e2005-04-16 15:20:36 -070061sel_pos(int n)
62{
Jan Engelhardt759448f2007-07-15 23:40:40 -070063 return inverse_translate(sel_cons, screen_glyph(sel_cons, n),
64 use_unicode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67/* remove the current selection highlight, if any,
68 from the console holding the selection. */
69void
70clear_selection(void) {
71 highlight_pointer(-1); /* hide the pointer */
72 if (sel_start != -1) {
73 highlight(sel_start, sel_end);
74 sel_start = -1;
75 }
76}
77
78/*
79 * User settable table: what characters are to be considered alphabetic?
80 * 256 bits
81 */
82static u32 inwordLut[8]={
83 0x00000000, /* control chars */
84 0x03FF0000, /* digits */
85 0x87FFFFFE, /* uppercase and '_' */
86 0x07FFFFFE, /* lowercase */
87 0x00000000,
88 0x00000000,
89 0xFF7FFFFF, /* latin-1 accented letters, not multiplication sign */
90 0xFF7FFFFF /* latin-1 accented letters, not division sign */
91};
92
Jan Engelhardt759448f2007-07-15 23:40:40 -070093static inline int inword(const u16 c) {
94 return c > 0xff || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97/* set inwordLut contents. Invoked by ioctl(). */
98int sel_loadlut(char __user *p)
99{
100 return copy_from_user(inwordLut, (u32 __user *)(p+4), 32) ? -EFAULT : 0;
101}
102
103/* does screen address p correspond to character at LH/RH edge of screen? */
104static inline int atedge(const int p, int size_row)
105{
106 return (!(p % size_row) || !((p + 2) % size_row));
107}
108
109/* constrain v such that v <= u */
110static inline unsigned short limit(const unsigned short v, const unsigned short u)
111{
112 return (v > u) ? u : v;
113}
114
Jan Engelhardt759448f2007-07-15 23:40:40 -0700115/* stores the char in UTF8 and returns the number of bytes used (1-3) */
116static int store_utf8(u16 c, char *p)
117{
118 if (c < 0x80) {
119 /* 0******* */
120 p[0] = c;
121 return 1;
122 } else if (c < 0x800) {
123 /* 110***** 10****** */
124 p[0] = 0xc0 | (c >> 6);
125 p[1] = 0x80 | (c & 0x3f);
126 return 2;
127 } else {
128 /* 1110**** 10****** 10****** */
129 p[0] = 0xe0 | (c >> 12);
130 p[1] = 0x80 | ((c >> 6) & 0x3f);
131 p[2] = 0x80 | (c & 0x3f);
132 return 3;
133 }
134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/* set the current selection. Invoked by ioctl() or by kernel code. */
137int set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
138{
139 struct vc_data *vc = vc_cons[fg_console].d;
140 int sel_mode, new_sel_start, new_sel_end, spc;
141 char *bp, *obp;
Jan Engelhardt759448f2007-07-15 23:40:40 -0700142 int i, ps, pe, multiplier;
143 u16 c;
144 struct kbd_struct *kbd = kbd_table + fg_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 poke_blanked_console();
147
148 { unsigned short xs, ys, xe, ye;
149
150 if (!access_ok(VERIFY_READ, sel, sizeof(*sel)))
151 return -EFAULT;
152 __get_user(xs, &sel->xs);
153 __get_user(ys, &sel->ys);
154 __get_user(xe, &sel->xe);
155 __get_user(ye, &sel->ye);
156 __get_user(sel_mode, &sel->sel_mode);
157 xs--; ys--; xe--; ye--;
158 xs = limit(xs, vc->vc_cols - 1);
159 ys = limit(ys, vc->vc_rows - 1);
160 xe = limit(xe, vc->vc_cols - 1);
161 ye = limit(ye, vc->vc_rows - 1);
162 ps = ys * vc->vc_size_row + (xs << 1);
163 pe = ye * vc->vc_size_row + (xe << 1);
164
165 if (sel_mode == TIOCL_SELCLEAR) {
166 /* useful for screendump without selection highlights */
167 clear_selection();
168 return 0;
169 }
170
171 if (mouse_reporting() && (sel_mode & TIOCL_SELMOUSEREPORT)) {
172 mouse_report(tty, sel_mode & TIOCL_SELBUTTONMASK, xs, ys);
173 return 0;
174 }
175 }
176
177 if (ps > pe) /* make sel_start <= sel_end */
178 {
179 int tmp = ps;
180 ps = pe;
181 pe = tmp;
182 }
183
184 if (sel_cons != vc_cons[fg_console].d) {
185 clear_selection();
186 sel_cons = vc_cons[fg_console].d;
187 }
Jan Engelhardt759448f2007-07-15 23:40:40 -0700188 use_unicode = kbd && kbd->kbdmode == VC_UNICODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 switch (sel_mode)
191 {
192 case TIOCL_SELCHAR: /* character-by-character selection */
193 new_sel_start = ps;
194 new_sel_end = pe;
195 break;
196 case TIOCL_SELWORD: /* word-by-word selection */
197 spc = isspace(sel_pos(ps));
198 for (new_sel_start = ps; ; ps -= 2)
199 {
200 if ((spc && !isspace(sel_pos(ps))) ||
201 (!spc && !inword(sel_pos(ps))))
202 break;
203 new_sel_start = ps;
204 if (!(ps % vc->vc_size_row))
205 break;
206 }
207 spc = isspace(sel_pos(pe));
208 for (new_sel_end = pe; ; pe += 2)
209 {
210 if ((spc && !isspace(sel_pos(pe))) ||
211 (!spc && !inword(sel_pos(pe))))
212 break;
213 new_sel_end = pe;
214 if (!((pe + 2) % vc->vc_size_row))
215 break;
216 }
217 break;
218 case TIOCL_SELLINE: /* line-by-line selection */
219 new_sel_start = ps - ps % vc->vc_size_row;
220 new_sel_end = pe + vc->vc_size_row
221 - pe % vc->vc_size_row - 2;
222 break;
223 case TIOCL_SELPOINTER:
224 highlight_pointer(pe);
225 return 0;
226 default:
227 return -EINVAL;
228 }
229
230 /* remove the pointer */
231 highlight_pointer(-1);
232
233 /* select to end of line if on trailing space */
234 if (new_sel_end > new_sel_start &&
235 !atedge(new_sel_end, vc->vc_size_row) &&
236 isspace(sel_pos(new_sel_end))) {
237 for (pe = new_sel_end + 2; ; pe += 2)
238 if (!isspace(sel_pos(pe)) ||
239 atedge(pe, vc->vc_size_row))
240 break;
241 if (isspace(sel_pos(pe)))
242 new_sel_end = pe;
243 }
244 if (sel_start == -1) /* no current selection */
245 highlight(new_sel_start, new_sel_end);
246 else if (new_sel_start == sel_start)
247 {
248 if (new_sel_end == sel_end) /* no action required */
249 return 0;
250 else if (new_sel_end > sel_end) /* extend to right */
251 highlight(sel_end + 2, new_sel_end);
252 else /* contract from right */
253 highlight(new_sel_end + 2, sel_end);
254 }
255 else if (new_sel_end == sel_end)
256 {
257 if (new_sel_start < sel_start) /* extend to left */
258 highlight(new_sel_start, sel_start - 2);
259 else /* contract from left */
260 highlight(sel_start, new_sel_start - 2);
261 }
262 else /* some other case; start selection from scratch */
263 {
264 clear_selection();
265 highlight(new_sel_start, new_sel_end);
266 }
267 sel_start = new_sel_start;
268 sel_end = new_sel_end;
269
270 /* Allocate a new buffer before freeing the old one ... */
Jan Engelhardt759448f2007-07-15 23:40:40 -0700271 multiplier = use_unicode ? 3 : 1; /* chars can take up to 3 bytes */
Mikulas Patocka878b8612009-01-30 15:27:14 -0500272 bp = kmalloc(((sel_end-sel_start)/2+1)*multiplier, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (!bp) {
274 printk(KERN_WARNING "selection: kmalloc() failed\n");
275 clear_selection();
276 return -ENOMEM;
277 }
Jesper Juhl735d5662005-11-07 01:01:29 -0800278 kfree(sel_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 sel_buffer = bp;
280
281 obp = bp;
282 for (i = sel_start; i <= sel_end; i += 2) {
Jan Engelhardt759448f2007-07-15 23:40:40 -0700283 c = sel_pos(i);
284 if (use_unicode)
285 bp += store_utf8(c, bp);
286 else
287 *bp++ = c;
288 if (!isspace(c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 obp = bp;
290 if (! ((i + 2) % vc->vc_size_row)) {
291 /* strip trailing blanks from line and add newline,
292 unless non-space at end of line. */
293 if (obp != bp) {
294 bp = obp;
295 *bp++ = '\r';
296 }
297 obp = bp;
298 }
299 }
300 sel_buffer_lth = bp - sel_buffer;
301 return 0;
302}
303
304/* Insert the contents of the selection buffer into the
305 * queue of the tty associated with the current console.
306 * Invoked by ioctl().
307 */
308int paste_selection(struct tty_struct *tty)
309{
Alan Coxc9f19e92009-01-02 13:47:26 +0000310 struct vc_data *vc = tty->driver_data;
Alan Cox33f0f882006-01-09 20:54:13 -0800311 int pasted = 0;
312 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct tty_ldisc *ld;
314 DECLARE_WAITQUEUE(wait, current);
315
Alan Coxe33ac1c2010-06-01 22:52:54 +0200316 lock_kernel();
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 acquire_console_sem();
319 poke_blanked_console();
320 release_console_sem();
321
322 ld = tty_ldisc_ref_wait(tty);
323
324 add_wait_queue(&vc->paste_wait, &wait);
325 while (sel_buffer && sel_buffer_lth > pasted) {
326 set_current_state(TASK_INTERRUPTIBLE);
327 if (test_bit(TTY_THROTTLED, &tty->flags)) {
328 schedule();
329 continue;
330 }
331 count = sel_buffer_lth - pasted;
Alan Cox33f0f882006-01-09 20:54:13 -0800332 count = min(count, tty->receive_room);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100333 tty->ldisc->ops->receive_buf(tty, sel_buffer + pasted,
Alan Coxa352def2008-07-16 21:53:12 +0100334 NULL, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 pasted += count;
336 }
337 remove_wait_queue(&vc->paste_wait, &wait);
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -0700338 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 tty_ldisc_deref(ld);
Alan Coxe33ac1c2010-06-01 22:52:54 +0200341 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return 0;
343}