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
|
/*
* 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 <stdio.h>
#include <string.h>
#include "log.h"
#include "cmd_queue.h"
#include "cmd_enqueue.h"
#include "cmd_utils.h"
#include "brl_cmds.h"
#include "cmd.h"
#include "queue.h"
#include "async_handle.h"
#include "async_alarm.h"
#include "prefs.h"
#include "ktb_types.h"
#include "scr.h"
#include "core.h"
#define LOG_LEVEL LOG_DEBUG
typedef struct CommandEnvironmentStruct CommandEnvironment;
typedef struct CommandHandlerLevelStruct CommandHandlerLevel;
struct CommandHandlerLevelStruct {
CommandHandlerLevel *previousLevel;
const char *levelName;
CommandHandler *handleCommand;
CommandDataDestructor *destroyData;
void *handlerData;
KeyTableCommandContext commandContext;
};
struct CommandEnvironmentStruct {
CommandEnvironment *previousEnvironment;
const char *environmentName;
CommandHandlerLevel *handlerStack;
CommandPreprocessor *preprocessCommand;
CommandPostprocessor *postprocessCommand;
unsigned handlingCommand:1;
};
static CommandEnvironment *commandEnvironmentStack = NULL;
static unsigned int commandQueueSuspendCount = 0;
static CommandHandlerLevel **
getCommandHandlerTop (void) {
if (!commandEnvironmentStack) return NULL;
return &commandEnvironmentStack->handlerStack;
}
KeyTableCommandContext
getCurrentCommandContext (void) {
CommandHandlerLevel **top = getCommandHandlerTop();
KeyTableCommandContext context = top && *top? (*top)->commandContext: KTB_CTX_DEFAULT;
if (context == KTB_CTX_DEFAULT) context = getScreenCommandContext();
return context;
}
static int
toPreferredCommand (int command) {
int preferred = command;
int cmd = command & BRL_MSK_CMD;
int blk = command & BRL_MSK_BLK;
if (blk) {
} else {
if (prefs.skipIdenticalLines) {
switch (cmd) {
case BRL_CMD_LNUP:
preferred = BRL_CMD_PRDIFLN;
break;
case BRL_CMD_LNDN:
preferred = BRL_CMD_NXDIFLN;
break;
case BRL_CMD_PRDIFLN:
preferred = BRL_CMD_LNUP;
break;
case BRL_CMD_NXDIFLN:
preferred = BRL_CMD_LNDN;
break;
default:
break;
}
}
if (prefs.skipBlankBrailleWindows) {
switch (cmd) {
case BRL_CMD_FWINLT:
preferred = BRL_CMD_FWINLTSKIP;
break;
case BRL_CMD_FWINRT:
preferred = BRL_CMD_FWINRTSKIP;
break;
case BRL_CMD_FWINLTSKIP:
preferred = BRL_CMD_FWINLT;
break;
case BRL_CMD_FWINRTSKIP:
preferred = BRL_CMD_FWINRT;
break;
default:
break;
}
}
}
if (preferred == command) {
logCommand(command);
} else {
preferred |= (command & ~BRL_MSK_CMD);
logTransformedCommand(command, preferred);
command = preferred;
}
return command;
}
int
handleCommand (int command) {
const CommandEnvironment *env = commandEnvironmentStack;
const CommandHandlerLevel *chl = env->handlerStack;
while (chl) {
if (chl->handleCommand(command, chl->handlerData)) return 1;
chl = chl->previousLevel;
}
logMessage(LOG_WARNING, "%s: %04X", gettext("unhandled command"), command);
return 0;
}
typedef struct {
int command;
} CommandQueueItem;
static void
deallocateCommandQueueItem (void *item, void *data) {
CommandQueueItem *cmd = item;
free(cmd);
}
static Queue *
createCommandQueue (void *data) {
return newQueue(deallocateCommandQueueItem, NULL);
}
static Queue *
getCommandQueue (int create) {
static Queue *commands = NULL;
return getProgramQueue(&commands, "command-queue", create,
createCommandQueue, NULL);
}
static int
dequeueCommand (Queue *queue) {
CommandQueueItem *item;
if ((item = dequeueItem(queue))) {
int command = item->command;
free(item);
item = NULL;
return command;
}
return EOF;
}
static void setCommandAlarm (void *data);
static AsyncHandle commandAlarm = NULL;
ASYNC_ALARM_CALLBACK(handleCommandAlarm) {
Queue *queue = getCommandQueue(0);
asyncDiscardHandle(commandAlarm);
commandAlarm = NULL;
if (queue) {
int command = dequeueCommand(queue);
if (command != EOF) {
command = toPreferredCommand(command);
const CommandEntry *cmd = findCommandEntry(command);
CommandEnvironment *env = commandEnvironmentStack;
env->handlingCommand = 1;
void *pre = env->preprocessCommand? env->preprocessCommand(): NULL;
int handled = handleCommand(command);
if (env->postprocessCommand) {
env->postprocessCommand(pre, command, cmd, handled);
}
env->handlingCommand = 0;
}
}
setCommandAlarm(parameters->data);
}
static void
setCommandAlarm (void *data) {
if (!commandAlarm && !commandQueueSuspendCount) {
const CommandEnvironment *env = commandEnvironmentStack;
if (env && !env->handlingCommand) {
Queue *queue = getCommandQueue(0);
if (queue && (getQueueSize(queue) > 0)) {
asyncNewRelativeAlarm(&commandAlarm, 0, handleCommandAlarm, data);
}
}
}
}
static void
cancelCommandAlarm (void) {
if (commandAlarm) {
asyncCancelRequest(commandAlarm);
commandAlarm = NULL;
}
}
int
enqueueCommand (int command) {
if (command == EOF) return 1;
{
Queue *queue = getCommandQueue(1);
if (queue) {
CommandQueueItem *item = malloc(sizeof(CommandQueueItem));
if (item) {
item->command = command;
if (enqueueItem(queue, item)) {
setCommandAlarm(NULL);
return 1;
}
free(item);
} else {
logMallocError();
}
}
}
return 0;
}
int
pushCommandHandler (
const char *name,
KeyTableCommandContext context,
CommandHandler *handler,
CommandDataDestructor *destructor,
void *data
) {
CommandHandlerLevel *chl;
if ((chl = malloc(sizeof(*chl)))) {
memset(chl, 0, sizeof(*chl));
chl->levelName = name;
chl->handleCommand = handler;
chl->destroyData = destructor;
chl->handlerData = data;
chl->commandContext = context;
{
CommandHandlerLevel **top = getCommandHandlerTop();
chl->previousLevel = *top;
*top = chl;
}
logMessage(LOG_LEVEL, "pushed command handler: %s", chl->levelName);
return 1;
} else {
logMallocError();
}
return 0;
}
int
popCommandHandler (void) {
CommandHandlerLevel **top = getCommandHandlerTop();
CommandHandlerLevel *chl = *top;
if (!chl) return 0;
*top = chl->previousLevel;
logMessage(LOG_LEVEL, "popped command handler: %s", chl->levelName);
if (chl->destroyData) chl->destroyData(chl->handlerData);
free(chl);
return 1;
}
int
pushCommandEnvironment (
const char *name,
CommandPreprocessor *preprocessCommand,
CommandPostprocessor *postprocessCommand
) {
CommandEnvironment *env;
if ((env = malloc(sizeof(*env)))) {
memset(env, 0, sizeof(*env));
env->environmentName = name;
env->handlerStack = NULL;
env->preprocessCommand = preprocessCommand;
env->postprocessCommand = postprocessCommand;
env->handlingCommand = 0;
env->previousEnvironment = commandEnvironmentStack;
commandEnvironmentStack = env;
setCommandAlarm(NULL);
logMessage(LOG_LEVEL, "pushed command environment: %s", env->environmentName);
return 1;
} else {
logMallocError();
}
return 0;
}
int
popCommandEnvironment (void) {
CommandEnvironment *env = commandEnvironmentStack;
if (!env) return 0;
while (popCommandHandler());
commandEnvironmentStack = env->previousEnvironment;
{
const CommandEnvironment *env = commandEnvironmentStack;
if (!env || env->handlingCommand) {
cancelCommandAlarm();
}
}
logMessage(LOG_LEVEL, "popped command environment: %s", env->environmentName);
free(env);
return 1;
}
int
beginCommandQueue (void) {
commandEnvironmentStack = NULL;
commandQueueSuspendCount = 0;
return pushCommandEnvironment("initial", NULL, NULL);
}
void
endCommandQueue (void) {
while (popCommandEnvironment());
}
void
suspendCommandQueue (void) {
if (!commandQueueSuspendCount++) cancelCommandAlarm();
}
void
resumeCommandQueue (void) {
if (!--commandQueueSuspendCount) setCommandAlarm(NULL);
}
|