File: screen.c

package info (click to toggle)
brltty 6.8-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,776 kB
  • sloc: ansic: 150,447; java: 13,484; sh: 9,667; xml: 5,702; tcl: 2,634; makefile: 2,328; awk: 713; lisp: 366; python: 321; ml: 301
file content (601 lines) | stat: -rw-r--r-- 13,047 bytes parent folder | download | duplicates (2)
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
/*
 * 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 <errno.h>
#include <fcntl.h>
#include <sys/stat.h>

#include "log.h"
#include "alert.h"
#include "strfmt.h"
#include "parse.h"
#include "utf8.h"
#include "brl_cmds.h"
#include "embed.h"

typedef enum {
  PARM_FILE,
  PARM_SHOW,
} ScreenParameters;

#define SCRPARMS "file", "show"
#include "scr_driver.h"

#include <term.h>
#include <stdio.h>

#include "get_curses.h"

static unsigned int showOnTerminal = 0;

static const char *filePath;
static wchar_t *fileCharacters;

typedef struct {
  unsigned int offset;
  unsigned int length;
} LineDescriptor;

static LineDescriptor *lineDescriptors;
static unsigned int lineSize;
static unsigned int lineCount;

static int screenWidth;
static int cursorOffset;

static int
processParameters_FileViewerScreen (char **parameters) {
  filePath = parameters[PARM_FILE];
  if (filePath && !*filePath) filePath = NULL;

  showOnTerminal = 0;
  {
    const char *parameter = parameters[PARM_SHOW];

    if (parameter && *parameter) {
      if (!validateYesNo(&showOnTerminal, parameter)) {
        logMessage(LOG_WARNING, "%s: %s", "invalid show setting", parameter);
      }
    }
  }

  return 1;
}

static int
addLine (const wchar_t *from, const wchar_t *to) {
  size_t lineLength = to - from;
  if (lineLength > screenWidth) screenWidth = lineLength;

  if (lineCount == lineSize) {
    size_t newSize = lineSize? lineSize<<1: 0X80;
    LineDescriptor *newArray = realloc(lineDescriptors, ARRAY_SIZE(lineDescriptors, newSize));

    if (!newArray) {
      logMallocError();
      return 0;
    }

    lineDescriptors = newArray;
    lineSize = newSize;
  }

  LineDescriptor *line = &lineDescriptors[lineCount++];
  line->offset = from - fileCharacters;
  line->length = to - from;

  return 1;
}

static int
setScreenContent (const char *text) {
  unsigned int characterCount = countUtf8Characters(text);
  fileCharacters = malloc(characterCount * sizeof(*fileCharacters));

  if (fileCharacters) {
    makeWcharsFromUtf8(text, fileCharacters, characterCount);

    const wchar_t *current = fileCharacters;
    const wchar_t *end = current + characterCount;

    while (current < end) {
      wchar_t *next = wcschr(current, WC_C('\n'));

      if (!next) {
        if (!addLine(current, end)) return 0;
        break;
      }

      if (!addLine(current, next)) return 0;
      current = next + 1;
    }

    return 1;
  } else {
    logMallocError();
  }

  return 0;
}

static int
loadFile (void) {
  const char *problem = NULL;

  if (filePath) {
    struct stat status;

    if (stat(filePath, &status) != -1) {
      size_t fileSize = status.st_size;
      char *text = malloc(fileSize + 1);

      if (text) {
        int fileDescriptor = open(filePath, O_RDONLY);

        if (fileDescriptor != -1) {
          ssize_t result = read(fileDescriptor, text, fileSize);

          if (result != -1) {
            if (result < fileSize) fileSize = result;
            text[fileSize] = 0;
            setScreenContent(text);
          } else {
            problem = strerror(errno);
          }

          close(fileDescriptor);
        } else {
          problem = strerror(errno);
        }

        free(text);
      } else {
        problem = strerror(errno);
      }
    } else {
      problem = strerror(errno);
    }
  } else {
    problem = gettext("file not specified");
    filePath = NULL;
  }

  if (!problem) return 1;
  char log[0X100];
  STR_BEGIN(log, sizeof(log));

  if (filePath) STR_PRINTF("%s: ", filePath);
  STR_PRINTF("%s", problem);

  STR_END;
  logMessage(LOG_WARNING, "%s", log);

  setScreenContent(log);
  return 0;
}

static int
toScreenRow (int offset) {
  return offset / screenWidth;
}

static int
toScreenColumn (int offset) {
  return offset % screenWidth;
}

#ifdef GOT_CURSES
static int terminalInitialized = 0;

static void
writeTerminalString (const char *string) {
  if (string) {
    putp(string);
  }
}

static void
writeTerminalCapability0 (const char *name) {
  writeTerminalString(tigetstr(name));
}

static void
writeTerminalCapability2 (const char *name, int p1, int p2) {
  writeTerminalString(tiparm(tigetstr(name), p1, p2));
}

static void
clearTerminalScreen (void) {
  writeTerminalCapability0("clear");
}

static void
placeTerminalCursor (int row, int column) {
  writeTerminalCapability2("cup", row, column);
}

static void
refreshTerminalScreen (int oldOffset) {
  if (terminalInitialized) {
    int newRow = toScreenRow(cursorOffset);
    int newFrom = (newRow / lines) * lines;
    int refresh = oldOffset < 0;

    if (!refresh) {
      int oldRow = toScreenRow(oldOffset);
      int oldFrom = (oldRow / lines) * lines;
      if (newFrom != oldFrom) refresh = 1;
    }

    if (refresh) {
      clearTerminalScreen();

      int to = newFrom + lines;
      if (to > lineCount) to = lineCount;

      const LineDescriptor *line = &lineDescriptors[newFrom];
      const LineDescriptor *lineEnd = &lineDescriptors[to];
      int row = 0;

      while (line < lineEnd) {
        placeTerminalCursor(row, 0);

        unsigned int count = line->length;
        int tooLong = count > columns;
        if (tooLong) count = columns - 1;

        {
          const wchar_t *wc = &fileCharacters[line->offset];
          const wchar_t *wcEnd = wc + count;

          while (wc < wcEnd) {
            Utf8Buffer utf8;
            convertCodepointToUtf8(*wc++, utf8);
            writeTerminalString(utf8);
          }
        }

        if (tooLong) {
          static Utf8Buffer ellipsis = {0};
          if (!ellipsis[0]) convertCodepointToUtf8(0X2026, ellipsis);
          writeTerminalString(ellipsis);
        }

        row += 1;
        line += 1;
      }
    }

    int column = toScreenColumn(cursorOffset);
    if (column >= columns) column = columns - 1;
    placeTerminalCursor((newRow - newFrom), column);
    fflush(stdout);
  }
}

static int
beginTerminal (void) {
  const char *problem = "terminal initialization failure";

  if (isatty(STDOUT_FILENO)) {
    int error;
    int result = setupterm(NULL, STDOUT_FILENO, &error);
    int initialized = result == OK;

    if (initialized) {
      writeTerminalCapability2("csr", 0, lines-1);
      writeTerminalCapability0("smcup");

      logMessage(
        LOG_CATEGORY(SCREEN_DRIVER),
        "terminal successfully initialized"
      );

      return 1;
    }

    if (result == ERR) {
      if (error == 0) {
        problem = "terminal is generic";
      } else if (error == 1) {
        problem = "terminal is hardcopy";
      } else if (error == -1) {
        problem = "terminfo database not found";
      }
    }
  } else {
    problem = "standard output isn't a terminal";
  }

  logMessage(LOG_WARNING, "%s", problem);
  return 0;
}

static void
endTerminal (void) {
  clearTerminalScreen();
  writeTerminalCapability0("rmcup");
  reset_shell_mode();
}
#endif /* GOT_CURSES */

static int
construct_FileViewerScreen (void) {
  fileCharacters = NULL;

  lineDescriptors = NULL;
  lineSize = 0;
  lineCount = 0;

  screenWidth = 0;
  loadFile();
  cursorOffset = 0;

  if (showOnTerminal) {
    #ifdef GOT_CURSES
    terminalInitialized = beginTerminal();
    refreshTerminalScreen(-1);
    #endif /* GOT_CURSES */
  }

  brlttyEnableInterrupt();
  return 1;
}

static void
destruct_FileViewerScreen (void) {
  brlttyDisableInterrupt();

  #ifdef GOT_CURSES
  if (terminalInitialized) endTerminal();
  #endif /* GOT_CURSES */

  if (lineDescriptors) {
    free(lineDescriptors);
    lineDescriptors = NULL;
  }

  if (fileCharacters) {
    free(fileCharacters);
    fileCharacters = NULL;
  }
}

static int
poll_FileViewerScreen (void) {
  return 0;
}

static int
refresh_FileViewerScreen (void) {
  return 1;
}

static void
describe_FileViewerScreen (ScreenDescription *description) {
  description->rows = lineCount;
  description->cols = screenWidth;
  description->posy = toScreenRow(cursorOffset);
  description->posx = toScreenColumn(cursorOffset);
}

static int
readCharacters_FileViewerScreen (const ScreenBox *box, ScreenCharacter *buffer) {
  if (validateScreenBox(box, screenWidth, lineCount)) {
    ScreenCharacter *target = buffer;

    for (unsigned int row=0; row<box->height; row+=1) {
      const LineDescriptor *line = &lineDescriptors[box->top + row];

      unsigned int from = box->left;
      unsigned int to = from + box->width;

      for (unsigned int column=from; column<to; column+=1) {
        target->text = (column < line->length)? fileCharacters[line->offset + column]: WC_C(' ');
        target->attributes = SCR_COLOUR_DEFAULT;
        target += 1;
      }
    }

    return 1;
  }

  return 0;
}

static void
placeCursor (int newOffset) {
  int oldOffset = cursorOffset;

  if (newOffset != oldOffset) {
    cursorOffset = newOffset;

    #ifdef GOT_CURSES
    refreshTerminalScreen(oldOffset);
    #endif /* GOT_CURSES */
  }
}

static int
toScreenOffset (int row, int column) {
  return (row * screenWidth) + column;
}

static int
routeCursor_FileViewerScreen (int column, int row, int screen) {
  placeCursor(toScreenOffset(row, column));
  return 1;
}

static void
moveCursor (int amount) {
  int newOffset = cursorOffset + amount;

  if ((newOffset >= 0) && (newOffset < (lineCount * screenWidth))) {
    placeCursor(newOffset);
  } else {
    alert(ALERT_COMMAND_REJECTED);
  }
}

static int
isBlankRow (int row) {
  const LineDescriptor *line = &lineDescriptors[row];
  const wchar_t *character = &fileCharacters[line->offset];
  const wchar_t *end = character + line->length;

  while (character < end) {
    if (!iswspace(*character)) return 0;
    character += 1;
  }

  return 1;
}

static void
findPreviousParagraph (void) {
  int wasBlank = 1;
  int row = toScreenRow(cursorOffset);

  while (row > 0) {
    int isBlank = isBlankRow(--row);

    if (isBlank != wasBlank) {
      if ((wasBlank = isBlank)) {
        placeCursor(toScreenOffset(row+1, 0));
        return;
      }
    }
  }

  if (wasBlank) {
    alert(ALERT_COMMAND_REJECTED);
  } else {
    placeCursor(toScreenOffset(row, 0));
  }
}

static void
findNextParagraph (void) {
  int wasBlank = 0;
  int row = toScreenRow(cursorOffset);

  while (row < lineCount) {
    int isBlank = isBlankRow(row);

    if (isBlank != wasBlank) {
      if (!(wasBlank = isBlank)) {
        placeCursor(toScreenOffset(row, 0));
        return;
      }
    }

    row += 1;
  }

  alert(ALERT_COMMAND_REJECTED);
}

static int
handleCommand_FileViewerScreen (int command) {
  int hasControl = command & BRL_FLG_INPUT_CONTROL;

  switch (command & BRL_MSK_CMD) {
    case BRL_CMD_KEY(ESCAPE):
      brlttyInterrupt(WAIT_STOP);
      return 1;

    case BRL_CMD_KEY(CURSOR_LEFT):
      moveCursor(-1);
      return 1;

    case BRL_CMD_KEY(CURSOR_RIGHT):
      moveCursor(1);
      return 1;

    case BRL_CMD_KEY(CURSOR_UP):
      moveCursor(-screenWidth);
      return 1;

    case BRL_CMD_KEY(CURSOR_DOWN):
      moveCursor(screenWidth);
      return 1;

    case BRL_CMD_KEY(PAGE_UP):
      findPreviousParagraph();
      return 1;

    case BRL_CMD_KEY(PAGE_DOWN):
      findNextParagraph();
      return 1;

    case BRL_CMD_KEY(HOME): {
      if (hasControl) {
        placeCursor(0);
      } else {
        moveCursor(-toScreenColumn(cursorOffset));
      }

      return 1;
    }

    case BRL_CMD_KEY(END): {
      if (hasControl) {
        placeCursor(toScreenOffset(lineCount-1, 0));
      } else {
        int row = toScreenRow(cursorOffset);
        const LineDescriptor *line = &lineDescriptors[row];
        placeCursor(toScreenOffset(row, (line->length - 1)));
      }

      return 1;
    }

    default: {
      switch (command & BRL_MSK_BLK) {
        case BRL_CMD_BLK(PASSDOTS):
        case BRL_CMD_BLK(PASSCHAR):
          alert(ALERT_COMMAND_REJECTED);
          return 1;
      }
    }
  }

  return 0;
}

static void
scr_initialize (MainScreen *main) {
  initializeRealScreen(main);

  main->base.poll = poll_FileViewerScreen;
  main->base.refresh = refresh_FileViewerScreen;

  main->base.describe = describe_FileViewerScreen;
  main->base.readCharacters = readCharacters_FileViewerScreen;

  main->base.routeCursor = routeCursor_FileViewerScreen;
  main->base.handleCommand = handleCommand_FileViewerScreen;

  main->processParameters = processParameters_FileViewerScreen;
  main->construct = construct_FileViewerScreen;
  main->destruct = destruct_FileViewerScreen;
}