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. */
46 static inline void highlight(const int s, const int e)
48 invert_screen(sel_cons, s, e-s+2, 1);
51 /* use complementary color to show the pointer */
52 static inline void highlight_pointer(const int where)
54 complement_pos(sel_cons, where);
60 return inverse_translate(sel_cons, screen_glyph(sel_cons, n));
63 /* remove the current selection highlight, if any,
64 from the console holding the selection. */
66 clear_selection(void) {
67 highlight_pointer(-1); /* hide the pointer */
68 if (sel_start != -1) {
69 highlight(sel_start, sel_end);
75 * User settable table: what characters are to be considered alphabetic?
78 static u32 inwordLut[8]={
79 0x00000000, /* control chars */
80 0x03FF0000, /* digits */
81 0x87FFFFFE, /* uppercase and '_' */
82 0x07FFFFFE, /* lowercase */
85 0xFF7FFFFF, /* latin-1 accented letters, not multiplication sign */
86 0xFF7FFFFF /* latin-1 accented letters, not division sign */
89 static inline int inword(const unsigned char c) {
90 return ( inwordLut[c>>5] >> (c & 0x1F) ) & 1;
93 /* set inwordLut contents. Invoked by ioctl(). */
94 int sel_loadlut(char __user *p)
96 return copy_from_user(inwordLut, (u32 __user *)(p+4), 32) ? -EFAULT : 0;
99 /* does screen address p correspond to character at LH/RH edge of screen? */
100 static inline int atedge(const int p, int size_row)
102 return (!(p % size_row) || !((p + 2) % size_row));
105 /* constrain v such that v <= u */
106 static inline unsigned short limit(const unsigned short v, const unsigned short u)
108 return (v > u) ? u : v;
111 /* set the current selection. Invoked by ioctl() or by kernel code. */
112 int set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
114 struct vc_data *vc = vc_cons[fg_console].d;
115 int sel_mode, new_sel_start, new_sel_end, spc;
119 poke_blanked_console();
121 { unsigned short xs, ys, xe, ye;
123 if (!access_ok(VERIFY_READ, sel, sizeof(*sel)))
125 __get_user(xs, &sel->xs);
126 __get_user(ys, &sel->ys);
127 __get_user(xe, &sel->xe);
128 __get_user(ye, &sel->ye);
129 __get_user(sel_mode, &sel->sel_mode);
130 xs--; ys--; xe--; ye--;
131 xs = limit(xs, vc->vc_cols - 1);
132 ys = limit(ys, vc->vc_rows - 1);
133 xe = limit(xe, vc->vc_cols - 1);
134 ye = limit(ye, vc->vc_rows - 1);
135 ps = ys * vc->vc_size_row + (xs << 1);
136 pe = ye * vc->vc_size_row + (xe << 1);
138 if (sel_mode == TIOCL_SELCLEAR) {
139 /* useful for screendump without selection highlights */
144 if (mouse_reporting() && (sel_mode & TIOCL_SELMOUSEREPORT)) {
145 mouse_report(tty, sel_mode & TIOCL_SELBUTTONMASK, xs, ys);
150 if (ps > pe) /* make sel_start <= sel_end */
157 if (sel_cons != vc_cons[fg_console].d) {
159 sel_cons = vc_cons[fg_console].d;
164 case TIOCL_SELCHAR: /* character-by-character selection */
168 case TIOCL_SELWORD: /* word-by-word selection */
169 spc = isspace(sel_pos(ps));
170 for (new_sel_start = ps; ; ps -= 2)
172 if ((spc && !isspace(sel_pos(ps))) ||
173 (!spc && !inword(sel_pos(ps))))
176 if (!(ps % vc->vc_size_row))
179 spc = isspace(sel_pos(pe));
180 for (new_sel_end = pe; ; pe += 2)
182 if ((spc && !isspace(sel_pos(pe))) ||
183 (!spc && !inword(sel_pos(pe))))
186 if (!((pe + 2) % vc->vc_size_row))
190 case TIOCL_SELLINE: /* line-by-line selection */
191 new_sel_start = ps - ps % vc->vc_size_row;
192 new_sel_end = pe + vc->vc_size_row
193 - pe % vc->vc_size_row - 2;
195 case TIOCL_SELPOINTER:
196 highlight_pointer(pe);
202 /* remove the pointer */
203 highlight_pointer(-1);
205 /* select to end of line if on trailing space */
206 if (new_sel_end > new_sel_start &&
207 !atedge(new_sel_end, vc->vc_size_row) &&
208 isspace(sel_pos(new_sel_end))) {
209 for (pe = new_sel_end + 2; ; pe += 2)
210 if (!isspace(sel_pos(pe)) ||
211 atedge(pe, vc->vc_size_row))
213 if (isspace(sel_pos(pe)))
216 if (sel_start == -1) /* no current selection */
217 highlight(new_sel_start, new_sel_end);
218 else if (new_sel_start == sel_start)
220 if (new_sel_end == sel_end) /* no action required */
222 else if (new_sel_end > sel_end) /* extend to right */
223 highlight(sel_end + 2, new_sel_end);
224 else /* contract from right */
225 highlight(new_sel_end + 2, sel_end);
227 else if (new_sel_end == sel_end)
229 if (new_sel_start < sel_start) /* extend to left */
230 highlight(new_sel_start, sel_start - 2);
231 else /* contract from left */
232 highlight(sel_start, new_sel_start - 2);
234 else /* some other case; start selection from scratch */
237 highlight(new_sel_start, new_sel_end);
239 sel_start = new_sel_start;
240 sel_end = new_sel_end;
242 /* Allocate a new buffer before freeing the old one ... */
243 bp = kmalloc((sel_end-sel_start)/2+1, GFP_KERNEL);
245 printk(KERN_WARNING "selection: kmalloc() failed\n");
253 for (i = sel_start; i <= sel_end; i += 2) {
257 if (! ((i + 2) % vc->vc_size_row)) {
258 /* strip trailing blanks from line and add newline,
259 unless non-space at end of line. */
267 sel_buffer_lth = bp - sel_buffer;
271 /* Insert the contents of the selection buffer into the
272 * queue of the tty associated with the current console.
273 * Invoked by ioctl().
275 int paste_selection(struct tty_struct *tty)
277 struct vc_data *vc = (struct vc_data *)tty->driver_data;
278 int pasted = 0, count;
279 struct tty_ldisc *ld;
280 DECLARE_WAITQUEUE(wait, current);
282 acquire_console_sem();
283 poke_blanked_console();
284 release_console_sem();
286 ld = tty_ldisc_ref_wait(tty);
288 add_wait_queue(&vc->paste_wait, &wait);
289 while (sel_buffer && sel_buffer_lth > pasted) {
290 set_current_state(TASK_INTERRUPTIBLE);
291 if (test_bit(TTY_THROTTLED, &tty->flags)) {
295 count = sel_buffer_lth - pasted;
296 count = min(count, tty->ldisc.receive_room(tty));
297 tty->ldisc.receive_buf(tty, sel_buffer + pasted, NULL, count);
300 remove_wait_queue(&vc->paste_wait, &wait);
301 current->state = TASK_RUNNING;