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
|
/*
* 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-2014 by The BRLTTY Developers.
*
* BRLTTY comes with ABSOLUTELY NO WARRANTY.
*
* This is free software, placed under the terms of the
* GNU General Public License, as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any
* later version. Please see the file LICENSE-GPL for details.
*
* Web Page: http://mielke.cc/brltty/
*
* This software is maintained by Dave Mielke <dave@mielke.cc>.
*/
#include "prologue.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif /* HAVE_SIGNAL_H */
#ifdef SIGUSR1
#include <sys/wait.h>
#endif /* SIGUSR1 */
#include "log.h"
#include "program.h"
#include "timing.h"
#include "scr.h"
#include "routing.h"
/*
* These control the performance of cursor routing. The optimal settings
* will depend heavily on system load, etc. See the documentation for
* further details.
* NOTE: if you try to route the cursor to an invalid place, BRLTTY won't
* give up until the timeout has elapsed!
*/
#define ROUTING_NICENESS 10 /* niceness of cursor routing subprocess */
#define ROUTING_INTERVAL 1 /* how often to check for response */
#define ROUTING_TIMEOUT 2000 /* max wait for response to key press */
typedef enum {
CRR_DONE,
CRR_NEAR,
CRR_FAIL
} RoutingResult;
typedef struct {
#ifdef HAVE_SIGNAL_H
sigset_t signalMask;
#endif /* HAVE_SIGNAL_H */
int screenNumber;
int screenRows;
int screenColumns;
int verticalDelta;
ScreenCharacter *rowBuffer;
int cury, curx;
int oldy, oldx;
long timeSum;
int timeCount;
} RoutingData;
typedef enum {
CURSOR_DIR_LEFT,
CURSOR_DIR_RIGHT,
CURSOR_DIR_UP,
CURSOR_DIR_DOWN
} CursorDirection;
typedef struct {
const char *name;
ScreenKey key;
} CursorDirectionEntry;
static const CursorDirectionEntry cursorDirectionTable[] = {
[CURSOR_DIR_LEFT] = {.name="left" , .key=SCR_KEY_CURSOR_LEFT },
[CURSOR_DIR_RIGHT] = {.name="right", .key=SCR_KEY_CURSOR_RIGHT},
[CURSOR_DIR_UP] = {.name="up" , .key=SCR_KEY_CURSOR_UP },
[CURSOR_DIR_DOWN] = {.name="down" , .key=SCR_KEY_CURSOR_DOWN }
};
typedef enum {
CURSOR_AXIS_HORIZONTAL,
CURSOR_AXIS_VERTICAL
} CursorAxis;
static void
adjustHorizontalCoordinate (int *y, int *x, int amount) {
*x += amount;
}
static void
adjustVerticalCoordinate (int *y, int *x, int amount) {
*y += amount;
}
typedef struct {
const CursorDirectionEntry *forward;
const CursorDirectionEntry *backward;
void (*adjustCoordinate) (int *y, int *x, int amount);
} CursorAxisEntry;
static const CursorAxisEntry cursorAxisTable[] = {
[CURSOR_AXIS_HORIZONTAL] = {
.forward = &cursorDirectionTable[CURSOR_DIR_RIGHT],
.backward = &cursorDirectionTable[CURSOR_DIR_LEFT],
.adjustCoordinate = adjustHorizontalCoordinate
}
,
[CURSOR_AXIS_VERTICAL] = {
.forward = &cursorDirectionTable[CURSOR_DIR_DOWN],
.backward = &cursorDirectionTable[CURSOR_DIR_UP],
.adjustCoordinate = adjustVerticalCoordinate
}
};
#define logRouting(...) logMessage(LOG_CATEGORY(CURSOR_ROUTING), __VA_ARGS__)
static int
readScreenRow (RoutingData *routing, ScreenCharacter *buffer, int row) {
if (!buffer) buffer = routing->rowBuffer;
return readScreen(0, row, routing->screenColumns, 1, buffer);
}
static int
getCurrentPosition (RoutingData *routing) {
ScreenDescription description;
describeScreen(&description);
if (description.number != routing->screenNumber) {
logRouting("screen changed: num=%d", description.number);
routing->screenNumber = description.number;
return 0;
}
if (!routing->rowBuffer) {
routing->screenRows = description.rows;
routing->screenColumns = description.cols;
routing->verticalDelta = 0;
if (!(routing->rowBuffer = calloc(routing->screenColumns, sizeof(*routing->rowBuffer)))) {
logMallocError();
goto error;
}
logRouting("screen: num=%d cols=%d rows=%d",
routing->screenNumber,
routing->screenColumns, routing->screenRows);
} else if ((routing->screenRows != description.rows) ||
(routing->screenColumns != description.cols)) {
logRouting("size changed: cols=%d rows=%d",
description.cols, description.rows);
goto error;
}
routing->cury = description.posy - routing->verticalDelta;
routing->curx = description.posx;
if (readScreenRow(routing, NULL, description.posy)) return 1;
logRouting("read failed: row=%d", description.posy);
error:
routing->screenNumber = -1;
return 0;
}
static void
moveCursor (RoutingData *routing, const CursorDirectionEntry *direction) {
#ifdef SIGUSR1
sigset_t oldMask;
sigprocmask(SIG_BLOCK, &routing->signalMask, &oldMask);
#endif /* SIGUSR1 */
logRouting("move: %s", direction->name);
insertScreenKey(direction->key);
#ifdef SIGUSR1
sigprocmask(SIG_SETMASK, &oldMask, NULL);
#endif /* SIGUSR1 */
}
static int
awaitCursorMotion (RoutingData *routing, int direction, const CursorAxisEntry *axis) {
int moved = 0;
long int timeout = routing->timeSum / routing->timeCount;
TimeValue start;
int trgy = routing->cury;
int trgx = routing->curx;
routing->oldy = routing->cury;
routing->oldx = routing->curx;
axis->adjustCoordinate(&trgy, &trgx, direction);
getMonotonicTime(&start);
while (1) {
long int time;
TimeValue now;
int oldy;
int oldx;
approximateDelay(ROUTING_INTERVAL);
getMonotonicTime(&now);
time = millisecondsBetween(&start, &now) + 1;
{
int row = routing->cury + routing->verticalDelta;
int bestRow = row;
int bestLength = 0;
do {
ScreenCharacter buffer[routing->screenColumns];
if (!readScreenRow(routing, buffer, row)) break;
{
int before = routing->curx;
int after = before;
while (buffer[before].text == routing->rowBuffer[before].text)
if (--before < 0)
break;
while (buffer[after].text == routing->rowBuffer[after].text)
if (++after >= routing->screenColumns)
break;
{
int length = after - before - 1;
if (length > bestLength) {
bestRow = row;
if ((bestLength = length) == routing->screenColumns) break;
}
}
}
row -= direction;
} while ((row >= 0) && (row < routing->screenRows));
routing->verticalDelta = bestRow - routing->cury;
}
oldy = routing->cury;
oldx = routing->curx;
if (!getCurrentPosition(routing)) return 0;
if ((routing->cury != oldy) || (routing->curx != oldx)) {
logRouting("moved: [%d,%d] -> [%d,%d] (%ldms)",
oldx, oldy, routing->curx, routing->cury, time);
if (!moved) {
moved = 1;
timeout = (time * 2) + 1;
routing->timeSum += time * 8;
routing->timeCount += 1;
}
if ((routing->cury == trgy) && (routing->curx == trgx)) break;
if (ROUTING_INTERVAL) {
start = now;
} else {
approximateDelay(1);
getMonotonicTime(&start);
}
} else if (time > timeout) {
if (!moved) logRouting("timed out: %ldms", timeout);
break;
}
}
return 1;
}
static RoutingResult
adjustCursorPosition (RoutingData *routing, int where, int trgy, int trgx, const CursorAxisEntry *axis) {
logRouting("to: [%d,%d]", trgx, trgy);
while (1) {
int dify = trgy - routing->cury;
int difx = (trgx < 0)? 0: (trgx - routing->curx);
int dir;
/* determine which direction the cursor needs to move in */
if (dify) {
dir = (dify > 0)? 1: -1;
} else if (difx) {
dir = (difx > 0)? 1: -1;
} else {
return CRR_DONE;
}
/* tell the cursor to move in the needed direction */
moveCursor(routing, ((dir > 0)? axis->forward: axis->backward));
if (!awaitCursorMotion(routing, dir, axis)) return CRR_FAIL;
if (routing->cury != routing->oldy) {
if (routing->oldy != trgy) {
if (((routing->cury - routing->oldy) * dir) > 0) {
int dif = trgy - routing->cury;
if ((dif * dify) >= 0) continue;
if (where > 0) {
if (routing->cury > trgy) return CRR_NEAR;
} else if (where < 0) {
if (routing->cury < trgy) return CRR_NEAR;
} else {
if ((dif * dif) < (dify * dify)) return CRR_NEAR;
}
}
}
} else if (routing->curx != routing->oldx) {
if (((routing->curx - routing->oldx) * dir) > 0) {
int dif = trgx - routing->curx;
if (routing->cury != trgy) continue;
if ((dif * difx) >= 0) continue;
if (where > 0) {
if (routing->curx > trgx) return CRR_NEAR;
} else if (where < 0) {
if (routing->curx < trgx) return CRR_NEAR;
} else {
if ((dif * dif) < (difx * difx)) return CRR_NEAR;
}
}
} else {
return CRR_NEAR;
}
/* We're getting farther from our target. Before giving up, let's
* try going back to the previous position since it was obviously
* the nearest ever reached.
*/
moveCursor(routing, ((dir > 0)? axis->backward: axis->forward));
return awaitCursorMotion(routing, -dir, axis)? CRR_NEAR: CRR_FAIL;
}
}
static RoutingResult
adjustCursorHorizontally (RoutingData *routing, int where, int row, int column) {
return adjustCursorPosition(routing, where, row, column, &cursorAxisTable[CURSOR_AXIS_HORIZONTAL]);
}
static RoutingResult
adjustCursorVertically (RoutingData *routing, int where, int row) {
return adjustCursorPosition(routing, where, row, -1, &cursorAxisTable[CURSOR_AXIS_VERTICAL]);
}
static RoutingStatus
doRouting (int column, int row, int screen) {
RoutingData routing;
#ifdef SIGUSR1
/* Set up the signal mask. */
sigemptyset(&routing.signalMask);
sigaddset(&routing.signalMask, SIGUSR1);
sigprocmask(SIG_UNBLOCK, &routing.signalMask, NULL);
#endif /* SIGUSR1 */
/* initialize the routing data structure */
routing.screenNumber = screen;
routing.rowBuffer = NULL;
routing.timeSum = ROUTING_TIMEOUT;
routing.timeCount = 1;
if (getCurrentPosition(&routing)) {
logRouting("from: [%d,%d]", routing.curx, routing.cury);
if (column < 0) {
adjustCursorVertically(&routing, 0, row);
} else {
if (adjustCursorVertically(&routing, -1, row) != CRR_FAIL)
if (adjustCursorHorizontally(&routing, 0, row, column) == CRR_NEAR)
if (routing.cury < row)
if (adjustCursorVertically(&routing, 1, routing.cury+1) != CRR_FAIL)
adjustCursorHorizontally(&routing, 0, row, column);
}
}
if (routing.rowBuffer) free(routing.rowBuffer);
if (routing.screenNumber != screen) return ROUTING_ERROR;
if (routing.cury != row) return ROUTING_WRONG_ROW;
if ((column >= 0) && (routing.curx != column)) return ROUTING_WRONG_COLUMN;
return ROUTING_DONE;
}
#ifdef SIGUSR1
#define NOT_ROUTING 0
static pid_t routingProcess = NOT_ROUTING;
int
isRouting (void) {
return routingProcess != NOT_ROUTING;
}
RoutingStatus
getRoutingStatus (int wait) {
if (isRouting()) {
int options = 0;
if (!wait) options |= WNOHANG;
doWait:
{
int status;
pid_t process = waitpid(routingProcess, &status, options);
if (process == routingProcess) {
routingProcess = NOT_ROUTING;
return WIFEXITED(status)? WEXITSTATUS(status): ROUTING_ERROR;
}
if (process == -1) {
if (errno == EINTR) goto doWait;
if (errno == ECHILD) {
routingProcess = NOT_ROUTING;
return ROUTING_ERROR;
}
logSystemError("waitpid");
}
}
}
return ROUTING_NONE;
}
static void
stopRouting (void) {
if (isRouting()) {
kill(routingProcess, SIGUSR1);
getRoutingStatus(1);
}
}
static void
exitCursorRouting (void *data) {
stopRouting();
}
#else /* SIGUSR1 */
static RoutingStatus routingStatus = ROUTING_NONE;
RoutingStatus
getRoutingStatus (int wait) {
RoutingStatus status = routingStatus;
routingStatus = ROUTING_NONE;
return status;
}
int
isRouting (void) {
return 0;
}
#endif /* SIGUSR1 */
int
startRouting (int column, int row, int screen) {
#ifdef SIGUSR1
int started = 0;
stopRouting();
switch (routingProcess = fork()) {
case 0: { /* child: cursor routing subprocess */
int result = ROUTING_ERROR;
if (!ROUTING_INTERVAL) {
int niceness = nice(ROUTING_NICENESS);
if (niceness == -1) {
logSystemError("nice");
}
}
if (constructRoutingScreen()) {
result = doRouting(column, row, screen); /* terminate child process */
destructRoutingScreen(); /* close second thread of screen reading */
}
_exit(result); /* terminate child process */
}
case -1: /* error: fork() failed */
logSystemError("fork");
routingProcess = NOT_ROUTING;
break;
default: /* parent: continue while cursor is being routed */
{
static int first = 1;
if (first) {
first = 0;
onProgramExit("cursor-routing", exitCursorRouting, NULL);
}
}
started = 1;
break;
}
return started;
#else /* SIGUSR1 */
routingStatus = doRouting(column, row, screen);
return 1;
#endif /* SIGUSR1 */
}
|