ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / selection.c
1 /*
2  * linux/drivers/char/selection.c
3  *
4  * This module exports the functions:
5  *
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)'
10  *
11  * Now that /dev/vcs exists, most of this can disappear again.
12  */
13
14 #include <linux/module.h>
15 #include <linux/tty.h>
16 #include <linux/sched.h>
17 #include <linux/mm.h>
18 #include <linux/slab.h>
19 #include <linux/types.h>
20
21 #include <asm/uaccess.h>
22
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>
28
29 #ifndef MIN
30 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
31 #endif
32
33 /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
34 #define isspace(c)      ((c) == ' ')
35
36 extern void poke_blanked_console(void);
37
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 */
42 static int sel_end;
43 static int sel_buffer_lth;
44 static char *sel_buffer;
45
46 /* clear_selection, highlight and highlight_pointer can be called
47    from interrupt (via scrollback/front) */
48
49 /* set reverse video on characters s-e of console with selection. */
50 inline static void
51 highlight(const int s, const int e) {
52         invert_screen(sel_cons, s, e-s+2, 1);
53 }
54
55 /* use complementary color to show the pointer */
56 inline static void
57 highlight_pointer(const int where) {
58         complement_pos(sel_cons, where);
59 }
60
61 static unsigned char
62 sel_pos(int n)
63 {
64         return inverse_translate(vc_cons[sel_cons].d, screen_glyph(sel_cons, n));
65 }
66
67 /* remove the current selection highlight, if any,
68    from the console holding the selection. */
69 void
70 clear_selection(void) {
71         highlight_pointer(-1); /* hide the pointer */
72         if (sel_start != -1) {
73                 highlight(sel_start, sel_end);
74                 sel_start = -1;
75         }
76 }
77
78 /*
79  * User settable table: what characters are to be considered alphabetic?
80  * 256 bits
81  */
82 static u32 inwordLut[8]={
83   0x00000000, /* control chars     */
84   0x03FF0000, /* digits            */
85   0x87FFFFFE, /* uppercase and '_' */
86   0x07FFFFFE, /* lowercase         */
87   0x00000000,
88   0x00000000,
89   0xFF7FFFFF, /* latin-1 accented letters, not multiplication sign */
90   0xFF7FFFFF  /* latin-1 accented letters, not division sign */
91 };
92
93 static inline int inword(const unsigned char c) {
94         return ( inwordLut[c>>5] >> (c & 0x1F) ) & 1;
95 }
96
97 /* set inwordLut contents. Invoked by ioctl(). */
98 int sel_loadlut(const unsigned long arg)
99 {
100         return copy_from_user(inwordLut, (u32 *)(arg+4), 32) ? -EFAULT : 0;
101 }
102
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)
105 {
106         return (!(p % size_row) || !((p + 2) % size_row));
107 }
108
109 /* constrain v such that v <= u */
110 static inline unsigned short limit(const unsigned short v, const unsigned short u)
111 {
112         return (v > u) ? u : v;
113 }
114
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)
117 {
118         int sel_mode, new_sel_start, new_sel_end, spc;
119         char *bp, *obp;
120         int i, ps, pe;
121         unsigned int currcons = fg_console;
122
123         poke_blanked_console();
124
125         { unsigned short xs, ys, xe, ye;
126
127           if (user) {
128                   if (verify_area(VERIFY_READ, sel, sizeof(*sel)))
129                         return -EFAULT;
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);
135           } else {
136                   xs = sel->xs; /* set selection from kernel */
137                   ys = sel->ys;
138                   xe = sel->xe;
139                   ye = sel->ye;
140                   sel_mode = sel->sel_mode;
141           }
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);
149
150           if (sel_mode == TIOCL_SELCLEAR) {
151               /* useful for screendump without selection highlights */
152               clear_selection();
153               return 0;
154           }
155
156           if (mouse_reporting() && (sel_mode & TIOCL_SELMOUSEREPORT)) {
157               mouse_report(tty, sel_mode & TIOCL_SELBUTTONMASK, xs, ys);
158               return 0;
159           }
160         }
161
162         if (ps > pe)    /* make sel_start <= sel_end */
163         {
164                 int tmp = ps;
165                 ps = pe;
166                 pe = tmp;
167         }
168
169         if (sel_cons != fg_console) {
170                 clear_selection();
171                 sel_cons = fg_console;
172         }
173
174         switch (sel_mode)
175         {
176                 case TIOCL_SELCHAR:     /* character-by-character selection */
177                         new_sel_start = ps;
178                         new_sel_end = pe;
179                         break;
180                 case TIOCL_SELWORD:     /* word-by-word selection */
181                         spc = isspace(sel_pos(ps));
182                         for (new_sel_start = ps; ; ps -= 2)
183                         {
184                                 if ((spc && !isspace(sel_pos(ps))) ||
185                                     (!spc && !inword(sel_pos(ps))))
186                                         break;
187                                 new_sel_start = ps;
188                                 if (!(ps % video_size_row))
189                                         break;
190                         }
191                         spc = isspace(sel_pos(pe));
192                         for (new_sel_end = pe; ; pe += 2)
193                         {
194                                 if ((spc && !isspace(sel_pos(pe))) ||
195                                     (!spc && !inword(sel_pos(pe))))
196                                         break;
197                                 new_sel_end = pe;
198                                 if (!((pe + 2) % video_size_row))
199                                         break;
200                         }
201                         break;
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;
206                         break;
207                 case TIOCL_SELPOINTER:
208                         highlight_pointer(pe);
209                         return 0;
210                 default:
211                         return -EINVAL;
212         }
213
214         /* remove the pointer */
215         highlight_pointer(-1);
216
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))
224                                 break;
225                 if (isspace(sel_pos(pe)))
226                         new_sel_end = pe;
227         }
228         if (sel_start == -1)    /* no current selection */
229                 highlight(new_sel_start, new_sel_end);
230         else if (new_sel_start == sel_start)
231         {
232                 if (new_sel_end == sel_end)     /* no action required */
233                         return 0;
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);
238         }
239         else if (new_sel_end == sel_end)
240         {
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);
245         }
246         else    /* some other case; start selection from scratch */
247         {
248                 clear_selection();
249                 highlight(new_sel_start, new_sel_end);
250         }
251         sel_start = new_sel_start;
252         sel_end = new_sel_end;
253
254         /* Allocate a new buffer before freeing the old one ... */
255         bp = kmalloc((sel_end-sel_start)/2+1, GFP_KERNEL);
256         if (!bp) {
257                 printk(KERN_WARNING "selection: kmalloc() failed\n");
258                 clear_selection();
259                 return -ENOMEM;
260         }
261         if (sel_buffer)
262                 kfree(sel_buffer);
263         sel_buffer = bp;
264
265         obp = bp;
266         for (i = sel_start; i <= sel_end; i += 2) {
267                 *bp = sel_pos(i);
268                 if (!isspace(*bp++))
269                         obp = bp;
270                 if (! ((i + 2) % video_size_row)) {
271                         /* strip trailing blanks from line and add newline,
272                            unless non-space at end of line. */
273                         if (obp != bp) {
274                                 bp = obp;
275                                 *bp++ = '\r';
276                         }
277                         obp = bp;
278                 }
279         }
280         sel_buffer_lth = bp - sel_buffer;
281         return 0;
282 }
283
284 /* Insert the contents of the selection buffer into the
285  * queue of the tty associated with the current console.
286  * Invoked by ioctl().
287  */
288 int paste_selection(struct tty_struct *tty)
289 {
290         struct vt_struct *vt = (struct vt_struct *) tty->driver_data;
291         int     pasted = 0, count;
292         DECLARE_WAITQUEUE(wait, current);
293
294         acquire_console_sem();
295         poke_blanked_console();
296         release_console_sem();
297
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)) {
302                         schedule();
303                         continue;
304                 }
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);
308                 pasted += count;
309         }
310         remove_wait_queue(&vt->paste_wait, &wait);
311         current->state = TASK_RUNNING;
312         return 0;
313 }
314
315 EXPORT_SYMBOL(set_selection);
316 EXPORT_SYMBOL(paste_selection);