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>
30 #define MIN(a,b) ((a) < (b) ? (a) : (b))
33 /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
34 #define isspace(c) ((c) == ' ')
36 extern void poke_blanked_console(void);
38 /* Variables for selection control. */
39 /* Use a dynamic buffer, instead of static (Dec 1994) */
40 int sel_cons; /* must not be disallocated */
41 static volatile int sel_start = -1; /* cleared by clear_selection */
43 static int sel_buffer_lth;
44 static char *sel_buffer;
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. */
51 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 */
57 highlight_pointer(const int where) {
58 complement_pos(sel_cons, where);
64 return inverse_translate(vc_cons[sel_cons].d, 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 unsigned char c) {
94 return ( 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 /* set the current selection. Invoked by ioctl() or by kernel code. */
116 int set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
118 int sel_mode, new_sel_start, new_sel_end, spc;
121 unsigned int currcons = fg_console;
123 poke_blanked_console();
125 { unsigned short xs, ys, xe, ye;
127 if (verify_area(VERIFY_READ, sel, sizeof(*sel)))
129 __get_user(xs, &sel->xs);
130 __get_user(ys, &sel->ys);
131 __get_user(xe, &sel->xe);
132 __get_user(ye, &sel->ye);
133 __get_user(sel_mode, &sel->sel_mode);
134 xs--; ys--; xe--; ye--;
135 xs = limit(xs, video_num_columns - 1);
136 ys = limit(ys, video_num_lines - 1);
137 xe = limit(xe, video_num_columns - 1);
138 ye = limit(ye, video_num_lines - 1);
139 ps = ys * video_size_row + (xs << 1);
140 pe = ye * video_size_row + (xe << 1);
142 if (sel_mode == TIOCL_SELCLEAR) {
143 /* useful for screendump without selection highlights */
148 if (mouse_reporting() && (sel_mode & TIOCL_SELMOUSEREPORT)) {
149 mouse_report(tty, sel_mode & TIOCL_SELBUTTONMASK, xs, ys);
154 if (ps > pe) /* make sel_start <= sel_end */
161 if (sel_cons != fg_console) {
163 sel_cons = fg_console;
168 case TIOCL_SELCHAR: /* character-by-character selection */
172 case TIOCL_SELWORD: /* word-by-word selection */
173 spc = isspace(sel_pos(ps));
174 for (new_sel_start = ps; ; ps -= 2)
176 if ((spc && !isspace(sel_pos(ps))) ||
177 (!spc && !inword(sel_pos(ps))))
180 if (!(ps % video_size_row))
183 spc = isspace(sel_pos(pe));
184 for (new_sel_end = pe; ; pe += 2)
186 if ((spc && !isspace(sel_pos(pe))) ||
187 (!spc && !inword(sel_pos(pe))))
190 if (!((pe + 2) % video_size_row))
194 case TIOCL_SELLINE: /* line-by-line selection */
195 new_sel_start = ps - ps % video_size_row;
196 new_sel_end = pe + video_size_row
197 - pe % video_size_row - 2;
199 case TIOCL_SELPOINTER:
200 highlight_pointer(pe);
206 /* remove the pointer */
207 highlight_pointer(-1);
209 /* select to end of line if on trailing space */
210 if (new_sel_end > new_sel_start &&
211 !atedge(new_sel_end, video_size_row) &&
212 isspace(sel_pos(new_sel_end))) {
213 for (pe = new_sel_end + 2; ; pe += 2)
214 if (!isspace(sel_pos(pe)) ||
215 atedge(pe, video_size_row))
217 if (isspace(sel_pos(pe)))
220 if (sel_start == -1) /* no current selection */
221 highlight(new_sel_start, new_sel_end);
222 else if (new_sel_start == sel_start)
224 if (new_sel_end == sel_end) /* no action required */
226 else if (new_sel_end > sel_end) /* extend to right */
227 highlight(sel_end + 2, new_sel_end);
228 else /* contract from right */
229 highlight(new_sel_end + 2, sel_end);
231 else if (new_sel_end == sel_end)
233 if (new_sel_start < sel_start) /* extend to left */
234 highlight(new_sel_start, sel_start - 2);
235 else /* contract from left */
236 highlight(sel_start, new_sel_start - 2);
238 else /* some other case; start selection from scratch */
241 highlight(new_sel_start, new_sel_end);
243 sel_start = new_sel_start;
244 sel_end = new_sel_end;
246 /* Allocate a new buffer before freeing the old one ... */
247 bp = kmalloc((sel_end-sel_start)/2+1, GFP_KERNEL);
249 printk(KERN_WARNING "selection: kmalloc() failed\n");
258 for (i = sel_start; i <= sel_end; i += 2) {
262 if (! ((i + 2) % video_size_row)) {
263 /* strip trailing blanks from line and add newline,
264 unless non-space at end of line. */
272 sel_buffer_lth = bp - sel_buffer;
276 /* Insert the contents of the selection buffer into the
277 * queue of the tty associated with the current console.
278 * Invoked by ioctl().
280 int paste_selection(struct tty_struct *tty)
282 struct vt_struct *vt = (struct vt_struct *) tty->driver_data;
283 int pasted = 0, count;
284 DECLARE_WAITQUEUE(wait, current);
286 acquire_console_sem();
287 poke_blanked_console();
288 release_console_sem();
290 add_wait_queue(&vt->paste_wait, &wait);
291 while (sel_buffer && sel_buffer_lth > pasted) {
292 set_current_state(TASK_INTERRUPTIBLE);
293 if (test_bit(TTY_THROTTLED, &tty->flags)) {
297 count = sel_buffer_lth - pasted;
298 count = MIN(count, tty->ldisc.receive_room(tty));
299 tty->ldisc.receive_buf(tty, sel_buffer + pasted, 0, count);
302 remove_wait_queue(&vt->paste_wait, &wait);
303 current->state = TASK_RUNNING;
307 EXPORT_SYMBOL(set_selection);
308 EXPORT_SYMBOL(paste_selection);