selection.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <asm/cmpxchg.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. if (obp != bp) {
  102. bp = obp;
  103. *bp++ = '\r';
  104. }
  105. obp = bp;
  106. }
  107. }
  108. sel_buffer_lth = bp - sel_buffer;
  109. return 0;
  110. }
  111. struct speakup_paste_work {
  112. struct work_struct work;
  113. struct tty_struct *tty;
  114. };
  115. static void __speakup_paste_selection(struct work_struct *work)
  116. {
  117. struct speakup_paste_work *spw =
  118. container_of(work, struct speakup_paste_work, work);
  119. struct tty_struct *tty = xchg(&spw->tty, NULL);
  120. struct vc_data *vc = (struct vc_data *) tty->driver_data;
  121. int pasted = 0, count;
  122. struct tty_ldisc *ld;
  123. DECLARE_WAITQUEUE(wait, current);
  124. ld = tty_ldisc_ref_wait(tty);
  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 (test_bit(TTY_THROTTLED, &tty->flags)) {
  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. current->state = TASK_RUNNING;
  140. tty_buffer_unlock_exclusive(&vc->port);
  141. tty_ldisc_deref(ld);
  142. tty_kref_put(tty);
  143. }
  144. static struct speakup_paste_work speakup_paste_work = {
  145. .work = __WORK_INITIALIZER(speakup_paste_work.work,
  146. __speakup_paste_selection)
  147. };
  148. int speakup_paste_selection(struct tty_struct *tty)
  149. {
  150. if (cmpxchg(&speakup_paste_work.tty, NULL, tty) != NULL)
  151. return -EBUSY;
  152. tty_kref_get(tty);
  153. schedule_work_on(WORK_CPU_UNBOUND, &speakup_paste_work.work);
  154. return 0;
  155. }
  156. void speakup_cancel_paste(void)
  157. {
  158. cancel_work_sync(&speakup_paste_work.work);
  159. tty_kref_put(speakup_paste_work.tty);
  160. }