]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/tty/vt/selection.c
2 * This module exports the functions:
4 * 'int set_selection(struct tiocl_selection __user *, struct tty_struct *)'
5 * 'void clear_selection(void)'
6 * 'int paste_selection(struct tty_struct *)'
7 * 'int sel_loadlut(char __user *)'
9 * Now that /dev/vcs exists, most of this can disappear again.
12 #include <linux/module.h>
13 #include <linux/tty.h>
14 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
19 #include <linux/uaccess.h>
21 #include <linux/kbd_kern.h>
22 #include <linux/vt_kern.h>
23 #include <linux/consolemap.h>
24 #include <linux/selection.h>
25 #include <linux/tiocl.h>
26 #include <linux/console.h>
27 #include <linux/tty_flip.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 /* FIXME: all this needs locking */
35 /* Variables for selection control. */
36 /* Use a dynamic buffer, instead of static (Dec 1994) */
37 struct vc_data
*sel_cons
; /* must not be deallocated */
38 static int use_unicode
;
39 static volatile int sel_start
= -1; /* cleared by clear_selection */
41 static int sel_buffer_lth
;
42 static char *sel_buffer
;
44 /* clear_selection, highlight and highlight_pointer can be called
45 from interrupt (via scrollback/front) */
47 /* set reverse video on characters s-e of console with selection. */
48 static inline void highlight(const int s
, const int e
)
50 invert_screen(sel_cons
, s
, e
-s
+2, 1);
53 /* use complementary color to show the pointer */
54 static inline void highlight_pointer(const int where
)
56 complement_pos(sel_cons
, where
);
62 return inverse_translate(sel_cons
, screen_glyph(sel_cons
, n
),
67 * clear_selection - remove current selection
69 * Remove the current selection highlight, if any from the console
70 * holding the selection. The caller must hold the console lock.
72 void clear_selection(void)
74 highlight_pointer(-1); /* hide the pointer */
75 if (sel_start
!= -1) {
76 highlight(sel_start
, sel_end
);
82 * User settable table: what characters are to be considered alphabetic?
83 * 128 bits. Locked by the console lock.
85 static u32 inwordLut
[]={
86 0x00000000, /* control chars */
87 0x03FFE000, /* digits and "-./" */
88 0x87FFFFFE, /* uppercase and '_' */
89 0x07FFFFFE, /* lowercase */
92 static inline int inword(const u16 c
) {
93 return c
> 0x7f || (( inwordLut
[c
>>5] >> (c
& 0x1F) ) & 1);
97 * set loadlut - load the LUT table
100 * Load the LUT table from user space. The caller must hold the console
101 * lock. Make a temporary copy so a partial update doesn't make a mess.
103 int sel_loadlut(char __user
*p
)
105 u32 tmplut
[ARRAY_SIZE(inwordLut
)];
106 if (copy_from_user(tmplut
, (u32 __user
*)(p
+4), sizeof(inwordLut
)))
108 memcpy(inwordLut
, tmplut
, sizeof(inwordLut
));
112 /* does screen address p correspond to character at LH/RH edge of screen? */
113 static inline int atedge(const int p
, int size_row
)
115 return (!(p
% size_row
) || !((p
+ 2) % size_row
));
118 /* constrain v such that v <= u */
119 static inline unsigned short limit(const unsigned short v
, const unsigned short u
)
121 return (v
> u
) ? u
: v
;
124 /* stores the char in UTF8 and returns the number of bytes used (1-3) */
125 static int store_utf8(u16 c
, char *p
)
131 } else if (c
< 0x800) {
132 /* 110***** 10****** */
133 p
[0] = 0xc0 | (c
>> 6);
134 p
[1] = 0x80 | (c
& 0x3f);
137 /* 1110**** 10****** 10****** */
138 p
[0] = 0xe0 | (c
>> 12);
139 p
[1] = 0x80 | ((c
>> 6) & 0x3f);
140 p
[2] = 0x80 | (c
& 0x3f);
146 * set_selection - set the current selection.
147 * @sel: user selection info
148 * @tty: the console tty
150 * Invoked by the ioctl handle for the vt layer.
152 * The entire selection process is managed under the console_lock. It's
153 * a lot under the lock but its hardly a performance path
155 int set_selection(const struct tiocl_selection __user
*sel
, struct tty_struct
*tty
)
157 struct vc_data
*vc
= vc_cons
[fg_console
].d
;
158 int sel_mode
, new_sel_start
, new_sel_end
, spc
;
160 int i
, ps
, pe
, multiplier
;
164 poke_blanked_console();
166 { unsigned short xs
, ys
, xe
, ye
;
168 if (!access_ok(VERIFY_READ
, sel
, sizeof(*sel
)))
170 __get_user(xs
, &sel
->xs
);
171 __get_user(ys
, &sel
->ys
);
172 __get_user(xe
, &sel
->xe
);
173 __get_user(ye
, &sel
->ye
);
174 __get_user(sel_mode
, &sel
->sel_mode
);
175 xs
--; ys
--; xe
--; ye
--;
176 xs
= limit(xs
, vc
->vc_cols
- 1);
177 ys
= limit(ys
, vc
->vc_rows
- 1);
178 xe
= limit(xe
, vc
->vc_cols
- 1);
179 ye
= limit(ye
, vc
->vc_rows
- 1);
180 ps
= ys
* vc
->vc_size_row
+ (xs
<< 1);
181 pe
= ye
* vc
->vc_size_row
+ (xe
<< 1);
183 if (sel_mode
== TIOCL_SELCLEAR
) {
184 /* useful for screendump without selection highlights */
189 if (mouse_reporting() && (sel_mode
& TIOCL_SELMOUSEREPORT
)) {
190 mouse_report(tty
, sel_mode
& TIOCL_SELBUTTONMASK
, xs
, ys
);
195 if (ps
> pe
) /* make sel_start <= sel_end */
202 if (sel_cons
!= vc_cons
[fg_console
].d
) {
204 sel_cons
= vc_cons
[fg_console
].d
;
206 mode
= vt_do_kdgkbmode(fg_console
);
207 if (mode
== K_UNICODE
)
214 case TIOCL_SELCHAR
: /* character-by-character selection */
218 case TIOCL_SELWORD
: /* word-by-word selection */
219 spc
= isspace(sel_pos(ps
));
220 for (new_sel_start
= ps
; ; ps
-= 2)
222 if ((spc
&& !isspace(sel_pos(ps
))) ||
223 (!spc
&& !inword(sel_pos(ps
))))
226 if (!(ps
% vc
->vc_size_row
))
229 spc
= isspace(sel_pos(pe
));
230 for (new_sel_end
= pe
; ; pe
+= 2)
232 if ((spc
&& !isspace(sel_pos(pe
))) ||
233 (!spc
&& !inword(sel_pos(pe
))))
236 if (!((pe
+ 2) % vc
->vc_size_row
))
240 case TIOCL_SELLINE
: /* line-by-line selection */
241 new_sel_start
= ps
- ps
% vc
->vc_size_row
;
242 new_sel_end
= pe
+ vc
->vc_size_row
243 - pe
% vc
->vc_size_row
- 2;
245 case TIOCL_SELPOINTER
:
246 highlight_pointer(pe
);
252 /* remove the pointer */
253 highlight_pointer(-1);
255 /* select to end of line if on trailing space */
256 if (new_sel_end
> new_sel_start
&&
257 !atedge(new_sel_end
, vc
->vc_size_row
) &&
258 isspace(sel_pos(new_sel_end
))) {
259 for (pe
= new_sel_end
+ 2; ; pe
+= 2)
260 if (!isspace(sel_pos(pe
)) ||
261 atedge(pe
, vc
->vc_size_row
))
263 if (isspace(sel_pos(pe
)))
266 if (sel_start
== -1) /* no current selection */
267 highlight(new_sel_start
, new_sel_end
);
268 else if (new_sel_start
== sel_start
)
270 if (new_sel_end
== sel_end
) /* no action required */
272 else if (new_sel_end
> sel_end
) /* extend to right */
273 highlight(sel_end
+ 2, new_sel_end
);
274 else /* contract from right */
275 highlight(new_sel_end
+ 2, sel_end
);
277 else if (new_sel_end
== sel_end
)
279 if (new_sel_start
< sel_start
) /* extend to left */
280 highlight(new_sel_start
, sel_start
- 2);
281 else /* contract from left */
282 highlight(sel_start
, new_sel_start
- 2);
284 else /* some other case; start selection from scratch */
287 highlight(new_sel_start
, new_sel_end
);
289 sel_start
= new_sel_start
;
290 sel_end
= new_sel_end
;
292 /* Allocate a new buffer before freeing the old one ... */
293 multiplier
= use_unicode
? 3 : 1; /* chars can take up to 3 bytes */
294 bp
= kmalloc(((sel_end
-sel_start
)/2+1)*multiplier
, GFP_KERNEL
);
296 printk(KERN_WARNING
"selection: kmalloc() failed\n");
304 for (i
= sel_start
; i
<= sel_end
; i
+= 2) {
307 bp
+= store_utf8(c
, bp
);
312 if (! ((i
+ 2) % vc
->vc_size_row
)) {
313 /* strip trailing blanks from line and add newline,
314 unless non-space at end of line. */
322 sel_buffer_lth
= bp
- sel_buffer
;
326 /* Insert the contents of the selection buffer into the
327 * queue of the tty associated with the current console.
328 * Invoked by ioctl().
330 * Locking: called without locks. Calls the ldisc wrongly with
333 int paste_selection(struct tty_struct
*tty
)
335 struct vc_data
*vc
= tty
->driver_data
;
338 struct tty_ldisc
*ld
;
339 DECLARE_WAITQUEUE(wait
, current
);
342 poke_blanked_console();
345 ld
= tty_ldisc_ref_wait(tty
);
347 return -EIO
; /* ldisc was hung up */
348 tty_buffer_lock_exclusive(&vc
->port
);
350 add_wait_queue(&vc
->paste_wait
, &wait
);
351 while (sel_buffer
&& sel_buffer_lth
> pasted
) {
352 set_current_state(TASK_INTERRUPTIBLE
);
353 if (tty_throttled(tty
)) {
357 __set_current_state(TASK_RUNNING
);
358 count
= sel_buffer_lth
- pasted
;
359 count
= tty_ldisc_receive_buf(ld
, sel_buffer
+ pasted
, NULL
,
363 remove_wait_queue(&vc
->paste_wait
, &wait
);
364 __set_current_state(TASK_RUNNING
);
366 tty_buffer_unlock_exclusive(&vc
->port
);