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