Path: blob/master/drivers/accessibility/speakup/buffers.c
26282 views
// SPDX-License-Identifier: GPL-2.01#include <linux/console.h>2#include <linux/types.h>3#include <linux/wait.h>45#include "speakup.h"6#include "spk_priv.h"78#define SYNTH_BUF_SIZE 8192 /* currently 8K bytes */910static u16 synth_buffer[SYNTH_BUF_SIZE]; /* guess what this is for! */11static u16 *buff_in = synth_buffer;12static u16 *buff_out = synth_buffer;13static u16 *buffer_end = synth_buffer + SYNTH_BUF_SIZE - 1;1415/* These try to throttle applications by stopping the TTYs16* Note: we need to make sure that we will restart them eventually, which is17* usually not possible to do from the notifiers. TODO: it should be possible18* 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*/23void speakup_start_ttys(void)24{25int i;2627for (i = 0; i < MAX_NR_CONSOLES; i++) {28if (speakup_console[i] && speakup_console[i]->tty_stopped)29continue;30if (vc_cons[i].d && vc_cons[i].d->port.tty)31start_tty(vc_cons[i].d->port.tty);32}33}34EXPORT_SYMBOL_GPL(speakup_start_ttys);3536static void speakup_stop_ttys(void)37{38int i;3940for (i = 0; i < MAX_NR_CONSOLES; i++)41if (vc_cons[i].d && vc_cons[i].d->port.tty)42stop_tty(vc_cons[i].d->port.tty);43}4445static int synth_buffer_free(void)46{47int chars_free;4849if (buff_in >= buff_out)50chars_free = SYNTH_BUF_SIZE - (buff_in - buff_out);51else52chars_free = buff_out - buff_in;53return chars_free;54}5556int synth_buffer_empty(void)57{58return (buff_in == buff_out);59}60EXPORT_SYMBOL_GPL(synth_buffer_empty);6162void synth_buffer_add(u16 ch)63{64if (!synth->alive) {65/* This makes sure that we won't stop TTYs if there is no synth66* to restart them67*/68return;69}70if (synth_buffer_free() <= 100) {71synth_start();72speakup_stop_ttys();73}74if (synth_buffer_free() <= 1)75return;76*buff_in++ = ch;77if (buff_in > buffer_end)78buff_in = synth_buffer;79/* We have written something to the speech synthesis, so we are not80* paused any more.81*/82spk_paused = false;83}8485u16 synth_buffer_getc(void)86{87u16 ch;8889if (buff_out == buff_in)90return 0;91ch = *buff_out++;92if (buff_out > buffer_end)93buff_out = synth_buffer;94return ch;95}96EXPORT_SYMBOL_GPL(synth_buffer_getc);9798u16 synth_buffer_peek(void)99{100if (buff_out == buff_in)101return 0;102return *buff_out;103}104EXPORT_SYMBOL_GPL(synth_buffer_peek);105106void synth_buffer_skip_nonlatin1(void)107{108while (buff_out != buff_in) {109if (*buff_out < 0x100)110return;111buff_out++;112if (buff_out > buffer_end)113buff_out = synth_buffer;114}115}116EXPORT_SYMBOL_GPL(synth_buffer_skip_nonlatin1);117118void synth_buffer_clear(void)119{120buff_in = synth_buffer;121buff_out = synth_buffer;122}123EXPORT_SYMBOL_GPL(synth_buffer_clear);124125126