blob: b7b60d5e866023d461a8661a2fa278330ff8a5de [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;
Greg Kroah-Hartman5b192082010-10-07 19:30:49 -070031 if ((vc_cons[i].d != NULL) && (vc_cons[i].d->port.tty != NULL))
32 start_tty(vc_cons[i].d->port.tty);
William Hubbsc6e3fd22010-10-07 13:20:02 -050033 }
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++)
Greg Kroah-Hartman5b192082010-10-07 19:30:49 -070042 if ((vc_cons[i].d != NULL) && (vc_cons[i].d->port.tty != NULL))
43 stop_tty(vc_cons[i].d->port.tty);
William Hubbsc6e3fd22010-10-07 13:20:02 -050044}
45
46static int synth_buffer_free(void)
47{
48 int bytesFree;
49
50 if (buff_in >= buff_out)
51 bytesFree = synthBufferSize - (buff_in - buff_out);
52 else
53 bytesFree = buff_out - buff_in;
54 return bytesFree;
55}
56
57int synth_buffer_empty(void)
58{
59 return (buff_in == buff_out);
60}
61EXPORT_SYMBOL_GPL(synth_buffer_empty);
62
63void synth_buffer_add(char ch)
64{
65 if (!synth->alive) {
66 /* This makes sure that we won't stop TTYs if there is no synth
67 * to restart them */
68 return;
69 }
70 if (synth_buffer_free() <= 100) {
71 synth_start();
72 speakup_stop_ttys();
73 }
74 if (synth_buffer_free() <= 1)
75 return;
76 *buff_in++ = ch;
77 if (buff_in > buffer_end)
78 buff_in = synth_buffer;
79}
80
81char synth_buffer_getc(void)
82{
83 char ch;
84
85 if (buff_out == buff_in)
86 return 0;
87 ch = *buff_out++;
88 if (buff_out > buffer_end)
89 buff_out = synth_buffer;
90 return ch;
91}
92EXPORT_SYMBOL_GPL(synth_buffer_getc);
93
94char synth_buffer_peek(void)
95{
96 if (buff_out == buff_in)
97 return 0;
98 return *buff_out;
99}
100EXPORT_SYMBOL_GPL(synth_buffer_peek);
101
102void synth_buffer_clear(void)
103{
104 buff_in = buff_out = synth_buffer;
105 return;
106}
107EXPORT_SYMBOL_GPL(synth_buffer_clear);