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
|
/*
* 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 <string.h>
#include <sys/stat.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include "log.h"
#include "scr_emulator.h"
static const int ipcCreationFlags = IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR;
void
moveScreenCharacters (ScreenSegmentCharacter *to, const ScreenSegmentCharacter *from, size_t count) {
if (count && (from != to)) {
memmove(to, from, (count * sizeof(*from)));
}
}
void
setScreenCharacters (ScreenSegmentCharacter *from, const ScreenSegmentCharacter *to, const ScreenSegmentCharacter *character) {
while (from < to) *from++ = *character;
}
void
propagateScreenCharacter (ScreenSegmentCharacter *from, const ScreenSegmentCharacter *to) {
setScreenCharacters(from+1, to, from);
}
void
fillScreenRows (ScreenSegmentHeader *segment, unsigned int row, unsigned int count, const ScreenSegmentCharacter *character) {
while (count--) {
const ScreenSegmentCharacter *to;
ScreenSegmentCharacter *from = getScreenRow(segment, row++, &to);
setScreenCharacters(from, to, character);
}
}
void
moveScreenRows (ScreenSegmentHeader *segment, unsigned int from, unsigned int to, unsigned int count) {
if (count && (from != to)) {
moveScreenCharacters(
getScreenRow(segment, to, NULL),
getScreenRow(segment, from, NULL),
(count * segment->screenWidth)
);
}
}
#define SWAP(a, b) do { (a) ^= (b); (b) ^= (a); (a) ^= (b); } while (0)
#undef HAVE_BUILTIN_CTZ
#ifdef __has_builtin
#if __has_builtin(__builtin_ctz)
#define HAVE_BUILTIN_CTZ
#endif /* __has_builtin(__builtin_ctz) */
#endif /* __has_builtin */
#ifdef HAVE_BUILTIN_CTZ
static inline int
ctz (unsigned int x) {
return __builtin_ctz(x);
}
#else /* HAVE_BUILTIN_CTZ */
#include <string.h>
static inline int
ctz (unsigned int x) {
return ffs(x) - 1;
}
#endif /* HAVE_BUILTIN_CTZ */
/* Greatest Common Divisor
*
* gcd(a,b) computes the greatest common divisor of a and b. I included
* a highly optimized implementation for speed. But the simplest
* implementation would look like:
*
* unsigned long gcd(unsigned long a, unsigned long b) {
* if (b == 0) return a;
* return gcd(b, a % b);
* }
*/
static unsigned int
gcd (unsigned int a, unsigned int b) {
unsigned int r = a | b;
if (!a || !b) return r;
b >>= ctz(b);
if (b == 1) return r & -r;
while (1) {
a >>= ctz(a);
if (a == 1) return r & -r;
if (a == b) return a << ctz(r);
if (a < b) SWAP(a, b);
a -= b;
}
}
/* Scrolling the Row Array
*
* The idea is to have lines indexed into an array. Then, a screen scroll
* can be achieved by performing an array rotation. To scroll one line
* up, the array is rotated left by one position and what used to be the
* top row becomes the bottom row and gets cleared. To scroll one line
* down, the array is rotated left by n-1 positions instead, and the bottom
* row becomes the top row. And this works the same regardless of the
* number of lines to scroll.
*
* The array rotation algorithm used here is complexity O(n) in execution
* and O(1) in memory usage, n being the array size. The scroll amount
* doesn't affect complexity.
*
* See https://www.geeksforgeeks.org/array-rotation/ for algorithmic
* details.
*/
void
scrollScreenRows (ScreenSegmentHeader *segment, unsigned int top, unsigned int size, unsigned int count, int down) {
if (haveScreenRowArray(segment)) {
unsigned int delta = down? (size - count): count;
for (unsigned int i=0; i<gcd(delta, size); i+=1) {
ScreenSegmentRow row = getScreenRowArray(segment)[top + i];
unsigned int j = i;
while (1) {
unsigned int k = j + delta;
if (k >= size) k -= size;
if (k == i) break;
getScreenRowArray(segment)[top + j] = getScreenRowArray(segment)[top + k];
j = k;
}
getScreenRowArray(segment)[top + j] = row;
}
} else if (down) {
moveScreenRows(segment, top, (top + count), (size - count));
} else {
moveScreenRows(segment, (top + count), top, (size - count));
}
}
int
destroyScreenSegment (int identifier) {
if (shmctl(identifier, IPC_RMID, NULL) != -1) return 1;
logSystemError("shmctl[IPC_RMID]");
return 0;
}
static void
initializeScreenCharacters (ScreenSegmentCharacter *from, const ScreenSegmentCharacter *to) {
const ScreenSegmentCharacter initializer = {
.text = ' ',
.foreground = SCREEN_SEGMENT_COLOR_WHITE,
.background = SCREEN_SEGMENT_COLOR_BLACK,
.alpha = UINT8_MAX,
};
setScreenCharacters(from, to, &initializer);
}
ScreenSegmentHeader *
createScreenSegment (int *identifier, key_t key, int columns, int rows, int enableRowArray) {
size_t rowsSize = enableRowArray? (sizeof(ScreenSegmentRow) * rows): 0;
size_t charactersSize = sizeof(ScreenSegmentCharacter) * rows * columns;
size_t segmentSize = sizeof(ScreenSegmentHeader) + rowsSize + charactersSize;
int segmentIdentifier;
if (getScreenSegment(&segmentIdentifier, key)) {
destroyScreenSegment(segmentIdentifier);
}
if ((segmentIdentifier = shmget(key, segmentSize, ipcCreationFlags)) != -1) {
ScreenSegmentHeader *segment = attachScreenSegment(segmentIdentifier);
if (segment) {
uint32_t nextOffset = 0;
segment->segmentSize = segmentSize;
segment->headerSize = sizeof(*segment);
nextOffset += segment->headerSize;
segment->screenHeight = rows;
segment->screenWidth = columns;
segment->cursorRow = 0;
segment->cursorColumn = 0;
segment->screenNumber = 0;
segment->commonFlags = 0;
segment->privateFlags = 0;
if (rowsSize) {
segment->rowSize = sizeof(ScreenSegmentRow);
segment->rowsOffset = nextOffset;
nextOffset += rowsSize;
} else {
segment->rowSize = 0;
segment->rowsOffset = 0;
}
segment->characterSize = sizeof(ScreenSegmentCharacter);
segment->charactersOffset = nextOffset;
nextOffset += charactersSize;
if (haveScreenRowArray(segment)) {
/* Rows are initially sequential. */
ScreenSegmentRow *row = getScreenRowArray(segment);
ScreenSegmentRow *end = row + rows;
uint32_t offset = segment->charactersOffset;
uint32_t increment = getScreenRowWidth(segment);
while (row < end) {
row->charactersOffset = offset;
offset += increment;
row += 1;
}
}
{
const ScreenSegmentCharacter *to;
ScreenSegmentCharacter *from = getScreenCharacterArray(segment, &to);
initializeScreenCharacters(from, to);
}
if (identifier) *identifier = segmentIdentifier;
return segment;
}
destroyScreenSegment(segmentIdentifier);
} else {
logSystemError("shmget");
}
return NULL;
}
int
destroyMessageQueue (int queue) {
if (msgctl(queue, IPC_RMID, NULL) != -1) return 1;
logSystemError("msgctl[IPC_RMID]");
return 0;
}
int
createMessageQueue (int *queue, key_t key) {
int q;
if (getMessageQueue(&q, key)) {
destroyMessageQueue(q);
}
if ((q = msgget(key, ipcCreationFlags)) != -1) {
if (queue) *queue = q;
return 1;
} else {
logSystemError("msgget");
}
return 0;
}
|