Staging: speakup: Move pasting into a work item
[pandora-kernel.git] / 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/device.h> /* for dev_warn */
6 #include <linux/selection.h>
7 #include <linux/workqueue.h>
8 #include <asm/cmpxchg.h>
9
10 #include "speakup.h"
11
12 /* ------ cut and paste ----- */
13 /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
14 #define ishardspace(c)      ((c) == ' ')
15
16 unsigned short spk_xs, spk_ys, spk_xe, spk_ye; /* our region points */
17
18 /* Variables for selection control. */
19 /* must not be deallocated */
20 struct vc_data *spk_sel_cons;
21 /* cleared by clear_selection */
22 static int sel_start = -1;
23 static int sel_end;
24 static int sel_buffer_lth;
25 static char *sel_buffer;
26
27 static unsigned char sel_pos(int n)
28 {
29         return inverse_translate(spk_sel_cons,
30                 screen_glyph(spk_sel_cons, n), 0);
31 }
32
33 void speakup_clear_selection(void)
34 {
35         sel_start = -1;
36 }
37
38 /* does screen address p correspond to character at LH/RH edge of screen? */
39 static int atedge(const int p, int size_row)
40 {
41         return !(p % size_row) || !((p + 2) % size_row);
42 }
43
44 /* constrain v such that v <= u */
45 static unsigned short limit(const unsigned short v, const unsigned short u)
46 {
47         return (v > u) ? u : v;
48 }
49
50 int speakup_set_selection(struct tty_struct *tty)
51 {
52         int new_sel_start, new_sel_end;
53         char *bp, *obp;
54         int i, ps, pe;
55         struct vc_data *vc = vc_cons[fg_console].d;
56
57         spk_xs = limit(spk_xs, vc->vc_cols - 1);
58         spk_ys = limit(spk_ys, vc->vc_rows - 1);
59         spk_xe = limit(spk_xe, vc->vc_cols - 1);
60         spk_ye = limit(spk_ye, vc->vc_rows - 1);
61         ps = spk_ys * vc->vc_size_row + (spk_xs << 1);
62         pe = spk_ye * vc->vc_size_row + (spk_xe << 1);
63
64         if (ps > pe) {
65                 /* make sel_start <= sel_end */
66                 int tmp = ps;
67                 ps = pe;
68                 pe = tmp;
69         }
70
71         if (spk_sel_cons != vc_cons[fg_console].d) {
72                 speakup_clear_selection();
73                 spk_sel_cons = vc_cons[fg_console].d;
74                 dev_warn(tty->dev,
75                         "Selection: mark console not the same as cut\n");
76                 return -EINVAL;
77         }
78
79         new_sel_start = ps;
80         new_sel_end = pe;
81
82         /* select to end of line if on trailing space */
83         if (new_sel_end > new_sel_start &&
84             !atedge(new_sel_end, vc->vc_size_row) &&
85             ishardspace(sel_pos(new_sel_end))) {
86                 for (pe = new_sel_end + 2; ; pe += 2)
87                         if (!ishardspace(sel_pos(pe)) ||
88                             atedge(pe, vc->vc_size_row))
89                                 break;
90                 if (ishardspace(sel_pos(pe)))
91                         new_sel_end = pe;
92         }
93         if ((new_sel_start == sel_start) && (new_sel_end == sel_end))
94                 return 0; /* no action required */
95
96         sel_start = new_sel_start;
97         sel_end = new_sel_end;
98         /* Allocate a new buffer before freeing the old one ... */
99         bp = kmalloc((sel_end-sel_start)/2+1, GFP_ATOMIC);
100         if (!bp) {
101                 speakup_clear_selection();
102                 return -ENOMEM;
103         }
104         kfree(sel_buffer);
105         sel_buffer = bp;
106
107         obp = bp;
108         for (i = sel_start; i <= sel_end; i += 2) {
109                 *bp = sel_pos(i);
110                 if (!ishardspace(*bp++))
111                         obp = bp;
112                 if (!((i + 2) % vc->vc_size_row)) {
113                         /* strip trailing blanks from line and add newline,
114                            unless non-space at end of line. */
115                         if (obp != bp) {
116                                 bp = obp;
117                                 *bp++ = '\r';
118                         }
119                         obp = bp;
120                 }
121         }
122         sel_buffer_lth = bp - sel_buffer;
123         return 0;
124 }
125
126 struct speakup_paste_work {
127         struct work_struct work;
128         struct tty_struct *tty;
129 };
130
131 static void __speakup_paste_selection(struct work_struct *work)
132 {
133         struct speakup_paste_work *spw =
134                 container_of(work, struct speakup_paste_work, work);
135         struct tty_struct *tty = xchg(&spw->tty, NULL);
136         struct vc_data *vc = (struct vc_data *) tty->driver_data;
137         int pasted = 0, count;
138         DECLARE_WAITQUEUE(wait, current);
139
140         add_wait_queue(&vc->paste_wait, &wait);
141         while (sel_buffer && sel_buffer_lth > pasted) {
142                 set_current_state(TASK_INTERRUPTIBLE);
143                 if (test_bit(TTY_THROTTLED, &tty->flags)) {
144                         schedule();
145                         continue;
146                 }
147                 count = sel_buffer_lth - pasted;
148                 count = min_t(int, count, tty->receive_room);
149                 tty->ldisc->ops->receive_buf(tty, sel_buffer + pasted,
150                         NULL, count);
151                 pasted += count;
152         }
153         remove_wait_queue(&vc->paste_wait, &wait);
154         current->state = TASK_RUNNING;
155         tty_kref_put(tty);
156 }
157
158 static struct speakup_paste_work speakup_paste_work = {
159         .work = __WORK_INITIALIZER(speakup_paste_work.work,
160                                    __speakup_paste_selection)
161 };
162
163 int speakup_paste_selection(struct tty_struct *tty)
164 {
165         if (cmpxchg(&speakup_paste_work.tty, NULL, tty) != NULL)
166                 return -EBUSY;
167
168         tty_kref_get(tty);
169         schedule_work_on(WORK_CPU_UNBOUND, &speakup_paste_work.work);
170         return 0;
171 }
172
173 void speakup_cancel_paste(void)
174 {
175         cancel_work_sync(&speakup_paste_work.work);
176         tty_kref_put(speakup_paste_work.tty);
177 }