~ubuntu-branches/ubuntu/saucy/geis/saucy-proposed

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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/**
 * @file geis_backend_test_fixture.c
 * @brief GEIS mock back end test fixture implementation
 *
 * Copyright 2010 Canonical Ltd.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of version 3 of the GNU Lesser General Public License as published
 * by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "geis_config.h"
#include "geis_backend.h"
#include "geis_backend_protected.h"

#include "geis_attr.h"
#include "geis_class.h"
#include "geis_device.h"
#include "geis_event.h"
#include "geis_filter.h"
#include "geis_filter_term.h"
#include "geis_frame.h"
#include "geis_group.h"
#include "geis_logging.h"
#include "geis_private.h"
#include "geis_subscription.h"
#include "geis_test_api.h"
#include "geis_touch.h"
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>


static inline GeisSize
_min(GeisSize a, GeisSize b)
{
  return (a < b) ? a : b;
}


static inline GeisSize
_max(GeisSize a, GeisSize b)
{
  return (a > b) ? a : b;
}

static GeisSize   g_min_touches = 1;
static GeisSize   g_max_touches = 5;


typedef struct GeisBackendTestFixture
{
  Geis                tf_geis;
} *GeisBackendTestFixture;


typedef struct TestBackendToken
{
  struct GeisBackendToken base;
  GeisBackendTestFixture  be;
  GeisInteger             min_touches;
  GeisInteger             max_touches;
} *TestBackendToken;

static inline TestBackendToken
_tbtoken_from_token(GeisBackendToken bet)
{
  return (TestBackendToken)bet;
}

static GeisBackendToken _token_clone(GeisBackendToken);
static void             _token_finalize(GeisBackendToken);
static void             _token_compose(GeisBackendToken, GeisBackendToken);
static GeisStatus       _token_activate(GeisBackendToken, GeisSubscription);
static GeisStatus       _token_deactivate(GeisBackendToken, GeisSubscription);
static void             _token_free_subscription_pdata(GeisBackendToken, GeisSubscription);

static struct GeisBackendTokenVtable _token_vtbl = {
  _token_clone,
  _token_finalize,
  _token_compose,
  _token_activate,
  _token_deactivate,
  _token_free_subscription_pdata
};

static GeisGestureClass g_poke_class = NULL;

static GeisStatus
_add_device_term(GeisBackendToken     token GEIS_UNUSED,
                 void                *context GEIS_UNUSED,
                 GeisString           name,
                 GeisFilterOperation  op GEIS_UNUSED,
                 void                *value GEIS_UNUSED)
{
  GeisStatus status = GEIS_STATUS_SUCCESS;
  geis_error("called: name=%s", name);
  return status;
}


static void
_create_test_devices(GeisBackendTestFixture tf)
{
  GeisDevice device = geis_device_new("abs-test-device", 0);
  struct GeisFilterableAttribute attrs[] = {
    { GEIS_DEVICE_ATTRIBUTE_NAME,         GEIS_ATTR_TYPE_STRING,  _add_device_term, NULL },
    { GEIS_DEVICE_ATTRIBUTE_ID,           GEIS_ATTR_TYPE_INTEGER, _add_device_term, NULL },
    { GEIS_DEVICE_ATTRIBUTE_TOUCHES,      GEIS_ATTR_TYPE_INTEGER, _add_device_term, NULL },
    { GEIS_DEVICE_ATTRIBUTE_DIRECT_TOUCH, GEIS_ATTR_TYPE_BOOLEAN, _add_device_term, NULL }
  };
  GeisSize attr_count = sizeof(attrs) / sizeof(struct GeisFilterableAttribute);

  geis_register_device(tf->tf_geis, device, attr_count, attrs);
}


static GeisStatus
_add_class_term(GeisBackendToken     gbtoken,
                void                *context GEIS_UNUSED,
                GeisString           name,
                GeisFilterOperation  op,
                void                *value)
{
  GeisStatus status = GEIS_STATUS_SUCCESS;
  TestBackendToken token = _tbtoken_from_token(gbtoken);

  if (0 == strcmp(name, GEIS_CLASS_ATTRIBUTE_NAME)
      && op == GEIS_FILTER_OP_EQ)
  {
    GeisString class_name = (GeisString)value;
    geis_debug("called: attr=%s name=\"%s\"", name, class_name);
  }
  else if (0 == strcmp(name, GEIS_GESTURE_ATTRIBUTE_TOUCHES))
  {
    GeisInteger touches = *(GeisInteger*)value;
    switch (op)
    {
      case GEIS_FILTER_OP_GT:
	token->min_touches = _max(token->min_touches, touches+1);
	break;
      case GEIS_FILTER_OP_GE:
	token->min_touches = _max(token->min_touches, touches);
	break;
      case GEIS_FILTER_OP_LT:
	token->max_touches = _min(touches-1, token->max_touches);
	break;
      case GEIS_FILTER_OP_LE:
	token->max_touches = _min(touches, token->max_touches);
	break;
      case GEIS_FILTER_OP_EQ:
	token->min_touches = _max(token->min_touches, touches);
	token->max_touches = _min(touches, token->max_touches);
	break;
      case GEIS_FILTER_OP_NE:
	break;
    }
    geis_debug("called: attr=%s touches=\"%d\" min=%d max=%d", name, touches, token->min_touches, token->max_touches);
  }
  return status;
}


static void
_create_test_classes(GeisBackendTestFixture tf)
{
  if (!g_poke_class)
  {
    g_poke_class = geis_gesture_class_new("poke", 2100);
    struct GeisFilterableAttribute attrs[] = {
      { GEIS_CLASS_ATTRIBUTE_NAME,      GEIS_ATTR_TYPE_STRING,  _add_class_term, g_poke_class },
      { GEIS_CLASS_ATTRIBUTE_ID,        GEIS_ATTR_TYPE_INTEGER, _add_class_term, g_poke_class },
      { GEIS_GESTURE_ATTRIBUTE_TOUCHES, GEIS_ATTR_TYPE_INTEGER, _add_class_term, g_poke_class }
    };
    GeisSize attr_count = sizeof(attrs) / sizeof(struct GeisFilterableAttribute);

    geis_register_gesture_class(tf->tf_geis, g_poke_class, attr_count, attrs);
  }
}


static void
_construct(void *mem, Geis geis)
{
  GeisBackendTestFixture tf = (GeisBackendTestFixture)mem;
  tf->tf_geis = geis;

  _create_test_devices(tf);
  _create_test_classes(tf);
  geis_post_event(tf->tf_geis, geis_event_new(GEIS_EVENT_INIT_COMPLETE));
}


static void 
_finalize(GeisBackend be)
{
  GeisBackendTestFixture tf GEIS_UNUSED = (GeisBackendTestFixture)be;
}


static GeisBackendToken
_create_token(GeisBackend be, GeisBackendTokenInitState init_state)
{
  TestBackendToken token = NULL;
  token = calloc(1, sizeof(struct TestBackendToken));
  if (token)
  {
    token->base.vtbl = &_token_vtbl;
    token->be = (GeisBackendTestFixture)be;
    if (init_state == GEIS_BACKEND_TOKEN_INIT_ALL)
    {
      token->min_touches = g_min_touches;
      token->max_touches = g_max_touches;
    }
    else
    {
      token->min_touches = g_max_touches;
      token->max_touches = g_min_touches;
    }
  }
  return (GeisBackendToken)token;
}


static GeisStatus
_gmock_accept_gesture(GeisBackend   be GEIS_UNUSED,
                      GeisGroup     group GEIS_UNUSED,
                      GeisGestureId gesture_ID GEIS_UNUSED)
{
  return GEIS_STATUS_SUCCESS;
}


static GeisStatus
_gmock_reject_gesture(GeisBackend   be GEIS_UNUSED,
                      GeisGroup     group GEIS_UNUSED,
                      GeisGestureId gesture_ID GEIS_UNUSED)
{
  return GEIS_STATUS_SUCCESS;
}


static GeisStatus
_gmock_get_configuration(GeisBackend      be GEIS_UNUSED,
                         GeisSubscription subscription GEIS_UNUSED,
                         GeisString       item_name GEIS_UNUSED,
                         GeisPointer      item_value GEIS_UNUSED)
{
  return GEIS_STATUS_NOT_SUPPORTED;
}


static GeisStatus
_gmock_set_configuration(GeisBackend      be GEIS_UNUSED,
                         GeisSubscription subscription GEIS_UNUSED,
                         GeisString       item_name GEIS_UNUSED,
                         GeisPointer      item_value GEIS_UNUSED)
{
  return GEIS_STATUS_NOT_SUPPORTED;
}


static struct GeisBackendVtable tf_vtbl = {
  _construct,
  _finalize,
  _create_token,
  _gmock_accept_gesture,
  _gmock_reject_gesture,
  _gmock_get_configuration,
  _gmock_set_configuration
};


/*
 * Generates a gesture event, the contets of which varies accoring to what's in
 * the token.
 */
static void
_create_gesture_events_for_token(TestBackendToken token)
{
  int i;

  GeisFloat    attr_float;
  GeisInteger  attr_int;
  GeisAttr     attr = NULL;

  GeisEvent    event = geis_event_new(GEIS_EVENT_GESTURE_BEGIN);
  GeisGroupSet groupset = geis_groupset_new();
  GeisGroup    group = geis_group_new(1);
  GeisAttr     group_attr = geis_attr_new(GEIS_EVENT_ATTRIBUTE_GROUPSET,
                                          GEIS_ATTR_TYPE_POINTER,
                                          groupset);
  GeisTouchSet touchset = geis_touchset_new();
  GeisAttr     touch_attr = geis_attr_new(GEIS_EVENT_ATTRIBUTE_TOUCHSET,
                                          GEIS_ATTR_TYPE_POINTER,
                                          touchset);

  GeisFrame frame = geis_frame_new(1);
  geis_frame_set_is_class(frame, g_poke_class);

  attr_int = 13;
  attr = geis_attr_new(GEIS_GESTURE_ATTRIBUTE_DEVICE_ID,
                       GEIS_ATTR_TYPE_INTEGER,
                       &attr_int);
  geis_frame_add_attr(frame, attr);

  attr_int = 1;
  attr = geis_attr_new(GEIS_GESTURE_ATTRIBUTE_TIMESTAMP,
                       GEIS_ATTR_TYPE_INTEGER,
                       &attr_int);
  geis_frame_add_attr(frame, attr);

  attr_int = 2;
  attr = geis_attr_new(GEIS_GESTURE_ATTRIBUTE_ROOT_WINDOW_ID,
                       GEIS_ATTR_TYPE_INTEGER,
                       &attr_int);
  geis_frame_add_attr(frame, attr);

  attr_int = 3;
  attr = geis_attr_new(GEIS_GESTURE_ATTRIBUTE_EVENT_WINDOW_ID,
                       GEIS_ATTR_TYPE_INTEGER,
                       &attr_int);
  geis_frame_add_attr(frame, attr);

  attr_int = 4;
  attr = geis_attr_new(GEIS_GESTURE_ATTRIBUTE_CHILD_WINDOW_ID,
                       GEIS_ATTR_TYPE_INTEGER,
                       &attr_int);
  geis_frame_add_attr(frame, attr);

  attr_float = 123.456;
  attr = geis_attr_new(GEIS_GESTURE_ATTRIBUTE_FOCUS_X,
                       GEIS_ATTR_TYPE_FLOAT,
                       &attr_float);
  geis_frame_add_attr(frame, attr);

  attr_float = 987.654;
  attr = geis_attr_new(GEIS_GESTURE_ATTRIBUTE_FOCUS_Y,
                       GEIS_ATTR_TYPE_FLOAT,
                       &attr_float);
  geis_frame_add_attr(frame, attr);

  attr = geis_attr_new(GEIS_GESTURE_ATTRIBUTE_GESTURE_NAME,
                       GEIS_ATTR_TYPE_STRING,
                       "mock gesture");
  geis_frame_add_attr(frame, attr);

  attr = geis_attr_new(GEIS_GESTURE_ATTRIBUTE_TOUCHES,
                       GEIS_ATTR_TYPE_INTEGER,
                       &token->min_touches);
  geis_frame_add_attr(frame, attr);

  for (i = 0; i < token->min_touches; ++i)
  {
    GeisTouch    touch = geis_touch_new(1);
    geis_touchset_insert(touchset, touch);
    geis_frame_add_touchid(frame, geis_touch_id(touch));
  }

  geis_group_insert_frame(group, frame);

  geis_groupset_insert(groupset, group);

  geis_event_add_attr(event, group_attr);
  geis_event_add_attr(event, touch_attr);

  geis_post_event(token->be->tf_geis, event);
}


static GeisBackendToken
_token_clone(GeisBackendToken gbtoken)
{
  TestBackendToken token = _tbtoken_from_token(gbtoken);
  TestBackendToken new_token = calloc(1, sizeof(struct TestBackendToken));
  if (new_token)
  {
    memcpy(new_token, token, sizeof(struct TestBackendToken));
    return &new_token->base;
  }
  return NULL;
}


void             
_token_finalize(GeisBackendToken gbtoken GEIS_UNUSED)
{
}


void 
_token_compose(GeisBackendToken lhs, GeisBackendToken rhs)
{
  TestBackendToken token1 = _tbtoken_from_token(lhs);
  TestBackendToken token2 = _tbtoken_from_token(rhs);
  token1->min_touches = _min(token1->min_touches, token2->min_touches);
  token1->max_touches = _max(token1->max_touches, token2->max_touches);
}


GeisStatus             
_token_activate(GeisBackendToken gbtoken,
                GeisSubscription subscription GEIS_UNUSED)
{
  GeisStatus status = GEIS_STATUS_SUCCESS;
  TestBackendToken token = _tbtoken_from_token(gbtoken);
  _create_gesture_events_for_token(token);
  return status;
}


GeisStatus             
_token_deactivate(GeisBackendToken gbtoken GEIS_UNUSED,
                  GeisSubscription subscription GEIS_UNUSED)
{
  GeisStatus status = GEIS_STATUS_SUCCESS;
  return status;
}

void
_token_free_subscription_pdata(GeisBackendToken gbtoken GEIS_UNUSED,
                               GeisSubscription subscription GEIS_UNUSED)
{
}

__attribute__((constructor))
static void _register_test_fixture()
{
  geis_register_backend(GEIS_INIT_MOCK_BACKEND,
                        sizeof(struct GeisBackendTestFixture),
                        &tf_vtbl);
}


/* A dummy routine to force linkage of this module without dlopening it */
void
geis_include_backend_test_fixture()
{
}