mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / staging / speakup / selection.c
blob34a6deef1d6c0f574d0d70d279d7b25e6d85fc9a
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 <linux/tty.h>
9 #include <linux/tty_flip.h>
10 #include <asm/cmpxchg.h>
12 #include "speakup.h"
14 /* ------ cut and paste ----- */
15 /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
16 #define ishardspace(c) ((c) == ' ')
18 unsigned short spk_xs, spk_ys, spk_xe, spk_ye; /* our region points */
20 /* Variables for selection control. */
21 /* must not be deallocated */
22 struct vc_data *spk_sel_cons;
23 /* cleared by clear_selection */
24 static int sel_start = -1;
25 static int sel_end;
26 static int sel_buffer_lth;
27 static char *sel_buffer;
29 static unsigned char sel_pos(int n)
31 return inverse_translate(spk_sel_cons,
32 screen_glyph(spk_sel_cons, n), 0);
35 void speakup_clear_selection(void)
37 sel_start = -1;
40 /* does screen address p correspond to character at LH/RH edge of screen? */
41 static int atedge(const int p, int size_row)
43 return !(p % size_row) || !((p + 2) % size_row);
46 /* constrain v such that v <= u */
47 static unsigned short limit(const unsigned short v, const unsigned short u)
49 return (v > u) ? u : v;
52 int speakup_set_selection(struct tty_struct *tty)
54 int new_sel_start, new_sel_end;
55 char *bp, *obp;
56 int i, ps, pe;
57 struct vc_data *vc = vc_cons[fg_console].d;
59 spk_xs = limit(spk_xs, vc->vc_cols - 1);
60 spk_ys = limit(spk_ys, vc->vc_rows - 1);
61 spk_xe = limit(spk_xe, vc->vc_cols - 1);
62 spk_ye = limit(spk_ye, vc->vc_rows - 1);
63 ps = spk_ys * vc->vc_size_row + (spk_xs << 1);
64 pe = spk_ye * vc->vc_size_row + (spk_xe << 1);
66 if (ps > pe) {
67 /* make sel_start <= sel_end */
68 int tmp = ps;
69 ps = pe;
70 pe = tmp;
73 if (spk_sel_cons != vc_cons[fg_console].d) {
74 speakup_clear_selection();
75 spk_sel_cons = vc_cons[fg_console].d;
76 dev_warn(tty->dev,
77 "Selection: mark console not the same as cut\n");
78 return -EINVAL;
81 new_sel_start = ps;
82 new_sel_end = pe;
84 /* select to end of line if on trailing space */
85 if (new_sel_end > new_sel_start &&
86 !atedge(new_sel_end, vc->vc_size_row) &&
87 ishardspace(sel_pos(new_sel_end))) {
88 for (pe = new_sel_end + 2; ; pe += 2)
89 if (!ishardspace(sel_pos(pe)) ||
90 atedge(pe, vc->vc_size_row))
91 break;
92 if (ishardspace(sel_pos(pe)))
93 new_sel_end = pe;
95 if ((new_sel_start == sel_start) && (new_sel_end == sel_end))
96 return 0; /* no action required */
98 sel_start = new_sel_start;
99 sel_end = new_sel_end;
100 /* Allocate a new buffer before freeing the old one ... */
101 bp = kmalloc((sel_end-sel_start)/2+1, GFP_ATOMIC);
102 if (!bp) {
103 speakup_clear_selection();
104 return -ENOMEM;
106 kfree(sel_buffer);
107 sel_buffer = bp;
109 obp = bp;
110 for (i = sel_start; i <= sel_end; i += 2) {
111 *bp = sel_pos(i);
112 if (!ishardspace(*bp++))
113 obp = bp;
114 if (!((i + 2) % vc->vc_size_row)) {
115 /* strip trailing blanks from line and add newline,
116 unless non-space at end of line. */
117 if (obp != bp) {
118 bp = obp;
119 *bp++ = '\r';
121 obp = bp;
124 sel_buffer_lth = bp - sel_buffer;
125 return 0;
128 struct speakup_paste_work {
129 struct work_struct work;
130 struct tty_struct *tty;
133 static void __speakup_paste_selection(struct work_struct *work)
135 struct speakup_paste_work *spw =
136 container_of(work, struct speakup_paste_work, work);
137 struct tty_struct *tty = xchg(&spw->tty, NULL);
138 struct vc_data *vc = (struct vc_data *) tty->driver_data;
139 int pasted = 0, count;
140 struct tty_ldisc *ld;
141 DECLARE_WAITQUEUE(wait, current);
143 ld = tty_ldisc_ref(tty);
144 if (!ld)
145 goto tty_unref;
146 tty_buffer_lock_exclusive(&vc->port);
148 add_wait_queue(&vc->paste_wait, &wait);
149 while (sel_buffer && sel_buffer_lth > pasted) {
150 set_current_state(TASK_INTERRUPTIBLE);
151 if (test_bit(TTY_THROTTLED, &tty->flags)) {
152 schedule();
153 continue;
155 count = sel_buffer_lth - pasted;
156 count = tty_ldisc_receive_buf(ld, sel_buffer + pasted, NULL,
157 count);
158 pasted += count;
160 remove_wait_queue(&vc->paste_wait, &wait);
161 current->state = TASK_RUNNING;
163 tty_buffer_unlock_exclusive(&vc->port);
164 tty_ldisc_deref(ld);
165 tty_unref:
166 tty_kref_put(tty);
169 static struct speakup_paste_work speakup_paste_work = {
170 .work = __WORK_INITIALIZER(speakup_paste_work.work,
171 __speakup_paste_selection)
174 int speakup_paste_selection(struct tty_struct *tty)
176 if (cmpxchg(&speakup_paste_work.tty, NULL, tty) != NULL)
177 return -EBUSY;
179 tty_kref_get(tty);
180 schedule_work_on(WORK_CPU_UNBOUND, &speakup_paste_work.work);
181 return 0;
184 void speakup_cancel_paste(void)
186 cancel_work_sync(&speakup_paste_work.work);
187 tty_kref_put(speakup_paste_work.tty);