2 * This module exports the functions:
4 * 'int set_selection(struct tiocl_selection __user *, struct tty_struct *)'
5 * 'void clear_selection(void)'
6 * 'int paste_selection(struct tty_struct *)'
7 * 'int sel_loadlut(char __user *)'
9 * Now that /dev/vcs exists, most of this can disappear again.
12 #include <linux/module.h>
13 #include <linux/tty.h>
14 #include <linux/sched.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
18 #include <linux/types.h>
20 #include <asm/uaccess.h>
22 #include <linux/kbd_kern.h>
23 #include <linux/vt_kern.h>
24 #include <linux/consolemap.h>
25 #include <linux/selection.h>
26 #include <linux/tiocl.h>
27 #include <linux/console.h>
28 #include <linux/tty_flip.h>
30 /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
31 #define isspace(c) ((c) == ' ')
33 extern void poke_blanked_console(void);
35 /* FIXME: all this needs locking */
36 /* Variables for selection control. */
37 /* Use a dynamic buffer, instead of static (Dec 1994) */
38 struct vc_data *sel_cons; /* must not be deallocated */
39 static int use_unicode;
40 static volatile int sel_start = -1; /* cleared by clear_selection */
42 static int sel_buffer_lth;
43 static char *sel_buffer;
44 static DEFINE_MUTEX(sel_lock);
46 /* clear_selection, highlight and highlight_pointer can be called
47 from interrupt (via scrollback/front) */
49 /* set reverse video on characters s-e of console with selection. */
50 static inline void highlight(const int s, const int e)
52 invert_screen(sel_cons, s, e-s+2, 1);
55 /* use complementary color to show the pointer */
56 static inline void highlight_pointer(const int where)
58 complement_pos(sel_cons, where);
64 return inverse_translate(sel_cons, screen_glyph(sel_cons, n),
69 * clear_selection - remove current selection
71 * Remove the current selection highlight, if any from the console
72 * holding the selection. The caller must hold the console lock.
74 void clear_selection(void)
76 highlight_pointer(-1); /* hide the pointer */
77 if (sel_start != -1) {
78 highlight(sel_start, sel_end);
83 bool vc_is_sel(struct vc_data *vc)
85 return vc == sel_cons;
89 * User settable table: what characters are to be considered alphabetic?
90 * 256 bits. Locked by the console lock.
92 static u32 inwordLut[8]={
93 0x00000000, /* control chars */
94 0x03FF0000, /* digits */
95 0x87FFFFFE, /* uppercase and '_' */
96 0x07FFFFFE, /* lowercase */
99 0xFF7FFFFF, /* latin-1 accented letters, not multiplication sign */
100 0xFF7FFFFF /* latin-1 accented letters, not division sign */
103 static inline int inword(const u16 c) {
104 return c > 0xff || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
108 * set loadlut - load the LUT table
111 * Load the LUT table from user space. The caller must hold the console
112 * lock. Make a temporary copy so a partial update doesn't make a mess.
114 int sel_loadlut(char __user *p)
117 if (copy_from_user(tmplut, (u32 __user *)(p+4), 32))
119 memcpy(inwordLut, tmplut, 32);
123 /* does screen address p correspond to character at LH/RH edge of screen? */
124 static inline int atedge(const int p, int size_row)
126 return (!(p % size_row) || !((p + 2) % size_row));
129 /* constrain v such that v <= u */
130 static inline unsigned short limit(const unsigned short v, const unsigned short u)
132 return (v > u) ? u : v;
135 /* stores the char in UTF8 and returns the number of bytes used (1-3) */
136 static int store_utf8(u16 c, char *p)
142 } else if (c < 0x800) {
143 /* 110***** 10****** */
144 p[0] = 0xc0 | (c >> 6);
145 p[1] = 0x80 | (c & 0x3f);
148 /* 1110**** 10****** 10****** */
149 p[0] = 0xe0 | (c >> 12);
150 p[1] = 0x80 | ((c >> 6) & 0x3f);
151 p[2] = 0x80 | (c & 0x3f);
157 * set_selection - set the current selection.
158 * @sel: user selection info
159 * @tty: the console tty
161 * Invoked by the ioctl handle for the vt layer.
163 * The entire selection process is managed under the console_lock. It's
164 * a lot under the lock but its hardly a performance path
166 static int __set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
168 struct vc_data *vc = vc_cons[fg_console].d;
169 int sel_mode, new_sel_start, new_sel_end, spc;
171 int i, ps, pe, multiplier;
175 poke_blanked_console();
177 { unsigned short xs, ys, xe, ye;
179 if (!access_ok(VERIFY_READ, sel, sizeof(*sel)))
181 __get_user(xs, &sel->xs);
182 __get_user(ys, &sel->ys);
183 __get_user(xe, &sel->xe);
184 __get_user(ye, &sel->ye);
185 __get_user(sel_mode, &sel->sel_mode);
186 xs--; ys--; xe--; ye--;
187 xs = limit(xs, vc->vc_cols - 1);
188 ys = limit(ys, vc->vc_rows - 1);
189 xe = limit(xe, vc->vc_cols - 1);
190 ye = limit(ye, vc->vc_rows - 1);
191 ps = ys * vc->vc_size_row + (xs << 1);
192 pe = ye * vc->vc_size_row + (xe << 1);
194 if (sel_mode == TIOCL_SELCLEAR) {
195 /* useful for screendump without selection highlights */
200 if (mouse_reporting() && (sel_mode & TIOCL_SELMOUSEREPORT)) {
201 mouse_report(tty, sel_mode & TIOCL_SELBUTTONMASK, xs, ys);
206 if (ps > pe) /* make sel_start <= sel_end */
213 if (sel_cons != vc_cons[fg_console].d) {
215 sel_cons = vc_cons[fg_console].d;
217 mode = vt_do_kdgkbmode(fg_console);
218 if (mode == K_UNICODE)
225 case TIOCL_SELCHAR: /* character-by-character selection */
229 case TIOCL_SELWORD: /* word-by-word selection */
230 spc = isspace(sel_pos(ps));
231 for (new_sel_start = ps; ; ps -= 2)
233 if ((spc && !isspace(sel_pos(ps))) ||
234 (!spc && !inword(sel_pos(ps))))
237 if (!(ps % vc->vc_size_row))
240 spc = isspace(sel_pos(pe));
241 for (new_sel_end = pe; ; pe += 2)
243 if ((spc && !isspace(sel_pos(pe))) ||
244 (!spc && !inword(sel_pos(pe))))
247 if (!((pe + 2) % vc->vc_size_row))
251 case TIOCL_SELLINE: /* line-by-line selection */
252 new_sel_start = ps - ps % vc->vc_size_row;
253 new_sel_end = pe + vc->vc_size_row
254 - pe % vc->vc_size_row - 2;
256 case TIOCL_SELPOINTER:
257 highlight_pointer(pe);
263 /* remove the pointer */
264 highlight_pointer(-1);
266 /* select to end of line if on trailing space */
267 if (new_sel_end > new_sel_start &&
268 !atedge(new_sel_end, vc->vc_size_row) &&
269 isspace(sel_pos(new_sel_end))) {
270 for (pe = new_sel_end + 2; ; pe += 2)
271 if (!isspace(sel_pos(pe)) ||
272 atedge(pe, vc->vc_size_row))
274 if (isspace(sel_pos(pe)))
277 if (sel_start == -1) /* no current selection */
278 highlight(new_sel_start, new_sel_end);
279 else if (new_sel_start == sel_start)
281 if (new_sel_end == sel_end) /* no action required */
283 else if (new_sel_end > sel_end) /* extend to right */
284 highlight(sel_end + 2, new_sel_end);
285 else /* contract from right */
286 highlight(new_sel_end + 2, sel_end);
288 else if (new_sel_end == sel_end)
290 if (new_sel_start < sel_start) /* extend to left */
291 highlight(new_sel_start, sel_start - 2);
292 else /* contract from left */
293 highlight(sel_start, new_sel_start - 2);
295 else /* some other case; start selection from scratch */
298 highlight(new_sel_start, new_sel_end);
300 sel_start = new_sel_start;
301 sel_end = new_sel_end;
303 /* Allocate a new buffer before freeing the old one ... */
304 multiplier = use_unicode ? 3 : 1; /* chars can take up to 3 bytes */
305 bp = kmalloc(((sel_end-sel_start)/2+1)*multiplier, GFP_KERNEL);
307 printk(KERN_WARNING "selection: kmalloc() failed\n");
315 for (i = sel_start; i <= sel_end; i += 2) {
318 bp += store_utf8(c, bp);
323 if (! ((i + 2) % vc->vc_size_row)) {
324 /* strip trailing blanks from line and add newline,
325 unless non-space at end of line. */
333 sel_buffer_lth = bp - sel_buffer;
338 int set_selection(const struct tiocl_selection __user *v, struct tty_struct *tty)
342 mutex_lock(&sel_lock);
344 ret = __set_selection(v, tty);
346 mutex_unlock(&sel_lock);
351 /* Insert the contents of the selection buffer into the
352 * queue of the tty associated with the current console.
353 * Invoked by ioctl().
355 * Locking: called without locks. Calls the ldisc wrongly with
358 int paste_selection(struct tty_struct *tty)
360 struct vc_data *vc = tty->driver_data;
363 struct tty_ldisc *ld;
364 DECLARE_WAITQUEUE(wait, current);
368 poke_blanked_console();
371 ld = tty_ldisc_ref_wait(tty);
373 return -EIO; /* ldisc was hung up */
374 tty_buffer_lock_exclusive(&vc->port);
376 add_wait_queue(&vc->paste_wait, &wait);
377 mutex_lock(&sel_lock);
378 while (sel_buffer && sel_buffer_lth > pasted) {
379 set_current_state(TASK_INTERRUPTIBLE);
380 if (signal_pending(current)) {
384 if (tty_throttled(tty)) {
385 mutex_unlock(&sel_lock);
387 mutex_lock(&sel_lock);
390 __set_current_state(TASK_RUNNING);
391 count = sel_buffer_lth - pasted;
392 count = tty_ldisc_receive_buf(ld, sel_buffer + pasted, NULL,
396 mutex_unlock(&sel_lock);
397 remove_wait_queue(&vc->paste_wait, &wait);
398 __set_current_state(TASK_RUNNING);
400 tty_buffer_unlock_exclusive(&vc->port);