1 /* Clutter.
2  * An OpenGL based 'interactive canvas' library.
3  * Authored By Matthew Allum  <mallum@openedhand.com>
4  * Copyright (C) 2006-2007 OpenedHand
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  *
20  */
21 
22 #include "config.h"
23 
24 #include "clutter-backend-egl.h"
25 #include "clutter-egl.h"
26 
27 #include "clutter-backend.h"
28 #include "clutter-event-private.h"
29 #include "clutter-private.h"
30 #include "clutter-debug.h"
31 #include "clutter-main.h"
32 
33 #include <string.h>
34 
35 #include <glib.h>
36 
37 #include <tslib.h>
38 
39 typedef struct _ClutterEventSource  ClutterEventSource;
40 
41 struct _ClutterEventSource
42 {
43   GSource source;
44 
45   ClutterBackendEGL *backend;
46   GPollFD event_poll_fd;
47 
48   struct tsdev *ts_device;
49 };
50 
51 static gboolean clutter_event_prepare  (GSource     *source,
52                                         gint        *timeout);
53 static gboolean clutter_event_check    (GSource     *source);
54 static gboolean clutter_event_dispatch (GSource     *source,
55                                         GSourceFunc  callback,
56                                         gpointer     user_data);
57 
58 static GList *event_sources = NULL;
59 
60 static GSourceFuncs event_funcs = {
61   clutter_event_prepare,
62   clutter_event_check,
63   clutter_event_dispatch,
64   NULL
65 };
66 
67 static GSource *
clutter_event_source_new(ClutterBackendEGL * backend)68 clutter_event_source_new (ClutterBackendEGL *backend)
69 {
70   GSource *source = g_source_new (&event_funcs, sizeof (ClutterEventSource));
71   ClutterEventSource *event_source = (ClutterEventSource *) source;
72 
73   event_source->backend = backend;
74 
75   return source;
76 }
77 
78 static guint32
get_backend_time(void)79 get_backend_time (void)
80 {
81   ClutterBackendEGL *backend_egl;
82 
83   backend_egl = CLUTTER_BACKEND_EGL (clutter_get_default_backend ());
84 
85   return g_timer_elapsed (backend_egl->event_timer, NULL) * 1000;
86 }
87 
88 void
_clutter_events_tslib_init(ClutterBackend * backend)89 _clutter_events_tslib_init (ClutterBackend *backend)
90 {
91   ClutterBackendEglNative *backend_egl;
92   ClutterEventSource *event_source;
93   const char *device_name;
94   GSource *source;
95 
96   backend_egl = CLUTTER_BACKEND_EGL (backend);
97 
98   CLUTTER_NOTE (EVENT, "Starting timer");
99   g_assert (backend_egl->event_timer != NULL);
100   g_timer_start (backend_egl->event_timer);
101 
102   source = backend_egl->event_source = clutter_event_source_new (backend_egl);
103   event_source = (ClutterEventSource *) source;
104 
105   device_name = g_getenv ("TSLIB_TSDEVICE");
106   if (device_name == NULL || device_name[0] == '\0')
107     {
108       g_warning ("No device for TSLib has been defined; please set the "
109                  "TSLIB_TSDEVICE environment variable to define a touch "
110                  "screen device to be used with Clutter.");
111       g_source_unref (source);
112       return;
113     }
114 
115   event_source->ts_device = ts_open (device_name, 0);
116   if (event_source->ts_device)
117     {
118       CLUTTER_NOTE (EVENT, "Opened '%s'", device_name);
119 
120       if (ts_config (event_source->ts_device))
121 	{
122 	  g_warning ("Closing device '%s': ts_config() failed", device_name);
123 	  ts_close (event_source->ts_device);
124           g_source_unref (source);
125 	  return;
126 	}
127 
128       g_source_set_priority (source, CLUTTER_PRIORITY_EVENTS);
129       event_source->event_poll_fd.fd = ts_fd (event_source->ts_device);
130       event_source->event_poll_fd.events = G_IO_IN;
131 
132       event_sources = g_list_prepend (event_sources, event_source);
133 
134       g_source_add_poll (source, &event_source->event_poll_fd);
135       g_source_set_can_recurse (source, TRUE);
136       g_source_attach (source, NULL);
137     }
138   else
139     {
140       g_warning ("Unable to open '%s'", device_name);
141       g_source_unref (source);
142     }
143 }
144 
145 void
_clutter_events_egl_uninit(ClutterBackendEglNative * backend_egl)146 _clutter_events_egl_uninit (ClutterBackendEglNative *backend_egl)
147 {
148   if (backend_egl->event_timer != NULL)
149     {
150       CLUTTER_NOTE (EVENT, "Stopping the timer");
151       g_timer_stop (backend_egl->event_timer);
152     }
153 
154   if (backend_egl->event_source != NULL)
155     {
156       CLUTTER_NOTE (EVENT, "Destroying the event source");
157 
158       ClutterEventSource *event_source =
159                 (ClutterEventSource *) backend_egl->event_source;
160 
161       ts_close (event_source->ts_device);
162       event_sources = g_list_remove (event_sources, backend_egl->event_source);
163 
164       g_source_destroy (backend_egl->event_source);
165       g_source_unref (backend_egl->event_source);
166       backend_egl->event_source = NULL;
167     }
168 }
169 
170 static gboolean
clutter_event_prepare(GSource * source,gint * timeout)171 clutter_event_prepare (GSource *source,
172                        gint    *timeout)
173 {
174   gboolean retval;
175 
176   _clutter_threads_acquire_lock ();
177 
178   *timeout = -1;
179   retval = clutter_events_pending ();
180 
181   _clutter_threads_release_lock ();
182 
183   return retval;
184 }
185 
186 static gboolean
clutter_event_check(GSource * source)187 clutter_event_check (GSource *source)
188 {
189   ClutterEventSource *event_source = (ClutterEventSource *) source;
190   gboolean retval;
191 
192   _clutter_threads_acquire_lock ();
193 
194   retval = ((event_source->event_poll_fd.revents & G_IO_IN) ||
195             clutter_events_pending ());
196 
197   _clutter_threads_release_lock ();
198 
199   return retval;
200 }
201 
202 static gboolean
clutter_event_dispatch(GSource * source,GSourceFunc callback,gpointer user_data)203 clutter_event_dispatch (GSource     *source,
204                         GSourceFunc  callback,
205                         gpointer     user_data)
206 {
207   ClutterEventSource *event_source = (ClutterEventSource *) source;
208   struct ts_sample tsevent;
209   ClutterEvent *event;
210 
211   _clutter_threads_acquire_lock ();
212 
213   /* FIXME while would be better here but need to deal with lockups */
214   if ((!clutter_events_pending()) &&
215       (ts_read(event_source->ts_device, &tsevent, 1) == 1))
216     {
217       static gint last_x = 0, last_y = 0;
218       static gboolean clicked = FALSE;
219 
220       /* Avoid sending too many events which are just pressure changes.
221        *
222        * FIXME - We don't current handle pressure in events and thus
223        * event_button_generate gets confused generating lots of double
224        * and triple clicks.
225       */
226       if (tsevent.pressure && last_x == tsevent.x && last_y == tsevent.y)
227         goto out;
228 
229       event = clutter_event_new (CLUTTER_NOTHING);
230 
231       event->any.stage = clutter_stage_get_default ();
232 
233       last_x = event->button.x = tsevent.x;
234       last_y = event->button.y = tsevent.y;
235 
236       if (tsevent.pressure && !clicked)
237         {
238 	  event->button.type = event->type = CLUTTER_BUTTON_PRESS;
239           event->button.time = get_backend_time ();
240           event->button.modifier_state = 0;
241           event->button.button = 1;
242 
243           clicked = TRUE;
244         }
245       else if (tsevent.pressure && clicked)
246         {
247           event->motion.type = event->type = CLUTTER_MOTION;
248           event->motion.time = get_backend_time ();
249           event->motion.modifier_state = 0;
250         }
251       else
252         {
253 	  event->button.type = event->type = CLUTTER_BUTTON_RELEASE;
254           event->button.time = get_backend_time ();
255           event->button.modifier_state = 0;
256           event->button.button = 1;
257 
258           clicked = FALSE;
259         }
260 
261       _clutter_event_push (event, FALSE);
262     }
263 
264   /* Pop an event off the queue if any */
265   event = clutter_event_get ();
266 
267   if (event)
268     {
269       /* forward the event into clutter for emission etc. */
270       _clutter_stage_queue_event (event->any.stage, event, FALSE);
271     }
272 
273 out:
274   _clutter_threads_release_lock ();
275 
276   return TRUE;
277 }
278