selection.c 4.6 KB

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