selection.c 4.6 KB

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