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 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
|
/*
* libbrlapi - A library providing access to braille terminals for applications.
*
* Copyright (C) 2006-2025 by Dave Mielke <dave@mielke.cc>
*
* libbrlapi 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>.
*/
#define BRLAPI_NO_SINGLE_SESSION 1
#include "prologue.h"
#include <brlapi.h>
#include <brl_cmds.h>
#include <errno.h>
#include <lua.h>
#include <lauxlib.h>
static const char *handle_t = "brlapi";
#define checkhandle(L, arg) (brlapi_handle_t *)luaL_checkudata(L, (arg), handle_t)
static void error(lua_State *L) {
lua_pushstring(L, brlapi_strerror(&brlapi_error));
lua_error(L);
}
static int openConnection(lua_State *L) {
const brlapi_connectionSettings_t desiredSettings = {
.host = luaL_optstring(L, 1, NULL), .auth = luaL_optstring(L, 2, NULL)
};
brlapi_connectionSettings_t actualSettings;
brlapi_handle_t *const handle = (brlapi_handle_t *)lua_newuserdatauv(
L, brlapi_getHandleSize(), 0
);
luaL_setmetatable(L, handle_t);
if (brlapi__openConnection(handle, &desiredSettings, &actualSettings) == -1)
error(L);
lua_pushstring(L, actualSettings.host);
lua_pushstring(L, actualSettings.auth);
return 3;
}
static int getFileDescriptor(lua_State *L) {
const int fileDescriptor = brlapi__getFileDescriptor(checkhandle(L, 1));
if (fileDescriptor == BRLAPI_INVALID_FILE_DESCRIPTOR) error(L);
lua_pushinteger(L, fileDescriptor);
return 1;
}
static int closeConnection(lua_State *L) {
brlapi__closeConnection(checkhandle(L, 1));
return 0;
}
static int getDriverName(lua_State *L) {
char name[BRLAPI_MAXNAMELENGTH + 1];
if (brlapi__getDriverName(checkhandle(L, 1), name, sizeof(name)) == -1)
error(L);
lua_pushstring(L, name);
return 1;
}
static int getModelIdentifier(lua_State *L) {
brlapi_handle_t *const handle = checkhandle(L, 1);
char ident[BRLAPI_MAXNAMELENGTH + 1];
if (brlapi__getModelIdentifier(handle, ident, sizeof(ident)) == -1) error(L);
lua_pushstring(L, ident);
return 1;
}
static int getDisplaySize(lua_State *L) {
unsigned int x, y;
if (brlapi__getDisplaySize(checkhandle(L, 1), &x, &y) == -1) error(L);
lua_pushinteger(L, x), lua_pushinteger(L, y);
return 2;
}
static int enterTtyMode(lua_State *L) {
const int result = brlapi__enterTtyMode(checkhandle(L, 1),
luaL_optinteger(L, 2, BRLAPI_TTY_DEFAULT), luaL_optstring(L, 3, NULL)
);
if (result == -1) error(L);
lua_pushinteger(L, result);
return 1;
}
static int leaveTtyMode(lua_State *L) {
if (brlapi__leaveTtyMode(checkhandle(L, 1)) == -1) error(L);
return 0;
}
static int setFocus(lua_State *L) {
if (brlapi__setFocus(checkhandle(L, 1), luaL_checkinteger(L, 2)) == -1)
error(L);
return 0;
}
static int writeText(lua_State *L) {
brlapi_handle_t *const handle = checkhandle(L, 1);
const int cursor = luaL_checkinteger(L, 2);
const char *const text = luaL_checkstring(L, 3);
if (brlapi__writeText(handle, cursor, text) == -1) error(L);
return 0;
}
static int writeDots(lua_State *L) {
brlapi_handle_t *const handle = checkhandle(L, 1);
const unsigned char *const dots = (const unsigned char *)luaL_checkstring(L, 2);
if (brlapi__writeDots(handle, dots) == -1) error(L);
return 0;
}
static int readKey(lua_State *L) {
brlapi_handle_t *const handle = checkhandle(L, 1);
const int wait = lua_toboolean(L, 2);
brlapi_keyCode_t keyCode;
int result;
do {
result = brlapi__readKey(handle, wait, &keyCode);
} while (result == -1 &&
brlapi_errno == BRLAPI_ERROR_LIBCERR && brlapi_libcerrno == EINTR);
switch (result) {
case -1:
error(L);
break; /* never reached */
case 0:
break;
case 1:
lua_pushinteger(L, (lua_Integer)keyCode);
return 1;
}
return 0;
}
static int readKeyWithTimeout(lua_State *L) {
brlapi_handle_t *const handle = checkhandle(L, 1);
const int timeout_ms = luaL_checkinteger(L, 2);
brlapi_keyCode_t keyCode;
int result;
do {
result = brlapi__readKeyWithTimeout(handle, timeout_ms, &keyCode);
} while (result == -1 &&
brlapi_errno == BRLAPI_ERROR_LIBCERR && brlapi_libcerrno == EINTR);
switch (result) {
case -1:
error(L);
break; /* never reached */
case 0:
break;
case 1:
lua_pushinteger(L, (lua_Integer)keyCode);
return 1;
}
return 0;
}
static int expandKeyCode(lua_State *L) {
brlapi_keyCode_t keyCode = (brlapi_keyCode_t)luaL_checkinteger(L, 1);
brlapi_expandedKeyCode_t expansion;
brlapi_expandKeyCode(keyCode, &expansion);
lua_pushinteger(L, expansion.type),
lua_pushinteger(L, expansion.command),
lua_pushinteger(L, expansion.argument),
lua_pushinteger(L, expansion.flags);
return 4;
}
static int describeKeyCode(lua_State *L) {
brlapi_keyCode_t keyCode = (brlapi_keyCode_t)luaL_checkinteger(L, 1);
brlapi_describedKeyCode_t description;
brlapi_describeKeyCode(keyCode, &description);
lua_pushstring(L, description.type),
lua_pushstring(L, description.command),
lua_pushinteger(L, description.argument);
lua_createtable(L, description.flags, 0);
for (size_t i = 0; i < description.flags; i += 1) {
lua_pushstring(L, description.flag[i]);
lua_rawseti(L, -2, i + 1);
}
return 4;
}
static int enterRawMode(lua_State *L) {
brlapi_handle_t *const handle = checkhandle(L, 1);
const char *const driverName = luaL_checkstring(L, 2);
if (brlapi__enterRawMode(handle, driverName) == -1) error(L);
return 0;
}
static int leaveRawMode(lua_State *L) {
brlapi_handle_t *const handle = checkhandle(L, 1);
if (brlapi__leaveRawMode(handle) == -1) error(L);
return 0;
}
static int changeKeys(
lua_State *L,
int BRLAPI_STDCALL (*change) (brlapi_handle_t *, brlapi_rangeType_t, const brlapi_keyCode_t[], unsigned int)
) {
brlapi_handle_t *const handle = checkhandle(L, 1);
static const char *rangeNames[] = {
"all", "code", "command", "key", "type", NULL
};
static const brlapi_rangeType_t rangeTypes[] = {
brlapi_rangeType_all,
brlapi_rangeType_code,
brlapi_rangeType_command,
brlapi_rangeType_key,
brlapi_rangeType_type
};
const brlapi_rangeType_t range = rangeTypes[
luaL_checkoption(L, 2, NULL, rangeNames)
];
if (range == brlapi_rangeType_all) {
if (change(handle, range, NULL, 0) == -1) error(L);
return 0;
}
luaL_checktype(L, 3, LUA_TTABLE);
lua_len(L, 3);
{
const size_t count = lua_tointeger(L, -1);
brlapi_keyCode_t keys[count];
lua_pop(L, 1);
for (size_t i = 0; i < count; i += 1) {
lua_geti(L, 3, i + 1);
keys[i] = (brlapi_keyCode_t)luaL_checkinteger(L, -1);
lua_pop(L, 1);
}
if (change(handle, range, keys, count) == -1) error(L);
}
return 0;
}
static int acceptKeys(lua_State *L) {
return changeKeys(L, brlapi__acceptKeys);
}
static int ignoreKeys(lua_State *L) {
return changeKeys(L, brlapi__ignoreKeys);
}
static int acceptAllKeys(lua_State *L) {
if (brlapi__acceptAllKeys(checkhandle(L, 1)) == -1) error(L);
return 0;
}
static int ignoreAllKeys(lua_State *L) {
if (brlapi__ignoreAllKeys(checkhandle(L, 1)) == -1) error(L);
return 0;
}
static int suspendDriver(lua_State *L) {
brlapi_handle_t *const handle = checkhandle(L, 1);
const char *const driverName = luaL_checkstring(L, 2);
if (brlapi__suspendDriver(handle, driverName) == -1) error(L);
return 0;
}
static int resumeDriver(lua_State *L) {
if (brlapi__resumeDriver(checkhandle(L, 1)) == -1) error(L);
return 0;
}
static int pause_(lua_State *L) {
brlapi_handle_t *const handle = checkhandle(L, 1);
const int timeout_ms = luaL_checkinteger(L, 2);
int result;
do {
result = brlapi__pause(handle, timeout_ms);
} while (timeout_ms == -1 && result == -1 &&
brlapi_errno == BRLAPI_ERROR_LIBCERR && brlapi_libcerrno == EINTR);
if (result == -1) error(L);
return 0;
}
static int getBooleanParameter(lua_State *L, brlapi_param_t param) {
brlapi_param_bool_t value;
const int result = brlapi__getParameter(checkhandle(L, 1),
param, 0, BRLAPI_PARAMF_GLOBAL, &value, sizeof(value)
);
if (result == -1) error(L);
lua_pushboolean(L, value);
return 1;
}
static int setBooleanParameter(lua_State *L, brlapi_param_t param) {
brlapi_handle_t *const handle = checkhandle(L, 1);
const brlapi_param_flags_t flags = BRLAPI_PARAMF_GLOBAL;
const brlapi_param_bool_t value = lua_toboolean(L, 2);
if (brlapi__setParameter(handle, param, 0, flags, &value, sizeof(value)) == -1)
error(L);
return 0;
}
static int getStringParameter(
lua_State *L,
brlapi_param_t param, brlapi_param_subparam_t subparam
) {
size_t count;
char *string = brlapi__getParameterAlloc(checkhandle(L, 1),
param, subparam, BRLAPI_PARAMF_GLOBAL, &count
);
if (string == NULL) error(L);
lua_pushlstring(L, string, count);
free(string);
return 1;
}
static int setStringParameter(
lua_State *L,
brlapi_param_t param, brlapi_param_subparam_t subparam
) {
brlapi_handle_t *const handle = checkhandle(L, 1);
size_t length;
const brlapi_param_flags_t flags = BRLAPI_PARAMF_GLOBAL;
const char *string = luaL_checklstring(L, 2, &length);
if (brlapi__setParameter(handle, param, subparam, flags, string, length) == -1)
error(L);
return 0;
}
static int getDriverCode(lua_State *L) {
return getStringParameter(L, BRLAPI_PARAM_DRIVER_CODE, 0);
}
static int getDriverVersion(lua_State *L) {
return getStringParameter(L, BRLAPI_PARAM_DRIVER_VERSION, 0);
}
static int getDeviceOnline(lua_State *L) {
return getBooleanParameter(L, BRLAPI_PARAM_DEVICE_ONLINE);
}
static int getAudibleAlerts(lua_State *L) {
return getBooleanParameter(L, BRLAPI_PARAM_AUDIBLE_ALERTS);
}
static int setAudibleAlerts(lua_State *L) {
return setBooleanParameter(L, BRLAPI_PARAM_AUDIBLE_ALERTS);
}
static int getClipboardContent(lua_State *L) {
return getStringParameter(L, BRLAPI_PARAM_CLIPBOARD_CONTENT, 0);
}
static int setClipboardContent(lua_State *L) {
return setStringParameter(L, BRLAPI_PARAM_CLIPBOARD_CONTENT, 0);
}
static int getComputerBrailleTable(lua_State *L) {
return getStringParameter(L, BRLAPI_PARAM_COMPUTER_BRAILLE_TABLE, 0);
}
static int setComputerBrailleTable(lua_State *L) {
return setStringParameter(L, BRLAPI_PARAM_COMPUTER_BRAILLE_TABLE, 0);
}
static int getLiteraryBrailleTable(lua_State *L) {
return getStringParameter(L, BRLAPI_PARAM_LITERARY_BRAILLE_TABLE, 0);
}
static int setLiteraryBrailleTable(lua_State *L) {
return setStringParameter(L, BRLAPI_PARAM_LITERARY_BRAILLE_TABLE, 0);
}
static int getMessageLocale(lua_State *L) {
return getStringParameter(L, BRLAPI_PARAM_MESSAGE_LOCALE, 0);
}
static int setMessageLocale(lua_State *L) {
return setStringParameter(L, BRLAPI_PARAM_MESSAGE_LOCALE, 0);
}
static int getBoundCommandKeycodes(lua_State *L) {
size_t count;
brlapi_keyCode_t *codes = brlapi__getParameterAlloc(checkhandle(L, 1),
BRLAPI_PARAM_BOUND_COMMAND_KEYCODES, 0, BRLAPI_PARAMF_GLOBAL, &count
);
if (codes == NULL) error(L);
count /= sizeof(brlapi_keyCode_t);
lua_newtable(L);
for (size_t i = 0; i < count; i += 1) {
lua_pushinteger(L, (lua_Integer)codes[i]);
lua_rawseti(L, -2, i + 1);
}
free(codes);
return 1;
}
static int getCommandKeycodeName(lua_State *L) {
const brlapi_keyCode_t keyCode = (brlapi_keyCode_t)luaL_checkinteger(L, 2);
return getStringParameter(L, BRLAPI_PARAM_COMMAND_KEYCODE_NAME, keyCode);
}
static int getCommandKeycodeSummary(lua_State *L) {
const brlapi_keyCode_t keyCode = (brlapi_keyCode_t)luaL_checkinteger(L, 2);
return getStringParameter(L, BRLAPI_PARAM_COMMAND_KEYCODE_SUMMARY, keyCode);
}
static int getDefinedDriverKeycodes(lua_State *L) {
size_t count;
brlapi_keyCode_t *codes = brlapi__getParameterAlloc(checkhandle(L, 1),
BRLAPI_PARAM_DEFINED_DRIVER_KEYCODES, 0, BRLAPI_PARAMF_GLOBAL, &count
);
if (codes == NULL) error(L);
count /= sizeof(brlapi_keyCode_t);
lua_newtable(L);
for (size_t i = 0; i < count; i += 1) {
lua_pushinteger(L, (lua_Integer)codes[i]);
lua_rawseti(L, -2, i + 1);
}
free(codes);
return 1;
}
static int getDriverKeycodeName(lua_State *L) {
const brlapi_keyCode_t keyCode = (brlapi_keyCode_t)luaL_checkinteger(L, 2);
return getStringParameter(L, BRLAPI_PARAM_DRIVER_KEYCODE_NAME, keyCode);
}
static int getDriverKeycodeSummary(lua_State *L) {
const brlapi_keyCode_t keyCode = (brlapi_keyCode_t)luaL_checkinteger(L, 2);
return getStringParameter(L, BRLAPI_PARAM_DRIVER_KEYCODE_SUMMARY, keyCode);
}
static inline int versionGetField(lua_State *L, const char *name) {
if (lua_getfield(L, 1, name) == LUA_TNUMBER) {
if (lua_getfield(L, 2, name) == LUA_TNUMBER) {
return 1;
}
}
return 0;
}
static int versionCompare(lua_State *L, int op) {
int result = 0;
if (versionGetField(L, "major")) {
if (lua_compare(L, -2, -1, LUA_OPLT)) {
result = 1;
} else if (lua_compare(L, -2, -1, LUA_OPEQ)) {
if (versionGetField(L, "minor")) {
if (lua_compare(L, -2, -1, LUA_OPLT)) {
result = 1;
} else if (lua_compare(L, -2, -1, LUA_OPEQ)) {
if (versionGetField(L, "revision")) {
if (lua_compare(L, -2, -1, op)) {
result = 1;
}
}
}
}
}
}
lua_pushboolean(L, result);
return 1;
}
static int versionLT(lua_State *L) {
return versionCompare(L, LUA_OPLT);
}
static int versionLE(lua_State *L) {
return versionCompare(L, LUA_OPLE);
}
static int versionString(lua_State *L) {
static const char *separator = ".";
lua_getfield(L, 1, "major");
lua_pushstring(L, separator);
lua_getfield(L, 1, "minor");
lua_pushstring(L, separator);
lua_getfield(L, 1, "revision");
lua_concat(L, 5);
return 1;
}
static const luaL_Reg version_meta[] = {
{ "__lt", versionLT },
{ "__le", versionLE },
{ "__tostring", versionString },
{ NULL, NULL }
};
static const luaL_Reg meta[] = {
{ "__close", closeConnection },
{ NULL, NULL }
};
static const luaL_Reg funcs[] = {
{ "openConnection", openConnection },
{ "getFileDescriptor", getFileDescriptor },
{ "closeConnection", closeConnection },
{ "getDriverName", getDriverName },
{ "getModelIdentifier", getModelIdentifier },
{ "getDisplaySize", getDisplaySize },
{ "enterTtyMode", enterTtyMode },
{ "leaveTtyMode", leaveTtyMode },
{ "setFocus", setFocus },
{ "writeText", writeText },
{ "writeDots", writeDots },
{ "readKey", readKey },
{ "readKeyWithTimeout", readKeyWithTimeout },
{ "expandKeyCode", expandKeyCode },
{ "describeKeyCode", describeKeyCode },
{ "enterRawMode", enterRawMode },
{ "leaveRawMode", leaveRawMode },
{ "acceptKeys", acceptKeys },
{ "ignoreKeys", ignoreKeys },
{ "acceptAllKeys", acceptAllKeys },
{ "ignoreAllKeys", ignoreAllKeys },
{ "suspendDriver", suspendDriver },
{ "resumeDriver", resumeDriver },
{ "pause", pause_ },
{ "getDriverCode", getDriverCode },
{ "getDriverVersion", getDriverVersion },
{ "getDeviceOnline", getDeviceOnline },
{ "getAudibleAlerts", getAudibleAlerts },
{ "setAudibleAlerts", setAudibleAlerts },
{ "getClipboardContent", getClipboardContent },
{ "setClipboardContent", setClipboardContent },
{ "getComputerBrailleTable", getComputerBrailleTable },
{ "setComputerBrailleTable", setComputerBrailleTable },
{ "getLiteraryBrailleTable", getLiteraryBrailleTable },
{ "setLiteraryBrailleTable", setLiteraryBrailleTable },
{ "getMessageLocale", getMessageLocale },
{ "setMessageLocale", setMessageLocale },
{ "getBoundCommandKeycodes", getBoundCommandKeycodes },
{ "getCommandKeycodeName", getCommandKeycodeName },
{ "getCommandKeycodeSummary", getCommandKeycodeSummary },
{ "getDefinedDriverKeycodes", getDefinedDriverKeycodes },
{ "getDriverKeycodeName", getDriverKeycodeName },
{ "getDriverKeycodeSummary", getDriverKeycodeSummary },
{ NULL, NULL }
};
static void createcmdtable(lua_State *L) {
lua_newtable(L);
#define CMD(name, value) lua_pushinteger(L, (lua_Integer)value), lua_setfield(L, -2, STRINGIFY(name));
#include "cmd.auto.h"
#undef CMD
}
int luaopen_brlapi(lua_State *L) {
luaL_newmetatable(L, handle_t);
luaL_setfuncs(L, meta, 0);
luaL_newlib(L, funcs);
{
int major, minor, revision;
brlapi_getLibraryVersion(&major, &minor, &revision);
lua_createtable(L, 0, 3);
lua_pushinteger(L, major), lua_setfield(L, -2, "major");
lua_pushinteger(L, minor), lua_setfield(L, -2, "minor");
lua_pushinteger(L, revision), lua_setfield(L, -2, "revision");
luaL_newmetatable(L, "version");
luaL_setfuncs(L, version_meta, 0);
lua_setmetatable(L, -2);
lua_setfield(L, -2, "version");
}
createcmdtable(L);
lua_setfield(L, -2, "CMD");
/* Set function table as metatable __index */
lua_pushvalue(L, -1), lua_setfield(L, -3, "__index");
return 1;
}
|