2 * linux/drivers/char/selection.c
4 * This module exports the functions:
6 * 'int set_selection(const unsigned long arg)'
7 * 'void clear_selection(void)'
8 * 'int paste_selection(struct tty_struct *tty)'
9 * 'int sel_loadlut(const unsigned long arg)'
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(const unsigned long arg)
100 return copy_from_user(inwordLut, (u32 *)(arg+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 *sel, struct tty_struct *tty, int user)
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;
128 if (verify_area(VERIFY_READ, sel, sizeof(*sel)))
130 __get_user(xs, &sel->xs);
131 __get_user(ys, &sel->ys);
132 __get_user(xe, &sel->xe);
133 __get_user(ye, &sel->ye);
134 __get_user(sel_mode, &sel->sel_mode);
136 xs = sel->xs; /* set selection from kernel */
140 sel_mode = sel->sel_mode;
142 xs--; ys--; xe--; ye--;
143 xs = limit(xs, video_num_columns - 1);
144 ys = limit(ys, video_num_lines - 1);
145 xe = limit(xe, video_num_columns - 1);
146 ye = limit(ye, video_num_lines - 1);
147 ps = ys * video_size_row + (xs << 1);
148 pe = ye * video_size_row + (xe << 1);
150 if (sel_mode == TIOCL_SELCLEAR) {
151 /* useful for screendump without selection highlights */
156 if (mouse_reporting() && (sel_mode & TIOCL_SELMOUSEREPORT)) {
157 mouse_report(tty, sel_mode & TIOCL_SELBUTTONMASK, xs, ys);
162 if (ps > pe) /* make sel_start <= sel_end */
169 if (sel_cons != fg_console) {
171 sel_cons = fg_console;
176 case TIOCL_SELCHAR: /* character-by-character selection */
180 case TIOCL_SELWORD: /* word-by-word selection */
181 spc = isspace(sel_pos(ps));
182 for (new_sel_start = ps; ; ps -= 2)
184 if ((spc && !isspace(sel_pos(ps))) ||
185 (!spc && !inword(sel_pos(ps))))
188 if (!(ps % video_size_row))
191 spc = isspace(sel_pos(pe));
192 for (new_sel_end = pe; ; pe += 2)
194 if ((spc && !isspace(sel_pos(pe))) ||
195 (!spc && !inword(sel_pos(pe))))
198 if (!((pe + 2) % video_size_row))
202 case TIOCL_SELLINE: /* line-by-line selection */
203 new_sel_start = ps - ps % video_size_row;
204 new_sel_end = pe + video_size_row
205 - pe % video_size_row - 2;
207 case TIOCL_SELPOINTER:
208 highlight_pointer(pe);
214 /* remove the pointer */
215 highlight_pointer(-1);
217 /* select to end of line if on trailing space */
218 if (new_sel_end > new_sel_start &&
219 !atedge(new_sel_end, video_size_row) &&
220 isspace(sel_pos(new_sel_end))) {
221 for (pe = new_sel_end + 2; ; pe += 2)
222 if (!isspace(sel_pos(pe)) ||
223 atedge(pe, video_size_row))
225 if (isspace(sel_pos(pe)))
228 if (sel_start == -1) /* no current selection */
229 highlight(new_sel_start, new_sel_end);
230 else if (new_sel_start == sel_start)
232 if (new_sel_end == sel_end) /* no action required */
234 else if (new_sel_end > sel_end) /* extend to right */
235 highlight(sel_end + 2, new_sel_end);
236 else /* contract from right */
237 highlight(new_sel_end + 2, sel_end);
239 else if (new_sel_end == sel_end)
241 if (new_sel_start < sel_start) /* extend to left */
242 highlight(new_sel_start, sel_start - 2);
243 else /* contract from left */
244 highlight(sel_start, new_sel_start - 2);
246 else /* some other case; start selection from scratch */
249 highlight(new_sel_start, new_sel_end);
251 sel_start = new_sel_start;
252 sel_end = new_sel_end;
254 /* Allocate a new buffer before freeing the old one ... */
255 bp = kmalloc((sel_end-sel_start)/2+1, GFP_KERNEL);
257 printk(KERN_WARNING "selection: kmalloc() failed\n");
266 for (i = sel_start; i <= sel_end; i += 2) {
270 if (! ((i + 2) % video_size_row)) {
271 /* strip trailing blanks from line and add newline,
272 unless non-space at end of line. */
280 sel_buffer_lth = bp - sel_buffer;
284 /* Insert the contents of the selection buffer into the
285 * queue of the tty associated with the current console.
286 * Invoked by ioctl().
288 int paste_selection(struct tty_struct *tty)
290 struct vt_struct *vt = (struct vt_struct *) tty->driver_data;
291 int pasted = 0, count;
292 DECLARE_WAITQUEUE(wait, current);
294 acquire_console_sem();
295 poke_blanked_console();
296 release_console_sem();
298 add_wait_queue(&vt->paste_wait, &wait);
299 while (sel_buffer && sel_buffer_lth > pasted) {
300 set_current_state(TASK_INTERRUPTIBLE);
301 if (test_bit(TTY_THROTTLED, &tty->flags)) {
305 count = sel_buffer_lth - pasted;
306 count = MIN(count, tty->ldisc.receive_room(tty));
307 tty->ldisc.receive_buf(tty, sel_buffer + pasted, 0, count);
310 remove_wait_queue(&vt->paste_wait, &wait);
311 current->state = TASK_RUNNING;
315 EXPORT_SYMBOL(set_selection);
316 EXPORT_SYMBOL(paste_selection);