0001
0002 #include <linux/slab.h> /* for kmalloc */
0003 #include <linux/consolemap.h>
0004 #include <linux/interrupt.h>
0005 #include <linux/sched.h>
0006 #include <linux/device.h> /* for dev_warn */
0007 #include <linux/selection.h>
0008 #include <linux/workqueue.h>
0009 #include <linux/tty.h>
0010 #include <linux/tty_flip.h>
0011 #include <linux/atomic.h>
0012 #include <linux/console.h>
0013
0014 #include "speakup.h"
0015
0016 unsigned short spk_xs, spk_ys, spk_xe, spk_ye;
0017 struct vc_data *spk_sel_cons;
0018
0019 struct speakup_selection_work {
0020 struct work_struct work;
0021 struct tiocl_selection sel;
0022 struct tty_struct *tty;
0023 };
0024
0025 static void __speakup_set_selection(struct work_struct *work)
0026 {
0027 struct speakup_selection_work *ssw =
0028 container_of(work, struct speakup_selection_work, work);
0029
0030 struct tty_struct *tty;
0031 struct tiocl_selection sel;
0032
0033 sel = ssw->sel;
0034
0035
0036 rmb();
0037
0038
0039 tty = xchg(&ssw->tty, NULL);
0040
0041 if (spk_sel_cons != vc_cons[fg_console].d) {
0042 spk_sel_cons = vc_cons[fg_console].d;
0043 pr_warn("Selection: mark console not the same as cut\n");
0044 goto unref;
0045 }
0046
0047 console_lock();
0048 clear_selection();
0049 console_unlock();
0050
0051 set_selection_kernel(&sel, tty);
0052
0053 unref:
0054 tty_kref_put(tty);
0055 }
0056
0057 static struct speakup_selection_work speakup_sel_work = {
0058 .work = __WORK_INITIALIZER(speakup_sel_work.work,
0059 __speakup_set_selection)
0060 };
0061
0062 int speakup_set_selection(struct tty_struct *tty)
0063 {
0064
0065
0066
0067
0068
0069
0070 tty_kref_get(tty);
0071 if (cmpxchg(&speakup_sel_work.tty, NULL, tty)) {
0072 tty_kref_put(tty);
0073 return -EBUSY;
0074 }
0075
0076
0077
0078
0079 wmb();
0080
0081 speakup_sel_work.sel.xs = spk_xs + 1;
0082 speakup_sel_work.sel.ys = spk_ys + 1;
0083 speakup_sel_work.sel.xe = spk_xe + 1;
0084 speakup_sel_work.sel.ye = spk_ye + 1;
0085 speakup_sel_work.sel.sel_mode = TIOCL_SELCHAR;
0086
0087 schedule_work_on(WORK_CPU_UNBOUND, &speakup_sel_work.work);
0088
0089 return 0;
0090 }
0091
0092 void speakup_cancel_selection(void)
0093 {
0094 struct tty_struct *tty;
0095
0096 cancel_work_sync(&speakup_sel_work.work);
0097
0098
0099
0100
0101 tty = xchg(&speakup_sel_work.tty, NULL);
0102 if (tty)
0103 tty_kref_put(tty);
0104 }
0105
0106 static void __speakup_paste_selection(struct work_struct *work)
0107 {
0108 struct speakup_selection_work *ssw =
0109 container_of(work, struct speakup_selection_work, work);
0110 struct tty_struct *tty = xchg(&ssw->tty, NULL);
0111
0112 paste_selection(tty);
0113 tty_kref_put(tty);
0114 }
0115
0116 static struct speakup_selection_work speakup_paste_work = {
0117 .work = __WORK_INITIALIZER(speakup_paste_work.work,
0118 __speakup_paste_selection)
0119 };
0120
0121 int speakup_paste_selection(struct tty_struct *tty)
0122 {
0123 tty_kref_get(tty);
0124 if (cmpxchg(&speakup_paste_work.tty, NULL, tty)) {
0125 tty_kref_put(tty);
0126 return -EBUSY;
0127 }
0128
0129 schedule_work_on(WORK_CPU_UNBOUND, &speakup_paste_work.work);
0130 return 0;
0131 }
0132
0133 void speakup_cancel_paste(void)
0134 {
0135 struct tty_struct *tty;
0136
0137 cancel_work_sync(&speakup_paste_work.work);
0138 tty = xchg(&speakup_paste_work.tty, NULL);
0139 if (tty)
0140 tty_kref_put(tty);
0141 }