buffers.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <linux/console.h>
  2. #include <linux/types.h>
  3. #include <linux/wait.h>
  4. #include "speakup.h"
  5. #include "spk_priv.h"
  6. #define SYNTH_BUF_SIZE 8192 /* currently 8K bytes */
  7. static u_char synth_buffer[SYNTH_BUF_SIZE]; /* guess what this is for! */
  8. static u_char *buff_in = synth_buffer;
  9. static u_char *buff_out = synth_buffer;
  10. static u_char *buffer_end = synth_buffer + SYNTH_BUF_SIZE - 1;
  11. /* These try to throttle applications by stopping the TTYs
  12. * Note: we need to make sure that we will restart them eventually, which is
  13. * usually not possible to do from the notifiers. TODO: it should be possible
  14. * starting from linux 2.6.26.
  15. *
  16. * So we only stop when we know alive == 1 (else we discard the data anyway),
  17. * and the alive synth will eventually call start_ttys from the thread context.
  18. */
  19. void speakup_start_ttys(void)
  20. {
  21. int i;
  22. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  23. if (speakup_console[i] && speakup_console[i]->tty_stopped)
  24. continue;
  25. if ((vc_cons[i].d != NULL) && (vc_cons[i].d->port.tty != NULL))
  26. start_tty(vc_cons[i].d->port.tty);
  27. }
  28. }
  29. EXPORT_SYMBOL_GPL(speakup_start_ttys);
  30. static void speakup_stop_ttys(void)
  31. {
  32. int i;
  33. for (i = 0; i < MAX_NR_CONSOLES; i++)
  34. if ((vc_cons[i].d != NULL) && (vc_cons[i].d->port.tty != NULL))
  35. stop_tty(vc_cons[i].d->port.tty);
  36. }
  37. static int synth_buffer_free(void)
  38. {
  39. int bytes_free;
  40. if (buff_in >= buff_out)
  41. bytes_free = SYNTH_BUF_SIZE - (buff_in - buff_out);
  42. else
  43. bytes_free = buff_out - buff_in;
  44. return bytes_free;
  45. }
  46. int synth_buffer_empty(void)
  47. {
  48. return (buff_in == buff_out);
  49. }
  50. EXPORT_SYMBOL_GPL(synth_buffer_empty);
  51. void synth_buffer_add(char ch)
  52. {
  53. if (!synth->alive) {
  54. /* This makes sure that we won't stop TTYs if there is no synth
  55. * to restart them
  56. */
  57. return;
  58. }
  59. if (synth_buffer_free() <= 100) {
  60. synth_start();
  61. speakup_stop_ttys();
  62. }
  63. if (synth_buffer_free() <= 1)
  64. return;
  65. *buff_in++ = ch;
  66. if (buff_in > buffer_end)
  67. buff_in = synth_buffer;
  68. }
  69. char synth_buffer_getc(void)
  70. {
  71. char ch;
  72. if (buff_out == buff_in)
  73. return 0;
  74. ch = *buff_out++;
  75. if (buff_out > buffer_end)
  76. buff_out = synth_buffer;
  77. return ch;
  78. }
  79. EXPORT_SYMBOL_GPL(synth_buffer_getc);
  80. char synth_buffer_peek(void)
  81. {
  82. if (buff_out == buff_in)
  83. return 0;
  84. return *buff_out;
  85. }
  86. EXPORT_SYMBOL_GPL(synth_buffer_peek);
  87. void synth_buffer_clear(void)
  88. {
  89. buff_in = synth_buffer;
  90. buff_out = synth_buffer;
  91. }
  92. EXPORT_SYMBOL_GPL(synth_buffer_clear);