~oif-team/frame/1.1.4

« back to all changes in this revision

Viewing changes to src/frame.c

  • Committer: Chase Douglas
  • Date: 2012-08-15 19:05:52 UTC
  • Revision ID: chase.douglas@canonical.com-20120815190552-ul59r06he8a8j7f8
Rename project to simply 'frame'

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*****************************************************************************
2
2
 *
3
 
 * utouch-frame - Touch Frame Library
 
3
 * frame - Touch Frame Library
4
4
 *
5
5
 * Copyright (C) 2010 Canonical Ltd.
6
6
 *
28
28
 
29
29
#define MAX(a, b) ((a) > (b) ? (a) : (b))
30
30
 
31
 
unsigned int utouch_frame_get_version(void)
 
31
unsigned int oif_frame_get_version(void)
32
32
{
33
 
        return UTOUCH_FRAME_VERSION;
 
33
        return OIF_FRAME_VERSION;
34
34
}
35
35
 
36
 
unsigned int utouch_frame_get_num_frames(utouch_frame_handle fh)
 
36
unsigned int oif_frame_get_num_frames(oif_frame_handle fh)
37
37
{
38
38
        return fh->num_frames;
39
39
}
40
40
 
41
 
unsigned int utouch_frame_get_num_slots(utouch_frame_handle fh)
 
41
unsigned int oif_frame_get_num_slots(oif_frame_handle fh)
42
42
{
43
43
        return fh->num_slots;
44
44
}
45
45
 
46
 
static void destroy_slots(struct utouch_contact **slots, int nslot)
 
46
static void destroy_slots(struct oif_contact **slots, int nslot)
47
47
{
48
48
        int i;
49
49
 
54
54
        }
55
55
}
56
56
 
57
 
static void destroy_frame(struct utouch_frame *frame, int nslot)
 
57
static void destroy_frame(struct oif_frame *frame, int nslot)
58
58
{
59
59
        if (frame) {
60
60
                destroy_slots(frame->slots, nslot);
63
63
        }
64
64
}
65
65
 
66
 
static void destroy_frames(struct utouch_frame **frames, int nframe, int nslot)
 
66
static void destroy_frames(struct oif_frame **frames, int nframe, int nslot)
67
67
{
68
68
        int i;
69
69
 
74
74
        }
75
75
}
76
76
 
77
 
static struct utouch_contact **create_slots(int nslot, int size)
 
77
static struct oif_contact **create_slots(int nslot, int size)
78
78
{
79
 
        struct utouch_contact **slots;
80
 
        struct utouch_contact *s;
 
79
        struct oif_contact **slots;
 
80
        struct oif_contact *s;
81
81
        int i;
82
82
 
83
83
        slots = calloc(nslot, sizeof(slots[0]));
99
99
        return 0;
100
100
}
101
101
 
102
 
static struct utouch_frame *create_frame(int nslot,
103
 
                                         int frame_size, int slot_size)
 
102
static struct oif_frame *create_frame(int nslot, int frame_size, int slot_size)
104
103
{
105
 
        struct utouch_frame *frame;
 
104
        struct oif_frame *frame;
106
105
 
107
106
        frame = calloc(1, frame_size);
108
107
        if (!frame)
119
118
        return 0;
120
119
}
121
120
 
122
 
static struct utouch_frame **create_frames(int nframe, int nslot,
123
 
                                           int frame_size, int slot_size)
 
121
static struct oif_frame **create_frames(int nframe, int nslot, int frame_size,
 
122
                                        int slot_size)
124
123
{
125
 
        struct utouch_frame **frames;
126
 
        struct utouch_frame *f;
 
124
        struct oif_frame **frames;
 
125
        struct oif_frame *f;
127
126
        int i;
128
127
 
129
128
        frames = calloc(nframe, sizeof(frames[0]));
143
142
        return 0;
144
143
}
145
144
 
146
 
utouch_frame_handle utouch_frame_new_engine_raw(unsigned int nframe,
147
 
                                                unsigned int nslot,
148
 
                                                unsigned int rate,
149
 
                                                unsigned int version,
150
 
                                                unsigned int surface_size,
151
 
                                                unsigned int frame_size,
152
 
                                                unsigned int slot_size)
 
145
oif_frame_handle oif_frame_new_engine_raw(unsigned int nframe,
 
146
                                          unsigned int nslot,
 
147
                                          unsigned int rate,
 
148
                                          unsigned int version,
 
149
                                          unsigned int surface_size,
 
150
                                          unsigned int frame_size,
 
151
                                          unsigned int slot_size)
153
152
{
154
 
        struct utouch_frame *f, *pf;
155
 
        utouch_frame_handle fh;
 
153
        struct oif_frame *f, *pf;
 
154
        oif_frame_handle fh;
156
155
        int i, j;
157
156
 
158
 
        fh = calloc(1, sizeof(struct utouch_frame_engine));
 
157
        fh = calloc(1, sizeof(struct oif_frame_engine));
159
158
        if (!fh)
160
159
                return 0;
161
160
 
163
162
        fh->num_slots = nslot;
164
163
        fh->hold_ms = 1000 / rate;
165
164
 
166
 
        surface_size = MAX(surface_size, sizeof(struct utouch_surface));
167
 
        frame_size = MAX(frame_size, sizeof(struct utouch_frame));
168
 
        slot_size = MAX(slot_size, sizeof(struct utouch_contact));
 
165
        surface_size = MAX(surface_size, sizeof(struct oif_surface));
 
166
        frame_size = MAX(frame_size, sizeof(struct oif_frame));
 
167
        slot_size = MAX(slot_size, sizeof(struct oif_contact));
169
168
 
170
169
        fh->surface = calloc(1, surface_size);
171
170
        fh->frames = create_frames(nframe, nslot, frame_size, slot_size);
184
183
 
185
184
        return fh;
186
185
out:
187
 
        utouch_frame_delete_engine(fh);
 
186
        oif_frame_delete_engine(fh);
188
187
        return 0;
189
188
}
190
189
 
191
 
void utouch_frame_delete_engine(utouch_frame_handle fh)
 
190
void oif_frame_delete_engine(oif_frame_handle fh)
192
191
{
193
192
        free(fh->evmap);
194
193
        destroy_frame(fh->next, fh->num_slots);
197
196
        free(fh);
198
197
}
199
198
 
200
 
struct utouch_surface *utouch_frame_get_surface(utouch_frame_handle fh)
 
199
struct oif_surface *oif_frame_get_surface(oif_frame_handle fh)
201
200
{
202
201
        return fh->surface;
203
202
}
204
203
 
205
 
struct utouch_contact *utouch_frame_get_current_slot(utouch_frame_handle fh)
 
204
struct oif_contact *oif_frame_get_current_slot(oif_frame_handle fh)
206
205
{
207
206
        return fh->next->slots[fh->slot];
208
207
}
209
208
 
210
 
int utouch_frame_set_current_slot(utouch_frame_handle fh, int slot)
 
209
int oif_frame_set_current_slot(oif_frame_handle fh, int slot)
211
210
{
212
211
        if (slot < 0 || slot >= fh->num_slots)
213
212
                return -EINVAL;
215
214
        return 0;
216
215
}
217
216
 
218
 
int utouch_frame_set_current_id(utouch_frame_handle fh, int id)
 
217
int oif_frame_set_current_id(oif_frame_handle fh, int id)
219
218
{
220
 
        struct utouch_contact *t;
 
219
        struct oif_contact *t;
221
220
        int i;
222
221
 
223
222
        for (i = 0; i < fh->num_slots; i++) {
224
223
                t = fh->next->slots[i];
225
224
                if (t->active && t->id == id)
226
 
                        return utouch_frame_set_current_slot(fh, i);
 
225
                        return oif_frame_set_current_slot(fh, i);
227
226
        }
228
227
        for (i = 0; i < fh->num_slots; i++) {
229
228
                t = fh->next->slots[i];
230
229
                if (!t->active) {
231
230
                        t->active = 1;
232
231
                        t->id = id;
233
 
                        return utouch_frame_set_current_slot(fh, i);
 
232
                        return oif_frame_set_current_slot(fh, i);
234
233
                }
235
234
        }
236
235
        return -ENOMEM;
237
236
}
238
237
 
239
 
static void transform_slot(struct utouch_contact *slot,
240
 
                           const struct utouch_surface *s)
 
238
static void transform_slot(struct oif_contact *slot,
 
239
                           const struct oif_surface *s)
241
240
{
242
241
        float fx = (s->mapped_max_x - s->mapped_min_x) / (s->max_x - s->min_x);
243
242
        float fy = (s->mapped_max_y - s->mapped_min_y) / (s->max_y - s->min_y);
255
254
        slot->distance *= f;
256
255
}
257
256
 
258
 
static void set_contact(utouch_frame_handle fh,
259
 
                        struct utouch_contact *a,
260
 
                        struct utouch_contact *b,
261
 
                        utouch_frame_time_t dt)
 
257
static void set_contact(oif_frame_handle fh, struct oif_contact *a,
 
258
                        struct oif_contact *b, oif_frame_time_t dt)
262
259
{
263
260
        static const float D = 0.333;
264
 
        struct utouch_surface *s = fh->surface;
265
 
        const struct utouch_contact *ap = a->prev;
 
261
        struct oif_surface *s = fh->surface;
 
262
        const struct oif_contact *ap = a->prev;
266
263
 
267
264
        a->active = b->active;
268
265
        a->id = b->id;
297
294
        b->vy = a->vy;
298
295
}
299
296
 
300
 
static int detect_addrem(const struct utouch_contact *a,
301
 
                         const struct utouch_contact *b)
 
297
static int detect_addrem(const struct oif_contact *a,
 
298
                         const struct oif_contact *b)
302
299
{
303
300
        return a->id != b->id || a->tool_type != b->tool_type ||
304
301
               a->active != b->active;
305
302
}
306
303
 
307
 
static int detect_mod(const struct utouch_contact *a,
308
 
                      const struct utouch_contact *b)
 
304
static int detect_mod(const struct oif_contact *a, const struct oif_contact *b)
309
305
{
310
306
        return a->x != b->x || a->y != b->y ||
311
307
                a->touch_major != b->touch_major ||
317
313
                a->distance != b->distance;
318
314
}
319
315
 
320
 
static utouch_frame_time_t get_time_ms()
 
316
static oif_frame_time_t get_time_ms()
321
317
{
322
 
        static const utouch_frame_time_t ms = 1000;
 
318
        static const oif_frame_time_t ms = 1000;
323
319
        struct timeval tv;
324
320
        gettimeofday(&tv, 0);
325
321
        return tv.tv_usec / ms + tv.tv_sec * ms;
326
322
}
327
323
 
328
 
static void set_semi_mt_touches(utouch_frame_handle fh)
 
324
static void set_semi_mt_touches(oif_frame_handle fh)
329
325
{
330
 
        struct utouch_frame *next = fh->next;
331
 
        struct utouch_contact *a = next->slots[0];
332
 
        struct utouch_contact *b = next->slots[1];
333
 
        struct utouch_surface *s = fh->surface;
 
326
        struct oif_frame *next = fh->next;
 
327
        struct oif_contact *a = next->slots[0];
 
328
        struct oif_contact *b = next->slots[1];
 
329
        struct oif_surface *s = fh->surface;
334
330
        /* use touch ids half way around the ID range */
335
331
        unsigned int id = a->id + (s->max_id - s->min_id) / 2;
336
332
        float x = (a->x + b->x) / 2;
338
334
        int i;
339
335
 
340
336
        for (i = 2; i < fh->semi_mt_num_active; i++) {
341
 
                struct utouch_contact *c = next->slots[i];
 
337
                struct oif_contact *c = next->slots[i];
342
338
 
343
 
                memset(c, 0, sizeof(struct utouch_contact));
 
339
                memset(c, 0, sizeof(struct oif_contact));
344
340
                c->active = 1;
345
341
                if (id > s->max_id)
346
342
                        id -= (s->max_id - s->min_id + 1);
353
349
                next->slots[i]->active = 0;
354
350
}
355
351
 
356
 
const struct utouch_frame *utouch_frame_sync(utouch_frame_handle fh,
357
 
                                             utouch_frame_time_t time)
 
352
const struct oif_frame *oif_frame_sync(oif_frame_handle fh,
 
353
                                       oif_frame_time_t time)
358
354
{
359
 
        struct utouch_frame *frame = fh->frames[fh->frame];
360
 
        const struct utouch_frame *prev = frame->prev;
361
 
        struct utouch_frame *next = fh->next;
 
355
        struct oif_frame *frame = fh->frames[fh->frame];
 
356
        const struct oif_frame *prev = frame->prev;
 
357
        struct oif_frame *next = fh->next;
362
358
        int naddrem = 0, nmod = 0;
363
 
        utouch_frame_time_t dt;
 
359
        oif_frame_time_t dt;
364
360
        int i;
365
361
 
366
362
        frame->time = time ? time : get_time_ms();
371
367
                set_semi_mt_touches(fh);
372
368
 
373
369
        for (i = 0; i < fh->num_slots; i++) {
374
 
                struct utouch_contact *p = frame->slots[i];
375
 
                struct utouch_contact *q = next->slots[i];
 
370
                struct oif_contact *p = frame->slots[i];
 
371
                struct oif_contact *q = next->slots[i];
376
372
 
377
373
                set_contact(fh, p, q, dt);
378
374
                if (p->active)