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>
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) == ' ')
16 unsigned short spk_xs, spk_ys, spk_xe, spk_ye; /* our region points */
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;
24 static int sel_buffer_lth;
25 static char *sel_buffer;
27 static unsigned char sel_pos(int n)
29 return inverse_translate(spk_sel_cons,
30 screen_glyph(spk_sel_cons, n), 0);
33 void speakup_clear_selection(void)
38 /* does screen address p correspond to character at LH/RH edge of screen? */
39 static int atedge(const int p, int size_row)
41 return !(p % size_row) || !((p + 2) % size_row);
44 /* constrain v such that v <= u */
45 static unsigned short limit(const unsigned short v, const unsigned short u)
47 return (v > u) ? u : v;
50 int speakup_set_selection(struct tty_struct *tty)
52 int new_sel_start, new_sel_end;
55 struct vc_data *vc = vc_cons[fg_console].d;
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);
65 /* make sel_start <= sel_end */
71 if (spk_sel_cons != vc_cons[fg_console].d) {
72 speakup_clear_selection();
73 spk_sel_cons = vc_cons[fg_console].d;
75 "Selection: mark console not the same as cut\n");
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))
90 if (ishardspace(sel_pos(pe)))
93 if ((new_sel_start == sel_start) && (new_sel_end == sel_end))
94 return 0; /* no action required */
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);
101 speakup_clear_selection();
108 for (i = sel_start; i <= sel_end; i += 2) {
110 if (!ishardspace(*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. */
122 sel_buffer_lth = bp - sel_buffer;
126 struct speakup_paste_work {
127 struct work_struct work;
128 struct tty_struct *tty;
131 static void __speakup_paste_selection(struct work_struct *work)
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);
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)) {
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,
153 remove_wait_queue(&vc->paste_wait, &wait);
154 current->state = TASK_RUNNING;
158 static struct speakup_paste_work speakup_paste_work = {
159 .work = __WORK_INITIALIZER(speakup_paste_work.work,
160 __speakup_paste_selection)
163 int speakup_paste_selection(struct tty_struct *tty)
165 if (cmpxchg(&speakup_paste_work.tty, NULL, tty) != NULL)
169 schedule_work_on(WORK_CPU_UNBOUND, &speakup_paste_work.work);
173 void speakup_cancel_paste(void)
175 cancel_work_sync(&speakup_paste_work.work);
176 tty_kref_put(speakup_paste_work.tty);