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/kbd_kern.h>
24 #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>
29 #include <linux/smp_lock.h>
31 /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
32 #define isspace(c) ((c) == ' ')
34 extern void poke_blanked_console(void);
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;
45 /* clear_selection, highlight and highlight_pointer can be called
46 from interrupt (via scrollback/front) */
48 /* set reverse video on characters s-e of console with selection. */
49 static inline void highlight(const int s, const int e)
51 invert_screen(sel_cons, s, e-s+2, 1);
54 /* use complementary color to show the pointer */
55 static inline void highlight_pointer(const int where)
57 complement_pos(sel_cons, where);
63 return inverse_translate(sel_cons, screen_glyph(sel_cons, n),
67 /* remove the current selection highlight, if any,
68 from the console holding the selection. */
70 clear_selection(void) {
71 highlight_pointer(-1); /* hide the pointer */
72 if (sel_start != -1) {
73 highlight(sel_start, sel_end);
79 * User settable table: what characters are to be considered alphabetic?
82 static u32 inwordLut[8]={
83 0x00000000, /* control chars */
84 0x03FF0000, /* digits */
85 0x87FFFFFE, /* uppercase and '_' */
86 0x07FFFFFE, /* lowercase */
89 0xFF7FFFFF, /* latin-1 accented letters, not multiplication sign */
90 0xFF7FFFFF /* latin-1 accented letters, not division sign */
93 static inline int inword(const u16 c) {
94 return c > 0xff || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
97 /* set inwordLut contents. Invoked by ioctl(). */
98 int sel_loadlut(char __user *p)
100 return copy_from_user(inwordLut, (u32 __user *)(p+4), 32) ? -EFAULT : 0;
103 /* does screen address p correspond to character at LH/RH edge of screen? */
104 static inline int atedge(const int p, int size_row)
106 return (!(p % size_row) || !((p + 2) % size_row));
109 /* constrain v such that v <= u */
110 static inline unsigned short limit(const unsigned short v, const unsigned short u)
112 return (v > u) ? u : v;
115 /* stores the char in UTF8 and returns the number of bytes used (1-3) */
116 static int store_utf8(u16 c, char *p)
122 } else if (c < 0x800) {
123 /* 110***** 10****** */
124 p[0] = 0xc0 | (c >> 6);
125 p[1] = 0x80 | (c & 0x3f);
128 /* 1110**** 10****** 10****** */
129 p[0] = 0xe0 | (c >> 12);
130 p[1] = 0x80 | ((c >> 6) & 0x3f);
131 p[2] = 0x80 | (c & 0x3f);
136 /* set the current selection. Invoked by ioctl() or by kernel code. */
137 int set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
139 struct vc_data *vc = vc_cons[fg_console].d;
140 int sel_mode, new_sel_start, new_sel_end, spc;
142 int i, ps, pe, multiplier;
144 struct kbd_struct *kbd = kbd_table + fg_console;
146 poke_blanked_console();
148 { unsigned short xs, ys, xe, ye;
150 if (!access_ok(VERIFY_READ, sel, sizeof(*sel)))
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);
165 if (sel_mode == TIOCL_SELCLEAR) {
166 /* useful for screendump without selection highlights */
171 if (mouse_reporting() && (sel_mode & TIOCL_SELMOUSEREPORT)) {
172 mouse_report(tty, sel_mode & TIOCL_SELBUTTONMASK, xs, ys);
177 if (ps > pe) /* make sel_start <= sel_end */
184 if (sel_cons != vc_cons[fg_console].d) {
186 sel_cons = vc_cons[fg_console].d;
188 use_unicode = kbd && kbd->kbdmode == VC_UNICODE;
192 case TIOCL_SELCHAR: /* character-by-character selection */
196 case TIOCL_SELWORD: /* word-by-word selection */
197 spc = isspace(sel_pos(ps));
198 for (new_sel_start = ps; ; ps -= 2)
200 if ((spc && !isspace(sel_pos(ps))) ||
201 (!spc && !inword(sel_pos(ps))))
204 if (!(ps % vc->vc_size_row))
207 spc = isspace(sel_pos(pe));
208 for (new_sel_end = pe; ; pe += 2)
210 if ((spc && !isspace(sel_pos(pe))) ||
211 (!spc && !inword(sel_pos(pe))))
214 if (!((pe + 2) % vc->vc_size_row))
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;
223 case TIOCL_SELPOINTER:
224 highlight_pointer(pe);
230 /* remove the pointer */
231 highlight_pointer(-1);
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))
241 if (isspace(sel_pos(pe)))
244 if (sel_start == -1) /* no current selection */
245 highlight(new_sel_start, new_sel_end);
246 else if (new_sel_start == sel_start)
248 if (new_sel_end == sel_end) /* no action required */
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);
255 else if (new_sel_end == sel_end)
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);
262 else /* some other case; start selection from scratch */
265 highlight(new_sel_start, new_sel_end);
267 sel_start = new_sel_start;
268 sel_end = new_sel_end;
270 /* Allocate a new buffer before freeing the old one ... */
271 multiplier = use_unicode ? 3 : 1; /* chars can take up to 3 bytes */
272 bp = kmalloc(((sel_end-sel_start)/2+1)*multiplier, GFP_KERNEL);
274 printk(KERN_WARNING "selection: kmalloc() failed\n");
282 for (i = sel_start; i <= sel_end; i += 2) {
285 bp += store_utf8(c, 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. */
300 sel_buffer_lth = bp - sel_buffer;
304 /* Insert the contents of the selection buffer into the
305 * queue of the tty associated with the current console.
306 * Invoked by ioctl().
308 int paste_selection(struct tty_struct *tty)
310 struct vc_data *vc = tty->driver_data;
313 struct tty_ldisc *ld;
314 DECLARE_WAITQUEUE(wait, current);
316 /* always called with BTM from vt_ioctl */
317 WARN_ON(!tty_locked());
319 acquire_console_sem();
320 poke_blanked_console();
321 release_console_sem();
323 ld = tty_ldisc_ref(tty);
326 ld = tty_ldisc_ref_wait(tty);
330 add_wait_queue(&vc->paste_wait, &wait);
331 while (sel_buffer && sel_buffer_lth > pasted) {
332 set_current_state(TASK_INTERRUPTIBLE);
333 if (test_bit(TTY_THROTTLED, &tty->flags)) {
337 count = sel_buffer_lth - pasted;
338 count = min(count, tty->receive_room);
339 tty->ldisc->ops->receive_buf(tty, sel_buffer + pasted,
343 remove_wait_queue(&vc->paste_wait, &wait);
344 __set_current_state(TASK_RUNNING);