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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
|
/*
* 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-2025 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 <dave@mielke.cc>.
*/
#include "prologue.h"
#include <string.h>
#include "embed.h"
#include "log.h"
#include "log_history.h"
#include "alert.h"
#include "message.h"
#include "defaults.h"
#include "async_wait.h"
#include "async_task.h"
#include "utf8.h"
#include "unicode.h"
#include "brl_utils.h"
#include "brl_cmds.h"
#include "clipboard.h"
#include "spk.h"
#include "ktb_types.h"
#include "ctb.h"
#include "update.h"
#include "cmd_queue.h"
#include "api_control.h"
#include "core.h"
int messageHoldTimeout = DEFAULT_MESSAGE_HOLD_TIMEOUT;
typedef struct {
const char *mode;
MessageOptions options;
unsigned char presented:1;
unsigned char deallocate:1;
char text[0];
} MessageParameters;
typedef struct {
const wchar_t *start;
size_t length;
} MessageSegment;
typedef struct {
const MessageParameters *parameters;
int timeout;
unsigned char endWait:1;
unsigned char hold:1;
unsigned char touch:1;
struct {
const MessageSegment *first;
const MessageSegment *current;
const MessageSegment *last;
} segments;
struct {
ClipboardObject *main;
const wchar_t *start;
size_t offset;
} clipboard;
struct {
const wchar_t *message;
const wchar_t *characters;
int *toMessageOffset;
} braille;
} MessageData;
ASYNC_CONDITION_TESTER(testEndMessageWait) {
const MessageData *mgd = data;
return mgd->endWait;
}
static const wchar_t *
toMessageCharacter (int arg, int end, MessageData *mgd) {
if (arg < 0) return NULL;
int column = arg % brl.textColumns;
int row = arg / brl.textColumns;
if (row >= brl.textRows) return NULL;
if ((column -= textStart) < 0) return NULL;
if (column >= textCount) return NULL;
const MessageSegment *segment = mgd->segments.current;
size_t offset = (row * textCount) + column;
if (offset >= segment->length) {
if (!end) return NULL;
offset = segment->length - 1;
}
const wchar_t *character = segment->start + offset;
offset = character - mgd->braille.characters;
if (mgd->braille.characters != mgd->braille.message) {
const int *toMessageOffset = mgd->braille.toMessageOffset;
if (toMessageOffset) {
if (end) {
while (toMessageOffset[++offset] == CTB_NO_OFFSET);
// we're now at the start of the next contraction
} else {
while (toMessageOffset[offset] == CTB_NO_OFFSET) offset -= 1;
// we're now at the start of the current contraction
}
offset = toMessageOffset[offset];
if (end) offset -= 1;
}
}
return mgd->braille.message + offset;
}
static void
beginClipboardCopy (const wchar_t *start, int append, MessageData *mgd) {
mgd->clipboard.start = start;
mgd->clipboard.offset = append? getClipboardContentLength(mgd->clipboard.main): 0;
alert(ALERT_CLIPBOARD_BEGIN);
}
static int
endClipboardCopy (ClipboardObject *clipboard, const wchar_t *characters, size_t length, int insertCR, MessageData *mgd) {
lockMainClipboard();
int copied = copyClipboardContent(clipboard, characters, length, mgd->clipboard.offset, insertCR);
unlockMainClipboard();
if (!copied) return 0;
alert(ALERT_CLIPBOARD_END);
onMainClipboardUpdated();
return 1;
}
static int
handleMessageCommands (int command, void *data) {
MessageData *mgd = data;
switch (command & BRL_MSK_CMD) {
case BRL_CMD_LNUP:
case BRL_CMD_PRDIFLN:
case BRL_CMD_PRNBWIN:
case BRL_CMD_FWINLTSKIP:
case BRL_CMD_FWINLT: {
if (mgd->segments.current > mgd->segments.first) {
mgd->segments.current -= 1;
mgd->endWait = 1;
} else {
alert(ALERT_BOUNCE);
}
mgd->hold = 1;
return 1;
}
case BRL_CMD_LNDN:
case BRL_CMD_NXDIFLN:
case BRL_CMD_NXNBWIN:
case BRL_CMD_FWINRTSKIP:
case BRL_CMD_FWINRT: {
if ((mgd->hold = mgd->segments.current < mgd->segments.last)) {
mgd->segments.current += 1;
}
break;
}
default: {
int arg1 = BRL_CODE_GET(ARG, command);
int arg2 = BRL_CODE_GET(EXT, command);
switch (command & BRL_MSK_BLK) {
case BRL_CMD_BLK(TOUCH_AT):
if ((mgd->touch = arg1 != BRL_MSK_ARG)) return 1;
mgd->timeout = 1000;
break;
{
int append;
case BRL_CMD_BLK(CLIP_COPY):
append = 0;
goto doClipCopy;
case BRL_CMD_BLK(CLIP_APPEND):
append = 1;
goto doClipCopy;
doClipCopy:
{
const wchar_t *first = toMessageCharacter(arg1, 0, mgd);
if (first) {
const wchar_t *last = toMessageCharacter(arg2, 1, mgd);
if (last) {
beginClipboardCopy(first, append, mgd);
size_t count = last - first + 1;
if (endClipboardCopy(mgd->clipboard.main, first, count, 0, mgd)) {
mgd->hold = 1;
return 1;
}
}
}
}
alert(ALERT_COMMAND_REJECTED);
return 1;
}
{
int append;
case BRL_CMD_BLK(CLIP_NEW):
append = 0;
goto doClipBegin;
case BRL_CMD_BLK(CLIP_ADD):
append = 1;
goto doClipBegin;
doClipBegin:
{
const wchar_t *first = toMessageCharacter(arg1, 0, mgd);
if (first) {
beginClipboardCopy(first, append, mgd);
mgd->hold = 1;
return 1;
}
}
alert(ALERT_COMMAND_REJECTED);
return 1;
}
{
int insertCR;
case BRL_CMD_BLK(COPY_RECT):
insertCR = 1;
goto doClipEnd;
case BRL_CMD_BLK(COPY_LINE):
insertCR = 0;
goto doClipEnd;
doClipEnd:
{
const wchar_t *first = mgd->clipboard.start;
if (first) {
const wchar_t *last = toMessageCharacter(arg1, 1, mgd);
if (last) {
if (last > first) {
size_t count = last - first + 1;
if (endClipboardCopy(mgd->clipboard.main, first, count, insertCR, mgd)) {
mgd->hold = 1;
return 1;
}
}
}
}
}
alert(ALERT_COMMAND_REJECTED);
return 1;
}
default:
mgd->hold = 0;
break;
}
break;
}
}
mgd->endWait = 1;
return 1;
}
int
sayMessage (const char *text) {
#ifdef ENABLE_SPEECH_SUPPORT
if (isAutospeakActive()) {
if (!sayString(&spk, text, SAY_OPT_MUTE_FIRST)) {
return 0;
}
}
#endif /* ENABLE_SPEECH_SUPPORT */
return 1;
}
static const MessageSegment *
makeSegments (MessageSegment *segment, const wchar_t *characters, size_t characterCount, int wordWrap) {
const size_t windowLength = textCount * brl.textRows;
const wchar_t *character = characters;
const wchar_t *const charactersEnd = character + characterCount;
while (*character) {
/* strip leading spaces */
while ((character < charactersEnd) && iswspace(*character)) character += 1;
const size_t charactersLeft = charactersEnd - character;
if (!charactersLeft) break;
segment->start = character;
if (charactersLeft <= windowLength) {
segment->length = charactersLeft;
} else {
segment->length = windowLength;
if (wordWrap) {
const wchar_t *segmentEnd = segment->start + segment->length;
if (!iswspace(*segmentEnd)) {
while (--segmentEnd >= segment->start) {
if (iswspace(*segmentEnd)) break;
}
if (segmentEnd > segment->start) {
segment->length = segmentEnd - segment->start + 1;
}
}
}
}
character += segment->length;
segment += 1;
}
return segment - 1;
}
static wchar_t *
makeBraille (const wchar_t *text, size_t textLength, size_t *brailleLength, int **offsetMap) {
wchar_t *braille = NULL;
*brailleLength = 0;
*offsetMap = NULL;
int cellsSize = MAX(0X10, textLength) * 2;
int cellsLimit = textLength * 10;
do {
unsigned char cells[cellsSize];
int toCellOffset[textLength + 1];
int textCount = textLength;
int cellCount = cellsSize;
contractText(
contractionTable, NULL,
text, &textCount,
cells, &cellCount,
toCellOffset, CTB_NO_CURSOR
);
// this sholdn't happen
if (textCount > textLength) break;
if (textCount == textLength) {
toCellOffset[textCount] = cellCount;
int *toTextOffset = makeInverseOffsetMap(toCellOffset, textLength);
if (!toTextOffset) break;
if ((braille = allocateCharacters(cellCount + 1))) {
const unsigned char *cell = cells;
const unsigned char *end = cell + cellCount;
wchar_t *character = braille;
while (cell < end) {
unsigned char dots = *cell++;
*character++ = dots? (dots | UNICODE_BRAILLE_ROW): WC_C(' ');
}
*character = 0;
*brailleLength = character - braille;
*offsetMap = toTextOffset;
} else {
free(toTextOffset);
}
break;
}
} while ((cellsSize <<= 1) <= cellsLimit);
return braille;
}
ASYNC_TASK_CALLBACK(presentMessage) {
MessageParameters *mgp = data;
if (!(mgp->options & MSG_SILENT)) {
sayMessage(mgp->text);
}
if (canBraille()) {
MessageData mgd = {
.parameters = mgp,
.hold = 0,
.touch = 0,
.clipboard = {
.main = getMainClipboard(),
.start = NULL,
.offset = 0,
},
};
const size_t textLength = countUtf8Characters(mgp->text);
wchar_t text[textLength + 1];
makeWcharsFromUtf8(mgp->text, text, ARRAY_COUNT(text));
mgd.braille.message = text;
mgd.braille.characters = text;
mgd.braille.toMessageOffset = NULL;
wchar_t *characters = text;
size_t characterCount = textLength;
int wordWrap = prefs.wordWrap;
if (isContracting()) {
size_t brailleLength;
int *offsetMap;
wchar_t *braille = makeBraille(text, textLength, &brailleLength, &offsetMap);
if (braille) {
characters = braille;
characterCount = brailleLength;
wordWrap = 1;
mgd.braille.characters = braille;
mgd.braille.toMessageOffset = offsetMap;
}
}
MessageSegment messageSegments[characterCount];
mgd.segments.current = mgd.segments.first = messageSegments;
mgd.segments.last = makeSegments(messageSegments, characters, characterCount, wordWrap);
mgd.clipboard.start = mgd.segments.first->start;
int apiWasLinked = api.isServerLinked();
if (apiWasLinked) api.unlinkServer();
suspendUpdates();
pushCommandEnvironment("message", NULL, NULL);
pushCommandHandler("message", KTB_CTX_WAITING,
handleMessageCommands, NULL, &mgd);
while (1) {
const MessageSegment *segment = mgd.segments.current;
int isLastSegment = segment == mgd.segments.last;
brl.cursor = BRL_NO_CURSOR;
if (!writeBrailleCharacters(mgp->mode, segment->start, segment->length)) {
mgp->presented = 0;
break;
}
mgd.timeout = messageHoldTimeout - brl.writeDelay;
drainBrailleOutput(&brl, 0);
if (!mgd.hold && isLastSegment && (mgp->options & MSG_NODELAY)) break;
mgd.timeout = MAX(mgd.timeout, 0);
while (1) {
int timeout = mgd.timeout;
mgd.timeout = -1;
mgd.endWait = 0;
int timedOut = !asyncAwaitCondition(timeout, testEndMessageWait, &mgd);
if (mgd.segments.current != segment) break;
if (mgd.hold || mgd.touch) {
mgd.timeout = 1000000;
} else if (timedOut) {
if (isLastSegment) goto DONE;
mgd.segments.current += 1;
mgd.timeout = messageHoldTimeout;
break;
} else if (mgd.timeout < 0) {
goto DONE;
}
}
}
DONE:
if (characters != text) free(characters);
if (mgd.braille.toMessageOffset) free(mgd.braille.toMessageOffset);
popCommandEnvironment();
resumeUpdates(1);
if (apiWasLinked) api.linkServer();
}
if (mgp->deallocate) free(mgp);
}
int
message (const char *mode, const char *text, MessageOptions options) {
if (options & MSG_LOG) pushLogMessage(text);
int presented = 0;
MessageParameters *mgp;
size_t size = sizeof(*mgp) + strlen(text) + 1;
if ((mgp = malloc(size))) {
memset(mgp, 0, size);
mgp->mode = mode? mode: "";
mgp->options = options;
mgp->presented = 1;
strcpy(mgp->text, text);
if (mgp->options & MSG_SYNC) {
mgp->deallocate = 0;
presentMessage(mgp);
if (mgp->presented) presented = 1;
} else {
mgp->deallocate = 1;
if (asyncAddTask(NULL, presentMessage, mgp)) return 1;
}
free(mgp);
}
return presented;
}
void
showMessage (const char *text) {
message(NULL, text, 0);
}
|