~ubuntu-branches/ubuntu/trusty/geis/trusty

« back to all changes in this revision

Viewing changes to examples/geis2.c

  • Committer: Package Import Robot
  • Author(s): Chase Douglas
  • Date: 2012-07-30 08:51:42 UTC
  • Revision ID: package-import@ubuntu.com-20120730085142-jrc33ygjvt0ob1wl
Tags: upstream-2.2.11
ImportĀ upstreamĀ versionĀ 2.2.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file geis2.c
 
3
 * @brief A simple example using the GEIS v2 API.
 
4
 *
 
5
 * Copyright 2011 Canonical Ltd.
 
6
 *
 
7
 * This program is free software: you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation, either version 3 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
#include "geis_config.h"
 
21
 
 
22
#include <errno.h>
 
23
#include <geis/geis.h>
 
24
#include <stdio.h>
 
25
#include <string.h>
 
26
#include <sys/select.h>
 
27
 
 
28
 
 
29
void print_attr(GeisAttr attr)
 
30
{
 
31
  GeisString attr_name = geis_attr_name(attr);
 
32
  switch (geis_attr_type(attr))
 
33
  {
 
34
    case GEIS_ATTR_TYPE_BOOLEAN:
 
35
      printf("  \"%s\": %s\n", attr_name,
 
36
             geis_attr_value_to_boolean(attr) ? "true" : "false");
 
37
      break;
 
38
    case GEIS_ATTR_TYPE_FLOAT:
 
39
      printf("  \"%s\": %g\n", attr_name, geis_attr_value_to_float(attr));
 
40
      break;
 
41
    case GEIS_ATTR_TYPE_INTEGER:
 
42
      printf("  \"%s\": %d\n", attr_name, geis_attr_value_to_integer(attr));
 
43
      break;
 
44
    case GEIS_ATTR_TYPE_STRING:
 
45
      printf("  \"%s\": %s\n", attr_name, geis_attr_value_to_string(attr));
 
46
      break;
 
47
    default:
 
48
      break;
 
49
  }
 
50
}
 
51
 
 
52
 
 
53
void
 
54
dump_device_event(GeisEvent event)
 
55
{
 
56
  GeisDevice device;
 
57
  GeisAttr attr;
 
58
  GeisSize i;
 
59
 
 
60
  attr = geis_event_attr_by_name(event, GEIS_EVENT_ATTRIBUTE_DEVICE);
 
61
  device = geis_attr_value_to_pointer(attr);
 
62
  printf("device %02d \"%s\"\n",
 
63
         geis_device_id(device), geis_device_name(device));
 
64
  for (i = 0; i < geis_device_attr_count(device); ++i)
 
65
  {
 
66
    print_attr(geis_device_attr(device, i));
 
67
  }
 
68
}
 
69
 
 
70
 
 
71
void
 
72
dump_gesture_event(GeisEvent event)
 
73
{
 
74
  GeisSize i;
 
75
  GeisTouchSet touchset;
 
76
  GeisGroupSet groupset;
 
77
  GeisAttr     attr;
 
78
 
 
79
  attr = geis_event_attr_by_name(event, GEIS_EVENT_ATTRIBUTE_TOUCHSET);
 
80
  touchset = geis_attr_value_to_pointer(attr);
 
81
 
 
82
  attr = geis_event_attr_by_name(event, GEIS_EVENT_ATTRIBUTE_GROUPSET);
 
83
  groupset = geis_attr_value_to_pointer(attr);
 
84
 
 
85
  printf("gesture\n");
 
86
  for (i= 0; i < geis_groupset_group_count(groupset); ++i)
 
87
  {
 
88
    GeisSize j;
 
89
    GeisGroup group = geis_groupset_group(groupset, i);
 
90
    printf("+group %u\n", geis_group_id(group));
 
91
 
 
92
    for (j=0; j < geis_group_frame_count(group); ++j)
 
93
    {
 
94
      GeisSize k;
 
95
      GeisFrame frame = geis_group_frame(group, j);
 
96
      GeisSize attr_count = geis_frame_attr_count(frame);
 
97
 
 
98
      printf("+frame %u\n", geis_frame_id(frame));
 
99
      for (k = 0; k < attr_count; ++k)
 
100
      {
 
101
        print_attr(geis_frame_attr(frame, k));
 
102
      }
 
103
 
 
104
      for (k = 0; k < geis_frame_touchid_count(frame); ++k)
 
105
      {
 
106
        GeisSize  touchid = geis_frame_touchid(frame, k);
 
107
        GeisTouch touch = geis_touchset_touch_by_id(touchset, touchid);
 
108
        GeisSize  n;
 
109
        printf("+touch %u\n", (unsigned)k);
 
110
        for (n = 0; n < geis_touch_attr_count(touch); ++n)
 
111
        {
 
112
          print_attr(geis_touch_attr(touch, n));
 
113
        }
 
114
      }
 
115
    }
 
116
  }
 
117
}
 
118
 
 
119
 
 
120
void
 
121
target_subscription(Geis geis, GeisSubscription subscription)
 
122
{
 
123
  GeisStatus status;
 
124
  GeisFilter filter = geis_filter_new(geis, "filter");
 
125
  geis_filter_add_term(filter,
 
126
                       GEIS_FILTER_CLASS,
 
127
                       GEIS_GESTURE_ATTRIBUTE_TOUCHES, GEIS_FILTER_OP_EQ, 2,
 
128
                       NULL);
 
129
 
 
130
  status = geis_subscription_add_filter(subscription, filter);
 
131
  if (status != GEIS_STATUS_SUCCESS)
 
132
  {
 
133
    fprintf(stderr, "error adding filter\n");
 
134
  }
 
135
}
 
136
 
 
137
 
 
138
int
 
139
main(int argc GEIS_UNUSED, char* argv[] GEIS_UNUSED)
 
140
{
 
141
  GeisStatus status;
 
142
  Geis geis;
 
143
  GeisSubscription subscription;
 
144
  int        fd;
 
145
 
 
146
  geis = geis_new(GEIS_INIT_TRACK_DEVICES,
 
147
                  GEIS_INIT_TRACK_GESTURE_CLASSES,
 
148
                  NULL);
 
149
  geis_get_configuration(geis, GEIS_CONFIGURATION_FD, &fd);
 
150
 
 
151
  subscription = geis_subscription_new(geis, "example", GEIS_SUBSCRIPTION_CONT);
 
152
 
 
153
  while(1)
 
154
  {
 
155
    fd_set read_fds;
 
156
    FD_ZERO(&read_fds);
 
157
    FD_SET(0, &read_fds);
 
158
    FD_SET(fd, &read_fds);
 
159
    int sstat = select(fd+1, &read_fds, NULL, NULL, NULL);
 
160
    if (sstat < 0)
 
161
    {
 
162
      fprintf(stderr, "error %d in select(): %s\n", errno, strerror(errno));
 
163
      break;
 
164
    }
 
165
 
 
166
    if (FD_ISSET(fd, &read_fds))
 
167
    {
 
168
      GeisEvent event;
 
169
      status = geis_dispatch_events(geis);
 
170
      status = geis_next_event(geis, &event);
 
171
      while (status == GEIS_STATUS_CONTINUE || status == GEIS_STATUS_SUCCESS)
 
172
      {
 
173
        switch (geis_event_type(event))
 
174
        {
 
175
          case GEIS_EVENT_INIT_COMPLETE:
 
176
            target_subscription(geis, subscription);
 
177
            status = geis_subscription_activate(subscription);
 
178
            break;
 
179
 
 
180
          case GEIS_EVENT_DEVICE_AVAILABLE:
 
181
          case GEIS_EVENT_DEVICE_UNAVAILABLE:
 
182
            dump_device_event(event);
 
183
            break;
 
184
 
 
185
          case GEIS_EVENT_GESTURE_BEGIN:
 
186
          case GEIS_EVENT_GESTURE_UPDATE:
 
187
          case GEIS_EVENT_GESTURE_END:
 
188
            dump_gesture_event(event);
 
189
            break;
 
190
 
 
191
          default:
 
192
            break;
 
193
        }
 
194
        geis_event_delete(event);
 
195
        status = geis_next_event(geis, &event);
 
196
      }
 
197
    }
 
198
 
 
199
    if (FD_ISSET(0, &read_fds))
 
200
    {
 
201
      break;
 
202
    }
 
203
  }
 
204
 
 
205
  geis_subscription_delete(subscription);
 
206
  geis_delete(geis);
 
207
}
 
208