]> Pileus Git - ~andy/linux/blob - drivers/staging/speakup/selection.c
Staging: add speakup to the staging directory
[~andy/linux] / drivers / staging / speakup / selection.c
1 #include <linux/slab.h> /* for kmalloc */
2 #include <linux/consolemap.h>
3 #include <linux/interrupt.h>
4 #include <linux/sched.h>
5 #include <linux/selection.h>
6
7 #include "speakup.h"
8
9 /* ------ cut and paste ----- */
10 /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
11 #define ishardspace(c)      ((c) == ' ')
12
13 unsigned short xs, ys, xe, ye; /* our region points */
14
15 /* Variables for selection control. */
16 /* must not be disallocated */
17 struct vc_data *spk_sel_cons;
18 /* cleared by clear_selection */
19 static int sel_start = -1;
20 static int sel_end;
21 static int sel_buffer_lth;
22 static char *sel_buffer;
23
24 static unsigned char sel_pos(int n)
25 {
26         return inverse_translate(spk_sel_cons, screen_glyph(spk_sel_cons, n), 0);
27 }
28
29 void speakup_clear_selection(void)
30 {
31         sel_start = -1;
32 }
33
34 /* does screen address p correspond to character at LH/RH edge of screen? */
35 static int atedge(const int p, int size_row)
36 {
37         return (!(p % size_row) || !((p + 2) % size_row));
38 }
39
40 /* constrain v such that v <= u */
41 static unsigned short limit(const unsigned short v, const unsigned short u)
42 {
43         return (v > u) ? u : v;
44 }
45
46 int speakup_set_selection(struct tty_struct *tty)
47 {
48         int new_sel_start, new_sel_end;
49         char *bp, *obp;
50         int i, ps, pe;
51         struct vc_data *vc = vc_cons[fg_console].d;
52
53         xs = limit(xs, vc->vc_cols - 1);
54         ys = limit(ys, vc->vc_rows - 1);
55         xe = limit(xe, vc->vc_cols - 1);
56         ye = limit(ye, vc->vc_rows - 1);
57         ps = ys * vc->vc_size_row + (xs << 1);
58         pe = ye * vc->vc_size_row + (xe << 1);
59
60         if (ps > pe) {
61                 /* make sel_start <= sel_end */
62                 int tmp = ps;
63                 ps = pe;
64                 pe = tmp;
65         }
66
67         if (spk_sel_cons != vc_cons[fg_console].d) {
68                 speakup_clear_selection();
69                 spk_sel_cons = vc_cons[fg_console].d;
70                 printk(KERN_WARNING
71                         "Selection: mark console not the same as cut\n");
72                 return -EINVAL;
73         }
74
75         new_sel_start = ps;
76         new_sel_end = pe;
77
78         /* select to end of line if on trailing space */
79         if (new_sel_end > new_sel_start &&
80             !atedge(new_sel_end, vc->vc_size_row) &&
81             ishardspace(sel_pos(new_sel_end))) {
82                 for (pe = new_sel_end + 2; ; pe += 2)
83                         if (!ishardspace(sel_pos(pe)) ||
84                             atedge(pe, vc->vc_size_row))
85                                 break;
86                 if (ishardspace(sel_pos(pe)))
87                         new_sel_end = pe;
88         }
89         if ((new_sel_start == sel_start) && (new_sel_end == sel_end))
90                 return 0; /* no action required */
91
92         sel_start = new_sel_start;
93         sel_end = new_sel_end;
94         /* Allocate a new buffer before freeing the old one ... */
95         bp = kmalloc((sel_end-sel_start)/2+1, GFP_ATOMIC);
96         if (!bp) {
97                 printk(KERN_WARNING "selection: kmalloc() failed\n");
98                 speakup_clear_selection();
99                 return -ENOMEM;
100         }
101         kfree(sel_buffer);
102         sel_buffer = bp;
103
104         obp = bp;
105         for (i = sel_start; i <= sel_end; i += 2) {
106                 *bp = sel_pos(i);
107                 if (!ishardspace(*bp++))
108                         obp = bp;
109                 if (!((i + 2) % vc->vc_size_row)) {
110                         /* strip trailing blanks from line and add newline,
111                            unless non-space at end of line. */
112                         if (obp != bp) {
113                                 bp = obp;
114                                 *bp++ = '\r';
115                         }
116                         obp = bp;
117                 }
118         }
119         sel_buffer_lth = bp - sel_buffer;
120         return 0;
121 }
122
123 /* TODO: move to some helper thread, probably.  That'd fix having to check for
124  * in_atomic().  */
125 int speakup_paste_selection(struct tty_struct *tty)
126 {
127         struct vc_data *vc = (struct vc_data *) tty->driver_data;
128         int pasted = 0, count;
129         DECLARE_WAITQUEUE(wait, current);
130         add_wait_queue(&vc->paste_wait, &wait);
131         while (sel_buffer && sel_buffer_lth > pasted) {
132                 set_current_state(TASK_INTERRUPTIBLE);
133                 if (test_bit(TTY_THROTTLED, &tty->flags)) {
134                         if (in_atomic())
135                                 /* can't be performed in an interrupt handler, abort */
136                                 break;
137                         schedule();
138                         continue;
139                 }
140                 count = sel_buffer_lth - pasted;
141                 count = min_t(int, count, tty->receive_room);
142 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
143                 tty->ldisc->ops->receive_buf(tty, sel_buffer + pasted, 0, count);
144 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
145                 tty->ldisc.ops->receive_buf(tty, sel_buffer + pasted, 0, count);
146 #else
147                 tty->ldisc.receive_buf(tty, sel_buffer + pasted, 0, count);
148 #endif
149                 pasted += count;
150         }
151         remove_wait_queue(&vc->paste_wait, &wait);
152         current->state = TASK_RUNNING;
153         return 0;
154 }
155