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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* CDK - The GIMP Drawing Kit
 * Copyright (C) 2009 Carlos Garnacho <carlosg@gnome.org>
 * Copyright (C) 2010 Kristian Rietveld <kris@gtk.org>
 *
 * 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 <cdk/cdkdeviceprivate.h>
#include <cdk/cdkdisplayprivate.h>

#import "CdkQuartzView.h"
#include "cdkquartzwindow.h"
#include "cdkquartzcursor.h"
#include "cdkprivate-quartz.h"
#include "cdkquartzdevice-core.h"
#include "cdkinternal-quartz.h"

struct _CdkQuartzDeviceCore
{
  CdkDevice parent_instance;

  gboolean active;
  NSUInteger device_id;
  unsigned long long unique_id;
};

struct _CdkQuartzDeviceCoreClass
{
  CdkDeviceClass parent_class;
};

static gboolean cdk_quartz_device_core_get_history (CdkDevice      *device,
                                                    CdkWindow      *window,
                                                    guint32         start,
                                                    guint32         stop,
                                                    CdkTimeCoord ***events,
                                                    gint           *n_events);
static void cdk_quartz_device_core_get_state (CdkDevice       *device,
                                              CdkWindow       *window,
                                              gdouble         *axes,
                                              CdkModifierType *mask);
static void cdk_quartz_device_core_set_window_cursor (CdkDevice *device,
                                                      CdkWindow *window,
                                                      CdkCursor *cursor);
static void cdk_quartz_device_core_warp (CdkDevice *device,
                                         CdkScreen *screen,
                                         gdouble    x,
                                         gdouble    y);
static void cdk_quartz_device_core_query_state (CdkDevice        *device,
                                                CdkWindow        *window,
                                                CdkWindow       **root_window,
                                                CdkWindow       **child_window,
                                                gdouble          *root_x,
                                                gdouble          *root_y,
                                                gdouble          *win_x,
                                                gdouble          *win_y,
                                                CdkModifierType  *mask);
static CdkGrabStatus cdk_quartz_device_core_grab   (CdkDevice     *device,
                                                    CdkWindow     *window,
                                                    gboolean       owner_events,
                                                    CdkEventMask   event_mask,
                                                    CdkWindow     *confine_to,
                                                    CdkCursor     *cursor,
                                                    guint32        time_);
static void          cdk_quartz_device_core_ungrab (CdkDevice     *device,
                                                    guint32        time_);
static CdkWindow * cdk_quartz_device_core_window_at_position (CdkDevice       *device,
                                                              gdouble         *win_x,
                                                              gdouble         *win_y,
                                                              CdkModifierType *mask,
                                                              gboolean         get_toplevel);
static void      cdk_quartz_device_core_select_window_events (CdkDevice       *device,
                                                              CdkWindow       *window,
                                                              CdkEventMask     event_mask);


G_DEFINE_TYPE (CdkQuartzDeviceCore, cdk_quartz_device_core, CDK_TYPE_DEVICE)

static void
cdk_quartz_device_core_class_init (CdkQuartzDeviceCoreClass *klass)
{
  CdkDeviceClass *device_class = CDK_DEVICE_CLASS (klass);

  device_class->get_history = cdk_quartz_device_core_get_history;
  device_class->get_state = cdk_quartz_device_core_get_state;
  device_class->set_window_cursor = cdk_quartz_device_core_set_window_cursor;
  device_class->warp = cdk_quartz_device_core_warp;
  device_class->query_state = cdk_quartz_device_core_query_state;
  device_class->grab = cdk_quartz_device_core_grab;
  device_class->ungrab = cdk_quartz_device_core_ungrab;
  device_class->window_at_position = cdk_quartz_device_core_window_at_position;
  device_class->select_window_events = cdk_quartz_device_core_select_window_events;
}

static void
cdk_quartz_device_core_init (CdkQuartzDeviceCore *quartz_device_core)
{
  CdkDevice *device;

  device = CDK_DEVICE (quartz_device_core);

  _cdk_device_add_axis (device, CDK_NONE, CDK_AXIS_X, 0, 0, 1);
  _cdk_device_add_axis (device, CDK_NONE, CDK_AXIS_Y, 0, 0, 1);
}

static gboolean
cdk_quartz_device_core_get_history (CdkDevice      *device,
                                    CdkWindow      *window,
                                    guint32         start,
                                    guint32         stop,
                                    CdkTimeCoord ***events,
                                    gint           *n_events)
{
  return FALSE;
}

static void
cdk_quartz_device_core_get_state (CdkDevice       *device,
                                  CdkWindow       *window,
                                  gdouble         *axes,
                                  CdkModifierType *mask)
{
  gint x_int, y_int;

  cdk_window_get_device_position (window, device, &x_int, &y_int, mask);

  if (axes)
    {
      axes[0] = x_int;
      axes[1] = y_int;
    }
}

static void
translate_coords_to_child_coords (CdkWindow *parent,
                                  CdkWindow *child,
                                  gint      *x,
                                  gint      *y)
{
  CdkWindow *current = child;

  if (child == parent)
    return;

  while (current != parent)
    {
      gint tmp_x, tmp_y;

      cdk_window_get_origin (current, &tmp_x, &tmp_y);

      *x -= tmp_x;
      *y -= tmp_y;

      current = cdk_window_get_effective_parent (current);
    }
}

static void
cdk_quartz_device_core_set_window_cursor (CdkDevice *device,
                                          CdkWindow *window,
                                          CdkCursor *cursor)
{
  NSCursor *nscursor;

  if (CDK_WINDOW_DESTROYED (window))
    return;

  nscursor = _cdk_quartz_cursor_get_ns_cursor (cursor);

  [nscursor set];
}

static void
cdk_quartz_device_core_warp (CdkDevice *device,
                             CdkScreen *screen,
                             gdouble    x,
                             gdouble    y)
{
  CGDisplayMoveCursorToPoint (CGMainDisplayID (), CGPointMake (x, y));
}

static CdkWindow *
cdk_quartz_device_core_query_state_helper (CdkWindow       *window,
                                           CdkDevice       *device,
                                           gdouble         *x,
                                           gdouble         *y,
                                           CdkModifierType *mask)
{
  CdkWindow *toplevel;
  NSPoint point;
  gint x_tmp, y_tmp;
  CdkWindow *found_window;

  g_return_val_if_fail (window == NULL || CDK_IS_WINDOW (window), NULL);

  if (CDK_WINDOW_DESTROYED (window))
    {
      *x = 0;
      *y = 0;
      *mask = 0;
      return NULL;
    }

  toplevel = cdk_window_get_effective_toplevel (window);

  if (mask)
    *mask = _cdk_quartz_events_get_current_keyboard_modifiers () |
        _cdk_quartz_events_get_current_mouse_modifiers ();

  /* Get the y coordinate, needs to be flipped. */
  if (window == _cdk_root)
    {
      point = [NSEvent mouseLocation];<--- syntax error: =[...];
      _cdk_quartz_window_nspoint_to_cdk_xy (point, &x_tmp, &y_tmp);
    }
  else
    {
      CdkWindowImplQuartz *impl;
      NSWindow *nswindow;

      impl = CDK_WINDOW_IMPL_QUARTZ (toplevel->impl);
      nswindow = impl->toplevel;

      point = [nswindow mouseLocationOutsideOfEventStream];

      x_tmp = point.x;
      y_tmp = toplevel->height - point.y;

      window = toplevel;
    }

  found_window = _cdk_quartz_window_find_child (window, x_tmp, y_tmp,
                                                FALSE);

  if (found_window == _cdk_root)
    found_window = NULL;
  else if (found_window)
    translate_coords_to_child_coords (window, found_window,
                                      &x_tmp, &y_tmp);

  if (x)
    *x = x_tmp;

  if (y)
    *y = y_tmp;

  return found_window;
}

static void
cdk_quartz_device_core_query_state (CdkDevice        *device,
                                    CdkWindow        *window,
                                    CdkWindow       **root_window,
                                    CdkWindow       **child_window,
                                    gdouble          *root_x,
                                    gdouble          *root_y,
                                    gdouble          *win_x,
                                    gdouble          *win_y,
                                    CdkModifierType  *mask)
{
  CdkWindow *found_window;
  NSPoint point;
  gint x_tmp, y_tmp;

  found_window = cdk_quartz_device_core_query_state_helper (window, device,
                                                            win_x, win_y,
                                                            mask);

  if (root_window)
    *root_window = _cdk_root;

  if (child_window)
    *child_window = found_window;

  point = [NSEvent mouseLocation];
  _cdk_quartz_window_nspoint_to_cdk_xy (point, &x_tmp, &y_tmp);

  if (root_x)
    *root_x = x_tmp;

  if (root_y)
    *root_y = y_tmp;
}

static CdkGrabStatus
cdk_quartz_device_core_grab (CdkDevice    *device,
                             CdkWindow    *window,
                             gboolean      owner_events,
                             CdkEventMask  event_mask,
                             CdkWindow    *confine_to,
                             CdkCursor    *cursor,
                             guint32       time_)
{
  /* Should remain empty */
  return CDK_GRAB_SUCCESS;
}

static void
cdk_quartz_device_core_ungrab (CdkDevice *device,
                               guint32    time_)
{
  CdkDeviceGrabInfo *grab;

  grab = _cdk_display_get_last_device_grab (_cdk_display, device);
  if (grab)
    grab->serial_end = 0;

  _cdk_display_device_grab_update (_cdk_display, device, NULL, 0);
}

static CdkWindow *
cdk_quartz_device_core_window_at_position (CdkDevice       *device,
                                           gdouble         *win_x,
                                           gdouble         *win_y,
                                           CdkModifierType *mask,
                                           gboolean         get_toplevel)
{
  CdkDisplay *display;
  CdkScreen *screen;
  CdkWindow *found_window;
  NSPoint point;
  gint x_tmp, y_tmp;

  display = cdk_device_get_display (device);
  screen = cdk_display_get_default_screen (display);

  /* Get mouse coordinates, find window under the mouse pointer */
  point = [NSEvent mouseLocation];
  _cdk_quartz_window_nspoint_to_cdk_xy (point, &x_tmp, &y_tmp);

  found_window = _cdk_quartz_window_find_child (_cdk_root, x_tmp, y_tmp,
                                                get_toplevel);

  if (found_window)
    translate_coords_to_child_coords (_cdk_root, found_window,
                                      &x_tmp, &y_tmp);

  if (win_x)
    *win_x = found_window ? x_tmp : -1;

  if (win_y)
    *win_y = found_window ? y_tmp : -1;

  if (mask)
    *mask = _cdk_quartz_events_get_current_keyboard_modifiers () |
        _cdk_quartz_events_get_current_mouse_modifiers ();

  return found_window;
}

static void
cdk_quartz_device_core_select_window_events (CdkDevice    *device,
                                             CdkWindow    *window,
                                             CdkEventMask  event_mask)
{
  /* The mask is set in the common code. */
}

void
_cdk_quartz_device_core_set_active (CdkDevice  *device,
                                    gboolean    active,
                                    NSUInteger  device_id)
{
  CdkQuartzDeviceCore *self = CDK_QUARTZ_DEVICE_CORE (device);

  self->active = active;
  self->device_id = device_id;
}

gboolean
_cdk_quartz_device_core_is_active (CdkDevice  *device,
                                   NSUInteger  device_id)
{
  CdkQuartzDeviceCore *self = CDK_QUARTZ_DEVICE_CORE (device);

  return (self->active && self->device_id == device_id);
}

void
_cdk_quartz_device_core_set_unique (CdkDevice          *device,
                                    unsigned long long  unique_id)
{
  CDK_QUARTZ_DEVICE_CORE (device)->unique_id = unique_id;
}

unsigned long long
_cdk_quartz_device_core_get_unique (CdkDevice *device)
{
  return CDK_QUARTZ_DEVICE_CORE (device)->unique_id;
}