File: clipboard.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 (287 lines) | stat: -rw-r--r-- 6,650 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
/*
 * 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 <ctype.h>

#include "log.h"
#include "clipboard.h"
#include "utf8.h"
#include "queue.h"
#include "lock.h"
#include "program.h"
#include "api_control.h"

typedef struct {
  wchar_t *characters;
  size_t length;
} HistoryEntry;

struct ClipboardObjectStruct {
  struct {
    wchar_t *characters;
    size_t size;
    size_t length;
  } buffer;

  struct {
    Queue *queue;
  } history;
};

const wchar_t *
getClipboardHistory (ClipboardObject *cpb, unsigned int index, size_t *length) {
  Element *element = getStackElement(cpb->history.queue, index);
  if (!element) return NULL;

  const HistoryEntry *entry = getElementItem(element);
  *length = entry->length;
  return entry->characters;
}

int
addClipboardHistory (ClipboardObject *cpb, const wchar_t *characters, size_t length) {
  if (!length) return 1;

  Queue *queue = cpb->history.queue;
  Element *element = getStackHead(queue);

  if (element) {
    const HistoryEntry *entry = getElementItem(element);

    if (length == entry->length) {
      if (wmemcmp(characters, entry->characters, length) == 0) {
        return 1;
      }
    }
  }

  {
    HistoryEntry *entry;

    if ((entry = malloc(sizeof(*entry)))) {
      if ((entry->characters = allocateCharacters(length))) {
        wmemcpy(entry->characters, characters, length);
        entry->length = length;

        if (enqueueItem(queue, entry)) {
          return 1;
        }

        free(entry->characters);
      } else {
        logMallocError();
      }

      free(entry);
    } else {
      logMallocError();
    }
  }

  return 0;
}

const wchar_t *
getClipboardContent (ClipboardObject *cpb, size_t *length) {
  *length = cpb->buffer.length;
  return cpb->buffer.characters;
}

char *
getClipboardContentUTF8 (ClipboardObject *cpb) {
  size_t length;
  const wchar_t *characters = getClipboardContent(cpb, &length);
  return getUtf8FromWchars(characters, length, NULL);
}

size_t
getClipboardContentLength (ClipboardObject *cpb) {
  return cpb->buffer.length;
}

int
truncateClipboardContent (ClipboardObject *cpb, size_t length) {
  if (length >= cpb->buffer.length) return 0;
  cpb->buffer.length = length;
  return 1;
}

int
clearClipboardContent (ClipboardObject *cpb) {
  size_t length;
  const wchar_t *characters = getClipboardContent(cpb, &length);

  if (!addClipboardHistory(cpb, characters, length)) return 0;
  truncateClipboardContent(cpb, 0);
  return 1;
}

int
appendClipboardContent (ClipboardObject *cpb, const wchar_t *characters, size_t length) {
  size_t newLength = cpb->buffer.length + length;

  if (newLength > cpb->buffer.size) {
    size_t newSize = newLength | 0XFF;
    wchar_t *newCharacters = allocateCharacters(newSize);

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

    wmemcpy(newCharacters, cpb->buffer.characters, cpb->buffer.length);
    if (cpb->buffer.characters) free(cpb->buffer.characters);
    cpb->buffer.characters = newCharacters;
    cpb->buffer.size = newSize;
  }

  wmemcpy(&cpb->buffer.characters[cpb->buffer.length], characters, length);
  cpb->buffer.length += length;
  return 1;
}

int
setClipboardContent (ClipboardObject *cpb, const wchar_t *characters, size_t length) {
  int truncated = truncateClipboardContent(cpb, 0);
  int appended = appendClipboardContent(cpb, characters, length);
  return truncated || appended;
}

int
appendClipboardContentUTF8 (ClipboardObject *cpb, const char *text) {
  size_t length = strlen(text);
  wchar_t characters[length + 1];
  size_t count = makeWcharsFromUtf8(text, characters, ARRAY_COUNT(characters));
  return appendClipboardContent(cpb, characters, count);
}

int
setClipboardContentUTF8 (ClipboardObject *cpb, const char *text) {
  int truncated = truncateClipboardContent(cpb, 0);
  int appended = appendClipboardContentUTF8(cpb, text);
  return truncated || appended;
}

static void
deallocateClipboardHistoryEntry (void *item, void *data) {
  HistoryEntry *entry = item;
  if (entry->characters) free(entry->characters);
  free(entry);
}

ClipboardObject *
newClipboard (void) {
  ClipboardObject *cpb;

  if ((cpb = malloc(sizeof(*cpb)))) {
    memset(cpb, 0, sizeof(*cpb));

    cpb->buffer.characters = NULL;
    cpb->buffer.size = 0;
    cpb->buffer.length = 0;

    if ((cpb->history.queue = newQueue(deallocateClipboardHistoryEntry, NULL))) {
      return cpb;
    }
  } else {
    logMallocError();
  }

  return NULL;
}

void
destroyClipboard (ClipboardObject *cpb) {
  if (cpb->buffer.characters) free(cpb->buffer.characters);
  deallocateQueue(cpb->history.queue);
  free(cpb);
}

static LockDescriptor *
getMainClipboardLock (void) {
  static LockDescriptor *lock = NULL;
  return getLockDescriptor(&lock, "main-clipboard");
}

void
lockMainClipboard (void) {
  obtainExclusiveLock(getMainClipboardLock());
}

void
unlockMainClipboard (void) {
  releaseLock(getMainClipboardLock());
}

static void
exitMainClipboard (void *data) {
  ClipboardObject **clipboard = data;

  lockMainClipboard();
    destroyClipboard(*clipboard);
    *clipboard = NULL;
  unlockMainClipboard();
}

ClipboardObject *
getMainClipboard (void) {
  static ClipboardObject *clipboard = NULL;

  lockMainClipboard();
    if (!clipboard) {
      if ((clipboard = newClipboard())) {
        onProgramExit("main-clipboard", exitMainClipboard, &clipboard);
      }
    }
  unlockMainClipboard();

  return clipboard;
}

void
onMainClipboardUpdated (void) {
  api.updateParameter(BRLAPI_PARAM_CLIPBOARD_CONTENT, 0);
}

int
setMainClipboardContent (const char *content) {
  ClipboardObject *cpb = getMainClipboard();
  int updated;

  lockMainClipboard();
    updated = setClipboardContentUTF8(cpb, content);
  unlockMainClipboard();

  if (updated) onMainClipboardUpdated();
  return updated;
}

char *
getMainClipboardContent (void) {
  ClipboardObject *cpb = getMainClipboard();
  char *content;

  lockMainClipboard();
    content = getClipboardContentUTF8(cpb);
  unlockMainClipboard();

  return content;
}