gdkevents.c 63.2 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GDK - The GIMP Drawing Kit
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6 7 8 9 10 11
 * 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
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17
 */

18
/*
19
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20 21 22 23 24
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

25
#include "config.h"
26

27
#include "gdkeventsprivate.h"
28
#include "gdkinternals.h"
29
#include "gdkdisplayprivate.h"
30
#include "gdkdndprivate.h"
31
#include "gdk-private.h"
32

33 34
#include <string.h>
#include <math.h>
35

36 37 38 39 40

/**
 * SECTION:events
 * @Short_description: Functions for handling events from the window system
 * @Title: Events
41
 * @See_also: [Event Structures][gdk3-Event-Structures]
42 43 44 45 46 47 48
 *
 * This section describes functions dealing with events from the window
 * system.
 *
 * In GTK+ applications the events are handled automatically in
 * gtk_main_do_event() and passed on to the appropriate widgets, so these
 * functions are rarely needed. Though some of the fields in the
49
 * [Event Structures][gdk3-Event-Structures] are useful.
50 51 52
 */


53
typedef struct _GdkIOClosure GdkIOClosure;
54

55 56
struct _GdkIOClosure
{
57
  GDestroyNotify notify;
58 59 60 61 62 63
  gpointer data;
};

/* Private variable declarations
 */

64 65 66 67 68 69 70
static GdkEventFunc   _gdk_event_func = NULL;    /* Callback for events */
static gpointer       _gdk_event_data = NULL;
static GDestroyNotify _gdk_event_notify = NULL;

void
_gdk_event_emit (GdkEvent *event)
{
71 72 73
  if (gdk_drag_context_handle_source_event (event))
    return;

74 75
  if (_gdk_event_func)
    (*_gdk_event_func) (event, _gdk_event_data);
76 77 78

  if (gdk_drag_context_handle_dest_event (event))
    return;
79
}
80

81 82 83 84
/*********************************************
 * Functions for maintaining the event queue *
 *********************************************/

85
/**
86
 * _gdk_event_queue_find_first:
87 88 89 90 91
 * @display: a #GdkDisplay
 * 
 * Find the first event on the queue that is not still
 * being filled in.
 * 
92 93
 * Returns: (nullable): Pointer to the list node for that event, or
 *   %NULL.
94
 **/
95
GList*
96
_gdk_event_queue_find_first (GdkDisplay *display)
97
{
98 99 100
  GList *tmp_list;
  GList *pending_motion = NULL;

101
  gboolean paused = display->event_pause_count > 0;
102

103
  tmp_list = display->queued_events;
104 105
  while (tmp_list)
    {
106
      GdkEventPrivate *event = tmp_list->data;
107

108 109
      if ((event->flags & GDK_EVENT_PENDING) == 0 &&
	  (!paused || (event->flags & GDK_EVENT_FLUSHED) != 0))
110 111 112 113
        {
          if (pending_motion)
            return pending_motion;

114
          if (event->event.any.type == GDK_MOTION_NOTIFY && (event->flags & GDK_EVENT_FLUSHED) == 0)
115 116 117 118
            pending_motion = tmp_list;
          else
            return tmp_list;
        }
119

120
      tmp_list = tmp_list->next;
121 122 123 124 125
    }

  return NULL;
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
/**
 * _gdk_event_queue_append:
 * @display: a #GdkDisplay
 * @event: Event to append.
 * 
 * Appends an event onto the tail of the event queue.
 *
 * Returns: the newly appended list node.
 **/
GList *
_gdk_event_queue_append (GdkDisplay *display,
			 GdkEvent   *event)
{
  display->queued_tail = g_list_append (display->queued_tail, event);
  
  if (!display->queued_events)
    display->queued_events = display->queued_tail;
  else
    display->queued_tail = display->queued_tail->next;

  return display->queued_tail;
}
148

149 150 151 152 153 154
/**
 * _gdk_event_queue_insert_after:
 * @display: a #GdkDisplay
 * @sibling: Append after this event.
 * @event: Event to append.
 *
155
 * Appends an event after the specified event, or if it isn’t in
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
 * the queue, onto the tail of the event queue.
 *
 * Returns: the newly appended list node.
 *
 * Since: 2.16
 */
GList*
_gdk_event_queue_insert_after (GdkDisplay *display,
                               GdkEvent   *sibling,
                               GdkEvent   *event)
{
  GList *prev = g_list_find (display->queued_events, sibling);
  if (prev && prev->next)
    {
      display->queued_events = g_list_insert_before (display->queued_events, prev->next, event);
      return prev->next;
    }
  else
    return _gdk_event_queue_append (display, event);
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
178
 * _gdk_event_queue_insert_before:
179
 * @display: a #GdkDisplay
Matthias Clasen's avatar
Matthias Clasen committed
180 181
 * @sibling: Append before this event
 * @event: Event to prepend
182
 *
183
 * Prepends an event before the specified event, or if it isn’t in
Matthias Clasen's avatar
Matthias Clasen committed
184
 * the queue, onto the head of the event queue.
185
 *
Matthias Clasen's avatar
Matthias Clasen committed
186
 * Returns: the newly prepended list node.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
 *
 * Since: 2.16
 */
GList*
_gdk_event_queue_insert_before (GdkDisplay *display,
				GdkEvent   *sibling,
				GdkEvent   *event)
{
  GList *next = g_list_find (display->queued_events, sibling);
  if (next)
    {
      display->queued_events = g_list_insert_before (display->queued_events, next, event);
      return next->prev;
    }
  else
    return _gdk_event_queue_append (display, event);
}


206 207 208 209 210 211 212
/**
 * _gdk_event_queue_remove_link:
 * @display: a #GdkDisplay
 * @node: node to remove
 * 
 * Removes a specified list node from the event queue.
 **/
213
void
214 215
_gdk_event_queue_remove_link (GdkDisplay *display,
			      GList      *node)
216 217 218 219
{
  if (node->prev)
    node->prev->next = node->next;
  else
220
    display->queued_events = node->next;
221 222 223 224
  
  if (node->next)
    node->next->prev = node->prev;
  else
225
    display->queued_tail = node->prev;
226 227
}

228 229 230 231 232 233 234
/**
 * _gdk_event_unqueue:
 * @display: a #GdkDisplay
 * 
 * Removes and returns the first event from the event
 * queue that is not still being filled in.
 * 
235
 * Returns: (nullable): the event, or %NULL. Ownership is transferred
236 237 238 239
 * to the caller.
 **/
GdkEvent*
_gdk_event_unqueue (GdkDisplay *display)
240
{
241 242 243 244 245 246 247 248 249 250 251 252 253
  GdkEvent *event = NULL;
  GList *tmp_list;

  tmp_list = _gdk_event_queue_find_first (display);

  if (tmp_list)
    {
      event = tmp_list->data;
      _gdk_event_queue_remove_link (display, tmp_list);
      g_list_free_1 (tmp_list);
    }

  return event;
254 255
}

256 257 258 259 260 261
void
_gdk_event_queue_handle_motion_compression (GdkDisplay *display)
{
  GList *tmp_list;
  GList *pending_motions = NULL;
  GdkWindow *pending_motion_window = NULL;
262
  GdkDevice *pending_motion_device = NULL;
263 264 265 266 267 268 269 270 271 272 273 274 275

  /* If the last N events in the event queue are motion notify
   * events for the same window, drop all but the last */

  tmp_list = display->queued_tail;

  while (tmp_list)
    {
      GdkEventPrivate *event = tmp_list->data;

      if (event->flags & GDK_EVENT_PENDING)
        break;

276
      if (event->event.any.type != GDK_MOTION_NOTIFY)
277 278 279
        break;

      if (pending_motion_window != NULL &&
280
          pending_motion_window != event->event.any.window)
281 282
        break;

283 284 285 286
      if (pending_motion_device != NULL &&
          pending_motion_device != event->event.motion.device)
        break;

287
      if (!event->event.any.window->event_compression)
288 289
        break;

290
      pending_motion_window = event->event.any.window;
291
      pending_motion_device = event->event.motion.device;
292 293 294 295 296 297 298 299
      pending_motions = tmp_list;

      tmp_list = tmp_list->prev;
    }

  while (pending_motions && pending_motions->next != NULL)
    {
      GList *next = pending_motions->next;
300
      gdk_event_free (pending_motions->data);
301 302 303 304 305 306 307 308 309 310
      display->queued_events = g_list_delete_link (display->queued_events,
                                                   pending_motions);
      pending_motions = next;
    }

  if (pending_motions &&
      pending_motions == display->queued_events &&
      pending_motions == display->queued_tail)
    {
      GdkFrameClock *clock = gdk_window_get_frame_clock (pending_motion_window);
311 312
      if (clock) /* might be NULL if window was destroyed */
	gdk_frame_clock_request_phase (clock, GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS);
313 314 315
    }
}

316 317 318 319 320 321 322 323 324 325 326 327
void
_gdk_event_queue_flush (GdkDisplay *display)
{
  GList *tmp_list;

  for (tmp_list = display->queued_events; tmp_list; tmp_list = tmp_list->next)
    {
      GdkEventPrivate *event = tmp_list->data;
      event->flags |= GDK_EVENT_FLUSHED;
    }
}

328
/**
329
 * gdk_event_handler_set:
330 331 332 333
 * @func: the function to call to handle events from GDK.
 * @data: user data to pass to the function. 
 * @notify: the function to call when the handler function is removed, i.e. when
 *          gdk_event_handler_set() is called with another event handler.
334
 * 
335 336 337 338 339 340 341
 * Sets the function to call to handle all events from GDK.
 *
 * Note that GTK+ uses this to install its own event handler, so it is
 * usually not useful for GTK+ applications. (Although an application
 * can call this function then call gtk_main_do_event() to pass
 * events to GTK+.)
 **/
342 343 344 345 346
void 
gdk_event_handler_set (GdkEventFunc   func,
		       gpointer       data,
		       GDestroyNotify notify)
{
347 348
  if (_gdk_event_notify)
    (*_gdk_event_notify) (_gdk_event_data);
349

350 351 352
  _gdk_event_func = func;
  _gdk_event_data = data;
  _gdk_event_notify = notify;
353 354
}

355
static GHashTable *event_hash = NULL;
356

Matthias Clasen's avatar
Matthias Clasen committed
357 358 359 360 361 362
/**
 * gdk_event_new:
 * @type: a #GdkEventType 
 * 
 * Creates a new event of the given type. All fields are set to 0.
 * 
363
 * Returns: a newly-allocated #GdkEvent. The returned #GdkEvent 
Matthias Clasen's avatar
Matthias Clasen committed
364
 * should be freed with gdk_event_free().
365 366
 *
 * Since: 2.2
Matthias Clasen's avatar
Matthias Clasen committed
367
 **/
368
GdkEvent*
369
gdk_event_new (GdkEventType type)
370
{
371 372
  GdkEventPrivate *new_private;
  GdkEvent *new_event;
373
  
374 375 376
  if (!event_hash)
    event_hash = g_hash_table_new (g_direct_hash, NULL);

377
  new_private = g_slice_new0 (GdkEventPrivate);
378 379
  
  new_private->flags = 0;
380
  new_private->display = NULL;
381 382 383 384 385 386

  g_hash_table_insert (event_hash, new_private, GUINT_TO_POINTER (1));

  new_event = (GdkEvent *) new_private;

  new_event->any.type = type;
387

388 389 390 391 392 393
  /*
   * Bytewise 0 initialization is reasonable for most of the 
   * current event types. Explicitely initialize double fields
   * since I trust bytewise 0 == 0. less than for integers
   * or pointers.
   */
394
  switch ((guint) type)
395 396 397 398 399 400 401 402 403 404 405 406 407 408
    {
    case GDK_MOTION_NOTIFY:
      new_event->motion.x = 0.;
      new_event->motion.y = 0.;
      new_event->motion.x_root = 0.;
      new_event->motion.y_root = 0.;
      break;
    case GDK_BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
      new_event->button.x = 0.;
      new_event->button.y = 0.;
      new_event->button.x_root = 0.;
      new_event->button.y_root = 0.;
      break;
409 410 411 412 413 414 415 416 417
    case GDK_TOUCH_BEGIN:
    case GDK_TOUCH_UPDATE:
    case GDK_TOUCH_END:
    case GDK_TOUCH_CANCEL:
      new_event->touch.x = 0.;
      new_event->touch.y = 0.;
      new_event->touch.x_root = 0.;
      new_event->touch.y_root = 0.;
      break;
418 419 420 421 422
    case GDK_SCROLL:
      new_event->scroll.x = 0.;
      new_event->scroll.y = 0.;
      new_event->scroll.x_root = 0.;
      new_event->scroll.y_root = 0.;
423 424
      new_event->scroll.delta_x = 0.;
      new_event->scroll.delta_y = 0.;
425
      new_event->scroll.is_stop = FALSE;
426 427 428 429 430 431 432 433
      break;
    case GDK_ENTER_NOTIFY:
    case GDK_LEAVE_NOTIFY:
      new_event->crossing.x = 0.;
      new_event->crossing.y = 0.;
      new_event->crossing.x_root = 0.;
      new_event->crossing.y_root = 0.;
      break;
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
    case GDK_TOUCHPAD_SWIPE:
      new_event->touchpad_swipe.x = 0;
      new_event->touchpad_swipe.y = 0;
      new_event->touchpad_swipe.dx = 0;
      new_event->touchpad_swipe.dy = 0;
      new_event->touchpad_swipe.x_root = 0;
      new_event->touchpad_swipe.y_root = 0;
      break;
    case GDK_TOUCHPAD_PINCH:
      new_event->touchpad_pinch.x = 0;
      new_event->touchpad_pinch.y = 0;
      new_event->touchpad_pinch.dx = 0;
      new_event->touchpad_pinch.dy = 0;
      new_event->touchpad_pinch.angle_delta = 0;
      new_event->touchpad_pinch.scale = 0;
      new_event->touchpad_pinch.x_root = 0;
      new_event->touchpad_pinch.y_root = 0;
      break;
452 453 454
    default:
      break;
    }
455
  
456
  return new_event;
457 458
}

459
static gboolean
460
gdk_event_is_allocated (const GdkEvent *event)
461 462 463
{
  if (event_hash)
    return g_hash_table_lookup (event_hash, event) != NULL;
464 465

  return FALSE;
466
}
467 468

void
469 470
gdk_event_set_pointer_emulated (GdkEvent *event,
                                gboolean  emulated)
471 472 473 474 475 476 477 478 479 480 481 482
{
  if (gdk_event_is_allocated (event))
    {
      GdkEventPrivate *private = (GdkEventPrivate *) event;

      if (emulated)
        private->flags |= GDK_EVENT_POINTER_EMULATED;
      else
        private->flags &= ~(GDK_EVENT_POINTER_EMULATED);
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
483 484 485 486 487 488 489 490 491 492 493
/**
 * gdk_event_get_pointer_emulated:
 * #event: a #GdkEvent
 *
 * Returns whether this event is an 'emulated' pointer event (typically
 * from a touch event), as opposed to a real one.
 *
 * Returns: %TRUE if this event is emulated
 *
 * Since: 3.22
 */
494
gboolean
495
gdk_event_get_pointer_emulated (GdkEvent *event)
496 497 498 499 500 501 502
{
  if (gdk_event_is_allocated (event))
    return (((GdkEventPrivate *) event)->flags & GDK_EVENT_POINTER_EMULATED) != 0;

  return FALSE;
}

503 504 505 506 507
/**
 * gdk_event_copy:
 * @event: a #GdkEvent
 * 
 * Copies a #GdkEvent, copying or incrementing the reference count of the
508
 * resources associated with it (e.g. #GdkWindow’s and strings).
509
 * 
510
 * Returns: a copy of @event. The returned #GdkEvent should be freed with
511 512
 * gdk_event_free().
 **/
513
GdkEvent*
514
gdk_event_copy (const GdkEvent *event)
515
{
516
  GdkEventPrivate *new_private;
517
  GdkEvent *new_event;
518

519
  g_return_val_if_fail (event != NULL, NULL);
520

521 522 523
  new_event = gdk_event_new (GDK_NOTHING);
  new_private = (GdkEventPrivate *)new_event;

524
  *new_event = *event;
525
  if (new_event->any.window)
526
    g_object_ref (new_event->any.window);
527

528 529 530 531
  if (gdk_event_is_allocated (event))
    {
      GdkEventPrivate *private = (GdkEventPrivate *)event;

532
      new_private->display = private->display;
533 534
      new_private->device = private->device ? g_object_ref (private->device) : NULL;
      new_private->source_device = private->source_device ? g_object_ref (private->source_device) : NULL;
535
      new_private->seat = private->seat;
536
      new_private->tool = private->tool;
537
      g_set_object (&new_private->user_data, private->user_data);
538
    }
539

540
  switch ((guint) event->any.type)
541 542 543 544 545
    {
    case GDK_KEY_PRESS:
    case GDK_KEY_RELEASE:
      new_event->key.string = g_strdup (event->key.string);
      break;
546

547 548 549
    case GDK_ENTER_NOTIFY:
    case GDK_LEAVE_NOTIFY:
      if (event->crossing.subwindow != NULL)
550
        g_object_ref (event->crossing.subwindow);
551
      break;
552

553 554 555 556 557 558
    case GDK_DRAG_ENTER:
    case GDK_DRAG_LEAVE:
    case GDK_DRAG_MOTION:
    case GDK_DRAG_STATUS:
    case GDK_DROP_START:
    case GDK_DROP_FINISHED:
559
      g_object_ref (event->dnd.context);
560
      break;
561

562
    case GDK_EXPOSE:
563
    case GDK_DAMAGE:
564
      if (event->expose.region)
565
        new_event->expose.region = cairo_region_copy (event->expose.region);
566
      break;
567

Larry Ewing's avatar
Larry Ewing committed
568 569
    case GDK_BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
570 571
      if (event->button.axes)
        new_event->button.axes = g_memdup (event->button.axes,
572
                                           sizeof (gdouble) * gdk_device_get_n_axes (event->button.device));
Larry Ewing's avatar
Larry Ewing committed
573 574
      break;

575 576 577 578 579 580 581 582 583
    case GDK_TOUCH_BEGIN:
    case GDK_TOUCH_UPDATE:
    case GDK_TOUCH_END:
    case GDK_TOUCH_CANCEL:
      if (event->touch.axes)
        new_event->touch.axes = g_memdup (event->touch.axes,
                                           sizeof (gdouble) * gdk_device_get_n_axes (event->touch.device));
      break;

Larry Ewing's avatar
Larry Ewing committed
584
    case GDK_MOTION_NOTIFY:
585 586 587
      if (event->motion.axes)
        new_event->motion.axes = g_memdup (event->motion.axes,
                                           sizeof (gdouble) * gdk_device_get_n_axes (event->motion.device));
Larry Ewing's avatar
Larry Ewing committed
588
      break;
589

590
    default:
591 592
      break;
    }
593 594

  if (gdk_event_is_allocated (event))
595
    _gdk_display_event_data_copy (gdk_event_get_display (event), event, new_event);
596

597 598 599
  return new_event;
}

600 601 602 603 604 605
/**
 * gdk_event_free:
 * @event:  a #GdkEvent.
 * 
 * Frees a #GdkEvent, freeing or decrementing any resources associated with it.
 * Note that this function should only be called with events returned from
606 607
 * functions such as gdk_event_peek(), gdk_event_get(), gdk_event_copy()
 * and gdk_event_new().
608
 **/
609 610 611
void
gdk_event_free (GdkEvent *event)
{
612
  GdkEventPrivate *private;
613 614
  GdkDisplay *display;

615
  g_return_if_fail (event != NULL);
616

617 618 619 620 621
  if (gdk_event_is_allocated (event))
    {
      private = (GdkEventPrivate *) event;
      g_clear_object (&private->device);
      g_clear_object (&private->source_device);
622
      g_clear_object (&private->user_data);
623 624
    }

625
  switch ((guint) event->any.type)
626 627 628 629 630 631 632 633 634
    {
    case GDK_KEY_PRESS:
    case GDK_KEY_RELEASE:
      g_free (event->key.string);
      break;
      
    case GDK_ENTER_NOTIFY:
    case GDK_LEAVE_NOTIFY:
      if (event->crossing.subwindow != NULL)
635
	g_object_unref (event->crossing.subwindow);
636 637 638 639 640 641 642 643
      break;
      
    case GDK_DRAG_ENTER:
    case GDK_DRAG_LEAVE:
    case GDK_DRAG_MOTION:
    case GDK_DRAG_STATUS:
    case GDK_DROP_START:
    case GDK_DROP_FINISHED:
644 645
      if (event->dnd.context != NULL)
        g_object_unref (event->dnd.context);
646
      break;
647 648 649

    case GDK_BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
650
      g_free (event->button.axes);
651
      break;
652 653 654 655 656 657 658 659

    case GDK_TOUCH_BEGIN:
    case GDK_TOUCH_UPDATE:
    case GDK_TOUCH_END:
    case GDK_TOUCH_CANCEL:
      g_free (event->touch.axes);
      break;

660
    case GDK_EXPOSE:
661
    case GDK_DAMAGE:
662
      if (event->expose.region)
Benjamin Otte's avatar
Benjamin Otte committed
663
	cairo_region_destroy (event->expose.region);
664 665
      break;
      
666
    case GDK_MOTION_NOTIFY:
667
      g_free (event->motion.axes);
668
      break;
669

670 671 672
    default:
      break;
    }
673

674
  display = gdk_event_get_display (event);
675 676
  if (display)
    _gdk_display_event_data_free (display, event);
677

678 679 680
  if (event->any.window)
    g_object_unref (event->any.window);

681
  g_hash_table_remove (event_hash, event);
682
  g_slice_free (GdkEventPrivate, (GdkEventPrivate*) event);
683 684
}

685 686 687 688 689 690
/**
 * gdk_event_get_window:
 * @event: a #GdkEvent
 *
 * Extracts the #GdkWindow associated with an event.
 *
691
 * Returns: (transfer none): The #GdkWindow associated with the event
692 693 694 695 696 697 698 699 700 701 702
 *
 * Since: 3.10
 */
GdkWindow *
gdk_event_get_window (const GdkEvent *event)
{
  g_return_val_if_fail (event != NULL, NULL);

  return event->any.window;
}

703
/**
704
 * gdk_event_get_time:
705 706 707 708 709
 * @event: a #GdkEvent
 * 
 * Returns the time stamp from @event, if there is one; otherwise
 * returns #GDK_CURRENT_TIME. If @event is %NULL, returns #GDK_CURRENT_TIME.
 * 
710
 * Returns: time stamp field from @event
711
 **/
712
guint32
713
gdk_event_get_time (const GdkEvent *event)
714 715
{
  if (event)
716
    switch (event->any.type)
717 718 719 720 721 722
      {
      case GDK_MOTION_NOTIFY:
	return event->motion.time;
      case GDK_BUTTON_PRESS:
      case GDK_BUTTON_RELEASE:
	return event->button.time;
723 724 725 726 727
      case GDK_TOUCH_BEGIN:
      case GDK_TOUCH_UPDATE:
      case GDK_TOUCH_END:
      case GDK_TOUCH_CANCEL:
        return event->touch.time;
728 729 730 731
      case GDK_TOUCHPAD_SWIPE:
        return event->touchpad_swipe.time;
      case GDK_TOUCHPAD_PINCH:
        return event->touchpad_pinch.time;
732 733
      case GDK_SCROLL:
        return event->scroll.time;
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
      case GDK_KEY_PRESS:
      case GDK_KEY_RELEASE:
	return event->key.time;
      case GDK_ENTER_NOTIFY:
      case GDK_LEAVE_NOTIFY:
	return event->crossing.time;
      case GDK_PROPERTY_NOTIFY:
	return event->property.time;
      case GDK_PROXIMITY_IN:
      case GDK_PROXIMITY_OUT:
	return event->proximity.time;
      case GDK_DRAG_ENTER:
      case GDK_DRAG_LEAVE:
      case GDK_DRAG_MOTION:
      case GDK_DRAG_STATUS:
      case GDK_DROP_START:
      case GDK_DROP_FINISHED:
	return event->dnd.time;
752 753 754 755 756 757 758 759
      case GDK_PAD_BUTTON_PRESS:
      case GDK_PAD_BUTTON_RELEASE:
        return event->pad_button.time;
      case GDK_PAD_RING:
      case GDK_PAD_STRIP:
        return event->pad_axis.time;
      case GDK_PAD_GROUP_MODE:
        return event->pad_group_mode.time;
760 761 762 763
      case GDK_CLIENT_EVENT:
      case GDK_CONFIGURE:
      case GDK_FOCUS_CHANGE:
      case GDK_NOTHING:
764
      case GDK_DAMAGE:
765 766 767 768 769
      case GDK_DELETE:
      case GDK_DESTROY:
      case GDK_EXPOSE:
      case GDK_MAP:
      case GDK_UNMAP:
770
      case GDK_WINDOW_STATE:
771
      case GDK_GRAB_BROKEN:
772
      case GDK_EVENT_LAST:
773
      default:
774 775
        /* return current time */
        break;
776 777 778 779 780
      }
  
  return GDK_CURRENT_TIME;
}

781 782
/**
 * gdk_event_get_state:
783
 * @event: (allow-none): a #GdkEvent or %NULL
784
 * @state: (out): return location for state
785
 * 
786
 * If the event contains a “state” field, puts that field in @state. Otherwise
787
 * stores an empty state (0). Returns %TRUE if there was a state field
788
 * in the event. @event may be %NULL, in which case it’s treated
789
 * as if the event had no state field.
790
 * 
791
 * Returns: %TRUE if there was a state field in the event 
792 793
 **/
gboolean
794 795
gdk_event_get_state (const GdkEvent        *event,
                     GdkModifierType       *state)
796
{
797 798
  g_return_val_if_fail (state != NULL, FALSE);
  
799
  if (event)
800
    switch (event->any.type)
801 802
      {
      case GDK_MOTION_NOTIFY:
803 804
	*state = event->motion.state;
        return TRUE;
805 806
      case GDK_BUTTON_PRESS:
      case GDK_BUTTON_RELEASE:
807 808 809 810 811 812 813
        *state = event->button.state;
        return TRUE;
      case GDK_TOUCH_BEGIN:
      case GDK_TOUCH_UPDATE:
      case GDK_TOUCH_END:
      case GDK_TOUCH_CANCEL:
        *state = event->touch.state;
814
        return TRUE;
815 816 817 818 819 820
      case GDK_TOUCHPAD_SWIPE:
        *state = event->touchpad_swipe.state;
        return TRUE;
      case GDK_TOUCHPAD_PINCH:
        *state = event->touchpad_pinch.state;
        return TRUE;
821
      case GDK_SCROLL:
822 823
	*state =  event->scroll.state;
        return TRUE;
824 825
      case GDK_KEY_PRESS:
      case GDK_KEY_RELEASE:
826 827
	*state =  event->key.state;
        return TRUE;
828 829
      case GDK_ENTER_NOTIFY:
      case GDK_LEAVE_NOTIFY:
830 831
	*state =  event->crossing.state;
        return TRUE;
832 833 834 835 836 837
      case GDK_PROPERTY_NOTIFY:
      case GDK_CLIENT_EVENT:
      case GDK_CONFIGURE:
      case GDK_FOCUS_CHANGE:
      case GDK_PROXIMITY_IN:
      case GDK_PROXIMITY_OUT:
838
      case GDK_DAMAGE:
839 840 841 842 843 844 845 846 847 848 849 850
      case GDK_DRAG_ENTER:
      case GDK_DRAG_LEAVE:
      case GDK_DRAG_MOTION:
      case GDK_DRAG_STATUS:
      case GDK_DROP_START:
      case GDK_DROP_FINISHED:
      case GDK_NOTHING:
      case GDK_DELETE:
      case GDK_DESTROY:
      case GDK_EXPOSE:
      case GDK_MAP:
      case GDK_UNMAP:
851
      case GDK_WINDOW_STATE:
852
      case GDK_GRAB_BROKEN:
853 854 855 856 857
      case GDK_PAD_BUTTON_PRESS:
      case GDK_PAD_BUTTON_RELEASE:
      case GDK_PAD_RING:
      case GDK_PAD_STRIP:
      case GDK_PAD_GROUP_MODE:
858
      case GDK_EVENT_LAST:
859
      default:
860 861 862 863
        /* no state field */
        break;
      }

864 865
  *state = 0;
  return FALSE;
866 867
}

868 869 870
/**
 * gdk_event_get_coords:
 * @event: a #GdkEvent
871 872
 * @x_win: (out) (optional): location to put event window x coordinate
 * @y_win: (out) (optional): location to put event window y coordinate
873 874 875
 * 
 * Extract the event window relative x/y coordinates from an event.
 * 
876
 * Returns: %TRUE if the event delivered event window coordinates
877 878
 **/
gboolean
879 880 881
gdk_event_get_coords (const GdkEvent *event,
		      gdouble        *x_win,
		      gdouble        *y_win)
882 883 884 885 886 887
{
  gdouble x = 0, y = 0;
  gboolean fetched = TRUE;
  
  g_return_val_if_fail (event != NULL, FALSE);

888
  switch ((guint) event->any.type)
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
    {
    case GDK_CONFIGURE:
      x = event->configure.x;
      y = event->configure.y;
      break;
    case GDK_ENTER_NOTIFY:
    case GDK_LEAVE_NOTIFY:
      x = event->crossing.x;
      y = event->crossing.y;
      break;
    case GDK_SCROLL:
      x = event->scroll.x;
      y = event->scroll.y;
      break;
    case GDK_BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
      x = event->button.x;
      y = event->button.y;
      break;
908 909 910 911 912 913 914
    case GDK_TOUCH_BEGIN:
    case GDK_TOUCH_UPDATE:
    case GDK_TOUCH_END:
    case GDK_TOUCH_CANCEL:
      x = event->touch.x;
      y = event->touch.y;
      break;
915 916 917 918
    case GDK_MOTION_NOTIFY:
      x = event->motion.x;
      y = event->motion.y;
      break;
919 920 921 922 923 924 925 926
    case GDK_TOUCHPAD_SWIPE:
      x = event->touchpad_swipe.x;
      y = event->touchpad_swipe.y;
      break;
    case GDK_TOUCHPAD_PINCH:
      x = event->touchpad_pinch.x;
      y = event->touchpad_pinch.y;
      break;
927 928 929 930 931 932 933 934
    default:
      fetched = FALSE;
      break;
    }

  if (x_win)
    *x_win = x;
  if (y_win)
935
    *y_win = y;
936 937 938 939 940 941 942

  return fetched;
}

/**
 * gdk_event_get_root_coords:
 * @event: a #GdkEvent
943 944
 * @x_root: (out) (optional): location to put root window x coordinate
 * @y_root: (out) (optional): location to put root window y coordinate
945 946 947
 * 
 * Extract the root window relative x/y coordinates from an event.
 * 
948
 * Returns: %TRUE if the event delivered root window coordinates
949 950
 **/
gboolean
951 952 953
gdk_event_get_root_coords (const GdkEvent *event,
			   gdouble        *x_root,
			   gdouble        *y_root)
954 955 956 957 958 959
{
  gdouble x = 0, y = 0;
  gboolean fetched = TRUE;
  
  g_return_val_if_fail (event != NULL, FALSE);

960
  switch ((guint) event->any.type)
961 962 963 964 965
    {
    case GDK_MOTION_NOTIFY:
      x = event->motion.x_root;
      y = event->motion.y_root;
      break;
966 967 968 969
    case GDK_SCROLL:
      x = event->scroll.x_root;
      y = event->scroll.y_root;
      break;
970 971 972 973 974
    case GDK_BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
      x = event->button.x_root;
      y = event->button.y_root;
      break;
975 976 977 978 979 980 981
    case GDK_TOUCH_BEGIN:
    case GDK_TOUCH_UPDATE:
    case GDK_TOUCH_END:
    case GDK_TOUCH_CANCEL:
      x = event->touch.x_root;
      y = event->touch.y_root;
      break;
982 983 984 985 986 987 988 989 990 991 992 993 994 995
    case GDK_ENTER_NOTIFY:
    case GDK_LEAVE_NOTIFY:
      x = event->crossing.x_root;
      y = event->crossing.y_root;
      break;
    case GDK_DRAG_ENTER:
    case GDK_DRAG_LEAVE:
    case GDK_DRAG_MOTION:
    case GDK_DRAG_STATUS:
    case GDK_DROP_START:
    case GDK_DROP_FINISHED:
      x = event->dnd.x_root;
      y = event->dnd.y_root;
      break;
996 997 998 999 1000 1001 1002 1003
    case GDK_TOUCHPAD_SWIPE:
      x = event->touchpad_swipe.x_root;
      y = event->touchpad_swipe.y_root;
      break;
    case GDK_TOUCHPAD_PINCH:
      x = event->touchpad_pinch.x_root;
      y = event->touchpad_pinch.y_root;
      break;
1004 1005 1006 1007 1008 1009 1010 1011
    default:
      fetched = FALSE;
      break;
    }

  if (x_root)
    *x_root = x;
  if (y_root)
1012
    *y_root = y;
1013 1014 1015 1016

  return fetched;
}

1017 1018 1019 1020 1021 1022 1023
void
gdk_event_set_coords (GdkEvent *event,
                      gdouble   x,
                      gdouble   y)
{
  g_return_if_fail (event != NULL);

1024
  switch ((guint) event->any.type)
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
    {
    case GDK_CONFIGURE:
      event->configure.x = x;
      event->configure.y = y;
      break;
    case GDK_ENTER_NOTIFY:
    case GDK_LEAVE_NOTIFY:
      event->crossing.x = x;
      event->crossing.y = y;
      break;
    case GDK_SCROLL:
      event->scroll.x = x;
      event->scroll.y = y;
      break;
    case GDK_BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
      event->button.x = x;
      event->button.y = y;
      break;
    case GDK_TOUCH_BEGIN:
    case GDK_TOUCH_UPDATE:
    case GDK_TOUCH_END:
    case GDK_TOUCH_CANCEL:
      event->touch.x = x;
      event->touch.y = y;
      break;
    case GDK_MOTION_NOTIFY:
      event->motion.x = x;
      event->motion.y = y;
      break;
    case GDK_TOUCHPAD_SWIPE:
      event->touchpad_swipe.x = x;
      event->touchpad_swipe.y = y;
      break;
    case GDK_TOUCHPAD_PINCH:
      event->touchpad_pinch.x = x;
      event->touchpad_pinch.y = y;
      break;
    default:
      break;
    }
}

1068 1069 1070 1071 1072 1073 1074
/**
 * gdk_event_get_button:
 * @event: a #GdkEvent
 * @button: (out): location to store mouse button number
 *
 * Extract the button number from an event.
 *
1075
 * Returns: %TRUE if the event delivered a button number
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
 *
 * Since: 3.2
 **/
gboolean
gdk_event_get_button (const GdkEvent *event,
                      guint *button)
{
  gboolean fetched = TRUE;
  guint number = 0;

  g_return_val_if_fail (event != NULL, FALSE);
  
1088
  switch ((guint) event->any.type)
1089 1090 1091 1092 1093
    {
    case GDK_BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
      number = event->button.button;
      break;
1094 1095 1096 1097
    case GDK_PAD_BUTTON_PRESS:
    case GDK_PAD_BUTTON_RELEASE:
      number = event->pad_button.button;
      break;
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
    default:
      fetched = FALSE;
      break;
    }

  if (button)
    *button = number;

  return fetched;
}

/**
 * gdk_event_get_click_count:
 * @event: a #GdkEvent
 * @click_count: (out): location to store click count
 *
 * Extracts the click count from an event.
 *
1116
 * Returns: %TRUE if the event delivered a click count
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
 *
 * Since: 3.2
 */
gboolean
gdk_event_get_click_count (const GdkEvent *event,
                           guint *click_count)
{
  gboolean fetched = TRUE;
  guint number = 0;

  g_return_val_if_fail (event != NULL, FALSE);

1129
  switch ((guint) event->any.type)
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
    {
    case GDK_BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
      number = 1;
      break;
    default:
      fetched = FALSE;
      break;
    }

  if (click_count)
    *click_count = number;

  return fetched;
}

/**
 * gdk_event_get_keyval:
 * @event: a #GdkEvent
 * @keyval: (out): location to store the keyval
 *
 * Extracts the keyval from an event.
 *
1153
 * Returns: %TRUE if the event delivered a key symbol
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
 *
 * Since: 3.2
 */
gboolean
gdk_event_get_keyval (const GdkEvent *event,
                      guint *keyval)
{
  gboolean fetched = TRUE;
  guint number = 0;

1164
  switch ((guint) event->any.type)
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
    {
    case GDK_KEY_PRESS:
    case GDK_KEY_RELEASE:
      number = event->key.keyval;
      break;
    default:
      fetched = FALSE;
      break;
    }

  if (keyval)
    *keyval = number;

  return fetched;
}

Matthias Clasen's avatar
Matthias Clasen committed
1181 1182 1183 1184
void
gdk_event_set_keyval (GdkEvent *event,
                      guint     keyval)
{
1185 1186
  if (event->any.type == GDK_KEY_PRESS ||
      event->any.type == GDK_KEY_RELEASE)
Matthias Clasen's avatar
Matthias Clasen committed
1187 1188 1189
    event->key.keyval = keyval;
}

1190 1191 1192 1193 1194 1195 1196
/**
 * gdk_event_get_keycode:
 * @event: a #GdkEvent
 * @keycode: (out): location to store the keycode
 *
 * Extracts the hardware keycode from an event.
 *
Matthias Clasen's avatar
Matthias Clasen committed
1197 1198
 * Also see gdk_event_get_scancode().
 *
1199
 * Returns: %TRUE if the event delivered a hardware keycode
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
 *
 * Since: 3.2
 */
gboolean
gdk_event_get_keycode (const GdkEvent *event,
                       guint16 *keycode)
{
  gboolean fetched = TRUE;
  guint16 number = 0;

1210
  switch ((guint) event->any.type)
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
    {
    case GDK_KEY_PRESS:
    case GDK_KEY_RELEASE:
      number = event->key.hardware_keycode;
      break;
    default:
      fetched = FALSE;
      break;
    }

  if (keycode)
    *keycode = number;

  return fetched;
}

1227 1228 1229 1230 1231 1232 1233
/**
 * gdk_event_get_key_group:
 * @event: a #GdkEvent
 * @group: (out):
 * 
 * Returns: %TRUE on success, otherwise %FALSE
 **/
1234 1235 1236 1237 1238 1239
gboolean
gdk_event_get_key_group (const GdkEvent *event,
                         guint          *group)
{
  gboolean fetched = TRUE;

1240
  switch ((guint) event->any.type)
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
    {
    case GDK_KEY_PRESS:
    case GDK_KEY_RELEASE:
      *group = event->key.group;
      break;
    default:
      *group = 0;
      fetched = FALSE;
      break;
    }

  return fetched;
}

1255 1256 1257 1258 1259 1260 1261
/**
 * gdk_event_get_string:
 * @event: a #GdkEvent
 * @string: (out) (transfer none):
 * 
 * Returns: %TRUE on success, otherwise %FALSE
 **/
1262 1263 1264 1265 1266 1267
gboolean
gdk_event_get_string (const GdkEvent  *event,
                      const char     **string)
{
  gboolean fetched = TRUE;

1268
  switch ((guint) event->any.type)
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
    {
    case GDK_KEY_PRESS:
    case GDK_KEY_RELEASE:
      *string = event->key.string;
      break;
    default:
      *string = NULL;
      fetched = FALSE;
      break;
    }

  return fetched;
}

1283 1284 1285 1286 1287 1288 1289
/**
 * gdk_event_get_key_is_modifier:
 * @event: a #GdkEvent
 * @is_modifier: (out):
 * 
 * Returns: %TRUE on success, otherwise %FALSE
 **/
1290 1291 1292 1293 1294 1295
gboolean
gdk_event_get_key_is_modifier (const GdkEvent *event,
                               gboolean       *is_modifier)
{
  gboolean fetched = TRUE;

1296
  switch ((guint) event->any.type)
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
    {
    case GDK_KEY_PRESS:
    case GDK_KEY_RELEASE:
      *is_modifier = event->key.is_modifier;
      break;
    default:
      *is_modifier = FALSE;
      fetched = FALSE;
      break;
    }

  return fetched;
}

1311 1312 1313 1314 1315 1316 1317
/**
 * gdk_event_get_scroll_direction:
 * @event: a #GdkEvent
 * @direction: (out): location to store the scroll direction
 *
 * Extracts the scroll direction from an event.
 *
1318
 * Returns: %TRUE if the event delivered a scroll direction
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
 *
 * Since: 3.2
 */
gboolean
gdk_event_get_scroll_direction (const GdkEvent *event,
                                GdkScrollDirection *direction)
{
  gboolean fetched = TRUE;
  GdkScrollDirection dir = 0;

1329
  switch ((guint) event->any.type)
1330 1331
    {
    case GDK_SCROLL:
1332 1333 1334 1335
      if (event->scroll.direction == GDK_SCROLL_SMOOTH)
        fetched = FALSE;
      else
        dir = event->scroll.direction;
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
      break;
    default:
      fetched = FALSE;
      break;
    }

  if (direction)
    *direction = dir;

  return fetched;
}

1348 1349 1350
/**
 * gdk_event_get_scroll_deltas:
 * @event: a #GdkEvent
1351 1352
 * @delta_x: (out): return location for X delta
 * @delta_y: (out): return location for Y delta
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
 *
 * Retrieves the scroll deltas from a #GdkEvent
 *
 * Returns: %TRUE if the event contains smooth scroll information
 *
 * Since: 3.4
 **/
gboolean
gdk_event_get_scroll_deltas (const GdkEvent *event,
                             gdouble        *delta_x,
                             gdouble        *delta_y)
{
  gboolean fetched = TRUE;
  gdouble dx = 0.0;
  gdouble dy = 0.0;

1369
  switch ((guint) event->any.type)
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
    {
    case GDK_SCROLL:
      if (event->scroll.direction == GDK_SCROLL_SMOOTH)
        {
          dx = event->scroll.delta_x;
          dy = event->scroll.delta_y;
        }
      else
        fetched = FALSE;
      break;
    default:
      fetched = FALSE;
      break;
    }

  if (delta_x)
    *delta_x = dx;

  if (delta_y)
    *delta_y = dy;

  return fetched;
}

1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
/**
 * gdk_event_is_scroll_stop_event
 * @event: a #GdkEvent
 *
 * Check whether a scroll event is a stop scroll event. Scroll sequences
 * with smooth scroll information may provide a stop scroll event once the
 * interaction with the device finishes, e.g. by lifting a finger. This
 * stop scroll event is the signal that a widget may trigger kinetic
 * scrolling based on the current velocity.
 *
 * Stop scroll events always have a a delta of 0/0.
 *
 * Returns: %TRUE if the event is a scroll stop event
 *
 * Since: 3.20
 */
gboolean
gdk_event_is_scroll_stop_event (const GdkEvent *event)
{
  return event->scroll.is_stop;
}

1416 1417 1418
/**
 * gdk_event_get_axis:
 * @event: a #GdkEvent
1419
 * @axis_use: the axis use to look for
1420
 * @value: (out): location to store the value found
1421 1422 1423 1424
 * 
 * Extract the axis value for a particular axis use from
 * an event structure.
 * 
1425
 * Returns: %TRUE if the specified axis was found, otherwise %FALSE
1426 1427
 **/
gboolean
1428 1429 1430
gdk_event_get_axis (const GdkEvent *event,
		    GdkAxisUse      axis_use,
		    gdouble        *value)
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
{
  gdouble *axes;
  GdkDevice *device;
  
  g_return_val_if_fail (event != NULL, FALSE);
  
  if (axis_use == GDK_AXIS_X || axis_use == GDK_AXIS_Y)
    {
      gdouble x, y;