summaryrefslogtreecommitdiff
path: root/src/bin/termptyext.c
blob: 1c183b0144f2211e39cb026363e83df4b7b839a5 (plain)
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
#include "private.h"
#include <Elementary.h>
#include "termio.h"
#include "termpty.h"
#include "termptyops.h"
#include "termiointernals.h"
#include "tytest.h"
#include <assert.h>

#undef CRITICAL
#undef ERR
#undef WRN
#undef INF
#undef DBG

#define CRITICAL(...) EINA_LOG_DOM_CRIT(_termpty_log_dom, __VA_ARGS__)
#define ERR(...)      EINA_LOG_DOM_ERR(_termpty_log_dom, __VA_ARGS__)
#define WRN(...)      EINA_LOG_DOM_WARN(_termpty_log_dom, __VA_ARGS__)
#define INF(...)      EINA_LOG_DOM_INFO(_termpty_log_dom, __VA_ARGS__)
#define DBG(...)      EINA_LOG_DOM_DBG(_termpty_log_dom, __VA_ARGS__)

//// extended terminology escape handling goes in here
//
// this is where escapes get handled *IF* the termpty layer needs to interpret
// them itself for some reason. if it returns EINA_FALSE, it means the escape
// is to be passed onto termio layer as a callback and handled there after
// this code. an extended escape may be handled in here exclusively (return
// EINA_TRUE), handled here first, then in termio (EINA_FALSE return) or not
// handled here at all and just passed to termio to figure it out (return
// EINA_FALSE).

#if defined(ENABLE_TESTS) || defined(ENABLE_TEST_UI)

static int
_tytest_arg_get(const Eina_Unicode *buf, int *value)
{
   int len = 0;
   int sum = 0;

   if (*buf == ';')
     {
        len++;
     }

   while (buf[len] >= '0' && buf[len] <= '9')
     {
        sum *= 10;
        sum += buf[len] - '0';
        len++;
     }
   *value = sum;
   return len;
}

/**
 * MODIFIERS is a bit field with the following values:
 *   - Alt
 *   - Shift
 *   - Ctrl
 *   - Super
 *   - Meta
 *   - Hyper
 *   - ISO_Level3_Shift
 *   - AltGr
 */
static int
_tytest_modifiers_get(const Eina_Unicode *buf, Termio_Modifiers *m)
{
   Termio_Modifiers modifier = {};
   int value = 0;
   int len = _tytest_arg_get(buf, &value);

   modifier.alt = !!(value & (1 << 0));
   modifier.shift = !!(value & (1 << 1));
   modifier.ctrl = !!(value & (1 << 2));
   modifier.super = !!(value & (1 << 3));
   modifier.meta = !!(value & (1 << 4));
   modifier.hyper = !!(value & (1 << 5));
   modifier.iso_level3_shift = !!(value & (1 << 6));
   modifier.altgr = !!(value & (1 << 7));

   *m = modifier;
   return len;
}

/**
 * FLAGS can be:
 * - 0
 * - 1: DOUBLE_CLICK
 * - 2: TRIPLE_CLICK
 */

/*
 * Format is td;X;Y;BUTTON;MODIFIERS;FLAGS
 */
static void
_handle_mouse_down(Termpty *ty,
                   const Eina_Unicode *buf)
{
   Evas_Event_Mouse_Down ev = {};
   Termio *sd = termio_get_from_obj(ty->obj);
   Termio_Modifiers modifiers = {};
   int value;

   /* X */
   value = 0;
   buf += _tytest_arg_get(buf, &value);
   ev.canvas.x = value;
   /* Y */
   value = 0;
   buf += _tytest_arg_get(buf, &value);
   ev.canvas.y = value;
   /* BUTTON */
   value = 0;
   buf += _tytest_arg_get(buf, &value);
   ev.button = value;
   /* MODIFIERS */
   buf += _tytest_modifiers_get(buf, &modifiers);
   /* FLAGS */
   value = 0;
   buf +=_tytest_arg_get(buf, &value);
   ev.flags = value;

   termio_internal_mouse_down(sd, &ev, modifiers);
}

/*
 * Format is tu;X;Y;BUTTON;MODIFIERS;FLAGS
 */
static void
_handle_mouse_up(Termpty *ty,
                 const Eina_Unicode *buf)
{
   Evas_Event_Mouse_Up ev = {};
   Termio *sd = termio_get_from_obj(ty->obj);
   Termio_Modifiers modifiers = {};
   int value;

   /* X */
   value = 0;
   buf += _tytest_arg_get(buf, &value);
   ev.canvas.x = value;
   /* Y */
   value = 0;
   buf += _tytest_arg_get(buf, &value);
   ev.canvas.y = value;
   /* BUTTON */
   value = 0;
   buf += _tytest_arg_get(buf, &value);
   ev.button = value;
   /* MODIFIERS */
   buf += _tytest_modifiers_get(buf, &modifiers);
   /* FLAGS */
   value = 0;
   buf +=_tytest_arg_get(buf, &value);
   ev.flags = value;

   termio_internal_mouse_up(sd, &ev, modifiers);
}

/*
 * Format is tm;X;Y;MODIFIERS
 */
static void
_handle_mouse_move(Termpty *ty,
                   const Eina_Unicode *buf)
{
   Evas_Event_Mouse_Move ev = {};
   Termio *sd = termio_get_from_obj(ty->obj);
   Termio_Modifiers modifiers = {};
   int value;

   /* X */
   value = 0;
   buf += _tytest_arg_get(buf, &value);
   ev.cur.canvas.x = value;
   /* Y */
   value = 0;
   buf += _tytest_arg_get(buf, &value);
   ev.cur.canvas.y = value;
   /* MODIFIERS */
   buf += _tytest_modifiers_get(buf, &modifiers);

   termio_internal_mouse_move(sd, &ev, modifiers);
}

/*
 * Format is tw;X;Y;DIRECTION;VALUE;MODIFIERS
 * DIRECTION: 1 to go up, 0 to go down
 */
static void
_handle_mouse_wheel(Termpty *ty,
                    const Eina_Unicode *buf)
{
   Evas_Event_Mouse_Wheel ev = {};
   Termio *sd = termio_get_from_obj(ty->obj);
   Termio_Modifiers modifiers = {};
   int value;

   /* X */
   value = 0;
   buf += _tytest_arg_get(buf, &value);
   ev.canvas.x = value;
   /* Y */
   value = 0;
   buf += _tytest_arg_get(buf, &value);
   ev.canvas.y = value;
   /* DIRECTION */
   value = 0;
   buf += _tytest_arg_get(buf, &value);
   ev.z = (value == 0)? 1 : -1;
   /* VALUE */
   value = 0;
   buf += _tytest_arg_get(buf, &value);
   ev.z *= value;
   /* MODIFIERS */
   buf += _tytest_modifiers_get(buf, &modifiers);

   termio_internal_mouse_wheel(sd, &ev, modifiers);
}

static void
_handle_selection_is(Termpty *ty,
                     const Eina_Unicode *buf)
{
   size_t len = 0;
   Termio *sd;
   const char *sel, *s;

   assert(ty->selection.is_active);

   sd = termio_get_from_obj(ty->obj);
   sel = s = termio_internal_get_selection(sd, &len);

   assert(s != NULL && "no selection");

   while (*buf)
     {
        int idx = 0;
        Eina_Unicode u = eina_unicode_utf8_next_get(s, &idx);

        /* skip spurious carriage returns */
        if (*buf != '\r')
          {
             assert(*buf == u && "unexpected character in selection");
             s += idx;
          }
        buf++;
     }
   eina_stringshare_del(sel);
}

static void
_handle_force_render(Termpty *ty)
{
   int preedit_x = 0, preedit_y = 0;
   Termio *sd = termio_get_from_obj(ty->obj);

   termio_internal_render(sd, 0, 0, &preedit_x, &preedit_y);
}

/*
 * Format is tc;C;V
 * where C is 0 for top-left, 1 for down-right
 * and V is 0 to unset, 1 to set
 */
static void
_handle_corner(Termpty *ty, const Eina_Unicode *buf)
{
   Termio *sd = termio_get_from_obj(ty->obj);
   int value;
   int corner;

   /* C */
   corner = 0;
   buf += _tytest_arg_get(buf, &corner);

   /* V */
   value = 0;
   buf += _tytest_arg_get(buf, &value);

   if (corner == 0)
     {
        sd->top_left = !! value;
     }
   else
     {
        sd->bottom_right = !! value;
     }
}

/* Testing escape codes that start with '\033}t' and end with '\0'
 * Then,
 * - 'c': set/unset top-left/down-right
 * - 'd': mouse down:
 * - 'u': mouse up;
 * - 'm': mouse move;
 * - 'r': force rendering and possibly remove selection;
 * - 'n': assert there is no selection
 * - 's': assert selection is what follows till '\0'
 */
static void
tytest_handle_escape_codes(Termpty *ty,
                           const Eina_Unicode *buf)
{
   switch (buf[0])
     {
      case 'c':
        _handle_corner(ty, buf + 1);
         break;
      case 'd':
        _handle_mouse_down(ty, buf + 1);
        break;
      case 'm':
        _handle_mouse_move(ty, buf + 1);
        break;
      case 'n':
        assert(!ty->selection.is_active);
        break;
      case 'r':
        _handle_force_render(ty);
        break;
      case 's':
        _handle_selection_is(ty, buf + 1);
        break;
      case 'u':
        _handle_mouse_up(ty, buf + 1);
        break;
      case 'w':
        _handle_mouse_wheel(ty, buf + 1);
        break;
      default:
        abort();
        break;
     }
}
#endif

#if defined(ENABLE_TESTS) || defined(ENABLE_TEST_UI)
#define ARG_USED_FOR_TESTS
#else
#define ARG_USED_FOR_TESTS EINA_UNUSED
#endif

Eina_Bool
termpty_ext_handle(Termpty *ty ARG_USED_FOR_TESTS,
                   const Eina_Unicode *buf ARG_USED_FOR_TESTS,
                   size_t blen EINA_UNUSED)
{
   switch (buf[0]) // major opcode
     {
#if defined(ENABLE_TESTS) || defined(ENABLE_TEST_UI)
      case 't':
        tytest_handle_escape_codes(ty, buf + 1);
        return EINA_TRUE;
        break;
#endif
      default:
        break;
     }
   return EINA_FALSE;
}
#undef ARG_USED_FOR_TESTS