forked from brltty/brltty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrl.c
313 lines (256 loc) · 7.68 KB
/
brl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* BRLTTY - A background process providing access to the console screen (when in
* text mode) for a blind person using a refreshable braille display.
*
* Copyright (C) 1995-2022 by The BRLTTY Developers.
*
* BRLTTY comes with ABSOLUTELY NO WARRANTY.
*
* This is free software, placed under the terms of the
* GNU Lesser General Public License, as published by the Free Software
* Foundation; either version 2.1 of the License, or (at your option) any
* later version. Please see the file LICENSE-LGPL for details.
*
* Web Page: http://brltty.app/
*
* This software is maintained by Dave Mielke <[email protected]>.
*/
#include "prologue.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include "log.h"
#include "parameters.h"
#include "charset.h"
#include "unicode.h"
#include "brl.h"
#include "ttb.h"
#include "ktb.h"
#include "queue.h"
#include "async_handle.h"
void
constructBrailleDisplay (BrailleDisplay *brl) {
brl->data = NULL;
brl->refreshBrailleDisplay = NULL;
brl->refreshBrailleRow = NULL;
brl->setBrailleFirmness = NULL;
brl->setTouchSensitivity = NULL;
brl->setAutorepeatProperties = NULL;
brl->textColumns = 0;
brl->textRows = 1;
brl->statusColumns = 0;
brl->statusRows = 0;
brl->cellSize = 8;
brl->keyBindings = "all";
brl->keyNames = NULL;
brl->keyTable = NULL;
brl->gioEndpoint = NULL;
brl->writeDelay = 0;
brl->buffer = NULL;
brl->quality = 0;
brl->isCoreBuffer = 0;
brl->bufferResized = NULL;
brl->resizeRequired = 0;
brl->cursor = BRL_NO_CURSOR;
brl->noDisplay = 0;
brl->hasFailed = 0;
brl->isOffline = 0;
brl->isSuspended = 0;
brl->hideCursor = 0;
brl->acknowledgements.messages = NULL;
brl->acknowledgements.alarm = NULL;
brl->acknowledgements.missing.timeout = BRAILLE_MESSAGE_ACKNOWLEDGEMENT_TIMEOUT;
brl->acknowledgements.missing.count = 0;
brl->acknowledgements.missing.limit = BRAILLE_MESSAGE_UNACKNOWLEDGEED_LIMIT;
}
void
destructBrailleDisplay (BrailleDisplay *brl) {
if (brl->acknowledgements.alarm) {
asyncCancelRequest(brl->acknowledgements.alarm);
brl->acknowledgements.alarm = NULL;
}
if (brl->acknowledgements.messages) {
deallocateQueue(brl->acknowledgements.messages);
brl->acknowledgements.messages = NULL;
}
if (brl->keyTable) {
destroyKeyTable(brl->keyTable);
brl->keyTable = NULL;
}
if (brl->buffer) {
if (brl->isCoreBuffer) free(brl->buffer);
brl->buffer = NULL;
}
}
static void
fillRegion (
wchar_t *text, unsigned char *dots,
unsigned int start, unsigned int count,
unsigned int columns, unsigned int rows,
void *data, unsigned int length,
void (*fill) (wchar_t *text, unsigned char *dots, void *data)
) {
text += start;
dots += start;
while (rows > 0) {
unsigned int index = 0;
size_t amount = length;
if (amount > count) amount = count;
while (index < amount) {
fill(&text[index], &dots[index], data);
index += 1;
}
length -= amount;
amount = count - index;
wmemset(&text[index], WC_C(' '), amount);
memset(&dots[index], 0, amount);
text += columns;
dots += columns;
rows -= 1;
}
}
static void
fillText (wchar_t *text, unsigned char *dots, void *data) {
wchar_t **character = data;
*dots = convertCharacterToDots(textTable, (*text = *(*character)++));
}
void
fillTextRegion (
wchar_t *text, unsigned char *dots,
unsigned int start, unsigned int count,
unsigned int columns, unsigned int rows,
const wchar_t *characters, size_t length
) {
fillRegion(text, dots, start, count, columns, rows, &characters, length, fillText);
}
static void
fillDots (wchar_t *text, unsigned char *dots, void *data) {
unsigned char **cell = data;
*text = UNICODE_BRAILLE_ROW | (*dots = *(*cell)++);
}
void
fillDotsRegion (
wchar_t *text, unsigned char *dots,
unsigned int start, unsigned int count,
unsigned int columns, unsigned int rows,
const unsigned char *cells, size_t length
) {
fillRegion(text, dots, start, count, columns, rows, &cells, length, fillDots);
}
int
setStatusText (BrailleDisplay *brl, const char *text) {
unsigned int length = brl->statusColumns * brl->statusRows;
if (braille->writeStatus && (length > 0)) {
unsigned char cells[length];
{
unsigned int index = 0;
while (index < length) {
char c;
wint_t wc;
if (!(c = text[index])) break;
if ((wc = convertCharToWchar(c)) == WEOF) wc = WC_C('?');
cells[index++] = convertCharacterToDots(textTable, wc);
}
memset(&cells[index], 0, length-index);
}
if (!braille->writeStatus(brl, cells)) return 0;
}
return 1;
}
int
clearStatusCells (BrailleDisplay *brl) {
return setStatusText(brl, "");
}
static void
brailleBufferResized (BrailleDisplay *brl, int infoLevel) {
logMessage(infoLevel,
"Braille Display Dimensions: %d %s, %d %s",
brl->textColumns, ((brl->textColumns == 1)? "column": "columns"),
brl->textRows, ((brl->textRows == 1)? "row": "rows")
);
memset(brl->buffer, 0, brl->textColumns*brl->textRows);
if (brl->bufferResized) brl->bufferResized(brl->textRows, brl->textColumns);
}
static int
resizeBrailleBuffer (BrailleDisplay *brl, int resized, int infoLevel) {
if (brl->resizeRequired) {
brl->resizeRequired = 0;
resized = 1;
if (brl->isCoreBuffer) {
size_t newSize = brl->textColumns * brl->textRows;
unsigned char *newAddress = malloc(newSize);
if (!newAddress) {
logMallocError();
return 0;
}
if (brl->buffer) free(brl->buffer);
brl->buffer = newAddress;
}
}
if (resized) brailleBufferResized(brl, infoLevel);
return 1;
}
int
ensureBrailleBuffer (BrailleDisplay *brl, int infoLevel) {
brl->resizeRequired = brl->isCoreBuffer = !brl->buffer;
if ((brl->noDisplay = !brl->textColumns)) brl->textColumns = 1;
return resizeBrailleBuffer(brl, 1, infoLevel);
}
int
readBrailleCommand (BrailleDisplay *brl, KeyTableCommandContext context) {
int command = braille->readCommand(brl, context);
resizeBrailleBuffer(brl, 0, LOG_INFO);
return command;
}
int
canRefreshBrailleDisplay (BrailleDisplay *brl) {
return brl->refreshBrailleDisplay != NULL;
}
int
refreshBrailleDisplay (BrailleDisplay *brl) {
if (!canRefreshBrailleDisplay(brl)) return 0;
logMessage(LOG_DEBUG, "refreshing braille display");
return brl->refreshBrailleDisplay(brl);
}
int
canRefreshBrailleRow (BrailleDisplay *brl) {
return brl->refreshBrailleRow != NULL;
}
int
refreshBrailleRow (BrailleDisplay *brl, int row) {
if (!canRefreshBrailleRow(brl)) return 0;
logMessage(LOG_DEBUG, "refreshing braille row: %d", row);
return brl->refreshBrailleRow(brl, row);
}
int
canSetBrailleFirmness (BrailleDisplay *brl) {
return brl->setBrailleFirmness != NULL;
}
int
setBrailleFirmness (BrailleDisplay *brl, BrailleFirmness setting) {
if (!canSetBrailleFirmness(brl)) return 0;
logMessage(LOG_DEBUG, "setting braille firmness: %d", setting);
return brl->setBrailleFirmness(brl, setting);
}
int
canSetTouchSensitivity (BrailleDisplay *brl) {
return brl->setTouchSensitivity != NULL;
}
int
setTouchSensitivity (BrailleDisplay *brl, TouchSensitivity setting) {
if (!canSetTouchSensitivity(brl)) return 0;
logMessage(LOG_DEBUG, "setting touch sensitivity: %d", setting);
return brl->setTouchSensitivity(brl, setting);
}
int
canSetAutorepeatProperties (BrailleDisplay *brl) {
return brl->setAutorepeatProperties != NULL;
}
int
setAutorepeatProperties (BrailleDisplay *brl, int on, int delay, int interval) {
if (!canSetAutorepeatProperties(brl)) return 0;
logMessage(LOG_DEBUG, "setting autorepeat properties: %s Delay:%d Interval:%d",
(on? "on": "off"), delay, interval);
return brl->setAutorepeatProperties(brl, on, delay, interval);
}