2 * linux/drivers/char/selection.c
4 * This module exports the functions:
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 *)'
11 * Now that /dev/vcs exists, most of this can disappear again.
14 #include <linux/module.h>
15 #include <linux/tty.h>
16 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/types.h>
21 #include <asm/uaccess.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>
29 /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
30 #define isspace(c) ((c) == ' ')
32 extern void poke_blanked_console(void);
34 /* Variables for selection control. */
35 /* Use a dynamic buffer, instead of static (Dec 1994) */
36 struct vc_data *sel_cons; /* must not be disallocated */
37 static volatile int sel_start = -1; /* cleared by clear_selection */
39 static int sel_buffer_lth;
40 static char *sel_buffer;
42 /* clear_selection, highlight and highlight_pointer can be called
43 from interrupt (via scrollback/front) */
45 /* set reverse video on characters s-e of console with selection. */
47 highlight(const int s, const int e)
49 invert_screen(sel_cons, s, e-s+2, 1);
52 /* use complementary color to show the pointer */
54 highlight_pointer(const int where)
56 complement_pos(sel_cons, where);
62 return inverse_translate(sel_cons, screen_glyph(sel_cons, n));
65 /* remove the current selection highlight, if any,
66 from the console holding the selection. */
68 clear_selection(void) {
69 highlight_pointer(-1); /* hide the pointer */
70 if (sel_start != -1) {
71 highlight(sel_start, sel_end);
77 * User settable table: what characters are to be considered alphabetic?
80 static u32 inwordLut[8]={
81 0x00000000, /* control chars */
82 0x03FF0000, /* digits */
83 0x87FFFFFE, /* uppercase and '_' */
84 0x07FFFFFE, /* lowercase */
87 0xFF7FFFFF, /* latin-1 accented letters, not multiplication sign */
88 0xFF7FFFFF /* latin-1 accented letters, not division sign */
91 static inline int inword(const unsigned char c) {
92 return ( inwordLut[c>>5] >> (c & 0x1F) ) & 1;
95 /* set inwordLut contents. Invoked by ioctl(). */
96 int sel_loadlut(char __user *p)
98 return copy_from_user(inwordLut, (u32 __user *)(p+4), 32) ? -EFAULT : 0;
101 /* does screen address p correspond to character at LH/RH edge of screen? */
102 static inline int atedge(const int p, int size_row)
104 return (!(p % size_row) || !((p + 2) % size_row));
107 /* constrain v such that v <= u */
108 static inline unsigned short limit(const unsigned short v, const unsigned short u)
110 return (v > u) ? u : v;
113 /* set the current selection. Invoked by ioctl() or by kernel code. */
114 int set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
116 struct vc_data *vc = vc_cons[fg_console].d;
117 int sel_mode, new_sel_start, new_sel_end, spc;
121 poke_blanked_console();
123 { unsigned short xs, ys, xe, ye;
125 if (verify_area(VERIFY_READ, sel, sizeof(*sel)))
127 __get_user(xs, &sel->xs);
128 __get_user(ys, &sel->ys);
129 __get_user(xe, &sel->xe);
130 __get_user(ye, &sel->ye);
131 __get_user(sel_mode, &sel->sel_mode);
132 xs--; ys--; xe--; ye--;
133 xs = limit(xs, vc->vc_cols - 1);
134 ys = limit(ys, vc->vc_rows - 1);
135 xe = limit(xe, vc->vc_cols - 1);
136 ye = limit(ye, vc->vc_rows - 1);
137 ps = ys * vc->vc_size_row + (xs << 1);
138 pe = ye * vc->vc_size_row + (xe << 1);
140 if (sel_mode == TIOCL_SELCLEAR) {
141 /* useful for screendump without selection highlights */
146 if (mouse_reporting() && (sel_mode & TIOCL_SELMOUSEREPORT)) {
147 mouse_report(tty, sel_mode & TIOCL_SELBUTTONMASK, xs, ys);
152 if (ps > pe) /* make sel_start <= sel_end */
159 if (sel_cons != vc_cons[fg_console].d) {
161 sel_cons = vc_cons[fg_console].d;
166 case TIOCL_SELCHAR: /* character-by-character selection */
170 case TIOCL_SELWORD: /* word-by-word selection */
171 spc = isspace(sel_pos(ps));
172 for (new_sel_start = ps; ; ps -= 2)
174 if ((spc && !isspace(sel_pos(ps))) ||
175 (!spc && !inword(sel_pos(ps))))
178 if (!(ps % vc->vc_size_row))
181 spc = isspace(sel_pos(pe));
182 for (new_sel_end = pe; ; pe += 2)
184 if ((spc && !isspace(sel_pos(pe))) ||
185 (!spc && !inword(sel_pos(pe))))
188 if (!((pe + 2) % vc->vc_size_row))
192 case TIOCL_SELLINE: /* line-by-line selection */
193 new_sel_start = ps - ps % vc->vc_size_row;
194 new_sel_end = pe + vc->vc_size_row
195 - pe % vc->vc_size_row - 2;
197 case TIOCL_SELPOINTER:
198 highlight_pointer(pe);
204 /* remove the pointer */
205 highlight_pointer(-1);
207 /* select to end of line if on trailing space */
208 if (new_sel_end > new_sel_start &&
209 !atedge(new_sel_end, vc->vc_size_row) &&
210 isspace(sel_pos(new_sel_end))) {
211 for (pe = new_sel_end + 2; ; pe += 2)
212 if (!isspace(sel_pos(pe)) ||
213 atedge(pe, vc->vc_size_row))
215 if (isspace(sel_pos(pe)))
218 if (sel_start == -1) /* no current selection */
219 highlight(new_sel_start, new_sel_end);
220 else if (new_sel_start == sel_start)
222 if (new_sel_end == sel_end) /* no action required */
224 else if (new_sel_end > sel_end) /* extend to right */
225 highlight(sel_end + 2, new_sel_end);
226 else /* contract from right */
227 highlight(new_sel_end + 2, sel_end);
229 else if (new_sel_end == sel_end)
231 if (new_sel_start < sel_start) /* extend to left */
232 highlight(new_sel_start, sel_start - 2);
233 else /* contract from left */
234 highlight(sel_start, new_sel_start - 2);
236 else /* some other case; start selection from scratch */
239 highlight(new_sel_start, new_sel_end);
241 sel_start = new_sel_start;
242 sel_end = new_sel_end;
244 /* Allocate a new buffer before freeing the old one ... */
245 bp = kmalloc((sel_end-sel_start)/2+1, GFP_KERNEL);
247 printk(KERN_WARNING "selection: kmalloc() failed\n");
256 for (i = sel_start; i <= sel_end; i += 2) {
260 if (! ((i + 2) % vc->vc_size_row)) {
261 /* strip trailing blanks from line and add newline,
262 unless non-space at end of line. */
270 sel_buffer_lth = bp - sel_buffer;
274 /* Insert the contents of the selection buffer into the
275 * queue of the tty associated with the current console.
276 * Invoked by ioctl().
278 int paste_selection(struct tty_struct *tty)
280 struct vt_struct *vt = (struct vt_struct *) tty->driver_data;
281 int pasted = 0, count;
282 struct tty_ldisc *ld;
283 DECLARE_WAITQUEUE(wait, current);
285 acquire_console_sem();
286 poke_blanked_console();
287 release_console_sem();
289 ld = tty_ldisc_ref_wait(tty);
291 add_wait_queue(&vt->paste_wait, &wait);
292 while (sel_buffer && sel_buffer_lth > pasted) {
293 set_current_state(TASK_INTERRUPTIBLE);
294 if (test_bit(TTY_THROTTLED, &tty->flags)) {
298 count = sel_buffer_lth - pasted;
299 count = min(count, tty->ldisc.receive_room(tty));
300 tty->ldisc.receive_buf(tty, sel_buffer + pasted, NULL, count);
303 remove_wait_queue(&vt->paste_wait, &wait);
304 current->state = TASK_RUNNING;