]> Pileus Git - ~andy/gtk/blob - gdk/gdkevents.c
Be more careful with private event data
[~andy/gtk] / gdk / gdkevents.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "config.h"
28
29 #include "gdkinternals.h"
30 #include "gdkdisplayprivate.h"
31
32 #include <string.h>
33 #include <math.h>
34
35
36 /**
37  * SECTION:events
38  * @Short_description: Functions for handling events from the window system
39  * @Title: Events
40  * @See_also: <link linkend="gdk-Event-Structures">Event Structures</link>
41  *
42  * This section describes functions dealing with events from the window
43  * system.
44  *
45  * In GTK+ applications the events are handled automatically in
46  * gtk_main_do_event() and passed on to the appropriate widgets, so these
47  * functions are rarely needed. Though some of the fields in the
48  * <link linkend="gdk-Event-Structures">Event Structures</link> are useful.
49  */
50
51
52 typedef struct _GdkIOClosure GdkIOClosure;
53
54 struct _GdkIOClosure
55 {
56   GDestroyNotify notify;
57   gpointer data;
58 };
59
60 /* Private variable declarations
61  */
62
63 static GdkEventFunc   _gdk_event_func = NULL;    /* Callback for events */
64 static gpointer       _gdk_event_data = NULL;
65 static GDestroyNotify _gdk_event_notify = NULL;
66
67 void
68 _gdk_event_emit (GdkEvent *event)
69 {
70   if (_gdk_event_func)
71     (*_gdk_event_func) (event, _gdk_event_data);
72 }
73
74 /*********************************************
75  * Functions for maintaining the event queue *
76  *********************************************/
77
78 /**
79  * _gdk_event_queue_find_first:
80  * @display: a #GdkDisplay
81  * 
82  * Find the first event on the queue that is not still
83  * being filled in.
84  * 
85  * Return value: Pointer to the list node for that event, or NULL.
86  **/
87 GList*
88 _gdk_event_queue_find_first (GdkDisplay *display)
89 {
90   GList *tmp_list = display->queued_events;
91
92   while (tmp_list)
93     {
94       GdkEventPrivate *event = tmp_list->data;
95       if (!(event->flags & GDK_EVENT_PENDING))
96         return tmp_list;
97
98       tmp_list = g_list_next (tmp_list);
99     }
100
101   return NULL;
102 }
103
104 /**
105  * _gdk_event_queue_prepend:
106  * @display: a #GdkDisplay
107  * @event: Event to prepend.
108  *
109  * Prepends an event before the head of the event queue.
110  *
111  * Returns: the newly prepended list node.
112  **/
113 GList*
114 _gdk_event_queue_prepend (GdkDisplay *display,
115                           GdkEvent   *event)
116 {
117   display->queued_events = g_list_prepend (display->queued_events, event);
118   if (!display->queued_tail)
119     display->queued_tail = display->queued_events;
120   return display->queued_events;
121 }
122
123 /**
124  * _gdk_event_queue_append:
125  * @display: a #GdkDisplay
126  * @event: Event to append.
127  * 
128  * Appends an event onto the tail of the event queue.
129  *
130  * Returns: the newly appended list node.
131  **/
132 GList *
133 _gdk_event_queue_append (GdkDisplay *display,
134                          GdkEvent   *event)
135 {
136   display->queued_tail = g_list_append (display->queued_tail, event);
137   
138   if (!display->queued_events)
139     display->queued_events = display->queued_tail;
140   else
141     display->queued_tail = display->queued_tail->next;
142
143   return display->queued_tail;
144 }
145
146 /**
147  * _gdk_event_queue_insert_after:
148  * @display: a #GdkDisplay
149  * @sibling: Append after this event.
150  * @event: Event to append.
151  *
152  * Appends an event after the specified event, or if it isn't in
153  * the queue, onto the tail of the event queue.
154  *
155  * Returns: the newly appended list node.
156  *
157  * Since: 2.16
158  */
159 GList*
160 _gdk_event_queue_insert_after (GdkDisplay *display,
161                                GdkEvent   *sibling,
162                                GdkEvent   *event)
163 {
164   GList *prev = g_list_find (display->queued_events, sibling);
165   if (prev && prev->next)
166     {
167       display->queued_events = g_list_insert_before (display->queued_events, prev->next, event);
168       return prev->next;
169     }
170   else
171     return _gdk_event_queue_append (display, event);
172 }
173
174 /**
175  * _gdk_event_queue_insert_after:
176  * @display: a #GdkDisplay
177  * @sibling: Append after this event.
178  * @event: Event to append.
179  *
180  * Appends an event before the specified event, or if it isn't in
181  * the queue, onto the tail of the event queue.
182  *
183  * Returns: the newly appended list node.
184  *
185  * Since: 2.16
186  */
187 GList*
188 _gdk_event_queue_insert_before (GdkDisplay *display,
189                                 GdkEvent   *sibling,
190                                 GdkEvent   *event)
191 {
192   GList *next = g_list_find (display->queued_events, sibling);
193   if (next)
194     {
195       display->queued_events = g_list_insert_before (display->queued_events, next, event);
196       return next->prev;
197     }
198   else
199     return _gdk_event_queue_append (display, event);
200 }
201
202
203 /**
204  * _gdk_event_queue_remove_link:
205  * @display: a #GdkDisplay
206  * @node: node to remove
207  * 
208  * Removes a specified list node from the event queue.
209  **/
210 void
211 _gdk_event_queue_remove_link (GdkDisplay *display,
212                               GList      *node)
213 {
214   if (node->prev)
215     node->prev->next = node->next;
216   else
217     display->queued_events = node->next;
218   
219   if (node->next)
220     node->next->prev = node->prev;
221   else
222     display->queued_tail = node->prev;
223 }
224
225 /**
226  * _gdk_event_unqueue:
227  * @display: a #GdkDisplay
228  * 
229  * Removes and returns the first event from the event
230  * queue that is not still being filled in.
231  * 
232  * Return value: the event, or %NULL. Ownership is transferred
233  * to the caller.
234  **/
235 GdkEvent*
236 _gdk_event_unqueue (GdkDisplay *display)
237 {
238   GdkEvent *event = NULL;
239   GList *tmp_list;
240
241   tmp_list = _gdk_event_queue_find_first (display);
242
243   if (tmp_list)
244     {
245       event = tmp_list->data;
246       _gdk_event_queue_remove_link (display, tmp_list);
247       g_list_free_1 (tmp_list);
248     }
249
250   return event;
251 }
252
253 /**
254  * gdk_event_handler_set:
255  * @func: the function to call to handle events from GDK.
256  * @data: user data to pass to the function. 
257  * @notify: the function to call when the handler function is removed, i.e. when
258  *          gdk_event_handler_set() is called with another event handler.
259  * 
260  * Sets the function to call to handle all events from GDK.
261  *
262  * Note that GTK+ uses this to install its own event handler, so it is
263  * usually not useful for GTK+ applications. (Although an application
264  * can call this function then call gtk_main_do_event() to pass
265  * events to GTK+.)
266  **/
267 void 
268 gdk_event_handler_set (GdkEventFunc   func,
269                        gpointer       data,
270                        GDestroyNotify notify)
271 {
272   if (_gdk_event_notify)
273     (*_gdk_event_notify) (_gdk_event_data);
274
275   _gdk_event_func = func;
276   _gdk_event_data = data;
277   _gdk_event_notify = notify;
278 }
279
280 /**
281  * gdk_events_pending:
282  *
283  * Checks if any events are ready to be processed for any display.
284  *
285  * Return value: %TRUE if any events are pending.
286  */
287 gboolean
288 gdk_events_pending (void)
289 {
290   GSList *list, *l;
291   gboolean pending;
292
293   pending = FALSE;
294   list = gdk_display_manager_list_displays (gdk_display_manager_get ());
295   for (l = list; l; l = l->next)
296     {
297       if (_gdk_event_queue_find_first (l->data))
298         {
299           pending = TRUE;
300           goto out;
301         }
302     }
303
304   for (l = list; l; l = l->next)
305     {
306       if (gdk_display_has_pending (l->data))
307         {
308           pending = TRUE;
309           goto out;
310         }
311     }
312
313  out:
314   g_slist_free (list);
315
316   return pending;
317 }
318
319 /**
320  * gdk_event_get:
321  * 
322  * Checks all open displays for a #GdkEvent to process,to be processed
323  * on, fetching events from the windowing system if necessary.
324  * See gdk_display_get_event().
325  * 
326  * Return value: the next #GdkEvent to be processed, or %NULL if no events
327  * are pending. The returned #GdkEvent should be freed with gdk_event_free().
328  **/
329 GdkEvent*
330 gdk_event_get (void)
331 {
332   GSList *list, *l;
333   GdkEvent *event;
334
335   event = NULL;
336   list = gdk_display_manager_list_displays (gdk_display_manager_get ());
337   for (l = list; l; l = l->next)
338     {
339       event = gdk_display_get_event (l->data);
340       if (event)
341         break;
342     }
343
344   g_slist_free (list);
345
346   return event;
347 }
348
349 /**
350  * gdk_event_peek:
351  *
352  * If there is an event waiting in the event queue of some open
353  * display, returns a copy of it. See gdk_display_peek_event().
354  * 
355  * Return value: a copy of the first #GdkEvent on some event queue, or %NULL if no
356  * events are in any queues. The returned #GdkEvent should be freed with
357  * gdk_event_free().
358  **/
359 GdkEvent*
360 gdk_event_peek (void)
361 {
362   GSList *list, *l;
363   GdkEvent *event;
364
365   event = NULL;
366   list = gdk_display_manager_list_displays (gdk_display_manager_get ());
367   for (l = list; l; l = l->next)
368     {
369       event = gdk_display_peek_event (l->data);
370       if (event)
371         break;
372     }
373
374   g_slist_free (list);
375
376   return event;
377 }
378
379 /**
380  * gdk_event_put:
381  * @event: a #GdkEvent.
382  *
383  * Appends a copy of the given event onto the front of the event
384  * queue for event->any.window's display, or the default event
385  * queue if event->any.window is %NULL. See gdk_display_put_event().
386  **/
387 void
388 gdk_event_put (const GdkEvent *event)
389 {
390   GdkDisplay *display;
391   
392   g_return_if_fail (event != NULL);
393
394   if (event->any.window)
395     display = gdk_window_get_display (event->any.window);
396   else
397     {
398       GDK_NOTE (MULTIHEAD,
399                 g_message ("Falling back to default display for gdk_event_put()"));
400       display = gdk_display_get_default ();
401     }
402
403   gdk_display_put_event (display, event);
404 }
405
406 static GHashTable *event_hash = NULL;
407
408 /**
409  * gdk_event_new:
410  * @type: a #GdkEventType 
411  * 
412  * Creates a new event of the given type. All fields are set to 0.
413  * 
414  * Return value: a newly-allocated #GdkEvent. The returned #GdkEvent 
415  * should be freed with gdk_event_free().
416  *
417  * Since: 2.2
418  **/
419 GdkEvent*
420 gdk_event_new (GdkEventType type)
421 {
422   GdkEventPrivate *new_private;
423   GdkEvent *new_event;
424   
425   if (!event_hash)
426     event_hash = g_hash_table_new (g_direct_hash, NULL);
427
428   new_private = g_slice_new0 (GdkEventPrivate);
429   
430   new_private->flags = 0;
431   new_private->screen = NULL;
432
433   g_hash_table_insert (event_hash, new_private, GUINT_TO_POINTER (1));
434
435   new_event = (GdkEvent *) new_private;
436
437   new_event->any.type = type;
438
439   /*
440    * Bytewise 0 initialization is reasonable for most of the 
441    * current event types. Explicitely initialize double fields
442    * since I trust bytewise 0 == 0. less than for integers
443    * or pointers.
444    */
445   switch (type)
446     {
447     case GDK_MOTION_NOTIFY:
448       new_event->motion.x = 0.;
449       new_event->motion.y = 0.;
450       new_event->motion.x_root = 0.;
451       new_event->motion.y_root = 0.;
452       break;
453     case GDK_BUTTON_PRESS:
454     case GDK_2BUTTON_PRESS:
455     case GDK_3BUTTON_PRESS:
456     case GDK_BUTTON_RELEASE:
457       new_event->button.x = 0.;
458       new_event->button.y = 0.;
459       new_event->button.x_root = 0.;
460       new_event->button.y_root = 0.;
461       break;
462     case GDK_SCROLL:
463       new_event->scroll.x = 0.;
464       new_event->scroll.y = 0.;
465       new_event->scroll.x_root = 0.;
466       new_event->scroll.y_root = 0.;
467       break;
468     case GDK_ENTER_NOTIFY:
469     case GDK_LEAVE_NOTIFY:
470       new_event->crossing.x = 0.;
471       new_event->crossing.y = 0.;
472       new_event->crossing.x_root = 0.;
473       new_event->crossing.y_root = 0.;
474       break;
475     default:
476       break;
477     }
478   
479   return new_event;
480 }
481
482 static gboolean
483 gdk_event_is_allocated (const GdkEvent *event)
484 {
485   if (event_hash)
486     return g_hash_table_lookup (event_hash, event) != NULL;
487
488   return FALSE;
489 }
490  
491 /**
492  * gdk_event_copy:
493  * @event: a #GdkEvent
494  * 
495  * Copies a #GdkEvent, copying or incrementing the reference count of the
496  * resources associated with it (e.g. #GdkWindow's and strings).
497  * 
498  * Return value: a copy of @event. The returned #GdkEvent should be freed with
499  * gdk_event_free().
500  **/
501 GdkEvent*
502 gdk_event_copy (const GdkEvent *event)
503 {
504   GdkEventPrivate *new_private;
505   GdkEvent *new_event;
506
507   g_return_val_if_fail (event != NULL, NULL);
508
509   new_event = gdk_event_new (GDK_NOTHING);
510   new_private = (GdkEventPrivate *)new_event;
511
512   *new_event = *event;
513   if (new_event->any.window)
514     g_object_ref (new_event->any.window);
515
516   if (gdk_event_is_allocated (event))
517     {
518       GdkEventPrivate *private = (GdkEventPrivate *)event;
519
520       new_private->screen = private->screen;
521       new_private->device = private->device;
522       new_private->source_device = private->source_device;
523     }
524
525   switch (event->any.type)
526     {
527     case GDK_KEY_PRESS:
528     case GDK_KEY_RELEASE:
529       new_event->key.string = g_strdup (event->key.string);
530       break;
531
532     case GDK_ENTER_NOTIFY:
533     case GDK_LEAVE_NOTIFY:
534       if (event->crossing.subwindow != NULL)
535         g_object_ref (event->crossing.subwindow);
536       break;
537
538     case GDK_DRAG_ENTER:
539     case GDK_DRAG_LEAVE:
540     case GDK_DRAG_MOTION:
541     case GDK_DRAG_STATUS:
542     case GDK_DROP_START:
543     case GDK_DROP_FINISHED:
544       g_object_ref (event->dnd.context);
545       break;
546
547     case GDK_EXPOSE:
548     case GDK_DAMAGE:
549       if (event->expose.region)
550         new_event->expose.region = cairo_region_copy (event->expose.region);
551       break;
552
553     case GDK_SETTING:
554       new_event->setting.name = g_strdup (new_event->setting.name);
555       break;
556
557     case GDK_BUTTON_PRESS:
558     case GDK_BUTTON_RELEASE:
559       if (event->button.axes)
560         new_event->button.axes = g_memdup (event->button.axes,
561                                            sizeof (gdouble) * gdk_device_get_n_axes (event->button.device));
562       break;
563
564     case GDK_MOTION_NOTIFY:
565       if (event->motion.axes)
566         new_event->motion.axes = g_memdup (event->motion.axes,
567                                            sizeof (gdouble) * gdk_device_get_n_axes (event->motion.device));
568       break;
569
570     default:
571       break;
572     }
573
574   if (gdk_event_is_allocated (event))
575     _gdk_display_event_data_copy (gdk_display_get_default (), event, new_event);
576
577   return new_event;
578 }
579
580 /**
581  * gdk_event_free:
582  * @event:  a #GdkEvent.
583  * 
584  * Frees a #GdkEvent, freeing or decrementing any resources associated with it.
585  * Note that this function should only be called with events returned from
586  * functions such as gdk_event_peek(), gdk_event_get(), gdk_event_copy()
587  * and gdk_event_new().
588  **/
589 void
590 gdk_event_free (GdkEvent *event)
591 {
592   g_return_if_fail (event != NULL);
593
594   if (event->any.window)
595     g_object_unref (event->any.window);
596   
597   switch (event->any.type)
598     {
599     case GDK_KEY_PRESS:
600     case GDK_KEY_RELEASE:
601       g_free (event->key.string);
602       break;
603       
604     case GDK_ENTER_NOTIFY:
605     case GDK_LEAVE_NOTIFY:
606       if (event->crossing.subwindow != NULL)
607         g_object_unref (event->crossing.subwindow);
608       break;
609       
610     case GDK_DRAG_ENTER:
611     case GDK_DRAG_LEAVE:
612     case GDK_DRAG_MOTION:
613     case GDK_DRAG_STATUS:
614     case GDK_DROP_START:
615     case GDK_DROP_FINISHED:
616       if (event->dnd.context != NULL)
617         g_object_unref (event->dnd.context);
618       break;
619
620     case GDK_BUTTON_PRESS:
621     case GDK_BUTTON_RELEASE:
622       g_free (event->button.axes);
623       break;
624       
625     case GDK_EXPOSE:
626     case GDK_DAMAGE:
627       if (event->expose.region)
628         cairo_region_destroy (event->expose.region);
629       break;
630       
631     case GDK_MOTION_NOTIFY:
632       g_free (event->motion.axes);
633       break;
634       
635     case GDK_SETTING:
636       g_free (event->setting.name);
637       break;
638       
639     default:
640       break;
641     }
642
643   _gdk_display_event_data_free (gdk_display_get_default (), event);
644
645   g_hash_table_remove (event_hash, event);
646   g_slice_free (GdkEventPrivate, (GdkEventPrivate*) event);
647 }
648
649 /**
650  * gdk_event_get_time:
651  * @event: a #GdkEvent
652  * 
653  * Returns the time stamp from @event, if there is one; otherwise
654  * returns #GDK_CURRENT_TIME. If @event is %NULL, returns #GDK_CURRENT_TIME.
655  * 
656  * Return value: time stamp field from @event
657  **/
658 guint32
659 gdk_event_get_time (const GdkEvent *event)
660 {
661   if (event)
662     switch (event->type)
663       {
664       case GDK_MOTION_NOTIFY:
665         return event->motion.time;
666       case GDK_BUTTON_PRESS:
667       case GDK_2BUTTON_PRESS:
668       case GDK_3BUTTON_PRESS:
669       case GDK_BUTTON_RELEASE:
670         return event->button.time;
671       case GDK_SCROLL:
672         return event->scroll.time;
673       case GDK_KEY_PRESS:
674       case GDK_KEY_RELEASE:
675         return event->key.time;
676       case GDK_ENTER_NOTIFY:
677       case GDK_LEAVE_NOTIFY:
678         return event->crossing.time;
679       case GDK_PROPERTY_NOTIFY:
680         return event->property.time;
681       case GDK_SELECTION_CLEAR:
682       case GDK_SELECTION_REQUEST:
683       case GDK_SELECTION_NOTIFY:
684         return event->selection.time;
685       case GDK_PROXIMITY_IN:
686       case GDK_PROXIMITY_OUT:
687         return event->proximity.time;
688       case GDK_DRAG_ENTER:
689       case GDK_DRAG_LEAVE:
690       case GDK_DRAG_MOTION:
691       case GDK_DRAG_STATUS:
692       case GDK_DROP_START:
693       case GDK_DROP_FINISHED:
694         return event->dnd.time;
695       case GDK_CLIENT_EVENT:
696       case GDK_VISIBILITY_NOTIFY:
697       case GDK_CONFIGURE:
698       case GDK_FOCUS_CHANGE:
699       case GDK_NOTHING:
700       case GDK_DAMAGE:
701       case GDK_DELETE:
702       case GDK_DESTROY:
703       case GDK_EXPOSE:
704       case GDK_MAP:
705       case GDK_UNMAP:
706       case GDK_WINDOW_STATE:
707       case GDK_SETTING:
708       case GDK_OWNER_CHANGE:
709       case GDK_GRAB_BROKEN:
710       case GDK_EVENT_LAST:
711         /* return current time */
712         break;
713       }
714   
715   return GDK_CURRENT_TIME;
716 }
717
718 /**
719  * gdk_event_get_state:
720  * @event: a #GdkEvent or NULL
721  * @state: (out): return location for state
722  * 
723  * If the event contains a "state" field, puts that field in @state. Otherwise
724  * stores an empty state (0). Returns %TRUE if there was a state field
725  * in the event. @event may be %NULL, in which case it's treated
726  * as if the event had no state field.
727  * 
728  * Return value: %TRUE if there was a state field in the event 
729  **/
730 gboolean
731 gdk_event_get_state (const GdkEvent        *event,
732                      GdkModifierType       *state)
733 {
734   g_return_val_if_fail (state != NULL, FALSE);
735   
736   if (event)
737     switch (event->type)
738       {
739       case GDK_MOTION_NOTIFY:
740         *state = event->motion.state;
741         return TRUE;
742       case GDK_BUTTON_PRESS:
743       case GDK_2BUTTON_PRESS:
744       case GDK_3BUTTON_PRESS:
745       case GDK_BUTTON_RELEASE:
746         *state =  event->button.state;
747         return TRUE;
748       case GDK_SCROLL:
749         *state =  event->scroll.state;
750         return TRUE;
751       case GDK_KEY_PRESS:
752       case GDK_KEY_RELEASE:
753         *state =  event->key.state;
754         return TRUE;
755       case GDK_ENTER_NOTIFY:
756       case GDK_LEAVE_NOTIFY:
757         *state =  event->crossing.state;
758         return TRUE;
759       case GDK_PROPERTY_NOTIFY:
760       case GDK_VISIBILITY_NOTIFY:
761       case GDK_CLIENT_EVENT:
762       case GDK_CONFIGURE:
763       case GDK_FOCUS_CHANGE:
764       case GDK_SELECTION_CLEAR:
765       case GDK_SELECTION_REQUEST:
766       case GDK_SELECTION_NOTIFY:
767       case GDK_PROXIMITY_IN:
768       case GDK_PROXIMITY_OUT:
769       case GDK_DAMAGE:
770       case GDK_DRAG_ENTER:
771       case GDK_DRAG_LEAVE:
772       case GDK_DRAG_MOTION:
773       case GDK_DRAG_STATUS:
774       case GDK_DROP_START:
775       case GDK_DROP_FINISHED:
776       case GDK_NOTHING:
777       case GDK_DELETE:
778       case GDK_DESTROY:
779       case GDK_EXPOSE:
780       case GDK_MAP:
781       case GDK_UNMAP:
782       case GDK_WINDOW_STATE:
783       case GDK_SETTING:
784       case GDK_OWNER_CHANGE:
785       case GDK_GRAB_BROKEN:
786       case GDK_EVENT_LAST:
787         /* no state field */
788         break;
789       }
790
791   *state = 0;
792   return FALSE;
793 }
794
795 /**
796  * gdk_event_get_coords:
797  * @event: a #GdkEvent
798  * @x_win: (out): location to put event window x coordinate
799  * @y_win: (out): location to put event window y coordinate
800  * 
801  * Extract the event window relative x/y coordinates from an event.
802  * 
803  * Return value: %TRUE if the event delivered event window coordinates
804  **/
805 gboolean
806 gdk_event_get_coords (const GdkEvent *event,
807                       gdouble        *x_win,
808                       gdouble        *y_win)
809 {
810   gdouble x = 0, y = 0;
811   gboolean fetched = TRUE;
812   
813   g_return_val_if_fail (event != NULL, FALSE);
814
815   switch (event->type)
816     {
817     case GDK_CONFIGURE:
818       x = event->configure.x;
819       y = event->configure.y;
820       break;
821     case GDK_ENTER_NOTIFY:
822     case GDK_LEAVE_NOTIFY:
823       x = event->crossing.x;
824       y = event->crossing.y;
825       break;
826     case GDK_SCROLL:
827       x = event->scroll.x;
828       y = event->scroll.y;
829       break;
830     case GDK_BUTTON_PRESS:
831     case GDK_2BUTTON_PRESS:
832     case GDK_3BUTTON_PRESS:
833     case GDK_BUTTON_RELEASE:
834       x = event->button.x;
835       y = event->button.y;
836       break;
837     case GDK_MOTION_NOTIFY:
838       x = event->motion.x;
839       y = event->motion.y;
840       break;
841     default:
842       fetched = FALSE;
843       break;
844     }
845
846   if (x_win)
847     *x_win = x;
848   if (y_win)
849     *y_win = y;
850
851   return fetched;
852 }
853
854 /**
855  * gdk_event_get_root_coords:
856  * @event: a #GdkEvent
857  * @x_root: (out): location to put root window x coordinate
858  * @y_root: (out): location to put root window y coordinate
859  * 
860  * Extract the root window relative x/y coordinates from an event.
861  * 
862  * Return value: %TRUE if the event delivered root window coordinates
863  **/
864 gboolean
865 gdk_event_get_root_coords (const GdkEvent *event,
866                            gdouble        *x_root,
867                            gdouble        *y_root)
868 {
869   gdouble x = 0, y = 0;
870   gboolean fetched = TRUE;
871   
872   g_return_val_if_fail (event != NULL, FALSE);
873
874   switch (event->type)
875     {
876     case GDK_MOTION_NOTIFY:
877       x = event->motion.x_root;
878       y = event->motion.y_root;
879       break;
880     case GDK_SCROLL:
881       x = event->scroll.x_root;
882       y = event->scroll.y_root;
883       break;
884     case GDK_BUTTON_PRESS:
885     case GDK_2BUTTON_PRESS:
886     case GDK_3BUTTON_PRESS:
887     case GDK_BUTTON_RELEASE:
888       x = event->button.x_root;
889       y = event->button.y_root;
890       break;
891     case GDK_ENTER_NOTIFY:
892     case GDK_LEAVE_NOTIFY:
893       x = event->crossing.x_root;
894       y = event->crossing.y_root;
895       break;
896     case GDK_DRAG_ENTER:
897     case GDK_DRAG_LEAVE:
898     case GDK_DRAG_MOTION:
899     case GDK_DRAG_STATUS:
900     case GDK_DROP_START:
901     case GDK_DROP_FINISHED:
902       x = event->dnd.x_root;
903       y = event->dnd.y_root;
904       break;
905     default:
906       fetched = FALSE;
907       break;
908     }
909
910   if (x_root)
911     *x_root = x;
912   if (y_root)
913     *y_root = y;
914
915   return fetched;
916 }
917
918 /**
919  * gdk_event_get_axis:
920  * @event: a #GdkEvent
921  * @axis_use: (out): the axis use to look for
922  * @value: (out): location to store the value found
923  * 
924  * Extract the axis value for a particular axis use from
925  * an event structure.
926  * 
927  * Return value: %TRUE if the specified axis was found, otherwise %FALSE
928  **/
929 gboolean
930 gdk_event_get_axis (const GdkEvent *event,
931                     GdkAxisUse      axis_use,
932                     gdouble        *value)
933 {
934   gdouble *axes;
935   GdkDevice *device;
936   
937   g_return_val_if_fail (event != NULL, FALSE);
938   
939   if (axis_use == GDK_AXIS_X || axis_use == GDK_AXIS_Y)
940     {
941       gdouble x, y;
942       
943       switch (event->type)
944         {
945         case GDK_MOTION_NOTIFY:
946           x = event->motion.x;
947           y = event->motion.y;
948           break;
949         case GDK_SCROLL:
950           x = event->scroll.x;
951           y = event->scroll.y;
952           break;
953         case GDK_BUTTON_PRESS:
954         case GDK_BUTTON_RELEASE:
955           x = event->button.x;
956           y = event->button.y;
957           break;
958         case GDK_ENTER_NOTIFY:
959         case GDK_LEAVE_NOTIFY:
960           x = event->crossing.x;
961           y = event->crossing.y;
962           break;
963           
964         default:
965           return FALSE;
966         }
967
968       if (axis_use == GDK_AXIS_X && value)
969         *value = x;
970       if (axis_use == GDK_AXIS_Y && value)
971         *value = y;
972
973       return TRUE;
974     }
975   else if (event->type == GDK_BUTTON_PRESS ||
976            event->type == GDK_BUTTON_RELEASE)
977     {
978       device = event->button.device;
979       axes = event->button.axes;
980     }
981   else if (event->type == GDK_MOTION_NOTIFY)
982     {
983       device = event->motion.device;
984       axes = event->motion.axes;
985     }
986   else
987     return FALSE;
988
989   return gdk_device_get_axis (device, axes, axis_use, value);
990 }
991
992 /**
993  * gdk_event_set_device:
994  * @event: a #GdkEvent
995  * @device: a #GdkDevice
996  *
997  * Sets the device for @event to @device. The event must
998  * have been allocated by GTK+, for instance, by
999  * gdk_event_copy().
1000  *
1001  * Since: 3.0
1002  **/
1003 void
1004 gdk_event_set_device (GdkEvent  *event,
1005                       GdkDevice *device)
1006 {
1007   GdkEventPrivate *private;
1008
1009   g_return_if_fail (gdk_event_is_allocated (event));
1010
1011   private = (GdkEventPrivate *) event;
1012
1013   private->device = device;
1014
1015   switch (event->type)
1016     {
1017     case GDK_MOTION_NOTIFY:
1018       event->motion.device = device;
1019       break;
1020     case GDK_BUTTON_PRESS:
1021     case GDK_2BUTTON_PRESS:
1022     case GDK_3BUTTON_PRESS:
1023     case GDK_BUTTON_RELEASE:
1024       event->button.device = device;
1025       break;
1026     case GDK_SCROLL:
1027       event->scroll.device = device;
1028       break;
1029     case GDK_PROXIMITY_IN:
1030     case GDK_PROXIMITY_OUT:
1031       event->proximity.device = device;
1032       break;
1033     default:
1034       break;
1035     }
1036 }
1037
1038 /**
1039  * gdk_event_get_device:
1040  * @event: a #GdkEvent.
1041  *
1042  * If the event contains a "device" field, this function will return
1043  * it, else it will return %NULL.
1044  *
1045  * Returns: (transfer none): a #GdkDevice, or %NULL.
1046  *
1047  * Since: 3.0
1048  **/
1049 GdkDevice *
1050 gdk_event_get_device (const GdkEvent *event)
1051 {
1052   g_return_val_if_fail (event != NULL, NULL);
1053
1054   if (gdk_event_is_allocated (event))
1055     {
1056       GdkEventPrivate *private = (GdkEventPrivate *) event;
1057
1058       if (private->device)
1059         return private->device;
1060     }
1061
1062   switch (event->type)
1063     {
1064     case GDK_MOTION_NOTIFY:
1065       return event->motion.device;
1066     case GDK_BUTTON_PRESS:
1067     case GDK_2BUTTON_PRESS:
1068     case GDK_3BUTTON_PRESS:
1069     case GDK_BUTTON_RELEASE:
1070       return event->button.device;
1071     case GDK_SCROLL:
1072       return event->scroll.device;
1073     case GDK_PROXIMITY_IN:
1074     case GDK_PROXIMITY_OUT:
1075       return event->proximity.device;
1076     default:
1077       break;
1078     }
1079
1080   /* Fallback if event has no device set */
1081   switch (event->type)
1082     {
1083     case GDK_MOTION_NOTIFY:
1084     case GDK_BUTTON_PRESS:
1085     case GDK_2BUTTON_PRESS:
1086     case GDK_3BUTTON_PRESS:
1087     case GDK_BUTTON_RELEASE:
1088     case GDK_ENTER_NOTIFY:
1089     case GDK_LEAVE_NOTIFY:
1090     case GDK_FOCUS_CHANGE:
1091     case GDK_PROXIMITY_IN:
1092     case GDK_PROXIMITY_OUT:
1093     case GDK_DRAG_ENTER:
1094     case GDK_DRAG_LEAVE:
1095     case GDK_DRAG_MOTION:
1096     case GDK_DRAG_STATUS:
1097     case GDK_DROP_START:
1098     case GDK_DROP_FINISHED:
1099     case GDK_SCROLL:
1100     case GDK_GRAB_BROKEN:
1101     case GDK_KEY_PRESS:
1102     case GDK_KEY_RELEASE:
1103       {
1104         GdkDisplay *display;
1105         GdkDeviceManager *device_manager;
1106         GdkDevice *client_pointer;
1107
1108         g_warning ("Event with type %d not holding a GdkDevice. "
1109                    "It is most likely synthesized outside Gdk/GTK+\n",
1110                    event->type);
1111
1112         display = gdk_window_get_display (event->any.window);
1113         device_manager = gdk_display_get_device_manager (display);
1114         client_pointer = gdk_device_manager_get_client_pointer (device_manager);
1115
1116         if (event->type == GDK_KEY_PRESS ||
1117             event->type == GDK_KEY_RELEASE)
1118           return gdk_device_get_associated_device (client_pointer);
1119         else
1120           return client_pointer;
1121       }
1122       break;
1123     default:
1124       return NULL;
1125     }
1126 }
1127
1128 /**
1129  * gdk_event_set_source_device:
1130  * @event: a #GdkEvent
1131  * @device: a #GdkDevice
1132  *
1133  * Sets the slave device for @event to @device.
1134  *
1135  * The event must have been allocated by GTK+,
1136  * for instance by gdk_event_copy().
1137  *
1138  * Since: 3.0
1139  **/
1140 void
1141 gdk_event_set_source_device (GdkEvent  *event,
1142                              GdkDevice *device)
1143 {
1144   GdkEventPrivate *private;
1145
1146   g_return_if_fail (gdk_event_is_allocated (event));
1147   g_return_if_fail (GDK_IS_DEVICE (device));
1148
1149   private = (GdkEventPrivate *) event;
1150
1151   private->source_device = device;
1152 }
1153
1154 /**
1155  * gdk_event_get_source_device:
1156  * @event: a #GdkEvent
1157  *
1158  * This function returns the hardware (slave) #GdkDevice that has
1159  * triggered the event, falling back to the virtual (master) device
1160  * (as in gdk_event_get_device()) if the event wasn't caused by
1161  * interaction with a hardware device. This may happen for example
1162  * in synthesized crossing events after a #GdkWindow updates its
1163  * geometry or a grab is acquired/released.
1164  *
1165  * If the event does not contain a device field, this function will
1166  * return %NULL.
1167  *
1168  * Returns: a #GdkDevice, or %NULL
1169  *
1170  * Since: 3.0
1171  **/
1172 GdkDevice *
1173 gdk_event_get_source_device (const GdkEvent *event)
1174 {
1175   GdkEventPrivate *private;
1176
1177   g_return_val_if_fail (event != NULL, NULL);
1178
1179   if (!gdk_event_is_allocated (event))
1180     return NULL;
1181
1182   private = (GdkEventPrivate *) event;
1183
1184   if (private->source_device)
1185     return private->source_device;
1186
1187   /* Fallback to event device */
1188   return gdk_event_get_device (event);
1189 }
1190
1191 /**
1192  * gdk_event_request_motions:
1193  * @event: a valid #GdkEvent
1194  *
1195  * Request more motion notifies if @event is a motion notify hint event.
1196  *
1197  * This function should be used instead of gdk_window_get_pointer() to
1198  * request further motion notifies, because it also works for extension
1199  * events where motion notifies are provided for devices other than the
1200  * core pointer. Coordinate extraction, processing and requesting more
1201  * motion events from a %GDK_MOTION_NOTIFY event usually works like this:
1202  *
1203  * |[
1204  * {
1205  *   /&ast; motion_event handler &ast;/
1206  *   x = motion_event->x;
1207  *   y = motion_event->y;
1208  *   /&ast; handle (x,y) motion &ast;/
1209  *   gdk_event_request_motions (motion_event); /&ast; handles is_hint events &ast;/
1210  * }
1211  * ]|
1212  *
1213  * Since: 2.12
1214  **/
1215 void
1216 gdk_event_request_motions (const GdkEventMotion *event)
1217 {
1218   GdkDisplay *display;
1219   
1220   g_return_if_fail (event != NULL);
1221   
1222   if (event->type == GDK_MOTION_NOTIFY && event->is_hint)
1223     {
1224       gdk_device_get_state (event->device, event->window, NULL, NULL);
1225       
1226       display = gdk_window_get_display (event->window);
1227       _gdk_display_enable_motion_hints (display, event->device);
1228     }
1229 }
1230
1231 static gboolean
1232 gdk_events_get_axis_distances (GdkEvent *event1,
1233                                GdkEvent *event2,
1234                                gdouble  *x_distance,
1235                                gdouble  *y_distance,
1236                                gdouble  *distance)
1237 {
1238   gdouble x1, x2, y1, y2;
1239   gdouble xd, yd;
1240
1241   if (!gdk_event_get_coords (event1, &x1, &y1) ||
1242       !gdk_event_get_coords (event2, &x2, &y2))
1243     return FALSE;
1244
1245   xd = x2 - x1;
1246   yd = y2 - y1;
1247
1248   if (x_distance)
1249     *x_distance = xd;
1250
1251   if (y_distance)
1252     *y_distance = yd;
1253
1254   if (distance)
1255     *distance = sqrt ((xd * xd) + (yd * yd));
1256
1257   return TRUE;
1258 }
1259
1260 /**
1261  * gdk_events_get_distance:
1262  * @event1: first #GdkEvent
1263  * @event2: second #GdkEvent
1264  * @distance: return location for the distance
1265  *
1266  * If both events have X/Y information, the distance between both coordinates
1267  * (as in a straight line going from @event1 to @event2) will be returned.
1268  *
1269  * Returns: %TRUE if the distance could be calculated.
1270  *
1271  * Since: 3.0
1272  **/
1273 gboolean
1274 gdk_events_get_distance (GdkEvent *event1,
1275                          GdkEvent *event2,
1276                          gdouble  *distance)
1277 {
1278   return gdk_events_get_axis_distances (event1, event2,
1279                                         NULL, NULL,
1280                                         distance);
1281 }
1282
1283 /**
1284  * gdk_events_get_angle:
1285  * @event1: first #GdkEvent
1286  * @event2: second #GdkEvent
1287  * @angle: return location for the relative angle between both events
1288  *
1289  * If both events contain X/Y information, this function will return %TRUE
1290  * and return in @angle the relative angle from @event1 to @event2. The rotation
1291  * direction for positive angles is from the positive X axis towards the positive
1292  * Y axis.
1293  *
1294  * Returns: %TRUE if the angle could be calculated.
1295  *
1296  * Since: 3.0
1297  **/
1298 gboolean
1299 gdk_events_get_angle (GdkEvent *event1,
1300                       GdkEvent *event2,
1301                       gdouble  *angle)
1302 {
1303   gdouble x_distance, y_distance, distance;
1304
1305   if (!gdk_events_get_axis_distances (event1, event2,
1306                                       &x_distance, &y_distance,
1307                                       &distance))
1308     return FALSE;
1309
1310   if (angle)
1311     {
1312       *angle = atan2 (x_distance, y_distance);
1313
1314       /* Invert angle */
1315       *angle = (2 * G_PI) - *angle;
1316
1317       /* Shift it 90° */
1318       *angle += G_PI / 2;
1319
1320       /* And constraint it to 0°-360° */
1321       *angle = fmod (*angle, 2 * G_PI);
1322     }
1323
1324   return TRUE;
1325 }
1326
1327 /**
1328  * gdk_events_get_center:
1329  * @event1: first #GdkEvent
1330  * @event2: second #GdkEvent
1331  * @x: (out): return location for the X coordinate of the center
1332  * @y: (out): return location for the Y coordinate of the center
1333  *
1334  * If both events contain X/Y information, the center of both coordinates
1335  * will be returned in @x and @y.
1336  *
1337  * Returns: %TRUE if the center could be calculated.
1338  *
1339  * Since: 3.0
1340  **/
1341 gboolean
1342 gdk_events_get_center (GdkEvent *event1,
1343                        GdkEvent *event2,
1344                        gdouble  *x,
1345                        gdouble  *y)
1346 {
1347   gdouble x1, x2, y1, y2;
1348
1349   if (!gdk_event_get_coords (event1, &x1, &y1) ||
1350       !gdk_event_get_coords (event2, &x2, &y2))
1351     return FALSE;
1352
1353   if (x)
1354     *x = (x2 + x1) / 2;
1355
1356   if (y)
1357     *y = (y2 + y1) / 2;
1358
1359   return TRUE;
1360 }
1361
1362 /**
1363  * gdk_event_set_screen:
1364  * @event: a #GdkEvent
1365  * @screen: a #GdkScreen
1366  * 
1367  * Sets the screen for @event to @screen. The event must
1368  * have been allocated by GTK+, for instance, by
1369  * gdk_event_copy().
1370  *
1371  * Since: 2.2
1372  **/
1373 void
1374 gdk_event_set_screen (GdkEvent  *event,
1375                       GdkScreen *screen)
1376 {
1377   GdkEventPrivate *private;
1378   
1379   g_return_if_fail (gdk_event_is_allocated (event));
1380
1381   private = (GdkEventPrivate *)event;
1382   
1383   private->screen = screen;
1384 }
1385
1386 /**
1387  * gdk_event_get_screen:
1388  * @event: a #GdkEvent
1389  * 
1390  * Returns the screen for the event. The screen is
1391  * typically the screen for <literal>event->any.window</literal>, but
1392  * for events such as mouse events, it is the screen
1393  * where the pointer was when the event occurs -
1394  * that is, the screen which has the root window 
1395  * to which <literal>event->motion.x_root</literal> and
1396  * <literal>event->motion.y_root</literal> are relative.
1397  * 
1398  * Return value: (transfer none): the screen for the event
1399  *
1400  * Since: 2.2
1401  **/
1402 GdkScreen *
1403 gdk_event_get_screen (const GdkEvent *event)
1404 {
1405   if (gdk_event_is_allocated (event))
1406     {
1407       GdkEventPrivate *private = (GdkEventPrivate *)event;
1408
1409       if (private->screen)
1410         return private->screen;
1411     }
1412
1413   if (event->any.window)
1414     return gdk_window_get_screen (event->any.window);
1415
1416   return NULL;
1417 }
1418
1419 /**
1420  * gdk_set_show_events:
1421  * @show_events:  %TRUE to output event debugging information.
1422  * 
1423  * Sets whether a trace of received events is output.
1424  * Note that GTK+ must be compiled with debugging (that is,
1425  * configured using the <option>--enable-debug</option> option)
1426  * to use this option.
1427  **/
1428 void
1429 gdk_set_show_events (gboolean show_events)
1430 {
1431   if (show_events)
1432     _gdk_debug_flags |= GDK_DEBUG_EVENTS;
1433   else
1434     _gdk_debug_flags &= ~GDK_DEBUG_EVENTS;
1435 }
1436
1437 /**
1438  * gdk_get_show_events:
1439  * 
1440  * Gets whether event debugging output is enabled.
1441  * 
1442  * Return value: %TRUE if event debugging output is enabled.
1443  **/
1444 gboolean
1445 gdk_get_show_events (void)
1446 {
1447   return (_gdk_debug_flags & GDK_DEBUG_EVENTS) != 0;
1448 }
1449
1450 /* What do we do with G_IO_NVAL?
1451  */
1452 #define READ_CONDITION (G_IO_IN | G_IO_HUP | G_IO_ERR)
1453 #define WRITE_CONDITION (G_IO_OUT | G_IO_ERR)
1454 #define EXCEPTION_CONDITION (G_IO_PRI)
1455
1456 static void
1457 gdk_synthesize_click (GdkDisplay *display,
1458                       GdkEvent   *event,
1459                       gint        nclicks)
1460 {
1461   GdkEvent *event_copy;
1462   GList *link;
1463
1464   event_copy = gdk_event_copy (event);
1465   event_copy->type = (nclicks == 2) ? GDK_2BUTTON_PRESS : GDK_3BUTTON_PRESS;
1466
1467   link = _gdk_event_queue_append (display, event_copy);
1468 }
1469
1470 void
1471 _gdk_event_button_generate (GdkDisplay *display,
1472                             GdkEvent   *event)
1473 {
1474   GdkMultipleClickInfo *info;
1475
1476   g_return_if_fail (event->type == GDK_BUTTON_PRESS);
1477
1478   info = g_hash_table_lookup (display->multiple_click_info, event->button.device);
1479
1480   if (G_UNLIKELY (!info))
1481     {
1482       info = g_new0 (GdkMultipleClickInfo, 1);
1483       info->button_number[0] = info->button_number[1] = -1;
1484
1485       g_hash_table_insert (display->multiple_click_info,
1486                            event->button.device, info);
1487     }
1488
1489   if ((event->button.time < (info->button_click_time[1] + 2 * display->double_click_time)) &&
1490       (event->button.window == info->button_window[1]) &&
1491       (event->button.button == info->button_number[1]) &&
1492       (ABS (event->button.x - info->button_x[1]) <= display->double_click_distance) &&
1493       (ABS (event->button.y - info->button_y[1]) <= display->double_click_distance))
1494     {
1495       gdk_synthesize_click (display, event, 3);
1496
1497       info->button_click_time[1] = 0;
1498       info->button_click_time[0] = 0;
1499       info->button_window[1] = NULL;
1500       info->button_window[0] = NULL;
1501       info->button_number[1] = -1;
1502       info->button_number[0] = -1;
1503       info->button_x[0] = info->button_x[1] = 0;
1504       info->button_y[0] = info->button_y[1] = 0;
1505     }
1506   else if ((event->button.time < (info->button_click_time[0] + display->double_click_time)) &&
1507            (event->button.window == info->button_window[0]) &&
1508            (event->button.button == info->button_number[0]) &&
1509            (ABS (event->button.x - info->button_x[0]) <= display->double_click_distance) &&
1510            (ABS (event->button.y - info->button_y[0]) <= display->double_click_distance))
1511     {
1512       gdk_synthesize_click (display, event, 2);
1513       
1514       info->button_click_time[1] = info->button_click_time[0];
1515       info->button_click_time[0] = event->button.time;
1516       info->button_window[1] = info->button_window[0];
1517       info->button_window[0] = event->button.window;
1518       info->button_number[1] = info->button_number[0];
1519       info->button_number[0] = event->button.button;
1520       info->button_x[1] = info->button_x[0];
1521       info->button_x[0] = event->button.x;
1522       info->button_y[1] = info->button_y[0];
1523       info->button_y[0] = event->button.y;
1524     }
1525   else
1526     {
1527       info->button_click_time[1] = 0;
1528       info->button_click_time[0] = event->button.time;
1529       info->button_window[1] = NULL;
1530       info->button_window[0] = event->button.window;
1531       info->button_number[1] = -1;
1532       info->button_number[0] = event->button.button;
1533       info->button_x[1] = 0;
1534       info->button_x[0] = event->button.x;
1535       info->button_y[1] = 0;
1536       info->button_y[0] = event->button.y;
1537     }
1538 }
1539
1540 void
1541 gdk_synthesize_window_state (GdkWindow     *window,
1542                              GdkWindowState unset_flags,
1543                              GdkWindowState set_flags)
1544 {
1545   GdkEvent temp_event;
1546   GdkWindowState old;
1547   
1548   g_return_if_fail (window != NULL);
1549   
1550   temp_event.window_state.window = window;
1551   temp_event.window_state.type = GDK_WINDOW_STATE;
1552   temp_event.window_state.send_event = FALSE;
1553   
1554   old = temp_event.window_state.window->state;
1555   
1556   temp_event.window_state.new_window_state = old;
1557   temp_event.window_state.new_window_state |= set_flags;
1558   temp_event.window_state.new_window_state &= ~unset_flags;
1559   temp_event.window_state.changed_mask = temp_event.window_state.new_window_state ^ old;
1560
1561   if (temp_event.window_state.new_window_state == old)
1562     return; /* No actual work to do, nothing changed. */
1563
1564   /* Actually update the field in GdkWindow, this is sort of an odd
1565    * place to do it, but seems like the safest since it ensures we expose no
1566    * inconsistent state to the user.
1567    */
1568   
1569   window->state = temp_event.window_state.new_window_state;
1570
1571   if (temp_event.window_state.changed_mask & GDK_WINDOW_STATE_WITHDRAWN)
1572     _gdk_window_update_viewable (window);
1573
1574   /* We only really send the event to toplevels, since
1575    * all the window states don't apply to non-toplevels.
1576    * Non-toplevels do use the GDK_WINDOW_STATE_WITHDRAWN flag
1577    * internally so we needed to update window->state.
1578    */
1579   switch (window->window_type)
1580     {
1581     case GDK_WINDOW_TOPLEVEL:
1582     case GDK_WINDOW_TEMP: /* ? */
1583       gdk_display_put_event (gdk_window_get_display (window), &temp_event);
1584       break;
1585       
1586     case GDK_WINDOW_FOREIGN:
1587     case GDK_WINDOW_ROOT:
1588     case GDK_WINDOW_CHILD:
1589       break;
1590     }
1591 }
1592
1593 /**
1594  * gdk_display_set_double_click_time:
1595  * @display: a #GdkDisplay
1596  * @msec: double click time in milliseconds (thousandths of a second) 
1597  * 
1598  * Sets the double click time (two clicks within this time interval
1599  * count as a double click and result in a #GDK_2BUTTON_PRESS event).
1600  * Applications should <emphasis>not</emphasis> set this, it is a global 
1601  * user-configured setting.
1602  *
1603  * Since: 2.2
1604  **/
1605 void
1606 gdk_display_set_double_click_time (GdkDisplay *display,
1607                                    guint       msec)
1608 {
1609   display->double_click_time = msec;
1610 }
1611
1612 /**
1613  * gdk_set_double_click_time:
1614  * @msec: double click time in milliseconds (thousandths of a second)
1615  *
1616  * Set the double click time for the default display. See
1617  * gdk_display_set_double_click_time(). 
1618  * See also gdk_display_set_double_click_distance().
1619  * Applications should <emphasis>not</emphasis> set this, it is a 
1620  * global user-configured setting.
1621  **/
1622 void
1623 gdk_set_double_click_time (guint msec)
1624 {
1625   gdk_display_set_double_click_time (gdk_display_get_default (), msec);
1626 }
1627
1628 /**
1629  * gdk_display_set_double_click_distance:
1630  * @display: a #GdkDisplay
1631  * @distance: distance in pixels
1632  * 
1633  * Sets the double click distance (two clicks within this distance
1634  * count as a double click and result in a #GDK_2BUTTON_PRESS event).
1635  * See also gdk_display_set_double_click_time().
1636  * Applications should <emphasis>not</emphasis> set this, it is a global 
1637  * user-configured setting.
1638  *
1639  * Since: 2.4
1640  **/
1641 void
1642 gdk_display_set_double_click_distance (GdkDisplay *display,
1643                                        guint       distance)
1644 {
1645   display->double_click_distance = distance;
1646 }
1647
1648 G_DEFINE_BOXED_TYPE (GdkEvent, gdk_event,
1649                      gdk_event_copy,
1650                      gdk_event_free)
1651
1652 /**
1653  * gdk_setting_get:
1654  * @name: the name of the setting.
1655  * @value: location to store the value of the setting.
1656  *
1657  * Obtains a desktop-wide setting, such as the double-click time,
1658  * for the default screen. See gdk_screen_get_setting().
1659  *
1660  * Returns: %TRUE if the setting existed and a value was stored
1661  *   in @value, %FALSE otherwise.
1662  **/
1663 gboolean
1664 gdk_setting_get (const gchar *name,
1665                  GValue      *value)
1666 {
1667   return gdk_screen_get_setting (gdk_screen_get_default (), name, value);
1668 }