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>
9 #include <asm/cmpxchg.h>
13 /* ------ cut and paste ----- */
14 /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
15 #define ishardspace(c) ((c) == ' ')
17 unsigned short spk_xs, spk_ys, spk_xe, spk_ye; /* our region points */
19 /* Variables for selection control. */
20 /* must not be deallocated */
21 struct vc_data *spk_sel_cons;
22 /* cleared by clear_selection */
23 static int sel_start = -1;
25 static int sel_buffer_lth;
26 static char *sel_buffer;
28 static unsigned char sel_pos(int n)
30 return inverse_translate(spk_sel_cons,
31 screen_glyph(spk_sel_cons, n), 0);
34 void speakup_clear_selection(void)
39 /* does screen address p correspond to character at LH/RH edge of screen? */
40 static int atedge(const int p, int size_row)
42 return !(p % size_row) || !((p + 2) % size_row);
45 /* constrain v such that v <= u */
46 static unsigned short limit(const unsigned short v, const unsigned short u)
48 return (v > u) ? u : v;
51 int speakup_set_selection(struct tty_struct *tty)
53 int new_sel_start, new_sel_end;
56 struct vc_data *vc = vc_cons[fg_console].d;
58 spk_xs = limit(spk_xs, vc->vc_cols - 1);
59 spk_ys = limit(spk_ys, vc->vc_rows - 1);
60 spk_xe = limit(spk_xe, vc->vc_cols - 1);
61 spk_ye = limit(spk_ye, vc->vc_rows - 1);
62 ps = spk_ys * vc->vc_size_row + (spk_xs << 1);
63 pe = spk_ye * vc->vc_size_row + (spk_xe << 1);
66 /* make sel_start <= sel_end */
72 if (spk_sel_cons != vc_cons[fg_console].d) {
73 speakup_clear_selection();
74 spk_sel_cons = vc_cons[fg_console].d;
76 "Selection: mark console not the same as cut\n");
83 /* select to end of line if on trailing space */
84 if (new_sel_end > new_sel_start &&
85 !atedge(new_sel_end, vc->vc_size_row) &&
86 ishardspace(sel_pos(new_sel_end))) {
87 for (pe = new_sel_end + 2; ; pe += 2)
88 if (!ishardspace(sel_pos(pe)) ||
89 atedge(pe, vc->vc_size_row))
91 if (ishardspace(sel_pos(pe)))
94 if ((new_sel_start == sel_start) && (new_sel_end == sel_end))
95 return 0; /* no action required */
97 sel_start = new_sel_start;
98 sel_end = new_sel_end;
99 /* Allocate a new buffer before freeing the old one ... */
100 bp = kmalloc((sel_end-sel_start)/2+1, GFP_ATOMIC);
102 speakup_clear_selection();
109 for (i = sel_start; i <= sel_end; i += 2) {
111 if (!ishardspace(*bp++))
113 if (!((i + 2) % vc->vc_size_row)) {
114 /* strip trailing blanks from line and add newline,
115 unless non-space at end of line. */
123 sel_buffer_lth = bp - sel_buffer;
127 struct speakup_paste_work {
128 struct work_struct work;
129 struct tty_struct *tty;
132 static void __speakup_paste_selection(struct work_struct *work)
134 struct speakup_paste_work *spw =
135 container_of(work, struct speakup_paste_work, work);
136 struct tty_struct *tty = xchg(&spw->tty, NULL);
137 struct vc_data *vc = (struct vc_data *) tty->driver_data;
138 int pasted = 0, count;
139 struct tty_ldisc *ld;
140 DECLARE_WAITQUEUE(wait, current);
142 ld = tty_ldisc_ref_wait(tty);
144 /* FIXME: this is completely unsafe */
145 add_wait_queue(&vc->paste_wait, &wait);
146 while (sel_buffer && sel_buffer_lth > pasted) {
147 set_current_state(TASK_INTERRUPTIBLE);
148 if (test_bit(TTY_THROTTLED, &tty->flags)) {
152 count = sel_buffer_lth - pasted;
153 count = min_t(int, count, tty->receive_room);
154 ld->ops->receive_buf(tty, sel_buffer + pasted, NULL, count);
157 remove_wait_queue(&vc->paste_wait, &wait);
158 current->state = TASK_RUNNING;
164 static struct speakup_paste_work speakup_paste_work = {
165 .work = __WORK_INITIALIZER(speakup_paste_work.work,
166 __speakup_paste_selection)
169 int speakup_paste_selection(struct tty_struct *tty)
171 if (cmpxchg(&speakup_paste_work.tty, NULL, tty) != NULL)
175 schedule_work_on(WORK_CPU_UNBOUND, &speakup_paste_work.work);
179 void speakup_cancel_paste(void)
181 cancel_work_sync(&speakup_paste_work.work);
182 tty_kref_put(speakup_paste_work.tty);