4 * An OpenGL based 'interactive canvas' library.
6 * Copyright © 2010, 2011 Intel Corp.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 * Author: Emmanuele Bassi <ebassi@linux.intel.com>
26 #include "clutter-input-device-core-x11.h"
28 #include "clutter-debug.h"
29 #include "clutter-device-manager-private.h"
30 #include "clutter-private.h"
31 #include "clutter-stage-private.h"
33 #include "clutter-backend-x11.h"
34 #include "clutter-stage-x11.h"
37 #include <X11/extensions/XInput.h>
40 #define MAX_DEVICE_CLASSES 13
42 typedef struct _ClutterInputDeviceClass ClutterInputDeviceX11Class;
44 /* a specific X11 input device */
45 struct _ClutterInputDeviceX11
47 ClutterInputDevice device;
52 XEventClass event_classes[MAX_DEVICE_CLASSES];
55 int button_press_type;
56 int button_release_type;
57 int motion_notify_type;
58 int state_notify_type;
61 #endif /* HAVE_XINPUT */
69 #define clutter_input_device_x11_get_type _clutter_input_device_x11_get_type
71 G_DEFINE_TYPE (ClutterInputDeviceX11,
72 clutter_input_device_x11,
73 CLUTTER_TYPE_INPUT_DEVICE);
76 clutter_input_device_x11_select_stage_events (ClutterInputDevice *device,
81 ClutterBackendX11 *backend_x11 = CLUTTER_BACKEND_X11 (device->backend);
82 ClutterInputDeviceX11 *device_x11;
83 ClutterStageX11 *stage_x11;
87 device_x11 = CLUTTER_INPUT_DEVICE_X11 (device);
89 stage_x11 = CLUTTER_STAGE_X11 (_clutter_stage_get_window (stage));
93 if (event_mask & ButtonPressMask)
95 DeviceButtonPress (device_x11->xdevice,
96 device_x11->button_press_type,
99 device_x11->event_classes[i++] = class;
101 DeviceButtonPressGrab (device_x11->xdevice, 0, class);
103 device_x11->event_classes[i++] = class;
106 if (event_mask & ButtonReleaseMask)
108 DeviceButtonRelease (device_x11->xdevice,
109 device_x11->button_release_type,
112 device_x11->event_classes[i++] = class;
115 if (event_mask & PointerMotionMask)
117 DeviceMotionNotify (device_x11->xdevice,
118 device_x11->motion_notify_type,
121 device_x11->event_classes[i++] = class;
123 DeviceStateNotify (device_x11->xdevice,
124 device_x11->state_notify_type,
127 device_x11->event_classes[i++] = class;
130 if (event_mask & KeyPressMask)
132 DeviceKeyPress (device_x11->xdevice,
133 device_x11->key_press_type,
136 device_x11->event_classes[i++] = class;
139 if (event_mask & KeyReleaseMask)
141 DeviceKeyRelease (device_x11->xdevice,
142 device_x11->key_release_type,
145 device_x11->event_classes[i++] = class;
148 device_x11->num_classes = i;
150 XSelectExtensionEvent (backend_x11->xdpy,
152 device_x11->event_classes,
153 device_x11->num_classes);
154 #endif /* HAVE_XINPUT */
158 clutter_input_device_x11_dispose (GObject *gobject)
160 ClutterInputDeviceX11 *device_x11 = CLUTTER_INPUT_DEVICE_X11 (gobject);
163 if (device_x11->xdevice)
165 ClutterInputDevice *device = CLUTTER_INPUT_DEVICE (gobject);
166 ClutterBackendX11 *backend_x11 = CLUTTER_BACKEND_X11 (device->backend);
168 XCloseDevice (backend_x11->xdpy, device_x11->xdevice);
169 device_x11->xdevice = NULL;
171 #endif /* HAVE_XINPUT */
173 g_free (device_x11->axis_data);
175 G_OBJECT_CLASS (clutter_input_device_x11_parent_class)->dispose (gobject);
179 clutter_input_device_x11_constructed (GObject *gobject)
182 ClutterInputDeviceX11 *device_x11 = CLUTTER_INPUT_DEVICE_X11 (gobject);
183 ClutterInputDevice *device = CLUTTER_INPUT_DEVICE (gobject);
184 ClutterBackendX11 *backend_x11;
186 backend_x11 = CLUTTER_BACKEND_X11 (device->backend);
188 clutter_x11_trap_x_errors ();
190 device_x11->xdevice = XOpenDevice (backend_x11->xdpy, device->id);
192 if (clutter_x11_untrap_x_errors ())
194 g_warning ("Device '%s' cannot be opened",
195 clutter_input_device_get_device_name (device));
197 #endif /* HAVE_XINPUT */
199 if (G_OBJECT_CLASS (clutter_input_device_x11_parent_class)->constructed)
200 G_OBJECT_CLASS (clutter_input_device_x11_parent_class)->constructed (gobject);
204 clutter_input_device_x11_keycode_to_evdev (ClutterInputDevice *device,
205 guint hardware_keycode,
206 guint *evdev_keycode)
208 /* When using evdev under X11 the hardware keycodes are the evdev
209 keycodes plus 8. I haven't been able to find any documentation to
210 know what the +8 is for. FIXME: This should probably verify that
211 X server is using evdev. */
212 *evdev_keycode = hardware_keycode - 8;
218 clutter_input_device_x11_class_init (ClutterInputDeviceX11Class *klass)
220 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
221 ClutterInputDeviceClass *device_class = CLUTTER_INPUT_DEVICE_CLASS (klass);
223 gobject_class->constructed = clutter_input_device_x11_constructed;
224 gobject_class->dispose = clutter_input_device_x11_dispose;
226 device_class->select_stage_events = clutter_input_device_x11_select_stage_events;
227 device_class->keycode_to_evdev = clutter_input_device_x11_keycode_to_evdev;
231 clutter_input_device_x11_init (ClutterInputDeviceX11 *self)
236 _clutter_input_device_x11_set_keycodes (ClutterInputDeviceX11 *device_x11,
240 device_x11->min_keycode = min_keycode;
241 device_x11->max_keycode = max_keycode;
245 _clutter_input_device_x11_get_min_keycode (ClutterInputDeviceX11 *device_x11)
247 return device_x11->min_keycode;
251 _clutter_input_device_x11_get_max_keycode (ClutterInputDeviceX11 *device_x11)
253 return device_x11->max_keycode;
258 update_axes (ClutterInputDeviceX11 *device_x11,
263 ClutterInputDevice *device = CLUTTER_INPUT_DEVICE (device_x11);
266 if (device_x11->axis_data == NULL)
268 device_x11->axis_data =
269 g_new0 (gint, clutter_input_device_get_n_axes (device));
272 for (i = 0; i < n_axes; i++)
273 device_x11->axis_data[first_axis + i] = axes_data[i];
277 translate_axes (ClutterInputDeviceX11 *device_x11,
278 ClutterStageX11 *stage_x11,
282 ClutterInputDevice *device = CLUTTER_INPUT_DEVICE (device_x11);
288 if (!_clutter_stage_x11_get_root_coords (stage_x11, &root_x, &root_y))
292 n_axes = clutter_input_device_get_n_axes (device);
294 retval = g_new0 (gdouble, n_axes);
296 for (i = 0; i < n_axes; i++)
298 ClutterInputAxis axis;
300 axis = clutter_input_device_get_axis (device, i);
303 case CLUTTER_INPUT_AXIS_X:
304 case CLUTTER_INPUT_AXIS_Y:
305 _clutter_x11_input_device_translate_screen_coord (device,
308 device_x11->axis_data[i],
310 if (axis == CLUTTER_INPUT_AXIS_X)
312 else if (axis == CLUTTER_INPUT_AXIS_Y)
317 _clutter_input_device_translate_axis (device, i,
318 device_x11->axis_data[i],
335 * @state: the keyboard state of the core device
336 * @device_state: the button state of the device
338 * Trivially translates the state and the device state into a
342 translate_state (guint state,
345 return device_state | (state & 0xff);
347 #endif /* HAVE_XINPUT */
350 _clutter_input_device_x11_translate_xi_event (ClutterInputDeviceX11 *device_x11,
351 ClutterStageX11 *stage_x11,
356 ClutterInputDevice *device = CLUTTER_INPUT_DEVICE (device_x11);
358 if ((xevent->type == device_x11->button_press_type) ||
359 (xevent->type == device_x11->button_release_type))
361 XDeviceButtonEvent *xdbe = (XDeviceButtonEvent *) xevent;
363 event->button.type = event->type =
364 (xdbe->type == device_x11->button_press_type) ? CLUTTER_BUTTON_PRESS
365 : CLUTTER_BUTTON_RELEASE;
366 event->button.device = device;
367 event->button.time = xdbe->time;
368 event->button.button = xdbe->button;
369 event->button.modifier_state =
370 translate_state (xdbe->state, xdbe->device_state);
372 update_axes (device_x11,
377 event->button.axes = translate_axes (device_x11, stage_x11,
381 _clutter_stage_x11_set_user_time (stage_x11, event->button.time);
386 if ((xevent->type == device_x11->key_press_type) ||
387 (xevent->type == device_x11->key_release_type))
389 XDeviceKeyEvent *xdke = (XDeviceKeyEvent *) xevent;
391 if (xdke->keycode < device_x11->min_keycode ||
392 xdke->keycode >= device_x11->max_keycode)
394 g_warning ("Invalid device key code received: %d", xdke->keycode);
398 clutter_input_device_get_key (device,
399 xdke->keycode - device_x11->min_keycode,
401 &event->key.modifier_state);
402 if (event->key.keyval == 0)
405 event->key.type = event->type =
406 (xdke->type == device_x11->key_press_type) ? CLUTTER_KEY_PRESS
407 : CLUTTER_KEY_RELEASE;
408 event->key.time = xdke->time;
409 event->key.modifier_state |=
410 translate_state (xdke->state, xdke->device_state);
411 event->key.device = device;
414 if ((event->key.keyval >= 0x20) && (event->key.keyval <= 0xff))
416 event->key.unicode = (gunichar) event->key.keyval;
420 _clutter_stage_x11_set_user_time (stage_x11, event->key.time);
425 if (xevent->type == device_x11->motion_notify_type)
427 XDeviceMotionEvent *xdme = (XDeviceMotionEvent *) xevent;
429 event->motion.type = event->type = CLUTTER_MOTION;
430 event->motion.time = xdme->time;
431 event->motion.modifier_state =
432 translate_state (xdme->state, xdme->device_state);
433 event->motion.device = device;
436 g_new0 (gdouble, clutter_input_device_get_n_axes (device));
438 update_axes (device_x11,
443 event->motion.axes = translate_axes (device_x11, stage_x11,
450 if (xevent->type == device_x11->state_notify_type)
452 XDeviceStateNotifyEvent *xdse = (XDeviceStateNotifyEvent *) xevent;
453 XInputClass *input_class = (XInputClass *) xdse->data;
454 gint n_axes = clutter_input_device_get_n_axes (device);
457 for (i = 0; i < xdse->num_classes; i++)
459 if (input_class->class == ValuatorClass)
461 int *axis_data = ((XValuatorState *) input_class)->valuators;
463 update_axes (device_x11, n_axes, 0, axis_data);
467 (XInputClass *)(((char *) input_class) + input_class->length);
470 #endif /* HAVE_XINPUT */