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