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/.
28 #include "gdkinternals.h"
30 typedef struct _GdkIOClosure GdkIOClosure;
31 typedef struct _GdkEventPrivate GdkEventPrivate;
33 #define DOUBLE_CLICK_TIME 250
34 #define TRIPLE_CLICK_TIME 500
35 #define DOUBLE_CLICK_DIST 5
36 #define TRIPLE_CLICK_DIST 5
40 /* Following flag is set for events on the event queue during
41 * translation and cleared afterwards.
43 GDK_EVENT_PENDING = 1 << 0
48 GdkInputFunction function;
49 GdkInputCondition condition;
50 GdkDestroyNotify notify;
54 struct _GdkEventPrivate
61 * Private function declarations
64 GdkFilterReturn gdk_wm_protocols_filter (GdkXEvent *xev,
68 /* Private variable declarations
71 static guint32 button_click_time[2] = { 0, 0}; /* The last 2 button click times. Used
72 * to determine if the latest button click
73 * is part of a double or triple click.
75 static GdkWindow *button_window[2] = { NULL, NULL}; /* The last 2 windows to receive button presses.
76 * Also used to determine if the latest button
77 * click is part of a double or triple click.
79 static guint button_number[2] = { -1, -1 }; /* The last 2 buttons to be pressed.
81 GdkEventFunc gdk_event_func = NULL; /* Callback for events */
82 gpointer gdk_event_data = NULL;
83 GDestroyNotify gdk_event_notify = NULL;
85 GPollFD event_poll_fd;
87 /*********************************************
88 * Functions for maintaining the event queue *
89 *********************************************/
91 /*************************************************************
92 * gdk_event_queue_find_first:
93 * Find the first event on the queue that is not still
98 * Pointer to the list node for that event, or NULL
99 *************************************************************/
102 gdk_event_queue_find_first (void)
104 GList *tmp_list = gdk_queued_events;
108 GdkEventPrivate *event = tmp_list->data;
109 if (!(event->flags & GDK_EVENT_PENDING))
112 tmp_list = g_list_next (tmp_list);
118 /*************************************************************
119 * gdk_event_queue_remove_link:
120 * Remove a specified list node from the event queue.
122 * node: Node to remove.
124 *************************************************************/
127 gdk_event_queue_remove_link (GList *node)
130 node->prev->next = node->next;
132 gdk_queued_events = node->next;
135 node->next->prev = node->prev;
137 gdk_queued_tail = node->prev;
140 /*************************************************************
141 * gdk_event_queue_append:
142 * Append an event onto the tail of the event queue.
144 * event: Event to append.
146 *************************************************************/
149 gdk_event_queue_append (GdkEvent *event)
151 gdk_queued_tail = g_list_append (gdk_queued_tail, event);
153 if (!gdk_queued_events)
154 gdk_queued_events = gdk_queued_tail;
156 gdk_queued_tail = gdk_queued_tail->next;
159 /*************************************************************
160 * gdk_event_handler_set:
163 * func: Callback function to be called for each event.
164 * data: Data supplied to the function
165 * notify: function called when function is no longer needed
168 *************************************************************/
171 gdk_event_handler_set (GdkEventFunc func,
173 GDestroyNotify notify)
175 if (gdk_event_notify)
176 (*gdk_event_notify) (gdk_event_data);
178 gdk_event_func = func;
179 gdk_event_data = data;
180 gdk_event_notify = notify;
184 *--------------------------------------------------------------
187 * Gets the next event.
192 * If an event is waiting that we care about, returns
193 * a pointer to that event, to be freed with gdk_event_free.
194 * Otherwise, returns NULL.
198 *--------------------------------------------------------------
206 return gdk_event_unqueue ();
210 *--------------------------------------------------------------
213 * Gets the next event.
218 * If an event is waiting that we care about, returns
219 * a copy of that event, but does not remove it from
220 * the queue. The pointer is to be freed with gdk_event_free.
221 * Otherwise, returns NULL.
225 *--------------------------------------------------------------
229 gdk_event_peek (void)
233 tmp_list = gdk_event_queue_find_first ();
236 return gdk_event_copy (tmp_list->data);
242 gdk_event_put (GdkEvent *event)
246 g_return_if_fail (event != NULL);
248 new_event = gdk_event_copy (event);
250 gdk_event_queue_append (new_event);
254 *--------------------------------------------------------------
257 * Copy a event structure into new storage.
260 * "event" is the event struct to copy.
263 * A new event structure. Free it with gdk_event_free.
266 * The reference count of the window in the event is increased.
268 *--------------------------------------------------------------
271 static GMemChunk *event_chunk = NULL;
276 GdkEventPrivate *new_event;
278 if (event_chunk == NULL)
279 event_chunk = g_mem_chunk_new ("events",
280 sizeof (GdkEventPrivate),
284 new_event = g_chunk_new (GdkEventPrivate, event_chunk);
285 new_event->flags = 0;
287 return (GdkEvent*) new_event;
291 gdk_event_copy (GdkEvent *event)
295 g_return_val_if_fail (event != NULL, NULL);
297 new_event = gdk_event_new ();
300 if (new_event->any.window)
301 gdk_window_ref (new_event->any.window);
303 switch (event->any.type)
306 case GDK_KEY_RELEASE:
307 new_event->key.string = g_strdup (event->key.string);
310 case GDK_ENTER_NOTIFY:
311 case GDK_LEAVE_NOTIFY:
312 if (event->crossing.subwindow != NULL)
313 gdk_window_ref (event->crossing.subwindow);
318 case GDK_DRAG_MOTION:
319 case GDK_DRAG_STATUS:
321 case GDK_DROP_FINISHED:
322 gdk_drag_context_ref (event->dnd.context);
326 if (event->expose.region)
327 new_event->expose.region = gdk_region_copy (event->expose.region);
337 *--------------------------------------------------------------
340 * Free a event structure obtained from gdk_event_copy. Do not use
341 * with other event structures.
344 * "event" is the event struct to free.
349 * The reference count of the window in the event is decreased and
350 * might be freed, too.
352 *-------------------------------------------------------------- */
355 gdk_event_free (GdkEvent *event)
357 g_return_if_fail (event != NULL);
359 g_assert (event_chunk != NULL); /* paranoid */
361 if (event->any.window)
362 gdk_window_unref (event->any.window);
364 switch (event->any.type)
367 case GDK_KEY_RELEASE:
368 g_free (event->key.string);
371 case GDK_ENTER_NOTIFY:
372 case GDK_LEAVE_NOTIFY:
373 if (event->crossing.subwindow != NULL)
374 gdk_window_unref (event->crossing.subwindow);
379 case GDK_DRAG_MOTION:
380 case GDK_DRAG_STATUS:
382 case GDK_DROP_FINISHED:
383 gdk_drag_context_unref (event->dnd.context);
386 case GDK_BUTTON_PRESS:
387 case GDK_BUTTON_RELEASE:
388 if (event->button.axes)
389 g_free (event->button.axes);
393 if (event->expose.region)
394 gdk_region_destroy (event->expose.region);
397 case GDK_MOTION_NOTIFY:
398 if (event->motion.axes)
399 g_free (event->motion.axes);
406 g_mem_chunk_free (event_chunk, event);
410 * gdk_event_get_time:
411 * @event: a #GdkEvent
413 * Returns the time stamp from @event, if there is one; otherwise
414 * returns #GDK_CURRENT_TIME. If @event is %NULL, returns #GDK_CURRENT_TIME.
416 * Return value: time stamp field from @event
419 gdk_event_get_time (GdkEvent *event)
424 case GDK_MOTION_NOTIFY:
425 return event->motion.time;
426 case GDK_BUTTON_PRESS:
427 case GDK_2BUTTON_PRESS:
428 case GDK_3BUTTON_PRESS:
429 case GDK_BUTTON_RELEASE:
430 return event->button.time;
432 return event->scroll.time;
434 case GDK_KEY_RELEASE:
435 return event->key.time;
436 case GDK_ENTER_NOTIFY:
437 case GDK_LEAVE_NOTIFY:
438 return event->crossing.time;
439 case GDK_PROPERTY_NOTIFY:
440 return event->property.time;
441 case GDK_SELECTION_CLEAR:
442 case GDK_SELECTION_REQUEST:
443 case GDK_SELECTION_NOTIFY:
444 return event->selection.time;
445 case GDK_PROXIMITY_IN:
446 case GDK_PROXIMITY_OUT:
447 return event->proximity.time;
450 case GDK_DRAG_MOTION:
451 case GDK_DRAG_STATUS:
453 case GDK_DROP_FINISHED:
454 return event->dnd.time;
455 case GDK_CLIENT_EVENT:
456 case GDK_VISIBILITY_NOTIFY:
459 case GDK_FOCUS_CHANGE:
466 case GDK_WINDOW_STATE:
467 /* return current time */
471 return GDK_CURRENT_TIME;
475 * gdk_event_get_state:
476 * @event: a #GdkEvent or NULL
477 * @state: return location for state
479 * If the event contains a "state" field, puts that field in @state. Otherwise
480 * stores an empty state (0). Returns %TRUE if there was a state field
481 * in the event. @event may be %NULL, in which case it's treated
482 * as if the event had no state field.
484 * Return value: %TRUE if there was a state field in the event
487 gdk_event_get_state (GdkEvent *event,
488 GdkModifierType *state)
490 g_return_val_if_fail (state != NULL, FALSE);
495 case GDK_MOTION_NOTIFY:
496 *state = event->motion.state;
498 case GDK_BUTTON_PRESS:
499 case GDK_2BUTTON_PRESS:
500 case GDK_3BUTTON_PRESS:
501 case GDK_BUTTON_RELEASE:
502 *state = event->button.state;
505 *state = event->scroll.state;
508 case GDK_KEY_RELEASE:
509 *state = event->key.state;
511 case GDK_ENTER_NOTIFY:
512 case GDK_LEAVE_NOTIFY:
513 *state = event->crossing.state;
515 case GDK_PROPERTY_NOTIFY:
516 *state = event->property.state;
518 case GDK_VISIBILITY_NOTIFY:
519 *state = event->visibility.state;
521 case GDK_CLIENT_EVENT:
524 case GDK_FOCUS_CHANGE:
525 case GDK_SELECTION_CLEAR:
526 case GDK_SELECTION_REQUEST:
527 case GDK_SELECTION_NOTIFY:
528 case GDK_PROXIMITY_IN:
529 case GDK_PROXIMITY_OUT:
532 case GDK_DRAG_MOTION:
533 case GDK_DRAG_STATUS:
535 case GDK_DROP_FINISHED:
542 case GDK_WINDOW_STATE:
552 * gdk_event_get_axis:
553 * @event: a #GdkEvent
554 * @axis_use: the axis use to look for
555 * @value: location to store the value found
557 * Extract the axis value for a particular axis use from
558 * an event structure.
560 * Return value: %TRUE if the specified axis was found, otherwise %FALSE
563 gdk_event_get_axis (GdkEvent *event,
570 g_return_val_if_fail (event != NULL, FALSE);
572 if (axis_use == GDK_AXIS_X || axis_use == GDK_AXIS_Y)
578 case GDK_MOTION_NOTIFY:
586 case GDK_BUTTON_PRESS:
587 case GDK_BUTTON_RELEASE:
591 case GDK_ENTER_NOTIFY:
592 case GDK_LEAVE_NOTIFY:
593 x = event->crossing.x;
594 y = event->crossing.y;
601 if (axis_use == GDK_AXIS_X && value)
603 if (axis_use == GDK_AXIS_Y && value)
608 else if (event->type == GDK_BUTTON_PRESS ||
609 event->type == GDK_BUTTON_RELEASE)
611 device = event->button.device;
612 axes = event->button.axes;
614 else if (event->type == GDK_MOTION_NOTIFY)
616 device = event->motion.device;
617 axes = event->motion.axes;
622 return gdk_device_get_axis (device, axes, axis_use, value);
626 *--------------------------------------------------------------
627 * gdk_set_show_events
629 * Turns on/off the showing of events.
632 * "show_events" is a boolean describing whether or
633 * not to show the events gdk receives.
638 * When "show_events" is TRUE, calls to "gdk_event_get"
639 * will output debugging informatin regarding the event
640 * received to stdout.
642 *--------------------------------------------------------------
646 gdk_set_show_events (gboolean show_events)
649 gdk_debug_flags |= GDK_DEBUG_EVENTS;
651 gdk_debug_flags &= ~GDK_DEBUG_EVENTS;
655 gdk_get_show_events (void)
657 return (gdk_debug_flags & GDK_DEBUG_EVENTS) != 0;
661 gdk_io_destroy (gpointer data)
663 GdkIOClosure *closure = data;
666 closure->notify (closure->data);
671 /* What do we do with G_IO_NVAL?
673 #define READ_CONDITION (G_IO_IN | G_IO_HUP | G_IO_ERR)
674 #define WRITE_CONDITION (G_IO_OUT | G_IO_ERR)
675 #define EXCEPTION_CONDITION (G_IO_PRI)
678 gdk_io_invoke (GIOChannel *source,
679 GIOCondition condition,
682 GdkIOClosure *closure = data;
683 GdkInputCondition gdk_cond = 0;
685 if (condition & READ_CONDITION)
686 gdk_cond |= GDK_INPUT_READ;
687 if (condition & WRITE_CONDITION)
688 gdk_cond |= GDK_INPUT_WRITE;
689 if (condition & EXCEPTION_CONDITION)
690 gdk_cond |= GDK_INPUT_EXCEPTION;
692 if (closure->condition & gdk_cond)
693 closure->function (closure->data, g_io_channel_unix_get_fd (source), gdk_cond);
699 gdk_input_add_full (gint source,
700 GdkInputCondition condition,
701 GdkInputFunction function,
703 GdkDestroyNotify destroy)
706 GdkIOClosure *closure = g_new (GdkIOClosure, 1);
708 GIOCondition cond = 0;
710 closure->function = function;
711 closure->condition = condition;
712 closure->notify = destroy;
713 closure->data = data;
715 if (condition & GDK_INPUT_READ)
716 cond |= READ_CONDITION;
717 if (condition & GDK_INPUT_WRITE)
718 cond |= WRITE_CONDITION;
719 if (condition & GDK_INPUT_EXCEPTION)
720 cond |= EXCEPTION_CONDITION;
722 channel = g_io_channel_unix_new (source);
723 result = g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, cond,
725 closure, gdk_io_destroy);
726 g_io_channel_unref (channel);
732 gdk_input_add (gint source,
733 GdkInputCondition condition,
734 GdkInputFunction function,
737 return gdk_input_add_full (source, condition, function, data, NULL);
741 gdk_input_remove (gint tag)
743 g_source_remove (tag);
747 gdk_event_unqueue (void)
749 GdkEvent *event = NULL;
752 tmp_list = gdk_event_queue_find_first ();
756 event = tmp_list->data;
757 gdk_event_queue_remove_link (tmp_list);
758 g_list_free_1 (tmp_list);
765 gdk_synthesize_click (GdkEvent *event,
770 g_return_if_fail (event != NULL);
773 temp_event.type = (nclicks == 2) ? GDK_2BUTTON_PRESS : GDK_3BUTTON_PRESS;
775 gdk_event_put (&temp_event);
779 gdk_event_button_generate (GdkEvent *event)
781 if ((event->button.time < (button_click_time[1] + TRIPLE_CLICK_TIME)) &&
782 (event->button.window == button_window[1]) &&
783 (event->button.button == button_number[1]))
785 gdk_synthesize_click (event, 3);
787 button_click_time[1] = 0;
788 button_click_time[0] = 0;
789 button_window[1] = NULL;
790 button_window[0] = 0;
791 button_number[1] = -1;
792 button_number[0] = -1;
794 else if ((event->button.time < (button_click_time[0] + DOUBLE_CLICK_TIME)) &&
795 (event->button.window == button_window[0]) &&
796 (event->button.button == button_number[0]))
798 gdk_synthesize_click (event, 2);
800 button_click_time[1] = button_click_time[0];
801 button_click_time[0] = event->button.time;
802 button_window[1] = button_window[0];
803 button_window[0] = event->button.window;
804 button_number[1] = button_number[0];
805 button_number[0] = event->button.button;
809 button_click_time[1] = 0;
810 button_click_time[0] = event->button.time;
811 button_window[1] = NULL;
812 button_window[0] = event->button.window;
813 button_number[1] = -1;
814 button_number[0] = event->button.button;
820 gdk_synthesize_window_state (GdkWindow *window,
821 GdkWindowState unset_flags,
822 GdkWindowState set_flags)
824 GdkEventWindowState temp_event;
827 g_return_if_fail (window != NULL);
829 temp_event.window = window;
830 temp_event.type = GDK_WINDOW_STATE;
831 temp_event.send_event = FALSE;
833 old = ((GdkWindowObject*) temp_event.window)->state;
835 temp_event.changed_mask = (unset_flags | set_flags) ^ old;
836 temp_event.new_window_state = old;
837 temp_event.new_window_state |= set_flags;
838 temp_event.new_window_state &= ~unset_flags;
840 if (temp_event.new_window_state == old)
841 return; /* No actual work to do, nothing changed. */
843 /* Actually update the field in GdkWindow, this is sort of an odd
844 * place to do it, but seems like the safest since it ensures we expose no
845 * inconsistent state to the user.
848 ((GdkWindowObject*) window)->state = temp_event.new_window_state;
850 /* We only really send the event to toplevels, since
851 * all the window states don't apply to non-toplevels.
852 * Non-toplevels do use the GDK_WINDOW_STATE_WITHDRAWN flag
853 * internally so we needed to update window->state.
855 switch (((GdkWindowObject*) window)->window_type)
857 case GDK_WINDOW_TOPLEVEL:
858 case GDK_WINDOW_DIALOG:
859 case GDK_WINDOW_TEMP: /* ? */
860 gdk_event_put ((GdkEvent*) &temp_event);
863 case GDK_WINDOW_FOREIGN:
864 case GDK_WINDOW_ROOT:
865 case GDK_WINDOW_CHILD: