gdkevents.c 54.8 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 "gdkinternals.h"
28
#include "gdkdisplayprivate.h"
29

30
31
#include <string.h>
#include <math.h>
32

33
34
35
36
37

/**
 * SECTION:events
 * @Short_description: Functions for handling events from the window system
 * @Title: Events
38
 * @See_also: [Event Structures][gdk3-Event-Structures]
39
40
41
42
43
44
45
 *
 * 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
46
 * [Event Structures][gdk3-Event-Structures] are useful.
47
48
49
 */


50
typedef struct _GdkIOClosure GdkIOClosure;
51

52
53
struct _GdkIOClosure
{
Michael Natterer's avatar
Michael Natterer committed
54
  GDestroyNotify notify;
55
56
57
58
59
60
  gpointer data;
};

/* Private variable declarations
 */

61
62
63
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)
{
  if (_gdk_event_func)
    (*_gdk_event_func) (event, _gdk_event_data);
}
71

72
73
74
75
/*********************************************
 * Functions for maintaining the event queue *
 *********************************************/

76
/**
77
 * _gdk_event_queue_find_first:
78
79
80
81
82
 * @display: a #GdkDisplay
 * 
 * Find the first event on the queue that is not still
 * being filled in.
 * 
83
84
 * Returns: (nullable): Pointer to the list node for that event, or
 *   %NULL.
85
 **/
86
GList*
87
_gdk_event_queue_find_first (GdkDisplay *display)
88
{
89
90
91
  GList *tmp_list;
  GList *pending_motion = NULL;

92
  gboolean paused = display->event_pause_count > 0;
93

94
  tmp_list = display->queued_events;
95
96
  while (tmp_list)
    {
97
      GdkEventPrivate *event = tmp_list->data;
98

99
100
      if ((event->flags & GDK_EVENT_PENDING) == 0 &&
	  (!paused || (event->flags & GDK_EVENT_FLUSHED) != 0))
101
102
103
104
        {
          if (pending_motion)
            return pending_motion;

105
          if (event->event.type == GDK_MOTION_NOTIFY && (event->flags & GDK_EVENT_FLUSHED) == 0)
106
107
108
109
            pending_motion = tmp_list;
          else
            return tmp_list;
        }
110
111

      tmp_list = g_list_next (tmp_list);
112
113
114
115
116
    }

  return NULL;
}

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
 * _gdk_event_queue_prepend:
 * @display: a #GdkDisplay
 * @event: Event to prepend.
 *
 * Prepends an event before the head of the event queue.
 *
 * Returns: the newly prepended list node.
 **/
GList*
_gdk_event_queue_prepend (GdkDisplay *display,
			  GdkEvent   *event)
{
  display->queued_events = g_list_prepend (display->queued_events, event);
  if (!display->queued_tail)
    display->queued_tail = display->queued_events;
  return display->queued_events;
}

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
 * _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;
}
158

159
160
161
162
163
164
/**
 * _gdk_event_queue_insert_after:
 * @display: a #GdkDisplay
 * @sibling: Append after this event.
 * @event: Event to append.
 *
165
 * Appends an event after the specified event, or if it isn’t in
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
 * 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
188
 * _gdk_event_queue_insert_before:
189
 * @display: a #GdkDisplay
Matthias Clasen's avatar
Matthias Clasen committed
190
191
 * @sibling: Append before this event
 * @event: Event to prepend
192
 *
193
 * Prepends an event before the specified event, or if it isn’t in
Matthias Clasen's avatar
Matthias Clasen committed
194
 * the queue, onto the head of the event queue.
195
 *
Matthias Clasen's avatar
Matthias Clasen committed
196
 * Returns: the newly prepended list node.
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
 *
 * 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);
}


216
217
218
219
220
221
222
/**
 * _gdk_event_queue_remove_link:
 * @display: a #GdkDisplay
 * @node: node to remove
 * 
 * Removes a specified list node from the event queue.
 **/
223
void
224
225
_gdk_event_queue_remove_link (GdkDisplay *display,
			      GList      *node)
226
227
228
229
{
  if (node->prev)
    node->prev->next = node->next;
  else
230
    display->queued_events = node->next;
231
232
233
234
  
  if (node->next)
    node->next->prev = node->prev;
  else
235
    display->queued_tail = node->prev;
236
237
}

238
239
240
241
242
243
244
/**
 * _gdk_event_unqueue:
 * @display: a #GdkDisplay
 * 
 * Removes and returns the first event from the event
 * queue that is not still being filled in.
 * 
245
 * Returns: (nullable): the event, or %NULL. Ownership is transferred
246
247
248
249
 * to the caller.
 **/
GdkEvent*
_gdk_event_unqueue (GdkDisplay *display)
250
{
251
252
253
254
255
256
257
258
259
260
261
262
263
  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;
264
265
}

266
267
268
269
270
271
void
_gdk_event_queue_handle_motion_compression (GdkDisplay *display)
{
  GList *tmp_list;
  GList *pending_motions = NULL;
  GdkWindow *pending_motion_window = NULL;
272
  GdkDevice *pending_motion_device = NULL;
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292

  /* 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;

      if (event->event.type != GDK_MOTION_NOTIFY)
        break;

      if (pending_motion_window != NULL &&
          pending_motion_window != event->event.motion.window)
        break;

293
294
295
296
      if (pending_motion_device != NULL &&
          pending_motion_device != event->event.motion.device)
        break;

297
298
299
      if (!event->event.motion.window->event_compression)
        break;

300
      pending_motion_window = event->event.motion.window;
301
      pending_motion_device = event->event.motion.device;
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
      pending_motions = tmp_list;

      tmp_list = tmp_list->prev;
    }

  while (pending_motions && pending_motions->next != NULL)
    {
      GList *next = pending_motions->next;
      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);
320
321
      if (clock) /* might be NULL if window was destroyed */
	gdk_frame_clock_request_phase (clock, GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS);
322
323
324
    }
}

325
326
327
328
329
330
331
332
333
334
335
336
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;
    }
}

337
/**
338
 * gdk_event_handler_set:
339
340
341
342
 * @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.
343
 * 
344
345
346
347
348
349
350
 * 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+.)
 **/
351
352
353
354
355
void 
gdk_event_handler_set (GdkEventFunc   func,
		       gpointer       data,
		       GDestroyNotify notify)
{
356
357
  if (_gdk_event_notify)
    (*_gdk_event_notify) (_gdk_event_data);
358

359
360
361
  _gdk_event_func = func;
  _gdk_event_data = data;
  _gdk_event_notify = notify;
362
363
}

364
365
366
367
368
/**
 * gdk_events_pending:
 *
 * Checks if any events are ready to be processed for any display.
 *
369
 * Returns: %TRUE if any events are pending.
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
 */
gboolean
gdk_events_pending (void)
{
  GSList *list, *l;
  gboolean pending;

  pending = FALSE;
  list = gdk_display_manager_list_displays (gdk_display_manager_get ());
  for (l = list; l; l = l->next)
    {
      if (_gdk_event_queue_find_first (l->data))
        {
          pending = TRUE;
          goto out;
        }
    }

  for (l = list; l; l = l->next)
    {
      if (gdk_display_has_pending (l->data))
        {
          pending = TRUE;
          goto out;
        }
    }

 out:
  g_slist_free (list);

  return pending;
}

403
404
405
406
407
408
409
/**
 * gdk_event_get:
 * 
 * Checks all open displays for a #GdkEvent to process,to be processed
 * on, fetching events from the windowing system if necessary.
 * See gdk_display_get_event().
 * 
410
411
412
 * Returns: (nullable): the next #GdkEvent to be processed, or %NULL
 * if no events are pending. The returned #GdkEvent should be freed
 * with gdk_event_free().
413
 **/
414
GdkEvent*
415
416
gdk_event_get (void)
{
417
418
  GSList *list, *l;
  GdkEvent *event;
419

420
421
422
  event = NULL;
  list = gdk_display_manager_list_displays (gdk_display_manager_get ());
  for (l = list; l; l = l->next)
423
    {
424
      event = gdk_display_get_event (l->data);
425
      if (event)
426
        break;
427
428
    }

429
430
431
  g_slist_free (list);

  return event;
432
433
}

434
435
/**
 * gdk_event_peek:
436
 *
437
438
439
 * If there is an event waiting in the event queue of some open
 * display, returns a copy of it. See gdk_display_peek_event().
 * 
440
441
442
 * Returns: (nullable): a copy of the first #GdkEvent on some event
 * queue, or %NULL if no events are in any queues. The returned
 * #GdkEvent should be freed with gdk_event_free().
443
 **/
444
GdkEvent*
445
446
gdk_event_peek (void)
{
447
448
  GSList *list, *l;
  GdkEvent *event;
449

450
451
452
  event = NULL;
  list = gdk_display_manager_list_displays (gdk_display_manager_get ());
  for (l = list; l; l = l->next)
453
    {
454
      event = gdk_display_peek_event (l->data);
455
      if (event)
456
        break;
457
458
    }

459
460
461
  g_slist_free (list);

  return event;
462
463
}

464
465
466
467
468
/**
 * gdk_event_put:
 * @event: a #GdkEvent.
 *
 * Appends a copy of the given event onto the front of the event
469
 * queue for event->any.window’s display, or the default event
470
471
 * queue if event->any.window is %NULL. See gdk_display_put_event().
 **/
472
void
473
gdk_event_put (const GdkEvent *event)
474
{
475
  GdkDisplay *display;
476
477
  
  g_return_if_fail (event != NULL);
478

479
  if (event->any.window)
480
    display = gdk_window_get_display (event->any.window);
481
  else
Matthias Clasen's avatar
Matthias Clasen committed
482
    display = gdk_display_get_default ();
483

484
485
  gdk_display_put_event (display, event);
}
486

487
static GHashTable *event_hash = NULL;
488

Matthias Clasen's avatar
Matthias Clasen committed
489
490
491
492
493
494
/**
 * gdk_event_new:
 * @type: a #GdkEventType 
 * 
 * Creates a new event of the given type. All fields are set to 0.
 * 
495
 * Returns: a newly-allocated #GdkEvent. The returned #GdkEvent 
Matthias Clasen's avatar
Matthias Clasen committed
496
 * should be freed with gdk_event_free().
Matthias Clasen's avatar
Matthias Clasen committed
497
498
 *
 * Since: 2.2
Matthias Clasen's avatar
Matthias Clasen committed
499
 **/
500
GdkEvent*
501
gdk_event_new (GdkEventType type)
502
{
503
504
  GdkEventPrivate *new_private;
  GdkEvent *new_event;
505
  
506
507
508
  if (!event_hash)
    event_hash = g_hash_table_new (g_direct_hash, NULL);

509
  new_private = g_slice_new0 (GdkEventPrivate);
510
511
512
513
514
515
516
517
518
  
  new_private->flags = 0;
  new_private->screen = NULL;

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

  new_event = (GdkEvent *) new_private;

  new_event->any.type = type;
519

520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
  /*
   * 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.
   */
  switch (type)
    {
    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_2BUTTON_PRESS:
    case GDK_3BUTTON_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;
543
544
545
546
547
548
549
550
551
    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;
552
553
554
555
556
    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.;
557
558
      new_event->scroll.delta_x = 0.;
      new_event->scroll.delta_y = 0.;
559
560
561
562
563
564
565
566
567
568
569
      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;
    default:
      break;
    }
570
  
571
  return new_event;
572
573
}

574
static gboolean
575
gdk_event_is_allocated (const GdkEvent *event)
576
577
578
{
  if (event_hash)
    return g_hash_table_lookup (event_hash, event) != NULL;
579
580

  return FALSE;
581
}
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606

void
_gdk_event_set_pointer_emulated (GdkEvent *event,
                                 gboolean  emulated)
{
  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);
    }
}

gboolean
_gdk_event_get_pointer_emulated (GdkEvent *event)
{
  if (gdk_event_is_allocated (event))
    return (((GdkEventPrivate *) event)->flags & GDK_EVENT_POINTER_EMULATED) != 0;

  return FALSE;
}

607
608
609
610
611
/**
 * gdk_event_copy:
 * @event: a #GdkEvent
 * 
 * Copies a #GdkEvent, copying or incrementing the reference count of the
612
 * resources associated with it (e.g. #GdkWindow’s and strings).
613
 * 
614
 * Returns: a copy of @event. The returned #GdkEvent should be freed with
615
616
 * gdk_event_free().
 **/
617
GdkEvent*
618
gdk_event_copy (const GdkEvent *event)
619
{
620
  GdkEventPrivate *new_private;
621
  GdkEvent *new_event;
622

623
  g_return_val_if_fail (event != NULL, NULL);
624

625
626
627
  new_event = gdk_event_new (GDK_NOTHING);
  new_private = (GdkEventPrivate *)new_event;

628
  *new_event = *event;
629
  if (new_event->any.window)
630
    g_object_ref (new_event->any.window);
631

632
633
634
635
636
  if (gdk_event_is_allocated (event))
    {
      GdkEventPrivate *private = (GdkEventPrivate *)event;

      new_private->screen = private->screen;
637
      new_private->device = private->device;
638
      new_private->source_device = private->source_device;
639
    }
640

641
642
643
644
645
646
  switch (event->any.type)
    {
    case GDK_KEY_PRESS:
    case GDK_KEY_RELEASE:
      new_event->key.string = g_strdup (event->key.string);
      break;
647

648
649
650
    case GDK_ENTER_NOTIFY:
    case GDK_LEAVE_NOTIFY:
      if (event->crossing.subwindow != NULL)
651
        g_object_ref (event->crossing.subwindow);
652
      break;
653

654
655
656
657
658
659
    case GDK_DRAG_ENTER:
    case GDK_DRAG_LEAVE:
    case GDK_DRAG_MOTION:
    case GDK_DRAG_STATUS:
    case GDK_DROP_START:
    case GDK_DROP_FINISHED:
660
      g_object_ref (event->dnd.context);
661
      break;
662

663
    case GDK_EXPOSE:
Matthias Clasen's avatar
Matthias Clasen committed
664
    case GDK_DAMAGE:
665
      if (event->expose.region)
666
        new_event->expose.region = cairo_region_copy (event->expose.region);
667
      break;
668

669
670
671
    case GDK_SETTING:
      new_event->setting.name = g_strdup (new_event->setting.name);
      break;
Larry Ewing's avatar
Larry Ewing committed
672
673

    case GDK_BUTTON_PRESS:
674
675
    case GDK_2BUTTON_PRESS:
    case GDK_3BUTTON_PRESS:
Larry Ewing's avatar
Larry Ewing committed
676
    case GDK_BUTTON_RELEASE:
677
678
      if (event->button.axes)
        new_event->button.axes = g_memdup (event->button.axes,
679
                                           sizeof (gdouble) * gdk_device_get_n_axes (event->button.device));
Larry Ewing's avatar
Larry Ewing committed
680
681
      break;

682
683
684
685
686
687
688
689
690
    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
691
    case GDK_MOTION_NOTIFY:
692
693
694
      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
695
      break;
696

697
    case GDK_OWNER_CHANGE:
698
699
700
      new_event->owner_change.owner = event->owner_change.owner;
      if (new_event->owner_change.owner)
        g_object_ref (new_event->owner_change.owner);
701
702
      break;

703
704
705
706
707
    case GDK_SELECTION_CLEAR:
    case GDK_SELECTION_NOTIFY:
    case GDK_SELECTION_REQUEST:
      new_event->selection.requestor = event->selection.requestor;
      if (new_event->selection.requestor)
708
        g_object_ref (new_event->selection.requestor);
709
710
      break;

711
    default:
712
713
      break;
    }
714
715

  if (gdk_event_is_allocated (event))
716
    _gdk_display_event_data_copy (gdk_display_get_default (), event, new_event);
717

718
719
720
  return new_event;
}

721
722
723
724
725
726
/**
 * 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
727
728
 * functions such as gdk_event_peek(), gdk_event_get(), gdk_event_copy()
 * and gdk_event_new().
729
 **/
730
731
732
void
gdk_event_free (GdkEvent *event)
{
733
734
  GdkDisplay *display;

735
  g_return_if_fail (event != NULL);
736

737
  if (event->any.window)
738
    g_object_unref (event->any.window);
739
740
741
742
743
744
745
746
747
748
749
  
  switch (event->any.type)
    {
    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)
750
	g_object_unref (event->crossing.subwindow);
751
752
753
754
755
756
757
758
      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:
759
760
      if (event->dnd.context != NULL)
        g_object_unref (event->dnd.context);
761
      break;
762
763

    case GDK_BUTTON_PRESS:
764
765
    case GDK_2BUTTON_PRESS:
    case GDK_3BUTTON_PRESS:
766
    case GDK_BUTTON_RELEASE:
767
      g_free (event->button.axes);
768
      break;
769
770
771
772
773
774
775
776

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

777
    case GDK_EXPOSE:
Matthias Clasen's avatar
Matthias Clasen committed
778
    case GDK_DAMAGE:
779
      if (event->expose.region)
Benjamin Otte's avatar
Benjamin Otte committed
780
	cairo_region_destroy (event->expose.region);
781
782
      break;
      
783
    case GDK_MOTION_NOTIFY:
784
      g_free (event->motion.axes);
785
      break;
786
      
787
788
789
790
    case GDK_SETTING:
      g_free (event->setting.name);
      break;
      
791
    case GDK_OWNER_CHANGE:
792
793
      if (event->owner_change.owner)
        g_object_unref (event->owner_change.owner);
794
795
      break;

796
797
798
799
800
801
802
    case GDK_SELECTION_CLEAR:
    case GDK_SELECTION_NOTIFY:
    case GDK_SELECTION_REQUEST:
      if (event->selection.requestor)
        g_object_unref (event->selection.requestor);
      break;

803
804
805
    default:
      break;
    }
806

807
808
809
  display = gdk_display_get_default ();
  if (display)
    _gdk_display_event_data_free (display, event);
810

811
  g_hash_table_remove (event_hash, event);
812
  g_slice_free (GdkEventPrivate, (GdkEventPrivate*) event);
813
814
}

815
816
817
818
819
820
/**
 * gdk_event_get_window:
 * @event: a #GdkEvent
 *
 * Extracts the #GdkWindow associated with an event.
 *
821
 * Returns: (transfer none): The #GdkWindow associated with the event
822
823
824
825
826
827
828
829
830
831
832
 *
 * Since: 3.10
 */
GdkWindow *
gdk_event_get_window (const GdkEvent *event)
{
  g_return_val_if_fail (event != NULL, NULL);

  return event->any.window;
}

833
/**
834
 * gdk_event_get_time:
835
836
837
838
839
 * @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.
 * 
840
 * Returns: time stamp field from @event
841
 **/
842
guint32
843
gdk_event_get_time (const GdkEvent *event)
844
845
846
847
848
849
850
851
852
853
854
{
  if (event)
    switch (event->type)
      {
      case GDK_MOTION_NOTIFY:
	return event->motion.time;
      case GDK_BUTTON_PRESS:
      case GDK_2BUTTON_PRESS:
      case GDK_3BUTTON_PRESS:
      case GDK_BUTTON_RELEASE:
	return event->button.time;
855
856
857
858
859
      case GDK_TOUCH_BEGIN:
      case GDK_TOUCH_UPDATE:
      case GDK_TOUCH_END:
      case GDK_TOUCH_CANCEL:
        return event->touch.time;
860
861
      case GDK_SCROLL:
        return event->scroll.time;
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
      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_SELECTION_CLEAR:
      case GDK_SELECTION_REQUEST:
      case GDK_SELECTION_NOTIFY:
	return event->selection.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;
884
885
886
887
888
      case GDK_CLIENT_EVENT:
      case GDK_VISIBILITY_NOTIFY:
      case GDK_CONFIGURE:
      case GDK_FOCUS_CHANGE:
      case GDK_NOTHING:
889
      case GDK_DAMAGE:
890
891
892
893
894
      case GDK_DELETE:
      case GDK_DESTROY:
      case GDK_EXPOSE:
      case GDK_MAP:
      case GDK_UNMAP:
Havoc Pennington's avatar
Havoc Pennington committed
895
      case GDK_WINDOW_STATE:
896
      case GDK_SETTING:
897
      case GDK_OWNER_CHANGE:
898
      case GDK_GRAB_BROKEN:
899
      case GDK_EVENT_LAST:
900
901
        /* return current time */
        break;
902
903
904
905
906
      }
  
  return GDK_CURRENT_TIME;
}

907
908
/**
 * gdk_event_get_state:
909
 * @event: (allow-none): a #GdkEvent or %NULL
910
 * @state: (out): return location for state
911
 * 
William Jon McCann's avatar
William Jon McCann committed
912
 * If the event contains a “state” field, puts that field in @state. Otherwise
913
 * stores an empty state (0). Returns %TRUE if there was a state field
914
 * in the event. @event may be %NULL, in which case it’s treated
915
 * as if the event had no state field.
916
 * 
917
 * Returns: %TRUE if there was a state field in the event 
918
919
 **/
gboolean
920
921
gdk_event_get_state (const GdkEvent        *event,
                     GdkModifierType       *state)
922
{
923
924
  g_return_val_if_fail (state != NULL, FALSE);
  
925
926
927
928
  if (event)
    switch (event->type)
      {
      case GDK_MOTION_NOTIFY:
929
930
	*state = event->motion.state;
        return TRUE;
931
932
933
934
      case GDK_BUTTON_PRESS:
      case GDK_2BUTTON_PRESS:
      case GDK_3BUTTON_PRESS:
      case GDK_BUTTON_RELEASE:
935
936
937
938
939
940
941
        *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;
942
        return TRUE;
943
      case GDK_SCROLL:
944
945
	*state =  event->scroll.state;
        return TRUE;
946
947
      case GDK_KEY_PRESS:
      case GDK_KEY_RELEASE:
948
949
	*state =  event->key.state;
        return TRUE;
950
951
      case GDK_ENTER_NOTIFY:
      case GDK_LEAVE_NOTIFY:
952
953
	*state =  event->crossing.state;
        return TRUE;
954
955
956
957
958
959
960
961
962
963
      case GDK_PROPERTY_NOTIFY:
      case GDK_VISIBILITY_NOTIFY:
      case GDK_CLIENT_EVENT:
      case GDK_CONFIGURE:
      case GDK_FOCUS_CHANGE:
      case GDK_SELECTION_CLEAR:
      case GDK_SELECTION_REQUEST:
      case GDK_SELECTION_NOTIFY:
      case GDK_PROXIMITY_IN:
      case GDK_PROXIMITY_OUT:
964
      case GDK_DAMAGE:
965
966
967
968
969
970
971
972
973
974
975
976
      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:
Havoc Pennington's avatar
Havoc Pennington committed
977
      case GDK_WINDOW_STATE:
978
      case GDK_SETTING:
979
      case GDK_OWNER_CHANGE:
980
      case GDK_GRAB_BROKEN:
981
      case GDK_EVENT_LAST:
982
983
984
985
        /* no state field */
        break;
      }

986
987
  *state = 0;
  return FALSE;
988
989
}

990
991
992
/**
 * gdk_event_get_coords:
 * @event: a #GdkEvent
993
994
 * @x_win: (out): location to put event window x coordinate
 * @y_win: (out): location to put event window y coordinate
995
996
997
 * 
 * Extract the event window relative x/y coordinates from an event.
 * 
998
 * Returns: %TRUE if the event delivered event window coordinates
999
1000
 **/
gboolean
1001
1002
1003
gdk_event_get_coords (const GdkEvent *event,
		      gdouble        *x_win,
		      gdouble        *y_win)
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
{
  gdouble x = 0, y = 0;
  gboolean fetched = TRUE;
  
  g_return_val_if_fail (event != NULL, FALSE);

  switch (event->type)
    {
    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_2BUTTON_PRESS:
    case GDK_3BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
      x = event->button.x;
      y = event->button.y;
      break;
1032
1033
1034
1035
1036
1037
1038
    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;
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
    case GDK_MOTION_NOTIFY:
      x = event->motion.x;
      y = event->motion.y;
      break;
    default:
      fetched = FALSE;
      break;
    }

  if (x_win)
    *x_win = x;
  if (y_win)
1051
    *y_win = y;
1052
1053
1054
1055
1056
1057
1058

  return fetched;
}

/**
 * gdk_event_get_root_coords:
 * @event: a #GdkEvent
1059
1060
 * @x_root: (out): location to put root window x coordinate
 * @y_root: (out): location to put root window y coordinate
1061
1062
1063
 * 
 * Extract the root window relative x/y coordinates from an event.
 * 
1064
 * Returns: %TRUE if the event delivered root window coordinates
1065
1066
 **/
gboolean
1067
1068
1069
gdk_event_get_root_coords (const GdkEvent *event,
			   gdouble        *x_root,
			   gdouble        *y_root)
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
{
  gdouble x = 0, y = 0;
  gboolean fetched = TRUE;
  
  g_return_val_if_fail (event != NULL, FALSE);

  switch (event->type)
    {
    case GDK_MOTION_NOTIFY:
      x = event->motion.x_root;
      y = event->motion.y_root;
      break;
1082
1083
1084
1085
    case GDK_SCROLL:
      x = event->scroll.x_root;
      y = event->scroll.y_root;
      break;
1086
1087
1088
1089
1090
1091
1092
    case GDK_BUTTON_PRESS:
    case GDK_2BUTTON_PRESS:
    case GDK_3BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
      x = event->button.x_root;
      y = event->button.y_root;
      break;
1093
1094
1095
1096
1097
1098
1099
    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;
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
    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;
    default:
      fetched = FALSE;
      break;
    }

  if (x_root)
    *x_root = x;
  if (y_root)
1122
    *y_root = y;
1123
1124
1125
1126

  return fetched;
}

1127
1128
1129
1130
1131
1132
1133
/**
 * gdk_event_get_button:
 * @event: a #GdkEvent
 * @button: (out): location to store mouse button number
 *
 * Extract the button number from an event.
 *
1134
 * Returns: %TRUE if the event delivered a button number
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
 *
 * 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);
  
  switch (event->type)
    {
    case GDK_BUTTON_PRESS:
    case GDK_2BUTTON_PRESS:
    case GDK_3BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
      number = event->button.button;
      break;
    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.
 *
1173
 * Returns: %TRUE if the event delivered a click count
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
 *
 * 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);

  switch (event->type)
    {
    case GDK_BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
      number = 1;
      break;
    case GDK_2BUTTON_PRESS:
      number = 2;
      break;
    case GDK_3BUTTON_PRESS:
      number = 3;
      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.
 *
1216
 * Returns: %TRUE if the event delivered a key symbol
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
 *
 * Since: 3.2
 */
gboolean
gdk_event_get_keyval (const GdkEvent *event,
                      guint *keyval)
{
  gboolean fetched = TRUE;
  guint number = 0;

  switch (event->type)
    {
    case GDK_KEY_PRESS:
    case GDK_KEY_RELEASE:
      number = event->key.keyval;
      break;
    default:
      fetched = FALSE;
      break;
    }

  if (keyval)
    *keyval = number;

  return fetched;
}

/**
 * gdk_event_get_keycode:
 * @event: a #GdkEvent
 * @keycode: (out): location to store the keycode
 *
 * Extracts the hardware keycode from an event.
 *
1251
 * Returns: %TRUE if the event delivered a hardware keycode
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
 *
 * Since: 3.2
 */
gboolean
gdk_event_get_keycode (const GdkEvent *event,
                       guint16 *keycode)
{
  gboolean fetched = TRUE;
  guint16 number = 0;

  switch (event->type)
    {
    case GDK_KEY_PRESS:
    case GDK_KEY_RELEASE:
      number = event->key.hardware_keycode;
      break;
    default:
      fetched = FALSE;
      break;
    }

  if (keycode)
    *keycode = number;

  return fetched;
}

/**
 * gdk_event_get_scroll_direction:
 * @event: a #GdkEvent
 * @direction: (out): location to store the scroll direction
 *
 * Extracts the scroll direction from an event.
 *
1286
 * Returns: %TRUE if the event delivered a scroll direction
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
 *
 * Since: 3.2
 */
gboolean
gdk_event_get_scroll_direction (const GdkEvent *event,
                                GdkScrollDirection *direction)
{
  gboolean fetched = TRUE;
  GdkScrollDirection dir = 0;

  switch (event->type)
    {
    case GDK_SCROLL:
1300
1301
1302
1303
      if (event->scroll.direction == GDK_SCROLL_SMOOTH)
        fetched = FALSE;
      else
        dir = event->scroll.direction;
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
      break;
    default:
      fetched = FALSE;
      break;
    }

  if (direction)
    *direction = dir;

  return fetched;
}

1316
1317
1318
/**
 * gdk_event_get_scroll_deltas:
 * @event: a #GdkEvent
1319
1320
 * @delta_x: (out): return location for X delta
 * @delta_y: (out): return location for Y delta
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
 *
 * 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;

  switch (event->type)
    {
    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;
}

1362
1363
1364
/**
 * gdk_event_get_axis:
 * @event: a #GdkEvent
1365
 * @axis_use: the axis use to look for
1366
 * @value: (out): location to store the value found
1367
1368
1369
1370
 * 
 * Extract the axis value for a particular axis use from
 * an event structure.
 * 
1371
 * Returns: %TRUE if the specified axis was found, otherwise %FALSE