summaryrefslogtreecommitdiff
path: root/src/grail-init.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/grail-init.c')
-rw-r--r--src/grail-init.c241
1 files changed, 241 insertions, 0 deletions
diff --git a/src/grail-init.c b/src/grail-init.c
new file mode 100644
index 0000000..18e6996
--- /dev/null
+++ b/src/grail-init.c
@@ -0,0 +1,241 @@
1/*****************************************************************************
2 *
3 * grail - Gesture Recognition And Instantiation Library
4 *
5 * Copyright (C) 2010 Canonical Ltd.
6 * Copyright (C) 2010 Henrik Rydberg <rydberg@bitmath.org>
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 ****************************************************************************/
22
23#include "grail-impl.h"
24#include <stdlib.h>
25#include <string.h>
26#include <math.h>
27
28static struct grail_control *create_control(int size)
29{
30 struct grail_control *c = calloc(1, size);
31
32 if (!c)
33 return 0;
34
35 c->glue_ms = 60;
36 c->bar_center_x = 0.03;
37 c->bar_center_y = 0.03;
38 c->bar_drag_x = 0.03;
39 c->bar_drag_y = 0.03;
40 c->bar_scale = 0.3;
41 c->bar_angle = 0.1;
42 c->drop_x_ms = 300;
43 c->drop_y_ms = 300;
44 c->drop_scale_ms = 300;
45 c->drop_angle_ms = 300;
46
47 return c;
48}
49
50static void destroy_slots(struct grail_element **slots, int nslot)
51{
52 int i;
53
54 if (slots) {
55 for (i = nslot - 1; i >= 0; i--)
56 free(slots[i]);
57 free(slots);
58 }
59}
60
61static void destroy_frame(struct grail_frame *frame, int nslot)
62{
63 if (frame) {
64 destroy_slots(frame->slots, nslot);
65 free(frame->ongoing);
66 free(frame);
67 }
68}
69
70static void destroy_frames(struct grail_frame **frames, int nframe, int nslot)
71{
72 int i;
73
74 if (frames) {
75 for (i = nframe - 1; i >= 0; i--)
76 destroy_frame(frames[i], nslot);
77 free(frames);
78 }
79}
80
81static struct grail_element **create_slots(int nslot, int ntouch, int size)
82{
83 struct grail_element **slots;
84 struct grail_element *s;
85 int i;
86
87 slots = calloc(nslot, sizeof(slots[0]));
88 if (!slots)
89 return 0;
90
91 for (i = 0; i < nslot; i++) {
92 s = calloc(1, size + ntouch * sizeof(void *));
93 if (!s)
94 goto out;
95 s->slot = i;
96 s->id = -1;
97 s->touches = (void *)((char *)s + size);
98 slots[i] = s;
99 }
100
101 return slots;
102 out:
103 destroy_slots(slots, nslot);
104 return 0;
105}
106
107static struct grail_frame *create_frame(int nslot, int ntouch,
108 int frame_size, int slot_size)
109{
110 struct grail_frame *frame;
111 int i;
112
113 frame = calloc(1, frame_size);
114 if (!frame)
115 return 0;
116
117 frame->ongoing = calloc(nslot, sizeof(frame->ongoing[0]));
118 frame->slots = create_slots(nslot, ntouch, slot_size);
119 if (!frame->ongoing || !frame->slots)
120 goto out;
121
122 return frame;
123 out:
124 destroy_frame(frame, nslot);
125 return 0;
126}
127
128static struct grail_frame **create_frames(int nframe, int nslot, int ntouch,
129 int frame_size, int slot_size)
130{
131 struct grail_frame **frames;
132 struct grail_frame *f;
133 int i;
134
135 frames = calloc(nframe, sizeof(frames[0]));
136 if (!frames)
137 return 0;
138
139 for (i = 0; i < nframe; i++) {
140 f = create_frame(nslot, ntouch, frame_size, slot_size);
141 if (!f)
142 goto out;
143 frames[i] = f;
144 }
145
146 return frames;
147 out:
148 destroy_frames(frames, nframe, nslot);
149 return 0;
150}
151
152int create_grail2(struct grail_impl *x,
153 utouch_frame_handle fh,
154 unsigned int num_frames,
155 grail_select_callback select,
156 unsigned int version,
157 unsigned int control_size,
158 unsigned int frame_size,
159 unsigned int slot_size)
160{
161 struct utouch_surface *s = utouch_frame_get_surface(fh);
162 unsigned int ntouch = utouch_frame_get_num_slots(fh);
163 unsigned int nslot = get_slot_count(ntouch);
164 int i, j;
165
166 x->select = select;
167 x->control_size = MAX(control_size, sizeof(struct grail_control));
168 x->frame_size = MAX(frame_size, sizeof(struct grail_frame));
169 x->slot_size = MAX(slot_size, sizeof(struct grail_element));
170
171 x->num_frames = num_frames;
172 x->num_slots = nslot;
173 x->num_touches = ntouch;
174
175 x->ctl = create_control(x->control_size);
176 if (!x->ctl)
177 goto freemem;
178
179 x->frames = create_frames(num_frames, nslot, ntouch,
180 x->frame_size, x->slot_size);
181 if (!x->frames)
182 goto freemem;
183
184 for (i = 0; i < num_frames; i++)
185 x->frames[(i + 1) % num_frames]->prev = x->frames[i];
186
187 return 0;
188
189 freemem:
190 destroy_grail2(x);
191 return -ENOMEM;
192}
193
194void destroy_grail2(struct grail_impl *x)
195{
196 destroy_frames(x->frames, x->num_frames, x->num_slots);
197 free(x->ctl);
198}
199
200grail_handle GRAIL_PUBLIC grail_new_raw(utouch_frame_handle fh,
201 unsigned int num_frames,
202 grail_select_callback select,
203 unsigned int version,
204 unsigned int control_size,
205 unsigned int frame_size,
206 unsigned int slot_size)
207{
208 struct grail *ge;
209 struct grail_impl *x;
210
211 ge = calloc(1, sizeof(*ge));
212 if (!ge)
213 return 0;
214 x = calloc(1, sizeof(*x));
215 if (!x)
216 goto out;
217 x->fh = fh;
218 if (create_grail2(x, fh, num_frames, select,
219 version, control_size, frame_size, slot_size))
220 goto out;
221
222 ge->impl = x;
223 return ge;
224
225 out:
226 free(x);
227 free(ge);
228 return 0;
229}
230
231void GRAIL_PUBLIC grail_delete(grail_handle ge)
232{
233 destroy_grail2(ge->impl);
234 free(ge->impl);
235 free(ge);
236}
237
238struct grail_control GRAIL_PUBLIC *grail_get_control(grail_handle ge)
239{
240 return ge->impl->ctl;
241}