1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
29 #include "gdkinternals.h"
30 #include "gdkdisplayprivate.h"
38 * @Short_description: Functions for handling events from the window system
40 * @See_also: <link linkend="gdk-Event-Structures">Event Structures</link>
42 * This section describes functions dealing with events from the window
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.
52 typedef struct _GdkIOClosure GdkIOClosure;
56 GDestroyNotify notify;
60 /* Private variable declarations
63 static GdkEventFunc _gdk_event_func = NULL; /* Callback for events */
64 static gpointer _gdk_event_data = NULL;
65 static GDestroyNotify _gdk_event_notify = NULL;
68 _gdk_event_emit (GdkEvent *event)
71 (*_gdk_event_func) (event, _gdk_event_data);
74 /*********************************************
75 * Functions for maintaining the event queue *
76 *********************************************/
79 * _gdk_event_queue_find_first:
80 * @display: a #GdkDisplay
82 * Find the first event on the queue that is not still
85 * Return value: Pointer to the list node for that event, or NULL.
88 _gdk_event_queue_find_first (GdkDisplay *display)
90 GList *tmp_list = display->queued_events;
94 GdkEventPrivate *event = tmp_list->data;
95 if (!(event->flags & GDK_EVENT_PENDING))
98 tmp_list = g_list_next (tmp_list);
105 * _gdk_event_queue_prepend:
106 * @display: a #GdkDisplay
107 * @event: Event to prepend.
109 * Prepends an event before the head of the event queue.
111 * Returns: the newly prepended list node.
114 _gdk_event_queue_prepend (GdkDisplay *display,
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;
124 * _gdk_event_queue_append:
125 * @display: a #GdkDisplay
126 * @event: Event to append.
128 * Appends an event onto the tail of the event queue.
130 * Returns: the newly appended list node.
133 _gdk_event_queue_append (GdkDisplay *display,
136 display->queued_tail = g_list_append (display->queued_tail, event);
138 if (!display->queued_events)
139 display->queued_events = display->queued_tail;
141 display->queued_tail = display->queued_tail->next;
143 return display->queued_tail;
147 * _gdk_event_queue_insert_after:
148 * @display: a #GdkDisplay
149 * @sibling: Append after this event.
150 * @event: Event to append.
152 * Appends an event after the specified event, or if it isn't in
153 * the queue, onto the tail of the event queue.
155 * Returns: the newly appended list node.
160 _gdk_event_queue_insert_after (GdkDisplay *display,
164 GList *prev = g_list_find (display->queued_events, sibling);
165 if (prev && prev->next)
167 display->queued_events = g_list_insert_before (display->queued_events, prev->next, event);
171 return _gdk_event_queue_append (display, event);
175 * _gdk_event_queue_insert_after:
176 * @display: a #GdkDisplay
177 * @sibling: Append after this event.
178 * @event: Event to append.
180 * Appends an event before the specified event, or if it isn't in
181 * the queue, onto the tail of the event queue.
183 * Returns: the newly appended list node.
188 _gdk_event_queue_insert_before (GdkDisplay *display,
192 GList *next = g_list_find (display->queued_events, sibling);
195 display->queued_events = g_list_insert_before (display->queued_events, next, event);
199 return _gdk_event_queue_append (display, event);
204 * _gdk_event_queue_remove_link:
205 * @display: a #GdkDisplay
206 * @node: node to remove
208 * Removes a specified list node from the event queue.
211 _gdk_event_queue_remove_link (GdkDisplay *display,
215 node->prev->next = node->next;
217 display->queued_events = node->next;
220 node->next->prev = node->prev;
222 display->queued_tail = node->prev;
226 * _gdk_event_unqueue:
227 * @display: a #GdkDisplay
229 * Removes and returns the first event from the event
230 * queue that is not still being filled in.
232 * Return value: the event, or %NULL. Ownership is transferred
236 _gdk_event_unqueue (GdkDisplay *display)
238 GdkEvent *event = NULL;
241 tmp_list = _gdk_event_queue_find_first (display);
245 event = tmp_list->data;
246 _gdk_event_queue_remove_link (display, tmp_list);
247 g_list_free_1 (tmp_list);
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.
260 * Sets the function to call to handle all events from GDK.
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
268 gdk_event_handler_set (GdkEventFunc func,
270 GDestroyNotify notify)
272 if (_gdk_event_notify)
273 (*_gdk_event_notify) (_gdk_event_data);
275 _gdk_event_func = func;
276 _gdk_event_data = data;
277 _gdk_event_notify = notify;
281 * gdk_events_pending:
283 * Checks if any events are ready to be processed for any display.
285 * Return value: %TRUE if any events are pending.
288 gdk_events_pending (void)
294 list = gdk_display_manager_list_displays (gdk_display_manager_get ());
295 for (l = list; l; l = l->next)
297 if (_gdk_event_queue_find_first (l->data))
304 for (l = list; l; l = l->next)
306 if (gdk_display_has_pending (l->data))
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().
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().
336 list = gdk_display_manager_list_displays (gdk_display_manager_get ());
337 for (l = list; l; l = l->next)
339 event = gdk_display_get_event (l->data);
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().
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
360 gdk_event_peek (void)
366 list = gdk_display_manager_list_displays (gdk_display_manager_get ());
367 for (l = list; l; l = l->next)
369 event = gdk_display_peek_event (l->data);
381 * @event: a #GdkEvent.
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().
388 gdk_event_put (const GdkEvent *event)
392 g_return_if_fail (event != NULL);
394 if (event->any.window)
395 display = gdk_window_get_display (event->any.window);
399 g_message ("Falling back to default display for gdk_event_put()"));
400 display = gdk_display_get_default ();
403 gdk_display_put_event (display, event);
406 static GHashTable *event_hash = NULL;
410 * @type: a #GdkEventType
412 * Creates a new event of the given type. All fields are set to 0.
414 * Return value: a newly-allocated #GdkEvent. The returned #GdkEvent
415 * should be freed with gdk_event_free().
420 gdk_event_new (GdkEventType type)
422 GdkEventPrivate *new_private;
426 event_hash = g_hash_table_new (g_direct_hash, NULL);
428 new_private = g_slice_new0 (GdkEventPrivate);
430 new_private->flags = 0;
431 new_private->screen = NULL;
433 g_hash_table_insert (event_hash, new_private, GUINT_TO_POINTER (1));
435 new_event = (GdkEvent *) new_private;
437 new_event->any.type = type;
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
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.;
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.;
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.;
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.;
483 gdk_event_is_allocated (const GdkEvent *event)
486 return g_hash_table_lookup (event_hash, event) != NULL;
493 * @event: a #GdkEvent
495 * Copies a #GdkEvent, copying or incrementing the reference count of the
496 * resources associated with it (e.g. #GdkWindow's and strings).
498 * Return value: a copy of @event. The returned #GdkEvent should be freed with
502 gdk_event_copy (const GdkEvent *event)
504 GdkEventPrivate *new_private;
507 g_return_val_if_fail (event != NULL, NULL);
509 new_event = gdk_event_new (GDK_NOTHING);
510 new_private = (GdkEventPrivate *)new_event;
513 if (new_event->any.window)
514 g_object_ref (new_event->any.window);
516 if (gdk_event_is_allocated (event))
518 GdkEventPrivate *private = (GdkEventPrivate *)event;
520 new_private->screen = private->screen;
521 new_private->device = private->device;
522 new_private->source_device = private->source_device;
525 switch (event->any.type)
528 case GDK_KEY_RELEASE:
529 new_event->key.string = g_strdup (event->key.string);
532 case GDK_ENTER_NOTIFY:
533 case GDK_LEAVE_NOTIFY:
534 if (event->crossing.subwindow != NULL)
535 g_object_ref (event->crossing.subwindow);
540 case GDK_DRAG_MOTION:
541 case GDK_DRAG_STATUS:
543 case GDK_DROP_FINISHED:
544 g_object_ref (event->dnd.context);
549 if (event->expose.region)
550 new_event->expose.region = cairo_region_copy (event->expose.region);
554 new_event->setting.name = g_strdup (new_event->setting.name);
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));
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));
570 case GDK_OWNER_CHANGE:
571 new_event->owner_change.owner = event->owner_change.owner;
572 if (new_event->owner_change.owner)
573 g_object_ref (new_event->owner_change.owner);
576 case GDK_SELECTION_CLEAR:
577 case GDK_SELECTION_NOTIFY:
578 case GDK_SELECTION_REQUEST:
579 new_event->selection.requestor = event->selection.requestor;
580 if (new_event->selection.requestor)
581 g_object_unref (new_event->selection.requestor);
588 if (gdk_event_is_allocated (event))
589 _gdk_display_event_data_copy (gdk_display_get_default (), event, new_event);
596 * @event: a #GdkEvent.
598 * Frees a #GdkEvent, freeing or decrementing any resources associated with it.
599 * Note that this function should only be called with events returned from
600 * functions such as gdk_event_peek(), gdk_event_get(), gdk_event_copy()
601 * and gdk_event_new().
604 gdk_event_free (GdkEvent *event)
606 g_return_if_fail (event != NULL);
608 if (event->any.window)
609 g_object_unref (event->any.window);
611 switch (event->any.type)
614 case GDK_KEY_RELEASE:
615 g_free (event->key.string);
618 case GDK_ENTER_NOTIFY:
619 case GDK_LEAVE_NOTIFY:
620 if (event->crossing.subwindow != NULL)
621 g_object_unref (event->crossing.subwindow);
626 case GDK_DRAG_MOTION:
627 case GDK_DRAG_STATUS:
629 case GDK_DROP_FINISHED:
630 if (event->dnd.context != NULL)
631 g_object_unref (event->dnd.context);
634 case GDK_BUTTON_PRESS:
635 case GDK_BUTTON_RELEASE:
636 g_free (event->button.axes);
641 if (event->expose.region)
642 cairo_region_destroy (event->expose.region);
645 case GDK_MOTION_NOTIFY:
646 g_free (event->motion.axes);
650 g_free (event->setting.name);
653 case GDK_OWNER_CHANGE:
654 if (event->owner_change.owner)
655 g_object_unref (event->owner_change.owner);
658 case GDK_SELECTION_CLEAR:
659 case GDK_SELECTION_NOTIFY:
660 case GDK_SELECTION_REQUEST:
661 if (event->selection.requestor)
662 g_object_unref (event->selection.requestor);
669 _gdk_display_event_data_free (gdk_display_get_default (), event);
671 g_hash_table_remove (event_hash, event);
672 g_slice_free (GdkEventPrivate, (GdkEventPrivate*) event);
676 * gdk_event_get_time:
677 * @event: a #GdkEvent
679 * Returns the time stamp from @event, if there is one; otherwise
680 * returns #GDK_CURRENT_TIME. If @event is %NULL, returns #GDK_CURRENT_TIME.
682 * Return value: time stamp field from @event
685 gdk_event_get_time (const GdkEvent *event)
690 case GDK_MOTION_NOTIFY:
691 return event->motion.time;
692 case GDK_BUTTON_PRESS:
693 case GDK_2BUTTON_PRESS:
694 case GDK_3BUTTON_PRESS:
695 case GDK_BUTTON_RELEASE:
696 return event->button.time;
698 return event->scroll.time;
700 case GDK_KEY_RELEASE:
701 return event->key.time;
702 case GDK_ENTER_NOTIFY:
703 case GDK_LEAVE_NOTIFY:
704 return event->crossing.time;
705 case GDK_PROPERTY_NOTIFY:
706 return event->property.time;
707 case GDK_SELECTION_CLEAR:
708 case GDK_SELECTION_REQUEST:
709 case GDK_SELECTION_NOTIFY:
710 return event->selection.time;
711 case GDK_PROXIMITY_IN:
712 case GDK_PROXIMITY_OUT:
713 return event->proximity.time;
716 case GDK_DRAG_MOTION:
717 case GDK_DRAG_STATUS:
719 case GDK_DROP_FINISHED:
720 return event->dnd.time;
721 case GDK_CLIENT_EVENT:
722 case GDK_VISIBILITY_NOTIFY:
724 case GDK_FOCUS_CHANGE:
732 case GDK_WINDOW_STATE:
734 case GDK_OWNER_CHANGE:
735 case GDK_GRAB_BROKEN:
737 /* return current time */
741 return GDK_CURRENT_TIME;
745 * gdk_event_get_state:
746 * @event: a #GdkEvent or NULL
747 * @state: (out): return location for state
749 * If the event contains a "state" field, puts that field in @state. Otherwise
750 * stores an empty state (0). Returns %TRUE if there was a state field
751 * in the event. @event may be %NULL, in which case it's treated
752 * as if the event had no state field.
754 * Return value: %TRUE if there was a state field in the event
757 gdk_event_get_state (const GdkEvent *event,
758 GdkModifierType *state)
760 g_return_val_if_fail (state != NULL, FALSE);
765 case GDK_MOTION_NOTIFY:
766 *state = event->motion.state;
768 case GDK_BUTTON_PRESS:
769 case GDK_2BUTTON_PRESS:
770 case GDK_3BUTTON_PRESS:
771 case GDK_BUTTON_RELEASE:
772 *state = event->button.state;
775 *state = event->scroll.state;
778 case GDK_KEY_RELEASE:
779 *state = event->key.state;
781 case GDK_ENTER_NOTIFY:
782 case GDK_LEAVE_NOTIFY:
783 *state = event->crossing.state;
785 case GDK_PROPERTY_NOTIFY:
786 case GDK_VISIBILITY_NOTIFY:
787 case GDK_CLIENT_EVENT:
789 case GDK_FOCUS_CHANGE:
790 case GDK_SELECTION_CLEAR:
791 case GDK_SELECTION_REQUEST:
792 case GDK_SELECTION_NOTIFY:
793 case GDK_PROXIMITY_IN:
794 case GDK_PROXIMITY_OUT:
798 case GDK_DRAG_MOTION:
799 case GDK_DRAG_STATUS:
801 case GDK_DROP_FINISHED:
808 case GDK_WINDOW_STATE:
810 case GDK_OWNER_CHANGE:
811 case GDK_GRAB_BROKEN:
822 * gdk_event_get_coords:
823 * @event: a #GdkEvent
824 * @x_win: (out): location to put event window x coordinate
825 * @y_win: (out): location to put event window y coordinate
827 * Extract the event window relative x/y coordinates from an event.
829 * Return value: %TRUE if the event delivered event window coordinates
832 gdk_event_get_coords (const GdkEvent *event,
836 gdouble x = 0, y = 0;
837 gboolean fetched = TRUE;
839 g_return_val_if_fail (event != NULL, FALSE);
844 x = event->configure.x;
845 y = event->configure.y;
847 case GDK_ENTER_NOTIFY:
848 case GDK_LEAVE_NOTIFY:
849 x = event->crossing.x;
850 y = event->crossing.y;
856 case GDK_BUTTON_PRESS:
857 case GDK_2BUTTON_PRESS:
858 case GDK_3BUTTON_PRESS:
859 case GDK_BUTTON_RELEASE:
863 case GDK_MOTION_NOTIFY:
881 * gdk_event_get_root_coords:
882 * @event: a #GdkEvent
883 * @x_root: (out): location to put root window x coordinate
884 * @y_root: (out): location to put root window y coordinate
886 * Extract the root window relative x/y coordinates from an event.
888 * Return value: %TRUE if the event delivered root window coordinates
891 gdk_event_get_root_coords (const GdkEvent *event,
895 gdouble x = 0, y = 0;
896 gboolean fetched = TRUE;
898 g_return_val_if_fail (event != NULL, FALSE);
902 case GDK_MOTION_NOTIFY:
903 x = event->motion.x_root;
904 y = event->motion.y_root;
907 x = event->scroll.x_root;
908 y = event->scroll.y_root;
910 case GDK_BUTTON_PRESS:
911 case GDK_2BUTTON_PRESS:
912 case GDK_3BUTTON_PRESS:
913 case GDK_BUTTON_RELEASE:
914 x = event->button.x_root;
915 y = event->button.y_root;
917 case GDK_ENTER_NOTIFY:
918 case GDK_LEAVE_NOTIFY:
919 x = event->crossing.x_root;
920 y = event->crossing.y_root;
924 case GDK_DRAG_MOTION:
925 case GDK_DRAG_STATUS:
927 case GDK_DROP_FINISHED:
928 x = event->dnd.x_root;
929 y = event->dnd.y_root;
945 * gdk_event_get_axis:
946 * @event: a #GdkEvent
947 * @axis_use: the axis use to look for
948 * @value: (out): location to store the value found
950 * Extract the axis value for a particular axis use from
951 * an event structure.
953 * Return value: %TRUE if the specified axis was found, otherwise %FALSE
956 gdk_event_get_axis (const GdkEvent *event,
963 g_return_val_if_fail (event != NULL, FALSE);
965 if (axis_use == GDK_AXIS_X || axis_use == GDK_AXIS_Y)
971 case GDK_MOTION_NOTIFY:
979 case GDK_BUTTON_PRESS:
980 case GDK_BUTTON_RELEASE:
984 case GDK_ENTER_NOTIFY:
985 case GDK_LEAVE_NOTIFY:
986 x = event->crossing.x;
987 y = event->crossing.y;
994 if (axis_use == GDK_AXIS_X && value)
996 if (axis_use == GDK_AXIS_Y && value)
1001 else if (event->type == GDK_BUTTON_PRESS ||
1002 event->type == GDK_BUTTON_RELEASE)
1004 device = event->button.device;
1005 axes = event->button.axes;
1007 else if (event->type == GDK_MOTION_NOTIFY)
1009 device = event->motion.device;
1010 axes = event->motion.axes;
1015 return gdk_device_get_axis (device, axes, axis_use, value);
1019 * gdk_event_set_device:
1020 * @event: a #GdkEvent
1021 * @device: a #GdkDevice
1023 * Sets the device for @event to @device. The event must
1024 * have been allocated by GTK+, for instance, by
1030 gdk_event_set_device (GdkEvent *event,
1033 GdkEventPrivate *private;
1035 g_return_if_fail (gdk_event_is_allocated (event));
1037 private = (GdkEventPrivate *) event;
1039 private->device = device;
1041 switch (event->type)
1043 case GDK_MOTION_NOTIFY:
1044 event->motion.device = device;
1046 case GDK_BUTTON_PRESS:
1047 case GDK_2BUTTON_PRESS:
1048 case GDK_3BUTTON_PRESS:
1049 case GDK_BUTTON_RELEASE:
1050 event->button.device = device;
1053 event->scroll.device = device;
1055 case GDK_PROXIMITY_IN:
1056 case GDK_PROXIMITY_OUT:
1057 event->proximity.device = device;
1065 * gdk_event_get_device:
1066 * @event: a #GdkEvent.
1068 * If the event contains a "device" field, this function will return
1069 * it, else it will return %NULL.
1071 * Returns: (transfer none): a #GdkDevice, or %NULL.
1076 gdk_event_get_device (const GdkEvent *event)
1078 g_return_val_if_fail (event != NULL, NULL);
1080 if (gdk_event_is_allocated (event))
1082 GdkEventPrivate *private = (GdkEventPrivate *) event;
1084 if (private->device)
1085 return private->device;
1088 switch (event->type)
1090 case GDK_MOTION_NOTIFY:
1091 return event->motion.device;
1092 case GDK_BUTTON_PRESS:
1093 case GDK_2BUTTON_PRESS:
1094 case GDK_3BUTTON_PRESS:
1095 case GDK_BUTTON_RELEASE:
1096 return event->button.device;
1098 return event->scroll.device;
1099 case GDK_PROXIMITY_IN:
1100 case GDK_PROXIMITY_OUT:
1101 return event->proximity.device;
1106 /* Fallback if event has no device set */
1107 switch (event->type)
1109 case GDK_MOTION_NOTIFY:
1110 case GDK_BUTTON_PRESS:
1111 case GDK_2BUTTON_PRESS:
1112 case GDK_3BUTTON_PRESS:
1113 case GDK_BUTTON_RELEASE:
1114 case GDK_ENTER_NOTIFY:
1115 case GDK_LEAVE_NOTIFY:
1116 case GDK_FOCUS_CHANGE:
1117 case GDK_PROXIMITY_IN:
1118 case GDK_PROXIMITY_OUT:
1119 case GDK_DRAG_ENTER:
1120 case GDK_DRAG_LEAVE:
1121 case GDK_DRAG_MOTION:
1122 case GDK_DRAG_STATUS:
1123 case GDK_DROP_START:
1124 case GDK_DROP_FINISHED:
1126 case GDK_GRAB_BROKEN:
1128 case GDK_KEY_RELEASE:
1130 GdkDisplay *display;
1131 GdkDeviceManager *device_manager;
1132 GdkDevice *client_pointer;
1134 g_warning ("Event with type %d not holding a GdkDevice. "
1135 "It is most likely synthesized outside Gdk/GTK+\n",
1138 display = gdk_window_get_display (event->any.window);
1139 device_manager = gdk_display_get_device_manager (display);
1140 client_pointer = gdk_device_manager_get_client_pointer (device_manager);
1142 if (event->type == GDK_KEY_PRESS ||
1143 event->type == GDK_KEY_RELEASE)
1144 return gdk_device_get_associated_device (client_pointer);
1146 return client_pointer;
1155 * gdk_event_set_source_device:
1156 * @event: a #GdkEvent
1157 * @device: a #GdkDevice
1159 * Sets the slave device for @event to @device.
1161 * The event must have been allocated by GTK+,
1162 * for instance by gdk_event_copy().
1167 gdk_event_set_source_device (GdkEvent *event,
1170 GdkEventPrivate *private;
1172 g_return_if_fail (gdk_event_is_allocated (event));
1173 g_return_if_fail (GDK_IS_DEVICE (device));
1175 private = (GdkEventPrivate *) event;
1177 private->source_device = device;
1181 * gdk_event_get_source_device:
1182 * @event: a #GdkEvent
1184 * This function returns the hardware (slave) #GdkDevice that has
1185 * triggered the event, falling back to the virtual (master) device
1186 * (as in gdk_event_get_device()) if the event wasn't caused by
1187 * interaction with a hardware device. This may happen for example
1188 * in synthesized crossing events after a #GdkWindow updates its
1189 * geometry or a grab is acquired/released.
1191 * If the event does not contain a device field, this function will
1194 * Returns: (transfer none): a #GdkDevice, or %NULL.
1199 gdk_event_get_source_device (const GdkEvent *event)
1201 GdkEventPrivate *private;
1203 g_return_val_if_fail (event != NULL, NULL);
1205 if (!gdk_event_is_allocated (event))
1208 private = (GdkEventPrivate *) event;
1210 if (private->source_device)
1211 return private->source_device;
1213 /* Fallback to event device */
1214 return gdk_event_get_device (event);
1218 * gdk_event_request_motions:
1219 * @event: a valid #GdkEvent
1221 * Request more motion notifies if @event is a motion notify hint event.
1223 * This function should be used instead of gdk_window_get_pointer() to
1224 * request further motion notifies, because it also works for extension
1225 * events where motion notifies are provided for devices other than the
1226 * core pointer. Coordinate extraction, processing and requesting more
1227 * motion events from a %GDK_MOTION_NOTIFY event usually works like this:
1231 * /* motion_event handler */
1232 * x = motion_event->x;
1233 * y = motion_event->y;
1234 * /* handle (x,y) motion */
1235 * gdk_event_request_motions (motion_event); /* handles is_hint events */
1242 gdk_event_request_motions (const GdkEventMotion *event)
1244 GdkDisplay *display;
1246 g_return_if_fail (event != NULL);
1248 if (event->type == GDK_MOTION_NOTIFY && event->is_hint)
1250 gdk_device_get_state (event->device, event->window, NULL, NULL);
1252 display = gdk_window_get_display (event->window);
1253 _gdk_display_enable_motion_hints (display, event->device);
1258 gdk_events_get_axis_distances (GdkEvent *event1,
1260 gdouble *x_distance,
1261 gdouble *y_distance,
1264 gdouble x1, x2, y1, y2;
1267 if (!gdk_event_get_coords (event1, &x1, &y1) ||
1268 !gdk_event_get_coords (event2, &x2, &y2))
1281 *distance = sqrt ((xd * xd) + (yd * yd));
1287 * gdk_events_get_distance:
1288 * @event1: first #GdkEvent
1289 * @event2: second #GdkEvent
1290 * @distance: (out): return location for the distance
1292 * If both events have X/Y information, the distance between both coordinates
1293 * (as in a straight line going from @event1 to @event2) will be returned.
1295 * Returns: %TRUE if the distance could be calculated.
1300 gdk_events_get_distance (GdkEvent *event1,
1304 return gdk_events_get_axis_distances (event1, event2,
1310 * gdk_events_get_angle:
1311 * @event1: first #GdkEvent
1312 * @event2: second #GdkEvent
1313 * @angle: (out): return location for the relative angle between both events
1315 * If both events contain X/Y information, this function will return %TRUE
1316 * and return in @angle the relative angle from @event1 to @event2. The rotation
1317 * direction for positive angles is from the positive X axis towards the positive
1320 * Returns: %TRUE if the angle could be calculated.
1325 gdk_events_get_angle (GdkEvent *event1,
1329 gdouble x_distance, y_distance, distance;
1331 if (!gdk_events_get_axis_distances (event1, event2,
1332 &x_distance, &y_distance,
1338 *angle = atan2 (x_distance, y_distance);
1341 *angle = (2 * G_PI) - *angle;
1346 /* And constraint it to 0°-360° */
1347 *angle = fmod (*angle, 2 * G_PI);
1354 * gdk_events_get_center:
1355 * @event1: first #GdkEvent
1356 * @event2: second #GdkEvent
1357 * @x: (out): return location for the X coordinate of the center
1358 * @y: (out): return location for the Y coordinate of the center
1360 * If both events contain X/Y information, the center of both coordinates
1361 * will be returned in @x and @y.
1363 * Returns: %TRUE if the center could be calculated.
1368 gdk_events_get_center (GdkEvent *event1,
1373 gdouble x1, x2, y1, y2;
1375 if (!gdk_event_get_coords (event1, &x1, &y1) ||
1376 !gdk_event_get_coords (event2, &x2, &y2))
1389 * gdk_event_set_screen:
1390 * @event: a #GdkEvent
1391 * @screen: a #GdkScreen
1393 * Sets the screen for @event to @screen. The event must
1394 * have been allocated by GTK+, for instance, by
1400 gdk_event_set_screen (GdkEvent *event,
1403 GdkEventPrivate *private;
1405 g_return_if_fail (gdk_event_is_allocated (event));
1407 private = (GdkEventPrivate *)event;
1409 private->screen = screen;
1413 * gdk_event_get_screen:
1414 * @event: a #GdkEvent
1416 * Returns the screen for the event. The screen is
1417 * typically the screen for <literal>event->any.window</literal>, but
1418 * for events such as mouse events, it is the screen
1419 * where the pointer was when the event occurs -
1420 * that is, the screen which has the root window
1421 * to which <literal>event->motion.x_root</literal> and
1422 * <literal>event->motion.y_root</literal> are relative.
1424 * Return value: (transfer none): the screen for the event
1429 gdk_event_get_screen (const GdkEvent *event)
1431 if (gdk_event_is_allocated (event))
1433 GdkEventPrivate *private = (GdkEventPrivate *)event;
1435 if (private->screen)
1436 return private->screen;
1439 if (event->any.window)
1440 return gdk_window_get_screen (event->any.window);
1446 * gdk_set_show_events:
1447 * @show_events: %TRUE to output event debugging information.
1449 * Sets whether a trace of received events is output.
1450 * Note that GTK+ must be compiled with debugging (that is,
1451 * configured using the <option>--enable-debug</option> option)
1452 * to use this option.
1455 gdk_set_show_events (gboolean show_events)
1458 _gdk_debug_flags |= GDK_DEBUG_EVENTS;
1460 _gdk_debug_flags &= ~GDK_DEBUG_EVENTS;
1464 * gdk_get_show_events:
1466 * Gets whether event debugging output is enabled.
1468 * Return value: %TRUE if event debugging output is enabled.
1471 gdk_get_show_events (void)
1473 return (_gdk_debug_flags & GDK_DEBUG_EVENTS) != 0;
1476 /* What do we do with G_IO_NVAL?
1478 #define READ_CONDITION (G_IO_IN | G_IO_HUP | G_IO_ERR)
1479 #define WRITE_CONDITION (G_IO_OUT | G_IO_ERR)
1480 #define EXCEPTION_CONDITION (G_IO_PRI)
1483 gdk_synthesize_click (GdkDisplay *display,
1487 GdkEvent *event_copy;
1489 event_copy = gdk_event_copy (event);
1490 event_copy->type = (nclicks == 2) ? GDK_2BUTTON_PRESS : GDK_3BUTTON_PRESS;
1492 _gdk_event_queue_append (display, event_copy);
1496 _gdk_event_button_generate (GdkDisplay *display,
1499 GdkMultipleClickInfo *info;
1501 g_return_if_fail (event->type == GDK_BUTTON_PRESS);
1503 info = g_hash_table_lookup (display->multiple_click_info, event->button.device);
1505 if (G_UNLIKELY (!info))
1507 info = g_new0 (GdkMultipleClickInfo, 1);
1508 info->button_number[0] = info->button_number[1] = -1;
1510 g_hash_table_insert (display->multiple_click_info,
1511 event->button.device, info);
1514 if ((event->button.time < (info->button_click_time[1] + 2 * display->double_click_time)) &&
1515 (event->button.window == info->button_window[1]) &&
1516 (event->button.button == info->button_number[1]) &&
1517 (ABS (event->button.x - info->button_x[1]) <= display->double_click_distance) &&
1518 (ABS (event->button.y - info->button_y[1]) <= display->double_click_distance))
1520 gdk_synthesize_click (display, event, 3);
1522 info->button_click_time[1] = 0;
1523 info->button_click_time[0] = 0;
1524 info->button_window[1] = NULL;
1525 info->button_window[0] = NULL;
1526 info->button_number[1] = -1;
1527 info->button_number[0] = -1;
1528 info->button_x[0] = info->button_x[1] = 0;
1529 info->button_y[0] = info->button_y[1] = 0;
1531 else if ((event->button.time < (info->button_click_time[0] + display->double_click_time)) &&
1532 (event->button.window == info->button_window[0]) &&
1533 (event->button.button == info->button_number[0]) &&
1534 (ABS (event->button.x - info->button_x[0]) <= display->double_click_distance) &&
1535 (ABS (event->button.y - info->button_y[0]) <= display->double_click_distance))
1537 gdk_synthesize_click (display, event, 2);
1539 info->button_click_time[1] = info->button_click_time[0];
1540 info->button_click_time[0] = event->button.time;
1541 info->button_window[1] = info->button_window[0];
1542 info->button_window[0] = event->button.window;
1543 info->button_number[1] = info->button_number[0];
1544 info->button_number[0] = event->button.button;
1545 info->button_x[1] = info->button_x[0];
1546 info->button_x[0] = event->button.x;
1547 info->button_y[1] = info->button_y[0];
1548 info->button_y[0] = event->button.y;
1552 info->button_click_time[1] = 0;
1553 info->button_click_time[0] = event->button.time;
1554 info->button_window[1] = NULL;
1555 info->button_window[0] = event->button.window;
1556 info->button_number[1] = -1;
1557 info->button_number[0] = event->button.button;
1558 info->button_x[1] = 0;
1559 info->button_x[0] = event->button.x;
1560 info->button_y[1] = 0;
1561 info->button_y[0] = event->button.y;
1566 gdk_synthesize_window_state (GdkWindow *window,
1567 GdkWindowState unset_flags,
1568 GdkWindowState set_flags)
1570 GdkEvent temp_event;
1573 g_return_if_fail (window != NULL);
1575 temp_event.window_state.window = window;
1576 temp_event.window_state.type = GDK_WINDOW_STATE;
1577 temp_event.window_state.send_event = FALSE;
1579 old = temp_event.window_state.window->state;
1581 temp_event.window_state.new_window_state = old;
1582 temp_event.window_state.new_window_state |= set_flags;
1583 temp_event.window_state.new_window_state &= ~unset_flags;
1584 temp_event.window_state.changed_mask = temp_event.window_state.new_window_state ^ old;
1586 if (temp_event.window_state.new_window_state == old)
1587 return; /* No actual work to do, nothing changed. */
1589 /* Actually update the field in GdkWindow, this is sort of an odd
1590 * place to do it, but seems like the safest since it ensures we expose no
1591 * inconsistent state to the user.
1594 window->state = temp_event.window_state.new_window_state;
1596 if (temp_event.window_state.changed_mask & GDK_WINDOW_STATE_WITHDRAWN)
1597 _gdk_window_update_viewable (window);
1599 /* We only really send the event to toplevels, since
1600 * all the window states don't apply to non-toplevels.
1601 * Non-toplevels do use the GDK_WINDOW_STATE_WITHDRAWN flag
1602 * internally so we needed to update window->state.
1604 switch (window->window_type)
1606 case GDK_WINDOW_TOPLEVEL:
1607 case GDK_WINDOW_TEMP: /* ? */
1608 gdk_display_put_event (gdk_window_get_display (window), &temp_event);
1611 case GDK_WINDOW_FOREIGN:
1612 case GDK_WINDOW_ROOT:
1613 case GDK_WINDOW_CHILD:
1619 * gdk_display_set_double_click_time:
1620 * @display: a #GdkDisplay
1621 * @msec: double click time in milliseconds (thousandths of a second)
1623 * Sets the double click time (two clicks within this time interval
1624 * count as a double click and result in a #GDK_2BUTTON_PRESS event).
1625 * Applications should <emphasis>not</emphasis> set this, it is a global
1626 * user-configured setting.
1631 gdk_display_set_double_click_time (GdkDisplay *display,
1634 display->double_click_time = msec;
1638 * gdk_set_double_click_time:
1639 * @msec: double click time in milliseconds (thousandths of a second)
1641 * Set the double click time for the default display. See
1642 * gdk_display_set_double_click_time().
1643 * See also gdk_display_set_double_click_distance().
1644 * Applications should <emphasis>not</emphasis> set this, it is a
1645 * global user-configured setting.
1648 gdk_set_double_click_time (guint msec)
1650 gdk_display_set_double_click_time (gdk_display_get_default (), msec);
1654 * gdk_display_set_double_click_distance:
1655 * @display: a #GdkDisplay
1656 * @distance: distance in pixels
1658 * Sets the double click distance (two clicks within this distance
1659 * count as a double click and result in a #GDK_2BUTTON_PRESS event).
1660 * See also gdk_display_set_double_click_time().
1661 * Applications should <emphasis>not</emphasis> set this, it is a global
1662 * user-configured setting.
1667 gdk_display_set_double_click_distance (GdkDisplay *display,
1670 display->double_click_distance = distance;
1673 G_DEFINE_BOXED_TYPE (GdkEvent, gdk_event,
1679 * @name: the name of the setting.
1680 * @value: location to store the value of the setting.
1682 * Obtains a desktop-wide setting, such as the double-click time,
1683 * for the default screen. See gdk_screen_get_setting().
1685 * Returns: %TRUE if the setting existed and a value was stored
1686 * in @value, %FALSE otherwise.
1689 gdk_setting_get (const gchar *name,
1692 return gdk_screen_get_setting (gdk_screen_get_default (), name, value);