File: cmd_utils.c

package info (click to toggle)
brltty 6.7-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 31,240 kB
  • sloc: ansic: 148,859; java: 13,418; sh: 9,623; xml: 5,699; tcl: 2,634; makefile: 2,333; awk: 713; lisp: 366; python: 321; ml: 301
file content (373 lines) | stat: -rw-r--r-- 9,151 bytes parent folder | download | duplicates (3)
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
/*
 * 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-2024 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 <ctype.h>

#include "log.h"
#include "strfmt.h"
#include "alert.h"
#include "brl_cmds.h"
#include "unicode.h"
#include "ascii.h"
#include "scr.h"
#include "core.h"

void
alertLineSkipped (unsigned int *count) {
  const unsigned int interval = 4;

  if (!*count) {
    alert(ALERT_SKIP_FIRST);
  } else if (*count <= interval) {
    alert(ALERT_SKIP_ONE);
  } else if (!(*count % interval)) {
    alert(ALERT_SKIP_SEVERAL);
  }

  *count += 1;
}

int
isTextOffset (int arg, int *first, int *last, int relaxed) {
  int y = arg / brl.textColumns;
  if (y >= brl.textRows) return 0;
  if ((ses->winy + y) >= scr.rows) return 0;

  int x = arg % brl.textColumns;
  if (x < textStart) return 0;
  if ((x -= textStart) >= textCount) return 0;

  if (isContracted) {
    BrailleRowDescriptor *brd = getBrailleRowDescriptor(y);
    if (!brd) return 0;

    int *offsets = brd->contracted.offsets.array;
    if (!offsets) return 0;

    int start = 0;
    int end = 0;

    {
      int textIndex = 0;

      while (textIndex < brd->contracted.length) {
        int cellIndex = offsets[textIndex];

        if (cellIndex != CTB_NO_OFFSET) {
          if (cellIndex > x) {
            end = textIndex - 1;
            break;
          }

          start = textIndex;
        }

        textIndex += 1;
      }

      if (textIndex == brd->contracted.length) end = textIndex - 1;
    }

    if (first) *first = start;
    if (last) *last = end;
  } else {
    if ((ses->winx + x) >= scr.cols) {
      if (!relaxed) return 0;
      x = scr.cols - ses->winx - 1;
    }

    if (prefs.wordWrap) {
      int length = getWordWrapLength(ses->winy, ses->winx, textCount);
      if (length > textCount) length = textCount;
      if (x >= length) x = length - 1;
    }

    if (first) *first = x;
    if (last) *last = x;
  }

  return 1;
}

int
getCharacterCoordinates (int arg, int *row, int *first, int *last, int relaxed) {
  if (arg == BRL_MSK_ARG) {
    if (!SCR_CURSOR_OK()) return 0;
    *row = scr.posy;
    if (first) *first = scr.posx;
    if (last) *last = scr.posx;
  } else {
    if (!isTextOffset(arg, first, last, relaxed)) return 0;
    if (row) *row = ses->winy;
    if (first) *first += ses->winx;
    if (last) *last += ses->winx;
  }

  return 1;
}

static ScreenCharacter
getScreenCharacter (int column, int row) {
  ScreenCharacter character;
  readScreen(column, row, 1, 1, &character);
  return character;
}

STR_BEGIN_FORMATTER(formatCharacterDescription, int column, int row)
  ScreenCharacter character = getScreenCharacter(column, row);

  {
    char name[0X40];

    if (getCharacterName(character.text, name, sizeof(name))) {
      {
        size_t length = strlen(name);
        for (int i=0; i<length; i+=1) name[i] = tolower(name[i]);
      }

      STR_PRINTF(" %s: ", name);
    }
  }

  {
    uint32_t text = character.text;
    STR_PRINTF("U+%04" PRIX32 " (%" PRIu32 "):", text, text);
  }

  {
    STR_PRINTF(" ");

    static const char *const colours[] = {
      /*      */ strtext("black"),
      /*    B */ strtext("blue"),
      /*   G  */ strtext("green"),
      /*   GB */ strtext("cyan"),
      /*  R   */ strtext("red"),
      /*  R B */ strtext("magenta"),
      /*  RG  */ strtext("brown"),
      /*  RGB */ strtext("light grey"),
      /* L    */ strtext("dark grey"),
      /* L  B */ strtext("light blue"),
      /* L G  */ strtext("light green"),
      /* L GB */ strtext("light cyan"),
      /* LR   */ strtext("light red"),
      /* LR B */ strtext("light magenta"),
      /* LRG  */ strtext("yellow"),
      /* LRGB */ strtext("white")
    };

    unsigned char attributes = character.attributes;
    const char *foreground = gettext(colours[attributes & SCR_MASK_FG]);
    const char *background = gettext(colours[(attributes & SCR_MASK_BG) >> 4]);

    // xgettext: This phrase describes the colour of a character on the screen.
    // xgettext: %1$s is the (already translated) foreground colour.
    // xgettext: %2$s is the (already translated) background colour.
    STR_PRINTF(gettext("%1$s on %2$s"), foreground, background);
  }

  if (character.attributes & SCR_ATTR_BLINK) {
    STR_PRINTF(" %s", gettext("blinking"));
  }
STR_END_FORMATTER

static const char *const phoneticWords[] = {
  [' '] = "space",

  ['a'] = "alpha",
  ['b'] = "bravo",
  ['c'] = "charlie",
  ['d'] = "delta",
  ['e'] = "echo",
  ['f'] = "foxtrot",
  ['g'] = "golf",
  ['h'] = "hotel",
  ['i'] = "india",
  ['j'] = "juliet",
  ['k'] = "kilo",
  ['l'] = "lima",
  ['m'] = "mike",
  ['n'] = "november",
  ['o'] = "oscar",
  ['p'] = "papa",
  ['q'] = "quebec",
  ['r'] = "romeo",
  ['s'] = "sierra",
  ['t'] = "tango",
  ['u'] = "uniform",
  ['v'] = "victor",
  ['w'] = "whiskey",
  ['x'] = "x-ray",
  ['y'] = "yankee",
  ['z'] = "zulu",

  ['0'] = "zero",
  ['1'] = "one",
  ['2'] = "two",
  ['3'] = "three",
  ['4'] = "four",
  ['5'] = "five",
  ['6'] = "six",
  ['7'] = "seven",
  ['8'] = "eight",
  ['9'] = "nine",

  ['+'] = "plus",
  ['='] = "equals",
  ['<'] = "less than",
  ['>'] = "greater than",

  ['('] = "left parenthesis",
  [')'] = "right parenthesis",
  ['['] = "left bracket",
  [']'] = "right bracket",
  ['{'] = "left brace",
  ['}'] = "right brace",

  ['"'] = "quote",
  ['\''] = "apostrophe",
  [','] = "comma",
  [';'] = "semicolon",
  [':'] = "colon",
  ['.'] = "period",
  ['!'] = "exclamation",
  ['?'] = "question",

  ['`'] = "grave",
  ['~'] = "tilde",
  ['@'] = "at",
  ['#'] = "number",
  ['$'] = "dollar",
  ['%'] = "percent",
  ['^'] = "circumflex",
  ['&'] = "ampersand",
  ['*'] = "asterisk",
  ['-'] = "dash",
  ['_'] = "underscore",

  ['/'] = "slash",
  ['\\'] = "backslash",
  ['|'] = "vertical bar",

  [ASCII_NUL] = "null",
  [ASCII_SOH] = "start of header",
  [ASCII_STX] = "start of text",
  [ASCII_ETX] = "end of text",
  [ASCII_EOT] = "end of transmission",
  [ASCII_ENQ] = "enquiry",
  [ASCII_ACK] = "acknowledgement",
  [ASCII_BEL] = "bell",
  [ASCII_BS] = "backspace",
  [ASCII_HT] = "horizontal tab",
  [ASCII_LF] = "line feed",
  [ASCII_VT] = "vertical tab",
  [ASCII_FF] = "form feed",
  [ASCII_CR] = "carriage return",
  [ASCII_SO] = "shift out",
  [ASCII_SI] = "shift in",
  [ASCII_DLE] = "data link escape",
  [ASCII_DC1] = "device control one",
  [ASCII_DC2] = "device control two",
  [ASCII_DC3] = "device control three",
  [ASCII_DC4] = "device control four",
  [ASCII_NAK] = "negative acknowledgement",
  [ASCII_SYN] = "synchronous idle",
  [ASCII_ETB] = "end of transmission block",
  [ASCII_CAN] = "cancel",
  [ASCII_EM] = "end of medium",
  [ASCII_SUB] = "substitute",
  [ASCII_ESC] = "escape",
  [ASCII_FS] = "file separator",
  [ASCII_GS] = "group separator",
  [ASCII_RS] = "record separator",
  [ASCII_US] = "unit separator",
  [ASCII_DEL] = "delete",
};

static const char *
getPhoneticWord (wchar_t character) {
  if (character >= ARRAY_COUNT(phoneticWords)) return NULL;
  return phoneticWords[character];
}

STR_BEGIN_FORMATTER(formatPhoneticPhrase, int column, int row)
  wchar_t character = getScreenCharacter(column, row).text;

  wchar_t characters[0X10];
  size_t characterCount = decomposeCharacter(character, characters, ARRAY_COUNT(characters));

  if (!characterCount) {
    characters[0] = character;
    characterCount = 1;
  }

  for (unsigned int characterIndex=0; characterIndex<characterCount; characterIndex+=1) {
    if (characterIndex > 0) {
      STR_PRINTF("%s ", ((characterIndex == 1)? " with": ","));
    }

    character = characters[characterIndex];
    const char *word = getPhoneticWord(character);
    char nameBuffer[0X40];

    if (!word) {
      if (iswupper(character)) {
        wchar_t lowercase = towlower(character);
        word = getPhoneticWord(lowercase);

        if (word) {
          STR_PRINTF("cap ");
          character = lowercase;
        }
      }
    }

    if (!word) {
      if (getCharacterName(character, nameBuffer, sizeof(nameBuffer))) {
        word = nameBuffer;

        {
          char *byte = nameBuffer;

          while (*byte) {
            *byte = tolower((unsigned char)*byte);
            byte += 1;
          }
        }

        {
          const char *space = strchr(word, ' ');

          if (space) {
            size_t length = space - word + 1;
            if (memcmp(word,  "combining ", length) == 0) word += length;
          }
        }
      }
    }

    if (word) {
      if (STR_LENGTH > 0) STR_PRINTF(" ");
      STR_PRINTF("%s", word);
    }
  }
STR_END_FORMATTER