File: dasher_editor_external_atspi.cpp

package info (click to toggle)
dasher 5.0.0~beta~repack2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 70,872 kB
  • sloc: xml: 181,314; cpp: 70,860; java: 8,020; python: 3,579; makefile: 939; sh: 324; ansic: 223; perl: 71
file content (426 lines) | stat: -rw-r--r-- 13,021 bytes parent folder | download | duplicates (5)
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
#include <X11/keysym.h>
#include <string.h>

extern "C" {
  #include <atspi/atspi.h>
}

#include "dasher_editor_external.h"
#include "dasher_editor_private.h"

typedef struct _DasherEditorExternalPrivate DasherEditorExternalPrivate;

struct _DasherEditorExternalPrivate {
  AtspiEventListener *pFocusListener;
  AtspiEventListener *pCaretListener;
  AtspiText *pAccessibleText;
  // Used to distinguish dasher initiated cursor moves from external moves so that the
  // interface stays in control mode. When dasher moves the caret it will also set
  // current_caret_position.
  glong current_caret_position;
};

void dasher_editor_external_handle_focus(DasherEditor *pSelf, const AtspiEvent *pEvent);
void dasher_editor_external_handle_caret(DasherEditor *pSelf, const AtspiEvent *pEvent);

void focus_listener(AtspiEvent *pEvent, void *pUserData);
void caret_listener(AtspiEvent *pEvent, void *pUserData);

static void listen_to_bus(DasherEditor *);
static void unlisten_to_bus(DasherEditor *);

bool
initSPI() {
#ifdef DEBUG_ATSPI
  int ret = atspi_init();
  printf("atspi_init() returned %d\n", ret);
  return (ret <= 1);
#else
  return (atspi_init() <= 1);
#endif
}

void
dasher_editor_external_finalize(GObject *pSelf) {
  DasherEditorPrivate *pPrivate = DASHER_EDITOR_GET_PRIVATE(pSelf);
  DasherEditorExternalPrivate *p = pPrivate->pExtPrivate;

#ifdef DEBUG_ATSPI
  printf("dasher_editor_external_finalize()\n");
#endif

  unlisten_to_bus(DASHER_EDITOR(pSelf));

  g_object_unref(p->pFocusListener);
  g_object_unref(p->pCaretListener);

  delete p;
}

void
dasher_editor_external_create_buffer(DasherEditor *pSelf) {
  DasherEditorPrivate *pPrivate = DASHER_EDITOR_GET_PRIVATE(pSelf);
  DasherEditorExternalPrivate *p;

#ifdef DEBUG_ATSPI
  printf("dasher_editor_external_create_buffer()\n");
#endif

  if(!initSPI()) {
    g_message("Could not initialise SPI - accessibility options disabled");
    return;
  }
  p = new DasherEditorExternalPrivate;
  p->pFocusListener = atspi_event_listener_new(focus_listener, pSelf, NULL);
  p->pCaretListener = atspi_event_listener_new(caret_listener, pSelf, NULL);
  p->pAccessibleText = NULL;
  p->current_caret_position = -1;
  pPrivate->pExtPrivate = p;

#ifdef DEBUG_ATSPI
  printf("dasher_editor_external_create_buffer: pPrivate=%p, pExtPrivate=%p\n", pPrivate, p);
#endif
}

static void
listen_to_bus(DasherEditor *pSelf) {
  DasherEditorPrivate *p = DASHER_EDITOR_GET_PRIVATE(pSelf);
  //atspi_event_listener_register(p->pExtPrivate->pFocusListener, "focus:", NULL);
  atspi_event_listener_register(p->pExtPrivate->pFocusListener, "object:state-changed:focused", NULL);
  atspi_event_listener_register(p->pExtPrivate->pCaretListener, "object:text-caret-moved", NULL);
}

static void
unlisten_to_bus(DasherEditor *pSelf) {
  DasherEditorPrivate *p = DASHER_EDITOR_GET_PRIVATE(pSelf);
//  atspi_event_listener_deregister(p->pExtPrivate->pFocusListener, "focus:", NULL);
  atspi_event_listener_deregister(p->pExtPrivate->pFocusListener, "object:state-changed:focused", NULL);
  atspi_event_listener_deregister(p->pExtPrivate->pCaretListener, "object:text-caret-moved", NULL);
}

void
dasher_editor_external_toggle_direct_mode(DasherEditor *pSelf, bool direct) {
  DasherEditorPrivate *p = DASHER_EDITOR_GET_PRIVATE(pSelf);
  p->pExtPrivate->current_caret_position = -1;
  if (direct)
    listen_to_bus(pSelf);
  else
    unlisten_to_bus(pSelf);
}

void
dasher_editor_external_output(DasherEditor *pSelf, const char *szText, int iOffset /* unused */) {
  if (!initSPI()) return;
  atspi_generate_keyboard_event(0, szText, ATSPI_KEY_STRING, NULL);
}

void
dasher_editor_external_delete(DasherEditor *pSelf, int iLength, int iOffset) {
  if (!initSPI()) return;

  atspi_generate_keyboard_event(XK_BackSpace, NULL, ATSPI_KEY_SYM, NULL);
}

std::string
dasher_editor_external_get_context(DasherEditor *pSelf, int iOffset, int iLength) {
  DasherEditorPrivate *pPrivate = DASHER_EDITOR_GET_PRIVATE(pSelf);

#ifdef DEBUG_ATSPI
  printf("dasher_editor_external_get_context(pPrivate=%p)\n", pPrivate);
#endif

  DASHER_ASSERT(pPrivate->pExtPrivate != NULL);
  AtspiText *textobj = pPrivate->pExtPrivate->pAccessibleText;

  if (textobj != nullptr) {
    auto text = atspi_text_get_text(textobj, iOffset, iOffset + iLength, NULL);
    if (text != nullptr) {
      std::string context = text;
      g_free(text);
      return context;
    }
  }
  return "";
}

int
dasher_editor_external_get_offset(DasherEditor *pSelf) {
  DasherEditorPrivate *pPrivate = DASHER_EDITOR_GET_PRIVATE(pSelf);

#ifdef DEBUG_ATSPI
  printf("dasher_editor_external_get_offset()\n");
#endif

  DASHER_ASSERT(pPrivate->pExtPrivate != NULL);
  AtspiText *textobj = pPrivate->pExtPrivate->pAccessibleText;
  AtspiRange *range;
  int ret;
  
  if (!textobj) {
#ifdef DEBUG_ATSPI
    printf("no textobj\n");
#endif
    return 0;
  }

  if (atspi_text_get_n_selections(textobj, NULL) == 0)
    return atspi_text_get_caret_offset(textobj, NULL);

  range = atspi_text_get_selection(textobj, 0, NULL);
  ret = std::min(range->start_offset, range->end_offset);
  g_free(range);

  return ret;
}

void
dasher_editor_external_handle_focus(DasherEditor *pSelf, const AtspiEvent *pEvent) {
  DasherEditorPrivate *pPrivate = DASHER_EDITOR_GET_PRIVATE(pSelf);

#ifdef DEBUG_ATSPI
  printf("dasher_editor_external_handle_focus()\n");
#endif

  DASHER_ASSERT(pPrivate->pExtPrivate != NULL);
  pPrivate->pExtPrivate->current_caret_position = -1;
  AtspiText *textobj = pPrivate->pExtPrivate->pAccessibleText;

  if (textobj) {
    g_object_unref(textobj);
    textobj = NULL;
  }

  AtspiAccessible *acc = pEvent->source;
  g_object_ref(acc);
  
#ifdef DEBUG_ATSPI
  printf("%s from %s, %s, %s\n",
    pEvent->type,
    atspi_accessible_get_name(acc, NULL),
    atspi_accessible_get_role_name(acc, NULL),
    atspi_accessible_get_description(acc, NULL));
#endif

  textobj = atspi_accessible_get_text_iface(acc);
  pPrivate->pExtPrivate->pAccessibleText = textobj;
  if (textobj) {
    g_object_ref(textobj);

    //ACL: in old code, external_buffer emitted signal, for which the editor_external had
    // registered a callback, which then emitted the same/corresponding signal from the
    // editor_external; so by combining buffer into editor, seems we don't need any of that
    // and can just emit the signal from the editor directly. However, the callback also said:
    //TODO: plumb signal back into control
    // ...if that makes any sense?

    g_signal_emit_by_name(G_OBJECT(pSelf), "buffer_changed", G_OBJECT(pSelf), NULL, NULL);
  }

  g_object_unref(acc);
}

void
dasher_editor_external_handle_caret(DasherEditor *pSelf, const AtspiEvent *pEvent) {
  DasherEditorPrivate *pPrivate = DASHER_EDITOR_GET_PRIVATE(pSelf);

#ifdef DEBUG_ATSPI
  printf("dasher_editor_external_handle_caret()\n");
#endif

  DASHER_ASSERT(pPrivate->pExtPrivate != NULL);
  AtspiText *textobj = pPrivate->pExtPrivate->pAccessibleText;

  if (textobj) {
    g_object_unref(textobj);
    textobj = NULL;
  }

  AtspiAccessible *acc = pEvent->source;
  g_object_ref(acc);
  
#ifdef DEBUG_ATSPI
  printf("%s from %s, %s, %s\n",
    pEvent->type,
    atspi_accessible_get_name(acc, NULL),
    atspi_accessible_get_role_name(acc, NULL),
    atspi_accessible_get_description(acc, NULL));
#endif

  textobj = atspi_accessible_get_text(acc);
  pPrivate->pExtPrivate->pAccessibleText = textobj;
  if (textobj) {
    // If dasher moved the caret don't send a notification to the control.
    glong caret = atspi_text_get_caret_offset(textobj, NULL);
    if (caret == pPrivate->pExtPrivate->current_caret_position) {
      return;
    }
    pPrivate->pExtPrivate->current_caret_position = -1;

    g_object_ref(textobj);

    //ACL: in old code, external_buffer emitted signal, for which the editor_external had
    // registered a callback, which then emitted the same/corresponding signal from the
    // editor_external; so by combining buffer into editor, seems we don't need any of that
    // and can just emit the signal from the editor directly. However, the callback also said:
    //TODO: plumb signal back into control
    // ...if that makes any sense?

    g_signal_emit_by_name(G_OBJECT(pSelf), "context_changed", G_OBJECT(pSelf), NULL, NULL);
    pPrivate->bInControlAction = false;
  }
#ifdef DEBUG_ATSPI
  else {
    printf("XXX Received text-caret-moved from source which doesn't implemenent AtspiText\n");
  }
#endif

  g_object_unref(acc);
}

void
focus_listener(AtspiEvent *pEvent, void *pUserData) {
  dasher_editor_external_handle_focus(DASHER_EDITOR(pUserData), pEvent);
}

void
caret_listener(AtspiEvent *pEvent, void *pUserData) {
  dasher_editor_external_handle_caret(DASHER_EDITOR(pUserData), pEvent);
}

void dasher_editor_external_move(DasherEditor *pSelf, bool bForwards, Dasher::CControlManager::EditDistance iDist) {
  DasherEditorPrivate *pPrivate = DASHER_EDITOR_GET_PRIVATE(pSelf);
  DASHER_ASSERT(pPrivate->pExtPrivate != nullptr);
  AtspiText *textobj = pPrivate->pExtPrivate->pAccessibleText;
  if (textobj == nullptr) {
    return;
  }

  GError *err = nullptr;
  auto caret_pos = atspi_text_get_caret_offset(textobj, &err);
  if (err != nullptr) {
    fprintf(stderr, "Failed to get the caret: %s\n", err->message);
    g_error_free(err);
    return;
  }

  auto length = atspi_text_get_character_count(textobj, &err);
  if (err != nullptr) {
    fprintf(stderr, "Failed to get the character count: %s\n", err->message);
    g_error_free(err);
    return;
  }
  if (length == 0) {
    return;
  }

  AtspiTextRange* range = nullptr;
  glong new_position = caret_pos;

  AtspiTextBoundaryType boundary = ATSPI_TEXT_BOUNDARY_CHAR;
  switch (iDist) {
    case Dasher::CControlManager::EDIT_CHAR:
      if (bForwards) {
        new_position = std::min<glong>(caret_pos + 1, length - 1);
      } else {
        new_position = std::max<glong>(caret_pos - 1, 0);
      }
      break;
    case Dasher::CControlManager::EDIT_FILE:
      if (bForwards) {
        new_position = length - 1;
      } else {
        new_position = 0;
      }
      break;
    case Dasher::CControlManager::EDIT_WORD:
      boundary = ATSPI_TEXT_BOUNDARY_WORD_START;
      break;
    case Dasher::CControlManager::EDIT_LINE:
      boundary = ATSPI_TEXT_BOUNDARY_LINE_START;
      break;
    case Dasher::CControlManager::EDIT_SENTENCE:
      boundary = ATSPI_TEXT_BOUNDARY_SENTENCE_START;
      break;
    default:
      break;
  }
  if (boundary != ATSPI_TEXT_BOUNDARY_CHAR) {
    if (bForwards) {
      range = atspi_text_get_text_after_offset(textobj, caret_pos, boundary, &err);
    } else {
      range = atspi_text_get_text_before_offset(textobj, caret_pos, boundary, &err);
    }
    if (err != nullptr) {
      fprintf(stderr, "Failed to get the text after/befor the offset: %s\n", err->message);
      g_error_free(err);
      return;
    }
    if (range != nullptr) {
      new_position = range->start_offset;
    }
    g_free(range);
  }
  atspi_text_set_caret_offset(textobj, new_position, &err);
  if (err != nullptr) {
    fprintf(stderr, "Failed to set the caret offset: %s\n", err->message);
    g_error_free(err);
    return;
  }
  pPrivate->pExtPrivate->current_caret_position = new_position;
}

std::string dasher_editor_external_get_text_around_cursor(
    DasherEditor *pSelf, Dasher::CControlManager::EditDistance distance) {
  DasherEditorPrivate *pPrivate = DASHER_EDITOR_GET_PRIVATE(pSelf);
  DASHER_ASSERT(pPrivate->pExtPrivate != nullptr);
  AtspiText *textobj = pPrivate->pExtPrivate->pAccessibleText;
  if (textobj == nullptr) {
    return "";
  }
  GError *err = nullptr;
  auto caret_pos = atspi_text_get_caret_offset(textobj, &err);
  if (err != nullptr) {
    fprintf(stderr, "Failed to get the caret offset: %s\n", err->message);
    g_error_free(err);
    return "";
  }

  std::string text;
  AtspiTextGranularity granularity;
  switch (distance) {
    case Dasher::CControlManager::EDIT_FILE: {
      auto length = atspi_text_get_character_count(textobj, nullptr);
      auto gtext = atspi_text_get_text(textobj, 0, length, nullptr);
      text = gtext;
      g_free(gtext);
      return text;
    }
      break;
    case Dasher::CControlManager::EDIT_WORD:
      granularity = ATSPI_TEXT_GRANULARITY_WORD;
      break;
    case Dasher::CControlManager::EDIT_LINE:
      granularity = ATSPI_TEXT_GRANULARITY_LINE;
      break;
    case Dasher::CControlManager::EDIT_SENTENCE:
      granularity = ATSPI_TEXT_GRANULARITY_SENTENCE;
      break;
    case Dasher::CControlManager::EDIT_PARAGRAPH:
      // TODO: figure out why ATSPI_TEXT_GRANULARITY_PARAGRAPH doesn't work.
      granularity = ATSPI_TEXT_GRANULARITY_PARAGRAPH;
      break;
    default:
      return "";
  }
  auto* range = atspi_text_get_string_at_offset(textobj, caret_pos, granularity, &err);
  if (err != nullptr) {
    fprintf(stderr, "Failed to get the caret offset: %s\n", err->message);
    g_error_free(err);
    return "";
  }
  if (range != nullptr) {
    text = range->content;
    g_free(range);
  }
  return text;
}