~oif-team/geis/2.1.2

« back to all changes in this revision

Viewing changes to examples/geis2.c

  • Committer: Stephen M. Webb
  • Date: 2011-03-17 04:06:04 UTC
  • mto: This revision was merged to the branch mainline in revision 117.
  • Revision ID: stephen.webb@canonical.com-20110317040604-ro1xq4wm6elg903c
Refined the API documentation and added an examples directory.

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
 
 
21
#include <errno.h>
 
22
#include <geis/geis.h>
 
23
#include <stdio.h>
 
24
#include <string.h>
 
25
#include <sys/select.h>
 
26
 
 
27
 
 
28
void print_attr(GeisAttr attr)
 
29
{
 
30
  GeisString attr_name = geis_attr_name(attr);
 
31
  switch (geis_attr_type(attr))
 
32
  {
 
33
    case GEIS_ATTR_TYPE_BOOLEAN:
 
34
      printf("  \"%s\": %s\n", attr_name,
 
35
             geis_attr_value_to_boolean(attr) ? "true" : "false");
 
36
      break;
 
37
    case GEIS_ATTR_TYPE_FLOAT:
 
38
      printf("  \"%s\": %g\n", attr_name, geis_attr_value_to_float(attr));
 
39
      break;
 
40
    case GEIS_ATTR_TYPE_INTEGER:
 
41
      printf("  \"%s\": %d\n", attr_name, geis_attr_value_to_integer(attr));
 
42
      break;
 
43
    case GEIS_ATTR_TYPE_STRING:
 
44
      printf("  \"%s\": %s\n", attr_name, geis_attr_value_to_string(attr));
 
45
      break;
 
46
    default:
 
47
      break;
 
48
  }
 
49
}
 
50
 
 
51
 
 
52
void
 
53
dump_device_event(GeisEvent event)
 
54
{
 
55
  GeisDevice device;
 
56
  GeisAttr attr;
 
57
  GeisSize i;
 
58
  GeisInputDeviceId device_id = 0;
 
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
 
 
91
    for (j=0; j < geis_group_frame_count(group); ++j)
 
92
    {
 
93
      GeisSize k;
 
94
      GeisFrame frame = geis_group_frame(group, j);
 
95
      GeisSize attr_count = geis_frame_attr_count(frame);
 
96
 
 
97
      for (k = 0; k < attr_count; ++k)
 
98
      {
 
99
        print_attr(geis_frame_attr(frame, k));
 
100
      }
 
101
 
 
102
      for (k = 0; k < geis_frame_touchid_count(frame); ++k)
 
103
      {
 
104
        GeisSize  touchid = geis_frame_touchid(frame, k);
 
105
        GeisTouch touch = geis_touchset_touch_by_id(touchset, touchid);
 
106
        GeisSize  n;
 
107
        printf("+touch %lu\n", k);
 
108
        for (n = 0; n < geis_touch_attr_count(touch); ++n)
 
109
        {
 
110
          print_attr(geis_touch_attr(touch, n));
 
111
        }
 
112
      }
 
113
    }
 
114
  }
 
115
}
 
116
 
 
117
 
 
118
int
 
119
main(int argc, char* argv[])
 
120
{
 
121
  GeisStatus status;
 
122
  Geis geis;
 
123
  GeisSubscription subscription;
 
124
  GeisFilter filter;
 
125
  int        fd;
 
126
 
 
127
  geis = geis_new(GEIS_INIT_UTOUCH_XCB,
 
128
                  GEIS_INIT_TRACK_DEVICES,
 
129
                  NULL);
 
130
  subscription = geis_subscription_new(geis, "example", GEIS_SUBSCRIPTION_CONT);
 
131
  filter = geis_filter_new(geis, "filter");
 
132
 
 
133
  geis_filter_add_term(filter,
 
134
                       GEIS_FILTER_REGION,
 
135
                       GEIS_GESTURE_ATTRIBUTE_TOUCHES, GEIS_FILTER_OP_EQ, 2,
 
136
                       NULL);
 
137
 
 
138
  status = geis_subscription_add_filter(subscription, filter);
 
139
  status = geis_subscription_activate(subscription);
 
140
 
 
141
  geis_get_configuration(geis, GEIS_CONFIGURATION_FD, &fd);
 
142
 
 
143
  while(1)
 
144
  {
 
145
    fd_set read_fds;
 
146
    FD_ZERO(&read_fds);
 
147
    FD_SET(0, &read_fds);
 
148
    FD_SET(fd, &read_fds);
 
149
    int sstat = select(fd+1, &read_fds, NULL, NULL, NULL);
 
150
    if (sstat < 0)
 
151
    {
 
152
      fprintf(stderr, "error %d in select(): %s\n", errno, strerror(errno));
 
153
      break;
 
154
    }
 
155
 
 
156
    if (FD_ISSET(fd, &read_fds))
 
157
    {
 
158
      GeisEvent event;
 
159
      status = geis_dispatch_events(geis);
 
160
      status = geis_next_event(geis, &event);
 
161
      while (status == GEIS_STATUS_CONTINUE || status == GEIS_STATUS_SUCCESS)
 
162
      {
 
163
        switch (geis_event_type(event))
 
164
        {
 
165
          case GEIS_EVENT_DEVICE_AVAILABLE:
 
166
          case GEIS_EVENT_DEVICE_UNAVAILABLE:
 
167
            dump_device_event(event);
 
168
            break;
 
169
 
 
170
          case GEIS_EVENT_GESTURE_BEGIN:
 
171
          case GEIS_EVENT_GESTURE_UPDATE:
 
172
          case GEIS_EVENT_GESTURE_END:
 
173
            dump_gesture_event(event);
 
174
            break;
 
175
        }
 
176
        geis_event_delete(event);
 
177
        status = geis_next_event(geis, &event);
 
178
      }
 
179
    }
 
180
 
 
181
    if (FD_ISSET(0, &read_fds))
 
182
    {
 
183
      break;
 
184
    }
 
185
  }
 
186
 
 
187
  geis_subscription_delete(subscription);
 
188
  geis_delete(geis);
 
189
}
 
190