File: clutter-event-tslib.c

package info (click to toggle)
clutter-1.0 1.26.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 34,352 kB
  • sloc: ansic: 128,533; sh: 5,580; xml: 1,641; makefile: 1,613; ruby: 149; perl: 142; sed: 16
file content (277 lines) | stat: -rw-r--r-- 7,604 bytes parent folder | download | duplicates (6)
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
/* Clutter.
 * An OpenGL based 'interactive canvas' library.
 * Authored By Matthew Allum  <mallum@openedhand.com>
 * Copyright (C) 2006-2007 OpenedHand
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * 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 Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 *
 */

#include "config.h"

#include "clutter-backend-egl.h"
#include "clutter-egl.h"

#include "clutter-backend.h"
#include "clutter-event-private.h"
#include "clutter-private.h"
#include "clutter-debug.h"
#include "clutter-main.h"

#include <string.h>

#include <glib.h>

#include <tslib.h>

typedef struct _ClutterEventSource  ClutterEventSource;

struct _ClutterEventSource
{
  GSource source;

  ClutterBackendEGL *backend;
  GPollFD event_poll_fd;

  struct tsdev *ts_device;
};

static gboolean clutter_event_prepare  (GSource     *source,
                                        gint        *timeout);
static gboolean clutter_event_check    (GSource     *source);
static gboolean clutter_event_dispatch (GSource     *source,
                                        GSourceFunc  callback,
                                        gpointer     user_data);

static GList *event_sources = NULL;

static GSourceFuncs event_funcs = {
  clutter_event_prepare,
  clutter_event_check,
  clutter_event_dispatch,
  NULL
};

static GSource *
clutter_event_source_new (ClutterBackendEGL *backend)
{
  GSource *source = g_source_new (&event_funcs, sizeof (ClutterEventSource));
  ClutterEventSource *event_source = (ClutterEventSource *) source;

  event_source->backend = backend;

  return source;
}

static guint32
get_backend_time (void)
{
  ClutterBackendEGL *backend_egl;

  backend_egl = CLUTTER_BACKEND_EGL (clutter_get_default_backend ());

  return g_timer_elapsed (backend_egl->event_timer, NULL) * 1000;
}

void
_clutter_events_tslib_init (ClutterBackend *backend)
{
  ClutterBackendEglNative *backend_egl;
  ClutterEventSource *event_source;
  const char *device_name;
  GSource *source;

  backend_egl = CLUTTER_BACKEND_EGL (backend);

  CLUTTER_NOTE (EVENT, "Starting timer");
  g_assert (backend_egl->event_timer != NULL);
  g_timer_start (backend_egl->event_timer);

  source = backend_egl->event_source = clutter_event_source_new (backend_egl);
  event_source = (ClutterEventSource *) source;

  device_name = g_getenv ("TSLIB_TSDEVICE");
  if (device_name == NULL || device_name[0] == '\0')
    {
      g_warning ("No device for TSLib has been defined; please set the "
                 "TSLIB_TSDEVICE environment variable to define a touch "
                 "screen device to be used with Clutter.");
      g_source_unref (source);
      return;
    }

  event_source->ts_device = ts_open (device_name, 0);
  if (event_source->ts_device)
    {
      CLUTTER_NOTE (EVENT, "Opened '%s'", device_name);

      if (ts_config (event_source->ts_device))
	{
	  g_warning ("Closing device '%s': ts_config() failed", device_name);
	  ts_close (event_source->ts_device);
          g_source_unref (source);
	  return;
	}

      g_source_set_priority (source, CLUTTER_PRIORITY_EVENTS);
      event_source->event_poll_fd.fd = ts_fd (event_source->ts_device);
      event_source->event_poll_fd.events = G_IO_IN;

      event_sources = g_list_prepend (event_sources, event_source);

      g_source_add_poll (source, &event_source->event_poll_fd);
      g_source_set_can_recurse (source, TRUE);
      g_source_attach (source, NULL);
    }
  else
    {
      g_warning ("Unable to open '%s'", device_name);
      g_source_unref (source);
    }
}

void
_clutter_events_egl_uninit (ClutterBackendEglNative *backend_egl)
{
  if (backend_egl->event_timer != NULL)
    {
      CLUTTER_NOTE (EVENT, "Stopping the timer");
      g_timer_stop (backend_egl->event_timer);
    }

  if (backend_egl->event_source != NULL)
    {
      CLUTTER_NOTE (EVENT, "Destroying the event source");

      ClutterEventSource *event_source =
                (ClutterEventSource *) backend_egl->event_source;

      ts_close (event_source->ts_device);
      event_sources = g_list_remove (event_sources, backend_egl->event_source);

      g_source_destroy (backend_egl->event_source);
      g_source_unref (backend_egl->event_source);
      backend_egl->event_source = NULL;
    }
}

static gboolean
clutter_event_prepare (GSource *source,
                       gint    *timeout)
{
  gboolean retval;

  _clutter_threads_acquire_lock ();

  *timeout = -1;
  retval = clutter_events_pending ();

  _clutter_threads_release_lock ();

  return retval;
}

static gboolean
clutter_event_check (GSource *source)
{
  ClutterEventSource *event_source = (ClutterEventSource *) source;
  gboolean retval;

  _clutter_threads_acquire_lock ();

  retval = ((event_source->event_poll_fd.revents & G_IO_IN) ||
            clutter_events_pending ());

  _clutter_threads_release_lock ();

  return retval;
}

static gboolean
clutter_event_dispatch (GSource     *source,
                        GSourceFunc  callback,
                        gpointer     user_data)
{
  ClutterEventSource *event_source = (ClutterEventSource *) source;
  struct ts_sample tsevent;
  ClutterEvent *event;

  _clutter_threads_acquire_lock ();

  /* FIXME while would be better here but need to deal with lockups */
  if ((!clutter_events_pending()) &&
      (ts_read(event_source->ts_device, &tsevent, 1) == 1))
    {
      static gint last_x = 0, last_y = 0;
      static gboolean clicked = FALSE;

      /* Avoid sending too many events which are just pressure changes.
       *
       * FIXME - We don't current handle pressure in events and thus
       * event_button_generate gets confused generating lots of double
       * and triple clicks.
      */
      if (tsevent.pressure && last_x == tsevent.x && last_y == tsevent.y)
        goto out;

      event = clutter_event_new (CLUTTER_NOTHING);

      event->any.stage = clutter_stage_get_default ();

      last_x = event->button.x = tsevent.x;
      last_y = event->button.y = tsevent.y;

      if (tsevent.pressure && !clicked)
        {
	  event->button.type = event->type = CLUTTER_BUTTON_PRESS;
          event->button.time = get_backend_time ();
          event->button.modifier_state = 0;
          event->button.button = 1;

          clicked = TRUE;
        }
      else if (tsevent.pressure && clicked)
        {
          event->motion.type = event->type = CLUTTER_MOTION;
          event->motion.time = get_backend_time ();
          event->motion.modifier_state = 0;
        }
      else
        {
	  event->button.type = event->type = CLUTTER_BUTTON_RELEASE;
          event->button.time = get_backend_time ();
          event->button.modifier_state = 0;
          event->button.button = 1;

          clicked = FALSE;
        }

      _clutter_event_push (event, FALSE);
    }

  /* Pop an event off the queue if any */
  event = clutter_event_get ();

  if (event)
    {
      /* forward the event into clutter for emission etc. */
      _clutter_stage_queue_event (event->any.stage, event, FALSE);
    }

out:
  _clutter_threads_release_lock ();

  return TRUE;
}