| 1 |
/* |
| 2 |
* Clutter. |
| 3 |
* |
| 4 |
* An OpenGL based 'interactive canvas' library. |
| 5 |
* |
| 6 |
* Copyright (C) 2010 Intel Corp. |
| 7 |
* Copyright (C) 2014 Jonas Ådahl |
| 8 |
* |
| 9 |
* This library is free software; you can redistribute it and/or |
| 10 |
* modify it under the terms of the GNU Lesser General Public |
| 11 |
* License as published by the Free Software Foundation; either |
| 12 |
* version 2 of the License, or (at your option) any later version. |
| 13 |
* |
| 14 |
* This library is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
* Lesser General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU Lesser General Public |
| 20 |
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
* |
| 22 |
* Author: Damien Lespiau <[email protected]> |
| 23 |
* Author: Jonas Ådahl <[email protected]> |
| 24 |
*/ |
| 25 |
|
| 26 |
#ifdef HAVE_CONFIG_H |
| 27 |
#include "config.h" |
| 28 |
#endif |
| 29 |
|
| 30 |
#include "clutter/clutter-device-manager-private.h" |
| 31 |
#include "clutter-private.h" |
| 32 |
#include "clutter-evdev.h" |
| 33 |
|
| 34 |
#include "clutter-input-device-evdev.h" |
| 35 |
#include "clutter-device-manager-evdev.h" |
| 36 |
|
| 37 |
typedef struct _ClutterInputDeviceClass ClutterInputDeviceEvdevClass; |
| 38 |
|
| 39 |
#define clutter_input_device_evdev_get_type _clutter_input_device_evdev_get_type |
| 40 |
|
| 41 |
G_DEFINE_TYPE (ClutterInputDeviceEvdev, |
| 42 |
clutter_input_device_evdev, |
| 43 |
CLUTTER_TYPE_INPUT_DEVICE) |
| 44 |
|
| 45 |
static void |
| 46 |
clutter_input_device_evdev_finalize (GObject *object) |
| 47 |
{ |
| 48 |
ClutterInputDevice *device = CLUTTER_INPUT_DEVICE (object); |
| 49 |
ClutterInputDeviceEvdev *device_evdev = CLUTTER_INPUT_DEVICE_EVDEV (object); |
| 50 |
ClutterDeviceManagerEvdev *manager_evdev = |
| 51 |
CLUTTER_DEVICE_MANAGER_EVDEV (device->device_manager); |
| 52 |
|
| 53 |
if (device_evdev->libinput_device) |
| 54 |
libinput_device_unref (device_evdev->libinput_device); |
| 55 |
|
| 56 |
_clutter_device_manager_evdev_release_device_id (manager_evdev, device); |
| 57 |
|
| 58 |
G_OBJECT_CLASS (clutter_input_device_evdev_parent_class)->finalize (object); |
| 59 |
} |
| 60 |
|
| 61 |
static gboolean |
| 62 |
clutter_input_device_evdev_keycode_to_evdev (ClutterInputDevice *device, |
| 63 |
guint hardware_keycode, |
| 64 |
guint *evdev_keycode) |
| 65 |
{ |
| 66 |
/* The hardware keycodes from the evdev backend are almost evdev |
| 67 |
keycodes: we use the evdev keycode file, but xkb rules have an |
| 68 |
offset by 8. See the comment in _clutter_key_event_new_from_evdev() |
| 69 |
*/ |
| 70 |
*evdev_keycode = hardware_keycode - 8; |
| 71 |
return TRUE; |
| 72 |
} |
| 73 |
|
| 74 |
static void |
| 75 |
clutter_input_device_evdev_class_init (ClutterInputDeviceEvdevClass *klass) |
| 76 |
{ |
| 77 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 78 |
|
| 79 |
object_class->finalize = clutter_input_device_evdev_finalize; |
| 80 |
klass->keycode_to_evdev = clutter_input_device_evdev_keycode_to_evdev; |
| 81 |
} |
| 82 |
|
| 83 |
static void |
| 84 |
clutter_input_device_evdev_init (ClutterInputDeviceEvdev *self) |
| 85 |
{ |
| 86 |
} |
| 87 |
|
| 88 |
/* |
| 89 |
* _clutter_input_device_evdev_new: |
| 90 |
* @manager: the device manager |
| 91 |
* @seat: the seat the device will belong to |
| 92 |
* @libinput_device: the libinput device |
| 93 |
* |
| 94 |
* Create a new ClutterInputDevice given a libinput device and associate |
| 95 |
* it with the provided seat. |
| 96 |
*/ |
| 97 |
ClutterInputDevice * |
| 98 |
_clutter_input_device_evdev_new (ClutterDeviceManager *manager, |
| 99 |
ClutterSeatEvdev *seat, |
| 100 |
struct libinput_device *libinput_device) |
| 101 |
{ |
| 102 |
ClutterInputDeviceEvdev *device; |
| 103 |
ClutterInputDeviceType type; |
| 104 |
ClutterDeviceManagerEvdev *manager_evdev; |
| 105 |
gchar *vendor, *product; |
| 106 |
gint device_id; |
| 107 |
|
| 108 |
type = _clutter_input_device_evdev_determine_type (libinput_device); |
| 109 |
vendor = g_strdup_printf ("%.4x", libinput_device_get_id_vendor (libinput_device)); |
| 110 |
product = g_strdup_printf ("%.4x", libinput_device_get_id_product (libinput_device)); |
| 111 |
manager_evdev = CLUTTER_DEVICE_MANAGER_EVDEV (manager); |
| 112 |
device_id = _clutter_device_manager_evdev_acquire_device_id (manager_evdev); |
| 113 |
device = g_object_new (CLUTTER_TYPE_INPUT_DEVICE_EVDEV, |
| 114 |
"id", device_id, |
| 115 |
"name", libinput_device_get_sysname (libinput_device), |
| 116 |
"device-manager", manager, |
| 117 |
"device-type", type, |
| 118 |
"device-mode", CLUTTER_INPUT_MODE_SLAVE, |
| 119 |
"enabled", TRUE, |
| 120 |
"vendor-id", vendor, |
| 121 |
"product-id", product, |
| 122 |
NULL); |
| 123 |
|
| 124 |
device->seat = seat; |
| 125 |
device->libinput_device = libinput_device; |
| 126 |
|
| 127 |
libinput_device_set_user_data (libinput_device, device); |
| 128 |
libinput_device_ref (libinput_device); |
| 129 |
g_free (vendor); |
| 130 |
g_free (product); |
| 131 |
|
| 132 |
return CLUTTER_INPUT_DEVICE (device); |
| 133 |
} |
| 134 |
|
| 135 |
/* |
| 136 |
* _clutter_input_device_evdev_new_virtual: |
| 137 |
* @manager: the device manager |
| 138 |
* @seat: the seat the device will belong to |
| 139 |
* @type: the input device type |
| 140 |
* |
| 141 |
* Create a new virtual ClutterInputDevice of the given type. |
| 142 |
*/ |
| 143 |
ClutterInputDevice * |
| 144 |
_clutter_input_device_evdev_new_virtual (ClutterDeviceManager *manager, |
| 145 |
ClutterSeatEvdev *seat, |
| 146 |
ClutterInputDeviceType type) |
| 147 |
{ |
| 148 |
ClutterInputDeviceEvdev *device; |
| 149 |
ClutterDeviceManagerEvdev *manager_evdev; |
| 150 |
const char *name; |
| 151 |
gint device_id; |
| 152 |
|
| 153 |
switch (type) |
| 154 |
{ |
| 155 |
case CLUTTER_KEYBOARD_DEVICE: |
| 156 |
name = "Virtual keyboard device for seat"; |
| 157 |
break; |
| 158 |
case CLUTTER_POINTER_DEVICE: |
| 159 |
name = "Virtual pointer device for seat"; |
| 160 |
break; |
| 161 |
default: |
| 162 |
name = "Virtual device for seat"; |
| 163 |
break; |
| 164 |
}; |
| 165 |
|
| 166 |
manager_evdev = CLUTTER_DEVICE_MANAGER_EVDEV (manager); |
| 167 |
device_id = _clutter_device_manager_evdev_acquire_device_id (manager_evdev); |
| 168 |
device = g_object_new (CLUTTER_TYPE_INPUT_DEVICE_EVDEV, |
| 169 |
"id", device_id, |
| 170 |
"name", name, |
| 171 |
"device-manager", manager, |
| 172 |
"device-type", type, |
| 173 |
"device-mode", CLUTTER_INPUT_MODE_MASTER, |
| 174 |
"enabled", TRUE, |
| 175 |
NULL); |
| 176 |
|
| 177 |
device->seat = seat; |
| 178 |
|
| 179 |
return CLUTTER_INPUT_DEVICE (device); |
| 180 |
} |
| 181 |
|
| 182 |
ClutterSeatEvdev * |
| 183 |
_clutter_input_device_evdev_get_seat (ClutterInputDeviceEvdev *device) |
| 184 |
{ |
| 185 |
return device->seat; |
| 186 |
} |
| 187 |
|
| 188 |
void |
| 189 |
_clutter_input_device_evdev_update_leds (ClutterInputDeviceEvdev *device, |
| 190 |
enum libinput_led leds) |
| 191 |
{ |
| 192 |
if (!device->libinput_device) |
| 193 |
return; |
| 194 |
|
| 195 |
libinput_device_led_update (device->libinput_device, leds); |
| 196 |
} |
| 197 |
|
| 198 |
ClutterInputDeviceType |
| 199 |
_clutter_input_device_evdev_determine_type (struct libinput_device *ldev) |
| 200 |
{ |
| 201 |
/* This setting is specific to touchpads and alike, only in these |
| 202 |
* devices there is this additional layer of touch event interpretation. |
| 203 |
*/ |
| 204 |
if (libinput_device_config_tap_get_finger_count (ldev) > 0) |
| 205 |
return CLUTTER_TOUCHPAD_DEVICE; |
| 206 |
else if (libinput_device_has_capability (ldev, LIBINPUT_DEVICE_CAP_POINTER)) |
| 207 |
return CLUTTER_POINTER_DEVICE; |
| 208 |
else if (libinput_device_has_capability (ldev, LIBINPUT_DEVICE_CAP_TOUCH)) |
| 209 |
return CLUTTER_TOUCHSCREEN_DEVICE; |
| 210 |
else if (libinput_device_has_capability (ldev, LIBINPUT_DEVICE_CAP_KEYBOARD)) |
| 211 |
return CLUTTER_KEYBOARD_DEVICE; |
| 212 |
else |
| 213 |
return CLUTTER_EXTENSION_DEVICE; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* clutter_evdev_input_device_get_libinput_device: |
| 218 |
* @device: a #ClutterInputDevice |
| 219 |
* |
| 220 |
* Retrieves the libinput_device struct held in @device. |
| 221 |
* |
| 222 |
* Returns: The libinput_device struct |
| 223 |
* |
| 224 |
* Since: 1.20 |
| 225 |
* Stability: unstable |
| 226 |
**/ |
| 227 |
struct libinput_device * |
| 228 |
clutter_evdev_input_device_get_libinput_device (ClutterInputDevice *device) |
| 229 |
{ |
| 230 |
ClutterInputDeviceEvdev *device_evdev; |
| 231 |
|
| 232 |
g_return_val_if_fail (CLUTTER_IS_INPUT_DEVICE_EVDEV (device), NULL); |
| 233 |
|
| 234 |
device_evdev = CLUTTER_INPUT_DEVICE_EVDEV (device); |
| 235 |
|
| 236 |
return device_evdev->libinput_device; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* clutter_evdev_event_sequence_get_slot: |
| 241 |
* @sequence: a #ClutterEventSequence |
| 242 |
* |
| 243 |
* Retrieves the touch slot triggered by this @sequence |
| 244 |
* |
| 245 |
* Returns: the libinput touch slot. |
| 246 |
* |
| 247 |
* Since: 1.20 |
| 248 |
* Stability: unstable |
| 249 |
**/ |
| 250 |
gint32 |
| 251 |
clutter_evdev_event_sequence_get_slot (const ClutterEventSequence *sequence) |
| 252 |
{ |
| 253 |
if (!sequence) |
| 254 |
return -1; |
| 255 |
|
| 256 |
return GPOINTER_TO_INT (sequence) - 1; |
| 257 |
} |