| 1 |
/* GDK - The GIMP Drawing Kit |
| 2 |
* Copyright (C) 2009 Carlos Garnacho <[email protected]> |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "config.h" |
| 19 |
|
| 20 |
#include <gdk/gdktypes.h> |
| 21 |
#include <gdk/gdkdevicemanager.h> |
| 22 |
#include <gdk/gdkdeviceprivate.h> |
| 23 |
#include <gdk/gdkseatdefaultprivate.h> |
| 24 |
#include <gdk/gdkdevicemanagerprivate.h> |
| 25 |
#include <gdk/gdkdisplayprivate.h> |
| 26 |
#include "gdkdevicemanager-core-quartz.h" |
| 27 |
#include "gdkquartzdevice-core.h" |
| 28 |
#include "gdkkeysyms.h" |
| 29 |
#include "gdkprivate-quartz.h" |
| 30 |
#include "gdkinternal-quartz.h" |
| 31 |
|
| 32 |
typedef enum |
| 33 |
{ |
| 34 |
#if MAC_OS_X_VERSION_MIN_REQUIRED < 101200 |
| 35 |
GDK_QUARTZ_POINTER_DEVICE_TYPE_CURSOR = NSCursorPointingDevice, |
| 36 |
GDK_QUARTZ_POINTER_DEVICE_TYPE_ERASER = NSEraserPointingDevice, |
| 37 |
GDK_QUARTZ_POINTER_DEVICE_TYPE_PEN = NSPenPointingDevice, |
| 38 |
#else |
| 39 |
GDK_QUARTZ_POINTER_DEVICE_TYPE_CURSOR = NSPointingDeviceTypeCursor, |
| 40 |
GDK_QUARTZ_POINTER_DEVICE_TYPE_ERASER = NSPointingDeviceTypeEraser, |
| 41 |
GDK_QUARTZ_POINTER_DEVICE_TYPE_PEN = NSPointingDeviceTypePen, |
| 42 |
#endif |
| 43 |
} GdkQuartzPointerDeviceType; |
| 44 |
|
| 45 |
#define HAS_FOCUS(toplevel) \ |
| 46 |
((toplevel)->has_focus || (toplevel)->has_pointer_focus) |
| 47 |
|
| 48 |
static void gdk_quartz_device_manager_core_finalize (GObject *object); |
| 49 |
static void gdk_quartz_device_manager_core_constructed (GObject *object); |
| 50 |
|
| 51 |
static GList * gdk_quartz_device_manager_core_list_devices (GdkDeviceManager *device_manager, |
| 52 |
GdkDeviceType type); |
| 53 |
static GdkDevice * gdk_quartz_device_manager_core_get_client_pointer (GdkDeviceManager *device_manager); |
| 54 |
|
| 55 |
|
| 56 |
G_DEFINE_TYPE (GdkQuartzDeviceManagerCore, gdk_quartz_device_manager_core, GDK_TYPE_DEVICE_MANAGER) |
| 57 |
|
| 58 |
static void |
| 59 |
gdk_quartz_device_manager_core_class_init (GdkQuartzDeviceManagerCoreClass *klass) |
| 60 |
{ |
| 61 |
GdkDeviceManagerClass *device_manager_class = GDK_DEVICE_MANAGER_CLASS (klass); |
| 62 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 63 |
|
| 64 |
object_class->finalize = gdk_quartz_device_manager_core_finalize; |
| 65 |
object_class->constructed = gdk_quartz_device_manager_core_constructed; |
| 66 |
device_manager_class->list_devices = gdk_quartz_device_manager_core_list_devices; |
| 67 |
device_manager_class->get_client_pointer = gdk_quartz_device_manager_core_get_client_pointer; |
| 68 |
} |
| 69 |
|
| 70 |
static GdkDevice * |
| 71 |
create_core_pointer (GdkDeviceManager *device_manager, |
| 72 |
GdkDisplay *display) |
| 73 |
{ |
| 74 |
return g_object_new (GDK_TYPE_QUARTZ_DEVICE_CORE, |
| 75 |
"name", "Core Pointer", |
| 76 |
"type", GDK_DEVICE_TYPE_MASTER, |
| 77 |
"input-source", GDK_SOURCE_MOUSE, |
| 78 |
"input-mode", GDK_MODE_SCREEN, |
| 79 |
"has-cursor", TRUE, |
| 80 |
"display", display, |
| 81 |
"device-manager", device_manager, |
| 82 |
NULL); |
| 83 |
} |
| 84 |
|
| 85 |
static GdkDevice * |
| 86 |
create_core_keyboard (GdkDeviceManager *device_manager, |
| 87 |
GdkDisplay *display) |
| 88 |
{ |
| 89 |
return g_object_new (GDK_TYPE_QUARTZ_DEVICE_CORE, |
| 90 |
"name", "Core Keyboard", |
| 91 |
"type", GDK_DEVICE_TYPE_MASTER, |
| 92 |
"input-source", GDK_SOURCE_KEYBOARD, |
| 93 |
"input-mode", GDK_MODE_SCREEN, |
| 94 |
"has-cursor", FALSE, |
| 95 |
"display", display, |
| 96 |
"device-manager", device_manager, |
| 97 |
NULL); |
| 98 |
} |
| 99 |
|
| 100 |
static void |
| 101 |
gdk_quartz_device_manager_core_init (GdkQuartzDeviceManagerCore *device_manager) |
| 102 |
{ |
| 103 |
device_manager->known_tablet_devices = NULL; |
| 104 |
} |
| 105 |
|
| 106 |
static void |
| 107 |
gdk_quartz_device_manager_core_finalize (GObject *object) |
| 108 |
{ |
| 109 |
GdkQuartzDeviceManagerCore *quartz_device_manager_core; |
| 110 |
|
| 111 |
quartz_device_manager_core = GDK_QUARTZ_DEVICE_MANAGER_CORE (object); |
| 112 |
|
| 113 |
g_object_unref (quartz_device_manager_core->core_pointer); |
| 114 |
g_object_unref (quartz_device_manager_core->core_keyboard); |
| 115 |
|
| 116 |
g_list_free_full (quartz_device_manager_core->known_tablet_devices, g_object_unref); |
| 117 |
|
| 118 |
G_OBJECT_CLASS (gdk_quartz_device_manager_core_parent_class)->finalize (object); |
| 119 |
} |
| 120 |
|
| 121 |
static void |
| 122 |
gdk_quartz_device_manager_core_constructed (GObject *object) |
| 123 |
{ |
| 124 |
GdkQuartzDeviceManagerCore *device_manager; |
| 125 |
GdkDisplay *display; |
| 126 |
GdkSeat *seat; |
| 127 |
|
| 128 |
device_manager = GDK_QUARTZ_DEVICE_MANAGER_CORE (object); |
| 129 |
display = gdk_device_manager_get_display (GDK_DEVICE_MANAGER (object)); |
| 130 |
device_manager->core_pointer = create_core_pointer (GDK_DEVICE_MANAGER (device_manager), display); |
| 131 |
device_manager->core_keyboard = create_core_keyboard (GDK_DEVICE_MANAGER (device_manager), display); |
| 132 |
|
| 133 |
_gdk_device_set_associated_device (device_manager->core_pointer, device_manager->core_keyboard); |
| 134 |
_gdk_device_set_associated_device (device_manager->core_keyboard, device_manager->core_pointer); |
| 135 |
|
| 136 |
seat = gdk_seat_default_new_for_master_pair (device_manager->core_pointer, |
| 137 |
device_manager->core_keyboard); |
| 138 |
gdk_display_add_seat (display, seat); |
| 139 |
g_object_unref (seat); |
| 140 |
} |
| 141 |
|
| 142 |
static GList * |
| 143 |
gdk_quartz_device_manager_core_list_devices (GdkDeviceManager *device_manager, |
| 144 |
GdkDeviceType type) |
| 145 |
{ |
| 146 |
GdkQuartzDeviceManagerCore *self; |
| 147 |
GList *devices = NULL; |
| 148 |
GList *l; |
| 149 |
|
| 150 |
self = GDK_QUARTZ_DEVICE_MANAGER_CORE (device_manager); |
| 151 |
|
| 152 |
if (type == GDK_DEVICE_TYPE_MASTER) |
| 153 |
{ |
| 154 |
devices = g_list_prepend (devices, self->core_keyboard); |
| 155 |
devices = g_list_prepend (devices, self->core_pointer); |
| 156 |
} |
| 157 |
|
| 158 |
for (l = self->known_tablet_devices; l; l = g_list_next (l)) |
| 159 |
{ |
| 160 |
devices = g_list_prepend (devices, GDK_DEVICE (l->data)); |
| 161 |
} |
| 162 |
|
| 163 |
devices = g_list_reverse (devices); |
| 164 |
|
| 165 |
return devices; |
| 166 |
} |
| 167 |
|
| 168 |
static GdkDevice * |
| 169 |
gdk_quartz_device_manager_core_get_client_pointer (GdkDeviceManager *device_manager) |
| 170 |
{ |
| 171 |
GdkQuartzDeviceManagerCore *quartz_device_manager_core; |
| 172 |
|
| 173 |
quartz_device_manager_core = (GdkQuartzDeviceManagerCore *) device_manager; |
| 174 |
return quartz_device_manager_core->core_pointer; |
| 175 |
} |
| 176 |
|
| 177 |
static GdkDevice * |
| 178 |
create_core_device (GdkDeviceManager *device_manager, |
| 179 |
const gchar *device_name, |
| 180 |
GdkInputSource source) |
| 181 |
{ |
| 182 |
GdkDisplay *display = gdk_device_manager_get_display (device_manager); |
| 183 |
GdkDevice *device = g_object_new (GDK_TYPE_QUARTZ_DEVICE_CORE, |
| 184 |
"name", device_name, |
| 185 |
"type", GDK_DEVICE_TYPE_SLAVE, |
| 186 |
"input-source", source, |
| 187 |
"input-mode", GDK_MODE_DISABLED, |
| 188 |
"has-cursor", FALSE, |
| 189 |
"display", display, |
| 190 |
"device-manager", device_manager, |
| 191 |
NULL); |
| 192 |
|
| 193 |
_gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_PRESSURE, 0.0, 1.0, 0.001); |
| 194 |
_gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_XTILT, -1.0, 1.0, 0.001); |
| 195 |
_gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_YTILT, -1.0, 1.0, 0.001); |
| 196 |
|
| 197 |
return device; |
| 198 |
} |
| 199 |
|
| 200 |
static void |
| 201 |
mimic_device_axes (GdkDevice *logical, |
| 202 |
GdkDevice *physical) |
| 203 |
{ |
| 204 |
double axis_min, axis_max, axis_resolution; |
| 205 |
GdkAtom axis_label; |
| 206 |
GdkAxisUse axis_use; |
| 207 |
int axis_count; |
| 208 |
int i; |
| 209 |
|
| 210 |
axis_count = gdk_device_get_n_axes (physical); |
| 211 |
|
| 212 |
for (i = 0; i < axis_count; i++) |
| 213 |
{ |
| 214 |
_gdk_device_get_axis_info (physical, i, &axis_label, &axis_use, &axis_min, |
| 215 |
&axis_max, &axis_resolution); |
| 216 |
_gdk_device_add_axis (logical, axis_label, axis_use, axis_min, |
| 217 |
axis_max, axis_resolution); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
static void |
| 222 |
translate_device_axes (GdkDevice *source_device, |
| 223 |
gboolean active) |
| 224 |
{ |
| 225 |
GdkSeat *seat = gdk_display_get_default_seat (_gdk_display); |
| 226 |
GdkDevice *core_pointer = gdk_seat_get_pointer (seat); |
| 227 |
|
| 228 |
g_object_freeze_notify (G_OBJECT (core_pointer)); |
| 229 |
|
| 230 |
_gdk_device_reset_axes (core_pointer); |
| 231 |
if (active && source_device) |
| 232 |
{ |
| 233 |
mimic_device_axes (core_pointer, source_device); |
| 234 |
} |
| 235 |
else |
| 236 |
{ |
| 237 |
_gdk_device_add_axis (core_pointer, GDK_NONE, GDK_AXIS_X, 0, 0, 1); |
| 238 |
_gdk_device_add_axis (core_pointer, GDK_NONE, GDK_AXIS_Y, 0, 0, 1); |
| 239 |
} |
| 240 |
|
| 241 |
g_object_thaw_notify (G_OBJECT (core_pointer)); |
| 242 |
} |
| 243 |
|
| 244 |
void |
| 245 |
_gdk_quartz_device_manager_register_device_for_ns_event (GdkDeviceManager *device_manager, |
| 246 |
NSEvent *nsevent) |
| 247 |
{ |
| 248 |
GdkQuartzDeviceManagerCore *self = GDK_QUARTZ_DEVICE_MANAGER_CORE (device_manager); |
| 249 |
GList *l = NULL; |
| 250 |
GdkInputSource input_source = GDK_SOURCE_MOUSE; |
| 251 |
GdkDevice *device = NULL; |
| 252 |
|
| 253 |
/* Only handle device updates for proximity events */ |
| 254 |
if ([nsevent type] != GDK_QUARTZ_EVENT_TABLET_PROXIMITY && |
| 255 |
[nsevent subtype] != GDK_QUARTZ_EVENT_SUBTYPE_TABLET_PROXIMITY) |
| 256 |
return; |
| 257 |
|
| 258 |
if ([nsevent pointingDeviceType] == GDK_QUARTZ_POINTER_DEVICE_TYPE_PEN) |
| 259 |
input_source = GDK_SOURCE_PEN; |
| 260 |
else if ([nsevent pointingDeviceType] == GDK_QUARTZ_POINTER_DEVICE_TYPE_CURSOR) |
| 261 |
input_source = GDK_SOURCE_CURSOR; |
| 262 |
else if ([nsevent pointingDeviceType] == GDK_QUARTZ_POINTER_DEVICE_TYPE_ERASER) |
| 263 |
input_source = GDK_SOURCE_ERASER; |
| 264 |
|
| 265 |
for (l = self->known_tablet_devices; l; l = g_list_next (l)) |
| 266 |
{ |
| 267 |
GdkDevice *device_to_check = GDK_DEVICE (l->data); |
| 268 |
|
| 269 |
if (input_source == gdk_device_get_source (device_to_check) && |
| 270 |
[nsevent uniqueID] == _gdk_quartz_device_core_get_unique (device_to_check)) |
| 271 |
{ |
| 272 |
device = device_to_check; |
| 273 |
if ([nsevent isEnteringProximity]) |
| 274 |
{ |
| 275 |
if (!_gdk_quartz_device_core_is_active (device, [nsevent deviceID])) |
| 276 |
self->num_active_devices++; |
| 277 |
|
| 278 |
_gdk_quartz_device_core_set_active (device, TRUE, [nsevent deviceID]); |
| 279 |
} |
| 280 |
else |
| 281 |
{ |
| 282 |
if (_gdk_quartz_device_core_is_active (device, [nsevent deviceID])) |
| 283 |
self->num_active_devices--; |
| 284 |
|
| 285 |
_gdk_quartz_device_core_set_active (device, FALSE, [nsevent deviceID]); |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
/* If we haven't seen this device before, add it */ |
| 291 |
if (!device) |
| 292 |
{ |
| 293 |
GdkSeat *seat; |
| 294 |
|
| 295 |
switch (input_source) |
| 296 |
{ |
| 297 |
case GDK_SOURCE_PEN: |
| 298 |
device = create_core_device (device_manager, |
| 299 |
"Quartz Pen", |
| 300 |
GDK_SOURCE_PEN); |
| 301 |
break; |
| 302 |
case GDK_SOURCE_CURSOR: |
| 303 |
device = create_core_device (device_manager, |
| 304 |
"Quartz Cursor", |
| 305 |
GDK_SOURCE_CURSOR); |
| 306 |
break; |
| 307 |
case GDK_SOURCE_ERASER: |
| 308 |
device = create_core_device (device_manager, |
| 309 |
"Quartz Eraser", |
| 310 |
GDK_SOURCE_ERASER); |
| 311 |
break; |
| 312 |
default: |
| 313 |
g_warning ("GDK Quarz unknown input source: %i", input_source); |
| 314 |
break; |
| 315 |
} |
| 316 |
|
| 317 |
_gdk_device_set_associated_device (GDK_DEVICE (device), self->core_pointer); |
| 318 |
_gdk_device_add_slave (self->core_pointer, GDK_DEVICE (device)); |
| 319 |
|
| 320 |
seat = gdk_device_get_seat (self->core_pointer); |
| 321 |
gdk_seat_default_add_slave (GDK_SEAT_DEFAULT (seat), device); |
| 322 |
|
| 323 |
_gdk_quartz_device_core_set_unique (device, [nsevent uniqueID]); |
| 324 |
_gdk_quartz_device_core_set_active (device, TRUE, [nsevent deviceID]); |
| 325 |
|
| 326 |
self->known_tablet_devices = g_list_append (self->known_tablet_devices, |
| 327 |
device); |
| 328 |
|
| 329 |
if ([nsevent isEnteringProximity]) |
| 330 |
{ |
| 331 |
if (!_gdk_quartz_device_core_is_active (device, [nsevent deviceID])) |
| 332 |
self->num_active_devices++; |
| 333 |
_gdk_quartz_device_core_set_active (device, TRUE, [nsevent deviceID]); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
translate_device_axes (device, [nsevent isEnteringProximity]); |
| 338 |
|
| 339 |
if (self->num_active_devices) |
| 340 |
[NSEvent setMouseCoalescingEnabled: FALSE]; |
| 341 |
else |
| 342 |
[NSEvent setMouseCoalescingEnabled: TRUE]; |
| 343 |
} |
| 344 |
|
| 345 |
GdkDevice * |
| 346 |
_gdk_quartz_device_manager_core_device_for_ns_event (GdkDeviceManager *device_manager, |
| 347 |
NSEvent *nsevent) |
| 348 |
{ |
| 349 |
GdkQuartzDeviceManagerCore *self = GDK_QUARTZ_DEVICE_MANAGER_CORE (device_manager); |
| 350 |
GdkDevice *device = NULL; |
| 351 |
|
| 352 |
if ([nsevent type] == GDK_QUARTZ_EVENT_TABLET_PROXIMITY || |
| 353 |
[nsevent subtype] == GDK_QUARTZ_EVENT_SUBTYPE_TABLET_PROXIMITY || |
| 354 |
[nsevent subtype] == GDK_QUARTZ_EVENT_SUBTYPE_TABLET_POINT) |
| 355 |
{ |
| 356 |
/* Find the device based on deviceID */ |
| 357 |
GList *l = NULL; |
| 358 |
|
| 359 |
for (l = self->known_tablet_devices; l && !device; l = g_list_next (l)) |
| 360 |
{ |
| 361 |
GdkDevice *device_to_check = GDK_DEVICE (l->data); |
| 362 |
|
| 363 |
if (_gdk_quartz_device_core_is_active (device_to_check, [nsevent deviceID])) |
| 364 |
device = device_to_check; |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
if (!device) |
| 369 |
device = self->core_pointer; |
| 370 |
|
| 371 |
return device; |
| 372 |
} |